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.
19 #include "libubox/md5.h"
20 #include "libubox/usock.h"
34 #include <arpa/inet.h>
35 #include <sys/timerfd.h>
37 #define ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) \
38 ((iface)->managed == RELAYD_MANAGED_NO_AFLAG || (i) == (m) || \
39 (addrs)[(i)].prefix > 64)
41 static void free_dhcpv6_assignment(struct dhcpv6_assignment
*c
);
42 static void reconf_timer(struct uloop_timeout
*event
);
43 static struct uloop_timeout reconf_event
= {.cb
= reconf_timer
};
44 static uint32_t serial
= 0;
45 static uint8_t statemd5
[16];
47 int dhcpv6_ia_init(void)
49 uloop_timeout_set(&reconf_event
, 2000);
53 int setup_dhcpv6_ia_interface(struct interface
*iface
, bool enable
)
55 if (!enable
&& iface
->ia_assignments
.next
) {
56 struct dhcpv6_assignment
*c
;
58 while (!list_empty(&iface
->ia_assignments
)) {
59 c
= list_first_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
60 free_dhcpv6_assignment(c
);
64 if (enable
&& iface
->dhcpv6
== RELAYD_SERVER
) {
65 if (!iface
->ia_assignments
.next
)
66 INIT_LIST_HEAD(&iface
->ia_assignments
);
68 if (list_empty(&iface
->ia_assignments
)) {
69 struct dhcpv6_assignment
*border
= calloc(1, sizeof(*border
));
71 syslog(LOG_ERR
, "Calloc failed for border on interface %s", iface
->ifname
);
76 list_add(&border
->head
, &iface
->ia_assignments
);
79 /* Parse static entries */
81 list_for_each_entry(lease
, &leases
, head
) {
83 size_t duid_len
= lease
->duid_len
? lease
->duid_len
: 14;
84 struct dhcpv6_assignment
*a
= calloc(1, sizeof(*a
) + duid_len
);
86 syslog(LOG_ERR
, "Calloc failed for static lease assignment on interface %s",
91 if (lease
->dhcpv4_leasetime
> 0)
92 a
->leasetime
= lease
->dhcpv4_leasetime
;
94 a
->clid_len
= duid_len
;
97 a
->assigned
= lease
->hostid
;
99 uint32_t i4a
= ntohl(lease
->ipaddr
.s_addr
) & 0xff;
100 a
->assigned
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
103 odhcpd_urandom(a
->key
, sizeof(a
->key
));
104 memcpy(a
->clid_data
, lease
->duid
, lease
->duid_len
);
105 memcpy(a
->mac
, lease
->mac
.ether_addr_octet
, sizeof(a
->mac
));
106 /* Static assignment */
107 a
->flags
|= OAF_STATIC
;
111 /* Assign to all interfaces */
112 struct dhcpv6_assignment
*c
;
113 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
114 if (c
->length
!= 128 || c
->assigned
> a
->assigned
) {
115 list_add_tail(&a
->head
, &c
->head
);
117 } else if (c
->assigned
== a
->assigned
)
118 /* Already an assignment with that number */
123 if (lease
->hostname
[0]) {
125 a
->hostname
= strdup(lease
->hostname
);
128 free_dhcpv6_assignment(a
);
134 static void free_dhcpv6_assignment(struct dhcpv6_assignment
*c
)
136 if (c
->managed_sock
.fd
.registered
) {
137 ustream_free(&c
->managed_sock
.stream
);
138 close(c
->managed_sock
.fd
.fd
);
149 static inline bool valid_prefix_length(const struct dhcpv6_assignment
*a
, const uint8_t prefix_length
)
151 return (a
->managed_size
|| a
->length
> prefix_length
);
154 static inline bool valid_addr(const struct odhcpd_ipaddr
*addr
, time_t now
)
156 return (addr
->prefix
<= 96 && addr
->preferred
> (uint32_t)now
);
159 static size_t elect_addr(const struct odhcpd_ipaddr
*addrs
, const size_t addrlen
)
163 for (i
= 0, m
= 0; i
< addrlen
; ++i
) {
164 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
165 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
166 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
173 static int send_reconf(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
176 struct dhcpv6_client_header hdr
;
180 uint16_t hardware_type
;
185 struct dhcpv6_auth_reconfigure auth
;
188 uint8_t clid_data
[128];
189 } __attribute__((packed
)) reconf_msg
= {
190 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
191 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
192 .srvid_len
= htons(10),
193 .duid_type
= htons(3),
194 .hardware_type
= htons(1),
195 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
197 .msg_id
= DHCPV6_MSG_RENEW
,
198 .auth
= {htons(DHCPV6_OPT_AUTH
),
199 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
200 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
201 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
202 .clid_len
= htons(assign
->clid_len
),
206 odhcpd_get_mac(iface
, reconf_msg
.mac
);
207 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
208 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
211 uint8_t secretbytes
[64];
212 memset(secretbytes
, 0, sizeof(secretbytes
));
213 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
215 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
216 secretbytes
[i
] ^= 0x36;
219 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
220 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
221 md5_end(reconf_msg
.auth
.key
, &md5
);
223 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
224 secretbytes
[i
] ^= 0x36;
225 secretbytes
[i
] ^= 0x5c;
229 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
230 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
231 md5_end(reconf_msg
.auth
.key
, &md5
);
233 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
236 void dhcpv6_write_statefile(void)
241 if (config
.dhcp_statefile
) {
242 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
243 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
247 lockf(fd
, F_LOCK
, 0);
248 if (ftruncate(fd
, 0) < 0) {}
250 FILE *fp
= fdopen(fd
, "w");
256 struct interface
*iface
;
257 list_for_each_entry(iface
, &interfaces
, head
) {
258 if (iface
->dhcpv6
!= RELAYD_SERVER
&& iface
->dhcpv4
!= RELAYD_SERVER
)
261 if (iface
->dhcpv6
== RELAYD_SERVER
&& iface
->ia_assignments
.next
) {
262 struct dhcpv6_assignment
*c
;
263 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
264 if (!(c
->flags
& OAF_BOUND
) || c
->managed_size
< 0)
267 char ipbuf
[INET6_ADDRSTRLEN
];
270 odhcpd_hexlify(duidbuf
, c
->clid_data
, c
->clid_len
);
272 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
273 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s %x %s %ld %x %u ",
274 iface
->ifname
, duidbuf
, ntohl(c
->iaid
),
275 (c
->hostname
? c
->hostname
: "-"),
276 (c
->valid_until
> now
?
277 (c
->valid_until
- now
+ wall_time
) :
278 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
279 c
->assigned
, (unsigned)c
->length
);
281 struct in6_addr addr
;
282 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->ia_addr
;
283 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->ia_addr_len
;
284 size_t m
= elect_addr(addrs
, addrlen
);
286 for (size_t i
= 0; i
< addrlen
; ++i
) {
287 if (!valid_addr(&addrs
[i
], now
) ||
288 (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
<= now
))
291 addr
= addrs
[i
].addr
;
292 if (c
->length
== 128) {
293 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
))
296 addr
.s6_addr32
[3] = htonl(c
->assigned
);
299 if (!valid_prefix_length(c
, addrs
[i
].prefix
))
302 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
305 inet_ntop(AF_INET6
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
307 if (c
->length
== 128 && c
->hostname
) {
311 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
312 iface
->search
, b
, sizeof(b
)) > 0)
313 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
315 fprintf(fp
, "\t%s\n", c
->hostname
);
316 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
317 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
320 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/%d ", ipbuf
,
321 (c
->managed_size
) ? addrs
[i
].prefix
: c
->length
);
323 leasebuf
[l
- 1] = '\n';
324 fwrite(leasebuf
, 1, l
, fp
);
328 if (iface
->dhcpv4
== RELAYD_SERVER
&& iface
->dhcpv4_assignments
.next
) {
329 struct dhcpv4_assignment
*c
;
330 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
331 if (!(c
->flags
& OAF_BOUND
))
334 char ipbuf
[INET6_ADDRSTRLEN
];
337 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
339 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
340 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s ipv4 %s %ld %x 32 ",
341 iface
->ifname
, duidbuf
,
342 (c
->hostname
? c
->hostname
: "-"),
343 (c
->valid_until
> now
?
344 (c
->valid_until
- now
+ wall_time
) :
345 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
348 struct in_addr addr
= {htonl(c
->addr
)};
349 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
355 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
356 iface
->search
, b
, sizeof(b
)) > 0)
357 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
359 fprintf(fp
, "\t%s\n", c
->hostname
);
360 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
361 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
364 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/32 ", ipbuf
);
365 leasebuf
[l
- 1] = '\n';
366 fwrite(leasebuf
, 1, l
, fp
);
375 md5_end(newmd5
, &md5
);
377 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
378 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
379 char *argv
[2] = {config
.dhcp_cb
, NULL
};
381 execv(argv
[0], argv
);
388 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
390 if (a
->length
> 64 || a
->managed_size
< 0)
393 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
394 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
396 for (size_t i
= 0; i
< addrlen
; ++i
) {
397 struct in6_addr prefix
= addrs
[i
].addr
;
398 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
399 odhcpd_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
400 iface
, &a
->peer
.sin6_addr
, 1024, add
);
404 /* More data was received from TCP connection */
405 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
407 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
408 time_t now
= odhcpd_time();
409 bool first
= c
->managed_size
< 0;
413 char *data
= ustream_get_read_buf(s
, &pending
);
414 char *end
= memmem(data
, pending
, "\n\n", 2);
423 if (c
->accept_reconf
)
427 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
428 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
429 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
431 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
432 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
435 x
= strtok_r(NULL
, ",", &saveptr2
);
436 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
439 x
= strtok_r(NULL
, ",", &saveptr2
);
440 if (sscanf(x
, "%u", &n
->preferred
) < 1)
443 x
= strtok_r(NULL
, ",", &saveptr2
);
444 if (sscanf(x
, "%u", &n
->valid
) < 1)
447 if (n
->preferred
> n
->valid
)
450 if (UINT32_MAX
- now
< n
->preferred
)
451 n
->preferred
= UINT32_MAX
;
455 if (UINT32_MAX
- now
< n
->valid
)
456 n
->valid
= UINT32_MAX
;
465 ustream_consume(s
, end
- data
);
468 if (first
&& c
->managed_size
== 0)
469 free_dhcpv6_assignment(c
);
470 else if (first
&& !(c
->flags
& OAF_STATIC
))
471 c
->valid_until
= now
+ 150;
475 /* TCP transmission has ended, either because of success or timeout or other error */
476 static void managed_handle_pd_done(struct ustream
*s
)
478 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
480 if (!(c
->flags
& OAF_STATIC
))
481 c
->valid_until
= odhcpd_time() + 15;
485 if (c
->accept_reconf
)
489 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
491 struct dhcpv6_assignment
*c
;
493 if (iface
->dhcpv6_pd_manager
[0]) {
494 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
497 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
499 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
500 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
501 ustream_fd_init(&assign
->managed_sock
, fd
);
502 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
503 iaidbuf
, assign
->iaid
, assign
->length
);
504 ustream_write_pending(&assign
->managed_sock
.stream
);
505 assign
->managed_size
= -1;
507 if (!(assign
->flags
& OAF_STATIC
))
508 assign
->valid_until
= odhcpd_time() + 15;
510 list_add(&assign
->head
, &iface
->ia_assignments
);
512 /* Wait initial period of up to 250ms for immediate assignment */
513 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
515 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
517 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
522 } else if (iface
->ia_addr_len
< 1)
525 /* Try honoring the hint first */
526 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
527 if (assign
->assigned
) {
528 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
529 if (c
->length
== 128 || c
->length
== 0)
532 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
533 list_add_tail(&assign
->head
, &c
->head
);
535 if (assign
->flags
& OAF_BOUND
)
536 apply_lease(iface
, assign
, true);
541 if (c
->assigned
!= 0)
542 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
546 /* Fallback to a variable assignment */
548 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
549 if (c
->length
== 128 || c
->length
== 0)
552 current
= (current
+ asize
) & (~asize
);
553 if (current
+ asize
< c
->assigned
) {
554 assign
->assigned
= current
;
555 list_add_tail(&assign
->head
, &c
->head
);
557 if (assign
->flags
& OAF_BOUND
)
558 apply_lease(iface
, assign
, true);
563 if (c
->assigned
!= 0)
564 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
570 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
572 /* Seed RNG with checksum of DUID */
574 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
575 seed
+= assign
->clid_data
[i
];
578 /* Try to assign up to 100x */
579 for (size_t i
= 0; i
< 100; ++i
) {
581 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
583 struct dhcpv6_assignment
*c
;
584 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
588 if (c
->assigned
> try || c
->length
!= 128) {
589 assign
->assigned
= try;
590 list_add_tail(&assign
->head
, &c
->head
);
592 } else if (c
->assigned
== try)
600 void dhcpv6_ia_preupdate(struct interface
*iface
)
602 if (iface
->dhcpv6
!= RELAYD_SERVER
)
605 struct dhcpv6_assignment
*c
, *border
= list_last_entry(
606 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
608 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
609 if (c
!= border
&& !iface
->managed
&& (c
->flags
& OAF_BOUND
))
610 apply_lease(iface
, c
, false);
613 void dhcpv6_ia_postupdate(struct interface
*iface
, time_t now
)
615 if (iface
->dhcpv6
!= RELAYD_SERVER
)
619 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
620 if (iface
->ia_addr
[i
].preferred
> (uint32_t)now
&&
621 iface
->ia_addr
[i
].prefix
< 64 &&
622 iface
->ia_addr
[i
].prefix
> minprefix
)
623 minprefix
= iface
->ia_addr
[i
].prefix
;
626 struct dhcpv6_assignment
*border
= list_last_entry(
627 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
629 if (minprefix
> 32 && minprefix
<= 64)
630 border
->assigned
= 1U << (64 - minprefix
);
632 border
->assigned
= 0;
634 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
635 struct dhcpv6_assignment
*c
, *d
;
636 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
637 if (c
->clid_len
== 0 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
641 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
642 list_move(&c
->head
, &reassign
);
643 else if (c
!= border
&& (c
->flags
& OAF_BOUND
))
644 apply_lease(iface
, c
, true);
646 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
648 c
->reconf_sent
= now
;
649 send_reconf(iface
, c
);
651 /* Leave all other assignments of that client alone */
652 struct dhcpv6_assignment
*a
;
653 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
654 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
655 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
656 c
->reconf_cnt
= INT_MAX
;
660 while (!list_empty(&reassign
)) {
661 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
663 if (!assign_pd(iface
, c
)) {
665 list_add(&c
->head
, &iface
->ia_assignments
);
669 dhcpv6_write_statefile();
672 static void reconf_timer(struct uloop_timeout
*event
)
674 time_t now
= odhcpd_time();
675 struct interface
*iface
;
676 list_for_each_entry(iface
, &interfaces
, head
) {
677 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
680 struct dhcpv6_assignment
*a
, *n
;
681 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
682 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
) {
683 if ((a
->length
< 128 && a
->clid_len
> 0) ||
684 (a
->length
== 128 && a
->clid_len
== 0))
685 free_dhcpv6_assignment(a
);
687 } else if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< 8 &&
688 now
> a
->reconf_sent
+ (1 << a
->reconf_cnt
)) {
690 a
->reconf_sent
= now
;
691 send_reconf(iface
, a
);
695 uloop_timeout_set(event
, 2000);
698 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
699 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
700 struct interface
*iface
, bool request
)
702 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
705 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
706 size_t datalen
= sizeof(out
);
707 time_t now
= odhcpd_time();
710 struct __attribute__((packed
)) {
714 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
717 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
718 datalen
+= sizeof(stat
);
722 if (a
->leasetime
> 0)
723 leasetime
= a
->leasetime
;
725 leasetime
= iface
->dhcpv4_leasetime
;
729 else if (leasetime
< 60)
732 uint32_t pref
= leasetime
;
733 uint32_t valid
= leasetime
;
735 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
736 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
737 size_t m
= elect_addr(addrs
, addrlen
);
739 for (size_t i
= 0; i
< addrlen
; ++i
) {
740 uint32_t prefix_pref
= addrs
[i
].preferred
;
741 uint32_t prefix_valid
= addrs
[i
].valid
;
743 if (!valid_addr(&addrs
[i
], now
))
746 if (prefix_pref
!= UINT32_MAX
)
749 if (prefix_valid
!= UINT32_MAX
)
752 if (a
->length
< 128) {
753 struct dhcpv6_ia_prefix p
= {
754 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
755 .len
= htons(sizeof(p
) - 4),
756 .preferred
= htonl(prefix_pref
),
757 .valid
= htonl(prefix_valid
),
758 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
759 .addr
= addrs
[i
].addr
761 p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
763 size_t entrlen
= sizeof(p
) - 4;
765 if (datalen
+ entrlen
+ 4 > buflen
||
766 (a
->assigned
== 0 && a
->managed_size
== 0) ||
767 !valid_prefix_length(a
, addrs
[i
].prefix
))
770 memcpy(buf
+ datalen
, &p
, sizeof(p
));
771 datalen
+= entrlen
+ 4;
773 struct dhcpv6_ia_addr n
= {
774 .type
= htons(DHCPV6_OPT_IA_ADDR
),
775 .len
= htons(sizeof(n
) - 4),
776 .addr
= addrs
[i
].addr
,
777 .preferred
= htonl(prefix_pref
),
778 .valid
= htonl(prefix_valid
)
780 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
781 size_t entrlen
= sizeof(n
) - 4;
783 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
) ||
785 datalen
+ entrlen
+ 4 > buflen
)
788 memcpy(buf
+ datalen
, &n
, sizeof(n
));
789 datalen
+= entrlen
+ 4;
792 /* Calculate T1 / T2 based on non-deprecated addresses */
793 if (prefix_pref
> 0) {
794 if (prefix_pref
< pref
)
797 if (prefix_valid
< valid
)
798 valid
= prefix_valid
;
802 if (!INFINITE_VALID(a
->valid_until
))
803 /* UINT32_MAX is considered as infinite leasetime */
804 a
->valid_until
= (valid
== UINT32_MAX
) ? 0 : valid
+ now
;
806 out
.t1
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 5 / 10);
807 out
.t2
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 8 / 10);
817 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
818 uint16_t otype
, olen
;
819 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
820 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
821 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
822 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
823 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
828 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
829 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
831 for (size_t i
= 0; i
< addrlen
; ++i
) {
832 if (!valid_addr(&addrs
[i
], now
))
835 struct in6_addr addr
= addrs
[i
].addr
;
836 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
837 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
839 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
840 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
843 addr
.s6_addr32
[3] = htonl(a
->assigned
);
845 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
852 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
853 struct dhcpv6_ia_prefix inv
= {
854 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
855 .len
= htons(sizeof(inv
) - 4),
862 if (datalen
+ sizeof(inv
) > buflen
)
865 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
866 datalen
+= sizeof(inv
);
868 struct dhcpv6_ia_addr inv
= {
869 .type
= htons(DHCPV6_OPT_IA_ADDR
),
870 .len
= htons(sizeof(inv
) - 4),
876 if (datalen
+ sizeof(inv
) > buflen
)
879 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
880 datalen
+= sizeof(inv
);
887 out
.len
= htons(datalen
- 4);
888 memcpy(buf
, &out
, sizeof(out
));
892 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
893 const char *duidbuf
, bool is_pd
, struct dhcpv6_assignment
*a
, int code
)
895 const char *type
= "UNKNOWN";
896 const char *status
= "UNKNOWN";
898 if (msgtype
== DHCPV6_MSG_RENEW
)
902 case DHCPV6_MSG_SOLICIT
:
905 case DHCPV6_MSG_REQUEST
:
908 case DHCPV6_MSG_CONFIRM
:
911 case DHCPV6_MSG_RENEW
:
914 case DHCPV6_MSG_REBIND
:
917 case DHCPV6_MSG_RELEASE
:
920 case DHCPV6_MSG_DECLINE
:
926 case DHCPV6_STATUS_OK
:
929 case DHCPV6_STATUS_NOADDRSAVAIL
:
930 status
= "no addresses available";
932 case DHCPV6_STATUS_NOBINDING
:
933 status
= "no binding";
935 case DHCPV6_STATUS_NOTONLINK
:
936 status
= "not on-link";
938 case DHCPV6_STATUS_NOPREFIXAVAIL
:
939 status
= "no prefix available";
943 char leasebuf
[256] = "";
946 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
947 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
949 size_t m
= elect_addr(addrs
, addrlen
);
950 char addrbuf
[INET6_ADDRSTRLEN
];
952 for (size_t i
= 0; i
< addrlen
; ++i
) {
953 if (!valid_addr(&addrs
[i
], now
))
956 struct in6_addr addr
= addrs
[i
].addr
;
957 int prefix
= a
->managed
? addrs
[i
].prefix
: a
->length
;
959 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
) ||
963 addr
.s6_addr32
[3] = htonl(a
->assigned
);
966 if (!valid_prefix_length(a
, addrs
[i
].prefix
))
969 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
972 inet_ntop(AF_INET6
, &addr
, addrbuf
, sizeof(addrbuf
));
973 lbsize
+= snprintf(leasebuf
+ lbsize
, sizeof(leasebuf
) - lbsize
, "%s/%d ", addrbuf
, prefix
);
977 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
978 duidbuf
, iface
->ifname
, status
, leasebuf
);
981 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
982 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
984 time_t now
= odhcpd_time();
985 size_t response_len
= 0;
986 const struct dhcpv6_client_header
*hdr
= data
;
987 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
988 uint16_t otype
, olen
;
989 /* Find and parse client-id and hostname */
990 bool accept_reconf
= false;
991 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
993 size_t hostname_len
= 0;
994 bool notonlink
= false;
997 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
998 if (otype
== DHCPV6_OPT_CLIENTID
) {
1002 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
1003 memcpy(mac
, &odata
[8], sizeof(mac
));
1004 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
1005 memcpy(mac
, &odata
[4], sizeof(mac
));
1008 odhcpd_hexlify(duidbuf
, odata
, olen
);
1009 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
1010 uint8_t fqdn_buf
[256];
1011 memcpy(fqdn_buf
, odata
, olen
);
1012 fqdn_buf
[olen
++] = 0;
1014 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
1015 hostname_len
= strcspn(hostname
, ".");
1016 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
)
1017 accept_reconf
= true;
1020 if (!clid_data
|| !clid_len
|| clid_len
> 130)
1023 struct dhcpv6_assignment
*first
= NULL
;
1024 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1025 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
1026 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
1027 bool ia_addr_present
= false;
1028 if (!is_pd
&& !is_na
)
1031 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1032 size_t ia_response_len
= 0;
1033 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1034 uint32_t reqhint
= 0;
1036 /* Parse request hint for IA-PD */
1039 uint16_t stype
, slen
;
1040 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1041 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1044 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1047 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1048 if (reqlen
> 32 && reqlen
<= 64)
1049 reqhint
&= (1U << (64 - reqlen
)) - 1;
1057 uint16_t stype
, slen
;
1058 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1059 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1062 ia_addr_present
= true;
1066 /* Find assignment */
1067 struct dhcpv6_assignment
*c
, *a
= NULL
;
1068 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1069 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1070 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1071 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1072 (c
->iaid
== ia
->iaid
|| INFINITE_VALID(c
->valid_until
) || now
< c
->valid_until
) &&
1073 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1077 if (a
->flags
& OAF_BOUND
)
1078 apply_lease(iface
, a
, false);
1080 memcpy(a
->clid_data
, clid_data
, clid_len
);
1081 a
->clid_len
= clid_len
;
1090 /* Generic message handling */
1091 uint16_t status
= DHCPV6_STATUS_OK
;
1092 if (a
&& a
->managed_size
< 0)
1095 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1096 bool assigned
= !!a
;
1098 if (!a
&& !iface
->no_dynamic_dhcp
) {
1099 /* Create new binding */
1100 a
= calloc(1, sizeof(*a
) + clid_len
);
1102 a
->clid_len
= clid_len
;
1106 a
->assigned
= reqhint
;
1107 /* Set valid time to current time indicating */
1108 /* assignment is not having infinite lifetime */
1109 a
->valid_until
= now
;
1112 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1114 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1115 memcpy(a
->clid_data
, clid_data
, clid_len
);
1118 while (!(assigned
= assign_pd(iface
, a
)) &&
1119 !a
->managed_size
&& ++a
->length
<= 64);
1121 assigned
= assign_na(iface
, a
);
1123 if (a
->managed_size
&& !assigned
)
1128 if (!assigned
|| iface
->ia_addr_len
== 0)
1129 /* Set error status */
1130 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1131 else if (assigned
&& !first
) {
1132 size_t handshake_len
= 4;
1134 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1138 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1139 struct dhcpv6_auth_reconfigure auth
= {
1140 htons(DHCPV6_OPT_AUTH
),
1141 htons(sizeof(auth
) - 4),
1143 {htonl(time(NULL
)), htonl(++serial
)},
1147 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1148 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1149 handshake_len
+= sizeof(auth
);
1152 buf
+= handshake_len
;
1153 buflen
-= handshake_len
;
1154 response_len
+= handshake_len
;
1159 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1161 /* Was only a solicitation: mark binding for removal */
1162 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1163 a
->flags
&= ~OAF_BOUND
;
1165 if (!(a
->flags
& OAF_STATIC
))
1166 a
->valid_until
= now
;
1167 } else if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1168 if (hostname_len
> 0) {
1169 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1171 memcpy(a
->hostname
, hostname
, hostname_len
);
1172 a
->hostname
[hostname_len
] = 0;
1175 a
->accept_reconf
= accept_reconf
;
1176 a
->flags
|= OAF_BOUND
;
1177 apply_lease(iface
, a
, true);
1178 } else if (!assigned
&& a
&& a
->managed_size
== 0) {
1179 /* Cleanup failed assignment */
1180 free_dhcpv6_assignment(a
);
1183 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1184 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1185 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1186 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1187 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1188 status
= DHCPV6_STATUS_NOBINDING
;
1189 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1190 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1191 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1192 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1194 a
->flags
|= OAF_BOUND
;
1195 apply_lease(iface
, a
, true);
1197 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1198 if (!(a
->flags
& OAF_STATIC
))
1199 a
->valid_until
= now
- 1;
1201 if (a
->flags
& OAF_BOUND
) {
1202 apply_lease(iface
, a
, false);
1203 a
->flags
&= ~OAF_BOUND
;
1205 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1206 a
->flags
&= ~OAF_BOUND
;
1208 if (!(a
->flags
& OAF_STATIC
)) {
1210 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1213 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1214 /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1215 status
= DHCPV6_STATUS_NOTONLINK
;
1216 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1220 buf
+= ia_response_len
;
1221 buflen
-= ia_response_len
;
1222 response_len
+= ia_response_len
;
1223 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1226 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1227 response_len
+ 6 < buflen
) {
1229 buf
[1] = DHCPV6_OPT_STATUS
;
1233 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1237 dhcpv6_write_statefile();
1240 return response_len
;