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_addr(const struct odhcpd_ipaddr
*addr
, time_t now
)
151 return (addr
->prefix
<= 96 && addr
->preferred
> (uint32_t)now
);
154 static size_t elect_addr(const struct odhcpd_ipaddr
*addrs
, const size_t addrlen
)
158 for (i
= 0, m
= 0; i
< addrlen
; ++i
) {
159 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
160 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
161 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
168 static int send_reconf(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
171 struct dhcpv6_client_header hdr
;
175 uint16_t hardware_type
;
180 struct dhcpv6_auth_reconfigure auth
;
183 uint8_t clid_data
[128];
184 } __attribute__((packed
)) reconf_msg
= {
185 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
186 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
187 .srvid_len
= htons(10),
188 .duid_type
= htons(3),
189 .hardware_type
= htons(1),
190 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
192 .msg_id
= DHCPV6_MSG_RENEW
,
193 .auth
= {htons(DHCPV6_OPT_AUTH
),
194 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
195 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
196 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
197 .clid_len
= htons(assign
->clid_len
),
201 odhcpd_get_mac(iface
, reconf_msg
.mac
);
202 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
203 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
206 uint8_t secretbytes
[64];
207 memset(secretbytes
, 0, sizeof(secretbytes
));
208 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
210 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
211 secretbytes
[i
] ^= 0x36;
214 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
215 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
216 md5_end(reconf_msg
.auth
.key
, &md5
);
218 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
219 secretbytes
[i
] ^= 0x36;
220 secretbytes
[i
] ^= 0x5c;
224 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
225 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
226 md5_end(reconf_msg
.auth
.key
, &md5
);
228 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
231 void dhcpv6_write_statefile(void)
236 if (config
.dhcp_statefile
) {
237 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
238 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
242 lockf(fd
, F_LOCK
, 0);
243 if (ftruncate(fd
, 0) < 0) {}
245 FILE *fp
= fdopen(fd
, "w");
251 struct interface
*iface
;
252 list_for_each_entry(iface
, &interfaces
, head
) {
253 if (iface
->dhcpv6
!= RELAYD_SERVER
&& iface
->dhcpv4
!= RELAYD_SERVER
)
256 if (iface
->dhcpv6
== RELAYD_SERVER
&& iface
->ia_assignments
.next
) {
257 struct dhcpv6_assignment
*c
;
258 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
259 if (!(c
->flags
& OAF_BOUND
) || c
->managed_size
< 0)
262 char ipbuf
[INET6_ADDRSTRLEN
];
265 odhcpd_hexlify(duidbuf
, c
->clid_data
, c
->clid_len
);
267 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
268 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s %x %s %ld %x %u ",
269 iface
->ifname
, duidbuf
, ntohl(c
->iaid
),
270 (c
->hostname
? c
->hostname
: "-"),
271 (c
->valid_until
> now
?
272 (c
->valid_until
- now
+ wall_time
) :
273 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
274 c
->assigned
, (unsigned)c
->length
);
276 struct in6_addr addr
;
277 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->ia_addr
;
278 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->ia_addr_len
;
279 size_t m
= elect_addr(addrs
, addrlen
);
281 for (size_t i
= 0; i
< addrlen
; ++i
) {
282 if (!valid_addr(&addrs
[i
], now
) ||
283 (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
<= now
) ||
284 !ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
))
287 addr
= addrs
[i
].addr
;
288 if (c
->length
== 128)
289 addr
.s6_addr32
[3] = htonl(c
->assigned
);
291 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
293 inet_ntop(AF_INET6
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
295 if (c
->length
== 128 && c
->hostname
) {
299 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
300 iface
->search
, b
, sizeof(b
)) > 0)
301 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
303 fprintf(fp
, "\t%s\n", c
->hostname
);
304 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
305 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
308 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/%d ", ipbuf
,
309 (c
->managed_size
) ? addrs
[i
].prefix
: c
->length
);
311 leasebuf
[l
- 1] = '\n';
312 fwrite(leasebuf
, 1, l
, fp
);
316 if (iface
->dhcpv4
== RELAYD_SERVER
&& iface
->dhcpv4_assignments
.next
) {
317 struct dhcpv4_assignment
*c
;
318 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
319 if (!(c
->flags
& OAF_BOUND
))
322 char ipbuf
[INET6_ADDRSTRLEN
];
325 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
327 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
328 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s ipv4 %s %ld %x 32 ",
329 iface
->ifname
, duidbuf
,
330 (c
->hostname
? c
->hostname
: "-"),
331 (c
->valid_until
> now
?
332 (c
->valid_until
- now
+ wall_time
) :
333 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
336 struct in_addr addr
= {htonl(c
->addr
)};
337 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
343 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
344 iface
->search
, b
, sizeof(b
)) > 0)
345 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
347 fprintf(fp
, "\t%s\n", c
->hostname
);
348 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
349 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
352 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/32 ", ipbuf
);
353 leasebuf
[l
- 1] = '\n';
354 fwrite(leasebuf
, 1, l
, fp
);
363 md5_end(newmd5
, &md5
);
365 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
366 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
367 char *argv
[2] = {config
.dhcp_cb
, NULL
};
369 execv(argv
[0], argv
);
376 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
378 if (a
->length
> 64 || a
->managed_size
< 0)
381 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
382 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
384 for (size_t i
= 0; i
< addrlen
; ++i
) {
385 struct in6_addr prefix
= addrs
[i
].addr
;
386 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
387 odhcpd_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
388 iface
, &a
->peer
.sin6_addr
, 1024, add
);
392 /* More data was received from TCP connection */
393 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
395 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
396 time_t now
= odhcpd_time();
397 bool first
= c
->managed_size
< 0;
401 char *data
= ustream_get_read_buf(s
, &pending
);
402 char *end
= memmem(data
, pending
, "\n\n", 2);
411 if (c
->accept_reconf
)
415 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
416 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
417 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
419 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
420 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
423 x
= strtok_r(NULL
, ",", &saveptr2
);
424 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
427 x
= strtok_r(NULL
, ",", &saveptr2
);
428 if (sscanf(x
, "%u", &n
->preferred
) < 1)
431 x
= strtok_r(NULL
, ",", &saveptr2
);
432 if (sscanf(x
, "%u", &n
->valid
) < 1)
435 if (n
->preferred
> n
->valid
)
438 if (UINT32_MAX
- now
< n
->preferred
)
439 n
->preferred
= UINT32_MAX
;
443 if (UINT32_MAX
- now
< n
->valid
)
444 n
->valid
= UINT32_MAX
;
453 ustream_consume(s
, end
- data
);
456 if (first
&& c
->managed_size
== 0)
457 free_dhcpv6_assignment(c
);
458 else if (first
&& !(c
->flags
& OAF_STATIC
))
459 c
->valid_until
= now
+ 150;
463 /* TCP transmission has ended, either because of success or timeout or other error */
464 static void managed_handle_pd_done(struct ustream
*s
)
466 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
468 if (!(c
->flags
& OAF_STATIC
))
469 c
->valid_until
= odhcpd_time() + 15;
473 if (c
->accept_reconf
)
477 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
479 struct dhcpv6_assignment
*c
;
481 if (iface
->dhcpv6_pd_manager
[0]) {
482 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
485 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
487 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
488 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
489 ustream_fd_init(&assign
->managed_sock
, fd
);
490 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
491 iaidbuf
, assign
->iaid
, assign
->length
);
492 ustream_write_pending(&assign
->managed_sock
.stream
);
493 assign
->managed_size
= -1;
495 if (!(assign
->flags
& OAF_STATIC
))
496 assign
->valid_until
= odhcpd_time() + 15;
498 list_add(&assign
->head
, &iface
->ia_assignments
);
500 /* Wait initial period of up to 250ms for immediate assignment */
501 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
503 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
505 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
510 } else if (iface
->ia_addr_len
< 1)
513 /* Try honoring the hint first */
514 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
515 if (assign
->assigned
) {
516 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
517 if (c
->length
== 128 || c
->length
== 0)
520 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
521 list_add_tail(&assign
->head
, &c
->head
);
522 apply_lease(iface
, assign
, true);
526 if (c
->assigned
!= 0)
527 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
531 /* Fallback to a variable assignment */
533 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
534 if (c
->length
== 128 || c
->length
== 0)
537 current
= (current
+ asize
) & (~asize
);
538 if (current
+ asize
< c
->assigned
) {
539 assign
->assigned
= current
;
540 list_add_tail(&assign
->head
, &c
->head
);
541 apply_lease(iface
, assign
, true);
545 if (c
->assigned
!= 0)
546 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
552 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
554 /* Seed RNG with checksum of DUID */
556 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
557 seed
+= assign
->clid_data
[i
];
560 /* Try to assign up to 100x */
561 for (size_t i
= 0; i
< 100; ++i
) {
563 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
565 struct dhcpv6_assignment
*c
;
566 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
570 if (c
->assigned
> try || c
->length
!= 128) {
571 assign
->assigned
= try;
572 list_add_tail(&assign
->head
, &c
->head
);
574 } else if (c
->assigned
== try)
582 void dhcpv6_ia_preupdate(struct interface
*iface
)
584 if (iface
->dhcpv6
!= RELAYD_SERVER
)
587 struct dhcpv6_assignment
*c
, *border
= list_last_entry(
588 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
590 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
591 if (c
!= border
&& !iface
->managed
)
592 apply_lease(iface
, c
, false);
595 void dhcpv6_ia_postupdate(struct interface
*iface
, time_t now
)
597 if (iface
->dhcpv6
!= RELAYD_SERVER
)
601 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
602 if (iface
->ia_addr
[i
].preferred
> (uint32_t)now
&&
603 iface
->ia_addr
[i
].prefix
< 64 &&
604 iface
->ia_addr
[i
].prefix
> minprefix
)
605 minprefix
= iface
->ia_addr
[i
].prefix
;
608 struct dhcpv6_assignment
*border
= list_last_entry(
609 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
611 if (minprefix
> 32 && minprefix
<= 64)
612 border
->assigned
= 1U << (64 - minprefix
);
614 border
->assigned
= 0;
616 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
617 struct dhcpv6_assignment
*c
, *d
;
618 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
619 if (c
->clid_len
== 0 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
623 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
624 list_move(&c
->head
, &reassign
);
625 else if (c
!= border
)
626 apply_lease(iface
, c
, true);
628 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
630 c
->reconf_sent
= now
;
631 send_reconf(iface
, c
);
633 /* Leave all other assignments of that client alone */
634 struct dhcpv6_assignment
*a
;
635 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
636 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
637 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
638 c
->reconf_cnt
= INT_MAX
;
642 while (!list_empty(&reassign
)) {
643 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
645 if (!assign_pd(iface
, c
)) {
647 list_add(&c
->head
, &iface
->ia_assignments
);
651 dhcpv6_write_statefile();
654 static void reconf_timer(struct uloop_timeout
*event
)
656 time_t now
= odhcpd_time();
657 struct interface
*iface
;
658 list_for_each_entry(iface
, &interfaces
, head
) {
659 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
662 struct dhcpv6_assignment
*a
, *n
;
663 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
664 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
) {
665 if ((a
->length
< 128 && a
->clid_len
> 0) ||
666 (a
->length
== 128 && a
->clid_len
== 0))
667 free_dhcpv6_assignment(a
);
669 } else if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< 8 &&
670 now
> a
->reconf_sent
+ (1 << a
->reconf_cnt
)) {
672 a
->reconf_sent
= now
;
673 send_reconf(iface
, a
);
677 uloop_timeout_set(event
, 2000);
680 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
681 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
682 struct interface
*iface
, bool request
)
684 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
687 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
688 size_t datalen
= sizeof(out
);
689 time_t now
= odhcpd_time();
692 struct __attribute__((packed
)) {
696 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
699 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
700 datalen
+= sizeof(stat
);
704 if (a
->leasetime
> 0)
705 leasetime
= a
->leasetime
;
707 leasetime
= iface
->dhcpv4_leasetime
;
711 else if (leasetime
< 60)
714 uint32_t pref
= leasetime
;
715 uint32_t valid
= leasetime
;
717 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
718 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
719 size_t m
= elect_addr(addrs
, addrlen
);
721 for (size_t i
= 0; i
< addrlen
; ++i
) {
722 uint32_t prefix_pref
= addrs
[i
].preferred
;
723 uint32_t prefix_valid
= addrs
[i
].valid
;
725 if (!valid_addr(&addrs
[i
], now
))
728 if (prefix_pref
!= UINT32_MAX
)
731 if (prefix_valid
!= UINT32_MAX
)
734 if (a
->length
< 128) {
735 struct dhcpv6_ia_prefix p
= {
736 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
737 .len
= htons(sizeof(p
) - 4),
738 .preferred
= htonl(prefix_pref
),
739 .valid
= htonl(prefix_valid
),
740 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
741 .addr
= addrs
[i
].addr
743 p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
745 size_t entrlen
= sizeof(p
) - 4;
747 if (datalen
+ entrlen
+ 4 > buflen
||
748 (a
->assigned
== 0 && a
->managed_size
== 0) ||
749 (!a
->managed_size
&& a
->length
<= addrs
[i
].prefix
))
752 memcpy(buf
+ datalen
, &p
, sizeof(p
));
753 datalen
+= entrlen
+ 4;
755 struct dhcpv6_ia_addr n
= {
756 .type
= htons(DHCPV6_OPT_IA_ADDR
),
757 .len
= htons(sizeof(n
) - 4),
758 .addr
= addrs
[i
].addr
,
759 .preferred
= htonl(prefix_pref
),
760 .valid
= htonl(prefix_valid
)
762 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
763 size_t entrlen
= sizeof(n
) - 4;
765 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
) ||
767 datalen
+ entrlen
+ 4 > buflen
)
770 memcpy(buf
+ datalen
, &n
, sizeof(n
));
771 datalen
+= entrlen
+ 4;
774 /* Calculate T1 / T2 based on non-deprecated addresses */
775 if (prefix_pref
> 0) {
776 if (prefix_pref
< pref
)
779 if (prefix_valid
< valid
)
780 valid
= prefix_valid
;
784 if (!INFINITE_VALID(a
->valid_until
))
785 /* UINT32_MAX is considered as infinite leasetime */
786 a
->valid_until
= (valid
== UINT32_MAX
) ? 0 : valid
+ now
;
788 out
.t1
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 5 / 10);
789 out
.t2
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 8 / 10);
799 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
800 uint16_t otype
, olen
;
801 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
802 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
803 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
804 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
805 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
810 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
811 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
813 for (size_t i
= 0; i
< addrlen
; ++i
) {
814 if (!valid_addr(&addrs
[i
], now
))
817 struct in6_addr addr
= addrs
[i
].addr
;
818 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
819 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
821 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
822 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
825 addr
.s6_addr32
[3] = htonl(a
->assigned
);
827 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
834 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
835 struct dhcpv6_ia_prefix inv
= {
836 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
837 .len
= htons(sizeof(inv
) - 4),
844 if (datalen
+ sizeof(inv
) > buflen
)
847 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
848 datalen
+= sizeof(inv
);
850 struct dhcpv6_ia_addr inv
= {
851 .type
= htons(DHCPV6_OPT_IA_ADDR
),
852 .len
= htons(sizeof(inv
) - 4),
858 if (datalen
+ sizeof(inv
) > buflen
)
861 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
862 datalen
+= sizeof(inv
);
869 out
.len
= htons(datalen
- 4);
870 memcpy(buf
, &out
, sizeof(out
));
874 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
875 const char *duidbuf
, bool is_pd
, struct dhcpv6_assignment
*a
, int code
)
877 const char *type
= "UNKNOWN";
878 const char *status
= "UNKNOWN";
880 if (msgtype
== DHCPV6_MSG_RENEW
)
884 case DHCPV6_MSG_SOLICIT
:
887 case DHCPV6_MSG_REQUEST
:
890 case DHCPV6_MSG_CONFIRM
:
893 case DHCPV6_MSG_RENEW
:
896 case DHCPV6_MSG_REBIND
:
899 case DHCPV6_MSG_RELEASE
:
902 case DHCPV6_MSG_DECLINE
:
908 case DHCPV6_STATUS_OK
:
911 case DHCPV6_STATUS_NOADDRSAVAIL
:
912 status
= "no addresses available";
914 case DHCPV6_STATUS_NOBINDING
:
915 status
= "no binding";
917 case DHCPV6_STATUS_NOTONLINK
:
918 status
= "not on-link";
920 case DHCPV6_STATUS_NOPREFIXAVAIL
:
921 status
= "no prefix available";
925 char leasebuf
[256] = "";
928 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
929 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
931 size_t m
= elect_addr(addrs
, addrlen
);
932 char addrbuf
[INET6_ADDRSTRLEN
];
934 for (size_t i
= 0; i
< addrlen
; ++i
) {
935 if (!valid_addr(&addrs
[i
], now
))
938 struct in6_addr addr
= addrs
[i
].addr
;
939 int prefix
= a
->managed
? addrs
[i
].prefix
: a
->length
;
941 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
) ||
945 addr
.s6_addr32
[3] = htonl(a
->assigned
);
948 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
950 inet_ntop(AF_INET6
, &addr
, addrbuf
, sizeof(addrbuf
));
951 lbsize
+= snprintf(leasebuf
+ lbsize
, sizeof(leasebuf
) - lbsize
, "%s/%d ", addrbuf
, prefix
);
955 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
956 duidbuf
, iface
->ifname
, status
, leasebuf
);
959 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
960 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
962 time_t now
= odhcpd_time();
963 size_t response_len
= 0;
964 const struct dhcpv6_client_header
*hdr
= data
;
965 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
966 uint16_t otype
, olen
;
967 /* Find and parse client-id and hostname */
968 bool accept_reconf
= false;
969 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
971 size_t hostname_len
= 0;
972 bool notonlink
= false;
975 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
976 if (otype
== DHCPV6_OPT_CLIENTID
) {
980 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
981 memcpy(mac
, &odata
[8], sizeof(mac
));
982 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
983 memcpy(mac
, &odata
[4], sizeof(mac
));
986 odhcpd_hexlify(duidbuf
, odata
, olen
);
987 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
988 uint8_t fqdn_buf
[256];
989 memcpy(fqdn_buf
, odata
, olen
);
990 fqdn_buf
[olen
++] = 0;
992 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
993 hostname_len
= strcspn(hostname
, ".");
994 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
)
995 accept_reconf
= true;
998 if (!clid_data
|| !clid_len
|| clid_len
> 130)
1001 struct dhcpv6_assignment
*first
= NULL
;
1002 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1003 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
1004 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
1005 bool ia_addr_present
= false;
1006 if (!is_pd
&& !is_na
)
1009 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1010 size_t ia_response_len
= 0;
1011 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1012 uint32_t reqhint
= 0;
1014 /* Parse request hint for IA-PD */
1017 uint16_t stype
, slen
;
1018 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1019 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1022 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1025 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1026 if (reqlen
> 32 && reqlen
<= 64)
1027 reqhint
&= (1U << (64 - reqlen
)) - 1;
1035 uint16_t stype
, slen
;
1036 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1037 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1040 ia_addr_present
= true;
1044 /* Find assignment */
1045 struct dhcpv6_assignment
*c
, *a
= NULL
;
1046 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1047 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1048 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1049 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1050 (c
->iaid
== ia
->iaid
|| INFINITE_VALID(c
->valid_until
) || now
< c
->valid_until
) &&
1051 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1055 apply_lease(iface
, a
, false);
1056 memcpy(a
->clid_data
, clid_data
, clid_len
);
1057 a
->clid_len
= clid_len
;
1066 /* Generic message handling */
1067 uint16_t status
= DHCPV6_STATUS_OK
;
1068 if (a
&& a
->managed_size
< 0)
1071 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1072 bool assigned
= !!a
;
1074 if (!a
&& !iface
->no_dynamic_dhcp
) {
1075 /* Create new binding */
1076 a
= calloc(1, sizeof(*a
) + clid_len
);
1078 a
->clid_len
= clid_len
;
1082 a
->assigned
= reqhint
;
1083 /* Set valid time to current time indicating */
1084 /* assignment is not having infinite lifetime */
1085 a
->valid_until
= now
;
1088 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1090 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1091 memcpy(a
->clid_data
, clid_data
, clid_len
);
1094 while (!(assigned
= assign_pd(iface
, a
)) &&
1095 !a
->managed_size
&& ++a
->length
<= 64);
1097 assigned
= assign_na(iface
, a
);
1099 if (a
->managed_size
&& !assigned
)
1104 if (!assigned
|| iface
->ia_addr_len
== 0)
1105 /* Set error status */
1106 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1107 else if (assigned
&& !first
) {
1108 size_t handshake_len
= 4;
1110 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1114 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1115 struct dhcpv6_auth_reconfigure auth
= {
1116 htons(DHCPV6_OPT_AUTH
),
1117 htons(sizeof(auth
) - 4),
1119 {htonl(time(NULL
)), htonl(++serial
)},
1123 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1124 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1125 handshake_len
+= sizeof(auth
);
1128 buf
+= handshake_len
;
1129 buflen
-= handshake_len
;
1130 response_len
+= handshake_len
;
1135 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1137 /* Was only a solicitation: mark binding for removal */
1138 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1139 a
->flags
&= ~OAF_BOUND
;
1141 if (!(a
->flags
& OAF_STATIC
))
1142 a
->valid_until
= now
;
1143 } else if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1144 if (hostname_len
> 0) {
1145 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1147 memcpy(a
->hostname
, hostname
, hostname_len
);
1148 a
->hostname
[hostname_len
] = 0;
1151 a
->accept_reconf
= accept_reconf
;
1152 a
->flags
|= OAF_BOUND
;
1153 apply_lease(iface
, a
, true);
1154 } else if (!assigned
&& a
&& a
->managed_size
== 0) {
1155 /* Cleanup failed assignment */
1156 free_dhcpv6_assignment(a
);
1159 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1160 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1161 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1162 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1163 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1164 status
= DHCPV6_STATUS_NOBINDING
;
1165 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1166 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1167 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1168 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1170 a
->flags
|= OAF_BOUND
;
1171 apply_lease(iface
, a
, true);
1173 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1174 if (!(a
->flags
& OAF_STATIC
))
1175 a
->valid_until
= now
- 1;
1177 a
->flags
&= ~OAF_BOUND
;
1178 apply_lease(iface
, a
, false);
1179 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1180 a
->flags
&= ~OAF_BOUND
;
1182 if (!(a
->flags
& OAF_STATIC
)) {
1184 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1187 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1188 /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1189 status
= DHCPV6_STATUS_NOTONLINK
;
1190 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1194 buf
+= ia_response_len
;
1195 buflen
-= ia_response_len
;
1196 response_len
+= ia_response_len
;
1197 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1200 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1201 response_len
+ 6 < buflen
) {
1203 buf
[1] = DHCPV6_OPT_STATUS
;
1207 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1211 dhcpv6_write_statefile();
1214 return response_len
;