c8786c3672c32d925e1da1d21d76ace66493de2c
[project/odhcpd.git] / src / dhcpv6-ia.c
1 /**
2 * Copyright (C) 2013 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2016 Hans Dedecker <dedeckeh@gmail.com>
4 *
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.
8 *
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.
13 *
14 */
15
16 #include "odhcpd.h"
17 #include "dhcpv6.h"
18 #include "dhcpv4.h"
19 #include "libubox/md5.h"
20 #include "libubox/usock.h"
21
22 #include <time.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <poll.h>
27 #include <alloca.h>
28 #include <resolv.h>
29 #include <limits.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <stdbool.h>
34 #include <arpa/inet.h>
35 #include <sys/timerfd.h>
36
37 static void free_dhcpv6_assignment(struct dhcpv6_assignment *c);
38 static void reconf_timer(struct uloop_timeout *event);
39 static struct uloop_timeout reconf_event = {.cb = reconf_timer};
40 static uint32_t serial = 0;
41 static uint8_t statemd5[16];
42
43 int dhcpv6_ia_init(void)
44 {
45 uloop_timeout_set(&reconf_event, 2000);
46 return 0;
47 }
48
49 int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
50 {
51 if (!enable && iface->ia_assignments.next) {
52 struct dhcpv6_assignment *c;
53 while (!list_empty(&iface->ia_assignments)) {
54 c = list_first_entry(&iface->ia_assignments, struct dhcpv6_assignment, head);
55 free_dhcpv6_assignment(c);
56 }
57 }
58
59 if (enable && iface->dhcpv6 == RELAYD_SERVER) {
60 if (!iface->ia_assignments.next)
61 INIT_LIST_HEAD(&iface->ia_assignments);
62
63 if (list_empty(&iface->ia_assignments)) {
64 struct dhcpv6_assignment *border = calloc(1, sizeof(*border));
65 if (!border) {
66 syslog(LOG_ERR, "Calloc failed for border on interface %s", iface->ifname);
67 return -1;
68 }
69
70 border->length = 64;
71 list_add(&border->head, &iface->ia_assignments);
72 }
73
74 /* Parse static entries */
75 struct lease *lease;
76 list_for_each_entry(lease, &leases, head) {
77 /* Construct entry */
78 size_t duid_len = lease->duid_len ? lease->duid_len : 14;
79 struct dhcpv6_assignment *a = calloc(1, sizeof(*a) + duid_len);
80 if (!a) {
81 syslog(LOG_ERR, "Calloc failed for static lease assignment on interface %s",
82 iface->ifname);
83 return -1;
84 }
85
86 if (lease->dhcpv4_leasetime > 0)
87 a->leasetime = lease->dhcpv4_leasetime;
88
89 a->clid_len = duid_len;
90 a->length = 128;
91 if (lease->hostid) {
92 a->assigned = lease->hostid;
93 } else {
94 uint32_t i4a = ntohl(lease->ipaddr.s_addr) & 0xff;
95 a->assigned = ((i4a / 100) << 8) | (((i4a % 100) / 10) << 4) | (i4a % 10);
96 }
97
98 odhcpd_urandom(a->key, sizeof(a->key));
99 memcpy(a->clid_data, lease->duid, lease->duid_len);
100 memcpy(a->mac, lease->mac.ether_addr_octet, sizeof(a->mac));
101 /* Static assignment */
102 a->flags |= OAF_STATIC;
103 /* Infinite valid */
104 a->valid_until = 0;
105
106 // Assign to all interfaces
107 struct dhcpv6_assignment *c;
108 list_for_each_entry(c, &iface->ia_assignments, head) {
109 if (c->length != 128 || c->assigned > a->assigned) {
110 list_add_tail(&a->head, &c->head);
111 break;
112 } else if (c->assigned == a->assigned)
113 /* Already an assignment with that number */
114 break;
115 }
116
117 if (a->head.next) {
118 if (lease->hostname[0]) {
119 free(a->hostname);
120 a->hostname = strdup(lease->hostname);
121 }
122 } else
123 free_dhcpv6_assignment(a);
124 }
125 }
126 return 0;
127 }
128
129 static void free_dhcpv6_assignment(struct dhcpv6_assignment *c)
130 {
131 if (c->managed_sock.fd.registered) {
132 ustream_free(&c->managed_sock.stream);
133 close(c->managed_sock.fd.fd);
134 }
135
136 if (c->head.next)
137 list_del(&c->head);
138
139 free(c->managed);
140 free(c->hostname);
141 free(c);
142 }
143
144 static inline bool valid_addr(const struct odhcpd_ipaddr *addr, time_t now)
145 {
146 return (addr->prefix <= 96 && addr->preferred > (uint32_t)now);
147 }
148
149 static size_t elect_addr(const struct odhcpd_ipaddr *addrs, const size_t addrlen)
150 {
151 size_t i, m;
152
153 for (i = 0, m = 0; i < addrlen; ++i) {
154 if (addrs[i].preferred > addrs[m].preferred ||
155 (addrs[i].preferred == addrs[m].preferred &&
156 memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
157 m = i;
158 }
159
160 return m;
161 }
162
163 static int send_reconf(struct interface *iface, struct dhcpv6_assignment *assign)
164 {
165 struct {
166 struct dhcpv6_client_header hdr;
167 uint16_t srvid_type;
168 uint16_t srvid_len;
169 uint16_t duid_type;
170 uint16_t hardware_type;
171 uint8_t mac[6];
172 uint16_t msg_type;
173 uint16_t msg_len;
174 uint8_t msg_id;
175 struct dhcpv6_auth_reconfigure auth;
176 uint16_t clid_type;
177 uint16_t clid_len;
178 uint8_t clid_data[128];
179 } __attribute__((packed)) reconf_msg = {
180 .hdr = {DHCPV6_MSG_RECONFIGURE, {0, 0, 0}},
181 .srvid_type = htons(DHCPV6_OPT_SERVERID),
182 .srvid_len = htons(10),
183 .duid_type = htons(3),
184 .hardware_type = htons(1),
185 .msg_type = htons(DHCPV6_OPT_RECONF_MSG),
186 .msg_len = htons(1),
187 .msg_id = DHCPV6_MSG_RENEW,
188 .auth = {htons(DHCPV6_OPT_AUTH),
189 htons(sizeof(reconf_msg.auth) - 4), 3, 1, 0,
190 {htonl(time(NULL)), htonl(++serial)}, 2, {0}},
191 .clid_type = htons(DHCPV6_OPT_CLIENTID),
192 .clid_len = htons(assign->clid_len),
193 .clid_data = {0},
194 };
195
196 odhcpd_get_mac(iface, reconf_msg.mac);
197 memcpy(reconf_msg.clid_data, assign->clid_data, assign->clid_len);
198 struct iovec iov = {&reconf_msg, sizeof(reconf_msg) - 128 + assign->clid_len};
199
200 md5_ctx_t md5;
201 uint8_t secretbytes[64];
202 memset(secretbytes, 0, sizeof(secretbytes));
203 memcpy(secretbytes, assign->key, sizeof(assign->key));
204
205 for (size_t i = 0; i < sizeof(secretbytes); ++i)
206 secretbytes[i] ^= 0x36;
207
208 md5_begin(&md5);
209 md5_hash(secretbytes, sizeof(secretbytes), &md5);
210 md5_hash(iov.iov_base, iov.iov_len, &md5);
211 md5_end(reconf_msg.auth.key, &md5);
212
213 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
214 secretbytes[i] ^= 0x36;
215 secretbytes[i] ^= 0x5c;
216 }
217
218 md5_begin(&md5);
219 md5_hash(secretbytes, sizeof(secretbytes), &md5);
220 md5_hash(reconf_msg.auth.key, 16, &md5);
221 md5_end(reconf_msg.auth.key, &md5);
222
223 return odhcpd_send(iface->dhcpv6_event.uloop.fd, &assign->peer, &iov, 1, iface);
224 }
225
226 void dhcpv6_write_statefile(void)
227 {
228 md5_ctx_t md5;
229 md5_begin(&md5);
230
231 if (config.dhcp_statefile) {
232 time_t now = odhcpd_time(), wall_time = time(NULL);
233 int fd = open(config.dhcp_statefile, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
234 if (fd < 0)
235 return;
236
237 lockf(fd, F_LOCK, 0);
238 if (ftruncate(fd, 0) < 0) {}
239
240 FILE *fp = fdopen(fd, "w");
241 if (!fp) {
242 close(fd);
243 return;
244 }
245
246 struct interface *iface;
247 list_for_each_entry(iface, &interfaces, head) {
248 if (iface->dhcpv6 != RELAYD_SERVER && iface->dhcpv4 != RELAYD_SERVER)
249 continue;
250
251 if (iface->dhcpv6 == RELAYD_SERVER && iface->ia_assignments.next) {
252 struct dhcpv6_assignment *c;
253 list_for_each_entry(c, &iface->ia_assignments, head) {
254 if (!(c->flags & OAF_BOUND) || c->managed_size < 0)
255 continue;
256
257 char ipbuf[INET6_ADDRSTRLEN];
258 char leasebuf[512];
259 char duidbuf[264];
260 odhcpd_hexlify(duidbuf, c->clid_data, c->clid_len);
261
262 // iface DUID iaid hostname lifetime assigned length [addrs...]
263 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s %x %s %ld %x %u ",
264 iface->ifname, duidbuf, ntohl(c->iaid),
265 (c->hostname ? c->hostname : "-"),
266 (c->valid_until > now ?
267 (c->valid_until - now + wall_time) :
268 (INFINITE_VALID(c->valid_until) ? -1 : 0)),
269 c->assigned, (unsigned)c->length);
270
271 struct in6_addr addr;
272 struct odhcpd_ipaddr *addrs = (c->managed) ? c->managed : iface->ia_addr;
273 size_t addrlen = (c->managed) ? (size_t)c->managed_size : iface->ia_addr_len;
274 size_t m = elect_addr(addrs, addrlen);
275
276 for (size_t i = 0; i < addrlen; ++i) {
277 if (!valid_addr(&addrs[i], now) || (!INFINITE_VALID(c->valid_until) && c->valid_until <= now) ||
278 (iface->managed < RELAYD_MANAGED_NO_AFLAG && i != m &&
279 addrs[i].prefix <= 64))
280 continue;
281
282 addr = addrs[i].addr;
283 if (c->length == 128)
284 addr.s6_addr32[3] = htonl(c->assigned);
285 else
286 addr.s6_addr32[1] |= htonl(c->assigned);
287
288 inet_ntop(AF_INET6, &addr, ipbuf, sizeof(ipbuf) - 1);
289
290 if (c->length == 128 && c->hostname) {
291 fputs(ipbuf, fp);
292
293 char b[256];
294 if (dn_expand(iface->search, iface->search + iface->search_len,
295 iface->search, b, sizeof(b)) > 0)
296 fprintf(fp, "\t%s.%s", c->hostname, b);
297
298 fprintf(fp, "\t%s\n", c->hostname);
299 md5_hash(ipbuf, strlen(ipbuf), &md5);
300 md5_hash(c->hostname, strlen(c->hostname), &md5);
301 }
302
303 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/%d ", ipbuf,
304 (c->managed_size) ? addrs[i].prefix : c->length);
305 }
306 leasebuf[l - 1] = '\n';
307 fwrite(leasebuf, 1, l, fp);
308 }
309 }
310
311 if (iface->dhcpv4 == RELAYD_SERVER && iface->dhcpv4_assignments.next) {
312 struct dhcpv4_assignment *c;
313 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
314 if (!(c->flags & OAF_BOUND))
315 continue;
316
317 char ipbuf[INET6_ADDRSTRLEN];
318 char leasebuf[512];
319 char duidbuf[16];
320 odhcpd_hexlify(duidbuf, c->hwaddr, sizeof(c->hwaddr));
321
322 // iface DUID iaid hostname lifetime assigned length [addrs...]
323 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s ipv4 %s %ld %x 32 ",
324 iface->ifname, duidbuf,
325 (c->hostname ? c->hostname : "-"),
326 (c->valid_until > now ?
327 (c->valid_until - now + wall_time) :
328 (INFINITE_VALID(c->valid_until) ? -1 : 0)),
329 c->addr);
330
331 struct in_addr addr = {htonl(c->addr)};
332 inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf) - 1);
333
334 if (c->hostname) {
335 fputs(ipbuf, fp);
336
337 char b[256];
338 if (dn_expand(iface->search, iface->search + iface->search_len,
339 iface->search, b, sizeof(b)) > 0)
340 fprintf(fp, "\t%s.%s", c->hostname, b);
341
342 fprintf(fp, "\t%s\n", c->hostname);
343 md5_hash(ipbuf, strlen(ipbuf), &md5);
344 md5_hash(c->hostname, strlen(c->hostname), &md5);
345 }
346
347 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/32 ", ipbuf);
348 leasebuf[l - 1] = '\n';
349 fwrite(leasebuf, 1, l, fp);
350 }
351 }
352 }
353
354 fclose(fp);
355 }
356
357 uint8_t newmd5[16];
358 md5_end(newmd5, &md5);
359
360 if (config.dhcp_cb && memcmp(newmd5, statemd5, sizeof(newmd5))) {
361 memcpy(statemd5, newmd5, sizeof(statemd5));
362 char *argv[2] = {config.dhcp_cb, NULL};
363 if (!vfork()) {
364 execv(argv[0], argv);
365 _exit(128);
366 }
367 }
368 }
369
370
371 static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bool add)
372 {
373 if (a->length > 64 || a->managed_size < 0)
374 return;
375
376 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
377 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
378
379 for (size_t i = 0; i < addrlen; ++i) {
380 struct in6_addr prefix = addrs[i].addr;
381 prefix.s6_addr32[1] |= htonl(a->assigned);
382 odhcpd_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length,
383 iface, &a->peer.sin6_addr, 1024, add);
384 }
385 }
386
387 // More data was received from TCP connection
388 static void managed_handle_pd_data(struct ustream *s, _unused int bytes_new)
389 {
390 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
391 time_t now = odhcpd_time();
392 bool first = c->managed_size < 0;
393
394 for (;;) {
395 int pending;
396 char *data = ustream_get_read_buf(s, &pending);
397 char *end = memmem(data, pending, "\n\n", 2);
398
399 if (!end)
400 break;
401
402 end += 2;
403 end[-1] = 0;
404
405 c->managed_size = 0;
406 if (c->accept_reconf)
407 c->reconf_cnt = 1;
408
409 char *saveptr;
410 for (char *line = strtok_r(data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr)) {
411 c->managed = realloc(c->managed, (c->managed_size + 1) * sizeof(*c->managed));
412 struct odhcpd_ipaddr *n = &c->managed[c->managed_size];
413
414 char *saveptr2, *x = strtok_r(line, "/", &saveptr2);
415 if (!x || inet_pton(AF_INET6, x, &n->addr) < 1)
416 continue;
417
418 x = strtok_r(NULL, ",", &saveptr2);
419 if (sscanf(x, "%hhu", &n->prefix) < 1)
420 continue;
421
422 x = strtok_r(NULL, ",", &saveptr2);
423 if (sscanf(x, "%u", &n->preferred) < 1)
424 continue;
425
426 x = strtok_r(NULL, ",", &saveptr2);
427 if (sscanf(x, "%u", &n->valid) < 1)
428 continue;
429
430 if (n->preferred > n->valid)
431 continue;
432
433 if (UINT32_MAX - now < n->preferred)
434 n->preferred = UINT32_MAX;
435 else
436 n->preferred += now;
437
438 if (UINT32_MAX - now < n->valid)
439 n->valid = UINT32_MAX;
440 else
441 n->valid += now;
442
443 n->dprefix = 0;
444
445 ++c->managed_size;
446 }
447
448 ustream_consume(s, end - data);
449 }
450
451 if (first && c->managed_size == 0)
452 free_dhcpv6_assignment(c);
453 else if (first & !(c->flags & OAF_STATIC))
454 c->valid_until = now + 150;
455 }
456
457
458 /* TCP transmission has ended, either because of success or timeout or other error */
459 static void managed_handle_pd_done(struct ustream *s)
460 {
461 struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
462
463 if (!(c->flags & OAF_STATIC))
464 c->valid_until = odhcpd_time() + 15;
465
466 c->managed_size = 0;
467
468 if (c->accept_reconf)
469 c->reconf_cnt = 1;
470 }
471
472 static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
473 {
474 struct dhcpv6_assignment *c;
475
476 if (iface->dhcpv6_pd_manager[0]) {
477 int fd = usock(USOCK_UNIX | USOCK_TCP, iface->dhcpv6_pd_manager, NULL);
478 if (fd >= 0) {
479 char iaidbuf[298];
480 odhcpd_hexlify(iaidbuf, assign->clid_data, assign->clid_len);
481
482 assign->managed_sock.stream.notify_read = managed_handle_pd_data;
483 assign->managed_sock.stream.notify_state = managed_handle_pd_done;
484 ustream_fd_init(&assign->managed_sock, fd);
485 ustream_printf(&assign->managed_sock.stream, "%s,%x\n::/%d,0,0\n\n",
486 iaidbuf, assign->iaid, assign->length);
487 ustream_write_pending(&assign->managed_sock.stream);
488 assign->managed_size = -1;
489
490 if (!(assign->flags & OAF_STATIC))
491 assign->valid_until = odhcpd_time() + 15;
492
493 list_add(&assign->head, &iface->ia_assignments);
494
495 /* Wait initial period of up to 250ms for immediate assignment */
496 struct pollfd pfd = { .fd = fd, .events = POLLIN };
497 poll(&pfd, 1, 250);
498 managed_handle_pd_data(&assign->managed_sock.stream, 0);
499
500 if (fcntl(fd, F_GETFL) >= 0 && assign->managed_size > 0)
501 return true;
502 }
503
504 return false;
505 } else if (iface->ia_addr_len < 1)
506 return false;
507
508 /* Try honoring the hint first */
509 uint32_t current = 1, asize = (1 << (64 - assign->length)) - 1;
510 if (assign->assigned) {
511 list_for_each_entry(c, &iface->ia_assignments, head) {
512 if (c->length == 128 || c->length == 0)
513 continue;
514
515 if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
516 list_add_tail(&assign->head, &c->head);
517 apply_lease(iface, assign, true);
518 return true;
519 }
520
521 if (c->assigned != 0)
522 current = (c->assigned + (1 << (64 - c->length)));
523 }
524 }
525
526 /* Fallback to a variable assignment */
527 current = 1;
528 list_for_each_entry(c, &iface->ia_assignments, head) {
529 if (c->length == 128 || c->length == 0)
530 continue;
531
532 current = (current + asize) & (~asize);
533 if (current + asize < c->assigned) {
534 assign->assigned = current;
535 list_add_tail(&assign->head, &c->head);
536 apply_lease(iface, assign, true);
537 return true;
538 }
539
540 if (c->assigned != 0)
541 current = (c->assigned + (1 << (64 - c->length)));
542 }
543
544 return false;
545 }
546
547 static bool assign_na(struct interface *iface, struct dhcpv6_assignment *assign)
548 {
549 /* Seed RNG with checksum of DUID */
550 uint32_t seed = 0;
551 for (size_t i = 0; i < assign->clid_len; ++i)
552 seed += assign->clid_data[i];
553 srand(seed);
554
555 /* Try to assign up to 100x */
556 for (size_t i = 0; i < 100; ++i) {
557 uint32_t try;
558 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
559
560 struct dhcpv6_assignment *c;
561 list_for_each_entry(c, &iface->ia_assignments, head) {
562 if (c->length == 0)
563 continue;
564
565 if (c->assigned > try || c->length != 128) {
566 assign->assigned = try;
567 list_add_tail(&assign->head, &c->head);
568 return true;
569 } else if (c->assigned == try)
570 break;
571 }
572 }
573
574 return false;
575 }
576
577 void dhcpv6_ia_preupdate(struct interface *iface)
578 {
579 if (iface->dhcpv6 != RELAYD_SERVER)
580 return;
581
582 struct dhcpv6_assignment *c, *border = list_last_entry(
583 &iface->ia_assignments, struct dhcpv6_assignment, head);
584
585 list_for_each_entry(c, &iface->ia_assignments, head)
586 if (c != border && !iface->managed)
587 apply_lease(iface, c, false);
588 }
589
590 void dhcpv6_ia_postupdate(struct interface *iface, time_t now)
591 {
592 if (iface->dhcpv6 != RELAYD_SERVER)
593 return;
594
595 int minprefix = -1;
596 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
597 if (iface->ia_addr[i].preferred > (uint32_t)now &&
598 iface->ia_addr[i].prefix < 64 &&
599 iface->ia_addr[i].prefix > minprefix)
600 minprefix = iface->ia_addr[i].prefix;
601 }
602
603 struct dhcpv6_assignment *border = list_last_entry(
604 &iface->ia_assignments, struct dhcpv6_assignment, head);
605
606 if (minprefix > 32 && minprefix <= 64)
607 border->assigned = 1U << (64 - minprefix);
608 else
609 border->assigned = 0;
610
611 struct list_head reassign = LIST_HEAD_INIT(reassign);
612 struct dhcpv6_assignment *c, *d;
613 list_for_each_entry_safe(c, d, &iface->ia_assignments, head) {
614 if (c->clid_len == 0 || (!INFINITE_VALID(c->valid_until) && c->valid_until < now) ||
615 c->managed_size)
616 continue;
617
618 if (c->length < 128 && c->assigned >= border->assigned && c != border)
619 list_move(&c->head, &reassign);
620 else if (c != border)
621 apply_lease(iface, c, true);
622
623 if (c->accept_reconf && c->reconf_cnt == 0) {
624 c->reconf_cnt = 1;
625 c->reconf_sent = now;
626 send_reconf(iface, c);
627
628 /* Leave all other assignments of that client alone */
629 struct dhcpv6_assignment *a;
630 list_for_each_entry(a, &iface->ia_assignments, head)
631 if (a != c && a->clid_len == c->clid_len &&
632 !memcmp(a->clid_data, c->clid_data, a->clid_len))
633 c->reconf_cnt = INT_MAX;
634 }
635 }
636
637 while (!list_empty(&reassign)) {
638 c = list_first_entry(&reassign, struct dhcpv6_assignment, head);
639 list_del(&c->head);
640 if (!assign_pd(iface, c)) {
641 c->assigned = 0;
642 list_add(&c->head, &iface->ia_assignments);
643 }
644 }
645
646 dhcpv6_write_statefile();
647 }
648
649 static void reconf_timer(struct uloop_timeout *event)
650 {
651 time_t now = odhcpd_time();
652 struct interface *iface;
653 list_for_each_entry(iface, &interfaces, head) {
654 if (iface->dhcpv6 != RELAYD_SERVER || iface->ia_assignments.next == NULL)
655 continue;
656
657 struct dhcpv6_assignment *a, *n;
658 list_for_each_entry_safe(a, n, &iface->ia_assignments, head) {
659 if (!INFINITE_VALID(a->valid_until) && a->valid_until < now) {
660 if ((a->length < 128 && a->clid_len > 0) ||
661 (a->length == 128 && a->clid_len == 0))
662 free_dhcpv6_assignment(a);
663
664 } else if (a->reconf_cnt > 0 && a->reconf_cnt < 8 &&
665 now > a->reconf_sent + (1 << a->reconf_cnt)) {
666 ++a->reconf_cnt;
667 a->reconf_sent = now;
668 send_reconf(iface, a);
669 }
670 }
671 }
672 uloop_timeout_set(event, 2000);
673 }
674
675 static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
676 const struct dhcpv6_ia_hdr *ia, struct dhcpv6_assignment *a,
677 struct interface *iface, bool request)
678 {
679 if (buflen < sizeof(*ia) + sizeof(struct dhcpv6_ia_prefix))
680 return 0;
681
682 struct dhcpv6_ia_hdr out = {ia->type, 0, ia->iaid, 0, 0};
683 size_t datalen = sizeof(out);
684 time_t now = odhcpd_time();
685
686 if (status) {
687 struct __attribute__((packed)) {
688 uint16_t type;
689 uint16_t len;
690 uint16_t value;
691 } stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
692 htons(status)};
693
694 memcpy(buf + datalen, &stat, sizeof(stat));
695 datalen += sizeof(stat);
696 } else {
697 if (a) {
698 uint32_t leasetime;
699 if (a->leasetime > 0)
700 leasetime = a->leasetime;
701 else
702 leasetime = iface->dhcpv4_leasetime;
703
704 if (leasetime == 0)
705 leasetime = 3600;
706 else if (leasetime < 60)
707 leasetime = 60;
708
709 uint32_t pref = leasetime;
710 uint32_t valid = leasetime;
711
712 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
713 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
714 size_t m = elect_addr(addrs, addrlen);
715
716 for (size_t i = 0; i < addrlen; ++i) {
717 uint32_t prefix_pref = addrs[i].preferred;
718 uint32_t prefix_valid = addrs[i].valid;
719
720 if (!valid_addr(&addrs[i], now))
721 continue;
722
723 if (prefix_pref != UINT32_MAX)
724 prefix_pref -= now;
725
726 if (prefix_valid != UINT32_MAX)
727 prefix_valid -= now;
728
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
737 };
738 p.addr.s6_addr32[1] |= htonl(a->assigned);
739
740 size_t entrlen = sizeof(p) - 4;
741
742 if (datalen + entrlen + 4 > buflen ||
743 (a->assigned == 0 && a->managed_size == 0) ||
744 (!a->managed_size && a->length <= addrs[i].prefix))
745 continue;
746
747 memcpy(buf + datalen, &p, sizeof(p));
748 datalen += entrlen + 4;
749 } else {
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)
756 };
757 n.addr.s6_addr32[3] = htonl(a->assigned);
758 size_t entrlen = sizeof(n) - 4;
759
760 if (iface->managed < RELAYD_MANAGED_NO_AFLAG && i != m &&
761 addrs[i].prefix <= 64)
762 continue;
763
764 if (datalen + entrlen + 4 > buflen || a->assigned == 0)
765 continue;
766
767 memcpy(buf + datalen, &n, sizeof(n));
768 datalen += entrlen + 4;
769 }
770
771 /* Calculate T1 / T2 based on non-deprecated addresses */
772 if (prefix_pref > 0) {
773 if (prefix_pref < pref)
774 pref = prefix_pref;
775
776 if (prefix_valid < valid)
777 valid = prefix_valid;
778 }
779 }
780
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;
784
785 out.t1 = htonl((pref == UINT32_MAX) ? pref : pref * 5 / 10);
786 out.t2 = htonl((pref == UINT32_MAX) ? pref : pref * 8 / 10);
787
788 if (!out.t1)
789 out.t1 = htonl(1);
790
791 if (!out.t2)
792 out.t2 = htonl(1);
793 }
794
795 if (!request) {
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))
803 continue;
804
805 bool found = false;
806 if (a) {
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;
809
810 for (size_t i = 0; i < addrlen; ++i) {
811 if (!valid_addr(&addrs[i], now))
812 continue;
813
814 struct in6_addr addr = addrs[i].addr;
815 if (ia->type == htons(DHCPV6_OPT_IA_PD)) {
816 addr.s6_addr32[1] |= htonl(a->assigned);
817
818 if (!memcmp(&p->addr, &addr, sizeof(addr)) &&
819 p->prefix == ((a->managed) ? addrs[i].prefix : a->length))
820 found = true;
821 } else {
822 addr.s6_addr32[3] = htonl(a->assigned);
823
824 if (!memcmp(&n->addr, &addr, sizeof(addr)))
825 found = true;
826 }
827 }
828 }
829
830 if (!found) {
831 if (otype == DHCPV6_OPT_IA_PREFIX) {
832 struct dhcpv6_ia_prefix inv = {
833 .type = htons(DHCPV6_OPT_IA_PREFIX),
834 .len = htons(sizeof(inv) - 4),
835 .preferred = 0,
836 .valid = 0,
837 .prefix = p->prefix,
838 .addr = p->addr
839 };
840
841 if (datalen + sizeof(inv) > buflen)
842 continue;
843
844 memcpy(buf + datalen, &inv, sizeof(inv));
845 datalen += sizeof(inv);
846 } else {
847 struct dhcpv6_ia_addr inv = {
848 .type = htons(DHCPV6_OPT_IA_ADDR),
849 .len = htons(sizeof(inv) - 4),
850 .addr = n->addr,
851 .preferred = 0,
852 .valid = 0
853 };
854
855 if (datalen + sizeof(inv) > buflen)
856 continue;
857
858 memcpy(buf + datalen, &inv, sizeof(inv));
859 datalen += sizeof(inv);
860 }
861 }
862 }
863 }
864 }
865
866 out.len = htons(datalen - 4);
867 memcpy(buf, &out, sizeof(out));
868 return datalen;
869 }
870
871 static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now,
872 const char *duidbuf, bool is_pd, struct dhcpv6_assignment *a, int code)
873 {
874 const char *type = "UNKNOWN";
875 const char *status = "UNKNOWN";
876
877 if (msgtype == DHCPV6_MSG_RENEW)
878 return;
879
880 switch (msgtype) {
881 case DHCPV6_MSG_SOLICIT:
882 type = "SOLICIT";
883 break;
884 case DHCPV6_MSG_REQUEST:
885 type = "REQUEST";
886 break;
887 case DHCPV6_MSG_CONFIRM:
888 type = "CONFIRM";
889 break;
890 case DHCPV6_MSG_RENEW:
891 type = "RENEW";
892 break;
893 case DHCPV6_MSG_REBIND:
894 type = "REBIND";
895 break;
896 case DHCPV6_MSG_RELEASE:
897 type = "RELEASE";
898 break;
899 case DHCPV6_MSG_DECLINE:
900 type = "DECLINE";
901 break;
902 }
903
904 switch (code) {
905 case DHCPV6_STATUS_OK:
906 status = "ok";
907 break;
908 case DHCPV6_STATUS_NOADDRSAVAIL:
909 status = "no addresses available";
910 break;
911 case DHCPV6_STATUS_NOBINDING:
912 status = "no binding";
913 break;
914 case DHCPV6_STATUS_NOTONLINK:
915 status = "not on-link";
916 break;
917 case DHCPV6_STATUS_NOPREFIXAVAIL:
918 status = "no prefix available";
919 break;
920 }
921
922 char leasebuf[256] = "";
923
924 if (a) {
925 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
926 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
927 size_t lbsize = 0;
928 char addrbuf[INET6_ADDRSTRLEN];
929
930 for (size_t i = 0; i < addrlen; ++i) {
931 if (!valid_addr(&addrs[i], now))
932 continue;
933
934 struct in6_addr addr = addrs[i].addr;
935 int prefix = a->managed ? addrs[i].prefix : a->length;
936 if (prefix == 128)
937 addr.s6_addr32[3] = htonl(a->assigned);
938 else
939 addr.s6_addr32[1] |= htonl(a->assigned);
940
941 inet_ntop(AF_INET6, &addr, addrbuf, sizeof(addrbuf));
942 lbsize += snprintf(leasebuf + lbsize, sizeof(leasebuf) - lbsize, "%s/%d ", addrbuf, prefix);
943 }
944 }
945
946 syslog(LOG_WARNING, "DHCPV6 %s %s from %s on %s: %s %s", type, (is_pd) ? "IA_PD" : "IA_NA",
947 duidbuf, iface->ifname, status, leasebuf);
948 }
949
950 ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
951 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end)
952 {
953 time_t now = odhcpd_time();
954 size_t response_len = 0;
955 const struct dhcpv6_client_header *hdr = data;
956 uint8_t *start = (uint8_t*)&hdr[1], *odata;
957 uint16_t otype, olen;
958 /* Find and parse client-id and hostname */
959 bool accept_reconf = false;
960 uint8_t *clid_data = NULL, clid_len = 0, mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
961 char hostname[256];
962 size_t hostname_len = 0;
963 bool notonlink = false;
964 char duidbuf[261];
965
966 dhcpv6_for_each_option(start, end, otype, olen, odata) {
967 if (otype == DHCPV6_OPT_CLIENTID) {
968 clid_data = odata;
969 clid_len = olen;
970
971 if (olen == 14 && odata[0] == 0 && odata[1] == 1)
972 memcpy(mac, &odata[8], sizeof(mac));
973 else if (olen == 10 && odata[0] == 0 && odata[1] == 3)
974 memcpy(mac, &odata[4], sizeof(mac));
975
976 if (olen <= 130)
977 odhcpd_hexlify(duidbuf, odata, olen);
978 } else if (otype == DHCPV6_OPT_FQDN && olen >= 2 && olen <= 255) {
979 uint8_t fqdn_buf[256];
980 memcpy(fqdn_buf, odata, olen);
981 fqdn_buf[olen++] = 0;
982
983 if (dn_expand(&fqdn_buf[1], &fqdn_buf[olen], &fqdn_buf[1], hostname, sizeof(hostname)) > 0)
984 hostname_len = strcspn(hostname, ".");
985 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT)
986 accept_reconf = true;
987 }
988
989 if (!clid_data || !clid_len || clid_len > 130)
990 goto out;
991
992 struct dhcpv6_assignment *first = NULL;
993 dhcpv6_for_each_option(start, end, otype, olen, odata) {
994 bool is_pd = (otype == DHCPV6_OPT_IA_PD);
995 bool is_na = (otype == DHCPV6_OPT_IA_NA);
996 bool ia_addr_present = false;
997 if (!is_pd && !is_na)
998 continue;
999
1000 struct dhcpv6_ia_hdr *ia = (struct dhcpv6_ia_hdr*)&odata[-4];
1001 size_t ia_response_len = 0;
1002 uint8_t reqlen = (is_pd) ? 62 : 128;
1003 uint32_t reqhint = 0;
1004
1005 /* Parse request hint for IA-PD */
1006 if (is_pd) {
1007 uint8_t *sdata;
1008 uint16_t stype, slen;
1009 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1010 if (stype != DHCPV6_OPT_IA_PREFIX || slen < sizeof(struct dhcpv6_ia_prefix) - 4)
1011 continue;
1012
1013 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&sdata[-4];
1014 if (p->prefix) {
1015 reqlen = p->prefix;
1016 reqhint = ntohl(p->addr.s6_addr32[1]);
1017 if (reqlen > 32 && reqlen <= 64)
1018 reqhint &= (1U << (64 - reqlen)) - 1;
1019 }
1020 }
1021
1022 if (reqlen > 64)
1023 reqlen = 64;
1024 } else if (is_na) {
1025 uint8_t *sdata;
1026 uint16_t stype, slen;
1027 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1028 if (stype != DHCPV6_OPT_IA_ADDR || slen < sizeof(struct dhcpv6_ia_addr) - 4)
1029 continue;
1030
1031 ia_addr_present = true;
1032 }
1033 }
1034
1035 /* Find assignment */
1036 struct dhcpv6_assignment *c, *a = NULL;
1037 list_for_each_entry(c, &iface->ia_assignments, head) {
1038 if (((c->clid_len == clid_len && !memcmp(c->clid_data, clid_data, clid_len)) ||
1039 (c->clid_len >= clid_len && !c->clid_data[0] && !c->clid_data[1]
1040 && !memcmp(c->mac, mac, sizeof(mac)))) &&
1041 (c->iaid == ia->iaid || INFINITE_VALID(c->valid_until) || now < c->valid_until) &&
1042 ((is_pd && c->length <= 64) || (is_na && c->length == 128))) {
1043 a = c;
1044
1045 // Reset state
1046 apply_lease(iface, a, false);
1047 memcpy(a->clid_data, clid_data, clid_len);
1048 a->clid_len = clid_len;
1049 a->iaid = ia->iaid;
1050 a->peer = *addr;
1051 a->reconf_cnt = 0;
1052 a->reconf_sent = 0;
1053 break;
1054 }
1055 }
1056
1057 /* Generic message handling */
1058 uint16_t status = DHCPV6_STATUS_OK;
1059 if (a && a->managed_size < 0)
1060 return -1;
1061
1062 if (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_REQUEST) {
1063 bool assigned = !!a;
1064
1065 if (!a && !iface->no_dynamic_dhcp) {
1066 /* Create new binding */
1067 a = calloc(1, sizeof(*a) + clid_len);
1068 if (a) {
1069 a->clid_len = clid_len;
1070 a->iaid = ia->iaid;
1071 a->length = reqlen;
1072 a->peer = *addr;
1073 a->assigned = reqhint;
1074 /* Set valid time to current time indicating */
1075 /* assignment is not having infinite lifetime */
1076 a->valid_until = now;
1077
1078 if (first)
1079 memcpy(a->key, first->key, sizeof(a->key));
1080 else
1081 odhcpd_urandom(a->key, sizeof(a->key));
1082 memcpy(a->clid_data, clid_data, clid_len);
1083
1084 if (is_pd)
1085 while (!(assigned = assign_pd(iface, a)) &&
1086 !a->managed_size && ++a->length <= 64);
1087 else
1088 assigned = assign_na(iface, a);
1089
1090 if (a->managed_size && !assigned)
1091 return -1;
1092 }
1093 }
1094
1095 if (!assigned || iface->ia_addr_len == 0)
1096 /* Set error status */
1097 status = (is_pd) ? DHCPV6_STATUS_NOPREFIXAVAIL : DHCPV6_STATUS_NOADDRSAVAIL;
1098 else if (assigned && !first) {
1099 size_t handshake_len = 4;
1100 buf[0] = 0;
1101 buf[1] = DHCPV6_OPT_RECONF_ACCEPT;
1102 buf[2] = 0;
1103 buf[3] = 0;
1104
1105 if (hdr->msg_type == DHCPV6_MSG_REQUEST) {
1106 struct dhcpv6_auth_reconfigure auth = {
1107 htons(DHCPV6_OPT_AUTH),
1108 htons(sizeof(auth) - 4),
1109 3, 1, 0,
1110 {htonl(time(NULL)), htonl(++serial)},
1111 1,
1112 {0}
1113 };
1114 memcpy(auth.key, a->key, sizeof(a->key));
1115 memcpy(buf + handshake_len, &auth, sizeof(auth));
1116 handshake_len += sizeof(auth);
1117 }
1118
1119 buf += handshake_len;
1120 buflen -= handshake_len;
1121 response_len += handshake_len;
1122
1123 first = a;
1124 }
1125
1126 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1127
1128 /* Was only a solicitation: mark binding for removal */
1129 if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
1130 a->flags &= ~OAF_BOUND;
1131
1132 if (!(a->flags & OAF_STATIC))
1133 a->valid_until = now;
1134 } else if (assigned && hdr->msg_type == DHCPV6_MSG_REQUEST) {
1135 if (hostname_len > 0) {
1136 a->hostname = realloc(a->hostname, hostname_len + 1);
1137 if (a->hostname) {
1138 memcpy(a->hostname, hostname, hostname_len);
1139 a->hostname[hostname_len] = 0;
1140 }
1141 }
1142 a->accept_reconf = accept_reconf;
1143 a->flags |= OAF_BOUND;
1144 apply_lease(iface, a, true);
1145 } else if (!assigned && a && a->managed_size == 0)
1146 /* Cleanup failed assignment */
1147 free_dhcpv6_assignment(a);
1148
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);
1159 if (a) {
1160 a->flags |= OAF_BOUND;
1161 apply_lease(iface, a, true);
1162 }
1163 } else if (hdr->msg_type == DHCPV6_MSG_RELEASE) {
1164 if (!(a->flags & OAF_STATIC))
1165 a->valid_until = now - 1;
1166
1167 a->flags &= ~OAF_BOUND;
1168 apply_lease(iface, a, false);
1169 } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
1170 a->flags &= ~OAF_BOUND;
1171
1172 if (!(a->flags & OAF_STATIC)) {
1173 a->clid_len = 0;
1174 a->valid_until = now + 3600; /* Block address for 1h */
1175 }
1176 }
1177 } else if (hdr->msg_type == DHCPV6_MSG_CONFIRM && ia_addr_present) {
1178 /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1179 status = DHCPV6_STATUS_NOTONLINK;
1180 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1181 notonlink = true;
1182 }
1183
1184 buf += ia_response_len;
1185 buflen -= ia_response_len;
1186 response_len += ia_response_len;
1187 dhcpv6_log(hdr->msg_type, iface, now, duidbuf, is_pd, a, status);
1188 }
1189
1190 if ((hdr->msg_type == DHCPV6_MSG_RELEASE || hdr->msg_type == DHCPV6_MSG_DECLINE || notonlink) &&
1191 response_len + 6 < buflen) {
1192 buf[0] = 0;
1193 buf[1] = DHCPV6_OPT_STATUS;
1194 buf[2] = 0;
1195 buf[3] = 2;
1196 buf[4] = 0;
1197 buf[5] = (notonlink) ? DHCPV6_STATUS_NOTONLINK : DHCPV6_STATUS_OK;
1198 response_len += 6;
1199 }
1200
1201 dhcpv6_write_statefile();
1202
1203 out:
1204 return response_len;
1205 }