1 /* $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * ++Copyright++ 1985, 1988, 1993
36 * Copyright (c) 1985, 1988, 1993
37 * The Regents of the University of California. All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
65 * Permission to use, copy, modify, and distribute this software for any
66 * purpose with or without fee is hereby granted, provided that the above
67 * copyright notice and this permission notice appear in all copies, and that
68 * the name of Digital Equipment Corporation not be used in advertising or
69 * publicity pertaining to distribution of the document or software without
70 * specific, written prior permission.
72 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
73 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
74 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
75 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
76 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
77 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
78 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
85 * Atsushi Onoe <onoe@sm.sony.co.jp>
88 #include <sys/cdefs.h>
89 __FBSDID("$FreeBSD$");
91 #include "namespace.h"
92 #include <sys/param.h>
93 #include <sys/socket.h>
95 #include <sys/queue.h>
96 #include <netinet/in.h>
99 #include <sys/sysctl.h>
100 #include <sys/ioctl.h>
101 #include <netinet6/in6_var.h> /* XXX */
104 #include <arpa/inet.h>
105 #include <arpa/nameser.h>
114 #include <nsswitch.h>
116 #include "un-namespace.h"
117 #include "netdb_private.h"
118 #include "res_private.h"
121 #define MAXALIASES 10
127 #define MAXDNAME 1025
131 #define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
132 sizeof(struct in_addr))
134 #define ADDRLEN(af) sizeof(struct in_addr)
137 #define MAPADDR(ab, ina) \
139 memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \
140 memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \
141 memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \
143 #define MAPADDRENABLED(flags) \
144 (((flags) & AI_V4MAPPED) || \
145 (((flags) & AI_V4MAPPED_CFG)))
148 struct in_addr in_addr;
150 struct in6_addr in6_addr;
155 struct in_addr mau_inaddr;
157 #define map_zero map_addr_un.mau_zero
158 #define map_one map_addr_un.mau_one
159 #define map_inaddr map_addr_un.mau_inaddr
163 TAILQ_ENTRY(policyqueue) pc_entry;
165 struct in6_addrpolicy pc_policy;
168 TAILQ_HEAD(policyhead, policyqueue);
170 #define AIO_SRCFLAG_DEPRECATED 0x1
174 struct sockaddr_storage aiou_ss;
175 struct sockaddr aiou_sa;
177 #define aio_srcsa aio_src_un.aiou_sa
178 u_int32_t aio_srcflag;
181 struct policyqueue *aio_srcpolicy;
182 struct policyqueue *aio_dstpolicy;
184 struct sockaddr_storage aiou_ss;
185 struct sockaddr aiou_sa;
187 #define aio_sa aio_un.aiou_sa
190 int aio_initial_sequence;
193 static struct hostent *_hpcopy(struct hostent *, int *);
194 static struct hostent *_hpaddr(int, const char *, void *, int *);
196 static struct hostent *_hpmerge(struct hostent *, struct hostent *, int *);
197 static struct hostent *_hpmapv6(struct hostent *, int *);
199 static struct hostent *_hpsort(struct hostent *, res_state);
202 static struct hostent *_hpreorder(struct hostent *);
203 static int get_addrselectpolicy(struct policyhead *);
204 static void free_addrselectpolicy(struct policyhead *);
205 static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
206 struct policyhead *);
207 static void set_source(struct hp_order *, struct policyhead *);
208 static int matchlen(struct sockaddr *, struct sockaddr *);
209 static int comp_dst(const void *, const void *);
210 static int gai_addr2scopetype(struct sockaddr *);
214 * Functions defined in RFC2553
215 * getipnodebyname, getipnodebyaddr, freehostent
219 getipnodebyname(const char *name, int af, int flags, int *errp)
222 union inx_addr addrbuf;
237 if (flags & AI_ADDRCONFIG) {
240 if ((s = _socket(af, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
244 * Note that implementation dependent test for address
245 * configuration should be done every time called
246 * (or appropriate interval),
247 * because addresses will be dynamically assigned or deleted.
253 /* special case for literal address */
254 if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
255 if (af != AF_INET6) {
256 *errp = HOST_NOT_FOUND;
259 return _hpaddr(af, name, &addrbuf, errp);
262 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
264 if (MAPADDRENABLED(flags)) {
265 MAPADDR(&addrbuf, &addrbuf.in_addr);
267 *errp = HOST_NOT_FOUND;
271 return _hpaddr(af, name, &addrbuf, errp);
275 statp = __res_state();
276 if ((statp->options & RES_INIT) == 0) {
277 if (res_ninit(statp) < 0) {
278 *errp = NETDB_INTERNAL;
283 options = statp->options;
284 statp->options &= ~RES_USE_INET6;
286 hp = gethostbyname2(name, af);
287 hp = _hpcopy(hp, errp);
292 if (af == AF_INET6 && ((flags & AI_ALL) || hp == NULL) &&
293 MAPADDRENABLED(flags)) {
294 struct hostent *hp2 = gethostbyname2(name, AF_INET);
297 *errp = statp->res_h_errno;
299 hp = _hpmapv6(hp2, errp);
301 if (hp2 && strcmp(hp->h_name, hp2->h_name) == 0) {
302 struct hostent *hpb = hp;
303 hp = _hpmerge(hpb, hp2, errp);
311 *errp = statp->res_h_errno;
313 statp->options = options;
314 return _hpsort(hp, statp);
318 getipnodebyaddr(const void *src, size_t len, int af, int *errp)
325 struct in6_addr addrbuf;
327 struct in_addr addrbuf;
332 if (len != sizeof(struct in_addr)) {
336 if (rounddown2((long)src, sizeof(struct in_addr))) {
337 memcpy(&addrbuf, src, len);
340 if (((struct in_addr *)src)->s_addr == 0)
345 if (len != sizeof(struct in6_addr)) {
349 if (rounddown2((long)src, sizeof(struct in6_addr) / 2)) {
351 memcpy(&addrbuf, src, len);
354 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
356 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
357 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
359 (sizeof(struct in6_addr) - sizeof(struct in_addr));
361 len = sizeof(struct in_addr);
370 statp = __res_state();
371 if ((statp->options & RES_INIT) == 0) {
372 if (res_ninit(statp) < 0) {
373 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
378 options = statp->options;
379 statp->options &= ~RES_USE_INET6;
381 hp = gethostbyaddr(src, len, af);
383 *errp = statp->res_h_errno;
385 statp->options = options;
386 return (_hpcopy(hp, errp));
390 freehostent(struct hostent *ptr)
396 * Private utility functions
400 * _hpcopy: allocate and copy hostent structure
402 static struct hostent *
403 _hpcopy(struct hostent *hp, int *errp)
408 int nalias = 0, naddr = 0;
415 /* count size to be allocated */
416 size = sizeof(struct hostent);
417 if (hp->h_name != NULL)
418 size += strlen(hp->h_name) + 1;
419 if ((pp = hp->h_aliases) != NULL) {
420 for (i = 0; *pp != NULL; i++, pp++) {
422 size += strlen(*pp) + 1;
427 /* adjust alignment */
430 size += sizeof(char *) * (nalias + 1);
431 addrsize = ALIGN(hp->h_length);
432 if ((pp = hp->h_addr_list) != NULL) {
433 while (*pp++ != NULL)
436 size += addrsize * naddr;
437 size += sizeof(char *) * (naddr + 1);
440 if ((nhp = (struct hostent *)malloc(size)) == NULL) {
444 cp = (char *)&nhp[1];
445 if (hp->h_name != NULL) {
447 strcpy(cp, hp->h_name);
448 cp += strlen(cp) + 1;
451 nhp->h_aliases = (char **)((char *)nhp + al_off);
452 if ((pp = hp->h_aliases) != NULL) {
453 for (i = 0; *pp != NULL; pp++) {
455 nhp->h_aliases[i++] = cp;
457 cp += strlen(cp) + 1;
461 nhp->h_aliases[nalias] = NULL;
462 cp = (char *)&nhp->h_aliases[nalias + 1];
463 nhp->h_addrtype = hp->h_addrtype;
464 nhp->h_length = hp->h_length;
465 nhp->h_addr_list = (char **)cp;
466 if ((pp = hp->h_addr_list) != NULL) {
467 cp = (char *)&nhp->h_addr_list[naddr + 1];
468 for (i = 0; *pp != NULL; pp++) {
469 nhp->h_addr_list[i++] = cp;
470 memcpy(cp, *pp, hp->h_length);
474 nhp->h_addr_list[naddr] = NULL;
479 * _hpaddr: construct hostent structure with one address
481 static struct hostent *
482 _hpaddr(int af, const char *name, void *addr, int *errp)
484 struct hostent *hp, hpbuf;
488 hp->h_name = (char *)name;
489 hp->h_aliases = NULL;
491 hp->h_length = ADDRLEN(af);
492 hp->h_addr_list = addrs;
493 addrs[0] = (char *)addr;
495 return (_hpcopy(hp, errp));
500 * _hpmerge: merge 2 hostent structure, arguments will be freed
502 static struct hostent *
503 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
508 struct hostent *hp, hpbuf;
509 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
510 union inx_addr addrbuf[MAXADDRS];
513 return _hpcopy(hp2, errp);
515 return _hpcopy(hp1, errp);
517 #define HP(i) (i == 1 ? hp1 : hp2)
519 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
520 hp->h_aliases = aliases;
522 for (i = 1; i <= 2; i++) {
523 if ((pp = HP(i)->h_aliases) == NULL)
525 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
526 /* check duplicates */
527 for (j = 0; j < nalias; j++)
528 if (strcasecmp(*pp, aliases[j]) == 0)
531 aliases[nalias++] = *pp;
534 aliases[nalias] = NULL;
535 if (hp1->h_length != hp2->h_length) {
536 hp->h_addrtype = AF_INET6;
537 hp->h_length = sizeof(struct in6_addr);
539 hp->h_addrtype = hp1->h_addrtype;
540 hp->h_length = hp1->h_length;
543 hp->h_addr_list = addrs;
545 for (i = 1; i <= 2; i++) {
546 if ((pp = HP(i)->h_addr_list) == NULL)
548 if (HP(i)->h_length == hp->h_length) {
549 while (naddr < MAXADDRS && *pp != NULL)
550 addrs[naddr++] = *pp++;
552 /* copy IPv4 addr as mapped IPv6 addr */
553 while (naddr < MAXADDRS && *pp != NULL) {
554 MAPADDR(&addrbuf[naddr], *pp++);
555 addrs[naddr] = (char *)&addrbuf[naddr];
561 return (_hpcopy(hp, errp));
566 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
569 static struct hostent *
570 _hpmapv6(struct hostent *hp, int *errp)
576 if (hp->h_addrtype == AF_INET6)
577 return _hpcopy(hp, errp);
579 memset(&hp6, 0, sizeof(struct hostent));
580 hp6.h_addrtype = AF_INET6;
581 hp6.h_length = sizeof(struct in6_addr);
582 return _hpmerge(&hp6, hp, errp);
587 * _hpsort: sort address by sortlist
589 static struct hostent *
590 _hpsort(struct hostent *hp, res_state statp)
593 u_char *ap, *sp, *mp, **pp;
595 char order[MAXADDRS];
596 int nsort = statp->nsort;
598 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
600 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
601 for (j = 0; j < nsort; j++) {
603 if (statp->_u._ext.ext->sort_list[j].af !=
606 sp = (u_char *)&statp->_u._ext.ext->sort_list[j].addr;
607 mp = (u_char *)&statp->_u._ext.ext->sort_list[j].mask;
609 sp = (u_char *)&statp->sort_list[j].addr;
610 mp = (u_char *)&statp->sort_list[j].mask;
612 for (n = 0; n < hp->h_length; n++) {
613 if ((ap[n] & mp[n]) != sp[n])
616 if (n == hp->h_length)
622 pp = (u_char **)hp->h_addr_list;
623 for (i = 0; i < n - 1; i++) {
624 for (j = i + 1; j < n; j++) {
625 if (order[i] > order[j]) {
640 * _hpreorder: sort address by default address selection
642 static struct hostent *
643 _hpreorder(struct hostent *hp)
645 struct hp_order *aio;
649 struct policyhead policyhead;
654 switch (hp->h_addrtype) {
664 /* count the number of addrinfo elements for sorting. */
665 for (n = 0; hp->h_addr_list[n] != NULL; n++)
669 * If the number is small enough, we can skip the reordering process.
674 /* allocate a temporary array for sort and initialization of it. */
675 if ((aio = malloc(sizeof(*aio) * n)) == NULL)
676 return hp; /* give up reordering */
677 memset(aio, 0, sizeof(*aio) * n);
679 /* retrieve address selection policy from the kernel */
680 TAILQ_INIT(&policyhead);
681 if (!get_addrselectpolicy(&policyhead)) {
682 /* no policy is installed into kernel, we don't sort. */
687 for (i = 0; i < n; i++) {
688 ap = hp->h_addr_list[i];
689 aio[i].aio_h_addr = ap;
691 switch (hp->h_addrtype) {
693 sa->sa_family = AF_INET;
694 sa->sa_len = sizeof(struct sockaddr_in);
695 memcpy(&((struct sockaddr_in *)sa)->sin_addr, ap,
696 sizeof(struct in_addr));
700 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
701 sa->sa_family = AF_INET;
702 sa->sa_len = sizeof(struct sockaddr_in);
703 memcpy(&((struct sockaddr_in *)sa)->sin_addr,
704 &ap[12], sizeof(struct in_addr));
706 sa->sa_family = AF_INET6;
707 sa->sa_len = sizeof(struct sockaddr_in6);
708 memcpy(&((struct sockaddr_in6 *)sa)->sin6_addr,
709 ap, sizeof(struct in6_addr));
714 aio[i].aio_dstscope = gai_addr2scopetype(sa);
715 aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead);
716 set_source(&aio[i], &policyhead);
717 aio[i].aio_initial_sequence = i;
720 /* perform sorting. */
721 qsort(aio, n, sizeof(*aio), comp_dst);
723 /* reorder the h_addr_list. */
724 for (i = 0; i < n; i++)
725 hp->h_addr_list[i] = aio[i].aio_h_addr;
727 /* cleanup and return */
729 free_addrselectpolicy(&policyhead);
734 get_addrselectpolicy(struct policyhead *head)
737 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
740 struct in6_addrpolicy *pol, *ep;
742 if (sysctl(mib, nitems(mib), NULL, &l, NULL, 0) < 0)
744 if ((buf = malloc(l)) == NULL)
746 if (sysctl(mib, nitems(mib), buf, &l, NULL, 0) < 0) {
751 ep = (struct in6_addrpolicy *)(buf + l);
752 for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
753 struct policyqueue *new;
755 if ((new = malloc(sizeof(*new))) == NULL) {
756 free_addrselectpolicy(head); /* make the list empty */
759 new->pc_policy = *pol;
760 TAILQ_INSERT_TAIL(head, new, pc_entry);
771 free_addrselectpolicy(struct policyhead *head)
773 struct policyqueue *ent, *nent;
775 for (ent = TAILQ_FIRST(head); ent; ent = nent) {
776 nent = TAILQ_NEXT(ent, pc_entry);
777 TAILQ_REMOVE(head, ent, pc_entry);
782 static struct policyqueue *
783 match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
786 struct policyqueue *ent, *bestent = NULL;
787 struct in6_addrpolicy *pol;
788 int matchlen, bestmatchlen = -1;
789 u_char *mp, *ep, *k, *p, m;
790 struct sockaddr_in6 key;
792 switch(addr->sa_family) {
794 key = *(struct sockaddr_in6 *)addr;
797 /* convert the address into IPv4-mapped IPv6 address. */
798 memset(&key, 0, sizeof(key));
799 key.sin6_family = AF_INET6;
800 key.sin6_len = sizeof(key);
802 (char *)&((struct sockaddr_in *)addr)->sin_addr,
803 (char *)&key.sin6_addr);
809 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
810 pol = &ent->pc_policy;
813 mp = (u_char *)&pol->addrmask.sin6_addr;
814 ep = mp + 16; /* XXX: scope field? */
815 k = (u_char *)&key.sin6_addr;
816 p = (u_char *)&pol->addr.sin6_addr;
817 for (; mp < ep && *mp; mp++, k++, p++) {
820 goto next; /* not match */
821 if (m == 0xff) /* short cut for a typical case */
831 /* matched. check if this is better than the current best. */
832 if (matchlen > bestmatchlen) {
834 bestmatchlen = matchlen;
849 set_source(struct hp_order *aio, struct policyhead *ph)
851 struct sockaddr_storage ss = aio->aio_un.aiou_ss;
855 /* set unspec ("no source is available"), just in case */
856 aio->aio_srcsa.sa_family = AF_UNSPEC;
857 aio->aio_srcscope = -1;
859 switch(ss.ss_family) {
861 ((struct sockaddr_in *)&ss)->sin_port = htons(1);
865 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(1);
868 default: /* ignore unsupported AFs explicitly */
872 /* open a socket to get the source address for the given dst */
873 if ((s = _socket(ss.ss_family, SOCK_DGRAM | SOCK_CLOEXEC,
875 return; /* give up */
876 if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0)
879 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
880 aio->aio_srcsa.sa_family = AF_UNSPEC;
883 aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
884 aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
885 aio->aio_matchlen = matchlen(&aio->aio_srcsa, (struct sockaddr *)&ss);
887 if (ss.ss_family == AF_INET6) {
888 struct in6_ifreq ifr6;
891 memset(&ifr6, 0, sizeof(ifr6));
892 memcpy(&ifr6.ifr_addr, &ss, ss.ss_len);
893 if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
894 flags6 = ifr6.ifr_ifru.ifru_flags6;
895 if ((flags6 & IN6_IFF_DEPRECATED))
896 aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
907 matchlen(struct sockaddr *src, struct sockaddr *dst)
914 switch (src->sa_family) {
917 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
918 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
919 addrlen = sizeof(struct in6_addr);
924 s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
925 d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
926 addrlen = sizeof(struct in_addr);
934 if ((r = (*d++ ^ *s++)) != 0) {
935 while ((r & 0x80) == 0) {
946 comp_dst(const void *arg1, const void *arg2)
948 const struct hp_order *dst1 = arg1, *dst2 = arg2;
951 * Rule 1: Avoid unusable destinations.
952 * XXX: we currently do not consider if an appropriate route exists.
954 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
955 dst2->aio_srcsa.sa_family == AF_UNSPEC) {
958 if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
959 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
963 /* Rule 2: Prefer matching scope. */
964 if (dst1->aio_dstscope == dst1->aio_srcscope &&
965 dst2->aio_dstscope != dst2->aio_srcscope) {
968 if (dst1->aio_dstscope != dst1->aio_srcscope &&
969 dst2->aio_dstscope == dst2->aio_srcscope) {
973 /* Rule 3: Avoid deprecated addresses. */
974 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
975 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
976 if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
977 (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
980 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
981 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
986 /* Rule 4: Prefer home addresses. */
987 /* XXX: not implemented yet */
989 /* Rule 5: Prefer matching label. */
991 if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
992 dst1->aio_srcpolicy->pc_policy.label ==
993 dst1->aio_dstpolicy->pc_policy.label &&
994 (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
995 dst2->aio_srcpolicy->pc_policy.label !=
996 dst2->aio_dstpolicy->pc_policy.label)) {
999 if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
1000 dst2->aio_srcpolicy->pc_policy.label ==
1001 dst2->aio_dstpolicy->pc_policy.label &&
1002 (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
1003 dst1->aio_srcpolicy->pc_policy.label !=
1004 dst1->aio_dstpolicy->pc_policy.label)) {
1009 /* Rule 6: Prefer higher precedence. */
1011 if (dst1->aio_dstpolicy &&
1012 (dst2->aio_dstpolicy == NULL ||
1013 dst1->aio_dstpolicy->pc_policy.preced >
1014 dst2->aio_dstpolicy->pc_policy.preced)) {
1017 if (dst2->aio_dstpolicy &&
1018 (dst1->aio_dstpolicy == NULL ||
1019 dst2->aio_dstpolicy->pc_policy.preced >
1020 dst1->aio_dstpolicy->pc_policy.preced)) {
1025 /* Rule 7: Prefer native transport. */
1026 /* XXX: not implemented yet */
1028 /* Rule 8: Prefer smaller scope. */
1029 if (dst1->aio_dstscope >= 0 &&
1030 dst1->aio_dstscope < dst2->aio_dstscope) {
1033 if (dst2->aio_dstscope >= 0 &&
1034 dst2->aio_dstscope < dst1->aio_dstscope) {
1039 * Rule 9: Use longest matching prefix.
1040 * We compare the match length in a same AF only.
1042 if (dst1->aio_sa.sa_family == dst2->aio_sa.sa_family) {
1043 if (dst1->aio_matchlen > dst2->aio_matchlen) {
1046 if (dst1->aio_matchlen < dst2->aio_matchlen) {
1051 /* Rule 10: Otherwise, leave the order unchanged. */
1054 * Note that qsort is unstable; so, we can't return zero and
1055 * expect the order to be unchanged.
1056 * That also means we can't depend on the current position of
1057 * dst2 being after dst1. We must enforce the initial order
1058 * with an explicit compare on the original position.
1059 * The qsort specification requires that "When the same objects
1060 * (consisting of width bytes, irrespective of their current
1061 * positions in the array) are passed more than once to the
1062 * comparison function, the results shall be consistent with one
1064 * In other words, If A < B, then we must also return B > A.
1066 if (dst2->aio_initial_sequence < dst1->aio_initial_sequence)
1073 * Copy from scope.c.
1074 * XXX: we should standardize the functions and link them as standard
1078 gai_addr2scopetype(struct sockaddr *sa)
1081 struct sockaddr_in6 *sa6;
1083 struct sockaddr_in *sa4;
1085 switch(sa->sa_family) {
1088 sa6 = (struct sockaddr_in6 *)sa;
1089 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1090 /* just use the scope field of the multicast address */
1091 return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1094 * Unicast addresses: map scope type to corresponding scope
1095 * value defined for multcast addresses.
1096 * XXX: hardcoded scope type values are bad...
1098 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1099 return(1); /* node local scope */
1100 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1101 return(2); /* link-local scope */
1102 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1103 return(5); /* site-local scope */
1104 return(14); /* global scope */
1109 * IPv4 pseudo scoping according to RFC 3484.
1111 sa4 = (struct sockaddr_in *)sa;
1112 /* IPv4 autoconfiguration addresses have link-local scope. */
1113 if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1114 ((u_char *)&sa4->sin_addr)[1] == 254)
1116 /* Private addresses have site-local scope. */
1117 if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1118 (((u_char *)&sa4->sin_addr)[0] == 172 &&
1119 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1120 (((u_char *)&sa4->sin_addr)[0] == 192 &&
1121 ((u_char *)&sa4->sin_addr)[1] == 168))
1122 return(14); /* XXX: It should be 5 unless NAT */
1123 /* Loopback addresses have link-local scope. */
1124 if (((u_char *)&sa4->sin_addr)[0] == 127)
1129 errno = EAFNOSUPPORT; /* is this a good error? */