]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-dhcp4.c
network: when Gateway=_dhcp, assume gateway family based on other settings
[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) {
ad208fac 41 k = route_remove(route, NULL, link, NULL);
6e537f62
YW
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;
6dd53981 315 route->gw_family = AF_INET;
ad098b14
SS
316 assert_se(sd_dhcp_route_get_gateway(static_routes[i], &route->gw.in) >= 0);
317 assert_se(sd_dhcp_route_get_destination(static_routes[i], &route->dst.in) >= 0);
318 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen) >= 0);
319 route->priority = link->network->dhcp_route_metric;
320 route->table = table;
321 route->mtu = link->network->dhcp_route_mtu;
322 route->scope = route_scope_from_address(route, &address);
323 if (IN_SET(route->scope, RT_SCOPE_LINK, RT_SCOPE_UNIVERSE))
324 route->prefsrc.in = address;
325
326 if (set_contains(link->dhcp_routes, route))
327 continue;
328
6e537f62 329 r = dhcp_route_configure(route, link);
ad098b14
SS
330 if (r < 0)
331 return log_link_error_errno(link, r, "Could not set route: %m");
332 }
5f04a209
SS
333 }
334
244490f5 335 if (link->network->dhcp_use_gateway) {
2200c3cf
YW
336 const struct in_addr *router;
337
244490f5
DS
338 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
339 if (IN_SET(r, 0, -ENODATA))
340 log_link_info(link, "DHCP: No gateway received from DHCP server.");
341 else if (r < 0)
2200c3cf 342 return log_link_error_errno(link, r, "DHCP error: could not get gateway: %m");
244490f5
DS
343 else if (in4_addr_is_null(&router[0]))
344 log_link_info(link, "DHCP: Received gateway is null.");
2200c3cf
YW
345 else if (classless_route)
346 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
347 * a Router option, the DHCP client MUST ignore the Router option. */
348 log_link_warning(link, "Classless static routes received from DHCP server: ignoring router option");
349 else {
244490f5
DS
350 _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL;
351
352 r = route_new(&route_gw);
353 if (r < 0)
6906794d 354 return log_link_error_errno(link, r, "Could not allocate route: %m");
244490f5
DS
355
356 /* The dhcp netmask may mask out the gateway. Add an explicit
357 * route for the gw host so that we can route no matter the
358 * netmask or existing kernel route tables. */
359 route_gw->family = AF_INET;
360 route_gw->dst.in = router[0];
361 route_gw->dst_prefixlen = 32;
362 route_gw->prefsrc.in = address;
363 route_gw->scope = RT_SCOPE_LINK;
364 route_gw->protocol = RTPROT_DHCP;
365 route_gw->priority = link->network->dhcp_route_metric;
366 route_gw->table = table;
367 route_gw->mtu = link->network->dhcp_route_mtu;
368
6e537f62 369 r = dhcp_route_configure(route_gw, link);
244490f5
DS
370 if (r < 0)
371 return log_link_error_errno(link, r, "Could not set host route: %m");
3c9b8860 372
244490f5
DS
373 r = route_new(&route);
374 if (r < 0)
375 return log_link_error_errno(link, r, "Could not allocate route: %m");
860e636c 376
244490f5 377 route->family = AF_INET;
6dd53981 378 route->gw_family = AF_INET;
244490f5
DS
379 route->gw.in = router[0];
380 route->prefsrc.in = address;
381 route->protocol = RTPROT_DHCP;
382 route->priority = link->network->dhcp_route_metric;
383 route->table = table;
384 route->mtu = link->network->dhcp_route_mtu;
3c9b8860 385
6e537f62 386 r = dhcp_route_configure(route, link);
244490f5
DS
387 if (r < 0)
388 return log_link_error_errno(link, r, "Could not set router: %m");
3c9b8860 389
2a54a044 390 HASHMAP_FOREACH(rt, link->network->routes_by_section) {
1a3a6309 391 if (!rt->gateway_from_dhcp_or_ra)
2200c3cf 392 continue;
1985c54f 393
2200c3cf
YW
394 if (rt->family != AF_INET)
395 continue;
1985c54f 396
6dd53981 397 rt->gw_family = AF_INET;
2200c3cf 398 rt->gw.in = router[0];
1985c54f 399
2200c3cf
YW
400 r = dhcp_route_configure(rt, link);
401 if (r < 0)
402 return log_link_error_errno(link, r, "Could not set gateway: %m");
403 }
244490f5 404 }
1985c54f
YW
405 }
406
854a1ccf 407 return link_set_dns_routes(link, &address);
3c9b8860
TG
408}
409
7fa472f9
YW
410static int dhcp_reset_mtu(Link *link) {
411 uint16_t mtu;
412 int r;
413
414 assert(link);
415
416 if (!link->network->dhcp_use_mtu)
417 return 0;
418
419 r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
46b875fb
YW
420 if (r == -ENODATA)
421 return 0;
7fa472f9 422 if (r < 0)
46b875fb 423 return log_link_error_errno(link, r, "DHCP error: failed to get MTU from lease: %m");
7fa472f9
YW
424
425 if (link->original_mtu == mtu)
426 return 0;
427
428 r = link_set_mtu(link, link->original_mtu);
46b875fb
YW
429 if (r < 0)
430 return log_link_error_errno(link, r, "DHCP error: could not reset MTU: %m");
3c9b8860 431
7fa472f9
YW
432 return 0;
433}
434
435static int dhcp_reset_hostname(Link *link) {
436 const char *hostname;
437 int r;
438
439 assert(link);
440
441 if (!link->network->dhcp_use_hostname)
442 return 0;
443
444 hostname = link->network->dhcp_hostname;
445 if (!hostname)
446 (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
447
448 if (!hostname)
449 return 0;
450
451 /* If a hostname was set due to the lease, then unset it now. */
452 r = manager_set_hostname(link->manager, NULL);
453 if (r < 0)
454 return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m");
455
456 return 0;
457}
458
6906794d
YW
459static int dhcp4_remove_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
460 int r;
461
462 assert(m);
463 assert(link);
464 assert(link->dhcp4_remove_messages > 0);
465
466 link->dhcp4_remove_messages--;
467
468 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
469 return 1;
470
471 r = sd_netlink_message_get_errno(m);
472 if (r < 0 && r != -ESRCH)
473 log_link_message_warning_errno(link, m, r, "Failed to remove DHCPv4 route, ignoring");
474
475 if (link->dhcp4_remove_messages == 0) {
476 r = dhcp4_update_address(link, false);
477 if (r < 0)
478 link_enter_failed(link);
479 }
480
481 return 1;
482}
483
484static int dhcp4_remove_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
485 int r;
486
487 assert(m);
488 assert(link);
489 assert(link->dhcp4_remove_messages > 0);
490
491 link->dhcp4_remove_messages--;
492
493 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
494 return 1;
495
496 r = sd_netlink_message_get_errno(m);
497 if (r < 0 && r != -EADDRNOTAVAIL)
498 log_link_message_warning_errno(link, m, r, "Failed to remove DHCPv4 address, ignoring");
499 else
500 (void) manager_rtnl_process_address(rtnl, m, link->manager);
501
502 if (link->dhcp4_remove_messages == 0) {
503 r = dhcp4_update_address(link, false);
504 if (r < 0)
505 link_enter_failed(link);
506 }
507
508 return 1;
509}
510
511static int dhcp4_remove_all(Link *link) {
6e537f62 512 Route *route;
6e537f62 513 int k, r = 0;
6906794d
YW
514
515 assert(link);
6906794d 516
90e74a66 517 SET_FOREACH(route, link->dhcp_routes) {
ad208fac 518 k = route_remove(route, NULL, link, dhcp4_remove_route_handler);
6e537f62
YW
519 if (k < 0)
520 r = k;
521 else
522 link->dhcp4_remove_messages++;
523 }
6906794d 524
6e537f62
YW
525 if (link->dhcp_address) {
526 k = address_remove(link->dhcp_address, link, dhcp4_remove_address_handler);
527 if (k < 0)
528 r = k;
529 else
530 link->dhcp4_remove_messages++;
531 }
6906794d 532
6e537f62 533 return r;
6906794d
YW
534}
535
7fa472f9 536static int dhcp_lease_lost(Link *link) {
2ac7eec3 537 int k, r = 0;
7fa472f9
YW
538
539 assert(link);
540 assert(link->dhcp_lease);
541
f8c2e4b9 542 log_link_info(link, "DHCP lease lost");
7fa472f9
YW
543
544 link->dhcp4_configured = false;
545
75be72d1 546 /* dhcp_lease_lost() may be called during renewing IP address. */
c45fdad6 547 k = dhcp4_release_old_lease(link);
6e537f62
YW
548 if (k < 0)
549 r = k;
75be72d1 550
6e537f62
YW
551 k = dhcp4_remove_all(link);
552 if (k < 0)
553 r = k;
6906794d 554
6e537f62
YW
555 k = dhcp_reset_mtu(link);
556 if (k < 0)
557 r = k;
6906794d 558
6e537f62
YW
559 k = dhcp_reset_hostname(link);
560 if (k < 0)
561 r = k;
7fa472f9 562
3c9b8860 563 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
6a3e5f6a 564 link_dirty(link);
3c9b8860 565
66f507e1
YW
566 if (link->dhcp_acd)
567 (void) sd_ipv4acd_stop(link->dhcp_acd);
568
6e537f62 569 return r;
3c9b8860
TG
570}
571
0f3ff4ea
SS
572static void dhcp_address_on_acd(sd_ipv4acd *acd, int event, void *userdata) {
573 _cleanup_free_ char *pretty = NULL;
574 union in_addr_union address = {};
575 Link *link;
576 int r;
577
578 assert(acd);
579 assert(userdata);
580
581 link = userdata;
582
583 switch (event) {
584 case SD_IPV4ACD_EVENT_STOP:
585 log_link_debug(link, "Stopping ACD client for DHCP4...");
586 return;
587
588 case SD_IPV4ACD_EVENT_BIND:
589 if (DEBUG_LOGGING) {
590 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address.in);
591 (void) in_addr_to_string(AF_INET, &address, &pretty);
592 log_link_debug(link, "Successfully claimed DHCP4 address %s", strna(pretty));
593 }
153cf041 594 link->dhcp4_address_bind = true;
c1d3fa29 595 dhcp4_check_ready(link);
0f3ff4ea
SS
596 break;
597
598 case SD_IPV4ACD_EVENT_CONFLICT:
599 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address.in);
600 (void) in_addr_to_string(AF_INET, &address, &pretty);
601 log_link_warning(link, "DAD conflict. Dropping DHCP4 address %s", strna(pretty));
602
f766d9af
YW
603 r = sd_dhcp_client_send_decline(link->dhcp_client);
604 if (r < 0)
605 log_link_warning_errno(link, r, "Failed to send DHCP DECLINE, ignoring: %m");
0f3ff4ea
SS
606
607 if (link->dhcp_lease) {
608 r = dhcp_lease_lost(link);
609 if (r < 0)
610 link_enter_failed(link);
611 }
612 break;
613
614 default:
615 assert_not_reached("Invalid IPv4ACD event.");
616 }
617
dce1cd41 618 (void) sd_ipv4acd_stop(acd);
0f3ff4ea
SS
619
620 return;
621}
622
10fa21c0 623static int dhcp4_configure_dad(Link *link) {
0f3ff4ea
SS
624 int r;
625
626 assert(link);
10fa21c0
YW
627 assert(link->manager);
628 assert(link->network);
0f3ff4ea 629
10fa21c0
YW
630 if (!link->network->dhcp_send_decline)
631 return 0;
0f3ff4ea 632
10fa21c0
YW
633 if (!link->dhcp_acd) {
634 r = sd_ipv4acd_new(&link->dhcp_acd);
635 if (r < 0)
636 return r;
0f3ff4ea 637
10fa21c0
YW
638 r = sd_ipv4acd_attach_event(link->dhcp_acd, link->manager->event, 0);
639 if (r < 0)
640 return r;
641 }
0f3ff4ea 642
10fa21c0 643 r = sd_ipv4acd_set_ifindex(link->dhcp_acd, link->ifindex);
0f3ff4ea
SS
644 if (r < 0)
645 return r;
646
10fa21c0 647 r = sd_ipv4acd_set_mac(link->dhcp_acd, &link->mac);
0f3ff4ea
SS
648 if (r < 0)
649 return r;
650
0f3ff4ea
SS
651 return 0;
652}
653
54312274
YW
654static int dhcp4_dad_update_mac(Link *link) {
655 bool running;
656 int r;
657
658 assert(link);
659
660 if (!link->dhcp_acd)
661 return 0;
662
663 running = sd_ipv4acd_is_running(link->dhcp_acd);
664
665 r = sd_ipv4acd_stop(link->dhcp_acd);
0f3ff4ea
SS
666 if (r < 0)
667 return r;
668
54312274 669 r = sd_ipv4acd_set_mac(link->dhcp_acd, &link->mac);
0f3ff4ea
SS
670 if (r < 0)
671 return r;
672
54312274
YW
673 if (running) {
674 r = sd_ipv4acd_start(link->dhcp_acd, true);
675 if (r < 0)
676 return r;
677 }
678
0f3ff4ea
SS
679 return 0;
680}
681
153cf041
YW
682static int dhcp4_start_acd(Link *link) {
683 union in_addr_union addr;
687b3bc6 684 struct in_addr old;
153cf041
YW
685 int r;
686
687 if (!link->network->dhcp_send_decline)
688 return 0;
689
690 if (!link->dhcp_lease)
691 return 0;
692
10fa21c0 693 (void) sd_ipv4acd_stop(link->dhcp_acd);
5acf54a0 694
153cf041
YW
695 link->dhcp4_address_bind = false;
696
697 r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr.in);
698 if (r < 0)
699 return r;
700
10fa21c0 701 r = sd_ipv4acd_get_address(link->dhcp_acd, &old);
687b3bc6
YW
702 if (r < 0)
703 return r;
704
10fa21c0 705 r = sd_ipv4acd_set_address(link->dhcp_acd, &addr.in);
153cf041
YW
706 if (r < 0)
707 return r;
708
10fa21c0 709 r = sd_ipv4acd_set_callback(link->dhcp_acd, dhcp_address_on_acd, link);
153cf041
YW
710 if (r < 0)
711 return r;
712
713 if (DEBUG_LOGGING) {
714 _cleanup_free_ char *pretty = NULL;
715
716 (void) in_addr_to_string(AF_INET, &addr, &pretty);
717 log_link_debug(link, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty));
718 }
719
10fa21c0 720 r = sd_ipv4acd_start(link->dhcp_acd, !in4_addr_equal(&addr.in, &old));
153cf041
YW
721 if (r < 0)
722 return r;
723
724 return 1;
725}
726
c45fdad6
YW
727static int dhcp4_address_ready_callback(Address *address) {
728 Link *link;
729 int r;
730
731 assert(address);
732
733 link = address->link;
734
735 /* Do not call this again. */
736 address->callback = NULL;
737
738 r = link_set_dhcp_routes(link);
739 if (r < 0)
740 return r;
741
742 /* Reconfigure static routes as kernel may remove some routes when lease expires. */
141318f7 743 r = link_set_routes(link);
c45fdad6
YW
744 if (r < 0)
745 return r;
746
747 r = dhcp4_start_acd(link);
748 if (r < 0)
69e3234d 749 return log_link_error_errno(link, r, "Failed to start IPv4ACD for DHCP4 address: %m");
c45fdad6
YW
750
751 dhcp4_check_ready(link);
752 return 0;
753}
754
302a796f 755static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3c9b8860
TG
756 int r;
757
758 assert(link);
759
3ab7ed3f
YW
760 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
761 return 1;
762
1c4baffc 763 r = sd_netlink_message_get_errno(m);
3c9b8860 764 if (r < 0 && r != -EEXIST) {
5ecb131d 765 log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 address");
3c9b8860 766 link_enter_failed(link);
3ab7ed3f 767 return 1;
4ff296b0
YW
768 } else if (r >= 0)
769 (void) manager_rtnl_process_address(rtnl, m, link->manager);
3c9b8860 770
c45fdad6
YW
771 if (address_is_ready(link->dhcp_address)) {
772 r = dhcp4_address_ready_callback(link->dhcp_address);
773 if (r < 0) {
774 link_enter_failed(link);
775 return 1;
776 }
777 } else
778 link->dhcp_address->callback = dhcp4_address_ready_callback;
0f3ff4ea 779
3c9b8860
TG
780 return 1;
781}
782
6906794d 783static int dhcp4_update_address(Link *link, bool announce) {
8e766630 784 _cleanup_(address_freep) Address *addr = NULL;
6906794d
YW
785 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
786 struct in_addr address, netmask;
3c9b8860 787 unsigned prefixlen;
6e537f62 788 Address *ret;
3c9b8860
TG
789 int r;
790
6906794d
YW
791 assert(link);
792 assert(link->network);
793
794 if (!link->dhcp_lease)
795 return 0;
796
797 link_set_state(link, LINK_STATE_CONFIGURING);
798 link->dhcp4_configured = false;
3c9b8860 799
141318f7
YW
800 /* address_handler calls link_set_routes() and link_set_nexthop(). Before they are called, the
801 * related flags must be cleared. Otherwise, the link becomes configured state before routes
802 * are configured. */
0c816fcc
YW
803 link->static_routes_configured = false;
804 link->static_nexthops_configured = false;
805
6906794d
YW
806 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
807 if (r < 0)
808 return log_link_warning_errno(link, r, "DHCP error: no address: %m");
809
810 r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
811 if (r < 0)
812 return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
813
814 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
815 r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
816 if (r < 0)
817 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
818 }
819
820 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
821
822 if (announce) {
823 const struct in_addr *router;
824
825 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
826 if (r < 0 && r != -ENODATA)
827 return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
828
829 if (r > 0 && !in4_addr_is_null(&router[0]))
830 log_struct(LOG_INFO,
831 LOG_LINK_INTERFACE(link),
ceea6c1a
YW
832 LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u via "IPV4_ADDRESS_FMT_STR,
833 IPV4_ADDRESS_FMT_VAL(address),
6906794d 834 prefixlen,
ceea6c1a
YW
835 IPV4_ADDRESS_FMT_VAL(router[0])),
836 "ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address),
6906794d 837 "PREFIXLEN=%u", prefixlen,
ceea6c1a 838 "GATEWAY="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(router[0]));
6906794d
YW
839 else
840 log_struct(LOG_INFO,
841 LOG_LINK_INTERFACE(link),
ceea6c1a
YW
842 LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u",
843 IPV4_ADDRESS_FMT_VAL(address),
6906794d 844 prefixlen),
ceea6c1a 845 "ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address),
6906794d
YW
846 "PREFIXLEN=%u", prefixlen);
847 }
3c9b8860 848
f0213e37 849 r = address_new(&addr);
3c9b8860 850 if (r < 0)
6906794d 851 return log_oom();
3c9b8860
TG
852
853 addr->family = AF_INET;
6906794d 854 addr->in_addr.in.s_addr = address.s_addr;
3c9b8860
TG
855 addr->cinfo.ifa_prefered = lifetime;
856 addr->cinfo.ifa_valid = lifetime;
857 addr->prefixlen = prefixlen;
6906794d 858 addr->broadcast.s_addr = address.s_addr | ~netmask.s_addr;
eaff204f 859 SET_FLAG(addr->flags, IFA_F_NOPREFIXROUTE, !link_prefixroute(link));
3c9b8860 860
66669078
TG
861 /* allow reusing an existing address and simply update its lifetime
862 * in case it already exists */
6e537f62 863 r = address_configure(addr, link, dhcp4_address_handler, true, &ret);
3c9b8860 864 if (r < 0)
6e537f62
YW
865 return log_link_error_errno(link, r, "Failed to set DHCPv4 address: %m");
866
867 if (!address_equal(link->dhcp_address, ret))
868 link->dhcp_address_old = link->dhcp_address;
869 link->dhcp_address = ret;
3c9b8860
TG
870
871 return 0;
872}
873
874static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
875 sd_dhcp_lease *lease;
3c9b8860
TG
876 int r;
877
878 assert(link);
879 assert(client);
3c9b8860
TG
880
881 r = sd_dhcp_client_get_lease(client, &lease);
f6b8196f
LP
882 if (r < 0)
883 return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
3c9b8860
TG
884
885 sd_dhcp_lease_unref(link->dhcp_lease);
e6b18ffa 886 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 887 link_dirty(link);
3c9b8860 888
6906794d 889 return dhcp4_update_address(link, false);
3c9b8860
TG
890}
891
892static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
893 sd_dhcp_lease *lease;
3c9b8860
TG
894 int r;
895
896 assert(client);
897 assert(link);
898
899 r = sd_dhcp_client_get_lease(client, &lease);
f2341e0a 900 if (r < 0)
f6b8196f 901 return log_link_error_errno(link, r, "DHCP error: No lease: %m");
3c9b8860 902
6906794d 903 sd_dhcp_lease_unref(link->dhcp_lease);
e6b18ffa 904 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 905 link_dirty(link);
3c9b8860 906
27cb34f5 907 if (link->network->dhcp_use_mtu) {
3c9b8860
TG
908 uint16_t mtu;
909
910 r = sd_dhcp_lease_get_mtu(lease, &mtu);
911 if (r >= 0) {
933c70a0 912 r = link_set_mtu(link, mtu);
3c9b8860 913 if (r < 0)
f2341e0a 914 log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu);
3c9b8860
TG
915 }
916 }
917
27cb34f5 918 if (link->network->dhcp_use_hostname) {
2de2abad
LB
919 const char *dhcpname = NULL;
920 _cleanup_free_ char *hostname = NULL;
3c9b8860 921
27cb34f5 922 if (link->network->dhcp_hostname)
2de2abad 923 dhcpname = link->network->dhcp_hostname;
dce391e7 924 else
2de2abad
LB
925 (void) sd_dhcp_lease_get_hostname(lease, &dhcpname);
926
927 if (dhcpname) {
928 r = shorten_overlong(dhcpname, &hostname);
929 if (r < 0)
930 log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
931 if (r == 1)
5238e957 932 log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
2de2abad 933 }
a7d0ef44 934
dce391e7 935 if (hostname) {
59eb33e0 936 r = manager_set_hostname(link->manager, hostname);
3c9b8860 937 if (r < 0)
f2341e0a 938 log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
3c9b8860
TG
939 }
940 }
941
27cb34f5 942 if (link->network->dhcp_use_timezone) {
21b80ad1
LP
943 const char *tz = NULL;
944
945 (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
946
947 if (tz) {
59eb33e0 948 r = manager_set_timezone(link->manager, tz);
21b80ad1
LP
949 if (r < 0)
950 log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz);
951 }
952 }
953
6906794d
YW
954 if (link->dhcp4_remove_messages == 0) {
955 r = dhcp4_update_address(link, true);
956 if (r < 0)
957 return r;
958 } else
959 log_link_debug(link,
960 "The link has previously assigned DHCPv4 address or routes. "
961 "The newly assigned address and routes will set up after old ones are removed.");
3c9b8860
TG
962
963 return 0;
964}
8bc17bb3 965
d03073dd
YW
966static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) {
967 int r;
968
d03073dd 969 r = dhcp_lease_acquired(client, link);
6e537f62 970 if (r < 0)
d03073dd 971 (void) dhcp_lease_lost(link);
d03073dd 972
6e537f62 973 return r;
d03073dd
YW
974}
975
6b000af4 976static int dhcp_server_is_deny_listed(Link *link, sd_dhcp_client *client) {
727b5734
SS
977 sd_dhcp_lease *lease;
978 struct in_addr addr;
979 int r;
980
981 assert(link);
982 assert(link->network);
983 assert(client);
984
985 r = sd_dhcp_client_get_lease(client, &lease);
986 if (r < 0)
987 return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
988
989 r = sd_dhcp_lease_get_server_identifier(lease, &addr);
990 if (r < 0)
0da425df 991 return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
727b5734 992
6b000af4 993 if (set_contains(link->network->dhcp_deny_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
727b5734
SS
994 log_struct(LOG_DEBUG,
995 LOG_LINK_INTERFACE(link),
ceea6c1a
YW
996 LOG_LINK_MESSAGE(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" found in deny-list, ignoring offer",
997 IPV4_ADDRESS_FMT_VAL(addr)));
727b5734
SS
998 return true;
999 }
1000
1001 return false;
1002}
1003
98ebef62
SS
1004static int dhcp_server_is_allow_listed(Link *link, sd_dhcp_client *client) {
1005 sd_dhcp_lease *lease;
1006 struct in_addr addr;
1007 int r;
1008
1009 assert(link);
1010 assert(link->network);
1011 assert(client);
1012
1013 r = sd_dhcp_client_get_lease(client, &lease);
1014 if (r < 0)
1015 return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
1016
1017 r = sd_dhcp_lease_get_server_identifier(lease, &addr);
1018 if (r < 0)
0da425df 1019 return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
98ebef62
SS
1020
1021 if (set_contains(link->network->dhcp_allow_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
1022 log_struct(LOG_DEBUG,
1023 LOG_LINK_INTERFACE(link),
ceea6c1a
YW
1024 LOG_LINK_MESSAGE(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" found in allow-list, accepting offer",
1025 IPV4_ADDRESS_FMT_VAL(addr)));
98ebef62
SS
1026 return true;
1027 }
1028
1029 return false;
1030}
1031
727b5734 1032static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
3c9b8860 1033 Link *link = userdata;
86e2be7b 1034 int r;
3c9b8860
TG
1035
1036 assert(link);
1037 assert(link->network);
1038 assert(link->manager);
1039
1040 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
727b5734 1041 return 0;
3c9b8860
TG
1042
1043 switch (event) {
03748142 1044 case SD_DHCP_CLIENT_EVENT_STOP:
8bc17bb3 1045
910feb78 1046 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4)) {
8bc17bb3
SS
1047 assert(link->ipv4ll);
1048
1049 log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address");
1050
1051 r = sd_ipv4ll_start(link->ipv4ll);
727b5734
SS
1052 if (r < 0)
1053 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
8bc17bb3
SS
1054 }
1055
7da377ef 1056 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
efdb62df
YW
1057 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1058 return 0;
1059 }
1060
efdb62df 1061 if (link->dhcp_lease) {
f766d9af
YW
1062 if (link->network->dhcp_send_release) {
1063 r = sd_dhcp_client_send_release(client);
1064 if (r < 0)
1065 log_link_warning_errno(link, r, "Failed to send DHCP RELEASE, ignoring: %m");
1066 }
8bea7e70 1067
efdb62df
YW
1068 r = dhcp_lease_lost(link);
1069 if (r < 0) {
1070 link_enter_failed(link);
1071 return r;
1072 }
1073 }
1074
1075 break;
8bc17bb3 1076 case SD_DHCP_CLIENT_EVENT_EXPIRED:
7da377ef 1077 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
efdb62df 1078 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
727b5734 1079 return 0;
3c9b8860
TG
1080 }
1081
1082 if (link->dhcp_lease) {
1083 r = dhcp_lease_lost(link);
1084 if (r < 0) {
1085 link_enter_failed(link);
727b5734 1086 return r;
3c9b8860
TG
1087 }
1088 }
1089
d03073dd
YW
1090 break;
1091 case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
1092 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
1093 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1094 return 0;
1095 }
1096
1097 r = dhcp_lease_ip_change(client, link);
1098 if (r < 0) {
1099 link_enter_failed(link);
1100 return r;
3c9b8860
TG
1101 }
1102
1103 break;
03748142 1104 case SD_DHCP_CLIENT_EVENT_RENEW:
3c9b8860
TG
1105 r = dhcp_lease_renew(client, link);
1106 if (r < 0) {
1107 link_enter_failed(link);
727b5734 1108 return r;
3c9b8860
TG
1109 }
1110 break;
03748142 1111 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
3c9b8860
TG
1112 r = dhcp_lease_acquired(client, link);
1113 if (r < 0) {
1114 link_enter_failed(link);
727b5734 1115 return r;
3c9b8860
TG
1116 }
1117 break;
727b5734 1118 case SD_DHCP_CLIENT_EVENT_SELECTING:
98ebef62
SS
1119 if (!set_isempty(link->network->dhcp_allow_listed_ip)) {
1120 r = dhcp_server_is_allow_listed(link, client);
1121 if (r < 0)
1122 return r;
1123 if (r == 0)
1124 return -ENOMSG;
1125 } else {
1126 r = dhcp_server_is_deny_listed(link, client);
1127 if (r < 0)
1128 return r;
1129 if (r != 0)
1130 return -ENOMSG;
1131 }
727b5734 1132 break;
3c9b8860
TG
1133 default:
1134 if (event < 0)
f6b8196f 1135 log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
3c9b8860 1136 else
f6b8196f 1137 log_link_warning(link, "DHCP unknown event: %i", event);
3c9b8860
TG
1138 break;
1139 }
1140
727b5734 1141 return 0;
3c9b8860
TG
1142}
1143
7192bb81
LP
1144static int dhcp4_set_hostname(Link *link) {
1145 _cleanup_free_ char *hostname = NULL;
1146 const char *hn;
1147 int r;
1148
1149 assert(link);
1150
1151 if (!link->network->dhcp_send_hostname)
1152 hn = NULL;
1153 else if (link->network->dhcp_hostname)
1154 hn = link->network->dhcp_hostname;
1155 else {
1156 r = gethostname_strict(&hostname);
1157 if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
1158 return r;
1159
1160 hn = hostname;
1161 }
1162
a8494759
YW
1163 r = sd_dhcp_client_set_hostname(link->dhcp_client, hn);
1164 if (r == -EINVAL && hostname)
1165 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1166 log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1167 else if (r < 0)
1168 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m");
1169
1170 return 0;
7192bb81
LP
1171}
1172
64b21ece 1173static bool promote_secondaries_enabled(const char *ifname) {
3f550c31
HV
1174 _cleanup_free_ char *promote_secondaries_sysctl = NULL;
1175 char *promote_secondaries_path;
64b21ece
MV
1176 int r;
1177
1178 promote_secondaries_path = strjoina("net/ipv4/conf/", ifname, "/promote_secondaries");
1179 r = sysctl_read(promote_secondaries_path, &promote_secondaries_sysctl);
1180 if (r < 0) {
1181 log_debug_errno(r, "Cannot read sysctl %s", promote_secondaries_path);
1182 return false;
1183 }
1184
1185 truncate_nl(promote_secondaries_sysctl);
1186 r = parse_boolean(promote_secondaries_sysctl);
1187 if (r < 0)
1188 log_warning_errno(r, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path, promote_secondaries_sysctl);
1189 return r > 0;
1190}
1191
1192/* dhcp4_set_promote_secondaries will ensure this interface has
1193 * the "promote_secondaries" option in the kernel set. If this sysctl
1194 * is not set DHCP will work only as long as the IP address does not
1195 * changes between leases. The kernel will remove all secondary IP
1196 * addresses of an interface otherwise. The way systemd-network works
1197 * is that the new IP of a lease is added as a secondary IP and when
1198 * the primary one expires it relies on the kernel to promote the
1199 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1200 */
2ffd6d73 1201static int dhcp4_set_promote_secondaries(Link *link) {
64b21ece
MV
1202 int r;
1203
1204 assert(link);
64b21ece
MV
1205
1206 /* check if the kernel has promote_secondaries enabled for our
1207 * interface. If it is not globally enabled or enabled for the
1208 * specific interface we must either enable it.
1209 */
8e1a7253 1210 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link->ifname))) {
64b21ece
MV
1211 char *promote_secondaries_path = NULL;
1212
1213 log_link_debug(link, "promote_secondaries is unset, setting it");
1214 promote_secondaries_path = strjoina("net/ipv4/conf/", link->ifname, "/promote_secondaries");
1215 r = sysctl_write(promote_secondaries_path, "1");
1216 if (r < 0)
1217 log_link_warning_errno(link, r, "cannot set sysctl %s to 1", promote_secondaries_path);
1218 return r > 0;
1219 }
1220
1221 return 0;
1222}
1223
d947f7f9 1224static int dhcp4_set_client_identifier(Link *link) {
fff1f40c
YW
1225 int r;
1226
1227 assert(link);
1228 assert(link->network);
1229 assert(link->dhcp_client);
1230
1231 switch (link->network->dhcp_client_identifier) {
1232 case DHCP_CLIENT_ID_DUID: {
0cf7c3fd 1233 /* If configured, apply user specified DUID and IAID */
fff1f40c
YW
1234 const DUID *duid = link_get_duid(link);
1235
0cf7c3fd
YW
1236 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1237 r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
8217ed5e 1238 link->network->iaid_set,
0cf7c3fd
YW
1239 link->network->iaid,
1240 duid->llt_time);
1241 else
1242 r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
8217ed5e 1243 link->network->iaid_set,
0cf7c3fd
YW
1244 link->network->iaid,
1245 duid->type,
1246 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1247 duid->raw_data_len);
fff1f40c 1248 if (r < 0)
0cf7c3fd 1249 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
fff1f40c
YW
1250 break;
1251 }
1252 case DHCP_CLIENT_ID_DUID_ONLY: {
1253 /* If configured, apply user specified DUID */
1254 const DUID *duid = link_get_duid(link);
1255
0cf7c3fd
YW
1256 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1257 r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
89b3fa66 1258 duid->llt_time);
0cf7c3fd
YW
1259 else
1260 r = sd_dhcp_client_set_duid(link->dhcp_client,
1261 duid->type,
1262 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1263 duid->raw_data_len);
fff1f40c
YW
1264 if (r < 0)
1265 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
1266 break;
1267 }
1268 case DHCP_CLIENT_ID_MAC:
1269 r = sd_dhcp_client_set_client_id(link->dhcp_client,
1270 ARPHRD_ETHER,
1271 (const uint8_t *) &link->mac,
1272 sizeof(link->mac));
1273 if (r < 0)
1274 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m");
1275 break;
1276 default:
1277 assert_not_reached("Unknown client identifier type.");
1278 }
1279
1280 return 0;
1281}
1282
571eeba9
YW
1283static int dhcp4_init(Link *link) {
1284 int r;
1285
1286 assert(link);
1287
1288 if (link->dhcp_client)
1289 return 0;
1290
1291 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
1292 if (r < 0)
1293 return r;
1294
1295 r = sd_dhcp_client_attach_event(link->dhcp_client, link->manager->event, 0);
1296 if (r < 0)
1297 return r;
1298
1299 return 0;
1300}
1301
3c9b8860 1302int dhcp4_configure(Link *link) {
cb29c156 1303 sd_dhcp_option *send_option;
5bc945be 1304 void *request_options;
3c9b8860
TG
1305 int r;
1306
1307 assert(link);
1308 assert(link->network);
2ffd6d73
YW
1309
1310 if (!link_dhcp4_enabled(link))
1311 return 0;
1312
1313 r = dhcp4_set_promote_secondaries(link);
1314 if (r < 0)
1315 return r;
3c9b8860 1316
571eeba9
YW
1317 r = dhcp4_init(link);
1318 if (r < 0)
1319 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to initialize DHCP4 client: %m");
3c9b8860 1320
76253e73
DW
1321 r = sd_dhcp_client_set_mac(link->dhcp_client,
1322 (const uint8_t *) &link->mac,
1323 sizeof (link->mac), ARPHRD_ETHER);
3c9b8860 1324 if (r < 0)
1f6860d9 1325 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m");
3c9b8860 1326
2f8e7633 1327 r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
3c9b8860 1328 if (r < 0)
1f6860d9 1329 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m");
3c9b8860
TG
1330
1331 r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
1332 if (r < 0)
1f6860d9 1333 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m");
3c9b8860
TG
1334
1335 r = sd_dhcp_client_set_request_broadcast(link->dhcp_client,
1336 link->network->dhcp_broadcast);
1337 if (r < 0)
1f6860d9 1338 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
3c9b8860
TG
1339
1340 if (link->mtu) {
1341 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
1342 if (r < 0)
1f6860d9 1343 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m");
3c9b8860
TG
1344 }
1345
27cb34f5 1346 if (link->network->dhcp_use_mtu) {
39745a5a 1347 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1348 SD_DHCP_OPTION_INTERFACE_MTU);
39745a5a 1349 if (r < 0)
1f6860d9 1350 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
3c9b8860
TG
1351 }
1352
5e77a146 1353 /* NOTE: even if this variable is called "use", it also "sends" PRL
1354 * options, maybe there should be a different configuration variable
1355 * to send or not route options?. */
28522b0d 1356 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1357 * by default, so they don't need to be added here. */
5e77a146 1358 if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) {
3c9b8860 1359 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1360 SD_DHCP_OPTION_STATIC_ROUTE);
3c9b8860 1361 if (r < 0)
1f6860d9
YW
1362 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1363
3c9b8860 1364 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1365 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
7d6884b6 1366 if (r < 0)
1f6860d9 1367 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
3c9b8860
TG
1368 }
1369
150d3b8e
YW
1370 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO && !link->network->dhcp_anonymize) {
1371 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST);
1372 if (r < 0)
1373 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1374 }
1375
ead36ce6 1376 if (link->network->dhcp_use_ntp) {
1377 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
1378 if (r < 0)
1f6860d9 1379 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
ead36ce6 1380 }
4b7b5abb 1381
299d578f
SS
1382 if (link->network->dhcp_use_sip) {
1383 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_SIP_SERVER);
1384 if (r < 0)
1385 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1386 }
1387
89573b37 1388 if (link->network->dhcp_use_timezone) {
1389 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
1390 if (r < 0)
1f6860d9 1391 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
89573b37 1392 }
8eb9058d 1393
90e74a66 1394 SET_FOREACH(request_options, link->network->dhcp_request_options) {
5bc945be
SS
1395 uint32_t option = PTR_TO_UINT32(request_options);
1396
1397 r = sd_dhcp_client_set_request_option(link->dhcp_client, option);
5bc945be
SS
1398 if (r < 0)
1399 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option);
1400 }
1401
90e74a66 1402 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_options) {
7354900d
DW
1403 r = sd_dhcp_client_add_option(link->dhcp_client, send_option);
1404 if (r == -EEXIST)
1405 continue;
1406 if (r < 0)
1407 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
1408 }
1409
90e74a66 1410 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_vendor_options) {
7354900d
DW
1411 r = sd_dhcp_client_add_vendor_option(link->dhcp_client, send_option);
1412 if (r == -EEXIST)
1413 continue;
cb29c156
SS
1414 if (r < 0)
1415 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
1416 }
1417
7192bb81
LP
1418 r = dhcp4_set_hostname(link);
1419 if (r < 0)
a8494759 1420 return r;
3c9b8860
TG
1421
1422 if (link->network->dhcp_vendor_class_identifier) {
1423 r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
1424 link->network->dhcp_vendor_class_identifier);
1425 if (r < 0)
1f6860d9 1426 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
3c9b8860
TG
1427 }
1428
7b8d23a9
SS
1429 if (link->network->dhcp_mudurl) {
1430 r = sd_dhcp_client_set_mud_url(link->dhcp_client,
1431 link->network->dhcp_mudurl);
1432 if (r < 0)
1433 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1434 }
1435
af1c0de0
SS
1436 if (link->network->dhcp_user_class) {
1437 r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class);
1438 if (r < 0)
1f6860d9 1439 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m");
af1c0de0
SS
1440 }
1441
9faed222
SS
1442 if (link->network->dhcp_client_port) {
1443 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
1444 if (r < 0)
1f6860d9 1445 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
9faed222
SS
1446 }
1447
715cedfb
SS
1448 if (link->network->dhcp_max_attempts > 0) {
1449 r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts);
1450 if (r < 0)
1451 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m");
1452 }
1453
afe42aef
SC
1454 if (link->network->ip_service_type > 0) {
1455 r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->ip_service_type);
1456 if (r < 0)
0da425df 1457 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IP service type: %m");
afe42aef 1458 }
0f3ff4ea 1459
d6463307
SS
1460 if (link->network->dhcp_fallback_lease_lifetime > 0) {
1461 r = sd_dhcp_client_set_fallback_lease_lifetime(link->dhcp_client, link->network->dhcp_fallback_lease_lifetime);
1462 if (r < 0)
1463 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1464 }
1465
10fa21c0
YW
1466 r = dhcp4_configure_dad(link);
1467 if (r < 0)
1468 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to configure service type: %m");
0f3ff4ea
SS
1469
1470 return dhcp4_set_client_identifier(link);
3c9b8860 1471}
ca5ad760 1472
d947f7f9
YW
1473int dhcp4_update_mac(Link *link) {
1474 int r;
1475
1476 assert(link);
1477
1478 if (!link->dhcp_client)
1479 return 0;
1480
1481 r = sd_dhcp_client_set_mac(link->dhcp_client, (const uint8_t *) &link->mac, sizeof (link->mac), ARPHRD_ETHER);
1482 if (r < 0)
1483 return r;
1484
1485 r = dhcp4_set_client_identifier(link);
1486 if (r < 0)
1487 return r;
1488
54312274
YW
1489 r = dhcp4_dad_update_mac(link);
1490 if (r < 0)
1491 return r;
1492
d947f7f9
YW
1493 return 0;
1494}
1495
571eeba9
YW
1496int link_deserialize_dhcp4(Link *link, const char *dhcp4_address) {
1497 union in_addr_union address;
1498 int r;
1499
1500 assert(link);
1501
1502 if (isempty(dhcp4_address))
1503 return 0;
1504
1505 r = in_addr_from_string(AF_INET, dhcp4_address, &address);
1506 if (r < 0)
1507 return log_link_debug_errno(link, r, "Failed to parse DHCPv4 address: %s", dhcp4_address);
1508
1509 r = dhcp4_init(link);
1510 if (r < 0)
1511 return log_link_debug_errno(link, r, "Failed to initialize DHCPv4 client: %m");
1512
1513 r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in);
1514 if (r < 0)
1515 return log_link_debug_errno(link, r, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address);
1516
1517 return 0;
1518}
1519
ca5ad760
YW
1520int config_parse_dhcp_max_attempts(
1521 const char *unit,
1522 const char *filename,
1523 unsigned line,
1524 const char *section,
1525 unsigned section_line,
1526 const char *lvalue,
1527 int ltype,
1528 const char *rvalue,
1529 void *data,
1530 void *userdata) {
1531
1532 Network *network = data;
1533 uint64_t a;
1534 int r;
1535
1536 assert(network);
1537 assert(lvalue);
1538 assert(rvalue);
1539
1540 if (isempty(rvalue)) {
1541 network->dhcp_max_attempts = 0;
1542 return 0;
1543 }
1544
1545 if (streq(rvalue, "infinity")) {
1546 network->dhcp_max_attempts = (uint64_t) -1;
1547 return 0;
1548 }
1549
1550 r = safe_atou64(rvalue, &a);
1551 if (r < 0) {
d96edb2c 1552 log_syntax(unit, LOG_WARNING, filename, line, r,
ca5ad760
YW
1553 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue);
1554 return 0;
1555 }
1556
1557 if (a == 0) {
d96edb2c 1558 log_syntax(unit, LOG_WARNING, filename, line, 0,
ca5ad760
YW
1559 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue);
1560 return 0;
1561 }
1562
1563 network->dhcp_max_attempts = a;
1564
1565 return 0;
1566}
1567
98ebef62 1568int config_parse_dhcp_acl_ip_address(
ca5ad760
YW
1569 const char *unit,
1570 const char *filename,
1571 unsigned line,
1572 const char *section,
1573 unsigned section_line,
1574 const char *lvalue,
1575 int ltype,
1576 const char *rvalue,
1577 void *data,
1578 void *userdata) {
1579
1580 Network *network = data;
98ebef62 1581 Set **acl;
ca5ad760
YW
1582 int r;
1583
1584 assert(filename);
1585 assert(lvalue);
1586 assert(rvalue);
1587 assert(data);
1588
98ebef62
SS
1589 acl = STR_IN_SET(lvalue, "DenyList", "BlackList") ? &network->dhcp_deny_listed_ip : &network->dhcp_allow_listed_ip;
1590
ca5ad760 1591 if (isempty(rvalue)) {
98ebef62 1592 *acl = set_free(*acl);
ca5ad760
YW
1593 return 0;
1594 }
1595
de7fef4b 1596 for (const char *p = rvalue;;) {
ca5ad760
YW
1597 _cleanup_free_ char *n = NULL;
1598 union in_addr_union ip;
1599
1600 r = extract_first_word(&p, &n, NULL, 0);
d96edb2c
YW
1601 if (r == -ENOMEM)
1602 return log_oom();
ca5ad760 1603 if (r < 0) {
d96edb2c 1604 log_syntax(unit, LOG_WARNING, filename, line, r,
98ebef62
SS
1605 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1606 lvalue, rvalue);
ca5ad760
YW
1607 return 0;
1608 }
1609 if (r == 0)
1610 return 0;
1611
1612 r = in_addr_from_string(AF_INET, n, &ip);
1613 if (r < 0) {
d96edb2c 1614 log_syntax(unit, LOG_WARNING, filename, line, r,
98ebef62 1615 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue, n);
ca5ad760
YW
1616 continue;
1617 }
1618
98ebef62 1619 r = set_ensure_put(acl, NULL, UINT32_TO_PTR(ip.in.s_addr));
ca5ad760 1620 if (r < 0)
d96edb2c 1621 log_syntax(unit, LOG_WARNING, filename, line, r,
98ebef62 1622 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue, n);
ca5ad760 1623 }
ca5ad760
YW
1624}
1625
a75b2117
SS
1626int config_parse_dhcp_ip_service_type(
1627 const char *unit,
1628 const char *filename,
1629 unsigned line,
1630 const char *section,
1631 unsigned section_line,
1632 const char *lvalue,
1633 int ltype,
1634 const char *rvalue,
1635 void *data,
1636 void *userdata) {
1637
1638 assert(filename);
1639 assert(lvalue);
1640 assert(rvalue);
1641
1642 if (streq(rvalue, "CS4"))
1643 *((int *)data) = IPTOS_CLASS_CS4;
1644 else if (streq(rvalue, "CS6"))
1645 *((int *)data) = IPTOS_CLASS_CS6;
1646 else
1647 log_syntax(unit, LOG_WARNING, filename, line, 0,
1648 "Failed to parse IPServiceType type '%s', ignoring.", rvalue);
1649
1650 return 0;
1651}
1652
7b8d23a9
SS
1653int config_parse_dhcp_mud_url(
1654 const char *unit,
1655 const char *filename,
1656 unsigned line,
1657 const char *section,
1658 unsigned section_line,
1659 const char *lvalue,
1660 int ltype,
1661 const char *rvalue,
1662 void *data,
1663 void *userdata) {
1664
1665 _cleanup_free_ char *unescaped = NULL;
1666 Network *network = data;
1667 int r;
1668
1669 assert(filename);
1670 assert(lvalue);
1671 assert(rvalue);
1672
1673 if (isempty(rvalue)) {
1674 network->dhcp_mudurl = mfree(network->dhcp_mudurl);
1675 return 0;
1676 }
1677
1678 r = cunescape(rvalue, 0, &unescaped);
1679 if (r < 0) {
d96edb2c 1680 log_syntax(unit, LOG_WARNING, filename, line, r,
7b8d23a9
SS
1681 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
1682 return 0;
1683 }
1684
1685 if (!http_url_is_valid(unescaped) || strlen(unescaped) > 255) {
d96edb2c 1686 log_syntax(unit, LOG_WARNING, filename, line, 0,
7b8d23a9
SS
1687 "Failed to parse MUD URL '%s', ignoring: %m", rvalue);
1688
1689 return 0;
1690 }
1691
1692 return free_and_strdup_warn(&network->dhcp_mudurl, unescaped);
1693}
1694
d6463307
SS
1695int config_parse_dhcp_fallback_lease_lifetime(const char *unit,
1696 const char *filename,
1697 unsigned line,
1698 const char *section,
1699 unsigned section_line,
1700 const char *lvalue,
1701 int ltype,
1702 const char *rvalue,
1703 void *data,
1704 void *userdata) {
1705 Network *network = userdata;
d2735796 1706 uint32_t k;
d6463307
SS
1707
1708 assert(filename);
1709 assert(section);
1710 assert(lvalue);
1711 assert(rvalue);
1712 assert(data);
1713
1714 if (isempty(rvalue)) {
1715 network->dhcp_fallback_lease_lifetime = 0;
1716 return 0;
1717 }
1718
1719 /* We accept only "forever" or "infinity". */
1720 if (STR_IN_SET(rvalue, "forever", "infinity"))
1721 k = CACHE_INFO_INFINITY_LIFE_TIME;
1722 else {
d96edb2c 1723 log_syntax(unit, LOG_WARNING, filename, line, 0,
d6463307
SS
1724 "Invalid LeaseLifetime= value, ignoring: %s", rvalue);
1725 return 0;
1726 }
1727
1728 network->dhcp_fallback_lease_lifetime = k;
1729
1730 return 0;
1731}
1732
ca5ad760
YW
1733static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = {
1734 [DHCP_CLIENT_ID_MAC] = "mac",
1735 [DHCP_CLIENT_ID_DUID] = "duid",
1736 [DHCP_CLIENT_ID_DUID_ONLY] = "duid-only",
1737};
1738
1739DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier);
1740DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier,
1741 "Failed to parse client identifier type");