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 update(struct interface
*iface
);
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
);
68 int setup_dhcpv6_ia_interface(struct interface
*iface
, bool enable
)
70 if (!enable
&& iface
->ia_assignments
.next
) {
71 struct dhcpv6_assignment
*c
;
72 while (!list_empty(&iface
->ia_assignments
)) {
73 c
= list_first_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
74 free_dhcpv6_assignment(c
);
78 if (iface
->dhcpv6
== RELAYD_SERVER
) {
79 if (!iface
->ia_assignments
.next
)
80 INIT_LIST_HEAD(&iface
->ia_assignments
);
82 if (list_empty(&iface
->ia_assignments
)) {
83 struct dhcpv6_assignment
*border
= calloc(1, sizeof(*border
));
85 syslog(LOG_ERR
, "Calloc failed for border on interface %s", iface
->ifname
);
90 list_add(&border
->head
, &iface
->ia_assignments
);
95 // Parse static entries
97 list_for_each_entry(lease
, &leases
, head
) {
99 struct dhcpv6_assignment
*a
= calloc(1, sizeof(*a
) + lease
->duid_len
);
101 syslog(LOG_ERR
, "Calloc failed for static lease assignment on interface %s",
106 a
->clid_len
= lease
->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
, a
->clid_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
);
146 static int send_reconf(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
149 struct dhcpv6_client_header hdr
;
153 uint16_t hardware_type
;
158 struct dhcpv6_auth_reconfigure auth
;
161 uint8_t clid_data
[128];
162 } __attribute__((packed
)) reconf_msg
= {
163 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
164 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
165 .srvid_len
= htons(10),
166 .duid_type
= htons(3),
167 .hardware_type
= htons(1),
168 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
170 .msg_id
= DHCPV6_MSG_RENEW
,
171 .auth
= {htons(DHCPV6_OPT_AUTH
),
172 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
173 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
174 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
175 .clid_len
= htons(assign
->clid_len
),
179 odhcpd_get_mac(iface
, reconf_msg
.mac
);
180 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
181 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
184 uint8_t secretbytes
[64];
185 memset(secretbytes
, 0, sizeof(secretbytes
));
186 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
188 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
189 secretbytes
[i
] ^= 0x36;
192 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
193 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
194 md5_end(reconf_msg
.auth
.key
, &md5
);
196 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
197 secretbytes
[i
] ^= 0x36;
198 secretbytes
[i
] ^= 0x5c;
202 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
203 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
204 md5_end(reconf_msg
.auth
.key
, &md5
);
206 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
210 void dhcpv6_write_statefile(void)
215 if (config
.dhcp_statefile
) {
216 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
217 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
221 lockf(fd
, F_LOCK
, 0);
222 if (ftruncate(fd
, 0) < 0) {}
224 FILE *fp
= fdopen(fd
, "w");
230 struct interface
*iface
;
231 list_for_each_entry(iface
, &interfaces
, head
) {
232 if (iface
->dhcpv6
!= RELAYD_SERVER
&& iface
->dhcpv4
!= RELAYD_SERVER
)
235 if (iface
->dhcpv6
== RELAYD_SERVER
&& iface
->ia_assignments
.next
) {
236 struct dhcpv6_assignment
*c
;
237 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
238 if (c
->clid_len
== 0 || c
->managed_size
< 0)
241 char ipbuf
[INET6_ADDRSTRLEN
];
244 odhcpd_hexlify(duidbuf
, c
->clid_data
, c
->clid_len
);
246 // iface DUID iaid hostname lifetime assigned length [addrs...]
247 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s %x %s %u %x %u ",
248 iface
->ifname
, duidbuf
, ntohl(c
->iaid
),
249 (c
->hostname
? c
->hostname
: "-"),
250 (unsigned)(c
->valid_until
> now
?
251 (c
->valid_until
- now
+ wall_time
) : 0),
252 c
->assigned
, (unsigned)c
->length
);
254 struct in6_addr addr
;
255 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->ia_addr
;
256 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->ia_addr_len
;
259 for (size_t i
= 0; i
< addrlen
; ++i
)
260 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
261 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
262 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
265 for (size_t i
= 0; i
< addrlen
; ++i
) {
266 if (addrs
[i
].prefix
> 96 || c
->valid_until
<= now
||
267 (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
268 addrs
[i
].prefix
== 64))
271 addr
= addrs
[i
].addr
;
272 if (c
->length
== 128)
273 addr
.s6_addr32
[3] = htonl(c
->assigned
);
275 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
277 inet_ntop(AF_INET6
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
279 if (c
->length
== 128 && c
->hostname
) {
283 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
284 iface
->search
, b
, sizeof(b
)) > 0)
285 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
287 fprintf(fp
, "\t%s\n", c
->hostname
);
288 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
289 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
292 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/%d ", ipbuf
,
293 (c
->managed_size
) ? addrs
[i
].prefix
: c
->length
);
295 leasebuf
[l
- 1] = '\n';
296 fwrite(leasebuf
, 1, l
, fp
);
300 if (iface
->dhcpv4
== RELAYD_SERVER
&& iface
->dhcpv4_assignments
.next
) {
301 struct dhcpv4_assignment
*c
;
302 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
303 char ipbuf
[INET6_ADDRSTRLEN
];
306 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
308 // iface DUID iaid hostname lifetime assigned length [addrs...]
309 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s ipv4 %s %u %x 32 ",
310 iface
->ifname
, duidbuf
,
311 (c
->hostname
? c
->hostname
: "-"),
312 (unsigned)(c
->valid_until
> now
?
313 (c
->valid_until
- now
+ wall_time
) : 0),
316 struct in_addr addr
= {htonl(c
->addr
)};
317 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
319 if (c
->hostname
[0]) {
323 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
324 iface
->search
, b
, sizeof(b
)) > 0)
325 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
327 fprintf(fp
, "\t%s\n", c
->hostname
);
328 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
329 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
332 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/32 ", ipbuf
);
333 leasebuf
[l
- 1] = '\n';
334 fwrite(leasebuf
, 1, l
, fp
);
343 md5_end(newmd5
, &md5
);
345 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
346 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
347 char *argv
[2] = {config
.dhcp_cb
, NULL
};
349 execv(argv
[0], argv
);
356 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
358 if (a
->length
> 64 || a
->managed_size
< 0)
361 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
362 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
364 for (size_t i
= 0; i
< addrlen
; ++i
) {
365 struct in6_addr prefix
= addrs
[i
].addr
;
366 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
367 odhcpd_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
368 iface
, &a
->peer
.sin6_addr
, add
);
373 // More data was received from TCP connection
374 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
376 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
377 time_t now
= odhcpd_time();
378 bool first
= c
->managed_size
< 0;
382 char *data
= ustream_get_read_buf(s
, &pending
);
383 char *end
= memmem(data
, pending
, "\n\n", 2);
392 if (c
->accept_reconf
)
396 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
397 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
398 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
400 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
401 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
404 x
= strtok_r(NULL
, ",", &saveptr2
);
405 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
408 x
= strtok_r(NULL
, ",", &saveptr2
);
409 if (sscanf(x
, "%u", &n
->preferred
) < 1)
412 x
= strtok_r(NULL
, ",", &saveptr2
);
413 if (sscanf(x
, "%u", &n
->valid
) < 1)
416 if (n
->preferred
> n
->valid
)
419 if (UINT32_MAX
- now
< n
->preferred
)
420 n
->preferred
= UINT32_MAX
;
424 if (UINT32_MAX
- now
< n
->valid
)
425 n
->valid
= UINT32_MAX
;
434 ustream_consume(s
, end
- data
);
437 if (first
&& c
->managed_size
== 0)
438 free_dhcpv6_assignment(c
);
440 c
->valid_until
= now
+ 150;
444 // TCP transmission has ended, either because of success or timeout or other error
445 static void managed_handle_pd_done(struct ustream
*s
)
447 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
448 c
->valid_until
= odhcpd_time() + 15;
450 if (c
->accept_reconf
)
456 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
458 struct dhcpv6_assignment
*c
;
460 if (iface
->dhcpv6_pd_manager
[0]) {
461 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
464 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
466 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
467 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
468 ustream_fd_init(&assign
->managed_sock
, fd
);
469 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
470 iaidbuf
, assign
->iaid
, assign
->length
);
471 ustream_write_pending(&assign
->managed_sock
.stream
);
472 assign
->managed_size
= -1;
473 assign
->valid_until
= odhcpd_time() + 15;
474 list_add(&assign
->head
, &iface
->ia_assignments
);
476 // Wait initial period of up to 250ms for immediate assignment
477 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
479 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
481 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
486 } 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
)));
530 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
532 // Seed RNG with checksum of DUID
534 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
535 seed
+= assign
->clid_data
[i
];
538 // Try to assign up to 100x
539 for (size_t i
= 0; i
< 100; ++i
) {
541 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
543 struct dhcpv6_assignment
*c
;
544 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
548 if (c
->assigned
> try || c
->length
!= 128) {
549 assign
->assigned
= try;
550 list_add_tail(&assign
->head
, &c
->head
);
552 } else if (c
->assigned
== try) {
562 static int prefixcmp(const void *va
, const void *vb
)
564 const struct odhcpd_ipaddr
*a
= va
, *b
= vb
;
565 uint32_t a_pref
= ((a
->addr
.s6_addr
[0] & 0xfe) != 0xfc) ? a
->preferred
: 1;
566 uint32_t b_pref
= ((b
->addr
.s6_addr
[0] & 0xfe) != 0xfc) ? b
->preferred
: 1;
567 return (a_pref
< b_pref
) ? 1 : (a_pref
> b_pref
) ? -1 : 0;
571 static void update(struct interface
*iface
)
573 struct odhcpd_ipaddr addr
[8];
574 memset(addr
, 0, sizeof(addr
));
575 int len
= odhcpd_get_interface_addresses(iface
->ifindex
, addr
, 8);
580 qsort(addr
, len
, sizeof(*addr
), prefixcmp
);
582 time_t now
= odhcpd_time();
585 for (int i
= 0; i
< len
; ++i
) {
586 if (addr
[i
].preferred
> 0 && addr
[i
].prefix
> minprefix
)
587 minprefix
= addr
[i
].prefix
;
589 addr
[i
].addr
.s6_addr32
[3] = 0;
591 if (addr
[i
].preferred
< UINT32_MAX
- now
)
592 addr
[i
].preferred
+= now
;
594 if (addr
[i
].valid
< UINT32_MAX
- now
)
595 addr
[i
].valid
+= now
;
598 struct dhcpv6_assignment
*border
= list_last_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
599 if (minprefix
> 32 && minprefix
<= 64)
600 border
->assigned
= 1U << (64 - minprefix
);
602 border
->assigned
= 0;
604 bool change
= len
!= (int)iface
->ia_addr_len
;
605 for (int i
= 0; !change
&& i
< len
; ++i
)
606 if (addr
[i
].addr
.s6_addr32
[0] != iface
->ia_addr
[i
].addr
.s6_addr32
[0] ||
607 addr
[i
].addr
.s6_addr32
[1] != iface
->ia_addr
[i
].addr
.s6_addr32
[1] ||
608 (addr
[i
].preferred
> 0) != (iface
->ia_addr
[i
].preferred
> 0) ||
609 (addr
[i
].valid
> (uint32_t)now
+ 7200) !=
610 (iface
->ia_addr
[i
].valid
> (uint32_t)now
+ 7200))
614 struct dhcpv6_assignment
*c
;
615 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
616 if (c
!= border
&& !iface
->managed
)
617 apply_lease(iface
, c
, false);
620 memcpy(iface
->ia_addr
, addr
, len
* sizeof(*addr
));
621 iface
->ia_addr_len
= len
;
623 if (change
) { // Addresses / prefixes have changed
624 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
625 struct dhcpv6_assignment
*c
, *d
;
626 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
627 if (c
->clid_len
== 0 || c
->valid_until
< now
|| c
->managed_size
)
630 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
631 list_move(&c
->head
, &reassign
);
632 else if (c
!= border
)
633 apply_lease(iface
, c
, true);
635 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
637 c
->reconf_sent
= now
;
638 send_reconf(iface
, c
);
640 // Leave all other assignments of that client alone
641 struct dhcpv6_assignment
*a
;
642 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
643 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
644 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
645 c
->reconf_cnt
= INT_MAX
;
649 while (!list_empty(&reassign
)) {
650 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
652 if (!assign_pd(iface
, c
)) {
654 list_add(&c
->head
, &iface
->ia_assignments
);
658 dhcpv6_write_statefile();
663 static void reconf_timer(struct uloop_timeout
*event
)
665 time_t now
= odhcpd_time();
666 struct interface
*iface
;
667 list_for_each_entry(iface
, &interfaces
, head
) {
668 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
671 struct dhcpv6_assignment
*a
, *n
;
672 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
673 if (a
->valid_until
< now
) {
674 if ((a
->length
< 128 && a
->clid_len
> 0) ||
675 (a
->length
== 128 && a
->clid_len
== 0)) {
677 free_dhcpv6_assignment(a
);
679 } else if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< 8 &&
680 now
> a
->reconf_sent
+ (1 << a
->reconf_cnt
)) {
682 a
->reconf_sent
= now
;
683 send_reconf(iface
, a
);
687 uloop_timeout_set(event
, 2000);
691 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
692 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
693 struct interface
*iface
, bool request
)
695 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
698 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
699 size_t datalen
= sizeof(out
);
700 time_t now
= odhcpd_time();
703 struct __attribute__((packed
)) {
707 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
710 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
711 datalen
+= sizeof(stat
);
714 uint32_t leasetime
= iface
->dhcpv4_leasetime
;
717 else if (leasetime
< 60)
720 uint32_t pref
= leasetime
;
721 uint32_t valid
= leasetime
;
723 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
724 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
727 for (size_t i
= 0; i
< addrlen
; ++i
)
728 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
729 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
730 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
733 for (size_t i
= 0; i
< addrlen
; ++i
) {
734 uint32_t prefix_pref
= addrs
[i
].preferred
;
735 uint32_t prefix_valid
= addrs
[i
].valid
;
737 if (addrs
[i
].prefix
> 96 ||
738 addrs
[i
].preferred
<= (uint32_t)now
)
741 if (prefix_pref
!= UINT32_MAX
)
744 if (prefix_valid
!= UINT32_MAX
)
747 if (a
->length
< 128) {
748 struct dhcpv6_ia_prefix p
= {
749 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
750 .len
= htons(sizeof(p
) - 4),
751 .preferred
= htonl(prefix_pref
),
752 .valid
= htonl(prefix_valid
),
753 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
754 .addr
= addrs
[i
].addr
756 p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
758 size_t entrlen
= sizeof(p
) - 4;
760 if (datalen
+ entrlen
+ 4 > buflen
||
761 (a
->assigned
== 0 && a
->managed_size
== 0))
764 memcpy(buf
+ datalen
, &p
, sizeof(p
));
765 datalen
+= entrlen
+ 4;
767 struct dhcpv6_ia_addr n
= {
768 .type
= htons(DHCPV6_OPT_IA_ADDR
),
769 .len
= htons(sizeof(n
) - 4),
770 .addr
= addrs
[i
].addr
,
771 .preferred
= htonl(prefix_pref
),
772 .valid
= htonl(prefix_valid
)
774 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
775 size_t entrlen
= sizeof(n
) - 4;
777 if (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= m
&&
778 addrs
[i
].prefix
== 64)
781 if (datalen
+ entrlen
+ 4 > buflen
|| a
->assigned
== 0)
784 memcpy(buf
+ datalen
, &n
, sizeof(n
));
785 datalen
+= entrlen
+ 4;
788 // Calculate T1 / T2 based on non-deprecated addresses
789 if (prefix_pref
> 0) {
790 if (prefix_pref
< pref
)
793 if (prefix_valid
< valid
)
794 valid
= prefix_valid
;
798 a
->valid_until
= valid
+ now
;
799 out
.t1
= htonl(pref
* 5 / 10);
800 out
.t2
= htonl(pref
* 8 / 10);
810 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
811 uint16_t otype
, olen
;
812 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
813 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
814 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
815 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
816 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
821 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
822 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
824 for (size_t i
= 0; i
< addrlen
; ++i
) {
825 if (addrs
[i
].prefix
> 96 ||
826 addrs
[i
].preferred
<= (uint32_t)now
)
829 struct in6_addr addr
= addrs
[i
].addr
;
830 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
831 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
833 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
834 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
837 addr
.s6_addr32
[3] = htonl(a
->assigned
);
839 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
846 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
847 struct dhcpv6_ia_prefix inv
= {
848 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
849 .len
= htons(sizeof(inv
) - 4),
856 if (datalen
+ sizeof(inv
) > buflen
)
859 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
860 datalen
+= sizeof(inv
);
862 struct dhcpv6_ia_addr inv
= {
863 .type
= htons(DHCPV6_OPT_IA_ADDR
),
864 .len
= htons(sizeof(inv
) - 4),
870 if (datalen
+ sizeof(inv
) > buflen
)
873 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
874 datalen
+= sizeof(inv
);
881 out
.len
= htons(datalen
- 4);
882 memcpy(buf
, &out
, sizeof(out
));
887 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
888 const char *duidbuf
, bool is_pd
, struct dhcpv6_assignment
*a
, int code
)
890 const char *type
= "UNKNOWN";
891 const char *status
= "UNKNOWN";
893 if (msgtype
== DHCPV6_MSG_RENEW
)
897 case DHCPV6_MSG_SOLICIT
:
900 case DHCPV6_MSG_REQUEST
:
903 case DHCPV6_MSG_CONFIRM
:
906 case DHCPV6_MSG_RENEW
:
909 case DHCPV6_MSG_REBIND
:
912 case DHCPV6_MSG_RELEASE
:
915 case DHCPV6_MSG_DECLINE
:
921 case DHCPV6_STATUS_OK
:
924 case DHCPV6_STATUS_NOADDRSAVAIL
:
925 status
= "no addresses available";
927 case DHCPV6_STATUS_NOBINDING
:
928 status
= "no binding";
930 case DHCPV6_STATUS_NOTONLINK
:
931 status
= "not on-link";
933 case DHCPV6_STATUS_NOPREFIXAVAIL
:
934 status
= "no prefix available";
938 char leasebuf
[256] = "";
941 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
942 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
944 char addrbuf
[INET6_ADDRSTRLEN
];
946 for (size_t i
= 0; i
< addrlen
; ++i
) {
947 if (addrs
[i
].prefix
> 96 || addrs
[i
].preferred
<= (uint32_t)now
)
950 struct in6_addr addr
= addrs
[i
].addr
;
951 int prefix
= a
->managed
? addrs
[i
].prefix
: a
->length
;
953 addr
.s6_addr32
[3] = htonl(a
->assigned
);
955 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
957 inet_ntop(AF_INET6
, &addr
, addrbuf
, sizeof(addrbuf
));
958 lbsize
+= snprintf(leasebuf
+ lbsize
, sizeof(leasebuf
) - lbsize
, "%s/%d ", addrbuf
, prefix
);
962 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
963 duidbuf
, iface
->ifname
, status
, leasebuf
);
968 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
969 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
971 time_t now
= odhcpd_time();
972 size_t response_len
= 0;
973 const struct dhcpv6_client_header
*hdr
= data
;
974 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
975 uint16_t otype
, olen
;
977 // Find and parse client-id and hostname
978 bool accept_reconf
= false;
979 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
981 size_t hostname_len
= 0;
982 bool notonlink
= false;
985 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
986 if (otype
== DHCPV6_OPT_CLIENTID
) {
990 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
991 memcpy(mac
, &odata
[8], sizeof(mac
));
992 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
993 memcpy(mac
, &odata
[4], sizeof(mac
));
996 odhcpd_hexlify(duidbuf
, odata
, olen
);
997 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
998 uint8_t fqdn_buf
[256];
999 memcpy(fqdn_buf
, odata
, olen
);
1000 fqdn_buf
[olen
++] = 0;
1002 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
1003 hostname_len
= strcspn(hostname
, ".");
1004 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
) {
1005 accept_reconf
= true;
1009 if (!clid_data
|| !clid_len
|| clid_len
> 130)
1014 struct dhcpv6_assignment
*first
= NULL
;
1015 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1016 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
1017 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
1018 bool ia_addr_present
= false;
1019 if (!is_pd
&& !is_na
)
1022 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1023 size_t ia_response_len
= 0;
1024 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1025 uint32_t reqhint
= 0;
1027 // Parse request hint for IA-PD
1030 uint16_t stype
, slen
;
1031 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1032 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1035 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1038 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1039 if (reqlen
> 32 && reqlen
<= 64)
1040 reqhint
&= (1U << (64 - reqlen
)) - 1;
1048 uint16_t stype
, slen
;
1049 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1050 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1053 ia_addr_present
= true;
1058 struct dhcpv6_assignment
*c
, *a
= NULL
;
1059 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1060 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1061 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1062 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1063 (c
->iaid
== ia
->iaid
|| c
->valid_until
< now
) &&
1064 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1068 apply_lease(iface
, a
, false);
1069 memcpy(a
->clid_data
, clid_data
, clid_len
);
1070 a
->clid_len
= clid_len
;
1079 // Generic message handling
1080 uint16_t status
= DHCPV6_STATUS_OK
;
1081 if (a
&& a
->managed_size
< 0) {
1083 } else if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1084 bool assigned
= !!a
;
1086 if (!a
&& !iface
->no_dynamic_dhcp
) { // Create new binding
1087 a
= calloc(1, sizeof(*a
) + clid_len
);
1089 a
->clid_len
= clid_len
;
1093 a
->assigned
= reqhint
;
1096 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1098 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1099 memcpy(a
->clid_data
, clid_data
, clid_len
);
1102 while (!(assigned
= assign_pd(iface
, a
)) &&
1103 !a
->managed_size
&& ++a
->length
<= 64);
1105 assigned
= assign_na(iface
, a
);
1107 if (a
->managed_size
&& !assigned
)
1112 if (!assigned
|| iface
->ia_addr_len
== 0) { // Set error status
1113 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1114 } else if (assigned
&& !first
) { //
1115 size_t handshake_len
= 4;
1117 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1121 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1122 struct dhcpv6_auth_reconfigure auth
= {
1123 htons(DHCPV6_OPT_AUTH
),
1124 htons(sizeof(auth
) - 4),
1126 {htonl(time(NULL
)), htonl(++serial
)},
1130 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1131 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1132 handshake_len
+= sizeof(auth
);
1135 buf
+= handshake_len
;
1136 buflen
-= handshake_len
;
1137 response_len
+= handshake_len
;
1142 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1144 // Was only a solicitation: mark binding for removal
1145 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1147 } else if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1148 if (hostname_len
> 0) {
1149 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1151 memcpy(a
->hostname
, hostname
, hostname_len
);
1152 a
->hostname
[hostname_len
] = 0;
1155 a
->accept_reconf
= accept_reconf
;
1156 apply_lease(iface
, a
, true);
1157 } else if (!assigned
&& a
&& a
->managed_size
== 0) { // Cleanup failed assignment
1158 free_dhcpv6_assignment(a
);
1160 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1161 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1162 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1163 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1164 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1165 status
= DHCPV6_STATUS_NOBINDING
;
1166 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1167 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1168 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1169 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1171 apply_lease(iface
, a
, true);
1172 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1174 apply_lease(iface
, a
, false);
1175 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1177 a
->valid_until
= now
+ 3600; // Block address for 1h
1179 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1180 // Send NOTONLINK for CONFIRM with addr present so that clients restart connection
1181 status
= DHCPV6_STATUS_NOTONLINK
;
1182 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1186 buf
+= ia_response_len
;
1187 buflen
-= ia_response_len
;
1188 response_len
+= ia_response_len
;
1189 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1192 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1193 response_len
+ 6 < buflen
) {
1195 buf
[1] = DHCPV6_OPT_STATUS
;
1199 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1203 dhcpv6_write_statefile();
1206 return response_len
;