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