Debian Codegraph @

src/shared/local-addresses.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-netlink.h"
4
5 #include "alloc-util.h"
6 #include "fd-util.h"
7 #include "local-addresses.h"
8 #include "log.h"
9 #include "netlink-util.h"
10 #include "socket-util.h"
11 #include "sort-util.h"
12
13 static int address_compare(const struct local_address *a, const struct local_address *b) {
14 int r;
15
16 /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */
17
18 if (a->family == AF_INET && b->family == AF_INET6)
19 return -1;
20 if (a->family == AF_INET6 && b->family == AF_INET)
21 return 1;
22
23 r = CMP(a->scope, b->scope);
24 if (r != 0)
25 return r;
26
27 r = CMP(a->priority, b->priority);
28 if (r != 0)
29 return r;
30
31 r = CMP(a->weight, b->weight);
32 if (r != 0)
33 return r;
34
35 r = CMP(a->ifindex, b->ifindex);
36 if (r != 0)
37 return r;
38
39 return memcmp(&a->address, &b->address, FAMILY_ADDRESS_SIZE(a->family));
40 }
41
42 bool has_local_address(const struct local_address *addresses, size_t n_addresses, const struct local_address *needle) {
43 assert(addresses || n_addresses == 0);
44 assert(needle);
45
46 FOREACH_ARRAY(i, addresses, n_addresses)
47 if (address_compare(i, needle) == 0)
48 return true;
49
50 return false;
51 }
52
53 static void suppress_duplicates(struct local_address *list, size_t *n_list) {
54 size_t old_size, new_size;
55
56 POINTER_MAY_BE_NULL(list);
57 assert(n_list);
58
59 /* Removes duplicate entries, assumes the list of addresses is already sorted. Updates in-place. */
60
61 if (*n_list < 2) /* list with less than two entries can't have duplicates */
62 return;
63
64 old_size = *n_list;
65 new_size = 1;
66
67 for (size_t i = 1; i < old_size; i++) {
68
69 if (address_compare(list + i, list + new_size - 1) == 0)
70 continue;
71
72 list[new_size++] = list[i];
73 }
74
75 *n_list = new_size;
76 }
77
78 static int add_local_address_full(
79 struct local_address **list,
80 size_t *n_list,
81 int ifindex,
82 unsigned char scope,
83 uint32_t priority,
84 uint32_t weight,
85 int family,
86 const union in_addr_union *address,
87 const union in_addr_union *prefsrc) {
88
89 assert(list);
90 assert(n_list);
91 assert(ifindex > 0);
92 assert(IN_SET(family, AF_INET, AF_INET6));
93 assert(address);
94 POINTER_MAY_BE_NULL(prefsrc);
95
96 if (!GREEDY_REALLOC(*list, *n_list + 1))
97 return -ENOMEM;
98
99 (*list)[(*n_list)++] = (struct local_address) {
100 .ifindex = ifindex,
101 .scope = scope,
102 .priority = priority,
103 .weight = weight,
104 .family = family,
105 .address = *address,
106 .prefsrc = prefsrc ? *prefsrc : IN_ADDR_NULL,
107 };
108
109 return 1;
110 }
111
112 int add_local_address(
113 struct local_address **list,
114 size_t *n_list,
115 int ifindex,
116 unsigned char scope,
117 int family,
118 const union in_addr_union *address) {
119
120 return add_local_address_full(
121 list, n_list, ifindex,
122 scope, /* priority= */ 0, /* weight= */ 0,
123 family, address, /* prefsrc= */ NULL);
124 }
125
126 int local_addresses(
127 sd_netlink *context,
128 int ifindex,
129 int af,
130 struct local_address **ret) {
131
132 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
133 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
134 _cleanup_free_ struct local_address *list = NULL;
135 size_t n_list = 0;
136 int r;
137
138 if (context)
139 rtnl = sd_netlink_ref(context);
140 else {
141 r = sd_netlink_open(&rtnl);
142 if (r < 0)
143 return r;
144 }
145
146 r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, ifindex, af);
147 if (r < 0)
148 return r;
149
150 r = sd_netlink_message_set_request_dump(req, true);
151 if (r < 0)
152 return r;
153
154 r = sd_netlink_call(rtnl, req, 0, &reply);
155 if (r < 0)
156 return r;
157
158 for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) {
159 union in_addr_union a;
160 unsigned char scope;
161 uint16_t type;
162 int ifi, family;
163
164 r = sd_netlink_message_get_errno(m);
165 if (r < 0)
166 return r;
167
168 r = sd_netlink_message_get_type(m, &type);
169 if (r < 0)
170 return r;
171 if (type != RTM_NEWADDR)
172 continue;
173
174 r = sd_rtnl_message_addr_get_ifindex(m, &ifi);
175 if (r < 0)
176 return r;
177 if (ifindex > 0 && ifi != ifindex)
178 continue;
179
180 r = sd_rtnl_message_addr_get_family(m, &family);
181 if (r < 0)
182 return r;
183 if (!IN_SET(family, AF_INET, AF_INET6))
184 continue;
185 if (af != AF_UNSPEC && af != family)
186 continue;
187
188 uint32_t flags;
189 r = sd_netlink_message_read_u32(m, IFA_FLAGS, &flags);
190 if (r < 0)
191 return r;
192 if ((flags & (IFA_F_DEPRECATED|IFA_F_TENTATIVE)) != 0)
193 continue;
194
195 r = sd_rtnl_message_addr_get_scope(m, &scope);
196 if (r < 0)
197 return r;
198
199 if (ifindex == 0 && IN_SET(scope, RT_SCOPE_HOST, RT_SCOPE_NOWHERE))
200 continue;
201
202 switch (family) {
203
204 case AF_INET:
205 r = sd_netlink_message_read_in_addr(m, IFA_LOCAL, &a.in);
206 if (r < 0) {
207 r = sd_netlink_message_read_in_addr(m, IFA_ADDRESS, &a.in);
208 if (r < 0)
209 continue;
210 }
211 break;
212
213 case AF_INET6:
214 r = sd_netlink_message_read_in6_addr(m, IFA_LOCAL, &a.in6);
215 if (r < 0) {
216 r = sd_netlink_message_read_in6_addr(m, IFA_ADDRESS, &a.in6);
217 if (r < 0)
218 continue;
219 }
220 break;
221
222 default:
223 assert_not_reached();
224 }
225
226 r = add_local_address(&list, &n_list, ifi, scope, family, &a);
227 if (r < 0)
228 return r;
229 };
230
231 typesafe_qsort(list, n_list, address_compare);
232 suppress_duplicates(list, &n_list);
233
234 if (ret)
235 *ret = TAKE_PTR(list);
236
237 return (int) n_list;
238 }
239
240 static int add_local_gateway(
241 struct local_address **list,
242 size_t *n_list,
243 int ifindex,
244 uint32_t priority,
245 uint32_t weight,
246 int family,
247 const union in_addr_union *address,
248 const union in_addr_union *prefsrc) {
249
250 return add_local_address_full(
251 list, n_list,
252 ifindex,
253 /* scope= */ 0, priority, weight,
254 family, address, prefsrc);
255 }
256
257 static int parse_nexthop_one(
258 struct local_address **list,
259 size_t *n_list,
260 bool allow_via,
261 int family,
262 uint32_t priority,
263 const union in_addr_union *prefsrc,
264 const struct rtnexthop *rtnh) {
265
266 bool has_gw = false;
267 int r;
268
269 assert(rtnh);
270
271 size_t len = rtnh->rtnh_len - sizeof(struct rtnexthop);
272 for (struct rtattr *attr = RTNH_DATA(rtnh); RTA_OK(attr, len); attr = RTA_NEXT(attr, len))
273
274 switch (attr->rta_type) {
275 case RTA_GATEWAY:
276 if (has_gw)
277 return -EBADMSG;
278
279 has_gw = true;
280
281 if (attr->rta_len != RTA_LENGTH(FAMILY_ADDRESS_SIZE(family)))
282 return -EBADMSG;
283
284 union in_addr_union a;
285 memcpy(&a, RTA_DATA(attr), FAMILY_ADDRESS_SIZE(family));
286 r = add_local_gateway(list, n_list, rtnh->rtnh_ifindex, priority, rtnh->rtnh_hops, family, &a, prefsrc);
287 if (r < 0)
288 return r;
289
290 break;
291
292 case RTA_VIA:
293 if (has_gw)
294 return -EBADMSG;
295
296 has_gw = true;
297
298 if (!allow_via)
299 continue;
300
301 if (family != AF_INET)
302 return -EBADMSG; /* RTA_VIA is only supported for IPv4 routes. */
303
304 if (attr->rta_len != RTA_LENGTH(sizeof(RouteVia)))
305 return -EBADMSG;
306
307 RouteVia *via = RTA_DATA(attr);
308 if (via->family != AF_INET6)
309 return -EBADMSG; /* gateway address should be always IPv6. */
310
311 r = add_local_gateway(list, n_list, rtnh->rtnh_ifindex, priority, rtnh->rtnh_hops, via->family,
312 &(union in_addr_union) { .in6 = via->address.in6 },
313 /* prefsrc= */ NULL);
314 if (r < 0)
315 return r;
316
317 break;
318 }
319
320 return 0;
321 }
322
323 static int parse_nexthops(
324 struct local_address **list,
325 size_t *n_list,
326 int ifindex,
327 bool allow_via,
328 int family,
329 uint32_t priority,
330 const union in_addr_union *prefsrc,
331 const struct rtnexthop *rtnh,
332 size_t size) {
333
334 int r;
335
336 assert(list);
337 assert(n_list);
338 assert(IN_SET(family, AF_INET, AF_INET6));
339 assert(rtnh || size == 0);
340
341 if (size < sizeof(struct rtnexthop))
342 return -EBADMSG;
343
344 for (; size >= sizeof(struct rtnexthop); ) {
345 if (NLMSG_ALIGN(rtnh->rtnh_len) > size)
346 return -EBADMSG;
347
348 if (rtnh->rtnh_len < sizeof(struct rtnexthop))
349 return -EBADMSG;
350
351 if (ifindex > 0 && rtnh->rtnh_ifindex != ifindex)
352 goto next_nexthop;
353
354 r = parse_nexthop_one(list, n_list, allow_via, family, priority, prefsrc, rtnh);
355 if (r < 0)
356 return r;
357
358 next_nexthop:
359 size -= NLMSG_ALIGN(rtnh->rtnh_len);
360 rtnh = RTNH_NEXT(rtnh);
361 }
362
363 return 0;
364 }
365
366 int local_gateways(
367 sd_netlink *context,
368 int ifindex,
369 int af,
370 struct local_address **ret) {
371
372 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
373 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
374 _cleanup_free_ struct local_address *list = NULL;
375 size_t n_list = 0;
376 int r;
377
378 /* The RTA_VIA attribute is used only for IPv4 routes with an IPv6 gateway. If IPv4 gateways are
379 * requested (af == AF_INET), then we do not return IPv6 gateway addresses. Similarly, if IPv6
380 * gateways are requested (af == AF_INET6), then we do not return gateway addresses for IPv4 routes.
381 * So, the RTA_VIA attribute is only parsed when af == AF_UNSPEC. */
382 bool allow_via = af == AF_UNSPEC;
383
384 if (context)
385 rtnl = sd_netlink_ref(context);
386 else {
387 r = sd_netlink_open(&rtnl);
388 if (r < 0)
389 return r;
390 }
391
392 r = sd_rtnl_message_new_route(rtnl, &req, RTM_GETROUTE, af, RTPROT_UNSPEC);
393 if (r < 0)
394 return r;
395
396 r = sd_rtnl_message_route_set_type(req, RTN_UNICAST);
397 if (r < 0)
398 return r;
399
400 r = sd_rtnl_message_route_set_table(req, RT_TABLE_MAIN);
401 if (r < 0)
402 return r;
403
404 r = sd_netlink_message_set_request_dump(req, true);
405 if (r < 0)
406 return r;
407
408 r = sd_netlink_call(rtnl, req, 0, &reply);
409 if (r < 0)
410 return r;
411
412 for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) {
413 union in_addr_union prefsrc = IN_ADDR_NULL;
414 uint16_t type;
415 unsigned char dst_len, src_len, table;
416 uint32_t ifi = 0, priority = 0;
417 int family;
418
419 r = sd_netlink_message_get_errno(m);
420 if (r < 0)
421 return r;
422
423 r = sd_netlink_message_get_type(m, &type);
424 if (r < 0)
425 return r;
426 if (type != RTM_NEWROUTE)
427 continue;
428
429 /* We only care for default routes */
430 r = sd_rtnl_message_route_get_dst_prefixlen(m, &dst_len);
431 if (r < 0)
432 return r;
433 if (dst_len != 0)
434 continue;
435
436 r = sd_rtnl_message_route_get_src_prefixlen(m, &src_len);
437 if (r < 0)
438 return r;
439 if (src_len != 0)
440 continue;
441
442 r = sd_rtnl_message_route_get_table(m, &table);
443 if (r < 0)
444 return r;
445 if (table != RT_TABLE_MAIN)
446 continue;
447
448 r = sd_netlink_message_read_u32(m, RTA_PRIORITY, &priority);
449 if (r < 0 && r != -ENODATA)
450 return r;
451
452 r = sd_rtnl_message_route_get_family(m, &family);
453 if (r < 0)
454 return r;
455 if (!IN_SET(family, AF_INET, AF_INET6))
456 continue;
457 if (af != AF_UNSPEC && af != family)
458 continue;
459
460 r = netlink_message_read_in_addr_union(m, RTA_PREFSRC, family, &prefsrc);
461 if (r < 0 && r != -ENODATA)
462 return r;
463
464 r = sd_netlink_message_read_u32(m, RTA_OIF, &ifi);
465 if (r < 0 && r != -ENODATA)
466 return r;
467 if (r >= 0) {
468 if (ifi <= 0)
469 return -EINVAL;
470 if (ifindex > 0 && (int) ifi != ifindex)
471 continue;
472
473 union in_addr_union gateway;
474 r = netlink_message_read_in_addr_union(m, RTA_GATEWAY, family, &gateway);
475 if (r < 0 && r != -ENODATA)
476 return r;
477 if (r >= 0) {
478 r = add_local_gateway(&list, &n_list, ifi, priority, 0, family, &gateway, &prefsrc);
479 if (r < 0)
480 return r;
481
482 continue;
483 }
484
485 if (!allow_via)
486 continue;
487
488 if (family != AF_INET)
489 continue;
490
491 RouteVia via;
492 r = sd_netlink_message_read(m, RTA_VIA, sizeof(via), &via);
493 if (r < 0 && r != -ENODATA)
494 return r;
495 if (r >= 0) {
496 if (via.family != AF_INET6)
497 return -EBADMSG;
498
499 /* Ignore prefsrc, and let's take the source address by socket command, if necessary. */
500 r = add_local_gateway(&list, &n_list, ifi, priority, 0, via.family,
501 &(union in_addr_union) { .in6 = via.address.in6 },
502 /* prefsrc= */ NULL);
503 if (r < 0)
504 return r;
505 }
506
507 /* If the route has RTA_OIF, it does not have RTA_MULTIPATH. */
508 continue;
509 }
510
511 size_t rta_len;
512 _cleanup_free_ void *rta_multipath = NULL;
513 r = sd_netlink_message_read_data(m, RTA_MULTIPATH, &rta_len, &rta_multipath);
514 if (r < 0 && r != -ENODATA)
515 return r;
516 if (r >= 0) {
517 r = parse_nexthops(&list, &n_list, ifindex, allow_via, family, priority, &prefsrc, rta_multipath, rta_len);
518 if (r < 0)
519 return r;
520 }
521 }
522
523 typesafe_qsort(list, n_list, address_compare);
524 suppress_duplicates(list, &n_list);
525
526 if (ret)
527 *ret = TAKE_PTR(list);
528
529 return (int) n_list;
530 }
531
532 static int add_local_outbound(
533 struct local_address **list,
534 size_t *n_list,
535 int ifindex,
536 int family,
537 const union in_addr_union *address) {
538
539 return add_local_address_full(
540 list, n_list, ifindex,
541 /* scope= */ 0, /* priority= */ 0, /* weight= */ 0,
542 family, address, /* prefsrc= */ NULL);
543 }
544
545 static int add_local_outbound_by_prefsrc(
546 struct local_address **list,
547 size_t *n_list,
548 const struct local_address *gateway,
549 const struct local_address *addresses,
550 size_t n_addresses) {
551
552 int r;
553
554 assert(list);
555 assert(n_list);
556 assert(gateway);
557
558 if (!in_addr_is_set(gateway->family, &gateway->prefsrc))
559 return 0;
560
561 /* If the gateway has prefsrc, then let's honor the field. But, check if the address is assigned to
562 * the same interface, like we do with SO_BINDTOINDEX. */
563
564 bool found = false;
565 FOREACH_ARRAY(a, addresses, n_addresses) {
566 if (a->ifindex != gateway->ifindex)
567 continue;
568 if (a->family != gateway->family)
569 continue;
570 if (in_addr_equal(a->family, &a->address, &gateway->prefsrc) <= 0)
571 continue;
572
573 found = true;
574 break;
575 }
576 if (!found)
577 return -EHOSTUNREACH;
578
579 r = add_local_outbound(list, n_list, gateway->ifindex, gateway->family, &gateway->prefsrc);
580 if (r < 0)
581 return r;
582
583 return 1;
584 }
585
586 int local_outbounds(
587 sd_netlink *context,
588 int ifindex,
589 int af,
590 struct local_address **ret) {
591
592 _cleanup_free_ struct local_address *list = NULL, *gateways = NULL, *addresses = NULL;
593 size_t n_list = 0;
594 int r, n_gateways, n_addresses;
595
596 /* Determines our default outbound addresses, i.e. the "primary" local addresses we use to talk to IP
597 * addresses behind the default routes. This is still an address of the local host (i.e. this doesn't
598 * resolve NAT or so), but it's the set of addresses the local IP stack most likely uses to talk to
599 * other hosts.
600 *
601 * This works by connect()ing a SOCK_DGRAM socket to the local gateways, and then reading the IP
602 * address off the socket that was chosen for the routing decision. */
603
604 n_gateways = local_gateways(context, ifindex, af, &gateways);
605 if (n_gateways < 0)
606 return n_gateways;
607 if (n_gateways == 0) {
608 /* No gateways? Then we have no outbound addresses either. */
609 if (ret)
610 *ret = NULL;
611
612 return 0;
613 }
614
615 n_addresses = local_addresses(context, ifindex, af, &addresses);
616 if (n_addresses < 0)
617 return n_addresses;
618
619 FOREACH_ARRAY(i, gateways, n_gateways) {
620 _cleanup_close_ int fd = -EBADF;
621 union sockaddr_union sa;
622 socklen_t salen;
623
624 r = add_local_outbound_by_prefsrc(&list, &n_list, i, addresses, n_addresses);
625 if (r > 0 || r == -EHOSTUNREACH)
626 continue;
627 if (r < 0)
628 return r;
629
630 fd = socket(i->family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
631 if (fd < 0)
632 return -errno;
633
634 switch (i->family) {
635
636 case AF_INET:
637 sa.in = (struct sockaddr_in) {
638 .sin_family = AF_INET,
639 .sin_addr = i->address.in,
640 .sin_port = htobe16(53), /* doesn't really matter which port we pick —
641 * we just care about the routing decision */
642 };
643
644 break;
645
646 case AF_INET6:
647 sa.in6 = (struct sockaddr_in6) {
648 .sin6_family = AF_INET6,
649 .sin6_addr = i->address.in6,
650 .sin6_port = htobe16(53),
651 .sin6_scope_id = i->ifindex,
652 };
653
654 break;
655
656 default:
657 assert_not_reached();
658 }
659
660 /* So ideally we'd just use IP_UNICAST_IF here to pass the ifindex info to the kernel before
661 * connect()ing, sot that it influences the routing decision. However, on current kernels
662 * IP_UNICAST_IF doesn't actually influence the routing decision for UDP — which I think
663 * should probably just be considered a bug. Once that bug is fixed this is the best API to
664 * use, since it is the most lightweight. */
665 r = socket_set_unicast_if(fd, i->family, i->ifindex);
666 if (r < 0)
667 log_debug_errno(r, "Failed to set unicast interface index %i, ignoring: %m", i->ifindex);
668
669 /* We'll also use SO_BINDTOINDEX. This requires CAP_NET_RAW on old kernels, hence there's a
670 * good chance this fails. Since 5.7 this restriction was dropped and the first
671 * SO_BINDTOINDEX on a socket may be done without privileges. This one has the benefit of
672 * really influencing the routing decision, i.e. this one definitely works for us — as long
673 * as we have the privileges for it. */
674 r = socket_bind_to_ifindex(fd, i->ifindex);
675 if (r < 0)
676 log_debug_errno(r, "Failed to bind socket to interface %i, ignoring: %m", i->ifindex);
677
678 /* Let's now connect() to the UDP socket, forcing the kernel to make a routing decision and
679 * auto-bind the socket. We ignore failures on this, since that failure might happen for a
680 * multitude of reasons (policy/firewall issues, who knows?) and some of them might be
681 * *after* the routing decision and the auto-binding already took place. If so we can still
682 * make use of the binding and return it. Hence, let's not unnecessarily fail early here: we
683 * can still easily detect if the auto-binding worked or not, by comparing the bound IP
684 * address with zero — which we do below. */
685 if (connect(fd, &sa.sa, sockaddr_len(&sa)) < 0)
686 log_debug_errno(errno, "Failed to connect SOCK_DGRAM socket to gateway, ignoring: %m");
687
688 /* Let's now read the socket address of the socket. A routing decision should have been
689 * made. Let's verify that and use the data. */
690 salen = sockaddr_len(&sa);
691 if (getsockname(fd, &sa.sa, &salen) < 0)
692 return -errno;
693 assert(sa.sa.sa_family == i->family);
694 assert(salen == sockaddr_len(&sa));
695
696 switch (i->family) {
697
698 case AF_INET:
699 if (in4_addr_is_null(&sa.in.sin_addr)) /* Auto-binding didn't work. :-( */
700 continue;
701
702 r = add_local_outbound(&list, &n_list, i->ifindex, i->family,
703 &(union in_addr_union) { .in = sa.in.sin_addr });
704 if (r < 0)
705 return r;
706 break;
707
708 case AF_INET6:
709 if (in6_addr_is_null(&sa.in6.sin6_addr))
710 continue;
711
712 r = add_local_outbound(&list, &n_list, i->ifindex, i->family,
713 &(union in_addr_union) { .in6 = sa.in6.sin6_addr });
714 if (r < 0)
715 return r;
716 break;
717
718 default:
719 assert_not_reached();
720 }
721 }
722
723 typesafe_qsort(list, n_list, address_compare);
724 suppress_duplicates(list, &n_list);
725
726 if (ret)
727 *ret = TAKE_PTR(list);
728
729 return (int) n_list;
730 }