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