2 * Copyright (C) 2013 Steven Barth <steven@midlink.org>
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.
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.
28 #include <arpa/inet.h>
29 #include <sys/timerfd.h>
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 int socket_fd
= -1;
36 static uint32_t serial
= 0;
39 int dhcpv6_ia_init(int dhcpv6_socket
)
41 socket_fd
= dhcpv6_socket
;
42 uloop_timeout_set(&reconf_event
, 2000);
47 int setup_dhcpv6_ia_interface(struct interface
*iface
, bool enable
)
49 if (!enable
&& iface
->ia_assignments
.next
) {
50 struct dhcpv6_assignment
*c
;
51 while (!list_empty(&iface
->ia_assignments
)) {
52 c
= list_first_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
60 if (iface
->dhcpv6
== RELAYD_SERVER
) {
61 if (!iface
->ia_assignments
.next
)
62 INIT_LIST_HEAD(&iface
->ia_assignments
);
64 if (list_empty(&iface
->ia_assignments
)) {
65 struct dhcpv6_assignment
*border
= calloc(1, sizeof(*border
));
67 list_add(&border
->head
, &iface
->ia_assignments
);
72 // Parse static entries
74 list_for_each_entry(lease
, &leases
, head
) {
76 struct dhcpv6_assignment
*a
= calloc(1, sizeof(*a
) + lease
->duid_len
);
77 a
->clid_len
= lease
->duid_len
;
79 a
->assigned
= lease
->hostid
;
80 odhcpd_urandom(a
->key
, sizeof(a
->key
));
81 memcpy(a
->clid_data
, lease
->duid
, a
->clid_len
);
82 memcpy(a
->mac
, lease
->mac
.ether_addr_octet
, sizeof(a
->mac
));
84 // Assign to all interfaces
85 struct dhcpv6_assignment
*c
;
86 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
87 if (c
->length
!= 128 || c
->assigned
> a
->assigned
) {
88 list_add_tail(&a
->head
, &c
->head
);
89 } else if (c
->assigned
== a
->assigned
) {
90 // Already an assignment with that number
96 if (lease
->hostname
[0]) {
98 a
->hostname
= strdup(lease
->hostname
);
111 static int send_reconf(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
114 struct dhcpv6_client_header hdr
;
118 uint16_t hardware_type
;
123 struct dhcpv6_auth_reconfigure auth
;
126 uint8_t clid_data
[128];
127 } __attribute__((packed
)) reconf_msg
= {
128 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
129 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
130 .srvid_len
= htons(10),
131 .duid_type
= htons(3),
132 .hardware_type
= htons(1),
133 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
135 .msg_id
= DHCPV6_MSG_RENEW
,
136 .auth
= {htons(DHCPV6_OPT_AUTH
),
137 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
138 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
139 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
140 .clid_len
= htons(assign
->clid_len
),
144 odhcpd_get_mac(iface
, reconf_msg
.mac
);
145 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
146 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
149 uint8_t secretbytes
[16];
150 memcpy(secretbytes
, assign
->key
, sizeof(secretbytes
));
152 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
153 secretbytes
[i
] ^= 0x36;
156 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
157 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
158 md5_end(reconf_msg
.auth
.key
, &md5
);
160 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
161 secretbytes
[i
] ^= 0x36;
162 secretbytes
[i
] ^= 0x5c;
166 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
167 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
168 md5_end(reconf_msg
.auth
.key
, &md5
);
170 return odhcpd_send(socket_fd
, &assign
->peer
, &iov
, 1, iface
);
174 void dhcpv6_write_statefile(void)
176 if (config
.dhcp_statefile
) {
177 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
178 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
182 lockf(fd
, F_LOCK
, 0);
185 FILE *fp
= fdopen(fd
, "w");
191 struct interface
*iface
;
192 list_for_each_entry(iface
, &interfaces
, head
) {
193 if (iface
->dhcpv6
!= RELAYD_SERVER
&& iface
->dhcpv4
!= RELAYD_SERVER
)
196 if (iface
->dhcpv6
== RELAYD_SERVER
) {
197 struct dhcpv6_assignment
*c
;
198 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
199 if (c
->clid_len
== 0)
202 char ipbuf
[INET6_ADDRSTRLEN
];
205 odhcpd_hexlify(duidbuf
, c
->clid_data
, c
->clid_len
);
207 // iface DUID iaid hostname lifetime assigned length [addrs...]
208 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s %x %s %u %x %u ",
209 iface
->ifname
, duidbuf
, ntohl(c
->iaid
),
210 (c
->hostname
? c
->hostname
: "-"),
211 (unsigned)(c
->valid_until
> now
?
212 (c
->valid_until
- now
+ wall_time
) : 0),
213 c
->assigned
, (unsigned)c
->length
);
215 struct in6_addr addr
;
216 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
217 if (iface
->ia_addr
[i
].prefix
> 64)
220 addr
= iface
->ia_addr
[i
].addr
;
221 if (c
->length
== 128)
222 addr
.s6_addr32
[3] = htonl(c
->assigned
);
224 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
225 inet_ntop(AF_INET6
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
227 if (c
->length
== 128 && c
->hostname
&& i
== 0)
228 fprintf(fp
, "%s\t%s\n", ipbuf
, c
->hostname
);
230 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/%hhu ", ipbuf
, c
->length
);
232 leasebuf
[l
- 1] = '\n';
233 fwrite(leasebuf
, 1, l
, fp
);
237 if (iface
->dhcpv4
== RELAYD_SERVER
) {
238 struct dhcpv4_assignment
*c
;
239 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
240 char ipbuf
[INET6_ADDRSTRLEN
];
243 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
245 // iface DUID iaid hostname lifetime assigned length [addrs...]
246 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s ipv4 %s %u %x 32 ",
247 iface
->ifname
, duidbuf
,
248 (c
->hostname
? c
->hostname
: "-"),
249 (unsigned)(c
->valid_until
> now
?
250 (c
->valid_until
- now
+ wall_time
) : 0),
253 struct in_addr addr
= {htonl(c
->addr
)};
254 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
257 fprintf(fp
, "%s\t%s\n", ipbuf
, c
->hostname
);
259 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/32 ", ipbuf
);
260 leasebuf
[l
- 1] = '\n';
261 fwrite(leasebuf
, 1, l
, fp
);
269 if (config
.dhcp_cb
) {
270 char *argv
[2] = {config
.dhcp_cb
, NULL
};
272 execv(argv
[0], argv
);
279 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
284 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
285 struct in6_addr prefix
= iface
->ia_addr
[i
].addr
;
286 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
287 odhcpd_setup_route(&prefix
, a
->length
, iface
, &a
->peer
.sin6_addr
, add
);
292 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
294 struct dhcpv6_assignment
*c
;
295 if (iface
->ia_addr_len
< 1)
298 // Try honoring the hint first
299 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
300 if (assign
->assigned
) {
301 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
302 if (c
->length
== 128)
305 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
306 list_add_tail(&assign
->head
, &c
->head
);
307 apply_lease(iface
, assign
, true);
311 if (c
->assigned
!= 0)
312 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
316 // Fallback to a variable assignment
318 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
319 if (c
->length
== 128)
322 current
= (current
+ asize
) & (~asize
);
323 if (current
+ asize
< c
->assigned
) {
324 assign
->assigned
= current
;
325 list_add_tail(&assign
->head
, &c
->head
);
326 apply_lease(iface
, assign
, true);
330 if (c
->assigned
!= 0)
331 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
338 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
341 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
342 if (!iface
->ia_addr
[i
].has_class
) {
345 } else if (assign
->classes_cnt
) {
346 for (size_t j
= 0; j
< assign
->classes_cnt
; ++j
)
347 if (assign
->classes
[j
] == iface
->ia_addr
[i
].class)
349 } else if (assign
->all_class
) {
357 // Seed RNG with checksum of DUID
359 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
360 seed
+= assign
->clid_data
[i
];
363 // Try to assign up to 100x
364 for (size_t i
= 0; i
< 100; ++i
) {
366 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
368 struct dhcpv6_assignment
*c
;
369 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
370 if (c
->assigned
> try || c
->length
!= 128) {
371 assign
->assigned
= try;
372 list_add_tail(&assign
->head
, &c
->head
);
374 } else if (c
->assigned
== try) {
384 static int prefixcmp(const void *va
, const void *vb
)
386 const struct odhcpd_ipaddr
*a
= va
, *b
= vb
;
387 uint32_t a_pref
= ((a
->addr
.s6_addr
[0] & 0xfe) != 0xfc) ? a
->preferred
: 1;
388 uint32_t b_pref
= ((b
->addr
.s6_addr
[0] & 0xfe) != 0xfc) ? b
->preferred
: 1;
389 return (a_pref
< b_pref
) ? 1 : (a_pref
> b_pref
) ? -1 : 0;
393 static void update(struct interface
*iface
)
395 struct odhcpd_ipaddr addr
[8];
396 memset(addr
, 0, sizeof(addr
));
397 int len
= odhcpd_get_interface_addresses(iface
->ifindex
, addr
, 8);
402 qsort(addr
, len
, sizeof(*addr
), prefixcmp
);
404 time_t now
= odhcpd_time();
407 for (int i
= 0; i
< len
; ++i
) {
408 if (addr
[i
].prefix
> minprefix
)
409 minprefix
= addr
[i
].prefix
;
411 addr
[i
].addr
.s6_addr32
[2] = 0;
412 addr
[i
].addr
.s6_addr32
[3] = 0;
414 if (addr
[i
].preferred
< UINT32_MAX
- now
)
415 addr
[i
].preferred
+= now
;
417 if (addr
[i
].valid
< UINT32_MAX
- now
)
418 addr
[i
].valid
+= now
;
421 struct dhcpv6_assignment
*border
= list_last_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
422 border
->assigned
= 1 << (64 - minprefix
);
424 bool change
= len
!= (int)iface
->ia_addr_len
;
425 for (int i
= 0; !change
&& i
< len
; ++i
)
426 if (addr
[i
].addr
.s6_addr32
[0] != iface
->ia_addr
[i
].addr
.s6_addr32
[0] ||
427 addr
[i
].addr
.s6_addr32
[1] != iface
->ia_addr
[i
].addr
.s6_addr32
[1] ||
428 (addr
[i
].preferred
> 0) != (iface
->ia_addr
[i
].preferred
> 0) ||
429 (addr
[i
].valid
> (uint32_t)now
+ 7200) !=
430 (iface
->ia_addr
[i
].valid
> (uint32_t)now
+ 7200))
434 struct dhcpv6_assignment
*c
;
435 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
437 apply_lease(iface
, c
, false);
440 memcpy(iface
->ia_addr
, addr
, len
* sizeof(*addr
));
441 iface
->ia_addr_len
= len
;
443 if (change
) { // Addresses / prefixes have changed
444 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
445 struct dhcpv6_assignment
*c
, *d
;
446 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
447 if (c
->clid_len
== 0 || c
->valid_until
< now
)
450 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
451 list_move(&c
->head
, &reassign
);
452 else if (c
!= border
)
453 apply_lease(iface
, c
, true);
455 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
457 c
->reconf_sent
= now
;
458 send_reconf(iface
, c
);
460 // Leave all other assignments of that client alone
461 struct dhcpv6_assignment
*a
;
462 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
463 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
464 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
465 c
->reconf_cnt
= INT_MAX
;
469 while (!list_empty(&reassign
)) {
470 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
472 if (!assign_pd(iface
, c
)) {
474 list_add(&c
->head
, &iface
->ia_assignments
);
478 dhcpv6_write_statefile();
483 static void reconf_timer(struct uloop_timeout
*event
)
485 time_t now
= odhcpd_time();
486 struct interface
*iface
;
487 list_for_each_entry(iface
, &interfaces
, head
) {
488 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
491 struct dhcpv6_assignment
*a
, *n
;
492 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
493 if (a
->valid_until
< now
) {
494 if ((a
->length
< 128 && a
->clid_len
> 0) ||
495 (a
->length
== 128 && a
->clid_len
== 0)) {
501 } else if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< 8 &&
502 now
> a
->reconf_sent
+ (1 << a
->reconf_cnt
)) {
504 a
->reconf_sent
= now
;
505 send_reconf(iface
, a
);
509 if (iface
->ia_reconf
) {
511 iface
->ia_reconf
= false;
515 uloop_timeout_set(event
, 2000);
519 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
520 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
521 struct interface
*iface
, bool request
)
523 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
526 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
527 size_t datalen
= sizeof(out
);
528 time_t now
= odhcpd_time();
531 struct __attribute__((packed
)) {
535 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
538 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
539 datalen
+= sizeof(stat
);
542 uint32_t pref
= 3600;
543 uint32_t valid
= 3600;
544 bool have_non_ula
= false;
545 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
)
546 if ((iface
->ia_addr
[i
].addr
.s6_addr
[0] & 0xfe) != 0xfc)
549 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
551 if (iface
->ia_addr
[i
].has_class
) {
553 if (a
->classes_cnt
) {
554 for (size_t j
= 0; j
< a
->classes_cnt
; ++j
)
555 if (a
->classes
[j
] == iface
->ia_addr
[i
].class)
557 } else if (a
->all_class
) {
565 uint32_t prefix_pref
= iface
->ia_addr
[i
].preferred
- now
;
566 uint32_t prefix_valid
= iface
->ia_addr
[i
].valid
- now
;
568 if (iface
->ia_addr
[i
].prefix
> 64 ||
569 iface
->ia_addr
[i
].preferred
<= (uint32_t)now
)
572 // ULA-deprecation compatibility workaround
573 if ((iface
->ia_addr
[i
].addr
.s6_addr
[0] & 0xfe) == 0xfc &&
574 a
->length
== 128 && have_non_ula
&&
575 iface
->deprecate_ula_if_public_avail
)
578 if (prefix_pref
> 86400)
581 if (prefix_valid
> 86400)
582 prefix_valid
= 86400;
584 #ifdef DHCPV6_OPT_PREFIX_CLASS
589 } pclass
= {htons(DHCPV6_OPT_PREFIX_CLASS
),
590 htons(2), htons(iface
->ia_addr
[i
].class)};
593 if (a
->length
< 128) {
594 struct dhcpv6_ia_prefix p
= {
595 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
596 .len
= htons(sizeof(p
) - 4),
597 .preferred
= htonl(prefix_pref
),
598 .valid
= htonl(prefix_valid
),
600 .addr
= iface
->ia_addr
[i
].addr
602 p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
603 size_t entrlen
= sizeof(p
) - 4;
605 #ifdef DHCPV6_OPT_PREFIX_CLASS
606 if (iface
->ia_addr
[i
].has_class
) {
607 entrlen
+= sizeof(pclass
);
608 p
.len
= htons(entrlen
);
612 if (datalen
+ entrlen
+ 4 > buflen
|| a
->assigned
== 0)
615 memcpy(buf
+ datalen
, &p
, sizeof(p
));
616 #ifdef DHCPV6_OPT_PREFIX_CLASS
617 memcpy(buf
+ datalen
+ sizeof(p
), &pclass
, sizeof(pclass
));
619 datalen
+= entrlen
+ 4;
621 struct dhcpv6_ia_addr n
= {
622 .type
= htons(DHCPV6_OPT_IA_ADDR
),
623 .len
= htons(sizeof(n
) - 4),
624 .addr
= iface
->ia_addr
[i
].addr
,
625 .preferred
= htonl(prefix_pref
),
626 .valid
= htonl(prefix_valid
)
628 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
629 size_t entrlen
= sizeof(n
) - 4;
631 #ifdef DHCPV6_OPT_PREFIX_CLASS
632 if (iface
->ia_addr
[i
].has_class
) {
633 entrlen
+= sizeof(pclass
);
634 n
.len
= htons(entrlen
);
638 if (datalen
+ entrlen
+ 4 > buflen
|| a
->assigned
== 0)
641 memcpy(buf
+ datalen
, &n
, sizeof(n
));
642 #ifdef DHCPV6_OPT_PREFIX_CLASS
643 memcpy(buf
+ datalen
+ sizeof(n
), &pclass
, sizeof(pclass
));
645 datalen
+= entrlen
+ 4;
648 // Calculate T1 / T2 based on non-deprecated addresses
649 if (prefix_pref
> 0) {
650 if (prefix_pref
< pref
)
653 if (prefix_valid
< valid
)
654 valid
= prefix_valid
;
658 a
->valid_until
= valid
+ now
;
659 out
.t1
= htonl(pref
* 5 / 10);
660 out
.t2
= htonl(pref
* 8 / 10);
670 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
671 uint16_t otype
, olen
;
672 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
673 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
674 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
675 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
676 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
681 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
682 if (iface
->ia_addr
[i
].prefix
> 64 ||
683 iface
->ia_addr
[i
].preferred
<= (uint32_t)now
)
686 struct in6_addr addr
= iface
->ia_addr
[i
].addr
;
687 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
688 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
690 if (IN6_ARE_ADDR_EQUAL(&p
->addr
, &addr
) &&
691 p
->prefix
== a
->length
)
694 addr
.s6_addr32
[3] = htonl(a
->assigned
);
696 if (IN6_ARE_ADDR_EQUAL(&n
->addr
, &addr
))
703 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
704 struct dhcpv6_ia_prefix inv
= {
705 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
706 .len
= htons(sizeof(inv
) - 4),
713 if (datalen
+ sizeof(inv
) > buflen
)
716 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
717 datalen
+= sizeof(inv
);
719 struct dhcpv6_ia_addr inv
= {
720 .type
= htons(DHCPV6_OPT_IA_ADDR
),
721 .len
= htons(sizeof(inv
) - 4),
727 if (datalen
+ sizeof(inv
) > buflen
)
730 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
731 datalen
+= sizeof(inv
);
738 out
.len
= htons(datalen
- 4);
739 memcpy(buf
, &out
, sizeof(out
));
744 size_t dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
745 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
747 time_t now
= odhcpd_time();
748 size_t response_len
= 0;
749 const struct dhcpv6_client_header
*hdr
= data
;
750 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
751 uint16_t otype
, olen
;
753 // Find and parse client-id and hostname
754 bool accept_reconf
= false;
755 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
757 size_t hostname_len
= 0;
758 bool class_oro
= false;
759 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
760 if (otype
== DHCPV6_OPT_CLIENTID
) {
764 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
765 memcpy(mac
, &odata
[8], sizeof(mac
));
766 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
767 memcpy(mac
, &odata
[4], sizeof(mac
));
768 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
769 uint8_t fqdn_buf
[256];
770 memcpy(fqdn_buf
, odata
, olen
);
771 fqdn_buf
[olen
++] = 0;
773 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
774 hostname_len
= strcspn(hostname
, ".");
775 } else if (otype
== DHCPV6_OPT_ORO
) {
776 #ifdef DHCPV6_OPT_PREFIX_CLASS
777 for (size_t i
= 0; i
+ 1 < olen
; i
+= 2) {
778 if (odata
[i
] == (DHCPV6_OPT_PREFIX_CLASS
>> 8) &&
779 odata
[i
+ 1] == (DHCPV6_OPT_PREFIX_CLASS
& 0xff))
783 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
) {
784 accept_reconf
= true;
788 if (!clid_data
|| !clid_len
|| clid_len
> 130)
792 bool update_state
= false;
794 struct dhcpv6_assignment
*first
= NULL
;
795 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
796 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
797 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
798 if (!is_pd
&& !is_na
)
801 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
802 size_t ia_response_len
= 0;
803 uint8_t reqlen
= (is_pd
) ? 62 : 128;
804 uint32_t reqhint
= 0;
806 const uint8_t classes_max
= 32;
807 uint8_t classes_cnt
= 0;
808 uint16_t classes
[classes_max
];
810 // Parse request hint for IA-PD
813 uint16_t stype
, slen
;
814 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
815 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
818 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
821 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
822 if (reqlen
> 32 && reqlen
<= 64)
823 reqhint
&= (1U << (64 - reqlen
)) - 1;
826 #ifdef DHCPV6_OPT_PREFIX_CLASS
828 uint16_t xtype
, xlen
;
829 dhcpv6_for_each_option(&p
[1], sdata
+ slen
, xtype
, xlen
, xdata
) {
830 if (xtype
!= DHCPV6_OPT_PREFIX_CLASS
|| xlen
!= 2)
833 if (classes_cnt
>= classes_max
)
836 classes
[classes_cnt
++] = (uint16_t)xdata
[0] << 8 | (uint16_t)xdata
[1];
845 uint16_t stype
, slen
;
846 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
847 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
850 #ifdef DHCPV6_OPT_PREFIX_CLASS
852 uint16_t xtype
, xlen
;
853 struct dhcpv6_ia_addr
*p
= (struct dhcpv6_ia_addr
*)&sdata
[-4];
854 dhcpv6_for_each_option(&p
[1], sdata
+ slen
, xtype
, xlen
, xdata
) {
855 if (xtype
!= DHCPV6_OPT_PREFIX_CLASS
|| xlen
!= 2)
858 if (classes_cnt
>= classes_max
)
861 classes
[classes_cnt
++] = (uint16_t)xdata
[0] << 8 | (uint16_t)xdata
[1];
868 struct dhcpv6_assignment
*c
, *a
= NULL
;
869 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
870 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
871 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
872 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
873 (c
->iaid
== ia
->iaid
|| c
->valid_until
< now
) &&
874 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
878 apply_lease(iface
, a
, false);
879 memcpy(a
->clid_data
, clid_data
, clid_len
);
880 a
->clid_len
= clid_len
;
889 // Generic message handling
890 uint16_t status
= DHCPV6_STATUS_OK
;
891 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
894 if (!a
&& !iface
->no_dynamic_dhcp
) { // Create new binding
895 a
= calloc(1, sizeof(*a
) + clid_len
);
896 a
->clid_len
= clid_len
;
900 a
->assigned
= reqhint
;
901 a
->all_class
= class_oro
;
902 a
->classes_cnt
= classes_cnt
;
904 a
->classes
= malloc(classes_cnt
* sizeof(uint16_t));
905 memcpy(a
->classes
, classes
, classes_cnt
* sizeof(uint16_t));
909 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
911 odhcpd_urandom(a
->key
, sizeof(a
->key
));
912 memcpy(a
->clid_data
, clid_data
, clid_len
);
915 while (!(assigned
= assign_pd(iface
, a
)) && ++a
->length
<= 64);
917 assigned
= assign_na(iface
, a
);
920 if (!assigned
|| iface
->ia_addr_len
== 0) { // Set error status
921 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
922 } else if (assigned
&& !first
) { //
923 size_t handshake_len
= 4;
925 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
929 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
930 struct dhcpv6_auth_reconfigure auth
= {
931 htons(DHCPV6_OPT_AUTH
),
932 htons(sizeof(auth
) - 4),
934 {htonl(time(NULL
)), htonl(++serial
)},
938 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
939 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
940 handshake_len
+= sizeof(auth
);
943 buf
+= handshake_len
;
944 buflen
-= handshake_len
;
945 response_len
+= handshake_len
;
950 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
952 // Was only a solicitation: mark binding for removal
953 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
955 } else if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
956 if (hostname_len
> 0) {
957 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
958 memcpy(a
->hostname
, hostname
, hostname_len
);
959 a
->hostname
[hostname_len
] = 0;
961 a
->accept_reconf
= accept_reconf
;
962 apply_lease(iface
, a
, true);
964 } else if (!assigned
&& a
) { // Cleanup failed assignment
969 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
970 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
971 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
972 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
973 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
974 status
= DHCPV6_STATUS_NOBINDING
;
975 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
976 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
977 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
978 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
980 apply_lease(iface
, a
, true);
981 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
983 apply_lease(iface
, a
, false);
985 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
987 a
->valid_until
= now
+ 3600; // Block address for 1h
990 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
) {
991 // Always send NOTONLINK for CONFIRM so that clients restart connection
992 status
= DHCPV6_STATUS_NOTONLINK
;
993 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
996 buf
+= ia_response_len
;
997 buflen
-= ia_response_len
;
998 response_len
+= ia_response_len
;
1001 if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
&& response_len
+ 6 < buflen
) {
1003 buf
[1] = DHCPV6_OPT_STATUS
;
1007 buf
[5] = DHCPV6_STATUS_OK
;
1012 dhcpv6_write_statefile();
1015 return response_len
;