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