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