]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-dhcp4.c
Drop the text argument from assert_not_reached()
[thirdparty/systemd.git] / src / network / networkd-dhcp4.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <netinet/in.h>
4 #include <netinet/ip.h>
5 #include <linux/if.h>
6 #include <linux/if_arp.h>
7
8 #include "alloc-util.h"
9 #include "dhcp-client-internal.h"
10 #include "hostname-setup.h"
11 #include "hostname-util.h"
12 #include "parse-util.h"
13 #include "network-internal.h"
14 #include "networkd-address.h"
15 #include "networkd-dhcp4.h"
16 #include "networkd-ipv4acd.h"
17 #include "networkd-link.h"
18 #include "networkd-manager.h"
19 #include "networkd-network.h"
20 #include "networkd-nexthop.h"
21 #include "networkd-queue.h"
22 #include "networkd-route.h"
23 #include "networkd-setlink.h"
24 #include "networkd-state-file.h"
25 #include "string-table.h"
26 #include "strv.h"
27 #include "sysctl-util.h"
28
29 static int dhcp4_request_address_and_routes(Link *link, bool announce);
30 static int dhcp4_remove_all(Link *link);
31
32 void network_adjust_dhcp4(Network *network) {
33 assert(network);
34
35 if (!FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV4))
36 return;
37
38 if (network->dhcp_use_gateway < 0)
39 network->dhcp_use_gateway = network->dhcp_use_routes;
40
41 /* RFC7844 section 3.: MAY contain the Client Identifier option
42 * Section 3.5: clients MUST use client identifiers based solely on the link-layer address
43 * NOTE: Using MAC, as it does not reveal extra information, and some servers might not answer
44 * if this option is not sent */
45 if (network->dhcp_anonymize &&
46 network->dhcp_client_identifier >= 0 &&
47 network->dhcp_client_identifier != DHCP_CLIENT_ID_MAC) {
48 log_warning("%s: ClientIdentifier= is set, although Anonymize=yes. Using ClientIdentifier=mac.",
49 network->filename);
50 network->dhcp_client_identifier = DHCP_CLIENT_ID_MAC;
51 }
52
53 if (network->dhcp_client_identifier < 0)
54 network->dhcp_client_identifier = network->dhcp_anonymize ? DHCP_CLIENT_ID_MAC : DHCP_CLIENT_ID_DUID;
55 }
56
57 static int dhcp4_release_old_lease(Link *link) {
58 Route *route;
59 int k, r = 0;
60
61 assert(link);
62
63 if (!link->dhcp_address_old && set_isempty(link->dhcp_routes_old))
64 return 0;
65
66 log_link_debug(link, "Removing old DHCPv4 address and routes.");
67
68 SET_FOREACH(route, link->dhcp_routes_old) {
69 k = route_remove(route, NULL, link);
70 if (k < 0)
71 r = k;
72 }
73
74 if (link->dhcp_address_old) {
75 k = address_remove(link->dhcp_address_old, link);
76 if (k < 0)
77 r = k;
78 }
79
80 return r;
81 }
82
83 static void dhcp4_check_ready(Link *link) {
84 int r;
85
86 if (link->dhcp4_messages > 0) {
87 log_link_debug(link, "%s(): DHCPv4 address and routes are not set.", __func__);
88 return;
89 }
90
91 if (!link->dhcp_address) {
92 log_link_debug(link, "%s(): DHCPv4 address is not set.", __func__);
93 return;
94 }
95
96 if (!address_is_ready(link->dhcp_address)) {
97 log_link_debug(link, "%s(): DHCPv4 address is not ready.", __func__);
98 return;
99 }
100
101 link->dhcp4_configured = true;
102
103 /* New address and routes are configured now. Let's release old lease. */
104 r = dhcp4_release_old_lease(link);
105 if (r < 0) {
106 link_enter_failed(link);
107 return;
108 }
109
110 r = sd_ipv4ll_stop(link->ipv4ll);
111 if (r < 0)
112 log_link_warning_errno(link, r, "Failed to drop IPv4 link-local address, ignoring: %m");
113
114 link_check_ready(link);
115 }
116
117 static int dhcp4_after_route_configure(Request *req, void *object) {
118 Route *route = object;
119 Link *link;
120 int r;
121
122 assert(req);
123 assert(req->link);
124 assert(req->type == REQUEST_TYPE_ROUTE);
125 assert(route);
126
127 link = req->link;
128
129 r = set_ensure_put(&link->dhcp_routes, &route_hash_ops, route);
130 if (r < 0)
131 return log_link_error_errno(link, r, "Failed to store DHCPv4 route: %m");
132
133 set_remove(link->dhcp_routes_old, route);
134
135 return 0;
136 }
137
138 static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
139 int r;
140
141 assert(link);
142 assert(link->dhcp4_messages > 0);
143
144 link->dhcp4_messages--;
145
146 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
147 return 1;
148
149 r = sd_netlink_message_get_errno(m);
150 if (r == -ENETUNREACH && !link->dhcp4_route_retrying) {
151
152 /* It seems kernel does not support that the prefix route cannot be configured with
153 * route table. Let's once drop the config and reconfigure them later. */
154
155 log_link_message_debug_errno(link, m, r, "Could not set DHCPv4 route, retrying later");
156 link->dhcp4_route_failed = true;
157 link->manager->dhcp4_prefix_root_cannot_set_table = true;
158 } else if (r < 0 && r != -EEXIST) {
159 log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 route");
160 link_enter_failed(link);
161 return 1;
162 }
163
164 if (link->dhcp4_messages == 0 && link->dhcp4_route_failed) {
165 link->dhcp4_route_failed = false;
166 link->dhcp4_route_retrying = true;
167
168 r = dhcp4_remove_all(link);
169 if (r < 0)
170 link_enter_failed(link);
171
172 r = link_request_static_nexthops(link, true);
173 if (r < 0)
174 link_enter_failed(link);
175
176 r = link_request_static_routes(link, true);
177 if (r < 0)
178 link_enter_failed(link);
179
180 r = dhcp4_request_address_and_routes(link, false);
181 if (r < 0)
182 link_enter_failed(link);
183
184 return 1;
185 }
186
187 dhcp4_check_ready(link);
188
189 return 1;
190 }
191
192 static int dhcp4_request_route(Route *in, Link *link) {
193 _cleanup_(route_freep) Route *route = in;
194 Request *req;
195 int r;
196
197 assert(route);
198 assert(link);
199
200 r = link_has_route(link, route);
201 if (r < 0)
202 return r;
203 if (r == 0)
204 link->dhcp4_configured = false;
205
206 r = link_request_route(link, TAKE_PTR(route), true, &link->dhcp4_messages,
207 dhcp4_route_handler, &req);
208 if (r <= 0)
209 return r;
210
211 req->after_configure = dhcp4_after_route_configure;
212
213 return 0;
214 }
215
216 static bool link_prefixroute(Link *link) {
217 return !link->network->dhcp_route_table_set ||
218 link->network->dhcp_route_table == RT_TABLE_MAIN ||
219 link->manager->dhcp4_prefix_root_cannot_set_table;
220 }
221
222 static int dhcp4_request_prefix_route(Link *link) {
223 _cleanup_(route_freep) Route *route = NULL;
224 struct in_addr address, netmask;
225 int r;
226
227 assert(link);
228 assert(link->dhcp_lease);
229
230 if (link_prefixroute(link))
231 /* When true, the route will be created by kernel. See dhcp4_update_address(). */
232 return 0;
233
234 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
235 if (r < 0)
236 return r;
237
238 r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
239 if (r < 0)
240 return r;
241
242 r = route_new(&route);
243 if (r < 0)
244 return r;
245
246 route->family = AF_INET;
247 route->dst.in.s_addr = address.s_addr & netmask.s_addr;
248 route->dst_prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
249 route->prefsrc.in = address;
250 route->scope = RT_SCOPE_LINK;
251 route->protocol = RTPROT_DHCP;
252 route->table = link_get_dhcp_route_table(link);
253 route->mtu = link->network->dhcp_route_mtu;
254
255 return dhcp4_request_route(TAKE_PTR(route), link);
256 }
257
258 static int dhcp4_request_route_to_gateway(Link *link, const struct in_addr *gw) {
259 _cleanup_(route_freep) Route *route = NULL;
260 struct in_addr address;
261 int r;
262
263 assert(link);
264 assert(link->dhcp_lease);
265 assert(gw);
266
267 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
268 if (r < 0)
269 return r;
270
271 r = route_new(&route);
272 if (r < 0)
273 return r;
274
275 route->family = AF_INET;
276 route->dst.in = *gw;
277 route->dst_prefixlen = 32;
278 route->prefsrc.in = address;
279 route->scope = RT_SCOPE_LINK;
280 route->protocol = RTPROT_DHCP;
281 route->priority = link->network->dhcp_route_metric;
282 route->table = link_get_dhcp_route_table(link);
283 route->mtu = link->network->dhcp_route_mtu;
284
285 return dhcp4_request_route(TAKE_PTR(route), link);
286 }
287
288 static int dhcp4_request_route_auto(
289 Route *in,
290 Link *link,
291 const struct in_addr *gw) {
292
293 _cleanup_(route_freep) Route *route = in;
294 struct in_addr address, netmask, prefix;
295 uint8_t prefixlen;
296 int r;
297
298 assert(route);
299 assert(link);
300 assert(link->dhcp_lease);
301 assert(gw);
302
303 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
304 if (r < 0)
305 return r;
306
307 r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
308 if (r < 0)
309 return r;
310
311 prefix.s_addr = address.s_addr & netmask.s_addr;
312 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
313
314 if (in4_addr_is_localhost(&route->dst.in)) {
315 if (in4_addr_is_set(gw))
316 log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is localhost, "
317 "ignoring gateway address "IPV4_ADDRESS_FMT_STR,
318 IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw));
319
320 route->scope = RT_SCOPE_HOST;
321 route->gw_family = AF_UNSPEC;
322 route->gw = IN_ADDR_NULL;
323 route->prefsrc = IN_ADDR_NULL;
324
325 } else if (in4_addr_equal(&route->dst.in, &address)) {
326 if (in4_addr_is_set(gw))
327 log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is equivalent to the acquired address, "
328 "ignoring gateway address "IPV4_ADDRESS_FMT_STR,
329 IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw));
330
331 route->scope = RT_SCOPE_HOST;
332 route->gw_family = AF_UNSPEC;
333 route->gw = IN_ADDR_NULL;
334 route->prefsrc.in = address;
335
336 } else if (route->dst_prefixlen >= prefixlen &&
337 (route->dst.in.s_addr & netmask.s_addr) == prefix.s_addr) {
338 if (in4_addr_is_set(gw))
339 log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is in the assigned network "
340 IPV4_ADDRESS_FMT_STR"/%u, ignoring gateway address "IPV4_ADDRESS_FMT_STR,
341 IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen,
342 IPV4_ADDRESS_FMT_VAL(prefix), prefixlen,
343 IPV4_ADDRESS_FMT_VAL(*gw));
344
345 route->scope = RT_SCOPE_LINK;
346 route->gw_family = AF_UNSPEC;
347 route->gw = IN_ADDR_NULL;
348 route->prefsrc.in = address;
349
350 } else {
351 if (in4_addr_is_null(gw)) {
352 log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is not in the assigned network "
353 IPV4_ADDRESS_FMT_STR"/%u, but no gateway is specified, ignoring.",
354 IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen,
355 IPV4_ADDRESS_FMT_VAL(prefix), prefixlen);
356 return 0;
357 }
358
359 r = dhcp4_request_route_to_gateway(link, gw);
360 if (r < 0)
361 return r;
362
363 route->scope = RT_SCOPE_UNIVERSE;
364 route->gw_family = AF_INET;
365 route->gw.in = *gw;
366 route->prefsrc.in = address;
367 }
368
369 return dhcp4_request_route(TAKE_PTR(route), link);
370 }
371
372 static int dhcp4_request_static_routes(Link *link, struct in_addr *ret_default_gw) {
373 _cleanup_free_ sd_dhcp_route **static_routes = NULL;
374 bool classless_route = false, static_route = false;
375 struct in_addr default_gw = {};
376 int n, r;
377
378 assert(link);
379 assert(link->dhcp_lease);
380 assert(ret_default_gw);
381
382 n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes);
383 if (IN_SET(n, 0, -ENODATA)) {
384 log_link_debug(link, "DHCP: No static routes received from DHCP server.");
385 return 0;
386 }
387 if (n < 0)
388 return n;
389
390 for (int i = 0; i < n; i++) {
391 switch (sd_dhcp_route_get_option(static_routes[i])) {
392 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE:
393 classless_route = true;
394 break;
395 case SD_DHCP_OPTION_STATIC_ROUTE:
396 static_route = true;
397 break;
398 }
399 }
400
401 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
402 * the DHCP client MUST ignore the Static Routes option. */
403 if (classless_route && static_route)
404 log_link_debug(link, "Classless static routes received from DHCP server: ignoring static-route option");
405
406 if (!link->network->dhcp_use_routes) {
407 if (!classless_route)
408 return 0;
409
410 /* Even if UseRoutes=no, try to find default gateway to make semi-static routes and
411 * routes to DNS or NTP servers can be configured in later steps. */
412 for (int i = 0; i < n; i++) {
413 struct in_addr dst;
414 uint8_t prefixlen;
415
416 if (sd_dhcp_route_get_option(static_routes[i]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE)
417 continue;
418
419 r = sd_dhcp_route_get_destination(static_routes[i], &dst);
420 if (r < 0)
421 return r;
422
423 if (in4_addr_is_set(&dst))
424 continue;
425
426 r = sd_dhcp_route_get_destination_prefix_length(static_routes[i], &prefixlen);
427 if (r < 0)
428 return r;
429
430 if (prefixlen != 0)
431 continue;
432
433 r = sd_dhcp_route_get_gateway(static_routes[i], ret_default_gw);
434 if (r < 0)
435 return r;
436
437 break;
438 }
439
440 /* Do not return 1 here, to ensure the router option can override the default gateway
441 * that was found. */
442 return 0;
443 }
444
445 for (int i = 0; i < n; i++) {
446 _cleanup_(route_freep) Route *route = NULL;
447 struct in_addr gw;
448
449 if (sd_dhcp_route_get_option(static_routes[i]) !=
450 (classless_route ? SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE : SD_DHCP_OPTION_STATIC_ROUTE))
451 continue;
452
453 r = route_new(&route);
454 if (r < 0)
455 return r;
456
457 route->family = AF_INET;
458 route->gw_family = AF_INET;
459 route->protocol = RTPROT_DHCP;
460 route->priority = link->network->dhcp_route_metric;
461 route->table = link_get_dhcp_route_table(link);
462 route->mtu = link->network->dhcp_route_mtu;
463
464 r = sd_dhcp_route_get_gateway(static_routes[i], &gw);
465 if (r < 0)
466 return r;
467
468 r = sd_dhcp_route_get_destination(static_routes[i], &route->dst.in);
469 if (r < 0)
470 return r;
471
472 r = sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen);
473 if (r < 0)
474 return r;
475
476 /* When classless static routes are provided, then router option will be ignored. To
477 * use the default gateway later in other routes, e.g., routes to dns servers, here we
478 * need to find the default gateway in the classless static routes. */
479 if (classless_route &&
480 in4_addr_is_null(&route->dst.in) && route->dst_prefixlen == 0 &&
481 in4_addr_is_null(&default_gw))
482 default_gw = gw;
483
484 r = dhcp4_request_route_auto(TAKE_PTR(route), link, &gw);
485 if (r < 0)
486 return r;
487 }
488
489 *ret_default_gw = default_gw;
490 return classless_route;
491 }
492
493 static int dhcp4_request_gateway(Link *link, struct in_addr *gw) {
494 _cleanup_(route_freep) Route *route = NULL;
495 const struct in_addr *router;
496 struct in_addr address;
497 int r;
498
499 assert(link);
500 assert(link->dhcp_lease);
501 assert(gw);
502
503 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
504 if (r < 0)
505 return r;
506
507 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
508 if (IN_SET(r, 0, -ENODATA)) {
509 log_link_debug(link, "DHCP: No gateway received from DHCP server.");
510 return 0;
511 }
512 if (r < 0)
513 return r;
514 if (in4_addr_is_null(&router[0])) {
515 log_link_debug(link, "DHCP: Received gateway address is null.");
516 return 0;
517 }
518
519 if (!link->network->dhcp_use_gateway) {
520 /* When no classless static route is provided, even if UseGateway=no, use the gateway
521 * address to configure semi-static routes or routes to DNS or NTP servers. Note, if
522 * neither UseRoutes= nor UseGateway= is disabled, use the default gateway in classless
523 * static routes if provided (in that case, in4_addr_is_null(gw) below is true). */
524 if (in4_addr_is_null(gw))
525 *gw = router[0];
526 return 0;
527 }
528
529 /* The dhcp netmask may mask out the gateway. First, add an explicit route for the gateway host
530 * so that we can route no matter the netmask or existing kernel route tables. */
531 r = dhcp4_request_route_to_gateway(link, &router[0]);
532 if (r < 0)
533 return r;
534
535 r = route_new(&route);
536 if (r < 0)
537 return r;
538
539 /* Next, add a default gateway. */
540 route->family = AF_INET;
541 route->gw_family = AF_INET;
542 route->gw.in = router[0];
543 route->prefsrc.in = address;
544 route->protocol = RTPROT_DHCP;
545 route->priority = link->network->dhcp_route_metric;
546 route->table = link_get_dhcp_route_table(link);
547 route->mtu = link->network->dhcp_route_mtu;
548
549 r = dhcp4_request_route(TAKE_PTR(route), link);
550 if (r < 0)
551 return r;
552
553 /* When no classless static route is provided, or UseRoutes=no, then use the router address to
554 * configure semi-static routes and routes to DNS or NTP servers in later steps. */
555 *gw = router[0];
556 return 0;
557 }
558
559 static int dhcp4_request_semi_static_routes(Link *link, const struct in_addr *gw) {
560 Route *rt;
561 int r;
562
563 assert(link);
564 assert(link->dhcp_lease);
565 assert(link->network);
566 assert(gw);
567
568 if (in4_addr_is_null(gw))
569 return 0;
570
571 HASHMAP_FOREACH(rt, link->network->routes_by_section) {
572 _cleanup_(route_freep) Route *route = NULL;
573
574 if (!rt->gateway_from_dhcp_or_ra)
575 continue;
576
577 if (rt->gw_family != AF_INET)
578 continue;
579
580 r = dhcp4_request_route_to_gateway(link, gw);
581 if (r < 0)
582 return r;
583
584 r = route_dup(rt, &route);
585 if (r < 0)
586 return r;
587
588 route->gw.in = *gw;
589 if (!route->protocol_set)
590 route->protocol = RTPROT_DHCP;
591 if (!route->priority_set)
592 route->priority = link->network->dhcp_route_metric;
593 if (!route->table_set)
594 route->table = link_get_dhcp_route_table(link);
595 if (route->mtu == 0)
596 route->mtu = link->network->dhcp_route_mtu;
597
598 r = dhcp4_request_route(TAKE_PTR(route), link);
599 if (r < 0)
600 return r;
601 }
602
603 return 0;
604 }
605
606 static int dhcp4_request_routes_to_servers(
607 Link *link,
608 const struct in_addr *servers,
609 size_t n_servers,
610 const struct in_addr *gw) {
611
612 int r;
613
614 assert(link);
615 assert(link->dhcp_lease);
616 assert(link->network);
617 assert(servers || n_servers == 0);
618 assert(gw);
619
620 for (size_t i = 0; i < n_servers; i++) {
621 _cleanup_(route_freep) Route *route = NULL;
622
623 if (in4_addr_is_null(&servers[i]))
624 continue;
625
626 r = route_new(&route);
627 if (r < 0)
628 return r;
629
630 route->family = AF_INET;
631 route->dst.in = servers[i];
632 route->dst_prefixlen = 32;
633 route->protocol = RTPROT_DHCP;
634 route->priority = link->network->dhcp_route_metric;
635 route->table = link_get_dhcp_route_table(link);
636 route->mtu = link->network->dhcp_route_mtu;
637
638 r = dhcp4_request_route_auto(TAKE_PTR(route), link, gw);
639 if (r < 0)
640 return r;
641 }
642
643 return 0;
644 }
645
646 static int dhcp4_request_routes_to_dns(Link *link, const struct in_addr *gw) {
647 const struct in_addr *dns;
648 int r;
649
650 assert(link);
651 assert(link->dhcp_lease);
652 assert(link->network);
653 assert(gw);
654
655 if (!link->network->dhcp_use_dns ||
656 !link->network->dhcp_routes_to_dns)
657 return 0;
658
659 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns);
660 if (IN_SET(r, 0, -ENODATA))
661 return 0;
662 if (r < 0)
663 return r;
664
665 return dhcp4_request_routes_to_servers(link, dns, r, gw);
666 }
667
668 static int dhcp4_request_routes_to_ntp(Link *link, const struct in_addr *gw) {
669 const struct in_addr *ntp;
670 int r;
671
672 assert(link);
673 assert(link->dhcp_lease);
674 assert(link->network);
675 assert(gw);
676
677 if (!link->network->dhcp_use_ntp ||
678 !link->network->dhcp_routes_to_ntp)
679 return 0;
680
681 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &ntp);
682 if (IN_SET(r, 0, -ENODATA))
683 return 0;
684 if (r < 0)
685 return r;
686
687 return dhcp4_request_routes_to_servers(link, ntp, r, gw);
688 }
689
690 static int dhcp4_request_routes(Link *link) {
691 struct in_addr gw = {};
692 Route *rt;
693 int r;
694
695 assert(link);
696
697 if (!link->dhcp_lease)
698 return 0;
699
700 while ((rt = set_steal_first(link->dhcp_routes))) {
701 r = set_ensure_put(&link->dhcp_routes_old, &route_hash_ops, rt);
702 if (r < 0)
703 return log_link_error_errno(link, r, "Failed to store old DHCPv4 route: %m");
704 }
705
706 r = dhcp4_request_prefix_route(link);
707 if (r < 0)
708 return log_link_error_errno(link, r, "DHCP error: Could not request prefix route: %m");
709
710 r = dhcp4_request_static_routes(link, &gw);
711 if (r < 0)
712 return log_link_error_errno(link, r, "DHCP error: Could not request static routes: %m");
713 if (r == 0) {
714 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
715 * a Router option, the DHCP client MUST ignore the Router option. */
716 r = dhcp4_request_gateway(link, &gw);
717 if (r < 0)
718 return log_link_error_errno(link, r, "DHCP error: Could not request gateway: %m");
719 }
720
721 r = dhcp4_request_semi_static_routes(link, &gw);
722 if (r < 0)
723 return log_link_error_errno(link, r, "DHCP error: Could not request routes with Gateway=_dhcp4 setting: %m");
724
725 r = dhcp4_request_routes_to_dns(link, &gw);
726 if (r < 0)
727 return log_link_error_errno(link, r, "DHCP error: Could not request routes to DNS servers: %m");
728
729 r = dhcp4_request_routes_to_ntp(link, &gw);
730 if (r < 0)
731 return log_link_error_errno(link, r, "DHCP error: Could not request routes to NTP servers: %m");
732
733 return 0;
734 }
735
736 static int dhcp_reset_mtu(Link *link) {
737 uint16_t mtu;
738 int r;
739
740 assert(link);
741
742 if (!link->network->dhcp_use_mtu)
743 return 0;
744
745 r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
746 if (r == -ENODATA)
747 return 0;
748 if (r < 0)
749 return log_link_error_errno(link, r, "DHCP error: failed to get MTU from lease: %m");
750
751 if (link->original_mtu == mtu)
752 return 0;
753
754 r = link_request_to_set_mtu(link, link->original_mtu);
755 if (r < 0)
756 return log_link_error_errno(link, r, "DHCP error: could not reset MTU: %m");
757
758 return 0;
759 }
760
761 static int dhcp_reset_hostname(Link *link) {
762 const char *hostname;
763 int r;
764
765 assert(link);
766
767 if (!link->network->dhcp_use_hostname)
768 return 0;
769
770 hostname = link->network->dhcp_hostname;
771 if (!hostname)
772 (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
773
774 if (!hostname)
775 return 0;
776
777 /* If a hostname was set due to the lease, then unset it now. */
778 r = manager_set_hostname(link->manager, NULL);
779 if (r < 0)
780 return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m");
781
782 return 0;
783 }
784
785 static int dhcp4_remove_all(Link *link) {
786 Route *route;
787 int k, r = 0;
788
789 assert(link);
790
791 SET_FOREACH(route, link->dhcp_routes) {
792 k = route_remove(route, NULL, link);
793 if (k < 0)
794 r = k;
795 }
796
797 if (link->dhcp_address) {
798 k = address_remove(link->dhcp_address, link);
799 if (k < 0)
800 r = k;
801 }
802
803 return r;
804 }
805
806 int dhcp4_lease_lost(Link *link) {
807 int k, r = 0;
808
809 assert(link);
810 assert(link->dhcp_lease);
811
812 log_link_info(link, "DHCP lease lost");
813
814 link->dhcp4_configured = false;
815
816 /* dhcp4_lease_lost() may be called during renewing IP address. */
817 k = dhcp4_release_old_lease(link);
818 if (k < 0)
819 r = k;
820
821 k = dhcp4_remove_all(link);
822 if (k < 0)
823 r = k;
824
825 k = dhcp_reset_mtu(link);
826 if (k < 0)
827 r = k;
828
829 k = dhcp_reset_hostname(link);
830 if (k < 0)
831 r = k;
832
833 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
834 link_dirty(link);
835
836 if (link->network->dhcp_send_decline) {
837 Address *a;
838
839 /* The acquired address may be still ARP probing and not configured. */
840
841 SET_FOREACH(a, link->addresses_ipv4acd)
842 if (!a->is_static && address_get(link, a, NULL) < 0) {
843 Request req = {
844 .link = link,
845 .address = a,
846 };
847
848 log_link_debug(link, "Canceling the request to configure DHCPv4 address "IPV4_ADDRESS_FMT_STR,
849 IPV4_ADDRESS_FMT_VAL(a->in_addr.in));
850 request_drop(ordered_set_get(link->manager->request_queue, &req));
851 }
852 }
853
854 if (r < 0)
855 return r;
856
857 r = link_request_static_nexthops(link, true);
858 if (r < 0)
859 return r;
860
861 return link_request_static_routes(link, true);
862 }
863
864 static int dhcp4_address_ready_callback(Address *address) {
865 assert(address);
866
867 /* Do not call this again. */
868 address->callback = NULL;
869
870 dhcp4_check_ready(address->link);
871 return 0;
872 }
873
874 static int dhcp4_after_address_configure(Request *req, void *object) {
875 Address *address = object;
876 Link *link;
877 int r;
878
879 assert(req);
880 assert(req->link);
881 assert(req->type == REQUEST_TYPE_ADDRESS);
882 assert(address);
883
884 link = req->link;
885
886 if (!address_equal(link->dhcp_address, address)) {
887 if (link->dhcp_address_old &&
888 !address_equal(link->dhcp_address_old, link->dhcp_address)) {
889 /* Still too old address exists? Let's remove it immediately. */
890 r = address_remove(link->dhcp_address_old, link);
891 if (r < 0)
892 return r;
893 }
894 link->dhcp_address_old = link->dhcp_address;
895 }
896
897 link->dhcp_address = address;
898 return 0;
899 }
900
901 static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
902 int r;
903
904 assert(link);
905 assert(link->dhcp4_messages > 0);
906
907 link->dhcp4_messages--;
908
909 r = address_configure_handler_internal(rtnl, m, link, "Could not set DHCPv4 address");
910 if (r <= 0)
911 return r;
912
913 if (address_is_ready(link->dhcp_address)) {
914 r = dhcp4_address_ready_callback(link->dhcp_address);
915 if (r < 0)
916 link_enter_failed(link);
917 } else
918 link->dhcp_address->callback = dhcp4_address_ready_callback;
919
920 return 1;
921 }
922
923 static int dhcp4_request_address(Link *link, bool announce) {
924 _cleanup_(address_freep) Address *addr = NULL;
925 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
926 struct in_addr address, netmask;
927 unsigned prefixlen;
928 Request *req;
929 int r;
930
931 assert(link);
932 assert(link->network);
933
934 if (!link->dhcp_lease)
935 return 0;
936
937 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
938 if (r < 0)
939 return log_link_warning_errno(link, r, "DHCP error: no address: %m");
940
941 r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
942 if (r < 0)
943 return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
944
945 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
946 r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
947 if (r < 0)
948 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
949 }
950
951 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
952
953 if (announce) {
954 const struct in_addr *router;
955
956 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
957 if (r < 0 && r != -ENODATA)
958 return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
959
960 if (r > 0 && in4_addr_is_set(&router[0]))
961 log_struct(LOG_INFO,
962 LOG_LINK_INTERFACE(link),
963 LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u via "IPV4_ADDRESS_FMT_STR,
964 IPV4_ADDRESS_FMT_VAL(address),
965 prefixlen,
966 IPV4_ADDRESS_FMT_VAL(router[0])),
967 "ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address),
968 "PREFIXLEN=%u", prefixlen,
969 "GATEWAY="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(router[0]));
970 else
971 log_struct(LOG_INFO,
972 LOG_LINK_INTERFACE(link),
973 LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u",
974 IPV4_ADDRESS_FMT_VAL(address),
975 prefixlen),
976 "ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address),
977 "PREFIXLEN=%u", prefixlen);
978 }
979
980 r = address_new(&addr);
981 if (r < 0)
982 return log_oom();
983
984 addr->family = AF_INET;
985 addr->in_addr.in.s_addr = address.s_addr;
986 addr->cinfo.ifa_prefered = lifetime;
987 addr->cinfo.ifa_valid = lifetime;
988 addr->prefixlen = prefixlen;
989 if (prefixlen <= 30)
990 addr->broadcast.s_addr = address.s_addr | ~netmask.s_addr;
991 SET_FLAG(addr->flags, IFA_F_NOPREFIXROUTE, !link_prefixroute(link));
992 addr->route_metric = link->network->dhcp_route_metric;
993 addr->duplicate_address_detection = link->network->dhcp_send_decline ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_NO;
994
995 if (address_get(link, addr, NULL) < 0)
996 link->dhcp4_configured = false;
997
998 r = link_request_address(link, TAKE_PTR(addr), true, &link->dhcp4_messages,
999 dhcp4_address_handler, &req);
1000 if (r < 0)
1001 return log_link_error_errno(link, r, "Failed to request DHCPv4 address: %m");
1002 if (r == 0)
1003 return 0;
1004
1005 req->after_configure = dhcp4_after_address_configure;
1006
1007 return 0;
1008 }
1009
1010 static int dhcp4_request_address_and_routes(Link *link, bool announce) {
1011 int r;
1012
1013 assert(link);
1014
1015 r = dhcp4_request_address(link, announce);
1016 if (r < 0)
1017 return r;
1018
1019 r = dhcp4_request_routes(link);
1020 if (r < 0)
1021 return r;
1022
1023 link_set_state(link, LINK_STATE_CONFIGURING);
1024 link_check_ready(link);
1025
1026 return 0;
1027 }
1028
1029 static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
1030 sd_dhcp_lease *lease;
1031 int r;
1032
1033 assert(link);
1034 assert(client);
1035
1036 r = sd_dhcp_client_get_lease(client, &lease);
1037 if (r < 0)
1038 return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
1039
1040 sd_dhcp_lease_unref(link->dhcp_lease);
1041 link->dhcp_lease = sd_dhcp_lease_ref(lease);
1042 link_dirty(link);
1043
1044 return dhcp4_request_address_and_routes(link, false);
1045 }
1046
1047 static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
1048 sd_dhcp_lease *lease;
1049 int r;
1050
1051 assert(client);
1052 assert(link);
1053
1054 r = sd_dhcp_client_get_lease(client, &lease);
1055 if (r < 0)
1056 return log_link_error_errno(link, r, "DHCP error: No lease: %m");
1057
1058 sd_dhcp_lease_unref(link->dhcp_lease);
1059 link->dhcp_lease = sd_dhcp_lease_ref(lease);
1060 link_dirty(link);
1061
1062 if (link->network->dhcp_use_mtu) {
1063 uint16_t mtu;
1064
1065 r = sd_dhcp_lease_get_mtu(lease, &mtu);
1066 if (r >= 0) {
1067 r = link_request_to_set_mtu(link, mtu);
1068 if (r < 0)
1069 log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu);
1070 }
1071 }
1072
1073 if (link->network->dhcp_use_hostname) {
1074 const char *dhcpname = NULL;
1075 _cleanup_free_ char *hostname = NULL;
1076
1077 if (link->network->dhcp_hostname)
1078 dhcpname = link->network->dhcp_hostname;
1079 else
1080 (void) sd_dhcp_lease_get_hostname(lease, &dhcpname);
1081
1082 if (dhcpname) {
1083 r = shorten_overlong(dhcpname, &hostname);
1084 if (r < 0)
1085 log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
1086 if (r == 1)
1087 log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
1088 }
1089
1090 if (hostname) {
1091 r = manager_set_hostname(link->manager, hostname);
1092 if (r < 0)
1093 log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
1094 }
1095 }
1096
1097 if (link->network->dhcp_use_timezone) {
1098 const char *tz = NULL;
1099
1100 (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
1101
1102 if (tz) {
1103 r = manager_set_timezone(link->manager, tz);
1104 if (r < 0)
1105 log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz);
1106 }
1107 }
1108
1109 return dhcp4_request_address_and_routes(link, true);
1110 }
1111
1112 static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) {
1113 int r;
1114
1115 r = dhcp_lease_acquired(client, link);
1116 if (r < 0)
1117 (void) dhcp4_lease_lost(link);
1118
1119 return r;
1120 }
1121
1122 static int dhcp_server_is_deny_listed(Link *link, sd_dhcp_client *client) {
1123 sd_dhcp_lease *lease;
1124 struct in_addr addr;
1125 int r;
1126
1127 assert(link);
1128 assert(link->network);
1129 assert(client);
1130
1131 r = sd_dhcp_client_get_lease(client, &lease);
1132 if (r < 0)
1133 return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
1134
1135 r = sd_dhcp_lease_get_server_identifier(lease, &addr);
1136 if (r < 0)
1137 return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
1138
1139 if (set_contains(link->network->dhcp_deny_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
1140 log_struct(LOG_DEBUG,
1141 LOG_LINK_INTERFACE(link),
1142 LOG_LINK_MESSAGE(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" found in deny-list, ignoring offer",
1143 IPV4_ADDRESS_FMT_VAL(addr)));
1144 return true;
1145 }
1146
1147 return false;
1148 }
1149
1150 static int dhcp_server_is_allow_listed(Link *link, sd_dhcp_client *client) {
1151 sd_dhcp_lease *lease;
1152 struct in_addr addr;
1153 int r;
1154
1155 assert(link);
1156 assert(link->network);
1157 assert(client);
1158
1159 r = sd_dhcp_client_get_lease(client, &lease);
1160 if (r < 0)
1161 return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
1162
1163 r = sd_dhcp_lease_get_server_identifier(lease, &addr);
1164 if (r < 0)
1165 return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
1166
1167 if (set_contains(link->network->dhcp_allow_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
1168 log_struct(LOG_DEBUG,
1169 LOG_LINK_INTERFACE(link),
1170 LOG_LINK_MESSAGE(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" found in allow-list, accepting offer",
1171 IPV4_ADDRESS_FMT_VAL(addr)));
1172 return true;
1173 }
1174
1175 return false;
1176 }
1177
1178 static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
1179 Link *link = userdata;
1180 int r;
1181
1182 assert(link);
1183 assert(link->network);
1184 assert(link->manager);
1185
1186 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1187 return 0;
1188
1189 switch (event) {
1190 case SD_DHCP_CLIENT_EVENT_STOP:
1191 if (link->ipv4ll) {
1192 log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address");
1193
1194 r = sd_ipv4ll_start(link->ipv4ll);
1195 if (r < 0)
1196 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
1197 }
1198
1199 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
1200 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1201 return 0;
1202 }
1203
1204 if (link->dhcp_lease) {
1205 if (link->network->dhcp_send_release) {
1206 r = sd_dhcp_client_send_release(client);
1207 if (r < 0)
1208 log_link_full_errno(link,
1209 ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_WARNING,
1210 r, "Failed to send DHCP RELEASE, ignoring: %m");
1211 }
1212
1213 r = dhcp4_lease_lost(link);
1214 if (r < 0) {
1215 link_enter_failed(link);
1216 return r;
1217 }
1218 }
1219
1220 break;
1221 case SD_DHCP_CLIENT_EVENT_EXPIRED:
1222 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
1223 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1224 return 0;
1225 }
1226
1227 if (link->dhcp_lease) {
1228 r = dhcp4_lease_lost(link);
1229 if (r < 0) {
1230 link_enter_failed(link);
1231 return r;
1232 }
1233 }
1234
1235 break;
1236 case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
1237 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
1238 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1239 return 0;
1240 }
1241
1242 r = dhcp_lease_ip_change(client, link);
1243 if (r < 0) {
1244 link_enter_failed(link);
1245 return r;
1246 }
1247
1248 break;
1249 case SD_DHCP_CLIENT_EVENT_RENEW:
1250 r = dhcp_lease_renew(client, link);
1251 if (r < 0) {
1252 link_enter_failed(link);
1253 return r;
1254 }
1255 break;
1256 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
1257 r = dhcp_lease_acquired(client, link);
1258 if (r < 0) {
1259 link_enter_failed(link);
1260 return r;
1261 }
1262 break;
1263 case SD_DHCP_CLIENT_EVENT_SELECTING:
1264 if (!set_isempty(link->network->dhcp_allow_listed_ip)) {
1265 r = dhcp_server_is_allow_listed(link, client);
1266 if (r < 0)
1267 return r;
1268 if (r == 0)
1269 return -ENOMSG;
1270 } else {
1271 r = dhcp_server_is_deny_listed(link, client);
1272 if (r < 0)
1273 return r;
1274 if (r != 0)
1275 return -ENOMSG;
1276 }
1277 break;
1278
1279 case SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE:
1280 if (link->ipv4ll && !sd_ipv4ll_is_running(link->ipv4ll)) {
1281 log_link_debug(link, "Problems acquiring DHCP lease, acquiring IPv4 link-local address");
1282
1283 r = sd_ipv4ll_start(link->ipv4ll);
1284 if (r < 0)
1285 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
1286 }
1287 break;
1288
1289 default:
1290 if (event < 0)
1291 log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
1292 else
1293 log_link_warning(link, "DHCP unknown event: %i", event);
1294 break;
1295 }
1296
1297 return 0;
1298 }
1299
1300 static int dhcp4_set_hostname(Link *link) {
1301 _cleanup_free_ char *hostname = NULL;
1302 const char *hn;
1303 int r;
1304
1305 assert(link);
1306
1307 if (!link->network->dhcp_send_hostname)
1308 hn = NULL;
1309 else if (link->network->dhcp_hostname)
1310 hn = link->network->dhcp_hostname;
1311 else {
1312 r = gethostname_strict(&hostname);
1313 if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
1314 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to get hostname: %m");
1315
1316 hn = hostname;
1317 }
1318
1319 r = sd_dhcp_client_set_hostname(link->dhcp_client, hn);
1320 if (r == -EINVAL && hostname)
1321 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1322 log_link_debug_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1323 else if (r < 0)
1324 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m");
1325
1326 return 0;
1327 }
1328
1329 static int dhcp4_set_client_identifier(Link *link) {
1330 int r;
1331
1332 assert(link);
1333 assert(link->network);
1334 assert(link->dhcp_client);
1335
1336 switch (link->network->dhcp_client_identifier) {
1337 case DHCP_CLIENT_ID_DUID: {
1338 /* If configured, apply user specified DUID and IAID */
1339 const DUID *duid = link_get_dhcp4_duid(link);
1340
1341 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1342 r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
1343 link->network->dhcp_iaid_set,
1344 link->network->dhcp_iaid,
1345 duid->llt_time);
1346 else
1347 r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
1348 link->network->dhcp_iaid_set,
1349 link->network->dhcp_iaid,
1350 duid->type,
1351 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1352 duid->raw_data_len);
1353 if (r < 0)
1354 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1355 break;
1356 }
1357 case DHCP_CLIENT_ID_DUID_ONLY: {
1358 /* If configured, apply user specified DUID */
1359 const DUID *duid = link_get_dhcp4_duid(link);
1360
1361 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1362 r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
1363 duid->llt_time);
1364 else
1365 r = sd_dhcp_client_set_duid(link->dhcp_client,
1366 duid->type,
1367 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1368 duid->raw_data_len);
1369 if (r < 0)
1370 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
1371 break;
1372 }
1373 case DHCP_CLIENT_ID_MAC: {
1374 const uint8_t *hw_addr = link->hw_addr.bytes;
1375 size_t hw_addr_len = link->hw_addr.length;
1376
1377 if (link->iftype == ARPHRD_INFINIBAND && hw_addr_len == INFINIBAND_ALEN) {
1378 /* set_client_id expects only last 8 bytes of an IB address */
1379 hw_addr += INFINIBAND_ALEN - 8;
1380 hw_addr_len -= INFINIBAND_ALEN - 8;
1381 }
1382
1383 r = sd_dhcp_client_set_client_id(link->dhcp_client,
1384 link->iftype,
1385 hw_addr,
1386 hw_addr_len);
1387 if (r < 0)
1388 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m");
1389 break;
1390 }
1391 default:
1392 assert_not_reached();
1393 }
1394
1395 return 0;
1396 }
1397
1398 static int dhcp4_configure_duid(Link *link) {
1399 assert(link);
1400
1401 if (!IN_SET(link->network->dhcp_client_identifier, DHCP_CLIENT_ID_DUID, DHCP_CLIENT_ID_DUID_ONLY))
1402 return 1;
1403
1404 return dhcp_configure_duid(link, link_get_dhcp4_duid(link));
1405 }
1406
1407 static int dhcp4_set_request_address(Link *link) {
1408 Address *a;
1409
1410 assert(link);
1411 assert(link->network);
1412 assert(link->dhcp_client);
1413
1414 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
1415 return 0;
1416
1417 SET_FOREACH(a, link->addresses_foreign) {
1418 if (a->family != AF_INET)
1419 continue;
1420 if (link_address_is_dynamic(link, a))
1421 break;
1422 }
1423
1424 if (!a)
1425 return 0;
1426
1427 log_link_debug(link, "DHCP4 CLIENT: requesting " IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(a->in_addr.in));
1428
1429 return sd_dhcp_client_set_request_address(link->dhcp_client, &a->in_addr.in);
1430 }
1431
1432 static bool link_needs_dhcp_broadcast(Link *link) {
1433 const char *val;
1434 int r;
1435
1436 assert(link);
1437 assert(link->network);
1438
1439 /* Return the setting in DHCP[4].RequestBroadcast if specified. Otherwise return the device property
1440 * ID_NET_DHCP_BROADCAST setting, which may be set for interfaces requiring that the DHCPOFFER message
1441 * is being broadcast because they can't handle unicast messages while not fully configured.
1442 * If neither is set or a failure occurs, return false, which is the default for this flag.
1443 */
1444 r = link->network->dhcp_broadcast;
1445 if (r < 0 && link->sd_device && sd_device_get_property_value(link->sd_device, "ID_NET_DHCP_BROADCAST", &val) >= 0) {
1446 r = parse_boolean(val);
1447 if (r < 0)
1448 log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to parse ID_NET_DHCP_BROADCAST, ignoring: %m");
1449 else
1450 log_link_debug(link, "DHCP4 CLIENT: Detected ID_NET_DHCP_BROADCAST='%d'.", r);
1451
1452 }
1453 return r == true;
1454 }
1455
1456 int dhcp4_configure(Link *link) {
1457 sd_dhcp_option *send_option;
1458 void *request_options;
1459 int r;
1460
1461 assert(link);
1462 assert(link->network);
1463
1464 if (!link_dhcp4_enabled(link))
1465 return 0;
1466
1467 if (link->dhcp_client)
1468 return -EBUSY; /* Already configured. */
1469
1470 r = dhcp4_configure_duid(link);
1471 if (r <= 0)
1472 return r;
1473
1474 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
1475 if (r < 0)
1476 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to allocate DHCP4 client: %m");
1477
1478 r = sd_dhcp_client_attach_event(link->dhcp_client, link->manager->event, 0);
1479 if (r < 0)
1480 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to attach event to DHCP4 client: %m");
1481
1482 r = sd_dhcp_client_set_mac(link->dhcp_client,
1483 link->hw_addr.bytes,
1484 link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL,
1485 link->hw_addr.length, link->iftype);
1486 if (r < 0)
1487 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m");
1488
1489 r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
1490 if (r < 0)
1491 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m");
1492
1493 r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
1494 if (r < 0)
1495 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m");
1496
1497 r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, link_needs_dhcp_broadcast(link));
1498 if (r < 0)
1499 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1500
1501 if (link->mtu > 0) {
1502 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
1503 if (r < 0)
1504 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m");
1505 }
1506
1507 if (!link->network->dhcp_anonymize) {
1508 if (link->network->dhcp_use_mtu) {
1509 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_INTERFACE_MTU);
1510 if (r < 0)
1511 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1512 }
1513
1514 if (link->network->dhcp_use_routes) {
1515 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_STATIC_ROUTE);
1516 if (r < 0)
1517 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1518
1519 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
1520 if (r < 0)
1521 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1522 }
1523
1524 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
1525 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST);
1526 if (r < 0)
1527 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1528 }
1529
1530 if (link->network->dhcp_use_ntp) {
1531 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
1532 if (r < 0)
1533 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1534 }
1535
1536 if (link->network->dhcp_use_sip) {
1537 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_SIP_SERVER);
1538 if (r < 0)
1539 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1540 }
1541
1542 if (link->network->dhcp_use_timezone) {
1543 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
1544 if (r < 0)
1545 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1546 }
1547
1548 SET_FOREACH(request_options, link->network->dhcp_request_options) {
1549 uint32_t option = PTR_TO_UINT32(request_options);
1550
1551 r = sd_dhcp_client_set_request_option(link->dhcp_client, option);
1552 if (r < 0)
1553 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option);
1554 }
1555
1556 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_options) {
1557 r = sd_dhcp_client_add_option(link->dhcp_client, send_option);
1558 if (r == -EEXIST)
1559 continue;
1560 if (r < 0)
1561 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
1562 }
1563
1564 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_vendor_options) {
1565 r = sd_dhcp_client_add_vendor_option(link->dhcp_client, send_option);
1566 if (r == -EEXIST)
1567 continue;
1568 if (r < 0)
1569 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
1570 }
1571
1572 r = dhcp4_set_hostname(link);
1573 if (r < 0)
1574 return r;
1575
1576 if (link->network->dhcp_vendor_class_identifier) {
1577 r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
1578 link->network->dhcp_vendor_class_identifier);
1579 if (r < 0)
1580 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1581 }
1582
1583 if (link->network->dhcp_mudurl) {
1584 r = sd_dhcp_client_set_mud_url(link->dhcp_client, link->network->dhcp_mudurl);
1585 if (r < 0)
1586 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1587 }
1588
1589 if (link->network->dhcp_user_class) {
1590 r = sd_dhcp_client_set_user_class(link->dhcp_client, link->network->dhcp_user_class);
1591 if (r < 0)
1592 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m");
1593 }
1594 }
1595
1596 if (link->network->dhcp_client_port > 0) {
1597 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
1598 if (r < 0)
1599 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
1600 }
1601
1602 if (link->network->dhcp_max_attempts > 0) {
1603 r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts);
1604 if (r < 0)
1605 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m");
1606 }
1607
1608 if (link->network->dhcp_ip_service_type > 0) {
1609 r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->dhcp_ip_service_type);
1610 if (r < 0)
1611 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set IP service type: %m");
1612 }
1613
1614 if (link->network->dhcp_fallback_lease_lifetime > 0) {
1615 r = sd_dhcp_client_set_fallback_lease_lifetime(link->dhcp_client, link->network->dhcp_fallback_lease_lifetime);
1616 if (r < 0)
1617 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1618 }
1619
1620 r = dhcp4_set_request_address(link);
1621 if (r < 0)
1622 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set initial DHCPv4 address: %m");
1623
1624 return dhcp4_set_client_identifier(link);
1625 }
1626
1627 int dhcp4_update_mac(Link *link) {
1628 int r;
1629
1630 assert(link);
1631
1632 if (!link->dhcp_client)
1633 return 0;
1634
1635 r = sd_dhcp_client_set_mac(link->dhcp_client, link->hw_addr.bytes,
1636 link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL,
1637 link->hw_addr.length, link->iftype);
1638 if (r < 0)
1639 return r;
1640
1641 return dhcp4_set_client_identifier(link);
1642 }
1643
1644 int dhcp4_start(Link *link) {
1645 assert(link);
1646
1647 if (!link->dhcp_client)
1648 return 0;
1649
1650 if (sd_dhcp_client_is_running(link->dhcp_client) > 0)
1651 return 0;
1652
1653 log_link_debug(link, "Acquiring DHCPv4 lease");
1654
1655 return sd_dhcp_client_start(link->dhcp_client);
1656 }
1657
1658 int config_parse_dhcp_max_attempts(
1659 const char *unit,
1660 const char *filename,
1661 unsigned line,
1662 const char *section,
1663 unsigned section_line,
1664 const char *lvalue,
1665 int ltype,
1666 const char *rvalue,
1667 void *data,
1668 void *userdata) {
1669
1670 Network *network = data;
1671 uint64_t a;
1672 int r;
1673
1674 assert(network);
1675 assert(lvalue);
1676 assert(rvalue);
1677
1678 if (isempty(rvalue)) {
1679 network->dhcp_max_attempts = 0;
1680 return 0;
1681 }
1682
1683 if (streq(rvalue, "infinity")) {
1684 network->dhcp_max_attempts = UINT64_MAX;
1685 return 0;
1686 }
1687
1688 r = safe_atou64(rvalue, &a);
1689 if (r < 0) {
1690 log_syntax(unit, LOG_WARNING, filename, line, r,
1691 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue);
1692 return 0;
1693 }
1694
1695 if (a == 0) {
1696 log_syntax(unit, LOG_WARNING, filename, line, 0,
1697 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue);
1698 return 0;
1699 }
1700
1701 network->dhcp_max_attempts = a;
1702
1703 return 0;
1704 }
1705
1706 int config_parse_dhcp_acl_ip_address(
1707 const char *unit,
1708 const char *filename,
1709 unsigned line,
1710 const char *section,
1711 unsigned section_line,
1712 const char *lvalue,
1713 int ltype,
1714 const char *rvalue,
1715 void *data,
1716 void *userdata) {
1717
1718 Network *network = data;
1719 Set **acl;
1720 int r;
1721
1722 assert(filename);
1723 assert(lvalue);
1724 assert(rvalue);
1725 assert(data);
1726
1727 acl = STR_IN_SET(lvalue, "DenyList", "BlackList") ? &network->dhcp_deny_listed_ip : &network->dhcp_allow_listed_ip;
1728
1729 if (isempty(rvalue)) {
1730 *acl = set_free(*acl);
1731 return 0;
1732 }
1733
1734 for (const char *p = rvalue;;) {
1735 _cleanup_free_ char *n = NULL;
1736 union in_addr_union ip;
1737
1738 r = extract_first_word(&p, &n, NULL, 0);
1739 if (r == -ENOMEM)
1740 return log_oom();
1741 if (r < 0) {
1742 log_syntax(unit, LOG_WARNING, filename, line, r,
1743 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1744 lvalue, rvalue);
1745 return 0;
1746 }
1747 if (r == 0)
1748 return 0;
1749
1750 r = in_addr_from_string(AF_INET, n, &ip);
1751 if (r < 0) {
1752 log_syntax(unit, LOG_WARNING, filename, line, r,
1753 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue, n);
1754 continue;
1755 }
1756
1757 r = set_ensure_put(acl, NULL, UINT32_TO_PTR(ip.in.s_addr));
1758 if (r < 0)
1759 log_syntax(unit, LOG_WARNING, filename, line, r,
1760 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue, n);
1761 }
1762 }
1763
1764 int config_parse_dhcp_ip_service_type(
1765 const char *unit,
1766 const char *filename,
1767 unsigned line,
1768 const char *section,
1769 unsigned section_line,
1770 const char *lvalue,
1771 int ltype,
1772 const char *rvalue,
1773 void *data,
1774 void *userdata) {
1775
1776 assert(filename);
1777 assert(lvalue);
1778 assert(rvalue);
1779
1780 if (streq(rvalue, "CS4"))
1781 *((int *)data) = IPTOS_CLASS_CS4;
1782 else if (streq(rvalue, "CS6"))
1783 *((int *)data) = IPTOS_CLASS_CS6;
1784 else
1785 log_syntax(unit, LOG_WARNING, filename, line, 0,
1786 "Failed to parse IPServiceType type '%s', ignoring.", rvalue);
1787
1788 return 0;
1789 }
1790
1791 int config_parse_dhcp_fallback_lease_lifetime(const char *unit,
1792 const char *filename,
1793 unsigned line,
1794 const char *section,
1795 unsigned section_line,
1796 const char *lvalue,
1797 int ltype,
1798 const char *rvalue,
1799 void *data,
1800 void *userdata) {
1801 Network *network = userdata;
1802 uint32_t k;
1803
1804 assert(filename);
1805 assert(section);
1806 assert(lvalue);
1807 assert(rvalue);
1808 assert(data);
1809
1810 if (isempty(rvalue)) {
1811 network->dhcp_fallback_lease_lifetime = 0;
1812 return 0;
1813 }
1814
1815 /* We accept only "forever" or "infinity". */
1816 if (STR_IN_SET(rvalue, "forever", "infinity"))
1817 k = CACHE_INFO_INFINITY_LIFE_TIME;
1818 else {
1819 log_syntax(unit, LOG_WARNING, filename, line, 0,
1820 "Invalid LeaseLifetime= value, ignoring: %s", rvalue);
1821 return 0;
1822 }
1823
1824 network->dhcp_fallback_lease_lifetime = k;
1825
1826 return 0;
1827 }
1828
1829 static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = {
1830 [DHCP_CLIENT_ID_MAC] = "mac",
1831 [DHCP_CLIENT_ID_DUID] = "duid",
1832 [DHCP_CLIENT_ID_DUID_ONLY] = "duid-only",
1833 };
1834
1835 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier);
1836 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier,
1837 "Failed to parse client identifier type");