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>
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];
44 int dhcpv6_ia_init(void)
46 uloop_timeout_set(&reconf_event
, 2000);
51 void free_dhcpv6_assignment(struct dhcpv6_assignment
*c
)
53 if (c
->managed_sock
.fd
.registered
) {
54 ustream_free(&c
->managed_sock
.stream
);
55 close(c
->managed_sock
.fd
.fd
);
67 int setup_dhcpv6_ia_interface(struct interface
*iface
, bool enable
)
69 if (!enable
&& iface
->ia_assignments
.next
) {
70 struct dhcpv6_assignment
*c
;
71 while (!list_empty(&iface
->ia_assignments
)) {
72 c
= list_first_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
73 free_dhcpv6_assignment(c
);
77 if (enable
&& iface
->dhcpv6
== RELAYD_SERVER
) {
78 if (!iface
->ia_assignments
.next
)
79 INIT_LIST_HEAD(&iface
->ia_assignments
);
81 if (list_empty(&iface
->ia_assignments
)) {
82 struct dhcpv6_assignment
*border
= calloc(1, sizeof(*border
));
84 syslog(LOG_ERR
, "Calloc failed for border on interface %s", iface
->ifname
);
89 list_add(&border
->head
, &iface
->ia_assignments
);
92 // Parse static entries
94 list_for_each_entry(lease
, &leases
, head
) {
96 size_t duid_len
= lease
->duid_len
? lease
->duid_len
: 14;
97 struct dhcpv6_assignment
*a
= calloc(1, sizeof(*a
) + duid_len
);
99 syslog(LOG_ERR
, "Calloc failed for static lease assignment on interface %s",
104 if (lease
->dhcpv4_leasetime
> 0)
105 a
->leasetime
= lease
->dhcpv4_leasetime
;
107 a
->clid_len
= duid_len
;
110 a
->assigned
= lease
->hostid
;
112 uint32_t i4a
= ntohl(lease
->ipaddr
.s_addr
) & 0xff;
113 a
->assigned
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
115 odhcpd_urandom(a
->key
, sizeof(a
->key
));
116 memcpy(a
->clid_data
, lease
->duid
, lease
->duid_len
);
117 memcpy(a
->mac
, lease
->mac
.ether_addr_octet
, sizeof(a
->mac
));
118 /* Static assignment */
119 a
->flags
|= OAF_STATIC
;
123 // Assign to all interfaces
124 struct dhcpv6_assignment
*c
;
125 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
126 if (c
->length
!= 128 || c
->assigned
> a
->assigned
) {
127 list_add_tail(&a
->head
, &c
->head
);
129 } else if (c
->assigned
== a
->assigned
) {
130 // Already an assignment with that number
136 if (lease
->hostname
[0]) {
138 a
->hostname
= strdup(lease
->hostname
);
150 static int send_reconf(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
153 struct dhcpv6_client_header hdr
;
157 uint16_t hardware_type
;
162 struct dhcpv6_auth_reconfigure auth
;
165 uint8_t clid_data
[128];
166 } __attribute__((packed
)) reconf_msg
= {
167 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
168 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
169 .srvid_len
= htons(10),
170 .duid_type
= htons(3),
171 .hardware_type
= htons(1),
172 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
174 .msg_id
= DHCPV6_MSG_RENEW
,
175 .auth
= {htons(DHCPV6_OPT_AUTH
),
176 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
177 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
178 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
179 .clid_len
= htons(assign
->clid_len
),
183 odhcpd_get_mac(iface
, reconf_msg
.mac
);
184 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
185 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
188 uint8_t secretbytes
[64];
189 memset(secretbytes
, 0, sizeof(secretbytes
));
190 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
192 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
193 secretbytes
[i
] ^= 0x36;
196 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
197 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
198 md5_end(reconf_msg
.auth
.key
, &md5
);
200 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
201 secretbytes
[i
] ^= 0x36;
202 secretbytes
[i
] ^= 0x5c;
206 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
207 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
208 md5_end(reconf_msg
.auth
.key
, &md5
);
210 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
214 void dhcpv6_write_statefile(void)
219 if (config
.dhcp_statefile
) {
220 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
221 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
225 lockf(fd
, F_LOCK
, 0);
226 if (ftruncate(fd
, 0) < 0) {}
228 FILE *fp
= fdopen(fd
, "w");
234 struct interface
*iface
;
235 list_for_each_entry(iface
, &interfaces
, head
) {
236 if (iface
->dhcpv6
!= RELAYD_SERVER
&& iface
->dhcpv4
!= RELAYD_SERVER
)
239 if (iface
->dhcpv6
== RELAYD_SERVER
&& iface
->ia_assignments
.next
) {
240 struct dhcpv6_assignment
*c
;
241 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
242 if (!(c
->flags
& OAF_BOUND
) || c
->managed_size
< 0)
245 char ipbuf
[INET6_ADDRSTRLEN
];
248 odhcpd_hexlify(duidbuf
, c
->clid_data
, c
->clid_len
);
250 // iface DUID iaid hostname lifetime assigned length [addrs...]
251 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s %x %s %ld %x %u ",
252 iface
->ifname
, duidbuf
, ntohl(c
->iaid
),
253 (c
->hostname
? c
->hostname
: "-"),
254 (c
->valid_until
> now
?
255 (c
->valid_until
- now
+ wall_time
) :
256 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
257 c
->assigned
, (unsigned)c
->length
);
259 struct in6_addr addr
;
260 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->ia_addr
;
261 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->ia_addr_len
;
264 for (size_t i
= 0; i
< addrlen
; ++i
)
265 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
266 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
267 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
270 for (size_t i
= 0; i
< addrlen
; ++i
) {
271 if (addrs
[i
].prefix
> 96 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
<= now
) ||
272 (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
273 addrs
[i
].prefix
== 64))
276 addr
= addrs
[i
].addr
;
277 if (c
->length
== 128)
278 addr
.s6_addr32
[3] = htonl(c
->assigned
);
280 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
282 inet_ntop(AF_INET6
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
284 if (c
->length
== 128 && c
->hostname
) {
288 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
289 iface
->search
, b
, sizeof(b
)) > 0)
290 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
292 fprintf(fp
, "\t%s\n", c
->hostname
);
293 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
294 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
297 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/%d ", ipbuf
,
298 (c
->managed_size
) ? addrs
[i
].prefix
: c
->length
);
300 leasebuf
[l
- 1] = '\n';
301 fwrite(leasebuf
, 1, l
, fp
);
305 if (iface
->dhcpv4
== RELAYD_SERVER
&& iface
->dhcpv4_assignments
.next
) {
306 struct dhcpv4_assignment
*c
;
307 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
308 if (!(c
->flags
& OAF_BOUND
))
311 char ipbuf
[INET6_ADDRSTRLEN
];
314 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
316 // iface DUID iaid hostname lifetime assigned length [addrs...]
317 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s ipv4 %s %ld %x 32 ",
318 iface
->ifname
, duidbuf
,
319 (c
->hostname
? c
->hostname
: "-"),
320 (c
->valid_until
> now
?
321 (c
->valid_until
- now
+ wall_time
) :
322 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
325 struct in_addr addr
= {htonl(c
->addr
)};
326 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
332 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
333 iface
->search
, b
, sizeof(b
)) > 0)
334 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
336 fprintf(fp
, "\t%s\n", c
->hostname
);
337 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
338 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
341 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/32 ", ipbuf
);
342 leasebuf
[l
- 1] = '\n';
343 fwrite(leasebuf
, 1, l
, fp
);
352 md5_end(newmd5
, &md5
);
354 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
355 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
356 char *argv
[2] = {config
.dhcp_cb
, NULL
};
358 execv(argv
[0], argv
);
365 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
367 if (a
->length
> 64 || a
->managed_size
< 0)
370 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
371 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
373 for (size_t i
= 0; i
< addrlen
; ++i
) {
374 struct in6_addr prefix
= addrs
[i
].addr
;
375 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
376 odhcpd_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
377 iface
, &a
->peer
.sin6_addr
, 1024, add
);
382 // More data was received from TCP connection
383 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
385 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
386 time_t now
= odhcpd_time();
387 bool first
= c
->managed_size
< 0;
391 char *data
= ustream_get_read_buf(s
, &pending
);
392 char *end
= memmem(data
, pending
, "\n\n", 2);
401 if (c
->accept_reconf
)
405 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
406 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
407 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
409 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
410 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
413 x
= strtok_r(NULL
, ",", &saveptr2
);
414 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
417 x
= strtok_r(NULL
, ",", &saveptr2
);
418 if (sscanf(x
, "%u", &n
->preferred
) < 1)
421 x
= strtok_r(NULL
, ",", &saveptr2
);
422 if (sscanf(x
, "%u", &n
->valid
) < 1)
425 if (n
->preferred
> n
->valid
)
428 if (UINT32_MAX
- now
< n
->preferred
)
429 n
->preferred
= UINT32_MAX
;
433 if (UINT32_MAX
- now
< n
->valid
)
434 n
->valid
= UINT32_MAX
;
443 ustream_consume(s
, end
- data
);
446 if (first
&& c
->managed_size
== 0)
447 free_dhcpv6_assignment(c
);
449 c
->valid_until
= now
+ 150;
453 // TCP transmission has ended, either because of success or timeout or other error
454 static void managed_handle_pd_done(struct ustream
*s
)
456 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
457 c
->valid_until
= odhcpd_time() + 15;
459 if (c
->accept_reconf
)
465 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
467 struct dhcpv6_assignment
*c
;
469 if (iface
->dhcpv6_pd_manager
[0]) {
470 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
473 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
475 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
476 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
477 ustream_fd_init(&assign
->managed_sock
, fd
);
478 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
479 iaidbuf
, assign
->iaid
, assign
->length
);
480 ustream_write_pending(&assign
->managed_sock
.stream
);
481 assign
->managed_size
= -1;
482 assign
->valid_until
= odhcpd_time() + 15;
483 list_add(&assign
->head
, &iface
->ia_assignments
);
485 // Wait initial period of up to 250ms for immediate assignment
486 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
488 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
490 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
495 } else if (iface
->ia_addr_len
< 1) {
499 // Try honoring the hint first
500 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
501 if (assign
->assigned
) {
502 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
503 if (c
->length
== 128 || c
->length
== 0)
506 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
507 list_add_tail(&assign
->head
, &c
->head
);
508 apply_lease(iface
, assign
, true);
512 if (c
->assigned
!= 0)
513 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
517 // Fallback to a variable assignment
519 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
520 if (c
->length
== 128 || c
->length
== 0)
523 current
= (current
+ asize
) & (~asize
);
524 if (current
+ asize
< c
->assigned
) {
525 assign
->assigned
= current
;
526 list_add_tail(&assign
->head
, &c
->head
);
527 apply_lease(iface
, assign
, true);
531 if (c
->assigned
!= 0)
532 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
539 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
541 // Seed RNG with checksum of DUID
543 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
544 seed
+= assign
->clid_data
[i
];
547 // Try to assign up to 100x
548 for (size_t i
= 0; i
< 100; ++i
) {
550 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
552 struct dhcpv6_assignment
*c
;
553 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
557 if (c
->assigned
> try || c
->length
!= 128) {
558 assign
->assigned
= try;
559 list_add_tail(&assign
->head
, &c
->head
);
561 } else if (c
->assigned
== try) {
570 void dhcpv6_ia_preupdate(struct interface
*iface
)
572 if (iface
->dhcpv6
!= RELAYD_SERVER
)
575 struct dhcpv6_assignment
*c
, *border
= list_last_entry(
576 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
577 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
578 if (c
!= border
&& !iface
->managed
)
579 apply_lease(iface
, c
, false);
582 void dhcpv6_ia_postupdate(struct interface
*iface
, time_t now
)
584 if (iface
->dhcpv6
!= RELAYD_SERVER
)
588 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
589 if (iface
->ia_addr
[i
].preferred
> (uint32_t)now
&&
590 iface
->ia_addr
[i
].prefix
< 64 &&
591 iface
->ia_addr
[i
].prefix
> minprefix
)
592 minprefix
= iface
->ia_addr
[i
].prefix
;
595 struct dhcpv6_assignment
*border
= list_last_entry(
596 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
597 if (minprefix
> 32 && minprefix
<= 64)
598 border
->assigned
= 1U << (64 - minprefix
);
600 border
->assigned
= 0;
602 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
603 struct dhcpv6_assignment
*c
, *d
;
604 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
605 if (c
->clid_len
== 0 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
609 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
610 list_move(&c
->head
, &reassign
);
611 else if (c
!= border
)
612 apply_lease(iface
, c
, true);
614 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
616 c
->reconf_sent
= now
;
617 send_reconf(iface
, c
);
619 // Leave all other assignments of that client alone
620 struct dhcpv6_assignment
*a
;
621 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
622 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
623 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
624 c
->reconf_cnt
= INT_MAX
;
628 while (!list_empty(&reassign
)) {
629 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
631 if (!assign_pd(iface
, c
)) {
633 list_add(&c
->head
, &iface
->ia_assignments
);
637 dhcpv6_write_statefile();
641 static void reconf_timer(struct uloop_timeout
*event
)
643 time_t now
= odhcpd_time();
644 struct interface
*iface
;
645 list_for_each_entry(iface
, &interfaces
, head
) {
646 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
649 struct dhcpv6_assignment
*a
, *n
;
650 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
651 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
) {
652 if ((a
->length
< 128 && a
->clid_len
> 0) ||
653 (a
->length
== 128 && a
->clid_len
== 0)) {
655 free_dhcpv6_assignment(a
);
657 } else if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< 8 &&
658 now
> a
->reconf_sent
+ (1 << a
->reconf_cnt
)) {
660 a
->reconf_sent
= now
;
661 send_reconf(iface
, a
);
665 uloop_timeout_set(event
, 2000);
669 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
670 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
671 struct interface
*iface
, bool request
)
673 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
676 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
677 size_t datalen
= sizeof(out
);
678 time_t now
= odhcpd_time();
681 struct __attribute__((packed
)) {
685 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
688 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
689 datalen
+= sizeof(stat
);
693 if (a
->leasetime
> 0) {
694 leasetime
= a
->leasetime
;
696 leasetime
= iface
->dhcpv4_leasetime
;
700 else if (leasetime
< 60)
703 uint32_t pref
= leasetime
;
704 uint32_t valid
= leasetime
;
706 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
707 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
710 for (size_t i
= 0; i
< addrlen
; ++i
)
711 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
712 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
713 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
716 for (size_t i
= 0; i
< addrlen
; ++i
) {
717 uint32_t prefix_pref
= addrs
[i
].preferred
;
718 uint32_t prefix_valid
= addrs
[i
].valid
;
720 if (addrs
[i
].prefix
> 96 ||
721 addrs
[i
].preferred
<= (uint32_t)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 (addrs
[i
].prefix
> 96 ||
813 addrs
[i
].preferred
<= (uint32_t)now
)
816 struct in6_addr addr
= addrs
[i
].addr
;
817 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
818 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
820 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
821 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
824 addr
.s6_addr32
[3] = htonl(a
->assigned
);
826 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
833 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
834 struct dhcpv6_ia_prefix inv
= {
835 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
836 .len
= htons(sizeof(inv
) - 4),
843 if (datalen
+ sizeof(inv
) > buflen
)
846 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
847 datalen
+= sizeof(inv
);
849 struct dhcpv6_ia_addr inv
= {
850 .type
= htons(DHCPV6_OPT_IA_ADDR
),
851 .len
= htons(sizeof(inv
) - 4),
857 if (datalen
+ sizeof(inv
) > buflen
)
860 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
861 datalen
+= sizeof(inv
);
868 out
.len
= htons(datalen
- 4);
869 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 char addrbuf
[INET6_ADDRSTRLEN
];
933 for (size_t i
= 0; i
< addrlen
; ++i
) {
934 if (addrs
[i
].prefix
> 96 || addrs
[i
].preferred
<= (uint32_t)now
)
937 struct in6_addr addr
= addrs
[i
].addr
;
938 int prefix
= a
->managed
? addrs
[i
].prefix
: a
->length
;
940 addr
.s6_addr32
[3] = htonl(a
->assigned
);
942 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
944 inet_ntop(AF_INET6
, &addr
, addrbuf
, sizeof(addrbuf
));
945 lbsize
+= snprintf(leasebuf
+ lbsize
, sizeof(leasebuf
) - lbsize
, "%s/%d ", addrbuf
, prefix
);
949 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
950 duidbuf
, iface
->ifname
, status
, leasebuf
);
955 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
956 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
958 time_t now
= odhcpd_time();
959 size_t response_len
= 0;
960 const struct dhcpv6_client_header
*hdr
= data
;
961 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
962 uint16_t otype
, olen
;
964 // Find and parse client-id and hostname
965 bool accept_reconf
= false;
966 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
968 size_t hostname_len
= 0;
969 bool notonlink
= false;
972 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
973 if (otype
== DHCPV6_OPT_CLIENTID
) {
977 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
978 memcpy(mac
, &odata
[8], sizeof(mac
));
979 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
980 memcpy(mac
, &odata
[4], sizeof(mac
));
983 odhcpd_hexlify(duidbuf
, odata
, olen
);
984 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
985 uint8_t fqdn_buf
[256];
986 memcpy(fqdn_buf
, odata
, olen
);
987 fqdn_buf
[olen
++] = 0;
989 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
990 hostname_len
= strcspn(hostname
, ".");
991 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
) {
992 accept_reconf
= true;
996 if (!clid_data
|| !clid_len
|| clid_len
> 130)
999 struct dhcpv6_assignment
*first
= NULL
;
1000 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1001 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
1002 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
1003 bool ia_addr_present
= false;
1004 if (!is_pd
&& !is_na
)
1007 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1008 size_t ia_response_len
= 0;
1009 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1010 uint32_t reqhint
= 0;
1012 // Parse request hint for IA-PD
1015 uint16_t stype
, slen
;
1016 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1017 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1020 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1023 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1024 if (reqlen
> 32 && reqlen
<= 64)
1025 reqhint
&= (1U << (64 - reqlen
)) - 1;
1033 uint16_t stype
, slen
;
1034 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1035 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1038 ia_addr_present
= true;
1043 struct dhcpv6_assignment
*c
, *a
= NULL
;
1044 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1045 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1046 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1047 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1048 (c
->iaid
== ia
->iaid
|| INFINITE_VALID(c
->valid_until
) || now
< c
->valid_until
) &&
1049 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1053 apply_lease(iface
, a
, false);
1054 memcpy(a
->clid_data
, clid_data
, clid_len
);
1055 a
->clid_len
= clid_len
;
1064 // Generic message handling
1065 uint16_t status
= DHCPV6_STATUS_OK
;
1066 if (a
&& a
->managed_size
< 0) {
1068 } else if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1069 bool assigned
= !!a
;
1071 if (!a
&& !iface
->no_dynamic_dhcp
) { // Create new binding
1072 a
= calloc(1, sizeof(*a
) + clid_len
);
1074 a
->clid_len
= clid_len
;
1078 a
->assigned
= reqhint
;
1079 // Set valid time to current time indicating
1080 // assignment is not having infinite lifetime
1081 a
->valid_until
= now
;
1084 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1086 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1087 memcpy(a
->clid_data
, clid_data
, clid_len
);
1090 while (!(assigned
= assign_pd(iface
, a
)) &&
1091 !a
->managed_size
&& ++a
->length
<= 64);
1093 assigned
= assign_na(iface
, a
);
1095 if (a
->managed_size
&& !assigned
)
1100 if (!assigned
|| iface
->ia_addr_len
== 0) { // Set error status
1101 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1102 } else if (assigned
&& !first
) { //
1103 size_t handshake_len
= 4;
1105 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1109 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1110 struct dhcpv6_auth_reconfigure auth
= {
1111 htons(DHCPV6_OPT_AUTH
),
1112 htons(sizeof(auth
) - 4),
1114 {htonl(time(NULL
)), htonl(++serial
)},
1118 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1119 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1120 handshake_len
+= sizeof(auth
);
1123 buf
+= handshake_len
;
1124 buflen
-= handshake_len
;
1125 response_len
+= handshake_len
;
1130 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1132 // Was only a solicitation: mark binding for removal
1133 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1134 a
->flags
&= ~OAF_BOUND
;
1136 if (!(a
->flags
& OAF_STATIC
))
1137 a
->valid_until
= now
;
1138 } else if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1139 if (hostname_len
> 0) {
1140 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1142 memcpy(a
->hostname
, hostname
, hostname_len
);
1143 a
->hostname
[hostname_len
] = 0;
1146 a
->accept_reconf
= accept_reconf
;
1147 a
->flags
|= OAF_BOUND
;
1148 apply_lease(iface
, a
, true);
1149 } else if (!assigned
&& a
&& a
->managed_size
== 0) { // Cleanup failed assignment
1150 free_dhcpv6_assignment(a
);
1152 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1153 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1154 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1155 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1156 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1157 status
= DHCPV6_STATUS_NOBINDING
;
1158 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1159 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1160 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1161 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1163 a
->flags
|= OAF_BOUND
;
1164 apply_lease(iface
, a
, true);
1166 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1167 if (!(a
->flags
& OAF_STATIC
))
1168 a
->valid_until
= now
- 1;
1170 a
->flags
&= ~OAF_BOUND
;
1171 apply_lease(iface
, a
, false);
1172 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1173 a
->flags
&= ~OAF_BOUND
;
1175 if (!(a
->flags
& OAF_STATIC
)) {
1177 a
->valid_until
= now
+ 3600; // Block address for 1h
1180 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1181 // Send NOTONLINK for CONFIRM with addr present so that clients restart connection
1182 status
= DHCPV6_STATUS_NOTONLINK
;
1183 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1187 buf
+= ia_response_len
;
1188 buflen
-= ia_response_len
;
1189 response_len
+= ia_response_len
;
1190 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1193 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1194 response_len
+ 6 < buflen
) {
1196 buf
[1] = DHCPV6_OPT_STATUS
;
1200 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1204 dhcpv6_write_statefile();
1207 return response_len
;