Debian Codegraph @

src/network/networkd-dhcp-prefix-delegation.c

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