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 int send_reconf(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
147 struct dhcpv6_client_header hdr
;
151 uint16_t hardware_type
;
156 struct dhcpv6_auth_reconfigure auth
;
159 uint8_t clid_data
[128];
160 } __attribute__((packed
)) reconf_msg
= {
161 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
162 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
163 .srvid_len
= htons(10),
164 .duid_type
= htons(3),
165 .hardware_type
= htons(1),
166 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
168 .msg_id
= DHCPV6_MSG_RENEW
,
169 .auth
= {htons(DHCPV6_OPT_AUTH
),
170 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
171 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
172 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
173 .clid_len
= htons(assign
->clid_len
),
177 odhcpd_get_mac(iface
, reconf_msg
.mac
);
178 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
179 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
182 uint8_t secretbytes
[64];
183 memset(secretbytes
, 0, sizeof(secretbytes
));
184 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
186 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
187 secretbytes
[i
] ^= 0x36;
190 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
191 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
192 md5_end(reconf_msg
.auth
.key
, &md5
);
194 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
195 secretbytes
[i
] ^= 0x36;
196 secretbytes
[i
] ^= 0x5c;
200 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
201 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
202 md5_end(reconf_msg
.auth
.key
, &md5
);
204 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
207 void dhcpv6_write_statefile(void)
212 if (config
.dhcp_statefile
) {
213 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
214 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
218 lockf(fd
, F_LOCK
, 0);
219 if (ftruncate(fd
, 0) < 0) {}
221 FILE *fp
= fdopen(fd
, "w");
227 struct interface
*iface
;
228 list_for_each_entry(iface
, &interfaces
, head
) {
229 if (iface
->dhcpv6
!= RELAYD_SERVER
&& iface
->dhcpv4
!= RELAYD_SERVER
)
232 if (iface
->dhcpv6
== RELAYD_SERVER
&& iface
->ia_assignments
.next
) {
233 struct dhcpv6_assignment
*c
;
234 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
235 if (!(c
->flags
& OAF_BOUND
) || c
->managed_size
< 0)
238 char ipbuf
[INET6_ADDRSTRLEN
];
241 odhcpd_hexlify(duidbuf
, c
->clid_data
, c
->clid_len
);
243 // iface DUID iaid hostname lifetime assigned length [addrs...]
244 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s %x %s %ld %x %u ",
245 iface
->ifname
, duidbuf
, ntohl(c
->iaid
),
246 (c
->hostname
? c
->hostname
: "-"),
247 (c
->valid_until
> now
?
248 (c
->valid_until
- now
+ wall_time
) :
249 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
250 c
->assigned
, (unsigned)c
->length
);
252 struct in6_addr addr
;
253 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->ia_addr
;
254 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->ia_addr_len
;
257 for (size_t i
= 0; i
< addrlen
; ++i
)
258 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
259 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
260 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
263 for (size_t i
= 0; i
< addrlen
; ++i
) {
264 if (addrs
[i
].prefix
> 96 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
<= now
) ||
265 (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
266 addrs
[i
].prefix
== 64))
269 addr
= addrs
[i
].addr
;
270 if (c
->length
== 128)
271 addr
.s6_addr32
[3] = htonl(c
->assigned
);
273 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
275 inet_ntop(AF_INET6
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
277 if (c
->length
== 128 && c
->hostname
) {
281 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
282 iface
->search
, b
, sizeof(b
)) > 0)
283 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
285 fprintf(fp
, "\t%s\n", c
->hostname
);
286 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
287 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
290 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/%d ", ipbuf
,
291 (c
->managed_size
) ? addrs
[i
].prefix
: c
->length
);
293 leasebuf
[l
- 1] = '\n';
294 fwrite(leasebuf
, 1, l
, fp
);
298 if (iface
->dhcpv4
== RELAYD_SERVER
&& iface
->dhcpv4_assignments
.next
) {
299 struct dhcpv4_assignment
*c
;
300 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
301 if (!(c
->flags
& OAF_BOUND
))
304 char ipbuf
[INET6_ADDRSTRLEN
];
307 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
309 // iface DUID iaid hostname lifetime assigned length [addrs...]
310 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s ipv4 %s %ld %x 32 ",
311 iface
->ifname
, duidbuf
,
312 (c
->hostname
? c
->hostname
: "-"),
313 (c
->valid_until
> now
?
314 (c
->valid_until
- now
+ wall_time
) :
315 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
318 struct in_addr addr
= {htonl(c
->addr
)};
319 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
325 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
326 iface
->search
, b
, sizeof(b
)) > 0)
327 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
329 fprintf(fp
, "\t%s\n", c
->hostname
);
330 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
331 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
334 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/32 ", ipbuf
);
335 leasebuf
[l
- 1] = '\n';
336 fwrite(leasebuf
, 1, l
, fp
);
345 md5_end(newmd5
, &md5
);
347 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
348 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
349 char *argv
[2] = {config
.dhcp_cb
, NULL
};
351 execv(argv
[0], argv
);
358 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
360 if (a
->length
> 64 || a
->managed_size
< 0)
363 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
364 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
366 for (size_t i
= 0; i
< addrlen
; ++i
) {
367 struct in6_addr prefix
= addrs
[i
].addr
;
368 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
369 odhcpd_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
370 iface
, &a
->peer
.sin6_addr
, 1024, add
);
374 // More data was received from TCP connection
375 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
377 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
378 time_t now
= odhcpd_time();
379 bool first
= c
->managed_size
< 0;
383 char *data
= ustream_get_read_buf(s
, &pending
);
384 char *end
= memmem(data
, pending
, "\n\n", 2);
393 if (c
->accept_reconf
)
397 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
398 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
399 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
401 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
402 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
405 x
= strtok_r(NULL
, ",", &saveptr2
);
406 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
409 x
= strtok_r(NULL
, ",", &saveptr2
);
410 if (sscanf(x
, "%u", &n
->preferred
) < 1)
413 x
= strtok_r(NULL
, ",", &saveptr2
);
414 if (sscanf(x
, "%u", &n
->valid
) < 1)
417 if (n
->preferred
> n
->valid
)
420 if (UINT32_MAX
- now
< n
->preferred
)
421 n
->preferred
= UINT32_MAX
;
425 if (UINT32_MAX
- now
< n
->valid
)
426 n
->valid
= UINT32_MAX
;
435 ustream_consume(s
, end
- data
);
438 if (first
&& c
->managed_size
== 0)
439 free_dhcpv6_assignment(c
);
441 c
->valid_until
= now
+ 150;
445 /* TCP transmission has ended, either because of success or timeout or other error */
446 static void managed_handle_pd_done(struct ustream
*s
)
448 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
450 c
->valid_until
= odhcpd_time() + 15;
453 if (c
->accept_reconf
)
457 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
459 struct dhcpv6_assignment
*c
;
461 if (iface
->dhcpv6_pd_manager
[0]) {
462 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
465 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
467 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
468 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
469 ustream_fd_init(&assign
->managed_sock
, fd
);
470 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
471 iaidbuf
, assign
->iaid
, assign
->length
);
472 ustream_write_pending(&assign
->managed_sock
.stream
);
473 assign
->managed_size
= -1;
474 assign
->valid_until
= odhcpd_time() + 15;
475 list_add(&assign
->head
, &iface
->ia_assignments
);
477 /* Wait initial period of up to 250ms for immediate assignment */
478 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
480 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
482 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
487 } else if (iface
->ia_addr_len
< 1)
490 /* Try honoring the hint first */
491 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
492 if (assign
->assigned
) {
493 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
494 if (c
->length
== 128 || c
->length
== 0)
497 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
498 list_add_tail(&assign
->head
, &c
->head
);
499 apply_lease(iface
, assign
, true);
503 if (c
->assigned
!= 0)
504 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
508 /* Fallback to a variable assignment */
510 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
511 if (c
->length
== 128 || c
->length
== 0)
514 current
= (current
+ asize
) & (~asize
);
515 if (current
+ asize
< c
->assigned
) {
516 assign
->assigned
= current
;
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
)));
529 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
531 /* Seed RNG with checksum of DUID */
533 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
534 seed
+= assign
->clid_data
[i
];
537 /* Try to assign up to 100x */
538 for (size_t i
= 0; i
< 100; ++i
) {
540 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
542 struct dhcpv6_assignment
*c
;
543 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
547 if (c
->assigned
> try || c
->length
!= 128) {
548 assign
->assigned
= try;
549 list_add_tail(&assign
->head
, &c
->head
);
551 } else if (c
->assigned
== try)
559 void dhcpv6_ia_preupdate(struct interface
*iface
)
561 if (iface
->dhcpv6
!= RELAYD_SERVER
)
564 struct dhcpv6_assignment
*c
, *border
= list_last_entry(
565 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
567 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
568 if (c
!= border
&& !iface
->managed
)
569 apply_lease(iface
, c
, false);
572 void dhcpv6_ia_postupdate(struct interface
*iface
, time_t now
)
574 if (iface
->dhcpv6
!= RELAYD_SERVER
)
578 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
579 if (iface
->ia_addr
[i
].preferred
> (uint32_t)now
&&
580 iface
->ia_addr
[i
].prefix
< 64 &&
581 iface
->ia_addr
[i
].prefix
> minprefix
)
582 minprefix
= iface
->ia_addr
[i
].prefix
;
585 struct dhcpv6_assignment
*border
= list_last_entry(
586 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
588 if (minprefix
> 32 && minprefix
<= 64)
589 border
->assigned
= 1U << (64 - minprefix
);
591 border
->assigned
= 0;
593 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
594 struct dhcpv6_assignment
*c
, *d
;
595 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
596 if (c
->clid_len
== 0 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
600 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
601 list_move(&c
->head
, &reassign
);
602 else if (c
!= border
)
603 apply_lease(iface
, c
, true);
605 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
607 c
->reconf_sent
= now
;
608 send_reconf(iface
, c
);
610 /* Leave all other assignments of that client alone */
611 struct dhcpv6_assignment
*a
;
612 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
613 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
614 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
615 c
->reconf_cnt
= INT_MAX
;
619 while (!list_empty(&reassign
)) {
620 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
622 if (!assign_pd(iface
, c
)) {
624 list_add(&c
->head
, &iface
->ia_assignments
);
628 dhcpv6_write_statefile();
631 static void reconf_timer(struct uloop_timeout
*event
)
633 time_t now
= odhcpd_time();
634 struct interface
*iface
;
635 list_for_each_entry(iface
, &interfaces
, head
) {
636 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
639 struct dhcpv6_assignment
*a
, *n
;
640 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
641 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
) {
642 if ((a
->length
< 128 && a
->clid_len
> 0) ||
643 (a
->length
== 128 && a
->clid_len
== 0))
644 free_dhcpv6_assignment(a
);
646 } else if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< 8 &&
647 now
> a
->reconf_sent
+ (1 << a
->reconf_cnt
)) {
649 a
->reconf_sent
= now
;
650 send_reconf(iface
, a
);
654 uloop_timeout_set(event
, 2000);
657 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
658 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
659 struct interface
*iface
, bool request
)
661 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
664 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
665 size_t datalen
= sizeof(out
);
666 time_t now
= odhcpd_time();
669 struct __attribute__((packed
)) {
673 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
676 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
677 datalen
+= sizeof(stat
);
681 if (a
->leasetime
> 0)
682 leasetime
= a
->leasetime
;
684 leasetime
= iface
->dhcpv4_leasetime
;
688 else if (leasetime
< 60)
691 uint32_t pref
= leasetime
;
692 uint32_t valid
= leasetime
;
694 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
695 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
698 for (size_t i
= 0; i
< addrlen
; ++i
)
699 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
700 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
701 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
704 for (size_t i
= 0; i
< addrlen
; ++i
) {
705 uint32_t prefix_pref
= addrs
[i
].preferred
;
706 uint32_t prefix_valid
= addrs
[i
].valid
;
708 if (addrs
[i
].prefix
> 96 ||
709 addrs
[i
].preferred
<= (uint32_t)now
)
712 if (prefix_pref
!= UINT32_MAX
)
715 if (prefix_valid
!= UINT32_MAX
)
718 if (a
->length
< 128) {
719 struct dhcpv6_ia_prefix p
= {
720 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
721 .len
= htons(sizeof(p
) - 4),
722 .preferred
= htonl(prefix_pref
),
723 .valid
= htonl(prefix_valid
),
724 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
725 .addr
= addrs
[i
].addr
727 p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
729 size_t entrlen
= sizeof(p
) - 4;
731 if (datalen
+ entrlen
+ 4 > buflen
||
732 (a
->assigned
== 0 && a
->managed_size
== 0) ||
733 (!a
->managed_size
&& a
->length
<= addrs
[i
].prefix
))
736 memcpy(buf
+ datalen
, &p
, sizeof(p
));
737 datalen
+= entrlen
+ 4;
739 struct dhcpv6_ia_addr n
= {
740 .type
= htons(DHCPV6_OPT_IA_ADDR
),
741 .len
= htons(sizeof(n
) - 4),
742 .addr
= addrs
[i
].addr
,
743 .preferred
= htonl(prefix_pref
),
744 .valid
= htonl(prefix_valid
)
746 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
747 size_t entrlen
= sizeof(n
) - 4;
749 if (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
750 addrs
[i
].prefix
<= 64)
753 if (datalen
+ entrlen
+ 4 > buflen
|| a
->assigned
== 0)
756 memcpy(buf
+ datalen
, &n
, sizeof(n
));
757 datalen
+= entrlen
+ 4;
760 /* Calculate T1 / T2 based on non-deprecated addresses */
761 if (prefix_pref
> 0) {
762 if (prefix_pref
< pref
)
765 if (prefix_valid
< valid
)
766 valid
= prefix_valid
;
770 if (!INFINITE_VALID(a
->valid_until
))
771 /* UINT32_MAX is considered as infinite leasetime */
772 a
->valid_until
= (valid
== UINT32_MAX
) ? 0 : valid
+ now
;
774 out
.t1
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 5 / 10);
775 out
.t2
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 8 / 10);
785 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
786 uint16_t otype
, olen
;
787 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
788 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
789 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
790 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
791 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
796 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
797 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
799 for (size_t i
= 0; i
< addrlen
; ++i
) {
800 if (addrs
[i
].prefix
> 96 ||
801 addrs
[i
].preferred
<= (uint32_t)now
)
804 struct in6_addr addr
= addrs
[i
].addr
;
805 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
806 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
808 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
809 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
812 addr
.s6_addr32
[3] = htonl(a
->assigned
);
814 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
821 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
822 struct dhcpv6_ia_prefix inv
= {
823 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
824 .len
= htons(sizeof(inv
) - 4),
831 if (datalen
+ sizeof(inv
) > buflen
)
834 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
835 datalen
+= sizeof(inv
);
837 struct dhcpv6_ia_addr inv
= {
838 .type
= htons(DHCPV6_OPT_IA_ADDR
),
839 .len
= htons(sizeof(inv
) - 4),
845 if (datalen
+ sizeof(inv
) > buflen
)
848 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
849 datalen
+= sizeof(inv
);
856 out
.len
= htons(datalen
- 4);
857 memcpy(buf
, &out
, sizeof(out
));
861 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
862 const char *duidbuf
, bool is_pd
, struct dhcpv6_assignment
*a
, int code
)
864 const char *type
= "UNKNOWN";
865 const char *status
= "UNKNOWN";
867 if (msgtype
== DHCPV6_MSG_RENEW
)
871 case DHCPV6_MSG_SOLICIT
:
874 case DHCPV6_MSG_REQUEST
:
877 case DHCPV6_MSG_CONFIRM
:
880 case DHCPV6_MSG_RENEW
:
883 case DHCPV6_MSG_REBIND
:
886 case DHCPV6_MSG_RELEASE
:
889 case DHCPV6_MSG_DECLINE
:
895 case DHCPV6_STATUS_OK
:
898 case DHCPV6_STATUS_NOADDRSAVAIL
:
899 status
= "no addresses available";
901 case DHCPV6_STATUS_NOBINDING
:
902 status
= "no binding";
904 case DHCPV6_STATUS_NOTONLINK
:
905 status
= "not on-link";
907 case DHCPV6_STATUS_NOPREFIXAVAIL
:
908 status
= "no prefix available";
912 char leasebuf
[256] = "";
915 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
916 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
918 char addrbuf
[INET6_ADDRSTRLEN
];
920 for (size_t i
= 0; i
< addrlen
; ++i
) {
921 if (addrs
[i
].prefix
> 96 || addrs
[i
].preferred
<= (uint32_t)now
)
924 struct in6_addr addr
= addrs
[i
].addr
;
925 int prefix
= a
->managed
? addrs
[i
].prefix
: a
->length
;
927 addr
.s6_addr32
[3] = htonl(a
->assigned
);
929 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
931 inet_ntop(AF_INET6
, &addr
, addrbuf
, sizeof(addrbuf
));
932 lbsize
+= snprintf(leasebuf
+ lbsize
, sizeof(leasebuf
) - lbsize
, "%s/%d ", addrbuf
, prefix
);
936 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
937 duidbuf
, iface
->ifname
, status
, leasebuf
);
940 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
941 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
943 time_t now
= odhcpd_time();
944 size_t response_len
= 0;
945 const struct dhcpv6_client_header
*hdr
= data
;
946 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
947 uint16_t otype
, olen
;
948 /* Find and parse client-id and hostname */
949 bool accept_reconf
= false;
950 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
952 size_t hostname_len
= 0;
953 bool notonlink
= false;
956 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
957 if (otype
== DHCPV6_OPT_CLIENTID
) {
961 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
962 memcpy(mac
, &odata
[8], sizeof(mac
));
963 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
964 memcpy(mac
, &odata
[4], sizeof(mac
));
967 odhcpd_hexlify(duidbuf
, odata
, olen
);
968 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
969 uint8_t fqdn_buf
[256];
970 memcpy(fqdn_buf
, odata
, olen
);
971 fqdn_buf
[olen
++] = 0;
973 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
974 hostname_len
= strcspn(hostname
, ".");
975 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
)
976 accept_reconf
= true;
979 if (!clid_data
|| !clid_len
|| clid_len
> 130)
982 struct dhcpv6_assignment
*first
= NULL
;
983 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
984 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
985 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
986 bool ia_addr_present
= false;
987 if (!is_pd
&& !is_na
)
990 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
991 size_t ia_response_len
= 0;
992 uint8_t reqlen
= (is_pd
) ? 62 : 128;
993 uint32_t reqhint
= 0;
995 /* Parse request hint for IA-PD */
998 uint16_t stype
, slen
;
999 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1000 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1003 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1006 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1007 if (reqlen
> 32 && reqlen
<= 64)
1008 reqhint
&= (1U << (64 - reqlen
)) - 1;
1016 uint16_t stype
, slen
;
1017 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1018 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1021 ia_addr_present
= true;
1025 /* Find assignment */
1026 struct dhcpv6_assignment
*c
, *a
= NULL
;
1027 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1028 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1029 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1030 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1031 (c
->iaid
== ia
->iaid
|| INFINITE_VALID(c
->valid_until
) || now
< c
->valid_until
) &&
1032 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1036 apply_lease(iface
, a
, false);
1037 memcpy(a
->clid_data
, clid_data
, clid_len
);
1038 a
->clid_len
= clid_len
;
1047 /* Generic message handling */
1048 uint16_t status
= DHCPV6_STATUS_OK
;
1049 if (a
&& a
->managed_size
< 0)
1052 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1053 bool assigned
= !!a
;
1055 if (!a
&& !iface
->no_dynamic_dhcp
) {
1056 /* Create new binding */
1057 a
= calloc(1, sizeof(*a
) + clid_len
);
1059 a
->clid_len
= clid_len
;
1063 a
->assigned
= reqhint
;
1064 /* Set valid time to current time indicating */
1065 /* assignment is not having infinite lifetime */
1066 a
->valid_until
= now
;
1069 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1071 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1072 memcpy(a
->clid_data
, clid_data
, clid_len
);
1075 while (!(assigned
= assign_pd(iface
, a
)) &&
1076 !a
->managed_size
&& ++a
->length
<= 64);
1078 assigned
= assign_na(iface
, a
);
1080 if (a
->managed_size
&& !assigned
)
1085 if (!assigned
|| iface
->ia_addr_len
== 0)
1086 /* Set error status */
1087 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1088 else if (assigned
&& !first
) {
1089 size_t handshake_len
= 4;
1091 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1095 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1096 struct dhcpv6_auth_reconfigure auth
= {
1097 htons(DHCPV6_OPT_AUTH
),
1098 htons(sizeof(auth
) - 4),
1100 {htonl(time(NULL
)), htonl(++serial
)},
1104 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1105 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1106 handshake_len
+= sizeof(auth
);
1109 buf
+= handshake_len
;
1110 buflen
-= handshake_len
;
1111 response_len
+= handshake_len
;
1116 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1118 /* Was only a solicitation: mark binding for removal */
1119 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1120 a
->flags
&= ~OAF_BOUND
;
1122 if (!(a
->flags
& OAF_STATIC
))
1123 a
->valid_until
= now
;
1124 } else if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1125 if (hostname_len
> 0) {
1126 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1128 memcpy(a
->hostname
, hostname
, hostname_len
);
1129 a
->hostname
[hostname_len
] = 0;
1132 a
->accept_reconf
= accept_reconf
;
1133 a
->flags
|= OAF_BOUND
;
1134 apply_lease(iface
, a
, true);
1135 } else if (!assigned
&& a
&& a
->managed_size
== 0)
1136 /* Cleanup failed assignment */
1137 free_dhcpv6_assignment(a
);
1139 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1140 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1141 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1142 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1143 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1144 status
= DHCPV6_STATUS_NOBINDING
;
1145 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1146 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1147 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1148 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1150 a
->flags
|= OAF_BOUND
;
1151 apply_lease(iface
, a
, true);
1153 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1154 if (!(a
->flags
& OAF_STATIC
))
1155 a
->valid_until
= now
- 1;
1157 a
->flags
&= ~OAF_BOUND
;
1158 apply_lease(iface
, a
, false);
1159 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1160 a
->flags
&= ~OAF_BOUND
;
1162 if (!(a
->flags
& OAF_STATIC
)) {
1164 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1167 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1168 /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1169 status
= DHCPV6_STATUS_NOTONLINK
;
1170 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1174 buf
+= ia_response_len
;
1175 buflen
-= ia_response_len
;
1176 response_len
+= ia_response_len
;
1177 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1180 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1181 response_len
+ 6 < buflen
) {
1183 buf
[1] = DHCPV6_OPT_STATUS
;
1187 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1191 dhcpv6_write_statefile();
1194 return response_len
;