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