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