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