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