]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-dhcp4.c
network dhcp4: Add support send request options in a generic manner
[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 4#include <linux/if.h>
8f815e8b 5#include <linux/if_arp.h>
3c9b8860 6
b5efdb8a 7#include "alloc-util.h"
958b66ea 8#include "hostname-util.h"
64b21ece 9#include "parse-util.h"
3c9b8860 10#include "network-internal.h"
ca5ad760 11#include "networkd-dhcp4.h"
23f53b99
TG
12#include "networkd-link.h"
13#include "networkd-manager.h"
14#include "networkd-network.h"
ca5ad760 15#include "string-table.h"
64b21ece
MV
16#include "string-util.h"
17#include "sysctl-util.h"
3c9b8860 18
d4c52ee5
YW
19static int dhcp_remove_routes(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all);
20static int dhcp_remove_router(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all);
854a1ccf 21static int dhcp_remove_dns_routes(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all);
d4c52ee5 22static int dhcp_remove_address(Link *link, sd_dhcp_lease *lease, const struct in_addr *address);
d03073dd
YW
23
24void dhcp4_release_old_lease(Link *link) {
d4c52ee5 25 struct in_addr address = {}, address_old = {};
d03073dd
YW
26
27 assert(link);
28
29 if (!link->dhcp_lease_old)
30 return;
31
32 assert(link->dhcp_lease);
33
d4c52ee5
YW
34 (void) sd_dhcp_lease_get_address(link->dhcp_lease_old, &address_old);
35 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address);
d03073dd 36
d4c52ee5
YW
37 (void) dhcp_remove_routes(link, link->dhcp_lease_old, &address_old, false);
38 (void) dhcp_remove_router(link, link->dhcp_lease_old, &address_old, false);
854a1ccf 39 (void) dhcp_remove_dns_routes(link, link->dhcp_lease_old, &address_old, false);
d03073dd 40
d4c52ee5
YW
41 if (!in4_addr_equal(&address_old, &address))
42 (void) dhcp_remove_address(link, link->dhcp_lease_old, &address_old);
d03073dd
YW
43
44 link->dhcp_lease_old = sd_dhcp_lease_unref(link->dhcp_lease_old);
45 link_dirty(link);
46}
47
302a796f 48static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3c9b8860
TG
49 int r;
50
51 assert(link);
6cf4a01c 52 assert(link->dhcp4_messages > 0);
3c9b8860 53
313cefa1 54 link->dhcp4_messages--;
3c9b8860 55
3ab7ed3f
YW
56 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
57 return 1;
58
1c4baffc 59 r = sd_netlink_message_get_errno(m);
3c9b8860 60 if (r < 0 && r != -EEXIST) {
f6b8196f 61 log_link_error_errno(link, r, "Could not set DHCPv4 route: %m");
3c9b8860 62 link_enter_failed(link);
3ab7ed3f 63 return 1;
3c9b8860
TG
64 }
65
6cf4a01c 66 if (link->dhcp4_messages == 0) {
3c9b8860 67 link->dhcp4_configured = true;
d03073dd
YW
68 /* New address and routes are configured now. Let's release old lease. */
69 dhcp4_release_old_lease(link);
8012cd39 70 link_check_ready(link);
3c9b8860
TG
71 }
72
73 return 1;
74}
75
d6eac9bd
DW
76static int route_scope_from_address(const Route *route, const struct in_addr *self_addr) {
77 assert(route);
78 assert(self_addr);
79
7ed5420a
YW
80 if (in4_addr_is_localhost(&route->dst.in) ||
81 (!in4_addr_is_null(self_addr) && in4_addr_equal(&route->dst.in, self_addr)))
d6eac9bd
DW
82 return RT_SCOPE_HOST;
83 else if (in4_addr_is_null(&route->gw.in))
84 return RT_SCOPE_LINK;
85 else
86 return RT_SCOPE_UNIVERSE;
87}
88
d4c52ee5
YW
89static int dhcp_route_configure(Route **route, Link *link) {
90 int r;
91
92 assert(route);
93 assert(*route);
94 assert(link);
95
96 if (set_contains(link->dhcp_routes, *route))
97 return 0;
98
99 r = route_configure(*route, link, dhcp4_route_handler);
100 if (r <= 0)
101 return r;
102
103 link->dhcp4_messages++;
104
105 r = set_put(link->dhcp_routes, *route);
106 if (r < 0)
107 return r;
108
109 TAKE_PTR(*route);
110 return 0;
111}
112
854a1ccf
YW
113static int link_set_dns_routes(Link *link, const struct in_addr *address) {
114 const struct in_addr *dns;
115 uint32_t table;
116 int i, n, r;
117
118 assert(link);
119 assert(link->dhcp_lease);
120 assert(link->network);
121
a24e12f0
YW
122 if (!link->network->dhcp_use_dns ||
123 !link->network->dhcp_routes_to_dns)
854a1ccf
YW
124 return 0;
125
126 n = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns);
127 if (IN_SET(n, 0, -ENODATA))
128 return 0;
129 if (n < 0)
130 return log_link_warning_errno(link, n, "DHCP error: could not get DNS servers: %m");
131
132 table = link_get_dhcp_route_table(link);
133
134 for (i = 0; i < n; i ++) {
135 _cleanup_(route_freep) Route *route = NULL;
136
137 r = route_new(&route);
138 if (r < 0)
139 return log_link_error_errno(link, r, "Could not allocate route: %m");
140
141 /* Set routes to DNS servers. */
142
143 route->family = AF_INET;
144 route->dst.in = dns[i];
145 route->dst_prefixlen = 32;
146 route->prefsrc.in = *address;
147 route->scope = RT_SCOPE_LINK;
148 route->protocol = RTPROT_DHCP;
149 route->priority = link->network->dhcp_route_metric;
150 route->table = table;
151
152 r = dhcp_route_configure(&route, link);
153 if (r < 0)
154 return log_link_error_errno(link, r, "Could not set route to DNS server: %m");
155 }
156
157 return 0;
158}
159
3c9b8860 160static int link_set_dhcp_routes(Link *link) {
f8693fc7 161 _cleanup_free_ sd_dhcp_route **static_routes = NULL;
8cdc46e7 162 bool classless_route = false, static_route = false;
f8862395
TH
163 const struct in_addr *router;
164 struct in_addr address;
3c9b8860 165 int r, n, i;
fc1ba79d 166 uint32_t table;
3c9b8860
TG
167
168 assert(link);
0c9b15a3
AJ
169
170 if (!link->dhcp_lease) /* link went down while we configured the IP addresses? */
171 return 0;
172
173 if (!link->network) /* link went down while we configured the IP addresses? */
174 return 0;
964b26fe
SS
175
176 if (!link->network->dhcp_use_routes)
177 return 0;
3c9b8860 178
4e2ef9d9
YW
179 if (!link_has_carrier(link) && !link->network->configure_without_carrier)
180 /* During configuring addresses, the link lost its carrier. As networkd is dropping
181 * the addresses now, let's not configure the routes either. */
182 return 0;
183
c077a205 184 r = set_ensure_allocated(&link->dhcp_routes, &route_hash_ops);
d4c52ee5
YW
185 if (r < 0)
186 return log_oom();
187
188 /* Clear old entries in case the set was already allocated */
189 set_clear(link->dhcp_routes);
190
bdb9f580 191 table = link_get_dhcp_route_table(link);
fc1ba79d 192
b23aec0d
DW
193 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
194 if (r < 0)
195 return log_link_warning_errno(link, r, "DHCP error: could not get address: %m");
196
5f04a209 197 n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes);
599c44e6
YW
198 if (n == -ENODATA)
199 log_link_debug_errno(link, n, "DHCP: No routes received from DHCP server: %m");
200 else if (n < 0)
61cda4d7 201 log_link_debug_errno(link, n, "DHCP: could not get routes: %m");
5f04a209 202
8cdc46e7 203 for (i = 0; i < n; i++) {
3476951c
TH
204 switch (sd_dhcp_route_get_option(static_routes[i])) {
205 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE:
8cdc46e7 206 classless_route = true;
3476951c
TH
207 break;
208 case SD_DHCP_OPTION_STATIC_ROUTE:
8cdc46e7 209 static_route = true;
3476951c
TH
210 break;
211 }
8cdc46e7
SS
212 }
213
5f04a209 214 for (i = 0; i < n; i++) {
8e766630 215 _cleanup_(route_freep) Route *route = NULL;
5f04a209 216
8cdc46e7
SS
217 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
218 the DHCP client MUST ignore the Static Routes option. */
3476951c
TH
219 if (classless_route &&
220 sd_dhcp_route_get_option(static_routes[i]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE)
8cdc46e7
SS
221 continue;
222
5f04a209
SS
223 r = route_new(&route);
224 if (r < 0)
225 return log_link_error_errno(link, r, "Could not allocate route: %m");
226
227 route->family = AF_INET;
228 route->protocol = RTPROT_DHCP;
229 assert_se(sd_dhcp_route_get_gateway(static_routes[i], &route->gw.in) >= 0);
230 assert_se(sd_dhcp_route_get_destination(static_routes[i], &route->dst.in) >= 0);
231 assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen) >= 0);
232 route->priority = link->network->dhcp_route_metric;
233 route->table = table;
234 route->scope = route_scope_from_address(route, &address);
ac2dce5f
DS
235 if (IN_SET(route->scope, RT_SCOPE_LINK, RT_SCOPE_UNIVERSE))
236 route->prefsrc.in = address;
5f04a209 237
d4c52ee5
YW
238 if (set_contains(link->dhcp_routes, route))
239 continue;
240
241 r = dhcp_route_configure(&route, link);
5f04a209 242 if (r < 0)
d4c52ee5 243 return log_link_error_errno(link, r, "Could not set route: %m");
5f04a209
SS
244 }
245
f8862395 246 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
825ace96
YW
247 if (IN_SET(r, 0, -ENODATA))
248 log_link_info(link, "DHCP: No gateway received from DHCP server.");
249 else if (r < 0)
444b0170 250 log_link_warning_errno(link, r, "DHCP error: could not get gateway: %m");
825ace96
YW
251 else if (in4_addr_is_null(&router[0]))
252 log_link_info(link, "DHCP: Received gateway is null.");
f6b8196f 253
5f04a209
SS
254 /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
255 a Router option, the DHCP client MUST ignore the Router option. */
8cdc46e7
SS
256 if (classless_route && static_route)
257 log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option and router option");
258
f8862395 259 if (r > 0 && !classless_route && !in4_addr_is_null(&router[0])) {
860e636c 260 _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL;
ed9e361a
TG
261
262 r = route_new(&route_gw);
f6b8196f
LP
263 if (r < 0)
264 return log_link_error_errno(link, r, "Could not allocate route: %m");
3c9b8860
TG
265
266 /* The dhcp netmask may mask out the gateway. Add an explicit
267 * route for the gw host so that we can route no matter the
268 * netmask or existing kernel route tables. */
269 route_gw->family = AF_INET;
f8862395 270 route_gw->dst.in = router[0];
3c9b8860 271 route_gw->dst_prefixlen = 32;
2ce40956 272 route_gw->prefsrc.in = address;
3c9b8860 273 route_gw->scope = RT_SCOPE_LINK;
ed9e361a 274 route_gw->protocol = RTPROT_DHCP;
86655331 275 route_gw->priority = link->network->dhcp_route_metric;
fc1ba79d 276 route_gw->table = table;
3c9b8860 277
d4c52ee5 278 r = dhcp_route_configure(&route_gw, link);
f6b8196f 279 if (r < 0)
1590dfa4 280 return log_link_error_errno(link, r, "Could not set host route: %m");
3c9b8860 281
860e636c
YW
282 r = route_new(&route);
283 if (r < 0)
284 return log_link_error_errno(link, r, "Could not allocate route: %m");
285
3c9b8860 286 route->family = AF_INET;
f8862395 287 route->gw.in = router[0];
2ce40956 288 route->prefsrc.in = address;
860e636c 289 route->protocol = RTPROT_DHCP;
86655331 290 route->priority = link->network->dhcp_route_metric;
fc1ba79d 291 route->table = table;
3c9b8860 292
d4c52ee5 293 r = dhcp_route_configure(&route, link);
1590dfa4 294 if (r < 0)
d4c52ee5 295 return log_link_error_errno(link, r, "Could not set router: %m");
3c9b8860
TG
296 }
297
854a1ccf 298 return link_set_dns_routes(link, &address);
3c9b8860
TG
299}
300
d4c52ee5
YW
301static int dhcp_remove_routes(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all) {
302 _cleanup_free_ sd_dhcp_route **routes = NULL;
7fa472f9 303 uint32_t table;
d4c52ee5 304 int n, i, r;
3c9b8860
TG
305
306 assert(link);
7fa472f9 307 assert(address);
3c9b8860 308
7fa472f9
YW
309 if (!link->network->dhcp_use_routes)
310 return 0;
3c9b8860 311
d03073dd
YW
312 n = sd_dhcp_lease_get_routes(lease, &routes);
313 if (IN_SET(n, 0, -ENODATA))
7fa472f9 314 return 0;
d03073dd
YW
315 else if (n < 0)
316 return log_link_error_errno(link, n, "DHCP error: Failed to get routes: %m");
317
7fa472f9 318 table = link_get_dhcp_route_table(link);
8df8ce78 319
7fa472f9
YW
320 for (i = 0; i < n; i++) {
321 _cleanup_(route_freep) Route *route = NULL;
3c9b8860 322
7fa472f9
YW
323 r = route_new(&route);
324 if (r < 0)
325 return log_oom();
326
327 route->family = AF_INET;
328 assert_se(sd_dhcp_route_get_gateway(routes[i], &route->gw.in) >= 0);
329 assert_se(sd_dhcp_route_get_destination(routes[i], &route->dst.in) >= 0);
330 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &route->dst_prefixlen) >= 0);
331 route->priority = link->network->dhcp_route_metric;
332 route->table = table;
333 route->scope = route_scope_from_address(route, address);
ac2dce5f
DS
334 if (IN_SET(route->scope, RT_SCOPE_LINK, RT_SCOPE_UNIVERSE))
335 route->prefsrc.in = *address;
7fa472f9 336
d4c52ee5 337 if (!remove_all && set_contains(link->dhcp_routes, route))
d03073dd
YW
338 continue;
339
7fa472f9 340 (void) route_remove(route, link, NULL);
b5799eeb 341 }
3c9b8860 342
7fa472f9
YW
343 return n;
344}
8df8ce78 345
d4c52ee5 346static int dhcp_remove_router(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all) {
7fa472f9
YW
347 _cleanup_(route_freep) Route *route_gw = NULL, *route = NULL;
348 const struct in_addr *router;
349 uint32_t table;
350 int r;
8df8ce78 351
7fa472f9
YW
352 assert(link);
353 assert(address);
3c9b8860 354
7fa472f9
YW
355 if (!link->network->dhcp_use_routes)
356 return 0;
3c9b8860 357
d03073dd 358 r = sd_dhcp_lease_get_router(lease, &router);
7fa472f9
YW
359 if (IN_SET(r, 0, -ENODATA)) {
360 log_link_debug(link, "DHCP: No gateway received from DHCP server.");
361 return 0;
362 } else if (r < 0)
363 return log_link_error_errno(link, r, "DHCP error: could not get gateway: %m");
364 else if (in4_addr_is_null(&router[0])) {
365 log_link_info(link, "DHCP: Received gateway is null, ignoring.");
366 return 0;
3c9b8860
TG
367 }
368
7fa472f9 369 table = link_get_dhcp_route_table(link);
3c9b8860 370
7fa472f9
YW
371 r = route_new(&route_gw);
372 if (r < 0)
373 return log_oom();
3c9b8860 374
7fa472f9
YW
375 route_gw->family = AF_INET;
376 route_gw->dst.in = router[0];
377 route_gw->dst_prefixlen = 32;
378 route_gw->prefsrc.in = *address;
379 route_gw->scope = RT_SCOPE_LINK;
380 route_gw->protocol = RTPROT_DHCP;
381 route_gw->priority = link->network->dhcp_route_metric;
382 route_gw->table = table;
a7d0ef44 383
d4c52ee5
YW
384 if (remove_all || !set_contains(link->dhcp_routes, route_gw))
385 (void) route_remove(route_gw, link, NULL);
7fa472f9
YW
386
387 r = route_new(&route);
388 if (r < 0)
389 return log_oom();
390
391 route->family = AF_INET;
392 route->gw.in = router[0];
393 route->prefsrc.in = *address;
394 route->protocol = RTPROT_DHCP;
395 route->priority = link->network->dhcp_route_metric;
396 route->table = table;
397
d4c52ee5
YW
398 if (remove_all || !set_contains(link->dhcp_routes, route))
399 (void) route_remove(route, link, NULL);
7fa472f9
YW
400
401 return 0;
402}
403
854a1ccf
YW
404static int dhcp_remove_dns_routes(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all) {
405 const struct in_addr *dns;
406 uint32_t table;
407 int i, n, r;
408
409 assert(link);
410 assert(lease);
411 assert(link->network);
412
a24e12f0
YW
413 if (!link->network->dhcp_use_dns ||
414 !link->network->dhcp_routes_to_dns)
854a1ccf
YW
415 return 0;
416
417 n = sd_dhcp_lease_get_dns(lease, &dns);
418 if (IN_SET(n, 0, -ENODATA))
419 return 0;
420 if (n < 0)
421 return log_link_warning_errno(link, n, "DHCP error: could not get DNS servers: %m");
422
423 table = link_get_dhcp_route_table(link);
424
425 for (i = 0; i < n; i ++) {
426 _cleanup_(route_freep) Route *route = NULL;
427
428 r = route_new(&route);
429 if (r < 0)
430 return log_link_error_errno(link, r, "Could not allocate route: %m");
431
432 route->family = AF_INET;
433 route->dst.in = dns[i];
434 route->dst_prefixlen = 32;
435 route->prefsrc.in = *address;
436 route->scope = RT_SCOPE_LINK;
437 route->protocol = RTPROT_DHCP;
438 route->priority = link->network->dhcp_route_metric;
439 route->table = table;
440
441 if (!remove_all && set_contains(link->dhcp_routes, route))
442 continue;
443
444 (void) route_remove(route, link, NULL);
445 }
446
447 return 0;
448}
449
d4c52ee5 450static int dhcp_remove_address(Link *link, sd_dhcp_lease *lease, const struct in_addr *address) {
7fa472f9
YW
451 _cleanup_(address_freep) Address *a = NULL;
452 struct in_addr netmask;
453 int r;
454
455 assert(link);
456 assert(address);
457
458 if (in4_addr_is_null(address))
459 return 0;
460
461 r = address_new(&a);
462 if (r < 0)
463 return log_oom();
464
465 a->family = AF_INET;
466 a->in_addr.in = *address;
467
d03073dd 468 if (sd_dhcp_lease_get_netmask(lease, &netmask) >= 0)
7fa472f9
YW
469 a->prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
470
471 (void) address_remove(a, link, NULL);
472
473 return 0;
474}
475
476static int dhcp_reset_mtu(Link *link) {
477 uint16_t mtu;
478 int r;
479
480 assert(link);
481
482 if (!link->network->dhcp_use_mtu)
483 return 0;
484
485 r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
486 if (r < 0)
487 return r;
488
489 if (link->original_mtu == mtu)
490 return 0;
491
492 r = link_set_mtu(link, link->original_mtu);
493 if (r < 0) {
494 log_link_error_errno(link, r, "DHCP error: could not reset MTU: %m");
495 link_enter_failed(link);
496 return r;
3c9b8860
TG
497 }
498
7fa472f9
YW
499 return 0;
500}
501
502static int dhcp_reset_hostname(Link *link) {
503 const char *hostname;
504 int r;
505
506 assert(link);
507
508 if (!link->network->dhcp_use_hostname)
509 return 0;
510
511 hostname = link->network->dhcp_hostname;
512 if (!hostname)
513 (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
514
515 if (!hostname)
516 return 0;
517
518 /* If a hostname was set due to the lease, then unset it now. */
519 r = manager_set_hostname(link->manager, NULL);
520 if (r < 0)
521 return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m");
522
523 return 0;
524}
525
526static int dhcp_lease_lost(Link *link) {
527 struct in_addr address = {};
528
529 assert(link);
530 assert(link->dhcp_lease);
531
f8c2e4b9 532 log_link_info(link, "DHCP lease lost");
7fa472f9
YW
533
534 link->dhcp4_configured = false;
535
536 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address);
d4c52ee5
YW
537 (void) dhcp_remove_routes(link, link->dhcp_lease, &address, true);
538 (void) dhcp_remove_router(link, link->dhcp_lease, &address, true);
854a1ccf 539 (void) dhcp_remove_dns_routes(link, link->dhcp_lease, &address, true);
d03073dd 540 (void) dhcp_remove_address(link, link->dhcp_lease, &address);
7fa472f9
YW
541 (void) dhcp_reset_mtu(link);
542 (void) dhcp_reset_hostname(link);
543
3c9b8860 544 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
6a3e5f6a 545 link_dirty(link);
3c9b8860
TG
546
547 return 0;
548}
549
302a796f 550static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3c9b8860
TG
551 int r;
552
553 assert(link);
554
3ab7ed3f
YW
555 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
556 return 1;
557
1c4baffc 558 r = sd_netlink_message_get_errno(m);
3c9b8860 559 if (r < 0 && r != -EEXIST) {
f6b8196f 560 log_link_error_errno(link, r, "Could not set DHCPv4 address: %m");
3c9b8860 561 link_enter_failed(link);
3ab7ed3f 562 return 1;
4ff296b0
YW
563 } else if (r >= 0)
564 (void) manager_rtnl_process_address(rtnl, m, link->manager);
3c9b8860 565
1590dfa4
YW
566 r = link_set_dhcp_routes(link);
567 if (r < 0) {
568 link_enter_failed(link);
569 return 1;
570 }
3c9b8860 571
b5799eeb 572 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
4ff296b0
YW
573 r = link_request_set_routes(link);
574 if (r < 0) {
575 link_enter_failed(link);
576 return 1;
577 }
b5799eeb 578
223932c7
AH
579 if (link->dhcp4_messages == 0) {
580 link->dhcp4_configured = true;
d03073dd
YW
581 /* The new address is configured, and no route is requested.
582 * Let's drop the old lease. */
583 dhcp4_release_old_lease(link);
223932c7
AH
584 link_check_ready(link);
585 }
586
3c9b8860
TG
587 return 1;
588}
589
590static int dhcp4_update_address(Link *link,
591 struct in_addr *address,
592 struct in_addr *netmask,
593 uint32_t lifetime) {
8e766630 594 _cleanup_(address_freep) Address *addr = NULL;
3c9b8860
TG
595 unsigned prefixlen;
596 int r;
597
598 assert(address);
599 assert(netmask);
600 assert(lifetime);
601
5a941f5f 602 prefixlen = in4_addr_netmask_to_prefixlen(netmask);
3c9b8860 603
f0213e37 604 r = address_new(&addr);
3c9b8860
TG
605 if (r < 0)
606 return r;
607
608 addr->family = AF_INET;
609 addr->in_addr.in.s_addr = address->s_addr;
610 addr->cinfo.ifa_prefered = lifetime;
611 addr->cinfo.ifa_valid = lifetime;
612 addr->prefixlen = prefixlen;
613 addr->broadcast.s_addr = address->s_addr | ~netmask->s_addr;
614
66669078
TG
615 /* allow reusing an existing address and simply update its lifetime
616 * in case it already exists */
483d099e 617 r = address_configure(addr, link, dhcp4_address_handler, true);
3c9b8860
TG
618 if (r < 0)
619 return r;
620
621 return 0;
622}
623
624static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
625 sd_dhcp_lease *lease;
626 struct in_addr address;
627 struct in_addr netmask;
628 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
629 int r;
630
631 assert(link);
632 assert(client);
633 assert(link->network);
634
635 r = sd_dhcp_client_get_lease(client, &lease);
f6b8196f
LP
636 if (r < 0)
637 return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
3c9b8860
TG
638
639 sd_dhcp_lease_unref(link->dhcp_lease);
640 link->dhcp4_configured = false;
e6b18ffa 641 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 642 link_dirty(link);
3c9b8860
TG
643
644 r = sd_dhcp_lease_get_address(lease, &address);
f6b8196f
LP
645 if (r < 0)
646 return log_link_warning_errno(link, r, "DHCP error: no address: %m");
3c9b8860
TG
647
648 r = sd_dhcp_lease_get_netmask(lease, &netmask);
f6b8196f
LP
649 if (r < 0)
650 return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
3c9b8860 651
7da377ef 652 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
f6b8196f
LP
653 r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
654 if (r < 0)
655 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
3c9b8860
TG
656 }
657
658 r = dhcp4_update_address(link, &address, &netmask, lifetime);
a2f68490
YW
659 if (r < 0)
660 return log_link_warning_errno(link, r, "Could not update IP address: %m");
3c9b8860
TG
661
662 return 0;
663}
664
665static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
f8862395 666 const struct in_addr *router;
3c9b8860
TG
667 sd_dhcp_lease *lease;
668 struct in_addr address;
669 struct in_addr netmask;
3c9b8860
TG
670 unsigned prefixlen;
671 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
672 int r;
673
674 assert(client);
675 assert(link);
676
a20c909c
YW
677 link->dhcp4_configured = false;
678
3c9b8860 679 r = sd_dhcp_client_get_lease(client, &lease);
f2341e0a 680 if (r < 0)
f6b8196f 681 return log_link_error_errno(link, r, "DHCP error: No lease: %m");
3c9b8860
TG
682
683 r = sd_dhcp_lease_get_address(lease, &address);
f2341e0a 684 if (r < 0)
f6b8196f 685 return log_link_error_errno(link, r, "DHCP error: No address: %m");
3c9b8860
TG
686
687 r = sd_dhcp_lease_get_netmask(lease, &netmask);
f2341e0a 688 if (r < 0)
f6b8196f 689 return log_link_error_errno(link, r, "DHCP error: No netmask: %m");
3c9b8860 690
5a941f5f 691 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
3c9b8860 692
448aaf9f
YW
693 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
694 r = sd_dhcp_lease_get_lifetime(lease, &lifetime);
695 if (r < 0)
696 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
697 }
698
f8862395 699 r = sd_dhcp_lease_get_router(lease, &router);
397d15fd 700 if (r < 0 && r != -ENODATA)
f6b8196f 701 return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
3c9b8860 702
f8862395 703 if (r > 0 && !in4_addr_is_null(&router[0]))
f2341e0a
LP
704 log_struct(LOG_INFO,
705 LOG_LINK_INTERFACE(link),
706 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
707 ADDRESS_FMT_VAL(address),
708 prefixlen,
f8862395 709 ADDRESS_FMT_VAL(router[0])),
f2341e0a
LP
710 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
711 "PREFIXLEN=%u", prefixlen,
f8862395 712 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router[0]));
3c9b8860 713 else
f2341e0a
LP
714 log_struct(LOG_INFO,
715 LOG_LINK_INTERFACE(link),
716 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u",
717 ADDRESS_FMT_VAL(address),
718 prefixlen),
719 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
a1230ff9 720 "PREFIXLEN=%u", prefixlen);
3c9b8860 721
e6b18ffa 722 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 723 link_dirty(link);
3c9b8860 724
27cb34f5 725 if (link->network->dhcp_use_mtu) {
3c9b8860
TG
726 uint16_t mtu;
727
728 r = sd_dhcp_lease_get_mtu(lease, &mtu);
729 if (r >= 0) {
933c70a0 730 r = link_set_mtu(link, mtu);
3c9b8860 731 if (r < 0)
f2341e0a 732 log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu);
3c9b8860
TG
733 }
734 }
735
27cb34f5 736 if (link->network->dhcp_use_hostname) {
2de2abad
LB
737 const char *dhcpname = NULL;
738 _cleanup_free_ char *hostname = NULL;
3c9b8860 739
27cb34f5 740 if (link->network->dhcp_hostname)
2de2abad 741 dhcpname = link->network->dhcp_hostname;
dce391e7 742 else
2de2abad
LB
743 (void) sd_dhcp_lease_get_hostname(lease, &dhcpname);
744
745 if (dhcpname) {
746 r = shorten_overlong(dhcpname, &hostname);
747 if (r < 0)
748 log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
749 if (r == 1)
5238e957 750 log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
2de2abad 751 }
a7d0ef44 752
dce391e7 753 if (hostname) {
59eb33e0 754 r = manager_set_hostname(link->manager, hostname);
3c9b8860 755 if (r < 0)
f2341e0a 756 log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
3c9b8860
TG
757 }
758 }
759
27cb34f5 760 if (link->network->dhcp_use_timezone) {
21b80ad1
LP
761 const char *tz = NULL;
762
763 (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
764
765 if (tz) {
59eb33e0 766 r = manager_set_timezone(link->manager, tz);
21b80ad1
LP
767 if (r < 0)
768 log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz);
769 }
770 }
771
3c9b8860 772 r = dhcp4_update_address(link, &address, &netmask, lifetime);
a2f68490
YW
773 if (r < 0)
774 return log_link_warning_errno(link, r, "Could not update IP address: %m");
3c9b8860
TG
775
776 return 0;
777}
8bc17bb3 778
d03073dd
YW
779static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) {
780 int r;
781
782 link->dhcp_lease_old = TAKE_PTR(link->dhcp_lease);
783
784 /* On ip address change, to keep the connectability, we would like to assign new address and
785 * routes, and then release old lease. There are two possible success paths:
786 *
787 * 1. new address and routes are configured.
788 * -> handled by dhcp_release_old_lease() in dhcp4_route_handler().
789 * 2. new address is configured and no route is requested.
790 * -> handled by dhcp_release_old_lease() in dhcp4_address_handler().
791 *
792 * On error in assigning new address and routes, then the link always enters to the failed
793 * state. And link_enter_failed() leads to the DHCP client to be stopped. So,
794 * dhcp_release_old_lease() will be also called by link_stop_clients().
795 */
796
797 r = dhcp_lease_acquired(client, link);
798 if (r < 0) {
799 /* If it fails, then the new address is not configured yet.
800 * So, let's simply drop the old lease. */
801 sd_dhcp_lease_unref(link->dhcp_lease);
802 link->dhcp_lease = TAKE_PTR(link->dhcp_lease_old);
803 (void) dhcp_lease_lost(link);
804 return r;
805 }
806
807 return 0;
808}
809
727b5734
SS
810static int dhcp_server_is_black_listed(Link *link, sd_dhcp_client *client) {
811 sd_dhcp_lease *lease;
812 struct in_addr addr;
813 int r;
814
815 assert(link);
816 assert(link->network);
817 assert(client);
818
819 r = sd_dhcp_client_get_lease(client, &lease);
820 if (r < 0)
821 return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
822
823 r = sd_dhcp_lease_get_server_identifier(lease, &addr);
824 if (r < 0)
825 return log_link_debug_errno(link, r, "Failed to get DHCP server ip address: %m");
826
827 if (set_contains(link->network->dhcp_black_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
828 log_struct(LOG_DEBUG,
829 LOG_LINK_INTERFACE(link),
830 LOG_LINK_MESSAGE(link, "DHCPv4 ip '%u.%u.%u.%u' found in black listed ip addresses, ignoring offer",
831 ADDRESS_FMT_VAL(addr)));
832 return true;
833 }
834
835 return false;
836}
837
838static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
3c9b8860 839 Link *link = userdata;
86e2be7b 840 int r;
3c9b8860
TG
841
842 assert(link);
843 assert(link->network);
844 assert(link->manager);
845
846 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
727b5734 847 return 0;
3c9b8860
TG
848
849 switch (event) {
03748142 850 case SD_DHCP_CLIENT_EVENT_STOP:
8bc17bb3 851
910feb78 852 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4)) {
8bc17bb3
SS
853 assert(link->ipv4ll);
854
855 log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address");
856
857 r = sd_ipv4ll_start(link->ipv4ll);
727b5734
SS
858 if (r < 0)
859 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
8bc17bb3
SS
860 }
861
7da377ef 862 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
efdb62df
YW
863 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
864 return 0;
865 }
866
1501b429
SS
867 if (link->network->dhcp_send_release)
868 (void) sd_dhcp_client_send_release(client);
869
efdb62df
YW
870 if (link->dhcp_lease) {
871 r = dhcp_lease_lost(link);
872 if (r < 0) {
873 link_enter_failed(link);
874 return r;
875 }
876 }
877
878 break;
8bc17bb3 879 case SD_DHCP_CLIENT_EVENT_EXPIRED:
7da377ef 880 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
efdb62df 881 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
727b5734 882 return 0;
3c9b8860
TG
883 }
884
885 if (link->dhcp_lease) {
886 r = dhcp_lease_lost(link);
887 if (r < 0) {
888 link_enter_failed(link);
727b5734 889 return r;
3c9b8860
TG
890 }
891 }
892
d03073dd
YW
893 break;
894 case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
895 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
896 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
897 return 0;
898 }
899
900 r = dhcp_lease_ip_change(client, link);
901 if (r < 0) {
902 link_enter_failed(link);
903 return r;
3c9b8860
TG
904 }
905
906 break;
03748142 907 case SD_DHCP_CLIENT_EVENT_RENEW:
3c9b8860
TG
908 r = dhcp_lease_renew(client, link);
909 if (r < 0) {
910 link_enter_failed(link);
727b5734 911 return r;
3c9b8860
TG
912 }
913 break;
03748142 914 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
3c9b8860
TG
915 r = dhcp_lease_acquired(client, link);
916 if (r < 0) {
917 link_enter_failed(link);
727b5734 918 return r;
3c9b8860
TG
919 }
920 break;
727b5734
SS
921 case SD_DHCP_CLIENT_EVENT_SELECTING:
922 r = dhcp_server_is_black_listed(link, client);
923 if (r < 0)
924 return r;
925 if (r != 0)
926 return -ENOMSG;
927 break;
3c9b8860
TG
928 default:
929 if (event < 0)
f6b8196f 930 log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
3c9b8860 931 else
f6b8196f 932 log_link_warning(link, "DHCP unknown event: %i", event);
3c9b8860
TG
933 break;
934 }
935
727b5734 936 return 0;
3c9b8860
TG
937}
938
7192bb81
LP
939static int dhcp4_set_hostname(Link *link) {
940 _cleanup_free_ char *hostname = NULL;
941 const char *hn;
942 int r;
943
944 assert(link);
945
946 if (!link->network->dhcp_send_hostname)
947 hn = NULL;
948 else if (link->network->dhcp_hostname)
949 hn = link->network->dhcp_hostname;
950 else {
951 r = gethostname_strict(&hostname);
952 if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
953 return r;
954
955 hn = hostname;
956 }
957
a8494759
YW
958 r = sd_dhcp_client_set_hostname(link->dhcp_client, hn);
959 if (r == -EINVAL && hostname)
960 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
961 log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
962 else if (r < 0)
963 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m");
964
965 return 0;
7192bb81
LP
966}
967
64b21ece 968static bool promote_secondaries_enabled(const char *ifname) {
3f550c31
HV
969 _cleanup_free_ char *promote_secondaries_sysctl = NULL;
970 char *promote_secondaries_path;
64b21ece
MV
971 int r;
972
973 promote_secondaries_path = strjoina("net/ipv4/conf/", ifname, "/promote_secondaries");
974 r = sysctl_read(promote_secondaries_path, &promote_secondaries_sysctl);
975 if (r < 0) {
976 log_debug_errno(r, "Cannot read sysctl %s", promote_secondaries_path);
977 return false;
978 }
979
980 truncate_nl(promote_secondaries_sysctl);
981 r = parse_boolean(promote_secondaries_sysctl);
982 if (r < 0)
983 log_warning_errno(r, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path, promote_secondaries_sysctl);
984 return r > 0;
985}
986
987/* dhcp4_set_promote_secondaries will ensure this interface has
988 * the "promote_secondaries" option in the kernel set. If this sysctl
989 * is not set DHCP will work only as long as the IP address does not
990 * changes between leases. The kernel will remove all secondary IP
991 * addresses of an interface otherwise. The way systemd-network works
992 * is that the new IP of a lease is added as a secondary IP and when
993 * the primary one expires it relies on the kernel to promote the
994 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
995 */
996int dhcp4_set_promote_secondaries(Link *link) {
997 int r;
998
999 assert(link);
1000 assert(link->network);
1001 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
1002
1003 /* check if the kernel has promote_secondaries enabled for our
1004 * interface. If it is not globally enabled or enabled for the
1005 * specific interface we must either enable it.
1006 */
8e1a7253 1007 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link->ifname))) {
64b21ece
MV
1008 char *promote_secondaries_path = NULL;
1009
1010 log_link_debug(link, "promote_secondaries is unset, setting it");
1011 promote_secondaries_path = strjoina("net/ipv4/conf/", link->ifname, "/promote_secondaries");
1012 r = sysctl_write(promote_secondaries_path, "1");
1013 if (r < 0)
1014 log_link_warning_errno(link, r, "cannot set sysctl %s to 1", promote_secondaries_path);
1015 return r > 0;
1016 }
1017
1018 return 0;
1019}
1020
fff1f40c
YW
1021int dhcp4_set_client_identifier(Link *link) {
1022 int r;
1023
1024 assert(link);
1025 assert(link->network);
1026 assert(link->dhcp_client);
1027
1028 switch (link->network->dhcp_client_identifier) {
1029 case DHCP_CLIENT_ID_DUID: {
0cf7c3fd 1030 /* If configured, apply user specified DUID and IAID */
fff1f40c
YW
1031 const DUID *duid = link_get_duid(link);
1032
0cf7c3fd
YW
1033 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1034 r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
8217ed5e 1035 link->network->iaid_set,
0cf7c3fd
YW
1036 link->network->iaid,
1037 duid->llt_time);
1038 else
1039 r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
8217ed5e 1040 link->network->iaid_set,
0cf7c3fd
YW
1041 link->network->iaid,
1042 duid->type,
1043 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1044 duid->raw_data_len);
fff1f40c 1045 if (r < 0)
0cf7c3fd 1046 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
fff1f40c
YW
1047 break;
1048 }
1049 case DHCP_CLIENT_ID_DUID_ONLY: {
1050 /* If configured, apply user specified DUID */
1051 const DUID *duid = link_get_duid(link);
1052
0cf7c3fd
YW
1053 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1054 r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
89b3fa66 1055 duid->llt_time);
0cf7c3fd
YW
1056 else
1057 r = sd_dhcp_client_set_duid(link->dhcp_client,
1058 duid->type,
1059 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1060 duid->raw_data_len);
fff1f40c
YW
1061 if (r < 0)
1062 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
1063 break;
1064 }
1065 case DHCP_CLIENT_ID_MAC:
1066 r = sd_dhcp_client_set_client_id(link->dhcp_client,
1067 ARPHRD_ETHER,
1068 (const uint8_t *) &link->mac,
1069 sizeof(link->mac));
1070 if (r < 0)
1071 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m");
1072 break;
1073 default:
1074 assert_not_reached("Unknown client identifier type.");
1075 }
1076
1077 return 0;
1078}
1079
3c9b8860 1080int dhcp4_configure(Link *link) {
5bc945be
SS
1081 void *request_options;
1082 Iterator i;
3c9b8860
TG
1083 int r;
1084
1085 assert(link);
1086 assert(link->network);
e0ee46f2 1087 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
3c9b8860 1088
0bc70f1d 1089 if (!link->dhcp_client) {
db3d2358 1090 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
1f6860d9
YW
1091 if (r == -ENOMEM)
1092 return log_oom();
0bc70f1d 1093 if (r < 0)
1f6860d9 1094 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
0bc70f1d 1095 }
3c9b8860
TG
1096
1097 r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
1098 if (r < 0)
1f6860d9 1099 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to attach event: %m");
3c9b8860 1100
76253e73
DW
1101 r = sd_dhcp_client_set_mac(link->dhcp_client,
1102 (const uint8_t *) &link->mac,
1103 sizeof (link->mac), ARPHRD_ETHER);
3c9b8860 1104 if (r < 0)
1f6860d9 1105 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m");
3c9b8860 1106
2f8e7633 1107 r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
3c9b8860 1108 if (r < 0)
1f6860d9 1109 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m");
3c9b8860
TG
1110
1111 r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
1112 if (r < 0)
1f6860d9 1113 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m");
3c9b8860
TG
1114
1115 r = sd_dhcp_client_set_request_broadcast(link->dhcp_client,
1116 link->network->dhcp_broadcast);
1117 if (r < 0)
1f6860d9 1118 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
3c9b8860
TG
1119
1120 if (link->mtu) {
1121 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
1122 if (r < 0)
1f6860d9 1123 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m");
3c9b8860
TG
1124 }
1125
27cb34f5 1126 if (link->network->dhcp_use_mtu) {
39745a5a 1127 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1128 SD_DHCP_OPTION_INTERFACE_MTU);
39745a5a 1129 if (r < 0)
1f6860d9 1130 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
3c9b8860
TG
1131 }
1132
5e77a146 1133 /* NOTE: even if this variable is called "use", it also "sends" PRL
1134 * options, maybe there should be a different configuration variable
1135 * to send or not route options?. */
28522b0d 1136 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1137 * by default, so they don't need to be added here. */
5e77a146 1138 if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) {
3c9b8860 1139 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1140 SD_DHCP_OPTION_STATIC_ROUTE);
3c9b8860 1141 if (r < 0)
1f6860d9
YW
1142 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1143
3c9b8860 1144 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1145 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
7d6884b6 1146 if (r < 0)
1f6860d9 1147 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
3c9b8860
TG
1148 }
1149
150d3b8e
YW
1150 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO && !link->network->dhcp_anonymize) {
1151 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST);
1152 if (r < 0)
1153 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1154 }
1155
ead36ce6 1156 if (link->network->dhcp_use_ntp) {
1157 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
1158 if (r < 0)
1f6860d9 1159 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
ead36ce6 1160 }
4b7b5abb 1161
89573b37 1162 if (link->network->dhcp_use_timezone) {
1163 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
1164 if (r < 0)
1f6860d9 1165 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
89573b37 1166 }
8eb9058d 1167
5bc945be
SS
1168 SET_FOREACH(request_options, link->network->dhcp_request_options, i) {
1169 uint32_t option = PTR_TO_UINT32(request_options);
1170
1171 r = sd_dhcp_client_set_request_option(link->dhcp_client, option);
1172 if (r == -EEXIST) {
1173 log_link_debug(link, "DHCP4 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option);
1174 continue;
1175 }
1176
1177 if (r < 0)
1178 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option);
1179 }
1180
7192bb81
LP
1181 r = dhcp4_set_hostname(link);
1182 if (r < 0)
a8494759 1183 return r;
3c9b8860
TG
1184
1185 if (link->network->dhcp_vendor_class_identifier) {
1186 r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
1187 link->network->dhcp_vendor_class_identifier);
1188 if (r < 0)
1f6860d9 1189 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
3c9b8860
TG
1190 }
1191
af1c0de0
SS
1192 if (link->network->dhcp_user_class) {
1193 r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class);
1194 if (r < 0)
1f6860d9 1195 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m");
af1c0de0
SS
1196 }
1197
9faed222
SS
1198 if (link->network->dhcp_client_port) {
1199 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
1200 if (r < 0)
1f6860d9 1201 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
9faed222
SS
1202 }
1203
715cedfb
SS
1204 if (link->network->dhcp_max_attempts > 0) {
1205 r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts);
1206 if (r < 0)
1207 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m");
1208 }
1209
fff1f40c 1210 return dhcp4_set_client_identifier(link);
3c9b8860 1211}
ca5ad760
YW
1212
1213int config_parse_dhcp_max_attempts(
1214 const char *unit,
1215 const char *filename,
1216 unsigned line,
1217 const char *section,
1218 unsigned section_line,
1219 const char *lvalue,
1220 int ltype,
1221 const char *rvalue,
1222 void *data,
1223 void *userdata) {
1224
1225 Network *network = data;
1226 uint64_t a;
1227 int r;
1228
1229 assert(network);
1230 assert(lvalue);
1231 assert(rvalue);
1232
1233 if (isempty(rvalue)) {
1234 network->dhcp_max_attempts = 0;
1235 return 0;
1236 }
1237
1238 if (streq(rvalue, "infinity")) {
1239 network->dhcp_max_attempts = (uint64_t) -1;
1240 return 0;
1241 }
1242
1243 r = safe_atou64(rvalue, &a);
1244 if (r < 0) {
1245 log_syntax(unit, LOG_ERR, filename, line, r,
1246 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue);
1247 return 0;
1248 }
1249
1250 if (a == 0) {
1251 log_syntax(unit, LOG_ERR, filename, line, 0,
1252 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue);
1253 return 0;
1254 }
1255
1256 network->dhcp_max_attempts = a;
1257
1258 return 0;
1259}
1260
1261int config_parse_dhcp_black_listed_ip_address(
1262 const char *unit,
1263 const char *filename,
1264 unsigned line,
1265 const char *section,
1266 unsigned section_line,
1267 const char *lvalue,
1268 int ltype,
1269 const char *rvalue,
1270 void *data,
1271 void *userdata) {
1272
1273 Network *network = data;
1274 const char *p;
1275 int r;
1276
1277 assert(filename);
1278 assert(lvalue);
1279 assert(rvalue);
1280 assert(data);
1281
1282 if (isempty(rvalue)) {
1283 network->dhcp_black_listed_ip = set_free(network->dhcp_black_listed_ip);
1284 return 0;
1285 }
1286
1287 for (p = rvalue;;) {
1288 _cleanup_free_ char *n = NULL;
1289 union in_addr_union ip;
1290
1291 r = extract_first_word(&p, &n, NULL, 0);
1292 if (r < 0) {
1293 log_syntax(unit, LOG_ERR, filename, line, r,
1294 "Failed to parse DHCP black listed ip address, ignoring assignment: %s",
1295 rvalue);
1296 return 0;
1297 }
1298 if (r == 0)
1299 return 0;
1300
1301 r = in_addr_from_string(AF_INET, n, &ip);
1302 if (r < 0) {
1303 log_syntax(unit, LOG_ERR, filename, line, r,
1304 "DHCP black listed ip address is invalid, ignoring assignment: %s", n);
1305 continue;
1306 }
1307
1308 r = set_ensure_allocated(&network->dhcp_black_listed_ip, NULL);
1309 if (r < 0)
1310 return log_oom();
1311
1312 r = set_put(network->dhcp_black_listed_ip, UINT32_TO_PTR(ip.in.s_addr));
1313 if (r < 0)
1314 log_syntax(unit, LOG_ERR, filename, line, r,
1315 "Failed to store DHCP black listed ip address '%s', ignoring assignment: %m", n);
1316 }
1317
1318 return 0;
1319}
1320
1321int config_parse_dhcp_user_class(
1322 const char *unit,
1323 const char *filename,
1324 unsigned line,
1325 const char *section,
1326 unsigned section_line,
1327 const char *lvalue,
1328 int ltype,
1329 const char *rvalue,
1330 void *data,
1331 void *userdata) {
1332
1333 char ***l = data;
1334 int r;
1335
1336 assert(l);
1337 assert(lvalue);
1338 assert(rvalue);
1339
1340 if (isempty(rvalue)) {
1341 *l = strv_free(*l);
1342 return 0;
1343 }
1344
1345 for (;;) {
1346 _cleanup_free_ char *w = NULL;
1347
1348 r = extract_first_word(&rvalue, &w, NULL, 0);
1349 if (r == -ENOMEM)
1350 return log_oom();
1351 if (r < 0) {
1352 log_syntax(unit, LOG_ERR, filename, line, r,
1353 "Failed to split user classes option, ignoring: %s", rvalue);
1354 break;
1355 }
1356 if (r == 0)
1357 break;
1358
1359 if (strlen(w) > 255) {
1360 log_syntax(unit, LOG_ERR, filename, line, 0,
1361 "%s length is not in the range 1-255, ignoring.", w);
1362 continue;
1363 }
1364
1365 r = strv_push(l, w);
1366 if (r < 0)
1367 return log_oom();
1368
1369 w = NULL;
1370 }
1371
1372 return 0;
1373}
1374
5bc945be
SS
1375int config_parse_dhcp_request_options(
1376 const char *unit,
1377 const char *filename,
1378 unsigned line,
1379 const char *section,
1380 unsigned section_line,
1381 const char *lvalue,
1382 int ltype,
1383 const char *rvalue,
1384 void *data,
1385 void *userdata) {
1386
1387 Network *network = data;
1388 const char *p;
1389 int r;
1390
1391 assert(filename);
1392 assert(lvalue);
1393 assert(rvalue);
1394 assert(data);
1395
1396 if (isempty(rvalue)) {
1397 network->dhcp_request_options = set_free(network->dhcp_request_options);
1398 return 0;
1399 }
1400
1401 for (p = rvalue;;) {
1402 _cleanup_free_ char *n = NULL;
1403 uint32_t i;
1404
1405 r = extract_first_word(&p, &n, NULL, 0);
1406 if (r < 0) {
1407 log_syntax(unit, LOG_ERR, filename, line, r,
1408 "Failed to parse DHCP request option, ignoring assignment: %s",
1409 rvalue);
1410 return 0;
1411 }
1412 if (r == 0)
1413 return 0;
1414
1415 r = safe_atou32(n, &i);
1416 if (r < 0) {
1417 log_syntax(unit, LOG_ERR, filename, line, r,
1418 "DHCP request option is invalid, ignoring assignment: %s", n);
1419 continue;
1420 }
1421
1422 if (i < 1 || i >= 255) {
1423 log_syntax(unit, LOG_ERR, filename, line, r,
1424 "DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n);
1425 continue;
1426 }
1427
1428 r = set_ensure_allocated(&network->dhcp_request_options, NULL);
1429 if (r < 0)
1430 return log_oom();
1431
1432 r = set_put(network->dhcp_request_options, UINT32_TO_PTR(i));
1433 if (r < 0)
1434 log_syntax(unit, LOG_ERR, filename, line, r,
1435 "Failed to store DHCP request option '%s', ignoring assignment: %m", n);
1436 }
1437
1438 return 0;
1439}
1440
ca5ad760
YW
1441static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = {
1442 [DHCP_CLIENT_ID_MAC] = "mac",
1443 [DHCP_CLIENT_ID_DUID] = "duid",
1444 [DHCP_CLIENT_ID_DUID_ONLY] = "duid-only",
1445};
1446
1447DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier);
1448DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier,
1449 "Failed to parse client identifier type");