Fix a segfault in state-script handling
[project/odhcpd.git] / src / dhcpv6-ia.c
1 /**
2 * Copyright (C) 2013 Steven Barth <steven@midlink.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15 #include "odhcpd.h"
16 #include "dhcpv6.h"
17 #include "dhcpv4.h"
18 #include "libubox/md5.h"
19
20 #include <time.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <alloca.h>
24 #include <resolv.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <arpa/inet.h>
29 #include <sys/timerfd.h>
30
31
32 static void update(struct interface *iface);
33 static void reconf_timer(struct uloop_timeout *event);
34 static struct uloop_timeout reconf_event = {.cb = reconf_timer};
35 static uint32_t serial = 0;
36 static uint8_t statemd5[16];
37
38
39 int dhcpv6_ia_init(void)
40 {
41 uloop_timeout_set(&reconf_event, 2000);
42 return 0;
43 }
44
45
46 int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
47 {
48 if (!enable && iface->ia_assignments.next) {
49 struct dhcpv6_assignment *c;
50 while (!list_empty(&iface->ia_assignments)) {
51 c = list_first_entry(&iface->ia_assignments, struct dhcpv6_assignment, head);
52 list_del(&c->head);
53 free(c->hostname);
54 free(c->classes);
55 free(c);
56 }
57 }
58
59 if (iface->dhcpv6 == RELAYD_SERVER) {
60 if (!iface->ia_assignments.next)
61 INIT_LIST_HEAD(&iface->ia_assignments);
62
63 if (list_empty(&iface->ia_assignments)) {
64 struct dhcpv6_assignment *border = calloc(1, sizeof(*border));
65 if (!border) {
66 syslog(LOG_ERR, "Calloc failed for border on interface %s", iface->ifname);
67 return -1;
68 }
69
70 border->length = 64;
71 list_add(&border->head, &iface->ia_assignments);
72 }
73
74 update(iface);
75
76 // Parse static entries
77 struct lease *lease;
78 list_for_each_entry(lease, &leases, head) {
79 // Construct entry
80 struct dhcpv6_assignment *a = calloc(1, sizeof(*a) + lease->duid_len);
81 if (!a) {
82 syslog(LOG_ERR, "Calloc failed for static lease assignment on interface %s",
83 iface->ifname);
84 return -1;
85 }
86
87 a->clid_len = lease->duid_len;
88 a->length = 128;
89 if (lease->hostid) {
90 a->assigned = lease->hostid;
91 } else {
92 uint32_t i4a = ntohl(lease->ipaddr.s_addr) & 0xff;
93 a->assigned = ((i4a / 100) << 8) | (((i4a % 100) / 10) << 4) | (i4a % 10);
94 }
95 odhcpd_urandom(a->key, sizeof(a->key));
96 memcpy(a->clid_data, lease->duid, a->clid_len);
97 memcpy(a->mac, lease->mac.ether_addr_octet, sizeof(a->mac));
98
99 // Assign to all interfaces
100 struct dhcpv6_assignment *c;
101 list_for_each_entry(c, &iface->ia_assignments, head) {
102 if (c->length != 128 || c->assigned > a->assigned) {
103 list_add_tail(&a->head, &c->head);
104 } else if (c->assigned == a->assigned) {
105 // Already an assignment with that number
106 break;
107 }
108 }
109
110 if (a->head.next) {
111 if (lease->hostname[0]) {
112 free(a->hostname);
113 a->hostname = strdup(lease->hostname);
114 }
115 } else {
116 free(a->classes);
117 free(a->hostname);
118 free(a);
119 }
120 }
121 }
122 return 0;
123 }
124
125
126 static int send_reconf(struct interface *iface, struct dhcpv6_assignment *assign)
127 {
128 struct {
129 struct dhcpv6_client_header hdr;
130 uint16_t srvid_type;
131 uint16_t srvid_len;
132 uint16_t duid_type;
133 uint16_t hardware_type;
134 uint8_t mac[6];
135 uint16_t msg_type;
136 uint16_t msg_len;
137 uint8_t msg_id;
138 struct dhcpv6_auth_reconfigure auth;
139 uint16_t clid_type;
140 uint16_t clid_len;
141 uint8_t clid_data[128];
142 } __attribute__((packed)) reconf_msg = {
143 .hdr = {DHCPV6_MSG_RECONFIGURE, {0, 0, 0}},
144 .srvid_type = htons(DHCPV6_OPT_SERVERID),
145 .srvid_len = htons(10),
146 .duid_type = htons(3),
147 .hardware_type = htons(1),
148 .msg_type = htons(DHCPV6_OPT_RECONF_MSG),
149 .msg_len = htons(1),
150 .msg_id = DHCPV6_MSG_RENEW,
151 .auth = {htons(DHCPV6_OPT_AUTH),
152 htons(sizeof(reconf_msg.auth) - 4), 3, 1, 0,
153 {htonl(time(NULL)), htonl(++serial)}, 2, {0}},
154 .clid_type = htons(DHCPV6_OPT_CLIENTID),
155 .clid_len = htons(assign->clid_len),
156 .clid_data = {0},
157 };
158
159 odhcpd_get_mac(iface, reconf_msg.mac);
160 memcpy(reconf_msg.clid_data, assign->clid_data, assign->clid_len);
161 struct iovec iov = {&reconf_msg, sizeof(reconf_msg) - 128 + assign->clid_len};
162
163 md5_ctx_t md5;
164 uint8_t secretbytes[16];
165 memcpy(secretbytes, assign->key, sizeof(secretbytes));
166
167 for (size_t i = 0; i < sizeof(secretbytes); ++i)
168 secretbytes[i] ^= 0x36;
169
170 md5_begin(&md5);
171 md5_hash(secretbytes, sizeof(secretbytes), &md5);
172 md5_hash(iov.iov_base, iov.iov_len, &md5);
173 md5_end(reconf_msg.auth.key, &md5);
174
175 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
176 secretbytes[i] ^= 0x36;
177 secretbytes[i] ^= 0x5c;
178 }
179
180 md5_begin(&md5);
181 md5_hash(secretbytes, sizeof(secretbytes), &md5);
182 md5_hash(reconf_msg.auth.key, 16, &md5);
183 md5_end(reconf_msg.auth.key, &md5);
184
185 return odhcpd_send(iface->dhcpv6_event.uloop.fd, &assign->peer, &iov, 1, iface);
186 }
187
188
189 void dhcpv6_write_statefile(void)
190 {
191 md5_ctx_t md5;
192 md5_begin(&md5);
193
194 if (config.dhcp_statefile) {
195 time_t now = odhcpd_time(), wall_time = time(NULL);
196 int fd = open(config.dhcp_statefile, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
197 if (fd < 0)
198 return;
199
200 lockf(fd, F_LOCK, 0);
201 ftruncate(fd, 0);
202
203 FILE *fp = fdopen(fd, "w");
204 if (!fp) {
205 close(fd);
206 return;
207 }
208
209 struct interface *iface;
210 list_for_each_entry(iface, &interfaces, head) {
211 if (iface->dhcpv6 != RELAYD_SERVER && iface->dhcpv4 != RELAYD_SERVER)
212 continue;
213
214 if (iface->dhcpv6 == RELAYD_SERVER && iface->ia_assignments.next) {
215 struct dhcpv6_assignment *c;
216 list_for_each_entry(c, &iface->ia_assignments, head) {
217 if (c->clid_len == 0)
218 continue;
219
220 char ipbuf[INET6_ADDRSTRLEN];
221 char leasebuf[512];
222 char duidbuf[264];
223 odhcpd_hexlify(duidbuf, c->clid_data, c->clid_len);
224
225 // iface DUID iaid hostname lifetime assigned length [addrs...]
226 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s %x %s %u %x %u ",
227 iface->ifname, duidbuf, ntohl(c->iaid),
228 (c->hostname ? c->hostname : "-"),
229 (unsigned)(c->valid_until > now ?
230 (c->valid_until - now + wall_time) : 0),
231 c->assigned, (unsigned)c->length);
232
233 struct in6_addr addr;
234 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
235 if (iface->ia_addr[i].prefix > 64)
236 continue;
237
238 addr = iface->ia_addr[i].addr;
239 if (c->length == 128)
240 addr.s6_addr32[3] = htonl(c->assigned);
241 else
242 addr.s6_addr32[1] |= htonl(c->assigned);
243 inet_ntop(AF_INET6, &addr, ipbuf, sizeof(ipbuf) - 1);
244
245 if (c->length == 128 && c->hostname && i == 0) {
246 fputs(ipbuf, fp);
247
248 char b[256];
249 if (dn_expand(iface->search, iface->search + iface->search_len,
250 iface->search, b, sizeof(b)) > 0)
251 fprintf(fp, "\t%s.%s", c->hostname, b);
252
253 fprintf(fp, "\t%s\n", c->hostname);
254 md5_hash(ipbuf, strlen(ipbuf), &md5);
255 md5_hash(c->hostname, strlen(c->hostname), &md5);
256 }
257
258 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/%hhu ", ipbuf, c->length);
259 }
260 leasebuf[l - 1] = '\n';
261 fwrite(leasebuf, 1, l, fp);
262 }
263 }
264
265 if (iface->dhcpv4 == RELAYD_SERVER && iface->dhcpv4_assignments.next) {
266 struct dhcpv4_assignment *c;
267 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
268 char ipbuf[INET6_ADDRSTRLEN];
269 char leasebuf[512];
270 char duidbuf[16];
271 odhcpd_hexlify(duidbuf, c->hwaddr, sizeof(c->hwaddr));
272
273 // iface DUID iaid hostname lifetime assigned length [addrs...]
274 int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s ipv4 %s %u %x 32 ",
275 iface->ifname, duidbuf,
276 (c->hostname ? c->hostname : "-"),
277 (unsigned)(c->valid_until > now ?
278 (c->valid_until - now + wall_time) : 0),
279 c->addr);
280
281 struct in_addr addr = {htonl(c->addr)};
282 inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf) - 1);
283
284 if (c->hostname[0]) {
285 fputs(ipbuf, fp);
286
287 char b[256];
288 if (dn_expand(iface->search, iface->search + iface->search_len,
289 iface->search, b, sizeof(b)) > 0)
290 fprintf(fp, "\t%s.%s", c->hostname, b);
291
292 fprintf(fp, "\t%s\n", c->hostname);
293 md5_hash(ipbuf, strlen(ipbuf), &md5);
294 md5_hash(c->hostname, strlen(c->hostname), &md5);
295 }
296
297 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/32 ", ipbuf);
298 leasebuf[l - 1] = '\n';
299 fwrite(leasebuf, 1, l, fp);
300 }
301 }
302 }
303
304 fclose(fp);
305 }
306
307 uint8_t newmd5[16];
308 md5_end(newmd5, &md5);
309
310 if (config.dhcp_cb && memcmp(newmd5, statemd5, sizeof(newmd5))) {
311 memcpy(statemd5, newmd5, sizeof(statemd5));
312 char *argv[2] = {config.dhcp_cb, NULL};
313 if (!vfork()) {
314 execv(argv[0], argv);
315 _exit(128);
316 }
317 }
318 }
319
320
321 static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bool add)
322 {
323 if (a->length > 64)
324 return;
325
326 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
327 struct in6_addr prefix = iface->ia_addr[i].addr;
328 prefix.s6_addr32[1] |= htonl(a->assigned);
329 odhcpd_setup_route(&prefix, a->length, iface, &a->peer.sin6_addr, add);
330 }
331 }
332
333
334 static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
335 {
336 struct dhcpv6_assignment *c;
337 if (iface->ia_addr_len < 1)
338 return false;
339
340 // Try honoring the hint first
341 uint32_t current = 1, asize = (1 << (64 - assign->length)) - 1;
342 if (assign->assigned) {
343 list_for_each_entry(c, &iface->ia_assignments, head) {
344 if (c->length == 128)
345 continue;
346
347 if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
348 list_add_tail(&assign->head, &c->head);
349 apply_lease(iface, assign, true);
350 return true;
351 }
352
353 if (c->assigned != 0)
354 current = (c->assigned + (1 << (64 - c->length)));
355 }
356 }
357
358 // Fallback to a variable assignment
359 current = 1;
360 list_for_each_entry(c, &iface->ia_assignments, head) {
361 if (c->length == 128)
362 continue;
363
364 current = (current + asize) & (~asize);
365 if (current + asize < c->assigned) {
366 assign->assigned = current;
367 list_add_tail(&assign->head, &c->head);
368 apply_lease(iface, assign, true);
369 return true;
370 }
371
372 if (c->assigned != 0)
373 current = (c->assigned + (1 << (64 - c->length)));
374 }
375
376 return false;
377 }
378
379
380 static bool assign_na(struct interface *iface, struct dhcpv6_assignment *assign)
381 {
382 bool match = false;
383 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
384 if (!iface->ia_addr[i].has_class) {
385 match = true;
386 continue;
387 } else if (assign->classes_cnt) {
388 for (size_t j = 0; j < assign->classes_cnt; ++j)
389 if (assign->classes[j] == iface->ia_addr[i].class)
390 match = true;
391 } else if (assign->all_class) {
392 match = true;
393 }
394 }
395
396 if (!match)
397 return false;
398
399 // Seed RNG with checksum of DUID
400 uint32_t seed = 0;
401 for (size_t i = 0; i < assign->clid_len; ++i)
402 seed += assign->clid_data[i];
403 srand(seed);
404
405 // Try to assign up to 100x
406 for (size_t i = 0; i < 100; ++i) {
407 uint32_t try;
408 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
409
410 struct dhcpv6_assignment *c;
411 list_for_each_entry(c, &iface->ia_assignments, head) {
412 if (c->assigned > try || c->length != 128) {
413 assign->assigned = try;
414 list_add_tail(&assign->head, &c->head);
415 return true;
416 } else if (c->assigned == try) {
417 break;
418 }
419 }
420 }
421
422 return false;
423 }
424
425
426 static int prefixcmp(const void *va, const void *vb)
427 {
428 const struct odhcpd_ipaddr *a = va, *b = vb;
429 uint32_t a_pref = ((a->addr.s6_addr[0] & 0xfe) != 0xfc) ? a->preferred : 1;
430 uint32_t b_pref = ((b->addr.s6_addr[0] & 0xfe) != 0xfc) ? b->preferred : 1;
431 return (a_pref < b_pref) ? 1 : (a_pref > b_pref) ? -1 : 0;
432 }
433
434
435 static void update(struct interface *iface)
436 {
437 struct odhcpd_ipaddr addr[8];
438 memset(addr, 0, sizeof(addr));
439 int len = odhcpd_get_interface_addresses(iface->ifindex, addr, 8);
440
441 if (len < 0)
442 return;
443
444 qsort(addr, len, sizeof(*addr), prefixcmp);
445
446 time_t now = odhcpd_time();
447 int minprefix = -1;
448
449 for (int i = 0; i < len; ++i) {
450 if (addr[i].prefix > minprefix)
451 minprefix = addr[i].prefix;
452
453 addr[i].addr.s6_addr32[2] = 0;
454 addr[i].addr.s6_addr32[3] = 0;
455
456 if (addr[i].preferred < UINT32_MAX - now)
457 addr[i].preferred += now;
458
459 if (addr[i].valid < UINT32_MAX - now)
460 addr[i].valid += now;
461 }
462
463 struct dhcpv6_assignment *border = list_last_entry(&iface->ia_assignments, struct dhcpv6_assignment, head);
464 border->assigned = 1 << (64 - minprefix);
465
466 bool change = len != (int)iface->ia_addr_len;
467 for (int i = 0; !change && i < len; ++i)
468 if (addr[i].addr.s6_addr32[0] != iface->ia_addr[i].addr.s6_addr32[0] ||
469 addr[i].addr.s6_addr32[1] != iface->ia_addr[i].addr.s6_addr32[1] ||
470 (addr[i].preferred > 0) != (iface->ia_addr[i].preferred > 0) ||
471 (addr[i].valid > (uint32_t)now + 7200) !=
472 (iface->ia_addr[i].valid > (uint32_t)now + 7200))
473 change = true;
474
475 if (change) {
476 struct dhcpv6_assignment *c;
477 list_for_each_entry(c, &iface->ia_assignments, head)
478 if (c != border)
479 apply_lease(iface, c, false);
480 }
481
482 memcpy(iface->ia_addr, addr, len * sizeof(*addr));
483 iface->ia_addr_len = len;
484
485 if (change) { // Addresses / prefixes have changed
486 struct list_head reassign = LIST_HEAD_INIT(reassign);
487 struct dhcpv6_assignment *c, *d;
488 list_for_each_entry_safe(c, d, &iface->ia_assignments, head) {
489 if (c->clid_len == 0 || c->valid_until < now)
490 continue;
491
492 if (c->length < 128 && c->assigned >= border->assigned && c != border)
493 list_move(&c->head, &reassign);
494 else if (c != border)
495 apply_lease(iface, c, true);
496
497 if (c->accept_reconf && c->reconf_cnt == 0) {
498 c->reconf_cnt = 1;
499 c->reconf_sent = now;
500 send_reconf(iface, c);
501
502 // Leave all other assignments of that client alone
503 struct dhcpv6_assignment *a;
504 list_for_each_entry(a, &iface->ia_assignments, head)
505 if (a != c && a->clid_len == c->clid_len &&
506 !memcmp(a->clid_data, c->clid_data, a->clid_len))
507 c->reconf_cnt = INT_MAX;
508 }
509 }
510
511 while (!list_empty(&reassign)) {
512 c = list_first_entry(&reassign, struct dhcpv6_assignment, head);
513 list_del(&c->head);
514 if (!assign_pd(iface, c)) {
515 c->assigned = 0;
516 list_add(&c->head, &iface->ia_assignments);
517 }
518 }
519
520 dhcpv6_write_statefile();
521 }
522 }
523
524
525 static void reconf_timer(struct uloop_timeout *event)
526 {
527 time_t now = odhcpd_time();
528 struct interface *iface;
529 list_for_each_entry(iface, &interfaces, head) {
530 if (iface->dhcpv6 != RELAYD_SERVER || iface->ia_assignments.next == NULL)
531 continue;
532
533 struct dhcpv6_assignment *a, *n;
534 list_for_each_entry_safe(a, n, &iface->ia_assignments, head) {
535 if (a->valid_until < now) {
536 if ((a->length < 128 && a->clid_len > 0) ||
537 (a->length == 128 && a->clid_len == 0)) {
538 list_del(&a->head);
539 free(a->classes);
540 free(a->hostname);
541 free(a);
542 }
543 } else if (a->reconf_cnt > 0 && a->reconf_cnt < 8 &&
544 now > a->reconf_sent + (1 << a->reconf_cnt)) {
545 ++a->reconf_cnt;
546 a->reconf_sent = now;
547 send_reconf(iface, a);
548 }
549 }
550
551 if (iface->ia_reconf) {
552 update(iface);
553 iface->ia_reconf = false;
554 }
555 }
556
557 uloop_timeout_set(event, 2000);
558 }
559
560
561 static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
562 const struct dhcpv6_ia_hdr *ia, struct dhcpv6_assignment *a,
563 struct interface *iface, bool request)
564 {
565 if (buflen < sizeof(*ia) + sizeof(struct dhcpv6_ia_prefix))
566 return 0;
567
568 struct dhcpv6_ia_hdr out = {ia->type, 0, ia->iaid, 0, 0};
569 size_t datalen = sizeof(out);
570 time_t now = odhcpd_time();
571
572 if (status) {
573 struct __attribute__((packed)) {
574 uint16_t type;
575 uint16_t len;
576 uint16_t value;
577 } stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
578 htons(status)};
579
580 memcpy(buf + datalen, &stat, sizeof(stat));
581 datalen += sizeof(stat);
582 } else {
583 if (a) {
584 uint32_t pref = 3600;
585 uint32_t valid = 3600;
586
587 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
588 bool match = true;
589 if (iface->ia_addr[i].has_class) {
590 match = false;
591 if (a->classes_cnt) {
592 for (size_t j = 0; j < a->classes_cnt; ++j)
593 if (a->classes[j] == iface->ia_addr[i].class)
594 match = true;
595 } else if (a->all_class) {
596 match = true;
597 }
598 }
599
600 if (!match)
601 continue;
602
603 uint32_t prefix_pref = iface->ia_addr[i].preferred - now;
604 uint32_t prefix_valid = iface->ia_addr[i].valid - now;
605
606 if (iface->ia_addr[i].prefix > 64 ||
607 iface->ia_addr[i].preferred <= (uint32_t)now)
608 continue;
609
610 if (prefix_pref > 86400)
611 prefix_pref = 86400;
612
613 if (prefix_valid > 86400)
614 prefix_valid = 86400;
615
616 #ifdef DHCPV6_OPT_PREFIX_CLASS
617 struct {
618 uint16_t code;
619 uint16_t length;
620 uint16_t class;
621 } pclass = {htons(DHCPV6_OPT_PREFIX_CLASS),
622 htons(2), htons(iface->ia_addr[i].class)};
623 #endif
624
625 if (a->length < 128) {
626 struct dhcpv6_ia_prefix p = {
627 .type = htons(DHCPV6_OPT_IA_PREFIX),
628 .len = htons(sizeof(p) - 4),
629 .preferred = htonl(prefix_pref),
630 .valid = htonl(prefix_valid),
631 .prefix = a->length,
632 .addr = iface->ia_addr[i].addr
633 };
634 p.addr.s6_addr32[1] |= htonl(a->assigned);
635 size_t entrlen = sizeof(p) - 4;
636
637 #ifdef DHCPV6_OPT_PREFIX_CLASS
638 if (iface->ia_addr[i].has_class) {
639 entrlen += sizeof(pclass);
640 p.len = htons(entrlen);
641 }
642 #endif
643
644 if (datalen + entrlen + 4 > buflen || a->assigned == 0)
645 continue;
646
647 memcpy(buf + datalen, &p, sizeof(p));
648 #ifdef DHCPV6_OPT_PREFIX_CLASS
649 memcpy(buf + datalen + sizeof(p), &pclass, sizeof(pclass));
650 #endif
651 datalen += entrlen + 4;
652 } else {
653 struct dhcpv6_ia_addr n = {
654 .type = htons(DHCPV6_OPT_IA_ADDR),
655 .len = htons(sizeof(n) - 4),
656 .addr = iface->ia_addr[i].addr,
657 .preferred = htonl(prefix_pref),
658 .valid = htonl(prefix_valid)
659 };
660 n.addr.s6_addr32[3] = htonl(a->assigned);
661 size_t entrlen = sizeof(n) - 4;
662
663 #ifdef DHCPV6_OPT_PREFIX_CLASS
664 if (iface->ia_addr[i].has_class) {
665 entrlen += sizeof(pclass);
666 n.len = htons(entrlen);
667 }
668 #endif
669
670 if (datalen + entrlen + 4 > buflen || a->assigned == 0)
671 continue;
672
673 memcpy(buf + datalen, &n, sizeof(n));
674 #ifdef DHCPV6_OPT_PREFIX_CLASS
675 memcpy(buf + datalen + sizeof(n), &pclass, sizeof(pclass));
676 #endif
677 datalen += entrlen + 4;
678 }
679
680 // Calculate T1 / T2 based on non-deprecated addresses
681 if (prefix_pref > 0) {
682 if (prefix_pref < pref)
683 pref = prefix_pref;
684
685 if (prefix_valid < valid)
686 valid = prefix_valid;
687 }
688 }
689
690 a->valid_until = valid + now;
691 out.t1 = htonl(pref * 5 / 10);
692 out.t2 = htonl(pref * 8 / 10);
693
694 if (!out.t1)
695 out.t1 = htonl(1);
696
697 if (!out.t2)
698 out.t2 = htonl(1);
699 }
700
701 if (!request) {
702 uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + 4;
703 uint16_t otype, olen;
704 dhcpv6_for_each_option((uint8_t*)&ia[1], end, otype, olen, odata) {
705 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&odata[-4];
706 struct dhcpv6_ia_addr *n = (struct dhcpv6_ia_addr*)&odata[-4];
707 if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*p) - 4) &&
708 (otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*n) - 4))
709 continue;
710
711 bool found = false;
712 if (a) {
713 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
714 if (iface->ia_addr[i].prefix > 64 ||
715 iface->ia_addr[i].preferred <= (uint32_t)now)
716 continue;
717
718 struct in6_addr addr = iface->ia_addr[i].addr;
719 if (ia->type == htons(DHCPV6_OPT_IA_PD)) {
720 addr.s6_addr32[1] |= htonl(a->assigned);
721
722 if (IN6_ARE_ADDR_EQUAL(&p->addr, &addr) &&
723 p->prefix == a->length)
724 found = true;
725 } else {
726 addr.s6_addr32[3] = htonl(a->assigned);
727
728 if (IN6_ARE_ADDR_EQUAL(&n->addr, &addr))
729 found = true;
730 }
731 }
732 }
733
734 if (!found) {
735 if (otype == DHCPV6_OPT_IA_PREFIX) {
736 struct dhcpv6_ia_prefix inv = {
737 .type = htons(DHCPV6_OPT_IA_PREFIX),
738 .len = htons(sizeof(inv) - 4),
739 .preferred = 0,
740 .valid = 0,
741 .prefix = p->prefix,
742 .addr = p->addr
743 };
744
745 if (datalen + sizeof(inv) > buflen)
746 continue;
747
748 memcpy(buf + datalen, &inv, sizeof(inv));
749 datalen += sizeof(inv);
750 } else {
751 struct dhcpv6_ia_addr inv = {
752 .type = htons(DHCPV6_OPT_IA_ADDR),
753 .len = htons(sizeof(inv) - 4),
754 .addr = n->addr,
755 .preferred = 0,
756 .valid = 0
757 };
758
759 if (datalen + sizeof(inv) > buflen)
760 continue;
761
762 memcpy(buf + datalen, &inv, sizeof(inv));
763 datalen += sizeof(inv);
764 }
765 }
766 }
767 }
768 }
769
770 out.len = htons(datalen - 4);
771 memcpy(buf, &out, sizeof(out));
772 return datalen;
773 }
774
775
776 size_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
777 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end)
778 {
779 time_t now = odhcpd_time();
780 size_t response_len = 0;
781 const struct dhcpv6_client_header *hdr = data;
782 uint8_t *start = (uint8_t*)&hdr[1], *odata;
783 uint16_t otype, olen;
784
785 // Find and parse client-id and hostname
786 bool accept_reconf = false;
787 uint8_t *clid_data = NULL, clid_len = 0, mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
788 char hostname[256];
789 size_t hostname_len = 0;
790 bool class_oro = false;
791 dhcpv6_for_each_option(start, end, otype, olen, odata) {
792 if (otype == DHCPV6_OPT_CLIENTID) {
793 clid_data = odata;
794 clid_len = olen;
795
796 if (olen == 14 && odata[0] == 0 && odata[1] == 1)
797 memcpy(mac, &odata[8], sizeof(mac));
798 else if (olen == 10 && odata[0] == 0 && odata[1] == 3)
799 memcpy(mac, &odata[4], sizeof(mac));
800 } else if (otype == DHCPV6_OPT_FQDN && olen >= 2 && olen <= 255) {
801 uint8_t fqdn_buf[256];
802 memcpy(fqdn_buf, odata, olen);
803 fqdn_buf[olen++] = 0;
804
805 if (dn_expand(&fqdn_buf[1], &fqdn_buf[olen], &fqdn_buf[1], hostname, sizeof(hostname)) > 0)
806 hostname_len = strcspn(hostname, ".");
807 } else if (otype == DHCPV6_OPT_ORO) {
808 #ifdef DHCPV6_OPT_PREFIX_CLASS
809 for (size_t i = 0; i + 1 < olen; i += 2) {
810 if (odata[i] == (DHCPV6_OPT_PREFIX_CLASS >> 8) &&
811 odata[i + 1] == (DHCPV6_OPT_PREFIX_CLASS & 0xff))
812 class_oro = true;
813 }
814 #endif
815 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
816 accept_reconf = true;
817 }
818 }
819
820 if (!clid_data || !clid_len || clid_len > 130)
821 goto out;
822
823 update(iface);
824 bool update_state = false;
825
826 struct dhcpv6_assignment *first = NULL;
827 dhcpv6_for_each_option(start, end, otype, olen, odata) {
828 bool is_pd = (otype == DHCPV6_OPT_IA_PD);
829 bool is_na = (otype == DHCPV6_OPT_IA_NA);
830 if (!is_pd && !is_na)
831 continue;
832
833 struct dhcpv6_ia_hdr *ia = (struct dhcpv6_ia_hdr*)&odata[-4];
834 size_t ia_response_len = 0;
835 uint8_t reqlen = (is_pd) ? 62 : 128;
836 uint32_t reqhint = 0;
837
838 const uint8_t classes_max = 32;
839 uint8_t classes_cnt = 0;
840 uint16_t classes[classes_max];
841
842 // Parse request hint for IA-PD
843 if (is_pd) {
844 uint8_t *sdata;
845 uint16_t stype, slen;
846 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
847 if (stype != DHCPV6_OPT_IA_PREFIX || slen < sizeof(struct dhcpv6_ia_prefix) - 4)
848 continue;
849
850 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&sdata[-4];
851 if (p->prefix) {
852 reqlen = p->prefix;
853 reqhint = ntohl(p->addr.s6_addr32[1]);
854 if (reqlen > 32 && reqlen <= 64)
855 reqhint &= (1U << (64 - reqlen)) - 1;
856 }
857
858 #ifdef DHCPV6_OPT_PREFIX_CLASS
859 uint8_t *xdata;
860 uint16_t xtype, xlen;
861 dhcpv6_for_each_option(&p[1], sdata + slen, xtype, xlen, xdata) {
862 if (xtype != DHCPV6_OPT_PREFIX_CLASS || xlen != 2)
863 continue;
864
865 if (classes_cnt >= classes_max)
866 continue;
867
868 classes[classes_cnt++] = (uint16_t)xdata[0] << 8 | (uint16_t)xdata[1];
869 }
870 #endif
871 }
872
873 if (reqlen > 64)
874 reqlen = 64;
875 } else if (is_na) {
876 uint8_t *sdata;
877 uint16_t stype, slen;
878 dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
879 if (stype != DHCPV6_OPT_IA_ADDR || slen < sizeof(struct dhcpv6_ia_addr) - 4)
880 continue;
881
882 #ifdef DHCPV6_OPT_PREFIX_CLASS
883 uint8_t *xdata;
884 uint16_t xtype, xlen;
885 struct dhcpv6_ia_addr *p = (struct dhcpv6_ia_addr*)&sdata[-4];
886 dhcpv6_for_each_option(&p[1], sdata + slen, xtype, xlen, xdata) {
887 if (xtype != DHCPV6_OPT_PREFIX_CLASS || xlen != 2)
888 continue;
889
890 if (classes_cnt >= classes_max)
891 continue;
892
893 classes[classes_cnt++] = (uint16_t)xdata[0] << 8 | (uint16_t)xdata[1];
894 }
895 #endif
896 }
897 }
898
899 // Find assignment
900 struct dhcpv6_assignment *c, *a = NULL;
901 list_for_each_entry(c, &iface->ia_assignments, head) {
902 if (((c->clid_len == clid_len && !memcmp(c->clid_data, clid_data, clid_len)) ||
903 (c->clid_len >= clid_len && !c->clid_data[0] && !c->clid_data[1]
904 && !memcmp(c->mac, mac, sizeof(mac)))) &&
905 (c->iaid == ia->iaid || c->valid_until < now) &&
906 ((is_pd && c->length <= 64) || (is_na && c->length == 128))) {
907 a = c;
908
909 // Reset state
910 apply_lease(iface, a, false);
911 memcpy(a->clid_data, clid_data, clid_len);
912 a->clid_len = clid_len;
913 a->iaid = ia->iaid;
914 a->peer = *addr;
915 a->reconf_cnt = 0;
916 a->reconf_sent = 0;
917 a->all_class = class_oro;
918 a->classes_cnt = classes_cnt;
919 a->classes = realloc(a->classes, classes_cnt * sizeof(uint16_t));
920 if (a->classes)
921 memcpy(a->classes, classes, classes_cnt * sizeof(uint16_t));
922 break;
923 }
924 }
925
926 // Generic message handling
927 uint16_t status = DHCPV6_STATUS_OK;
928 if (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_REQUEST) {
929 bool assigned = !!a;
930
931 if (!a && !iface->no_dynamic_dhcp) { // Create new binding
932 a = calloc(1, sizeof(*a) + clid_len);
933 if (a) {
934 a->clid_len = clid_len;
935 a->iaid = ia->iaid;
936 a->length = reqlen;
937 a->peer = *addr;
938 a->assigned = reqhint;
939 a->all_class = class_oro;
940 a->classes_cnt = classes_cnt;
941 if (classes_cnt) {
942 a->classes = malloc(classes_cnt * sizeof(uint16_t));
943 if (a->classes)
944 memcpy(a->classes, classes, classes_cnt * sizeof(uint16_t));
945 }
946
947 if (first)
948 memcpy(a->key, first->key, sizeof(a->key));
949 else
950 odhcpd_urandom(a->key, sizeof(a->key));
951 memcpy(a->clid_data, clid_data, clid_len);
952
953 if (is_pd)
954 while (!(assigned = assign_pd(iface, a)) && ++a->length <= 64);
955 else
956 assigned = assign_na(iface, a);
957 }
958 }
959
960 if (!assigned || iface->ia_addr_len == 0) { // Set error status
961 status = (is_pd) ? DHCPV6_STATUS_NOPREFIXAVAIL : DHCPV6_STATUS_NOADDRSAVAIL;
962 } else if (assigned && !first) { //
963 size_t handshake_len = 4;
964 buf[0] = 0;
965 buf[1] = DHCPV6_OPT_RECONF_ACCEPT;
966 buf[2] = 0;
967 buf[3] = 0;
968
969 if (hdr->msg_type == DHCPV6_MSG_REQUEST) {
970 struct dhcpv6_auth_reconfigure auth = {
971 htons(DHCPV6_OPT_AUTH),
972 htons(sizeof(auth) - 4),
973 3, 1, 0,
974 {htonl(time(NULL)), htonl(++serial)},
975 1,
976 {0}
977 };
978 memcpy(auth.key, a->key, sizeof(a->key));
979 memcpy(buf + handshake_len, &auth, sizeof(auth));
980 handshake_len += sizeof(auth);
981 }
982
983 buf += handshake_len;
984 buflen -= handshake_len;
985 response_len += handshake_len;
986
987 first = a;
988 }
989
990 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
991
992 // Was only a solicitation: mark binding for removal
993 if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
994 a->valid_until = 0;
995 } else if (assigned && hdr->msg_type == DHCPV6_MSG_REQUEST) {
996 if (hostname_len > 0) {
997 a->hostname = realloc(a->hostname, hostname_len + 1);
998 if (a->hostname) {
999 memcpy(a->hostname, hostname, hostname_len);
1000 a->hostname[hostname_len] = 0;
1001 }
1002 }
1003 a->accept_reconf = accept_reconf;
1004 apply_lease(iface, a, true);
1005 update_state = true;
1006 } else if (!assigned && a) { // Cleanup failed assignment
1007 free(a->classes);
1008 free(a->hostname);
1009 free(a);
1010 }
1011 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1012 hdr->msg_type == DHCPV6_MSG_RELEASE ||
1013 hdr->msg_type == DHCPV6_MSG_REBIND ||
1014 hdr->msg_type == DHCPV6_MSG_DECLINE) {
1015 if (!a && hdr->msg_type != DHCPV6_MSG_REBIND) {
1016 status = DHCPV6_STATUS_NOBINDING;
1017 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1018 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1019 hdr->msg_type == DHCPV6_MSG_REBIND) {
1020 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1021 if (a)
1022 apply_lease(iface, a, true);
1023 } else if (hdr->msg_type == DHCPV6_MSG_RELEASE) {
1024 a->valid_until = 0;
1025 apply_lease(iface, a, false);
1026 update_state = true;
1027 } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
1028 a->clid_len = 0;
1029 a->valid_until = now + 3600; // Block address for 1h
1030 update_state = true;
1031 }
1032 } else if (hdr->msg_type == DHCPV6_MSG_CONFIRM) {
1033 // Always send NOTONLINK for CONFIRM so that clients restart connection
1034 status = DHCPV6_STATUS_NOTONLINK;
1035 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1036 }
1037
1038 buf += ia_response_len;
1039 buflen -= ia_response_len;
1040 response_len += ia_response_len;
1041 }
1042
1043 if (hdr->msg_type == DHCPV6_MSG_RELEASE && response_len + 6 < buflen) {
1044 buf[0] = 0;
1045 buf[1] = DHCPV6_OPT_STATUS;
1046 buf[2] = 0;
1047 buf[3] = 2;
1048 buf[4] = 0;
1049 buf[5] = DHCPV6_STATUS_OK;
1050 response_len += 6;
1051 }
1052
1053 if (update_state)
1054 dhcpv6_write_statefile();
1055
1056 out:
1057 return response_len;
1058 }