]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-dhcp4.c
Merge pull request #17474 from yuwata/network-drop-link-deserialization-logic
[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 "strv.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 log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to get hostname: %m");
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_debug_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1173 else if (r < 0)
1174 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m");
1175
1176 return 0;
1177 }
1178
1179 static int dhcp4_set_client_identifier(Link *link) {
1180 int r;
1181
1182 assert(link);
1183 assert(link->network);
1184 assert(link->dhcp_client);
1185
1186 switch (link->network->dhcp_client_identifier) {
1187 case DHCP_CLIENT_ID_DUID: {
1188 /* If configured, apply user specified DUID and IAID */
1189 const DUID *duid = link_get_duid(link);
1190
1191 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1192 r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
1193 link->network->iaid_set,
1194 link->network->iaid,
1195 duid->llt_time);
1196 else
1197 r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
1198 link->network->iaid_set,
1199 link->network->iaid,
1200 duid->type,
1201 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1202 duid->raw_data_len);
1203 if (r < 0)
1204 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
1205 break;
1206 }
1207 case DHCP_CLIENT_ID_DUID_ONLY: {
1208 /* If configured, apply user specified DUID */
1209 const DUID *duid = link_get_duid(link);
1210
1211 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1212 r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
1213 duid->llt_time);
1214 else
1215 r = sd_dhcp_client_set_duid(link->dhcp_client,
1216 duid->type,
1217 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1218 duid->raw_data_len);
1219 if (r < 0)
1220 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
1221 break;
1222 }
1223 case DHCP_CLIENT_ID_MAC: {
1224 const uint8_t *hw_addr = link->hw_addr.addr.bytes;
1225 size_t hw_addr_len = link->hw_addr.length;
1226
1227 if (link->iftype == ARPHRD_INFINIBAND && hw_addr_len == INFINIBAND_ALEN) {
1228 /* set_client_id expects only last 8 bytes of an IB address */
1229 hw_addr += INFINIBAND_ALEN - 8;
1230 hw_addr_len -= INFINIBAND_ALEN - 8;
1231 }
1232
1233 r = sd_dhcp_client_set_client_id(link->dhcp_client,
1234 link->iftype,
1235 hw_addr,
1236 hw_addr_len);
1237 if (r < 0)
1238 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m");
1239 break;
1240 }
1241 default:
1242 assert_not_reached("Unknown client identifier type.");
1243 }
1244
1245 return 0;
1246 }
1247
1248 static int dhcp4_set_request_address(Link *link) {
1249 Address *a;
1250
1251 assert(link);
1252 assert(link->network);
1253 assert(link->dhcp_client);
1254
1255 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
1256 return 0;
1257
1258 SET_FOREACH(a, link->addresses_foreign) {
1259 if (a->family != AF_INET)
1260 continue;
1261 if (link_address_is_dynamic(link, a))
1262 break;
1263 }
1264
1265 if (!a)
1266 return 0;
1267
1268 return sd_dhcp_client_set_request_address(link->dhcp_client, &a->in_addr.in);
1269 }
1270
1271 int dhcp4_configure(Link *link) {
1272 sd_dhcp_option *send_option;
1273 void *request_options;
1274 int r;
1275
1276 assert(link);
1277 assert(link->network);
1278
1279 if (!link_dhcp4_enabled(link))
1280 return 0;
1281
1282 if (!link->dhcp_client) {
1283 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
1284 if (r < 0)
1285 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to allocate DHCP4 client: %m");
1286
1287 r = sd_dhcp_client_attach_event(link->dhcp_client, link->manager->event, 0);
1288 if (r < 0)
1289 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to attach event to DHCP4 client: %m");
1290 }
1291
1292 r = sd_dhcp_client_set_mac(link->dhcp_client,
1293 link->hw_addr.addr.bytes,
1294 link->bcast_addr.length > 0 ? link->bcast_addr.addr.bytes : NULL,
1295 link->hw_addr.length, link->iftype);
1296 if (r < 0)
1297 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m");
1298
1299 r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
1300 if (r < 0)
1301 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m");
1302
1303 r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
1304 if (r < 0)
1305 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m");
1306
1307 r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, link->network->dhcp_broadcast);
1308 if (r < 0)
1309 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1310
1311 if (link->mtu > 0) {
1312 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
1313 if (r < 0)
1314 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m");
1315 }
1316
1317 if (link->network->dhcp_use_mtu) {
1318 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_INTERFACE_MTU);
1319 if (r < 0)
1320 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1321 }
1322
1323 /* NOTE: even if this variable is called "use", it also "sends" PRL
1324 * options, maybe there should be a different configuration variable
1325 * to send or not route options?. */
1326 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1327 * by default, so they don't need to be added here. */
1328 if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) {
1329 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_STATIC_ROUTE);
1330 if (r < 0)
1331 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1332
1333 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
1334 if (r < 0)
1335 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1336 }
1337
1338 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO && !link->network->dhcp_anonymize) {
1339 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST);
1340 if (r < 0)
1341 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1342 }
1343
1344 if (link->network->dhcp_use_ntp) {
1345 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
1346 if (r < 0)
1347 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1348 }
1349
1350 if (link->network->dhcp_use_sip) {
1351 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_SIP_SERVER);
1352 if (r < 0)
1353 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1354 }
1355
1356 if (link->network->dhcp_use_timezone) {
1357 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
1358 if (r < 0)
1359 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1360 }
1361
1362 SET_FOREACH(request_options, link->network->dhcp_request_options) {
1363 uint32_t option = PTR_TO_UINT32(request_options);
1364
1365 r = sd_dhcp_client_set_request_option(link->dhcp_client, option);
1366 if (r < 0)
1367 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option);
1368 }
1369
1370 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_options) {
1371 r = sd_dhcp_client_add_option(link->dhcp_client, send_option);
1372 if (r == -EEXIST)
1373 continue;
1374 if (r < 0)
1375 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
1376 }
1377
1378 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_vendor_options) {
1379 r = sd_dhcp_client_add_vendor_option(link->dhcp_client, send_option);
1380 if (r == -EEXIST)
1381 continue;
1382 if (r < 0)
1383 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
1384 }
1385
1386 r = dhcp4_set_hostname(link);
1387 if (r < 0)
1388 return r;
1389
1390 if (link->network->dhcp_vendor_class_identifier) {
1391 r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
1392 link->network->dhcp_vendor_class_identifier);
1393 if (r < 0)
1394 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1395 }
1396
1397 if (link->network->dhcp_mudurl) {
1398 r = sd_dhcp_client_set_mud_url(link->dhcp_client, link->network->dhcp_mudurl);
1399 if (r < 0)
1400 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1401 }
1402
1403 if (link->network->dhcp_user_class) {
1404 r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class);
1405 if (r < 0)
1406 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m");
1407 }
1408
1409 if (link->network->dhcp_client_port > 0) {
1410 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
1411 if (r < 0)
1412 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
1413 }
1414
1415 if (link->network->dhcp_max_attempts > 0) {
1416 r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts);
1417 if (r < 0)
1418 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m");
1419 }
1420
1421 if (link->network->dhcp_ip_service_type > 0) {
1422 r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->dhcp_ip_service_type);
1423 if (r < 0)
1424 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set IP service type: %m");
1425 }
1426
1427 if (link->network->dhcp_fallback_lease_lifetime > 0) {
1428 r = sd_dhcp_client_set_fallback_lease_lifetime(link->dhcp_client, link->network->dhcp_fallback_lease_lifetime);
1429 if (r < 0)
1430 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1431 }
1432
1433 r = dhcp4_set_request_address(link);
1434 if (r < 0)
1435 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set initial DHCPv4 address: %m");
1436
1437 r = dhcp4_configure_dad(link);
1438 if (r < 0)
1439 return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to configure service type: %m");
1440
1441 return dhcp4_set_client_identifier(link);
1442 }
1443
1444 int dhcp4_update_mac(Link *link) {
1445 int r;
1446
1447 assert(link);
1448
1449 if (!link->dhcp_client)
1450 return 0;
1451
1452 r = sd_dhcp_client_set_mac(link->dhcp_client, link->hw_addr.addr.bytes,
1453 link->bcast_addr.length > 0 ? link->bcast_addr.addr.bytes : NULL,
1454 link->hw_addr.length, link->iftype);
1455 if (r < 0)
1456 return r;
1457
1458 r = dhcp4_set_client_identifier(link);
1459 if (r < 0)
1460 return r;
1461
1462 r = dhcp4_dad_update_mac(link);
1463 if (r < 0)
1464 return r;
1465
1466 return 0;
1467 }
1468
1469 int config_parse_dhcp_max_attempts(
1470 const char *unit,
1471 const char *filename,
1472 unsigned line,
1473 const char *section,
1474 unsigned section_line,
1475 const char *lvalue,
1476 int ltype,
1477 const char *rvalue,
1478 void *data,
1479 void *userdata) {
1480
1481 Network *network = data;
1482 uint64_t a;
1483 int r;
1484
1485 assert(network);
1486 assert(lvalue);
1487 assert(rvalue);
1488
1489 if (isempty(rvalue)) {
1490 network->dhcp_max_attempts = 0;
1491 return 0;
1492 }
1493
1494 if (streq(rvalue, "infinity")) {
1495 network->dhcp_max_attempts = (uint64_t) -1;
1496 return 0;
1497 }
1498
1499 r = safe_atou64(rvalue, &a);
1500 if (r < 0) {
1501 log_syntax(unit, LOG_WARNING, filename, line, r,
1502 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue);
1503 return 0;
1504 }
1505
1506 if (a == 0) {
1507 log_syntax(unit, LOG_WARNING, filename, line, 0,
1508 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue);
1509 return 0;
1510 }
1511
1512 network->dhcp_max_attempts = a;
1513
1514 return 0;
1515 }
1516
1517 int config_parse_dhcp_acl_ip_address(
1518 const char *unit,
1519 const char *filename,
1520 unsigned line,
1521 const char *section,
1522 unsigned section_line,
1523 const char *lvalue,
1524 int ltype,
1525 const char *rvalue,
1526 void *data,
1527 void *userdata) {
1528
1529 Network *network = data;
1530 Set **acl;
1531 int r;
1532
1533 assert(filename);
1534 assert(lvalue);
1535 assert(rvalue);
1536 assert(data);
1537
1538 acl = STR_IN_SET(lvalue, "DenyList", "BlackList") ? &network->dhcp_deny_listed_ip : &network->dhcp_allow_listed_ip;
1539
1540 if (isempty(rvalue)) {
1541 *acl = set_free(*acl);
1542 return 0;
1543 }
1544
1545 for (const char *p = rvalue;;) {
1546 _cleanup_free_ char *n = NULL;
1547 union in_addr_union ip;
1548
1549 r = extract_first_word(&p, &n, NULL, 0);
1550 if (r == -ENOMEM)
1551 return log_oom();
1552 if (r < 0) {
1553 log_syntax(unit, LOG_WARNING, filename, line, r,
1554 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1555 lvalue, rvalue);
1556 return 0;
1557 }
1558 if (r == 0)
1559 return 0;
1560
1561 r = in_addr_from_string(AF_INET, n, &ip);
1562 if (r < 0) {
1563 log_syntax(unit, LOG_WARNING, filename, line, r,
1564 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue, n);
1565 continue;
1566 }
1567
1568 r = set_ensure_put(acl, NULL, UINT32_TO_PTR(ip.in.s_addr));
1569 if (r < 0)
1570 log_syntax(unit, LOG_WARNING, filename, line, r,
1571 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue, n);
1572 }
1573 }
1574
1575 int config_parse_dhcp_ip_service_type(
1576 const char *unit,
1577 const char *filename,
1578 unsigned line,
1579 const char *section,
1580 unsigned section_line,
1581 const char *lvalue,
1582 int ltype,
1583 const char *rvalue,
1584 void *data,
1585 void *userdata) {
1586
1587 assert(filename);
1588 assert(lvalue);
1589 assert(rvalue);
1590
1591 if (streq(rvalue, "CS4"))
1592 *((int *)data) = IPTOS_CLASS_CS4;
1593 else if (streq(rvalue, "CS6"))
1594 *((int *)data) = IPTOS_CLASS_CS6;
1595 else
1596 log_syntax(unit, LOG_WARNING, filename, line, 0,
1597 "Failed to parse IPServiceType type '%s', ignoring.", rvalue);
1598
1599 return 0;
1600 }
1601
1602 int config_parse_dhcp_mud_url(
1603 const char *unit,
1604 const char *filename,
1605 unsigned line,
1606 const char *section,
1607 unsigned section_line,
1608 const char *lvalue,
1609 int ltype,
1610 const char *rvalue,
1611 void *data,
1612 void *userdata) {
1613
1614 _cleanup_free_ char *unescaped = NULL;
1615 Network *network = data;
1616 int r;
1617
1618 assert(filename);
1619 assert(lvalue);
1620 assert(rvalue);
1621
1622 if (isempty(rvalue)) {
1623 network->dhcp_mudurl = mfree(network->dhcp_mudurl);
1624 return 0;
1625 }
1626
1627 r = cunescape(rvalue, 0, &unescaped);
1628 if (r < 0) {
1629 log_syntax(unit, LOG_WARNING, filename, line, r,
1630 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
1631 return 0;
1632 }
1633
1634 if (!http_url_is_valid(unescaped) || strlen(unescaped) > 255) {
1635 log_syntax(unit, LOG_WARNING, filename, line, 0,
1636 "Failed to parse MUD URL '%s', ignoring: %m", rvalue);
1637
1638 return 0;
1639 }
1640
1641 return free_and_strdup_warn(&network->dhcp_mudurl, unescaped);
1642 }
1643
1644 int config_parse_dhcp_fallback_lease_lifetime(const char *unit,
1645 const char *filename,
1646 unsigned line,
1647 const char *section,
1648 unsigned section_line,
1649 const char *lvalue,
1650 int ltype,
1651 const char *rvalue,
1652 void *data,
1653 void *userdata) {
1654 Network *network = userdata;
1655 uint32_t k;
1656
1657 assert(filename);
1658 assert(section);
1659 assert(lvalue);
1660 assert(rvalue);
1661 assert(data);
1662
1663 if (isempty(rvalue)) {
1664 network->dhcp_fallback_lease_lifetime = 0;
1665 return 0;
1666 }
1667
1668 /* We accept only "forever" or "infinity". */
1669 if (STR_IN_SET(rvalue, "forever", "infinity"))
1670 k = CACHE_INFO_INFINITY_LIFE_TIME;
1671 else {
1672 log_syntax(unit, LOG_WARNING, filename, line, 0,
1673 "Invalid LeaseLifetime= value, ignoring: %s", rvalue);
1674 return 0;
1675 }
1676
1677 network->dhcp_fallback_lease_lifetime = k;
1678
1679 return 0;
1680 }
1681
1682 static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = {
1683 [DHCP_CLIENT_ID_MAC] = "mac",
1684 [DHCP_CLIENT_ID_DUID] = "duid",
1685 [DHCP_CLIENT_ID_DUID_ONLY] = "duid-only",
1686 };
1687
1688 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier);
1689 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier,
1690 "Failed to parse client identifier type");