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 stop_reconf(struct dhcpv6_assignment
*a
);
44 static void valid_until_cb(struct uloop_timeout
*event
);
46 static struct uloop_timeout valid_until_timeout
= {.cb
= valid_until_cb
};
47 static uint32_t serial
= 0;
48 static uint8_t statemd5
[16];
50 int dhcpv6_ia_init(void)
52 uloop_timeout_set(&valid_until_timeout
, 1000);
56 int setup_dhcpv6_ia_interface(struct interface
*iface
, bool enable
)
58 if (!enable
&& iface
->ia_assignments
.next
) {
59 struct dhcpv6_assignment
*c
;
61 while (!list_empty(&iface
->ia_assignments
)) {
62 c
= list_first_entry(&iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
63 free_dhcpv6_assignment(c
);
67 if (enable
&& iface
->dhcpv6
== MODE_SERVER
) {
68 if (!iface
->ia_assignments
.next
)
69 INIT_LIST_HEAD(&iface
->ia_assignments
);
71 if (list_empty(&iface
->ia_assignments
)) {
72 struct dhcpv6_assignment
*border
= calloc(1, sizeof(*border
));
74 syslog(LOG_ERR
, "Calloc failed for border on interface %s", iface
->ifname
);
79 list_add(&border
->head
, &iface
->ia_assignments
);
82 /* Parse static entries */
84 list_for_each_entry(lease
, &leases
, head
) {
86 size_t duid_len
= lease
->duid_len
? lease
->duid_len
: 14;
87 struct dhcpv6_assignment
*a
= calloc(1, sizeof(*a
) + duid_len
);
89 syslog(LOG_ERR
, "Calloc failed for static lease assignment on interface %s",
94 a
->leasetime
= lease
->dhcpv4_leasetime
;
96 a
->clid_len
= duid_len
;
99 a
->assigned
= lease
->hostid
;
101 uint32_t i4a
= ntohl(lease
->ipaddr
.s_addr
) & 0xff;
102 a
->assigned
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
105 odhcpd_urandom(a
->key
, sizeof(a
->key
));
106 memcpy(a
->clid_data
, lease
->duid
, lease
->duid_len
);
107 memcpy(a
->mac
, lease
->mac
.ether_addr_octet
, sizeof(a
->mac
));
108 /* Static assignment */
109 a
->flags
|= OAF_STATIC
;
113 /* Assign to all interfaces */
114 struct dhcpv6_assignment
*c
;
115 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
116 if (c
->length
!= 128 || c
->assigned
> a
->assigned
) {
117 list_add_tail(&a
->head
, &c
->head
);
119 } else if (c
->assigned
== a
->assigned
)
120 /* Already an assignment with that number */
126 if (lease
->hostname
[0]) {
128 a
->hostname
= strdup(lease
->hostname
);
131 free_dhcpv6_assignment(a
);
137 static void free_dhcpv6_assignment(struct dhcpv6_assignment
*c
)
139 if (c
->managed_sock
.fd
.registered
) {
140 ustream_free(&c
->managed_sock
.stream
);
141 close(c
->managed_sock
.fd
.fd
);
155 static inline bool valid_prefix_length(const struct dhcpv6_assignment
*a
, const uint8_t prefix_length
)
157 return (a
->managed_size
|| a
->length
> prefix_length
);
160 static inline bool valid_addr(const struct odhcpd_ipaddr
*addr
, time_t now
)
162 return (addr
->prefix
<= 96 && addr
->preferred
> (uint32_t)now
);
165 static size_t get_preferred_addr(const struct odhcpd_ipaddr
*addrs
, const size_t addrlen
)
169 for (i
= 0, m
= 0; i
< addrlen
; ++i
) {
170 if (addrs
[i
].preferred
> addrs
[m
].preferred
||
171 (addrs
[i
].preferred
== addrs
[m
].preferred
&&
172 memcmp(&addrs
[i
].addr
, &addrs
[m
].addr
, 16) > 0))
179 static int send_reconf(struct dhcpv6_assignment
*assign
)
182 struct dhcpv6_client_header hdr
;
186 uint16_t hardware_type
;
191 struct dhcpv6_auth_reconfigure auth
;
194 uint8_t clid_data
[128];
195 } __attribute__((packed
)) reconf_msg
= {
196 .hdr
= {DHCPV6_MSG_RECONFIGURE
, {0, 0, 0}},
197 .srvid_type
= htons(DHCPV6_OPT_SERVERID
),
198 .srvid_len
= htons(10),
199 .duid_type
= htons(3),
200 .hardware_type
= htons(1),
201 .msg_type
= htons(DHCPV6_OPT_RECONF_MSG
),
203 .msg_id
= DHCPV6_MSG_RENEW
,
204 .auth
= {htons(DHCPV6_OPT_AUTH
),
205 htons(sizeof(reconf_msg
.auth
) - 4), 3, 1, 0,
206 {htonl(time(NULL
)), htonl(++serial
)}, 2, {0}},
207 .clid_type
= htons(DHCPV6_OPT_CLIENTID
),
208 .clid_len
= htons(assign
->clid_len
),
211 struct interface
*iface
= assign
->iface
;
213 odhcpd_get_mac(iface
, reconf_msg
.mac
);
214 memcpy(reconf_msg
.clid_data
, assign
->clid_data
, assign
->clid_len
);
215 struct iovec iov
= {&reconf_msg
, sizeof(reconf_msg
) - 128 + assign
->clid_len
};
218 uint8_t secretbytes
[64];
219 memset(secretbytes
, 0, sizeof(secretbytes
));
220 memcpy(secretbytes
, assign
->key
, sizeof(assign
->key
));
222 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
223 secretbytes
[i
] ^= 0x36;
226 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
227 md5_hash(iov
.iov_base
, iov
.iov_len
, &md5
);
228 md5_end(reconf_msg
.auth
.key
, &md5
);
230 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
231 secretbytes
[i
] ^= 0x36;
232 secretbytes
[i
] ^= 0x5c;
236 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
237 md5_hash(reconf_msg
.auth
.key
, 16, &md5
);
238 md5_end(reconf_msg
.auth
.key
, &md5
);
240 return odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &assign
->peer
, &iov
, 1, iface
);
243 void dhcpv6_enum_ia_addrs(struct interface
*iface
, struct dhcpv6_assignment
*c
,
244 time_t now
, dhcpv6_binding_cb_handler_t func
, void *arg
)
246 struct odhcpd_ipaddr
*addrs
= (c
->managed
) ? c
->managed
: iface
->ia_addr
;
247 size_t addrlen
= (c
->managed
) ? (size_t)c
->managed_size
: iface
->ia_addr_len
;
248 size_t m
= get_preferred_addr(addrs
, addrlen
);
250 for (size_t i
= 0; i
< addrlen
; ++i
) {
251 struct in6_addr addr
;
252 uint32_t pref
, valid
;
253 int prefix
= c
->managed
? addrs
[i
].prefix
: c
->length
;
255 if (!valid_addr(&addrs
[i
], now
))
258 addr
= addrs
[i
].addr
.in6
;
259 pref
= addrs
[i
].preferred
;
260 valid
= addrs
[i
].valid
;
262 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
))
265 addr
.s6_addr32
[3] = htonl(c
->assigned
);
267 if (!valid_prefix_length(c
, addrs
[i
].prefix
))
270 addr
.s6_addr32
[1] |= htonl(c
->assigned
);
271 addr
.s6_addr32
[2] = addr
.s6_addr32
[3] = 0;
274 if (pref
!= UINT32_MAX
)
277 if (valid
!= UINT32_MAX
)
280 func(&addr
, prefix
, pref
, valid
, arg
);
287 struct dhcpv6_assignment
*c
;
288 struct interface
*iface
;
294 void dhcpv6_write_ia_addr(struct in6_addr
*addr
, int prefix
, _unused
uint32_t pref
,
295 _unused
uint32_t valid
, void *arg
)
297 struct write_ctxt
*ctxt
= (struct write_ctxt
*)arg
;
298 char ipbuf
[INET6_ADDRSTRLEN
];
300 inet_ntop(AF_INET6
, addr
, ipbuf
, sizeof(ipbuf
) - 1);
302 if (ctxt
->c
->length
== 128 && ctxt
->c
->hostname
) {
303 fputs(ipbuf
, ctxt
->fp
);
306 if (dn_expand(ctxt
->iface
->search
, ctxt
->iface
->search
+ ctxt
->iface
->search_len
,
307 ctxt
->iface
->search
, b
, sizeof(b
)) > 0)
308 fprintf(ctxt
->fp
, "\t%s.%s", ctxt
->c
->hostname
, b
);
310 fprintf(ctxt
->fp
, "\t%s\n", ctxt
->c
->hostname
);
311 md5_hash(ipbuf
, strlen(ipbuf
), &ctxt
->md5
);
312 md5_hash(ctxt
->c
->hostname
, strlen(ctxt
->c
->hostname
), &ctxt
->md5
);
315 ctxt
->buf_idx
+= snprintf(ctxt
->buf
+ ctxt
->buf_idx
,ctxt
->buf_len
- ctxt
->buf_idx
,
316 "%s/%d ", ipbuf
, prefix
);
319 void dhcpv6_write_statefile(void)
321 struct write_ctxt ctxt
;
323 md5_begin(&ctxt
.md5
);
325 if (config
.dhcp_statefile
) {
326 time_t now
= odhcpd_time(), wall_time
= time(NULL
);
327 int fd
= open(config
.dhcp_statefile
, O_CREAT
| O_WRONLY
| O_CLOEXEC
, 0644);
333 ret
= lockf(fd
, F_LOCK
, 0);
338 if (ftruncate(fd
, 0) < 0) {}
340 ctxt
.fp
= fdopen(fd
, "w");
347 ctxt
.buf_len
= sizeof(leasebuf
);
349 list_for_each_entry(ctxt
.iface
, &interfaces
, head
) {
350 if (ctxt
.iface
->dhcpv6
!= MODE_SERVER
&&
351 ctxt
.iface
->dhcpv4
!= MODE_SERVER
)
354 if (ctxt
.iface
->dhcpv6
== MODE_SERVER
&&
355 ctxt
.iface
->ia_assignments
.next
) {
356 list_for_each_entry(ctxt
.c
, &ctxt
.iface
->ia_assignments
, head
) {
357 if (!(ctxt
.c
->flags
& OAF_BOUND
) || ctxt
.c
->managed_size
< 0)
362 odhcpd_hexlify(duidbuf
, ctxt
.c
->clid_data
, ctxt
.c
->clid_len
);
364 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
365 ctxt
.buf_idx
= snprintf(ctxt
.buf
, ctxt
.buf_len
, "# %s %s %x %s %ld %x %u ",
366 ctxt
.iface
->ifname
, duidbuf
, ntohl(ctxt
.c
->iaid
),
367 (ctxt
.c
->hostname
? ctxt
.c
->hostname
: "-"),
368 (ctxt
.c
->valid_until
> now
?
369 (ctxt
.c
->valid_until
- now
+ wall_time
) :
370 (INFINITE_VALID(ctxt
.c
->valid_until
) ? -1 : 0)),
371 ctxt
.c
->assigned
, (unsigned)ctxt
.c
->length
);
373 if (INFINITE_VALID(ctxt
.c
->valid_until
) || ctxt
.c
->valid_until
> now
)
374 dhcpv6_enum_ia_addrs(ctxt
.iface
, ctxt
.c
, now
,
375 dhcpv6_write_ia_addr
, &ctxt
);
377 ctxt
.buf
[ctxt
.buf_idx
- 1] = '\n';
378 fwrite(ctxt
.buf
, 1, ctxt
.buf_idx
, ctxt
.fp
);
382 if (ctxt
.iface
->dhcpv4
== MODE_SERVER
&&
383 ctxt
.iface
->dhcpv4_assignments
.next
) {
384 struct dhcpv4_assignment
*c
;
385 list_for_each_entry(c
, &ctxt
.iface
->dhcpv4_assignments
, head
) {
386 if (!(c
->flags
& OAF_BOUND
))
389 char ipbuf
[INET6_ADDRSTRLEN
];
391 odhcpd_hexlify(duidbuf
, c
->hwaddr
, sizeof(c
->hwaddr
));
393 /* iface DUID iaid hostname lifetime assigned length [addrs...] */
394 ctxt
.buf_idx
= snprintf(ctxt
.buf
, ctxt
.buf_len
, "# %s %s ipv4 %s %ld %x 32 ",
395 ctxt
.iface
->ifname
, duidbuf
,
396 (c
->hostname
? c
->hostname
: "-"),
397 (c
->valid_until
> now
?
398 (c
->valid_until
- now
+ wall_time
) :
399 (INFINITE_VALID(c
->valid_until
) ? -1 : 0)),
402 struct in_addr addr
= {.s_addr
= c
->addr
};
403 inet_ntop(AF_INET
, &addr
, ipbuf
, sizeof(ipbuf
) - 1);
406 fputs(ipbuf
, ctxt
.fp
);
409 if (dn_expand(ctxt
.iface
->search
,
410 ctxt
.iface
->search
+ ctxt
.iface
->search_len
,
411 ctxt
.iface
->search
, b
, sizeof(b
)) > 0)
412 fprintf(ctxt
.fp
, "\t%s.%s", c
->hostname
, b
);
414 fprintf(ctxt
.fp
, "\t%s\n", c
->hostname
);
415 md5_hash(ipbuf
, strlen(ipbuf
), &ctxt
.md5
);
416 md5_hash(c
->hostname
, strlen(c
->hostname
), &ctxt
.md5
);
419 ctxt
.buf_idx
+= snprintf(ctxt
.buf
+ ctxt
.buf_idx
,
420 ctxt
.buf_len
- ctxt
.buf_idx
,
422 ctxt
.buf
[ctxt
.buf_idx
- 1] = '\n';
423 fwrite(ctxt
.buf
, 1, ctxt
.buf_idx
, ctxt
.fp
);
432 md5_end(newmd5
, &ctxt
.md5
);
434 if (config
.dhcp_cb
&& memcmp(newmd5
, statemd5
, sizeof(newmd5
))) {
435 memcpy(statemd5
, newmd5
, sizeof(statemd5
));
436 char *argv
[2] = {config
.dhcp_cb
, NULL
};
438 execv(argv
[0], argv
);
445 static void apply_lease(struct interface
*iface
, struct dhcpv6_assignment
*a
, bool add
)
447 if (a
->length
> 64 || a
->managed_size
< 0)
450 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
451 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
453 for (size_t i
= 0; i
< addrlen
; ++i
) {
454 struct in6_addr prefix
= addrs
[i
].addr
.in6
;
455 prefix
.s6_addr32
[1] |= htonl(a
->assigned
);
456 prefix
.s6_addr32
[2] = prefix
.s6_addr32
[3] = 0;
457 odhcpd_setup_route(&prefix
, (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
458 iface
, &a
->peer
.sin6_addr
, 1024, add
);
462 /* More data was received from TCP connection */
463 static void managed_handle_pd_data(struct ustream
*s
, _unused
int bytes_new
)
465 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
466 time_t now
= odhcpd_time();
467 bool first
= c
->managed_size
< 0;
471 char *data
= ustream_get_read_buf(s
, &pending
);
472 char *end
= memmem(data
, pending
, "\n\n", 2);
481 if (c
->accept_reconf
)
485 for (char *line
= strtok_r(data
, "\n", &saveptr
); line
; line
= strtok_r(NULL
, "\n", &saveptr
)) {
486 c
->managed
= realloc(c
->managed
, (c
->managed_size
+ 1) * sizeof(*c
->managed
));
487 struct odhcpd_ipaddr
*n
= &c
->managed
[c
->managed_size
];
489 char *saveptr2
, *x
= strtok_r(line
, "/", &saveptr2
);
490 if (!x
|| inet_pton(AF_INET6
, x
, &n
->addr
) < 1)
493 x
= strtok_r(NULL
, ",", &saveptr2
);
494 if (sscanf(x
, "%hhu", &n
->prefix
) < 1)
497 x
= strtok_r(NULL
, ",", &saveptr2
);
498 if (sscanf(x
, "%u", &n
->preferred
) < 1)
501 x
= strtok_r(NULL
, ",", &saveptr2
);
502 if (sscanf(x
, "%u", &n
->valid
) < 1)
505 if (n
->preferred
> n
->valid
)
508 if (UINT32_MAX
- now
< n
->preferred
)
509 n
->preferred
= UINT32_MAX
;
513 if (UINT32_MAX
- now
< n
->valid
)
514 n
->valid
= UINT32_MAX
;
523 ustream_consume(s
, end
- data
);
526 if (first
&& c
->managed_size
== 0)
527 free_dhcpv6_assignment(c
);
528 else if (first
&& !(c
->flags
& OAF_STATIC
))
529 c
->valid_until
= now
+ 150;
533 /* TCP transmission has ended, either because of success or timeout or other error */
534 static void managed_handle_pd_done(struct ustream
*s
)
536 struct dhcpv6_assignment
*c
= container_of(s
, struct dhcpv6_assignment
, managed_sock
);
538 if (!(c
->flags
& OAF_STATIC
))
539 c
->valid_until
= odhcpd_time() + 15;
543 if (c
->accept_reconf
)
547 static bool assign_pd(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
549 struct dhcpv6_assignment
*c
;
551 if (iface
->dhcpv6_pd_manager
[0]) {
552 int fd
= usock(USOCK_UNIX
| USOCK_TCP
, iface
->dhcpv6_pd_manager
, NULL
);
555 odhcpd_hexlify(iaidbuf
, assign
->clid_data
, assign
->clid_len
);
557 assign
->managed_sock
.stream
.notify_read
= managed_handle_pd_data
;
558 assign
->managed_sock
.stream
.notify_state
= managed_handle_pd_done
;
559 ustream_fd_init(&assign
->managed_sock
, fd
);
560 ustream_printf(&assign
->managed_sock
.stream
, "%s,%x\n::/%d,0,0\n\n",
561 iaidbuf
, assign
->iaid
, assign
->length
);
562 ustream_write_pending(&assign
->managed_sock
.stream
);
563 assign
->managed_size
= -1;
565 if (!(assign
->flags
& OAF_STATIC
))
566 assign
->valid_until
= odhcpd_time() + 15;
568 list_add(&assign
->head
, &iface
->ia_assignments
);
570 /* Wait initial period of up to 250ms for immediate assignment */
571 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
573 managed_handle_pd_data(&assign
->managed_sock
.stream
, 0);
575 if (fcntl(fd
, F_GETFL
) >= 0 && assign
->managed_size
> 0)
580 } else if (iface
->ia_addr_len
< 1)
583 /* Try honoring the hint first */
584 uint32_t current
= 1, asize
= (1 << (64 - assign
->length
)) - 1;
585 if (assign
->assigned
) {
586 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
587 if (c
->length
== 128 || c
->length
== 0)
590 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
591 list_add_tail(&assign
->head
, &c
->head
);
593 if (assign
->flags
& OAF_BOUND
)
594 apply_lease(iface
, assign
, true);
599 if (c
->assigned
!= 0)
600 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
604 /* Fallback to a variable assignment */
606 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
607 if (c
->length
== 128 || c
->length
== 0)
610 current
= (current
+ asize
) & (~asize
);
611 if (current
+ asize
< c
->assigned
) {
612 assign
->assigned
= current
;
613 list_add_tail(&assign
->head
, &c
->head
);
615 if (assign
->flags
& OAF_BOUND
)
616 apply_lease(iface
, assign
, true);
621 if (c
->assigned
!= 0)
622 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
628 static bool assign_na(struct interface
*iface
, struct dhcpv6_assignment
*assign
)
630 /* Seed RNG with checksum of DUID */
632 for (size_t i
= 0; i
< assign
->clid_len
; ++i
)
633 seed
+= assign
->clid_data
[i
];
636 /* Try to assign up to 100x */
637 for (size_t i
= 0; i
< 100; ++i
) {
639 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
641 struct dhcpv6_assignment
*c
;
642 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
646 if (c
->assigned
> try || c
->length
!= 128) {
647 assign
->assigned
= try;
648 list_add_tail(&assign
->head
, &c
->head
);
650 } else if (c
->assigned
== try)
658 void dhcpv6_ia_preupdate(struct interface
*iface
)
660 if (iface
->dhcpv6
!= MODE_SERVER
)
663 struct dhcpv6_assignment
*c
, *border
= list_last_entry(
664 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
666 list_for_each_entry(c
, &iface
->ia_assignments
, head
)
667 if (c
!= border
&& iface
->ra_managed
== RA_MANAGED_NO_MFLAG
668 && (c
->flags
& OAF_BOUND
))
669 apply_lease(iface
, c
, false);
672 static void reconf_timeout_cb(struct uloop_timeout
*event
)
674 struct dhcpv6_assignment
*a
= container_of(event
, struct dhcpv6_assignment
, reconf_timer
);
676 if (a
->reconf_cnt
> 0 && a
->reconf_cnt
< DHCPV6_REC_MAX_RC
) {
678 uloop_timeout_set(&a
->reconf_timer
,
679 DHCPV6_REC_TIMEOUT
<< a
->reconf_cnt
);
685 static void start_reconf(struct dhcpv6_assignment
*a
)
687 uloop_timeout_set(&a
->reconf_timer
,
688 DHCPV6_REC_TIMEOUT
<< a
->reconf_cnt
);
689 a
->reconf_timer
.cb
= reconf_timeout_cb
;
695 static void stop_reconf(struct dhcpv6_assignment
*a
)
697 uloop_timeout_cancel(&a
->reconf_timer
);
699 a
->reconf_timer
.cb
= NULL
;
702 void dhcpv6_ia_postupdate(struct interface
*iface
)
704 if (iface
->dhcpv6
!= MODE_SERVER
)
707 time_t now
= odhcpd_time();
709 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
710 if (iface
->ia_addr
[i
].preferred
> (uint32_t)now
&&
711 iface
->ia_addr
[i
].prefix
< 64 &&
712 iface
->ia_addr
[i
].prefix
> minprefix
)
713 minprefix
= iface
->ia_addr
[i
].prefix
;
716 struct dhcpv6_assignment
*border
= list_last_entry(
717 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
719 if (minprefix
> 32 && minprefix
<= 64)
720 border
->assigned
= 1U << (64 - minprefix
);
722 border
->assigned
= 0;
724 struct list_head reassign
= LIST_HEAD_INIT(reassign
);
725 struct dhcpv6_assignment
*c
, *d
;
726 list_for_each_entry_safe(c
, d
, &iface
->ia_assignments
, head
) {
727 if (c
->clid_len
== 0 || (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
) ||
731 if (c
->length
< 128 && c
->assigned
>= border
->assigned
&& c
!= border
)
732 list_move(&c
->head
, &reassign
);
733 else if (c
!= border
&& (c
->flags
& OAF_BOUND
))
734 apply_lease(iface
, c
, true);
736 if (c
->accept_reconf
&& c
->reconf_cnt
== 0) {
739 /* Leave all other assignments of that client alone */
740 struct dhcpv6_assignment
*a
;
741 list_for_each_entry(a
, &iface
->ia_assignments
, head
)
742 if (a
!= c
&& a
->clid_len
== c
->clid_len
&&
743 !memcmp(a
->clid_data
, c
->clid_data
, a
->clid_len
))
744 a
->reconf_cnt
= INT_MAX
;
748 while (!list_empty(&reassign
)) {
749 c
= list_first_entry(&reassign
, struct dhcpv6_assignment
, head
);
751 if (!assign_pd(iface
, c
)) {
753 list_add(&c
->head
, &iface
->ia_assignments
);
757 dhcpv6_write_statefile();
760 static void valid_until_cb(struct uloop_timeout
*event
)
762 time_t now
= odhcpd_time();
763 struct interface
*iface
;
764 list_for_each_entry(iface
, &interfaces
, head
) {
765 if (iface
->dhcpv6
!= MODE_SERVER
|| iface
->ia_assignments
.next
== NULL
)
768 struct dhcpv6_assignment
*a
, *n
;
769 list_for_each_entry_safe(a
, n
, &iface
->ia_assignments
, head
) {
770 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
) {
771 if ((a
->length
< 128 && a
->clid_len
> 0) ||
772 (a
->length
== 128 && a
->clid_len
== 0))
773 free_dhcpv6_assignment(a
);
778 uloop_timeout_set(event
, 1000);
781 static size_t append_reply(uint8_t *buf
, size_t buflen
, uint16_t status
,
782 const struct dhcpv6_ia_hdr
*ia
, struct dhcpv6_assignment
*a
,
783 struct interface
*iface
, bool request
)
785 if (buflen
< sizeof(*ia
) + sizeof(struct dhcpv6_ia_prefix
))
788 struct dhcpv6_ia_hdr out
= {ia
->type
, 0, ia
->iaid
, 0, 0};
789 size_t datalen
= sizeof(out
);
790 time_t now
= odhcpd_time();
793 struct __attribute__((packed
)) {
797 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
800 memcpy(buf
+ datalen
, &stat
, sizeof(stat
));
801 datalen
+= sizeof(stat
);
806 leasetime
= a
->leasetime
;
808 leasetime
= iface
->dhcpv4_leasetime
;
810 uint32_t pref
= leasetime
;
811 uint32_t valid
= leasetime
;
813 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
814 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
815 size_t m
= get_preferred_addr(addrs
, addrlen
);
817 for (size_t i
= 0; i
< addrlen
; ++i
) {
818 uint32_t prefix_pref
= addrs
[i
].preferred
;
819 uint32_t prefix_valid
= addrs
[i
].valid
;
821 if (!valid_addr(&addrs
[i
], now
))
824 if (prefix_pref
!= UINT32_MAX
)
827 if (prefix_valid
!= UINT32_MAX
)
830 if (a
->length
< 128) {
831 struct dhcpv6_ia_prefix p
= {
832 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
833 .len
= htons(sizeof(p
) - 4),
834 .preferred
= htonl(prefix_pref
),
835 .valid
= htonl(prefix_valid
),
836 .prefix
= (a
->managed_size
) ? addrs
[i
].prefix
: a
->length
,
837 .addr
= addrs
[i
].addr
.in6
,
839 p
.addr
.s6_addr32
[1] |= htonl(a
->assigned
);
840 p
.addr
.s6_addr32
[2] = p
.addr
.s6_addr32
[3] = 0;
842 size_t entrlen
= sizeof(p
) - 4;
844 if (datalen
+ entrlen
+ 4 > buflen
||
845 (a
->assigned
== 0 && a
->managed_size
== 0) ||
846 !valid_prefix_length(a
, addrs
[i
].prefix
))
849 memcpy(buf
+ datalen
, &p
, sizeof(p
));
850 datalen
+= entrlen
+ 4;
852 struct dhcpv6_ia_addr n
= {
853 .type
= htons(DHCPV6_OPT_IA_ADDR
),
854 .len
= htons(sizeof(n
) - 4),
855 .addr
= addrs
[i
].addr
.in6
,
856 .preferred
= htonl(prefix_pref
),
857 .valid
= htonl(prefix_valid
)
859 n
.addr
.s6_addr32
[3] = htonl(a
->assigned
);
860 size_t entrlen
= sizeof(n
) - 4;
862 if (!ADDR_ENTRY_VALID_IA_ADDR(iface
, i
, m
, addrs
) ||
864 datalen
+ entrlen
+ 4 > buflen
)
867 memcpy(buf
+ datalen
, &n
, sizeof(n
));
868 datalen
+= entrlen
+ 4;
871 /* Calculate T1 / T2 based on non-deprecated addresses */
872 if (prefix_pref
> 0) {
873 if (prefix_pref
< pref
)
876 if (prefix_valid
< valid
)
877 valid
= prefix_valid
;
881 if (!INFINITE_VALID(a
->valid_until
))
882 /* UINT32_MAX is considered as infinite leasetime */
883 a
->valid_until
= (valid
== UINT32_MAX
) ? 0 : valid
+ now
;
885 out
.t1
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 5 / 10);
886 out
.t2
= htonl((pref
== UINT32_MAX
) ? pref
: pref
* 8 / 10);
896 uint8_t *odata
, *end
= ((uint8_t*)ia
) + htons(ia
->len
) + 4;
897 uint16_t otype
, olen
;
898 dhcpv6_for_each_option((uint8_t*)&ia
[1], end
, otype
, olen
, odata
) {
899 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&odata
[-4];
900 struct dhcpv6_ia_addr
*n
= (struct dhcpv6_ia_addr
*)&odata
[-4];
901 if ((otype
!= DHCPV6_OPT_IA_PREFIX
|| olen
< sizeof(*p
) - 4) &&
902 (otype
!= DHCPV6_OPT_IA_ADDR
|| olen
< sizeof(*n
) - 4))
907 struct odhcpd_ipaddr
*addrs
= (a
->managed
) ? a
->managed
: iface
->ia_addr
;
908 size_t addrlen
= (a
->managed
) ? (size_t)a
->managed_size
: iface
->ia_addr_len
;
910 for (size_t i
= 0; i
< addrlen
; ++i
) {
911 if (!valid_addr(&addrs
[i
], now
))
914 struct in6_addr addr
= addrs
[i
].addr
.in6
;
915 if (ia
->type
== htons(DHCPV6_OPT_IA_PD
)) {
916 addr
.s6_addr32
[1] |= htonl(a
->assigned
);
917 addr
.s6_addr32
[2] = addr
.s6_addr32
[3] = 0;
919 if (!memcmp(&p
->addr
, &addr
, sizeof(addr
)) &&
920 p
->prefix
== ((a
->managed
) ? addrs
[i
].prefix
: a
->length
))
923 addr
.s6_addr32
[3] = htonl(a
->assigned
);
925 if (!memcmp(&n
->addr
, &addr
, sizeof(addr
)))
932 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
933 struct dhcpv6_ia_prefix inv
= {
934 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
935 .len
= htons(sizeof(inv
) - 4),
942 if (datalen
+ sizeof(inv
) > buflen
)
945 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
946 datalen
+= sizeof(inv
);
948 struct dhcpv6_ia_addr inv
= {
949 .type
= htons(DHCPV6_OPT_IA_ADDR
),
950 .len
= htons(sizeof(inv
) - 4),
956 if (datalen
+ sizeof(inv
) > buflen
)
959 memcpy(buf
+ datalen
, &inv
, sizeof(inv
));
960 datalen
+= sizeof(inv
);
967 out
.len
= htons(datalen
- 4);
968 memcpy(buf
, &out
, sizeof(out
));
978 static void dhcpv6_log_ia_addr(struct in6_addr
*addr
, int prefix
, _unused
uint32_t pref
,
979 _unused
uint32_t valid
, void *arg
)
981 struct log_ctxt
*ctxt
= (struct log_ctxt
*)arg
;
982 char addrbuf
[INET6_ADDRSTRLEN
];
984 inet_ntop(AF_INET6
, addr
, addrbuf
, sizeof(addrbuf
));
985 ctxt
->buf_idx
+= snprintf(ctxt
->buf
+ ctxt
->buf_idx
, ctxt
->buf_len
- ctxt
->buf_idx
,
986 "%s/%d ", addrbuf
, prefix
);
989 static void dhcpv6_log(uint8_t msgtype
, struct interface
*iface
, time_t now
,
990 const char *duidbuf
, bool is_pd
, struct dhcpv6_assignment
*a
, int code
)
992 const char *type
= "UNKNOWN";
993 const char *status
= "UNKNOWN";
995 if (msgtype
== DHCPV6_MSG_RENEW
)
999 case DHCPV6_MSG_SOLICIT
:
1002 case DHCPV6_MSG_REQUEST
:
1005 case DHCPV6_MSG_CONFIRM
:
1008 case DHCPV6_MSG_RENEW
:
1011 case DHCPV6_MSG_REBIND
:
1014 case DHCPV6_MSG_RELEASE
:
1017 case DHCPV6_MSG_DECLINE
:
1023 case DHCPV6_STATUS_OK
:
1026 case DHCPV6_STATUS_NOADDRSAVAIL
:
1027 status
= "no addresses available";
1029 case DHCPV6_STATUS_NOBINDING
:
1030 status
= "no binding";
1032 case DHCPV6_STATUS_NOTONLINK
:
1033 status
= "not on-link";
1035 case DHCPV6_STATUS_NOPREFIXAVAIL
:
1036 status
= "no prefix available";
1040 char leasebuf
[256] = "";
1043 struct log_ctxt ctxt
= {.buf
= leasebuf
,
1044 .buf_len
= sizeof(leasebuf
),
1047 dhcpv6_enum_ia_addrs(iface
, a
, now
, dhcpv6_log_ia_addr
, &ctxt
);
1050 syslog(LOG_WARNING
, "DHCPV6 %s %s from %s on %s: %s %s", type
, (is_pd
) ? "IA_PD" : "IA_NA",
1051 duidbuf
, iface
->ifname
, status
, leasebuf
);
1054 ssize_t
dhcpv6_handle_ia(uint8_t *buf
, size_t buflen
, struct interface
*iface
,
1055 const struct sockaddr_in6
*addr
, const void *data
, const uint8_t *end
)
1057 time_t now
= odhcpd_time();
1058 size_t response_len
= 0;
1059 const struct dhcpv6_client_header
*hdr
= data
;
1060 uint8_t *start
= (uint8_t*)&hdr
[1], *odata
;
1061 uint16_t otype
, olen
;
1062 /* Find and parse client-id and hostname */
1063 bool accept_reconf
= false;
1064 uint8_t *clid_data
= NULL
, clid_len
= 0, mac
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1066 size_t hostname_len
= 0;
1067 bool notonlink
= false;
1070 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1071 if (otype
== DHCPV6_OPT_CLIENTID
) {
1075 if (olen
== 14 && odata
[0] == 0 && odata
[1] == 1)
1076 memcpy(mac
, &odata
[8], sizeof(mac
));
1077 else if (olen
== 10 && odata
[0] == 0 && odata
[1] == 3)
1078 memcpy(mac
, &odata
[4], sizeof(mac
));
1081 odhcpd_hexlify(duidbuf
, odata
, olen
);
1082 } else if (otype
== DHCPV6_OPT_FQDN
&& olen
>= 2 && olen
<= 255) {
1083 uint8_t fqdn_buf
[256];
1084 memcpy(fqdn_buf
, odata
, olen
);
1085 fqdn_buf
[olen
++] = 0;
1087 if (dn_expand(&fqdn_buf
[1], &fqdn_buf
[olen
], &fqdn_buf
[1], hostname
, sizeof(hostname
)) > 0)
1088 hostname_len
= strcspn(hostname
, ".");
1089 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
)
1090 accept_reconf
= true;
1093 if (!clid_data
|| !clid_len
|| clid_len
> 130)
1096 struct dhcpv6_assignment
*first
= NULL
;
1097 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
1098 bool is_pd
= (otype
== DHCPV6_OPT_IA_PD
);
1099 bool is_na
= (otype
== DHCPV6_OPT_IA_NA
);
1100 bool ia_addr_present
= false;
1101 if (!is_pd
&& !is_na
)
1104 struct dhcpv6_ia_hdr
*ia
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
1105 size_t ia_response_len
= 0;
1106 uint8_t reqlen
= (is_pd
) ? 62 : 128;
1107 uint32_t reqhint
= 0;
1109 /* Parse request hint for IA-PD */
1112 uint16_t stype
, slen
;
1113 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1114 if (stype
!= DHCPV6_OPT_IA_PREFIX
|| slen
< sizeof(struct dhcpv6_ia_prefix
) - 4)
1117 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&sdata
[-4];
1120 reqhint
= ntohl(p
->addr
.s6_addr32
[1]);
1121 if (reqlen
> 32 && reqlen
<= 64)
1122 reqhint
&= (1U << (64 - reqlen
)) - 1;
1130 uint16_t stype
, slen
;
1131 dhcpv6_for_each_option(&ia
[1], odata
+ olen
, stype
, slen
, sdata
) {
1132 if (stype
!= DHCPV6_OPT_IA_ADDR
|| slen
< sizeof(struct dhcpv6_ia_addr
) - 4)
1135 ia_addr_present
= true;
1139 /* Find assignment */
1140 struct dhcpv6_assignment
*c
, *a
= NULL
;
1141 list_for_each_entry(c
, &iface
->ia_assignments
, head
) {
1142 if (((c
->clid_len
== clid_len
&& !memcmp(c
->clid_data
, clid_data
, clid_len
)) ||
1143 (c
->clid_len
>= clid_len
&& !c
->clid_data
[0] && !c
->clid_data
[1]
1144 && !memcmp(c
->mac
, mac
, sizeof(mac
)))) &&
1145 (c
->iaid
== ia
->iaid
|| INFINITE_VALID(c
->valid_until
) || now
< c
->valid_until
) &&
1146 ((is_pd
&& c
->length
<= 64) || (is_na
&& c
->length
== 128))) {
1150 if (a
->flags
& OAF_BOUND
)
1151 apply_lease(iface
, a
, false);
1153 memcpy(a
->clid_data
, clid_data
, clid_len
);
1154 a
->clid_len
= clid_len
;
1162 /* Generic message handling */
1163 uint16_t status
= DHCPV6_STATUS_OK
;
1164 if (a
&& a
->managed_size
< 0)
1167 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
||
1168 hdr
->msg_type
== DHCPV6_MSG_REQUEST
||
1169 (hdr
->msg_type
== DHCPV6_MSG_REBIND
&& !a
)) {
1170 bool assigned
= !!a
;
1172 if (!a
&& !iface
->no_dynamic_dhcp
) {
1173 /* Create new binding */
1174 a
= calloc(1, sizeof(*a
) + clid_len
);
1176 a
->clid_len
= clid_len
;
1180 a
->assigned
= reqhint
;
1181 /* Set valid time to current time indicating */
1182 /* assignment is not having infinite lifetime */
1183 a
->valid_until
= now
;
1187 memcpy(a
->key
, first
->key
, sizeof(a
->key
));
1189 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1190 memcpy(a
->clid_data
, clid_data
, clid_len
);
1193 while (!(assigned
= assign_pd(iface
, a
)) &&
1194 !a
->managed_size
&& ++a
->length
<= 64);
1196 assigned
= assign_na(iface
, a
);
1198 if (a
->managed_size
&& !assigned
)
1203 if (!assigned
|| iface
->ia_addr_len
== 0)
1204 /* Set error status */
1205 status
= (is_pd
) ? DHCPV6_STATUS_NOPREFIXAVAIL
: DHCPV6_STATUS_NOADDRSAVAIL
;
1206 else if (assigned
&& !first
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1207 size_t handshake_len
= 4;
1209 buf
[1] = DHCPV6_OPT_RECONF_ACCEPT
;
1213 if (hdr
->msg_type
== DHCPV6_MSG_REQUEST
) {
1214 struct dhcpv6_auth_reconfigure auth
= {
1215 htons(DHCPV6_OPT_AUTH
),
1216 htons(sizeof(auth
) - 4),
1218 {htonl(time(NULL
)), htonl(++serial
)},
1222 memcpy(auth
.key
, a
->key
, sizeof(a
->key
));
1223 memcpy(buf
+ handshake_len
, &auth
, sizeof(auth
));
1224 handshake_len
+= sizeof(auth
);
1227 buf
+= handshake_len
;
1228 buflen
-= handshake_len
;
1229 response_len
+= handshake_len
;
1234 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
,
1235 hdr
->msg_type
== DHCPV6_MSG_REBIND
? false : true);
1237 /* Was only a solicitation: mark binding for removal */
1238 if (assigned
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
1239 a
->flags
&= ~OAF_BOUND
;
1241 if (!(a
->flags
& OAF_STATIC
))
1242 a
->valid_until
= now
;
1243 } else if (assigned
&&
1244 (hdr
->msg_type
== DHCPV6_MSG_REQUEST
||
1245 hdr
->msg_type
== DHCPV6_MSG_REBIND
)) {
1246 if (hostname_len
> 0) {
1247 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1249 memcpy(a
->hostname
, hostname
, hostname_len
);
1250 a
->hostname
[hostname_len
] = 0;
1253 a
->accept_reconf
= accept_reconf
;
1254 a
->flags
|= OAF_BOUND
;
1255 apply_lease(iface
, a
, true);
1256 } else if (!assigned
&& a
&& a
->managed_size
== 0) {
1257 /* Cleanup failed assignment */
1258 free_dhcpv6_assignment(a
);
1261 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1262 hdr
->msg_type
== DHCPV6_MSG_RELEASE
||
1263 hdr
->msg_type
== DHCPV6_MSG_REBIND
||
1264 hdr
->msg_type
== DHCPV6_MSG_DECLINE
) {
1265 if (!a
&& hdr
->msg_type
!= DHCPV6_MSG_REBIND
) {
1266 status
= DHCPV6_STATUS_NOBINDING
;
1267 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1268 } else if (hdr
->msg_type
== DHCPV6_MSG_RENEW
||
1269 hdr
->msg_type
== DHCPV6_MSG_REBIND
) {
1270 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, false);
1272 a
->flags
|= OAF_BOUND
;
1273 apply_lease(iface
, a
, true);
1275 } else if (hdr
->msg_type
== DHCPV6_MSG_RELEASE
) {
1276 if (!(a
->flags
& OAF_STATIC
))
1277 a
->valid_until
= now
- 1;
1279 if (a
->flags
& OAF_BOUND
) {
1280 apply_lease(iface
, a
, false);
1281 a
->flags
&= ~OAF_BOUND
;
1283 } else if (hdr
->msg_type
== DHCPV6_MSG_DECLINE
&& a
->length
== 128) {
1284 a
->flags
&= ~OAF_BOUND
;
1286 if (!(a
->flags
& OAF_STATIC
)) {
1288 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1291 } else if (hdr
->msg_type
== DHCPV6_MSG_CONFIRM
&& ia_addr_present
) {
1292 /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1293 status
= DHCPV6_STATUS_NOTONLINK
;
1294 ia_response_len
= append_reply(buf
, buflen
, status
, ia
, a
, iface
, true);
1298 buf
+= ia_response_len
;
1299 buflen
-= ia_response_len
;
1300 response_len
+= ia_response_len
;
1301 dhcpv6_log(hdr
->msg_type
, iface
, now
, duidbuf
, is_pd
, a
, status
);
1304 if ((hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
|| notonlink
) &&
1305 response_len
+ 6 < buflen
) {
1307 buf
[1] = DHCPV6_OPT_STATUS
;
1311 buf
[5] = (notonlink
) ? DHCPV6_STATUS_NOTONLINK
: DHCPV6_STATUS_OK
;
1315 dhcpv6_write_statefile();
1318 return response_len
;