2 * Copyright (C) 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.
32 #include <arpa/inet.h>
33 #include <sys/timerfd.h>
35 #include <libubox/md5.h>
36 #include <libubox/usock.h>
38 #define ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) \
39 ((iface)->dhcpv6_assignall || (i) == (m) || \
40 (addrs)[(i)].prefix > 64)
42 static void dhcpv6_netevent_cb(unsigned long event, struct netevent_handler_info *info);
43 static void free_dhcpv6_assignment(struct dhcpv6_assignment *c);
44 static void handle_addrlist_change(struct netevent_handler_info *info);
45 static void start_reconf(struct dhcpv6_assignment *a);
46 static void stop_reconf(struct dhcpv6_assignment *a);
47 static void valid_until_cb(struct uloop_timeout *event);
49 static struct netevent_handler dhcpv6_netevent_handler = { .cb = dhcpv6_netevent_cb, };
50 static struct uloop_timeout valid_until_timeout = {.cb = valid_until_cb};
51 static uint32_t serial = 0;
52 static uint8_t statemd5[16];
54 int dhcpv6_ia_init(void)
56 uloop_timeout_set(&valid_until_timeout, 1000);
58 netlink_add_netevent_handler(&dhcpv6_netevent_handler);
63 int dhcpv6_setup_ia_interface(struct interface *iface, bool enable)
65 if (!enable && iface->ia_assignments.next) {
66 struct dhcpv6_assignment *c;
68 while (!list_empty(&iface->ia_assignments)) {
69 c = list_first_entry(&iface->ia_assignments, struct dhcpv6_assignment, head);
70 free_dhcpv6_assignment(c);
74 if (enable && iface->dhcpv6 == MODE_SERVER) {
75 if (!iface->ia_assignments.next)
76 INIT_LIST_HEAD(&iface->ia_assignments);
78 if (list_empty(&iface->ia_assignments)) {
79 struct dhcpv6_assignment *border = calloc(1, sizeof(*border));
81 syslog(LOG_ERR, "Calloc failed for border on interface %s", iface->ifname);
86 list_add(&border->head, &iface->ia_assignments);
89 /* Parse static entries */
91 list_for_each_entry(lease, &leases, head) {
93 size_t duid_len = lease->duid_len ? lease->duid_len : 14;
94 struct dhcpv6_assignment *a = calloc(1, sizeof(*a) + duid_len);
96 syslog(LOG_ERR, "Calloc failed for static lease assignment on interface %s",
101 a->leasetime = lease->dhcpv4_leasetime;
103 a->clid_len = duid_len;
106 a->assigned = lease->hostid;
108 uint32_t i4a = ntohl(lease->ipaddr.s_addr) & 0xff;
109 a->assigned = ((i4a / 100) << 8) | (((i4a % 100) / 10) << 4) | (i4a % 10);
112 odhcpd_urandom(a->key, sizeof(a->key));
113 memcpy(a->clid_data, lease->duid, lease->duid_len);
114 memcpy(a->mac, lease->mac.ether_addr_octet, sizeof(a->mac));
115 /* Static assignment */
116 a->flags |= OAF_STATIC;
120 /* Assign to all interfaces */
121 struct dhcpv6_assignment *c;
122 list_for_each_entry(c, &iface->ia_assignments, head) {
123 if (c->length != 128 || c->assigned > a->assigned) {
124 list_add_tail(&a->head, &c->head);
126 } else if (c->assigned == a->assigned)
127 /* Already an assignment with that number */
133 if (lease->hostname[0]) {
135 a->hostname = strdup(lease->hostname);
138 free_dhcpv6_assignment(a);
145 static void dhcpv6_netevent_cb(unsigned long event, struct netevent_handler_info *info)
147 struct interface *iface = info->iface;
149 if (!iface || iface->dhcpv6 != MODE_SERVER)
153 case NETEV_ADDR6LIST_CHANGE:
154 handle_addrlist_change(info);
162 static void free_dhcpv6_assignment(struct dhcpv6_assignment *c)
164 if (c->managed_sock.fd.registered) {
165 ustream_free(&c->managed_sock.stream);
166 close(c->managed_sock.fd.fd);
180 static inline bool valid_prefix_length(const struct dhcpv6_assignment *a, const uint8_t prefix_length)
182 return (a->managed_size || a->length > prefix_length);
185 static inline bool valid_addr(const struct odhcpd_ipaddr *addr, time_t now)
187 return (addr->prefix <= 96 && addr->preferred > (uint32_t)now);
190 static size_t get_preferred_addr(const struct odhcpd_ipaddr *addrs, const size_t addrlen)
194 for (i = 0, m = 0; i < addrlen; ++i) {
195 if (addrs[i].preferred > addrs[m].preferred ||
196 (addrs[i].preferred == addrs[m].preferred &&
197 memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
204 static int send_reconf(struct dhcpv6_assignment *assign)
207 struct dhcpv6_client_header hdr;
211 uint16_t hardware_type;
216 struct dhcpv6_auth_reconfigure auth;
219 uint8_t clid_data[128];
220 } __attribute__((packed)) reconf_msg = {
221 .hdr = {DHCPV6_MSG_RECONFIGURE, {0, 0, 0}},
222 .srvid_type = htons(DHCPV6_OPT_SERVERID),
223 .srvid_len = htons(10),
224 .duid_type = htons(3),
225 .hardware_type = htons(1),
226 .msg_type = htons(DHCPV6_OPT_RECONF_MSG),
228 .msg_id = DHCPV6_MSG_RENEW,
229 .auth = {htons(DHCPV6_OPT_AUTH),
230 htons(sizeof(reconf_msg.auth) - 4), 3, 1, 0,
231 {htonl(time(NULL)), htonl(++serial)}, 2, {0}},
232 .clid_type = htons(DHCPV6_OPT_CLIENTID),
233 .clid_len = htons(assign->clid_len),
236 struct interface *iface = assign->iface;
238 odhcpd_get_mac(iface, reconf_msg.mac);
239 memcpy(reconf_msg.clid_data, assign->clid_data, assign->clid_len);
240 struct iovec iov = {&reconf_msg, sizeof(reconf_msg) - 128 + assign->clid_len};
243 uint8_t secretbytes[64];
244 memset(secretbytes, 0, sizeof(secretbytes));
245 memcpy(secretbytes, assign->key, sizeof(assign->key));
247 for (size_t i = 0; i < sizeof(secretbytes); ++i)
248 secretbytes[i] ^= 0x36;
251 md5_hash(secretbytes, sizeof(secretbytes), &md5);
252 md5_hash(iov.iov_base, iov.iov_len, &md5);
253 md5_end(reconf_msg.auth.key, &md5);
255 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
256 secretbytes[i] ^= 0x36;
257 secretbytes[i] ^= 0x5c;
261 md5_hash(secretbytes, sizeof(secretbytes), &md5);
262 md5_hash(reconf_msg.auth.key, 16, &md5);
263 md5_end(reconf_msg.auth.key, &md5);
265 return odhcpd_send(iface->dhcpv6_event.uloop.fd, &assign->peer, &iov, 1, iface);
268 void dhcpv6_enum_ia_addrs(struct interface *iface, struct dhcpv6_assignment *c,
269 time_t now, dhcpv6_binding_cb_handler_t func, void *arg)
271 struct odhcpd_ipaddr *addrs = (c->managed) ? c->managed : iface->addr6;
272 size_t addrlen = (c->managed) ? (size_t)c->managed_size : iface->addr6_len;
273 size_t m = get_preferred_addr(addrs, addrlen);
275 for (size_t i = 0; i < addrlen; ++i) {
276 struct in6_addr addr;
277 uint32_t pref, valid;
278 int prefix = c->managed ? addrs[i].prefix : c->length;
280 if (!valid_addr(&addrs[i], now))
283 addr = addrs[i].addr.in6;
284 pref = addrs[i].preferred;
285 valid = addrs[i].valid;
287 if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs))
290 addr.s6_addr32[3] = htonl(c->assigned);
292 if (!valid_prefix_length(c, addrs[i].prefix))
295 addr.s6_addr32[1] |= htonl(c->assigned);
296 addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
299 if (pref != UINT32_MAX)
302 if (valid != UINT32_MAX)
305 func(&addr, prefix, pref, valid, arg);
312 struct dhcpv6_assignment *c;
313 struct interface *iface;
319 void dhcpv6_write_ia_addr(struct in6_addr *addr, int prefix, _unused uint32_t pref,
320 _unused uint32_t valid, void *arg)
322 struct write_ctxt *ctxt = (struct write_ctxt *)arg;
323 char ipbuf[INET6_ADDRSTRLEN];
325 inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf) - 1);
327 if (ctxt->c->length == 128 && ctxt->c->hostname) {
328 fputs(ipbuf, ctxt->fp);
331 if (dn_expand(ctxt->iface->search, ctxt->iface->search + ctxt->iface->search_len,
332 ctxt->iface->search, b, sizeof(b)) > 0)
333 fprintf(ctxt->fp, "\t%s.%s", ctxt->c->hostname, b);
335 fprintf(ctxt->fp, "\t%s\n", ctxt->c->hostname);
336 md5_hash(ipbuf, strlen(ipbuf), &ctxt->md5);
337 md5_hash(ctxt->c->hostname, strlen(ctxt->c->hostname), &ctxt->md5);
340 ctxt->buf_idx += snprintf(ctxt->buf + ctxt->buf_idx,ctxt->buf_len - ctxt->buf_idx,
341 "%s/%d ", ipbuf, prefix);
344 void dhcpv6_write_statefile(void)
346 struct write_ctxt ctxt;
348 md5_begin(&ctxt.md5);
350 if (config.dhcp_statefile) {
351 time_t now = odhcpd_time(), wall_time = time(NULL);
352 int fd = open(config.dhcp_statefile, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
358 ret = lockf(fd, F_LOCK, 0);
363 if (ftruncate(fd, 0) < 0) {}
365 ctxt.fp = fdopen(fd, "w");
372 ctxt.buf_len = sizeof(leasebuf);
374 list_for_each_entry(ctxt.iface, &interfaces, head) {
375 if (ctxt.iface->dhcpv6 != MODE_SERVER &&
376 ctxt.iface->dhcpv4 != MODE_SERVER)
379 if (ctxt.iface->dhcpv6 == MODE_SERVER &&
380 ctxt.iface->ia_assignments.next) {
381 list_for_each_entry(ctxt.c, &ctxt.iface->ia_assignments, head) {
382 if (!(ctxt.c->flags & OAF_BOUND) || ctxt.c->managed_size < 0)
387 odhcpd_hexlify(duidbuf, ctxt.c->clid_data, ctxt.c->clid_len);
389 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
390 ctxt.buf_idx = snprintf(ctxt.buf, ctxt.buf_len, "# %s %s %x %s %ld %x %u ",
391 ctxt.iface->ifname, duidbuf, ntohl(ctxt.c->iaid),
392 (ctxt.c->hostname ? ctxt.c->hostname : "-"),
393 (ctxt.c->valid_until > now ?
394 (ctxt.c->valid_until - now + wall_time) :
395 (INFINITE_VALID(ctxt.c->valid_until) ? -1 : 0)),
396 ctxt.c->assigned, (unsigned)ctxt.c->length);
398 if (INFINITE_VALID(ctxt.c->valid_until) || ctxt.c->valid_until > now)
399 dhcpv6_enum_ia_addrs(ctxt.iface, ctxt.c, now,
400 dhcpv6_write_ia_addr, &ctxt);
402 ctxt.buf[ctxt.buf_idx - 1] = '\n';
403 fwrite(ctxt.buf, 1, ctxt.buf_idx, ctxt.fp);
407 if (ctxt.iface->dhcpv4 == MODE_SERVER &&
408 ctxt.iface->dhcpv4_assignments.next) {
409 struct dhcpv4_assignment *c;
410 list_for_each_entry(c, &ctxt.iface->dhcpv4_assignments, head) {
411 if (!(c->flags & OAF_BOUND))
414 char ipbuf[INET6_ADDRSTRLEN];
416 odhcpd_hexlify(duidbuf, c->hwaddr, sizeof(c->hwaddr));
418 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
419 ctxt.buf_idx = snprintf(ctxt.buf, ctxt.buf_len, "# %s %s ipv4 %s %ld %x 32 ",
420 ctxt.iface->ifname, duidbuf,
421 (c->hostname ? c->hostname : "-"),
422 (c->valid_until > now ?
423 (c->valid_until - now + wall_time) :
424 (INFINITE_VALID(c->valid_until) ? -1 : 0)),
427 struct in_addr addr = {.s_addr = c->addr};
428 inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf) - 1);
431 fputs(ipbuf, ctxt.fp);
434 if (dn_expand(ctxt.iface->search,
435 ctxt.iface->search + ctxt.iface->search_len,
436 ctxt.iface->search, b, sizeof(b)) > 0)
437 fprintf(ctxt.fp, "\t%s.%s", c->hostname, b);
439 fprintf(ctxt.fp, "\t%s\n", c->hostname);
440 md5_hash(ipbuf, strlen(ipbuf), &ctxt.md5);
441 md5_hash(c->hostname, strlen(c->hostname), &ctxt.md5);
444 ctxt.buf_idx += snprintf(ctxt.buf + ctxt.buf_idx,
445 ctxt.buf_len - ctxt.buf_idx,
447 ctxt.buf[ctxt.buf_idx - 1] = '\n';
448 fwrite(ctxt.buf, 1, ctxt.buf_idx, ctxt.fp);
457 md5_end(newmd5, &ctxt.md5);
459 if (config.dhcp_cb && memcmp(newmd5, statemd5, sizeof(newmd5))) {
460 memcpy(statemd5, newmd5, sizeof(statemd5));
461 char *argv[2] = {config.dhcp_cb, NULL};
463 execv(argv[0], argv);
469 static void __apply_lease(struct interface *iface, struct dhcpv6_assignment *a,
470 struct odhcpd_ipaddr *addrs, ssize_t addr_len, bool add)
475 for (ssize_t i = 0; i < addr_len; ++i) {
476 struct in6_addr prefix = addrs[i].addr.in6;
477 prefix.s6_addr32[1] |= htonl(a->assigned);
478 prefix.s6_addr32[2] = prefix.s6_addr32[3] = 0;
479 netlink_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length,
480 iface->ifindex, &a->peer.sin6_addr, 1024, add);
484 static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bool add)
486 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->addr6;
487 ssize_t addrlen = (a->managed) ? a->managed_size : (ssize_t)iface->addr6_len;
489 __apply_lease(iface, a, addrs, addrlen, add);
492 /* More data was received from TCP connection */
493 static void managed_handle_pd_data(struct ustream *s, _unused int bytes_new)
495 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
496 time_t now = odhcpd_time();
497 bool first = c->managed_size < 0;
501 char *data = ustream_get_read_buf(s, &pending);
502 char *end = memmem(data, pending, "\n\n", 2);
511 if (c->accept_reconf)
515 for (char *line = strtok_r(data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr)) {
516 c->managed = realloc(c->managed, (c->managed_size + 1) * sizeof(*c->managed));
517 struct odhcpd_ipaddr *n = &c->managed[c->managed_size];
519 char *saveptr2, *x = strtok_r(line, "/", &saveptr2);
520 if (!x || inet_pton(AF_INET6, x, &n->addr) < 1)
523 x = strtok_r(NULL, ",", &saveptr2);
524 if (sscanf(x, "%hhu", &n->prefix) < 1)
527 x = strtok_r(NULL, ",", &saveptr2);
528 if (sscanf(x, "%u", &n->preferred) < 1)
531 x = strtok_r(NULL, ",", &saveptr2);
532 if (sscanf(x, "%u", &n->valid) < 1)
535 if (n->preferred > n->valid)
538 if (UINT32_MAX - now < n->preferred)
539 n->preferred = UINT32_MAX;
543 if (UINT32_MAX - now < n->valid)
544 n->valid = UINT32_MAX;
553 ustream_consume(s, end - data);
556 if (first && c->managed_size == 0)
557 free_dhcpv6_assignment(c);
558 else if (first && !(c->flags & OAF_STATIC))
559 c->valid_until = now + 150;
563 /* TCP transmission has ended, either because of success or timeout or other error */
564 static void managed_handle_pd_done(struct ustream *s)
566 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
568 if (!(c->flags & OAF_STATIC))
569 c->valid_until = odhcpd_time() + 15;
573 if (c->accept_reconf)
577 static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
579 struct dhcpv6_assignment *c;
581 if (iface->dhcpv6_pd_manager[0]) {
582 int fd = usock(USOCK_UNIX | USOCK_TCP, iface->dhcpv6_pd_manager, NULL);
584 struct pollfd pfd = { .fd = fd, .events = POLLIN };
587 odhcpd_hexlify(iaidbuf, assign->clid_data, assign->clid_len);
589 assign->managed_sock.stream.notify_read = managed_handle_pd_data;
590 assign->managed_sock.stream.notify_state = managed_handle_pd_done;
591 ustream_fd_init(&assign->managed_sock, fd);
592 ustream_printf(&assign->managed_sock.stream, "%s,%x\n::/%d,0,0\n\n",
593 iaidbuf, assign->iaid, assign->length);
594 ustream_write_pending(&assign->managed_sock.stream);
595 assign->managed_size = -1;
597 if (!(assign->flags & OAF_STATIC))
598 assign->valid_until = odhcpd_time() + 15;
600 list_add(&assign->head, &iface->ia_assignments);
602 /* Wait initial period of up to 250ms for immediate assignment */
603 if (poll(&pfd, 1, 250) < 0) {
604 syslog(LOG_ERR, "poll(): %m");
608 managed_handle_pd_data(&assign->managed_sock.stream, 0);
610 if (fcntl(fd, F_GETFL) >= 0 && assign->managed_size > 0)
615 } else if (iface->addr6_len < 1)
618 /* Try honoring the hint first */
619 uint32_t current = 1, asize = (1 << (64 - assign->length)) - 1;
620 if (assign->assigned) {
621 list_for_each_entry(c, &iface->ia_assignments, head) {
622 if (c->length == 128 || c->length == 0)
625 if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
626 list_add_tail(&assign->head, &c->head);
628 if (assign->flags & OAF_BOUND)
629 apply_lease(iface, assign, true);
634 if (c->assigned != 0)
635 current = (c->assigned + (1 << (64 - c->length)));
639 /* Fallback to a variable assignment */
641 list_for_each_entry(c, &iface->ia_assignments, head) {
642 if (c->length == 128 || c->length == 0)
645 current = (current + asize) & (~asize);
646 if (current + asize < c->assigned) {
647 assign->assigned = current;
648 list_add_tail(&assign->head, &c->head);
650 if (assign->flags & OAF_BOUND)
651 apply_lease(iface, assign, true);
656 if (c->assigned != 0)
657 current = (c->assigned + (1 << (64 - c->length)));
663 static bool assign_na(struct interface *iface, struct dhcpv6_assignment *assign)
665 /* Seed RNG with checksum of DUID */
667 for (size_t i = 0; i < assign->clid_len; ++i)
668 seed += assign->clid_data[i];
671 /* Try to assign up to 100x */
672 for (size_t i = 0; i < 100; ++i) {
674 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
676 struct dhcpv6_assignment *c;
677 list_for_each_entry(c, &iface->ia_assignments, head) {
681 if (c->assigned > try || c->length != 128) {
682 assign->assigned = try;
683 list_add_tail(&assign->head, &c->head);
685 } else if (c->assigned == try)
693 static void handle_addrlist_change(struct netevent_handler_info *info)
695 struct interface *iface = info->iface;
696 struct dhcpv6_assignment *c, *d, *border = list_last_entry(
697 &iface->ia_assignments, struct dhcpv6_assignment, head);
698 time_t now = odhcpd_time();
701 list_for_each_entry(c, &iface->ia_assignments, head)
702 if (c != border && iface->ra_managed == RA_MANAGED_NO_MFLAG
703 && (c->flags & OAF_BOUND))
704 __apply_lease(iface, c, info->addrs_old.addrs,
705 info->addrs_old.len, false);
707 for (size_t i = 0; i < iface->addr6_len; ++i) {
708 if (iface->addr6[i].preferred > (uint32_t)now &&
709 iface->addr6[i].prefix < 64 &&
710 iface->addr6[i].prefix > minprefix)
711 minprefix = iface->addr6[i].prefix;
714 if (minprefix > 32 && minprefix <= 64)
715 border->assigned = 1U << (64 - minprefix);
717 border->assigned = 0;
719 struct list_head reassign = LIST_HEAD_INIT(reassign);
720 list_for_each_entry_safe(c, d, &iface->ia_assignments, head) {
721 if (c->clid_len == 0 || (!INFINITE_VALID(c->valid_until) && c->valid_until < now) ||
725 if (c->length < 128 && c->assigned >= border->assigned && c != border)
726 list_move(&c->head, &reassign);
727 else if (c != border && (c->flags & OAF_BOUND))
728 apply_lease(iface, c, true);
730 if (c->accept_reconf && c->reconf_cnt == 0) {
733 /* Leave all other assignments of that client alone */
734 struct dhcpv6_assignment *a;
735 list_for_each_entry(a, &iface->ia_assignments, head)
736 if (a != c && a->clid_len == c->clid_len &&
737 !memcmp(a->clid_data, c->clid_data, a->clid_len))
738 a->reconf_cnt = INT_MAX;
742 while (!list_empty(&reassign)) {
743 c = list_first_entry(&reassign, struct dhcpv6_assignment, head);
745 if (!assign_pd(iface, c)) {
747 list_add(&c->head, &iface->ia_assignments);
751 dhcpv6_write_statefile();
754 static void reconf_timeout_cb(struct uloop_timeout *event)
756 struct dhcpv6_assignment *a = container_of(event, struct dhcpv6_assignment, reconf_timer);
758 if (a->reconf_cnt > 0 && a->reconf_cnt < DHCPV6_REC_MAX_RC) {
760 uloop_timeout_set(&a->reconf_timer,
761 DHCPV6_REC_TIMEOUT << a->reconf_cnt);
767 static void start_reconf(struct dhcpv6_assignment *a)
769 uloop_timeout_set(&a->reconf_timer,
770 DHCPV6_REC_TIMEOUT << a->reconf_cnt);
771 a->reconf_timer.cb = reconf_timeout_cb;
777 static void stop_reconf(struct dhcpv6_assignment *a)
779 uloop_timeout_cancel(&a->reconf_timer);
781 a->reconf_timer.cb = NULL;
784 static void valid_until_cb(struct uloop_timeout *event)
786 time_t now = odhcpd_time();
787 struct interface *iface;
788 list_for_each_entry(iface, &interfaces, head) {
789 if (iface->dhcpv6 != MODE_SERVER || iface->ia_assignments.next == NULL)
792 struct dhcpv6_assignment *a, *n;
793 list_for_each_entry_safe(a, n, &iface->ia_assignments, head) {
794 if (!INFINITE_VALID(a->valid_until) && a->valid_until < now) {
795 if ((a->length < 128 && a->clid_len > 0) ||
796 (a->length == 128 && a->clid_len == 0))
797 free_dhcpv6_assignment(a);
802 uloop_timeout_set(event, 1000);
805 static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
806 const struct dhcpv6_ia_hdr *ia, struct dhcpv6_assignment *a,
807 struct interface *iface, bool request)
809 if (buflen < sizeof(*ia) + sizeof(struct dhcpv6_ia_prefix))
812 struct dhcpv6_ia_hdr out = {ia->type, 0, ia->iaid, 0, 0};
813 size_t datalen = sizeof(out);
814 time_t now = odhcpd_time();
817 struct __attribute__((packed)) {
821 } stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
824 memcpy(buf + datalen, &stat, sizeof(stat));
825 datalen += sizeof(stat);
830 leasetime = a->leasetime;
832 leasetime = iface->dhcpv4_leasetime;
834 uint32_t pref = leasetime;
835 uint32_t valid = leasetime;
837 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->addr6;
838 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->addr6_len;
839 size_t m = get_preferred_addr(addrs, addrlen);
841 for (size_t i = 0; i < addrlen; ++i) {
842 uint32_t prefix_pref = addrs[i].preferred;
843 uint32_t prefix_valid = addrs[i].valid;
845 if (!valid_addr(&addrs[i], now))
848 if (prefix_pref != UINT32_MAX)
851 if (prefix_valid != UINT32_MAX)
854 if (a->length < 128) {
855 struct dhcpv6_ia_prefix p = {
856 .type = htons(DHCPV6_OPT_IA_PREFIX),
857 .len = htons(sizeof(p) - 4),
858 .preferred = htonl(prefix_pref),
859 .valid = htonl(prefix_valid),
860 .prefix = (a->managed_size) ? addrs[i].prefix : a->length,
861 .addr = addrs[i].addr.in6,
863 p.addr.s6_addr32[1] |= htonl(a->assigned);
864 p.addr.s6_addr32[2] = p.addr.s6_addr32[3] = 0;
866 size_t entrlen = sizeof(p) - 4;
868 if (datalen + entrlen + 4 > buflen ||
869 (a->assigned == 0 && a->managed_size == 0) ||
870 !valid_prefix_length(a, addrs[i].prefix))
873 memcpy(buf + datalen, &p, sizeof(p));
874 datalen += entrlen + 4;
876 struct dhcpv6_ia_addr n = {
877 .type = htons(DHCPV6_OPT_IA_ADDR),
878 .len = htons(sizeof(n) - 4),
879 .addr = addrs[i].addr.in6,
880 .preferred = htonl(prefix_pref),
881 .valid = htonl(prefix_valid)
883 n.addr.s6_addr32[3] = htonl(a->assigned);
884 size_t entrlen = sizeof(n) - 4;
886 if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) ||
888 datalen + entrlen + 4 > buflen)
891 memcpy(buf + datalen, &n, sizeof(n));
892 datalen += entrlen + 4;
895 /* Calculate T1 / T2 based on non-deprecated addresses */
896 if (prefix_pref > 0) {
897 if (prefix_pref < pref)
900 if (prefix_valid < valid)
901 valid = prefix_valid;
905 if (!INFINITE_VALID(a->valid_until))
906 /* UINT32_MAX is considered as infinite leasetime */
907 a->valid_until = (valid == UINT32_MAX) ? 0 : valid + now;
909 out.t1 = htonl((pref == UINT32_MAX) ? pref : pref * 5 / 10);
910 out.t2 = htonl((pref == UINT32_MAX) ? pref : pref * 8 / 10);
920 uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + 4;
921 uint16_t otype, olen;
922 dhcpv6_for_each_option((uint8_t*)&ia[1], end, otype, olen, odata) {
923 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&odata[-4];
924 struct dhcpv6_ia_addr *n = (struct dhcpv6_ia_addr*)&odata[-4];
925 if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*p) - 4) &&
926 (otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*n) - 4))
931 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->addr6;
932 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->addr6_len;
934 for (size_t i = 0; i < addrlen; ++i) {
935 if (!valid_addr(&addrs[i], now))
938 struct in6_addr addr = addrs[i].addr.in6;
939 if (ia->type == htons(DHCPV6_OPT_IA_PD)) {
940 addr.s6_addr32[1] |= htonl(a->assigned);
941 addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
943 if (!memcmp(&p->addr, &addr, sizeof(addr)) &&
944 p->prefix == ((a->managed) ? addrs[i].prefix : a->length))
947 addr.s6_addr32[3] = htonl(a->assigned);
949 if (!memcmp(&n->addr, &addr, sizeof(addr)))
956 if (otype == DHCPV6_OPT_IA_PREFIX) {
957 struct dhcpv6_ia_prefix inv = {
958 .type = htons(DHCPV6_OPT_IA_PREFIX),
959 .len = htons(sizeof(inv) - 4),
966 if (datalen + sizeof(inv) > buflen)
969 memcpy(buf + datalen, &inv, sizeof(inv));
970 datalen += sizeof(inv);
972 struct dhcpv6_ia_addr inv = {
973 .type = htons(DHCPV6_OPT_IA_ADDR),
974 .len = htons(sizeof(inv) - 4),
980 if (datalen + sizeof(inv) > buflen)
983 memcpy(buf + datalen, &inv, sizeof(inv));
984 datalen += sizeof(inv);
991 out.len = htons(datalen - 4);
992 memcpy(buf, &out, sizeof(out));
1002 static void dhcpv6_log_ia_addr(struct in6_addr *addr, int prefix, _unused uint32_t pref,
1003 _unused uint32_t valid, void *arg)
1005 struct log_ctxt *ctxt = (struct log_ctxt *)arg;
1006 char addrbuf[INET6_ADDRSTRLEN];
1008 inet_ntop(AF_INET6, addr, addrbuf, sizeof(addrbuf));
1009 ctxt->buf_idx += snprintf(ctxt->buf + ctxt->buf_idx, ctxt->buf_len - ctxt->buf_idx,
1010 "%s/%d ", addrbuf, prefix);
1013 static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now,
1014 const char *duidbuf, bool is_pd, struct dhcpv6_assignment *a, int code)
1016 const char *type = "UNKNOWN";
1017 const char *status = "UNKNOWN";
1020 case DHCPV6_MSG_SOLICIT:
1023 case DHCPV6_MSG_REQUEST:
1026 case DHCPV6_MSG_CONFIRM:
1029 case DHCPV6_MSG_RENEW:
1032 case DHCPV6_MSG_REBIND:
1035 case DHCPV6_MSG_RELEASE:
1038 case DHCPV6_MSG_DECLINE:
1044 case DHCPV6_STATUS_OK:
1047 case DHCPV6_STATUS_NOADDRSAVAIL:
1048 status = "no addresses available";
1050 case DHCPV6_STATUS_NOBINDING:
1051 status = "no binding";
1053 case DHCPV6_STATUS_NOTONLINK:
1054 status = "not on-link";
1056 case DHCPV6_STATUS_NOPREFIXAVAIL:
1057 status = "no prefix available";
1061 char leasebuf[256] = "";
1064 struct log_ctxt ctxt = {.buf = leasebuf,
1065 .buf_len = sizeof(leasebuf),
1068 dhcpv6_enum_ia_addrs(iface, a, now, dhcpv6_log_ia_addr, &ctxt);
1071 syslog(LOG_WARNING, "DHCPV6 %s %s from %s on %s: %s %s", type, (is_pd) ? "IA_PD" : "IA_NA",
1072 duidbuf, iface->ifname, status, leasebuf);
1075 ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
1076 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end)
1078 time_t now = odhcpd_time();
1079 size_t response_len = 0;
1080 const struct dhcpv6_client_header *hdr = data;
1081 uint8_t *start = (uint8_t*)&hdr[1], *odata;
1082 uint16_t otype, olen;
1083 /* Find and parse client-id and hostname */
1084 bool accept_reconf = false;
1085 uint8_t *clid_data = NULL, clid_len = 0, mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1087 size_t hostname_len = 0;
1088 bool notonlink = false;
1091 dhcpv6_for_each_option(start, end, otype, olen, odata) {
1092 if (otype == DHCPV6_OPT_CLIENTID) {
1096 if (olen == 14 && odata[0] == 0 && odata[1] == 1)
1097 memcpy(mac, &odata[8], sizeof(mac));
1098 else if (olen == 10 && odata[0] == 0 && odata[1] == 3)
1099 memcpy(mac, &odata[4], sizeof(mac));
1102 odhcpd_hexlify(duidbuf, odata, olen);
1103 } else if (otype == DHCPV6_OPT_FQDN && olen >= 2 && olen <= 255) {
1104 uint8_t fqdn_buf[256];
1105 memcpy(fqdn_buf, odata, olen);
1106 fqdn_buf[olen++] = 0;
1108 if (dn_expand(&fqdn_buf[1], &fqdn_buf[olen], &fqdn_buf[1], hostname, sizeof(hostname)) > 0)
1109 hostname_len = strcspn(hostname, ".");
1110 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT)
1111 accept_reconf = true;
1114 if (!clid_data || !clid_len || clid_len > 130)
1117 struct dhcpv6_assignment *first = NULL;
1118 dhcpv6_for_each_option(start, end, otype, olen, odata) {
1119 bool is_pd = (otype == DHCPV6_OPT_IA_PD);
1120 bool is_na = (otype == DHCPV6_OPT_IA_NA);
1121 bool ia_addr_present = false;
1122 if (!is_pd && !is_na)
1125 struct dhcpv6_ia_hdr *ia = (struct dhcpv6_ia_hdr*)&odata[-4];
1126 size_t ia_response_len = 0;
1127 uint8_t reqlen = (is_pd) ? 62 : 128;
1128 uint32_t reqhint = 0;
1130 /* Parse request hint for IA-PD */
1133 uint16_t stype, slen;
1134 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1135 if (stype != DHCPV6_OPT_IA_PREFIX || slen < sizeof(struct dhcpv6_ia_prefix) - 4)
1138 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&sdata[-4];
1141 reqhint = ntohl(p->addr.s6_addr32[1]);
1142 if (reqlen > 32 && reqlen <= 64)
1143 reqhint &= (1U << (64 - reqlen)) - 1;
1151 uint16_t stype, slen;
1152 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1153 if (stype != DHCPV6_OPT_IA_ADDR || slen < sizeof(struct dhcpv6_ia_addr) - 4)
1156 ia_addr_present = true;
1160 /* Find assignment */
1161 struct dhcpv6_assignment *c, *a = NULL;
1162 list_for_each_entry(c, &iface->ia_assignments, head) {
1163 if (((c->clid_len == clid_len && !memcmp(c->clid_data, clid_data, clid_len)) ||
1164 (c->clid_len >= clid_len && !c->clid_data[0] && !c->clid_data[1]
1165 && !memcmp(c->mac, mac, sizeof(mac)))) &&
1166 (!(c->flags & (OAF_BOUND|OAF_TENTATIVE)) || c->iaid == ia->iaid) &&
1167 (INFINITE_VALID(c->valid_until) || now < c->valid_until) &&
1168 ((is_pd && c->length <= 64) || (is_na && c->length == 128))) {
1172 if (a->flags & OAF_BOUND)
1173 apply_lease(iface, a, false);
1175 memcpy(a->clid_data, clid_data, clid_len);
1176 a->clid_len = clid_len;
1184 /* Generic message handling */
1185 uint16_t status = DHCPV6_STATUS_OK;
1186 if (a && a->managed_size < 0)
1189 if (hdr->msg_type == DHCPV6_MSG_SOLICIT ||
1190 hdr->msg_type == DHCPV6_MSG_REQUEST ||
1191 (hdr->msg_type == DHCPV6_MSG_REBIND && !a)) {
1192 bool assigned = !!a;
1194 if (!a && !iface->no_dynamic_dhcp) {
1195 /* Create new binding */
1196 a = calloc(1, sizeof(*a) + clid_len);
1198 a->clid_len = clid_len;
1202 a->assigned = reqhint;
1203 /* Set valid time to current time indicating */
1204 /* assignment is not having infinite lifetime */
1205 a->valid_until = now;
1209 memcpy(a->key, first->key, sizeof(a->key));
1211 odhcpd_urandom(a->key, sizeof(a->key));
1212 memcpy(a->clid_data, clid_data, clid_len);
1215 while (!(assigned = assign_pd(iface, a)) &&
1216 !a->managed_size && ++a->length <= 64);
1218 assigned = assign_na(iface, a);
1220 if (a->managed_size && !assigned)
1225 if (!assigned || iface->addr6_len == 0)
1226 /* Set error status */
1227 status = (is_pd) ? DHCPV6_STATUS_NOPREFIXAVAIL : DHCPV6_STATUS_NOADDRSAVAIL;
1228 else if (accept_reconf && assigned && !first &&
1229 hdr->msg_type != DHCPV6_MSG_REBIND) {
1230 size_t handshake_len = 4;
1232 buf[1] = DHCPV6_OPT_RECONF_ACCEPT;
1236 if (hdr->msg_type == DHCPV6_MSG_REQUEST) {
1237 struct dhcpv6_auth_reconfigure auth = {
1238 htons(DHCPV6_OPT_AUTH),
1239 htons(sizeof(auth) - 4),
1241 {htonl(time(NULL)), htonl(++serial)},
1245 memcpy(auth.key, a->key, sizeof(a->key));
1246 memcpy(buf + handshake_len, &auth, sizeof(auth));
1247 handshake_len += sizeof(auth);
1250 buf += handshake_len;
1251 buflen -= handshake_len;
1252 response_len += handshake_len;
1257 ia_response_len = append_reply(buf, buflen, status, ia, a, iface,
1258 hdr->msg_type == DHCPV6_MSG_REBIND ? false : true);
1260 /* Was only a solicitation: mark binding for removal */
1261 if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
1262 a->flags &= ~OAF_BOUND;
1263 a->flags |= OAF_TENTATIVE;
1265 if (!(a->flags & OAF_STATIC))
1266 /* Keep tentative assignment around for 60 seconds */
1267 a->valid_until = now + 60;
1268 } else if (assigned &&
1269 (hdr->msg_type == DHCPV6_MSG_REQUEST ||
1270 hdr->msg_type == DHCPV6_MSG_REBIND)) {
1271 if (hostname_len > 0) {
1272 a->hostname = realloc(a->hostname, hostname_len + 1);
1274 memcpy(a->hostname, hostname, hostname_len);
1275 a->hostname[hostname_len] = 0;
1278 a->accept_reconf = accept_reconf;
1279 a->flags &= ~OAF_TENTATIVE;
1280 a->flags |= OAF_BOUND;
1281 apply_lease(iface, a, true);
1282 } else if (!assigned && a && a->managed_size == 0) {
1283 /* Cleanup failed assignment */
1284 free_dhcpv6_assignment(a);
1287 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1288 hdr->msg_type == DHCPV6_MSG_RELEASE ||
1289 hdr->msg_type == DHCPV6_MSG_REBIND ||
1290 hdr->msg_type == DHCPV6_MSG_DECLINE) {
1291 if (!a && hdr->msg_type != DHCPV6_MSG_REBIND) {
1292 status = DHCPV6_STATUS_NOBINDING;
1293 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1294 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1295 hdr->msg_type == DHCPV6_MSG_REBIND) {
1296 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1298 a->flags |= OAF_BOUND;
1299 apply_lease(iface, a, true);
1301 } else if (hdr->msg_type == DHCPV6_MSG_RELEASE) {
1302 if (!(a->flags & OAF_STATIC))
1303 a->valid_until = now - 1;
1305 if (a->flags & OAF_BOUND) {
1306 apply_lease(iface, a, false);
1307 a->flags &= ~OAF_BOUND;
1309 } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
1310 a->flags &= ~OAF_BOUND;
1312 if (!(a->flags & OAF_STATIC)) {
1314 a->valid_until = now + 3600; /* Block address for 1h */
1317 } else if (hdr->msg_type == DHCPV6_MSG_CONFIRM && ia_addr_present) {
1318 /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1319 status = DHCPV6_STATUS_NOTONLINK;
1320 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1324 buf += ia_response_len;
1325 buflen -= ia_response_len;
1326 response_len += ia_response_len;
1327 dhcpv6_log(hdr->msg_type, iface, now, duidbuf, is_pd, a, status);
1330 if ((hdr->msg_type == DHCPV6_MSG_RELEASE || hdr->msg_type == DHCPV6_MSG_DECLINE || notonlink) &&
1331 response_len + 6 < buflen) {
1333 buf[1] = DHCPV6_OPT_STATUS;
1337 buf[5] = (notonlink) ? DHCPV6_STATUS_NOTONLINK : DHCPV6_STATUS_OK;
1341 dhcpv6_write_statefile();
1344 return response_len;