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