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
, sd_dhcp_lease
*new_lease
, struct in_addr
*address
);
19 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, struct in_addr
*address
);
20 static int dhcp_remove_address(Link
*link
, sd_dhcp_lease
*lease
, struct in_addr
*address
);
22 void dhcp4_release_old_lease(Link
*link
) {
23 union in_addr_union address
= IN_ADDR_NULL
, address_old
= IN_ADDR_NULL
;
27 if (!link
->dhcp_lease_old
)
30 assert(link
->dhcp_lease
);
32 (void) sd_dhcp_lease_get_address(link
->dhcp_lease_old
, &address_old
.in
);
33 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
.in
);
35 (void) dhcp_remove_routes(link
, link
->dhcp_lease_old
, link
->dhcp_lease
, &address_old
.in
);
37 if (!in_addr_equal(AF_INET
, &address_old
, &address
)) {
38 (void) dhcp_remove_router(link
, link
->dhcp_lease_old
, &address_old
.in
);
39 (void) dhcp_remove_address(link
, link
->dhcp_lease_old
, &address_old
.in
);
42 link
->dhcp_lease_old
= sd_dhcp_lease_unref(link
->dhcp_lease_old
);
46 static int dhcp4_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
50 assert(link
->dhcp4_messages
> 0);
52 link
->dhcp4_messages
--;
54 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
57 r
= sd_netlink_message_get_errno(m
);
58 if (r
< 0 && r
!= -EEXIST
) {
59 log_link_error_errno(link
, r
, "Could not set DHCPv4 route: %m");
60 link_enter_failed(link
);
64 if (link
->dhcp4_messages
== 0) {
65 link
->dhcp4_configured
= true;
66 /* New address and routes are configured now. Let's release old lease. */
67 dhcp4_release_old_lease(link
);
68 link_check_ready(link
);
74 static int route_scope_from_address(const Route
*route
, const struct in_addr
*self_addr
) {
78 if (in4_addr_is_localhost(&route
->dst
.in
) ||
79 (!in4_addr_is_null(self_addr
) && in4_addr_equal(&route
->dst
.in
, self_addr
)))
81 else if (in4_addr_is_null(&route
->gw
.in
))
84 return RT_SCOPE_UNIVERSE
;
87 static int link_set_dhcp_routes(Link
*link
) {
88 _cleanup_free_ sd_dhcp_route
**static_routes
= NULL
;
89 bool classless_route
= false, static_route
= false;
90 const struct in_addr
*router
;
91 struct in_addr address
;
97 if (!link
->dhcp_lease
) /* link went down while we configured the IP addresses? */
100 if (!link
->network
) /* link went down while we configured the IP addresses? */
103 if (!link
->network
->dhcp_use_routes
)
106 table
= link_get_dhcp_route_table(link
);
108 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
110 return log_link_warning_errno(link
, r
, "DHCP error: could not get address: %m");
112 n
= sd_dhcp_lease_get_routes(link
->dhcp_lease
, &static_routes
);
114 log_link_debug_errno(link
, n
, "DHCP: No routes received from DHCP server: %m");
116 log_link_debug_errno(link
, n
, "DHCP error: could not get routes: %m");
118 for (i
= 0; i
< n
; i
++) {
119 switch (sd_dhcp_route_get_option(static_routes
[i
])) {
120 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
121 classless_route
= true;
123 case SD_DHCP_OPTION_STATIC_ROUTE
:
129 for (i
= 0; i
< n
; i
++) {
130 _cleanup_(route_freep
) Route
*route
= NULL
;
132 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
133 the DHCP client MUST ignore the Static Routes option. */
134 if (classless_route
&&
135 sd_dhcp_route_get_option(static_routes
[i
]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
)
138 r
= route_new(&route
);
140 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
142 route
->family
= AF_INET
;
143 route
->protocol
= RTPROT_DHCP
;
144 assert_se(sd_dhcp_route_get_gateway(static_routes
[i
], &route
->gw
.in
) >= 0);
145 assert_se(sd_dhcp_route_get_destination(static_routes
[i
], &route
->dst
.in
) >= 0);
146 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &route
->dst_prefixlen
) >= 0);
147 route
->priority
= link
->network
->dhcp_route_metric
;
148 route
->table
= table
;
149 route
->scope
= route_scope_from_address(route
, &address
);
150 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
151 route
->prefsrc
.in
= address
;
153 r
= route_configure(route
, link
, dhcp4_route_handler
);
155 return log_link_error_errno(link
, r
, "Could not set host route: %m");
157 link
->dhcp4_messages
++;
160 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
161 if (IN_SET(r
, 0, -ENODATA
))
162 log_link_info(link
, "DHCP: No gateway received from DHCP server.");
164 log_link_warning_errno(link
, r
, "DHCP error: could not get gateway: %m");
165 else if (in4_addr_is_null(&router
[0]))
166 log_link_info(link
, "DHCP: Received gateway is null.");
168 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
169 a Router option, the DHCP client MUST ignore the Router option. */
170 if (classless_route
&& static_route
)
171 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring static-route option and router option");
173 if (r
> 0 && !classless_route
&& !in4_addr_is_null(&router
[0])) {
174 _cleanup_(route_freep
) Route
*route
= NULL
, *route_gw
= NULL
;
176 r
= route_new(&route_gw
);
178 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
180 /* The dhcp netmask may mask out the gateway. Add an explicit
181 * route for the gw host so that we can route no matter the
182 * netmask or existing kernel route tables. */
183 route_gw
->family
= AF_INET
;
184 route_gw
->dst
.in
= router
[0];
185 route_gw
->dst_prefixlen
= 32;
186 route_gw
->prefsrc
.in
= address
;
187 route_gw
->scope
= RT_SCOPE_LINK
;
188 route_gw
->protocol
= RTPROT_DHCP
;
189 route_gw
->priority
= link
->network
->dhcp_route_metric
;
190 route_gw
->table
= table
;
192 r
= route_configure(route_gw
, link
, dhcp4_route_handler
);
194 return log_link_error_errno(link
, r
, "Could not set host route: %m");
196 link
->dhcp4_messages
++;
198 r
= route_new(&route
);
200 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
202 route
->family
= AF_INET
;
203 route
->gw
.in
= router
[0];
204 route
->prefsrc
.in
= address
;
205 route
->protocol
= RTPROT_DHCP
;
206 route
->priority
= link
->network
->dhcp_route_metric
;
207 route
->table
= table
;
209 r
= route_configure(route
, link
, dhcp4_route_handler
);
211 return log_link_error_errno(link
, r
, "Could not set routes: %m");
213 link
->dhcp4_messages
++;
219 static bool route_present_in_routes(const Route
*route
, sd_dhcp_route
**routes
, unsigned n_routes
) {
220 assert(n_routes
== 0 || routes
);
222 for (unsigned j
= 0; j
< n_routes
; j
++) {
223 union in_addr_union a
;
226 assert_se(sd_dhcp_route_get_gateway(routes
[j
], &a
.in
) >= 0);
227 if (!in_addr_equal(AF_INET
, &a
, &route
->gw
))
229 assert_se(sd_dhcp_route_get_destination(routes
[j
], &a
.in
) >= 0);
230 if (!in_addr_equal(AF_INET
, &a
, &route
->dst
))
232 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[j
], &l
) >= 0);
233 if (l
!= route
->dst_prefixlen
)
241 static int dhcp_remove_routes(Link
*link
, sd_dhcp_lease
*lease
, sd_dhcp_lease
*new_lease
, struct in_addr
*address
) {
242 _cleanup_free_ sd_dhcp_route
**routes
= NULL
, **new_routes
= NULL
;
249 if (!link
->network
->dhcp_use_routes
)
252 n
= sd_dhcp_lease_get_routes(lease
, &routes
);
253 if (IN_SET(n
, 0, -ENODATA
))
256 return log_link_error_errno(link
, n
, "DHCP error: Failed to get routes: %m");
259 m
= sd_dhcp_lease_get_routes(new_lease
, &new_routes
);
263 return log_link_error_errno(link
, m
, "DHCP error: Failed to get routes: %m");
266 table
= link_get_dhcp_route_table(link
);
268 for (i
= 0; i
< n
; i
++) {
269 _cleanup_(route_freep
) Route
*route
= NULL
;
271 r
= route_new(&route
);
275 route
->family
= AF_INET
;
276 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &route
->gw
.in
) >= 0);
277 assert_se(sd_dhcp_route_get_destination(routes
[i
], &route
->dst
.in
) >= 0);
278 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &route
->dst_prefixlen
) >= 0);
279 route
->priority
= link
->network
->dhcp_route_metric
;
280 route
->table
= table
;
281 route
->scope
= route_scope_from_address(route
, address
);
282 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
283 route
->prefsrc
.in
= *address
;
285 if (route_present_in_routes(route
, new_routes
, m
))
288 (void) route_remove(route
, link
, NULL
);
294 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, struct in_addr
*address
) {
295 _cleanup_(route_freep
) Route
*route_gw
= NULL
, *route
= NULL
;
296 const struct in_addr
*router
;
303 if (!link
->network
->dhcp_use_routes
)
306 r
= sd_dhcp_lease_get_router(lease
, &router
);
307 if (IN_SET(r
, 0, -ENODATA
)) {
308 log_link_debug(link
, "DHCP: No gateway received from DHCP server.");
311 return log_link_error_errno(link
, r
, "DHCP error: could not get gateway: %m");
312 else if (in4_addr_is_null(&router
[0])) {
313 log_link_info(link
, "DHCP: Received gateway is null, ignoring.");
317 table
= link_get_dhcp_route_table(link
);
319 r
= route_new(&route_gw
);
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 (void) route_remove(route_gw
, link
, NULL
);
334 r
= route_new(&route
);
338 route
->family
= AF_INET
;
339 route
->gw
.in
= router
[0];
340 route
->prefsrc
.in
= *address
;
341 route
->protocol
= RTPROT_DHCP
;
342 route
->priority
= link
->network
->dhcp_route_metric
;
343 route
->table
= table
;
345 (void) route_remove(route
, link
, NULL
);
350 static int dhcp_remove_address(Link
*link
, sd_dhcp_lease
*lease
, struct in_addr
*address
) {
351 _cleanup_(address_freep
) Address
*a
= NULL
;
352 struct in_addr netmask
;
358 if (in4_addr_is_null(address
))
366 a
->in_addr
.in
= *address
;
368 if (sd_dhcp_lease_get_netmask(lease
, &netmask
) >= 0)
369 a
->prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
371 (void) address_remove(a
, link
, NULL
);
376 static int dhcp_reset_mtu(Link
*link
) {
382 if (!link
->network
->dhcp_use_mtu
)
385 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
389 if (link
->original_mtu
== mtu
)
392 r
= link_set_mtu(link
, link
->original_mtu
);
394 log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
395 link_enter_failed(link
);
402 static int dhcp_reset_hostname(Link
*link
) {
403 const char *hostname
;
408 if (!link
->network
->dhcp_use_hostname
)
411 hostname
= link
->network
->dhcp_hostname
;
413 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
418 /* If a hostname was set due to the lease, then unset it now. */
419 r
= manager_set_hostname(link
->manager
, NULL
);
421 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
426 static int dhcp_lease_lost(Link
*link
) {
427 struct in_addr address
= {};
430 assert(link
->dhcp_lease
);
432 log_link_warning(link
, "DHCP lease lost");
434 link
->dhcp4_configured
= false;
436 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
437 (void) dhcp_remove_routes(link
, link
->dhcp_lease
, NULL
, &address
);
438 (void) dhcp_remove_router(link
, link
->dhcp_lease
, &address
);
439 (void) dhcp_remove_address(link
, link
->dhcp_lease
, &address
);
440 (void) dhcp_reset_mtu(link
);
441 (void) dhcp_reset_hostname(link
);
443 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
449 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
454 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
457 r
= sd_netlink_message_get_errno(m
);
458 if (r
< 0 && r
!= -EEXIST
) {
459 log_link_error_errno(link
, r
, "Could not set DHCPv4 address: %m");
460 link_enter_failed(link
);
464 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
466 r
= link_set_dhcp_routes(link
);
468 link_enter_failed(link
);
472 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
473 link_request_set_routes(link
);
475 if (link
->dhcp4_messages
== 0) {
476 link
->dhcp4_configured
= true;
477 /* The new address is configured, and no route is requested.
478 * Let's drop the old lease. */
479 dhcp4_release_old_lease(link
);
480 link_check_ready(link
);
486 static int dhcp4_update_address(Link
*link
,
487 struct in_addr
*address
,
488 struct in_addr
*netmask
,
490 _cleanup_(address_freep
) Address
*addr
= NULL
;
498 prefixlen
= in4_addr_netmask_to_prefixlen(netmask
);
500 r
= address_new(&addr
);
504 addr
->family
= AF_INET
;
505 addr
->in_addr
.in
.s_addr
= address
->s_addr
;
506 addr
->cinfo
.ifa_prefered
= lifetime
;
507 addr
->cinfo
.ifa_valid
= lifetime
;
508 addr
->prefixlen
= prefixlen
;
509 addr
->broadcast
.s_addr
= address
->s_addr
| ~netmask
->s_addr
;
511 /* allow reusing an existing address and simply update its lifetime
512 * in case it already exists */
513 r
= address_configure(addr
, link
, dhcp4_address_handler
, true);
520 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
521 sd_dhcp_lease
*lease
;
522 struct in_addr address
;
523 struct in_addr netmask
;
524 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
529 assert(link
->network
);
531 r
= sd_dhcp_client_get_lease(client
, &lease
);
533 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
535 sd_dhcp_lease_unref(link
->dhcp_lease
);
536 link
->dhcp4_configured
= false;
537 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
540 r
= sd_dhcp_lease_get_address(lease
, &address
);
542 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
544 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
546 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
548 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
549 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
551 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
554 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
556 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
561 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
562 const struct in_addr
*router
;
563 sd_dhcp_lease
*lease
;
564 struct in_addr address
;
565 struct in_addr netmask
;
567 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
573 link
->dhcp4_configured
= false;
575 r
= sd_dhcp_client_get_lease(client
, &lease
);
577 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
579 r
= sd_dhcp_lease_get_address(lease
, &address
);
581 return log_link_error_errno(link
, r
, "DHCP error: No address: %m");
583 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
585 return log_link_error_errno(link
, r
, "DHCP error: No netmask: %m");
587 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
589 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
590 r
= sd_dhcp_lease_get_lifetime(lease
, &lifetime
);
592 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
595 r
= sd_dhcp_lease_get_router(lease
, &router
);
596 if (r
< 0 && r
!= -ENODATA
)
597 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
599 if (r
> 0 && !in4_addr_is_null(&router
[0]))
601 LOG_LINK_INTERFACE(link
),
602 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
603 ADDRESS_FMT_VAL(address
),
605 ADDRESS_FMT_VAL(router
[0])),
606 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
607 "PREFIXLEN=%u", prefixlen
,
608 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router
[0]));
611 LOG_LINK_INTERFACE(link
),
612 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u",
613 ADDRESS_FMT_VAL(address
),
615 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
616 "PREFIXLEN=%u", prefixlen
);
618 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
621 if (link
->network
->dhcp_use_mtu
) {
624 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
626 r
= link_set_mtu(link
, mtu
);
628 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
632 if (link
->network
->dhcp_use_hostname
) {
633 const char *dhcpname
= NULL
;
634 _cleanup_free_
char *hostname
= NULL
;
636 if (link
->network
->dhcp_hostname
)
637 dhcpname
= link
->network
->dhcp_hostname
;
639 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
642 r
= shorten_overlong(dhcpname
, &hostname
);
644 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
646 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
650 r
= manager_set_hostname(link
->manager
, hostname
);
652 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
656 if (link
->network
->dhcp_use_timezone
) {
657 const char *tz
= NULL
;
659 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
662 r
= manager_set_timezone(link
->manager
, tz
);
664 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
668 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
670 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
675 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
678 link
->dhcp_lease_old
= TAKE_PTR(link
->dhcp_lease
);
680 /* On ip address change, to keep the connectability, we would like to assign new address and
681 * routes, and then release old lease. There are two possible success paths:
683 * 1. new address and routes are configured.
684 * -> handled by dhcp_release_old_lease() in dhcp4_route_handler().
685 * 2. new address is configured and no route is requested.
686 * -> handled by dhcp_release_old_lease() in dhcp4_address_handler().
688 * On error in assigning new address and routes, then the link always enters to the failed
689 * state. And link_enter_failed() leads to the DHCP client to be stopped. So,
690 * dhcp_release_old_lease() will be also called by link_stop_clients().
693 r
= dhcp_lease_acquired(client
, link
);
695 /* If it fails, then the new address is not configured yet.
696 * So, let's simply drop the old lease. */
697 sd_dhcp_lease_unref(link
->dhcp_lease
);
698 link
->dhcp_lease
= TAKE_PTR(link
->dhcp_lease_old
);
699 (void) dhcp_lease_lost(link
);
706 static int dhcp_server_is_black_listed(Link
*link
, sd_dhcp_client
*client
) {
707 sd_dhcp_lease
*lease
;
712 assert(link
->network
);
715 r
= sd_dhcp_client_get_lease(client
, &lease
);
717 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
719 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
721 return log_link_debug_errno(link
, r
, "Failed to get DHCP server ip address: %m");
723 if (set_contains(link
->network
->dhcp_black_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
724 log_struct(LOG_DEBUG
,
725 LOG_LINK_INTERFACE(link
),
726 LOG_LINK_MESSAGE(link
, "DHCPv4 ip '%u.%u.%u.%u' found in black listed ip addresses, ignoring offer",
727 ADDRESS_FMT_VAL(addr
)));
734 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
735 Link
*link
= userdata
;
739 assert(link
->network
);
740 assert(link
->manager
);
742 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
746 case SD_DHCP_CLIENT_EVENT_STOP
:
748 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
)) {
749 assert(link
->ipv4ll
);
751 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
753 r
= sd_ipv4ll_start(link
->ipv4ll
);
755 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
758 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
759 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
763 if (link
->network
->dhcp_send_release
)
764 (void) sd_dhcp_client_send_release(client
);
766 if (link
->dhcp_lease
) {
767 r
= dhcp_lease_lost(link
);
769 link_enter_failed(link
);
775 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
776 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
777 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
781 if (link
->dhcp_lease
) {
782 r
= dhcp_lease_lost(link
);
784 link_enter_failed(link
);
790 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
791 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
792 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
796 r
= dhcp_lease_ip_change(client
, link
);
798 link_enter_failed(link
);
803 case SD_DHCP_CLIENT_EVENT_RENEW
:
804 r
= dhcp_lease_renew(client
, link
);
806 link_enter_failed(link
);
810 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
811 r
= dhcp_lease_acquired(client
, link
);
813 link_enter_failed(link
);
817 case SD_DHCP_CLIENT_EVENT_SELECTING
:
818 r
= dhcp_server_is_black_listed(link
, client
);
826 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
828 log_link_warning(link
, "DHCP unknown event: %i", event
);
835 static int dhcp4_set_hostname(Link
*link
) {
836 _cleanup_free_
char *hostname
= NULL
;
842 if (!link
->network
->dhcp_send_hostname
)
844 else if (link
->network
->dhcp_hostname
)
845 hn
= link
->network
->dhcp_hostname
;
847 r
= gethostname_strict(&hostname
);
848 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
854 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
855 if (r
== -EINVAL
&& hostname
)
856 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
857 log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
859 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
864 static bool promote_secondaries_enabled(const char *ifname
) {
865 _cleanup_free_
char *promote_secondaries_sysctl
= NULL
;
866 char *promote_secondaries_path
;
869 promote_secondaries_path
= strjoina("net/ipv4/conf/", ifname
, "/promote_secondaries");
870 r
= sysctl_read(promote_secondaries_path
, &promote_secondaries_sysctl
);
872 log_debug_errno(r
, "Cannot read sysctl %s", promote_secondaries_path
);
876 truncate_nl(promote_secondaries_sysctl
);
877 r
= parse_boolean(promote_secondaries_sysctl
);
879 log_warning_errno(r
, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path
, promote_secondaries_sysctl
);
883 /* dhcp4_set_promote_secondaries will ensure this interface has
884 * the "promote_secondaries" option in the kernel set. If this sysctl
885 * is not set DHCP will work only as long as the IP address does not
886 * changes between leases. The kernel will remove all secondary IP
887 * addresses of an interface otherwise. The way systemd-network works
888 * is that the new IP of a lease is added as a secondary IP and when
889 * the primary one expires it relies on the kernel to promote the
890 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
892 int dhcp4_set_promote_secondaries(Link
*link
) {
896 assert(link
->network
);
897 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
899 /* check if the kernel has promote_secondaries enabled for our
900 * interface. If it is not globally enabled or enabled for the
901 * specific interface we must either enable it.
903 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link
->ifname
))) {
904 char *promote_secondaries_path
= NULL
;
906 log_link_debug(link
, "promote_secondaries is unset, setting it");
907 promote_secondaries_path
= strjoina("net/ipv4/conf/", link
->ifname
, "/promote_secondaries");
908 r
= sysctl_write(promote_secondaries_path
, "1");
910 log_link_warning_errno(link
, r
, "cannot set sysctl %s to 1", promote_secondaries_path
);
917 int dhcp4_set_client_identifier(Link
*link
) {
921 assert(link
->network
);
922 assert(link
->dhcp_client
);
924 switch (link
->network
->dhcp_client_identifier
) {
925 case DHCP_CLIENT_ID_DUID
: {
926 /* If configured, apply user specified DUID and IAID */
927 const DUID
*duid
= link_get_duid(link
);
929 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
930 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
931 link
->network
->iaid_set
,
935 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
936 link
->network
->iaid_set
,
939 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
942 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
945 case DHCP_CLIENT_ID_DUID_ONLY
: {
946 /* If configured, apply user specified DUID */
947 const DUID
*duid
= link_get_duid(link
);
949 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
950 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
953 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
955 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
958 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
961 case DHCP_CLIENT_ID_MAC
:
962 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
964 (const uint8_t *) &link
->mac
,
967 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
970 assert_not_reached("Unknown client identifier type.");
976 int dhcp4_configure(Link
*link
) {
980 assert(link
->network
);
981 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
983 if (!link
->dhcp_client
) {
984 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
988 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
991 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
993 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to attach event: %m");
995 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
996 (const uint8_t *) &link
->mac
,
997 sizeof (link
->mac
), ARPHRD_ETHER
);
999 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1001 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1003 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1005 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1007 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1009 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
,
1010 link
->network
->dhcp_broadcast
);
1012 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1015 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1017 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1020 if (link
->network
->dhcp_use_mtu
) {
1021 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1022 SD_DHCP_OPTION_INTERFACE_MTU
);
1024 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1027 /* NOTE: even if this variable is called "use", it also "sends" PRL
1028 * options, maybe there should be a different configuration variable
1029 * to send or not route options?. */
1030 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1031 * by default, so they don't need to be added here. */
1032 if (link
->network
->dhcp_use_routes
&& !link
->network
->dhcp_anonymize
) {
1033 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1034 SD_DHCP_OPTION_STATIC_ROUTE
);
1036 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1038 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1039 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1041 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1044 if (link
->network
->dhcp_use_ntp
) {
1045 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1047 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1050 if (link
->network
->dhcp_use_timezone
) {
1051 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1053 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1056 r
= dhcp4_set_hostname(link
);
1060 if (link
->network
->dhcp_vendor_class_identifier
) {
1061 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1062 link
->network
->dhcp_vendor_class_identifier
);
1064 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1067 if (link
->network
->dhcp_user_class
) {
1068 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, (const char **) link
->network
->dhcp_user_class
);
1070 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1073 if (link
->network
->dhcp_client_port
) {
1074 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1076 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen port: %m");
1079 if (link
->network
->dhcp_max_attempts
> 0) {
1080 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1082 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1085 return dhcp4_set_client_identifier(link
);
1088 int config_parse_dhcp_max_attempts(
1090 const char *filename
,
1092 const char *section
,
1093 unsigned section_line
,
1100 Network
*network
= data
;
1108 if (isempty(rvalue
)) {
1109 network
->dhcp_max_attempts
= 0;
1113 if (streq(rvalue
, "infinity")) {
1114 network
->dhcp_max_attempts
= (uint64_t) -1;
1118 r
= safe_atou64(rvalue
, &a
);
1120 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1121 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1126 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1127 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1131 network
->dhcp_max_attempts
= a
;
1136 int config_parse_dhcp_black_listed_ip_address(
1138 const char *filename
,
1140 const char *section
,
1141 unsigned section_line
,
1148 Network
*network
= data
;
1157 if (isempty(rvalue
)) {
1158 network
->dhcp_black_listed_ip
= set_free(network
->dhcp_black_listed_ip
);
1162 for (p
= rvalue
;;) {
1163 _cleanup_free_
char *n
= NULL
;
1164 union in_addr_union ip
;
1166 r
= extract_first_word(&p
, &n
, NULL
, 0);
1168 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1169 "Failed to parse DHCP black listed ip address, ignoring assignment: %s",
1176 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1178 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1179 "DHCP black listed ip address is invalid, ignoring assignment: %s", n
);
1183 r
= set_ensure_allocated(&network
->dhcp_black_listed_ip
, NULL
);
1187 r
= set_put(network
->dhcp_black_listed_ip
, UINT32_TO_PTR(ip
.in
.s_addr
));
1189 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1190 "Failed to store DHCP black listed ip address '%s', ignoring assignment: %m", n
);
1196 int config_parse_dhcp_user_class(
1198 const char *filename
,
1200 const char *section
,
1201 unsigned section_line
,
1215 if (isempty(rvalue
)) {
1221 _cleanup_free_
char *w
= NULL
;
1223 r
= extract_first_word(&rvalue
, &w
, NULL
, 0);
1227 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1228 "Failed to split user classes option, ignoring: %s", rvalue
);
1234 if (strlen(w
) > 255) {
1235 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1236 "%s length is not in the range 1-255, ignoring.", w
);
1240 r
= strv_push(l
, w
);
1250 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1251 [DHCP_CLIENT_ID_MAC
] = "mac",
1252 [DHCP_CLIENT_ID_DUID
] = "duid",
1253 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1256 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1257 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1258 "Failed to parse client identifier type");