2 * Copyright (C) 2013 Steven Barth <steven@midlink.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
18 #include "libubox/md5.h"
19 #include "libubox/usock.h"
33 #include <arpa/inet.h>
34 #include <sys/timerfd.h>
37 static void reconf_timer(struct uloop_timeout
*event
);
38 static struct uloop_timeout reconf_event
= {.cb
= reconf_timer
};
39 static uint32_t serial
= 0;
40 static uint8_t statemd5
[16];
43 int dhcpv6_ia_init(void)
45 uloop_timeout_set(&reconf_event
, 2000);
50 void free_dhcpv6_assignment(struct dhcpv6_assignment
*c
)
52 if (c
->managed_sock
.fd
.registered
) {
53 ustream_free(&c
->managed_sock
.stream
);
54 close(c
->managed_sock
.fd
.fd
);
66 int setup_dhcpv6_ia_interface(struct interface
*iface
, bool enable
)
68 if (!enable
&& iface
->ia_assignments
.next
) {
69 struct dhcpv6_assignment
*c
;
70 while (!list_empty(&iface
->ia_assignments
)) {
71 c
= list_first_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
72 free_dhcpv6_assignment(c
);
76 if (enable
&& iface
->dhcpv6
== RELAYD_SERVER
) {
77 if (!iface
->ia_assignments
.next
)
78 INIT_LIST_HEAD(&iface
->ia_assignments
);
80 if (list_empty(&iface
->ia_assignments
)) {
81 struct dhcpv6_assignment
*border
= calloc(1, sizeof(*border
));
83 syslog(LOG_ERR
, "Calloc failed for border on interface %s", iface
->ifname
);
88 list_add(&border
->head
, &iface
->ia_assignments
);
91 // Parse static entries
93 list_for_each_entry(lease
, &leases
, head
) {
95 size_t duid_len
= lease
->duid_len
? lease
->duid_len
: 14;
96 struct dhcpv6_assignment
*a
= calloc(1, sizeof(*a
) + duid_len
);
98 syslog(LOG_ERR
, "Calloc failed for static lease assignment on interface %s",
103 if (lease
->dhcpv4_leasetime
> 0)
104 a
->leasetime
= lease
->dhcpv4_leasetime
;
106 a
->clid_len
= duid_len
;
109 a
->assigned
= lease
->hostid
;
111 uint32_t i4a
= ntohl(lease
->ipaddr
.s_addr
) & 0xff;
112 a
->assigned
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
114 odhcpd_urandom(a
->key
, sizeof(a
->key
));
115 memcpy(a
->clid_data
, lease
->duid
, lease
->duid_len
);
116 memcpy(a
->mac
, lease
->mac
.ether_addr_octet
, sizeof(a
->mac
));
118 // Assign to all interfaces
119 struct dhcpv6_assignment
*c
;
120 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
121 if (c
->length
!= 128 || c
->assigned
> a
->assigned
) {
122 list_add_tail(&a
->head
, &c
->head
);
124 } else if (c
->assigned
== a
->assigned
) {
125 // Already an assignment with that number
131 if (lease
->hostname
[0]) {
133 a
->hostname
= strdup(lease
->hostname
);
145 static int send_reconf(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
148 struct dhcpv6_client_header hdr
;
152 uint16_t hardware_type
;
157 struct dhcpv6_auth_reconfigure auth
;
160 uint8_t clid_data
[128];
161 } __attribute__((packed
)) reconf_msg
= {
162 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
163 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
164 .srvid_len
= htons(10),
165 .duid_type
= htons(3),
166 .hardware_type
= htons(1),
167 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
169 .msg_id
= DHCPV6_MSG_RENEW
,
170 .auth
= {htons(DHCPV6_OPT_AUTH
),
171 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
172 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
173 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
174 .clid_len
= htons(assign
->clid_len
),
178 odhcpd_get_mac(iface
, reconf_msg
.mac
);
179 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
180 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
183 uint8_t secretbytes
[64];
184 memset(secretbytes
, 0, sizeof(secretbytes
));
185 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
187 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
188 secretbytes
[i
] ^= 0x36;
191 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
192 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
193 md5_end(reconf_msg
.auth
.key
, &md5
);
195 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
196 secretbytes
[i
] ^= 0x36;
197 secretbytes
[i
] ^= 0x5c;
201 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
202 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
203 md5_end(reconf_msg
.auth
.key
, &md5
);
205 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
209 void dhcpv6_write_statefile(void)
214 if (config
.dhcp_statefile
) {
215 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
216 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
220 lockf(fd
, F_LOCK
, 0);
221 if (ftruncate(fd
, 0) < 0) {}
223 FILE *fp
= fdopen(fd
, "w");
229 struct interface
*iface
;
230 list_for_each_entry(iface
, &interfaces
, head
) {
231 if (iface
->dhcpv6
!= RELAYD_SERVER
&& iface
->dhcpv4
!= RELAYD_SERVER
)
234 if (iface
->dhcpv6
== RELAYD_SERVER
&& iface
->ia_assignments
.next
) {
235 struct dhcpv6_assignment
*c
;
236 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
237 if (c
->clid_len
== 0 || c
->managed_size
< 0)
240 char ipbuf
[INET6_ADDRSTRLEN
];
243 odhcpd_hexlify(duidbuf
, c
->clid_data
, c
->clid_len
);
245 // iface DUID iaid hostname lifetime assigned length [addrs...]
246 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s %x %s %u %x %u ",
247 iface
->ifname
, duidbuf
, ntohl(c
->iaid
),
248 (c
->hostname
? c
->hostname
: "-"),
249 (unsigned)(c
->valid_until
> now
?
250 (c
->valid_until
- now
+ wall_time
) : 0),
251 c
->assigned
, (unsigned)c
->length
);
253 struct in6_addr addr
;
254 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->ia_addr
;
255 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->ia_addr_len
;
258 for (size_t i
= 0; i
< addrlen
; ++i
)
259 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
260 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
261 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
264 for (size_t i
= 0; i
< addrlen
; ++i
) {
265 if (addrs
[i
].prefix
> 96 || c
->valid_until
<= now
||
266 (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
267 addrs
[i
].prefix
== 64))
270 addr
= addrs
[i
].addr
;
271 if (c
->length
== 128)
272 addr
.s6_addr32
[3] = htonl(c
->assigned
);
274 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
276 inet_ntop(AF_INET6
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
278 if (c
->length
== 128 && c
->hostname
) {
282 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
283 iface
->search
, b
, sizeof(b
)) > 0)
284 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
286 fprintf(fp
, "\t%s\n", c
->hostname
);
287 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
288 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
291 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/%d ", ipbuf
,
292 (c
->managed_size
) ? addrs
[i
].prefix
: c
->length
);
294 leasebuf
[l
- 1] = '\n';
295 fwrite(leasebuf
, 1, l
, fp
);
299 if (iface
->dhcpv4
== RELAYD_SERVER
&& iface
->dhcpv4_assignments
.next
) {
300 struct dhcpv4_assignment
*c
;
301 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
302 char ipbuf
[INET6_ADDRSTRLEN
];
305 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
307 // iface DUID iaid hostname lifetime assigned length [addrs...]
308 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s ipv4 %s %u %x 32 ",
309 iface
->ifname
, duidbuf
,
310 (c
->hostname
? c
->hostname
: "-"),
311 (unsigned)(c
->valid_until
> now
?
312 (c
->valid_until
- now
+ wall_time
) : 0),
315 struct in_addr addr
= {htonl(c
->addr
)};
316 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
318 if (c
->hostname
[0]) {
322 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
323 iface
->search
, b
, sizeof(b
)) > 0)
324 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
326 fprintf(fp
, "\t%s\n", c
->hostname
);
327 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
328 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
331 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/32 ", ipbuf
);
332 leasebuf
[l
- 1] = '\n';
333 fwrite(leasebuf
, 1, l
, fp
);
342 md5_end(newmd5
, &md5
);
344 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
345 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
346 char *argv
[2] = {config
.dhcp_cb
, NULL
};
348 execv(argv
[0], argv
);
355 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
357 if (a
->length
> 64 || a
->managed_size
< 0)
360 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
361 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
363 for (size_t i
= 0; i
< addrlen
; ++i
) {
364 struct in6_addr prefix
= addrs
[i
].addr
;
365 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
366 odhcpd_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
367 iface
, &a
->peer
.sin6_addr
, 1024, add
);
372 // More data was received from TCP connection
373 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
375 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
376 time_t now
= odhcpd_time();
377 bool first
= c
->managed_size
< 0;
381 char *data
= ustream_get_read_buf(s
, &pending
);
382 char *end
= memmem(data
, pending
, "\n\n", 2);
391 if (c
->accept_reconf
)
395 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
396 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
397 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
399 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
400 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
403 x
= strtok_r(NULL
, ",", &saveptr2
);
404 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
407 x
= strtok_r(NULL
, ",", &saveptr2
);
408 if (sscanf(x
, "%u", &n
->preferred
) < 1)
411 x
= strtok_r(NULL
, ",", &saveptr2
);
412 if (sscanf(x
, "%u", &n
->valid
) < 1)
415 if (n
->preferred
> n
->valid
)
418 if (UINT32_MAX
- now
< n
->preferred
)
419 n
->preferred
= UINT32_MAX
;
423 if (UINT32_MAX
- now
< n
->valid
)
424 n
->valid
= UINT32_MAX
;
433 ustream_consume(s
, end
- data
);
436 if (first
&& c
->managed_size
== 0)
437 free_dhcpv6_assignment(c
);
439 c
->valid_until
= now
+ 150;
443 // TCP transmission has ended, either because of success or timeout or other error
444 static void managed_handle_pd_done(struct ustream
*s
)
446 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
447 c
->valid_until
= odhcpd_time() + 15;
449 if (c
->accept_reconf
)
455 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
457 struct dhcpv6_assignment
*c
;
459 if (iface
->dhcpv6_pd_manager
[0]) {
460 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
463 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
465 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
466 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
467 ustream_fd_init(&assign
->managed_sock
, fd
);
468 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
469 iaidbuf
, assign
->iaid
, assign
->length
);
470 ustream_write_pending(&assign
->managed_sock
.stream
);
471 assign
->managed_size
= -1;
472 assign
->valid_until
= odhcpd_time() + 15;
473 list_add(&assign
->head
, &iface
->ia_assignments
);
475 // Wait initial period of up to 250ms for immediate assignment
476 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
478 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
480 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
485 } else if (iface
->ia_addr_len
< 1) {
489 // Try honoring the hint first
490 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
491 if (assign
->assigned
) {
492 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
493 if (c
->length
== 128 || c
->length
== 0)
496 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
497 list_add_tail(&assign
->head
, &c
->head
);
498 apply_lease(iface
, assign
, true);
502 if (c
->assigned
!= 0)
503 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
507 // Fallback to a variable assignment
509 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
510 if (c
->length
== 128 || c
->length
== 0)
513 current
= (current
+ asize
) & (~asize
);
514 if (current
+ asize
< c
->assigned
) {
515 assign
->assigned
= current
;
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
)));
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) {
560 void dhcpv6_ia_preupdate(struct interface
*iface
)
562 if (iface
->dhcpv6
!= RELAYD_SERVER
)
565 struct dhcpv6_assignment
*c
, *border
= list_last_entry(
566 &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
);
587 if (minprefix
> 32 && minprefix
<= 64)
588 border
->assigned
= 1U << (64 - minprefix
);
590 border
->assigned
= 0;
592 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
593 struct dhcpv6_assignment
*c
, *d
;
594 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
595 if (c
->clid_len
== 0 || c
->valid_until
< now
|| c
->managed_size
)
598 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
599 list_move(&c
->head
, &reassign
);
600 else if (c
!= border
)
601 apply_lease(iface
, c
, true);
603 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
605 c
->reconf_sent
= now
;
606 send_reconf(iface
, c
);
608 // Leave all other assignments of that client alone
609 struct dhcpv6_assignment
*a
;
610 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
611 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
612 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
613 c
->reconf_cnt
= INT_MAX
;
617 while (!list_empty(&reassign
)) {
618 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
620 if (!assign_pd(iface
, c
)) {
622 list_add(&c
->head
, &iface
->ia_assignments
);
626 dhcpv6_write_statefile();
630 static void reconf_timer(struct uloop_timeout
*event
)
632 time_t now
= odhcpd_time();
633 struct interface
*iface
;
634 list_for_each_entry(iface
, &interfaces
, head
) {
635 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
638 struct dhcpv6_assignment
*a
, *n
;
639 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
640 if (a
->valid_until
< now
) {
641 if ((a
->length
< 128 && a
->clid_len
> 0) ||
642 (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);
658 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
659 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
660 struct interface
*iface
, bool request
)
662 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
665 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
666 size_t datalen
= sizeof(out
);
667 time_t now
= odhcpd_time();
670 struct __attribute__((packed
)) {
674 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
677 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
678 datalen
+= sizeof(stat
);
682 if (a
->leasetime
> 0) {
683 leasetime
= a
->leasetime
;
685 leasetime
= iface
->dhcpv4_leasetime
;
689 else if (leasetime
< 60)
692 uint32_t pref
= leasetime
;
693 uint32_t valid
= leasetime
;
695 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
696 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
699 for (size_t i
= 0; i
< addrlen
; ++i
)
700 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
701 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
702 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
705 for (size_t i
= 0; i
< addrlen
; ++i
) {
706 uint32_t prefix_pref
= addrs
[i
].preferred
;
707 uint32_t prefix_valid
= addrs
[i
].valid
;
709 if (addrs
[i
].prefix
> 96 ||
710 addrs
[i
].preferred
<= (uint32_t)now
)
713 if (prefix_pref
!= UINT32_MAX
)
716 if (prefix_valid
!= UINT32_MAX
)
719 if (a
->length
< 128) {
720 struct dhcpv6_ia_prefix p
= {
721 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
722 .len
= htons(sizeof(p
) - 4),
723 .preferred
= htonl(prefix_pref
),
724 .valid
= htonl(prefix_valid
),
725 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
726 .addr
= addrs
[i
].addr
728 p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
730 size_t entrlen
= sizeof(p
) - 4;
732 if (datalen
+ entrlen
+ 4 > buflen
||
733 (a
->assigned
== 0 && a
->managed_size
== 0) ||
734 (!a
->managed_size
&& a
->length
<= addrs
[i
].prefix
))
737 memcpy(buf
+ datalen
, &p
, sizeof(p
));
738 datalen
+= entrlen
+ 4;
740 struct dhcpv6_ia_addr n
= {
741 .type
= htons(DHCPV6_OPT_IA_ADDR
),
742 .len
= htons(sizeof(n
) - 4),
743 .addr
= addrs
[i
].addr
,
744 .preferred
= htonl(prefix_pref
),
745 .valid
= htonl(prefix_valid
)
747 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
748 size_t entrlen
= sizeof(n
) - 4;
750 if (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
751 addrs
[i
].prefix
<= 64)
754 if (datalen
+ entrlen
+ 4 > buflen
|| a
->assigned
== 0)
757 memcpy(buf
+ datalen
, &n
, sizeof(n
));
758 datalen
+= entrlen
+ 4;
761 // Calculate T1 / T2 based on non-deprecated addresses
762 if (prefix_pref
> 0) {
763 if (prefix_pref
< pref
)
766 if (prefix_valid
< valid
)
767 valid
= prefix_valid
;
771 a
->valid_until
= valid
+ now
;
772 out
.t1
= htonl(pref
* 5 / 10);
773 out
.t2
= htonl(pref
* 8 / 10);
783 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
784 uint16_t otype
, olen
;
785 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
786 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
787 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
788 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
789 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
794 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
795 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
797 for (size_t i
= 0; i
< addrlen
; ++i
) {
798 if (addrs
[i
].prefix
> 96 ||
799 addrs
[i
].preferred
<= (uint32_t)now
)
802 struct in6_addr addr
= addrs
[i
].addr
;
803 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
804 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
806 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
807 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
810 addr
.s6_addr32
[3] = htonl(a
->assigned
);
812 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
819 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
820 struct dhcpv6_ia_prefix inv
= {
821 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
822 .len
= htons(sizeof(inv
) - 4),
829 if (datalen
+ sizeof(inv
) > buflen
)
832 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
833 datalen
+= sizeof(inv
);
835 struct dhcpv6_ia_addr inv
= {
836 .type
= htons(DHCPV6_OPT_IA_ADDR
),
837 .len
= htons(sizeof(inv
) - 4),
843 if (datalen
+ sizeof(inv
) > buflen
)
846 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
847 datalen
+= sizeof(inv
);
854 out
.len
= htons(datalen
- 4);
855 memcpy(buf
, &out
, sizeof(out
));
860 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
861 const char *duidbuf
, bool is_pd
, struct dhcpv6_assignment
*a
, int code
)
863 const char *type
= "UNKNOWN";
864 const char *status
= "UNKNOWN";
866 if (msgtype
== DHCPV6_MSG_RENEW
)
870 case DHCPV6_MSG_SOLICIT
:
873 case DHCPV6_MSG_REQUEST
:
876 case DHCPV6_MSG_CONFIRM
:
879 case DHCPV6_MSG_RENEW
:
882 case DHCPV6_MSG_REBIND
:
885 case DHCPV6_MSG_RELEASE
:
888 case DHCPV6_MSG_DECLINE
:
894 case DHCPV6_STATUS_OK
:
897 case DHCPV6_STATUS_NOADDRSAVAIL
:
898 status
= "no addresses available";
900 case DHCPV6_STATUS_NOBINDING
:
901 status
= "no binding";
903 case DHCPV6_STATUS_NOTONLINK
:
904 status
= "not on-link";
906 case DHCPV6_STATUS_NOPREFIXAVAIL
:
907 status
= "no prefix available";
911 char leasebuf
[256] = "";
914 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
915 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
917 char addrbuf
[INET6_ADDRSTRLEN
];
919 for (size_t i
= 0; i
< addrlen
; ++i
) {
920 if (addrs
[i
].prefix
> 96 || addrs
[i
].preferred
<= (uint32_t)now
)
923 struct in6_addr addr
= addrs
[i
].addr
;
924 int prefix
= a
->managed
? addrs
[i
].prefix
: a
->length
;
926 addr
.s6_addr32
[3] = htonl(a
->assigned
);
928 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
930 inet_ntop(AF_INET6
, &addr
, addrbuf
, sizeof(addrbuf
));
931 lbsize
+= snprintf(leasebuf
+ lbsize
, sizeof(leasebuf
) - lbsize
, "%s/%d ", addrbuf
, prefix
);
935 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
936 duidbuf
, iface
->ifname
, status
, leasebuf
);
941 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
942 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
944 time_t now
= odhcpd_time();
945 size_t response_len
= 0;
946 const struct dhcpv6_client_header
*hdr
= data
;
947 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
948 uint16_t otype
, olen
;
950 // Find and parse client-id and hostname
951 bool accept_reconf
= false;
952 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
954 size_t hostname_len
= 0;
955 bool notonlink
= false;
958 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
959 if (otype
== DHCPV6_OPT_CLIENTID
) {
963 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
964 memcpy(mac
, &odata
[8], sizeof(mac
));
965 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
966 memcpy(mac
, &odata
[4], sizeof(mac
));
969 odhcpd_hexlify(duidbuf
, odata
, olen
);
970 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
971 uint8_t fqdn_buf
[256];
972 memcpy(fqdn_buf
, odata
, olen
);
973 fqdn_buf
[olen
++] = 0;
975 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
976 hostname_len
= strcspn(hostname
, ".");
977 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
) {
978 accept_reconf
= true;
982 if (!clid_data
|| !clid_len
|| clid_len
> 130)
985 struct dhcpv6_assignment
*first
= NULL
;
986 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
987 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
988 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
989 bool ia_addr_present
= false;
990 if (!is_pd
&& !is_na
)
993 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
994 size_t ia_response_len
= 0;
995 uint8_t reqlen
= (is_pd
) ? 62 : 128;
996 uint32_t reqhint
= 0;
998 // Parse request hint for IA-PD
1001 uint16_t stype
, slen
;
1002 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1003 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1006 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1009 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1010 if (reqlen
> 32 && reqlen
<= 64)
1011 reqhint
&= (1U << (64 - reqlen
)) - 1;
1019 uint16_t stype
, slen
;
1020 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1021 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1024 ia_addr_present
= true;
1029 struct dhcpv6_assignment
*c
, *a
= NULL
;
1030 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1031 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1032 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1033 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1034 (c
->iaid
== ia
->iaid
|| c
->valid_until
< now
) &&
1035 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1039 apply_lease(iface
, a
, false);
1040 memcpy(a
->clid_data
, clid_data
, clid_len
);
1041 a
->clid_len
= clid_len
;
1050 // Generic message handling
1051 uint16_t status
= DHCPV6_STATUS_OK
;
1052 if (a
&& a
->managed_size
< 0) {
1054 } else if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1055 bool assigned
= !!a
;
1057 if (!a
&& !iface
->no_dynamic_dhcp
) { // Create new binding
1058 a
= calloc(1, sizeof(*a
) + clid_len
);
1060 a
->clid_len
= clid_len
;
1064 a
->assigned
= reqhint
;
1067 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1069 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1070 memcpy(a
->clid_data
, clid_data
, clid_len
);
1073 while (!(assigned
= assign_pd(iface
, a
)) &&
1074 !a
->managed_size
&& ++a
->length
<= 64);
1076 assigned
= assign_na(iface
, a
);
1078 if (a
->managed_size
&& !assigned
)
1083 if (!assigned
|| iface
->ia_addr_len
== 0) { // Set error status
1084 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1085 } else if (assigned
&& !first
) { //
1086 size_t handshake_len
= 4;
1088 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1092 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1093 struct dhcpv6_auth_reconfigure auth
= {
1094 htons(DHCPV6_OPT_AUTH
),
1095 htons(sizeof(auth
) - 4),
1097 {htonl(time(NULL
)), htonl(++serial
)},
1101 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1102 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1103 handshake_len
+= sizeof(auth
);
1106 buf
+= handshake_len
;
1107 buflen
-= handshake_len
;
1108 response_len
+= handshake_len
;
1113 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1115 // Was only a solicitation: mark binding for removal
1116 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1118 } else if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1119 if (hostname_len
> 0) {
1120 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1122 memcpy(a
->hostname
, hostname
, hostname_len
);
1123 a
->hostname
[hostname_len
] = 0;
1126 a
->accept_reconf
= accept_reconf
;
1127 apply_lease(iface
, a
, true);
1128 } else if (!assigned
&& a
&& a
->managed_size
== 0) { // Cleanup failed assignment
1129 free_dhcpv6_assignment(a
);
1131 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1132 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1133 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1134 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1135 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1136 status
= DHCPV6_STATUS_NOBINDING
;
1137 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1138 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1139 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1140 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1142 apply_lease(iface
, a
, true);
1143 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1145 apply_lease(iface
, a
, false);
1146 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1148 a
->valid_until
= now
+ 3600; // Block address for 1h
1150 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1151 // Send NOTONLINK for CONFIRM with addr present so that clients restart connection
1152 status
= DHCPV6_STATUS_NOTONLINK
;
1153 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1157 buf
+= ia_response_len
;
1158 buflen
-= ia_response_len
;
1159 response_len
+= ia_response_len
;
1160 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1163 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1164 response_len
+ 6 < buflen
) {
1166 buf
[1] = DHCPV6_OPT_STATUS
;
1170 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1174 dhcpv6_write_statefile();
1177 return response_len
;