]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-dhcp-prefix-delegation.c
network: dhcp-pd: rename [DHCPv6PrefixDelegation] -> [DHCPPrefixDelegation]
[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"
18#include "parse-util.h"
19#include "string-util.h"
20#include "strv.h"
21
a27588d4 22bool link_dhcp_pd_is_enabled(Link *link) {
d5ebcf65
YW
23 assert(link);
24
25 if (!link->network)
26 return false;
27
a27588d4 28 return link->network->dhcp_pd;
d5ebcf65
YW
29}
30
a27588d4 31bool dhcp_pd_is_uplink(Link *link, Link *target, bool accept_auto) {
d5ebcf65
YW
32 assert(link);
33 assert(target);
34
a27588d4 35 if (!link_dhcp_pd_is_enabled(link))
d5ebcf65
YW
36 return false;
37
a27588d4
YW
38 if (link->network->dhcp_pd_uplink_name)
39 return streq_ptr(target->ifname, link->network->dhcp_pd_uplink_name) ||
40 strv_contains(target->alternative_names, link->network->dhcp_pd_uplink_name);
d5ebcf65 41
a27588d4
YW
42 if (link->network->dhcp_pd_uplink_index > 0)
43 return target->ifindex == link->network->dhcp_pd_uplink_index;
d5ebcf65 44
a27588d4 45 if (link->network->dhcp_pd_uplink_index == UPLINK_INDEX_SELF)
d5ebcf65
YW
46 return link == target;
47
a27588d4 48 assert(link->network->dhcp_pd_uplink_index == UPLINK_INDEX_AUTO);
d5ebcf65
YW
49 return accept_auto;
50}
51
52bool dhcp6_lease_has_pd_prefix(sd_dhcp6_lease *lease) {
53 uint32_t lifetime_preferred_sec, lifetime_valid_sec;
54 struct in6_addr pd_prefix;
55 uint8_t pd_prefix_len;
56
57 if (!lease)
58 return false;
59
60 sd_dhcp6_lease_reset_pd_prefix_iter(lease);
61
62 return sd_dhcp6_lease_get_pd(lease, &pd_prefix, &pd_prefix_len, &lifetime_preferred_sec, &lifetime_valid_sec) >= 0;
63}
64
a27588d4 65static void link_remove_dhcp_pd_subnet_prefix(Link *link, const struct in6_addr *prefix) {
d5ebcf65
YW
66 void *key;
67
68 assert(link);
69 assert(link->manager);
70 assert(prefix);
71
a27588d4 72 if (hashmap_get(link->manager->links_by_dhcp_pd_subnet_prefix, prefix) != link)
d5ebcf65
YW
73 return;
74
a27588d4 75 hashmap_remove2(link->manager->links_by_dhcp_pd_subnet_prefix, prefix, &key);
d5ebcf65
YW
76 free(key);
77}
78
a27588d4 79static int link_add_dhcp_pd_subnet_prefix(Link *link, const struct in6_addr *prefix) {
d5ebcf65
YW
80 _cleanup_free_ struct in6_addr *copy = NULL;
81 int r;
82
83 assert(link);
84 assert(prefix);
85
86 copy = newdup(struct in6_addr, prefix, 1);
87 if (!copy)
88 return -ENOMEM;
89
a27588d4 90 r = hashmap_ensure_put(&link->manager->links_by_dhcp_pd_subnet_prefix, &in6_addr_hash_ops_free, copy, link);
d5ebcf65
YW
91 if (r < 0)
92 return r;
93 if (r > 0)
94 TAKE_PTR(copy);
95
96 return 0;
97}
98
a27588d4 99static int link_get_by_dhcp_pd_subnet_prefix(Manager *manager, const struct in6_addr *prefix, Link **ret) {
d5ebcf65
YW
100 Link *link;
101
102 assert(manager);
103 assert(prefix);
104
a27588d4 105 link = hashmap_get(manager->links_by_dhcp_pd_subnet_prefix, prefix);
d5ebcf65
YW
106 if (!link)
107 return -ENODEV;
108
109 if (ret)
110 *ret = link;
111 return 0;
112}
113
a27588d4 114static 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
115 assert(link);
116 assert(pd_prefix);
117
a27588d4 118 if (!link_dhcp_pd_is_enabled(link))
d5ebcf65
YW
119 return -ENOENT;
120
a27588d4 121 if (link->network->dhcp_pd_assign) {
d5ebcf65
YW
122 Address *address;
123
124 SET_FOREACH(address, link->addresses) {
a27588d4 125 if (address->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65
YW
126 continue;
127 assert(address->family == AF_INET6);
128
129 if (in6_addr_prefix_covers(pd_prefix, pd_prefix_len, &address->in_addr.in6) <= 0)
130 continue;
131
132 if (ret) {
133 struct in6_addr prefix = address->in_addr.in6;
134
135 in6_addr_mask(&prefix, 64);
136 *ret = prefix;
137 }
138 return 0;
139 }
140 } else {
141 Route *route;
142
143 SET_FOREACH(route, link->routes) {
a27588d4 144 if (route->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65
YW
145 continue;
146 assert(route->family == AF_INET6);
147
148 if (in6_addr_prefix_covers(pd_prefix, pd_prefix_len, &route->dst.in6) > 0) {
149 if (ret)
150 *ret = route->dst.in6;
151 return 0;
152 }
153 }
154 }
155
156 return -ENOENT;
157}
158
a27588d4 159int dhcp_pd_remove(Link *link, bool only_marked) {
d5ebcf65
YW
160 int k, r = 0;
161
162 assert(link);
163 assert(link->manager);
164
a27588d4 165 if (!link_dhcp_pd_is_enabled(link))
d5ebcf65
YW
166 return 0;
167
168 if (!only_marked)
a27588d4 169 link->dhcp_pd_configured = false;
d5ebcf65 170
a27588d4 171 if (!link->network->dhcp_pd_assign) {
d5ebcf65
YW
172 Route *route;
173
174 SET_FOREACH(route, link->routes) {
a27588d4 175 if (route->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65
YW
176 continue;
177 if (only_marked && !route_is_marked(route))
178 continue;
179
180 if (link->radv)
181 (void) sd_radv_remove_prefix(link->radv, &route->dst.in6, 64);
182
a27588d4 183 link_remove_dhcp_pd_subnet_prefix(link, &route->dst.in6);
d5ebcf65
YW
184
185 k = route_remove(route);
186 if (k < 0)
187 r = k;
188
95eb38c8 189 route_cancel_request(route, link);
d5ebcf65
YW
190 }
191 } else {
192 Address *address;
193
194 SET_FOREACH(address, link->addresses) {
195 struct in6_addr prefix;
196
a27588d4 197 if (address->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65
YW
198 continue;
199 if (only_marked && !address_is_marked(address))
200 continue;
201
202 prefix = address->in_addr.in6;
203 in6_addr_mask(&prefix, 64);
204
205 if (link->radv)
206 (void) sd_radv_remove_prefix(link->radv, &prefix, 64);
207
a27588d4 208 link_remove_dhcp_pd_subnet_prefix(link, &prefix);
d5ebcf65
YW
209
210 k = address_remove(address);
211 if (k < 0)
212 r = k;
213
214 address_cancel_request(address);
215 }
216 }
217
218 return r;
219}
220
a27588d4 221static int dhcp_pd_check_ready(Link *link);
d5ebcf65 222
a27588d4 223static int dhcp_pd_address_ready_callback(Address *address) {
d5ebcf65
YW
224 Address *a;
225
226 assert(address);
227 assert(address->link);
228
229 SET_FOREACH(a, address->link->addresses)
a27588d4 230 if (a->source == NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65
YW
231 a->callback = NULL;
232
a27588d4 233 return dhcp_pd_check_ready(address->link);
d5ebcf65
YW
234}
235
a27588d4 236static int dhcp_pd_check_ready(Link *link) {
d5ebcf65
YW
237 int r;
238
239 assert(link);
240 assert(link->network);
241
a27588d4
YW
242 if (link->dhcp_pd_messages > 0) {
243 log_link_debug(link, "%s(): DHCP-PD addresses and routes are not set.", __func__);
d5ebcf65
YW
244 return 0;
245 }
246
a27588d4 247 if (link->network->dhcp_pd_assign) {
d5ebcf65
YW
248 bool has_ready = false;
249 Address *address;
250
251 SET_FOREACH(address, link->addresses) {
a27588d4 252 if (address->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
d5ebcf65
YW
253 continue;
254 if (address_is_ready(address)) {
255 has_ready = true;
256 break;
257 }
258 }
259
260 if (!has_ready) {
261 SET_FOREACH(address, link->addresses)
a27588d4
YW
262 if (address->source == NETWORK_CONFIG_SOURCE_DHCP_PD)
263 address->callback = dhcp_pd_address_ready_callback;
d5ebcf65 264
a27588d4 265 log_link_debug(link, "%s(): no DHCP-PD address is ready.", __func__);
d5ebcf65
YW
266 return 0;
267 }
268 }
269
a27588d4 270 link->dhcp_pd_configured = true;
d5ebcf65 271
a27588d4 272 log_link_debug(link, "DHCP-PD addresses and routes set.");
d5ebcf65 273
a27588d4 274 r = dhcp_pd_remove(link, /* only_marked = */ true);
d5ebcf65
YW
275 if (r < 0)
276 return r;
277
278 link_check_ready(link);
279 return 1;
280}
281
a27588d4 282static int dhcp_pd_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
d5ebcf65
YW
283 int r;
284
285 assert(link);
a27588d4 286 assert(link->dhcp_pd_messages > 0);
d5ebcf65 287
a27588d4 288 link->dhcp_pd_messages--;
d5ebcf65 289
a27588d4 290 r = route_configure_handler_internal(rtnl, m, link, "Failed to add prefix route for DHCP delegated subnet prefix");
d5ebcf65
YW
291 if (r <= 0)
292 return r;
293
a27588d4 294 r = dhcp_pd_check_ready(link);
d5ebcf65
YW
295 if (r < 0)
296 link_enter_failed(link);
297
298 return 1;
299}
300
a27588d4 301static int dhcp_pd_request_route(Link *link, const struct in6_addr *prefix, uint8_t prefixlen, usec_t lifetime_usec) {
d5ebcf65
YW
302 _cleanup_(route_freep) Route *route = NULL;
303 Route *existing;
304 int r;
305
306 assert(link);
307 assert(link->network);
308 assert(prefix);
309
a27588d4 310 if (link->network->dhcp_pd_assign)
d5ebcf65
YW
311 return 0;
312
313 r = route_new(&route);
314 if (r < 0)
315 return r;
316
a27588d4 317 route->source = NETWORK_CONFIG_SOURCE_DHCP_PD;
d5ebcf65
YW
318 route->family = AF_INET6;
319 route->dst.in6 = *prefix;
ab0c82d9 320 route->dst_prefixlen = prefixlen;
d5ebcf65 321 route->protocol = RTPROT_DHCP;
a27588d4 322 route->priority = link->network->dhcp_pd_route_metric;
d5ebcf65
YW
323 route->lifetime_usec = lifetime_usec;
324
325 if (route_get(NULL, link, route, &existing) < 0)
a27588d4 326 link->dhcp_pd_configured = false;
d5ebcf65
YW
327 else
328 route_unmark(existing);
329
a27588d4
YW
330 r = link_request_route(link, TAKE_PTR(route), true, &link->dhcp_pd_messages,
331 dhcp_pd_route_handler, NULL);
d5ebcf65 332 if (r < 0)
a27588d4 333 return log_link_error_errno(link, r, "Failed to request DHCP-PD prefix route: %m");
d5ebcf65
YW
334
335 return 0;
336}
337
a27588d4 338static int dhcp_pd_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
d5ebcf65
YW
339 int r;
340
341 assert(link);
a27588d4 342 assert(link->dhcp_pd_messages > 0);
d5ebcf65 343
a27588d4 344 link->dhcp_pd_messages--;
d5ebcf65 345
a27588d4 346 r = address_configure_handler_internal(rtnl, m, link, "Could not set DHCP-PD address");
d5ebcf65
YW
347 if (r <= 0)
348 return r;
349
a27588d4 350 r = dhcp_pd_check_ready(link);
d5ebcf65
YW
351 if (r < 0)
352 link_enter_failed(link);
353
354 return 1;
355}
356
a27588d4 357static void log_dhcp_pd_address(Link *link, const Address *address) {
d5ebcf65
YW
358 _cleanup_free_ char *buffer = NULL;
359 int log_level;
360
361 assert(address);
362 assert(address->family == AF_INET6);
363
364 log_level = address_get(link, address, NULL) >= 0 ? LOG_DEBUG : LOG_INFO;
365
366 if (log_level < log_get_max_level())
367 return;
368
369 (void) in6_addr_prefix_to_string(&address->in_addr.in6, address->prefixlen, &buffer);
370
a27588d4 371 log_link_full(link, log_level, "DHCP-PD address %s (valid %s, preferred %s)",
d5ebcf65
YW
372 strna(buffer),
373 FORMAT_LIFETIME(address->lifetime_valid_usec),
374 FORMAT_LIFETIME(address->lifetime_preferred_usec));
375}
376
a27588d4 377static int dhcp_pd_request_address(
d5ebcf65
YW
378 Link *link,
379 const struct in6_addr *prefix,
ab0c82d9 380 uint8_t prefixlen,
d5ebcf65
YW
381 usec_t lifetime_preferred_usec,
382 usec_t lifetime_valid_usec) {
383
384 _cleanup_set_free_ Set *addresses = NULL;
385 struct in6_addr *a;
386 int r;
387
388 assert(link);
389 assert(link->network);
390 assert(prefix);
391
a27588d4 392 if (!link->network->dhcp_pd_assign)
d5ebcf65
YW
393 return 0;
394
a27588d4 395 r = dhcp_pd_generate_addresses(link, prefix, prefixlen, &addresses);
d5ebcf65 396 if (r < 0)
a27588d4 397 return log_link_warning_errno(link, r, "Failed to generate addresses for acquired DHCP delegated prefix: %m");
d5ebcf65
YW
398
399 SET_FOREACH(a, addresses) {
400 _cleanup_(address_freep) Address *address = NULL;
401 Address *existing;
402
403 r = address_new(&address);
404 if (r < 0)
a27588d4 405 return log_link_error_errno(link, r, "Failed to allocate address for DHCP delegated prefix: %m");
d5ebcf65 406
a27588d4 407 address->source = NETWORK_CONFIG_SOURCE_DHCP_PD;
d5ebcf65
YW
408 address->family = AF_INET6;
409 address->in_addr.in6 = *a;
ab0c82d9 410 address->prefixlen = prefixlen;
d5ebcf65
YW
411 address->lifetime_preferred_usec = lifetime_preferred_usec;
412 address->lifetime_valid_usec = lifetime_valid_usec;
ab0c82d9 413 if (prefixlen == 64)
a27588d4
YW
414 SET_FLAG(address->flags, IFA_F_MANAGETEMPADDR, link->network->dhcp_pd_manage_temporary_address);
415 address->route_metric = link->network->dhcp_pd_route_metric;
d5ebcf65 416
a27588d4 417 log_dhcp_pd_address(link, address);
d5ebcf65
YW
418
419 if (address_get(link, address, &existing) < 0)
a27588d4 420 link->dhcp_pd_configured = false;
d5ebcf65
YW
421 else
422 address_unmark(existing);
423
a27588d4
YW
424 r = link_request_address(link, TAKE_PTR(address), true, &link->dhcp_pd_messages,
425 dhcp_pd_address_handler, NULL);
d5ebcf65 426 if (r < 0)
a27588d4 427 return log_link_error_errno(link, r, "Failed to request DHCP delegated prefix address: %m");
d5ebcf65
YW
428 }
429
430 return 0;
431}
432
a27588d4 433static int dhcp_pd_calculate_subnet_prefix(
d5ebcf65
YW
434 const struct in6_addr *pd_prefix,
435 uint8_t pd_prefix_len,
436 uint64_t subnet_id,
437 struct in6_addr *ret) {
438
439 struct in6_addr prefix;
440
441 assert(pd_prefix);
442 assert(pd_prefix_len <= 64);
443 assert(ret);
444
445 if (subnet_id >= UINT64_C(1) << (64 - pd_prefix_len))
446 return -ERANGE;
447
448 prefix = *pd_prefix;
449
450 if (pd_prefix_len < 32)
451 prefix.s6_addr32[0] |= htobe32(subnet_id >> 32);
452
453 prefix.s6_addr32[1] |= htobe32(subnet_id & 0xffffffff);
454
455 *ret = prefix;
456 return 0;
457}
458
a27588d4 459static int dhcp_pd_get_preferred_subnet_prefix(
d5ebcf65
YW
460 Link *link,
461 const struct in6_addr *pd_prefix,
462 uint8_t pd_prefix_len,
463 struct in6_addr *ret) {
464
465 struct in6_addr prefix;
466 Link *assigned_link;
467 int r;
468
469 assert(link);
470 assert(link->manager);
11d8a83f 471 assert(link->network);
d5ebcf65
YW
472 assert(pd_prefix);
473
a27588d4 474 if (link->network->dhcp_pd_subnet_id >= 0) {
d5ebcf65
YW
475 /* If the link has a preference for a particular subnet id try to allocate that */
476
a27588d4 477 r = dhcp_pd_calculate_subnet_prefix(pd_prefix, pd_prefix_len, link->network->dhcp_pd_subnet_id, &prefix);
d5ebcf65
YW
478 if (r < 0)
479 return log_link_warning_errno(link, r,
480 "subnet id %" PRIu64 " is out of range. Only have %" PRIu64 " subnets.",
a27588d4 481 link->network->dhcp_pd_subnet_id, UINT64_C(1) << (64 - pd_prefix_len));
d5ebcf65 482
a27588d4 483 if (link_get_by_dhcp_pd_subnet_prefix(link->manager, &prefix, &assigned_link) >= 0 &&
d5ebcf65
YW
484 assigned_link != link) {
485 _cleanup_free_ char *assigned_buf = NULL;
486
487 (void) in6_addr_to_string(&prefix, &assigned_buf);
488 return log_link_warning_errno(link, SYNTHETIC_ERRNO(EAGAIN),
489 "The requested prefix %s is already assigned to another link.",
490 strna(assigned_buf));
491 }
492
493 *ret = prefix;
494 return 0;
495 }
496
497 for (uint64_t n = 0; ; n++) {
498 /* If we do not have an allocation preference just iterate
499 * through the address space and return the first free prefix. */
500
a27588d4 501 r = dhcp_pd_calculate_subnet_prefix(pd_prefix, pd_prefix_len, n, &prefix);
d5ebcf65
YW
502 if (r < 0)
503 return log_link_warning_errno(link, r,
504 "Couldn't find a suitable prefix. Ran out of address space.");
505
506 /* Do not use explicitly requested subnet IDs. Note that the corresponding link may not
507 * appear yet. So, we need to check the ID is not used in any .network files. */
a27588d4 508 if (set_contains(link->manager->dhcp_pd_subnet_ids, &n))
d5ebcf65
YW
509 continue;
510
511 /* Check that the prefix is not assigned to another link. */
a27588d4 512 if (link_get_by_dhcp_pd_subnet_prefix(link->manager, &prefix, &assigned_link) < 0 ||
d5ebcf65
YW
513 assigned_link == link) {
514 *ret = prefix;
515 return 0;
516 }
517 }
518}
519
a27588d4 520static int dhcp_pd_assign_subnet_prefix(
d5ebcf65
YW
521 Link *link,
522 const struct in6_addr *pd_prefix,
523 uint8_t pd_prefix_len,
524 usec_t lifetime_preferred_usec,
525 usec_t lifetime_valid_usec) {
526
527 _cleanup_free_ char *buf = NULL;
528 struct in6_addr prefix;
529 int r;
530
531 assert(link);
532 assert(link->network);
533 assert(pd_prefix);
534
a27588d4
YW
535 if (dhcp_pd_get_assigned_subnet_prefix(link, pd_prefix, pd_prefix_len, &prefix) < 0 &&
536 dhcp_pd_get_preferred_subnet_prefix(link, pd_prefix, pd_prefix_len, &prefix) < 0)
d5ebcf65
YW
537 return 0;
538
539 (void) in6_addr_prefix_to_string(&prefix, 64, &buf);
540
a27588d4 541 if (link_radv_enabled(link) && link->network->dhcp_pd_announce) {
d5ebcf65
YW
542 r = radv_add_prefix(link, &prefix, 64, lifetime_preferred_usec, lifetime_valid_usec);
543 if (r < 0)
544 return log_link_warning_errno(link, r,
545 "Failed to assign/update prefix %s to IPv6 Router Advertisement: %m",
546 strna(buf));
547 }
548
a27588d4 549 r = dhcp_pd_request_route(link, &prefix, 64, lifetime_valid_usec);
d5ebcf65
YW
550 if (r < 0)
551 return log_link_warning_errno(link, r,
552 "Failed to assign/update route for prefix %s: %m",
553 strna(buf));
554
a27588d4 555 r = dhcp_pd_request_address(link, &prefix, 64, lifetime_preferred_usec, lifetime_valid_usec);
d5ebcf65
YW
556 if (r < 0)
557 return log_link_warning_errno(link, r,
558 "Failed to assign/update address for prefix %s: %m",
559 strna(buf));
560
a27588d4 561 r = link_add_dhcp_pd_subnet_prefix(link, &prefix);
d5ebcf65
YW
562 if (r < 0)
563 return log_link_warning_errno(link, r,
564 "Failed to save assigned prefix %s: %m",
565 strna(buf));
566
567 log_link_debug(link, "Assigned prefix %s", strna(buf));
568 return 1;
569}
570
a27588d4 571static int dhcp_pd_assign_prefix_on_uplink(
ab0c82d9
YW
572 Link *link,
573 const struct in6_addr *pd_prefix,
574 uint8_t pd_prefix_len,
575 usec_t lifetime_preferred_usec,
576 usec_t lifetime_valid_usec) {
577
578 _cleanup_free_ char *buf = NULL;
579 int r;
580
581 assert(link);
582 assert(link->network);
583 assert(pd_prefix);
584
585 (void) in6_addr_prefix_to_string(pd_prefix, pd_prefix_len, &buf);
586
a27588d4 587 if (link->network->dhcp_pd_announce)
ab0c82d9
YW
588 log_link_debug(link, "Ignoring Announce= setting on upstream interface.");
589
a27588d4 590 r = dhcp_pd_request_route(link, pd_prefix, pd_prefix_len, lifetime_valid_usec);
ab0c82d9
YW
591 if (r < 0)
592 return log_link_warning_errno(link, r,
593 "Failed to assign/update route for prefix %s: %m",
594 strna(buf));
595
a27588d4 596 r = dhcp_pd_request_address(link, pd_prefix, pd_prefix_len, lifetime_preferred_usec, lifetime_valid_usec);
ab0c82d9
YW
597 if (r < 0)
598 return log_link_warning_errno(link, r,
599 "Failed to assign/update address for prefix %s: %m",
600 strna(buf));
601
602 log_link_debug(link, "Assigned prefix %s", strna(buf));
603 return 1;
604}
605
a27588d4 606static int dhcp_pd_prepare(Link *link) {
d5ebcf65
YW
607 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
608 return 0;
609
a27588d4 610 if (!link_dhcp_pd_is_enabled(link))
d5ebcf65
YW
611 return 0;
612
a27588d4 613 if (link_radv_enabled(link) && link->network->dhcp_pd_announce && !link->radv)
d5ebcf65
YW
614 return 0;
615
a27588d4
YW
616 link_mark_addresses(link, NETWORK_CONFIG_SOURCE_DHCP_PD, NULL);
617 link_mark_routes(link, NETWORK_CONFIG_SOURCE_DHCP_PD, NULL);
d5ebcf65 618
c3cd5351 619 return 1;
d5ebcf65
YW
620}
621
a27588d4 622static int dhcp_pd_finalize(Link *link) {
d5ebcf65
YW
623 int r;
624
625 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
626 return 0;
627
a27588d4
YW
628 if (link->dhcp_pd_messages == 0) {
629 link->dhcp_pd_configured = false;
d5ebcf65 630
a27588d4 631 r = dhcp_pd_remove(link, /* only_marked = */ true);
d5ebcf65
YW
632 if (r < 0)
633 return r;
634 }
635
a27588d4 636 if (!link->dhcp_pd_configured)
d5ebcf65
YW
637 link_set_state(link, LINK_STATE_CONFIGURING);
638
639 link_check_ready(link);
640 return 0;
641}
642
a27588d4 643void dhcp_pd_prefix_lost(Link *uplink) {
da10d2d5 644 Route *route;
d5ebcf65
YW
645 Link *link;
646 int r;
647
a27588d4
YW
648 assert(uplink);
649 assert(uplink->manager);
d5ebcf65 650
a27588d4
YW
651 HASHMAP_FOREACH(link, uplink->manager->links_by_index) {
652 if (!dhcp_pd_is_uplink(link, uplink, /* accept_auto = */ true))
d5ebcf65
YW
653 continue;
654
a27588d4 655 r = dhcp_pd_remove(link, /* only_marked = */ false);
d5ebcf65
YW
656 if (r < 0)
657 link_enter_failed(link);
658 }
659
a27588d4
YW
660 SET_FOREACH(route, uplink->manager->routes) {
661 if (IN_SET(route->source, NETWORK_CONFIG_SOURCE_DHCP4, NETWORK_CONFIG_SOURCE_DHCP6))
da10d2d5
YW
662 continue;
663 if (route->family != AF_INET6)
664 continue;
665 if (route->type != RTN_UNREACHABLE)
666 continue;
a27588d4 667 if (!set_contains(uplink->dhcp_pd_prefixes,
da10d2d5
YW
668 &(struct in_addr_prefix) {
669 .family = AF_INET6,
670 .prefixlen = route->dst_prefixlen,
671 .address = route->dst }))
672 continue;
673
674 (void) route_remove(route);
675
a27588d4 676 route_cancel_request(route, uplink);
da10d2d5
YW
677 }
678
a27588d4 679 set_clear(uplink->dhcp_pd_prefixes);
d5ebcf65
YW
680}
681
a27588d4 682static int dhcp6_unreachable_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
d5ebcf65
YW
683 int r;
684
685 assert(link);
686 assert(link->dhcp6_messages > 0);
687
688 link->dhcp6_messages--;
689
a27588d4 690 r = route_configure_handler_internal(rtnl, m, link, "Failed to set unreachable route for DHCP delegated prefix");
d5ebcf65
YW
691 if (r <= 0)
692 return r;
693
694 r = dhcp6_check_ready(link);
695 if (r < 0)
696 link_enter_failed(link);
697
698 return 1;
699}
700
a27588d4 701static int dhcp_request_unreachable_route(
5ed10a19
YW
702 Link *link,
703 const struct in6_addr *addr,
704 uint8_t prefixlen,
705 usec_t lifetime_usec,
a27588d4
YW
706 NetworkConfigSource source,
707 const union in_addr_union *server_address,
708 unsigned *counter,
709 link_netlink_message_handler_t callback) {
5ed10a19 710
d5ebcf65 711 _cleanup_(route_freep) Route *route = NULL;
d5ebcf65
YW
712 Route *existing;
713 int r;
714
715 assert(link);
716 assert(addr);
a27588d4 717 assert(IN_SET(source, NETWORK_CONFIG_SOURCE_DHCP4, NETWORK_CONFIG_SOURCE_DHCP6));
5ed10a19 718 assert(server_address);
a27588d4
YW
719 assert(counter);
720 assert(callback);
d5ebcf65 721
a536ec38
YW
722 if (prefixlen >= 64) {
723 _cleanup_free_ char *buf = NULL;
d5ebcf65 724
a536ec38 725 (void) in6_addr_prefix_to_string(addr, prefixlen, &buf);
a27588d4 726 log_link_debug(link, "Not adding a blocking route for DHCP delegated prefix %s since the prefix has length >= 64.",
d5ebcf65
YW
727 strna(buf));
728 return 0;
729 }
730
731 r = route_new(&route);
732 if (r < 0)
733 return log_oom();
734
a27588d4 735 route->source = source;
5ed10a19 736 route->provider = *server_address;
d5ebcf65
YW
737 route->family = AF_INET6;
738 route->dst.in6 = *addr;
739 route->dst_prefixlen = prefixlen;
d5ebcf65
YW
740 route->type = RTN_UNREACHABLE;
741 route->protocol = RTPROT_DHCP;
557e1b52 742 route->priority = IP6_RT_PRIO_USER;
d5ebcf65
YW
743 route->lifetime_usec = lifetime_usec;
744
745 if (route_get(link->manager, NULL, route, &existing) < 0)
746 link->dhcp6_configured = false;
747 else
748 route_unmark(existing);
749
a27588d4 750 r = link_request_route(link, TAKE_PTR(route), true, counter, callback, NULL);
a536ec38
YW
751 if (r < 0) {
752 _cleanup_free_ char *buf = NULL;
753
754 (void) in6_addr_prefix_to_string(addr, prefixlen, &buf);
a27588d4 755 return log_link_error_errno(link, r, "Failed to request unreachable route for DHCP delegated prefix %s: %m",
d5ebcf65 756 strna(buf));
a536ec38 757 }
d5ebcf65
YW
758
759 return 0;
760}
761
a27588d4
YW
762static int dhcp6_request_unreachable_route(
763 Link *link,
764 const struct in6_addr *addr,
765 uint8_t prefixlen,
766 usec_t lifetime_usec,
767 const union in_addr_union *server_address) {
768
769 return dhcp_request_unreachable_route(link, addr, prefixlen, lifetime_usec,
770 NETWORK_CONFIG_SOURCE_DHCP6, server_address,
771 &link->dhcp6_messages, dhcp6_unreachable_route_handler);
772}
773
774static int dhcp_pd_prefix_add(Link *link, const struct in6_addr *prefix, uint8_t prefixlen) {
d5ebcf65
YW
775 _cleanup_free_ char *buf = NULL;
776 struct in_addr_prefix *p;
777 int r;
778
779 assert(link);
780 assert(prefix);
781
782 p = new(struct in_addr_prefix, 1);
783 if (!p)
784 return log_oom();
785
786 *p = (struct in_addr_prefix) {
787 .family = AF_INET6,
788 .prefixlen = prefixlen,
789 .address.in6 = *prefix,
790 };
791
792 (void) in6_addr_prefix_to_string(prefix, prefixlen, &buf);
793
794 log_link_full(link,
a27588d4 795 set_contains(link->dhcp_pd_prefixes, p) ? LOG_DEBUG :
d5ebcf65 796 prefixlen > 64 || prefixlen < 48 ? LOG_WARNING : LOG_INFO,
a27588d4 797 "DHCP: received PD Prefix %s%s",
d5ebcf65
YW
798 strna(buf),
799 prefixlen > 64 ? " with prefix length > 64, ignoring." :
800 prefixlen < 48 ? " with prefix length < 48, looks unusual.": "");
801
802 /* Store PD prefix even if prefixlen > 64, not to make logged at warning level so frequently. */
a27588d4 803 r = set_ensure_consume(&link->dhcp_pd_prefixes, &in_addr_prefix_hash_ops_free, p);
d5ebcf65 804 if (r < 0)
a27588d4 805 return log_link_error_errno(link, r, "Failed to store DHCP PD prefix %s: %m", strna(buf));
d5ebcf65 806
a536ec38 807 return 0;
d5ebcf65
YW
808}
809
a27588d4 810static int dhcp6_pd_assign_subnet_prefixes(Link *link, Link *uplink) {
d5ebcf65 811 usec_t timestamp_usec;
d5ebcf65
YW
812 int r;
813
5014e660
YW
814 assert(link);
815 assert(uplink);
816 assert(uplink->dhcp6_lease);
d5ebcf65 817
a27588d4 818 r = dhcp_pd_prepare(link);
c3cd5351 819 if (r <= 0)
5014e660 820 return r;
d5ebcf65 821
c3cd5351 822 r = sd_dhcp6_lease_get_timestamp(uplink->dhcp6_lease, clock_boottime_or_monotonic(), &timestamp_usec);
5014e660
YW
823 if (r < 0)
824 return r;
d5ebcf65 825
5014e660 826 for (sd_dhcp6_lease_reset_pd_prefix_iter(uplink->dhcp6_lease);;) {
d5ebcf65
YW
827 uint32_t lifetime_preferred_sec, lifetime_valid_sec;
828 usec_t lifetime_preferred_usec, lifetime_valid_usec;
829 struct in6_addr pd_prefix;
830 uint8_t pd_prefix_len;
831
5014e660 832 r = sd_dhcp6_lease_get_pd(uplink->dhcp6_lease, &pd_prefix, &pd_prefix_len,
d5ebcf65
YW
833 &lifetime_preferred_sec, &lifetime_valid_sec);
834 if (r < 0)
835 break;
836
5014e660 837 if (pd_prefix_len > 64)
d5ebcf65
YW
838 continue;
839
d5ebcf65
YW
840 /* Mask prefix for safety. */
841 r = in6_addr_mask(&pd_prefix, pd_prefix_len);
842 if (r < 0)
5014e660 843 return r;
d5ebcf65 844
21cf8e9e
YW
845 lifetime_preferred_usec = usec_add(lifetime_preferred_sec * USEC_PER_SEC, timestamp_usec);
846 lifetime_valid_usec = usec_add(lifetime_valid_sec * USEC_PER_SEC, timestamp_usec);
d5ebcf65 847
ab0c82d9 848 if (link == uplink)
a27588d4 849 r = dhcp_pd_assign_prefix_on_uplink(link, &pd_prefix, pd_prefix_len, lifetime_preferred_usec, lifetime_valid_usec);
ab0c82d9 850 else
a27588d4 851 r = dhcp_pd_assign_subnet_prefix(link, &pd_prefix, pd_prefix_len, lifetime_preferred_usec, lifetime_valid_usec);
d5ebcf65
YW
852 if (r < 0)
853 return r;
854 }
855
a27588d4 856 return dhcp_pd_finalize(link);
d5ebcf65
YW
857}
858
a27588d4 859int dhcp6_pd_prefix_acquired(Link *uplink) {
5ed10a19 860 union in_addr_union server_address;
d5ebcf65 861 usec_t timestamp_usec;
d5ebcf65 862 Link *link;
d5ebcf65
YW
863 int r;
864
a27588d4
YW
865 assert(uplink);
866 assert(uplink->dhcp6_lease);
d5ebcf65 867
a27588d4 868 r = sd_dhcp6_lease_get_server_address(uplink->dhcp6_lease, &server_address.in6);
5ed10a19 869 if (r < 0)
a27588d4 870 return log_link_warning_errno(uplink, r, "Failed to get server address of DHCPv6 lease: %m");
5ed10a19 871
a27588d4 872 r = sd_dhcp6_lease_get_timestamp(uplink->dhcp6_lease, clock_boottime_or_monotonic(), &timestamp_usec);
d5ebcf65 873 if (r < 0)
a27588d4 874 return log_link_warning_errno(uplink, r, "Failed to get timestamp of DHCPv6 lease: %m");
d5ebcf65 875
41664456 876 /* First, logs acquired prefixes and request unreachable routes. */
a27588d4 877 for (sd_dhcp6_lease_reset_pd_prefix_iter(uplink->dhcp6_lease);;) {
d5ebcf65 878 uint32_t lifetime_preferred_sec, lifetime_valid_sec;
41664456 879 usec_t lifetime_valid_usec;
d5ebcf65
YW
880 struct in6_addr pd_prefix;
881 uint8_t pd_prefix_len;
882
a27588d4 883 r = sd_dhcp6_lease_get_pd(uplink->dhcp6_lease, &pd_prefix, &pd_prefix_len,
d5ebcf65
YW
884 &lifetime_preferred_sec, &lifetime_valid_sec);
885 if (r < 0)
886 break;
887
d5ebcf65
YW
888 /* Mask prefix for safety. */
889 r = in6_addr_mask(&pd_prefix, pd_prefix_len);
41664456 890 if (r < 0)
a27588d4 891 return log_link_error_errno(uplink, r, "Failed to mask DHCPv6 PD prefix: %m");
41664456 892
d5ebcf65
YW
893 lifetime_valid_usec = usec_add(lifetime_valid_sec * USEC_PER_SEC, timestamp_usec);
894
a27588d4 895 r = dhcp_pd_prefix_add(uplink, &pd_prefix, pd_prefix_len);
d5ebcf65
YW
896 if (r < 0)
897 return r;
898
a27588d4 899 r = dhcp6_request_unreachable_route(uplink, &pd_prefix, pd_prefix_len, lifetime_valid_usec, &server_address);
d5ebcf65
YW
900 if (r < 0)
901 return r;
902 }
903
41664456 904 /* Then, assign subnet prefixes. */
a27588d4
YW
905 HASHMAP_FOREACH(link, uplink->manager->links_by_index) {
906 if (!dhcp_pd_is_uplink(link, uplink, /* accept_auto = */ true))
41664456
YW
907 continue;
908
a27588d4 909 r = dhcp6_pd_assign_subnet_prefixes(link, uplink);
d5ebcf65 910 if (r < 0) {
a27588d4 911 /* When failed on the upstream interface (i.e., the case link == uplink),
41664456
YW
912 * immediately abort the assignment of the prefixes. As, the all assigned
913 * prefixes will be dropped soon in link_enter_failed(), and it is meaningless
914 * to continue the assignment. */
a27588d4 915 if (link == uplink)
d5ebcf65
YW
916 return r;
917
918 link_enter_failed(link);
919 }
920 }
d5ebcf65
YW
921
922 return 0;
923}
924
925static bool dhcp6_pd_uplink_is_ready(Link *link) {
926 assert(link);
927
928 if (!link->network)
929 return false;
930
931 if (!link->network->dhcp6_use_pd_prefix)
932 return false;
933
934 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
935 return false;
936
937 if (!link->dhcp6_client)
938 return false;
939
940 if (sd_dhcp6_client_is_running(link->dhcp6_client) <= 0)
941 return false;
942
943 if (!link->dhcp6_lease)
944 return false;
945
946 return dhcp6_lease_has_pd_prefix(link->dhcp6_lease);
947}
948
a27588d4 949int dhcp_pd_find_uplink(Link *link, Link **ret) {
d5ebcf65
YW
950 Link *uplink = NULL;
951 int r = 0;
952
953 assert(link);
954 assert(link->manager);
a27588d4 955 assert(link_dhcp_pd_is_enabled(link));
d5ebcf65
YW
956 assert(ret);
957
a27588d4
YW
958 if (link->network->dhcp_pd_uplink_name)
959 r = link_get_by_name(link->manager, link->network->dhcp_pd_uplink_name, &uplink);
960 else if (link->network->dhcp_pd_uplink_index > 0)
961 r = link_get_by_index(link->manager, link->network->dhcp_pd_uplink_index, &uplink);
962 else if (link->network->dhcp_pd_uplink_index == UPLINK_INDEX_SELF)
d5ebcf65
YW
963 uplink = link;
964 if (r < 0)
965 return r;
966
967 if (uplink) {
968 if (!dhcp6_pd_uplink_is_ready(uplink))
969 return -EBUSY;
970
971 *ret = uplink;
972 return 0;
973 }
974
975 HASHMAP_FOREACH(uplink, link->manager->links_by_index) {
976 if (!dhcp6_pd_uplink_is_ready(uplink))
977 continue;
978
979 /* Assume that there exists at most one link which acquired delegated prefixes. */
980 *ret = uplink;
981 return 0;
982 }
983
984 return -ENODEV;
985}
986
a27588d4 987int dhcp_request_prefix_delegation(Link *link) {
d5ebcf65
YW
988 Link *uplink;
989
990 assert(link);
991
a27588d4 992 if (!link_dhcp_pd_is_enabled(link))
d5ebcf65
YW
993 return 0;
994
a27588d4 995 if (dhcp_pd_find_uplink(link, &uplink) < 0)
d5ebcf65
YW
996 return 0;
997
998 log_link_debug(link, "Requesting subnets of delegated prefixes acquired by %s", uplink->ifname);
a27588d4 999 return dhcp6_pd_assign_subnet_prefixes(link, uplink);
d5ebcf65
YW
1000}
1001
a27588d4 1002int config_parse_dhcp_pd_subnet_id(
d5ebcf65
YW
1003 const char *unit,
1004 const char *filename,
1005 unsigned line,
1006 const char *section,
1007 unsigned section_line,
1008 const char *lvalue,
1009 int ltype,
1010 const char *rvalue,
1011 void *data,
1012 void *userdata) {
1013
1014 int64_t *p = data;
1015 uint64_t t;
1016 int r;
1017
1018 assert(filename);
1019 assert(lvalue);
1020 assert(rvalue);
1021 assert(data);
1022
1023 if (isempty(rvalue) || streq(rvalue, "auto")) {
1024 *p = -1;
1025 return 0;
1026 }
1027
1028 r = safe_atoux64(rvalue, &t);
1029 if (r < 0) {
1030 log_syntax(unit, LOG_WARNING, filename, line, r,
1031 "Failed to parse %s=, ignoring assignment: %s",
1032 lvalue, rvalue);
1033 return 0;
1034 }
1035 if (t > INT64_MAX) {
1036 log_syntax(unit, LOG_WARNING, filename, line, 0,
1037 "Invalid subnet id '%s', ignoring assignment.",
1038 rvalue);
1039 return 0;
1040 }
1041
1042 *p = (int64_t) t;
1043
1044 return 0;
1045}