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