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 Link
*dhcp6_prefix_get(Manager
*m
, struct in6_addr
*addr
);
28 static int dhcp6_prefix_add(Manager
*m
, struct in6_addr
*addr
, Link
*link
);
29 static int dhcp6_prefix_remove_all(Manager
*m
, Link
*link
);
30 static int dhcp6_assign_delegated_prefix(Link
*link
, const struct in6_addr
*prefix
,
32 uint32_t lifetime_preferred
,
33 uint32_t lifetime_valid
);
35 bool dhcp6_get_prefix_delegation(Link
*link
) {
39 return IN_SET(link
->network
->router_prefix_delegation
,
40 RADV_PREFIX_DELEGATION_DHCP6
,
41 RADV_PREFIX_DELEGATION_BOTH
);
44 static bool dhcp6_has_preferred_subnet_id(Link
*link
) {
48 return link
->network
->router_prefix_subnet_id
>= 0;
51 static int dhcp6_get_preferred_delegated_prefix(
54 const struct in6_addr
*pd_prefix
,
55 uint8_t pd_prefix_len
,
56 struct in6_addr
*ret_addr
) {
58 int64_t subnet_id
= link
->network
->router_prefix_subnet_id
;
59 uint8_t prefix_bits
= 64 - pd_prefix_len
;
60 uint64_t n_prefixes
= UINT64_C(1) << prefix_bits
;
61 _cleanup_free_
char *assigned_buf
= NULL
;
62 union in_addr_union pd_prefix_union
= {
65 /* We start off with the original PD prefix we have been assigned and
66 * iterate from there */
67 union in_addr_union prefix
= {
72 assert(pd_prefix_len
<= 64);
75 assert(link
->network
);
78 /* If the link has a preference for a particular subnet id try to allocate that */
79 if ((uint64_t) subnet_id
>= n_prefixes
)
80 return log_link_debug_errno(link
,
81 SYNTHETIC_ERRNO(ERANGE
),
82 "subnet id %" PRIi64
" is out of range. Only have %" PRIu64
" subnets.",
86 r
= in_addr_prefix_nth(AF_INET6
, &prefix
, 64, subnet_id
);
88 return log_link_debug_errno(link
,
90 "subnet id %" PRIi64
" is out of range. Only have %" PRIu64
" subnets.",
94 /* Verify that the prefix we did calculate fits in the pd prefix.
95 * This should not fail as we checked the prefix size beforehand */
96 assert_se(in_addr_prefix_covers(AF_INET6
, &pd_prefix_union
, pd_prefix_len
, &prefix
) > 0);
98 Link
* assigned_link
= dhcp6_prefix_get(manager
, &prefix
.in6
);
100 (void) in_addr_to_string(AF_INET6
, &prefix
, &assigned_buf
);
102 if (assigned_link
&& assigned_link
!= link
)
103 return log_link_error_errno(link
, SYNTHETIC_ERRNO(EAGAIN
),
104 "The requested prefix %s is already assigned to another link: %s",
105 strnull(assigned_buf
),
106 strnull(assigned_link
->ifname
));
108 *ret_addr
= prefix
.in6
;
110 log_link_debug(link
, "The requested prefix %s is available. Using it.",
111 strnull(assigned_buf
));
115 for (uint64_t n
= 0; n
< n_prefixes
; n
++) {
116 /* if we do not have an allocation preference just iterate
117 * through the address space and return the first free prefix. */
118 Link
* assigned_link
= dhcp6_prefix_get(manager
, &prefix
.in6
);
120 if (!assigned_link
|| assigned_link
== link
) {
121 *ret_addr
= prefix
.in6
;
125 r
= in_addr_prefix_next(AF_INET6
, &prefix
, 64);
127 return log_link_error_errno(link
, r
, "Can't allocate another prefix. Out of address space?: %m");
130 return log_link_warning_errno(link
, SYNTHETIC_ERRNO(ERANGE
), "Couldn't find a suitable prefix. Ran out of address space.");
133 static bool dhcp6_enable_prefix_delegation(Link
*dhcp6_link
) {
140 manager
= dhcp6_link
->manager
;
143 HASHMAP_FOREACH(l
, manager
->links
, i
) {
147 if (!dhcp6_get_prefix_delegation(l
))
156 static int dhcp6_lease_information_acquired(sd_dhcp6_client
*client
, Link
*link
) {
160 static int dhcp6_pd_prefix_assign(Link
*link
, struct in6_addr
*prefix
,
162 uint32_t lifetime_preferred
,
163 uint32_t lifetime_valid
) {
166 r
= radv_add_prefix(link
, prefix
, prefix_len
, lifetime_preferred
, lifetime_valid
);
170 r
= dhcp6_prefix_add(link
->manager
, prefix
, link
);
174 r
= dhcp6_assign_delegated_prefix(link
, prefix
, prefix_len
, lifetime_preferred
, lifetime_valid
);
181 static int dhcp6_route_remove_handler(sd_netlink
*nl
, sd_netlink_message
*m
, Link
*link
) {
186 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
189 r
= sd_netlink_message_get_errno(m
);
191 log_link_message_warning_errno(link
, m
, r
, "Received error on unreachable route removal for DHCPv6 delegated subnet");
196 int dhcp6_lease_pd_prefix_lost(sd_dhcp6_client
*client
, Link
* link
) {
197 uint32_t lifetime_preferred
, lifetime_valid
;
198 union in_addr_union pd_prefix
;
199 uint8_t pd_prefix_len
;
200 sd_dhcp6_lease
*lease
;
203 r
= sd_dhcp6_client_get_lease(client
, &lease
);
207 sd_dhcp6_lease_reset_pd_prefix_iter(lease
);
209 while (sd_dhcp6_lease_get_pd(lease
, &pd_prefix
.in6
, &pd_prefix_len
,
211 &lifetime_valid
) >= 0) {
212 _cleanup_free_
char *buf
= NULL
;
213 _cleanup_(route_freep
) Route
*route
= NULL
;
215 if (pd_prefix_len
>= 64)
218 (void) in_addr_to_string(AF_INET6
, &pd_prefix
, &buf
);
220 r
= route_new(&route
);
224 route
->family
= AF_INET6
;
225 route
->dst
= pd_prefix
;
226 route
->dst_prefixlen
= pd_prefix_len
;
227 route
->type
= RTN_UNREACHABLE
;
229 r
= route_remove(route
, link
, dhcp6_route_remove_handler
);
231 log_link_warning_errno(link
, r
, "Cannot delete unreachable route for DHCPv6 delegated subnet %s/%u: %m",
237 log_link_debug(link
, "Removing unreachable route %s/%u",
238 strnull(buf
), pd_prefix_len
);
244 static int dhcp6_pd_prefix_distribute(Link
*dhcp6_link
,
245 struct in6_addr
*pd_prefix
,
246 uint8_t pd_prefix_len
,
247 uint32_t lifetime_preferred
,
248 uint32_t lifetime_valid
,
249 bool assign_preferred_subnet_id
) {
251 _cleanup_free_
char *assigned_buf
= NULL
, *buf
= NULL
;
252 Manager
*manager
= dhcp6_link
->manager
;
253 union in_addr_union prefix
= {
256 bool pool_depleted
= false;
263 assert(pd_prefix_len
<= 64);
265 r
= in_addr_mask(AF_INET6
, &prefix
, pd_prefix_len
);
269 n_prefixes
= UINT64_C(1) << (64 - pd_prefix_len
);
271 (void) in_addr_to_string(AF_INET6
, &prefix
, &buf
);
272 log_link_debug(dhcp6_link
, "Assigning up to %" PRIu64
" prefixes from %s/%u",
273 n_prefixes
, strnull(buf
), pd_prefix_len
);
275 HASHMAP_FOREACH(link
, manager
->links
, i
) {
276 union in_addr_union assigned_prefix
;
278 if (link
== dhcp6_link
)
281 if (!dhcp6_get_prefix_delegation(link
))
284 if (assign_preferred_subnet_id
!= dhcp6_has_preferred_subnet_id(link
))
287 r
= dhcp6_get_preferred_delegated_prefix(manager
, link
, &prefix
.in6
, pd_prefix_len
,
288 &assigned_prefix
.in6
);
290 if (assign_preferred_subnet_id
&& r
== -EAGAIN
) {
291 /* A link has a preferred subnet_id but that one is
292 * already taken by another link. Now all the remaining
293 * links will also not obtain a prefix. */
294 pool_depleted
= true;
299 (void) in_addr_to_string(AF_INET6
, &assigned_prefix
, &assigned_buf
);
300 r
= dhcp6_pd_prefix_assign(link
, &assigned_prefix
.in6
, 64,
301 lifetime_preferred
, lifetime_valid
);
303 log_link_error_errno(link
, r
, "Unable to assign/update prefix %s/64 from %s/%u for link: %m",
304 strnull(assigned_buf
),
305 strnull(buf
), pd_prefix_len
);
307 log_link_debug(link
, "Assigned prefix %s/64 from %s/%u to link",
308 strnull(assigned_buf
),
309 strnull(buf
), pd_prefix_len
);
312 /* If one of the link requests couldn't be fulfilled, signal that we
313 should try again with another prefix. */
320 static int dhcp6_route_handler(sd_netlink
*nl
, sd_netlink_message
*m
, Link
*link
) {
324 assert(link
->dhcp6_route_messages
> 0);
326 link
->dhcp6_route_messages
--;
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
, "Failed to add unreachable route for DHCPv6 delegated subnet");
334 link_enter_failed(link
);
338 if (link
->dhcp6_route_messages
== 0) {
339 log_link_debug(link
, "Unreachable routes for DHCPv6 delegated subnets set");
340 link
->dhcp6_route_configured
= true;
341 link_check_ready(link
);
347 static int dhcp6_lease_pd_prefix_acquired(sd_dhcp6_client
*client
, Link
*link
) {
348 uint32_t lifetime_preferred
, lifetime_valid
;
349 union in_addr_union pd_prefix
;
350 sd_dhcp6_lease
*lease
;
351 uint8_t pd_prefix_len
;
354 link
->dhcp6_route_configured
= false;
356 r
= sd_dhcp6_client_get_lease(client
, &lease
);
360 sd_dhcp6_lease_reset_pd_prefix_iter(lease
);
362 while (sd_dhcp6_lease_get_pd(lease
, &pd_prefix
.in6
, &pd_prefix_len
,
364 &lifetime_valid
) >= 0) {
366 _cleanup_free_
char *buf
= NULL
;
368 (void) in_addr_to_string(AF_INET6
, &pd_prefix
, &buf
);
370 if (pd_prefix_len
> 64) {
371 log_link_debug(link
, "PD Prefix length > 64, ignoring prefix %s/%u",
372 strnull(buf
), pd_prefix_len
);
376 if (pd_prefix_len
< 48)
377 log_link_warning(link
, "PD Prefix length < 48, looks unusual %s/%u",
378 strnull(buf
), pd_prefix_len
);
380 if (pd_prefix_len
< 64) {
381 _cleanup_(route_freep
) Route
*route
= NULL
;
383 r
= route_new(&route
);
387 route
->family
= AF_INET6
;
388 route
->dst
= pd_prefix
;
389 route
->dst_prefixlen
= pd_prefix_len
;
390 route
->table
= link_get_dhcp_route_table(link
);
391 route
->type
= RTN_UNREACHABLE
;
393 r
= route_configure(route
, link
, dhcp6_route_handler
);
395 log_link_warning_errno(link
, r
, "Cannot configure unreachable route for delegated subnet %s/%u: %m",
401 link
->dhcp6_route_messages
++;
403 log_link_debug(link
, "Configuring unreachable route for %s/%u",
404 strnull(buf
), pd_prefix_len
);
406 log_link_debug(link
, "Not adding a blocking route since distributed prefix is /64");
408 /* We are doing prefix allocation in two steps:
409 * 1. all those links that have a preferred subnet id will be assigned their subnet
410 * 2. all those links that remain will receive prefixes in sequential
411 * order. Prefixes that were previously already allocated to another
412 * link will be skipped.
414 * If a subnet id request couldn't be fulfilled the failure will be logged (as error)
415 * and no further attempts at obtaining a prefix will be made.
417 * The assignment has to be split in two phases since subnet id
418 * preferences should be honored. Meaning that any subnet id should be
419 * handed out to the requesting link and not to some link that didn't
420 * specify any preference. */
422 r
= dhcp6_pd_prefix_distribute(link
, &pd_prefix
.in6
,
427 if (r
< 0 && r
!= -EAGAIN
)
430 /* if r == -EAGAIN then the allocation failed because we ran
431 * out of addresses for the preferred subnet id's. This doesn't
432 * mean we can't fulfill other prefix requests.
434 * Since we do not have dedicated lists of links that request
435 * specific subnet id's and those that accept any prefix we
436 * *must* reset the iterator to the start as otherwise some
437 * links might not get their requested prefix. */
439 r
= dhcp6_pd_prefix_distribute(link
, &pd_prefix
.in6
,
444 if (r
< 0 && r
!= -EAGAIN
)
447 /* If the prefix distribution did return -EAGAIN we will try to
448 * fulfill those with the next available pd delegated prefix. */
451 if (link
->dhcp6_route_messages
== 0) {
452 link
->dhcp6_route_configured
= true;
453 link_check_ready(link
);
455 log_link_debug(link
, "Setting unreachable routes for DHCPv6 delegated subnets");
456 link_set_state(link
, LINK_STATE_CONFIGURING
);
462 int dhcp6_request_prefix_delegation(Link
*link
) {
466 assert_return(link
, -EINVAL
);
467 assert_return(link
->manager
, -EOPNOTSUPP
);
469 if (dhcp6_get_prefix_delegation(link
) <= 0)
472 log_link_debug(link
, "Requesting DHCPv6 prefixes to be delegated for new link");
474 HASHMAP_FOREACH(l
, link
->manager
->links
, i
) {
480 if (!l
->dhcp6_client
)
483 r
= sd_dhcp6_client_get_prefix_delegation(l
->dhcp6_client
, &enabled
);
485 log_link_warning_errno(l
, r
, "Cannot get prefix delegation when adding new link: %m");
490 r
= sd_dhcp6_client_set_prefix_delegation(l
->dhcp6_client
, 1);
492 log_link_warning_errno(l
, r
, "Cannot enable prefix delegation when adding new link: 5m");
497 r
= sd_dhcp6_client_is_running(l
->dhcp6_client
);
502 log_link_debug(l
, "Requesting re-assignment of delegated prefixes after adding new link");
503 (void) dhcp6_lease_pd_prefix_acquired(l
->dhcp6_client
, l
);
508 r
= sd_dhcp6_client_stop(l
->dhcp6_client
);
510 log_link_warning_errno(l
, r
, "Cannot stop DHCPv6 prefix delegation client after adding new link: %m");
514 r
= sd_dhcp6_client_start(l
->dhcp6_client
);
516 log_link_warning_errno(l
, r
, "Cannot restart DHCPv6 prefix delegation client after adding new link: %m");
520 log_link_debug(l
, "Restarted DHCPv6 client to acquire prefix delegations after adding new link");
526 static int dhcp6_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
530 assert(link
->dhcp6_address_messages
> 0);
532 link
->dhcp6_address_messages
--;
534 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
537 r
= sd_netlink_message_get_errno(m
);
538 if (r
< 0 && r
!= -EEXIST
) {
539 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv6 address");
540 link_enter_failed(link
);
543 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
545 if (link
->dhcp6_address_messages
== 0) {
546 log_link_debug(link
, "DHCPv6 addresses set");
547 link
->dhcp6_address_configured
= true;
548 r
= link_request_set_routes(link
);
550 link_enter_failed(link
);
558 static int dhcp6_address_change(
560 struct in6_addr
*ip6_addr
,
561 uint32_t lifetime_preferred
,
562 uint32_t lifetime_valid
) {
564 _cleanup_(address_freep
) Address
*addr
= NULL
;
565 _cleanup_free_
char *buffer
= NULL
;
568 r
= address_new(&addr
);
572 addr
->family
= AF_INET6
;
573 addr
->in_addr
.in6
= *ip6_addr
;
574 addr
->flags
= IFA_F_NOPREFIXROUTE
;
575 addr
->prefixlen
= 128;
576 addr
->cinfo
.ifa_prefered
= lifetime_preferred
;
577 addr
->cinfo
.ifa_valid
= lifetime_valid
;
579 (void) in_addr_to_string(addr
->family
, &addr
->in_addr
, &buffer
);
581 "DHCPv6 address %s/%d timeout preferred %d valid %d",
582 strnull(buffer
), addr
->prefixlen
, lifetime_preferred
, lifetime_valid
);
584 r
= address_configure(addr
, link
, dhcp6_address_handler
, true);
586 return log_link_warning_errno(link
, r
, "Could not assign DHCPv6 address: %m");
588 link
->dhcp6_address_messages
++;
593 static int dhcp6_lease_address_acquired(sd_dhcp6_client
*client
, Link
*link
) {
595 sd_dhcp6_lease
*lease
;
596 struct in6_addr ip6_addr
;
597 uint32_t lifetime_preferred
, lifetime_valid
;
599 link
->dhcp6_address_configured
= false;
601 r
= sd_dhcp6_client_get_lease(client
, &lease
);
605 sd_dhcp6_lease_reset_address_iter(lease
);
606 while (sd_dhcp6_lease_get_address(lease
, &ip6_addr
,
608 &lifetime_valid
) >= 0) {
610 r
= dhcp6_address_change(link
, &ip6_addr
, lifetime_preferred
, lifetime_valid
);
615 if (link
->dhcp6_address_messages
== 0) {
616 link
->dhcp6_address_configured
= true;
617 return link_request_set_routes(link
);
619 log_link_debug(link
, "Setting DHCPv6 addresses");
620 /* address_handler calls link_request_set_routes() and link_request_set_nexthop().
621 * Before they are called, the related flags must be cleared. Otherwise, the link
622 * becomes configured state before routes are configured. */
623 link
->static_routes_configured
= false;
624 link
->static_nexthops_configured
= false;
625 link_set_state(link
, LINK_STATE_CONFIGURING
);
631 static void dhcp6_handler(sd_dhcp6_client
*client
, int event
, void *userdata
) {
633 Link
*link
= userdata
;
636 assert(link
->network
);
638 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
642 case SD_DHCP6_CLIENT_EVENT_STOP
:
643 case SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE
:
644 case SD_DHCP6_CLIENT_EVENT_RETRANS_MAX
:
645 if (sd_dhcp6_client_get_lease(client
, NULL
) >= 0)
646 log_link_warning(link
, "DHCPv6 lease lost");
648 (void) dhcp6_lease_pd_prefix_lost(client
, link
);
649 (void) dhcp6_prefix_remove_all(link
->manager
, link
);
654 case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE
:
655 r
= dhcp6_lease_address_acquired(client
, link
);
657 link_enter_failed(link
);
661 r
= dhcp6_lease_pd_prefix_acquired(client
, link
);
663 log_link_debug_errno(link
, r
, "DHCPv6 did not receive prefixes to delegate: %m");
666 case SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST
:
667 r
= dhcp6_lease_information_acquired(client
, link
);
669 link_enter_failed(link
);
678 log_link_warning_errno(link
, event
, "DHCPv6 error: %m");
680 log_link_warning(link
, "DHCPv6 unknown event: %d", event
);
684 link_check_ready(link
);
687 int dhcp6_request_address(Link
*link
, int ir
) {
692 assert(link
->dhcp6_client
);
693 assert(link
->network
);
694 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
696 r
= sd_dhcp6_client_is_running(link
->dhcp6_client
);
701 r
= sd_dhcp6_client_get_prefix_delegation(link
->dhcp6_client
, &pd
);
705 if (pd
&& ir
&& link
->network
->dhcp6_force_pd_other_information
) {
706 log_link_debug(link
, "Enabling managed mode to request DHCPv6 PD with 'Other Information' set");
708 r
= sd_dhcp6_client_set_address_request(link
->dhcp6_client
,
717 r
= sd_dhcp6_client_get_information_request(link
->dhcp6_client
, &inf_req
);
724 r
= sd_dhcp6_client_stop(link
->dhcp6_client
);
728 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
733 r
= sd_dhcp6_client_set_information_request(link
->dhcp6_client
, ir
);
737 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
744 static int dhcp6_set_hostname(sd_dhcp6_client
*client
, Link
*link
) {
745 _cleanup_free_
char *hostname
= NULL
;
751 if (!link
->network
->dhcp_send_hostname
)
753 else if (link
->network
->dhcp_hostname
)
754 hn
= link
->network
->dhcp_hostname
;
756 r
= gethostname_strict(&hostname
);
757 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
763 r
= sd_dhcp6_client_set_fqdn(client
, hn
);
764 if (r
== -EINVAL
&& hostname
)
765 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
766 log_link_warning_errno(link
, r
, "DHCP6 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
768 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set hostname: %m");
773 int dhcp6_configure(Link
*link
) {
774 _cleanup_(sd_dhcp6_client_unrefp
) sd_dhcp6_client
*client
= NULL
;
775 sd_dhcp6_option
*vendor_option
;
776 sd_dhcp6_option
*send_option
;
777 void *request_options
;
783 assert(link
->network
);
785 if (link
->dhcp6_client
)
788 r
= sd_dhcp6_client_new(&client
);
792 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to create DHCP6 client: %m");
794 r
= sd_dhcp6_client_attach_event(client
, NULL
, 0);
796 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to attach event: %m");
798 r
= sd_dhcp6_client_set_mac(client
,
799 (const uint8_t *) &link
->mac
,
800 sizeof (link
->mac
), ARPHRD_ETHER
);
802 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set MAC address: %m");
804 if (link
->network
->iaid_set
) {
805 r
= sd_dhcp6_client_set_iaid(client
, link
->network
->iaid
);
807 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set IAID: %m");
810 duid
= link_get_duid(link
);
811 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
812 r
= sd_dhcp6_client_set_duid_llt(client
, duid
->llt_time
);
814 r
= sd_dhcp6_client_set_duid(client
,
816 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
819 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set DUID: %m");
821 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp6_client_send_options
, i
) {
822 r
= sd_dhcp6_client_add_option(client
, send_option
);
826 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set option: %m");
829 r
= dhcp6_set_hostname(client
, link
);
833 r
= sd_dhcp6_client_set_ifindex(client
, link
->ifindex
);
835 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set ifindex: %m");
837 if (link
->network
->rapid_commit
) {
838 r
= sd_dhcp6_client_set_request_option(client
, SD_DHCP6_OPTION_RAPID_COMMIT
);
840 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set request flag for rapid commit: %m");
843 if (link
->network
->dhcp6_mudurl
) {
844 r
= sd_dhcp6_client_set_request_mud_url(client
, link
->network
->dhcp6_mudurl
);
846 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set MUD URL: %m");
849 SET_FOREACH(request_options
, link
->network
->dhcp6_request_options
, i
) {
850 uint32_t option
= PTR_TO_UINT32(request_options
);
852 r
= sd_dhcp6_client_set_request_option(client
, option
);
854 log_link_debug(link
, "DHCP6 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option
);
858 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set request flag for '%u': %m", option
);
861 if (link
->network
->dhcp6_user_class
) {
862 r
= sd_dhcp6_client_set_request_user_class(client
, link
->network
->dhcp6_user_class
);
864 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set user class: %m");
867 if (link
->network
->dhcp6_vendor_class
) {
868 r
= sd_dhcp6_client_set_request_vendor_class(client
, link
->network
->dhcp6_vendor_class
);
870 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set vendor class: %m");
873 ORDERED_HASHMAP_FOREACH(vendor_option
, link
->network
->dhcp6_client_send_vendor_options
, i
) {
874 r
= sd_dhcp6_client_add_vendor_option(client
, vendor_option
);
878 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set vendor option: %m");
881 r
= sd_dhcp6_client_set_callback(client
, dhcp6_handler
, link
);
883 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set callback: %m");
885 if (dhcp6_enable_prefix_delegation(link
)) {
886 r
= sd_dhcp6_client_set_prefix_delegation(client
, true);
888 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set prefix delegation: %m");
891 if (link
->network
->dhcp6_pd_length
> 0) {
892 r
= sd_dhcp6_client_set_prefix_delegation_hint(client
, link
->network
->dhcp6_pd_length
, &link
->network
->dhcp6_pd_address
);
894 return log_link_error_errno(link
, r
, "DHCP6 CLIENT: Failed to set prefix hint: %m");
897 link
->dhcp6_client
= TAKE_PTR(client
);
902 static Link
*dhcp6_prefix_get(Manager
*m
, struct in6_addr
*addr
) {
903 assert_return(m
, NULL
);
904 assert_return(addr
, NULL
);
906 return hashmap_get(m
->dhcp6_prefixes
, addr
);
909 static int dhcp6_pd_route_handler(sd_netlink
*nl
, sd_netlink_message
*m
, Link
*link
) {
913 assert(link
->dhcp6_pd_route_messages
> 0);
915 link
->dhcp6_pd_route_messages
--;
917 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
920 r
= sd_netlink_message_get_errno(m
);
921 if (r
< 0 && r
!= -EEXIST
) {
922 log_link_message_warning_errno(link
, m
, r
, "Failed to add DHCPv6 Prefix Delegation route");
923 link_enter_failed(link
);
927 if (link
->dhcp6_pd_route_messages
== 0) {
928 log_link_debug(link
, "DHCPv6 prefix delegation routes set");
929 link
->dhcp6_pd_route_configured
= true;
930 link_check_ready(link
);
936 static int dhcp6_prefix_add(Manager
*m
, struct in6_addr
*addr
, Link
*link
) {
937 _cleanup_(route_freep
) Route
*route
= NULL
;
938 _cleanup_free_
struct in6_addr
*a
= NULL
;
939 _cleanup_free_
char *buf
= NULL
;
943 assert_return(m
, -EINVAL
);
944 assert_return(addr
, -EINVAL
);
946 r
= route_new(&route
);
950 route
->family
= AF_INET6
;
951 route
->dst
.in6
= *addr
;
952 route
->dst_prefixlen
= 64;
954 link
->dhcp6_pd_route_configured
= false;
955 link_set_state(link
, LINK_STATE_CONFIGURING
);
957 r
= route_configure(route
, link
, dhcp6_pd_route_handler
);
961 link
->dhcp6_pd_route_messages
++;
963 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) addr
, &buf
);
964 log_link_debug(link
, "Adding prefix route %s/64", strnull(buf
));
966 assigned_link
= hashmap_get(m
->dhcp6_prefixes
, addr
);
968 assert(assigned_link
== link
);
972 a
= newdup(struct in6_addr
, addr
, 1);
976 r
= hashmap_ensure_allocated(&m
->dhcp6_prefixes
, &in6_addr_hash_ops
);
980 r
= hashmap_put(m
->dhcp6_prefixes
, a
, link
);
989 static int dhcp6_prefix_remove_handler(sd_netlink
*nl
, sd_netlink_message
*m
, Link
*link
) {
994 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
997 r
= sd_netlink_message_get_errno(m
);
999 log_link_message_warning_errno(link
, m
, r
, "Received error on DHCPv6 Prefix Delegation route removal");
1000 link_enter_failed(link
);
1007 int dhcp6_prefix_remove(Manager
*m
, struct in6_addr
*addr
) {
1008 _cleanup_free_
struct in6_addr
*a
= NULL
;
1009 _cleanup_(link_unrefp
) Link
*l
= NULL
;
1010 _cleanup_(route_freep
) Route
*route
= NULL
;
1011 _cleanup_free_
char *buf
= NULL
;
1014 assert_return(m
, -EINVAL
);
1015 assert_return(addr
, -EINVAL
);
1017 l
= hashmap_remove2(m
->dhcp6_prefixes
, addr
, (void **) &a
);
1021 (void) sd_radv_remove_prefix(l
->radv
, addr
, 64);
1023 r
= route_new(&route
);
1027 route
->family
= AF_INET6
;
1028 route
->dst
.in6
= *addr
;
1029 route
->dst_prefixlen
= 64;
1031 r
= route_remove(route
, l
, dhcp6_prefix_remove_handler
);
1035 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) addr
, &buf
);
1036 log_link_debug(l
, "Removing prefix route %s/64", strnull(buf
));
1041 static int dhcp6_prefix_remove_all(Manager
*m
, Link
*link
) {
1042 struct in6_addr
*addr
;
1046 assert_return(m
, -EINVAL
);
1047 assert_return(link
, -EINVAL
);
1049 HASHMAP_FOREACH_KEY(l
, addr
, m
->dhcp6_prefixes
, i
)
1051 (void) dhcp6_prefix_remove(m
, addr
);
1056 static int dhcp6_pd_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1060 assert(link
->dhcp6_pd_address_messages
> 0);
1062 link
->dhcp6_pd_address_messages
--;
1064 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1067 r
= sd_netlink_message_get_errno(m
);
1068 if (r
< 0 && r
!= -EEXIST
) {
1069 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv6 delegated prefix address");
1070 link_enter_failed(link
);
1073 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1075 if (link
->dhcp6_pd_address_messages
== 0) {
1076 log_link_debug(link
, "DHCPv6 delegated prefix addresses set");
1077 link
->dhcp6_pd_address_configured
= true;
1078 r
= link_request_set_routes(link
);
1080 link_enter_failed(link
);
1088 static int dhcp6_assign_delegated_prefix(Link
*link
,
1089 const struct in6_addr
*prefix
,
1091 uint32_t lifetime_preferred
,
1092 uint32_t lifetime_valid
) {
1094 _cleanup_(address_freep
) Address
*address
= NULL
;
1098 assert(link
->network
);
1101 if (!link
->network
->dhcp6_pd_assign_prefix
) {
1102 link
->dhcp6_pd_address_configured
= true;
1106 r
= address_new(&address
);
1108 return log_link_error_errno(link
, r
, "Failed to allocate address for DHCPv6 delegated prefix: %m");
1110 address
->in_addr
.in6
= *prefix
;
1112 if (!in_addr_is_null(AF_INET6
, &link
->network
->dhcp6_delegation_prefix_token
))
1113 memcpy(address
->in_addr
.in6
.s6_addr
+ 8, link
->network
->dhcp6_delegation_prefix_token
.in6
.s6_addr
+ 8, 8);
1115 r
= generate_ipv6_eui_64_address(link
, &address
->in_addr
.in6
);
1117 return log_link_warning_errno(link
, r
, "Failed to generate EUI64 address for acquired DHCPv6 delegated prefix: %m");
1120 address
->prefixlen
= prefix_len
;
1121 address
->family
= AF_INET6
;
1122 address
->cinfo
.ifa_prefered
= lifetime_preferred
;
1123 address
->cinfo
.ifa_valid
= lifetime_valid
;
1125 /* address_handler calls link_request_set_routes() and link_request_set_nexthop(). Before they
1126 * are called, the related flags must be cleared. Otherwise, the link becomes configured state
1127 * before routes are configured. */
1128 link
->static_routes_configured
= false;
1129 link
->static_nexthops_configured
= false;
1130 link
->dhcp6_pd_address_configured
= false;
1131 link_set_state(link
, LINK_STATE_CONFIGURING
);
1133 r
= address_configure(address
, link
, dhcp6_pd_address_handler
, true);
1135 return log_link_warning_errno(link
, r
, "Failed to set acquired DHCPv6 delegated prefix address: %m");
1137 link
->dhcp6_pd_address_messages
++;
1142 int config_parse_dhcp6_pd_hint(
1144 const char *filename
,
1146 const char *section
,
1147 unsigned section_line
,
1154 Network
*network
= data
;
1162 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, (union in_addr_union
*) &network
->dhcp6_pd_address
, &network
->dhcp6_pd_length
);
1164 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse PrefixDelegationHint=%s, ignoring assignment", rvalue
);
1168 if (network
->dhcp6_pd_length
< 1 || network
->dhcp6_pd_length
> 128) {
1169 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid prefix length='%d', ignoring assignment", network
->dhcp6_pd_length
);
1170 network
->dhcp6_pd_length
= 0;
1177 int config_parse_dhcp6_mud_url(
1179 const char *filename
,
1181 const char *section
,
1182 unsigned section_line
,
1189 _cleanup_free_
char *unescaped
= NULL
;
1190 Network
*network
= data
;
1197 if (isempty(rvalue
)) {
1198 network
->dhcp6_mudurl
= mfree(network
->dhcp6_mudurl
);
1202 r
= cunescape(rvalue
, 0, &unescaped
);
1204 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1205 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue
);
1209 if (!http_url_is_valid(unescaped
) || strlen(unescaped
) > UINT8_MAX
) {
1210 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1211 "Failed to parse MUD URL '%s', ignoring: %m", rvalue
);
1216 return free_and_replace(network
->dhcp6_mudurl
, unescaped
);
1219 int config_parse_dhcp6_delegated_prefix_token(
1221 const char *filename
,
1223 const char *section
,
1224 unsigned section_line
,
1231 Network
*network
= data
;
1239 if (isempty(rvalue
)) {
1240 network
->dhcp6_delegation_prefix_token
= IN_ADDR_NULL
;
1244 r
= in_addr_from_string(AF_INET6
, rvalue
, &network
->dhcp6_delegation_prefix_token
);
1246 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1247 "Failed to parse DHCPv6 %s, ignoring: %s", lvalue
, rvalue
);
1251 if (in_addr_is_null(AF_INET6
, &network
->dhcp6_delegation_prefix_token
)) {
1252 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1253 "DHCPv6 %s cannot be the ANY address, ignoring: %s", lvalue
, rvalue
);
1260 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp6_client_start_mode
, dhcp6_client_start_mode
, DHCP6ClientStartMode
,
1261 "Failed to parse WithoutRA= setting");
1263 static const char* const dhcp6_client_start_mode_table
[_DHCP6_CLIENT_START_MODE_MAX
] = {
1264 [DHCP6_CLIENT_START_MODE_NO
] = "no",
1265 [DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST
] = "information-request",
1266 [DHCP6_CLIENT_START_MODE_SOLICIT
] = "solicit",
1269 DEFINE_STRING_TABLE_LOOKUP(dhcp6_client_start_mode
, DHCP6ClientStartMode
);