]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-dhcp6.c
network: set dhcp6_xxx_configured flag after routes/addresses are assigned
[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?");
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");
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");
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");
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");
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
600 link->dhcp6_address_configured = false;
601
602 r = sd_dhcp6_client_get_lease(client, &lease);
603 if (r < 0)
604 return r;
605
606 sd_dhcp6_lease_reset_address_iter(lease);
607 while (sd_dhcp6_lease_get_address(lease, &ip6_addr,
608 &lifetime_preferred,
609 &lifetime_valid) >= 0) {
610
611 r = dhcp6_address_change(link, &ip6_addr, lifetime_preferred, lifetime_valid);
612 if (r < 0)
613 return r;
614 }
615
616 if (link->dhcp6_address_messages == 0) {
617 link->dhcp6_address_configured = true;
618 return link_request_set_routes(link);
619 } else {
620 log_link_debug(link, "Setting DHCPv6 addresses");
621 link_set_state(link, LINK_STATE_CONFIGURING);
622 }
623
624 return 0;
625 }
626
627 static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
628 int r;
629 Link *link = userdata;
630
631 assert(link);
632 assert(link->network);
633
634 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
635 return;
636
637 switch(event) {
638 case SD_DHCP6_CLIENT_EVENT_STOP:
639 case SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE:
640 case SD_DHCP6_CLIENT_EVENT_RETRANS_MAX:
641 if (sd_dhcp6_client_get_lease(client, NULL) >= 0)
642 log_link_warning(link, "DHCPv6 lease lost");
643
644 (void) dhcp6_lease_pd_prefix_lost(client, link);
645 (void) dhcp6_prefix_remove_all(link->manager, link);
646
647 link_dirty(link);
648 break;
649
650 case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE:
651 r = dhcp6_lease_address_acquired(client, link);
652 if (r < 0) {
653 link_enter_failed(link);
654 return;
655 }
656
657 r = dhcp6_lease_pd_prefix_acquired(client, link);
658 if (r < 0)
659 log_link_debug_errno(link, r, "DHCPv6 did not receive prefixes to delegate");
660
661 _fallthrough_;
662 case SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST:
663 r = dhcp6_lease_information_acquired(client, link);
664 if (r < 0) {
665 link_enter_failed(link);
666 return;
667 }
668
669 link_dirty(link);
670 break;
671
672 default:
673 if (event < 0)
674 log_link_warning_errno(link, event, "DHCPv6 error: %m");
675 else
676 log_link_warning(link, "DHCPv6 unknown event: %d", event);
677 return;
678 }
679
680 link_check_ready(link);
681 }
682
683 int dhcp6_request_address(Link *link, int ir) {
684 int r, inf_req, pd;
685 bool running;
686
687 assert(link);
688 assert(link->dhcp6_client);
689 assert(link->network);
690 assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0);
691
692 r = sd_dhcp6_client_is_running(link->dhcp6_client);
693 if (r < 0)
694 return r;
695 running = r;
696
697 r = sd_dhcp6_client_get_prefix_delegation(link->dhcp6_client, &pd);
698 if (r < 0)
699 return r;
700
701 if (pd && ir && link->network->dhcp6_force_pd_other_information) {
702 log_link_debug(link, "Enabling managed mode to request DHCPv6 PD with 'Other Information' set");
703
704 r = sd_dhcp6_client_set_address_request(link->dhcp6_client,
705 false);
706 if (r < 0)
707 return r;
708
709 ir = false;
710 }
711
712 if (running) {
713 r = sd_dhcp6_client_get_information_request(link->dhcp6_client, &inf_req);
714 if (r < 0)
715 return r;
716
717 if (inf_req == ir)
718 return 0;
719
720 r = sd_dhcp6_client_stop(link->dhcp6_client);
721 if (r < 0)
722 return r;
723 } else {
724 r = sd_dhcp6_client_set_local_address(link->dhcp6_client, &link->ipv6ll_address);
725 if (r < 0)
726 return r;
727 }
728
729 r = sd_dhcp6_client_set_information_request(link->dhcp6_client, ir);
730 if (r < 0)
731 return r;
732
733 r = sd_dhcp6_client_start(link->dhcp6_client);
734 if (r < 0)
735 return r;
736
737 return 0;
738 }
739
740 static int dhcp6_set_hostname(sd_dhcp6_client *client, Link *link) {
741 _cleanup_free_ char *hostname = NULL;
742 const char *hn;
743 int r;
744
745 assert(link);
746
747 if (!link->network->dhcp_send_hostname)
748 hn = NULL;
749 else if (link->network->dhcp_hostname)
750 hn = link->network->dhcp_hostname;
751 else {
752 r = gethostname_strict(&hostname);
753 if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
754 return r;
755
756 hn = hostname;
757 }
758
759 r = sd_dhcp6_client_set_fqdn(client, hn);
760 if (r == -EINVAL && hostname)
761 /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
762 log_link_warning_errno(link, r, "DHCP6 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
763 else if (r < 0)
764 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set hostname: %m");
765
766 return 0;
767 }
768
769 int dhcp6_configure(Link *link) {
770 _cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL;
771 sd_dhcp6_option *vendor_option;
772 sd_dhcp6_option *send_option;
773 void *request_options;
774 const DUID *duid;
775 Iterator i;
776 int r;
777
778 assert(link);
779 assert(link->network);
780
781 if (link->dhcp6_client)
782 return 0;
783
784 r = sd_dhcp6_client_new(&client);
785 if (r == -ENOMEM)
786 return log_oom();
787 if (r < 0)
788 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to create DHCP6 client: %m");
789
790 r = sd_dhcp6_client_attach_event(client, NULL, 0);
791 if (r < 0)
792 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to attach event: %m");
793
794 r = sd_dhcp6_client_set_mac(client,
795 (const uint8_t *) &link->mac,
796 sizeof (link->mac), ARPHRD_ETHER);
797 if (r < 0)
798 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set MAC address: %m");
799
800 if (link->network->iaid_set) {
801 r = sd_dhcp6_client_set_iaid(client, link->network->iaid);
802 if (r < 0)
803 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set IAID: %m");
804 }
805
806 duid = link_get_duid(link);
807 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
808 r = sd_dhcp6_client_set_duid_llt(client, duid->llt_time);
809 else
810 r = sd_dhcp6_client_set_duid(client,
811 duid->type,
812 duid->raw_data_len > 0 ? duid->raw_data : NULL,
813 duid->raw_data_len);
814 if (r < 0)
815 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set DUID: %m");
816
817 ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp6_client_send_options, i) {
818 r = sd_dhcp6_client_add_option(client, send_option);
819 if (r == -EEXIST)
820 continue;
821 if (r < 0)
822 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set option: %m");
823 }
824
825 r = dhcp6_set_hostname(client, link);
826 if (r < 0)
827 return r;
828
829 r = sd_dhcp6_client_set_ifindex(client, link->ifindex);
830 if (r < 0)
831 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set ifindex: %m");
832
833 if (link->network->rapid_commit) {
834 r = sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_RAPID_COMMIT);
835 if (r < 0)
836 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set request flag for rapid commit: %m");
837 }
838
839 if (link->network->dhcp6_mudurl) {
840 r = sd_dhcp6_client_set_request_mud_url(client, link->network->dhcp6_mudurl);
841 if (r < 0)
842 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set MUD URL: %m");
843 }
844
845 SET_FOREACH(request_options, link->network->dhcp6_request_options, i) {
846 uint32_t option = PTR_TO_UINT32(request_options);
847
848 r = sd_dhcp6_client_set_request_option(client, option);
849 if (r == -EEXIST) {
850 log_link_debug(link, "DHCP6 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option);
851 continue;
852 }
853
854 if (r < 0)
855 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set request flag for '%u': %m", option);
856 }
857
858 if (link->network->dhcp6_user_class) {
859 r = sd_dhcp6_client_set_request_user_class(client, link->network->dhcp6_user_class);
860 if (r < 0)
861 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set user class: %m");
862 }
863
864 if (link->network->dhcp6_vendor_class) {
865 r = sd_dhcp6_client_set_request_vendor_class(client, link->network->dhcp6_vendor_class);
866 if (r < 0)
867 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set vendor class: %m");
868 }
869
870 ORDERED_HASHMAP_FOREACH(vendor_option, link->network->dhcp6_client_send_vendor_options, i) {
871 r = sd_dhcp6_client_add_vendor_option(client, vendor_option);
872 if (r == -EEXIST)
873 continue;
874 if (r < 0)
875 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set vendor option: %m");
876 }
877
878 r = sd_dhcp6_client_set_callback(client, dhcp6_handler, link);
879 if (r < 0)
880 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set callback: %m");
881
882 if (dhcp6_enable_prefix_delegation(link)) {
883 r = sd_dhcp6_client_set_prefix_delegation(client, true);
884 if (r < 0)
885 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set prefix delegation: %m");
886 }
887
888 if (link->network->dhcp6_pd_length > 0) {
889 r = sd_dhcp6_client_set_prefix_delegation_hint(client, link->network->dhcp6_pd_length, &link->network->dhcp6_pd_address);
890 if (r < 0)
891 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set prefix hint: %m");
892 }
893
894 link->dhcp6_client = TAKE_PTR(client);
895
896 return 0;
897 }
898
899 static Link *dhcp6_prefix_get(Manager *m, struct in6_addr *addr) {
900 assert_return(m, NULL);
901 assert_return(addr, NULL);
902
903 return hashmap_get(m->dhcp6_prefixes, addr);
904 }
905
906 static int dhcp6_pd_route_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
907 int r;
908
909 assert(link);
910 assert(link->dhcp6_pd_route_messages > 0);
911
912 link->dhcp6_pd_route_messages--;
913
914 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
915 return 1;
916
917 r = sd_netlink_message_get_errno(m);
918 if (r < 0 && r != -EEXIST) {
919 log_link_message_warning_errno(link, m, r, "Failed to add DHCPv6 Prefix Delegation route");
920 link_enter_failed(link);
921 return 1;
922 }
923
924 if (link->dhcp6_pd_route_messages == 0) {
925 log_link_debug(link, "DHCPv6 prefix delegation routes set");
926 link->dhcp6_pd_route_configured = true;
927 link_check_ready(link);
928 }
929
930 return 1;
931 }
932
933 static int dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) {
934 _cleanup_(route_freep) Route *route = NULL;
935 _cleanup_free_ struct in6_addr *a = NULL;
936 _cleanup_free_ char *buf = NULL;
937 Link *assigned_link;
938 int r;
939
940 assert_return(m, -EINVAL);
941 assert_return(addr, -EINVAL);
942
943 r = route_new(&route);
944 if (r < 0)
945 return r;
946
947 route->family = AF_INET6;
948 route->dst.in6 = *addr;
949 route->dst_prefixlen = 64;
950
951 link->dhcp6_pd_route_configured = false;
952 link_set_state(link, LINK_STATE_CONFIGURING);
953
954 r = route_configure(route, link, dhcp6_pd_route_handler);
955 if (r < 0)
956 return r;
957 if (r > 0)
958 link->dhcp6_pd_route_messages++;
959
960 (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf);
961 log_link_debug(link, "Adding prefix route %s/64", strnull(buf));
962
963 assigned_link = hashmap_get(m->dhcp6_prefixes, addr);
964 if (assigned_link) {
965 assert(assigned_link == link);
966 return 0;
967 }
968
969 a = newdup(struct in6_addr, addr, 1);
970 if (!a)
971 return -ENOMEM;
972
973 r = hashmap_ensure_allocated(&m->dhcp6_prefixes, &in6_addr_hash_ops);
974 if (r < 0)
975 return r;
976
977 r = hashmap_put(m->dhcp6_prefixes, a, link);
978 if (r < 0)
979 return r;
980
981 TAKE_PTR(a);
982 link_ref(link);
983 return 0;
984 }
985
986 static int dhcp6_prefix_remove_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
987 int r;
988
989 assert(link);
990
991 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
992 return 1;
993
994 r = sd_netlink_message_get_errno(m);
995 if (r < 0) {
996 log_link_message_warning_errno(link, m, r, "Received error on DHCPv6 Prefix Delegation route removal");
997 link_enter_failed(link);
998 return 1;
999 }
1000
1001 return 1;
1002 }
1003
1004 int dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) {
1005 _cleanup_free_ struct in6_addr *a = NULL;
1006 _cleanup_(link_unrefp) Link *l = NULL;
1007 _cleanup_(route_freep) Route *route = NULL;
1008 _cleanup_free_ char *buf = NULL;
1009 int r;
1010
1011 assert_return(m, -EINVAL);
1012 assert_return(addr, -EINVAL);
1013
1014 l = hashmap_remove2(m->dhcp6_prefixes, addr, (void **) &a);
1015 if (!l)
1016 return -EINVAL;
1017
1018 (void) sd_radv_remove_prefix(l->radv, addr, 64);
1019
1020 r = route_new(&route);
1021 if (r < 0)
1022 return r;
1023
1024 route->family = AF_INET6;
1025 route->dst.in6 = *addr;
1026 route->dst_prefixlen = 64;
1027
1028 r = route_remove(route, l, dhcp6_prefix_remove_handler);
1029 if (r < 0)
1030 return r;
1031
1032 (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf);
1033 log_link_debug(l, "Removing prefix route %s/64", strnull(buf));
1034
1035 return 0;
1036 }
1037
1038 static int dhcp6_prefix_remove_all(Manager *m, Link *link) {
1039 struct in6_addr *addr;
1040 Iterator i;
1041 Link *l;
1042
1043 assert_return(m, -EINVAL);
1044 assert_return(link, -EINVAL);
1045
1046 HASHMAP_FOREACH_KEY(l, addr, m->dhcp6_prefixes, i)
1047 if (l == link)
1048 (void) dhcp6_prefix_remove(m, addr);
1049
1050 return 0;
1051 }
1052
1053 static int dhcp6_pd_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1054 int r;
1055
1056 assert(link);
1057 assert(link->dhcp6_pd_address_messages > 0);
1058
1059 link->dhcp6_pd_address_messages--;
1060
1061 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1062 return 1;
1063
1064 r = sd_netlink_message_get_errno(m);
1065 if (r < 0 && r != -EEXIST) {
1066 log_link_message_warning_errno(link, m, r, "Could not set DHCPv6 delegated prefix address");
1067 link_enter_failed(link);
1068 return 1;
1069 } else if (r >= 0)
1070 (void) manager_rtnl_process_address(rtnl, m, link->manager);
1071
1072 if (link->dhcp6_pd_address_messages == 0) {
1073 log_link_debug(link, "DHCPv6 delegated prefix addresses set");
1074 link->dhcp6_pd_address_configured = true;
1075 r = link_request_set_routes(link);
1076 if (r < 0) {
1077 link_enter_failed(link);
1078 return 1;
1079 }
1080 }
1081
1082 return 1;
1083 }
1084
1085 static int dhcp6_assign_delegated_prefix(Link *link,
1086 const struct in6_addr *prefix,
1087 uint8_t prefix_len,
1088 uint32_t lifetime_preferred,
1089 uint32_t lifetime_valid) {
1090
1091 _cleanup_(address_freep) Address *address = NULL;
1092 int r;
1093
1094 assert(link);
1095 assert(link->network);
1096 assert(prefix);
1097
1098 if (!link->network->dhcp6_pd_assign_prefix) {
1099 link->dhcp6_pd_address_configured = true;
1100 return 0;
1101 }
1102
1103 r = address_new(&address);
1104 if (r < 0)
1105 return log_link_error_errno(link, r, "Failed to allocate address for DHCPv6 delegated prefix: %m");
1106
1107 address->in_addr.in6 = *prefix;
1108
1109 if (!in_addr_is_null(AF_INET6, &link->network->dhcp6_delegation_prefix_token))
1110 memcpy(address->in_addr.in6.s6_addr + 8, link->network->dhcp6_delegation_prefix_token.in6.s6_addr + 8, 8);
1111 else {
1112 r = generate_ipv6_eui_64_address(link, &address->in_addr.in6);
1113 if (r < 0)
1114 return log_link_warning_errno(link, r, "Failed to generate EUI64 address for acquired DHCPv6 delegated prefix: %m");
1115 }
1116
1117 address->prefixlen = prefix_len;
1118 address->family = AF_INET6;
1119 address->cinfo.ifa_prefered = lifetime_preferred;
1120 address->cinfo.ifa_valid = lifetime_valid;
1121
1122 link->dhcp6_pd_address_configured = false;
1123 link_set_state(link, LINK_STATE_CONFIGURING);
1124
1125 r = address_configure(address, link, dhcp6_pd_address_handler, true);
1126 if (r < 0)
1127 return log_link_warning_errno(link, r, "Failed to set acquired DHCPv6 delegated prefix address: %m");
1128 if (r > 0)
1129 link->dhcp6_pd_address_messages++;
1130
1131 return 0;
1132 }
1133
1134 int config_parse_dhcp6_pd_hint(
1135 const char* unit,
1136 const char *filename,
1137 unsigned line,
1138 const char *section,
1139 unsigned section_line,
1140 const char *lvalue,
1141 int ltype,
1142 const char *rvalue,
1143 void *data,
1144 void *userdata) {
1145
1146 Network *network = data;
1147 int r;
1148
1149 assert(filename);
1150 assert(lvalue);
1151 assert(rvalue);
1152 assert(data);
1153
1154 r = in_addr_prefix_from_string(rvalue, AF_INET6, (union in_addr_union *) &network->dhcp6_pd_address, &network->dhcp6_pd_length);
1155 if (r < 0) {
1156 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse PrefixDelegationHint=%s, ignoring assignment", rvalue);
1157 return 0;
1158 }
1159
1160 if (network->dhcp6_pd_length < 1 || network->dhcp6_pd_length > 128) {
1161 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid prefix length='%d', ignoring assignment", network->dhcp6_pd_length);
1162 network->dhcp6_pd_length = 0;
1163 return 0;
1164 }
1165
1166 return 0;
1167 }
1168
1169 int config_parse_dhcp6_mud_url(
1170 const char *unit,
1171 const char *filename,
1172 unsigned line,
1173 const char *section,
1174 unsigned section_line,
1175 const char *lvalue,
1176 int ltype,
1177 const char *rvalue,
1178 void *data,
1179 void *userdata) {
1180
1181 _cleanup_free_ char *unescaped = NULL;
1182 Network *network = data;
1183 int r;
1184
1185 assert(filename);
1186 assert(lvalue);
1187 assert(rvalue);
1188
1189 if (isempty(rvalue)) {
1190 network->dhcp6_mudurl = mfree(network->dhcp6_mudurl);
1191 return 0;
1192 }
1193
1194 r = cunescape(rvalue, 0, &unescaped);
1195 if (r < 0) {
1196 log_syntax(unit, LOG_ERR, filename, line, r,
1197 "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
1198 return 0;
1199 }
1200
1201 if (!http_url_is_valid(unescaped) || strlen(unescaped) > UINT8_MAX) {
1202 log_syntax(unit, LOG_ERR, filename, line, 0,
1203 "Failed to parse MUD URL '%s', ignoring: %m", rvalue);
1204
1205 return 0;
1206 }
1207
1208 return free_and_replace(network->dhcp6_mudurl, unescaped);
1209 }
1210
1211 int config_parse_dhcp6_delegated_prefix_token(
1212 const char *unit,
1213 const char *filename,
1214 unsigned line,
1215 const char *section,
1216 unsigned section_line,
1217 const char *lvalue,
1218 int ltype,
1219 const char *rvalue,
1220 void *data,
1221 void *userdata) {
1222
1223 Network *network = data;
1224 int r;
1225
1226 assert(filename);
1227 assert(lvalue);
1228 assert(rvalue);
1229 assert(data);
1230
1231 if (isempty(rvalue)) {
1232 network->dhcp6_delegation_prefix_token = IN_ADDR_NULL;
1233 return 0;
1234 }
1235
1236 r = in_addr_from_string(AF_INET6, rvalue, &network->dhcp6_delegation_prefix_token);
1237 if (r < 0) {
1238 log_syntax(unit, LOG_ERR, filename, line, r,
1239 "Failed to parse DHCPv6 %s, ignoring: %s", lvalue, rvalue);
1240 return 0;
1241 }
1242
1243 if (in_addr_is_null(AF_INET6, &network->dhcp6_delegation_prefix_token)) {
1244 log_syntax(unit, LOG_ERR, filename, line, 0,
1245 "DHCPv6 %s cannot be the ANY address, ignoring: %s", lvalue, rvalue);
1246 return 0;
1247 }
1248
1249 return 0;
1250 }
1251
1252 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp6_client_start_mode, dhcp6_client_start_mode, DHCP6ClientStartMode,
1253 "Failed to parse WithoutRA= setting");
1254
1255 static const char* const dhcp6_client_start_mode_table[_DHCP6_CLIENT_START_MODE_MAX] = {
1256 [DHCP6_CLIENT_START_MODE_NO] = "no",
1257 [DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST] = "information-request",
1258 [DHCP6_CLIENT_START_MODE_SOLICIT] = "solicit",
1259 };
1260
1261 DEFINE_STRING_TABLE_LOOKUP(dhcp6_client_start_mode, DHCP6ClientStartMode);