]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-dhcp4.c
Merge pull request #13022 from keszybz/coverity-cleanups
[thirdparty/systemd.git] / src / network / networkd-dhcp4.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <netinet/in.h>
4 #include <linux/if.h>
5
6 #include "alloc-util.h"
7 #include "hostname-util.h"
8 #include "parse-util.h"
9 #include "network-internal.h"
10 #include "networkd-dhcp4.h"
11 #include "networkd-link.h"
12 #include "networkd-manager.h"
13 #include "networkd-network.h"
14 #include "string-table.h"
15 #include "string-util.h"
16 #include "sysctl-util.h"
17
18 static int dhcp_remove_routes(Link *link, sd_dhcp_lease *lease, sd_dhcp_lease *new_lease, struct in_addr *address);
19 static int dhcp_remove_router(Link *link, sd_dhcp_lease *lease, struct in_addr *address);
20 static int dhcp_remove_address(Link *link, sd_dhcp_lease *lease, struct in_addr *address);
21
22 void dhcp4_release_old_lease(Link *link) {
23 union in_addr_union address = IN_ADDR_NULL, address_old = IN_ADDR_NULL;
24
25 assert(link);
26
27 if (!link->dhcp_lease_old)
28 return;
29
30 assert(link->dhcp_lease);
31
32 (void) sd_dhcp_lease_get_address(link->dhcp_lease_old, &address_old.in);
33 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address.in);
34
35 (void) dhcp_remove_routes(link, link->dhcp_lease_old, link->dhcp_lease, &address_old.in);
36
37 if (!in_addr_equal(AF_INET, &address_old, &address)) {
38 (void) dhcp_remove_router(link, link->dhcp_lease_old, &address_old.in);
39 (void) dhcp_remove_address(link, link->dhcp_lease_old, &address_old.in);
40 }
41
42 link->dhcp_lease_old = sd_dhcp_lease_unref(link->dhcp_lease_old);
43 link_dirty(link);
44 }
45
46 static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
47 int r;
48
49 assert(link);
50 assert(link->dhcp4_messages > 0);
51
52 link->dhcp4_messages--;
53
54 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
55 return 1;
56
57 r = sd_netlink_message_get_errno(m);
58 if (r < 0 && r != -EEXIST) {
59 log_link_error_errno(link, r, "Could not set DHCPv4 route: %m");
60 link_enter_failed(link);
61 return 1;
62 }
63
64 if (link->dhcp4_messages == 0) {
65 link->dhcp4_configured = true;
66 /* New address and routes are configured now. Let's release old lease. */
67 dhcp4_release_old_lease(link);
68 link_check_ready(link);
69 }
70
71 return 1;
72 }
73
74 static int route_scope_from_address(const Route *route, const struct in_addr *self_addr) {
75 assert(route);
76 assert(self_addr);
77
78 if (in4_addr_is_localhost(&route->dst.in) ||
79 (!in4_addr_is_null(self_addr) && in4_addr_equal(&route->dst.in, self_addr)))
80 return RT_SCOPE_HOST;
81 else if (in4_addr_is_null(&route->gw.in))
82 return RT_SCOPE_LINK;
83 else
84 return RT_SCOPE_UNIVERSE;
85 }
86
87 static int link_set_dhcp_routes(Link *link) {
88 _cleanup_free_ sd_dhcp_route **static_routes = NULL;
89 bool classless_route = false, static_route = false;
90 const struct in_addr *router;
91 struct in_addr address;
92 int r, n, i;
93 uint32_t table;
94
95 assert(link);
96
97 if (!link->dhcp_lease) /* link went down while we configured the IP addresses? */
98 return 0;
99
100 if (!link->network) /* link went down while we configured the IP addresses? */
101 return 0;
102
103 if (!link->network->dhcp_use_routes)
104 return 0;
105
106 table = link_get_dhcp_route_table(link);
107
108 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
109 if (r < 0)
110 return log_link_warning_errno(link, r, "DHCP error: could not get address: %m");
111
112 n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes);
113 if (n == -ENODATA)
114 log_link_debug_errno(link, n, "DHCP: No routes received from DHCP server: %m");
115 else if (n < 0)
116 log_link_debug_errno(link, n, "DHCP error: could not get routes: %m");
117
118 for (i = 0; i < n; i++) {
119 switch (sd_dhcp_route_get_option(static_routes[i])) {
120 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE:
121 classless_route = true;
122 break;
123 case SD_DHCP_OPTION_STATIC_ROUTE:
124 static_route = true;
125 break;
126 }
127 }
128
129 for (i = 0; i < n; i++) {
130 _cleanup_(route_freep) Route *route = NULL;
131
132 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
133 the DHCP client MUST ignore the Static Routes option. */
134 if (classless_route &&
135 sd_dhcp_route_get_option(static_routes[i]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE)
136 continue;
137
138 r = route_new(&route);
139 if (r < 0)
140 return log_link_error_errno(link, r, "Could not allocate route: %m");
141
142 route->family = AF_INET;
143 route->protocol = RTPROT_DHCP;
144 assert_se(sd_dhcp_route_get_gateway(static_routes[i], &route->gw.in) >= 0);
145 assert_se(sd_dhcp_route_get_destination(static_routes[i], &route->dst.in) >= 0);
146 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen) >= 0);
147 route->priority = link->network->dhcp_route_metric;
148 route->table = table;
149 route->scope = route_scope_from_address(route, &address);
150 if (IN_SET(route->scope, RT_SCOPE_LINK, RT_SCOPE_UNIVERSE))
151 route->prefsrc.in = address;
152
153 r = route_configure(route, link, dhcp4_route_handler);
154 if (r < 0)
155 return log_link_error_errno(link, r, "Could not set host route: %m");
156 if (r > 0)
157 link->dhcp4_messages++;
158 }
159
160 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
161 if (IN_SET(r, 0, -ENODATA))
162 log_link_info(link, "DHCP: No gateway received from DHCP server.");
163 else if (r < 0)
164 log_link_warning_errno(link, r, "DHCP error: could not get gateway: %m");
165 else if (in4_addr_is_null(&router[0]))
166 log_link_info(link, "DHCP: Received gateway is null.");
167
168 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
169 a Router option, the DHCP client MUST ignore the Router option. */
170 if (classless_route && static_route)
171 log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option and router option");
172
173 if (r > 0 && !classless_route && !in4_addr_is_null(&router[0])) {
174 _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL;
175
176 r = route_new(&route_gw);
177 if (r < 0)
178 return log_link_error_errno(link, r, "Could not allocate route: %m");
179
180 /* The dhcp netmask may mask out the gateway. Add an explicit
181 * route for the gw host so that we can route no matter the
182 * netmask or existing kernel route tables. */
183 route_gw->family = AF_INET;
184 route_gw->dst.in = router[0];
185 route_gw->dst_prefixlen = 32;
186 route_gw->prefsrc.in = address;
187 route_gw->scope = RT_SCOPE_LINK;
188 route_gw->protocol = RTPROT_DHCP;
189 route_gw->priority = link->network->dhcp_route_metric;
190 route_gw->table = table;
191
192 r = route_configure(route_gw, link, dhcp4_route_handler);
193 if (r < 0)
194 return log_link_error_errno(link, r, "Could not set host route: %m");
195 if (r > 0)
196 link->dhcp4_messages++;
197
198 r = route_new(&route);
199 if (r < 0)
200 return log_link_error_errno(link, r, "Could not allocate route: %m");
201
202 route->family = AF_INET;
203 route->gw.in = router[0];
204 route->prefsrc.in = address;
205 route->protocol = RTPROT_DHCP;
206 route->priority = link->network->dhcp_route_metric;
207 route->table = table;
208
209 r = route_configure(route, link, dhcp4_route_handler);
210 if (r < 0)
211 return log_link_error_errno(link, r, "Could not set routes: %m");
212 if (r > 0)
213 link->dhcp4_messages++;
214 }
215
216 return 0;
217 }
218
219 static bool route_present_in_routes(const Route *route, sd_dhcp_route **routes, unsigned n_routes) {
220 assert(n_routes == 0 || routes);
221
222 for (unsigned j = 0; j < n_routes; j++) {
223 union in_addr_union a;
224 unsigned char l;
225
226 assert_se(sd_dhcp_route_get_gateway(routes[j], &a.in) >= 0);
227 if (!in_addr_equal(AF_INET, &a, &route->gw))
228 continue;
229 assert_se(sd_dhcp_route_get_destination(routes[j], &a.in) >= 0);
230 if (!in_addr_equal(AF_INET, &a, &route->dst))
231 continue;
232 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[j], &l) >= 0);
233 if (l != route->dst_prefixlen)
234 continue;
235 return true;
236 }
237
238 return false;
239 }
240
241 static int dhcp_remove_routes(Link *link, sd_dhcp_lease *lease, sd_dhcp_lease *new_lease, struct in_addr *address) {
242 _cleanup_free_ sd_dhcp_route **routes = NULL, **new_routes = NULL;
243 uint32_t table;
244 int m = 0, n, i, r;
245
246 assert(link);
247 assert(address);
248
249 if (!link->network->dhcp_use_routes)
250 return 0;
251
252 n = sd_dhcp_lease_get_routes(lease, &routes);
253 if (IN_SET(n, 0, -ENODATA))
254 return 0;
255 else if (n < 0)
256 return log_link_error_errno(link, n, "DHCP error: Failed to get routes: %m");
257
258 if (new_lease) {
259 m = sd_dhcp_lease_get_routes(new_lease, &new_routes);
260 if (m == -ENODATA)
261 m = 0;
262 else if (m < 0)
263 return log_link_error_errno(link, m, "DHCP error: Failed to get routes: %m");
264 }
265
266 table = link_get_dhcp_route_table(link);
267
268 for (i = 0; i < n; i++) {
269 _cleanup_(route_freep) Route *route = NULL;
270
271 r = route_new(&route);
272 if (r < 0)
273 return log_oom();
274
275 route->family = AF_INET;
276 assert_se(sd_dhcp_route_get_gateway(routes[i], &route->gw.in) >= 0);
277 assert_se(sd_dhcp_route_get_destination(routes[i], &route->dst.in) >= 0);
278 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &route->dst_prefixlen) >= 0);
279 route->priority = link->network->dhcp_route_metric;
280 route->table = table;
281 route->scope = route_scope_from_address(route, address);
282 if (IN_SET(route->scope, RT_SCOPE_LINK, RT_SCOPE_UNIVERSE))
283 route->prefsrc.in = *address;
284
285 if (route_present_in_routes(route, new_routes, m))
286 continue;
287
288 (void) route_remove(route, link, NULL);
289 }
290
291 return n;
292 }
293
294 static int dhcp_remove_router(Link *link, sd_dhcp_lease *lease, struct in_addr *address) {
295 _cleanup_(route_freep) Route *route_gw = NULL, *route = NULL;
296 const struct in_addr *router;
297 uint32_t table;
298 int r;
299
300 assert(link);
301 assert(address);
302
303 if (!link->network->dhcp_use_routes)
304 return 0;
305
306 r = sd_dhcp_lease_get_router(lease, &router);
307 if (IN_SET(r, 0, -ENODATA)) {
308 log_link_debug(link, "DHCP: No gateway received from DHCP server.");
309 return 0;
310 } else if (r < 0)
311 return log_link_error_errno(link, r, "DHCP error: could not get gateway: %m");
312 else if (in4_addr_is_null(&router[0])) {
313 log_link_info(link, "DHCP: Received gateway is null, ignoring.");
314 return 0;
315 }
316
317 table = link_get_dhcp_route_table(link);
318
319 r = route_new(&route_gw);
320 if (r < 0)
321 return log_oom();
322
323 route_gw->family = AF_INET;
324 route_gw->dst.in = router[0];
325 route_gw->dst_prefixlen = 32;
326 route_gw->prefsrc.in = *address;
327 route_gw->scope = RT_SCOPE_LINK;
328 route_gw->protocol = RTPROT_DHCP;
329 route_gw->priority = link->network->dhcp_route_metric;
330 route_gw->table = table;
331
332 (void) route_remove(route_gw, link, NULL);
333
334 r = route_new(&route);
335 if (r < 0)
336 return log_oom();
337
338 route->family = AF_INET;
339 route->gw.in = router[0];
340 route->prefsrc.in = *address;
341 route->protocol = RTPROT_DHCP;
342 route->priority = link->network->dhcp_route_metric;
343 route->table = table;
344
345 (void) route_remove(route, link, NULL);
346
347 return 0;
348 }
349
350 static int dhcp_remove_address(Link *link, sd_dhcp_lease *lease, struct in_addr *address) {
351 _cleanup_(address_freep) Address *a = NULL;
352 struct in_addr netmask;
353 int r;
354
355 assert(link);
356 assert(address);
357
358 if (in4_addr_is_null(address))
359 return 0;
360
361 r = address_new(&a);
362 if (r < 0)
363 return log_oom();
364
365 a->family = AF_INET;
366 a->in_addr.in = *address;
367
368 if (sd_dhcp_lease_get_netmask(lease, &netmask) >= 0)
369 a->prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
370
371 (void) address_remove(a, link, NULL);
372
373 return 0;
374 }
375
376 static int dhcp_reset_mtu(Link *link) {
377 uint16_t mtu;
378 int r;
379
380 assert(link);
381
382 if (!link->network->dhcp_use_mtu)
383 return 0;
384
385 r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
386 if (r < 0)
387 return r;
388
389 if (link->original_mtu == mtu)
390 return 0;
391
392 r = link_set_mtu(link, link->original_mtu);
393 if (r < 0) {
394 log_link_error_errno(link, r, "DHCP error: could not reset MTU: %m");
395 link_enter_failed(link);
396 return r;
397 }
398
399 return 0;
400 }
401
402 static int dhcp_reset_hostname(Link *link) {
403 const char *hostname;
404 int r;
405
406 assert(link);
407
408 if (!link->network->dhcp_use_hostname)
409 return 0;
410
411 hostname = link->network->dhcp_hostname;
412 if (!hostname)
413 (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
414
415 if (!hostname)
416 return 0;
417
418 /* If a hostname was set due to the lease, then unset it now. */
419 r = manager_set_hostname(link->manager, NULL);
420 if (r < 0)
421 return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m");
422
423 return 0;
424 }
425
426 static int dhcp_lease_lost(Link *link) {
427 struct in_addr address = {};
428
429 assert(link);
430 assert(link->dhcp_lease);
431
432 log_link_warning(link, "DHCP lease lost");
433
434 link->dhcp4_configured = false;
435
436 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address);
437 (void) dhcp_remove_routes(link, link->dhcp_lease, NULL, &address);
438 (void) dhcp_remove_router(link, link->dhcp_lease, &address);
439 (void) dhcp_remove_address(link, link->dhcp_lease, &address);
440 (void) dhcp_reset_mtu(link);
441 (void) dhcp_reset_hostname(link);
442
443 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
444 link_dirty(link);
445
446 return 0;
447 }
448
449 static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
450 int r;
451
452 assert(link);
453
454 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
455 return 1;
456
457 r = sd_netlink_message_get_errno(m);
458 if (r < 0 && r != -EEXIST) {
459 log_link_error_errno(link, r, "Could not set DHCPv4 address: %m");
460 link_enter_failed(link);
461 return 1;
462 }
463 if (r >= 0)
464 manager_rtnl_process_address(rtnl, m, link->manager);
465
466 r = link_set_dhcp_routes(link);
467 if (r < 0) {
468 link_enter_failed(link);
469 return 1;
470 }
471
472 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
473 link_request_set_routes(link);
474
475 if (link->dhcp4_messages == 0) {
476 link->dhcp4_configured = true;
477 /* The new address is configured, and no route is requested.
478 * Let's drop the old lease. */
479 dhcp4_release_old_lease(link);
480 link_check_ready(link);
481 }
482
483 return 1;
484 }
485
486 static int dhcp4_update_address(Link *link,
487 struct in_addr *address,
488 struct in_addr *netmask,
489 uint32_t lifetime) {
490 _cleanup_(address_freep) Address *addr = NULL;
491 unsigned prefixlen;
492 int r;
493
494 assert(address);
495 assert(netmask);
496 assert(lifetime);
497
498 prefixlen = in4_addr_netmask_to_prefixlen(netmask);
499
500 r = address_new(&addr);
501 if (r < 0)
502 return r;
503
504 addr->family = AF_INET;
505 addr->in_addr.in.s_addr = address->s_addr;
506 addr->cinfo.ifa_prefered = lifetime;
507 addr->cinfo.ifa_valid = lifetime;
508 addr->prefixlen = prefixlen;
509 addr->broadcast.s_addr = address->s_addr | ~netmask->s_addr;
510
511 /* allow reusing an existing address and simply update its lifetime
512 * in case it already exists */
513 r = address_configure(addr, link, dhcp4_address_handler, true);
514 if (r < 0)
515 return r;
516
517 return 0;
518 }
519
520 static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
521 sd_dhcp_lease *lease;
522 struct in_addr address;
523 struct in_addr netmask;
524 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
525 int r;
526
527 assert(link);
528 assert(client);
529 assert(link->network);
530
531 r = sd_dhcp_client_get_lease(client, &lease);
532 if (r < 0)
533 return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
534
535 sd_dhcp_lease_unref(link->dhcp_lease);
536 link->dhcp4_configured = false;
537 link->dhcp_lease = sd_dhcp_lease_ref(lease);
538 link_dirty(link);
539
540 r = sd_dhcp_lease_get_address(lease, &address);
541 if (r < 0)
542 return log_link_warning_errno(link, r, "DHCP error: no address: %m");
543
544 r = sd_dhcp_lease_get_netmask(lease, &netmask);
545 if (r < 0)
546 return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
547
548 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
549 r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
550 if (r < 0)
551 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
552 }
553
554 r = dhcp4_update_address(link, &address, &netmask, lifetime);
555 if (r < 0)
556 return log_link_warning_errno(link, r, "Could not update IP address: %m");
557
558 return 0;
559 }
560
561 static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
562 const struct in_addr *router;
563 sd_dhcp_lease *lease;
564 struct in_addr address;
565 struct in_addr netmask;
566 unsigned prefixlen;
567 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
568 int r;
569
570 assert(client);
571 assert(link);
572
573 link->dhcp4_configured = false;
574
575 r = sd_dhcp_client_get_lease(client, &lease);
576 if (r < 0)
577 return log_link_error_errno(link, r, "DHCP error: No lease: %m");
578
579 r = sd_dhcp_lease_get_address(lease, &address);
580 if (r < 0)
581 return log_link_error_errno(link, r, "DHCP error: No address: %m");
582
583 r = sd_dhcp_lease_get_netmask(lease, &netmask);
584 if (r < 0)
585 return log_link_error_errno(link, r, "DHCP error: No netmask: %m");
586
587 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
588
589 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
590 r = sd_dhcp_lease_get_lifetime(lease, &lifetime);
591 if (r < 0)
592 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
593 }
594
595 r = sd_dhcp_lease_get_router(lease, &router);
596 if (r < 0 && r != -ENODATA)
597 return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
598
599 if (r > 0 && !in4_addr_is_null(&router[0]))
600 log_struct(LOG_INFO,
601 LOG_LINK_INTERFACE(link),
602 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
603 ADDRESS_FMT_VAL(address),
604 prefixlen,
605 ADDRESS_FMT_VAL(router[0])),
606 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
607 "PREFIXLEN=%u", prefixlen,
608 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router[0]));
609 else
610 log_struct(LOG_INFO,
611 LOG_LINK_INTERFACE(link),
612 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u",
613 ADDRESS_FMT_VAL(address),
614 prefixlen),
615 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
616 "PREFIXLEN=%u", prefixlen);
617
618 link->dhcp_lease = sd_dhcp_lease_ref(lease);
619 link_dirty(link);
620
621 if (link->network->dhcp_use_mtu) {
622 uint16_t mtu;
623
624 r = sd_dhcp_lease_get_mtu(lease, &mtu);
625 if (r >= 0) {
626 r = link_set_mtu(link, mtu);
627 if (r < 0)
628 log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu);
629 }
630 }
631
632 if (link->network->dhcp_use_hostname) {
633 const char *dhcpname = NULL;
634 _cleanup_free_ char *hostname = NULL;
635
636 if (link->network->dhcp_hostname)
637 dhcpname = link->network->dhcp_hostname;
638 else
639 (void) sd_dhcp_lease_get_hostname(lease, &dhcpname);
640
641 if (dhcpname) {
642 r = shorten_overlong(dhcpname, &hostname);
643 if (r < 0)
644 log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
645 if (r == 1)
646 log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
647 }
648
649 if (hostname) {
650 r = manager_set_hostname(link->manager, hostname);
651 if (r < 0)
652 log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
653 }
654 }
655
656 if (link->network->dhcp_use_timezone) {
657 const char *tz = NULL;
658
659 (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
660
661 if (tz) {
662 r = manager_set_timezone(link->manager, tz);
663 if (r < 0)
664 log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz);
665 }
666 }
667
668 r = dhcp4_update_address(link, &address, &netmask, lifetime);
669 if (r < 0)
670 return log_link_warning_errno(link, r, "Could not update IP address: %m");
671
672 return 0;
673 }
674
675 static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) {
676 int r;
677
678 link->dhcp_lease_old = TAKE_PTR(link->dhcp_lease);
679
680 /* On ip address change, to keep the connectability, we would like to assign new address and
681 * routes, and then release old lease. There are two possible success paths:
682 *
683 * 1. new address and routes are configured.
684 * -> handled by dhcp_release_old_lease() in dhcp4_route_handler().
685 * 2. new address is configured and no route is requested.
686 * -> handled by dhcp_release_old_lease() in dhcp4_address_handler().
687 *
688 * On error in assigning new address and routes, then the link always enters to the failed
689 * state. And link_enter_failed() leads to the DHCP client to be stopped. So,
690 * dhcp_release_old_lease() will be also called by link_stop_clients().
691 */
692
693 r = dhcp_lease_acquired(client, link);
694 if (r < 0) {
695 /* If it fails, then the new address is not configured yet.
696 * So, let's simply drop the old lease. */
697 sd_dhcp_lease_unref(link->dhcp_lease);
698 link->dhcp_lease = TAKE_PTR(link->dhcp_lease_old);
699 (void) dhcp_lease_lost(link);
700 return r;
701 }
702
703 return 0;
704 }
705
706 static int dhcp_server_is_black_listed(Link *link, sd_dhcp_client *client) {
707 sd_dhcp_lease *lease;
708 struct in_addr addr;
709 int r;
710
711 assert(link);
712 assert(link->network);
713 assert(client);
714
715 r = sd_dhcp_client_get_lease(client, &lease);
716 if (r < 0)
717 return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
718
719 r = sd_dhcp_lease_get_server_identifier(lease, &addr);
720 if (r < 0)
721 return log_link_debug_errno(link, r, "Failed to get DHCP server ip address: %m");
722
723 if (set_contains(link->network->dhcp_black_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
724 log_struct(LOG_DEBUG,
725 LOG_LINK_INTERFACE(link),
726 LOG_LINK_MESSAGE(link, "DHCPv4 ip '%u.%u.%u.%u' found in black listed ip addresses, ignoring offer",
727 ADDRESS_FMT_VAL(addr)));
728 return true;
729 }
730
731 return false;
732 }
733
734 static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
735 Link *link = userdata;
736 int r;
737
738 assert(link);
739 assert(link->network);
740 assert(link->manager);
741
742 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
743 return 0;
744
745 switch (event) {
746 case SD_DHCP_CLIENT_EVENT_STOP:
747
748 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4)) {
749 assert(link->ipv4ll);
750
751 log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address");
752
753 r = sd_ipv4ll_start(link->ipv4ll);
754 if (r < 0)
755 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
756 }
757
758 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
759 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
760 return 0;
761 }
762
763 if (link->network->dhcp_send_release)
764 (void) sd_dhcp_client_send_release(client);
765
766 if (link->dhcp_lease) {
767 r = dhcp_lease_lost(link);
768 if (r < 0) {
769 link_enter_failed(link);
770 return r;
771 }
772 }
773
774 break;
775 case SD_DHCP_CLIENT_EVENT_EXPIRED:
776 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
777 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
778 return 0;
779 }
780
781 if (link->dhcp_lease) {
782 r = dhcp_lease_lost(link);
783 if (r < 0) {
784 link_enter_failed(link);
785 return r;
786 }
787 }
788
789 break;
790 case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
791 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
792 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
793 return 0;
794 }
795
796 r = dhcp_lease_ip_change(client, link);
797 if (r < 0) {
798 link_enter_failed(link);
799 return r;
800 }
801
802 break;
803 case SD_DHCP_CLIENT_EVENT_RENEW:
804 r = dhcp_lease_renew(client, link);
805 if (r < 0) {
806 link_enter_failed(link);
807 return r;
808 }
809 break;
810 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
811 r = dhcp_lease_acquired(client, link);
812 if (r < 0) {
813 link_enter_failed(link);
814 return r;
815 }
816 break;
817 case SD_DHCP_CLIENT_EVENT_SELECTING:
818 r = dhcp_server_is_black_listed(link, client);
819 if (r < 0)
820 return r;
821 if (r != 0)
822 return -ENOMSG;
823 break;
824 default:
825 if (event < 0)
826 log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
827 else
828 log_link_warning(link, "DHCP unknown event: %i", event);
829 break;
830 }
831
832 return 0;
833 }
834
835 static int dhcp4_set_hostname(Link *link) {
836 _cleanup_free_ char *hostname = NULL;
837 const char *hn;
838 int r;
839
840 assert(link);
841
842 if (!link->network->dhcp_send_hostname)
843 hn = NULL;
844 else if (link->network->dhcp_hostname)
845 hn = link->network->dhcp_hostname;
846 else {
847 r = gethostname_strict(&hostname);
848 if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
849 return r;
850
851 hn = hostname;
852 }
853
854 r = sd_dhcp_client_set_hostname(link->dhcp_client, hn);
855 if (r == -EINVAL && hostname)
856 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
857 log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
858 else if (r < 0)
859 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m");
860
861 return 0;
862 }
863
864 static bool promote_secondaries_enabled(const char *ifname) {
865 _cleanup_free_ char *promote_secondaries_sysctl = NULL;
866 char *promote_secondaries_path;
867 int r;
868
869 promote_secondaries_path = strjoina("net/ipv4/conf/", ifname, "/promote_secondaries");
870 r = sysctl_read(promote_secondaries_path, &promote_secondaries_sysctl);
871 if (r < 0) {
872 log_debug_errno(r, "Cannot read sysctl %s", promote_secondaries_path);
873 return false;
874 }
875
876 truncate_nl(promote_secondaries_sysctl);
877 r = parse_boolean(promote_secondaries_sysctl);
878 if (r < 0)
879 log_warning_errno(r, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path, promote_secondaries_sysctl);
880 return r > 0;
881 }
882
883 /* dhcp4_set_promote_secondaries will ensure this interface has
884 * the "promote_secondaries" option in the kernel set. If this sysctl
885 * is not set DHCP will work only as long as the IP address does not
886 * changes between leases. The kernel will remove all secondary IP
887 * addresses of an interface otherwise. The way systemd-network works
888 * is that the new IP of a lease is added as a secondary IP and when
889 * the primary one expires it relies on the kernel to promote the
890 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
891 */
892 int dhcp4_set_promote_secondaries(Link *link) {
893 int r;
894
895 assert(link);
896 assert(link->network);
897 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
898
899 /* check if the kernel has promote_secondaries enabled for our
900 * interface. If it is not globally enabled or enabled for the
901 * specific interface we must either enable it.
902 */
903 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link->ifname))) {
904 char *promote_secondaries_path = NULL;
905
906 log_link_debug(link, "promote_secondaries is unset, setting it");
907 promote_secondaries_path = strjoina("net/ipv4/conf/", link->ifname, "/promote_secondaries");
908 r = sysctl_write(promote_secondaries_path, "1");
909 if (r < 0)
910 log_link_warning_errno(link, r, "cannot set sysctl %s to 1", promote_secondaries_path);
911 return r > 0;
912 }
913
914 return 0;
915 }
916
917 int dhcp4_set_client_identifier(Link *link) {
918 int r;
919
920 assert(link);
921 assert(link->network);
922 assert(link->dhcp_client);
923
924 switch (link->network->dhcp_client_identifier) {
925 case DHCP_CLIENT_ID_DUID: {
926 /* If configured, apply user specified DUID and IAID */
927 const DUID *duid = link_get_duid(link);
928
929 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
930 r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
931 link->network->iaid_set,
932 link->network->iaid,
933 duid->llt_time);
934 else
935 r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
936 link->network->iaid_set,
937 link->network->iaid,
938 duid->type,
939 duid->raw_data_len > 0 ? duid->raw_data : NULL,
940 duid->raw_data_len);
941 if (r < 0)
942 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
943 break;
944 }
945 case DHCP_CLIENT_ID_DUID_ONLY: {
946 /* If configured, apply user specified DUID */
947 const DUID *duid = link_get_duid(link);
948
949 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
950 r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
951 duid->llt_time);
952 else
953 r = sd_dhcp_client_set_duid(link->dhcp_client,
954 duid->type,
955 duid->raw_data_len > 0 ? duid->raw_data : NULL,
956 duid->raw_data_len);
957 if (r < 0)
958 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
959 break;
960 }
961 case DHCP_CLIENT_ID_MAC:
962 r = sd_dhcp_client_set_client_id(link->dhcp_client,
963 ARPHRD_ETHER,
964 (const uint8_t *) &link->mac,
965 sizeof(link->mac));
966 if (r < 0)
967 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m");
968 break;
969 default:
970 assert_not_reached("Unknown client identifier type.");
971 }
972
973 return 0;
974 }
975
976 int dhcp4_configure(Link *link) {
977 int r;
978
979 assert(link);
980 assert(link->network);
981 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
982
983 if (!link->dhcp_client) {
984 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
985 if (r == -ENOMEM)
986 return log_oom();
987 if (r < 0)
988 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
989 }
990
991 r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
992 if (r < 0)
993 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to attach event: %m");
994
995 r = sd_dhcp_client_set_mac(link->dhcp_client,
996 (const uint8_t *) &link->mac,
997 sizeof (link->mac), ARPHRD_ETHER);
998 if (r < 0)
999 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m");
1000
1001 r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
1002 if (r < 0)
1003 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m");
1004
1005 r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
1006 if (r < 0)
1007 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m");
1008
1009 r = sd_dhcp_client_set_request_broadcast(link->dhcp_client,
1010 link->network->dhcp_broadcast);
1011 if (r < 0)
1012 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
1013
1014 if (link->mtu) {
1015 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
1016 if (r < 0)
1017 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m");
1018 }
1019
1020 if (link->network->dhcp_use_mtu) {
1021 r = sd_dhcp_client_set_request_option(link->dhcp_client,
1022 SD_DHCP_OPTION_INTERFACE_MTU);
1023 if (r < 0)
1024 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
1025 }
1026
1027 /* NOTE: even if this variable is called "use", it also "sends" PRL
1028 * options, maybe there should be a different configuration variable
1029 * to send or not route options?. */
1030 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1031 * by default, so they don't need to be added here. */
1032 if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) {
1033 r = sd_dhcp_client_set_request_option(link->dhcp_client,
1034 SD_DHCP_OPTION_STATIC_ROUTE);
1035 if (r < 0)
1036 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1037
1038 r = sd_dhcp_client_set_request_option(link->dhcp_client,
1039 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
1040 if (r < 0)
1041 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
1042 }
1043
1044 if (link->network->dhcp_use_ntp) {
1045 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
1046 if (r < 0)
1047 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
1048 }
1049
1050 if (link->network->dhcp_use_timezone) {
1051 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
1052 if (r < 0)
1053 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
1054 }
1055
1056 r = dhcp4_set_hostname(link);
1057 if (r < 0)
1058 return r;
1059
1060 if (link->network->dhcp_vendor_class_identifier) {
1061 r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
1062 link->network->dhcp_vendor_class_identifier);
1063 if (r < 0)
1064 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
1065 }
1066
1067 if (link->network->dhcp_user_class) {
1068 r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class);
1069 if (r < 0)
1070 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m");
1071 }
1072
1073 if (link->network->dhcp_client_port) {
1074 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
1075 if (r < 0)
1076 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
1077 }
1078
1079 if (link->network->dhcp_max_attempts > 0) {
1080 r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts);
1081 if (r < 0)
1082 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m");
1083 }
1084
1085 return dhcp4_set_client_identifier(link);
1086 }
1087
1088 int config_parse_dhcp_max_attempts(
1089 const char *unit,
1090 const char *filename,
1091 unsigned line,
1092 const char *section,
1093 unsigned section_line,
1094 const char *lvalue,
1095 int ltype,
1096 const char *rvalue,
1097 void *data,
1098 void *userdata) {
1099
1100 Network *network = data;
1101 uint64_t a;
1102 int r;
1103
1104 assert(network);
1105 assert(lvalue);
1106 assert(rvalue);
1107
1108 if (isempty(rvalue)) {
1109 network->dhcp_max_attempts = 0;
1110 return 0;
1111 }
1112
1113 if (streq(rvalue, "infinity")) {
1114 network->dhcp_max_attempts = (uint64_t) -1;
1115 return 0;
1116 }
1117
1118 r = safe_atou64(rvalue, &a);
1119 if (r < 0) {
1120 log_syntax(unit, LOG_ERR, filename, line, r,
1121 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue);
1122 return 0;
1123 }
1124
1125 if (a == 0) {
1126 log_syntax(unit, LOG_ERR, filename, line, 0,
1127 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue);
1128 return 0;
1129 }
1130
1131 network->dhcp_max_attempts = a;
1132
1133 return 0;
1134 }
1135
1136 int config_parse_dhcp_black_listed_ip_address(
1137 const char *unit,
1138 const char *filename,
1139 unsigned line,
1140 const char *section,
1141 unsigned section_line,
1142 const char *lvalue,
1143 int ltype,
1144 const char *rvalue,
1145 void *data,
1146 void *userdata) {
1147
1148 Network *network = data;
1149 const char *p;
1150 int r;
1151
1152 assert(filename);
1153 assert(lvalue);
1154 assert(rvalue);
1155 assert(data);
1156
1157 if (isempty(rvalue)) {
1158 network->dhcp_black_listed_ip = set_free(network->dhcp_black_listed_ip);
1159 return 0;
1160 }
1161
1162 for (p = rvalue;;) {
1163 _cleanup_free_ char *n = NULL;
1164 union in_addr_union ip;
1165
1166 r = extract_first_word(&p, &n, NULL, 0);
1167 if (r < 0) {
1168 log_syntax(unit, LOG_ERR, filename, line, r,
1169 "Failed to parse DHCP black listed ip address, ignoring assignment: %s",
1170 rvalue);
1171 return 0;
1172 }
1173 if (r == 0)
1174 return 0;
1175
1176 r = in_addr_from_string(AF_INET, n, &ip);
1177 if (r < 0) {
1178 log_syntax(unit, LOG_ERR, filename, line, r,
1179 "DHCP black listed ip address is invalid, ignoring assignment: %s", n);
1180 continue;
1181 }
1182
1183 r = set_ensure_allocated(&network->dhcp_black_listed_ip, NULL);
1184 if (r < 0)
1185 return log_oom();
1186
1187 r = set_put(network->dhcp_black_listed_ip, UINT32_TO_PTR(ip.in.s_addr));
1188 if (r < 0)
1189 log_syntax(unit, LOG_ERR, filename, line, r,
1190 "Failed to store DHCP black listed ip address '%s', ignoring assignment: %m", n);
1191 }
1192
1193 return 0;
1194 }
1195
1196 int config_parse_dhcp_user_class(
1197 const char *unit,
1198 const char *filename,
1199 unsigned line,
1200 const char *section,
1201 unsigned section_line,
1202 const char *lvalue,
1203 int ltype,
1204 const char *rvalue,
1205 void *data,
1206 void *userdata) {
1207
1208 char ***l = data;
1209 int r;
1210
1211 assert(l);
1212 assert(lvalue);
1213 assert(rvalue);
1214
1215 if (isempty(rvalue)) {
1216 *l = strv_free(*l);
1217 return 0;
1218 }
1219
1220 for (;;) {
1221 _cleanup_free_ char *w = NULL;
1222
1223 r = extract_first_word(&rvalue, &w, NULL, 0);
1224 if (r == -ENOMEM)
1225 return log_oom();
1226 if (r < 0) {
1227 log_syntax(unit, LOG_ERR, filename, line, r,
1228 "Failed to split user classes option, ignoring: %s", rvalue);
1229 break;
1230 }
1231 if (r == 0)
1232 break;
1233
1234 if (strlen(w) > 255) {
1235 log_syntax(unit, LOG_ERR, filename, line, 0,
1236 "%s length is not in the range 1-255, ignoring.", w);
1237 continue;
1238 }
1239
1240 r = strv_push(l, w);
1241 if (r < 0)
1242 return log_oom();
1243
1244 w = NULL;
1245 }
1246
1247 return 0;
1248 }
1249
1250 static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = {
1251 [DHCP_CLIENT_ID_MAC] = "mac",
1252 [DHCP_CLIENT_ID_DUID] = "duid",
1253 [DHCP_CLIENT_ID_DUID_ONLY] = "duid-only",
1254 };
1255
1256 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier);
1257 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier,
1258 "Failed to parse client identifier type");