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 size_t duid_len = lease->duid_len ? lease->duid_len : 14;
96 struct dhcpv6_assignment *a = calloc(1, sizeof(*a) + duid_len);
98 syslog(LOG_ERR, "Calloc failed for static lease assignment on interface %s",
103 if (lease->dhcpv4_leasetime > 0)
104 a->leasetime = lease->dhcpv4_leasetime;
106 a->clid_len = duid_len;
109 a->assigned = lease->hostid;
111 uint32_t i4a = ntohl(lease->ipaddr.s_addr) & 0xff;
112 a->assigned = ((i4a / 100) << 8) | (((i4a % 100) / 10) << 4) | (i4a % 10);
114 odhcpd_urandom(a->key, sizeof(a->key));
115 memcpy(a->clid_data, lease->duid, lease->duid_len);
116 memcpy(a->mac, lease->mac.ether_addr_octet, sizeof(a->mac));
117 /* Static assignment */
118 a->flags |= OAF_STATIC;
122 // Assign to all interfaces
123 struct dhcpv6_assignment *c;
124 list_for_each_entry(c, &iface->ia_assignments, head) {
125 if (c->length != 128 || c->assigned > a->assigned) {
126 list_add_tail(&a->head, &c->head);
128 } else if (c->assigned == a->assigned) {
129 // Already an assignment with that number
135 if (lease->hostname[0]) {
137 a->hostname = strdup(lease->hostname);
149 static int send_reconf(struct interface *iface, struct dhcpv6_assignment *assign)
152 struct dhcpv6_client_header hdr;
156 uint16_t hardware_type;
161 struct dhcpv6_auth_reconfigure auth;
164 uint8_t clid_data[128];
165 } __attribute__((packed)) reconf_msg = {
166 .hdr = {DHCPV6_MSG_RECONFIGURE, {0, 0, 0}},
167 .srvid_type = htons(DHCPV6_OPT_SERVERID),
168 .srvid_len = htons(10),
169 .duid_type = htons(3),
170 .hardware_type = htons(1),
171 .msg_type = htons(DHCPV6_OPT_RECONF_MSG),
173 .msg_id = DHCPV6_MSG_RENEW,
174 .auth = {htons(DHCPV6_OPT_AUTH),
175 htons(sizeof(reconf_msg.auth) - 4), 3, 1, 0,
176 {htonl(time(NULL)), htonl(++serial)}, 2, {0}},
177 .clid_type = htons(DHCPV6_OPT_CLIENTID),
178 .clid_len = htons(assign->clid_len),
182 odhcpd_get_mac(iface, reconf_msg.mac);
183 memcpy(reconf_msg.clid_data, assign->clid_data, assign->clid_len);
184 struct iovec iov = {&reconf_msg, sizeof(reconf_msg) - 128 + assign->clid_len};
187 uint8_t secretbytes[64];
188 memset(secretbytes, 0, sizeof(secretbytes));
189 memcpy(secretbytes, assign->key, sizeof(assign->key));
191 for (size_t i = 0; i < sizeof(secretbytes); ++i)
192 secretbytes[i] ^= 0x36;
195 md5_hash(secretbytes, sizeof(secretbytes), &md5);
196 md5_hash(iov.iov_base, iov.iov_len, &md5);
197 md5_end(reconf_msg.auth.key, &md5);
199 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
200 secretbytes[i] ^= 0x36;
201 secretbytes[i] ^= 0x5c;
205 md5_hash(secretbytes, sizeof(secretbytes), &md5);
206 md5_hash(reconf_msg.auth.key, 16, &md5);
207 md5_end(reconf_msg.auth.key, &md5);
209 return odhcpd_send(iface->dhcpv6_event.uloop.fd, &assign->peer, &iov, 1, iface);
213 void dhcpv6_write_statefile(void)
218 if (config.dhcp_statefile) {
219 time_t now = odhcpd_time(), wall_time = time(NULL);
220 int fd = open(config.dhcp_statefile, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
224 lockf(fd, F_LOCK, 0);
225 if (ftruncate(fd, 0) < 0) {}
227 FILE *fp = fdopen(fd, "w");
233 struct interface *iface;
234 list_for_each_entry(iface, &interfaces, head) {
235 if (iface->dhcpv6 != RELAYD_SERVER && iface->dhcpv4 != RELAYD_SERVER)
238 if (iface->dhcpv6 == RELAYD_SERVER && iface->ia_assignments.next) {
239 struct dhcpv6_assignment *c;
240 list_for_each_entry(c, &iface->ia_assignments, head) {
241 if (!(c->flags & OAF_BOUND) || c->managed_size < 0)
244 char ipbuf[INET6_ADDRSTRLEN];
247 odhcpd_hexlify(duidbuf, c->clid_data, c->clid_len);
249 // iface DUID iaid hostname lifetime assigned length [addrs...]
250 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s %x %s %ld %x %u ",
251 iface->ifname, duidbuf, ntohl(c->iaid),
252 (c->hostname ? c->hostname : "-"),
253 (c->valid_until > now ?
254 (c->valid_until - now + wall_time) :
255 (INFINITE_VALID(c->valid_until) ? -1 : 0)),
256 c->assigned, (unsigned)c->length);
258 struct in6_addr addr;
259 struct odhcpd_ipaddr *addrs = (c->managed) ? c->managed : iface->ia_addr;
260 size_t addrlen = (c->managed) ? (size_t)c->managed_size : iface->ia_addr_len;
263 for (size_t i = 0; i < addrlen; ++i)
264 if (addrs[i].preferred > addrs[m].preferred ||
265 (addrs[i].preferred == addrs[m].preferred &&
266 memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
269 for (size_t i = 0; i < addrlen; ++i) {
270 if (addrs[i].prefix > 96 || (!INFINITE_VALID(c->valid_until) && c->valid_until <= now) ||
271 (iface->managed < RELAYD_MANAGED_NO_AFLAG && i != m &&
272 addrs[i].prefix == 64))
275 addr = addrs[i].addr;
276 if (c->length == 128)
277 addr.s6_addr32[3] = htonl(c->assigned);
279 addr.s6_addr32[1] |= htonl(c->assigned);
281 inet_ntop(AF_INET6, &addr, ipbuf, sizeof(ipbuf) - 1);
283 if (c->length == 128 && c->hostname) {
287 if (dn_expand(iface->search, iface->search + iface->search_len,
288 iface->search, b, sizeof(b)) > 0)
289 fprintf(fp, "\t%s.%s", c->hostname, b);
291 fprintf(fp, "\t%s\n", c->hostname);
292 md5_hash(ipbuf, strlen(ipbuf), &md5);
293 md5_hash(c->hostname, strlen(c->hostname), &md5);
296 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/%d ", ipbuf,
297 (c->managed_size) ? addrs[i].prefix : c->length);
299 leasebuf[l - 1] = '\n';
300 fwrite(leasebuf, 1, l, fp);
304 if (iface->dhcpv4 == RELAYD_SERVER && iface->dhcpv4_assignments.next) {
305 struct dhcpv4_assignment *c;
306 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
307 if (!(c->flags & OAF_BOUND))
310 char ipbuf[INET6_ADDRSTRLEN];
313 odhcpd_hexlify(duidbuf, c->hwaddr, sizeof(c->hwaddr));
315 // iface DUID iaid hostname lifetime assigned length [addrs...]
316 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s ipv4 %s %ld %x 32 ",
317 iface->ifname, duidbuf,
318 (c->hostname ? c->hostname : "-"),
319 (c->valid_until > now ?
320 (c->valid_until - now + wall_time) :
321 (INFINITE_VALID(c->valid_until) ? -1 : 0)),
324 struct in_addr addr = {htonl(c->addr)};
325 inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf) - 1);
327 if (c->hostname[0]) {
331 if (dn_expand(iface->search, iface->search + iface->search_len,
332 iface->search, b, sizeof(b)) > 0)
333 fprintf(fp, "\t%s.%s", c->hostname, b);
335 fprintf(fp, "\t%s\n", c->hostname);
336 md5_hash(ipbuf, strlen(ipbuf), &md5);
337 md5_hash(c->hostname, strlen(c->hostname), &md5);
340 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/32 ", ipbuf);
341 leasebuf[l - 1] = '\n';
342 fwrite(leasebuf, 1, l, fp);
351 md5_end(newmd5, &md5);
353 if (config.dhcp_cb && memcmp(newmd5, statemd5, sizeof(newmd5))) {
354 memcpy(statemd5, newmd5, sizeof(statemd5));
355 char *argv[2] = {config.dhcp_cb, NULL};
357 execv(argv[0], argv);
364 static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bool add)
366 if (a->length > 64 || a->managed_size < 0)
369 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
370 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
372 for (size_t i = 0; i < addrlen; ++i) {
373 struct in6_addr prefix = addrs[i].addr;
374 prefix.s6_addr32[1] |= htonl(a->assigned);
375 odhcpd_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length,
376 iface, &a->peer.sin6_addr, 1024, add);
381 // More data was received from TCP connection
382 static void managed_handle_pd_data(struct ustream *s, _unused int bytes_new)
384 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
385 time_t now = odhcpd_time();
386 bool first = c->managed_size < 0;
390 char *data = ustream_get_read_buf(s, &pending);
391 char *end = memmem(data, pending, "\n\n", 2);
400 if (c->accept_reconf)
404 for (char *line = strtok_r(data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr)) {
405 c->managed = realloc(c->managed, (c->managed_size + 1) * sizeof(*c->managed));
406 struct odhcpd_ipaddr *n = &c->managed[c->managed_size];
408 char *saveptr2, *x = strtok_r(line, "/", &saveptr2);
409 if (!x || inet_pton(AF_INET6, x, &n->addr) < 1)
412 x = strtok_r(NULL, ",", &saveptr2);
413 if (sscanf(x, "%hhu", &n->prefix) < 1)
416 x = strtok_r(NULL, ",", &saveptr2);
417 if (sscanf(x, "%u", &n->preferred) < 1)
420 x = strtok_r(NULL, ",", &saveptr2);
421 if (sscanf(x, "%u", &n->valid) < 1)
424 if (n->preferred > n->valid)
427 if (UINT32_MAX - now < n->preferred)
428 n->preferred = UINT32_MAX;
432 if (UINT32_MAX - now < n->valid)
433 n->valid = UINT32_MAX;
442 ustream_consume(s, end - data);
445 if (first && c->managed_size == 0)
446 free_dhcpv6_assignment(c);
448 c->valid_until = now + 150;
452 // TCP transmission has ended, either because of success or timeout or other error
453 static void managed_handle_pd_done(struct ustream *s)
455 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
456 c->valid_until = odhcpd_time() + 15;
458 if (c->accept_reconf)
464 static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
466 struct dhcpv6_assignment *c;
468 if (iface->dhcpv6_pd_manager[0]) {
469 int fd = usock(USOCK_UNIX | USOCK_TCP, iface->dhcpv6_pd_manager, NULL);
472 odhcpd_hexlify(iaidbuf, assign->clid_data, assign->clid_len);
474 assign->managed_sock.stream.notify_read = managed_handle_pd_data;
475 assign->managed_sock.stream.notify_state = managed_handle_pd_done;
476 ustream_fd_init(&assign->managed_sock, fd);
477 ustream_printf(&assign->managed_sock.stream, "%s,%x\n::/%d,0,0\n\n",
478 iaidbuf, assign->iaid, assign->length);
479 ustream_write_pending(&assign->managed_sock.stream);
480 assign->managed_size = -1;
481 assign->valid_until = odhcpd_time() + 15;
482 list_add(&assign->head, &iface->ia_assignments);
484 // Wait initial period of up to 250ms for immediate assignment
485 struct pollfd pfd = { .fd = fd, .events = POLLIN };
487 managed_handle_pd_data(&assign->managed_sock.stream, 0);
489 if (fcntl(fd, F_GETFL) >= 0 && assign->managed_size > 0)
494 } else if (iface->ia_addr_len < 1) {
498 // Try honoring the hint first
499 uint32_t current = 1, asize = (1 << (64 - assign->length)) - 1;
500 if (assign->assigned) {
501 list_for_each_entry(c, &iface->ia_assignments, head) {
502 if (c->length == 128 || c->length == 0)
505 if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
506 list_add_tail(&assign->head, &c->head);
507 apply_lease(iface, assign, true);
511 if (c->assigned != 0)
512 current = (c->assigned + (1 << (64 - c->length)));
516 // Fallback to a variable assignment
518 list_for_each_entry(c, &iface->ia_assignments, head) {
519 if (c->length == 128 || c->length == 0)
522 current = (current + asize) & (~asize);
523 if (current + asize < c->assigned) {
524 assign->assigned = current;
525 list_add_tail(&assign->head, &c->head);
526 apply_lease(iface, assign, true);
530 if (c->assigned != 0)
531 current = (c->assigned + (1 << (64 - c->length)));
538 static bool assign_na(struct interface *iface, struct dhcpv6_assignment *assign)
540 // Seed RNG with checksum of DUID
542 for (size_t i = 0; i < assign->clid_len; ++i)
543 seed += assign->clid_data[i];
546 // Try to assign up to 100x
547 for (size_t i = 0; i < 100; ++i) {
549 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
551 struct dhcpv6_assignment *c;
552 list_for_each_entry(c, &iface->ia_assignments, head) {
556 if (c->assigned > try || c->length != 128) {
557 assign->assigned = try;
558 list_add_tail(&assign->head, &c->head);
560 } else if (c->assigned == try) {
569 void dhcpv6_ia_preupdate(struct interface *iface)
571 if (iface->dhcpv6 != RELAYD_SERVER)
574 struct dhcpv6_assignment *c, *border = list_last_entry(
575 &iface->ia_assignments, struct dhcpv6_assignment, head);
576 list_for_each_entry(c, &iface->ia_assignments, head)
577 if (c != border && !iface->managed)
578 apply_lease(iface, c, false);
581 void dhcpv6_ia_postupdate(struct interface *iface, time_t now)
583 if (iface->dhcpv6 != RELAYD_SERVER)
587 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
588 if (iface->ia_addr[i].preferred > (uint32_t)now &&
589 iface->ia_addr[i].prefix < 64 &&
590 iface->ia_addr[i].prefix > minprefix)
591 minprefix = iface->ia_addr[i].prefix;
594 struct dhcpv6_assignment *border = list_last_entry(
595 &iface->ia_assignments, struct dhcpv6_assignment, head);
596 if (minprefix > 32 && minprefix <= 64)
597 border->assigned = 1U << (64 - minprefix);
599 border->assigned = 0;
601 struct list_head reassign = LIST_HEAD_INIT(reassign);
602 struct dhcpv6_assignment *c, *d;
603 list_for_each_entry_safe(c, d, &iface->ia_assignments, head) {
604 if (c->clid_len == 0 || (!INFINITE_VALID(c->valid_until) && c->valid_until < now) ||
608 if (c->length < 128 && c->assigned >= border->assigned && c != border)
609 list_move(&c->head, &reassign);
610 else if (c != border)
611 apply_lease(iface, c, true);
613 if (c->accept_reconf && c->reconf_cnt == 0) {
615 c->reconf_sent = now;
616 send_reconf(iface, c);
618 // Leave all other assignments of that client alone
619 struct dhcpv6_assignment *a;
620 list_for_each_entry(a, &iface->ia_assignments, head)
621 if (a != c && a->clid_len == c->clid_len &&
622 !memcmp(a->clid_data, c->clid_data, a->clid_len))
623 c->reconf_cnt = INT_MAX;
627 while (!list_empty(&reassign)) {
628 c = list_first_entry(&reassign, struct dhcpv6_assignment, head);
630 if (!assign_pd(iface, c)) {
632 list_add(&c->head, &iface->ia_assignments);
636 dhcpv6_write_statefile();
640 static void reconf_timer(struct uloop_timeout *event)
642 time_t now = odhcpd_time();
643 struct interface *iface;
644 list_for_each_entry(iface, &interfaces, head) {
645 if (iface->dhcpv6 != RELAYD_SERVER || iface->ia_assignments.next == NULL)
648 struct dhcpv6_assignment *a, *n;
649 list_for_each_entry_safe(a, n, &iface->ia_assignments, head) {
650 if (!INFINITE_VALID(a->valid_until) && a->valid_until < now) {
651 if ((a->length < 128 && a->clid_len > 0) ||
652 (a->length == 128 && a->clid_len == 0)) {
654 free_dhcpv6_assignment(a);
656 } else if (a->reconf_cnt > 0 && a->reconf_cnt < 8 &&
657 now > a->reconf_sent + (1 << a->reconf_cnt)) {
659 a->reconf_sent = now;
660 send_reconf(iface, a);
664 uloop_timeout_set(event, 2000);
668 static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
669 const struct dhcpv6_ia_hdr *ia, struct dhcpv6_assignment *a,
670 struct interface *iface, bool request)
672 if (buflen < sizeof(*ia) + sizeof(struct dhcpv6_ia_prefix))
675 struct dhcpv6_ia_hdr out = {ia->type, 0, ia->iaid, 0, 0};
676 size_t datalen = sizeof(out);
677 time_t now = odhcpd_time();
680 struct __attribute__((packed)) {
684 } stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
687 memcpy(buf + datalen, &stat, sizeof(stat));
688 datalen += sizeof(stat);
692 if (a->leasetime > 0) {
693 leasetime = a->leasetime;
695 leasetime = iface->dhcpv4_leasetime;
699 else if (leasetime < 60)
702 uint32_t pref = leasetime;
703 uint32_t valid = leasetime;
705 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
706 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
709 for (size_t i = 0; i < addrlen; ++i)
710 if (addrs[i].preferred > addrs[m].preferred ||
711 (addrs[i].preferred == addrs[m].preferred &&
712 memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
715 for (size_t i = 0; i < addrlen; ++i) {
716 uint32_t prefix_pref = addrs[i].preferred;
717 uint32_t prefix_valid = addrs[i].valid;
719 if (addrs[i].prefix > 96 ||
720 addrs[i].preferred <= (uint32_t)now)
723 if (prefix_pref != UINT32_MAX)
726 if (prefix_valid != UINT32_MAX)
729 if (a->length < 128) {
730 struct dhcpv6_ia_prefix p = {
731 .type = htons(DHCPV6_OPT_IA_PREFIX),
732 .len = htons(sizeof(p) - 4),
733 .preferred = htonl(prefix_pref),
734 .valid = htonl(prefix_valid),
735 .prefix = (a->managed_size) ? addrs[i].prefix : a->length,
736 .addr = addrs[i].addr
738 p.addr.s6_addr32[1] |= htonl(a->assigned);
740 size_t entrlen = sizeof(p) - 4;
742 if (datalen + entrlen + 4 > buflen ||
743 (a->assigned == 0 && a->managed_size == 0) ||
744 (!a->managed_size && a->length <= addrs[i].prefix))
747 memcpy(buf + datalen, &p, sizeof(p));
748 datalen += entrlen + 4;
750 struct dhcpv6_ia_addr n = {
751 .type = htons(DHCPV6_OPT_IA_ADDR),
752 .len = htons(sizeof(n) - 4),
753 .addr = addrs[i].addr,
754 .preferred = htonl(prefix_pref),
755 .valid = htonl(prefix_valid)
757 n.addr.s6_addr32[3] = htonl(a->assigned);
758 size_t entrlen = sizeof(n) - 4;
760 if (iface->managed < RELAYD_MANAGED_NO_AFLAG && i != m &&
761 addrs[i].prefix <= 64)
764 if (datalen + entrlen + 4 > buflen || a->assigned == 0)
767 memcpy(buf + datalen, &n, sizeof(n));
768 datalen += entrlen + 4;
771 // Calculate T1 / T2 based on non-deprecated addresses
772 if (prefix_pref > 0) {
773 if (prefix_pref < pref)
776 if (prefix_valid < valid)
777 valid = prefix_valid;
781 if (!INFINITE_VALID(a->valid_until))
782 /* UINT32_MAX is considered as infinite leasetime */
783 a->valid_until = (valid == UINT32_MAX) ? 0 : valid + now;
785 out.t1 = htonl((pref == UINT32_MAX) ? pref : pref * 5 / 10);
786 out.t2 = htonl((pref == UINT32_MAX) ? pref : pref * 8 / 10);
796 uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + 4;
797 uint16_t otype, olen;
798 dhcpv6_for_each_option((uint8_t*)&ia[1], end, otype, olen, odata) {
799 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&odata[-4];
800 struct dhcpv6_ia_addr *n = (struct dhcpv6_ia_addr*)&odata[-4];
801 if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*p) - 4) &&
802 (otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*n) - 4))
807 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
808 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
810 for (size_t i = 0; i < addrlen; ++i) {
811 if (addrs[i].prefix > 96 ||
812 addrs[i].preferred <= (uint32_t)now)
815 struct in6_addr addr = addrs[i].addr;
816 if (ia->type == htons(DHCPV6_OPT_IA_PD)) {
817 addr.s6_addr32[1] |= htonl(a->assigned);
819 if (!memcmp(&p->addr, &addr, sizeof(addr)) &&
820 p->prefix == ((a->managed) ? addrs[i].prefix : a->length))
823 addr.s6_addr32[3] = htonl(a->assigned);
825 if (!memcmp(&n->addr, &addr, sizeof(addr)))
832 if (otype == DHCPV6_OPT_IA_PREFIX) {
833 struct dhcpv6_ia_prefix inv = {
834 .type = htons(DHCPV6_OPT_IA_PREFIX),
835 .len = htons(sizeof(inv) - 4),
842 if (datalen + sizeof(inv) > buflen)
845 memcpy(buf + datalen, &inv, sizeof(inv));
846 datalen += sizeof(inv);
848 struct dhcpv6_ia_addr inv = {
849 .type = htons(DHCPV6_OPT_IA_ADDR),
850 .len = htons(sizeof(inv) - 4),
856 if (datalen + sizeof(inv) > buflen)
859 memcpy(buf + datalen, &inv, sizeof(inv));
860 datalen += sizeof(inv);
867 out.len = htons(datalen - 4);
868 memcpy(buf, &out, sizeof(out));
873 static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now,
874 const char *duidbuf, bool is_pd, struct dhcpv6_assignment *a, int code)
876 const char *type = "UNKNOWN";
877 const char *status = "UNKNOWN";
879 if (msgtype == DHCPV6_MSG_RENEW)
883 case DHCPV6_MSG_SOLICIT:
886 case DHCPV6_MSG_REQUEST:
889 case DHCPV6_MSG_CONFIRM:
892 case DHCPV6_MSG_RENEW:
895 case DHCPV6_MSG_REBIND:
898 case DHCPV6_MSG_RELEASE:
901 case DHCPV6_MSG_DECLINE:
907 case DHCPV6_STATUS_OK:
910 case DHCPV6_STATUS_NOADDRSAVAIL:
911 status = "no addresses available";
913 case DHCPV6_STATUS_NOBINDING:
914 status = "no binding";
916 case DHCPV6_STATUS_NOTONLINK:
917 status = "not on-link";
919 case DHCPV6_STATUS_NOPREFIXAVAIL:
920 status = "no prefix available";
924 char leasebuf[256] = "";
927 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
928 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
930 char addrbuf[INET6_ADDRSTRLEN];
932 for (size_t i = 0; i < addrlen; ++i) {
933 if (addrs[i].prefix > 96 || addrs[i].preferred <= (uint32_t)now)
936 struct in6_addr addr = addrs[i].addr;
937 int prefix = a->managed ? addrs[i].prefix : a->length;
939 addr.s6_addr32[3] = htonl(a->assigned);
941 addr.s6_addr32[1] |= htonl(a->assigned);
943 inet_ntop(AF_INET6, &addr, addrbuf, sizeof(addrbuf));
944 lbsize += snprintf(leasebuf + lbsize, sizeof(leasebuf) - lbsize, "%s/%d ", addrbuf, prefix);
948 syslog(LOG_WARNING, "DHCPV6 %s %s from %s on %s: %s %s", type, (is_pd) ? "IA_PD" : "IA_NA",
949 duidbuf, iface->ifname, status, leasebuf);
954 ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
955 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end)
957 time_t now = odhcpd_time();
958 size_t response_len = 0;
959 const struct dhcpv6_client_header *hdr = data;
960 uint8_t *start = (uint8_t*)&hdr[1], *odata;
961 uint16_t otype, olen;
963 // Find and parse client-id and hostname
964 bool accept_reconf = false;
965 uint8_t *clid_data = NULL, clid_len = 0, mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
967 size_t hostname_len = 0;
968 bool notonlink = false;
971 dhcpv6_for_each_option(start, end, otype, olen, odata) {
972 if (otype == DHCPV6_OPT_CLIENTID) {
976 if (olen == 14 && odata[0] == 0 && odata[1] == 1)
977 memcpy(mac, &odata[8], sizeof(mac));
978 else if (olen == 10 && odata[0] == 0 && odata[1] == 3)
979 memcpy(mac, &odata[4], sizeof(mac));
982 odhcpd_hexlify(duidbuf, odata, olen);
983 } else if (otype == DHCPV6_OPT_FQDN && olen >= 2 && olen <= 255) {
984 uint8_t fqdn_buf[256];
985 memcpy(fqdn_buf, odata, olen);
986 fqdn_buf[olen++] = 0;
988 if (dn_expand(&fqdn_buf[1], &fqdn_buf[olen], &fqdn_buf[1], hostname, sizeof(hostname)) > 0)
989 hostname_len = strcspn(hostname, ".");
990 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
991 accept_reconf = true;
995 if (!clid_data || !clid_len || clid_len > 130)
998 struct dhcpv6_assignment *first = NULL;
999 dhcpv6_for_each_option(start, end, otype, olen, odata) {
1000 bool is_pd = (otype == DHCPV6_OPT_IA_PD);
1001 bool is_na = (otype == DHCPV6_OPT_IA_NA);
1002 bool ia_addr_present = false;
1003 if (!is_pd && !is_na)
1006 struct dhcpv6_ia_hdr *ia = (struct dhcpv6_ia_hdr*)&odata[-4];
1007 size_t ia_response_len = 0;
1008 uint8_t reqlen = (is_pd) ? 62 : 128;
1009 uint32_t reqhint = 0;
1011 // Parse request hint for IA-PD
1014 uint16_t stype, slen;
1015 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1016 if (stype != DHCPV6_OPT_IA_PREFIX || slen < sizeof(struct dhcpv6_ia_prefix) - 4)
1019 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&sdata[-4];
1022 reqhint = ntohl(p->addr.s6_addr32[1]);
1023 if (reqlen > 32 && reqlen <= 64)
1024 reqhint &= (1U << (64 - reqlen)) - 1;
1032 uint16_t stype, slen;
1033 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1034 if (stype != DHCPV6_OPT_IA_ADDR || slen < sizeof(struct dhcpv6_ia_addr) - 4)
1037 ia_addr_present = true;
1042 struct dhcpv6_assignment *c, *a = NULL;
1043 list_for_each_entry(c, &iface->ia_assignments, head) {
1044 if (((c->clid_len == clid_len && !memcmp(c->clid_data, clid_data, clid_len)) ||
1045 (c->clid_len >= clid_len && !c->clid_data[0] && !c->clid_data[1]
1046 && !memcmp(c->mac, mac, sizeof(mac)))) &&
1047 (c->iaid == ia->iaid || INFINITE_VALID(c->valid_until) || now < c->valid_until) &&
1048 ((is_pd && c->length <= 64) || (is_na && c->length == 128))) {
1052 apply_lease(iface, a, false);
1053 memcpy(a->clid_data, clid_data, clid_len);
1054 a->clid_len = clid_len;
1063 // Generic message handling
1064 uint16_t status = DHCPV6_STATUS_OK;
1065 if (a && a->managed_size < 0) {
1067 } else if (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_REQUEST) {
1068 bool assigned = !!a;
1070 if (!a && !iface->no_dynamic_dhcp) { // Create new binding
1071 a = calloc(1, sizeof(*a) + clid_len);
1073 a->clid_len = clid_len;
1077 a->assigned = reqhint;
1078 // Set valid time to current time indicating
1079 // assignment is not having infinite lifetime
1080 a->valid_until = now;
1083 memcpy(a->key, first->key, sizeof(a->key));
1085 odhcpd_urandom(a->key, sizeof(a->key));
1086 memcpy(a->clid_data, clid_data, clid_len);
1089 while (!(assigned = assign_pd(iface, a)) &&
1090 !a->managed_size && ++a->length <= 64);
1092 assigned = assign_na(iface, a);
1094 if (a->managed_size && !assigned)
1099 if (!assigned || iface->ia_addr_len == 0) { // Set error status
1100 status = (is_pd) ? DHCPV6_STATUS_NOPREFIXAVAIL : DHCPV6_STATUS_NOADDRSAVAIL;
1101 } else if (assigned && !first) { //
1102 size_t handshake_len = 4;
1104 buf[1] = DHCPV6_OPT_RECONF_ACCEPT;
1108 if (hdr->msg_type == DHCPV6_MSG_REQUEST) {
1109 struct dhcpv6_auth_reconfigure auth = {
1110 htons(DHCPV6_OPT_AUTH),
1111 htons(sizeof(auth) - 4),
1113 {htonl(time(NULL)), htonl(++serial)},
1117 memcpy(auth.key, a->key, sizeof(a->key));
1118 memcpy(buf + handshake_len, &auth, sizeof(auth));
1119 handshake_len += sizeof(auth);
1122 buf += handshake_len;
1123 buflen -= handshake_len;
1124 response_len += handshake_len;
1129 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1131 // Was only a solicitation: mark binding for removal
1132 if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
1133 a->flags &= ~OAF_BOUND;
1135 if (!(a->flags & OAF_STATIC))
1136 a->valid_until = now;
1137 } else if (assigned && hdr->msg_type == DHCPV6_MSG_REQUEST) {
1138 if (hostname_len > 0) {
1139 a->hostname = realloc(a->hostname, hostname_len + 1);
1141 memcpy(a->hostname, hostname, hostname_len);
1142 a->hostname[hostname_len] = 0;
1145 a->accept_reconf = accept_reconf;
1146 a->flags |= OAF_BOUND;
1147 apply_lease(iface, a, true);
1148 } else if (!assigned && a && a->managed_size == 0) { // Cleanup failed assignment
1149 free_dhcpv6_assignment(a);
1151 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1152 hdr->msg_type == DHCPV6_MSG_RELEASE ||
1153 hdr->msg_type == DHCPV6_MSG_REBIND ||
1154 hdr->msg_type == DHCPV6_MSG_DECLINE) {
1155 if (!a && hdr->msg_type != DHCPV6_MSG_REBIND) {
1156 status = DHCPV6_STATUS_NOBINDING;
1157 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1158 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1159 hdr->msg_type == DHCPV6_MSG_REBIND) {
1160 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1162 a->flags |= OAF_BOUND;
1163 apply_lease(iface, a, true);
1165 } else if (hdr->msg_type == DHCPV6_MSG_RELEASE) {
1166 if (!(a->flags & OAF_STATIC))
1167 a->valid_until = now - 1;
1169 a->flags &= ~OAF_BOUND;
1170 apply_lease(iface, a, false);
1171 } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
1172 a->flags &= ~OAF_BOUND;
1174 if (!(a->flags & OAF_STATIC)) {
1176 a->valid_until = now + 3600; // Block address for 1h
1179 } else if (hdr->msg_type == DHCPV6_MSG_CONFIRM && ia_addr_present) {
1180 // Send NOTONLINK for CONFIRM with addr present so that clients restart connection
1181 status = DHCPV6_STATUS_NOTONLINK;
1182 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1186 buf += ia_response_len;
1187 buflen -= ia_response_len;
1188 response_len += ia_response_len;
1189 dhcpv6_log(hdr->msg_type, iface, now, duidbuf, is_pd, a, status);
1192 if ((hdr->msg_type == DHCPV6_MSG_RELEASE || hdr->msg_type == DHCPV6_MSG_DECLINE || notonlink) &&
1193 response_len + 6 < buflen) {
1195 buf[1] = DHCPV6_OPT_STATUS;
1199 buf[5] = (notonlink) ? DHCPV6_STATUS_NOTONLINK : DHCPV6_STATUS_OK;
1203 dhcpv6_write_statefile();
1206 return response_len;