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