1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
5 #include <linux/if_arp.h>
7 #include "alloc-util.h"
8 #include "dhcp-client-internal.h"
9 #include "hostname-util.h"
10 #include "parse-util.h"
11 #include "network-internal.h"
12 #include "networkd-dhcp4.h"
13 #include "networkd-link.h"
14 #include "networkd-manager.h"
15 #include "networkd-network.h"
16 #include "string-table.h"
17 #include "string-util.h"
18 #include "sysctl-util.h"
20 static int dhcp_remove_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
21 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
22 static int dhcp_remove_dns_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
);
23 static int dhcp_remove_address(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, link_netlink_message_handler_t callback
);
24 static int dhcp_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
);
25 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
);
27 void dhcp4_release_old_lease(Link
*link
) {
28 struct in_addr address
= {}, address_old
= {};
32 if (!link
->dhcp_lease_old
)
35 assert(link
->dhcp_lease
);
37 (void) sd_dhcp_lease_get_address(link
->dhcp_lease_old
, &address_old
);
38 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
40 (void) dhcp_remove_routes(link
, link
->dhcp_lease_old
, &address_old
, false);
41 (void) dhcp_remove_router(link
, link
->dhcp_lease_old
, &address_old
, false);
42 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease_old
, &address_old
, false);
44 if (!in4_addr_equal(&address_old
, &address
))
45 (void) dhcp_remove_address(link
, link
->dhcp_lease_old
, &address_old
, NULL
);
47 link
->dhcp_lease_old
= sd_dhcp_lease_unref(link
->dhcp_lease_old
);
51 static int dhcp4_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
55 assert(link
->dhcp4_messages
> 0);
57 link
->dhcp4_messages
--;
59 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
62 r
= sd_netlink_message_get_errno(m
);
63 if (r
== -ENETUNREACH
&& !link
->dhcp4_route_retrying
) {
65 /* It seems kernel does not support that the prefix route cannot be configured with
66 * route table. Let's once drop the config and reconfigure them later. */
68 log_link_message_debug_errno(link
, m
, r
, "Could not set DHCPv4 route, retrying later: %m");
69 link
->dhcp4_route_failed
= true;
70 link
->manager
->dhcp4_prefix_root_cannot_set_table
= true;
71 } else if (r
< 0 && r
!= -EEXIST
) {
72 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 route: %m");
73 link_enter_failed(link
);
77 if (link
->dhcp4_messages
== 0) {
78 if (link
->dhcp4_route_failed
) {
79 struct in_addr address
= {};
81 link
->dhcp4_route_failed
= false;
82 link
->dhcp4_route_retrying
= true;
84 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
85 (void) dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true);
86 (void) dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true);
87 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true);
88 (void) dhcp_remove_address(link
, link
->dhcp_lease
, &address
, dhcp_remove_address_handler
);
92 link
->dhcp4_configured
= true;
93 /* New address and routes are configured now. Let's release old lease. */
94 dhcp4_release_old_lease(link
);
95 link_check_ready(link
);
101 static int route_scope_from_address(const Route
*route
, const struct in_addr
*self_addr
) {
105 if (in4_addr_is_localhost(&route
->dst
.in
) ||
106 (!in4_addr_is_null(self_addr
) && in4_addr_equal(&route
->dst
.in
, self_addr
)))
107 return RT_SCOPE_HOST
;
108 else if (in4_addr_is_null(&route
->gw
.in
))
109 return RT_SCOPE_LINK
;
111 return RT_SCOPE_UNIVERSE
;
114 static bool link_noprefixroute(Link
*link
) {
115 return link
->network
->dhcp_route_table_set
&&
116 link
->network
->dhcp_route_table
!= RT_TABLE_MAIN
&&
117 !link
->manager
->dhcp4_prefix_root_cannot_set_table
;
120 static int dhcp_route_configure(Route
**route
, Link
*link
) {
127 if (set_contains(link
->dhcp_routes
, *route
))
130 r
= route_configure(*route
, link
, dhcp4_route_handler
);
134 link
->dhcp4_messages
++;
136 r
= set_put(link
->dhcp_routes
, *route
);
144 static int link_set_dns_routes(Link
*link
, const struct in_addr
*address
) {
145 const struct in_addr
*dns
;
150 assert(link
->dhcp_lease
);
151 assert(link
->network
);
153 if (!link
->network
->dhcp_use_dns
||
154 !link
->network
->dhcp_routes_to_dns
)
157 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &dns
);
158 if (IN_SET(n
, 0, -ENODATA
))
161 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
163 table
= link_get_dhcp_route_table(link
);
165 for (i
= 0; i
< n
; i
++) {
166 _cleanup_(route_freep
) Route
*route
= NULL
;
168 r
= route_new(&route
);
170 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
172 /* Set routes to DNS servers. */
174 route
->family
= AF_INET
;
175 route
->dst
.in
= dns
[i
];
176 route
->dst_prefixlen
= 32;
177 route
->prefsrc
.in
= *address
;
178 route
->scope
= RT_SCOPE_LINK
;
179 route
->protocol
= RTPROT_DHCP
;
180 route
->priority
= link
->network
->dhcp_route_metric
;
181 route
->table
= table
;
183 r
= dhcp_route_configure(&route
, link
);
185 return log_link_error_errno(link
, r
, "Could not set route to DNS server: %m");
191 static int dhcp_prefix_route_from_lease(
192 const sd_dhcp_lease
*lease
,
194 const struct in_addr
*address
,
198 struct in_addr netmask
;
201 r
= sd_dhcp_lease_get_netmask((sd_dhcp_lease
*) lease
, &netmask
);
205 r
= route_new(&route
);
209 route
->family
= AF_INET
;
210 route
->dst
.in
.s_addr
= address
->s_addr
& netmask
.s_addr
;
211 route
->dst_prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
212 route
->prefsrc
.in
= *address
;
213 route
->scope
= RT_SCOPE_LINK
;
214 route
->protocol
= RTPROT_DHCP
;
215 route
->table
= table
;
220 static int link_set_dhcp_routes(Link
*link
) {
221 _cleanup_free_ sd_dhcp_route
**static_routes
= NULL
;
222 bool classless_route
= false, static_route
= false;
223 const struct in_addr
*router
;
224 struct in_addr address
;
230 if (!link
->dhcp_lease
) /* link went down while we configured the IP addresses? */
233 if (!link
->network
) /* link went down while we configured the IP addresses? */
236 if (!link
->network
->dhcp_use_routes
)
239 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
240 /* During configuring addresses, the link lost its carrier. As networkd is dropping
241 * the addresses now, let's not configure the routes either. */
244 r
= set_ensure_allocated(&link
->dhcp_routes
, &route_hash_ops
);
248 /* Clear old entries in case the set was already allocated */
249 set_clear(link
->dhcp_routes
);
251 table
= link_get_dhcp_route_table(link
);
253 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
255 return log_link_warning_errno(link
, r
, "DHCP error: could not get address: %m");
257 if (link_noprefixroute(link
)) {
258 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
260 r
= dhcp_prefix_route_from_lease(link
->dhcp_lease
, table
, &address
, &prefix_route
);
262 return log_link_error_errno(link
, r
, "Could not create prefix route: %m");
264 r
= dhcp_route_configure(&prefix_route
, link
);
266 return log_link_error_errno(link
, r
, "Could not set prefix route: %m");
269 n
= sd_dhcp_lease_get_routes(link
->dhcp_lease
, &static_routes
);
271 log_link_debug_errno(link
, n
, "DHCP: No routes received from DHCP server: %m");
273 log_link_debug_errno(link
, n
, "DHCP: could not get routes: %m");
275 for (i
= 0; i
< n
; i
++) {
276 switch (sd_dhcp_route_get_option(static_routes
[i
])) {
277 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
278 classless_route
= true;
280 case SD_DHCP_OPTION_STATIC_ROUTE
:
286 for (i
= 0; i
< n
; i
++) {
287 _cleanup_(route_freep
) Route
*route
= NULL
;
289 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
290 the DHCP client MUST ignore the Static Routes option. */
291 if (classless_route
&&
292 sd_dhcp_route_get_option(static_routes
[i
]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
)
295 r
= route_new(&route
);
297 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
299 route
->family
= AF_INET
;
300 route
->protocol
= RTPROT_DHCP
;
301 assert_se(sd_dhcp_route_get_gateway(static_routes
[i
], &route
->gw
.in
) >= 0);
302 assert_se(sd_dhcp_route_get_destination(static_routes
[i
], &route
->dst
.in
) >= 0);
303 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes
[i
], &route
->dst_prefixlen
) >= 0);
304 route
->priority
= link
->network
->dhcp_route_metric
;
305 route
->table
= table
;
306 route
->scope
= route_scope_from_address(route
, &address
);
307 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
308 route
->prefsrc
.in
= address
;
310 if (set_contains(link
->dhcp_routes
, route
))
313 r
= dhcp_route_configure(&route
, link
);
315 return log_link_error_errno(link
, r
, "Could not set route: %m");
318 r
= sd_dhcp_lease_get_router(link
->dhcp_lease
, &router
);
319 if (IN_SET(r
, 0, -ENODATA
))
320 log_link_info(link
, "DHCP: No gateway received from DHCP server.");
322 log_link_warning_errno(link
, r
, "DHCP error: could not get gateway: %m");
323 else if (in4_addr_is_null(&router
[0]))
324 log_link_info(link
, "DHCP: Received gateway is null.");
326 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
327 a Router option, the DHCP client MUST ignore the Router option. */
328 if (classless_route
&& static_route
)
329 log_link_warning(link
, "Classless static routes received from DHCP server: ignoring static-route option and router option");
331 if (r
> 0 && !classless_route
&& !in4_addr_is_null(&router
[0])) {
332 _cleanup_(route_freep
) Route
*route
= NULL
, *route_gw
= NULL
;
334 r
= route_new(&route_gw
);
336 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
338 /* The dhcp netmask may mask out the gateway. Add an explicit
339 * route for the gw host so that we can route no matter the
340 * netmask or existing kernel route tables. */
341 route_gw
->family
= AF_INET
;
342 route_gw
->dst
.in
= router
[0];
343 route_gw
->dst_prefixlen
= 32;
344 route_gw
->prefsrc
.in
= address
;
345 route_gw
->scope
= RT_SCOPE_LINK
;
346 route_gw
->protocol
= RTPROT_DHCP
;
347 route_gw
->priority
= link
->network
->dhcp_route_metric
;
348 route_gw
->table
= table
;
350 r
= dhcp_route_configure(&route_gw
, link
);
352 return log_link_error_errno(link
, r
, "Could not set host route: %m");
354 r
= route_new(&route
);
356 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
358 route
->family
= AF_INET
;
359 route
->gw
.in
= router
[0];
360 route
->prefsrc
.in
= address
;
361 route
->protocol
= RTPROT_DHCP
;
362 route
->priority
= link
->network
->dhcp_route_metric
;
363 route
->table
= table
;
365 r
= dhcp_route_configure(&route
, link
);
367 return log_link_error_errno(link
, r
, "Could not set router: %m");
370 return link_set_dns_routes(link
, &address
);
373 static int dhcp_remove_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
374 _cleanup_free_ sd_dhcp_route
**routes
= NULL
;
381 if (!link
->network
->dhcp_use_routes
)
384 n
= sd_dhcp_lease_get_routes(lease
, &routes
);
385 if (IN_SET(n
, 0, -ENODATA
))
388 return log_link_error_errno(link
, n
, "DHCP error: Failed to get routes: %m");
390 table
= link_get_dhcp_route_table(link
);
392 for (i
= 0; i
< n
; i
++) {
393 _cleanup_(route_freep
) Route
*route
= NULL
;
395 r
= route_new(&route
);
399 route
->family
= AF_INET
;
400 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &route
->gw
.in
) >= 0);
401 assert_se(sd_dhcp_route_get_destination(routes
[i
], &route
->dst
.in
) >= 0);
402 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &route
->dst_prefixlen
) >= 0);
403 route
->priority
= link
->network
->dhcp_route_metric
;
404 route
->table
= table
;
405 route
->scope
= route_scope_from_address(route
, address
);
406 if (IN_SET(route
->scope
, RT_SCOPE_LINK
, RT_SCOPE_UNIVERSE
))
407 route
->prefsrc
.in
= *address
;
409 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
412 (void) route_remove(route
, link
, NULL
);
418 static int dhcp_remove_router(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
419 _cleanup_(route_freep
) Route
*route_gw
= NULL
, *route
= NULL
;
420 const struct in_addr
*router
;
427 if (!link
->network
->dhcp_use_routes
)
430 r
= sd_dhcp_lease_get_router(lease
, &router
);
431 if (IN_SET(r
, 0, -ENODATA
)) {
432 log_link_debug(link
, "DHCP: No gateway received from DHCP server.");
435 return log_link_error_errno(link
, r
, "DHCP error: could not get gateway: %m");
436 else if (in4_addr_is_null(&router
[0])) {
437 log_link_info(link
, "DHCP: Received gateway is null, ignoring.");
441 table
= link_get_dhcp_route_table(link
);
443 r
= route_new(&route_gw
);
447 route_gw
->family
= AF_INET
;
448 route_gw
->dst
.in
= router
[0];
449 route_gw
->dst_prefixlen
= 32;
450 route_gw
->prefsrc
.in
= *address
;
451 route_gw
->scope
= RT_SCOPE_LINK
;
452 route_gw
->protocol
= RTPROT_DHCP
;
453 route_gw
->priority
= link
->network
->dhcp_route_metric
;
454 route_gw
->table
= table
;
456 if (remove_all
|| !set_contains(link
->dhcp_routes
, route_gw
))
457 (void) route_remove(route_gw
, link
, NULL
);
459 r
= route_new(&route
);
463 route
->family
= AF_INET
;
464 route
->gw
.in
= router
[0];
465 route
->prefsrc
.in
= *address
;
466 route
->protocol
= RTPROT_DHCP
;
467 route
->priority
= link
->network
->dhcp_route_metric
;
468 route
->table
= table
;
470 if (remove_all
|| !set_contains(link
->dhcp_routes
, route
))
471 (void) route_remove(route
, link
, NULL
);
476 static int dhcp_remove_dns_routes(Link
*link
, sd_dhcp_lease
*lease
, const struct in_addr
*address
, bool remove_all
) {
477 const struct in_addr
*dns
;
483 assert(link
->network
);
485 if (!link
->network
->dhcp_use_dns
||
486 !link
->network
->dhcp_routes_to_dns
)
489 n
= sd_dhcp_lease_get_dns(lease
, &dns
);
490 if (IN_SET(n
, 0, -ENODATA
))
493 return log_link_warning_errno(link
, n
, "DHCP error: could not get DNS servers: %m");
495 table
= link_get_dhcp_route_table(link
);
497 for (i
= 0; i
< n
; i
++) {
498 _cleanup_(route_freep
) Route
*route
= NULL
;
500 r
= route_new(&route
);
502 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
504 route
->family
= AF_INET
;
505 route
->dst
.in
= dns
[i
];
506 route
->dst_prefixlen
= 32;
507 route
->prefsrc
.in
= *address
;
508 route
->scope
= RT_SCOPE_LINK
;
509 route
->protocol
= RTPROT_DHCP
;
510 route
->priority
= link
->network
->dhcp_route_metric
;
511 route
->table
= table
;
513 if (!remove_all
&& set_contains(link
->dhcp_routes
, route
))
516 (void) route_remove(route
, link
, NULL
);
519 if (link_noprefixroute(link
)) {
520 _cleanup_(route_freep
) Route
*prefix_route
= NULL
;
522 r
= dhcp_prefix_route_from_lease(lease
, table
, address
, &prefix_route
);
524 return log_link_warning_errno(link
, r
, "Could not delete prefix route: %m");
526 if (remove_all
|| !set_contains(link
->dhcp_routes
, prefix_route
))
527 (void) route_remove(prefix_route
, link
, NULL
);
533 static int dhcp_remove_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
538 /* This is only used when retrying to assign the address received from DHCPv4 server.
539 * See dhcp4_route_handler(). */
541 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
544 r
= sd_netlink_message_get_errno(m
);
546 log_link_message_warning_errno(link
, m
, r
, "Failed to remove DHCPv4 address, ignoring");
548 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
550 (void) dhcp_lease_renew(link
->dhcp_client
, link
);
554 static int dhcp_remove_address(
555 Link
*link
, sd_dhcp_lease
*lease
,
556 const struct in_addr
*address
,
557 link_netlink_message_handler_t callback
) {
559 _cleanup_(address_freep
) Address
*a
= NULL
;
560 struct in_addr netmask
;
566 if (in4_addr_is_null(address
))
574 a
->in_addr
.in
= *address
;
576 if (sd_dhcp_lease_get_netmask(lease
, &netmask
) >= 0)
577 a
->prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
579 (void) address_remove(a
, link
, callback
);
584 static int dhcp_reset_mtu(Link
*link
) {
590 if (!link
->network
->dhcp_use_mtu
)
593 r
= sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &mtu
);
597 if (link
->original_mtu
== mtu
)
600 r
= link_set_mtu(link
, link
->original_mtu
);
602 log_link_error_errno(link
, r
, "DHCP error: could not reset MTU: %m");
603 link_enter_failed(link
);
610 static int dhcp_reset_hostname(Link
*link
) {
611 const char *hostname
;
616 if (!link
->network
->dhcp_use_hostname
)
619 hostname
= link
->network
->dhcp_hostname
;
621 (void) sd_dhcp_lease_get_hostname(link
->dhcp_lease
, &hostname
);
626 /* If a hostname was set due to the lease, then unset it now. */
627 r
= manager_set_hostname(link
->manager
, NULL
);
629 return log_link_error_errno(link
, r
, "DHCP error: Failed to reset transient hostname: %m");
634 static int dhcp_lease_lost(Link
*link
) {
635 struct in_addr address
= {};
638 assert(link
->dhcp_lease
);
640 log_link_info(link
, "DHCP lease lost");
642 link
->dhcp4_configured
= false;
644 (void) sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
645 (void) dhcp_remove_routes(link
, link
->dhcp_lease
, &address
, true);
646 (void) dhcp_remove_router(link
, link
->dhcp_lease
, &address
, true);
647 (void) dhcp_remove_dns_routes(link
, link
->dhcp_lease
, &address
, true);
648 (void) dhcp_remove_address(link
, link
->dhcp_lease
, &address
, NULL
);
649 (void) dhcp_reset_mtu(link
);
650 (void) dhcp_reset_hostname(link
);
652 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
658 static int dhcp4_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
663 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
666 r
= sd_netlink_message_get_errno(m
);
667 if (r
< 0 && r
!= -EEXIST
) {
668 log_link_message_warning_errno(link
, m
, r
, "Could not set DHCPv4 address");
669 link_enter_failed(link
);
672 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
674 r
= link_set_dhcp_routes(link
);
676 link_enter_failed(link
);
680 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
681 r
= link_request_set_routes(link
);
683 link_enter_failed(link
);
687 if (link
->dhcp4_messages
== 0) {
688 link
->dhcp4_configured
= true;
689 /* The new address is configured, and no route is requested.
690 * Let's drop the old lease. */
691 dhcp4_release_old_lease(link
);
692 link_check_ready(link
);
698 static int dhcp4_update_address(Link
*link
,
699 struct in_addr
*address
,
700 struct in_addr
*netmask
,
702 _cleanup_(address_freep
) Address
*addr
= NULL
;
710 prefixlen
= in4_addr_netmask_to_prefixlen(netmask
);
712 r
= address_new(&addr
);
716 addr
->family
= AF_INET
;
717 addr
->in_addr
.in
.s_addr
= address
->s_addr
;
718 addr
->cinfo
.ifa_prefered
= lifetime
;
719 addr
->cinfo
.ifa_valid
= lifetime
;
720 addr
->prefixlen
= prefixlen
;
721 addr
->broadcast
.s_addr
= address
->s_addr
| ~netmask
->s_addr
;
722 addr
->prefix_route
= link_noprefixroute(link
);
724 /* allow reusing an existing address and simply update its lifetime
725 * in case it already exists */
726 r
= address_configure(addr
, link
, dhcp4_address_handler
, true);
733 static int dhcp_lease_renew(sd_dhcp_client
*client
, Link
*link
) {
734 sd_dhcp_lease
*lease
;
735 struct in_addr address
;
736 struct in_addr netmask
;
737 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
742 assert(link
->network
);
744 r
= sd_dhcp_client_get_lease(client
, &lease
);
746 return log_link_warning_errno(link
, r
, "DHCP error: no lease: %m");
748 sd_dhcp_lease_unref(link
->dhcp_lease
);
749 link
->dhcp4_configured
= false;
750 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
753 r
= sd_dhcp_lease_get_address(lease
, &address
);
755 return log_link_warning_errno(link
, r
, "DHCP error: no address: %m");
757 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
759 return log_link_warning_errno(link
, r
, "DHCP error: no netmask: %m");
761 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
762 r
= sd_dhcp_lease_get_lifetime(link
->dhcp_lease
, &lifetime
);
764 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
767 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
769 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
774 static int dhcp_lease_acquired(sd_dhcp_client
*client
, Link
*link
) {
775 const struct in_addr
*router
;
776 sd_dhcp_lease
*lease
;
777 struct in_addr address
;
778 struct in_addr netmask
;
780 uint32_t lifetime
= CACHE_INFO_INFINITY_LIFE_TIME
;
786 link
->dhcp4_configured
= false;
788 r
= sd_dhcp_client_get_lease(client
, &lease
);
790 return log_link_error_errno(link
, r
, "DHCP error: No lease: %m");
792 r
= sd_dhcp_lease_get_address(lease
, &address
);
794 return log_link_error_errno(link
, r
, "DHCP error: No address: %m");
796 r
= sd_dhcp_lease_get_netmask(lease
, &netmask
);
798 return log_link_error_errno(link
, r
, "DHCP error: No netmask: %m");
800 prefixlen
= in4_addr_netmask_to_prefixlen(&netmask
);
802 if (!FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
803 r
= sd_dhcp_lease_get_lifetime(lease
, &lifetime
);
805 return log_link_warning_errno(link
, r
, "DHCP error: no lifetime: %m");
808 r
= sd_dhcp_lease_get_router(lease
, &router
);
809 if (r
< 0 && r
!= -ENODATA
)
810 return log_link_error_errno(link
, r
, "DHCP error: Could not get gateway: %m");
812 if (r
> 0 && !in4_addr_is_null(&router
[0]))
814 LOG_LINK_INTERFACE(link
),
815 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
816 ADDRESS_FMT_VAL(address
),
818 ADDRESS_FMT_VAL(router
[0])),
819 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
820 "PREFIXLEN=%u", prefixlen
,
821 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router
[0]));
824 LOG_LINK_INTERFACE(link
),
825 LOG_LINK_MESSAGE(link
, "DHCPv4 address %u.%u.%u.%u/%u",
826 ADDRESS_FMT_VAL(address
),
828 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address
),
829 "PREFIXLEN=%u", prefixlen
);
831 link
->dhcp_lease
= sd_dhcp_lease_ref(lease
);
834 if (link
->network
->dhcp_use_mtu
) {
837 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
839 r
= link_set_mtu(link
, mtu
);
841 log_link_error_errno(link
, r
, "Failed to set MTU to %" PRIu16
": %m", mtu
);
845 if (link
->network
->dhcp_use_hostname
) {
846 const char *dhcpname
= NULL
;
847 _cleanup_free_
char *hostname
= NULL
;
849 if (link
->network
->dhcp_hostname
)
850 dhcpname
= link
->network
->dhcp_hostname
;
852 (void) sd_dhcp_lease_get_hostname(lease
, &dhcpname
);
855 r
= shorten_overlong(dhcpname
, &hostname
);
857 log_link_warning_errno(link
, r
, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname
);
859 log_link_notice(link
, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname
, hostname
);
863 r
= manager_set_hostname(link
->manager
, hostname
);
865 log_link_error_errno(link
, r
, "Failed to set transient hostname to '%s': %m", hostname
);
869 if (link
->network
->dhcp_use_timezone
) {
870 const char *tz
= NULL
;
872 (void) sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
875 r
= manager_set_timezone(link
->manager
, tz
);
877 log_link_error_errno(link
, r
, "Failed to set timezone to '%s': %m", tz
);
881 r
= dhcp4_update_address(link
, &address
, &netmask
, lifetime
);
883 return log_link_warning_errno(link
, r
, "Could not update IP address: %m");
888 static int dhcp_lease_ip_change(sd_dhcp_client
*client
, Link
*link
) {
891 link
->dhcp_lease_old
= TAKE_PTR(link
->dhcp_lease
);
893 /* On ip address change, to keep the connectability, we would like to assign new address and
894 * routes, and then release old lease. There are two possible success paths:
896 * 1. new address and routes are configured.
897 * -> handled by dhcp_release_old_lease() in dhcp4_route_handler().
898 * 2. new address is configured and no route is requested.
899 * -> handled by dhcp_release_old_lease() in dhcp4_address_handler().
901 * On error in assigning new address and routes, then the link always enters to the failed
902 * state. And link_enter_failed() leads to the DHCP client to be stopped. So,
903 * dhcp_release_old_lease() will be also called by link_stop_clients().
906 r
= dhcp_lease_acquired(client
, link
);
908 /* If it fails, then the new address is not configured yet.
909 * So, let's simply drop the old lease. */
910 sd_dhcp_lease_unref(link
->dhcp_lease
);
911 link
->dhcp_lease
= TAKE_PTR(link
->dhcp_lease_old
);
912 (void) dhcp_lease_lost(link
);
919 static int dhcp_server_is_black_listed(Link
*link
, sd_dhcp_client
*client
) {
920 sd_dhcp_lease
*lease
;
925 assert(link
->network
);
928 r
= sd_dhcp_client_get_lease(client
, &lease
);
930 return log_link_error_errno(link
, r
, "Failed to get DHCP lease: %m");
932 r
= sd_dhcp_lease_get_server_identifier(lease
, &addr
);
934 return log_link_debug_errno(link
, r
, "Failed to get DHCP server ip address: %m");
936 if (set_contains(link
->network
->dhcp_black_listed_ip
, UINT32_TO_PTR(addr
.s_addr
))) {
937 log_struct(LOG_DEBUG
,
938 LOG_LINK_INTERFACE(link
),
939 LOG_LINK_MESSAGE(link
, "DHCPv4 ip '%u.%u.%u.%u' found in black listed ip addresses, ignoring offer",
940 ADDRESS_FMT_VAL(addr
)));
947 static int dhcp4_handler(sd_dhcp_client
*client
, int event
, void *userdata
) {
948 Link
*link
= userdata
;
952 assert(link
->network
);
953 assert(link
->manager
);
955 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
959 case SD_DHCP_CLIENT_EVENT_STOP
:
961 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
)) {
962 assert(link
->ipv4ll
);
964 log_link_debug(link
, "DHCP client is stopped. Acquiring IPv4 link-local address");
966 r
= sd_ipv4ll_start(link
->ipv4ll
);
968 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
971 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
972 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
976 if (link
->dhcp_lease
) {
977 if (link
->network
->dhcp_send_release
)
978 (void) sd_dhcp_client_send_release(client
);
980 r
= dhcp_lease_lost(link
);
982 link_enter_failed(link
);
988 case SD_DHCP_CLIENT_EVENT_EXPIRED
:
989 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
990 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
994 if (link
->dhcp_lease
) {
995 r
= dhcp_lease_lost(link
);
997 link_enter_failed(link
);
1003 case SD_DHCP_CLIENT_EVENT_IP_CHANGE
:
1004 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
)) {
1005 log_link_notice(link
, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1009 r
= dhcp_lease_ip_change(client
, link
);
1011 link_enter_failed(link
);
1016 case SD_DHCP_CLIENT_EVENT_RENEW
:
1017 r
= dhcp_lease_renew(client
, link
);
1019 link_enter_failed(link
);
1023 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE
:
1024 r
= dhcp_lease_acquired(client
, link
);
1026 link_enter_failed(link
);
1030 case SD_DHCP_CLIENT_EVENT_SELECTING
:
1031 r
= dhcp_server_is_black_listed(link
, client
);
1039 log_link_warning_errno(link
, event
, "DHCP error: Client failed: %m");
1041 log_link_warning(link
, "DHCP unknown event: %i", event
);
1048 static int dhcp4_set_hostname(Link
*link
) {
1049 _cleanup_free_
char *hostname
= NULL
;
1055 if (!link
->network
->dhcp_send_hostname
)
1057 else if (link
->network
->dhcp_hostname
)
1058 hn
= link
->network
->dhcp_hostname
;
1060 r
= gethostname_strict(&hostname
);
1061 if (r
< 0 && r
!= -ENXIO
) /* ENXIO: no hostname set or hostname is "localhost" */
1067 r
= sd_dhcp_client_set_hostname(link
->dhcp_client
, hn
);
1068 if (r
== -EINVAL
&& hostname
)
1069 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1070 log_link_warning_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1072 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set hostname: %m");
1077 static bool promote_secondaries_enabled(const char *ifname
) {
1078 _cleanup_free_
char *promote_secondaries_sysctl
= NULL
;
1079 char *promote_secondaries_path
;
1082 promote_secondaries_path
= strjoina("net/ipv4/conf/", ifname
, "/promote_secondaries");
1083 r
= sysctl_read(promote_secondaries_path
, &promote_secondaries_sysctl
);
1085 log_debug_errno(r
, "Cannot read sysctl %s", promote_secondaries_path
);
1089 truncate_nl(promote_secondaries_sysctl
);
1090 r
= parse_boolean(promote_secondaries_sysctl
);
1092 log_warning_errno(r
, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path
, promote_secondaries_sysctl
);
1096 /* dhcp4_set_promote_secondaries will ensure this interface has
1097 * the "promote_secondaries" option in the kernel set. If this sysctl
1098 * is not set DHCP will work only as long as the IP address does not
1099 * changes between leases. The kernel will remove all secondary IP
1100 * addresses of an interface otherwise. The way systemd-network works
1101 * is that the new IP of a lease is added as a secondary IP and when
1102 * the primary one expires it relies on the kernel to promote the
1103 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1105 int dhcp4_set_promote_secondaries(Link
*link
) {
1109 assert(link
->network
);
1110 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1112 /* check if the kernel has promote_secondaries enabled for our
1113 * interface. If it is not globally enabled or enabled for the
1114 * specific interface we must either enable it.
1116 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link
->ifname
))) {
1117 char *promote_secondaries_path
= NULL
;
1119 log_link_debug(link
, "promote_secondaries is unset, setting it");
1120 promote_secondaries_path
= strjoina("net/ipv4/conf/", link
->ifname
, "/promote_secondaries");
1121 r
= sysctl_write(promote_secondaries_path
, "1");
1123 log_link_warning_errno(link
, r
, "cannot set sysctl %s to 1", promote_secondaries_path
);
1130 int dhcp4_set_client_identifier(Link
*link
) {
1134 assert(link
->network
);
1135 assert(link
->dhcp_client
);
1137 switch (link
->network
->dhcp_client_identifier
) {
1138 case DHCP_CLIENT_ID_DUID
: {
1139 /* If configured, apply user specified DUID and IAID */
1140 const DUID
*duid
= link_get_duid(link
);
1142 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1143 r
= sd_dhcp_client_set_iaid_duid_llt(link
->dhcp_client
,
1144 link
->network
->iaid_set
,
1145 link
->network
->iaid
,
1148 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
1149 link
->network
->iaid_set
,
1150 link
->network
->iaid
,
1152 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1153 duid
->raw_data_len
);
1155 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1158 case DHCP_CLIENT_ID_DUID_ONLY
: {
1159 /* If configured, apply user specified DUID */
1160 const DUID
*duid
= link_get_duid(link
);
1162 if (duid
->type
== DUID_TYPE_LLT
&& duid
->raw_data_len
== 0)
1163 r
= sd_dhcp_client_set_duid_llt(link
->dhcp_client
,
1166 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
1168 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
1169 duid
->raw_data_len
);
1171 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set DUID: %m");
1174 case DHCP_CLIENT_ID_MAC
:
1175 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
1177 (const uint8_t *) &link
->mac
,
1180 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set client ID: %m");
1183 assert_not_reached("Unknown client identifier type.");
1189 int dhcp4_configure(Link
*link
) {
1190 sd_dhcp_option
*send_option
;
1191 void *request_options
;
1196 assert(link
->network
);
1197 assert(link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
);
1199 if (!link
->dhcp_client
) {
1200 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
1204 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
1207 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
1209 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to attach event: %m");
1211 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1212 (const uint8_t *) &link
->mac
,
1213 sizeof (link
->mac
), ARPHRD_ETHER
);
1215 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MAC address: %m");
1217 r
= sd_dhcp_client_set_ifindex(link
->dhcp_client
, link
->ifindex
);
1219 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ifindex: %m");
1221 r
= sd_dhcp_client_set_callback(link
->dhcp_client
, dhcp4_handler
, link
);
1223 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set callback: %m");
1225 r
= sd_dhcp_client_set_request_broadcast(link
->dhcp_client
,
1226 link
->network
->dhcp_broadcast
);
1228 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1231 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
1233 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set MTU: %m");
1236 if (link
->network
->dhcp_use_mtu
) {
1237 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1238 SD_DHCP_OPTION_INTERFACE_MTU
);
1240 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1243 /* NOTE: even if this variable is called "use", it also "sends" PRL
1244 * options, maybe there should be a different configuration variable
1245 * to send or not route options?. */
1246 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1247 * by default, so they don't need to be added here. */
1248 if (link
->network
->dhcp_use_routes
&& !link
->network
->dhcp_anonymize
) {
1249 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1250 SD_DHCP_OPTION_STATIC_ROUTE
);
1252 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1254 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
,
1255 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
);
1257 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1260 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
&& !link
->network
->dhcp_anonymize
) {
1261 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
);
1263 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1266 if (link
->network
->dhcp_use_ntp
) {
1267 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NTP_SERVER
);
1269 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1272 if (link
->network
->dhcp_use_sip
) {
1273 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_SIP_SERVER
);
1275 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1278 if (link
->network
->dhcp_use_timezone
) {
1279 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
);
1281 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1284 SET_FOREACH(request_options
, link
->network
->dhcp_request_options
, i
) {
1285 uint32_t option
= PTR_TO_UINT32(request_options
);
1287 r
= sd_dhcp_client_set_request_option(link
->dhcp_client
, option
);
1289 log_link_debug(link
, "DHCP4 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option
);
1294 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option
);
1297 ORDERED_HASHMAP_FOREACH(send_option
, link
->network
->dhcp_client_send_options
, i
) {
1298 r
= sd_dhcp_client_set_dhcp_option(link
->dhcp_client
, send_option
);
1300 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set send option: %m");
1303 r
= dhcp4_set_hostname(link
);
1307 if (link
->network
->dhcp_vendor_class_identifier
) {
1308 r
= sd_dhcp_client_set_vendor_class_identifier(link
->dhcp_client
,
1309 link
->network
->dhcp_vendor_class_identifier
);
1311 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1314 if (link
->network
->dhcp_user_class
) {
1315 r
= sd_dhcp_client_set_user_class(link
->dhcp_client
, (const char **) link
->network
->dhcp_user_class
);
1317 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set user class: %m");
1320 if (link
->network
->dhcp_client_port
) {
1321 r
= sd_dhcp_client_set_client_port(link
->dhcp_client
, link
->network
->dhcp_client_port
);
1323 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set listen port: %m");
1326 if (link
->network
->dhcp_max_attempts
> 0) {
1327 r
= sd_dhcp_client_set_max_attempts(link
->dhcp_client
, link
->network
->dhcp_max_attempts
);
1329 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set max attempts: %m");
1332 if (link
->network
->ip_service_type
> 0) {
1333 r
= sd_dhcp_client_set_service_type(link
->dhcp_client
, link
->network
->ip_service_type
);
1335 return log_link_error_errno(link
, r
, "DHCP4 CLIENT: Failed to set ip service type: %m");
1337 return dhcp4_set_client_identifier(link
);
1340 int config_parse_dhcp_max_attempts(
1342 const char *filename
,
1344 const char *section
,
1345 unsigned section_line
,
1352 Network
*network
= data
;
1360 if (isempty(rvalue
)) {
1361 network
->dhcp_max_attempts
= 0;
1365 if (streq(rvalue
, "infinity")) {
1366 network
->dhcp_max_attempts
= (uint64_t) -1;
1370 r
= safe_atou64(rvalue
, &a
);
1372 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1373 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue
);
1378 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1379 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue
, rvalue
);
1383 network
->dhcp_max_attempts
= a
;
1388 int config_parse_dhcp_black_listed_ip_address(
1390 const char *filename
,
1392 const char *section
,
1393 unsigned section_line
,
1400 Network
*network
= data
;
1409 if (isempty(rvalue
)) {
1410 network
->dhcp_black_listed_ip
= set_free(network
->dhcp_black_listed_ip
);
1414 for (p
= rvalue
;;) {
1415 _cleanup_free_
char *n
= NULL
;
1416 union in_addr_union ip
;
1418 r
= extract_first_word(&p
, &n
, NULL
, 0);
1420 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1421 "Failed to parse DHCP black listed ip address, ignoring assignment: %s",
1428 r
= in_addr_from_string(AF_INET
, n
, &ip
);
1430 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1431 "DHCP black listed ip address is invalid, ignoring assignment: %s", n
);
1435 r
= set_ensure_allocated(&network
->dhcp_black_listed_ip
, NULL
);
1439 r
= set_put(network
->dhcp_black_listed_ip
, UINT32_TO_PTR(ip
.in
.s_addr
));
1441 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1442 "Failed to store DHCP black listed ip address '%s', ignoring assignment: %m", n
);
1448 int config_parse_dhcp_user_class(
1450 const char *filename
,
1452 const char *section
,
1453 unsigned section_line
,
1467 if (isempty(rvalue
)) {
1473 _cleanup_free_
char *w
= NULL
;
1475 r
= extract_first_word(&rvalue
, &w
, NULL
, 0);
1479 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1480 "Failed to split user classes option, ignoring: %s", rvalue
);
1486 if (strlen(w
) > 255) {
1487 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1488 "%s length is not in the range 1-255, ignoring.", w
);
1492 r
= strv_push(l
, w
);
1502 int config_parse_dhcp_request_options(
1504 const char *filename
,
1506 const char *section
,
1507 unsigned section_line
,
1514 Network
*network
= data
;
1523 if (isempty(rvalue
)) {
1524 network
->dhcp_request_options
= set_free(network
->dhcp_request_options
);
1528 for (p
= rvalue
;;) {
1529 _cleanup_free_
char *n
= NULL
;
1532 r
= extract_first_word(&p
, &n
, NULL
, 0);
1534 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1535 "Failed to parse DHCP request option, ignoring assignment: %s",
1542 r
= safe_atou32(n
, &i
);
1544 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1545 "DHCP request option is invalid, ignoring assignment: %s", n
);
1549 if (i
< 1 || i
>= 255) {
1550 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1551 "DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n
);
1555 r
= set_ensure_allocated(&network
->dhcp_request_options
, NULL
);
1559 r
= set_put(network
->dhcp_request_options
, UINT32_TO_PTR(i
));
1561 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1562 "Failed to store DHCP request option '%s', ignoring assignment: %m", n
);
1568 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
1569 [DHCP_CLIENT_ID_MAC
] = "mac",
1570 [DHCP_CLIENT_ID_DUID
] = "duid",
1571 [DHCP_CLIENT_ID_DUID_ONLY
] = "duid-only",
1574 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DHCPClientIdentifier
);
1575 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DHCPClientIdentifier
,
1576 "Failed to parse client identifier type");