1 /* $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 * ++Copyright++ 1985, 1988, 1993
34 * Copyright (c) 1985, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
61 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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
83 * Atsushi Onoe <onoe@sm.sony.co.jp>
86 #include <sys/cdefs.h>
87 __FBSDID("$FreeBSD$");
89 #include "namespace.h"
90 #include <sys/param.h>
91 #include <sys/socket.h>
93 #include <sys/queue.h>
94 #include <netinet/in.h>
97 #include <net/if_var.h>
98 #include <sys/sysctl.h>
99 #include <sys/ioctl.h>
100 #include <netinet6/in6_var.h> /* XXX */
103 #include <arpa/inet.h>
104 #include <arpa/nameser.h>
113 #include <nsswitch.h>
115 #include "un-namespace.h"
116 #include "netdb_private.h"
117 #include "res_private.h"
120 #define MAXALIASES 10
126 #define MAXDNAME 1025
130 #define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
131 sizeof(struct in_addr))
133 #define ADDRLEN(af) sizeof(struct in_addr)
136 #define MAPADDR(ab, ina) \
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)); \
142 #define MAPADDRENABLED(flags) \
143 (((flags) & AI_V4MAPPED) || \
144 (((flags) & AI_V4MAPPED_CFG)))
147 struct in_addr in_addr;
149 struct in6_addr in6_addr;
154 struct in_addr mau_inaddr;
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
162 TAILQ_ENTRY(policyqueue) pc_entry;
164 struct in6_addrpolicy pc_policy;
167 TAILQ_HEAD(policyhead, policyqueue);
169 #define AIO_SRCFLAG_DEPRECATED 0x1
173 struct sockaddr_storage aiou_ss;
174 struct sockaddr aiou_sa;
176 #define aio_srcsa aio_src_un.aiou_sa
177 u_int32_t aio_srcflag;
180 struct policyqueue *aio_srcpolicy;
181 struct policyqueue *aio_dstpolicy;
183 struct sockaddr_storage aiou_ss;
184 struct sockaddr aiou_sa;
186 #define aio_sa aio_un.aiou_sa
191 static struct hostent *_hpcopy(struct hostent *, int *);
192 static struct hostent *_hpaddr(int, const char *, void *, int *);
194 static struct hostent *_hpmerge(struct hostent *, struct hostent *, int *);
195 static struct hostent *_hpmapv6(struct hostent *, int *);
197 static struct hostent *_hpsort(struct hostent *, res_state);
200 static struct hostent *_hpreorder(struct hostent *);
201 static int get_addrselectpolicy(struct policyhead *);
202 static void free_addrselectpolicy(struct policyhead *);
203 static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
204 struct policyhead *);
205 static void set_source(struct hp_order *, struct policyhead *);
206 static int matchlen(struct sockaddr *, struct sockaddr *);
207 static int comp_dst(const void *, const void *);
208 static int gai_addr2scopetype(struct sockaddr *);
212 * Functions defined in RFC2553
213 * getipnodebyname, getipnodebyaddr, freehostent
217 getipnodebyname(const char *name, int af, int flags, int *errp)
220 union inx_addr addrbuf;
235 if (flags & AI_ADDRCONFIG) {
238 if ((s = _socket(af, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
242 * Note that implementation dependent test for address
243 * configuration should be done everytime called
244 * (or apropriate interval),
245 * because addresses will be dynamically assigned or deleted.
251 /* special case for literal address */
252 if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
253 if (af != AF_INET6) {
254 *errp = HOST_NOT_FOUND;
257 return _hpaddr(af, name, &addrbuf, errp);
260 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
262 if (MAPADDRENABLED(flags)) {
263 MAPADDR(&addrbuf, &addrbuf.in_addr);
265 *errp = HOST_NOT_FOUND;
269 return _hpaddr(af, name, &addrbuf, errp);
273 statp = __res_state();
274 if ((statp->options & RES_INIT) == 0) {
275 if (res_ninit(statp) < 0) {
276 *errp = NETDB_INTERNAL;
281 options = statp->options;
282 statp->options &= ~RES_USE_INET6;
284 hp = gethostbyname2(name, af);
285 hp = _hpcopy(hp, errp);
290 if (af == AF_INET6 && ((flags & AI_ALL) || hp == NULL) &&
291 MAPADDRENABLED(flags)) {
292 struct hostent *hp2 = gethostbyname2(name, AF_INET);
295 *errp = statp->res_h_errno;
297 hp = _hpmapv6(hp2, errp);
299 if (hp2 && strcmp(hp->h_name, hp2->h_name) == 0) {
300 struct hostent *hpb = hp;
301 hp = _hpmerge(hpb, hp2, errp);
309 *errp = statp->res_h_errno;
311 statp->options = options;
312 return _hpsort(hp, statp);
316 getipnodebyaddr(const void *src, size_t len, int af, int *errp)
323 struct in6_addr addrbuf;
325 struct in_addr addrbuf;
330 if (len != sizeof(struct in_addr)) {
334 if ((long)src & ~(sizeof(struct in_addr) - 1)) {
335 memcpy(&addrbuf, src, len);
338 if (((struct in_addr *)src)->s_addr == 0)
343 if (len != sizeof(struct in6_addr)) {
347 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/
348 memcpy(&addrbuf, src, len);
351 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
353 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
354 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
356 (sizeof(struct in6_addr) - sizeof(struct in_addr));
358 len = sizeof(struct in_addr);
367 statp = __res_state();
368 if ((statp->options & RES_INIT) == 0) {
369 if (res_ninit(statp) < 0) {
370 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
375 options = statp->options;
376 statp->options &= ~RES_USE_INET6;
378 hp = gethostbyaddr(src, len, af);
380 *errp = statp->res_h_errno;
382 statp->options = options;
383 return (_hpcopy(hp, errp));
387 freehostent(struct hostent *ptr)
393 * Private utility functions
397 * _hpcopy: allocate and copy hostent structure
399 static struct hostent *
400 _hpcopy(struct hostent *hp, int *errp)
405 int nalias = 0, naddr = 0;
412 /* count size to be allocated */
413 size = sizeof(struct hostent);
414 if (hp->h_name != NULL)
415 size += strlen(hp->h_name) + 1;
416 if ((pp = hp->h_aliases) != NULL) {
417 for (i = 0; *pp != NULL; i++, pp++) {
419 size += strlen(*pp) + 1;
424 /* adjust alignment */
427 size += sizeof(char *) * (nalias + 1);
428 addrsize = ALIGN(hp->h_length);
429 if ((pp = hp->h_addr_list) != NULL) {
430 while (*pp++ != NULL)
433 size += addrsize * naddr;
434 size += sizeof(char *) * (naddr + 1);
437 if ((nhp = (struct hostent *)malloc(size)) == NULL) {
441 cp = (char *)&nhp[1];
442 if (hp->h_name != NULL) {
444 strcpy(cp, hp->h_name);
445 cp += strlen(cp) + 1;
448 nhp->h_aliases = (char **)((char *)nhp + al_off);
449 if ((pp = hp->h_aliases) != NULL) {
450 for (i = 0; *pp != NULL; pp++) {
452 nhp->h_aliases[i++] = cp;
454 cp += strlen(cp) + 1;
458 nhp->h_aliases[nalias] = NULL;
459 cp = (char *)&nhp->h_aliases[nalias + 1];
460 nhp->h_addrtype = hp->h_addrtype;
461 nhp->h_length = hp->h_length;
462 nhp->h_addr_list = (char **)cp;
463 if ((pp = hp->h_addr_list) != NULL) {
464 cp = (char *)&nhp->h_addr_list[naddr + 1];
465 for (i = 0; *pp != NULL; pp++) {
466 nhp->h_addr_list[i++] = cp;
467 memcpy(cp, *pp, hp->h_length);
471 nhp->h_addr_list[naddr] = NULL;
476 * _hpaddr: construct hostent structure with one address
478 static struct hostent *
479 _hpaddr(int af, const char *name, void *addr, int *errp)
481 struct hostent *hp, hpbuf;
485 hp->h_name = (char *)name;
486 hp->h_aliases = NULL;
488 hp->h_length = ADDRLEN(af);
489 hp->h_addr_list = addrs;
490 addrs[0] = (char *)addr;
492 return (_hpcopy(hp, errp));
497 * _hpmerge: merge 2 hostent structure, arguments will be freed
499 static struct hostent *
500 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
505 struct hostent *hp, hpbuf;
506 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
507 union inx_addr addrbuf[MAXADDRS];
510 return _hpcopy(hp2, errp);
512 return _hpcopy(hp1, errp);
514 #define HP(i) (i == 1 ? hp1 : hp2)
516 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
517 hp->h_aliases = aliases;
519 for (i = 1; i <= 2; i++) {
520 if ((pp = HP(i)->h_aliases) == NULL)
522 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
523 /* check duplicates */
524 for (j = 0; j < nalias; j++)
525 if (strcasecmp(*pp, aliases[j]) == 0)
528 aliases[nalias++] = *pp;
531 aliases[nalias] = NULL;
532 if (hp1->h_length != hp2->h_length) {
533 hp->h_addrtype = AF_INET6;
534 hp->h_length = sizeof(struct in6_addr);
536 hp->h_addrtype = hp1->h_addrtype;
537 hp->h_length = hp1->h_length;
540 hp->h_addr_list = addrs;
542 for (i = 1; i <= 2; i++) {
543 if ((pp = HP(i)->h_addr_list) == NULL)
545 if (HP(i)->h_length == hp->h_length) {
546 while (naddr < MAXADDRS && *pp != NULL)
547 addrs[naddr++] = *pp++;
549 /* copy IPv4 addr as mapped IPv6 addr */
550 while (naddr < MAXADDRS && *pp != NULL) {
551 MAPADDR(&addrbuf[naddr], *pp++);
552 addrs[naddr] = (char *)&addrbuf[naddr];
558 return (_hpcopy(hp, errp));
563 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
566 static struct hostent *
567 _hpmapv6(struct hostent *hp, int *errp)
573 if (hp->h_addrtype == AF_INET6)
574 return _hpcopy(hp, errp);
576 memset(&hp6, 0, sizeof(struct hostent));
577 hp6.h_addrtype = AF_INET6;
578 hp6.h_length = sizeof(struct in6_addr);
579 return _hpmerge(&hp6, hp, errp);
584 * _hpsort: sort address by sortlist
586 static struct hostent *
587 _hpsort(struct hostent *hp, res_state statp)
590 u_char *ap, *sp, *mp, **pp;
592 char order[MAXADDRS];
593 int nsort = statp->nsort;
595 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
597 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
598 for (j = 0; j < nsort; j++) {
600 if (statp->_u._ext.ext->sort_list[j].af !=
603 sp = (u_char *)&statp->_u._ext.ext->sort_list[j].addr;
604 mp = (u_char *)&statp->_u._ext.ext->sort_list[j].mask;
606 sp = (u_char *)&statp->sort_list[j].addr;
607 mp = (u_char *)&statp->sort_list[j].mask;
609 for (n = 0; n < hp->h_length; n++) {
610 if ((ap[n] & mp[n]) != sp[n])
613 if (n == hp->h_length)
619 pp = (u_char **)hp->h_addr_list;
620 for (i = 0; i < n - 1; i++) {
621 for (j = i + 1; j < n; j++) {
622 if (order[i] > order[j]) {
637 * _hpreorder: sort address by default address selection
639 static struct hostent *
640 _hpreorder(struct hostent *hp)
642 struct hp_order *aio;
646 struct policyhead policyhead;
651 switch (hp->h_addrtype) {
658 free_addrselectpolicy(&policyhead);
662 /* count the number of addrinfo elements for sorting. */
663 for (n = 0; hp->h_addr_list[n] != NULL; n++)
667 * If the number is small enough, we can skip the reordering process.
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);
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. */
685 for (i = 0; i < n; i++) {
686 ap = hp->h_addr_list[i];
687 aio[i].aio_h_addr = ap;
689 switch (hp->h_addrtype) {
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));
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));
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));
712 aio[i].aio_dstscope = gai_addr2scopetype(sa);
713 aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead);
714 set_source(&aio[i], &policyhead);
717 /* perform sorting. */
718 qsort(aio, n, sizeof(*aio), comp_dst);
720 /* reorder the h_addr_list. */
721 for (i = 0; i < n; i++)
722 hp->h_addr_list[i] = aio[i].aio_h_addr;
724 /* cleanup and return */
726 free_addrselectpolicy(&policyhead);
731 get_addrselectpolicy(struct policyhead *head)
734 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
737 struct in6_addrpolicy *pol, *ep;
739 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
741 if ((buf = malloc(l)) == NULL)
743 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
748 ep = (struct in6_addrpolicy *)(buf + l);
749 for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
750 struct policyqueue *new;
752 if ((new = malloc(sizeof(*new))) == NULL) {
753 free_addrselectpolicy(head); /* make the list empty */
756 new->pc_policy = *pol;
757 TAILQ_INSERT_TAIL(head, new, pc_entry);
768 free_addrselectpolicy(struct policyhead *head)
770 struct policyqueue *ent, *nent;
772 for (ent = TAILQ_FIRST(head); ent; ent = nent) {
773 nent = TAILQ_NEXT(ent, pc_entry);
774 TAILQ_REMOVE(head, ent, pc_entry);
779 static struct policyqueue *
780 match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
783 struct policyqueue *ent, *bestent = NULL;
784 struct in6_addrpolicy *pol;
785 int matchlen, bestmatchlen = -1;
786 u_char *mp, *ep, *k, *p, m;
787 struct sockaddr_in6 key;
789 switch(addr->sa_family) {
791 key = *(struct sockaddr_in6 *)addr;
794 /* convert the address into IPv4-mapped IPv6 address. */
795 memset(&key, 0, sizeof(key));
796 key.sin6_family = AF_INET6;
797 key.sin6_len = sizeof(key);
798 key.sin6_addr.s6_addr[10] = 0xff;
799 key.sin6_addr.s6_addr[11] = 0xff;
800 memcpy(&key.sin6_addr.s6_addr[12],
801 &((struct sockaddr_in *)addr)->sin_addr, 4);
807 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
808 pol = &ent->pc_policy;
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++) {
818 goto next; /* not match */
819 if (m == 0xff) /* short cut for a typical case */
829 /* matched. check if this is better than the current best. */
830 if (matchlen > bestmatchlen) {
832 bestmatchlen = matchlen;
847 set_source(struct hp_order *aio, struct policyhead *ph)
849 struct sockaddr_storage ss = aio->aio_un.aiou_ss;
853 /* set unspec ("no source is available"), just in case */
854 aio->aio_srcsa.sa_family = AF_UNSPEC;
855 aio->aio_srcscope = -1;
857 switch(ss.ss_family) {
859 ((struct sockaddr_in *)&ss)->sin_port = htons(1);
863 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(1);
866 default: /* ignore unsupported AFs explicitly */
870 /* open a socket to get the source address for the given dst */
871 if ((s = _socket(ss.ss_family, SOCK_DGRAM | SOCK_CLOEXEC,
873 return; /* give up */
874 if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0)
877 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
878 aio->aio_srcsa.sa_family = AF_UNSPEC;
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);
885 if (ss.ss_family == AF_INET6) {
886 struct in6_ifreq ifr6;
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;
905 matchlen(struct sockaddr *src, struct sockaddr *dst)
912 switch (src->sa_family) {
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);
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);
932 if ((r = (*d++ ^ *s++)) != 0) {
933 while (r < addrlen * 8) {
944 comp_dst(const void *arg1, const void *arg2)
946 const struct hp_order *dst1 = arg1, *dst2 = arg2;
949 * Rule 1: Avoid unusable destinations.
950 * XXX: we currently do not consider if an appropriate route exists.
952 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
953 dst2->aio_srcsa.sa_family == AF_UNSPEC) {
956 if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
957 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
961 /* Rule 2: Prefer matching scope. */
962 if (dst1->aio_dstscope == dst1->aio_srcscope &&
963 dst2->aio_dstscope != dst2->aio_srcscope) {
966 if (dst1->aio_dstscope != dst1->aio_srcscope &&
967 dst2->aio_dstscope == dst2->aio_srcscope) {
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)) {
978 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
979 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
984 /* Rule 4: Prefer home addresses. */
985 /* XXX: not implemented yet */
987 /* Rule 5: Prefer matching label. */
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)) {
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)) {
1007 /* Rule 6: Prefer higher precedence. */
1009 if (dst1->aio_dstpolicy &&
1010 (dst2->aio_dstpolicy == NULL ||
1011 dst1->aio_dstpolicy->pc_policy.preced >
1012 dst2->aio_dstpolicy->pc_policy.preced)) {
1015 if (dst2->aio_dstpolicy &&
1016 (dst1->aio_dstpolicy == NULL ||
1017 dst2->aio_dstpolicy->pc_policy.preced >
1018 dst1->aio_dstpolicy->pc_policy.preced)) {
1023 /* Rule 7: Prefer native transport. */
1024 /* XXX: not implemented yet */
1026 /* Rule 8: Prefer smaller scope. */
1027 if (dst1->aio_dstscope >= 0 &&
1028 dst1->aio_dstscope < dst2->aio_dstscope) {
1031 if (dst2->aio_dstscope >= 0 &&
1032 dst2->aio_dstscope < dst1->aio_dstscope) {
1037 * Rule 9: Use longest matching prefix.
1038 * We compare the match length in a same AF only.
1040 if (dst1->aio_sa.sa_family == dst2->aio_sa.sa_family) {
1041 if (dst1->aio_matchlen > dst2->aio_matchlen) {
1044 if (dst1->aio_matchlen < dst2->aio_matchlen) {
1049 /* Rule 10: Otherwise, leave the order unchanged. */
1054 * Copy from scope.c.
1055 * XXX: we should standardize the functions and link them as standard
1059 gai_addr2scopetype(struct sockaddr *sa)
1062 struct sockaddr_in6 *sa6;
1064 struct sockaddr_in *sa4;
1066 switch(sa->sa_family) {
1069 sa6 = (struct sockaddr_in6 *)sa;
1070 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1071 /* just use the scope field of the multicast address */
1072 return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1075 * Unicast addresses: map scope type to corresponding scope
1076 * value defined for multcast addresses.
1077 * XXX: hardcoded scope type values are bad...
1079 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1080 return(1); /* node local scope */
1081 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1082 return(2); /* link-local scope */
1083 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1084 return(5); /* site-local scope */
1085 return(14); /* global scope */
1090 * IPv4 pseudo scoping according to RFC 3484.
1092 sa4 = (struct sockaddr_in *)sa;
1093 /* IPv4 autoconfiguration addresses have link-local scope. */
1094 if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1095 ((u_char *)&sa4->sin_addr)[1] == 254)
1097 /* Private addresses have site-local scope. */
1098 if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1099 (((u_char *)&sa4->sin_addr)[0] == 172 &&
1100 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1101 (((u_char *)&sa4->sin_addr)[0] == 192 &&
1102 ((u_char *)&sa4->sin_addr)[1] == 168))
1103 return(14); /* XXX: It should be 5 unless NAT */
1104 /* Loopback addresses have link-local scope. */
1105 if (((u_char *)&sa4->sin_addr)[0] == 127)
1110 errno = EAFNOSUPPORT; /* is this a good error? */