2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2016 Hans Dedecker <dedeckeh@gmail.com>
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.
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.
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>
32 #include <libubox/md5.h>
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 void valid_until_cb(struct uloop_timeout *event);
42 static void handle_addrlist_change(struct interface *iface);
43 static void free_dhcpv4_assignment(struct dhcpv4_assignment *a);
44 static void dhcpv4_fr_start(struct dhcpv4_assignment *a);
45 static void dhcpv4_fr_stop(struct dhcpv4_assignment *a);
46 static void handle_dhcpv4(void *addr, void *data, size_t len,
47 struct interface *iface, void *dest_addr);
48 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
49 enum dhcpv4_msg msg, const uint8_t *mac, const uint32_t reqaddr,
50 uint32_t *leasetime, const char *hostname, const size_t hostname_len,
51 const bool accept_fr_nonce, bool *incl_fr_opt, uint32_t *fr_serverid);
53 static struct netevent_handler dhcpv4_netevent_handler = { .cb = dhcpv4_netevent_cb, };
54 static struct uloop_timeout valid_until_timeout = {.cb = valid_until_cb};
55 static uint32_t serial = 0;
57 struct odhcpd_ref_ip {
58 struct list_head head;
60 struct odhcpd_ipaddr addr;
63 /* Create socket and register events */
66 uloop_timeout_set(&valid_until_timeout, 1000);
67 netlink_add_netevent_handler(&dhcpv4_netevent_handler);
72 int dhcpv4_setup_interface(struct interface *iface, bool enable)
74 if (iface->dhcpv4_event.uloop.fd > 0) {
75 uloop_fd_delete(&iface->dhcpv4_event.uloop);
76 close(iface->dhcpv4_event.uloop.fd);
77 iface->dhcpv4_event.uloop.fd = -1;
80 if (iface->dhcpv4 && enable) {
81 if (!iface->dhcpv4_assignments.next)
82 INIT_LIST_HEAD(&iface->dhcpv4_assignments);
84 if (!iface->dhcpv4_fr_ips.next)
85 INIT_LIST_HEAD(&iface->dhcpv4_fr_ips);
87 int sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
89 syslog(LOG_ERR, "Failed to create DHCPv4 server socket: %m");
93 /* Basic IPv4 configuration */
95 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
96 setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));
97 setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val));
99 val = IPTOS_PREC_INTERNETCONTROL;
100 setsockopt(sock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
102 val = IP_PMTUDISC_DONT;
103 setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val));
105 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE,
106 iface->ifname, strlen(iface->ifname));
108 struct sockaddr_in bind_addr = {AF_INET, htons(DHCPV4_SERVER_PORT),
111 if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr))) {
112 syslog(LOG_ERR, "Failed to open DHCPv4 server socket: %m");
116 iface->dhcpv4_event.uloop.fd = sock;
117 iface->dhcpv4_event.handle_dgram = handle_dhcpv4;
118 odhcpd_register(&iface->dhcpv4_event);
120 if (setup_dhcpv4_addresses(iface) < 0)
123 update_static_assignments(iface);
124 } else if (iface->dhcpv4_assignments.next) {
125 while (!list_empty(&iface->dhcpv4_assignments))
126 free_dhcpv4_assignment(list_first_entry(&iface->dhcpv4_assignments,
127 struct dhcpv4_assignment, head));
133 static void dhcpv4_netevent_cb(unsigned long event, struct netevent_handler_info *info)
135 struct interface *iface = info->iface;
137 if (!iface || iface->dhcpv4 == MODE_DISABLED)
141 case NETEV_IFINDEX_CHANGE:
142 dhcpv4_setup_interface(iface, true);
144 case NETEV_ADDRLIST_CHANGE:
145 handle_addrlist_change(iface);
152 static struct dhcpv4_assignment *find_assignment_by_hwaddr(struct interface *iface, const uint8_t *hwaddr)
154 struct dhcpv4_assignment *a;
156 list_for_each_entry(a, &iface->dhcpv4_assignments, head)
157 if (!memcmp(a->hwaddr, hwaddr, 6))
163 static struct dhcpv4_assignment *find_assignment_by_addr(struct interface *iface, const uint32_t addr)
165 struct dhcpv4_assignment *a;
167 list_for_each_entry(a, &iface->dhcpv4_assignments, head)
174 static int setup_dhcpv4_addresses(struct interface *iface)
176 iface->dhcpv4_start_ip.s_addr = INADDR_ANY;
177 iface->dhcpv4_end_ip.s_addr = INADDR_ANY;
178 iface->dhcpv4_local.s_addr = INADDR_ANY;
179 iface->dhcpv4_bcast.s_addr = INADDR_ANY;
180 iface->dhcpv4_mask.s_addr = INADDR_ANY;
183 if (iface->dhcpv4_start.s_addr & htonl(0xffff0000) ||
184 iface->dhcpv4_end.s_addr & htonl(0xffff0000) ||
185 ntohl(iface->dhcpv4_start.s_addr) > ntohl(iface->dhcpv4_end.s_addr)) {
186 syslog(LOG_ERR, "invalid DHCP range for %s", iface->name);
190 if (!iface->addr4_len) {
191 syslog(LOG_WARNING, "no network(s) available on %s", iface->name);
195 uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
196 uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
198 for (size_t i = 0; i < iface->addr4_len && start && end; i++) {
199 struct in_addr *addr = &iface->addr4[i].addr.in;
202 odhcpd_bitlen2netmask(false, iface->addr4[i].prefix, &mask);
203 if ((start & ntohl(~mask.s_addr)) == start &&
204 (end & ntohl(~mask.s_addr)) == end) {
205 iface->dhcpv4_start_ip.s_addr = htonl(start) |
206 (addr->s_addr & mask.s_addr);
207 iface->dhcpv4_end_ip.s_addr = htonl(end) |
208 (addr->s_addr & mask.s_addr);
209 iface->dhcpv4_local = *addr;
210 iface->dhcpv4_bcast = iface->addr4[i].broadcast;
211 iface->dhcpv4_mask = mask;
216 /* Don't allocate IP range for subnets bigger than 28 */
217 if (iface->addr4[0].prefix > 28) {
218 syslog(LOG_WARNING, "auto allocation of DHCP range fails on %s", iface->name);
222 iface->dhcpv4_local = iface->addr4[0].addr.in;
223 iface->dhcpv4_bcast = iface->addr4[0].broadcast;
224 odhcpd_bitlen2netmask(false, iface->addr4[0].prefix, &iface->dhcpv4_mask);
225 end = start = iface->dhcpv4_local.s_addr & iface->dhcpv4_mask.s_addr;
227 /* Auto allocate ranges */
228 if (ntohl(iface->dhcpv4_mask.s_addr) <= 0xffffff00) {
229 iface->dhcpv4_start_ip.s_addr = start | htonl(100);
230 iface->dhcpv4_end_ip.s_addr = end | htonl(250);
231 } else if (ntohl(iface->dhcpv4_mask.s_addr) <= 0xffffffc0) {
232 iface->dhcpv4_start_ip.s_addr = start | htonl(10);
233 iface->dhcpv4_end_ip.s_addr = end | htonl(60);
234 } else if (ntohl(iface->dhcpv4_mask.s_addr) <= 0xffffffe0) {
235 iface->dhcpv4_start_ip.s_addr = start | htonl(10);
236 iface->dhcpv4_end_ip.s_addr = end | htonl(30);
238 iface->dhcpv4_start_ip.s_addr = start | htonl(3);
239 iface->dhcpv4_end_ip.s_addr = end | htonl(12);
245 static void update_static_assignments(struct interface *iface)
247 struct dhcpv4_assignment *a, *c;
249 /* Cleanup static entries not belonging to the network */
250 list_for_each_entry_safe(a, c, &iface->dhcpv4_assignments, head) {
251 if ((a->flags & OAF_STATIC) &&
252 ((a->addr & iface->dhcpv4_mask.s_addr) !=
253 (iface->dhcpv4_start.s_addr & iface->dhcpv4_mask.s_addr)))
254 free_dhcpv4_assignment(a);
257 /* Parse static entries */
259 list_for_each_entry(lease, &leases, head) {
260 if ((iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr) !=
261 (lease->ipaddr.s_addr & iface->dhcpv4_mask.s_addr)) {
265 a = find_assignment_by_hwaddr(iface, lease->mac.ether_addr_octet);
268 /* Check if there's an assignment with the specified IP address */
269 if (find_assignment_by_addr(iface, lease->ipaddr.s_addr))
272 /* Construct entry */
273 a = calloc(1, sizeof(*a));
275 syslog(LOG_ERR, "Calloc failed for static lease on interface %s",
279 memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
282 a->leasetime = lease->dhcpv4_leasetime;
284 a->addr = lease->ipaddr.s_addr;
285 /* Static assignment */
286 a->flags |= OAF_STATIC;
290 if (lease->hostname[0]) {
292 a->hostname = strdup(lease->hostname);
295 /* Assign to all interfaces */
296 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
297 if (ntohl(c->addr) > ntohl(a->addr)) {
298 list_add_tail(&a->head, &c->head);
303 if (&c->head == &iface->dhcpv4_assignments)
304 list_add(&a->head, &iface->dhcpv4_assignments);
308 static void inc_ref_cnt_ip(struct odhcpd_ref_ip **ptr, struct odhcpd_ref_ip *ip)
314 static void decr_ref_cnt_ip(struct odhcpd_ref_ip **ptr, struct interface *iface)
316 struct odhcpd_ref_ip *ip = *ptr;
318 if (--ip->ref_cnt == 0) {
319 netlink_setup_addr(&ip->addr, iface->ifindex, false, false);
328 static bool leases_require_fr(struct interface *iface, struct odhcpd_ipaddr *addr,
331 struct dhcpv4_assignment *a = NULL;
332 struct odhcpd_ref_ip *fr_ip = NULL;
334 list_for_each_entry(a, &iface->dhcpv4_assignments, head) {
335 if ((a->accept_fr_nonce || iface->dhcpv4_forcereconf) &&
337 ((a->addr & mask) == (addr->addr.in.s_addr & mask))) {
339 fr_ip = calloc(1, sizeof(*fr_ip));
343 list_add(&fr_ip->head, &iface->dhcpv4_fr_ips);
346 inc_ref_cnt_ip(&a->fr_ip, fr_ip);
350 return fr_ip ? true : false;
353 static void valid_until_cb(struct uloop_timeout *event)
355 time_t now = odhcpd_time();
356 struct interface *iface;
357 list_for_each_entry(iface, &interfaces, head) {
358 if (iface->dhcpv4 != MODE_SERVER || iface->dhcpv4_assignments.next == NULL)
361 struct dhcpv4_assignment *a, *n;
362 list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
363 if (!INFINITE_VALID(a->valid_until) && a->valid_until < now)
364 free_dhcpv4_assignment(a);
367 uloop_timeout_set(event, 1000);
370 static void handle_addrlist_change(struct interface *iface)
372 struct odhcpd_ipaddr ip;
373 struct odhcpd_ref_ip *a;
374 struct dhcpv4_assignment *c;
375 uint32_t mask = iface->dhcpv4_mask.s_addr;
377 memset(&ip, 0, sizeof(ip));
378 ip.addr.in = iface->dhcpv4_local;
379 ip.prefix = odhcpd_netmask2bitlen(false, &iface->dhcpv4_mask);
380 ip.broadcast = iface->dhcpv4_bcast;
382 setup_dhcpv4_addresses(iface);
384 if ((ip.addr.in.s_addr & mask) ==
385 (iface->dhcpv4_local.s_addr & iface->dhcpv4_mask.s_addr))
388 if (ip.addr.in.s_addr && !leases_require_fr(iface, &ip, mask))
391 if (iface->dhcpv4_local.s_addr == INADDR_ANY || list_empty(&iface->dhcpv4_fr_ips))
394 a = list_first_entry(&iface->dhcpv4_fr_ips, struct odhcpd_ref_ip, head);
396 if (netlink_setup_addr(&a->addr, iface->ifindex, false, true)) {
397 syslog(LOG_ERR, "Failed to add ip address");
401 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
402 if ((c->flags & OAF_BOUND) && c->fr_ip && !c->fr_cnt) {
403 if (c->accept_fr_nonce || iface->dhcpv4_forcereconf)
411 static char *dhcpv4_msg_to_string(uint8_t reqmsg)
414 case (DHCPV4_MSG_DISCOVER):
415 return "DHCPV4_MSG_DISCOVER";
416 case (DHCPV4_MSG_OFFER):
417 return "DHCPV4_MSG_OFFER";
418 case (DHCPV4_MSG_REQUEST):
419 return "DHCPV4_MSG_REQUEST";
420 case (DHCPV4_MSG_DECLINE):
421 return "DHCPV4_MSG_DECLINE";
422 case (DHCPV4_MSG_ACK):
423 return "DHCPV4_MSG_ACK";
424 case (DHCPV4_MSG_NAK):
425 return "DHCPV4_MSG_NAK";
426 case (DHCPV4_MSG_RELEASE):
427 return "DHCPV4_MSG_RELEASE";
428 case (DHCPV4_MSG_INFORM):
429 return "DHCPV4_MSG_INFORM";
430 case (DHCPV4_MSG_FORCERENEW):
431 return "DHCPV4_MSG_FORCERENEW";
437 static void free_dhcpv4_assignment(struct dhcpv4_assignment *a)
449 static void dhcpv4_put(struct dhcpv4_message *msg, uint8_t **cookie,
450 uint8_t type, uint8_t len, const void *data)
452 uint8_t *c = *cookie;
453 if (*cookie + 2 + len > (uint8_t*)&msg[1])
458 memcpy(c, data, len);
463 static void dhcpv4_fr_send(struct dhcpv4_assignment *a)
465 struct dhcpv4_message fr_msg = {
466 .op = DHCPV4_BOOTREPLY,
472 .ciaddr = {INADDR_ANY},
473 .yiaddr = {INADDR_ANY},
474 .siaddr = {INADDR_ANY},
475 .giaddr = {INADDR_ANY},
480 struct dhcpv4_auth_forcerenew *auth_o, auth = {
484 .replay = {htonl(time(NULL)), htonl(++serial)},
488 struct interface *iface = a->iface;
490 odhcpd_urandom(&fr_msg.xid, sizeof(fr_msg.xid));
491 memcpy(fr_msg.chaddr, a->hwaddr, fr_msg.hlen);
493 fr_msg.options[0] = 0x63;
494 fr_msg.options[1] = 0x82;
495 fr_msg.options[2] = 0x53;
496 fr_msg.options[3] = 0x63;
498 uint8_t *cookie = &fr_msg.options[4];
499 uint8_t msg = DHCPV4_MSG_FORCERENEW;
501 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg);
502 if (a->accept_fr_nonce) {
503 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_AUTHENTICATION, sizeof(auth), &auth);
504 auth_o = (struct dhcpv4_auth_forcerenew *)(cookie - sizeof(auth));
505 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_END, 0, NULL);
508 uint8_t secretbytes[64];
509 memset(secretbytes, 0, sizeof(secretbytes));
510 memcpy(secretbytes, a->key, sizeof(a->key));
512 for (size_t i = 0; i < sizeof(secretbytes); ++i)
513 secretbytes[i] ^= 0x36;
516 md5_hash(secretbytes, sizeof(secretbytes), &md5);
517 md5_hash(&fr_msg, sizeof(fr_msg), &md5);
518 md5_end(auth_o->key, &md5);
520 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
521 secretbytes[i] ^= 0x36;
522 secretbytes[i] ^= 0x5c;
526 md5_hash(secretbytes, sizeof(secretbytes), &md5);
527 md5_hash(auth_o->key, sizeof(auth_o->key), &md5);
528 md5_end(auth_o->key, &md5);
530 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_SERVERID, 4,
531 &a->fr_ip->addr.addr.in.s_addr);
532 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_END, 0, NULL);
535 struct sockaddr_in dest;
536 memset(&dest, 0, sizeof(dest));
537 dest.sin_family = AF_INET;
538 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
539 dest.sin_addr.s_addr = a->addr;
541 syslog(LOG_WARNING, "sending %s to %02x:%02x:%02x:%02x:%02x:%02x - %s",
542 dhcpv4_msg_to_string(msg),
543 a->hwaddr[0], a->hwaddr[1], a->hwaddr[2],
544 a->hwaddr[3], a->hwaddr[4], a->hwaddr[5],
545 inet_ntoa(dest.sin_addr));
547 sendto(iface->dhcpv4_event.uloop.fd, &fr_msg, sizeof(fr_msg),
548 MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest));
551 static void dhcpv4_fr_timer(struct uloop_timeout *event)
553 struct dhcpv4_assignment *a = container_of(event, struct dhcpv4_assignment, fr_timer);
555 if (a->fr_cnt > 0 && a->fr_cnt < 8) {
557 uloop_timeout_set(&a->fr_timer, 1000 << a->fr_cnt);
563 static void dhcpv4_fr_start(struct dhcpv4_assignment *a)
565 uloop_timeout_set(&a->fr_timer, 1000 << a->fr_cnt);
566 a->fr_timer.cb = dhcpv4_fr_timer;
572 static void dhcpv4_fr_stop(struct dhcpv4_assignment *a)
574 uloop_timeout_cancel(&a->fr_timer);
575 decr_ref_cnt_ip(&a->fr_ip, a->iface);
577 a->fr_timer.cb = NULL;
580 // Handler for DHCPv4 messages
581 static void handle_dhcpv4(void *addr, void *data, size_t len,
582 struct interface *iface, _unused void *dest_addr)
587 struct dhcpv4_message *req = data;
588 if (len < offsetof(struct dhcpv4_message, options) + 4 ||
589 req->op != DHCPV4_BOOTREQUEST || req->hlen != 6)
593 syslog(LOG_NOTICE, "Got DHCPv4 request");
595 if (!iface->dhcpv4_start_ip.s_addr && !iface->dhcpv4_end_ip.s_addr) {
596 syslog(LOG_WARNING, "No DHCP range available on %s", iface->name);
600 int sock = iface->dhcpv4_event.uloop.fd;
602 struct dhcpv4_message reply = {
603 .op = DHCPV4_BOOTREPLY,
610 .ciaddr = {INADDR_ANY},
611 .giaddr = req->giaddr,
612 .siaddr = iface->dhcpv4_local,
614 memcpy(reply.chaddr, req->chaddr, sizeof(reply.chaddr));
616 reply.options[0] = 0x63;
617 reply.options[1] = 0x82;
618 reply.options[2] = 0x53;
619 reply.options[3] = 0x63;
621 uint8_t *cookie = &reply.options[4];
622 uint8_t reqmsg = DHCPV4_MSG_REQUEST;
623 uint8_t msg = DHCPV4_MSG_ACK;
625 uint32_t reqaddr = INADDR_ANY;
626 uint32_t leasetime = 0;
627 size_t hostname_len = 0;
629 bool accept_fr_nonce = false;
630 bool incl_fr_opt = false;
632 uint8_t *start = &req->options[4];
633 uint8_t *end = ((uint8_t*)data) + len;
634 struct dhcpv4_option *opt;
635 dhcpv4_for_each_option(start, end, opt) {
636 if (opt->type == DHCPV4_OPT_MESSAGE && opt->len == 1)
637 reqmsg = opt->data[0];
638 else if (opt->type == DHCPV4_OPT_HOSTNAME && opt->len > 0) {
639 hostname_len = opt->len;
640 memcpy(hostname, opt->data, hostname_len);
641 hostname[hostname_len] = 0;
642 } else if (opt->type == DHCPV4_OPT_IPADDRESS && opt->len == 4)
643 memcpy(&reqaddr, opt->data, 4);
644 else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) {
645 if (memcmp(opt->data, &iface->dhcpv4_local, 4))
647 } else if (iface->filter_class && opt->type == DHCPV4_OPT_USER_CLASS) {
648 uint8_t *c = opt->data, *cend = &opt->data[opt->len];
649 for (; c < cend && &c[*c] < cend; c = &c[1 + *c]) {
650 size_t elen = strlen(iface->filter_class);
651 if (*c == elen && !memcmp(&c[1], iface->filter_class, elen))
652 return; // Ignore from homenet
654 } else if (opt->type == DHCPV4_OPT_LEASETIME && opt->len == 4)
655 memcpy(&leasetime, opt->data, 4);
656 else if (opt->type == DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE && opt->len > 0) {
657 for (uint8_t i = 0; i < opt->len; i++) {
658 if (opt->data[i] == 1) {
659 accept_fr_nonce = true;
667 if (reqmsg != DHCPV4_MSG_DISCOVER && reqmsg != DHCPV4_MSG_REQUEST &&
668 reqmsg != DHCPV4_MSG_INFORM && reqmsg != DHCPV4_MSG_DECLINE &&
669 reqmsg != DHCPV4_MSG_RELEASE)
672 struct dhcpv4_assignment *lease = NULL;
673 uint32_t serverid = iface->dhcpv4_local.s_addr;
674 uint32_t fr_serverid = INADDR_ANY;
676 if (reqmsg != DHCPV4_MSG_INFORM)
677 lease = dhcpv4_lease(iface, reqmsg, req->chaddr, reqaddr,
678 &leasetime, hostname, hostname_len,
679 accept_fr_nonce, &incl_fr_opt, &fr_serverid);
682 if (reqmsg == DHCPV4_MSG_REQUEST)
683 msg = DHCPV4_MSG_NAK;
684 else if (reqmsg == DHCPV4_MSG_DISCOVER)
686 } else if (reqmsg == DHCPV4_MSG_DISCOVER)
687 msg = DHCPV4_MSG_OFFER;
688 else if (reqmsg == DHCPV4_MSG_REQUEST &&
689 ((reqaddr && reqaddr != lease->addr) ||
690 (req->ciaddr.s_addr && req->ciaddr.s_addr != lease->addr))) {
691 msg = DHCPV4_MSG_NAK;
693 * DHCP client requested an IP which we can't offer to him. Probably the
694 * client changed the network or the network has been changed. The reply
695 * type is set to DHCPV4_MSG_NAK, because the client should not use that IP.
697 * For modern devices we build an answer that includes a valid IP, like
698 * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
699 * perform additional DHCP round trips.
705 * Buggy clients do serverid checking in nack messages; therefore set the
706 * serverid in nack messages triggered by a previous force renew equal to
707 * the server id in use at that time by the server
711 serverid = fr_serverid;
713 if (req->ciaddr.s_addr &&
714 ((iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr) !=
715 (req->ciaddr.s_addr & iface->dhcpv4_mask.s_addr)))
716 req->ciaddr.s_addr = INADDR_ANY;
719 syslog(LOG_WARNING, "received %s from %02x:%02x:%02x:%02x:%02x:%02x",
720 dhcpv4_msg_to_string(reqmsg),
721 req->chaddr[0],req->chaddr[1],req->chaddr[2],
722 req->chaddr[3],req->chaddr[4],req->chaddr[5]);
725 if (reqmsg == DHCPV4_MSG_RELEASE)
726 ubus_bcast_dhcp_event("dhcp.release", req->chaddr, req->hlen,
727 &req->ciaddr, hostname, iface->ifname);
729 if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
732 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg);
733 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SERVERID, 4, &serverid);
738 reply.yiaddr.s_addr = lease->addr;
740 val = htonl(leasetime);
741 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_LEASETIME, 4, &val);
743 if (leasetime != UINT32_MAX) {
744 val = htonl(500 * leasetime / 1000);
745 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_RENEW, 4, &val);
747 val = htonl(875 * leasetime / 1000);
748 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_REBIND, 4, &val);
751 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NETMASK, 4,
752 &iface->dhcpv4_mask.s_addr);
755 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_HOSTNAME,
756 strlen(lease->hostname), lease->hostname);
758 if (iface->dhcpv4_bcast.s_addr != INADDR_ANY)
759 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_BROADCAST, 4, &iface->dhcpv4_bcast);
762 if (reqmsg == DHCPV4_MSG_REQUEST) {
763 struct dhcpv4_auth_forcerenew auth = {
767 .replay = {htonl(time(NULL)), htonl(++serial)},
772 memcpy(auth.key, lease->key, sizeof(auth.key));
773 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_AUTHENTICATION, sizeof(auth), &auth);
776 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE,
782 struct ifreq ifr = {.ifr_name = ""};
783 strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
785 if (!ioctl(sock, SIOCGIFMTU, &ifr)) {
786 uint16_t mtu = htons(ifr.ifr_mtu);
787 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MTU, 2, &mtu);
790 if (iface->search && iface->search_len <= 255)
791 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
792 iface->search_len, iface->search);
793 else if (!res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
794 uint8_t search_buf[256];
795 int len = dn_comp(_res.dnsrch[0], search_buf,
796 sizeof(search_buf), NULL, NULL);
798 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
802 if (iface->dhcpv4_router_cnt == 0)
803 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER, 4, &iface->dhcpv4_local);
805 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER,
806 4 * iface->dhcpv4_router_cnt, iface->dhcpv4_router);
809 if (iface->dhcpv4_dns_cnt == 0)
810 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &iface->dhcpv4_local);
812 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER,
813 4 * iface->dhcpv4_dns_cnt, iface->dhcpv4_dns);
816 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_END, 0, NULL);
818 struct sockaddr_in dest = *((struct sockaddr_in*)addr);
819 if (req->giaddr.s_addr) {
821 * relay agent is configured, send reply to the agent
823 dest.sin_addr = req->giaddr;
824 dest.sin_port = htons(DHCPV4_SERVER_PORT);
825 } else if (req->ciaddr.s_addr && req->ciaddr.s_addr != dest.sin_addr.s_addr) {
827 * client has existing configuration (ciaddr is set) AND this address is
828 * not the address it used for the dhcp message
830 dest.sin_addr = req->ciaddr;
831 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
832 } else if ((ntohs(req->flags) & DHCPV4_FLAG_BROADCAST) ||
833 req->hlen != reply.hlen || !reply.yiaddr.s_addr) {
835 * client requests a broadcast reply OR we can't offer an IP
837 dest.sin_addr.s_addr = INADDR_BROADCAST;
838 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
839 } else if (!req->ciaddr.s_addr && msg == DHCPV4_MSG_NAK) {
841 * client has no previous configuration -> no IP, so we need to reply
842 * with a broadcast packet
844 dest.sin_addr.s_addr = INADDR_BROADCAST;
845 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
848 * send reply to the newly (in this proccess) allocated IP
850 dest.sin_addr = reply.yiaddr;
851 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
853 struct arpreq arp = {.arp_flags = ATF_COM};
854 memcpy(arp.arp_ha.sa_data, req->chaddr, 6);
855 memcpy(&arp.arp_pa, &dest, sizeof(arp.arp_pa));
856 memcpy(arp.arp_dev, iface->ifname, sizeof(arp.arp_dev));
857 ioctl(sock, SIOCSARP, &arp);
860 if (dest.sin_addr.s_addr == INADDR_BROADCAST)
861 /* reply goes to IP broadcast -> MAC broadcast */
862 syslog(LOG_WARNING, "sending %s to ff:ff:ff:ff:ff:ff - %s",
863 dhcpv4_msg_to_string(msg),
864 inet_ntoa(dest.sin_addr));
867 * reply is send directly to IP,
868 * MAC is assumed to be the same as the request
870 syslog(LOG_WARNING, "sending %s to %02x:%02x:%02x:%02x:%02x:%02x - %s",
871 dhcpv4_msg_to_string(msg),
872 req->chaddr[0],req->chaddr[1],req->chaddr[2],
873 req->chaddr[3],req->chaddr[4],req->chaddr[5],
874 inet_ntoa(dest.sin_addr));
876 sendto(sock, &reply, sizeof(reply), MSG_DONTWAIT,
877 (struct sockaddr*)&dest, sizeof(dest));
880 if (msg == DHCPV4_MSG_ACK)
881 ubus_bcast_dhcp_event("dhcp.ack", req->chaddr, req->hlen, &reply.yiaddr,
882 hostname, iface->ifname);
886 static bool dhcpv4_assign(struct interface *iface,
887 struct dhcpv4_assignment *assign, uint32_t raddr)
889 uint32_t start = ntohl(iface->dhcpv4_start_ip.s_addr);
890 uint32_t end = ntohl(iface->dhcpv4_end_ip.s_addr);
891 uint32_t count = end - start + 1;
893 // try to assign the IP the client asked for
894 if (start <= ntohl(raddr) && ntohl(raddr) <= end &&
895 !find_assignment_by_addr(iface, raddr)) {
896 assign->addr = raddr;
897 syslog(LOG_INFO, "assigning the IP the client asked for: %u.%u.%u.%u",
898 ((uint8_t *)&assign->addr)[0],
899 ((uint8_t *)&assign->addr)[1],
900 ((uint8_t *)&assign->addr)[2],
901 ((uint8_t *)&assign->addr)[3]);
905 // Seed RNG with checksum of hwaddress
907 for (size_t i = 0; i < sizeof(assign->hwaddr); ++i) {
908 // Knuth's multiplicative method
909 uint8_t o = assign->hwaddr[i];
910 seed += (o*2654435761) % UINT32_MAX;
915 uint32_t try = (((uint32_t)rand()) % count) + start;
917 if (list_empty(&iface->dhcpv4_assignments)) {
918 assign->addr = htonl(try);
919 syslog(LOG_INFO, "assigning mapped IP (empty list): %u.%u.%u.%u",
920 ((uint8_t *)&assign->addr)[0],
921 ((uint8_t *)&assign->addr)[1],
922 ((uint8_t *)&assign->addr)[2],
923 ((uint8_t *)&assign->addr)[3]);
927 for (uint32_t i = 0; i < count; ++i) {
928 if (!find_assignment_by_addr(iface, htonl(try))) {
929 /* test was successful: IP address is not assigned, assign it */
930 assign->addr = htonl(try);
931 syslog(LOG_DEBUG, "assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
932 ((uint8_t *)&assign->addr)[0],
933 ((uint8_t *)&assign->addr)[1],
934 ((uint8_t *)&assign->addr)[2],
935 ((uint8_t *)&assign->addr)[3],
939 try = (((try - start) + 1) % count) + start;
942 syslog(LOG_WARNING, "can't assign any IP address -> address space is full");
947 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
948 enum dhcpv4_msg msg, const uint8_t *mac, const uint32_t reqaddr,
949 uint32_t *leasetime, const char *hostname, const size_t hostname_len,
950 const bool accept_fr_nonce, bool *incl_fr_opt, uint32_t *fr_serverid)
952 struct dhcpv4_assignment *a = find_assignment_by_hwaddr(iface, mac);
953 struct dhcpv4_assignment *lease = NULL;
954 time_t now = odhcpd_time();
956 if (a && (a->flags & OAF_BOUND) && a->fr_ip) {
957 *fr_serverid = a->fr_ip->addr.addr.in.s_addr;
961 if (msg == DHCPV4_MSG_DISCOVER || msg == DHCPV4_MSG_REQUEST) {
965 if (!iface->no_dynamic_dhcp) {
966 /* Create new binding */
967 a = calloc(1, sizeof(*a));
969 syslog(LOG_ERR, "Failed to calloc binding on interface %s",
973 memcpy(a->hwaddr, mac, sizeof(a->hwaddr));
974 /* Don't consider new assignment as infinite */
975 a->valid_until = now;
977 assigned = dhcpv4_assign(iface, a, reqaddr);
980 list_add(&a->head, &iface->dhcpv4_assignments);
983 } else if ((a->addr & iface->dhcpv4_mask.s_addr) !=
984 (iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr)) {
987 assigned = dhcpv4_assign(iface, a, reqaddr);
989 list_add(&a->head, &iface->dhcpv4_assignments);
993 uint32_t my_leasetime;
996 my_leasetime = a->leasetime;
998 my_leasetime = iface->dhcpv4_leasetime;
1000 if ((*leasetime == 0) || (my_leasetime < *leasetime))
1001 *leasetime = my_leasetime;
1003 if (msg == DHCPV4_MSG_DISCOVER) {
1004 a->flags &= ~OAF_BOUND;
1006 *incl_fr_opt = accept_fr_nonce;
1007 if (!(a->flags & OAF_STATIC))
1008 a->valid_until = now;
1010 if (hostname_len > 0) {
1011 a->hostname = realloc(a->hostname, hostname_len + 1);
1013 memcpy(a->hostname, hostname, hostname_len);
1014 a->hostname[hostname_len] = 0;
1018 if (!(a->flags & OAF_BOUND)) {
1019 a->accept_fr_nonce = accept_fr_nonce;
1020 *incl_fr_opt = accept_fr_nonce;
1021 odhcpd_urandom(a->key, sizeof(a->key));
1022 a->flags |= OAF_BOUND;
1024 *incl_fr_opt = false;
1026 if (!(a->flags & OAF_STATIC))
1027 a->valid_until = ((*leasetime == UINT32_MAX) ? 0 : (time_t)(now + *leasetime));
1029 } else if (!assigned && a) {
1030 /* Cleanup failed assignment */
1031 free_dhcpv4_assignment(a);
1037 } else if (msg == DHCPV4_MSG_RELEASE && a) {
1038 a->flags &= ~OAF_BOUND;
1040 if (!(a->flags & OAF_STATIC))
1041 a->valid_until = now - 1;
1043 } else if (msg == DHCPV4_MSG_DECLINE && a) {
1044 a->flags &= ~OAF_BOUND;
1046 if (!(a->flags & OAF_STATIC)) {
1047 memset(a->hwaddr, 0, sizeof(a->hwaddr));
1048 a->valid_until = now + 3600; /* Block address for 1h */
1052 dhcpv6_write_statefile();