Minor fixes
[project/odhcpd.git] / src / dhcpv4.c
1 /**
2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 */
15
16 #include <time.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <resolv.h>
23 #include <net/if.h>
24 #include <net/if_arp.h>
25 #include <netinet/ip.h>
26 #include <sys/ioctl.h>
27 #include <sys/timerfd.h>
28 #include <arpa/inet.h>
29
30 #include "odhcpd.h"
31 #include "dhcpv4.h"
32 #include "dhcpv6.h"
33
34
35 static void handle_dhcpv4(void *addr, void *data, size_t len,
36 struct interface *iface);
37 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
38 enum dhcpv4_msg msg, const uint8_t *mac, struct in_addr reqaddr,
39 const char *hostname);
40
41
42 // Create socket and register events
43 int init_dhcpv4(void)
44 {
45 return 0;
46 }
47
48
49 int setup_dhcpv4_interface(struct interface *iface, bool enable)
50 {
51 if (iface->dhcpv4_event.uloop.fd > 0) {
52 uloop_fd_delete(&iface->dhcpv4_event.uloop);
53 close(iface->dhcpv4_event.uloop.fd);
54 iface->dhcpv4_event.uloop.fd = -1;
55 }
56
57 if (iface->dhcpv4 && enable) {
58 if (!iface->dhcpv4_assignments.next)
59 INIT_LIST_HEAD(&iface->dhcpv4_assignments);
60
61 int sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
62
63 // Basic IPv6 configuration
64 int val = 1;
65 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
66 setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));
67 setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val));
68
69 val = IPTOS_PREC_INTERNETCONTROL;
70 setsockopt(sock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
71
72 val = IP_PMTUDISC_DONT;
73 setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val));
74
75 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE,
76 iface->ifname, strlen(iface->ifname));
77
78 struct sockaddr_in bind_addr = {AF_INET, htons(DHCPV4_SERVER_PORT),
79 {INADDR_ANY}, {0}};
80
81 if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr))) {
82 syslog(LOG_ERR, "Failed to open DHCPv4 server socket: %s",
83 strerror(errno));
84 return -1;
85 }
86
87
88 if (ntohl(iface->dhcpv4_start.s_addr) > ntohl(iface->dhcpv4_end.s_addr)) {
89 syslog(LOG_ERR, "Invalid DHCP range");
90 return -1;
91 }
92
93 // Create a range if not specified
94 struct ifreq ifreq;
95 strncpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name));
96
97 struct sockaddr_in *saddr = (struct sockaddr_in*)&ifreq.ifr_addr;
98 struct sockaddr_in *smask = (struct sockaddr_in*)&ifreq.ifr_netmask;
99 if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) &&
100 !(iface->dhcpv4_end.s_addr & htonl(0xffff0000)) &&
101 !ioctl(sock, SIOCGIFADDR, &ifreq)) {
102 struct in_addr addr = saddr->sin_addr;
103
104 ioctl(sock, SIOCGIFNETMASK, &ifreq);
105 struct in_addr mask = smask->sin_addr;
106
107 uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
108 uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
109
110 if (start && end && start < end &&
111 start > ntohl(addr.s_addr & ~mask.s_addr) &&
112 (start & ntohl(mask.s_addr)) == start &&
113 (end & ntohl(mask.s_addr)) == end) {
114 iface->dhcpv4_start.s_addr = htonl(start) |
115 (addr.s_addr & mask.s_addr);
116 iface->dhcpv4_end.s_addr = htonl(end) |
117 (addr.s_addr & mask.s_addr);
118 } else if (ntohl(mask.s_addr) <= 0xffffffc0) {
119 start = addr.s_addr & mask.s_addr;
120 end = addr.s_addr & mask.s_addr;
121
122 if (ntohl(mask.s_addr) <= 0xffffff00) {
123 iface->dhcpv4_start.s_addr = start | htonl(100);
124 iface->dhcpv4_end.s_addr = end | htonl(250);
125 } else {
126 iface->dhcpv4_start.s_addr = start | htonl(10);
127 iface->dhcpv4_end.s_addr = end | htonl(59);
128 }
129 }
130
131
132 }
133
134 // Parse static entries
135 struct lease *lease;
136 list_for_each_entry(lease, &leases, head) {
137 // Construct entry
138 size_t hostlen = strlen(lease->hostname) + 1;
139 struct dhcpv4_assignment *a = calloc(1, sizeof(*a) + hostlen);
140
141 a->addr = ntohl(lease->ipaddr.s_addr);
142 memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
143 memcpy(a->hostname, lease->hostname, hostlen);
144
145 // Assign to all interfaces
146 struct dhcpv4_assignment *c;
147 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
148 if (c->addr > a->addr) {
149 list_add_tail(&a->head, &c->head);
150 } else if (c->addr == a->addr) {
151 // Already an assignment with that number
152 break;
153 }
154 }
155
156 if (!a->head.next)
157 free(a);
158 }
159
160 // Clean invalid assignments
161 struct dhcpv4_assignment *a, *n;
162 list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
163 if ((htonl(a->addr) & smask->sin_addr.s_addr) !=
164 (saddr->sin_addr.s_addr & smask->sin_addr.s_addr)) {
165 list_del(&a->head);
166 free(a);
167 }
168 }
169
170
171 if (iface->dhcpv4_leasetime < 60)
172 iface->dhcpv4_leasetime = 43200;
173
174 iface->dhcpv4_event.uloop.fd = sock;
175 iface->dhcpv4_event.handle_dgram = handle_dhcpv4;
176 odhcpd_register(&iface->dhcpv4_event);
177 } else if (iface->dhcpv4_assignments.next) {
178 while (!list_empty(&iface->dhcpv4_assignments)) {
179 struct dhcpv4_assignment *a = list_first_entry(&iface->dhcpv4_assignments,
180 struct dhcpv4_assignment, head);
181 list_del(&a->head);
182 free(a->hostname);
183 free(a);
184 }
185
186 }
187 return 0;
188 }
189
190
191 static void dhcpv4_put(struct dhcpv4_message *msg, uint8_t **cookie,
192 uint8_t type, uint8_t len, const void *data)
193 {
194 uint8_t *c = *cookie;
195 if (*cookie + 2 + len > (uint8_t*)&msg[1])
196 return;
197
198 *c++ = type;
199 *c++ = len;
200 memcpy(c, data, len);
201
202 *cookie = c + len;
203 }
204
205
206 // Simple DHCPv6-server for information requests
207 static void handle_dhcpv4(void *addr, void *data, size_t len,
208 struct interface *iface)
209 {
210 if (!iface->dhcpv4)
211 return;
212
213 struct dhcpv4_message *req = data;
214 if (len < offsetof(struct dhcpv4_message, options) + 4 ||
215 req->op != DHCPV4_BOOTREQUEST || req->hlen != 6)
216 return;
217
218 int sock = iface->dhcpv4_event.uloop.fd;
219 struct sockaddr_in ifaddr;
220 struct sockaddr_in ifnetmask;
221
222 syslog(LOG_NOTICE, "Got DHCPv4 request");
223
224 struct ifreq ifreq;
225 memcpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name));
226 if (ioctl(sock, SIOCGIFADDR, &ifreq)) {
227 syslog(LOG_WARNING, "DHCPv4 failed to detect address: %s", strerror(errno));
228 return;
229 }
230
231 memcpy(&ifaddr, &ifreq.ifr_addr, sizeof(ifaddr));
232 if (ioctl(sock, SIOCGIFNETMASK, &ifreq))
233 return;
234
235 memcpy(&ifnetmask, &ifreq.ifr_netmask, sizeof(ifnetmask));
236 uint32_t network = ifaddr.sin_addr.s_addr & ifnetmask.sin_addr.s_addr;
237
238 if ((iface->dhcpv4_start.s_addr & ifnetmask.sin_addr.s_addr) != network ||
239 (iface->dhcpv4_end.s_addr & ifnetmask.sin_addr.s_addr) != network) {
240 syslog(LOG_WARNING, "DHCPv4 range out of assigned network");
241 return;
242 }
243
244 struct ifreq ifr = {.ifr_name = ""};
245 strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
246
247 struct dhcpv4_message reply = {
248 .op = DHCPV4_BOOTREPLY,
249 .htype = 1,
250 .hlen = 6,
251 .hops = 0,
252 .xid = req->xid,
253 .secs = 0,
254 .flags = req->flags,
255 .ciaddr = {INADDR_ANY},
256 .giaddr = req->giaddr,
257 .siaddr = ifaddr.sin_addr,
258 };
259 memcpy(reply.chaddr, req->chaddr, sizeof(reply.chaddr));
260
261 reply.options[0] = 0x63;
262 reply.options[1] = 0x82;
263 reply.options[2] = 0x53;
264 reply.options[3] = 0x63;
265
266 uint8_t *cookie = &reply.options[4];
267 uint8_t reqmsg = DHCPV4_MSG_REQUEST;
268 uint8_t msg = DHCPV4_MSG_ACK;
269
270 struct in_addr reqaddr = {INADDR_ANY};
271 char hostname[256];
272 hostname[0] = 0;
273
274 uint8_t *start = &req->options[4];
275 uint8_t *end = ((uint8_t*)data) + len;
276 struct dhcpv4_option *opt;
277 dhcpv4_for_each_option(start, end, opt) {
278 if (opt->type == DHCPV4_OPT_MESSAGE && opt->len == 1) {
279 reqmsg = opt->data[0];
280 } else if (opt->type == DHCPV4_OPT_HOSTNAME && opt->len > 0) {
281 memcpy(hostname, opt->data, opt->len);
282 hostname[opt->len] = 0;
283 } else if (opt->type == DHCPV4_OPT_IPADDRESS && opt->len == 4) {
284 memcpy(&reqaddr, opt->data, 4);
285 } else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) {
286 if (memcmp(opt->data, &ifaddr.sin_addr, 4))
287 return;
288 }
289 }
290
291 if (reqmsg != DHCPV4_MSG_DISCOVER && reqmsg != DHCPV4_MSG_REQUEST &&
292 reqmsg != DHCPV4_MSG_INFORM && reqmsg != DHCPV4_MSG_DECLINE &&
293 reqmsg != DHCPV4_MSG_RELEASE)
294 return;
295
296 struct dhcpv4_assignment *lease = NULL;
297 if (reqmsg != DHCPV4_MSG_INFORM)
298 lease = dhcpv4_lease(iface, reqmsg, req->chaddr, reqaddr, hostname);
299
300 if (!lease) {
301 if (reqmsg == DHCPV4_MSG_REQUEST)
302 msg = DHCPV4_MSG_NAK;
303 else if (reqmsg == DHCPV4_MSG_DISCOVER)
304 return;
305 } else if (reqmsg == DHCPV4_MSG_DISCOVER) {
306 msg = DHCPV4_MSG_OFFER;
307 }
308
309 if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
310 return;
311
312 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg);
313 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SERVERID, 4, &ifaddr.sin_addr);
314
315 if (lease) {
316 reply.yiaddr.s_addr = htonl(lease->addr);
317
318 uint32_t val = htonl(iface->dhcpv4_leasetime);
319 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_LEASETIME, 4, &val);
320
321 val = htonl(500 * iface->dhcpv4_leasetime / 1000);
322 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_RENEW, 4, &val);
323
324 val = htonl(875 * iface->dhcpv4_leasetime / 1000);
325 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_REBIND, 4, &val);
326
327 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NETMASK, 4, &ifnetmask.sin_addr);
328
329 if (lease->hostname[0])
330 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_HOSTNAME,
331 strlen(lease->hostname), lease->hostname);
332
333 if (!ioctl(sock, SIOCGIFBRDADDR, &ifr)) {
334 struct sockaddr_in *ina = (struct sockaddr_in*)&ifr.ifr_broadaddr;
335 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_BROADCAST, 4, &ina->sin_addr);
336 }
337 }
338
339 if (!ioctl(sock, SIOCGIFMTU, &ifr)) {
340 uint16_t mtu = htons(ifr.ifr_mtu);
341 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MTU, 2, &mtu);
342 }
343
344 if (iface->search) {
345 char b[256];
346 if (dn_expand(iface->search, iface->search + iface->search_len,
347 iface->search, b, sizeof(b)) > 0)
348 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DOMAIN, strlen(b), b);
349 } else if (!res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
350 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DOMAIN,
351 strlen(_res.dnsrch[0]), _res.dnsrch[0]);
352 }
353
354 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER, 4, &ifaddr.sin_addr);
355
356
357
358 if (iface->dhcpv4_dns_cnt == 0)
359 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &ifaddr.sin_addr);
360 else
361 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER,
362 4 * iface->dhcpv4_dns_cnt, iface->dhcpv4_dns);
363
364
365 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_END, 0, NULL);
366
367 struct sockaddr_in dest = *((struct sockaddr_in*)addr);
368 if (req->giaddr.s_addr) {
369 dest.sin_addr = req->giaddr;
370 dest.sin_port = htons(DHCPV4_SERVER_PORT);
371 } else if (req->ciaddr.s_addr && req->ciaddr.s_addr != dest.sin_addr.s_addr) {
372 dest.sin_addr = req->ciaddr;
373 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
374 } else if ((ntohs(req->flags) & DHCPV4_FLAG_BROADCAST) ||
375 req->hlen != reply.hlen) {
376 dest.sin_addr.s_addr = INADDR_BROADCAST;
377 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
378 } else {
379 dest.sin_addr = reply.yiaddr;
380 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
381
382 struct arpreq arp = {.arp_flags = ATF_COM};
383 memcpy(arp.arp_ha.sa_data, req->chaddr, 6);
384 memcpy(&arp.arp_pa, &dest, sizeof(arp.arp_pa));
385 memcpy(arp.arp_dev, iface->ifname, sizeof(arp.arp_dev));
386 ioctl(sock, SIOCSARP, &arp);
387 }
388
389 sendto(sock, &reply, sizeof(reply), MSG_DONTWAIT,
390 (struct sockaddr*)&dest, sizeof(dest));
391 }
392
393
394 static bool dhcpv4_assign(struct interface *iface,
395 struct dhcpv4_assignment *assign, uint32_t raddr)
396 {
397 const unsigned tries = 10;
398 uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
399 uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
400 uint32_t count = end - start + 1;
401
402 // Seed RNG with checksum of DUID
403 uint32_t seed = 0;
404 for (size_t i = 0; i < sizeof(assign->hwaddr); ++i)
405 seed += assign->hwaddr[i];
406 srand(seed);
407
408 // Try to assign up to 100x
409 for (unsigned i = 0; i < tries; ++i) {
410 uint32_t try = (((uint32_t)rand()) % count) + start;
411 if (i == 0 && raddr >= start && raddr <= end)
412 try = raddr;
413 else if (i == tries - 1)
414 try = start;
415
416 if (list_empty(&iface->dhcpv4_assignments)) {
417 assign->addr = try;
418 list_add(&assign->head, &iface->dhcpv4_assignments);
419 return true;
420 }
421
422 struct dhcpv4_assignment *c;
423 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
424 if (c->addr > try) {
425 assign->addr = try;
426 list_add_tail(&assign->head, &c->head);
427 return true;
428 } else if (c->addr == try) {
429 if (i < tries - 1)
430 break;
431 else
432 ++try;
433 }
434 }
435 }
436
437 return false;
438 }
439
440
441 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
442 enum dhcpv4_msg msg, const uint8_t *mac, struct in_addr reqaddr,
443 const char *hostname)
444 {
445 struct dhcpv4_assignment *lease = NULL;
446 uint32_t raddr = ntohl(reqaddr.s_addr);
447 time_t now = odhcpd_time();
448
449 struct dhcpv4_assignment *c, *n, *a = NULL;
450 list_for_each_entry_safe(c, n, &iface->dhcpv4_assignments, head) {
451 if (c->addr == raddr && !memcmp(c->hwaddr, mac, 6)) {
452 a = c;
453 break;
454 } else if (c->valid_until < now) {
455 list_del(&c->head);
456 free(c);
457 }
458 }
459
460 bool update_state = false;
461 if (msg == DHCPV4_MSG_DISCOVER || msg == DHCPV4_MSG_REQUEST) {
462 bool assigned = !!a;
463 size_t hostlen = strlen(hostname) + 1;
464
465 if (!a && !iface->no_dynamic_dhcp) { // Create new binding
466 a = calloc(1, sizeof(*a) + hostlen);
467 memcpy(a->hwaddr, mac, sizeof(a->hwaddr));
468 memcpy(a->hostname, hostname, hostlen);
469
470 assigned = dhcpv4_assign(iface, a, raddr);
471 }
472
473 if (assigned && !a->hostname[0] && hostname) {
474 a = realloc(a, sizeof(*a) + hostlen);
475 memcpy(a->hostname, hostname, hostlen);
476
477 // Fixup list
478 a->head.next->prev = &a->head;
479 a->head.prev->next = &a->head;
480 }
481
482 // Was only a solicitation: mark binding for removal
483 if (assigned && a->valid_until < now) {
484 a->valid_until = (msg == DHCPV4_MSG_DISCOVER) ? 0 :
485 (now + iface->dhcpv4_leasetime);
486 } else if (!assigned && a) { // Cleanup failed assignment
487 free(a);
488 a = NULL;
489 }
490
491 if (assigned && a)
492 lease = a;
493 } else if (msg == DHCPV4_MSG_RELEASE) {
494 if (a) {
495 a->valid_until = 0;
496 update_state = true;
497 }
498 } else if (msg == DHCPV4_MSG_DECLINE) {
499 memset(a->hwaddr, 0, sizeof(a->hwaddr));
500 a->valid_until = now + 3600; // Block address for 1h
501 update_state = true;
502 }
503
504 if (update_state)
505 dhcpv6_write_statefile();
506
507 return lease;
508 }
509