1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
6 #include "alloc-util.h"
7 #include "hostname-util.h"
8 #include "parse-util.h"
9 #include "network-internal.h"
10 #include "networkd-dhcp4.h"
11 #include "networkd-link.h"
12 #include "networkd-manager.h"
13 #include "networkd-network.h"
14 #include "string-table.h"
15 #include "string-util.h"
16 #include "sysctl-util.h"
18 static int dhcp_remove_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
19 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
20 static int dhcp_remove_dns_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
21 static int dhcp_remove_address(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, link_netlink_message_handler_t callback
);
22 static int dhcp_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
);
23 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
);
25 void dhcp4_release_old_lease(Link
*link
) {
26 struct in_addr address
= {}, address_old
= {};
30 if (!link
->dhcp_lease_old
)
33 assert(link
->dhcp_lease
);
35 (void) sd_dhcp_lease_get_address(link
->dhcp_lease_old
, &address_old
);
36 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
38 (void) dhcp_remove_routes(link
, link
->dhcp_lease_old
, &address_old
, false);
39 (void) dhcp_remove_router(link
, link
->dhcp_lease_old
, &address_old
, false);
40 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease_old
, &address_old
, false);
42 if (!in4_addr_equal(&address_old
, &address
))
43 (void) dhcp_remove_address(link
, link
->dhcp_lease_old
, &address_old
, NULL
);
45 link
->dhcp_lease_old
= sd_dhcp_lease_unref(link
->dhcp_lease_old
);
49 static int dhcp4_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
53 assert(link
->dhcp4_messages
> 0);
55 link
->dhcp4_messages
--;
57 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
60 r
= sd_netlink_message_get_errno(m
);
61 if (r
== -ENETUNREACH
&& !link
->dhcp4_route_retrying
) {
63 /* It seems kernel does not support that the prefix route cannot be configured with
64 * route table. Let's once drop the config and reconfigure them later. */
66 log_link_debug_errno(link
, r
, "Could not set DHCPv4 route, retrying later: %m");
67 link
->dhcp4_route_failed
= true;
68 link
->manager
->dhcp4_prefix_root_cannot_set_table
= true;
69 } else if (r
< 0 && r
!= -EEXIST
) {
70 log_link_error_errno(link
, r
, "Could not set DHCPv4 route: %m");
71 link_enter_failed(link
);
75 if (link
->dhcp4_messages
== 0) {
76 if (link
->dhcp4_route_failed
) {
77 struct in_addr address
= {};
79 link
->dhcp4_route_failed
= false;
80 link
->dhcp4_route_retrying
= true;
82 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
83 (void) dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true);
84 (void) dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true);
85 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true);
86 (void) dhcp_remove_address(link
, link
->dhcp_lease
, &address
, dhcp_remove_address_handler
);
90 link
->dhcp4_configured
= true;
91 /* New address and routes are configured now. Let's release old lease. */
92 dhcp4_release_old_lease(link
);
93 link_check_ready(link
);
99 static int route_scope_from_address(const Route
*route
, const struct in_addr
*self_addr
) {
103 if (in4_addr_is_localhost(&route
->dst
.in
) ||
104 (!in4_addr_is_null(self_addr
) && in4_addr_equal(&route
->dst
.in
, self_addr
)))
105 return RT_SCOPE_HOST
;
106 else if (in4_addr_is_null(&route
->gw
.in
))
107 return RT_SCOPE_LINK
;
109 return RT_SCOPE_UNIVERSE
;
112 static bool link_noprefixroute(Link
*link
) {
113 return link
->network
->dhcp_route_table_set
&&
114 link
->network
->dhcp_route_table
!= RT_TABLE_MAIN
&&
115 !link
->manager
->dhcp4_prefix_root_cannot_set_table
;
118 static int dhcp_route_configure(Route
**route
, Link
*link
) {
125 if (set_contains(link
->dhcp_routes
, *route
))
128 r
= route_configure(*route
, link
, dhcp4_route_handler
);
132 link
->dhcp4_messages
++;
134 r
= set_put(link
->dhcp_routes
, *route
);
142 static int link_set_dns_routes(Link
*link
, const struct in_addr
*address
) {
143 const struct in_addr
*dns
;
148 assert(link
->dhcp_lease
);
149 assert(link
->network
);
151 if (!link
->network
->dhcp_use_dns
||
152 !link
->network
->dhcp_routes_to_dns
)
155 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &dns
);
156 if (IN_SET(n
, 0, -ENODATA
))
159 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
161 table
= link_get_dhcp_route_table(link
);
163 for (i
= 0; i
< n
; i
++) {
164 _cleanup_(route_freep
) Route
*route
= NULL
;
166 r
= route_new(&route
);
168 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
170 /* Set routes to DNS servers. */
172 route
->family
= AF_INET
;
173 route
->dst
.in
= dns
[i
];
174 route
->dst_prefixlen
= 32;
175 route
->prefsrc
.in
= *address
;
176 route
->scope
= RT_SCOPE_LINK
;
177 route
->protocol
= RTPROT_DHCP
;
178 route
->priority
= link
->network
->dhcp_route_metric
;
179 route
->table
= table
;
181 r
= dhcp_route_configure(&route
, link
);
183 return log_link_error_errno(link
, r
, "Could not set route to DNS server: %m");
189 static int link_set_dhcp_routes(Link
*link
) {
190 _cleanup_free_ sd_dhcp_route
**static_routes
= NULL
;
191 bool classless_route
= false, static_route
= false;
192 const struct in_addr
*router
;
193 struct in_addr address
;
199 if (!link
->dhcp_lease
) /* link went down while we configured the IP addresses? */
202 if (!link
->network
) /* link went down while we configured the IP addresses? */
205 if (!link
->network
->dhcp_use_routes
)
208 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
209 /* During configuring addresses, the link lost its carrier. As networkd is dropping
210 * the addresses now, let's not configure the routes either. */
213 r
= set_ensure_allocated(&link
->dhcp_routes
, &route_full_hash_ops
);
217 /* Clear old entries in case the set was already allocated */
218 set_clear(link
->dhcp_routes
);
220 table
= link_get_dhcp_route_table(link
);
222 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
224 return log_link_warning_errno(link
, r
, "DHCP error: could not get address: %m");
226 if (link_noprefixroute(link
)) {
227 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
228 struct in_addr netmask
;
230 r
= sd_dhcp_lease_get_netmask(link
->dhcp_lease
, &netmask
);
232 return log_link_error_errno(link
, r
, "DHCP error: No netmask: %m");
234 r
= route_new(&prefix_route
);
236 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
238 prefix_route
->family
= AF_INET
;
239 prefix_route
->dst
.in
.s_addr
= address
.s_addr
& netmask
.s_addr
;
240 prefix_route
->dst_prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
241 prefix_route
->prefsrc
.in
= address
;
242 prefix_route
->scope
= RT_SCOPE_LINK
;
243 prefix_route
->protocol
= RTPROT_DHCP
;
244 prefix_route
->table
= table
;
246 r
= dhcp_route_configure(&prefix_route
, link
);
248 return log_link_error_errno(link
, r
, "Could not set prefix route: %m");
251 n
= sd_dhcp_lease_get_routes(link
->dhcp_lease
, &static_routes
);
253 log_link_debug_errno(link
, n
, "DHCP: No routes received from DHCP server: %m");
255 log_link_debug_errno(link
, n
, "DHCP error: could not get routes: %m");
257 for (i
= 0; i
< n
; i
++) {
258 switch (sd_dhcp_route_get_option(static_routes
[i
])) {
259 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
260 classless_route
= true;
262 case SD_DHCP_OPTION_STATIC_ROUTE
:
268 for (i
= 0; i
< n
; i
++) {
269 _cleanup_(route_freep
) Route
*route
= NULL
;
271 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
272 the DHCP client MUST ignore the Static Routes option. */
273 if (classless_route
&&
274 sd_dhcp_route_get_option(static_routes
[i
]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
)
277 r
= route_new(&route
);
279 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
281 route
->family
= AF_INET
;
282 route
->protocol
= RTPROT_DHCP
;
283 assert_se(sd_dhcp_route_get_gateway(static_routes
[i
], &route
->gw
.in
) >= 0);
284 assert_se(sd_dhcp_route_get_destination(static_routes
[i
], &route
->dst
.in
) >= 0);
285 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &route
->dst_prefixlen
) >= 0);
286 route
->priority
= link
->network
->dhcp_route_metric
;
287 route
->table
= table
;
288 route
->scope
= route_scope_from_address(route
, &address
);
289 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
290 route
->prefsrc
.in
= address
;
292 if (set_contains(link
->dhcp_routes
, route
))
295 r
= dhcp_route_configure(&route
, link
);
297 return log_link_error_errno(link
, r
, "Could not set route: %m");
300 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
301 if (IN_SET(r
, 0, -ENODATA
))
302 log_link_info(link
, "DHCP: No gateway received from DHCP server.");
304 log_link_warning_errno(link
, r
, "DHCP error: could not get gateway: %m");
305 else if (in4_addr_is_null(&router
[0]))
306 log_link_info(link
, "DHCP: Received gateway is null.");
308 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
309 a Router option, the DHCP client MUST ignore the Router option. */
310 if (classless_route
&& static_route
)
311 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring static-route option and router option");
313 if (r
> 0 && !classless_route
&& !in4_addr_is_null(&router
[0])) {
314 _cleanup_(route_freep
) Route
*route
= NULL
, *route_gw
= NULL
;
316 r
= route_new(&route_gw
);
318 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
320 /* The dhcp netmask may mask out the gateway. Add an explicit
321 * route for the gw host so that we can route no matter the
322 * netmask or existing kernel route tables. */
323 route_gw
->family
= AF_INET
;
324 route_gw
->dst
.in
= router
[0];
325 route_gw
->dst_prefixlen
= 32;
326 route_gw
->prefsrc
.in
= address
;
327 route_gw
->scope
= RT_SCOPE_LINK
;
328 route_gw
->protocol
= RTPROT_DHCP
;
329 route_gw
->priority
= link
->network
->dhcp_route_metric
;
330 route_gw
->table
= table
;
332 r
= dhcp_route_configure(&route_gw
, link
);
334 return log_link_error_errno(link
, r
, "Could not set host route: %m");
336 r
= route_new(&route
);
338 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
340 route
->family
= AF_INET
;
341 route
->gw
.in
= router
[0];
342 route
->prefsrc
.in
= address
;
343 route
->protocol
= RTPROT_DHCP
;
344 route
->priority
= link
->network
->dhcp_route_metric
;
345 route
->table
= table
;
347 r
= dhcp_route_configure(&route
, link
);
349 return log_link_error_errno(link
, r
, "Could not set router: %m");
352 return link_set_dns_routes(link
, &address
);
355 static int dhcp_remove_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
356 _cleanup_free_ sd_dhcp_route
**routes
= NULL
;
363 if (!link
->network
->dhcp_use_routes
)
366 n
= sd_dhcp_lease_get_routes(lease
, &routes
);
367 if (IN_SET(n
, 0, -ENODATA
))
370 return log_link_error_errno(link
, n
, "DHCP error: Failed to get routes: %m");
372 table
= link_get_dhcp_route_table(link
);
374 for (i
= 0; i
< n
; i
++) {
375 _cleanup_(route_freep
) Route
*route
= NULL
;
377 r
= route_new(&route
);
381 route
->family
= AF_INET
;
382 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &route
->gw
.in
) >= 0);
383 assert_se(sd_dhcp_route_get_destination(routes
[i
], &route
->dst
.in
) >= 0);
384 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &route
->dst_prefixlen
) >= 0);
385 route
->priority
= link
->network
->dhcp_route_metric
;
386 route
->table
= table
;
387 route
->scope
= route_scope_from_address(route
, address
);
388 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
389 route
->prefsrc
.in
= *address
;
391 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
394 (void) route_remove(route
, link
, NULL
);
400 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
401 _cleanup_(route_freep
) Route
*route_gw
= NULL
, *route
= NULL
;
402 const struct in_addr
*router
;
409 if (!link
->network
->dhcp_use_routes
)
412 r
= sd_dhcp_lease_get_router(lease
, &router
);
413 if (IN_SET(r
, 0, -ENODATA
)) {
414 log_link_debug(link
, "DHCP: No gateway received from DHCP server.");
417 return log_link_error_errno(link
, r
, "DHCP error: could not get gateway: %m");
418 else if (in4_addr_is_null(&router
[0])) {
419 log_link_info(link
, "DHCP: Received gateway is null, ignoring.");
423 table
= link_get_dhcp_route_table(link
);
425 r
= route_new(&route_gw
);
429 route_gw
->family
= AF_INET
;
430 route_gw
->dst
.in
= router
[0];
431 route_gw
->dst_prefixlen
= 32;
432 route_gw
->prefsrc
.in
= *address
;
433 route_gw
->scope
= RT_SCOPE_LINK
;
434 route_gw
->protocol
= RTPROT_DHCP
;
435 route_gw
->priority
= link
->network
->dhcp_route_metric
;
436 route_gw
->table
= table
;
438 if (remove_all
|| !set_contains(link
->dhcp_routes
, route_gw
))
439 (void) route_remove(route_gw
, link
, NULL
);
441 r
= route_new(&route
);
445 route
->family
= AF_INET
;
446 route
->gw
.in
= router
[0];
447 route
->prefsrc
.in
= *address
;
448 route
->protocol
= RTPROT_DHCP
;
449 route
->priority
= link
->network
->dhcp_route_metric
;
450 route
->table
= table
;
452 if (remove_all
|| !set_contains(link
->dhcp_routes
, route
))
453 (void) route_remove(route
, link
, NULL
);
458 static int dhcp_remove_dns_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
459 const struct in_addr
*dns
;
465 assert(link
->network
);
467 if (!link
->network
->dhcp_use_dns
||
468 !link
->network
->dhcp_routes_to_dns
)
471 n
= sd_dhcp_lease_get_dns(lease
, &dns
);
472 if (IN_SET(n
, 0, -ENODATA
))
475 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
477 table
= link_get_dhcp_route_table(link
);
479 for (i
= 0; i
< n
; i
++) {
480 _cleanup_(route_freep
) Route
*route
= NULL
;
482 r
= route_new(&route
);
484 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
486 route
->family
= AF_INET
;
487 route
->dst
.in
= dns
[i
];
488 route
->dst_prefixlen
= 32;
489 route
->prefsrc
.in
= *address
;
490 route
->scope
= RT_SCOPE_LINK
;
491 route
->protocol
= RTPROT_DHCP
;
492 route
->priority
= link
->network
->dhcp_route_metric
;
493 route
->table
= table
;
495 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
498 (void) route_remove(route
, link
, NULL
);
501 if (link_noprefixroute(link
)) {
502 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
503 struct in_addr netmask
;
505 r
= route_new(&prefix_route
);
507 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
509 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
511 return log_link_error_errno(link
, r
, "DHCP error: No netmask: %m");
513 prefix_route
->family
= AF_INET
;
514 prefix_route
->dst
.in
.s_addr
= address
->s_addr
& netmask
.s_addr
;
515 prefix_route
->dst_prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
516 prefix_route
->prefsrc
.in
= *address
;
517 prefix_route
->scope
= RT_SCOPE_LINK
;
518 prefix_route
->protocol
= RTPROT_DHCP
;
519 prefix_route
->table
= table
;
521 if (remove_all
|| !set_contains(link
->dhcp_routes
, prefix_route
))
522 (void) route_remove(prefix_route
, link
, NULL
);
528 static int dhcp_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
533 /* This is only used when retrying to assign the address received from DHCPv4 server.
534 * See dhcp4_route_handler(). */
536 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
539 r
= sd_netlink_message_get_errno(m
);
541 log_link_debug_errno(link
, r
, "Failed to remove DHCPv4 address, ignoring: %m");
543 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
545 (void) dhcp_lease_renew(link
->dhcp_client
, link
);
549 static int dhcp_remove_address(
550 Link
*link
, sd_dhcp_lease
*lease
,
551 const struct in_addr
*address
,
552 link_netlink_message_handler_t callback
) {
554 _cleanup_(address_freep
) Address
*a
= NULL
;
555 struct in_addr netmask
;
561 if (in4_addr_is_null(address
))
569 a
->in_addr
.in
= *address
;
571 if (sd_dhcp_lease_get_netmask(lease
, &netmask
) >= 0)
572 a
->prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
574 (void) address_remove(a
, link
, callback
);
579 static int dhcp_reset_mtu(Link
*link
) {
585 if (!link
->network
->dhcp_use_mtu
)
588 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
592 if (link
->original_mtu
== mtu
)
595 r
= link_set_mtu(link
, link
->original_mtu
);
597 log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
598 link_enter_failed(link
);
605 static int dhcp_reset_hostname(Link
*link
) {
606 const char *hostname
;
611 if (!link
->network
->dhcp_use_hostname
)
614 hostname
= link
->network
->dhcp_hostname
;
616 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
621 /* If a hostname was set due to the lease, then unset it now. */
622 r
= manager_set_hostname(link
->manager
, NULL
);
624 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
629 static int dhcp_lease_lost(Link
*link
) {
630 struct in_addr address
= {};
633 assert(link
->dhcp_lease
);
635 log_link_warning(link
, "DHCP lease lost");
637 link
->dhcp4_configured
= false;
639 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
640 (void) dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true);
641 (void) dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true);
642 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true);
643 (void) dhcp_remove_address(link
, link
->dhcp_lease
, &address
, NULL
);
644 (void) dhcp_reset_mtu(link
);
645 (void) dhcp_reset_hostname(link
);
647 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
653 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
658 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
661 r
= sd_netlink_message_get_errno(m
);
662 if (r
< 0 && r
!= -EEXIST
) {
663 log_link_error_errno(link
, r
, "Could not set DHCPv4 address: %m");
664 link_enter_failed(link
);
667 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
669 r
= link_set_dhcp_routes(link
);
671 link_enter_failed(link
);
675 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
676 r
= link_request_set_routes(link
);
678 link_enter_failed(link
);
682 if (link
->dhcp4_messages
== 0) {
683 link
->dhcp4_configured
= true;
684 /* The new address is configured, and no route is requested.
685 * Let's drop the old lease. */
686 dhcp4_release_old_lease(link
);
687 link_check_ready(link
);
693 static int dhcp4_update_address(Link
*link
,
694 struct in_addr
*address
,
695 struct in_addr
*netmask
,
697 _cleanup_(address_freep
) Address
*addr
= NULL
;
705 prefixlen
= in4_addr_netmask_to_prefixlen(netmask
);
707 r
= address_new(&addr
);
711 addr
->family
= AF_INET
;
712 addr
->in_addr
.in
.s_addr
= address
->s_addr
;
713 addr
->cinfo
.ifa_prefered
= lifetime
;
714 addr
->cinfo
.ifa_valid
= lifetime
;
715 addr
->prefixlen
= prefixlen
;
716 addr
->broadcast
.s_addr
= address
->s_addr
| ~netmask
->s_addr
;
717 addr
->prefix_route
= link_noprefixroute(link
);
719 /* allow reusing an existing address and simply update its lifetime
720 * in case it already exists */
721 r
= address_configure(addr
, link
, dhcp4_address_handler
, true);
728 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
729 sd_dhcp_lease
*lease
;
730 struct in_addr address
;
731 struct in_addr netmask
;
732 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
737 assert(link
->network
);
739 r
= sd_dhcp_client_get_lease(client
, &lease
);
741 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
743 sd_dhcp_lease_unref(link
->dhcp_lease
);
744 link
->dhcp4_configured
= false;
745 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
748 r
= sd_dhcp_lease_get_address(lease
, &address
);
750 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
752 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
754 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
756 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
757 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
759 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
762 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
764 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
769 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
770 const struct in_addr
*router
;
771 sd_dhcp_lease
*lease
;
772 struct in_addr address
;
773 struct in_addr netmask
;
775 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
781 link
->dhcp4_configured
= false;
783 r
= sd_dhcp_client_get_lease(client
, &lease
);
785 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
787 r
= sd_dhcp_lease_get_address(lease
, &address
);
789 return log_link_error_errno(link
, r
, "DHCP error: No address: %m");
791 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
793 return log_link_error_errno(link
, r
, "DHCP error: No netmask: %m");
795 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
797 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
798 r
= sd_dhcp_lease_get_lifetime(lease
, &lifetime
);
800 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
803 r
= sd_dhcp_lease_get_router(lease
, &router
);
804 if (r
< 0 && r
!= -ENODATA
)
805 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
807 if (r
> 0 && !in4_addr_is_null(&router
[0]))
809 LOG_LINK_INTERFACE(link
),
810 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
811 ADDRESS_FMT_VAL(address
),
813 ADDRESS_FMT_VAL(router
[0])),
814 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
815 "PREFIXLEN=%u", prefixlen
,
816 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router
[0]));
819 LOG_LINK_INTERFACE(link
),
820 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u",
821 ADDRESS_FMT_VAL(address
),
823 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
824 "PREFIXLEN=%u", prefixlen
);
826 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
829 if (link
->network
->dhcp_use_mtu
) {
832 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
834 r
= link_set_mtu(link
, mtu
);
836 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
840 if (link
->network
->dhcp_use_hostname
) {
841 const char *dhcpname
= NULL
;
842 _cleanup_free_
char *hostname
= NULL
;
844 if (link
->network
->dhcp_hostname
)
845 dhcpname
= link
->network
->dhcp_hostname
;
847 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
850 r
= shorten_overlong(dhcpname
, &hostname
);
852 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
854 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
858 r
= manager_set_hostname(link
->manager
, hostname
);
860 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
864 if (link
->network
->dhcp_use_timezone
) {
865 const char *tz
= NULL
;
867 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
870 r
= manager_set_timezone(link
->manager
, tz
);
872 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
876 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
878 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
883 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
886 link
->dhcp_lease_old
= TAKE_PTR(link
->dhcp_lease
);
888 /* On ip address change, to keep the connectability, we would like to assign new address and
889 * routes, and then release old lease. There are two possible success paths:
891 * 1. new address and routes are configured.
892 * -> handled by dhcp_release_old_lease() in dhcp4_route_handler().
893 * 2. new address is configured and no route is requested.
894 * -> handled by dhcp_release_old_lease() in dhcp4_address_handler().
896 * On error in assigning new address and routes, then the link always enters to the failed
897 * state. And link_enter_failed() leads to the DHCP client to be stopped. So,
898 * dhcp_release_old_lease() will be also called by link_stop_clients().
901 r
= dhcp_lease_acquired(client
, link
);
903 /* If it fails, then the new address is not configured yet.
904 * So, let's simply drop the old lease. */
905 sd_dhcp_lease_unref(link
->dhcp_lease
);
906 link
->dhcp_lease
= TAKE_PTR(link
->dhcp_lease_old
);
907 (void) dhcp_lease_lost(link
);
914 static int dhcp_server_is_black_listed(Link
*link
, sd_dhcp_client
*client
) {
915 sd_dhcp_lease
*lease
;
920 assert(link
->network
);
923 r
= sd_dhcp_client_get_lease(client
, &lease
);
925 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
927 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
929 return log_link_debug_errno(link
, r
, "Failed to get DHCP server ip address: %m");
931 if (set_contains(link
->network
->dhcp_black_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
932 log_struct(LOG_DEBUG
,
933 LOG_LINK_INTERFACE(link
),
934 LOG_LINK_MESSAGE(link
, "DHCPv4 ip '%u.%u.%u.%u' found in black listed ip addresses, ignoring offer",
935 ADDRESS_FMT_VAL(addr
)));
942 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
943 Link
*link
= userdata
;
947 assert(link
->network
);
948 assert(link
->manager
);
950 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
954 case SD_DHCP_CLIENT_EVENT_STOP
:
956 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
)) {
957 assert(link
->ipv4ll
);
959 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
961 r
= sd_ipv4ll_start(link
->ipv4ll
);
963 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
966 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
967 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
971 if (link
->network
->dhcp_send_release
)
972 (void) sd_dhcp_client_send_release(client
);
974 if (link
->dhcp_lease
) {
975 r
= dhcp_lease_lost(link
);
977 link_enter_failed(link
);
983 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
984 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
985 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
989 if (link
->dhcp_lease
) {
990 r
= dhcp_lease_lost(link
);
992 link_enter_failed(link
);
998 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
999 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1000 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1004 r
= dhcp_lease_ip_change(client
, link
);
1006 link_enter_failed(link
);
1011 case SD_DHCP_CLIENT_EVENT_RENEW
:
1012 r
= dhcp_lease_renew(client
, link
);
1014 link_enter_failed(link
);
1018 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1019 r
= dhcp_lease_acquired(client
, link
);
1021 link_enter_failed(link
);
1025 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1026 r
= dhcp_server_is_black_listed(link
, client
);
1034 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1036 log_link_warning(link
, "DHCP unknown event: %i", event
);
1043 static int dhcp4_set_hostname(Link
*link
) {
1044 _cleanup_free_
char *hostname
= NULL
;
1050 if (!link
->network
->dhcp_send_hostname
)
1052 else if (link
->network
->dhcp_hostname
)
1053 hn
= link
->network
->dhcp_hostname
;
1055 r
= gethostname_strict(&hostname
);
1056 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1062 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1063 if (r
== -EINVAL
&& hostname
)
1064 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1065 log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1067 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
1072 static bool promote_secondaries_enabled(const char *ifname
) {
1073 _cleanup_free_
char *promote_secondaries_sysctl
= NULL
;
1074 char *promote_secondaries_path
;
1077 promote_secondaries_path
= strjoina("net/ipv4/conf/", ifname
, "/promote_secondaries");
1078 r
= sysctl_read(promote_secondaries_path
, &promote_secondaries_sysctl
);
1080 log_debug_errno(r
, "Cannot read sysctl %s", promote_secondaries_path
);
1084 truncate_nl(promote_secondaries_sysctl
);
1085 r
= parse_boolean(promote_secondaries_sysctl
);
1087 log_warning_errno(r
, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path
, promote_secondaries_sysctl
);
1091 /* dhcp4_set_promote_secondaries will ensure this interface has
1092 * the "promote_secondaries" option in the kernel set. If this sysctl
1093 * is not set DHCP will work only as long as the IP address does not
1094 * changes between leases. The kernel will remove all secondary IP
1095 * addresses of an interface otherwise. The way systemd-network works
1096 * is that the new IP of a lease is added as a secondary IP and when
1097 * the primary one expires it relies on the kernel to promote the
1098 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1100 int dhcp4_set_promote_secondaries(Link
*link
) {
1104 assert(link
->network
);
1105 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1107 /* check if the kernel has promote_secondaries enabled for our
1108 * interface. If it is not globally enabled or enabled for the
1109 * specific interface we must either enable it.
1111 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link
->ifname
))) {
1112 char *promote_secondaries_path
= NULL
;
1114 log_link_debug(link
, "promote_secondaries is unset, setting it");
1115 promote_secondaries_path
= strjoina("net/ipv4/conf/", link
->ifname
, "/promote_secondaries");
1116 r
= sysctl_write(promote_secondaries_path
, "1");
1118 log_link_warning_errno(link
, r
, "cannot set sysctl %s to 1", promote_secondaries_path
);
1125 int dhcp4_set_client_identifier(Link
*link
) {
1129 assert(link
->network
);
1130 assert(link
->dhcp_client
);
1132 switch (link
->network
->dhcp_client_identifier
) {
1133 case DHCP_CLIENT_ID_DUID
: {
1134 /* If configured, apply user specified DUID and IAID */
1135 const DUID
*duid
= link_get_duid(link
);
1137 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1138 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1139 link
->network
->iaid_set
,
1140 link
->network
->iaid
,
1143 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
1144 link
->network
->iaid_set
,
1145 link
->network
->iaid
,
1147 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1148 duid
->raw_data_len
);
1150 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1153 case DHCP_CLIENT_ID_DUID_ONLY
: {
1154 /* If configured, apply user specified DUID */
1155 const DUID
*duid
= link_get_duid(link
);
1157 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1158 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
1161 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
1163 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1164 duid
->raw_data_len
);
1166 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
1169 case DHCP_CLIENT_ID_MAC
:
1170 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1172 (const uint8_t *) &link
->mac
,
1175 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
1178 assert_not_reached("Unknown client identifier type.");
1184 int dhcp4_configure(Link
*link
) {
1188 assert(link
->network
);
1189 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1191 if (!link
->dhcp_client
) {
1192 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1196 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
1199 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
1201 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to attach event: %m");
1203 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1204 (const uint8_t *) &link
->mac
,
1205 sizeof (link
->mac
), ARPHRD_ETHER
);
1207 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1209 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1211 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1213 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1215 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1217 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
,
1218 link
->network
->dhcp_broadcast
);
1220 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1223 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1225 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1228 if (link
->network
->dhcp_use_mtu
) {
1229 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1230 SD_DHCP_OPTION_INTERFACE_MTU
);
1232 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1235 /* NOTE: even if this variable is called "use", it also "sends" PRL
1236 * options, maybe there should be a different configuration variable
1237 * to send or not route options?. */
1238 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1239 * by default, so they don't need to be added here. */
1240 if (link
->network
->dhcp_use_routes
&& !link
->network
->dhcp_anonymize
) {
1241 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1242 SD_DHCP_OPTION_STATIC_ROUTE
);
1244 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1246 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1247 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1249 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1252 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
&& !link
->network
->dhcp_anonymize
) {
1253 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
);
1255 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1258 if (link
->network
->dhcp_use_ntp
) {
1259 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1261 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1264 if (link
->network
->dhcp_use_timezone
) {
1265 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1267 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1270 r
= dhcp4_set_hostname(link
);
1274 if (link
->network
->dhcp_vendor_class_identifier
) {
1275 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1276 link
->network
->dhcp_vendor_class_identifier
);
1278 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1281 if (link
->network
->dhcp_user_class
) {
1282 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, (const char **) link
->network
->dhcp_user_class
);
1284 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1287 if (link
->network
->dhcp_client_port
) {
1288 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1290 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen port: %m");
1293 if (link
->network
->dhcp_max_attempts
> 0) {
1294 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1296 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1299 return dhcp4_set_client_identifier(link
);
1302 int config_parse_dhcp_max_attempts(
1304 const char *filename
,
1306 const char *section
,
1307 unsigned section_line
,
1314 Network
*network
= data
;
1322 if (isempty(rvalue
)) {
1323 network
->dhcp_max_attempts
= 0;
1327 if (streq(rvalue
, "infinity")) {
1328 network
->dhcp_max_attempts
= (uint64_t) -1;
1332 r
= safe_atou64(rvalue
, &a
);
1334 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1335 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1340 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1341 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1345 network
->dhcp_max_attempts
= a
;
1350 int config_parse_dhcp_black_listed_ip_address(
1352 const char *filename
,
1354 const char *section
,
1355 unsigned section_line
,
1362 Network
*network
= data
;
1371 if (isempty(rvalue
)) {
1372 network
->dhcp_black_listed_ip
= set_free(network
->dhcp_black_listed_ip
);
1376 for (p
= rvalue
;;) {
1377 _cleanup_free_
char *n
= NULL
;
1378 union in_addr_union ip
;
1380 r
= extract_first_word(&p
, &n
, NULL
, 0);
1382 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1383 "Failed to parse DHCP black listed ip address, ignoring assignment: %s",
1390 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1392 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1393 "DHCP black listed ip address is invalid, ignoring assignment: %s", n
);
1397 r
= set_ensure_allocated(&network
->dhcp_black_listed_ip
, NULL
);
1401 r
= set_put(network
->dhcp_black_listed_ip
, UINT32_TO_PTR(ip
.in
.s_addr
));
1403 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1404 "Failed to store DHCP black listed ip address '%s', ignoring assignment: %m", n
);
1410 int config_parse_dhcp_user_class(
1412 const char *filename
,
1414 const char *section
,
1415 unsigned section_line
,
1429 if (isempty(rvalue
)) {
1435 _cleanup_free_
char *w
= NULL
;
1437 r
= extract_first_word(&rvalue
, &w
, NULL
, 0);
1441 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1442 "Failed to split user classes option, ignoring: %s", rvalue
);
1448 if (strlen(w
) > 255) {
1449 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1450 "%s length is not in the range 1-255, ignoring.", w
);
1454 r
= strv_push(l
, w
);
1464 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1465 [DHCP_CLIENT_ID_MAC
] = "mac",
1466 [DHCP_CLIENT_ID_DUID
] = "duid",
1467 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1470 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1471 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1472 "Failed to parse client identifier type");