1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
4 #include <netinet/ip.h>
6 #include <linux/if_arp.h>
9 #include "alloc-util.h"
10 #include "dhcp-client-internal.h"
11 #include "hostname-util.h"
12 #include "parse-util.h"
13 #include "network-internal.h"
14 #include "networkd-dhcp4.h"
15 #include "networkd-link.h"
16 #include "networkd-manager.h"
17 #include "networkd-network.h"
18 #include "string-table.h"
19 #include "string-util.h"
20 #include "sysctl-util.h"
23 static int dhcp_remove_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
24 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
25 static int dhcp_remove_dns_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
26 static int dhcp_remove_address(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, link_netlink_message_handler_t callback
);
27 static int dhcp_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
);
28 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
);
30 void dhcp4_release_old_lease(Link
*link
) {
31 struct in_addr address
= {}, address_old
= {};
35 if (!link
->dhcp_lease_old
)
38 assert(link
->dhcp_lease
);
40 (void) sd_dhcp_lease_get_address(link
->dhcp_lease_old
, &address_old
);
41 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
43 (void) dhcp_remove_routes(link
, link
->dhcp_lease_old
, &address_old
, false);
44 (void) dhcp_remove_router(link
, link
->dhcp_lease_old
, &address_old
, false);
45 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease_old
, &address_old
, false);
47 if (!in4_addr_equal(&address_old
, &address
))
48 (void) dhcp_remove_address(link
, link
->dhcp_lease_old
, &address_old
, NULL
);
50 link
->dhcp_lease_old
= sd_dhcp_lease_unref(link
->dhcp_lease_old
);
54 static void dhcp4_check_ready(Link
*link
) {
55 if (link
->dhcp4_messages
== 0) {
56 link
->dhcp4_configured
= true;
57 /* New address and routes are configured now. Let's release old lease. */
58 dhcp4_release_old_lease(link
);
59 link_check_ready(link
);
63 static int dhcp4_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
67 assert(link
->dhcp4_messages
> 0);
69 link
->dhcp4_messages
--;
71 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
74 r
= sd_netlink_message_get_errno(m
);
75 if (r
== -ENETUNREACH
&& !link
->dhcp4_route_retrying
) {
77 /* It seems kernel does not support that the prefix route cannot be configured with
78 * route table. Let's once drop the config and reconfigure them later. */
80 log_link_message_debug_errno(link
, m
, r
, "Could not set DHCPv4 route, retrying later");
81 link
->dhcp4_route_failed
= true;
82 link
->manager
->dhcp4_prefix_root_cannot_set_table
= true;
83 } else if (r
< 0 && r
!= -EEXIST
) {
84 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 route");
85 link_enter_failed(link
);
89 if (link
->dhcp4_messages
== 0) {
90 if (link
->dhcp4_route_failed
) {
91 struct in_addr address
= {};
93 link
->dhcp4_route_failed
= false;
94 link
->dhcp4_route_retrying
= true;
96 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
97 (void) dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true);
98 (void) dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true);
99 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true);
100 (void) dhcp_remove_address(link
, link
->dhcp_lease
, &address
, dhcp_remove_address_handler
);
104 if (!link
->network
->dhcp_send_decline
)
105 dhcp4_check_ready(link
);
111 static int route_scope_from_address(const Route
*route
, const struct in_addr
*self_addr
) {
115 if (in4_addr_is_localhost(&route
->dst
.in
) ||
116 (!in4_addr_is_null(self_addr
) && in4_addr_equal(&route
->dst
.in
, self_addr
)))
117 return RT_SCOPE_HOST
;
118 else if (in4_addr_is_null(&route
->gw
.in
))
119 return RT_SCOPE_LINK
;
121 return RT_SCOPE_UNIVERSE
;
124 static bool link_prefixroute(Link
*link
) {
125 return !link
->network
->dhcp_route_table_set
||
126 link
->network
->dhcp_route_table
== RT_TABLE_MAIN
||
127 link
->manager
->dhcp4_prefix_root_cannot_set_table
;
130 static int dhcp_route_configure(Route
**route
, Link
*link
) {
137 if (set_contains(link
->dhcp_routes
, *route
))
140 r
= route_configure(*route
, link
, dhcp4_route_handler
);
144 link
->dhcp4_messages
++;
146 r
= set_put(link
->dhcp_routes
, *route
);
154 static int link_set_dns_routes(Link
*link
, const struct in_addr
*address
) {
155 const struct in_addr
*dns
;
160 assert(link
->dhcp_lease
);
161 assert(link
->network
);
163 if (!link
->network
->dhcp_use_dns
||
164 !link
->network
->dhcp_routes_to_dns
)
167 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &dns
);
168 if (IN_SET(n
, 0, -ENODATA
))
171 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
173 table
= link_get_dhcp_route_table(link
);
175 for (i
= 0; i
< n
; i
++) {
176 _cleanup_(route_freep
) Route
*route
= NULL
;
178 r
= route_new(&route
);
180 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
182 /* Set routes to DNS servers. */
184 route
->family
= AF_INET
;
185 route
->dst
.in
= dns
[i
];
186 route
->dst_prefixlen
= 32;
187 route
->prefsrc
.in
= *address
;
188 route
->scope
= RT_SCOPE_LINK
;
189 route
->protocol
= RTPROT_DHCP
;
190 route
->priority
= link
->network
->dhcp_route_metric
;
191 route
->table
= table
;
193 r
= dhcp_route_configure(&route
, link
);
195 return log_link_error_errno(link
, r
, "Could not set route to DNS server: %m");
201 static int dhcp_prefix_route_from_lease(
202 const sd_dhcp_lease
*lease
,
204 const struct in_addr
*address
,
208 struct in_addr netmask
;
211 r
= sd_dhcp_lease_get_netmask((sd_dhcp_lease
*) lease
, &netmask
);
215 r
= route_new(&route
);
219 route
->family
= AF_INET
;
220 route
->dst
.in
.s_addr
= address
->s_addr
& netmask
.s_addr
;
221 route
->dst_prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
222 route
->prefsrc
.in
= *address
;
223 route
->scope
= RT_SCOPE_LINK
;
224 route
->protocol
= RTPROT_DHCP
;
225 route
->table
= table
;
230 static int link_set_dhcp_routes(Link
*link
) {
231 _cleanup_free_ sd_dhcp_route
**static_routes
= NULL
;
232 bool classless_route
= false, static_route
= false;
233 const struct in_addr
*router
;
234 struct in_addr address
;
240 if (!link
->dhcp_lease
) /* link went down while we configured the IP addresses? */
243 if (!link
->network
) /* link went down while we configured the IP addresses? */
246 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
247 /* During configuring addresses, the link lost its carrier. As networkd is dropping
248 * the addresses now, let's not configure the routes either. */
251 r
= set_ensure_allocated(&link
->dhcp_routes
, &route_hash_ops
);
255 /* Clear old entries in case the set was already allocated */
256 set_clear(link
->dhcp_routes
);
258 table
= link_get_dhcp_route_table(link
);
260 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
262 return log_link_warning_errno(link
, r
, "DHCP error: could not get address: %m");
264 if (!link_prefixroute(link
)) {
265 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
267 r
= dhcp_prefix_route_from_lease(link
->dhcp_lease
, table
, &address
, &prefix_route
);
269 return log_link_error_errno(link
, r
, "Could not create prefix route: %m");
271 r
= dhcp_route_configure(&prefix_route
, link
);
273 return log_link_error_errno(link
, r
, "Could not set prefix route: %m");
276 n
= sd_dhcp_lease_get_routes(link
->dhcp_lease
, &static_routes
);
278 log_link_debug_errno(link
, n
, "DHCP: No routes received from DHCP server: %m");
280 log_link_debug_errno(link
, n
, "DHCP: could not get routes: %m");
282 for (i
= 0; i
< n
; i
++) {
283 switch (sd_dhcp_route_get_option(static_routes
[i
])) {
284 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
285 classless_route
= true;
287 case SD_DHCP_OPTION_STATIC_ROUTE
:
293 if (link
->network
->dhcp_use_routes
) {
294 for (i
= 0; i
< n
; i
++) {
295 _cleanup_(route_freep
) Route
*route
= NULL
;
297 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
298 the DHCP client MUST ignore the Static Routes option. */
299 if (classless_route
&&
300 sd_dhcp_route_get_option(static_routes
[i
]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
)
303 r
= route_new(&route
);
305 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
307 route
->family
= AF_INET
;
308 route
->protocol
= RTPROT_DHCP
;
309 assert_se(sd_dhcp_route_get_gateway(static_routes
[i
], &route
->gw
.in
) >= 0);
310 assert_se(sd_dhcp_route_get_destination(static_routes
[i
], &route
->dst
.in
) >= 0);
311 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &route
->dst_prefixlen
) >= 0);
312 route
->priority
= link
->network
->dhcp_route_metric
;
313 route
->table
= table
;
314 route
->mtu
= link
->network
->dhcp_route_mtu
;
315 route
->scope
= route_scope_from_address(route
, &address
);
316 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
317 route
->prefsrc
.in
= address
;
319 if (set_contains(link
->dhcp_routes
, route
))
322 r
= dhcp_route_configure(&route
, link
);
324 return log_link_error_errno(link
, r
, "Could not set route: %m");
328 if (link
->network
->dhcp_use_gateway
) {
329 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
330 if (IN_SET(r
, 0, -ENODATA
))
331 log_link_info(link
, "DHCP: No gateway received from DHCP server.");
333 log_link_warning_errno(link
, r
, "DHCP error: could not get gateway: %m");
334 else if (in4_addr_is_null(&router
[0]))
335 log_link_info(link
, "DHCP: Received gateway is null.");
337 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
338 a Router option, the DHCP client MUST ignore the Router option. */
339 if (classless_route
&& static_route
)
340 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring static-route option and router option");
342 if (r
> 0 && !classless_route
&& !in4_addr_is_null(&router
[0])) {
343 _cleanup_(route_freep
) Route
*route
= NULL
, *route_gw
= NULL
;
345 r
= route_new(&route_gw
);
347 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
349 /* The dhcp netmask may mask out the gateway. Add an explicit
350 * route for the gw host so that we can route no matter the
351 * netmask or existing kernel route tables. */
352 route_gw
->family
= AF_INET
;
353 route_gw
->dst
.in
= router
[0];
354 route_gw
->dst_prefixlen
= 32;
355 route_gw
->prefsrc
.in
= address
;
356 route_gw
->scope
= RT_SCOPE_LINK
;
357 route_gw
->protocol
= RTPROT_DHCP
;
358 route_gw
->priority
= link
->network
->dhcp_route_metric
;
359 route_gw
->table
= table
;
360 route_gw
->mtu
= link
->network
->dhcp_route_mtu
;
362 r
= dhcp_route_configure(&route_gw
, link
);
364 return log_link_error_errno(link
, r
, "Could not set host route: %m");
366 r
= route_new(&route
);
368 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
370 route
->family
= AF_INET
;
371 route
->gw
.in
= router
[0];
372 route
->prefsrc
.in
= address
;
373 route
->protocol
= RTPROT_DHCP
;
374 route
->priority
= link
->network
->dhcp_route_metric
;
375 route
->table
= table
;
376 route
->mtu
= link
->network
->dhcp_route_mtu
;
378 r
= dhcp_route_configure(&route
, link
);
380 return log_link_error_errno(link
, r
, "Could not set router: %m");
384 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
385 if (!rt
->gateway_from_dhcp
)
388 if (rt
->family
!= AF_INET
)
391 rt
->gw
.in
= router
[0];
393 r
= route_configure(rt
, link
, dhcp4_route_handler
);
395 return log_link_error_errno(link
, r
, "Could not set gateway: %m");
397 link
->dhcp4_messages
++;
401 return link_set_dns_routes(link
, &address
);
404 static int dhcp_remove_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
405 _cleanup_free_ sd_dhcp_route
**routes
= NULL
;
412 if (!link
->network
->dhcp_use_routes
)
415 n
= sd_dhcp_lease_get_routes(lease
, &routes
);
416 if (IN_SET(n
, 0, -ENODATA
))
419 return log_link_error_errno(link
, n
, "DHCP error: Failed to get routes: %m");
421 table
= link_get_dhcp_route_table(link
);
423 for (i
= 0; i
< n
; i
++) {
424 _cleanup_(route_freep
) Route
*route
= NULL
;
426 r
= route_new(&route
);
430 route
->family
= AF_INET
;
431 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &route
->gw
.in
) >= 0);
432 assert_se(sd_dhcp_route_get_destination(routes
[i
], &route
->dst
.in
) >= 0);
433 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &route
->dst_prefixlen
) >= 0);
434 route
->priority
= link
->network
->dhcp_route_metric
;
435 route
->table
= table
;
436 route
->scope
= route_scope_from_address(route
, address
);
437 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
438 route
->prefsrc
.in
= *address
;
440 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
443 (void) route_remove(route
, link
, NULL
);
449 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
450 _cleanup_(route_freep
) Route
*route_gw
= NULL
, *route
= NULL
;
451 const struct in_addr
*router
;
458 if (!link
->network
->dhcp_use_gateway
)
461 r
= sd_dhcp_lease_get_router(lease
, &router
);
462 if (IN_SET(r
, 0, -ENODATA
)) {
463 log_link_debug(link
, "DHCP: No gateway received from DHCP server.");
466 return log_link_error_errno(link
, r
, "DHCP error: could not get gateway: %m");
467 else if (in4_addr_is_null(&router
[0])) {
468 log_link_info(link
, "DHCP: Received gateway is null, ignoring.");
472 table
= link_get_dhcp_route_table(link
);
474 r
= route_new(&route_gw
);
478 route_gw
->family
= AF_INET
;
479 route_gw
->dst
.in
= router
[0];
480 route_gw
->dst_prefixlen
= 32;
481 route_gw
->prefsrc
.in
= *address
;
482 route_gw
->scope
= RT_SCOPE_LINK
;
483 route_gw
->protocol
= RTPROT_DHCP
;
484 route_gw
->priority
= link
->network
->dhcp_route_metric
;
485 route_gw
->table
= table
;
487 if (remove_all
|| !set_contains(link
->dhcp_routes
, route_gw
))
488 (void) route_remove(route_gw
, link
, NULL
);
490 r
= route_new(&route
);
494 route
->family
= AF_INET
;
495 route
->gw
.in
= router
[0];
496 route
->prefsrc
.in
= *address
;
497 route
->protocol
= RTPROT_DHCP
;
498 route
->priority
= link
->network
->dhcp_route_metric
;
499 route
->table
= table
;
501 if (remove_all
|| !set_contains(link
->dhcp_routes
, route
))
502 (void) route_remove(route
, link
, NULL
);
505 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
506 if (!rt
->gateway_from_dhcp
)
509 if (rt
->family
!= AF_INET
)
512 if (!remove_all
&& in4_addr_equal(router
, &rt
->gw
.in
))
515 (void) route_remove(rt
, link
, NULL
);
521 static int dhcp_remove_dns_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
522 const struct in_addr
*dns
;
528 assert(link
->network
);
530 if (!link
->network
->dhcp_use_dns
||
531 !link
->network
->dhcp_routes_to_dns
)
534 n
= sd_dhcp_lease_get_dns(lease
, &dns
);
535 if (IN_SET(n
, 0, -ENODATA
))
538 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
540 table
= link_get_dhcp_route_table(link
);
542 for (i
= 0; i
< n
; i
++) {
543 _cleanup_(route_freep
) Route
*route
= NULL
;
545 r
= route_new(&route
);
547 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
549 route
->family
= AF_INET
;
550 route
->dst
.in
= dns
[i
];
551 route
->dst_prefixlen
= 32;
552 route
->prefsrc
.in
= *address
;
553 route
->scope
= RT_SCOPE_LINK
;
554 route
->protocol
= RTPROT_DHCP
;
555 route
->priority
= link
->network
->dhcp_route_metric
;
556 route
->table
= table
;
558 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
561 (void) route_remove(route
, link
, NULL
);
564 if (!link_prefixroute(link
)) {
565 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
567 r
= dhcp_prefix_route_from_lease(lease
, table
, address
, &prefix_route
);
569 return log_link_warning_errno(link
, r
, "Could not delete prefix route: %m");
571 if (remove_all
|| !set_contains(link
->dhcp_routes
, prefix_route
))
572 (void) route_remove(prefix_route
, link
, NULL
);
578 static int dhcp_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
583 /* This is only used when retrying to assign the address received from DHCPv4 server.
584 * See dhcp4_route_handler(). */
586 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
589 r
= sd_netlink_message_get_errno(m
);
591 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 address, ignoring");
593 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
595 (void) dhcp_lease_renew(link
->dhcp_client
, link
);
599 static int dhcp_remove_address(
600 Link
*link
, sd_dhcp_lease
*lease
,
601 const struct in_addr
*address
,
602 link_netlink_message_handler_t callback
) {
604 _cleanup_(address_freep
) Address
*a
= NULL
;
605 struct in_addr netmask
;
611 if (in4_addr_is_null(address
))
619 a
->in_addr
.in
= *address
;
621 if (sd_dhcp_lease_get_netmask(lease
, &netmask
) >= 0)
622 a
->prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
624 (void) address_remove(a
, link
, callback
);
629 static int dhcp_reset_mtu(Link
*link
) {
635 if (!link
->network
->dhcp_use_mtu
)
638 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
642 if (link
->original_mtu
== mtu
)
645 r
= link_set_mtu(link
, link
->original_mtu
);
647 log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
648 link_enter_failed(link
);
655 static int dhcp_reset_hostname(Link
*link
) {
656 const char *hostname
;
661 if (!link
->network
->dhcp_use_hostname
)
664 hostname
= link
->network
->dhcp_hostname
;
666 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
671 /* If a hostname was set due to the lease, then unset it now. */
672 r
= manager_set_hostname(link
->manager
, NULL
);
674 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
679 static int dhcp_lease_lost(Link
*link
) {
680 struct in_addr address
= {};
683 assert(link
->dhcp_lease
);
685 log_link_info(link
, "DHCP lease lost");
687 link
->dhcp4_configured
= false;
689 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
690 (void) dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true);
691 (void) dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true);
692 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true);
693 (void) dhcp_remove_address(link
, link
->dhcp_lease
, &address
, NULL
);
694 (void) dhcp_reset_mtu(link
);
695 (void) dhcp_reset_hostname(link
);
697 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
703 static void dhcp_address_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
704 _cleanup_free_
char *pretty
= NULL
;
705 union in_addr_union address
= {};
715 case SD_IPV4ACD_EVENT_STOP
:
716 log_link_debug(link
, "Stopping ACD client for DHCP4...");
719 case SD_IPV4ACD_EVENT_BIND
:
721 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
722 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
723 log_link_debug(link
, "Successfully claimed DHCP4 address %s", strna(pretty
));
725 dhcp4_check_ready(link
);
728 case SD_IPV4ACD_EVENT_CONFLICT
:
729 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
730 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
731 log_link_warning(link
, "DAD conflict. Dropping DHCP4 address %s", strna(pretty
));
733 (void) sd_dhcp_client_send_decline(link
->dhcp_client
);
735 if (link
->dhcp_lease
) {
736 r
= dhcp_lease_lost(link
);
738 link_enter_failed(link
);
743 assert_not_reached("Invalid IPv4ACD event.");
746 sd_ipv4acd_stop(acd
);
751 static int configure_dhcpv4_duplicate_address_detection(Link
*link
) {
756 r
= sd_ipv4acd_new(&link
->network
->dhcp_acd
);
760 r
= sd_ipv4acd_attach_event(link
->network
->dhcp_acd
, NULL
, 0);
764 r
= sd_ipv4acd_set_ifindex(link
->network
->dhcp_acd
, link
->ifindex
);
768 r
= sd_ipv4acd_set_mac(link
->network
->dhcp_acd
, &link
->mac
);
775 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
780 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
783 r
= sd_netlink_message_get_errno(m
);
784 if (r
< 0 && r
!= -EEXIST
) {
785 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 address");
786 link_enter_failed(link
);
789 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
791 r
= link_set_dhcp_routes(link
);
793 link_enter_failed(link
);
797 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
798 r
= link_request_set_routes(link
);
800 link_enter_failed(link
);
804 if (link
->network
->dhcp_send_decline
) {
805 union in_addr_union addr
;
807 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &addr
.in
);
809 r
= sd_ipv4acd_set_address(link
->network
->dhcp_acd
, &addr
.in
);
813 r
= sd_ipv4acd_set_callback(link
->network
->dhcp_acd
, dhcp_address_on_acd
, link
);
818 _cleanup_free_
char *pretty
= NULL
;
820 (void) in_addr_to_string(AF_INET
, &addr
, &pretty
);
821 log_link_debug(link
, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty
));
824 r
= sd_ipv4acd_start(link
->network
->dhcp_acd
, true);
826 log_link_warning_errno(link
, r
, "Failed to start IPv4ACD client, ignoring: %m");
828 dhcp4_check_ready(link
);
833 static int dhcp4_update_address(Link
*link
,
834 struct in_addr
*address
,
835 struct in_addr
*netmask
,
837 _cleanup_(address_freep
) Address
*addr
= NULL
;
845 /* address_handler calls link_request_set_routes() and link_request_set_nexthop(). Before they
846 * are called, the related flags must be cleared. Otherwise, the link becomes configured state
847 * before routes are configured. */
848 link
->static_routes_configured
= false;
849 link
->static_nexthops_configured
= false;
851 prefixlen
= in4_addr_netmask_to_prefixlen(netmask
);
853 r
= address_new(&addr
);
857 addr
->family
= AF_INET
;
858 addr
->in_addr
.in
.s_addr
= address
->s_addr
;
859 addr
->cinfo
.ifa_prefered
= lifetime
;
860 addr
->cinfo
.ifa_valid
= lifetime
;
861 addr
->prefixlen
= prefixlen
;
862 addr
->broadcast
.s_addr
= address
->s_addr
| ~netmask
->s_addr
;
863 addr
->prefix_route
= link_prefixroute(link
);
865 /* allow reusing an existing address and simply update its lifetime
866 * in case it already exists */
867 r
= address_configure(addr
, link
, dhcp4_address_handler
, true);
874 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
875 sd_dhcp_lease
*lease
;
876 struct in_addr address
;
877 struct in_addr netmask
;
878 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
883 assert(link
->network
);
885 r
= sd_dhcp_client_get_lease(client
, &lease
);
887 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
889 sd_dhcp_lease_unref(link
->dhcp_lease
);
890 link
->dhcp4_configured
= false;
891 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
894 r
= sd_dhcp_lease_get_address(lease
, &address
);
896 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
898 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
900 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
902 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
903 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
905 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
908 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
910 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
915 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
916 const struct in_addr
*router
;
917 sd_dhcp_lease
*lease
;
918 struct in_addr address
;
919 struct in_addr netmask
;
921 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
927 link
->dhcp4_configured
= false;
929 r
= sd_dhcp_client_get_lease(client
, &lease
);
931 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
933 r
= sd_dhcp_lease_get_address(lease
, &address
);
935 return log_link_error_errno(link
, r
, "DHCP error: No address: %m");
937 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
939 return log_link_error_errno(link
, r
, "DHCP error: No netmask: %m");
941 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
943 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
944 r
= sd_dhcp_lease_get_lifetime(lease
, &lifetime
);
946 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
949 r
= sd_dhcp_lease_get_router(lease
, &router
);
950 if (r
< 0 && r
!= -ENODATA
)
951 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
953 if (r
> 0 && !in4_addr_is_null(&router
[0]))
955 LOG_LINK_INTERFACE(link
),
956 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
957 ADDRESS_FMT_VAL(address
),
959 ADDRESS_FMT_VAL(router
[0])),
960 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
961 "PREFIXLEN=%u", prefixlen
,
962 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router
[0]));
965 LOG_LINK_INTERFACE(link
),
966 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u",
967 ADDRESS_FMT_VAL(address
),
969 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
970 "PREFIXLEN=%u", prefixlen
);
972 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
975 if (link
->network
->dhcp_use_mtu
) {
978 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
980 r
= link_set_mtu(link
, mtu
);
982 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
986 if (link
->network
->dhcp_use_hostname
) {
987 const char *dhcpname
= NULL
;
988 _cleanup_free_
char *hostname
= NULL
;
990 if (link
->network
->dhcp_hostname
)
991 dhcpname
= link
->network
->dhcp_hostname
;
993 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
996 r
= shorten_overlong(dhcpname
, &hostname
);
998 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
1000 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
1004 r
= manager_set_hostname(link
->manager
, hostname
);
1006 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
1010 if (link
->network
->dhcp_use_timezone
) {
1011 const char *tz
= NULL
;
1013 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
1016 r
= manager_set_timezone(link
->manager
, tz
);
1018 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
1022 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
1024 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
1029 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
1032 link
->dhcp_lease_old
= TAKE_PTR(link
->dhcp_lease
);
1034 /* On IP address change, to keep the connectability, we would like to assign new address and
1035 * routes, and then release old lease. There are two possible success paths:
1037 * 1. new address and routes are configured.
1038 * -> handled by dhcp_release_old_lease() in dhcp4_route_handler().
1039 * 2. new address is configured and no route is requested.
1040 * -> handled by dhcp_release_old_lease() in dhcp4_address_handler().
1042 * On error in assigning new address and routes, then the link always enters to the failed
1043 * state. And link_enter_failed() leads to the DHCP client to be stopped. So,
1044 * dhcp_release_old_lease() will be also called by link_stop_clients().
1047 r
= dhcp_lease_acquired(client
, link
);
1049 /* If it fails, then the new address is not configured yet.
1050 * So, let's simply drop the old lease. */
1051 sd_dhcp_lease_unref(link
->dhcp_lease
);
1052 link
->dhcp_lease
= TAKE_PTR(link
->dhcp_lease_old
);
1053 (void) dhcp_lease_lost(link
);
1060 static int dhcp_server_is_deny_listed(Link
*link
, sd_dhcp_client
*client
) {
1061 sd_dhcp_lease
*lease
;
1062 struct in_addr addr
;
1066 assert(link
->network
);
1069 r
= sd_dhcp_client_get_lease(client
, &lease
);
1071 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1073 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1075 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1077 if (set_contains(link
->network
->dhcp_deny_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1078 log_struct(LOG_DEBUG
,
1079 LOG_LINK_INTERFACE(link
),
1080 LOG_LINK_MESSAGE(link
, "DHCPv4 IP '%u.%u.%u.%u' found in deny-listed IP addresses, ignoring offer",
1081 ADDRESS_FMT_VAL(addr
)));
1088 static int dhcp_server_is_allow_listed(Link
*link
, sd_dhcp_client
*client
) {
1089 sd_dhcp_lease
*lease
;
1090 struct in_addr addr
;
1094 assert(link
->network
);
1097 r
= sd_dhcp_client_get_lease(client
, &lease
);
1099 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1101 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1103 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1105 if (set_contains(link
->network
->dhcp_allow_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1106 log_struct(LOG_DEBUG
,
1107 LOG_LINK_INTERFACE(link
),
1108 LOG_LINK_MESSAGE(link
, "DHCPv4 IP '%u.%u.%u.%u' found in allow-listed IP addresses, accepting offer",
1109 ADDRESS_FMT_VAL(addr
)));
1116 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
1117 Link
*link
= userdata
;
1121 assert(link
->network
);
1122 assert(link
->manager
);
1124 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1128 case SD_DHCP_CLIENT_EVENT_STOP
:
1130 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
)) {
1131 assert(link
->ipv4ll
);
1133 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
1135 r
= sd_ipv4ll_start(link
->ipv4ll
);
1137 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1140 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1141 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1145 if (link
->dhcp_lease
) {
1146 if (link
->network
->dhcp_send_release
)
1147 (void) sd_dhcp_client_send_release(client
);
1149 r
= dhcp_lease_lost(link
);
1151 link_enter_failed(link
);
1157 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
1158 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1159 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1163 if (link
->dhcp_lease
) {
1164 r
= dhcp_lease_lost(link
);
1166 link_enter_failed(link
);
1172 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
1173 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1174 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1178 r
= dhcp_lease_ip_change(client
, link
);
1180 link_enter_failed(link
);
1185 case SD_DHCP_CLIENT_EVENT_RENEW
:
1186 r
= dhcp_lease_renew(client
, link
);
1188 link_enter_failed(link
);
1192 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1193 r
= dhcp_lease_acquired(client
, link
);
1195 link_enter_failed(link
);
1199 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1200 if (!set_isempty(link
->network
->dhcp_allow_listed_ip
)) {
1201 r
= dhcp_server_is_allow_listed(link
, client
);
1207 r
= dhcp_server_is_deny_listed(link
, client
);
1216 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1218 log_link_warning(link
, "DHCP unknown event: %i", event
);
1225 static int dhcp4_set_hostname(Link
*link
) {
1226 _cleanup_free_
char *hostname
= NULL
;
1232 if (!link
->network
->dhcp_send_hostname
)
1234 else if (link
->network
->dhcp_hostname
)
1235 hn
= link
->network
->dhcp_hostname
;
1237 r
= gethostname_strict(&hostname
);
1238 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1244 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1245 if (r
== -EINVAL
&& hostname
)
1246 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1247 log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1249 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
1254 static bool promote_secondaries_enabled(const char *ifname
) {
1255 _cleanup_free_
char *promote_secondaries_sysctl
= NULL
;
1256 char *promote_secondaries_path
;
1259 promote_secondaries_path
= strjoina("net/ipv4/conf/", ifname
, "/promote_secondaries");
1260 r
= sysctl_read(promote_secondaries_path
, &promote_secondaries_sysctl
);
1262 log_debug_errno(r
, "Cannot read sysctl %s", promote_secondaries_path
);
1266 truncate_nl(promote_secondaries_sysctl
);
1267 r
= parse_boolean(promote_secondaries_sysctl
);
1269 log_warning_errno(r
, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path
, promote_secondaries_sysctl
);
1273 /* dhcp4_set_promote_secondaries will ensure this interface has
1274 * the "promote_secondaries" option in the kernel set. If this sysctl
1275 * is not set DHCP will work only as long as the IP address does not
1276 * changes between leases. The kernel will remove all secondary IP
1277 * addresses of an interface otherwise. The way systemd-network works
1278 * is that the new IP of a lease is added as a secondary IP and when
1279 * the primary one expires it relies on the kernel to promote the
1280 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1282 int dhcp4_set_promote_secondaries(Link
*link
) {
1286 assert(link
->network
);
1287 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1289 /* check if the kernel has promote_secondaries enabled for our
1290 * interface. If it is not globally enabled or enabled for the
1291 * specific interface we must either enable it.
1293 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link
->ifname
))) {
1294 char *promote_secondaries_path
= NULL
;
1296 log_link_debug(link
, "promote_secondaries is unset, setting it");
1297 promote_secondaries_path
= strjoina("net/ipv4/conf/", link
->ifname
, "/promote_secondaries");
1298 r
= sysctl_write(promote_secondaries_path
, "1");
1300 log_link_warning_errno(link
, r
, "cannot set sysctl %s to 1", promote_secondaries_path
);
1307 int dhcp4_set_client_identifier(Link
*link
) {
1311 assert(link
->network
);
1312 assert(link
->dhcp_client
);
1314 switch (link
->network
->dhcp_client_identifier
) {
1315 case DHCP_CLIENT_ID_DUID
: {
1316 /* If configured, apply user specified DUID and IAID */
1317 const DUID
*duid
= link_get_duid(link
);
1319 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1320 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1321 link
->network
->iaid_set
,
1322 link
->network
->iaid
,
1325 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
1326 link
->network
->iaid_set
,
1327 link
->network
->iaid
,
1329 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1330 duid
->raw_data_len
);
1332 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1335 case DHCP_CLIENT_ID_DUID_ONLY
: {
1336 /* If configured, apply user specified DUID */
1337 const DUID
*duid
= link_get_duid(link
);
1339 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1340 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
1343 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
1345 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1346 duid
->raw_data_len
);
1348 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
1351 case DHCP_CLIENT_ID_MAC
:
1352 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1354 (const uint8_t *) &link
->mac
,
1357 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
1360 assert_not_reached("Unknown client identifier type.");
1366 int dhcp4_configure(Link
*link
) {
1367 sd_dhcp_option
*send_option
;
1368 void *request_options
;
1373 assert(link
->network
);
1374 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1376 if (!link
->dhcp_client
) {
1377 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1381 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
1383 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
1385 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to attach event: %m");
1388 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1389 (const uint8_t *) &link
->mac
,
1390 sizeof (link
->mac
), ARPHRD_ETHER
);
1392 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1394 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1396 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1398 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1400 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1402 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
,
1403 link
->network
->dhcp_broadcast
);
1405 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1408 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1410 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1413 if (link
->network
->dhcp_use_mtu
) {
1414 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1415 SD_DHCP_OPTION_INTERFACE_MTU
);
1417 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1420 /* NOTE: even if this variable is called "use", it also "sends" PRL
1421 * options, maybe there should be a different configuration variable
1422 * to send or not route options?. */
1423 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1424 * by default, so they don't need to be added here. */
1425 if (link
->network
->dhcp_use_routes
&& !link
->network
->dhcp_anonymize
) {
1426 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1427 SD_DHCP_OPTION_STATIC_ROUTE
);
1429 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1431 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1432 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1434 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1437 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
&& !link
->network
->dhcp_anonymize
) {
1438 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
);
1440 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1443 if (link
->network
->dhcp_use_ntp
) {
1444 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1446 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1449 if (link
->network
->dhcp_use_sip
) {
1450 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_SIP_SERVER
);
1452 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1455 if (link
->network
->dhcp_use_timezone
) {
1456 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1458 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1461 SET_FOREACH(request_options
, link
->network
->dhcp_request_options
, i
) {
1462 uint32_t option
= PTR_TO_UINT32(request_options
);
1464 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, option
);
1466 log_link_debug(link
, "DHCP4 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option
);
1471 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option
);
1474 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_options
, i
) {
1475 r
= sd_dhcp_client_add_option(link
->dhcp_client
, send_option
);
1479 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1482 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_vendor_options
, i
) {
1483 r
= sd_dhcp_client_add_vendor_option(link
->dhcp_client
, send_option
);
1487 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1490 r
= dhcp4_set_hostname(link
);
1494 if (link
->network
->dhcp_vendor_class_identifier
) {
1495 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1496 link
->network
->dhcp_vendor_class_identifier
);
1498 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1501 if (link
->network
->dhcp_mudurl
) {
1502 r
= sd_dhcp_client_set_mud_url(link
->dhcp_client
,
1503 link
->network
->dhcp_mudurl
);
1505 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1508 if (link
->network
->dhcp_user_class
) {
1509 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, (const char **) link
->network
->dhcp_user_class
);
1511 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1514 if (link
->network
->dhcp_client_port
) {
1515 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1517 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen port: %m");
1520 if (link
->network
->dhcp_max_attempts
> 0) {
1521 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1523 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1526 if (link
->network
->ip_service_type
> 0) {
1527 r
= sd_dhcp_client_set_service_type(link
->dhcp_client
, link
->network
->ip_service_type
);
1529 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IP service type: %m");
1532 if (link
->network
->dhcp_fallback_lease_lifetime
> 0) {
1533 r
= sd_dhcp_client_set_fallback_lease_lifetime(link
->dhcp_client
, link
->network
->dhcp_fallback_lease_lifetime
);
1535 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1538 if (link
->network
->dhcp_send_decline
) {
1539 r
= configure_dhcpv4_duplicate_address_detection(link
);
1541 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to configure service type: %m");
1544 return dhcp4_set_client_identifier(link
);
1547 int config_parse_dhcp_max_attempts(
1549 const char *filename
,
1551 const char *section
,
1552 unsigned section_line
,
1559 Network
*network
= data
;
1567 if (isempty(rvalue
)) {
1568 network
->dhcp_max_attempts
= 0;
1572 if (streq(rvalue
, "infinity")) {
1573 network
->dhcp_max_attempts
= (uint64_t) -1;
1577 r
= safe_atou64(rvalue
, &a
);
1579 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1580 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1585 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1586 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1590 network
->dhcp_max_attempts
= a
;
1595 int config_parse_dhcp_acl_ip_address(
1597 const char *filename
,
1599 const char *section
,
1600 unsigned section_line
,
1607 Network
*network
= data
;
1616 acl
= STR_IN_SET(lvalue
, "DenyList", "BlackList") ? &network
->dhcp_deny_listed_ip
: &network
->dhcp_allow_listed_ip
;
1618 if (isempty(rvalue
)) {
1619 *acl
= set_free(*acl
);
1623 for (const char *p
= rvalue
;;) {
1624 _cleanup_free_
char *n
= NULL
;
1625 union in_addr_union ip
;
1627 r
= extract_first_word(&p
, &n
, NULL
, 0);
1629 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1630 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1637 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1639 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1640 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue
, n
);
1644 r
= set_ensure_put(acl
, NULL
, UINT32_TO_PTR(ip
.in
.s_addr
));
1646 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1647 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue
, n
);
1653 int config_parse_dhcp_ip_service_type(
1655 const char *filename
,
1657 const char *section
,
1658 unsigned section_line
,
1669 if (streq(rvalue
, "CS4"))
1670 *((int *)data
) = IPTOS_CLASS_CS4
;
1671 else if (streq(rvalue
, "CS6"))
1672 *((int *)data
) = IPTOS_CLASS_CS6
;
1674 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1675 "Failed to parse IPServiceType type '%s', ignoring.", rvalue
);
1680 int config_parse_dhcp_mud_url(
1682 const char *filename
,
1684 const char *section
,
1685 unsigned section_line
,
1692 _cleanup_free_
char *unescaped
= NULL
;
1693 Network
*network
= data
;
1700 if (isempty(rvalue
)) {
1701 network
->dhcp_mudurl
= mfree(network
->dhcp_mudurl
);
1705 r
= cunescape(rvalue
, 0, &unescaped
);
1707 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1708 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue
);
1712 if (!http_url_is_valid(unescaped
) || strlen(unescaped
) > 255) {
1713 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1714 "Failed to parse MUD URL '%s', ignoring: %m", rvalue
);
1719 return free_and_strdup_warn(&network
->dhcp_mudurl
, unescaped
);
1722 int config_parse_dhcp_fallback_lease_lifetime(const char *unit
,
1723 const char *filename
,
1725 const char *section
,
1726 unsigned section_line
,
1732 Network
*network
= userdata
;
1741 if (isempty(rvalue
)) {
1742 network
->dhcp_fallback_lease_lifetime
= 0;
1746 /* We accept only "forever" or "infinity". */
1747 if (STR_IN_SET(rvalue
, "forever", "infinity"))
1748 k
= CACHE_INFO_INFINITY_LIFE_TIME
;
1750 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1751 "Invalid LeaseLifetime= value, ignoring: %s", rvalue
);
1755 network
->dhcp_fallback_lease_lifetime
= k
;
1760 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1761 [DHCP_CLIENT_ID_MAC
] = "mac",
1762 [DHCP_CLIENT_ID_DUID
] = "duid",
1763 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1766 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1767 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1768 "Failed to parse client identifier type");