]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
5c79bd79 2/***
810adae9 3 Copyright © 2014 Intel Corporation. All rights reserved.
5c79bd79
PF
4***/
5
9aa5d8ba 6#include <netinet/in.h>
5c79bd79 7#include <linux/if.h>
8f815e8b 8#include <linux/if_arp.h>
5c79bd79 9
5c79bd79
PF
10#include "sd-dhcp6-client.h"
11
c24dd739 12#include "escape.h"
76c3246d 13#include "hashmap.h"
8006aa32 14#include "hostname-util.h"
aeed8332 15#include "missing_network.h"
07630cea 16#include "network-internal.h"
ca5ad760 17#include "networkd-dhcp6.h"
23f53b99
TG
18#include "networkd-link.h"
19#include "networkd-manager.h"
1d596fde 20#include "networkd-radv.h"
76c3246d 21#include "siphash24.h"
838d39af 22#include "string-table.h"
76c3246d
PF
23#include "string-util.h"
24#include "radv-internal.h"
c24dd739 25#include "web-util.h"
07630cea 26
04ed9949
YW
27static Link *dhcp6_prefix_get(Manager *m, struct in6_addr *addr);
28static int dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link);
29static int dhcp6_prefix_remove_all(Manager *m, Link *link);
9efa8a3c
SS
30static 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);
be3a09b7 34
2a877f45 35bool dhcp6_get_prefix_delegation(Link *link) {
03d4fc2e 36 if (!link->network)
103b81ee 37 return false;
103b81ee 38
a9eea606
YW
39 return IN_SET(link->network->router_prefix_delegation,
40 RADV_PREFIX_DELEGATION_DHCP6,
41 RADV_PREFIX_DELEGATION_BOTH);
103b81ee
PF
42}
43
02e9e34b
AR
44static 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
51static 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) {
02e9e34b 57
120b5c0b 58 int64_t subnet_id = link->network->router_prefix_subnet_id;
02e9e34b
AR
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;
120b5c0b
SS
62 union in_addr_union pd_prefix_union = {
63 .in6 = *pd_prefix,
64 };
02e9e34b
AR
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 };
120b5c0b
SS
70 int r;
71
72 assert(pd_prefix_len <= 64);
73 assert(manager);
74 assert(link);
75 assert(link->network);
02e9e34b
AR
76
77 if (subnet_id >= 0) {
78 /* If the link has a preference for a particular subnet id try to allocate that */
1419ff04 79 if ((uint64_t) subnet_id >= n_prefixes)
02e9e34b
AR
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;
1419ff04 113 }
02e9e34b 114
1419ff04
YW
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;
02e9e34b
AR
123 }
124
1419ff04
YW
125 r = in_addr_prefix_next(AF_INET6, &prefix, 64);
126 if (r < 0)
feb596b5 127 return log_link_error_errno(link, r, "Can't allocate another prefix. Out of address space?: %m");
02e9e34b
AR
128 }
129
1419ff04 130 return log_link_warning_errno(link, SYNTHETIC_ERRNO(ERANGE), "Couldn't find a suitable prefix. Ran out of address space.");
02e9e34b
AR
131}
132
103b81ee
PF
133static 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
03d4fc2e 147 if (!dhcp6_get_prefix_delegation(l))
103b81ee
PF
148 continue;
149
150 return true;
151 }
152
153 return false;
154}
155
8fc2a5df 156static int dhcp6_lease_information_acquired(sd_dhcp6_client *client, Link *link) {
c62c4628
PF
157 return 0;
158}
159
76c3246d
PF
160static 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) {
76c3246d 164 int r;
76c3246d 165
1d596fde 166 r = radv_add_prefix(link, prefix, prefix_len, lifetime_preferred, lifetime_valid);
76c3246d
PF
167 if (r < 0)
168 return r;
169
04ed9949 170 r = dhcp6_prefix_add(link->manager, prefix, link);
76c3246d
PF
171 if (r < 0)
172 return r;
173
2a877f45
YW
174 r = dhcp6_assign_delegated_prefix(link, prefix, prefix_len, lifetime_preferred, lifetime_valid);
175 if (r < 0)
176 return r;
9efa8a3c 177
1d596fde 178 return 0;
76c3246d
PF
179}
180
302a796f 181static int dhcp6_route_remove_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
494c868d
PF
182 int r;
183
1046bf9b
YW
184 assert(link);
185
4ff296b0
YW
186 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
187 return 1;
188
494c868d
PF
189 r = sd_netlink_message_get_errno(m);
190 if (r < 0)
5ecb131d 191 log_link_message_warning_errno(link, m, r, "Received error on unreachable route removal for DHCPv6 delegated subnet");
494c868d 192
0ae286e6 193 return 1;
494c868d
PF
194}
195
65dd5e31 196int dhcp6_lease_pd_prefix_lost(sd_dhcp6_client *client, Link* link) {
120b5c0b 197 uint32_t lifetime_preferred, lifetime_valid;
494c868d
PF
198 union in_addr_union pd_prefix;
199 uint8_t pd_prefix_len;
120b5c0b
SS
200 sd_dhcp6_lease *lease;
201 int r;
494c868d
PF
202
203 r = sd_dhcp6_client_get_lease(client, &lease);
204 if (r < 0)
205 return r;
206
494c868d
PF
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;
f1368755 213 _cleanup_(route_freep) Route *route = NULL;
494c868d 214
ca7c792b 215 if (pd_prefix_len >= 64)
494c868d
PF
216 continue;
217
218 (void) in_addr_to_string(AF_INET6, &pd_prefix, &buf);
219
f1368755
YW
220 r = route_new(&route);
221 if (r < 0)
222 return r;
dd5ab7d9 223
f1368755
YW
224 route->family = AF_INET6;
225 route->dst = pd_prefix;
226 route->dst_prefixlen = pd_prefix_len;
227 route->type = RTN_UNREACHABLE;
494c868d 228
ca7c792b
YW
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;
494c868d 235 }
ca7c792b
YW
236
237 log_link_debug(link, "Removing unreachable route %s/%u",
238 strnull(buf), pd_prefix_len);
494c868d
PF
239 }
240
241 return 0;
242}
243
02e9e34b 244static int dhcp6_pd_prefix_distribute(Link *dhcp6_link,
76c3246d
PF
245 struct in6_addr *pd_prefix,
246 uint8_t pd_prefix_len,
247 uint32_t lifetime_preferred,
02e9e34b
AR
248 uint32_t lifetime_valid,
249 bool assign_preferred_subnet_id) {
120b5c0b
SS
250
251 _cleanup_free_ char *assigned_buf = NULL, *buf = NULL;
76c3246d 252 Manager *manager = dhcp6_link->manager;
02e9e34b
AR
253 union in_addr_union prefix = {
254 .in6 = *pd_prefix,
255 };
120b5c0b 256 bool pool_depleted = false;
02e9e34b 257 uint64_t n_prefixes;
120b5c0b
SS
258 Iterator i;
259 Link *link;
76c3246d
PF
260 int r;
261
262 assert(manager);
263 assert(pd_prefix_len <= 64);
264
76c3246d
PF
265 r = in_addr_mask(AF_INET6, &prefix, pd_prefix_len);
266 if (r < 0)
267 return r;
268
9fb96abd 269 n_prefixes = UINT64_C(1) << (64 - pd_prefix_len);
76c3246d
PF
270
271 (void) in_addr_to_string(AF_INET6, &prefix, &buf);
9fb96abd 272 log_link_debug(dhcp6_link, "Assigning up to %" PRIu64 " prefixes from %s/%u",
76c3246d
PF
273 n_prefixes, strnull(buf), pd_prefix_len);
274
02e9e34b
AR
275 HASHMAP_FOREACH(link, manager->links, i) {
276 union in_addr_union assigned_prefix;
76c3246d
PF
277
278 if (link == dhcp6_link)
279 continue;
280
03d4fc2e 281 if (!dhcp6_get_prefix_delegation(link))
76c3246d
PF
282 continue;
283
02e9e34b
AR
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,
cecd68c7 288 &assigned_prefix.in6);
02e9e34b
AR
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,
76c3246d
PF
301 lifetime_preferred, lifetime_valid);
302 if (r < 0) {
02e9e34b 303 log_link_error_errno(link, r, "Unable to assign/update prefix %s/64 from %s/%u for link: %m",
eb75b919 304 strnull(assigned_buf),
76c3246d 305 strnull(buf), pd_prefix_len);
76c3246d 306 } else
02e9e34b 307 log_link_debug(link, "Assigned prefix %s/64 from %s/%u to link",
eb75b919
PF
308 strnull(assigned_buf),
309 strnull(buf), pd_prefix_len);
76c3246d
PF
310 }
311
02e9e34b
AR
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
494c868d
PF
317 return 0;
318}
26db55f3 319
302a796f 320static int dhcp6_route_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
494c868d 321 int r;
73922903 322
1046bf9b 323 assert(link);
2a877f45
YW
324 assert(link->dhcp6_route_messages > 0);
325
326 link->dhcp6_route_messages--;
1046bf9b 327
4ff296b0
YW
328 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
329 return 1;
330
494c868d 331 r = sd_netlink_message_get_errno(m);
2a877f45
YW
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 }
73922903 343
0ae286e6 344 return 1;
76c3246d
PF
345}
346
347static int dhcp6_lease_pd_prefix_acquired(sd_dhcp6_client *client, Link *link) {
120b5c0b 348 uint32_t lifetime_preferred, lifetime_valid;
494c868d 349 union in_addr_union pd_prefix;
120b5c0b 350 sd_dhcp6_lease *lease;
76c3246d 351 uint8_t pd_prefix_len;
120b5c0b 352 int r;
76c3246d 353
2a877f45
YW
354 link->dhcp6_route_configured = false;
355
76c3246d
PF
356 r = sd_dhcp6_client_get_lease(client, &lease);
357 if (r < 0)
358 return r;
359
76c3246d
PF
360 sd_dhcp6_lease_reset_pd_prefix_iter(lease);
361
494c868d 362 while (sd_dhcp6_lease_get_pd(lease, &pd_prefix.in6, &pd_prefix_len,
76c3246d
PF
363 &lifetime_preferred,
364 &lifetime_valid) >= 0) {
365
ca7c792b
YW
366 _cleanup_free_ char *buf = NULL;
367
368 (void) in_addr_to_string(AF_INET6, &pd_prefix, &buf);
369
76c3246d
PF
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
ca7c792b 376 if (pd_prefix_len < 48)
3ec8303f
PF
377 log_link_warning(link, "PD Prefix length < 48, looks unusual %s/%u",
378 strnull(buf), pd_prefix_len);
3ec8303f 379
494c868d 380 if (pd_prefix_len < 64) {
f1368755 381 _cleanup_(route_freep) Route *route = NULL;
bdb9f580 382
f1368755
YW
383 r = route_new(&route);
384 if (r < 0)
385 return r;
57445b53 386
f1368755
YW
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;
494c868d 392
c8ee637e 393 r = route_configure(route, link, dhcp6_route_handler);
494c868d
PF
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);
494c868d
PF
398 continue;
399 }
2a877f45
YW
400 if (r > 0)
401 link->dhcp6_route_messages++;
494c868d 402
494c868d
PF
403 log_link_debug(link, "Configuring unreachable route for %s/%u",
404 strnull(buf), pd_prefix_len);
494c868d
PF
405 } else
406 log_link_debug(link, "Not adding a blocking route since distributed prefix is /64");
407
02e9e34b
AR
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
ceae6295 414 * If a subnet id request couldn't be fulfilled the failure will be logged (as error)
02e9e34b
AR
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,
76c3246d
PF
423 pd_prefix_len,
424 lifetime_preferred,
02e9e34b
AR
425 lifetime_valid,
426 true);
76c3246d
PF
427 if (r < 0 && r != -EAGAIN)
428 return r;
429
02e9e34b
AR
430 /* if r == -EAGAIN then the allocation failed because we ran
431 * out of addresses for the preferred subnet id's. This doesn't
ceae6295 432 * mean we can't fulfill other prefix requests.
02e9e34b
AR
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
ceae6295 448 * fulfill those with the next available pd delegated prefix. */
76c3246d
PF
449 }
450
2a877f45
YW
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
76c3246d
PF
459 return 0;
460}
461
10752343
PF
462int 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) {
feb596b5 485 log_link_warning_errno(l, r, "Cannot get prefix delegation when adding new link: %m");
10752343
PF
486 continue;
487 }
488
489 if (enabled == 0) {
490 r = sd_dhcp6_client_set_prefix_delegation(l->dhcp6_client, 1);
491 if (r < 0) {
feb596b5 492 log_link_warning_errno(l, r, "Cannot enable prefix delegation when adding new link: 5m");
10752343
PF
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) {
feb596b5 510 log_link_warning_errno(l, r, "Cannot stop DHCPv6 prefix delegation client after adding new link: %m");
10752343
PF
511 continue;
512 }
513
514 r = sd_dhcp6_client_start(l->dhcp6_client);
515 if (r < 0) {
feb596b5 516 log_link_warning_errno(l, r, "Cannot restart DHCPv6 prefix delegation client after adding new link: %m");
10752343
PF
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
302a796f 526static int dhcp6_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
c62c4628
PF
527 int r;
528
529 assert(link);
2a877f45
YW
530 assert(link->dhcp6_address_messages > 0);
531
532 link->dhcp6_address_messages--;
c62c4628 533
4ff296b0
YW
534 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
535 return 1;
536
1c4baffc 537 r = sd_netlink_message_get_errno(m);
c62c4628 538 if (r < 0 && r != -EEXIST) {
5ecb131d 539 log_link_message_warning_errno(link, m, r, "Could not set DHCPv6 address");
c62c4628 540 link_enter_failed(link);
73854ba1 541 return 1;
8107f473 542 } else if (r >= 0)
4ff296b0 543 (void) manager_rtnl_process_address(rtnl, m, link->manager);
c62c4628 544
2a877f45
YW
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 }
4ff296b0 553 }
6545067a 554
c62c4628
PF
555 return 1;
556}
557
1e7a0e21
LP
558static int dhcp6_address_change(
559 Link *link,
560 struct in6_addr *ip6_addr,
561 uint32_t lifetime_preferred,
562 uint32_t lifetime_valid) {
563
8e766630 564 _cleanup_(address_freep) Address *addr = NULL;
57e44707 565 _cleanup_free_ char *buffer = NULL;
1e7a0e21 566 int r;
c62c4628 567
f0213e37 568 r = address_new(&addr);
c62c4628
PF
569 if (r < 0)
570 return r;
571
572 addr->family = AF_INET6;
57e44707 573 addr->in_addr.in6 = *ip6_addr;
851c9f82 574 addr->flags = IFA_F_NOPREFIXROUTE;
6d8f6b0b 575 addr->prefixlen = 128;
c62c4628
PF
576 addr->cinfo.ifa_prefered = lifetime_preferred;
577 addr->cinfo.ifa_valid = lifetime_valid;
578
57e44707 579 (void) in_addr_to_string(addr->family, &addr->in_addr, &buffer);
f2341e0a 580 log_link_info(link,
1e7a0e21 581 "DHCPv6 address %s/%d timeout preferred %d valid %d",
57e44707 582 strnull(buffer), addr->prefixlen, lifetime_preferred, lifetime_valid);
c62c4628 583
66669078 584 r = address_configure(addr, link, dhcp6_address_handler, true);
c62c4628 585 if (r < 0)
4ff296b0 586 return log_link_warning_errno(link, r, "Could not assign DHCPv6 address: %m");
2a877f45
YW
587 if (r > 0)
588 link->dhcp6_address_messages++;
c62c4628 589
4ff296b0 590 return 0;
c62c4628
PF
591}
592
c62c4628
PF
593static 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;
c62c4628 598
2a877f45
YW
599 link->dhcp6_address_configured = false;
600
c62c4628
PF
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);
c62c4628 606 while (sd_dhcp6_lease_get_address(lease, &ip6_addr,
483d099e
ZJS
607 &lifetime_preferred,
608 &lifetime_valid) >= 0) {
c62c4628 609
6d8f6b0b 610 r = dhcp6_address_change(link, &ip6_addr, lifetime_preferred, lifetime_valid);
c62c4628
PF
611 if (r < 0)
612 return r;
613 }
614
2a877f45
YW
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");
0c816fcc
YW
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;
2a877f45
YW
625 link_set_state(link, LINK_STATE_CONFIGURING);
626 }
627
c62c4628
PF
628 return 0;
629}
630
5c79bd79 631static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
c62c4628 632 int r;
5c79bd79
PF
633 Link *link = userdata;
634
635 assert(link);
636 assert(link->network);
5c79bd79
PF
637
638 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
639 return;
640
641 switch(event) {
10c9ce61
DH
642 case SD_DHCP6_CLIENT_EVENT_STOP:
643 case SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE:
644 case SD_DHCP6_CLIENT_EVENT_RETRANS_MAX:
3098562c
TG
645 if (sd_dhcp6_client_get_lease(client, NULL) >= 0)
646 log_link_warning(link, "DHCPv6 lease lost");
18d29550 647
494c868d 648 (void) dhcp6_lease_pd_prefix_lost(client, link);
04ed9949 649 (void) dhcp6_prefix_remove_all(link->manager, link);
76c3246d 650
6787917d 651 link_dirty(link);
c62c4628
PF
652 break;
653
10c9ce61 654 case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE:
c62c4628
PF
655 r = dhcp6_lease_address_acquired(client, link);
656 if (r < 0) {
657 link_enter_failed(link);
658 return;
659 }
660
76c3246d
PF
661 r = dhcp6_lease_pd_prefix_acquired(client, link);
662 if (r < 0)
feb596b5 663 log_link_debug_errno(link, r, "DHCPv6 did not receive prefixes to delegate: %m");
76c3246d 664
4831981d 665 _fallthrough_;
10c9ce61 666 case SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST:
c62c4628
PF
667 r = dhcp6_lease_information_acquired(client, link);
668 if (r < 0) {
669 link_enter_failed(link);
670 return;
671 }
5c79bd79 672
6787917d 673 link_dirty(link);
5c79bd79
PF
674 break;
675
676 default:
677 if (event < 0)
e53fc357 678 log_link_warning_errno(link, event, "DHCPv6 error: %m");
5c79bd79 679 else
e53fc357 680 log_link_warning(link, "DHCPv6 unknown event: %d", event);
5c79bd79
PF
681 return;
682 }
18d29550 683
8012cd39 684 link_check_ready(link);
5c79bd79
PF
685}
686
720bec40 687int dhcp6_request_address(Link *link, int ir) {
125f20b4 688 int r, inf_req, pd;
7a695d8e 689 bool running;
5c79bd79 690
7a695d8e
TG
691 assert(link);
692 assert(link->dhcp6_client);
125f20b4 693 assert(link->network);
720bec40 694 assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0);
85bd849f 695
7a695d8e
TG
696 r = sd_dhcp6_client_is_running(link->dhcp6_client);
697 if (r < 0)
698 return r;
5e871240 699 running = r;
e6604041 700
125f20b4
PF
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);
2a877f45 710 if (r < 0)
125f20b4
PF
711 return r;
712
713 ir = false;
714 }
715
7a695d8e 716 if (running) {
720bec40
TY
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
7a695d8e
TG
724 r = sd_dhcp6_client_stop(link->dhcp6_client);
725 if (r < 0)
726 return r;
720bec40
TY
727 } else {
728 r = sd_dhcp6_client_set_local_address(link->dhcp6_client, &link->ipv6ll_address);
729 if (r < 0)
730 return r;
7a695d8e 731 }
85bd849f 732
720bec40
TY
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);
7a695d8e
TG
738 if (r < 0)
739 return r;
85bd849f 740
7a695d8e
TG
741 return 0;
742}
18d29550 743
8006aa32
SA
744static 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
a8494759
YW
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;
8006aa32
SA
771}
772
7a695d8e 773int dhcp6_configure(Link *link) {
5bad7ebd 774 _cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL;
b4ccc5de 775 sd_dhcp6_option *vendor_option;
e7d5fe17 776 sd_dhcp6_option *send_option;
35f6a5cb 777 void *request_options;
8341a5c3 778 const DUID *duid;
e7d5fe17 779 Iterator i;
fb5c8216 780 int r;
7a695d8e
TG
781
782 assert(link);
fb5c8216 783 assert(link->network);
7a695d8e 784
62379e88
TG
785 if (link->dhcp6_client)
786 return 0;
787
7a695d8e 788 r = sd_dhcp6_client_new(&client);
5bad7ebd
YW
789 if (r == -ENOMEM)
790 return log_oom();
7a695d8e 791 if (r < 0)
5bad7ebd 792 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to create DHCP6 client: %m");
5c79bd79 793
7a695d8e 794 r = sd_dhcp6_client_attach_event(client, NULL, 0);
5c79bd79 795 if (r < 0)
5bad7ebd 796 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to attach event: %m");
5c79bd79 797
7a695d8e 798 r = sd_dhcp6_client_set_mac(client,
5c79bd79
PF
799 (const uint8_t *) &link->mac,
800 sizeof (link->mac), ARPHRD_ETHER);
e6604041 801 if (r < 0)
5bad7ebd 802 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set MAC address: %m");
5c79bd79 803
8217ed5e
TH
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 }
413708d1 809
f24648a6 810 duid = link_get_duid(link);
0cf7c3fd
YW
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);
413708d1 818 if (r < 0)
5bad7ebd 819 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set DUID: %m");
413708d1 820
e7d5fe17
AD
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
8006aa32 829 r = dhcp6_set_hostname(client, link);
8006aa32 830 if (r < 0)
a8494759 831 return r;
8006aa32 832
2f8e7633 833 r = sd_dhcp6_client_set_ifindex(client, link->ifindex);
e6604041 834 if (r < 0)
5bad7ebd 835 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set ifindex: %m");
5c79bd79 836
fb5c8216
SS
837 if (link->network->rapid_commit) {
838 r = sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_RAPID_COMMIT);
839 if (r < 0)
5bad7ebd 840 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set request flag for rapid commit: %m");
fb5c8216
SS
841 }
842
3175a8c2
SS
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
35f6a5cb
SS
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 }
35f6a5cb
SS
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
f37f2a6b
SS
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
ed0d1b2e
SS
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
b4ccc5de
SS
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
7a695d8e 881 r = sd_dhcp6_client_set_callback(client, dhcp6_handler, link);
e6604041 882 if (r < 0)
5bad7ebd 883 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set callback: %m");
5c79bd79 884
103b81ee
PF
885 if (dhcp6_enable_prefix_delegation(link)) {
886 r = sd_dhcp6_client_set_prefix_delegation(client, true);
887 if (r < 0)
5bad7ebd 888 return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set prefix delegation: %m");
103b81ee
PF
889 }
890
2805536b
SS
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
5bad7ebd 897 link->dhcp6_client = TAKE_PTR(client);
e6604041 898
7a695d8e 899 return 0;
5c79bd79 900}
04ed9949
YW
901
902static 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
2a877f45 909static int dhcp6_pd_route_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
04ed9949
YW
910 int r;
911
912 assert(link);
2a877f45
YW
913 assert(link->dhcp6_pd_route_messages > 0);
914
915 link->dhcp6_pd_route_messages--;
04ed9949 916
4ff296b0
YW
917 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
918 return 1;
919
04ed9949 920 r = sd_netlink_message_get_errno(m);
4ff296b0 921 if (r < 0 && r != -EEXIST) {
2a877f45 922 log_link_message_warning_errno(link, m, r, "Failed to add DHCPv6 Prefix Delegation route");
4ff296b0
YW
923 link_enter_failed(link);
924 return 1;
925 }
04ed9949 926
2a877f45
YW
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
4ff296b0 933 return 1;
04ed9949
YW
934}
935
936static int dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) {
f1368755 937 _cleanup_(route_freep) Route *route = NULL;
04ed9949
YW
938 _cleanup_free_ struct in6_addr *a = NULL;
939 _cleanup_free_ char *buf = NULL;
940 Link *assigned_link;
04ed9949
YW
941 int r;
942
943 assert_return(m, -EINVAL);
944 assert_return(addr, -EINVAL);
945
f1368755 946 r = route_new(&route);
04ed9949
YW
947 if (r < 0)
948 return r;
949
f1368755
YW
950 route->family = AF_INET6;
951 route->dst.in6 = *addr;
952 route->dst_prefixlen = 64;
953
2a877f45
YW
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);
04ed9949
YW
958 if (r < 0)
959 return r;
2a877f45
YW
960 if (r > 0)
961 link->dhcp6_pd_route_messages++;
04ed9949
YW
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
989static int dhcp6_prefix_remove_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
990 int r;
991
992 assert(link);
993
4ff296b0
YW
994 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
995 return 1;
996
04ed9949 997 r = sd_netlink_message_get_errno(m);
4ff296b0 998 if (r < 0) {
5ecb131d 999 log_link_message_warning_errno(link, m, r, "Received error on DHCPv6 Prefix Delegation route removal");
4ff296b0
YW
1000 link_enter_failed(link);
1001 return 1;
1002 }
04ed9949
YW
1003
1004 return 1;
1005}
1006
1007int dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) {
1008 _cleanup_free_ struct in6_addr *a = NULL;
1009 _cleanup_(link_unrefp) Link *l = NULL;
f1368755 1010 _cleanup_(route_freep) Route *route = NULL;
04ed9949 1011 _cleanup_free_ char *buf = NULL;
04ed9949
YW
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);
f1368755
YW
1022
1023 r = route_new(&route);
04ed9949
YW
1024 if (r < 0)
1025 return r;
1026
f1368755
YW
1027 route->family = AF_INET6;
1028 route->dst.in6 = *addr;
1029 route->dst_prefixlen = 64;
1030
04ed9949
YW
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
1041static 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}
02e9e34b 1055
2a877f45 1056static int dhcp6_pd_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
a211ff4b
YW
1057 int r;
1058
1059 assert(link);
2a877f45
YW
1060 assert(link->dhcp6_pd_address_messages > 0);
1061
1062 link->dhcp6_pd_address_messages--;
a211ff4b
YW
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) {
2a877f45 1069 log_link_message_warning_errno(link, m, r, "Could not set DHCPv6 delegated prefix address");
a211ff4b
YW
1070 link_enter_failed(link);
1071 return 1;
1072 } else if (r >= 0)
1073 (void) manager_rtnl_process_address(rtnl, m, link->manager);
1074
2a877f45
YW
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 }
a211ff4b
YW
1083 }
1084
1085 return 1;
1086}
1087
9efa8a3c
SS
1088static 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) {
120b5c0b 1093
9efa8a3c
SS
1094 _cleanup_(address_freep) Address *address = NULL;
1095 int r;
1096
1097 assert(link);
1098 assert(link->network);
1099 assert(prefix);
1100
2a877f45
YW
1101 if (!link->network->dhcp6_pd_assign_prefix) {
1102 link->dhcp6_pd_address_configured = true;
9efa8a3c 1103 return 0;
2a877f45 1104 }
9efa8a3c
SS
1105
1106 r = address_new(&address);
1107 if (r < 0)
120b5c0b 1108 return log_link_error_errno(link, r, "Failed to allocate address for DHCPv6 delegated prefix: %m");
9efa8a3c
SS
1109
1110 address->in_addr.in6 = *prefix;
120b5c0b
SS
1111
1112 if (!in_addr_is_null(AF_INET6, &link->network->dhcp6_delegation_prefix_token))
8d9982e3 1113 memcpy(address->in_addr.in6.s6_addr + 8, link->network->dhcp6_delegation_prefix_token.in6.s6_addr + 8, 8);
120b5c0b
SS
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 }
9efa8a3c
SS
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
0c816fcc
YW
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;
2a877f45 1130 link->dhcp6_pd_address_configured = false;
9efa8a3c
SS
1131 link_set_state(link, LINK_STATE_CONFIGURING);
1132
2a877f45 1133 r = address_configure(address, link, dhcp6_pd_address_handler, true);
9efa8a3c 1134 if (r < 0)
120b5c0b 1135 return log_link_warning_errno(link, r, "Failed to set acquired DHCPv6 delegated prefix address: %m");
2a877f45
YW
1136 if (r > 0)
1137 link->dhcp6_pd_address_messages++;
9efa8a3c
SS
1138
1139 return 0;
1140}
c24dd739
YW
1141
1142int 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
1177int 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) {
120b5c0b 1188
c24dd739
YW
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}
120b5c0b
SS
1218
1219int 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}
838d39af
SS
1259
1260DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp6_client_start_mode, dhcp6_client_start_mode, DHCP6ClientStartMode,
1261 "Failed to parse WithoutRA= setting");
1262
1263static 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
1269DEFINE_STRING_TABLE_LOOKUP(dhcp6_client_start_mode, DHCP6ClientStartMode);