3386abbeaa721e66de4154ae931c06ac8556d564
[project/odhcpd.git] / src / dhcpv4.c
1 /**
2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2016 Hans Dedecker <dedeckeh@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License v2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 *
15 */
16
17 #include <time.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <resolv.h>
24 #include <limits.h>
25 #include <net/if.h>
26 #include <net/if_arp.h>
27 #include <netinet/ip.h>
28 #include <sys/ioctl.h>
29 #include <sys/timerfd.h>
30 #include <arpa/inet.h>
31
32 #include <libubox/md5.h>
33
34 #include "odhcpd.h"
35 #include "dhcpv4.h"
36 #include "dhcpv6.h"
37
38 static void dhcpv4_netevent_cb(unsigned long event, struct netevent_handler_info *info);
39 static int setup_dhcpv4_addresses(struct interface *iface);
40 static void update_static_assignments(struct interface *iface);
41 static bool addr_is_fr_ip(struct interface *iface, struct in_addr *addr);
42 static void valid_until_cb(struct uloop_timeout *event);
43 static void handle_addrlist_change(struct interface *iface);
44 static void free_dhcpv4_assignment(struct dhcpv4_assignment *a);
45 static void dhcpv4_fr_start(struct dhcpv4_assignment *a);
46 static void dhcpv4_fr_rand_delay(struct dhcpv4_assignment *a);
47 static void dhcpv4_fr_stop(struct dhcpv4_assignment *a);
48 static void handle_dhcpv4(void *addr, void *data, size_t len,
49 struct interface *iface, void *dest_addr);
50 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
51 enum dhcpv4_msg msg, const uint8_t *mac, const uint32_t reqaddr,
52 uint32_t *leasetime, const char *hostname, const size_t hostname_len,
53 const bool accept_fr_nonce, bool *incl_fr_opt, uint32_t *fr_serverid);
54
55 static struct netevent_handler dhcpv4_netevent_handler = { .cb = dhcpv4_netevent_cb, };
56 static struct uloop_timeout valid_until_timeout = {.cb = valid_until_cb};
57 static uint32_t serial = 0;
58
59 struct odhcpd_ref_ip {
60 struct list_head head;
61 int ref_cnt;
62 struct odhcpd_ipaddr addr;
63 };
64
65 /* Create socket and register events */
66 int dhcpv4_init(void)
67 {
68 uloop_timeout_set(&valid_until_timeout, 1000);
69 netlink_add_netevent_handler(&dhcpv4_netevent_handler);
70
71 return 0;
72 }
73
74 int dhcpv4_setup_interface(struct interface *iface, bool enable)
75 {
76 int ret = 0;
77
78 if (iface->dhcpv4_event.uloop.fd > 0) {
79 uloop_fd_delete(&iface->dhcpv4_event.uloop);
80 close(iface->dhcpv4_event.uloop.fd);
81 iface->dhcpv4_event.uloop.fd = -1;
82 }
83
84 if (iface->dhcpv4 && enable) {
85 struct sockaddr_in bind_addr = {AF_INET, htons(DHCPV4_SERVER_PORT),
86 {INADDR_ANY}, {0}};
87 int val = 1;
88
89 if (!iface->dhcpv4_assignments.next)
90 INIT_LIST_HEAD(&iface->dhcpv4_assignments);
91
92 if (!iface->dhcpv4_fr_ips.next)
93 INIT_LIST_HEAD(&iface->dhcpv4_fr_ips);
94
95 iface->dhcpv4_event.uloop.fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
96 if (iface->dhcpv4_event.uloop.fd < 0) {
97 syslog(LOG_ERR, "socket(AF_INET): %m");
98 ret = -1;
99 goto out;
100 }
101
102 /* Basic IPv4 configuration */
103 if (setsockopt(iface->dhcpv4_event.uloop.fd, SOL_SOCKET, SO_REUSEADDR,
104 &val, sizeof(val)) < 0) {
105 syslog(LOG_ERR, "setsockopt(SO_REUSEADDR): %m");
106 ret = -1;
107 goto out;
108 }
109
110 if (setsockopt(iface->dhcpv4_event.uloop.fd, SOL_SOCKET, SO_BROADCAST,
111 &val, sizeof(val)) < 0) {
112 syslog(LOG_ERR, "setsockopt(SO_BROADCAST): %m");
113 ret = -1;
114 goto out;
115 }
116
117 if (setsockopt(iface->dhcpv4_event.uloop.fd, IPPROTO_IP, IP_PKTINFO,
118 &val, sizeof(val)) < 0) {
119 syslog(LOG_ERR, "setsockopt(IP_PKTINFO): %m");
120 ret = -1;
121 goto out;
122 }
123
124 val = IPTOS_PREC_INTERNETCONTROL;
125 if (setsockopt(iface->dhcpv4_event.uloop.fd, IPPROTO_IP, IP_TOS,
126 &val, sizeof(val)) < 0) {
127 syslog(LOG_ERR, "setsockopt(IP_TOS): %m");
128 ret = -1;
129 goto out;
130 }
131
132 val = IP_PMTUDISC_DONT;
133 if (setsockopt(iface->dhcpv4_event.uloop.fd, IPPROTO_IP, IP_MTU_DISCOVER,
134 &val, sizeof(val)) < 0) {
135 syslog(LOG_ERR, "setsockopt(IP_MTU_DISCOVER): %m");
136 ret = -1;
137 goto out;
138 }
139
140 if (setsockopt(iface->dhcpv4_event.uloop.fd, SOL_SOCKET, SO_BINDTODEVICE,
141 iface->ifname, strlen(iface->ifname)) < 0) {
142 syslog(LOG_ERR, "setsockopt(SO_BINDTODEVICE): %m");
143 ret = -1;
144 goto out;
145 }
146
147 if (bind(iface->dhcpv4_event.uloop.fd, (struct sockaddr*)&bind_addr,
148 sizeof(bind_addr)) < 0) {
149 syslog(LOG_ERR, "bind(): %m");
150 ret = -1;
151 goto out;
152 }
153
154 if (setup_dhcpv4_addresses(iface) < 0) {
155 ret = -1;
156 goto out;
157 }
158
159 iface->dhcpv4_event.handle_dgram = handle_dhcpv4;
160 odhcpd_register(&iface->dhcpv4_event);
161
162 update_static_assignments(iface);
163 } else if (iface->dhcpv4_assignments.next) {
164 while (!list_empty(&iface->dhcpv4_assignments))
165 free_dhcpv4_assignment(list_first_entry(&iface->dhcpv4_assignments,
166 struct dhcpv4_assignment, head));
167 }
168
169 out:
170 if (ret < 0 && iface->dhcpv4_event.uloop.fd > 0) {
171 close(iface->dhcpv4_event.uloop.fd);
172 iface->dhcpv4_event.uloop.fd = -1;
173 }
174
175 return ret;
176 }
177
178
179 static void dhcpv4_netevent_cb(unsigned long event, struct netevent_handler_info *info)
180 {
181 struct interface *iface = info->iface;
182
183 if (!iface || iface->dhcpv4 == MODE_DISABLED)
184 return;
185
186 switch (event) {
187 case NETEV_IFINDEX_CHANGE:
188 dhcpv4_setup_interface(iface, true);
189 break;
190 case NETEV_ADDRLIST_CHANGE:
191 handle_addrlist_change(iface);
192 break;
193 default:
194 break;
195 }
196 }
197
198 static struct dhcpv4_assignment *find_assignment_by_hwaddr(struct interface *iface, const uint8_t *hwaddr)
199 {
200 struct dhcpv4_assignment *a;
201
202 list_for_each_entry(a, &iface->dhcpv4_assignments, head)
203 if (!memcmp(a->hwaddr, hwaddr, 6))
204 return a;
205
206 return NULL;
207 }
208
209 static struct dhcpv4_assignment *find_assignment_by_addr(struct interface *iface, const uint32_t addr)
210 {
211 struct dhcpv4_assignment *a;
212
213 list_for_each_entry(a, &iface->dhcpv4_assignments, head)
214 if (a->addr == addr)
215 return a;
216
217 return NULL;
218 }
219
220 static int setup_dhcpv4_addresses(struct interface *iface)
221 {
222 iface->dhcpv4_start_ip.s_addr = INADDR_ANY;
223 iface->dhcpv4_end_ip.s_addr = INADDR_ANY;
224 iface->dhcpv4_local.s_addr = INADDR_ANY;
225 iface->dhcpv4_bcast.s_addr = INADDR_ANY;
226 iface->dhcpv4_mask.s_addr = INADDR_ANY;
227
228 /* Sanity checks */
229 if (iface->dhcpv4_start.s_addr & htonl(0xffff0000) ||
230 iface->dhcpv4_end.s_addr & htonl(0xffff0000) ||
231 ntohl(iface->dhcpv4_start.s_addr) > ntohl(iface->dhcpv4_end.s_addr)) {
232 syslog(LOG_ERR, "invalid DHCP range for %s", iface->name);
233 return -1;
234 }
235
236 if (!iface->addr4_len) {
237 syslog(LOG_WARNING, "no network(s) available on %s", iface->name);
238 return -1;
239 }
240
241 uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
242 uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
243
244 for (size_t i = 0; i < iface->addr4_len && start && end; i++) {
245 struct in_addr *addr = &iface->addr4[i].addr.in;
246 struct in_addr mask;
247
248 if (addr_is_fr_ip(iface, addr))
249 continue;
250
251 odhcpd_bitlen2netmask(false, iface->addr4[i].prefix, &mask);
252 if ((start & ntohl(~mask.s_addr)) == start &&
253 (end & ntohl(~mask.s_addr)) == end) {
254 iface->dhcpv4_start_ip.s_addr = htonl(start) |
255 (addr->s_addr & mask.s_addr);
256 iface->dhcpv4_end_ip.s_addr = htonl(end) |
257 (addr->s_addr & mask.s_addr);
258 iface->dhcpv4_local = *addr;
259 iface->dhcpv4_bcast = iface->addr4[i].broadcast;
260 iface->dhcpv4_mask = mask;
261 return 0;
262 }
263 }
264
265 /* Don't allocate IP range for subnets bigger than 28 */
266 if (iface->addr4[0].prefix > 28) {
267 syslog(LOG_WARNING, "auto allocation of DHCP range fails on %s", iface->name);
268 return -1;
269 }
270
271 iface->dhcpv4_local = iface->addr4[0].addr.in;
272 iface->dhcpv4_bcast = iface->addr4[0].broadcast;
273 odhcpd_bitlen2netmask(false, iface->addr4[0].prefix, &iface->dhcpv4_mask);
274 end = start = iface->dhcpv4_local.s_addr & iface->dhcpv4_mask.s_addr;
275
276 /* Auto allocate ranges */
277 if (ntohl(iface->dhcpv4_mask.s_addr) <= 0xffffff00) {
278 iface->dhcpv4_start_ip.s_addr = start | htonl(100);
279 iface->dhcpv4_end_ip.s_addr = end | htonl(250);
280 } else if (ntohl(iface->dhcpv4_mask.s_addr) <= 0xffffffc0) {
281 iface->dhcpv4_start_ip.s_addr = start | htonl(10);
282 iface->dhcpv4_end_ip.s_addr = end | htonl(60);
283 } else if (ntohl(iface->dhcpv4_mask.s_addr) <= 0xffffffe0) {
284 iface->dhcpv4_start_ip.s_addr = start | htonl(10);
285 iface->dhcpv4_end_ip.s_addr = end | htonl(30);
286 } else {
287 iface->dhcpv4_start_ip.s_addr = start | htonl(3);
288 iface->dhcpv4_end_ip.s_addr = end | htonl(12);
289 }
290
291 return 0;
292 }
293
294 static void update_static_assignments(struct interface *iface)
295 {
296 struct dhcpv4_assignment *a, *c;
297
298 /* Cleanup static entries not belonging to the network */
299 list_for_each_entry_safe(a, c, &iface->dhcpv4_assignments, head) {
300 if ((a->flags & OAF_STATIC) &&
301 ((a->addr & iface->dhcpv4_mask.s_addr) !=
302 (iface->dhcpv4_start.s_addr & iface->dhcpv4_mask.s_addr)))
303 free_dhcpv4_assignment(a);
304 }
305
306 /* Parse static entries */
307 struct lease *lease;
308 list_for_each_entry(lease, &leases, head) {
309 if ((iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr) !=
310 (lease->ipaddr.s_addr & iface->dhcpv4_mask.s_addr)) {
311 continue;
312 }
313
314 a = find_assignment_by_hwaddr(iface, lease->mac.ether_addr_octet);
315
316 if (!a) {
317 /* Check if there's an assignment with the specified IP address */
318 if (find_assignment_by_addr(iface, lease->ipaddr.s_addr))
319 continue;
320
321 /* Construct entry */
322 a = calloc(1, sizeof(*a));
323 if (!a) {
324 syslog(LOG_ERR, "Calloc failed for static lease on interface %s",
325 iface->ifname);
326 continue;
327 }
328 memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
329 }
330
331 a->leasetime = lease->dhcpv4_leasetime;
332
333 a->addr = lease->ipaddr.s_addr;
334 /* Static assignment */
335 a->flags |= OAF_STATIC;
336 /* Infinite valid */
337 a->valid_until = 0;
338 a->iface = iface;
339 if (lease->hostname[0]) {
340 free(a->hostname);
341 a->hostname = strdup(lease->hostname);
342 }
343
344 /* Assign to all interfaces */
345 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
346 if (ntohl(c->addr) > ntohl(a->addr)) {
347 list_add_tail(&a->head, &c->head);
348 break;
349 }
350 }
351
352 if (&c->head == &iface->dhcpv4_assignments)
353 list_add(&a->head, &iface->dhcpv4_assignments);
354 }
355 }
356
357 static void inc_ref_cnt_ip(struct odhcpd_ref_ip **ptr, struct odhcpd_ref_ip *ip)
358 {
359 *ptr = ip;
360 ip->ref_cnt++;
361 }
362
363 static void decr_ref_cnt_ip(struct odhcpd_ref_ip **ptr, struct interface *iface)
364 {
365 struct odhcpd_ref_ip *ip = *ptr;
366
367 if (--ip->ref_cnt == 0) {
368 netlink_setup_addr(&ip->addr, iface->ifindex, false, false);
369
370 list_del(&ip->head);
371 free(ip);
372 }
373
374 *ptr = NULL;
375 }
376
377 static bool addr_is_fr_ip(struct interface *iface, struct in_addr *addr)
378 {
379 struct odhcpd_ref_ip *p;
380
381 list_for_each_entry(p, &iface->dhcpv4_fr_ips, head) {
382 if (addr->s_addr == p->addr.addr.in.s_addr)
383 return true;
384 }
385
386 return false;
387 }
388
389 static bool leases_require_fr(struct interface *iface, struct odhcpd_ipaddr *addr,
390 uint32_t mask)
391 {
392 struct dhcpv4_assignment *a = NULL;
393 struct odhcpd_ref_ip *fr_ip = NULL;
394
395 list_for_each_entry(a, &iface->dhcpv4_assignments, head) {
396 if ((a->accept_fr_nonce || iface->dhcpv4_forcereconf) &&
397 !a->fr_ip &&
398 ((a->addr & mask) == (addr->addr.in.s_addr & mask))) {
399 if (!fr_ip) {
400 fr_ip = calloc(1, sizeof(*fr_ip));
401 if (!fr_ip)
402 break;
403
404 list_add(&fr_ip->head, &iface->dhcpv4_fr_ips);
405 fr_ip->addr = *addr;
406 }
407 inc_ref_cnt_ip(&a->fr_ip, fr_ip);
408 }
409 }
410
411 return fr_ip ? true : false;
412 }
413
414 static void valid_until_cb(struct uloop_timeout *event)
415 {
416 time_t now = odhcpd_time();
417 struct interface *iface;
418 list_for_each_entry(iface, &interfaces, head) {
419 if (iface->dhcpv4 != MODE_SERVER || iface->dhcpv4_assignments.next == NULL)
420 continue;
421
422 struct dhcpv4_assignment *a, *n;
423 list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
424 if (!INFINITE_VALID(a->valid_until) && a->valid_until < now)
425 free_dhcpv4_assignment(a);
426 }
427 }
428 uloop_timeout_set(event, 1000);
429 }
430
431 static void handle_addrlist_change(struct interface *iface)
432 {
433 struct odhcpd_ipaddr ip;
434 struct odhcpd_ref_ip *a;
435 struct dhcpv4_assignment *c;
436 uint32_t mask = iface->dhcpv4_mask.s_addr;
437
438 memset(&ip, 0, sizeof(ip));
439 ip.addr.in = iface->dhcpv4_local;
440 ip.prefix = odhcpd_netmask2bitlen(false, &iface->dhcpv4_mask);
441 ip.broadcast = iface->dhcpv4_bcast;
442
443 setup_dhcpv4_addresses(iface);
444
445 if ((ip.addr.in.s_addr & mask) ==
446 (iface->dhcpv4_local.s_addr & iface->dhcpv4_mask.s_addr))
447 return;
448
449 if (ip.addr.in.s_addr && !leases_require_fr(iface, &ip, mask))
450 return;
451
452 if (iface->dhcpv4_local.s_addr == INADDR_ANY || list_empty(&iface->dhcpv4_fr_ips))
453 return;
454
455 a = list_first_entry(&iface->dhcpv4_fr_ips, struct odhcpd_ref_ip, head);
456
457 if (netlink_setup_addr(&a->addr, iface->ifindex, false, true)) {
458 syslog(LOG_ERR, "Failed to add ip address");
459 return;
460 }
461
462 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
463 if ((c->flags & OAF_BOUND) && c->fr_ip && !c->fr_cnt) {
464 if (c->accept_fr_nonce || iface->dhcpv4_forcereconf)
465 dhcpv4_fr_rand_delay(c);
466 else
467 dhcpv4_fr_stop(c);
468 }
469 }
470 }
471
472 static char *dhcpv4_msg_to_string(uint8_t reqmsg)
473 {
474 switch (reqmsg) {
475 case (DHCPV4_MSG_DISCOVER):
476 return "DHCPV4_MSG_DISCOVER";
477 case (DHCPV4_MSG_OFFER):
478 return "DHCPV4_MSG_OFFER";
479 case (DHCPV4_MSG_REQUEST):
480 return "DHCPV4_MSG_REQUEST";
481 case (DHCPV4_MSG_DECLINE):
482 return "DHCPV4_MSG_DECLINE";
483 case (DHCPV4_MSG_ACK):
484 return "DHCPV4_MSG_ACK";
485 case (DHCPV4_MSG_NAK):
486 return "DHCPV4_MSG_NAK";
487 case (DHCPV4_MSG_RELEASE):
488 return "DHCPV4_MSG_RELEASE";
489 case (DHCPV4_MSG_INFORM):
490 return "DHCPV4_MSG_INFORM";
491 case (DHCPV4_MSG_FORCERENEW):
492 return "DHCPV4_MSG_FORCERENEW";
493 default:
494 return "UNKNOWN";
495 }
496 }
497
498 static void free_dhcpv4_assignment(struct dhcpv4_assignment *a)
499 {
500 if (a->head.next)
501 list_del(&a->head);
502
503 if (a->fr_ip)
504 dhcpv4_fr_stop(a);
505
506 free(a->hostname);
507 free(a);
508 }
509
510 static void dhcpv4_put(struct dhcpv4_message *msg, uint8_t **cookie,
511 uint8_t type, uint8_t len, const void *data)
512 {
513 uint8_t *c = *cookie;
514 uint8_t *end = (uint8_t *)msg + sizeof(*msg);
515
516 if (*cookie + 2 + len > end)
517 return;
518
519 *c++ = type;
520 *c++ = len;
521 memcpy(c, data, len);
522
523 *cookie = c + len;
524 }
525
526 static void dhcpv4_fr_send(struct dhcpv4_assignment *a)
527 {
528 struct dhcpv4_message fr_msg = {
529 .op = DHCPV4_BOOTREPLY,
530 .htype = 1,
531 .hlen = 6,
532 .hops = 0,
533 .secs = 0,
534 .flags = 0,
535 .ciaddr = {INADDR_ANY},
536 .yiaddr = {INADDR_ANY},
537 .siaddr = {INADDR_ANY},
538 .giaddr = {INADDR_ANY},
539 .chaddr = {0},
540 .sname = {0},
541 .file = {0},
542 };
543 struct dhcpv4_auth_forcerenew *auth_o, auth = {
544 .protocol = 3,
545 .algorithm = 1,
546 .rdm = 0,
547 .replay = {htonl(time(NULL)), htonl(++serial)},
548 .type = 2,
549 .key = {0},
550 };
551 struct interface *iface = a->iface;
552
553 odhcpd_urandom(&fr_msg.xid, sizeof(fr_msg.xid));
554 memcpy(fr_msg.chaddr, a->hwaddr, fr_msg.hlen);
555
556 fr_msg.options[0] = 0x63;
557 fr_msg.options[1] = 0x82;
558 fr_msg.options[2] = 0x53;
559 fr_msg.options[3] = 0x63;
560
561 uint8_t *cookie = &fr_msg.options[4];
562 uint8_t msg = DHCPV4_MSG_FORCERENEW;
563
564 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg);
565 if (a->accept_fr_nonce) {
566 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_AUTHENTICATION, sizeof(auth), &auth);
567 auth_o = (struct dhcpv4_auth_forcerenew *)(cookie - sizeof(auth));
568 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_END, 0, NULL);
569
570 md5_ctx_t md5;
571 uint8_t secretbytes[64];
572 memset(secretbytes, 0, sizeof(secretbytes));
573 memcpy(secretbytes, a->key, sizeof(a->key));
574
575 for (size_t i = 0; i < sizeof(secretbytes); ++i)
576 secretbytes[i] ^= 0x36;
577
578 md5_begin(&md5);
579 md5_hash(secretbytes, sizeof(secretbytes), &md5);
580 md5_hash(&fr_msg, sizeof(fr_msg), &md5);
581 md5_end(auth_o->key, &md5);
582
583 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
584 secretbytes[i] ^= 0x36;
585 secretbytes[i] ^= 0x5c;
586 }
587
588 md5_begin(&md5);
589 md5_hash(secretbytes, sizeof(secretbytes), &md5);
590 md5_hash(auth_o->key, sizeof(auth_o->key), &md5);
591 md5_end(auth_o->key, &md5);
592 } else {
593 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_SERVERID, 4,
594 &a->fr_ip->addr.addr.in.s_addr);
595 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_END, 0, NULL);
596 }
597
598 struct sockaddr_in dest;
599 memset(&dest, 0, sizeof(dest));
600 dest.sin_family = AF_INET;
601 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
602 dest.sin_addr.s_addr = a->addr;
603
604 if (sendto(iface->dhcpv4_event.uloop.fd, &fr_msg, sizeof(fr_msg),
605 MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest)) < 0)
606 syslog(LOG_ERR, "Failed to send %s to %s - %s: %m", dhcpv4_msg_to_string(msg),
607 odhcpd_print_mac(a->hwaddr, sizeof(a->hwaddr)), inet_ntoa(dest.sin_addr));
608 else
609 syslog(LOG_WARNING, "Sent %s to %s - %s", dhcpv4_msg_to_string(msg),
610 odhcpd_print_mac(a->hwaddr, sizeof(a->hwaddr)), inet_ntoa(dest.sin_addr));
611 }
612
613 static void dhcpv4_fr_timer(struct uloop_timeout *event)
614 {
615 struct dhcpv4_assignment *a = container_of(event, struct dhcpv4_assignment, fr_timer);
616
617 if (a->fr_cnt > 0 && a->fr_cnt < 8) {
618 dhcpv4_fr_send(a);
619 uloop_timeout_set(&a->fr_timer, 1000 << a->fr_cnt);
620 a->fr_cnt++;
621 } else
622 dhcpv4_fr_stop(a);
623 }
624
625 static void dhcpv4_fr_start(struct dhcpv4_assignment *a)
626 {
627 uloop_timeout_set(&a->fr_timer, 1000 << a->fr_cnt);
628 a->fr_timer.cb = dhcpv4_fr_timer;
629 a->fr_cnt++;
630
631 dhcpv4_fr_send(a);
632 }
633
634 static void dhcpv4_fr_delay_timer(struct uloop_timeout *event)
635 {
636 struct dhcpv4_assignment *a = container_of(event, struct dhcpv4_assignment, fr_timer);
637 struct interface *iface = a->iface;
638
639 (iface->dhcpv4_event.uloop.fd == -1 ? dhcpv4_fr_rand_delay(a) : dhcpv4_fr_start(a));
640 }
641
642 static void dhcpv4_fr_rand_delay(struct dhcpv4_assignment *a)
643 {
644 #define MIN_DELAY 500
645 #define MAX_FUZZ 500
646 int msecs;
647
648 odhcpd_urandom(&msecs, sizeof(msecs));
649
650 msecs = labs(msecs)%MAX_FUZZ + MIN_DELAY;
651
652 uloop_timeout_set(&a->fr_timer, msecs);
653 a->fr_timer.cb = dhcpv4_fr_delay_timer;
654 }
655
656 static void dhcpv4_fr_stop(struct dhcpv4_assignment *a)
657 {
658 uloop_timeout_cancel(&a->fr_timer);
659 decr_ref_cnt_ip(&a->fr_ip, a->iface);
660 a->fr_cnt = 0;
661 a->fr_timer.cb = NULL;
662 }
663
664 /* Handler for DHCPv4 messages */
665 static void handle_dhcpv4(void *addr, void *data, size_t len,
666 struct interface *iface, _unused void *dest_addr)
667 {
668 if (!iface->dhcpv4)
669 return;
670
671 struct dhcpv4_message *req = data;
672 if (len < offsetof(struct dhcpv4_message, options) + 4 ||
673 req->op != DHCPV4_BOOTREQUEST || req->hlen != 6)
674 return;
675
676
677 syslog(LOG_NOTICE, "Got DHCPv4 request");
678
679 if (!iface->dhcpv4_start_ip.s_addr && !iface->dhcpv4_end_ip.s_addr) {
680 syslog(LOG_WARNING, "No DHCP range available on %s", iface->name);
681 return;
682 }
683
684 int sock = iface->dhcpv4_event.uloop.fd;
685
686 struct dhcpv4_message reply = {
687 .op = DHCPV4_BOOTREPLY,
688 .htype = req->htype,
689 .hlen = req->hlen,
690 .hops = 0,
691 .xid = req->xid,
692 .secs = 0,
693 .flags = req->flags,
694 .ciaddr = {INADDR_ANY},
695 .giaddr = req->giaddr,
696 .siaddr = iface->dhcpv4_local,
697 };
698 memcpy(reply.chaddr, req->chaddr, sizeof(reply.chaddr));
699
700 reply.options[0] = 0x63;
701 reply.options[1] = 0x82;
702 reply.options[2] = 0x53;
703 reply.options[3] = 0x63;
704
705 uint8_t *cookie = &reply.options[4];
706 uint8_t reqmsg = DHCPV4_MSG_REQUEST;
707 uint8_t msg = DHCPV4_MSG_ACK;
708
709 uint32_t reqaddr = INADDR_ANY;
710 uint32_t leasetime = 0;
711 size_t hostname_len = 0;
712 char hostname[256];
713 bool accept_fr_nonce = false;
714 bool incl_fr_opt = false;
715
716 uint8_t *start = &req->options[4];
717 uint8_t *end = ((uint8_t*)data) + len;
718 struct dhcpv4_option *opt;
719 dhcpv4_for_each_option(start, end, opt) {
720 if (opt->type == DHCPV4_OPT_MESSAGE && opt->len == 1)
721 reqmsg = opt->data[0];
722 else if (opt->type == DHCPV4_OPT_HOSTNAME && opt->len > 0) {
723 hostname_len = opt->len;
724 memcpy(hostname, opt->data, hostname_len);
725 hostname[hostname_len] = 0;
726 } else if (opt->type == DHCPV4_OPT_IPADDRESS && opt->len == 4)
727 memcpy(&reqaddr, opt->data, 4);
728 else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) {
729 if (memcmp(opt->data, &iface->dhcpv4_local, 4))
730 return;
731 } else if (iface->filter_class && opt->type == DHCPV4_OPT_USER_CLASS) {
732 uint8_t *c = opt->data, *cend = &opt->data[opt->len];
733 for (; c < cend && &c[*c] < cend; c = &c[1 + *c]) {
734 size_t elen = strlen(iface->filter_class);
735 if (*c == elen && !memcmp(&c[1], iface->filter_class, elen))
736 return; // Ignore from homenet
737 }
738 } else if (opt->type == DHCPV4_OPT_LEASETIME && opt->len == 4)
739 memcpy(&leasetime, opt->data, 4);
740 else if (opt->type == DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE && opt->len > 0) {
741 for (uint8_t i = 0; i < opt->len; i++) {
742 if (opt->data[i] == 1) {
743 accept_fr_nonce = true;
744 break;
745 }
746 }
747
748 }
749 }
750
751 if (reqmsg != DHCPV4_MSG_DISCOVER && reqmsg != DHCPV4_MSG_REQUEST &&
752 reqmsg != DHCPV4_MSG_INFORM && reqmsg != DHCPV4_MSG_DECLINE &&
753 reqmsg != DHCPV4_MSG_RELEASE)
754 return;
755
756 struct dhcpv4_assignment *lease = NULL;
757 uint32_t serverid = iface->dhcpv4_local.s_addr;
758 uint32_t fr_serverid = INADDR_ANY;
759
760 if (reqmsg != DHCPV4_MSG_INFORM)
761 lease = dhcpv4_lease(iface, reqmsg, req->chaddr, reqaddr,
762 &leasetime, hostname, hostname_len,
763 accept_fr_nonce, &incl_fr_opt, &fr_serverid);
764
765 if (!lease) {
766 if (reqmsg == DHCPV4_MSG_REQUEST)
767 msg = DHCPV4_MSG_NAK;
768 else if (reqmsg == DHCPV4_MSG_DISCOVER)
769 return;
770 } else if (reqmsg == DHCPV4_MSG_DISCOVER)
771 msg = DHCPV4_MSG_OFFER;
772 else if (reqmsg == DHCPV4_MSG_REQUEST &&
773 ((reqaddr && reqaddr != lease->addr) ||
774 (req->ciaddr.s_addr && req->ciaddr.s_addr != lease->addr))) {
775 msg = DHCPV4_MSG_NAK;
776 /*
777 * DHCP client requested an IP which we can't offer to him. Probably the
778 * client changed the network or the network has been changed. The reply
779 * type is set to DHCPV4_MSG_NAK, because the client should not use that IP.
780 *
781 * For modern devices we build an answer that includes a valid IP, like
782 * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
783 * perform additional DHCP round trips.
784 *
785 */
786
787 /*
788 *
789 * Buggy clients do serverid checking in nack messages; therefore set the
790 * serverid in nack messages triggered by a previous force renew equal to
791 * the server id in use at that time by the server
792 *
793 */
794 if (fr_serverid)
795 serverid = fr_serverid;
796
797 if (req->ciaddr.s_addr &&
798 ((iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr) !=
799 (req->ciaddr.s_addr & iface->dhcpv4_mask.s_addr)))
800 req->ciaddr.s_addr = INADDR_ANY;
801 }
802
803 syslog(LOG_WARNING, "received %s from %s", dhcpv4_msg_to_string(reqmsg),
804 odhcpd_print_mac(req->chaddr, req->hlen));
805
806 #ifdef WITH_UBUS
807 if (reqmsg == DHCPV4_MSG_RELEASE)
808 ubus_bcast_dhcp_event("dhcp.release", req->chaddr, req->hlen,
809 &req->ciaddr, hostname, iface->ifname);
810 #endif
811 if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
812 return;
813
814 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg);
815 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SERVERID, 4, &serverid);
816
817 if (lease) {
818 uint32_t val;
819
820 reply.yiaddr.s_addr = lease->addr;
821
822 val = htonl(leasetime);
823 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_LEASETIME, 4, &val);
824
825 if (leasetime != UINT32_MAX) {
826 val = htonl(500 * leasetime / 1000);
827 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_RENEW, 4, &val);
828
829 val = htonl(875 * leasetime / 1000);
830 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_REBIND, 4, &val);
831 }
832
833 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NETMASK, 4,
834 &iface->dhcpv4_mask.s_addr);
835
836 if (lease->hostname)
837 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_HOSTNAME,
838 strlen(lease->hostname), lease->hostname);
839
840 if (iface->dhcpv4_bcast.s_addr != INADDR_ANY)
841 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_BROADCAST, 4, &iface->dhcpv4_bcast);
842
843 if (incl_fr_opt) {
844 if (reqmsg == DHCPV4_MSG_REQUEST) {
845 struct dhcpv4_auth_forcerenew auth = {
846 .protocol = 3,
847 .algorithm = 1,
848 .rdm = 0,
849 .replay = {htonl(time(NULL)), htonl(++serial)},
850 .type = 1,
851 .key = {0},
852 };
853
854 memcpy(auth.key, lease->key, sizeof(auth.key));
855 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_AUTHENTICATION, sizeof(auth), &auth);
856 } else {
857 uint8_t one = 1;
858 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE,
859 sizeof(one), &one);
860 }
861 }
862 }
863
864 struct ifreq ifr;
865
866 memset(&ifr, 0, sizeof(ifr));
867 strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name) - 1);
868
869 if (!ioctl(sock, SIOCGIFMTU, &ifr)) {
870 uint16_t mtu = htons(ifr.ifr_mtu);
871 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MTU, 2, &mtu);
872 }
873
874 if (iface->search && iface->search_len <= 255)
875 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
876 iface->search_len, iface->search);
877 else if (!res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
878 uint8_t search_buf[256];
879 int len = dn_comp(_res.dnsrch[0], search_buf,
880 sizeof(search_buf), NULL, NULL);
881 if (len > 0)
882 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
883 len, search_buf);
884 }
885
886 if (iface->dhcpv4_router_cnt == 0)
887 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER, 4, &iface->dhcpv4_local);
888 else
889 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER,
890 4 * iface->dhcpv4_router_cnt, iface->dhcpv4_router);
891
892
893 if (iface->dhcpv4_dns_cnt == 0)
894 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &iface->dhcpv4_local);
895 else
896 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER,
897 4 * iface->dhcpv4_dns_cnt, iface->dhcpv4_dns);
898
899
900 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_END, 0, NULL);
901
902 struct sockaddr_in dest = *((struct sockaddr_in*)addr);
903 if (req->giaddr.s_addr) {
904 /*
905 * relay agent is configured, send reply to the agent
906 */
907 dest.sin_addr = req->giaddr;
908 dest.sin_port = htons(DHCPV4_SERVER_PORT);
909 } else if (req->ciaddr.s_addr && req->ciaddr.s_addr != dest.sin_addr.s_addr) {
910 /*
911 * client has existing configuration (ciaddr is set) AND this address is
912 * not the address it used for the dhcp message
913 */
914 dest.sin_addr = req->ciaddr;
915 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
916 } else if ((ntohs(req->flags) & DHCPV4_FLAG_BROADCAST) ||
917 req->hlen != reply.hlen || !reply.yiaddr.s_addr) {
918 /*
919 * client requests a broadcast reply OR we can't offer an IP
920 */
921 dest.sin_addr.s_addr = INADDR_BROADCAST;
922 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
923 } else if (!req->ciaddr.s_addr && msg == DHCPV4_MSG_NAK) {
924 /*
925 * client has no previous configuration -> no IP, so we need to reply
926 * with a broadcast packet
927 */
928 dest.sin_addr.s_addr = INADDR_BROADCAST;
929 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
930 } else {
931 struct arpreq arp = {.arp_flags = ATF_COM};
932
933 /*
934 * send reply to the newly (in this proccess) allocated IP
935 */
936 dest.sin_addr = reply.yiaddr;
937 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
938
939 memcpy(arp.arp_ha.sa_data, req->chaddr, 6);
940 memcpy(&arp.arp_pa, &dest, sizeof(arp.arp_pa));
941 memcpy(arp.arp_dev, iface->ifname, sizeof(arp.arp_dev));
942
943 if (ioctl(sock, SIOCSARP, &arp) < 0)
944 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
945 }
946
947 if (sendto(sock, &reply, sizeof(reply), MSG_DONTWAIT,
948 (struct sockaddr*)&dest, sizeof(dest)) < 0)
949 syslog(LOG_ERR, "Failed to send %s to %s - %s: %m",
950 dhcpv4_msg_to_string(msg),
951 dest.sin_addr.s_addr == INADDR_BROADCAST ?
952 "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req->chaddr, req->hlen),
953 inet_ntoa(dest.sin_addr));
954 else
955 syslog(LOG_ERR, "Sent %s to %s - %s",
956 dhcpv4_msg_to_string(msg),
957 dest.sin_addr.s_addr == INADDR_BROADCAST ?
958 "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req->chaddr, req->hlen),
959 inet_ntoa(dest.sin_addr));
960
961
962 #ifdef WITH_UBUS
963 if (msg == DHCPV4_MSG_ACK)
964 ubus_bcast_dhcp_event("dhcp.ack", req->chaddr, req->hlen, &reply.yiaddr,
965 hostname, iface->ifname);
966 #endif
967 }
968
969 static bool dhcpv4_assign(struct interface *iface,
970 struct dhcpv4_assignment *assign, uint32_t raddr)
971 {
972 uint32_t start = ntohl(iface->dhcpv4_start_ip.s_addr);
973 uint32_t end = ntohl(iface->dhcpv4_end_ip.s_addr);
974 uint32_t count = end - start + 1;
975
976 /* try to assign the IP the client asked for */
977 if (start <= ntohl(raddr) && ntohl(raddr) <= end &&
978 !find_assignment_by_addr(iface, raddr)) {
979 assign->addr = raddr;
980 syslog(LOG_INFO, "assigning the IP the client asked for: %u.%u.%u.%u",
981 ((uint8_t *)&assign->addr)[0],
982 ((uint8_t *)&assign->addr)[1],
983 ((uint8_t *)&assign->addr)[2],
984 ((uint8_t *)&assign->addr)[3]);
985 return true;
986 }
987
988 /* Seed RNG with checksum of hwaddress */
989 uint32_t seed = 0;
990 for (size_t i = 0; i < sizeof(assign->hwaddr); ++i) {
991 /* Knuth's multiplicative method */
992 uint8_t o = assign->hwaddr[i];
993 seed += (o*2654435761) % UINT32_MAX;
994 }
995
996 srand(seed);
997
998 uint32_t try = (((uint32_t)rand()) % count) + start;
999
1000 if (list_empty(&iface->dhcpv4_assignments)) {
1001 assign->addr = htonl(try);
1002 syslog(LOG_INFO, "assigning mapped IP (empty list): %u.%u.%u.%u",
1003 ((uint8_t *)&assign->addr)[0],
1004 ((uint8_t *)&assign->addr)[1],
1005 ((uint8_t *)&assign->addr)[2],
1006 ((uint8_t *)&assign->addr)[3]);
1007 return true;
1008 }
1009
1010 for (uint32_t i = 0; i < count; ++i) {
1011 if (!find_assignment_by_addr(iface, htonl(try))) {
1012 /* test was successful: IP address is not assigned, assign it */
1013 assign->addr = htonl(try);
1014 syslog(LOG_DEBUG, "assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
1015 ((uint8_t *)&assign->addr)[0],
1016 ((uint8_t *)&assign->addr)[1],
1017 ((uint8_t *)&assign->addr)[2],
1018 ((uint8_t *)&assign->addr)[3],
1019 i, count);
1020 return true;
1021 }
1022 try = (((try - start) + 1) % count) + start;
1023 }
1024
1025 syslog(LOG_WARNING, "can't assign any IP address -> address space is full");
1026 return false;
1027 }
1028
1029
1030 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
1031 enum dhcpv4_msg msg, const uint8_t *mac, const uint32_t reqaddr,
1032 uint32_t *leasetime, const char *hostname, const size_t hostname_len,
1033 const bool accept_fr_nonce, bool *incl_fr_opt, uint32_t *fr_serverid)
1034 {
1035 struct dhcpv4_assignment *a = find_assignment_by_hwaddr(iface, mac);
1036 struct dhcpv4_assignment *lease = NULL;
1037 time_t now = odhcpd_time();
1038
1039 if (a && (a->flags & OAF_BOUND) && a->fr_ip) {
1040 *fr_serverid = a->fr_ip->addr.addr.in.s_addr;
1041 dhcpv4_fr_stop(a);
1042 }
1043
1044 if (msg == DHCPV4_MSG_DISCOVER || msg == DHCPV4_MSG_REQUEST) {
1045 bool assigned = !!a;
1046
1047 if (!a) {
1048 if (!iface->no_dynamic_dhcp) {
1049 /* Create new binding */
1050 a = calloc(1, sizeof(*a));
1051 if (!a) {
1052 syslog(LOG_ERR, "Failed to calloc binding on interface %s",
1053 iface->ifname);
1054 return NULL;
1055 }
1056 memcpy(a->hwaddr, mac, sizeof(a->hwaddr));
1057 /* Don't consider new assignment as infinite */
1058 a->valid_until = now;
1059
1060 assigned = dhcpv4_assign(iface, a, reqaddr);
1061 if (assigned) {
1062 a->iface = iface;
1063 list_add(&a->head, &iface->dhcpv4_assignments);
1064 }
1065 }
1066 } else if ((a->addr & iface->dhcpv4_mask.s_addr) !=
1067 (iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr)) {
1068 list_del(&a->head);
1069
1070 assigned = dhcpv4_assign(iface, a, reqaddr);
1071 if (assigned)
1072 list_add(&a->head, &iface->dhcpv4_assignments);
1073 }
1074
1075 if (assigned) {
1076 uint32_t my_leasetime;
1077
1078 if (a->leasetime)
1079 my_leasetime = a->leasetime;
1080 else
1081 my_leasetime = iface->dhcpv4_leasetime;
1082
1083 if ((*leasetime == 0) || (my_leasetime < *leasetime))
1084 *leasetime = my_leasetime;
1085
1086 if (msg == DHCPV4_MSG_DISCOVER) {
1087 a->flags &= ~OAF_BOUND;
1088
1089 *incl_fr_opt = accept_fr_nonce;
1090 if (!(a->flags & OAF_STATIC))
1091 a->valid_until = now;
1092 } else {
1093 if (hostname_len > 0) {
1094 a->hostname = realloc(a->hostname, hostname_len + 1);
1095 if (a->hostname) {
1096 memcpy(a->hostname, hostname, hostname_len);
1097 a->hostname[hostname_len] = 0;
1098 }
1099 }
1100
1101 if (!(a->flags & OAF_BOUND)) {
1102 a->accept_fr_nonce = accept_fr_nonce;
1103 *incl_fr_opt = accept_fr_nonce;
1104 odhcpd_urandom(a->key, sizeof(a->key));
1105 a->flags |= OAF_BOUND;
1106 } else
1107 *incl_fr_opt = false;
1108
1109 if (!(a->flags & OAF_STATIC))
1110 a->valid_until = ((*leasetime == UINT32_MAX) ? 0 : (time_t)(now + *leasetime));
1111 }
1112 } else if (!assigned && a) {
1113 /* Cleanup failed assignment */
1114 free_dhcpv4_assignment(a);
1115 a = NULL;
1116 }
1117
1118 if (assigned && a)
1119 lease = a;
1120 } else if (msg == DHCPV4_MSG_RELEASE && a) {
1121 a->flags &= ~OAF_BOUND;
1122
1123 if (!(a->flags & OAF_STATIC))
1124 a->valid_until = now - 1;
1125
1126 } else if (msg == DHCPV4_MSG_DECLINE && a) {
1127 a->flags &= ~OAF_BOUND;
1128
1129 if (!(a->flags & OAF_STATIC)) {
1130 memset(a->hwaddr, 0, sizeof(a->hwaddr));
1131 a->valid_until = now + 3600; /* Block address for 1h */
1132 }
1133 }
1134
1135 dhcpv6_write_statefile();
1136
1137 return lease;
1138 }