]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-ndisc.c
tree-wide: make hash_ops typesafe
[thirdparty/systemd.git] / src / network / networkd-ndisc.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2014 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <netinet/icmp6.h>
7 #include <arpa/inet.h>
8
9 #include "sd-ndisc.h"
10
11 #include "networkd-ndisc.h"
12 #include "networkd-route.h"
13 #include "strv.h"
14
15 #define NDISC_DNSSL_MAX 64U
16 #define NDISC_RDNSS_MAX 64U
17 #define NDISC_PREFIX_LFT_MIN 7200U
18
19 static int ndisc_route_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
20 Link *link = userdata;
21 int r;
22
23 assert(link);
24 assert(link->ndisc_messages > 0);
25
26 link->ndisc_messages--;
27
28 r = sd_netlink_message_get_errno(m);
29 if (r < 0 && r != -EEXIST)
30 log_link_error_errno(link, r, "Could not set NDisc route or address: %m");
31
32 if (link->ndisc_messages == 0) {
33 link->ndisc_configured = true;
34 link_check_ready(link);
35 }
36
37 return 1;
38 }
39
40 static int ndisc_router_process_default(Link *link, sd_ndisc_router *rt) {
41 _cleanup_(route_freep) Route *route = NULL;
42 struct in6_addr gateway;
43 uint16_t lifetime;
44 unsigned preference;
45 uint32_t mtu;
46 usec_t time_now;
47 int r;
48 Address *address;
49 Iterator i;
50
51 assert(link);
52 assert(rt);
53
54 r = sd_ndisc_router_get_lifetime(rt, &lifetime);
55 if (r < 0)
56 return log_link_warning_errno(link, r, "Failed to get gateway address from RA: %m");
57
58 if (lifetime == 0) /* not a default router */
59 return 0;
60
61 r = sd_ndisc_router_get_address(rt, &gateway);
62 if (r < 0)
63 return log_link_warning_errno(link, r, "Failed to get gateway address from RA: %m");
64
65 SET_FOREACH(address, link->addresses, i)
66 if (!memcmp(&gateway, &address->in_addr.in6, sizeof(address->in_addr.in6))) {
67 char buffer[INET6_ADDRSTRLEN];
68
69 log_link_debug(link, "No NDisc route added, gateway %s matches local address",
70 inet_ntop(AF_INET6,
71 &address->in_addr.in6,
72 buffer, sizeof(buffer)));
73 return 0;
74 }
75
76 SET_FOREACH(address, link->addresses_foreign, i)
77 if (!memcmp(&gateway, &address->in_addr.in6, sizeof(address->in_addr.in6))) {
78 char buffer[INET6_ADDRSTRLEN];
79
80 log_link_debug(link, "No NDisc route added, gateway %s matches local address",
81 inet_ntop(AF_INET6,
82 &address->in_addr.in6,
83 buffer, sizeof(buffer)));
84 return 0;
85 }
86
87 r = sd_ndisc_router_get_preference(rt, &preference);
88 if (r < 0)
89 return log_link_warning_errno(link, r, "Failed to get default router preference from RA: %m");
90
91 r = sd_ndisc_router_get_timestamp(rt, clock_boottime_or_monotonic(), &time_now);
92 if (r < 0)
93 return log_link_warning_errno(link, r, "Failed to get RA timestamp: %m");
94
95 r = sd_ndisc_router_get_mtu(rt, &mtu);
96 if (r == -ENODATA)
97 mtu = 0;
98 else if (r < 0)
99 return log_link_warning_errno(link, r, "Failed to get default router MTU from RA: %m");
100
101 r = route_new(&route);
102 if (r < 0)
103 return log_link_error_errno(link, r, "Could not allocate route: %m");
104
105 route->family = AF_INET6;
106 route->table = link->network->ipv6_accept_ra_route_table;
107 route->priority = link->network->dhcp_route_metric;
108 route->protocol = RTPROT_RA;
109 route->pref = preference;
110 route->gw.in6 = gateway;
111 route->lifetime = time_now + lifetime * USEC_PER_SEC;
112 route->mtu = mtu;
113
114 r = route_configure(route, link, ndisc_route_handler);
115 if (r < 0) {
116 log_link_warning_errno(link, r, "Could not set default route: %m");
117 link_enter_failed(link);
118 return r;
119 }
120
121 link->ndisc_messages++;
122
123 return 0;
124 }
125
126 static int ndisc_router_process_autonomous_prefix(Link *link, sd_ndisc_router *rt) {
127 _cleanup_(address_freep) Address *address = NULL;
128 Address *existing_address;
129 uint32_t lifetime_valid, lifetime_preferred, lifetime_remaining;
130 usec_t time_now;
131 unsigned prefixlen;
132 int r;
133
134 assert(link);
135 assert(rt);
136
137 r = sd_ndisc_router_get_timestamp(rt, clock_boottime_or_monotonic(), &time_now);
138 if (r < 0)
139 return log_link_warning_errno(link, r, "Failed to get RA timestamp: %m");
140
141 r = sd_ndisc_router_prefix_get_prefixlen(rt, &prefixlen);
142 if (r < 0)
143 return log_link_error_errno(link, r, "Failed to get prefix length: %m");
144
145 r = sd_ndisc_router_prefix_get_valid_lifetime(rt, &lifetime_valid);
146 if (r < 0)
147 return log_link_error_errno(link, r, "Failed to get prefix valid lifetime: %m");
148
149 r = sd_ndisc_router_prefix_get_preferred_lifetime(rt, &lifetime_preferred);
150 if (r < 0)
151 return log_link_error_errno(link, r, "Failed to get prefix preferred lifetime: %m");
152
153 /* The preferred lifetime is never greater than the valid lifetime */
154 if (lifetime_preferred > lifetime_valid)
155 return 0;
156
157 r = address_new(&address);
158 if (r < 0)
159 return log_link_error_errno(link, r, "Could not allocate address: %m");
160
161 address->family = AF_INET6;
162 r = sd_ndisc_router_prefix_get_address(rt, &address->in_addr.in6);
163 if (r < 0)
164 return log_link_error_errno(link, r, "Failed to get prefix address: %m");
165
166 if (in_addr_is_null(AF_INET6, (const union in_addr_union *) &link->network->ipv6_token) == 0)
167 memcpy(((char *)&address->in_addr.in6) + 8, ((char *)&link->network->ipv6_token) + 8, 8);
168 else {
169 /* see RFC4291 section 2.5.1 */
170 address->in_addr.in6.s6_addr[8] = link->mac.ether_addr_octet[0];
171 address->in_addr.in6.s6_addr[8] ^= 1 << 1;
172 address->in_addr.in6.s6_addr[9] = link->mac.ether_addr_octet[1];
173 address->in_addr.in6.s6_addr[10] = link->mac.ether_addr_octet[2];
174 address->in_addr.in6.s6_addr[11] = 0xff;
175 address->in_addr.in6.s6_addr[12] = 0xfe;
176 address->in_addr.in6.s6_addr[13] = link->mac.ether_addr_octet[3];
177 address->in_addr.in6.s6_addr[14] = link->mac.ether_addr_octet[4];
178 address->in_addr.in6.s6_addr[15] = link->mac.ether_addr_octet[5];
179 }
180 address->prefixlen = prefixlen;
181 address->flags = IFA_F_NOPREFIXROUTE|IFA_F_MANAGETEMPADDR;
182 address->cinfo.ifa_prefered = lifetime_preferred;
183
184 /* see RFC4862 section 5.5.3.e */
185 r = address_get(link, address->family, &address->in_addr, address->prefixlen, &existing_address);
186 if (r > 0) {
187 lifetime_remaining = existing_address->cinfo.tstamp / 100 + existing_address->cinfo.ifa_valid - time_now / USEC_PER_SEC;
188 if (lifetime_valid > NDISC_PREFIX_LFT_MIN || lifetime_valid > lifetime_remaining)
189 address->cinfo.ifa_valid = lifetime_valid;
190 else if (lifetime_remaining <= NDISC_PREFIX_LFT_MIN)
191 address->cinfo.ifa_valid = lifetime_remaining;
192 else
193 address->cinfo.ifa_valid = NDISC_PREFIX_LFT_MIN;
194 } else if (lifetime_valid > 0)
195 address->cinfo.ifa_valid = lifetime_valid;
196 else
197 return 0; /* see RFC4862 section 5.5.3.d */
198
199 if (address->cinfo.ifa_valid == 0)
200 return 0;
201
202 r = address_configure(address, link, ndisc_route_handler, true);
203 if (r < 0) {
204 log_link_warning_errno(link, r, "Could not set SLAAC address: %m");
205 link_enter_failed(link);
206 return r;
207 }
208
209 link->ndisc_messages++;
210
211 return 0;
212 }
213
214 static int ndisc_router_process_onlink_prefix(Link *link, sd_ndisc_router *rt) {
215 _cleanup_(route_freep) Route *route = NULL;
216 usec_t time_now;
217 uint32_t lifetime;
218 unsigned prefixlen;
219 int r;
220
221 assert(link);
222 assert(rt);
223
224 r = sd_ndisc_router_get_timestamp(rt, clock_boottime_or_monotonic(), &time_now);
225 if (r < 0)
226 return log_link_warning_errno(link, r, "Failed to get RA timestamp: %m");
227
228 r = sd_ndisc_router_prefix_get_prefixlen(rt, &prefixlen);
229 if (r < 0)
230 return log_link_error_errno(link, r, "Failed to get prefix length: %m");
231
232 r = sd_ndisc_router_prefix_get_valid_lifetime(rt, &lifetime);
233 if (r < 0)
234 return log_link_error_errno(link, r, "Failed to get prefix lifetime: %m");
235
236 r = route_new(&route);
237 if (r < 0)
238 return log_link_error_errno(link, r, "Could not allocate route: %m");
239
240 route->family = AF_INET6;
241 route->table = link->network->ipv6_accept_ra_route_table;
242 route->priority = link->network->dhcp_route_metric;
243 route->protocol = RTPROT_RA;
244 route->flags = RTM_F_PREFIX;
245 route->dst_prefixlen = prefixlen;
246 route->lifetime = time_now + lifetime * USEC_PER_SEC;
247
248 r = sd_ndisc_router_prefix_get_address(rt, &route->dst.in6);
249 if (r < 0)
250 return log_link_error_errno(link, r, "Failed to get prefix address: %m");
251
252 r = route_configure(route, link, ndisc_route_handler);
253 if (r < 0) {
254 log_link_warning_errno(link, r, "Could not set prefix route: %m");
255 link_enter_failed(link);
256 return r;
257 }
258
259 link->ndisc_messages++;
260
261 return 0;
262 }
263
264 static int ndisc_router_process_route(Link *link, sd_ndisc_router *rt) {
265 _cleanup_(route_freep) Route *route = NULL;
266 struct in6_addr gateway;
267 uint32_t lifetime;
268 unsigned preference, prefixlen;
269 usec_t time_now;
270 int r;
271
272 assert(link);
273
274 r = sd_ndisc_router_route_get_lifetime(rt, &lifetime);
275 if (r < 0)
276 return log_link_warning_errno(link, r, "Failed to get gateway address from RA: %m");
277
278 if (lifetime == 0)
279 return 0;
280
281 r = sd_ndisc_router_get_address(rt, &gateway);
282 if (r < 0)
283 return log_link_warning_errno(link, r, "Failed to get gateway address from RA: %m");
284
285 r = sd_ndisc_router_route_get_prefixlen(rt, &prefixlen);
286 if (r < 0)
287 return log_link_warning_errno(link, r, "Failed to get route prefix length: %m");
288
289 r = sd_ndisc_router_route_get_preference(rt, &preference);
290 if (r < 0)
291 return log_link_warning_errno(link, r, "Failed to get default router preference from RA: %m");
292
293 r = sd_ndisc_router_get_timestamp(rt, clock_boottime_or_monotonic(), &time_now);
294 if (r < 0)
295 return log_link_warning_errno(link, r, "Failed to get RA timestamp: %m");
296
297 r = route_new(&route);
298 if (r < 0)
299 return log_link_error_errno(link, r, "Could not allocate route: %m");
300
301 route->family = AF_INET6;
302 route->table = link->network->ipv6_accept_ra_route_table;
303 route->protocol = RTPROT_RA;
304 route->pref = preference;
305 route->gw.in6 = gateway;
306 route->dst_prefixlen = prefixlen;
307 route->lifetime = time_now + lifetime * USEC_PER_SEC;
308
309 r = sd_ndisc_router_route_get_address(rt, &route->dst.in6);
310 if (r < 0)
311 return log_link_error_errno(link, r, "Failed to get route address: %m");
312
313 r = route_configure(route, link, ndisc_route_handler);
314 if (r < 0) {
315 log_link_warning_errno(link, r, "Could not set additional route: %m");
316 link_enter_failed(link);
317 return r;
318 }
319
320 link->ndisc_messages++;
321
322 return 0;
323 }
324
325 static void ndisc_rdnss_hash_func(const NDiscRDNSS *x, struct siphash *state) {
326 siphash24_compress(&x->address, sizeof(x->address), state);
327 }
328
329 static int ndisc_rdnss_compare_func(const NDiscRDNSS *a, const NDiscRDNSS *b) {
330 return memcmp(&a->address, &b->address, sizeof(a->address));
331 }
332
333 DEFINE_PRIVATE_HASH_OPS(ndisc_rdnss_hash_ops, NDiscRDNSS, ndisc_rdnss_hash_func, ndisc_rdnss_compare_func);
334
335 static int ndisc_router_process_rdnss(Link *link, sd_ndisc_router *rt) {
336 uint32_t lifetime;
337 const struct in6_addr *a;
338 usec_t time_now;
339 int i, n, r;
340
341 assert(link);
342 assert(rt);
343
344 r = sd_ndisc_router_get_timestamp(rt, clock_boottime_or_monotonic(), &time_now);
345 if (r < 0)
346 return log_link_warning_errno(link, r, "Failed to get RA timestamp: %m");
347
348 r = sd_ndisc_router_rdnss_get_lifetime(rt, &lifetime);
349 if (r < 0)
350 return log_link_warning_errno(link, r, "Failed to get RDNSS lifetime: %m");
351
352 n = sd_ndisc_router_rdnss_get_addresses(rt, &a);
353 if (n < 0)
354 return log_link_warning_errno(link, n, "Failed to get RDNSS addresses: %m");
355
356 for (i = 0; i < n; i++) {
357 _cleanup_free_ NDiscRDNSS *x = NULL;
358 NDiscRDNSS d = {
359 .address = a[i],
360 }, *y;
361
362 if (lifetime == 0) {
363 (void) set_remove(link->ndisc_rdnss, &d);
364 link_dirty(link);
365 continue;
366 }
367
368 y = set_get(link->ndisc_rdnss, &d);
369 if (y) {
370 y->valid_until = time_now + lifetime * USEC_PER_SEC;
371 continue;
372 }
373
374 ndisc_vacuum(link);
375
376 if (set_size(link->ndisc_rdnss) >= NDISC_RDNSS_MAX) {
377 log_link_warning(link, "Too many RDNSS records per link, ignoring.");
378 continue;
379 }
380
381 r = set_ensure_allocated(&link->ndisc_rdnss, &ndisc_rdnss_hash_ops);
382 if (r < 0)
383 return log_oom();
384
385 x = new(NDiscRDNSS, 1);
386 if (!x)
387 return log_oom();
388
389 *x = (NDiscRDNSS) {
390 .address = a[i],
391 .valid_until = time_now + lifetime * USEC_PER_SEC,
392 };
393
394 r = set_put(link->ndisc_rdnss, x);
395 if (r < 0)
396 return log_oom();
397
398 TAKE_PTR(x);
399
400 assert(r > 0);
401 link_dirty(link);
402 }
403
404 return 0;
405 }
406
407 static void ndisc_dnssl_hash_func(const NDiscDNSSL *x, struct siphash *state) {
408 siphash24_compress(NDISC_DNSSL_DOMAIN(x), strlen(NDISC_DNSSL_DOMAIN(x)), state);
409 }
410
411 static int ndisc_dnssl_compare_func(const NDiscDNSSL *a, const NDiscDNSSL *b) {
412 return strcmp(NDISC_DNSSL_DOMAIN(a), NDISC_DNSSL_DOMAIN(b));
413 }
414
415 DEFINE_PRIVATE_HASH_OPS(ndisc_dnssl_hash_ops, NDiscDNSSL, ndisc_dnssl_hash_func, ndisc_dnssl_compare_func);
416
417 static void ndisc_router_process_dnssl(Link *link, sd_ndisc_router *rt) {
418 _cleanup_strv_free_ char **l = NULL;
419 uint32_t lifetime;
420 usec_t time_now;
421 char **i;
422 int r;
423
424 assert(link);
425 assert(rt);
426
427 r = sd_ndisc_router_get_timestamp(rt, clock_boottime_or_monotonic(), &time_now);
428 if (r < 0) {
429 log_link_warning_errno(link, r, "Failed to get RA timestamp: %m");
430 return;
431 }
432
433 r = sd_ndisc_router_dnssl_get_lifetime(rt, &lifetime);
434 if (r < 0) {
435 log_link_warning_errno(link, r, "Failed to get RDNSS lifetime: %m");
436 return;
437 }
438
439 r = sd_ndisc_router_dnssl_get_domains(rt, &l);
440 if (r < 0) {
441 log_link_warning_errno(link, r, "Failed to get RDNSS addresses: %m");
442 return;
443 }
444
445 STRV_FOREACH(i, l) {
446 _cleanup_free_ NDiscDNSSL *s;
447 NDiscDNSSL *x;
448
449 s = malloc0(ALIGN(sizeof(NDiscDNSSL)) + strlen(*i) + 1);
450 if (!s) {
451 log_oom();
452 return;
453 }
454
455 strcpy(NDISC_DNSSL_DOMAIN(s), *i);
456
457 if (lifetime == 0) {
458 (void) set_remove(link->ndisc_dnssl, s);
459 link_dirty(link);
460 continue;
461 }
462
463 x = set_get(link->ndisc_dnssl, s);
464 if (x) {
465 x->valid_until = time_now + lifetime * USEC_PER_SEC;
466 continue;
467 }
468
469 ndisc_vacuum(link);
470
471 if (set_size(link->ndisc_dnssl) >= NDISC_DNSSL_MAX) {
472 log_link_warning(link, "Too many DNSSL records per link, ignoring.");
473 continue;
474 }
475
476 r = set_ensure_allocated(&link->ndisc_dnssl, &ndisc_dnssl_hash_ops);
477 if (r < 0) {
478 log_oom();
479 return;
480 }
481
482 s->valid_until = time_now + lifetime * USEC_PER_SEC;
483
484 r = set_put(link->ndisc_dnssl, s);
485 if (r < 0) {
486 log_oom();
487 return;
488 }
489
490 s = NULL;
491 assert(r > 0);
492 link_dirty(link);
493 }
494 }
495
496 static void ndisc_router_process_options(Link *link, sd_ndisc_router *rt) {
497 int r;
498
499 assert(link);
500 assert(rt);
501
502 r = sd_ndisc_router_option_rewind(rt);
503 for (;;) {
504 uint8_t type;
505
506 if (r < 0) {
507 log_link_warning_errno(link, r, "Failed to iterate through options: %m");
508 return;
509 }
510 if (r == 0) /* EOF */
511 break;
512
513 r = sd_ndisc_router_option_get_type(rt, &type);
514 if (r < 0) {
515 log_link_warning_errno(link, r, "Failed to get RA option type: %m");
516 return;
517 }
518
519 switch (type) {
520
521 case SD_NDISC_OPTION_PREFIX_INFORMATION: {
522 uint8_t flags;
523
524 r = sd_ndisc_router_prefix_get_flags(rt, &flags);
525 if (r < 0) {
526 log_link_warning_errno(link, r, "Failed to get RA prefix flags: %m");
527 return;
528 }
529
530 if (flags & ND_OPT_PI_FLAG_ONLINK)
531 (void) ndisc_router_process_onlink_prefix(link, rt);
532 if (flags & ND_OPT_PI_FLAG_AUTO)
533 (void) ndisc_router_process_autonomous_prefix(link, rt);
534
535 break;
536 }
537
538 case SD_NDISC_OPTION_ROUTE_INFORMATION:
539 (void) ndisc_router_process_route(link, rt);
540 break;
541
542 case SD_NDISC_OPTION_RDNSS:
543 if (link->network->ipv6_accept_ra_use_dns)
544 (void) ndisc_router_process_rdnss(link, rt);
545 break;
546
547 case SD_NDISC_OPTION_DNSSL:
548 if (link->network->ipv6_accept_ra_use_dns)
549 (void) ndisc_router_process_dnssl(link, rt);
550 break;
551 }
552
553 r = sd_ndisc_router_option_next(rt);
554 }
555 }
556
557 static int ndisc_router_handler(Link *link, sd_ndisc_router *rt) {
558 uint64_t flags;
559 int r = 0;
560
561 assert(link);
562 assert(link->network);
563 assert(link->manager);
564 assert(rt);
565
566 r = sd_ndisc_router_get_flags(rt, &flags);
567 if (r < 0)
568 return log_link_warning_errno(link, r, "Failed to get RA flags: %m");
569
570 if (flags & (ND_RA_FLAG_MANAGED | ND_RA_FLAG_OTHER)) {
571 /* (re)start DHCPv6 client in stateful or stateless mode according to RA flags */
572 r = dhcp6_request_address(link, !(flags & ND_RA_FLAG_MANAGED));
573 if (r < 0 && r != -EBUSY)
574 log_link_warning_errno(link, r, "Could not acquire DHCPv6 lease on NDisc request: %m");
575 else {
576 log_link_debug(link, "Acquiring DHCPv6 lease on NDisc request");
577 r = 0;
578 }
579 }
580
581 ndisc_router_process_default(link, rt);
582 ndisc_router_process_options(link, rt);
583
584 return r;
585 }
586
587 static void ndisc_handler(sd_ndisc *nd, sd_ndisc_event event, sd_ndisc_router *rt, void *userdata) {
588 Link *link = userdata;
589
590 assert(link);
591
592 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
593 return;
594
595 switch (event) {
596
597 case SD_NDISC_EVENT_ROUTER:
598 (void) ndisc_router_handler(link, rt);
599 break;
600
601 case SD_NDISC_EVENT_TIMEOUT:
602 link->ndisc_configured = true;
603 link_check_ready(link);
604
605 break;
606 default:
607 log_link_warning(link, "IPv6 Neighbor Discovery unknown event: %d", event);
608 }
609 }
610
611 int ndisc_configure(Link *link) {
612 int r;
613
614 assert(link);
615
616 r = sd_ndisc_new(&link->ndisc);
617 if (r < 0)
618 return r;
619
620 r = sd_ndisc_attach_event(link->ndisc, NULL, 0);
621 if (r < 0)
622 return r;
623
624 r = sd_ndisc_set_mac(link->ndisc, &link->mac);
625 if (r < 0)
626 return r;
627
628 r = sd_ndisc_set_ifindex(link->ndisc, link->ifindex);
629 if (r < 0)
630 return r;
631
632 r = sd_ndisc_set_callback(link->ndisc, ndisc_handler, link);
633 if (r < 0)
634 return r;
635
636 return 0;
637 }
638
639 void ndisc_vacuum(Link *link) {
640 NDiscRDNSS *r;
641 NDiscDNSSL *d;
642 Iterator i;
643 usec_t time_now;
644
645 assert(link);
646
647 /* Removes all RDNSS and DNSSL entries whose validity time has passed */
648
649 time_now = now(clock_boottime_or_monotonic());
650
651 SET_FOREACH(r, link->ndisc_rdnss, i)
652 if (r->valid_until < time_now) {
653 free(set_remove(link->ndisc_rdnss, r));
654 link_dirty(link);
655 }
656
657 SET_FOREACH(d, link->ndisc_dnssl, i)
658 if (d->valid_until < time_now) {
659 free(set_remove(link->ndisc_dnssl, d));
660 link_dirty(link);
661 }
662 }
663
664 void ndisc_flush(Link *link) {
665 assert(link);
666
667 /* Removes all RDNSS and DNSSL entries, without exception */
668
669 link->ndisc_rdnss = set_free_free(link->ndisc_rdnss);
670 link->ndisc_dnssl = set_free_free(link->ndisc_dnssl);
671 }