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