]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-dhcp4.c
network: dhcp4: also release old lease in dhcp_lease_lost()
[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 29
75be72d1 30static void 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
75be72d1
YW
796 /* dhcp_lease_lost() may be called during renewing IP address. */
797 dhcp4_release_old_lease(link);
798
6906794d
YW
799 r = dhcp4_remove_all(link);
800 if (r < 0)
801 return r;
802
803 r = dhcp_reset_mtu(link);
804 if (r < 0)
805 return r;
806
807 r = dhcp_reset_hostname(link);
808 if (r < 0)
809 return r;
7fa472f9 810
3c9b8860 811 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
6a3e5f6a 812 link_dirty(link);
3c9b8860
TG
813
814 return 0;
815}
816
0f3ff4ea
SS
817static void dhcp_address_on_acd(sd_ipv4acd *acd, int event, void *userdata) {
818 _cleanup_free_ char *pretty = NULL;
819 union in_addr_union address = {};
820 Link *link;
821 int r;
822
823 assert(acd);
824 assert(userdata);
825
826 link = userdata;
827
828 switch (event) {
829 case SD_IPV4ACD_EVENT_STOP:
830 log_link_debug(link, "Stopping ACD client for DHCP4...");
831 return;
832
833 case SD_IPV4ACD_EVENT_BIND:
834 if (DEBUG_LOGGING) {
835 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address.in);
836 (void) in_addr_to_string(AF_INET, &address, &pretty);
837 log_link_debug(link, "Successfully claimed DHCP4 address %s", strna(pretty));
838 }
c1d3fa29 839 dhcp4_check_ready(link);
0f3ff4ea
SS
840 break;
841
842 case SD_IPV4ACD_EVENT_CONFLICT:
843 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address.in);
844 (void) in_addr_to_string(AF_INET, &address, &pretty);
845 log_link_warning(link, "DAD conflict. Dropping DHCP4 address %s", strna(pretty));
846
847 (void) sd_dhcp_client_send_decline(link->dhcp_client);
848
849 if (link->dhcp_lease) {
850 r = dhcp_lease_lost(link);
851 if (r < 0)
852 link_enter_failed(link);
853 }
854 break;
855
856 default:
857 assert_not_reached("Invalid IPv4ACD event.");
858 }
859
860 sd_ipv4acd_stop(acd);
861
862 return;
863}
864
865static int configure_dhcpv4_duplicate_address_detection(Link *link) {
866 int r;
867
868 assert(link);
869
870 r = sd_ipv4acd_new(&link->network->dhcp_acd);
871 if (r < 0)
872 return r;
873
874 r = sd_ipv4acd_attach_event(link->network->dhcp_acd, NULL, 0);
875 if (r < 0)
876 return r;
877
878 r = sd_ipv4acd_set_ifindex(link->network->dhcp_acd, link->ifindex);
879 if (r < 0)
880 return r;
881
882 r = sd_ipv4acd_set_mac(link->network->dhcp_acd, &link->mac);
883 if (r < 0)
884 return r;
885
886 return 0;
887}
888
302a796f 889static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3c9b8860
TG
890 int r;
891
892 assert(link);
893
3ab7ed3f
YW
894 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
895 return 1;
896
1c4baffc 897 r = sd_netlink_message_get_errno(m);
3c9b8860 898 if (r < 0 && r != -EEXIST) {
5ecb131d 899 log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 address");
3c9b8860 900 link_enter_failed(link);
3ab7ed3f 901 return 1;
4ff296b0
YW
902 } else if (r >= 0)
903 (void) manager_rtnl_process_address(rtnl, m, link->manager);
3c9b8860 904
1590dfa4
YW
905 r = link_set_dhcp_routes(link);
906 if (r < 0) {
907 link_enter_failed(link);
908 return 1;
909 }
3c9b8860 910
b5799eeb 911 /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
4ff296b0
YW
912 r = link_request_set_routes(link);
913 if (r < 0) {
914 link_enter_failed(link);
915 return 1;
916 }
b5799eeb 917
0f3ff4ea
SS
918 if (link->network->dhcp_send_decline) {
919 union in_addr_union addr;
920
921 (void) sd_dhcp_lease_get_address(link->dhcp_lease, &addr.in);
922
923 r = sd_ipv4acd_set_address(link->network->dhcp_acd, &addr.in);
924 if (r < 0)
925 return r;
926
927 r = sd_ipv4acd_set_callback(link->network->dhcp_acd, dhcp_address_on_acd, link);
928 if (r < 0)
929 return r;
930
931 if (DEBUG_LOGGING) {
932 _cleanup_free_ char *pretty = NULL;
933
934 (void) in_addr_to_string(AF_INET, &addr, &pretty);
98b02994 935 log_link_debug(link, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty));
0f3ff4ea
SS
936 }
937
938 r = sd_ipv4acd_start(link->network->dhcp_acd, true);
939 if (r < 0)
940 log_link_warning_errno(link, r, "Failed to start IPv4ACD client, ignoring: %m");
c1d3fa29
YW
941 } else
942 dhcp4_check_ready(link);
0f3ff4ea 943
3c9b8860
TG
944 return 1;
945}
946
6906794d 947static int dhcp4_update_address(Link *link, bool announce) {
8e766630 948 _cleanup_(address_freep) Address *addr = NULL;
6906794d
YW
949 uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
950 struct in_addr address, netmask;
3c9b8860
TG
951 unsigned prefixlen;
952 int r;
953
6906794d
YW
954 assert(link);
955 assert(link->network);
956
957 if (!link->dhcp_lease)
958 return 0;
959
960 link_set_state(link, LINK_STATE_CONFIGURING);
961 link->dhcp4_configured = false;
3c9b8860 962
0c816fcc
YW
963 /* address_handler calls link_request_set_routes() and link_request_set_nexthop(). Before they
964 * are called, the related flags must be cleared. Otherwise, the link becomes configured state
965 * before routes are configured. */
966 link->static_routes_configured = false;
967 link->static_nexthops_configured = false;
968
6906794d
YW
969 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
970 if (r < 0)
971 return log_link_warning_errno(link, r, "DHCP error: no address: %m");
972
973 r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
974 if (r < 0)
975 return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
976
977 if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
978 r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
979 if (r < 0)
980 return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
981 }
982
983 prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
984
985 if (announce) {
986 const struct in_addr *router;
987
988 r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
989 if (r < 0 && r != -ENODATA)
990 return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
991
992 if (r > 0 && !in4_addr_is_null(&router[0]))
993 log_struct(LOG_INFO,
994 LOG_LINK_INTERFACE(link),
995 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
996 ADDRESS_FMT_VAL(address),
997 prefixlen,
998 ADDRESS_FMT_VAL(router[0])),
999 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
1000 "PREFIXLEN=%u", prefixlen,
1001 "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router[0]));
1002 else
1003 log_struct(LOG_INFO,
1004 LOG_LINK_INTERFACE(link),
1005 LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u",
1006 ADDRESS_FMT_VAL(address),
1007 prefixlen),
1008 "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
1009 "PREFIXLEN=%u", prefixlen);
1010 }
3c9b8860 1011
f0213e37 1012 r = address_new(&addr);
3c9b8860 1013 if (r < 0)
6906794d 1014 return log_oom();
3c9b8860
TG
1015
1016 addr->family = AF_INET;
6906794d 1017 addr->in_addr.in.s_addr = address.s_addr;
3c9b8860
TG
1018 addr->cinfo.ifa_prefered = lifetime;
1019 addr->cinfo.ifa_valid = lifetime;
1020 addr->prefixlen = prefixlen;
6906794d 1021 addr->broadcast.s_addr = address.s_addr | ~netmask.s_addr;
de697db0 1022 addr->prefix_route = link_prefixroute(link);
3c9b8860 1023
66669078
TG
1024 /* allow reusing an existing address and simply update its lifetime
1025 * in case it already exists */
483d099e 1026 r = address_configure(addr, link, dhcp4_address_handler, true);
3c9b8860
TG
1027 if (r < 0)
1028 return r;
1029
1030 return 0;
1031}
1032
1033static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
1034 sd_dhcp_lease *lease;
3c9b8860
TG
1035 int r;
1036
1037 assert(link);
1038 assert(client);
3c9b8860
TG
1039
1040 r = sd_dhcp_client_get_lease(client, &lease);
f6b8196f
LP
1041 if (r < 0)
1042 return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
3c9b8860
TG
1043
1044 sd_dhcp_lease_unref(link->dhcp_lease);
e6b18ffa 1045 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 1046 link_dirty(link);
3c9b8860 1047
6906794d 1048 return dhcp4_update_address(link, false);
3c9b8860
TG
1049}
1050
1051static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
1052 sd_dhcp_lease *lease;
3c9b8860
TG
1053 int r;
1054
1055 assert(client);
1056 assert(link);
1057
1058 r = sd_dhcp_client_get_lease(client, &lease);
f2341e0a 1059 if (r < 0)
f6b8196f 1060 return log_link_error_errno(link, r, "DHCP error: No lease: %m");
3c9b8860 1061
6906794d 1062 sd_dhcp_lease_unref(link->dhcp_lease);
e6b18ffa 1063 link->dhcp_lease = sd_dhcp_lease_ref(lease);
6a3e5f6a 1064 link_dirty(link);
3c9b8860 1065
27cb34f5 1066 if (link->network->dhcp_use_mtu) {
3c9b8860
TG
1067 uint16_t mtu;
1068
1069 r = sd_dhcp_lease_get_mtu(lease, &mtu);
1070 if (r >= 0) {
933c70a0 1071 r = link_set_mtu(link, mtu);
3c9b8860 1072 if (r < 0)
f2341e0a 1073 log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu);
3c9b8860
TG
1074 }
1075 }
1076
27cb34f5 1077 if (link->network->dhcp_use_hostname) {
2de2abad
LB
1078 const char *dhcpname = NULL;
1079 _cleanup_free_ char *hostname = NULL;
3c9b8860 1080
27cb34f5 1081 if (link->network->dhcp_hostname)
2de2abad 1082 dhcpname = link->network->dhcp_hostname;
dce391e7 1083 else
2de2abad
LB
1084 (void) sd_dhcp_lease_get_hostname(lease, &dhcpname);
1085
1086 if (dhcpname) {
1087 r = shorten_overlong(dhcpname, &hostname);
1088 if (r < 0)
1089 log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
1090 if (r == 1)
5238e957 1091 log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
2de2abad 1092 }
a7d0ef44 1093
dce391e7 1094 if (hostname) {
59eb33e0 1095 r = manager_set_hostname(link->manager, hostname);
3c9b8860 1096 if (r < 0)
f2341e0a 1097 log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
3c9b8860
TG
1098 }
1099 }
1100
27cb34f5 1101 if (link->network->dhcp_use_timezone) {
21b80ad1
LP
1102 const char *tz = NULL;
1103
1104 (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
1105
1106 if (tz) {
59eb33e0 1107 r = manager_set_timezone(link->manager, tz);
21b80ad1
LP
1108 if (r < 0)
1109 log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz);
1110 }
1111 }
1112
6906794d
YW
1113 if (link->dhcp4_remove_messages == 0) {
1114 r = dhcp4_update_address(link, true);
1115 if (r < 0)
1116 return r;
1117 } else
1118 log_link_debug(link,
1119 "The link has previously assigned DHCPv4 address or routes. "
1120 "The newly assigned address and routes will set up after old ones are removed.");
3c9b8860
TG
1121
1122 return 0;
1123}
8bc17bb3 1124
d03073dd
YW
1125static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) {
1126 int r;
1127
1128 link->dhcp_lease_old = TAKE_PTR(link->dhcp_lease);
1129
0da425df 1130 /* On IP address change, to keep the connectability, we would like to assign new address and
d03073dd
YW
1131 * routes, and then release old lease. There are two possible success paths:
1132 *
1133 * 1. new address and routes are configured.
1134 * -> handled by dhcp_release_old_lease() in dhcp4_route_handler().
1135 * 2. new address is configured and no route is requested.
1136 * -> handled by dhcp_release_old_lease() in dhcp4_address_handler().
1137 *
1138 * On error in assigning new address and routes, then the link always enters to the failed
1139 * state. And link_enter_failed() leads to the DHCP client to be stopped. So,
1140 * dhcp_release_old_lease() will be also called by link_stop_clients().
1141 */
1142
1143 r = dhcp_lease_acquired(client, link);
1144 if (r < 0) {
1145 /* If it fails, then the new address is not configured yet.
1146 * So, let's simply drop the old lease. */
1147 sd_dhcp_lease_unref(link->dhcp_lease);
1148 link->dhcp_lease = TAKE_PTR(link->dhcp_lease_old);
1149 (void) dhcp_lease_lost(link);
1150 return r;
1151 }
1152
1153 return 0;
1154}
1155
6b000af4 1156static int dhcp_server_is_deny_listed(Link *link, sd_dhcp_client *client) {
727b5734
SS
1157 sd_dhcp_lease *lease;
1158 struct in_addr addr;
1159 int r;
1160
1161 assert(link);
1162 assert(link->network);
1163 assert(client);
1164
1165 r = sd_dhcp_client_get_lease(client, &lease);
1166 if (r < 0)
1167 return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
1168
1169 r = sd_dhcp_lease_get_server_identifier(lease, &addr);
1170 if (r < 0)
0da425df 1171 return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
727b5734 1172
6b000af4 1173 if (set_contains(link->network->dhcp_deny_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
727b5734
SS
1174 log_struct(LOG_DEBUG,
1175 LOG_LINK_INTERFACE(link),
0da425df 1176 LOG_LINK_MESSAGE(link, "DHCPv4 IP '%u.%u.%u.%u' found in deny-listed IP addresses, ignoring offer",
727b5734
SS
1177 ADDRESS_FMT_VAL(addr)));
1178 return true;
1179 }
1180
1181 return false;
1182}
1183
98ebef62
SS
1184static int dhcp_server_is_allow_listed(Link *link, sd_dhcp_client *client) {
1185 sd_dhcp_lease *lease;
1186 struct in_addr addr;
1187 int r;
1188
1189 assert(link);
1190 assert(link->network);
1191 assert(client);
1192
1193 r = sd_dhcp_client_get_lease(client, &lease);
1194 if (r < 0)
1195 return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
1196
1197 r = sd_dhcp_lease_get_server_identifier(lease, &addr);
1198 if (r < 0)
0da425df 1199 return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
98ebef62
SS
1200
1201 if (set_contains(link->network->dhcp_allow_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
1202 log_struct(LOG_DEBUG,
1203 LOG_LINK_INTERFACE(link),
0da425df 1204 LOG_LINK_MESSAGE(link, "DHCPv4 IP '%u.%u.%u.%u' found in allow-listed IP addresses, accepting offer",
98ebef62
SS
1205 ADDRESS_FMT_VAL(addr)));
1206 return true;
1207 }
1208
1209 return false;
1210}
1211
727b5734 1212static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
3c9b8860 1213 Link *link = userdata;
86e2be7b 1214 int r;
3c9b8860
TG
1215
1216 assert(link);
1217 assert(link->network);
1218 assert(link->manager);
1219
1220 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
727b5734 1221 return 0;
3c9b8860
TG
1222
1223 switch (event) {
03748142 1224 case SD_DHCP_CLIENT_EVENT_STOP:
8bc17bb3 1225
910feb78 1226 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4)) {
8bc17bb3
SS
1227 assert(link->ipv4ll);
1228
1229 log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address");
1230
1231 r = sd_ipv4ll_start(link->ipv4ll);
727b5734
SS
1232 if (r < 0)
1233 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
8bc17bb3
SS
1234 }
1235
7da377ef 1236 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
efdb62df
YW
1237 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1238 return 0;
1239 }
1240
efdb62df 1241 if (link->dhcp_lease) {
8bea7e70
ZJS
1242 if (link->network->dhcp_send_release)
1243 (void) sd_dhcp_client_send_release(client);
1244
efdb62df
YW
1245 r = dhcp_lease_lost(link);
1246 if (r < 0) {
1247 link_enter_failed(link);
1248 return r;
1249 }
1250 }
1251
1252 break;
8bc17bb3 1253 case SD_DHCP_CLIENT_EVENT_EXPIRED:
7da377ef 1254 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
efdb62df 1255 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
727b5734 1256 return 0;
3c9b8860
TG
1257 }
1258
1259 if (link->dhcp_lease) {
1260 r = dhcp_lease_lost(link);
1261 if (r < 0) {
1262 link_enter_failed(link);
727b5734 1263 return r;
3c9b8860
TG
1264 }
1265 }
1266
d03073dd
YW
1267 break;
1268 case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
1269 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
1270 log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
1271 return 0;
1272 }
1273
1274 r = dhcp_lease_ip_change(client, link);
1275 if (r < 0) {
1276 link_enter_failed(link);
1277 return r;
3c9b8860
TG
1278 }
1279
1280 break;
03748142 1281 case SD_DHCP_CLIENT_EVENT_RENEW:
3c9b8860
TG
1282 r = dhcp_lease_renew(client, link);
1283 if (r < 0) {
1284 link_enter_failed(link);
727b5734 1285 return r;
3c9b8860
TG
1286 }
1287 break;
03748142 1288 case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
3c9b8860
TG
1289 r = dhcp_lease_acquired(client, link);
1290 if (r < 0) {
1291 link_enter_failed(link);
727b5734 1292 return r;
3c9b8860
TG
1293 }
1294 break;
727b5734 1295 case SD_DHCP_CLIENT_EVENT_SELECTING:
98ebef62
SS
1296 if (!set_isempty(link->network->dhcp_allow_listed_ip)) {
1297 r = dhcp_server_is_allow_listed(link, client);
1298 if (r < 0)
1299 return r;
1300 if (r == 0)
1301 return -ENOMSG;
1302 } else {
1303 r = dhcp_server_is_deny_listed(link, client);
1304 if (r < 0)
1305 return r;
1306 if (r != 0)
1307 return -ENOMSG;
1308 }
727b5734 1309 break;
3c9b8860
TG
1310 default:
1311 if (event < 0)
f6b8196f 1312 log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
3c9b8860 1313 else
f6b8196f 1314 log_link_warning(link, "DHCP unknown event: %i", event);
3c9b8860
TG
1315 break;
1316 }
1317
727b5734 1318 return 0;
3c9b8860
TG
1319}
1320
7192bb81
LP
1321static int dhcp4_set_hostname(Link *link) {
1322 _cleanup_free_ char *hostname = NULL;
1323 const char *hn;
1324 int r;
1325
1326 assert(link);
1327
1328 if (!link->network->dhcp_send_hostname)
1329 hn = NULL;
1330 else if (link->network->dhcp_hostname)
1331 hn = link->network->dhcp_hostname;
1332 else {
1333 r = gethostname_strict(&hostname);
1334 if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
1335 return r;
1336
1337 hn = hostname;
1338 }
1339
a8494759
YW
1340 r = sd_dhcp_client_set_hostname(link->dhcp_client, hn);
1341 if (r == -EINVAL && hostname)
1342 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
1343 log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
1344 else if (r < 0)
1345 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m");
1346
1347 return 0;
7192bb81
LP
1348}
1349
64b21ece 1350static bool promote_secondaries_enabled(const char *ifname) {
3f550c31
HV
1351 _cleanup_free_ char *promote_secondaries_sysctl = NULL;
1352 char *promote_secondaries_path;
64b21ece
MV
1353 int r;
1354
1355 promote_secondaries_path = strjoina("net/ipv4/conf/", ifname, "/promote_secondaries");
1356 r = sysctl_read(promote_secondaries_path, &promote_secondaries_sysctl);
1357 if (r < 0) {
1358 log_debug_errno(r, "Cannot read sysctl %s", promote_secondaries_path);
1359 return false;
1360 }
1361
1362 truncate_nl(promote_secondaries_sysctl);
1363 r = parse_boolean(promote_secondaries_sysctl);
1364 if (r < 0)
1365 log_warning_errno(r, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path, promote_secondaries_sysctl);
1366 return r > 0;
1367}
1368
1369/* dhcp4_set_promote_secondaries will ensure this interface has
1370 * the "promote_secondaries" option in the kernel set. If this sysctl
1371 * is not set DHCP will work only as long as the IP address does not
1372 * changes between leases. The kernel will remove all secondary IP
1373 * addresses of an interface otherwise. The way systemd-network works
1374 * is that the new IP of a lease is added as a secondary IP and when
1375 * the primary one expires it relies on the kernel to promote the
1376 * secondary IP. See also https://github.com/systemd/systemd/issues/7163
1377 */
1378int dhcp4_set_promote_secondaries(Link *link) {
1379 int r;
1380
1381 assert(link);
1382 assert(link->network);
1383 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
1384
1385 /* check if the kernel has promote_secondaries enabled for our
1386 * interface. If it is not globally enabled or enabled for the
1387 * specific interface we must either enable it.
1388 */
8e1a7253 1389 if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link->ifname))) {
64b21ece
MV
1390 char *promote_secondaries_path = NULL;
1391
1392 log_link_debug(link, "promote_secondaries is unset, setting it");
1393 promote_secondaries_path = strjoina("net/ipv4/conf/", link->ifname, "/promote_secondaries");
1394 r = sysctl_write(promote_secondaries_path, "1");
1395 if (r < 0)
1396 log_link_warning_errno(link, r, "cannot set sysctl %s to 1", promote_secondaries_path);
1397 return r > 0;
1398 }
1399
1400 return 0;
1401}
1402
fff1f40c
YW
1403int dhcp4_set_client_identifier(Link *link) {
1404 int r;
1405
1406 assert(link);
1407 assert(link->network);
1408 assert(link->dhcp_client);
1409
1410 switch (link->network->dhcp_client_identifier) {
1411 case DHCP_CLIENT_ID_DUID: {
0cf7c3fd 1412 /* If configured, apply user specified DUID and IAID */
fff1f40c
YW
1413 const DUID *duid = link_get_duid(link);
1414
0cf7c3fd
YW
1415 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1416 r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
8217ed5e 1417 link->network->iaid_set,
0cf7c3fd
YW
1418 link->network->iaid,
1419 duid->llt_time);
1420 else
1421 r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
8217ed5e 1422 link->network->iaid_set,
0cf7c3fd
YW
1423 link->network->iaid,
1424 duid->type,
1425 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1426 duid->raw_data_len);
fff1f40c 1427 if (r < 0)
0cf7c3fd 1428 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
fff1f40c
YW
1429 break;
1430 }
1431 case DHCP_CLIENT_ID_DUID_ONLY: {
1432 /* If configured, apply user specified DUID */
1433 const DUID *duid = link_get_duid(link);
1434
0cf7c3fd
YW
1435 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
1436 r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
89b3fa66 1437 duid->llt_time);
0cf7c3fd
YW
1438 else
1439 r = sd_dhcp_client_set_duid(link->dhcp_client,
1440 duid->type,
1441 duid->raw_data_len > 0 ? duid->raw_data : NULL,
1442 duid->raw_data_len);
fff1f40c
YW
1443 if (r < 0)
1444 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
1445 break;
1446 }
1447 case DHCP_CLIENT_ID_MAC:
1448 r = sd_dhcp_client_set_client_id(link->dhcp_client,
1449 ARPHRD_ETHER,
1450 (const uint8_t *) &link->mac,
1451 sizeof(link->mac));
1452 if (r < 0)
1453 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m");
1454 break;
1455 default:
1456 assert_not_reached("Unknown client identifier type.");
1457 }
1458
1459 return 0;
1460}
1461
3c9b8860 1462int dhcp4_configure(Link *link) {
cb29c156 1463 sd_dhcp_option *send_option;
5bc945be
SS
1464 void *request_options;
1465 Iterator i;
3c9b8860
TG
1466 int r;
1467
1468 assert(link);
1469 assert(link->network);
e0ee46f2 1470 assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
3c9b8860 1471
0bc70f1d 1472 if (!link->dhcp_client) {
db3d2358 1473 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
1f6860d9
YW
1474 if (r == -ENOMEM)
1475 return log_oom();
0bc70f1d 1476 if (r < 0)
1f6860d9 1477 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
3c9b8860 1478
08c588d1
YW
1479 r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
1480 if (r < 0)
1481 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to attach event: %m");
1482 }
3c9b8860 1483
76253e73
DW
1484 r = sd_dhcp_client_set_mac(link->dhcp_client,
1485 (const uint8_t *) &link->mac,
1486 sizeof (link->mac), ARPHRD_ETHER);
3c9b8860 1487 if (r < 0)
1f6860d9 1488 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m");
3c9b8860 1489
2f8e7633 1490 r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
3c9b8860 1491 if (r < 0)
1f6860d9 1492 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m");
3c9b8860
TG
1493
1494 r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
1495 if (r < 0)
1f6860d9 1496 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m");
3c9b8860
TG
1497
1498 r = sd_dhcp_client_set_request_broadcast(link->dhcp_client,
1499 link->network->dhcp_broadcast);
1500 if (r < 0)
1f6860d9 1501 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
3c9b8860
TG
1502
1503 if (link->mtu) {
1504 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
1505 if (r < 0)
1f6860d9 1506 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m");
3c9b8860
TG
1507 }
1508
27cb34f5 1509 if (link->network->dhcp_use_mtu) {
39745a5a 1510 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1511 SD_DHCP_OPTION_INTERFACE_MTU);
39745a5a 1512 if (r < 0)
1f6860d9 1513 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
3c9b8860
TG
1514 }
1515
5e77a146 1516 /* NOTE: even if this variable is called "use", it also "sends" PRL
1517 * options, maybe there should be a different configuration variable
1518 * to send or not route options?. */
28522b0d 1519 /* NOTE: when using Anonymize=yes, routes PRL options are sent
1520 * by default, so they don't need to be added here. */
5e77a146 1521 if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) {
3c9b8860 1522 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1523 SD_DHCP_OPTION_STATIC_ROUTE);
3c9b8860 1524 if (r < 0)
1f6860d9
YW
1525 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
1526
3c9b8860 1527 r = sd_dhcp_client_set_request_option(link->dhcp_client,
22805d92 1528 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
7d6884b6 1529 if (r < 0)
1f6860d9 1530 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
3c9b8860
TG
1531 }
1532
150d3b8e
YW
1533 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO && !link->network->dhcp_anonymize) {
1534 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST);
1535 if (r < 0)
1536 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
1537 }
1538
ead36ce6 1539 if (link->network->dhcp_use_ntp) {
1540 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
1541 if (r < 0)
1f6860d9 1542 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
ead36ce6 1543 }
4b7b5abb 1544
299d578f
SS
1545 if (link->network->dhcp_use_sip) {
1546 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_SIP_SERVER);
1547 if (r < 0)
1548 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
1549 }
1550
89573b37 1551 if (link->network->dhcp_use_timezone) {
1552 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
1553 if (r < 0)
1f6860d9 1554 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
89573b37 1555 }
8eb9058d 1556
5bc945be
SS
1557 SET_FOREACH(request_options, link->network->dhcp_request_options, i) {
1558 uint32_t option = PTR_TO_UINT32(request_options);
1559
1560 r = sd_dhcp_client_set_request_option(link->dhcp_client, option);
1561 if (r == -EEXIST) {
1562 log_link_debug(link, "DHCP4 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option);
1563 continue;
1564 }
1565
1566 if (r < 0)
1567 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option);
1568 }
1569
0e96961d 1570 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_options, i) {
7354900d
DW
1571 r = sd_dhcp_client_add_option(link->dhcp_client, send_option);
1572 if (r == -EEXIST)
1573 continue;
1574 if (r < 0)
1575 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
1576 }
1577
1578 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_vendor_options, i) {
1579 r = sd_dhcp_client_add_vendor_option(link->dhcp_client, send_option);
1580 if (r == -EEXIST)
1581 continue;
cb29c156
SS
1582 if (r < 0)
1583 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
1584 }
1585
7192bb81
LP
1586 r = dhcp4_set_hostname(link);
1587 if (r < 0)
a8494759 1588 return r;
3c9b8860
TG
1589
1590 if (link->network->dhcp_vendor_class_identifier) {
1591 r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
1592 link->network->dhcp_vendor_class_identifier);
1593 if (r < 0)
1f6860d9 1594 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
3c9b8860
TG
1595 }
1596
7b8d23a9
SS
1597 if (link->network->dhcp_mudurl) {
1598 r = sd_dhcp_client_set_mud_url(link->dhcp_client,
1599 link->network->dhcp_mudurl);
1600 if (r < 0)
1601 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MUD URL: %m");
1602 }
1603
af1c0de0
SS
1604 if (link->network->dhcp_user_class) {
1605 r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class);
1606 if (r < 0)
1f6860d9 1607 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m");
af1c0de0
SS
1608 }
1609
9faed222
SS
1610 if (link->network->dhcp_client_port) {
1611 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
1612 if (r < 0)
1f6860d9 1613 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
9faed222
SS
1614 }
1615
715cedfb
SS
1616 if (link->network->dhcp_max_attempts > 0) {
1617 r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts);
1618 if (r < 0)
1619 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m");
1620 }
1621
afe42aef
SC
1622 if (link->network->ip_service_type > 0) {
1623 r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->ip_service_type);
1624 if (r < 0)
0da425df 1625 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IP service type: %m");
afe42aef 1626 }
0f3ff4ea 1627
d6463307
SS
1628 if (link->network->dhcp_fallback_lease_lifetime > 0) {
1629 r = sd_dhcp_client_set_fallback_lease_lifetime(link->dhcp_client, link->network->dhcp_fallback_lease_lifetime);
1630 if (r < 0)
1631 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
1632 }
1633
0f3ff4ea
SS
1634 if (link->network->dhcp_send_decline) {
1635 r = configure_dhcpv4_duplicate_address_detection(link);
1636 if (r < 0)
1637 return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to configure service type: %m");
1638 }
1639
1640 return dhcp4_set_client_identifier(link);
3c9b8860 1641}
ca5ad760
YW
1642
1643int config_parse_dhcp_max_attempts(
1644 const char *unit,
1645 const char *filename,
1646 unsigned line,
1647 const char *section,
1648 unsigned section_line,
1649 const char *lvalue,
1650 int ltype,
1651 const char *rvalue,
1652 void *data,
1653 void *userdata) {
1654
1655 Network *network = data;
1656 uint64_t a;
1657 int r;
1658
1659 assert(network);
1660 assert(lvalue);
1661 assert(rvalue);
1662
1663 if (isempty(rvalue)) {
1664 network->dhcp_max_attempts = 0;
1665 return 0;
1666 }
1667
1668 if (streq(rvalue, "infinity")) {
1669 network->dhcp_max_attempts = (uint64_t) -1;
1670 return 0;
1671 }
1672
1673 r = safe_atou64(rvalue, &a);
1674 if (r < 0) {
1675 log_syntax(unit, LOG_ERR, filename, line, r,
1676 "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue);
1677 return 0;
1678 }
1679
1680 if (a == 0) {
1681 log_syntax(unit, LOG_ERR, filename, line, 0,
1682 "%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue);
1683 return 0;
1684 }
1685
1686 network->dhcp_max_attempts = a;
1687
1688 return 0;
1689}
1690
98ebef62 1691int config_parse_dhcp_acl_ip_address(
ca5ad760
YW
1692 const char *unit,
1693 const char *filename,
1694 unsigned line,
1695 const char *section,
1696 unsigned section_line,
1697 const char *lvalue,
1698 int ltype,
1699 const char *rvalue,
1700 void *data,
1701 void *userdata) {
1702
1703 Network *network = data;
98ebef62 1704 Set **acl;
ca5ad760
YW
1705 int r;
1706
1707 assert(filename);
1708 assert(lvalue);
1709 assert(rvalue);
1710 assert(data);
1711
98ebef62
SS
1712 acl = STR_IN_SET(lvalue, "DenyList", "BlackList") ? &network->dhcp_deny_listed_ip : &network->dhcp_allow_listed_ip;
1713
ca5ad760 1714 if (isempty(rvalue)) {
98ebef62 1715 *acl = set_free(*acl);
ca5ad760
YW
1716 return 0;
1717 }
1718
de7fef4b 1719 for (const char *p = rvalue;;) {
ca5ad760
YW
1720 _cleanup_free_ char *n = NULL;
1721 union in_addr_union ip;
1722
1723 r = extract_first_word(&p, &n, NULL, 0);
1724 if (r < 0) {
1725 log_syntax(unit, LOG_ERR, filename, line, r,
98ebef62
SS
1726 "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
1727 lvalue, rvalue);
ca5ad760
YW
1728 return 0;
1729 }
1730 if (r == 0)
1731 return 0;
1732
1733 r = in_addr_from_string(AF_INET, n, &ip);
1734 if (r < 0) {
1735 log_syntax(unit, LOG_ERR, filename, line, r,
98ebef62 1736 "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue, n);
ca5ad760
YW
1737 continue;
1738 }
1739
98ebef62 1740 r = set_ensure_put(acl, NULL, UINT32_TO_PTR(ip.in.s_addr));
ca5ad760
YW
1741 if (r < 0)
1742 log_syntax(unit, LOG_ERR, filename, line, r,
98ebef62 1743 "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue, n);
ca5ad760
YW
1744 }
1745
1746 return 0;
1747}
1748
a75b2117
SS
1749int config_parse_dhcp_ip_service_type(
1750 const char *unit,
1751 const char *filename,
1752 unsigned line,
1753 const char *section,
1754 unsigned section_line,
1755 const char *lvalue,
1756 int ltype,
1757 const char *rvalue,
1758 void *data,
1759 void *userdata) {
1760
1761 assert(filename);
1762 assert(lvalue);
1763 assert(rvalue);
1764
1765 if (streq(rvalue, "CS4"))
1766 *((int *)data) = IPTOS_CLASS_CS4;
1767 else if (streq(rvalue, "CS6"))
1768 *((int *)data) = IPTOS_CLASS_CS6;
1769 else
1770 log_syntax(unit, LOG_WARNING, filename, line, 0,
1771 "Failed to parse IPServiceType type '%s', ignoring.", rvalue);
1772
1773 return 0;
1774}
1775
7b8d23a9
SS
1776int config_parse_dhcp_mud_url(
1777 const char *unit,
1778 const char *filename,
1779 unsigned line,
1780 const char *section,
1781 unsigned section_line,
1782 const char *lvalue,
1783 int ltype,
1784 const char *rvalue,
1785 void *data,
1786 void *userdata) {
1787
1788 _cleanup_free_ char *unescaped = NULL;
1789 Network *network = data;
1790 int r;
1791
1792 assert(filename);
1793 assert(lvalue);
1794 assert(rvalue);
1795
1796 if (isempty(rvalue)) {
1797 network->dhcp_mudurl = mfree(network->dhcp_mudurl);
1798 return 0;
1799 }
1800
1801 r = cunescape(rvalue, 0, &unescaped);
1802 if (r < 0) {
1803 log_syntax(unit, LOG_ERR, filename, line, r,
1804 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
1805 return 0;
1806 }
1807
1808 if (!http_url_is_valid(unescaped) || strlen(unescaped) > 255) {
1809 log_syntax(unit, LOG_ERR, filename, line, 0,
1810 "Failed to parse MUD URL '%s', ignoring: %m", rvalue);
1811
1812 return 0;
1813 }
1814
1815 return free_and_strdup_warn(&network->dhcp_mudurl, unescaped);
1816}
1817
d6463307
SS
1818int config_parse_dhcp_fallback_lease_lifetime(const char *unit,
1819 const char *filename,
1820 unsigned line,
1821 const char *section,
1822 unsigned section_line,
1823 const char *lvalue,
1824 int ltype,
1825 const char *rvalue,
1826 void *data,
1827 void *userdata) {
1828 Network *network = userdata;
d2735796 1829 uint32_t k;
d6463307
SS
1830
1831 assert(filename);
1832 assert(section);
1833 assert(lvalue);
1834 assert(rvalue);
1835 assert(data);
1836
1837 if (isempty(rvalue)) {
1838 network->dhcp_fallback_lease_lifetime = 0;
1839 return 0;
1840 }
1841
1842 /* We accept only "forever" or "infinity". */
1843 if (STR_IN_SET(rvalue, "forever", "infinity"))
1844 k = CACHE_INFO_INFINITY_LIFE_TIME;
1845 else {
1846 log_syntax(unit, LOG_ERR, filename, line, 0,
1847 "Invalid LeaseLifetime= value, ignoring: %s", rvalue);
1848 return 0;
1849 }
1850
1851 network->dhcp_fallback_lease_lifetime = k;
1852
1853 return 0;
1854}
1855
ca5ad760
YW
1856static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = {
1857 [DHCP_CLIENT_ID_MAC] = "mac",
1858 [DHCP_CLIENT_ID_DUID] = "duid",
1859 [DHCP_CLIENT_ID_DUID_ONLY] = "duid-only",
1860};
1861
1862DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier);
1863DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier,
1864 "Failed to parse client identifier type");