2 * Copyright (C) 2013 Steven Barth <steven@midlink.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
18 #include "libubox/md5.h"
19 #include "libubox/usock.h"
33 #include <arpa/inet.h>
34 #include <sys/timerfd.h>
37 static void reconf_timer(struct uloop_timeout *event);
38 static struct uloop_timeout reconf_event = {.cb = reconf_timer};
39 static uint32_t serial = 0;
40 static uint8_t statemd5[16];
43 int dhcpv6_ia_init(void)
45 uloop_timeout_set(&reconf_event, 2000);
50 void free_dhcpv6_assignment(struct dhcpv6_assignment *c)
52 if (c->managed_sock.fd.registered) {
53 ustream_free(&c->managed_sock.stream);
54 close(c->managed_sock.fd.fd);
66 int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
68 if (!enable && iface->ia_assignments.next) {
69 struct dhcpv6_assignment *c;
70 while (!list_empty(&iface->ia_assignments)) {
71 c = list_first_entry(&iface->ia_assignments, struct dhcpv6_assignment, head);
72 free_dhcpv6_assignment(c);
76 if (enable && iface->dhcpv6 == RELAYD_SERVER) {
77 if (!iface->ia_assignments.next)
78 INIT_LIST_HEAD(&iface->ia_assignments);
80 if (list_empty(&iface->ia_assignments)) {
81 struct dhcpv6_assignment *border = calloc(1, sizeof(*border));
83 syslog(LOG_ERR, "Calloc failed for border on interface %s", iface->ifname);
88 list_add(&border->head, &iface->ia_assignments);
91 // Parse static entries
93 list_for_each_entry(lease, &leases, head) {
95 struct dhcpv6_assignment *a = calloc(1, sizeof(*a) + lease->duid_len);
97 syslog(LOG_ERR, "Calloc failed for static lease assignment on interface %s",
102 a->clid_len = lease->duid_len;
105 a->assigned = lease->hostid;
107 uint32_t i4a = ntohl(lease->ipaddr.s_addr) & 0xff;
108 a->assigned = ((i4a / 100) << 8) | (((i4a % 100) / 10) << 4) | (i4a % 10);
110 odhcpd_urandom(a->key, sizeof(a->key));
111 memcpy(a->clid_data, lease->duid, a->clid_len);
112 memcpy(a->mac, lease->mac.ether_addr_octet, sizeof(a->mac));
114 // Assign to all interfaces
115 struct dhcpv6_assignment *c;
116 list_for_each_entry(c, &iface->ia_assignments, head) {
117 if (c->length != 128 || c->assigned > a->assigned) {
118 list_add_tail(&a->head, &c->head);
120 } else if (c->assigned == a->assigned) {
121 // Already an assignment with that number
127 if (lease->hostname[0]) {
129 a->hostname = strdup(lease->hostname);
141 static int send_reconf(struct interface *iface, struct dhcpv6_assignment *assign)
144 struct dhcpv6_client_header hdr;
148 uint16_t hardware_type;
153 struct dhcpv6_auth_reconfigure auth;
156 uint8_t clid_data[128];
157 } __attribute__((packed)) reconf_msg = {
158 .hdr = {DHCPV6_MSG_RECONFIGURE, {0, 0, 0}},
159 .srvid_type = htons(DHCPV6_OPT_SERVERID),
160 .srvid_len = htons(10),
161 .duid_type = htons(3),
162 .hardware_type = htons(1),
163 .msg_type = htons(DHCPV6_OPT_RECONF_MSG),
165 .msg_id = DHCPV6_MSG_RENEW,
166 .auth = {htons(DHCPV6_OPT_AUTH),
167 htons(sizeof(reconf_msg.auth) - 4), 3, 1, 0,
168 {htonl(time(NULL)), htonl(++serial)}, 2, {0}},
169 .clid_type = htons(DHCPV6_OPT_CLIENTID),
170 .clid_len = htons(assign->clid_len),
174 odhcpd_get_mac(iface, reconf_msg.mac);
175 memcpy(reconf_msg.clid_data, assign->clid_data, assign->clid_len);
176 struct iovec iov = {&reconf_msg, sizeof(reconf_msg) - 128 + assign->clid_len};
179 uint8_t secretbytes[64];
180 memset(secretbytes, 0, sizeof(secretbytes));
181 memcpy(secretbytes, assign->key, sizeof(assign->key));
183 for (size_t i = 0; i < sizeof(secretbytes); ++i)
184 secretbytes[i] ^= 0x36;
187 md5_hash(secretbytes, sizeof(secretbytes), &md5);
188 md5_hash(iov.iov_base, iov.iov_len, &md5);
189 md5_end(reconf_msg.auth.key, &md5);
191 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
192 secretbytes[i] ^= 0x36;
193 secretbytes[i] ^= 0x5c;
197 md5_hash(secretbytes, sizeof(secretbytes), &md5);
198 md5_hash(reconf_msg.auth.key, 16, &md5);
199 md5_end(reconf_msg.auth.key, &md5);
201 return odhcpd_send(iface->dhcpv6_event.uloop.fd, &assign->peer, &iov, 1, iface);
205 void dhcpv6_write_statefile(void)
210 if (config.dhcp_statefile) {
211 time_t now = odhcpd_time(), wall_time = time(NULL);
212 int fd = open(config.dhcp_statefile, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
216 lockf(fd, F_LOCK, 0);
217 if (ftruncate(fd, 0) < 0) {}
219 FILE *fp = fdopen(fd, "w");
225 struct interface *iface;
226 list_for_each_entry(iface, &interfaces, head) {
227 if (iface->dhcpv6 != RELAYD_SERVER && iface->dhcpv4 != RELAYD_SERVER)
230 if (iface->dhcpv6 == RELAYD_SERVER && iface->ia_assignments.next) {
231 struct dhcpv6_assignment *c;
232 list_for_each_entry(c, &iface->ia_assignments, head) {
233 if (c->clid_len == 0 || c->managed_size < 0)
236 char ipbuf[INET6_ADDRSTRLEN];
239 odhcpd_hexlify(duidbuf, c->clid_data, c->clid_len);
241 // iface DUID iaid hostname lifetime assigned length [addrs...]
242 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s %x %s %u %x %u ",
243 iface->ifname, duidbuf, ntohl(c->iaid),
244 (c->hostname ? c->hostname : "-"),
245 (unsigned)(c->valid_until > now ?
246 (c->valid_until - now + wall_time) : 0),
247 c->assigned, (unsigned)c->length);
249 struct in6_addr addr;
250 struct odhcpd_ipaddr *addrs = (c->managed) ? c->managed : iface->ia_addr;
251 size_t addrlen = (c->managed) ? (size_t)c->managed_size : iface->ia_addr_len;
254 for (size_t i = 0; i < addrlen; ++i)
255 if (addrs[i].preferred > addrs[m].preferred ||
256 (addrs[i].preferred == addrs[m].preferred &&
257 memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
260 for (size_t i = 0; i < addrlen; ++i) {
261 if (addrs[i].prefix > 96 || c->valid_until <= now ||
262 (iface->managed < RELAYD_MANAGED_NO_AFLAG && i != m &&
263 addrs[i].prefix == 64))
266 addr = addrs[i].addr;
267 if (c->length == 128)
268 addr.s6_addr32[3] = htonl(c->assigned);
270 addr.s6_addr32[1] |= htonl(c->assigned);
272 inet_ntop(AF_INET6, &addr, ipbuf, sizeof(ipbuf) - 1);
274 if (c->length == 128 && c->hostname) {
278 if (dn_expand(iface->search, iface->search + iface->search_len,
279 iface->search, b, sizeof(b)) > 0)
280 fprintf(fp, "\t%s.%s", c->hostname, b);
282 fprintf(fp, "\t%s\n", c->hostname);
283 md5_hash(ipbuf, strlen(ipbuf), &md5);
284 md5_hash(c->hostname, strlen(c->hostname), &md5);
287 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/%d ", ipbuf,
288 (c->managed_size) ? addrs[i].prefix : c->length);
290 leasebuf[l - 1] = '\n';
291 fwrite(leasebuf, 1, l, fp);
295 if (iface->dhcpv4 == RELAYD_SERVER && iface->dhcpv4_assignments.next) {
296 struct dhcpv4_assignment *c;
297 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
298 char ipbuf[INET6_ADDRSTRLEN];
301 odhcpd_hexlify(duidbuf, c->hwaddr, sizeof(c->hwaddr));
303 // iface DUID iaid hostname lifetime assigned length [addrs...]
304 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s ipv4 %s %u %x 32 ",
305 iface->ifname, duidbuf,
306 (c->hostname ? c->hostname : "-"),
307 (unsigned)(c->valid_until > now ?
308 (c->valid_until - now + wall_time) : 0),
311 struct in_addr addr = {htonl(c->addr)};
312 inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf) - 1);
314 if (c->hostname[0]) {
318 if (dn_expand(iface->search, iface->search + iface->search_len,
319 iface->search, b, sizeof(b)) > 0)
320 fprintf(fp, "\t%s.%s", c->hostname, b);
322 fprintf(fp, "\t%s\n", c->hostname);
323 md5_hash(ipbuf, strlen(ipbuf), &md5);
324 md5_hash(c->hostname, strlen(c->hostname), &md5);
327 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/32 ", ipbuf);
328 leasebuf[l - 1] = '\n';
329 fwrite(leasebuf, 1, l, fp);
338 md5_end(newmd5, &md5);
340 if (config.dhcp_cb && memcmp(newmd5, statemd5, sizeof(newmd5))) {
341 memcpy(statemd5, newmd5, sizeof(statemd5));
342 char *argv[2] = {config.dhcp_cb, NULL};
344 execv(argv[0], argv);
351 static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bool add)
353 if (a->length > 64 || a->managed_size < 0)
356 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
357 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
359 for (size_t i = 0; i < addrlen; ++i) {
360 struct in6_addr prefix = addrs[i].addr;
361 prefix.s6_addr32[1] |= htonl(a->assigned);
362 odhcpd_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length,
363 iface, &a->peer.sin6_addr, 1024, add);
368 // More data was received from TCP connection
369 static void managed_handle_pd_data(struct ustream *s, _unused int bytes_new)
371 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
372 time_t now = odhcpd_time();
373 bool first = c->managed_size < 0;
377 char *data = ustream_get_read_buf(s, &pending);
378 char *end = memmem(data, pending, "\n\n", 2);
387 if (c->accept_reconf)
391 for (char *line = strtok_r(data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr)) {
392 c->managed = realloc(c->managed, (c->managed_size + 1) * sizeof(*c->managed));
393 struct odhcpd_ipaddr *n = &c->managed[c->managed_size];
395 char *saveptr2, *x = strtok_r(line, "/", &saveptr2);
396 if (!x || inet_pton(AF_INET6, x, &n->addr) < 1)
399 x = strtok_r(NULL, ",", &saveptr2);
400 if (sscanf(x, "%hhu", &n->prefix) < 1)
403 x = strtok_r(NULL, ",", &saveptr2);
404 if (sscanf(x, "%u", &n->preferred) < 1)
407 x = strtok_r(NULL, ",", &saveptr2);
408 if (sscanf(x, "%u", &n->valid) < 1)
411 if (n->preferred > n->valid)
414 if (UINT32_MAX - now < n->preferred)
415 n->preferred = UINT32_MAX;
419 if (UINT32_MAX - now < n->valid)
420 n->valid = UINT32_MAX;
429 ustream_consume(s, end - data);
432 if (first && c->managed_size == 0)
433 free_dhcpv6_assignment(c);
435 c->valid_until = now + 150;
439 // TCP transmission has ended, either because of success or timeout or other error
440 static void managed_handle_pd_done(struct ustream *s)
442 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
443 c->valid_until = odhcpd_time() + 15;
445 if (c->accept_reconf)
451 static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
453 struct dhcpv6_assignment *c;
455 if (iface->dhcpv6_pd_manager[0]) {
456 int fd = usock(USOCK_UNIX | USOCK_TCP, iface->dhcpv6_pd_manager, NULL);
459 odhcpd_hexlify(iaidbuf, assign->clid_data, assign->clid_len);
461 assign->managed_sock.stream.notify_read = managed_handle_pd_data;
462 assign->managed_sock.stream.notify_state = managed_handle_pd_done;
463 ustream_fd_init(&assign->managed_sock, fd);
464 ustream_printf(&assign->managed_sock.stream, "%s,%x\n::/%d,0,0\n\n",
465 iaidbuf, assign->iaid, assign->length);
466 ustream_write_pending(&assign->managed_sock.stream);
467 assign->managed_size = -1;
468 assign->valid_until = odhcpd_time() + 15;
469 list_add(&assign->head, &iface->ia_assignments);
471 // Wait initial period of up to 250ms for immediate assignment
472 struct pollfd pfd = { .fd = fd, .events = POLLIN };
474 managed_handle_pd_data(&assign->managed_sock.stream, 0);
476 if (fcntl(fd, F_GETFL) >= 0 && assign->managed_size > 0)
481 } else if (iface->ia_addr_len < 1) {
485 // Try honoring the hint first
486 uint32_t current = 1, asize = (1 << (64 - assign->length)) - 1;
487 if (assign->assigned) {
488 list_for_each_entry(c, &iface->ia_assignments, head) {
489 if (c->length == 128 || c->length == 0)
492 if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
493 list_add_tail(&assign->head, &c->head);
494 apply_lease(iface, assign, true);
498 if (c->assigned != 0)
499 current = (c->assigned + (1 << (64 - c->length)));
503 // Fallback to a variable assignment
505 list_for_each_entry(c, &iface->ia_assignments, head) {
506 if (c->length == 128 || c->length == 0)
509 current = (current + asize) & (~asize);
510 if (current + asize < c->assigned) {
511 assign->assigned = current;
512 list_add_tail(&assign->head, &c->head);
513 apply_lease(iface, assign, true);
517 if (c->assigned != 0)
518 current = (c->assigned + (1 << (64 - c->length)));
525 static bool assign_na(struct interface *iface, struct dhcpv6_assignment *assign)
527 // Seed RNG with checksum of DUID
529 for (size_t i = 0; i < assign->clid_len; ++i)
530 seed += assign->clid_data[i];
533 // Try to assign up to 100x
534 for (size_t i = 0; i < 100; ++i) {
536 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
538 struct dhcpv6_assignment *c;
539 list_for_each_entry(c, &iface->ia_assignments, head) {
543 if (c->assigned > try || c->length != 128) {
544 assign->assigned = try;
545 list_add_tail(&assign->head, &c->head);
547 } else if (c->assigned == try) {
556 void dhcpv6_ia_preupdate(struct interface *iface)
558 if (iface->dhcpv6 != RELAYD_SERVER)
561 struct dhcpv6_assignment *c, *border = list_last_entry(
562 &iface->ia_assignments, struct dhcpv6_assignment, head);
563 list_for_each_entry(c, &iface->ia_assignments, head)
564 if (c != border && !iface->managed)
565 apply_lease(iface, c, false);
568 void dhcpv6_ia_postupdate(struct interface *iface, time_t now)
570 if (iface->dhcpv6 != RELAYD_SERVER)
574 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
575 if (iface->ia_addr[i].preferred > (uint32_t)now &&
576 iface->ia_addr[i].prefix < 64 &&
577 iface->ia_addr[i].prefix > minprefix)
578 minprefix = iface->ia_addr[i].prefix;
581 struct dhcpv6_assignment *border = list_last_entry(
582 &iface->ia_assignments, struct dhcpv6_assignment, head);
583 if (minprefix > 32 && minprefix <= 64)
584 border->assigned = 1U << (64 - minprefix);
586 border->assigned = 0;
588 struct list_head reassign = LIST_HEAD_INIT(reassign);
589 struct dhcpv6_assignment *c, *d;
590 list_for_each_entry_safe(c, d, &iface->ia_assignments, head) {
591 if (c->clid_len == 0 || c->valid_until < now || c->managed_size)
594 if (c->length < 128 && c->assigned >= border->assigned && c != border)
595 list_move(&c->head, &reassign);
596 else if (c != border)
597 apply_lease(iface, c, true);
599 if (c->accept_reconf && c->reconf_cnt == 0) {
601 c->reconf_sent = now;
602 send_reconf(iface, c);
604 // Leave all other assignments of that client alone
605 struct dhcpv6_assignment *a;
606 list_for_each_entry(a, &iface->ia_assignments, head)
607 if (a != c && a->clid_len == c->clid_len &&
608 !memcmp(a->clid_data, c->clid_data, a->clid_len))
609 c->reconf_cnt = INT_MAX;
613 while (!list_empty(&reassign)) {
614 c = list_first_entry(&reassign, struct dhcpv6_assignment, head);
616 if (!assign_pd(iface, c)) {
618 list_add(&c->head, &iface->ia_assignments);
622 dhcpv6_write_statefile();
626 static void reconf_timer(struct uloop_timeout *event)
628 time_t now = odhcpd_time();
629 struct interface *iface;
630 list_for_each_entry(iface, &interfaces, head) {
631 if (iface->dhcpv6 != RELAYD_SERVER || iface->ia_assignments.next == NULL)
634 struct dhcpv6_assignment *a, *n;
635 list_for_each_entry_safe(a, n, &iface->ia_assignments, head) {
636 if (a->valid_until < now) {
637 if ((a->length < 128 && a->clid_len > 0) ||
638 (a->length == 128 && a->clid_len == 0)) {
640 free_dhcpv6_assignment(a);
642 } else if (a->reconf_cnt > 0 && a->reconf_cnt < 8 &&
643 now > a->reconf_sent + (1 << a->reconf_cnt)) {
645 a->reconf_sent = now;
646 send_reconf(iface, a);
650 uloop_timeout_set(event, 2000);
654 static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
655 const struct dhcpv6_ia_hdr *ia, struct dhcpv6_assignment *a,
656 struct interface *iface, bool request)
658 if (buflen < sizeof(*ia) + sizeof(struct dhcpv6_ia_prefix))
661 struct dhcpv6_ia_hdr out = {ia->type, 0, ia->iaid, 0, 0};
662 size_t datalen = sizeof(out);
663 time_t now = odhcpd_time();
666 struct __attribute__((packed)) {
670 } stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
673 memcpy(buf + datalen, &stat, sizeof(stat));
674 datalen += sizeof(stat);
677 uint32_t leasetime = iface->dhcpv4_leasetime;
680 else if (leasetime < 60)
683 uint32_t pref = leasetime;
684 uint32_t valid = leasetime;
686 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
687 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
690 for (size_t i = 0; i < addrlen; ++i)
691 if (addrs[i].preferred > addrs[m].preferred ||
692 (addrs[i].preferred == addrs[m].preferred &&
693 memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
696 for (size_t i = 0; i < addrlen; ++i) {
697 uint32_t prefix_pref = addrs[i].preferred;
698 uint32_t prefix_valid = addrs[i].valid;
700 if (addrs[i].prefix > 96 ||
701 addrs[i].preferred <= (uint32_t)now)
704 if (prefix_pref != UINT32_MAX)
707 if (prefix_valid != UINT32_MAX)
710 if (a->length < 128) {
711 struct dhcpv6_ia_prefix p = {
712 .type = htons(DHCPV6_OPT_IA_PREFIX),
713 .len = htons(sizeof(p) - 4),
714 .preferred = htonl(prefix_pref),
715 .valid = htonl(prefix_valid),
716 .prefix = (a->managed_size) ? addrs[i].prefix : a->length,
717 .addr = addrs[i].addr
719 p.addr.s6_addr32[1] |= htonl(a->assigned);
721 size_t entrlen = sizeof(p) - 4;
723 if (datalen + entrlen + 4 > buflen ||
724 (a->assigned == 0 && a->managed_size == 0) ||
725 (!a->managed_size && a->length <= addrs[i].prefix))
728 memcpy(buf + datalen, &p, sizeof(p));
729 datalen += entrlen + 4;
731 struct dhcpv6_ia_addr n = {
732 .type = htons(DHCPV6_OPT_IA_ADDR),
733 .len = htons(sizeof(n) - 4),
734 .addr = addrs[i].addr,
735 .preferred = htonl(prefix_pref),
736 .valid = htonl(prefix_valid)
738 n.addr.s6_addr32[3] = htonl(a->assigned);
739 size_t entrlen = sizeof(n) - 4;
741 if (iface->managed < RELAYD_MANAGED_NO_AFLAG && i != m &&
742 addrs[i].prefix <= 64)
745 if (datalen + entrlen + 4 > buflen || a->assigned == 0)
748 memcpy(buf + datalen, &n, sizeof(n));
749 datalen += entrlen + 4;
752 // Calculate T1 / T2 based on non-deprecated addresses
753 if (prefix_pref > 0) {
754 if (prefix_pref < pref)
757 if (prefix_valid < valid)
758 valid = prefix_valid;
762 a->valid_until = valid + now;
763 out.t1 = htonl(pref * 5 / 10);
764 out.t2 = htonl(pref * 8 / 10);
774 uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + 4;
775 uint16_t otype, olen;
776 dhcpv6_for_each_option((uint8_t*)&ia[1], end, otype, olen, odata) {
777 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&odata[-4];
778 struct dhcpv6_ia_addr *n = (struct dhcpv6_ia_addr*)&odata[-4];
779 if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*p) - 4) &&
780 (otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*n) - 4))
785 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
786 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
788 for (size_t i = 0; i < addrlen; ++i) {
789 if (addrs[i].prefix > 96 ||
790 addrs[i].preferred <= (uint32_t)now)
793 struct in6_addr addr = addrs[i].addr;
794 if (ia->type == htons(DHCPV6_OPT_IA_PD)) {
795 addr.s6_addr32[1] |= htonl(a->assigned);
797 if (!memcmp(&p->addr, &addr, sizeof(addr)) &&
798 p->prefix == ((a->managed) ? addrs[i].prefix : a->length))
801 addr.s6_addr32[3] = htonl(a->assigned);
803 if (!memcmp(&n->addr, &addr, sizeof(addr)))
810 if (otype == DHCPV6_OPT_IA_PREFIX) {
811 struct dhcpv6_ia_prefix inv = {
812 .type = htons(DHCPV6_OPT_IA_PREFIX),
813 .len = htons(sizeof(inv) - 4),
820 if (datalen + sizeof(inv) > buflen)
823 memcpy(buf + datalen, &inv, sizeof(inv));
824 datalen += sizeof(inv);
826 struct dhcpv6_ia_addr inv = {
827 .type = htons(DHCPV6_OPT_IA_ADDR),
828 .len = htons(sizeof(inv) - 4),
834 if (datalen + sizeof(inv) > buflen)
837 memcpy(buf + datalen, &inv, sizeof(inv));
838 datalen += sizeof(inv);
845 out.len = htons(datalen - 4);
846 memcpy(buf, &out, sizeof(out));
851 static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now,
852 const char *duidbuf, bool is_pd, struct dhcpv6_assignment *a, int code)
854 const char *type = "UNKNOWN";
855 const char *status = "UNKNOWN";
857 if (msgtype == DHCPV6_MSG_RENEW)
861 case DHCPV6_MSG_SOLICIT:
864 case DHCPV6_MSG_REQUEST:
867 case DHCPV6_MSG_CONFIRM:
870 case DHCPV6_MSG_RENEW:
873 case DHCPV6_MSG_REBIND:
876 case DHCPV6_MSG_RELEASE:
879 case DHCPV6_MSG_DECLINE:
885 case DHCPV6_STATUS_OK:
888 case DHCPV6_STATUS_NOADDRSAVAIL:
889 status = "no addresses available";
891 case DHCPV6_STATUS_NOBINDING:
892 status = "no binding";
894 case DHCPV6_STATUS_NOTONLINK:
895 status = "not on-link";
897 case DHCPV6_STATUS_NOPREFIXAVAIL:
898 status = "no prefix available";
902 char leasebuf[256] = "";
905 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
906 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
908 char addrbuf[INET6_ADDRSTRLEN];
910 for (size_t i = 0; i < addrlen; ++i) {
911 if (addrs[i].prefix > 96 || addrs[i].preferred <= (uint32_t)now)
914 struct in6_addr addr = addrs[i].addr;
915 int prefix = a->managed ? addrs[i].prefix : a->length;
917 addr.s6_addr32[3] = htonl(a->assigned);
919 addr.s6_addr32[1] |= htonl(a->assigned);
921 inet_ntop(AF_INET6, &addr, addrbuf, sizeof(addrbuf));
922 lbsize += snprintf(leasebuf + lbsize, sizeof(leasebuf) - lbsize, "%s/%d ", addrbuf, prefix);
926 syslog(LOG_WARNING, "DHCPV6 %s %s from %s on %s: %s %s", type, (is_pd) ? "IA_PD" : "IA_NA",
927 duidbuf, iface->ifname, status, leasebuf);
932 ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
933 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end)
935 time_t now = odhcpd_time();
936 size_t response_len = 0;
937 const struct dhcpv6_client_header *hdr = data;
938 uint8_t *start = (uint8_t*)&hdr[1], *odata;
939 uint16_t otype, olen;
941 // Find and parse client-id and hostname
942 bool accept_reconf = false;
943 uint8_t *clid_data = NULL, clid_len = 0, mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
945 size_t hostname_len = 0;
946 bool notonlink = false;
949 dhcpv6_for_each_option(start, end, otype, olen, odata) {
950 if (otype == DHCPV6_OPT_CLIENTID) {
954 if (olen == 14 && odata[0] == 0 && odata[1] == 1)
955 memcpy(mac, &odata[8], sizeof(mac));
956 else if (olen == 10 && odata[0] == 0 && odata[1] == 3)
957 memcpy(mac, &odata[4], sizeof(mac));
960 odhcpd_hexlify(duidbuf, odata, olen);
961 } else if (otype == DHCPV6_OPT_FQDN && olen >= 2 && olen <= 255) {
962 uint8_t fqdn_buf[256];
963 memcpy(fqdn_buf, odata, olen);
964 fqdn_buf[olen++] = 0;
966 if (dn_expand(&fqdn_buf[1], &fqdn_buf[olen], &fqdn_buf[1], hostname, sizeof(hostname)) > 0)
967 hostname_len = strcspn(hostname, ".");
968 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
969 accept_reconf = true;
973 if (!clid_data || !clid_len || clid_len > 130)
976 struct dhcpv6_assignment *first = NULL;
977 dhcpv6_for_each_option(start, end, otype, olen, odata) {
978 bool is_pd = (otype == DHCPV6_OPT_IA_PD);
979 bool is_na = (otype == DHCPV6_OPT_IA_NA);
980 bool ia_addr_present = false;
981 if (!is_pd && !is_na)
984 struct dhcpv6_ia_hdr *ia = (struct dhcpv6_ia_hdr*)&odata[-4];
985 size_t ia_response_len = 0;
986 uint8_t reqlen = (is_pd) ? 62 : 128;
987 uint32_t reqhint = 0;
989 // Parse request hint for IA-PD
992 uint16_t stype, slen;
993 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
994 if (stype != DHCPV6_OPT_IA_PREFIX || slen < sizeof(struct dhcpv6_ia_prefix) - 4)
997 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&sdata[-4];
1000 reqhint = ntohl(p->addr.s6_addr32[1]);
1001 if (reqlen > 32 && reqlen <= 64)
1002 reqhint &= (1U << (64 - reqlen)) - 1;
1010 uint16_t stype, slen;
1011 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1012 if (stype != DHCPV6_OPT_IA_ADDR || slen < sizeof(struct dhcpv6_ia_addr) - 4)
1015 ia_addr_present = true;
1020 struct dhcpv6_assignment *c, *a = NULL;
1021 list_for_each_entry(c, &iface->ia_assignments, head) {
1022 if (((c->clid_len == clid_len && !memcmp(c->clid_data, clid_data, clid_len)) ||
1023 (c->clid_len >= clid_len && !c->clid_data[0] && !c->clid_data[1]
1024 && !memcmp(c->mac, mac, sizeof(mac)))) &&
1025 (c->iaid == ia->iaid || c->valid_until < now) &&
1026 ((is_pd && c->length <= 64) || (is_na && c->length == 128))) {
1030 apply_lease(iface, a, false);
1031 memcpy(a->clid_data, clid_data, clid_len);
1032 a->clid_len = clid_len;
1041 // Generic message handling
1042 uint16_t status = DHCPV6_STATUS_OK;
1043 if (a && a->managed_size < 0) {
1045 } else if (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_REQUEST) {
1046 bool assigned = !!a;
1048 if (!a && !iface->no_dynamic_dhcp) { // Create new binding
1049 a = calloc(1, sizeof(*a) + clid_len);
1051 a->clid_len = clid_len;
1055 a->assigned = reqhint;
1058 memcpy(a->key, first->key, sizeof(a->key));
1060 odhcpd_urandom(a->key, sizeof(a->key));
1061 memcpy(a->clid_data, clid_data, clid_len);
1064 while (!(assigned = assign_pd(iface, a)) &&
1065 !a->managed_size && ++a->length <= 64);
1067 assigned = assign_na(iface, a);
1069 if (a->managed_size && !assigned)
1074 if (!assigned || iface->ia_addr_len == 0) { // Set error status
1075 status = (is_pd) ? DHCPV6_STATUS_NOPREFIXAVAIL : DHCPV6_STATUS_NOADDRSAVAIL;
1076 } else if (assigned && !first) { //
1077 size_t handshake_len = 4;
1079 buf[1] = DHCPV6_OPT_RECONF_ACCEPT;
1083 if (hdr->msg_type == DHCPV6_MSG_REQUEST) {
1084 struct dhcpv6_auth_reconfigure auth = {
1085 htons(DHCPV6_OPT_AUTH),
1086 htons(sizeof(auth) - 4),
1088 {htonl(time(NULL)), htonl(++serial)},
1092 memcpy(auth.key, a->key, sizeof(a->key));
1093 memcpy(buf + handshake_len, &auth, sizeof(auth));
1094 handshake_len += sizeof(auth);
1097 buf += handshake_len;
1098 buflen -= handshake_len;
1099 response_len += handshake_len;
1104 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1106 // Was only a solicitation: mark binding for removal
1107 if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
1109 } else if (assigned && hdr->msg_type == DHCPV6_MSG_REQUEST) {
1110 if (hostname_len > 0) {
1111 a->hostname = realloc(a->hostname, hostname_len + 1);
1113 memcpy(a->hostname, hostname, hostname_len);
1114 a->hostname[hostname_len] = 0;
1117 a->accept_reconf = accept_reconf;
1118 apply_lease(iface, a, true);
1119 } else if (!assigned && a && a->managed_size == 0) { // Cleanup failed assignment
1120 free_dhcpv6_assignment(a);
1122 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1123 hdr->msg_type == DHCPV6_MSG_RELEASE ||
1124 hdr->msg_type == DHCPV6_MSG_REBIND ||
1125 hdr->msg_type == DHCPV6_MSG_DECLINE) {
1126 if (!a && hdr->msg_type != DHCPV6_MSG_REBIND) {
1127 status = DHCPV6_STATUS_NOBINDING;
1128 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1129 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1130 hdr->msg_type == DHCPV6_MSG_REBIND) {
1131 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1133 apply_lease(iface, a, true);
1134 } else if (hdr->msg_type == DHCPV6_MSG_RELEASE) {
1136 apply_lease(iface, a, false);
1137 } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
1139 a->valid_until = now + 3600; // Block address for 1h
1141 } else if (hdr->msg_type == DHCPV6_MSG_CONFIRM && ia_addr_present) {
1142 // Send NOTONLINK for CONFIRM with addr present so that clients restart connection
1143 status = DHCPV6_STATUS_NOTONLINK;
1144 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1148 buf += ia_response_len;
1149 buflen -= ia_response_len;
1150 response_len += ia_response_len;
1151 dhcpv6_log(hdr->msg_type, iface, now, duidbuf, is_pd, a, status);
1154 if ((hdr->msg_type == DHCPV6_MSG_RELEASE || hdr->msg_type == DHCPV6_MSG_DECLINE || notonlink) &&
1155 response_len + 6 < buflen) {
1157 buf[1] = DHCPV6_OPT_STATUS;
1161 buf[5] = (notonlink) ? DHCPV6_STATUS_NOTONLINK : DHCPV6_STATUS_OK;
1165 dhcpv6_write_statefile();
1168 return response_len;