Debian Codegraph @

src/network/networkd-radv.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2017 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <netinet/icmp6.h>
7
8 #include "sd-radv.h"
9
10 #include "conf-parser.h"
11 #include "dns-domain.h"
12 #include "extract-word.h"
13 #include "ndisc-router-internal.h"
14 #include "networkd-address.h"
15 #include "networkd-address-generation.h"
16 #include "networkd-dhcp-prefix-delegation.h"
17 #include "networkd-ipv6ll.h"
18 #include "networkd-link.h"
19 #include "networkd-manager.h"
20 #include "networkd-network.h"
21 #include "networkd-queue.h"
22 #include "networkd-radv.h"
23 #include "networkd-route-util.h"
24 #include "ordered-set.h"
25 #include "parse-util.h"
26 #include "radv-internal.h"
27 #include "set.h"
28 #include "string-table.h"
29 #include "string-util.h"
30 #include "strv.h"
31
32 bool link_radv_enabled(Link *link) {
33 assert(link);
34
35 if (!link_multicast_enabled(link))
36 return false;
37
38 if (!link_ipv6ll_enabled_harder(link))
39 return false;
40
41 return link->network->router_prefix_delegation;
42 }
43
44 static Prefix* prefix_free(Prefix *prefix) {
45 if (!prefix)
46 return NULL;
47
48 if (prefix->network) {
49 assert(prefix->section);
50 hashmap_remove(prefix->network->prefixes_by_section, prefix->section);
51 }
52
53 config_section_free(prefix->section);
54 set_free(prefix->tokens);
55
56 return mfree(prefix);
57 }
58
59 DEFINE_SECTION_CLEANUP_FUNCTIONS(Prefix, prefix_free);
60
61 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
62 prefix_hash_ops_by_section,
63 ConfigSection, config_section_hash_func, config_section_compare_func,
64 Prefix, prefix_free);
65
66 static int prefix_new_static(Network *network, const char *filename, unsigned section_line, Prefix **ret) {
67 _cleanup_(config_section_freep) ConfigSection *n = NULL;
68 _cleanup_(prefix_freep) Prefix *prefix = NULL;
69 int r;
70
71 assert(network);
72 assert(ret);
73 assert(filename);
74 assert(section_line > 0);
75
76 r = config_section_new(filename, section_line, &n);
77 if (r < 0)
78 return r;
79
80 prefix = hashmap_get(network->prefixes_by_section, n);
81 if (prefix) {
82 *ret = TAKE_PTR(prefix);
83 return 0;
84 }
85
86 prefix = new(Prefix, 1);
87 if (!prefix)
88 return -ENOMEM;
89
90 *prefix = (Prefix) {
91 .network = network,
92 .section = TAKE_PTR(n),
93
94 .prefix.flags = ND_OPT_PI_FLAG_ONLINK | ND_OPT_PI_FLAG_AUTO,
95 .prefix.valid_lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC,
96 .prefix.preferred_lifetime = RADV_DEFAULT_PREFERRED_LIFETIME_USEC,
97 .prefix.valid_until = USEC_INFINITY,
98 .prefix.preferred_until = USEC_INFINITY,
99 };
100
101 r = hashmap_ensure_put(&network->prefixes_by_section, &prefix_hash_ops_by_section, prefix->section, prefix);
102 if (r < 0)
103 return r;
104
105 *ret = TAKE_PTR(prefix);
106 return 0;
107 }
108
109 static RoutePrefix* route_prefix_free(RoutePrefix *prefix) {
110 if (!prefix)
111 return NULL;
112
113 if (prefix->network) {
114 assert(prefix->section);
115 hashmap_remove(prefix->network->route_prefixes_by_section, prefix->section);
116 }
117
118 config_section_free(prefix->section);
119
120 return mfree(prefix);
121 }
122
123 DEFINE_SECTION_CLEANUP_FUNCTIONS(RoutePrefix, route_prefix_free);
124
125 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
126 route_prefix_hash_ops_by_section,
127 ConfigSection, config_section_hash_func, config_section_compare_func,
128 RoutePrefix, route_prefix_free);
129
130 static int route_prefix_new_static(Network *network, const char *filename, unsigned section_line, RoutePrefix **ret) {
131 _cleanup_(config_section_freep) ConfigSection *n = NULL;
132 _cleanup_(route_prefix_freep) RoutePrefix *prefix = NULL;
133 int r;
134
135 assert(network);
136 assert(ret);
137 assert(filename);
138 assert(section_line > 0);
139
140 r = config_section_new(filename, section_line, &n);
141 if (r < 0)
142 return r;
143
144 prefix = hashmap_get(network->route_prefixes_by_section, n);
145 if (prefix) {
146 *ret = TAKE_PTR(prefix);
147 return 0;
148 }
149
150 prefix = new(RoutePrefix, 1);
151 if (!prefix)
152 return -ENOMEM;
153
154 *prefix = (RoutePrefix) {
155 .network = network,
156 .section = TAKE_PTR(n),
157
158 .route.lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC,
159 .route.valid_until = USEC_INFINITY,
160 };
161
162 r = hashmap_ensure_put(&network->route_prefixes_by_section, &route_prefix_hash_ops_by_section, prefix->section, prefix);
163 if (r < 0)
164 return r;
165
166 *ret = TAKE_PTR(prefix);
167 return 0;
168 }
169
170 static Prefix64* prefix64_free(Prefix64 *prefix) {
171 if (!prefix)
172 return NULL;
173
174 if (prefix->network) {
175 assert(prefix->section);
176 hashmap_remove(prefix->network->pref64_prefixes_by_section, prefix->section);
177 }
178
179 config_section_free(prefix->section);
180
181 return mfree(prefix);
182 }
183
184 DEFINE_SECTION_CLEANUP_FUNCTIONS(Prefix64, prefix64_free);
185
186 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
187 prefix64_hash_ops_by_section,
188 ConfigSection, config_section_hash_func, config_section_compare_func,
189 Prefix64, prefix64_free);
190
191 static int prefix64_new_static(Network *network, const char *filename, unsigned section_line, Prefix64 **ret) {
192 _cleanup_(config_section_freep) ConfigSection *n = NULL;
193 _cleanup_(prefix64_freep) Prefix64 *prefix = NULL;
194 int r;
195
196 assert(network);
197 assert(ret);
198 assert(filename);
199 assert(section_line > 0);
200
201 r = config_section_new(filename, section_line, &n);
202 if (r < 0)
203 return r;
204
205 prefix = hashmap_get(network->pref64_prefixes_by_section, n);
206 if (prefix) {
207 *ret = TAKE_PTR(prefix);
208 return 0;
209 }
210
211 prefix = new(Prefix64, 1);
212 if (!prefix)
213 return -ENOMEM;
214
215 *prefix = (Prefix64) {
216 .network = network,
217 .section = TAKE_PTR(n),
218
219 .prefix64.lifetime = RADV_PREF64_DEFAULT_LIFETIME_USEC,
220 .prefix64.valid_until = USEC_INFINITY,
221 };
222
223 r = hashmap_ensure_put(&network->pref64_prefixes_by_section, &prefix64_hash_ops_by_section, prefix->section, prefix);
224 if (r < 0)
225 return r;
226
227 *ret = TAKE_PTR(prefix);
228 return 0;
229 }
230
231 int link_request_radv_addresses(Link *link) {
232 Prefix *p;
233 int r;
234
235 assert(link);
236
237 if (!link_radv_enabled(link))
238 return 0;
239
240 HASHMAP_FOREACH(p, link->network->prefixes_by_section) {
241 if (!p->assign)
242 continue;
243
244 /* radv_generate_addresses() below requires the prefix length <= 64. */
245 if (p->prefix.prefixlen > 64)
246 continue;
247
248 _cleanup_hashmap_free_ Hashmap *tokens_by_address = NULL;
249 r = radv_generate_addresses(link, p->tokens, &p->prefix.address, p->prefix.prefixlen, &tokens_by_address);
250 if (r < 0)
251 return r;
252
253 IPv6Token *token;
254 struct in6_addr *a;
255 HASHMAP_FOREACH_KEY(token, a, tokens_by_address) {
256 _cleanup_(address_unrefp) Address *address = NULL;
257
258 r = address_new(&address);
259 if (r < 0)
260 return -ENOMEM;
261
262 address->source = NETWORK_CONFIG_SOURCE_STATIC;
263 address->family = AF_INET6;
264 address->in_addr.in6 = *a;
265 address->prefixlen = p->prefix.prefixlen;
266 address->route_metric = p->route_metric;
267 address->token = ipv6_token_ref(token);
268
269 r = link_request_static_address(link, address);
270 if (r < 0)
271 return r;
272 }
273 }
274
275 return 0;
276 }
277
278 int link_reconfigure_radv_address(Address *address, Link *link) {
279 int r;
280
281 assert(address);
282 assert(address->source == NETWORK_CONFIG_SOURCE_STATIC);
283 assert(link);
284
285 r = regenerate_address(address, link);
286 if (r <= 0)
287 return r;
288
289 r = link_request_static_address(link, address);
290 if (r < 0)
291 return r;
292
293 if (link->static_address_messages != 0) {
294 link->static_addresses_configured = false;
295 link_set_state(link, LINK_STATE_CONFIGURING);
296 }
297
298 return 0;
299 }
300
301 static int network_get_ipv6_dns(Network *network, struct in6_addr **ret_addresses, size_t *ret_size) {
302 _cleanup_free_ struct in6_addr *addresses = NULL;
303 size_t n_addresses = 0;
304
305 assert(network);
306 assert(ret_addresses);
307 assert(ret_size);
308
309 for (size_t i = 0; i < network->n_dns; i++) {
310 union in_addr_union *addr;
311
312 if (network->dns[i]->family != AF_INET6)
313 continue;
314
315 addr = &network->dns[i]->address;
316
317 if (in_addr_is_null(AF_INET6, addr) ||
318 in_addr_is_link_local(AF_INET6, addr) ||
319 in_addr_is_localhost(AF_INET6, addr))
320 continue;
321
322 if (!GREEDY_REALLOC(addresses, n_addresses + 1))
323 return -ENOMEM;
324
325 addresses[n_addresses++] = addr->in6;
326 }
327
328 *ret_addresses = TAKE_PTR(addresses);
329 *ret_size = n_addresses;
330
331 return n_addresses;
332 }
333
334 static int radv_set_dns(Link *link, Link *uplink) {
335 _cleanup_free_ struct in6_addr *dns = NULL;
336 size_t n_dns;
337 int r;
338
339 if (!link->network->router_emit_dns)
340 return 0;
341
342 if (link->network->router_dns) {
343 struct in6_addr *p;
344
345 dns = new(struct in6_addr, link->network->n_router_dns);
346 if (!dns)
347 return -ENOMEM;
348
349 p = dns;
350 for (size_t i = 0; i < link->network->n_router_dns; i++)
351 if (in6_addr_is_null(&link->network->router_dns[i])) {
352 if (in6_addr_is_set(&link->ipv6ll_address))
353 *(p++) = link->ipv6ll_address;
354 } else
355 *(p++) = link->network->router_dns[i];
356
357 n_dns = p - dns;
358
359 goto set_dns;
360 }
361
362 r = network_get_ipv6_dns(link->network, &dns, &n_dns);
363 if (r > 0)
364 goto set_dns;
365
366 if (uplink) {
367 assert(uplink->network);
368
369 r = network_get_ipv6_dns(uplink->network, &dns, &n_dns);
370 if (r > 0)
371 goto set_dns;
372 }
373
374 return 0;
375
376 set_dns:
377 return sd_radv_add_rdnss(
378 link->radv,
379 n_dns,
380 dns,
381 link->network->router_dns_lifetime_usec,
382 /* valid_until= */ USEC_INFINITY);
383 }
384
385 static int radv_set_domains(Link *link, Link *uplink) {
386 _cleanup_free_ char **s = NULL; /* just free() because the strings are owned by the set */
387 OrderedSet *search_domains;
388
389 if (!link->network->router_emit_domains)
390 return 0;
391
392 search_domains = link->network->router_search_domains;
393
394 if (search_domains)
395 goto set_domains;
396
397 search_domains = link->network->search_domains;
398 if (search_domains)
399 goto set_domains;
400
401 if (uplink) {
402 assert(uplink->network);
403
404 search_domains = uplink->network->search_domains;
405 if (search_domains)
406 goto set_domains;
407 }
408
409 return 0;
410
411 set_domains:
412 s = ordered_set_get_strv(search_domains);
413 if (!s)
414 return log_oom();
415
416 return sd_radv_add_dnssl(
417 link->radv,
418 s,
419 link->network->router_dns_lifetime_usec,
420 /* valid_until= */ USEC_INFINITY);
421 }
422
423 static int radv_find_uplink(Link *link, Link **ret) {
424 int r;
425
426 assert(link);
427 assert(ret);
428
429 if (link->network->router_uplink_name)
430 return link_get_by_name(link->manager, link->network->router_uplink_name, ret);
431
432 if (link->network->router_uplink_index > 0)
433 return link_get_by_index(link->manager, link->network->router_uplink_index, ret);
434
435 if (link->network->router_uplink_index == UPLINK_INDEX_AUTO) {
436 if (link_dhcp_pd_is_enabled(link))
437 r = dhcp_pd_find_uplink(link, ret); /* When DHCP-PD is enabled, use its uplink. */
438 else
439 r = manager_find_uplink(link->manager, AF_INET6, link, ret);
440 if (r < 0)
441 /* It is not necessary to propagate error in automatic selection. */
442 *ret = NULL;
443 return 0;
444 }
445
446 *ret = NULL;
447 return 0;
448 }
449
450 static int radv_configure(Link *link) {
451 Link *uplink = NULL;
452 int r;
453
454 assert(link);
455 assert(link->network);
456
457 if (link->radv)
458 return -EBUSY;
459
460 r = sd_radv_new(&link->radv);
461 if (r < 0)
462 return r;
463
464 r = sd_radv_attach_event(link->radv, link->manager->event, 0);
465 if (r < 0)
466 return r;
467
468 if (link->hw_addr.length == ETH_ALEN) {
469 r = sd_radv_set_mac(link->radv, &link->hw_addr.ether);
470 if (r < 0)
471 return r;
472 }
473
474 r = sd_radv_set_ifindex(link->radv, link->ifindex);
475 if (r < 0)
476 return r;
477
478 r = sd_radv_set_managed_information(link->radv, link->network->router_managed);
479 if (r < 0)
480 return r;
481
482 r = sd_radv_set_other_information(link->radv, link->network->router_other_information);
483 if (r < 0)
484 return r;
485
486 r = sd_radv_set_router_lifetime(link->radv, link->network->router_lifetime_usec);
487 if (r < 0)
488 return r;
489
490 r = sd_radv_set_hop_limit(link->radv, link->network->router_hop_limit);
491 if (r < 0)
492 return r;
493
494 r = sd_radv_set_preference(link->radv, link->network->router_preference);
495 if (r < 0)
496 return r;
497
498 r = sd_radv_set_reachable_time(link->radv, link->network->router_reachable_usec);
499 if (r < 0)
500 return r;
501
502 r = sd_radv_set_retransmit(link->radv, link->network->router_retransmit_usec);
503 if (r < 0)
504 return r;
505
506 Prefix *p;
507 HASHMAP_FOREACH(p, link->network->prefixes_by_section) {
508 r = sd_radv_add_prefix(
509 link->radv,
510 &p->prefix.address,
511 p->prefix.prefixlen,
512 p->prefix.flags,
513 p->prefix.valid_lifetime,
514 p->prefix.preferred_lifetime,
515 p->prefix.valid_until,
516 p->prefix.preferred_until);
517 if (r < 0 && r != -EEXIST)
518 return r;
519 }
520
521 RoutePrefix *q;
522 HASHMAP_FOREACH(q, link->network->route_prefixes_by_section) {
523 r = sd_radv_add_route(
524 link->radv,
525 &q->route.address,
526 q->route.prefixlen,
527 q->route.preference,
528 q->route.lifetime,
529 q->route.valid_until);
530 if (r < 0 && r != -EEXIST)
531 return r;
532 }
533
534 Prefix64 *n;
535 HASHMAP_FOREACH(n, link->network->pref64_prefixes_by_section) {
536 r = sd_radv_add_prefix64(
537 link->radv,
538 &n->prefix64.prefix,
539 n->prefix64.prefixlen,
540 n->prefix64.lifetime,
541 n->prefix64.valid_until);
542 if (r < 0 && r != -EEXIST)
543 return r;
544 }
545
546 (void) radv_find_uplink(link, &uplink);
547
548 r = radv_set_dns(link, uplink);
549 if (r < 0)
550 return log_link_debug_errno(link, r, "Could not set RA DNS: %m");
551
552 r = radv_set_domains(link, uplink);
553 if (r < 0)
554 return log_link_debug_errno(link, r, "Could not set RA Domains: %m");
555
556 if (link->network->router_home_agent_information) {
557 r = sd_radv_set_home_agent(
558 link->radv,
559 link->network->router_home_agent_preference,
560 link->network->home_agent_lifetime_usec,
561 /* valid_until= */ USEC_INFINITY);
562 if (r < 0)
563 return r;
564 }
565
566 return 0;
567 }
568
569 int radv_update_mac(Link *link) {
570 assert(link);
571
572 if (!link->radv)
573 return 0;
574
575 if (link->hw_addr.length != ETH_ALEN)
576 return 0;
577
578 return sd_radv_set_mac(link->radv, &link->hw_addr.ether);
579 }
580
581 static int radv_is_ready_to_configure(Link *link) {
582 bool needs_uplink = false;
583 int r;
584
585 assert(link);
586 assert(link->network);
587
588 if (!link_is_ready_to_configure(link, /* allow_unmanaged= */ false))
589 return false;
590
591 if (in6_addr_is_null(&link->ipv6ll_address))
592 return false;
593
594 if (link->hw_addr.length != ETH_ALEN || hw_addr_is_null(&link->hw_addr))
595 return false;
596
597 if (link->network->router_emit_dns && !link->network->router_dns) {
598 _cleanup_free_ struct in6_addr *dns = NULL;
599 size_t n_dns;
600
601 r = network_get_ipv6_dns(link->network, &dns, &n_dns);
602 if (r < 0)
603 return r;
604
605 needs_uplink = r == 0;
606 }
607
608 if (link->network->router_emit_domains &&
609 !link->network->router_search_domains &&
610 !link->network->search_domains)
611 needs_uplink = true;
612
613 if (needs_uplink) {
614 Link *uplink = NULL;
615
616 if (radv_find_uplink(link, &uplink) < 0)
617 return false;
618
619 if (uplink && !uplink->network)
620 return false;
621 }
622
623 return true;
624 }
625
626 static int radv_process_request(Request *req, Link *link, void *userdata) {
627 int r;
628
629 assert(link);
630
631 r = radv_is_ready_to_configure(link);
632 if (r <= 0)
633 return r;
634
635 r = radv_configure(link);
636 if (r < 0)
637 return log_link_warning_errno(link, r, "Failed to configure IPv6 Router Advertisement engine: %m");
638
639 if (link_has_carrier(link)) {
640 r = radv_start(link);
641 if (r < 0)
642 return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
643 }
644
645 log_link_debug(link, "IPv6 Router Advertisement engine is configured%s.",
646 link_has_carrier(link) ? " and started" : "");
647 return 1;
648 }
649
650 int link_request_radv(Link *link) {
651 int r;
652
653 assert(link);
654
655 if (!link_radv_enabled(link))
656 return 0;
657
658 if (link->radv)
659 return 0;
660
661 r = link_queue_request(link, REQUEST_TYPE_RADV, radv_process_request, NULL);
662 if (r < 0)
663 return log_link_warning_errno(link, r, "Failed to request configuring of the IPv6 Router Advertisement engine: %m");
664
665 log_link_debug(link, "Requested configuring of the IPv6 Router Advertisement engine.");
666 return 0;
667 }
668
669 int link_drop_radv_config(Link *link, Network *network) {
670 int ret = 0;
671
672 assert(link);
673 assert(link->network);
674
675 if (link->network == network)
676 return 0; /* .network file is unchanged. It is not necessary to reconfigure the server. */
677
678 // FIXME: check detailed settings and do not stop if nothing changed.
679 // FIXME: save dynamic prefixes acquired by DHCP-PD.
680 ret = sd_radv_stop(link->radv);
681 link->radv = sd_radv_unref(link->radv);
682 return ret;
683 }
684
685 int radv_start(Link *link) {
686 int r;
687
688 assert(link);
689 assert(link->network);
690
691 if (!link->radv)
692 return 0;
693
694 if (!link_has_carrier(link))
695 return 0;
696
697 if (in6_addr_is_null(&link->ipv6ll_address))
698 return 0;
699
700 /* Update the source IPv6LL before the running check so replacement IPv6LL handover can
701 * rebind RADV without requiring stop/start. */
702 r = sd_radv_set_link_local_address(link->radv, &link->ipv6ll_address);
703 if (r < 0)
704 return r;
705
706 if (sd_radv_is_running(link->radv))
707 return 0;
708
709 if (link->network->dhcp_pd_announce) {
710 r = dhcp_request_prefix_delegation(link);
711 if (r < 0)
712 return log_link_debug_errno(link, r, "Failed to request DHCP delegated subnet prefix: %m");
713 }
714
715 log_link_debug(link, "Starting IPv6 Router Advertisements");
716 return sd_radv_start(link->radv);
717 }
718
719 int radv_add_prefix(
720 Link *link,
721 const struct in6_addr *prefix,
722 uint8_t prefix_len,
723 usec_t lifetime_preferred_usec,
724 usec_t lifetime_valid_usec) {
725
726 int r;
727
728 assert(link);
729 assert(prefix);
730
731 if (!link->radv)
732 return 0;
733
734 r = sd_radv_add_prefix(
735 link->radv,
736 prefix,
737 prefix_len,
738 ND_OPT_PI_FLAG_ONLINK | ND_OPT_PI_FLAG_AUTO,
739 RADV_DEFAULT_VALID_LIFETIME_USEC,
740 RADV_DEFAULT_PREFERRED_LIFETIME_USEC,
741 lifetime_valid_usec,
742 lifetime_preferred_usec);
743 if (r == -EEXIST)
744 return 0;
745 if (r < 0)
746 return r;
747
748 if (sd_radv_is_running(link->radv)) {
749 /* Announce updated prefix now. */
750 r = sd_radv_send(link->radv);
751 if (r < 0)
752 return r;
753 }
754
755 return 0;
756 }
757
758 static int prefix_section_verify(Prefix *p) {
759 assert(p);
760
761 if (section_is_invalid(p->section))
762 return -EINVAL;
763
764 if (in6_addr_is_null(&p->prefix.address))
765 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
766 "%s: [IPv6Prefix] section without Prefix= field configured, "
767 "or specified prefix is the null address. "
768 "Ignoring [IPv6Prefix] section from line %u.",
769 p->section->filename, p->section->line);
770
771 if (p->prefix.prefixlen < 3 || p->prefix.prefixlen > 128)
772 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
773 "%s: Invalid prefix length %u is specified in [IPv6Prefix] section. "
774 "Valid range is 3…128. Ignoring [IPv6Prefix] section from line %u.",
775 p->section->filename, p->prefix.prefixlen, p->section->line);
776
777 if (p->prefix.prefixlen > 64) {
778 log_info("%s:%u: Unusual prefix length %u (> 64) is specified in [IPv6Prefix] section from line %s%s.",
779 p->section->filename, p->section->line,
780 p->prefix.prefixlen,
781 p->assign ? ", refusing to assign an address in " : "",
782 p->assign ? IN6_ADDR_PREFIX_TO_STRING(&p->prefix.address, p->prefix.prefixlen) : "");
783
784 p->assign = false;
785 }
786
787 if (p->prefix.preferred_lifetime > p->prefix.valid_lifetime)
788 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
789 "%s: The preferred lifetime %s is longer than the valid lifetime %s. "
790 "Ignoring [IPv6Prefix] section from line %u.",
791 p->section->filename,
792 FORMAT_TIMESPAN(p->prefix.preferred_lifetime, USEC_PER_SEC),
793 FORMAT_TIMESPAN(p->prefix.valid_lifetime, USEC_PER_SEC),
794 p->section->line);
795
796 return 0;
797 }
798
799 static int route_prefix_section_verify(RoutePrefix *p) {
800 if (section_is_invalid(p->section))
801 return -EINVAL;
802
803 if (p->route.prefixlen > 128)
804 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
805 "%s: Invalid prefix length %u is specified in [IPv6RoutePrefix] section. "
806 "Valid range is 0…128. Ignoring [IPv6RoutePrefix] section from line %u.",
807 p->section->filename, p->route.prefixlen, p->section->line);
808
809 return 0;
810 }
811
812 void network_adjust_radv(Network *network) {
813 assert(network);
814
815 /* After this function is called, network->router_prefix_delegation can be treated as a boolean. */
816
817 if (network->dhcp_pd < 0)
818 /* For backward compatibility. */
819 network->dhcp_pd = FLAGS_SET(network->router_prefix_delegation, RADV_PREFIX_DELEGATION_DHCP6);
820
821 if (!FLAGS_SET(network->link_local, ADDRESS_FAMILY_IPV6) &&
822 !network_has_static_ipv6ll_address(network)) {
823 if (network->router_prefix_delegation != RADV_PREFIX_DELEGATION_NONE)
824 log_warning("%s: IPv6PrefixDelegation= is enabled but IPv6 link-local addressing is disabled. "
825 "Disabling IPv6PrefixDelegation=.", network->filename);
826
827 network->router_prefix_delegation = RADV_PREFIX_DELEGATION_NONE;
828 }
829
830 if (network->router_prefix_delegation == RADV_PREFIX_DELEGATION_NONE) {
831 network->n_router_dns = 0;
832 network->router_dns = mfree(network->router_dns);
833 network->router_search_domains = ordered_set_free(network->router_search_domains);
834 }
835
836 if (!FLAGS_SET(network->router_prefix_delegation, RADV_PREFIX_DELEGATION_STATIC)) {
837 network->prefixes_by_section = hashmap_free(network->prefixes_by_section);
838 network->route_prefixes_by_section = hashmap_free(network->route_prefixes_by_section);
839 network->pref64_prefixes_by_section = hashmap_free(network->pref64_prefixes_by_section);
840 }
841
842 if (!network->router_prefix_delegation)
843 return;
844
845 /* Below, let's verify router settings, if enabled. */
846
847 if (network->router_lifetime_usec == 0 && network->router_preference != SD_NDISC_PREFERENCE_MEDIUM)
848 /* RFC 4191, Section 2.2,
849 * If the Router Lifetime is zero, the preference value MUST be set to (00) by the sender.
850 *
851 * Note, radv_send_router() gracefully handle that. So, it is not necessary to refuse, but
852 * let's warn about that. */
853 log_notice("%s: RouterPreference=%s specified with RouterLifetimeSec=0, ignoring RouterPreference= setting.",
854 network->filename, ndisc_router_preference_to_string(network->router_preference));
855
856 Prefix *prefix;
857 HASHMAP_FOREACH(prefix, network->prefixes_by_section)
858 if (prefix_section_verify(prefix) < 0)
859 prefix_free(prefix);
860
861 RoutePrefix *route;
862 HASHMAP_FOREACH(route, network->route_prefixes_by_section)
863 if (route_prefix_section_verify(route) < 0)
864 route_prefix_free(route);
865
866 Prefix64 *pref64;
867 HASHMAP_FOREACH(pref64, network->pref64_prefixes_by_section)
868 if (section_is_invalid(pref64->section))
869 prefix64_free(pref64);
870 }
871
872 int config_parse_prefix(
873 const char *unit,
874 const char *filename,
875 unsigned line,
876 const char *section,
877 unsigned section_line,
878 const char *lvalue,
879 int ltype,
880 const char *rvalue,
881 void *data,
882 void *userdata) {
883
884 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
885 Network *network = ASSERT_PTR(userdata);
886 union in_addr_union a;
887 int r;
888
889 assert(filename);
890 assert(section);
891 assert(lvalue);
892 assert(rvalue);
893
894 r = prefix_new_static(network, filename, section_line, &p);
895 if (r < 0)
896 return log_oom();
897
898 r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->prefix.prefixlen);
899 if (r < 0) {
900 log_syntax(unit, LOG_WARNING, filename, line, r,
901 "Prefix is invalid, ignoring assignment: %s", rvalue);
902 return 0;
903 }
904
905 (void) in6_addr_mask(&a.in6, p->prefix.prefixlen);
906 p->prefix.address = a.in6;
907
908 TAKE_PTR(p);
909 return 0;
910 }
911
912 int config_parse_prefix_boolean(
913 const char *unit,
914 const char *filename,
915 unsigned line,
916 const char *section,
917 unsigned section_line,
918 const char *lvalue,
919 int ltype,
920 const char *rvalue,
921 void *data,
922 void *userdata) {
923
924 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
925 Network *network = ASSERT_PTR(userdata);
926 int r;
927
928 assert(filename);
929 assert(section);
930 assert(lvalue);
931 assert(rvalue);
932
933 r = prefix_new_static(network, filename, section_line, &p);
934 if (r < 0)
935 return log_oom();
936
937 r = parse_boolean(rvalue);
938 if (r < 0) {
939 log_syntax(unit, LOG_WARNING, filename, line, r,
940 "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
941 return 0;
942 }
943
944 if (ltype != 0)
945 SET_FLAG(p->prefix.flags, ltype, r);
946 else {
947 assert(streq(lvalue, "Assign"));
948 p->assign = r;
949 }
950
951 TAKE_PTR(p);
952 return 0;
953 }
954
955 int config_parse_prefix_lifetime(
956 const char *unit,
957 const char *filename,
958 unsigned line,
959 const char *section,
960 unsigned section_line,
961 const char *lvalue,
962 int ltype,
963 const char *rvalue,
964 void *data,
965 void *userdata) {
966
967 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
968 Network *network = ASSERT_PTR(userdata);
969 usec_t usec;
970 int r;
971
972 assert(filename);
973 assert(section);
974 assert(lvalue);
975 assert(rvalue);
976
977 r = prefix_new_static(network, filename, section_line, &p);
978 if (r < 0)
979 return log_oom();
980
981 r = parse_sec(rvalue, &usec);
982 if (r < 0) {
983 log_syntax(unit, LOG_WARNING, filename, line, r,
984 "Lifetime is invalid, ignoring assignment: %s", rvalue);
985 return 0;
986 }
987
988 if (usec != USEC_INFINITY && DIV_ROUND_UP(usec, USEC_PER_SEC) >= UINT32_MAX) {
989 log_syntax(unit, LOG_WARNING, filename, line, 0,
990 "Lifetime is too long, ignoring assignment: %s", rvalue);
991 return 0;
992 }
993
994 if (streq(lvalue, "PreferredLifetimeSec"))
995 p->prefix.preferred_lifetime = usec;
996 else if (streq(lvalue, "ValidLifetimeSec"))
997 p->prefix.valid_lifetime = usec;
998 else
999 assert_not_reached();
1000
1001 TAKE_PTR(p);
1002 return 0;
1003 }
1004
1005 int config_parse_prefix_metric(
1006 const char *unit,
1007 const char *filename,
1008 unsigned line,
1009 const char *section,
1010 unsigned section_line,
1011 const char *lvalue,
1012 int ltype,
1013 const char *rvalue,
1014 void *data,
1015 void *userdata) {
1016
1017 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
1018 Network *network = ASSERT_PTR(userdata);
1019 int r;
1020
1021 assert(filename);
1022 assert(section);
1023 assert(lvalue);
1024 assert(rvalue);
1025
1026 r = prefix_new_static(network, filename, section_line, &p);
1027 if (r < 0)
1028 return log_oom();
1029
1030 r = safe_atou32(rvalue, &p->route_metric);
1031 if (r < 0) {
1032 log_syntax(unit, LOG_WARNING, filename, line, r,
1033 "Failed to parse %s=, ignoring assignment: %s",
1034 lvalue, rvalue);
1035 return 0;
1036 }
1037
1038 TAKE_PTR(p);
1039 return 0;
1040 }
1041
1042 int config_parse_prefix_token(
1043 const char *unit,
1044 const char *filename,
1045 unsigned line,
1046 const char *section,
1047 unsigned section_line,
1048 const char *lvalue,
1049 int ltype,
1050 const char *rvalue,
1051 void *data,
1052 void *userdata) {
1053
1054 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
1055 Network *network = ASSERT_PTR(userdata);
1056 int r;
1057
1058 assert(filename);
1059 assert(section);
1060 assert(lvalue);
1061 assert(rvalue);
1062
1063 r = prefix_new_static(network, filename, section_line, &p);
1064 if (r < 0)
1065 return log_oom();
1066
1067 r = config_parse_address_generation_type(unit, filename, line, section, section_line,
1068 lvalue, ltype, rvalue, &p->tokens, userdata);
1069 if (r < 0)
1070 return r;
1071
1072 TAKE_PTR(p);
1073 return 0;
1074 }
1075
1076 int config_parse_route_prefix(
1077 const char *unit,
1078 const char *filename,
1079 unsigned line,
1080 const char *section,
1081 unsigned section_line,
1082 const char *lvalue,
1083 int ltype,
1084 const char *rvalue,
1085 void *data,
1086 void *userdata) {
1087
1088 _cleanup_(route_prefix_free_or_set_invalidp) RoutePrefix *p = NULL;
1089 Network *network = ASSERT_PTR(userdata);
1090 union in_addr_union a;
1091 int r;
1092
1093 assert(filename);
1094 assert(section);
1095 assert(lvalue);
1096 assert(rvalue);
1097
1098 r = route_prefix_new_static(network, filename, section_line, &p);
1099 if (r < 0)
1100 return log_oom();
1101
1102 r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->route.prefixlen);
1103 if (r < 0) {
1104 log_syntax(unit, LOG_WARNING, filename, line, r,
1105 "Route prefix is invalid, ignoring assignment: %s", rvalue);
1106 return 0;
1107 }
1108
1109 (void) in6_addr_mask(&a.in6, p->route.prefixlen);
1110 p->route.address = a.in6;
1111
1112 TAKE_PTR(p);
1113 return 0;
1114 }
1115
1116 int config_parse_route_prefix_lifetime(
1117 const char *unit,
1118 const char *filename,
1119 unsigned line,
1120 const char *section,
1121 unsigned section_line,
1122 const char *lvalue,
1123 int ltype,
1124 const char *rvalue,
1125 void *data,
1126 void *userdata) {
1127
1128 _cleanup_(route_prefix_free_or_set_invalidp) RoutePrefix *p = NULL;
1129 Network *network = ASSERT_PTR(userdata);
1130 usec_t usec;
1131 int r;
1132
1133 assert(filename);
1134 assert(section);
1135 assert(lvalue);
1136 assert(rvalue);
1137
1138 r = route_prefix_new_static(network, filename, section_line, &p);
1139 if (r < 0)
1140 return log_oom();
1141
1142 r = parse_sec(rvalue, &usec);
1143 if (r < 0) {
1144 log_syntax(unit, LOG_WARNING, filename, line, r,
1145 "Route lifetime is invalid, ignoring assignment: %s", rvalue);
1146 return 0;
1147 }
1148
1149 if (usec != USEC_INFINITY && DIV_ROUND_UP(usec, USEC_PER_SEC) >= UINT32_MAX) {
1150 log_syntax(unit, LOG_WARNING, filename, line, 0,
1151 "Lifetime is too long, ignoring assignment: %s", rvalue);
1152 return 0;
1153 }
1154
1155 p->route.lifetime = usec;
1156
1157 TAKE_PTR(p);
1158 return 0;
1159 }
1160
1161 int config_parse_route_prefix_preference(
1162 const char *unit,
1163 const char *filename,
1164 unsigned line,
1165 const char *section,
1166 unsigned section_line,
1167 const char *lvalue,
1168 int ltype,
1169 const char *rvalue,
1170 void *data,
1171 void *userdata) {
1172
1173 _cleanup_(route_prefix_free_or_set_invalidp) RoutePrefix *p = NULL;
1174 Network *network = ASSERT_PTR(userdata);
1175 int r;
1176
1177 assert(filename);
1178
1179 r = route_prefix_new_static(network, filename, section_line, &p);
1180 if (r < 0)
1181 return log_oom();
1182
1183 r = config_parse_router_preference(unit, filename, line, section, section_line,
1184 lvalue, ltype, rvalue, &p->route.preference, NULL);
1185 if (r <= 0)
1186 return r;
1187
1188 TAKE_PTR(p);
1189 return 0;
1190 }
1191
1192 int config_parse_pref64_prefix(
1193 const char *unit,
1194 const char *filename,
1195 unsigned line,
1196 const char *section,
1197 unsigned section_line,
1198 const char *lvalue,
1199 int ltype,
1200 const char *rvalue,
1201 void *data,
1202 void *userdata) {
1203
1204 _cleanup_(prefix64_free_or_set_invalidp) Prefix64 *p = NULL;
1205 Network *network = ASSERT_PTR(userdata);
1206 union in_addr_union a;
1207 uint8_t prefixlen;
1208 int r;
1209
1210 assert(filename);
1211 assert(section);
1212 assert(lvalue);
1213 assert(rvalue);
1214
1215 r = prefix64_new_static(network, filename, section_line, &p);
1216 if (r < 0)
1217 return log_oom();
1218
1219 r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &prefixlen);
1220 if (r < 0) {
1221 log_syntax(unit, LOG_WARNING, filename, line, r,
1222 "PREF64 prefix is invalid, ignoring assignment: %s", rvalue);
1223 return 0;
1224 }
1225
1226 if (!IN_SET(prefixlen, 96, 64, 56, 48, 40, 32)) {
1227 log_syntax(unit, LOG_WARNING, filename, line, 0,
1228 "PREF64 prefixlen is invalid, ignoring assignment: %s", rvalue);
1229 return 0;
1230 }
1231
1232 (void) in6_addr_mask(&a.in6, prefixlen);
1233 p->prefix64.prefix = a.in6;
1234 p->prefix64.prefixlen = prefixlen;
1235
1236 TAKE_PTR(p);
1237 return 0;
1238 }
1239
1240 int config_parse_pref64_prefix_lifetime(
1241 const char *unit,
1242 const char *filename,
1243 unsigned line,
1244 const char *section,
1245 unsigned section_line,
1246 const char *lvalue,
1247 int ltype,
1248 const char *rvalue,
1249 void *data,
1250 void *userdata) {
1251
1252 _cleanup_(prefix64_free_or_set_invalidp) Prefix64 *p = NULL;
1253 Network *network = ASSERT_PTR(userdata);
1254 usec_t usec;
1255 int r;
1256
1257 assert(filename);
1258 assert(section);
1259 assert(lvalue);
1260 assert(rvalue);
1261
1262 r = prefix64_new_static(network, filename, section_line, &p);
1263 if (r < 0)
1264 return log_oom();
1265
1266 r = parse_sec(rvalue, &usec);
1267 if (r < 0) {
1268 log_syntax(unit, LOG_WARNING, filename, line, r,
1269 "PREF64 lifetime is invalid, ignoring assignment: %s", rvalue);
1270 return 0;
1271 }
1272
1273 if (usec == USEC_INFINITY || DIV_ROUND_UP(usec, 8 * USEC_PER_SEC) >= UINT64_C(1) << 13) {
1274 log_syntax(unit, LOG_WARNING, filename, line, 0,
1275 "PREF64 lifetime is too long, ignoring assignment: %s", rvalue);
1276 return 0;
1277 }
1278
1279 p->prefix64.lifetime = usec;
1280
1281 TAKE_PTR(p);
1282 return 0;
1283 }
1284
1285 int config_parse_radv_dns(
1286 const char *unit,
1287 const char *filename,
1288 unsigned line,
1289 const char *section,
1290 unsigned section_line,
1291 const char *lvalue,
1292 int ltype,
1293 const char *rvalue,
1294 void *data,
1295 void *userdata) {
1296
1297 Network *n = data;
1298 int r;
1299
1300 assert(filename);
1301 assert(lvalue);
1302 assert(rvalue);
1303
1304 if (isempty(rvalue)) {
1305 n->n_router_dns = 0;
1306 n->router_dns = mfree(n->router_dns);
1307 return 0;
1308 }
1309
1310 for (const char *p = rvalue;;) {
1311 _cleanup_free_ char *w = NULL;
1312 union in_addr_union a;
1313
1314 r = extract_first_word(&p, &w, NULL, 0);
1315 if (r == -ENOMEM)
1316 return log_oom();
1317 if (r < 0) {
1318 log_syntax(unit, LOG_WARNING, filename, line, r,
1319 "Failed to extract word, ignoring: %s", rvalue);
1320 return 0;
1321 }
1322 if (r == 0)
1323 return 0;
1324
1325 if (streq(w, "_link_local"))
1326 a = IN_ADDR_NULL;
1327 else {
1328 r = in_addr_from_string(AF_INET6, w, &a);
1329 if (r < 0) {
1330 log_syntax(unit, LOG_WARNING, filename, line, r,
1331 "Failed to parse DNS server address, ignoring: %s", w);
1332 continue;
1333 }
1334
1335 if (in_addr_is_null(AF_INET6, &a)) {
1336 log_syntax(unit, LOG_WARNING, filename, line, 0,
1337 "DNS server address is null, ignoring: %s", w);
1338 continue;
1339 }
1340 }
1341
1342 if (!GREEDY_REALLOC(n->router_dns, n->n_router_dns + 1))
1343 return log_oom();
1344
1345 n->router_dns[n->n_router_dns++] = a.in6;
1346 }
1347 }
1348
1349 int config_parse_radv_search_domains(
1350 const char *unit,
1351 const char *filename,
1352 unsigned line,
1353 const char *section,
1354 unsigned section_line,
1355 const char *lvalue,
1356 int ltype,
1357 const char *rvalue,
1358 void *data,
1359 void *userdata) {
1360
1361 Network *n = data;
1362 int r;
1363
1364 assert(filename);
1365 assert(lvalue);
1366 assert(rvalue);
1367
1368 if (isempty(rvalue)) {
1369 n->router_search_domains = ordered_set_free(n->router_search_domains);
1370 return 0;
1371 }
1372
1373 for (const char *p = rvalue;;) {
1374 _cleanup_free_ char *w = NULL, *idna = NULL;
1375
1376 r = extract_first_word(&p, &w, NULL, 0);
1377 if (r == -ENOMEM)
1378 return log_oom();
1379 if (r < 0) {
1380 log_syntax(unit, LOG_WARNING, filename, line, r,
1381 "Failed to extract word, ignoring: %s", rvalue);
1382 return 0;
1383 }
1384 if (r == 0)
1385 return 0;
1386
1387 r = dns_name_apply_idna(w, &idna);
1388 if (r < 0) {
1389 log_syntax(unit, LOG_WARNING, filename, line, r,
1390 "Failed to apply IDNA to domain name '%s', ignoring: %m", w);
1391 continue;
1392 } else if (r == 0)
1393 /* transfer ownership to simplify subsequent operations */
1394 idna = TAKE_PTR(w);
1395
1396 r = ordered_set_ensure_allocated(&n->router_search_domains, &string_hash_ops_free);
1397 if (r < 0)
1398 return log_oom();
1399
1400 r = ordered_set_consume(n->router_search_domains, TAKE_PTR(idna));
1401 if (r < 0)
1402 return log_oom();
1403 }
1404 }
1405
1406 static const char * const radv_prefix_delegation_table[_RADV_PREFIX_DELEGATION_MAX] = {
1407 [RADV_PREFIX_DELEGATION_NONE] = "no",
1408 [RADV_PREFIX_DELEGATION_STATIC] = "static",
1409 [RADV_PREFIX_DELEGATION_DHCP6] = "dhcpv6",
1410 [RADV_PREFIX_DELEGATION_BOTH] = "yes",
1411 };
1412
1413 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
1414 radv_prefix_delegation,
1415 RADVPrefixDelegation,
1416 RADV_PREFIX_DELEGATION_BOTH);
1417
1418 int config_parse_router_prefix_delegation(
1419 const char *unit,
1420 const char *filename,
1421 unsigned line,
1422 const char *section,
1423 unsigned section_line,
1424 const char *lvalue,
1425 int ltype,
1426 const char *rvalue,
1427 void *data,
1428 void *userdata) {
1429
1430 RADVPrefixDelegation val, *ra = ASSERT_PTR(data);
1431 int r;
1432
1433 assert(filename);
1434 assert(lvalue);
1435 assert(rvalue);
1436
1437 if (streq(lvalue, "IPv6SendRA")) {
1438 r = parse_boolean(rvalue);
1439 if (r < 0) {
1440 log_syntax(unit, LOG_WARNING, filename, line, r,
1441 "Invalid %s= setting, ignoring assignment: %s", lvalue, rvalue);
1442 return 0;
1443 }
1444
1445 /* When IPv6SendRA= is enabled, only static prefixes are sent by default, and users
1446 * need to explicitly enable DHCPPrefixDelegation=. */
1447 *ra = r ? RADV_PREFIX_DELEGATION_STATIC : RADV_PREFIX_DELEGATION_NONE;
1448 return 0;
1449 }
1450
1451 /* For backward compatibility */
1452 val = radv_prefix_delegation_from_string(rvalue);
1453 if (val < 0) {
1454 log_syntax(unit, LOG_WARNING, filename, line, val,
1455 "Invalid %s= setting, ignoring assignment: %s", lvalue, rvalue);
1456 return 0;
1457 }
1458
1459 *ra = val;
1460 return 0;
1461 }
1462
1463 int config_parse_router_lifetime(
1464 const char *unit,
1465 const char *filename,
1466 unsigned line,
1467 const char *section,
1468 unsigned section_line,
1469 const char *lvalue,
1470 int ltype,
1471 const char *rvalue,
1472 void *data,
1473 void *userdata) {
1474
1475 usec_t usec, *lifetime = ASSERT_PTR(data);
1476 int r;
1477
1478 assert(filename);
1479 assert(section);
1480 assert(lvalue);
1481 assert(rvalue);
1482
1483 if (isempty(rvalue)) {
1484 *lifetime = RADV_DEFAULT_ROUTER_LIFETIME_USEC;
1485 return 0;
1486 }
1487
1488 r = parse_sec(rvalue, &usec);
1489 if (r < 0) {
1490 log_syntax(unit, LOG_WARNING, filename, line, r,
1491 "Failed to parse router lifetime, ignoring assignment: %s", rvalue);
1492 return 0;
1493 }
1494 if (usec > 0) {
1495 if (usec < RADV_MIN_ROUTER_LIFETIME_USEC) {
1496 log_syntax(unit, LOG_WARNING, filename, line, 0,
1497 "Router lifetime %s is too short, using %s.",
1498 FORMAT_TIMESPAN(usec, USEC_PER_SEC),
1499 FORMAT_TIMESPAN(RADV_MIN_ROUTER_LIFETIME_USEC, USEC_PER_SEC));
1500 usec = RADV_MIN_ROUTER_LIFETIME_USEC;
1501 } else if (usec > RADV_MAX_ROUTER_LIFETIME_USEC) {
1502 log_syntax(unit, LOG_WARNING, filename, line, 0,
1503 "Router lifetime %s is too large, using %s.",
1504 FORMAT_TIMESPAN(usec, USEC_PER_SEC),
1505 FORMAT_TIMESPAN(RADV_MAX_ROUTER_LIFETIME_USEC, USEC_PER_SEC));
1506 usec = RADV_MAX_ROUTER_LIFETIME_USEC;
1507 }
1508 }
1509
1510 *lifetime = usec;
1511 return 0;
1512 }
1513
1514 int config_parse_router_uint32_msec_usec(
1515 const char *unit,
1516 const char *filename,
1517 unsigned line,
1518 const char *section,
1519 unsigned section_line,
1520 const char *lvalue,
1521 int ltype,
1522 const char *rvalue,
1523 void *data,
1524 void *userdata) {
1525
1526 usec_t usec, *router_usec = ASSERT_PTR(data);
1527 int r;
1528
1529 assert(filename);
1530 assert(section);
1531 assert(lvalue);
1532 assert(rvalue);
1533
1534 if (isempty(rvalue)) {
1535 *router_usec = 0;
1536 return 0;
1537 }
1538
1539 r = parse_sec(rvalue, &usec);
1540 if (r < 0) {
1541 log_syntax(unit, LOG_WARNING, filename, line, r,
1542 "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
1543 return 0;
1544 }
1545
1546 if (usec != USEC_INFINITY &&
1547 usec > RADV_MAX_UINT32_MSEC_USEC) {
1548 log_syntax(unit, LOG_WARNING, filename, line, 0,
1549 "Invalid [%s] %s=, ignoring assignment: %s", section, lvalue, rvalue);
1550 return 0;
1551 }
1552
1553 *router_usec = usec;
1554 return 0;
1555 }
1556
1557 int config_parse_router_preference(
1558 const char *unit,
1559 const char *filename,
1560 unsigned line,
1561 const char *section,
1562 unsigned section_line,
1563 const char *lvalue,
1564 int ltype,
1565 const char *rvalue,
1566 void *data,
1567 void *userdata) {
1568
1569 uint8_t *preference = ASSERT_PTR(data);
1570
1571 if (isempty(rvalue) || STR_IN_SET(rvalue, "medium", "normal", "default"))
1572 *preference = SD_NDISC_PREFERENCE_MEDIUM;
1573 else if (streq(rvalue, "high"))
1574 *preference = SD_NDISC_PREFERENCE_HIGH;
1575 else if (streq(rvalue, "low"))
1576 *preference = SD_NDISC_PREFERENCE_LOW;
1577 else
1578 return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue);
1579
1580 return 1;
1581 }
1582
1583 int config_parse_router_home_agent_lifetime(
1584 const char *unit,
1585 const char *filename,
1586 unsigned line,
1587 const char *section,
1588 unsigned section_line,
1589 const char *lvalue,
1590 int ltype,
1591 const char *rvalue,
1592 void *data,
1593 void *userdata) {
1594
1595 usec_t usec, *home_agent_lifetime_usec = ASSERT_PTR(data);
1596 int r;
1597
1598 assert(filename);
1599 assert(section);
1600 assert(lvalue);
1601 assert(rvalue);
1602
1603 if (isempty(rvalue)) {
1604 *home_agent_lifetime_usec = 0;
1605 return 0;
1606 }
1607
1608 r = parse_sec(rvalue, &usec);
1609 if (r < 0) {
1610 log_syntax(unit, LOG_WARNING, filename, line, r,
1611 "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
1612 return 0;
1613 }
1614
1615 if (!timestamp_is_set(usec) ||
1616 usec > RADV_HOME_AGENT_MAX_LIFETIME_USEC) {
1617 log_syntax(unit, LOG_WARNING, filename, line, 0,
1618 "Invalid [%s] %s=, ignoring assignment: %s", section, lvalue, rvalue);
1619 return 0;
1620 }
1621
1622 *home_agent_lifetime_usec = usec;
1623 return 0;
1624 }