Line data Source code
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-address.h"
11 : #include "networkd-manager.h"
12 : #include "networkd-radv.h"
13 : #include "parse-util.h"
14 : #include "sd-radv.h"
15 : #include "string-util.h"
16 : #include "string-table.h"
17 : #include "strv.h"
18 :
19 0 : void prefix_free(Prefix *prefix) {
20 0 : if (!prefix)
21 0 : return;
22 :
23 0 : if (prefix->network) {
24 0 : LIST_REMOVE(prefixes, prefix->network->static_prefixes, prefix);
25 0 : assert(prefix->network->n_static_prefixes > 0);
26 0 : prefix->network->n_static_prefixes--;
27 :
28 0 : if (prefix->section)
29 0 : hashmap_remove(prefix->network->prefixes_by_section,
30 0 : prefix->section);
31 : }
32 :
33 0 : network_config_section_free(prefix->section);
34 0 : prefix->radv_prefix = sd_radv_prefix_unref(prefix->radv_prefix);
35 :
36 0 : free(prefix);
37 : }
38 :
39 0 : int prefix_new(Prefix **ret) {
40 0 : _cleanup_(prefix_freep) Prefix *prefix = NULL;
41 :
42 0 : prefix = new0(Prefix, 1);
43 0 : if (!prefix)
44 0 : return -ENOMEM;
45 :
46 0 : if (sd_radv_prefix_new(&prefix->radv_prefix) < 0)
47 0 : return -ENOMEM;
48 :
49 0 : *ret = TAKE_PTR(prefix);
50 :
51 0 : return 0;
52 : }
53 :
54 0 : static int prefix_new_static(Network *network, const char *filename,
55 : unsigned section_line, Prefix **ret) {
56 0 : _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
57 0 : _cleanup_(prefix_freep) Prefix *prefix = NULL;
58 : int r;
59 :
60 0 : assert(network);
61 0 : assert(ret);
62 0 : assert(!!filename == (section_line > 0));
63 :
64 0 : if (filename) {
65 0 : r = network_config_section_new(filename, section_line, &n);
66 0 : if (r < 0)
67 0 : return r;
68 :
69 0 : if (section_line) {
70 0 : prefix = hashmap_get(network->prefixes_by_section, n);
71 0 : if (prefix) {
72 0 : *ret = TAKE_PTR(prefix);
73 :
74 0 : return 0;
75 : }
76 : }
77 : }
78 :
79 0 : r = prefix_new(&prefix);
80 0 : if (r < 0)
81 0 : return r;
82 :
83 0 : prefix->network = network;
84 0 : LIST_APPEND(prefixes, network->static_prefixes, prefix);
85 0 : network->n_static_prefixes++;
86 :
87 0 : if (filename) {
88 0 : prefix->section = TAKE_PTR(n);
89 :
90 0 : r = hashmap_ensure_allocated(&network->prefixes_by_section, &network_config_hash_ops);
91 0 : if (r < 0)
92 0 : return r;
93 :
94 0 : r = hashmap_put(network->prefixes_by_section, prefix->section, prefix);
95 0 : if (r < 0)
96 0 : return r;
97 : }
98 :
99 0 : *ret = TAKE_PTR(prefix);
100 :
101 0 : return 0;
102 : }
103 :
104 0 : int config_parse_prefix(const char *unit,
105 : const char *filename,
106 : unsigned line,
107 : const char *section,
108 : unsigned section_line,
109 : const char *lvalue,
110 : int ltype,
111 : const char *rvalue,
112 : void *data,
113 : void *userdata) {
114 :
115 0 : Network *network = userdata;
116 0 : _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
117 0 : uint8_t prefixlen = 64;
118 : union in_addr_union in6addr;
119 : int r;
120 :
121 0 : assert(filename);
122 0 : assert(section);
123 0 : assert(lvalue);
124 0 : assert(rvalue);
125 0 : assert(data);
126 :
127 0 : r = prefix_new_static(network, filename, section_line, &p);
128 0 : if (r < 0)
129 0 : return r;
130 :
131 0 : r = in_addr_prefix_from_string(rvalue, AF_INET6, &in6addr, &prefixlen);
132 0 : if (r < 0) {
133 0 : log_syntax(unit, LOG_ERR, filename, line, r, "Prefix is invalid, ignoring assignment: %s", rvalue);
134 0 : return 0;
135 : }
136 :
137 0 : if (sd_radv_prefix_set_prefix(p->radv_prefix, &in6addr.in6, prefixlen) < 0)
138 0 : return -EADDRNOTAVAIL;
139 :
140 0 : log_syntax(unit, LOG_INFO, filename, line, r, "Found prefix %s", rvalue);
141 :
142 0 : p = NULL;
143 :
144 0 : return 0;
145 : }
146 :
147 0 : int config_parse_prefix_flags(const char *unit,
148 : const char *filename,
149 : unsigned line,
150 : const char *section,
151 : unsigned section_line,
152 : const char *lvalue,
153 : int ltype,
154 : const char *rvalue,
155 : void *data,
156 : void *userdata) {
157 0 : Network *network = userdata;
158 0 : _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
159 : int r, val;
160 :
161 0 : assert(filename);
162 0 : assert(section);
163 0 : assert(lvalue);
164 0 : assert(rvalue);
165 0 : assert(data);
166 :
167 0 : r = prefix_new_static(network, filename, section_line, &p);
168 0 : if (r < 0)
169 0 : return r;
170 :
171 0 : r = parse_boolean(rvalue);
172 0 : if (r < 0) {
173 0 : log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address flag, ignoring: %s", rvalue);
174 0 : return 0;
175 : }
176 :
177 0 : val = r;
178 :
179 0 : if (streq(lvalue, "OnLink"))
180 0 : r = sd_radv_prefix_set_onlink(p->radv_prefix, val);
181 0 : else if (streq(lvalue, "AddressAutoconfiguration"))
182 0 : r = sd_radv_prefix_set_address_autoconfiguration(p->radv_prefix, val);
183 0 : if (r < 0)
184 0 : return r;
185 :
186 0 : p = NULL;
187 :
188 0 : return 0;
189 : }
190 :
191 0 : int config_parse_prefix_lifetime(const char *unit,
192 : const char *filename,
193 : unsigned line,
194 : const char *section,
195 : unsigned section_line,
196 : const char *lvalue,
197 : int ltype,
198 : const char *rvalue,
199 : void *data,
200 : void *userdata) {
201 0 : Network *network = userdata;
202 0 : _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
203 : usec_t usec;
204 : int r;
205 :
206 0 : assert(filename);
207 0 : assert(section);
208 0 : assert(lvalue);
209 0 : assert(rvalue);
210 0 : assert(data);
211 :
212 0 : r = prefix_new_static(network, filename, section_line, &p);
213 0 : if (r < 0)
214 0 : return r;
215 :
216 0 : r = parse_sec(rvalue, &usec);
217 0 : if (r < 0) {
218 0 : log_syntax(unit, LOG_ERR, filename, line, r, "Lifetime is invalid, ignoring assignment: %s", rvalue);
219 0 : return 0;
220 : }
221 :
222 : /* a value of 0xffffffff represents infinity */
223 0 : if (streq(lvalue, "PreferredLifetimeSec"))
224 0 : r = sd_radv_prefix_set_preferred_lifetime(p->radv_prefix,
225 0 : DIV_ROUND_UP(usec, USEC_PER_SEC));
226 0 : else if (streq(lvalue, "ValidLifetimeSec"))
227 0 : r = sd_radv_prefix_set_valid_lifetime(p->radv_prefix,
228 0 : DIV_ROUND_UP(usec, USEC_PER_SEC));
229 0 : if (r < 0)
230 0 : return r;
231 :
232 0 : p = NULL;
233 :
234 0 : return 0;
235 : }
236 :
237 0 : static int radv_get_ip6dns(Network *network, struct in6_addr **dns,
238 : size_t *n_dns) {
239 0 : _cleanup_free_ struct in6_addr *addresses = NULL;
240 0 : size_t i, n_addresses = 0, n_allocated = 0;
241 :
242 0 : assert(network);
243 0 : assert(dns);
244 0 : assert(n_dns);
245 :
246 0 : for (i = 0; i < network->n_dns; i++) {
247 : union in_addr_union *addr;
248 :
249 0 : if (network->dns[i].family != AF_INET6)
250 0 : continue;
251 :
252 0 : addr = &network->dns[i].address;
253 :
254 0 : if (in_addr_is_null(AF_INET6, addr) ||
255 0 : in_addr_is_link_local(AF_INET6, addr) ||
256 0 : in_addr_is_localhost(AF_INET6, addr))
257 0 : continue;
258 :
259 0 : if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1))
260 0 : return -ENOMEM;
261 :
262 0 : addresses[n_addresses++] = addr->in6;
263 : }
264 :
265 0 : if (addresses) {
266 0 : *dns = TAKE_PTR(addresses);
267 :
268 0 : *n_dns = n_addresses;
269 : }
270 :
271 0 : return n_addresses;
272 : }
273 :
274 0 : static int radv_set_dns(Link *link, Link *uplink) {
275 0 : _cleanup_free_ struct in6_addr *dns = NULL;
276 : size_t n_dns;
277 : usec_t lifetime_usec;
278 : int r;
279 :
280 0 : if (!link->network->router_emit_dns)
281 0 : return 0;
282 :
283 0 : if (link->network->router_dns) {
284 0 : dns = newdup(struct in6_addr, link->network->router_dns,
285 : link->network->n_router_dns);
286 0 : if (!dns)
287 0 : return -ENOMEM;
288 :
289 0 : n_dns = link->network->n_router_dns;
290 0 : lifetime_usec = link->network->router_dns_lifetime_usec;
291 :
292 0 : goto set_dns;
293 : }
294 :
295 0 : lifetime_usec = SD_RADV_DEFAULT_DNS_LIFETIME_USEC;
296 :
297 0 : r = radv_get_ip6dns(link->network, &dns, &n_dns);
298 0 : if (r > 0)
299 0 : goto set_dns;
300 :
301 0 : if (uplink) {
302 0 : if (!uplink->network) {
303 0 : log_link_debug(uplink, "Cannot fetch DNS servers as uplink interface is not managed by us");
304 0 : return 0;
305 : }
306 :
307 0 : r = radv_get_ip6dns(uplink->network, &dns, &n_dns);
308 0 : if (r > 0)
309 0 : goto set_dns;
310 : }
311 :
312 0 : return 0;
313 :
314 0 : set_dns:
315 0 : return sd_radv_set_rdnss(link->radv,
316 0 : DIV_ROUND_UP(lifetime_usec, USEC_PER_SEC),
317 : dns, n_dns);
318 : }
319 :
320 0 : static int radv_set_domains(Link *link, Link *uplink) {
321 : OrderedSet *search_domains;
322 : usec_t lifetime_usec;
323 0 : _cleanup_free_ char **s = NULL; /* just free() because the strings are owned by the set */
324 :
325 0 : if (!link->network->router_emit_domains)
326 0 : return 0;
327 :
328 0 : search_domains = link->network->router_search_domains;
329 0 : lifetime_usec = link->network->router_dns_lifetime_usec;
330 :
331 0 : if (search_domains)
332 0 : goto set_domains;
333 :
334 0 : lifetime_usec = SD_RADV_DEFAULT_DNS_LIFETIME_USEC;
335 :
336 0 : search_domains = link->network->search_domains;
337 0 : if (search_domains)
338 0 : goto set_domains;
339 :
340 0 : if (uplink) {
341 0 : if (!uplink->network) {
342 0 : log_link_debug(uplink, "Cannot fetch DNS search domains as uplink interface is not managed by us");
343 0 : return 0;
344 : }
345 :
346 0 : search_domains = uplink->network->search_domains;
347 0 : if (search_domains)
348 0 : goto set_domains;
349 : }
350 :
351 0 : return 0;
352 :
353 0 : set_domains:
354 0 : s = ordered_set_get_strv(search_domains);
355 0 : if (!s)
356 0 : return log_oom();
357 :
358 0 : return sd_radv_set_dnssl(link->radv,
359 0 : DIV_ROUND_UP(lifetime_usec, USEC_PER_SEC),
360 : s);
361 :
362 : }
363 :
364 0 : int radv_emit_dns(Link *link) {
365 : Link *uplink;
366 : int r;
367 :
368 0 : uplink = manager_find_uplink(link->manager, link);
369 :
370 0 : r = radv_set_dns(link, uplink);
371 0 : if (r < 0)
372 0 : log_link_warning_errno(link, r, "Could not set RA DNS: %m");
373 :
374 0 : r = radv_set_domains(link, uplink);
375 0 : if (r < 0)
376 0 : log_link_warning_errno(link, r, "Could not set RA Domains: %m");
377 :
378 0 : return 0;
379 : }
380 :
381 0 : int radv_configure(Link *link) {
382 : int r;
383 : Prefix *p;
384 :
385 0 : assert(link);
386 0 : assert(link->network);
387 :
388 0 : r = sd_radv_new(&link->radv);
389 0 : if (r < 0)
390 0 : return r;
391 :
392 0 : r = sd_radv_attach_event(link->radv, NULL, 0);
393 0 : if (r < 0)
394 0 : return r;
395 :
396 0 : r = sd_radv_set_mac(link->radv, &link->mac);
397 0 : if (r < 0)
398 0 : return r;
399 :
400 0 : r = sd_radv_set_ifindex(link->radv, link->ifindex);
401 0 : if (r < 0)
402 0 : return r;
403 :
404 0 : r = sd_radv_set_managed_information(link->radv, link->network->router_managed);
405 0 : if (r < 0)
406 0 : return r;
407 :
408 0 : r = sd_radv_set_other_information(link->radv, link->network->router_other_information);
409 0 : if (r < 0)
410 0 : return r;
411 :
412 : /* a value of 0xffffffff represents infinity, 0x0 means this host is
413 : not a router */
414 0 : r = sd_radv_set_router_lifetime(link->radv,
415 0 : DIV_ROUND_UP(link->network->router_lifetime_usec, USEC_PER_SEC));
416 0 : if (r < 0)
417 0 : return r;
418 :
419 0 : if (link->network->router_lifetime_usec > 0) {
420 0 : r = sd_radv_set_preference(link->radv,
421 0 : link->network->router_preference);
422 0 : if (r < 0)
423 0 : return r;
424 : }
425 :
426 0 : if (IN_SET(link->network->router_prefix_delegation,
427 : RADV_PREFIX_DELEGATION_STATIC,
428 : RADV_PREFIX_DELEGATION_BOTH)) {
429 :
430 0 : LIST_FOREACH(prefixes, p, link->network->static_prefixes) {
431 0 : r = sd_radv_add_prefix(link->radv, p->radv_prefix, false);
432 0 : if (r == -EEXIST)
433 0 : continue;
434 0 : if (r == -ENOEXEC) {
435 0 : log_link_warning_errno(link, r, "[IPv6Prefix] section configured without Prefix= setting, ignoring section.");
436 0 : continue;
437 : }
438 0 : if (r < 0)
439 0 : return r;
440 : }
441 : }
442 :
443 0 : return radv_emit_dns(link);
444 : }
445 :
446 0 : int config_parse_radv_dns(
447 : const char *unit,
448 : const char *filename,
449 : unsigned line,
450 : const char *section,
451 : unsigned section_line,
452 : const char *lvalue,
453 : int ltype,
454 : const char *rvalue,
455 : void *data,
456 : void *userdata) {
457 :
458 0 : Network *n = data;
459 0 : const char *p = rvalue;
460 : int r;
461 :
462 0 : assert(filename);
463 0 : assert(lvalue);
464 0 : assert(rvalue);
465 :
466 0 : for (;;) {
467 0 : _cleanup_free_ char *w = NULL;
468 : union in_addr_union a;
469 :
470 0 : r = extract_first_word(&p, &w, NULL, 0);
471 0 : if (r == -ENOMEM)
472 0 : return log_oom();
473 0 : if (r < 0) {
474 0 : log_syntax(unit, LOG_ERR, filename, line, r,
475 : "Failed to extract word, ignoring: %s", rvalue);
476 0 : return 0;
477 : }
478 0 : if (r == 0)
479 0 : break;
480 :
481 0 : if (in_addr_from_string(AF_INET6, w, &a) >= 0) {
482 : struct in6_addr *m;
483 :
484 0 : m = reallocarray(n->router_dns, n->n_router_dns + 1, sizeof(struct in6_addr));
485 0 : if (!m)
486 0 : return log_oom();
487 :
488 0 : m[n->n_router_dns++] = a.in6;
489 0 : n->router_dns = m;
490 :
491 : } else
492 0 : log_syntax(unit, LOG_ERR, filename, line, 0,
493 : "Failed to parse DNS server address, ignoring: %s", w);
494 : }
495 :
496 0 : return 0;
497 : }
498 :
499 0 : int config_parse_radv_search_domains(
500 : const char *unit,
501 : const char *filename,
502 : unsigned line,
503 : const char *section,
504 : unsigned section_line,
505 : const char *lvalue,
506 : int ltype,
507 : const char *rvalue,
508 : void *data,
509 : void *userdata) {
510 :
511 0 : Network *n = data;
512 0 : const char *p = rvalue;
513 : int r;
514 :
515 0 : assert(filename);
516 0 : assert(lvalue);
517 0 : assert(rvalue);
518 :
519 0 : for (;;) {
520 0 : _cleanup_free_ char *w = NULL, *idna = NULL;
521 :
522 0 : r = extract_first_word(&p, &w, NULL, 0);
523 0 : if (r == -ENOMEM)
524 0 : return log_oom();
525 0 : if (r < 0) {
526 0 : log_syntax(unit, LOG_ERR, filename, line, r,
527 : "Failed to extract word, ignoring: %s", rvalue);
528 0 : return 0;
529 : }
530 0 : if (r == 0)
531 0 : break;
532 :
533 0 : r = dns_name_apply_idna(w, &idna);
534 0 : if (r < 0) {
535 0 : log_syntax(unit, LOG_ERR, filename, line, r,
536 : "Failed to apply IDNA to domain name '%s', ignoring: %m", w);
537 0 : continue;
538 0 : } else if (r == 0)
539 : /* transfer ownership to simplify subsequent operations */
540 0 : idna = TAKE_PTR(w);
541 :
542 0 : r = ordered_set_ensure_allocated(&n->router_search_domains, &string_hash_ops);
543 0 : if (r < 0)
544 0 : return r;
545 :
546 0 : r = ordered_set_consume(n->router_search_domains, TAKE_PTR(idna));
547 0 : if (r < 0)
548 0 : return r;
549 : }
550 :
551 0 : return 0;
552 : }
553 :
554 : static const char * const radv_prefix_delegation_table[_RADV_PREFIX_DELEGATION_MAX] = {
555 : [RADV_PREFIX_DELEGATION_NONE] = "no",
556 : [RADV_PREFIX_DELEGATION_STATIC] = "static",
557 : [RADV_PREFIX_DELEGATION_DHCP6] = "dhcpv6",
558 : [RADV_PREFIX_DELEGATION_BOTH] = "yes",
559 : };
560 :
561 12 : DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
562 : radv_prefix_delegation,
563 : RADVPrefixDelegation,
564 : RADV_PREFIX_DELEGATION_BOTH);
565 :
566 0 : int config_parse_router_prefix_delegation(
567 : const char *unit,
568 : const char *filename,
569 : unsigned line,
570 : const char *section,
571 : unsigned section_line,
572 : const char *lvalue,
573 : int ltype,
574 : const char *rvalue,
575 : void *data,
576 : void *userdata) {
577 :
578 0 : Network *network = userdata;
579 : RADVPrefixDelegation d;
580 :
581 0 : assert(filename);
582 0 : assert(section);
583 0 : assert(lvalue);
584 0 : assert(rvalue);
585 0 : assert(data);
586 :
587 0 : d = radv_prefix_delegation_from_string(rvalue);
588 0 : if (d < 0) {
589 0 : log_syntax(unit, LOG_ERR, filename, line, -EINVAL, "Invalid router prefix delegation '%s', ignoring assignment.", rvalue);
590 0 : return 0;
591 : }
592 :
593 0 : network->router_prefix_delegation = d;
594 :
595 0 : return 0;
596 : }
597 :
598 0 : int config_parse_router_preference(const char *unit,
599 : const char *filename,
600 : unsigned line,
601 : const char *section,
602 : unsigned section_line,
603 : const char *lvalue,
604 : int ltype,
605 : const char *rvalue,
606 : void *data,
607 : void *userdata) {
608 0 : Network *network = userdata;
609 :
610 0 : assert(filename);
611 0 : assert(section);
612 0 : assert(lvalue);
613 0 : assert(rvalue);
614 0 : assert(data);
615 :
616 0 : if (streq(rvalue, "high"))
617 0 : network->router_preference = SD_NDISC_PREFERENCE_HIGH;
618 0 : else if (STR_IN_SET(rvalue, "medium", "normal", "default"))
619 0 : network->router_preference = SD_NDISC_PREFERENCE_MEDIUM;
620 0 : else if (streq(rvalue, "low"))
621 0 : network->router_preference = SD_NDISC_PREFERENCE_LOW;
622 : else
623 0 : log_syntax(unit, LOG_ERR, filename, line, -EINVAL, "Router preference '%s' is invalid, ignoring assignment: %m", rvalue);
624 :
625 0 : return 0;
626 : }
|