]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-dhcp4.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / network / networkd-dhcp4.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
3c9b8860 2/***
96b2fb93 3 Copyright © 2013-2014 Tom Gundersen <teg@jklm.no>
3c9b8860
TG
4***/
5
6#include <netinet/ether.h>
7#include <linux/if.h>
8
b5efdb8a
LP
9#include "alloc-util.h"
10#include "dhcp-lease-internal.h"
958b66ea 11#include "hostname-util.h"
64b21ece 12#include "parse-util.h"
fc1ba79d 13#include "netdev/vrf.h"
3c9b8860 14#include "network-internal.h"
23f53b99
TG
15#include "networkd-link.h"
16#include "networkd-manager.h"
17#include "networkd-network.h"
64b21ece
MV
18#include "string-util.h"
19#include "sysctl-util.h"
3c9b8860 20
1c4baffc 21static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m,
3c9b8860 22 void *userdata) {
8e766630 23 _cleanup_(link_unrefp) Link *link = userdata;
3c9b8860
TG
24 int r;
25
26 assert(link);
6cf4a01c 27 assert(link->dhcp4_messages > 0);
3c9b8860 28
313cefa1 29 link->dhcp4_messages--;
3c9b8860 30
1c4baffc 31 r = sd_netlink_message_get_errno(m);
3c9b8860 32 if (r < 0 && r != -EEXIST) {
f6b8196f 33 log_link_error_errno(link, r, "Could not set DHCPv4 route: %m");
3c9b8860
TG
34 link_enter_failed(link);
35 }
36
6cf4a01c 37 if (link->dhcp4_messages == 0) {
3c9b8860 38 link->dhcp4_configured = true;
8012cd39 39 link_check_ready(link);
3c9b8860
TG
40 }
41
42 return 1;
43}
44
d6eac9bd
DW
45static int route_scope_from_address(const Route *route, const struct in_addr *self_addr) {
46 assert(route);
47 assert(self_addr);
48
49 if (in_addr_is_localhost(AF_INET, &route->dst) ||
50 (self_addr->s_addr && route->dst.in.s_addr == self_addr->s_addr))
51 return RT_SCOPE_HOST;
52 else if (in4_addr_is_null(&route->gw.in))
53 return RT_SCOPE_LINK;
54 else
55 return RT_SCOPE_UNIVERSE;
56}
57
3c9b8860 58static int link_set_dhcp_routes(Link *link) {
f8693fc7 59 _cleanup_free_ sd_dhcp_route **static_routes = NULL;
8cdc46e7
SS
60 bool classless_route = false, static_route = false;
61 struct in_addr gateway, address;
3c9b8860 62 int r, n, i;
fc1ba79d 63 uint32_t table;
3c9b8860
TG
64
65 assert(link);
0c9b15a3
AJ
66
67 if (!link->dhcp_lease) /* link went down while we configured the IP addresses? */
68 return 0;
69
70 if (!link->network) /* link went down while we configured the IP addresses? */
71 return 0;
964b26fe
SS
72
73 if (!link->network->dhcp_use_routes)
74 return 0;
3c9b8860 75
fc1ba79d
AR
76 /* When the interface is part of an VRF use the VRFs routing table, unless
77 * there is a another table specified. */
78 table = link->network->dhcp_route_table;
79 if (!link->network->dhcp_route_table_set && link->network->vrf != NULL)
80 table = VRF(link->network->vrf)->table;
81
b23aec0d
DW
82 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
83 if (r < 0)
84 return log_link_warning_errno(link, r, "DHCP error: could not get address: %m");
85
5f04a209 86 n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes);
5f04a209 87 if (n < 0)
8dc787d1 88 log_link_debug_errno(link, n, "DHCP error: could not get routes: %m");
5f04a209 89
8cdc46e7
SS
90 for (i = 0; i < n; i++) {
91 if (static_routes[i]->option == SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE)
92 classless_route = true;
93
94 if (static_routes[i]->option == SD_DHCP_OPTION_STATIC_ROUTE)
95 static_route = true;
96 }
97
5f04a209 98 for (i = 0; i < n; i++) {
8e766630 99 _cleanup_(route_freep) Route *route = NULL;
5f04a209 100
8cdc46e7
SS
101 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
102 the DHCP client MUST ignore the Static Routes option. */
103 if (classless_route && static_routes[i]->option == SD_DHCP_OPTION_STATIC_ROUTE)
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
SS
127 if (r == -ENODATA)
128 log_link_info_errno(link, r, "DHCP: No routes received from DHCP server: %m");
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
91b5f997 217 route_remove(route, link,
483d099e 218 link_route_remove_handler);
3c9b8860
TG
219 }
220 }
221 }
222 }
223
f0213e37 224 r = address_new(&address);
3c9b8860
TG
225 if (r >= 0) {
226 r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
227 if (r >= 0) {
8e766630
LP
228 _cleanup_(route_freep) Route *route_gw = NULL;
229 _cleanup_(route_freep) Route *route = NULL;
3c9b8860 230
ed9e361a 231 r = route_new(&route_gw);
3c9b8860
TG
232 if (r >= 0) {
233 route_gw->family = AF_INET;
2ce40956 234 route_gw->dst.in = gateway;
3c9b8860
TG
235 route_gw->dst_prefixlen = 32;
236 route_gw->scope = RT_SCOPE_LINK;
237
91b5f997 238 route_remove(route_gw, link,
483d099e 239 link_route_remove_handler);
3c9b8860
TG
240 }
241
ed9e361a 242 r = route_new(&route);
3c9b8860
TG
243 if (r >= 0) {
244 route->family = AF_INET;
2ce40956 245 route->gw.in = gateway;
3c9b8860 246
91b5f997 247 route_remove(route, link,
483d099e 248 link_route_remove_handler);
3c9b8860
TG
249 }
250 }
251
f414a269
TG
252 r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
253 if (r >= 0) {
254 r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
255 if (r >= 0)
5a941f5f 256 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
3c9b8860 257
f414a269
TG
258 address->family = AF_INET;
259 address->in_addr.in = addr;
260 address->prefixlen = prefixlen;
3c9b8860 261
483d099e 262 address_remove(address, link, link_address_remove_handler);
f414a269 263 }
3c9b8860
TG
264 }
265
27cb34f5 266 if (link->network->dhcp_use_mtu) {
3c9b8860
TG
267 uint16_t mtu;
268
269 r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
270 if (r >= 0 && link->original_mtu != mtu) {
271 r = link_set_mtu(link, link->original_mtu);
272 if (r < 0) {
79008bdd 273 log_link_warning(link,
3c9b8860
TG
274 "DHCP error: could not reset MTU");
275 link_enter_failed(link);
276 return r;
277 }
278 }
279 }
280
27cb34f5 281 if (link->network->dhcp_use_hostname) {
3c9b8860
TG
282 const char *hostname = NULL;
283
27cb34f5
LP
284 if (link->network->dhcp_hostname)
285 hostname = link->network->dhcp_hostname;
dce391e7
LP
286 else
287 (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
a7d0ef44 288
dce391e7
LP
289 if (hostname) {
290 /* If a hostname was set due to the lease, then unset it now. */
59eb33e0 291 r = manager_set_hostname(link->manager, NULL);
3c9b8860 292 if (r < 0)
dce391e7 293 log_link_warning_errno(link, r, "Failed to reset transient hostname: %m");
3c9b8860
TG
294 }
295 }
296
297 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
6a3e5f6a 298 link_dirty(link);
3c9b8860
TG
299 link->dhcp4_configured = false;
300
301 return 0;
302}
303
1c4baffc 304static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m,
3c9b8860 305 void *userdata) {
8e766630 306 _cleanup_(link_unrefp) Link *link = userdata;
3c9b8860
TG
307 int r;
308
309 assert(link);
310
1c4baffc 311 r = sd_netlink_message_get_errno(m);
3c9b8860 312 if (r < 0 && r != -EEXIST) {
f6b8196f 313 log_link_error_errno(link, r, "Could not set DHCPv4 address: %m");
3c9b8860 314 link_enter_failed(link);
45af44d4 315 } else if (r >= 0)
200a0868 316 manager_rtnl_process_address(rtnl, m, link->manager);
3c9b8860
TG
317
318 link_set_dhcp_routes(link);
319
223932c7
AH
320 if (link->dhcp4_messages == 0) {
321 link->dhcp4_configured = true;
322 link_check_ready(link);
323 }
324
3c9b8860
TG
325 return 1;
326}
327
328static int dhcp4_update_address(Link *link,
329 struct in_addr *address,
330 struct in_addr *netmask,
331 uint32_t lifetime) {
8e766630 332 _cleanup_(address_freep) Address *addr = NULL;
3c9b8860
TG
333 unsigned prefixlen;
334 int r;
335
336 assert(address);
337 assert(netmask);
338 assert(lifetime);
339
5a941f5f 340 prefixlen = in4_addr_netmask_to_prefixlen(netmask);
3c9b8860 341
f0213e37 342 r = address_new(&addr);
3c9b8860
TG
343 if (r < 0)
344 return r;
345
346 addr->family = AF_INET;
347 addr->in_addr.in.s_addr = address->s_addr;
348 addr->cinfo.ifa_prefered = lifetime;
349 addr->cinfo.ifa_valid = lifetime;
350 addr->prefixlen = prefixlen;
351 addr->broadcast.s_addr = address->s_addr | ~netmask->s_addr;
352
66669078
TG
353 /* allow reusing an existing address and simply update its lifetime
354 * in case it already exists */
483d099e 355 r = address_configure(addr, link, dhcp4_address_handler, true);
3c9b8860
TG
356 if (r < 0)
357 return r;
358
359 return 0;
360}
361
362static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
363 sd_dhcp_lease *lease;
364 struct in_addr address;
365 struct in_addr netmask;
366 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
367 int r;
368
369 assert(link);
370 assert(client);
371 assert(link->network);
372
373 r = sd_dhcp_client_get_lease(client, &lease);
f6b8196f
LP
374 if (r < 0)
375 return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
3c9b8860
TG
376
377 sd_dhcp_lease_unref(link->dhcp_lease);
378 link->dhcp4_configured = false;
e6b18ffa 379 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 380 link_dirty(link);
3c9b8860
TG
381
382 r = sd_dhcp_lease_get_address(lease, &address);
f6b8196f
LP
383 if (r < 0)
384 return log_link_warning_errno(link, r, "DHCP error: no address: %m");
3c9b8860
TG
385
386 r = sd_dhcp_lease_get_netmask(lease, &netmask);
f6b8196f
LP
387 if (r < 0)
388 return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
3c9b8860
TG
389
390 if (!link->network->dhcp_critical) {
f6b8196f
LP
391 r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
392 if (r < 0)
393 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
3c9b8860
TG
394 }
395
396 r = dhcp4_update_address(link, &address, &netmask, lifetime);
397 if (r < 0) {
f6b8196f 398 log_link_warning_errno(link, r, "Could not update IP address: %m");
3c9b8860
TG
399 link_enter_failed(link);
400 return r;
401 }
402
403 return 0;
404}
405
406static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
407 sd_dhcp_lease *lease;
408 struct in_addr address;
409 struct in_addr netmask;
410 struct in_addr gateway;
411 unsigned prefixlen;
412 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
413 int r;
414
415 assert(client);
416 assert(link);
417
418 r = sd_dhcp_client_get_lease(client, &lease);
f2341e0a 419 if (r < 0)
f6b8196f 420 return log_link_error_errno(link, r, "DHCP error: No lease: %m");
3c9b8860
TG
421
422 r = sd_dhcp_lease_get_address(lease, &address);
f2341e0a 423 if (r < 0)
f6b8196f 424 return log_link_error_errno(link, r, "DHCP error: No address: %m");
3c9b8860
TG
425
426 r = sd_dhcp_lease_get_netmask(lease, &netmask);
f2341e0a 427 if (r < 0)
f6b8196f 428 return log_link_error_errno(link, r, "DHCP error: No netmask: %m");
3c9b8860 429
5a941f5f 430 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
3c9b8860
TG
431
432 r = sd_dhcp_lease_get_router(lease, &gateway);
397d15fd 433 if (r < 0 && r != -ENODATA)
f6b8196f 434 return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
3c9b8860
TG
435
436 if (r >= 0)
f2341e0a
LP
437 log_struct(LOG_INFO,
438 LOG_LINK_INTERFACE(link),
439 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
440 ADDRESS_FMT_VAL(address),
441 prefixlen,
442 ADDRESS_FMT_VAL(gateway)),
443 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
444 "PREFIXLEN=%u", prefixlen,
a1230ff9 445 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(gateway));
3c9b8860 446 else
f2341e0a
LP
447 log_struct(LOG_INFO,
448 LOG_LINK_INTERFACE(link),
449 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u",
450 ADDRESS_FMT_VAL(address),
451 prefixlen),
452 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
a1230ff9 453 "PREFIXLEN=%u", prefixlen);
3c9b8860 454
e6b18ffa 455 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 456 link_dirty(link);
3c9b8860 457
27cb34f5 458 if (link->network->dhcp_use_mtu) {
3c9b8860
TG
459 uint16_t mtu;
460
461 r = sd_dhcp_lease_get_mtu(lease, &mtu);
462 if (r >= 0) {
463 r = link_set_mtu(link, mtu);
464 if (r < 0)
f2341e0a 465 log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu);
3c9b8860
TG
466 }
467 }
468
27cb34f5 469 if (link->network->dhcp_use_hostname) {
2de2abad
LB
470 const char *dhcpname = NULL;
471 _cleanup_free_ char *hostname = NULL;
3c9b8860 472
27cb34f5 473 if (link->network->dhcp_hostname)
2de2abad 474 dhcpname = link->network->dhcp_hostname;
dce391e7 475 else
2de2abad
LB
476 (void) sd_dhcp_lease_get_hostname(lease, &dhcpname);
477
478 if (dhcpname) {
479 r = shorten_overlong(dhcpname, &hostname);
480 if (r < 0)
481 log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
482 if (r == 1)
483 log_link_notice(link, "Overlong DCHP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
484 }
a7d0ef44 485
dce391e7 486 if (hostname) {
59eb33e0 487 r = manager_set_hostname(link->manager, hostname);
3c9b8860 488 if (r < 0)
f2341e0a 489 log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
3c9b8860
TG
490 }
491 }
492
27cb34f5 493 if (link->network->dhcp_use_timezone) {
21b80ad1
LP
494 const char *tz = NULL;
495
496 (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
497
498 if (tz) {
59eb33e0 499 r = manager_set_timezone(link->manager, tz);
21b80ad1
LP
500 if (r < 0)
501 log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz);
502 }
503 }
504
3c9b8860 505 if (!link->network->dhcp_critical) {
f2341e0a 506 r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
3c9b8860 507 if (r < 0) {
f2341e0a 508 log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
3c9b8860
TG
509 return r;
510 }
511 }
512
513 r = dhcp4_update_address(link, &address, &netmask, lifetime);
514 if (r < 0) {
f2341e0a 515 log_link_warning_errno(link, r, "Could not update IP address: %m");
3c9b8860
TG
516 link_enter_failed(link);
517 return r;
518 }
519
520 return 0;
521}
522static void dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
523 Link *link = userdata;
524 int r = 0;
525
526 assert(link);
527 assert(link->network);
528 assert(link->manager);
529
530 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
531 return;
532
533 switch (event) {
03748142
DH
534 case SD_DHCP_CLIENT_EVENT_EXPIRED:
535 case SD_DHCP_CLIENT_EVENT_STOP:
536 case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
3c9b8860 537 if (link->network->dhcp_critical) {
f6b8196f 538 log_link_error(link, "DHCPv4 connection considered system critical, ignoring request to reconfigure it.");
3c9b8860
TG
539 return;
540 }
541
542 if (link->dhcp_lease) {
543 r = dhcp_lease_lost(link);
544 if (r < 0) {
545 link_enter_failed(link);
546 return;
547 }
548 }
549
03748142 550 if (event == SD_DHCP_CLIENT_EVENT_IP_CHANGE) {
3c9b8860
TG
551 r = dhcp_lease_acquired(client, link);
552 if (r < 0) {
553 link_enter_failed(link);
554 return;
555 }
556 }
557
558 break;
03748142 559 case SD_DHCP_CLIENT_EVENT_RENEW:
3c9b8860
TG
560 r = dhcp_lease_renew(client, link);
561 if (r < 0) {
562 link_enter_failed(link);
563 return;
564 }
565 break;
03748142 566 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
3c9b8860
TG
567 r = dhcp_lease_acquired(client, link);
568 if (r < 0) {
569 link_enter_failed(link);
570 return;
571 }
572 break;
573 default:
574 if (event < 0)
f6b8196f 575 log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
3c9b8860 576 else
f6b8196f 577 log_link_warning(link, "DHCP unknown event: %i", event);
3c9b8860
TG
578 break;
579 }
580
581 return;
582}
583
7192bb81
LP
584static int dhcp4_set_hostname(Link *link) {
585 _cleanup_free_ char *hostname = NULL;
586 const char *hn;
587 int r;
588
589 assert(link);
590
591 if (!link->network->dhcp_send_hostname)
592 hn = NULL;
593 else if (link->network->dhcp_hostname)
594 hn = link->network->dhcp_hostname;
595 else {
596 r = gethostname_strict(&hostname);
597 if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
598 return r;
599
600 hn = hostname;
601 }
602
603 return sd_dhcp_client_set_hostname(link->dhcp_client, hn);
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
3c9b8860
TG
659int dhcp4_configure(Link *link) {
660 int r;
661
662 assert(link);
663 assert(link->network);
e0ee46f2 664 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
3c9b8860 665
0bc70f1d 666 if (!link->dhcp_client) {
db3d2358 667 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
0bc70f1d
TG
668 if (r < 0)
669 return r;
670 }
3c9b8860
TG
671
672 r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
673 if (r < 0)
674 return r;
675
76253e73
DW
676 r = sd_dhcp_client_set_mac(link->dhcp_client,
677 (const uint8_t *) &link->mac,
678 sizeof (link->mac), ARPHRD_ETHER);
3c9b8860
TG
679 if (r < 0)
680 return r;
681
2f8e7633 682 r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
3c9b8860
TG
683 if (r < 0)
684 return r;
685
686 r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
687 if (r < 0)
688 return r;
689
690 r = sd_dhcp_client_set_request_broadcast(link->dhcp_client,
691 link->network->dhcp_broadcast);
692 if (r < 0)
693 return r;
694
695 if (link->mtu) {
696 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
697 if (r < 0)
698 return r;
699 }
700
27cb34f5 701 if (link->network->dhcp_use_mtu) {
39745a5a 702 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 703 SD_DHCP_OPTION_INTERFACE_MTU);
39745a5a
LP
704 if (r < 0)
705 return r;
3c9b8860
TG
706 }
707
5e77a146 708 /* NOTE: even if this variable is called "use", it also "sends" PRL
709 * options, maybe there should be a different configuration variable
710 * to send or not route options?. */
28522b0d 711 /* NOTE: when using Anonymize=yes, routes PRL options are sent
712 * by default, so they don't need to be added here. */
5e77a146 713 if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) {
3c9b8860 714 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 715 SD_DHCP_OPTION_STATIC_ROUTE);
3c9b8860
TG
716 if (r < 0)
717 return r;
718 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 719 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
7d6884b6
TA
720 if (r < 0)
721 return r;
3c9b8860
TG
722 }
723
ead36ce6 724 if (link->network->dhcp_use_ntp) {
725 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
726 if (r < 0)
727 return r;
728 }
4b7b5abb 729
89573b37 730 if (link->network->dhcp_use_timezone) {
731 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
732 if (r < 0)
733 return r;
734 }
8eb9058d 735
7192bb81
LP
736 r = dhcp4_set_hostname(link);
737 if (r < 0)
738 return r;
3c9b8860
TG
739
740 if (link->network->dhcp_vendor_class_identifier) {
741 r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
742 link->network->dhcp_vendor_class_identifier);
743 if (r < 0)
744 return r;
745 }
746
af1c0de0
SS
747 if (link->network->dhcp_user_class) {
748 r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class);
749 if (r < 0)
750 return r;
751 }
752
9faed222
SS
753 if (link->network->dhcp_client_port) {
754 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
755 if (r < 0)
756 return r;
757 }
758
3e43b2cd 759 switch (link->network->dhcp_client_identifier) {
8341a5c3 760 case DHCP_CLIENT_ID_DUID: {
413708d1 761 /* If configured, apply user specified DUID and/or IAID */
8341a5c3
ZJS
762 const DUID *duid = link_duid(link);
763
764 r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
765 link->network->iaid,
766 duid->type,
767 duid->raw_data_len > 0 ? duid->raw_data : NULL,
768 duid->raw_data_len);
413708d1
VK
769 if (r < 0)
770 return r;
3e43b2cd 771 break;
8341a5c3 772 }
dace710c
YW
773 case DHCP_CLIENT_ID_DUID_ONLY: {
774 /* If configured, apply user specified DUID */
775 const DUID *duid = link_duid(link);
776
777 r = sd_dhcp_client_set_duid(link->dhcp_client,
778 duid->type,
779 duid->raw_data_len > 0 ? duid->raw_data : NULL,
780 duid->raw_data_len);
781 if (r < 0)
782 return r;
783 break;
784 }
3e43b2cd
JJ
785 case DHCP_CLIENT_ID_MAC:
786 r = sd_dhcp_client_set_client_id(link->dhcp_client,
787 ARPHRD_ETHER,
788 (const uint8_t *) &link->mac,
8341a5c3 789 sizeof(link->mac));
3e43b2cd
JJ
790 if (r < 0)
791 return r;
792 break;
793 default:
794 assert_not_reached("Unknown client identifier type.");
795 }
796
3c9b8860
TG
797 return 0;
798}