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.
19 #include "libubox/md5.h"
20 #include "libubox/usock.h"
34 #include <arpa/inet.h>
35 #include <sys/timerfd.h>
37 #define ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) \
38 ((iface)->managed == RELAYD_MANAGED_NO_AFLAG || (i) == (m) || \
39 (addrs)[(i)].prefix > 64)
41 static void free_dhcpv6_assignment(struct dhcpv6_assignment *c);
42 static void reconf_timer(struct uloop_timeout *event);
43 static struct uloop_timeout reconf_event = {.cb = reconf_timer};
44 static uint32_t serial = 0;
45 static uint8_t statemd5[16];
47 int dhcpv6_ia_init(void)
49 uloop_timeout_set(&reconf_event, 2000);
53 int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
55 if (!enable && iface->ia_assignments.next) {
56 struct dhcpv6_assignment *c;
58 while (!list_empty(&iface->ia_assignments)) {
59 c = list_first_entry(&iface->ia_assignments, struct dhcpv6_assignment, head);
60 free_dhcpv6_assignment(c);
64 if (enable && iface->dhcpv6 == RELAYD_SERVER) {
65 if (!iface->ia_assignments.next)
66 INIT_LIST_HEAD(&iface->ia_assignments);
68 if (list_empty(&iface->ia_assignments)) {
69 struct dhcpv6_assignment *border = calloc(1, sizeof(*border));
71 syslog(LOG_ERR, "Calloc failed for border on interface %s", iface->ifname);
76 list_add(&border->head, &iface->ia_assignments);
79 /* Parse static entries */
81 list_for_each_entry(lease, &leases, head) {
83 size_t duid_len = lease->duid_len ? lease->duid_len : 14;
84 struct dhcpv6_assignment *a = calloc(1, sizeof(*a) + duid_len);
86 syslog(LOG_ERR, "Calloc failed for static lease assignment on interface %s",
91 a->leasetime = lease->dhcpv4_leasetime;
93 a->clid_len = duid_len;
96 a->assigned = lease->hostid;
98 uint32_t i4a = ntohl(lease->ipaddr.s_addr) & 0xff;
99 a->assigned = ((i4a / 100) << 8) | (((i4a % 100) / 10) << 4) | (i4a % 10);
102 odhcpd_urandom(a->key, sizeof(a->key));
103 memcpy(a->clid_data, lease->duid, lease->duid_len);
104 memcpy(a->mac, lease->mac.ether_addr_octet, sizeof(a->mac));
105 /* Static assignment */
106 a->flags |= OAF_STATIC;
110 /* Assign to all interfaces */
111 struct dhcpv6_assignment *c;
112 list_for_each_entry(c, &iface->ia_assignments, head) {
113 if (c->length != 128 || c->assigned > a->assigned) {
114 list_add_tail(&a->head, &c->head);
116 } else if (c->assigned == a->assigned)
117 /* Already an assignment with that number */
122 if (lease->hostname[0]) {
124 a->hostname = strdup(lease->hostname);
127 free_dhcpv6_assignment(a);
133 static void free_dhcpv6_assignment(struct dhcpv6_assignment *c)
135 if (c->managed_sock.fd.registered) {
136 ustream_free(&c->managed_sock.stream);
137 close(c->managed_sock.fd.fd);
148 static inline bool valid_prefix_length(const struct dhcpv6_assignment *a, const uint8_t prefix_length)
150 return (a->managed_size || a->length > prefix_length);
153 static inline bool valid_addr(const struct odhcpd_ipaddr *addr, time_t now)
155 return (addr->prefix <= 96 && addr->preferred > (uint32_t)now);
158 static size_t elect_addr(const struct odhcpd_ipaddr *addrs, const size_t addrlen)
162 for (i = 0, m = 0; i < addrlen; ++i) {
163 if (addrs[i].preferred > addrs[m].preferred ||
164 (addrs[i].preferred == addrs[m].preferred &&
165 memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
172 static int send_reconf(struct interface *iface, struct dhcpv6_assignment *assign)
175 struct dhcpv6_client_header hdr;
179 uint16_t hardware_type;
184 struct dhcpv6_auth_reconfigure auth;
187 uint8_t clid_data[128];
188 } __attribute__((packed)) reconf_msg = {
189 .hdr = {DHCPV6_MSG_RECONFIGURE, {0, 0, 0}},
190 .srvid_type = htons(DHCPV6_OPT_SERVERID),
191 .srvid_len = htons(10),
192 .duid_type = htons(3),
193 .hardware_type = htons(1),
194 .msg_type = htons(DHCPV6_OPT_RECONF_MSG),
196 .msg_id = DHCPV6_MSG_RENEW,
197 .auth = {htons(DHCPV6_OPT_AUTH),
198 htons(sizeof(reconf_msg.auth) - 4), 3, 1, 0,
199 {htonl(time(NULL)), htonl(++serial)}, 2, {0}},
200 .clid_type = htons(DHCPV6_OPT_CLIENTID),
201 .clid_len = htons(assign->clid_len),
205 odhcpd_get_mac(iface, reconf_msg.mac);
206 memcpy(reconf_msg.clid_data, assign->clid_data, assign->clid_len);
207 struct iovec iov = {&reconf_msg, sizeof(reconf_msg) - 128 + assign->clid_len};
210 uint8_t secretbytes[64];
211 memset(secretbytes, 0, sizeof(secretbytes));
212 memcpy(secretbytes, assign->key, sizeof(assign->key));
214 for (size_t i = 0; i < sizeof(secretbytes); ++i)
215 secretbytes[i] ^= 0x36;
218 md5_hash(secretbytes, sizeof(secretbytes), &md5);
219 md5_hash(iov.iov_base, iov.iov_len, &md5);
220 md5_end(reconf_msg.auth.key, &md5);
222 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
223 secretbytes[i] ^= 0x36;
224 secretbytes[i] ^= 0x5c;
228 md5_hash(secretbytes, sizeof(secretbytes), &md5);
229 md5_hash(reconf_msg.auth.key, 16, &md5);
230 md5_end(reconf_msg.auth.key, &md5);
232 return odhcpd_send(iface->dhcpv6_event.uloop.fd, &assign->peer, &iov, 1, iface);
235 void dhcpv6_write_statefile(void)
240 if (config.dhcp_statefile) {
241 time_t now = odhcpd_time(), wall_time = time(NULL);
242 int fd = open(config.dhcp_statefile, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
246 ret = lockf(fd, F_LOCK, 0);
251 if (ftruncate(fd, 0) < 0) {}
253 FILE *fp = fdopen(fd, "w");
259 struct interface *iface;
260 list_for_each_entry(iface, &interfaces, head) {
261 if (iface->dhcpv6 != RELAYD_SERVER && iface->dhcpv4 != RELAYD_SERVER)
264 if (iface->dhcpv6 == RELAYD_SERVER && iface->ia_assignments.next) {
265 struct dhcpv6_assignment *c;
266 list_for_each_entry(c, &iface->ia_assignments, head) {
267 if (!(c->flags & OAF_BOUND) || c->managed_size < 0)
270 char ipbuf[INET6_ADDRSTRLEN];
273 odhcpd_hexlify(duidbuf, c->clid_data, c->clid_len);
275 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
276 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s %x %s %ld %x %u ",
277 iface->ifname, duidbuf, ntohl(c->iaid),
278 (c->hostname ? c->hostname : "-"),
279 (c->valid_until > now ?
280 (c->valid_until - now + wall_time) :
281 (INFINITE_VALID(c->valid_until) ? -1 : 0)),
282 c->assigned, (unsigned)c->length);
284 struct in6_addr addr;
285 struct odhcpd_ipaddr *addrs = (c->managed) ? c->managed : iface->ia_addr;
286 size_t addrlen = (c->managed) ? (size_t)c->managed_size : iface->ia_addr_len;
287 size_t m = elect_addr(addrs, addrlen);
289 for (size_t i = 0; i < addrlen; ++i) {
290 if (!valid_addr(&addrs[i], now) ||
291 (!INFINITE_VALID(c->valid_until) && c->valid_until <= now))
294 addr = addrs[i].addr;
295 if (c->length == 128) {
296 if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs))
299 addr.s6_addr32[3] = htonl(c->assigned);
302 if (!valid_prefix_length(c, addrs[i].prefix))
305 addr.s6_addr32[1] |= htonl(c->assigned);
308 inet_ntop(AF_INET6, &addr, ipbuf, sizeof(ipbuf) - 1);
310 if (c->length == 128 && c->hostname) {
314 if (dn_expand(iface->search, iface->search + iface->search_len,
315 iface->search, b, sizeof(b)) > 0)
316 fprintf(fp, "\t%s.%s", c->hostname, b);
318 fprintf(fp, "\t%s\n", c->hostname);
319 md5_hash(ipbuf, strlen(ipbuf), &md5);
320 md5_hash(c->hostname, strlen(c->hostname), &md5);
323 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/%d ", ipbuf,
324 (c->managed_size) ? addrs[i].prefix : c->length);
326 leasebuf[l - 1] = '\n';
327 fwrite(leasebuf, 1, l, fp);
331 if (iface->dhcpv4 == RELAYD_SERVER && iface->dhcpv4_assignments.next) {
332 struct dhcpv4_assignment *c;
333 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
334 if (!(c->flags & OAF_BOUND))
337 char ipbuf[INET6_ADDRSTRLEN];
340 odhcpd_hexlify(duidbuf, c->hwaddr, sizeof(c->hwaddr));
342 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
343 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s ipv4 %s %ld %x 32 ",
344 iface->ifname, duidbuf,
345 (c->hostname ? c->hostname : "-"),
346 (c->valid_until > now ?
347 (c->valid_until - now + wall_time) :
348 (INFINITE_VALID(c->valid_until) ? -1 : 0)),
351 struct in_addr addr = {htonl(c->addr)};
352 inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf) - 1);
358 if (dn_expand(iface->search, iface->search + iface->search_len,
359 iface->search, b, sizeof(b)) > 0)
360 fprintf(fp, "\t%s.%s", c->hostname, b);
362 fprintf(fp, "\t%s\n", c->hostname);
363 md5_hash(ipbuf, strlen(ipbuf), &md5);
364 md5_hash(c->hostname, strlen(c->hostname), &md5);
367 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/32 ", ipbuf);
368 leasebuf[l - 1] = '\n';
369 fwrite(leasebuf, 1, l, fp);
378 md5_end(newmd5, &md5);
380 if (config.dhcp_cb && memcmp(newmd5, statemd5, sizeof(newmd5))) {
381 memcpy(statemd5, newmd5, sizeof(statemd5));
382 char *argv[2] = {config.dhcp_cb, NULL};
384 execv(argv[0], argv);
391 static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bool add)
393 if (a->length > 64 || a->managed_size < 0)
396 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
397 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
399 for (size_t i = 0; i < addrlen; ++i) {
400 struct in6_addr prefix = addrs[i].addr;
401 prefix.s6_addr32[1] |= htonl(a->assigned);
402 odhcpd_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length,
403 iface, &a->peer.sin6_addr, 1024, add);
407 /* More data was received from TCP connection */
408 static void managed_handle_pd_data(struct ustream *s, _unused int bytes_new)
410 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
411 time_t now = odhcpd_time();
412 bool first = c->managed_size < 0;
416 char *data = ustream_get_read_buf(s, &pending);
417 char *end = memmem(data, pending, "\n\n", 2);
426 if (c->accept_reconf)
430 for (char *line = strtok_r(data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr)) {
431 c->managed = realloc(c->managed, (c->managed_size + 1) * sizeof(*c->managed));
432 struct odhcpd_ipaddr *n = &c->managed[c->managed_size];
434 char *saveptr2, *x = strtok_r(line, "/", &saveptr2);
435 if (!x || inet_pton(AF_INET6, x, &n->addr) < 1)
438 x = strtok_r(NULL, ",", &saveptr2);
439 if (sscanf(x, "%hhu", &n->prefix) < 1)
442 x = strtok_r(NULL, ",", &saveptr2);
443 if (sscanf(x, "%u", &n->preferred) < 1)
446 x = strtok_r(NULL, ",", &saveptr2);
447 if (sscanf(x, "%u", &n->valid) < 1)
450 if (n->preferred > n->valid)
453 if (UINT32_MAX - now < n->preferred)
454 n->preferred = UINT32_MAX;
458 if (UINT32_MAX - now < n->valid)
459 n->valid = UINT32_MAX;
468 ustream_consume(s, end - data);
471 if (first && c->managed_size == 0)
472 free_dhcpv6_assignment(c);
473 else if (first && !(c->flags & OAF_STATIC))
474 c->valid_until = now + 150;
478 /* TCP transmission has ended, either because of success or timeout or other error */
479 static void managed_handle_pd_done(struct ustream *s)
481 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
483 if (!(c->flags & OAF_STATIC))
484 c->valid_until = odhcpd_time() + 15;
488 if (c->accept_reconf)
492 static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
494 struct dhcpv6_assignment *c;
496 if (iface->dhcpv6_pd_manager[0]) {
497 int fd = usock(USOCK_UNIX | USOCK_TCP, iface->dhcpv6_pd_manager, NULL);
500 odhcpd_hexlify(iaidbuf, assign->clid_data, assign->clid_len);
502 assign->managed_sock.stream.notify_read = managed_handle_pd_data;
503 assign->managed_sock.stream.notify_state = managed_handle_pd_done;
504 ustream_fd_init(&assign->managed_sock, fd);
505 ustream_printf(&assign->managed_sock.stream, "%s,%x\n::/%d,0,0\n\n",
506 iaidbuf, assign->iaid, assign->length);
507 ustream_write_pending(&assign->managed_sock.stream);
508 assign->managed_size = -1;
510 if (!(assign->flags & OAF_STATIC))
511 assign->valid_until = odhcpd_time() + 15;
513 list_add(&assign->head, &iface->ia_assignments);
515 /* Wait initial period of up to 250ms for immediate assignment */
516 struct pollfd pfd = { .fd = fd, .events = POLLIN };
518 managed_handle_pd_data(&assign->managed_sock.stream, 0);
520 if (fcntl(fd, F_GETFL) >= 0 && assign->managed_size > 0)
525 } else if (iface->ia_addr_len < 1)
528 /* Try honoring the hint first */
529 uint32_t current = 1, asize = (1 << (64 - assign->length)) - 1;
530 if (assign->assigned) {
531 list_for_each_entry(c, &iface->ia_assignments, head) {
532 if (c->length == 128 || c->length == 0)
535 if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
536 list_add_tail(&assign->head, &c->head);
538 if (assign->flags & OAF_BOUND)
539 apply_lease(iface, assign, true);
544 if (c->assigned != 0)
545 current = (c->assigned + (1 << (64 - c->length)));
549 /* Fallback to a variable assignment */
551 list_for_each_entry(c, &iface->ia_assignments, head) {
552 if (c->length == 128 || c->length == 0)
555 current = (current + asize) & (~asize);
556 if (current + asize < c->assigned) {
557 assign->assigned = current;
558 list_add_tail(&assign->head, &c->head);
560 if (assign->flags & OAF_BOUND)
561 apply_lease(iface, assign, true);
566 if (c->assigned != 0)
567 current = (c->assigned + (1 << (64 - c->length)));
573 static bool assign_na(struct interface *iface, struct dhcpv6_assignment *assign)
575 /* Seed RNG with checksum of DUID */
577 for (size_t i = 0; i < assign->clid_len; ++i)
578 seed += assign->clid_data[i];
581 /* Try to assign up to 100x */
582 for (size_t i = 0; i < 100; ++i) {
584 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
586 struct dhcpv6_assignment *c;
587 list_for_each_entry(c, &iface->ia_assignments, head) {
591 if (c->assigned > try || c->length != 128) {
592 assign->assigned = try;
593 list_add_tail(&assign->head, &c->head);
595 } else if (c->assigned == try)
603 void dhcpv6_ia_preupdate(struct interface *iface)
605 if (iface->dhcpv6 != RELAYD_SERVER)
608 struct dhcpv6_assignment *c, *border = list_last_entry(
609 &iface->ia_assignments, struct dhcpv6_assignment, head);
611 list_for_each_entry(c, &iface->ia_assignments, head)
612 if (c != border && !iface->managed && (c->flags & OAF_BOUND))
613 apply_lease(iface, c, false);
616 void dhcpv6_ia_postupdate(struct interface *iface, time_t now)
618 if (iface->dhcpv6 != RELAYD_SERVER)
622 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
623 if (iface->ia_addr[i].preferred > (uint32_t)now &&
624 iface->ia_addr[i].prefix < 64 &&
625 iface->ia_addr[i].prefix > minprefix)
626 minprefix = iface->ia_addr[i].prefix;
629 struct dhcpv6_assignment *border = list_last_entry(
630 &iface->ia_assignments, struct dhcpv6_assignment, head);
632 if (minprefix > 32 && minprefix <= 64)
633 border->assigned = 1U << (64 - minprefix);
635 border->assigned = 0;
637 struct list_head reassign = LIST_HEAD_INIT(reassign);
638 struct dhcpv6_assignment *c, *d;
639 list_for_each_entry_safe(c, d, &iface->ia_assignments, head) {
640 if (c->clid_len == 0 || (!INFINITE_VALID(c->valid_until) && c->valid_until < now) ||
644 if (c->length < 128 && c->assigned >= border->assigned && c != border)
645 list_move(&c->head, &reassign);
646 else if (c != border && (c->flags & OAF_BOUND))
647 apply_lease(iface, c, true);
649 if (c->accept_reconf && c->reconf_cnt == 0) {
651 c->reconf_sent = now;
652 send_reconf(iface, c);
654 /* Leave all other assignments of that client alone */
655 struct dhcpv6_assignment *a;
656 list_for_each_entry(a, &iface->ia_assignments, head)
657 if (a != c && a->clid_len == c->clid_len &&
658 !memcmp(a->clid_data, c->clid_data, a->clid_len))
659 c->reconf_cnt = INT_MAX;
663 while (!list_empty(&reassign)) {
664 c = list_first_entry(&reassign, struct dhcpv6_assignment, head);
666 if (!assign_pd(iface, c)) {
668 list_add(&c->head, &iface->ia_assignments);
672 dhcpv6_write_statefile();
675 static void reconf_timer(struct uloop_timeout *event)
677 time_t now = odhcpd_time();
678 struct interface *iface;
679 list_for_each_entry(iface, &interfaces, head) {
680 if (iface->dhcpv6 != RELAYD_SERVER || iface->ia_assignments.next == NULL)
683 struct dhcpv6_assignment *a, *n;
684 list_for_each_entry_safe(a, n, &iface->ia_assignments, head) {
685 if (!INFINITE_VALID(a->valid_until) && a->valid_until < now) {
686 if ((a->length < 128 && a->clid_len > 0) ||
687 (a->length == 128 && a->clid_len == 0))
688 free_dhcpv6_assignment(a);
690 } else if (a->reconf_cnt > 0 && a->reconf_cnt < 8 &&
691 now > a->reconf_sent + (1 << a->reconf_cnt)) {
693 a->reconf_sent = now;
694 send_reconf(iface, a);
698 uloop_timeout_set(event, 2000);
701 static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
702 const struct dhcpv6_ia_hdr *ia, struct dhcpv6_assignment *a,
703 struct interface *iface, bool request)
705 if (buflen < sizeof(*ia) + sizeof(struct dhcpv6_ia_prefix))
708 struct dhcpv6_ia_hdr out = {ia->type, 0, ia->iaid, 0, 0};
709 size_t datalen = sizeof(out);
710 time_t now = odhcpd_time();
713 struct __attribute__((packed)) {
717 } stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
720 memcpy(buf + datalen, &stat, sizeof(stat));
721 datalen += sizeof(stat);
726 leasetime = a->leasetime;
728 leasetime = iface->dhcpv4_leasetime;
730 uint32_t pref = leasetime;
731 uint32_t valid = leasetime;
733 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
734 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
735 size_t m = elect_addr(addrs, addrlen);
737 for (size_t i = 0; i < addrlen; ++i) {
738 uint32_t prefix_pref = addrs[i].preferred;
739 uint32_t prefix_valid = addrs[i].valid;
741 if (!valid_addr(&addrs[i], now))
744 if (prefix_pref != UINT32_MAX)
747 if (prefix_valid != UINT32_MAX)
750 if (a->length < 128) {
751 struct dhcpv6_ia_prefix p = {
752 .type = htons(DHCPV6_OPT_IA_PREFIX),
753 .len = htons(sizeof(p) - 4),
754 .preferred = htonl(prefix_pref),
755 .valid = htonl(prefix_valid),
756 .prefix = (a->managed_size) ? addrs[i].prefix : a->length,
757 .addr = addrs[i].addr
759 p.addr.s6_addr32[1] |= htonl(a->assigned);
761 size_t entrlen = sizeof(p) - 4;
763 if (datalen + entrlen + 4 > buflen ||
764 (a->assigned == 0 && a->managed_size == 0) ||
765 !valid_prefix_length(a, addrs[i].prefix))
768 memcpy(buf + datalen, &p, sizeof(p));
769 datalen += entrlen + 4;
771 struct dhcpv6_ia_addr n = {
772 .type = htons(DHCPV6_OPT_IA_ADDR),
773 .len = htons(sizeof(n) - 4),
774 .addr = addrs[i].addr,
775 .preferred = htonl(prefix_pref),
776 .valid = htonl(prefix_valid)
778 n.addr.s6_addr32[3] = htonl(a->assigned);
779 size_t entrlen = sizeof(n) - 4;
781 if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) ||
783 datalen + entrlen + 4 > buflen)
786 memcpy(buf + datalen, &n, sizeof(n));
787 datalen += entrlen + 4;
790 /* Calculate T1 / T2 based on non-deprecated addresses */
791 if (prefix_pref > 0) {
792 if (prefix_pref < pref)
795 if (prefix_valid < valid)
796 valid = prefix_valid;
800 if (!INFINITE_VALID(a->valid_until))
801 /* UINT32_MAX is considered as infinite leasetime */
802 a->valid_until = (valid == UINT32_MAX) ? 0 : valid + now;
804 out.t1 = htonl((pref == UINT32_MAX) ? pref : pref * 5 / 10);
805 out.t2 = htonl((pref == UINT32_MAX) ? pref : pref * 8 / 10);
815 uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + 4;
816 uint16_t otype, olen;
817 dhcpv6_for_each_option((uint8_t*)&ia[1], end, otype, olen, odata) {
818 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&odata[-4];
819 struct dhcpv6_ia_addr *n = (struct dhcpv6_ia_addr*)&odata[-4];
820 if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*p) - 4) &&
821 (otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*n) - 4))
826 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
827 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
829 for (size_t i = 0; i < addrlen; ++i) {
830 if (!valid_addr(&addrs[i], now))
833 struct in6_addr addr = addrs[i].addr;
834 if (ia->type == htons(DHCPV6_OPT_IA_PD)) {
835 addr.s6_addr32[1] |= htonl(a->assigned);
837 if (!memcmp(&p->addr, &addr, sizeof(addr)) &&
838 p->prefix == ((a->managed) ? addrs[i].prefix : a->length))
841 addr.s6_addr32[3] = htonl(a->assigned);
843 if (!memcmp(&n->addr, &addr, sizeof(addr)))
850 if (otype == DHCPV6_OPT_IA_PREFIX) {
851 struct dhcpv6_ia_prefix inv = {
852 .type = htons(DHCPV6_OPT_IA_PREFIX),
853 .len = htons(sizeof(inv) - 4),
860 if (datalen + sizeof(inv) > buflen)
863 memcpy(buf + datalen, &inv, sizeof(inv));
864 datalen += sizeof(inv);
866 struct dhcpv6_ia_addr inv = {
867 .type = htons(DHCPV6_OPT_IA_ADDR),
868 .len = htons(sizeof(inv) - 4),
874 if (datalen + sizeof(inv) > buflen)
877 memcpy(buf + datalen, &inv, sizeof(inv));
878 datalen += sizeof(inv);
885 out.len = htons(datalen - 4);
886 memcpy(buf, &out, sizeof(out));
890 static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now,
891 const char *duidbuf, bool is_pd, struct dhcpv6_assignment *a, int code)
893 const char *type = "UNKNOWN";
894 const char *status = "UNKNOWN";
896 if (msgtype == DHCPV6_MSG_RENEW)
900 case DHCPV6_MSG_SOLICIT:
903 case DHCPV6_MSG_REQUEST:
906 case DHCPV6_MSG_CONFIRM:
909 case DHCPV6_MSG_RENEW:
912 case DHCPV6_MSG_REBIND:
915 case DHCPV6_MSG_RELEASE:
918 case DHCPV6_MSG_DECLINE:
924 case DHCPV6_STATUS_OK:
927 case DHCPV6_STATUS_NOADDRSAVAIL:
928 status = "no addresses available";
930 case DHCPV6_STATUS_NOBINDING:
931 status = "no binding";
933 case DHCPV6_STATUS_NOTONLINK:
934 status = "not on-link";
936 case DHCPV6_STATUS_NOPREFIXAVAIL:
937 status = "no prefix available";
941 char leasebuf[256] = "";
944 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
945 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
947 size_t m = elect_addr(addrs, addrlen);
948 char addrbuf[INET6_ADDRSTRLEN];
950 for (size_t i = 0; i < addrlen; ++i) {
951 if (!valid_addr(&addrs[i], now))
954 struct in6_addr addr = addrs[i].addr;
955 int prefix = a->managed ? addrs[i].prefix : a->length;
957 if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) ||
961 addr.s6_addr32[3] = htonl(a->assigned);
964 if (!valid_prefix_length(a, addrs[i].prefix))
967 addr.s6_addr32[1] |= htonl(a->assigned);
970 inet_ntop(AF_INET6, &addr, addrbuf, sizeof(addrbuf));
971 lbsize += snprintf(leasebuf + lbsize, sizeof(leasebuf) - lbsize, "%s/%d ", addrbuf, prefix);
975 syslog(LOG_WARNING, "DHCPV6 %s %s from %s on %s: %s %s", type, (is_pd) ? "IA_PD" : "IA_NA",
976 duidbuf, iface->ifname, status, leasebuf);
979 ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
980 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end)
982 time_t now = odhcpd_time();
983 size_t response_len = 0;
984 const struct dhcpv6_client_header *hdr = data;
985 uint8_t *start = (uint8_t*)&hdr[1], *odata;
986 uint16_t otype, olen;
987 /* Find and parse client-id and hostname */
988 bool accept_reconf = false;
989 uint8_t *clid_data = NULL, clid_len = 0, mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
991 size_t hostname_len = 0;
992 bool notonlink = false;
995 dhcpv6_for_each_option(start, end, otype, olen, odata) {
996 if (otype == DHCPV6_OPT_CLIENTID) {
1000 if (olen == 14 && odata[0] == 0 && odata[1] == 1)
1001 memcpy(mac, &odata[8], sizeof(mac));
1002 else if (olen == 10 && odata[0] == 0 && odata[1] == 3)
1003 memcpy(mac, &odata[4], sizeof(mac));
1006 odhcpd_hexlify(duidbuf, odata, olen);
1007 } else if (otype == DHCPV6_OPT_FQDN && olen >= 2 && olen <= 255) {
1008 uint8_t fqdn_buf[256];
1009 memcpy(fqdn_buf, odata, olen);
1010 fqdn_buf[olen++] = 0;
1012 if (dn_expand(&fqdn_buf[1], &fqdn_buf[olen], &fqdn_buf[1], hostname, sizeof(hostname)) > 0)
1013 hostname_len = strcspn(hostname, ".");
1014 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT)
1015 accept_reconf = true;
1018 if (!clid_data || !clid_len || clid_len > 130)
1021 struct dhcpv6_assignment *first = NULL;
1022 dhcpv6_for_each_option(start, end, otype, olen, odata) {
1023 bool is_pd = (otype == DHCPV6_OPT_IA_PD);
1024 bool is_na = (otype == DHCPV6_OPT_IA_NA);
1025 bool ia_addr_present = false;
1026 if (!is_pd && !is_na)
1029 struct dhcpv6_ia_hdr *ia = (struct dhcpv6_ia_hdr*)&odata[-4];
1030 size_t ia_response_len = 0;
1031 uint8_t reqlen = (is_pd) ? 62 : 128;
1032 uint32_t reqhint = 0;
1034 /* Parse request hint for IA-PD */
1037 uint16_t stype, slen;
1038 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1039 if (stype != DHCPV6_OPT_IA_PREFIX || slen < sizeof(struct dhcpv6_ia_prefix) - 4)
1042 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&sdata[-4];
1045 reqhint = ntohl(p->addr.s6_addr32[1]);
1046 if (reqlen > 32 && reqlen <= 64)
1047 reqhint &= (1U << (64 - reqlen)) - 1;
1055 uint16_t stype, slen;
1056 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1057 if (stype != DHCPV6_OPT_IA_ADDR || slen < sizeof(struct dhcpv6_ia_addr) - 4)
1060 ia_addr_present = true;
1064 /* Find assignment */
1065 struct dhcpv6_assignment *c, *a = NULL;
1066 list_for_each_entry(c, &iface->ia_assignments, head) {
1067 if (((c->clid_len == clid_len && !memcmp(c->clid_data, clid_data, clid_len)) ||
1068 (c->clid_len >= clid_len && !c->clid_data[0] && !c->clid_data[1]
1069 && !memcmp(c->mac, mac, sizeof(mac)))) &&
1070 (c->iaid == ia->iaid || INFINITE_VALID(c->valid_until) || now < c->valid_until) &&
1071 ((is_pd && c->length <= 64) || (is_na && c->length == 128))) {
1075 if (a->flags & OAF_BOUND)
1076 apply_lease(iface, a, false);
1078 memcpy(a->clid_data, clid_data, clid_len);
1079 a->clid_len = clid_len;
1088 /* Generic message handling */
1089 uint16_t status = DHCPV6_STATUS_OK;
1090 if (a && a->managed_size < 0)
1093 if (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_REQUEST) {
1094 bool assigned = !!a;
1096 if (!a && !iface->no_dynamic_dhcp) {
1097 /* Create new binding */
1098 a = calloc(1, sizeof(*a) + clid_len);
1100 a->clid_len = clid_len;
1104 a->assigned = reqhint;
1105 /* Set valid time to current time indicating */
1106 /* assignment is not having infinite lifetime */
1107 a->valid_until = now;
1110 memcpy(a->key, first->key, sizeof(a->key));
1112 odhcpd_urandom(a->key, sizeof(a->key));
1113 memcpy(a->clid_data, clid_data, clid_len);
1116 while (!(assigned = assign_pd(iface, a)) &&
1117 !a->managed_size && ++a->length <= 64);
1119 assigned = assign_na(iface, a);
1121 if (a->managed_size && !assigned)
1126 if (!assigned || iface->ia_addr_len == 0)
1127 /* Set error status */
1128 status = (is_pd) ? DHCPV6_STATUS_NOPREFIXAVAIL : DHCPV6_STATUS_NOADDRSAVAIL;
1129 else if (assigned && !first) {
1130 size_t handshake_len = 4;
1132 buf[1] = DHCPV6_OPT_RECONF_ACCEPT;
1136 if (hdr->msg_type == DHCPV6_MSG_REQUEST) {
1137 struct dhcpv6_auth_reconfigure auth = {
1138 htons(DHCPV6_OPT_AUTH),
1139 htons(sizeof(auth) - 4),
1141 {htonl(time(NULL)), htonl(++serial)},
1145 memcpy(auth.key, a->key, sizeof(a->key));
1146 memcpy(buf + handshake_len, &auth, sizeof(auth));
1147 handshake_len += sizeof(auth);
1150 buf += handshake_len;
1151 buflen -= handshake_len;
1152 response_len += handshake_len;
1157 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1159 /* Was only a solicitation: mark binding for removal */
1160 if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
1161 a->flags &= ~OAF_BOUND;
1163 if (!(a->flags & OAF_STATIC))
1164 a->valid_until = now;
1165 } else if (assigned && hdr->msg_type == DHCPV6_MSG_REQUEST) {
1166 if (hostname_len > 0) {
1167 a->hostname = realloc(a->hostname, hostname_len + 1);
1169 memcpy(a->hostname, hostname, hostname_len);
1170 a->hostname[hostname_len] = 0;
1173 a->accept_reconf = accept_reconf;
1174 a->flags |= OAF_BOUND;
1175 apply_lease(iface, a, true);
1176 } else if (!assigned && a && a->managed_size == 0) {
1177 /* Cleanup failed assignment */
1178 free_dhcpv6_assignment(a);
1181 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1182 hdr->msg_type == DHCPV6_MSG_RELEASE ||
1183 hdr->msg_type == DHCPV6_MSG_REBIND ||
1184 hdr->msg_type == DHCPV6_MSG_DECLINE) {
1185 if (!a && hdr->msg_type != DHCPV6_MSG_REBIND) {
1186 status = DHCPV6_STATUS_NOBINDING;
1187 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1188 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1189 hdr->msg_type == DHCPV6_MSG_REBIND) {
1190 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1192 a->flags |= OAF_BOUND;
1193 apply_lease(iface, a, true);
1195 } else if (hdr->msg_type == DHCPV6_MSG_RELEASE) {
1196 if (!(a->flags & OAF_STATIC))
1197 a->valid_until = now - 1;
1199 if (a->flags & OAF_BOUND) {
1200 apply_lease(iface, a, false);
1201 a->flags &= ~OAF_BOUND;
1203 } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
1204 a->flags &= ~OAF_BOUND;
1206 if (!(a->flags & OAF_STATIC)) {
1208 a->valid_until = now + 3600; /* Block address for 1h */
1211 } else if (hdr->msg_type == DHCPV6_MSG_CONFIRM && ia_addr_present) {
1212 /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1213 status = DHCPV6_STATUS_NOTONLINK;
1214 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1218 buf += ia_response_len;
1219 buflen -= ia_response_len;
1220 response_len += ia_response_len;
1221 dhcpv6_log(hdr->msg_type, iface, now, duidbuf, is_pd, a, status);
1224 if ((hdr->msg_type == DHCPV6_MSG_RELEASE || hdr->msg_type == DHCPV6_MSG_DECLINE || notonlink) &&
1225 response_len + 6 < buflen) {
1227 buf[1] = DHCPV6_OPT_STATUS;
1231 buf[5] = (notonlink) ? DHCPV6_STATUS_NOTONLINK : DHCPV6_STATUS_OK;
1235 dhcpv6_write_statefile();
1238 return response_len;