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