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.
32 #include <arpa/inet.h>
33 #include <sys/timerfd.h>
35 #include <libubox/md5.h>
36 #include <libubox/usock.h>
38 #define ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) \
39 ((iface)->dhcpv6_assignall || (i) == (m) || \
40 (addrs)[(i)].prefix > 64)
42 static void dhcpv6_netevent_cb(unsigned long event
, struct netevent_handler_info
*info
);
43 static void free_dhcpv6_assignment(struct dhcp_assignment
*c
);
44 static void set_border_assignment_size(struct interface
*iface
, struct dhcp_assignment
*b
);
45 static void handle_addrlist_change(struct netevent_handler_info
*info
);
46 static void start_reconf(struct dhcp_assignment
*a
);
47 static void stop_reconf(struct dhcp_assignment
*a
);
48 static void valid_until_cb(struct uloop_timeout
*event
);
50 static struct netevent_handler dhcpv6_netevent_handler
= { .cb
= dhcpv6_netevent_cb
, };
51 static struct uloop_timeout valid_until_timeout
= {.cb
= valid_until_cb
};
52 static uint32_t serial
= 0;
53 static uint8_t statemd5
[16];
55 int dhcpv6_ia_init(void)
57 uloop_timeout_set(&valid_until_timeout
, 1000);
59 netlink_add_netevent_handler(&dhcpv6_netevent_handler
);
64 int dhcpv6_ia_setup_interface(struct interface
*iface
, bool enable
)
66 if (!enable
&& iface
->ia_assignments
.next
) {
67 struct dhcp_assignment
*c
;
69 while (!list_empty(&iface
->ia_assignments
)) {
70 c
= list_first_entry(&iface
->ia_assignments
, struct dhcp_assignment
, head
);
71 free_dhcpv6_assignment(c
);
75 if (enable
&& iface
->dhcpv6
== MODE_SERVER
) {
76 struct dhcp_assignment
*border
;
79 if (!iface
->ia_assignments
.next
)
80 INIT_LIST_HEAD(&iface
->ia_assignments
);
82 if (list_empty(&iface
->ia_assignments
)) {
83 border
= calloc(1, sizeof(*border
));
86 syslog(LOG_ERR
, "Calloc failed for border on %s", iface
->name
);
91 list_add(&border
->head
, &iface
->ia_assignments
);
93 border
= list_last_entry(&iface
->ia_assignments
, struct dhcp_assignment
, head
);
95 set_border_assignment_size(iface
, border
);
97 /* Parse static entries */
98 list_for_each_entry(lease
, &leases
, head
) {
100 size_t duid_len
= lease
->duid_len
? lease
->duid_len
: 14;
101 struct dhcp_assignment
*a
= calloc(1, sizeof(*a
) + duid_len
);
103 syslog(LOG_ERR
, "Calloc failed for static lease assignment on %s",
108 a
->leasetime
= lease
->dhcpv4_leasetime
;
110 a
->clid_len
= duid_len
;
113 a
->assigned
= lease
->hostid
;
115 uint32_t i4a
= ntohl(lease
->ipaddr
.s_addr
) & 0xff;
116 a
->assigned
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
119 odhcpd_urandom(a
->key
, sizeof(a
->key
));
120 memcpy(a
->clid_data
, lease
->duid
, lease
->duid_len
);
121 memcpy(a
->mac
, lease
->mac
.ether_addr_octet
, sizeof(a
->mac
));
122 /* Static assignment */
123 a
->flags
|= OAF_STATIC
;
127 /* Assign to all interfaces */
128 struct dhcp_assignment
*c
;
129 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
130 if (c
->length
!= 128 || c
->assigned
> a
->assigned
) {
131 list_add_tail(&a
->head
, &c
->head
);
133 } else if (c
->assigned
== a
->assigned
)
134 /* Already an assignment with that number */
140 if (lease
->hostname
[0]) {
142 a
->hostname
= strdup(lease
->hostname
);
145 free_dhcpv6_assignment(a
);
152 static void dhcpv6_netevent_cb(unsigned long event
, struct netevent_handler_info
*info
)
154 struct interface
*iface
= info
->iface
;
156 if (!iface
|| iface
->dhcpv6
!= MODE_SERVER
)
160 case NETEV_ADDR6LIST_CHANGE
:
161 handle_addrlist_change(info
);
169 static void free_dhcpv6_assignment(struct dhcp_assignment
*c
)
171 if (c
->managed_sock
.fd
.registered
) {
172 ustream_free(&c
->managed_sock
.stream
);
173 close(c
->managed_sock
.fd
.fd
);
187 static inline bool valid_prefix_length(const struct dhcp_assignment
*a
, const uint8_t prefix_length
)
189 return (a
->managed_size
|| a
->length
> prefix_length
);
192 static inline bool valid_addr(const struct odhcpd_ipaddr
*addr
, time_t now
)
194 return (addr
->prefix
<= 96 && addr
->preferred
> (uint32_t)now
);
197 static size_t get_preferred_addr(const struct odhcpd_ipaddr
*addrs
, const size_t addrlen
)
201 for (i
= 0, m
= 0; i
< addrlen
; ++i
) {
202 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
203 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
204 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
211 static int send_reconf(struct dhcp_assignment
*assign
)
214 struct dhcpv6_client_header hdr
;
218 uint16_t hardware_type
;
223 struct dhcpv6_auth_reconfigure auth
;
226 uint8_t clid_data
[128];
227 } __attribute__((packed
)) reconf_msg
= {
228 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
229 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
230 .srvid_len
= htons(10),
231 .duid_type
= htons(3),
232 .hardware_type
= htons(1),
233 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
235 .msg_id
= DHCPV6_MSG_RENEW
,
236 .auth
= {htons(DHCPV6_OPT_AUTH
),
237 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
238 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
239 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
240 .clid_len
= htons(assign
->clid_len
),
243 struct interface
*iface
= assign
->iface
;
245 odhcpd_get_mac(iface
, reconf_msg
.mac
);
246 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
247 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
250 uint8_t secretbytes
[64];
251 memset(secretbytes
, 0, sizeof(secretbytes
));
252 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
254 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
255 secretbytes
[i
] ^= 0x36;
258 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
259 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
260 md5_end(reconf_msg
.auth
.key
, &md5
);
262 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
263 secretbytes
[i
] ^= 0x36;
264 secretbytes
[i
] ^= 0x5c;
268 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
269 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
270 md5_end(reconf_msg
.auth
.key
, &md5
);
272 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
275 void dhcpv6_ia_enum_addrs(struct interface
*iface
, struct dhcp_assignment
*c
,
276 time_t now
, dhcpv6_binding_cb_handler_t func
, void *arg
)
278 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->addr6
;
279 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->addr6_len
;
280 size_t m
= get_preferred_addr(addrs
, addrlen
);
282 for (size_t i
= 0; i
< addrlen
; ++i
) {
283 struct in6_addr addr
;
284 uint32_t pref
, valid
;
285 int prefix
= c
->managed
? addrs
[i
].prefix
: c
->length
;
287 if (!valid_addr(&addrs
[i
], now
))
290 addr
= addrs
[i
].addr
.in6
;
291 pref
= addrs
[i
].preferred
;
292 valid
= addrs
[i
].valid
;
294 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
))
297 addr
.s6_addr32
[3] = htonl(c
->assigned
);
299 if (!valid_prefix_length(c
, addrs
[i
].prefix
))
302 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
303 addr
.s6_addr32
[2] = addr
.s6_addr32
[3] = 0;
306 if (pref
!= UINT32_MAX
)
309 if (valid
!= UINT32_MAX
)
312 func(&addr
, prefix
, pref
, valid
, arg
);
319 struct dhcp_assignment
*c
;
320 struct interface
*iface
;
326 void dhcpv6_write_ia_addr(struct in6_addr
*addr
, int prefix
, _unused
uint32_t pref
,
327 _unused
uint32_t valid
, void *arg
)
329 struct write_ctxt
*ctxt
= (struct write_ctxt
*)arg
;
330 char ipbuf
[INET6_ADDRSTRLEN
];
332 inet_ntop(AF_INET6
, addr
, ipbuf
, sizeof(ipbuf
) - 1);
334 if (ctxt
->c
->length
== 128 && ctxt
->c
->hostname
&&
335 !(ctxt
->c
->flags
& OAF_BROKEN_HOSTNAME
)) {
336 fputs(ipbuf
, ctxt
->fp
);
339 if (dn_expand(ctxt
->iface
->search
, ctxt
->iface
->search
+ ctxt
->iface
->search_len
,
340 ctxt
->iface
->search
, b
, sizeof(b
)) > 0)
341 fprintf(ctxt
->fp
, "\t%s.%s", ctxt
->c
->hostname
, b
);
343 fprintf(ctxt
->fp
, "\t%s\n", ctxt
->c
->hostname
);
344 md5_hash(ipbuf
, strlen(ipbuf
), &ctxt
->md5
);
345 md5_hash(ctxt
->c
->hostname
, strlen(ctxt
->c
->hostname
), &ctxt
->md5
);
348 ctxt
->buf_idx
+= snprintf(ctxt
->buf
+ ctxt
->buf_idx
,ctxt
->buf_len
- ctxt
->buf_idx
,
349 "%s/%d ", ipbuf
, prefix
);
352 void dhcpv6_ia_write_statefile(void)
354 struct write_ctxt ctxt
;
356 md5_begin(&ctxt
.md5
);
358 if (config
.dhcp_statefile
) {
359 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
360 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
366 ret
= lockf(fd
, F_LOCK
, 0);
371 if (ftruncate(fd
, 0) < 0) {}
373 ctxt
.fp
= fdopen(fd
, "w");
380 ctxt
.buf_len
= sizeof(leasebuf
);
382 avl_for_each_element(&interfaces
, ctxt
.iface
, avl
) {
383 if (ctxt
.iface
->dhcpv6
!= MODE_SERVER
&&
384 ctxt
.iface
->dhcpv4
!= MODE_SERVER
)
387 if (ctxt
.iface
->dhcpv6
== MODE_SERVER
&&
388 ctxt
.iface
->ia_assignments
.next
) {
389 list_for_each_entry(ctxt
.c
, &ctxt
.iface
->ia_assignments
, head
) {
390 if (!(ctxt
.c
->flags
& OAF_BOUND
) || ctxt
.c
->managed_size
< 0)
395 odhcpd_hexlify(duidbuf
, ctxt
.c
->clid_data
, ctxt
.c
->clid_len
);
397 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
398 ctxt
.buf_idx
= snprintf(ctxt
.buf
, ctxt
.buf_len
, "# %s %s %x %s%s %ld %x %u ",
399 ctxt
.iface
->ifname
, duidbuf
, ntohl(ctxt
.c
->iaid
),
400 (ctxt
.c
->flags
& OAF_BROKEN_HOSTNAME
) ? "broken\\x20" : "",
401 (ctxt
.c
->hostname
? ctxt
.c
->hostname
: "-"),
402 (ctxt
.c
->valid_until
> now
?
403 (ctxt
.c
->valid_until
- now
+ wall_time
) :
404 (INFINITE_VALID(ctxt
.c
->valid_until
) ? -1 : 0)),
405 ctxt
.c
->assigned
, (unsigned)ctxt
.c
->length
);
407 if (INFINITE_VALID(ctxt
.c
->valid_until
) || ctxt
.c
->valid_until
> now
)
408 dhcpv6_ia_enum_addrs(ctxt
.iface
, ctxt
.c
, now
,
409 dhcpv6_write_ia_addr
, &ctxt
);
411 ctxt
.buf
[ctxt
.buf_idx
- 1] = '\n';
412 fwrite(ctxt
.buf
, 1, ctxt
.buf_idx
, ctxt
.fp
);
416 if (ctxt
.iface
->dhcpv4
== MODE_SERVER
&&
417 ctxt
.iface
->dhcpv4_assignments
.next
) {
418 struct dhcp_assignment
*c
;
419 list_for_each_entry(c
, &ctxt
.iface
->dhcpv4_assignments
, head
) {
420 if (!(c
->flags
& OAF_BOUND
))
423 char ipbuf
[INET6_ADDRSTRLEN
];
425 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
427 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
428 ctxt
.buf_idx
= snprintf(ctxt
.buf
, ctxt
.buf_len
, "# %s %s ipv4 %s%s %ld %x 32 ",
429 ctxt
.iface
->ifname
, duidbuf
,
430 (c
->flags
& OAF_BROKEN_HOSTNAME
) ? "broken\\x20" : "",
431 (c
->hostname
? c
->hostname
: "-"),
432 (c
->valid_until
> now
?
433 (c
->valid_until
- now
+ wall_time
) :
434 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
437 struct in_addr addr
= {.s_addr
= c
->addr
};
438 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
440 if (c
->hostname
&& !(c
->flags
& OAF_BROKEN_HOSTNAME
)) {
441 fputs(ipbuf
, ctxt
.fp
);
444 if (dn_expand(ctxt
.iface
->search
,
445 ctxt
.iface
->search
+ ctxt
.iface
->search_len
,
446 ctxt
.iface
->search
, b
, sizeof(b
)) > 0)
447 fprintf(ctxt
.fp
, "\t%s.%s", c
->hostname
, b
);
449 fprintf(ctxt
.fp
, "\t%s\n", c
->hostname
);
450 md5_hash(ipbuf
, strlen(ipbuf
), &ctxt
.md5
);
451 md5_hash(c
->hostname
, strlen(c
->hostname
), &ctxt
.md5
);
454 ctxt
.buf_idx
+= snprintf(ctxt
.buf
+ ctxt
.buf_idx
,
455 ctxt
.buf_len
- ctxt
.buf_idx
,
457 ctxt
.buf
[ctxt
.buf_idx
- 1] = '\n';
458 fwrite(ctxt
.buf
, 1, ctxt
.buf_idx
, ctxt
.fp
);
467 md5_end(newmd5
, &ctxt
.md5
);
469 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
470 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
471 char *argv
[2] = {config
.dhcp_cb
, NULL
};
473 execv(argv
[0], argv
);
479 static void __apply_lease(struct interface
*iface
, struct dhcp_assignment
*a
,
480 struct odhcpd_ipaddr
*addrs
, ssize_t addr_len
, bool add
)
485 for (ssize_t i
= 0; i
< addr_len
; ++i
) {
486 struct in6_addr prefix
= addrs
[i
].addr
.in6
;
487 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
488 prefix
.s6_addr32
[2] = prefix
.s6_addr32
[3] = 0;
489 netlink_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
490 iface
->ifindex
, &a
->peer
.sin6_addr
, 1024, add
);
494 static void apply_lease(struct interface
*iface
, struct dhcp_assignment
*a
, bool add
)
496 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->addr6
;
497 ssize_t addrlen
= (a
->managed
) ? a
->managed_size
: (ssize_t
)iface
->addr6_len
;
499 __apply_lease(iface
, a
, addrs
, addrlen
, add
);
502 /* Set border assignment size based on the IPv6 address prefixes */
503 static void set_border_assignment_size(struct interface
*iface
, struct dhcp_assignment
*b
)
505 time_t now
= odhcpd_time();
508 for (size_t i
= 0; i
< iface
->addr6_len
; ++i
) {
509 if (iface
->addr6
[i
].preferred
> (uint32_t)now
&&
510 iface
->addr6
[i
].prefix
< 64 &&
511 iface
->addr6
[i
].prefix
> minprefix
)
512 minprefix
= iface
->addr6
[i
].prefix
;
515 if (minprefix
> 32 && minprefix
<= 64)
516 b
->assigned
= 1U << (64 - minprefix
);
521 /* More data was received from TCP connection */
522 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
524 struct ustream_fd
*fd
= container_of(s
, struct ustream_fd
, stream
);
525 struct dhcp_assignment
*c
= container_of(fd
, struct dhcp_assignment
, managed_sock
);
526 time_t now
= odhcpd_time();
527 bool first
= c
->managed_size
< 0;
531 char *data
= ustream_get_read_buf(s
, &pending
);
532 char *end
= memmem(data
, pending
, "\n\n", 2);
541 if (c
->accept_reconf
)
545 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
546 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
547 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
549 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
550 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
553 x
= strtok_r(NULL
, ",", &saveptr2
);
554 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
557 x
= strtok_r(NULL
, ",", &saveptr2
);
558 if (sscanf(x
, "%u", &n
->preferred
) < 1)
561 x
= strtok_r(NULL
, ",", &saveptr2
);
562 if (sscanf(x
, "%u", &n
->valid
) < 1)
565 if (n
->preferred
> n
->valid
)
568 if (UINT32_MAX
- now
< n
->preferred
)
569 n
->preferred
= UINT32_MAX
;
573 if (UINT32_MAX
- now
< n
->valid
)
574 n
->valid
= UINT32_MAX
;
583 ustream_consume(s
, end
- data
);
586 if (first
&& c
->managed_size
== 0)
587 free_dhcpv6_assignment(c
);
588 else if (first
&& !(c
->flags
& OAF_STATIC
))
589 c
->valid_until
= now
+ 150;
593 /* TCP transmission has ended, either because of success or timeout or other error */
594 static void managed_handle_pd_done(struct ustream
*s
)
596 struct ustream_fd
*fd
= container_of(s
, struct ustream_fd
, stream
);
597 struct dhcp_assignment
*c
= container_of(fd
, struct dhcp_assignment
, managed_sock
);
599 if (!(c
->flags
& OAF_STATIC
))
600 c
->valid_until
= odhcpd_time() + 15;
604 if (c
->accept_reconf
)
608 static bool assign_pd(struct interface
*iface
, struct dhcp_assignment
*assign
)
610 struct dhcp_assignment
*c
;
612 if (iface
->dhcpv6_pd_manager
[0]) {
613 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
615 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
618 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
620 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
621 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
622 ustream_fd_init(&assign
->managed_sock
, fd
);
623 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
624 iaidbuf
, assign
->iaid
, assign
->length
);
625 ustream_write_pending(&assign
->managed_sock
.stream
);
626 assign
->managed_size
= -1;
628 if (!(assign
->flags
& OAF_STATIC
))
629 assign
->valid_until
= odhcpd_time() + 15;
631 list_add(&assign
->head
, &iface
->ia_assignments
);
633 /* Wait initial period of up to 250ms for immediate assignment */
634 if (poll(&pfd
, 1, 250) < 0) {
635 syslog(LOG_ERR
, "poll(): %m");
639 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
641 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
646 } else if (iface
->addr6_len
< 1)
649 /* Try honoring the hint first */
650 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
651 if (assign
->assigned
) {
652 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
653 if (c
->length
== 128 || c
->length
== 0)
656 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
657 list_add_tail(&assign
->head
, &c
->head
);
659 if (assign
->flags
& OAF_BOUND
)
660 apply_lease(iface
, assign
, true);
665 if (c
->assigned
!= 0)
666 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
670 /* Fallback to a variable assignment */
672 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
673 if (c
->length
== 128 || c
->length
== 0)
676 current
= (current
+ asize
) & (~asize
);
677 if (current
+ asize
< c
->assigned
) {
678 assign
->assigned
= current
;
679 list_add_tail(&assign
->head
, &c
->head
);
681 if (assign
->flags
& OAF_BOUND
)
682 apply_lease(iface
, assign
, true);
687 if (c
->assigned
!= 0)
688 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
694 static bool assign_na(struct interface
*iface
, struct dhcp_assignment
*assign
)
696 /* Seed RNG with checksum of DUID */
698 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
699 seed
+= assign
->clid_data
[i
];
702 /* Try to assign up to 100x */
703 for (size_t i
= 0; i
< 100; ++i
) {
705 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
707 struct dhcp_assignment
*c
;
708 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
712 if (c
->assigned
> try || c
->length
!= 128) {
713 assign
->assigned
= try;
714 list_add_tail(&assign
->head
, &c
->head
);
716 } else if (c
->assigned
== try)
724 static void handle_addrlist_change(struct netevent_handler_info
*info
)
726 struct interface
*iface
= info
->iface
;
727 struct dhcp_assignment
*c
, *d
, *border
= list_last_entry(
728 &iface
->ia_assignments
, struct dhcp_assignment
, head
);
729 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
730 time_t now
= odhcpd_time();
732 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
733 if (c
!= border
&& iface
->ra_managed
== RA_MANAGED_NO_MFLAG
734 && (c
->flags
& OAF_BOUND
))
735 __apply_lease(iface
, c
, info
->addrs_old
.addrs
,
736 info
->addrs_old
.len
, false);
739 set_border_assignment_size(iface
, border
);
741 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
742 if (c
->clid_len
== 0 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
746 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
747 list_move(&c
->head
, &reassign
);
748 else if (c
!= border
&& (c
->flags
& OAF_BOUND
))
749 apply_lease(iface
, c
, true);
751 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
752 struct dhcp_assignment
*a
;
756 /* Leave all other assignments of that client alone */
757 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
758 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
759 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
760 a
->reconf_cnt
= INT_MAX
;
764 while (!list_empty(&reassign
)) {
765 c
= list_first_entry(&reassign
, struct dhcp_assignment
, head
);
767 if (!assign_pd(iface
, c
)) {
769 list_add(&c
->head
, &iface
->ia_assignments
);
773 dhcpv6_ia_write_statefile();
776 static void reconf_timeout_cb(struct uloop_timeout
*event
)
778 struct dhcp_assignment
*a
= container_of(event
, struct dhcp_assignment
, reconf_timer
);
780 if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< DHCPV6_REC_MAX_RC
) {
782 uloop_timeout_set(&a
->reconf_timer
,
783 DHCPV6_REC_TIMEOUT
<< a
->reconf_cnt
);
789 static void start_reconf(struct dhcp_assignment
*a
)
791 uloop_timeout_set(&a
->reconf_timer
,
792 DHCPV6_REC_TIMEOUT
<< a
->reconf_cnt
);
793 a
->reconf_timer
.cb
= reconf_timeout_cb
;
799 static void stop_reconf(struct dhcp_assignment
*a
)
801 uloop_timeout_cancel(&a
->reconf_timer
);
803 a
->reconf_timer
.cb
= NULL
;
806 static void valid_until_cb(struct uloop_timeout
*event
)
808 struct interface
*iface
;
809 time_t now
= odhcpd_time();
811 avl_for_each_element(&interfaces
, iface
, avl
) {
812 struct dhcp_assignment
*a
, *n
;
814 if (iface
->dhcpv6
!= MODE_SERVER
|| iface
->ia_assignments
.next
== NULL
)
817 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
818 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
) {
819 if ((a
->length
< 128 && a
->clid_len
> 0) ||
820 (a
->length
== 128 && a
->clid_len
== 0))
821 free_dhcpv6_assignment(a
);
826 uloop_timeout_set(event
, 1000);
829 static size_t build_ia(uint8_t *buf
, size_t buflen
, uint16_t status
,
830 const struct dhcpv6_ia_hdr
*ia
, struct dhcp_assignment
*a
,
831 struct interface
*iface
, bool request
)
833 struct dhcpv6_ia_hdr o_ia
= {
840 size_t ia_len
= sizeof(o_ia
);
841 time_t now
= odhcpd_time();
847 struct __attribute__((packed
)) {
852 .type
= htons(DHCPV6_OPT_STATUS
),
853 .len
= htons(sizeof(o_status
) - 4),
854 .val
= htons(status
),
857 memcpy(buf
+ ia_len
, &o_status
, sizeof(o_status
));
858 ia_len
+= sizeof(o_status
);
860 o_ia
.len
= htons(ia_len
- 4);
861 memcpy(buf
, &o_ia
, sizeof(o_ia
));
870 leasetime
= a
->leasetime
;
872 leasetime
= iface
->dhcpv4_leasetime
;
874 uint32_t pref
= leasetime
;
875 uint32_t valid
= leasetime
;
877 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->addr6
;
878 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->addr6_len
;
879 size_t m
= get_preferred_addr(addrs
, addrlen
);
881 for (size_t i
= 0; i
< addrlen
; ++i
) {
882 uint32_t prefix_pref
= addrs
[i
].preferred
;
883 uint32_t prefix_valid
= addrs
[i
].valid
;
885 if (!valid_addr(&addrs
[i
], now
))
888 if (prefix_pref
!= UINT32_MAX
)
891 if (prefix_valid
!= UINT32_MAX
)
894 if (a
->length
< 128) {
895 struct dhcpv6_ia_prefix o_ia_p
= {
896 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
897 .len
= htons(sizeof(o_ia_p
) - 4),
898 .preferred
= htonl(prefix_pref
),
899 .valid
= htonl(prefix_valid
),
900 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
901 .addr
= addrs
[i
].addr
.in6
,
904 o_ia_p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
905 o_ia_p
.addr
.s6_addr32
[2] = o_ia_p
.addr
.s6_addr32
[3] = 0;
907 if ((a
->assigned
== 0 && a
->managed_size
== 0) ||
908 !valid_prefix_length(a
, addrs
[i
].prefix
))
911 if (buflen
< ia_len
+ sizeof(o_ia_p
))
914 memcpy(buf
+ ia_len
, &o_ia_p
, sizeof(o_ia_p
));
915 ia_len
+= sizeof(o_ia_p
);
917 struct dhcpv6_ia_addr o_ia_a
= {
918 .type
= htons(DHCPV6_OPT_IA_ADDR
),
919 .len
= htons(sizeof(o_ia_a
) - 4),
920 .addr
= addrs
[i
].addr
.in6
,
921 .preferred
= htonl(prefix_pref
),
922 .valid
= htonl(prefix_valid
)
925 o_ia_a
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
927 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
) ||
931 if (buflen
< ia_len
+ sizeof(o_ia_a
))
934 memcpy(buf
+ ia_len
, &o_ia_a
, sizeof(o_ia_a
));
935 ia_len
+= sizeof(o_ia_a
);
938 /* Calculate T1 / T2 based on non-deprecated addresses */
939 if (prefix_pref
> 0) {
940 if (prefix_pref
< pref
)
943 if (prefix_valid
< valid
)
944 valid
= prefix_valid
;
948 if (!INFINITE_VALID(a
->valid_until
))
949 /* UINT32_MAX is considered as infinite leasetime */
950 a
->valid_until
= (valid
== UINT32_MAX
) ? 0 : valid
+ now
;
952 o_ia
.t1
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 5 / 10);
953 o_ia
.t2
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 8 / 10);
963 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
964 uint16_t otype
, olen
;
966 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
967 struct dhcpv6_ia_prefix
*ia_p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
968 struct dhcpv6_ia_addr
*ia_a
= (struct dhcpv6_ia_addr
*)&odata
[-4];
971 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*ia_p
) - 4) &&
972 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*ia_a
) - 4))
976 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->addr6
;
977 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->addr6_len
;
979 for (size_t i
= 0; i
< addrlen
; ++i
) {
980 if (!valid_addr(&addrs
[i
], now
))
983 struct in6_addr addr
= addrs
[i
].addr
.in6
;
984 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
985 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
986 addr
.s6_addr32
[2] = addr
.s6_addr32
[3] = 0;
988 if (!memcmp(&ia_p
->addr
, &addr
, sizeof(addr
)) &&
989 ia_p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
992 addr
.s6_addr32
[3] = htonl(a
->assigned
);
994 if (!memcmp(&ia_a
->addr
, &addr
, sizeof(addr
)))
1001 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
1002 struct dhcpv6_ia_prefix o_ia_p
= {
1003 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
1004 .len
= htons(sizeof(o_ia_p
) - 4),
1007 .prefix
= ia_p
->prefix
,
1011 if (buflen
< ia_len
+ sizeof(o_ia_p
))
1014 memcpy(buf
+ ia_len
, &o_ia_p
, sizeof(o_ia_p
));
1015 ia_len
+= sizeof(o_ia_p
);
1017 struct dhcpv6_ia_addr o_ia_a
= {
1018 .type
= htons(DHCPV6_OPT_IA_ADDR
),
1019 .len
= htons(sizeof(o_ia_a
) - 4),
1025 if (buflen
< ia_len
+ sizeof(o_ia_a
))
1028 memcpy(buf
+ ia_len
, &o_ia_a
, sizeof(o_ia_a
));
1029 ia_len
+= sizeof(o_ia_a
);
1035 o_ia
.len
= htons(ia_len
- 4);
1036 memcpy(buf
, &o_ia
, sizeof(o_ia
));
1046 static void dhcpv6_log_ia_addr(struct in6_addr
*addr
, int prefix
, _unused
uint32_t pref
,
1047 _unused
uint32_t valid
, void *arg
)
1049 struct log_ctxt
*ctxt
= (struct log_ctxt
*)arg
;
1050 char addrbuf
[INET6_ADDRSTRLEN
];
1052 inet_ntop(AF_INET6
, addr
, addrbuf
, sizeof(addrbuf
));
1053 ctxt
->buf_idx
+= snprintf(ctxt
->buf
+ ctxt
->buf_idx
, ctxt
->buf_len
- ctxt
->buf_idx
,
1054 "%s/%d ", addrbuf
, prefix
);
1057 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
1058 const char *duidbuf
, bool is_pd
, struct dhcp_assignment
*a
, int code
)
1060 const char *type
= "UNKNOWN";
1061 const char *status
= "UNKNOWN";
1064 case DHCPV6_MSG_SOLICIT
:
1067 case DHCPV6_MSG_REQUEST
:
1070 case DHCPV6_MSG_CONFIRM
:
1073 case DHCPV6_MSG_RENEW
:
1076 case DHCPV6_MSG_REBIND
:
1079 case DHCPV6_MSG_RELEASE
:
1082 case DHCPV6_MSG_DECLINE
:
1088 case DHCPV6_STATUS_OK
:
1091 case DHCPV6_STATUS_NOADDRSAVAIL
:
1092 status
= "no addresses available";
1094 case DHCPV6_STATUS_NOBINDING
:
1095 status
= "no binding";
1097 case DHCPV6_STATUS_NOTONLINK
:
1098 status
= "not on-link";
1100 case DHCPV6_STATUS_NOPREFIXAVAIL
:
1101 status
= "no prefix available";
1105 char leasebuf
[256] = "";
1108 struct log_ctxt ctxt
= {.buf
= leasebuf
,
1109 .buf_len
= sizeof(leasebuf
),
1112 dhcpv6_ia_enum_addrs(iface
, a
, now
, dhcpv6_log_ia_addr
, &ctxt
);
1115 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
1116 duidbuf
, iface
->name
, status
, leasebuf
);
1119 static bool dhcpv6_ia_on_link(const struct dhcpv6_ia_hdr
*ia
, struct dhcp_assignment
*a
,
1120 struct interface
*iface
)
1122 struct odhcpd_ipaddr
*addrs
= (a
&& a
->managed
) ? a
->managed
: iface
->addr6
;
1123 size_t addrlen
= (a
&& a
->managed
) ? (size_t)a
->managed_size
: iface
->addr6_len
;
1124 time_t now
= odhcpd_time();
1125 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
1126 uint16_t otype
, olen
;
1129 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
1130 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
1131 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
1133 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
1134 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
1138 for (size_t i
= 0; i
< addrlen
; ++i
) {
1139 if (!valid_addr(&addrs
[i
], now
))
1142 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
1143 if (p
->prefix
< addrs
[i
].prefix
||
1144 odhcpd_bmemcmp(&p
->addr
, &addrs
[i
].addr
.in6
, addrs
[i
].prefix
))
1147 } else if (odhcpd_bmemcmp(&n
->addr
, &addrs
[i
].addr
.in6
, addrs
[i
].prefix
))
1160 ssize_t
dhcpv6_ia_handle_IAs(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
1161 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
1163 time_t now
= odhcpd_time();
1164 size_t response_len
= 0;
1165 const struct dhcpv6_client_header
*hdr
= data
;
1166 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
1167 uint16_t otype
, olen
;
1168 /* Find and parse client-id and hostname */
1169 bool accept_reconf
= false;
1170 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1172 size_t hostname_len
= 0;
1173 bool notonlink
= false;
1176 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1177 if (otype
== DHCPV6_OPT_CLIENTID
) {
1181 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
1182 memcpy(mac
, &odata
[8], sizeof(mac
));
1183 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
1184 memcpy(mac
, &odata
[4], sizeof(mac
));
1187 odhcpd_hexlify(duidbuf
, odata
, olen
);
1188 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
1189 uint8_t fqdn_buf
[256];
1190 memcpy(fqdn_buf
, odata
, olen
);
1191 fqdn_buf
[olen
++] = 0;
1193 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
1194 hostname_len
= strcspn(hostname
, ".");
1195 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
)
1196 accept_reconf
= true;
1199 if (!clid_data
|| !clid_len
|| clid_len
> 130)
1202 struct dhcp_assignment
*first
= NULL
;
1203 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1204 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
1205 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
1206 bool ia_addr_present
= false;
1207 if (!is_pd
&& !is_na
)
1210 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1211 size_t ia_response_len
= 0;
1212 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1213 uint32_t reqhint
= 0;
1215 /* Parse request hint for IA-PD */
1218 uint16_t stype
, slen
;
1219 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1220 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1223 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1226 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1227 if (reqlen
> 32 && reqlen
<= 64)
1228 reqhint
&= (1U << (64 - reqlen
)) - 1;
1236 uint16_t stype
, slen
;
1237 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1238 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1241 ia_addr_present
= true;
1245 /* Find assignment */
1246 struct dhcp_assignment
*c
, *a
= NULL
;
1247 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1248 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1249 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1250 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1251 (!(c
->flags
& (OAF_BOUND
|OAF_TENTATIVE
)) || c
->iaid
== ia
->iaid
) &&
1252 (INFINITE_VALID(c
->valid_until
) || now
< c
->valid_until
) &&
1253 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1257 if (a
->flags
& OAF_BOUND
)
1258 apply_lease(iface
, a
, false);
1260 memcpy(a
->clid_data
, clid_data
, clid_len
);
1261 a
->clid_len
= clid_len
;
1269 /* Generic message handling */
1270 uint16_t status
= DHCPV6_STATUS_OK
;
1271 if (a
&& a
->managed_size
< 0)
1274 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
||
1275 hdr
->msg_type
== DHCPV6_MSG_REQUEST
||
1276 (hdr
->msg_type
== DHCPV6_MSG_REBIND
&& !a
)) {
1277 bool assigned
= !!a
;
1279 if (!a
&& !iface
->no_dynamic_dhcp
&& (iface
->dhcpv6_pd
|| iface
->dhcpv6_na
)) {
1280 /* Create new binding */
1281 a
= calloc(1, sizeof(*a
) + clid_len
);
1283 a
->clid_len
= clid_len
;
1287 a
->assigned
= reqhint
;
1288 /* Set valid time to current time indicating */
1289 /* assignment is not having infinite lifetime */
1290 a
->valid_until
= now
;
1294 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1296 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1297 memcpy(a
->clid_data
, clid_data
, clid_len
);
1299 if (is_pd
&& iface
->dhcpv6_pd
)
1300 while (!(assigned
= assign_pd(iface
, a
)) &&
1301 !a
->managed_size
&& ++a
->length
<= 64);
1302 else if (is_na
&& iface
->dhcpv6_na
)
1303 assigned
= assign_na(iface
, a
);
1305 if (a
->managed_size
&& !assigned
)
1310 if (!assigned
|| iface
->addr6_len
== 0)
1311 /* Set error status */
1312 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1313 else if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
&& !dhcpv6_ia_on_link(ia
, a
, iface
)) {
1314 /* Send NOTONLINK staus for the IA */
1315 status
= DHCPV6_STATUS_NOTONLINK
;
1317 } else if (accept_reconf
&& assigned
&& !first
&&
1318 hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1319 size_t handshake_len
= 4;
1321 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1325 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1326 struct dhcpv6_auth_reconfigure auth
= {
1327 htons(DHCPV6_OPT_AUTH
),
1328 htons(sizeof(auth
) - 4),
1330 {htonl(time(NULL
)), htonl(++serial
)},
1334 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1335 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1336 handshake_len
+= sizeof(auth
);
1340 buf
+= handshake_len
;
1341 buflen
-= handshake_len
;
1342 response_len
+= handshake_len
;
1347 ia_response_len
= build_ia(buf
, buflen
, status
, ia
, a
, iface
,
1348 hdr
->msg_type
== DHCPV6_MSG_REBIND
? false : true);
1350 /* Was only a solicitation: mark binding for removal */
1351 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1352 a
->flags
&= ~OAF_BOUND
;
1353 a
->flags
|= OAF_TENTATIVE
;
1355 if (!(a
->flags
& OAF_STATIC
))
1356 /* Keep tentative assignment around for 60 seconds */
1357 a
->valid_until
= now
+ 60;
1358 } else if (assigned
&&
1359 (hdr
->msg_type
== DHCPV6_MSG_REQUEST
||
1360 hdr
->msg_type
== DHCPV6_MSG_REBIND
)) {
1361 if (hostname_len
> 0) {
1362 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1364 memcpy(a
->hostname
, hostname
, hostname_len
);
1365 a
->hostname
[hostname_len
] = 0;
1367 if (odhcpd_valid_hostname(a
->hostname
))
1368 a
->flags
&= ~OAF_BROKEN_HOSTNAME
;
1370 a
->flags
|= OAF_BROKEN_HOSTNAME
;
1373 a
->accept_reconf
= accept_reconf
;
1374 a
->flags
&= ~OAF_TENTATIVE
;
1375 a
->flags
|= OAF_BOUND
;
1376 apply_lease(iface
, a
, true);
1377 } else if (!assigned
&& a
&& a
->managed_size
== 0) {
1378 /* Cleanup failed assignment */
1379 free_dhcpv6_assignment(a
);
1382 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1383 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1384 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1385 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1386 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1387 status
= DHCPV6_STATUS_NOBINDING
;
1388 ia_response_len
= build_ia(buf
, buflen
, status
, ia
, a
, iface
, false);
1389 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1390 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1391 ia_response_len
= build_ia(buf
, buflen
, status
, ia
, a
, iface
, false);
1393 a
->flags
|= OAF_BOUND
;
1394 apply_lease(iface
, a
, true);
1396 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1397 if (!(a
->flags
& OAF_STATIC
))
1398 a
->valid_until
= now
- 1;
1400 if (a
->flags
& OAF_BOUND
) {
1401 apply_lease(iface
, a
, false);
1402 a
->flags
&= ~OAF_BOUND
;
1404 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1405 a
->flags
&= ~OAF_BOUND
;
1407 if (!(a
->flags
& OAF_STATIC
)) {
1409 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1412 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
) {
1413 if (ia_addr_present
&& !dhcpv6_ia_on_link(ia
, a
, iface
)) {
1418 if (!ia_addr_present
|| !a
|| !(a
->flags
& OAF_BOUND
)) {
1424 buf
+= ia_response_len
;
1425 buflen
-= ia_response_len
;
1426 response_len
+= ia_response_len
;
1427 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1430 switch (hdr
->msg_type
) {
1431 case DHCPV6_MSG_RELEASE
:
1432 case DHCPV6_MSG_DECLINE
:
1433 case DHCPV6_MSG_CONFIRM
:
1434 if (response_len
+ 6 < buflen
) {
1436 buf
[1] = DHCPV6_OPT_STATUS
;
1440 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1449 dhcpv6_ia_write_statefile();
1452 return response_len
;