]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-radv.c
Merge pull request #17357 from yuwata/network-dhcp6-pd-announce-17353
[thirdparty/systemd.git] / src / network / networkd-radv.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2017 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <netinet/icmp6.h>
7 #include <arpa/inet.h>
8
9 #include "dns-domain.h"
10 #include "networkd-link.h"
11 #include "networkd-manager.h"
12 #include "networkd-network.h"
13 #include "networkd-radv.h"
14 #include "parse-util.h"
15 #include "string-util.h"
16 #include "string-table.h"
17 #include "strv.h"
18
19 Prefix *prefix_free(Prefix *prefix) {
20 if (!prefix)
21 return NULL;
22
23 if (prefix->network) {
24 assert(prefix->section);
25 hashmap_remove(prefix->network->prefixes_by_section, prefix->section);
26 }
27
28 network_config_section_free(prefix->section);
29 sd_radv_prefix_unref(prefix->radv_prefix);
30
31 return mfree(prefix);
32 }
33
34 DEFINE_NETWORK_SECTION_FUNCTIONS(Prefix, prefix_free);
35
36 static int prefix_new(Prefix **ret) {
37 _cleanup_(prefix_freep) Prefix *prefix = NULL;
38
39 prefix = new0(Prefix, 1);
40 if (!prefix)
41 return -ENOMEM;
42
43 if (sd_radv_prefix_new(&prefix->radv_prefix) < 0)
44 return -ENOMEM;
45
46 *ret = TAKE_PTR(prefix);
47
48 return 0;
49 }
50
51 static int prefix_new_static(Network *network, const char *filename, unsigned section_line, Prefix **ret) {
52 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
53 _cleanup_(prefix_freep) Prefix *prefix = NULL;
54 int r;
55
56 assert(network);
57 assert(ret);
58 assert(filename);
59 assert(section_line > 0);
60
61 r = network_config_section_new(filename, section_line, &n);
62 if (r < 0)
63 return r;
64
65 prefix = hashmap_get(network->prefixes_by_section, n);
66 if (prefix) {
67 *ret = TAKE_PTR(prefix);
68 return 0;
69 }
70
71 r = prefix_new(&prefix);
72 if (r < 0)
73 return r;
74
75 prefix->network = network;
76 prefix->section = TAKE_PTR(n);
77
78 r = hashmap_ensure_allocated(&network->prefixes_by_section, &network_config_hash_ops);
79 if (r < 0)
80 return r;
81
82 r = hashmap_put(network->prefixes_by_section, prefix->section, prefix);
83 if (r < 0)
84 return r;
85
86 *ret = TAKE_PTR(prefix);
87
88 return 0;
89 }
90
91 RoutePrefix *route_prefix_free(RoutePrefix *prefix) {
92 if (!prefix)
93 return NULL;
94
95 if (prefix->network) {
96 assert(prefix->section);
97 hashmap_remove(prefix->network->route_prefixes_by_section, prefix->section);
98 }
99
100 network_config_section_free(prefix->section);
101 sd_radv_route_prefix_unref(prefix->radv_route_prefix);
102
103 return mfree(prefix);
104 }
105
106 DEFINE_NETWORK_SECTION_FUNCTIONS(RoutePrefix, route_prefix_free);
107
108 static int route_prefix_new(RoutePrefix **ret) {
109 _cleanup_(route_prefix_freep) RoutePrefix *prefix = NULL;
110
111 prefix = new0(RoutePrefix, 1);
112 if (!prefix)
113 return -ENOMEM;
114
115 if (sd_radv_route_prefix_new(&prefix->radv_route_prefix) < 0)
116 return -ENOMEM;
117
118 *ret = TAKE_PTR(prefix);
119
120 return 0;
121 }
122
123 static int route_prefix_new_static(Network *network, const char *filename, unsigned section_line, RoutePrefix **ret) {
124 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
125 _cleanup_(route_prefix_freep) RoutePrefix *prefix = NULL;
126 int r;
127
128 assert(network);
129 assert(ret);
130 assert(filename);
131 assert(section_line > 0);
132
133 r = network_config_section_new(filename, section_line, &n);
134 if (r < 0)
135 return r;
136
137 prefix = hashmap_get(network->route_prefixes_by_section, n);
138 if (prefix) {
139 *ret = TAKE_PTR(prefix);
140 return 0;
141 }
142
143 r = route_prefix_new(&prefix);
144 if (r < 0)
145 return r;
146
147 prefix->network = network;
148 prefix->section = TAKE_PTR(n);
149
150 r = hashmap_ensure_allocated(&network->route_prefixes_by_section, &network_config_hash_ops);
151 if (r < 0)
152 return r;
153
154 r = hashmap_put(network->route_prefixes_by_section, prefix->section, prefix);
155 if (r < 0)
156 return r;
157
158 *ret = TAKE_PTR(prefix);
159
160 return 0;
161 }
162
163 void network_drop_invalid_prefixes(Network *network) {
164 Prefix *prefix;
165
166 assert(network);
167
168 HASHMAP_FOREACH(prefix, network->prefixes_by_section)
169 if (section_is_invalid(prefix->section))
170 prefix_free(prefix);
171 }
172
173 void network_drop_invalid_route_prefixes(Network *network) {
174 RoutePrefix *prefix;
175
176 assert(network);
177
178 HASHMAP_FOREACH(prefix, network->route_prefixes_by_section)
179 if (section_is_invalid(prefix->section))
180 route_prefix_free(prefix);
181 }
182
183 void network_adjust_radv(Network *network) {
184 assert(network);
185
186 /* After this function is called, network->router_prefix_delegation can be treated as a boolean. */
187
188 if (network->dhcp6_pd < 0)
189 /* For backward compatibility. */
190 network->dhcp6_pd = FLAGS_SET(network->router_prefix_delegation, RADV_PREFIX_DELEGATION_DHCP6);
191
192 if (!FLAGS_SET(network->link_local, ADDRESS_FAMILY_IPV6)) {
193 if (network->router_prefix_delegation != RADV_PREFIX_DELEGATION_NONE)
194 log_warning("%s: IPv6PrefixDelegation= is enabled but IPv6 link local addressing is disabled. "
195 "Disabling IPv6PrefixDelegation=.", network->filename);
196
197 network->router_prefix_delegation = RADV_PREFIX_DELEGATION_NONE;
198 }
199
200 if (network->router_prefix_delegation == RADV_PREFIX_DELEGATION_NONE) {
201 network->n_router_dns = 0;
202 network->router_dns = mfree(network->router_dns);
203 network->router_search_domains = ordered_set_free(network->router_search_domains);
204 }
205
206 if (!FLAGS_SET(network->router_prefix_delegation, RADV_PREFIX_DELEGATION_STATIC)) {
207 network->prefixes_by_section = hashmap_free_with_destructor(network->prefixes_by_section, prefix_free);
208 network->route_prefixes_by_section = hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free);
209 }
210 }
211
212 int config_parse_prefix(
213 const char *unit,
214 const char *filename,
215 unsigned line,
216 const char *section,
217 unsigned section_line,
218 const char *lvalue,
219 int ltype,
220 const char *rvalue,
221 void *data,
222 void *userdata) {
223
224 Network *network = userdata;
225 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
226 uint8_t prefixlen = 64;
227 union in_addr_union in6addr;
228 int r;
229
230 assert(filename);
231 assert(section);
232 assert(lvalue);
233 assert(rvalue);
234 assert(data);
235
236 r = prefix_new_static(network, filename, section_line, &p);
237 if (r < 0)
238 return log_oom();
239
240 r = in_addr_prefix_from_string(rvalue, AF_INET6, &in6addr, &prefixlen);
241 if (r < 0) {
242 log_syntax(unit, LOG_WARNING, filename, line, r, "Prefix is invalid, ignoring assignment: %s", rvalue);
243 return 0;
244 }
245
246 r = sd_radv_prefix_set_prefix(p->radv_prefix, &in6addr.in6, prefixlen);
247 if (r < 0) {
248 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to set radv prefix, ignoring assignment: %s", rvalue);
249 return 0;
250 }
251
252 p = NULL;
253
254 return 0;
255 }
256
257 int config_parse_prefix_flags(
258 const char *unit,
259 const char *filename,
260 unsigned line,
261 const char *section,
262 unsigned section_line,
263 const char *lvalue,
264 int ltype,
265 const char *rvalue,
266 void *data,
267 void *userdata) {
268
269 Network *network = userdata;
270 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
271 int r;
272
273 assert(filename);
274 assert(section);
275 assert(lvalue);
276 assert(rvalue);
277 assert(data);
278
279 r = prefix_new_static(network, filename, section_line, &p);
280 if (r < 0)
281 return log_oom();
282
283 r = parse_boolean(rvalue);
284 if (r < 0) {
285 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
286 return 0;
287 }
288
289 if (streq(lvalue, "OnLink"))
290 r = sd_radv_prefix_set_onlink(p->radv_prefix, r);
291 else if (streq(lvalue, "AddressAutoconfiguration"))
292 r = sd_radv_prefix_set_address_autoconfiguration(p->radv_prefix, r);
293 if (r < 0) {
294 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to set %s=, ignoring assignment: %m", lvalue);
295 return 0;
296 }
297
298 p = NULL;
299
300 return 0;
301 }
302
303 int config_parse_prefix_lifetime(
304 const char *unit,
305 const char *filename,
306 unsigned line,
307 const char *section,
308 unsigned section_line,
309 const char *lvalue,
310 int ltype,
311 const char *rvalue,
312 void *data,
313 void *userdata) {
314
315 Network *network = userdata;
316 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
317 usec_t usec;
318 int r;
319
320 assert(filename);
321 assert(section);
322 assert(lvalue);
323 assert(rvalue);
324 assert(data);
325
326 r = prefix_new_static(network, filename, section_line, &p);
327 if (r < 0)
328 return log_oom();
329
330 r = parse_sec(rvalue, &usec);
331 if (r < 0) {
332 log_syntax(unit, LOG_WARNING, filename, line, r, "Lifetime is invalid, ignoring assignment: %s", rvalue);
333 return 0;
334 }
335
336 /* a value of 0xffffffff represents infinity */
337 if (streq(lvalue, "PreferredLifetimeSec"))
338 r = sd_radv_prefix_set_preferred_lifetime(p->radv_prefix,
339 DIV_ROUND_UP(usec, USEC_PER_SEC));
340 else if (streq(lvalue, "ValidLifetimeSec"))
341 r = sd_radv_prefix_set_valid_lifetime(p->radv_prefix,
342 DIV_ROUND_UP(usec, USEC_PER_SEC));
343 if (r < 0) {
344 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to set %s=, ignoring assignment: %m", lvalue);
345 return 0;
346 }
347
348 p = NULL;
349
350 return 0;
351 }
352
353 int config_parse_prefix_assign(
354 const char *unit,
355 const char *filename,
356 unsigned line,
357 const char *section,
358 unsigned section_line,
359 const char *lvalue,
360 int ltype,
361 const char *rvalue,
362 void *data,
363 void *userdata) {
364
365 Network *network = userdata;
366 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
367 int r;
368
369 assert(filename);
370 assert(section);
371 assert(lvalue);
372 assert(rvalue);
373 assert(data);
374
375 r = prefix_new_static(network, filename, section_line, &p);
376 if (r < 0)
377 return log_oom();
378
379 r = parse_boolean(rvalue);
380 if (r < 0) {
381 log_syntax(unit, LOG_WARNING, filename, line, r,
382 "Failed to parse %s=, ignoring assignment: %s",
383 lvalue, rvalue);
384 return 0;
385 }
386
387 p->assign = r;
388 p = NULL;
389
390 return 0;
391 }
392
393 int config_parse_route_prefix(
394 const char *unit,
395 const char *filename,
396 unsigned line,
397 const char *section,
398 unsigned section_line,
399 const char *lvalue,
400 int ltype,
401 const char *rvalue,
402 void *data,
403 void *userdata) {
404
405 Network *network = userdata;
406 _cleanup_(route_prefix_free_or_set_invalidp) RoutePrefix *p = NULL;
407 uint8_t prefixlen = 64;
408 union in_addr_union in6addr;
409 int r;
410
411 assert(filename);
412 assert(section);
413 assert(lvalue);
414 assert(rvalue);
415 assert(data);
416
417 r = route_prefix_new_static(network, filename, section_line, &p);
418 if (r < 0)
419 return log_oom();
420
421 r = in_addr_prefix_from_string(rvalue, AF_INET6, &in6addr, &prefixlen);
422 if (r < 0) {
423 log_syntax(unit, LOG_WARNING, filename, line, r, "Route prefix is invalid, ignoring assignment: %s", rvalue);
424 return 0;
425 }
426
427 r = sd_radv_prefix_set_route_prefix(p->radv_route_prefix, &in6addr.in6, prefixlen);
428 if (r < 0) {
429 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to set route prefix, ignoring assignment: %m");
430 return 0;
431 }
432
433 p = NULL;
434
435 return 0;
436 }
437
438 int config_parse_route_prefix_lifetime(
439 const char *unit,
440 const char *filename,
441 unsigned line,
442 const char *section,
443 unsigned section_line,
444 const char *lvalue,
445 int ltype,
446 const char *rvalue,
447 void *data,
448 void *userdata) {
449
450 Network *network = userdata;
451 _cleanup_(route_prefix_free_or_set_invalidp) RoutePrefix *p = NULL;
452 usec_t usec;
453 int r;
454
455 assert(filename);
456 assert(section);
457 assert(lvalue);
458 assert(rvalue);
459 assert(data);
460
461 r = route_prefix_new_static(network, filename, section_line, &p);
462 if (r < 0)
463 return log_oom();
464
465 r = parse_sec(rvalue, &usec);
466 if (r < 0) {
467 log_syntax(unit, LOG_WARNING, filename, line, r,
468 "Route lifetime is invalid, ignoring assignment: %s", rvalue);
469 return 0;
470 }
471
472 /* a value of 0xffffffff represents infinity */
473 r = sd_radv_route_prefix_set_lifetime(p->radv_route_prefix, DIV_ROUND_UP(usec, USEC_PER_SEC));
474 if (r < 0) {
475 log_syntax(unit, LOG_WARNING, filename, line, r,
476 "Failed to set route lifetime, ignoring assignment: %m");
477 return 0;
478 }
479
480 p = NULL;
481
482 return 0;
483 }
484
485 static int network_get_ipv6_dns(Network *network, struct in6_addr **ret_addresses, size_t *ret_size) {
486 _cleanup_free_ struct in6_addr *addresses = NULL;
487 size_t n_addresses = 0, n_allocated = 0;
488
489 assert(network);
490 assert(ret_addresses);
491 assert(ret_size);
492
493 for (size_t i = 0; i < network->n_dns; i++) {
494 union in_addr_union *addr;
495
496 if (network->dns[i]->family != AF_INET6)
497 continue;
498
499 addr = &network->dns[i]->address;
500
501 if (in_addr_is_null(AF_INET6, addr) ||
502 in_addr_is_link_local(AF_INET6, addr) ||
503 in_addr_is_localhost(AF_INET6, addr))
504 continue;
505
506 if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1))
507 return -ENOMEM;
508
509 addresses[n_addresses++] = addr->in6;
510 }
511
512 *ret_addresses = TAKE_PTR(addresses);
513 *ret_size = n_addresses;
514
515 return n_addresses;
516 }
517
518 static int radv_set_dns(Link *link, Link *uplink) {
519 _cleanup_free_ struct in6_addr *dns = NULL;
520 usec_t lifetime_usec;
521 size_t n_dns;
522 int r;
523
524 if (!link->network->router_emit_dns)
525 return 0;
526
527 if (link->network->router_dns) {
528 struct in6_addr *p;
529
530 dns = new(struct in6_addr, link->network->n_router_dns);
531 if (!dns)
532 return -ENOMEM;
533
534 p = dns;
535 for (size_t i = 0; i < link->network->n_router_dns; i++)
536 if (IN6_IS_ADDR_UNSPECIFIED(&link->network->router_dns[i])) {
537 if (!IN6_IS_ADDR_UNSPECIFIED(&link->ipv6ll_address))
538 *(p++) = link->ipv6ll_address;
539 } else
540 *(p++) = link->network->router_dns[i];
541
542 n_dns = p - dns;
543 lifetime_usec = link->network->router_dns_lifetime_usec;
544
545 goto set_dns;
546 }
547
548 lifetime_usec = SD_RADV_DEFAULT_DNS_LIFETIME_USEC;
549
550 r = network_get_ipv6_dns(link->network, &dns, &n_dns);
551 if (r > 0)
552 goto set_dns;
553
554 if (uplink) {
555 if (!uplink->network) {
556 log_link_debug(uplink, "Cannot fetch DNS servers as uplink interface is not managed by us");
557 return 0;
558 }
559
560 r = network_get_ipv6_dns(uplink->network, &dns, &n_dns);
561 if (r > 0)
562 goto set_dns;
563 }
564
565 return 0;
566
567 set_dns:
568 return sd_radv_set_rdnss(link->radv,
569 DIV_ROUND_UP(lifetime_usec, USEC_PER_SEC),
570 dns, n_dns);
571 }
572
573 static int radv_set_domains(Link *link, Link *uplink) {
574 OrderedSet *search_domains;
575 usec_t lifetime_usec;
576 _cleanup_free_ char **s = NULL; /* just free() because the strings are owned by the set */
577
578 if (!link->network->router_emit_domains)
579 return 0;
580
581 search_domains = link->network->router_search_domains;
582 lifetime_usec = link->network->router_dns_lifetime_usec;
583
584 if (search_domains)
585 goto set_domains;
586
587 lifetime_usec = SD_RADV_DEFAULT_DNS_LIFETIME_USEC;
588
589 search_domains = link->network->search_domains;
590 if (search_domains)
591 goto set_domains;
592
593 if (uplink) {
594 if (!uplink->network) {
595 log_link_debug(uplink, "Cannot fetch DNS search domains as uplink interface is not managed by us");
596 return 0;
597 }
598
599 search_domains = uplink->network->search_domains;
600 if (search_domains)
601 goto set_domains;
602 }
603
604 return 0;
605
606 set_domains:
607 s = ordered_set_get_strv(search_domains);
608 if (!s)
609 return log_oom();
610
611 return sd_radv_set_dnssl(link->radv,
612 DIV_ROUND_UP(lifetime_usec, USEC_PER_SEC),
613 s);
614
615 }
616
617 int radv_emit_dns(Link *link) {
618 Link *uplink;
619 int r;
620
621 uplink = manager_find_uplink(link->manager, link);
622
623 r = radv_set_dns(link, uplink);
624 if (r < 0)
625 log_link_warning_errno(link, r, "Could not set RA DNS: %m");
626
627 r = radv_set_domains(link, uplink);
628 if (r < 0)
629 log_link_warning_errno(link, r, "Could not set RA Domains: %m");
630
631 return 0;
632 }
633
634 static bool link_radv_enabled(Link *link) {
635 assert(link);
636
637 if (!link_ipv6ll_enabled(link))
638 return false;
639
640 return link->network->router_prefix_delegation;
641 }
642
643 int radv_configure(Link *link) {
644 RoutePrefix *q;
645 Prefix *p;
646 int r;
647
648 assert(link);
649 assert(link->network);
650
651 if (!link_radv_enabled(link))
652 return 0;
653
654 r = sd_radv_new(&link->radv);
655 if (r < 0)
656 return r;
657
658 r = sd_radv_attach_event(link->radv, link->manager->event, 0);
659 if (r < 0)
660 return r;
661
662 r = sd_radv_set_mac(link->radv, &link->mac);
663 if (r < 0)
664 return r;
665
666 r = sd_radv_set_ifindex(link->radv, link->ifindex);
667 if (r < 0)
668 return r;
669
670 r = sd_radv_set_managed_information(link->radv, link->network->router_managed);
671 if (r < 0)
672 return r;
673
674 r = sd_radv_set_other_information(link->radv, link->network->router_other_information);
675 if (r < 0)
676 return r;
677
678 /* a value of 0xffffffff represents infinity, 0x0 means this host is
679 not a router */
680 r = sd_radv_set_router_lifetime(link->radv,
681 DIV_ROUND_UP(link->network->router_lifetime_usec, USEC_PER_SEC));
682 if (r < 0)
683 return r;
684
685 if (link->network->router_lifetime_usec > 0) {
686 r = sd_radv_set_preference(link->radv,
687 link->network->router_preference);
688 if (r < 0)
689 return r;
690 }
691
692 HASHMAP_FOREACH(p, link->network->prefixes_by_section) {
693 r = sd_radv_add_prefix(link->radv, p->radv_prefix, false);
694 if (r == -EEXIST)
695 continue;
696 if (r == -ENOEXEC) {
697 log_link_warning_errno(link, r, "[IPv6Prefix] section configured without Prefix= setting, ignoring section.");
698 continue;
699 }
700 if (r < 0)
701 return r;
702 }
703
704 HASHMAP_FOREACH(q, link->network->route_prefixes_by_section) {
705 r = sd_radv_add_route_prefix(link->radv, q->radv_route_prefix, false);
706 if (r == -EEXIST)
707 continue;
708 if (r < 0)
709 return r;
710 }
711
712 return 0;
713 }
714
715 int radv_update_mac(Link *link) {
716 bool restart;
717 int r;
718
719 assert(link);
720
721 if (!link->radv)
722 return 0;
723
724 restart = sd_radv_is_running(link->radv);
725
726 r = sd_radv_stop(link->radv);
727 if (r < 0)
728 return r;
729
730 r = sd_radv_set_mac(link->radv, &link->mac);
731 if (r < 0)
732 return r;
733
734 if (restart) {
735 r = sd_radv_start(link->radv);
736 if (r < 0)
737 return r;
738 }
739
740 return 0;
741 }
742
743 int radv_add_prefix(
744 Link *link,
745 const struct in6_addr *prefix,
746 uint8_t prefix_len,
747 uint32_t lifetime_preferred,
748 uint32_t lifetime_valid) {
749
750 _cleanup_(sd_radv_prefix_unrefp) sd_radv_prefix *p = NULL;
751 int r;
752
753 assert(link);
754
755 if (!link->radv)
756 return 0;
757
758 r = sd_radv_prefix_new(&p);
759 if (r < 0)
760 return r;
761
762 r = sd_radv_prefix_set_prefix(p, prefix, prefix_len);
763 if (r < 0)
764 return r;
765
766 r = sd_radv_prefix_set_preferred_lifetime(p, lifetime_preferred);
767 if (r < 0)
768 return r;
769
770 r = sd_radv_prefix_set_valid_lifetime(p, lifetime_valid);
771 if (r < 0)
772 return r;
773
774 r = sd_radv_add_prefix(link->radv, p, true);
775 if (r < 0 && r != -EEXIST)
776 return r;
777
778 return 0;
779 }
780
781 int config_parse_radv_dns(
782 const char *unit,
783 const char *filename,
784 unsigned line,
785 const char *section,
786 unsigned section_line,
787 const char *lvalue,
788 int ltype,
789 const char *rvalue,
790 void *data,
791 void *userdata) {
792
793 Network *n = data;
794 int r;
795
796 assert(filename);
797 assert(lvalue);
798 assert(rvalue);
799
800 if (isempty(rvalue)) {
801 n->n_router_dns = 0;
802 n->router_dns = mfree(n->router_dns);
803 return 0;
804 }
805
806 for (const char *p = rvalue;;) {
807 _cleanup_free_ char *w = NULL;
808 union in_addr_union a;
809
810 r = extract_first_word(&p, &w, NULL, 0);
811 if (r == -ENOMEM)
812 return log_oom();
813 if (r < 0) {
814 log_syntax(unit, LOG_WARNING, filename, line, r,
815 "Failed to extract word, ignoring: %s", rvalue);
816 return 0;
817 }
818 if (r == 0)
819 return 0;
820
821 if (streq(w, "_link_local"))
822 a = IN_ADDR_NULL;
823 else {
824 r = in_addr_from_string(AF_INET6, w, &a);
825 if (r < 0) {
826 log_syntax(unit, LOG_WARNING, filename, line, r,
827 "Failed to parse DNS server address, ignoring: %s", w);
828 continue;
829 }
830
831 if (in_addr_is_null(AF_INET6, &a)) {
832 log_syntax(unit, LOG_WARNING, filename, line, 0,
833 "DNS server address is null, ignoring: %s", w);
834 continue;
835 }
836 }
837
838 struct in6_addr *m;
839 m = reallocarray(n->router_dns, n->n_router_dns + 1, sizeof(struct in6_addr));
840 if (!m)
841 return log_oom();
842
843 m[n->n_router_dns++] = a.in6;
844 n->router_dns = m;
845 }
846 }
847
848 int config_parse_radv_search_domains(
849 const char *unit,
850 const char *filename,
851 unsigned line,
852 const char *section,
853 unsigned section_line,
854 const char *lvalue,
855 int ltype,
856 const char *rvalue,
857 void *data,
858 void *userdata) {
859
860 Network *n = data;
861 int r;
862
863 assert(filename);
864 assert(lvalue);
865 assert(rvalue);
866
867 if (isempty(rvalue)) {
868 n->router_search_domains = ordered_set_free(n->router_search_domains);
869 return 0;
870 }
871
872 for (const char *p = rvalue;;) {
873 _cleanup_free_ char *w = NULL, *idna = NULL;
874
875 r = extract_first_word(&p, &w, NULL, 0);
876 if (r == -ENOMEM)
877 return log_oom();
878 if (r < 0) {
879 log_syntax(unit, LOG_WARNING, filename, line, r,
880 "Failed to extract word, ignoring: %s", rvalue);
881 return 0;
882 }
883 if (r == 0)
884 return 0;
885
886 r = dns_name_apply_idna(w, &idna);
887 if (r < 0) {
888 log_syntax(unit, LOG_WARNING, filename, line, r,
889 "Failed to apply IDNA to domain name '%s', ignoring: %m", w);
890 continue;
891 } else if (r == 0)
892 /* transfer ownership to simplify subsequent operations */
893 idna = TAKE_PTR(w);
894
895 r = ordered_set_ensure_allocated(&n->router_search_domains, &string_hash_ops_free);
896 if (r < 0)
897 return log_oom();
898
899 r = ordered_set_consume(n->router_search_domains, TAKE_PTR(idna));
900 if (r < 0)
901 return log_oom();
902 }
903 }
904
905 static const char * const radv_prefix_delegation_table[_RADV_PREFIX_DELEGATION_MAX] = {
906 [RADV_PREFIX_DELEGATION_NONE] = "no",
907 [RADV_PREFIX_DELEGATION_STATIC] = "static",
908 [RADV_PREFIX_DELEGATION_DHCP6] = "dhcpv6",
909 [RADV_PREFIX_DELEGATION_BOTH] = "yes",
910 };
911
912 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
913 radv_prefix_delegation,
914 RADVPrefixDelegation,
915 RADV_PREFIX_DELEGATION_BOTH);
916
917 int config_parse_router_prefix_delegation(
918 const char *unit,
919 const char *filename,
920 unsigned line,
921 const char *section,
922 unsigned section_line,
923 const char *lvalue,
924 int ltype,
925 const char *rvalue,
926 void *data,
927 void *userdata) {
928
929 RADVPrefixDelegation val, *ra = data;
930 int r;
931
932 assert(filename);
933 assert(lvalue);
934 assert(rvalue);
935 assert(data);
936
937 if (streq(lvalue, "IPv6SendRA")) {
938 r = parse_boolean(rvalue);
939 if (r < 0) {
940 log_syntax(unit, LOG_WARNING, filename, line, r,
941 "Invalid %s= setting, ignoring assignment: %s", lvalue, rvalue);
942 return 0;
943 }
944
945 /* When IPv6SendRA= is enabled, only static prefixes are sent by default, and users
946 * need to explicitly enable DHCPv6PrefixDelegation=. */
947 *ra = r ? RADV_PREFIX_DELEGATION_STATIC : RADV_PREFIX_DELEGATION_NONE;
948 return 0;
949 }
950
951 /* For backward compatibility */
952 val = radv_prefix_delegation_from_string(rvalue);
953 if (val < 0) {
954 log_syntax(unit, LOG_WARNING, filename, line, 0,
955 "Invalid %s= setting, ignoring assignment: %s", lvalue, rvalue);
956 return 0;
957 }
958
959 *ra = val;
960 return 0;
961 }
962
963 int config_parse_router_preference(
964 const char *unit,
965 const char *filename,
966 unsigned line,
967 const char *section,
968 unsigned section_line,
969 const char *lvalue,
970 int ltype,
971 const char *rvalue,
972 void *data,
973 void *userdata) {
974
975 Network *network = userdata;
976
977 assert(filename);
978 assert(section);
979 assert(lvalue);
980 assert(rvalue);
981 assert(data);
982
983 if (streq(rvalue, "high"))
984 network->router_preference = SD_NDISC_PREFERENCE_HIGH;
985 else if (STR_IN_SET(rvalue, "medium", "normal", "default"))
986 network->router_preference = SD_NDISC_PREFERENCE_MEDIUM;
987 else if (streq(rvalue, "low"))
988 network->router_preference = SD_NDISC_PREFERENCE_LOW;
989 else
990 log_syntax(unit, LOG_WARNING, filename, line, 0,
991 "Invalid router preference, ignoring assignment: %s", rvalue);
992
993 return 0;
994 }