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
;
53 while (!list_empty(&iface
->ia_assignments
)) {
54 c
= list_first_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
55 free_dhcpv6_assignment(c
);
59 if (enable
&& iface
->dhcpv6
== RELAYD_SERVER
) {
60 if (!iface
->ia_assignments
.next
)
61 INIT_LIST_HEAD(&iface
->ia_assignments
);
63 if (list_empty(&iface
->ia_assignments
)) {
64 struct dhcpv6_assignment
*border
= calloc(1, sizeof(*border
));
66 syslog(LOG_ERR
, "Calloc failed for border on interface %s", iface
->ifname
);
71 list_add(&border
->head
, &iface
->ia_assignments
);
74 /* Parse static entries */
76 list_for_each_entry(lease
, &leases
, head
) {
78 size_t duid_len
= lease
->duid_len
? lease
->duid_len
: 14;
79 struct dhcpv6_assignment
*a
= calloc(1, sizeof(*a
) + duid_len
);
81 syslog(LOG_ERR
, "Calloc failed for static lease assignment on interface %s",
86 if (lease
->dhcpv4_leasetime
> 0)
87 a
->leasetime
= lease
->dhcpv4_leasetime
;
89 a
->clid_len
= duid_len
;
92 a
->assigned
= lease
->hostid
;
94 uint32_t i4a
= ntohl(lease
->ipaddr
.s_addr
) & 0xff;
95 a
->assigned
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
98 odhcpd_urandom(a
->key
, sizeof(a
->key
));
99 memcpy(a
->clid_data
, lease
->duid
, lease
->duid_len
);
100 memcpy(a
->mac
, lease
->mac
.ether_addr_octet
, sizeof(a
->mac
));
101 /* Static assignment */
102 a
->flags
|= OAF_STATIC
;
106 // Assign to all interfaces
107 struct dhcpv6_assignment
*c
;
108 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
109 if (c
->length
!= 128 || c
->assigned
> a
->assigned
) {
110 list_add_tail(&a
->head
, &c
->head
);
112 } else if (c
->assigned
== a
->assigned
)
113 /* Already an assignment with that number */
118 if (lease
->hostname
[0]) {
120 a
->hostname
= strdup(lease
->hostname
);
123 free_dhcpv6_assignment(a
);
129 static void free_dhcpv6_assignment(struct dhcpv6_assignment
*c
)
131 if (c
->managed_sock
.fd
.registered
) {
132 ustream_free(&c
->managed_sock
.stream
);
133 close(c
->managed_sock
.fd
.fd
);
144 static inline bool valid_addr(const struct odhcpd_ipaddr
*addr
, time_t now
)
146 return (addr
->prefix
<= 96 && addr
->preferred
> (uint32_t)now
);
149 static size_t elect_addr(const struct odhcpd_ipaddr
*addrs
, const size_t addrlen
)
153 for (i
= 0, m
= 0; i
< addrlen
; ++i
) {
154 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
155 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
156 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
163 static int send_reconf(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
166 struct dhcpv6_client_header hdr
;
170 uint16_t hardware_type
;
175 struct dhcpv6_auth_reconfigure auth
;
178 uint8_t clid_data
[128];
179 } __attribute__((packed
)) reconf_msg
= {
180 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
181 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
182 .srvid_len
= htons(10),
183 .duid_type
= htons(3),
184 .hardware_type
= htons(1),
185 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
187 .msg_id
= DHCPV6_MSG_RENEW
,
188 .auth
= {htons(DHCPV6_OPT_AUTH
),
189 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
190 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
191 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
192 .clid_len
= htons(assign
->clid_len
),
196 odhcpd_get_mac(iface
, reconf_msg
.mac
);
197 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
198 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
201 uint8_t secretbytes
[64];
202 memset(secretbytes
, 0, sizeof(secretbytes
));
203 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
205 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
206 secretbytes
[i
] ^= 0x36;
209 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
210 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
211 md5_end(reconf_msg
.auth
.key
, &md5
);
213 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
214 secretbytes
[i
] ^= 0x36;
215 secretbytes
[i
] ^= 0x5c;
219 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
220 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
221 md5_end(reconf_msg
.auth
.key
, &md5
);
223 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
226 void dhcpv6_write_statefile(void)
231 if (config
.dhcp_statefile
) {
232 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
233 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
237 lockf(fd
, F_LOCK
, 0);
238 if (ftruncate(fd
, 0) < 0) {}
240 FILE *fp
= fdopen(fd
, "w");
246 struct interface
*iface
;
247 list_for_each_entry(iface
, &interfaces
, head
) {
248 if (iface
->dhcpv6
!= RELAYD_SERVER
&& iface
->dhcpv4
!= RELAYD_SERVER
)
251 if (iface
->dhcpv6
== RELAYD_SERVER
&& iface
->ia_assignments
.next
) {
252 struct dhcpv6_assignment
*c
;
253 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
254 if (!(c
->flags
& OAF_BOUND
) || c
->managed_size
< 0)
257 char ipbuf
[INET6_ADDRSTRLEN
];
260 odhcpd_hexlify(duidbuf
, c
->clid_data
, c
->clid_len
);
262 // iface DUID iaid hostname lifetime assigned length [addrs...]
263 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s %x %s %ld %x %u ",
264 iface
->ifname
, duidbuf
, ntohl(c
->iaid
),
265 (c
->hostname
? c
->hostname
: "-"),
266 (c
->valid_until
> now
?
267 (c
->valid_until
- now
+ wall_time
) :
268 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
269 c
->assigned
, (unsigned)c
->length
);
271 struct in6_addr addr
;
272 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->ia_addr
;
273 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->ia_addr_len
;
274 size_t m
= elect_addr(addrs
, addrlen
);
276 for (size_t i
= 0; i
< addrlen
; ++i
) {
277 if (!valid_addr(&addrs
[i
], now
) || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
<= now
) ||
278 (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
279 addrs
[i
].prefix
<= 64))
282 addr
= addrs
[i
].addr
;
283 if (c
->length
== 128)
284 addr
.s6_addr32
[3] = htonl(c
->assigned
);
286 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
288 inet_ntop(AF_INET6
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
290 if (c
->length
== 128 && c
->hostname
) {
294 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
295 iface
->search
, b
, sizeof(b
)) > 0)
296 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
298 fprintf(fp
, "\t%s\n", c
->hostname
);
299 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
300 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
303 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/%d ", ipbuf
,
304 (c
->managed_size
) ? addrs
[i
].prefix
: c
->length
);
306 leasebuf
[l
- 1] = '\n';
307 fwrite(leasebuf
, 1, l
, fp
);
311 if (iface
->dhcpv4
== RELAYD_SERVER
&& iface
->dhcpv4_assignments
.next
) {
312 struct dhcpv4_assignment
*c
;
313 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
314 if (!(c
->flags
& OAF_BOUND
))
317 char ipbuf
[INET6_ADDRSTRLEN
];
320 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
322 // iface DUID iaid hostname lifetime assigned length [addrs...]
323 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s ipv4 %s %ld %x 32 ",
324 iface
->ifname
, duidbuf
,
325 (c
->hostname
? c
->hostname
: "-"),
326 (c
->valid_until
> now
?
327 (c
->valid_until
- now
+ wall_time
) :
328 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
331 struct in_addr addr
= {htonl(c
->addr
)};
332 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
338 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
339 iface
->search
, b
, sizeof(b
)) > 0)
340 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
342 fprintf(fp
, "\t%s\n", c
->hostname
);
343 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
344 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
347 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/32 ", ipbuf
);
348 leasebuf
[l
- 1] = '\n';
349 fwrite(leasebuf
, 1, l
, fp
);
358 md5_end(newmd5
, &md5
);
360 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
361 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
362 char *argv
[2] = {config
.dhcp_cb
, NULL
};
364 execv(argv
[0], argv
);
371 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
373 if (a
->length
> 64 || a
->managed_size
< 0)
376 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
377 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
379 for (size_t i
= 0; i
< addrlen
; ++i
) {
380 struct in6_addr prefix
= addrs
[i
].addr
;
381 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
382 odhcpd_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
383 iface
, &a
->peer
.sin6_addr
, 1024, add
);
387 // More data was received from TCP connection
388 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
390 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
391 time_t now
= odhcpd_time();
392 bool first
= c
->managed_size
< 0;
396 char *data
= ustream_get_read_buf(s
, &pending
);
397 char *end
= memmem(data
, pending
, "\n\n", 2);
406 if (c
->accept_reconf
)
410 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
411 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
412 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
414 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
415 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
418 x
= strtok_r(NULL
, ",", &saveptr2
);
419 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
422 x
= strtok_r(NULL
, ",", &saveptr2
);
423 if (sscanf(x
, "%u", &n
->preferred
) < 1)
426 x
= strtok_r(NULL
, ",", &saveptr2
);
427 if (sscanf(x
, "%u", &n
->valid
) < 1)
430 if (n
->preferred
> n
->valid
)
433 if (UINT32_MAX
- now
< n
->preferred
)
434 n
->preferred
= UINT32_MAX
;
438 if (UINT32_MAX
- now
< n
->valid
)
439 n
->valid
= UINT32_MAX
;
448 ustream_consume(s
, end
- data
);
451 if (first
&& c
->managed_size
== 0)
452 free_dhcpv6_assignment(c
);
454 c
->valid_until
= now
+ 150;
458 /* TCP transmission has ended, either because of success or timeout or other error */
459 static void managed_handle_pd_done(struct ustream
*s
)
461 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
463 c
->valid_until
= odhcpd_time() + 15;
466 if (c
->accept_reconf
)
470 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
472 struct dhcpv6_assignment
*c
;
474 if (iface
->dhcpv6_pd_manager
[0]) {
475 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
478 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
480 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
481 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
482 ustream_fd_init(&assign
->managed_sock
, fd
);
483 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
484 iaidbuf
, assign
->iaid
, assign
->length
);
485 ustream_write_pending(&assign
->managed_sock
.stream
);
486 assign
->managed_size
= -1;
487 assign
->valid_until
= odhcpd_time() + 15;
488 list_add(&assign
->head
, &iface
->ia_assignments
);
490 /* Wait initial period of up to 250ms for immediate assignment */
491 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
493 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
495 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
500 } else if (iface
->ia_addr_len
< 1)
503 /* Try honoring the hint first */
504 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
505 if (assign
->assigned
) {
506 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
507 if (c
->length
== 128 || c
->length
== 0)
510 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
511 list_add_tail(&assign
->head
, &c
->head
);
512 apply_lease(iface
, assign
, true);
516 if (c
->assigned
!= 0)
517 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
521 /* Fallback to a variable assignment */
523 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
524 if (c
->length
== 128 || c
->length
== 0)
527 current
= (current
+ asize
) & (~asize
);
528 if (current
+ asize
< c
->assigned
) {
529 assign
->assigned
= current
;
530 list_add_tail(&assign
->head
, &c
->head
);
531 apply_lease(iface
, assign
, true);
535 if (c
->assigned
!= 0)
536 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
542 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
544 /* Seed RNG with checksum of DUID */
546 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
547 seed
+= assign
->clid_data
[i
];
550 /* Try to assign up to 100x */
551 for (size_t i
= 0; i
< 100; ++i
) {
553 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
555 struct dhcpv6_assignment
*c
;
556 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
560 if (c
->assigned
> try || c
->length
!= 128) {
561 assign
->assigned
= try;
562 list_add_tail(&assign
->head
, &c
->head
);
564 } else if (c
->assigned
== try)
572 void dhcpv6_ia_preupdate(struct interface
*iface
)
574 if (iface
->dhcpv6
!= RELAYD_SERVER
)
577 struct dhcpv6_assignment
*c
, *border
= list_last_entry(
578 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
580 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
581 if (c
!= border
&& !iface
->managed
)
582 apply_lease(iface
, c
, false);
585 void dhcpv6_ia_postupdate(struct interface
*iface
, time_t now
)
587 if (iface
->dhcpv6
!= RELAYD_SERVER
)
591 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
592 if (iface
->ia_addr
[i
].preferred
> (uint32_t)now
&&
593 iface
->ia_addr
[i
].prefix
< 64 &&
594 iface
->ia_addr
[i
].prefix
> minprefix
)
595 minprefix
= iface
->ia_addr
[i
].prefix
;
598 struct dhcpv6_assignment
*border
= list_last_entry(
599 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
601 if (minprefix
> 32 && minprefix
<= 64)
602 border
->assigned
= 1U << (64 - minprefix
);
604 border
->assigned
= 0;
606 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
607 struct dhcpv6_assignment
*c
, *d
;
608 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
609 if (c
->clid_len
== 0 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
613 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
614 list_move(&c
->head
, &reassign
);
615 else if (c
!= border
)
616 apply_lease(iface
, c
, true);
618 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
620 c
->reconf_sent
= now
;
621 send_reconf(iface
, c
);
623 /* Leave all other assignments of that client alone */
624 struct dhcpv6_assignment
*a
;
625 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
626 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
627 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
628 c
->reconf_cnt
= INT_MAX
;
632 while (!list_empty(&reassign
)) {
633 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
635 if (!assign_pd(iface
, c
)) {
637 list_add(&c
->head
, &iface
->ia_assignments
);
641 dhcpv6_write_statefile();
644 static void reconf_timer(struct uloop_timeout
*event
)
646 time_t now
= odhcpd_time();
647 struct interface
*iface
;
648 list_for_each_entry(iface
, &interfaces
, head
) {
649 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
652 struct dhcpv6_assignment
*a
, *n
;
653 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
654 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
) {
655 if ((a
->length
< 128 && a
->clid_len
> 0) ||
656 (a
->length
== 128 && a
->clid_len
== 0))
657 free_dhcpv6_assignment(a
);
659 } else if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< 8 &&
660 now
> a
->reconf_sent
+ (1 << a
->reconf_cnt
)) {
662 a
->reconf_sent
= now
;
663 send_reconf(iface
, a
);
667 uloop_timeout_set(event
, 2000);
670 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
671 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
672 struct interface
*iface
, bool request
)
674 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
677 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
678 size_t datalen
= sizeof(out
);
679 time_t now
= odhcpd_time();
682 struct __attribute__((packed
)) {
686 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
689 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
690 datalen
+= sizeof(stat
);
694 if (a
->leasetime
> 0)
695 leasetime
= a
->leasetime
;
697 leasetime
= iface
->dhcpv4_leasetime
;
701 else if (leasetime
< 60)
704 uint32_t pref
= leasetime
;
705 uint32_t valid
= leasetime
;
707 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
708 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
709 size_t m
= elect_addr(addrs
, addrlen
);
711 for (size_t i
= 0; i
< addrlen
; ++i
) {
712 uint32_t prefix_pref
= addrs
[i
].preferred
;
713 uint32_t prefix_valid
= addrs
[i
].valid
;
715 if (!valid_addr(&addrs
[i
], now
))
718 if (prefix_pref
!= UINT32_MAX
)
721 if (prefix_valid
!= UINT32_MAX
)
724 if (a
->length
< 128) {
725 struct dhcpv6_ia_prefix p
= {
726 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
727 .len
= htons(sizeof(p
) - 4),
728 .preferred
= htonl(prefix_pref
),
729 .valid
= htonl(prefix_valid
),
730 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
731 .addr
= addrs
[i
].addr
733 p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
735 size_t entrlen
= sizeof(p
) - 4;
737 if (datalen
+ entrlen
+ 4 > buflen
||
738 (a
->assigned
== 0 && a
->managed_size
== 0) ||
739 (!a
->managed_size
&& a
->length
<= addrs
[i
].prefix
))
742 memcpy(buf
+ datalen
, &p
, sizeof(p
));
743 datalen
+= entrlen
+ 4;
745 struct dhcpv6_ia_addr n
= {
746 .type
= htons(DHCPV6_OPT_IA_ADDR
),
747 .len
= htons(sizeof(n
) - 4),
748 .addr
= addrs
[i
].addr
,
749 .preferred
= htonl(prefix_pref
),
750 .valid
= htonl(prefix_valid
)
752 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
753 size_t entrlen
= sizeof(n
) - 4;
755 if (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
756 addrs
[i
].prefix
<= 64)
759 if (datalen
+ entrlen
+ 4 > buflen
|| a
->assigned
== 0)
762 memcpy(buf
+ datalen
, &n
, sizeof(n
));
763 datalen
+= entrlen
+ 4;
766 /* Calculate T1 / T2 based on non-deprecated addresses */
767 if (prefix_pref
> 0) {
768 if (prefix_pref
< pref
)
771 if (prefix_valid
< valid
)
772 valid
= prefix_valid
;
776 if (!INFINITE_VALID(a
->valid_until
))
777 /* UINT32_MAX is considered as infinite leasetime */
778 a
->valid_until
= (valid
== UINT32_MAX
) ? 0 : valid
+ now
;
780 out
.t1
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 5 / 10);
781 out
.t2
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 8 / 10);
791 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
792 uint16_t otype
, olen
;
793 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
794 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
795 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
796 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
797 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
802 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
803 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
805 for (size_t i
= 0; i
< addrlen
; ++i
) {
806 if (!valid_addr(&addrs
[i
], now
))
809 struct in6_addr addr
= addrs
[i
].addr
;
810 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
811 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
813 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
814 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
817 addr
.s6_addr32
[3] = htonl(a
->assigned
);
819 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
826 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
827 struct dhcpv6_ia_prefix inv
= {
828 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
829 .len
= htons(sizeof(inv
) - 4),
836 if (datalen
+ sizeof(inv
) > buflen
)
839 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
840 datalen
+= sizeof(inv
);
842 struct dhcpv6_ia_addr inv
= {
843 .type
= htons(DHCPV6_OPT_IA_ADDR
),
844 .len
= htons(sizeof(inv
) - 4),
850 if (datalen
+ sizeof(inv
) > buflen
)
853 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
854 datalen
+= sizeof(inv
);
861 out
.len
= htons(datalen
- 4);
862 memcpy(buf
, &out
, sizeof(out
));
866 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
867 const char *duidbuf
, bool is_pd
, struct dhcpv6_assignment
*a
, int code
)
869 const char *type
= "UNKNOWN";
870 const char *status
= "UNKNOWN";
872 if (msgtype
== DHCPV6_MSG_RENEW
)
876 case DHCPV6_MSG_SOLICIT
:
879 case DHCPV6_MSG_REQUEST
:
882 case DHCPV6_MSG_CONFIRM
:
885 case DHCPV6_MSG_RENEW
:
888 case DHCPV6_MSG_REBIND
:
891 case DHCPV6_MSG_RELEASE
:
894 case DHCPV6_MSG_DECLINE
:
900 case DHCPV6_STATUS_OK
:
903 case DHCPV6_STATUS_NOADDRSAVAIL
:
904 status
= "no addresses available";
906 case DHCPV6_STATUS_NOBINDING
:
907 status
= "no binding";
909 case DHCPV6_STATUS_NOTONLINK
:
910 status
= "not on-link";
912 case DHCPV6_STATUS_NOPREFIXAVAIL
:
913 status
= "no prefix available";
917 char leasebuf
[256] = "";
920 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
921 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
923 char addrbuf
[INET6_ADDRSTRLEN
];
925 for (size_t i
= 0; i
< addrlen
; ++i
) {
926 if (!valid_addr(&addrs
[i
], now
))
929 struct in6_addr addr
= addrs
[i
].addr
;
930 int prefix
= a
->managed
? addrs
[i
].prefix
: a
->length
;
932 addr
.s6_addr32
[3] = htonl(a
->assigned
);
934 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
936 inet_ntop(AF_INET6
, &addr
, addrbuf
, sizeof(addrbuf
));
937 lbsize
+= snprintf(leasebuf
+ lbsize
, sizeof(leasebuf
) - lbsize
, "%s/%d ", addrbuf
, prefix
);
941 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
942 duidbuf
, iface
->ifname
, status
, leasebuf
);
945 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
946 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
948 time_t now
= odhcpd_time();
949 size_t response_len
= 0;
950 const struct dhcpv6_client_header
*hdr
= data
;
951 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
952 uint16_t otype
, olen
;
953 /* Find and parse client-id and hostname */
954 bool accept_reconf
= false;
955 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
957 size_t hostname_len
= 0;
958 bool notonlink
= false;
961 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
962 if (otype
== DHCPV6_OPT_CLIENTID
) {
966 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
967 memcpy(mac
, &odata
[8], sizeof(mac
));
968 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
969 memcpy(mac
, &odata
[4], sizeof(mac
));
972 odhcpd_hexlify(duidbuf
, odata
, olen
);
973 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
974 uint8_t fqdn_buf
[256];
975 memcpy(fqdn_buf
, odata
, olen
);
976 fqdn_buf
[olen
++] = 0;
978 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
979 hostname_len
= strcspn(hostname
, ".");
980 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
)
981 accept_reconf
= true;
984 if (!clid_data
|| !clid_len
|| clid_len
> 130)
987 struct dhcpv6_assignment
*first
= NULL
;
988 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
989 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
990 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
991 bool ia_addr_present
= false;
992 if (!is_pd
&& !is_na
)
995 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
996 size_t ia_response_len
= 0;
997 uint8_t reqlen
= (is_pd
) ? 62 : 128;
998 uint32_t reqhint
= 0;
1000 /* Parse request hint for IA-PD */
1003 uint16_t stype
, slen
;
1004 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1005 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1008 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1011 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1012 if (reqlen
> 32 && reqlen
<= 64)
1013 reqhint
&= (1U << (64 - reqlen
)) - 1;
1021 uint16_t stype
, slen
;
1022 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1023 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1026 ia_addr_present
= true;
1030 /* Find assignment */
1031 struct dhcpv6_assignment
*c
, *a
= NULL
;
1032 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1033 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1034 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1035 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1036 (c
->iaid
== ia
->iaid
|| INFINITE_VALID(c
->valid_until
) || now
< c
->valid_until
) &&
1037 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1041 apply_lease(iface
, a
, false);
1042 memcpy(a
->clid_data
, clid_data
, clid_len
);
1043 a
->clid_len
= clid_len
;
1052 /* Generic message handling */
1053 uint16_t status
= DHCPV6_STATUS_OK
;
1054 if (a
&& a
->managed_size
< 0)
1057 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1058 bool assigned
= !!a
;
1060 if (!a
&& !iface
->no_dynamic_dhcp
) {
1061 /* Create new binding */
1062 a
= calloc(1, sizeof(*a
) + clid_len
);
1064 a
->clid_len
= clid_len
;
1068 a
->assigned
= reqhint
;
1069 /* Set valid time to current time indicating */
1070 /* assignment is not having infinite lifetime */
1071 a
->valid_until
= now
;
1074 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1076 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1077 memcpy(a
->clid_data
, clid_data
, clid_len
);
1080 while (!(assigned
= assign_pd(iface
, a
)) &&
1081 !a
->managed_size
&& ++a
->length
<= 64);
1083 assigned
= assign_na(iface
, a
);
1085 if (a
->managed_size
&& !assigned
)
1090 if (!assigned
|| iface
->ia_addr_len
== 0)
1091 /* Set error status */
1092 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1093 else if (assigned
&& !first
) {
1094 size_t handshake_len
= 4;
1096 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1100 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1101 struct dhcpv6_auth_reconfigure auth
= {
1102 htons(DHCPV6_OPT_AUTH
),
1103 htons(sizeof(auth
) - 4),
1105 {htonl(time(NULL
)), htonl(++serial
)},
1109 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1110 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1111 handshake_len
+= sizeof(auth
);
1114 buf
+= handshake_len
;
1115 buflen
-= handshake_len
;
1116 response_len
+= handshake_len
;
1121 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1123 /* Was only a solicitation: mark binding for removal */
1124 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1125 a
->flags
&= ~OAF_BOUND
;
1127 if (!(a
->flags
& OAF_STATIC
))
1128 a
->valid_until
= now
;
1129 } else if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1130 if (hostname_len
> 0) {
1131 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1133 memcpy(a
->hostname
, hostname
, hostname_len
);
1134 a
->hostname
[hostname_len
] = 0;
1137 a
->accept_reconf
= accept_reconf
;
1138 a
->flags
|= OAF_BOUND
;
1139 apply_lease(iface
, a
, true);
1140 } else if (!assigned
&& a
&& a
->managed_size
== 0)
1141 /* Cleanup failed assignment */
1142 free_dhcpv6_assignment(a
);
1144 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1145 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1146 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1147 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1148 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1149 status
= DHCPV6_STATUS_NOBINDING
;
1150 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1151 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1152 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1153 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1155 a
->flags
|= OAF_BOUND
;
1156 apply_lease(iface
, a
, true);
1158 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1159 if (!(a
->flags
& OAF_STATIC
))
1160 a
->valid_until
= now
- 1;
1162 a
->flags
&= ~OAF_BOUND
;
1163 apply_lease(iface
, a
, false);
1164 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1165 a
->flags
&= ~OAF_BOUND
;
1167 if (!(a
->flags
& OAF_STATIC
)) {
1169 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1172 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1173 /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1174 status
= DHCPV6_STATUS_NOTONLINK
;
1175 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1179 buf
+= ia_response_len
;
1180 buflen
-= ia_response_len
;
1181 response_len
+= ia_response_len
;
1182 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1185 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1186 response_len
+ 6 < buflen
) {
1188 buf
[1] = DHCPV6_OPT_STATUS
;
1192 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1196 dhcpv6_write_statefile();
1199 return response_len
;