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