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