]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/network/networkd-dhcp-prefix-delegation.c
network: dhcp-pd: rename [DHCPv6PrefixDelegation] -> [DHCPPrefixDelegation]
[thirdparty/systemd.git] / src / network / networkd-dhcp-prefix-delegation.c
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3#include <linux/ipv6_route.h>
4
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
22bool link_dhcp_pd_is_enabled(Link *link) {
23 assert(link);
24
25 if (!link->network)
26 return false;
27
28 return link->network->dhcp_pd;
29}
30
31bool dhcp_pd_is_uplink(Link *link, Link *target, bool accept_auto) {
32 assert(link);
33 assert(target);
34
35 if (!link_dhcp_pd_is_enabled(link))
36 return false;
37
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);
41
42 if (link->network->dhcp_pd_uplink_index > 0)
43 return target->ifindex == link->network->dhcp_pd_uplink_index;
44
45 if (link->network->dhcp_pd_uplink_index == UPLINK_INDEX_SELF)
46 return link == target;
47
48 assert(link->network->dhcp_pd_uplink_index == UPLINK_INDEX_AUTO);
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
65static void link_remove_dhcp_pd_subnet_prefix(Link *link, const struct in6_addr *prefix) {
66 void *key;
67
68 assert(link);
69 assert(link->manager);
70 assert(prefix);
71
72 if (hashmap_get(link->manager->links_by_dhcp_pd_subnet_prefix, prefix) != link)
73 return;
74
75 hashmap_remove2(link->manager->links_by_dhcp_pd_subnet_prefix, prefix, &key);
76 free(key);
77}
78
79static int link_add_dhcp_pd_subnet_prefix(Link *link, const struct in6_addr *prefix) {
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
90 r = hashmap_ensure_put(&link->manager->links_by_dhcp_pd_subnet_prefix, &in6_addr_hash_ops_free, copy, link);
91 if (r < 0)
92 return r;
93 if (r > 0)
94 TAKE_PTR(copy);
95
96 return 0;
97}
98
99static int link_get_by_dhcp_pd_subnet_prefix(Manager *manager, const struct in6_addr *prefix, Link **ret) {
100 Link *link;
101
102 assert(manager);
103 assert(prefix);
104
105 link = hashmap_get(manager->links_by_dhcp_pd_subnet_prefix, prefix);
106 if (!link)
107 return -ENODEV;
108
109 if (ret)
110 *ret = link;
111 return 0;
112}
113
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) {
115 assert(link);
116 assert(pd_prefix);
117
118 if (!link_dhcp_pd_is_enabled(link))
119 return -ENOENT;
120
121 if (link->network->dhcp_pd_assign) {
122 Address *address;
123
124 SET_FOREACH(address, link->addresses) {
125 if (address->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
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) {
144 if (route->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
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
159int dhcp_pd_remove(Link *link, bool only_marked) {
160 int k, r = 0;
161
162 assert(link);
163 assert(link->manager);
164
165 if (!link_dhcp_pd_is_enabled(link))
166 return 0;
167
168 if (!only_marked)
169 link->dhcp_pd_configured = false;
170
171 if (!link->network->dhcp_pd_assign) {
172 Route *route;
173
174 SET_FOREACH(route, link->routes) {
175 if (route->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
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
183 link_remove_dhcp_pd_subnet_prefix(link, &route->dst.in6);
184
185 k = route_remove(route);
186 if (k < 0)
187 r = k;
188
189 route_cancel_request(route, link);
190 }
191 } else {
192 Address *address;
193
194 SET_FOREACH(address, link->addresses) {
195 struct in6_addr prefix;
196
197 if (address->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
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
208 link_remove_dhcp_pd_subnet_prefix(link, &prefix);
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
221static int dhcp_pd_check_ready(Link *link);
222
223static int dhcp_pd_address_ready_callback(Address *address) {
224 Address *a;
225
226 assert(address);
227 assert(address->link);
228
229 SET_FOREACH(a, address->link->addresses)
230 if (a->source == NETWORK_CONFIG_SOURCE_DHCP_PD)
231 a->callback = NULL;
232
233 return dhcp_pd_check_ready(address->link);
234}
235
236static int dhcp_pd_check_ready(Link *link) {
237 int r;
238
239 assert(link);
240 assert(link->network);
241
242 if (link->dhcp_pd_messages > 0) {
243 log_link_debug(link, "%s(): DHCP-PD addresses and routes are not set.", __func__);
244 return 0;
245 }
246
247 if (link->network->dhcp_pd_assign) {
248 bool has_ready = false;
249 Address *address;
250
251 SET_FOREACH(address, link->addresses) {
252 if (address->source != NETWORK_CONFIG_SOURCE_DHCP_PD)
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)
262 if (address->source == NETWORK_CONFIG_SOURCE_DHCP_PD)
263 address->callback = dhcp_pd_address_ready_callback;
264
265 log_link_debug(link, "%s(): no DHCP-PD address is ready.", __func__);
266 return 0;
267 }
268 }
269
270 link->dhcp_pd_configured = true;
271
272 log_link_debug(link, "DHCP-PD addresses and routes set.");
273
274 r = dhcp_pd_remove(link, /* only_marked = */ true);
275 if (r < 0)
276 return r;
277
278 link_check_ready(link);
279 return 1;
280}
281
282static int dhcp_pd_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
283 int r;
284
285 assert(link);
286 assert(link->dhcp_pd_messages > 0);
287
288 link->dhcp_pd_messages--;
289
290 r = route_configure_handler_internal(rtnl, m, link, "Failed to add prefix route for DHCP delegated subnet prefix");
291 if (r <= 0)
292 return r;
293
294 r = dhcp_pd_check_ready(link);
295 if (r < 0)
296 link_enter_failed(link);
297
298 return 1;
299}
300
301static int dhcp_pd_request_route(Link *link, const struct in6_addr *prefix, uint8_t prefixlen, usec_t lifetime_usec) {
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
310 if (link->network->dhcp_pd_assign)
311 return 0;
312
313 r = route_new(&route);
314 if (r < 0)
315 return r;
316
317 route->source = NETWORK_CONFIG_SOURCE_DHCP_PD;
318 route->family = AF_INET6;
319 route->dst.in6 = *prefix;
320 route->dst_prefixlen = prefixlen;
321 route->protocol = RTPROT_DHCP;
322 route->priority = link->network->dhcp_pd_route_metric;
323 route->lifetime_usec = lifetime_usec;
324
325 if (route_get(NULL, link, route, &existing) < 0)
326 link->dhcp_pd_configured = false;
327 else
328 route_unmark(existing);
329
330 r = link_request_route(link, TAKE_PTR(route), true, &link->dhcp_pd_messages,
331 dhcp_pd_route_handler, NULL);
332 if (r < 0)
333 return log_link_error_errno(link, r, "Failed to request DHCP-PD prefix route: %m");
334
335 return 0;
336}
337
338static int dhcp_pd_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
339 int r;
340
341 assert(link);
342 assert(link->dhcp_pd_messages > 0);
343
344 link->dhcp_pd_messages--;
345
346 r = address_configure_handler_internal(rtnl, m, link, "Could not set DHCP-PD address");
347 if (r <= 0)
348 return r;
349
350 r = dhcp_pd_check_ready(link);
351 if (r < 0)
352 link_enter_failed(link);
353
354 return 1;
355}
356
357static void log_dhcp_pd_address(Link *link, const Address *address) {
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
371 log_link_full(link, log_level, "DHCP-PD address %s (valid %s, preferred %s)",
372 strna(buffer),
373 FORMAT_LIFETIME(address->lifetime_valid_usec),
374 FORMAT_LIFETIME(address->lifetime_preferred_usec));
375}
376
377static int dhcp_pd_request_address(
378 Link *link,
379 const struct in6_addr *prefix,
380 uint8_t prefixlen,
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
392 if (!link->network->dhcp_pd_assign)
393 return 0;
394
395 r = dhcp_pd_generate_addresses(link, prefix, prefixlen, &addresses);
396 if (r < 0)
397 return log_link_warning_errno(link, r, "Failed to generate addresses for acquired DHCP delegated prefix: %m");
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)
405 return log_link_error_errno(link, r, "Failed to allocate address for DHCP delegated prefix: %m");
406
407 address->source = NETWORK_CONFIG_SOURCE_DHCP_PD;
408 address->family = AF_INET6;
409 address->in_addr.in6 = *a;
410 address->prefixlen = prefixlen;
411 address->lifetime_preferred_usec = lifetime_preferred_usec;
412 address->lifetime_valid_usec = lifetime_valid_usec;
413 if (prefixlen == 64)
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;
416
417 log_dhcp_pd_address(link, address);
418
419 if (address_get(link, address, &existing) < 0)
420 link->dhcp_pd_configured = false;
421 else
422 address_unmark(existing);
423
424 r = link_request_address(link, TAKE_PTR(address), true, &link->dhcp_pd_messages,
425 dhcp_pd_address_handler, NULL);
426 if (r < 0)
427 return log_link_error_errno(link, r, "Failed to request DHCP delegated prefix address: %m");
428 }
429
430 return 0;
431}
432
433static int dhcp_pd_calculate_subnet_prefix(
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
459static int dhcp_pd_get_preferred_subnet_prefix(
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);
471 assert(link->network);
472 assert(pd_prefix);
473
474 if (link->network->dhcp_pd_subnet_id >= 0) {
475 /* If the link has a preference for a particular subnet id try to allocate that */
476
477 r = dhcp_pd_calculate_subnet_prefix(pd_prefix, pd_prefix_len, link->network->dhcp_pd_subnet_id, &prefix);
478 if (r < 0)
479 return log_link_warning_errno(link, r,
480 "subnet id %" PRIu64 " is out of range. Only have %" PRIu64 " subnets.",
481 link->network->dhcp_pd_subnet_id, UINT64_C(1) << (64 - pd_prefix_len));
482
483 if (link_get_by_dhcp_pd_subnet_prefix(link->manager, &prefix, &assigned_link) >= 0 &&
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
501 r = dhcp_pd_calculate_subnet_prefix(pd_prefix, pd_prefix_len, n, &prefix);
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. */
508 if (set_contains(link->manager->dhcp_pd_subnet_ids, &n))
509 continue;
510
511 /* Check that the prefix is not assigned to another link. */
512 if (link_get_by_dhcp_pd_subnet_prefix(link->manager, &prefix, &assigned_link) < 0 ||
513 assigned_link == link) {
514 *ret = prefix;
515 return 0;
516 }
517 }
518}
519
520static int dhcp_pd_assign_subnet_prefix(
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
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)
537 return 0;
538
539 (void) in6_addr_prefix_to_string(&prefix, 64, &buf);
540
541 if (link_radv_enabled(link) && link->network->dhcp_pd_announce) {
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
549 r = dhcp_pd_request_route(link, &prefix, 64, lifetime_valid_usec);
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
555 r = dhcp_pd_request_address(link, &prefix, 64, lifetime_preferred_usec, lifetime_valid_usec);
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
561 r = link_add_dhcp_pd_subnet_prefix(link, &prefix);
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
571static int dhcp_pd_assign_prefix_on_uplink(
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
587 if (link->network->dhcp_pd_announce)
588 log_link_debug(link, "Ignoring Announce= setting on upstream interface.");
589
590 r = dhcp_pd_request_route(link, pd_prefix, pd_prefix_len, lifetime_valid_usec);
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
596 r = dhcp_pd_request_address(link, pd_prefix, pd_prefix_len, lifetime_preferred_usec, lifetime_valid_usec);
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
606static int dhcp_pd_prepare(Link *link) {
607 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
608 return 0;
609
610 if (!link_dhcp_pd_is_enabled(link))
611 return 0;
612
613 if (link_radv_enabled(link) && link->network->dhcp_pd_announce && !link->radv)
614 return 0;
615
616 link_mark_addresses(link, NETWORK_CONFIG_SOURCE_DHCP_PD, NULL);
617 link_mark_routes(link, NETWORK_CONFIG_SOURCE_DHCP_PD, NULL);
618
619 return 1;
620}
621
622static int dhcp_pd_finalize(Link *link) {
623 int r;
624
625 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
626 return 0;
627
628 if (link->dhcp_pd_messages == 0) {
629 link->dhcp_pd_configured = false;
630
631 r = dhcp_pd_remove(link, /* only_marked = */ true);
632 if (r < 0)
633 return r;
634 }
635
636 if (!link->dhcp_pd_configured)
637 link_set_state(link, LINK_STATE_CONFIGURING);
638
639 link_check_ready(link);
640 return 0;
641}
642
643void dhcp_pd_prefix_lost(Link *uplink) {
644 Route *route;
645 Link *link;
646 int r;
647
648 assert(uplink);
649 assert(uplink->manager);
650
651 HASHMAP_FOREACH(link, uplink->manager->links_by_index) {
652 if (!dhcp_pd_is_uplink(link, uplink, /* accept_auto = */ true))
653 continue;
654
655 r = dhcp_pd_remove(link, /* only_marked = */ false);
656 if (r < 0)
657 link_enter_failed(link);
658 }
659
660 SET_FOREACH(route, uplink->manager->routes) {
661 if (IN_SET(route->source, NETWORK_CONFIG_SOURCE_DHCP4, NETWORK_CONFIG_SOURCE_DHCP6))
662 continue;
663 if (route->family != AF_INET6)
664 continue;
665 if (route->type != RTN_UNREACHABLE)
666 continue;
667 if (!set_contains(uplink->dhcp_pd_prefixes,
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
676 route_cancel_request(route, uplink);
677 }
678
679 set_clear(uplink->dhcp_pd_prefixes);
680}
681
682static int dhcp6_unreachable_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
683 int r;
684
685 assert(link);
686 assert(link->dhcp6_messages > 0);
687
688 link->dhcp6_messages--;
689
690 r = route_configure_handler_internal(rtnl, m, link, "Failed to set unreachable route for DHCP delegated prefix");
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
701static int dhcp_request_unreachable_route(
702 Link *link,
703 const struct in6_addr *addr,
704 uint8_t prefixlen,
705 usec_t lifetime_usec,
706 NetworkConfigSource source,
707 const union in_addr_union *server_address,
708 unsigned *counter,
709 link_netlink_message_handler_t callback) {
710
711 _cleanup_(route_freep) Route *route = NULL;
712 Route *existing;
713 int r;
714
715 assert(link);
716 assert(addr);
717 assert(IN_SET(source, NETWORK_CONFIG_SOURCE_DHCP4, NETWORK_CONFIG_SOURCE_DHCP6));
718 assert(server_address);
719 assert(counter);
720 assert(callback);
721
722 if (prefixlen >= 64) {
723 _cleanup_free_ char *buf = NULL;
724
725 (void) in6_addr_prefix_to_string(addr, prefixlen, &buf);
726 log_link_debug(link, "Not adding a blocking route for DHCP delegated prefix %s since the prefix has length >= 64.",
727 strna(buf));
728 return 0;
729 }
730
731 r = route_new(&route);
732 if (r < 0)
733 return log_oom();
734
735 route->source = source;
736 route->provider = *server_address;
737 route->family = AF_INET6;
738 route->dst.in6 = *addr;
739 route->dst_prefixlen = prefixlen;
740 route->type = RTN_UNREACHABLE;
741 route->protocol = RTPROT_DHCP;
742 route->priority = IP6_RT_PRIO_USER;
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
750 r = link_request_route(link, TAKE_PTR(route), true, counter, callback, NULL);
751 if (r < 0) {
752 _cleanup_free_ char *buf = NULL;
753
754 (void) in6_addr_prefix_to_string(addr, prefixlen, &buf);
755 return log_link_error_errno(link, r, "Failed to request unreachable route for DHCP delegated prefix %s: %m",
756 strna(buf));
757 }
758
759 return 0;
760}
761
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) {
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,
795 set_contains(link->dhcp_pd_prefixes, p) ? LOG_DEBUG :
796 prefixlen > 64 || prefixlen < 48 ? LOG_WARNING : LOG_INFO,
797 "DHCP: received PD Prefix %s%s",
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. */
803 r = set_ensure_consume(&link->dhcp_pd_prefixes, &in_addr_prefix_hash_ops_free, p);
804 if (r < 0)
805 return log_link_error_errno(link, r, "Failed to store DHCP PD prefix %s: %m", strna(buf));
806
807 return 0;
808}
809
810static int dhcp6_pd_assign_subnet_prefixes(Link *link, Link *uplink) {
811 usec_t timestamp_usec;
812 int r;
813
814 assert(link);
815 assert(uplink);
816 assert(uplink->dhcp6_lease);
817
818 r = dhcp_pd_prepare(link);
819 if (r <= 0)
820 return r;
821
822 r = sd_dhcp6_lease_get_timestamp(uplink->dhcp6_lease, clock_boottime_or_monotonic(), &timestamp_usec);
823 if (r < 0)
824 return r;
825
826 for (sd_dhcp6_lease_reset_pd_prefix_iter(uplink->dhcp6_lease);;) {
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
832 r = sd_dhcp6_lease_get_pd(uplink->dhcp6_lease, &pd_prefix, &pd_prefix_len,
833 &lifetime_preferred_sec, &lifetime_valid_sec);
834 if (r < 0)
835 break;
836
837 if (pd_prefix_len > 64)
838 continue;
839
840 /* Mask prefix for safety. */
841 r = in6_addr_mask(&pd_prefix, pd_prefix_len);
842 if (r < 0)
843 return r;
844
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);
847
848 if (link == uplink)
849 r = dhcp_pd_assign_prefix_on_uplink(link, &pd_prefix, pd_prefix_len, lifetime_preferred_usec, lifetime_valid_usec);
850 else
851 r = dhcp_pd_assign_subnet_prefix(link, &pd_prefix, pd_prefix_len, lifetime_preferred_usec, lifetime_valid_usec);
852 if (r < 0)
853 return r;
854 }
855
856 return dhcp_pd_finalize(link);
857}
858
859int dhcp6_pd_prefix_acquired(Link *uplink) {
860 union in_addr_union server_address;
861 usec_t timestamp_usec;
862 Link *link;
863 int r;
864
865 assert(uplink);
866 assert(uplink->dhcp6_lease);
867
868 r = sd_dhcp6_lease_get_server_address(uplink->dhcp6_lease, &server_address.in6);
869 if (r < 0)
870 return log_link_warning_errno(uplink, r, "Failed to get server address of DHCPv6 lease: %m");
871
872 r = sd_dhcp6_lease_get_timestamp(uplink->dhcp6_lease, clock_boottime_or_monotonic(), &timestamp_usec);
873 if (r < 0)
874 return log_link_warning_errno(uplink, r, "Failed to get timestamp of DHCPv6 lease: %m");
875
876 /* First, logs acquired prefixes and request unreachable routes. */
877 for (sd_dhcp6_lease_reset_pd_prefix_iter(uplink->dhcp6_lease);;) {
878 uint32_t lifetime_preferred_sec, lifetime_valid_sec;
879 usec_t lifetime_valid_usec;
880 struct in6_addr pd_prefix;
881 uint8_t pd_prefix_len;
882
883 r = sd_dhcp6_lease_get_pd(uplink->dhcp6_lease, &pd_prefix, &pd_prefix_len,
884 &lifetime_preferred_sec, &lifetime_valid_sec);
885 if (r < 0)
886 break;
887
888 /* Mask prefix for safety. */
889 r = in6_addr_mask(&pd_prefix, pd_prefix_len);
890 if (r < 0)
891 return log_link_error_errno(uplink, r, "Failed to mask DHCPv6 PD prefix: %m");
892
893 lifetime_valid_usec = usec_add(lifetime_valid_sec * USEC_PER_SEC, timestamp_usec);
894
895 r = dhcp_pd_prefix_add(uplink, &pd_prefix, pd_prefix_len);
896 if (r < 0)
897 return r;
898
899 r = dhcp6_request_unreachable_route(uplink, &pd_prefix, pd_prefix_len, lifetime_valid_usec, &server_address);
900 if (r < 0)
901 return r;
902 }
903
904 /* Then, assign subnet prefixes. */
905 HASHMAP_FOREACH(link, uplink->manager->links_by_index) {
906 if (!dhcp_pd_is_uplink(link, uplink, /* accept_auto = */ true))
907 continue;
908
909 r = dhcp6_pd_assign_subnet_prefixes(link, uplink);
910 if (r < 0) {
911 /* When failed on the upstream interface (i.e., the case link == uplink),
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. */
915 if (link == uplink)
916 return r;
917
918 link_enter_failed(link);
919 }
920 }
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
949int dhcp_pd_find_uplink(Link *link, Link **ret) {
950 Link *uplink = NULL;
951 int r = 0;
952
953 assert(link);
954 assert(link->manager);
955 assert(link_dhcp_pd_is_enabled(link));
956 assert(ret);
957
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)
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
987int dhcp_request_prefix_delegation(Link *link) {
988 Link *uplink;
989
990 assert(link);
991
992 if (!link_dhcp_pd_is_enabled(link))
993 return 0;
994
995 if (dhcp_pd_find_uplink(link, &uplink) < 0)
996 return 0;
997
998 log_link_debug(link, "Requesting subnets of delegated prefixes acquired by %s", uplink->ifname);
999 return dhcp6_pd_assign_subnet_prefixes(link, uplink);
1000}
1001
1002int config_parse_dhcp_pd_subnet_id(
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}