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. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
67 * Permission to use, copy, modify, and distribute this software for any
68 * purpose with or without fee is hereby granted, provided that the above
69 * copyright notice and this permission notice appear in all copies, and that
70 * the name of Digital Equipment Corporation not be used in advertising or
71 * publicity pertaining to distribution of the document or software without
72 * specific, written prior permission.
74 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
75 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
76 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
77 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
78 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
79 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
80 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
87 * Atsushi Onoe <onoe@sm.sony.co.jp>
90 #include <sys/cdefs.h>
91 __FBSDID("$FreeBSD$");
93 #include "namespace.h"
94 #include <sys/param.h>
95 #include <sys/socket.h>
97 #include <sys/queue.h>
98 #include <netinet/in.h>
101 #include <net/if_var.h>
102 #include <sys/sysctl.h>
103 #include <sys/ioctl.h>
104 #include <netinet6/in6_var.h> /* XXX */
107 #include <arpa/inet.h>
108 #include <arpa/nameser.h>
117 #include <nsswitch.h>
119 #include "un-namespace.h"
120 #include "netdb_private.h"
121 #include "res_private.h"
124 #define MAXALIASES 10
130 #define MAXDNAME 1025
134 #define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
135 sizeof(struct in_addr))
137 #define ADDRLEN(af) sizeof(struct in_addr)
140 #define MAPADDR(ab, ina) \
142 memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \
143 memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \
144 memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \
146 #define MAPADDRENABLED(flags) \
147 (((flags) & AI_V4MAPPED) || \
148 (((flags) & AI_V4MAPPED_CFG)))
151 struct in_addr in_addr;
153 struct in6_addr in6_addr;
158 struct in_addr mau_inaddr;
160 #define map_zero map_addr_un.mau_zero
161 #define map_one map_addr_un.mau_one
162 #define map_inaddr map_addr_un.mau_inaddr
166 TAILQ_ENTRY(policyqueue) pc_entry;
168 struct in6_addrpolicy pc_policy;
171 TAILQ_HEAD(policyhead, policyqueue);
173 #define AIO_SRCFLAG_DEPRECATED 0x1
177 struct sockaddr_storage aiou_ss;
178 struct sockaddr aiou_sa;
180 #define aio_srcsa aio_src_un.aiou_sa
181 u_int32_t aio_srcflag;
184 struct policyqueue *aio_srcpolicy;
185 struct policyqueue *aio_dstpolicy;
187 struct sockaddr_storage aiou_ss;
188 struct sockaddr aiou_sa;
190 #define aio_sa aio_un.aiou_sa
195 static struct hostent *_hpcopy(struct hostent *, int *);
196 static struct hostent *_hpaddr(int, const char *, void *, int *);
198 static struct hostent *_hpmerge(struct hostent *, struct hostent *, int *);
199 static struct hostent *_hpmapv6(struct hostent *, int *);
201 static struct hostent *_hpsort(struct hostent *, res_state);
204 static struct hostent *_hpreorder(struct hostent *);
205 static int get_addrselectpolicy(struct policyhead *);
206 static void free_addrselectpolicy(struct policyhead *);
207 static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
208 struct policyhead *);
209 static void set_source(struct hp_order *, struct policyhead *);
210 static int matchlen(struct sockaddr *, struct sockaddr *);
211 static int comp_dst(const void *, const void *);
212 static int gai_addr2scopetype(struct sockaddr *);
216 * Functions defined in RFC2553
217 * getipnodebyname, getipnodebyaddr, freehostent
221 getipnodebyname(const char *name, int af, int flags, int *errp)
224 union inx_addr addrbuf;
239 if (flags & AI_ADDRCONFIG) {
242 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
246 * Note that implementation dependent test for address
247 * configuration should be done everytime called
248 * (or apropriate interval),
249 * because addresses will be dynamically assigned or deleted.
255 /* special case for literal address */
256 if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
257 if (af != AF_INET6) {
258 *errp = HOST_NOT_FOUND;
261 return _hpaddr(af, name, &addrbuf, errp);
264 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
266 if (MAPADDRENABLED(flags)) {
267 MAPADDR(&addrbuf, &addrbuf.in_addr);
269 *errp = HOST_NOT_FOUND;
273 return _hpaddr(af, name, &addrbuf, errp);
277 statp = __res_state();
278 if ((statp->options & RES_INIT) == 0) {
279 if (res_ninit(statp) < 0) {
280 *errp = NETDB_INTERNAL;
285 options = statp->options;
286 statp->options &= ~RES_USE_INET6;
288 hp = gethostbyname2(name, af);
289 hp = _hpcopy(hp, errp);
294 if (af == AF_INET6 && ((flags & AI_ALL) || hp == NULL) &&
295 MAPADDRENABLED(flags)) {
296 struct hostent *hp2 = gethostbyname2(name, AF_INET);
299 *errp = statp->res_h_errno;
301 hp = _hpmapv6(hp2, errp);
303 if (hp2 && strcmp(hp->h_name, hp2->h_name) == 0) {
304 struct hostent *hpb = hp;
305 hp = _hpmerge(hpb, hp2, errp);
313 *errp = statp->res_h_errno;
315 statp->options = options;
316 return _hpsort(hp, statp);
320 getipnodebyaddr(const void *src, size_t len, int af, int *errp)
327 struct in6_addr addrbuf;
329 struct in_addr addrbuf;
334 if (len != sizeof(struct in_addr)) {
338 if ((long)src & ~(sizeof(struct in_addr) - 1)) {
339 memcpy(&addrbuf, src, len);
342 if (((struct in_addr *)src)->s_addr == 0)
347 if (len != sizeof(struct in6_addr)) {
351 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/
352 memcpy(&addrbuf, src, len);
355 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
357 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
358 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
360 (sizeof(struct in6_addr) - sizeof(struct in_addr));
362 len = sizeof(struct in_addr);
371 statp = __res_state();
372 if ((statp->options & RES_INIT) == 0) {
373 if (res_ninit(statp) < 0) {
374 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
379 options = statp->options;
380 statp->options &= ~RES_USE_INET6;
382 hp = gethostbyaddr(src, len, af);
384 *errp = statp->res_h_errno;
386 statp->options = options;
387 return (_hpcopy(hp, errp));
391 freehostent(struct hostent *ptr)
397 * Private utility functions
401 * _hpcopy: allocate and copy hostent structure
403 static struct hostent *
404 _hpcopy(struct hostent *hp, int *errp)
409 int nalias = 0, naddr = 0;
416 /* count size to be allocated */
417 size = sizeof(struct hostent);
418 if (hp->h_name != NULL)
419 size += strlen(hp->h_name) + 1;
420 if ((pp = hp->h_aliases) != NULL) {
421 for (i = 0; *pp != NULL; i++, pp++) {
423 size += strlen(*pp) + 1;
428 /* adjust alignment */
431 size += sizeof(char *) * (nalias + 1);
432 addrsize = ALIGN(hp->h_length);
433 if ((pp = hp->h_addr_list) != NULL) {
434 while (*pp++ != NULL)
437 size += addrsize * naddr;
438 size += sizeof(char *) * (naddr + 1);
441 if ((nhp = (struct hostent *)malloc(size)) == NULL) {
445 cp = (char *)&nhp[1];
446 if (hp->h_name != NULL) {
448 strcpy(cp, hp->h_name);
449 cp += strlen(cp) + 1;
452 nhp->h_aliases = (char **)((char *)nhp + al_off);
453 if ((pp = hp->h_aliases) != NULL) {
454 for (i = 0; *pp != NULL; pp++) {
456 nhp->h_aliases[i++] = cp;
458 cp += strlen(cp) + 1;
462 nhp->h_aliases[nalias] = NULL;
463 cp = (char *)&nhp->h_aliases[nalias + 1];
464 nhp->h_addrtype = hp->h_addrtype;
465 nhp->h_length = hp->h_length;
466 nhp->h_addr_list = (char **)cp;
467 if ((pp = hp->h_addr_list) != NULL) {
468 cp = (char *)&nhp->h_addr_list[naddr + 1];
469 for (i = 0; *pp != NULL; pp++) {
470 nhp->h_addr_list[i++] = cp;
471 memcpy(cp, *pp, hp->h_length);
475 nhp->h_addr_list[naddr] = NULL;
480 * _hpaddr: construct hostent structure with one address
482 static struct hostent *
483 _hpaddr(int af, const char *name, void *addr, int *errp)
485 struct hostent *hp, hpbuf;
489 hp->h_name = (char *)name;
490 hp->h_aliases = NULL;
492 hp->h_length = ADDRLEN(af);
493 hp->h_addr_list = addrs;
494 addrs[0] = (char *)addr;
496 return (_hpcopy(hp, errp));
501 * _hpmerge: merge 2 hostent structure, arguments will be freed
503 static struct hostent *
504 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
509 struct hostent *hp, hpbuf;
510 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
511 union inx_addr addrbuf[MAXADDRS];
514 return _hpcopy(hp2, errp);
516 return _hpcopy(hp1, errp);
518 #define HP(i) (i == 1 ? hp1 : hp2)
520 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
521 hp->h_aliases = aliases;
523 for (i = 1; i <= 2; i++) {
524 if ((pp = HP(i)->h_aliases) == NULL)
526 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
527 /* check duplicates */
528 for (j = 0; j < nalias; j++)
529 if (strcasecmp(*pp, aliases[j]) == 0)
532 aliases[nalias++] = *pp;
535 aliases[nalias] = NULL;
536 if (hp1->h_length != hp2->h_length) {
537 hp->h_addrtype = AF_INET6;
538 hp->h_length = sizeof(struct in6_addr);
540 hp->h_addrtype = hp1->h_addrtype;
541 hp->h_length = hp1->h_length;
544 hp->h_addr_list = addrs;
546 for (i = 1; i <= 2; i++) {
547 if ((pp = HP(i)->h_addr_list) == NULL)
549 if (HP(i)->h_length == hp->h_length) {
550 while (naddr < MAXADDRS && *pp != NULL)
551 addrs[naddr++] = *pp++;
553 /* copy IPv4 addr as mapped IPv6 addr */
554 while (naddr < MAXADDRS && *pp != NULL) {
555 MAPADDR(&addrbuf[naddr], *pp++);
556 addrs[naddr] = (char *)&addrbuf[naddr];
562 return (_hpcopy(hp, errp));
567 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
570 static struct hostent *
571 _hpmapv6(struct hostent *hp, int *errp)
577 if (hp->h_addrtype == AF_INET6)
578 return _hpcopy(hp, errp);
580 memset(&hp6, 0, sizeof(struct hostent));
581 hp6.h_addrtype = AF_INET6;
582 hp6.h_length = sizeof(struct in6_addr);
583 return _hpmerge(&hp6, hp, errp);
588 * _hpsort: sort address by sortlist
590 static struct hostent *
591 _hpsort(struct hostent *hp, res_state statp)
594 u_char *ap, *sp, *mp, **pp;
596 char order[MAXADDRS];
597 int nsort = statp->nsort;
599 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
601 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
602 for (j = 0; j < nsort; j++) {
604 if (statp->_u._ext.ext->sort_list[j].af !=
607 sp = (u_char *)&statp->_u._ext.ext->sort_list[j].addr;
608 mp = (u_char *)&statp->_u._ext.ext->sort_list[j].mask;
610 sp = (u_char *)&statp->sort_list[j].addr;
611 mp = (u_char *)&statp->sort_list[j].mask;
613 for (n = 0; n < hp->h_length; n++) {
614 if ((ap[n] & mp[n]) != sp[n])
617 if (n == hp->h_length)
623 pp = (u_char **)hp->h_addr_list;
624 for (i = 0; i < n - 1; i++) {
625 for (j = i + 1; j < n; j++) {
626 if (order[i] > order[j]) {
641 * _hpreorder: sort address by default address selection
643 static struct hostent *
644 _hpreorder(struct hostent *hp)
646 struct hp_order *aio;
650 struct policyhead policyhead;
655 switch (hp->h_addrtype) {
662 free_addrselectpolicy(&policyhead);
666 /* count the number of addrinfo elements for sorting. */
667 for (n = 0; hp->h_addr_list[n] != NULL; n++)
671 * If the number is small enough, we can skip the reordering process.
676 /* allocate a temporary array for sort and initialization of it. */
677 if ((aio = malloc(sizeof(*aio) * n)) == NULL)
678 return hp; /* give up reordering */
679 memset(aio, 0, sizeof(*aio) * n);
681 /* retrieve address selection policy from the kernel */
682 TAILQ_INIT(&policyhead);
683 if (!get_addrselectpolicy(&policyhead)) {
684 /* no policy is installed into kernel, we don't sort. */
689 for (i = 0; i < n; i++) {
690 ap = hp->h_addr_list[i];
691 aio[i].aio_h_addr = ap;
693 switch (hp->h_addrtype) {
695 sa->sa_family = AF_INET;
696 sa->sa_len = sizeof(struct sockaddr_in);
697 memcpy(&((struct sockaddr_in *)sa)->sin_addr, ap,
698 sizeof(struct in_addr));
702 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
703 sa->sa_family = AF_INET;
704 sa->sa_len = sizeof(struct sockaddr_in);
705 memcpy(&((struct sockaddr_in *)sa)->sin_addr,
706 &ap[12], sizeof(struct in_addr));
708 sa->sa_family = AF_INET6;
709 sa->sa_len = sizeof(struct sockaddr_in6);
710 memcpy(&((struct sockaddr_in6 *)sa)->sin6_addr,
711 ap, sizeof(struct in6_addr));
716 aio[i].aio_dstscope = gai_addr2scopetype(sa);
717 aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead);
718 set_source(&aio[i], &policyhead);
721 /* perform sorting. */
722 qsort(aio, n, sizeof(*aio), comp_dst);
724 /* reorder the h_addr_list. */
725 for (i = 0; i < n; i++)
726 hp->h_addr_list[i] = aio[i].aio_h_addr;
728 /* cleanup and return */
730 free_addrselectpolicy(&policyhead);
735 get_addrselectpolicy(struct policyhead *head)
738 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
741 struct in6_addrpolicy *pol, *ep;
743 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
745 if ((buf = malloc(l)) == NULL)
747 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
752 ep = (struct in6_addrpolicy *)(buf + l);
753 for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
754 struct policyqueue *new;
756 if ((new = malloc(sizeof(*new))) == NULL) {
757 free_addrselectpolicy(head); /* make the list empty */
760 new->pc_policy = *pol;
761 TAILQ_INSERT_TAIL(head, new, pc_entry);
772 free_addrselectpolicy(struct policyhead *head)
774 struct policyqueue *ent, *nent;
776 for (ent = TAILQ_FIRST(head); ent; ent = nent) {
777 nent = TAILQ_NEXT(ent, pc_entry);
778 TAILQ_REMOVE(head, ent, pc_entry);
783 static struct policyqueue *
784 match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
787 struct policyqueue *ent, *bestent = NULL;
788 struct in6_addrpolicy *pol;
789 int matchlen, bestmatchlen = -1;
790 u_char *mp, *ep, *k, *p, m;
791 struct sockaddr_in6 key;
793 switch(addr->sa_family) {
795 key = *(struct sockaddr_in6 *)addr;
798 /* convert the address into IPv4-mapped IPv6 address. */
799 memset(&key, 0, sizeof(key));
800 key.sin6_family = AF_INET6;
801 key.sin6_len = sizeof(key);
802 key.sin6_addr.s6_addr[10] = 0xff;
803 key.sin6_addr.s6_addr[11] = 0xff;
804 memcpy(&key.sin6_addr.s6_addr[12],
805 &((struct sockaddr_in *)addr)->sin_addr, 4);
811 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
812 pol = &ent->pc_policy;
815 mp = (u_char *)&pol->addrmask.sin6_addr;
816 ep = mp + 16; /* XXX: scope field? */
817 k = (u_char *)&key.sin6_addr;
818 p = (u_char *)&pol->addr.sin6_addr;
819 for (; mp < ep && *mp; mp++, k++, p++) {
822 goto next; /* not match */
823 if (m == 0xff) /* short cut for a typical case */
833 /* matched. check if this is better than the current best. */
834 if (matchlen > bestmatchlen) {
836 bestmatchlen = matchlen;
851 set_source(struct hp_order *aio, struct policyhead *ph)
853 struct sockaddr_storage ss = aio->aio_un.aiou_ss;
857 /* set unspec ("no source is available"), just in case */
858 aio->aio_srcsa.sa_family = AF_UNSPEC;
859 aio->aio_srcscope = -1;
861 switch(ss.ss_family) {
863 ((struct sockaddr_in *)&ss)->sin_port = htons(1);
867 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(1);
870 default: /* ignore unsupported AFs explicitly */
874 /* open a socket to get the source address for the given dst */
875 if ((s = _socket(ss.ss_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
876 return; /* give up */
877 if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0)
880 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
881 aio->aio_srcsa.sa_family = AF_UNSPEC;
884 aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
885 aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
886 aio->aio_matchlen = matchlen(&aio->aio_srcsa, (struct sockaddr *)&ss);
888 if (ss.ss_family == AF_INET6) {
889 struct in6_ifreq ifr6;
892 memset(&ifr6, 0, sizeof(ifr6));
893 memcpy(&ifr6.ifr_addr, &ss, ss.ss_len);
894 if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
895 flags6 = ifr6.ifr_ifru.ifru_flags6;
896 if ((flags6 & IN6_IFF_DEPRECATED))
897 aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
908 matchlen(struct sockaddr *src, struct sockaddr *dst)
915 switch (src->sa_family) {
918 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
919 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
920 addrlen = sizeof(struct in6_addr);
925 s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
926 d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
927 addrlen = sizeof(struct in_addr);
935 if ((r = (*d++ ^ *s++)) != 0) {
936 while (r < addrlen * 8) {
947 comp_dst(const void *arg1, const void *arg2)
949 const struct hp_order *dst1 = arg1, *dst2 = arg2;
952 * Rule 1: Avoid unusable destinations.
953 * XXX: we currently do not consider if an appropriate route exists.
955 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
956 dst2->aio_srcsa.sa_family == AF_UNSPEC) {
959 if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
960 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
964 /* Rule 2: Prefer matching scope. */
965 if (dst1->aio_dstscope == dst1->aio_srcscope &&
966 dst2->aio_dstscope != dst2->aio_srcscope) {
969 if (dst1->aio_dstscope != dst1->aio_srcscope &&
970 dst2->aio_dstscope == dst2->aio_srcscope) {
974 /* Rule 3: Avoid deprecated addresses. */
975 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
976 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
977 if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
978 (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
981 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
982 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
987 /* Rule 4: Prefer home addresses. */
988 /* XXX: not implemented yet */
990 /* Rule 5: Prefer matching label. */
992 if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
993 dst1->aio_srcpolicy->pc_policy.label ==
994 dst1->aio_dstpolicy->pc_policy.label &&
995 (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
996 dst2->aio_srcpolicy->pc_policy.label !=
997 dst2->aio_dstpolicy->pc_policy.label)) {
1000 if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
1001 dst2->aio_srcpolicy->pc_policy.label ==
1002 dst2->aio_dstpolicy->pc_policy.label &&
1003 (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
1004 dst1->aio_srcpolicy->pc_policy.label !=
1005 dst1->aio_dstpolicy->pc_policy.label)) {
1010 /* Rule 6: Prefer higher precedence. */
1012 if (dst1->aio_dstpolicy &&
1013 (dst2->aio_dstpolicy == NULL ||
1014 dst1->aio_dstpolicy->pc_policy.preced >
1015 dst2->aio_dstpolicy->pc_policy.preced)) {
1018 if (dst2->aio_dstpolicy &&
1019 (dst1->aio_dstpolicy == NULL ||
1020 dst2->aio_dstpolicy->pc_policy.preced >
1021 dst1->aio_dstpolicy->pc_policy.preced)) {
1026 /* Rule 7: Prefer native transport. */
1027 /* XXX: not implemented yet */
1029 /* Rule 8: Prefer smaller scope. */
1030 if (dst1->aio_dstscope >= 0 &&
1031 dst1->aio_dstscope < dst2->aio_dstscope) {
1034 if (dst2->aio_dstscope >= 0 &&
1035 dst2->aio_dstscope < dst1->aio_dstscope) {
1040 * Rule 9: Use longest matching prefix.
1041 * We compare the match length in a same AF only.
1043 if (dst1->aio_sa.sa_family == dst2->aio_sa.sa_family) {
1044 if (dst1->aio_matchlen > dst2->aio_matchlen) {
1047 if (dst1->aio_matchlen < dst2->aio_matchlen) {
1052 /* Rule 10: Otherwise, leave the order unchanged. */
1057 * Copy from scope.c.
1058 * XXX: we should standardize the functions and link them as standard
1062 gai_addr2scopetype(struct sockaddr *sa)
1065 struct sockaddr_in6 *sa6;
1067 struct sockaddr_in *sa4;
1069 switch(sa->sa_family) {
1072 sa6 = (struct sockaddr_in6 *)sa;
1073 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1074 /* just use the scope field of the multicast address */
1075 return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1078 * Unicast addresses: map scope type to corresponding scope
1079 * value defined for multcast addresses.
1080 * XXX: hardcoded scope type values are bad...
1082 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1083 return(1); /* node local scope */
1084 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1085 return(2); /* link-local scope */
1086 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1087 return(5); /* site-local scope */
1088 return(14); /* global scope */
1093 * IPv4 pseudo scoping according to RFC 3484.
1095 sa4 = (struct sockaddr_in *)sa;
1096 /* IPv4 autoconfiguration addresses have link-local scope. */
1097 if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1098 ((u_char *)&sa4->sin_addr)[1] == 254)
1100 /* Private addresses have site-local scope. */
1101 if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1102 (((u_char *)&sa4->sin_addr)[0] == 172 &&
1103 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1104 (((u_char *)&sa4->sin_addr)[0] == 192 &&
1105 ((u_char *)&sa4->sin_addr)[1] == 168))
1106 return(14); /* XXX: It should be 5 unless NAT */
1107 /* Loopback addresses have link-local scope. */
1108 if (((u_char *)&sa4->sin_addr)[0] == 127)
1113 errno = EAFNOSUPPORT; /* is this a good error? */