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 static void free_dhcpv6_assignment(struct dhcpv6_assignment
*c
);
38 static void reconf_timer(struct uloop_timeout
*event
);
39 static struct uloop_timeout reconf_event
= {.cb
= reconf_timer
};
40 static uint32_t serial
= 0;
41 static uint8_t statemd5
[16];
43 int dhcpv6_ia_init(void)
45 uloop_timeout_set(&reconf_event
, 2000);
49 int setup_dhcpv6_ia_interface(struct interface
*iface
, bool enable
)
51 if (!enable
&& iface
->ia_assignments
.next
) {
52 struct dhcpv6_assignment
*c
;
54 while (!list_empty(&iface
->ia_assignments
)) {
55 c
= list_first_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
56 free_dhcpv6_assignment(c
);
60 if (enable
&& iface
->dhcpv6
== RELAYD_SERVER
) {
61 if (!iface
->ia_assignments
.next
)
62 INIT_LIST_HEAD(&iface
->ia_assignments
);
64 if (list_empty(&iface
->ia_assignments
)) {
65 struct dhcpv6_assignment
*border
= calloc(1, sizeof(*border
));
67 syslog(LOG_ERR
, "Calloc failed for border on interface %s", iface
->ifname
);
72 list_add(&border
->head
, &iface
->ia_assignments
);
75 /* Parse static entries */
77 list_for_each_entry(lease
, &leases
, head
) {
79 size_t duid_len
= lease
->duid_len
? lease
->duid_len
: 14;
80 struct dhcpv6_assignment
*a
= calloc(1, sizeof(*a
) + duid_len
);
82 syslog(LOG_ERR
, "Calloc failed for static lease assignment on interface %s",
87 if (lease
->dhcpv4_leasetime
> 0)
88 a
->leasetime
= lease
->dhcpv4_leasetime
;
90 a
->clid_len
= duid_len
;
93 a
->assigned
= lease
->hostid
;
95 uint32_t i4a
= ntohl(lease
->ipaddr
.s_addr
) & 0xff;
96 a
->assigned
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
99 odhcpd_urandom(a
->key
, sizeof(a
->key
));
100 memcpy(a
->clid_data
, lease
->duid
, lease
->duid_len
);
101 memcpy(a
->mac
, lease
->mac
.ether_addr_octet
, sizeof(a
->mac
));
102 /* Static assignment */
103 a
->flags
|= OAF_STATIC
;
107 /* Assign to all interfaces */
108 struct dhcpv6_assignment
*c
;
109 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
110 if (c
->length
!= 128 || c
->assigned
> a
->assigned
) {
111 list_add_tail(&a
->head
, &c
->head
);
113 } else if (c
->assigned
== a
->assigned
)
114 /* Already an assignment with that number */
119 if (lease
->hostname
[0]) {
121 a
->hostname
= strdup(lease
->hostname
);
124 free_dhcpv6_assignment(a
);
130 static void free_dhcpv6_assignment(struct dhcpv6_assignment
*c
)
132 if (c
->managed_sock
.fd
.registered
) {
133 ustream_free(&c
->managed_sock
.stream
);
134 close(c
->managed_sock
.fd
.fd
);
145 static inline bool valid_addr(const struct odhcpd_ipaddr
*addr
, time_t now
)
147 return (addr
->prefix
<= 96 && addr
->preferred
> (uint32_t)now
);
150 static size_t elect_addr(const struct odhcpd_ipaddr
*addrs
, const size_t addrlen
)
154 for (i
= 0, m
= 0; i
< addrlen
; ++i
) {
155 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
156 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
157 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
164 static int send_reconf(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
167 struct dhcpv6_client_header hdr
;
171 uint16_t hardware_type
;
176 struct dhcpv6_auth_reconfigure auth
;
179 uint8_t clid_data
[128];
180 } __attribute__((packed
)) reconf_msg
= {
181 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
182 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
183 .srvid_len
= htons(10),
184 .duid_type
= htons(3),
185 .hardware_type
= htons(1),
186 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
188 .msg_id
= DHCPV6_MSG_RENEW
,
189 .auth
= {htons(DHCPV6_OPT_AUTH
),
190 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
191 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
192 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
193 .clid_len
= htons(assign
->clid_len
),
197 odhcpd_get_mac(iface
, reconf_msg
.mac
);
198 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
199 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
202 uint8_t secretbytes
[64];
203 memset(secretbytes
, 0, sizeof(secretbytes
));
204 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
206 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
207 secretbytes
[i
] ^= 0x36;
210 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
211 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
212 md5_end(reconf_msg
.auth
.key
, &md5
);
214 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
215 secretbytes
[i
] ^= 0x36;
216 secretbytes
[i
] ^= 0x5c;
220 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
221 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
222 md5_end(reconf_msg
.auth
.key
, &md5
);
224 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
227 void dhcpv6_write_statefile(void)
232 if (config
.dhcp_statefile
) {
233 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
234 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
238 lockf(fd
, F_LOCK
, 0);
239 if (ftruncate(fd
, 0) < 0) {}
241 FILE *fp
= fdopen(fd
, "w");
247 struct interface
*iface
;
248 list_for_each_entry(iface
, &interfaces
, head
) {
249 if (iface
->dhcpv6
!= RELAYD_SERVER
&& iface
->dhcpv4
!= RELAYD_SERVER
)
252 if (iface
->dhcpv6
== RELAYD_SERVER
&& iface
->ia_assignments
.next
) {
253 struct dhcpv6_assignment
*c
;
254 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
255 if (!(c
->flags
& OAF_BOUND
) || c
->managed_size
< 0)
258 char ipbuf
[INET6_ADDRSTRLEN
];
261 odhcpd_hexlify(duidbuf
, c
->clid_data
, c
->clid_len
);
263 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
264 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s %x %s %ld %x %u ",
265 iface
->ifname
, duidbuf
, ntohl(c
->iaid
),
266 (c
->hostname
? c
->hostname
: "-"),
267 (c
->valid_until
> now
?
268 (c
->valid_until
- now
+ wall_time
) :
269 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
270 c
->assigned
, (unsigned)c
->length
);
272 struct in6_addr addr
;
273 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->ia_addr
;
274 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->ia_addr_len
;
275 size_t m
= elect_addr(addrs
, addrlen
);
277 for (size_t i
= 0; i
< addrlen
; ++i
) {
278 if (!valid_addr(&addrs
[i
], now
) || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
<= now
) ||
279 (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
280 addrs
[i
].prefix
<= 64))
283 addr
= addrs
[i
].addr
;
284 if (c
->length
== 128)
285 addr
.s6_addr32
[3] = htonl(c
->assigned
);
287 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
289 inet_ntop(AF_INET6
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
291 if (c
->length
== 128 && c
->hostname
) {
295 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
296 iface
->search
, b
, sizeof(b
)) > 0)
297 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
299 fprintf(fp
, "\t%s\n", c
->hostname
);
300 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
301 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
304 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/%d ", ipbuf
,
305 (c
->managed_size
) ? addrs
[i
].prefix
: c
->length
);
307 leasebuf
[l
- 1] = '\n';
308 fwrite(leasebuf
, 1, l
, fp
);
312 if (iface
->dhcpv4
== RELAYD_SERVER
&& iface
->dhcpv4_assignments
.next
) {
313 struct dhcpv4_assignment
*c
;
314 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
315 if (!(c
->flags
& OAF_BOUND
))
318 char ipbuf
[INET6_ADDRSTRLEN
];
321 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
323 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
324 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s ipv4 %s %ld %x 32 ",
325 iface
->ifname
, duidbuf
,
326 (c
->hostname
? c
->hostname
: "-"),
327 (c
->valid_until
> now
?
328 (c
->valid_until
- now
+ wall_time
) :
329 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
332 struct in_addr addr
= {htonl(c
->addr
)};
333 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
339 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
340 iface
->search
, b
, sizeof(b
)) > 0)
341 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
343 fprintf(fp
, "\t%s\n", c
->hostname
);
344 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
345 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
348 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/32 ", ipbuf
);
349 leasebuf
[l
- 1] = '\n';
350 fwrite(leasebuf
, 1, l
, fp
);
359 md5_end(newmd5
, &md5
);
361 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
362 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
363 char *argv
[2] = {config
.dhcp_cb
, NULL
};
365 execv(argv
[0], argv
);
372 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
374 if (a
->length
> 64 || a
->managed_size
< 0)
377 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
378 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
380 for (size_t i
= 0; i
< addrlen
; ++i
) {
381 struct in6_addr prefix
= addrs
[i
].addr
;
382 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
383 odhcpd_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
384 iface
, &a
->peer
.sin6_addr
, 1024, add
);
388 /* More data was received from TCP connection */
389 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
391 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
392 time_t now
= odhcpd_time();
393 bool first
= c
->managed_size
< 0;
397 char *data
= ustream_get_read_buf(s
, &pending
);
398 char *end
= memmem(data
, pending
, "\n\n", 2);
407 if (c
->accept_reconf
)
411 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
412 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
413 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
415 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
416 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
419 x
= strtok_r(NULL
, ",", &saveptr2
);
420 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
423 x
= strtok_r(NULL
, ",", &saveptr2
);
424 if (sscanf(x
, "%u", &n
->preferred
) < 1)
427 x
= strtok_r(NULL
, ",", &saveptr2
);
428 if (sscanf(x
, "%u", &n
->valid
) < 1)
431 if (n
->preferred
> n
->valid
)
434 if (UINT32_MAX
- now
< n
->preferred
)
435 n
->preferred
= UINT32_MAX
;
439 if (UINT32_MAX
- now
< n
->valid
)
440 n
->valid
= UINT32_MAX
;
449 ustream_consume(s
, end
- data
);
452 if (first
&& c
->managed_size
== 0)
453 free_dhcpv6_assignment(c
);
454 else if (first
&& !(c
->flags
& OAF_STATIC
))
455 c
->valid_until
= now
+ 150;
459 /* TCP transmission has ended, either because of success or timeout or other error */
460 static void managed_handle_pd_done(struct ustream
*s
)
462 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
464 if (!(c
->flags
& OAF_STATIC
))
465 c
->valid_until
= odhcpd_time() + 15;
469 if (c
->accept_reconf
)
473 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
475 struct dhcpv6_assignment
*c
;
477 if (iface
->dhcpv6_pd_manager
[0]) {
478 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
481 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
483 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
484 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
485 ustream_fd_init(&assign
->managed_sock
, fd
);
486 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
487 iaidbuf
, assign
->iaid
, assign
->length
);
488 ustream_write_pending(&assign
->managed_sock
.stream
);
489 assign
->managed_size
= -1;
491 if (!(assign
->flags
& OAF_STATIC
))
492 assign
->valid_until
= odhcpd_time() + 15;
494 list_add(&assign
->head
, &iface
->ia_assignments
);
496 /* Wait initial period of up to 250ms for immediate assignment */
497 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
499 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
501 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
506 } else if (iface
->ia_addr_len
< 1)
509 /* Try honoring the hint first */
510 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
511 if (assign
->assigned
) {
512 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
513 if (c
->length
== 128 || c
->length
== 0)
516 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
517 list_add_tail(&assign
->head
, &c
->head
);
518 apply_lease(iface
, assign
, true);
522 if (c
->assigned
!= 0)
523 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
527 /* Fallback to a variable assignment */
529 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
530 if (c
->length
== 128 || c
->length
== 0)
533 current
= (current
+ asize
) & (~asize
);
534 if (current
+ asize
< c
->assigned
) {
535 assign
->assigned
= current
;
536 list_add_tail(&assign
->head
, &c
->head
);
537 apply_lease(iface
, assign
, true);
541 if (c
->assigned
!= 0)
542 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
548 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
550 /* Seed RNG with checksum of DUID */
552 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
553 seed
+= assign
->clid_data
[i
];
556 /* Try to assign up to 100x */
557 for (size_t i
= 0; i
< 100; ++i
) {
559 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
561 struct dhcpv6_assignment
*c
;
562 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
566 if (c
->assigned
> try || c
->length
!= 128) {
567 assign
->assigned
= try;
568 list_add_tail(&assign
->head
, &c
->head
);
570 } else if (c
->assigned
== try)
578 void dhcpv6_ia_preupdate(struct interface
*iface
)
580 if (iface
->dhcpv6
!= RELAYD_SERVER
)
583 struct dhcpv6_assignment
*c
, *border
= list_last_entry(
584 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
586 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
587 if (c
!= border
&& !iface
->managed
)
588 apply_lease(iface
, c
, false);
591 void dhcpv6_ia_postupdate(struct interface
*iface
, time_t now
)
593 if (iface
->dhcpv6
!= RELAYD_SERVER
)
597 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
598 if (iface
->ia_addr
[i
].preferred
> (uint32_t)now
&&
599 iface
->ia_addr
[i
].prefix
< 64 &&
600 iface
->ia_addr
[i
].prefix
> minprefix
)
601 minprefix
= iface
->ia_addr
[i
].prefix
;
604 struct dhcpv6_assignment
*border
= list_last_entry(
605 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
607 if (minprefix
> 32 && minprefix
<= 64)
608 border
->assigned
= 1U << (64 - minprefix
);
610 border
->assigned
= 0;
612 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
613 struct dhcpv6_assignment
*c
, *d
;
614 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
615 if (c
->clid_len
== 0 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
619 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
620 list_move(&c
->head
, &reassign
);
621 else if (c
!= border
)
622 apply_lease(iface
, c
, true);
624 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
626 c
->reconf_sent
= now
;
627 send_reconf(iface
, c
);
629 /* Leave all other assignments of that client alone */
630 struct dhcpv6_assignment
*a
;
631 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
632 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
633 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
634 c
->reconf_cnt
= INT_MAX
;
638 while (!list_empty(&reassign
)) {
639 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
641 if (!assign_pd(iface
, c
)) {
643 list_add(&c
->head
, &iface
->ia_assignments
);
647 dhcpv6_write_statefile();
650 static void reconf_timer(struct uloop_timeout
*event
)
652 time_t now
= odhcpd_time();
653 struct interface
*iface
;
654 list_for_each_entry(iface
, &interfaces
, head
) {
655 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
658 struct dhcpv6_assignment
*a
, *n
;
659 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
660 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
) {
661 if ((a
->length
< 128 && a
->clid_len
> 0) ||
662 (a
->length
== 128 && a
->clid_len
== 0))
663 free_dhcpv6_assignment(a
);
665 } else if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< 8 &&
666 now
> a
->reconf_sent
+ (1 << a
->reconf_cnt
)) {
668 a
->reconf_sent
= now
;
669 send_reconf(iface
, a
);
673 uloop_timeout_set(event
, 2000);
676 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
677 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
678 struct interface
*iface
, bool request
)
680 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
683 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
684 size_t datalen
= sizeof(out
);
685 time_t now
= odhcpd_time();
688 struct __attribute__((packed
)) {
692 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
695 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
696 datalen
+= sizeof(stat
);
700 if (a
->leasetime
> 0)
701 leasetime
= a
->leasetime
;
703 leasetime
= iface
->dhcpv4_leasetime
;
707 else if (leasetime
< 60)
710 uint32_t pref
= leasetime
;
711 uint32_t valid
= leasetime
;
713 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
714 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
715 size_t m
= elect_addr(addrs
, addrlen
);
717 for (size_t i
= 0; i
< addrlen
; ++i
) {
718 uint32_t prefix_pref
= addrs
[i
].preferred
;
719 uint32_t prefix_valid
= addrs
[i
].valid
;
721 if (!valid_addr(&addrs
[i
], now
))
724 if (prefix_pref
!= UINT32_MAX
)
727 if (prefix_valid
!= UINT32_MAX
)
730 if (a
->length
< 128) {
731 struct dhcpv6_ia_prefix p
= {
732 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
733 .len
= htons(sizeof(p
) - 4),
734 .preferred
= htonl(prefix_pref
),
735 .valid
= htonl(prefix_valid
),
736 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
737 .addr
= addrs
[i
].addr
739 p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
741 size_t entrlen
= sizeof(p
) - 4;
743 if (datalen
+ entrlen
+ 4 > buflen
||
744 (a
->assigned
== 0 && a
->managed_size
== 0) ||
745 (!a
->managed_size
&& a
->length
<= addrs
[i
].prefix
))
748 memcpy(buf
+ datalen
, &p
, sizeof(p
));
749 datalen
+= entrlen
+ 4;
751 struct dhcpv6_ia_addr n
= {
752 .type
= htons(DHCPV6_OPT_IA_ADDR
),
753 .len
= htons(sizeof(n
) - 4),
754 .addr
= addrs
[i
].addr
,
755 .preferred
= htonl(prefix_pref
),
756 .valid
= htonl(prefix_valid
)
758 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
759 size_t entrlen
= sizeof(n
) - 4;
761 if (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
762 addrs
[i
].prefix
<= 64)
765 if (datalen
+ entrlen
+ 4 > buflen
|| a
->assigned
== 0)
768 memcpy(buf
+ datalen
, &n
, sizeof(n
));
769 datalen
+= entrlen
+ 4;
772 /* Calculate T1 / T2 based on non-deprecated addresses */
773 if (prefix_pref
> 0) {
774 if (prefix_pref
< pref
)
777 if (prefix_valid
< valid
)
778 valid
= prefix_valid
;
782 if (!INFINITE_VALID(a
->valid_until
))
783 /* UINT32_MAX is considered as infinite leasetime */
784 a
->valid_until
= (valid
== UINT32_MAX
) ? 0 : valid
+ now
;
786 out
.t1
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 5 / 10);
787 out
.t2
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 8 / 10);
797 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
798 uint16_t otype
, olen
;
799 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
800 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
801 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
802 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
803 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
808 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
809 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
811 for (size_t i
= 0; i
< addrlen
; ++i
) {
812 if (!valid_addr(&addrs
[i
], now
))
815 struct in6_addr addr
= addrs
[i
].addr
;
816 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
817 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
819 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
820 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
823 addr
.s6_addr32
[3] = htonl(a
->assigned
);
825 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
832 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
833 struct dhcpv6_ia_prefix inv
= {
834 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
835 .len
= htons(sizeof(inv
) - 4),
842 if (datalen
+ sizeof(inv
) > buflen
)
845 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
846 datalen
+= sizeof(inv
);
848 struct dhcpv6_ia_addr inv
= {
849 .type
= htons(DHCPV6_OPT_IA_ADDR
),
850 .len
= htons(sizeof(inv
) - 4),
856 if (datalen
+ sizeof(inv
) > buflen
)
859 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
860 datalen
+= sizeof(inv
);
867 out
.len
= htons(datalen
- 4);
868 memcpy(buf
, &out
, sizeof(out
));
872 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
873 const char *duidbuf
, bool is_pd
, struct dhcpv6_assignment
*a
, int code
)
875 const char *type
= "UNKNOWN";
876 const char *status
= "UNKNOWN";
878 if (msgtype
== DHCPV6_MSG_RENEW
)
882 case DHCPV6_MSG_SOLICIT
:
885 case DHCPV6_MSG_REQUEST
:
888 case DHCPV6_MSG_CONFIRM
:
891 case DHCPV6_MSG_RENEW
:
894 case DHCPV6_MSG_REBIND
:
897 case DHCPV6_MSG_RELEASE
:
900 case DHCPV6_MSG_DECLINE
:
906 case DHCPV6_STATUS_OK
:
909 case DHCPV6_STATUS_NOADDRSAVAIL
:
910 status
= "no addresses available";
912 case DHCPV6_STATUS_NOBINDING
:
913 status
= "no binding";
915 case DHCPV6_STATUS_NOTONLINK
:
916 status
= "not on-link";
918 case DHCPV6_STATUS_NOPREFIXAVAIL
:
919 status
= "no prefix available";
923 char leasebuf
[256] = "";
926 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
927 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
929 char addrbuf
[INET6_ADDRSTRLEN
];
931 for (size_t i
= 0; i
< addrlen
; ++i
) {
932 if (!valid_addr(&addrs
[i
], now
))
935 struct in6_addr addr
= addrs
[i
].addr
;
936 int prefix
= a
->managed
? addrs
[i
].prefix
: a
->length
;
938 addr
.s6_addr32
[3] = htonl(a
->assigned
);
940 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
942 inet_ntop(AF_INET6
, &addr
, addrbuf
, sizeof(addrbuf
));
943 lbsize
+= snprintf(leasebuf
+ lbsize
, sizeof(leasebuf
) - lbsize
, "%s/%d ", addrbuf
, prefix
);
947 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
948 duidbuf
, iface
->ifname
, status
, leasebuf
);
951 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
952 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
954 time_t now
= odhcpd_time();
955 size_t response_len
= 0;
956 const struct dhcpv6_client_header
*hdr
= data
;
957 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
958 uint16_t otype
, olen
;
959 /* Find and parse client-id and hostname */
960 bool accept_reconf
= false;
961 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
963 size_t hostname_len
= 0;
964 bool notonlink
= false;
967 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
968 if (otype
== DHCPV6_OPT_CLIENTID
) {
972 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
973 memcpy(mac
, &odata
[8], sizeof(mac
));
974 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
975 memcpy(mac
, &odata
[4], sizeof(mac
));
978 odhcpd_hexlify(duidbuf
, odata
, olen
);
979 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
980 uint8_t fqdn_buf
[256];
981 memcpy(fqdn_buf
, odata
, olen
);
982 fqdn_buf
[olen
++] = 0;
984 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
985 hostname_len
= strcspn(hostname
, ".");
986 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
)
987 accept_reconf
= true;
990 if (!clid_data
|| !clid_len
|| clid_len
> 130)
993 struct dhcpv6_assignment
*first
= NULL
;
994 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
995 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
996 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
997 bool ia_addr_present
= false;
998 if (!is_pd
&& !is_na
)
1001 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1002 size_t ia_response_len
= 0;
1003 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1004 uint32_t reqhint
= 0;
1006 /* Parse request hint for IA-PD */
1009 uint16_t stype
, slen
;
1010 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1011 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1014 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1017 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1018 if (reqlen
> 32 && reqlen
<= 64)
1019 reqhint
&= (1U << (64 - reqlen
)) - 1;
1027 uint16_t stype
, slen
;
1028 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1029 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1032 ia_addr_present
= true;
1036 /* Find assignment */
1037 struct dhcpv6_assignment
*c
, *a
= NULL
;
1038 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1039 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1040 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1041 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1042 (c
->iaid
== ia
->iaid
|| INFINITE_VALID(c
->valid_until
) || now
< c
->valid_until
) &&
1043 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1047 apply_lease(iface
, a
, false);
1048 memcpy(a
->clid_data
, clid_data
, clid_len
);
1049 a
->clid_len
= clid_len
;
1058 /* Generic message handling */
1059 uint16_t status
= DHCPV6_STATUS_OK
;
1060 if (a
&& a
->managed_size
< 0)
1063 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1064 bool assigned
= !!a
;
1066 if (!a
&& !iface
->no_dynamic_dhcp
) {
1067 /* Create new binding */
1068 a
= calloc(1, sizeof(*a
) + clid_len
);
1070 a
->clid_len
= clid_len
;
1074 a
->assigned
= reqhint
;
1075 /* Set valid time to current time indicating */
1076 /* assignment is not having infinite lifetime */
1077 a
->valid_until
= now
;
1080 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1082 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1083 memcpy(a
->clid_data
, clid_data
, clid_len
);
1086 while (!(assigned
= assign_pd(iface
, a
)) &&
1087 !a
->managed_size
&& ++a
->length
<= 64);
1089 assigned
= assign_na(iface
, a
);
1091 if (a
->managed_size
&& !assigned
)
1096 if (!assigned
|| iface
->ia_addr_len
== 0)
1097 /* Set error status */
1098 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1099 else if (assigned
&& !first
) {
1100 size_t handshake_len
= 4;
1102 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1106 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1107 struct dhcpv6_auth_reconfigure auth
= {
1108 htons(DHCPV6_OPT_AUTH
),
1109 htons(sizeof(auth
) - 4),
1111 {htonl(time(NULL
)), htonl(++serial
)},
1115 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1116 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1117 handshake_len
+= sizeof(auth
);
1120 buf
+= handshake_len
;
1121 buflen
-= handshake_len
;
1122 response_len
+= handshake_len
;
1127 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1129 /* Was only a solicitation: mark binding for removal */
1130 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1131 a
->flags
&= ~OAF_BOUND
;
1133 if (!(a
->flags
& OAF_STATIC
))
1134 a
->valid_until
= now
;
1135 } else if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1136 if (hostname_len
> 0) {
1137 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1139 memcpy(a
->hostname
, hostname
, hostname_len
);
1140 a
->hostname
[hostname_len
] = 0;
1143 a
->accept_reconf
= accept_reconf
;
1144 a
->flags
|= OAF_BOUND
;
1145 apply_lease(iface
, a
, true);
1146 } else if (!assigned
&& a
&& a
->managed_size
== 0)
1147 /* Cleanup failed assignment */
1148 free_dhcpv6_assignment(a
);
1150 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1151 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1152 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1153 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1154 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1155 status
= DHCPV6_STATUS_NOBINDING
;
1156 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1157 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1158 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1159 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1161 a
->flags
|= OAF_BOUND
;
1162 apply_lease(iface
, a
, true);
1164 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1165 if (!(a
->flags
& OAF_STATIC
))
1166 a
->valid_until
= now
- 1;
1168 a
->flags
&= ~OAF_BOUND
;
1169 apply_lease(iface
, a
, false);
1170 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1171 a
->flags
&= ~OAF_BOUND
;
1173 if (!(a
->flags
& OAF_STATIC
)) {
1175 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1178 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1179 /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1180 status
= DHCPV6_STATUS_NOTONLINK
;
1181 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1185 buf
+= ia_response_len
;
1186 buflen
-= ia_response_len
;
1187 response_len
+= ia_response_len
;
1188 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1191 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1192 response_len
+ 6 < buflen
) {
1194 buf
[1] = DHCPV6_OPT_STATUS
;
1198 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1202 dhcpv6_write_statefile();
1205 return response_len
;