]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-dhcp4.c
network: drop unnecessary link_enter_failed() calls
[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>
3c9b8860
TG
4#include <linux/if.h>
5
b5efdb8a 6#include "alloc-util.h"
958b66ea 7#include "hostname-util.h"
64b21ece 8#include "parse-util.h"
3c9b8860 9#include "network-internal.h"
23f53b99
TG
10#include "networkd-link.h"
11#include "networkd-manager.h"
12#include "networkd-network.h"
64b21ece
MV
13#include "string-util.h"
14#include "sysctl-util.h"
3c9b8860 15
302a796f 16static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3c9b8860
TG
17 int r;
18
19 assert(link);
6cf4a01c 20 assert(link->dhcp4_messages > 0);
3c9b8860 21
313cefa1 22 link->dhcp4_messages--;
3c9b8860 23
1c4baffc 24 r = sd_netlink_message_get_errno(m);
3c9b8860 25 if (r < 0 && r != -EEXIST) {
f6b8196f 26 log_link_error_errno(link, r, "Could not set DHCPv4 route: %m");
3c9b8860
TG
27 link_enter_failed(link);
28 }
29
6cf4a01c 30 if (link->dhcp4_messages == 0) {
3c9b8860 31 link->dhcp4_configured = true;
8012cd39 32 link_check_ready(link);
3c9b8860
TG
33 }
34
35 return 1;
36}
37
d6eac9bd
DW
38static int route_scope_from_address(const Route *route, const struct in_addr *self_addr) {
39 assert(route);
40 assert(self_addr);
41
42 if (in_addr_is_localhost(AF_INET, &route->dst) ||
43 (self_addr->s_addr && route->dst.in.s_addr == self_addr->s_addr))
44 return RT_SCOPE_HOST;
45 else if (in4_addr_is_null(&route->gw.in))
46 return RT_SCOPE_LINK;
47 else
48 return RT_SCOPE_UNIVERSE;
49}
50
3c9b8860 51static int link_set_dhcp_routes(Link *link) {
f8693fc7 52 _cleanup_free_ sd_dhcp_route **static_routes = NULL;
8cdc46e7 53 bool classless_route = false, static_route = false;
f8862395
TH
54 const struct in_addr *router;
55 struct in_addr address;
3c9b8860 56 int r, n, i;
fc1ba79d 57 uint32_t table;
3c9b8860
TG
58
59 assert(link);
0c9b15a3
AJ
60
61 if (!link->dhcp_lease) /* link went down while we configured the IP addresses? */
62 return 0;
63
64 if (!link->network) /* link went down while we configured the IP addresses? */
65 return 0;
964b26fe
SS
66
67 if (!link->network->dhcp_use_routes)
68 return 0;
3c9b8860 69
bdb9f580 70 table = link_get_dhcp_route_table(link);
fc1ba79d 71
b23aec0d
DW
72 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
73 if (r < 0)
74 return log_link_warning_errno(link, r, "DHCP error: could not get address: %m");
75
5f04a209 76 n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes);
599c44e6
YW
77 if (n == -ENODATA)
78 log_link_debug_errno(link, n, "DHCP: No routes received from DHCP server: %m");
79 else if (n < 0)
8dc787d1 80 log_link_debug_errno(link, n, "DHCP error: could not get routes: %m");
5f04a209 81
8cdc46e7 82 for (i = 0; i < n; i++) {
3476951c
TH
83 switch (sd_dhcp_route_get_option(static_routes[i])) {
84 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE:
8cdc46e7 85 classless_route = true;
3476951c
TH
86 break;
87 case SD_DHCP_OPTION_STATIC_ROUTE:
8cdc46e7 88 static_route = true;
3476951c
TH
89 break;
90 }
8cdc46e7
SS
91 }
92
5f04a209 93 for (i = 0; i < n; i++) {
8e766630 94 _cleanup_(route_freep) Route *route = NULL;
5f04a209 95
8cdc46e7
SS
96 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
97 the DHCP client MUST ignore the Static Routes option. */
3476951c
TH
98 if (classless_route &&
99 sd_dhcp_route_get_option(static_routes[i]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE)
8cdc46e7
SS
100 continue;
101
5f04a209
SS
102 r = route_new(&route);
103 if (r < 0)
104 return log_link_error_errno(link, r, "Could not allocate route: %m");
105
106 route->family = AF_INET;
107 route->protocol = RTPROT_DHCP;
108 assert_se(sd_dhcp_route_get_gateway(static_routes[i], &route->gw.in) >= 0);
109 assert_se(sd_dhcp_route_get_destination(static_routes[i], &route->dst.in) >= 0);
110 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen) >= 0);
111 route->priority = link->network->dhcp_route_metric;
112 route->table = table;
113 route->scope = route_scope_from_address(route, &address);
114
115 r = route_configure(route, link, dhcp4_route_handler);
116 if (r < 0)
1590dfa4 117 return log_link_error_errno(link, r, "Could not set host route: %m");
5f04a209
SS
118
119 link->dhcp4_messages++;
120 }
121
f8862395 122 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
825ace96
YW
123 if (IN_SET(r, 0, -ENODATA))
124 log_link_info(link, "DHCP: No gateway received from DHCP server.");
125 else if (r < 0)
444b0170 126 log_link_warning_errno(link, r, "DHCP error: could not get gateway: %m");
825ace96
YW
127 else if (in4_addr_is_null(&router[0]))
128 log_link_info(link, "DHCP: Received gateway is null.");
f6b8196f 129
5f04a209
SS
130 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
131 a Router option, the DHCP client MUST ignore the Router option. */
8cdc46e7
SS
132 if (classless_route && static_route)
133 log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option and router option");
134
f8862395 135 if (r > 0 && !classless_route && !in4_addr_is_null(&router[0])) {
860e636c 136 _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL;
ed9e361a
TG
137
138 r = route_new(&route_gw);
f6b8196f
LP
139 if (r < 0)
140 return log_link_error_errno(link, r, "Could not allocate route: %m");
3c9b8860
TG
141
142 /* The dhcp netmask may mask out the gateway. Add an explicit
143 * route for the gw host so that we can route no matter the
144 * netmask or existing kernel route tables. */
145 route_gw->family = AF_INET;
f8862395 146 route_gw->dst.in = router[0];
3c9b8860 147 route_gw->dst_prefixlen = 32;
2ce40956 148 route_gw->prefsrc.in = address;
3c9b8860 149 route_gw->scope = RT_SCOPE_LINK;
ed9e361a 150 route_gw->protocol = RTPROT_DHCP;
86655331 151 route_gw->priority = link->network->dhcp_route_metric;
fc1ba79d 152 route_gw->table = table;
3c9b8860 153
483d099e 154 r = route_configure(route_gw, link, dhcp4_route_handler);
f6b8196f 155 if (r < 0)
1590dfa4 156 return log_link_error_errno(link, r, "Could not set host route: %m");
3c9b8860 157
313cefa1 158 link->dhcp4_messages++;
3c9b8860 159
860e636c
YW
160 r = route_new(&route);
161 if (r < 0)
162 return log_link_error_errno(link, r, "Could not allocate route: %m");
163
3c9b8860 164 route->family = AF_INET;
f8862395 165 route->gw.in = router[0];
2ce40956 166 route->prefsrc.in = address;
860e636c 167 route->protocol = RTPROT_DHCP;
86655331 168 route->priority = link->network->dhcp_route_metric;
fc1ba79d 169 route->table = table;
3c9b8860 170
483d099e 171 r = route_configure(route, link, dhcp4_route_handler);
1590dfa4
YW
172 if (r < 0)
173 return log_link_error_errno(link, r, "Could not set routes: %m");
3c9b8860 174
313cefa1 175 link->dhcp4_messages++;
3c9b8860
TG
176 }
177
3c9b8860
TG
178 return 0;
179}
180
7fa472f9
YW
181static int dhcp_remove_routes(Link *link, struct in_addr *address) {
182 _cleanup_free_ sd_dhcp_route **routes = NULL;
183 uint32_t table;
184 int n, i, r;
3c9b8860
TG
185
186 assert(link);
7fa472f9 187 assert(address);
3c9b8860 188
7fa472f9
YW
189 if (!link->network->dhcp_use_routes)
190 return 0;
3c9b8860 191
7fa472f9
YW
192 n = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes);
193 if (IN_SET(n, 0, -ENODATA)) {
194 log_link_debug(link, "DHCP: No routes received from DHCP server: %m");
195 return 0;
196 } else if (n < 0)
197 return log_link_error_errno(link, n, "DHCP error: could not get routes: %m");
4c9c8272 198
7fa472f9 199 table = link_get_dhcp_route_table(link);
8df8ce78 200
7fa472f9
YW
201 for (i = 0; i < n; i++) {
202 _cleanup_(route_freep) Route *route = NULL;
3c9b8860 203
7fa472f9
YW
204 r = route_new(&route);
205 if (r < 0)
206 return log_oom();
207
208 route->family = AF_INET;
209 assert_se(sd_dhcp_route_get_gateway(routes[i], &route->gw.in) >= 0);
210 assert_se(sd_dhcp_route_get_destination(routes[i], &route->dst.in) >= 0);
211 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &route->dst_prefixlen) >= 0);
212 route->priority = link->network->dhcp_route_metric;
213 route->table = table;
214 route->scope = route_scope_from_address(route, address);
215
216 (void) route_remove(route, link, NULL);
b5799eeb 217 }
3c9b8860 218
7fa472f9
YW
219 return n;
220}
8df8ce78 221
7fa472f9
YW
222static int dhcp_remove_router(Link *link, struct in_addr *address) {
223 _cleanup_(route_freep) Route *route_gw = NULL, *route = NULL;
224 const struct in_addr *router;
225 uint32_t table;
226 int r;
8df8ce78 227
7fa472f9
YW
228 assert(link);
229 assert(address);
3c9b8860 230
7fa472f9
YW
231 if (!link->network->dhcp_use_routes)
232 return 0;
3c9b8860 233
7fa472f9
YW
234 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
235 if (IN_SET(r, 0, -ENODATA)) {
236 log_link_debug(link, "DHCP: No gateway received from DHCP server.");
237 return 0;
238 } else if (r < 0)
239 return log_link_error_errno(link, r, "DHCP error: could not get gateway: %m");
240 else if (in4_addr_is_null(&router[0])) {
241 log_link_info(link, "DHCP: Received gateway is null, ignoring.");
242 return 0;
3c9b8860
TG
243 }
244
7fa472f9 245 table = link_get_dhcp_route_table(link);
3c9b8860 246
7fa472f9
YW
247 r = route_new(&route_gw);
248 if (r < 0)
249 return log_oom();
3c9b8860 250
7fa472f9
YW
251 route_gw->family = AF_INET;
252 route_gw->dst.in = router[0];
253 route_gw->dst_prefixlen = 32;
254 route_gw->prefsrc.in = *address;
255 route_gw->scope = RT_SCOPE_LINK;
256 route_gw->protocol = RTPROT_DHCP;
257 route_gw->priority = link->network->dhcp_route_metric;
258 route_gw->table = table;
a7d0ef44 259
7fa472f9
YW
260 (void) route_remove(route_gw, link, NULL);
261
262 r = route_new(&route);
263 if (r < 0)
264 return log_oom();
265
266 route->family = AF_INET;
267 route->gw.in = router[0];
268 route->prefsrc.in = *address;
269 route->protocol = RTPROT_DHCP;
270 route->priority = link->network->dhcp_route_metric;
271 route->table = table;
272
273 (void) route_remove(route, link, NULL);
274
275 return 0;
276}
277
278static int dhcp_remove_address(Link *link, struct in_addr *address) {
279 _cleanup_(address_freep) Address *a = NULL;
280 struct in_addr netmask;
281 int r;
282
283 assert(link);
284 assert(address);
285
286 if (in4_addr_is_null(address))
287 return 0;
288
289 r = address_new(&a);
290 if (r < 0)
291 return log_oom();
292
293 a->family = AF_INET;
294 a->in_addr.in = *address;
295
296 if (sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask) >= 0)
297 a->prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
298
299 (void) address_remove(a, link, NULL);
300
301 return 0;
302}
303
304static int dhcp_reset_mtu(Link *link) {
305 uint16_t mtu;
306 int r;
307
308 assert(link);
309
310 if (!link->network->dhcp_use_mtu)
311 return 0;
312
313 r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
314 if (r < 0)
315 return r;
316
317 if (link->original_mtu == mtu)
318 return 0;
319
320 r = link_set_mtu(link, link->original_mtu);
321 if (r < 0) {
322 log_link_error_errno(link, r, "DHCP error: could not reset MTU: %m");
323 link_enter_failed(link);
324 return r;
3c9b8860
TG
325 }
326
7fa472f9
YW
327 return 0;
328}
329
330static int dhcp_reset_hostname(Link *link) {
331 const char *hostname;
332 int r;
333
334 assert(link);
335
336 if (!link->network->dhcp_use_hostname)
337 return 0;
338
339 hostname = link->network->dhcp_hostname;
340 if (!hostname)
341 (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
342
343 if (!hostname)
344 return 0;
345
346 /* If a hostname was set due to the lease, then unset it now. */
347 r = manager_set_hostname(link->manager, NULL);
348 if (r < 0)
349 return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m");
350
351 return 0;
352}
353
354static int dhcp_lease_lost(Link *link) {
355 struct in_addr address = {};
356
357 assert(link);
358 assert(link->dhcp_lease);
359
360 log_link_warning(link, "DHCP lease lost");
361
362 link->dhcp4_configured = false;
363
364 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address);
365 (void) dhcp_remove_routes(link, &address);
366 (void) dhcp_remove_router(link, &address);
367 (void) dhcp_remove_address(link, &address);
368 (void) dhcp_reset_mtu(link);
369 (void) dhcp_reset_hostname(link);
370
3c9b8860 371 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
6a3e5f6a 372 link_dirty(link);
3c9b8860
TG
373
374 return 0;
375}
376
302a796f 377static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3c9b8860
TG
378 int r;
379
380 assert(link);
381
1c4baffc 382 r = sd_netlink_message_get_errno(m);
3c9b8860 383 if (r < 0 && r != -EEXIST) {
f6b8196f 384 log_link_error_errno(link, r, "Could not set DHCPv4 address: %m");
3c9b8860 385 link_enter_failed(link);
45af44d4 386 } else if (r >= 0)
200a0868 387 manager_rtnl_process_address(rtnl, m, link->manager);
3c9b8860 388
1590dfa4
YW
389 r = link_set_dhcp_routes(link);
390 if (r < 0) {
391 link_enter_failed(link);
392 return 1;
393 }
3c9b8860 394
b5799eeb
SS
395 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
396 link_request_set_routes(link);
397
223932c7
AH
398 if (link->dhcp4_messages == 0) {
399 link->dhcp4_configured = true;
400 link_check_ready(link);
401 }
402
3c9b8860
TG
403 return 1;
404}
405
406static int dhcp4_update_address(Link *link,
407 struct in_addr *address,
408 struct in_addr *netmask,
409 uint32_t lifetime) {
8e766630 410 _cleanup_(address_freep) Address *addr = NULL;
3c9b8860
TG
411 unsigned prefixlen;
412 int r;
413
414 assert(address);
415 assert(netmask);
416 assert(lifetime);
417
5a941f5f 418 prefixlen = in4_addr_netmask_to_prefixlen(netmask);
3c9b8860 419
f0213e37 420 r = address_new(&addr);
3c9b8860
TG
421 if (r < 0)
422 return r;
423
424 addr->family = AF_INET;
425 addr->in_addr.in.s_addr = address->s_addr;
426 addr->cinfo.ifa_prefered = lifetime;
427 addr->cinfo.ifa_valid = lifetime;
428 addr->prefixlen = prefixlen;
429 addr->broadcast.s_addr = address->s_addr | ~netmask->s_addr;
430
66669078
TG
431 /* allow reusing an existing address and simply update its lifetime
432 * in case it already exists */
483d099e 433 r = address_configure(addr, link, dhcp4_address_handler, true);
3c9b8860
TG
434 if (r < 0)
435 return r;
436
437 return 0;
438}
439
440static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
441 sd_dhcp_lease *lease;
442 struct in_addr address;
443 struct in_addr netmask;
444 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
445 int r;
446
447 assert(link);
448 assert(client);
449 assert(link->network);
450
451 r = sd_dhcp_client_get_lease(client, &lease);
f6b8196f
LP
452 if (r < 0)
453 return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
3c9b8860
TG
454
455 sd_dhcp_lease_unref(link->dhcp_lease);
456 link->dhcp4_configured = false;
e6b18ffa 457 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 458 link_dirty(link);
3c9b8860
TG
459
460 r = sd_dhcp_lease_get_address(lease, &address);
f6b8196f
LP
461 if (r < 0)
462 return log_link_warning_errno(link, r, "DHCP error: no address: %m");
3c9b8860
TG
463
464 r = sd_dhcp_lease_get_netmask(lease, &netmask);
f6b8196f
LP
465 if (r < 0)
466 return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
3c9b8860 467
7da377ef 468 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
f6b8196f
LP
469 r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
470 if (r < 0)
471 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
3c9b8860
TG
472 }
473
474 r = dhcp4_update_address(link, &address, &netmask, lifetime);
a2f68490
YW
475 if (r < 0)
476 return log_link_warning_errno(link, r, "Could not update IP address: %m");
3c9b8860
TG
477
478 return 0;
479}
480
481static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
f8862395 482 const struct in_addr *router;
3c9b8860
TG
483 sd_dhcp_lease *lease;
484 struct in_addr address;
485 struct in_addr netmask;
3c9b8860
TG
486 unsigned prefixlen;
487 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
488 int r;
489
490 assert(client);
491 assert(link);
492
a20c909c
YW
493 link->dhcp4_configured = false;
494
3c9b8860 495 r = sd_dhcp_client_get_lease(client, &lease);
f2341e0a 496 if (r < 0)
f6b8196f 497 return log_link_error_errno(link, r, "DHCP error: No lease: %m");
3c9b8860
TG
498
499 r = sd_dhcp_lease_get_address(lease, &address);
f2341e0a 500 if (r < 0)
f6b8196f 501 return log_link_error_errno(link, r, "DHCP error: No address: %m");
3c9b8860
TG
502
503 r = sd_dhcp_lease_get_netmask(lease, &netmask);
f2341e0a 504 if (r < 0)
f6b8196f 505 return log_link_error_errno(link, r, "DHCP error: No netmask: %m");
3c9b8860 506
5a941f5f 507 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
3c9b8860 508
448aaf9f
YW
509 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
510 r = sd_dhcp_lease_get_lifetime(lease, &lifetime);
511 if (r < 0)
512 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
513 }
514
f8862395 515 r = sd_dhcp_lease_get_router(lease, &router);
397d15fd 516 if (r < 0 && r != -ENODATA)
f6b8196f 517 return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
3c9b8860 518
f8862395 519 if (r > 0 && !in4_addr_is_null(&router[0]))
f2341e0a
LP
520 log_struct(LOG_INFO,
521 LOG_LINK_INTERFACE(link),
522 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
523 ADDRESS_FMT_VAL(address),
524 prefixlen,
f8862395 525 ADDRESS_FMT_VAL(router[0])),
f2341e0a
LP
526 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
527 "PREFIXLEN=%u", prefixlen,
f8862395 528 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router[0]));
3c9b8860 529 else
f2341e0a
LP
530 log_struct(LOG_INFO,
531 LOG_LINK_INTERFACE(link),
532 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u",
533 ADDRESS_FMT_VAL(address),
534 prefixlen),
535 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
a1230ff9 536 "PREFIXLEN=%u", prefixlen);
3c9b8860 537
e6b18ffa 538 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 539 link_dirty(link);
3c9b8860 540
27cb34f5 541 if (link->network->dhcp_use_mtu) {
3c9b8860
TG
542 uint16_t mtu;
543
544 r = sd_dhcp_lease_get_mtu(lease, &mtu);
545 if (r >= 0) {
933c70a0 546 r = link_set_mtu(link, mtu);
3c9b8860 547 if (r < 0)
f2341e0a 548 log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu);
3c9b8860
TG
549 }
550 }
551
27cb34f5 552 if (link->network->dhcp_use_hostname) {
2de2abad
LB
553 const char *dhcpname = NULL;
554 _cleanup_free_ char *hostname = NULL;
3c9b8860 555
27cb34f5 556 if (link->network->dhcp_hostname)
2de2abad 557 dhcpname = link->network->dhcp_hostname;
dce391e7 558 else
2de2abad
LB
559 (void) sd_dhcp_lease_get_hostname(lease, &dhcpname);
560
561 if (dhcpname) {
562 r = shorten_overlong(dhcpname, &hostname);
563 if (r < 0)
564 log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
565 if (r == 1)
5238e957 566 log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
2de2abad 567 }
a7d0ef44 568
dce391e7 569 if (hostname) {
59eb33e0 570 r = manager_set_hostname(link->manager, hostname);
3c9b8860 571 if (r < 0)
f2341e0a 572 log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
3c9b8860
TG
573 }
574 }
575
27cb34f5 576 if (link->network->dhcp_use_timezone) {
21b80ad1
LP
577 const char *tz = NULL;
578
579 (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
580
581 if (tz) {
59eb33e0 582 r = manager_set_timezone(link->manager, tz);
21b80ad1
LP
583 if (r < 0)
584 log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz);
585 }
586 }
587
3c9b8860 588 r = dhcp4_update_address(link, &address, &netmask, lifetime);
a2f68490
YW
589 if (r < 0)
590 return log_link_warning_errno(link, r, "Could not update IP address: %m");
3c9b8860
TG
591
592 return 0;
593}
8bc17bb3 594
727b5734
SS
595static int dhcp_server_is_black_listed(Link *link, sd_dhcp_client *client) {
596 sd_dhcp_lease *lease;
597 struct in_addr addr;
598 int r;
599
600 assert(link);
601 assert(link->network);
602 assert(client);
603
604 r = sd_dhcp_client_get_lease(client, &lease);
605 if (r < 0)
606 return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
607
608 r = sd_dhcp_lease_get_server_identifier(lease, &addr);
609 if (r < 0)
610 return log_link_debug_errno(link, r, "Failed to get DHCP server ip address: %m");
611
612 if (set_contains(link->network->dhcp_black_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
613 log_struct(LOG_DEBUG,
614 LOG_LINK_INTERFACE(link),
615 LOG_LINK_MESSAGE(link, "DHCPv4 ip '%u.%u.%u.%u' found in black listed ip addresses, ignoring offer",
616 ADDRESS_FMT_VAL(addr)));
617 return true;
618 }
619
620 return false;
621}
622
623static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
3c9b8860 624 Link *link = userdata;
86e2be7b 625 int r;
3c9b8860
TG
626
627 assert(link);
628 assert(link->network);
629 assert(link->manager);
630
631 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
727b5734 632 return 0;
3c9b8860
TG
633
634 switch (event) {
03748142 635 case SD_DHCP_CLIENT_EVENT_STOP:
8bc17bb3 636
910feb78 637 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4)) {
8bc17bb3
SS
638 assert(link->ipv4ll);
639
640 log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address");
641
642 r = sd_ipv4ll_start(link->ipv4ll);
727b5734
SS
643 if (r < 0)
644 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
8bc17bb3
SS
645 }
646
7da377ef 647 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
efdb62df
YW
648 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
649 return 0;
650 }
651
1501b429
SS
652 if (link->network->dhcp_send_release)
653 (void) sd_dhcp_client_send_release(client);
654
efdb62df
YW
655 if (link->dhcp_lease) {
656 r = dhcp_lease_lost(link);
657 if (r < 0) {
658 link_enter_failed(link);
659 return r;
660 }
661 }
662
663 break;
8bc17bb3 664 case SD_DHCP_CLIENT_EVENT_EXPIRED:
03748142 665 case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
8bc17bb3 666
7da377ef 667 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
efdb62df 668 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
727b5734 669 return 0;
3c9b8860
TG
670 }
671
672 if (link->dhcp_lease) {
673 r = dhcp_lease_lost(link);
674 if (r < 0) {
675 link_enter_failed(link);
727b5734 676 return r;
3c9b8860
TG
677 }
678 }
679
03748142 680 if (event == SD_DHCP_CLIENT_EVENT_IP_CHANGE) {
3c9b8860
TG
681 r = dhcp_lease_acquired(client, link);
682 if (r < 0) {
683 link_enter_failed(link);
727b5734 684 return r;
3c9b8860
TG
685 }
686 }
687
688 break;
03748142 689 case SD_DHCP_CLIENT_EVENT_RENEW:
3c9b8860
TG
690 r = dhcp_lease_renew(client, link);
691 if (r < 0) {
692 link_enter_failed(link);
727b5734 693 return r;
3c9b8860
TG
694 }
695 break;
03748142 696 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
3c9b8860
TG
697 r = dhcp_lease_acquired(client, link);
698 if (r < 0) {
699 link_enter_failed(link);
727b5734 700 return r;
3c9b8860
TG
701 }
702 break;
727b5734
SS
703 case SD_DHCP_CLIENT_EVENT_SELECTING:
704 r = dhcp_server_is_black_listed(link, client);
705 if (r < 0)
706 return r;
707 if (r != 0)
708 return -ENOMSG;
709 break;
3c9b8860
TG
710 default:
711 if (event < 0)
f6b8196f 712 log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
3c9b8860 713 else
f6b8196f 714 log_link_warning(link, "DHCP unknown event: %i", event);
3c9b8860
TG
715 break;
716 }
717
727b5734 718 return 0;
3c9b8860
TG
719}
720
7192bb81
LP
721static int dhcp4_set_hostname(Link *link) {
722 _cleanup_free_ char *hostname = NULL;
723 const char *hn;
724 int r;
725
726 assert(link);
727
728 if (!link->network->dhcp_send_hostname)
729 hn = NULL;
730 else if (link->network->dhcp_hostname)
731 hn = link->network->dhcp_hostname;
732 else {
733 r = gethostname_strict(&hostname);
734 if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
735 return r;
736
737 hn = hostname;
738 }
739
a8494759
YW
740 r = sd_dhcp_client_set_hostname(link->dhcp_client, hn);
741 if (r == -EINVAL && hostname)
742 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
743 log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
744 else if (r < 0)
745 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m");
746
747 return 0;
7192bb81
LP
748}
749
64b21ece 750static bool promote_secondaries_enabled(const char *ifname) {
3f550c31
HV
751 _cleanup_free_ char *promote_secondaries_sysctl = NULL;
752 char *promote_secondaries_path;
64b21ece
MV
753 int r;
754
755 promote_secondaries_path = strjoina("net/ipv4/conf/", ifname, "/promote_secondaries");
756 r = sysctl_read(promote_secondaries_path, &promote_secondaries_sysctl);
757 if (r < 0) {
758 log_debug_errno(r, "Cannot read sysctl %s", promote_secondaries_path);
759 return false;
760 }
761
762 truncate_nl(promote_secondaries_sysctl);
763 r = parse_boolean(promote_secondaries_sysctl);
764 if (r < 0)
765 log_warning_errno(r, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path, promote_secondaries_sysctl);
766 return r > 0;
767}
768
769/* dhcp4_set_promote_secondaries will ensure this interface has
770 * the "promote_secondaries" option in the kernel set. If this sysctl
771 * is not set DHCP will work only as long as the IP address does not
772 * changes between leases. The kernel will remove all secondary IP
773 * addresses of an interface otherwise. The way systemd-network works
774 * is that the new IP of a lease is added as a secondary IP and when
775 * the primary one expires it relies on the kernel to promote the
776 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
777 */
778int dhcp4_set_promote_secondaries(Link *link) {
779 int r;
780
781 assert(link);
782 assert(link->network);
783 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
784
785 /* check if the kernel has promote_secondaries enabled for our
786 * interface. If it is not globally enabled or enabled for the
787 * specific interface we must either enable it.
788 */
8e1a7253 789 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link->ifname))) {
64b21ece
MV
790 char *promote_secondaries_path = NULL;
791
792 log_link_debug(link, "promote_secondaries is unset, setting it");
793 promote_secondaries_path = strjoina("net/ipv4/conf/", link->ifname, "/promote_secondaries");
794 r = sysctl_write(promote_secondaries_path, "1");
795 if (r < 0)
796 log_link_warning_errno(link, r, "cannot set sysctl %s to 1", promote_secondaries_path);
797 return r > 0;
798 }
799
800 return 0;
801}
802
fff1f40c
YW
803int dhcp4_set_client_identifier(Link *link) {
804 int r;
805
806 assert(link);
807 assert(link->network);
808 assert(link->dhcp_client);
809
810 switch (link->network->dhcp_client_identifier) {
811 case DHCP_CLIENT_ID_DUID: {
0cf7c3fd 812 /* If configured, apply user specified DUID and IAID */
fff1f40c
YW
813 const DUID *duid = link_get_duid(link);
814
0cf7c3fd
YW
815 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
816 r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
8217ed5e 817 link->network->iaid_set,
0cf7c3fd
YW
818 link->network->iaid,
819 duid->llt_time);
820 else
821 r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
8217ed5e 822 link->network->iaid_set,
0cf7c3fd
YW
823 link->network->iaid,
824 duid->type,
825 duid->raw_data_len > 0 ? duid->raw_data : NULL,
826 duid->raw_data_len);
fff1f40c 827 if (r < 0)
0cf7c3fd 828 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
fff1f40c
YW
829 break;
830 }
831 case DHCP_CLIENT_ID_DUID_ONLY: {
832 /* If configured, apply user specified DUID */
833 const DUID *duid = link_get_duid(link);
834
0cf7c3fd
YW
835 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
836 r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
89b3fa66 837 duid->llt_time);
0cf7c3fd
YW
838 else
839 r = sd_dhcp_client_set_duid(link->dhcp_client,
840 duid->type,
841 duid->raw_data_len > 0 ? duid->raw_data : NULL,
842 duid->raw_data_len);
fff1f40c
YW
843 if (r < 0)
844 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
845 break;
846 }
847 case DHCP_CLIENT_ID_MAC:
848 r = sd_dhcp_client_set_client_id(link->dhcp_client,
849 ARPHRD_ETHER,
850 (const uint8_t *) &link->mac,
851 sizeof(link->mac));
852 if (r < 0)
853 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m");
854 break;
855 default:
856 assert_not_reached("Unknown client identifier type.");
857 }
858
859 return 0;
860}
861
3c9b8860
TG
862int dhcp4_configure(Link *link) {
863 int r;
864
865 assert(link);
866 assert(link->network);
e0ee46f2 867 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
3c9b8860 868
0bc70f1d 869 if (!link->dhcp_client) {
db3d2358 870 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
1f6860d9
YW
871 if (r == -ENOMEM)
872 return log_oom();
0bc70f1d 873 if (r < 0)
1f6860d9 874 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
0bc70f1d 875 }
3c9b8860
TG
876
877 r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
878 if (r < 0)
1f6860d9 879 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to attach event: %m");
3c9b8860 880
76253e73
DW
881 r = sd_dhcp_client_set_mac(link->dhcp_client,
882 (const uint8_t *) &link->mac,
883 sizeof (link->mac), ARPHRD_ETHER);
3c9b8860 884 if (r < 0)
1f6860d9 885 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m");
3c9b8860 886
2f8e7633 887 r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
3c9b8860 888 if (r < 0)
1f6860d9 889 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m");
3c9b8860
TG
890
891 r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
892 if (r < 0)
1f6860d9 893 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m");
3c9b8860
TG
894
895 r = sd_dhcp_client_set_request_broadcast(link->dhcp_client,
896 link->network->dhcp_broadcast);
897 if (r < 0)
1f6860d9 898 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
3c9b8860
TG
899
900 if (link->mtu) {
901 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
902 if (r < 0)
1f6860d9 903 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m");
3c9b8860
TG
904 }
905
27cb34f5 906 if (link->network->dhcp_use_mtu) {
39745a5a 907 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 908 SD_DHCP_OPTION_INTERFACE_MTU);
39745a5a 909 if (r < 0)
1f6860d9 910 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
3c9b8860
TG
911 }
912
5e77a146 913 /* NOTE: even if this variable is called "use", it also "sends" PRL
914 * options, maybe there should be a different configuration variable
915 * to send or not route options?. */
28522b0d 916 /* NOTE: when using Anonymize=yes, routes PRL options are sent
917 * by default, so they don't need to be added here. */
5e77a146 918 if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) {
3c9b8860 919 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 920 SD_DHCP_OPTION_STATIC_ROUTE);
3c9b8860 921 if (r < 0)
1f6860d9
YW
922 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
923
3c9b8860 924 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 925 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
7d6884b6 926 if (r < 0)
1f6860d9 927 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
3c9b8860
TG
928 }
929
ead36ce6 930 if (link->network->dhcp_use_ntp) {
931 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
932 if (r < 0)
1f6860d9 933 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
ead36ce6 934 }
4b7b5abb 935
89573b37 936 if (link->network->dhcp_use_timezone) {
937 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
938 if (r < 0)
1f6860d9 939 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
89573b37 940 }
8eb9058d 941
7192bb81
LP
942 r = dhcp4_set_hostname(link);
943 if (r < 0)
a8494759 944 return r;
3c9b8860
TG
945
946 if (link->network->dhcp_vendor_class_identifier) {
947 r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
948 link->network->dhcp_vendor_class_identifier);
949 if (r < 0)
1f6860d9 950 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
3c9b8860
TG
951 }
952
af1c0de0
SS
953 if (link->network->dhcp_user_class) {
954 r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class);
955 if (r < 0)
1f6860d9 956 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m");
af1c0de0
SS
957 }
958
9faed222
SS
959 if (link->network->dhcp_client_port) {
960 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
961 if (r < 0)
1f6860d9 962 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
9faed222
SS
963 }
964
715cedfb
SS
965 if (link->network->dhcp_max_attempts > 0) {
966 r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts);
967 if (r < 0)
968 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m");
969 }
970
fff1f40c 971 return dhcp4_set_client_identifier(link);
3c9b8860 972}