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