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
, link_netlink_message_handler_t callback
);
24 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
, link_netlink_message_handler_t callback
);
25 static int dhcp_remove_dns_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
, link_netlink_message_handler_t callback
);
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 dhcp4_update_address(Link
*link
, bool announce
);
28 static int dhcp4_remove_all(Link
*link
);
30 static 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, NULL
);
44 (void) dhcp_remove_router(link
, link
->dhcp_lease_old
, &address_old
, false, NULL
);
45 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease_old
, &address_old
, false, NULL
);
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
->network
->dhcp_send_decline
&& !link
->dhcp4_address_bind
)
58 if (link
->dhcp4_messages
> 0)
61 link
->dhcp4_configured
= true;
62 /* New address and routes are configured now. Let's release old lease. */
63 dhcp4_release_old_lease(link
);
64 link_check_ready(link
);
67 static int dhcp4_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
71 assert(link
->dhcp4_messages
> 0);
73 link
->dhcp4_messages
--;
75 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
78 r
= sd_netlink_message_get_errno(m
);
79 if (r
== -ENETUNREACH
&& !link
->dhcp4_route_retrying
) {
81 /* It seems kernel does not support that the prefix route cannot be configured with
82 * route table. Let's once drop the config and reconfigure them later. */
84 log_link_message_debug_errno(link
, m
, r
, "Could not set DHCPv4 route, retrying later");
85 link
->dhcp4_route_failed
= true;
86 link
->manager
->dhcp4_prefix_root_cannot_set_table
= true;
87 } else if (r
< 0 && r
!= -EEXIST
) {
88 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 route");
89 link_enter_failed(link
);
93 if (link
->dhcp4_messages
== 0 && link
->dhcp4_route_failed
) {
94 link
->dhcp4_route_failed
= false;
95 link
->dhcp4_route_retrying
= true;
97 r
= dhcp4_remove_all(link
);
99 link_enter_failed(link
);
103 dhcp4_check_ready(link
);
108 static int route_scope_from_address(const Route
*route
, const struct in_addr
*self_addr
) {
112 if (in4_addr_is_localhost(&route
->dst
.in
) ||
113 (!in4_addr_is_null(self_addr
) && in4_addr_equal(&route
->dst
.in
, self_addr
)))
114 return RT_SCOPE_HOST
;
115 else if (in4_addr_is_null(&route
->gw
.in
))
116 return RT_SCOPE_LINK
;
118 return RT_SCOPE_UNIVERSE
;
121 static bool link_prefixroute(Link
*link
) {
122 return !link
->network
->dhcp_route_table_set
||
123 link
->network
->dhcp_route_table
== RT_TABLE_MAIN
||
124 link
->manager
->dhcp4_prefix_root_cannot_set_table
;
127 static int dhcp_route_configure(Route
**route
, Link
*link
) {
134 if (set_contains(link
->dhcp_routes
, *route
))
137 r
= route_configure(*route
, link
, dhcp4_route_handler
);
141 link
->dhcp4_messages
++;
143 r
= set_put(link
->dhcp_routes
, *route
);
151 static int link_set_dns_routes(Link
*link
, const struct in_addr
*address
) {
152 const struct in_addr
*dns
;
157 assert(link
->dhcp_lease
);
158 assert(link
->network
);
160 if (!link
->network
->dhcp_use_dns
||
161 !link
->network
->dhcp_routes_to_dns
)
164 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &dns
);
165 if (IN_SET(n
, 0, -ENODATA
))
168 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
170 table
= link_get_dhcp_route_table(link
);
172 for (i
= 0; i
< n
; i
++) {
173 _cleanup_(route_freep
) Route
*route
= NULL
;
175 r
= route_new(&route
);
177 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
179 /* Set routes to DNS servers. */
181 route
->family
= AF_INET
;
182 route
->dst
.in
= dns
[i
];
183 route
->dst_prefixlen
= 32;
184 route
->prefsrc
.in
= *address
;
185 route
->scope
= RT_SCOPE_LINK
;
186 route
->protocol
= RTPROT_DHCP
;
187 route
->priority
= link
->network
->dhcp_route_metric
;
188 route
->table
= table
;
190 r
= dhcp_route_configure(&route
, link
);
192 return log_link_error_errno(link
, r
, "Could not set route to DNS server: %m");
198 static int dhcp_prefix_route_from_lease(
199 const sd_dhcp_lease
*lease
,
201 const struct in_addr
*address
,
205 struct in_addr netmask
;
208 r
= sd_dhcp_lease_get_netmask((sd_dhcp_lease
*) lease
, &netmask
);
212 r
= route_new(&route
);
216 route
->family
= AF_INET
;
217 route
->dst
.in
.s_addr
= address
->s_addr
& netmask
.s_addr
;
218 route
->dst_prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
219 route
->prefsrc
.in
= *address
;
220 route
->scope
= RT_SCOPE_LINK
;
221 route
->protocol
= RTPROT_DHCP
;
222 route
->table
= table
;
227 static int link_set_dhcp_routes(Link
*link
) {
228 _cleanup_free_ sd_dhcp_route
**static_routes
= NULL
;
229 bool classless_route
= false, static_route
= false;
230 const struct in_addr
*router
;
231 struct in_addr address
;
237 if (!link
->dhcp_lease
) /* link went down while we configured the IP addresses? */
240 if (!link
->network
) /* link went down while we configured the IP addresses? */
243 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
244 /* During configuring addresses, the link lost its carrier. As networkd is dropping
245 * the addresses now, let's not configure the routes either. */
248 r
= set_ensure_allocated(&link
->dhcp_routes
, &route_hash_ops
);
252 /* Clear old entries in case the set was already allocated */
253 set_clear(link
->dhcp_routes
);
255 table
= link_get_dhcp_route_table(link
);
257 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
259 return log_link_warning_errno(link
, r
, "DHCP error: could not get address: %m");
261 if (!link_prefixroute(link
)) {
262 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
264 r
= dhcp_prefix_route_from_lease(link
->dhcp_lease
, table
, &address
, &prefix_route
);
266 return log_link_error_errno(link
, r
, "Could not create prefix route: %m");
268 r
= dhcp_route_configure(&prefix_route
, link
);
270 return log_link_error_errno(link
, r
, "Could not set prefix route: %m");
273 n
= sd_dhcp_lease_get_routes(link
->dhcp_lease
, &static_routes
);
275 log_link_debug_errno(link
, n
, "DHCP: No routes received from DHCP server: %m");
277 log_link_debug_errno(link
, n
, "DHCP: could not get routes: %m");
279 for (i
= 0; i
< n
; i
++) {
280 switch (sd_dhcp_route_get_option(static_routes
[i
])) {
281 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
282 classless_route
= true;
284 case SD_DHCP_OPTION_STATIC_ROUTE
:
290 if (link
->network
->dhcp_use_routes
) {
291 for (i
= 0; i
< n
; i
++) {
292 _cleanup_(route_freep
) Route
*route
= NULL
;
294 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
295 the DHCP client MUST ignore the Static Routes option. */
296 if (classless_route
&&
297 sd_dhcp_route_get_option(static_routes
[i
]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
)
300 r
= route_new(&route
);
302 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
304 route
->family
= AF_INET
;
305 route
->protocol
= RTPROT_DHCP
;
306 assert_se(sd_dhcp_route_get_gateway(static_routes
[i
], &route
->gw
.in
) >= 0);
307 assert_se(sd_dhcp_route_get_destination(static_routes
[i
], &route
->dst
.in
) >= 0);
308 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &route
->dst_prefixlen
) >= 0);
309 route
->priority
= link
->network
->dhcp_route_metric
;
310 route
->table
= table
;
311 route
->mtu
= link
->network
->dhcp_route_mtu
;
312 route
->scope
= route_scope_from_address(route
, &address
);
313 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
314 route
->prefsrc
.in
= address
;
316 if (set_contains(link
->dhcp_routes
, route
))
319 r
= dhcp_route_configure(&route
, link
);
321 return log_link_error_errno(link
, r
, "Could not set route: %m");
325 if (link
->network
->dhcp_use_gateway
) {
326 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
327 if (IN_SET(r
, 0, -ENODATA
))
328 log_link_info(link
, "DHCP: No gateway received from DHCP server.");
330 log_link_warning_errno(link
, r
, "DHCP error: could not get gateway: %m");
331 else if (in4_addr_is_null(&router
[0]))
332 log_link_info(link
, "DHCP: Received gateway is null.");
334 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
335 a Router option, the DHCP client MUST ignore the Router option. */
336 if (classless_route
&& static_route
)
337 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring static-route option and router option");
339 if (r
> 0 && !classless_route
&& !in4_addr_is_null(&router
[0])) {
340 _cleanup_(route_freep
) Route
*route
= NULL
, *route_gw
= NULL
;
342 r
= route_new(&route_gw
);
344 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
346 /* The dhcp netmask may mask out the gateway. Add an explicit
347 * route for the gw host so that we can route no matter the
348 * netmask or existing kernel route tables. */
349 route_gw
->family
= AF_INET
;
350 route_gw
->dst
.in
= router
[0];
351 route_gw
->dst_prefixlen
= 32;
352 route_gw
->prefsrc
.in
= address
;
353 route_gw
->scope
= RT_SCOPE_LINK
;
354 route_gw
->protocol
= RTPROT_DHCP
;
355 route_gw
->priority
= link
->network
->dhcp_route_metric
;
356 route_gw
->table
= table
;
357 route_gw
->mtu
= link
->network
->dhcp_route_mtu
;
359 r
= dhcp_route_configure(&route_gw
, link
);
361 return log_link_error_errno(link
, r
, "Could not set host route: %m");
363 r
= route_new(&route
);
365 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
367 route
->family
= AF_INET
;
368 route
->gw
.in
= router
[0];
369 route
->prefsrc
.in
= address
;
370 route
->protocol
= RTPROT_DHCP
;
371 route
->priority
= link
->network
->dhcp_route_metric
;
372 route
->table
= table
;
373 route
->mtu
= link
->network
->dhcp_route_mtu
;
375 r
= dhcp_route_configure(&route
, link
);
377 return log_link_error_errno(link
, r
, "Could not set router: %m");
381 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
382 if (!rt
->gateway_from_dhcp
)
385 if (rt
->family
!= AF_INET
)
388 rt
->gw
.in
= router
[0];
390 r
= route_configure(rt
, link
, dhcp4_route_handler
);
392 return log_link_error_errno(link
, r
, "Could not set gateway: %m");
394 link
->dhcp4_messages
++;
398 return link_set_dns_routes(link
, &address
);
401 static int dhcp_route_remove(Route
*route
, Link
*link
, link_netlink_message_handler_t callback
) {
404 r
= route_remove(route
, link
, callback
);
409 link
->dhcp4_remove_messages
++;
414 static int dhcp_remove_routes(
416 sd_dhcp_lease
*lease
,
417 const struct in_addr
*address
,
419 link_netlink_message_handler_t callback
) {
421 _cleanup_free_ sd_dhcp_route
**routes
= NULL
;
428 if (!link
->network
->dhcp_use_routes
)
431 n
= sd_dhcp_lease_get_routes(lease
, &routes
);
432 if (IN_SET(n
, 0, -ENODATA
))
435 return log_link_error_errno(link
, n
, "DHCP error: Failed to get routes: %m");
437 table
= link_get_dhcp_route_table(link
);
439 for (i
= 0; i
< n
; i
++) {
440 _cleanup_(route_freep
) Route
*route
= NULL
;
442 r
= route_new(&route
);
446 route
->family
= AF_INET
;
447 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &route
->gw
.in
) >= 0);
448 assert_se(sd_dhcp_route_get_destination(routes
[i
], &route
->dst
.in
) >= 0);
449 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &route
->dst_prefixlen
) >= 0);
450 route
->priority
= link
->network
->dhcp_route_metric
;
451 route
->table
= table
;
452 route
->scope
= route_scope_from_address(route
, address
);
453 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
454 route
->prefsrc
.in
= *address
;
456 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
459 r
= dhcp_route_remove(route
, link
, callback
);
467 static int dhcp_remove_router(
469 sd_dhcp_lease
*lease
,
470 const struct in_addr
*address
,
472 link_netlink_message_handler_t callback
) {
474 _cleanup_(route_freep
) Route
*route_gw
= NULL
, *route
= NULL
;
475 const struct in_addr
*router
;
482 if (!link
->network
->dhcp_use_gateway
)
485 r
= sd_dhcp_lease_get_router(lease
, &router
);
486 if (IN_SET(r
, 0, -ENODATA
)) {
487 log_link_debug(link
, "DHCP: No gateway received from DHCP server.");
490 return log_link_error_errno(link
, r
, "DHCP error: could not get gateway: %m");
491 else if (in4_addr_is_null(&router
[0])) {
492 log_link_info(link
, "DHCP: Received gateway is null, ignoring.");
496 table
= link_get_dhcp_route_table(link
);
498 r
= route_new(&route_gw
);
502 route_gw
->family
= AF_INET
;
503 route_gw
->dst
.in
= router
[0];
504 route_gw
->dst_prefixlen
= 32;
505 route_gw
->prefsrc
.in
= *address
;
506 route_gw
->scope
= RT_SCOPE_LINK
;
507 route_gw
->protocol
= RTPROT_DHCP
;
508 route_gw
->priority
= link
->network
->dhcp_route_metric
;
509 route_gw
->table
= table
;
511 if (remove_all
|| !set_contains(link
->dhcp_routes
, route_gw
)) {
512 r
= dhcp_route_remove(route_gw
, link
, callback
);
517 r
= route_new(&route
);
521 route
->family
= AF_INET
;
522 route
->gw
.in
= router
[0];
523 route
->prefsrc
.in
= *address
;
524 route
->protocol
= RTPROT_DHCP
;
525 route
->priority
= link
->network
->dhcp_route_metric
;
526 route
->table
= table
;
528 if (remove_all
|| !set_contains(link
->dhcp_routes
, route
)) {
529 r
= dhcp_route_remove(route
, link
, callback
);
535 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
536 if (!rt
->gateway_from_dhcp
)
539 if (rt
->family
!= AF_INET
)
542 if (!remove_all
&& in4_addr_equal(router
, &rt
->gw
.in
))
545 r
= dhcp_route_remove(rt
, link
, callback
);
553 static int dhcp_remove_dns_routes(
555 sd_dhcp_lease
*lease
,
556 const struct in_addr
*address
,
558 link_netlink_message_handler_t callback
) {
560 const struct in_addr
*dns
;
566 assert(link
->network
);
568 if (!link
->network
->dhcp_use_dns
||
569 !link
->network
->dhcp_routes_to_dns
)
572 n
= sd_dhcp_lease_get_dns(lease
, &dns
);
573 if (IN_SET(n
, 0, -ENODATA
))
576 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
578 table
= link_get_dhcp_route_table(link
);
580 for (i
= 0; i
< n
; i
++) {
581 _cleanup_(route_freep
) Route
*route
= NULL
;
583 r
= route_new(&route
);
585 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
587 route
->family
= AF_INET
;
588 route
->dst
.in
= dns
[i
];
589 route
->dst_prefixlen
= 32;
590 route
->prefsrc
.in
= *address
;
591 route
->scope
= RT_SCOPE_LINK
;
592 route
->protocol
= RTPROT_DHCP
;
593 route
->priority
= link
->network
->dhcp_route_metric
;
594 route
->table
= table
;
596 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
599 r
= dhcp_route_remove(route
, link
, callback
);
604 if (!link_prefixroute(link
)) {
605 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
607 r
= dhcp_prefix_route_from_lease(lease
, table
, address
, &prefix_route
);
609 return log_link_warning_errno(link
, r
, "Could not create prefix route: %m");
611 if (remove_all
|| !set_contains(link
->dhcp_routes
, prefix_route
)) {
612 r
= dhcp_route_remove(prefix_route
, link
, callback
);
621 static int dhcp_remove_address(
622 Link
*link
, sd_dhcp_lease
*lease
,
623 const struct in_addr
*address
,
624 link_netlink_message_handler_t callback
) {
626 _cleanup_(address_freep
) Address
*a
= NULL
;
627 struct in_addr netmask
;
633 if (in4_addr_is_null(address
))
641 a
->in_addr
.in
= *address
;
643 if (sd_dhcp_lease_get_netmask(lease
, &netmask
) >= 0)
644 a
->prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
646 r
= address_remove(a
, link
, callback
);
651 link
->dhcp4_remove_messages
++;
656 static int dhcp_reset_mtu(Link
*link
) {
662 if (!link
->network
->dhcp_use_mtu
)
665 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
669 if (link
->original_mtu
== mtu
)
672 r
= link_set_mtu(link
, link
->original_mtu
);
674 log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
675 link_enter_failed(link
);
682 static int dhcp_reset_hostname(Link
*link
) {
683 const char *hostname
;
688 if (!link
->network
->dhcp_use_hostname
)
691 hostname
= link
->network
->dhcp_hostname
;
693 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
698 /* If a hostname was set due to the lease, then unset it now. */
699 r
= manager_set_hostname(link
->manager
, NULL
);
701 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
706 static int dhcp4_remove_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
711 assert(link
->dhcp4_remove_messages
> 0);
713 link
->dhcp4_remove_messages
--;
715 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
718 r
= sd_netlink_message_get_errno(m
);
719 if (r
< 0 && r
!= -ESRCH
)
720 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 route, ignoring");
722 if (link
->dhcp4_remove_messages
== 0) {
723 r
= dhcp4_update_address(link
, false);
725 link_enter_failed(link
);
731 static int dhcp4_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
736 assert(link
->dhcp4_remove_messages
> 0);
738 link
->dhcp4_remove_messages
--;
740 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
743 r
= sd_netlink_message_get_errno(m
);
744 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
745 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 address, ignoring");
747 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
749 if (link
->dhcp4_remove_messages
== 0) {
750 r
= dhcp4_update_address(link
, false);
752 link_enter_failed(link
);
758 static int dhcp4_remove_all(Link
*link
) {
759 struct in_addr address
;
763 assert(link
->dhcp_lease
);
765 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
767 return log_link_error_errno(link
, r
, "Failed to get DHCPv4 address: %m");
769 r
= dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true, dhcp4_remove_route_handler
);
773 r
= dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true, dhcp4_remove_route_handler
);
777 r
= dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true, dhcp4_remove_route_handler
);
781 r
= dhcp_remove_address(link
, link
->dhcp_lease
, &address
, dhcp4_remove_address_handler
);
788 static int dhcp_lease_lost(Link
*link
) {
792 assert(link
->dhcp_lease
);
794 log_link_info(link
, "DHCP lease lost");
796 link
->dhcp4_configured
= false;
798 /* dhcp_lease_lost() may be called during renewing IP address. */
799 dhcp4_release_old_lease(link
);
801 r
= dhcp4_remove_all(link
);
805 r
= dhcp_reset_mtu(link
);
809 r
= dhcp_reset_hostname(link
);
813 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
819 static void dhcp_address_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
820 _cleanup_free_
char *pretty
= NULL
;
821 union in_addr_union address
= {};
831 case SD_IPV4ACD_EVENT_STOP
:
832 log_link_debug(link
, "Stopping ACD client for DHCP4...");
835 case SD_IPV4ACD_EVENT_BIND
:
837 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
838 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
839 log_link_debug(link
, "Successfully claimed DHCP4 address %s", strna(pretty
));
841 link
->dhcp4_address_bind
= true;
842 dhcp4_check_ready(link
);
845 case SD_IPV4ACD_EVENT_CONFLICT
:
846 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
847 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
848 log_link_warning(link
, "DAD conflict. Dropping DHCP4 address %s", strna(pretty
));
850 r
= sd_dhcp_client_send_decline(link
->dhcp_client
);
852 log_link_warning_errno(link
, r
, "Failed to send DHCP DECLINE, ignoring: %m");
854 if (link
->dhcp_lease
) {
855 r
= dhcp_lease_lost(link
);
857 link_enter_failed(link
);
862 assert_not_reached("Invalid IPv4ACD event.");
865 sd_ipv4acd_stop(acd
);
870 static int configure_dhcpv4_duplicate_address_detection(Link
*link
) {
875 r
= sd_ipv4acd_new(&link
->network
->dhcp_acd
);
879 r
= sd_ipv4acd_attach_event(link
->network
->dhcp_acd
, NULL
, 0);
883 r
= sd_ipv4acd_set_ifindex(link
->network
->dhcp_acd
, link
->ifindex
);
887 r
= sd_ipv4acd_set_mac(link
->network
->dhcp_acd
, &link
->mac
);
894 static int dhcp4_start_acd(Link
*link
) {
895 union in_addr_union addr
;
898 if (!link
->network
->dhcp_send_decline
)
901 if (!link
->dhcp_lease
)
904 link
->dhcp4_address_bind
= false;
906 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &addr
.in
);
910 r
= sd_ipv4acd_set_address(link
->network
->dhcp_acd
, &addr
.in
);
914 r
= sd_ipv4acd_set_callback(link
->network
->dhcp_acd
, dhcp_address_on_acd
, link
);
919 _cleanup_free_
char *pretty
= NULL
;
921 (void) in_addr_to_string(AF_INET
, &addr
, &pretty
);
922 log_link_debug(link
, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty
));
925 r
= sd_ipv4acd_start(link
->network
->dhcp_acd
, true);
932 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
937 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
940 r
= sd_netlink_message_get_errno(m
);
941 if (r
< 0 && r
!= -EEXIST
) {
942 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 address");
943 link_enter_failed(link
);
946 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
948 r
= link_set_dhcp_routes(link
);
950 link_enter_failed(link
);
954 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
955 r
= link_request_set_routes(link
);
957 link_enter_failed(link
);
961 r
= dhcp4_start_acd(link
);
963 log_link_error_errno(link
, r
, "Failed to start IPv4ACD for DHCP4 adddress: %m");
964 link_enter_failed(link
);
968 dhcp4_check_ready(link
);
973 static int dhcp4_update_address(Link
*link
, bool announce
) {
974 _cleanup_(address_freep
) Address
*addr
= NULL
;
975 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
976 struct in_addr address
, netmask
;
981 assert(link
->network
);
983 if (!link
->dhcp_lease
)
986 link_set_state(link
, LINK_STATE_CONFIGURING
);
987 link
->dhcp4_configured
= false;
989 /* address_handler calls link_request_set_routes() and link_request_set_nexthop(). Before they
990 * are called, the related flags must be cleared. Otherwise, the link becomes configured state
991 * before routes are configured. */
992 link
->static_routes_configured
= false;
993 link
->static_nexthops_configured
= false;
995 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
997 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
999 r
= sd_dhcp_lease_get_netmask(link
->dhcp_lease
, &netmask
);
1001 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
1003 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1004 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
1006 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
1009 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
1012 const struct in_addr
*router
;
1014 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
1015 if (r
< 0 && r
!= -ENODATA
)
1016 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
1018 if (r
> 0 && !in4_addr_is_null(&router
[0]))
1019 log_struct(LOG_INFO
,
1020 LOG_LINK_INTERFACE(link
),
1021 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
1022 ADDRESS_FMT_VAL(address
),
1024 ADDRESS_FMT_VAL(router
[0])),
1025 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
1026 "PREFIXLEN=%u", prefixlen
,
1027 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router
[0]));
1029 log_struct(LOG_INFO
,
1030 LOG_LINK_INTERFACE(link
),
1031 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u",
1032 ADDRESS_FMT_VAL(address
),
1034 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
1035 "PREFIXLEN=%u", prefixlen
);
1038 r
= address_new(&addr
);
1042 addr
->family
= AF_INET
;
1043 addr
->in_addr
.in
.s_addr
= address
.s_addr
;
1044 addr
->cinfo
.ifa_prefered
= lifetime
;
1045 addr
->cinfo
.ifa_valid
= lifetime
;
1046 addr
->prefixlen
= prefixlen
;
1047 addr
->broadcast
.s_addr
= address
.s_addr
| ~netmask
.s_addr
;
1048 addr
->prefix_route
= link_prefixroute(link
);
1050 /* allow reusing an existing address and simply update its lifetime
1051 * in case it already exists */
1052 r
= address_configure(addr
, link
, dhcp4_address_handler
, true);
1059 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
1060 sd_dhcp_lease
*lease
;
1066 r
= sd_dhcp_client_get_lease(client
, &lease
);
1068 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
1070 sd_dhcp_lease_unref(link
->dhcp_lease
);
1071 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
1074 return dhcp4_update_address(link
, false);
1077 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
1078 sd_dhcp_lease
*lease
;
1084 r
= sd_dhcp_client_get_lease(client
, &lease
);
1086 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
1088 sd_dhcp_lease_unref(link
->dhcp_lease
);
1089 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
1092 if (link
->network
->dhcp_use_mtu
) {
1095 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
1097 r
= link_set_mtu(link
, mtu
);
1099 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
1103 if (link
->network
->dhcp_use_hostname
) {
1104 const char *dhcpname
= NULL
;
1105 _cleanup_free_
char *hostname
= NULL
;
1107 if (link
->network
->dhcp_hostname
)
1108 dhcpname
= link
->network
->dhcp_hostname
;
1110 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
1113 r
= shorten_overlong(dhcpname
, &hostname
);
1115 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
1117 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
1121 r
= manager_set_hostname(link
->manager
, hostname
);
1123 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
1127 if (link
->network
->dhcp_use_timezone
) {
1128 const char *tz
= NULL
;
1130 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
1133 r
= manager_set_timezone(link
->manager
, tz
);
1135 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
1139 if (link
->dhcp4_remove_messages
== 0) {
1140 r
= dhcp4_update_address(link
, true);
1144 log_link_debug(link
,
1145 "The link has previously assigned DHCPv4 address or routes. "
1146 "The newly assigned address and routes will set up after old ones are removed.");
1151 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
1154 link
->dhcp_lease_old
= TAKE_PTR(link
->dhcp_lease
);
1156 /* On IP address change, to keep the connectability, we would like to assign new address and
1157 * routes, and then release old lease. There are two possible success paths:
1159 * 1. new address and routes are configured.
1160 * -> handled by dhcp_release_old_lease() in dhcp4_route_handler().
1161 * 2. new address is configured and no route is requested.
1162 * -> handled by dhcp_release_old_lease() in dhcp4_address_handler().
1164 * On error in assigning new address and routes, then the link always enters to the failed
1165 * state. And link_enter_failed() leads to the DHCP client to be stopped. So,
1166 * dhcp_release_old_lease() will be also called by link_stop_clients().
1169 r
= dhcp_lease_acquired(client
, link
);
1171 /* If it fails, then the new address is not configured yet.
1172 * So, let's simply drop the old lease. */
1173 sd_dhcp_lease_unref(link
->dhcp_lease
);
1174 link
->dhcp_lease
= TAKE_PTR(link
->dhcp_lease_old
);
1175 (void) dhcp_lease_lost(link
);
1182 static int dhcp_server_is_deny_listed(Link
*link
, sd_dhcp_client
*client
) {
1183 sd_dhcp_lease
*lease
;
1184 struct in_addr addr
;
1188 assert(link
->network
);
1191 r
= sd_dhcp_client_get_lease(client
, &lease
);
1193 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1195 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1197 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1199 if (set_contains(link
->network
->dhcp_deny_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1200 log_struct(LOG_DEBUG
,
1201 LOG_LINK_INTERFACE(link
),
1202 LOG_LINK_MESSAGE(link
, "DHCPv4 IP '%u.%u.%u.%u' found in deny-listed IP addresses, ignoring offer",
1203 ADDRESS_FMT_VAL(addr
)));
1210 static int dhcp_server_is_allow_listed(Link
*link
, sd_dhcp_client
*client
) {
1211 sd_dhcp_lease
*lease
;
1212 struct in_addr addr
;
1216 assert(link
->network
);
1219 r
= sd_dhcp_client_get_lease(client
, &lease
);
1221 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1223 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1225 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1227 if (set_contains(link
->network
->dhcp_allow_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1228 log_struct(LOG_DEBUG
,
1229 LOG_LINK_INTERFACE(link
),
1230 LOG_LINK_MESSAGE(link
, "DHCPv4 IP '%u.%u.%u.%u' found in allow-listed IP addresses, accepting offer",
1231 ADDRESS_FMT_VAL(addr
)));
1238 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
1239 Link
*link
= userdata
;
1243 assert(link
->network
);
1244 assert(link
->manager
);
1246 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1250 case SD_DHCP_CLIENT_EVENT_STOP
:
1252 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
)) {
1253 assert(link
->ipv4ll
);
1255 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
1257 r
= sd_ipv4ll_start(link
->ipv4ll
);
1259 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1262 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1263 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1267 if (link
->dhcp_lease
) {
1268 if (link
->network
->dhcp_send_release
) {
1269 r
= sd_dhcp_client_send_release(client
);
1271 log_link_warning_errno(link
, r
, "Failed to send DHCP RELEASE, ignoring: %m");
1274 r
= dhcp_lease_lost(link
);
1276 link_enter_failed(link
);
1282 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
1283 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1284 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1288 if (link
->dhcp_lease
) {
1289 r
= dhcp_lease_lost(link
);
1291 link_enter_failed(link
);
1297 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
1298 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1299 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1303 r
= dhcp_lease_ip_change(client
, link
);
1305 link_enter_failed(link
);
1310 case SD_DHCP_CLIENT_EVENT_RENEW
:
1311 r
= dhcp_lease_renew(client
, link
);
1313 link_enter_failed(link
);
1317 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1318 r
= dhcp_lease_acquired(client
, link
);
1320 link_enter_failed(link
);
1324 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1325 if (!set_isempty(link
->network
->dhcp_allow_listed_ip
)) {
1326 r
= dhcp_server_is_allow_listed(link
, client
);
1332 r
= dhcp_server_is_deny_listed(link
, client
);
1341 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1343 log_link_warning(link
, "DHCP unknown event: %i", event
);
1350 static int dhcp4_set_hostname(Link
*link
) {
1351 _cleanup_free_
char *hostname
= NULL
;
1357 if (!link
->network
->dhcp_send_hostname
)
1359 else if (link
->network
->dhcp_hostname
)
1360 hn
= link
->network
->dhcp_hostname
;
1362 r
= gethostname_strict(&hostname
);
1363 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1369 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1370 if (r
== -EINVAL
&& hostname
)
1371 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1372 log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1374 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
1379 static bool promote_secondaries_enabled(const char *ifname
) {
1380 _cleanup_free_
char *promote_secondaries_sysctl
= NULL
;
1381 char *promote_secondaries_path
;
1384 promote_secondaries_path
= strjoina("net/ipv4/conf/", ifname
, "/promote_secondaries");
1385 r
= sysctl_read(promote_secondaries_path
, &promote_secondaries_sysctl
);
1387 log_debug_errno(r
, "Cannot read sysctl %s", promote_secondaries_path
);
1391 truncate_nl(promote_secondaries_sysctl
);
1392 r
= parse_boolean(promote_secondaries_sysctl
);
1394 log_warning_errno(r
, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path
, promote_secondaries_sysctl
);
1398 /* dhcp4_set_promote_secondaries will ensure this interface has
1399 * the "promote_secondaries" option in the kernel set. If this sysctl
1400 * is not set DHCP will work only as long as the IP address does not
1401 * changes between leases. The kernel will remove all secondary IP
1402 * addresses of an interface otherwise. The way systemd-network works
1403 * is that the new IP of a lease is added as a secondary IP and when
1404 * the primary one expires it relies on the kernel to promote the
1405 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1407 int dhcp4_set_promote_secondaries(Link
*link
) {
1411 assert(link
->network
);
1412 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1414 /* check if the kernel has promote_secondaries enabled for our
1415 * interface. If it is not globally enabled or enabled for the
1416 * specific interface we must either enable it.
1418 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link
->ifname
))) {
1419 char *promote_secondaries_path
= NULL
;
1421 log_link_debug(link
, "promote_secondaries is unset, setting it");
1422 promote_secondaries_path
= strjoina("net/ipv4/conf/", link
->ifname
, "/promote_secondaries");
1423 r
= sysctl_write(promote_secondaries_path
, "1");
1425 log_link_warning_errno(link
, r
, "cannot set sysctl %s to 1", promote_secondaries_path
);
1432 int dhcp4_set_client_identifier(Link
*link
) {
1436 assert(link
->network
);
1437 assert(link
->dhcp_client
);
1439 switch (link
->network
->dhcp_client_identifier
) {
1440 case DHCP_CLIENT_ID_DUID
: {
1441 /* If configured, apply user specified DUID and IAID */
1442 const DUID
*duid
= link_get_duid(link
);
1444 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1445 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1446 link
->network
->iaid_set
,
1447 link
->network
->iaid
,
1450 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
1451 link
->network
->iaid_set
,
1452 link
->network
->iaid
,
1454 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1455 duid
->raw_data_len
);
1457 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1460 case DHCP_CLIENT_ID_DUID_ONLY
: {
1461 /* If configured, apply user specified DUID */
1462 const DUID
*duid
= link_get_duid(link
);
1464 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1465 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
1468 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
1470 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1471 duid
->raw_data_len
);
1473 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
1476 case DHCP_CLIENT_ID_MAC
:
1477 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1479 (const uint8_t *) &link
->mac
,
1482 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
1485 assert_not_reached("Unknown client identifier type.");
1491 int dhcp4_configure(Link
*link
) {
1492 sd_dhcp_option
*send_option
;
1493 void *request_options
;
1498 assert(link
->network
);
1499 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1501 if (!link
->dhcp_client
) {
1502 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1506 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
1508 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
1510 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to attach event: %m");
1513 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1514 (const uint8_t *) &link
->mac
,
1515 sizeof (link
->mac
), ARPHRD_ETHER
);
1517 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1519 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1521 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1523 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1525 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1527 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
,
1528 link
->network
->dhcp_broadcast
);
1530 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1533 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1535 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1538 if (link
->network
->dhcp_use_mtu
) {
1539 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1540 SD_DHCP_OPTION_INTERFACE_MTU
);
1542 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1545 /* NOTE: even if this variable is called "use", it also "sends" PRL
1546 * options, maybe there should be a different configuration variable
1547 * to send or not route options?. */
1548 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1549 * by default, so they don't need to be added here. */
1550 if (link
->network
->dhcp_use_routes
&& !link
->network
->dhcp_anonymize
) {
1551 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1552 SD_DHCP_OPTION_STATIC_ROUTE
);
1554 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1556 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1557 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1559 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1562 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
&& !link
->network
->dhcp_anonymize
) {
1563 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
);
1565 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1568 if (link
->network
->dhcp_use_ntp
) {
1569 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1571 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1574 if (link
->network
->dhcp_use_sip
) {
1575 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_SIP_SERVER
);
1577 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1580 if (link
->network
->dhcp_use_timezone
) {
1581 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1583 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1586 SET_FOREACH(request_options
, link
->network
->dhcp_request_options
, i
) {
1587 uint32_t option
= PTR_TO_UINT32(request_options
);
1589 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, option
);
1591 log_link_debug(link
, "DHCP4 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option
);
1595 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option
);
1598 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_options
, i
) {
1599 r
= sd_dhcp_client_add_option(link
->dhcp_client
, send_option
);
1603 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1606 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_vendor_options
, i
) {
1607 r
= sd_dhcp_client_add_vendor_option(link
->dhcp_client
, send_option
);
1611 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1614 r
= dhcp4_set_hostname(link
);
1618 if (link
->network
->dhcp_vendor_class_identifier
) {
1619 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1620 link
->network
->dhcp_vendor_class_identifier
);
1622 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1625 if (link
->network
->dhcp_mudurl
) {
1626 r
= sd_dhcp_client_set_mud_url(link
->dhcp_client
,
1627 link
->network
->dhcp_mudurl
);
1629 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1632 if (link
->network
->dhcp_user_class
) {
1633 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, (const char **) link
->network
->dhcp_user_class
);
1635 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1638 if (link
->network
->dhcp_client_port
) {
1639 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1641 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen 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_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1650 if (link
->network
->ip_service_type
> 0) {
1651 r
= sd_dhcp_client_set_service_type(link
->dhcp_client
, link
->network
->ip_service_type
);
1653 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IP service type: %m");
1656 if (link
->network
->dhcp_fallback_lease_lifetime
> 0) {
1657 r
= sd_dhcp_client_set_fallback_lease_lifetime(link
->dhcp_client
, link
->network
->dhcp_fallback_lease_lifetime
);
1659 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1662 if (link
->network
->dhcp_send_decline
) {
1663 r
= configure_dhcpv4_duplicate_address_detection(link
);
1665 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to configure service type: %m");
1668 return dhcp4_set_client_identifier(link
);
1671 int config_parse_dhcp_max_attempts(
1673 const char *filename
,
1675 const char *section
,
1676 unsigned section_line
,
1683 Network
*network
= data
;
1691 if (isempty(rvalue
)) {
1692 network
->dhcp_max_attempts
= 0;
1696 if (streq(rvalue
, "infinity")) {
1697 network
->dhcp_max_attempts
= (uint64_t) -1;
1701 r
= safe_atou64(rvalue
, &a
);
1703 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1704 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1709 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1710 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1714 network
->dhcp_max_attempts
= a
;
1719 int config_parse_dhcp_acl_ip_address(
1721 const char *filename
,
1723 const char *section
,
1724 unsigned section_line
,
1731 Network
*network
= data
;
1740 acl
= STR_IN_SET(lvalue
, "DenyList", "BlackList") ? &network
->dhcp_deny_listed_ip
: &network
->dhcp_allow_listed_ip
;
1742 if (isempty(rvalue
)) {
1743 *acl
= set_free(*acl
);
1747 for (const char *p
= rvalue
;;) {
1748 _cleanup_free_
char *n
= NULL
;
1749 union in_addr_union ip
;
1751 r
= extract_first_word(&p
, &n
, NULL
, 0);
1753 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1754 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1761 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1763 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1764 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue
, n
);
1768 r
= set_ensure_put(acl
, NULL
, UINT32_TO_PTR(ip
.in
.s_addr
));
1770 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1771 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue
, n
);
1777 int config_parse_dhcp_ip_service_type(
1779 const char *filename
,
1781 const char *section
,
1782 unsigned section_line
,
1793 if (streq(rvalue
, "CS4"))
1794 *((int *)data
) = IPTOS_CLASS_CS4
;
1795 else if (streq(rvalue
, "CS6"))
1796 *((int *)data
) = IPTOS_CLASS_CS6
;
1798 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1799 "Failed to parse IPServiceType type '%s', ignoring.", rvalue
);
1804 int config_parse_dhcp_mud_url(
1806 const char *filename
,
1808 const char *section
,
1809 unsigned section_line
,
1816 _cleanup_free_
char *unescaped
= NULL
;
1817 Network
*network
= data
;
1824 if (isempty(rvalue
)) {
1825 network
->dhcp_mudurl
= mfree(network
->dhcp_mudurl
);
1829 r
= cunescape(rvalue
, 0, &unescaped
);
1831 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1832 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue
);
1836 if (!http_url_is_valid(unescaped
) || strlen(unescaped
) > 255) {
1837 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1838 "Failed to parse MUD URL '%s', ignoring: %m", rvalue
);
1843 return free_and_strdup_warn(&network
->dhcp_mudurl
, unescaped
);
1846 int config_parse_dhcp_fallback_lease_lifetime(const char *unit
,
1847 const char *filename
,
1849 const char *section
,
1850 unsigned section_line
,
1856 Network
*network
= userdata
;
1865 if (isempty(rvalue
)) {
1866 network
->dhcp_fallback_lease_lifetime
= 0;
1870 /* We accept only "forever" or "infinity". */
1871 if (STR_IN_SET(rvalue
, "forever", "infinity"))
1872 k
= CACHE_INFO_INFINITY_LIFE_TIME
;
1874 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1875 "Invalid LeaseLifetime= value, ignoring: %s", rvalue
);
1879 network
->dhcp_fallback_lease_lifetime
= k
;
1884 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1885 [DHCP_CLIENT_ID_MAC
] = "mac",
1886 [DHCP_CLIENT_ID_DUID
] = "duid",
1887 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1890 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1891 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1892 "Failed to parse client identifier type");