]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | bool link_dhcp6_pd_is_enabled(Link *link) { | |
23 | assert(link); | |
24 | ||
25 | if (!link->network) | |
26 | return false; | |
27 | ||
28 | return link->network->dhcp6_pd; | |
29 | } | |
30 | ||
31 | bool dhcp6_pd_is_uplink(Link *link, Link *target, bool accept_auto) { | |
32 | assert(link); | |
33 | assert(target); | |
34 | ||
35 | if (!link_dhcp6_pd_is_enabled(link)) | |
36 | return false; | |
37 | ||
38 | if (link->network->dhcp6_pd_uplink_name) | |
39 | return streq_ptr(target->ifname, link->network->dhcp6_pd_uplink_name) || | |
40 | strv_contains(target->alternative_names, link->network->dhcp6_pd_uplink_name); | |
41 | ||
42 | if (link->network->dhcp6_pd_uplink_index > 0) | |
43 | return target->ifindex == link->network->dhcp6_pd_uplink_index; | |
44 | ||
45 | if (link->network->dhcp6_pd_uplink_index == UPLINK_INDEX_SELF) | |
46 | return link == target; | |
47 | ||
48 | assert(link->network->dhcp6_pd_uplink_index == UPLINK_INDEX_AUTO); | |
49 | return accept_auto; | |
50 | } | |
51 | ||
52 | bool 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 | ||
65 | static void link_remove_dhcp6_pd_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_dhcp6_pd_prefix, prefix) != link) | |
73 | return; | |
74 | ||
75 | hashmap_remove2(link->manager->links_by_dhcp6_pd_prefix, prefix, &key); | |
76 | free(key); | |
77 | } | |
78 | ||
79 | static int link_add_dhcp6_pd_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_dhcp6_pd_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 | ||
99 | static int link_get_by_dhcp6_pd_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_dhcp6_pd_prefix, prefix); | |
106 | if (!link) | |
107 | return -ENODEV; | |
108 | ||
109 | if (ret) | |
110 | *ret = link; | |
111 | return 0; | |
112 | } | |
113 | ||
114 | static int dhcp6_pd_get_assigned_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_dhcp6_pd_is_enabled(link)) | |
119 | return -ENOENT; | |
120 | ||
121 | if (link->network->dhcp6_pd_assign) { | |
122 | Address *address; | |
123 | ||
124 | SET_FOREACH(address, link->addresses) { | |
125 | if (address->source != NETWORK_CONFIG_SOURCE_DHCP6PD) | |
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_DHCP6PD) | |
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 | ||
159 | int dhcp6_pd_remove(Link *link, bool only_marked) { | |
160 | int k, r = 0; | |
161 | ||
162 | assert(link); | |
163 | assert(link->manager); | |
164 | ||
165 | if (!link_dhcp6_pd_is_enabled(link)) | |
166 | return 0; | |
167 | ||
168 | if (!only_marked) | |
169 | link->dhcp6_pd_configured = false; | |
170 | ||
171 | if (!link->network->dhcp6_pd_assign) { | |
172 | Route *route; | |
173 | ||
174 | SET_FOREACH(route, link->routes) { | |
175 | if (route->source != NETWORK_CONFIG_SOURCE_DHCP6PD) | |
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_dhcp6_pd_prefix(link, &route->dst.in6); | |
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 | ||
197 | if (address->source != NETWORK_CONFIG_SOURCE_DHCP6PD) | |
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_dhcp6_pd_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 | ||
221 | static int dhcp6_pd_check_ready(Link *link); | |
222 | ||
223 | static int dhcp6_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_DHCP6PD) | |
231 | a->callback = NULL; | |
232 | ||
233 | return dhcp6_pd_check_ready(address->link); | |
234 | } | |
235 | ||
236 | static int dhcp6_pd_check_ready(Link *link) { | |
237 | int r; | |
238 | ||
239 | assert(link); | |
240 | assert(link->network); | |
241 | ||
242 | if (link->dhcp6_pd_messages > 0) { | |
243 | log_link_debug(link, "%s(): DHCPv6PD addresses and routes are not set.", __func__); | |
244 | return 0; | |
245 | } | |
246 | ||
247 | if (link->network->dhcp6_pd_assign) { | |
248 | bool has_ready = false; | |
249 | Address *address; | |
250 | ||
251 | SET_FOREACH(address, link->addresses) { | |
252 | if (address->source != NETWORK_CONFIG_SOURCE_DHCP6PD) | |
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_DHCP6PD) | |
263 | address->callback = dhcp6_pd_address_ready_callback; | |
264 | ||
265 | log_link_debug(link, "%s(): no DHCPv6PD address is ready.", __func__); | |
266 | return 0; | |
267 | } | |
268 | } | |
269 | ||
270 | link->dhcp6_pd_configured = true; | |
271 | ||
272 | log_link_debug(link, "DHCPv6 PD addresses and routes set."); | |
273 | ||
274 | r = dhcp6_pd_remove(link, /* only_marked = */ true); | |
275 | if (r < 0) | |
276 | return r; | |
277 | ||
278 | link_check_ready(link); | |
279 | return 1; | |
280 | } | |
281 | ||
282 | static int dhcp6_pd_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { | |
283 | int r; | |
284 | ||
285 | assert(link); | |
286 | assert(link->dhcp6_pd_messages > 0); | |
287 | ||
288 | link->dhcp6_pd_messages--; | |
289 | ||
290 | r = route_configure_handler_internal(rtnl, m, link, "Failed to add DHCPv6 Prefix Delegation route"); | |
291 | if (r <= 0) | |
292 | return r; | |
293 | ||
294 | r = dhcp6_pd_check_ready(link); | |
295 | if (r < 0) | |
296 | link_enter_failed(link); | |
297 | ||
298 | return 1; | |
299 | } | |
300 | ||
ab0c82d9 | 301 | static int dhcp6_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 | ||
310 | if (link->network->dhcp6_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_DHCP6PD; | |
318 | route->family = AF_INET6; | |
319 | route->dst.in6 = *prefix; | |
ab0c82d9 | 320 | route->dst_prefixlen = prefixlen; |
d5ebcf65 YW |
321 | route->protocol = RTPROT_DHCP; |
322 | route->priority = link->network->dhcp6_pd_route_metric; | |
323 | route->lifetime_usec = lifetime_usec; | |
324 | ||
325 | if (route_get(NULL, link, route, &existing) < 0) | |
326 | link->dhcp6_pd_configured = false; | |
327 | else | |
328 | route_unmark(existing); | |
329 | ||
330 | r = link_request_route(link, TAKE_PTR(route), true, &link->dhcp6_pd_messages, | |
331 | dhcp6_pd_route_handler, NULL); | |
332 | if (r < 0) | |
333 | return log_link_error_errno(link, r, "Failed to request DHCPv6 prefix route: %m"); | |
334 | ||
335 | return 0; | |
336 | } | |
337 | ||
338 | static int dhcp6_pd_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { | |
339 | int r; | |
340 | ||
341 | assert(link); | |
342 | assert(link->dhcp6_pd_messages > 0); | |
343 | ||
344 | link->dhcp6_pd_messages--; | |
345 | ||
346 | r = address_configure_handler_internal(rtnl, m, link, "Could not set DHCPv6 delegated prefix address"); | |
347 | if (r <= 0) | |
348 | return r; | |
349 | ||
350 | r = dhcp6_pd_check_ready(link); | |
351 | if (r < 0) | |
352 | link_enter_failed(link); | |
353 | ||
354 | return 1; | |
355 | } | |
356 | ||
357 | static void log_dhcp6_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, "DHCPv6-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 | ||
377 | static int dhcp6_pd_request_address( | |
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 | ||
392 | if (!link->network->dhcp6_pd_assign) | |
393 | return 0; | |
394 | ||
ab0c82d9 | 395 | r = dhcp6_pd_generate_addresses(link, prefix, prefixlen, &addresses); |
d5ebcf65 YW |
396 | if (r < 0) |
397 | return log_link_warning_errno(link, r, "Failed to generate addresses for acquired DHCPv6 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 DHCPv6 delegated prefix: %m"); | |
406 | ||
407 | address->source = NETWORK_CONFIG_SOURCE_DHCP6PD; | |
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 YW |
413 | if (prefixlen == 64) |
414 | SET_FLAG(address->flags, IFA_F_MANAGETEMPADDR, link->network->dhcp6_pd_manage_temporary_address); | |
d5ebcf65 YW |
415 | address->route_metric = link->network->dhcp6_pd_route_metric; |
416 | ||
417 | log_dhcp6_pd_address(link, address); | |
418 | ||
419 | if (address_get(link, address, &existing) < 0) | |
420 | link->dhcp6_pd_configured = false; | |
421 | else | |
422 | address_unmark(existing); | |
423 | ||
424 | r = link_request_address(link, TAKE_PTR(address), true, &link->dhcp6_pd_messages, | |
425 | dhcp6_pd_address_handler, NULL); | |
426 | if (r < 0) | |
427 | return log_link_error_errno(link, r, "Failed to request DHCPv6 delegated prefix address: %m"); | |
428 | } | |
429 | ||
430 | return 0; | |
431 | } | |
432 | ||
d5ebcf65 YW |
433 | static int dhcp6_pd_calculate_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 | ||
459 | static int dhcp6_pd_get_preferred_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); | |
11d8a83f | 471 | assert(link->network); |
d5ebcf65 YW |
472 | assert(pd_prefix); |
473 | ||
11d8a83f | 474 | if (link->network->dhcp6_pd_subnet_id >= 0) { |
d5ebcf65 YW |
475 | /* If the link has a preference for a particular subnet id try to allocate that */ |
476 | ||
477 | r = dhcp6_pd_calculate_prefix(pd_prefix, pd_prefix_len, link->network->dhcp6_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->dhcp6_pd_subnet_id, UINT64_C(1) << (64 - pd_prefix_len)); | |
482 | ||
483 | if (link_get_by_dhcp6_pd_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 = dhcp6_pd_calculate_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->dhcp6_pd_subnet_ids, &n)) | |
509 | continue; | |
510 | ||
511 | /* Check that the prefix is not assigned to another link. */ | |
512 | if (link_get_by_dhcp6_pd_prefix(link->manager, &prefix, &assigned_link) < 0 || | |
513 | assigned_link == link) { | |
514 | *ret = prefix; | |
515 | return 0; | |
516 | } | |
517 | } | |
518 | } | |
519 | ||
520 | static int dhcp6_pd_assign_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 (dhcp6_pd_get_assigned_prefix(link, pd_prefix, pd_prefix_len, &prefix) < 0 && | |
536 | dhcp6_pd_get_preferred_prefix(link, pd_prefix, pd_prefix_len, &prefix) < 0) | |
537 | return 0; | |
538 | ||
539 | (void) in6_addr_prefix_to_string(&prefix, 64, &buf); | |
540 | ||
2747f132 | 541 | if (link_radv_enabled(link) && link->network->dhcp6_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 | ||
ab0c82d9 | 549 | r = dhcp6_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 | ||
ab0c82d9 | 555 | r = dhcp6_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 | ||
561 | r = link_add_dhcp6_pd_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 | ||
ab0c82d9 YW |
571 | static int dhcp6_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->dhcp6_pd_announce) | |
588 | log_link_debug(link, "Ignoring Announce= setting on upstream interface."); | |
589 | ||
590 | r = dhcp6_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 = dhcp6_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 | ||
d5ebcf65 YW |
606 | static int dhcp6_pd_prepare(Link *link) { |
607 | if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) | |
608 | return 0; | |
609 | ||
610 | if (!link_dhcp6_pd_is_enabled(link)) | |
611 | return 0; | |
612 | ||
2747f132 | 613 | if (link_radv_enabled(link) && link->network->dhcp6_pd_announce && !link->radv) |
d5ebcf65 YW |
614 | return 0; |
615 | ||
616 | link_mark_addresses(link, NETWORK_CONFIG_SOURCE_DHCP6PD, NULL); | |
617 | link_mark_routes(link, NETWORK_CONFIG_SOURCE_DHCP6PD, NULL); | |
618 | ||
c3cd5351 | 619 | return 1; |
d5ebcf65 YW |
620 | } |
621 | ||
622 | static int dhcp6_pd_finalize(Link *link) { | |
623 | int r; | |
624 | ||
625 | if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) | |
626 | return 0; | |
627 | ||
d5ebcf65 YW |
628 | if (link->dhcp6_pd_messages == 0) { |
629 | link->dhcp6_pd_configured = false; | |
630 | ||
631 | r = dhcp6_pd_remove(link, /* only_marked = */ true); | |
632 | if (r < 0) | |
633 | return r; | |
634 | } | |
635 | ||
636 | if (!link->dhcp6_pd_configured) | |
637 | link_set_state(link, LINK_STATE_CONFIGURING); | |
638 | ||
639 | link_check_ready(link); | |
640 | return 0; | |
641 | } | |
642 | ||
643 | void dhcp6_pd_prefix_lost(Link *dhcp6_link) { | |
da10d2d5 | 644 | Route *route; |
d5ebcf65 YW |
645 | Link *link; |
646 | int r; | |
647 | ||
648 | assert(dhcp6_link); | |
649 | assert(dhcp6_link->manager); | |
650 | ||
651 | HASHMAP_FOREACH(link, dhcp6_link->manager->links_by_index) { | |
1d44187c | 652 | if (!dhcp6_pd_is_uplink(link, dhcp6_link, /* accept_auto = */ true)) |
d5ebcf65 YW |
653 | continue; |
654 | ||
655 | r = dhcp6_pd_remove(link, /* only_marked = */ false); | |
656 | if (r < 0) | |
657 | link_enter_failed(link); | |
658 | } | |
659 | ||
da10d2d5 YW |
660 | SET_FOREACH(route, dhcp6_link->manager->routes) { |
661 | if (route->source != 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(dhcp6_link->dhcp6_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, dhcp6_link); | |
677 | } | |
678 | ||
d5ebcf65 YW |
679 | set_clear(dhcp6_link->dhcp6_pd_prefixes); |
680 | } | |
681 | ||
682 | static int dhcp6_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 DHCPv6 delegated subnet"); | |
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 | ||
5ed10a19 YW |
701 | static int dhcp6_request_unreachable_route( |
702 | Link *link, | |
703 | const struct in6_addr *addr, | |
704 | uint8_t prefixlen, | |
705 | usec_t lifetime_usec, | |
706 | const union in_addr_union *server_address) { | |
707 | ||
d5ebcf65 | 708 | _cleanup_(route_freep) Route *route = NULL; |
d5ebcf65 YW |
709 | Route *existing; |
710 | int r; | |
711 | ||
712 | assert(link); | |
713 | assert(addr); | |
5ed10a19 | 714 | assert(server_address); |
d5ebcf65 | 715 | |
a536ec38 YW |
716 | if (prefixlen >= 64) { |
717 | _cleanup_free_ char *buf = NULL; | |
d5ebcf65 | 718 | |
a536ec38 YW |
719 | (void) in6_addr_prefix_to_string(addr, prefixlen, &buf); |
720 | log_link_debug(link, "Not adding a blocking route for DHCPv6 delegated prefix %s since the prefix has length >= 64.", | |
d5ebcf65 YW |
721 | strna(buf)); |
722 | return 0; | |
723 | } | |
724 | ||
725 | r = route_new(&route); | |
726 | if (r < 0) | |
727 | return log_oom(); | |
728 | ||
729 | route->source = NETWORK_CONFIG_SOURCE_DHCP6; | |
5ed10a19 | 730 | route->provider = *server_address; |
d5ebcf65 YW |
731 | route->family = AF_INET6; |
732 | route->dst.in6 = *addr; | |
733 | route->dst_prefixlen = prefixlen; | |
d5ebcf65 YW |
734 | route->type = RTN_UNREACHABLE; |
735 | route->protocol = RTPROT_DHCP; | |
557e1b52 | 736 | route->priority = IP6_RT_PRIO_USER; |
d5ebcf65 YW |
737 | route->lifetime_usec = lifetime_usec; |
738 | ||
739 | if (route_get(link->manager, NULL, route, &existing) < 0) | |
740 | link->dhcp6_configured = false; | |
741 | else | |
742 | route_unmark(existing); | |
743 | ||
744 | r = link_request_route(link, TAKE_PTR(route), true, &link->dhcp6_messages, | |
745 | dhcp6_route_handler, NULL); | |
a536ec38 YW |
746 | if (r < 0) { |
747 | _cleanup_free_ char *buf = NULL; | |
748 | ||
749 | (void) in6_addr_prefix_to_string(addr, prefixlen, &buf); | |
d5ebcf65 YW |
750 | return log_link_error_errno(link, r, "Failed to request unreachable route for DHCPv6 delegated subnet %s: %m", |
751 | strna(buf)); | |
a536ec38 | 752 | } |
d5ebcf65 YW |
753 | |
754 | return 0; | |
755 | } | |
756 | ||
757 | static int dhcp6_pd_prefix_add(Link *link, const struct in6_addr *prefix, uint8_t prefixlen) { | |
758 | _cleanup_free_ char *buf = NULL; | |
759 | struct in_addr_prefix *p; | |
760 | int r; | |
761 | ||
762 | assert(link); | |
763 | assert(prefix); | |
764 | ||
765 | p = new(struct in_addr_prefix, 1); | |
766 | if (!p) | |
767 | return log_oom(); | |
768 | ||
769 | *p = (struct in_addr_prefix) { | |
770 | .family = AF_INET6, | |
771 | .prefixlen = prefixlen, | |
772 | .address.in6 = *prefix, | |
773 | }; | |
774 | ||
775 | (void) in6_addr_prefix_to_string(prefix, prefixlen, &buf); | |
776 | ||
777 | log_link_full(link, | |
778 | set_contains(link->dhcp6_pd_prefixes, p) ? LOG_DEBUG : | |
779 | prefixlen > 64 || prefixlen < 48 ? LOG_WARNING : LOG_INFO, | |
780 | "DHCPv6: received PD Prefix %s%s", | |
781 | strna(buf), | |
782 | prefixlen > 64 ? " with prefix length > 64, ignoring." : | |
783 | prefixlen < 48 ? " with prefix length < 48, looks unusual.": ""); | |
784 | ||
785 | /* Store PD prefix even if prefixlen > 64, not to make logged at warning level so frequently. */ | |
786 | r = set_ensure_consume(&link->dhcp6_pd_prefixes, &in_addr_prefix_hash_ops_free, p); | |
787 | if (r < 0) | |
788 | return log_link_error_errno(link, r, "Failed to store DHCPv6 PD prefix %s: %m", strna(buf)); | |
789 | ||
a536ec38 | 790 | return 0; |
d5ebcf65 YW |
791 | } |
792 | ||
5014e660 | 793 | static int dhcp6_pd_assign_prefixes(Link *link, Link *uplink) { |
d5ebcf65 | 794 | usec_t timestamp_usec; |
d5ebcf65 YW |
795 | int r; |
796 | ||
5014e660 YW |
797 | assert(link); |
798 | assert(uplink); | |
799 | assert(uplink->dhcp6_lease); | |
d5ebcf65 | 800 | |
c3cd5351 YW |
801 | r = dhcp6_pd_prepare(link); |
802 | if (r <= 0) | |
5014e660 | 803 | return r; |
d5ebcf65 | 804 | |
c3cd5351 | 805 | r = sd_dhcp6_lease_get_timestamp(uplink->dhcp6_lease, clock_boottime_or_monotonic(), ×tamp_usec); |
5014e660 YW |
806 | if (r < 0) |
807 | return r; | |
d5ebcf65 | 808 | |
5014e660 | 809 | for (sd_dhcp6_lease_reset_pd_prefix_iter(uplink->dhcp6_lease);;) { |
d5ebcf65 YW |
810 | uint32_t lifetime_preferred_sec, lifetime_valid_sec; |
811 | usec_t lifetime_preferred_usec, lifetime_valid_usec; | |
812 | struct in6_addr pd_prefix; | |
813 | uint8_t pd_prefix_len; | |
814 | ||
5014e660 | 815 | r = sd_dhcp6_lease_get_pd(uplink->dhcp6_lease, &pd_prefix, &pd_prefix_len, |
d5ebcf65 YW |
816 | &lifetime_preferred_sec, &lifetime_valid_sec); |
817 | if (r < 0) | |
818 | break; | |
819 | ||
5014e660 | 820 | if (pd_prefix_len > 64) |
d5ebcf65 YW |
821 | continue; |
822 | ||
d5ebcf65 YW |
823 | /* Mask prefix for safety. */ |
824 | r = in6_addr_mask(&pd_prefix, pd_prefix_len); | |
825 | if (r < 0) | |
5014e660 | 826 | return r; |
d5ebcf65 | 827 | |
21cf8e9e YW |
828 | lifetime_preferred_usec = usec_add(lifetime_preferred_sec * USEC_PER_SEC, timestamp_usec); |
829 | lifetime_valid_usec = usec_add(lifetime_valid_sec * USEC_PER_SEC, timestamp_usec); | |
d5ebcf65 | 830 | |
ab0c82d9 YW |
831 | if (link == uplink) |
832 | r = dhcp6_pd_assign_prefix_on_uplink(link, &pd_prefix, pd_prefix_len, lifetime_preferred_usec, lifetime_valid_usec); | |
833 | else | |
834 | r = dhcp6_pd_assign_prefix(link, &pd_prefix, pd_prefix_len, lifetime_preferred_usec, lifetime_valid_usec); | |
d5ebcf65 YW |
835 | if (r < 0) |
836 | return r; | |
837 | } | |
838 | ||
542d0495 | 839 | return dhcp6_pd_finalize(link); |
d5ebcf65 YW |
840 | } |
841 | ||
d5ebcf65 | 842 | int dhcp6_pd_prefix_acquired(Link *dhcp6_link) { |
5ed10a19 | 843 | union in_addr_union server_address; |
d5ebcf65 | 844 | usec_t timestamp_usec; |
d5ebcf65 | 845 | Link *link; |
d5ebcf65 YW |
846 | int r; |
847 | ||
d5ebcf65 YW |
848 | assert(dhcp6_link); |
849 | assert(dhcp6_link->dhcp6_lease); | |
d5ebcf65 | 850 | |
5ed10a19 YW |
851 | r = sd_dhcp6_lease_get_server_address(dhcp6_link->dhcp6_lease, &server_address.in6); |
852 | if (r < 0) | |
853 | return log_link_warning_errno(dhcp6_link, r, "Failed to get server address of DHCPv6 lease: %m"); | |
854 | ||
d5ebcf65 | 855 | r = sd_dhcp6_lease_get_timestamp(dhcp6_link->dhcp6_lease, clock_boottime_or_monotonic(), ×tamp_usec); |
d5ebcf65 | 856 | if (r < 0) |
d5ebcf65 | 857 | return log_link_warning_errno(dhcp6_link, r, "Failed to get timestamp of DHCPv6 lease: %m"); |
d5ebcf65 | 858 | |
41664456 | 859 | /* First, logs acquired prefixes and request unreachable routes. */ |
d5ebcf65 | 860 | for (sd_dhcp6_lease_reset_pd_prefix_iter(dhcp6_link->dhcp6_lease);;) { |
d5ebcf65 | 861 | uint32_t lifetime_preferred_sec, lifetime_valid_sec; |
41664456 | 862 | usec_t lifetime_valid_usec; |
d5ebcf65 YW |
863 | struct in6_addr pd_prefix; |
864 | uint8_t pd_prefix_len; | |
865 | ||
d5ebcf65 | 866 | r = sd_dhcp6_lease_get_pd(dhcp6_link->dhcp6_lease, &pd_prefix, &pd_prefix_len, |
d5ebcf65 YW |
867 | &lifetime_preferred_sec, &lifetime_valid_sec); |
868 | if (r < 0) | |
869 | break; | |
870 | ||
d5ebcf65 YW |
871 | /* Mask prefix for safety. */ |
872 | r = in6_addr_mask(&pd_prefix, pd_prefix_len); | |
41664456 YW |
873 | if (r < 0) |
874 | return log_link_error_errno(dhcp6_link, r, "Failed to mask DHCPv6 PD prefix: %m"); | |
875 | ||
d5ebcf65 YW |
876 | lifetime_valid_usec = usec_add(lifetime_valid_sec * USEC_PER_SEC, timestamp_usec); |
877 | ||
878 | r = dhcp6_pd_prefix_add(dhcp6_link, &pd_prefix, pd_prefix_len); | |
d5ebcf65 YW |
879 | if (r < 0) |
880 | return r; | |
881 | ||
5ed10a19 | 882 | r = dhcp6_request_unreachable_route(dhcp6_link, &pd_prefix, pd_prefix_len, lifetime_valid_usec, &server_address); |
d5ebcf65 YW |
883 | if (r < 0) |
884 | return r; | |
885 | } | |
886 | ||
41664456 | 887 | /* Then, assign subnet prefixes. */ |
d5ebcf65 | 888 | HASHMAP_FOREACH(link, dhcp6_link->manager->links_by_index) { |
41664456 YW |
889 | if (!dhcp6_pd_is_uplink(link, dhcp6_link, /* accept_auto = */ true)) |
890 | continue; | |
891 | ||
892 | r = dhcp6_pd_assign_prefixes(link, dhcp6_link); | |
d5ebcf65 | 893 | if (r < 0) { |
41664456 YW |
894 | /* When failed on the upstream interface (i.e., the case link == dhcp6_link), |
895 | * immediately abort the assignment of the prefixes. As, the all assigned | |
896 | * prefixes will be dropped soon in link_enter_failed(), and it is meaningless | |
897 | * to continue the assignment. */ | |
d5ebcf65 YW |
898 | if (link == dhcp6_link) |
899 | return r; | |
900 | ||
901 | link_enter_failed(link); | |
902 | } | |
903 | } | |
d5ebcf65 YW |
904 | |
905 | return 0; | |
906 | } | |
907 | ||
908 | static bool dhcp6_pd_uplink_is_ready(Link *link) { | |
909 | assert(link); | |
910 | ||
911 | if (!link->network) | |
912 | return false; | |
913 | ||
914 | if (!link->network->dhcp6_use_pd_prefix) | |
915 | return false; | |
916 | ||
917 | if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) | |
918 | return false; | |
919 | ||
920 | if (!link->dhcp6_client) | |
921 | return false; | |
922 | ||
923 | if (sd_dhcp6_client_is_running(link->dhcp6_client) <= 0) | |
924 | return false; | |
925 | ||
926 | if (!link->dhcp6_lease) | |
927 | return false; | |
928 | ||
929 | return dhcp6_lease_has_pd_prefix(link->dhcp6_lease); | |
930 | } | |
931 | ||
932 | int dhcp6_pd_find_uplink(Link *link, Link **ret) { | |
933 | Link *uplink = NULL; | |
934 | int r = 0; | |
935 | ||
936 | assert(link); | |
937 | assert(link->manager); | |
938 | assert(link_dhcp6_pd_is_enabled(link)); | |
939 | assert(ret); | |
940 | ||
941 | if (link->network->dhcp6_pd_uplink_name) | |
942 | r = link_get_by_name(link->manager, link->network->dhcp6_pd_uplink_name, &uplink); | |
943 | else if (link->network->dhcp6_pd_uplink_index > 0) | |
944 | r = link_get_by_index(link->manager, link->network->dhcp6_pd_uplink_index, &uplink); | |
945 | else if (link->network->dhcp6_pd_uplink_index == UPLINK_INDEX_SELF) | |
946 | uplink = link; | |
947 | if (r < 0) | |
948 | return r; | |
949 | ||
950 | if (uplink) { | |
951 | if (!dhcp6_pd_uplink_is_ready(uplink)) | |
952 | return -EBUSY; | |
953 | ||
954 | *ret = uplink; | |
955 | return 0; | |
956 | } | |
957 | ||
958 | HASHMAP_FOREACH(uplink, link->manager->links_by_index) { | |
959 | if (!dhcp6_pd_uplink_is_ready(uplink)) | |
960 | continue; | |
961 | ||
962 | /* Assume that there exists at most one link which acquired delegated prefixes. */ | |
963 | *ret = uplink; | |
964 | return 0; | |
965 | } | |
966 | ||
967 | return -ENODEV; | |
968 | } | |
969 | ||
970 | int dhcp6_request_prefix_delegation(Link *link) { | |
971 | Link *uplink; | |
972 | ||
973 | assert(link); | |
974 | ||
975 | if (!link_dhcp6_pd_is_enabled(link)) | |
976 | return 0; | |
977 | ||
978 | if (dhcp6_pd_find_uplink(link, &uplink) < 0) | |
979 | return 0; | |
980 | ||
981 | log_link_debug(link, "Requesting subnets of delegated prefixes acquired by %s", uplink->ifname); | |
982 | return dhcp6_pd_assign_prefixes(link, uplink); | |
983 | } | |
984 | ||
985 | int config_parse_dhcp6_pd_subnet_id( | |
986 | const char *unit, | |
987 | const char *filename, | |
988 | unsigned line, | |
989 | const char *section, | |
990 | unsigned section_line, | |
991 | const char *lvalue, | |
992 | int ltype, | |
993 | const char *rvalue, | |
994 | void *data, | |
995 | void *userdata) { | |
996 | ||
997 | int64_t *p = data; | |
998 | uint64_t t; | |
999 | int r; | |
1000 | ||
1001 | assert(filename); | |
1002 | assert(lvalue); | |
1003 | assert(rvalue); | |
1004 | assert(data); | |
1005 | ||
1006 | if (isempty(rvalue) || streq(rvalue, "auto")) { | |
1007 | *p = -1; | |
1008 | return 0; | |
1009 | } | |
1010 | ||
1011 | r = safe_atoux64(rvalue, &t); | |
1012 | if (r < 0) { | |
1013 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1014 | "Failed to parse %s=, ignoring assignment: %s", | |
1015 | lvalue, rvalue); | |
1016 | return 0; | |
1017 | } | |
1018 | if (t > INT64_MAX) { | |
1019 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1020 | "Invalid subnet id '%s', ignoring assignment.", | |
1021 | rvalue); | |
1022 | return 0; | |
1023 | } | |
1024 | ||
1025 | *p = (int64_t) t; | |
1026 | ||
1027 | return 0; | |
1028 | } |