]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-radv.c
network: make address_configure() and friends take Request object
[thirdparty/systemd.git] / src / network / networkd-radv.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
091214b6 2/***
810adae9 3 Copyright © 2017 Intel Corporation. All rights reserved.
091214b6
PF
4***/
5
6#include <netinet/icmp6.h>
7#include <arpa/inet.h>
8
ca5ad760 9#include "dns-domain.h"
f09a4747 10#include "networkd-address-generation.h"
3b6a3bde 11#include "networkd-address.h"
d5ebcf65 12#include "networkd-dhcp-prefix-delegation.h"
b5ce4047 13#include "networkd-link.h"
c555a358 14#include "networkd-manager.h"
b5ce4047 15#include "networkd-network.h"
a254fab2 16#include "networkd-queue.h"
091214b6 17#include "networkd-radv.h"
344b3cff 18#include "networkd-route-util.h"
6e849e95 19#include "parse-util.h"
4f1ac4a3 20#include "radv-internal.h"
6e849e95 21#include "string-util.h"
6b1dec66 22#include "string-table.h"
51517f9e 23#include "strv.h"
6e849e95 24
0943b3b7
YW
25void network_adjust_radv(Network *network) {
26 assert(network);
27
28 /* After this function is called, network->router_prefix_delegation can be treated as a boolean. */
29
a27588d4 30 if (network->dhcp_pd < 0)
0943b3b7 31 /* For backward compatibility. */
a27588d4 32 network->dhcp_pd = FLAGS_SET(network->router_prefix_delegation, RADV_PREFIX_DELEGATION_DHCP6);
0943b3b7
YW
33
34 if (!FLAGS_SET(network->link_local, ADDRESS_FAMILY_IPV6)) {
35 if (network->router_prefix_delegation != RADV_PREFIX_DELEGATION_NONE)
36 log_warning("%s: IPv6PrefixDelegation= is enabled but IPv6 link local addressing is disabled. "
37 "Disabling IPv6PrefixDelegation=.", network->filename);
38
39 network->router_prefix_delegation = RADV_PREFIX_DELEGATION_NONE;
40 }
41
42 if (network->router_prefix_delegation == RADV_PREFIX_DELEGATION_NONE) {
43 network->n_router_dns = 0;
44 network->router_dns = mfree(network->router_dns);
45 network->router_search_domains = ordered_set_free(network->router_search_domains);
46 }
47
48 if (!FLAGS_SET(network->router_prefix_delegation, RADV_PREFIX_DELEGATION_STATIC)) {
49 network->prefixes_by_section = hashmap_free_with_destructor(network->prefixes_by_section, prefix_free);
50 network->route_prefixes_by_section = hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free);
51 }
52}
53
0f96a823 54bool link_radv_enabled(Link *link) {
0943b3b7
YW
55 assert(link);
56
e096cab2 57 if (!link_may_have_ipv6ll(link))
0943b3b7
YW
58 return false;
59
218a8502
YW
60 if (link->hw_addr.length != ETH_ALEN)
61 return false;
62
0943b3b7
YW
63 return link->network->router_prefix_delegation;
64}
65
064dfb05 66Prefix *prefix_free(Prefix *prefix) {
6e849e95 67 if (!prefix)
064dfb05 68 return NULL;
6e849e95
PF
69
70 if (prefix->network) {
ecb0e85e
YW
71 assert(prefix->section);
72 hashmap_remove(prefix->network->prefixes_by_section, prefix->section);
6e849e95
PF
73 }
74
307fe3cd 75 config_section_free(prefix->section);
e609cd06 76 set_free(prefix->tokens);
6e849e95 77
064dfb05 78 return mfree(prefix);
6e849e95
PF
79}
80
307fe3cd 81DEFINE_SECTION_CLEANUP_FUNCTIONS(Prefix, prefix_free);
064dfb05 82
a8d4a210 83static int prefix_new_static(Network *network, const char *filename, unsigned section_line, Prefix **ret) {
307fe3cd 84 _cleanup_(config_section_freep) ConfigSection *n = NULL;
8e766630 85 _cleanup_(prefix_freep) Prefix *prefix = NULL;
6e849e95
PF
86 int r;
87
88 assert(network);
89 assert(ret);
ecb0e85e
YW
90 assert(filename);
91 assert(section_line > 0);
6e849e95 92
307fe3cd 93 r = config_section_new(filename, section_line, &n);
ecb0e85e
YW
94 if (r < 0)
95 return r;
6e849e95 96
ecb0e85e
YW
97 prefix = hashmap_get(network->prefixes_by_section, n);
98 if (prefix) {
99 *ret = TAKE_PTR(prefix);
100 return 0;
6e849e95
PF
101 }
102
2ac41679
YW
103 prefix = new(Prefix, 1);
104 if (!prefix)
105 return -ENOMEM;
106
107 *prefix = (Prefix) {
108 .network = network,
109 .section = TAKE_PTR(n),
6e849e95 110
c9e2c2da
YW
111 .preferred_lifetime = RADV_DEFAULT_PREFERRED_LIFETIME_USEC,
112 .valid_lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC,
2ac41679
YW
113 .onlink = true,
114 .address_auto_configuration = true,
115 };
6e849e95 116
307fe3cd 117 r = hashmap_ensure_put(&network->prefixes_by_section, &config_section_hash_ops, prefix->section, prefix);
ecb0e85e
YW
118 if (r < 0)
119 return r;
6e849e95 120
1cc6c93a 121 *ret = TAKE_PTR(prefix);
6e849e95
PF
122 return 0;
123}
124
064dfb05 125RoutePrefix *route_prefix_free(RoutePrefix *prefix) {
203d4df5 126 if (!prefix)
064dfb05 127 return NULL;
203d4df5
SS
128
129 if (prefix->network) {
ecb0e85e
YW
130 assert(prefix->section);
131 hashmap_remove(prefix->network->route_prefixes_by_section, prefix->section);
203d4df5
SS
132 }
133
307fe3cd 134 config_section_free(prefix->section);
203d4df5 135
064dfb05
YW
136 return mfree(prefix);
137}
138
307fe3cd 139DEFINE_SECTION_CLEANUP_FUNCTIONS(RoutePrefix, route_prefix_free);
064dfb05 140
a8d4a210 141static int route_prefix_new_static(Network *network, const char *filename, unsigned section_line, RoutePrefix **ret) {
307fe3cd 142 _cleanup_(config_section_freep) ConfigSection *n = NULL;
95081e08 143 _cleanup_(route_prefix_freep) RoutePrefix *prefix = NULL;
203d4df5
SS
144 int r;
145
146 assert(network);
147 assert(ret);
ecb0e85e
YW
148 assert(filename);
149 assert(section_line > 0);
203d4df5 150
307fe3cd 151 r = config_section_new(filename, section_line, &n);
ecb0e85e
YW
152 if (r < 0)
153 return r;
203d4df5 154
ecb0e85e
YW
155 prefix = hashmap_get(network->route_prefixes_by_section, n);
156 if (prefix) {
157 *ret = TAKE_PTR(prefix);
158 return 0;
203d4df5
SS
159 }
160
2ac41679
YW
161 prefix = new(RoutePrefix, 1);
162 if (!prefix)
163 return -ENOMEM;
203d4df5 164
2ac41679
YW
165 *prefix = (RoutePrefix) {
166 .network = network,
167 .section = TAKE_PTR(n),
168
c9e2c2da 169 .lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC,
2ac41679 170 };
203d4df5 171
307fe3cd 172 r = hashmap_ensure_put(&network->route_prefixes_by_section, &config_section_hash_ops, prefix->section, prefix);
ecb0e85e
YW
173 if (r < 0)
174 return r;
203d4df5
SS
175
176 *ret = TAKE_PTR(prefix);
203d4df5
SS
177 return 0;
178}
179
3b6a3bde
YW
180int link_request_radv_addresses(Link *link) {
181 Prefix *p;
182 int r;
183
184 assert(link);
185
186 if (!link_radv_enabled(link))
187 return 0;
188
189 HASHMAP_FOREACH(p, link->network->prefixes_by_section) {
e609cd06 190 _cleanup_set_free_ Set *addresses = NULL;
2ac41679 191 struct in6_addr *a;
3b6a3bde
YW
192
193 if (!p->assign)
194 continue;
195
e609cd06 196 /* radv_generate_addresses() below requires the prefix length <= 64. */
2110040b 197 if (p->prefixlen > 64)
fcd7ad52 198 continue;
fcd7ad52 199
2ac41679 200 r = radv_generate_addresses(link, p->tokens, &p->prefix, p->prefixlen, &addresses);
3b6a3bde 201 if (r < 0)
e609cd06 202 return r;
00f1261d 203
e609cd06
YW
204 SET_FOREACH(a, addresses) {
205 _cleanup_(address_freep) Address *address = NULL;
3b6a3bde 206
e609cd06
YW
207 r = address_new(&address);
208 if (r < 0)
209 return -ENOMEM;
3b6a3bde 210
e609cd06
YW
211 address->source = NETWORK_CONFIG_SOURCE_STATIC;
212 address->family = AF_INET6;
213 address->in_addr.in6 = *a;
2ac41679 214 address->prefixlen = p->prefixlen;
e609cd06
YW
215 address->route_metric = p->route_metric;
216
217 r = link_request_static_address(link, TAKE_PTR(address), true);
218 if (r < 0)
219 return r;
220 }
3b6a3bde
YW
221 }
222
223 return 0;
224}
225
2ac41679
YW
226static uint32_t usec_to_lifetime(usec_t usec) {
227 uint64_t t;
228
229 if (usec == USEC_INFINITY)
230 return UINT32_MAX;
231
232 t = DIV_ROUND_UP(usec, USEC_PER_SEC);
233 if (t >= UINT32_MAX)
234 return UINT32_MAX;
235
236 return (uint32_t) t;
237}
238
239static int radv_set_prefix(Link *link, Prefix *prefix) {
240 _cleanup_(sd_radv_prefix_unrefp) sd_radv_prefix *p = NULL;
241 int r;
242
243 assert(link);
244 assert(link->radv);
245 assert(prefix);
246
247 r = sd_radv_prefix_new(&p);
248 if (r < 0)
249 return r;
250
251 r = sd_radv_prefix_set_prefix(p, &prefix->prefix, prefix->prefixlen);
252 if (r < 0)
253 return r;
254
95e104e0 255 r = sd_radv_prefix_set_preferred_lifetime(p, prefix->preferred_lifetime, USEC_INFINITY);
2ac41679
YW
256 if (r < 0)
257 return r;
258
95e104e0 259 r = sd_radv_prefix_set_valid_lifetime(p, prefix->valid_lifetime, USEC_INFINITY);
2ac41679
YW
260 if (r < 0)
261 return r;
262
263 r = sd_radv_prefix_set_onlink(p, prefix->onlink);
264 if (r < 0)
265 return r;
266
267 r = sd_radv_prefix_set_address_autoconfiguration(p, prefix->address_auto_configuration);
268 if (r < 0)
269 return r;
270
95e104e0 271 return sd_radv_add_prefix(link->radv, p);
2ac41679
YW
272}
273
274static int radv_set_route_prefix(Link *link, RoutePrefix *prefix) {
275 _cleanup_(sd_radv_route_prefix_unrefp) sd_radv_route_prefix *p = NULL;
276 int r;
277
278 assert(link);
279 assert(link->radv);
280 assert(prefix);
281
282 r = sd_radv_route_prefix_new(&p);
283 if (r < 0)
284 return r;
285
286 r = sd_radv_route_prefix_set_prefix(p, &prefix->prefix, prefix->prefixlen);
287 if (r < 0)
288 return r;
289
95e104e0 290 r = sd_radv_route_prefix_set_lifetime(p, prefix->lifetime, USEC_INFINITY);
2ac41679
YW
291 if (r < 0)
292 return r;
293
95e104e0 294 return sd_radv_add_route_prefix(link->radv, p);
2ac41679
YW
295}
296
0943b3b7
YW
297static int network_get_ipv6_dns(Network *network, struct in6_addr **ret_addresses, size_t *ret_size) {
298 _cleanup_free_ struct in6_addr *addresses = NULL;
299 size_t n_addresses = 0;
6e849e95 300
0943b3b7
YW
301 assert(network);
302 assert(ret_addresses);
303 assert(ret_size);
6e849e95 304
0943b3b7
YW
305 for (size_t i = 0; i < network->n_dns; i++) {
306 union in_addr_union *addr;
6e849e95 307
0943b3b7
YW
308 if (network->dns[i]->family != AF_INET6)
309 continue;
6e849e95 310
0943b3b7 311 addr = &network->dns[i]->address;
6e849e95 312
0943b3b7
YW
313 if (in_addr_is_null(AF_INET6, addr) ||
314 in_addr_is_link_local(AF_INET6, addr) ||
315 in_addr_is_localhost(AF_INET6, addr))
316 continue;
317
318 if (!GREEDY_REALLOC(addresses, n_addresses + 1))
319 return -ENOMEM;
320
321 addresses[n_addresses++] = addr->in6;
c9778516 322 }
6e849e95 323
0943b3b7
YW
324 *ret_addresses = TAKE_PTR(addresses);
325 *ret_size = n_addresses;
6e849e95 326
0943b3b7 327 return n_addresses;
6e849e95
PF
328}
329
0943b3b7
YW
330static int radv_set_dns(Link *link, Link *uplink) {
331 _cleanup_free_ struct in6_addr *dns = NULL;
0943b3b7 332 size_t n_dns;
c9778516 333 int r;
6e849e95 334
0943b3b7 335 if (!link->network->router_emit_dns)
6e849e95 336 return 0;
6e849e95 337
0943b3b7
YW
338 if (link->network->router_dns) {
339 struct in6_addr *p;
6e849e95 340
0943b3b7
YW
341 dns = new(struct in6_addr, link->network->n_router_dns);
342 if (!dns)
343 return -ENOMEM;
6e849e95 344
0943b3b7
YW
345 p = dns;
346 for (size_t i = 0; i < link->network->n_router_dns; i++)
347 if (in6_addr_is_null(&link->network->router_dns[i])) {
348 if (in6_addr_is_set(&link->ipv6ll_address))
349 *(p++) = link->ipv6ll_address;
350 } else
351 *(p++) = link->network->router_dns[i];
6e849e95 352
0943b3b7 353 n_dns = p - dns;
a8d4a210 354
0943b3b7
YW
355 goto set_dns;
356 }
6e849e95 357
0943b3b7
YW
358 r = network_get_ipv6_dns(link->network, &dns, &n_dns);
359 if (r > 0)
360 goto set_dns;
6e849e95 361
0943b3b7
YW
362 if (uplink) {
363 assert(uplink->network);
364
365 r = network_get_ipv6_dns(uplink->network, &dns, &n_dns);
366 if (r > 0)
367 goto set_dns;
6e849e95
PF
368 }
369
6e849e95 370 return 0;
bd6379ec 371
0943b3b7
YW
372set_dns:
373 return sd_radv_set_rdnss(link->radv,
165a654e 374 usec_to_lifetime(link->network->router_dns_lifetime_usec),
0943b3b7
YW
375 dns, n_dns);
376}
bd6379ec 377
0943b3b7 378static int radv_set_domains(Link *link, Link *uplink) {
0943b3b7 379 _cleanup_free_ char **s = NULL; /* just free() because the strings are owned by the set */
165a654e 380 OrderedSet *search_domains;
bd6379ec 381
0943b3b7 382 if (!link->network->router_emit_domains)
bd6379ec 383 return 0;
bd6379ec 384
0943b3b7 385 search_domains = link->network->router_search_domains;
bd6379ec 386
0943b3b7
YW
387 if (search_domains)
388 goto set_domains;
0e1fb1d0 389
0943b3b7
YW
390 search_domains = link->network->search_domains;
391 if (search_domains)
392 goto set_domains;
0e1fb1d0 393
0943b3b7
YW
394 if (uplink) {
395 assert(uplink->network);
0e1fb1d0 396
0943b3b7
YW
397 search_domains = uplink->network->search_domains;
398 if (search_domains)
399 goto set_domains;
0e1fb1d0
YW
400 }
401
0e1fb1d0 402 return 0;
0e1fb1d0 403
0943b3b7
YW
404set_domains:
405 s = ordered_set_get_strv(search_domains);
406 if (!s)
407 return log_oom();
203d4df5 408
0943b3b7 409 return sd_radv_set_dnssl(link->radv,
165a654e 410 usec_to_lifetime(link->network->router_dns_lifetime_usec),
0943b3b7 411 s);
203d4df5 412
0943b3b7 413}
203d4df5 414
0943b3b7 415static int radv_find_uplink(Link *link, Link **ret) {
f6032ff3
YW
416 int r;
417
0943b3b7 418 assert(link);
203d4df5 419
0943b3b7
YW
420 if (link->network->router_uplink_name)
421 return link_get_by_name(link->manager, link->network->router_uplink_name, ret);
203d4df5 422
0943b3b7
YW
423 if (link->network->router_uplink_index > 0)
424 return link_get_by_index(link->manager, link->network->router_uplink_index, ret);
425
426 if (link->network->router_uplink_index == UPLINK_INDEX_AUTO) {
a27588d4
YW
427 if (link_dhcp_pd_is_enabled(link))
428 r = dhcp_pd_find_uplink(link, ret); /* When DHCP-PD is enabled, use its uplink. */
f6032ff3
YW
429 else
430 r = manager_find_uplink(link->manager, AF_INET6, link, ret);
431 if (r < 0)
432 /* It is not necessary to propagate error in automatic selection. */
0943b3b7 433 *ret = NULL;
c9778516
YW
434 return 0;
435 }
203d4df5 436
0943b3b7 437 *ret = NULL;
203d4df5
SS
438 return 0;
439}
440
0943b3b7 441static int radv_configure(Link *link) {
0943b3b7
YW
442 Link *uplink = NULL;
443 RoutePrefix *q;
444 Prefix *p;
203d4df5
SS
445 int r;
446
0943b3b7
YW
447 assert(link);
448 assert(link->network);
203d4df5 449
0943b3b7
YW
450 if (link->radv)
451 return -EBUSY;
452
453 r = sd_radv_new(&link->radv);
203d4df5 454 if (r < 0)
0943b3b7 455 return r;
203d4df5 456
0943b3b7
YW
457 r = sd_radv_attach_event(link->radv, link->manager->event, 0);
458 if (r < 0)
459 return r;
203d4df5 460
0943b3b7
YW
461 r = sd_radv_set_mac(link->radv, &link->hw_addr.ether);
462 if (r < 0)
463 return r;
203d4df5 464
0943b3b7
YW
465 r = sd_radv_set_ifindex(link->radv, link->ifindex);
466 if (r < 0)
467 return r;
203d4df5 468
0943b3b7
YW
469 r = sd_radv_set_managed_information(link->radv, link->network->router_managed);
470 if (r < 0)
471 return r;
203d4df5 472
0943b3b7
YW
473 r = sd_radv_set_other_information(link->radv, link->network->router_other_information);
474 if (r < 0)
475 return r;
c555a358 476
7003b114 477 r = sd_radv_set_router_lifetime(link->radv, link->network->router_lifetime_usec);
0943b3b7
YW
478 if (r < 0)
479 return r;
c555a358 480
7003b114 481 if (link->network->router_lifetime_usec > 0) {
0943b3b7
YW
482 r = sd_radv_set_preference(link->radv, link->network->router_preference);
483 if (r < 0)
484 return r;
485 }
c555a358 486
0943b3b7 487 HASHMAP_FOREACH(p, link->network->prefixes_by_section) {
2ac41679
YW
488 r = radv_set_prefix(link, p);
489 if (r < 0 && r != -EEXIST)
0943b3b7
YW
490 return r;
491 }
c555a358 492
0943b3b7 493 HASHMAP_FOREACH(q, link->network->route_prefixes_by_section) {
2ac41679
YW
494 r = radv_set_route_prefix(link, q);
495 if (r < 0 && r != -EEXIST)
0943b3b7
YW
496 return r;
497 }
c555a358 498
0943b3b7 499 (void) radv_find_uplink(link, &uplink);
c555a358 500
0943b3b7
YW
501 r = radv_set_dns(link, uplink);
502 if (r < 0)
503 return log_link_debug_errno(link, r, "Could not set RA DNS: %m");
c555a358 504
0943b3b7
YW
505 r = radv_set_domains(link, uplink);
506 if (r < 0)
507 return log_link_debug_errno(link, r, "Could not set RA Domains: %m");
c555a358 508
0943b3b7 509 return 0;
c555a358
PF
510}
511
0943b3b7
YW
512int radv_update_mac(Link *link) {
513 bool restart;
c555a358
PF
514 int r;
515
0943b3b7 516 assert(link);
c555a358 517
0943b3b7
YW
518 if (!link->radv)
519 return 0;
fd3ef936 520
0943b3b7 521 restart = sd_radv_is_running(link->radv);
c555a358 522
0943b3b7
YW
523 r = sd_radv_stop(link->radv);
524 if (r < 0)
525 return r;
fd3ef936 526
0943b3b7
YW
527 r = sd_radv_set_mac(link->radv, &link->hw_addr.ether);
528 if (r < 0)
529 return r;
c555a358 530
0943b3b7
YW
531 if (restart) {
532 r = sd_radv_start(link->radv);
533 if (r < 0)
534 return r;
c555a358
PF
535 }
536
0943b3b7
YW
537 return 0;
538}
c555a358 539
0943b3b7
YW
540static int radv_is_ready_to_configure(Link *link) {
541 bool needs_uplink = false;
542 int r;
c555a358 543
0943b3b7
YW
544 assert(link);
545 assert(link->network);
349a981d 546
0943b3b7
YW
547 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
548 return false;
c555a358 549
0943b3b7
YW
550 if (in6_addr_is_null(&link->ipv6ll_address))
551 return false;
c555a358 552
218a8502
YW
553 if (link->hw_addr.length != ETH_ALEN || hw_addr_is_null(&link->hw_addr))
554 return false;
555
0943b3b7
YW
556 if (link->network->router_emit_dns && !link->network->router_dns) {
557 _cleanup_free_ struct in6_addr *dns = NULL;
558 size_t n_dns;
c555a358 559
0943b3b7
YW
560 r = network_get_ipv6_dns(link->network, &dns, &n_dns);
561 if (r < 0)
562 return r;
c555a358 563
0943b3b7
YW
564 needs_uplink = r == 0;
565 }
c555a358 566
0943b3b7
YW
567 if (link->network->router_emit_domains &&
568 !link->network->router_search_domains &&
569 !link->network->search_domains)
570 needs_uplink = true;
571
572 if (needs_uplink) {
573 Link *uplink = NULL;
c555a358 574
0943b3b7
YW
575 if (radv_find_uplink(link, &uplink) < 0)
576 return false;
c555a358 577
0943b3b7
YW
578 if (uplink && !uplink->network)
579 return false;
580 }
c555a358 581
0943b3b7
YW
582 return true;
583}
c555a358 584
0943b3b7
YW
585int request_process_radv(Request *req) {
586 Link *link;
587 int r;
349a981d 588
0943b3b7
YW
589 assert(req);
590 assert(req->link);
591 assert(req->type == REQUEST_TYPE_RADV);
c555a358 592
0943b3b7 593 link = req->link;
c555a358 594
0943b3b7
YW
595 r = radv_is_ready_to_configure(link);
596 if (r <= 0)
597 return r;
5e2a51d5 598
0943b3b7
YW
599 r = radv_configure(link);
600 if (r < 0)
601 return log_link_warning_errno(link, r, "Failed to configure IPv6 Router Advertisement engine: %m");
c555a358 602
0943b3b7 603 if (link_has_carrier(link)) {
0f96a823 604 r = radv_start(link);
0943b3b7
YW
605 if (r < 0)
606 return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
607 }
608
609 log_link_debug(link, "IPv6 Router Advertisement engine is configured%s.",
610 link_has_carrier(link) ? " and started." : "");
611 return 1;
c555a358
PF
612}
613
0943b3b7
YW
614int link_request_radv(Link *link) {
615 int r;
63295b42 616
0943b3b7 617 assert(link);
63295b42 618
0943b3b7
YW
619 if (!link_radv_enabled(link))
620 return 0;
63295b42 621
0943b3b7 622 if (link->radv)
63295b42 623 return 0;
63295b42 624
0943b3b7
YW
625 r = link_queue_request(link, REQUEST_TYPE_RADV, NULL, false, NULL, NULL, NULL);
626 if (r < 0)
627 return log_link_warning_errno(link, r, "Failed to request configuring of the IPv6 Router Advertisement engine: %m");
628
629 log_link_debug(link, "Requested configuring of the IPv6 Router Advertisement engine.");
63295b42
YW
630 return 0;
631}
632
0f96a823
YW
633int radv_start(Link *link) {
634 int r;
635
636 assert(link);
637 assert(link->network);
638
639 if (!link->radv)
640 return 0;
641
642 if (!link_has_carrier(link))
643 return 0;
644
645 if (in6_addr_is_null(&link->ipv6ll_address))
646 return 0;
647
648 if (sd_radv_is_running(link->radv))
649 return 0;
650
a27588d4
YW
651 if (link->network->dhcp_pd_announce) {
652 r = dhcp_request_prefix_delegation(link);
0f96a823 653 if (r < 0)
a27588d4 654 return log_link_debug_errno(link, r, "Failed to request DHCP delegated subnet prefix: %m");
0f96a823
YW
655 }
656
657 log_link_debug(link, "Starting IPv6 Router Advertisements");
658 return sd_radv_start(link->radv);
659}
660
0943b3b7
YW
661int radv_add_prefix(
662 Link *link,
663 const struct in6_addr *prefix,
664 uint8_t prefix_len,
16bc8635
YW
665 usec_t lifetime_preferred_usec,
666 usec_t lifetime_valid_usec) {
0943b3b7
YW
667
668 _cleanup_(sd_radv_prefix_unrefp) sd_radv_prefix *p = NULL;
95081e08 669 int r;
091214b6
PF
670
671 assert(link);
091214b6 672
0943b3b7
YW
673 if (!link->radv)
674 return 0;
bc9e40c9 675
0943b3b7 676 r = sd_radv_prefix_new(&p);
091214b6
PF
677 if (r < 0)
678 return r;
679
0943b3b7 680 r = sd_radv_prefix_set_prefix(p, prefix, prefix_len);
091214b6
PF
681 if (r < 0)
682 return r;
683
c9e2c2da 684 r = sd_radv_prefix_set_preferred_lifetime(p, RADV_DEFAULT_PREFERRED_LIFETIME_USEC, lifetime_preferred_usec);
091214b6
PF
685 if (r < 0)
686 return r;
687
c9e2c2da 688 r = sd_radv_prefix_set_valid_lifetime(p, RADV_DEFAULT_VALID_LIFETIME_USEC, lifetime_valid_usec);
091214b6
PF
689 if (r < 0)
690 return r;
691
95e104e0 692 r = sd_radv_add_prefix(link->radv, p);
0943b3b7 693 if (r < 0 && r != -EEXIST)
091214b6
PF
694 return r;
695
0943b3b7
YW
696 return 0;
697}
091214b6 698
2110040b
YW
699static int prefix_section_verify(Prefix *p) {
700 assert(p);
701
702 if (section_is_invalid(p->section))
703 return -EINVAL;
704
705 if (in6_addr_is_null(&p->prefix))
706 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
707 "%s: [IPv6Prefix] section without Prefix= field configured, "
708 "or specified prefix is the null address. "
709 "Ignoring [IPv6Prefix] section from line %u.",
710 p->section->filename, p->section->line);
711
712 if (p->prefixlen < 3 || p->prefixlen > 128)
713 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
714 "%s: Invalid prefix length %u is specified in [IPv6Prefix] section. "
715 "Valid range is 3…128. Ignoring [IPv6Prefix] section from line %u.",
716 p->section->filename, p->prefixlen, p->section->line);
717
718 if (p->prefixlen > 64) {
719 _cleanup_free_ char *str = NULL;
720
721 if (p->assign)
722 (void) in6_addr_prefix_to_string(&p->prefix, p->prefixlen, &str);
723
724 log_info("%s: Unusual prefix length %u (> 64) is specified in [IPv6Prefix] section from line %u%s%s.",
725 p->section->filename, p->prefixlen, p->section->line,
726 p->assign ? ", refusing to assign an address in " : "",
727 p->assign ? strna(str) : "");
728
729 p->assign = false;
730 }
731
732 if (p->valid_lifetime == 0)
733 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
734 "%s: The valid lifetime of prefix cannot be zero. "
735 "Ignoring [IPv6Prefix] section from line %u.",
736 p->section->filename, p->section->line);
737
738 if (p->preferred_lifetime > p->valid_lifetime)
739 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
740 "%s: The preferred lifetime %s is longer than the valid lifetime %s. "
741 "Ignoring [IPv6Prefix] section from line %u.",
742 p->section->filename,
743 FORMAT_TIMESPAN(p->preferred_lifetime, USEC_PER_SEC),
744 FORMAT_TIMESPAN(p->valid_lifetime, USEC_PER_SEC),
745 p->section->line);
746
747 return 0;
748}
749
0943b3b7 750void network_drop_invalid_prefixes(Network *network) {
2110040b 751 Prefix *p;
2075e596 752
0943b3b7
YW
753 assert(network);
754
2110040b
YW
755 HASHMAP_FOREACH(p, network->prefixes_by_section)
756 if (prefix_section_verify(p) < 0)
757 prefix_free(p);
758}
759
760static int route_prefix_section_verify(RoutePrefix *p) {
761 if (section_is_invalid(p->section))
762 return -EINVAL;
763
764 if (p->prefixlen > 128)
765 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
766 "%s: Invalid prefix length %u is specified in [IPv6RoutePrefix] section. "
767 "Valid range is 0…128. Ignoring [IPv6RoutePrefix] section from line %u.",
768 p->section->filename, p->prefixlen, p->section->line);
769
770 if (p->lifetime == 0)
771 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
772 "%s: The lifetime of route cannot be zero. "
773 "Ignoring [IPv6RoutePrefix] section from line %u.",
774 p->section->filename, p->section->line);
775
776 return 0;
0943b3b7
YW
777}
778
779void network_drop_invalid_route_prefixes(Network *network) {
2110040b 780 RoutePrefix *p;
0943b3b7
YW
781
782 assert(network);
783
2110040b
YW
784 HASHMAP_FOREACH(p, network->route_prefixes_by_section)
785 if (route_prefix_section_verify(p) < 0)
786 route_prefix_free(p);
0943b3b7
YW
787}
788
789int config_parse_prefix(
790 const char *unit,
791 const char *filename,
792 unsigned line,
793 const char *section,
794 unsigned section_line,
795 const char *lvalue,
796 int ltype,
797 const char *rvalue,
798 void *data,
799 void *userdata) {
800
0943b3b7 801 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
2ac41679
YW
802 Network *network = userdata;
803 union in_addr_union a;
0943b3b7
YW
804 int r;
805
806 assert(filename);
807 assert(section);
808 assert(lvalue);
809 assert(rvalue);
2ac41679 810 assert(userdata);
0943b3b7
YW
811
812 r = prefix_new_static(network, filename, section_line, &p);
091214b6 813 if (r < 0)
0943b3b7 814 return log_oom();
091214b6 815
2ac41679 816 r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->prefixlen);
0943b3b7 817 if (r < 0) {
2ac41679
YW
818 log_syntax(unit, LOG_WARNING, filename, line, r,
819 "Prefix is invalid, ignoring assignment: %s", rvalue);
0943b3b7 820 return 0;
8a08bbfc 821 }
cf72568a
YW
822
823 (void) in6_addr_mask(&a.in6, p->prefixlen);
2ac41679 824 p->prefix = a.in6;
203d4df5 825
2ac41679 826 TAKE_PTR(p);
0943b3b7
YW
827 return 0;
828}
a254fab2 829
2ac41679 830int config_parse_prefix_boolean(
0943b3b7
YW
831 const char *unit,
832 const char *filename,
833 unsigned line,
834 const char *section,
835 unsigned section_line,
836 const char *lvalue,
837 int ltype,
838 const char *rvalue,
839 void *data,
840 void *userdata) {
a254fab2 841
0943b3b7 842 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
2ac41679 843 Network *network = userdata;
0943b3b7
YW
844 int r;
845
846 assert(filename);
847 assert(section);
848 assert(lvalue);
849 assert(rvalue);
2ac41679 850 assert(userdata);
0943b3b7
YW
851
852 r = prefix_new_static(network, filename, section_line, &p);
a254fab2 853 if (r < 0)
0943b3b7
YW
854 return log_oom();
855
856 r = parse_boolean(rvalue);
857 if (r < 0) {
2ac41679
YW
858 log_syntax(unit, LOG_WARNING, filename, line, r,
859 "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
0943b3b7
YW
860 return 0;
861 }
862
863 if (streq(lvalue, "OnLink"))
2ac41679 864 p->onlink = r;
0943b3b7 865 else if (streq(lvalue, "AddressAutoconfiguration"))
2ac41679
YW
866 p->address_auto_configuration = r;
867 else if (streq(lvalue, "Assign"))
868 p->assign = r;
869 else
870 assert_not_reached();
a254fab2 871
2ac41679 872 TAKE_PTR(p);
fd3ef936 873 return 0;
091214b6 874}
ca5ad760 875
0943b3b7
YW
876int config_parse_prefix_lifetime(
877 const char *unit,
878 const char *filename,
879 unsigned line,
880 const char *section,
881 unsigned section_line,
882 const char *lvalue,
883 int ltype,
884 const char *rvalue,
885 void *data,
886 void *userdata) {
887
0943b3b7 888 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
2ac41679 889 Network *network = userdata;
0943b3b7 890 usec_t usec;
be9363cc
YW
891 int r;
892
0943b3b7
YW
893 assert(filename);
894 assert(section);
895 assert(lvalue);
896 assert(rvalue);
2ac41679 897 assert(userdata);
be9363cc 898
0943b3b7 899 r = prefix_new_static(network, filename, section_line, &p);
a391901e 900 if (r < 0)
0943b3b7 901 return log_oom();
be9363cc 902
0943b3b7
YW
903 r = parse_sec(rvalue, &usec);
904 if (r < 0) {
2ac41679
YW
905 log_syntax(unit, LOG_WARNING, filename, line, r,
906 "Lifetime is invalid, ignoring assignment: %s", rvalue);
0943b3b7
YW
907 return 0;
908 }
be9363cc 909
2ac41679
YW
910 if (usec != USEC_INFINITY && DIV_ROUND_UP(usec, USEC_PER_SEC) >= UINT32_MAX) {
911 log_syntax(unit, LOG_WARNING, filename, line, 0,
912 "Lifetime is too long, ignoring assignment: %s", rvalue);
0943b3b7 913 return 0;
be9363cc
YW
914 }
915
2ac41679
YW
916 if (streq(lvalue, "PreferredLifetimeSec"))
917 p->preferred_lifetime = usec;
918 else if (streq(lvalue, "ValidLifetimeSec"))
919 p->valid_lifetime = usec;
920 else
921 assert_not_reached();
63295b42 922
2ac41679 923 TAKE_PTR(p);
0943b3b7 924 return 0;
a254fab2
YW
925}
926
0943b3b7
YW
927int config_parse_prefix_metric(
928 const char *unit,
929 const char *filename,
930 unsigned line,
931 const char *section,
932 unsigned section_line,
933 const char *lvalue,
934 int ltype,
935 const char *rvalue,
936 void *data,
937 void *userdata) {
a254fab2 938
0943b3b7 939 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
2ac41679 940 Network *network = userdata;
0943b3b7 941 int r;
a254fab2 942
0943b3b7
YW
943 assert(filename);
944 assert(section);
945 assert(lvalue);
946 assert(rvalue);
2ac41679 947 assert(userdata);
a254fab2 948
0943b3b7 949 r = prefix_new_static(network, filename, section_line, &p);
a254fab2 950 if (r < 0)
0943b3b7 951 return log_oom();
a254fab2 952
0943b3b7
YW
953 r = safe_atou32(rvalue, &p->route_metric);
954 if (r < 0) {
955 log_syntax(unit, LOG_WARNING, filename, line, r,
956 "Failed to parse %s=, ignoring assignment: %s",
957 lvalue, rvalue);
958 return 0;
a254fab2
YW
959 }
960
0943b3b7 961 TAKE_PTR(p);
0943b3b7 962 return 0;
a254fab2
YW
963}
964
e609cd06
YW
965int config_parse_prefix_token(
966 const char *unit,
967 const char *filename,
968 unsigned line,
969 const char *section,
970 unsigned section_line,
971 const char *lvalue,
972 int ltype,
973 const char *rvalue,
974 void *data,
975 void *userdata) {
976
977 _cleanup_(prefix_free_or_set_invalidp) Prefix *p = NULL;
978 Network *network = userdata;
979 int r;
980
981 assert(filename);
982 assert(section);
983 assert(lvalue);
984 assert(rvalue);
985 assert(userdata);
986
987 r = prefix_new_static(network, filename, section_line, &p);
988 if (r < 0)
989 return log_oom();
990
991 r = config_parse_address_generation_type(unit, filename, line, section, section_line,
992 lvalue, ltype, rvalue, &p->tokens, userdata);
993 if (r < 0)
994 return r;
995
996 TAKE_PTR(p);
997 return 0;
998}
999
0943b3b7
YW
1000int config_parse_route_prefix(
1001 const char *unit,
1002 const char *filename,
1003 unsigned line,
1004 const char *section,
1005 unsigned section_line,
1006 const char *lvalue,
1007 int ltype,
1008 const char *rvalue,
1009 void *data,
1010 void *userdata) {
1011
0943b3b7 1012 _cleanup_(route_prefix_free_or_set_invalidp) RoutePrefix *p = NULL;
2ac41679
YW
1013 Network *network = userdata;
1014 union in_addr_union a;
a254fab2
YW
1015 int r;
1016
0943b3b7
YW
1017 assert(filename);
1018 assert(section);
1019 assert(lvalue);
1020 assert(rvalue);
2ac41679 1021 assert(userdata);
a254fab2 1022
0943b3b7
YW
1023 r = route_prefix_new_static(network, filename, section_line, &p);
1024 if (r < 0)
1025 return log_oom();
1026
2ac41679 1027 r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->prefixlen);
0943b3b7 1028 if (r < 0) {
2ac41679
YW
1029 log_syntax(unit, LOG_WARNING, filename, line, r,
1030 "Route prefix is invalid, ignoring assignment: %s", rvalue);
a254fab2 1031 return 0;
0943b3b7 1032 }
cf72568a
YW
1033
1034 (void) in6_addr_mask(&a.in6, p->prefixlen);
2ac41679 1035 p->prefix = a.in6;
a254fab2 1036
2ac41679 1037 TAKE_PTR(p);
a254fab2
YW
1038 return 0;
1039}
1040
0943b3b7
YW
1041int config_parse_route_prefix_lifetime(
1042 const char *unit,
1043 const char *filename,
1044 unsigned line,
1045 const char *section,
1046 unsigned section_line,
1047 const char *lvalue,
1048 int ltype,
1049 const char *rvalue,
1050 void *data,
1051 void *userdata) {
a8d4a210 1052
0943b3b7 1053 _cleanup_(route_prefix_free_or_set_invalidp) RoutePrefix *p = NULL;
2ac41679 1054 Network *network = userdata;
0943b3b7 1055 usec_t usec;
1d596fde
YW
1056 int r;
1057
0943b3b7
YW
1058 assert(filename);
1059 assert(section);
1060 assert(lvalue);
1061 assert(rvalue);
2ac41679 1062 assert(userdata);
1d596fde 1063
0943b3b7 1064 r = route_prefix_new_static(network, filename, section_line, &p);
1d596fde 1065 if (r < 0)
0943b3b7 1066 return log_oom();
1d596fde 1067
0943b3b7
YW
1068 r = parse_sec(rvalue, &usec);
1069 if (r < 0) {
1070 log_syntax(unit, LOG_WARNING, filename, line, r,
1071 "Route lifetime is invalid, ignoring assignment: %s", rvalue);
1072 return 0;
1073 }
1d596fde 1074
2ac41679
YW
1075 if (usec != USEC_INFINITY && DIV_ROUND_UP(usec, USEC_PER_SEC) >= UINT32_MAX) {
1076 log_syntax(unit, LOG_WARNING, filename, line, 0,
1077 "Lifetime is too long, ignoring assignment: %s", rvalue);
0943b3b7
YW
1078 return 0;
1079 }
1d596fde 1080
2ac41679 1081 p->lifetime = usec;
1d596fde 1082
2ac41679 1083 TAKE_PTR(p);
1d596fde
YW
1084 return 0;
1085}
1086
ca5ad760
YW
1087int config_parse_radv_dns(
1088 const char *unit,
1089 const char *filename,
1090 unsigned line,
1091 const char *section,
1092 unsigned section_line,
1093 const char *lvalue,
1094 int ltype,
1095 const char *rvalue,
1096 void *data,
1097 void *userdata) {
1098
1099 Network *n = data;
ca5ad760
YW
1100 int r;
1101
1102 assert(filename);
1103 assert(lvalue);
1104 assert(rvalue);
1105
a3c1a949
YW
1106 if (isempty(rvalue)) {
1107 n->n_router_dns = 0;
1108 n->router_dns = mfree(n->router_dns);
1109 return 0;
1110 }
1111
d96edb2c 1112 for (const char *p = rvalue;;) {
ca5ad760
YW
1113 _cleanup_free_ char *w = NULL;
1114 union in_addr_union a;
1115
1116 r = extract_first_word(&p, &w, NULL, 0);
1117 if (r == -ENOMEM)
1118 return log_oom();
1119 if (r < 0) {
d96edb2c 1120 log_syntax(unit, LOG_WARNING, filename, line, r,
ca5ad760
YW
1121 "Failed to extract word, ignoring: %s", rvalue);
1122 return 0;
1123 }
1124 if (r == 0)
d96edb2c 1125 return 0;
ca5ad760 1126
fd3ef936
YW
1127 if (streq(w, "_link_local"))
1128 a = IN_ADDR_NULL;
1129 else {
1130 r = in_addr_from_string(AF_INET6, w, &a);
1131 if (r < 0) {
d96edb2c 1132 log_syntax(unit, LOG_WARNING, filename, line, r,
fd3ef936
YW
1133 "Failed to parse DNS server address, ignoring: %s", w);
1134 continue;
1135 }
ca5ad760 1136
fd3ef936 1137 if (in_addr_is_null(AF_INET6, &a)) {
d96edb2c 1138 log_syntax(unit, LOG_WARNING, filename, line, 0,
fd3ef936
YW
1139 "DNS server address is null, ignoring: %s", w);
1140 continue;
1141 }
1142 }
ca5ad760 1143
fd3ef936
YW
1144 struct in6_addr *m;
1145 m = reallocarray(n->router_dns, n->n_router_dns + 1, sizeof(struct in6_addr));
1146 if (!m)
1147 return log_oom();
ca5ad760 1148
fd3ef936
YW
1149 m[n->n_router_dns++] = a.in6;
1150 n->router_dns = m;
ca5ad760 1151 }
ca5ad760
YW
1152}
1153
1154int config_parse_radv_search_domains(
1155 const char *unit,
1156 const char *filename,
1157 unsigned line,
1158 const char *section,
1159 unsigned section_line,
1160 const char *lvalue,
1161 int ltype,
1162 const char *rvalue,
1163 void *data,
1164 void *userdata) {
1165
1166 Network *n = data;
ca5ad760
YW
1167 int r;
1168
1169 assert(filename);
1170 assert(lvalue);
1171 assert(rvalue);
1172
a3c1a949
YW
1173 if (isempty(rvalue)) {
1174 n->router_search_domains = ordered_set_free(n->router_search_domains);
1175 return 0;
1176 }
1177
d96edb2c 1178 for (const char *p = rvalue;;) {
ca5ad760
YW
1179 _cleanup_free_ char *w = NULL, *idna = NULL;
1180
1181 r = extract_first_word(&p, &w, NULL, 0);
1182 if (r == -ENOMEM)
1183 return log_oom();
1184 if (r < 0) {
d96edb2c 1185 log_syntax(unit, LOG_WARNING, filename, line, r,
ca5ad760
YW
1186 "Failed to extract word, ignoring: %s", rvalue);
1187 return 0;
1188 }
1189 if (r == 0)
d96edb2c 1190 return 0;
ca5ad760
YW
1191
1192 r = dns_name_apply_idna(w, &idna);
1193 if (r < 0) {
d96edb2c 1194 log_syntax(unit, LOG_WARNING, filename, line, r,
ca5ad760
YW
1195 "Failed to apply IDNA to domain name '%s', ignoring: %m", w);
1196 continue;
1197 } else if (r == 0)
1198 /* transfer ownership to simplify subsequent operations */
1199 idna = TAKE_PTR(w);
1200
5e276772 1201 r = ordered_set_ensure_allocated(&n->router_search_domains, &string_hash_ops_free);
ca5ad760 1202 if (r < 0)
d96edb2c 1203 return log_oom();
ca5ad760
YW
1204
1205 r = ordered_set_consume(n->router_search_domains, TAKE_PTR(idna));
1206 if (r < 0)
d96edb2c 1207 return log_oom();
ca5ad760 1208 }
ca5ad760
YW
1209}
1210
1211static const char * const radv_prefix_delegation_table[_RADV_PREFIX_DELEGATION_MAX] = {
a8d4a210 1212 [RADV_PREFIX_DELEGATION_NONE] = "no",
ca5ad760 1213 [RADV_PREFIX_DELEGATION_STATIC] = "static",
a8d4a210
YW
1214 [RADV_PREFIX_DELEGATION_DHCP6] = "dhcpv6",
1215 [RADV_PREFIX_DELEGATION_BOTH] = "yes",
ca5ad760
YW
1216};
1217
1218DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
1219 radv_prefix_delegation,
1220 RADVPrefixDelegation,
1221 RADV_PREFIX_DELEGATION_BOTH);
1222
27ff0490
YW
1223int config_parse_router_prefix_delegation(
1224 const char *unit,
1225 const char *filename,
1226 unsigned line,
1227 const char *section,
1228 unsigned section_line,
1229 const char *lvalue,
1230 int ltype,
1231 const char *rvalue,
1232 void *data,
1233 void *userdata) {
1234
1235 RADVPrefixDelegation val, *ra = data;
1236 int r;
1237
1238 assert(filename);
1239 assert(lvalue);
1240 assert(rvalue);
1241 assert(data);
1242
1243 if (streq(lvalue, "IPv6SendRA")) {
1244 r = parse_boolean(rvalue);
1245 if (r < 0) {
1246 log_syntax(unit, LOG_WARNING, filename, line, r,
1247 "Invalid %s= setting, ignoring assignment: %s", lvalue, rvalue);
1248 return 0;
1249 }
1250
1251 /* When IPv6SendRA= is enabled, only static prefixes are sent by default, and users
1252 * need to explicitly enable DHCPv6PrefixDelegation=. */
1253 *ra = r ? RADV_PREFIX_DELEGATION_STATIC : RADV_PREFIX_DELEGATION_NONE;
1254 return 0;
1255 }
1256
1257 /* For backward compatibility */
1258 val = radv_prefix_delegation_from_string(rvalue);
1259 if (val < 0) {
b98680b2 1260 log_syntax(unit, LOG_WARNING, filename, line, val,
27ff0490
YW
1261 "Invalid %s= setting, ignoring assignment: %s", lvalue, rvalue);
1262 return 0;
1263 }
1264
1265 *ra = val;
1266 return 0;
1267}
a8d4a210 1268
4f1ac4a3
YW
1269int config_parse_router_lifetime(
1270 const char *unit,
1271 const char *filename,
1272 unsigned line,
1273 const char *section,
1274 unsigned section_line,
1275 const char *lvalue,
1276 int ltype,
1277 const char *rvalue,
1278 void *data,
1279 void *userdata) {
1280
1281 usec_t usec, *lifetime = data;
1282 int r;
1283
1284 assert(filename);
1285 assert(section);
1286 assert(lvalue);
1287 assert(rvalue);
1288 assert(data);
1289
1290 if (isempty(rvalue)) {
1291 *lifetime = RADV_DEFAULT_ROUTER_LIFETIME_USEC;
1292 return 0;
1293 }
1294
1295 r = parse_sec(rvalue, &usec);
1296 if (r < 0) {
1297 log_syntax(unit, LOG_WARNING, filename, line, r,
1298 "Failed to parse router lifetime, ignoring assignment: %s", rvalue);
1299 return 0;
1300 }
1301 if (usec > 0) {
1302 if (usec < RADV_MIN_ROUTER_LIFETIME_USEC) {
1303 log_syntax(unit, LOG_WARNING, filename, line, 0,
1304 "Router lifetime %s is too short, using %s.",
1305 FORMAT_TIMESPAN(usec, USEC_PER_SEC),
1306 FORMAT_TIMESPAN(RADV_MIN_ROUTER_LIFETIME_USEC, USEC_PER_SEC));
1307 usec = RADV_MIN_ROUTER_LIFETIME_USEC;
1308 } else if (usec > RADV_MAX_ROUTER_LIFETIME_USEC) {
1309 log_syntax(unit, LOG_WARNING, filename, line, 0,
1310 "Router lifetime %s is too large, using %s.",
1311 FORMAT_TIMESPAN(usec, USEC_PER_SEC),
1312 FORMAT_TIMESPAN(RADV_MAX_ROUTER_LIFETIME_USEC, USEC_PER_SEC));
1313 usec = RADV_MAX_ROUTER_LIFETIME_USEC;
1314 }
1315 }
1316
1317 *lifetime = usec;
1318 return 0;
1319}
1320
a8d4a210
YW
1321int config_parse_router_preference(
1322 const char *unit,
1323 const char *filename,
1324 unsigned line,
1325 const char *section,
1326 unsigned section_line,
1327 const char *lvalue,
1328 int ltype,
1329 const char *rvalue,
1330 void *data,
1331 void *userdata) {
1332
ca5ad760
YW
1333 Network *network = userdata;
1334
1335 assert(filename);
1336 assert(section);
1337 assert(lvalue);
1338 assert(rvalue);
1339 assert(data);
1340
1341 if (streq(rvalue, "high"))
1342 network->router_preference = SD_NDISC_PREFERENCE_HIGH;
1343 else if (STR_IN_SET(rvalue, "medium", "normal", "default"))
1344 network->router_preference = SD_NDISC_PREFERENCE_MEDIUM;
1345 else if (streq(rvalue, "low"))
1346 network->router_preference = SD_NDISC_PREFERENCE_LOW;
1347 else
d96edb2c
YW
1348 log_syntax(unit, LOG_WARNING, filename, line, 0,
1349 "Invalid router preference, ignoring assignment: %s", rvalue);
ca5ad760
YW
1350
1351 return 0;
1352}