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"
10 #include "hostname-util.h"
11 #include "parse-util.h"
12 #include "network-internal.h"
13 #include "networkd-dhcp4.h"
14 #include "networkd-link.h"
15 #include "networkd-manager.h"
16 #include "networkd-network.h"
17 #include "string-table.h"
18 #include "string-util.h"
19 #include "sysctl-util.h"
21 static int dhcp_remove_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
22 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
23 static int dhcp_remove_dns_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
24 static int dhcp_remove_address(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, link_netlink_message_handler_t callback
);
25 static int dhcp_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
);
26 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
);
28 void dhcp4_release_old_lease(Link
*link
) {
29 struct in_addr address
= {}, address_old
= {};
33 if (!link
->dhcp_lease_old
)
36 assert(link
->dhcp_lease
);
38 (void) sd_dhcp_lease_get_address(link
->dhcp_lease_old
, &address_old
);
39 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
41 (void) dhcp_remove_routes(link
, link
->dhcp_lease_old
, &address_old
, false);
42 (void) dhcp_remove_router(link
, link
->dhcp_lease_old
, &address_old
, false);
43 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease_old
, &address_old
, false);
45 if (!in4_addr_equal(&address_old
, &address
))
46 (void) dhcp_remove_address(link
, link
->dhcp_lease_old
, &address_old
, NULL
);
48 link
->dhcp_lease_old
= sd_dhcp_lease_unref(link
->dhcp_lease_old
);
52 static int dhcp4_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
56 assert(link
->dhcp4_messages
> 0);
58 link
->dhcp4_messages
--;
60 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
63 r
= sd_netlink_message_get_errno(m
);
64 if (r
== -ENETUNREACH
&& !link
->dhcp4_route_retrying
) {
66 /* It seems kernel does not support that the prefix route cannot be configured with
67 * route table. Let's once drop the config and reconfigure them later. */
69 log_link_debug_errno(link
, r
, "Could not set DHCPv4 route, retrying later: %m");
70 link
->dhcp4_route_failed
= true;
71 link
->manager
->dhcp4_prefix_root_cannot_set_table
= true;
72 } else if (r
< 0 && r
!= -EEXIST
) {
73 log_link_error_errno(link
, r
, "Could not set DHCPv4 route: %m");
74 link_enter_failed(link
);
78 if (link
->dhcp4_messages
== 0) {
79 if (link
->dhcp4_route_failed
) {
80 struct in_addr address
= {};
82 link
->dhcp4_route_failed
= false;
83 link
->dhcp4_route_retrying
= true;
85 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
86 (void) dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true);
87 (void) dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true);
88 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true);
89 (void) dhcp_remove_address(link
, link
->dhcp_lease
, &address
, dhcp_remove_address_handler
);
93 link
->dhcp4_configured
= true;
94 /* New address and routes are configured now. Let's release old lease. */
95 dhcp4_release_old_lease(link
);
96 link_check_ready(link
);
102 static int route_scope_from_address(const Route
*route
, const struct in_addr
*self_addr
) {
106 if (in4_addr_is_localhost(&route
->dst
.in
) ||
107 (!in4_addr_is_null(self_addr
) && in4_addr_equal(&route
->dst
.in
, self_addr
)))
108 return RT_SCOPE_HOST
;
109 else if (in4_addr_is_null(&route
->gw
.in
))
110 return RT_SCOPE_LINK
;
112 return RT_SCOPE_UNIVERSE
;
115 static bool link_noprefixroute(Link
*link
) {
116 return link
->network
->dhcp_route_table_set
&&
117 link
->network
->dhcp_route_table
!= RT_TABLE_MAIN
&&
118 !link
->manager
->dhcp4_prefix_root_cannot_set_table
;
121 static int dhcp_route_configure(Route
**route
, Link
*link
) {
128 if (set_contains(link
->dhcp_routes
, *route
))
131 r
= route_configure(*route
, link
, dhcp4_route_handler
);
135 link
->dhcp4_messages
++;
137 r
= set_put(link
->dhcp_routes
, *route
);
145 static int link_set_dns_routes(Link
*link
, const struct in_addr
*address
) {
146 const struct in_addr
*dns
;
151 assert(link
->dhcp_lease
);
152 assert(link
->network
);
154 if (!link
->network
->dhcp_use_dns
||
155 !link
->network
->dhcp_routes_to_dns
)
158 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &dns
);
159 if (IN_SET(n
, 0, -ENODATA
))
162 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
164 table
= link_get_dhcp_route_table(link
);
166 for (i
= 0; i
< n
; i
++) {
167 _cleanup_(route_freep
) Route
*route
= NULL
;
169 r
= route_new(&route
);
171 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
173 /* Set routes to DNS servers. */
175 route
->family
= AF_INET
;
176 route
->dst
.in
= dns
[i
];
177 route
->dst_prefixlen
= 32;
178 route
->prefsrc
.in
= *address
;
179 route
->scope
= RT_SCOPE_LINK
;
180 route
->protocol
= RTPROT_DHCP
;
181 route
->priority
= link
->network
->dhcp_route_metric
;
182 route
->table
= table
;
184 r
= dhcp_route_configure(&route
, link
);
186 return log_link_error_errno(link
, r
, "Could not set route to DNS server: %m");
192 static int dhcp_prefix_route_from_lease(
193 const sd_dhcp_lease
*lease
,
195 const struct in_addr
*address
,
199 struct in_addr netmask
;
202 r
= sd_dhcp_lease_get_netmask((sd_dhcp_lease
*) lease
, &netmask
);
206 r
= route_new(&route
);
210 route
->family
= AF_INET
;
211 route
->dst
.in
.s_addr
= address
->s_addr
& netmask
.s_addr
;
212 route
->dst_prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
213 route
->prefsrc
.in
= *address
;
214 route
->scope
= RT_SCOPE_LINK
;
215 route
->protocol
= RTPROT_DHCP
;
216 route
->table
= table
;
221 static int link_set_dhcp_routes(Link
*link
) {
222 _cleanup_free_ sd_dhcp_route
**static_routes
= NULL
;
223 bool classless_route
= false, static_route
= false;
224 const struct in_addr
*router
;
225 struct in_addr address
;
231 if (!link
->dhcp_lease
) /* link went down while we configured the IP addresses? */
234 if (!link
->network
) /* link went down while we configured the IP addresses? */
237 if (!link
->network
->dhcp_use_routes
)
240 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
241 /* During configuring addresses, the link lost its carrier. As networkd is dropping
242 * the addresses now, let's not configure the routes either. */
245 r
= set_ensure_allocated(&link
->dhcp_routes
, &route_hash_ops
);
249 /* Clear old entries in case the set was already allocated */
250 set_clear(link
->dhcp_routes
);
252 table
= link_get_dhcp_route_table(link
);
254 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
256 return log_link_warning_errno(link
, r
, "DHCP error: could not get address: %m");
258 if (link_noprefixroute(link
)) {
259 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
261 r
= dhcp_prefix_route_from_lease(link
->dhcp_lease
, table
, &address
, &prefix_route
);
263 return log_link_error_errno(link
, r
, "Could not create prefix route: %m");
265 r
= dhcp_route_configure(&prefix_route
, link
);
267 return log_link_error_errno(link
, r
, "Could not set prefix route: %m");
270 n
= sd_dhcp_lease_get_routes(link
->dhcp_lease
, &static_routes
);
272 log_link_debug_errno(link
, n
, "DHCP: No routes received from DHCP server: %m");
274 log_link_debug_errno(link
, n
, "DHCP: could not get routes: %m");
276 for (i
= 0; i
< n
; i
++) {
277 switch (sd_dhcp_route_get_option(static_routes
[i
])) {
278 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
279 classless_route
= true;
281 case SD_DHCP_OPTION_STATIC_ROUTE
:
287 for (i
= 0; i
< n
; i
++) {
288 _cleanup_(route_freep
) Route
*route
= NULL
;
290 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
291 the DHCP client MUST ignore the Static Routes option. */
292 if (classless_route
&&
293 sd_dhcp_route_get_option(static_routes
[i
]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
)
296 r
= route_new(&route
);
298 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
300 route
->family
= AF_INET
;
301 route
->protocol
= RTPROT_DHCP
;
302 assert_se(sd_dhcp_route_get_gateway(static_routes
[i
], &route
->gw
.in
) >= 0);
303 assert_se(sd_dhcp_route_get_destination(static_routes
[i
], &route
->dst
.in
) >= 0);
304 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &route
->dst_prefixlen
) >= 0);
305 route
->priority
= link
->network
->dhcp_route_metric
;
306 route
->table
= table
;
307 route
->scope
= route_scope_from_address(route
, &address
);
308 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
309 route
->prefsrc
.in
= address
;
311 if (set_contains(link
->dhcp_routes
, route
))
314 r
= dhcp_route_configure(&route
, link
);
316 return log_link_error_errno(link
, r
, "Could not set route: %m");
319 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
320 if (IN_SET(r
, 0, -ENODATA
))
321 log_link_info(link
, "DHCP: No gateway received from DHCP server.");
323 log_link_warning_errno(link
, r
, "DHCP error: could not get gateway: %m");
324 else if (in4_addr_is_null(&router
[0]))
325 log_link_info(link
, "DHCP: Received gateway is null.");
327 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
328 a Router option, the DHCP client MUST ignore the Router option. */
329 if (classless_route
&& static_route
)
330 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring static-route option and router option");
332 if (r
> 0 && !classless_route
&& !in4_addr_is_null(&router
[0])) {
333 _cleanup_(route_freep
) Route
*route
= NULL
, *route_gw
= NULL
;
335 r
= route_new(&route_gw
);
337 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
339 /* The dhcp netmask may mask out the gateway. Add an explicit
340 * route for the gw host so that we can route no matter the
341 * netmask or existing kernel route tables. */
342 route_gw
->family
= AF_INET
;
343 route_gw
->dst
.in
= router
[0];
344 route_gw
->dst_prefixlen
= 32;
345 route_gw
->prefsrc
.in
= address
;
346 route_gw
->scope
= RT_SCOPE_LINK
;
347 route_gw
->protocol
= RTPROT_DHCP
;
348 route_gw
->priority
= link
->network
->dhcp_route_metric
;
349 route_gw
->table
= table
;
351 r
= dhcp_route_configure(&route_gw
, link
);
353 return log_link_error_errno(link
, r
, "Could not set host route: %m");
355 r
= route_new(&route
);
357 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
359 route
->family
= AF_INET
;
360 route
->gw
.in
= router
[0];
361 route
->prefsrc
.in
= address
;
362 route
->protocol
= RTPROT_DHCP
;
363 route
->priority
= link
->network
->dhcp_route_metric
;
364 route
->table
= table
;
366 r
= dhcp_route_configure(&route
, link
);
368 return log_link_error_errno(link
, r
, "Could not set router: %m");
371 return link_set_dns_routes(link
, &address
);
374 static int dhcp_remove_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
375 _cleanup_free_ sd_dhcp_route
**routes
= NULL
;
382 if (!link
->network
->dhcp_use_routes
)
385 n
= sd_dhcp_lease_get_routes(lease
, &routes
);
386 if (IN_SET(n
, 0, -ENODATA
))
389 return log_link_error_errno(link
, n
, "DHCP error: Failed to get routes: %m");
391 table
= link_get_dhcp_route_table(link
);
393 for (i
= 0; i
< n
; i
++) {
394 _cleanup_(route_freep
) Route
*route
= NULL
;
396 r
= route_new(&route
);
400 route
->family
= AF_INET
;
401 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &route
->gw
.in
) >= 0);
402 assert_se(sd_dhcp_route_get_destination(routes
[i
], &route
->dst
.in
) >= 0);
403 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &route
->dst_prefixlen
) >= 0);
404 route
->priority
= link
->network
->dhcp_route_metric
;
405 route
->table
= table
;
406 route
->scope
= route_scope_from_address(route
, address
);
407 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
408 route
->prefsrc
.in
= *address
;
410 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
413 (void) route_remove(route
, link
, NULL
);
419 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
420 _cleanup_(route_freep
) Route
*route_gw
= NULL
, *route
= NULL
;
421 const struct in_addr
*router
;
428 if (!link
->network
->dhcp_use_routes
)
431 r
= sd_dhcp_lease_get_router(lease
, &router
);
432 if (IN_SET(r
, 0, -ENODATA
)) {
433 log_link_debug(link
, "DHCP: No gateway received from DHCP server.");
436 return log_link_error_errno(link
, r
, "DHCP error: could not get gateway: %m");
437 else if (in4_addr_is_null(&router
[0])) {
438 log_link_info(link
, "DHCP: Received gateway is null, ignoring.");
442 table
= link_get_dhcp_route_table(link
);
444 r
= route_new(&route_gw
);
448 route_gw
->family
= AF_INET
;
449 route_gw
->dst
.in
= router
[0];
450 route_gw
->dst_prefixlen
= 32;
451 route_gw
->prefsrc
.in
= *address
;
452 route_gw
->scope
= RT_SCOPE_LINK
;
453 route_gw
->protocol
= RTPROT_DHCP
;
454 route_gw
->priority
= link
->network
->dhcp_route_metric
;
455 route_gw
->table
= table
;
457 if (remove_all
|| !set_contains(link
->dhcp_routes
, route_gw
))
458 (void) route_remove(route_gw
, link
, NULL
);
460 r
= route_new(&route
);
464 route
->family
= AF_INET
;
465 route
->gw
.in
= router
[0];
466 route
->prefsrc
.in
= *address
;
467 route
->protocol
= RTPROT_DHCP
;
468 route
->priority
= link
->network
->dhcp_route_metric
;
469 route
->table
= table
;
471 if (remove_all
|| !set_contains(link
->dhcp_routes
, route
))
472 (void) route_remove(route
, link
, NULL
);
477 static int dhcp_remove_dns_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
478 const struct in_addr
*dns
;
484 assert(link
->network
);
486 if (!link
->network
->dhcp_use_dns
||
487 !link
->network
->dhcp_routes_to_dns
)
490 n
= sd_dhcp_lease_get_dns(lease
, &dns
);
491 if (IN_SET(n
, 0, -ENODATA
))
494 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
496 table
= link_get_dhcp_route_table(link
);
498 for (i
= 0; i
< n
; i
++) {
499 _cleanup_(route_freep
) Route
*route
= NULL
;
501 r
= route_new(&route
);
503 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
505 route
->family
= AF_INET
;
506 route
->dst
.in
= dns
[i
];
507 route
->dst_prefixlen
= 32;
508 route
->prefsrc
.in
= *address
;
509 route
->scope
= RT_SCOPE_LINK
;
510 route
->protocol
= RTPROT_DHCP
;
511 route
->priority
= link
->network
->dhcp_route_metric
;
512 route
->table
= table
;
514 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
517 (void) route_remove(route
, link
, NULL
);
520 if (link_noprefixroute(link
)) {
521 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
523 r
= dhcp_prefix_route_from_lease(lease
, table
, address
, &prefix_route
);
525 return log_link_warning_errno(link
, r
, "Could not delete prefix route: %m");
527 if (remove_all
|| !set_contains(link
->dhcp_routes
, prefix_route
))
528 (void) route_remove(prefix_route
, link
, NULL
);
534 static int dhcp_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
539 /* This is only used when retrying to assign the address received from DHCPv4 server.
540 * See dhcp4_route_handler(). */
542 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
545 r
= sd_netlink_message_get_errno(m
);
547 log_link_debug_errno(link
, r
, "Failed to remove DHCPv4 address, ignoring: %m");
549 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
551 (void) dhcp_lease_renew(link
->dhcp_client
, link
);
555 static int dhcp_remove_address(
556 Link
*link
, sd_dhcp_lease
*lease
,
557 const struct in_addr
*address
,
558 link_netlink_message_handler_t callback
) {
560 _cleanup_(address_freep
) Address
*a
= NULL
;
561 struct in_addr netmask
;
567 if (in4_addr_is_null(address
))
575 a
->in_addr
.in
= *address
;
577 if (sd_dhcp_lease_get_netmask(lease
, &netmask
) >= 0)
578 a
->prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
580 (void) address_remove(a
, link
, callback
);
585 static int dhcp_reset_mtu(Link
*link
) {
591 if (!link
->network
->dhcp_use_mtu
)
594 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
598 if (link
->original_mtu
== mtu
)
601 r
= link_set_mtu(link
, link
->original_mtu
);
603 log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
604 link_enter_failed(link
);
611 static int dhcp_reset_hostname(Link
*link
) {
612 const char *hostname
;
617 if (!link
->network
->dhcp_use_hostname
)
620 hostname
= link
->network
->dhcp_hostname
;
622 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
627 /* If a hostname was set due to the lease, then unset it now. */
628 r
= manager_set_hostname(link
->manager
, NULL
);
630 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
635 static int dhcp_lease_lost(Link
*link
) {
636 struct in_addr address
= {};
639 assert(link
->dhcp_lease
);
641 log_link_info(link
, "DHCP lease lost");
643 link
->dhcp4_configured
= false;
645 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
646 (void) dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true);
647 (void) dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true);
648 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true);
649 (void) dhcp_remove_address(link
, link
->dhcp_lease
, &address
, NULL
);
650 (void) dhcp_reset_mtu(link
);
651 (void) dhcp_reset_hostname(link
);
653 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
659 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
664 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
667 r
= sd_netlink_message_get_errno(m
);
668 if (r
< 0 && r
!= -EEXIST
) {
669 log_link_error_errno(link
, r
, "Could not set DHCPv4 address: %m");
670 link_enter_failed(link
);
673 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
675 r
= link_set_dhcp_routes(link
);
677 link_enter_failed(link
);
681 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
682 r
= link_request_set_routes(link
);
684 link_enter_failed(link
);
688 if (link
->dhcp4_messages
== 0) {
689 link
->dhcp4_configured
= true;
690 /* The new address is configured, and no route is requested.
691 * Let's drop the old lease. */
692 dhcp4_release_old_lease(link
);
693 link_check_ready(link
);
699 static int dhcp4_update_address(Link
*link
,
700 struct in_addr
*address
,
701 struct in_addr
*netmask
,
703 _cleanup_(address_freep
) Address
*addr
= NULL
;
711 prefixlen
= in4_addr_netmask_to_prefixlen(netmask
);
713 r
= address_new(&addr
);
717 addr
->family
= AF_INET
;
718 addr
->in_addr
.in
.s_addr
= address
->s_addr
;
719 addr
->cinfo
.ifa_prefered
= lifetime
;
720 addr
->cinfo
.ifa_valid
= lifetime
;
721 addr
->prefixlen
= prefixlen
;
722 addr
->broadcast
.s_addr
= address
->s_addr
| ~netmask
->s_addr
;
723 addr
->prefix_route
= link_noprefixroute(link
);
725 /* allow reusing an existing address and simply update its lifetime
726 * in case it already exists */
727 r
= address_configure(addr
, link
, dhcp4_address_handler
, true);
734 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
735 sd_dhcp_lease
*lease
;
736 struct in_addr address
;
737 struct in_addr netmask
;
738 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
743 assert(link
->network
);
745 r
= sd_dhcp_client_get_lease(client
, &lease
);
747 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
749 sd_dhcp_lease_unref(link
->dhcp_lease
);
750 link
->dhcp4_configured
= false;
751 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
754 r
= sd_dhcp_lease_get_address(lease
, &address
);
756 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
758 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
760 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
762 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
763 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
765 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
768 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
770 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
775 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
776 const struct in_addr
*router
;
777 sd_dhcp_lease
*lease
;
778 struct in_addr address
;
779 struct in_addr netmask
;
781 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
787 link
->dhcp4_configured
= false;
789 r
= sd_dhcp_client_get_lease(client
, &lease
);
791 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
793 r
= sd_dhcp_lease_get_address(lease
, &address
);
795 return log_link_error_errno(link
, r
, "DHCP error: No address: %m");
797 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
799 return log_link_error_errno(link
, r
, "DHCP error: No netmask: %m");
801 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
803 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
804 r
= sd_dhcp_lease_get_lifetime(lease
, &lifetime
);
806 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
809 r
= sd_dhcp_lease_get_router(lease
, &router
);
810 if (r
< 0 && r
!= -ENODATA
)
811 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
813 if (r
> 0 && !in4_addr_is_null(&router
[0]))
815 LOG_LINK_INTERFACE(link
),
816 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
817 ADDRESS_FMT_VAL(address
),
819 ADDRESS_FMT_VAL(router
[0])),
820 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
821 "PREFIXLEN=%u", prefixlen
,
822 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router
[0]));
825 LOG_LINK_INTERFACE(link
),
826 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u",
827 ADDRESS_FMT_VAL(address
),
829 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
830 "PREFIXLEN=%u", prefixlen
);
832 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
835 if (link
->network
->dhcp_use_mtu
) {
838 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
840 r
= link_set_mtu(link
, mtu
);
842 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
846 if (link
->network
->dhcp_use_hostname
) {
847 const char *dhcpname
= NULL
;
848 _cleanup_free_
char *hostname
= NULL
;
850 if (link
->network
->dhcp_hostname
)
851 dhcpname
= link
->network
->dhcp_hostname
;
853 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
856 r
= shorten_overlong(dhcpname
, &hostname
);
858 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
860 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
864 r
= manager_set_hostname(link
->manager
, hostname
);
866 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
870 if (link
->network
->dhcp_use_timezone
) {
871 const char *tz
= NULL
;
873 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
876 r
= manager_set_timezone(link
->manager
, tz
);
878 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
882 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
884 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
889 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
892 link
->dhcp_lease_old
= TAKE_PTR(link
->dhcp_lease
);
894 /* On ip address change, to keep the connectability, we would like to assign new address and
895 * routes, and then release old lease. There are two possible success paths:
897 * 1. new address and routes are configured.
898 * -> handled by dhcp_release_old_lease() in dhcp4_route_handler().
899 * 2. new address is configured and no route is requested.
900 * -> handled by dhcp_release_old_lease() in dhcp4_address_handler().
902 * On error in assigning new address and routes, then the link always enters to the failed
903 * state. And link_enter_failed() leads to the DHCP client to be stopped. So,
904 * dhcp_release_old_lease() will be also called by link_stop_clients().
907 r
= dhcp_lease_acquired(client
, link
);
909 /* If it fails, then the new address is not configured yet.
910 * So, let's simply drop the old lease. */
911 sd_dhcp_lease_unref(link
->dhcp_lease
);
912 link
->dhcp_lease
= TAKE_PTR(link
->dhcp_lease_old
);
913 (void) dhcp_lease_lost(link
);
920 static int dhcp_server_is_black_listed(Link
*link
, sd_dhcp_client
*client
) {
921 sd_dhcp_lease
*lease
;
926 assert(link
->network
);
929 r
= sd_dhcp_client_get_lease(client
, &lease
);
931 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
933 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
935 return log_link_debug_errno(link
, r
, "Failed to get DHCP server ip address: %m");
937 if (set_contains(link
->network
->dhcp_black_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
938 log_struct(LOG_DEBUG
,
939 LOG_LINK_INTERFACE(link
),
940 LOG_LINK_MESSAGE(link
, "DHCPv4 ip '%u.%u.%u.%u' found in black listed ip addresses, ignoring offer",
941 ADDRESS_FMT_VAL(addr
)));
948 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
949 Link
*link
= userdata
;
953 assert(link
->network
);
954 assert(link
->manager
);
956 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
960 case SD_DHCP_CLIENT_EVENT_STOP
:
962 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
)) {
963 assert(link
->ipv4ll
);
965 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
967 r
= sd_ipv4ll_start(link
->ipv4ll
);
969 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
972 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
973 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
977 if (link
->dhcp_lease
) {
978 if (link
->network
->dhcp_send_release
)
979 (void) sd_dhcp_client_send_release(client
);
981 r
= dhcp_lease_lost(link
);
983 link_enter_failed(link
);
989 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
990 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
991 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
995 if (link
->dhcp_lease
) {
996 r
= dhcp_lease_lost(link
);
998 link_enter_failed(link
);
1004 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
1005 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1006 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1010 r
= dhcp_lease_ip_change(client
, link
);
1012 link_enter_failed(link
);
1017 case SD_DHCP_CLIENT_EVENT_RENEW
:
1018 r
= dhcp_lease_renew(client
, link
);
1020 link_enter_failed(link
);
1024 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1025 r
= dhcp_lease_acquired(client
, link
);
1027 link_enter_failed(link
);
1031 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1032 r
= dhcp_server_is_black_listed(link
, client
);
1040 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1042 log_link_warning(link
, "DHCP unknown event: %i", event
);
1049 static int dhcp4_set_hostname(Link
*link
) {
1050 _cleanup_free_
char *hostname
= NULL
;
1056 if (!link
->network
->dhcp_send_hostname
)
1058 else if (link
->network
->dhcp_hostname
)
1059 hn
= link
->network
->dhcp_hostname
;
1061 r
= gethostname_strict(&hostname
);
1062 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1068 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1069 if (r
== -EINVAL
&& hostname
)
1070 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1071 log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1073 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
1078 static bool promote_secondaries_enabled(const char *ifname
) {
1079 _cleanup_free_
char *promote_secondaries_sysctl
= NULL
;
1080 char *promote_secondaries_path
;
1083 promote_secondaries_path
= strjoina("net/ipv4/conf/", ifname
, "/promote_secondaries");
1084 r
= sysctl_read(promote_secondaries_path
, &promote_secondaries_sysctl
);
1086 log_debug_errno(r
, "Cannot read sysctl %s", promote_secondaries_path
);
1090 truncate_nl(promote_secondaries_sysctl
);
1091 r
= parse_boolean(promote_secondaries_sysctl
);
1093 log_warning_errno(r
, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path
, promote_secondaries_sysctl
);
1097 /* dhcp4_set_promote_secondaries will ensure this interface has
1098 * the "promote_secondaries" option in the kernel set. If this sysctl
1099 * is not set DHCP will work only as long as the IP address does not
1100 * changes between leases. The kernel will remove all secondary IP
1101 * addresses of an interface otherwise. The way systemd-network works
1102 * is that the new IP of a lease is added as a secondary IP and when
1103 * the primary one expires it relies on the kernel to promote the
1104 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1106 int dhcp4_set_promote_secondaries(Link
*link
) {
1110 assert(link
->network
);
1111 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1113 /* check if the kernel has promote_secondaries enabled for our
1114 * interface. If it is not globally enabled or enabled for the
1115 * specific interface we must either enable it.
1117 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link
->ifname
))) {
1118 char *promote_secondaries_path
= NULL
;
1120 log_link_debug(link
, "promote_secondaries is unset, setting it");
1121 promote_secondaries_path
= strjoina("net/ipv4/conf/", link
->ifname
, "/promote_secondaries");
1122 r
= sysctl_write(promote_secondaries_path
, "1");
1124 log_link_warning_errno(link
, r
, "cannot set sysctl %s to 1", promote_secondaries_path
);
1131 int dhcp4_set_client_identifier(Link
*link
) {
1135 assert(link
->network
);
1136 assert(link
->dhcp_client
);
1138 switch (link
->network
->dhcp_client_identifier
) {
1139 case DHCP_CLIENT_ID_DUID
: {
1140 /* If configured, apply user specified DUID and IAID */
1141 const DUID
*duid
= link_get_duid(link
);
1143 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1144 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1145 link
->network
->iaid_set
,
1146 link
->network
->iaid
,
1149 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
1150 link
->network
->iaid_set
,
1151 link
->network
->iaid
,
1153 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1154 duid
->raw_data_len
);
1156 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1159 case DHCP_CLIENT_ID_DUID_ONLY
: {
1160 /* If configured, apply user specified DUID */
1161 const DUID
*duid
= link_get_duid(link
);
1163 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1164 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
1167 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
1169 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1170 duid
->raw_data_len
);
1172 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
1175 case DHCP_CLIENT_ID_MAC
:
1176 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1178 (const uint8_t *) &link
->mac
,
1181 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
1184 assert_not_reached("Unknown client identifier type.");
1190 int dhcp4_configure(Link
*link
) {
1191 sd_dhcp_option
*send_option
;
1192 void *request_options
;
1197 assert(link
->network
);
1198 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1200 if (!link
->dhcp_client
) {
1201 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1205 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
1208 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
1210 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to attach event: %m");
1212 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1213 (const uint8_t *) &link
->mac
,
1214 sizeof (link
->mac
), ARPHRD_ETHER
);
1216 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1218 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1220 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1222 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1224 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1226 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
,
1227 link
->network
->dhcp_broadcast
);
1229 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1232 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1234 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1237 if (link
->network
->dhcp_use_mtu
) {
1238 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1239 SD_DHCP_OPTION_INTERFACE_MTU
);
1241 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1244 /* NOTE: even if this variable is called "use", it also "sends" PRL
1245 * options, maybe there should be a different configuration variable
1246 * to send or not route options?. */
1247 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1248 * by default, so they don't need to be added here. */
1249 if (link
->network
->dhcp_use_routes
&& !link
->network
->dhcp_anonymize
) {
1250 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1251 SD_DHCP_OPTION_STATIC_ROUTE
);
1253 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1255 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1256 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1258 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1261 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
&& !link
->network
->dhcp_anonymize
) {
1262 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
);
1264 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1267 if (link
->network
->dhcp_use_ntp
) {
1268 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1270 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1273 if (link
->network
->dhcp_use_sip
) {
1274 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_SIP_SERVER
);
1276 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1279 if (link
->network
->dhcp_use_timezone
) {
1280 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1282 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1285 SET_FOREACH(request_options
, link
->network
->dhcp_request_options
, i
) {
1286 uint32_t option
= PTR_TO_UINT32(request_options
);
1288 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, option
);
1290 log_link_debug(link
, "DHCP4 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option
);
1295 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option
);
1298 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_send_options
, i
) {
1299 r
= sd_dhcp_client_set_dhcp_option(link
->dhcp_client
, send_option
);
1301 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1304 r
= dhcp4_set_hostname(link
);
1308 if (link
->network
->dhcp_vendor_class_identifier
) {
1309 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1310 link
->network
->dhcp_vendor_class_identifier
);
1312 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1315 if (link
->network
->dhcp_user_class
) {
1316 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, (const char **) link
->network
->dhcp_user_class
);
1318 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1321 if (link
->network
->dhcp_client_port
) {
1322 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1324 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen port: %m");
1327 if (link
->network
->dhcp_max_attempts
> 0) {
1328 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1330 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1333 if (link
->network
->ip_service_type
> 0) {
1334 r
= sd_dhcp_client_set_service_type(link
->dhcp_client
, link
->network
->ip_service_type
);
1336 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ip service type: %m");
1338 return dhcp4_set_client_identifier(link
);
1341 int config_parse_dhcp_max_attempts(
1343 const char *filename
,
1345 const char *section
,
1346 unsigned section_line
,
1353 Network
*network
= data
;
1361 if (isempty(rvalue
)) {
1362 network
->dhcp_max_attempts
= 0;
1366 if (streq(rvalue
, "infinity")) {
1367 network
->dhcp_max_attempts
= (uint64_t) -1;
1371 r
= safe_atou64(rvalue
, &a
);
1373 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1374 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1379 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1380 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1384 network
->dhcp_max_attempts
= a
;
1389 int config_parse_dhcp_black_listed_ip_address(
1391 const char *filename
,
1393 const char *section
,
1394 unsigned section_line
,
1401 Network
*network
= data
;
1410 if (isempty(rvalue
)) {
1411 network
->dhcp_black_listed_ip
= set_free(network
->dhcp_black_listed_ip
);
1415 for (p
= rvalue
;;) {
1416 _cleanup_free_
char *n
= NULL
;
1417 union in_addr_union ip
;
1419 r
= extract_first_word(&p
, &n
, NULL
, 0);
1421 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1422 "Failed to parse DHCP black listed ip address, ignoring assignment: %s",
1429 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1431 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1432 "DHCP black listed ip address is invalid, ignoring assignment: %s", n
);
1436 r
= set_ensure_allocated(&network
->dhcp_black_listed_ip
, NULL
);
1440 r
= set_put(network
->dhcp_black_listed_ip
, UINT32_TO_PTR(ip
.in
.s_addr
));
1442 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1443 "Failed to store DHCP black listed ip address '%s', ignoring assignment: %m", n
);
1449 int config_parse_dhcp_user_class(
1451 const char *filename
,
1453 const char *section
,
1454 unsigned section_line
,
1468 if (isempty(rvalue
)) {
1474 _cleanup_free_
char *w
= NULL
;
1476 r
= extract_first_word(&rvalue
, &w
, NULL
, 0);
1480 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1481 "Failed to split user classes option, ignoring: %s", rvalue
);
1487 if (strlen(w
) > 255) {
1488 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1489 "%s length is not in the range 1-255, ignoring.", w
);
1493 r
= strv_push(l
, w
);
1503 int config_parse_dhcp_request_options(
1505 const char *filename
,
1507 const char *section
,
1508 unsigned section_line
,
1515 Network
*network
= data
;
1524 if (isempty(rvalue
)) {
1525 network
->dhcp_request_options
= set_free(network
->dhcp_request_options
);
1529 for (p
= rvalue
;;) {
1530 _cleanup_free_
char *n
= NULL
;
1533 r
= extract_first_word(&p
, &n
, NULL
, 0);
1535 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1536 "Failed to parse DHCP request option, ignoring assignment: %s",
1543 r
= safe_atou32(n
, &i
);
1545 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1546 "DHCP request option is invalid, ignoring assignment: %s", n
);
1550 if (i
< 1 || i
>= 255) {
1551 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1552 "DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n
);
1556 r
= set_ensure_allocated(&network
->dhcp_request_options
, NULL
);
1560 r
= set_put(network
->dhcp_request_options
, UINT32_TO_PTR(i
));
1562 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1563 "Failed to store DHCP request option '%s', ignoring assignment: %m", n
);
1569 int config_parse_dhcp_send_option(
1571 const char *filename
,
1573 const char *section
,
1574 unsigned section_line
,
1581 _cleanup_(sd_dhcp_option_unrefp
) sd_dhcp_option
*opt
= NULL
, *old
= NULL
;
1582 _cleanup_free_
char *word
= NULL
, *q
= NULL
;
1583 Network
*network
= data
;
1594 if (isempty(rvalue
)) {
1595 network
->dhcp_send_options
= ordered_hashmap_free(network
->dhcp_send_options
);
1600 r
= extract_first_word(&p
, &word
, ":", 0);
1604 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1605 "Invalid DHCP send option, ignoring assignment: %s", rvalue
);
1609 r
= safe_atou8(word
, &u
);
1611 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1612 "Invalid DHCP send option, ignoring assignment: %s", rvalue
);
1615 if (u
< 1 || u
>= 255) {
1616 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1617 "Invalid DHCP send option, valid range is 1-254, ignoring assignment: %s", rvalue
);
1621 sz
= cunescape(p
, 0, &q
);
1623 log_syntax(unit
, LOG_ERR
, filename
, line
, sz
,
1624 "Failed to decode option data, ignoring assignment: %s", p
);
1628 r
= sd_dhcp_option_new(u
, q
, sz
, &opt
);
1630 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1631 "Failed to store DHCP send option '%s', ignoring assignment: %m", rvalue
);
1635 r
= ordered_hashmap_ensure_allocated(&network
->dhcp_send_options
, &dhcp_option_hash_ops
);
1639 /* Overwrite existing option */
1640 old
= ordered_hashmap_remove(network
->dhcp_send_options
, UINT_TO_PTR(u
));
1641 r
= ordered_hashmap_put(network
->dhcp_send_options
, UINT_TO_PTR(u
), opt
);
1643 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1644 "Failed to store DHCP send option '%s'", rvalue
);
1652 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1653 [DHCP_CLIENT_ID_MAC
] = "mac",
1654 [DHCP_CLIENT_ID_DUID
] = "duid",
1655 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1658 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1659 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1660 "Failed to parse client identifier type");