]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-dhcp6.c
network: make link_request_set_nexthop() called from link_request_set_routes() or...
[thirdparty/systemd.git] / src / network / networkd-dhcp6.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2014 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <netinet/in.h>
7 #include <linux/if.h>
8 #include <linux/if_arp.h>
9
10 #include "sd-dhcp6-client.h"
11
12 #include "escape.h"
13 #include "hashmap.h"
14 #include "hostname-util.h"
15 #include "missing_network.h"
16 #include "network-internal.h"
17 #include "networkd-dhcp6.h"
18 #include "networkd-link.h"
19 #include "networkd-manager.h"
20 #include "networkd-radv.h"
21 #include "siphash24.h"
22 #include "string-table.h"
23 #include "string-util.h"
24 #include "radv-internal.h"
25 #include "web-util.h"
26
27 static Link *dhcp6_prefix_get(Manager *m, struct in6_addr *addr);
28 static int dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link);
29 static int dhcp6_prefix_remove_all(Manager *m, Link *link);
30 static int dhcp6_assign_delegated_prefix(Link *link, const struct in6_addr *prefix,
31 uint8_t prefix_len,
32 uint32_t lifetime_preferred,
33 uint32_t lifetime_valid);
34
35 bool dhcp6_get_prefix_delegation(Link *link) {
36 if (!link->network)
37 return false;
38
39 return IN_SET(link->network->router_prefix_delegation,
40 RADV_PREFIX_DELEGATION_DHCP6,
41 RADV_PREFIX_DELEGATION_BOTH);
42 }
43
44 static bool dhcp6_has_preferred_subnet_id(Link *link) {
45 if (!link->network)
46 return false;
47
48 return link->network->router_prefix_subnet_id >= 0;
49 }
50
51 static int dhcp6_get_preferred_delegated_prefix(
52 Manager* manager,
53 Link *link,
54 const struct in6_addr *pd_prefix,
55 uint8_t pd_prefix_len,
56 struct in6_addr *ret_addr) {
57
58 int64_t subnet_id = link->network->router_prefix_subnet_id;
59 uint8_t prefix_bits = 64 - pd_prefix_len;
60 uint64_t n_prefixes = UINT64_C(1) << prefix_bits;
61 _cleanup_free_ char *assigned_buf = NULL;
62 union in_addr_union pd_prefix_union = {
63 .in6 = *pd_prefix,
64 };
65 /* We start off with the original PD prefix we have been assigned and
66 * iterate from there */
67 union in_addr_union prefix = {
68 .in6 = *pd_prefix,
69 };
70 int r;
71
72 assert(pd_prefix_len <= 64);
73 assert(manager);
74 assert(link);
75 assert(link->network);
76
77 if (subnet_id >= 0) {
78 /* If the link has a preference for a particular subnet id try to allocate that */
79 if ((uint64_t) subnet_id >= n_prefixes)
80 return log_link_debug_errno(link,
81 SYNTHETIC_ERRNO(ERANGE),
82 "subnet id %" PRIi64 " is out of range. Only have %" PRIu64 " subnets.",
83 subnet_id,
84 n_prefixes);
85
86 r = in_addr_prefix_nth(AF_INET6, &prefix, 64, subnet_id);
87 if (r < 0)
88 return log_link_debug_errno(link,
89 r,
90 "subnet id %" PRIi64 " is out of range. Only have %" PRIu64 " subnets.",
91 subnet_id,
92 n_prefixes);
93
94 /* Verify that the prefix we did calculate fits in the pd prefix.
95 * This should not fail as we checked the prefix size beforehand */
96 assert_se(in_addr_prefix_covers(AF_INET6, &pd_prefix_union, pd_prefix_len, &prefix) > 0);
97
98 Link* assigned_link = dhcp6_prefix_get(manager, &prefix.in6);
99
100 (void) in_addr_to_string(AF_INET6, &prefix, &assigned_buf);
101
102 if (assigned_link && assigned_link != link)
103 return log_link_error_errno(link, SYNTHETIC_ERRNO(EAGAIN),
104 "The requested prefix %s is already assigned to another link: %s",
105 strnull(assigned_buf),
106 strnull(assigned_link->ifname));
107
108 *ret_addr = prefix.in6;
109
110 log_link_debug(link, "The requested prefix %s is available. Using it.",
111 strnull(assigned_buf));
112 return 0;
113 }
114
115 for (uint64_t n = 0; n < n_prefixes; n++) {
116 /* if we do not have an allocation preference just iterate
117 * through the address space and return the first free prefix. */
118 Link* assigned_link = dhcp6_prefix_get(manager, &prefix.in6);
119
120 if (!assigned_link || assigned_link == link) {
121 *ret_addr = prefix.in6;
122 return 0;
123 }
124
125 r = in_addr_prefix_next(AF_INET6, &prefix, 64);
126 if (r < 0)
127 return log_link_error_errno(link, r, "Can't allocate another prefix. Out of address space?: %m");
128 }
129
130 return log_link_warning_errno(link, SYNTHETIC_ERRNO(ERANGE), "Couldn't find a suitable prefix. Ran out of address space.");
131 }
132
133 static bool dhcp6_enable_prefix_delegation(Link *dhcp6_link) {
134 Manager *manager;
135 Link *l;
136 Iterator i;
137
138 assert(dhcp6_link);
139
140 manager = dhcp6_link->manager;
141 assert(manager);
142
143 HASHMAP_FOREACH(l, manager->links, i) {
144 if (l == dhcp6_link)
145 continue;
146
147 if (!dhcp6_get_prefix_delegation(l))
148 continue;
149
150 return true;
151 }
152
153 return false;
154 }
155
156 static int dhcp6_lease_information_acquired(sd_dhcp6_client *client, Link *link) {
157 return 0;
158 }
159
160 static int dhcp6_pd_prefix_assign(Link *link, struct in6_addr *prefix,
161 uint8_t prefix_len,
162 uint32_t lifetime_preferred,
163 uint32_t lifetime_valid) {
164 int r;
165
166 r = radv_add_prefix(link, prefix, prefix_len, lifetime_preferred, lifetime_valid);
167 if (r < 0)
168 return r;
169
170 r = dhcp6_prefix_add(link->manager, prefix, link);
171 if (r < 0)
172 return r;
173
174 r = dhcp6_assign_delegated_prefix(link, prefix, prefix_len, lifetime_preferred, lifetime_valid);
175 if (r < 0)
176 return r;
177
178 return 0;
179 }
180
181 static int dhcp6_route_remove_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
182 int r;
183
184 assert(link);
185
186 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
187 return 1;
188
189 r = sd_netlink_message_get_errno(m);
190 if (r < 0)
191 log_link_message_warning_errno(link, m, r, "Received error on unreachable route removal for DHCPv6 delegated subnet");
192
193 return 1;
194 }
195
196 int dhcp6_lease_pd_prefix_lost(sd_dhcp6_client *client, Link* link) {
197 uint32_t lifetime_preferred, lifetime_valid;
198 union in_addr_union pd_prefix;
199 uint8_t pd_prefix_len;
200 sd_dhcp6_lease *lease;
201 int r;
202
203 r = sd_dhcp6_client_get_lease(client, &lease);
204 if (r < 0)
205 return r;
206
207 sd_dhcp6_lease_reset_pd_prefix_iter(lease);
208
209 while (sd_dhcp6_lease_get_pd(lease, &pd_prefix.in6, &pd_prefix_len,
210 &lifetime_preferred,
211 &lifetime_valid) >= 0) {
212 _cleanup_free_ char *buf = NULL;
213 _cleanup_(route_freep) Route *route = NULL;
214
215 if (pd_prefix_len >= 64)
216 continue;
217
218 (void) in_addr_to_string(AF_INET6, &pd_prefix, &buf);
219
220 r = route_new(&route);
221 if (r < 0)
222 return r;
223
224 route->family = AF_INET6;
225 route->dst = pd_prefix;
226 route->dst_prefixlen = pd_prefix_len;
227 route->type = RTN_UNREACHABLE;
228
229 r = route_remove(route, link, dhcp6_route_remove_handler);
230 if (r < 0) {
231 log_link_warning_errno(link, r, "Cannot delete unreachable route for DHCPv6 delegated subnet %s/%u: %m",
232 strnull(buf),
233 pd_prefix_len);
234 continue;
235 }
236
237 log_link_debug(link, "Removing unreachable route %s/%u",
238 strnull(buf), pd_prefix_len);
239 }
240
241 return 0;
242 }
243
244 static int dhcp6_pd_prefix_distribute(Link *dhcp6_link,
245 struct in6_addr *pd_prefix,
246 uint8_t pd_prefix_len,
247 uint32_t lifetime_preferred,
248 uint32_t lifetime_valid,
249 bool assign_preferred_subnet_id) {
250
251 _cleanup_free_ char *assigned_buf = NULL, *buf = NULL;
252 Manager *manager = dhcp6_link->manager;
253 union in_addr_union prefix = {
254 .in6 = *pd_prefix,
255 };
256 bool pool_depleted = false;
257 uint64_t n_prefixes;
258 Iterator i;
259 Link *link;
260 int r;
261
262 assert(manager);
263 assert(pd_prefix_len <= 64);
264
265 r = in_addr_mask(AF_INET6, &prefix, pd_prefix_len);
266 if (r < 0)
267 return r;
268
269 n_prefixes = UINT64_C(1) << (64 - pd_prefix_len);
270
271 (void) in_addr_to_string(AF_INET6, &prefix, &buf);
272 log_link_debug(dhcp6_link, "Assigning up to %" PRIu64 " prefixes from %s/%u",
273 n_prefixes, strnull(buf), pd_prefix_len);
274
275 HASHMAP_FOREACH(link, manager->links, i) {
276 union in_addr_union assigned_prefix;
277
278 if (link == dhcp6_link)
279 continue;
280
281 if (!dhcp6_get_prefix_delegation(link))
282 continue;
283
284 if (assign_preferred_subnet_id != dhcp6_has_preferred_subnet_id(link))
285 continue;
286
287 r = dhcp6_get_preferred_delegated_prefix(manager, link, &prefix.in6, pd_prefix_len,
288 &assigned_prefix.in6);
289
290 if (assign_preferred_subnet_id && r == -EAGAIN) {
291 /* A link has a preferred subnet_id but that one is
292 * already taken by another link. Now all the remaining
293 * links will also not obtain a prefix. */
294 pool_depleted = true;
295 continue;
296 } else if (r < 0)
297 return r;
298
299 (void) in_addr_to_string(AF_INET6, &assigned_prefix, &assigned_buf);
300 r = dhcp6_pd_prefix_assign(link, &assigned_prefix.in6, 64,
301 lifetime_preferred, lifetime_valid);
302 if (r < 0) {
303 log_link_error_errno(link, r, "Unable to assign/update prefix %s/64 from %s/%u for link: %m",
304 strnull(assigned_buf),
305 strnull(buf), pd_prefix_len);
306 } else
307 log_link_debug(link, "Assigned prefix %s/64 from %s/%u to link",
308 strnull(assigned_buf),
309 strnull(buf), pd_prefix_len);
310 }
311
312 /* If one of the link requests couldn't be fulfilled, signal that we
313 should try again with another prefix. */
314 if (pool_depleted)
315 return -EAGAIN;
316
317 return 0;
318 }
319
320 static int dhcp6_route_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
321 int r;
322
323 assert(link);
324 assert(link->dhcp6_route_messages > 0);
325
326 link->dhcp6_route_messages--;
327
328 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
329 return 1;
330
331 r = sd_netlink_message_get_errno(m);
332 if (r < 0 && r != -EEXIST) {
333 log_link_message_warning_errno(link, m, r, "Failed to add unreachable route for DHCPv6 delegated subnet");
334 link_enter_failed(link);
335 return 1;
336 }
337
338 if (link->dhcp6_route_messages == 0) {
339 log_link_debug(link, "Unreachable routes for DHCPv6 delegated subnets set");
340 link->dhcp6_route_configured = true;
341 link_check_ready(link);
342 }
343
344 return 1;
345 }
346
347 static int dhcp6_lease_pd_prefix_acquired(sd_dhcp6_client *client, Link *link) {
348 uint32_t lifetime_preferred, lifetime_valid;
349 union in_addr_union pd_prefix;
350 sd_dhcp6_lease *lease;
351 uint8_t pd_prefix_len;
352 int r;
353
354 link->dhcp6_route_configured = false;
355
356 r = sd_dhcp6_client_get_lease(client, &lease);
357 if (r < 0)
358 return r;
359
360 sd_dhcp6_lease_reset_pd_prefix_iter(lease);
361
362 while (sd_dhcp6_lease_get_pd(lease, &pd_prefix.in6, &pd_prefix_len,
363 &lifetime_preferred,
364 &lifetime_valid) >= 0) {
365
366 _cleanup_free_ char *buf = NULL;
367
368 (void) in_addr_to_string(AF_INET6, &pd_prefix, &buf);
369
370 if (pd_prefix_len > 64) {
371 log_link_debug(link, "PD Prefix length > 64, ignoring prefix %s/%u",
372 strnull(buf), pd_prefix_len);
373 continue;
374 }
375
376 if (pd_prefix_len < 48)
377 log_link_warning(link, "PD Prefix length < 48, looks unusual %s/%u",
378 strnull(buf), pd_prefix_len);
379
380 if (pd_prefix_len < 64) {
381 _cleanup_(route_freep) Route *route = NULL;
382
383 r = route_new(&route);
384 if (r < 0)
385 return r;
386
387 route->family = AF_INET6;
388 route->dst = pd_prefix;
389 route->dst_prefixlen = pd_prefix_len;
390 route->table = link_get_dhcp_route_table(link);
391 route->type = RTN_UNREACHABLE;
392
393 r = route_configure(route, link, dhcp6_route_handler);
394 if (r < 0) {
395 log_link_warning_errno(link, r, "Cannot configure unreachable route for delegated subnet %s/%u: %m",
396 strnull(buf),
397 pd_prefix_len);
398 continue;
399 }
400 if (r > 0)
401 link->dhcp6_route_messages++;
402
403 log_link_debug(link, "Configuring unreachable route for %s/%u",
404 strnull(buf), pd_prefix_len);
405 } else
406 log_link_debug(link, "Not adding a blocking route since distributed prefix is /64");
407
408 /* We are doing prefix allocation in two steps:
409 * 1. all those links that have a preferred subnet id will be assigned their subnet
410 * 2. all those links that remain will receive prefixes in sequential
411 * order. Prefixes that were previously already allocated to another
412 * link will be skipped.
413
414 * If a subnet id request couldn't be fulfilled the failure will be logged (as error)
415 * and no further attempts at obtaining a prefix will be made.
416
417 * The assignment has to be split in two phases since subnet id
418 * preferences should be honored. Meaning that any subnet id should be
419 * handed out to the requesting link and not to some link that didn't
420 * specify any preference. */
421
422 r = dhcp6_pd_prefix_distribute(link, &pd_prefix.in6,
423 pd_prefix_len,
424 lifetime_preferred,
425 lifetime_valid,
426 true);
427 if (r < 0 && r != -EAGAIN)
428 return r;
429
430 /* if r == -EAGAIN then the allocation failed because we ran
431 * out of addresses for the preferred subnet id's. This doesn't
432 * mean we can't fulfill other prefix requests.
433 *
434 * Since we do not have dedicated lists of links that request
435 * specific subnet id's and those that accept any prefix we
436 * *must* reset the iterator to the start as otherwise some
437 * links might not get their requested prefix. */
438
439 r = dhcp6_pd_prefix_distribute(link, &pd_prefix.in6,
440 pd_prefix_len,
441 lifetime_preferred,
442 lifetime_valid,
443 false);
444 if (r < 0 && r != -EAGAIN)
445 return r;
446
447 /* If the prefix distribution did return -EAGAIN we will try to
448 * fulfill those with the next available pd delegated prefix. */
449 }
450
451 if (link->dhcp6_route_messages == 0) {
452 link->dhcp6_route_configured = true;
453 link_check_ready(link);
454 } else {
455 log_link_debug(link, "Setting unreachable routes for DHCPv6 delegated subnets");
456 link_set_state(link, LINK_STATE_CONFIGURING);
457 }
458
459 return 0;
460 }
461
462 int dhcp6_request_prefix_delegation(Link *link) {
463 Link *l;
464 Iterator i;
465
466 assert_return(link, -EINVAL);
467 assert_return(link->manager, -EOPNOTSUPP);
468
469 if (dhcp6_get_prefix_delegation(link) <= 0)
470 return 0;
471
472 log_link_debug(link, "Requesting DHCPv6 prefixes to be delegated for new link");
473
474 HASHMAP_FOREACH(l, link->manager->links, i) {
475 int r, enabled;
476
477 if (l == link)
478 continue;
479
480 if (!l->dhcp6_client)
481 continue;
482
483 r = sd_dhcp6_client_get_prefix_delegation(l->dhcp6_client, &enabled);
484 if (r < 0) {
485 log_link_warning_errno(l, r, "Cannot get prefix delegation when adding new link: %m");
486 continue;
487 }
488
489 if (enabled == 0) {
490 r = sd_dhcp6_client_set_prefix_delegation(l->dhcp6_client, 1);
491 if (r < 0) {
492 log_link_warning_errno(l, r, "Cannot enable prefix delegation when adding new link: 5m");
493 continue;
494 }
495 }
496
497 r = sd_dhcp6_client_is_running(l->dhcp6_client);
498 if (r <= 0)
499 continue;
500
501 if (enabled != 0) {
502 log_link_debug(l, "Requesting re-assignment of delegated prefixes after adding new link");
503 (void) dhcp6_lease_pd_prefix_acquired(l->dhcp6_client, l);
504
505 continue;
506 }
507
508 r = sd_dhcp6_client_stop(l->dhcp6_client);
509 if (r < 0) {
510 log_link_warning_errno(l, r, "Cannot stop DHCPv6 prefix delegation client after adding new link: %m");
511 continue;
512 }
513
514 r = sd_dhcp6_client_start(l->dhcp6_client);
515 if (r < 0) {
516 log_link_warning_errno(l, r, "Cannot restart DHCPv6 prefix delegation client after adding new link: %m");
517 continue;
518 }
519
520 log_link_debug(l, "Restarted DHCPv6 client to acquire prefix delegations after adding new link");
521 }
522
523 return 0;
524 }
525
526 static int dhcp6_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
527 int r;
528
529 assert(link);
530 assert(link->dhcp6_address_messages > 0);
531
532 link->dhcp6_address_messages--;
533
534 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
535 return 1;
536
537 r = sd_netlink_message_get_errno(m);
538 if (r < 0 && r != -EEXIST) {
539 log_link_message_warning_errno(link, m, r, "Could not set DHCPv6 address");
540 link_enter_failed(link);
541 return 1;
542 } else if (r >= 0)
543 (void) manager_rtnl_process_address(rtnl, m, link->manager);
544
545 if (link->dhcp6_address_messages == 0) {
546 log_link_debug(link, "DHCPv6 addresses set");
547 link->dhcp6_address_configured = true;
548 r = link_request_set_routes(link);
549 if (r < 0) {
550 link_enter_failed(link);
551 return 1;
552 }
553 }
554
555 return 1;
556 }
557
558 static int dhcp6_address_change(
559 Link *link,
560 struct in6_addr *ip6_addr,
561 uint32_t lifetime_preferred,
562 uint32_t lifetime_valid) {
563
564 _cleanup_(address_freep) Address *addr = NULL;
565 _cleanup_free_ char *buffer = NULL;
566 int r;
567
568 r = address_new(&addr);
569 if (r < 0)
570 return r;
571
572 addr->family = AF_INET6;
573 addr->in_addr.in6 = *ip6_addr;
574 addr->flags = IFA_F_NOPREFIXROUTE;
575 addr->prefixlen = 128;
576 addr->cinfo.ifa_prefered = lifetime_preferred;
577 addr->cinfo.ifa_valid = lifetime_valid;
578
579 (void) in_addr_to_string(addr->family, &addr->in_addr, &buffer);
580 log_link_info(link,
581 "DHCPv6 address %s/%d timeout preferred %d valid %d",
582 strnull(buffer), addr->prefixlen, lifetime_preferred, lifetime_valid);
583
584 r = address_configure(addr, link, dhcp6_address_handler, true);
585 if (r < 0)
586 return log_link_warning_errno(link, r, "Could not assign DHCPv6 address: %m");
587 if (r > 0)
588 link->dhcp6_address_messages++;
589
590 return 0;
591 }
592
593 static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link) {
594 int r;
595 sd_dhcp6_lease *lease;
596 struct in6_addr ip6_addr;
597 uint32_t lifetime_preferred, lifetime_valid;
598
599 link->dhcp6_address_configured = false;
600
601 r = sd_dhcp6_client_get_lease(client, &lease);
602 if (r < 0)
603 return r;
604
605 sd_dhcp6_lease_reset_address_iter(lease);
606 while (sd_dhcp6_lease_get_address(lease, &ip6_addr,
607 &lifetime_preferred,
608 &lifetime_valid) >= 0) {
609
610 r = dhcp6_address_change(link, &ip6_addr, lifetime_preferred, lifetime_valid);
611 if (r < 0)
612 return r;
613 }
614
615 if (link->dhcp6_address_messages == 0) {
616 link->dhcp6_address_configured = true;
617 return link_request_set_routes(link);
618 } else {
619 log_link_debug(link, "Setting DHCPv6 addresses");
620 /* address_handler calls link_request_set_routes() and link_request_set_nexthop().
621 * Before they are called, the related flags must be cleared. Otherwise, the link
622 * becomes configured state before routes are configured. */
623 link->static_routes_configured = false;
624 link->static_nexthops_configured = false;
625 link_set_state(link, LINK_STATE_CONFIGURING);
626 }
627
628 return 0;
629 }
630
631 static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
632 int r;
633 Link *link = userdata;
634
635 assert(link);
636 assert(link->network);
637
638 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
639 return;
640
641 switch(event) {
642 case SD_DHCP6_CLIENT_EVENT_STOP:
643 case SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE:
644 case SD_DHCP6_CLIENT_EVENT_RETRANS_MAX:
645 if (sd_dhcp6_client_get_lease(client, NULL) >= 0)
646 log_link_warning(link, "DHCPv6 lease lost");
647
648 (void) dhcp6_lease_pd_prefix_lost(client, link);
649 (void) dhcp6_prefix_remove_all(link->manager, link);
650
651 link_dirty(link);
652 break;
653
654 case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE:
655 r = dhcp6_lease_address_acquired(client, link);
656 if (r < 0) {
657 link_enter_failed(link);
658 return;
659 }
660
661 r = dhcp6_lease_pd_prefix_acquired(client, link);
662 if (r < 0)
663 log_link_debug_errno(link, r, "DHCPv6 did not receive prefixes to delegate: %m");
664
665 _fallthrough_;
666 case SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST:
667 r = dhcp6_lease_information_acquired(client, link);
668 if (r < 0) {
669 link_enter_failed(link);
670 return;
671 }
672
673 link_dirty(link);
674 break;
675
676 default:
677 if (event < 0)
678 log_link_warning_errno(link, event, "DHCPv6 error: %m");
679 else
680 log_link_warning(link, "DHCPv6 unknown event: %d", event);
681 return;
682 }
683
684 link_check_ready(link);
685 }
686
687 int dhcp6_request_address(Link *link, int ir) {
688 int r, inf_req, pd;
689 bool running;
690
691 assert(link);
692 assert(link->dhcp6_client);
693 assert(link->network);
694 assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0);
695
696 r = sd_dhcp6_client_is_running(link->dhcp6_client);
697 if (r < 0)
698 return r;
699 running = r;
700
701 r = sd_dhcp6_client_get_prefix_delegation(link->dhcp6_client, &pd);
702 if (r < 0)
703 return r;
704
705 if (pd && ir && link->network->dhcp6_force_pd_other_information) {
706 log_link_debug(link, "Enabling managed mode to request DHCPv6 PD with 'Other Information' set");
707
708 r = sd_dhcp6_client_set_address_request(link->dhcp6_client,
709 false);
710 if (r < 0)
711 return r;
712
713 ir = false;
714 }
715
716 if (running) {
717 r = sd_dhcp6_client_get_information_request(link->dhcp6_client, &inf_req);
718 if (r < 0)
719 return r;
720
721 if (inf_req == ir)
722 return 0;
723
724 r = sd_dhcp6_client_stop(link->dhcp6_client);
725 if (r < 0)
726 return r;
727 } else {
728 r = sd_dhcp6_client_set_local_address(link->dhcp6_client, &link->ipv6ll_address);
729 if (r < 0)
730 return r;
731 }
732
733 r = sd_dhcp6_client_set_information_request(link->dhcp6_client, ir);
734 if (r < 0)
735 return r;
736
737 r = sd_dhcp6_client_start(link->dhcp6_client);
738 if (r < 0)
739 return r;
740
741 return 0;
742 }
743
744 static int dhcp6_set_hostname(sd_dhcp6_client *client, Link *link) {
745 _cleanup_free_ char *hostname = NULL;
746 const char *hn;
747 int r;
748
749 assert(link);
750
751 if (!link->network->dhcp_send_hostname)
752 hn = NULL;
753 else if (link->network->dhcp_hostname)
754 hn = link->network->dhcp_hostname;
755 else {
756 r = gethostname_strict(&hostname);
757 if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
758 return r;
759
760 hn = hostname;
761 }
762
763 r = sd_dhcp6_client_set_fqdn(client, hn);
764 if (r == -EINVAL && hostname)
765 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
766 log_link_warning_errno(link, r, "DHCP6 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
767 else if (r < 0)
768 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set hostname: %m");
769
770 return 0;
771 }
772
773 int dhcp6_configure(Link *link) {
774 _cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL;
775 sd_dhcp6_option *vendor_option;
776 sd_dhcp6_option *send_option;
777 void *request_options;
778 const DUID *duid;
779 Iterator i;
780 int r;
781
782 assert(link);
783 assert(link->network);
784
785 if (link->dhcp6_client)
786 return 0;
787
788 r = sd_dhcp6_client_new(&client);
789 if (r == -ENOMEM)
790 return log_oom();
791 if (r < 0)
792 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to create DHCP6 client: %m");
793
794 r = sd_dhcp6_client_attach_event(client, NULL, 0);
795 if (r < 0)
796 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to attach event: %m");
797
798 r = sd_dhcp6_client_set_mac(client,
799 (const uint8_t *) &link->mac,
800 sizeof (link->mac), ARPHRD_ETHER);
801 if (r < 0)
802 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set MAC address: %m");
803
804 if (link->network->iaid_set) {
805 r = sd_dhcp6_client_set_iaid(client, link->network->iaid);
806 if (r < 0)
807 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set IAID: %m");
808 }
809
810 duid = link_get_duid(link);
811 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
812 r = sd_dhcp6_client_set_duid_llt(client, duid->llt_time);
813 else
814 r = sd_dhcp6_client_set_duid(client,
815 duid->type,
816 duid->raw_data_len > 0 ? duid->raw_data : NULL,
817 duid->raw_data_len);
818 if (r < 0)
819 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set DUID: %m");
820
821 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp6_client_send_options, i) {
822 r = sd_dhcp6_client_add_option(client, send_option);
823 if (r == -EEXIST)
824 continue;
825 if (r < 0)
826 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set option: %m");
827 }
828
829 r = dhcp6_set_hostname(client, link);
830 if (r < 0)
831 return r;
832
833 r = sd_dhcp6_client_set_ifindex(client, link->ifindex);
834 if (r < 0)
835 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set ifindex: %m");
836
837 if (link->network->rapid_commit) {
838 r = sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_RAPID_COMMIT);
839 if (r < 0)
840 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set request flag for rapid commit: %m");
841 }
842
843 if (link->network->dhcp6_mudurl) {
844 r = sd_dhcp6_client_set_request_mud_url(client, link->network->dhcp6_mudurl);
845 if (r < 0)
846 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set MUD URL: %m");
847 }
848
849 SET_FOREACH(request_options, link->network->dhcp6_request_options, i) {
850 uint32_t option = PTR_TO_UINT32(request_options);
851
852 r = sd_dhcp6_client_set_request_option(client, option);
853 if (r == -EEXIST) {
854 log_link_debug(link, "DHCP6 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option);
855 continue;
856 }
857 if (r < 0)
858 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set request flag for '%u': %m", option);
859 }
860
861 if (link->network->dhcp6_user_class) {
862 r = sd_dhcp6_client_set_request_user_class(client, link->network->dhcp6_user_class);
863 if (r < 0)
864 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set user class: %m");
865 }
866
867 if (link->network->dhcp6_vendor_class) {
868 r = sd_dhcp6_client_set_request_vendor_class(client, link->network->dhcp6_vendor_class);
869 if (r < 0)
870 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set vendor class: %m");
871 }
872
873 ORDERED_HASHMAP_FOREACH(vendor_option, link->network->dhcp6_client_send_vendor_options, i) {
874 r = sd_dhcp6_client_add_vendor_option(client, vendor_option);
875 if (r == -EEXIST)
876 continue;
877 if (r < 0)
878 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set vendor option: %m");
879 }
880
881 r = sd_dhcp6_client_set_callback(client, dhcp6_handler, link);
882 if (r < 0)
883 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set callback: %m");
884
885 if (dhcp6_enable_prefix_delegation(link)) {
886 r = sd_dhcp6_client_set_prefix_delegation(client, true);
887 if (r < 0)
888 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set prefix delegation: %m");
889 }
890
891 if (link->network->dhcp6_pd_length > 0) {
892 r = sd_dhcp6_client_set_prefix_delegation_hint(client, link->network->dhcp6_pd_length, &link->network->dhcp6_pd_address);
893 if (r < 0)
894 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set prefix hint: %m");
895 }
896
897 link->dhcp6_client = TAKE_PTR(client);
898
899 return 0;
900 }
901
902 static Link *dhcp6_prefix_get(Manager *m, struct in6_addr *addr) {
903 assert_return(m, NULL);
904 assert_return(addr, NULL);
905
906 return hashmap_get(m->dhcp6_prefixes, addr);
907 }
908
909 static int dhcp6_pd_route_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
910 int r;
911
912 assert(link);
913 assert(link->dhcp6_pd_route_messages > 0);
914
915 link->dhcp6_pd_route_messages--;
916
917 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
918 return 1;
919
920 r = sd_netlink_message_get_errno(m);
921 if (r < 0 && r != -EEXIST) {
922 log_link_message_warning_errno(link, m, r, "Failed to add DHCPv6 Prefix Delegation route");
923 link_enter_failed(link);
924 return 1;
925 }
926
927 if (link->dhcp6_pd_route_messages == 0) {
928 log_link_debug(link, "DHCPv6 prefix delegation routes set");
929 link->dhcp6_pd_route_configured = true;
930 link_check_ready(link);
931 }
932
933 return 1;
934 }
935
936 static int dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) {
937 _cleanup_(route_freep) Route *route = NULL;
938 _cleanup_free_ struct in6_addr *a = NULL;
939 _cleanup_free_ char *buf = NULL;
940 Link *assigned_link;
941 int r;
942
943 assert_return(m, -EINVAL);
944 assert_return(addr, -EINVAL);
945
946 r = route_new(&route);
947 if (r < 0)
948 return r;
949
950 route->family = AF_INET6;
951 route->dst.in6 = *addr;
952 route->dst_prefixlen = 64;
953
954 link->dhcp6_pd_route_configured = false;
955 link_set_state(link, LINK_STATE_CONFIGURING);
956
957 r = route_configure(route, link, dhcp6_pd_route_handler);
958 if (r < 0)
959 return r;
960 if (r > 0)
961 link->dhcp6_pd_route_messages++;
962
963 (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf);
964 log_link_debug(link, "Adding prefix route %s/64", strnull(buf));
965
966 assigned_link = hashmap_get(m->dhcp6_prefixes, addr);
967 if (assigned_link) {
968 assert(assigned_link == link);
969 return 0;
970 }
971
972 a = newdup(struct in6_addr, addr, 1);
973 if (!a)
974 return -ENOMEM;
975
976 r = hashmap_ensure_allocated(&m->dhcp6_prefixes, &in6_addr_hash_ops);
977 if (r < 0)
978 return r;
979
980 r = hashmap_put(m->dhcp6_prefixes, a, link);
981 if (r < 0)
982 return r;
983
984 TAKE_PTR(a);
985 link_ref(link);
986 return 0;
987 }
988
989 static int dhcp6_prefix_remove_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
990 int r;
991
992 assert(link);
993
994 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
995 return 1;
996
997 r = sd_netlink_message_get_errno(m);
998 if (r < 0) {
999 log_link_message_warning_errno(link, m, r, "Received error on DHCPv6 Prefix Delegation route removal");
1000 link_enter_failed(link);
1001 return 1;
1002 }
1003
1004 return 1;
1005 }
1006
1007 int dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) {
1008 _cleanup_free_ struct in6_addr *a = NULL;
1009 _cleanup_(link_unrefp) Link *l = NULL;
1010 _cleanup_(route_freep) Route *route = NULL;
1011 _cleanup_free_ char *buf = NULL;
1012 int r;
1013
1014 assert_return(m, -EINVAL);
1015 assert_return(addr, -EINVAL);
1016
1017 l = hashmap_remove2(m->dhcp6_prefixes, addr, (void **) &a);
1018 if (!l)
1019 return -EINVAL;
1020
1021 (void) sd_radv_remove_prefix(l->radv, addr, 64);
1022
1023 r = route_new(&route);
1024 if (r < 0)
1025 return r;
1026
1027 route->family = AF_INET6;
1028 route->dst.in6 = *addr;
1029 route->dst_prefixlen = 64;
1030
1031 r = route_remove(route, l, dhcp6_prefix_remove_handler);
1032 if (r < 0)
1033 return r;
1034
1035 (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf);
1036 log_link_debug(l, "Removing prefix route %s/64", strnull(buf));
1037
1038 return 0;
1039 }
1040
1041 static int dhcp6_prefix_remove_all(Manager *m, Link *link) {
1042 struct in6_addr *addr;
1043 Iterator i;
1044 Link *l;
1045
1046 assert_return(m, -EINVAL);
1047 assert_return(link, -EINVAL);
1048
1049 HASHMAP_FOREACH_KEY(l, addr, m->dhcp6_prefixes, i)
1050 if (l == link)
1051 (void) dhcp6_prefix_remove(m, addr);
1052
1053 return 0;
1054 }
1055
1056 static int dhcp6_pd_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1057 int r;
1058
1059 assert(link);
1060 assert(link->dhcp6_pd_address_messages > 0);
1061
1062 link->dhcp6_pd_address_messages--;
1063
1064 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1065 return 1;
1066
1067 r = sd_netlink_message_get_errno(m);
1068 if (r < 0 && r != -EEXIST) {
1069 log_link_message_warning_errno(link, m, r, "Could not set DHCPv6 delegated prefix address");
1070 link_enter_failed(link);
1071 return 1;
1072 } else if (r >= 0)
1073 (void) manager_rtnl_process_address(rtnl, m, link->manager);
1074
1075 if (link->dhcp6_pd_address_messages == 0) {
1076 log_link_debug(link, "DHCPv6 delegated prefix addresses set");
1077 link->dhcp6_pd_address_configured = true;
1078 r = link_request_set_routes(link);
1079 if (r < 0) {
1080 link_enter_failed(link);
1081 return 1;
1082 }
1083 }
1084
1085 return 1;
1086 }
1087
1088 static int dhcp6_assign_delegated_prefix(Link *link,
1089 const struct in6_addr *prefix,
1090 uint8_t prefix_len,
1091 uint32_t lifetime_preferred,
1092 uint32_t lifetime_valid) {
1093
1094 _cleanup_(address_freep) Address *address = NULL;
1095 int r;
1096
1097 assert(link);
1098 assert(link->network);
1099 assert(prefix);
1100
1101 if (!link->network->dhcp6_pd_assign_prefix) {
1102 link->dhcp6_pd_address_configured = true;
1103 return 0;
1104 }
1105
1106 r = address_new(&address);
1107 if (r < 0)
1108 return log_link_error_errno(link, r, "Failed to allocate address for DHCPv6 delegated prefix: %m");
1109
1110 address->in_addr.in6 = *prefix;
1111
1112 if (!in_addr_is_null(AF_INET6, &link->network->dhcp6_delegation_prefix_token))
1113 memcpy(address->in_addr.in6.s6_addr + 8, link->network->dhcp6_delegation_prefix_token.in6.s6_addr + 8, 8);
1114 else {
1115 r = generate_ipv6_eui_64_address(link, &address->in_addr.in6);
1116 if (r < 0)
1117 return log_link_warning_errno(link, r, "Failed to generate EUI64 address for acquired DHCPv6 delegated prefix: %m");
1118 }
1119
1120 address->prefixlen = prefix_len;
1121 address->family = AF_INET6;
1122 address->cinfo.ifa_prefered = lifetime_preferred;
1123 address->cinfo.ifa_valid = lifetime_valid;
1124
1125 /* address_handler calls link_request_set_routes() and link_request_set_nexthop(). Before they
1126 * are called, the related flags must be cleared. Otherwise, the link becomes configured state
1127 * before routes are configured. */
1128 link->static_routes_configured = false;
1129 link->static_nexthops_configured = false;
1130 link->dhcp6_pd_address_configured = false;
1131 link_set_state(link, LINK_STATE_CONFIGURING);
1132
1133 r = address_configure(address, link, dhcp6_pd_address_handler, true);
1134 if (r < 0)
1135 return log_link_warning_errno(link, r, "Failed to set acquired DHCPv6 delegated prefix address: %m");
1136 if (r > 0)
1137 link->dhcp6_pd_address_messages++;
1138
1139 return 0;
1140 }
1141
1142 int config_parse_dhcp6_pd_hint(
1143 const char* unit,
1144 const char *filename,
1145 unsigned line,
1146 const char *section,
1147 unsigned section_line,
1148 const char *lvalue,
1149 int ltype,
1150 const char *rvalue,
1151 void *data,
1152 void *userdata) {
1153
1154 Network *network = data;
1155 int r;
1156
1157 assert(filename);
1158 assert(lvalue);
1159 assert(rvalue);
1160 assert(data);
1161
1162 r = in_addr_prefix_from_string(rvalue, AF_INET6, (union in_addr_union *) &network->dhcp6_pd_address, &network->dhcp6_pd_length);
1163 if (r < 0) {
1164 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse PrefixDelegationHint=%s, ignoring assignment", rvalue);
1165 return 0;
1166 }
1167
1168 if (network->dhcp6_pd_length < 1 || network->dhcp6_pd_length > 128) {
1169 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid prefix length='%d', ignoring assignment", network->dhcp6_pd_length);
1170 network->dhcp6_pd_length = 0;
1171 return 0;
1172 }
1173
1174 return 0;
1175 }
1176
1177 int config_parse_dhcp6_mud_url(
1178 const char *unit,
1179 const char *filename,
1180 unsigned line,
1181 const char *section,
1182 unsigned section_line,
1183 const char *lvalue,
1184 int ltype,
1185 const char *rvalue,
1186 void *data,
1187 void *userdata) {
1188
1189 _cleanup_free_ char *unescaped = NULL;
1190 Network *network = data;
1191 int r;
1192
1193 assert(filename);
1194 assert(lvalue);
1195 assert(rvalue);
1196
1197 if (isempty(rvalue)) {
1198 network->dhcp6_mudurl = mfree(network->dhcp6_mudurl);
1199 return 0;
1200 }
1201
1202 r = cunescape(rvalue, 0, &unescaped);
1203 if (r < 0) {
1204 log_syntax(unit, LOG_ERR, filename, line, r,
1205 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
1206 return 0;
1207 }
1208
1209 if (!http_url_is_valid(unescaped) || strlen(unescaped) > UINT8_MAX) {
1210 log_syntax(unit, LOG_ERR, filename, line, 0,
1211 "Failed to parse MUD URL '%s', ignoring: %m", rvalue);
1212
1213 return 0;
1214 }
1215
1216 return free_and_replace(network->dhcp6_mudurl, unescaped);
1217 }
1218
1219 int config_parse_dhcp6_delegated_prefix_token(
1220 const char *unit,
1221 const char *filename,
1222 unsigned line,
1223 const char *section,
1224 unsigned section_line,
1225 const char *lvalue,
1226 int ltype,
1227 const char *rvalue,
1228 void *data,
1229 void *userdata) {
1230
1231 Network *network = data;
1232 int r;
1233
1234 assert(filename);
1235 assert(lvalue);
1236 assert(rvalue);
1237 assert(data);
1238
1239 if (isempty(rvalue)) {
1240 network->dhcp6_delegation_prefix_token = IN_ADDR_NULL;
1241 return 0;
1242 }
1243
1244 r = in_addr_from_string(AF_INET6, rvalue, &network->dhcp6_delegation_prefix_token);
1245 if (r < 0) {
1246 log_syntax(unit, LOG_ERR, filename, line, r,
1247 "Failed to parse DHCPv6 %s, ignoring: %s", lvalue, rvalue);
1248 return 0;
1249 }
1250
1251 if (in_addr_is_null(AF_INET6, &network->dhcp6_delegation_prefix_token)) {
1252 log_syntax(unit, LOG_ERR, filename, line, 0,
1253 "DHCPv6 %s cannot be the ANY address, ignoring: %s", lvalue, rvalue);
1254 return 0;
1255 }
1256
1257 return 0;
1258 }
1259
1260 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp6_client_start_mode, dhcp6_client_start_mode, DHCP6ClientStartMode,
1261 "Failed to parse WithoutRA= setting");
1262
1263 static const char* const dhcp6_client_start_mode_table[_DHCP6_CLIENT_START_MODE_MAX] = {
1264 [DHCP6_CLIENT_START_MODE_NO] = "no",
1265 [DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST] = "information-request",
1266 [DHCP6_CLIENT_START_MODE_SOLICIT] = "solicit",
1267 };
1268
1269 DEFINE_STRING_TABLE_LOOKUP(dhcp6_client_start_mode, DHCP6ClientStartMode);