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