-
Notifications
You must be signed in to change notification settings - Fork 1
/
dhcp.c
200 lines (152 loc) · 4.31 KB
/
dhcp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include "dhcp.h"
#include "udp4.h"
static void fill_dhcp_header (uint32_t xid, dhcp_t *frame,
ipaddr_t src_ip, macaddr_t chaddr,
ipaddr_t dst_ip, uint8_t op) {
bzero(frame, sizeof(dhcp_t));
frame->op = op;
frame->htype = 0x01;
frame->hlen = ETH_ALEN;
frame->xid = xid;
frame->magic = htonl(DHCP_MAGIC);
frame->yiaddr = dst_ip;
frame->siaddr = src_ip;
memcpy(frame->chaddr, chaddr, ETH_ALEN);
}
int send_dhcpreply(raw_iface_t *iface, uint8_t type, uint32_t xid,
macaddr_t src_mac, ipaddr_t src_ip,
macaddr_t dst_mac, ipaddr_t dst_ip,
ipaddr_t gateway, ipaddr_t dns, ipaddr_t netmask,
macaddr_t chaddr, ipaddr_t ciaddr) {
uint8_t buffer[UDP4_MAXLEN];
size_t len;
dhcp_t *frame;
uint32_t ttl;
unsigned opts = 0;
frame = (dhcp_t*)buffer;
len = sizeof(dhcp_t);
fill_dhcp_header(xid ,frame, src_ip, chaddr, ciaddr, DHCP_OPREPLAY);
frame->options[opts++] = DHCP_MSG_TYPE;
frame->options[opts++] = 1;
frame->options[opts++] = type;
frame->options[opts++] = DHCP_SRVID;
frame->options[opts++] = IP_ALEN;
memcpy(frame->options+opts, &src_ip, IP_ALEN);
opts+= IP_ALEN;
frame->options[opts++] = DHCP_LEASETIME;
frame->options[opts++] = 4;
ttl = htonl(86400);
memcpy(frame->options+opts, &ttl, sizeof(uint32_t));
opts+= sizeof(uint32_t);
frame->options[opts++] = DHCP_NETMASK;
frame->options[opts++] = 4;
memcpy(frame->options+opts, &netmask, IP_ALEN);
opts+= IP_ALEN;
frame->options[opts++] = DHCP_ROUTER;
frame->options[opts++] = IP_ALEN;
memcpy(frame->options+opts, &gateway, IP_ALEN);
opts+= IP_ALEN;
frame->options[opts++] = DHCP_DNS;
frame->options[opts++] = IP_ALEN;
memcpy(frame->options+opts, &dns, IP_ALEN);
opts+= IP_ALEN;
frame->options[opts++] = DHCP_TERMINATE;
len+=opts;
return send_udp4(iface,
src_mac, src_ip, 67,
dst_mac, dst_ip, 68,
frame, len, 64);
}
int send_dhcp_discovery(raw_iface_t *iface, macaddr_t src_mac) {
uint8_t buffer[UDP4_MAXLEN];
size_t len;
dhcp_t *frame;
unsigned opts = 0;
frame = (dhcp_t*)buffer;
len = sizeof(dhcp_t);
bzero(frame, sizeof(dhcp_t));
frame->op = DHCP_OPREQUEST;
frame->htype = 0x01;
frame->hlen = ETH_ALEN;
frame->xid = random();
frame->magic = htonl(DHCP_MAGIC);
memcpy(frame->chaddr, src_mac, ETH_ALEN);
frame->options[opts++] = DHCP_MSG_TYPE;
frame->options[opts++] = 1;
frame->options[opts++] = DHCP_DISCOVERY;
frame->options[opts++] = DHCP_CLIENT_ID;
frame->options[opts++] = 7;
frame->options[opts++] = 1;
memcpy(frame->options+opts, src_mac, 6);
opts+=6;
frame->options[opts++] = DHCP_TERMINATE;
len+=opts;
return send_udp4(iface, src_mac, 0, 68,
broadcast_macaddr, -1, 67,
frame, len, 64);
}
int dhcp_parse_request(dhcp_t *dhcp, ipaddr_t *rq_addr) {
uint8_t *a;
a = dhcp->options;
if(dhcp->magic != htonl(DHCP_MAGIC))
return -2;
while(*a != DHCP_TERMINATE) {
if(*a == 50) {
memcpy(rq_addr, a+2, IP_ALEN);
return a-(dhcp->options);
}
a += *a ? *(a+1)+2 : 1;
}
return -1;
}
int dhcp_parse_dns(dhcp_t *dhcp, ipaddr_t *dns) {
uint8_t *a;
a = dhcp->options;
if(dhcp->magic != htonl(DHCP_MAGIC))
return -2;
while(*a != DHCP_TERMINATE) {
if(*a == 6) {
memcpy(dns, a+2, IP_ALEN);
return a-(dhcp->options);
}
a += *a ? *(a+1)+2 : 1;
}
return -1;
}
int dhcp_parse_netmask(dhcp_t *dhcp, ipaddr_t *netmask) {
uint8_t *a;
a = dhcp->options;
if(dhcp->magic != htonl(DHCP_MAGIC))
return -2;
while(*a != DHCP_TERMINATE) {
if(*a == 1) {
memcpy(netmask, a+2, IP_ALEN);
return a-(dhcp->options);
}
a += *a ? *(a+1)+2 : 1;
}
return -1;
}
int dhcp_parse_type(dhcp_t *dhcp, uint8_t *type) {
uint8_t *a;
a = dhcp->options;
if(dhcp->magic != htonl(DHCP_MAGIC))
return -2;
while(*a != DHCP_TERMINATE) {
if(*a == DHCP_MSG_TYPE) {
*type = *(a+2);
return a-(dhcp->options);
}
a += *a ? *(a+1)+2 : 1;
}
return -1;
}
/* Local Variables: */
/* mode: c */
/* tab-width: 2 */
/* indent-tabs-mode: nil */
/* End: */