]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-dhcp4.c
network: introduce network_verify_sr_iov()
[thirdparty/systemd.git] / src / network / networkd-dhcp4.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
3c9b8860 2
9aa5d8ba 3#include <netinet/in.h>
a75b2117 4#include <netinet/ip.h>
3c9b8860 5#include <linux/if.h>
8f815e8b 6#include <linux/if_arp.h>
3c9b8860 7
7b8d23a9 8#include "escape.h"
b5efdb8a 9#include "alloc-util.h"
cb29c156 10#include "dhcp-client-internal.h"
958b66ea 11#include "hostname-util.h"
64b21ece 12#include "parse-util.h"
3c9b8860 13#include "network-internal.h"
093e3533 14#include "networkd-address.h"
ca5ad760 15#include "networkd-dhcp4.h"
23f53b99
TG
16#include "networkd-link.h"
17#include "networkd-manager.h"
18#include "networkd-network.h"
ca5ad760 19#include "string-table.h"
64b21ece
MV
20#include "string-util.h"
21#include "sysctl-util.h"
7b8d23a9 22#include "web-util.h"
3c9b8860 23
6906794d
YW
24static int dhcp4_update_address(Link *link, bool announce);
25static int dhcp4_remove_all(Link *link);
d03073dd 26
c45fdad6 27static int dhcp4_release_old_lease(Link *link) {
6e537f62 28 Route *route;
6e537f62 29 int k, r = 0;
d03073dd 30
6e537f62 31 assert(link);
d03073dd 32
6e537f62
YW
33 if (!link->dhcp_address_old && set_isempty(link->dhcp_routes_old))
34 return 0;
d03073dd 35
6e537f62 36 log_link_debug(link, "Removing old DHCPv4 address and routes.");
d03073dd 37
d03073dd 38 link_dirty(link);
6e537f62 39
90e74a66 40 SET_FOREACH(route, link->dhcp_routes_old) {
6e537f62
YW
41 k = route_remove(route, link, NULL);
42 if (k < 0)
43 r = k;
44 }
45
46 if (link->dhcp_address_old) {
47 k = address_remove(link->dhcp_address_old, link, NULL);
48 if (k < 0)
49 r = k;
50 }
51
52 return r;
d03073dd
YW
53}
54
c1d3fa29 55static void dhcp4_check_ready(Link *link) {
6e537f62
YW
56 int r;
57
153cf041
YW
58 if (link->network->dhcp_send_decline && !link->dhcp4_address_bind)
59 return;
60
61 if (link->dhcp4_messages > 0)
62 return;
63
64 link->dhcp4_configured = true;
6e537f62 65
153cf041 66 /* New address and routes are configured now. Let's release old lease. */
c45fdad6 67 r = dhcp4_release_old_lease(link);
6e537f62
YW
68 if (r < 0) {
69 link_enter_failed(link);
70 return;
71 }
72
153cf041 73 link_check_ready(link);
c1d3fa29
YW
74}
75
302a796f 76static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3c9b8860
TG
77 int r;
78
79 assert(link);
6cf4a01c 80 assert(link->dhcp4_messages > 0);
3c9b8860 81
313cefa1 82 link->dhcp4_messages--;
3c9b8860 83
3ab7ed3f
YW
84 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
85 return 1;
86
1c4baffc 87 r = sd_netlink_message_get_errno(m);
dd9b10c8
YW
88 if (r == -ENETUNREACH && !link->dhcp4_route_retrying) {
89
90 /* It seems kernel does not support that the prefix route cannot be configured with
91 * route table. Let's once drop the config and reconfigure them later. */
92
81eb5bc5 93 log_link_message_debug_errno(link, m, r, "Could not set DHCPv4 route, retrying later");
dd9b10c8
YW
94 link->dhcp4_route_failed = true;
95 link->manager->dhcp4_prefix_root_cannot_set_table = true;
96 } else if (r < 0 && r != -EEXIST) {
81eb5bc5 97 log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 route");
3c9b8860 98 link_enter_failed(link);
3ab7ed3f 99 return 1;
3c9b8860
TG
100 }
101
153cf041
YW
102 if (link->dhcp4_messages == 0 && link->dhcp4_route_failed) {
103 link->dhcp4_route_failed = false;
104 link->dhcp4_route_retrying = true;
dd9b10c8 105
153cf041
YW
106 r = dhcp4_remove_all(link);
107 if (r < 0)
108 link_enter_failed(link);
109 return 1;
3c9b8860
TG
110 }
111
153cf041
YW
112 dhcp4_check_ready(link);
113
3c9b8860
TG
114 return 1;
115}
116
d6eac9bd
DW
117static int route_scope_from_address(const Route *route, const struct in_addr *self_addr) {
118 assert(route);
119 assert(self_addr);
120
7ed5420a
YW
121 if (in4_addr_is_localhost(&route->dst.in) ||
122 (!in4_addr_is_null(self_addr) && in4_addr_equal(&route->dst.in, self_addr)))
d6eac9bd
DW
123 return RT_SCOPE_HOST;
124 else if (in4_addr_is_null(&route->gw.in))
125 return RT_SCOPE_LINK;
126 else
127 return RT_SCOPE_UNIVERSE;
128}
129
de697db0
YW
130static bool link_prefixroute(Link *link) {
131 return !link->network->dhcp_route_table_set ||
132 link->network->dhcp_route_table == RT_TABLE_MAIN ||
133 link->manager->dhcp4_prefix_root_cannot_set_table;
156ddf8d
YW
134}
135
6e537f62
YW
136static int dhcp_route_configure(Route *route, Link *link) {
137 Route *ret;
d4c52ee5
YW
138 int r;
139
140 assert(route);
d4c52ee5
YW
141 assert(link);
142
6e537f62
YW
143 r = route_configure(route, link, dhcp4_route_handler, &ret);
144 if (r < 0)
145 return log_link_error_errno(link, r, "Failed to set DHCPv4 route: %m");
d4c52ee5
YW
146
147 link->dhcp4_messages++;
148
6e537f62 149 r = set_ensure_put(&link->dhcp_routes, &route_hash_ops, ret);
d4c52ee5 150 if (r < 0)
6e537f62
YW
151 return log_link_error_errno(link, r, "Failed to store DHCPv4 route: %m");
152
153 (void) set_remove(link->dhcp_routes_old, ret);
d4c52ee5 154
d4c52ee5
YW
155 return 0;
156}
157
854a1ccf
YW
158static int link_set_dns_routes(Link *link, const struct in_addr *address) {
159 const struct in_addr *dns;
160 uint32_t table;
161 int i, n, r;
162
163 assert(link);
164 assert(link->dhcp_lease);
165 assert(link->network);
166
a24e12f0
YW
167 if (!link->network->dhcp_use_dns ||
168 !link->network->dhcp_routes_to_dns)
854a1ccf
YW
169 return 0;
170
171 n = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns);
172 if (IN_SET(n, 0, -ENODATA))
173 return 0;
174 if (n < 0)
175 return log_link_warning_errno(link, n, "DHCP error: could not get DNS servers: %m");
176
177 table = link_get_dhcp_route_table(link);
178
179 for (i = 0; i < n; i ++) {
180 _cleanup_(route_freep) Route *route = NULL;
181
182 r = route_new(&route);
183 if (r < 0)
6906794d 184 return log_link_error_errno(link, r, "Could not allocate route: %m");
854a1ccf
YW
185
186 /* Set routes to DNS servers. */
187
188 route->family = AF_INET;
189 route->dst.in = dns[i];
190 route->dst_prefixlen = 32;
191 route->prefsrc.in = *address;
192 route->scope = RT_SCOPE_LINK;
193 route->protocol = RTPROT_DHCP;
194 route->priority = link->network->dhcp_route_metric;
195 route->table = table;
196
6e537f62 197 r = dhcp_route_configure(route, link);
854a1ccf
YW
198 if (r < 0)
199 return log_link_error_errno(link, r, "Could not set route to DNS server: %m");
200 }
201
202 return 0;
203}
204
e723fbd7
ZJS
205static int dhcp_prefix_route_from_lease(
206 const sd_dhcp_lease *lease,
207 uint32_t table,
208 const struct in_addr *address,
209 Route **ret_route) {
210
211 Route *route;
212 struct in_addr netmask;
213 int r;
214
215 r = sd_dhcp_lease_get_netmask((sd_dhcp_lease*) lease, &netmask);
216 if (r < 0)
217 return r;
218
219 r = route_new(&route);
220 if (r < 0)
221 return r;
222
223 route->family = AF_INET;
224 route->dst.in.s_addr = address->s_addr & netmask.s_addr;
225 route->dst_prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
226 route->prefsrc.in = *address;
227 route->scope = RT_SCOPE_LINK;
228 route->protocol = RTPROT_DHCP;
229 route->table = table;
230 *ret_route = route;
231 return 0;
232}
233
3c9b8860 234static int link_set_dhcp_routes(Link *link) {
f8693fc7 235 _cleanup_free_ sd_dhcp_route **static_routes = NULL;
8cdc46e7 236 bool classless_route = false, static_route = false;
f8862395 237 struct in_addr address;
fc1ba79d 238 uint32_t table;
6e537f62 239 Route *rt;
2200c3cf 240 int r, n;
3c9b8860
TG
241
242 assert(link);
0c9b15a3
AJ
243
244 if (!link->dhcp_lease) /* link went down while we configured the IP addresses? */
245 return 0;
246
247 if (!link->network) /* link went down while we configured the IP addresses? */
248 return 0;
964b26fe 249
4e2ef9d9
YW
250 if (!link_has_carrier(link) && !link->network->configure_without_carrier)
251 /* During configuring addresses, the link lost its carrier. As networkd is dropping
252 * the addresses now, let's not configure the routes either. */
253 return 0;
254
6e537f62
YW
255 while ((rt = set_steal_first(link->dhcp_routes))) {
256 r = set_ensure_put(&link->dhcp_routes_old, &route_hash_ops, rt);
257 if (r < 0)
258 return log_link_error_errno(link, r, "Failed to store old DHCPv4 route: %m");
259 }
d4c52ee5 260
bdb9f580 261 table = link_get_dhcp_route_table(link);
fc1ba79d 262
b23aec0d
DW
263 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
264 if (r < 0)
265 return log_link_warning_errno(link, r, "DHCP error: could not get address: %m");
266
de697db0 267 if (!link_prefixroute(link)) {
156ddf8d 268 _cleanup_(route_freep) Route *prefix_route = NULL;
156ddf8d 269
e723fbd7 270 r = dhcp_prefix_route_from_lease(link->dhcp_lease, table, &address, &prefix_route);
156ddf8d 271 if (r < 0)
6906794d 272 return log_link_error_errno(link, r, "Could not create prefix route: %m");
156ddf8d 273
6e537f62 274 r = dhcp_route_configure(prefix_route, link);
156ddf8d
YW
275 if (r < 0)
276 return log_link_error_errno(link, r, "Could not set prefix route: %m");
277 }
278
5f04a209 279 n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes);
599c44e6
YW
280 if (n == -ENODATA)
281 log_link_debug_errno(link, n, "DHCP: No routes received from DHCP server: %m");
282 else if (n < 0)
2200c3cf 283 return log_link_error_errno(link, n, "DHCP: could not get routes: %m");
5f04a209 284
2200c3cf 285 for (int i = 0; i < n; i++) {
3476951c
TH
286 switch (sd_dhcp_route_get_option(static_routes[i])) {
287 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE:
8cdc46e7 288 classless_route = true;
3476951c
TH
289 break;
290 case SD_DHCP_OPTION_STATIC_ROUTE:
8cdc46e7 291 static_route = true;
3476951c
TH
292 break;
293 }
8cdc46e7
SS
294 }
295
ad098b14 296 if (link->network->dhcp_use_routes) {
2200c3cf
YW
297 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
298 * the DHCP client MUST ignore the Static Routes option. */
299 if (classless_route && static_route)
300 log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option");
301
302 for (int i = 0; i < n; i++) {
ad098b14
SS
303 _cleanup_(route_freep) Route *route = NULL;
304
ad098b14
SS
305 if (classless_route &&
306 sd_dhcp_route_get_option(static_routes[i]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE)
307 continue;
308
309 r = route_new(&route);
310 if (r < 0)
311 return log_link_error_errno(link, r, "Could not allocate route: %m");
312
313 route->family = AF_INET;
314 route->protocol = RTPROT_DHCP;
315 assert_se(sd_dhcp_route_get_gateway(static_routes[i], &route->gw.in) >= 0);
316 assert_se(sd_dhcp_route_get_destination(static_routes[i], &route->dst.in) >= 0);
317 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen) >= 0);
318 route->priority = link->network->dhcp_route_metric;
319 route->table = table;
320 route->mtu = link->network->dhcp_route_mtu;
321 route->scope = route_scope_from_address(route, &address);
322 if (IN_SET(route->scope, RT_SCOPE_LINK, RT_SCOPE_UNIVERSE))
323 route->prefsrc.in = address;
324
325 if (set_contains(link->dhcp_routes, route))
326 continue;
327
6e537f62 328 r = dhcp_route_configure(route, link);
ad098b14
SS
329 if (r < 0)
330 return log_link_error_errno(link, r, "Could not set route: %m");
331 }
5f04a209
SS
332 }
333
244490f5 334 if (link->network->dhcp_use_gateway) {
2200c3cf
YW
335 const struct in_addr *router;
336
244490f5
DS
337 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
338 if (IN_SET(r, 0, -ENODATA))
339 log_link_info(link, "DHCP: No gateway received from DHCP server.");
340 else if (r < 0)
2200c3cf 341 return log_link_error_errno(link, r, "DHCP error: could not get gateway: %m");
244490f5
DS
342 else if (in4_addr_is_null(&router[0]))
343 log_link_info(link, "DHCP: Received gateway is null.");
2200c3cf
YW
344 else if (classless_route)
345 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
346 * a Router option, the DHCP client MUST ignore the Router option. */
347 log_link_warning(link, "Classless static routes received from DHCP server: ignoring router option");
348 else {
244490f5
DS
349 _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL;
350
351 r = route_new(&route_gw);
352 if (r < 0)
6906794d 353 return log_link_error_errno(link, r, "Could not allocate route: %m");
244490f5
DS
354
355 /* The dhcp netmask may mask out the gateway. Add an explicit
356 * route for the gw host so that we can route no matter the
357 * netmask or existing kernel route tables. */
358 route_gw->family = AF_INET;
359 route_gw->dst.in = router[0];
360 route_gw->dst_prefixlen = 32;
361 route_gw->prefsrc.in = address;
362 route_gw->scope = RT_SCOPE_LINK;
363 route_gw->protocol = RTPROT_DHCP;
364 route_gw->priority = link->network->dhcp_route_metric;
365 route_gw->table = table;
366 route_gw->mtu = link->network->dhcp_route_mtu;
367
6e537f62 368 r = dhcp_route_configure(route_gw, link);
244490f5
DS
369 if (r < 0)
370 return log_link_error_errno(link, r, "Could not set host route: %m");
3c9b8860 371
244490f5
DS
372 r = route_new(&route);
373 if (r < 0)
374 return log_link_error_errno(link, r, "Could not allocate route: %m");
860e636c 375
244490f5
DS
376 route->family = AF_INET;
377 route->gw.in = router[0];
378 route->prefsrc.in = address;
379 route->protocol = RTPROT_DHCP;
380 route->priority = link->network->dhcp_route_metric;
381 route->table = table;
382 route->mtu = link->network->dhcp_route_mtu;
3c9b8860 383
6e537f62 384 r = dhcp_route_configure(route, link);
244490f5
DS
385 if (r < 0)
386 return log_link_error_errno(link, r, "Could not set router: %m");
3c9b8860 387
2a54a044 388 HASHMAP_FOREACH(rt, link->network->routes_by_section) {
2200c3cf
YW
389 if (!rt->gateway_from_dhcp)
390 continue;
1985c54f 391
2200c3cf
YW
392 if (rt->family != AF_INET)
393 continue;
1985c54f 394
2200c3cf 395 rt->gw.in = router[0];
1985c54f 396
2200c3cf
YW
397 r = dhcp_route_configure(rt, link);
398 if (r < 0)
399 return log_link_error_errno(link, r, "Could not set gateway: %m");
400 }
244490f5 401 }
1985c54f
YW
402 }
403
854a1ccf 404 return link_set_dns_routes(link, &address);
3c9b8860
TG
405}
406
7fa472f9
YW
407static int dhcp_reset_mtu(Link *link) {
408 uint16_t mtu;
409 int r;
410
411 assert(link);
412
413 if (!link->network->dhcp_use_mtu)
414 return 0;
415
416 r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
46b875fb
YW
417 if (r == -ENODATA)
418 return 0;
7fa472f9 419 if (r < 0)
46b875fb 420 return log_link_error_errno(link, r, "DHCP error: failed to get MTU from lease: %m");
7fa472f9
YW
421
422 if (link->original_mtu == mtu)
423 return 0;
424
425 r = link_set_mtu(link, link->original_mtu);
46b875fb
YW
426 if (r < 0)
427 return log_link_error_errno(link, r, "DHCP error: could not reset MTU: %m");
3c9b8860 428
7fa472f9
YW
429 return 0;
430}
431
432static int dhcp_reset_hostname(Link *link) {
433 const char *hostname;
434 int r;
435
436 assert(link);
437
438 if (!link->network->dhcp_use_hostname)
439 return 0;
440
441 hostname = link->network->dhcp_hostname;
442 if (!hostname)
443 (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
444
445 if (!hostname)
446 return 0;
447
448 /* If a hostname was set due to the lease, then unset it now. */
449 r = manager_set_hostname(link->manager, NULL);
450 if (r < 0)
451 return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m");
452
453 return 0;
454}
455
6906794d
YW
456static int dhcp4_remove_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
457 int r;
458
459 assert(m);
460 assert(link);
461 assert(link->dhcp4_remove_messages > 0);
462
463 link->dhcp4_remove_messages--;
464
465 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
466 return 1;
467
468 r = sd_netlink_message_get_errno(m);
469 if (r < 0 && r != -ESRCH)
470 log_link_message_warning_errno(link, m, r, "Failed to remove DHCPv4 route, ignoring");
471
472 if (link->dhcp4_remove_messages == 0) {
473 r = dhcp4_update_address(link, false);
474 if (r < 0)
475 link_enter_failed(link);
476 }
477
478 return 1;
479}
480
481static int dhcp4_remove_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
482 int r;
483
484 assert(m);
485 assert(link);
486 assert(link->dhcp4_remove_messages > 0);
487
488 link->dhcp4_remove_messages--;
489
490 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
491 return 1;
492
493 r = sd_netlink_message_get_errno(m);
494 if (r < 0 && r != -EADDRNOTAVAIL)
495 log_link_message_warning_errno(link, m, r, "Failed to remove DHCPv4 address, ignoring");
496 else
497 (void) manager_rtnl_process_address(rtnl, m, link->manager);
498
499 if (link->dhcp4_remove_messages == 0) {
500 r = dhcp4_update_address(link, false);
501 if (r < 0)
502 link_enter_failed(link);
503 }
504
505 return 1;
506}
507
508static int dhcp4_remove_all(Link *link) {
6e537f62 509 Route *route;
6e537f62 510 int k, r = 0;
6906794d
YW
511
512 assert(link);
6906794d 513
90e74a66 514 SET_FOREACH(route, link->dhcp_routes) {
6e537f62
YW
515 k = route_remove(route, link, dhcp4_remove_route_handler);
516 if (k < 0)
517 r = k;
518 else
519 link->dhcp4_remove_messages++;
520 }
6906794d 521
6e537f62
YW
522 if (link->dhcp_address) {
523 k = address_remove(link->dhcp_address, link, dhcp4_remove_address_handler);
524 if (k < 0)
525 r = k;
526 else
527 link->dhcp4_remove_messages++;
528 }
6906794d 529
6e537f62 530 return r;
6906794d
YW
531}
532
7fa472f9 533static int dhcp_lease_lost(Link *link) {
2ac7eec3 534 int k, r = 0;
7fa472f9
YW
535
536 assert(link);
537 assert(link->dhcp_lease);
538
f8c2e4b9 539 log_link_info(link, "DHCP lease lost");
7fa472f9
YW
540
541 link->dhcp4_configured = false;
542
75be72d1 543 /* dhcp_lease_lost() may be called during renewing IP address. */
c45fdad6 544 k = dhcp4_release_old_lease(link);
6e537f62
YW
545 if (k < 0)
546 r = k;
75be72d1 547
6e537f62
YW
548 k = dhcp4_remove_all(link);
549 if (k < 0)
550 r = k;
6906794d 551
6e537f62
YW
552 k = dhcp_reset_mtu(link);
553 if (k < 0)
554 r = k;
6906794d 555
6e537f62
YW
556 k = dhcp_reset_hostname(link);
557 if (k < 0)
558 r = k;
7fa472f9 559
3c9b8860 560 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
6a3e5f6a 561 link_dirty(link);
3c9b8860 562
6e537f62 563 return r;
3c9b8860
TG
564}
565
0f3ff4ea
SS
566static void dhcp_address_on_acd(sd_ipv4acd *acd, int event, void *userdata) {
567 _cleanup_free_ char *pretty = NULL;
568 union in_addr_union address = {};
569 Link *link;
570 int r;
571
572 assert(acd);
573 assert(userdata);
574
575 link = userdata;
576
577 switch (event) {
578 case SD_IPV4ACD_EVENT_STOP:
579 log_link_debug(link, "Stopping ACD client for DHCP4...");
580 return;
581
582 case SD_IPV4ACD_EVENT_BIND:
583 if (DEBUG_LOGGING) {
584 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address.in);
585 (void) in_addr_to_string(AF_INET, &address, &pretty);
586 log_link_debug(link, "Successfully claimed DHCP4 address %s", strna(pretty));
587 }
153cf041 588 link->dhcp4_address_bind = true;
c1d3fa29 589 dhcp4_check_ready(link);
0f3ff4ea
SS
590 break;
591
592 case SD_IPV4ACD_EVENT_CONFLICT:
593 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address.in);
594 (void) in_addr_to_string(AF_INET, &address, &pretty);
595 log_link_warning(link, "DAD conflict. Dropping DHCP4 address %s", strna(pretty));
596
f766d9af
YW
597 r = sd_dhcp_client_send_decline(link->dhcp_client);
598 if (r < 0)
599 log_link_warning_errno(link, r, "Failed to send DHCP DECLINE, ignoring: %m");
0f3ff4ea
SS
600
601 if (link->dhcp_lease) {
602 r = dhcp_lease_lost(link);
603 if (r < 0)
604 link_enter_failed(link);
605 }
606 break;
607
608 default:
609 assert_not_reached("Invalid IPv4ACD event.");
610 }
611
dce1cd41 612 (void) sd_ipv4acd_stop(acd);
0f3ff4ea
SS
613
614 return;
615}
616
617static int configure_dhcpv4_duplicate_address_detection(Link *link) {
618 int r;
619
620 assert(link);
621
622 r = sd_ipv4acd_new(&link->network->dhcp_acd);
623 if (r < 0)
624 return r;
625
626 r = sd_ipv4acd_attach_event(link->network->dhcp_acd, NULL, 0);
627 if (r < 0)
628 return r;
629
630 r = sd_ipv4acd_set_ifindex(link->network->dhcp_acd, link->ifindex);
631 if (r < 0)
632 return r;
633
634 r = sd_ipv4acd_set_mac(link->network->dhcp_acd, &link->mac);
635 if (r < 0)
636 return r;
637
638 return 0;
639}
640
153cf041
YW
641static int dhcp4_start_acd(Link *link) {
642 union in_addr_union addr;
687b3bc6 643 struct in_addr old;
153cf041
YW
644 int r;
645
646 if (!link->network->dhcp_send_decline)
647 return 0;
648
649 if (!link->dhcp_lease)
650 return 0;
651
5acf54a0
YW
652 (void) sd_ipv4acd_stop(link->network->dhcp_acd);
653
153cf041
YW
654 link->dhcp4_address_bind = false;
655
656 r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr.in);
657 if (r < 0)
658 return r;
659
687b3bc6
YW
660 r = sd_ipv4acd_get_address(link->network->dhcp_acd, &old);
661 if (r < 0)
662 return r;
663
153cf041
YW
664 r = sd_ipv4acd_set_address(link->network->dhcp_acd, &addr.in);
665 if (r < 0)
666 return r;
667
668 r = sd_ipv4acd_set_callback(link->network->dhcp_acd, dhcp_address_on_acd, link);
669 if (r < 0)
670 return r;
671
672 if (DEBUG_LOGGING) {
673 _cleanup_free_ char *pretty = NULL;
674
675 (void) in_addr_to_string(AF_INET, &addr, &pretty);
676 log_link_debug(link, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty));
677 }
678
687b3bc6 679 r = sd_ipv4acd_start(link->network->dhcp_acd, !in4_addr_equal(&addr.in, &old));
153cf041
YW
680 if (r < 0)
681 return r;
682
683 return 1;
684}
685
c45fdad6
YW
686static int dhcp4_address_ready_callback(Address *address) {
687 Link *link;
688 int r;
689
690 assert(address);
691
692 link = address->link;
693
694 /* Do not call this again. */
695 address->callback = NULL;
696
697 r = link_set_dhcp_routes(link);
698 if (r < 0)
699 return r;
700
701 /* Reconfigure static routes as kernel may remove some routes when lease expires. */
141318f7 702 r = link_set_routes(link);
c45fdad6
YW
703 if (r < 0)
704 return r;
705
706 r = dhcp4_start_acd(link);
707 if (r < 0)
69e3234d 708 return log_link_error_errno(link, r, "Failed to start IPv4ACD for DHCP4 address: %m");
c45fdad6
YW
709
710 dhcp4_check_ready(link);
711 return 0;
712}
713
302a796f 714static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3c9b8860
TG
715 int r;
716
717 assert(link);
718
3ab7ed3f
YW
719 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
720 return 1;
721
1c4baffc 722 r = sd_netlink_message_get_errno(m);
3c9b8860 723 if (r < 0 && r != -EEXIST) {
5ecb131d 724 log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 address");
3c9b8860 725 link_enter_failed(link);
3ab7ed3f 726 return 1;
4ff296b0
YW
727 } else if (r >= 0)
728 (void) manager_rtnl_process_address(rtnl, m, link->manager);
3c9b8860 729
c45fdad6
YW
730 if (address_is_ready(link->dhcp_address)) {
731 r = dhcp4_address_ready_callback(link->dhcp_address);
732 if (r < 0) {
733 link_enter_failed(link);
734 return 1;
735 }
736 } else
737 link->dhcp_address->callback = dhcp4_address_ready_callback;
0f3ff4ea 738
3c9b8860
TG
739 return 1;
740}
741
6906794d 742static int dhcp4_update_address(Link *link, bool announce) {
8e766630 743 _cleanup_(address_freep) Address *addr = NULL;
6906794d
YW
744 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
745 struct in_addr address, netmask;
3c9b8860 746 unsigned prefixlen;
6e537f62 747 Address *ret;
3c9b8860
TG
748 int r;
749
6906794d
YW
750 assert(link);
751 assert(link->network);
752
753 if (!link->dhcp_lease)
754 return 0;
755
756 link_set_state(link, LINK_STATE_CONFIGURING);
757 link->dhcp4_configured = false;
3c9b8860 758
141318f7
YW
759 /* address_handler calls link_set_routes() and link_set_nexthop(). Before they are called, the
760 * related flags must be cleared. Otherwise, the link becomes configured state before routes
761 * are configured. */
0c816fcc
YW
762 link->static_routes_configured = false;
763 link->static_nexthops_configured = false;
764
6906794d
YW
765 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
766 if (r < 0)
767 return log_link_warning_errno(link, r, "DHCP error: no address: %m");
768
769 r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
770 if (r < 0)
771 return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
772
773 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
774 r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
775 if (r < 0)
776 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
777 }
778
779 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
780
781 if (announce) {
782 const struct in_addr *router;
783
784 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
785 if (r < 0 && r != -ENODATA)
786 return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
787
788 if (r > 0 && !in4_addr_is_null(&router[0]))
789 log_struct(LOG_INFO,
790 LOG_LINK_INTERFACE(link),
791 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
792 ADDRESS_FMT_VAL(address),
793 prefixlen,
794 ADDRESS_FMT_VAL(router[0])),
795 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
796 "PREFIXLEN=%u", prefixlen,
797 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router[0]));
798 else
799 log_struct(LOG_INFO,
800 LOG_LINK_INTERFACE(link),
801 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u",
802 ADDRESS_FMT_VAL(address),
803 prefixlen),
804 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
805 "PREFIXLEN=%u", prefixlen);
806 }
3c9b8860 807
f0213e37 808 r = address_new(&addr);
3c9b8860 809 if (r < 0)
6906794d 810 return log_oom();
3c9b8860
TG
811
812 addr->family = AF_INET;
6906794d 813 addr->in_addr.in.s_addr = address.s_addr;
3c9b8860
TG
814 addr->cinfo.ifa_prefered = lifetime;
815 addr->cinfo.ifa_valid = lifetime;
816 addr->prefixlen = prefixlen;
6906794d 817 addr->broadcast.s_addr = address.s_addr | ~netmask.s_addr;
de697db0 818 addr->prefix_route = link_prefixroute(link);
3c9b8860 819
66669078
TG
820 /* allow reusing an existing address and simply update its lifetime
821 * in case it already exists */
6e537f62 822 r = address_configure(addr, link, dhcp4_address_handler, true, &ret);
3c9b8860 823 if (r < 0)
6e537f62
YW
824 return log_link_error_errno(link, r, "Failed to set DHCPv4 address: %m");
825
826 if (!address_equal(link->dhcp_address, ret))
827 link->dhcp_address_old = link->dhcp_address;
828 link->dhcp_address = ret;
3c9b8860
TG
829
830 return 0;
831}
832
833static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
834 sd_dhcp_lease *lease;
3c9b8860
TG
835 int r;
836
837 assert(link);
838 assert(client);
3c9b8860
TG
839
840 r = sd_dhcp_client_get_lease(client, &lease);
f6b8196f
LP
841 if (r < 0)
842 return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
3c9b8860
TG
843
844 sd_dhcp_lease_unref(link->dhcp_lease);
e6b18ffa 845 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 846 link_dirty(link);
3c9b8860 847
6906794d 848 return dhcp4_update_address(link, false);
3c9b8860
TG
849}
850
851static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
852 sd_dhcp_lease *lease;
3c9b8860
TG
853 int r;
854
855 assert(client);
856 assert(link);
857
858 r = sd_dhcp_client_get_lease(client, &lease);
f2341e0a 859 if (r < 0)
f6b8196f 860 return log_link_error_errno(link, r, "DHCP error: No lease: %m");
3c9b8860 861
6906794d 862 sd_dhcp_lease_unref(link->dhcp_lease);
e6b18ffa 863 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 864 link_dirty(link);
3c9b8860 865
27cb34f5 866 if (link->network->dhcp_use_mtu) {
3c9b8860
TG
867 uint16_t mtu;
868
869 r = sd_dhcp_lease_get_mtu(lease, &mtu);
870 if (r >= 0) {
933c70a0 871 r = link_set_mtu(link, mtu);
3c9b8860 872 if (r < 0)
f2341e0a 873 log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu);
3c9b8860
TG
874 }
875 }
876
27cb34f5 877 if (link->network->dhcp_use_hostname) {
2de2abad
LB
878 const char *dhcpname = NULL;
879 _cleanup_free_ char *hostname = NULL;
3c9b8860 880
27cb34f5 881 if (link->network->dhcp_hostname)
2de2abad 882 dhcpname = link->network->dhcp_hostname;
dce391e7 883 else
2de2abad
LB
884 (void) sd_dhcp_lease_get_hostname(lease, &dhcpname);
885
886 if (dhcpname) {
887 r = shorten_overlong(dhcpname, &hostname);
888 if (r < 0)
889 log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
890 if (r == 1)
5238e957 891 log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
2de2abad 892 }
a7d0ef44 893
dce391e7 894 if (hostname) {
59eb33e0 895 r = manager_set_hostname(link->manager, hostname);
3c9b8860 896 if (r < 0)
f2341e0a 897 log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
3c9b8860
TG
898 }
899 }
900
27cb34f5 901 if (link->network->dhcp_use_timezone) {
21b80ad1
LP
902 const char *tz = NULL;
903
904 (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
905
906 if (tz) {
59eb33e0 907 r = manager_set_timezone(link->manager, tz);
21b80ad1
LP
908 if (r < 0)
909 log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz);
910 }
911 }
912
6906794d
YW
913 if (link->dhcp4_remove_messages == 0) {
914 r = dhcp4_update_address(link, true);
915 if (r < 0)
916 return r;
917 } else
918 log_link_debug(link,
919 "The link has previously assigned DHCPv4 address or routes. "
920 "The newly assigned address and routes will set up after old ones are removed.");
3c9b8860
TG
921
922 return 0;
923}
8bc17bb3 924
d03073dd
YW
925static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) {
926 int r;
927
d03073dd 928 r = dhcp_lease_acquired(client, link);
6e537f62 929 if (r < 0)
d03073dd 930 (void) dhcp_lease_lost(link);
d03073dd 931
6e537f62 932 return r;
d03073dd
YW
933}
934
6b000af4 935static int dhcp_server_is_deny_listed(Link *link, sd_dhcp_client *client) {
727b5734
SS
936 sd_dhcp_lease *lease;
937 struct in_addr addr;
938 int r;
939
940 assert(link);
941 assert(link->network);
942 assert(client);
943
944 r = sd_dhcp_client_get_lease(client, &lease);
945 if (r < 0)
946 return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
947
948 r = sd_dhcp_lease_get_server_identifier(lease, &addr);
949 if (r < 0)
0da425df 950 return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
727b5734 951
6b000af4 952 if (set_contains(link->network->dhcp_deny_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
727b5734
SS
953 log_struct(LOG_DEBUG,
954 LOG_LINK_INTERFACE(link),
0da425df 955 LOG_LINK_MESSAGE(link, "DHCPv4 IP '%u.%u.%u.%u' found in deny-listed IP addresses, ignoring offer",
727b5734
SS
956 ADDRESS_FMT_VAL(addr)));
957 return true;
958 }
959
960 return false;
961}
962
98ebef62
SS
963static int dhcp_server_is_allow_listed(Link *link, sd_dhcp_client *client) {
964 sd_dhcp_lease *lease;
965 struct in_addr addr;
966 int r;
967
968 assert(link);
969 assert(link->network);
970 assert(client);
971
972 r = sd_dhcp_client_get_lease(client, &lease);
973 if (r < 0)
974 return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
975
976 r = sd_dhcp_lease_get_server_identifier(lease, &addr);
977 if (r < 0)
0da425df 978 return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
98ebef62
SS
979
980 if (set_contains(link->network->dhcp_allow_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
981 log_struct(LOG_DEBUG,
982 LOG_LINK_INTERFACE(link),
0da425df 983 LOG_LINK_MESSAGE(link, "DHCPv4 IP '%u.%u.%u.%u' found in allow-listed IP addresses, accepting offer",
98ebef62
SS
984 ADDRESS_FMT_VAL(addr)));
985 return true;
986 }
987
988 return false;
989}
990
727b5734 991static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
3c9b8860 992 Link *link = userdata;
86e2be7b 993 int r;
3c9b8860
TG
994
995 assert(link);
996 assert(link->network);
997 assert(link->manager);
998
999 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
727b5734 1000 return 0;
3c9b8860
TG
1001
1002 switch (event) {
03748142 1003 case SD_DHCP_CLIENT_EVENT_STOP:
8bc17bb3 1004
910feb78 1005 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4)) {
8bc17bb3
SS
1006 assert(link->ipv4ll);
1007
1008 log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address");
1009
1010 r = sd_ipv4ll_start(link->ipv4ll);
727b5734
SS
1011 if (r < 0)
1012 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
8bc17bb3
SS
1013 }
1014
7da377ef 1015 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
efdb62df
YW
1016 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1017 return 0;
1018 }
1019
efdb62df 1020 if (link->dhcp_lease) {
f766d9af
YW
1021 if (link->network->dhcp_send_release) {
1022 r = sd_dhcp_client_send_release(client);
1023 if (r < 0)
1024 log_link_warning_errno(link, r, "Failed to send DHCP RELEASE, ignoring: %m");
1025 }
8bea7e70 1026
efdb62df
YW
1027 r = dhcp_lease_lost(link);
1028 if (r < 0) {
1029 link_enter_failed(link);
1030 return r;
1031 }
1032 }
1033
1034 break;
8bc17bb3 1035 case SD_DHCP_CLIENT_EVENT_EXPIRED:
7da377ef 1036 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
efdb62df 1037 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
727b5734 1038 return 0;
3c9b8860
TG
1039 }
1040
1041 if (link->dhcp_lease) {
1042 r = dhcp_lease_lost(link);
1043 if (r < 0) {
1044 link_enter_failed(link);
727b5734 1045 return r;
3c9b8860
TG
1046 }
1047 }
1048
d03073dd
YW
1049 break;
1050 case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
1051 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
1052 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1053 return 0;
1054 }
1055
1056 r = dhcp_lease_ip_change(client, link);
1057 if (r < 0) {
1058 link_enter_failed(link);
1059 return r;
3c9b8860
TG
1060 }
1061
1062 break;
03748142 1063 case SD_DHCP_CLIENT_EVENT_RENEW:
3c9b8860
TG
1064 r = dhcp_lease_renew(client, link);
1065 if (r < 0) {
1066 link_enter_failed(link);
727b5734 1067 return r;
3c9b8860
TG
1068 }
1069 break;
03748142 1070 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
3c9b8860
TG
1071 r = dhcp_lease_acquired(client, link);
1072 if (r < 0) {
1073 link_enter_failed(link);
727b5734 1074 return r;
3c9b8860
TG
1075 }
1076 break;
727b5734 1077 case SD_DHCP_CLIENT_EVENT_SELECTING:
98ebef62
SS
1078 if (!set_isempty(link->network->dhcp_allow_listed_ip)) {
1079 r = dhcp_server_is_allow_listed(link, client);
1080 if (r < 0)
1081 return r;
1082 if (r == 0)
1083 return -ENOMSG;
1084 } else {
1085 r = dhcp_server_is_deny_listed(link, client);
1086 if (r < 0)
1087 return r;
1088 if (r != 0)
1089 return -ENOMSG;
1090 }
727b5734 1091 break;
3c9b8860
TG
1092 default:
1093 if (event < 0)
f6b8196f 1094 log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
3c9b8860 1095 else
f6b8196f 1096 log_link_warning(link, "DHCP unknown event: %i", event);
3c9b8860
TG
1097 break;
1098 }
1099
727b5734 1100 return 0;
3c9b8860
TG
1101}
1102
7192bb81
LP
1103static int dhcp4_set_hostname(Link *link) {
1104 _cleanup_free_ char *hostname = NULL;
1105 const char *hn;
1106 int r;
1107
1108 assert(link);
1109
1110 if (!link->network->dhcp_send_hostname)
1111 hn = NULL;
1112 else if (link->network->dhcp_hostname)
1113 hn = link->network->dhcp_hostname;
1114 else {
1115 r = gethostname_strict(&hostname);
1116 if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
1117 return r;
1118
1119 hn = hostname;
1120 }
1121
a8494759
YW
1122 r = sd_dhcp_client_set_hostname(link->dhcp_client, hn);
1123 if (r == -EINVAL && hostname)
1124 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1125 log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1126 else if (r < 0)
1127 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m");
1128
1129 return 0;
7192bb81
LP
1130}
1131
64b21ece 1132static bool promote_secondaries_enabled(const char *ifname) {
3f550c31
HV
1133 _cleanup_free_ char *promote_secondaries_sysctl = NULL;
1134 char *promote_secondaries_path;
64b21ece
MV
1135 int r;
1136
1137 promote_secondaries_path = strjoina("net/ipv4/conf/", ifname, "/promote_secondaries");
1138 r = sysctl_read(promote_secondaries_path, &promote_secondaries_sysctl);
1139 if (r < 0) {
1140 log_debug_errno(r, "Cannot read sysctl %s", promote_secondaries_path);
1141 return false;
1142 }
1143
1144 truncate_nl(promote_secondaries_sysctl);
1145 r = parse_boolean(promote_secondaries_sysctl);
1146 if (r < 0)
1147 log_warning_errno(r, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path, promote_secondaries_sysctl);
1148 return r > 0;
1149}
1150
1151/* dhcp4_set_promote_secondaries will ensure this interface has
1152 * the "promote_secondaries" option in the kernel set. If this sysctl
1153 * is not set DHCP will work only as long as the IP address does not
1154 * changes between leases. The kernel will remove all secondary IP
1155 * addresses of an interface otherwise. The way systemd-network works
1156 * is that the new IP of a lease is added as a secondary IP and when
1157 * the primary one expires it relies on the kernel to promote the
1158 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1159 */
1160int dhcp4_set_promote_secondaries(Link *link) {
1161 int r;
1162
1163 assert(link);
1164 assert(link->network);
1165 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
1166
1167 /* check if the kernel has promote_secondaries enabled for our
1168 * interface. If it is not globally enabled or enabled for the
1169 * specific interface we must either enable it.
1170 */
8e1a7253 1171 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link->ifname))) {
64b21ece
MV
1172 char *promote_secondaries_path = NULL;
1173
1174 log_link_debug(link, "promote_secondaries is unset, setting it");
1175 promote_secondaries_path = strjoina("net/ipv4/conf/", link->ifname, "/promote_secondaries");
1176 r = sysctl_write(promote_secondaries_path, "1");
1177 if (r < 0)
1178 log_link_warning_errno(link, r, "cannot set sysctl %s to 1", promote_secondaries_path);
1179 return r > 0;
1180 }
1181
1182 return 0;
1183}
1184
fff1f40c
YW
1185int dhcp4_set_client_identifier(Link *link) {
1186 int r;
1187
1188 assert(link);
1189 assert(link->network);
1190 assert(link->dhcp_client);
1191
1192 switch (link->network->dhcp_client_identifier) {
1193 case DHCP_CLIENT_ID_DUID: {
0cf7c3fd 1194 /* If configured, apply user specified DUID and IAID */
fff1f40c
YW
1195 const DUID *duid = link_get_duid(link);
1196
0cf7c3fd
YW
1197 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1198 r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
8217ed5e 1199 link->network->iaid_set,
0cf7c3fd
YW
1200 link->network->iaid,
1201 duid->llt_time);
1202 else
1203 r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
8217ed5e 1204 link->network->iaid_set,
0cf7c3fd
YW
1205 link->network->iaid,
1206 duid->type,
1207 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1208 duid->raw_data_len);
fff1f40c 1209 if (r < 0)
0cf7c3fd 1210 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
fff1f40c
YW
1211 break;
1212 }
1213 case DHCP_CLIENT_ID_DUID_ONLY: {
1214 /* If configured, apply user specified DUID */
1215 const DUID *duid = link_get_duid(link);
1216
0cf7c3fd
YW
1217 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1218 r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
89b3fa66 1219 duid->llt_time);
0cf7c3fd
YW
1220 else
1221 r = sd_dhcp_client_set_duid(link->dhcp_client,
1222 duid->type,
1223 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1224 duid->raw_data_len);
fff1f40c
YW
1225 if (r < 0)
1226 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
1227 break;
1228 }
1229 case DHCP_CLIENT_ID_MAC:
1230 r = sd_dhcp_client_set_client_id(link->dhcp_client,
1231 ARPHRD_ETHER,
1232 (const uint8_t *) &link->mac,
1233 sizeof(link->mac));
1234 if (r < 0)
1235 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m");
1236 break;
1237 default:
1238 assert_not_reached("Unknown client identifier type.");
1239 }
1240
1241 return 0;
1242}
1243
3c9b8860 1244int dhcp4_configure(Link *link) {
cb29c156 1245 sd_dhcp_option *send_option;
5bc945be 1246 void *request_options;
3c9b8860
TG
1247 int r;
1248
1249 assert(link);
1250 assert(link->network);
e0ee46f2 1251 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
3c9b8860 1252
0bc70f1d 1253 if (!link->dhcp_client) {
db3d2358 1254 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
1f6860d9
YW
1255 if (r == -ENOMEM)
1256 return log_oom();
0bc70f1d 1257 if (r < 0)
1f6860d9 1258 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
3c9b8860 1259
08c588d1
YW
1260 r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
1261 if (r < 0)
1262 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to attach event: %m");
1263 }
3c9b8860 1264
76253e73
DW
1265 r = sd_dhcp_client_set_mac(link->dhcp_client,
1266 (const uint8_t *) &link->mac,
1267 sizeof (link->mac), ARPHRD_ETHER);
3c9b8860 1268 if (r < 0)
1f6860d9 1269 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m");
3c9b8860 1270
2f8e7633 1271 r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
3c9b8860 1272 if (r < 0)
1f6860d9 1273 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m");
3c9b8860
TG
1274
1275 r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
1276 if (r < 0)
1f6860d9 1277 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m");
3c9b8860
TG
1278
1279 r = sd_dhcp_client_set_request_broadcast(link->dhcp_client,
1280 link->network->dhcp_broadcast);
1281 if (r < 0)
1f6860d9 1282 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
3c9b8860
TG
1283
1284 if (link->mtu) {
1285 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
1286 if (r < 0)
1f6860d9 1287 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m");
3c9b8860
TG
1288 }
1289
27cb34f5 1290 if (link->network->dhcp_use_mtu) {
39745a5a 1291 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1292 SD_DHCP_OPTION_INTERFACE_MTU);
39745a5a 1293 if (r < 0)
1f6860d9 1294 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
3c9b8860
TG
1295 }
1296
5e77a146 1297 /* NOTE: even if this variable is called "use", it also "sends" PRL
1298 * options, maybe there should be a different configuration variable
1299 * to send or not route options?. */
28522b0d 1300 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1301 * by default, so they don't need to be added here. */
5e77a146 1302 if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) {
3c9b8860 1303 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1304 SD_DHCP_OPTION_STATIC_ROUTE);
3c9b8860 1305 if (r < 0)
1f6860d9
YW
1306 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1307
3c9b8860 1308 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1309 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
7d6884b6 1310 if (r < 0)
1f6860d9 1311 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
3c9b8860
TG
1312 }
1313
150d3b8e
YW
1314 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO && !link->network->dhcp_anonymize) {
1315 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST);
1316 if (r < 0)
1317 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1318 }
1319
ead36ce6 1320 if (link->network->dhcp_use_ntp) {
1321 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
1322 if (r < 0)
1f6860d9 1323 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
ead36ce6 1324 }
4b7b5abb 1325
299d578f
SS
1326 if (link->network->dhcp_use_sip) {
1327 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_SIP_SERVER);
1328 if (r < 0)
1329 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1330 }
1331
89573b37 1332 if (link->network->dhcp_use_timezone) {
1333 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
1334 if (r < 0)
1f6860d9 1335 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
89573b37 1336 }
8eb9058d 1337
90e74a66 1338 SET_FOREACH(request_options, link->network->dhcp_request_options) {
5bc945be
SS
1339 uint32_t option = PTR_TO_UINT32(request_options);
1340
1341 r = sd_dhcp_client_set_request_option(link->dhcp_client, option);
1342 if (r == -EEXIST) {
1343 log_link_debug(link, "DHCP4 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option);
1344 continue;
1345 }
5bc945be
SS
1346 if (r < 0)
1347 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option);
1348 }
1349
90e74a66 1350 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_options) {
7354900d
DW
1351 r = sd_dhcp_client_add_option(link->dhcp_client, send_option);
1352 if (r == -EEXIST)
1353 continue;
1354 if (r < 0)
1355 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
1356 }
1357
90e74a66 1358 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_vendor_options) {
7354900d
DW
1359 r = sd_dhcp_client_add_vendor_option(link->dhcp_client, send_option);
1360 if (r == -EEXIST)
1361 continue;
cb29c156
SS
1362 if (r < 0)
1363 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
1364 }
1365
7192bb81
LP
1366 r = dhcp4_set_hostname(link);
1367 if (r < 0)
a8494759 1368 return r;
3c9b8860
TG
1369
1370 if (link->network->dhcp_vendor_class_identifier) {
1371 r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
1372 link->network->dhcp_vendor_class_identifier);
1373 if (r < 0)
1f6860d9 1374 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
3c9b8860
TG
1375 }
1376
7b8d23a9
SS
1377 if (link->network->dhcp_mudurl) {
1378 r = sd_dhcp_client_set_mud_url(link->dhcp_client,
1379 link->network->dhcp_mudurl);
1380 if (r < 0)
1381 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1382 }
1383
af1c0de0
SS
1384 if (link->network->dhcp_user_class) {
1385 r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class);
1386 if (r < 0)
1f6860d9 1387 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m");
af1c0de0
SS
1388 }
1389
9faed222
SS
1390 if (link->network->dhcp_client_port) {
1391 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
1392 if (r < 0)
1f6860d9 1393 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
9faed222
SS
1394 }
1395
715cedfb
SS
1396 if (link->network->dhcp_max_attempts > 0) {
1397 r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts);
1398 if (r < 0)
1399 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m");
1400 }
1401
afe42aef
SC
1402 if (link->network->ip_service_type > 0) {
1403 r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->ip_service_type);
1404 if (r < 0)
0da425df 1405 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IP service type: %m");
afe42aef 1406 }
0f3ff4ea 1407
d6463307
SS
1408 if (link->network->dhcp_fallback_lease_lifetime > 0) {
1409 r = sd_dhcp_client_set_fallback_lease_lifetime(link->dhcp_client, link->network->dhcp_fallback_lease_lifetime);
1410 if (r < 0)
1411 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1412 }
1413
0f3ff4ea
SS
1414 if (link->network->dhcp_send_decline) {
1415 r = configure_dhcpv4_duplicate_address_detection(link);
1416 if (r < 0)
1417 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to configure service type: %m");
1418 }
1419
1420 return dhcp4_set_client_identifier(link);
3c9b8860 1421}
ca5ad760
YW
1422
1423int config_parse_dhcp_max_attempts(
1424 const char *unit,
1425 const char *filename,
1426 unsigned line,
1427 const char *section,
1428 unsigned section_line,
1429 const char *lvalue,
1430 int ltype,
1431 const char *rvalue,
1432 void *data,
1433 void *userdata) {
1434
1435 Network *network = data;
1436 uint64_t a;
1437 int r;
1438
1439 assert(network);
1440 assert(lvalue);
1441 assert(rvalue);
1442
1443 if (isempty(rvalue)) {
1444 network->dhcp_max_attempts = 0;
1445 return 0;
1446 }
1447
1448 if (streq(rvalue, "infinity")) {
1449 network->dhcp_max_attempts = (uint64_t) -1;
1450 return 0;
1451 }
1452
1453 r = safe_atou64(rvalue, &a);
1454 if (r < 0) {
d96edb2c 1455 log_syntax(unit, LOG_WARNING, filename, line, r,
ca5ad760
YW
1456 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue);
1457 return 0;
1458 }
1459
1460 if (a == 0) {
d96edb2c 1461 log_syntax(unit, LOG_WARNING, filename, line, 0,
ca5ad760
YW
1462 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue);
1463 return 0;
1464 }
1465
1466 network->dhcp_max_attempts = a;
1467
1468 return 0;
1469}
1470
98ebef62 1471int config_parse_dhcp_acl_ip_address(
ca5ad760
YW
1472 const char *unit,
1473 const char *filename,
1474 unsigned line,
1475 const char *section,
1476 unsigned section_line,
1477 const char *lvalue,
1478 int ltype,
1479 const char *rvalue,
1480 void *data,
1481 void *userdata) {
1482
1483 Network *network = data;
98ebef62 1484 Set **acl;
ca5ad760
YW
1485 int r;
1486
1487 assert(filename);
1488 assert(lvalue);
1489 assert(rvalue);
1490 assert(data);
1491
98ebef62
SS
1492 acl = STR_IN_SET(lvalue, "DenyList", "BlackList") ? &network->dhcp_deny_listed_ip : &network->dhcp_allow_listed_ip;
1493
ca5ad760 1494 if (isempty(rvalue)) {
98ebef62 1495 *acl = set_free(*acl);
ca5ad760
YW
1496 return 0;
1497 }
1498
de7fef4b 1499 for (const char *p = rvalue;;) {
ca5ad760
YW
1500 _cleanup_free_ char *n = NULL;
1501 union in_addr_union ip;
1502
1503 r = extract_first_word(&p, &n, NULL, 0);
d96edb2c
YW
1504 if (r == -ENOMEM)
1505 return log_oom();
ca5ad760 1506 if (r < 0) {
d96edb2c 1507 log_syntax(unit, LOG_WARNING, filename, line, r,
98ebef62
SS
1508 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1509 lvalue, rvalue);
ca5ad760
YW
1510 return 0;
1511 }
1512 if (r == 0)
1513 return 0;
1514
1515 r = in_addr_from_string(AF_INET, n, &ip);
1516 if (r < 0) {
d96edb2c 1517 log_syntax(unit, LOG_WARNING, filename, line, r,
98ebef62 1518 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue, n);
ca5ad760
YW
1519 continue;
1520 }
1521
98ebef62 1522 r = set_ensure_put(acl, NULL, UINT32_TO_PTR(ip.in.s_addr));
ca5ad760 1523 if (r < 0)
d96edb2c 1524 log_syntax(unit, LOG_WARNING, filename, line, r,
98ebef62 1525 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue, n);
ca5ad760 1526 }
ca5ad760
YW
1527}
1528
a75b2117
SS
1529int config_parse_dhcp_ip_service_type(
1530 const char *unit,
1531 const char *filename,
1532 unsigned line,
1533 const char *section,
1534 unsigned section_line,
1535 const char *lvalue,
1536 int ltype,
1537 const char *rvalue,
1538 void *data,
1539 void *userdata) {
1540
1541 assert(filename);
1542 assert(lvalue);
1543 assert(rvalue);
1544
1545 if (streq(rvalue, "CS4"))
1546 *((int *)data) = IPTOS_CLASS_CS4;
1547 else if (streq(rvalue, "CS6"))
1548 *((int *)data) = IPTOS_CLASS_CS6;
1549 else
1550 log_syntax(unit, LOG_WARNING, filename, line, 0,
1551 "Failed to parse IPServiceType type '%s', ignoring.", rvalue);
1552
1553 return 0;
1554}
1555
7b8d23a9
SS
1556int config_parse_dhcp_mud_url(
1557 const char *unit,
1558 const char *filename,
1559 unsigned line,
1560 const char *section,
1561 unsigned section_line,
1562 const char *lvalue,
1563 int ltype,
1564 const char *rvalue,
1565 void *data,
1566 void *userdata) {
1567
1568 _cleanup_free_ char *unescaped = NULL;
1569 Network *network = data;
1570 int r;
1571
1572 assert(filename);
1573 assert(lvalue);
1574 assert(rvalue);
1575
1576 if (isempty(rvalue)) {
1577 network->dhcp_mudurl = mfree(network->dhcp_mudurl);
1578 return 0;
1579 }
1580
1581 r = cunescape(rvalue, 0, &unescaped);
1582 if (r < 0) {
d96edb2c 1583 log_syntax(unit, LOG_WARNING, filename, line, r,
7b8d23a9
SS
1584 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
1585 return 0;
1586 }
1587
1588 if (!http_url_is_valid(unescaped) || strlen(unescaped) > 255) {
d96edb2c 1589 log_syntax(unit, LOG_WARNING, filename, line, 0,
7b8d23a9
SS
1590 "Failed to parse MUD URL '%s', ignoring: %m", rvalue);
1591
1592 return 0;
1593 }
1594
1595 return free_and_strdup_warn(&network->dhcp_mudurl, unescaped);
1596}
1597
d6463307
SS
1598int config_parse_dhcp_fallback_lease_lifetime(const char *unit,
1599 const char *filename,
1600 unsigned line,
1601 const char *section,
1602 unsigned section_line,
1603 const char *lvalue,
1604 int ltype,
1605 const char *rvalue,
1606 void *data,
1607 void *userdata) {
1608 Network *network = userdata;
d2735796 1609 uint32_t k;
d6463307
SS
1610
1611 assert(filename);
1612 assert(section);
1613 assert(lvalue);
1614 assert(rvalue);
1615 assert(data);
1616
1617 if (isempty(rvalue)) {
1618 network->dhcp_fallback_lease_lifetime = 0;
1619 return 0;
1620 }
1621
1622 /* We accept only "forever" or "infinity". */
1623 if (STR_IN_SET(rvalue, "forever", "infinity"))
1624 k = CACHE_INFO_INFINITY_LIFE_TIME;
1625 else {
d96edb2c 1626 log_syntax(unit, LOG_WARNING, filename, line, 0,
d6463307
SS
1627 "Invalid LeaseLifetime= value, ignoring: %s", rvalue);
1628 return 0;
1629 }
1630
1631 network->dhcp_fallback_lease_lifetime = k;
1632
1633 return 0;
1634}
1635
ca5ad760
YW
1636static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = {
1637 [DHCP_CLIENT_ID_MAC] = "mac",
1638 [DHCP_CLIENT_ID_DUID] = "duid",
1639 [DHCP_CLIENT_ID_DUID_ONLY] = "duid-only",
1640};
1641
1642DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier);
1643DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier,
1644 "Failed to parse client identifier type");