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-dhcp4.h"
16 #include "networkd-ipv4acd.h"
17 #include "networkd-link.h"
18 #include "networkd-manager.h"
19 #include "networkd-network.h"
20 #include "networkd-nexthop.h"
21 #include "networkd-queue.h"
22 #include "networkd-route.h"
23 #include "networkd-setlink.h"
24 #include "networkd-state-file.h"
25 #include "string-table.h"
27 #include "sysctl-util.h"
29 static int dhcp4_request_address_and_routes(Link
*link
, bool announce
);
30 static int dhcp4_remove_all(Link
*link
);
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
;
57 static int dhcp4_release_old_lease(Link
*link
) {
63 if (!link
->dhcp_address_old
&& set_isempty(link
->dhcp_routes_old
))
66 log_link_debug(link
, "Removing old DHCPv4 address and routes.");
68 SET_FOREACH(route
, link
->dhcp_routes_old
) {
69 k
= route_remove(route
, NULL
, link
);
74 if (link
->dhcp_address_old
) {
75 k
= address_remove(link
->dhcp_address_old
, link
);
83 static void dhcp4_check_ready(Link
*link
) {
86 if (link
->dhcp4_messages
> 0) {
87 log_link_debug(link
, "%s(): DHCPv4 address and routes are not set.", __func__
);
91 if (!link
->dhcp_address
) {
92 log_link_debug(link
, "%s(): DHCPv4 address is not set.", __func__
);
96 if (!address_is_ready(link
->dhcp_address
)) {
97 log_link_debug(link
, "%s(): DHCPv4 address is not ready.", __func__
);
101 link
->dhcp4_configured
= true;
103 /* New address and routes are configured now. Let's release old lease. */
104 r
= dhcp4_release_old_lease(link
);
106 link_enter_failed(link
);
110 r
= sd_ipv4ll_stop(link
->ipv4ll
);
112 log_link_warning_errno(link
, r
, "Failed to drop IPv4 link-local address, ignoring: %m");
114 link_check_ready(link
);
117 static int dhcp4_after_route_configure(Request
*req
, void *object
) {
118 Route
*route
= object
;
124 assert(req
->type
== REQUEST_TYPE_ROUTE
);
129 r
= set_ensure_put(&link
->dhcp_routes
, &route_hash_ops
, route
);
131 return log_link_error_errno(link
, r
, "Failed to store DHCPv4 route: %m");
133 set_remove(link
->dhcp_routes_old
, route
);
138 static int dhcp4_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
142 assert(link
->dhcp4_messages
> 0);
144 link
->dhcp4_messages
--;
146 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
149 r
= sd_netlink_message_get_errno(m
);
150 if (r
== -ENETUNREACH
&& !link
->dhcp4_route_retrying
) {
152 /* It seems kernel does not support that the prefix route cannot be configured with
153 * route table. Let's once drop the config and reconfigure them later. */
155 log_link_message_debug_errno(link
, m
, r
, "Could not set DHCPv4 route, retrying later");
156 link
->dhcp4_route_failed
= true;
157 link
->manager
->dhcp4_prefix_root_cannot_set_table
= true;
158 } else if (r
< 0 && r
!= -EEXIST
) {
159 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 route");
160 link_enter_failed(link
);
164 if (link
->dhcp4_messages
== 0 && link
->dhcp4_route_failed
) {
165 link
->dhcp4_route_failed
= false;
166 link
->dhcp4_route_retrying
= true;
168 r
= dhcp4_remove_all(link
);
170 link_enter_failed(link
);
172 r
= link_request_static_nexthops(link
, true);
174 link_enter_failed(link
);
176 r
= link_request_static_routes(link
, true);
178 link_enter_failed(link
);
180 r
= dhcp4_request_address_and_routes(link
, false);
182 link_enter_failed(link
);
187 dhcp4_check_ready(link
);
192 static int dhcp4_request_route(Route
*in
, Link
*link
) {
193 _cleanup_(route_freep
) Route
*route
= in
;
200 r
= link_has_route(link
, route
);
204 link
->dhcp4_configured
= false;
206 r
= link_request_route(link
, TAKE_PTR(route
), true, &link
->dhcp4_messages
,
207 dhcp4_route_handler
, &req
);
211 req
->after_configure
= dhcp4_after_route_configure
;
216 static bool link_prefixroute(Link
*link
) {
217 return !link
->network
->dhcp_route_table_set
||
218 link
->network
->dhcp_route_table
== RT_TABLE_MAIN
||
219 link
->manager
->dhcp4_prefix_root_cannot_set_table
;
222 static int dhcp4_request_prefix_route(Link
*link
) {
223 _cleanup_(route_freep
) Route
*route
= NULL
;
224 struct in_addr address
, netmask
;
228 assert(link
->dhcp_lease
);
230 if (link_prefixroute(link
))
231 /* When true, the route will be created by kernel. See dhcp4_update_address(). */
234 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
238 r
= sd_dhcp_lease_get_netmask(link
->dhcp_lease
, &netmask
);
242 r
= route_new(&route
);
246 route
->family
= AF_INET
;
247 route
->dst
.in
.s_addr
= address
.s_addr
& netmask
.s_addr
;
248 route
->dst_prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
249 route
->prefsrc
.in
= address
;
250 route
->scope
= RT_SCOPE_LINK
;
251 route
->protocol
= RTPROT_DHCP
;
252 route
->table
= link_get_dhcp_route_table(link
);
253 route
->mtu
= link
->network
->dhcp_route_mtu
;
255 return dhcp4_request_route(TAKE_PTR(route
), link
);
258 static int dhcp4_request_route_to_gateway(Link
*link
, const struct in_addr
*gw
) {
259 _cleanup_(route_freep
) Route
*route
= NULL
;
260 struct in_addr address
;
264 assert(link
->dhcp_lease
);
267 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
271 r
= route_new(&route
);
275 route
->family
= AF_INET
;
277 route
->dst_prefixlen
= 32;
278 route
->prefsrc
.in
= address
;
279 route
->scope
= RT_SCOPE_LINK
;
280 route
->protocol
= RTPROT_DHCP
;
281 route
->priority
= link
->network
->dhcp_route_metric
;
282 route
->table
= link_get_dhcp_route_table(link
);
283 route
->mtu
= link
->network
->dhcp_route_mtu
;
285 return dhcp4_request_route(TAKE_PTR(route
), link
);
288 static int dhcp4_request_route_auto(
291 const struct in_addr
*gw
) {
293 _cleanup_(route_freep
) Route
*route
= in
;
294 struct in_addr address
, netmask
, prefix
;
300 assert(link
->dhcp_lease
);
303 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
307 r
= sd_dhcp_lease_get_netmask(link
->dhcp_lease
, &netmask
);
311 prefix
.s_addr
= address
.s_addr
& netmask
.s_addr
;
312 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
314 if (in4_addr_is_localhost(&route
->dst
.in
)) {
315 if (in4_addr_is_set(gw
))
316 log_link_debug(link
, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR
"/%u is localhost, "
317 "ignoring gateway address "IPV4_ADDRESS_FMT_STR
,
318 IPV4_ADDRESS_FMT_VAL(route
->dst
.in
), route
->dst_prefixlen
, IPV4_ADDRESS_FMT_VAL(*gw
));
320 route
->scope
= RT_SCOPE_HOST
;
321 route
->gw_family
= AF_UNSPEC
;
322 route
->gw
= IN_ADDR_NULL
;
323 route
->prefsrc
= IN_ADDR_NULL
;
325 } else if (in4_addr_equal(&route
->dst
.in
, &address
)) {
326 if (in4_addr_is_set(gw
))
327 log_link_debug(link
, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR
"/%u is equivalent to the acquired address, "
328 "ignoring gateway address "IPV4_ADDRESS_FMT_STR
,
329 IPV4_ADDRESS_FMT_VAL(route
->dst
.in
), route
->dst_prefixlen
, IPV4_ADDRESS_FMT_VAL(*gw
));
331 route
->scope
= RT_SCOPE_HOST
;
332 route
->gw_family
= AF_UNSPEC
;
333 route
->gw
= IN_ADDR_NULL
;
334 route
->prefsrc
.in
= address
;
336 } else if (route
->dst_prefixlen
>= prefixlen
&&
337 (route
->dst
.in
.s_addr
& netmask
.s_addr
) == prefix
.s_addr
) {
338 if (in4_addr_is_set(gw
))
339 log_link_debug(link
, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR
"/%u is in the assigned network "
340 IPV4_ADDRESS_FMT_STR
"/%u, ignoring gateway address "IPV4_ADDRESS_FMT_STR
,
341 IPV4_ADDRESS_FMT_VAL(route
->dst
.in
), route
->dst_prefixlen
,
342 IPV4_ADDRESS_FMT_VAL(prefix
), prefixlen
,
343 IPV4_ADDRESS_FMT_VAL(*gw
));
345 route
->scope
= RT_SCOPE_LINK
;
346 route
->gw_family
= AF_UNSPEC
;
347 route
->gw
= IN_ADDR_NULL
;
348 route
->prefsrc
.in
= address
;
351 if (in4_addr_is_null(gw
)) {
352 log_link_debug(link
, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR
"/%u is not in the assigned network "
353 IPV4_ADDRESS_FMT_STR
"/%u, but no gateway is specified, ignoring.",
354 IPV4_ADDRESS_FMT_VAL(route
->dst
.in
), route
->dst_prefixlen
,
355 IPV4_ADDRESS_FMT_VAL(prefix
), prefixlen
);
359 r
= dhcp4_request_route_to_gateway(link
, gw
);
363 route
->scope
= RT_SCOPE_UNIVERSE
;
364 route
->gw_family
= AF_INET
;
366 route
->prefsrc
.in
= address
;
369 return dhcp4_request_route(TAKE_PTR(route
), link
);
372 static int dhcp4_request_static_routes(Link
*link
, struct in_addr
*ret_default_gw
) {
373 _cleanup_free_ sd_dhcp_route
**static_routes
= NULL
;
374 bool classless_route
= false, static_route
= false;
375 struct in_addr default_gw
= {};
379 assert(link
->dhcp_lease
);
380 assert(ret_default_gw
);
382 n
= sd_dhcp_lease_get_routes(link
->dhcp_lease
, &static_routes
);
383 if (IN_SET(n
, 0, -ENODATA
)) {
384 log_link_debug(link
, "DHCP: No static routes received from DHCP server.");
390 for (int i
= 0; i
< n
; i
++) {
391 switch (sd_dhcp_route_get_option(static_routes
[i
])) {
392 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
393 classless_route
= true;
395 case SD_DHCP_OPTION_STATIC_ROUTE
:
401 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
402 * the DHCP client MUST ignore the Static Routes option. */
403 if (classless_route
&& static_route
)
404 log_link_debug(link
, "Classless static routes received from DHCP server: ignoring static-route option");
406 if (!link
->network
->dhcp_use_routes
) {
407 if (!classless_route
)
410 /* Even if UseRoutes=no, try to find default gateway to make semi-static routes and
411 * routes to DNS or NTP servers can be configured in later steps. */
412 for (int i
= 0; i
< n
; i
++) {
416 if (sd_dhcp_route_get_option(static_routes
[i
]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
)
419 r
= sd_dhcp_route_get_destination(static_routes
[i
], &dst
);
423 if (in4_addr_is_set(&dst
))
426 r
= sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &prefixlen
);
433 r
= sd_dhcp_route_get_gateway(static_routes
[i
], ret_default_gw
);
440 /* Do not return 1 here, to ensure the router option can override the default gateway
445 for (int i
= 0; i
< n
; i
++) {
446 _cleanup_(route_freep
) Route
*route
= NULL
;
449 if (sd_dhcp_route_get_option(static_routes
[i
]) !=
450 (classless_route
? SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
: SD_DHCP_OPTION_STATIC_ROUTE
))
453 r
= route_new(&route
);
457 route
->family
= AF_INET
;
458 route
->gw_family
= AF_INET
;
459 route
->protocol
= RTPROT_DHCP
;
460 route
->priority
= link
->network
->dhcp_route_metric
;
461 route
->table
= link_get_dhcp_route_table(link
);
462 route
->mtu
= link
->network
->dhcp_route_mtu
;
464 r
= sd_dhcp_route_get_gateway(static_routes
[i
], &gw
);
468 r
= sd_dhcp_route_get_destination(static_routes
[i
], &route
->dst
.in
);
472 r
= sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &route
->dst_prefixlen
);
476 /* When classless static routes are provided, then router option will be ignored. To
477 * use the default gateway later in other routes, e.g., routes to dns servers, here we
478 * need to find the default gateway in the classless static routes. */
479 if (classless_route
&&
480 in4_addr_is_null(&route
->dst
.in
) && route
->dst_prefixlen
== 0 &&
481 in4_addr_is_null(&default_gw
))
484 r
= dhcp4_request_route_auto(TAKE_PTR(route
), link
, &gw
);
489 *ret_default_gw
= default_gw
;
490 return classless_route
;
493 static int dhcp4_request_gateway(Link
*link
, struct in_addr
*gw
) {
494 _cleanup_(route_freep
) Route
*route
= NULL
;
495 const struct in_addr
*router
;
496 struct in_addr address
;
500 assert(link
->dhcp_lease
);
503 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
507 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
508 if (IN_SET(r
, 0, -ENODATA
)) {
509 log_link_debug(link
, "DHCP: No gateway received from DHCP server.");
514 if (in4_addr_is_null(&router
[0])) {
515 log_link_debug(link
, "DHCP: Received gateway address is null.");
519 if (!link
->network
->dhcp_use_gateway
) {
520 /* When no classless static route is provided, even if UseGateway=no, use the gateway
521 * address to configure semi-static routes or routes to DNS or NTP servers. Note, if
522 * neither UseRoutes= nor UseGateway= is disabled, use the default gateway in classless
523 * static routes if provided (in that case, in4_addr_is_null(gw) below is true). */
524 if (in4_addr_is_null(gw
))
529 /* The dhcp netmask may mask out the gateway. First, add an explicit route for the gateway host
530 * so that we can route no matter the netmask or existing kernel route tables. */
531 r
= dhcp4_request_route_to_gateway(link
, &router
[0]);
535 r
= route_new(&route
);
539 /* Next, add a default gateway. */
540 route
->family
= AF_INET
;
541 route
->gw_family
= AF_INET
;
542 route
->gw
.in
= router
[0];
543 route
->prefsrc
.in
= address
;
544 route
->protocol
= RTPROT_DHCP
;
545 route
->priority
= link
->network
->dhcp_route_metric
;
546 route
->table
= link_get_dhcp_route_table(link
);
547 route
->mtu
= link
->network
->dhcp_route_mtu
;
549 r
= dhcp4_request_route(TAKE_PTR(route
), link
);
553 /* When no classless static route is provided, or UseRoutes=no, then use the router address to
554 * configure semi-static routes and routes to DNS or NTP servers in later steps. */
559 static int dhcp4_request_semi_static_routes(Link
*link
, const struct in_addr
*gw
) {
564 assert(link
->dhcp_lease
);
565 assert(link
->network
);
568 if (in4_addr_is_null(gw
))
571 HASHMAP_FOREACH(rt
, link
->network
->routes_by_section
) {
572 _cleanup_(route_freep
) Route
*route
= NULL
;
574 if (!rt
->gateway_from_dhcp_or_ra
)
577 if (rt
->gw_family
!= AF_INET
)
580 r
= dhcp4_request_route_to_gateway(link
, gw
);
584 r
= route_dup(rt
, &route
);
589 if (!route
->protocol_set
)
590 route
->protocol
= RTPROT_DHCP
;
591 if (!route
->priority_set
)
592 route
->priority
= link
->network
->dhcp_route_metric
;
593 if (!route
->table_set
)
594 route
->table
= link_get_dhcp_route_table(link
);
596 route
->mtu
= link
->network
->dhcp_route_mtu
;
598 r
= dhcp4_request_route(TAKE_PTR(route
), link
);
606 static int dhcp4_request_routes_to_servers(
608 const struct in_addr
*servers
,
610 const struct in_addr
*gw
) {
615 assert(link
->dhcp_lease
);
616 assert(link
->network
);
617 assert(servers
|| n_servers
== 0);
620 for (size_t i
= 0; i
< n_servers
; i
++) {
621 _cleanup_(route_freep
) Route
*route
= NULL
;
623 if (in4_addr_is_null(&servers
[i
]))
626 r
= route_new(&route
);
630 route
->family
= AF_INET
;
631 route
->dst
.in
= servers
[i
];
632 route
->dst_prefixlen
= 32;
633 route
->protocol
= RTPROT_DHCP
;
634 route
->priority
= link
->network
->dhcp_route_metric
;
635 route
->table
= link_get_dhcp_route_table(link
);
636 route
->mtu
= link
->network
->dhcp_route_mtu
;
638 r
= dhcp4_request_route_auto(TAKE_PTR(route
), link
, gw
);
646 static int dhcp4_request_routes_to_dns(Link
*link
, const struct in_addr
*gw
) {
647 const struct in_addr
*dns
;
651 assert(link
->dhcp_lease
);
652 assert(link
->network
);
655 if (!link
->network
->dhcp_use_dns
||
656 !link
->network
->dhcp_routes_to_dns
)
659 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &dns
);
660 if (IN_SET(r
, 0, -ENODATA
))
665 return dhcp4_request_routes_to_servers(link
, dns
, r
, gw
);
668 static int dhcp4_request_routes_to_ntp(Link
*link
, const struct in_addr
*gw
) {
669 const struct in_addr
*ntp
;
673 assert(link
->dhcp_lease
);
674 assert(link
->network
);
677 if (!link
->network
->dhcp_use_ntp
||
678 !link
->network
->dhcp_routes_to_ntp
)
681 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &ntp
);
682 if (IN_SET(r
, 0, -ENODATA
))
687 return dhcp4_request_routes_to_servers(link
, ntp
, r
, gw
);
690 static int dhcp4_request_routes(Link
*link
) {
691 struct in_addr gw
= {};
697 if (!link
->dhcp_lease
)
700 while ((rt
= set_steal_first(link
->dhcp_routes
))) {
701 r
= set_ensure_put(&link
->dhcp_routes_old
, &route_hash_ops
, rt
);
703 return log_link_error_errno(link
, r
, "Failed to store old DHCPv4 route: %m");
706 r
= dhcp4_request_prefix_route(link
);
708 return log_link_error_errno(link
, r
, "DHCP error: Could not request prefix route: %m");
710 r
= dhcp4_request_static_routes(link
, &gw
);
712 return log_link_error_errno(link
, r
, "DHCP error: Could not request static routes: %m");
714 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
715 * a Router option, the DHCP client MUST ignore the Router option. */
716 r
= dhcp4_request_gateway(link
, &gw
);
718 return log_link_error_errno(link
, r
, "DHCP error: Could not request gateway: %m");
721 r
= dhcp4_request_semi_static_routes(link
, &gw
);
723 return log_link_error_errno(link
, r
, "DHCP error: Could not request routes with Gateway=_dhcp4 setting: %m");
725 r
= dhcp4_request_routes_to_dns(link
, &gw
);
727 return log_link_error_errno(link
, r
, "DHCP error: Could not request routes to DNS servers: %m");
729 r
= dhcp4_request_routes_to_ntp(link
, &gw
);
731 return log_link_error_errno(link
, r
, "DHCP error: Could not request routes to NTP servers: %m");
736 static int dhcp_reset_mtu(Link
*link
) {
742 if (!link
->network
->dhcp_use_mtu
)
745 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
749 return log_link_error_errno(link
, r
, "DHCP error: failed to get MTU from lease: %m");
751 if (link
->original_mtu
== mtu
)
754 r
= link_request_to_set_mtu(link
, link
->original_mtu
);
756 return log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
761 static int dhcp_reset_hostname(Link
*link
) {
762 const char *hostname
;
767 if (!link
->network
->dhcp_use_hostname
)
770 hostname
= link
->network
->dhcp_hostname
;
772 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
777 /* If a hostname was set due to the lease, then unset it now. */
778 r
= manager_set_hostname(link
->manager
, NULL
);
780 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
785 static int dhcp4_remove_all(Link
*link
) {
791 SET_FOREACH(route
, link
->dhcp_routes
) {
792 k
= route_remove(route
, NULL
, link
);
797 if (link
->dhcp_address
) {
798 k
= address_remove(link
->dhcp_address
, link
);
806 int dhcp4_lease_lost(Link
*link
) {
810 assert(link
->dhcp_lease
);
812 log_link_info(link
, "DHCP lease lost");
814 link
->dhcp4_configured
= false;
816 /* dhcp4_lease_lost() may be called during renewing IP address. */
817 k
= dhcp4_release_old_lease(link
);
821 k
= dhcp4_remove_all(link
);
825 k
= dhcp_reset_mtu(link
);
829 k
= dhcp_reset_hostname(link
);
833 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
836 if (link
->network
->dhcp_send_decline
) {
839 /* The acquired address may be still ARP probing and not configured. */
841 SET_FOREACH(a
, link
->addresses_ipv4acd
)
842 if (!a
->is_static
&& address_get(link
, a
, NULL
) < 0) {
848 log_link_debug(link
, "Canceling the request to configure DHCPv4 address "IPV4_ADDRESS_FMT_STR
,
849 IPV4_ADDRESS_FMT_VAL(a
->in_addr
.in
));
850 request_drop(ordered_set_get(link
->manager
->request_queue
, &req
));
857 r
= link_request_static_nexthops(link
, true);
861 return link_request_static_routes(link
, true);
864 static int dhcp4_address_ready_callback(Address
*address
) {
867 /* Do not call this again. */
868 address
->callback
= NULL
;
870 dhcp4_check_ready(address
->link
);
874 static int dhcp4_after_address_configure(Request
*req
, void *object
) {
875 Address
*address
= object
;
881 assert(req
->type
== REQUEST_TYPE_ADDRESS
);
886 if (!address_equal(link
->dhcp_address
, address
)) {
887 if (link
->dhcp_address_old
&&
888 !address_equal(link
->dhcp_address_old
, link
->dhcp_address
)) {
889 /* Still too old address exists? Let's remove it immediately. */
890 r
= address_remove(link
->dhcp_address_old
, link
);
894 link
->dhcp_address_old
= link
->dhcp_address
;
897 link
->dhcp_address
= address
;
901 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
905 assert(link
->dhcp4_messages
> 0);
907 link
->dhcp4_messages
--;
909 r
= address_configure_handler_internal(rtnl
, m
, link
, "Could not set DHCPv4 address");
913 if (address_is_ready(link
->dhcp_address
)) {
914 r
= dhcp4_address_ready_callback(link
->dhcp_address
);
916 link_enter_failed(link
);
918 link
->dhcp_address
->callback
= dhcp4_address_ready_callback
;
923 static int dhcp4_request_address(Link
*link
, bool announce
) {
924 _cleanup_(address_freep
) Address
*addr
= NULL
;
925 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
926 struct in_addr address
, netmask
;
932 assert(link
->network
);
934 if (!link
->dhcp_lease
)
937 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
939 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
941 r
= sd_dhcp_lease_get_netmask(link
->dhcp_lease
, &netmask
);
943 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
945 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
946 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
948 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
951 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
954 const struct in_addr
*router
;
956 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
957 if (r
< 0 && r
!= -ENODATA
)
958 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
960 if (r
> 0 && in4_addr_is_set(&router
[0]))
962 LOG_LINK_INTERFACE(link
),
963 LOG_LINK_MESSAGE(link
, "DHCPv4 address "IPV4_ADDRESS_FMT_STR
"/%u via "IPV4_ADDRESS_FMT_STR
,
964 IPV4_ADDRESS_FMT_VAL(address
),
966 IPV4_ADDRESS_FMT_VAL(router
[0])),
967 "ADDRESS="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(address
),
968 "PREFIXLEN=%u", prefixlen
,
969 "GATEWAY="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(router
[0]));
972 LOG_LINK_INTERFACE(link
),
973 LOG_LINK_MESSAGE(link
, "DHCPv4 address "IPV4_ADDRESS_FMT_STR
"/%u",
974 IPV4_ADDRESS_FMT_VAL(address
),
976 "ADDRESS="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(address
),
977 "PREFIXLEN=%u", prefixlen
);
980 r
= address_new(&addr
);
984 addr
->family
= AF_INET
;
985 addr
->in_addr
.in
.s_addr
= address
.s_addr
;
986 addr
->cinfo
.ifa_prefered
= lifetime
;
987 addr
->cinfo
.ifa_valid
= lifetime
;
988 addr
->prefixlen
= prefixlen
;
990 addr
->broadcast
.s_addr
= address
.s_addr
| ~netmask
.s_addr
;
991 SET_FLAG(addr
->flags
, IFA_F_NOPREFIXROUTE
, !link_prefixroute(link
));
992 addr
->route_metric
= link
->network
->dhcp_route_metric
;
993 addr
->duplicate_address_detection
= link
->network
->dhcp_send_decline
? ADDRESS_FAMILY_IPV4
: ADDRESS_FAMILY_NO
;
995 if (address_get(link
, addr
, NULL
) < 0)
996 link
->dhcp4_configured
= false;
998 r
= link_request_address(link
, TAKE_PTR(addr
), true, &link
->dhcp4_messages
,
999 dhcp4_address_handler
, &req
);
1001 return log_link_error_errno(link
, r
, "Failed to request DHCPv4 address: %m");
1005 req
->after_configure
= dhcp4_after_address_configure
;
1010 static int dhcp4_request_address_and_routes(Link
*link
, bool announce
) {
1015 r
= dhcp4_request_address(link
, announce
);
1019 r
= dhcp4_request_routes(link
);
1023 link_set_state(link
, LINK_STATE_CONFIGURING
);
1024 link_check_ready(link
);
1029 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
1030 sd_dhcp_lease
*lease
;
1036 r
= sd_dhcp_client_get_lease(client
, &lease
);
1038 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
1040 sd_dhcp_lease_unref(link
->dhcp_lease
);
1041 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
1044 return dhcp4_request_address_and_routes(link
, false);
1047 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
1048 sd_dhcp_lease
*lease
;
1054 r
= sd_dhcp_client_get_lease(client
, &lease
);
1056 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
1058 sd_dhcp_lease_unref(link
->dhcp_lease
);
1059 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
1062 if (link
->network
->dhcp_use_mtu
) {
1065 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
1067 r
= link_request_to_set_mtu(link
, mtu
);
1069 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
1073 if (link
->network
->dhcp_use_hostname
) {
1074 const char *dhcpname
= NULL
;
1075 _cleanup_free_
char *hostname
= NULL
;
1077 if (link
->network
->dhcp_hostname
)
1078 dhcpname
= link
->network
->dhcp_hostname
;
1080 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
1083 r
= shorten_overlong(dhcpname
, &hostname
);
1085 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
1087 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
1091 r
= manager_set_hostname(link
->manager
, hostname
);
1093 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
1097 if (link
->network
->dhcp_use_timezone
) {
1098 const char *tz
= NULL
;
1100 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
1103 r
= manager_set_timezone(link
->manager
, tz
);
1105 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
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_deny_listed(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 (set_contains(link
->network
->dhcp_deny_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1140 log_struct(LOG_DEBUG
,
1141 LOG_LINK_INTERFACE(link
),
1142 LOG_LINK_MESSAGE(link
, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR
" found in deny-list, ignoring offer",
1143 IPV4_ADDRESS_FMT_VAL(addr
)));
1150 static int dhcp_server_is_allow_listed(Link
*link
, sd_dhcp_client
*client
) {
1151 sd_dhcp_lease
*lease
;
1152 struct in_addr addr
;
1156 assert(link
->network
);
1159 r
= sd_dhcp_client_get_lease(client
, &lease
);
1161 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1163 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1165 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1167 if (set_contains(link
->network
->dhcp_allow_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1168 log_struct(LOG_DEBUG
,
1169 LOG_LINK_INTERFACE(link
),
1170 LOG_LINK_MESSAGE(link
, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR
" found in allow-list, accepting offer",
1171 IPV4_ADDRESS_FMT_VAL(addr
)));
1178 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
1179 Link
*link
= userdata
;
1183 assert(link
->network
);
1184 assert(link
->manager
);
1186 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1190 case SD_DHCP_CLIENT_EVENT_STOP
:
1192 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
1194 r
= sd_ipv4ll_start(link
->ipv4ll
);
1196 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1199 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1200 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1204 if (link
->dhcp_lease
) {
1205 if (link
->network
->dhcp_send_release
) {
1206 r
= sd_dhcp_client_send_release(client
);
1208 log_link_full_errno(link
,
1209 ERRNO_IS_DISCONNECT(r
) ? LOG_DEBUG
: LOG_WARNING
,
1210 r
, "Failed to send DHCP RELEASE, ignoring: %m");
1213 r
= dhcp4_lease_lost(link
);
1215 link_enter_failed(link
);
1221 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
1222 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1223 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1227 if (link
->dhcp_lease
) {
1228 r
= dhcp4_lease_lost(link
);
1230 link_enter_failed(link
);
1236 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
1237 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1238 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1242 r
= dhcp_lease_ip_change(client
, link
);
1244 link_enter_failed(link
);
1249 case SD_DHCP_CLIENT_EVENT_RENEW
:
1250 r
= dhcp_lease_renew(client
, link
);
1252 link_enter_failed(link
);
1256 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1257 r
= dhcp_lease_acquired(client
, link
);
1259 link_enter_failed(link
);
1263 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1264 if (!set_isempty(link
->network
->dhcp_allow_listed_ip
)) {
1265 r
= dhcp_server_is_allow_listed(link
, client
);
1271 r
= dhcp_server_is_deny_listed(link
, client
);
1279 case SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE
:
1280 if (link
->ipv4ll
&& !sd_ipv4ll_is_running(link
->ipv4ll
)) {
1281 log_link_debug(link
, "Problems acquiring DHCP lease, acquiring IPv4 link-local address");
1283 r
= sd_ipv4ll_start(link
->ipv4ll
);
1285 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1291 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1293 log_link_warning(link
, "DHCP unknown event: %i", event
);
1300 static int dhcp4_set_hostname(Link
*link
) {
1301 _cleanup_free_
char *hostname
= NULL
;
1307 if (!link
->network
->dhcp_send_hostname
)
1309 else if (link
->network
->dhcp_hostname
)
1310 hn
= link
->network
->dhcp_hostname
;
1312 r
= gethostname_strict(&hostname
);
1313 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1314 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to get hostname: %m");
1319 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1320 if (r
== -EINVAL
&& hostname
)
1321 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1322 log_link_debug_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1324 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
1329 static int dhcp4_set_client_identifier(Link
*link
) {
1333 assert(link
->network
);
1334 assert(link
->dhcp_client
);
1336 switch (link
->network
->dhcp_client_identifier
) {
1337 case DHCP_CLIENT_ID_DUID
: {
1338 /* If configured, apply user specified DUID and IAID */
1339 const DUID
*duid
= link_get_dhcp4_duid(link
);
1341 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1342 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1343 link
->network
->dhcp_iaid_set
,
1344 link
->network
->dhcp_iaid
,
1347 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
1348 link
->network
->dhcp_iaid_set
,
1349 link
->network
->dhcp_iaid
,
1351 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1352 duid
->raw_data_len
);
1354 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1357 case DHCP_CLIENT_ID_DUID_ONLY
: {
1358 /* If configured, apply user specified DUID */
1359 const DUID
*duid
= link_get_dhcp4_duid(link
);
1361 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1362 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
1365 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
1367 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1368 duid
->raw_data_len
);
1370 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
1373 case DHCP_CLIENT_ID_MAC
: {
1374 const uint8_t *hw_addr
= link
->hw_addr
.bytes
;
1375 size_t hw_addr_len
= link
->hw_addr
.length
;
1377 if (link
->iftype
== ARPHRD_INFINIBAND
&& hw_addr_len
== INFINIBAND_ALEN
) {
1378 /* set_client_id expects only last 8 bytes of an IB address */
1379 hw_addr
+= INFINIBAND_ALEN
- 8;
1380 hw_addr_len
-= INFINIBAND_ALEN
- 8;
1383 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1388 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
1392 assert_not_reached();
1398 static int dhcp4_configure_duid(Link
*link
) {
1401 if (!IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
1404 return dhcp_configure_duid(link
, link_get_dhcp4_duid(link
));
1407 static int dhcp4_set_request_address(Link
*link
) {
1411 assert(link
->network
);
1412 assert(link
->dhcp_client
);
1414 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
1417 SET_FOREACH(a
, link
->addresses_foreign
) {
1418 if (a
->family
!= AF_INET
)
1420 if (link_address_is_dynamic(link
, a
))
1427 log_link_debug(link
, "DHCP4 CLIENT: requesting " IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(a
->in_addr
.in
));
1429 return sd_dhcp_client_set_request_address(link
->dhcp_client
, &a
->in_addr
.in
);
1432 static bool link_needs_dhcp_broadcast(Link
*link
) {
1437 assert(link
->network
);
1439 /* Return the setting in DHCP[4].RequestBroadcast if specified. Otherwise return the device property
1440 * ID_NET_DHCP_BROADCAST setting, which may be set for interfaces requiring that the DHCPOFFER message
1441 * is being broadcast because they can't handle unicast messages while not fully configured.
1442 * If neither is set or a failure occurs, return false, which is the default for this flag.
1444 r
= link
->network
->dhcp_broadcast
;
1445 if (r
< 0 && link
->sd_device
&& sd_device_get_property_value(link
->sd_device
, "ID_NET_DHCP_BROADCAST", &val
) >= 0) {
1446 r
= parse_boolean(val
);
1448 log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to parse ID_NET_DHCP_BROADCAST, ignoring: %m");
1450 log_link_debug(link
, "DHCP4 CLIENT: Detected ID_NET_DHCP_BROADCAST='%d'.", r
);
1456 int dhcp4_configure(Link
*link
) {
1457 sd_dhcp_option
*send_option
;
1458 void *request_options
;
1462 assert(link
->network
);
1464 if (!link_dhcp4_enabled(link
))
1467 if (link
->dhcp_client
)
1468 return -EBUSY
; /* Already configured. */
1470 r
= dhcp4_configure_duid(link
);
1474 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1476 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to allocate DHCP4 client: %m");
1478 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, link
->manager
->event
, 0);
1480 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to attach event to DHCP4 client: %m");
1482 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1483 link
->hw_addr
.bytes
,
1484 link
->bcast_addr
.length
> 0 ? link
->bcast_addr
.bytes
: NULL
,
1485 link
->hw_addr
.length
, link
->iftype
);
1487 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1489 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1491 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1493 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1495 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1497 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
, link_needs_dhcp_broadcast(link
));
1499 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1501 if (link
->mtu
> 0) {
1502 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1504 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1507 if (!link
->network
->dhcp_anonymize
) {
1508 if (link
->network
->dhcp_use_mtu
) {
1509 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_INTERFACE_MTU
);
1511 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1514 if (link
->network
->dhcp_use_routes
) {
1515 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_STATIC_ROUTE
);
1517 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1519 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1521 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1524 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
1525 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
);
1527 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1530 if (link
->network
->dhcp_use_ntp
) {
1531 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1533 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1536 if (link
->network
->dhcp_use_sip
) {
1537 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_SIP_SERVER
);
1539 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1542 if (link
->network
->dhcp_use_timezone
) {
1543 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1545 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1548 SET_FOREACH(request_options
, link
->network
->dhcp_request_options
) {
1549 uint32_t option
= PTR_TO_UINT32(request_options
);
1551 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, option
);
1553 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option
);
1556 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_options
) {
1557 r
= sd_dhcp_client_add_option(link
->dhcp_client
, send_option
);
1561 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1564 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_vendor_options
) {
1565 r
= sd_dhcp_client_add_vendor_option(link
->dhcp_client
, send_option
);
1569 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1572 r
= dhcp4_set_hostname(link
);
1576 if (link
->network
->dhcp_vendor_class_identifier
) {
1577 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1578 link
->network
->dhcp_vendor_class_identifier
);
1580 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1583 if (link
->network
->dhcp_mudurl
) {
1584 r
= sd_dhcp_client_set_mud_url(link
->dhcp_client
, link
->network
->dhcp_mudurl
);
1586 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1589 if (link
->network
->dhcp_user_class
) {
1590 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, link
->network
->dhcp_user_class
);
1592 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1596 if (link
->network
->dhcp_client_port
> 0) {
1597 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1599 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen port: %m");
1602 if (link
->network
->dhcp_max_attempts
> 0) {
1603 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1605 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1608 if (link
->network
->dhcp_ip_service_type
> 0) {
1609 r
= sd_dhcp_client_set_service_type(link
->dhcp_client
, link
->network
->dhcp_ip_service_type
);
1611 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set IP service type: %m");
1614 if (link
->network
->dhcp_fallback_lease_lifetime
> 0) {
1615 r
= sd_dhcp_client_set_fallback_lease_lifetime(link
->dhcp_client
, link
->network
->dhcp_fallback_lease_lifetime
);
1617 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1620 r
= dhcp4_set_request_address(link
);
1622 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set initial DHCPv4 address: %m");
1624 return dhcp4_set_client_identifier(link
);
1627 int dhcp4_update_mac(Link
*link
) {
1632 if (!link
->dhcp_client
)
1635 r
= sd_dhcp_client_set_mac(link
->dhcp_client
, link
->hw_addr
.bytes
,
1636 link
->bcast_addr
.length
> 0 ? link
->bcast_addr
.bytes
: NULL
,
1637 link
->hw_addr
.length
, link
->iftype
);
1641 return dhcp4_set_client_identifier(link
);
1644 int dhcp4_start(Link
*link
) {
1647 if (!link
->dhcp_client
)
1650 if (sd_dhcp_client_is_running(link
->dhcp_client
) > 0)
1653 log_link_debug(link
, "Acquiring DHCPv4 lease");
1655 return sd_dhcp_client_start(link
->dhcp_client
);
1658 int config_parse_dhcp_max_attempts(
1660 const char *filename
,
1662 const char *section
,
1663 unsigned section_line
,
1670 Network
*network
= data
;
1678 if (isempty(rvalue
)) {
1679 network
->dhcp_max_attempts
= 0;
1683 if (streq(rvalue
, "infinity")) {
1684 network
->dhcp_max_attempts
= UINT64_MAX
;
1688 r
= safe_atou64(rvalue
, &a
);
1690 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1691 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1696 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1697 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1701 network
->dhcp_max_attempts
= a
;
1706 int config_parse_dhcp_acl_ip_address(
1708 const char *filename
,
1710 const char *section
,
1711 unsigned section_line
,
1718 Network
*network
= data
;
1727 acl
= STR_IN_SET(lvalue
, "DenyList", "BlackList") ? &network
->dhcp_deny_listed_ip
: &network
->dhcp_allow_listed_ip
;
1729 if (isempty(rvalue
)) {
1730 *acl
= set_free(*acl
);
1734 for (const char *p
= rvalue
;;) {
1735 _cleanup_free_
char *n
= NULL
;
1736 union in_addr_union ip
;
1738 r
= extract_first_word(&p
, &n
, NULL
, 0);
1742 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1743 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1750 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1752 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1753 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue
, n
);
1757 r
= set_ensure_put(acl
, NULL
, UINT32_TO_PTR(ip
.in
.s_addr
));
1759 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1760 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue
, n
);
1764 int config_parse_dhcp_ip_service_type(
1766 const char *filename
,
1768 const char *section
,
1769 unsigned section_line
,
1780 if (streq(rvalue
, "CS4"))
1781 *((int *)data
) = IPTOS_CLASS_CS4
;
1782 else if (streq(rvalue
, "CS6"))
1783 *((int *)data
) = IPTOS_CLASS_CS6
;
1785 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1786 "Failed to parse IPServiceType type '%s', ignoring.", rvalue
);
1791 int config_parse_dhcp_fallback_lease_lifetime(const char *unit
,
1792 const char *filename
,
1794 const char *section
,
1795 unsigned section_line
,
1801 Network
*network
= userdata
;
1810 if (isempty(rvalue
)) {
1811 network
->dhcp_fallback_lease_lifetime
= 0;
1815 /* We accept only "forever" or "infinity". */
1816 if (STR_IN_SET(rvalue
, "forever", "infinity"))
1817 k
= CACHE_INFO_INFINITY_LIFE_TIME
;
1819 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1820 "Invalid LeaseLifetime= value, ignoring: %s", rvalue
);
1824 network
->dhcp_fallback_lease_lifetime
= k
;
1829 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1830 [DHCP_CLIENT_ID_MAC
] = "mac",
1831 [DHCP_CLIENT_ID_DUID
] = "duid",
1832 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1835 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1836 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1837 "Failed to parse client identifier type");