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