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