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"
32 #include <arpa/inet.h>
33 #include <sys/timerfd.h>
36 static void update(struct interface *iface);
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);
67 int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
69 if (!enable && iface->ia_assignments.next) {
70 struct dhcpv6_assignment *c;
71 while (!list_empty(&iface->ia_assignments)) {
72 c = list_first_entry(&iface->ia_assignments, struct dhcpv6_assignment, head);
73 free_dhcpv6_assignment(c);
77 if (iface->dhcpv6 == RELAYD_SERVER) {
78 if (!iface->ia_assignments.next)
79 INIT_LIST_HEAD(&iface->ia_assignments);
81 if (list_empty(&iface->ia_assignments)) {
82 struct dhcpv6_assignment *border = calloc(1, sizeof(*border));
84 syslog(LOG_ERR, "Calloc failed for border on interface %s", iface->ifname);
89 list_add(&border->head, &iface->ia_assignments);
94 // Parse static entries
96 list_for_each_entry(lease, &leases, head) {
98 struct dhcpv6_assignment *a = calloc(1, sizeof(*a) + lease->duid_len);
100 syslog(LOG_ERR, "Calloc failed for static lease assignment on interface %s",
105 a->clid_len = lease->duid_len;
108 a->assigned = lease->hostid;
110 uint32_t i4a = ntohl(lease->ipaddr.s_addr) & 0xff;
111 a->assigned = ((i4a / 100) << 8) | (((i4a % 100) / 10) << 4) | (i4a % 10);
113 odhcpd_urandom(a->key, sizeof(a->key));
114 memcpy(a->clid_data, lease->duid, a->clid_len);
115 memcpy(a->mac, lease->mac.ether_addr_octet, sizeof(a->mac));
117 // Assign to all interfaces
118 struct dhcpv6_assignment *c;
119 list_for_each_entry(c, &iface->ia_assignments, head) {
120 if (c->length != 128 || c->assigned > a->assigned) {
121 list_add_tail(&a->head, &c->head);
123 } else if (c->assigned == a->assigned) {
124 // Already an assignment with that number
130 if (lease->hostname[0]) {
132 a->hostname = strdup(lease->hostname);
145 static int send_reconf(struct interface *iface, struct dhcpv6_assignment *assign)
148 struct dhcpv6_client_header hdr;
152 uint16_t hardware_type;
157 struct dhcpv6_auth_reconfigure auth;
160 uint8_t clid_data[128];
161 } __attribute__((packed)) reconf_msg = {
162 .hdr = {DHCPV6_MSG_RECONFIGURE, {0, 0, 0}},
163 .srvid_type = htons(DHCPV6_OPT_SERVERID),
164 .srvid_len = htons(10),
165 .duid_type = htons(3),
166 .hardware_type = htons(1),
167 .msg_type = htons(DHCPV6_OPT_RECONF_MSG),
169 .msg_id = DHCPV6_MSG_RENEW,
170 .auth = {htons(DHCPV6_OPT_AUTH),
171 htons(sizeof(reconf_msg.auth) - 4), 3, 1, 0,
172 {htonl(time(NULL)), htonl(++serial)}, 2, {0}},
173 .clid_type = htons(DHCPV6_OPT_CLIENTID),
174 .clid_len = htons(assign->clid_len),
178 odhcpd_get_mac(iface, reconf_msg.mac);
179 memcpy(reconf_msg.clid_data, assign->clid_data, assign->clid_len);
180 struct iovec iov = {&reconf_msg, sizeof(reconf_msg) - 128 + assign->clid_len};
183 uint8_t secretbytes[64];
184 memset(secretbytes, 0, sizeof(secretbytes));
185 memcpy(secretbytes, assign->key, sizeof(assign->key));
187 for (size_t i = 0; i < sizeof(secretbytes); ++i)
188 secretbytes[i] ^= 0x36;
191 md5_hash(secretbytes, sizeof(secretbytes), &md5);
192 md5_hash(iov.iov_base, iov.iov_len, &md5);
193 md5_end(reconf_msg.auth.key, &md5);
195 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
196 secretbytes[i] ^= 0x36;
197 secretbytes[i] ^= 0x5c;
201 md5_hash(secretbytes, sizeof(secretbytes), &md5);
202 md5_hash(reconf_msg.auth.key, 16, &md5);
203 md5_end(reconf_msg.auth.key, &md5);
205 return odhcpd_send(iface->dhcpv6_event.uloop.fd, &assign->peer, &iov, 1, iface);
209 void dhcpv6_write_statefile(void)
214 if (config.dhcp_statefile) {
215 time_t now = odhcpd_time(), wall_time = time(NULL);
216 int fd = open(config.dhcp_statefile, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
220 lockf(fd, F_LOCK, 0);
223 FILE *fp = fdopen(fd, "w");
229 struct interface *iface;
230 list_for_each_entry(iface, &interfaces, head) {
231 if (iface->dhcpv6 != RELAYD_SERVER && iface->dhcpv4 != RELAYD_SERVER)
234 if (iface->dhcpv6 == RELAYD_SERVER && iface->ia_assignments.next) {
235 struct dhcpv6_assignment *c;
236 list_for_each_entry(c, &iface->ia_assignments, head) {
237 if (c->clid_len == 0 || c->managed_size < 0)
240 char ipbuf[INET6_ADDRSTRLEN];
243 odhcpd_hexlify(duidbuf, c->clid_data, c->clid_len);
245 // iface DUID iaid hostname lifetime assigned length [addrs...]
246 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s %x %s %u %x %u ",
247 iface->ifname, duidbuf, ntohl(c->iaid),
248 (c->hostname ? c->hostname : "-"),
249 (unsigned)(c->valid_until > now ?
250 (c->valid_until - now + wall_time) : 0),
251 c->assigned, (unsigned)c->length);
253 struct in6_addr addr;
254 struct odhcpd_ipaddr *addrs = (c->managed) ? c->managed : iface->ia_addr;
255 size_t addrlen = (c->managed) ? (size_t)c->managed_size : iface->ia_addr_len;
257 for (size_t i = 0; i < addrlen; ++i) {
258 if (addrs[i].prefix > 96)
261 addr = addrs[i].addr;
262 if (c->length == 128)
263 addr.s6_addr32[3] = htonl(c->assigned);
265 addr.s6_addr32[1] |= htonl(c->assigned);
267 inet_ntop(AF_INET6, &addr, ipbuf, sizeof(ipbuf) - 1);
269 if (c->length == 128 && c->hostname && i == 0) {
273 if (dn_expand(iface->search, iface->search + iface->search_len,
274 iface->search, b, sizeof(b)) > 0)
275 fprintf(fp, "\t%s.%s", c->hostname, b);
277 fprintf(fp, "\t%s\n", c->hostname);
278 md5_hash(ipbuf, strlen(ipbuf), &md5);
279 md5_hash(c->hostname, strlen(c->hostname), &md5);
282 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/%hhu ", ipbuf,
283 (c->managed_size) ? addrs[i].prefix : c->length);
285 leasebuf[l - 1] = '\n';
286 fwrite(leasebuf, 1, l, fp);
290 if (iface->dhcpv4 == RELAYD_SERVER && iface->dhcpv4_assignments.next) {
291 struct dhcpv4_assignment *c;
292 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
293 char ipbuf[INET6_ADDRSTRLEN];
296 odhcpd_hexlify(duidbuf, c->hwaddr, sizeof(c->hwaddr));
298 // iface DUID iaid hostname lifetime assigned length [addrs...]
299 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s ipv4 %s %u %x 32 ",
300 iface->ifname, duidbuf,
301 (c->hostname ? c->hostname : "-"),
302 (unsigned)(c->valid_until > now ?
303 (c->valid_until - now + wall_time) : 0),
306 struct in_addr addr = {htonl(c->addr)};
307 inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf) - 1);
309 if (c->hostname[0]) {
313 if (dn_expand(iface->search, iface->search + iface->search_len,
314 iface->search, b, sizeof(b)) > 0)
315 fprintf(fp, "\t%s.%s", c->hostname, b);
317 fprintf(fp, "\t%s\n", c->hostname);
318 md5_hash(ipbuf, strlen(ipbuf), &md5);
319 md5_hash(c->hostname, strlen(c->hostname), &md5);
322 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/32 ", ipbuf);
323 leasebuf[l - 1] = '\n';
324 fwrite(leasebuf, 1, l, fp);
333 md5_end(newmd5, &md5);
335 if (config.dhcp_cb && memcmp(newmd5, statemd5, sizeof(newmd5))) {
336 memcpy(statemd5, newmd5, sizeof(statemd5));
337 char *argv[2] = {config.dhcp_cb, NULL};
339 execv(argv[0], argv);
346 static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bool add)
348 if (a->length > 64 || a->managed_size < 0)
351 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
352 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
354 for (size_t i = 0; i < addrlen; ++i) {
355 struct in6_addr prefix = addrs[i].addr;
356 prefix.s6_addr32[1] |= htonl(a->assigned);
357 odhcpd_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length,
358 iface, &a->peer.sin6_addr, add);
363 // More data was received from TCP connection
364 static void managed_handle_pd_data(struct ustream *s, _unused int bytes_new)
366 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
367 time_t now = odhcpd_time();
368 bool first = c->managed_size < 0;
372 char *data = ustream_get_read_buf(s, &pending);
373 char *end = memmem(data, pending, "\n\n", 2);
382 if (c->accept_reconf)
386 for (char *line = strtok_r(data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr)) {
387 c->managed = realloc(c->managed, (c->managed_size + 1) * sizeof(*c->managed));
388 struct odhcpd_ipaddr *n = &c->managed[c->managed_size];
390 char *saveptr2, *x = strtok_r(line, "/", &saveptr2);
391 if (!x || inet_pton(AF_INET6, x, &n->addr) < 1)
394 x = strtok_r(NULL, ",", &saveptr2);
395 if (sscanf(x, "%hhu", &n->prefix) < 1)
398 x = strtok_r(NULL, ",", &saveptr2);
399 if (sscanf(x, "%u", &n->preferred) < 1)
402 x = strtok_r(NULL, ",", &saveptr2);
403 if (sscanf(x, "%u", &n->valid) < 1)
406 if (n->preferred > n->valid)
409 if (UINT32_MAX - now < n->preferred)
410 n->preferred = UINT32_MAX;
414 if (UINT32_MAX - now < n->valid)
415 n->valid = UINT32_MAX;
419 n->has_class = false;
426 ustream_consume(s, end - data);
429 if (first && c->managed_size == 0)
430 free_dhcpv6_assignment(c);
434 // TCP transmission has ended, either because of success or timeout or other error
435 static void managed_handle_pd_done(struct ustream *s)
437 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
438 c->valid_until = odhcpd_time() + 15;
440 if (c->accept_reconf)
446 static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
448 struct dhcpv6_assignment *c;
450 if (iface->dhcpv6_pd_manager[0]) {
451 int fd = usock(USOCK_UNIX | USOCK_TCP, iface->dhcpv6_pd_manager, NULL);
454 odhcpd_hexlify(iaidbuf, assign->clid_data, assign->clid_len);
456 assign->managed_sock.stream.notify_read = managed_handle_pd_data;
457 assign->managed_sock.stream.notify_state = managed_handle_pd_done;
458 ustream_fd_init(&assign->managed_sock, fd);
459 ustream_printf(&assign->managed_sock.stream, "%s,%x\n::/%d,0,0\n\n",
460 iaidbuf, assign->iaid, assign->length);
461 ustream_write_pending(&assign->managed_sock.stream);
462 assign->managed_size = -1;
463 assign->valid_until = odhcpd_time() + 15;
464 list_add(&assign->head, &iface->ia_assignments);
468 } else if (iface->ia_addr_len < 1) {
472 // Try honoring the hint first
473 uint32_t current = 1, asize = (1 << (64 - assign->length)) - 1;
474 if (assign->assigned) {
475 list_for_each_entry(c, &iface->ia_assignments, head) {
476 if (c->length == 128 || c->length == 0)
479 if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
480 list_add_tail(&assign->head, &c->head);
481 apply_lease(iface, assign, true);
485 if (c->assigned != 0)
486 current = (c->assigned + (1 << (64 - c->length)));
490 // Fallback to a variable assignment
492 list_for_each_entry(c, &iface->ia_assignments, head) {
493 if (c->length == 128 || c->length == 0)
496 current = (current + asize) & (~asize);
497 if (current + asize < c->assigned) {
498 assign->assigned = current;
499 list_add_tail(&assign->head, &c->head);
500 apply_lease(iface, assign, true);
504 if (c->assigned != 0)
505 current = (c->assigned + (1 << (64 - c->length)));
512 static bool assign_na(struct interface *iface, struct dhcpv6_assignment *assign)
515 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
516 if (!iface->ia_addr[i].has_class) {
519 } else if (assign->classes_cnt) {
520 for (size_t j = 0; j < assign->classes_cnt; ++j)
521 if (assign->classes[j] == iface->ia_addr[i].class)
523 } else if (assign->all_class) {
531 // Seed RNG with checksum of DUID
533 for (size_t i = 0; i < assign->clid_len; ++i)
534 seed += assign->clid_data[i];
537 // Try to assign up to 100x
538 for (size_t i = 0; i < 100; ++i) {
540 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
542 struct dhcpv6_assignment *c;
543 list_for_each_entry(c, &iface->ia_assignments, head) {
547 if (c->assigned > try || c->length != 128) {
548 assign->assigned = try;
549 list_add_tail(&assign->head, &c->head);
551 } else if (c->assigned == try) {
561 static int prefixcmp(const void *va, const void *vb)
563 const struct odhcpd_ipaddr *a = va, *b = vb;
564 uint32_t a_pref = ((a->addr.s6_addr[0] & 0xfe) != 0xfc) ? a->preferred : 1;
565 uint32_t b_pref = ((b->addr.s6_addr[0] & 0xfe) != 0xfc) ? b->preferred : 1;
566 return (a_pref < b_pref) ? 1 : (a_pref > b_pref) ? -1 : 0;
570 static void update(struct interface *iface)
572 struct odhcpd_ipaddr addr[8];
573 memset(addr, 0, sizeof(addr));
574 int len = odhcpd_get_interface_addresses(iface->ifindex, addr, 8);
579 qsort(addr, len, sizeof(*addr), prefixcmp);
581 time_t now = odhcpd_time();
584 for (int i = 0; i < len; ++i) {
585 if (addr[i].prefix > minprefix)
586 minprefix = addr[i].prefix;
588 addr[i].addr.s6_addr32[3] = 0;
590 if (addr[i].preferred < UINT32_MAX - now)
591 addr[i].preferred += now;
593 if (addr[i].valid < UINT32_MAX - now)
594 addr[i].valid += now;
597 struct dhcpv6_assignment *border = list_last_entry(&iface->ia_assignments, struct dhcpv6_assignment, head);
598 border->assigned = 1 << (64 - minprefix);
600 bool change = len != (int)iface->ia_addr_len;
601 for (int i = 0; !change && i < len; ++i)
602 if (addr[i].addr.s6_addr32[0] != iface->ia_addr[i].addr.s6_addr32[0] ||
603 addr[i].addr.s6_addr32[1] != iface->ia_addr[i].addr.s6_addr32[1] ||
604 (addr[i].preferred > 0) != (iface->ia_addr[i].preferred > 0) ||
605 (addr[i].valid > (uint32_t)now + 7200) !=
606 (iface->ia_addr[i].valid > (uint32_t)now + 7200))
610 struct dhcpv6_assignment *c;
611 list_for_each_entry(c, &iface->ia_assignments, head)
613 apply_lease(iface, c, false);
616 memcpy(iface->ia_addr, addr, len * sizeof(*addr));
617 iface->ia_addr_len = len;
619 if (change) { // Addresses / prefixes have changed
620 struct list_head reassign = LIST_HEAD_INIT(reassign);
621 struct dhcpv6_assignment *c, *d;
622 list_for_each_entry_safe(c, d, &iface->ia_assignments, head) {
623 if (c->clid_len == 0 || c->valid_until < now || c->managed_size)
626 if (c->length < 128 && c->assigned >= border->assigned && c != border)
627 list_move(&c->head, &reassign);
628 else if (c != border)
629 apply_lease(iface, c, true);
631 if (c->accept_reconf && c->reconf_cnt == 0) {
633 c->reconf_sent = now;
634 send_reconf(iface, c);
636 // Leave all other assignments of that client alone
637 struct dhcpv6_assignment *a;
638 list_for_each_entry(a, &iface->ia_assignments, head)
639 if (a != c && a->clid_len == c->clid_len &&
640 !memcmp(a->clid_data, c->clid_data, a->clid_len))
641 c->reconf_cnt = INT_MAX;
645 while (!list_empty(&reassign)) {
646 c = list_first_entry(&reassign, struct dhcpv6_assignment, head);
648 if (!assign_pd(iface, c)) {
650 list_add(&c->head, &iface->ia_assignments);
654 dhcpv6_write_statefile();
659 static void reconf_timer(struct uloop_timeout *event)
661 time_t now = odhcpd_time();
662 struct interface *iface;
663 list_for_each_entry(iface, &interfaces, head) {
664 if (iface->dhcpv6 != RELAYD_SERVER || iface->ia_assignments.next == NULL)
667 struct dhcpv6_assignment *a, *n;
668 list_for_each_entry_safe(a, n, &iface->ia_assignments, head) {
669 if (a->valid_until < now) {
670 if ((a->length < 128 && a->clid_len > 0) ||
671 (a->length == 128 && a->clid_len == 0)) {
673 free_dhcpv6_assignment(a);
675 } else if (a->reconf_cnt > 0 && a->reconf_cnt < 8 &&
676 now > a->reconf_sent + (1 << a->reconf_cnt)) {
678 a->reconf_sent = now;
679 send_reconf(iface, a);
683 uloop_timeout_set(event, 2000);
687 static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
688 const struct dhcpv6_ia_hdr *ia, struct dhcpv6_assignment *a,
689 struct interface *iface, bool request)
691 if (buflen < sizeof(*ia) + sizeof(struct dhcpv6_ia_prefix))
694 struct dhcpv6_ia_hdr out = {ia->type, 0, ia->iaid, 0, 0};
695 size_t datalen = sizeof(out);
696 time_t now = odhcpd_time();
699 struct __attribute__((packed)) {
703 } stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
706 memcpy(buf + datalen, &stat, sizeof(stat));
707 datalen += sizeof(stat);
710 uint32_t pref = 3600;
711 uint32_t valid = 3600;
713 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
714 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
716 for (size_t i = 0; i < addrlen; ++i) {
718 if (addrs[i].has_class) {
720 if (a->classes_cnt) {
721 for (size_t j = 0; j < a->classes_cnt; ++j)
722 if (a->classes[j] == addrs[i].class)
724 } else if (a->all_class) {
732 uint32_t prefix_pref = addrs[i].preferred - now;
733 uint32_t prefix_valid = addrs[i].valid - now;
735 if (addrs[i].prefix > 96 ||
736 addrs[i].preferred <= (uint32_t)now)
739 if (prefix_pref > 86400)
742 if (prefix_valid > 86400)
743 prefix_valid = 86400;
745 #ifdef DHCPV6_OPT_PREFIX_CLASS
750 } pclass = {htons(DHCPV6_OPT_PREFIX_CLASS),
751 htons(2), htons(iface->ia_addr[i].class)};
754 if (a->length < 128) {
755 struct dhcpv6_ia_prefix p = {
756 .type = htons(DHCPV6_OPT_IA_PREFIX),
757 .len = htons(sizeof(p) - 4),
758 .preferred = htonl(prefix_pref),
759 .valid = htonl(prefix_valid),
760 .prefix = (a->managed_size) ? addrs[i].prefix : a->length,
761 .addr = addrs[i].addr
763 p.addr.s6_addr32[1] |= htonl(a->assigned);
765 size_t entrlen = sizeof(p) - 4;
767 #ifdef DHCPV6_OPT_PREFIX_CLASS
768 if (iface->ia_addr[i].has_class) {
769 entrlen += sizeof(pclass);
770 p.len = htons(entrlen);
774 if (datalen + entrlen + 4 > buflen ||
775 (a->assigned == 0 && a->managed_size == 0))
778 memcpy(buf + datalen, &p, sizeof(p));
779 #ifdef DHCPV6_OPT_PREFIX_CLASS
780 memcpy(buf + datalen + sizeof(p), &pclass, sizeof(pclass));
782 datalen += entrlen + 4;
784 struct dhcpv6_ia_addr n = {
785 .type = htons(DHCPV6_OPT_IA_ADDR),
786 .len = htons(sizeof(n) - 4),
787 .addr = addrs[i].addr,
788 .preferred = htonl(prefix_pref),
789 .valid = htonl(prefix_valid)
791 n.addr.s6_addr32[3] = htonl(a->assigned);
792 size_t entrlen = sizeof(n) - 4;
794 #ifdef DHCPV6_OPT_PREFIX_CLASS
795 if (iface->ia_addr[i].has_class) {
796 entrlen += sizeof(pclass);
797 n.len = htons(entrlen);
801 if (datalen + entrlen + 4 > buflen || a->assigned == 0)
804 memcpy(buf + datalen, &n, sizeof(n));
805 #ifdef DHCPV6_OPT_PREFIX_CLASS
806 memcpy(buf + datalen + sizeof(n), &pclass, sizeof(pclass));
808 datalen += entrlen + 4;
811 // Calculate T1 / T2 based on non-deprecated addresses
812 if (prefix_pref > 0) {
813 if (prefix_pref < pref)
816 if (prefix_valid < valid)
817 valid = prefix_valid;
821 a->valid_until = valid + now;
822 out.t1 = htonl(pref * 5 / 10);
823 out.t2 = htonl(pref * 8 / 10);
833 uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + 4;
834 uint16_t otype, olen;
835 dhcpv6_for_each_option((uint8_t*)&ia[1], end, otype, olen, odata) {
836 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&odata[-4];
837 struct dhcpv6_ia_addr *n = (struct dhcpv6_ia_addr*)&odata[-4];
838 if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*p) - 4) &&
839 (otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*n) - 4))
844 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
845 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
847 for (size_t i = 0; i < addrlen; ++i) {
848 if (addrs[i].prefix > 96 ||
849 addrs[i].preferred <= (uint32_t)now)
852 struct in6_addr addr = addrs[i].addr;
853 if (ia->type == htons(DHCPV6_OPT_IA_PD)) {
854 addr.s6_addr32[1] |= htonl(a->assigned);
856 if (!memcmp(&p->addr, &addr, sizeof(addr)) &&
857 p->prefix == a->length)
860 addr.s6_addr32[3] = htonl(a->assigned);
862 if (!memcmp(&n->addr, &addr, sizeof(addr)))
869 if (otype == DHCPV6_OPT_IA_PREFIX) {
870 struct dhcpv6_ia_prefix inv = {
871 .type = htons(DHCPV6_OPT_IA_PREFIX),
872 .len = htons(sizeof(inv) - 4),
879 if (datalen + sizeof(inv) > buflen)
882 memcpy(buf + datalen, &inv, sizeof(inv));
883 datalen += sizeof(inv);
885 struct dhcpv6_ia_addr inv = {
886 .type = htons(DHCPV6_OPT_IA_ADDR),
887 .len = htons(sizeof(inv) - 4),
893 if (datalen + sizeof(inv) > buflen)
896 memcpy(buf + datalen, &inv, sizeof(inv));
897 datalen += sizeof(inv);
904 out.len = htons(datalen - 4);
905 memcpy(buf, &out, sizeof(out));
910 ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
911 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end)
913 time_t now = odhcpd_time();
914 size_t response_len = 0;
915 const struct dhcpv6_client_header *hdr = data;
916 uint8_t *start = (uint8_t*)&hdr[1], *odata;
917 uint16_t otype, olen;
919 // Find and parse client-id and hostname
920 bool accept_reconf = false;
921 uint8_t *clid_data = NULL, clid_len = 0, mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
923 size_t hostname_len = 0;
924 bool class_oro = false;
925 dhcpv6_for_each_option(start, end, otype, olen, odata) {
926 if (otype == DHCPV6_OPT_CLIENTID) {
930 if (olen == 14 && odata[0] == 0 && odata[1] == 1)
931 memcpy(mac, &odata[8], sizeof(mac));
932 else if (olen == 10 && odata[0] == 0 && odata[1] == 3)
933 memcpy(mac, &odata[4], sizeof(mac));
934 } else if (otype == DHCPV6_OPT_FQDN && olen >= 2 && olen <= 255) {
935 uint8_t fqdn_buf[256];
936 memcpy(fqdn_buf, odata, olen);
937 fqdn_buf[olen++] = 0;
939 if (dn_expand(&fqdn_buf[1], &fqdn_buf[olen], &fqdn_buf[1], hostname, sizeof(hostname)) > 0)
940 hostname_len = strcspn(hostname, ".");
941 } else if (otype == DHCPV6_OPT_ORO) {
942 #ifdef DHCPV6_OPT_PREFIX_CLASS
943 for (size_t i = 0; i + 1 < olen; i += 2) {
944 if (odata[i] == (DHCPV6_OPT_PREFIX_CLASS >> 8) &&
945 odata[i + 1] == (DHCPV6_OPT_PREFIX_CLASS & 0xff))
949 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
950 accept_reconf = true;
954 if (!clid_data || !clid_len || clid_len > 130)
958 bool update_state = false;
960 struct dhcpv6_assignment *first = NULL;
961 dhcpv6_for_each_option(start, end, otype, olen, odata) {
962 bool is_pd = (otype == DHCPV6_OPT_IA_PD);
963 bool is_na = (otype == DHCPV6_OPT_IA_NA);
964 if (!is_pd && !is_na)
967 struct dhcpv6_ia_hdr *ia = (struct dhcpv6_ia_hdr*)&odata[-4];
968 size_t ia_response_len = 0;
969 uint8_t reqlen = (is_pd) ? 62 : 128;
970 uint32_t reqhint = 0;
972 const uint8_t classes_max = 32;
973 uint8_t classes_cnt = 0;
974 uint16_t classes[classes_max];
976 // Parse request hint for IA-PD
979 uint16_t stype, slen;
980 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
981 if (stype != DHCPV6_OPT_IA_PREFIX || slen < sizeof(struct dhcpv6_ia_prefix) - 4)
984 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&sdata[-4];
987 reqhint = ntohl(p->addr.s6_addr32[1]);
988 if (reqlen > 32 && reqlen <= 64)
989 reqhint &= (1U << (64 - reqlen)) - 1;
992 #ifdef DHCPV6_OPT_PREFIX_CLASS
994 uint16_t xtype, xlen;
995 dhcpv6_for_each_option(&p[1], sdata + slen, xtype, xlen, xdata) {
996 if (xtype != DHCPV6_OPT_PREFIX_CLASS || xlen != 2)
999 if (classes_cnt >= classes_max)
1002 classes[classes_cnt++] = (uint16_t)xdata[0] << 8 | (uint16_t)xdata[1];
1011 uint16_t stype, slen;
1012 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1013 if (stype != DHCPV6_OPT_IA_ADDR || slen < sizeof(struct dhcpv6_ia_addr) - 4)
1016 #ifdef DHCPV6_OPT_PREFIX_CLASS
1018 uint16_t xtype, xlen;
1019 struct dhcpv6_ia_addr *p = (struct dhcpv6_ia_addr*)&sdata[-4];
1020 dhcpv6_for_each_option(&p[1], sdata + slen, xtype, xlen, xdata) {
1021 if (xtype != DHCPV6_OPT_PREFIX_CLASS || xlen != 2)
1024 if (classes_cnt >= classes_max)
1027 classes[classes_cnt++] = (uint16_t)xdata[0] << 8 | (uint16_t)xdata[1];
1034 struct dhcpv6_assignment *c, *a = NULL;
1035 list_for_each_entry(c, &iface->ia_assignments, head) {
1036 if (((c->clid_len == clid_len && !memcmp(c->clid_data, clid_data, clid_len)) ||
1037 (c->clid_len >= clid_len && !c->clid_data[0] && !c->clid_data[1]
1038 && !memcmp(c->mac, mac, sizeof(mac)))) &&
1039 (c->iaid == ia->iaid || c->valid_until < now) &&
1040 ((is_pd && c->length <= 64) || (is_na && c->length == 128))) {
1044 apply_lease(iface, a, false);
1045 memcpy(a->clid_data, clid_data, clid_len);
1046 a->clid_len = clid_len;
1051 a->all_class = class_oro;
1052 a->classes_cnt = classes_cnt;
1053 a->classes = realloc(a->classes, classes_cnt * sizeof(uint16_t));
1055 memcpy(a->classes, classes, classes_cnt * sizeof(uint16_t));
1060 // Generic message handling
1061 uint16_t status = DHCPV6_STATUS_OK;
1062 if (a && a->managed_size < 0) {
1064 } else if (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_REQUEST) {
1065 bool assigned = !!a;
1067 if (!a && !iface->no_dynamic_dhcp) { // Create new binding
1068 a = calloc(1, sizeof(*a) + clid_len);
1070 a->clid_len = clid_len;
1074 a->assigned = reqhint;
1075 a->all_class = class_oro;
1076 a->classes_cnt = classes_cnt;
1078 a->classes = malloc(classes_cnt * sizeof(uint16_t));
1080 memcpy(a->classes, classes, classes_cnt * sizeof(uint16_t));
1084 memcpy(a->key, first->key, sizeof(a->key));
1086 odhcpd_urandom(a->key, sizeof(a->key));
1087 memcpy(a->clid_data, clid_data, clid_len);
1090 while (!(assigned = assign_pd(iface, a)) &&
1091 !a->managed_size && ++a->length <= 64);
1093 assigned = assign_na(iface, a);
1095 if (a->managed_size && !assigned)
1100 if (!assigned || iface->ia_addr_len == 0) { // Set error status
1101 status = (is_pd) ? DHCPV6_STATUS_NOPREFIXAVAIL : DHCPV6_STATUS_NOADDRSAVAIL;
1102 } else if (assigned && !first) { //
1103 size_t handshake_len = 4;
1105 buf[1] = DHCPV6_OPT_RECONF_ACCEPT;
1109 if (hdr->msg_type == DHCPV6_MSG_REQUEST) {
1110 struct dhcpv6_auth_reconfigure auth = {
1111 htons(DHCPV6_OPT_AUTH),
1112 htons(sizeof(auth) - 4),
1114 {htonl(time(NULL)), htonl(++serial)},
1118 memcpy(auth.key, a->key, sizeof(a->key));
1119 memcpy(buf + handshake_len, &auth, sizeof(auth));
1120 handshake_len += sizeof(auth);
1123 buf += handshake_len;
1124 buflen -= handshake_len;
1125 response_len += handshake_len;
1130 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1132 // Was only a solicitation: mark binding for removal
1133 if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
1135 } else if (assigned && hdr->msg_type == DHCPV6_MSG_REQUEST) {
1136 if (hostname_len > 0) {
1137 a->hostname = realloc(a->hostname, hostname_len + 1);
1139 memcpy(a->hostname, hostname, hostname_len);
1140 a->hostname[hostname_len] = 0;
1143 a->accept_reconf = accept_reconf;
1144 apply_lease(iface, a, true);
1145 update_state = true;
1146 } else if (!assigned && a && a->managed_size == 0) { // Cleanup failed assignment
1147 free_dhcpv6_assignment(a);
1149 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1150 hdr->msg_type == DHCPV6_MSG_RELEASE ||
1151 hdr->msg_type == DHCPV6_MSG_REBIND ||
1152 hdr->msg_type == DHCPV6_MSG_DECLINE) {
1153 if (!a && hdr->msg_type != DHCPV6_MSG_REBIND) {
1154 status = DHCPV6_STATUS_NOBINDING;
1155 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1156 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1157 hdr->msg_type == DHCPV6_MSG_REBIND) {
1158 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1160 apply_lease(iface, a, true);
1161 } else if (hdr->msg_type == DHCPV6_MSG_RELEASE) {
1163 apply_lease(iface, a, false);
1164 update_state = true;
1165 } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
1167 a->valid_until = now + 3600; // Block address for 1h
1168 update_state = true;
1170 } else if (hdr->msg_type == DHCPV6_MSG_CONFIRM) {
1171 // Always send NOTONLINK for CONFIRM so that clients restart connection
1172 status = DHCPV6_STATUS_NOTONLINK;
1173 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1176 buf += ia_response_len;
1177 buflen -= ia_response_len;
1178 response_len += ia_response_len;
1181 if (hdr->msg_type == DHCPV6_MSG_RELEASE && response_len + 6 < buflen) {
1183 buf[1] = DHCPV6_OPT_STATUS;
1187 buf[5] = DHCPV6_STATUS_OK;
1192 dhcpv6_write_statefile();
1195 return response_len;