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