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