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