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