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