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
);
453 else if (first
& !(c
->flags
& OAF_STATIC
))
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 if (!(c
->flags
& OAF_STATIC
))
464 c
->valid_until
= odhcpd_time() + 15;
468 if (c
->accept_reconf
)
472 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
474 struct dhcpv6_assignment
*c
;
476 if (iface
->dhcpv6_pd_manager
[0]) {
477 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
480 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
482 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
483 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
484 ustream_fd_init(&assign
->managed_sock
, fd
);
485 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
486 iaidbuf
, assign
->iaid
, assign
->length
);
487 ustream_write_pending(&assign
->managed_sock
.stream
);
488 assign
->managed_size
= -1;
490 if (!(assign
->flags
& OAF_STATIC
))
491 assign
->valid_until
= odhcpd_time() + 15;
493 list_add(&assign
->head
, &iface
->ia_assignments
);
495 /* Wait initial period of up to 250ms for immediate assignment */
496 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
498 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
500 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
505 } else if (iface
->ia_addr_len
< 1)
508 /* Try honoring the hint first */
509 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
510 if (assign
->assigned
) {
511 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
512 if (c
->length
== 128 || c
->length
== 0)
515 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
516 list_add_tail(&assign
->head
, &c
->head
);
517 apply_lease(iface
, assign
, true);
521 if (c
->assigned
!= 0)
522 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
526 /* Fallback to a variable assignment */
528 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
529 if (c
->length
== 128 || c
->length
== 0)
532 current
= (current
+ asize
) & (~asize
);
533 if (current
+ asize
< c
->assigned
) {
534 assign
->assigned
= current
;
535 list_add_tail(&assign
->head
, &c
->head
);
536 apply_lease(iface
, assign
, true);
540 if (c
->assigned
!= 0)
541 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
547 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
549 /* Seed RNG with checksum of DUID */
551 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
552 seed
+= assign
->clid_data
[i
];
555 /* Try to assign up to 100x */
556 for (size_t i
= 0; i
< 100; ++i
) {
558 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
560 struct dhcpv6_assignment
*c
;
561 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
565 if (c
->assigned
> try || c
->length
!= 128) {
566 assign
->assigned
= try;
567 list_add_tail(&assign
->head
, &c
->head
);
569 } else if (c
->assigned
== try)
577 void dhcpv6_ia_preupdate(struct interface
*iface
)
579 if (iface
->dhcpv6
!= RELAYD_SERVER
)
582 struct dhcpv6_assignment
*c
, *border
= list_last_entry(
583 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
585 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
586 if (c
!= border
&& !iface
->managed
)
587 apply_lease(iface
, c
, false);
590 void dhcpv6_ia_postupdate(struct interface
*iface
, time_t now
)
592 if (iface
->dhcpv6
!= RELAYD_SERVER
)
596 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
597 if (iface
->ia_addr
[i
].preferred
> (uint32_t)now
&&
598 iface
->ia_addr
[i
].prefix
< 64 &&
599 iface
->ia_addr
[i
].prefix
> minprefix
)
600 minprefix
= iface
->ia_addr
[i
].prefix
;
603 struct dhcpv6_assignment
*border
= list_last_entry(
604 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
606 if (minprefix
> 32 && minprefix
<= 64)
607 border
->assigned
= 1U << (64 - minprefix
);
609 border
->assigned
= 0;
611 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
612 struct dhcpv6_assignment
*c
, *d
;
613 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
614 if (c
->clid_len
== 0 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
618 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
619 list_move(&c
->head
, &reassign
);
620 else if (c
!= border
)
621 apply_lease(iface
, c
, true);
623 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
625 c
->reconf_sent
= now
;
626 send_reconf(iface
, c
);
628 /* Leave all other assignments of that client alone */
629 struct dhcpv6_assignment
*a
;
630 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
631 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
632 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
633 c
->reconf_cnt
= INT_MAX
;
637 while (!list_empty(&reassign
)) {
638 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
640 if (!assign_pd(iface
, c
)) {
642 list_add(&c
->head
, &iface
->ia_assignments
);
646 dhcpv6_write_statefile();
649 static void reconf_timer(struct uloop_timeout
*event
)
651 time_t now
= odhcpd_time();
652 struct interface
*iface
;
653 list_for_each_entry(iface
, &interfaces
, head
) {
654 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
657 struct dhcpv6_assignment
*a
, *n
;
658 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
659 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
) {
660 if ((a
->length
< 128 && a
->clid_len
> 0) ||
661 (a
->length
== 128 && a
->clid_len
== 0))
662 free_dhcpv6_assignment(a
);
664 } else if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< 8 &&
665 now
> a
->reconf_sent
+ (1 << a
->reconf_cnt
)) {
667 a
->reconf_sent
= now
;
668 send_reconf(iface
, a
);
672 uloop_timeout_set(event
, 2000);
675 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
676 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
677 struct interface
*iface
, bool request
)
679 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
682 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
683 size_t datalen
= sizeof(out
);
684 time_t now
= odhcpd_time();
687 struct __attribute__((packed
)) {
691 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
694 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
695 datalen
+= sizeof(stat
);
699 if (a
->leasetime
> 0)
700 leasetime
= a
->leasetime
;
702 leasetime
= iface
->dhcpv4_leasetime
;
706 else if (leasetime
< 60)
709 uint32_t pref
= leasetime
;
710 uint32_t valid
= leasetime
;
712 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
713 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
714 size_t m
= elect_addr(addrs
, addrlen
);
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 (!valid_addr(&addrs
[i
], now
))
723 if (prefix_pref
!= UINT32_MAX
)
726 if (prefix_valid
!= UINT32_MAX
)
729 if (a
->length
< 128) {
730 struct dhcpv6_ia_prefix p
= {
731 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
732 .len
= htons(sizeof(p
) - 4),
733 .preferred
= htonl(prefix_pref
),
734 .valid
= htonl(prefix_valid
),
735 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
736 .addr
= addrs
[i
].addr
738 p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
740 size_t entrlen
= sizeof(p
) - 4;
742 if (datalen
+ entrlen
+ 4 > buflen
||
743 (a
->assigned
== 0 && a
->managed_size
== 0) ||
744 (!a
->managed_size
&& a
->length
<= addrs
[i
].prefix
))
747 memcpy(buf
+ datalen
, &p
, sizeof(p
));
748 datalen
+= entrlen
+ 4;
750 struct dhcpv6_ia_addr n
= {
751 .type
= htons(DHCPV6_OPT_IA_ADDR
),
752 .len
= htons(sizeof(n
) - 4),
753 .addr
= addrs
[i
].addr
,
754 .preferred
= htonl(prefix_pref
),
755 .valid
= htonl(prefix_valid
)
757 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
758 size_t entrlen
= sizeof(n
) - 4;
760 if (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
761 addrs
[i
].prefix
<= 64)
764 if (datalen
+ entrlen
+ 4 > buflen
|| a
->assigned
== 0)
767 memcpy(buf
+ datalen
, &n
, sizeof(n
));
768 datalen
+= entrlen
+ 4;
771 /* Calculate T1 / T2 based on non-deprecated addresses */
772 if (prefix_pref
> 0) {
773 if (prefix_pref
< pref
)
776 if (prefix_valid
< valid
)
777 valid
= prefix_valid
;
781 if (!INFINITE_VALID(a
->valid_until
))
782 /* UINT32_MAX is considered as infinite leasetime */
783 a
->valid_until
= (valid
== UINT32_MAX
) ? 0 : valid
+ now
;
785 out
.t1
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 5 / 10);
786 out
.t2
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 8 / 10);
796 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
797 uint16_t otype
, olen
;
798 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
799 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
800 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
801 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
802 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
807 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
808 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
810 for (size_t i
= 0; i
< addrlen
; ++i
) {
811 if (!valid_addr(&addrs
[i
], now
))
814 struct in6_addr addr
= addrs
[i
].addr
;
815 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
816 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
818 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
819 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
822 addr
.s6_addr32
[3] = htonl(a
->assigned
);
824 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
831 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
832 struct dhcpv6_ia_prefix inv
= {
833 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
834 .len
= htons(sizeof(inv
) - 4),
841 if (datalen
+ sizeof(inv
) > buflen
)
844 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
845 datalen
+= sizeof(inv
);
847 struct dhcpv6_ia_addr inv
= {
848 .type
= htons(DHCPV6_OPT_IA_ADDR
),
849 .len
= htons(sizeof(inv
) - 4),
855 if (datalen
+ sizeof(inv
) > buflen
)
858 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
859 datalen
+= sizeof(inv
);
866 out
.len
= htons(datalen
- 4);
867 memcpy(buf
, &out
, sizeof(out
));
871 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
872 const char *duidbuf
, bool is_pd
, struct dhcpv6_assignment
*a
, int code
)
874 const char *type
= "UNKNOWN";
875 const char *status
= "UNKNOWN";
877 if (msgtype
== DHCPV6_MSG_RENEW
)
881 case DHCPV6_MSG_SOLICIT
:
884 case DHCPV6_MSG_REQUEST
:
887 case DHCPV6_MSG_CONFIRM
:
890 case DHCPV6_MSG_RENEW
:
893 case DHCPV6_MSG_REBIND
:
896 case DHCPV6_MSG_RELEASE
:
899 case DHCPV6_MSG_DECLINE
:
905 case DHCPV6_STATUS_OK
:
908 case DHCPV6_STATUS_NOADDRSAVAIL
:
909 status
= "no addresses available";
911 case DHCPV6_STATUS_NOBINDING
:
912 status
= "no binding";
914 case DHCPV6_STATUS_NOTONLINK
:
915 status
= "not on-link";
917 case DHCPV6_STATUS_NOPREFIXAVAIL
:
918 status
= "no prefix available";
922 char leasebuf
[256] = "";
925 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
926 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
928 char addrbuf
[INET6_ADDRSTRLEN
];
930 for (size_t i
= 0; i
< addrlen
; ++i
) {
931 if (!valid_addr(&addrs
[i
], now
))
934 struct in6_addr addr
= addrs
[i
].addr
;
935 int prefix
= a
->managed
? addrs
[i
].prefix
: a
->length
;
937 addr
.s6_addr32
[3] = htonl(a
->assigned
);
939 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
941 inet_ntop(AF_INET6
, &addr
, addrbuf
, sizeof(addrbuf
));
942 lbsize
+= snprintf(leasebuf
+ lbsize
, sizeof(leasebuf
) - lbsize
, "%s/%d ", addrbuf
, prefix
);
946 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
947 duidbuf
, iface
->ifname
, status
, leasebuf
);
950 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
951 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
953 time_t now
= odhcpd_time();
954 size_t response_len
= 0;
955 const struct dhcpv6_client_header
*hdr
= data
;
956 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
957 uint16_t otype
, olen
;
958 /* Find and parse client-id and hostname */
959 bool accept_reconf
= false;
960 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
962 size_t hostname_len
= 0;
963 bool notonlink
= false;
966 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
967 if (otype
== DHCPV6_OPT_CLIENTID
) {
971 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
972 memcpy(mac
, &odata
[8], sizeof(mac
));
973 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
974 memcpy(mac
, &odata
[4], sizeof(mac
));
977 odhcpd_hexlify(duidbuf
, odata
, olen
);
978 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
979 uint8_t fqdn_buf
[256];
980 memcpy(fqdn_buf
, odata
, olen
);
981 fqdn_buf
[olen
++] = 0;
983 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
984 hostname_len
= strcspn(hostname
, ".");
985 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
)
986 accept_reconf
= true;
989 if (!clid_data
|| !clid_len
|| clid_len
> 130)
992 struct dhcpv6_assignment
*first
= NULL
;
993 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
994 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
995 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
996 bool ia_addr_present
= false;
997 if (!is_pd
&& !is_na
)
1000 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1001 size_t ia_response_len
= 0;
1002 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1003 uint32_t reqhint
= 0;
1005 /* Parse request hint for IA-PD */
1008 uint16_t stype
, slen
;
1009 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1010 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1013 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1016 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1017 if (reqlen
> 32 && reqlen
<= 64)
1018 reqhint
&= (1U << (64 - reqlen
)) - 1;
1026 uint16_t stype
, slen
;
1027 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1028 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1031 ia_addr_present
= true;
1035 /* Find assignment */
1036 struct dhcpv6_assignment
*c
, *a
= NULL
;
1037 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1038 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1039 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1040 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1041 (c
->iaid
== ia
->iaid
|| INFINITE_VALID(c
->valid_until
) || now
< c
->valid_until
) &&
1042 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1046 apply_lease(iface
, a
, false);
1047 memcpy(a
->clid_data
, clid_data
, clid_len
);
1048 a
->clid_len
= clid_len
;
1057 /* Generic message handling */
1058 uint16_t status
= DHCPV6_STATUS_OK
;
1059 if (a
&& a
->managed_size
< 0)
1062 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1063 bool assigned
= !!a
;
1065 if (!a
&& !iface
->no_dynamic_dhcp
) {
1066 /* Create new binding */
1067 a
= calloc(1, sizeof(*a
) + clid_len
);
1069 a
->clid_len
= clid_len
;
1073 a
->assigned
= reqhint
;
1074 /* Set valid time to current time indicating */
1075 /* assignment is not having infinite lifetime */
1076 a
->valid_until
= now
;
1079 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1081 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1082 memcpy(a
->clid_data
, clid_data
, clid_len
);
1085 while (!(assigned
= assign_pd(iface
, a
)) &&
1086 !a
->managed_size
&& ++a
->length
<= 64);
1088 assigned
= assign_na(iface
, a
);
1090 if (a
->managed_size
&& !assigned
)
1095 if (!assigned
|| iface
->ia_addr_len
== 0)
1096 /* Set error status */
1097 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1098 else if (assigned
&& !first
) {
1099 size_t handshake_len
= 4;
1101 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1105 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1106 struct dhcpv6_auth_reconfigure auth
= {
1107 htons(DHCPV6_OPT_AUTH
),
1108 htons(sizeof(auth
) - 4),
1110 {htonl(time(NULL
)), htonl(++serial
)},
1114 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1115 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1116 handshake_len
+= sizeof(auth
);
1119 buf
+= handshake_len
;
1120 buflen
-= handshake_len
;
1121 response_len
+= handshake_len
;
1126 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1128 /* Was only a solicitation: mark binding for removal */
1129 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1130 a
->flags
&= ~OAF_BOUND
;
1132 if (!(a
->flags
& OAF_STATIC
))
1133 a
->valid_until
= now
;
1134 } else if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1135 if (hostname_len
> 0) {
1136 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1138 memcpy(a
->hostname
, hostname
, hostname_len
);
1139 a
->hostname
[hostname_len
] = 0;
1142 a
->accept_reconf
= accept_reconf
;
1143 a
->flags
|= OAF_BOUND
;
1144 apply_lease(iface
, a
, true);
1145 } else if (!assigned
&& a
&& a
->managed_size
== 0)
1146 /* Cleanup failed assignment */
1147 free_dhcpv6_assignment(a
);
1149 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1150 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1151 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1152 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1153 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1154 status
= DHCPV6_STATUS_NOBINDING
;
1155 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1156 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1157 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1158 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1160 a
->flags
|= OAF_BOUND
;
1161 apply_lease(iface
, a
, true);
1163 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1164 if (!(a
->flags
& OAF_STATIC
))
1165 a
->valid_until
= now
- 1;
1167 a
->flags
&= ~OAF_BOUND
;
1168 apply_lease(iface
, a
, false);
1169 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1170 a
->flags
&= ~OAF_BOUND
;
1172 if (!(a
->flags
& OAF_STATIC
)) {
1174 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1177 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1178 /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1179 status
= DHCPV6_STATUS_NOTONLINK
;
1180 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1184 buf
+= ia_response_len
;
1185 buflen
-= ia_response_len
;
1186 response_len
+= ia_response_len
;
1187 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1190 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1191 response_len
+ 6 < buflen
) {
1193 buf
[1] = DHCPV6_OPT_STATUS
;
1197 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1201 dhcpv6_write_statefile();
1204 return response_len
;