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-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
)
394 if (rt
->family
!= AF_INET
)
397 rt
->gw_family
= AF_INET
;
398 rt
->gw
.in
= router
[0];
400 r
= dhcp_route_configure(rt
, link
);
402 return log_link_error_errno(link
, r
, "Could not set gateway: %m");
407 return link_set_dns_routes(link
, &address
);
410 static int dhcp_reset_mtu(Link
*link
) {
416 if (!link
->network
->dhcp_use_mtu
)
419 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
423 return log_link_error_errno(link
, r
, "DHCP error: failed to get MTU from lease: %m");
425 if (link
->original_mtu
== mtu
)
428 r
= link_set_mtu(link
, link
->original_mtu
);
430 return log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
435 static int dhcp_reset_hostname(Link
*link
) {
436 const char *hostname
;
441 if (!link
->network
->dhcp_use_hostname
)
444 hostname
= link
->network
->dhcp_hostname
;
446 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
451 /* If a hostname was set due to the lease, then unset it now. */
452 r
= manager_set_hostname(link
->manager
, NULL
);
454 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
459 static int dhcp4_remove_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
464 assert(link
->dhcp4_remove_messages
> 0);
466 link
->dhcp4_remove_messages
--;
468 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
471 r
= sd_netlink_message_get_errno(m
);
472 if (r
< 0 && r
!= -ESRCH
)
473 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 route, ignoring");
475 if (link
->dhcp4_remove_messages
== 0) {
476 r
= dhcp4_update_address(link
, false);
478 link_enter_failed(link
);
484 static int dhcp4_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
489 assert(link
->dhcp4_remove_messages
> 0);
491 link
->dhcp4_remove_messages
--;
493 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
496 r
= sd_netlink_message_get_errno(m
);
497 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
498 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 address, ignoring");
500 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
502 if (link
->dhcp4_remove_messages
== 0) {
503 r
= dhcp4_update_address(link
, false);
505 link_enter_failed(link
);
511 static int dhcp4_remove_all(Link
*link
) {
517 SET_FOREACH(route
, link
->dhcp_routes
) {
518 k
= route_remove(route
, NULL
, link
, dhcp4_remove_route_handler
);
522 link
->dhcp4_remove_messages
++;
525 if (link
->dhcp_address
) {
526 k
= address_remove(link
->dhcp_address
, link
, dhcp4_remove_address_handler
);
530 link
->dhcp4_remove_messages
++;
536 static int dhcp_lease_lost(Link
*link
) {
540 assert(link
->dhcp_lease
);
542 log_link_info(link
, "DHCP lease lost");
544 link
->dhcp4_configured
= false;
546 /* dhcp_lease_lost() may be called during renewing IP address. */
547 k
= dhcp4_release_old_lease(link
);
551 k
= dhcp4_remove_all(link
);
555 k
= dhcp_reset_mtu(link
);
559 k
= dhcp_reset_hostname(link
);
563 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
567 (void) sd_ipv4acd_stop(link
->dhcp_acd
);
572 static void dhcp_address_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
573 _cleanup_free_
char *pretty
= NULL
;
574 union in_addr_union address
= {};
584 case SD_IPV4ACD_EVENT_STOP
:
585 log_link_debug(link
, "Stopping ACD client for DHCP4...");
588 case SD_IPV4ACD_EVENT_BIND
:
590 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
591 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
592 log_link_debug(link
, "Successfully claimed DHCP4 address %s", strna(pretty
));
594 link
->dhcp4_address_bind
= true;
595 dhcp4_check_ready(link
);
598 case SD_IPV4ACD_EVENT_CONFLICT
:
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_warning(link
, "DAD conflict. Dropping DHCP4 address %s", strna(pretty
));
603 r
= sd_dhcp_client_send_decline(link
->dhcp_client
);
605 log_link_warning_errno(link
, r
, "Failed to send DHCP DECLINE, ignoring: %m");
607 if (link
->dhcp_lease
) {
608 r
= dhcp_lease_lost(link
);
610 link_enter_failed(link
);
615 assert_not_reached("Invalid IPv4ACD event.");
618 (void) sd_ipv4acd_stop(acd
);
623 static int dhcp4_configure_dad(Link
*link
) {
627 assert(link
->manager
);
628 assert(link
->network
);
630 if (!link
->network
->dhcp_send_decline
)
633 if (!link
->dhcp_acd
) {
634 r
= sd_ipv4acd_new(&link
->dhcp_acd
);
638 r
= sd_ipv4acd_attach_event(link
->dhcp_acd
, link
->manager
->event
, 0);
643 r
= sd_ipv4acd_set_ifindex(link
->dhcp_acd
, link
->ifindex
);
647 r
= sd_ipv4acd_set_mac(link
->dhcp_acd
, &link
->mac
);
654 static int dhcp4_dad_update_mac(Link
*link
) {
663 running
= sd_ipv4acd_is_running(link
->dhcp_acd
);
665 r
= sd_ipv4acd_stop(link
->dhcp_acd
);
669 r
= sd_ipv4acd_set_mac(link
->dhcp_acd
, &link
->mac
);
674 r
= sd_ipv4acd_start(link
->dhcp_acd
, true);
682 static int dhcp4_start_acd(Link
*link
) {
683 union in_addr_union addr
;
687 if (!link
->network
->dhcp_send_decline
)
690 if (!link
->dhcp_lease
)
693 (void) sd_ipv4acd_stop(link
->dhcp_acd
);
695 link
->dhcp4_address_bind
= false;
697 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &addr
.in
);
701 r
= sd_ipv4acd_get_address(link
->dhcp_acd
, &old
);
705 r
= sd_ipv4acd_set_address(link
->dhcp_acd
, &addr
.in
);
709 r
= sd_ipv4acd_set_callback(link
->dhcp_acd
, dhcp_address_on_acd
, link
);
714 _cleanup_free_
char *pretty
= NULL
;
716 (void) in_addr_to_string(AF_INET
, &addr
, &pretty
);
717 log_link_debug(link
, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty
));
720 r
= sd_ipv4acd_start(link
->dhcp_acd
, !in4_addr_equal(&addr
.in
, &old
));
727 static int dhcp4_address_ready_callback(Address
*address
) {
733 link
= address
->link
;
735 /* Do not call this again. */
736 address
->callback
= NULL
;
738 r
= link_set_dhcp_routes(link
);
742 /* Reconfigure static routes as kernel may remove some routes when lease expires. */
743 r
= link_set_routes(link
);
747 r
= dhcp4_start_acd(link
);
749 return log_link_error_errno(link
, r
, "Failed to start IPv4ACD for DHCP4 address: %m");
751 dhcp4_check_ready(link
);
755 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
760 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
763 r
= sd_netlink_message_get_errno(m
);
764 if (r
< 0 && r
!= -EEXIST
) {
765 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 address");
766 link_enter_failed(link
);
769 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
771 if (address_is_ready(link
->dhcp_address
)) {
772 r
= dhcp4_address_ready_callback(link
->dhcp_address
);
774 link_enter_failed(link
);
778 link
->dhcp_address
->callback
= dhcp4_address_ready_callback
;
783 static int dhcp4_update_address(Link
*link
, bool announce
) {
784 _cleanup_(address_freep
) Address
*addr
= NULL
;
785 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
786 struct in_addr address
, netmask
;
792 assert(link
->network
);
794 if (!link
->dhcp_lease
)
797 link_set_state(link
, LINK_STATE_CONFIGURING
);
798 link
->dhcp4_configured
= false;
800 /* address_handler calls link_set_routes() and link_set_nexthop(). Before they are called, the
801 * related flags must be cleared. Otherwise, the link becomes configured state before routes
803 link
->static_routes_configured
= false;
804 link
->static_nexthops_configured
= false;
806 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
808 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
810 r
= sd_dhcp_lease_get_netmask(link
->dhcp_lease
, &netmask
);
812 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
814 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
815 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
817 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
820 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
823 const struct in_addr
*router
;
825 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
826 if (r
< 0 && r
!= -ENODATA
)
827 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
829 if (r
> 0 && !in4_addr_is_null(&router
[0]))
831 LOG_LINK_INTERFACE(link
),
832 LOG_LINK_MESSAGE(link
, "DHCPv4 address "IPV4_ADDRESS_FMT_STR
"/%u via "IPV4_ADDRESS_FMT_STR
,
833 IPV4_ADDRESS_FMT_VAL(address
),
835 IPV4_ADDRESS_FMT_VAL(router
[0])),
836 "ADDRESS="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(address
),
837 "PREFIXLEN=%u", prefixlen
,
838 "GATEWAY="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(router
[0]));
841 LOG_LINK_INTERFACE(link
),
842 LOG_LINK_MESSAGE(link
, "DHCPv4 address "IPV4_ADDRESS_FMT_STR
"/%u",
843 IPV4_ADDRESS_FMT_VAL(address
),
845 "ADDRESS="IPV4_ADDRESS_FMT_STR
, IPV4_ADDRESS_FMT_VAL(address
),
846 "PREFIXLEN=%u", prefixlen
);
849 r
= address_new(&addr
);
853 addr
->family
= AF_INET
;
854 addr
->in_addr
.in
.s_addr
= address
.s_addr
;
855 addr
->cinfo
.ifa_prefered
= lifetime
;
856 addr
->cinfo
.ifa_valid
= lifetime
;
857 addr
->prefixlen
= prefixlen
;
858 addr
->broadcast
.s_addr
= address
.s_addr
| ~netmask
.s_addr
;
859 SET_FLAG(addr
->flags
, IFA_F_NOPREFIXROUTE
, !link_prefixroute(link
));
861 /* allow reusing an existing address and simply update its lifetime
862 * in case it already exists */
863 r
= address_configure(addr
, link
, dhcp4_address_handler
, true, &ret
);
865 return log_link_error_errno(link
, r
, "Failed to set DHCPv4 address: %m");
867 if (!address_equal(link
->dhcp_address
, ret
))
868 link
->dhcp_address_old
= link
->dhcp_address
;
869 link
->dhcp_address
= ret
;
874 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
875 sd_dhcp_lease
*lease
;
881 r
= sd_dhcp_client_get_lease(client
, &lease
);
883 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
885 sd_dhcp_lease_unref(link
->dhcp_lease
);
886 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
889 return dhcp4_update_address(link
, false);
892 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
893 sd_dhcp_lease
*lease
;
899 r
= sd_dhcp_client_get_lease(client
, &lease
);
901 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
903 sd_dhcp_lease_unref(link
->dhcp_lease
);
904 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
907 if (link
->network
->dhcp_use_mtu
) {
910 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
912 r
= link_set_mtu(link
, mtu
);
914 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
918 if (link
->network
->dhcp_use_hostname
) {
919 const char *dhcpname
= NULL
;
920 _cleanup_free_
char *hostname
= NULL
;
922 if (link
->network
->dhcp_hostname
)
923 dhcpname
= link
->network
->dhcp_hostname
;
925 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
928 r
= shorten_overlong(dhcpname
, &hostname
);
930 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
932 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
936 r
= manager_set_hostname(link
->manager
, hostname
);
938 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
942 if (link
->network
->dhcp_use_timezone
) {
943 const char *tz
= NULL
;
945 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
948 r
= manager_set_timezone(link
->manager
, tz
);
950 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
954 if (link
->dhcp4_remove_messages
== 0) {
955 r
= dhcp4_update_address(link
, true);
960 "The link has previously assigned DHCPv4 address or routes. "
961 "The newly assigned address and routes will set up after old ones are removed.");
966 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
969 r
= dhcp_lease_acquired(client
, link
);
971 (void) dhcp_lease_lost(link
);
976 static int dhcp_server_is_deny_listed(Link
*link
, sd_dhcp_client
*client
) {
977 sd_dhcp_lease
*lease
;
982 assert(link
->network
);
985 r
= sd_dhcp_client_get_lease(client
, &lease
);
987 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
989 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
991 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
993 if (set_contains(link
->network
->dhcp_deny_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
994 log_struct(LOG_DEBUG
,
995 LOG_LINK_INTERFACE(link
),
996 LOG_LINK_MESSAGE(link
, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR
" found in deny-list, ignoring offer",
997 IPV4_ADDRESS_FMT_VAL(addr
)));
1004 static int dhcp_server_is_allow_listed(Link
*link
, sd_dhcp_client
*client
) {
1005 sd_dhcp_lease
*lease
;
1006 struct in_addr addr
;
1010 assert(link
->network
);
1013 r
= sd_dhcp_client_get_lease(client
, &lease
);
1015 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1017 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1019 return log_link_debug_errno(link
, r
, "Failed to get DHCP server IP address: %m");
1021 if (set_contains(link
->network
->dhcp_allow_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1022 log_struct(LOG_DEBUG
,
1023 LOG_LINK_INTERFACE(link
),
1024 LOG_LINK_MESSAGE(link
, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR
" found in allow-list, accepting offer",
1025 IPV4_ADDRESS_FMT_VAL(addr
)));
1032 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
1033 Link
*link
= userdata
;
1037 assert(link
->network
);
1038 assert(link
->manager
);
1040 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1044 case SD_DHCP_CLIENT_EVENT_STOP
:
1046 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
)) {
1047 assert(link
->ipv4ll
);
1049 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
1051 r
= sd_ipv4ll_start(link
->ipv4ll
);
1053 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1056 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1057 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1061 if (link
->dhcp_lease
) {
1062 if (link
->network
->dhcp_send_release
) {
1063 r
= sd_dhcp_client_send_release(client
);
1065 log_link_warning_errno(link
, r
, "Failed to send DHCP RELEASE, ignoring: %m");
1068 r
= dhcp_lease_lost(link
);
1070 link_enter_failed(link
);
1076 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
1077 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1078 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1082 if (link
->dhcp_lease
) {
1083 r
= dhcp_lease_lost(link
);
1085 link_enter_failed(link
);
1091 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
1092 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1093 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1097 r
= dhcp_lease_ip_change(client
, link
);
1099 link_enter_failed(link
);
1104 case SD_DHCP_CLIENT_EVENT_RENEW
:
1105 r
= dhcp_lease_renew(client
, link
);
1107 link_enter_failed(link
);
1111 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1112 r
= dhcp_lease_acquired(client
, link
);
1114 link_enter_failed(link
);
1118 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1119 if (!set_isempty(link
->network
->dhcp_allow_listed_ip
)) {
1120 r
= dhcp_server_is_allow_listed(link
, client
);
1126 r
= dhcp_server_is_deny_listed(link
, client
);
1135 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1137 log_link_warning(link
, "DHCP unknown event: %i", event
);
1144 static int dhcp4_set_hostname(Link
*link
) {
1145 _cleanup_free_
char *hostname
= NULL
;
1151 if (!link
->network
->dhcp_send_hostname
)
1153 else if (link
->network
->dhcp_hostname
)
1154 hn
= link
->network
->dhcp_hostname
;
1156 r
= gethostname_strict(&hostname
);
1157 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1163 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1164 if (r
== -EINVAL
&& hostname
)
1165 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1166 log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1168 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
1173 static bool promote_secondaries_enabled(const char *ifname
) {
1174 _cleanup_free_
char *promote_secondaries_sysctl
= NULL
;
1175 char *promote_secondaries_path
;
1178 promote_secondaries_path
= strjoina("net/ipv4/conf/", ifname
, "/promote_secondaries");
1179 r
= sysctl_read(promote_secondaries_path
, &promote_secondaries_sysctl
);
1181 log_debug_errno(r
, "Cannot read sysctl %s", promote_secondaries_path
);
1185 truncate_nl(promote_secondaries_sysctl
);
1186 r
= parse_boolean(promote_secondaries_sysctl
);
1188 log_warning_errno(r
, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path
, promote_secondaries_sysctl
);
1192 /* dhcp4_set_promote_secondaries will ensure this interface has
1193 * the "promote_secondaries" option in the kernel set. If this sysctl
1194 * is not set DHCP will work only as long as the IP address does not
1195 * changes between leases. The kernel will remove all secondary IP
1196 * addresses of an interface otherwise. The way systemd-network works
1197 * is that the new IP of a lease is added as a secondary IP and when
1198 * the primary one expires it relies on the kernel to promote the
1199 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1201 static int dhcp4_set_promote_secondaries(Link
*link
) {
1206 /* check if the kernel has promote_secondaries enabled for our
1207 * interface. If it is not globally enabled or enabled for the
1208 * specific interface we must either enable it.
1210 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link
->ifname
))) {
1211 char *promote_secondaries_path
= NULL
;
1213 log_link_debug(link
, "promote_secondaries is unset, setting it");
1214 promote_secondaries_path
= strjoina("net/ipv4/conf/", link
->ifname
, "/promote_secondaries");
1215 r
= sysctl_write(promote_secondaries_path
, "1");
1217 log_link_warning_errno(link
, r
, "cannot set sysctl %s to 1", promote_secondaries_path
);
1224 static int dhcp4_set_client_identifier(Link
*link
) {
1228 assert(link
->network
);
1229 assert(link
->dhcp_client
);
1231 switch (link
->network
->dhcp_client_identifier
) {
1232 case DHCP_CLIENT_ID_DUID
: {
1233 /* If configured, apply user specified DUID and IAID */
1234 const DUID
*duid
= link_get_duid(link
);
1236 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1237 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1238 link
->network
->iaid_set
,
1239 link
->network
->iaid
,
1242 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
1243 link
->network
->iaid_set
,
1244 link
->network
->iaid
,
1246 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1247 duid
->raw_data_len
);
1249 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1252 case DHCP_CLIENT_ID_DUID_ONLY
: {
1253 /* If configured, apply user specified DUID */
1254 const DUID
*duid
= link_get_duid(link
);
1256 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1257 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
1260 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
1262 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1263 duid
->raw_data_len
);
1265 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
1268 case DHCP_CLIENT_ID_MAC
:
1269 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1271 (const uint8_t *) &link
->mac
,
1274 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
1277 assert_not_reached("Unknown client identifier type.");
1283 static int dhcp4_init(Link
*link
) {
1288 if (link
->dhcp_client
)
1291 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1295 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, link
->manager
->event
, 0);
1302 int dhcp4_configure(Link
*link
) {
1303 sd_dhcp_option
*send_option
;
1304 void *request_options
;
1308 assert(link
->network
);
1310 if (!link_dhcp4_enabled(link
))
1313 r
= dhcp4_set_promote_secondaries(link
);
1317 r
= dhcp4_init(link
);
1319 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to initialize DHCP4 client: %m");
1321 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1322 (const uint8_t *) &link
->mac
,
1323 sizeof (link
->mac
), ARPHRD_ETHER
);
1325 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1327 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1329 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1331 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1333 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1335 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
,
1336 link
->network
->dhcp_broadcast
);
1338 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1341 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1343 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1346 if (link
->network
->dhcp_use_mtu
) {
1347 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1348 SD_DHCP_OPTION_INTERFACE_MTU
);
1350 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1353 /* NOTE: even if this variable is called "use", it also "sends" PRL
1354 * options, maybe there should be a different configuration variable
1355 * to send or not route options?. */
1356 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1357 * by default, so they don't need to be added here. */
1358 if (link
->network
->dhcp_use_routes
&& !link
->network
->dhcp_anonymize
) {
1359 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1360 SD_DHCP_OPTION_STATIC_ROUTE
);
1362 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1364 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1365 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1367 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1370 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
&& !link
->network
->dhcp_anonymize
) {
1371 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
);
1373 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1376 if (link
->network
->dhcp_use_ntp
) {
1377 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1379 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1382 if (link
->network
->dhcp_use_sip
) {
1383 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_SIP_SERVER
);
1385 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1388 if (link
->network
->dhcp_use_timezone
) {
1389 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1391 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1394 SET_FOREACH(request_options
, link
->network
->dhcp_request_options
) {
1395 uint32_t option
= PTR_TO_UINT32(request_options
);
1397 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, option
);
1399 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option
);
1402 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_options
) {
1403 r
= sd_dhcp_client_add_option(link
->dhcp_client
, send_option
);
1407 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1410 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_vendor_options
) {
1411 r
= sd_dhcp_client_add_vendor_option(link
->dhcp_client
, send_option
);
1415 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1418 r
= dhcp4_set_hostname(link
);
1422 if (link
->network
->dhcp_vendor_class_identifier
) {
1423 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1424 link
->network
->dhcp_vendor_class_identifier
);
1426 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1429 if (link
->network
->dhcp_mudurl
) {
1430 r
= sd_dhcp_client_set_mud_url(link
->dhcp_client
,
1431 link
->network
->dhcp_mudurl
);
1433 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1436 if (link
->network
->dhcp_user_class
) {
1437 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, (const char **) link
->network
->dhcp_user_class
);
1439 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1442 if (link
->network
->dhcp_client_port
) {
1443 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1445 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen port: %m");
1448 if (link
->network
->dhcp_max_attempts
> 0) {
1449 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1451 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1454 if (link
->network
->ip_service_type
> 0) {
1455 r
= sd_dhcp_client_set_service_type(link
->dhcp_client
, link
->network
->ip_service_type
);
1457 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IP service type: %m");
1460 if (link
->network
->dhcp_fallback_lease_lifetime
> 0) {
1461 r
= sd_dhcp_client_set_fallback_lease_lifetime(link
->dhcp_client
, link
->network
->dhcp_fallback_lease_lifetime
);
1463 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1466 r
= dhcp4_configure_dad(link
);
1468 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to configure service type: %m");
1470 return dhcp4_set_client_identifier(link
);
1473 int dhcp4_update_mac(Link
*link
) {
1478 if (!link
->dhcp_client
)
1481 r
= sd_dhcp_client_set_mac(link
->dhcp_client
, (const uint8_t *) &link
->mac
, sizeof (link
->mac
), ARPHRD_ETHER
);
1485 r
= dhcp4_set_client_identifier(link
);
1489 r
= dhcp4_dad_update_mac(link
);
1496 int link_deserialize_dhcp4(Link
*link
, const char *dhcp4_address
) {
1497 union in_addr_union address
;
1502 if (isempty(dhcp4_address
))
1505 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
1507 return log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address: %s", dhcp4_address
);
1509 r
= dhcp4_init(link
);
1511 return log_link_debug_errno(link
, r
, "Failed to initialize DHCPv4 client: %m");
1513 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
1515 return log_link_debug_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
1520 int config_parse_dhcp_max_attempts(
1522 const char *filename
,
1524 const char *section
,
1525 unsigned section_line
,
1532 Network
*network
= data
;
1540 if (isempty(rvalue
)) {
1541 network
->dhcp_max_attempts
= 0;
1545 if (streq(rvalue
, "infinity")) {
1546 network
->dhcp_max_attempts
= (uint64_t) -1;
1550 r
= safe_atou64(rvalue
, &a
);
1552 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1553 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1558 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1559 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1563 network
->dhcp_max_attempts
= a
;
1568 int config_parse_dhcp_acl_ip_address(
1570 const char *filename
,
1572 const char *section
,
1573 unsigned section_line
,
1580 Network
*network
= data
;
1589 acl
= STR_IN_SET(lvalue
, "DenyList", "BlackList") ? &network
->dhcp_deny_listed_ip
: &network
->dhcp_allow_listed_ip
;
1591 if (isempty(rvalue
)) {
1592 *acl
= set_free(*acl
);
1596 for (const char *p
= rvalue
;;) {
1597 _cleanup_free_
char *n
= NULL
;
1598 union in_addr_union ip
;
1600 r
= extract_first_word(&p
, &n
, NULL
, 0);
1604 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1605 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1612 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1614 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1615 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue
, n
);
1619 r
= set_ensure_put(acl
, NULL
, UINT32_TO_PTR(ip
.in
.s_addr
));
1621 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1622 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue
, n
);
1626 int config_parse_dhcp_ip_service_type(
1628 const char *filename
,
1630 const char *section
,
1631 unsigned section_line
,
1642 if (streq(rvalue
, "CS4"))
1643 *((int *)data
) = IPTOS_CLASS_CS4
;
1644 else if (streq(rvalue
, "CS6"))
1645 *((int *)data
) = IPTOS_CLASS_CS6
;
1647 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1648 "Failed to parse IPServiceType type '%s', ignoring.", rvalue
);
1653 int config_parse_dhcp_mud_url(
1655 const char *filename
,
1657 const char *section
,
1658 unsigned section_line
,
1665 _cleanup_free_
char *unescaped
= NULL
;
1666 Network
*network
= data
;
1673 if (isempty(rvalue
)) {
1674 network
->dhcp_mudurl
= mfree(network
->dhcp_mudurl
);
1678 r
= cunescape(rvalue
, 0, &unescaped
);
1680 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1681 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue
);
1685 if (!http_url_is_valid(unescaped
) || strlen(unescaped
) > 255) {
1686 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1687 "Failed to parse MUD URL '%s', ignoring: %m", rvalue
);
1692 return free_and_strdup_warn(&network
->dhcp_mudurl
, unescaped
);
1695 int config_parse_dhcp_fallback_lease_lifetime(const char *unit
,
1696 const char *filename
,
1698 const char *section
,
1699 unsigned section_line
,
1705 Network
*network
= userdata
;
1714 if (isempty(rvalue
)) {
1715 network
->dhcp_fallback_lease_lifetime
= 0;
1719 /* We accept only "forever" or "infinity". */
1720 if (STR_IN_SET(rvalue
, "forever", "infinity"))
1721 k
= CACHE_INFO_INFINITY_LIFE_TIME
;
1723 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1724 "Invalid LeaseLifetime= value, ignoring: %s", rvalue
);
1728 network
->dhcp_fallback_lease_lifetime
= k
;
1733 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1734 [DHCP_CLIENT_ID_MAC
] = "mac",
1735 [DHCP_CLIENT_ID_DUID
] = "duid",
1736 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1739 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1740 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1741 "Failed to parse client identifier type");