1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
5 #include <linux/if_arp.h>
7 #include "alloc-util.h"
8 #include "dhcp-client-internal.h"
9 #include "hostname-util.h"
10 #include "parse-util.h"
11 #include "network-internal.h"
12 #include "networkd-dhcp4.h"
13 #include "networkd-link.h"
14 #include "networkd-manager.h"
15 #include "networkd-network.h"
16 #include "string-table.h"
17 #include "string-util.h"
18 #include "sysctl-util.h"
20 static int dhcp_remove_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
21 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
22 static int dhcp_remove_dns_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
23 static int dhcp_remove_address(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, link_netlink_message_handler_t callback
);
24 static int dhcp_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
);
25 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
);
27 void dhcp4_release_old_lease(Link
*link
) {
28 struct in_addr address
= {}, address_old
= {};
32 if (!link
->dhcp_lease_old
)
35 assert(link
->dhcp_lease
);
37 (void) sd_dhcp_lease_get_address(link
->dhcp_lease_old
, &address_old
);
38 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
40 (void) dhcp_remove_routes(link
, link
->dhcp_lease_old
, &address_old
, false);
41 (void) dhcp_remove_router(link
, link
->dhcp_lease_old
, &address_old
, false);
42 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease_old
, &address_old
, false);
44 if (!in4_addr_equal(&address_old
, &address
))
45 (void) dhcp_remove_address(link
, link
->dhcp_lease_old
, &address_old
, NULL
);
47 link
->dhcp_lease_old
= sd_dhcp_lease_unref(link
->dhcp_lease_old
);
51 static void dhcp4_check_ready(Link
*link
) {
52 if (link
->dhcp4_messages
== 0) {
53 link
->dhcp4_configured
= true;
54 /* New address and routes are configured now. Let's release old lease. */
55 dhcp4_release_old_lease(link
);
56 link_check_ready(link
);
60 static int dhcp4_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
64 assert(link
->dhcp4_messages
> 0);
66 link
->dhcp4_messages
--;
68 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
71 r
= sd_netlink_message_get_errno(m
);
72 if (r
== -ENETUNREACH
&& !link
->dhcp4_route_retrying
) {
74 /* It seems kernel does not support that the prefix route cannot be configured with
75 * route table. Let's once drop the config and reconfigure them later. */
77 log_link_message_debug_errno(link
, m
, r
, "Could not set DHCPv4 route, retrying later: %m");
78 link
->dhcp4_route_failed
= true;
79 link
->manager
->dhcp4_prefix_root_cannot_set_table
= true;
80 } else if (r
< 0 && r
!= -EEXIST
) {
81 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 route: %m");
82 link_enter_failed(link
);
86 if (link
->dhcp4_messages
== 0) {
87 if (link
->dhcp4_route_failed
) {
88 struct in_addr address
= {};
90 link
->dhcp4_route_failed
= false;
91 link
->dhcp4_route_retrying
= true;
93 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
94 (void) dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true);
95 (void) dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true);
96 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true);
97 (void) dhcp_remove_address(link
, link
->dhcp_lease
, &address
, dhcp_remove_address_handler
);
101 if (!link
->network
->dhcp_send_decline
)
102 dhcp4_check_ready(link
);
108 static int route_scope_from_address(const Route
*route
, const struct in_addr
*self_addr
) {
112 if (in4_addr_is_localhost(&route
->dst
.in
) ||
113 (!in4_addr_is_null(self_addr
) && in4_addr_equal(&route
->dst
.in
, self_addr
)))
114 return RT_SCOPE_HOST
;
115 else if (in4_addr_is_null(&route
->gw
.in
))
116 return RT_SCOPE_LINK
;
118 return RT_SCOPE_UNIVERSE
;
121 static bool link_prefixroute(Link
*link
) {
122 return !link
->network
->dhcp_route_table_set
||
123 link
->network
->dhcp_route_table
== RT_TABLE_MAIN
||
124 link
->manager
->dhcp4_prefix_root_cannot_set_table
;
127 static int dhcp_route_configure(Route
**route
, Link
*link
) {
134 if (set_contains(link
->dhcp_routes
, *route
))
137 r
= route_configure(*route
, link
, dhcp4_route_handler
);
141 link
->dhcp4_messages
++;
143 r
= set_put(link
->dhcp_routes
, *route
);
151 static int link_set_dns_routes(Link
*link
, const struct in_addr
*address
) {
152 const struct in_addr
*dns
;
157 assert(link
->dhcp_lease
);
158 assert(link
->network
);
160 if (!link
->network
->dhcp_use_dns
||
161 !link
->network
->dhcp_routes_to_dns
)
164 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &dns
);
165 if (IN_SET(n
, 0, -ENODATA
))
168 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
170 table
= link_get_dhcp_route_table(link
);
172 for (i
= 0; i
< n
; i
++) {
173 _cleanup_(route_freep
) Route
*route
= NULL
;
175 r
= route_new(&route
);
177 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
179 /* Set routes to DNS servers. */
181 route
->family
= AF_INET
;
182 route
->dst
.in
= dns
[i
];
183 route
->dst_prefixlen
= 32;
184 route
->prefsrc
.in
= *address
;
185 route
->scope
= RT_SCOPE_LINK
;
186 route
->protocol
= RTPROT_DHCP
;
187 route
->priority
= link
->network
->dhcp_route_metric
;
188 route
->table
= table
;
190 r
= dhcp_route_configure(&route
, link
);
192 return log_link_error_errno(link
, r
, "Could not set route to DNS server: %m");
198 static int dhcp_prefix_route_from_lease(
199 const sd_dhcp_lease
*lease
,
201 const struct in_addr
*address
,
205 struct in_addr netmask
;
208 r
= sd_dhcp_lease_get_netmask((sd_dhcp_lease
*) lease
, &netmask
);
212 r
= route_new(&route
);
216 route
->family
= AF_INET
;
217 route
->dst
.in
.s_addr
= address
->s_addr
& netmask
.s_addr
;
218 route
->dst_prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
219 route
->prefsrc
.in
= *address
;
220 route
->scope
= RT_SCOPE_LINK
;
221 route
->protocol
= RTPROT_DHCP
;
222 route
->table
= table
;
227 static int link_set_dhcp_routes(Link
*link
) {
228 _cleanup_free_ sd_dhcp_route
**static_routes
= NULL
;
229 bool classless_route
= false, static_route
= false;
230 const struct in_addr
*router
;
231 struct in_addr address
;
237 if (!link
->dhcp_lease
) /* link went down while we configured the IP addresses? */
240 if (!link
->network
) /* link went down while we configured the IP addresses? */
243 if (!link
->network
->dhcp_use_routes
)
246 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
247 /* During configuring addresses, the link lost its carrier. As networkd is dropping
248 * the addresses now, let's not configure the routes either. */
251 r
= set_ensure_allocated(&link
->dhcp_routes
, &route_hash_ops
);
255 /* Clear old entries in case the set was already allocated */
256 set_clear(link
->dhcp_routes
);
258 table
= link_get_dhcp_route_table(link
);
260 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
262 return log_link_warning_errno(link
, r
, "DHCP error: could not get address: %m");
264 if (!link_prefixroute(link
)) {
265 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
267 r
= dhcp_prefix_route_from_lease(link
->dhcp_lease
, table
, &address
, &prefix_route
);
269 return log_link_error_errno(link
, r
, "Could not create prefix route: %m");
271 r
= dhcp_route_configure(&prefix_route
, link
);
273 return log_link_error_errno(link
, r
, "Could not set prefix route: %m");
276 n
= sd_dhcp_lease_get_routes(link
->dhcp_lease
, &static_routes
);
278 log_link_debug_errno(link
, n
, "DHCP: No routes received from DHCP server: %m");
280 log_link_debug_errno(link
, n
, "DHCP: could not get routes: %m");
282 for (i
= 0; i
< n
; i
++) {
283 switch (sd_dhcp_route_get_option(static_routes
[i
])) {
284 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
285 classless_route
= true;
287 case SD_DHCP_OPTION_STATIC_ROUTE
:
293 for (i
= 0; i
< n
; i
++) {
294 _cleanup_(route_freep
) Route
*route
= NULL
;
296 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
297 the DHCP client MUST ignore the Static Routes option. */
298 if (classless_route
&&
299 sd_dhcp_route_get_option(static_routes
[i
]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
)
302 r
= route_new(&route
);
304 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
306 route
->family
= AF_INET
;
307 route
->protocol
= RTPROT_DHCP
;
308 assert_se(sd_dhcp_route_get_gateway(static_routes
[i
], &route
->gw
.in
) >= 0);
309 assert_se(sd_dhcp_route_get_destination(static_routes
[i
], &route
->dst
.in
) >= 0);
310 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &route
->dst_prefixlen
) >= 0);
311 route
->priority
= link
->network
->dhcp_route_metric
;
312 route
->table
= table
;
313 route
->mtu
= link
->network
->dhcp_route_mtu
;
314 route
->scope
= route_scope_from_address(route
, &address
);
315 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
316 route
->prefsrc
.in
= address
;
318 if (set_contains(link
->dhcp_routes
, route
))
321 r
= dhcp_route_configure(&route
, link
);
323 return log_link_error_errno(link
, r
, "Could not set route: %m");
326 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
327 if (IN_SET(r
, 0, -ENODATA
))
328 log_link_info(link
, "DHCP: No gateway received from DHCP server.");
330 log_link_warning_errno(link
, r
, "DHCP error: could not get gateway: %m");
331 else if (in4_addr_is_null(&router
[0]))
332 log_link_info(link
, "DHCP: Received gateway is null.");
334 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
335 a Router option, the DHCP client MUST ignore the Router option. */
336 if (classless_route
&& static_route
)
337 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring static-route option and router option");
339 if (r
> 0 && !classless_route
&& !in4_addr_is_null(&router
[0])) {
340 _cleanup_(route_freep
) Route
*route
= NULL
, *route_gw
= NULL
;
342 r
= route_new(&route_gw
);
344 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
346 /* The dhcp netmask may mask out the gateway. Add an explicit
347 * route for the gw host so that we can route no matter the
348 * netmask or existing kernel route tables. */
349 route_gw
->family
= AF_INET
;
350 route_gw
->dst
.in
= router
[0];
351 route_gw
->dst_prefixlen
= 32;
352 route_gw
->prefsrc
.in
= address
;
353 route_gw
->scope
= RT_SCOPE_LINK
;
354 route_gw
->protocol
= RTPROT_DHCP
;
355 route_gw
->priority
= link
->network
->dhcp_route_metric
;
356 route_gw
->table
= table
;
357 route_gw
->mtu
= link
->network
->dhcp_route_mtu
;
359 r
= dhcp_route_configure(&route_gw
, link
);
361 return log_link_error_errno(link
, r
, "Could not set host route: %m");
363 r
= route_new(&route
);
365 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
367 route
->family
= AF_INET
;
368 route
->gw
.in
= router
[0];
369 route
->prefsrc
.in
= address
;
370 route
->protocol
= RTPROT_DHCP
;
371 route
->priority
= link
->network
->dhcp_route_metric
;
372 route
->table
= table
;
373 route
->mtu
= link
->network
->dhcp_route_mtu
;
375 r
= dhcp_route_configure(&route
, link
);
377 return log_link_error_errno(link
, r
, "Could not set router: %m");
381 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
382 if (!rt
->gateway_from_dhcp
)
385 if (rt
->family
!= AF_INET
)
388 rt
->gw
.in
= router
[0];
390 r
= route_configure(rt
, link
, dhcp4_route_handler
);
392 return log_link_error_errno(link
, r
, "Could not set gateway: %m");
394 link
->dhcp4_messages
++;
397 return link_set_dns_routes(link
, &address
);
400 static int dhcp_remove_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
401 _cleanup_free_ sd_dhcp_route
**routes
= NULL
;
408 if (!link
->network
->dhcp_use_routes
)
411 n
= sd_dhcp_lease_get_routes(lease
, &routes
);
412 if (IN_SET(n
, 0, -ENODATA
))
415 return log_link_error_errno(link
, n
, "DHCP error: Failed to get routes: %m");
417 table
= link_get_dhcp_route_table(link
);
419 for (i
= 0; i
< n
; i
++) {
420 _cleanup_(route_freep
) Route
*route
= NULL
;
422 r
= route_new(&route
);
426 route
->family
= AF_INET
;
427 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &route
->gw
.in
) >= 0);
428 assert_se(sd_dhcp_route_get_destination(routes
[i
], &route
->dst
.in
) >= 0);
429 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &route
->dst_prefixlen
) >= 0);
430 route
->priority
= link
->network
->dhcp_route_metric
;
431 route
->table
= table
;
432 route
->scope
= route_scope_from_address(route
, address
);
433 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
434 route
->prefsrc
.in
= *address
;
436 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
439 (void) route_remove(route
, link
, NULL
);
445 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
446 _cleanup_(route_freep
) Route
*route_gw
= NULL
, *route
= NULL
;
447 const struct in_addr
*router
;
454 if (!link
->network
->dhcp_use_routes
)
457 r
= sd_dhcp_lease_get_router(lease
, &router
);
458 if (IN_SET(r
, 0, -ENODATA
)) {
459 log_link_debug(link
, "DHCP: No gateway received from DHCP server.");
462 return log_link_error_errno(link
, r
, "DHCP error: could not get gateway: %m");
463 else if (in4_addr_is_null(&router
[0])) {
464 log_link_info(link
, "DHCP: Received gateway is null, ignoring.");
468 table
= link_get_dhcp_route_table(link
);
470 r
= route_new(&route_gw
);
474 route_gw
->family
= AF_INET
;
475 route_gw
->dst
.in
= router
[0];
476 route_gw
->dst_prefixlen
= 32;
477 route_gw
->prefsrc
.in
= *address
;
478 route_gw
->scope
= RT_SCOPE_LINK
;
479 route_gw
->protocol
= RTPROT_DHCP
;
480 route_gw
->priority
= link
->network
->dhcp_route_metric
;
481 route_gw
->table
= table
;
483 if (remove_all
|| !set_contains(link
->dhcp_routes
, route_gw
))
484 (void) route_remove(route_gw
, link
, NULL
);
486 r
= route_new(&route
);
490 route
->family
= AF_INET
;
491 route
->gw
.in
= router
[0];
492 route
->prefsrc
.in
= *address
;
493 route
->protocol
= RTPROT_DHCP
;
494 route
->priority
= link
->network
->dhcp_route_metric
;
495 route
->table
= table
;
497 if (remove_all
|| !set_contains(link
->dhcp_routes
, route
))
498 (void) route_remove(route
, link
, NULL
);
501 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
502 if (!rt
->gateway_from_dhcp
)
505 if (rt
->family
!= AF_INET
)
508 if (!remove_all
&& in4_addr_equal(router
, &rt
->gw
.in
))
511 (void) route_remove(rt
, link
, NULL
);
517 static int dhcp_remove_dns_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
518 const struct in_addr
*dns
;
524 assert(link
->network
);
526 if (!link
->network
->dhcp_use_dns
||
527 !link
->network
->dhcp_routes_to_dns
)
530 n
= sd_dhcp_lease_get_dns(lease
, &dns
);
531 if (IN_SET(n
, 0, -ENODATA
))
534 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
536 table
= link_get_dhcp_route_table(link
);
538 for (i
= 0; i
< n
; i
++) {
539 _cleanup_(route_freep
) Route
*route
= NULL
;
541 r
= route_new(&route
);
543 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
545 route
->family
= AF_INET
;
546 route
->dst
.in
= dns
[i
];
547 route
->dst_prefixlen
= 32;
548 route
->prefsrc
.in
= *address
;
549 route
->scope
= RT_SCOPE_LINK
;
550 route
->protocol
= RTPROT_DHCP
;
551 route
->priority
= link
->network
->dhcp_route_metric
;
552 route
->table
= table
;
554 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
557 (void) route_remove(route
, link
, NULL
);
560 if (!link_prefixroute(link
)) {
561 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
563 r
= dhcp_prefix_route_from_lease(lease
, table
, address
, &prefix_route
);
565 return log_link_warning_errno(link
, r
, "Could not delete prefix route: %m");
567 if (remove_all
|| !set_contains(link
->dhcp_routes
, prefix_route
))
568 (void) route_remove(prefix_route
, link
, NULL
);
574 static int dhcp_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
579 /* This is only used when retrying to assign the address received from DHCPv4 server.
580 * See dhcp4_route_handler(). */
582 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
585 r
= sd_netlink_message_get_errno(m
);
587 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 address, ignoring");
589 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
591 (void) dhcp_lease_renew(link
->dhcp_client
, link
);
595 static int dhcp_remove_address(
596 Link
*link
, sd_dhcp_lease
*lease
,
597 const struct in_addr
*address
,
598 link_netlink_message_handler_t callback
) {
600 _cleanup_(address_freep
) Address
*a
= NULL
;
601 struct in_addr netmask
;
607 if (in4_addr_is_null(address
))
615 a
->in_addr
.in
= *address
;
617 if (sd_dhcp_lease_get_netmask(lease
, &netmask
) >= 0)
618 a
->prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
620 (void) address_remove(a
, link
, callback
);
625 static int dhcp_reset_mtu(Link
*link
) {
631 if (!link
->network
->dhcp_use_mtu
)
634 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
638 if (link
->original_mtu
== mtu
)
641 r
= link_set_mtu(link
, link
->original_mtu
);
643 log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
644 link_enter_failed(link
);
651 static int dhcp_reset_hostname(Link
*link
) {
652 const char *hostname
;
657 if (!link
->network
->dhcp_use_hostname
)
660 hostname
= link
->network
->dhcp_hostname
;
662 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
667 /* If a hostname was set due to the lease, then unset it now. */
668 r
= manager_set_hostname(link
->manager
, NULL
);
670 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
675 static int dhcp_lease_lost(Link
*link
) {
676 struct in_addr address
= {};
679 assert(link
->dhcp_lease
);
681 log_link_info(link
, "DHCP lease lost");
683 link
->dhcp4_configured
= false;
685 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
686 (void) dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true);
687 (void) dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true);
688 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true);
689 (void) dhcp_remove_address(link
, link
->dhcp_lease
, &address
, NULL
);
690 (void) dhcp_reset_mtu(link
);
691 (void) dhcp_reset_hostname(link
);
693 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
699 static void dhcp_address_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
700 _cleanup_free_
char *pretty
= NULL
;
701 union in_addr_union address
= {};
711 case SD_IPV4ACD_EVENT_STOP
:
712 log_link_debug(link
, "Stopping ACD client for DHCP4...");
715 case SD_IPV4ACD_EVENT_BIND
:
717 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
718 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
719 log_link_debug(link
, "Successfully claimed DHCP4 address %s", strna(pretty
));
721 dhcp4_check_ready(link
);
724 case SD_IPV4ACD_EVENT_CONFLICT
:
725 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
726 (void) in_addr_to_string(AF_INET
, &address
, &pretty
);
727 log_link_warning(link
, "DAD conflict. Dropping DHCP4 address %s", strna(pretty
));
729 (void) sd_dhcp_client_send_decline(link
->dhcp_client
);
731 if (link
->dhcp_lease
) {
732 r
= dhcp_lease_lost(link
);
734 link_enter_failed(link
);
739 assert_not_reached("Invalid IPv4ACD event.");
742 sd_ipv4acd_stop(acd
);
747 static int configure_dhcpv4_duplicate_address_detection(Link
*link
) {
752 r
= sd_ipv4acd_new(&link
->network
->dhcp_acd
);
756 r
= sd_ipv4acd_attach_event(link
->network
->dhcp_acd
, NULL
, 0);
760 r
= sd_ipv4acd_set_ifindex(link
->network
->dhcp_acd
, link
->ifindex
);
764 r
= sd_ipv4acd_set_mac(link
->network
->dhcp_acd
, &link
->mac
);
771 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
776 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
779 r
= sd_netlink_message_get_errno(m
);
780 if (r
< 0 && r
!= -EEXIST
) {
781 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 address");
782 link_enter_failed(link
);
785 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
787 r
= link_set_dhcp_routes(link
);
789 link_enter_failed(link
);
793 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
794 r
= link_request_set_routes(link
);
796 link_enter_failed(link
);
800 if (link
->network
->dhcp_send_decline
) {
801 union in_addr_union addr
;
803 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &addr
.in
);
805 r
= sd_ipv4acd_set_address(link
->network
->dhcp_acd
, &addr
.in
);
809 r
= sd_ipv4acd_set_callback(link
->network
->dhcp_acd
, dhcp_address_on_acd
, link
);
814 _cleanup_free_
char *pretty
= NULL
;
816 (void) in_addr_to_string(AF_INET
, &addr
, &pretty
);
817 log_link_debug(link
, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty
));
820 r
= sd_ipv4acd_start(link
->network
->dhcp_acd
, true);
822 log_link_warning_errno(link
, r
, "Failed to start IPv4ACD client, ignoring: %m");
824 dhcp4_check_ready(link
);
829 static int dhcp4_update_address(Link
*link
,
830 struct in_addr
*address
,
831 struct in_addr
*netmask
,
833 _cleanup_(address_freep
) Address
*addr
= NULL
;
841 prefixlen
= in4_addr_netmask_to_prefixlen(netmask
);
843 r
= address_new(&addr
);
847 addr
->family
= AF_INET
;
848 addr
->in_addr
.in
.s_addr
= address
->s_addr
;
849 addr
->cinfo
.ifa_prefered
= lifetime
;
850 addr
->cinfo
.ifa_valid
= lifetime
;
851 addr
->prefixlen
= prefixlen
;
852 addr
->broadcast
.s_addr
= address
->s_addr
| ~netmask
->s_addr
;
853 addr
->prefix_route
= link_prefixroute(link
);
855 /* allow reusing an existing address and simply update its lifetime
856 * in case it already exists */
857 r
= address_configure(addr
, link
, dhcp4_address_handler
, true);
864 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
865 sd_dhcp_lease
*lease
;
866 struct in_addr address
;
867 struct in_addr netmask
;
868 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
873 assert(link
->network
);
875 r
= sd_dhcp_client_get_lease(client
, &lease
);
877 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
879 sd_dhcp_lease_unref(link
->dhcp_lease
);
880 link
->dhcp4_configured
= false;
881 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
884 r
= sd_dhcp_lease_get_address(lease
, &address
);
886 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
888 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
890 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
892 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
893 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
895 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
898 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
900 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
905 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
906 const struct in_addr
*router
;
907 sd_dhcp_lease
*lease
;
908 struct in_addr address
;
909 struct in_addr netmask
;
911 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
917 link
->dhcp4_configured
= false;
919 r
= sd_dhcp_client_get_lease(client
, &lease
);
921 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
923 r
= sd_dhcp_lease_get_address(lease
, &address
);
925 return log_link_error_errno(link
, r
, "DHCP error: No address: %m");
927 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
929 return log_link_error_errno(link
, r
, "DHCP error: No netmask: %m");
931 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
933 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
934 r
= sd_dhcp_lease_get_lifetime(lease
, &lifetime
);
936 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
939 r
= sd_dhcp_lease_get_router(lease
, &router
);
940 if (r
< 0 && r
!= -ENODATA
)
941 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
943 if (r
> 0 && !in4_addr_is_null(&router
[0]))
945 LOG_LINK_INTERFACE(link
),
946 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
947 ADDRESS_FMT_VAL(address
),
949 ADDRESS_FMT_VAL(router
[0])),
950 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
951 "PREFIXLEN=%u", prefixlen
,
952 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router
[0]));
955 LOG_LINK_INTERFACE(link
),
956 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u",
957 ADDRESS_FMT_VAL(address
),
959 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
960 "PREFIXLEN=%u", prefixlen
);
962 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
965 if (link
->network
->dhcp_use_mtu
) {
968 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
970 r
= link_set_mtu(link
, mtu
);
972 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
976 if (link
->network
->dhcp_use_hostname
) {
977 const char *dhcpname
= NULL
;
978 _cleanup_free_
char *hostname
= NULL
;
980 if (link
->network
->dhcp_hostname
)
981 dhcpname
= link
->network
->dhcp_hostname
;
983 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
986 r
= shorten_overlong(dhcpname
, &hostname
);
988 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
990 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
994 r
= manager_set_hostname(link
->manager
, hostname
);
996 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
1000 if (link
->network
->dhcp_use_timezone
) {
1001 const char *tz
= NULL
;
1003 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
1006 r
= manager_set_timezone(link
->manager
, tz
);
1008 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
1012 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
1014 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
1019 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
1022 link
->dhcp_lease_old
= TAKE_PTR(link
->dhcp_lease
);
1024 /* On ip address change, to keep the connectability, we would like to assign new address and
1025 * routes, and then release old lease. There are two possible success paths:
1027 * 1. new address and routes are configured.
1028 * -> handled by dhcp_release_old_lease() in dhcp4_route_handler().
1029 * 2. new address is configured and no route is requested.
1030 * -> handled by dhcp_release_old_lease() in dhcp4_address_handler().
1032 * On error in assigning new address and routes, then the link always enters to the failed
1033 * state. And link_enter_failed() leads to the DHCP client to be stopped. So,
1034 * dhcp_release_old_lease() will be also called by link_stop_clients().
1037 r
= dhcp_lease_acquired(client
, link
);
1039 /* If it fails, then the new address is not configured yet.
1040 * So, let's simply drop the old lease. */
1041 sd_dhcp_lease_unref(link
->dhcp_lease
);
1042 link
->dhcp_lease
= TAKE_PTR(link
->dhcp_lease_old
);
1043 (void) dhcp_lease_lost(link
);
1050 static int dhcp_server_is_black_listed(Link
*link
, sd_dhcp_client
*client
) {
1051 sd_dhcp_lease
*lease
;
1052 struct in_addr addr
;
1056 assert(link
->network
);
1059 r
= sd_dhcp_client_get_lease(client
, &lease
);
1061 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
1063 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
1065 return log_link_debug_errno(link
, r
, "Failed to get DHCP server ip address: %m");
1067 if (set_contains(link
->network
->dhcp_black_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
1068 log_struct(LOG_DEBUG
,
1069 LOG_LINK_INTERFACE(link
),
1070 LOG_LINK_MESSAGE(link
, "DHCPv4 ip '%u.%u.%u.%u' found in black listed ip addresses, ignoring offer",
1071 ADDRESS_FMT_VAL(addr
)));
1078 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
1079 Link
*link
= userdata
;
1083 assert(link
->network
);
1084 assert(link
->manager
);
1086 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1090 case SD_DHCP_CLIENT_EVENT_STOP
:
1092 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
)) {
1093 assert(link
->ipv4ll
);
1095 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
1097 r
= sd_ipv4ll_start(link
->ipv4ll
);
1099 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1102 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1103 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1107 if (link
->dhcp_lease
) {
1108 if (link
->network
->dhcp_send_release
)
1109 (void) sd_dhcp_client_send_release(client
);
1111 r
= dhcp_lease_lost(link
);
1113 link_enter_failed(link
);
1119 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
1120 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1121 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1125 if (link
->dhcp_lease
) {
1126 r
= dhcp_lease_lost(link
);
1128 link_enter_failed(link
);
1134 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
1135 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1136 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1140 r
= dhcp_lease_ip_change(client
, link
);
1142 link_enter_failed(link
);
1147 case SD_DHCP_CLIENT_EVENT_RENEW
:
1148 r
= dhcp_lease_renew(client
, link
);
1150 link_enter_failed(link
);
1154 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1155 r
= dhcp_lease_acquired(client
, link
);
1157 link_enter_failed(link
);
1161 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1162 r
= dhcp_server_is_black_listed(link
, client
);
1171 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1173 log_link_warning(link
, "DHCP unknown event: %i", event
);
1180 static int dhcp4_set_hostname(Link
*link
) {
1181 _cleanup_free_
char *hostname
= NULL
;
1187 if (!link
->network
->dhcp_send_hostname
)
1189 else if (link
->network
->dhcp_hostname
)
1190 hn
= link
->network
->dhcp_hostname
;
1192 r
= gethostname_strict(&hostname
);
1193 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1199 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1200 if (r
== -EINVAL
&& hostname
)
1201 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1202 log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1204 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
1209 static bool promote_secondaries_enabled(const char *ifname
) {
1210 _cleanup_free_
char *promote_secondaries_sysctl
= NULL
;
1211 char *promote_secondaries_path
;
1214 promote_secondaries_path
= strjoina("net/ipv4/conf/", ifname
, "/promote_secondaries");
1215 r
= sysctl_read(promote_secondaries_path
, &promote_secondaries_sysctl
);
1217 log_debug_errno(r
, "Cannot read sysctl %s", promote_secondaries_path
);
1221 truncate_nl(promote_secondaries_sysctl
);
1222 r
= parse_boolean(promote_secondaries_sysctl
);
1224 log_warning_errno(r
, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path
, promote_secondaries_sysctl
);
1228 /* dhcp4_set_promote_secondaries will ensure this interface has
1229 * the "promote_secondaries" option in the kernel set. If this sysctl
1230 * is not set DHCP will work only as long as the IP address does not
1231 * changes between leases. The kernel will remove all secondary IP
1232 * addresses of an interface otherwise. The way systemd-network works
1233 * is that the new IP of a lease is added as a secondary IP and when
1234 * the primary one expires it relies on the kernel to promote the
1235 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1237 int dhcp4_set_promote_secondaries(Link
*link
) {
1241 assert(link
->network
);
1242 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1244 /* check if the kernel has promote_secondaries enabled for our
1245 * interface. If it is not globally enabled or enabled for the
1246 * specific interface we must either enable it.
1248 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link
->ifname
))) {
1249 char *promote_secondaries_path
= NULL
;
1251 log_link_debug(link
, "promote_secondaries is unset, setting it");
1252 promote_secondaries_path
= strjoina("net/ipv4/conf/", link
->ifname
, "/promote_secondaries");
1253 r
= sysctl_write(promote_secondaries_path
, "1");
1255 log_link_warning_errno(link
, r
, "cannot set sysctl %s to 1", promote_secondaries_path
);
1262 int dhcp4_set_client_identifier(Link
*link
) {
1266 assert(link
->network
);
1267 assert(link
->dhcp_client
);
1269 switch (link
->network
->dhcp_client_identifier
) {
1270 case DHCP_CLIENT_ID_DUID
: {
1271 /* If configured, apply user specified DUID and IAID */
1272 const DUID
*duid
= link_get_duid(link
);
1274 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1275 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1276 link
->network
->iaid_set
,
1277 link
->network
->iaid
,
1280 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
1281 link
->network
->iaid_set
,
1282 link
->network
->iaid
,
1284 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1285 duid
->raw_data_len
);
1287 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1290 case DHCP_CLIENT_ID_DUID_ONLY
: {
1291 /* If configured, apply user specified DUID */
1292 const DUID
*duid
= link_get_duid(link
);
1294 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1295 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
1298 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
1300 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1301 duid
->raw_data_len
);
1303 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
1306 case DHCP_CLIENT_ID_MAC
:
1307 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1309 (const uint8_t *) &link
->mac
,
1312 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
1315 assert_not_reached("Unknown client identifier type.");
1321 int dhcp4_configure(Link
*link
) {
1322 sd_dhcp_option
*send_option
;
1323 void *request_options
;
1328 assert(link
->network
);
1329 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1331 if (!link
->dhcp_client
) {
1332 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1336 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
1339 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
1341 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to attach event: %m");
1343 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1344 (const uint8_t *) &link
->mac
,
1345 sizeof (link
->mac
), ARPHRD_ETHER
);
1347 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1349 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1351 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1353 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1355 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1357 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
,
1358 link
->network
->dhcp_broadcast
);
1360 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1363 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1365 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1368 if (link
->network
->dhcp_use_mtu
) {
1369 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1370 SD_DHCP_OPTION_INTERFACE_MTU
);
1372 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1375 /* NOTE: even if this variable is called "use", it also "sends" PRL
1376 * options, maybe there should be a different configuration variable
1377 * to send or not route options?. */
1378 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1379 * by default, so they don't need to be added here. */
1380 if (link
->network
->dhcp_use_routes
&& !link
->network
->dhcp_anonymize
) {
1381 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1382 SD_DHCP_OPTION_STATIC_ROUTE
);
1384 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1386 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1387 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1389 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1392 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
&& !link
->network
->dhcp_anonymize
) {
1393 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
);
1395 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1398 if (link
->network
->dhcp_use_ntp
) {
1399 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1401 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1404 if (link
->network
->dhcp_use_sip
) {
1405 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_SIP_SERVER
);
1407 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1410 if (link
->network
->dhcp_use_timezone
) {
1411 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1413 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1416 SET_FOREACH(request_options
, link
->network
->dhcp_request_options
, i
) {
1417 uint32_t option
= PTR_TO_UINT32(request_options
);
1419 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, option
);
1421 log_link_debug(link
, "DHCP4 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option
);
1426 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option
);
1429 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_options
, i
) {
1430 r
= sd_dhcp_client_set_dhcp_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_user_class
) {
1447 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, (const char **) link
->network
->dhcp_user_class
);
1449 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1452 if (link
->network
->dhcp_client_port
) {
1453 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1455 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen port: %m");
1458 if (link
->network
->dhcp_max_attempts
> 0) {
1459 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1461 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1464 if (link
->network
->ip_service_type
> 0) {
1465 r
= sd_dhcp_client_set_service_type(link
->dhcp_client
, link
->network
->ip_service_type
);
1467 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ip service type: %m");
1470 if (link
->network
->dhcp_send_decline
) {
1471 r
= configure_dhcpv4_duplicate_address_detection(link
);
1473 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to configure service type: %m");
1476 return dhcp4_set_client_identifier(link
);
1479 int config_parse_dhcp_max_attempts(
1481 const char *filename
,
1483 const char *section
,
1484 unsigned section_line
,
1491 Network
*network
= data
;
1499 if (isempty(rvalue
)) {
1500 network
->dhcp_max_attempts
= 0;
1504 if (streq(rvalue
, "infinity")) {
1505 network
->dhcp_max_attempts
= (uint64_t) -1;
1509 r
= safe_atou64(rvalue
, &a
);
1511 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1512 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1517 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1518 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1522 network
->dhcp_max_attempts
= a
;
1527 int config_parse_dhcp_black_listed_ip_address(
1529 const char *filename
,
1531 const char *section
,
1532 unsigned section_line
,
1539 Network
*network
= data
;
1548 if (isempty(rvalue
)) {
1549 network
->dhcp_black_listed_ip
= set_free(network
->dhcp_black_listed_ip
);
1553 for (p
= rvalue
;;) {
1554 _cleanup_free_
char *n
= NULL
;
1555 union in_addr_union ip
;
1557 r
= extract_first_word(&p
, &n
, NULL
, 0);
1559 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1560 "Failed to parse DHCP black listed ip address, ignoring assignment: %s",
1567 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1569 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1570 "DHCP black listed ip address is invalid, ignoring assignment: %s", n
);
1574 r
= set_ensure_allocated(&network
->dhcp_black_listed_ip
, NULL
);
1578 r
= set_put(network
->dhcp_black_listed_ip
, UINT32_TO_PTR(ip
.in
.s_addr
));
1580 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1581 "Failed to store DHCP black listed ip address '%s', ignoring assignment: %m", n
);
1587 int config_parse_dhcp_user_class(
1589 const char *filename
,
1591 const char *section
,
1592 unsigned section_line
,
1606 if (isempty(rvalue
)) {
1612 _cleanup_free_
char *w
= NULL
;
1614 r
= extract_first_word(&rvalue
, &w
, NULL
, 0);
1618 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1619 "Failed to split user classes option, ignoring: %s", rvalue
);
1625 if (strlen(w
) > 255) {
1626 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1627 "%s length is not in the range 1-255, ignoring.", w
);
1631 r
= strv_push(l
, w
);
1641 int config_parse_dhcp_request_options(
1643 const char *filename
,
1645 const char *section
,
1646 unsigned section_line
,
1653 Network
*network
= data
;
1662 if (isempty(rvalue
)) {
1663 network
->dhcp_request_options
= set_free(network
->dhcp_request_options
);
1667 for (p
= rvalue
;;) {
1668 _cleanup_free_
char *n
= NULL
;
1671 r
= extract_first_word(&p
, &n
, NULL
, 0);
1673 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1674 "Failed to parse DHCP request option, ignoring assignment: %s",
1681 r
= safe_atou32(n
, &i
);
1683 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1684 "DHCP request option is invalid, ignoring assignment: %s", n
);
1688 if (i
< 1 || i
>= 255) {
1689 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1690 "DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n
);
1694 r
= set_ensure_allocated(&network
->dhcp_request_options
, NULL
);
1698 r
= set_put(network
->dhcp_request_options
, UINT32_TO_PTR(i
));
1700 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1701 "Failed to store DHCP request option '%s', ignoring assignment: %m", n
);
1707 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1708 [DHCP_CLIENT_ID_MAC
] = "mac",
1709 [DHCP_CLIENT_ID_DUID
] = "duid",
1710 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1713 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1714 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1715 "Failed to parse client identifier type");