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.
32 #include <arpa/inet.h>
33 #include <sys/timerfd.h>
35 #include <libubox/md5.h>
36 #include <libubox/usock.h>
38 #define ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) \
39 ((iface)->dhcpv6_assignall || (i) == (m) || \
40 (addrs)[(i)].prefix > 64)
42 static void free_dhcpv6_assignment(struct dhcpv6_assignment
*c
);
43 static void reconf_timer(struct uloop_timeout
*event
);
44 static struct uloop_timeout reconf_event
= {.cb
= reconf_timer
};
45 static uint32_t serial
= 0;
46 static uint8_t statemd5
[16];
48 int dhcpv6_ia_init(void)
50 uloop_timeout_set(&reconf_event
, 2000);
54 int setup_dhcpv6_ia_interface(struct interface
*iface
, bool enable
)
56 if (!enable
&& iface
->ia_assignments
.next
) {
57 struct dhcpv6_assignment
*c
;
59 while (!list_empty(&iface
->ia_assignments
)) {
60 c
= list_first_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
61 free_dhcpv6_assignment(c
);
65 if (enable
&& iface
->dhcpv6
== RELAYD_SERVER
) {
66 if (!iface
->ia_assignments
.next
)
67 INIT_LIST_HEAD(&iface
->ia_assignments
);
69 if (list_empty(&iface
->ia_assignments
)) {
70 struct dhcpv6_assignment
*border
= calloc(1, sizeof(*border
));
72 syslog(LOG_ERR
, "Calloc failed for border on interface %s", iface
->ifname
);
77 list_add(&border
->head
, &iface
->ia_assignments
);
80 /* Parse static entries */
82 list_for_each_entry(lease
, &leases
, head
) {
84 size_t duid_len
= lease
->duid_len
? lease
->duid_len
: 14;
85 struct dhcpv6_assignment
*a
= calloc(1, sizeof(*a
) + duid_len
);
87 syslog(LOG_ERR
, "Calloc failed for static lease assignment on interface %s",
92 a
->leasetime
= lease
->dhcpv4_leasetime
;
94 a
->clid_len
= duid_len
;
97 a
->assigned
= lease
->hostid
;
99 uint32_t i4a
= ntohl(lease
->ipaddr
.s_addr
) & 0xff;
100 a
->assigned
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
103 odhcpd_urandom(a
->key
, sizeof(a
->key
));
104 memcpy(a
->clid_data
, lease
->duid
, lease
->duid_len
);
105 memcpy(a
->mac
, lease
->mac
.ether_addr_octet
, sizeof(a
->mac
));
106 /* Static assignment */
107 a
->flags
|= OAF_STATIC
;
111 /* Assign to all interfaces */
112 struct dhcpv6_assignment
*c
;
113 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
114 if (c
->length
!= 128 || c
->assigned
> a
->assigned
) {
115 list_add_tail(&a
->head
, &c
->head
);
117 } else if (c
->assigned
== a
->assigned
)
118 /* Already an assignment with that number */
123 if (lease
->hostname
[0]) {
125 a
->hostname
= strdup(lease
->hostname
);
128 free_dhcpv6_assignment(a
);
134 static void free_dhcpv6_assignment(struct dhcpv6_assignment
*c
)
136 if (c
->managed_sock
.fd
.registered
) {
137 ustream_free(&c
->managed_sock
.stream
);
138 close(c
->managed_sock
.fd
.fd
);
149 static inline bool valid_prefix_length(const struct dhcpv6_assignment
*a
, const uint8_t prefix_length
)
151 return (a
->managed_size
|| a
->length
> prefix_length
);
154 static inline bool valid_addr(const struct odhcpd_ipaddr
*addr
, time_t now
)
156 return (addr
->prefix
<= 96 && addr
->preferred
> (uint32_t)now
);
159 static size_t get_preferred_addr(const struct odhcpd_ipaddr
*addrs
, const size_t addrlen
)
163 for (i
= 0, m
= 0; i
< addrlen
; ++i
) {
164 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
165 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
166 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
173 static int send_reconf(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
176 struct dhcpv6_client_header hdr
;
180 uint16_t hardware_type
;
185 struct dhcpv6_auth_reconfigure auth
;
188 uint8_t clid_data
[128];
189 } __attribute__((packed
)) reconf_msg
= {
190 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
191 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
192 .srvid_len
= htons(10),
193 .duid_type
= htons(3),
194 .hardware_type
= htons(1),
195 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
197 .msg_id
= DHCPV6_MSG_RENEW
,
198 .auth
= {htons(DHCPV6_OPT_AUTH
),
199 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
200 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
201 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
202 .clid_len
= htons(assign
->clid_len
),
206 odhcpd_get_mac(iface
, reconf_msg
.mac
);
207 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
208 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
211 uint8_t secretbytes
[64];
212 memset(secretbytes
, 0, sizeof(secretbytes
));
213 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
215 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
216 secretbytes
[i
] ^= 0x36;
219 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
220 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
221 md5_end(reconf_msg
.auth
.key
, &md5
);
223 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
224 secretbytes
[i
] ^= 0x36;
225 secretbytes
[i
] ^= 0x5c;
229 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
230 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
231 md5_end(reconf_msg
.auth
.key
, &md5
);
233 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
236 void dhcpv6_enum_ia_addrs(struct interface
*iface
, struct dhcpv6_assignment
*c
,
237 time_t now
, dhcpv6_binding_cb_handler_t func
, void *arg
)
239 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->ia_addr
;
240 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->ia_addr_len
;
241 size_t m
= get_preferred_addr(addrs
, addrlen
);
243 for (size_t i
= 0; i
< addrlen
; ++i
) {
244 struct in6_addr addr
;
245 uint32_t pref
, valid
;
246 int prefix
= c
->managed
? addrs
[i
].prefix
: c
->length
;
248 if (!valid_addr(&addrs
[i
], now
))
251 addr
= addrs
[i
].addr
.in6
;
252 pref
= addrs
[i
].preferred
;
253 valid
= addrs
[i
].valid
;
255 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
))
258 addr
.s6_addr32
[3] = htonl(c
->assigned
);
260 if (!valid_prefix_length(c
, addrs
[i
].prefix
))
263 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
264 addr
.s6_addr32
[2] = addr
.s6_addr32
[3] = 0;
267 if (pref
!= UINT32_MAX
)
270 if (valid
!= UINT32_MAX
)
273 func(&addr
, prefix
, pref
, valid
, arg
);
280 struct dhcpv6_assignment
*c
;
281 struct interface
*iface
;
287 void dhcpv6_write_ia_addr(struct in6_addr
*addr
, int prefix
, _unused
uint32_t pref
,
288 _unused
uint32_t valid
, void *arg
)
290 struct write_ctxt
*ctxt
= (struct write_ctxt
*)arg
;
291 char ipbuf
[INET6_ADDRSTRLEN
];
293 inet_ntop(AF_INET6
, addr
, ipbuf
, sizeof(ipbuf
) - 1);
295 if (ctxt
->c
->length
== 128 && ctxt
->c
->hostname
) {
296 fputs(ipbuf
, ctxt
->fp
);
299 if (dn_expand(ctxt
->iface
->search
, ctxt
->iface
->search
+ ctxt
->iface
->search_len
,
300 ctxt
->iface
->search
, b
, sizeof(b
)) > 0)
301 fprintf(ctxt
->fp
, "\t%s.%s", ctxt
->c
->hostname
, b
);
303 fprintf(ctxt
->fp
, "\t%s\n", ctxt
->c
->hostname
);
304 md5_hash(ipbuf
, strlen(ipbuf
), &ctxt
->md5
);
305 md5_hash(ctxt
->c
->hostname
, strlen(ctxt
->c
->hostname
), &ctxt
->md5
);
308 ctxt
->buf_idx
+= snprintf(ctxt
->buf
+ ctxt
->buf_idx
,ctxt
->buf_len
- ctxt
->buf_idx
,
309 "%s/%d ", ipbuf
, prefix
);
312 void dhcpv6_write_statefile(void)
314 struct write_ctxt ctxt
;
316 md5_begin(&ctxt
.md5
);
318 if (config
.dhcp_statefile
) {
319 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
320 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
326 ret
= lockf(fd
, F_LOCK
, 0);
331 if (ftruncate(fd
, 0) < 0) {}
333 ctxt
.fp
= fdopen(fd
, "w");
340 ctxt
.buf_len
= sizeof(leasebuf
);
342 list_for_each_entry(ctxt
.iface
, &interfaces
, head
) {
343 if (ctxt
.iface
->dhcpv6
!= RELAYD_SERVER
&&
344 ctxt
.iface
->dhcpv4
!= RELAYD_SERVER
)
347 if (ctxt
.iface
->dhcpv6
== RELAYD_SERVER
&&
348 ctxt
.iface
->ia_assignments
.next
) {
349 list_for_each_entry(ctxt
.c
, &ctxt
.iface
->ia_assignments
, head
) {
350 if (!(ctxt
.c
->flags
& OAF_BOUND
) || ctxt
.c
->managed_size
< 0)
355 odhcpd_hexlify(duidbuf
, ctxt
.c
->clid_data
, ctxt
.c
->clid_len
);
357 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
358 ctxt
.buf_idx
= snprintf(ctxt
.buf
, ctxt
.buf_len
, "# %s %s %x %s %ld %x %u ",
359 ctxt
.iface
->ifname
, duidbuf
, ntohl(ctxt
.c
->iaid
),
360 (ctxt
.c
->hostname
? ctxt
.c
->hostname
: "-"),
361 (ctxt
.c
->valid_until
> now
?
362 (ctxt
.c
->valid_until
- now
+ wall_time
) :
363 (INFINITE_VALID(ctxt
.c
->valid_until
) ? -1 : 0)),
364 ctxt
.c
->assigned
, (unsigned)ctxt
.c
->length
);
366 if (INFINITE_VALID(ctxt
.c
->valid_until
) || ctxt
.c
->valid_until
> now
)
367 dhcpv6_enum_ia_addrs(ctxt
.iface
, ctxt
.c
, now
,
368 dhcpv6_write_ia_addr
, &ctxt
);
370 ctxt
.buf
[ctxt
.buf_idx
- 1] = '\n';
371 fwrite(ctxt
.buf
, 1, ctxt
.buf_idx
, ctxt
.fp
);
375 if (ctxt
.iface
->dhcpv4
== RELAYD_SERVER
&&
376 ctxt
.iface
->dhcpv4_assignments
.next
) {
377 struct dhcpv4_assignment
*c
;
378 list_for_each_entry(c
, &ctxt
.iface
->dhcpv4_assignments
, head
) {
379 if (!(c
->flags
& OAF_BOUND
))
382 char ipbuf
[INET6_ADDRSTRLEN
];
384 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
386 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
387 ctxt
.buf_idx
= snprintf(ctxt
.buf
, ctxt
.buf_len
, "# %s %s ipv4 %s %ld %x 32 ",
388 ctxt
.iface
->ifname
, duidbuf
,
389 (c
->hostname
? c
->hostname
: "-"),
390 (c
->valid_until
> now
?
391 (c
->valid_until
- now
+ wall_time
) :
392 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
395 struct in_addr addr
= {htonl(c
->addr
)};
396 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
399 fputs(ipbuf
, ctxt
.fp
);
402 if (dn_expand(ctxt
.iface
->search
,
403 ctxt
.iface
->search
+ ctxt
.iface
->search_len
,
404 ctxt
.iface
->search
, b
, sizeof(b
)) > 0)
405 fprintf(ctxt
.fp
, "\t%s.%s", c
->hostname
, b
);
407 fprintf(ctxt
.fp
, "\t%s\n", c
->hostname
);
408 md5_hash(ipbuf
, strlen(ipbuf
), &ctxt
.md5
);
409 md5_hash(c
->hostname
, strlen(c
->hostname
), &ctxt
.md5
);
412 ctxt
.buf_idx
+= snprintf(ctxt
.buf
+ ctxt
.buf_idx
,
413 ctxt
.buf_len
- ctxt
.buf_idx
,
415 ctxt
.buf
[ctxt
.buf_idx
- 1] = '\n';
416 fwrite(ctxt
.buf
, 1, ctxt
.buf_idx
, ctxt
.fp
);
425 md5_end(newmd5
, &ctxt
.md5
);
427 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
428 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
429 char *argv
[2] = {config
.dhcp_cb
, NULL
};
431 execv(argv
[0], argv
);
438 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
440 if (a
->length
> 64 || a
->managed_size
< 0)
443 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
444 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
446 for (size_t i
= 0; i
< addrlen
; ++i
) {
447 struct in6_addr prefix
= addrs
[i
].addr
.in6
;
448 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
449 prefix
.s6_addr32
[2] = prefix
.s6_addr32
[3] = 0;
450 odhcpd_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
451 iface
, &a
->peer
.sin6_addr
, 1024, add
);
455 /* More data was received from TCP connection */
456 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
458 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
459 time_t now
= odhcpd_time();
460 bool first
= c
->managed_size
< 0;
464 char *data
= ustream_get_read_buf(s
, &pending
);
465 char *end
= memmem(data
, pending
, "\n\n", 2);
474 if (c
->accept_reconf
)
478 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
479 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
480 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
482 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
483 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
486 x
= strtok_r(NULL
, ",", &saveptr2
);
487 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
490 x
= strtok_r(NULL
, ",", &saveptr2
);
491 if (sscanf(x
, "%u", &n
->preferred
) < 1)
494 x
= strtok_r(NULL
, ",", &saveptr2
);
495 if (sscanf(x
, "%u", &n
->valid
) < 1)
498 if (n
->preferred
> n
->valid
)
501 if (UINT32_MAX
- now
< n
->preferred
)
502 n
->preferred
= UINT32_MAX
;
506 if (UINT32_MAX
- now
< n
->valid
)
507 n
->valid
= UINT32_MAX
;
516 ustream_consume(s
, end
- data
);
519 if (first
&& c
->managed_size
== 0)
520 free_dhcpv6_assignment(c
);
521 else if (first
&& !(c
->flags
& OAF_STATIC
))
522 c
->valid_until
= now
+ 150;
526 /* TCP transmission has ended, either because of success or timeout or other error */
527 static void managed_handle_pd_done(struct ustream
*s
)
529 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
531 if (!(c
->flags
& OAF_STATIC
))
532 c
->valid_until
= odhcpd_time() + 15;
536 if (c
->accept_reconf
)
540 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
542 struct dhcpv6_assignment
*c
;
544 if (iface
->dhcpv6_pd_manager
[0]) {
545 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
548 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
550 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
551 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
552 ustream_fd_init(&assign
->managed_sock
, fd
);
553 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
554 iaidbuf
, assign
->iaid
, assign
->length
);
555 ustream_write_pending(&assign
->managed_sock
.stream
);
556 assign
->managed_size
= -1;
558 if (!(assign
->flags
& OAF_STATIC
))
559 assign
->valid_until
= odhcpd_time() + 15;
561 list_add(&assign
->head
, &iface
->ia_assignments
);
563 /* Wait initial period of up to 250ms for immediate assignment */
564 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
566 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
568 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
573 } else if (iface
->ia_addr_len
< 1)
576 /* Try honoring the hint first */
577 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
578 if (assign
->assigned
) {
579 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
580 if (c
->length
== 128 || c
->length
== 0)
583 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
584 list_add_tail(&assign
->head
, &c
->head
);
586 if (assign
->flags
& OAF_BOUND
)
587 apply_lease(iface
, assign
, true);
592 if (c
->assigned
!= 0)
593 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
597 /* Fallback to a variable assignment */
599 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
600 if (c
->length
== 128 || c
->length
== 0)
603 current
= (current
+ asize
) & (~asize
);
604 if (current
+ asize
< c
->assigned
) {
605 assign
->assigned
= current
;
606 list_add_tail(&assign
->head
, &c
->head
);
608 if (assign
->flags
& OAF_BOUND
)
609 apply_lease(iface
, assign
, true);
614 if (c
->assigned
!= 0)
615 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
621 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
623 /* Seed RNG with checksum of DUID */
625 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
626 seed
+= assign
->clid_data
[i
];
629 /* Try to assign up to 100x */
630 for (size_t i
= 0; i
< 100; ++i
) {
632 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
634 struct dhcpv6_assignment
*c
;
635 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
639 if (c
->assigned
> try || c
->length
!= 128) {
640 assign
->assigned
= try;
641 list_add_tail(&assign
->head
, &c
->head
);
643 } else if (c
->assigned
== try)
651 void dhcpv6_ia_preupdate(struct interface
*iface
)
653 if (iface
->dhcpv6
!= RELAYD_SERVER
)
656 struct dhcpv6_assignment
*c
, *border
= list_last_entry(
657 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
659 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
660 if (c
!= border
&& iface
->ra_managed
== RA_MANAGED_NO_MFLAG
661 && (c
->flags
& OAF_BOUND
))
662 apply_lease(iface
, c
, false);
665 void dhcpv6_ia_postupdate(struct interface
*iface
)
667 if (iface
->dhcpv6
!= RELAYD_SERVER
)
670 time_t now
= odhcpd_time();
672 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
673 if (iface
->ia_addr
[i
].preferred
> (uint32_t)now
&&
674 iface
->ia_addr
[i
].prefix
< 64 &&
675 iface
->ia_addr
[i
].prefix
> minprefix
)
676 minprefix
= iface
->ia_addr
[i
].prefix
;
679 struct dhcpv6_assignment
*border
= list_last_entry(
680 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
682 if (minprefix
> 32 && minprefix
<= 64)
683 border
->assigned
= 1U << (64 - minprefix
);
685 border
->assigned
= 0;
687 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
688 struct dhcpv6_assignment
*c
, *d
;
689 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
690 if (c
->clid_len
== 0 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
694 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
695 list_move(&c
->head
, &reassign
);
696 else if (c
!= border
&& (c
->flags
& OAF_BOUND
))
697 apply_lease(iface
, c
, true);
699 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
701 c
->reconf_sent
= now
;
702 send_reconf(iface
, c
);
704 /* Leave all other assignments of that client alone */
705 struct dhcpv6_assignment
*a
;
706 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
707 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
708 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
709 c
->reconf_cnt
= INT_MAX
;
713 while (!list_empty(&reassign
)) {
714 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
716 if (!assign_pd(iface
, c
)) {
718 list_add(&c
->head
, &iface
->ia_assignments
);
722 dhcpv6_write_statefile();
725 static void reconf_timer(struct uloop_timeout
*event
)
727 time_t now
= odhcpd_time();
728 struct interface
*iface
;
729 list_for_each_entry(iface
, &interfaces
, head
) {
730 if (iface
->dhcpv6
!= RELAYD_SERVER
|| iface
->ia_assignments
.next
== NULL
)
733 struct dhcpv6_assignment
*a
, *n
;
734 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
735 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
) {
736 if ((a
->length
< 128 && a
->clid_len
> 0) ||
737 (a
->length
== 128 && a
->clid_len
== 0))
738 free_dhcpv6_assignment(a
);
740 } else if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< 8 &&
741 now
> a
->reconf_sent
+ (1 << a
->reconf_cnt
)) {
743 a
->reconf_sent
= now
;
744 send_reconf(iface
, a
);
748 uloop_timeout_set(event
, 2000);
751 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
752 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
753 struct interface
*iface
, bool request
)
755 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
758 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
759 size_t datalen
= sizeof(out
);
760 time_t now
= odhcpd_time();
763 struct __attribute__((packed
)) {
767 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
770 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
771 datalen
+= sizeof(stat
);
776 leasetime
= a
->leasetime
;
778 leasetime
= iface
->dhcpv4_leasetime
;
780 uint32_t pref
= leasetime
;
781 uint32_t valid
= leasetime
;
783 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
784 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
785 size_t m
= get_preferred_addr(addrs
, addrlen
);
787 for (size_t i
= 0; i
< addrlen
; ++i
) {
788 uint32_t prefix_pref
= addrs
[i
].preferred
;
789 uint32_t prefix_valid
= addrs
[i
].valid
;
791 if (!valid_addr(&addrs
[i
], now
))
794 if (prefix_pref
!= UINT32_MAX
)
797 if (prefix_valid
!= UINT32_MAX
)
800 if (a
->length
< 128) {
801 struct dhcpv6_ia_prefix p
= {
802 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
803 .len
= htons(sizeof(p
) - 4),
804 .preferred
= htonl(prefix_pref
),
805 .valid
= htonl(prefix_valid
),
806 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
807 .addr
= addrs
[i
].addr
.in6
,
809 p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
810 p
.addr
.s6_addr32
[2] = p
.addr
.s6_addr32
[3] = 0;
812 size_t entrlen
= sizeof(p
) - 4;
814 if (datalen
+ entrlen
+ 4 > buflen
||
815 (a
->assigned
== 0 && a
->managed_size
== 0) ||
816 !valid_prefix_length(a
, addrs
[i
].prefix
))
819 memcpy(buf
+ datalen
, &p
, sizeof(p
));
820 datalen
+= entrlen
+ 4;
822 struct dhcpv6_ia_addr n
= {
823 .type
= htons(DHCPV6_OPT_IA_ADDR
),
824 .len
= htons(sizeof(n
) - 4),
825 .addr
= addrs
[i
].addr
.in6
,
826 .preferred
= htonl(prefix_pref
),
827 .valid
= htonl(prefix_valid
)
829 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
830 size_t entrlen
= sizeof(n
) - 4;
832 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
) ||
834 datalen
+ entrlen
+ 4 > buflen
)
837 memcpy(buf
+ datalen
, &n
, sizeof(n
));
838 datalen
+= entrlen
+ 4;
841 /* Calculate T1 / T2 based on non-deprecated addresses */
842 if (prefix_pref
> 0) {
843 if (prefix_pref
< pref
)
846 if (prefix_valid
< valid
)
847 valid
= prefix_valid
;
851 if (!INFINITE_VALID(a
->valid_until
))
852 /* UINT32_MAX is considered as infinite leasetime */
853 a
->valid_until
= (valid
== UINT32_MAX
) ? 0 : valid
+ now
;
855 out
.t1
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 5 / 10);
856 out
.t2
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 8 / 10);
866 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
867 uint16_t otype
, olen
;
868 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
869 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
870 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
871 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
872 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
877 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
878 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
880 for (size_t i
= 0; i
< addrlen
; ++i
) {
881 if (!valid_addr(&addrs
[i
], now
))
884 struct in6_addr addr
= addrs
[i
].addr
.in6
;
885 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
886 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
887 addr
.s6_addr32
[2] = addr
.s6_addr32
[3] = 0;
889 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
890 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
893 addr
.s6_addr32
[3] = htonl(a
->assigned
);
895 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
902 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
903 struct dhcpv6_ia_prefix inv
= {
904 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
905 .len
= htons(sizeof(inv
) - 4),
912 if (datalen
+ sizeof(inv
) > buflen
)
915 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
916 datalen
+= sizeof(inv
);
918 struct dhcpv6_ia_addr inv
= {
919 .type
= htons(DHCPV6_OPT_IA_ADDR
),
920 .len
= htons(sizeof(inv
) - 4),
926 if (datalen
+ sizeof(inv
) > buflen
)
929 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
930 datalen
+= sizeof(inv
);
937 out
.len
= htons(datalen
- 4);
938 memcpy(buf
, &out
, sizeof(out
));
948 static void dhcpv6_log_ia_addr(struct in6_addr
*addr
, int prefix
, _unused
uint32_t pref
,
949 _unused
uint32_t valid
, void *arg
)
951 struct log_ctxt
*ctxt
= (struct log_ctxt
*)arg
;
952 char addrbuf
[INET6_ADDRSTRLEN
];
954 inet_ntop(AF_INET6
, addr
, addrbuf
, sizeof(addrbuf
));
955 ctxt
->buf_idx
+= snprintf(ctxt
->buf
+ ctxt
->buf_idx
, ctxt
->buf_len
- ctxt
->buf_idx
,
956 "%s/%d ", addrbuf
, prefix
);
959 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
960 const char *duidbuf
, bool is_pd
, struct dhcpv6_assignment
*a
, int code
)
962 const char *type
= "UNKNOWN";
963 const char *status
= "UNKNOWN";
965 if (msgtype
== DHCPV6_MSG_RENEW
)
969 case DHCPV6_MSG_SOLICIT
:
972 case DHCPV6_MSG_REQUEST
:
975 case DHCPV6_MSG_CONFIRM
:
978 case DHCPV6_MSG_RENEW
:
981 case DHCPV6_MSG_REBIND
:
984 case DHCPV6_MSG_RELEASE
:
987 case DHCPV6_MSG_DECLINE
:
993 case DHCPV6_STATUS_OK
:
996 case DHCPV6_STATUS_NOADDRSAVAIL
:
997 status
= "no addresses available";
999 case DHCPV6_STATUS_NOBINDING
:
1000 status
= "no binding";
1002 case DHCPV6_STATUS_NOTONLINK
:
1003 status
= "not on-link";
1005 case DHCPV6_STATUS_NOPREFIXAVAIL
:
1006 status
= "no prefix available";
1010 char leasebuf
[256] = "";
1013 struct log_ctxt ctxt
= {.buf
= leasebuf
,
1014 .buf_len
= sizeof(leasebuf
),
1017 dhcpv6_enum_ia_addrs(iface
, a
, now
, dhcpv6_log_ia_addr
, &ctxt
);
1020 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
1021 duidbuf
, iface
->ifname
, status
, leasebuf
);
1024 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
1025 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
1027 time_t now
= odhcpd_time();
1028 size_t response_len
= 0;
1029 const struct dhcpv6_client_header
*hdr
= data
;
1030 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
1031 uint16_t otype
, olen
;
1032 /* Find and parse client-id and hostname */
1033 bool accept_reconf
= false;
1034 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1036 size_t hostname_len
= 0;
1037 bool notonlink
= false;
1040 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1041 if (otype
== DHCPV6_OPT_CLIENTID
) {
1045 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
1046 memcpy(mac
, &odata
[8], sizeof(mac
));
1047 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
1048 memcpy(mac
, &odata
[4], sizeof(mac
));
1051 odhcpd_hexlify(duidbuf
, odata
, olen
);
1052 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
1053 uint8_t fqdn_buf
[256];
1054 memcpy(fqdn_buf
, odata
, olen
);
1055 fqdn_buf
[olen
++] = 0;
1057 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
1058 hostname_len
= strcspn(hostname
, ".");
1059 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
)
1060 accept_reconf
= true;
1063 if (!clid_data
|| !clid_len
|| clid_len
> 130)
1066 struct dhcpv6_assignment
*first
= NULL
;
1067 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1068 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
1069 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
1070 bool ia_addr_present
= false;
1071 if (!is_pd
&& !is_na
)
1074 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1075 size_t ia_response_len
= 0;
1076 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1077 uint32_t reqhint
= 0;
1079 /* Parse request hint for IA-PD */
1082 uint16_t stype
, slen
;
1083 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1084 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1087 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1090 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1091 if (reqlen
> 32 && reqlen
<= 64)
1092 reqhint
&= (1U << (64 - reqlen
)) - 1;
1100 uint16_t stype
, slen
;
1101 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1102 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1105 ia_addr_present
= true;
1109 /* Find assignment */
1110 struct dhcpv6_assignment
*c
, *a
= NULL
;
1111 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1112 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1113 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1114 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1115 (c
->iaid
== ia
->iaid
|| INFINITE_VALID(c
->valid_until
) || now
< c
->valid_until
) &&
1116 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1120 if (a
->flags
& OAF_BOUND
)
1121 apply_lease(iface
, a
, false);
1123 memcpy(a
->clid_data
, clid_data
, clid_len
);
1124 a
->clid_len
= clid_len
;
1133 /* Generic message handling */
1134 uint16_t status
= DHCPV6_STATUS_OK
;
1135 if (a
&& a
->managed_size
< 0)
1138 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
||
1139 hdr
->msg_type
== DHCPV6_MSG_REQUEST
||
1140 (hdr
->msg_type
== DHCPV6_MSG_REBIND
&& !a
)) {
1141 bool assigned
= !!a
;
1143 if (!a
&& !iface
->no_dynamic_dhcp
) {
1144 /* Create new binding */
1145 a
= calloc(1, sizeof(*a
) + clid_len
);
1147 a
->clid_len
= clid_len
;
1151 a
->assigned
= reqhint
;
1152 /* Set valid time to current time indicating */
1153 /* assignment is not having infinite lifetime */
1154 a
->valid_until
= now
;
1157 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1159 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1160 memcpy(a
->clid_data
, clid_data
, clid_len
);
1163 while (!(assigned
= assign_pd(iface
, a
)) &&
1164 !a
->managed_size
&& ++a
->length
<= 64);
1166 assigned
= assign_na(iface
, a
);
1168 if (a
->managed_size
&& !assigned
)
1173 if (!assigned
|| iface
->ia_addr_len
== 0)
1174 /* Set error status */
1175 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1176 else if (assigned
&& !first
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1177 size_t handshake_len
= 4;
1179 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1183 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1184 struct dhcpv6_auth_reconfigure auth
= {
1185 htons(DHCPV6_OPT_AUTH
),
1186 htons(sizeof(auth
) - 4),
1188 {htonl(time(NULL
)), htonl(++serial
)},
1192 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1193 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1194 handshake_len
+= sizeof(auth
);
1197 buf
+= handshake_len
;
1198 buflen
-= handshake_len
;
1199 response_len
+= handshake_len
;
1204 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
,
1205 hdr
->msg_type
== DHCPV6_MSG_REBIND
? false : true);
1207 /* Was only a solicitation: mark binding for removal */
1208 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1209 a
->flags
&= ~OAF_BOUND
;
1211 if (!(a
->flags
& OAF_STATIC
))
1212 a
->valid_until
= now
;
1213 } else if (assigned
&&
1214 (hdr
->msg_type
== DHCPV6_MSG_REQUEST
||
1215 hdr
->msg_type
== DHCPV6_MSG_REBIND
)) {
1216 if (hostname_len
> 0) {
1217 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1219 memcpy(a
->hostname
, hostname
, hostname_len
);
1220 a
->hostname
[hostname_len
] = 0;
1223 a
->accept_reconf
= accept_reconf
;
1224 a
->flags
|= OAF_BOUND
;
1225 apply_lease(iface
, a
, true);
1226 } else if (!assigned
&& a
&& a
->managed_size
== 0) {
1227 /* Cleanup failed assignment */
1228 free_dhcpv6_assignment(a
);
1231 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1232 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1233 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1234 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1235 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1236 status
= DHCPV6_STATUS_NOBINDING
;
1237 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1238 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1239 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1240 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1242 a
->flags
|= OAF_BOUND
;
1243 apply_lease(iface
, a
, true);
1245 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1246 if (!(a
->flags
& OAF_STATIC
))
1247 a
->valid_until
= now
- 1;
1249 if (a
->flags
& OAF_BOUND
) {
1250 apply_lease(iface
, a
, false);
1251 a
->flags
&= ~OAF_BOUND
;
1253 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1254 a
->flags
&= ~OAF_BOUND
;
1256 if (!(a
->flags
& OAF_STATIC
)) {
1258 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1261 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1262 /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1263 status
= DHCPV6_STATUS_NOTONLINK
;
1264 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1268 buf
+= ia_response_len
;
1269 buflen
-= ia_response_len
;
1270 response_len
+= ia_response_len
;
1271 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1274 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1275 response_len
+ 6 < buflen
) {
1277 buf
[1] = DHCPV6_OPT_STATUS
;
1281 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1285 dhcpv6_write_statefile();
1288 return response_len
;