2 * Copyright (C) 2013 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2016 Hans Dedecker <dedeckeh@gmail.com>
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.
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.
33 #include <arpa/inet.h>
34 #include <sys/timerfd.h>
36 #include <libubox/md5.h>
37 #include <libubox/usock.h>
39 #define ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) \
40 ((iface)->dhcpv6_assignall || (i) == (m) || \
41 (addrs)[(i)].prefix > 64)
43 static void dhcpv6_netevent_cb(unsigned long event
, struct netevent_handler_info
*info
);
44 static void apply_lease(struct dhcp_assignment
*a
, bool add
);
45 static void set_border_assignment_size(struct interface
*iface
, struct dhcp_assignment
*b
);
46 static void handle_addrlist_change(struct netevent_handler_info
*info
);
47 static void start_reconf(struct dhcp_assignment
*a
);
48 static void stop_reconf(struct dhcp_assignment
*a
);
49 static void valid_until_cb(struct uloop_timeout
*event
);
51 static struct netevent_handler dhcpv6_netevent_handler
= { .cb
= dhcpv6_netevent_cb
, };
52 static struct uloop_timeout valid_until_timeout
= {.cb
= valid_until_cb
};
53 static uint32_t serial
= 0;
54 static uint8_t statemd5
[16];
56 int dhcpv6_ia_init(void)
58 uloop_timeout_set(&valid_until_timeout
, 1000);
60 netlink_add_netevent_handler(&dhcpv6_netevent_handler
);
65 int dhcpv6_ia_setup_interface(struct interface
*iface
, bool enable
)
67 enable
= enable
&& (iface
->dhcpv6
== MODE_SERVER
);
70 struct dhcp_assignment
*border
;
72 if (list_empty(&iface
->ia_assignments
)) {
73 border
= alloc_assignment(0);
76 warn("Failed to alloc border on %s", iface
->name
);
81 list_add(&border
->head
, &iface
->ia_assignments
);
83 border
= list_last_entry(&iface
->ia_assignments
, struct dhcp_assignment
, head
);
85 set_border_assignment_size(iface
, border
);
87 struct dhcp_assignment
*c
;
89 while (!list_empty(&iface
->ia_assignments
)) {
90 c
= list_first_entry(&iface
->ia_assignments
, struct dhcp_assignment
, head
);
99 static void dhcpv6_netevent_cb(unsigned long event
, struct netevent_handler_info
*info
)
101 struct interface
*iface
= info
->iface
;
103 if (!iface
|| iface
->dhcpv6
!= MODE_SERVER
)
107 case NETEV_ADDR6LIST_CHANGE
:
108 handle_addrlist_change(info
);
116 static inline bool valid_prefix_length(const struct dhcp_assignment
*a
, const uint8_t prefix_length
)
118 return (a
->managed_size
|| a
->length
> prefix_length
);
121 static inline bool valid_addr(const struct odhcpd_ipaddr
*addr
, time_t now
)
123 return (addr
->prefix
<= 96 && addr
->valid_lt
> (uint32_t)now
&& addr
->preferred_lt
> (uint32_t)now
);
126 static size_t get_preferred_addr(const struct odhcpd_ipaddr
*addrs
, const size_t addrlen
)
130 for (i
= 0, m
= 0; i
< addrlen
; ++i
) {
131 if (addrs
[i
].preferred_lt
> addrs
[m
].preferred_lt
||
132 (addrs
[i
].preferred_lt
== addrs
[m
].preferred_lt
&&
133 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
140 static int send_reconf(struct dhcp_assignment
*assign
)
143 struct dhcpv6_client_header hdr
;
147 uint16_t hardware_type
;
152 struct dhcpv6_auth_reconfigure auth
;
155 uint8_t clid_data
[128];
156 } __attribute__((packed
)) reconf_msg
= {
157 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
158 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
159 .srvid_len
= htons(10),
160 .duid_type
= htons(3),
161 .hardware_type
= htons(1),
162 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
164 .msg_id
= DHCPV6_MSG_RENEW
,
165 .auth
= {htons(DHCPV6_OPT_AUTH
),
166 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
167 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
168 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
169 .clid_len
= htons(assign
->clid_len
),
172 struct interface
*iface
= assign
->iface
;
174 odhcpd_get_mac(iface
, reconf_msg
.mac
);
175 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
176 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
179 uint8_t secretbytes
[64];
180 memset(secretbytes
, 0, sizeof(secretbytes
));
181 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
183 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
184 secretbytes
[i
] ^= 0x36;
187 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
188 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
189 md5_end(reconf_msg
.auth
.key
, &md5
);
191 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
192 secretbytes
[i
] ^= 0x36;
193 secretbytes
[i
] ^= 0x5c;
197 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
198 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
199 md5_end(reconf_msg
.auth
.key
, &md5
);
201 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
204 static void dhcpv6_ia_free_assignment(struct dhcp_assignment
*a
)
206 if (a
->managed_sock
.fd
.registered
) {
207 ustream_free(&a
->managed_sock
.stream
);
208 close(a
->managed_sock
.fd
.fd
);
211 if ((a
->flags
& OAF_BOUND
) && (a
->flags
& OAF_DHCPV6_PD
))
212 apply_lease(a
, false);
220 void dhcpv6_ia_enum_addrs(struct interface
*iface
, struct dhcp_assignment
*c
,
221 time_t now
, dhcpv6_binding_cb_handler_t func
, void *arg
)
223 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->addr6
;
224 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->addr6_len
;
225 size_t m
= get_preferred_addr(addrs
, addrlen
);
227 for (size_t i
= 0; i
< addrlen
; ++i
) {
228 struct in6_addr addr
;
229 uint32_t preferred_lt
, valid_lt
;
230 int prefix
= c
->managed
? addrs
[i
].prefix
: c
->length
;
232 if (!valid_addr(&addrs
[i
], now
))
235 /* Filter Out Prefixes */
236 if (ADDR_MATCH_PIO_FILTER(&addrs
[i
], iface
)) {
237 char addrbuf
[INET6_ADDRSTRLEN
];
238 info("Address %s filtered out on %s",
239 inet_ntop(AF_INET6
, &addrs
[i
].addr
.in6
, addrbuf
, sizeof(addrbuf
)),
244 addr
= addrs
[i
].addr
.in6
;
245 preferred_lt
= addrs
[i
].preferred_lt
;
246 valid_lt
= addrs
[i
].valid_lt
;
248 if (c
->flags
& OAF_DHCPV6_NA
) {
249 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
))
252 addr
.s6_addr32
[2] = htonl(c
->assigned_host_id
>> 32);
253 addr
.s6_addr32
[3] = htonl(c
->assigned_host_id
& UINT32_MAX
);
255 if (!valid_prefix_length(c
, addrs
[i
].prefix
))
258 addr
.s6_addr32
[1] |= htonl(c
->assigned_subnet_id
);
259 addr
.s6_addr32
[2] = addr
.s6_addr32
[3] = 0;
262 if (preferred_lt
> (uint32_t)c
->preferred_until
)
263 preferred_lt
= c
->preferred_until
;
265 if (preferred_lt
> (uint32_t)c
->valid_until
)
266 preferred_lt
= c
->valid_until
;
268 if (preferred_lt
!= UINT32_MAX
)
271 if (valid_lt
> (uint32_t)c
->valid_until
)
272 valid_lt
= c
->valid_until
;
274 if (valid_lt
!= UINT32_MAX
)
277 func(&addr
, prefix
, preferred_lt
, valid_lt
, arg
);
284 struct dhcp_assignment
*c
;
285 struct interface
*iface
;
291 static void dhcpv6_write_ia_addrhosts(struct in6_addr
*addr
, int prefix
, _unused
uint32_t pref_lt
,
292 _unused
uint32_t valid_lt
, void *arg
)
294 struct write_ctxt
*ctxt
= (struct write_ctxt
*)arg
;
295 char ipbuf
[INET6_ADDRSTRLEN
];
297 if ((ctxt
->c
->flags
& OAF_DHCPV6_NA
) && ctxt
->c
->hostname
&&
298 !(ctxt
->c
->flags
& OAF_BROKEN_HOSTNAME
)) {
299 inet_ntop(AF_INET6
, addr
, ipbuf
, sizeof(ipbuf
) - 1);
300 fputs(ipbuf
, ctxt
->fp
);
303 if (dn_expand(ctxt
->iface
->search
, ctxt
->iface
->search
+ ctxt
->iface
->search_len
,
304 ctxt
->iface
->search
, b
, sizeof(b
)) > 0)
305 fprintf(ctxt
->fp
, "\t%s.%s", ctxt
->c
->hostname
, b
);
307 fprintf(ctxt
->fp
, "\t%s\n", ctxt
->c
->hostname
);
311 static void dhcpv6_write_ia_addr(struct in6_addr
*addr
, int prefix
, _unused
uint32_t pref_lt
,
312 _unused
uint32_t valid_lt
, void *arg
)
314 struct write_ctxt
*ctxt
= (struct write_ctxt
*)arg
;
315 char ipbuf
[INET6_ADDRSTRLEN
];
317 inet_ntop(AF_INET6
, addr
, ipbuf
, sizeof(ipbuf
) - 1);
319 if ((ctxt
->c
->flags
& OAF_DHCPV6_NA
) && ctxt
->c
->hostname
&&
320 !(ctxt
->c
->flags
& OAF_BROKEN_HOSTNAME
)) {
321 fputs(ipbuf
, ctxt
->fp
);
324 if (dn_expand(ctxt
->iface
->search
, ctxt
->iface
->search
+ ctxt
->iface
->search_len
,
325 ctxt
->iface
->search
, b
, sizeof(b
)) > 0)
326 fprintf(ctxt
->fp
, "\t%s.%s", ctxt
->c
->hostname
, b
);
328 fprintf(ctxt
->fp
, "\t%s\n", ctxt
->c
->hostname
);
329 md5_hash(ipbuf
, strlen(ipbuf
), &ctxt
->md5
);
330 md5_hash(ctxt
->c
->hostname
, strlen(ctxt
->c
->hostname
), &ctxt
->md5
);
333 ctxt
->buf_idx
+= snprintf(ctxt
->buf
+ ctxt
->buf_idx
,ctxt
->buf_len
- ctxt
->buf_idx
,
334 "%s/%d ", ipbuf
, prefix
);
337 static void dhcpv6_ia_write_hostsfile(time_t now
)
339 struct write_ctxt ctxt
;
341 unsigned hostsfile_strlen
= strlen(config
.dhcp_hostsfile
) + 1;
342 unsigned tmp_hostsfile_strlen
= hostsfile_strlen
+ 1; /* space for . */
343 char *tmp_hostsfile
= alloca(tmp_hostsfile_strlen
);
346 char *base_hostsfile
;
347 char *pdir_hostsfile
;
348 char *pbase_hostsfile
;
352 dir_hostsfile
= strndup(config
.dhcp_hostsfile
, hostsfile_strlen
);
353 base_hostsfile
= strndup(config
.dhcp_hostsfile
, hostsfile_strlen
);
355 pdir_hostsfile
= dirname(dir_hostsfile
);
356 pbase_hostsfile
= basename(base_hostsfile
);
358 snprintf(tmp_hostsfile
, tmp_hostsfile_strlen
, "%s/.%s", pdir_hostsfile
, pbase_hostsfile
);
361 free(base_hostsfile
);
363 fd
= open(tmp_hostsfile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
367 ret
= lockf(fd
, F_LOCK
, 0);
373 if (ftruncate(fd
, 0) < 0) {}
375 ctxt
.fp
= fdopen(fd
, "w");
381 avl_for_each_element(&interfaces
, ctxt
.iface
, avl
) {
382 if (ctxt
.iface
->dhcpv6
!= MODE_SERVER
&&
383 ctxt
.iface
->dhcpv4
!= MODE_SERVER
)
386 if (ctxt
.iface
->dhcpv6
== MODE_SERVER
) {
387 list_for_each_entry(ctxt
.c
, &ctxt
.iface
->ia_assignments
, head
) {
388 if (!(ctxt
.c
->flags
& OAF_BOUND
) || ctxt
.c
->managed_size
< 0)
391 if (INFINITE_VALID(ctxt
.c
->valid_until
) || ctxt
.c
->valid_until
> now
)
392 dhcpv6_ia_enum_addrs(ctxt
.iface
, ctxt
.c
, now
,
393 dhcpv6_write_ia_addrhosts
, &ctxt
);
397 if (ctxt
.iface
->dhcpv4
== MODE_SERVER
) {
398 struct dhcp_assignment
*c
;
400 list_for_each_entry(c
, &ctxt
.iface
->dhcpv4_assignments
, head
) {
401 if (!(c
->flags
& OAF_BOUND
))
404 char ipbuf
[INET6_ADDRSTRLEN
];
405 struct in_addr addr
= {.s_addr
= c
->addr
};
406 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
408 if (c
->hostname
&& !(c
->flags
& OAF_BROKEN_HOSTNAME
)) {
409 fputs(ipbuf
, ctxt
.fp
);
413 if (dn_expand(ctxt
.iface
->search
,
414 ctxt
.iface
->search
+ ctxt
.iface
->search_len
,
415 ctxt
.iface
->search
, b
, sizeof(b
)) > 0)
416 fprintf(ctxt
.fp
, "\t%s.%s", c
->hostname
, b
);
418 fprintf(ctxt
.fp
, "\t%s\n", c
->hostname
);
426 rename(tmp_hostsfile
, config
.dhcp_hostsfile
);
429 void dhcpv6_ia_write_statefile(void)
431 struct write_ctxt ctxt
;
433 md5_begin(&ctxt
.md5
);
435 if (config
.dhcp_statefile
) {
436 unsigned statefile_strlen
= strlen(config
.dhcp_statefile
) + 1;
437 unsigned tmp_statefile_strlen
= statefile_strlen
+ 1; /* space for . */
438 char *tmp_statefile
= alloca(tmp_statefile_strlen
);
441 char *base_statefile
;
442 char *pdir_statefile
;
443 char *pbase_statefile
;
445 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
449 dir_statefile
= strndup(config
.dhcp_statefile
, statefile_strlen
);
450 base_statefile
= strndup(config
.dhcp_statefile
, statefile_strlen
);
452 pdir_statefile
= dirname(dir_statefile
);
453 pbase_statefile
= basename(base_statefile
);
455 snprintf(tmp_statefile
, tmp_statefile_strlen
, "%s/.%s", pdir_statefile
, pbase_statefile
);
458 free(base_statefile
);
460 fd
= open(tmp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
464 ret
= lockf(fd
, F_LOCK
, 0);
470 if (ftruncate(fd
, 0) < 0) {}
472 ctxt
.fp
= fdopen(fd
, "w");
479 ctxt
.buf_len
= sizeof(leasebuf
);
481 avl_for_each_element(&interfaces
, ctxt
.iface
, avl
) {
482 if (ctxt
.iface
->dhcpv6
!= MODE_SERVER
&&
483 ctxt
.iface
->dhcpv4
!= MODE_SERVER
)
486 if (ctxt
.iface
->dhcpv6
== MODE_SERVER
) {
487 list_for_each_entry(ctxt
.c
, &ctxt
.iface
->ia_assignments
, head
) {
488 if (!(ctxt
.c
->flags
& OAF_BOUND
) || ctxt
.c
->managed_size
< 0)
491 char duidbuf
[DUID_HEXSTRLEN
];
493 odhcpd_hexlify(duidbuf
, ctxt
.c
->clid_data
, ctxt
.c
->clid_len
);
495 /* iface DUID iaid hostname lifetime assigned_host_id length [addrs...] */
496 ctxt
.buf_idx
= snprintf(ctxt
.buf
, ctxt
.buf_len
, "# %s %s %x %s%s %"PRId64
" ",
497 ctxt
.iface
->ifname
, duidbuf
, ntohl(ctxt
.c
->iaid
),
498 (ctxt
.c
->flags
& OAF_BROKEN_HOSTNAME
) ? "broken\\x20" : "",
499 (ctxt
.c
->hostname
? ctxt
.c
->hostname
: "-"),
500 (ctxt
.c
->valid_until
> now
?
501 (int64_t)(ctxt
.c
->valid_until
- now
+ wall_time
) :
502 (INFINITE_VALID(ctxt
.c
->valid_until
) ? -1 : 0)));
504 if (ctxt
.c
->flags
& OAF_DHCPV6_NA
)
505 ctxt
.buf_idx
+= snprintf(ctxt
.buf
+ ctxt
.buf_idx
, ctxt
.buf_len
- ctxt
.buf_idx
,
506 "%" PRIx64
" %u ", ctxt
.c
->assigned_host_id
, (unsigned)ctxt
.c
->length
);
508 ctxt
.buf_idx
+= snprintf(ctxt
.buf
+ ctxt
.buf_idx
, ctxt
.buf_len
- ctxt
.buf_idx
,
509 "%" PRIx32
" %u ", ctxt
.c
->assigned_subnet_id
, (unsigned)ctxt
.c
->length
);
511 if (INFINITE_VALID(ctxt
.c
->valid_until
) || ctxt
.c
->valid_until
> now
)
512 dhcpv6_ia_enum_addrs(ctxt
.iface
, ctxt
.c
, now
,
513 dhcpv6_write_ia_addr
, &ctxt
);
515 ctxt
.buf
[ctxt
.buf_idx
- 1] = '\n';
516 fwrite(ctxt
.buf
, 1, ctxt
.buf_idx
, ctxt
.fp
);
520 if (ctxt
.iface
->dhcpv4
== MODE_SERVER
) {
521 struct dhcp_assignment
*c
;
523 list_for_each_entry(c
, &ctxt
.iface
->dhcpv4_assignments
, head
) {
524 if (!(c
->flags
& OAF_BOUND
))
527 char ipbuf
[INET6_ADDRSTRLEN
];
529 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
531 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
532 ctxt
.buf_idx
= snprintf(ctxt
.buf
, ctxt
.buf_len
, "# %s %s ipv4 %s%s %"PRId64
" %x 32 ",
533 ctxt
.iface
->ifname
, duidbuf
,
534 (c
->flags
& OAF_BROKEN_HOSTNAME
) ? "broken\\x20" : "",
535 (c
->hostname
? c
->hostname
: "-"),
536 (c
->valid_until
> now
?
537 (int64_t)(c
->valid_until
- now
+ wall_time
) :
538 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
541 struct in_addr addr
= {.s_addr
= c
->addr
};
542 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
544 if (c
->hostname
&& !(c
->flags
& OAF_BROKEN_HOSTNAME
)) {
545 fputs(ipbuf
, ctxt
.fp
);
548 if (dn_expand(ctxt
.iface
->search
,
549 ctxt
.iface
->search
+ ctxt
.iface
->search_len
,
550 ctxt
.iface
->search
, b
, sizeof(b
)) > 0)
551 fprintf(ctxt
.fp
, "\t%s.%s", c
->hostname
, b
);
553 fprintf(ctxt
.fp
, "\t%s\n", c
->hostname
);
554 md5_hash(ipbuf
, strlen(ipbuf
), &ctxt
.md5
);
555 md5_hash(c
->hostname
, strlen(c
->hostname
), &ctxt
.md5
);
558 ctxt
.buf_idx
+= snprintf(ctxt
.buf
+ ctxt
.buf_idx
,
559 ctxt
.buf_len
- ctxt
.buf_idx
,
561 ctxt
.buf
[ctxt
.buf_idx
- 1] = '\n';
562 fwrite(ctxt
.buf
, 1, ctxt
.buf_idx
, ctxt
.fp
);
570 md5_end(newmd5
, &ctxt
.md5
);
572 rename(tmp_statefile
, config
.dhcp_statefile
);
574 if (memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
575 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
577 if (config
.dhcp_hostsfile
)
578 dhcpv6_ia_write_hostsfile(now
);
580 if (config
.dhcp_cb
) {
581 char *argv
[2] = {config
.dhcp_cb
, NULL
};
583 execv(argv
[0], argv
);
591 static void __apply_lease(struct dhcp_assignment
*a
,
592 struct odhcpd_ipaddr
*addrs
, ssize_t addr_len
, bool add
)
594 if (a
->flags
& OAF_DHCPV6_NA
)
597 for (ssize_t i
= 0; i
< addr_len
; ++i
) {
598 struct in6_addr prefix
;
600 if (ADDR_MATCH_PIO_FILTER(&addrs
[i
], a
->iface
))
603 prefix
= addrs
[i
].addr
.in6
;
604 prefix
.s6_addr32
[1] |= htonl(a
->assigned_subnet_id
);
605 prefix
.s6_addr32
[2] = prefix
.s6_addr32
[3] = 0;
606 netlink_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
607 a
->iface
->ifindex
, &a
->peer
.sin6_addr
, 1024, add
);
611 static void apply_lease(struct dhcp_assignment
*a
, bool add
)
613 struct interface
*iface
= a
->iface
;
614 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->addr6
;
615 ssize_t addrlen
= (a
->managed
) ? a
->managed_size
: (ssize_t
)iface
->addr6_len
;
617 __apply_lease(a
, addrs
, addrlen
, add
);
620 /* Set border assignment size based on the IPv6 address prefixes */
621 static void set_border_assignment_size(struct interface
*iface
, struct dhcp_assignment
*b
)
623 time_t now
= odhcpd_time();
626 for (size_t i
= 0; i
< iface
->addr6_len
; ++i
) {
627 struct odhcpd_ipaddr
*addr
= &iface
->addr6
[i
];
629 if (ADDR_MATCH_PIO_FILTER(addr
, iface
))
632 if (addr
->preferred_lt
> (uint32_t)now
&&
634 addr
->prefix
> minprefix
)
635 minprefix
= addr
->prefix
;
638 if (minprefix
> 32 && minprefix
<= 64)
639 b
->assigned_subnet_id
= 1U << (64 - minprefix
);
641 b
->assigned_subnet_id
= 0;
644 /* More data was received from TCP connection */
645 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
647 struct ustream_fd
*fd
= container_of(s
, struct ustream_fd
, stream
);
648 struct dhcp_assignment
*c
= container_of(fd
, struct dhcp_assignment
, managed_sock
);
649 time_t now
= odhcpd_time();
650 bool first
= c
->managed_size
< 0;
654 char *data
= ustream_get_read_buf(s
, &pending
);
655 char *end
= memmem(data
, pending
, "\n\n", 2);
664 if (c
->accept_fr_nonce
)
668 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
669 struct odhcpd_ipaddr
*n
;
672 n
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
676 n
= &c
->managed
[c
->managed_size
];
678 x
= strtok_r(line
, ",", &saveptr2
);
679 if (odhcpd_parse_addr6_prefix(x
, &n
->addr
.in6
, &n
->prefix
))
682 x
= strtok_r(NULL
, ",", &saveptr2
);
683 if (sscanf(x
, "%u", &n
->preferred_lt
) < 1)
686 x
= strtok_r(NULL
, ",", &saveptr2
);
687 if (sscanf(x
, "%u", &n
->valid_lt
) < 1)
690 if (n
->preferred_lt
> n
->valid_lt
)
693 if (UINT32_MAX
- now
< n
->preferred_lt
)
694 n
->preferred_lt
= UINT32_MAX
;
696 n
->preferred_lt
+= now
;
698 if (UINT32_MAX
- now
< n
->valid_lt
)
699 n
->valid_lt
= UINT32_MAX
;
708 ustream_consume(s
, end
- data
);
711 if (first
&& c
->managed_size
== 0)
714 c
->valid_until
= now
+ 150;
718 /* TCP transmission has ended, either because of success or timeout or other error */
719 static void managed_handle_pd_done(struct ustream
*s
)
721 struct ustream_fd
*fd
= container_of(s
, struct ustream_fd
, stream
);
722 struct dhcp_assignment
*c
= container_of(fd
, struct dhcp_assignment
, managed_sock
);
724 c
->valid_until
= odhcpd_time() + 15;
728 if (c
->accept_fr_nonce
)
732 static bool assign_pd(struct interface
*iface
, struct dhcp_assignment
*assign
)
734 struct dhcp_assignment
*c
;
736 if (iface
->dhcpv6_pd_manager
[0]) {
737 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
739 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
742 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
744 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
745 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
746 ustream_fd_init(&assign
->managed_sock
, fd
);
747 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
748 iaidbuf
, assign
->iaid
, assign
->length
);
749 ustream_write_pending(&assign
->managed_sock
.stream
);
750 assign
->managed_size
= -1;
752 assign
->valid_until
= odhcpd_time() + 15;
754 list_add(&assign
->head
, &iface
->ia_assignments
);
756 /* Wait initial period of up to 250ms for immediate assignment */
757 if (poll(&pfd
, 1, 250) < 0) {
762 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
764 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
769 } else if (iface
->addr6_len
< 1)
772 /* Try honoring the hint first */
773 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
774 if (assign
->assigned_subnet_id
) {
775 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
776 if (c
->flags
& OAF_DHCPV6_NA
)
779 if (assign
->assigned_subnet_id
>= current
&& assign
->assigned_subnet_id
+ asize
< c
->assigned_subnet_id
) {
780 list_add_tail(&assign
->head
, &c
->head
);
782 if (assign
->flags
& OAF_BOUND
)
783 apply_lease(assign
, true);
788 current
= (c
->assigned_subnet_id
+ (1 << (64 - c
->length
)));
792 /* Fallback to a variable assignment */
794 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
795 if (c
->flags
& OAF_DHCPV6_NA
)
798 current
= (current
+ asize
) & (~asize
);
800 if (current
+ asize
< c
->assigned_subnet_id
) {
801 assign
->assigned_subnet_id
= current
;
802 list_add_tail(&assign
->head
, &c
->head
);
804 if (assign
->flags
& OAF_BOUND
)
805 apply_lease(assign
, true);
810 current
= (c
->assigned_subnet_id
+ (1 << (64 - c
->length
)));
816 /* Check iid against reserved IPv6 interface identifiers.
818 http://www.iana.org/assignments/ipv6-interface-ids */
819 static bool is_reserved_ipv6_iid(uint64_t iid
)
821 if (iid
== 0x0000000000000000)
822 /* Subnet-Router Anycast [RFC4291] */
825 if ((iid
& 0xFFFFFFFFFF000000) == 0x02005EFFFE000000)
826 /* Reserved IPv6 Interface Identifiers corresponding
827 to the IANA Ethernet Block [RFC4291] */
830 if ((iid
& 0xFFFFFFFFFFFFFF80) == 0xFDFFFFFFFFFFFF80)
831 /* Reserved Subnet Anycast Addresses [RFC2526] */
837 static bool assign_na(struct interface
*iface
, struct dhcp_assignment
*a
)
839 struct dhcp_assignment
*c
;
842 /* Preconfigured assignment by static lease */
843 if (a
->assigned_host_id
) {
844 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
845 if (!(c
->flags
& OAF_DHCPV6_NA
) || c
->assigned_host_id
> a
->assigned_host_id
) {
846 list_add_tail(&a
->head
, &c
->head
);
848 } else if (c
->assigned_host_id
== a
->assigned_host_id
)
853 /* Seed RNG with checksum of DUID */
854 for (size_t i
= 0; i
< a
->clid_len
; ++i
)
855 seed
+= a
->clid_data
[i
];
858 /* Try to assign up to 100x */
859 for (size_t i
= 0; i
< 100; ++i
) {
862 if (iface
->dhcpv6_hostid_len
> 32) {
865 if (iface
->dhcpv6_hostid_len
>= 64)
866 mask_high
= UINT32_MAX
;
868 mask_high
= (1 << (iface
->dhcpv6_hostid_len
- 32)) - 1;
871 try = (uint32_t)random();
872 try |= (uint64_t)((uint32_t)random() & mask_high
) << 32;
873 } while (try < 0x100);
877 if (iface
->dhcpv6_hostid_len
== 32)
878 mask_low
= UINT32_MAX
;
880 mask_low
= (1 << iface
->dhcpv6_hostid_len
) - 1;
881 do try = ((uint32_t)random()) & mask_low
; while (try < 0x100);
884 if (is_reserved_ipv6_iid(try))
887 if (config_find_lease_by_hostid(try))
890 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
891 if (!(c
->flags
& OAF_DHCPV6_NA
) || c
->assigned_host_id
> try) {
892 a
->assigned_host_id
= try;
893 list_add_tail(&a
->head
, &c
->head
);
895 } else if (c
->assigned_host_id
== try)
903 static void handle_addrlist_change(struct netevent_handler_info
*info
)
905 struct interface
*iface
= info
->iface
;
906 struct dhcp_assignment
*c
, *d
, *border
= list_last_entry(
907 &iface
->ia_assignments
, struct dhcp_assignment
, head
);
908 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
909 time_t now
= odhcpd_time();
911 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
912 if ((c
->flags
& OAF_DHCPV6_PD
) && !(iface
->ra_flags
& ND_RA_FLAG_MANAGED
)
913 && (c
->flags
& OAF_BOUND
))
914 __apply_lease(c
, info
->addrs_old
.addrs
,
915 info
->addrs_old
.len
, false);
918 set_border_assignment_size(iface
, border
);
920 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
921 if (c
->clid_len
== 0 ||
922 !(c
->flags
& OAF_DHCPV6_PD
) ||
923 (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
927 if (c
->assigned_subnet_id
>= border
->assigned_subnet_id
)
928 list_move(&c
->head
, &reassign
);
929 else if (c
->flags
& OAF_BOUND
)
930 apply_lease(c
, true);
932 if (c
->accept_fr_nonce
&& c
->fr_cnt
== 0) {
933 struct dhcp_assignment
*a
;
937 /* Leave all other assignments of that client alone */
938 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
939 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
940 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
945 while (!list_empty(&reassign
)) {
946 c
= list_first_entry(&reassign
, struct dhcp_assignment
, head
);
947 list_del_init(&c
->head
);
948 if (!assign_pd(iface
, c
))
952 dhcpv6_ia_write_statefile();
955 static void reconf_timeout_cb(struct uloop_timeout
*event
)
957 struct dhcp_assignment
*a
= container_of(event
, struct dhcp_assignment
, fr_timer
);
959 if (a
->fr_cnt
> 0 && a
->fr_cnt
< DHCPV6_REC_MAX_RC
) {
961 uloop_timeout_set(&a
->fr_timer
,
962 DHCPV6_REC_TIMEOUT
<< a
->fr_cnt
);
968 static void start_reconf(struct dhcp_assignment
*a
)
970 uloop_timeout_set(&a
->fr_timer
,
971 DHCPV6_REC_TIMEOUT
<< a
->fr_cnt
);
972 a
->fr_timer
.cb
= reconf_timeout_cb
;
978 static void stop_reconf(struct dhcp_assignment
*a
)
980 uloop_timeout_cancel(&a
->fr_timer
);
982 a
->fr_timer
.cb
= NULL
;
985 static void valid_until_cb(struct uloop_timeout
*event
)
987 struct interface
*iface
;
988 time_t now
= odhcpd_time();
990 avl_for_each_element(&interfaces
, iface
, avl
) {
991 struct dhcp_assignment
*a
, *n
;
993 if (iface
->dhcpv6
!= MODE_SERVER
)
996 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
997 if (a
->clid_len
> 0 && !INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
)
1001 uloop_timeout_set(event
, 1000);
1004 static size_t build_ia(uint8_t *buf
, size_t buflen
, uint16_t status
,
1005 const struct dhcpv6_ia_hdr
*ia
, struct dhcp_assignment
*a
,
1006 struct interface
*iface
, bool request
)
1008 struct dhcpv6_ia_hdr o_ia
= {
1015 size_t ia_len
= sizeof(o_ia
);
1016 time_t now
= odhcpd_time();
1018 if (buflen
< ia_len
)
1022 struct __attribute__((packed
)) {
1027 .type
= htons(DHCPV6_OPT_STATUS
),
1028 .len
= htons(sizeof(o_status
) - 4),
1029 .val
= htons(status
),
1032 memcpy(buf
+ ia_len
, &o_status
, sizeof(o_status
));
1033 ia_len
+= sizeof(o_status
);
1035 o_ia
.len
= htons(ia_len
- 4);
1036 memcpy(buf
, &o_ia
, sizeof(o_ia
));
1045 leasetime
= a
->leasetime
;
1047 leasetime
= iface
->dhcp_leasetime
;
1050 uint32_t floor_preferred_lifetime
, floor_valid_lifetime
; /* For calculating T1 / T2 */
1052 if (iface
->max_preferred_lifetime
&& iface
->max_preferred_lifetime
< leasetime
) {
1053 floor_preferred_lifetime
= iface
->max_preferred_lifetime
;
1055 floor_preferred_lifetime
= leasetime
;
1058 if (iface
->max_valid_lifetime
&& iface
->max_valid_lifetime
< leasetime
) {
1059 floor_valid_lifetime
= iface
->max_valid_lifetime
;
1061 floor_valid_lifetime
= leasetime
;
1064 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->addr6
;
1065 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->addr6_len
;
1066 size_t m
= get_preferred_addr(addrs
, addrlen
);
1068 for (size_t i
= 0; i
< addrlen
; ++i
) {
1069 uint32_t prefix_preferred_lt
, prefix_valid_lt
;
1071 if (!valid_addr(&addrs
[i
], now
))
1074 /* Filter Out Prefixes */
1075 if (ADDR_MATCH_PIO_FILTER(&addrs
[i
], iface
)) {
1076 char addrbuf
[INET6_ADDRSTRLEN
];
1077 info("Address %s filtered out on %s",
1078 inet_ntop(AF_INET6
, &addrs
[i
].addr
.in6
, addrbuf
, sizeof(addrbuf
)),
1083 prefix_preferred_lt
= addrs
[i
].preferred_lt
;
1084 prefix_valid_lt
= addrs
[i
].valid_lt
;
1086 if (prefix_preferred_lt
!= UINT32_MAX
) {
1087 prefix_preferred_lt
-= now
;
1089 if (iface
->max_preferred_lifetime
&& prefix_preferred_lt
> iface
->max_preferred_lifetime
)
1090 prefix_preferred_lt
= iface
->max_preferred_lifetime
;
1093 if (prefix_valid_lt
!= UINT32_MAX
) {
1094 prefix_valid_lt
-= now
;
1096 if (iface
->max_valid_lifetime
&& prefix_valid_lt
> iface
->max_valid_lifetime
)
1097 prefix_valid_lt
= iface
->max_valid_lifetime
;
1100 if (prefix_valid_lt
> leasetime
)
1101 prefix_valid_lt
= leasetime
;
1103 if (prefix_preferred_lt
> prefix_valid_lt
)
1104 prefix_preferred_lt
= prefix_valid_lt
;
1106 if (a
->flags
& OAF_DHCPV6_PD
) {
1107 struct dhcpv6_ia_prefix o_ia_p
= {
1108 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
1109 .len
= htons(sizeof(o_ia_p
) - 4),
1110 .preferred_lt
= htonl(prefix_preferred_lt
),
1111 .valid_lt
= htonl(prefix_valid_lt
),
1112 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
1113 .addr
= addrs
[i
].addr
.in6
,
1116 o_ia_p
.addr
.s6_addr32
[1] |= htonl(a
->assigned_subnet_id
);
1117 o_ia_p
.addr
.s6_addr32
[2] = o_ia_p
.addr
.s6_addr32
[3] = 0;
1119 if (!valid_prefix_length(a
, addrs
[i
].prefix
))
1122 if (buflen
< ia_len
+ sizeof(o_ia_p
))
1125 memcpy(buf
+ ia_len
, &o_ia_p
, sizeof(o_ia_p
));
1126 ia_len
+= sizeof(o_ia_p
);
1129 if (a
->flags
& OAF_DHCPV6_NA
) {
1130 struct dhcpv6_ia_addr o_ia_a
= {
1131 .type
= htons(DHCPV6_OPT_IA_ADDR
),
1132 .len
= htons(sizeof(o_ia_a
) - 4),
1133 .addr
= addrs
[i
].addr
.in6
,
1134 .preferred_lt
= htonl(prefix_preferred_lt
),
1135 .valid_lt
= htonl(prefix_valid_lt
)
1138 o_ia_a
.addr
.s6_addr32
[2] = htonl(a
->assigned_host_id
>> 32);
1139 o_ia_a
.addr
.s6_addr32
[3] = htonl(a
->assigned_host_id
& UINT32_MAX
);
1141 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
))
1144 if (buflen
< ia_len
+ sizeof(o_ia_a
))
1147 memcpy(buf
+ ia_len
, &o_ia_a
, sizeof(o_ia_a
));
1148 ia_len
+= sizeof(o_ia_a
);
1151 /* Calculate T1 / T2 based on non-deprecated addresses */
1152 if (prefix_preferred_lt
> 0) {
1153 if (floor_preferred_lifetime
> prefix_preferred_lt
)
1154 floor_preferred_lifetime
= prefix_preferred_lt
;
1156 if (floor_valid_lifetime
> prefix_valid_lt
)
1157 floor_valid_lifetime
= prefix_valid_lt
;
1161 if (!INFINITE_VALID(a
->valid_until
))
1162 /* UINT32_MAX is RFC defined as infinite lease-time */
1163 a
->valid_until
= (floor_valid_lifetime
== UINT32_MAX
) ? 0 : floor_valid_lifetime
+ now
;
1165 if (!INFINITE_VALID(a
->preferred_until
))
1166 /* UINT32_MAX is RFC defined as infinite lease-time */
1167 a
->preferred_until
= (floor_preferred_lifetime
== UINT32_MAX
) ? 0 : floor_preferred_lifetime
+ now
;
1169 o_ia
.t1
= htonl((floor_preferred_lifetime
== UINT32_MAX
) ? floor_preferred_lifetime
: floor_preferred_lifetime
* 5 / 10);
1170 o_ia
.t2
= htonl((floor_preferred_lifetime
== UINT32_MAX
) ? floor_preferred_lifetime
: floor_preferred_lifetime
* 8 / 10);
1180 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
1181 uint16_t otype
, olen
;
1183 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
1184 struct dhcpv6_ia_prefix
*ia_p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
1185 struct dhcpv6_ia_addr
*ia_a
= (struct dhcpv6_ia_addr
*)&odata
[-4];
1188 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*ia_p
) - 4) &&
1189 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*ia_a
) - 4))
1193 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->addr6
;
1194 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->addr6_len
;
1196 for (size_t i
= 0; i
< addrlen
; ++i
) {
1197 struct in6_addr addr
;
1199 if (!valid_addr(&addrs
[i
], now
))
1202 if (!valid_prefix_length(a
, addrs
[i
].prefix
))
1205 if (ADDR_MATCH_PIO_FILTER(&addrs
[i
], iface
))
1208 addr
= addrs
[i
].addr
.in6
;
1209 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
1210 addr
.s6_addr32
[1] |= htonl(a
->assigned_subnet_id
);
1211 addr
.s6_addr32
[2] = addr
.s6_addr32
[3] = 0;
1213 if (!memcmp(&ia_p
->addr
, &addr
, sizeof(addr
)) &&
1214 ia_p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
1217 addr
.s6_addr32
[2] = htonl(a
->assigned_host_id
>> 32);
1218 addr
.s6_addr32
[3] = htonl(a
->assigned_host_id
& UINT32_MAX
);
1220 if (!memcmp(&ia_a
->addr
, &addr
, sizeof(addr
)))
1227 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
1228 struct dhcpv6_ia_prefix o_ia_p
= {
1229 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
1230 .len
= htons(sizeof(o_ia_p
) - 4),
1233 .prefix
= ia_p
->prefix
,
1237 if (buflen
< ia_len
+ sizeof(o_ia_p
))
1240 memcpy(buf
+ ia_len
, &o_ia_p
, sizeof(o_ia_p
));
1241 ia_len
+= sizeof(o_ia_p
);
1243 struct dhcpv6_ia_addr o_ia_a
= {
1244 .type
= htons(DHCPV6_OPT_IA_ADDR
),
1245 .len
= htons(sizeof(o_ia_a
) - 4),
1251 if (buflen
< ia_len
+ sizeof(o_ia_a
))
1254 memcpy(buf
+ ia_len
, &o_ia_a
, sizeof(o_ia_a
));
1255 ia_len
+= sizeof(o_ia_a
);
1261 o_ia
.len
= htons(ia_len
- 4);
1262 memcpy(buf
, &o_ia
, sizeof(o_ia
));
1272 static void dhcpv6_log_ia_addr(struct in6_addr
*addr
, int prefix
, _unused
uint32_t pref_lt
,
1273 _unused
uint32_t valid_lt
, void *arg
)
1275 struct log_ctxt
*ctxt
= (struct log_ctxt
*)arg
;
1276 char addrbuf
[INET6_ADDRSTRLEN
];
1278 inet_ntop(AF_INET6
, addr
, addrbuf
, sizeof(addrbuf
));
1279 ctxt
->buf_idx
+= snprintf(ctxt
->buf
+ ctxt
->buf_idx
, ctxt
->buf_len
- ctxt
->buf_idx
,
1280 "%s/%d ", addrbuf
, prefix
);
1283 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
1284 const char *duidbuf
, bool is_pd
, struct dhcp_assignment
*a
, int code
)
1286 const char *type
= "UNKNOWN";
1287 const char *status
= "UNKNOWN";
1290 case DHCPV6_MSG_SOLICIT
:
1293 case DHCPV6_MSG_REQUEST
:
1296 case DHCPV6_MSG_CONFIRM
:
1299 case DHCPV6_MSG_RENEW
:
1302 case DHCPV6_MSG_REBIND
:
1305 case DHCPV6_MSG_RELEASE
:
1308 case DHCPV6_MSG_DECLINE
:
1314 case DHCPV6_STATUS_OK
:
1317 case DHCPV6_STATUS_NOADDRSAVAIL
:
1318 status
= "no addresses available";
1320 case DHCPV6_STATUS_NOBINDING
:
1321 status
= "no binding";
1323 case DHCPV6_STATUS_NOTONLINK
:
1324 status
= "not on-link";
1326 case DHCPV6_STATUS_NOPREFIXAVAIL
:
1327 status
= "no prefix available";
1331 char leasebuf
[256] = "";
1334 struct log_ctxt ctxt
= {.buf
= leasebuf
,
1335 .buf_len
= sizeof(leasebuf
),
1338 dhcpv6_ia_enum_addrs(iface
, a
, now
, dhcpv6_log_ia_addr
, &ctxt
);
1341 info("DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
1342 duidbuf
, iface
->name
, status
, leasebuf
);
1345 static bool dhcpv6_ia_on_link(const struct dhcpv6_ia_hdr
*ia
, struct dhcp_assignment
*a
,
1346 struct interface
*iface
)
1348 struct odhcpd_ipaddr
*addrs
= (a
&& a
->managed
) ? a
->managed
: iface
->addr6
;
1349 size_t addrlen
= (a
&& a
->managed
) ? (size_t)a
->managed_size
: iface
->addr6_len
;
1350 time_t now
= odhcpd_time();
1351 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
1352 uint16_t otype
, olen
;
1355 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
1356 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
1357 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
1359 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
1360 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
1364 for (size_t i
= 0; i
< addrlen
; ++i
) {
1365 if (!valid_addr(&addrs
[i
], now
))
1368 if (ADDR_MATCH_PIO_FILTER(&addrs
[i
], iface
))
1371 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
1372 if (p
->prefix
< addrs
[i
].prefix
||
1373 odhcpd_bmemcmp(&p
->addr
, &addrs
[i
].addr
.in6
, addrs
[i
].prefix
))
1376 } else if (odhcpd_bmemcmp(&n
->addr
, &addrs
[i
].addr
.in6
, addrs
[i
].prefix
))
1389 ssize_t
dhcpv6_ia_handle_IAs(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
1390 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
1392 struct dhcp_assignment
*first
= NULL
;
1393 const struct dhcpv6_client_header
*hdr
= data
;
1394 time_t now
= odhcpd_time();
1395 uint16_t otype
, olen
, clid_len
= 0;
1396 uint8_t *start
= (uint8_t *)&hdr
[1], *odata
;
1397 uint8_t *clid_data
= NULL
, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1398 size_t hostname_len
= 0, response_len
= 0;
1399 bool notonlink
= false, rapid_commit
= false, accept_reconf
= false;
1400 char duidbuf
[DUID_HEXSTRLEN
], hostname
[256];
1402 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1403 if (otype
== DHCPV6_OPT_CLIENTID
) {
1407 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
1408 memcpy(mac
, &odata
[8], sizeof(mac
));
1409 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
1410 memcpy(mac
, &odata
[4], sizeof(mac
));
1412 if (olen
<= DUID_MAX_LEN
)
1413 odhcpd_hexlify(duidbuf
, odata
, olen
);
1414 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
1415 uint8_t fqdn_buf
[256];
1416 memcpy(fqdn_buf
, odata
, olen
);
1417 fqdn_buf
[olen
++] = 0;
1419 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
1420 hostname_len
= strcspn(hostname
, ".");
1421 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
)
1422 accept_reconf
= true;
1423 else if (otype
== DHCPV6_OPT_RAPID_COMMIT
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
)
1424 rapid_commit
= true;
1427 if (!clid_data
|| !clid_len
|| clid_len
> DUID_MAX_LEN
)
1430 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1431 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
1432 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
1433 bool ia_addr_present
= false;
1434 if (!is_pd
&& !is_na
)
1437 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1438 size_t ia_response_len
= 0;
1439 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1440 uint32_t reqhint
= 0;
1443 l
= config_find_lease_by_duid_and_iaid(clid_data
, clid_len
, ntohl(ia
->iaid
));
1445 l
= config_find_lease_by_mac(mac
);
1447 /* Parse request hint for IA-PD */
1450 uint16_t stype
, slen
;
1451 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1452 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1455 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1458 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1459 if (reqlen
> 32 && reqlen
<= 64)
1460 reqhint
&= (1U << (64 - reqlen
)) - 1;
1468 * A requesting router can include a desired prefix length for its
1469 * delegation. The delegating router (us) is not required to honor
1470 * the hint (RFC3633, section 11.2, we MAY choose to use the
1471 * information in the option; RFC8168, section 3.2 has several SHOULDs
1472 * about desired choices for selecting a prefix to delegate).
1474 * We support a policy setting to conserve prefix space, which purposely
1475 * assigns prefixes that might not match the requesting router's hint.
1477 * If the minimum prefix length is set in this interface's
1478 * configuration, we use it as a floor for the requested (hinted)
1479 * prefix length. This allows us to conserve prefix space so that
1480 * any single router can't grab too much of it. Consider if we have
1481 * an interface with a /56 prefix. A requesting router could ask for
1482 * a /58 and take 1/4 of our total address space. But if we set a
1483 * minimum of /60, we can limit each requesting router to get only
1484 * 1/16 of our total address space.
1486 if (iface
->dhcpv6_pd_min_len
&& reqlen
< iface
->dhcpv6_pd_min_len
) {
1487 info("clamping requested PD from %d to %d", reqlen
,
1488 iface
->dhcpv6_pd_min_len
);
1489 reqlen
= iface
->dhcpv6_pd_min_len
;
1493 uint16_t stype
, slen
;
1494 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1495 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1498 ia_addr_present
= true;
1502 /* Find an existing assignment */
1503 struct dhcp_assignment
*c
, *a
= NULL
;
1504 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1505 /* If we're looking for a PD, is this a PD? */
1506 if (is_pd
&& !(c
->flags
& OAF_DHCPV6_PD
))
1509 /* If we're looking for a NA, is this a NA? */
1510 if (is_na
&& !(c
->flags
& OAF_DHCPV6_NA
))
1513 /* Is this assignment still valid? */
1514 if (!INFINITE_VALID(c
->valid_until
) && now
>= c
->valid_until
)
1517 /* Does the DUID match? */
1518 if (c
->clid_len
!= clid_len
|| memcmp(c
->clid_data
, clid_data
, clid_len
))
1521 /* Does the IAID match? */
1522 if (c
->iaid
!= ia
->iaid
) {
1526 /* Does the existing assignment stem from the same static lease cfg? */
1531 * If there's a DUID configured for this static lease, but without
1532 * an IAID, we will proceed under the assumption that a request
1533 * with the right DUID but with *any* IAID should be able to take
1534 * over the assignment. E.g. when switching from WiFi to ethernet
1535 * on the same client. This is similar to how multiple MAC adresses
1536 * are handled for DHCPv4.
1538 for (size_t i
= 0; i
< l
->duid_count
; i
++) {
1539 if (l
->duids
[i
].iaid_set
&& l
->duids
[i
].iaid
!= htonl(ia
->iaid
))
1542 if (l
->duids
[i
].len
!= clid_len
)
1545 if (memcmp(l
->duids
[i
].id
, clid_data
, clid_len
))
1549 * Reconf doesn't specify the IAID, so we have to assume the client
1550 * already knows or doesn't care about the old assignment.
1559 /* We have a match */
1563 if (a
->flags
& OAF_BOUND
)
1564 apply_lease(a
, false);
1570 if (l
&& a
&& a
->lease
!= l
) {
1576 /* Generic message handling */
1577 uint16_t status
= DHCPV6_STATUS_OK
;
1578 if (a
&& a
->managed_size
< 0)
1581 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
||
1582 hdr
->msg_type
== DHCPV6_MSG_REQUEST
||
1583 (hdr
->msg_type
== DHCPV6_MSG_REBIND
&& !a
)) {
1584 bool assigned
= !!a
;
1587 if ((!iface
->no_dynamic_dhcp
|| (l
&& is_na
)) &&
1588 (iface
->dhcpv6_pd
|| iface
->dhcpv6_na
)) {
1589 /* Create new binding */
1590 a
= alloc_assignment(clid_len
);
1593 a
->clid_len
= clid_len
;
1594 memcpy(a
->clid_data
, clid_data
, clid_len
);
1599 a
->assigned_host_id
= l
? l
->hostid
: 0;
1601 a
->assigned_subnet_id
= reqhint
;
1602 a
->valid_until
= now
;
1603 a
->preferred_until
= now
;
1604 a
->dhcp_free_cb
= dhcpv6_ia_free_assignment
;
1606 a
->flags
= (is_pd
? OAF_DHCPV6_PD
: OAF_DHCPV6_NA
);
1609 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1611 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1613 if (is_pd
&& iface
->dhcpv6_pd
)
1614 while (!(assigned
= assign_pd(iface
, a
)) &&
1615 !a
->managed_size
&& ++a
->length
<= 64);
1616 else if (is_na
&& iface
->dhcpv6_na
)
1617 assigned
= assign_na(iface
, a
);
1619 if (l
&& assigned
) {
1620 a
->flags
|= OAF_STATIC
;
1623 a
->hostname
= strdup(l
->hostname
);
1626 a
->leasetime
= l
->leasetime
;
1628 list_add(&a
->lease_list
, &l
->assignments
);
1632 if (a
->managed_size
&& !assigned
)
1638 if (!assigned
|| iface
->addr6_len
== 0)
1639 /* Set error status */
1640 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1641 else if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
&& !dhcpv6_ia_on_link(ia
, a
, iface
)) {
1642 /* Send NOTONLINK status for the IA */
1643 status
= DHCPV6_STATUS_NOTONLINK
;
1645 } else if (accept_reconf
&& assigned
&& !first
&&
1646 hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1647 size_t handshake_len
= 4;
1649 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1653 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1654 struct dhcpv6_auth_reconfigure auth
= {
1655 htons(DHCPV6_OPT_AUTH
),
1656 htons(sizeof(auth
) - 4),
1658 {htonl(time(NULL
)), htonl(++serial
)},
1662 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1663 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1664 handshake_len
+= sizeof(auth
);
1668 buf
+= handshake_len
;
1669 buflen
-= handshake_len
;
1670 response_len
+= handshake_len
;
1675 ia_response_len
= build_ia(buf
, buflen
, status
, ia
, a
, iface
,
1676 hdr
->msg_type
== DHCPV6_MSG_REBIND
? false : true);
1678 /* Was only a solicitation: mark binding for removal */
1679 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
&& !rapid_commit
) {
1680 a
->flags
&= ~OAF_BOUND
;
1681 a
->flags
|= OAF_TENTATIVE
;
1683 /* Keep tentative assignment around for 60 seconds */
1684 a
->valid_until
= now
+ 60;
1686 } else if (assigned
&&
1687 ((hdr
->msg_type
== DHCPV6_MSG_SOLICIT
&& rapid_commit
) ||
1688 hdr
->msg_type
== DHCPV6_MSG_REQUEST
||
1689 hdr
->msg_type
== DHCPV6_MSG_REBIND
)) {
1690 if ((!(a
->flags
& OAF_STATIC
) || !a
->hostname
) && hostname_len
> 0) {
1691 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1693 memcpy(a
->hostname
, hostname
, hostname_len
);
1694 a
->hostname
[hostname_len
] = 0;
1696 if (odhcpd_valid_hostname(a
->hostname
))
1697 a
->flags
&= ~OAF_BROKEN_HOSTNAME
;
1699 a
->flags
|= OAF_BROKEN_HOSTNAME
;
1702 a
->accept_fr_nonce
= accept_reconf
;
1703 a
->flags
&= ~OAF_TENTATIVE
;
1704 a
->flags
|= OAF_BOUND
;
1705 apply_lease(a
, true);
1706 } else if (!assigned
&& a
&& a
->managed_size
== 0) {
1707 /* Cleanup failed assignment */
1711 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1712 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1713 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1714 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1715 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1716 status
= DHCPV6_STATUS_NOBINDING
;
1717 ia_response_len
= build_ia(buf
, buflen
, status
, ia
, a
, iface
, false);
1718 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1719 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1720 ia_response_len
= build_ia(buf
, buflen
, status
, ia
, a
, iface
, false);
1722 a
->flags
|= OAF_BOUND
;
1723 apply_lease(a
, true);
1725 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1726 a
->valid_until
= now
- 1;
1727 } else if ((a
->flags
& OAF_DHCPV6_NA
) && hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1728 a
->flags
&= ~OAF_BOUND
;
1730 if (!(a
->flags
& OAF_STATIC
) || a
->lease
->hostid
!= a
->assigned_host_id
) {
1731 memset(a
->clid_data
, 0, a
->clid_len
);
1732 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1734 a
->valid_until
= now
- 1;
1736 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
) {
1737 if (ia_addr_present
&& !dhcpv6_ia_on_link(ia
, a
, iface
)) {
1742 if (!ia_addr_present
|| !a
|| !(a
->flags
& OAF_BOUND
)) {
1748 buf
+= ia_response_len
;
1749 buflen
-= ia_response_len
;
1750 response_len
+= ia_response_len
;
1751 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1754 switch (hdr
->msg_type
) {
1755 case DHCPV6_MSG_RELEASE
:
1756 case DHCPV6_MSG_DECLINE
:
1757 case DHCPV6_MSG_CONFIRM
:
1758 if (response_len
+ 6 < buflen
) {
1760 buf
[1] = DHCPV6_OPT_STATUS
;
1764 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1773 dhcpv6_ia_write_statefile();
1776 return response_len
;