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 set_border_assignment_size(struct interface
*iface
, struct dhcp_assignment
*b
);
44 static void handle_addrlist_change(struct netevent_handler_info
*info
);
45 static void start_reconf(struct dhcp_assignment
*a
);
46 static void stop_reconf(struct dhcp_assignment
*a
);
47 static void valid_until_cb(struct uloop_timeout
*event
);
49 static struct netevent_handler dhcpv6_netevent_handler
= { .cb
= dhcpv6_netevent_cb
, };
50 static struct uloop_timeout valid_until_timeout
= {.cb
= valid_until_cb
};
51 static uint32_t serial
= 0;
52 static uint8_t statemd5
[16];
54 int dhcpv6_ia_init(void)
56 uloop_timeout_set(&valid_until_timeout
, 1000);
58 netlink_add_netevent_handler(&dhcpv6_netevent_handler
);
63 int dhcpv6_ia_setup_interface(struct interface
*iface
, bool enable
)
65 enable
= enable
&& (iface
->dhcpv6
== MODE_SERVER
);
68 struct dhcp_assignment
*border
;
70 if (list_empty(&iface
->ia_assignments
)) {
71 border
= alloc_assignment(0);
74 syslog(LOG_ERR
, "Failed to alloc border on %s", iface
->name
);
79 list_add(&border
->head
, &iface
->ia_assignments
);
81 border
= list_last_entry(&iface
->ia_assignments
, struct dhcp_assignment
, head
);
83 set_border_assignment_size(iface
, border
);
85 struct dhcp_assignment
*c
;
87 while (!list_empty(&iface
->ia_assignments
)) {
88 c
= list_first_entry(&iface
->ia_assignments
, struct dhcp_assignment
, head
);
97 static void dhcpv6_netevent_cb(unsigned long event
, struct netevent_handler_info
*info
)
99 struct interface
*iface
= info
->iface
;
101 if (!iface
|| iface
->dhcpv6
!= MODE_SERVER
)
105 case NETEV_ADDR6LIST_CHANGE
:
106 handle_addrlist_change(info
);
114 static inline bool valid_prefix_length(const struct dhcp_assignment
*a
, const uint8_t prefix_length
)
116 return (a
->managed_size
|| a
->length
> prefix_length
);
119 static inline bool valid_addr(const struct odhcpd_ipaddr
*addr
, time_t now
)
121 return (addr
->prefix
<= 96 && addr
->preferred
> (uint32_t)now
);
124 static size_t get_preferred_addr(const struct odhcpd_ipaddr
*addrs
, const size_t addrlen
)
128 for (i
= 0, m
= 0; i
< addrlen
; ++i
) {
129 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
130 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
131 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
138 static int send_reconf(struct dhcp_assignment
*assign
)
141 struct dhcpv6_client_header hdr
;
145 uint16_t hardware_type
;
150 struct dhcpv6_auth_reconfigure auth
;
153 uint8_t clid_data
[128];
154 } __attribute__((packed
)) reconf_msg
= {
155 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
156 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
157 .srvid_len
= htons(10),
158 .duid_type
= htons(3),
159 .hardware_type
= htons(1),
160 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
162 .msg_id
= DHCPV6_MSG_RENEW
,
163 .auth
= {htons(DHCPV6_OPT_AUTH
),
164 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
165 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
166 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
167 .clid_len
= htons(assign
->clid_len
),
170 struct interface
*iface
= assign
->iface
;
172 odhcpd_get_mac(iface
, reconf_msg
.mac
);
173 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
174 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
177 uint8_t secretbytes
[64];
178 memset(secretbytes
, 0, sizeof(secretbytes
));
179 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
181 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
182 secretbytes
[i
] ^= 0x36;
185 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
186 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
187 md5_end(reconf_msg
.auth
.key
, &md5
);
189 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
190 secretbytes
[i
] ^= 0x36;
191 secretbytes
[i
] ^= 0x5c;
195 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
196 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
197 md5_end(reconf_msg
.auth
.key
, &md5
);
199 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
202 static void dhcpv6_ia_free_assignment(struct dhcp_assignment
*a
)
204 if (a
->managed_sock
.fd
.registered
) {
205 ustream_free(&a
->managed_sock
.stream
);
206 close(a
->managed_sock
.fd
.fd
);
215 void dhcpv6_ia_enum_addrs(struct interface
*iface
, struct dhcp_assignment
*c
,
216 time_t now
, dhcpv6_binding_cb_handler_t func
, void *arg
)
218 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->addr6
;
219 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->addr6_len
;
220 size_t m
= get_preferred_addr(addrs
, addrlen
);
222 for (size_t i
= 0; i
< addrlen
; ++i
) {
223 struct in6_addr addr
;
224 uint32_t pref
, valid
;
225 int prefix
= c
->managed
? addrs
[i
].prefix
: c
->length
;
227 if (!valid_addr(&addrs
[i
], now
))
230 addr
= addrs
[i
].addr
.in6
;
231 pref
= addrs
[i
].preferred
;
232 valid
= addrs
[i
].valid
;
234 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
))
237 addr
.s6_addr32
[3] = htonl(c
->assigned
);
239 if (!valid_prefix_length(c
, addrs
[i
].prefix
))
242 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
243 addr
.s6_addr32
[2] = addr
.s6_addr32
[3] = 0;
246 if (pref
!= UINT32_MAX
)
249 if (valid
!= UINT32_MAX
)
252 func(&addr
, prefix
, pref
, valid
, arg
);
259 struct dhcp_assignment
*c
;
260 struct interface
*iface
;
266 static void dhcpv6_write_ia_addr(struct in6_addr
*addr
, int prefix
, _unused
uint32_t pref
,
267 _unused
uint32_t valid
, void *arg
)
269 struct write_ctxt
*ctxt
= (struct write_ctxt
*)arg
;
270 char ipbuf
[INET6_ADDRSTRLEN
];
272 inet_ntop(AF_INET6
, addr
, ipbuf
, sizeof(ipbuf
) - 1);
274 if (ctxt
->c
->length
== 128 && ctxt
->c
->hostname
&&
275 !(ctxt
->c
->flags
& OAF_BROKEN_HOSTNAME
)) {
276 fputs(ipbuf
, ctxt
->fp
);
279 if (dn_expand(ctxt
->iface
->search
, ctxt
->iface
->search
+ ctxt
->iface
->search_len
,
280 ctxt
->iface
->search
, b
, sizeof(b
)) > 0)
281 fprintf(ctxt
->fp
, "\t%s.%s", ctxt
->c
->hostname
, b
);
283 fprintf(ctxt
->fp
, "\t%s\n", ctxt
->c
->hostname
);
284 md5_hash(ipbuf
, strlen(ipbuf
), &ctxt
->md5
);
285 md5_hash(ctxt
->c
->hostname
, strlen(ctxt
->c
->hostname
), &ctxt
->md5
);
288 ctxt
->buf_idx
+= snprintf(ctxt
->buf
+ ctxt
->buf_idx
,ctxt
->buf_len
- ctxt
->buf_idx
,
289 "%s/%d ", ipbuf
, prefix
);
292 void dhcpv6_ia_write_statefile(void)
294 struct write_ctxt ctxt
;
296 md5_begin(&ctxt
.md5
);
298 if (config
.dhcp_statefile
) {
299 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
300 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
306 ret
= lockf(fd
, F_LOCK
, 0);
311 if (ftruncate(fd
, 0) < 0) {}
313 ctxt
.fp
= fdopen(fd
, "w");
320 ctxt
.buf_len
= sizeof(leasebuf
);
322 avl_for_each_element(&interfaces
, ctxt
.iface
, avl
) {
323 if (ctxt
.iface
->dhcpv6
!= MODE_SERVER
&&
324 ctxt
.iface
->dhcpv4
!= MODE_SERVER
)
327 if (ctxt
.iface
->dhcpv6
== MODE_SERVER
) {
328 list_for_each_entry(ctxt
.c
, &ctxt
.iface
->ia_assignments
, head
) {
329 if (!(ctxt
.c
->flags
& OAF_BOUND
) || ctxt
.c
->managed_size
< 0)
334 odhcpd_hexlify(duidbuf
, ctxt
.c
->clid_data
, ctxt
.c
->clid_len
);
336 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
337 ctxt
.buf_idx
= snprintf(ctxt
.buf
, ctxt
.buf_len
, "# %s %s %x %s%s %ld %x %u ",
338 ctxt
.iface
->ifname
, duidbuf
, ntohl(ctxt
.c
->iaid
),
339 (ctxt
.c
->flags
& OAF_BROKEN_HOSTNAME
) ? "broken\\x20" : "",
340 (ctxt
.c
->hostname
? ctxt
.c
->hostname
: "-"),
341 (ctxt
.c
->valid_until
> now
?
342 (ctxt
.c
->valid_until
- now
+ wall_time
) :
343 (INFINITE_VALID(ctxt
.c
->valid_until
) ? -1 : 0)),
344 ctxt
.c
->assigned
, (unsigned)ctxt
.c
->length
);
346 if (INFINITE_VALID(ctxt
.c
->valid_until
) || ctxt
.c
->valid_until
> now
)
347 dhcpv6_ia_enum_addrs(ctxt
.iface
, ctxt
.c
, now
,
348 dhcpv6_write_ia_addr
, &ctxt
);
350 ctxt
.buf
[ctxt
.buf_idx
- 1] = '\n';
351 fwrite(ctxt
.buf
, 1, ctxt
.buf_idx
, ctxt
.fp
);
355 if (ctxt
.iface
->dhcpv4
== MODE_SERVER
) {
356 struct dhcp_assignment
*c
;
358 list_for_each_entry(c
, &ctxt
.iface
->dhcpv4_assignments
, head
) {
359 if (!(c
->flags
& OAF_BOUND
))
362 char ipbuf
[INET6_ADDRSTRLEN
];
364 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
366 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
367 ctxt
.buf_idx
= snprintf(ctxt
.buf
, ctxt
.buf_len
, "# %s %s ipv4 %s%s %ld %x 32 ",
368 ctxt
.iface
->ifname
, duidbuf
,
369 (c
->flags
& OAF_BROKEN_HOSTNAME
) ? "broken\\x20" : "",
370 (c
->hostname
? c
->hostname
: "-"),
371 (c
->valid_until
> now
?
372 (c
->valid_until
- now
+ wall_time
) :
373 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
376 struct in_addr addr
= {.s_addr
= c
->addr
};
377 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
379 if (c
->hostname
&& !(c
->flags
& OAF_BROKEN_HOSTNAME
)) {
380 fputs(ipbuf
, ctxt
.fp
);
383 if (dn_expand(ctxt
.iface
->search
,
384 ctxt
.iface
->search
+ ctxt
.iface
->search_len
,
385 ctxt
.iface
->search
, b
, sizeof(b
)) > 0)
386 fprintf(ctxt
.fp
, "\t%s.%s", c
->hostname
, b
);
388 fprintf(ctxt
.fp
, "\t%s\n", c
->hostname
);
389 md5_hash(ipbuf
, strlen(ipbuf
), &ctxt
.md5
);
390 md5_hash(c
->hostname
, strlen(c
->hostname
), &ctxt
.md5
);
393 ctxt
.buf_idx
+= snprintf(ctxt
.buf
+ ctxt
.buf_idx
,
394 ctxt
.buf_len
- ctxt
.buf_idx
,
396 ctxt
.buf
[ctxt
.buf_idx
- 1] = '\n';
397 fwrite(ctxt
.buf
, 1, ctxt
.buf_idx
, ctxt
.fp
);
406 md5_end(newmd5
, &ctxt
.md5
);
408 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
409 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
410 char *argv
[2] = {config
.dhcp_cb
, NULL
};
412 execv(argv
[0], argv
);
418 static void __apply_lease(struct interface
*iface
, struct dhcp_assignment
*a
,
419 struct odhcpd_ipaddr
*addrs
, ssize_t addr_len
, bool add
)
424 for (ssize_t i
= 0; i
< addr_len
; ++i
) {
425 struct in6_addr prefix
= addrs
[i
].addr
.in6
;
426 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
427 prefix
.s6_addr32
[2] = prefix
.s6_addr32
[3] = 0;
428 netlink_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
429 iface
->ifindex
, &a
->peer
.sin6_addr
, 1024, add
);
433 static void apply_lease(struct interface
*iface
, struct dhcp_assignment
*a
, bool add
)
435 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->addr6
;
436 ssize_t addrlen
= (a
->managed
) ? a
->managed_size
: (ssize_t
)iface
->addr6_len
;
438 __apply_lease(iface
, a
, addrs
, addrlen
, add
);
441 /* Set border assignment size based on the IPv6 address prefixes */
442 static void set_border_assignment_size(struct interface
*iface
, struct dhcp_assignment
*b
)
444 time_t now
= odhcpd_time();
447 for (size_t i
= 0; i
< iface
->addr6_len
; ++i
) {
448 if (iface
->addr6
[i
].preferred
> (uint32_t)now
&&
449 iface
->addr6
[i
].prefix
< 64 &&
450 iface
->addr6
[i
].prefix
> minprefix
)
451 minprefix
= iface
->addr6
[i
].prefix
;
454 if (minprefix
> 32 && minprefix
<= 64)
455 b
->assigned
= 1U << (64 - minprefix
);
460 /* More data was received from TCP connection */
461 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
463 struct ustream_fd
*fd
= container_of(s
, struct ustream_fd
, stream
);
464 struct dhcp_assignment
*c
= container_of(fd
, struct dhcp_assignment
, managed_sock
);
465 time_t now
= odhcpd_time();
466 bool first
= c
->managed_size
< 0;
470 char *data
= ustream_get_read_buf(s
, &pending
);
471 char *end
= memmem(data
, pending
, "\n\n", 2);
480 if (c
->accept_reconf
)
484 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
485 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
486 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
488 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
489 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
492 x
= strtok_r(NULL
, ",", &saveptr2
);
493 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
496 x
= strtok_r(NULL
, ",", &saveptr2
);
497 if (sscanf(x
, "%u", &n
->preferred
) < 1)
500 x
= strtok_r(NULL
, ",", &saveptr2
);
501 if (sscanf(x
, "%u", &n
->valid
) < 1)
504 if (n
->preferred
> n
->valid
)
507 if (UINT32_MAX
- now
< n
->preferred
)
508 n
->preferred
= UINT32_MAX
;
512 if (UINT32_MAX
- now
< n
->valid
)
513 n
->valid
= UINT32_MAX
;
522 ustream_consume(s
, end
- data
);
525 if (first
&& c
->managed_size
== 0)
527 else if (first
&& !(c
->flags
& OAF_STATIC
))
528 c
->valid_until
= now
+ 150;
532 /* TCP transmission has ended, either because of success or timeout or other error */
533 static void managed_handle_pd_done(struct ustream
*s
)
535 struct ustream_fd
*fd
= container_of(s
, struct ustream_fd
, stream
);
536 struct dhcp_assignment
*c
= container_of(fd
, struct dhcp_assignment
, managed_sock
);
538 if (!(c
->flags
& OAF_STATIC
))
539 c
->valid_until
= odhcpd_time() + 15;
543 if (c
->accept_reconf
)
547 static bool assign_pd(struct interface
*iface
, struct dhcp_assignment
*assign
)
549 struct dhcp_assignment
*c
;
551 if (iface
->dhcpv6_pd_manager
[0]) {
552 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
554 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
557 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
559 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
560 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
561 ustream_fd_init(&assign
->managed_sock
, fd
);
562 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
563 iaidbuf
, assign
->iaid
, assign
->length
);
564 ustream_write_pending(&assign
->managed_sock
.stream
);
565 assign
->managed_size
= -1;
567 if (!(assign
->flags
& OAF_STATIC
))
568 assign
->valid_until
= odhcpd_time() + 15;
570 list_add(&assign
->head
, &iface
->ia_assignments
);
572 /* Wait initial period of up to 250ms for immediate assignment */
573 if (poll(&pfd
, 1, 250) < 0) {
574 syslog(LOG_ERR
, "poll(): %m");
578 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
580 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
585 } else if (iface
->addr6_len
< 1)
588 /* Try honoring the hint first */
589 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
590 if (assign
->assigned
) {
591 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
592 if (c
->length
== 128 || c
->length
== 0)
595 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
596 list_add_tail(&assign
->head
, &c
->head
);
598 if (assign
->flags
& OAF_BOUND
)
599 apply_lease(iface
, assign
, true);
604 if (c
->assigned
!= 0)
605 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
609 /* Fallback to a variable assignment */
611 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
612 if (c
->length
== 128 || c
->length
== 0)
615 current
= (current
+ asize
) & (~asize
);
616 if (current
+ asize
< c
->assigned
) {
617 assign
->assigned
= current
;
618 list_add_tail(&assign
->head
, &c
->head
);
620 if (assign
->flags
& OAF_BOUND
)
621 apply_lease(iface
, assign
, true);
626 if (c
->assigned
!= 0)
627 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
633 static bool assign_na(struct interface
*iface
, struct dhcp_assignment
*a
)
635 struct dhcp_assignment
*c
;
638 /* Preconfigured assignment by static lease */
640 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
644 if (c
->assigned
> a
->assigned
|| c
->length
!= 128) {
645 list_add_tail(&a
->head
, &c
->head
);
647 } else if (c
->assigned
== a
->assigned
)
652 /* Seed RNG with checksum of DUID */
653 for (size_t i
= 0; i
< a
->clid_len
; ++i
)
654 seed
+= a
->clid_data
[i
];
657 /* Try to assign up to 100x */
658 for (size_t i
= 0; i
< 100; ++i
) {
660 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
662 if (config_find_lease_by_hostid(try))
665 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
669 if (c
->assigned
> try || c
->length
!= 128) {
671 list_add_tail(&a
->head
, &c
->head
);
673 } else if (c
->assigned
== try)
681 static void handle_addrlist_change(struct netevent_handler_info
*info
)
683 struct interface
*iface
= info
->iface
;
684 struct dhcp_assignment
*c
, *d
, *border
= list_last_entry(
685 &iface
->ia_assignments
, struct dhcp_assignment
, head
);
686 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
687 time_t now
= odhcpd_time();
689 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
690 if (c
!= border
&& !(iface
->ra_flags
& ND_RA_FLAG_MANAGED
)
691 && (c
->flags
& OAF_BOUND
))
692 __apply_lease(iface
, c
, info
->addrs_old
.addrs
,
693 info
->addrs_old
.len
, false);
696 set_border_assignment_size(iface
, border
);
698 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
699 if (c
->clid_len
== 0 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
703 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
704 list_move(&c
->head
, &reassign
);
705 else if (c
!= border
&& (c
->flags
& OAF_BOUND
))
706 apply_lease(iface
, c
, true);
708 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
709 struct dhcp_assignment
*a
;
713 /* Leave all other assignments of that client alone */
714 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
715 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
716 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
717 a
->reconf_cnt
= INT_MAX
;
721 while (!list_empty(&reassign
)) {
722 c
= list_first_entry(&reassign
, struct dhcp_assignment
, head
);
723 list_del_init(&c
->head
);
724 if (!assign_pd(iface
, c
)) {
726 list_add(&c
->head
, &iface
->ia_assignments
);
730 dhcpv6_ia_write_statefile();
733 static void reconf_timeout_cb(struct uloop_timeout
*event
)
735 struct dhcp_assignment
*a
= container_of(event
, struct dhcp_assignment
, reconf_timer
);
737 if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< DHCPV6_REC_MAX_RC
) {
739 uloop_timeout_set(&a
->reconf_timer
,
740 DHCPV6_REC_TIMEOUT
<< a
->reconf_cnt
);
746 static void start_reconf(struct dhcp_assignment
*a
)
748 uloop_timeout_set(&a
->reconf_timer
,
749 DHCPV6_REC_TIMEOUT
<< a
->reconf_cnt
);
750 a
->reconf_timer
.cb
= reconf_timeout_cb
;
756 static void stop_reconf(struct dhcp_assignment
*a
)
758 uloop_timeout_cancel(&a
->reconf_timer
);
760 a
->reconf_timer
.cb
= NULL
;
763 static void valid_until_cb(struct uloop_timeout
*event
)
765 struct interface
*iface
;
766 time_t now
= odhcpd_time();
768 avl_for_each_element(&interfaces
, iface
, avl
) {
769 struct dhcp_assignment
*a
, *n
;
771 if (iface
->dhcpv6
!= MODE_SERVER
)
774 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
775 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
) {
776 if ((a
->length
< 128 && a
->clid_len
> 0) ||
777 (a
->length
== 128 && a
->clid_len
== 0))
783 uloop_timeout_set(event
, 1000);
786 static size_t build_ia(uint8_t *buf
, size_t buflen
, uint16_t status
,
787 const struct dhcpv6_ia_hdr
*ia
, struct dhcp_assignment
*a
,
788 struct interface
*iface
, bool request
)
790 struct dhcpv6_ia_hdr o_ia
= {
797 size_t ia_len
= sizeof(o_ia
);
798 time_t now
= odhcpd_time();
804 struct __attribute__((packed
)) {
809 .type
= htons(DHCPV6_OPT_STATUS
),
810 .len
= htons(sizeof(o_status
) - 4),
811 .val
= htons(status
),
814 memcpy(buf
+ ia_len
, &o_status
, sizeof(o_status
));
815 ia_len
+= sizeof(o_status
);
817 o_ia
.len
= htons(ia_len
- 4);
818 memcpy(buf
, &o_ia
, sizeof(o_ia
));
827 leasetime
= a
->leasetime
;
829 leasetime
= iface
->dhcpv4_leasetime
;
831 uint32_t pref
= leasetime
;
832 uint32_t valid
= leasetime
;
834 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->addr6
;
835 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->addr6_len
;
836 size_t m
= get_preferred_addr(addrs
, addrlen
);
838 for (size_t i
= 0; i
< addrlen
; ++i
) {
839 uint32_t prefix_pref
= addrs
[i
].preferred
;
840 uint32_t prefix_valid
= addrs
[i
].valid
;
842 if (!valid_addr(&addrs
[i
], now
))
845 if (prefix_pref
!= UINT32_MAX
)
848 if (prefix_valid
!= UINT32_MAX
)
851 if (a
->length
< 128) {
852 struct dhcpv6_ia_prefix o_ia_p
= {
853 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
854 .len
= htons(sizeof(o_ia_p
) - 4),
855 .preferred
= htonl(prefix_pref
),
856 .valid
= htonl(prefix_valid
),
857 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
858 .addr
= addrs
[i
].addr
.in6
,
861 o_ia_p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
862 o_ia_p
.addr
.s6_addr32
[2] = o_ia_p
.addr
.s6_addr32
[3] = 0;
864 if ((a
->assigned
== 0 && a
->managed_size
== 0) ||
865 !valid_prefix_length(a
, addrs
[i
].prefix
))
868 if (buflen
< ia_len
+ sizeof(o_ia_p
))
871 memcpy(buf
+ ia_len
, &o_ia_p
, sizeof(o_ia_p
));
872 ia_len
+= sizeof(o_ia_p
);
874 struct dhcpv6_ia_addr o_ia_a
= {
875 .type
= htons(DHCPV6_OPT_IA_ADDR
),
876 .len
= htons(sizeof(o_ia_a
) - 4),
877 .addr
= addrs
[i
].addr
.in6
,
878 .preferred
= htonl(prefix_pref
),
879 .valid
= htonl(prefix_valid
)
882 o_ia_a
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
884 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
) ||
888 if (buflen
< ia_len
+ sizeof(o_ia_a
))
891 memcpy(buf
+ ia_len
, &o_ia_a
, sizeof(o_ia_a
));
892 ia_len
+= sizeof(o_ia_a
);
895 /* Calculate T1 / T2 based on non-deprecated addresses */
896 if (prefix_pref
> 0) {
897 if (prefix_pref
< pref
)
900 if (prefix_valid
< valid
)
901 valid
= prefix_valid
;
905 if (!INFINITE_VALID(a
->valid_until
))
906 /* UINT32_MAX is considered as infinite leasetime */
907 a
->valid_until
= (valid
== UINT32_MAX
) ? 0 : valid
+ now
;
909 o_ia
.t1
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 5 / 10);
910 o_ia
.t2
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 8 / 10);
920 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
921 uint16_t otype
, olen
;
923 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
924 struct dhcpv6_ia_prefix
*ia_p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
925 struct dhcpv6_ia_addr
*ia_a
= (struct dhcpv6_ia_addr
*)&odata
[-4];
928 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*ia_p
) - 4) &&
929 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*ia_a
) - 4))
933 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->addr6
;
934 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->addr6_len
;
936 for (size_t i
= 0; i
< addrlen
; ++i
) {
937 if (!valid_addr(&addrs
[i
], now
))
940 struct in6_addr addr
= addrs
[i
].addr
.in6
;
941 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
942 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
943 addr
.s6_addr32
[2] = addr
.s6_addr32
[3] = 0;
945 if (!memcmp(&ia_p
->addr
, &addr
, sizeof(addr
)) &&
946 ia_p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
949 addr
.s6_addr32
[3] = htonl(a
->assigned
);
951 if (!memcmp(&ia_a
->addr
, &addr
, sizeof(addr
)))
958 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
959 struct dhcpv6_ia_prefix o_ia_p
= {
960 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
961 .len
= htons(sizeof(o_ia_p
) - 4),
964 .prefix
= ia_p
->prefix
,
968 if (buflen
< ia_len
+ sizeof(o_ia_p
))
971 memcpy(buf
+ ia_len
, &o_ia_p
, sizeof(o_ia_p
));
972 ia_len
+= sizeof(o_ia_p
);
974 struct dhcpv6_ia_addr o_ia_a
= {
975 .type
= htons(DHCPV6_OPT_IA_ADDR
),
976 .len
= htons(sizeof(o_ia_a
) - 4),
982 if (buflen
< ia_len
+ sizeof(o_ia_a
))
985 memcpy(buf
+ ia_len
, &o_ia_a
, sizeof(o_ia_a
));
986 ia_len
+= sizeof(o_ia_a
);
992 o_ia
.len
= htons(ia_len
- 4);
993 memcpy(buf
, &o_ia
, sizeof(o_ia
));
1003 static void dhcpv6_log_ia_addr(struct in6_addr
*addr
, int prefix
, _unused
uint32_t pref
,
1004 _unused
uint32_t valid
, void *arg
)
1006 struct log_ctxt
*ctxt
= (struct log_ctxt
*)arg
;
1007 char addrbuf
[INET6_ADDRSTRLEN
];
1009 inet_ntop(AF_INET6
, addr
, addrbuf
, sizeof(addrbuf
));
1010 ctxt
->buf_idx
+= snprintf(ctxt
->buf
+ ctxt
->buf_idx
, ctxt
->buf_len
- ctxt
->buf_idx
,
1011 "%s/%d ", addrbuf
, prefix
);
1014 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
1015 const char *duidbuf
, bool is_pd
, struct dhcp_assignment
*a
, int code
)
1017 const char *type
= "UNKNOWN";
1018 const char *status
= "UNKNOWN";
1021 case DHCPV6_MSG_SOLICIT
:
1024 case DHCPV6_MSG_REQUEST
:
1027 case DHCPV6_MSG_CONFIRM
:
1030 case DHCPV6_MSG_RENEW
:
1033 case DHCPV6_MSG_REBIND
:
1036 case DHCPV6_MSG_RELEASE
:
1039 case DHCPV6_MSG_DECLINE
:
1045 case DHCPV6_STATUS_OK
:
1048 case DHCPV6_STATUS_NOADDRSAVAIL
:
1049 status
= "no addresses available";
1051 case DHCPV6_STATUS_NOBINDING
:
1052 status
= "no binding";
1054 case DHCPV6_STATUS_NOTONLINK
:
1055 status
= "not on-link";
1057 case DHCPV6_STATUS_NOPREFIXAVAIL
:
1058 status
= "no prefix available";
1062 char leasebuf
[256] = "";
1065 struct log_ctxt ctxt
= {.buf
= leasebuf
,
1066 .buf_len
= sizeof(leasebuf
),
1069 dhcpv6_ia_enum_addrs(iface
, a
, now
, dhcpv6_log_ia_addr
, &ctxt
);
1072 syslog(LOG_NOTICE
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
1073 duidbuf
, iface
->name
, status
, leasebuf
);
1076 static bool dhcpv6_ia_on_link(const struct dhcpv6_ia_hdr
*ia
, struct dhcp_assignment
*a
,
1077 struct interface
*iface
)
1079 struct odhcpd_ipaddr
*addrs
= (a
&& a
->managed
) ? a
->managed
: iface
->addr6
;
1080 size_t addrlen
= (a
&& a
->managed
) ? (size_t)a
->managed_size
: iface
->addr6_len
;
1081 time_t now
= odhcpd_time();
1082 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
1083 uint16_t otype
, olen
;
1086 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
1087 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
1088 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
1090 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
1091 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
1095 for (size_t i
= 0; i
< addrlen
; ++i
) {
1096 if (!valid_addr(&addrs
[i
], now
))
1099 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
1100 if (p
->prefix
< addrs
[i
].prefix
||
1101 odhcpd_bmemcmp(&p
->addr
, &addrs
[i
].addr
.in6
, addrs
[i
].prefix
))
1104 } else if (odhcpd_bmemcmp(&n
->addr
, &addrs
[i
].addr
.in6
, addrs
[i
].prefix
))
1117 ssize_t
dhcpv6_ia_handle_IAs(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
1118 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
1121 struct dhcp_assignment
*first
= NULL
;
1122 const struct dhcpv6_client_header
*hdr
= data
;
1123 time_t now
= odhcpd_time();
1124 uint16_t otype
, olen
, clid_len
= 0;
1125 uint8_t *start
= (uint8_t *)&hdr
[1], *odata
;
1126 uint8_t *clid_data
= NULL
, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1127 size_t hostname_len
= 0, response_len
= 0;
1128 bool notonlink
= false, rapid_commit
= false, accept_reconf
= false;
1129 char duidbuf
[261], hostname
[256];
1131 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1132 if (otype
== DHCPV6_OPT_CLIENTID
) {
1136 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
1137 memcpy(mac
, &odata
[8], sizeof(mac
));
1138 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
1139 memcpy(mac
, &odata
[4], sizeof(mac
));
1142 odhcpd_hexlify(duidbuf
, odata
, olen
);
1143 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
1144 uint8_t fqdn_buf
[256];
1145 memcpy(fqdn_buf
, odata
, olen
);
1146 fqdn_buf
[olen
++] = 0;
1148 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
1149 hostname_len
= strcspn(hostname
, ".");
1150 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
)
1151 accept_reconf
= true;
1152 else if (otype
== DHCPV6_OPT_RAPID_COMMIT
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
)
1153 rapid_commit
= true;
1156 if (!clid_data
|| !clid_len
|| clid_len
> 130)
1159 l
= config_find_lease_by_duid(clid_data
, clid_len
);
1161 l
= config_find_lease_by_mac(mac
);
1163 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1164 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
1165 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
1166 bool ia_addr_present
= false;
1167 if (!is_pd
&& !is_na
)
1170 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1171 size_t ia_response_len
= 0;
1172 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1173 uint32_t reqhint
= 0;
1175 /* Parse request hint for IA-PD */
1178 uint16_t stype
, slen
;
1179 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1180 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1183 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1186 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1187 if (reqlen
> 32 && reqlen
<= 64)
1188 reqhint
&= (1U << (64 - reqlen
)) - 1;
1196 uint16_t stype
, slen
;
1197 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1198 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1201 ia_addr_present
= true;
1205 /* Find assignment */
1206 struct dhcp_assignment
*c
, *a
= NULL
;
1207 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1208 if ((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) &&
1209 c
->iaid
== ia
->iaid
&& (INFINITE_VALID(c
->valid_until
) || now
< c
->valid_until
) &&
1210 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1214 if (a
->flags
& OAF_BOUND
)
1215 apply_lease(iface
, a
, false);
1222 if (l
&& a
&& a
->lease
!= l
) {
1227 /* Generic message handling */
1228 uint16_t status
= DHCPV6_STATUS_OK
;
1229 if (a
&& a
->managed_size
< 0)
1232 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
||
1233 hdr
->msg_type
== DHCPV6_MSG_REQUEST
||
1234 (hdr
->msg_type
== DHCPV6_MSG_REBIND
&& !a
)) {
1235 bool assigned
= !!a
;
1238 if ((!iface
->no_dynamic_dhcp
|| (l
&& is_na
)) &&
1239 (iface
->dhcpv6_pd
|| iface
->dhcpv6_na
)) {
1240 /* Create new binding */
1241 a
= alloc_assignment(clid_len
);
1244 a
->clid_len
= clid_len
;
1245 memcpy(a
->clid_data
, clid_data
, clid_len
);
1249 a
->assigned
= is_na
&& l
? l
->hostid
: reqhint
;
1250 /* Set valid time to 0 for static lease indicating */
1251 /* infinite lifetime otherwise current time */
1252 a
->valid_until
= l
? 0 : now
;
1253 a
->dhcp_free_cb
= dhcpv6_ia_free_assignment
;
1255 a
->flags
= OAF_DHCPV6
;
1258 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1260 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1262 if (is_pd
&& iface
->dhcpv6_pd
)
1263 while (!(assigned
= assign_pd(iface
, a
)) &&
1264 !a
->managed_size
&& ++a
->length
<= 64);
1265 else if (is_na
&& iface
->dhcpv6_na
)
1266 assigned
= assign_na(iface
, a
);
1268 if (l
&& assigned
) {
1269 a
->flags
|= OAF_STATIC
;
1272 a
->hostname
= strdup(l
->hostname
);
1275 a
->leasetime
= l
->leasetime
;
1277 list_add(&a
->lease_list
, &l
->assignments
);
1281 if (a
->managed_size
&& !assigned
)
1287 if (!assigned
|| iface
->addr6_len
== 0)
1288 /* Set error status */
1289 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1290 else if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
&& !dhcpv6_ia_on_link(ia
, a
, iface
)) {
1291 /* Send NOTONLINK staus for the IA */
1292 status
= DHCPV6_STATUS_NOTONLINK
;
1294 } else if (accept_reconf
&& assigned
&& !first
&&
1295 hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1296 size_t handshake_len
= 4;
1298 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1302 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1303 struct dhcpv6_auth_reconfigure auth
= {
1304 htons(DHCPV6_OPT_AUTH
),
1305 htons(sizeof(auth
) - 4),
1307 {htonl(time(NULL
)), htonl(++serial
)},
1311 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1312 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1313 handshake_len
+= sizeof(auth
);
1317 buf
+= handshake_len
;
1318 buflen
-= handshake_len
;
1319 response_len
+= handshake_len
;
1324 ia_response_len
= build_ia(buf
, buflen
, status
, ia
, a
, iface
,
1325 hdr
->msg_type
== DHCPV6_MSG_REBIND
? false : true);
1327 /* Was only a solicitation: mark binding for removal */
1328 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
&& !rapid_commit
) {
1329 a
->flags
&= ~OAF_BOUND
;
1330 a
->flags
|= OAF_TENTATIVE
;
1332 if (!(a
->flags
& OAF_STATIC
))
1333 /* Keep tentative assignment around for 60 seconds */
1334 a
->valid_until
= now
+ 60;
1336 } else if (assigned
&&
1337 ((hdr
->msg_type
== DHCPV6_MSG_SOLICIT
&& rapid_commit
) ||
1338 hdr
->msg_type
== DHCPV6_MSG_REQUEST
||
1339 hdr
->msg_type
== DHCPV6_MSG_REBIND
)) {
1340 if ((!(a
->flags
& OAF_STATIC
) || !a
->hostname
) && hostname_len
> 0) {
1341 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1343 memcpy(a
->hostname
, hostname
, hostname_len
);
1344 a
->hostname
[hostname_len
] = 0;
1346 if (odhcpd_valid_hostname(a
->hostname
))
1347 a
->flags
&= ~OAF_BROKEN_HOSTNAME
;
1349 a
->flags
|= OAF_BROKEN_HOSTNAME
;
1352 a
->accept_reconf
= accept_reconf
;
1353 a
->flags
&= ~OAF_TENTATIVE
;
1354 a
->flags
|= OAF_BOUND
;
1355 apply_lease(iface
, a
, true);
1356 } else if (!assigned
&& a
&& a
->managed_size
== 0) {
1357 /* Cleanup failed assignment */
1361 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1362 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1363 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1364 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1365 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1366 status
= DHCPV6_STATUS_NOBINDING
;
1367 ia_response_len
= build_ia(buf
, buflen
, status
, ia
, a
, iface
, false);
1368 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1369 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1370 ia_response_len
= build_ia(buf
, buflen
, status
, ia
, a
, iface
, false);
1372 a
->flags
|= OAF_BOUND
;
1373 apply_lease(iface
, a
, true);
1375 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1376 if (!(a
->flags
& OAF_STATIC
))
1377 a
->valid_until
= now
- 1;
1379 if (a
->flags
& OAF_BOUND
) {
1380 apply_lease(iface
, a
, false);
1381 a
->flags
&= ~OAF_BOUND
;
1383 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1384 a
->flags
&= ~OAF_BOUND
;
1386 if (!(a
->flags
& OAF_STATIC
)) {
1388 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1391 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
) {
1392 if (ia_addr_present
&& !dhcpv6_ia_on_link(ia
, a
, iface
)) {
1397 if (!ia_addr_present
|| !a
|| !(a
->flags
& OAF_BOUND
)) {
1403 buf
+= ia_response_len
;
1404 buflen
-= ia_response_len
;
1405 response_len
+= ia_response_len
;
1406 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1409 switch (hdr
->msg_type
) {
1410 case DHCPV6_MSG_RELEASE
:
1411 case DHCPV6_MSG_DECLINE
:
1412 case DHCPV6_MSG_CONFIRM
:
1413 if (response_len
+ 6 < buflen
) {
1415 buf
[1] = DHCPV6_OPT_STATUS
;
1419 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1428 dhcpv6_ia_write_statefile();
1431 return response_len
;