1 /* $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31 /*
32 * ++Copyright++ 1985, 1988, 1993
33 * -
34 * Copyright (c) 1985, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 * -
61 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
62 *
63 * Permission to use, copy, modify, and distribute this software for any
64 * purpose with or without fee is hereby granted, provided that the above
65 * copyright notice and this permission notice appear in all copies, and that
66 * the name of Digital Equipment Corporation not be used in advertising or
67 * publicity pertaining to distribution of the document or software without
68 * specific, written prior permission.
69 *
70 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
71 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
72 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
73 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
74 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
75 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
76 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
77 * SOFTWARE.
78 * -
79 * --Copyright--
80 */
81
82 /*
83 * Atsushi Onoe <onoe@sm.sony.co.jp>
84 */
85
86 #include <sys/cdefs.h>
87 __FBSDID("$FreeBSD: stable/10/lib/libc/net/name6.c 305401 2016-09-05 00:36:52Z ache $");
88
89 #include "namespace.h"
90 #include <sys/param.h>
91 #include <sys/socket.h>
92 #include <sys/time.h>
93 #include <sys/queue.h>
94 #include <netinet/in.h>
95 #ifdef INET6
96 #include <net/if.h>
97 #include <net/if_var.h>
98 #include <sys/sysctl.h>
99 #include <sys/ioctl.h>
100 #include <netinet6/in6_var.h> /* XXX */
101 #endif
102
103 #include <arpa/inet.h>
104 #include <arpa/nameser.h>
105
106 #include <errno.h>
107 #include <netdb.h>
108 #include <resolv.h>
109 #include <stdio.h>
110 #include <stdlib.h>
111 #include <string.h>
112 #include <stdarg.h>
113 #include <nsswitch.h>
114 #include <unistd.h>
115 #include "un-namespace.h"
116 #include "netdb_private.h"
117 #include "res_private.h"
118
119 #ifndef MAXALIASES
120 #define MAXALIASES 10
121 #endif
122 #ifndef MAXADDRS
123 #define MAXADDRS 20
124 #endif
125 #ifndef MAXDNAME
126 #define MAXDNAME 1025
127 #endif
128
129 #ifdef INET6
130 #define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
131 sizeof(struct in_addr))
132 #else
133 #define ADDRLEN(af) sizeof(struct in_addr)
134 #endif
135
136 #define MAPADDR(ab, ina) \
137 do { \
138 memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \
139 memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \
140 memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \
141 } while (0)
142 #define MAPADDRENABLED(flags) \
143 (((flags) & AI_V4MAPPED) || \
144 (((flags) & AI_V4MAPPED_CFG)))
145
146 union inx_addr {
147 struct in_addr in_addr;
148 #ifdef INET6
149 struct in6_addr in6_addr;
150 #endif
151 struct {
152 u_char mau_zero[10];
153 u_char mau_one[2];
154 struct in_addr mau_inaddr;
155 } map_addr_un;
156 #define map_zero map_addr_un.mau_zero
157 #define map_one map_addr_un.mau_one
158 #define map_inaddr map_addr_un.mau_inaddr
159 };
160
161 struct policyqueue {
162 TAILQ_ENTRY(policyqueue) pc_entry;
163 #ifdef INET6
164 struct in6_addrpolicy pc_policy;
165 #endif
166 };
167 TAILQ_HEAD(policyhead, policyqueue);
168
169 #define AIO_SRCFLAG_DEPRECATED 0x1
170
171 struct hp_order {
172 union {
173 struct sockaddr_storage aiou_ss;
174 struct sockaddr aiou_sa;
175 } aio_src_un;
176 #define aio_srcsa aio_src_un.aiou_sa
177 u_int32_t aio_srcflag;
178 int aio_srcscope;
179 int aio_dstscope;
180 struct policyqueue *aio_srcpolicy;
181 struct policyqueue *aio_dstpolicy;
182 union {
183 struct sockaddr_storage aiou_ss;
184 struct sockaddr aiou_sa;
185 } aio_un;
186 #define aio_sa aio_un.aiou_sa
187 int aio_matchlen;
188 char *aio_h_addr;
189 int aio_initial_sequence;
190 };
191
192 static struct hostent *_hpcopy(struct hostent *, int *);
193 static struct hostent *_hpaddr(int, const char *, void *, int *);
194 #ifdef INET6
195 static struct hostent *_hpmerge(struct hostent *, struct hostent *, int *);
196 static struct hostent *_hpmapv6(struct hostent *, int *);
197 #endif
198 static struct hostent *_hpsort(struct hostent *, res_state);
199
200 #ifdef INET6
201 static struct hostent *_hpreorder(struct hostent *);
202 static int get_addrselectpolicy(struct policyhead *);
203 static void free_addrselectpolicy(struct policyhead *);
204 static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
205 struct policyhead *);
206 static void set_source(struct hp_order *, struct policyhead *);
207 static int matchlen(struct sockaddr *, struct sockaddr *);
208 static int comp_dst(const void *, const void *);
209 static int gai_addr2scopetype(struct sockaddr *);
210 #endif
211
212 /*
213 * Functions defined in RFC2553
214 * getipnodebyname, getipnodebyaddr, freehostent
215 */
216
217 struct hostent *
getipnodebyname(const char * name,int af,int flags,int * errp)218 getipnodebyname(const char *name, int af, int flags, int *errp)
219 {
220 struct hostent *hp;
221 union inx_addr addrbuf;
222 res_state statp;
223 u_long options;
224
225 switch (af) {
226 case AF_INET:
227 #ifdef INET6
228 case AF_INET6:
229 #endif
230 break;
231 default:
232 *errp = NO_RECOVERY;
233 return NULL;
234 }
235
236 if (flags & AI_ADDRCONFIG) {
237 int s;
238
239 if ((s = _socket(af, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
240 return NULL;
241 /*
242 * TODO:
243 * Note that implementation dependent test for address
244 * configuration should be done everytime called
245 * (or apropriate interval),
246 * because addresses will be dynamically assigned or deleted.
247 */
248 _close(s);
249 }
250
251 #ifdef INET6
252 /* special case for literal address */
253 if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
254 if (af != AF_INET6) {
255 *errp = HOST_NOT_FOUND;
256 return NULL;
257 }
258 return _hpaddr(af, name, &addrbuf, errp);
259 }
260 #endif
261 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
262 if (af != AF_INET) {
263 if (MAPADDRENABLED(flags)) {
264 MAPADDR(&addrbuf, &addrbuf.in_addr);
265 } else {
266 *errp = HOST_NOT_FOUND;
267 return NULL;
268 }
269 }
270 return _hpaddr(af, name, &addrbuf, errp);
271 }
272
273
274 statp = __res_state();
275 if ((statp->options & RES_INIT) == 0) {
276 if (res_ninit(statp) < 0) {
277 *errp = NETDB_INTERNAL;
278 return NULL;
279 }
280 }
281
282 options = statp->options;
283 statp->options &= ~RES_USE_INET6;
284
285 hp = gethostbyname2(name, af);
286 hp = _hpcopy(hp, errp);
287 #ifdef INET6
288 if (af == AF_INET6)
289 hp = _hpreorder(hp);
290
291 if (af == AF_INET6 && ((flags & AI_ALL) || hp == NULL) &&
292 MAPADDRENABLED(flags)) {
293 struct hostent *hp2 = gethostbyname2(name, AF_INET);
294 if (hp == NULL)
295 if (hp2 == NULL)
296 *errp = statp->res_h_errno;
297 else
298 hp = _hpmapv6(hp2, errp);
299 else {
300 if (hp2 && strcmp(hp->h_name, hp2->h_name) == 0) {
301 struct hostent *hpb = hp;
302 hp = _hpmerge(hpb, hp2, errp);
303 freehostent(hpb);
304 }
305 }
306 }
307 #endif
308
309 if (hp == NULL)
310 *errp = statp->res_h_errno;
311
312 statp->options = options;
313 return _hpsort(hp, statp);
314 }
315
316 struct hostent *
getipnodebyaddr(const void * src,size_t len,int af,int * errp)317 getipnodebyaddr(const void *src, size_t len, int af, int *errp)
318 {
319 struct hostent *hp;
320 res_state statp;
321 u_long options;
322
323 #ifdef INET6
324 struct in6_addr addrbuf;
325 #else
326 struct in_addr addrbuf;
327 #endif
328
329 switch (af) {
330 case AF_INET:
331 if (len != sizeof(struct in_addr)) {
332 *errp = NO_RECOVERY;
333 return NULL;
334 }
335 if ((long)src & ~(sizeof(struct in_addr) - 1)) {
336 memcpy(&addrbuf, src, len);
337 src = &addrbuf;
338 }
339 if (((struct in_addr *)src)->s_addr == 0)
340 return NULL;
341 break;
342 #ifdef INET6
343 case AF_INET6:
344 if (len != sizeof(struct in6_addr)) {
345 *errp = NO_RECOVERY;
346 return NULL;
347 }
348 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/
349 memcpy(&addrbuf, src, len);
350 src = &addrbuf;
351 }
352 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
353 return NULL;
354 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
355 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
356 src = (char *)src +
357 (sizeof(struct in6_addr) - sizeof(struct in_addr));
358 af = AF_INET;
359 len = sizeof(struct in_addr);
360 }
361 break;
362 #endif
363 default:
364 *errp = NO_RECOVERY;
365 return NULL;
366 }
367
368 statp = __res_state();
369 if ((statp->options & RES_INIT) == 0) {
370 if (res_ninit(statp) < 0) {
371 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
372 return NULL;
373 }
374 }
375
376 options = statp->options;
377 statp->options &= ~RES_USE_INET6;
378
379 hp = gethostbyaddr(src, len, af);
380 if (hp == NULL)
381 *errp = statp->res_h_errno;
382
383 statp->options = options;
384 return (_hpcopy(hp, errp));
385 }
386
387 void
freehostent(struct hostent * ptr)388 freehostent(struct hostent *ptr)
389 {
390 free(ptr);
391 }
392
393 /*
394 * Private utility functions
395 */
396
397 /*
398 * _hpcopy: allocate and copy hostent structure
399 */
400 static struct hostent *
_hpcopy(struct hostent * hp,int * errp)401 _hpcopy(struct hostent *hp, int *errp)
402 {
403 struct hostent *nhp;
404 char *cp, **pp;
405 int size, addrsize;
406 int nalias = 0, naddr = 0;
407 int al_off;
408 int i;
409
410 if (hp == NULL)
411 return hp;
412
413 /* count size to be allocated */
414 size = sizeof(struct hostent);
415 if (hp->h_name != NULL)
416 size += strlen(hp->h_name) + 1;
417 if ((pp = hp->h_aliases) != NULL) {
418 for (i = 0; *pp != NULL; i++, pp++) {
419 if (**pp != '\0') {
420 size += strlen(*pp) + 1;
421 nalias++;
422 }
423 }
424 }
425 /* adjust alignment */
426 size = ALIGN(size);
427 al_off = size;
428 size += sizeof(char *) * (nalias + 1);
429 addrsize = ALIGN(hp->h_length);
430 if ((pp = hp->h_addr_list) != NULL) {
431 while (*pp++ != NULL)
432 naddr++;
433 }
434 size += addrsize * naddr;
435 size += sizeof(char *) * (naddr + 1);
436
437 /* copy */
438 if ((nhp = (struct hostent *)malloc(size)) == NULL) {
439 *errp = TRY_AGAIN;
440 return NULL;
441 }
442 cp = (char *)&nhp[1];
443 if (hp->h_name != NULL) {
444 nhp->h_name = cp;
445 strcpy(cp, hp->h_name);
446 cp += strlen(cp) + 1;
447 } else
448 nhp->h_name = NULL;
449 nhp->h_aliases = (char **)((char *)nhp + al_off);
450 if ((pp = hp->h_aliases) != NULL) {
451 for (i = 0; *pp != NULL; pp++) {
452 if (**pp != '\0') {
453 nhp->h_aliases[i++] = cp;
454 strcpy(cp, *pp);
455 cp += strlen(cp) + 1;
456 }
457 }
458 }
459 nhp->h_aliases[nalias] = NULL;
460 cp = (char *)&nhp->h_aliases[nalias + 1];
461 nhp->h_addrtype = hp->h_addrtype;
462 nhp->h_length = hp->h_length;
463 nhp->h_addr_list = (char **)cp;
464 if ((pp = hp->h_addr_list) != NULL) {
465 cp = (char *)&nhp->h_addr_list[naddr + 1];
466 for (i = 0; *pp != NULL; pp++) {
467 nhp->h_addr_list[i++] = cp;
468 memcpy(cp, *pp, hp->h_length);
469 cp += addrsize;
470 }
471 }
472 nhp->h_addr_list[naddr] = NULL;
473 return nhp;
474 }
475
476 /*
477 * _hpaddr: construct hostent structure with one address
478 */
479 static struct hostent *
_hpaddr(int af,const char * name,void * addr,int * errp)480 _hpaddr(int af, const char *name, void *addr, int *errp)
481 {
482 struct hostent *hp, hpbuf;
483 char *addrs[2];
484
485 hp = &hpbuf;
486 hp->h_name = (char *)name;
487 hp->h_aliases = NULL;
488 hp->h_addrtype = af;
489 hp->h_length = ADDRLEN(af);
490 hp->h_addr_list = addrs;
491 addrs[0] = (char *)addr;
492 addrs[1] = NULL;
493 return (_hpcopy(hp, errp));
494 }
495
496 #ifdef INET6
497 /*
498 * _hpmerge: merge 2 hostent structure, arguments will be freed
499 */
500 static struct hostent *
_hpmerge(struct hostent * hp1,struct hostent * hp2,int * errp)501 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
502 {
503 int i, j;
504 int naddr, nalias;
505 char **pp;
506 struct hostent *hp, hpbuf;
507 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
508 union inx_addr addrbuf[MAXADDRS];
509
510 if (hp1 == NULL)
511 return _hpcopy(hp2, errp);
512 if (hp2 == NULL)
513 return _hpcopy(hp1, errp);
514
515 #define HP(i) (i == 1 ? hp1 : hp2)
516 hp = &hpbuf;
517 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
518 hp->h_aliases = aliases;
519 nalias = 0;
520 for (i = 1; i <= 2; i++) {
521 if ((pp = HP(i)->h_aliases) == NULL)
522 continue;
523 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
524 /* check duplicates */
525 for (j = 0; j < nalias; j++)
526 if (strcasecmp(*pp, aliases[j]) == 0)
527 break;
528 if (j == nalias)
529 aliases[nalias++] = *pp;
530 }
531 }
532 aliases[nalias] = NULL;
533 if (hp1->h_length != hp2->h_length) {
534 hp->h_addrtype = AF_INET6;
535 hp->h_length = sizeof(struct in6_addr);
536 } else {
537 hp->h_addrtype = hp1->h_addrtype;
538 hp->h_length = hp1->h_length;
539 }
540
541 hp->h_addr_list = addrs;
542 naddr = 0;
543 for (i = 1; i <= 2; i++) {
544 if ((pp = HP(i)->h_addr_list) == NULL)
545 continue;
546 if (HP(i)->h_length == hp->h_length) {
547 while (naddr < MAXADDRS && *pp != NULL)
548 addrs[naddr++] = *pp++;
549 } else {
550 /* copy IPv4 addr as mapped IPv6 addr */
551 while (naddr < MAXADDRS && *pp != NULL) {
552 MAPADDR(&addrbuf[naddr], *pp++);
553 addrs[naddr] = (char *)&addrbuf[naddr];
554 naddr++;
555 }
556 }
557 }
558 addrs[naddr] = NULL;
559 return (_hpcopy(hp, errp));
560 }
561 #endif
562
563 /*
564 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
565 */
566 #ifdef INET6
567 static struct hostent *
_hpmapv6(struct hostent * hp,int * errp)568 _hpmapv6(struct hostent *hp, int *errp)
569 {
570 struct hostent hp6;
571
572 if (hp == NULL)
573 return NULL;
574 if (hp->h_addrtype == AF_INET6)
575 return _hpcopy(hp, errp);
576
577 memset(&hp6, 0, sizeof(struct hostent));
578 hp6.h_addrtype = AF_INET6;
579 hp6.h_length = sizeof(struct in6_addr);
580 return _hpmerge(&hp6, hp, errp);
581 }
582 #endif
583
584 /*
585 * _hpsort: sort address by sortlist
586 */
587 static struct hostent *
_hpsort(struct hostent * hp,res_state statp)588 _hpsort(struct hostent *hp, res_state statp)
589 {
590 int i, j, n;
591 u_char *ap, *sp, *mp, **pp;
592 char t;
593 char order[MAXADDRS];
594 int nsort = statp->nsort;
595
596 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
597 return hp;
598 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
599 for (j = 0; j < nsort; j++) {
600 #ifdef INET6
601 if (statp->_u._ext.ext->sort_list[j].af !=
602 hp->h_addrtype)
603 continue;
604 sp = (u_char *)&statp->_u._ext.ext->sort_list[j].addr;
605 mp = (u_char *)&statp->_u._ext.ext->sort_list[j].mask;
606 #else
607 sp = (u_char *)&statp->sort_list[j].addr;
608 mp = (u_char *)&statp->sort_list[j].mask;
609 #endif
610 for (n = 0; n < hp->h_length; n++) {
611 if ((ap[n] & mp[n]) != sp[n])
612 break;
613 }
614 if (n == hp->h_length)
615 break;
616 }
617 order[i] = j;
618 }
619 n = i;
620 pp = (u_char **)hp->h_addr_list;
621 for (i = 0; i < n - 1; i++) {
622 for (j = i + 1; j < n; j++) {
623 if (order[i] > order[j]) {
624 ap = pp[i];
625 pp[i] = pp[j];
626 pp[j] = ap;
627 t = order[i];
628 order[i] = order[j];
629 order[j] = t;
630 }
631 }
632 }
633 return hp;
634 }
635
636 #ifdef INET6
637 /*
638 * _hpreorder: sort address by default address selection
639 */
640 static struct hostent *
_hpreorder(struct hostent * hp)641 _hpreorder(struct hostent *hp)
642 {
643 struct hp_order *aio;
644 int i, n;
645 char *ap;
646 struct sockaddr *sa;
647 struct policyhead policyhead;
648
649 if (hp == NULL)
650 return hp;
651
652 switch (hp->h_addrtype) {
653 case AF_INET:
654 #ifdef INET6
655 case AF_INET6:
656 #endif
657 break;
658 default:
659 return hp;
660 }
661
662 /* count the number of addrinfo elements for sorting. */
663 for (n = 0; hp->h_addr_list[n] != NULL; n++)
664 ;
665
666 /*
667 * If the number is small enough, we can skip the reordering process.
668 */
669 if (n <= 1)
670 return hp;
671
672 /* allocate a temporary array for sort and initialization of it. */
673 if ((aio = malloc(sizeof(*aio) * n)) == NULL)
674 return hp; /* give up reordering */
675 memset(aio, 0, sizeof(*aio) * n);
676
677 /* retrieve address selection policy from the kernel */
678 TAILQ_INIT(&policyhead);
679 if (!get_addrselectpolicy(&policyhead)) {
680 /* no policy is installed into kernel, we don't sort. */
681 free(aio);
682 return hp;
683 }
684
685 for (i = 0; i < n; i++) {
686 ap = hp->h_addr_list[i];
687 aio[i].aio_h_addr = ap;
688 sa = &aio[i].aio_sa;
689 switch (hp->h_addrtype) {
690 case AF_INET:
691 sa->sa_family = AF_INET;
692 sa->sa_len = sizeof(struct sockaddr_in);
693 memcpy(&((struct sockaddr_in *)sa)->sin_addr, ap,
694 sizeof(struct in_addr));
695 break;
696 #ifdef INET6
697 case AF_INET6:
698 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
699 sa->sa_family = AF_INET;
700 sa->sa_len = sizeof(struct sockaddr_in);
701 memcpy(&((struct sockaddr_in *)sa)->sin_addr,
702 &ap[12], sizeof(struct in_addr));
703 } else {
704 sa->sa_family = AF_INET6;
705 sa->sa_len = sizeof(struct sockaddr_in6);
706 memcpy(&((struct sockaddr_in6 *)sa)->sin6_addr,
707 ap, sizeof(struct in6_addr));
708 }
709 break;
710 #endif
711 }
712 aio[i].aio_dstscope = gai_addr2scopetype(sa);
713 aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead);
714 set_source(&aio[i], &policyhead);
715 aio[i].aio_initial_sequence = i;
716 }
717
718 /* perform sorting. */
719 qsort(aio, n, sizeof(*aio), comp_dst);
720
721 /* reorder the h_addr_list. */
722 for (i = 0; i < n; i++)
723 hp->h_addr_list[i] = aio[i].aio_h_addr;
724
725 /* cleanup and return */
726 free(aio);
727 free_addrselectpolicy(&policyhead);
728 return hp;
729 }
730
731 static int
get_addrselectpolicy(struct policyhead * head)732 get_addrselectpolicy(struct policyhead *head)
733 {
734 #ifdef INET6
735 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
736 size_t l;
737 char *buf;
738 struct in6_addrpolicy *pol, *ep;
739
740 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
741 return (0);
742 if ((buf = malloc(l)) == NULL)
743 return (0);
744 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
745 free(buf);
746 return (0);
747 }
748
749 ep = (struct in6_addrpolicy *)(buf + l);
750 for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
751 struct policyqueue *new;
752
753 if ((new = malloc(sizeof(*new))) == NULL) {
754 free_addrselectpolicy(head); /* make the list empty */
755 break;
756 }
757 new->pc_policy = *pol;
758 TAILQ_INSERT_TAIL(head, new, pc_entry);
759 }
760
761 free(buf);
762 return (1);
763 #else
764 return (0);
765 #endif
766 }
767
768 static void
free_addrselectpolicy(struct policyhead * head)769 free_addrselectpolicy(struct policyhead *head)
770 {
771 struct policyqueue *ent, *nent;
772
773 for (ent = TAILQ_FIRST(head); ent; ent = nent) {
774 nent = TAILQ_NEXT(ent, pc_entry);
775 TAILQ_REMOVE(head, ent, pc_entry);
776 free(ent);
777 }
778 }
779
780 static struct policyqueue *
match_addrselectpolicy(struct sockaddr * addr,struct policyhead * head)781 match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
782 {
783 #ifdef INET6
784 struct policyqueue *ent, *bestent = NULL;
785 struct in6_addrpolicy *pol;
786 int matchlen, bestmatchlen = -1;
787 u_char *mp, *ep, *k, *p, m;
788 struct sockaddr_in6 key;
789
790 switch(addr->sa_family) {
791 case AF_INET6:
792 key = *(struct sockaddr_in6 *)addr;
793 break;
794 case AF_INET:
795 /* convert the address into IPv4-mapped IPv6 address. */
796 memset(&key, 0, sizeof(key));
797 key.sin6_family = AF_INET6;
798 key.sin6_len = sizeof(key);
799 _map_v4v6_address(
800 (char *)&((struct sockaddr_in *)addr)->sin_addr,
801 (char *)&key.sin6_addr);
802 break;
803 default:
804 return(NULL);
805 }
806
807 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
808 pol = &ent->pc_policy;
809 matchlen = 0;
810
811 mp = (u_char *)&pol->addrmask.sin6_addr;
812 ep = mp + 16; /* XXX: scope field? */
813 k = (u_char *)&key.sin6_addr;
814 p = (u_char *)&pol->addr.sin6_addr;
815 for (; mp < ep && *mp; mp++, k++, p++) {
816 m = *mp;
817 if ((*k & m) != *p)
818 goto next; /* not match */
819 if (m == 0xff) /* short cut for a typical case */
820 matchlen += 8;
821 else {
822 while (m >= 0x80) {
823 matchlen++;
824 m <<= 1;
825 }
826 }
827 }
828
829 /* matched. check if this is better than the current best. */
830 if (matchlen > bestmatchlen) {
831 bestent = ent;
832 bestmatchlen = matchlen;
833 }
834
835 next:
836 continue;
837 }
838
839 return(bestent);
840 #else
841 return(NULL);
842 #endif
843
844 }
845
846 static void
set_source(struct hp_order * aio,struct policyhead * ph)847 set_source(struct hp_order *aio, struct policyhead *ph)
848 {
849 struct sockaddr_storage ss = aio->aio_un.aiou_ss;
850 socklen_t srclen;
851 int s;
852
853 /* set unspec ("no source is available"), just in case */
854 aio->aio_srcsa.sa_family = AF_UNSPEC;
855 aio->aio_srcscope = -1;
856
857 switch(ss.ss_family) {
858 case AF_INET:
859 ((struct sockaddr_in *)&ss)->sin_port = htons(1);
860 break;
861 #ifdef INET6
862 case AF_INET6:
863 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(1);
864 break;
865 #endif
866 default: /* ignore unsupported AFs explicitly */
867 return;
868 }
869
870 /* open a socket to get the source address for the given dst */
871 if ((s = _socket(ss.ss_family, SOCK_DGRAM | SOCK_CLOEXEC,
872 IPPROTO_UDP)) < 0)
873 return; /* give up */
874 if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0)
875 goto cleanup;
876 srclen = ss.ss_len;
877 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
878 aio->aio_srcsa.sa_family = AF_UNSPEC;
879 goto cleanup;
880 }
881 aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
882 aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
883 aio->aio_matchlen = matchlen(&aio->aio_srcsa, (struct sockaddr *)&ss);
884 #ifdef INET6
885 if (ss.ss_family == AF_INET6) {
886 struct in6_ifreq ifr6;
887 u_int32_t flags6;
888
889 memset(&ifr6, 0, sizeof(ifr6));
890 memcpy(&ifr6.ifr_addr, &ss, ss.ss_len);
891 if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
892 flags6 = ifr6.ifr_ifru.ifru_flags6;
893 if ((flags6 & IN6_IFF_DEPRECATED))
894 aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
895 }
896 }
897 #endif
898
899 cleanup:
900 _close(s);
901 return;
902 }
903
904 static int
matchlen(struct sockaddr * src,struct sockaddr * dst)905 matchlen(struct sockaddr *src, struct sockaddr *dst)
906 {
907 int match = 0;
908 u_char *s, *d;
909 u_char *lim, r;
910 int addrlen;
911
912 switch (src->sa_family) {
913 #ifdef INET6
914 case AF_INET6:
915 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
916 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
917 addrlen = sizeof(struct in6_addr);
918 lim = s + addrlen;
919 break;
920 #endif
921 case AF_INET:
922 s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
923 d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
924 addrlen = sizeof(struct in_addr);
925 lim = s + addrlen;
926 break;
927 default:
928 return(0);
929 }
930
931 while (s < lim)
932 if ((r = (*d++ ^ *s++)) != 0) {
933 while ((r & 0x80) == 0) {
934 match++;
935 r <<= 1;
936 }
937 break;
938 } else
939 match += 8;
940 return(match);
941 }
942
943 static int
comp_dst(const void * arg1,const void * arg2)944 comp_dst(const void *arg1, const void *arg2)
945 {
946 const struct hp_order *dst1 = arg1, *dst2 = arg2;
947
948 /*
949 * Rule 1: Avoid unusable destinations.
950 * XXX: we currently do not consider if an appropriate route exists.
951 */
952 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
953 dst2->aio_srcsa.sa_family == AF_UNSPEC) {
954 return(-1);
955 }
956 if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
957 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
958 return(1);
959 }
960
961 /* Rule 2: Prefer matching scope. */
962 if (dst1->aio_dstscope == dst1->aio_srcscope &&
963 dst2->aio_dstscope != dst2->aio_srcscope) {
964 return(-1);
965 }
966 if (dst1->aio_dstscope != dst1->aio_srcscope &&
967 dst2->aio_dstscope == dst2->aio_srcscope) {
968 return(1);
969 }
970
971 /* Rule 3: Avoid deprecated addresses. */
972 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
973 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
974 if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
975 (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
976 return(-1);
977 }
978 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
979 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
980 return(1);
981 }
982 }
983
984 /* Rule 4: Prefer home addresses. */
985 /* XXX: not implemented yet */
986
987 /* Rule 5: Prefer matching label. */
988 #ifdef INET6
989 if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
990 dst1->aio_srcpolicy->pc_policy.label ==
991 dst1->aio_dstpolicy->pc_policy.label &&
992 (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
993 dst2->aio_srcpolicy->pc_policy.label !=
994 dst2->aio_dstpolicy->pc_policy.label)) {
995 return(-1);
996 }
997 if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
998 dst2->aio_srcpolicy->pc_policy.label ==
999 dst2->aio_dstpolicy->pc_policy.label &&
1000 (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
1001 dst1->aio_srcpolicy->pc_policy.label !=
1002 dst1->aio_dstpolicy->pc_policy.label)) {
1003 return(1);
1004 }
1005 #endif
1006
1007 /* Rule 6: Prefer higher precedence. */
1008 #ifdef INET6
1009 if (dst1->aio_dstpolicy &&
1010 (dst2->aio_dstpolicy == NULL ||
1011 dst1->aio_dstpolicy->pc_policy.preced >
1012 dst2->aio_dstpolicy->pc_policy.preced)) {
1013 return(-1);
1014 }
1015 if (dst2->aio_dstpolicy &&
1016 (dst1->aio_dstpolicy == NULL ||
1017 dst2->aio_dstpolicy->pc_policy.preced >
1018 dst1->aio_dstpolicy->pc_policy.preced)) {
1019 return(1);
1020 }
1021 #endif
1022
1023 /* Rule 7: Prefer native transport. */
1024 /* XXX: not implemented yet */
1025
1026 /* Rule 8: Prefer smaller scope. */
1027 if (dst1->aio_dstscope >= 0 &&
1028 dst1->aio_dstscope < dst2->aio_dstscope) {
1029 return(-1);
1030 }
1031 if (dst2->aio_dstscope >= 0 &&
1032 dst2->aio_dstscope < dst1->aio_dstscope) {
1033 return(1);
1034 }
1035
1036 /*
1037 * Rule 9: Use longest matching prefix.
1038 * We compare the match length in a same AF only.
1039 */
1040 if (dst1->aio_sa.sa_family == dst2->aio_sa.sa_family) {
1041 if (dst1->aio_matchlen > dst2->aio_matchlen) {
1042 return(-1);
1043 }
1044 if (dst1->aio_matchlen < dst2->aio_matchlen) {
1045 return(1);
1046 }
1047 }
1048
1049 /* Rule 10: Otherwise, leave the order unchanged. */
1050
1051 /*
1052 * Note that qsort is unstable; so, we can't return zero and
1053 * expect the order to be unchanged.
1054 * That also means we can't depend on the current position of
1055 * dst2 being after dst1. We must enforce the initial order
1056 * with an explicit compare on the original position.
1057 * The qsort specification requires that "When the same objects
1058 * (consisting of width bytes, irrespective of their current
1059 * positions in the array) are passed more than once to the
1060 * comparison function, the results shall be consistent with one
1061 * another."
1062 * In other words, If A < B, then we must also return B > A.
1063 */
1064 if (dst2->aio_initial_sequence < dst1->aio_initial_sequence)
1065 return(1);
1066
1067 return(-1);
1068 }
1069
1070 /*
1071 * Copy from scope.c.
1072 * XXX: we should standardize the functions and link them as standard
1073 * library.
1074 */
1075 static int
gai_addr2scopetype(struct sockaddr * sa)1076 gai_addr2scopetype(struct sockaddr *sa)
1077 {
1078 #ifdef INET6
1079 struct sockaddr_in6 *sa6;
1080 #endif
1081 struct sockaddr_in *sa4;
1082
1083 switch(sa->sa_family) {
1084 #ifdef INET6
1085 case AF_INET6:
1086 sa6 = (struct sockaddr_in6 *)sa;
1087 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1088 /* just use the scope field of the multicast address */
1089 return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1090 }
1091 /*
1092 * Unicast addresses: map scope type to corresponding scope
1093 * value defined for multcast addresses.
1094 * XXX: hardcoded scope type values are bad...
1095 */
1096 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1097 return(1); /* node local scope */
1098 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1099 return(2); /* link-local scope */
1100 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1101 return(5); /* site-local scope */
1102 return(14); /* global scope */
1103 break;
1104 #endif
1105 case AF_INET:
1106 /*
1107 * IPv4 pseudo scoping according to RFC 3484.
1108 */
1109 sa4 = (struct sockaddr_in *)sa;
1110 /* IPv4 autoconfiguration addresses have link-local scope. */
1111 if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1112 ((u_char *)&sa4->sin_addr)[1] == 254)
1113 return(2);
1114 /* Private addresses have site-local scope. */
1115 if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1116 (((u_char *)&sa4->sin_addr)[0] == 172 &&
1117 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1118 (((u_char *)&sa4->sin_addr)[0] == 192 &&
1119 ((u_char *)&sa4->sin_addr)[1] == 168))
1120 return(14); /* XXX: It should be 5 unless NAT */
1121 /* Loopback addresses have link-local scope. */
1122 if (((u_char *)&sa4->sin_addr)[0] == 127)
1123 return(2);
1124 return(14);
1125 break;
1126 default:
1127 errno = EAFNOSUPPORT; /* is this a good error? */
1128 return(-1);
1129 }
1130 }
1131 #endif
1132