1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <netinet/in.h>
4 #include <netinet/ip.h>
6 #include <linux/if_arp.h>
8 #include "alloc-util.h"
9 #include "dhcp-client-internal.h"
10 #include "hostname-setup.h"
11 #include "hostname-util.h"
12 #include "parse-util.h"
13 #include "network-internal.h"
14 #include "networkd-address.h"
15 #include "networkd-dhcp-prefix-delegation.h"
16 #include "networkd-dhcp4-bus.h"
17 #include "networkd-dhcp4.h"
18 #include "networkd-ipv4acd.h"
19 #include "networkd-link.h"
20 #include "networkd-manager.h"
21 #include "networkd-network.h"
22 #include "networkd-nexthop.h"
23 #include "networkd-ntp.h"
24 #include "networkd-queue.h"
25 #include "networkd-route.h"
26 #include "networkd-setlink.h"
27 #include "networkd-state-file.h"
28 #include "string-table.h"
30 #include "sysctl-util.h"
32 void network_adjust_dhcp4(Network
*network
) {
35 if (!FLAGS_SET(network
->dhcp
, ADDRESS_FAMILY_IPV4
))
38 if (network
->dhcp_use_gateway
< 0)
39 network
->dhcp_use_gateway
= network
->dhcp_use_routes
;
41 /* RFC7844 section 3.: MAY contain the Client Identifier option
42 * Section 3.5: clients MUST use client identifiers based solely on the link-layer address
43 * NOTE: Using MAC, as it does not reveal extra information, and some servers might not answer
44 * if this option is not sent */
45 if (network
->dhcp_anonymize
&&
46 network
->dhcp_client_identifier
>= 0 &&
47 network
->dhcp_client_identifier
!= DHCP_CLIENT_ID_MAC
) {
48 log_warning("%s: ClientIdentifier= is set, although Anonymize=yes. Using ClientIdentifier=mac.",
50 network
->dhcp_client_identifier
= DHCP_CLIENT_ID_MAC
;
53 if (network
->dhcp_client_identifier
< 0)
54 network
->dhcp_client_identifier
= network
->dhcp_anonymize
? DHCP_CLIENT_ID_MAC
: DHCP_CLIENT_ID_DUID
;
56 /* By default, RapidCommit= is enabled when Anonymize=no and neither AllowList= nor DenyList= is specified. */
57 if (network
->dhcp_use_rapid_commit
< 0)
58 network
->dhcp_use_rapid_commit
=
59 !network
->dhcp_anonymize
&&
60 set_isempty(network
->dhcp_allow_listed_ip
) &&
61 set_isempty(network
->dhcp_deny_listed_ip
);
64 static int dhcp4_prefix_covers(
66 const struct in_addr
*in_prefix
,
67 uint8_t in_prefixlen
) {
69 struct in_addr prefix
;
74 assert(link
->dhcp_lease
);
77 /* Return true if the input address or address range is in the assigned network.
78 * E.g. if the DHCP server provides 192.168.0.100/24, then this returns true for the address or
79 * address range in 192.168.0.0/24, and returns false otherwise. */
81 r
= sd_dhcp_lease_get_prefix(link
->dhcp_lease
, &prefix
, &prefixlen
);
85 return in4_addr_prefix_covers_full(&prefix
, prefixlen
, in_prefix
, in_prefixlen
);
88 static int dhcp4_get_router(Link
*link
, struct in_addr
*ret
) {
89 const struct in_addr
*routers
;
93 assert(link
->dhcp_lease
);
96 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &routers
);
100 /* The router option may provide multiple routers, We only use the first non-null address. */
102 FOREACH_ARRAY(router
, routers
, r
) {
103 if (in4_addr_is_null(router
))
113 static int dhcp4_get_classless_static_or_static_routes(Link
*link
, sd_dhcp_route
***ret_routes
, size_t *ret_num
) {
114 _cleanup_free_ sd_dhcp_route
**routes
= NULL
;
118 assert(link
->dhcp_lease
);
120 /* If the DHCP server returns both a Classless Static Routes option and a Static Routes option,
121 * the DHCP client MUST ignore the Static Routes option. */
123 r
= sd_dhcp_lease_get_classless_routes(link
->dhcp_lease
, &routes
);
127 *ret_routes
= TAKE_PTR(routes
);
130 return 1; /* classless */
131 } else if (r
!= -ENODATA
)
134 r
= sd_dhcp_lease_get_static_routes(link
->dhcp_lease
, &routes
);
140 *ret_routes
= TAKE_PTR(routes
);
143 return 0; /* static */
146 static int dhcp4_find_gateway_for_destination(
148 const struct in_addr
*destination
,
149 uint8_t prefixlength
,
151 struct in_addr
*ret
) {
153 _cleanup_free_ sd_dhcp_route
**routes
= NULL
;
155 bool is_classless
, reachable
;
156 uint8_t max_prefixlen
= UINT8_MAX
;
161 assert(link
->dhcp_lease
);
165 /* This tries to find the most suitable gateway for an address or address range.
166 * E.g. if the server provides the default gateway 192.168.0.1 and a classless static route for
167 * 8.0.0.0/8 with gateway 192.168.0.2, then this returns 192.168.0.2 for 8.8.8.8/32, and 192.168.0.1
168 * for 9.9.9.9/32. If 'allow_null' flag is set, and the input address or address range is in the
169 * assigned network, then the default gateway will be ignored and the null address will be returned
170 * unless a matching non-default gateway found. */
172 r
= dhcp4_prefix_covers(link
, destination
, prefixlength
);
177 r
= dhcp4_get_classless_static_or_static_routes(link
, &routes
, &n_routes
);
178 if (r
< 0 && r
!= -ENODATA
)
180 is_classless
= r
> 0;
182 /* First, find most suitable gateway. */
183 FOREACH_ARRAY(e
, routes
, n_routes
) {
187 r
= sd_dhcp_route_get_destination(*e
, &dst
);
191 r
= sd_dhcp_route_get_destination_prefix_length(*e
, &len
);
195 r
= in4_addr_prefix_covers_full(&dst
, len
, destination
, prefixlength
);
201 if (max_prefixlen
!= UINT8_MAX
&& max_prefixlen
> len
)
204 r
= sd_dhcp_route_get_gateway(*e
, &gw
);
211 /* Found a suitable gateway in classless static routes or static routes. */
212 if (max_prefixlen
!= UINT8_MAX
) {
213 if (max_prefixlen
== 0 && reachable
&& allow_null
)
214 /* Do not return the default gateway, if the destination is in the assigned network. */
215 *ret
= (struct in_addr
) {};
221 /* When the destination is in the assigned network, return the null address if allowed. */
222 if (reachable
&& allow_null
) {
223 *ret
= (struct in_addr
) {};
227 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
228 * a Router option, the DHCP client MUST ignore the Router option. */
230 r
= dhcp4_get_router(link
, ret
);
238 return -EHOSTUNREACH
; /* Not in the same network, cannot reach the destination. */
241 return -ENODATA
; /* No matching gateway found. */
244 static int dhcp4_remove_address_and_routes(Link
*link
, bool only_marked
) {
250 assert(link
->manager
);
252 SET_FOREACH(route
, link
->manager
->routes
) {
253 if (route
->source
!= NETWORK_CONFIG_SOURCE_DHCP4
)
255 if (route
->nexthop
.ifindex
!= 0 && route
->nexthop
.ifindex
!= link
->ifindex
)
257 if (only_marked
&& !route_is_marked(route
))
260 RET_GATHER(ret
, route_remove_and_cancel(route
, link
->manager
));
263 SET_FOREACH(address
, link
->addresses
) {
264 if (address
->source
!= NETWORK_CONFIG_SOURCE_DHCP4
)
266 if (only_marked
&& !address_is_marked(address
))
269 RET_GATHER(ret
, address_remove_and_cancel(address
, link
));
275 static int dhcp4_address_get(Link
*link
, Address
**ret
) {
280 SET_FOREACH(address
, link
->addresses
) {
281 if (address
->source
!= NETWORK_CONFIG_SOURCE_DHCP4
)
283 if (address_is_marked(address
))
294 static int dhcp4_address_ready_callback(Address
*address
) {
296 assert(address
->link
);
298 /* Do not call this again. */
299 address
->callback
= NULL
;
301 return dhcp4_check_ready(address
->link
);
304 int dhcp4_check_ready(Link
*link
) {
310 if (link
->dhcp4_messages
> 0) {
311 log_link_debug(link
, "%s(): DHCPv4 address and routes are not set.", __func__
);
315 if (dhcp4_address_get(link
, &address
) < 0) {
316 log_link_debug(link
, "%s(): DHCPv4 address is not set.", __func__
);
320 if (!address_is_ready(address
)) {
321 log_link_debug(link
, "%s(): DHCPv4 address is not ready.", __func__
);
322 address
->callback
= dhcp4_address_ready_callback
;
326 link
->dhcp4_configured
= true;
327 log_link_debug(link
, "DHCPv4 address and routes set.");
329 /* New address and routes are configured now. Let's release old lease. */
330 r
= dhcp4_remove_address_and_routes(link
, /* only_marked = */ true);
334 r
= sd_ipv4ll_stop(link
->ipv4ll
);
336 return log_link_warning_errno(link
, r
, "Failed to drop IPv4 link-local address: %m");
338 link_check_ready(link
);
342 static int dhcp4_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, Route
*route
) {
348 r
= route_configure_handler_internal(rtnl
, m
, link
, route
, "Could not set DHCPv4 route");
352 r
= dhcp4_check_ready(link
);
354 link_enter_failed(link
);
359 static int dhcp4_request_route(Route
*route
, Link
*link
) {
360 struct in_addr server
;
366 assert(link
->manager
);
367 assert(link
->network
);
368 assert(link
->dhcp_lease
);
370 r
= sd_dhcp_lease_get_server_identifier(link
->dhcp_lease
, &server
);
372 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
374 route
->source
= NETWORK_CONFIG_SOURCE_DHCP4
;
375 route
->provider
.in
= server
;
376 route
->family
= AF_INET
;
377 if (!route
->protocol_set
)
378 route
->protocol
= RTPROT_DHCP
;
379 if (!route
->priority_set
)
380 route
->priority
= link
->network
->dhcp_route_metric
;
381 if (!route
->table_set
)
382 route
->table
= link_get_dhcp4_route_table(link
);
383 r
= route_metric_set(&route
->metric
, RTAX_MTU
, link
->network
->dhcp_route_mtu
);
386 r
= route_metric_set(&route
->metric
, RTAX_INITCWND
, link
->network
->dhcp_initial_congestion_window
);
389 r
= route_metric_set(&route
->metric
, RTAX_INITRWND
, link
->network
->dhcp_advertised_receive_window
);
392 r
= route_metric_set(&route
->metric
, RTAX_QUICKACK
, link
->network
->dhcp_quickack
);
396 r
= route_adjust_nexthops(route
, link
);
400 if (route_get(link
->manager
, route
, &existing
) < 0) /* This is a new route. */
401 link
->dhcp4_configured
= false;
403 route_unmark(existing
);
405 return link_request_route(link
, route
, &link
->dhcp4_messages
, dhcp4_route_handler
);
408 static bool link_prefixroute(Link
*link
) {
409 return !link
->network
->dhcp_route_table_set
||
410 link
->network
->dhcp_route_table
== RT_TABLE_MAIN
;
413 static int dhcp4_request_prefix_route(Link
*link
) {
414 _cleanup_(route_unrefp
) Route
*route
= NULL
;
418 assert(link
->dhcp_lease
);
420 if (link_prefixroute(link
))
421 /* When true, the route will be created by kernel. See dhcp4_update_address(). */
424 r
= route_new(&route
);
428 route
->scope
= RT_SCOPE_LINK
;
430 r
= sd_dhcp_lease_get_prefix(link
->dhcp_lease
, &route
->dst
.in
, &route
->dst_prefixlen
);
434 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &route
->prefsrc
.in
);
438 return dhcp4_request_route(route
, link
);
441 static int dhcp4_request_route_to_gateway(Link
*link
, const struct in_addr
*gw
) {
442 _cleanup_(route_unrefp
) Route
*route
= NULL
;
443 struct in_addr address
;
447 assert(link
->dhcp_lease
);
450 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
454 r
= route_new(&route
);
459 route
->dst_prefixlen
= 32;
460 route
->prefsrc
.in
= address
;
461 route
->scope
= RT_SCOPE_LINK
;
463 return dhcp4_request_route(route
, link
);
466 static int dhcp4_request_route_auto(
469 const struct in_addr
*gw
) {
471 struct in_addr address
;
476 assert(link
->dhcp_lease
);
479 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
483 if (in4_addr_is_localhost(&route
->dst
.in
)) {
484 if (in4_addr_is_set(gw
))
485 log_link_debug(link
, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR
"/%u is localhost, "
486 "ignoring gateway address "IPV4_ADDRESS_FMT_STR
,
487 IPV4_ADDRESS_FMT_VAL(route
->dst
.in
), route
->dst_prefixlen
, IPV4_ADDRESS_FMT_VAL(*gw
));
489 route
->scope
= RT_SCOPE_HOST
;
490 route
->nexthop
.family
= AF_UNSPEC
;
491 route
->nexthop
.gw
= IN_ADDR_NULL
;
492 route
->prefsrc
= IN_ADDR_NULL
;
494 } else if (in4_addr_equal(&route
->dst
.in
, &address
)) {
495 if (in4_addr_is_set(gw
))
496 log_link_debug(link
, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR
"/%u is equivalent to the acquired address, "
497 "ignoring gateway address "IPV4_ADDRESS_FMT_STR
,
498 IPV4_ADDRESS_FMT_VAL(route
->dst
.in
), route
->dst_prefixlen
, IPV4_ADDRESS_FMT_VAL(*gw
));
500 route
->scope
= RT_SCOPE_HOST
;
501 route
->nexthop
.family
= AF_UNSPEC
;
502 route
->nexthop
.gw
= IN_ADDR_NULL
;
503 route
->prefsrc
.in
= address
;
505 } else if (in4_addr_is_null(gw
)) {
506 r
= dhcp4_prefix_covers(link
, &route
->dst
.in
, route
->dst_prefixlen
);
509 if (r
== 0 && DEBUG_LOGGING
) {
510 struct in_addr prefix
;
513 r
= sd_dhcp_lease_get_prefix(link
->dhcp_lease
, &prefix
, &prefixlen
);
517 log_link_debug(link
, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR
"/%u is not in the assigned network "
518 IPV4_ADDRESS_FMT_STR
"/%u, but no gateway is specified, using 'link' scope.",
519 IPV4_ADDRESS_FMT_VAL(route
->dst
.in
), route
->dst_prefixlen
,
520 IPV4_ADDRESS_FMT_VAL(prefix
), prefixlen
);
523 route
->scope
= RT_SCOPE_LINK
;
524 route
->nexthop
.family
= AF_UNSPEC
;
525 route
->nexthop
.gw
= IN_ADDR_NULL
;
526 route
->prefsrc
.in
= address
;
529 r
= dhcp4_request_route_to_gateway(link
, gw
);
533 route
->scope
= RT_SCOPE_UNIVERSE
;
534 route
->nexthop
.family
= AF_INET
;
535 route
->nexthop
.gw
.in
= *gw
;
536 route
->prefsrc
.in
= address
;
539 return dhcp4_request_route(route
, link
);
542 static int dhcp4_request_classless_static_or_static_routes(Link
*link
) {
543 _cleanup_free_ sd_dhcp_route
**routes
= NULL
;
548 assert(link
->dhcp_lease
);
550 if (!link
->network
->dhcp_use_routes
)
553 r
= dhcp4_get_classless_static_or_static_routes(link
, &routes
, &n_routes
);
559 FOREACH_ARRAY(e
, routes
, n_routes
) {
560 _cleanup_(route_unrefp
) Route
*route
= NULL
;
563 r
= route_new(&route
);
567 r
= sd_dhcp_route_get_gateway(*e
, &gw
);
571 r
= sd_dhcp_route_get_destination(*e
, &route
->dst
.in
);
575 r
= sd_dhcp_route_get_destination_prefix_length(*e
, &route
->dst_prefixlen
);
579 r
= dhcp4_request_route_auto(route
, link
, &gw
);
587 static int dhcp4_request_default_gateway(Link
*link
) {
588 _cleanup_(route_unrefp
) Route
*route
= NULL
;
589 struct in_addr address
, router
;
593 assert(link
->dhcp_lease
);
595 if (!link
->network
->dhcp_use_gateway
)
598 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
599 * a Router option, the DHCP client MUST ignore the Router option. */
600 if (link
->network
->dhcp_use_routes
&&
601 dhcp4_get_classless_static_or_static_routes(link
, NULL
, NULL
) > 0)
604 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
608 r
= dhcp4_get_router(link
, &router
);
610 log_link_debug(link
, "DHCP: No valid router address received from DHCP server.");
616 /* The dhcp netmask may mask out the gateway. First, add an explicit route for the gateway host
617 * so that we can route no matter the netmask or existing kernel route tables. */
618 r
= dhcp4_request_route_to_gateway(link
, &router
);
622 r
= route_new(&route
);
626 /* Next, add a default gateway. */
627 route
->nexthop
.family
= AF_INET
;
628 route
->nexthop
.gw
.in
= router
;
629 route
->prefsrc
.in
= address
;
631 return dhcp4_request_route(route
, link
);
634 static int dhcp4_request_semi_static_routes(Link
*link
) {
639 assert(link
->dhcp_lease
);
640 assert(link
->network
);
642 HASHMAP_FOREACH(rt
, link
->network
->routes_by_section
) {
643 _cleanup_(route_unrefp
) Route
*route
= NULL
;
646 if (!rt
->gateway_from_dhcp_or_ra
)
649 if (rt
->nexthop
.family
!= AF_INET
)
652 assert(rt
->family
== AF_INET
);
654 r
= dhcp4_find_gateway_for_destination(link
, &rt
->dst
.in
, rt
->dst_prefixlen
, /* allow_null = */ false, &gw
);
655 if (IN_SET(r
, -EHOSTUNREACH
, -ENODATA
)) {
656 log_link_debug_errno(link
, r
, "DHCP: Cannot find suitable gateway for destination %s of semi-static route, ignoring: %m",
657 IN4_ADDR_PREFIX_TO_STRING(&rt
->dst
.in
, rt
->dst_prefixlen
));
663 r
= dhcp4_request_route_to_gateway(link
, &gw
);
667 r
= route_dup(rt
, NULL
, &route
);
671 route
->nexthop
.gw
.in
= gw
;
673 r
= dhcp4_request_route(route
, link
);
681 static int dhcp4_request_routes_to_servers(
683 const struct in_addr
*servers
,
689 assert(link
->dhcp_lease
);
690 assert(link
->network
);
691 assert(servers
|| n_servers
== 0);
693 FOREACH_ARRAY(dst
, servers
, n_servers
) {
694 _cleanup_(route_unrefp
) Route
*route
= NULL
;
697 if (in4_addr_is_null(dst
))
700 r
= dhcp4_find_gateway_for_destination(link
, dst
, 32, /* allow_null = */ true, &gw
);
701 if (r
== -EHOSTUNREACH
) {
702 log_link_debug_errno(link
, r
, "DHCP: Cannot find suitable gateway for destination %s, ignoring: %m",
703 IN4_ADDR_PREFIX_TO_STRING(dst
, 32));
709 r
= route_new(&route
);
713 route
->dst
.in
= *dst
;
714 route
->dst_prefixlen
= 32;
716 r
= dhcp4_request_route_auto(route
, link
, &gw
);
724 static int dhcp4_request_routes_to_dns(Link
*link
) {
725 const struct in_addr
*dns
;
729 assert(link
->dhcp_lease
);
730 assert(link
->network
);
732 if (!link_get_use_dns(link
, NETWORK_CONFIG_SOURCE_DHCP4
) ||
733 !link
->network
->dhcp_routes_to_dns
)
736 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &dns
);
737 if (IN_SET(r
, 0, -ENODATA
))
742 return dhcp4_request_routes_to_servers(link
, dns
, r
);
745 static int dhcp4_request_routes_to_ntp(Link
*link
) {
746 const struct in_addr
*ntp
;
750 assert(link
->dhcp_lease
);
751 assert(link
->network
);
753 if (!link_get_use_ntp(link
, NETWORK_CONFIG_SOURCE_DHCP4
) ||
754 !link
->network
->dhcp_routes_to_ntp
)
757 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &ntp
);
758 if (IN_SET(r
, 0, -ENODATA
))
763 return dhcp4_request_routes_to_servers(link
, ntp
, r
);
766 static int dhcp4_request_routes(Link
*link
) {
770 assert(link
->dhcp_lease
);
772 r
= dhcp4_request_prefix_route(link
);
774 return log_link_error_errno(link
, r
, "DHCP error: Could not request prefix route: %m");
776 r
= dhcp4_request_default_gateway(link
);
778 return log_link_error_errno(link
, r
, "DHCP error: Could not request default gateway: %m");
780 r
= dhcp4_request_classless_static_or_static_routes(link
);
782 return log_link_error_errno(link
, r
, "DHCP error: Could not request static routes: %m");
784 r
= dhcp4_request_semi_static_routes(link
);
786 return log_link_error_errno(link
, r
, "DHCP error: Could not request routes with Gateway=_dhcp4 setting: %m");
788 r
= dhcp4_request_routes_to_dns(link
);
790 return log_link_error_errno(link
, r
, "DHCP error: Could not request routes to DNS servers: %m");
792 r
= dhcp4_request_routes_to_ntp(link
);
794 return log_link_error_errno(link
, r
, "DHCP error: Could not request routes to NTP servers: %m");
799 static int dhcp_reset_mtu(Link
*link
) {
804 if (!link
->network
->dhcp_use_mtu
)
807 r
= link_request_to_set_mtu(link
, link
->original_mtu
);
809 return log_link_error_errno(link
, r
, "DHCP error: Could not queue request to reset MTU: %m");
814 static int dhcp_reset_hostname(Link
*link
) {
815 const char *hostname
;
820 if (!link
->network
->dhcp_use_hostname
)
823 hostname
= link
->network
->dhcp_hostname
;
825 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
830 /* If a hostname was set due to the lease, then unset it now. */
831 r
= manager_set_hostname(link
->manager
, NULL
);
833 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
838 int dhcp4_lease_lost(Link
*link
) {
842 assert(link
->dhcp_lease
);
843 assert(link
->network
);
845 log_link_info(link
, "DHCP lease lost");
847 link
->dhcp4_configured
= false;
849 if (link
->network
->dhcp_use_6rd
&&
850 sd_dhcp_lease_has_6rd(link
->dhcp_lease
))
851 dhcp4_pd_prefix_lost(link
);
853 RET_GATHER(r
, dhcp4_remove_address_and_routes(link
, /* only_marked = */ false));
854 RET_GATHER(r
, dhcp_reset_mtu(link
));
855 RET_GATHER(r
, dhcp_reset_hostname(link
));
857 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
860 /* If one of the above failed. Do not request nexthops and routes. */
864 r
= link_request_static_nexthops(link
, true);
868 return link_request_static_routes(link
, true);
871 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, Address
*address
) {
876 r
= address_configure_handler_internal(rtnl
, m
, link
, "Could not set DHCPv4 address");
880 r
= dhcp4_check_ready(link
);
882 link_enter_failed(link
);
887 static int dhcp4_request_address(Link
*link
, bool announce
) {
888 _cleanup_(address_unrefp
) Address
*addr
= NULL
;
889 struct in_addr address
, server
;
892 usec_t lifetime_usec
;
896 assert(link
->manager
);
897 assert(link
->network
);
898 assert(link
->dhcp_lease
);
900 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
902 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
904 r
= sd_dhcp_lease_get_prefix(link
->dhcp_lease
, NULL
, &prefixlen
);
906 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
908 r
= sd_dhcp_lease_get_server_identifier(link
->dhcp_lease
, &server
);
910 return log_link_debug_errno(link
, r
, "DHCP error: failed to get DHCP server IP address: %m");
912 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
913 r
= sd_dhcp_lease_get_lifetime_timestamp(link
->dhcp_lease
, CLOCK_BOOTTIME
, &lifetime_usec
);
915 return log_link_warning_errno(link
, r
, "DHCP error: failed to get lifetime: %m");
917 lifetime_usec
= USEC_INFINITY
;
920 const struct in_addr
*router
;
922 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
923 if (r
< 0 && r
!= -ENODATA
)
924 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
926 if (r
> 0 && in4_addr_is_set(&router
[0]))
928 LOG_LINK_INTERFACE(link
),
929 LOG_LINK_MESSAGE(link
, "DHCPv4 address "IPV4_ADDRESS_FMT_STR
"/%u, gateway "IPV4_ADDRESS_FMT_STR
" acquired from "IPV4_ADDRESS_FMT_STR
,
930 IPV4_ADDRESS_FMT_VAL(address
),
932 IPV4_ADDRESS_FMT_VAL(router
[0]),
933 IPV4_ADDRESS_FMT_VAL(server
)),
934 "ADDRESS="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(address
),
935 "PREFIXLEN=%u", prefixlen
,
936 "GATEWAY="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(router
[0]));
939 LOG_LINK_INTERFACE(link
),
940 LOG_LINK_MESSAGE(link
, "DHCPv4 address "IPV4_ADDRESS_FMT_STR
"/%u acquired from "IPV4_ADDRESS_FMT_STR
,
941 IPV4_ADDRESS_FMT_VAL(address
),
943 IPV4_ADDRESS_FMT_VAL(server
)),
944 "ADDRESS="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(address
),
945 "PREFIXLEN=%u", prefixlen
);
948 r
= address_new(&addr
);
952 addr
->source
= NETWORK_CONFIG_SOURCE_DHCP4
;
953 addr
->provider
.in
= server
;
954 addr
->family
= AF_INET
;
955 addr
->in_addr
.in
.s_addr
= address
.s_addr
;
956 addr
->lifetime_preferred_usec
= lifetime_usec
;
957 addr
->lifetime_valid_usec
= lifetime_usec
;
958 addr
->prefixlen
= prefixlen
;
959 r
= sd_dhcp_lease_get_broadcast(link
->dhcp_lease
, &addr
->broadcast
);
960 if (r
< 0 && r
!= -ENODATA
)
961 return log_link_warning_errno(link
, r
, "DHCP: failed to get broadcast address: %m");
962 SET_FLAG(addr
->flags
, IFA_F_NOPREFIXROUTE
, !link_prefixroute(link
));
963 addr
->route_metric
= link
->network
->dhcp_route_metric
;
964 addr
->duplicate_address_detection
= link
->network
->dhcp_send_decline
? ADDRESS_FAMILY_IPV4
: ADDRESS_FAMILY_NO
;
966 r
= free_and_strdup_warn(&addr
->label
, link
->network
->dhcp_label
);
970 r
= free_and_strdup_warn(&addr
->netlabel
, link
->network
->dhcp_netlabel
);
974 if (address_get(link
, addr
, &existing
) < 0) /* The address is new. */
975 link
->dhcp4_configured
= false;
977 address_unmark(existing
);
979 r
= link_request_address(link
, addr
, &link
->dhcp4_messages
,
980 dhcp4_address_handler
, NULL
);
982 return log_link_error_errno(link
, r
, "Failed to request DHCPv4 address: %m");
987 static int dhcp4_request_address_and_routes(Link
*link
, bool announce
) {
992 link_mark_addresses(link
, NETWORK_CONFIG_SOURCE_DHCP4
);
993 manager_mark_routes(link
->manager
, link
, NETWORK_CONFIG_SOURCE_DHCP4
);
995 r
= dhcp4_request_address(link
, announce
);
999 r
= dhcp4_request_routes(link
);
1003 if (!link
->dhcp4_configured
) {
1004 link_set_state(link
, LINK_STATE_CONFIGURING
);
1005 link_check_ready(link
);
1011 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
1012 _cleanup_(sd_dhcp_lease_unrefp
) sd_dhcp_lease
*old_lease
= NULL
;
1013 sd_dhcp_lease
*lease
;
1017 assert(link
->network
);
1020 r
= sd_dhcp_client_get_lease(client
, &lease
);
1022 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
1024 old_lease
= TAKE_PTR(link
->dhcp_lease
);
1025 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
1028 if (link
->network
->dhcp_use_6rd
) {
1029 if (sd_dhcp_lease_has_6rd(link
->dhcp_lease
)) {
1030 r
= dhcp4_pd_prefix_acquired(link
);
1032 return log_link_warning_errno(link
, r
, "Failed to process 6rd option: %m");
1033 } else if (sd_dhcp_lease_has_6rd(old_lease
))
1034 dhcp4_pd_prefix_lost(link
);
1037 return dhcp4_request_address_and_routes(link
, false);
1040 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
1041 sd_dhcp_lease
*lease
;
1047 r
= sd_dhcp_client_get_lease(client
, &lease
);
1049 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
1051 sd_dhcp_lease_unref(link
->dhcp_lease
);
1052 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
1055 if (link
->network
->dhcp_use_mtu
) {
1058 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
1060 r
= link_request_to_set_mtu(link
, mtu
);
1062 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
1066 if (link
->network
->dhcp_use_hostname
) {
1067 const char *dhcpname
= NULL
;
1068 _cleanup_free_
char *hostname
= NULL
;
1070 if (link
->network
->dhcp_hostname
)
1071 dhcpname
= link
->network
->dhcp_hostname
;
1073 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
1076 r
= shorten_overlong(dhcpname
, &hostname
);
1078 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
1080 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
1084 r
= manager_set_hostname(link
->manager
, hostname
);
1086 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
1090 if (link
->network
->dhcp_use_timezone
) {
1091 const char *tz
= NULL
;
1093 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
1096 r
= manager_set_timezone(link
->manager
, tz
);
1098 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
1102 if (link
->network
->dhcp_use_6rd
&&
1103 sd_dhcp_lease_has_6rd(link
->dhcp_lease
)) {
1104 r
= dhcp4_pd_prefix_acquired(link
);
1106 return log_link_warning_errno(link
, r
, "Failed to process 6rd option: %m");
1109 return dhcp4_request_address_and_routes(link
, true);
1112 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
1115 r
= dhcp_lease_acquired(client
, link
);
1117 (void) dhcp4_lease_lost(link
);
1122 static int dhcp_server_is_filtered(Link
*link
, sd_dhcp_client
*client
) {
1123 sd_dhcp_lease
*lease
;
1124 struct in_addr addr
;
1128 assert(link
->network
);
1131 r
= sd_dhcp_client_get_lease(client
, &lease
);
1133 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1135 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1137 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1139 if (in4_address_is_filtered(&addr
, link
->network
->dhcp_allow_listed_ip
, link
->network
->dhcp_deny_listed_ip
)) {
1140 if (DEBUG_LOGGING
) {
1141 if (link
->network
->dhcp_allow_listed_ip
)
1142 log_link_debug(link
, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR
" not found in allow-list, ignoring offer.",
1143 IPV4_ADDRESS_FMT_VAL(addr
));
1145 log_link_debug(link
, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR
" found in deny-list, ignoring offer.",
1146 IPV4_ADDRESS_FMT_VAL(addr
));
1155 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
1156 Link
*link
= ASSERT_PTR(userdata
);
1159 assert(link
->network
);
1160 assert(link
->manager
);
1162 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1166 case SD_DHCP_CLIENT_EVENT_STOP
:
1168 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
1170 if (in4_addr_is_set(&link
->network
->ipv4ll_start_address
)) {
1171 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &link
->network
->ipv4ll_start_address
);
1173 return log_link_warning_errno(link
, r
, "Could not set IPv4 link-local start address: %m");
1176 r
= sd_ipv4ll_start(link
->ipv4ll
);
1177 if (r
< 0 && r
!= -ESTALE
) /* On exit, we cannot and should not start sd-ipv4ll. */
1178 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1181 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1182 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1186 if (link
->dhcp_lease
) {
1187 if (link
->network
->dhcp_send_release
) {
1188 r
= sd_dhcp_client_send_release(client
);
1190 log_link_full_errno(link
,
1191 ERRNO_IS_DISCONNECT(r
) ? LOG_DEBUG
: LOG_WARNING
,
1192 r
, "Failed to send DHCP RELEASE, ignoring: %m");
1195 r
= dhcp4_lease_lost(link
);
1197 link_enter_failed(link
);
1203 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
1204 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1205 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1209 if (link
->dhcp_lease
) {
1210 r
= dhcp4_lease_lost(link
);
1212 link_enter_failed(link
);
1218 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
1219 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1220 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1224 r
= dhcp_lease_ip_change(client
, link
);
1226 link_enter_failed(link
);
1231 case SD_DHCP_CLIENT_EVENT_RENEW
:
1232 r
= dhcp_lease_renew(client
, link
);
1234 link_enter_failed(link
);
1238 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1239 r
= dhcp_lease_acquired(client
, link
);
1241 link_enter_failed(link
);
1245 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1246 r
= dhcp_server_is_filtered(link
, client
);
1248 link_enter_failed(link
);
1255 case SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE
:
1256 if (link
->ipv4ll
&& !sd_ipv4ll_is_running(link
->ipv4ll
)) {
1257 log_link_debug(link
, "Problems acquiring DHCP lease, acquiring IPv4 link-local address");
1259 if (in4_addr_is_set(&link
->network
->ipv4ll_start_address
)) {
1260 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &link
->network
->ipv4ll_start_address
);
1262 return log_link_warning_errno(link
, r
, "Could not set IPv4 link-local start address: %m");
1265 r
= sd_ipv4ll_start(link
->ipv4ll
);
1267 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1273 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1275 log_link_warning(link
, "DHCP unknown event: %i", event
);
1282 static int dhcp4_set_hostname(Link
*link
) {
1283 _cleanup_free_
char *hostname
= NULL
;
1289 if (!link
->network
->dhcp_send_hostname
)
1291 else if (link
->network
->dhcp_hostname
)
1292 hn
= link
->network
->dhcp_hostname
;
1294 r
= gethostname_strict(&hostname
);
1295 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1296 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to get hostname: %m");
1301 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1302 if (r
== -EINVAL
&& hostname
)
1303 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1304 log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1306 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set hostname: %m");
1311 static int dhcp4_set_client_identifier(Link
*link
) {
1315 assert(link
->network
);
1316 assert(link
->dhcp_client
);
1318 switch (link
->network
->dhcp_client_identifier
) {
1319 case DHCP_CLIENT_ID_DUID
: {
1320 /* If configured, apply user specified DUID and IAID */
1321 const DUID
*duid
= link_get_dhcp4_duid(link
);
1323 if (duid
->raw_data_len
== 0)
1324 switch (duid
->type
) {
1326 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1327 link
->network
->dhcp_iaid_set
,
1328 link
->network
->dhcp_iaid
,
1332 r
= sd_dhcp_client_set_iaid_duid_ll(link
->dhcp_client
,
1333 link
->network
->dhcp_iaid_set
,
1334 link
->network
->dhcp_iaid
);
1337 r
= sd_dhcp_client_set_iaid_duid_en(link
->dhcp_client
,
1338 link
->network
->dhcp_iaid_set
,
1339 link
->network
->dhcp_iaid
);
1341 case DUID_TYPE_UUID
:
1342 r
= sd_dhcp_client_set_iaid_duid_uuid(link
->dhcp_client
,
1343 link
->network
->dhcp_iaid_set
,
1344 link
->network
->dhcp_iaid
);
1347 r
= sd_dhcp_client_set_iaid_duid_raw(link
->dhcp_client
,
1348 link
->network
->dhcp_iaid_set
,
1349 link
->network
->dhcp_iaid
,
1350 duid
->type
, NULL
, 0);
1353 r
= sd_dhcp_client_set_iaid_duid_raw(link
->dhcp_client
,
1354 link
->network
->dhcp_iaid_set
,
1355 link
->network
->dhcp_iaid
,
1356 duid
->type
, duid
->raw_data
, duid
->raw_data_len
);
1361 case DHCP_CLIENT_ID_MAC
: {
1362 const uint8_t *hw_addr
= link
->hw_addr
.bytes
;
1363 size_t hw_addr_len
= link
->hw_addr
.length
;
1365 if (link
->iftype
== ARPHRD_INFINIBAND
&& hw_addr_len
== INFINIBAND_ALEN
) {
1366 /* set_client_id expects only last 8 bytes of an IB address */
1367 hw_addr
+= INFINIBAND_ALEN
- 8;
1368 hw_addr_len
-= INFINIBAND_ALEN
- 8;
1371 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1376 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set client ID: %m");
1380 assert_not_reached();
1386 static int dhcp4_find_dynamic_address(Link
*link
, struct in_addr
*ret
) {
1390 assert(link
->network
);
1393 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
1396 SET_FOREACH(a
, link
->addresses
) {
1397 if (a
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
)
1399 if (a
->family
!= AF_INET
)
1401 if (link_address_is_dynamic(link
, a
))
1408 *ret
= a
->in_addr
.in
;
1412 static int dhcp4_set_request_address(Link
*link
) {
1416 assert(link
->network
);
1417 assert(link
->dhcp_client
);
1419 a
= link
->network
->dhcp_request_address
;
1421 if (in4_addr_is_null(&a
))
1422 (void) dhcp4_find_dynamic_address(link
, &a
);
1424 if (in4_addr_is_null(&a
))
1427 log_link_debug(link
, "DHCPv4 CLIENT: requesting %s.", IN4_ADDR_TO_STRING(&a
));
1428 return sd_dhcp_client_set_request_address(link
->dhcp_client
, &a
);
1431 static bool link_needs_dhcp_broadcast(Link
*link
) {
1436 assert(link
->network
);
1438 /* Return the setting in DHCP[4].RequestBroadcast if specified. Otherwise return the device property
1439 * ID_NET_DHCP_BROADCAST setting, which may be set for interfaces requiring that the DHCPOFFER message
1440 * is being broadcast because they can't handle unicast messages while not fully configured.
1441 * If neither is set or a failure occurs, return false, which is the default for this flag.
1443 r
= link
->network
->dhcp_broadcast
;
1444 if (r
< 0 && link
->dev
&& sd_device_get_property_value(link
->dev
, "ID_NET_DHCP_BROADCAST", &val
) >= 0) {
1445 r
= parse_boolean(val
);
1447 log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to parse ID_NET_DHCP_BROADCAST, ignoring: %m");
1449 log_link_debug(link
, "DHCPv4 CLIENT: Detected ID_NET_DHCP_BROADCAST='%d'.", r
);
1455 static bool link_dhcp4_ipv6_only_mode(Link
*link
) {
1457 assert(link
->network
);
1459 /* If it is explicitly specified, then honor the setting. */
1460 if (link
->network
->dhcp_ipv6_only_mode
>= 0)
1461 return link
->network
->dhcp_ipv6_only_mode
;
1463 /* Defaults to false, until we support 464XLAT. See issue #30891. */
1467 static int dhcp4_configure(Link
*link
) {
1468 sd_dhcp_option
*send_option
;
1469 void *request_options
;
1473 assert(link
->network
);
1475 if (link
->dhcp_client
)
1476 return log_link_debug_errno(link
, SYNTHETIC_ERRNO(EBUSY
), "DHCPv4 client is already configured.");
1478 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1480 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to allocate DHCPv4 client: %m");
1482 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, link
->manager
->event
, 0);
1484 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to attach event to DHCPv4 client: %m");
1486 r
= sd_dhcp_client_attach_device(link
->dhcp_client
, link
->dev
);
1488 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to attach device: %m");
1490 r
= sd_dhcp_client_set_rapid_commit(link
->dhcp_client
, link
->network
->dhcp_use_rapid_commit
);
1492 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set rapid commit: %m");
1494 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1495 link
->hw_addr
.bytes
,
1496 link
->bcast_addr
.length
> 0 ? link
->bcast_addr
.bytes
: NULL
,
1497 link
->hw_addr
.length
, link
->iftype
);
1499 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set MAC address: %m");
1501 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1503 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set ifindex: %m");
1505 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1507 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set callback: %m");
1509 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
, link_needs_dhcp_broadcast(link
));
1511 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for broadcast: %m");
1513 r
= dhcp_client_set_state_callback(link
->dhcp_client
, dhcp_client_callback_bus
, link
);
1515 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set state change callback: %m");
1517 if (link
->mtu
> 0) {
1518 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1520 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set MTU: %m");
1523 if (!link
->network
->dhcp_anonymize
) {
1524 r
= dhcp4_set_request_address(link
);
1526 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set initial DHCPv4 address: %m");
1528 if (link
->network
->dhcp_use_mtu
) {
1529 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_MTU_INTERFACE
);
1531 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for MTU: %m");
1534 if (link
->network
->dhcp_use_routes
) {
1535 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_STATIC_ROUTE
);
1537 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for static route: %m");
1539 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1541 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for classless static route: %m");
1544 if (link_get_use_domains(link
, NETWORK_CONFIG_SOURCE_DHCP4
) > 0) {
1545 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH
);
1547 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for domain search list: %m");
1550 if (link_get_use_ntp(link
, NETWORK_CONFIG_SOURCE_DHCP4
)) {
1551 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1553 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for NTP server: %m");
1556 if (link
->network
->dhcp_use_sip
) {
1557 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_SIP_SERVER
);
1559 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for SIP server: %m");
1561 if (link
->network
->dhcp_use_captive_portal
) {
1562 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DHCP_CAPTIVE_PORTAL
);
1564 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for captive portal: %m");
1567 if (link
->network
->dhcp_use_timezone
) {
1568 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_TZDB_TIMEZONE
);
1570 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for timezone: %m");
1573 if (link
->network
->dhcp_use_6rd
) {
1574 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_6RD
);
1576 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for 6rd: %m");
1579 if (link_dhcp4_ipv6_only_mode(link
)) {
1580 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED
);
1582 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for IPv6-only preferred option: %m");
1585 SET_FOREACH(request_options
, link
->network
->dhcp_request_options
) {
1586 uint32_t option
= PTR_TO_UINT32(request_options
);
1588 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, option
);
1590 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set request flag for '%u': %m", option
);
1593 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_options
) {
1594 r
= sd_dhcp_client_add_option(link
->dhcp_client
, send_option
);
1598 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set send option: %m");
1601 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_vendor_options
) {
1602 r
= sd_dhcp_client_add_vendor_option(link
->dhcp_client
, send_option
);
1606 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set send option: %m");
1609 r
= dhcp4_set_hostname(link
);
1613 if (link
->network
->dhcp_vendor_class_identifier
) {
1614 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1615 link
->network
->dhcp_vendor_class_identifier
);
1617 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set vendor class identifier: %m");
1620 if (link
->network
->dhcp_mudurl
) {
1621 r
= sd_dhcp_client_set_mud_url(link
->dhcp_client
, link
->network
->dhcp_mudurl
);
1623 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set MUD URL: %m");
1626 if (link
->network
->dhcp_user_class
) {
1627 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, link
->network
->dhcp_user_class
);
1629 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set user class: %m");
1633 if (link
->network
->dhcp_client_port
> 0) {
1634 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1636 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set listen port: %m");
1638 if (link
->network
->dhcp_port
> 0) {
1639 r
= sd_dhcp_client_set_port(link
->dhcp_client
, link
->network
->dhcp_port
);
1641 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set server port: %m");
1644 if (link
->network
->dhcp_max_attempts
> 0) {
1645 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1647 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set max attempts: %m");
1650 if (link
->network
->dhcp_ip_service_type
>= 0) {
1651 r
= sd_dhcp_client_set_service_type(link
->dhcp_client
, link
->network
->dhcp_ip_service_type
);
1653 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set IP service type: %m");
1656 if (link
->network
->dhcp_socket_priority_set
) {
1657 r
= sd_dhcp_client_set_socket_priority(link
->dhcp_client
, link
->network
->dhcp_socket_priority
);
1659 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed to set socket priority: %m");
1662 if (link
->network
->dhcp_fallback_lease_lifetime_usec
> 0) {
1663 r
= sd_dhcp_client_set_fallback_lease_lifetime(link
->dhcp_client
, link
->network
->dhcp_fallback_lease_lifetime_usec
);
1665 return log_link_debug_errno(link
, r
, "DHCPv4 CLIENT: Failed set to lease lifetime: %m");
1668 return dhcp4_set_client_identifier(link
);
1671 int dhcp4_update_mac(Link
*link
) {
1677 if (!link
->dhcp_client
)
1680 restart
= sd_dhcp_client_is_running(link
->dhcp_client
);
1682 r
= sd_dhcp_client_stop(link
->dhcp_client
);
1686 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1687 link
->hw_addr
.bytes
,
1688 link
->bcast_addr
.length
> 0 ? link
->bcast_addr
.bytes
: NULL
,
1689 link
->hw_addr
.length
, link
->iftype
);
1693 r
= dhcp4_set_client_identifier(link
);
1698 r
= dhcp4_start(link
);
1706 int dhcp4_update_ipv6_connectivity(Link
*link
) {
1712 if (!link
->network
->dhcp_ipv6_only_mode
)
1715 if (!link
->dhcp_client
)
1718 /* If the client is running, set the current connectivity. */
1719 if (sd_dhcp_client_is_running(link
->dhcp_client
))
1720 return sd_dhcp_client_set_ipv6_connectivity(link
->dhcp_client
, link_has_ipv6_connectivity(link
));
1722 /* If the client has been already stopped or not started yet, let's check the current connectivity
1723 * and start the client if necessary. */
1724 if (link_has_ipv6_connectivity(link
))
1727 return dhcp4_start_full(link
, /* set_ipv6_connectivity = */ false);
1730 int dhcp4_start_full(Link
*link
, bool set_ipv6_connectivity
) {
1734 assert(link
->network
);
1736 if (!link
->dhcp_client
)
1739 if (!link_has_carrier(link
))
1742 if (sd_dhcp_client_is_running(link
->dhcp_client
) > 0)
1745 r
= sd_dhcp_client_start(link
->dhcp_client
);
1749 if (set_ipv6_connectivity
) {
1750 r
= dhcp4_update_ipv6_connectivity(link
);
1758 int dhcp4_renew(Link
*link
) {
1761 if (!link
->dhcp_client
)
1764 /* The DHCPv4 client may have been stopped by the IPv6 only mode. Let's unconditionally restart the
1765 * client if it is not running. */
1766 if (!sd_dhcp_client_is_running(link
->dhcp_client
))
1767 return dhcp4_start(link
);
1769 /* The client may be waiting for IPv6 connectivity. Let's restart the client in that case. */
1770 if (dhcp_client_get_state(link
->dhcp_client
) != DHCP_STATE_BOUND
)
1771 return sd_dhcp_client_interrupt_ipv6_only_mode(link
->dhcp_client
);
1773 /* Otherwise, send a RENEW command. */
1774 return sd_dhcp_client_send_renew(link
->dhcp_client
);
1777 static int dhcp4_configure_duid(Link
*link
) {
1779 assert(link
->network
);
1781 if (link
->network
->dhcp_client_identifier
!= DHCP_CLIENT_ID_DUID
)
1784 return dhcp_configure_duid(link
, link_get_dhcp4_duid(link
));
1787 static int dhcp4_process_request(Request
*req
, Link
*link
, void *userdata
) {
1792 if (!link_is_ready_to_configure(link
, /* allow_unmanaged = */ false))
1795 r
= dhcp4_configure_duid(link
);
1799 r
= dhcp4_configure(link
);
1801 return log_link_warning_errno(link
, r
, "Failed to configure DHCPv4 client: %m");
1803 r
= dhcp4_start(link
);
1805 return log_link_warning_errno(link
, r
, "Failed to start DHCPv4 client: %m");
1807 log_link_debug(link
, "DHCPv4 client is configured%s.",
1808 r
> 0 ? ", acquiring DHCPv4 lease" : "");
1812 int link_request_dhcp4_client(Link
*link
) {
1817 if (!link_dhcp4_enabled(link
))
1820 if (link
->dhcp_client
)
1823 r
= link_queue_request(link
, REQUEST_TYPE_DHCP4_CLIENT
, dhcp4_process_request
, NULL
);
1825 return log_link_warning_errno(link
, r
, "Failed to request configuring of the DHCPv4 client: %m");
1827 log_link_debug(link
, "Requested configuring of the DHCPv4 client.");
1831 int config_parse_dhcp_max_attempts(
1833 const char *filename
,
1835 const char *section
,
1836 unsigned section_line
,
1843 Network
*network
= ASSERT_PTR(data
);
1850 if (isempty(rvalue
)) {
1851 network
->dhcp_max_attempts
= 0;
1855 if (streq(rvalue
, "infinity")) {
1856 network
->dhcp_max_attempts
= UINT64_MAX
;
1860 r
= safe_atou64(rvalue
, &a
);
1862 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1863 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1868 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1869 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1873 network
->dhcp_max_attempts
= a
;
1878 int config_parse_dhcp_ip_service_type(
1880 const char *filename
,
1882 const char *section
,
1883 unsigned section_line
,
1890 int *tos
= ASSERT_PTR(data
);
1896 if (isempty(rvalue
))
1897 *tos
= -1; /* use sd_dhcp_client's default (currently, CS6). */
1898 else if (streq(rvalue
, "none"))
1900 else if (streq(rvalue
, "CS4"))
1901 *tos
= IPTOS_CLASS_CS4
;
1902 else if (streq(rvalue
, "CS6"))
1903 *tos
= IPTOS_CLASS_CS6
;
1905 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1906 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1911 int config_parse_dhcp_socket_priority(
1913 const char *filename
,
1915 const char *section
,
1916 unsigned section_line
,
1923 Network
*network
= ASSERT_PTR(data
);
1929 if (isempty(rvalue
)) {
1930 network
->dhcp_socket_priority_set
= false;
1934 r
= safe_atoi(rvalue
, &a
);
1936 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1937 "Failed to parse socket priority, ignoring: %s", rvalue
);
1941 network
->dhcp_socket_priority_set
= true;
1942 network
->dhcp_socket_priority
= a
;
1947 int config_parse_dhcp_fallback_lease_lifetime(
1949 const char *filename
,
1951 const char *section
,
1952 unsigned section_line
,
1959 Network
*network
= userdata
;
1967 if (isempty(rvalue
)) {
1968 network
->dhcp_fallback_lease_lifetime_usec
= 0;
1972 /* We accept only "forever" or "infinity". */
1973 if (!STR_IN_SET(rvalue
, "forever", "infinity")) {
1974 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1975 "Invalid LeaseLifetime= value, ignoring: %s", rvalue
);
1979 network
->dhcp_fallback_lease_lifetime_usec
= USEC_INFINITY
;
1984 int config_parse_dhcp_label(
1986 const char *filename
,
1988 const char *section
,
1989 unsigned section_line
,
1996 char **label
= ASSERT_PTR(data
);
2002 if (isempty(rvalue
)) {
2003 *label
= mfree(*label
);
2007 if (!address_label_valid(rvalue
)) {
2008 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
2009 "Address label is too long or invalid, ignoring assignment: %s", rvalue
);
2013 return free_and_strdup_warn(label
, rvalue
);
2016 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
2017 [DHCP_CLIENT_ID_MAC
] = "mac",
2018 [DHCP_CLIENT_ID_DUID
] = "duid",
2021 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
2022 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
2023 "Failed to parse client identifier type");