1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Intel Corporation. All rights reserved.
6 #include <netinet/in.h>
8 #include <linux/if_arp.h>
10 #include "sd-dhcp6-client.h"
14 #include "hostname-util.h"
15 #include "missing_network.h"
16 #include "network-internal.h"
17 #include "networkd-dhcp6.h"
18 #include "networkd-link.h"
19 #include "networkd-manager.h"
20 #include "networkd-radv.h"
21 #include "siphash24.h"
22 #include "string-table.h"
23 #include "string-util.h"
24 #include "radv-internal.h"
27 static int dhcp6_lease_address_acquired(sd_dhcp6_client
*client
, Link
*link
);
28 static Link
*dhcp6_prefix_get(Manager
*m
, struct in6_addr
*addr
);
29 static int dhcp6_prefix_add(Manager
*m
, struct in6_addr
*addr
, Link
*link
);
30 static int dhcp6_prefix_remove_all(Manager
*m
, Link
*link
);
31 static int dhcp6_assign_delegated_prefix(Link
*link
, const struct in6_addr
*prefix
,
33 uint32_t lifetime_preferred
,
34 uint32_t lifetime_valid
);
36 static bool dhcp6_get_prefix_delegation(Link
*link
) {
40 return IN_SET(link
->network
->router_prefix_delegation
,
41 RADV_PREFIX_DELEGATION_DHCP6
,
42 RADV_PREFIX_DELEGATION_BOTH
);
45 static bool dhcp6_has_preferred_subnet_id(Link
*link
) {
49 return link
->network
->router_prefix_subnet_id
>= 0;
52 static int dhcp6_get_preferred_delegated_prefix(
55 const struct in6_addr
*pd_prefix
,
56 uint8_t pd_prefix_len
,
57 struct in6_addr
*ret_addr
) {
59 int64_t subnet_id
= link
->network
->router_prefix_subnet_id
;
60 uint8_t prefix_bits
= 64 - pd_prefix_len
;
61 uint64_t n_prefixes
= UINT64_C(1) << prefix_bits
;
62 _cleanup_free_
char *assigned_buf
= NULL
;
63 union in_addr_union pd_prefix_union
= {
66 /* We start off with the original PD prefix we have been assigned and
67 * iterate from there */
68 union in_addr_union prefix
= {
73 assert(pd_prefix_len
<= 64);
76 assert(link
->network
);
79 /* If the link has a preference for a particular subnet id try to allocate that */
80 if ((uint64_t) subnet_id
>= n_prefixes
)
81 return log_link_debug_errno(link
,
82 SYNTHETIC_ERRNO(ERANGE
),
83 "subnet id %" PRIi64
" is out of range. Only have %" PRIu64
" subnets.",
87 r
= in_addr_prefix_nth(AF_INET6
, &prefix
, 64, subnet_id
);
89 return log_link_debug_errno(link
,
91 "subnet id %" PRIi64
" is out of range. Only have %" PRIu64
" subnets.",
95 /* Verify that the prefix we did calculate fits in the pd prefix.
96 * This should not fail as we checked the prefix size beforehand */
97 assert_se(in_addr_prefix_covers(AF_INET6
, &pd_prefix_union
, pd_prefix_len
, &prefix
) > 0);
99 Link
* assigned_link
= dhcp6_prefix_get(manager
, &prefix
.in6
);
101 (void) in_addr_to_string(AF_INET6
, &prefix
, &assigned_buf
);
103 if (assigned_link
&& assigned_link
!= link
)
104 return log_link_error_errno(link
, SYNTHETIC_ERRNO(EAGAIN
),
105 "The requested prefix %s is already assigned to another link: %s",
106 strnull(assigned_buf
),
107 strnull(assigned_link
->ifname
));
109 *ret_addr
= prefix
.in6
;
111 log_link_debug(link
, "The requested prefix %s is available. Using it.",
112 strnull(assigned_buf
));
116 for (uint64_t n
= 0; n
< n_prefixes
; n
++) {
117 /* if we do not have an allocation preference just iterate
118 * through the address space and return the first free prefix. */
119 Link
* assigned_link
= dhcp6_prefix_get(manager
, &prefix
.in6
);
121 if (!assigned_link
|| assigned_link
== link
) {
122 *ret_addr
= prefix
.in6
;
126 r
= in_addr_prefix_next(AF_INET6
, &prefix
, 64);
128 return log_link_error_errno(link
, r
, "Can't allocate another prefix. Out of address space?");
131 return log_link_warning_errno(link
, SYNTHETIC_ERRNO(ERANGE
), "Couldn't find a suitable prefix. Ran out of address space.");
134 static bool dhcp6_enable_prefix_delegation(Link
*dhcp6_link
) {
141 manager
= dhcp6_link
->manager
;
144 HASHMAP_FOREACH(l
, manager
->links
, i
) {
148 if (!dhcp6_get_prefix_delegation(l
))
157 static int dhcp6_lease_information_acquired(sd_dhcp6_client
*client
, Link
*link
) {
161 static int dhcp6_pd_prefix_assign(Link
*link
, struct in6_addr
*prefix
,
163 uint32_t lifetime_preferred
,
164 uint32_t lifetime_valid
) {
167 r
= radv_add_prefix(link
, prefix
, prefix_len
, lifetime_preferred
, lifetime_valid
);
171 r
= dhcp6_prefix_add(link
->manager
, prefix
, link
);
175 if (link
->network
->dhcp6_pd_assign_prefix
) {
176 r
= dhcp6_assign_delegated_prefix(link
, prefix
, prefix_len
, lifetime_preferred
, lifetime_valid
);
184 static int dhcp6_route_remove_handler(sd_netlink
*nl
, sd_netlink_message
*m
, Link
*link
) {
189 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
192 r
= sd_netlink_message_get_errno(m
);
194 log_link_message_warning_errno(link
, m
, r
, "Received error on unreachable route removal for DHCPv6 delegated subnet");
199 int dhcp6_lease_pd_prefix_lost(sd_dhcp6_client
*client
, Link
* link
) {
200 uint32_t lifetime_preferred
, lifetime_valid
;
201 union in_addr_union pd_prefix
;
202 uint8_t pd_prefix_len
;
203 sd_dhcp6_lease
*lease
;
206 r
= sd_dhcp6_client_get_lease(client
, &lease
);
210 sd_dhcp6_lease_reset_pd_prefix_iter(lease
);
212 while (sd_dhcp6_lease_get_pd(lease
, &pd_prefix
.in6
, &pd_prefix_len
,
214 &lifetime_valid
) >= 0) {
215 _cleanup_free_
char *buf
= NULL
;
216 _cleanup_(route_freep
) Route
*route
= NULL
;
218 if (pd_prefix_len
>= 64)
221 (void) in_addr_to_string(AF_INET6
, &pd_prefix
, &buf
);
223 r
= route_new(&route
);
227 route
->family
= AF_INET6
;
228 route
->dst
= pd_prefix
;
229 route
->dst_prefixlen
= pd_prefix_len
;
230 route
->type
= RTN_UNREACHABLE
;
232 r
= route_remove(route
, link
, dhcp6_route_remove_handler
);
234 log_link_warning_errno(link
, r
, "Cannot delete unreachable route for DHCPv6 delegated subnet %s/%u: %m",
240 log_link_debug(link
, "Removing unreachable route %s/%u",
241 strnull(buf
), pd_prefix_len
);
247 static int dhcp6_pd_prefix_distribute(Link
*dhcp6_link
,
248 struct in6_addr
*pd_prefix
,
249 uint8_t pd_prefix_len
,
250 uint32_t lifetime_preferred
,
251 uint32_t lifetime_valid
,
252 bool assign_preferred_subnet_id
) {
254 _cleanup_free_
char *assigned_buf
= NULL
, *buf
= NULL
;
255 Manager
*manager
= dhcp6_link
->manager
;
256 union in_addr_union prefix
= {
259 bool pool_depleted
= false;
266 assert(pd_prefix_len
<= 64);
268 r
= in_addr_mask(AF_INET6
, &prefix
, pd_prefix_len
);
272 n_prefixes
= UINT64_C(1) << (64 - pd_prefix_len
);
274 (void) in_addr_to_string(AF_INET6
, &prefix
, &buf
);
275 log_link_debug(dhcp6_link
, "Assigning up to %" PRIu64
" prefixes from %s/%u",
276 n_prefixes
, strnull(buf
), pd_prefix_len
);
278 HASHMAP_FOREACH(link
, manager
->links
, i
) {
279 union in_addr_union assigned_prefix
;
281 if (link
== dhcp6_link
)
284 if (!dhcp6_get_prefix_delegation(link
))
287 if (assign_preferred_subnet_id
!= dhcp6_has_preferred_subnet_id(link
))
290 r
= dhcp6_get_preferred_delegated_prefix(manager
, link
, &prefix
.in6
, pd_prefix_len
,
291 &assigned_prefix
.in6
);
293 if (assign_preferred_subnet_id
&& r
== -EAGAIN
) {
294 /* A link has a preferred subnet_id but that one is
295 * already taken by another link. Now all the remaining
296 * links will also not obtain a prefix. */
297 pool_depleted
= true;
302 (void) in_addr_to_string(AF_INET6
, &assigned_prefix
, &assigned_buf
);
303 r
= dhcp6_pd_prefix_assign(link
, &assigned_prefix
.in6
, 64,
304 lifetime_preferred
, lifetime_valid
);
306 log_link_error_errno(link
, r
, "Unable to assign/update prefix %s/64 from %s/%u for link: %m",
307 strnull(assigned_buf
),
308 strnull(buf
), pd_prefix_len
);
310 log_link_debug(link
, "Assigned prefix %s/64 from %s/%u to link",
311 strnull(assigned_buf
),
312 strnull(buf
), pd_prefix_len
);
315 /* If one of the link requests couldn't be fulfilled, signal that we
316 should try again with another prefix. */
323 static int dhcp6_route_handler(sd_netlink
*nl
, sd_netlink_message
*m
, Link
*link
) {
328 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
331 r
= sd_netlink_message_get_errno(m
);
332 if (r
< 0 && r
!= -EEXIST
)
333 log_link_message_warning_errno(link
, m
, r
, "Received error when adding unreachable route for DHCPv6 delegated subnet");
338 static int dhcp6_lease_pd_prefix_acquired(sd_dhcp6_client
*client
, Link
*link
) {
339 uint32_t lifetime_preferred
, lifetime_valid
;
340 union in_addr_union pd_prefix
;
341 sd_dhcp6_lease
*lease
;
342 uint8_t pd_prefix_len
;
345 r
= sd_dhcp6_client_get_lease(client
, &lease
);
349 sd_dhcp6_lease_reset_pd_prefix_iter(lease
);
351 while (sd_dhcp6_lease_get_pd(lease
, &pd_prefix
.in6
, &pd_prefix_len
,
353 &lifetime_valid
) >= 0) {
355 _cleanup_free_
char *buf
= NULL
;
357 (void) in_addr_to_string(AF_INET6
, &pd_prefix
, &buf
);
359 if (pd_prefix_len
> 64) {
360 log_link_debug(link
, "PD Prefix length > 64, ignoring prefix %s/%u",
361 strnull(buf
), pd_prefix_len
);
365 if (pd_prefix_len
< 48)
366 log_link_warning(link
, "PD Prefix length < 48, looks unusual %s/%u",
367 strnull(buf
), pd_prefix_len
);
369 if (pd_prefix_len
< 64) {
370 _cleanup_(route_freep
) Route
*route
= NULL
;
372 r
= route_new(&route
);
376 route
->family
= AF_INET6
;
377 route
->dst
= pd_prefix
;
378 route
->dst_prefixlen
= pd_prefix_len
;
379 route
->table
= link_get_dhcp_route_table(link
);
380 route
->type
= RTN_UNREACHABLE
;
382 r
= route_configure(route
, link
, dhcp6_route_handler
);
384 log_link_warning_errno(link
, r
, "Cannot configure unreachable route for delegated subnet %s/%u: %m",
390 log_link_debug(link
, "Configuring unreachable route for %s/%u",
391 strnull(buf
), pd_prefix_len
);
393 log_link_debug(link
, "Not adding a blocking route since distributed prefix is /64");
395 /* We are doing prefix allocation in two steps:
396 * 1. all those links that have a preferred subnet id will be assigned their subnet
397 * 2. all those links that remain will receive prefixes in sequential
398 * order. Prefixes that were previously already allocated to another
399 * link will be skipped.
401 * If a subnet id request couldn't be fulfilled the failure will be logged (as error)
402 * and no further attempts at obtaining a prefix will be made.
404 * The assignment has to be split in two phases since subnet id
405 * preferences should be honored. Meaning that any subnet id should be
406 * handed out to the requesting link and not to some link that didn't
407 * specify any preference. */
409 r
= dhcp6_pd_prefix_distribute(link
, &pd_prefix
.in6
,
414 if (r
< 0 && r
!= -EAGAIN
)
417 /* if r == -EAGAIN then the allocation failed because we ran
418 * out of addresses for the preferred subnet id's. This doesn't
419 * mean we can't fulfill other prefix requests.
421 * Since we do not have dedicated lists of links that request
422 * specific subnet id's and those that accept any prefix we
423 * *must* reset the iterator to the start as otherwise some
424 * links might not get their requested prefix. */
426 r
= dhcp6_pd_prefix_distribute(link
, &pd_prefix
.in6
,
431 if (r
< 0 && r
!= -EAGAIN
)
434 /* If the prefix distribution did return -EAGAIN we will try to
435 * fulfill those with the next available pd delegated prefix. */
441 int dhcp6_request_prefix_delegation(Link
*link
) {
445 assert_return(link
, -EINVAL
);
446 assert_return(link
->manager
, -EOPNOTSUPP
);
448 if (dhcp6_get_prefix_delegation(link
) <= 0)
451 log_link_debug(link
, "Requesting DHCPv6 prefixes to be delegated for new link");
453 HASHMAP_FOREACH(l
, link
->manager
->links
, i
) {
459 if (!l
->dhcp6_client
)
462 r
= sd_dhcp6_client_get_prefix_delegation(l
->dhcp6_client
, &enabled
);
464 log_link_warning_errno(l
, r
, "Cannot get prefix delegation when adding new link");
469 r
= sd_dhcp6_client_set_prefix_delegation(l
->dhcp6_client
, 1);
471 log_link_warning_errno(l
, r
, "Cannot enable prefix delegation when adding new link");
476 r
= sd_dhcp6_client_is_running(l
->dhcp6_client
);
481 log_link_debug(l
, "Requesting re-assignment of delegated prefixes after adding new link");
482 (void) dhcp6_lease_pd_prefix_acquired(l
->dhcp6_client
, l
);
487 r
= sd_dhcp6_client_stop(l
->dhcp6_client
);
489 log_link_warning_errno(l
, r
, "Cannot stop DHCPv6 prefix delegation client after adding new link");
493 r
= sd_dhcp6_client_start(l
->dhcp6_client
);
495 log_link_warning_errno(l
, r
, "Cannot restart DHCPv6 prefix delegation client after adding new link");
499 log_link_debug(l
, "Restarted DHCPv6 client to acquire prefix delegations after adding new link");
505 static int dhcp6_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
510 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
513 r
= sd_netlink_message_get_errno(m
);
514 if (r
< 0 && r
!= -EEXIST
) {
515 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv6 address");
516 link_enter_failed(link
);
519 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
521 r
= link_request_set_routes(link
);
523 link_enter_failed(link
);
530 static int dhcp6_address_change(
532 struct in6_addr
*ip6_addr
,
533 uint32_t lifetime_preferred
,
534 uint32_t lifetime_valid
) {
536 _cleanup_(address_freep
) Address
*addr
= NULL
;
537 _cleanup_free_
char *buffer
= NULL
;
540 r
= address_new(&addr
);
544 addr
->family
= AF_INET6
;
545 addr
->in_addr
.in6
= *ip6_addr
;
546 addr
->flags
= IFA_F_NOPREFIXROUTE
;
547 addr
->prefixlen
= 128;
548 addr
->cinfo
.ifa_prefered
= lifetime_preferred
;
549 addr
->cinfo
.ifa_valid
= lifetime_valid
;
551 (void) in_addr_to_string(addr
->family
, &addr
->in_addr
, &buffer
);
553 "DHCPv6 address %s/%d timeout preferred %d valid %d",
554 strnull(buffer
), addr
->prefixlen
, lifetime_preferred
, lifetime_valid
);
556 r
= address_configure(addr
, link
, dhcp6_address_handler
, true);
558 return log_link_warning_errno(link
, r
, "Could not assign DHCPv6 address: %m");
563 static int dhcp6_lease_address_acquired(sd_dhcp6_client
*client
, Link
*link
) {
565 sd_dhcp6_lease
*lease
;
566 struct in6_addr ip6_addr
;
567 uint32_t lifetime_preferred
, lifetime_valid
;
569 r
= sd_dhcp6_client_get_lease(client
, &lease
);
573 sd_dhcp6_lease_reset_address_iter(lease
);
575 while (sd_dhcp6_lease_get_address(lease
, &ip6_addr
,
577 &lifetime_valid
) >= 0) {
579 r
= dhcp6_address_change(link
, &ip6_addr
, lifetime_preferred
, lifetime_valid
);
587 static void dhcp6_handler(sd_dhcp6_client
*client
, int event
, void *userdata
) {
589 Link
*link
= userdata
;
592 assert(link
->network
);
594 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
598 case SD_DHCP6_CLIENT_EVENT_STOP
:
599 case SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE
:
600 case SD_DHCP6_CLIENT_EVENT_RETRANS_MAX
:
601 if (sd_dhcp6_client_get_lease(client
, NULL
) >= 0)
602 log_link_warning(link
, "DHCPv6 lease lost");
604 (void) dhcp6_lease_pd_prefix_lost(client
, link
);
605 (void) dhcp6_prefix_remove_all(link
->manager
, link
);
608 link
->dhcp6_configured
= false;
611 case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE
:
612 r
= dhcp6_lease_address_acquired(client
, link
);
614 link_enter_failed(link
);
618 r
= dhcp6_lease_pd_prefix_acquired(client
, link
);
620 log_link_debug(link
, "DHCPv6 did not receive prefixes to delegate");
623 case SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST
:
624 r
= dhcp6_lease_information_acquired(client
, link
);
626 link_enter_failed(link
);
631 link
->dhcp6_configured
= true;
636 log_link_warning_errno(link
, event
, "DHCPv6 error: %m");
638 log_link_warning(link
, "DHCPv6 unknown event: %d", event
);
642 link_check_ready(link
);
645 int dhcp6_request_address(Link
*link
, int ir
) {
650 assert(link
->dhcp6_client
);
651 assert(link
->network
);
652 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
654 r
= sd_dhcp6_client_is_running(link
->dhcp6_client
);
659 r
= sd_dhcp6_client_get_prefix_delegation(link
->dhcp6_client
, &pd
);
663 if (pd
&& ir
&& link
->network
->dhcp6_force_pd_other_information
) {
664 log_link_debug(link
, "Enabling managed mode to request DHCPv6 PD with 'Other Information' set");
666 r
= sd_dhcp6_client_set_address_request(link
->dhcp6_client
,
675 r
= sd_dhcp6_client_get_information_request(link
->dhcp6_client
, &inf_req
);
682 r
= sd_dhcp6_client_stop(link
->dhcp6_client
);
686 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
691 r
= sd_dhcp6_client_set_information_request(link
->dhcp6_client
, ir
);
695 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
702 static int dhcp6_set_hostname(sd_dhcp6_client
*client
, Link
*link
) {
703 _cleanup_free_
char *hostname
= NULL
;
709 if (!link
->network
->dhcp_send_hostname
)
711 else if (link
->network
->dhcp_hostname
)
712 hn
= link
->network
->dhcp_hostname
;
714 r
= gethostname_strict(&hostname
);
715 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
721 r
= sd_dhcp6_client_set_fqdn(client
, hn
);
722 if (r
== -EINVAL
&& hostname
)
723 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
724 log_link_warning_errno(link
, r
, "DHCP6 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
726 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set hostname: %m");
731 int dhcp6_configure(Link
*link
) {
732 _cleanup_(sd_dhcp6_client_unrefp
) sd_dhcp6_client
*client
= NULL
;
733 sd_dhcp6_option
*vendor_option
;
734 sd_dhcp6_option
*send_option
;
735 void *request_options
;
741 assert(link
->network
);
743 if (link
->dhcp6_client
)
746 r
= sd_dhcp6_client_new(&client
);
750 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to create DHCP6 client: %m");
752 r
= sd_dhcp6_client_attach_event(client
, NULL
, 0);
754 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to attach event: %m");
756 r
= sd_dhcp6_client_set_mac(client
,
757 (const uint8_t *) &link
->mac
,
758 sizeof (link
->mac
), ARPHRD_ETHER
);
760 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set MAC address: %m");
762 if (link
->network
->iaid_set
) {
763 r
= sd_dhcp6_client_set_iaid(client
, link
->network
->iaid
);
765 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set IAID: %m");
768 duid
= link_get_duid(link
);
769 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
770 r
= sd_dhcp6_client_set_duid_llt(client
, duid
->llt_time
);
772 r
= sd_dhcp6_client_set_duid(client
,
774 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
777 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set DUID: %m");
779 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp6_client_send_options
, i
) {
780 r
= sd_dhcp6_client_add_option(client
, send_option
);
784 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set option: %m");
787 r
= dhcp6_set_hostname(client
, link
);
791 r
= sd_dhcp6_client_set_ifindex(client
, link
->ifindex
);
793 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set ifindex: %m");
795 if (link
->network
->rapid_commit
) {
796 r
= sd_dhcp6_client_set_request_option(client
, SD_DHCP6_OPTION_RAPID_COMMIT
);
798 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set request flag for rapid commit: %m");
801 if (link
->network
->dhcp6_mudurl
) {
802 r
= sd_dhcp6_client_set_request_mud_url(client
, link
->network
->dhcp6_mudurl
);
804 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set MUD URL: %m");
807 SET_FOREACH(request_options
, link
->network
->dhcp6_request_options
, i
) {
808 uint32_t option
= PTR_TO_UINT32(request_options
);
810 r
= sd_dhcp6_client_set_request_option(client
, option
);
812 log_link_debug(link
, "DHCP6 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option
);
817 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set request flag for '%u': %m", option
);
820 if (link
->network
->dhcp6_user_class
) {
821 r
= sd_dhcp6_client_set_request_user_class(client
, link
->network
->dhcp6_user_class
);
823 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set user class: %m");
826 if (link
->network
->dhcp6_vendor_class
) {
827 r
= sd_dhcp6_client_set_request_vendor_class(client
, link
->network
->dhcp6_vendor_class
);
829 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set vendor class: %m");
832 ORDERED_HASHMAP_FOREACH(vendor_option
, link
->network
->dhcp6_client_send_vendor_options
, i
) {
833 r
= sd_dhcp6_client_add_vendor_option(client
, vendor_option
);
837 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set vendor option: %m");
840 r
= sd_dhcp6_client_set_callback(client
, dhcp6_handler
, link
);
842 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set callback: %m");
844 if (dhcp6_enable_prefix_delegation(link
)) {
845 r
= sd_dhcp6_client_set_prefix_delegation(client
, true);
847 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set prefix delegation: %m");
850 if (link
->network
->dhcp6_pd_length
> 0) {
851 r
= sd_dhcp6_client_set_prefix_delegation_hint(client
, link
->network
->dhcp6_pd_length
, &link
->network
->dhcp6_pd_address
);
853 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set prefix hint: %m");
856 link
->dhcp6_client
= TAKE_PTR(client
);
861 static Link
*dhcp6_prefix_get(Manager
*m
, struct in6_addr
*addr
) {
862 assert_return(m
, NULL
);
863 assert_return(addr
, NULL
);
865 return hashmap_get(m
->dhcp6_prefixes
, addr
);
868 static int dhcp6_route_add_handler(sd_netlink
*nl
, sd_netlink_message
*m
, Link
*link
) {
873 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
876 r
= sd_netlink_message_get_errno(m
);
877 if (r
< 0 && r
!= -EEXIST
) {
878 log_link_message_warning_errno(link
, m
, r
, "Received error adding DHCPv6 Prefix Delegation route");
879 link_enter_failed(link
);
886 static int dhcp6_prefix_add(Manager
*m
, struct in6_addr
*addr
, Link
*link
) {
887 _cleanup_(route_freep
) Route
*route
= NULL
;
888 _cleanup_free_
struct in6_addr
*a
= NULL
;
889 _cleanup_free_
char *buf
= NULL
;
893 assert_return(m
, -EINVAL
);
894 assert_return(addr
, -EINVAL
);
896 r
= route_new(&route
);
900 route
->family
= AF_INET6
;
901 route
->dst
.in6
= *addr
;
902 route
->dst_prefixlen
= 64;
904 r
= route_configure(route
, link
, dhcp6_route_add_handler
);
908 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) addr
, &buf
);
909 log_link_debug(link
, "Adding prefix route %s/64", strnull(buf
));
911 assigned_link
= hashmap_get(m
->dhcp6_prefixes
, addr
);
913 assert(assigned_link
== link
);
917 a
= newdup(struct in6_addr
, addr
, 1);
921 r
= hashmap_ensure_allocated(&m
->dhcp6_prefixes
, &in6_addr_hash_ops
);
925 r
= hashmap_put(m
->dhcp6_prefixes
, a
, link
);
934 static int dhcp6_prefix_remove_handler(sd_netlink
*nl
, sd_netlink_message
*m
, Link
*link
) {
939 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
942 r
= sd_netlink_message_get_errno(m
);
944 log_link_message_warning_errno(link
, m
, r
, "Received error on DHCPv6 Prefix Delegation route removal");
945 link_enter_failed(link
);
952 int dhcp6_prefix_remove(Manager
*m
, struct in6_addr
*addr
) {
953 _cleanup_free_
struct in6_addr
*a
= NULL
;
954 _cleanup_(link_unrefp
) Link
*l
= NULL
;
955 _cleanup_(route_freep
) Route
*route
= NULL
;
956 _cleanup_free_
char *buf
= NULL
;
959 assert_return(m
, -EINVAL
);
960 assert_return(addr
, -EINVAL
);
962 l
= hashmap_remove2(m
->dhcp6_prefixes
, addr
, (void **) &a
);
966 (void) sd_radv_remove_prefix(l
->radv
, addr
, 64);
968 r
= route_new(&route
);
972 route
->family
= AF_INET6
;
973 route
->dst
.in6
= *addr
;
974 route
->dst_prefixlen
= 64;
976 r
= route_remove(route
, l
, dhcp6_prefix_remove_handler
);
980 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) addr
, &buf
);
981 log_link_debug(l
, "Removing prefix route %s/64", strnull(buf
));
986 static int dhcp6_prefix_remove_all(Manager
*m
, Link
*link
) {
987 struct in6_addr
*addr
;
991 assert_return(m
, -EINVAL
);
992 assert_return(link
, -EINVAL
);
994 HASHMAP_FOREACH_KEY(l
, addr
, m
->dhcp6_prefixes
, i
)
996 (void) dhcp6_prefix_remove(m
, addr
);
1001 static int dhcp6_assign_delegeted_prefix_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1006 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1009 r
= sd_netlink_message_get_errno(m
);
1010 if (r
< 0 && r
!= -EEXIST
) {
1011 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv6 delegated prefix address ");
1012 link_enter_failed(link
);
1015 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1017 r
= link_request_set_routes(link
);
1019 link_enter_failed(link
);
1026 static int dhcp6_assign_delegated_prefix(Link
*link
,
1027 const struct in6_addr
*prefix
,
1029 uint32_t lifetime_preferred
,
1030 uint32_t lifetime_valid
) {
1032 _cleanup_(address_freep
) Address
*address
= NULL
;
1036 assert(link
->network
);
1039 if (!link
->network
->dhcp6_pd_assign_prefix
)
1042 r
= address_new(&address
);
1044 return log_link_error_errno(link
, r
, "Failed to allocate address for DHCPv6 delegated prefix: %m");
1046 address
->in_addr
.in6
= *prefix
;
1048 if (!in_addr_is_null(AF_INET6
, &link
->network
->dhcp6_delegation_prefix_token
))
1049 memcpy(address
->in_addr
.in6
.s6_addr
+ 8, link
->network
->dhcp6_delegation_prefix_token
.in6
.s6_addr
+ 8, 8);
1051 r
= generate_ipv6_eui_64_address(link
, &address
->in_addr
.in6
);
1053 return log_link_warning_errno(link
, r
, "Failed to generate EUI64 address for acquired DHCPv6 delegated prefix: %m");
1056 address
->prefixlen
= prefix_len
;
1057 address
->family
= AF_INET6
;
1058 address
->cinfo
.ifa_prefered
= lifetime_preferred
;
1059 address
->cinfo
.ifa_valid
= lifetime_valid
;
1061 link_set_state(link
, LINK_STATE_CONFIGURING
);
1063 r
= address_configure(address
, link
, dhcp6_assign_delegeted_prefix_address_handler
, true);
1065 return log_link_warning_errno(link
, r
, "Failed to set acquired DHCPv6 delegated prefix address: %m");
1070 int config_parse_dhcp6_pd_hint(
1072 const char *filename
,
1074 const char *section
,
1075 unsigned section_line
,
1082 Network
*network
= data
;
1090 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, (union in_addr_union
*) &network
->dhcp6_pd_address
, &network
->dhcp6_pd_length
);
1092 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse PrefixDelegationHint=%s, ignoring assignment", rvalue
);
1096 if (network
->dhcp6_pd_length
< 1 || network
->dhcp6_pd_length
> 128) {
1097 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid prefix length='%d', ignoring assignment", network
->dhcp6_pd_length
);
1098 network
->dhcp6_pd_length
= 0;
1105 int config_parse_dhcp6_mud_url(
1107 const char *filename
,
1109 const char *section
,
1110 unsigned section_line
,
1117 _cleanup_free_
char *unescaped
= NULL
;
1118 Network
*network
= data
;
1125 if (isempty(rvalue
)) {
1126 network
->dhcp6_mudurl
= mfree(network
->dhcp6_mudurl
);
1130 r
= cunescape(rvalue
, 0, &unescaped
);
1132 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1133 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue
);
1137 if (!http_url_is_valid(unescaped
) || strlen(unescaped
) > UINT8_MAX
) {
1138 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1139 "Failed to parse MUD URL '%s', ignoring: %m", rvalue
);
1144 return free_and_replace(network
->dhcp6_mudurl
, unescaped
);
1147 int config_parse_dhcp6_delegated_prefix_token(
1149 const char *filename
,
1151 const char *section
,
1152 unsigned section_line
,
1159 Network
*network
= data
;
1167 if (isempty(rvalue
)) {
1168 network
->dhcp6_delegation_prefix_token
= IN_ADDR_NULL
;
1172 r
= in_addr_from_string(AF_INET6
, rvalue
, &network
->dhcp6_delegation_prefix_token
);
1174 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1175 "Failed to parse DHCPv6 %s, ignoring: %s", lvalue
, rvalue
);
1179 if (in_addr_is_null(AF_INET6
, &network
->dhcp6_delegation_prefix_token
)) {
1180 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1181 "DHCPv6 %s cannot be the ANY address, ignoring: %s", lvalue
, rvalue
);
1188 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp6_client_start_mode
, dhcp6_client_start_mode
, DHCP6ClientStartMode
,
1189 "Failed to parse WithoutRA= setting");
1191 static const char* const dhcp6_client_start_mode_table
[_DHCP6_CLIENT_START_MODE_MAX
] = {
1192 [DHCP6_CLIENT_START_MODE_NO
] = "no",
1193 [DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST
] = "information-request",
1194 [DHCP6_CLIENT_START_MODE_SOLICIT
] = "solicit",
1197 DEFINE_STRING_TABLE_LOOKUP(dhcp6_client_start_mode
, DHCP6ClientStartMode
);