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);
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
;
258 for (size_t i
= 0; i
< addrlen
; ++i
) {
259 if (addrs
[i
].prefix
> 96)
261 if (c
->valid_until
<= now
)
264 addr
= addrs
[i
].addr
;
265 if (c
->length
== 128)
266 addr
.s6_addr32
[3] = htonl(c
->assigned
);
268 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
270 inet_ntop(AF_INET6
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
272 if (c
->length
== 128 && c
->hostname
) {
276 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
277 iface
->search
, b
, sizeof(b
)) > 0)
278 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
280 fprintf(fp
, "\t%s\n", c
->hostname
);
281 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
282 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
285 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/%d ", ipbuf
,
286 (c
->managed_size
) ? addrs
[i
].prefix
: c
->length
);
288 leasebuf
[l
- 1] = '\n';
289 fwrite(leasebuf
, 1, l
, fp
);
293 if (iface
->dhcpv4
== RELAYD_SERVER
&& iface
->dhcpv4_assignments
.next
) {
294 struct dhcpv4_assignment
*c
;
295 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
296 char ipbuf
[INET6_ADDRSTRLEN
];
299 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
301 // iface DUID iaid hostname lifetime assigned length [addrs...]
302 int l
= snprintf(leasebuf
, sizeof(leasebuf
), "# %s %s ipv4 %s %u %x 32 ",
303 iface
->ifname
, duidbuf
,
304 (c
->hostname
? c
->hostname
: "-"),
305 (unsigned)(c
->valid_until
> now
?
306 (c
->valid_until
- now
+ wall_time
) : 0),
309 struct in_addr addr
= {htonl(c
->addr
)};
310 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
312 if (c
->hostname
[0]) {
316 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
317 iface
->search
, b
, sizeof(b
)) > 0)
318 fprintf(fp
, "\t%s.%s", c
->hostname
, b
);
320 fprintf(fp
, "\t%s\n", c
->hostname
);
321 md5_hash(ipbuf
, strlen(ipbuf
), &md5
);
322 md5_hash(c
->hostname
, strlen(c
->hostname
), &md5
);
325 l
+= snprintf(leasebuf
+ l
, sizeof(leasebuf
) - l
, "%s/32 ", ipbuf
);
326 leasebuf
[l
- 1] = '\n';
327 fwrite(leasebuf
, 1, l
, fp
);
336 md5_end(newmd5
, &md5
);
338 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
339 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
340 char *argv
[2] = {config
.dhcp_cb
, NULL
};
342 execv(argv
[0], argv
);
349 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
351 if (a
->length
> 64 || a
->managed_size
< 0)
354 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
355 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
357 for (size_t i
= 0; i
< addrlen
; ++i
) {
358 struct in6_addr prefix
= addrs
[i
].addr
;
359 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
360 odhcpd_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
361 iface
, &a
->peer
.sin6_addr
, add
);
366 // More data was received from TCP connection
367 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
369 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
370 time_t now
= odhcpd_time();
371 bool first
= c
->managed_size
< 0;
375 char *data
= ustream_get_read_buf(s
, &pending
);
376 char *end
= memmem(data
, pending
, "\n\n", 2);
385 if (c
->accept_reconf
)
389 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
390 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
391 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
393 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
394 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
397 x
= strtok_r(NULL
, ",", &saveptr2
);
398 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
401 x
= strtok_r(NULL
, ",", &saveptr2
);
402 if (sscanf(x
, "%u", &n
->preferred
) < 1)
405 x
= strtok_r(NULL
, ",", &saveptr2
);
406 if (sscanf(x
, "%u", &n
->valid
) < 1)
409 if (n
->preferred
> n
->valid
)
412 if (UINT32_MAX
- now
< n
->preferred
)
413 n
->preferred
= UINT32_MAX
;
417 if (UINT32_MAX
- now
< n
->valid
)
418 n
->valid
= UINT32_MAX
;
427 ustream_consume(s
, end
- data
);
430 if (first
&& c
->managed_size
== 0)
431 free_dhcpv6_assignment(c
);
433 c
->valid_until
= now
+ 150;
437 // TCP transmission has ended, either because of success or timeout or other error
438 static void managed_handle_pd_done(struct ustream
*s
)
440 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
441 c
->valid_until
= odhcpd_time() + 15;
443 if (c
->accept_reconf
)
449 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
451 struct dhcpv6_assignment
*c
;
453 if (iface
->dhcpv6_pd_manager
[0]) {
454 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
457 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
459 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
460 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
461 ustream_fd_init(&assign
->managed_sock
, fd
);
462 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
463 iaidbuf
, assign
->iaid
, assign
->length
);
464 ustream_write_pending(&assign
->managed_sock
.stream
);
465 assign
->managed_size
= -1;
466 assign
->valid_until
= odhcpd_time() + 15;
467 list_add(&assign
->head
, &iface
->ia_assignments
);
469 // Wait initial period of up to 250ms for immediate assignment
470 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
472 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
474 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
479 } else if (iface
->ia_addr_len
< 1) {
483 // Try honoring the hint first
484 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
485 if (assign
->assigned
) {
486 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
487 if (c
->length
== 128 || c
->length
== 0)
490 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
491 list_add_tail(&assign
->head
, &c
->head
);
492 apply_lease(iface
, assign
, true);
496 if (c
->assigned
!= 0)
497 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
501 // Fallback to a variable assignment
503 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
504 if (c
->length
== 128 || c
->length
== 0)
507 current
= (current
+ asize
) & (~asize
);
508 if (current
+ asize
< c
->assigned
) {
509 assign
->assigned
= current
;
510 list_add_tail(&assign
->head
, &c
->head
);
511 apply_lease(iface
, assign
, true);
515 if (c
->assigned
!= 0)
516 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
523 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
525 // Seed RNG with checksum of DUID
527 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
528 seed
+= assign
->clid_data
[i
];
531 // Try to assign up to 100x
532 for (size_t i
= 0; i
< 100; ++i
) {
534 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
536 struct dhcpv6_assignment
*c
;
537 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
541 if (c
->assigned
> try || c
->length
!= 128) {
542 assign
->assigned
= try;
543 list_add_tail(&assign
->head
, &c
->head
);
545 } else if (c
->assigned
== try) {
555 static int prefixcmp(const void *va
, const void *vb
)
557 const struct odhcpd_ipaddr
*a
= va
, *b
= vb
;
558 uint32_t a_pref
= ((a
->addr
.s6_addr
[0] & 0xfe) != 0xfc) ? a
->preferred
: 1;
559 uint32_t b_pref
= ((b
->addr
.s6_addr
[0] & 0xfe) != 0xfc) ? b
->preferred
: 1;
560 return (a_pref
< b_pref
) ? 1 : (a_pref
> b_pref
) ? -1 : 0;
564 static void update(struct interface
*iface
)
566 struct odhcpd_ipaddr addr
[8];
567 memset(addr
, 0, sizeof(addr
));
568 int len
= odhcpd_get_interface_addresses(iface
->ifindex
, addr
, 8);
573 qsort(addr
, len
, sizeof(*addr
), prefixcmp
);
575 time_t now
= odhcpd_time();
578 for (int i
= 0; i
< len
; ++i
) {
579 if (addr
[i
].preferred
> 0 && addr
[i
].prefix
> minprefix
)
580 minprefix
= addr
[i
].prefix
;
582 addr
[i
].addr
.s6_addr32
[3] = 0;
584 if (addr
[i
].preferred
< UINT32_MAX
- now
)
585 addr
[i
].preferred
+= now
;
587 if (addr
[i
].valid
< UINT32_MAX
- now
)
588 addr
[i
].valid
+= now
;
591 struct dhcpv6_assignment
*border
= list_last_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
592 border
->assigned
= 1 << (64 - minprefix
);
594 bool change
= len
!= (int)iface
->ia_addr_len
;
595 for (int i
= 0; !change
&& i
< len
; ++i
)
596 if (addr
[i
].addr
.s6_addr32
[0] != iface
->ia_addr
[i
].addr
.s6_addr32
[0] ||
597 addr
[i
].addr
.s6_addr32
[1] != iface
->ia_addr
[i
].addr
.s6_addr32
[1] ||
598 (addr
[i
].preferred
> 0) != (iface
->ia_addr
[i
].preferred
> 0) ||
599 (addr
[i
].valid
> (uint32_t)now
+ 7200) !=
600 (iface
->ia_addr
[i
].valid
> (uint32_t)now
+ 7200))
604 struct dhcpv6_assignment
*c
;
605 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
606 if (c
!= border
&& !iface
->managed
)
607 apply_lease(iface
, c
, false);
610 memcpy(iface
->ia_addr
, addr
, len
* sizeof(*addr
));
611 iface
->ia_addr_len
= len
;
613 if (change
) { // Addresses / prefixes have changed
614 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
615 struct dhcpv6_assignment
*c
, *d
;
616 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
617 if (c
->clid_len
== 0 || c
->valid_until
< now
|| c
->managed_size
)
620 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
621 list_move(&c
->head
, &reassign
);
622 else if (c
!= border
)
623 apply_lease(iface
, c
, true);
625 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
627 c
->reconf_sent
= now
;
628 send_reconf(iface
, c
);
630 // Leave all other assignments of that client alone
631 struct dhcpv6_assignment
*a
;
632 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
633 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
634 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
635 c
->reconf_cnt
= INT_MAX
;
639 while (!list_empty(&reassign
)) {
640 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
642 if (!assign_pd(iface
, c
)) {
644 list_add(&c
->head
, &iface
->ia_assignments
);
648 dhcpv6_write_statefile();
653 static void reconf_timer(struct uloop_timeout
*event
)
655 time_t now
= odhcpd_time();
656 struct interface
*iface
;
657 list_for_each_entry(iface
, &interfaces
, head
) {
658 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
661 struct dhcpv6_assignment
*a
, *n
;
662 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
663 if (a
->valid_until
< now
) {
664 if ((a
->length
< 128 && a
->clid_len
> 0) ||
665 (a
->length
== 128 && a
->clid_len
== 0)) {
667 free_dhcpv6_assignment(a
);
669 } else if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< 8 &&
670 now
> a
->reconf_sent
+ (1 << a
->reconf_cnt
)) {
672 a
->reconf_sent
= now
;
673 send_reconf(iface
, a
);
677 uloop_timeout_set(event
, 2000);
681 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
682 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
683 struct interface
*iface
, bool request
)
685 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
688 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
689 size_t datalen
= sizeof(out
);
690 time_t now
= odhcpd_time();
693 struct __attribute__((packed
)) {
697 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
700 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
701 datalen
+= sizeof(stat
);
704 uint32_t leasetime
= iface
->dhcpv4_leasetime
;
707 else if (leasetime
< 60)
710 uint32_t pref
= leasetime
;
711 uint32_t valid
= leasetime
;
713 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
714 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
717 for (size_t i
= 0; i
< addrlen
; ++i
)
718 if (addrs
[i
].preferred
> addrs
[mostpref
].preferred
)
721 for (size_t i
= 0; i
< addrlen
; ++i
) {
722 uint32_t prefix_pref
= addrs
[i
].preferred
- now
;
723 uint32_t prefix_valid
= addrs
[i
].valid
- now
;
725 if (addrs
[i
].prefix
> 96 ||
726 addrs
[i
].preferred
<= (uint32_t)now
)
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))
746 memcpy(buf
+ datalen
, &p
, sizeof(p
));
747 datalen
+= entrlen
+ 4;
749 struct dhcpv6_ia_addr n
= {
750 .type
= htons(DHCPV6_OPT_IA_ADDR
),
751 .len
= htons(sizeof(n
) - 4),
752 .addr
= addrs
[i
].addr
,
753 .preferred
= htonl(prefix_pref
),
754 .valid
= htonl(prefix_valid
)
756 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
757 size_t entrlen
= sizeof(n
) - 4;
759 if (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& i
!= mostpref
)
762 if (datalen
+ entrlen
+ 4 > buflen
|| a
->assigned
== 0)
765 memcpy(buf
+ datalen
, &n
, sizeof(n
));
766 datalen
+= entrlen
+ 4;
769 // Calculate T1 / T2 based on non-deprecated addresses
770 if (prefix_pref
> 0) {
771 if (prefix_pref
< pref
)
774 if (prefix_valid
< valid
)
775 valid
= prefix_valid
;
779 a
->valid_until
= valid
+ now
;
780 out
.t1
= htonl(pref
* 5 / 10);
781 out
.t2
= htonl(pref
* 8 / 10);
791 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
792 uint16_t otype
, olen
;
793 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
794 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
795 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
796 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
797 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
802 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
803 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
805 for (size_t i
= 0; i
< addrlen
; ++i
) {
806 if (addrs
[i
].prefix
> 96 ||
807 addrs
[i
].preferred
<= (uint32_t)now
)
810 struct in6_addr addr
= addrs
[i
].addr
;
811 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
812 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
814 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
815 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
818 addr
.s6_addr32
[3] = htonl(a
->assigned
);
820 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
827 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
828 struct dhcpv6_ia_prefix inv
= {
829 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
830 .len
= htons(sizeof(inv
) - 4),
837 if (datalen
+ sizeof(inv
) > buflen
)
840 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
841 datalen
+= sizeof(inv
);
843 struct dhcpv6_ia_addr inv
= {
844 .type
= htons(DHCPV6_OPT_IA_ADDR
),
845 .len
= htons(sizeof(inv
) - 4),
851 if (datalen
+ sizeof(inv
) > buflen
)
854 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
855 datalen
+= sizeof(inv
);
862 out
.len
= htons(datalen
- 4);
863 memcpy(buf
, &out
, sizeof(out
));
868 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
869 const char *duidbuf
, bool is_pd
, struct dhcpv6_assignment
*a
, int code
)
871 const char *type
= "UNKNOWN";
872 const char *status
= "UNKNOWN";
874 if (msgtype
== DHCPV6_MSG_RENEW
)
878 case DHCPV6_MSG_SOLICIT
:
881 case DHCPV6_MSG_REQUEST
:
884 case DHCPV6_MSG_CONFIRM
:
887 case DHCPV6_MSG_RENEW
:
890 case DHCPV6_MSG_REBIND
:
893 case DHCPV6_MSG_RELEASE
:
896 case DHCPV6_MSG_DECLINE
:
902 case DHCPV6_STATUS_OK
:
905 case DHCPV6_STATUS_NOADDRSAVAIL
:
906 status
= "no addresses available";
908 case DHCPV6_STATUS_NOBINDING
:
909 status
= "no binding";
911 case DHCPV6_STATUS_NOTONLINK
:
912 status
= "not on-link";
914 case DHCPV6_STATUS_NOPREFIXAVAIL
:
915 status
= "no prefix available";
919 char leasebuf
[256] = "";
922 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
923 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
925 char addrbuf
[INET6_ADDRSTRLEN
];
927 for (size_t i
= 0; i
< addrlen
; ++i
) {
928 if (addrs
[i
].prefix
> 96 || addrs
[i
].preferred
<= (uint32_t)now
)
931 struct in6_addr addr
= addrs
[i
].addr
;
932 int prefix
= a
->managed
? addrs
[i
].prefix
: a
->length
;
934 addr
.s6_addr32
[3] = htonl(a
->assigned
);
936 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
938 inet_ntop(AF_INET6
, &addr
, addrbuf
, sizeof(addrbuf
));
939 lbsize
+= snprintf(leasebuf
+ lbsize
, sizeof(leasebuf
) - lbsize
, "%s/%d ", addrbuf
, prefix
);
943 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
944 duidbuf
, iface
->ifname
, status
, leasebuf
);
949 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
950 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
952 time_t now
= odhcpd_time();
953 size_t response_len
= 0;
954 const struct dhcpv6_client_header
*hdr
= data
;
955 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
956 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;
990 if (!clid_data
|| !clid_len
|| clid_len
> 130)
995 struct dhcpv6_assignment
*first
= NULL
;
996 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
997 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
998 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
999 bool ia_addr_present
= false;
1000 if (!is_pd
&& !is_na
)
1003 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1004 size_t ia_response_len
= 0;
1005 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1006 uint32_t reqhint
= 0;
1008 // Parse request hint for IA-PD
1011 uint16_t stype
, slen
;
1012 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1013 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1016 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1019 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1020 if (reqlen
> 32 && reqlen
<= 64)
1021 reqhint
&= (1U << (64 - reqlen
)) - 1;
1029 uint16_t stype
, slen
;
1030 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1031 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1034 ia_addr_present
= true;
1039 struct dhcpv6_assignment
*c
, *a
= NULL
;
1040 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1041 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1042 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1043 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1044 (c
->iaid
== ia
->iaid
|| c
->valid_until
< now
) &&
1045 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1049 apply_lease(iface
, a
, false);
1050 memcpy(a
->clid_data
, clid_data
, clid_len
);
1051 a
->clid_len
= clid_len
;
1060 // Generic message handling
1061 uint16_t status
= DHCPV6_STATUS_OK
;
1062 if (a
&& a
->managed_size
< 0) {
1064 } else if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1065 bool assigned
= !!a
;
1067 if (!a
&& !iface
->no_dynamic_dhcp
) { // Create new binding
1068 a
= calloc(1, sizeof(*a
) + clid_len
);
1070 a
->clid_len
= clid_len
;
1074 a
->assigned
= reqhint
;
1077 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1079 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1080 memcpy(a
->clid_data
, clid_data
, clid_len
);
1083 while (!(assigned
= assign_pd(iface
, a
)) &&
1084 !a
->managed_size
&& ++a
->length
<= 64);
1086 assigned
= assign_na(iface
, a
);
1088 if (a
->managed_size
&& !assigned
)
1093 if (!assigned
|| iface
->ia_addr_len
== 0) { // Set error status
1094 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1095 } else if (assigned
&& !first
) { //
1096 size_t handshake_len
= 4;
1098 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1102 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1103 struct dhcpv6_auth_reconfigure auth
= {
1104 htons(DHCPV6_OPT_AUTH
),
1105 htons(sizeof(auth
) - 4),
1107 {htonl(time(NULL
)), htonl(++serial
)},
1111 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1112 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1113 handshake_len
+= sizeof(auth
);
1116 buf
+= handshake_len
;
1117 buflen
-= handshake_len
;
1118 response_len
+= handshake_len
;
1123 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1125 // Was only a solicitation: mark binding for removal
1126 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1128 } else if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1129 if (hostname_len
> 0) {
1130 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1132 memcpy(a
->hostname
, hostname
, hostname_len
);
1133 a
->hostname
[hostname_len
] = 0;
1136 a
->accept_reconf
= accept_reconf
;
1137 apply_lease(iface
, a
, true);
1138 } else if (!assigned
&& a
&& a
->managed_size
== 0) { // Cleanup failed assignment
1139 free_dhcpv6_assignment(a
);
1141 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1142 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1143 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1144 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1145 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1146 status
= DHCPV6_STATUS_NOBINDING
;
1147 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1148 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1149 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1150 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1152 apply_lease(iface
, a
, true);
1153 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1155 apply_lease(iface
, a
, false);
1156 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1158 a
->valid_until
= now
+ 3600; // Block address for 1h
1160 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1161 // Send NOTONLINK for CONFIRM with addr present so that clients restart connection
1162 status
= DHCPV6_STATUS_NOTONLINK
;
1163 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1167 buf
+= ia_response_len
;
1168 buflen
-= ia_response_len
;
1169 response_len
+= ia_response_len
;
1170 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1173 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1174 response_len
+ 6 < buflen
) {
1176 buf
[1] = DHCPV6_OPT_STATUS
;
1180 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1184 dhcpv6_write_statefile();
1187 return response_len
;