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