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 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
->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 link
->dhcp4_route_failed
= false;
92 link
->dhcp4_route_retrying
= true;
94 r
= dhcp4_remove_all(link
);
96 link_enter_failed(link
);
99 if (!link
->network
->dhcp_send_decline
)
100 dhcp4_check_ready(link
);
106 static int route_scope_from_address(const Route
*route
, const struct in_addr
*self_addr
) {
110 if (in4_addr_is_localhost(&route
->dst
.in
) ||
111 (!in4_addr_is_null(self_addr
) && in4_addr_equal(&route
->dst
.in
, self_addr
)))
112 return RT_SCOPE_HOST
;
113 else if (in4_addr_is_null(&route
->gw
.in
))
114 return RT_SCOPE_LINK
;
116 return RT_SCOPE_UNIVERSE
;
119 static bool link_prefixroute(Link
*link
) {
120 return !link
->network
->dhcp_route_table_set
||
121 link
->network
->dhcp_route_table
== RT_TABLE_MAIN
||
122 link
->manager
->dhcp4_prefix_root_cannot_set_table
;
125 static int dhcp_route_configure(Route
**route
, Link
*link
) {
132 if (set_contains(link
->dhcp_routes
, *route
))
135 r
= route_configure(*route
, link
, dhcp4_route_handler
);
139 link
->dhcp4_messages
++;
141 r
= set_put(link
->dhcp_routes
, *route
);
149 static int link_set_dns_routes(Link
*link
, const struct in_addr
*address
) {
150 const struct in_addr
*dns
;
155 assert(link
->dhcp_lease
);
156 assert(link
->network
);
158 if (!link
->network
->dhcp_use_dns
||
159 !link
->network
->dhcp_routes_to_dns
)
162 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &dns
);
163 if (IN_SET(n
, 0, -ENODATA
))
166 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
168 table
= link_get_dhcp_route_table(link
);
170 for (i
= 0; i
< n
; i
++) {
171 _cleanup_(route_freep
) Route
*route
= NULL
;
173 r
= route_new(&route
);
175 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
177 /* Set routes to DNS servers. */
179 route
->family
= AF_INET
;
180 route
->dst
.in
= dns
[i
];
181 route
->dst_prefixlen
= 32;
182 route
->prefsrc
.in
= *address
;
183 route
->scope
= RT_SCOPE_LINK
;
184 route
->protocol
= RTPROT_DHCP
;
185 route
->priority
= link
->network
->dhcp_route_metric
;
186 route
->table
= table
;
188 r
= dhcp_route_configure(&route
, link
);
190 return log_link_error_errno(link
, r
, "Could not set route to DNS server: %m");
196 static int dhcp_prefix_route_from_lease(
197 const sd_dhcp_lease
*lease
,
199 const struct in_addr
*address
,
203 struct in_addr netmask
;
206 r
= sd_dhcp_lease_get_netmask((sd_dhcp_lease
*) lease
, &netmask
);
210 r
= route_new(&route
);
214 route
->family
= AF_INET
;
215 route
->dst
.in
.s_addr
= address
->s_addr
& netmask
.s_addr
;
216 route
->dst_prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
217 route
->prefsrc
.in
= *address
;
218 route
->scope
= RT_SCOPE_LINK
;
219 route
->protocol
= RTPROT_DHCP
;
220 route
->table
= table
;
225 static int link_set_dhcp_routes(Link
*link
) {
226 _cleanup_free_ sd_dhcp_route
**static_routes
= NULL
;
227 bool classless_route
= false, static_route
= false;
228 const struct in_addr
*router
;
229 struct in_addr address
;
235 if (!link
->dhcp_lease
) /* link went down while we configured the IP addresses? */
238 if (!link
->network
) /* link went down while we configured the IP addresses? */
241 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
242 /* During configuring addresses, the link lost its carrier. As networkd is dropping
243 * the addresses now, let's not configure the routes either. */
246 r
= set_ensure_allocated(&link
->dhcp_routes
, &route_hash_ops
);
250 /* Clear old entries in case the set was already allocated */
251 set_clear(link
->dhcp_routes
);
253 table
= link_get_dhcp_route_table(link
);
255 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
257 return log_link_warning_errno(link
, r
, "DHCP error: could not get address: %m");
259 if (!link_prefixroute(link
)) {
260 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
262 r
= dhcp_prefix_route_from_lease(link
->dhcp_lease
, table
, &address
, &prefix_route
);
264 return log_link_error_errno(link
, r
, "Could not create prefix route: %m");
266 r
= dhcp_route_configure(&prefix_route
, link
);
268 return log_link_error_errno(link
, r
, "Could not set prefix route: %m");
271 n
= sd_dhcp_lease_get_routes(link
->dhcp_lease
, &static_routes
);
273 log_link_debug_errno(link
, n
, "DHCP: No routes received from DHCP server: %m");
275 log_link_debug_errno(link
, n
, "DHCP: could not get routes: %m");
277 for (i
= 0; i
< n
; i
++) {
278 switch (sd_dhcp_route_get_option(static_routes
[i
])) {
279 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
280 classless_route
= true;
282 case SD_DHCP_OPTION_STATIC_ROUTE
:
288 if (link
->network
->dhcp_use_routes
) {
289 for (i
= 0; i
< n
; i
++) {
290 _cleanup_(route_freep
) Route
*route
= NULL
;
292 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
293 the DHCP client MUST ignore the Static Routes option. */
294 if (classless_route
&&
295 sd_dhcp_route_get_option(static_routes
[i
]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
)
298 r
= route_new(&route
);
300 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
302 route
->family
= AF_INET
;
303 route
->protocol
= RTPROT_DHCP
;
304 assert_se(sd_dhcp_route_get_gateway(static_routes
[i
], &route
->gw
.in
) >= 0);
305 assert_se(sd_dhcp_route_get_destination(static_routes
[i
], &route
->dst
.in
) >= 0);
306 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &route
->dst_prefixlen
) >= 0);
307 route
->priority
= link
->network
->dhcp_route_metric
;
308 route
->table
= table
;
309 route
->mtu
= link
->network
->dhcp_route_mtu
;
310 route
->scope
= route_scope_from_address(route
, &address
);
311 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
312 route
->prefsrc
.in
= address
;
314 if (set_contains(link
->dhcp_routes
, route
))
317 r
= dhcp_route_configure(&route
, link
);
319 return log_link_error_errno(link
, r
, "Could not set route: %m");
323 if (link
->network
->dhcp_use_gateway
) {
324 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
325 if (IN_SET(r
, 0, -ENODATA
))
326 log_link_info(link
, "DHCP: No gateway received from DHCP server.");
328 log_link_warning_errno(link
, r
, "DHCP error: could not get gateway: %m");
329 else if (in4_addr_is_null(&router
[0]))
330 log_link_info(link
, "DHCP: Received gateway is null.");
332 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
333 a Router option, the DHCP client MUST ignore the Router option. */
334 if (classless_route
&& static_route
)
335 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring static-route option and router option");
337 if (r
> 0 && !classless_route
&& !in4_addr_is_null(&router
[0])) {
338 _cleanup_(route_freep
) Route
*route
= NULL
, *route_gw
= NULL
;
340 r
= route_new(&route_gw
);
342 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
344 /* The dhcp netmask may mask out the gateway. Add an explicit
345 * route for the gw host so that we can route no matter the
346 * netmask or existing kernel route tables. */
347 route_gw
->family
= AF_INET
;
348 route_gw
->dst
.in
= router
[0];
349 route_gw
->dst_prefixlen
= 32;
350 route_gw
->prefsrc
.in
= address
;
351 route_gw
->scope
= RT_SCOPE_LINK
;
352 route_gw
->protocol
= RTPROT_DHCP
;
353 route_gw
->priority
= link
->network
->dhcp_route_metric
;
354 route_gw
->table
= table
;
355 route_gw
->mtu
= link
->network
->dhcp_route_mtu
;
357 r
= dhcp_route_configure(&route_gw
, link
);
359 return log_link_error_errno(link
, r
, "Could not set host route: %m");
361 r
= route_new(&route
);
363 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
365 route
->family
= AF_INET
;
366 route
->gw
.in
= router
[0];
367 route
->prefsrc
.in
= address
;
368 route
->protocol
= RTPROT_DHCP
;
369 route
->priority
= link
->network
->dhcp_route_metric
;
370 route
->table
= table
;
371 route
->mtu
= link
->network
->dhcp_route_mtu
;
373 r
= dhcp_route_configure(&route
, link
);
375 return log_link_error_errno(link
, r
, "Could not set router: %m");
379 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
380 if (!rt
->gateway_from_dhcp
)
383 if (rt
->family
!= AF_INET
)
386 rt
->gw
.in
= router
[0];
388 r
= route_configure(rt
, link
, dhcp4_route_handler
);
390 return log_link_error_errno(link
, r
, "Could not set gateway: %m");
392 link
->dhcp4_messages
++;
396 return link_set_dns_routes(link
, &address
);
399 static int dhcp_route_remove(Route
*route
, Link
*link
, link_netlink_message_handler_t callback
) {
402 r
= route_remove(route
, link
, callback
);
407 link
->dhcp4_remove_messages
++;
412 static int dhcp_remove_routes(
414 sd_dhcp_lease
*lease
,
415 const struct in_addr
*address
,
417 link_netlink_message_handler_t callback
) {
419 _cleanup_free_ sd_dhcp_route
**routes
= NULL
;
426 if (!link
->network
->dhcp_use_routes
)
429 n
= sd_dhcp_lease_get_routes(lease
, &routes
);
430 if (IN_SET(n
, 0, -ENODATA
))
433 return log_link_error_errno(link
, n
, "DHCP error: Failed to get routes: %m");
435 table
= link_get_dhcp_route_table(link
);
437 for (i
= 0; i
< n
; i
++) {
438 _cleanup_(route_freep
) Route
*route
= NULL
;
440 r
= route_new(&route
);
444 route
->family
= AF_INET
;
445 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &route
->gw
.in
) >= 0);
446 assert_se(sd_dhcp_route_get_destination(routes
[i
], &route
->dst
.in
) >= 0);
447 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &route
->dst_prefixlen
) >= 0);
448 route
->priority
= link
->network
->dhcp_route_metric
;
449 route
->table
= table
;
450 route
->scope
= route_scope_from_address(route
, address
);
451 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
452 route
->prefsrc
.in
= *address
;
454 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
457 r
= dhcp_route_remove(route
, link
, callback
);
465 static int dhcp_remove_router(
467 sd_dhcp_lease
*lease
,
468 const struct in_addr
*address
,
470 link_netlink_message_handler_t callback
) {
472 _cleanup_(route_freep
) Route
*route_gw
= NULL
, *route
= NULL
;
473 const struct in_addr
*router
;
480 if (!link
->network
->dhcp_use_gateway
)
483 r
= sd_dhcp_lease_get_router(lease
, &router
);
484 if (IN_SET(r
, 0, -ENODATA
)) {
485 log_link_debug(link
, "DHCP: No gateway received from DHCP server.");
488 return log_link_error_errno(link
, r
, "DHCP error: could not get gateway: %m");
489 else if (in4_addr_is_null(&router
[0])) {
490 log_link_info(link
, "DHCP: Received gateway is null, ignoring.");
494 table
= link_get_dhcp_route_table(link
);
496 r
= route_new(&route_gw
);
500 route_gw
->family
= AF_INET
;
501 route_gw
->dst
.in
= router
[0];
502 route_gw
->dst_prefixlen
= 32;
503 route_gw
->prefsrc
.in
= *address
;
504 route_gw
->scope
= RT_SCOPE_LINK
;
505 route_gw
->protocol
= RTPROT_DHCP
;
506 route_gw
->priority
= link
->network
->dhcp_route_metric
;
507 route_gw
->table
= table
;
509 if (remove_all
|| !set_contains(link
->dhcp_routes
, route_gw
)) {
510 r
= dhcp_route_remove(route_gw
, link
, callback
);
515 r
= route_new(&route
);
519 route
->family
= AF_INET
;
520 route
->gw
.in
= router
[0];
521 route
->prefsrc
.in
= *address
;
522 route
->protocol
= RTPROT_DHCP
;
523 route
->priority
= link
->network
->dhcp_route_metric
;
524 route
->table
= table
;
526 if (remove_all
|| !set_contains(link
->dhcp_routes
, route
)) {
527 r
= dhcp_route_remove(route
, link
, callback
);
533 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
534 if (!rt
->gateway_from_dhcp
)
537 if (rt
->family
!= AF_INET
)
540 if (!remove_all
&& in4_addr_equal(router
, &rt
->gw
.in
))
543 r
= dhcp_route_remove(rt
, link
, callback
);
551 static int dhcp_remove_dns_routes(
553 sd_dhcp_lease
*lease
,
554 const struct in_addr
*address
,
556 link_netlink_message_handler_t callback
) {
558 const struct in_addr
*dns
;
564 assert(link
->network
);
566 if (!link
->network
->dhcp_use_dns
||
567 !link
->network
->dhcp_routes_to_dns
)
570 n
= sd_dhcp_lease_get_dns(lease
, &dns
);
571 if (IN_SET(n
, 0, -ENODATA
))
574 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
576 table
= link_get_dhcp_route_table(link
);
578 for (i
= 0; i
< n
; i
++) {
579 _cleanup_(route_freep
) Route
*route
= NULL
;
581 r
= route_new(&route
);
583 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
585 route
->family
= AF_INET
;
586 route
->dst
.in
= dns
[i
];
587 route
->dst_prefixlen
= 32;
588 route
->prefsrc
.in
= *address
;
589 route
->scope
= RT_SCOPE_LINK
;
590 route
->protocol
= RTPROT_DHCP
;
591 route
->priority
= link
->network
->dhcp_route_metric
;
592 route
->table
= table
;
594 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
597 r
= dhcp_route_remove(route
, link
, callback
);
602 if (!link_prefixroute(link
)) {
603 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
605 r
= dhcp_prefix_route_from_lease(lease
, table
, address
, &prefix_route
);
607 return log_link_warning_errno(link
, r
, "Could not create prefix route: %m");
609 if (remove_all
|| !set_contains(link
->dhcp_routes
, prefix_route
)) {
610 r
= dhcp_route_remove(prefix_route
, link
, callback
);
619 static int dhcp_remove_address(
620 Link
*link
, sd_dhcp_lease
*lease
,
621 const struct in_addr
*address
,
622 link_netlink_message_handler_t callback
) {
624 _cleanup_(address_freep
) Address
*a
= NULL
;
625 struct in_addr netmask
;
631 if (in4_addr_is_null(address
))
639 a
->in_addr
.in
= *address
;
641 if (sd_dhcp_lease_get_netmask(lease
, &netmask
) >= 0)
642 a
->prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
644 r
= address_remove(a
, link
, callback
);
649 link
->dhcp4_remove_messages
++;
654 static int dhcp_reset_mtu(Link
*link
) {
660 if (!link
->network
->dhcp_use_mtu
)
663 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
667 if (link
->original_mtu
== mtu
)
670 r
= link_set_mtu(link
, link
->original_mtu
);
672 log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
673 link_enter_failed(link
);
680 static int dhcp_reset_hostname(Link
*link
) {
681 const char *hostname
;
686 if (!link
->network
->dhcp_use_hostname
)
689 hostname
= link
->network
->dhcp_hostname
;
691 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
696 /* If a hostname was set due to the lease, then unset it now. */
697 r
= manager_set_hostname(link
->manager
, NULL
);
699 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
704 static int dhcp4_remove_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
709 assert(link
->dhcp4_remove_messages
> 0);
711 link
->dhcp4_remove_messages
--;
713 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
716 r
= sd_netlink_message_get_errno(m
);
717 if (r
< 0 && r
!= -ESRCH
)
718 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 route, ignoring");
720 if (link
->dhcp4_remove_messages
== 0) {
721 r
= dhcp4_update_address(link
, false);
723 link_enter_failed(link
);
729 static int dhcp4_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
734 assert(link
->dhcp4_remove_messages
> 0);
736 link
->dhcp4_remove_messages
--;
738 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
741 r
= sd_netlink_message_get_errno(m
);
742 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
743 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 address, ignoring");
745 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
747 if (link
->dhcp4_remove_messages
== 0) {
748 r
= dhcp4_update_address(link
, false);
750 link_enter_failed(link
);
756 static int dhcp4_remove_all(Link
*link
) {
757 struct in_addr address
;
761 assert(link
->dhcp_lease
);
763 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
765 return log_link_error_errno(link
, r
, "Failed to get DHCPv4 address: %m");
767 r
= dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true, dhcp4_remove_route_handler
);
771 r
= dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true, dhcp4_remove_route_handler
);
775 r
= dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true, dhcp4_remove_route_handler
);
779 r
= dhcp_remove_address(link
, link
->dhcp_lease
, &address
, dhcp4_remove_address_handler
);
786 static int dhcp_lease_lost(Link
*link
) {
790 assert(link
->dhcp_lease
);
792 log_link_info(link
, "DHCP lease lost");
794 link
->dhcp4_configured
= false;
796 r
= dhcp4_remove_all(link
);
800 r
= dhcp_reset_mtu(link
);
804 r
= dhcp_reset_hostname(link
);
808 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
814 static void dhcp_address_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
815 _cleanup_free_
char *pretty
= NULL
;
816 union in_addr_union address
= {};
826 case SD_IPV4ACD_EVENT_STOP
:
827 log_link_debug(link
, "Stopping ACD client for DHCP4...");
830 case SD_IPV4ACD_EVENT_BIND
:
832 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
833 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
834 log_link_debug(link
, "Successfully claimed DHCP4 address %s", strna(pretty
));
836 dhcp4_check_ready(link
);
839 case SD_IPV4ACD_EVENT_CONFLICT
:
840 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
841 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
842 log_link_warning(link
, "DAD conflict. Dropping DHCP4 address %s", strna(pretty
));
844 (void) sd_dhcp_client_send_decline(link
->dhcp_client
);
846 if (link
->dhcp_lease
) {
847 r
= dhcp_lease_lost(link
);
849 link_enter_failed(link
);
854 assert_not_reached("Invalid IPv4ACD event.");
857 sd_ipv4acd_stop(acd
);
862 static int configure_dhcpv4_duplicate_address_detection(Link
*link
) {
867 r
= sd_ipv4acd_new(&link
->network
->dhcp_acd
);
871 r
= sd_ipv4acd_attach_event(link
->network
->dhcp_acd
, NULL
, 0);
875 r
= sd_ipv4acd_set_ifindex(link
->network
->dhcp_acd
, link
->ifindex
);
879 r
= sd_ipv4acd_set_mac(link
->network
->dhcp_acd
, &link
->mac
);
886 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
891 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
894 r
= sd_netlink_message_get_errno(m
);
895 if (r
< 0 && r
!= -EEXIST
) {
896 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 address");
897 link_enter_failed(link
);
900 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
902 r
= link_set_dhcp_routes(link
);
904 link_enter_failed(link
);
908 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
909 r
= link_request_set_routes(link
);
911 link_enter_failed(link
);
915 if (link
->network
->dhcp_send_decline
) {
916 union in_addr_union addr
;
918 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &addr
.in
);
920 r
= sd_ipv4acd_set_address(link
->network
->dhcp_acd
, &addr
.in
);
924 r
= sd_ipv4acd_set_callback(link
->network
->dhcp_acd
, dhcp_address_on_acd
, link
);
929 _cleanup_free_
char *pretty
= NULL
;
931 (void) in_addr_to_string(AF_INET
, &addr
, &pretty
);
932 log_link_debug(link
, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty
));
935 r
= sd_ipv4acd_start(link
->network
->dhcp_acd
, true);
937 log_link_warning_errno(link
, r
, "Failed to start IPv4ACD client, ignoring: %m");
939 dhcp4_check_ready(link
);
944 static int dhcp4_update_address(Link
*link
, bool announce
) {
945 _cleanup_(address_freep
) Address
*addr
= NULL
;
946 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
947 struct in_addr address
, netmask
;
952 assert(link
->network
);
954 if (!link
->dhcp_lease
)
957 link_set_state(link
, LINK_STATE_CONFIGURING
);
958 link
->dhcp4_configured
= false;
960 /* address_handler calls link_request_set_routes() and link_request_set_nexthop(). Before they
961 * are called, the related flags must be cleared. Otherwise, the link becomes configured state
962 * before routes are configured. */
963 link
->static_routes_configured
= false;
964 link
->static_nexthops_configured
= false;
966 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
968 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
970 r
= sd_dhcp_lease_get_netmask(link
->dhcp_lease
, &netmask
);
972 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
974 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
975 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
977 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
980 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
983 const struct in_addr
*router
;
985 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
986 if (r
< 0 && r
!= -ENODATA
)
987 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
989 if (r
> 0 && !in4_addr_is_null(&router
[0]))
991 LOG_LINK_INTERFACE(link
),
992 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
993 ADDRESS_FMT_VAL(address
),
995 ADDRESS_FMT_VAL(router
[0])),
996 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
997 "PREFIXLEN=%u", prefixlen
,
998 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router
[0]));
1000 log_struct(LOG_INFO
,
1001 LOG_LINK_INTERFACE(link
),
1002 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u",
1003 ADDRESS_FMT_VAL(address
),
1005 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
1006 "PREFIXLEN=%u", prefixlen
);
1009 r
= address_new(&addr
);
1013 addr
->family
= AF_INET
;
1014 addr
->in_addr
.in
.s_addr
= address
.s_addr
;
1015 addr
->cinfo
.ifa_prefered
= lifetime
;
1016 addr
->cinfo
.ifa_valid
= lifetime
;
1017 addr
->prefixlen
= prefixlen
;
1018 addr
->broadcast
.s_addr
= address
.s_addr
| ~netmask
.s_addr
;
1019 addr
->prefix_route
= link_prefixroute(link
);
1021 /* allow reusing an existing address and simply update its lifetime
1022 * in case it already exists */
1023 r
= address_configure(addr
, link
, dhcp4_address_handler
, true);
1030 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
1031 sd_dhcp_lease
*lease
;
1037 r
= sd_dhcp_client_get_lease(client
, &lease
);
1039 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
1041 sd_dhcp_lease_unref(link
->dhcp_lease
);
1042 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
1045 return dhcp4_update_address(link
, false);
1048 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
1049 sd_dhcp_lease
*lease
;
1055 r
= sd_dhcp_client_get_lease(client
, &lease
);
1057 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
1059 sd_dhcp_lease_unref(link
->dhcp_lease
);
1060 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
1063 if (link
->network
->dhcp_use_mtu
) {
1066 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
1068 r
= link_set_mtu(link
, mtu
);
1070 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
1074 if (link
->network
->dhcp_use_hostname
) {
1075 const char *dhcpname
= NULL
;
1076 _cleanup_free_
char *hostname
= NULL
;
1078 if (link
->network
->dhcp_hostname
)
1079 dhcpname
= link
->network
->dhcp_hostname
;
1081 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
1084 r
= shorten_overlong(dhcpname
, &hostname
);
1086 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
1088 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
1092 r
= manager_set_hostname(link
->manager
, hostname
);
1094 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
1098 if (link
->network
->dhcp_use_timezone
) {
1099 const char *tz
= NULL
;
1101 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
1104 r
= manager_set_timezone(link
->manager
, tz
);
1106 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
1110 if (link
->dhcp4_remove_messages
== 0) {
1111 r
= dhcp4_update_address(link
, true);
1115 log_link_debug(link
,
1116 "The link has previously assigned DHCPv4 address or routes. "
1117 "The newly assigned address and routes will set up after old ones are removed.");
1122 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
1125 link
->dhcp_lease_old
= TAKE_PTR(link
->dhcp_lease
);
1127 /* On IP address change, to keep the connectability, we would like to assign new address and
1128 * routes, and then release old lease. There are two possible success paths:
1130 * 1. new address and routes are configured.
1131 * -> handled by dhcp_release_old_lease() in dhcp4_route_handler().
1132 * 2. new address is configured and no route is requested.
1133 * -> handled by dhcp_release_old_lease() in dhcp4_address_handler().
1135 * On error in assigning new address and routes, then the link always enters to the failed
1136 * state. And link_enter_failed() leads to the DHCP client to be stopped. So,
1137 * dhcp_release_old_lease() will be also called by link_stop_clients().
1140 r
= dhcp_lease_acquired(client
, link
);
1142 /* If it fails, then the new address is not configured yet.
1143 * So, let's simply drop the old lease. */
1144 sd_dhcp_lease_unref(link
->dhcp_lease
);
1145 link
->dhcp_lease
= TAKE_PTR(link
->dhcp_lease_old
);
1146 (void) dhcp_lease_lost(link
);
1153 static int dhcp_server_is_deny_listed(Link
*link
, sd_dhcp_client
*client
) {
1154 sd_dhcp_lease
*lease
;
1155 struct in_addr addr
;
1159 assert(link
->network
);
1162 r
= sd_dhcp_client_get_lease(client
, &lease
);
1164 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1166 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1168 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1170 if (set_contains(link
->network
->dhcp_deny_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1171 log_struct(LOG_DEBUG
,
1172 LOG_LINK_INTERFACE(link
),
1173 LOG_LINK_MESSAGE(link
, "DHCPv4 IP '%u.%u.%u.%u' found in deny-listed IP addresses, ignoring offer",
1174 ADDRESS_FMT_VAL(addr
)));
1181 static int dhcp_server_is_allow_listed(Link
*link
, sd_dhcp_client
*client
) {
1182 sd_dhcp_lease
*lease
;
1183 struct in_addr addr
;
1187 assert(link
->network
);
1190 r
= sd_dhcp_client_get_lease(client
, &lease
);
1192 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1194 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1196 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1198 if (set_contains(link
->network
->dhcp_allow_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1199 log_struct(LOG_DEBUG
,
1200 LOG_LINK_INTERFACE(link
),
1201 LOG_LINK_MESSAGE(link
, "DHCPv4 IP '%u.%u.%u.%u' found in allow-listed IP addresses, accepting offer",
1202 ADDRESS_FMT_VAL(addr
)));
1209 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
1210 Link
*link
= userdata
;
1214 assert(link
->network
);
1215 assert(link
->manager
);
1217 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1221 case SD_DHCP_CLIENT_EVENT_STOP
:
1223 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
)) {
1224 assert(link
->ipv4ll
);
1226 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
1228 r
= sd_ipv4ll_start(link
->ipv4ll
);
1230 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1233 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1234 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1238 if (link
->dhcp_lease
) {
1239 if (link
->network
->dhcp_send_release
)
1240 (void) sd_dhcp_client_send_release(client
);
1242 r
= dhcp_lease_lost(link
);
1244 link_enter_failed(link
);
1250 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
1251 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1252 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1256 if (link
->dhcp_lease
) {
1257 r
= dhcp_lease_lost(link
);
1259 link_enter_failed(link
);
1265 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
1266 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1267 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1271 r
= dhcp_lease_ip_change(client
, link
);
1273 link_enter_failed(link
);
1278 case SD_DHCP_CLIENT_EVENT_RENEW
:
1279 r
= dhcp_lease_renew(client
, link
);
1281 link_enter_failed(link
);
1285 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1286 r
= dhcp_lease_acquired(client
, link
);
1288 link_enter_failed(link
);
1292 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1293 if (!set_isempty(link
->network
->dhcp_allow_listed_ip
)) {
1294 r
= dhcp_server_is_allow_listed(link
, client
);
1300 r
= dhcp_server_is_deny_listed(link
, client
);
1309 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1311 log_link_warning(link
, "DHCP unknown event: %i", event
);
1318 static int dhcp4_set_hostname(Link
*link
) {
1319 _cleanup_free_
char *hostname
= NULL
;
1325 if (!link
->network
->dhcp_send_hostname
)
1327 else if (link
->network
->dhcp_hostname
)
1328 hn
= link
->network
->dhcp_hostname
;
1330 r
= gethostname_strict(&hostname
);
1331 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1337 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1338 if (r
== -EINVAL
&& hostname
)
1339 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1340 log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1342 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
1347 static bool promote_secondaries_enabled(const char *ifname
) {
1348 _cleanup_free_
char *promote_secondaries_sysctl
= NULL
;
1349 char *promote_secondaries_path
;
1352 promote_secondaries_path
= strjoina("net/ipv4/conf/", ifname
, "/promote_secondaries");
1353 r
= sysctl_read(promote_secondaries_path
, &promote_secondaries_sysctl
);
1355 log_debug_errno(r
, "Cannot read sysctl %s", promote_secondaries_path
);
1359 truncate_nl(promote_secondaries_sysctl
);
1360 r
= parse_boolean(promote_secondaries_sysctl
);
1362 log_warning_errno(r
, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path
, promote_secondaries_sysctl
);
1366 /* dhcp4_set_promote_secondaries will ensure this interface has
1367 * the "promote_secondaries" option in the kernel set. If this sysctl
1368 * is not set DHCP will work only as long as the IP address does not
1369 * changes between leases. The kernel will remove all secondary IP
1370 * addresses of an interface otherwise. The way systemd-network works
1371 * is that the new IP of a lease is added as a secondary IP and when
1372 * the primary one expires it relies on the kernel to promote the
1373 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1375 int dhcp4_set_promote_secondaries(Link
*link
) {
1379 assert(link
->network
);
1380 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1382 /* check if the kernel has promote_secondaries enabled for our
1383 * interface. If it is not globally enabled or enabled for the
1384 * specific interface we must either enable it.
1386 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link
->ifname
))) {
1387 char *promote_secondaries_path
= NULL
;
1389 log_link_debug(link
, "promote_secondaries is unset, setting it");
1390 promote_secondaries_path
= strjoina("net/ipv4/conf/", link
->ifname
, "/promote_secondaries");
1391 r
= sysctl_write(promote_secondaries_path
, "1");
1393 log_link_warning_errno(link
, r
, "cannot set sysctl %s to 1", promote_secondaries_path
);
1400 int dhcp4_set_client_identifier(Link
*link
) {
1404 assert(link
->network
);
1405 assert(link
->dhcp_client
);
1407 switch (link
->network
->dhcp_client_identifier
) {
1408 case DHCP_CLIENT_ID_DUID
: {
1409 /* If configured, apply user specified DUID and IAID */
1410 const DUID
*duid
= link_get_duid(link
);
1412 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1413 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1414 link
->network
->iaid_set
,
1415 link
->network
->iaid
,
1418 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
1419 link
->network
->iaid_set
,
1420 link
->network
->iaid
,
1422 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1423 duid
->raw_data_len
);
1425 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1428 case DHCP_CLIENT_ID_DUID_ONLY
: {
1429 /* If configured, apply user specified DUID */
1430 const DUID
*duid
= link_get_duid(link
);
1432 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1433 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
1436 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
1438 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1439 duid
->raw_data_len
);
1441 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
1444 case DHCP_CLIENT_ID_MAC
:
1445 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1447 (const uint8_t *) &link
->mac
,
1450 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
1453 assert_not_reached("Unknown client identifier type.");
1459 int dhcp4_configure(Link
*link
) {
1460 sd_dhcp_option
*send_option
;
1461 void *request_options
;
1466 assert(link
->network
);
1467 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1469 if (!link
->dhcp_client
) {
1470 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1474 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
1476 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
1478 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to attach event: %m");
1481 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1482 (const uint8_t *) &link
->mac
,
1483 sizeof (link
->mac
), ARPHRD_ETHER
);
1485 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1487 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1489 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1491 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1493 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1495 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
,
1496 link
->network
->dhcp_broadcast
);
1498 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1501 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1503 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1506 if (link
->network
->dhcp_use_mtu
) {
1507 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1508 SD_DHCP_OPTION_INTERFACE_MTU
);
1510 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1513 /* NOTE: even if this variable is called "use", it also "sends" PRL
1514 * options, maybe there should be a different configuration variable
1515 * to send or not route options?. */
1516 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1517 * by default, so they don't need to be added here. */
1518 if (link
->network
->dhcp_use_routes
&& !link
->network
->dhcp_anonymize
) {
1519 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1520 SD_DHCP_OPTION_STATIC_ROUTE
);
1522 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1524 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1525 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1527 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1530 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
&& !link
->network
->dhcp_anonymize
) {
1531 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
);
1533 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1536 if (link
->network
->dhcp_use_ntp
) {
1537 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1539 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1542 if (link
->network
->dhcp_use_sip
) {
1543 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_SIP_SERVER
);
1545 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1548 if (link
->network
->dhcp_use_timezone
) {
1549 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1551 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1554 SET_FOREACH(request_options
, link
->network
->dhcp_request_options
, i
) {
1555 uint32_t option
= PTR_TO_UINT32(request_options
);
1557 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, option
);
1559 log_link_debug(link
, "DHCP4 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option
);
1564 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option
);
1567 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_options
, i
) {
1568 r
= sd_dhcp_client_add_option(link
->dhcp_client
, send_option
);
1572 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1575 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_vendor_options
, i
) {
1576 r
= sd_dhcp_client_add_vendor_option(link
->dhcp_client
, send_option
);
1580 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1583 r
= dhcp4_set_hostname(link
);
1587 if (link
->network
->dhcp_vendor_class_identifier
) {
1588 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1589 link
->network
->dhcp_vendor_class_identifier
);
1591 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1594 if (link
->network
->dhcp_mudurl
) {
1595 r
= sd_dhcp_client_set_mud_url(link
->dhcp_client
,
1596 link
->network
->dhcp_mudurl
);
1598 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1601 if (link
->network
->dhcp_user_class
) {
1602 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, (const char **) link
->network
->dhcp_user_class
);
1604 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1607 if (link
->network
->dhcp_client_port
) {
1608 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1610 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen port: %m");
1613 if (link
->network
->dhcp_max_attempts
> 0) {
1614 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1616 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1619 if (link
->network
->ip_service_type
> 0) {
1620 r
= sd_dhcp_client_set_service_type(link
->dhcp_client
, link
->network
->ip_service_type
);
1622 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IP service type: %m");
1625 if (link
->network
->dhcp_fallback_lease_lifetime
> 0) {
1626 r
= sd_dhcp_client_set_fallback_lease_lifetime(link
->dhcp_client
, link
->network
->dhcp_fallback_lease_lifetime
);
1628 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1631 if (link
->network
->dhcp_send_decline
) {
1632 r
= configure_dhcpv4_duplicate_address_detection(link
);
1634 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to configure service type: %m");
1637 return dhcp4_set_client_identifier(link
);
1640 int config_parse_dhcp_max_attempts(
1642 const char *filename
,
1644 const char *section
,
1645 unsigned section_line
,
1652 Network
*network
= data
;
1660 if (isempty(rvalue
)) {
1661 network
->dhcp_max_attempts
= 0;
1665 if (streq(rvalue
, "infinity")) {
1666 network
->dhcp_max_attempts
= (uint64_t) -1;
1670 r
= safe_atou64(rvalue
, &a
);
1672 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1673 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1678 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1679 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1683 network
->dhcp_max_attempts
= a
;
1688 int config_parse_dhcp_acl_ip_address(
1690 const char *filename
,
1692 const char *section
,
1693 unsigned section_line
,
1700 Network
*network
= data
;
1709 acl
= STR_IN_SET(lvalue
, "DenyList", "BlackList") ? &network
->dhcp_deny_listed_ip
: &network
->dhcp_allow_listed_ip
;
1711 if (isempty(rvalue
)) {
1712 *acl
= set_free(*acl
);
1716 for (const char *p
= rvalue
;;) {
1717 _cleanup_free_
char *n
= NULL
;
1718 union in_addr_union ip
;
1720 r
= extract_first_word(&p
, &n
, NULL
, 0);
1722 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1723 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1730 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1732 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1733 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue
, n
);
1737 r
= set_ensure_put(acl
, NULL
, UINT32_TO_PTR(ip
.in
.s_addr
));
1739 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1740 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue
, n
);
1746 int config_parse_dhcp_ip_service_type(
1748 const char *filename
,
1750 const char *section
,
1751 unsigned section_line
,
1762 if (streq(rvalue
, "CS4"))
1763 *((int *)data
) = IPTOS_CLASS_CS4
;
1764 else if (streq(rvalue
, "CS6"))
1765 *((int *)data
) = IPTOS_CLASS_CS6
;
1767 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1768 "Failed to parse IPServiceType type '%s', ignoring.", rvalue
);
1773 int config_parse_dhcp_mud_url(
1775 const char *filename
,
1777 const char *section
,
1778 unsigned section_line
,
1785 _cleanup_free_
char *unescaped
= NULL
;
1786 Network
*network
= data
;
1793 if (isempty(rvalue
)) {
1794 network
->dhcp_mudurl
= mfree(network
->dhcp_mudurl
);
1798 r
= cunescape(rvalue
, 0, &unescaped
);
1800 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1801 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue
);
1805 if (!http_url_is_valid(unescaped
) || strlen(unescaped
) > 255) {
1806 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1807 "Failed to parse MUD URL '%s', ignoring: %m", rvalue
);
1812 return free_and_strdup_warn(&network
->dhcp_mudurl
, unescaped
);
1815 int config_parse_dhcp_fallback_lease_lifetime(const char *unit
,
1816 const char *filename
,
1818 const char *section
,
1819 unsigned section_line
,
1825 Network
*network
= userdata
;
1834 if (isempty(rvalue
)) {
1835 network
->dhcp_fallback_lease_lifetime
= 0;
1839 /* We accept only "forever" or "infinity". */
1840 if (STR_IN_SET(rvalue
, "forever", "infinity"))
1841 k
= CACHE_INFO_INFINITY_LIFE_TIME
;
1843 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1844 "Invalid LeaseLifetime= value, ignoring: %s", rvalue
);
1848 network
->dhcp_fallback_lease_lifetime
= k
;
1853 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1854 [DHCP_CLIENT_ID_MAC
] = "mac",
1855 [DHCP_CLIENT_ID_DUID
] = "duid",
1856 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1859 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1860 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1861 "Failed to parse client identifier type");