1 /*
2 * Original file name getaddrinfo.c
3 * Lifted from the 'Android Bionic' project with the BSD license.
4 */
5
6 /*
7 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Copyright (C) 2018 The Android Open Source Project
9 * Copyright (C) 2019 by Andrew Selivanov
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the project nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include "ares_setup.h"
38
39 #ifdef HAVE_NETINET_IN_H
40 # include <netinet/in.h>
41 #endif
42 #ifdef HAVE_NETDB_H
43 # include <netdb.h>
44 #endif
45 #ifdef HAVE_STRINGS_H
46 # include <strings.h>
47 #endif
48
49 #include <assert.h>
50 #include <limits.h>
51
52 #include "ares.h"
53 #include "ares_private.h"
54
55 struct addrinfo_sort_elem
56 {
57 struct ares_addrinfo_node *ai;
58 int has_src_addr;
59 ares_sockaddr src_addr;
60 int original_order;
61 };
62
63 #define ARES_IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
64
65 #define ARES_IPV6_ADDR_SCOPE_NODELOCAL 0x01
66 #define ARES_IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
67 #define ARES_IPV6_ADDR_SCOPE_LINKLOCAL 0x02
68 #define ARES_IPV6_ADDR_SCOPE_SITELOCAL 0x05
69 #define ARES_IPV6_ADDR_SCOPE_ORGLOCAL 0x08
70 #define ARES_IPV6_ADDR_SCOPE_GLOBAL 0x0e
71
72 #define ARES_IN_LOOPBACK(a) ((((long int)(a)) & 0xff000000) == 0x7f000000)
73
74 /* RFC 4193. */
75 #define ARES_IN6_IS_ADDR_ULA(a) (((a)->s6_addr[0] & 0xfe) == 0xfc)
76
77 /* These macros are modelled after the ones in <netinet/in6.h>. */
78 /* RFC 4380, section 2.6 */
79 #define ARES_IN6_IS_ADDR_TEREDO(a) \
80 ((*(const unsigned int *)(const void *)(&(a)->s6_addr[0]) == ntohl(0x20010000)))
81 /* RFC 3056, section 2. */
82 #define ARES_IN6_IS_ADDR_6TO4(a) \
83 (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
84 /* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
85 #define ARES_IN6_IS_ADDR_6BONE(a) \
86 (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
87
get_scope(const struct sockaddr * addr)88 static int get_scope(const struct sockaddr *addr)
89 {
90 if (addr->sa_family == AF_INET6)
91 {
92 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
93 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr))
94 {
95 return ARES_IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
96 }
97 else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
98 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr))
99 {
100 /*
101 * RFC 4291 section 2.5.3 says loopback is to be treated as having
102 * link-local scope.
103 */
104 return ARES_IPV6_ADDR_SCOPE_LINKLOCAL;
105 }
106 else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr))
107 {
108 return ARES_IPV6_ADDR_SCOPE_SITELOCAL;
109 }
110 else
111 {
112 return ARES_IPV6_ADDR_SCOPE_GLOBAL;
113 }
114 }
115 else if (addr->sa_family == AF_INET)
116 {
117 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
118 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
119 if (ARES_IN_LOOPBACK(na) || /* 127.0.0.0/8 */
120 (na & 0xffff0000) == 0xa9fe0000) /* 169.254.0.0/16 */
121 {
122 return ARES_IPV6_ADDR_SCOPE_LINKLOCAL;
123 }
124 else
125 {
126 /*
127 * RFC 6724 section 3.2. Other IPv4 addresses, including private
128 * addresses and shared addresses (100.64.0.0/10), are assigned global
129 * scope.
130 */
131 return ARES_IPV6_ADDR_SCOPE_GLOBAL;
132 }
133 }
134 else
135 {
136 /*
137 * This should never happen.
138 * Return a scope with low priority as a last resort.
139 */
140 return ARES_IPV6_ADDR_SCOPE_NODELOCAL;
141 }
142 }
143
get_label(const struct sockaddr * addr)144 static int get_label(const struct sockaddr *addr)
145 {
146 if (addr->sa_family == AF_INET)
147 {
148 return 4;
149 }
150 else if (addr->sa_family == AF_INET6)
151 {
152 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
153 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr))
154 {
155 return 0;
156 }
157 else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr))
158 {
159 return 4;
160 }
161 else if (ARES_IN6_IS_ADDR_6TO4(&addr6->sin6_addr))
162 {
163 return 2;
164 }
165 else if (ARES_IN6_IS_ADDR_TEREDO(&addr6->sin6_addr))
166 {
167 return 5;
168 }
169 else if (ARES_IN6_IS_ADDR_ULA(&addr6->sin6_addr))
170 {
171 return 13;
172 }
173 else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr))
174 {
175 return 3;
176 }
177 else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr))
178 {
179 return 11;
180 }
181 else if (ARES_IN6_IS_ADDR_6BONE(&addr6->sin6_addr))
182 {
183 return 12;
184 }
185 else
186 {
187 /* All other IPv6 addresses, including global unicast addresses. */
188 return 1;
189 }
190 }
191 else
192 {
193 /*
194 * This should never happen.
195 * Return a semi-random label as a last resort.
196 */
197 return 1;
198 }
199 }
200
201 /*
202 * Get the precedence for a given IPv4/IPv6 address.
203 * RFC 6724, section 2.1.
204 */
get_precedence(const struct sockaddr * addr)205 static int get_precedence(const struct sockaddr *addr)
206 {
207 if (addr->sa_family == AF_INET)
208 {
209 return 35;
210 }
211 else if (addr->sa_family == AF_INET6)
212 {
213 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
214 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr))
215 {
216 return 50;
217 }
218 else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr))
219 {
220 return 35;
221 }
222 else if (ARES_IN6_IS_ADDR_6TO4(&addr6->sin6_addr))
223 {
224 return 30;
225 }
226 else if (ARES_IN6_IS_ADDR_TEREDO(&addr6->sin6_addr))
227 {
228 return 5;
229 }
230 else if (ARES_IN6_IS_ADDR_ULA(&addr6->sin6_addr))
231 {
232 return 3;
233 }
234 else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
235 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
236 ARES_IN6_IS_ADDR_6BONE(&addr6->sin6_addr))
237 {
238 return 1;
239 }
240 else
241 {
242 /* All other IPv6 addresses, including global unicast addresses. */
243 return 40;
244 }
245 }
246 else
247 {
248 return 1;
249 }
250 }
251
252 /*
253 * Find number of matching initial bits between the two addresses a1 and a2.
254 */
common_prefix_len(const struct in6_addr * a1,const struct in6_addr * a2)255 static int common_prefix_len(const struct in6_addr *a1,
256 const struct in6_addr *a2)
257 {
258 const char *p1 = (const char *)a1;
259 const char *p2 = (const char *)a2;
260 unsigned i;
261 for (i = 0; i < sizeof(*a1); ++i)
262 {
263 int x, j;
264 if (p1[i] == p2[i])
265 {
266 continue;
267 }
268 x = p1[i] ^ p2[i];
269 for (j = 0; j < CHAR_BIT; ++j)
270 {
271 if (x & (1 << (CHAR_BIT - 1)))
272 {
273 return i * CHAR_BIT + j;
274 }
275 x <<= 1;
276 }
277 }
278 return sizeof(*a1) * CHAR_BIT;
279 }
280
281 /*
282 * Compare two source/destination address pairs.
283 * RFC 6724, section 6.
284 */
rfc6724_compare(const void * ptr1,const void * ptr2)285 static int rfc6724_compare(const void *ptr1, const void *ptr2)
286 {
287 const struct addrinfo_sort_elem *a1 = (const struct addrinfo_sort_elem *)ptr1;
288 const struct addrinfo_sort_elem *a2 = (const struct addrinfo_sort_elem *)ptr2;
289 int scope_src1, scope_dst1, scope_match1;
290 int scope_src2, scope_dst2, scope_match2;
291 int label_src1, label_dst1, label_match1;
292 int label_src2, label_dst2, label_match2;
293 int precedence1, precedence2;
294 int prefixlen1, prefixlen2;
295
296 /* Rule 1: Avoid unusable destinations. */
297 if (a1->has_src_addr != a2->has_src_addr)
298 {
299 return a2->has_src_addr - a1->has_src_addr;
300 }
301
302 /* Rule 2: Prefer matching scope. */
303 scope_src1 = get_scope(&a1->src_addr.sa);
304 scope_dst1 = get_scope(a1->ai->ai_addr);
305 scope_match1 = (scope_src1 == scope_dst1);
306
307 scope_src2 = get_scope(&a2->src_addr.sa);
308 scope_dst2 = get_scope(a2->ai->ai_addr);
309 scope_match2 = (scope_src2 == scope_dst2);
310
311 if (scope_match1 != scope_match2)
312 {
313 return scope_match2 - scope_match1;
314 }
315
316 /* Rule 3: Avoid deprecated addresses. */
317
318 /* Rule 4: Prefer home addresses. */
319
320 /* Rule 5: Prefer matching label. */
321 label_src1 = get_label(&a1->src_addr.sa);
322 label_dst1 = get_label(a1->ai->ai_addr);
323 label_match1 = (label_src1 == label_dst1);
324
325 label_src2 = get_label(&a2->src_addr.sa);
326 label_dst2 = get_label(a2->ai->ai_addr);
327 label_match2 = (label_src2 == label_dst2);
328
329 if (label_match1 != label_match2)
330 {
331 return label_match2 - label_match1;
332 }
333
334 /* Rule 6: Prefer higher precedence. */
335 precedence1 = get_precedence(a1->ai->ai_addr);
336 precedence2 = get_precedence(a2->ai->ai_addr);
337 if (precedence1 != precedence2)
338 {
339 return precedence2 - precedence1;
340 }
341
342 /* Rule 7: Prefer native transport. */
343
344 /* Rule 8: Prefer smaller scope. */
345 if (scope_dst1 != scope_dst2)
346 {
347 return scope_dst1 - scope_dst2;
348 }
349
350 /* Rule 9: Use longest matching prefix. */
351 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 &&
352 a2->has_src_addr && a2->ai->ai_addr->sa_family == AF_INET6)
353 {
354 const struct sockaddr_in6 *a1_src = &a1->src_addr.sa6;
355 const struct sockaddr_in6 *a1_dst =
356 (const struct sockaddr_in6 *)a1->ai->ai_addr;
357 const struct sockaddr_in6 *a2_src = &a2->src_addr.sa6;
358 const struct sockaddr_in6 *a2_dst =
359 (const struct sockaddr_in6 *)a2->ai->ai_addr;
360 prefixlen1 = common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
361 prefixlen2 = common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
362 if (prefixlen1 != prefixlen2)
363 {
364 return prefixlen2 - prefixlen1;
365 }
366 }
367
368 /*
369 * Rule 10: Leave the order unchanged.
370 * We need this since qsort() is not necessarily stable.
371 */
372 return a1->original_order - a2->original_order;
373 }
374
375 /*
376 * Find the source address that will be used if trying to connect to the given
377 * address.
378 *
379 * Returns 1 if a source address was found, 0 if the address is unreachable,
380 * and -1 if a fatal error occurred. If 0 or 1, the contents of src_addr are
381 * undefined.
382 */
find_src_addr(ares_channel channel,const struct sockaddr * addr,struct sockaddr * src_addr)383 static int find_src_addr(ares_channel channel,
384 const struct sockaddr *addr,
385 struct sockaddr *src_addr)
386 {
387 int sock;
388 int ret;
389 ares_socklen_t len;
390
391 switch (addr->sa_family)
392 {
393 case AF_INET:
394 len = sizeof(struct sockaddr_in);
395 break;
396 case AF_INET6:
397 len = sizeof(struct sockaddr_in6);
398 break;
399 default:
400 /* No known usable source address for non-INET families. */
401 return 0;
402 }
403
404 sock = ares__open_socket(channel, addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
405 if (sock == -1)
406 {
407 if (errno == EAFNOSUPPORT)
408 {
409 return 0;
410 }
411 else
412 {
413 return -1;
414 }
415 }
416
417 do
418 {
419 ret = ares__connect_socket(channel, sock, addr, len);
420 }
421 while (ret == -1 && errno == EINTR);
422
423 if (ret == -1)
424 {
425 ares__close_socket(channel, sock);
426 return 0;
427 }
428
429 if (getsockname(sock, src_addr, &len) == -1)
430 {
431 ares__close_socket(channel, sock);
432 return -1;
433 }
434 ares__close_socket(channel, sock);
435 return 1;
436 }
437
438 /*
439 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
440 * Will leave the list unchanged if an error occurs.
441 */
ares__sortaddrinfo(ares_channel channel,struct ares_addrinfo_node * list_sentinel)442 int ares__sortaddrinfo(ares_channel channel, struct ares_addrinfo_node *list_sentinel)
443 {
444 struct ares_addrinfo_node *cur;
445 int nelem = 0, i;
446 int has_src_addr;
447 struct addrinfo_sort_elem *elems;
448
449 cur = list_sentinel->ai_next;
450 while (cur)
451 {
452 ++nelem;
453 cur = cur->ai_next;
454 }
455 elems = (struct addrinfo_sort_elem *)ares_malloc(
456 nelem * sizeof(struct addrinfo_sort_elem));
457 if (!elems)
458 {
459 return ARES_ENOMEM;
460 }
461
462 /*
463 * Convert the linked list to an array that also contains the candidate
464 * source address for each destination address.
465 */
466 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next)
467 {
468 assert(cur != NULL);
469 elems[i].ai = cur;
470 elems[i].original_order = i;
471 has_src_addr = find_src_addr(channel, cur->ai_addr, &elems[i].src_addr.sa);
472 if (has_src_addr == -1)
473 {
474 ares_free(elems);
475 return ARES_ENOTFOUND;
476 }
477 elems[i].has_src_addr = has_src_addr;
478 }
479
480 /* Sort the addresses, and rearrange the linked list so it matches the sorted
481 * order. */
482 qsort((void *)elems, nelem, sizeof(struct addrinfo_sort_elem),
483 rfc6724_compare);
484
485 list_sentinel->ai_next = elems[0].ai;
486 for (i = 0; i < nelem - 1; ++i)
487 {
488 elems[i].ai->ai_next = elems[i + 1].ai;
489 }
490 elems[nelem - 1].ai->ai_next = NULL;
491
492 ares_free(elems);
493 return ARES_SUCCESS;
494 }