13f587e57SKris Kennaway /* $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $ */ 273b30f0cSJun-ichiro itojun Hagino 37d56d374SYoshinobu Inoue /* 47d56d374SYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 57d56d374SYoshinobu Inoue * All rights reserved. 67d56d374SYoshinobu Inoue * 77d56d374SYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 87d56d374SYoshinobu Inoue * modification, are permitted provided that the following conditions 97d56d374SYoshinobu Inoue * are met: 107d56d374SYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 117d56d374SYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 127d56d374SYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 137d56d374SYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 147d56d374SYoshinobu Inoue * documentation and/or other materials provided with the distribution. 157d56d374SYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 167d56d374SYoshinobu Inoue * may be used to endorse or promote products derived from this software 177d56d374SYoshinobu Inoue * without specific prior written permission. 187d56d374SYoshinobu Inoue * 197d56d374SYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 207d56d374SYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 217d56d374SYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 227d56d374SYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 237d56d374SYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 247d56d374SYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 257d56d374SYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 267d56d374SYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 277d56d374SYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 287d56d374SYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 297d56d374SYoshinobu Inoue * SUCH DAMAGE. 307d56d374SYoshinobu Inoue */ 3173b30f0cSJun-ichiro itojun Hagino /* 3273b30f0cSJun-ichiro itojun Hagino * ++Copyright++ 1985, 1988, 1993 3373b30f0cSJun-ichiro itojun Hagino * - 3473b30f0cSJun-ichiro itojun Hagino * Copyright (c) 1985, 1988, 1993 3573b30f0cSJun-ichiro itojun Hagino * The Regents of the University of California. All rights reserved. 3673b30f0cSJun-ichiro itojun Hagino * 3773b30f0cSJun-ichiro itojun Hagino * Redistribution and use in source and binary forms, with or without 3873b30f0cSJun-ichiro itojun Hagino * modification, are permitted provided that the following conditions 3973b30f0cSJun-ichiro itojun Hagino * are met: 4073b30f0cSJun-ichiro itojun Hagino * 1. Redistributions of source code must retain the above copyright 4173b30f0cSJun-ichiro itojun Hagino * notice, this list of conditions and the following disclaimer. 4273b30f0cSJun-ichiro itojun Hagino * 2. Redistributions in binary form must reproduce the above copyright 4373b30f0cSJun-ichiro itojun Hagino * notice, this list of conditions and the following disclaimer in the 4473b30f0cSJun-ichiro itojun Hagino * documentation and/or other materials provided with the distribution. 45e2e9c35fSEd Maste * 3. Neither the name of the University nor the names of its contributors 4673b30f0cSJun-ichiro itojun Hagino * may be used to endorse or promote products derived from this software 4773b30f0cSJun-ichiro itojun Hagino * without specific prior written permission. 4873b30f0cSJun-ichiro itojun Hagino * 4973b30f0cSJun-ichiro itojun Hagino * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5073b30f0cSJun-ichiro itojun Hagino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5173b30f0cSJun-ichiro itojun Hagino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5273b30f0cSJun-ichiro itojun Hagino * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5373b30f0cSJun-ichiro itojun Hagino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5473b30f0cSJun-ichiro itojun Hagino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5573b30f0cSJun-ichiro itojun Hagino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5673b30f0cSJun-ichiro itojun Hagino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5773b30f0cSJun-ichiro itojun Hagino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5873b30f0cSJun-ichiro itojun Hagino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5973b30f0cSJun-ichiro itojun Hagino * SUCH DAMAGE. 6073b30f0cSJun-ichiro itojun Hagino * - 6173b30f0cSJun-ichiro itojun Hagino * Portions Copyright (c) 1993 by Digital Equipment Corporation. 6273b30f0cSJun-ichiro itojun Hagino * 6373b30f0cSJun-ichiro itojun Hagino * Permission to use, copy, modify, and distribute this software for any 6473b30f0cSJun-ichiro itojun Hagino * purpose with or without fee is hereby granted, provided that the above 6573b30f0cSJun-ichiro itojun Hagino * copyright notice and this permission notice appear in all copies, and that 6673b30f0cSJun-ichiro itojun Hagino * the name of Digital Equipment Corporation not be used in advertising or 6773b30f0cSJun-ichiro itojun Hagino * publicity pertaining to distribution of the document or software without 6873b30f0cSJun-ichiro itojun Hagino * specific, written prior permission. 6973b30f0cSJun-ichiro itojun Hagino * 7073b30f0cSJun-ichiro itojun Hagino * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 7173b30f0cSJun-ichiro itojun Hagino * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 7273b30f0cSJun-ichiro itojun Hagino * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 7373b30f0cSJun-ichiro itojun Hagino * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 7473b30f0cSJun-ichiro itojun Hagino * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 7573b30f0cSJun-ichiro itojun Hagino * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 7673b30f0cSJun-ichiro itojun Hagino * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 7773b30f0cSJun-ichiro itojun Hagino * SOFTWARE. 7873b30f0cSJun-ichiro itojun Hagino * - 7973b30f0cSJun-ichiro itojun Hagino * --Copyright-- 8073b30f0cSJun-ichiro itojun Hagino */ 8173b30f0cSJun-ichiro itojun Hagino 827d56d374SYoshinobu Inoue /* 837d56d374SYoshinobu Inoue * Atsushi Onoe <onoe@sm.sony.co.jp> 847d56d374SYoshinobu Inoue */ 857d56d374SYoshinobu Inoue 86333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 87333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 88333fc21eSDavid E. O'Brien 89d201fe46SDaniel Eischen #include "namespace.h" 907d56d374SYoshinobu Inoue #include <sys/param.h> 917d56d374SYoshinobu Inoue #include <sys/socket.h> 927d56d374SYoshinobu Inoue #include <sys/time.h> 93e6f35403SYoshinobu Inoue #include <sys/queue.h> 947d56d374SYoshinobu Inoue #include <netinet/in.h> 95b8b31f33SHajimu UMEMOTO #ifdef INET6 96b8b31f33SHajimu UMEMOTO #include <net/if.h> 97b8b31f33SHajimu UMEMOTO #include <sys/sysctl.h> 98146cd1bcSHajimu UMEMOTO #include <sys/ioctl.h> 99b8b31f33SHajimu UMEMOTO #include <netinet6/in6_var.h> /* XXX */ 100b8b31f33SHajimu UMEMOTO #endif 1017d56d374SYoshinobu Inoue 1027d56d374SYoshinobu Inoue #include <arpa/inet.h> 1037d56d374SYoshinobu Inoue #include <arpa/nameser.h> 1047d56d374SYoshinobu Inoue 105e6f35403SYoshinobu Inoue #include <errno.h> 1067d56d374SYoshinobu Inoue #include <netdb.h> 1077d56d374SYoshinobu Inoue #include <resolv.h> 1087d56d374SYoshinobu Inoue #include <stdio.h> 1097d56d374SYoshinobu Inoue #include <stdlib.h> 1107d56d374SYoshinobu Inoue #include <string.h> 111248aee62SJacques Vidrine #include <stdarg.h> 112248aee62SJacques Vidrine #include <nsswitch.h> 1137d56d374SYoshinobu Inoue #include <unistd.h> 114d201fe46SDaniel Eischen #include "un-namespace.h" 115bcb131aaSHajimu UMEMOTO #include "netdb_private.h" 1165342d17fSHajimu UMEMOTO #include "res_private.h" 1177d56d374SYoshinobu Inoue 1187d56d374SYoshinobu Inoue #ifndef MAXALIASES 1197d56d374SYoshinobu Inoue #define MAXALIASES 10 1207d56d374SYoshinobu Inoue #endif 1217d56d374SYoshinobu Inoue #ifndef MAXADDRS 1227d56d374SYoshinobu Inoue #define MAXADDRS 20 1237d56d374SYoshinobu Inoue #endif 1247d56d374SYoshinobu Inoue #ifndef MAXDNAME 1257d56d374SYoshinobu Inoue #define MAXDNAME 1025 1267d56d374SYoshinobu Inoue #endif 1277d56d374SYoshinobu Inoue 1287d56d374SYoshinobu Inoue #ifdef INET6 1297d56d374SYoshinobu Inoue #define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \ 1307d56d374SYoshinobu Inoue sizeof(struct in_addr)) 1317d56d374SYoshinobu Inoue #else 1327d56d374SYoshinobu Inoue #define ADDRLEN(af) sizeof(struct in_addr) 1337d56d374SYoshinobu Inoue #endif 1347d56d374SYoshinobu Inoue 1357d56d374SYoshinobu Inoue #define MAPADDR(ab, ina) \ 1367d56d374SYoshinobu Inoue do { \ 1377d56d374SYoshinobu Inoue memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \ 1387d56d374SYoshinobu Inoue memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \ 1397d56d374SYoshinobu Inoue memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \ 1407d56d374SYoshinobu Inoue } while (0) 1417d56d374SYoshinobu Inoue #define MAPADDRENABLED(flags) \ 1427d56d374SYoshinobu Inoue (((flags) & AI_V4MAPPED) || \ 1433e83e4a3SMichael Bushkov (((flags) & AI_V4MAPPED_CFG))) 1447d56d374SYoshinobu Inoue 1457d56d374SYoshinobu Inoue union inx_addr { 1467d56d374SYoshinobu Inoue struct in_addr in_addr; 1477d56d374SYoshinobu Inoue #ifdef INET6 1487d56d374SYoshinobu Inoue struct in6_addr in6_addr; 1497d56d374SYoshinobu Inoue #endif 1507d56d374SYoshinobu Inoue struct { 1517d56d374SYoshinobu Inoue u_char mau_zero[10]; 1527d56d374SYoshinobu Inoue u_char mau_one[2]; 1537d56d374SYoshinobu Inoue struct in_addr mau_inaddr; 1547d56d374SYoshinobu Inoue } map_addr_un; 1557d56d374SYoshinobu Inoue #define map_zero map_addr_un.mau_zero 1567d56d374SYoshinobu Inoue #define map_one map_addr_un.mau_one 1577d56d374SYoshinobu Inoue #define map_inaddr map_addr_un.mau_inaddr 1587d56d374SYoshinobu Inoue }; 1597d56d374SYoshinobu Inoue 160b8b31f33SHajimu UMEMOTO struct policyqueue { 161b8b31f33SHajimu UMEMOTO TAILQ_ENTRY(policyqueue) pc_entry; 162b8b31f33SHajimu UMEMOTO #ifdef INET6 163b8b31f33SHajimu UMEMOTO struct in6_addrpolicy pc_policy; 164b8b31f33SHajimu UMEMOTO #endif 165b8b31f33SHajimu UMEMOTO }; 166b8b31f33SHajimu UMEMOTO TAILQ_HEAD(policyhead, policyqueue); 167b8b31f33SHajimu UMEMOTO 168146cd1bcSHajimu UMEMOTO #define AIO_SRCFLAG_DEPRECATED 0x1 169146cd1bcSHajimu UMEMOTO 170146cd1bcSHajimu UMEMOTO struct hp_order { 171146cd1bcSHajimu UMEMOTO union { 172146cd1bcSHajimu UMEMOTO struct sockaddr_storage aiou_ss; 173146cd1bcSHajimu UMEMOTO struct sockaddr aiou_sa; 174146cd1bcSHajimu UMEMOTO } aio_src_un; 175146cd1bcSHajimu UMEMOTO #define aio_srcsa aio_src_un.aiou_sa 176146cd1bcSHajimu UMEMOTO u_int32_t aio_srcflag; 177146cd1bcSHajimu UMEMOTO int aio_srcscope; 178146cd1bcSHajimu UMEMOTO int aio_dstscope; 179146cd1bcSHajimu UMEMOTO struct policyqueue *aio_srcpolicy; 180146cd1bcSHajimu UMEMOTO struct policyqueue *aio_dstpolicy; 181146cd1bcSHajimu UMEMOTO union { 182146cd1bcSHajimu UMEMOTO struct sockaddr_storage aiou_ss; 183146cd1bcSHajimu UMEMOTO struct sockaddr aiou_sa; 184146cd1bcSHajimu UMEMOTO } aio_un; 185146cd1bcSHajimu UMEMOTO #define aio_sa aio_un.aiou_sa 186146cd1bcSHajimu UMEMOTO int aio_matchlen; 187d6fd3ed8SHajimu UMEMOTO char *aio_h_addr; 188af98aa6eSAndrey A. Chernov int aio_initial_sequence; 189146cd1bcSHajimu UMEMOTO }; 190146cd1bcSHajimu UMEMOTO 1915342d17fSHajimu UMEMOTO static struct hostent *_hpcopy(struct hostent *, int *); 1925342d17fSHajimu UMEMOTO static struct hostent *_hpaddr(int, const char *, void *, int *); 1937d56d374SYoshinobu Inoue #ifdef INET6 1943e83e4a3SMichael Bushkov static struct hostent *_hpmerge(struct hostent *, struct hostent *, int *); 1955342d17fSHajimu UMEMOTO static struct hostent *_hpmapv6(struct hostent *, int *); 1967d56d374SYoshinobu Inoue #endif 1975342d17fSHajimu UMEMOTO static struct hostent *_hpsort(struct hostent *, res_state); 1987d56d374SYoshinobu Inoue 199f09c52c3SHajimu UMEMOTO #ifdef INET6 2005342d17fSHajimu UMEMOTO static struct hostent *_hpreorder(struct hostent *); 201b8b31f33SHajimu UMEMOTO static int get_addrselectpolicy(struct policyhead *); 202b8b31f33SHajimu UMEMOTO static void free_addrselectpolicy(struct policyhead *); 203b8b31f33SHajimu UMEMOTO static struct policyqueue *match_addrselectpolicy(struct sockaddr *, 204b8b31f33SHajimu UMEMOTO struct policyhead *); 205146cd1bcSHajimu UMEMOTO static void set_source(struct hp_order *, struct policyhead *); 206146cd1bcSHajimu UMEMOTO static int matchlen(struct sockaddr *, struct sockaddr *); 207146cd1bcSHajimu UMEMOTO static int comp_dst(const void *, const void *); 208146cd1bcSHajimu UMEMOTO static int gai_addr2scopetype(struct sockaddr *); 209d38f4eadSHajimu UMEMOTO #endif 210b8b31f33SHajimu UMEMOTO 2117d56d374SYoshinobu Inoue /* 2127d56d374SYoshinobu Inoue * Functions defined in RFC2553 21373b30f0cSJun-ichiro itojun Hagino * getipnodebyname, getipnodebyaddr, freehostent 2147d56d374SYoshinobu Inoue */ 2157d56d374SYoshinobu Inoue 2163e83e4a3SMichael Bushkov struct hostent * 2173e83e4a3SMichael Bushkov getipnodebyname(const char *name, int af, int flags, int *errp) 2187d56d374SYoshinobu Inoue { 2197d56d374SYoshinobu Inoue struct hostent *hp; 2203e83e4a3SMichael Bushkov union inx_addr addrbuf; 2213e83e4a3SMichael Bushkov res_state statp; 2223e83e4a3SMichael Bushkov u_long options; 223248aee62SJacques Vidrine 2243e83e4a3SMichael Bushkov switch (af) { 2253e83e4a3SMichael Bushkov case AF_INET: 2263e83e4a3SMichael Bushkov #ifdef INET6 2273e83e4a3SMichael Bushkov case AF_INET6: 22806a99fe3SHajimu UMEMOTO #endif 2293e83e4a3SMichael Bushkov break; 2303e83e4a3SMichael Bushkov default: 2313e83e4a3SMichael Bushkov *errp = NO_RECOVERY; 2323e83e4a3SMichael Bushkov return NULL; 2333e83e4a3SMichael Bushkov } 2347d56d374SYoshinobu Inoue 2357d56d374SYoshinobu Inoue if (flags & AI_ADDRCONFIG) { 2367d56d374SYoshinobu Inoue int s; 2377d56d374SYoshinobu Inoue 23872531978SJilles Tjoelker if ((s = _socket(af, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0) 2390fbf0979SHajimu UMEMOTO return NULL; 2407d56d374SYoshinobu Inoue /* 2417d56d374SYoshinobu Inoue * TODO: 2427d56d374SYoshinobu Inoue * Note that implementation dependent test for address 2437d56d374SYoshinobu Inoue * configuration should be done every time called 24432223c1bSPedro F. Giffuni * (or appropriate interval), 2457d56d374SYoshinobu Inoue * because addresses will be dynamically assigned or deleted. 2467d56d374SYoshinobu Inoue */ 2479233c4d9SJason Evans _close(s); 2487d56d374SYoshinobu Inoue } 2497d56d374SYoshinobu Inoue 2507d56d374SYoshinobu Inoue #ifdef INET6 2517d56d374SYoshinobu Inoue /* special case for literal address */ 2527d56d374SYoshinobu Inoue if (inet_pton(AF_INET6, name, &addrbuf) == 1) { 2537d56d374SYoshinobu Inoue if (af != AF_INET6) { 2547d56d374SYoshinobu Inoue *errp = HOST_NOT_FOUND; 2557d56d374SYoshinobu Inoue return NULL; 2567d56d374SYoshinobu Inoue } 2577d56d374SYoshinobu Inoue return _hpaddr(af, name, &addrbuf, errp); 2587d56d374SYoshinobu Inoue } 2597d56d374SYoshinobu Inoue #endif 260be26adb5SYoshinobu Inoue if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) { 2617d56d374SYoshinobu Inoue if (af != AF_INET) { 2627d56d374SYoshinobu Inoue if (MAPADDRENABLED(flags)) { 2637d56d374SYoshinobu Inoue MAPADDR(&addrbuf, &addrbuf.in_addr); 2647d56d374SYoshinobu Inoue } else { 2657d56d374SYoshinobu Inoue *errp = HOST_NOT_FOUND; 2667d56d374SYoshinobu Inoue return NULL; 2677d56d374SYoshinobu Inoue } 2687d56d374SYoshinobu Inoue } 2697d56d374SYoshinobu Inoue return _hpaddr(af, name, &addrbuf, errp); 2707d56d374SYoshinobu Inoue } 2717d56d374SYoshinobu Inoue 2723e83e4a3SMichael Bushkov 2735342d17fSHajimu UMEMOTO statp = __res_state(); 2745342d17fSHajimu UMEMOTO if ((statp->options & RES_INIT) == 0) { 2755342d17fSHajimu UMEMOTO if (res_ninit(statp) < 0) { 2765342d17fSHajimu UMEMOTO *errp = NETDB_INTERNAL; 2775342d17fSHajimu UMEMOTO return NULL; 2785342d17fSHajimu UMEMOTO } 2795342d17fSHajimu UMEMOTO } 2805342d17fSHajimu UMEMOTO 2813e83e4a3SMichael Bushkov options = statp->options; 2823e83e4a3SMichael Bushkov statp->options &= ~RES_USE_INET6; 2833e83e4a3SMichael Bushkov 2843e83e4a3SMichael Bushkov hp = gethostbyname2(name, af); 2853e83e4a3SMichael Bushkov hp = _hpcopy(hp, errp); 2867d56d374SYoshinobu Inoue #ifdef INET6 287f09c52c3SHajimu UMEMOTO if (af == AF_INET6) 288f09c52c3SHajimu UMEMOTO hp = _hpreorder(hp); 289f09c52c3SHajimu UMEMOTO 290946e41f1SHajimu UMEMOTO if (af == AF_INET6 && ((flags & AI_ALL) || hp == NULL) && 291946e41f1SHajimu UMEMOTO MAPADDRENABLED(flags)) { 2923e83e4a3SMichael Bushkov struct hostent *hp2 = gethostbyname2(name, AF_INET); 2937d56d374SYoshinobu Inoue if (hp == NULL) 2943e83e4a3SMichael Bushkov if (hp2 == NULL) 2953e83e4a3SMichael Bushkov *errp = statp->res_h_errno; 2963e83e4a3SMichael Bushkov else 2977d56d374SYoshinobu Inoue hp = _hpmapv6(hp2, errp); 2987d56d374SYoshinobu Inoue else { 2993e83e4a3SMichael Bushkov if (hp2 && strcmp(hp->h_name, hp2->h_name) == 0) { 3003e83e4a3SMichael Bushkov struct hostent *hpb = hp; 3013e83e4a3SMichael Bushkov hp = _hpmerge(hpb, hp2, errp); 3023e83e4a3SMichael Bushkov freehostent(hpb); 3037d56d374SYoshinobu Inoue } 3047d56d374SYoshinobu Inoue } 3057d56d374SYoshinobu Inoue } 3067d56d374SYoshinobu Inoue #endif 3073e83e4a3SMichael Bushkov 3083e83e4a3SMichael Bushkov if (hp == NULL) 3093e83e4a3SMichael Bushkov *errp = statp->res_h_errno; 3103e83e4a3SMichael Bushkov 3113e83e4a3SMichael Bushkov statp->options = options; 312d38f4eadSHajimu UMEMOTO return _hpsort(hp, statp); 3137d56d374SYoshinobu Inoue } 3147d56d374SYoshinobu Inoue 3157d56d374SYoshinobu Inoue struct hostent * 3167d56d374SYoshinobu Inoue getipnodebyaddr(const void *src, size_t len, int af, int *errp) 3177d56d374SYoshinobu Inoue { 3187d56d374SYoshinobu Inoue struct hostent *hp; 3193e83e4a3SMichael Bushkov res_state statp; 3203e83e4a3SMichael Bushkov u_long options; 3213e83e4a3SMichael Bushkov 3227d56d374SYoshinobu Inoue #ifdef INET6 3237d56d374SYoshinobu Inoue struct in6_addr addrbuf; 3247d56d374SYoshinobu Inoue #else 3257d56d374SYoshinobu Inoue struct in_addr addrbuf; 3267d56d374SYoshinobu Inoue #endif 3277d56d374SYoshinobu Inoue 3287d56d374SYoshinobu Inoue switch (af) { 3297d56d374SYoshinobu Inoue case AF_INET: 3307d56d374SYoshinobu Inoue if (len != sizeof(struct in_addr)) { 3317d56d374SYoshinobu Inoue *errp = NO_RECOVERY; 3327d56d374SYoshinobu Inoue return NULL; 3337d56d374SYoshinobu Inoue } 33475a5de5aSPedro F. Giffuni if (rounddown2((long)src, sizeof(struct in_addr))) { 3357d56d374SYoshinobu Inoue memcpy(&addrbuf, src, len); 3367d56d374SYoshinobu Inoue src = &addrbuf; 3377d56d374SYoshinobu Inoue } 3387d56d374SYoshinobu Inoue if (((struct in_addr *)src)->s_addr == 0) 3397d56d374SYoshinobu Inoue return NULL; 3407d56d374SYoshinobu Inoue break; 3417d56d374SYoshinobu Inoue #ifdef INET6 3427d56d374SYoshinobu Inoue case AF_INET6: 3437d56d374SYoshinobu Inoue if (len != sizeof(struct in6_addr)) { 3447d56d374SYoshinobu Inoue *errp = NO_RECOVERY; 3457d56d374SYoshinobu Inoue return NULL; 3467d56d374SYoshinobu Inoue } 34775a5de5aSPedro F. Giffuni if (rounddown2((long)src, sizeof(struct in6_addr) / 2)) { 34875a5de5aSPedro F. Giffuni /* XXX */ 3497d56d374SYoshinobu Inoue memcpy(&addrbuf, src, len); 3507d56d374SYoshinobu Inoue src = &addrbuf; 3517d56d374SYoshinobu Inoue } 3523d670abcSYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src)) 3533d670abcSYoshinobu Inoue return NULL; 3547d56d374SYoshinobu Inoue if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src) 3557d56d374SYoshinobu Inoue || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) { 3567d56d374SYoshinobu Inoue src = (char *)src + 3577d56d374SYoshinobu Inoue (sizeof(struct in6_addr) - sizeof(struct in_addr)); 3587d56d374SYoshinobu Inoue af = AF_INET; 3597d56d374SYoshinobu Inoue len = sizeof(struct in_addr); 3607d56d374SYoshinobu Inoue } 3617d56d374SYoshinobu Inoue break; 3627d56d374SYoshinobu Inoue #endif 3637d56d374SYoshinobu Inoue default: 3647d56d374SYoshinobu Inoue *errp = NO_RECOVERY; 3657d56d374SYoshinobu Inoue return NULL; 3667d56d374SYoshinobu Inoue } 3677d56d374SYoshinobu Inoue 3683e83e4a3SMichael Bushkov statp = __res_state(); 3693e83e4a3SMichael Bushkov if ((statp->options & RES_INIT) == 0) { 3703e83e4a3SMichael Bushkov if (res_ninit(statp) < 0) { 3713e83e4a3SMichael Bushkov RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 3723e83e4a3SMichael Bushkov return NULL; 3733e83e4a3SMichael Bushkov } 3743e83e4a3SMichael Bushkov } 3753e83e4a3SMichael Bushkov 3763e83e4a3SMichael Bushkov options = statp->options; 3773e83e4a3SMichael Bushkov statp->options &= ~RES_USE_INET6; 3783e83e4a3SMichael Bushkov 3793e83e4a3SMichael Bushkov hp = gethostbyaddr(src, len, af); 3803e83e4a3SMichael Bushkov if (hp == NULL) 3813e83e4a3SMichael Bushkov *errp = statp->res_h_errno; 3823e83e4a3SMichael Bushkov 3833e83e4a3SMichael Bushkov statp->options = options; 3843e83e4a3SMichael Bushkov return (_hpcopy(hp, errp)); 3857d56d374SYoshinobu Inoue } 3867d56d374SYoshinobu Inoue 3877d56d374SYoshinobu Inoue void 3887d56d374SYoshinobu Inoue freehostent(struct hostent *ptr) 3897d56d374SYoshinobu Inoue { 3907d56d374SYoshinobu Inoue free(ptr); 3917d56d374SYoshinobu Inoue } 3927d56d374SYoshinobu Inoue 3937d56d374SYoshinobu Inoue /* 3947d56d374SYoshinobu Inoue * Private utility functions 3957d56d374SYoshinobu Inoue */ 3967d56d374SYoshinobu Inoue 3977d56d374SYoshinobu Inoue /* 3987d56d374SYoshinobu Inoue * _hpcopy: allocate and copy hostent structure 3997d56d374SYoshinobu Inoue */ 4007d56d374SYoshinobu Inoue static struct hostent * 4017d56d374SYoshinobu Inoue _hpcopy(struct hostent *hp, int *errp) 4027d56d374SYoshinobu Inoue { 4037d56d374SYoshinobu Inoue struct hostent *nhp; 4047d56d374SYoshinobu Inoue char *cp, **pp; 4057d56d374SYoshinobu Inoue int size, addrsize; 4067d56d374SYoshinobu Inoue int nalias = 0, naddr = 0; 4077d56d374SYoshinobu Inoue int al_off; 4087d56d374SYoshinobu Inoue int i; 4097d56d374SYoshinobu Inoue 4107d56d374SYoshinobu Inoue if (hp == NULL) 4117d56d374SYoshinobu Inoue return hp; 4127d56d374SYoshinobu Inoue 4137d56d374SYoshinobu Inoue /* count size to be allocated */ 4147d56d374SYoshinobu Inoue size = sizeof(struct hostent); 41505c36511SHajimu UMEMOTO if (hp->h_name != NULL) 4167d56d374SYoshinobu Inoue size += strlen(hp->h_name) + 1; 4177d56d374SYoshinobu Inoue if ((pp = hp->h_aliases) != NULL) { 4187d56d374SYoshinobu Inoue for (i = 0; *pp != NULL; i++, pp++) { 4197d56d374SYoshinobu Inoue if (**pp != '\0') { 4207d56d374SYoshinobu Inoue size += strlen(*pp) + 1; 4217d56d374SYoshinobu Inoue nalias++; 4227d56d374SYoshinobu Inoue } 4237d56d374SYoshinobu Inoue } 4247d56d374SYoshinobu Inoue } 4257d56d374SYoshinobu Inoue /* adjust alignment */ 4267d56d374SYoshinobu Inoue size = ALIGN(size); 4277d56d374SYoshinobu Inoue al_off = size; 4287d56d374SYoshinobu Inoue size += sizeof(char *) * (nalias + 1); 4297d56d374SYoshinobu Inoue addrsize = ALIGN(hp->h_length); 4307d56d374SYoshinobu Inoue if ((pp = hp->h_addr_list) != NULL) { 4317d56d374SYoshinobu Inoue while (*pp++ != NULL) 4327d56d374SYoshinobu Inoue naddr++; 4337d56d374SYoshinobu Inoue } 4347d56d374SYoshinobu Inoue size += addrsize * naddr; 4357d56d374SYoshinobu Inoue size += sizeof(char *) * (naddr + 1); 4367d56d374SYoshinobu Inoue 4377d56d374SYoshinobu Inoue /* copy */ 4387d56d374SYoshinobu Inoue if ((nhp = (struct hostent *)malloc(size)) == NULL) { 4397d56d374SYoshinobu Inoue *errp = TRY_AGAIN; 4407d56d374SYoshinobu Inoue return NULL; 4417d56d374SYoshinobu Inoue } 4427d56d374SYoshinobu Inoue cp = (char *)&nhp[1]; 44305c36511SHajimu UMEMOTO if (hp->h_name != NULL) { 4447d56d374SYoshinobu Inoue nhp->h_name = cp; 4457d56d374SYoshinobu Inoue strcpy(cp, hp->h_name); 4467d56d374SYoshinobu Inoue cp += strlen(cp) + 1; 4477d56d374SYoshinobu Inoue } else 4487d56d374SYoshinobu Inoue nhp->h_name = NULL; 4497d56d374SYoshinobu Inoue nhp->h_aliases = (char **)((char *)nhp + al_off); 4507d56d374SYoshinobu Inoue if ((pp = hp->h_aliases) != NULL) { 4517d56d374SYoshinobu Inoue for (i = 0; *pp != NULL; pp++) { 4527d56d374SYoshinobu Inoue if (**pp != '\0') { 4537d56d374SYoshinobu Inoue nhp->h_aliases[i++] = cp; 4547d56d374SYoshinobu Inoue strcpy(cp, *pp); 4557d56d374SYoshinobu Inoue cp += strlen(cp) + 1; 4567d56d374SYoshinobu Inoue } 4577d56d374SYoshinobu Inoue } 4587d56d374SYoshinobu Inoue } 4597d56d374SYoshinobu Inoue nhp->h_aliases[nalias] = NULL; 4607d56d374SYoshinobu Inoue cp = (char *)&nhp->h_aliases[nalias + 1]; 4617d56d374SYoshinobu Inoue nhp->h_addrtype = hp->h_addrtype; 4627d56d374SYoshinobu Inoue nhp->h_length = hp->h_length; 4637d56d374SYoshinobu Inoue nhp->h_addr_list = (char **)cp; 4647d56d374SYoshinobu Inoue if ((pp = hp->h_addr_list) != NULL) { 4657d56d374SYoshinobu Inoue cp = (char *)&nhp->h_addr_list[naddr + 1]; 4667d56d374SYoshinobu Inoue for (i = 0; *pp != NULL; pp++) { 4677d56d374SYoshinobu Inoue nhp->h_addr_list[i++] = cp; 4687d56d374SYoshinobu Inoue memcpy(cp, *pp, hp->h_length); 4697d56d374SYoshinobu Inoue cp += addrsize; 4707d56d374SYoshinobu Inoue } 4717d56d374SYoshinobu Inoue } 4727d56d374SYoshinobu Inoue nhp->h_addr_list[naddr] = NULL; 4737d56d374SYoshinobu Inoue return nhp; 4747d56d374SYoshinobu Inoue } 4757d56d374SYoshinobu Inoue 4767d56d374SYoshinobu Inoue /* 4777d56d374SYoshinobu Inoue * _hpaddr: construct hostent structure with one address 4787d56d374SYoshinobu Inoue */ 4797d56d374SYoshinobu Inoue static struct hostent * 4807d56d374SYoshinobu Inoue _hpaddr(int af, const char *name, void *addr, int *errp) 4817d56d374SYoshinobu Inoue { 4827d56d374SYoshinobu Inoue struct hostent *hp, hpbuf; 4837d56d374SYoshinobu Inoue char *addrs[2]; 4847d56d374SYoshinobu Inoue 4857d56d374SYoshinobu Inoue hp = &hpbuf; 4867d56d374SYoshinobu Inoue hp->h_name = (char *)name; 4877d56d374SYoshinobu Inoue hp->h_aliases = NULL; 4887d56d374SYoshinobu Inoue hp->h_addrtype = af; 4897d56d374SYoshinobu Inoue hp->h_length = ADDRLEN(af); 4907d56d374SYoshinobu Inoue hp->h_addr_list = addrs; 4917d56d374SYoshinobu Inoue addrs[0] = (char *)addr; 4927d56d374SYoshinobu Inoue addrs[1] = NULL; 4933e83e4a3SMichael Bushkov return (_hpcopy(hp, errp)); 4947d56d374SYoshinobu Inoue } 4957d56d374SYoshinobu Inoue 4963e83e4a3SMichael Bushkov #ifdef INET6 4977d56d374SYoshinobu Inoue /* 4987d56d374SYoshinobu Inoue * _hpmerge: merge 2 hostent structure, arguments will be freed 4997d56d374SYoshinobu Inoue */ 5007d56d374SYoshinobu Inoue static struct hostent * 5017d56d374SYoshinobu Inoue _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp) 5027d56d374SYoshinobu Inoue { 5037d56d374SYoshinobu Inoue int i, j; 5047d56d374SYoshinobu Inoue int naddr, nalias; 5057d56d374SYoshinobu Inoue char **pp; 5067d56d374SYoshinobu Inoue struct hostent *hp, hpbuf; 5077d56d374SYoshinobu Inoue char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1]; 5087d56d374SYoshinobu Inoue union inx_addr addrbuf[MAXADDRS]; 5097d56d374SYoshinobu Inoue 5107d56d374SYoshinobu Inoue if (hp1 == NULL) 5113e83e4a3SMichael Bushkov return _hpcopy(hp2, errp); 5127d56d374SYoshinobu Inoue if (hp2 == NULL) 5133e83e4a3SMichael Bushkov return _hpcopy(hp1, errp); 5147d56d374SYoshinobu Inoue 5157d56d374SYoshinobu Inoue #define HP(i) (i == 1 ? hp1 : hp2) 5167d56d374SYoshinobu Inoue hp = &hpbuf; 5177d56d374SYoshinobu Inoue hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name); 5187d56d374SYoshinobu Inoue hp->h_aliases = aliases; 5197d56d374SYoshinobu Inoue nalias = 0; 5207d56d374SYoshinobu Inoue for (i = 1; i <= 2; i++) { 5217d56d374SYoshinobu Inoue if ((pp = HP(i)->h_aliases) == NULL) 5227d56d374SYoshinobu Inoue continue; 5237d56d374SYoshinobu Inoue for (; nalias < MAXALIASES && *pp != NULL; pp++) { 5247d56d374SYoshinobu Inoue /* check duplicates */ 5257d56d374SYoshinobu Inoue for (j = 0; j < nalias; j++) 5267d56d374SYoshinobu Inoue if (strcasecmp(*pp, aliases[j]) == 0) 5277d56d374SYoshinobu Inoue break; 5287d56d374SYoshinobu Inoue if (j == nalias) 5297d56d374SYoshinobu Inoue aliases[nalias++] = *pp; 5307d56d374SYoshinobu Inoue } 5317d56d374SYoshinobu Inoue } 5327d56d374SYoshinobu Inoue aliases[nalias] = NULL; 5337d56d374SYoshinobu Inoue if (hp1->h_length != hp2->h_length) { 5347d56d374SYoshinobu Inoue hp->h_addrtype = AF_INET6; 5357d56d374SYoshinobu Inoue hp->h_length = sizeof(struct in6_addr); 5367d56d374SYoshinobu Inoue } else { 5377d56d374SYoshinobu Inoue hp->h_addrtype = hp1->h_addrtype; 5387d56d374SYoshinobu Inoue hp->h_length = hp1->h_length; 5397d56d374SYoshinobu Inoue } 5403e83e4a3SMichael Bushkov 5417d56d374SYoshinobu Inoue hp->h_addr_list = addrs; 5427d56d374SYoshinobu Inoue naddr = 0; 5437d56d374SYoshinobu Inoue for (i = 1; i <= 2; i++) { 5447d56d374SYoshinobu Inoue if ((pp = HP(i)->h_addr_list) == NULL) 5457d56d374SYoshinobu Inoue continue; 5467d56d374SYoshinobu Inoue if (HP(i)->h_length == hp->h_length) { 5477d56d374SYoshinobu Inoue while (naddr < MAXADDRS && *pp != NULL) 5487d56d374SYoshinobu Inoue addrs[naddr++] = *pp++; 5497d56d374SYoshinobu Inoue } else { 5507d56d374SYoshinobu Inoue /* copy IPv4 addr as mapped IPv6 addr */ 5517d56d374SYoshinobu Inoue while (naddr < MAXADDRS && *pp != NULL) { 5527d56d374SYoshinobu Inoue MAPADDR(&addrbuf[naddr], *pp++); 5537d56d374SYoshinobu Inoue addrs[naddr] = (char *)&addrbuf[naddr]; 5547d56d374SYoshinobu Inoue naddr++; 5557d56d374SYoshinobu Inoue } 5567d56d374SYoshinobu Inoue } 5577d56d374SYoshinobu Inoue } 5587d56d374SYoshinobu Inoue addrs[naddr] = NULL; 5593e83e4a3SMichael Bushkov return (_hpcopy(hp, errp)); 5607d56d374SYoshinobu Inoue } 5613e83e4a3SMichael Bushkov #endif 5627d56d374SYoshinobu Inoue 5637d56d374SYoshinobu Inoue /* 5647d56d374SYoshinobu Inoue * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses 5657d56d374SYoshinobu Inoue */ 5667d56d374SYoshinobu Inoue #ifdef INET6 5677d56d374SYoshinobu Inoue static struct hostent * 5687d56d374SYoshinobu Inoue _hpmapv6(struct hostent *hp, int *errp) 5697d56d374SYoshinobu Inoue { 5703e83e4a3SMichael Bushkov struct hostent hp6; 5717d56d374SYoshinobu Inoue 5727d56d374SYoshinobu Inoue if (hp == NULL) 5737d56d374SYoshinobu Inoue return NULL; 5747d56d374SYoshinobu Inoue if (hp->h_addrtype == AF_INET6) 5753e83e4a3SMichael Bushkov return _hpcopy(hp, errp); 5767d56d374SYoshinobu Inoue 5773e83e4a3SMichael Bushkov memset(&hp6, 0, sizeof(struct hostent)); 5783e83e4a3SMichael Bushkov hp6.h_addrtype = AF_INET6; 5793e83e4a3SMichael Bushkov hp6.h_length = sizeof(struct in6_addr); 5803e83e4a3SMichael Bushkov return _hpmerge(&hp6, hp, errp); 5817d56d374SYoshinobu Inoue } 5827d56d374SYoshinobu Inoue #endif 5837d56d374SYoshinobu Inoue 5847d56d374SYoshinobu Inoue /* 5857d56d374SYoshinobu Inoue * _hpsort: sort address by sortlist 5867d56d374SYoshinobu Inoue */ 5877d56d374SYoshinobu Inoue static struct hostent * 5885342d17fSHajimu UMEMOTO _hpsort(struct hostent *hp, res_state statp) 5897d56d374SYoshinobu Inoue { 5907d56d374SYoshinobu Inoue int i, j, n; 5917d56d374SYoshinobu Inoue u_char *ap, *sp, *mp, **pp; 5927d56d374SYoshinobu Inoue char t; 5937d56d374SYoshinobu Inoue char order[MAXADDRS]; 5945342d17fSHajimu UMEMOTO int nsort = statp->nsort; 5957d56d374SYoshinobu Inoue 5967d56d374SYoshinobu Inoue if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0) 5977d56d374SYoshinobu Inoue return hp; 5987d56d374SYoshinobu Inoue for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) { 5997d56d374SYoshinobu Inoue for (j = 0; j < nsort; j++) { 6007d56d374SYoshinobu Inoue #ifdef INET6 6015342d17fSHajimu UMEMOTO if (statp->_u._ext.ext->sort_list[j].af != 6025342d17fSHajimu UMEMOTO hp->h_addrtype) 6037d56d374SYoshinobu Inoue continue; 6045342d17fSHajimu UMEMOTO sp = (u_char *)&statp->_u._ext.ext->sort_list[j].addr; 6055342d17fSHajimu UMEMOTO mp = (u_char *)&statp->_u._ext.ext->sort_list[j].mask; 6067d56d374SYoshinobu Inoue #else 6075342d17fSHajimu UMEMOTO sp = (u_char *)&statp->sort_list[j].addr; 6085342d17fSHajimu UMEMOTO mp = (u_char *)&statp->sort_list[j].mask; 6097d56d374SYoshinobu Inoue #endif 6107d56d374SYoshinobu Inoue for (n = 0; n < hp->h_length; n++) { 6117d56d374SYoshinobu Inoue if ((ap[n] & mp[n]) != sp[n]) 6127d56d374SYoshinobu Inoue break; 6137d56d374SYoshinobu Inoue } 6147d56d374SYoshinobu Inoue if (n == hp->h_length) 6157d56d374SYoshinobu Inoue break; 6167d56d374SYoshinobu Inoue } 6177d56d374SYoshinobu Inoue order[i] = j; 6187d56d374SYoshinobu Inoue } 6197d56d374SYoshinobu Inoue n = i; 6207d56d374SYoshinobu Inoue pp = (u_char **)hp->h_addr_list; 6217d56d374SYoshinobu Inoue for (i = 0; i < n - 1; i++) { 6227d56d374SYoshinobu Inoue for (j = i + 1; j < n; j++) { 6237d56d374SYoshinobu Inoue if (order[i] > order[j]) { 6247d56d374SYoshinobu Inoue ap = pp[i]; 6257d56d374SYoshinobu Inoue pp[i] = pp[j]; 6267d56d374SYoshinobu Inoue pp[j] = ap; 6277d56d374SYoshinobu Inoue t = order[i]; 6287d56d374SYoshinobu Inoue order[i] = order[j]; 6297d56d374SYoshinobu Inoue order[j] = t; 6307d56d374SYoshinobu Inoue } 6317d56d374SYoshinobu Inoue } 6327d56d374SYoshinobu Inoue } 6337d56d374SYoshinobu Inoue return hp; 6347d56d374SYoshinobu Inoue } 6357d56d374SYoshinobu Inoue 636f09c52c3SHajimu UMEMOTO #ifdef INET6 6377d56d374SYoshinobu Inoue /* 638b8b31f33SHajimu UMEMOTO * _hpreorder: sort address by default address selection 639b8b31f33SHajimu UMEMOTO */ 640b8b31f33SHajimu UMEMOTO static struct hostent * 641b8b31f33SHajimu UMEMOTO _hpreorder(struct hostent *hp) 642b8b31f33SHajimu UMEMOTO { 643146cd1bcSHajimu UMEMOTO struct hp_order *aio; 644146cd1bcSHajimu UMEMOTO int i, n; 645d6fd3ed8SHajimu UMEMOTO char *ap; 646146cd1bcSHajimu UMEMOTO struct sockaddr *sa; 647b8b31f33SHajimu UMEMOTO struct policyhead policyhead; 648b8b31f33SHajimu UMEMOTO 649146cd1bcSHajimu UMEMOTO if (hp == NULL) 650b8b31f33SHajimu UMEMOTO return hp; 651b8b31f33SHajimu UMEMOTO 652b8b31f33SHajimu UMEMOTO switch (hp->h_addrtype) { 653b8b31f33SHajimu UMEMOTO case AF_INET: 654b8b31f33SHajimu UMEMOTO #ifdef INET6 655b8b31f33SHajimu UMEMOTO case AF_INET6: 656b8b31f33SHajimu UMEMOTO #endif 657146cd1bcSHajimu UMEMOTO break; 658b8b31f33SHajimu UMEMOTO default: 659b8b31f33SHajimu UMEMOTO return hp; 660b8b31f33SHajimu UMEMOTO } 661b8b31f33SHajimu UMEMOTO 662146cd1bcSHajimu UMEMOTO /* count the number of addrinfo elements for sorting. */ 663146cd1bcSHajimu UMEMOTO for (n = 0; hp->h_addr_list[n] != NULL; n++) 664146cd1bcSHajimu UMEMOTO ; 665146cd1bcSHajimu UMEMOTO 666146cd1bcSHajimu UMEMOTO /* 667146cd1bcSHajimu UMEMOTO * If the number is small enough, we can skip the reordering process. 668146cd1bcSHajimu UMEMOTO */ 669146cd1bcSHajimu UMEMOTO if (n <= 1) 670146cd1bcSHajimu UMEMOTO return hp; 671146cd1bcSHajimu UMEMOTO 672146cd1bcSHajimu UMEMOTO /* allocate a temporary array for sort and initialization of it. */ 673146cd1bcSHajimu UMEMOTO if ((aio = malloc(sizeof(*aio) * n)) == NULL) 674146cd1bcSHajimu UMEMOTO return hp; /* give up reordering */ 675146cd1bcSHajimu UMEMOTO memset(aio, 0, sizeof(*aio) * n); 676146cd1bcSHajimu UMEMOTO 677146cd1bcSHajimu UMEMOTO /* retrieve address selection policy from the kernel */ 678146cd1bcSHajimu UMEMOTO TAILQ_INIT(&policyhead); 679146cd1bcSHajimu UMEMOTO if (!get_addrselectpolicy(&policyhead)) { 680146cd1bcSHajimu UMEMOTO /* no policy is installed into kernel, we don't sort. */ 681146cd1bcSHajimu UMEMOTO free(aio); 682146cd1bcSHajimu UMEMOTO return hp; 683b8b31f33SHajimu UMEMOTO } 684b8b31f33SHajimu UMEMOTO 685146cd1bcSHajimu UMEMOTO for (i = 0; i < n; i++) { 686d6fd3ed8SHajimu UMEMOTO ap = hp->h_addr_list[i]; 687146cd1bcSHajimu UMEMOTO aio[i].aio_h_addr = ap; 688146cd1bcSHajimu UMEMOTO sa = &aio[i].aio_sa; 689146cd1bcSHajimu UMEMOTO switch (hp->h_addrtype) { 690146cd1bcSHajimu UMEMOTO case AF_INET: 691146cd1bcSHajimu UMEMOTO sa->sa_family = AF_INET; 692146cd1bcSHajimu UMEMOTO sa->sa_len = sizeof(struct sockaddr_in); 693146cd1bcSHajimu UMEMOTO memcpy(&((struct sockaddr_in *)sa)->sin_addr, ap, 694146cd1bcSHajimu UMEMOTO sizeof(struct in_addr)); 695146cd1bcSHajimu UMEMOTO break; 696146cd1bcSHajimu UMEMOTO #ifdef INET6 697146cd1bcSHajimu UMEMOTO case AF_INET6: 698146cd1bcSHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) { 699146cd1bcSHajimu UMEMOTO sa->sa_family = AF_INET; 700146cd1bcSHajimu UMEMOTO sa->sa_len = sizeof(struct sockaddr_in); 701146cd1bcSHajimu UMEMOTO memcpy(&((struct sockaddr_in *)sa)->sin_addr, 702146cd1bcSHajimu UMEMOTO &ap[12], sizeof(struct in_addr)); 703146cd1bcSHajimu UMEMOTO } else { 704146cd1bcSHajimu UMEMOTO sa->sa_family = AF_INET6; 705146cd1bcSHajimu UMEMOTO sa->sa_len = sizeof(struct sockaddr_in6); 706146cd1bcSHajimu UMEMOTO memcpy(&((struct sockaddr_in6 *)sa)->sin6_addr, 707146cd1bcSHajimu UMEMOTO ap, sizeof(struct in6_addr)); 708146cd1bcSHajimu UMEMOTO } 709146cd1bcSHajimu UMEMOTO break; 710146cd1bcSHajimu UMEMOTO #endif 711146cd1bcSHajimu UMEMOTO } 712146cd1bcSHajimu UMEMOTO aio[i].aio_dstscope = gai_addr2scopetype(sa); 713146cd1bcSHajimu UMEMOTO aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead); 714146cd1bcSHajimu UMEMOTO set_source(&aio[i], &policyhead); 715af98aa6eSAndrey A. Chernov aio[i].aio_initial_sequence = i; 716146cd1bcSHajimu UMEMOTO } 717146cd1bcSHajimu UMEMOTO 718146cd1bcSHajimu UMEMOTO /* perform sorting. */ 719146cd1bcSHajimu UMEMOTO qsort(aio, n, sizeof(*aio), comp_dst); 720146cd1bcSHajimu UMEMOTO 721146cd1bcSHajimu UMEMOTO /* reorder the h_addr_list. */ 722146cd1bcSHajimu UMEMOTO for (i = 0; i < n; i++) 723146cd1bcSHajimu UMEMOTO hp->h_addr_list[i] = aio[i].aio_h_addr; 724146cd1bcSHajimu UMEMOTO 725b8b31f33SHajimu UMEMOTO /* cleanup and return */ 726146cd1bcSHajimu UMEMOTO free(aio); 727b8b31f33SHajimu UMEMOTO free_addrselectpolicy(&policyhead); 728b8b31f33SHajimu UMEMOTO return hp; 729b8b31f33SHajimu UMEMOTO } 730b8b31f33SHajimu UMEMOTO 731b8b31f33SHajimu UMEMOTO static int 73290d1d883SHajimu UMEMOTO get_addrselectpolicy(struct policyhead *head) 733b8b31f33SHajimu UMEMOTO { 734b8b31f33SHajimu UMEMOTO #ifdef INET6 735b8b31f33SHajimu UMEMOTO int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY }; 736b8b31f33SHajimu UMEMOTO size_t l; 737b8b31f33SHajimu UMEMOTO char *buf; 738b8b31f33SHajimu UMEMOTO struct in6_addrpolicy *pol, *ep; 739b8b31f33SHajimu UMEMOTO 740bf51882aSPedro F. Giffuni if (sysctl(mib, nitems(mib), NULL, &l, NULL, 0) < 0) 741b8b31f33SHajimu UMEMOTO return (0); 742b8b31f33SHajimu UMEMOTO if ((buf = malloc(l)) == NULL) 743b8b31f33SHajimu UMEMOTO return (0); 744bf51882aSPedro F. Giffuni if (sysctl(mib, nitems(mib), buf, &l, NULL, 0) < 0) { 745b8b31f33SHajimu UMEMOTO free(buf); 746b8b31f33SHajimu UMEMOTO return (0); 747b8b31f33SHajimu UMEMOTO } 748b8b31f33SHajimu UMEMOTO 749b8b31f33SHajimu UMEMOTO ep = (struct in6_addrpolicy *)(buf + l); 750b8b31f33SHajimu UMEMOTO for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) { 751b8b31f33SHajimu UMEMOTO struct policyqueue *new; 752b8b31f33SHajimu UMEMOTO 753b8b31f33SHajimu UMEMOTO if ((new = malloc(sizeof(*new))) == NULL) { 754b8b31f33SHajimu UMEMOTO free_addrselectpolicy(head); /* make the list empty */ 755b8b31f33SHajimu UMEMOTO break; 756b8b31f33SHajimu UMEMOTO } 757b8b31f33SHajimu UMEMOTO new->pc_policy = *pol; 758b8b31f33SHajimu UMEMOTO TAILQ_INSERT_TAIL(head, new, pc_entry); 759b8b31f33SHajimu UMEMOTO } 760b8b31f33SHajimu UMEMOTO 761b8b31f33SHajimu UMEMOTO free(buf); 762b8b31f33SHajimu UMEMOTO return (1); 763b8b31f33SHajimu UMEMOTO #else 764b8b31f33SHajimu UMEMOTO return (0); 765b8b31f33SHajimu UMEMOTO #endif 766b8b31f33SHajimu UMEMOTO } 767b8b31f33SHajimu UMEMOTO 768b8b31f33SHajimu UMEMOTO static void 76990d1d883SHajimu UMEMOTO free_addrselectpolicy(struct policyhead *head) 770b8b31f33SHajimu UMEMOTO { 771b8b31f33SHajimu UMEMOTO struct policyqueue *ent, *nent; 772b8b31f33SHajimu UMEMOTO 773b8b31f33SHajimu UMEMOTO for (ent = TAILQ_FIRST(head); ent; ent = nent) { 774b8b31f33SHajimu UMEMOTO nent = TAILQ_NEXT(ent, pc_entry); 775b8b31f33SHajimu UMEMOTO TAILQ_REMOVE(head, ent, pc_entry); 776b8b31f33SHajimu UMEMOTO free(ent); 777b8b31f33SHajimu UMEMOTO } 778b8b31f33SHajimu UMEMOTO } 779b8b31f33SHajimu UMEMOTO 780b8b31f33SHajimu UMEMOTO static struct policyqueue * 78190d1d883SHajimu UMEMOTO match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head) 782b8b31f33SHajimu UMEMOTO { 783b8b31f33SHajimu UMEMOTO #ifdef INET6 784b8b31f33SHajimu UMEMOTO struct policyqueue *ent, *bestent = NULL; 785b8b31f33SHajimu UMEMOTO struct in6_addrpolicy *pol; 786b8b31f33SHajimu UMEMOTO int matchlen, bestmatchlen = -1; 787b8b31f33SHajimu UMEMOTO u_char *mp, *ep, *k, *p, m; 788b8b31f33SHajimu UMEMOTO struct sockaddr_in6 key; 789b8b31f33SHajimu UMEMOTO 790b8b31f33SHajimu UMEMOTO switch(addr->sa_family) { 791b8b31f33SHajimu UMEMOTO case AF_INET6: 792b8b31f33SHajimu UMEMOTO key = *(struct sockaddr_in6 *)addr; 793b8b31f33SHajimu UMEMOTO break; 794b8b31f33SHajimu UMEMOTO case AF_INET: 795b8b31f33SHajimu UMEMOTO /* convert the address into IPv4-mapped IPv6 address. */ 796b8b31f33SHajimu UMEMOTO memset(&key, 0, sizeof(key)); 797b8b31f33SHajimu UMEMOTO key.sin6_family = AF_INET6; 798b8b31f33SHajimu UMEMOTO key.sin6_len = sizeof(key); 799bf231994SHajimu UMEMOTO _map_v4v6_address( 800bf231994SHajimu UMEMOTO (char *)&((struct sockaddr_in *)addr)->sin_addr, 801bf231994SHajimu UMEMOTO (char *)&key.sin6_addr); 802b8b31f33SHajimu UMEMOTO break; 803b8b31f33SHajimu UMEMOTO default: 804b8b31f33SHajimu UMEMOTO return(NULL); 805b8b31f33SHajimu UMEMOTO } 806b8b31f33SHajimu UMEMOTO 807b8b31f33SHajimu UMEMOTO for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) { 808b8b31f33SHajimu UMEMOTO pol = &ent->pc_policy; 809b8b31f33SHajimu UMEMOTO matchlen = 0; 810b8b31f33SHajimu UMEMOTO 811b8b31f33SHajimu UMEMOTO mp = (u_char *)&pol->addrmask.sin6_addr; 812b8b31f33SHajimu UMEMOTO ep = mp + 16; /* XXX: scope field? */ 813b8b31f33SHajimu UMEMOTO k = (u_char *)&key.sin6_addr; 814b8b31f33SHajimu UMEMOTO p = (u_char *)&pol->addr.sin6_addr; 815b8b31f33SHajimu UMEMOTO for (; mp < ep && *mp; mp++, k++, p++) { 816b8b31f33SHajimu UMEMOTO m = *mp; 817b8b31f33SHajimu UMEMOTO if ((*k & m) != *p) 818b8b31f33SHajimu UMEMOTO goto next; /* not match */ 819b8b31f33SHajimu UMEMOTO if (m == 0xff) /* short cut for a typical case */ 820b8b31f33SHajimu UMEMOTO matchlen += 8; 821b8b31f33SHajimu UMEMOTO else { 822b8b31f33SHajimu UMEMOTO while (m >= 0x80) { 823b8b31f33SHajimu UMEMOTO matchlen++; 824b8b31f33SHajimu UMEMOTO m <<= 1; 825b8b31f33SHajimu UMEMOTO } 826b8b31f33SHajimu UMEMOTO } 827b8b31f33SHajimu UMEMOTO } 828b8b31f33SHajimu UMEMOTO 829b8b31f33SHajimu UMEMOTO /* matched. check if this is better than the current best. */ 830b8b31f33SHajimu UMEMOTO if (matchlen > bestmatchlen) { 831b8b31f33SHajimu UMEMOTO bestent = ent; 832b8b31f33SHajimu UMEMOTO bestmatchlen = matchlen; 833b8b31f33SHajimu UMEMOTO } 834b8b31f33SHajimu UMEMOTO 835b8b31f33SHajimu UMEMOTO next: 836b8b31f33SHajimu UMEMOTO continue; 837b8b31f33SHajimu UMEMOTO } 838b8b31f33SHajimu UMEMOTO 839b8b31f33SHajimu UMEMOTO return(bestent); 840b8b31f33SHajimu UMEMOTO #else 841b8b31f33SHajimu UMEMOTO return(NULL); 842b8b31f33SHajimu UMEMOTO #endif 843b8b31f33SHajimu UMEMOTO 844b8b31f33SHajimu UMEMOTO } 845b8b31f33SHajimu UMEMOTO 846146cd1bcSHajimu UMEMOTO static void 84790d1d883SHajimu UMEMOTO set_source(struct hp_order *aio, struct policyhead *ph) 848146cd1bcSHajimu UMEMOTO { 849146cd1bcSHajimu UMEMOTO struct sockaddr_storage ss = aio->aio_un.aiou_ss; 850943db5a2SHajimu UMEMOTO socklen_t srclen; 851943db5a2SHajimu UMEMOTO int s; 852146cd1bcSHajimu UMEMOTO 853146cd1bcSHajimu UMEMOTO /* set unspec ("no source is available"), just in case */ 854146cd1bcSHajimu UMEMOTO aio->aio_srcsa.sa_family = AF_UNSPEC; 855146cd1bcSHajimu UMEMOTO aio->aio_srcscope = -1; 856146cd1bcSHajimu UMEMOTO 857146cd1bcSHajimu UMEMOTO switch(ss.ss_family) { 858146cd1bcSHajimu UMEMOTO case AF_INET: 859146cd1bcSHajimu UMEMOTO ((struct sockaddr_in *)&ss)->sin_port = htons(1); 860146cd1bcSHajimu UMEMOTO break; 861146cd1bcSHajimu UMEMOTO #ifdef INET6 862146cd1bcSHajimu UMEMOTO case AF_INET6: 863146cd1bcSHajimu UMEMOTO ((struct sockaddr_in6 *)&ss)->sin6_port = htons(1); 864146cd1bcSHajimu UMEMOTO break; 865146cd1bcSHajimu UMEMOTO #endif 866146cd1bcSHajimu UMEMOTO default: /* ignore unsupported AFs explicitly */ 867146cd1bcSHajimu UMEMOTO return; 868146cd1bcSHajimu UMEMOTO } 869146cd1bcSHajimu UMEMOTO 870146cd1bcSHajimu UMEMOTO /* open a socket to get the source address for the given dst */ 87172531978SJilles Tjoelker if ((s = _socket(ss.ss_family, SOCK_DGRAM | SOCK_CLOEXEC, 87272531978SJilles Tjoelker IPPROTO_UDP)) < 0) 873146cd1bcSHajimu UMEMOTO return; /* give up */ 874146cd1bcSHajimu UMEMOTO if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0) 875146cd1bcSHajimu UMEMOTO goto cleanup; 876146cd1bcSHajimu UMEMOTO srclen = ss.ss_len; 877146cd1bcSHajimu UMEMOTO if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) { 878146cd1bcSHajimu UMEMOTO aio->aio_srcsa.sa_family = AF_UNSPEC; 879146cd1bcSHajimu UMEMOTO goto cleanup; 880146cd1bcSHajimu UMEMOTO } 881146cd1bcSHajimu UMEMOTO aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa); 882146cd1bcSHajimu UMEMOTO aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph); 883146cd1bcSHajimu UMEMOTO aio->aio_matchlen = matchlen(&aio->aio_srcsa, (struct sockaddr *)&ss); 884146cd1bcSHajimu UMEMOTO #ifdef INET6 885146cd1bcSHajimu UMEMOTO if (ss.ss_family == AF_INET6) { 886146cd1bcSHajimu UMEMOTO struct in6_ifreq ifr6; 887146cd1bcSHajimu UMEMOTO u_int32_t flags6; 888146cd1bcSHajimu UMEMOTO 889146cd1bcSHajimu UMEMOTO memset(&ifr6, 0, sizeof(ifr6)); 890146cd1bcSHajimu UMEMOTO memcpy(&ifr6.ifr_addr, &ss, ss.ss_len); 891146cd1bcSHajimu UMEMOTO if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) { 892146cd1bcSHajimu UMEMOTO flags6 = ifr6.ifr_ifru.ifru_flags6; 893146cd1bcSHajimu UMEMOTO if ((flags6 & IN6_IFF_DEPRECATED)) 894146cd1bcSHajimu UMEMOTO aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED; 895146cd1bcSHajimu UMEMOTO } 896146cd1bcSHajimu UMEMOTO } 897146cd1bcSHajimu UMEMOTO #endif 898146cd1bcSHajimu UMEMOTO 899146cd1bcSHajimu UMEMOTO cleanup: 900146cd1bcSHajimu UMEMOTO _close(s); 901146cd1bcSHajimu UMEMOTO return; 902146cd1bcSHajimu UMEMOTO } 903146cd1bcSHajimu UMEMOTO 904146cd1bcSHajimu UMEMOTO static int 90590d1d883SHajimu UMEMOTO matchlen(struct sockaddr *src, struct sockaddr *dst) 906146cd1bcSHajimu UMEMOTO { 907146cd1bcSHajimu UMEMOTO int match = 0; 908146cd1bcSHajimu UMEMOTO u_char *s, *d; 909146cd1bcSHajimu UMEMOTO u_char *lim, r; 910146cd1bcSHajimu UMEMOTO int addrlen; 911146cd1bcSHajimu UMEMOTO 912146cd1bcSHajimu UMEMOTO switch (src->sa_family) { 913146cd1bcSHajimu UMEMOTO #ifdef INET6 914146cd1bcSHajimu UMEMOTO case AF_INET6: 915146cd1bcSHajimu UMEMOTO s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr; 916146cd1bcSHajimu UMEMOTO d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr; 917146cd1bcSHajimu UMEMOTO addrlen = sizeof(struct in6_addr); 918146cd1bcSHajimu UMEMOTO lim = s + addrlen; 919146cd1bcSHajimu UMEMOTO break; 920146cd1bcSHajimu UMEMOTO #endif 921146cd1bcSHajimu UMEMOTO case AF_INET: 922fd57e549SGeorge V. Neville-Neil s = (u_char *)&((struct sockaddr_in *)src)->sin_addr; 923fd57e549SGeorge V. Neville-Neil d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr; 924146cd1bcSHajimu UMEMOTO addrlen = sizeof(struct in_addr); 925146cd1bcSHajimu UMEMOTO lim = s + addrlen; 926146cd1bcSHajimu UMEMOTO break; 927146cd1bcSHajimu UMEMOTO default: 928146cd1bcSHajimu UMEMOTO return(0); 929146cd1bcSHajimu UMEMOTO } 930146cd1bcSHajimu UMEMOTO 931146cd1bcSHajimu UMEMOTO while (s < lim) 932146cd1bcSHajimu UMEMOTO if ((r = (*d++ ^ *s++)) != 0) { 933e1ec67bbSAndrey A. Chernov while ((r & 0x80) == 0) { 934146cd1bcSHajimu UMEMOTO match++; 935146cd1bcSHajimu UMEMOTO r <<= 1; 936146cd1bcSHajimu UMEMOTO } 937146cd1bcSHajimu UMEMOTO break; 938146cd1bcSHajimu UMEMOTO } else 939146cd1bcSHajimu UMEMOTO match += 8; 940146cd1bcSHajimu UMEMOTO return(match); 941146cd1bcSHajimu UMEMOTO } 942146cd1bcSHajimu UMEMOTO 943146cd1bcSHajimu UMEMOTO static int 94490d1d883SHajimu UMEMOTO comp_dst(const void *arg1, const void *arg2) 945146cd1bcSHajimu UMEMOTO { 946146cd1bcSHajimu UMEMOTO const struct hp_order *dst1 = arg1, *dst2 = arg2; 947146cd1bcSHajimu UMEMOTO 948146cd1bcSHajimu UMEMOTO /* 949146cd1bcSHajimu UMEMOTO * Rule 1: Avoid unusable destinations. 950146cd1bcSHajimu UMEMOTO * XXX: we currently do not consider if an appropriate route exists. 951146cd1bcSHajimu UMEMOTO */ 952146cd1bcSHajimu UMEMOTO if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 953146cd1bcSHajimu UMEMOTO dst2->aio_srcsa.sa_family == AF_UNSPEC) { 954146cd1bcSHajimu UMEMOTO return(-1); 955146cd1bcSHajimu UMEMOTO } 956146cd1bcSHajimu UMEMOTO if (dst1->aio_srcsa.sa_family == AF_UNSPEC && 957146cd1bcSHajimu UMEMOTO dst2->aio_srcsa.sa_family != AF_UNSPEC) { 958146cd1bcSHajimu UMEMOTO return(1); 959146cd1bcSHajimu UMEMOTO } 960146cd1bcSHajimu UMEMOTO 961146cd1bcSHajimu UMEMOTO /* Rule 2: Prefer matching scope. */ 962146cd1bcSHajimu UMEMOTO if (dst1->aio_dstscope == dst1->aio_srcscope && 963146cd1bcSHajimu UMEMOTO dst2->aio_dstscope != dst2->aio_srcscope) { 964146cd1bcSHajimu UMEMOTO return(-1); 965146cd1bcSHajimu UMEMOTO } 966146cd1bcSHajimu UMEMOTO if (dst1->aio_dstscope != dst1->aio_srcscope && 967146cd1bcSHajimu UMEMOTO dst2->aio_dstscope == dst2->aio_srcscope) { 968146cd1bcSHajimu UMEMOTO return(1); 969146cd1bcSHajimu UMEMOTO } 970146cd1bcSHajimu UMEMOTO 971146cd1bcSHajimu UMEMOTO /* Rule 3: Avoid deprecated addresses. */ 972146cd1bcSHajimu UMEMOTO if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 973146cd1bcSHajimu UMEMOTO dst2->aio_srcsa.sa_family != AF_UNSPEC) { 974146cd1bcSHajimu UMEMOTO if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 975146cd1bcSHajimu UMEMOTO (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 976146cd1bcSHajimu UMEMOTO return(-1); 977146cd1bcSHajimu UMEMOTO } 978146cd1bcSHajimu UMEMOTO if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 979146cd1bcSHajimu UMEMOTO !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 980146cd1bcSHajimu UMEMOTO return(1); 981146cd1bcSHajimu UMEMOTO } 982146cd1bcSHajimu UMEMOTO } 983146cd1bcSHajimu UMEMOTO 984146cd1bcSHajimu UMEMOTO /* Rule 4: Prefer home addresses. */ 985146cd1bcSHajimu UMEMOTO /* XXX: not implemented yet */ 986146cd1bcSHajimu UMEMOTO 987146cd1bcSHajimu UMEMOTO /* Rule 5: Prefer matching label. */ 988146cd1bcSHajimu UMEMOTO #ifdef INET6 989146cd1bcSHajimu UMEMOTO if (dst1->aio_srcpolicy && dst1->aio_dstpolicy && 990146cd1bcSHajimu UMEMOTO dst1->aio_srcpolicy->pc_policy.label == 991146cd1bcSHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.label && 992146cd1bcSHajimu UMEMOTO (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL || 993146cd1bcSHajimu UMEMOTO dst2->aio_srcpolicy->pc_policy.label != 994146cd1bcSHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.label)) { 995146cd1bcSHajimu UMEMOTO return(-1); 996146cd1bcSHajimu UMEMOTO } 997146cd1bcSHajimu UMEMOTO if (dst2->aio_srcpolicy && dst2->aio_dstpolicy && 998146cd1bcSHajimu UMEMOTO dst2->aio_srcpolicy->pc_policy.label == 999146cd1bcSHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.label && 1000146cd1bcSHajimu UMEMOTO (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL || 1001146cd1bcSHajimu UMEMOTO dst1->aio_srcpolicy->pc_policy.label != 1002146cd1bcSHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.label)) { 1003146cd1bcSHajimu UMEMOTO return(1); 1004146cd1bcSHajimu UMEMOTO } 1005146cd1bcSHajimu UMEMOTO #endif 1006146cd1bcSHajimu UMEMOTO 1007146cd1bcSHajimu UMEMOTO /* Rule 6: Prefer higher precedence. */ 1008146cd1bcSHajimu UMEMOTO #ifdef INET6 1009146cd1bcSHajimu UMEMOTO if (dst1->aio_dstpolicy && 1010146cd1bcSHajimu UMEMOTO (dst2->aio_dstpolicy == NULL || 1011146cd1bcSHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.preced > 1012146cd1bcSHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.preced)) { 1013146cd1bcSHajimu UMEMOTO return(-1); 1014146cd1bcSHajimu UMEMOTO } 1015146cd1bcSHajimu UMEMOTO if (dst2->aio_dstpolicy && 1016146cd1bcSHajimu UMEMOTO (dst1->aio_dstpolicy == NULL || 1017146cd1bcSHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.preced > 1018146cd1bcSHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.preced)) { 1019146cd1bcSHajimu UMEMOTO return(1); 1020146cd1bcSHajimu UMEMOTO } 1021146cd1bcSHajimu UMEMOTO #endif 1022146cd1bcSHajimu UMEMOTO 1023146cd1bcSHajimu UMEMOTO /* Rule 7: Prefer native transport. */ 1024146cd1bcSHajimu UMEMOTO /* XXX: not implemented yet */ 1025146cd1bcSHajimu UMEMOTO 1026146cd1bcSHajimu UMEMOTO /* Rule 8: Prefer smaller scope. */ 1027146cd1bcSHajimu UMEMOTO if (dst1->aio_dstscope >= 0 && 1028146cd1bcSHajimu UMEMOTO dst1->aio_dstscope < dst2->aio_dstscope) { 1029146cd1bcSHajimu UMEMOTO return(-1); 1030146cd1bcSHajimu UMEMOTO } 1031146cd1bcSHajimu UMEMOTO if (dst2->aio_dstscope >= 0 && 1032146cd1bcSHajimu UMEMOTO dst2->aio_dstscope < dst1->aio_dstscope) { 1033146cd1bcSHajimu UMEMOTO return(1); 1034146cd1bcSHajimu UMEMOTO } 1035146cd1bcSHajimu UMEMOTO 1036146cd1bcSHajimu UMEMOTO /* 1037146cd1bcSHajimu UMEMOTO * Rule 9: Use longest matching prefix. 1038146cd1bcSHajimu UMEMOTO * We compare the match length in a same AF only. 1039146cd1bcSHajimu UMEMOTO */ 1040146cd1bcSHajimu UMEMOTO if (dst1->aio_sa.sa_family == dst2->aio_sa.sa_family) { 1041146cd1bcSHajimu UMEMOTO if (dst1->aio_matchlen > dst2->aio_matchlen) { 1042146cd1bcSHajimu UMEMOTO return(-1); 1043146cd1bcSHajimu UMEMOTO } 1044146cd1bcSHajimu UMEMOTO if (dst1->aio_matchlen < dst2->aio_matchlen) { 1045146cd1bcSHajimu UMEMOTO return(1); 1046146cd1bcSHajimu UMEMOTO } 1047146cd1bcSHajimu UMEMOTO } 1048146cd1bcSHajimu UMEMOTO 1049146cd1bcSHajimu UMEMOTO /* Rule 10: Otherwise, leave the order unchanged. */ 1050af98aa6eSAndrey A. Chernov 1051af98aa6eSAndrey A. Chernov /* 1052af98aa6eSAndrey A. Chernov * Note that qsort is unstable; so, we can't return zero and 1053af98aa6eSAndrey A. Chernov * expect the order to be unchanged. 1054af98aa6eSAndrey A. Chernov * That also means we can't depend on the current position of 1055af98aa6eSAndrey A. Chernov * dst2 being after dst1. We must enforce the initial order 1056af98aa6eSAndrey A. Chernov * with an explicit compare on the original position. 1057af98aa6eSAndrey A. Chernov * The qsort specification requires that "When the same objects 1058af98aa6eSAndrey A. Chernov * (consisting of width bytes, irrespective of their current 1059af98aa6eSAndrey A. Chernov * positions in the array) are passed more than once to the 1060af98aa6eSAndrey A. Chernov * comparison function, the results shall be consistent with one 1061af98aa6eSAndrey A. Chernov * another." 1062af98aa6eSAndrey A. Chernov * In other words, If A < B, then we must also return B > A. 1063af98aa6eSAndrey A. Chernov */ 1064af98aa6eSAndrey A. Chernov if (dst2->aio_initial_sequence < dst1->aio_initial_sequence) 1065af98aa6eSAndrey A. Chernov return(1); 1066af98aa6eSAndrey A. Chernov 1067146cd1bcSHajimu UMEMOTO return(-1); 1068146cd1bcSHajimu UMEMOTO } 1069146cd1bcSHajimu UMEMOTO 1070146cd1bcSHajimu UMEMOTO /* 1071146cd1bcSHajimu UMEMOTO * Copy from scope.c. 1072146cd1bcSHajimu UMEMOTO * XXX: we should standardize the functions and link them as standard 1073146cd1bcSHajimu UMEMOTO * library. 1074146cd1bcSHajimu UMEMOTO */ 1075146cd1bcSHajimu UMEMOTO static int 107690d1d883SHajimu UMEMOTO gai_addr2scopetype(struct sockaddr *sa) 1077146cd1bcSHajimu UMEMOTO { 1078146cd1bcSHajimu UMEMOTO #ifdef INET6 1079146cd1bcSHajimu UMEMOTO struct sockaddr_in6 *sa6; 1080146cd1bcSHajimu UMEMOTO #endif 1081146cd1bcSHajimu UMEMOTO struct sockaddr_in *sa4; 1082146cd1bcSHajimu UMEMOTO 1083146cd1bcSHajimu UMEMOTO switch(sa->sa_family) { 1084146cd1bcSHajimu UMEMOTO #ifdef INET6 1085146cd1bcSHajimu UMEMOTO case AF_INET6: 1086146cd1bcSHajimu UMEMOTO sa6 = (struct sockaddr_in6 *)sa; 1087146cd1bcSHajimu UMEMOTO if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 1088146cd1bcSHajimu UMEMOTO /* just use the scope field of the multicast address */ 1089146cd1bcSHajimu UMEMOTO return(sa6->sin6_addr.s6_addr[2] & 0x0f); 1090146cd1bcSHajimu UMEMOTO } 1091146cd1bcSHajimu UMEMOTO /* 1092146cd1bcSHajimu UMEMOTO * Unicast addresses: map scope type to corresponding scope 1093146cd1bcSHajimu UMEMOTO * value defined for multcast addresses. 1094146cd1bcSHajimu UMEMOTO * XXX: hardcoded scope type values are bad... 1095146cd1bcSHajimu UMEMOTO */ 1096146cd1bcSHajimu UMEMOTO if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) 1097146cd1bcSHajimu UMEMOTO return(1); /* node local scope */ 1098146cd1bcSHajimu UMEMOTO if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) 1099146cd1bcSHajimu UMEMOTO return(2); /* link-local scope */ 1100146cd1bcSHajimu UMEMOTO if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr)) 1101146cd1bcSHajimu UMEMOTO return(5); /* site-local scope */ 1102146cd1bcSHajimu UMEMOTO return(14); /* global scope */ 1103146cd1bcSHajimu UMEMOTO break; 1104146cd1bcSHajimu UMEMOTO #endif 1105146cd1bcSHajimu UMEMOTO case AF_INET: 1106146cd1bcSHajimu UMEMOTO /* 1107146cd1bcSHajimu UMEMOTO * IPv4 pseudo scoping according to RFC 3484. 1108146cd1bcSHajimu UMEMOTO */ 1109146cd1bcSHajimu UMEMOTO sa4 = (struct sockaddr_in *)sa; 1110146cd1bcSHajimu UMEMOTO /* IPv4 autoconfiguration addresses have link-local scope. */ 1111146cd1bcSHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 169 && 1112146cd1bcSHajimu UMEMOTO ((u_char *)&sa4->sin_addr)[1] == 254) 1113146cd1bcSHajimu UMEMOTO return(2); 1114146cd1bcSHajimu UMEMOTO /* Private addresses have site-local scope. */ 1115146cd1bcSHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 10 || 1116146cd1bcSHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[0] == 172 && 1117146cd1bcSHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) || 1118146cd1bcSHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[0] == 192 && 1119146cd1bcSHajimu UMEMOTO ((u_char *)&sa4->sin_addr)[1] == 168)) 1120146cd1bcSHajimu UMEMOTO return(14); /* XXX: It should be 5 unless NAT */ 1121146cd1bcSHajimu UMEMOTO /* Loopback addresses have link-local scope. */ 1122146cd1bcSHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 127) 1123146cd1bcSHajimu UMEMOTO return(2); 1124146cd1bcSHajimu UMEMOTO return(14); 1125146cd1bcSHajimu UMEMOTO break; 1126146cd1bcSHajimu UMEMOTO default: 1127146cd1bcSHajimu UMEMOTO errno = EAFNOSUPPORT; /* is this a good error? */ 1128146cd1bcSHajimu UMEMOTO return(-1); 1129146cd1bcSHajimu UMEMOTO } 1130146cd1bcSHajimu UMEMOTO } 1131d38f4eadSHajimu UMEMOTO #endif 1132