1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <netinet/in.h>
4 #include <netinet/ip.h>
6 #include <linux/if_arp.h>
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-address.h"
15 #include "networkd-dhcp4.h"
16 #include "networkd-link.h"
17 #include "networkd-manager.h"
18 #include "networkd-network.h"
19 #include "string-table.h"
20 #include "string-util.h"
21 #include "sysctl-util.h"
24 static int dhcp4_update_address(Link
*link
, bool announce
);
25 static int dhcp4_remove_all(Link
*link
);
27 static int dhcp4_release_old_lease(Link
*link
) {
33 if (!link
->dhcp_address_old
&& set_isempty(link
->dhcp_routes_old
))
36 log_link_debug(link
, "Removing old DHCPv4 address and routes.");
40 SET_FOREACH(route
, link
->dhcp_routes_old
) {
41 k
= route_remove(route
, NULL
, link
, NULL
);
46 if (link
->dhcp_address_old
) {
47 k
= address_remove(link
->dhcp_address_old
, link
, NULL
);
55 static void dhcp4_check_ready(Link
*link
) {
58 if (link
->network
->dhcp_send_decline
&& !link
->dhcp4_address_bind
)
61 if (link
->dhcp4_messages
> 0)
64 link
->dhcp4_configured
= true;
66 /* New address and routes are configured now. Let's release old lease. */
67 r
= dhcp4_release_old_lease(link
);
69 link_enter_failed(link
);
73 link_check_ready(link
);
76 static int dhcp4_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
80 assert(link
->dhcp4_messages
> 0);
82 link
->dhcp4_messages
--;
84 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
87 r
= sd_netlink_message_get_errno(m
);
88 if (r
== -ENETUNREACH
&& !link
->dhcp4_route_retrying
) {
90 /* It seems kernel does not support that the prefix route cannot be configured with
91 * route table. Let's once drop the config and reconfigure them later. */
93 log_link_message_debug_errno(link
, m
, r
, "Could not set DHCPv4 route, retrying later");
94 link
->dhcp4_route_failed
= true;
95 link
->manager
->dhcp4_prefix_root_cannot_set_table
= true;
96 } else if (r
< 0 && r
!= -EEXIST
) {
97 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 route");
98 link_enter_failed(link
);
102 if (link
->dhcp4_messages
== 0 && link
->dhcp4_route_failed
) {
103 link
->dhcp4_route_failed
= false;
104 link
->dhcp4_route_retrying
= true;
106 r
= dhcp4_remove_all(link
);
108 link_enter_failed(link
);
112 dhcp4_check_ready(link
);
117 static int route_scope_from_address(const Route
*route
, const struct in_addr
*self_addr
) {
121 if (in4_addr_is_localhost(&route
->dst
.in
) ||
122 (!in4_addr_is_null(self_addr
) && in4_addr_equal(&route
->dst
.in
, self_addr
)))
123 return RT_SCOPE_HOST
;
124 else if (in4_addr_is_null(&route
->gw
.in
))
125 return RT_SCOPE_LINK
;
127 return RT_SCOPE_UNIVERSE
;
130 static bool link_prefixroute(Link
*link
) {
131 return !link
->network
->dhcp_route_table_set
||
132 link
->network
->dhcp_route_table
== RT_TABLE_MAIN
||
133 link
->manager
->dhcp4_prefix_root_cannot_set_table
;
136 static int dhcp_route_configure(Route
*route
, Link
*link
) {
143 r
= route_configure(route
, link
, dhcp4_route_handler
, &ret
);
145 return log_link_error_errno(link
, r
, "Failed to set DHCPv4 route: %m");
147 link
->dhcp4_messages
++;
149 r
= set_ensure_put(&link
->dhcp_routes
, &route_hash_ops
, ret
);
151 return log_link_error_errno(link
, r
, "Failed to store DHCPv4 route: %m");
153 (void) set_remove(link
->dhcp_routes_old
, ret
);
158 static int link_set_dns_routes(Link
*link
, const struct in_addr
*address
) {
159 const struct in_addr
*dns
;
164 assert(link
->dhcp_lease
);
165 assert(link
->network
);
167 if (!link
->network
->dhcp_use_dns
||
168 !link
->network
->dhcp_routes_to_dns
)
171 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &dns
);
172 if (IN_SET(n
, 0, -ENODATA
))
175 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
177 table
= link_get_dhcp_route_table(link
);
179 for (i
= 0; i
< n
; i
++) {
180 _cleanup_(route_freep
) Route
*route
= NULL
;
182 r
= route_new(&route
);
184 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
186 /* Set routes to DNS servers. */
188 route
->family
= AF_INET
;
189 route
->dst
.in
= dns
[i
];
190 route
->dst_prefixlen
= 32;
191 route
->prefsrc
.in
= *address
;
192 route
->scope
= RT_SCOPE_LINK
;
193 route
->protocol
= RTPROT_DHCP
;
194 route
->priority
= link
->network
->dhcp_route_metric
;
195 route
->table
= table
;
197 r
= dhcp_route_configure(route
, link
);
199 return log_link_error_errno(link
, r
, "Could not set route to DNS server: %m");
205 static int dhcp_prefix_route_from_lease(
206 const sd_dhcp_lease
*lease
,
208 const struct in_addr
*address
,
212 struct in_addr netmask
;
215 r
= sd_dhcp_lease_get_netmask((sd_dhcp_lease
*) lease
, &netmask
);
219 r
= route_new(&route
);
223 route
->family
= AF_INET
;
224 route
->dst
.in
.s_addr
= address
->s_addr
& netmask
.s_addr
;
225 route
->dst_prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
226 route
->prefsrc
.in
= *address
;
227 route
->scope
= RT_SCOPE_LINK
;
228 route
->protocol
= RTPROT_DHCP
;
229 route
->table
= table
;
234 static int link_set_dhcp_routes(Link
*link
) {
235 _cleanup_free_ sd_dhcp_route
**static_routes
= NULL
;
236 bool classless_route
= false, static_route
= false;
237 struct in_addr address
;
244 if (!link
->dhcp_lease
) /* link went down while we configured the IP addresses? */
247 if (!link
->network
) /* link went down while we configured the IP addresses? */
250 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
251 /* During configuring addresses, the link lost its carrier. As networkd is dropping
252 * the addresses now, let's not configure the routes either. */
255 while ((rt
= set_steal_first(link
->dhcp_routes
))) {
256 r
= set_ensure_put(&link
->dhcp_routes_old
, &route_hash_ops
, rt
);
258 return log_link_error_errno(link
, r
, "Failed to store old DHCPv4 route: %m");
261 table
= link_get_dhcp_route_table(link
);
263 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
265 return log_link_warning_errno(link
, r
, "DHCP error: could not get address: %m");
267 if (!link_prefixroute(link
)) {
268 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
270 r
= dhcp_prefix_route_from_lease(link
->dhcp_lease
, table
, &address
, &prefix_route
);
272 return log_link_error_errno(link
, r
, "Could not create prefix route: %m");
274 r
= dhcp_route_configure(prefix_route
, link
);
276 return log_link_error_errno(link
, r
, "Could not set prefix route: %m");
279 n
= sd_dhcp_lease_get_routes(link
->dhcp_lease
, &static_routes
);
281 log_link_debug_errno(link
, n
, "DHCP: No routes received from DHCP server: %m");
283 return log_link_error_errno(link
, n
, "DHCP: could not get routes: %m");
285 for (int i
= 0; i
< n
; i
++) {
286 switch (sd_dhcp_route_get_option(static_routes
[i
])) {
287 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
288 classless_route
= true;
290 case SD_DHCP_OPTION_STATIC_ROUTE
:
296 if (link
->network
->dhcp_use_routes
) {
297 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
298 * the DHCP client MUST ignore the Static Routes option. */
299 if (classless_route
&& static_route
)
300 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring static-route option");
302 for (int i
= 0; i
< n
; i
++) {
303 _cleanup_(route_freep
) Route
*route
= NULL
;
305 if (classless_route
&&
306 sd_dhcp_route_get_option(static_routes
[i
]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
)
309 r
= route_new(&route
);
311 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
313 route
->family
= AF_INET
;
314 route
->protocol
= RTPROT_DHCP
;
315 route
->gw_family
= AF_INET
;
316 assert_se(sd_dhcp_route_get_gateway(static_routes
[i
], &route
->gw
.in
) >= 0);
317 assert_se(sd_dhcp_route_get_destination(static_routes
[i
], &route
->dst
.in
) >= 0);
318 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &route
->dst_prefixlen
) >= 0);
319 route
->priority
= link
->network
->dhcp_route_metric
;
320 route
->table
= table
;
321 route
->mtu
= link
->network
->dhcp_route_mtu
;
322 route
->scope
= route_scope_from_address(route
, &address
);
323 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
324 route
->prefsrc
.in
= address
;
326 if (set_contains(link
->dhcp_routes
, route
))
329 r
= dhcp_route_configure(route
, link
);
331 return log_link_error_errno(link
, r
, "Could not set route: %m");
335 if (link
->network
->dhcp_use_gateway
) {
336 const struct in_addr
*router
;
338 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
339 if (IN_SET(r
, 0, -ENODATA
))
340 log_link_info(link
, "DHCP: No gateway received from DHCP server.");
342 return log_link_error_errno(link
, r
, "DHCP error: could not get gateway: %m");
343 else if (in4_addr_is_null(&router
[0]))
344 log_link_info(link
, "DHCP: Received gateway is null.");
345 else if (classless_route
)
346 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
347 * a Router option, the DHCP client MUST ignore the Router option. */
348 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring router option");
350 _cleanup_(route_freep
) Route
*route
= NULL
, *route_gw
= NULL
;
352 r
= route_new(&route_gw
);
354 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
356 /* The dhcp netmask may mask out the gateway. Add an explicit
357 * route for the gw host so that we can route no matter the
358 * netmask or existing kernel route tables. */
359 route_gw
->family
= AF_INET
;
360 route_gw
->dst
.in
= router
[0];
361 route_gw
->dst_prefixlen
= 32;
362 route_gw
->prefsrc
.in
= address
;
363 route_gw
->scope
= RT_SCOPE_LINK
;
364 route_gw
->protocol
= RTPROT_DHCP
;
365 route_gw
->priority
= link
->network
->dhcp_route_metric
;
366 route_gw
->table
= table
;
367 route_gw
->mtu
= link
->network
->dhcp_route_mtu
;
369 r
= dhcp_route_configure(route_gw
, link
);
371 return log_link_error_errno(link
, r
, "Could not set host route: %m");
373 r
= route_new(&route
);
375 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
377 route
->family
= AF_INET
;
378 route
->gw_family
= AF_INET
;
379 route
->gw
.in
= router
[0];
380 route
->prefsrc
.in
= address
;
381 route
->protocol
= RTPROT_DHCP
;
382 route
->priority
= link
->network
->dhcp_route_metric
;
383 route
->table
= table
;
384 route
->mtu
= link
->network
->dhcp_route_mtu
;
386 r
= dhcp_route_configure(route
, link
);
388 return log_link_error_errno(link
, r
, "Could not set router: %m");
390 HASHMAP_FOREACH(rt
, link
->network
->routes_by_section
) {
391 if (!rt
->gateway_from_dhcp_or_ra
)
394 if (rt
->gw_family
!= AF_INET
)
397 rt
->gw
.in
= router
[0];
398 if (!rt
->protocol_set
)
399 rt
->protocol
= RTPROT_DHCP
;
400 if (!rt
->priority_set
)
401 rt
->priority
= link
->network
->dhcp_route_metric
;
405 rt
->mtu
= link
->network
->dhcp_route_mtu
;
407 r
= dhcp_route_configure(rt
, link
);
409 return log_link_error_errno(link
, r
, "Could not set gateway: %m");
414 return link_set_dns_routes(link
, &address
);
417 static int dhcp_reset_mtu(Link
*link
) {
423 if (!link
->network
->dhcp_use_mtu
)
426 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
430 return log_link_error_errno(link
, r
, "DHCP error: failed to get MTU from lease: %m");
432 if (link
->original_mtu
== mtu
)
435 r
= link_set_mtu(link
, link
->original_mtu
);
437 return log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
442 static int dhcp_reset_hostname(Link
*link
) {
443 const char *hostname
;
448 if (!link
->network
->dhcp_use_hostname
)
451 hostname
= link
->network
->dhcp_hostname
;
453 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
458 /* If a hostname was set due to the lease, then unset it now. */
459 r
= manager_set_hostname(link
->manager
, NULL
);
461 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
466 static int dhcp4_remove_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
471 assert(link
->dhcp4_remove_messages
> 0);
473 link
->dhcp4_remove_messages
--;
475 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
478 r
= sd_netlink_message_get_errno(m
);
479 if (r
< 0 && r
!= -ESRCH
)
480 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 route, ignoring");
482 if (link
->dhcp4_remove_messages
== 0) {
483 r
= dhcp4_update_address(link
, false);
485 link_enter_failed(link
);
491 static int dhcp4_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
496 assert(link
->dhcp4_remove_messages
> 0);
498 link
->dhcp4_remove_messages
--;
500 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
503 r
= sd_netlink_message_get_errno(m
);
504 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
505 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 address, ignoring");
507 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
509 if (link
->dhcp4_remove_messages
== 0) {
510 r
= dhcp4_update_address(link
, false);
512 link_enter_failed(link
);
518 static int dhcp4_remove_all(Link
*link
) {
524 SET_FOREACH(route
, link
->dhcp_routes
) {
525 k
= route_remove(route
, NULL
, link
, dhcp4_remove_route_handler
);
529 link
->dhcp4_remove_messages
++;
532 if (link
->dhcp_address
) {
533 k
= address_remove(link
->dhcp_address
, link
, dhcp4_remove_address_handler
);
537 link
->dhcp4_remove_messages
++;
543 static int dhcp_lease_lost(Link
*link
) {
547 assert(link
->dhcp_lease
);
549 log_link_info(link
, "DHCP lease lost");
551 link
->dhcp4_configured
= false;
553 /* dhcp_lease_lost() may be called during renewing IP address. */
554 k
= dhcp4_release_old_lease(link
);
558 k
= dhcp4_remove_all(link
);
562 k
= dhcp_reset_mtu(link
);
566 k
= dhcp_reset_hostname(link
);
570 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
573 (void) sd_ipv4acd_stop(link
->dhcp_acd
);
578 static void dhcp_address_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
579 _cleanup_free_
char *pretty
= NULL
;
580 union in_addr_union address
= {};
590 case SD_IPV4ACD_EVENT_STOP
:
591 log_link_debug(link
, "Stopping ACD client for DHCP4...");
594 case SD_IPV4ACD_EVENT_BIND
:
596 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
597 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
598 log_link_debug(link
, "Successfully claimed DHCP4 address %s", strna(pretty
));
600 link
->dhcp4_address_bind
= true;
601 dhcp4_check_ready(link
);
604 case SD_IPV4ACD_EVENT_CONFLICT
:
605 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
606 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
607 log_link_warning(link
, "DAD conflict. Dropping DHCP4 address %s", strna(pretty
));
609 r
= sd_dhcp_client_send_decline(link
->dhcp_client
);
611 log_link_warning_errno(link
, r
, "Failed to send DHCP DECLINE, ignoring: %m");
613 if (link
->dhcp_lease
) {
614 r
= dhcp_lease_lost(link
);
616 link_enter_failed(link
);
621 assert_not_reached("Invalid IPv4ACD event.");
624 (void) sd_ipv4acd_stop(acd
);
629 static int dhcp4_configure_dad(Link
*link
) {
633 assert(link
->manager
);
634 assert(link
->network
);
636 if (!link
->network
->dhcp_send_decline
)
639 if (!link
->dhcp_acd
) {
640 r
= sd_ipv4acd_new(&link
->dhcp_acd
);
644 r
= sd_ipv4acd_attach_event(link
->dhcp_acd
, link
->manager
->event
, 0);
649 r
= sd_ipv4acd_set_ifindex(link
->dhcp_acd
, link
->ifindex
);
653 r
= sd_ipv4acd_set_mac(link
->dhcp_acd
, &link
->hw_addr
.addr
.ether
);
660 static int dhcp4_dad_update_mac(Link
*link
) {
669 running
= sd_ipv4acd_is_running(link
->dhcp_acd
);
671 r
= sd_ipv4acd_stop(link
->dhcp_acd
);
675 r
= sd_ipv4acd_set_mac(link
->dhcp_acd
, &link
->hw_addr
.addr
.ether
);
680 r
= sd_ipv4acd_start(link
->dhcp_acd
, true);
688 static int dhcp4_start_acd(Link
*link
) {
689 union in_addr_union addr
;
693 if (!link
->network
->dhcp_send_decline
)
696 if (!link
->dhcp_lease
)
699 (void) sd_ipv4acd_stop(link
->dhcp_acd
);
701 link
->dhcp4_address_bind
= false;
703 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &addr
.in
);
707 r
= sd_ipv4acd_get_address(link
->dhcp_acd
, &old
);
711 r
= sd_ipv4acd_set_address(link
->dhcp_acd
, &addr
.in
);
715 r
= sd_ipv4acd_set_callback(link
->dhcp_acd
, dhcp_address_on_acd
, link
);
720 _cleanup_free_
char *pretty
= NULL
;
722 (void) in_addr_to_string(AF_INET
, &addr
, &pretty
);
723 log_link_debug(link
, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty
));
726 r
= sd_ipv4acd_start(link
->dhcp_acd
, !in4_addr_equal(&addr
.in
, &old
));
733 static int dhcp4_address_ready_callback(Address
*address
) {
739 link
= address
->link
;
741 /* Do not call this again. */
742 address
->callback
= NULL
;
744 r
= link_set_dhcp_routes(link
);
748 /* Reconfigure static routes as kernel may remove some routes when lease expires. */
749 r
= link_set_routes(link
);
753 r
= dhcp4_start_acd(link
);
755 return log_link_error_errno(link
, r
, "Failed to start IPv4ACD for DHCP4 address: %m");
757 dhcp4_check_ready(link
);
761 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
766 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
769 r
= sd_netlink_message_get_errno(m
);
770 if (r
< 0 && r
!= -EEXIST
) {
771 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 address");
772 link_enter_failed(link
);
775 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
777 if (address_is_ready(link
->dhcp_address
)) {
778 r
= dhcp4_address_ready_callback(link
->dhcp_address
);
780 link_enter_failed(link
);
784 link
->dhcp_address
->callback
= dhcp4_address_ready_callback
;
789 static int dhcp4_update_address(Link
*link
, bool announce
) {
790 _cleanup_(address_freep
) Address
*addr
= NULL
;
791 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
792 struct in_addr address
, netmask
;
798 assert(link
->network
);
800 if (!link
->dhcp_lease
)
803 link_set_state(link
, LINK_STATE_CONFIGURING
);
804 link
->dhcp4_configured
= false;
806 /* address_handler calls link_set_routes() and link_set_nexthop(). Before they are called, the
807 * related flags must be cleared. Otherwise, the link becomes configured state before routes
809 link
->static_routes_configured
= false;
810 link
->static_nexthops_configured
= false;
812 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
814 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
816 r
= sd_dhcp_lease_get_netmask(link
->dhcp_lease
, &netmask
);
818 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
820 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
821 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
823 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
826 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
829 const struct in_addr
*router
;
831 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
832 if (r
< 0 && r
!= -ENODATA
)
833 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
835 if (r
> 0 && !in4_addr_is_null(&router
[0]))
837 LOG_LINK_INTERFACE(link
),
838 LOG_LINK_MESSAGE(link
, "DHCPv4 address "IPV4_ADDRESS_FMT_STR
"/%u via "IPV4_ADDRESS_FMT_STR
,
839 IPV4_ADDRESS_FMT_VAL(address
),
841 IPV4_ADDRESS_FMT_VAL(router
[0])),
842 "ADDRESS="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(address
),
843 "PREFIXLEN=%u", prefixlen
,
844 "GATEWAY="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(router
[0]));
847 LOG_LINK_INTERFACE(link
),
848 LOG_LINK_MESSAGE(link
, "DHCPv4 address "IPV4_ADDRESS_FMT_STR
"/%u",
849 IPV4_ADDRESS_FMT_VAL(address
),
851 "ADDRESS="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(address
),
852 "PREFIXLEN=%u", prefixlen
);
855 r
= address_new(&addr
);
859 addr
->family
= AF_INET
;
860 addr
->in_addr
.in
.s_addr
= address
.s_addr
;
861 addr
->cinfo
.ifa_prefered
= lifetime
;
862 addr
->cinfo
.ifa_valid
= lifetime
;
863 addr
->prefixlen
= prefixlen
;
864 addr
->broadcast
.s_addr
= address
.s_addr
| ~netmask
.s_addr
;
865 SET_FLAG(addr
->flags
, IFA_F_NOPREFIXROUTE
, !link_prefixroute(link
));
867 /* allow reusing an existing address and simply update its lifetime
868 * in case it already exists */
869 r
= address_configure(addr
, link
, dhcp4_address_handler
, true, &ret
);
871 return log_link_error_errno(link
, r
, "Failed to set DHCPv4 address: %m");
873 if (!address_equal(link
->dhcp_address
, ret
))
874 link
->dhcp_address_old
= link
->dhcp_address
;
875 link
->dhcp_address
= ret
;
880 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
881 sd_dhcp_lease
*lease
;
887 r
= sd_dhcp_client_get_lease(client
, &lease
);
889 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
891 sd_dhcp_lease_unref(link
->dhcp_lease
);
892 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
895 return dhcp4_update_address(link
, false);
898 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
899 sd_dhcp_lease
*lease
;
905 r
= sd_dhcp_client_get_lease(client
, &lease
);
907 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
909 sd_dhcp_lease_unref(link
->dhcp_lease
);
910 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
913 if (link
->network
->dhcp_use_mtu
) {
916 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
918 r
= link_set_mtu(link
, mtu
);
920 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
924 if (link
->network
->dhcp_use_hostname
) {
925 const char *dhcpname
= NULL
;
926 _cleanup_free_
char *hostname
= NULL
;
928 if (link
->network
->dhcp_hostname
)
929 dhcpname
= link
->network
->dhcp_hostname
;
931 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
934 r
= shorten_overlong(dhcpname
, &hostname
);
936 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
938 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
942 r
= manager_set_hostname(link
->manager
, hostname
);
944 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
948 if (link
->network
->dhcp_use_timezone
) {
949 const char *tz
= NULL
;
951 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
954 r
= manager_set_timezone(link
->manager
, tz
);
956 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
960 if (link
->dhcp4_remove_messages
== 0) {
961 r
= dhcp4_update_address(link
, true);
966 "The link has previously assigned DHCPv4 address or routes. "
967 "The newly assigned address and routes will set up after old ones are removed.");
972 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
975 r
= dhcp_lease_acquired(client
, link
);
977 (void) dhcp_lease_lost(link
);
982 static int dhcp_server_is_deny_listed(Link
*link
, sd_dhcp_client
*client
) {
983 sd_dhcp_lease
*lease
;
988 assert(link
->network
);
991 r
= sd_dhcp_client_get_lease(client
, &lease
);
993 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
995 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
997 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
999 if (set_contains(link
->network
->dhcp_deny_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1000 log_struct(LOG_DEBUG
,
1001 LOG_LINK_INTERFACE(link
),
1002 LOG_LINK_MESSAGE(link
, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR
" found in deny-list, ignoring offer",
1003 IPV4_ADDRESS_FMT_VAL(addr
)));
1010 static int dhcp_server_is_allow_listed(Link
*link
, sd_dhcp_client
*client
) {
1011 sd_dhcp_lease
*lease
;
1012 struct in_addr addr
;
1016 assert(link
->network
);
1019 r
= sd_dhcp_client_get_lease(client
, &lease
);
1021 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1023 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1025 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1027 if (set_contains(link
->network
->dhcp_allow_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1028 log_struct(LOG_DEBUG
,
1029 LOG_LINK_INTERFACE(link
),
1030 LOG_LINK_MESSAGE(link
, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR
" found in allow-list, accepting offer",
1031 IPV4_ADDRESS_FMT_VAL(addr
)));
1038 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
1039 Link
*link
= userdata
;
1043 assert(link
->network
);
1044 assert(link
->manager
);
1046 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1050 case SD_DHCP_CLIENT_EVENT_STOP
:
1052 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
)) {
1053 assert(link
->ipv4ll
);
1055 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
1057 r
= sd_ipv4ll_start(link
->ipv4ll
);
1059 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1062 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1063 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1067 if (link
->dhcp_lease
) {
1068 if (link
->network
->dhcp_send_release
) {
1069 r
= sd_dhcp_client_send_release(client
);
1071 log_link_warning_errno(link
, r
, "Failed to send DHCP RELEASE, ignoring: %m");
1074 r
= dhcp_lease_lost(link
);
1076 link_enter_failed(link
);
1082 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
1083 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1084 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1088 if (link
->dhcp_lease
) {
1089 r
= dhcp_lease_lost(link
);
1091 link_enter_failed(link
);
1097 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
1098 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1099 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1103 r
= dhcp_lease_ip_change(client
, link
);
1105 link_enter_failed(link
);
1110 case SD_DHCP_CLIENT_EVENT_RENEW
:
1111 r
= dhcp_lease_renew(client
, link
);
1113 link_enter_failed(link
);
1117 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1118 r
= dhcp_lease_acquired(client
, link
);
1120 link_enter_failed(link
);
1124 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1125 if (!set_isempty(link
->network
->dhcp_allow_listed_ip
)) {
1126 r
= dhcp_server_is_allow_listed(link
, client
);
1132 r
= dhcp_server_is_deny_listed(link
, client
);
1141 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1143 log_link_warning(link
, "DHCP unknown event: %i", event
);
1150 static int dhcp4_set_hostname(Link
*link
) {
1151 _cleanup_free_
char *hostname
= NULL
;
1157 if (!link
->network
->dhcp_send_hostname
)
1159 else if (link
->network
->dhcp_hostname
)
1160 hn
= link
->network
->dhcp_hostname
;
1162 r
= gethostname_strict(&hostname
);
1163 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1169 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1170 if (r
== -EINVAL
&& hostname
)
1171 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1172 log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1174 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
1179 static bool promote_secondaries_enabled(const char *ifname
) {
1180 _cleanup_free_
char *promote_secondaries_sysctl
= NULL
;
1181 char *promote_secondaries_path
;
1184 promote_secondaries_path
= strjoina("net/ipv4/conf/", ifname
, "/promote_secondaries");
1185 r
= sysctl_read(promote_secondaries_path
, &promote_secondaries_sysctl
);
1187 log_debug_errno(r
, "Cannot read sysctl %s", promote_secondaries_path
);
1191 truncate_nl(promote_secondaries_sysctl
);
1192 r
= parse_boolean(promote_secondaries_sysctl
);
1194 log_warning_errno(r
, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path
, promote_secondaries_sysctl
);
1198 /* dhcp4_set_promote_secondaries will ensure this interface has
1199 * the "promote_secondaries" option in the kernel set. If this sysctl
1200 * is not set DHCP will work only as long as the IP address does not
1201 * changes between leases. The kernel will remove all secondary IP
1202 * addresses of an interface otherwise. The way systemd-network works
1203 * is that the new IP of a lease is added as a secondary IP and when
1204 * the primary one expires it relies on the kernel to promote the
1205 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1207 static int dhcp4_set_promote_secondaries(Link
*link
) {
1212 /* check if the kernel has promote_secondaries enabled for our
1213 * interface. If it is not globally enabled or enabled for the
1214 * specific interface we must either enable it.
1216 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link
->ifname
))) {
1217 char *promote_secondaries_path
= NULL
;
1219 log_link_debug(link
, "promote_secondaries is unset, setting it");
1220 promote_secondaries_path
= strjoina("net/ipv4/conf/", link
->ifname
, "/promote_secondaries");
1221 r
= sysctl_write(promote_secondaries_path
, "1");
1223 log_link_warning_errno(link
, r
, "cannot set sysctl %s to 1", promote_secondaries_path
);
1230 static int dhcp4_set_client_identifier(Link
*link
) {
1234 assert(link
->network
);
1235 assert(link
->dhcp_client
);
1237 switch (link
->network
->dhcp_client_identifier
) {
1238 case DHCP_CLIENT_ID_DUID
: {
1239 /* If configured, apply user specified DUID and IAID */
1240 const DUID
*duid
= link_get_duid(link
);
1242 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1243 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1244 link
->network
->iaid_set
,
1245 link
->network
->iaid
,
1248 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
1249 link
->network
->iaid_set
,
1250 link
->network
->iaid
,
1252 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1253 duid
->raw_data_len
);
1255 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1258 case DHCP_CLIENT_ID_DUID_ONLY
: {
1259 /* If configured, apply user specified DUID */
1260 const DUID
*duid
= link_get_duid(link
);
1262 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1263 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
1266 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
1268 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1269 duid
->raw_data_len
);
1271 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
1274 case DHCP_CLIENT_ID_MAC
: {
1275 const uint8_t *hw_addr
= link
->hw_addr
.addr
.bytes
;
1276 size_t hw_addr_len
= link
->hw_addr
.length
;
1278 if (link
->iftype
== ARPHRD_INFINIBAND
&& hw_addr_len
== INFINIBAND_ALEN
) {
1279 /* set_client_id expects only last 8 bytes of an IB address */
1280 hw_addr
+= INFINIBAND_ALEN
- 8;
1281 hw_addr_len
-= INFINIBAND_ALEN
- 8;
1284 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1289 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
1293 assert_not_reached("Unknown client identifier type.");
1299 static int dhcp4_init(Link
*link
) {
1304 if (link
->dhcp_client
)
1307 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1311 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, link
->manager
->event
, 0);
1318 int dhcp4_configure(Link
*link
) {
1319 sd_dhcp_option
*send_option
;
1320 void *request_options
;
1324 assert(link
->network
);
1326 if (!link_dhcp4_enabled(link
))
1329 r
= dhcp4_set_promote_secondaries(link
);
1333 r
= dhcp4_init(link
);
1335 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to initialize DHCP4 client: %m");
1337 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1338 link
->hw_addr
.addr
.bytes
,
1339 link
->bcast_addr
.length
> 0 ? link
->bcast_addr
.addr
.bytes
: NULL
,
1340 link
->hw_addr
.length
, link
->iftype
);
1342 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1344 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1346 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1348 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1350 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1352 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
,
1353 link
->network
->dhcp_broadcast
);
1355 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1358 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1360 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1363 if (link
->network
->dhcp_use_mtu
) {
1364 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1365 SD_DHCP_OPTION_INTERFACE_MTU
);
1367 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1370 /* NOTE: even if this variable is called "use", it also "sends" PRL
1371 * options, maybe there should be a different configuration variable
1372 * to send or not route options?. */
1373 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1374 * by default, so they don't need to be added here. */
1375 if (link
->network
->dhcp_use_routes
&& !link
->network
->dhcp_anonymize
) {
1376 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1377 SD_DHCP_OPTION_STATIC_ROUTE
);
1379 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1381 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1382 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1384 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1387 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
&& !link
->network
->dhcp_anonymize
) {
1388 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
);
1390 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1393 if (link
->network
->dhcp_use_ntp
) {
1394 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1396 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1399 if (link
->network
->dhcp_use_sip
) {
1400 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_SIP_SERVER
);
1402 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1405 if (link
->network
->dhcp_use_timezone
) {
1406 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1408 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1411 SET_FOREACH(request_options
, link
->network
->dhcp_request_options
) {
1412 uint32_t option
= PTR_TO_UINT32(request_options
);
1414 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, option
);
1416 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option
);
1419 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_options
) {
1420 r
= sd_dhcp_client_add_option(link
->dhcp_client
, send_option
);
1424 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1427 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_vendor_options
) {
1428 r
= sd_dhcp_client_add_vendor_option(link
->dhcp_client
, send_option
);
1432 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1435 r
= dhcp4_set_hostname(link
);
1439 if (link
->network
->dhcp_vendor_class_identifier
) {
1440 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1441 link
->network
->dhcp_vendor_class_identifier
);
1443 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1446 if (link
->network
->dhcp_mudurl
) {
1447 r
= sd_dhcp_client_set_mud_url(link
->dhcp_client
,
1448 link
->network
->dhcp_mudurl
);
1450 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1453 if (link
->network
->dhcp_user_class
) {
1454 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, (const char **) link
->network
->dhcp_user_class
);
1456 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1459 if (link
->network
->dhcp_client_port
) {
1460 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1462 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen port: %m");
1465 if (link
->network
->dhcp_max_attempts
> 0) {
1466 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1468 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1471 if (link
->network
->dhcp_ip_service_type
> 0) {
1472 r
= sd_dhcp_client_set_service_type(link
->dhcp_client
, link
->network
->dhcp_ip_service_type
);
1474 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IP service type: %m");
1477 if (link
->network
->dhcp_fallback_lease_lifetime
> 0) {
1478 r
= sd_dhcp_client_set_fallback_lease_lifetime(link
->dhcp_client
, link
->network
->dhcp_fallback_lease_lifetime
);
1480 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1483 r
= dhcp4_configure_dad(link
);
1485 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to configure service type: %m");
1487 return dhcp4_set_client_identifier(link
);
1490 int dhcp4_update_mac(Link
*link
) {
1495 if (!link
->dhcp_client
)
1498 r
= sd_dhcp_client_set_mac(link
->dhcp_client
, link
->hw_addr
.addr
.bytes
,
1499 link
->bcast_addr
.length
> 0 ? link
->bcast_addr
.addr
.bytes
: NULL
,
1500 link
->hw_addr
.length
, link
->iftype
);
1504 r
= dhcp4_set_client_identifier(link
);
1508 r
= dhcp4_dad_update_mac(link
);
1515 int link_deserialize_dhcp4(Link
*link
, const char *dhcp4_address
) {
1516 union in_addr_union address
;
1521 if (isempty(dhcp4_address
))
1524 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
1526 return log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address: %s", dhcp4_address
);
1528 r
= dhcp4_init(link
);
1530 return log_link_debug_errno(link
, r
, "Failed to initialize DHCPv4 client: %m");
1532 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
1534 return log_link_debug_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
1539 int config_parse_dhcp_max_attempts(
1541 const char *filename
,
1543 const char *section
,
1544 unsigned section_line
,
1551 Network
*network
= data
;
1559 if (isempty(rvalue
)) {
1560 network
->dhcp_max_attempts
= 0;
1564 if (streq(rvalue
, "infinity")) {
1565 network
->dhcp_max_attempts
= (uint64_t) -1;
1569 r
= safe_atou64(rvalue
, &a
);
1571 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1572 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1577 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1578 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1582 network
->dhcp_max_attempts
= a
;
1587 int config_parse_dhcp_acl_ip_address(
1589 const char *filename
,
1591 const char *section
,
1592 unsigned section_line
,
1599 Network
*network
= data
;
1608 acl
= STR_IN_SET(lvalue
, "DenyList", "BlackList") ? &network
->dhcp_deny_listed_ip
: &network
->dhcp_allow_listed_ip
;
1610 if (isempty(rvalue
)) {
1611 *acl
= set_free(*acl
);
1615 for (const char *p
= rvalue
;;) {
1616 _cleanup_free_
char *n
= NULL
;
1617 union in_addr_union ip
;
1619 r
= extract_first_word(&p
, &n
, NULL
, 0);
1623 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1624 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1631 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1633 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1634 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue
, n
);
1638 r
= set_ensure_put(acl
, NULL
, UINT32_TO_PTR(ip
.in
.s_addr
));
1640 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1641 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue
, n
);
1645 int config_parse_dhcp_ip_service_type(
1647 const char *filename
,
1649 const char *section
,
1650 unsigned section_line
,
1661 if (streq(rvalue
, "CS4"))
1662 *((int *)data
) = IPTOS_CLASS_CS4
;
1663 else if (streq(rvalue
, "CS6"))
1664 *((int *)data
) = IPTOS_CLASS_CS6
;
1666 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1667 "Failed to parse IPServiceType type '%s', ignoring.", rvalue
);
1672 int config_parse_dhcp_mud_url(
1674 const char *filename
,
1676 const char *section
,
1677 unsigned section_line
,
1684 _cleanup_free_
char *unescaped
= NULL
;
1685 Network
*network
= data
;
1692 if (isempty(rvalue
)) {
1693 network
->dhcp_mudurl
= mfree(network
->dhcp_mudurl
);
1697 r
= cunescape(rvalue
, 0, &unescaped
);
1699 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1700 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue
);
1704 if (!http_url_is_valid(unescaped
) || strlen(unescaped
) > 255) {
1705 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1706 "Failed to parse MUD URL '%s', ignoring: %m", rvalue
);
1711 return free_and_strdup_warn(&network
->dhcp_mudurl
, unescaped
);
1714 int config_parse_dhcp_fallback_lease_lifetime(const char *unit
,
1715 const char *filename
,
1717 const char *section
,
1718 unsigned section_line
,
1724 Network
*network
= userdata
;
1733 if (isempty(rvalue
)) {
1734 network
->dhcp_fallback_lease_lifetime
= 0;
1738 /* We accept only "forever" or "infinity". */
1739 if (STR_IN_SET(rvalue
, "forever", "infinity"))
1740 k
= CACHE_INFO_INFINITY_LIFE_TIME
;
1742 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1743 "Invalid LeaseLifetime= value, ignoring: %s", rvalue
);
1747 network
->dhcp_fallback_lease_lifetime
= k
;
1752 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1753 [DHCP_CLIENT_ID_MAC
] = "mac",
1754 [DHCP_CLIENT_ID_DUID
] = "duid",
1755 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1758 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1759 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1760 "Failed to parse client identifier type");