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-setup.h"
12 #include "hostname-util.h"
13 #include "parse-util.h"
14 #include "network-internal.h"
15 #include "networkd-address.h"
16 #include "networkd-dhcp4.h"
17 #include "networkd-link.h"
18 #include "networkd-manager.h"
19 #include "networkd-network.h"
20 #include "string-table.h"
22 #include "sysctl-util.h"
25 static int dhcp4_update_address(Link
*link
, bool announce
);
26 static int dhcp4_remove_all(Link
*link
);
28 static int dhcp4_release_old_lease(Link
*link
) {
34 if (!link
->dhcp_address_old
&& set_isempty(link
->dhcp_routes_old
))
37 log_link_debug(link
, "Removing old DHCPv4 address and routes.");
39 SET_FOREACH(route
, link
->dhcp_routes_old
) {
40 k
= route_remove(route
, NULL
, link
, NULL
);
45 if (link
->dhcp_address_old
) {
46 k
= address_remove(link
->dhcp_address_old
, link
, NULL
);
54 static void dhcp4_check_ready(Link
*link
) {
57 if (link
->network
->dhcp_send_decline
&& !link
->dhcp4_address_bind
)
60 if (link
->dhcp4_messages
> 0)
63 link
->dhcp4_configured
= true;
65 /* New address and routes are configured now. Let's release old lease. */
66 r
= dhcp4_release_old_lease(link
);
68 link_enter_failed(link
);
72 r
= sd_ipv4ll_stop(link
->ipv4ll
);
74 log_link_warning_errno(link
, r
, "Failed to drop IPv4 link-local address, ignoring: %m");
76 link_check_ready(link
);
79 static int dhcp4_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
83 assert(link
->dhcp4_messages
> 0);
85 link
->dhcp4_messages
--;
87 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
90 r
= sd_netlink_message_get_errno(m
);
91 if (r
== -ENETUNREACH
&& !link
->dhcp4_route_retrying
) {
93 /* It seems kernel does not support that the prefix route cannot be configured with
94 * route table. Let's once drop the config and reconfigure them later. */
96 log_link_message_debug_errno(link
, m
, r
, "Could not set DHCPv4 route, retrying later");
97 link
->dhcp4_route_failed
= true;
98 link
->manager
->dhcp4_prefix_root_cannot_set_table
= true;
99 } else if (r
< 0 && r
!= -EEXIST
) {
100 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 route");
101 link_enter_failed(link
);
105 if (link
->dhcp4_messages
== 0 && link
->dhcp4_route_failed
) {
106 link
->dhcp4_route_failed
= false;
107 link
->dhcp4_route_retrying
= true;
109 r
= dhcp4_remove_all(link
);
111 link_enter_failed(link
);
115 dhcp4_check_ready(link
);
120 static int route_scope_from_address(const Route
*route
, const struct in_addr
*self_addr
) {
124 if (in4_addr_is_localhost(&route
->dst
.in
) ||
125 (!in4_addr_is_null(self_addr
) && in4_addr_equal(&route
->dst
.in
, self_addr
)))
126 return RT_SCOPE_HOST
;
127 else if (in4_addr_is_null(&route
->gw
.in
))
128 return RT_SCOPE_LINK
;
130 return RT_SCOPE_UNIVERSE
;
133 static bool link_prefixroute(Link
*link
) {
134 return !link
->network
->dhcp_route_table_set
||
135 link
->network
->dhcp_route_table
== RT_TABLE_MAIN
||
136 link
->manager
->dhcp4_prefix_root_cannot_set_table
;
139 static int dhcp_route_configure(Route
*route
, Link
*link
) {
146 r
= route_configure(route
, link
, dhcp4_route_handler
, &ret
);
148 return log_link_error_errno(link
, r
, "Failed to set DHCPv4 route: %m");
150 link
->dhcp4_messages
++;
152 r
= set_ensure_put(&link
->dhcp_routes
, &route_hash_ops
, ret
);
154 return log_link_error_errno(link
, r
, "Failed to store DHCPv4 route: %m");
156 (void) set_remove(link
->dhcp_routes_old
, ret
);
161 static int link_set_dns_routes(Link
*link
, const struct in_addr
*address
) {
162 const struct in_addr
*dns
;
167 assert(link
->dhcp_lease
);
168 assert(link
->network
);
170 if (!link
->network
->dhcp_use_dns
||
171 !link
->network
->dhcp_routes_to_dns
)
174 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &dns
);
175 if (IN_SET(n
, 0, -ENODATA
))
178 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
180 table
= link_get_dhcp_route_table(link
);
182 for (int i
= 0; i
< n
; i
++) {
183 _cleanup_(route_freep
) Route
*route
= NULL
;
185 r
= route_new(&route
);
187 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
189 /* Set routes to DNS servers. */
191 route
->family
= AF_INET
;
192 route
->dst
.in
= dns
[i
];
193 route
->dst_prefixlen
= 32;
194 route
->prefsrc
.in
= *address
;
195 route
->scope
= RT_SCOPE_LINK
;
196 route
->protocol
= RTPROT_DHCP
;
197 route
->priority
= link
->network
->dhcp_route_metric
;
198 route
->table
= table
;
200 r
= dhcp_route_configure(route
, link
);
202 return log_link_error_errno(link
, r
, "Could not set route to DNS server: %m");
208 static int dhcp_prefix_route_from_lease(
209 const sd_dhcp_lease
*lease
,
211 const struct in_addr
*address
,
215 struct in_addr netmask
;
218 r
= sd_dhcp_lease_get_netmask((sd_dhcp_lease
*) lease
, &netmask
);
222 r
= route_new(&route
);
226 route
->family
= AF_INET
;
227 route
->dst
.in
.s_addr
= address
->s_addr
& netmask
.s_addr
;
228 route
->dst_prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
229 route
->prefsrc
.in
= *address
;
230 route
->scope
= RT_SCOPE_LINK
;
231 route
->protocol
= RTPROT_DHCP
;
232 route
->table
= table
;
237 static int link_set_dhcp_routes(Link
*link
) {
238 _cleanup_free_ sd_dhcp_route
**static_routes
= NULL
;
239 bool classless_route
= false, static_route
= false;
240 struct in_addr address
;
247 if (!link
->dhcp_lease
) /* link went down while we configured the IP addresses? */
250 if (!link
->network
) /* link went down while we configured the IP addresses? */
253 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
254 /* During configuring addresses, the link lost its carrier. As networkd is dropping
255 * the addresses now, let's not configure the routes either. */
258 while ((rt
= set_steal_first(link
->dhcp_routes
))) {
259 r
= set_ensure_put(&link
->dhcp_routes_old
, &route_hash_ops
, rt
);
261 return log_link_error_errno(link
, r
, "Failed to store old DHCPv4 route: %m");
264 table
= link_get_dhcp_route_table(link
);
266 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
268 return log_link_warning_errno(link
, r
, "DHCP error: could not get address: %m");
270 if (!link_prefixroute(link
)) {
271 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
273 r
= dhcp_prefix_route_from_lease(link
->dhcp_lease
, table
, &address
, &prefix_route
);
275 return log_link_error_errno(link
, r
, "Could not create prefix route: %m");
277 r
= dhcp_route_configure(prefix_route
, link
);
279 return log_link_error_errno(link
, r
, "Could not set prefix route: %m");
282 n
= sd_dhcp_lease_get_routes(link
->dhcp_lease
, &static_routes
);
284 log_link_debug_errno(link
, n
, "DHCP: No routes received from DHCP server: %m");
286 return log_link_error_errno(link
, n
, "DHCP: could not get routes: %m");
288 for (int i
= 0; i
< n
; i
++) {
289 switch (sd_dhcp_route_get_option(static_routes
[i
])) {
290 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
291 classless_route
= true;
293 case SD_DHCP_OPTION_STATIC_ROUTE
:
299 if (link
->network
->dhcp_use_routes
) {
300 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
301 * the DHCP client MUST ignore the Static Routes option. */
302 if (classless_route
&& static_route
)
303 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring static-route option");
305 for (int i
= 0; i
< n
; i
++) {
306 _cleanup_(route_freep
) Route
*route
= NULL
;
308 if (classless_route
&&
309 sd_dhcp_route_get_option(static_routes
[i
]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
)
312 r
= route_new(&route
);
314 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
316 route
->family
= AF_INET
;
317 route
->protocol
= RTPROT_DHCP
;
318 route
->gw_family
= AF_INET
;
319 assert_se(sd_dhcp_route_get_gateway(static_routes
[i
], &route
->gw
.in
) >= 0);
320 assert_se(sd_dhcp_route_get_destination(static_routes
[i
], &route
->dst
.in
) >= 0);
321 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &route
->dst_prefixlen
) >= 0);
322 route
->priority
= link
->network
->dhcp_route_metric
;
323 route
->table
= table
;
324 route
->mtu
= link
->network
->dhcp_route_mtu
;
325 route
->scope
= route_scope_from_address(route
, &address
);
326 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
327 route
->prefsrc
.in
= address
;
329 if (set_contains(link
->dhcp_routes
, route
))
332 r
= dhcp_route_configure(route
, link
);
334 return log_link_error_errno(link
, r
, "Could not set route: %m");
338 if (link
->network
->dhcp_use_gateway
) {
339 const struct in_addr
*router
;
341 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
342 if (IN_SET(r
, 0, -ENODATA
))
343 log_link_info(link
, "DHCP: No gateway received from DHCP server.");
345 return log_link_error_errno(link
, r
, "DHCP error: could not get gateway: %m");
346 else if (in4_addr_is_null(&router
[0]))
347 log_link_info(link
, "DHCP: Received gateway is null.");
348 else if (classless_route
)
349 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
350 * a Router option, the DHCP client MUST ignore the Router option. */
351 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring router option");
353 _cleanup_(route_freep
) Route
*route
= NULL
, *route_gw
= NULL
;
355 r
= route_new(&route_gw
);
357 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
359 /* The dhcp netmask may mask out the gateway. Add an explicit
360 * route for the gw host so that we can route no matter the
361 * netmask or existing kernel route tables. */
362 route_gw
->family
= AF_INET
;
363 route_gw
->dst
.in
= router
[0];
364 route_gw
->dst_prefixlen
= 32;
365 route_gw
->prefsrc
.in
= address
;
366 route_gw
->scope
= RT_SCOPE_LINK
;
367 route_gw
->protocol
= RTPROT_DHCP
;
368 route_gw
->priority
= link
->network
->dhcp_route_metric
;
369 route_gw
->table
= table
;
370 route_gw
->mtu
= link
->network
->dhcp_route_mtu
;
372 r
= dhcp_route_configure(route_gw
, link
);
374 return log_link_error_errno(link
, r
, "Could not set host route: %m");
376 r
= route_new(&route
);
378 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
380 route
->family
= AF_INET
;
381 route
->gw_family
= AF_INET
;
382 route
->gw
.in
= router
[0];
383 route
->prefsrc
.in
= address
;
384 route
->protocol
= RTPROT_DHCP
;
385 route
->priority
= link
->network
->dhcp_route_metric
;
386 route
->table
= table
;
387 route
->mtu
= link
->network
->dhcp_route_mtu
;
389 r
= dhcp_route_configure(route
, link
);
391 return log_link_error_errno(link
, r
, "Could not set router: %m");
393 HASHMAP_FOREACH(rt
, link
->network
->routes_by_section
) {
394 if (!rt
->gateway_from_dhcp_or_ra
)
397 if (rt
->gw_family
!= AF_INET
)
400 rt
->gw
.in
= router
[0];
401 if (!rt
->protocol_set
)
402 rt
->protocol
= RTPROT_DHCP
;
403 if (!rt
->priority_set
)
404 rt
->priority
= link
->network
->dhcp_route_metric
;
408 rt
->mtu
= link
->network
->dhcp_route_mtu
;
410 r
= dhcp_route_configure(rt
, link
);
412 return log_link_error_errno(link
, r
, "Could not set gateway: %m");
417 return link_set_dns_routes(link
, &address
);
420 static int dhcp_reset_mtu(Link
*link
) {
426 if (!link
->network
->dhcp_use_mtu
)
429 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
433 return log_link_error_errno(link
, r
, "DHCP error: failed to get MTU from lease: %m");
435 if (link
->original_mtu
== mtu
)
438 r
= link_set_mtu(link
, link
->original_mtu
);
440 return log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
445 static int dhcp_reset_hostname(Link
*link
) {
446 const char *hostname
;
451 if (!link
->network
->dhcp_use_hostname
)
454 hostname
= link
->network
->dhcp_hostname
;
456 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
461 /* If a hostname was set due to the lease, then unset it now. */
462 r
= manager_set_hostname(link
->manager
, NULL
);
464 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
469 static int dhcp4_remove_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
474 assert(link
->dhcp4_remove_messages
> 0);
476 link
->dhcp4_remove_messages
--;
478 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
481 r
= sd_netlink_message_get_errno(m
);
482 if (r
< 0 && r
!= -ESRCH
)
483 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 route, ignoring");
485 if (link
->dhcp4_remove_messages
== 0) {
486 r
= dhcp4_update_address(link
, false);
488 link_enter_failed(link
);
494 static int dhcp4_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
499 assert(link
->dhcp4_remove_messages
> 0);
501 link
->dhcp4_remove_messages
--;
503 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
506 r
= sd_netlink_message_get_errno(m
);
507 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
508 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 address, ignoring");
510 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
512 if (link
->dhcp4_remove_messages
== 0) {
513 r
= dhcp4_update_address(link
, false);
515 link_enter_failed(link
);
521 static int dhcp4_remove_all(Link
*link
) {
527 SET_FOREACH(route
, link
->dhcp_routes
) {
528 k
= route_remove(route
, NULL
, link
, dhcp4_remove_route_handler
);
532 link
->dhcp4_remove_messages
++;
535 if (link
->dhcp_address
) {
536 k
= address_remove(link
->dhcp_address
, link
, dhcp4_remove_address_handler
);
540 link
->dhcp4_remove_messages
++;
546 static int dhcp_lease_lost(Link
*link
) {
550 assert(link
->dhcp_lease
);
552 log_link_info(link
, "DHCP lease lost");
554 link
->dhcp4_configured
= false;
556 /* dhcp_lease_lost() may be called during renewing IP address. */
557 k
= dhcp4_release_old_lease(link
);
561 k
= dhcp4_remove_all(link
);
565 k
= dhcp_reset_mtu(link
);
569 k
= dhcp_reset_hostname(link
);
573 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
576 (void) sd_ipv4acd_stop(link
->dhcp_acd
);
581 static void dhcp_address_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
582 _cleanup_free_
char *pretty
= NULL
;
583 union in_addr_union address
= {};
593 case SD_IPV4ACD_EVENT_STOP
:
594 log_link_debug(link
, "Stopping ACD client for DHCP4...");
597 case SD_IPV4ACD_EVENT_BIND
:
599 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
600 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
601 log_link_debug(link
, "Successfully claimed DHCP4 address %s", strna(pretty
));
603 link
->dhcp4_address_bind
= true;
604 dhcp4_check_ready(link
);
607 case SD_IPV4ACD_EVENT_CONFLICT
:
608 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
609 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
610 log_link_warning(link
, "DAD conflict. Dropping DHCP4 address %s", strna(pretty
));
612 r
= sd_dhcp_client_send_decline(link
->dhcp_client
);
614 log_link_warning_errno(link
, r
, "Failed to send DHCP DECLINE, ignoring: %m");
616 if (link
->dhcp_lease
) {
617 r
= dhcp_lease_lost(link
);
619 link_enter_failed(link
);
624 assert_not_reached("Invalid IPv4ACD event.");
627 (void) sd_ipv4acd_stop(acd
);
632 static int dhcp4_configure_dad(Link
*link
) {
636 assert(link
->manager
);
637 assert(link
->network
);
639 if (!link
->network
->dhcp_send_decline
)
642 if (!link
->dhcp_acd
) {
643 r
= sd_ipv4acd_new(&link
->dhcp_acd
);
647 r
= sd_ipv4acd_attach_event(link
->dhcp_acd
, link
->manager
->event
, 0);
652 r
= sd_ipv4acd_set_ifindex(link
->dhcp_acd
, link
->ifindex
);
656 r
= sd_ipv4acd_set_mac(link
->dhcp_acd
, &link
->hw_addr
.addr
.ether
);
663 static int dhcp4_dad_update_mac(Link
*link
) {
672 running
= sd_ipv4acd_is_running(link
->dhcp_acd
);
674 r
= sd_ipv4acd_stop(link
->dhcp_acd
);
678 r
= sd_ipv4acd_set_mac(link
->dhcp_acd
, &link
->hw_addr
.addr
.ether
);
683 r
= sd_ipv4acd_start(link
->dhcp_acd
, true);
691 static int dhcp4_start_acd(Link
*link
) {
692 union in_addr_union addr
;
696 if (!link
->network
->dhcp_send_decline
)
699 if (!link
->dhcp_lease
)
702 (void) sd_ipv4acd_stop(link
->dhcp_acd
);
704 link
->dhcp4_address_bind
= false;
706 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &addr
.in
);
710 r
= sd_ipv4acd_get_address(link
->dhcp_acd
, &old
);
714 r
= sd_ipv4acd_set_address(link
->dhcp_acd
, &addr
.in
);
718 r
= sd_ipv4acd_set_callback(link
->dhcp_acd
, dhcp_address_on_acd
, link
);
723 _cleanup_free_
char *pretty
= NULL
;
725 (void) in_addr_to_string(AF_INET
, &addr
, &pretty
);
726 log_link_debug(link
, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty
));
729 r
= sd_ipv4acd_start(link
->dhcp_acd
, !in4_addr_equal(&addr
.in
, &old
));
736 static int dhcp4_address_ready_callback(Address
*address
) {
742 link
= address
->link
;
744 /* Do not call this again. */
745 address
->callback
= NULL
;
747 r
= link_set_dhcp_routes(link
);
751 /* Reconfigure static routes as kernel may remove some routes when lease expires. */
752 r
= link_set_routes(link
);
756 r
= dhcp4_start_acd(link
);
758 return log_link_error_errno(link
, r
, "Failed to start IPv4ACD for DHCP4 address: %m");
760 dhcp4_check_ready(link
);
764 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
769 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
772 r
= sd_netlink_message_get_errno(m
);
773 if (r
< 0 && r
!= -EEXIST
) {
774 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 address");
775 link_enter_failed(link
);
778 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
780 if (address_is_ready(link
->dhcp_address
)) {
781 r
= dhcp4_address_ready_callback(link
->dhcp_address
);
783 link_enter_failed(link
);
787 link
->dhcp_address
->callback
= dhcp4_address_ready_callback
;
792 static int dhcp4_update_address(Link
*link
, bool announce
) {
793 _cleanup_(address_freep
) Address
*addr
= NULL
;
794 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
795 struct in_addr address
, netmask
;
801 assert(link
->network
);
803 if (!link
->dhcp_lease
)
806 link_set_state(link
, LINK_STATE_CONFIGURING
);
807 link
->dhcp4_configured
= false;
809 /* address_handler calls link_set_routes() and link_set_nexthop(). Before they are called, the
810 * related flags must be cleared. Otherwise, the link becomes configured state before routes
812 link
->static_routes_configured
= false;
813 link
->static_nexthops_configured
= false;
815 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
817 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
819 r
= sd_dhcp_lease_get_netmask(link
->dhcp_lease
, &netmask
);
821 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
823 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
824 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
826 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
829 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
832 const struct in_addr
*router
;
834 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
835 if (r
< 0 && r
!= -ENODATA
)
836 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
838 if (r
> 0 && !in4_addr_is_null(&router
[0]))
840 LOG_LINK_INTERFACE(link
),
841 LOG_LINK_MESSAGE(link
, "DHCPv4 address "IPV4_ADDRESS_FMT_STR
"/%u via "IPV4_ADDRESS_FMT_STR
,
842 IPV4_ADDRESS_FMT_VAL(address
),
844 IPV4_ADDRESS_FMT_VAL(router
[0])),
845 "ADDRESS="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(address
),
846 "PREFIXLEN=%u", prefixlen
,
847 "GATEWAY="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(router
[0]));
850 LOG_LINK_INTERFACE(link
),
851 LOG_LINK_MESSAGE(link
, "DHCPv4 address "IPV4_ADDRESS_FMT_STR
"/%u",
852 IPV4_ADDRESS_FMT_VAL(address
),
854 "ADDRESS="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(address
),
855 "PREFIXLEN=%u", prefixlen
);
858 r
= address_new(&addr
);
862 addr
->family
= AF_INET
;
863 addr
->in_addr
.in
.s_addr
= address
.s_addr
;
864 addr
->cinfo
.ifa_prefered
= lifetime
;
865 addr
->cinfo
.ifa_valid
= lifetime
;
866 addr
->prefixlen
= prefixlen
;
868 addr
->broadcast
.s_addr
= address
.s_addr
| ~netmask
.s_addr
;
869 SET_FLAG(addr
->flags
, IFA_F_NOPREFIXROUTE
, !link_prefixroute(link
));
871 /* allow reusing an existing address and simply update its lifetime
872 * in case it already exists */
873 r
= address_configure(addr
, link
, dhcp4_address_handler
, true, &ret
);
875 return log_link_error_errno(link
, r
, "Failed to set DHCPv4 address: %m");
877 if (!address_equal(link
->dhcp_address
, ret
))
878 link
->dhcp_address_old
= link
->dhcp_address
;
879 link
->dhcp_address
= ret
;
884 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
885 sd_dhcp_lease
*lease
;
891 r
= sd_dhcp_client_get_lease(client
, &lease
);
893 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
895 sd_dhcp_lease_unref(link
->dhcp_lease
);
896 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
899 return dhcp4_update_address(link
, false);
902 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
903 sd_dhcp_lease
*lease
;
909 r
= sd_dhcp_client_get_lease(client
, &lease
);
911 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
913 sd_dhcp_lease_unref(link
->dhcp_lease
);
914 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
917 if (link
->network
->dhcp_use_mtu
) {
920 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
922 r
= link_set_mtu(link
, mtu
);
924 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
928 if (link
->network
->dhcp_use_hostname
) {
929 const char *dhcpname
= NULL
;
930 _cleanup_free_
char *hostname
= NULL
;
932 if (link
->network
->dhcp_hostname
)
933 dhcpname
= link
->network
->dhcp_hostname
;
935 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
938 r
= shorten_overlong(dhcpname
, &hostname
);
940 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
942 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
946 r
= manager_set_hostname(link
->manager
, hostname
);
948 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
952 if (link
->network
->dhcp_use_timezone
) {
953 const char *tz
= NULL
;
955 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
958 r
= manager_set_timezone(link
->manager
, tz
);
960 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
964 if (link
->dhcp4_remove_messages
== 0) {
965 r
= dhcp4_update_address(link
, true);
970 "The link has previously assigned DHCPv4 address or routes. "
971 "The newly assigned address and routes will set up after old ones are removed.");
976 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
979 r
= dhcp_lease_acquired(client
, link
);
981 (void) dhcp_lease_lost(link
);
986 static int dhcp_server_is_deny_listed(Link
*link
, sd_dhcp_client
*client
) {
987 sd_dhcp_lease
*lease
;
992 assert(link
->network
);
995 r
= sd_dhcp_client_get_lease(client
, &lease
);
997 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
999 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1001 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1003 if (set_contains(link
->network
->dhcp_deny_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1004 log_struct(LOG_DEBUG
,
1005 LOG_LINK_INTERFACE(link
),
1006 LOG_LINK_MESSAGE(link
, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR
" found in deny-list, ignoring offer",
1007 IPV4_ADDRESS_FMT_VAL(addr
)));
1014 static int dhcp_server_is_allow_listed(Link
*link
, sd_dhcp_client
*client
) {
1015 sd_dhcp_lease
*lease
;
1016 struct in_addr addr
;
1020 assert(link
->network
);
1023 r
= sd_dhcp_client_get_lease(client
, &lease
);
1025 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1027 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1029 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1031 if (set_contains(link
->network
->dhcp_allow_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1032 log_struct(LOG_DEBUG
,
1033 LOG_LINK_INTERFACE(link
),
1034 LOG_LINK_MESSAGE(link
, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR
" found in allow-list, accepting offer",
1035 IPV4_ADDRESS_FMT_VAL(addr
)));
1042 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
1043 Link
*link
= userdata
;
1047 assert(link
->network
);
1048 assert(link
->manager
);
1050 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1054 case SD_DHCP_CLIENT_EVENT_STOP
:
1056 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
1058 r
= sd_ipv4ll_start(link
->ipv4ll
);
1060 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1063 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1064 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1068 if (link
->dhcp_lease
) {
1069 if (link
->network
->dhcp_send_release
) {
1070 r
= sd_dhcp_client_send_release(client
);
1072 log_link_warning_errno(link
, r
, "Failed to send DHCP RELEASE, ignoring: %m");
1075 r
= dhcp_lease_lost(link
);
1077 link_enter_failed(link
);
1083 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
1084 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1085 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1089 if (link
->dhcp_lease
) {
1090 r
= dhcp_lease_lost(link
);
1092 link_enter_failed(link
);
1098 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
1099 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1100 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1104 r
= dhcp_lease_ip_change(client
, link
);
1106 link_enter_failed(link
);
1111 case SD_DHCP_CLIENT_EVENT_RENEW
:
1112 r
= dhcp_lease_renew(client
, link
);
1114 link_enter_failed(link
);
1118 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1119 r
= dhcp_lease_acquired(client
, link
);
1121 link_enter_failed(link
);
1125 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1126 if (!set_isempty(link
->network
->dhcp_allow_listed_ip
)) {
1127 r
= dhcp_server_is_allow_listed(link
, client
);
1133 r
= dhcp_server_is_deny_listed(link
, client
);
1141 case SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE
:
1142 if (link
->ipv4ll
&& !sd_ipv4ll_is_running(link
->ipv4ll
)) {
1143 log_link_debug(link
, "Problems acquiring DHCP lease, acquiring IPv4 link-local address");
1145 r
= sd_ipv4ll_start(link
->ipv4ll
);
1147 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1153 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1155 log_link_warning(link
, "DHCP unknown event: %i", event
);
1162 static int dhcp4_set_hostname(Link
*link
) {
1163 _cleanup_free_
char *hostname
= NULL
;
1169 if (!link
->network
->dhcp_send_hostname
)
1171 else if (link
->network
->dhcp_hostname
)
1172 hn
= link
->network
->dhcp_hostname
;
1174 r
= gethostname_strict(&hostname
);
1175 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1176 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to get hostname: %m");
1181 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1182 if (r
== -EINVAL
&& hostname
)
1183 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1184 log_link_debug_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1186 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
1191 static int dhcp4_set_client_identifier(Link
*link
) {
1195 assert(link
->network
);
1196 assert(link
->dhcp_client
);
1198 switch (link
->network
->dhcp_client_identifier
) {
1199 case DHCP_CLIENT_ID_DUID
: {
1200 /* If configured, apply user specified DUID and IAID */
1201 const DUID
*duid
= link_get_duid(link
);
1203 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1204 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1205 link
->network
->iaid_set
,
1206 link
->network
->iaid
,
1209 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
1210 link
->network
->iaid_set
,
1211 link
->network
->iaid
,
1213 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1214 duid
->raw_data_len
);
1216 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1219 case DHCP_CLIENT_ID_DUID_ONLY
: {
1220 /* If configured, apply user specified DUID */
1221 const DUID
*duid
= link_get_duid(link
);
1223 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1224 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
1227 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
1229 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1230 duid
->raw_data_len
);
1232 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
1235 case DHCP_CLIENT_ID_MAC
: {
1236 const uint8_t *hw_addr
= link
->hw_addr
.addr
.bytes
;
1237 size_t hw_addr_len
= link
->hw_addr
.length
;
1239 if (link
->iftype
== ARPHRD_INFINIBAND
&& hw_addr_len
== INFINIBAND_ALEN
) {
1240 /* set_client_id expects only last 8 bytes of an IB address */
1241 hw_addr
+= INFINIBAND_ALEN
- 8;
1242 hw_addr_len
-= INFINIBAND_ALEN
- 8;
1245 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1250 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
1254 assert_not_reached("Unknown client identifier type.");
1260 static int dhcp4_set_request_address(Link
*link
) {
1264 assert(link
->network
);
1265 assert(link
->dhcp_client
);
1267 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
1270 SET_FOREACH(a
, link
->addresses_foreign
) {
1271 if (a
->family
!= AF_INET
)
1273 if (link_address_is_dynamic(link
, a
))
1280 log_link_debug(link
, "DHCP4 CLIENT: requesting " IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(a
->in_addr
.in
));
1282 return sd_dhcp_client_set_request_address(link
->dhcp_client
, &a
->in_addr
.in
);
1285 int dhcp4_configure(Link
*link
) {
1286 sd_dhcp_option
*send_option
;
1287 void *request_options
;
1291 assert(link
->network
);
1293 if (!link_dhcp4_enabled(link
))
1296 if (!link
->dhcp_client
) {
1297 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1299 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to allocate DHCP4 client: %m");
1301 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, link
->manager
->event
, 0);
1303 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to attach event to DHCP4 client: %m");
1306 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1307 link
->hw_addr
.addr
.bytes
,
1308 link
->bcast_addr
.length
> 0 ? link
->bcast_addr
.addr
.bytes
: NULL
,
1309 link
->hw_addr
.length
, link
->iftype
);
1311 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1313 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1315 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1317 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1319 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1321 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
, link
->network
->dhcp_broadcast
);
1323 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1325 if (link
->mtu
> 0) {
1326 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1328 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1331 if (link
->network
->dhcp_use_mtu
) {
1332 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_INTERFACE_MTU
);
1334 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1337 /* NOTE: even if this variable is called "use", it also "sends" PRL
1338 * options, maybe there should be a different configuration variable
1339 * to send or not route options?. */
1340 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1341 * by default, so they don't need to be added here. */
1342 if (link
->network
->dhcp_use_routes
&& !link
->network
->dhcp_anonymize
) {
1343 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_STATIC_ROUTE
);
1345 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1347 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1349 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1352 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
&& !link
->network
->dhcp_anonymize
) {
1353 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
);
1355 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1358 if (link
->network
->dhcp_use_ntp
) {
1359 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1361 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1364 if (link
->network
->dhcp_use_sip
) {
1365 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_SIP_SERVER
);
1367 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1370 if (link
->network
->dhcp_use_timezone
) {
1371 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1373 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1376 SET_FOREACH(request_options
, link
->network
->dhcp_request_options
) {
1377 uint32_t option
= PTR_TO_UINT32(request_options
);
1379 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, option
);
1381 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option
);
1384 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_options
) {
1385 r
= sd_dhcp_client_add_option(link
->dhcp_client
, send_option
);
1389 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1392 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_vendor_options
) {
1393 r
= sd_dhcp_client_add_vendor_option(link
->dhcp_client
, send_option
);
1397 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1400 r
= dhcp4_set_hostname(link
);
1404 if (link
->network
->dhcp_vendor_class_identifier
) {
1405 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1406 link
->network
->dhcp_vendor_class_identifier
);
1408 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1411 if (link
->network
->dhcp_mudurl
) {
1412 r
= sd_dhcp_client_set_mud_url(link
->dhcp_client
, link
->network
->dhcp_mudurl
);
1414 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1417 if (link
->network
->dhcp_user_class
) {
1418 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, link
->network
->dhcp_user_class
);
1420 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1423 if (link
->network
->dhcp_client_port
> 0) {
1424 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1426 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen port: %m");
1429 if (link
->network
->dhcp_max_attempts
> 0) {
1430 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1432 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1435 if (link
->network
->dhcp_ip_service_type
> 0) {
1436 r
= sd_dhcp_client_set_service_type(link
->dhcp_client
, link
->network
->dhcp_ip_service_type
);
1438 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set IP service type: %m");
1441 if (link
->network
->dhcp_fallback_lease_lifetime
> 0) {
1442 r
= sd_dhcp_client_set_fallback_lease_lifetime(link
->dhcp_client
, link
->network
->dhcp_fallback_lease_lifetime
);
1444 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1447 r
= dhcp4_set_request_address(link
);
1449 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set initial DHCPv4 address: %m");
1451 r
= dhcp4_configure_dad(link
);
1453 return log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to configure service type: %m");
1455 return dhcp4_set_client_identifier(link
);
1458 int dhcp4_update_mac(Link
*link
) {
1463 if (!link
->dhcp_client
)
1466 r
= sd_dhcp_client_set_mac(link
->dhcp_client
, link
->hw_addr
.addr
.bytes
,
1467 link
->bcast_addr
.length
> 0 ? link
->bcast_addr
.addr
.bytes
: NULL
,
1468 link
->hw_addr
.length
, link
->iftype
);
1472 r
= dhcp4_set_client_identifier(link
);
1476 r
= dhcp4_dad_update_mac(link
);
1483 int config_parse_dhcp_max_attempts(
1485 const char *filename
,
1487 const char *section
,
1488 unsigned section_line
,
1495 Network
*network
= data
;
1503 if (isempty(rvalue
)) {
1504 network
->dhcp_max_attempts
= 0;
1508 if (streq(rvalue
, "infinity")) {
1509 network
->dhcp_max_attempts
= (uint64_t) -1;
1513 r
= safe_atou64(rvalue
, &a
);
1515 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1516 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1521 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1522 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1526 network
->dhcp_max_attempts
= a
;
1531 int config_parse_dhcp_acl_ip_address(
1533 const char *filename
,
1535 const char *section
,
1536 unsigned section_line
,
1543 Network
*network
= data
;
1552 acl
= STR_IN_SET(lvalue
, "DenyList", "BlackList") ? &network
->dhcp_deny_listed_ip
: &network
->dhcp_allow_listed_ip
;
1554 if (isempty(rvalue
)) {
1555 *acl
= set_free(*acl
);
1559 for (const char *p
= rvalue
;;) {
1560 _cleanup_free_
char *n
= NULL
;
1561 union in_addr_union ip
;
1563 r
= extract_first_word(&p
, &n
, NULL
, 0);
1567 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1568 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1575 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1577 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1578 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue
, n
);
1582 r
= set_ensure_put(acl
, NULL
, UINT32_TO_PTR(ip
.in
.s_addr
));
1584 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1585 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue
, n
);
1589 int config_parse_dhcp_ip_service_type(
1591 const char *filename
,
1593 const char *section
,
1594 unsigned section_line
,
1605 if (streq(rvalue
, "CS4"))
1606 *((int *)data
) = IPTOS_CLASS_CS4
;
1607 else if (streq(rvalue
, "CS6"))
1608 *((int *)data
) = IPTOS_CLASS_CS6
;
1610 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1611 "Failed to parse IPServiceType type '%s', ignoring.", rvalue
);
1616 int config_parse_dhcp_mud_url(
1618 const char *filename
,
1620 const char *section
,
1621 unsigned section_line
,
1628 _cleanup_free_
char *unescaped
= NULL
;
1629 Network
*network
= data
;
1636 if (isempty(rvalue
)) {
1637 network
->dhcp_mudurl
= mfree(network
->dhcp_mudurl
);
1641 r
= cunescape(rvalue
, 0, &unescaped
);
1643 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1644 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue
);
1648 if (!http_url_is_valid(unescaped
) || strlen(unescaped
) > 255) {
1649 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1650 "Failed to parse MUD URL '%s', ignoring: %m", rvalue
);
1655 return free_and_strdup_warn(&network
->dhcp_mudurl
, unescaped
);
1658 int config_parse_dhcp_fallback_lease_lifetime(const char *unit
,
1659 const char *filename
,
1661 const char *section
,
1662 unsigned section_line
,
1668 Network
*network
= userdata
;
1677 if (isempty(rvalue
)) {
1678 network
->dhcp_fallback_lease_lifetime
= 0;
1682 /* We accept only "forever" or "infinity". */
1683 if (STR_IN_SET(rvalue
, "forever", "infinity"))
1684 k
= CACHE_INFO_INFINITY_LIFE_TIME
;
1686 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1687 "Invalid LeaseLifetime= value, ignoring: %s", rvalue
);
1691 network
->dhcp_fallback_lease_lifetime
= k
;
1696 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1697 [DHCP_CLIENT_ID_MAC
] = "mac",
1698 [DHCP_CLIENT_ID_DUID
] = "duid",
1699 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1702 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1703 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1704 "Failed to parse client identifier type");