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