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