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