]>
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) | |
190 | (void) sd_radv_remove_prefix(link->radv, &route->dst.in6, 64); | |
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) | |
215 | (void) sd_radv_remove_prefix(link->radv, &prefix, 64); | |
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 | _cleanup_free_ char *buffer = NULL; |
362 | int log_level; | |
363 | ||
364 | assert(address); | |
365 | assert(address->family == AF_INET6); | |
366 | ||
367 | log_level = address_get(link, address, NULL) >= 0 ? LOG_DEBUG : LOG_INFO; | |
368 | ||
369 | if (log_level < log_get_max_level()) | |
370 | return; | |
371 | ||
372 | (void) in6_addr_prefix_to_string(&address->in_addr.in6, address->prefixlen, &buffer); | |
373 | ||
a27588d4 | 374 | log_link_full(link, log_level, "DHCP-PD address %s (valid %s, preferred %s)", |
d5ebcf65 YW |
375 | strna(buffer), |
376 | FORMAT_LIFETIME(address->lifetime_valid_usec), | |
377 | FORMAT_LIFETIME(address->lifetime_preferred_usec)); | |
378 | } | |
379 | ||
a27588d4 | 380 | static int dhcp_pd_request_address( |
d5ebcf65 YW |
381 | Link *link, |
382 | const struct in6_addr *prefix, | |
383 | usec_t lifetime_preferred_usec, | |
384 | usec_t lifetime_valid_usec) { | |
385 | ||
386 | _cleanup_set_free_ Set *addresses = NULL; | |
387 | struct in6_addr *a; | |
388 | int r; | |
389 | ||
390 | assert(link); | |
391 | assert(link->network); | |
392 | assert(prefix); | |
393 | ||
a27588d4 | 394 | if (!link->network->dhcp_pd_assign) |
d5ebcf65 YW |
395 | return 0; |
396 | ||
38488bab | 397 | r = dhcp_pd_generate_addresses(link, prefix, &addresses); |
d5ebcf65 | 398 | if (r < 0) |
a27588d4 | 399 | return log_link_warning_errno(link, r, "Failed to generate addresses for acquired DHCP delegated prefix: %m"); |
d5ebcf65 YW |
400 | |
401 | SET_FOREACH(a, addresses) { | |
402 | _cleanup_(address_freep) Address *address = NULL; | |
403 | Address *existing; | |
404 | ||
405 | r = address_new(&address); | |
406 | if (r < 0) | |
a27588d4 | 407 | return log_link_error_errno(link, r, "Failed to allocate address for DHCP delegated prefix: %m"); |
d5ebcf65 | 408 | |
a27588d4 | 409 | address->source = NETWORK_CONFIG_SOURCE_DHCP_PD; |
d5ebcf65 YW |
410 | address->family = AF_INET6; |
411 | address->in_addr.in6 = *a; | |
38488bab | 412 | address->prefixlen = 64; |
d5ebcf65 YW |
413 | address->lifetime_preferred_usec = lifetime_preferred_usec; |
414 | address->lifetime_valid_usec = lifetime_valid_usec; | |
38488bab | 415 | SET_FLAG(address->flags, IFA_F_MANAGETEMPADDR, link->network->dhcp_pd_manage_temporary_address); |
a27588d4 | 416 | address->route_metric = link->network->dhcp_pd_route_metric; |
d5ebcf65 | 417 | |
a27588d4 | 418 | log_dhcp_pd_address(link, address); |
d5ebcf65 YW |
419 | |
420 | if (address_get(link, address, &existing) < 0) | |
a27588d4 | 421 | link->dhcp_pd_configured = false; |
d5ebcf65 YW |
422 | else |
423 | address_unmark(existing); | |
424 | ||
a27588d4 YW |
425 | r = link_request_address(link, TAKE_PTR(address), true, &link->dhcp_pd_messages, |
426 | dhcp_pd_address_handler, NULL); | |
d5ebcf65 | 427 | if (r < 0) |
a27588d4 | 428 | return log_link_error_errno(link, r, "Failed to request DHCP delegated prefix address: %m"); |
d5ebcf65 YW |
429 | } |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
a27588d4 | 434 | static int dhcp_pd_calculate_subnet_prefix( |
d5ebcf65 YW |
435 | const struct in6_addr *pd_prefix, |
436 | uint8_t pd_prefix_len, | |
437 | uint64_t subnet_id, | |
438 | struct in6_addr *ret) { | |
439 | ||
440 | struct in6_addr prefix; | |
441 | ||
442 | assert(pd_prefix); | |
443 | assert(pd_prefix_len <= 64); | |
444 | assert(ret); | |
445 | ||
446 | if (subnet_id >= UINT64_C(1) << (64 - pd_prefix_len)) | |
447 | return -ERANGE; | |
448 | ||
449 | prefix = *pd_prefix; | |
450 | ||
451 | if (pd_prefix_len < 32) | |
452 | prefix.s6_addr32[0] |= htobe32(subnet_id >> 32); | |
453 | ||
454 | prefix.s6_addr32[1] |= htobe32(subnet_id & 0xffffffff); | |
455 | ||
456 | *ret = prefix; | |
457 | return 0; | |
458 | } | |
459 | ||
a27588d4 | 460 | static int dhcp_pd_get_preferred_subnet_prefix( |
d5ebcf65 YW |
461 | Link *link, |
462 | const struct in6_addr *pd_prefix, | |
463 | uint8_t pd_prefix_len, | |
464 | struct in6_addr *ret) { | |
465 | ||
466 | struct in6_addr prefix; | |
467 | Link *assigned_link; | |
468 | int r; | |
469 | ||
470 | assert(link); | |
471 | assert(link->manager); | |
11d8a83f | 472 | assert(link->network); |
d5ebcf65 YW |
473 | assert(pd_prefix); |
474 | ||
a27588d4 | 475 | if (link->network->dhcp_pd_subnet_id >= 0) { |
d5ebcf65 YW |
476 | /* If the link has a preference for a particular subnet id try to allocate that */ |
477 | ||
a27588d4 | 478 | r = dhcp_pd_calculate_subnet_prefix(pd_prefix, pd_prefix_len, link->network->dhcp_pd_subnet_id, &prefix); |
d5ebcf65 YW |
479 | if (r < 0) |
480 | return log_link_warning_errno(link, r, | |
481 | "subnet id %" PRIu64 " is out of range. Only have %" PRIu64 " subnets.", | |
a27588d4 | 482 | link->network->dhcp_pd_subnet_id, UINT64_C(1) << (64 - pd_prefix_len)); |
d5ebcf65 | 483 | |
d5ebcf65 YW |
484 | *ret = prefix; |
485 | return 0; | |
486 | } | |
487 | ||
07b7337a YW |
488 | if (dhcp_pd_get_assigned_subnet_prefix(link, pd_prefix, pd_prefix_len, ret) >= 0) |
489 | return 0; | |
490 | ||
d5ebcf65 YW |
491 | for (uint64_t n = 0; ; n++) { |
492 | /* If we do not have an allocation preference just iterate | |
493 | * through the address space and return the first free prefix. */ | |
494 | ||
a27588d4 | 495 | r = dhcp_pd_calculate_subnet_prefix(pd_prefix, pd_prefix_len, n, &prefix); |
d5ebcf65 YW |
496 | if (r < 0) |
497 | return log_link_warning_errno(link, r, | |
498 | "Couldn't find a suitable prefix. Ran out of address space."); | |
499 | ||
500 | /* Do not use explicitly requested subnet IDs. Note that the corresponding link may not | |
501 | * appear yet. So, we need to check the ID is not used in any .network files. */ | |
a27588d4 | 502 | if (set_contains(link->manager->dhcp_pd_subnet_ids, &n)) |
d5ebcf65 YW |
503 | continue; |
504 | ||
505 | /* Check that the prefix is not assigned to another link. */ | |
a27588d4 | 506 | if (link_get_by_dhcp_pd_subnet_prefix(link->manager, &prefix, &assigned_link) < 0 || |
07b7337a YW |
507 | assigned_link == link) |
508 | break; | |
d5ebcf65 | 509 | } |
07b7337a YW |
510 | |
511 | r = link_add_dhcp_pd_subnet_prefix(link, &prefix); | |
512 | if (r < 0) | |
513 | return log_link_warning_errno(link, r, "Failed to save acquired free subnet prefix: %m"); | |
514 | ||
515 | *ret = prefix; | |
516 | return 0; | |
d5ebcf65 YW |
517 | } |
518 | ||
a27588d4 | 519 | static int dhcp_pd_assign_subnet_prefix( |
d5ebcf65 YW |
520 | Link *link, |
521 | const struct in6_addr *pd_prefix, | |
522 | uint8_t pd_prefix_len, | |
523 | usec_t lifetime_preferred_usec, | |
38488bab YW |
524 | usec_t lifetime_valid_usec, |
525 | bool is_uplink) { | |
d5ebcf65 YW |
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 | ||
07b7337a YW |
535 | r = dhcp_pd_get_preferred_subnet_prefix(link, pd_prefix, pd_prefix_len, &prefix); |
536 | if (r < 0) | |
537 | return r == -ERANGE ? 0 : r; | |
d5ebcf65 YW |
538 | |
539 | (void) in6_addr_prefix_to_string(&prefix, 64, &buf); | |
540 | ||
a27588d4 | 541 | if (link_radv_enabled(link) && link->network->dhcp_pd_announce) { |
38488bab YW |
542 | if (is_uplink) |
543 | log_link_debug(link, "Ignoring Announce= setting on upstream interface."); | |
544 | else { | |
545 | r = radv_add_prefix(link, &prefix, 64, lifetime_preferred_usec, lifetime_valid_usec); | |
546 | if (r < 0) | |
547 | return log_link_warning_errno(link, r, | |
548 | "Failed to assign/update prefix %s to IPv6 Router Advertisement: %m", | |
549 | strna(buf)); | |
550 | } | |
d5ebcf65 YW |
551 | } |
552 | ||
38488bab | 553 | r = dhcp_pd_request_route(link, &prefix, lifetime_valid_usec); |
d5ebcf65 YW |
554 | if (r < 0) |
555 | return log_link_warning_errno(link, r, | |
556 | "Failed to assign/update route for prefix %s: %m", | |
557 | strna(buf)); | |
558 | ||
38488bab | 559 | r = dhcp_pd_request_address(link, &prefix, lifetime_preferred_usec, lifetime_valid_usec); |
d5ebcf65 YW |
560 | if (r < 0) |
561 | return log_link_warning_errno(link, r, | |
562 | "Failed to assign/update address for prefix %s: %m", | |
563 | strna(buf)); | |
564 | ||
d5ebcf65 YW |
565 | log_link_debug(link, "Assigned prefix %s", strna(buf)); |
566 | return 1; | |
567 | } | |
568 | ||
a27588d4 | 569 | static int dhcp_pd_prepare(Link *link) { |
d5ebcf65 YW |
570 | if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) |
571 | return 0; | |
572 | ||
a27588d4 | 573 | if (!link_dhcp_pd_is_enabled(link)) |
d5ebcf65 YW |
574 | return 0; |
575 | ||
a27588d4 | 576 | if (link_radv_enabled(link) && link->network->dhcp_pd_announce && !link->radv) |
d5ebcf65 YW |
577 | return 0; |
578 | ||
a27588d4 YW |
579 | link_mark_addresses(link, NETWORK_CONFIG_SOURCE_DHCP_PD, NULL); |
580 | link_mark_routes(link, NETWORK_CONFIG_SOURCE_DHCP_PD, NULL); | |
d5ebcf65 | 581 | |
c3cd5351 | 582 | return 1; |
d5ebcf65 YW |
583 | } |
584 | ||
a27588d4 | 585 | static int dhcp_pd_finalize(Link *link) { |
d5ebcf65 YW |
586 | int r; |
587 | ||
588 | if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) | |
589 | return 0; | |
590 | ||
a27588d4 YW |
591 | if (link->dhcp_pd_messages == 0) { |
592 | link->dhcp_pd_configured = false; | |
d5ebcf65 | 593 | |
a27588d4 | 594 | r = dhcp_pd_remove(link, /* only_marked = */ true); |
d5ebcf65 YW |
595 | if (r < 0) |
596 | return r; | |
597 | } | |
598 | ||
a27588d4 | 599 | if (!link->dhcp_pd_configured) |
d5ebcf65 YW |
600 | link_set_state(link, LINK_STATE_CONFIGURING); |
601 | ||
602 | link_check_ready(link); | |
603 | return 0; | |
604 | } | |
605 | ||
a27588d4 | 606 | void dhcp_pd_prefix_lost(Link *uplink) { |
da10d2d5 | 607 | Route *route; |
d5ebcf65 YW |
608 | Link *link; |
609 | int r; | |
610 | ||
a27588d4 YW |
611 | assert(uplink); |
612 | assert(uplink->manager); | |
d5ebcf65 | 613 | |
a27588d4 YW |
614 | HASHMAP_FOREACH(link, uplink->manager->links_by_index) { |
615 | if (!dhcp_pd_is_uplink(link, uplink, /* accept_auto = */ true)) | |
d5ebcf65 YW |
616 | continue; |
617 | ||
a27588d4 | 618 | r = dhcp_pd_remove(link, /* only_marked = */ false); |
d5ebcf65 YW |
619 | if (r < 0) |
620 | link_enter_failed(link); | |
621 | } | |
622 | ||
a27588d4 | 623 | SET_FOREACH(route, uplink->manager->routes) { |
277521a1 | 624 | if (!IN_SET(route->source, NETWORK_CONFIG_SOURCE_DHCP4, NETWORK_CONFIG_SOURCE_DHCP6)) |
da10d2d5 YW |
625 | continue; |
626 | if (route->family != AF_INET6) | |
627 | continue; | |
628 | if (route->type != RTN_UNREACHABLE) | |
629 | continue; | |
a27588d4 | 630 | if (!set_contains(uplink->dhcp_pd_prefixes, |
da10d2d5 YW |
631 | &(struct in_addr_prefix) { |
632 | .family = AF_INET6, | |
633 | .prefixlen = route->dst_prefixlen, | |
634 | .address = route->dst })) | |
635 | continue; | |
636 | ||
637 | (void) route_remove(route); | |
638 | ||
a27588d4 | 639 | route_cancel_request(route, uplink); |
da10d2d5 YW |
640 | } |
641 | ||
a27588d4 | 642 | set_clear(uplink->dhcp_pd_prefixes); |
d5ebcf65 YW |
643 | } |
644 | ||
e49bad01 YW |
645 | void dhcp4_pd_prefix_lost(Link *uplink) { |
646 | Link *tunnel; | |
647 | ||
648 | dhcp_pd_prefix_lost(uplink); | |
649 | ||
650 | if (uplink->dhcp4_6rd_tunnel_name && | |
651 | link_get_by_name(uplink->manager, uplink->dhcp4_6rd_tunnel_name, &tunnel) >= 0) | |
652 | (void) link_remove(tunnel); | |
653 | } | |
654 | ||
80d62d4f | 655 | static int dhcp4_unreachable_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) { |
e49bad01 YW |
656 | int r; |
657 | ||
658 | assert(link); | |
e49bad01 | 659 | |
21feba0a | 660 | r = route_configure_handler_internal(rtnl, m, link, "Failed to set unreachable route for DHCPv4 delegated prefix"); |
e49bad01 YW |
661 | if (r <= 0) |
662 | return r; | |
663 | ||
664 | r = dhcp4_check_ready(link); | |
665 | if (r < 0) | |
666 | link_enter_failed(link); | |
667 | ||
668 | return 1; | |
669 | } | |
670 | ||
80d62d4f | 671 | static int dhcp6_unreachable_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) { |
d5ebcf65 YW |
672 | int r; |
673 | ||
674 | assert(link); | |
d5ebcf65 | 675 | |
21feba0a | 676 | r = route_configure_handler_internal(rtnl, m, link, "Failed to set unreachable route for DHCPv6 delegated prefix"); |
d5ebcf65 YW |
677 | if (r <= 0) |
678 | return r; | |
679 | ||
680 | r = dhcp6_check_ready(link); | |
681 | if (r < 0) | |
682 | link_enter_failed(link); | |
683 | ||
684 | return 1; | |
685 | } | |
686 | ||
a27588d4 | 687 | static int dhcp_request_unreachable_route( |
5ed10a19 YW |
688 | Link *link, |
689 | const struct in6_addr *addr, | |
690 | uint8_t prefixlen, | |
691 | usec_t lifetime_usec, | |
a27588d4 YW |
692 | NetworkConfigSource source, |
693 | const union in_addr_union *server_address, | |
694 | unsigned *counter, | |
80d62d4f | 695 | route_netlink_handler_t callback) { |
5ed10a19 | 696 | |
d5ebcf65 | 697 | _cleanup_(route_freep) Route *route = NULL; |
d5ebcf65 YW |
698 | Route *existing; |
699 | int r; | |
700 | ||
701 | assert(link); | |
702 | assert(addr); | |
a27588d4 | 703 | assert(IN_SET(source, NETWORK_CONFIG_SOURCE_DHCP4, NETWORK_CONFIG_SOURCE_DHCP6)); |
5ed10a19 | 704 | assert(server_address); |
a27588d4 YW |
705 | assert(counter); |
706 | assert(callback); | |
d5ebcf65 | 707 | |
a536ec38 YW |
708 | if (prefixlen >= 64) { |
709 | _cleanup_free_ char *buf = NULL; | |
d5ebcf65 | 710 | |
a536ec38 | 711 | (void) in6_addr_prefix_to_string(addr, prefixlen, &buf); |
a27588d4 | 712 | log_link_debug(link, "Not adding a blocking route for DHCP delegated prefix %s since the prefix has length >= 64.", |
d5ebcf65 YW |
713 | strna(buf)); |
714 | return 0; | |
715 | } | |
716 | ||
717 | r = route_new(&route); | |
718 | if (r < 0) | |
719 | return log_oom(); | |
720 | ||
a27588d4 | 721 | route->source = source; |
5ed10a19 | 722 | route->provider = *server_address; |
d5ebcf65 YW |
723 | route->family = AF_INET6; |
724 | route->dst.in6 = *addr; | |
725 | route->dst_prefixlen = prefixlen; | |
d5ebcf65 YW |
726 | route->type = RTN_UNREACHABLE; |
727 | route->protocol = RTPROT_DHCP; | |
557e1b52 | 728 | route->priority = IP6_RT_PRIO_USER; |
d5ebcf65 YW |
729 | route->lifetime_usec = lifetime_usec; |
730 | ||
731 | if (route_get(link->manager, NULL, route, &existing) < 0) | |
732 | link->dhcp6_configured = false; | |
733 | else | |
734 | route_unmark(existing); | |
735 | ||
a27588d4 | 736 | r = link_request_route(link, TAKE_PTR(route), true, counter, callback, NULL); |
a536ec38 YW |
737 | if (r < 0) { |
738 | _cleanup_free_ char *buf = NULL; | |
739 | ||
740 | (void) in6_addr_prefix_to_string(addr, prefixlen, &buf); | |
a27588d4 | 741 | return log_link_error_errno(link, r, "Failed to request unreachable route for DHCP delegated prefix %s: %m", |
d5ebcf65 | 742 | strna(buf)); |
a536ec38 | 743 | } |
d5ebcf65 YW |
744 | |
745 | return 0; | |
746 | } | |
747 | ||
e49bad01 YW |
748 | static int dhcp4_request_unreachable_route( |
749 | Link *link, | |
750 | const struct in6_addr *addr, | |
751 | uint8_t prefixlen, | |
752 | usec_t lifetime_usec, | |
753 | const union in_addr_union *server_address) { | |
754 | ||
755 | return dhcp_request_unreachable_route(link, addr, prefixlen, lifetime_usec, | |
756 | NETWORK_CONFIG_SOURCE_DHCP4, server_address, | |
757 | &link->dhcp4_messages, dhcp4_unreachable_route_handler); | |
758 | } | |
759 | ||
a27588d4 YW |
760 | static int dhcp6_request_unreachable_route( |
761 | Link *link, | |
762 | const struct in6_addr *addr, | |
763 | uint8_t prefixlen, | |
764 | usec_t lifetime_usec, | |
765 | const union in_addr_union *server_address) { | |
766 | ||
767 | return dhcp_request_unreachable_route(link, addr, prefixlen, lifetime_usec, | |
768 | NETWORK_CONFIG_SOURCE_DHCP6, server_address, | |
769 | &link->dhcp6_messages, dhcp6_unreachable_route_handler); | |
770 | } | |
771 | ||
772 | static int dhcp_pd_prefix_add(Link *link, const struct in6_addr *prefix, uint8_t prefixlen) { | |
d5ebcf65 YW |
773 | _cleanup_free_ char *buf = NULL; |
774 | struct in_addr_prefix *p; | |
775 | int r; | |
776 | ||
777 | assert(link); | |
778 | assert(prefix); | |
779 | ||
780 | p = new(struct in_addr_prefix, 1); | |
781 | if (!p) | |
782 | return log_oom(); | |
783 | ||
784 | *p = (struct in_addr_prefix) { | |
785 | .family = AF_INET6, | |
786 | .prefixlen = prefixlen, | |
787 | .address.in6 = *prefix, | |
788 | }; | |
789 | ||
790 | (void) in6_addr_prefix_to_string(prefix, prefixlen, &buf); | |
791 | ||
792 | log_link_full(link, | |
a27588d4 | 793 | set_contains(link->dhcp_pd_prefixes, p) ? LOG_DEBUG : |
d5ebcf65 | 794 | prefixlen > 64 || prefixlen < 48 ? LOG_WARNING : LOG_INFO, |
21feba0a | 795 | "DHCP: received delegated prefix %s%s", |
d5ebcf65 YW |
796 | strna(buf), |
797 | prefixlen > 64 ? " with prefix length > 64, ignoring." : | |
798 | prefixlen < 48 ? " with prefix length < 48, looks unusual.": ""); | |
799 | ||
800 | /* Store PD prefix even if prefixlen > 64, not to make logged at warning level so frequently. */ | |
a27588d4 | 801 | r = set_ensure_consume(&link->dhcp_pd_prefixes, &in_addr_prefix_hash_ops_free, p); |
d5ebcf65 | 802 | if (r < 0) |
21feba0a | 803 | return log_link_error_errno(link, r, "Failed to store DHCP delegated prefix %s: %m", strna(buf)); |
d5ebcf65 | 804 | |
a536ec38 | 805 | return 0; |
d5ebcf65 YW |
806 | } |
807 | ||
e49bad01 YW |
808 | static int dhcp4_pd_request_default_gateway_on_6rd_tunnel(Link *link, const struct in_addr *br_address, usec_t lifetime_usec) { |
809 | _cleanup_(route_freep) Route *route = NULL; | |
810 | Route *existing; | |
811 | int r; | |
812 | ||
813 | assert(link); | |
814 | assert(br_address); | |
815 | ||
816 | r = route_new(&route); | |
817 | if (r < 0) | |
818 | return log_link_debug_errno(link, r, "Failed to allocate default gateway for DHCP delegated prefix: %m"); | |
819 | ||
820 | route->source = NETWORK_CONFIG_SOURCE_DHCP_PD; | |
821 | route->family = AF_INET6; | |
822 | route->gw_family = AF_INET6; | |
823 | route->gw.in6.s6_addr32[3] = br_address->s_addr; | |
824 | route->scope = RT_SCOPE_UNIVERSE; | |
825 | route->protocol = RTPROT_DHCP; | |
826 | route->priority = IP6_RT_PRIO_USER; | |
827 | route->lifetime_usec = lifetime_usec; | |
828 | ||
829 | if (route_get(NULL, link, route, &existing) < 0) /* This is a new route. */ | |
830 | link->dhcp_pd_configured = false; | |
831 | else | |
832 | route_unmark(existing); | |
833 | ||
834 | r = link_request_route(link, TAKE_PTR(route), true, &link->dhcp_pd_messages, | |
835 | dhcp_pd_route_handler, NULL); | |
836 | if (r < 0) | |
837 | return log_link_debug_errno(link, r, "Failed to request default gateway for DHCP delegated prefix: %m"); | |
838 | ||
839 | return 0; | |
840 | } | |
841 | ||
842 | static void dhcp4_calculate_pd_prefix( | |
843 | const struct in_addr *ipv4address, | |
844 | uint8_t ipv4masklen, | |
845 | const struct in6_addr *sixrd_prefix, | |
846 | uint8_t sixrd_prefixlen, | |
847 | struct in6_addr *ret_pd_prefix, | |
848 | uint8_t *ret_pd_prefixlen) { | |
849 | ||
850 | struct in6_addr pd_prefix; | |
851 | ||
852 | assert(ipv4address); | |
853 | assert(ipv4masklen <= 32); | |
854 | assert(sixrd_prefix); | |
855 | assert(32 - ipv4masklen + sixrd_prefixlen <= 128); | |
856 | assert(ret_pd_prefix); | |
857 | ||
858 | pd_prefix = *sixrd_prefix; | |
859 | for (unsigned i = 0; i < (unsigned) (32 - ipv4masklen); i++) | |
860 | if (ipv4address->s_addr & htobe32(UINT32_C(1) << (32 - ipv4masklen - i - 1))) | |
861 | pd_prefix.s6_addr[(i + sixrd_prefixlen) / 8] |= 1 << (7 - (i + sixrd_prefixlen) % 8); | |
862 | ||
863 | *ret_pd_prefix = pd_prefix; | |
864 | if (ret_pd_prefixlen) | |
865 | *ret_pd_prefixlen = 32 - ipv4masklen + sixrd_prefixlen; | |
866 | } | |
867 | ||
868 | static int dhcp4_pd_assign_subnet_prefix(Link *link, Link *uplink) { | |
869 | uint8_t ipv4masklen, sixrd_prefixlen, pd_prefixlen; | |
870 | struct in6_addr sixrd_prefix, pd_prefix; | |
871 | const struct in_addr *br_addresses; | |
872 | struct in_addr ipv4address; | |
873 | uint32_t lifetime_sec; | |
874 | usec_t lifetime_usec; | |
875 | int r; | |
876 | ||
877 | assert(link); | |
878 | assert(uplink); | |
879 | assert(uplink->dhcp_lease); | |
880 | ||
881 | r = sd_dhcp_lease_get_address(uplink->dhcp_lease, &ipv4address); | |
882 | if (r < 0) | |
883 | return log_link_warning_errno(uplink, r, "Failed to get DHCPv4 address: %m"); | |
884 | ||
885 | r = sd_dhcp_lease_get_lifetime(uplink->dhcp_lease, &lifetime_sec); | |
886 | if (r < 0) | |
887 | return log_link_warning_errno(uplink, r, "Failed to get lifetime of DHCPv4 lease: %m"); | |
888 | ||
889 | lifetime_usec = usec_add(lifetime_sec * USEC_PER_SEC, now(clock_boottime_or_monotonic())); | |
890 | ||
891 | r = sd_dhcp_lease_get_6rd(uplink->dhcp_lease, &ipv4masklen, &sixrd_prefixlen, &sixrd_prefix, &br_addresses, NULL); | |
892 | if (r < 0) | |
21feba0a | 893 | return log_link_warning_errno(uplink, r, "Failed to get DHCPv4 6rd option: %m"); |
e49bad01 YW |
894 | |
895 | dhcp4_calculate_pd_prefix(&ipv4address, ipv4masklen, &sixrd_prefix, sixrd_prefixlen, &pd_prefix, &pd_prefixlen); | |
896 | ||
897 | if (pd_prefixlen > 64) | |
898 | return 0; | |
899 | ||
900 | r = dhcp_pd_prepare(link); | |
901 | if (r <= 0) | |
902 | return r; | |
903 | ||
904 | if (streq_ptr(uplink->dhcp4_6rd_tunnel_name, link->ifname)) { | |
e49bad01 YW |
905 | r = dhcp4_pd_request_default_gateway_on_6rd_tunnel(link, &br_addresses[0], lifetime_usec); |
906 | if (r < 0) | |
907 | return r; | |
e49bad01 YW |
908 | } |
909 | ||
38488bab YW |
910 | r = dhcp_pd_assign_subnet_prefix(link, &pd_prefix, pd_prefixlen, lifetime_usec, lifetime_usec, /* is_uplink = */ false); |
911 | if (r < 0) | |
912 | return r; | |
913 | ||
e49bad01 YW |
914 | return dhcp_pd_finalize(link); |
915 | } | |
916 | ||
917 | static int dhcp4_pd_6rd_tunnel_create_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { | |
918 | int r; | |
919 | ||
920 | assert(m); | |
921 | assert(link); | |
922 | assert(link->manager); | |
923 | assert(link->dhcp4_6rd_tunnel_name); | |
924 | ||
925 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
926 | return 0; | |
927 | ||
928 | r = sd_netlink_message_get_errno(m); | |
929 | if (r < 0) { | |
21feba0a | 930 | log_link_message_warning_errno(link, m, r, "Failed to create tunnel device for DHCPv4 6rd"); |
e49bad01 YW |
931 | link_enter_failed(link); |
932 | return 0; | |
933 | } | |
934 | ||
935 | return 0; | |
936 | } | |
937 | ||
938 | int dhcp4_pd_prefix_acquired(Link *uplink) { | |
939 | _cleanup_free_ char *tunnel_name = NULL; | |
940 | uint8_t ipv4masklen, sixrd_prefixlen, pd_prefixlen; | |
941 | struct in6_addr sixrd_prefix, pd_prefix; | |
942 | struct in_addr ipv4address; | |
943 | union in_addr_union server_address; | |
21feba0a | 944 | const struct in_addr *br_addresses; |
e49bad01 YW |
945 | uint32_t lifetime_sec; |
946 | usec_t lifetime_usec; | |
947 | Link *link; | |
948 | int r; | |
949 | ||
950 | assert(uplink); | |
951 | assert(uplink->dhcp_lease); | |
952 | ||
953 | r = sd_dhcp_lease_get_address(uplink->dhcp_lease, &ipv4address); | |
954 | if (r < 0) | |
955 | return log_link_warning_errno(uplink, r, "Failed to get DHCPv4 address: %m"); | |
956 | ||
957 | r = sd_dhcp_lease_get_lifetime(uplink->dhcp_lease, &lifetime_sec); | |
958 | if (r < 0) | |
959 | return log_link_warning_errno(uplink, r, "Failed to get lifetime of DHCPv4 lease: %m"); | |
960 | ||
961 | lifetime_usec = usec_add(lifetime_sec * USEC_PER_SEC, now(clock_boottime_or_monotonic())); | |
962 | ||
963 | r = sd_dhcp_lease_get_server_identifier(uplink->dhcp_lease, &server_address.in); | |
964 | if (r < 0) | |
965 | return log_link_warning_errno(uplink, r, "Failed to get server address of DHCPv4 lease: %m"); | |
966 | ||
21feba0a | 967 | r = sd_dhcp_lease_get_6rd(uplink->dhcp_lease, &ipv4masklen, &sixrd_prefixlen, &sixrd_prefix, &br_addresses, NULL); |
e49bad01 | 968 | if (r < 0) |
21feba0a YW |
969 | return log_link_warning_errno(uplink, r, "Failed to get DHCPv4 6rd option: %m"); |
970 | ||
971 | if (DEBUG_LOGGING) { | |
972 | _cleanup_free_ char *buf = NULL; | |
973 | ||
974 | (void) in6_addr_prefix_to_string(&sixrd_prefix, sixrd_prefixlen, &buf); | |
975 | log_link_debug(uplink, "DHCPv4: 6rd option is acquired: IPv4_masklen=%u, 6rd_prefix=%s, br_address="IPV4_ADDRESS_FMT_STR, | |
976 | ipv4masklen, strna(buf), IPV4_ADDRESS_FMT_VAL(*br_addresses)); | |
977 | } | |
e49bad01 YW |
978 | |
979 | /* Calculate PD prefix */ | |
980 | dhcp4_calculate_pd_prefix(&ipv4address, ipv4masklen, &sixrd_prefix, sixrd_prefixlen, &pd_prefix, &pd_prefixlen); | |
981 | ||
982 | /* Register and log PD prefix */ | |
983 | r = dhcp_pd_prefix_add(uplink, &pd_prefix, pd_prefixlen); | |
984 | if (r < 0) | |
985 | return r; | |
986 | ||
987 | /* Request unreachable route */ | |
988 | r = dhcp4_request_unreachable_route(uplink, &pd_prefix, pd_prefixlen, lifetime_usec, &server_address); | |
989 | if (r < 0) | |
990 | return r; | |
991 | ||
992 | /* Generate 6rd SIT tunnel device name. */ | |
993 | r = dhcp4_pd_create_6rd_tunnel_name(uplink, &tunnel_name); | |
994 | if (r < 0) | |
995 | return r; | |
996 | ||
997 | /* Remove old tunnel device if exists. */ | |
998 | if (!streq_ptr(uplink->dhcp4_6rd_tunnel_name, tunnel_name)) { | |
999 | Link *old_tunnel; | |
1000 | ||
1001 | if (uplink->dhcp4_6rd_tunnel_name && | |
1002 | link_get_by_name(uplink->manager, uplink->dhcp4_6rd_tunnel_name, &old_tunnel) >= 0) | |
1003 | (void) link_remove(old_tunnel); | |
1004 | ||
1005 | free_and_replace(uplink->dhcp4_6rd_tunnel_name, tunnel_name); | |
1006 | } | |
1007 | ||
1008 | /* Create 6rd SIT tunnel device if it does not exist yet. */ | |
1009 | if (link_get_by_name(uplink->manager, uplink->dhcp4_6rd_tunnel_name, NULL) < 0) { | |
1010 | r = dhcp4_pd_create_6rd_tunnel(uplink, dhcp4_pd_6rd_tunnel_create_handler); | |
1011 | if (r < 0) | |
1012 | return r; | |
1013 | } | |
1014 | ||
1015 | /* Then, assign subnet prefixes to downstream interfaces. */ | |
1016 | HASHMAP_FOREACH(link, uplink->manager->links_by_index) { | |
1017 | if (!dhcp_pd_is_uplink(link, uplink, /* accept_auto = */ true)) | |
1018 | continue; | |
1019 | ||
1020 | r = dhcp4_pd_assign_subnet_prefix(link, uplink); | |
1021 | if (r < 0) { | |
1022 | /* When failed on the upstream interface (i.e., the case link == uplink), | |
1023 | * immediately abort the assignment of the prefixes. As, the all assigned | |
1024 | * prefixes will be dropped soon in link_enter_failed(), and it is meaningless | |
1025 | * to continue the assignment. */ | |
1026 | if (link == uplink) | |
1027 | return r; | |
1028 | ||
1029 | link_enter_failed(link); | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | return 0; | |
1034 | } | |
1035 | ||
a27588d4 | 1036 | static int dhcp6_pd_assign_subnet_prefixes(Link *link, Link *uplink) { |
d5ebcf65 | 1037 | usec_t timestamp_usec; |
d5ebcf65 YW |
1038 | int r; |
1039 | ||
5014e660 YW |
1040 | assert(link); |
1041 | assert(uplink); | |
1042 | assert(uplink->dhcp6_lease); | |
d5ebcf65 | 1043 | |
a27588d4 | 1044 | r = dhcp_pd_prepare(link); |
c3cd5351 | 1045 | if (r <= 0) |
5014e660 | 1046 | return r; |
d5ebcf65 | 1047 | |
c3cd5351 | 1048 | r = sd_dhcp6_lease_get_timestamp(uplink->dhcp6_lease, clock_boottime_or_monotonic(), ×tamp_usec); |
5014e660 YW |
1049 | if (r < 0) |
1050 | return r; | |
d5ebcf65 | 1051 | |
5014e660 | 1052 | for (sd_dhcp6_lease_reset_pd_prefix_iter(uplink->dhcp6_lease);;) { |
d5ebcf65 YW |
1053 | uint32_t lifetime_preferred_sec, lifetime_valid_sec; |
1054 | usec_t lifetime_preferred_usec, lifetime_valid_usec; | |
1055 | struct in6_addr pd_prefix; | |
1056 | uint8_t pd_prefix_len; | |
1057 | ||
5014e660 | 1058 | r = sd_dhcp6_lease_get_pd(uplink->dhcp6_lease, &pd_prefix, &pd_prefix_len, |
d5ebcf65 YW |
1059 | &lifetime_preferred_sec, &lifetime_valid_sec); |
1060 | if (r < 0) | |
1061 | break; | |
1062 | ||
5014e660 | 1063 | if (pd_prefix_len > 64) |
d5ebcf65 YW |
1064 | continue; |
1065 | ||
d5ebcf65 YW |
1066 | /* Mask prefix for safety. */ |
1067 | r = in6_addr_mask(&pd_prefix, pd_prefix_len); | |
1068 | if (r < 0) | |
5014e660 | 1069 | return r; |
d5ebcf65 | 1070 | |
21cf8e9e YW |
1071 | lifetime_preferred_usec = usec_add(lifetime_preferred_sec * USEC_PER_SEC, timestamp_usec); |
1072 | lifetime_valid_usec = usec_add(lifetime_valid_sec * USEC_PER_SEC, timestamp_usec); | |
d5ebcf65 | 1073 | |
38488bab YW |
1074 | r = dhcp_pd_assign_subnet_prefix(link, &pd_prefix, pd_prefix_len, |
1075 | lifetime_preferred_usec, lifetime_valid_usec, | |
1076 | /* is_uplink = */ link == uplink); | |
d5ebcf65 YW |
1077 | if (r < 0) |
1078 | return r; | |
1079 | } | |
1080 | ||
a27588d4 | 1081 | return dhcp_pd_finalize(link); |
d5ebcf65 YW |
1082 | } |
1083 | ||
a27588d4 | 1084 | int dhcp6_pd_prefix_acquired(Link *uplink) { |
5ed10a19 | 1085 | union in_addr_union server_address; |
d5ebcf65 | 1086 | usec_t timestamp_usec; |
d5ebcf65 | 1087 | Link *link; |
d5ebcf65 YW |
1088 | int r; |
1089 | ||
a27588d4 YW |
1090 | assert(uplink); |
1091 | assert(uplink->dhcp6_lease); | |
d5ebcf65 | 1092 | |
a27588d4 | 1093 | r = sd_dhcp6_lease_get_server_address(uplink->dhcp6_lease, &server_address.in6); |
5ed10a19 | 1094 | if (r < 0) |
a27588d4 | 1095 | return log_link_warning_errno(uplink, r, "Failed to get server address of DHCPv6 lease: %m"); |
5ed10a19 | 1096 | |
a27588d4 | 1097 | r = sd_dhcp6_lease_get_timestamp(uplink->dhcp6_lease, clock_boottime_or_monotonic(), ×tamp_usec); |
d5ebcf65 | 1098 | if (r < 0) |
a27588d4 | 1099 | return log_link_warning_errno(uplink, r, "Failed to get timestamp of DHCPv6 lease: %m"); |
d5ebcf65 | 1100 | |
41664456 | 1101 | /* First, logs acquired prefixes and request unreachable routes. */ |
a27588d4 | 1102 | for (sd_dhcp6_lease_reset_pd_prefix_iter(uplink->dhcp6_lease);;) { |
d5ebcf65 | 1103 | uint32_t lifetime_preferred_sec, lifetime_valid_sec; |
41664456 | 1104 | usec_t lifetime_valid_usec; |
d5ebcf65 YW |
1105 | struct in6_addr pd_prefix; |
1106 | uint8_t pd_prefix_len; | |
1107 | ||
a27588d4 | 1108 | r = sd_dhcp6_lease_get_pd(uplink->dhcp6_lease, &pd_prefix, &pd_prefix_len, |
d5ebcf65 YW |
1109 | &lifetime_preferred_sec, &lifetime_valid_sec); |
1110 | if (r < 0) | |
1111 | break; | |
1112 | ||
d5ebcf65 YW |
1113 | /* Mask prefix for safety. */ |
1114 | r = in6_addr_mask(&pd_prefix, pd_prefix_len); | |
41664456 | 1115 | if (r < 0) |
21feba0a | 1116 | return log_link_error_errno(uplink, r, "Failed to mask DHCPv6 delegated prefix: %m"); |
41664456 | 1117 | |
d5ebcf65 YW |
1118 | lifetime_valid_usec = usec_add(lifetime_valid_sec * USEC_PER_SEC, timestamp_usec); |
1119 | ||
a27588d4 | 1120 | r = dhcp_pd_prefix_add(uplink, &pd_prefix, pd_prefix_len); |
d5ebcf65 YW |
1121 | if (r < 0) |
1122 | return r; | |
1123 | ||
a27588d4 | 1124 | r = dhcp6_request_unreachable_route(uplink, &pd_prefix, pd_prefix_len, lifetime_valid_usec, &server_address); |
d5ebcf65 YW |
1125 | if (r < 0) |
1126 | return r; | |
1127 | } | |
1128 | ||
41664456 | 1129 | /* Then, assign subnet prefixes. */ |
a27588d4 YW |
1130 | HASHMAP_FOREACH(link, uplink->manager->links_by_index) { |
1131 | if (!dhcp_pd_is_uplink(link, uplink, /* accept_auto = */ true)) | |
41664456 YW |
1132 | continue; |
1133 | ||
a27588d4 | 1134 | r = dhcp6_pd_assign_subnet_prefixes(link, uplink); |
d5ebcf65 | 1135 | if (r < 0) { |
a27588d4 | 1136 | /* When failed on the upstream interface (i.e., the case link == uplink), |
41664456 YW |
1137 | * immediately abort the assignment of the prefixes. As, the all assigned |
1138 | * prefixes will be dropped soon in link_enter_failed(), and it is meaningless | |
1139 | * to continue the assignment. */ | |
a27588d4 | 1140 | if (link == uplink) |
d5ebcf65 YW |
1141 | return r; |
1142 | ||
1143 | link_enter_failed(link); | |
1144 | } | |
1145 | } | |
d5ebcf65 YW |
1146 | |
1147 | return 0; | |
1148 | } | |
1149 | ||
e49bad01 YW |
1150 | static bool dhcp4_pd_uplink_is_ready(Link *link) { |
1151 | assert(link); | |
1152 | ||
1153 | if (!link->network) | |
1154 | return false; | |
1155 | ||
1156 | if (!link->network->dhcp_use_6rd) | |
1157 | return false; | |
1158 | ||
1159 | if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) | |
1160 | return false; | |
1161 | ||
1162 | if (!link->dhcp_client) | |
1163 | return false; | |
1164 | ||
1165 | if (sd_dhcp_client_is_running(link->dhcp_client) <= 0) | |
1166 | return false; | |
1167 | ||
1168 | if (!link->dhcp_lease) | |
1169 | return false; | |
1170 | ||
1171 | return dhcp4_lease_has_pd_prefix(link->dhcp_lease); | |
1172 | } | |
1173 | ||
d5ebcf65 YW |
1174 | static bool dhcp6_pd_uplink_is_ready(Link *link) { |
1175 | assert(link); | |
1176 | ||
1177 | if (!link->network) | |
1178 | return false; | |
1179 | ||
1180 | if (!link->network->dhcp6_use_pd_prefix) | |
1181 | return false; | |
1182 | ||
1183 | if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) | |
1184 | return false; | |
1185 | ||
1186 | if (!link->dhcp6_client) | |
1187 | return false; | |
1188 | ||
1189 | if (sd_dhcp6_client_is_running(link->dhcp6_client) <= 0) | |
1190 | return false; | |
1191 | ||
1192 | if (!link->dhcp6_lease) | |
1193 | return false; | |
1194 | ||
1195 | return dhcp6_lease_has_pd_prefix(link->dhcp6_lease); | |
1196 | } | |
1197 | ||
a27588d4 | 1198 | int dhcp_pd_find_uplink(Link *link, Link **ret) { |
d5ebcf65 YW |
1199 | Link *uplink = NULL; |
1200 | int r = 0; | |
1201 | ||
1202 | assert(link); | |
1203 | assert(link->manager); | |
a27588d4 | 1204 | assert(link_dhcp_pd_is_enabled(link)); |
d5ebcf65 YW |
1205 | assert(ret); |
1206 | ||
a27588d4 YW |
1207 | if (link->network->dhcp_pd_uplink_name) |
1208 | r = link_get_by_name(link->manager, link->network->dhcp_pd_uplink_name, &uplink); | |
1209 | else if (link->network->dhcp_pd_uplink_index > 0) | |
1210 | r = link_get_by_index(link->manager, link->network->dhcp_pd_uplink_index, &uplink); | |
1211 | else if (link->network->dhcp_pd_uplink_index == UPLINK_INDEX_SELF) | |
d5ebcf65 YW |
1212 | uplink = link; |
1213 | if (r < 0) | |
1214 | return r; | |
1215 | ||
1216 | if (uplink) { | |
e49bad01 YW |
1217 | if (dhcp4_pd_uplink_is_ready(uplink)) { |
1218 | *ret = uplink; | |
1219 | return AF_INET; | |
1220 | } | |
d5ebcf65 | 1221 | |
e49bad01 YW |
1222 | if (dhcp6_pd_uplink_is_ready(uplink)) { |
1223 | *ret = uplink; | |
1224 | return AF_INET6; | |
1225 | } | |
1226 | ||
1227 | return -EBUSY; | |
d5ebcf65 YW |
1228 | } |
1229 | ||
1230 | HASHMAP_FOREACH(uplink, link->manager->links_by_index) { | |
d5ebcf65 | 1231 | /* Assume that there exists at most one link which acquired delegated prefixes. */ |
e49bad01 YW |
1232 | if (dhcp4_pd_uplink_is_ready(uplink)) { |
1233 | *ret = uplink; | |
1234 | return AF_INET; | |
1235 | } | |
1236 | ||
1237 | if (dhcp6_pd_uplink_is_ready(uplink)) { | |
1238 | *ret = uplink; | |
1239 | return AF_INET6; | |
1240 | } | |
d5ebcf65 YW |
1241 | } |
1242 | ||
1243 | return -ENODEV; | |
1244 | } | |
1245 | ||
a27588d4 | 1246 | int dhcp_request_prefix_delegation(Link *link) { |
d5ebcf65 | 1247 | Link *uplink; |
e49bad01 | 1248 | int r; |
d5ebcf65 YW |
1249 | |
1250 | assert(link); | |
1251 | ||
a27588d4 | 1252 | if (!link_dhcp_pd_is_enabled(link)) |
d5ebcf65 YW |
1253 | return 0; |
1254 | ||
e49bad01 YW |
1255 | r = dhcp_pd_find_uplink(link, &uplink); |
1256 | if (r < 0) | |
d5ebcf65 YW |
1257 | return 0; |
1258 | ||
e49bad01 YW |
1259 | log_link_debug(link, "Requesting subnets of delegated prefixes acquired by DHCPv%c client on %s", |
1260 | r == AF_INET ? '4' : '6', uplink->ifname); | |
1261 | ||
1262 | return r == AF_INET ? | |
1263 | dhcp4_pd_assign_subnet_prefix(link, uplink) : | |
1264 | dhcp6_pd_assign_subnet_prefixes(link, uplink); | |
d5ebcf65 YW |
1265 | } |
1266 | ||
a27588d4 | 1267 | int config_parse_dhcp_pd_subnet_id( |
d5ebcf65 YW |
1268 | const char *unit, |
1269 | const char *filename, | |
1270 | unsigned line, | |
1271 | const char *section, | |
1272 | unsigned section_line, | |
1273 | const char *lvalue, | |
1274 | int ltype, | |
1275 | const char *rvalue, | |
1276 | void *data, | |
1277 | void *userdata) { | |
1278 | ||
1279 | int64_t *p = data; | |
1280 | uint64_t t; | |
1281 | int r; | |
1282 | ||
1283 | assert(filename); | |
1284 | assert(lvalue); | |
1285 | assert(rvalue); | |
1286 | assert(data); | |
1287 | ||
1288 | if (isempty(rvalue) || streq(rvalue, "auto")) { | |
1289 | *p = -1; | |
1290 | return 0; | |
1291 | } | |
1292 | ||
1293 | r = safe_atoux64(rvalue, &t); | |
1294 | if (r < 0) { | |
1295 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1296 | "Failed to parse %s=, ignoring assignment: %s", | |
1297 | lvalue, rvalue); | |
1298 | return 0; | |
1299 | } | |
1300 | if (t > INT64_MAX) { | |
1301 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1302 | "Invalid subnet id '%s', ignoring assignment.", | |
1303 | rvalue); | |
1304 | return 0; | |
1305 | } | |
1306 | ||
1307 | *p = (int64_t) t; | |
1308 | ||
1309 | return 0; | |
1310 | } |