7ca8e4faad2b5a73c5563eb28191a6f1239b7fe6
[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
20 #include <time.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <poll.h>
25 #include <alloca.h>
26 #include <resolv.h>
27 #include <limits.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdbool.h>
32 #include <arpa/inet.h>
33 #include <sys/timerfd.h>
34
35 #include <libubox/md5.h>
36 #include <libubox/usock.h>
37
38 #define ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) \
39 ((iface)->dhcpv6_assignall || (i) == (m) || \
40 (addrs)[(i)].prefix > 64)
41
42 static void dhcpv6_netevent_cb(unsigned long event, struct netevent_handler_info *info);
43 static void apply_lease(struct dhcp_assignment *a, bool add);
44 static void set_border_assignment_size(struct interface *iface, struct dhcp_assignment *b);
45 static void handle_addrlist_change(struct netevent_handler_info *info);
46 static void start_reconf(struct dhcp_assignment *a);
47 static void stop_reconf(struct dhcp_assignment *a);
48 static void valid_until_cb(struct uloop_timeout *event);
49
50 static struct netevent_handler dhcpv6_netevent_handler = { .cb = dhcpv6_netevent_cb, };
51 static struct uloop_timeout valid_until_timeout = {.cb = valid_until_cb};
52 static uint32_t serial = 0;
53 static uint8_t statemd5[16];
54
55 int dhcpv6_ia_init(void)
56 {
57 uloop_timeout_set(&valid_until_timeout, 1000);
58
59 netlink_add_netevent_handler(&dhcpv6_netevent_handler);
60
61 return 0;
62 }
63
64 int dhcpv6_ia_setup_interface(struct interface *iface, bool enable)
65 {
66 enable = enable && (iface->dhcpv6 == MODE_SERVER);
67
68 if (enable) {
69 struct dhcp_assignment *border;
70
71 if (list_empty(&iface->ia_assignments)) {
72 border = alloc_assignment(0);
73
74 if (!border) {
75 syslog(LOG_WARNING, "Failed to alloc border on %s", iface->name);
76 return -1;
77 }
78
79 border->length = 64;
80 list_add(&border->head, &iface->ia_assignments);
81 } else
82 border = list_last_entry(&iface->ia_assignments, struct dhcp_assignment, head);
83
84 set_border_assignment_size(iface, border);
85 } else {
86 struct dhcp_assignment *c;
87
88 while (!list_empty(&iface->ia_assignments)) {
89 c = list_first_entry(&iface->ia_assignments, struct dhcp_assignment, head);
90 free_assignment(c);
91 }
92 }
93
94 return 0;
95 }
96
97
98 static void dhcpv6_netevent_cb(unsigned long event, struct netevent_handler_info *info)
99 {
100 struct interface *iface = info->iface;
101
102 if (!iface || iface->dhcpv6 != MODE_SERVER)
103 return;
104
105 switch (event) {
106 case NETEV_ADDR6LIST_CHANGE:
107 handle_addrlist_change(info);
108 break;
109 default:
110 break;
111 }
112 }
113
114
115 static inline bool valid_prefix_length(const struct dhcp_assignment *a, const uint8_t prefix_length)
116 {
117 return (a->managed_size || a->length > prefix_length);
118 }
119
120 static inline bool valid_addr(const struct odhcpd_ipaddr *addr, time_t now)
121 {
122 return (addr->prefix <= 96 && addr->preferred > (uint32_t)now);
123 }
124
125 static size_t get_preferred_addr(const struct odhcpd_ipaddr *addrs, const size_t addrlen)
126 {
127 size_t i, m;
128
129 for (i = 0, m = 0; i < addrlen; ++i) {
130 if (addrs[i].preferred > addrs[m].preferred ||
131 (addrs[i].preferred == addrs[m].preferred &&
132 memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
133 m = i;
134 }
135
136 return m;
137 }
138
139 static int send_reconf(struct dhcp_assignment *assign)
140 {
141 struct {
142 struct dhcpv6_client_header hdr;
143 uint16_t srvid_type;
144 uint16_t srvid_len;
145 uint16_t duid_type;
146 uint16_t hardware_type;
147 uint8_t mac[6];
148 uint16_t msg_type;
149 uint16_t msg_len;
150 uint8_t msg_id;
151 struct dhcpv6_auth_reconfigure auth;
152 uint16_t clid_type;
153 uint16_t clid_len;
154 uint8_t clid_data[128];
155 } __attribute__((packed)) reconf_msg = {
156 .hdr = {DHCPV6_MSG_RECONFIGURE, {0, 0, 0}},
157 .srvid_type = htons(DHCPV6_OPT_SERVERID),
158 .srvid_len = htons(10),
159 .duid_type = htons(3),
160 .hardware_type = htons(1),
161 .msg_type = htons(DHCPV6_OPT_RECONF_MSG),
162 .msg_len = htons(1),
163 .msg_id = DHCPV6_MSG_RENEW,
164 .auth = {htons(DHCPV6_OPT_AUTH),
165 htons(sizeof(reconf_msg.auth) - 4), 3, 1, 0,
166 {htonl(time(NULL)), htonl(++serial)}, 2, {0}},
167 .clid_type = htons(DHCPV6_OPT_CLIENTID),
168 .clid_len = htons(assign->clid_len),
169 .clid_data = {0},
170 };
171 struct interface *iface = assign->iface;
172
173 odhcpd_get_mac(iface, reconf_msg.mac);
174 memcpy(reconf_msg.clid_data, assign->clid_data, assign->clid_len);
175 struct iovec iov = {&reconf_msg, sizeof(reconf_msg) - 128 + assign->clid_len};
176
177 md5_ctx_t md5;
178 uint8_t secretbytes[64];
179 memset(secretbytes, 0, sizeof(secretbytes));
180 memcpy(secretbytes, assign->key, sizeof(assign->key));
181
182 for (size_t i = 0; i < sizeof(secretbytes); ++i)
183 secretbytes[i] ^= 0x36;
184
185 md5_begin(&md5);
186 md5_hash(secretbytes, sizeof(secretbytes), &md5);
187 md5_hash(iov.iov_base, iov.iov_len, &md5);
188 md5_end(reconf_msg.auth.key, &md5);
189
190 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
191 secretbytes[i] ^= 0x36;
192 secretbytes[i] ^= 0x5c;
193 }
194
195 md5_begin(&md5);
196 md5_hash(secretbytes, sizeof(secretbytes), &md5);
197 md5_hash(reconf_msg.auth.key, 16, &md5);
198 md5_end(reconf_msg.auth.key, &md5);
199
200 return odhcpd_send(iface->dhcpv6_event.uloop.fd, &assign->peer, &iov, 1, iface);
201 }
202
203 static void dhcpv6_ia_free_assignment(struct dhcp_assignment *a)
204 {
205 if (a->managed_sock.fd.registered) {
206 ustream_free(&a->managed_sock.stream);
207 close(a->managed_sock.fd.fd);
208 }
209
210 if ((a->flags & OAF_BOUND) && (a->flags & OAF_DHCPV6_PD))
211 apply_lease(a, false);
212
213 if (a->reconf_cnt)
214 stop_reconf(a);
215
216 free(a->managed);
217 }
218
219 void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcp_assignment *c,
220 time_t now, dhcpv6_binding_cb_handler_t func, void *arg)
221 {
222 struct odhcpd_ipaddr *addrs = (c->managed) ? c->managed : iface->addr6;
223 size_t addrlen = (c->managed) ? (size_t)c->managed_size : iface->addr6_len;
224 size_t m = get_preferred_addr(addrs, addrlen);
225
226 for (size_t i = 0; i < addrlen; ++i) {
227 struct in6_addr addr;
228 uint32_t pref, valid;
229 int prefix = c->managed ? addrs[i].prefix : c->length;
230
231 if (!valid_addr(&addrs[i], now))
232 continue;
233
234 addr = addrs[i].addr.in6;
235 pref = addrs[i].preferred;
236 valid = addrs[i].valid;
237 if (c->flags & OAF_DHCPV6_NA) {
238 if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs))
239 continue;
240
241 addr.s6_addr32[3] = htonl(c->assigned);
242 } else {
243 if (!valid_prefix_length(c, addrs[i].prefix))
244 continue;
245
246 addr.s6_addr32[1] |= htonl(c->assigned);
247 addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
248 }
249
250 if (pref > (uint32_t)c->valid_until)
251 pref = c->valid_until;
252
253 if (pref != UINT32_MAX)
254 pref -= now;
255
256 if (valid > (uint32_t)c->valid_until)
257 valid = c->valid_until;
258
259 if (valid != UINT32_MAX)
260 valid -= now;
261
262 func(&addr, prefix, pref, valid, arg);
263 }
264 }
265
266 struct write_ctxt {
267 FILE *fp;
268 md5_ctx_t md5;
269 struct dhcp_assignment *c;
270 struct interface *iface;
271 char *buf;
272 int buf_len;
273 int buf_idx;
274 };
275
276 static void dhcpv6_write_ia_addr(struct in6_addr *addr, int prefix, _unused uint32_t pref,
277 _unused uint32_t valid, void *arg)
278 {
279 struct write_ctxt *ctxt = (struct write_ctxt *)arg;
280 char ipbuf[INET6_ADDRSTRLEN];
281
282 inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf) - 1);
283
284 if ((ctxt->c->flags & OAF_DHCPV6_NA) && ctxt->c->hostname &&
285 !(ctxt->c->flags & OAF_BROKEN_HOSTNAME)) {
286 fputs(ipbuf, ctxt->fp);
287
288 char b[256];
289 if (dn_expand(ctxt->iface->search, ctxt->iface->search + ctxt->iface->search_len,
290 ctxt->iface->search, b, sizeof(b)) > 0)
291 fprintf(ctxt->fp, "\t%s.%s", ctxt->c->hostname, b);
292
293 fprintf(ctxt->fp, "\t%s\n", ctxt->c->hostname);
294 md5_hash(ipbuf, strlen(ipbuf), &ctxt->md5);
295 md5_hash(ctxt->c->hostname, strlen(ctxt->c->hostname), &ctxt->md5);
296 }
297
298 ctxt->buf_idx += snprintf(ctxt->buf + ctxt->buf_idx,ctxt->buf_len - ctxt->buf_idx,
299 "%s/%d ", ipbuf, prefix);
300 }
301
302 void dhcpv6_ia_write_statefile(void)
303 {
304 struct write_ctxt ctxt;
305
306 md5_begin(&ctxt.md5);
307
308 if (config.dhcp_statefile) {
309 unsigned tmp_statefile_strlen = strlen(config.dhcp_statefile) + strlen(".tmp") + 1;
310 char *tmp_statefile = alloca(tmp_statefile_strlen);
311 time_t now = odhcpd_time(), wall_time = time(NULL);
312 int fd, ret;
313 char leasebuf[512];
314
315 snprintf(tmp_statefile, tmp_statefile_strlen, "%s.tmp", config.dhcp_statefile);
316
317 fd = open(tmp_statefile, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
318 if (fd < 0)
319 return;
320
321 ret = lockf(fd, F_LOCK, 0);
322 if (ret < 0) {
323 close(fd);
324 return;
325 }
326
327 if (ftruncate(fd, 0) < 0) {}
328
329 ctxt.fp = fdopen(fd, "w");
330 if (!ctxt.fp) {
331 close(fd);
332 return;
333 }
334
335 ctxt.buf = leasebuf;
336 ctxt.buf_len = sizeof(leasebuf);
337
338 avl_for_each_element(&interfaces, ctxt.iface, avl) {
339 if (ctxt.iface->dhcpv6 != MODE_SERVER &&
340 ctxt.iface->dhcpv4 != MODE_SERVER)
341 continue;
342
343 if (ctxt.iface->dhcpv6 == MODE_SERVER) {
344 list_for_each_entry(ctxt.c, &ctxt.iface->ia_assignments, head) {
345 if (!(ctxt.c->flags & OAF_BOUND) || ctxt.c->managed_size < 0)
346 continue;
347
348 char duidbuf[264];
349
350 odhcpd_hexlify(duidbuf, ctxt.c->clid_data, ctxt.c->clid_len);
351
352 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
353 ctxt.buf_idx = snprintf(ctxt.buf, ctxt.buf_len, "# %s %s %x %s%s %"PRId64" %x %u ",
354 ctxt.iface->ifname, duidbuf, ntohl(ctxt.c->iaid),
355 (ctxt.c->flags & OAF_BROKEN_HOSTNAME) ? "broken\\x20" : "",
356 (ctxt.c->hostname ? ctxt.c->hostname : "-"),
357 (ctxt.c->valid_until > now ?
358 (int64_t)(ctxt.c->valid_until - now + wall_time) :
359 (INFINITE_VALID(ctxt.c->valid_until) ? -1 : 0)),
360 ctxt.c->assigned, (unsigned)ctxt.c->length);
361
362 if (INFINITE_VALID(ctxt.c->valid_until) || ctxt.c->valid_until > now)
363 dhcpv6_ia_enum_addrs(ctxt.iface, ctxt.c, now,
364 dhcpv6_write_ia_addr, &ctxt);
365
366 ctxt.buf[ctxt.buf_idx - 1] = '\n';
367 fwrite(ctxt.buf, 1, ctxt.buf_idx, ctxt.fp);
368 }
369 }
370
371 if (ctxt.iface->dhcpv4 == MODE_SERVER) {
372 struct dhcp_assignment *c;
373
374 list_for_each_entry(c, &ctxt.iface->dhcpv4_assignments, head) {
375 if (!(c->flags & OAF_BOUND))
376 continue;
377
378 char ipbuf[INET6_ADDRSTRLEN];
379 char duidbuf[16];
380 odhcpd_hexlify(duidbuf, c->hwaddr, sizeof(c->hwaddr));
381
382 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
383 ctxt.buf_idx = snprintf(ctxt.buf, ctxt.buf_len, "# %s %s ipv4 %s%s %"PRId64" %x 32 ",
384 ctxt.iface->ifname, duidbuf,
385 (c->flags & OAF_BROKEN_HOSTNAME) ? "broken\\x20" : "",
386 (c->hostname ? c->hostname : "-"),
387 (c->valid_until > now ?
388 (int64_t)(c->valid_until - now + wall_time) :
389 (INFINITE_VALID(c->valid_until) ? -1 : 0)),
390 ntohl(c->addr));
391
392 struct in_addr addr = {.s_addr = c->addr};
393 inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf) - 1);
394
395 if (c->hostname && !(c->flags & OAF_BROKEN_HOSTNAME)) {
396 fputs(ipbuf, ctxt.fp);
397
398 char b[256];
399 if (dn_expand(ctxt.iface->search,
400 ctxt.iface->search + ctxt.iface->search_len,
401 ctxt.iface->search, b, sizeof(b)) > 0)
402 fprintf(ctxt.fp, "\t%s.%s", c->hostname, b);
403
404 fprintf(ctxt.fp, "\t%s\n", c->hostname);
405 md5_hash(ipbuf, strlen(ipbuf), &ctxt.md5);
406 md5_hash(c->hostname, strlen(c->hostname), &ctxt.md5);
407 }
408
409 ctxt.buf_idx += snprintf(ctxt.buf + ctxt.buf_idx,
410 ctxt.buf_len - ctxt.buf_idx,
411 "%s/32 ", ipbuf);
412 ctxt.buf[ctxt.buf_idx - 1] = '\n';
413 fwrite(ctxt.buf, 1, ctxt.buf_idx, ctxt.fp);
414 }
415 }
416 }
417
418 fclose(ctxt.fp);
419
420 rename(tmp_statefile, config.dhcp_statefile);
421 }
422
423 uint8_t newmd5[16];
424 md5_end(newmd5, &ctxt.md5);
425
426 if (config.dhcp_cb && memcmp(newmd5, statemd5, sizeof(newmd5))) {
427 memcpy(statemd5, newmd5, sizeof(statemd5));
428 char *argv[2] = {config.dhcp_cb, NULL};
429 if (!vfork()) {
430 execv(argv[0], argv);
431 _exit(128);
432 }
433 }
434 }
435
436 static void __apply_lease(struct dhcp_assignment *a,
437 struct odhcpd_ipaddr *addrs, ssize_t addr_len, bool add)
438 {
439 if (a->flags & OAF_DHCPV6_NA)
440 return;
441
442 for (ssize_t i = 0; i < addr_len; ++i) {
443 struct in6_addr prefix = addrs[i].addr.in6;
444 prefix.s6_addr32[1] |= htonl(a->assigned);
445 prefix.s6_addr32[2] = prefix.s6_addr32[3] = 0;
446 netlink_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length,
447 a->iface->ifindex, &a->peer.sin6_addr, 1024, add);
448 }
449 }
450
451 static void apply_lease(struct dhcp_assignment *a, bool add)
452 {
453 struct interface *iface = a->iface;
454 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->addr6;
455 ssize_t addrlen = (a->managed) ? a->managed_size : (ssize_t)iface->addr6_len;
456
457 __apply_lease(a, addrs, addrlen, add);
458 }
459
460 /* Set border assignment size based on the IPv6 address prefixes */
461 static void set_border_assignment_size(struct interface *iface, struct dhcp_assignment *b)
462 {
463 time_t now = odhcpd_time();
464 int minprefix = -1;
465
466 for (size_t i = 0; i < iface->addr6_len; ++i) {
467 if (iface->addr6[i].preferred > (uint32_t)now &&
468 iface->addr6[i].prefix < 64 &&
469 iface->addr6[i].prefix > minprefix)
470 minprefix = iface->addr6[i].prefix;
471 }
472
473 if (minprefix > 32 && minprefix <= 64)
474 b->assigned = 1U << (64 - minprefix);
475 else
476 b->assigned = 0;
477 }
478
479 /* More data was received from TCP connection */
480 static void managed_handle_pd_data(struct ustream *s, _unused int bytes_new)
481 {
482 struct ustream_fd *fd = container_of(s, struct ustream_fd, stream);
483 struct dhcp_assignment *c = container_of(fd, struct dhcp_assignment, managed_sock);
484 time_t now = odhcpd_time();
485 bool first = c->managed_size < 0;
486
487 for (;;) {
488 int pending;
489 char *data = ustream_get_read_buf(s, &pending);
490 char *end = memmem(data, pending, "\n\n", 2);
491
492 if (!end)
493 break;
494
495 end += 2;
496 end[-1] = 0;
497
498 c->managed_size = 0;
499 if (c->accept_reconf)
500 c->reconf_cnt = 1;
501
502 char *saveptr;
503 for (char *line = strtok_r(data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr)) {
504 c->managed = realloc(c->managed, (c->managed_size + 1) * sizeof(*c->managed));
505 struct odhcpd_ipaddr *n = &c->managed[c->managed_size];
506
507 char *saveptr2, *x = strtok_r(line, "/", &saveptr2);
508 if (!x || inet_pton(AF_INET6, x, &n->addr) < 1)
509 continue;
510
511 x = strtok_r(NULL, ",", &saveptr2);
512 if (sscanf(x, "%hhu", &n->prefix) < 1)
513 continue;
514
515 x = strtok_r(NULL, ",", &saveptr2);
516 if (sscanf(x, "%u", &n->preferred) < 1)
517 continue;
518
519 x = strtok_r(NULL, ",", &saveptr2);
520 if (sscanf(x, "%u", &n->valid) < 1)
521 continue;
522
523 if (n->preferred > n->valid)
524 continue;
525
526 if (UINT32_MAX - now < n->preferred)
527 n->preferred = UINT32_MAX;
528 else
529 n->preferred += now;
530
531 if (UINT32_MAX - now < n->valid)
532 n->valid = UINT32_MAX;
533 else
534 n->valid += now;
535
536 n->dprefix = 0;
537
538 ++c->managed_size;
539 }
540
541 ustream_consume(s, end - data);
542 }
543
544 if (first && c->managed_size == 0)
545 free_assignment(c);
546 else if (first)
547 c->valid_until = now + 150;
548 }
549
550
551 /* TCP transmission has ended, either because of success or timeout or other error */
552 static void managed_handle_pd_done(struct ustream *s)
553 {
554 struct ustream_fd *fd = container_of(s, struct ustream_fd, stream);
555 struct dhcp_assignment *c = container_of(fd, struct dhcp_assignment, managed_sock);
556
557 c->valid_until = odhcpd_time() + 15;
558
559 c->managed_size = 0;
560
561 if (c->accept_reconf)
562 c->reconf_cnt = 1;
563 }
564
565 static bool assign_pd(struct interface *iface, struct dhcp_assignment *assign)
566 {
567 struct dhcp_assignment *c;
568
569 if (iface->dhcpv6_pd_manager[0]) {
570 int fd = usock(USOCK_UNIX | USOCK_TCP, iface->dhcpv6_pd_manager, NULL);
571 if (fd >= 0) {
572 struct pollfd pfd = { .fd = fd, .events = POLLIN };
573 char iaidbuf[298];
574
575 odhcpd_hexlify(iaidbuf, assign->clid_data, assign->clid_len);
576
577 assign->managed_sock.stream.notify_read = managed_handle_pd_data;
578 assign->managed_sock.stream.notify_state = managed_handle_pd_done;
579 ustream_fd_init(&assign->managed_sock, fd);
580 ustream_printf(&assign->managed_sock.stream, "%s,%x\n::/%d,0,0\n\n",
581 iaidbuf, assign->iaid, assign->length);
582 ustream_write_pending(&assign->managed_sock.stream);
583 assign->managed_size = -1;
584
585 assign->valid_until = odhcpd_time() + 15;
586
587 list_add(&assign->head, &iface->ia_assignments);
588
589 /* Wait initial period of up to 250ms for immediate assignment */
590 if (poll(&pfd, 1, 250) < 0) {
591 syslog(LOG_ERR, "poll(): %m");
592 return false;
593 }
594
595 managed_handle_pd_data(&assign->managed_sock.stream, 0);
596
597 if (fcntl(fd, F_GETFL) >= 0 && assign->managed_size > 0)
598 return true;
599 }
600
601 return false;
602 } else if (iface->addr6_len < 1)
603 return false;
604
605 /* Try honoring the hint first */
606 uint32_t current = 1, asize = (1 << (64 - assign->length)) - 1;
607 if (assign->assigned) {
608 list_for_each_entry(c, &iface->ia_assignments, head) {
609 if (c->flags & OAF_DHCPV6_NA)
610 continue;
611
612 if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
613 list_add_tail(&assign->head, &c->head);
614
615 if (assign->flags & OAF_BOUND)
616 apply_lease(assign, true);
617
618 return true;
619 }
620
621 if (c->assigned != 0)
622 current = (c->assigned + (1 << (64 - c->length)));
623 }
624 }
625
626 /* Fallback to a variable assignment */
627 current = 1;
628 list_for_each_entry(c, &iface->ia_assignments, head) {
629 if (c->flags & OAF_DHCPV6_NA)
630 continue;
631
632 current = (current + asize) & (~asize);
633 if (current + asize < c->assigned) {
634 assign->assigned = current;
635 list_add_tail(&assign->head, &c->head);
636
637 if (assign->flags & OAF_BOUND)
638 apply_lease(assign, true);
639
640 return true;
641 }
642
643 if (c->assigned != 0)
644 current = (c->assigned + (1 << (64 - c->length)));
645 }
646
647 return false;
648 }
649
650 static bool assign_na(struct interface *iface, struct dhcp_assignment *a)
651 {
652 struct dhcp_assignment *c;
653 uint32_t seed = 0;
654
655 /* Preconfigured assignment by static lease */
656 if (a->assigned) {
657 list_for_each_entry(c, &iface->ia_assignments, head) {
658 if (c->assigned > a->assigned || !(c->flags & OAF_DHCPV6_NA)) {
659 list_add_tail(&a->head, &c->head);
660 return true;
661 } else if (c->assigned == a->assigned)
662 return false;
663 }
664 }
665
666 /* Seed RNG with checksum of DUID */
667 for (size_t i = 0; i < a->clid_len; ++i)
668 seed += a->clid_data[i];
669 srand(seed);
670
671 /* Try to assign up to 100x */
672 for (size_t i = 0; i < 100; ++i) {
673 uint32_t try;
674 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
675
676 if (config_find_lease_by_hostid(try))
677 continue;
678
679 list_for_each_entry(c, &iface->ia_assignments, head) {
680 if (c->assigned > try || !(c->flags & OAF_DHCPV6_NA)) {
681 a->assigned = try;
682 list_add_tail(&a->head, &c->head);
683 return true;
684 } else if (c->assigned == try)
685 break;
686 }
687 }
688
689 return false;
690 }
691
692 static void handle_addrlist_change(struct netevent_handler_info *info)
693 {
694 struct interface *iface = info->iface;
695 struct dhcp_assignment *c, *d, *border = list_last_entry(
696 &iface->ia_assignments, struct dhcp_assignment, head);
697 struct list_head reassign = LIST_HEAD_INIT(reassign);
698 time_t now = odhcpd_time();
699
700 list_for_each_entry(c, &iface->ia_assignments, head) {
701 if ((c->flags & OAF_DHCPV6_PD) && !(iface->ra_flags & ND_RA_FLAG_MANAGED)
702 && (c->flags & OAF_BOUND))
703 __apply_lease(c, info->addrs_old.addrs,
704 info->addrs_old.len, false);
705 }
706
707 set_border_assignment_size(iface, border);
708
709 list_for_each_entry_safe(c, d, &iface->ia_assignments, head) {
710 if (c->clid_len == 0 ||
711 !(c->flags & OAF_DHCPV6_PD) ||
712 (!INFINITE_VALID(c->valid_until) && c->valid_until < now) ||
713 c->managed_size)
714 continue;
715
716 if (c->assigned == 0 || c->assigned >= border->assigned)
717 list_move(&c->head, &reassign);
718 else if (c->flags & OAF_BOUND)
719 apply_lease(c, true);
720
721 if (c->accept_reconf && c->reconf_cnt == 0) {
722 struct dhcp_assignment *a;
723
724 start_reconf(c);
725
726 /* Leave all other assignments of that client alone */
727 list_for_each_entry(a, &iface->ia_assignments, head)
728 if (a != c && a->clid_len == c->clid_len &&
729 !memcmp(a->clid_data, c->clid_data, a->clid_len))
730 a->reconf_cnt = INT_MAX;
731 }
732 }
733
734 while (!list_empty(&reassign)) {
735 c = list_first_entry(&reassign, struct dhcp_assignment, head);
736 list_del_init(&c->head);
737 if (!assign_pd(iface, c)) {
738 c->assigned = 0;
739 list_add(&c->head, &iface->ia_assignments);
740 }
741 }
742
743 dhcpv6_ia_write_statefile();
744 }
745
746 static void reconf_timeout_cb(struct uloop_timeout *event)
747 {
748 struct dhcp_assignment *a = container_of(event, struct dhcp_assignment, reconf_timer);
749
750 if (a->reconf_cnt > 0 && a->reconf_cnt < DHCPV6_REC_MAX_RC) {
751 send_reconf(a);
752 uloop_timeout_set(&a->reconf_timer,
753 DHCPV6_REC_TIMEOUT << a->reconf_cnt);
754 a->reconf_cnt++;
755 } else
756 stop_reconf(a);
757 }
758
759 static void start_reconf(struct dhcp_assignment *a)
760 {
761 uloop_timeout_set(&a->reconf_timer,
762 DHCPV6_REC_TIMEOUT << a->reconf_cnt);
763 a->reconf_timer.cb = reconf_timeout_cb;
764 a->reconf_cnt++;
765
766 send_reconf(a);
767 }
768
769 static void stop_reconf(struct dhcp_assignment *a)
770 {
771 uloop_timeout_cancel(&a->reconf_timer);
772 a->reconf_cnt = 0;
773 a->reconf_timer.cb = NULL;
774 }
775
776 static void valid_until_cb(struct uloop_timeout *event)
777 {
778 struct interface *iface;
779 time_t now = odhcpd_time();
780
781 avl_for_each_element(&interfaces, iface, avl) {
782 struct dhcp_assignment *a, *n;
783
784 if (iface->dhcpv6 != MODE_SERVER)
785 continue;
786
787 list_for_each_entry_safe(a, n, &iface->ia_assignments, head) {
788 if (a->clid_len > 0 && !INFINITE_VALID(a->valid_until) && a->valid_until < now)
789 free_assignment(a);
790 }
791 }
792 uloop_timeout_set(event, 1000);
793 }
794
795 static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
796 const struct dhcpv6_ia_hdr *ia, struct dhcp_assignment *a,
797 struct interface *iface, bool request)
798 {
799 struct dhcpv6_ia_hdr o_ia = {
800 .type = ia->type,
801 .len = 0,
802 .iaid = ia->iaid,
803 .t1 = 0,
804 .t2 = 0,
805 };
806 size_t ia_len = sizeof(o_ia);
807 time_t now = odhcpd_time();
808
809 if (buflen < ia_len)
810 return 0;
811
812 if (status) {
813 struct __attribute__((packed)) {
814 uint16_t type;
815 uint16_t len;
816 uint16_t val;
817 } o_status = {
818 .type = htons(DHCPV6_OPT_STATUS),
819 .len = htons(sizeof(o_status) - 4),
820 .val = htons(status),
821 };
822
823 memcpy(buf + ia_len, &o_status, sizeof(o_status));
824 ia_len += sizeof(o_status);
825
826 o_ia.len = htons(ia_len - 4);
827 memcpy(buf, &o_ia, sizeof(o_ia));
828
829 return ia_len;
830 }
831
832 if (a) {
833 uint32_t leasetime;
834
835 if (a->leasetime)
836 leasetime = a->leasetime;
837 else
838 leasetime = iface->dhcp_leasetime;
839
840 uint32_t pref = leasetime;
841 uint32_t valid = leasetime;
842
843 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->addr6;
844 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->addr6_len;
845 size_t m = get_preferred_addr(addrs, addrlen);
846
847 for (size_t i = 0; i < addrlen; ++i) {
848 uint32_t prefix_pref = addrs[i].preferred;
849 uint32_t prefix_valid = addrs[i].valid;
850
851 if (!valid_addr(&addrs[i], now))
852 continue;
853
854 if (prefix_pref != UINT32_MAX)
855 prefix_pref -= now;
856
857 if (prefix_pref > leasetime)
858 prefix_pref = leasetime;
859
860 if (prefix_valid != UINT32_MAX)
861 prefix_valid -= now;
862
863 if (prefix_valid > leasetime)
864 prefix_valid = leasetime;
865
866 if (a->flags & OAF_DHCPV6_PD) {
867 struct dhcpv6_ia_prefix o_ia_p = {
868 .type = htons(DHCPV6_OPT_IA_PREFIX),
869 .len = htons(sizeof(o_ia_p) - 4),
870 .preferred = htonl(prefix_pref),
871 .valid = htonl(prefix_valid),
872 .prefix = (a->managed_size) ? addrs[i].prefix : a->length,
873 .addr = addrs[i].addr.in6,
874 };
875
876 o_ia_p.addr.s6_addr32[1] |= htonl(a->assigned);
877 o_ia_p.addr.s6_addr32[2] = o_ia_p.addr.s6_addr32[3] = 0;
878
879 if ((a->assigned == 0 && a->managed_size == 0) ||
880 !valid_prefix_length(a, addrs[i].prefix))
881 continue;
882
883 if (buflen < ia_len + sizeof(o_ia_p))
884 return 0;
885
886 memcpy(buf + ia_len, &o_ia_p, sizeof(o_ia_p));
887 ia_len += sizeof(o_ia_p);
888 }
889
890 if (a->flags & OAF_DHCPV6_NA) {
891 struct dhcpv6_ia_addr o_ia_a = {
892 .type = htons(DHCPV6_OPT_IA_ADDR),
893 .len = htons(sizeof(o_ia_a) - 4),
894 .addr = addrs[i].addr.in6,
895 .preferred = htonl(prefix_pref),
896 .valid = htonl(prefix_valid)
897 };
898
899 o_ia_a.addr.s6_addr32[3] = htonl(a->assigned);
900
901 if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) ||
902 a->assigned == 0)
903 continue;
904
905 if (buflen < ia_len + sizeof(o_ia_a))
906 return 0;
907
908 memcpy(buf + ia_len, &o_ia_a, sizeof(o_ia_a));
909 ia_len += sizeof(o_ia_a);
910 }
911
912 /* Calculate T1 / T2 based on non-deprecated addresses */
913 if (prefix_pref > 0) {
914 if (prefix_pref < pref)
915 pref = prefix_pref;
916
917 if (prefix_valid < valid)
918 valid = prefix_valid;
919 }
920 }
921
922 if (!INFINITE_VALID(a->valid_until))
923 /* UINT32_MAX is considered as infinite leasetime */
924 a->valid_until = (valid == UINT32_MAX) ? 0 : valid + now;
925
926 o_ia.t1 = htonl((pref == UINT32_MAX) ? pref : pref * 5 / 10);
927 o_ia.t2 = htonl((pref == UINT32_MAX) ? pref : pref * 8 / 10);
928
929 if (!o_ia.t1)
930 o_ia.t1 = htonl(1);
931
932 if (!o_ia.t2)
933 o_ia.t2 = htonl(1);
934 }
935
936 if (!request) {
937 uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + 4;
938 uint16_t otype, olen;
939
940 dhcpv6_for_each_option((uint8_t*)&ia[1], end, otype, olen, odata) {
941 struct dhcpv6_ia_prefix *ia_p = (struct dhcpv6_ia_prefix *)&odata[-4];
942 struct dhcpv6_ia_addr *ia_a = (struct dhcpv6_ia_addr *)&odata[-4];
943 bool found = false;
944
945 if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*ia_p) - 4) &&
946 (otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*ia_a) - 4))
947 continue;
948
949 if (a) {
950 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->addr6;
951 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->addr6_len;
952
953 for (size_t i = 0; i < addrlen; ++i) {
954 if (!valid_addr(&addrs[i], now))
955 continue;
956
957 struct in6_addr addr = addrs[i].addr.in6;
958 if (ia->type == htons(DHCPV6_OPT_IA_PD)) {
959 addr.s6_addr32[1] |= htonl(a->assigned);
960 addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
961
962 if (!memcmp(&ia_p->addr, &addr, sizeof(addr)) &&
963 ia_p->prefix == ((a->managed) ? addrs[i].prefix : a->length))
964 found = true;
965 } else {
966 addr.s6_addr32[3] = htonl(a->assigned);
967
968 if (!memcmp(&ia_a->addr, &addr, sizeof(addr)))
969 found = true;
970 }
971 }
972 }
973
974 if (!found) {
975 if (otype == DHCPV6_OPT_IA_PREFIX) {
976 struct dhcpv6_ia_prefix o_ia_p = {
977 .type = htons(DHCPV6_OPT_IA_PREFIX),
978 .len = htons(sizeof(o_ia_p) - 4),
979 .preferred = 0,
980 .valid = 0,
981 .prefix = ia_p->prefix,
982 .addr = ia_p->addr,
983 };
984
985 if (buflen < ia_len + sizeof(o_ia_p))
986 return 0;
987
988 memcpy(buf + ia_len, &o_ia_p, sizeof(o_ia_p));
989 ia_len += sizeof(o_ia_p);
990 } else {
991 struct dhcpv6_ia_addr o_ia_a = {
992 .type = htons(DHCPV6_OPT_IA_ADDR),
993 .len = htons(sizeof(o_ia_a) - 4),
994 .addr = ia_a->addr,
995 .preferred = 0,
996 .valid = 0,
997 };
998
999 if (buflen < ia_len + sizeof(o_ia_a))
1000 continue;
1001
1002 memcpy(buf + ia_len, &o_ia_a, sizeof(o_ia_a));
1003 ia_len += sizeof(o_ia_a);
1004 }
1005 }
1006 }
1007 }
1008
1009 o_ia.len = htons(ia_len - 4);
1010 memcpy(buf, &o_ia, sizeof(o_ia));
1011 return ia_len;
1012 }
1013
1014 struct log_ctxt {
1015 char *buf;
1016 int buf_len;
1017 int buf_idx;
1018 };
1019
1020 static void dhcpv6_log_ia_addr(struct in6_addr *addr, int prefix, _unused uint32_t pref,
1021 _unused uint32_t valid, void *arg)
1022 {
1023 struct log_ctxt *ctxt = (struct log_ctxt *)arg;
1024 char addrbuf[INET6_ADDRSTRLEN];
1025
1026 inet_ntop(AF_INET6, addr, addrbuf, sizeof(addrbuf));
1027 ctxt->buf_idx += snprintf(ctxt->buf + ctxt->buf_idx, ctxt->buf_len - ctxt->buf_idx,
1028 "%s/%d ", addrbuf, prefix);
1029 }
1030
1031 static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now,
1032 const char *duidbuf, bool is_pd, struct dhcp_assignment *a, int code)
1033 {
1034 const char *type = "UNKNOWN";
1035 const char *status = "UNKNOWN";
1036
1037 switch (msgtype) {
1038 case DHCPV6_MSG_SOLICIT:
1039 type = "SOLICIT";
1040 break;
1041 case DHCPV6_MSG_REQUEST:
1042 type = "REQUEST";
1043 break;
1044 case DHCPV6_MSG_CONFIRM:
1045 type = "CONFIRM";
1046 break;
1047 case DHCPV6_MSG_RENEW:
1048 type = "RENEW";
1049 break;
1050 case DHCPV6_MSG_REBIND:
1051 type = "REBIND";
1052 break;
1053 case DHCPV6_MSG_RELEASE:
1054 type = "RELEASE";
1055 break;
1056 case DHCPV6_MSG_DECLINE:
1057 type = "DECLINE";
1058 break;
1059 }
1060
1061 switch (code) {
1062 case DHCPV6_STATUS_OK:
1063 status = "ok";
1064 break;
1065 case DHCPV6_STATUS_NOADDRSAVAIL:
1066 status = "no addresses available";
1067 break;
1068 case DHCPV6_STATUS_NOBINDING:
1069 status = "no binding";
1070 break;
1071 case DHCPV6_STATUS_NOTONLINK:
1072 status = "not on-link";
1073 break;
1074 case DHCPV6_STATUS_NOPREFIXAVAIL:
1075 status = "no prefix available";
1076 break;
1077 }
1078
1079 char leasebuf[256] = "";
1080
1081 if (a) {
1082 struct log_ctxt ctxt = {.buf = leasebuf,
1083 .buf_len = sizeof(leasebuf),
1084 .buf_idx = 0 };
1085
1086 dhcpv6_ia_enum_addrs(iface, a, now, dhcpv6_log_ia_addr, &ctxt);
1087 }
1088
1089 syslog(LOG_INFO, "DHCPV6 %s %s from %s on %s: %s %s", type, (is_pd) ? "IA_PD" : "IA_NA",
1090 duidbuf, iface->name, status, leasebuf);
1091 }
1092
1093 static bool dhcpv6_ia_on_link(const struct dhcpv6_ia_hdr *ia, struct dhcp_assignment *a,
1094 struct interface *iface)
1095 {
1096 struct odhcpd_ipaddr *addrs = (a && a->managed) ? a->managed : iface->addr6;
1097 size_t addrlen = (a && a->managed) ? (size_t)a->managed_size : iface->addr6_len;
1098 time_t now = odhcpd_time();
1099 uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + 4;
1100 uint16_t otype, olen;
1101 bool onlink = true;
1102
1103 dhcpv6_for_each_option((uint8_t*)&ia[1], end, otype, olen, odata) {
1104 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix *)&odata[-4];
1105 struct dhcpv6_ia_addr *n = (struct dhcpv6_ia_addr *)&odata[-4];
1106
1107 if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*p) - 4) &&
1108 (otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*n) - 4))
1109 continue;
1110
1111 onlink = false;
1112 for (size_t i = 0; i < addrlen; ++i) {
1113 if (!valid_addr(&addrs[i], now))
1114 continue;
1115
1116 if (ia->type == htons(DHCPV6_OPT_IA_PD)) {
1117 if (p->prefix < addrs[i].prefix ||
1118 odhcpd_bmemcmp(&p->addr, &addrs[i].addr.in6, addrs[i].prefix))
1119 continue;
1120
1121 } else if (odhcpd_bmemcmp(&n->addr, &addrs[i].addr.in6, addrs[i].prefix))
1122 continue;
1123
1124 onlink = true;
1125 }
1126
1127 if (!onlink)
1128 break;
1129 }
1130
1131 return onlink;
1132 }
1133
1134 ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *iface,
1135 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end)
1136 {
1137 struct lease *l;
1138 struct dhcp_assignment *first = NULL;
1139 const struct dhcpv6_client_header *hdr = data;
1140 time_t now = odhcpd_time();
1141 uint16_t otype, olen, clid_len = 0;
1142 uint8_t *start = (uint8_t *)&hdr[1], *odata;
1143 uint8_t *clid_data = NULL, mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1144 size_t hostname_len = 0, response_len = 0;
1145 bool notonlink = false, rapid_commit = false, accept_reconf = false;
1146 char duidbuf[261], hostname[256];
1147
1148 dhcpv6_for_each_option(start, end, otype, olen, odata) {
1149 if (otype == DHCPV6_OPT_CLIENTID) {
1150 clid_data = odata;
1151 clid_len = olen;
1152
1153 if (olen == 14 && odata[0] == 0 && odata[1] == 1)
1154 memcpy(mac, &odata[8], sizeof(mac));
1155 else if (olen == 10 && odata[0] == 0 && odata[1] == 3)
1156 memcpy(mac, &odata[4], sizeof(mac));
1157
1158 if (olen <= 130)
1159 odhcpd_hexlify(duidbuf, odata, olen);
1160 } else if (otype == DHCPV6_OPT_FQDN && olen >= 2 && olen <= 255) {
1161 uint8_t fqdn_buf[256];
1162 memcpy(fqdn_buf, odata, olen);
1163 fqdn_buf[olen++] = 0;
1164
1165 if (dn_expand(&fqdn_buf[1], &fqdn_buf[olen], &fqdn_buf[1], hostname, sizeof(hostname)) > 0)
1166 hostname_len = strcspn(hostname, ".");
1167 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT)
1168 accept_reconf = true;
1169 else if (otype == DHCPV6_OPT_RAPID_COMMIT && hdr->msg_type == DHCPV6_MSG_SOLICIT)
1170 rapid_commit = true;
1171 }
1172
1173 if (!clid_data || !clid_len || clid_len > 130)
1174 goto out;
1175
1176 l = config_find_lease_by_duid(clid_data, clid_len);
1177 if (!l)
1178 l = config_find_lease_by_mac(mac);
1179
1180 dhcpv6_for_each_option(start, end, otype, olen, odata) {
1181 bool is_pd = (otype == DHCPV6_OPT_IA_PD);
1182 bool is_na = (otype == DHCPV6_OPT_IA_NA);
1183 bool ia_addr_present = false;
1184 if (!is_pd && !is_na)
1185 continue;
1186
1187 struct dhcpv6_ia_hdr *ia = (struct dhcpv6_ia_hdr*)&odata[-4];
1188 size_t ia_response_len = 0;
1189 uint8_t reqlen = (is_pd) ? 62 : 128;
1190 uint32_t reqhint = 0;
1191
1192 /* Parse request hint for IA-PD */
1193 if (is_pd) {
1194 uint8_t *sdata;
1195 uint16_t stype, slen;
1196 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1197 if (stype != DHCPV6_OPT_IA_PREFIX || slen < sizeof(struct dhcpv6_ia_prefix) - 4)
1198 continue;
1199
1200 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&sdata[-4];
1201 if (p->prefix) {
1202 reqlen = p->prefix;
1203 reqhint = ntohl(p->addr.s6_addr32[1]);
1204 if (reqlen > 32 && reqlen <= 64)
1205 reqhint &= (1U << (64 - reqlen)) - 1;
1206 }
1207 }
1208
1209 if (reqlen > 64)
1210 reqlen = 64;
1211 } else if (is_na) {
1212 uint8_t *sdata;
1213 uint16_t stype, slen;
1214 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1215 if (stype != DHCPV6_OPT_IA_ADDR || slen < sizeof(struct dhcpv6_ia_addr) - 4)
1216 continue;
1217
1218 ia_addr_present = true;
1219 }
1220 }
1221
1222 /* Find assignment */
1223 struct dhcp_assignment *c, *a = NULL;
1224 list_for_each_entry(c, &iface->ia_assignments, head) {
1225 if ((c->clid_len == clid_len && !memcmp(c->clid_data, clid_data, clid_len)) &&
1226 c->iaid == ia->iaid && (INFINITE_VALID(c->valid_until) || now < c->valid_until) &&
1227 ((is_pd && (c->flags & OAF_DHCPV6_PD)) || (is_na && (c->flags & OAF_DHCPV6_NA)))) {
1228 a = c;
1229
1230 /* Reset state */
1231 if (a->flags & OAF_BOUND)
1232 apply_lease(a, false);
1233
1234 stop_reconf(a);
1235 break;
1236 }
1237 }
1238
1239 if (l && a && a->lease != l) {
1240 free_assignment(a);
1241 a = NULL;
1242 }
1243
1244 /* Generic message handling */
1245 uint16_t status = DHCPV6_STATUS_OK;
1246 if (a && a->managed_size < 0)
1247 return -1;
1248
1249 if (hdr->msg_type == DHCPV6_MSG_SOLICIT ||
1250 hdr->msg_type == DHCPV6_MSG_REQUEST ||
1251 (hdr->msg_type == DHCPV6_MSG_REBIND && !a)) {
1252 bool assigned = !!a;
1253
1254 if (!a) {
1255 if ((!iface->no_dynamic_dhcp || (l && is_na)) &&
1256 (iface->dhcpv6_pd || iface->dhcpv6_na)) {
1257 /* Create new binding */
1258 a = alloc_assignment(clid_len);
1259
1260 if (a) {
1261 a->clid_len = clid_len;
1262 memcpy(a->clid_data, clid_data, clid_len);
1263 a->iaid = ia->iaid;
1264 a->length = reqlen;
1265 a->peer = *addr;
1266 a->assigned = is_na && l ? l->hostid : reqhint;
1267 a->valid_until = now;
1268 a->dhcp_free_cb = dhcpv6_ia_free_assignment;
1269 a->iface = iface;
1270 a->flags = (is_pd ? OAF_DHCPV6_PD : OAF_DHCPV6_NA);
1271
1272 if (first)
1273 memcpy(a->key, first->key, sizeof(a->key));
1274 else
1275 odhcpd_urandom(a->key, sizeof(a->key));
1276
1277 if (is_pd && iface->dhcpv6_pd)
1278 while (!(assigned = assign_pd(iface, a)) &&
1279 !a->managed_size && ++a->length <= 64);
1280 else if (is_na && iface->dhcpv6_na)
1281 assigned = assign_na(iface, a);
1282
1283 if (l && assigned) {
1284 a->flags |= OAF_STATIC;
1285
1286 if (l->hostname)
1287 a->hostname = strdup(l->hostname);
1288
1289 if (l->leasetime)
1290 a->leasetime = l->leasetime;
1291
1292 list_add(&a->lease_list, &l->assignments);
1293 a->lease = l;
1294 }
1295
1296 if (a->managed_size && !assigned)
1297 return -1;
1298 }
1299 }
1300 }
1301
1302 if (!assigned || iface->addr6_len == 0)
1303 /* Set error status */
1304 status = (is_pd) ? DHCPV6_STATUS_NOPREFIXAVAIL : DHCPV6_STATUS_NOADDRSAVAIL;
1305 else if (hdr->msg_type == DHCPV6_MSG_REQUEST && !dhcpv6_ia_on_link(ia, a, iface)) {
1306 /* Send NOTONLINK staus for the IA */
1307 status = DHCPV6_STATUS_NOTONLINK;
1308 assigned = false;
1309 } else if (accept_reconf && assigned && !first &&
1310 hdr->msg_type != DHCPV6_MSG_REBIND) {
1311 size_t handshake_len = 4;
1312 buf[0] = 0;
1313 buf[1] = DHCPV6_OPT_RECONF_ACCEPT;
1314 buf[2] = 0;
1315 buf[3] = 0;
1316
1317 if (hdr->msg_type == DHCPV6_MSG_REQUEST) {
1318 struct dhcpv6_auth_reconfigure auth = {
1319 htons(DHCPV6_OPT_AUTH),
1320 htons(sizeof(auth) - 4),
1321 3, 1, 0,
1322 {htonl(time(NULL)), htonl(++serial)},
1323 1,
1324 {0}
1325 };
1326 memcpy(auth.key, a->key, sizeof(a->key));
1327 memcpy(buf + handshake_len, &auth, sizeof(auth));
1328 handshake_len += sizeof(auth);
1329 }
1330
1331
1332 buf += handshake_len;
1333 buflen -= handshake_len;
1334 response_len += handshake_len;
1335
1336 first = a;
1337 }
1338
1339 ia_response_len = build_ia(buf, buflen, status, ia, a, iface,
1340 hdr->msg_type == DHCPV6_MSG_REBIND ? false : true);
1341
1342 /* Was only a solicitation: mark binding for removal */
1343 if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT && !rapid_commit) {
1344 a->flags &= ~OAF_BOUND;
1345 a->flags |= OAF_TENTATIVE;
1346
1347 /* Keep tentative assignment around for 60 seconds */
1348 a->valid_until = now + 60;
1349
1350 } else if (assigned &&
1351 ((hdr->msg_type == DHCPV6_MSG_SOLICIT && rapid_commit) ||
1352 hdr->msg_type == DHCPV6_MSG_REQUEST ||
1353 hdr->msg_type == DHCPV6_MSG_REBIND)) {
1354 if ((!(a->flags & OAF_STATIC) || !a->hostname) && hostname_len > 0) {
1355 a->hostname = realloc(a->hostname, hostname_len + 1);
1356 if (a->hostname) {
1357 memcpy(a->hostname, hostname, hostname_len);
1358 a->hostname[hostname_len] = 0;
1359
1360 if (odhcpd_valid_hostname(a->hostname))
1361 a->flags &= ~OAF_BROKEN_HOSTNAME;
1362 else
1363 a->flags |= OAF_BROKEN_HOSTNAME;
1364 }
1365 }
1366 a->accept_reconf = accept_reconf;
1367 a->flags &= ~OAF_TENTATIVE;
1368 a->flags |= OAF_BOUND;
1369 apply_lease(a, true);
1370 } else if (!assigned && a && a->managed_size == 0) {
1371 /* Cleanup failed assignment */
1372 free_assignment(a);
1373 a = NULL;
1374 }
1375 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1376 hdr->msg_type == DHCPV6_MSG_RELEASE ||
1377 hdr->msg_type == DHCPV6_MSG_REBIND ||
1378 hdr->msg_type == DHCPV6_MSG_DECLINE) {
1379 if (!a && hdr->msg_type != DHCPV6_MSG_REBIND) {
1380 status = DHCPV6_STATUS_NOBINDING;
1381 ia_response_len = build_ia(buf, buflen, status, ia, a, iface, false);
1382 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1383 hdr->msg_type == DHCPV6_MSG_REBIND) {
1384 ia_response_len = build_ia(buf, buflen, status, ia, a, iface, false);
1385 if (a) {
1386 a->flags |= OAF_BOUND;
1387 apply_lease(a, true);
1388 }
1389 } else if (hdr->msg_type == DHCPV6_MSG_RELEASE) {
1390 a->valid_until = now - 1;
1391 } else if ((a->flags & OAF_DHCPV6_NA) && hdr->msg_type == DHCPV6_MSG_DECLINE) {
1392 a->flags &= ~OAF_BOUND;
1393
1394 if (!(a->flags & OAF_STATIC) || a->lease->hostid != a->assigned) {
1395 memset(a->clid_data, 0, a->clid_len);
1396 a->valid_until = now + 3600; /* Block address for 1h */
1397 } else
1398 a->valid_until = now - 1;
1399 }
1400 } else if (hdr->msg_type == DHCPV6_MSG_CONFIRM) {
1401 if (ia_addr_present && !dhcpv6_ia_on_link(ia, a, iface)) {
1402 notonlink = true;
1403 break;
1404 }
1405
1406 if (!ia_addr_present || !a || !(a->flags & OAF_BOUND)) {
1407 response_len = 0;
1408 goto out;
1409 }
1410 }
1411
1412 buf += ia_response_len;
1413 buflen -= ia_response_len;
1414 response_len += ia_response_len;
1415 dhcpv6_log(hdr->msg_type, iface, now, duidbuf, is_pd, a, status);
1416 }
1417
1418 switch (hdr->msg_type) {
1419 case DHCPV6_MSG_RELEASE:
1420 case DHCPV6_MSG_DECLINE:
1421 case DHCPV6_MSG_CONFIRM:
1422 if (response_len + 6 < buflen) {
1423 buf[0] = 0;
1424 buf[1] = DHCPV6_OPT_STATUS;
1425 buf[2] = 0;
1426 buf[3] = 2;
1427 buf[4] = 0;
1428 buf[5] = (notonlink) ? DHCPV6_STATUS_NOTONLINK : DHCPV6_STATUS_OK;
1429 response_len += 6;
1430 }
1431 break;
1432
1433 default:
1434 break;
1435 }
1436
1437 dhcpv6_ia_write_statefile();
1438
1439 out:
1440 return response_len;
1441 }