]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-dhcp-prefix-delegation.c
network/route: introduce ref/unref functions for Route object
[thirdparty/systemd.git] / src / network / networkd-dhcp-prefix-delegation.c
CommitLineData
d5ebcf65
YW
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
557e1b52
YW
3#include <linux/ipv6_route.h>
4
d8ec95c7 5#include "dhcp6-lease-internal.h"
d5ebcf65
YW
6#include "hashmap.h"
7#include "in-addr-prefix-util.h"
8#include "networkd-address-generation.h"
9#include "networkd-address.h"
10#include "networkd-dhcp-prefix-delegation.h"
11#include "networkd-dhcp6.h"
12#include "networkd-link.h"
13#include "networkd-manager.h"
14#include "networkd-queue.h"
15#include "networkd-radv.h"
16#include "networkd-route.h"
e49bad01 17#include "networkd-setlink.h"
d5ebcf65
YW
18#include "parse-util.h"
19#include "string-util.h"
20#include "strv.h"
e49bad01 21#include "tunnel.h"
d5ebcf65 22
a27588d4 23bool link_dhcp_pd_is_enabled(Link *link) {
d5ebcf65
YW
24 assert(link);
25
26 if (!link->network)
27 return false;
28
a27588d4 29 return link->network->dhcp_pd;
d5ebcf65
YW
30}
31
a27588d4 32bool dhcp_pd_is_uplink(Link *link, Link *target, bool accept_auto) {
d5ebcf65
YW
33 assert(link);
34 assert(target);
35
a27588d4 36 if (!link_dhcp_pd_is_enabled(link))
d5ebcf65
YW
37 return false;
38
a27588d4
YW
39 if (link->network->dhcp_pd_uplink_name)
40 return streq_ptr(target->ifname, link->network->dhcp_pd_uplink_name) ||
41 strv_contains(target->alternative_names, link->network->dhcp_pd_uplink_name);
d5ebcf65 42
a27588d4
YW
43 if (link->network->dhcp_pd_uplink_index > 0)
44 return target->ifindex == link->network->dhcp_pd_uplink_index;
d5ebcf65 45
a27588d4 46 if (link->network->dhcp_pd_uplink_index == UPLINK_INDEX_SELF)
d5ebcf65
YW
47 return link == target;
48
a27588d4 49 assert(link->network->dhcp_pd_uplink_index == UPLINK_INDEX_AUTO);
d5ebcf65
YW
50 return accept_auto;
51}
52
a27588d4 53static void link_remove_dhcp_pd_subnet_prefix(Link *link, const struct in6_addr *prefix) {
d5ebcf65
YW
54 void *key;
55
56 assert(link);
57 assert(link->manager);
58 assert(prefix);
59
a27588d4 60 if (hashmap_get(link->manager->links_by_dhcp_pd_subnet_prefix, prefix) != link)
d5ebcf65
YW
61 return;
62
a27588d4 63 hashmap_remove2(link->manager->links_by_dhcp_pd_subnet_prefix, prefix, &key);
d5ebcf65
YW
64 free(key);
65}
66
a27588d4 67static int link_add_dhcp_pd_subnet_prefix(Link *link, const struct in6_addr *prefix) {
d5ebcf65
YW
68 _cleanup_free_ struct in6_addr *copy = NULL;
69 int r;
70
71 assert(link);
72 assert(prefix);
73
74 copy = newdup(struct in6_addr, prefix, 1);
75 if (!copy)
76 return -ENOMEM;
77
a27588d4 78 r = hashmap_ensure_put(&link->manager->links_by_dhcp_pd_subnet_prefix, &in6_addr_hash_ops_free, copy, link);
d5ebcf65
YW
79 if (r < 0)
80 return r;
81 if (r > 0)
82 TAKE_PTR(copy);
83
84 return 0;
85}
86
a27588d4 87static int link_get_by_dhcp_pd_subnet_prefix(Manager *manager, const struct in6_addr *prefix, Link **ret) {
d5ebcf65
YW
88 Link *link;
89
90 assert(manager);
91 assert(prefix);
92
a27588d4 93 link = hashmap_get(manager->links_by_dhcp_pd_subnet_prefix, prefix);
d5ebcf65
YW
94 if (!link)
95 return -ENODEV;
96
97 if (ret)
98 *ret = link;
99 return 0;
100}
101
a27588d4 102static int dhcp_pd_get_assigned_subnet_prefix(Link *link, const struct in6_addr *pd_prefix, uint8_t pd_prefix_len, struct in6_addr *ret) {
d5ebcf65 103 assert(link);
8d01e44c 104 assert(link->manager);
d5ebcf65
YW
105 assert(pd_prefix);
106
a27588d4 107 if (!link_dhcp_pd_is_enabled(link))
d5ebcf65
YW
108 return -ENOENT;
109
a27588d4 110 if (link->network->dhcp_pd_assign) {
d5ebcf65
YW
111 Address *address;
112
113 SET_FOREACH(address, link->addresses) {
a27588d4 114 if (address->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65
YW
115 continue;
116 assert(address->family == AF_INET6);
117
118 if (in6_addr_prefix_covers(pd_prefix, pd_prefix_len, &address->in_addr.in6) <= 0)
119 continue;
120
121 if (ret) {
122 struct in6_addr prefix = address->in_addr.in6;
123
124 in6_addr_mask(&prefix, 64);
125 *ret = prefix;
126 }
127 return 0;
128 }
129 } else {
130 Route *route;
131
8d01e44c 132 SET_FOREACH(route, link->manager->routes) {
a27588d4 133 if (route->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65
YW
134 continue;
135 assert(route->family == AF_INET6);
136
8d01e44c
YW
137 if (route->nexthop.ifindex != link->ifindex)
138 continue;
139
d5ebcf65
YW
140 if (in6_addr_prefix_covers(pd_prefix, pd_prefix_len, &route->dst.in6) > 0) {
141 if (ret)
142 *ret = route->dst.in6;
143 return 0;
144 }
145 }
146 }
147
148 return -ENOENT;
149}
150
a27588d4 151int dhcp_pd_remove(Link *link, bool only_marked) {
372acaad 152 int ret = 0;
d5ebcf65
YW
153
154 assert(link);
155 assert(link->manager);
156
a27588d4 157 if (!link_dhcp_pd_is_enabled(link))
d5ebcf65
YW
158 return 0;
159
160 if (!only_marked)
a27588d4 161 link->dhcp_pd_configured = false;
d5ebcf65 162
a27588d4 163 if (!link->network->dhcp_pd_assign) {
d5ebcf65
YW
164 Route *route;
165
8d01e44c 166 SET_FOREACH(route, link->manager->routes) {
a27588d4 167 if (route->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65 168 continue;
8d01e44c
YW
169 if (route->nexthop.ifindex != link->ifindex)
170 continue;
d5ebcf65
YW
171 if (only_marked && !route_is_marked(route))
172 continue;
173
174 if (link->radv)
95931532 175 sd_radv_remove_prefix(link->radv, &route->dst.in6, 64);
d5ebcf65 176
a27588d4 177 link_remove_dhcp_pd_subnet_prefix(link, &route->dst.in6);
d5ebcf65 178
3caed9ea 179 RET_GATHER(ret, route_remove_and_cancel(route, link->manager));
d5ebcf65
YW
180 }
181 } else {
182 Address *address;
183
184 SET_FOREACH(address, link->addresses) {
185 struct in6_addr prefix;
186
a27588d4 187 if (address->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65
YW
188 continue;
189 if (only_marked && !address_is_marked(address))
190 continue;
191
192 prefix = address->in_addr.in6;
193 in6_addr_mask(&prefix, 64);
194
195 if (link->radv)
95931532 196 sd_radv_remove_prefix(link->radv, &prefix, 64);
d5ebcf65 197
a27588d4 198 link_remove_dhcp_pd_subnet_prefix(link, &prefix);
d5ebcf65 199
f22b586a 200 RET_GATHER(ret, address_remove_and_cancel(address, link));
d5ebcf65
YW
201 }
202 }
203
372acaad 204 return ret;
d5ebcf65
YW
205}
206
a27588d4 207static int dhcp_pd_check_ready(Link *link);
d5ebcf65 208
a27588d4 209static int dhcp_pd_address_ready_callback(Address *address) {
d5ebcf65
YW
210 Address *a;
211
212 assert(address);
213 assert(address->link);
214
215 SET_FOREACH(a, address->link->addresses)
a27588d4 216 if (a->source == NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65
YW
217 a->callback = NULL;
218
a27588d4 219 return dhcp_pd_check_ready(address->link);
d5ebcf65
YW
220}
221
a27588d4 222static int dhcp_pd_check_ready(Link *link) {
d5ebcf65
YW
223 int r;
224
225 assert(link);
226 assert(link->network);
227
a27588d4
YW
228 if (link->dhcp_pd_messages > 0) {
229 log_link_debug(link, "%s(): DHCP-PD addresses and routes are not set.", __func__);
d5ebcf65
YW
230 return 0;
231 }
232
a27588d4 233 if (link->network->dhcp_pd_assign) {
d5ebcf65
YW
234 bool has_ready = false;
235 Address *address;
236
237 SET_FOREACH(address, link->addresses) {
a27588d4 238 if (address->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65
YW
239 continue;
240 if (address_is_ready(address)) {
241 has_ready = true;
242 break;
243 }
244 }
245
246 if (!has_ready) {
247 SET_FOREACH(address, link->addresses)
a27588d4
YW
248 if (address->source == NETWORK_CONFIG_SOURCE_DHCP_PD)
249 address->callback = dhcp_pd_address_ready_callback;
d5ebcf65 250
a27588d4 251 log_link_debug(link, "%s(): no DHCP-PD address is ready.", __func__);
d5ebcf65
YW
252 return 0;
253 }
254 }
255
a27588d4 256 link->dhcp_pd_configured = true;
d5ebcf65 257
a27588d4 258 log_link_debug(link, "DHCP-PD addresses and routes set.");
d5ebcf65 259
a27588d4 260 r = dhcp_pd_remove(link, /* only_marked = */ true);
d5ebcf65
YW
261 if (r < 0)
262 return r;
263
264 link_check_ready(link);
265 return 1;
266}
267
80d62d4f 268static int dhcp_pd_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) {
d5ebcf65
YW
269 int r;
270
271 assert(link);
d5ebcf65 272
2c0b49ba 273 r = route_configure_handler_internal(rtnl, m, link, route, "Failed to add prefix route for DHCP delegated subnet prefix");
d5ebcf65
YW
274 if (r <= 0)
275 return r;
276
a27588d4 277 r = dhcp_pd_check_ready(link);
d5ebcf65
YW
278 if (r < 0)
279 link_enter_failed(link);
280
281 return 1;
282}
283
38488bab 284static int dhcp_pd_request_route(Link *link, const struct in6_addr *prefix, usec_t lifetime_usec) {
74c301b9 285 _cleanup_(route_unrefp) Route *route = NULL;
d5ebcf65
YW
286 Route *existing;
287 int r;
288
289 assert(link);
8d01e44c 290 assert(link->manager);
d5ebcf65
YW
291 assert(link->network);
292 assert(prefix);
293
a27588d4 294 if (link->network->dhcp_pd_assign)
d5ebcf65
YW
295 return 0;
296
297 r = route_new(&route);
298 if (r < 0)
299 return r;
300
a27588d4 301 route->source = NETWORK_CONFIG_SOURCE_DHCP_PD;
d5ebcf65
YW
302 route->family = AF_INET6;
303 route->dst.in6 = *prefix;
38488bab 304 route->dst_prefixlen = 64;
d5ebcf65 305 route->protocol = RTPROT_DHCP;
a27588d4 306 route->priority = link->network->dhcp_pd_route_metric;
d5ebcf65
YW
307 route->lifetime_usec = lifetime_usec;
308
2f542fc3
YW
309 r = route_adjust_nexthops(route, link);
310 if (r < 0)
311 return r;
312
8d01e44c 313 if (route_get(link->manager, route, &existing) < 0)
a27588d4 314 link->dhcp_pd_configured = false;
d5ebcf65
YW
315 else
316 route_unmark(existing);
317
5a18697d 318 r = link_request_route(link, route, &link->dhcp_pd_messages, dhcp_pd_route_handler);
d5ebcf65 319 if (r < 0)
a27588d4 320 return log_link_error_errno(link, r, "Failed to request DHCP-PD prefix route: %m");
d5ebcf65
YW
321
322 return 0;
323}
324
80d62d4f 325static int dhcp_pd_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) {
d5ebcf65
YW
326 int r;
327
328 assert(link);
d5ebcf65 329
a27588d4 330 r = address_configure_handler_internal(rtnl, m, link, "Could not set DHCP-PD address");
d5ebcf65
YW
331 if (r <= 0)
332 return r;
333
a27588d4 334 r = dhcp_pd_check_ready(link);
d5ebcf65
YW
335 if (r < 0)
336 link_enter_failed(link);
337
338 return 1;
339}
340
a27588d4 341static void log_dhcp_pd_address(Link *link, const Address *address) {
d5ebcf65
YW
342 assert(address);
343 assert(address->family == AF_INET6);
344
b33dd04e 345 int log_level = address_get_harder(link, address, NULL) >= 0 ? LOG_DEBUG : LOG_INFO;
d5ebcf65
YW
346
347 if (log_level < log_get_max_level())
348 return;
349
a27588d4 350 log_link_full(link, log_level, "DHCP-PD address %s (valid %s, preferred %s)",
c71384a9 351 IN6_ADDR_PREFIX_TO_STRING(&address->in_addr.in6, address->prefixlen),
d5ebcf65
YW
352 FORMAT_LIFETIME(address->lifetime_valid_usec),
353 FORMAT_LIFETIME(address->lifetime_preferred_usec));
354}
355
a27588d4 356static int dhcp_pd_request_address(
d5ebcf65
YW
357 Link *link,
358 const struct in6_addr *prefix,
359 usec_t lifetime_preferred_usec,
360 usec_t lifetime_valid_usec) {
361
362 _cleanup_set_free_ Set *addresses = NULL;
363 struct in6_addr *a;
364 int r;
365
366 assert(link);
367 assert(link->network);
368 assert(prefix);
369
a27588d4 370 if (!link->network->dhcp_pd_assign)
d5ebcf65
YW
371 return 0;
372
38488bab 373 r = dhcp_pd_generate_addresses(link, prefix, &addresses);
d5ebcf65 374 if (r < 0)
a27588d4 375 return log_link_warning_errno(link, r, "Failed to generate addresses for acquired DHCP delegated prefix: %m");
d5ebcf65
YW
376
377 SET_FOREACH(a, addresses) {
ebd96906 378 _cleanup_(address_unrefp) Address *address = NULL;
d5ebcf65
YW
379 Address *existing;
380
381 r = address_new(&address);
382 if (r < 0)
a27588d4 383 return log_link_error_errno(link, r, "Failed to allocate address for DHCP delegated prefix: %m");
d5ebcf65 384
a27588d4 385 address->source = NETWORK_CONFIG_SOURCE_DHCP_PD;
d5ebcf65
YW
386 address->family = AF_INET6;
387 address->in_addr.in6 = *a;
38488bab 388 address->prefixlen = 64;
d5ebcf65
YW
389 address->lifetime_preferred_usec = lifetime_preferred_usec;
390 address->lifetime_valid_usec = lifetime_valid_usec;
38488bab 391 SET_FLAG(address->flags, IFA_F_MANAGETEMPADDR, link->network->dhcp_pd_manage_temporary_address);
a27588d4 392 address->route_metric = link->network->dhcp_pd_route_metric;
d5ebcf65 393
a27588d4 394 log_dhcp_pd_address(link, address);
d5ebcf65 395
4b3590c3
TM
396 r = free_and_strdup_warn(&address->netlabel, link->network->dhcp_pd_netlabel);
397 if (r < 0)
398 return r;
399
d5ebcf65 400 if (address_get(link, address, &existing) < 0)
a27588d4 401 link->dhcp_pd_configured = false;
d5ebcf65
YW
402 else
403 address_unmark(existing);
404
f60e6558 405 r = link_request_address(link, address, &link->dhcp_pd_messages,
a27588d4 406 dhcp_pd_address_handler, NULL);
d5ebcf65 407 if (r < 0)
a27588d4 408 return log_link_error_errno(link, r, "Failed to request DHCP delegated prefix address: %m");
d5ebcf65
YW
409 }
410
411 return 0;
412}
413
a27588d4 414static int dhcp_pd_calculate_subnet_prefix(
d5ebcf65
YW
415 const struct in6_addr *pd_prefix,
416 uint8_t pd_prefix_len,
417 uint64_t subnet_id,
418 struct in6_addr *ret) {
419
420 struct in6_addr prefix;
421
422 assert(pd_prefix);
423 assert(pd_prefix_len <= 64);
424 assert(ret);
425
426 if (subnet_id >= UINT64_C(1) << (64 - pd_prefix_len))
427 return -ERANGE;
428
429 prefix = *pd_prefix;
430
431 if (pd_prefix_len < 32)
432 prefix.s6_addr32[0] |= htobe32(subnet_id >> 32);
433
434 prefix.s6_addr32[1] |= htobe32(subnet_id & 0xffffffff);
435
436 *ret = prefix;
437 return 0;
438}
439
a27588d4 440static int dhcp_pd_get_preferred_subnet_prefix(
d5ebcf65
YW
441 Link *link,
442 const struct in6_addr *pd_prefix,
443 uint8_t pd_prefix_len,
444 struct in6_addr *ret) {
445
446 struct in6_addr prefix;
447 Link *assigned_link;
448 int r;
449
450 assert(link);
451 assert(link->manager);
11d8a83f 452 assert(link->network);
d5ebcf65
YW
453 assert(pd_prefix);
454
a27588d4 455 if (link->network->dhcp_pd_subnet_id >= 0) {
d5ebcf65
YW
456 /* If the link has a preference for a particular subnet id try to allocate that */
457
a27588d4 458 r = dhcp_pd_calculate_subnet_prefix(pd_prefix, pd_prefix_len, link->network->dhcp_pd_subnet_id, &prefix);
d5ebcf65
YW
459 if (r < 0)
460 return log_link_warning_errno(link, r,
c0f86d66 461 "subnet id %" PRIi64 " is out of range. Only have %" PRIu64 " subnets.",
a27588d4 462 link->network->dhcp_pd_subnet_id, UINT64_C(1) << (64 - pd_prefix_len));
d5ebcf65 463
d5ebcf65
YW
464 *ret = prefix;
465 return 0;
466 }
467
07b7337a
YW
468 if (dhcp_pd_get_assigned_subnet_prefix(link, pd_prefix, pd_prefix_len, ret) >= 0)
469 return 0;
470
d5ebcf65
YW
471 for (uint64_t n = 0; ; n++) {
472 /* If we do not have an allocation preference just iterate
473 * through the address space and return the first free prefix. */
474
a27588d4 475 r = dhcp_pd_calculate_subnet_prefix(pd_prefix, pd_prefix_len, n, &prefix);
d5ebcf65
YW
476 if (r < 0)
477 return log_link_warning_errno(link, r,
478 "Couldn't find a suitable prefix. Ran out of address space.");
479
480 /* Do not use explicitly requested subnet IDs. Note that the corresponding link may not
481 * appear yet. So, we need to check the ID is not used in any .network files. */
a27588d4 482 if (set_contains(link->manager->dhcp_pd_subnet_ids, &n))
d5ebcf65
YW
483 continue;
484
485 /* Check that the prefix is not assigned to another link. */
a27588d4 486 if (link_get_by_dhcp_pd_subnet_prefix(link->manager, &prefix, &assigned_link) < 0 ||
07b7337a
YW
487 assigned_link == link)
488 break;
d5ebcf65 489 }
07b7337a
YW
490
491 r = link_add_dhcp_pd_subnet_prefix(link, &prefix);
492 if (r < 0)
493 return log_link_warning_errno(link, r, "Failed to save acquired free subnet prefix: %m");
494
495 *ret = prefix;
496 return 0;
d5ebcf65
YW
497}
498
a27588d4 499static int dhcp_pd_assign_subnet_prefix(
d5ebcf65
YW
500 Link *link,
501 const struct in6_addr *pd_prefix,
502 uint8_t pd_prefix_len,
503 usec_t lifetime_preferred_usec,
38488bab
YW
504 usec_t lifetime_valid_usec,
505 bool is_uplink) {
d5ebcf65 506
d5ebcf65
YW
507 struct in6_addr prefix;
508 int r;
509
510 assert(link);
511 assert(link->network);
512 assert(pd_prefix);
513
07b7337a
YW
514 r = dhcp_pd_get_preferred_subnet_prefix(link, pd_prefix, pd_prefix_len, &prefix);
515 if (r < 0)
516 return r == -ERANGE ? 0 : r;
d5ebcf65 517
c71384a9 518 const char *pretty = IN6_ADDR_PREFIX_TO_STRING(&prefix, 64);
d5ebcf65 519
a27588d4 520 if (link_radv_enabled(link) && link->network->dhcp_pd_announce) {
38488bab
YW
521 if (is_uplink)
522 log_link_debug(link, "Ignoring Announce= setting on upstream interface.");
523 else {
524 r = radv_add_prefix(link, &prefix, 64, lifetime_preferred_usec, lifetime_valid_usec);
525 if (r < 0)
526 return log_link_warning_errno(link, r,
527 "Failed to assign/update prefix %s to IPv6 Router Advertisement: %m",
c71384a9 528 pretty);
38488bab 529 }
d5ebcf65
YW
530 }
531
38488bab 532 r = dhcp_pd_request_route(link, &prefix, lifetime_valid_usec);
d5ebcf65
YW
533 if (r < 0)
534 return log_link_warning_errno(link, r,
c71384a9 535 "Failed to assign/update route for prefix %s: %m", pretty);
d5ebcf65 536
38488bab 537 r = dhcp_pd_request_address(link, &prefix, lifetime_preferred_usec, lifetime_valid_usec);
d5ebcf65
YW
538 if (r < 0)
539 return log_link_warning_errno(link, r,
c71384a9 540 "Failed to assign/update address for prefix %s: %m", pretty);
d5ebcf65 541
c71384a9 542 log_link_debug(link, "Assigned prefix %s", pretty);
d5ebcf65
YW
543 return 1;
544}
545
a27588d4 546static int dhcp_pd_prepare(Link *link) {
d5ebcf65
YW
547 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
548 return 0;
549
a27588d4 550 if (!link_dhcp_pd_is_enabled(link))
d5ebcf65
YW
551 return 0;
552
a27588d4 553 if (link_radv_enabled(link) && link->network->dhcp_pd_announce && !link->radv)
d5ebcf65
YW
554 return 0;
555
2ccada8d 556 link_mark_addresses(link, NETWORK_CONFIG_SOURCE_DHCP_PD);
8d01e44c 557 manager_mark_routes(link->manager, link, NETWORK_CONFIG_SOURCE_DHCP_PD);
d5ebcf65 558
c3cd5351 559 return 1;
d5ebcf65
YW
560}
561
a27588d4 562static int dhcp_pd_finalize(Link *link) {
d5ebcf65
YW
563 int r;
564
565 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
566 return 0;
567
a27588d4
YW
568 if (link->dhcp_pd_messages == 0) {
569 link->dhcp_pd_configured = false;
d5ebcf65 570
a27588d4 571 r = dhcp_pd_remove(link, /* only_marked = */ true);
d5ebcf65
YW
572 if (r < 0)
573 return r;
574 }
575
a27588d4 576 if (!link->dhcp_pd_configured)
d5ebcf65
YW
577 link_set_state(link, LINK_STATE_CONFIGURING);
578
579 link_check_ready(link);
580 return 0;
581}
582
a27588d4 583void dhcp_pd_prefix_lost(Link *uplink) {
da10d2d5 584 Route *route;
d5ebcf65
YW
585 Link *link;
586 int r;
587
a27588d4
YW
588 assert(uplink);
589 assert(uplink->manager);
d5ebcf65 590
a27588d4
YW
591 HASHMAP_FOREACH(link, uplink->manager->links_by_index) {
592 if (!dhcp_pd_is_uplink(link, uplink, /* accept_auto = */ true))
d5ebcf65
YW
593 continue;
594
a27588d4 595 r = dhcp_pd_remove(link, /* only_marked = */ false);
d5ebcf65
YW
596 if (r < 0)
597 link_enter_failed(link);
598 }
599
a27588d4 600 SET_FOREACH(route, uplink->manager->routes) {
277521a1 601 if (!IN_SET(route->source, NETWORK_CONFIG_SOURCE_DHCP4, NETWORK_CONFIG_SOURCE_DHCP6))
da10d2d5
YW
602 continue;
603 if (route->family != AF_INET6)
604 continue;
605 if (route->type != RTN_UNREACHABLE)
606 continue;
a27588d4 607 if (!set_contains(uplink->dhcp_pd_prefixes,
da10d2d5
YW
608 &(struct in_addr_prefix) {
609 .family = AF_INET6,
610 .prefixlen = route->dst_prefixlen,
611 .address = route->dst }))
612 continue;
613
3caed9ea 614 (void) route_remove_and_cancel(route, uplink->manager);
da10d2d5
YW
615 }
616
a27588d4 617 set_clear(uplink->dhcp_pd_prefixes);
d5ebcf65
YW
618}
619
e49bad01
YW
620void dhcp4_pd_prefix_lost(Link *uplink) {
621 Link *tunnel;
622
623 dhcp_pd_prefix_lost(uplink);
624
625 if (uplink->dhcp4_6rd_tunnel_name &&
626 link_get_by_name(uplink->manager, uplink->dhcp4_6rd_tunnel_name, &tunnel) >= 0)
627 (void) link_remove(tunnel);
628}
629
80d62d4f 630static int dhcp4_unreachable_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) {
e49bad01
YW
631 int r;
632
633 assert(link);
e49bad01 634
2c0b49ba 635 r = route_configure_handler_internal(rtnl, m, link, route, "Failed to set unreachable route for DHCPv4 delegated prefix");
e49bad01
YW
636 if (r <= 0)
637 return r;
638
639 r = dhcp4_check_ready(link);
640 if (r < 0)
641 link_enter_failed(link);
642
643 return 1;
644}
645
80d62d4f 646static int dhcp6_unreachable_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) {
d5ebcf65
YW
647 int r;
648
649 assert(link);
d5ebcf65 650
2c0b49ba 651 r = route_configure_handler_internal(rtnl, m, link, route, "Failed to set unreachable route for DHCPv6 delegated prefix");
d5ebcf65
YW
652 if (r <= 0)
653 return r;
654
655 r = dhcp6_check_ready(link);
656 if (r < 0)
657 link_enter_failed(link);
658
659 return 1;
660}
661
a27588d4 662static int dhcp_request_unreachable_route(
5ed10a19
YW
663 Link *link,
664 const struct in6_addr *addr,
665 uint8_t prefixlen,
666 usec_t lifetime_usec,
a27588d4
YW
667 NetworkConfigSource source,
668 const union in_addr_union *server_address,
669 unsigned *counter,
706cd859
YW
670 route_netlink_handler_t callback,
671 bool *configured) {
5ed10a19 672
74c301b9 673 _cleanup_(route_unrefp) Route *route = NULL;
d5ebcf65
YW
674 Route *existing;
675 int r;
676
677 assert(link);
8d01e44c 678 assert(link->manager);
d5ebcf65 679 assert(addr);
a27588d4 680 assert(IN_SET(source, NETWORK_CONFIG_SOURCE_DHCP4, NETWORK_CONFIG_SOURCE_DHCP6));
5ed10a19 681 assert(server_address);
a27588d4
YW
682 assert(counter);
683 assert(callback);
706cd859 684 assert(configured);
d5ebcf65 685
a536ec38 686 if (prefixlen >= 64) {
a27588d4 687 log_link_debug(link, "Not adding a blocking route for DHCP delegated prefix %s since the prefix has length >= 64.",
c71384a9 688 IN6_ADDR_PREFIX_TO_STRING(addr, prefixlen));
d5ebcf65
YW
689 return 0;
690 }
691
692 r = route_new(&route);
693 if (r < 0)
694 return log_oom();
695
a27588d4 696 route->source = source;
5ed10a19 697 route->provider = *server_address;
d5ebcf65
YW
698 route->family = AF_INET6;
699 route->dst.in6 = *addr;
700 route->dst_prefixlen = prefixlen;
d5ebcf65
YW
701 route->type = RTN_UNREACHABLE;
702 route->protocol = RTPROT_DHCP;
557e1b52 703 route->priority = IP6_RT_PRIO_USER;
d5ebcf65
YW
704 route->lifetime_usec = lifetime_usec;
705
2f542fc3
YW
706 r = route_adjust_nexthops(route, link);
707 if (r < 0)
708 return r;
709
8d01e44c 710 if (route_get(link->manager, route, &existing) < 0)
706cd859 711 *configured = false;
d5ebcf65
YW
712 else
713 route_unmark(existing);
714
5a18697d 715 r = link_request_route(link, route, counter, callback);
c71384a9 716 if (r < 0)
a27588d4 717 return log_link_error_errno(link, r, "Failed to request unreachable route for DHCP delegated prefix %s: %m",
c71384a9 718 IN6_ADDR_PREFIX_TO_STRING(addr, prefixlen));
d5ebcf65
YW
719
720 return 0;
721}
722
e49bad01
YW
723static int dhcp4_request_unreachable_route(
724 Link *link,
725 const struct in6_addr *addr,
726 uint8_t prefixlen,
727 usec_t lifetime_usec,
728 const union in_addr_union *server_address) {
729
730 return dhcp_request_unreachable_route(link, addr, prefixlen, lifetime_usec,
731 NETWORK_CONFIG_SOURCE_DHCP4, server_address,
706cd859
YW
732 &link->dhcp4_messages, dhcp4_unreachable_route_handler,
733 &link->dhcp4_configured);
e49bad01
YW
734}
735
a27588d4
YW
736static int dhcp6_request_unreachable_route(
737 Link *link,
738 const struct in6_addr *addr,
739 uint8_t prefixlen,
740 usec_t lifetime_usec,
741 const union in_addr_union *server_address) {
742
743 return dhcp_request_unreachable_route(link, addr, prefixlen, lifetime_usec,
744 NETWORK_CONFIG_SOURCE_DHCP6, server_address,
706cd859
YW
745 &link->dhcp6_messages, dhcp6_unreachable_route_handler,
746 &link->dhcp6_configured);
a27588d4
YW
747}
748
749static int dhcp_pd_prefix_add(Link *link, const struct in6_addr *prefix, uint8_t prefixlen) {
d5ebcf65
YW
750 struct in_addr_prefix *p;
751 int r;
752
753 assert(link);
754 assert(prefix);
755
756 p = new(struct in_addr_prefix, 1);
757 if (!p)
758 return log_oom();
759
760 *p = (struct in_addr_prefix) {
761 .family = AF_INET6,
762 .prefixlen = prefixlen,
763 .address.in6 = *prefix,
764 };
765
c71384a9
ZJS
766 int log_level = set_contains(link->dhcp_pd_prefixes, p) ? LOG_DEBUG :
767 prefixlen > 64 || prefixlen < 48 ? LOG_WARNING : LOG_INFO;
d5ebcf65 768 log_link_full(link,
c71384a9 769 log_level,
21feba0a 770 "DHCP: received delegated prefix %s%s",
c71384a9 771 IN6_ADDR_PREFIX_TO_STRING(prefix, prefixlen),
d5ebcf65
YW
772 prefixlen > 64 ? " with prefix length > 64, ignoring." :
773 prefixlen < 48 ? " with prefix length < 48, looks unusual.": "");
774
775 /* Store PD prefix even if prefixlen > 64, not to make logged at warning level so frequently. */
a27588d4 776 r = set_ensure_consume(&link->dhcp_pd_prefixes, &in_addr_prefix_hash_ops_free, p);
d5ebcf65 777 if (r < 0)
c71384a9
ZJS
778 return log_link_error_errno(link, r, "Failed to store DHCP delegated prefix %s: %m",
779 IN6_ADDR_PREFIX_TO_STRING(prefix, prefixlen));
a536ec38 780 return 0;
d5ebcf65
YW
781}
782
e49bad01 783static int dhcp4_pd_request_default_gateway_on_6rd_tunnel(Link *link, const struct in_addr *br_address, usec_t lifetime_usec) {
74c301b9 784 _cleanup_(route_unrefp) Route *route = NULL;
e49bad01
YW
785 Route *existing;
786 int r;
787
788 assert(link);
8d01e44c 789 assert(link->manager);
e49bad01
YW
790 assert(br_address);
791
792 r = route_new(&route);
793 if (r < 0)
794 return log_link_debug_errno(link, r, "Failed to allocate default gateway for DHCP delegated prefix: %m");
795
796 route->source = NETWORK_CONFIG_SOURCE_DHCP_PD;
797 route->family = AF_INET6;
054b8c28
YW
798 route->nexthop.family = AF_INET6;
799 route->nexthop.gw.in6.s6_addr32[3] = br_address->s_addr;
e49bad01
YW
800 route->scope = RT_SCOPE_UNIVERSE;
801 route->protocol = RTPROT_DHCP;
802 route->priority = IP6_RT_PRIO_USER;
803 route->lifetime_usec = lifetime_usec;
804
2f542fc3
YW
805 r = route_adjust_nexthops(route, link);
806 if (r < 0)
807 return r;
808
8d01e44c 809 if (route_get(link->manager, route, &existing) < 0) /* This is a new route. */
e49bad01
YW
810 link->dhcp_pd_configured = false;
811 else
812 route_unmark(existing);
813
5a18697d 814 r = link_request_route(link, route, &link->dhcp_pd_messages, dhcp_pd_route_handler);
e49bad01
YW
815 if (r < 0)
816 return log_link_debug_errno(link, r, "Failed to request default gateway for DHCP delegated prefix: %m");
817
818 return 0;
819}
820
821static void dhcp4_calculate_pd_prefix(
822 const struct in_addr *ipv4address,
823 uint8_t ipv4masklen,
824 const struct in6_addr *sixrd_prefix,
825 uint8_t sixrd_prefixlen,
826 struct in6_addr *ret_pd_prefix,
827 uint8_t *ret_pd_prefixlen) {
828
829 struct in6_addr pd_prefix;
830
831 assert(ipv4address);
832 assert(ipv4masklen <= 32);
833 assert(sixrd_prefix);
834 assert(32 - ipv4masklen + sixrd_prefixlen <= 128);
835 assert(ret_pd_prefix);
836
837 pd_prefix = *sixrd_prefix;
838 for (unsigned i = 0; i < (unsigned) (32 - ipv4masklen); i++)
839 if (ipv4address->s_addr & htobe32(UINT32_C(1) << (32 - ipv4masklen - i - 1)))
840 pd_prefix.s6_addr[(i + sixrd_prefixlen) / 8] |= 1 << (7 - (i + sixrd_prefixlen) % 8);
841
842 *ret_pd_prefix = pd_prefix;
843 if (ret_pd_prefixlen)
844 *ret_pd_prefixlen = 32 - ipv4masklen + sixrd_prefixlen;
845}
846
847static int dhcp4_pd_assign_subnet_prefix(Link *link, Link *uplink) {
848 uint8_t ipv4masklen, sixrd_prefixlen, pd_prefixlen;
849 struct in6_addr sixrd_prefix, pd_prefix;
850 const struct in_addr *br_addresses;
851 struct in_addr ipv4address;
a93538e8 852 usec_t lifetime_usec;
e49bad01
YW
853 int r;
854
855 assert(link);
856 assert(uplink);
def4741b 857 assert(uplink->manager);
e49bad01
YW
858 assert(uplink->dhcp_lease);
859
860 r = sd_dhcp_lease_get_address(uplink->dhcp_lease, &ipv4address);
861 if (r < 0)
862 return log_link_warning_errno(uplink, r, "Failed to get DHCPv4 address: %m");
863
a93538e8 864 r = sd_dhcp_lease_get_lifetime_timestamp(uplink->dhcp_lease, CLOCK_BOOTTIME, &lifetime_usec);
e49bad01
YW
865 if (r < 0)
866 return log_link_warning_errno(uplink, r, "Failed to get lifetime of DHCPv4 lease: %m");
867
e49bad01
YW
868 r = sd_dhcp_lease_get_6rd(uplink->dhcp_lease, &ipv4masklen, &sixrd_prefixlen, &sixrd_prefix, &br_addresses, NULL);
869 if (r < 0)
21feba0a 870 return log_link_warning_errno(uplink, r, "Failed to get DHCPv4 6rd option: %m");
e49bad01
YW
871
872 dhcp4_calculate_pd_prefix(&ipv4address, ipv4masklen, &sixrd_prefix, sixrd_prefixlen, &pd_prefix, &pd_prefixlen);
873
874 if (pd_prefixlen > 64)
875 return 0;
876
877 r = dhcp_pd_prepare(link);
878 if (r <= 0)
879 return r;
880
881 if (streq_ptr(uplink->dhcp4_6rd_tunnel_name, link->ifname)) {
e49bad01
YW
882 r = dhcp4_pd_request_default_gateway_on_6rd_tunnel(link, &br_addresses[0], lifetime_usec);
883 if (r < 0)
884 return r;
e49bad01
YW
885 }
886
38488bab
YW
887 r = dhcp_pd_assign_subnet_prefix(link, &pd_prefix, pd_prefixlen, lifetime_usec, lifetime_usec, /* is_uplink = */ false);
888 if (r < 0)
889 return r;
890
e49bad01
YW
891 return dhcp_pd_finalize(link);
892}
893
894static int dhcp4_pd_6rd_tunnel_create_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
895 int r;
896
897 assert(m);
898 assert(link);
899 assert(link->manager);
900 assert(link->dhcp4_6rd_tunnel_name);
901
902 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
903 return 0;
904
905 r = sd_netlink_message_get_errno(m);
906 if (r < 0) {
21feba0a 907 log_link_message_warning_errno(link, m, r, "Failed to create tunnel device for DHCPv4 6rd");
e49bad01
YW
908 link_enter_failed(link);
909 return 0;
910 }
911
912 return 0;
913}
914
915int dhcp4_pd_prefix_acquired(Link *uplink) {
916 _cleanup_free_ char *tunnel_name = NULL;
917 uint8_t ipv4masklen, sixrd_prefixlen, pd_prefixlen;
918 struct in6_addr sixrd_prefix, pd_prefix;
919 struct in_addr ipv4address;
920 union in_addr_union server_address;
21feba0a 921 const struct in_addr *br_addresses;
a93538e8 922 usec_t lifetime_usec;
e49bad01
YW
923 Link *link;
924 int r;
925
926 assert(uplink);
def4741b 927 assert(uplink->manager);
e49bad01
YW
928 assert(uplink->dhcp_lease);
929
930 r = sd_dhcp_lease_get_address(uplink->dhcp_lease, &ipv4address);
931 if (r < 0)
932 return log_link_warning_errno(uplink, r, "Failed to get DHCPv4 address: %m");
933
a93538e8 934 r = sd_dhcp_lease_get_lifetime_timestamp(uplink->dhcp_lease, CLOCK_BOOTTIME, &lifetime_usec);
e49bad01
YW
935 if (r < 0)
936 return log_link_warning_errno(uplink, r, "Failed to get lifetime of DHCPv4 lease: %m");
937
e49bad01
YW
938 r = sd_dhcp_lease_get_server_identifier(uplink->dhcp_lease, &server_address.in);
939 if (r < 0)
940 return log_link_warning_errno(uplink, r, "Failed to get server address of DHCPv4 lease: %m");
941
21feba0a 942 r = sd_dhcp_lease_get_6rd(uplink->dhcp_lease, &ipv4masklen, &sixrd_prefixlen, &sixrd_prefix, &br_addresses, NULL);
e49bad01 943 if (r < 0)
21feba0a
YW
944 return log_link_warning_errno(uplink, r, "Failed to get DHCPv4 6rd option: %m");
945
c71384a9 946 if (DEBUG_LOGGING)
21feba0a 947 log_link_debug(uplink, "DHCPv4: 6rd option is acquired: IPv4_masklen=%u, 6rd_prefix=%s, br_address="IPV4_ADDRESS_FMT_STR,
c71384a9
ZJS
948 ipv4masklen,
949 IN6_ADDR_PREFIX_TO_STRING(&sixrd_prefix, sixrd_prefixlen),
950 IPV4_ADDRESS_FMT_VAL(*br_addresses));
e49bad01
YW
951
952 /* Calculate PD prefix */
953 dhcp4_calculate_pd_prefix(&ipv4address, ipv4masklen, &sixrd_prefix, sixrd_prefixlen, &pd_prefix, &pd_prefixlen);
954
955 /* Register and log PD prefix */
956 r = dhcp_pd_prefix_add(uplink, &pd_prefix, pd_prefixlen);
957 if (r < 0)
958 return r;
959
960 /* Request unreachable route */
961 r = dhcp4_request_unreachable_route(uplink, &pd_prefix, pd_prefixlen, lifetime_usec, &server_address);
962 if (r < 0)
963 return r;
964
965 /* Generate 6rd SIT tunnel device name. */
966 r = dhcp4_pd_create_6rd_tunnel_name(uplink, &tunnel_name);
967 if (r < 0)
968 return r;
969
970 /* Remove old tunnel device if exists. */
971 if (!streq_ptr(uplink->dhcp4_6rd_tunnel_name, tunnel_name)) {
972 Link *old_tunnel;
973
974 if (uplink->dhcp4_6rd_tunnel_name &&
975 link_get_by_name(uplink->manager, uplink->dhcp4_6rd_tunnel_name, &old_tunnel) >= 0)
976 (void) link_remove(old_tunnel);
977
978 free_and_replace(uplink->dhcp4_6rd_tunnel_name, tunnel_name);
979 }
980
981 /* Create 6rd SIT tunnel device if it does not exist yet. */
982 if (link_get_by_name(uplink->manager, uplink->dhcp4_6rd_tunnel_name, NULL) < 0) {
983 r = dhcp4_pd_create_6rd_tunnel(uplink, dhcp4_pd_6rd_tunnel_create_handler);
984 if (r < 0)
985 return r;
986 }
987
988 /* Then, assign subnet prefixes to downstream interfaces. */
989 HASHMAP_FOREACH(link, uplink->manager->links_by_index) {
990 if (!dhcp_pd_is_uplink(link, uplink, /* accept_auto = */ true))
991 continue;
992
993 r = dhcp4_pd_assign_subnet_prefix(link, uplink);
994 if (r < 0) {
995 /* When failed on the upstream interface (i.e., the case link == uplink),
996 * immediately abort the assignment of the prefixes. As, the all assigned
997 * prefixes will be dropped soon in link_enter_failed(), and it is meaningless
998 * to continue the assignment. */
999 if (link == uplink)
1000 return r;
1001
1002 link_enter_failed(link);
1003 }
1004 }
1005
1006 return 0;
1007}
1008
a27588d4 1009static int dhcp6_pd_assign_subnet_prefixes(Link *link, Link *uplink) {
d5ebcf65
YW
1010 int r;
1011
5014e660
YW
1012 assert(link);
1013 assert(uplink);
1014 assert(uplink->dhcp6_lease);
d5ebcf65 1015
a27588d4 1016 r = dhcp_pd_prepare(link);
c3cd5351 1017 if (r <= 0)
5014e660 1018 return r;
d5ebcf65 1019
d8ec95c7
YW
1020 FOREACH_DHCP6_PD_PREFIX(uplink->dhcp6_lease) {
1021 usec_t lifetime_preferred_usec, lifetime_valid_usec;
d5ebcf65
YW
1022 struct in6_addr pd_prefix;
1023 uint8_t pd_prefix_len;
1024
d8ec95c7 1025 r = sd_dhcp6_lease_get_pd_prefix(uplink->dhcp6_lease, &pd_prefix, &pd_prefix_len);
d5ebcf65 1026 if (r < 0)
d8ec95c7 1027 return r;
d5ebcf65 1028
5014e660 1029 if (pd_prefix_len > 64)
d5ebcf65
YW
1030 continue;
1031
d5ebcf65
YW
1032 /* Mask prefix for safety. */
1033 r = in6_addr_mask(&pd_prefix, pd_prefix_len);
1034 if (r < 0)
5014e660 1035 return r;
d5ebcf65 1036
d8ec95c7
YW
1037 r = sd_dhcp6_lease_get_pd_lifetime_timestamp(uplink->dhcp6_lease, CLOCK_BOOTTIME,
1038 &lifetime_preferred_usec, &lifetime_valid_usec);
1039 if (r < 0)
1040 return r;
1041
38488bab 1042 r = dhcp_pd_assign_subnet_prefix(link, &pd_prefix, pd_prefix_len,
d8ec95c7 1043 lifetime_preferred_usec, lifetime_valid_usec,
38488bab 1044 /* is_uplink = */ link == uplink);
d5ebcf65
YW
1045 if (r < 0)
1046 return r;
1047 }
1048
a27588d4 1049 return dhcp_pd_finalize(link);
d5ebcf65
YW
1050}
1051
a27588d4 1052int dhcp6_pd_prefix_acquired(Link *uplink) {
5ed10a19 1053 union in_addr_union server_address;
d5ebcf65 1054 Link *link;
d5ebcf65
YW
1055 int r;
1056
a27588d4
YW
1057 assert(uplink);
1058 assert(uplink->dhcp6_lease);
d5ebcf65 1059
a27588d4 1060 r = sd_dhcp6_lease_get_server_address(uplink->dhcp6_lease, &server_address.in6);
5ed10a19 1061 if (r < 0)
a27588d4 1062 return log_link_warning_errno(uplink, r, "Failed to get server address of DHCPv6 lease: %m");
5ed10a19 1063
41664456 1064 /* First, logs acquired prefixes and request unreachable routes. */
d8ec95c7
YW
1065 FOREACH_DHCP6_PD_PREFIX(uplink->dhcp6_lease) {
1066 usec_t lifetime_valid_usec;
d5ebcf65
YW
1067 struct in6_addr pd_prefix;
1068 uint8_t pd_prefix_len;
1069
d8ec95c7 1070 r = sd_dhcp6_lease_get_pd_prefix(uplink->dhcp6_lease, &pd_prefix, &pd_prefix_len);
d5ebcf65 1071 if (r < 0)
d8ec95c7 1072 return r;
d5ebcf65 1073
d5ebcf65
YW
1074 /* Mask prefix for safety. */
1075 r = in6_addr_mask(&pd_prefix, pd_prefix_len);
41664456 1076 if (r < 0)
21feba0a 1077 return log_link_error_errno(uplink, r, "Failed to mask DHCPv6 delegated prefix: %m");
41664456 1078
a27588d4 1079 r = dhcp_pd_prefix_add(uplink, &pd_prefix, pd_prefix_len);
d5ebcf65
YW
1080 if (r < 0)
1081 return r;
1082
d8ec95c7
YW
1083 r = sd_dhcp6_lease_get_pd_lifetime_timestamp(uplink->dhcp6_lease, CLOCK_BOOTTIME,
1084 NULL, &lifetime_valid_usec);
1085 if (r < 0)
1086 return r;
1087
5235d739 1088 r = dhcp6_request_unreachable_route(uplink, &pd_prefix, pd_prefix_len,
d8ec95c7 1089 lifetime_valid_usec, &server_address);
d5ebcf65
YW
1090 if (r < 0)
1091 return r;
1092 }
1093
41664456 1094 /* Then, assign subnet prefixes. */
a27588d4
YW
1095 HASHMAP_FOREACH(link, uplink->manager->links_by_index) {
1096 if (!dhcp_pd_is_uplink(link, uplink, /* accept_auto = */ true))
41664456
YW
1097 continue;
1098
a27588d4 1099 r = dhcp6_pd_assign_subnet_prefixes(link, uplink);
d5ebcf65 1100 if (r < 0) {
a27588d4 1101 /* When failed on the upstream interface (i.e., the case link == uplink),
41664456
YW
1102 * immediately abort the assignment of the prefixes. As, the all assigned
1103 * prefixes will be dropped soon in link_enter_failed(), and it is meaningless
1104 * to continue the assignment. */
a27588d4 1105 if (link == uplink)
d5ebcf65
YW
1106 return r;
1107
1108 link_enter_failed(link);
1109 }
1110 }
d5ebcf65
YW
1111
1112 return 0;
1113}
1114
e49bad01
YW
1115static bool dhcp4_pd_uplink_is_ready(Link *link) {
1116 assert(link);
1117
1118 if (!link->network)
1119 return false;
1120
1121 if (!link->network->dhcp_use_6rd)
1122 return false;
1123
1124 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
1125 return false;
1126
1127 if (!link->dhcp_client)
1128 return false;
1129
1130 if (sd_dhcp_client_is_running(link->dhcp_client) <= 0)
1131 return false;
1132
0ad8d953 1133 return sd_dhcp_lease_has_6rd(link->dhcp_lease);
e49bad01
YW
1134}
1135
d5ebcf65
YW
1136static bool dhcp6_pd_uplink_is_ready(Link *link) {
1137 assert(link);
1138
1139 if (!link->network)
1140 return false;
1141
1142 if (!link->network->dhcp6_use_pd_prefix)
1143 return false;
1144
1145 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
1146 return false;
1147
1148 if (!link->dhcp6_client)
1149 return false;
1150
1151 if (sd_dhcp6_client_is_running(link->dhcp6_client) <= 0)
1152 return false;
1153
fb70992d 1154 return sd_dhcp6_lease_has_pd_prefix(link->dhcp6_lease);
d5ebcf65
YW
1155}
1156
a27588d4 1157int dhcp_pd_find_uplink(Link *link, Link **ret) {
d5ebcf65
YW
1158 Link *uplink = NULL;
1159 int r = 0;
1160
1161 assert(link);
1162 assert(link->manager);
a27588d4 1163 assert(link_dhcp_pd_is_enabled(link));
d5ebcf65
YW
1164 assert(ret);
1165
a27588d4
YW
1166 if (link->network->dhcp_pd_uplink_name)
1167 r = link_get_by_name(link->manager, link->network->dhcp_pd_uplink_name, &uplink);
1168 else if (link->network->dhcp_pd_uplink_index > 0)
1169 r = link_get_by_index(link->manager, link->network->dhcp_pd_uplink_index, &uplink);
1170 else if (link->network->dhcp_pd_uplink_index == UPLINK_INDEX_SELF)
d5ebcf65
YW
1171 uplink = link;
1172 if (r < 0)
1173 return r;
1174
1175 if (uplink) {
e49bad01
YW
1176 if (dhcp4_pd_uplink_is_ready(uplink)) {
1177 *ret = uplink;
1178 return AF_INET;
1179 }
d5ebcf65 1180
e49bad01
YW
1181 if (dhcp6_pd_uplink_is_ready(uplink)) {
1182 *ret = uplink;
1183 return AF_INET6;
1184 }
1185
1186 return -EBUSY;
d5ebcf65
YW
1187 }
1188
1189 HASHMAP_FOREACH(uplink, link->manager->links_by_index) {
d5ebcf65 1190 /* Assume that there exists at most one link which acquired delegated prefixes. */
e49bad01
YW
1191 if (dhcp4_pd_uplink_is_ready(uplink)) {
1192 *ret = uplink;
1193 return AF_INET;
1194 }
1195
1196 if (dhcp6_pd_uplink_is_ready(uplink)) {
1197 *ret = uplink;
1198 return AF_INET6;
1199 }
d5ebcf65
YW
1200 }
1201
1202 return -ENODEV;
1203}
1204
a27588d4 1205int dhcp_request_prefix_delegation(Link *link) {
d5ebcf65 1206 Link *uplink;
e49bad01 1207 int r;
d5ebcf65
YW
1208
1209 assert(link);
1210
a27588d4 1211 if (!link_dhcp_pd_is_enabled(link))
d5ebcf65
YW
1212 return 0;
1213
e49bad01
YW
1214 r = dhcp_pd_find_uplink(link, &uplink);
1215 if (r < 0)
d5ebcf65
YW
1216 return 0;
1217
e49bad01
YW
1218 log_link_debug(link, "Requesting subnets of delegated prefixes acquired by DHCPv%c client on %s",
1219 r == AF_INET ? '4' : '6', uplink->ifname);
1220
1221 return r == AF_INET ?
1222 dhcp4_pd_assign_subnet_prefix(link, uplink) :
1223 dhcp6_pd_assign_subnet_prefixes(link, uplink);
d5ebcf65
YW
1224}
1225
a27588d4 1226int config_parse_dhcp_pd_subnet_id(
d5ebcf65
YW
1227 const char *unit,
1228 const char *filename,
1229 unsigned line,
1230 const char *section,
1231 unsigned section_line,
1232 const char *lvalue,
1233 int ltype,
1234 const char *rvalue,
1235 void *data,
1236 void *userdata) {
1237
99534007 1238 int64_t *p = ASSERT_PTR(data);
d5ebcf65
YW
1239 uint64_t t;
1240 int r;
1241
1242 assert(filename);
1243 assert(lvalue);
1244 assert(rvalue);
d5ebcf65
YW
1245
1246 if (isempty(rvalue) || streq(rvalue, "auto")) {
1247 *p = -1;
1248 return 0;
1249 }
1250
1251 r = safe_atoux64(rvalue, &t);
1252 if (r < 0) {
1253 log_syntax(unit, LOG_WARNING, filename, line, r,
1254 "Failed to parse %s=, ignoring assignment: %s",
1255 lvalue, rvalue);
1256 return 0;
1257 }
1258 if (t > INT64_MAX) {
1259 log_syntax(unit, LOG_WARNING, filename, line, 0,
1260 "Invalid subnet id '%s', ignoring assignment.",
1261 rvalue);
1262 return 0;
1263 }
1264
1265 *p = (int64_t) t;
1266
1267 return 0;
1268}