]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-dhcp4.c
01f4e955c85e9b899cf4e4b1c0e45866de9ec3b7
[thirdparty/systemd.git] / src / network / networkd-dhcp4.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <netinet/ether.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-link.h"
11 #include "networkd-manager.h"
12 #include "networkd-network.h"
13 #include "string-util.h"
14 #include "sysctl-util.h"
15
16 static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
17 int r;
18
19 assert(link);
20 assert(link->dhcp4_messages > 0);
21
22 link->dhcp4_messages--;
23
24 r = sd_netlink_message_get_errno(m);
25 if (r < 0 && r != -EEXIST) {
26 log_link_error_errno(link, r, "Could not set DHCPv4 route: %m");
27 link_enter_failed(link);
28 }
29
30 if (link->dhcp4_messages == 0) {
31 link->dhcp4_configured = true;
32 link_check_ready(link);
33 }
34
35 return 1;
36 }
37
38 static int route_scope_from_address(const Route *route, const struct in_addr *self_addr) {
39 assert(route);
40 assert(self_addr);
41
42 if (in_addr_is_localhost(AF_INET, &route->dst) ||
43 (self_addr->s_addr && route->dst.in.s_addr == self_addr->s_addr))
44 return RT_SCOPE_HOST;
45 else if (in4_addr_is_null(&route->gw.in))
46 return RT_SCOPE_LINK;
47 else
48 return RT_SCOPE_UNIVERSE;
49 }
50
51 static int link_set_dhcp_routes(Link *link) {
52 _cleanup_free_ sd_dhcp_route **static_routes = NULL;
53 bool classless_route = false, static_route = false;
54 const struct in_addr *router;
55 struct in_addr address;
56 int r, n, i;
57 uint32_t table;
58
59 assert(link);
60
61 if (!link->dhcp_lease) /* link went down while we configured the IP addresses? */
62 return 0;
63
64 if (!link->network) /* link went down while we configured the IP addresses? */
65 return 0;
66
67 if (!link->network->dhcp_use_routes)
68 return 0;
69
70 table = link_get_dhcp_route_table(link);
71
72 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
73 if (r < 0)
74 return log_link_warning_errno(link, r, "DHCP error: could not get address: %m");
75
76 n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes);
77 if (n == -ENODATA)
78 log_link_debug_errno(link, n, "DHCP: No routes received from DHCP server: %m");
79 else if (n < 0)
80 log_link_debug_errno(link, n, "DHCP error: could not get routes: %m");
81
82 for (i = 0; i < n; i++) {
83 switch (sd_dhcp_route_get_option(static_routes[i])) {
84 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE:
85 classless_route = true;
86 break;
87 case SD_DHCP_OPTION_STATIC_ROUTE:
88 static_route = true;
89 break;
90 }
91 }
92
93 for (i = 0; i < n; i++) {
94 _cleanup_(route_freep) Route *route = NULL;
95
96 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
97 the DHCP client MUST ignore the Static Routes option. */
98 if (classless_route &&
99 sd_dhcp_route_get_option(static_routes[i]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE)
100 continue;
101
102 r = route_new(&route);
103 if (r < 0)
104 return log_link_error_errno(link, r, "Could not allocate route: %m");
105
106 route->family = AF_INET;
107 route->protocol = RTPROT_DHCP;
108 assert_se(sd_dhcp_route_get_gateway(static_routes[i], &route->gw.in) >= 0);
109 assert_se(sd_dhcp_route_get_destination(static_routes[i], &route->dst.in) >= 0);
110 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen) >= 0);
111 route->priority = link->network->dhcp_route_metric;
112 route->table = table;
113 route->scope = route_scope_from_address(route, &address);
114
115 r = route_configure(route, link, dhcp4_route_handler);
116 if (r < 0)
117 return log_link_warning_errno(link, r, "Could not set host route: %m");
118
119 link->dhcp4_messages++;
120 }
121
122 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
123 if (IN_SET(r, 0, -ENODATA))
124 log_link_info(link, "DHCP: No gateway received from DHCP server.");
125 else if (r < 0)
126 log_link_warning_errno(link, r, "DHCP error: could not get gateway: %m");
127 else if (in4_addr_is_null(&router[0]))
128 log_link_info(link, "DHCP: Received gateway is null.");
129
130 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
131 a Router option, the DHCP client MUST ignore the Router option. */
132 if (classless_route && static_route)
133 log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option and router option");
134
135 if (r > 0 && !classless_route && !in4_addr_is_null(&router[0])) {
136 _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL;
137
138 r = route_new(&route_gw);
139 if (r < 0)
140 return log_link_error_errno(link, r, "Could not allocate route: %m");
141
142 /* The dhcp netmask may mask out the gateway. Add an explicit
143 * route for the gw host so that we can route no matter the
144 * netmask or existing kernel route tables. */
145 route_gw->family = AF_INET;
146 route_gw->dst.in = router[0];
147 route_gw->dst_prefixlen = 32;
148 route_gw->prefsrc.in = address;
149 route_gw->scope = RT_SCOPE_LINK;
150 route_gw->protocol = RTPROT_DHCP;
151 route_gw->priority = link->network->dhcp_route_metric;
152 route_gw->table = table;
153
154 r = route_configure(route_gw, link, dhcp4_route_handler);
155 if (r < 0)
156 return log_link_warning_errno(link, r, "Could not set host route: %m");
157
158 link->dhcp4_messages++;
159
160 r = route_new(&route);
161 if (r < 0)
162 return log_link_error_errno(link, r, "Could not allocate route: %m");
163
164 route->family = AF_INET;
165 route->gw.in = router[0];
166 route->prefsrc.in = address;
167 route->protocol = RTPROT_DHCP;
168 route->priority = link->network->dhcp_route_metric;
169 route->table = table;
170
171 r = route_configure(route, link, dhcp4_route_handler);
172 if (r < 0) {
173 log_link_warning_errno(link, r, "Could not set routes: %m");
174 link_enter_failed(link);
175 return r;
176 }
177
178 link->dhcp4_messages++;
179 }
180
181 return 0;
182 }
183
184 static int dhcp_lease_lost(Link *link) {
185 _cleanup_(address_freep) Address *address = NULL;
186 const struct in_addr *router;
187 struct in_addr addr;
188 struct in_addr netmask;
189 unsigned prefixlen = 0;
190 int r;
191
192 assert(link);
193 assert(link->dhcp_lease);
194
195 log_link_warning(link, "DHCP lease lost");
196
197 if (link->network->dhcp_use_routes) {
198 _cleanup_free_ sd_dhcp_route **routes = NULL;
199 int n, i;
200
201 n = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes);
202 if (n >= 0) {
203 for (i = 0; i < n; i++) {
204 _cleanup_(route_freep) Route *route = NULL;
205
206 r = route_new(&route);
207 if (r >= 0) {
208 route->family = AF_INET;
209 assert_se(sd_dhcp_route_get_gateway(routes[i], &route->gw.in) >= 0);
210 assert_se(sd_dhcp_route_get_destination(routes[i], &route->dst.in) >= 0);
211 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &route->dst_prefixlen) >= 0);
212
213 route_remove(route, link, NULL);
214 }
215 }
216 }
217
218 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
219 if (r > 0 && !in4_addr_is_null(&router[0])) {
220 _cleanup_(route_freep) Route *route_gw = NULL;
221 _cleanup_(route_freep) Route *route = NULL;
222
223 r = route_new(&route_gw);
224 if (r >= 0) {
225 route_gw->family = AF_INET;
226 route_gw->dst.in = router[0];
227 route_gw->dst_prefixlen = 32;
228 route_gw->scope = RT_SCOPE_LINK;
229
230 route_remove(route_gw, link, NULL);
231 }
232
233 r = route_new(&route);
234 if (r >= 0) {
235 route->family = AF_INET;
236 route->gw.in = router[0];
237
238 route_remove(route, link, NULL);
239 }
240 }
241 }
242
243 r = address_new(&address);
244 if (r >= 0) {
245 r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
246 if (r >= 0) {
247 r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
248 if (r >= 0)
249 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
250
251 address->family = AF_INET;
252 address->in_addr.in = addr;
253 address->prefixlen = prefixlen;
254
255 address_remove(address, link, NULL);
256 }
257 }
258
259 if (link->network->dhcp_use_mtu) {
260 uint16_t mtu;
261
262 r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
263 if (r >= 0 && link->original_mtu != mtu) {
264 r = link_set_mtu(link, link->original_mtu, true);
265 if (r < 0) {
266 log_link_warning(link,
267 "DHCP error: could not reset MTU");
268 link_enter_failed(link);
269 return r;
270 }
271 }
272 }
273
274 if (link->network->dhcp_use_hostname) {
275 const char *hostname = NULL;
276
277 if (link->network->dhcp_hostname)
278 hostname = link->network->dhcp_hostname;
279 else
280 (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
281
282 if (hostname) {
283 /* If a hostname was set due to the lease, then unset it now. */
284 r = manager_set_hostname(link->manager, NULL);
285 if (r < 0)
286 log_link_warning_errno(link, r, "Failed to reset transient hostname: %m");
287 }
288 }
289
290 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
291 link_dirty(link);
292 link->dhcp4_configured = false;
293
294 return 0;
295 }
296
297 static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
298 int r;
299
300 assert(link);
301
302 r = sd_netlink_message_get_errno(m);
303 if (r < 0 && r != -EEXIST) {
304 log_link_error_errno(link, r, "Could not set DHCPv4 address: %m");
305 link_enter_failed(link);
306 } else if (r >= 0)
307 manager_rtnl_process_address(rtnl, m, link->manager);
308
309 link_set_dhcp_routes(link);
310
311 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
312 link_request_set_routes(link);
313
314 if (link->dhcp4_messages == 0) {
315 link->dhcp4_configured = true;
316 link_check_ready(link);
317 }
318
319 return 1;
320 }
321
322 static int dhcp4_update_address(Link *link,
323 struct in_addr *address,
324 struct in_addr *netmask,
325 uint32_t lifetime) {
326 _cleanup_(address_freep) Address *addr = NULL;
327 unsigned prefixlen;
328 int r;
329
330 assert(address);
331 assert(netmask);
332 assert(lifetime);
333
334 prefixlen = in4_addr_netmask_to_prefixlen(netmask);
335
336 r = address_new(&addr);
337 if (r < 0)
338 return r;
339
340 addr->family = AF_INET;
341 addr->in_addr.in.s_addr = address->s_addr;
342 addr->cinfo.ifa_prefered = lifetime;
343 addr->cinfo.ifa_valid = lifetime;
344 addr->prefixlen = prefixlen;
345 addr->broadcast.s_addr = address->s_addr | ~netmask->s_addr;
346
347 /* allow reusing an existing address and simply update its lifetime
348 * in case it already exists */
349 r = address_configure(addr, link, dhcp4_address_handler, true);
350 if (r < 0)
351 return r;
352
353 return 0;
354 }
355
356 static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
357 sd_dhcp_lease *lease;
358 struct in_addr address;
359 struct in_addr netmask;
360 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
361 int r;
362
363 assert(link);
364 assert(client);
365 assert(link->network);
366
367 r = sd_dhcp_client_get_lease(client, &lease);
368 if (r < 0)
369 return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
370
371 sd_dhcp_lease_unref(link->dhcp_lease);
372 link->dhcp4_configured = false;
373 link->dhcp_lease = sd_dhcp_lease_ref(lease);
374 link_dirty(link);
375
376 r = sd_dhcp_lease_get_address(lease, &address);
377 if (r < 0)
378 return log_link_warning_errno(link, r, "DHCP error: no address: %m");
379
380 r = sd_dhcp_lease_get_netmask(lease, &netmask);
381 if (r < 0)
382 return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
383
384 if (!link->network->dhcp_critical) {
385 r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
386 if (r < 0)
387 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
388 }
389
390 r = dhcp4_update_address(link, &address, &netmask, lifetime);
391 if (r < 0) {
392 log_link_warning_errno(link, r, "Could not update IP address: %m");
393 link_enter_failed(link);
394 return r;
395 }
396
397 return 0;
398 }
399
400 static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
401 const struct in_addr *router;
402 sd_dhcp_lease *lease;
403 struct in_addr address;
404 struct in_addr netmask;
405 unsigned prefixlen;
406 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
407 int r;
408
409 assert(client);
410 assert(link);
411
412 r = sd_dhcp_client_get_lease(client, &lease);
413 if (r < 0)
414 return log_link_error_errno(link, r, "DHCP error: No lease: %m");
415
416 r = sd_dhcp_lease_get_address(lease, &address);
417 if (r < 0)
418 return log_link_error_errno(link, r, "DHCP error: No address: %m");
419
420 r = sd_dhcp_lease_get_netmask(lease, &netmask);
421 if (r < 0)
422 return log_link_error_errno(link, r, "DHCP error: No netmask: %m");
423
424 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
425
426 r = sd_dhcp_lease_get_router(lease, &router);
427 if (r < 0 && r != -ENODATA)
428 return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
429
430 if (r > 0 && !in4_addr_is_null(&router[0]))
431 log_struct(LOG_INFO,
432 LOG_LINK_INTERFACE(link),
433 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
434 ADDRESS_FMT_VAL(address),
435 prefixlen,
436 ADDRESS_FMT_VAL(router[0])),
437 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
438 "PREFIXLEN=%u", prefixlen,
439 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router[0]));
440 else
441 log_struct(LOG_INFO,
442 LOG_LINK_INTERFACE(link),
443 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u",
444 ADDRESS_FMT_VAL(address),
445 prefixlen),
446 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
447 "PREFIXLEN=%u", prefixlen);
448
449 link->dhcp_lease = sd_dhcp_lease_ref(lease);
450 link_dirty(link);
451
452 if (link->network->dhcp_use_mtu) {
453 uint16_t mtu;
454
455 r = sd_dhcp_lease_get_mtu(lease, &mtu);
456 if (r >= 0) {
457 r = link_set_mtu(link, mtu, true);
458 if (r < 0)
459 log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu);
460 }
461 }
462
463 if (link->network->dhcp_use_hostname) {
464 const char *dhcpname = NULL;
465 _cleanup_free_ char *hostname = NULL;
466
467 if (link->network->dhcp_hostname)
468 dhcpname = link->network->dhcp_hostname;
469 else
470 (void) sd_dhcp_lease_get_hostname(lease, &dhcpname);
471
472 if (dhcpname) {
473 r = shorten_overlong(dhcpname, &hostname);
474 if (r < 0)
475 log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
476 if (r == 1)
477 log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
478 }
479
480 if (hostname) {
481 r = manager_set_hostname(link->manager, hostname);
482 if (r < 0)
483 log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
484 }
485 }
486
487 if (link->network->dhcp_use_timezone) {
488 const char *tz = NULL;
489
490 (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
491
492 if (tz) {
493 r = manager_set_timezone(link->manager, tz);
494 if (r < 0)
495 log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz);
496 }
497 }
498
499 if (!link->network->dhcp_critical) {
500 r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
501 if (r < 0)
502 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
503 }
504
505 r = dhcp4_update_address(link, &address, &netmask, lifetime);
506 if (r < 0) {
507 log_link_warning_errno(link, r, "Could not update IP address: %m");
508 link_enter_failed(link);
509 return r;
510 }
511
512 return 0;
513 }
514
515 static void dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
516 Link *link = userdata;
517 int r = 0;
518
519 assert(link);
520 assert(link->network);
521 assert(link->manager);
522
523 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
524 return;
525
526 switch (event) {
527 case SD_DHCP_CLIENT_EVENT_STOP:
528
529 if (link_ipv4ll_fallback_enabled(link)) {
530 assert(link->ipv4ll);
531
532 log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address");
533
534 r = sd_ipv4ll_start(link->ipv4ll);
535 if (r < 0) {
536 log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
537 return;
538 }
539 }
540
541 _fallthrough_;
542 case SD_DHCP_CLIENT_EVENT_EXPIRED:
543 case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
544
545 if (link->network->dhcp_critical) {
546 log_link_error(link, "DHCPv4 connection considered system critical, ignoring request to reconfigure it.");
547 return;
548 }
549
550 if (link->dhcp_lease) {
551 r = dhcp_lease_lost(link);
552 if (r < 0) {
553 link_enter_failed(link);
554 return;
555 }
556 }
557
558 if (event == SD_DHCP_CLIENT_EVENT_IP_CHANGE) {
559 r = dhcp_lease_acquired(client, link);
560 if (r < 0) {
561 link_enter_failed(link);
562 return;
563 }
564 }
565
566 break;
567 case SD_DHCP_CLIENT_EVENT_RENEW:
568 r = dhcp_lease_renew(client, link);
569 if (r < 0) {
570 link_enter_failed(link);
571 return;
572 }
573 break;
574 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
575 r = dhcp_lease_acquired(client, link);
576 if (r < 0) {
577 link_enter_failed(link);
578 return;
579 }
580 break;
581 default:
582 if (event < 0)
583 log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
584 else
585 log_link_warning(link, "DHCP unknown event: %i", event);
586 break;
587 }
588
589 return;
590 }
591
592 static int dhcp4_set_hostname(Link *link) {
593 _cleanup_free_ char *hostname = NULL;
594 const char *hn;
595 int r;
596
597 assert(link);
598
599 if (!link->network->dhcp_send_hostname)
600 hn = NULL;
601 else if (link->network->dhcp_hostname)
602 hn = link->network->dhcp_hostname;
603 else {
604 r = gethostname_strict(&hostname);
605 if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
606 return r;
607
608 hn = hostname;
609 }
610
611 r = sd_dhcp_client_set_hostname(link->dhcp_client, hn);
612 if (r == -EINVAL && hostname)
613 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
614 log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
615 else if (r < 0)
616 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m");
617
618 return 0;
619 }
620
621 static bool promote_secondaries_enabled(const char *ifname) {
622 _cleanup_free_ char *promote_secondaries_sysctl = NULL;
623 char *promote_secondaries_path;
624 int r;
625
626 promote_secondaries_path = strjoina("net/ipv4/conf/", ifname, "/promote_secondaries");
627 r = sysctl_read(promote_secondaries_path, &promote_secondaries_sysctl);
628 if (r < 0) {
629 log_debug_errno(r, "Cannot read sysctl %s", promote_secondaries_path);
630 return false;
631 }
632
633 truncate_nl(promote_secondaries_sysctl);
634 r = parse_boolean(promote_secondaries_sysctl);
635 if (r < 0)
636 log_warning_errno(r, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path, promote_secondaries_sysctl);
637 return r > 0;
638 }
639
640 /* dhcp4_set_promote_secondaries will ensure this interface has
641 * the "promote_secondaries" option in the kernel set. If this sysctl
642 * is not set DHCP will work only as long as the IP address does not
643 * changes between leases. The kernel will remove all secondary IP
644 * addresses of an interface otherwise. The way systemd-network works
645 * is that the new IP of a lease is added as a secondary IP and when
646 * the primary one expires it relies on the kernel to promote the
647 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
648 */
649 int dhcp4_set_promote_secondaries(Link *link) {
650 int r;
651
652 assert(link);
653 assert(link->network);
654 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
655
656 /* check if the kernel has promote_secondaries enabled for our
657 * interface. If it is not globally enabled or enabled for the
658 * specific interface we must either enable it.
659 */
660 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link->ifname))) {
661 char *promote_secondaries_path = NULL;
662
663 log_link_debug(link, "promote_secondaries is unset, setting it");
664 promote_secondaries_path = strjoina("net/ipv4/conf/", link->ifname, "/promote_secondaries");
665 r = sysctl_write(promote_secondaries_path, "1");
666 if (r < 0)
667 log_link_warning_errno(link, r, "cannot set sysctl %s to 1", promote_secondaries_path);
668 return r > 0;
669 }
670
671 return 0;
672 }
673
674 int dhcp4_set_client_identifier(Link *link) {
675 int r;
676
677 assert(link);
678 assert(link->network);
679 assert(link->dhcp_client);
680
681 switch (link->network->dhcp_client_identifier) {
682 case DHCP_CLIENT_ID_DUID: {
683 /* If configured, apply user specified DUID and IAID */
684 const DUID *duid = link_get_duid(link);
685
686 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
687 r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
688 link->network->iaid_set,
689 link->network->iaid,
690 duid->llt_time);
691 else
692 r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
693 link->network->iaid_set,
694 link->network->iaid,
695 duid->type,
696 duid->raw_data_len > 0 ? duid->raw_data : NULL,
697 duid->raw_data_len);
698 if (r < 0)
699 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
700 break;
701 }
702 case DHCP_CLIENT_ID_DUID_ONLY: {
703 /* If configured, apply user specified DUID */
704 const DUID *duid = link_get_duid(link);
705
706 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
707 r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
708 duid->llt_time);
709 else
710 r = sd_dhcp_client_set_duid(link->dhcp_client,
711 duid->type,
712 duid->raw_data_len > 0 ? duid->raw_data : NULL,
713 duid->raw_data_len);
714 if (r < 0)
715 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
716 break;
717 }
718 case DHCP_CLIENT_ID_MAC:
719 r = sd_dhcp_client_set_client_id(link->dhcp_client,
720 ARPHRD_ETHER,
721 (const uint8_t *) &link->mac,
722 sizeof(link->mac));
723 if (r < 0)
724 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m");
725 break;
726 default:
727 assert_not_reached("Unknown client identifier type.");
728 }
729
730 return 0;
731 }
732
733 int dhcp4_configure(Link *link) {
734 int r;
735
736 assert(link);
737 assert(link->network);
738 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
739
740 if (!link->dhcp_client) {
741 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
742 if (r == -ENOMEM)
743 return log_oom();
744 if (r < 0)
745 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
746 }
747
748 r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
749 if (r < 0)
750 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to attach event: %m");
751
752 r = sd_dhcp_client_set_mac(link->dhcp_client,
753 (const uint8_t *) &link->mac,
754 sizeof (link->mac), ARPHRD_ETHER);
755 if (r < 0)
756 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m");
757
758 r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
759 if (r < 0)
760 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m");
761
762 r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
763 if (r < 0)
764 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m");
765
766 r = sd_dhcp_client_set_request_broadcast(link->dhcp_client,
767 link->network->dhcp_broadcast);
768 if (r < 0)
769 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
770
771 if (link->mtu) {
772 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
773 if (r < 0)
774 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m");
775 }
776
777 if (link->network->dhcp_use_mtu) {
778 r = sd_dhcp_client_set_request_option(link->dhcp_client,
779 SD_DHCP_OPTION_INTERFACE_MTU);
780 if (r < 0)
781 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
782 }
783
784 /* NOTE: even if this variable is called "use", it also "sends" PRL
785 * options, maybe there should be a different configuration variable
786 * to send or not route options?. */
787 /* NOTE: when using Anonymize=yes, routes PRL options are sent
788 * by default, so they don't need to be added here. */
789 if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) {
790 r = sd_dhcp_client_set_request_option(link->dhcp_client,
791 SD_DHCP_OPTION_STATIC_ROUTE);
792 if (r < 0)
793 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
794
795 r = sd_dhcp_client_set_request_option(link->dhcp_client,
796 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
797 if (r < 0)
798 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
799 }
800
801 if (link->network->dhcp_use_ntp) {
802 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
803 if (r < 0)
804 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
805 }
806
807 if (link->network->dhcp_use_timezone) {
808 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
809 if (r < 0)
810 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
811 }
812
813 r = dhcp4_set_hostname(link);
814 if (r < 0)
815 return r;
816
817 if (link->network->dhcp_vendor_class_identifier) {
818 r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
819 link->network->dhcp_vendor_class_identifier);
820 if (r < 0)
821 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
822 }
823
824 if (link->network->dhcp_user_class) {
825 r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class);
826 if (r < 0)
827 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m");
828 }
829
830 if (link->network->dhcp_client_port) {
831 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
832 if (r < 0)
833 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
834 }
835
836 if (link->network->dhcp_max_attempts > 0) {
837 r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts);
838 if (r < 0)
839 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m");
840 }
841
842 return dhcp4_set_client_identifier(link);
843 }