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