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