1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate * 26*0Sstevel@tonic-gate * This file defines and implements the re-entrant getipnodebyname(), 27*0Sstevel@tonic-gate * getipnodebyaddr(), and freehostent() routines for IPv6. These routines 28*0Sstevel@tonic-gate * follow use the netdir_getbyYY() (see netdir_inet.c). 29*0Sstevel@tonic-gate * 30*0Sstevel@tonic-gate * lib/libnsl/nss/getipnodeby.c 31*0Sstevel@tonic-gate */ 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #include "mt.h" 36*0Sstevel@tonic-gate #include <stdlib.h> 37*0Sstevel@tonic-gate #include <unistd.h> 38*0Sstevel@tonic-gate #include <stropts.h> 39*0Sstevel@tonic-gate #include <ctype.h> 40*0Sstevel@tonic-gate #include <string.h> 41*0Sstevel@tonic-gate #include <strings.h> 42*0Sstevel@tonic-gate #include <netdb.h> 43*0Sstevel@tonic-gate #include <stdio.h> 44*0Sstevel@tonic-gate #include <arpa/inet.h> 45*0Sstevel@tonic-gate #include <nss_dbdefs.h> 46*0Sstevel@tonic-gate #include <rpc/trace.h> 47*0Sstevel@tonic-gate #include <netinet/in.h> 48*0Sstevel@tonic-gate #include <sys/socket.h> 49*0Sstevel@tonic-gate #include <sys/sockio.h> 50*0Sstevel@tonic-gate #include <nss_netdir.h> 51*0Sstevel@tonic-gate #include <net/if.h> 52*0Sstevel@tonic-gate #include <netinet/in.h> 53*0Sstevel@tonic-gate #include <netdir.h> 54*0Sstevel@tonic-gate #include <thread.h> 55*0Sstevel@tonic-gate #include <synch.h> 56*0Sstevel@tonic-gate #include <fcntl.h> 57*0Sstevel@tonic-gate #include <sys/time.h> 58*0Sstevel@tonic-gate #include "nss.h" 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate #define IPV6_LITERAL_CHAR ':' 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate /* 63*0Sstevel@tonic-gate * The number of nanoseconds getipnodebyname() waits before getting 64*0Sstevel@tonic-gate * fresh interface count information with SIOCGLIFNUM. The default is 65*0Sstevel@tonic-gate * five minutes. 66*0Sstevel@tonic-gate */ 67*0Sstevel@tonic-gate #define IFNUM_TIMEOUT ((hrtime_t)300 * NANOSEC) 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate /* 70*0Sstevel@tonic-gate * Bits in the bitfield returned by getipnodebyname_processflags(). 71*0Sstevel@tonic-gate * 72*0Sstevel@tonic-gate * IPNODE_WANTIPV6 The user wants IPv6 addresses returned. 73*0Sstevel@tonic-gate * IPNODE_WANTIPV4 The user wants IPv4 addresses returned. 74*0Sstevel@tonic-gate * IPNODE_IPV4IFNOIPV6 The user only wants IPv4 addresses returned if no IPv6 75*0Sstevel@tonic-gate * addresses are returned. 76*0Sstevel@tonic-gate * IPNODE_LOOKUPIPNODES getipnodebyname() needs to lookup the name in ipnodes. 77*0Sstevel@tonic-gate * IPNODE_LOOKUPHOSTS getipnodebyname() needs to lookup the name in hosts. 78*0Sstevel@tonic-gate * IPNODE_ISLITERAL The name supplied is a literal address string. 79*0Sstevel@tonic-gate */ 80*0Sstevel@tonic-gate #define IPNODE_WANTIPV6 0x00000001u 81*0Sstevel@tonic-gate #define IPNODE_WANTIPV4 0x00000002u 82*0Sstevel@tonic-gate #define IPNODE_IPV4IFNOIPV6 0x00000004u 83*0Sstevel@tonic-gate #define IPNODE_LOOKUPIPNODES 0x00000008u 84*0Sstevel@tonic-gate #define IPNODE_LOOKUPHOSTS 0x00000010u 85*0Sstevel@tonic-gate #define IPNODE_LITERAL 0x00000020u 86*0Sstevel@tonic-gate #define IPNODE_IPV4 (IPNODE_WANTIPV4 | IPNODE_IPV4IFNOIPV6) 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate /* 89*0Sstevel@tonic-gate * The default set of bits corresponding to a getipnodebyname() flags 90*0Sstevel@tonic-gate * argument of AI_DEFAULT. 91*0Sstevel@tonic-gate */ 92*0Sstevel@tonic-gate #define IPNODE_DEFAULT (IPNODE_WANTIPV6 | IPNODE_IPV4 | \ 93*0Sstevel@tonic-gate IPNODE_LOOKUPIPNODES | IPNODE_LOOKUPHOSTS) 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate extern struct netconfig *__rpc_getconfip(char *); 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate static struct hostent *__mapv4tov6(struct hostent *, struct hostent *, 98*0Sstevel@tonic-gate nss_XbyY_buf_t *, int); 99*0Sstevel@tonic-gate struct hostent *__mappedtov4(struct hostent *, int *); 100*0Sstevel@tonic-gate static struct hostent *__filter_addresses(int, struct hostent *); 101*0Sstevel@tonic-gate static int __find_mapped(struct hostent *, int); 102*0Sstevel@tonic-gate static nss_XbyY_buf_t *__IPv6_alloc(int); 103*0Sstevel@tonic-gate static void __IPv6_cleanup(nss_XbyY_buf_t *); 104*0Sstevel@tonic-gate static int __ai_addrconfig(int); 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate #ifdef PIC 108*0Sstevel@tonic-gate struct hostent * 109*0Sstevel@tonic-gate _uncached_getipnodebyname(const char *nam, struct hostent *result, 110*0Sstevel@tonic-gate char *buffer, int buflen, int af_family, int flags, int *h_errnop) 111*0Sstevel@tonic-gate { 112*0Sstevel@tonic-gate return 113*0Sstevel@tonic-gate (_switch_getipnodebyname_r(nam, result, buffer, buflen, af_family, 114*0Sstevel@tonic-gate flags, h_errnop)); 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate struct hostent * 118*0Sstevel@tonic-gate _uncached_getipnodebyaddr(const char *addr, int length, int type, 119*0Sstevel@tonic-gate struct hostent *result, char *buffer, int buflen, int *h_errnop) 120*0Sstevel@tonic-gate { 121*0Sstevel@tonic-gate if (type == AF_INET) 122*0Sstevel@tonic-gate return (_switch_gethostbyaddr_r(addr, length, type, 123*0Sstevel@tonic-gate result, buffer, buflen, h_errnop)); 124*0Sstevel@tonic-gate else if (type == AF_INET6) 125*0Sstevel@tonic-gate return (_switch_getipnodebyaddr_r(addr, length, type, 126*0Sstevel@tonic-gate result, buffer, buflen, h_errnop)); 127*0Sstevel@tonic-gate return (NULL); 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate #endif 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate /* 132*0Sstevel@tonic-gate * Given a name, an address family, and a set of flags, return a 133*0Sstevel@tonic-gate * bitfield that getipnodebyname() will use. 134*0Sstevel@tonic-gate */ 135*0Sstevel@tonic-gate static uint_t 136*0Sstevel@tonic-gate getipnodebyname_processflags(const char *name, int af, int flags) 137*0Sstevel@tonic-gate { 138*0Sstevel@tonic-gate int ifnum6, ifnum4; 139*0Sstevel@tonic-gate uint_t ipnode_bits = IPNODE_DEFAULT; 140*0Sstevel@tonic-gate boolean_t ipv6configured = B_FALSE; 141*0Sstevel@tonic-gate boolean_t ipv4configured = B_FALSE; 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate /* 144*0Sstevel@tonic-gate * If AI_ADDRCONFIG is specified, we need to determine the number 145*0Sstevel@tonic-gate * of addresses of each address family configured on the system as 146*0Sstevel@tonic-gate * appropriate. 147*0Sstevel@tonic-gate */ 148*0Sstevel@tonic-gate if (flags & AI_ADDRCONFIG) { 149*0Sstevel@tonic-gate ipv6configured = (af == AF_INET6 && 150*0Sstevel@tonic-gate __ai_addrconfig(AF_INET6) > 0); 151*0Sstevel@tonic-gate ipv4configured = ((af == AF_INET || (flags & AI_V4MAPPED)) && 152*0Sstevel@tonic-gate __ai_addrconfig(AF_INET) > 0); 153*0Sstevel@tonic-gate } 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate /* 156*0Sstevel@tonic-gate * Determine what kinds of addresses the user is interested 157*0Sstevel@tonic-gate * in getting back. 158*0Sstevel@tonic-gate */ 159*0Sstevel@tonic-gate switch (af) { 160*0Sstevel@tonic-gate case AF_INET6: 161*0Sstevel@tonic-gate if ((flags & AI_ADDRCONFIG) && !ipv6configured) 162*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_WANTIPV6; 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate if (flags & AI_V4MAPPED) { 165*0Sstevel@tonic-gate if ((flags & AI_ADDRCONFIG) && !ipv4configured) { 166*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4; 167*0Sstevel@tonic-gate } else if (flags & AI_ALL) { 168*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4IFNOIPV6; 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate } else { 171*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4; 172*0Sstevel@tonic-gate } 173*0Sstevel@tonic-gate break; 174*0Sstevel@tonic-gate case AF_INET: 175*0Sstevel@tonic-gate if ((flags & AI_ADDRCONFIG) && !ipv4configured) 176*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4; 177*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_WANTIPV6; 178*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4IFNOIPV6; 179*0Sstevel@tonic-gate break; 180*0Sstevel@tonic-gate default: 181*0Sstevel@tonic-gate ipnode_bits = 0; 182*0Sstevel@tonic-gate break; 183*0Sstevel@tonic-gate } 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate /* 186*0Sstevel@tonic-gate * If we're not looking for IPv4 addresses, don't bother looking 187*0Sstevel@tonic-gate * in hosts. 188*0Sstevel@tonic-gate */ 189*0Sstevel@tonic-gate if (!(ipnode_bits & IPNODE_WANTIPV4)) 190*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPHOSTS; 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate /* 193*0Sstevel@tonic-gate * Determine if name is a literal IP address. This will 194*0Sstevel@tonic-gate * further narrow down what type of lookup we're going to do. 195*0Sstevel@tonic-gate */ 196*0Sstevel@tonic-gate if (strchr(name, IPV6_LITERAL_CHAR) != NULL) { 197*0Sstevel@tonic-gate /* Literal IPv6 address */ 198*0Sstevel@tonic-gate ipnode_bits |= IPNODE_LITERAL; 199*0Sstevel@tonic-gate /* 200*0Sstevel@tonic-gate * In s9 we accepted the literal without filtering independent 201*0Sstevel@tonic-gate * of what family was passed in hints. We continue to do 202*0Sstevel@tonic-gate * this. 203*0Sstevel@tonic-gate */ 204*0Sstevel@tonic-gate ipnode_bits |= (IPNODE_WANTIPV6 | IPNODE_WANTIPV4); 205*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPHOSTS; 206*0Sstevel@tonic-gate } else if (inet_addr(name) != -1) { 207*0Sstevel@tonic-gate /* Literal IPv4 address */ 208*0Sstevel@tonic-gate ipnode_bits |= (IPNODE_LITERAL | IPNODE_WANTIPV4); 209*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_WANTIPV6; 210*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPIPNODES; 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate return (ipnode_bits); 213*0Sstevel@tonic-gate } 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate struct hostent * 216*0Sstevel@tonic-gate getipnodebyname(const char *name, int af, int flags, int *error_num) 217*0Sstevel@tonic-gate { 218*0Sstevel@tonic-gate struct hostent *hp = NULL; 219*0Sstevel@tonic-gate nss_XbyY_buf_t *buf4 = NULL; 220*0Sstevel@tonic-gate nss_XbyY_buf_t *buf6 = NULL; 221*0Sstevel@tonic-gate struct netconfig *nconf; 222*0Sstevel@tonic-gate struct nss_netdirbyname_in nssin; 223*0Sstevel@tonic-gate union nss_netdirbyname_out nssout; 224*0Sstevel@tonic-gate int ret; 225*0Sstevel@tonic-gate uint_t ipnode_bits; 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate trace1(TR_getipnodebyname, 0); 228*0Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 229*0Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 230*0Sstevel@tonic-gate trace2(TR_getipnodebyname, 1, buflen); 231*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 232*0Sstevel@tonic-gate return (NULL); 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate ipnode_bits = getipnodebyname_processflags(name, af, flags); 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate /* Make sure we have something to look up. */ 238*0Sstevel@tonic-gate if (!(ipnode_bits & (IPNODE_WANTIPV6 | IPNODE_WANTIPV4))) { 239*0Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 240*0Sstevel@tonic-gate goto cleanup; 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate /* 244*0Sstevel@tonic-gate * Perform the requested lookups. We always look through 245*0Sstevel@tonic-gate * ipnodes first for both IPv4 and IPv6 addresses. Depending 246*0Sstevel@tonic-gate * on what was returned and what was needed, we either filter 247*0Sstevel@tonic-gate * out the garbage, or ask for more using hosts. 248*0Sstevel@tonic-gate */ 249*0Sstevel@tonic-gate if (ipnode_bits & IPNODE_LOOKUPIPNODES) { 250*0Sstevel@tonic-gate if ((buf6 = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == NULL) { 251*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 252*0Sstevel@tonic-gate goto cleanup; 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 255*0Sstevel@tonic-gate nssin.arg.nss.host6.name = name; 256*0Sstevel@tonic-gate nssin.arg.nss.host6.buf = buf6->buffer; 257*0Sstevel@tonic-gate nssin.arg.nss.host6.buflen = buf6->buflen; 258*0Sstevel@tonic-gate nssin.arg.nss.host6.af_family = af; 259*0Sstevel@tonic-gate nssin.arg.nss.host6.flags = flags; 260*0Sstevel@tonic-gate nssout.nss.host.hent = buf6->result; 261*0Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 262*0Sstevel@tonic-gate ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout); 263*0Sstevel@tonic-gate if (ret != ND_OK) { 264*0Sstevel@tonic-gate __IPv6_cleanup(buf6); 265*0Sstevel@tonic-gate buf6 = NULL; 266*0Sstevel@tonic-gate } else if (ipnode_bits & IPNODE_WANTIPV4) { 267*0Sstevel@tonic-gate /* 268*0Sstevel@tonic-gate * buf6 may have all that we need if we either 269*0Sstevel@tonic-gate * only wanted IPv4 addresses if there were no 270*0Sstevel@tonic-gate * IPv6 addresses returned, or if there are 271*0Sstevel@tonic-gate * IPv4-mapped addresses in buf6. If either 272*0Sstevel@tonic-gate * of these are true, then there's no need to 273*0Sstevel@tonic-gate * look in hosts. 274*0Sstevel@tonic-gate */ 275*0Sstevel@tonic-gate if (ipnode_bits & IPNODE_IPV4IFNOIPV6 || 276*0Sstevel@tonic-gate __find_mapped(buf6->result, 0) != 0) { 277*0Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPHOSTS; 278*0Sstevel@tonic-gate } else if (!(ipnode_bits & IPNODE_WANTIPV6)) { 279*0Sstevel@tonic-gate /* 280*0Sstevel@tonic-gate * If all we're looking for are IPv4 281*0Sstevel@tonic-gate * addresses and there are none in 282*0Sstevel@tonic-gate * buf6 then buf6 is now useless. 283*0Sstevel@tonic-gate */ 284*0Sstevel@tonic-gate __IPv6_cleanup(buf6); 285*0Sstevel@tonic-gate buf6 = NULL; 286*0Sstevel@tonic-gate } 287*0Sstevel@tonic-gate } 288*0Sstevel@tonic-gate } 289*0Sstevel@tonic-gate if (ipnode_bits & IPNODE_LOOKUPHOSTS) { 290*0Sstevel@tonic-gate if ((buf4 = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == NULL) { 291*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 292*0Sstevel@tonic-gate goto cleanup; 293*0Sstevel@tonic-gate } 294*0Sstevel@tonic-gate nssin.op_t = NSS_HOST; 295*0Sstevel@tonic-gate nssin.arg.nss.host.name = name; 296*0Sstevel@tonic-gate nssin.arg.nss.host.buf = buf4->buffer; 297*0Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf4->buflen; 298*0Sstevel@tonic-gate nssout.nss.host.hent = buf4->result; 299*0Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 300*0Sstevel@tonic-gate ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout); 301*0Sstevel@tonic-gate if (ret != ND_OK) { 302*0Sstevel@tonic-gate __IPv6_cleanup(buf4); 303*0Sstevel@tonic-gate buf4 = NULL; 304*0Sstevel@tonic-gate } 305*0Sstevel@tonic-gate } 306*0Sstevel@tonic-gate 307*0Sstevel@tonic-gate if (buf6 == NULL && buf4 == NULL) { 308*0Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 309*0Sstevel@tonic-gate goto cleanup; 310*0Sstevel@tonic-gate } 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate /* Extract the appropriate addresses from the returned buffer(s). */ 313*0Sstevel@tonic-gate switch (af) { 314*0Sstevel@tonic-gate case AF_INET6: { 315*0Sstevel@tonic-gate if (buf4 != NULL) { 316*0Sstevel@tonic-gate nss_XbyY_buf_t *mergebuf; 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate /* 319*0Sstevel@tonic-gate * The IPv4 results we have need to be 320*0Sstevel@tonic-gate * converted to IPv4-mapped addresses, 321*0Sstevel@tonic-gate * conditionally merged with the IPv6 322*0Sstevel@tonic-gate * results, and the end result needs to be 323*0Sstevel@tonic-gate * re-ordered. 324*0Sstevel@tonic-gate */ 325*0Sstevel@tonic-gate mergebuf = __IPv6_alloc(NSS_BUFLEN_IPNODES); 326*0Sstevel@tonic-gate if (mergebuf == NULL) { 327*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 328*0Sstevel@tonic-gate goto cleanup; 329*0Sstevel@tonic-gate } 330*0Sstevel@tonic-gate hp = __mapv4tov6(buf4->result, 331*0Sstevel@tonic-gate ((buf6 != NULL) ? buf6->result : NULL), 332*0Sstevel@tonic-gate mergebuf, 1); 333*0Sstevel@tonic-gate if (hp != NULL) 334*0Sstevel@tonic-gate order_haddrlist_af(AF_INET6, hp->h_addr_list); 335*0Sstevel@tonic-gate else 336*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 337*0Sstevel@tonic-gate free(mergebuf); 338*0Sstevel@tonic-gate } 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gate if (buf4 == NULL && buf6 != NULL) { 341*0Sstevel@tonic-gate hp = buf6->result; 342*0Sstevel@tonic-gate 343*0Sstevel@tonic-gate /* 344*0Sstevel@tonic-gate * We have what we need in buf6, but we may need 345*0Sstevel@tonic-gate * to filter out some addresses depending on what 346*0Sstevel@tonic-gate * is being asked for. 347*0Sstevel@tonic-gate */ 348*0Sstevel@tonic-gate if (!(ipnode_bits & IPNODE_WANTIPV4)) 349*0Sstevel@tonic-gate hp = __filter_addresses(AF_INET, buf6->result); 350*0Sstevel@tonic-gate else if (!(ipnode_bits & IPNODE_WANTIPV6)) 351*0Sstevel@tonic-gate hp = __filter_addresses(AF_INET6, buf6->result); 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate if (hp == NULL) 354*0Sstevel@tonic-gate *error_num = NO_ADDRESS; 355*0Sstevel@tonic-gate } 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gate break; 358*0Sstevel@tonic-gate } 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate case AF_INET: 361*0Sstevel@tonic-gate /* We could have results in buf6 or buf4, not both */ 362*0Sstevel@tonic-gate if (buf6 != NULL) { 363*0Sstevel@tonic-gate /* 364*0Sstevel@tonic-gate * Extract the IPv4-mapped addresses from buf6 365*0Sstevel@tonic-gate * into hp. 366*0Sstevel@tonic-gate */ 367*0Sstevel@tonic-gate hp = __mappedtov4(buf6->result, error_num); 368*0Sstevel@tonic-gate } else { 369*0Sstevel@tonic-gate /* We have what we need in buf4. */ 370*0Sstevel@tonic-gate hp = buf4->result; 371*0Sstevel@tonic-gate if (ipnode_bits & IPNODE_LITERAL) { 372*0Sstevel@tonic-gate /* 373*0Sstevel@tonic-gate * There is a special case here for literal 374*0Sstevel@tonic-gate * IPv4 address strings. The hosts 375*0Sstevel@tonic-gate * front-end sets h_aliases to a one 376*0Sstevel@tonic-gate * element array containing a single NULL 377*0Sstevel@tonic-gate * pointer (in ndaddr2hent()), while 378*0Sstevel@tonic-gate * getipnodebyname() requires h_aliases to 379*0Sstevel@tonic-gate * be a NULL pointer itself. We're not 380*0Sstevel@tonic-gate * going to change the front-end since it 381*0Sstevel@tonic-gate * needs to remain backward compatible for 382*0Sstevel@tonic-gate * gethostbyname() and friends. Just set 383*0Sstevel@tonic-gate * h_aliases to NULL here instead. 384*0Sstevel@tonic-gate */ 385*0Sstevel@tonic-gate hp->h_aliases = NULL; 386*0Sstevel@tonic-gate } 387*0Sstevel@tonic-gate } 388*0Sstevel@tonic-gate 389*0Sstevel@tonic-gate break; 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate default: 392*0Sstevel@tonic-gate break; 393*0Sstevel@tonic-gate } 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate cleanup: 396*0Sstevel@tonic-gate /* 397*0Sstevel@tonic-gate * Free the memory we allocated, but make sure we don't free 398*0Sstevel@tonic-gate * the memory we're returning to the caller. 399*0Sstevel@tonic-gate */ 400*0Sstevel@tonic-gate if (buf6 != NULL) { 401*0Sstevel@tonic-gate if (buf6->result == hp) 402*0Sstevel@tonic-gate buf6->result = NULL; 403*0Sstevel@tonic-gate __IPv6_cleanup(buf6); 404*0Sstevel@tonic-gate } 405*0Sstevel@tonic-gate if (buf4 != NULL) { 406*0Sstevel@tonic-gate if (buf4->result == hp) 407*0Sstevel@tonic-gate buf4->result = NULL; 408*0Sstevel@tonic-gate __IPv6_cleanup(buf4); 409*0Sstevel@tonic-gate } 410*0Sstevel@tonic-gate (void) freenetconfigent(nconf); 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate trace1(TR_getipnodebyname, 1); 413*0Sstevel@tonic-gate return (hp); 414*0Sstevel@tonic-gate } 415*0Sstevel@tonic-gate 416*0Sstevel@tonic-gate /* 417*0Sstevel@tonic-gate * This is the IPv6 interface for "gethostbyaddr". 418*0Sstevel@tonic-gate */ 419*0Sstevel@tonic-gate struct hostent * 420*0Sstevel@tonic-gate getipnodebyaddr(const void *src, size_t len, int type, int *error_num) 421*0Sstevel@tonic-gate { 422*0Sstevel@tonic-gate struct in6_addr *addr6 = 0; 423*0Sstevel@tonic-gate struct in_addr *addr4 = 0; 424*0Sstevel@tonic-gate nss_XbyY_buf_t *buf = 0; 425*0Sstevel@tonic-gate nss_XbyY_buf_t *res = 0; 426*0Sstevel@tonic-gate struct netconfig *nconf; 427*0Sstevel@tonic-gate struct hostent *hp = 0; 428*0Sstevel@tonic-gate struct nss_netdirbyaddr_in nssin; 429*0Sstevel@tonic-gate union nss_netdirbyaddr_out nssout; 430*0Sstevel@tonic-gate int neterr; 431*0Sstevel@tonic-gate char tmpbuf[64]; 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gate trace2(TR_gethostbyaddr, 0, len); 434*0Sstevel@tonic-gate if (type == AF_INET6) { 435*0Sstevel@tonic-gate if ((addr6 = (struct in6_addr *)src) == NULL) { 436*0Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 437*0Sstevel@tonic-gate return (NULL); 438*0Sstevel@tonic-gate } 439*0Sstevel@tonic-gate } else if (type == AF_INET) { 440*0Sstevel@tonic-gate if ((addr4 = (struct in_addr *)src) == NULL) { 441*0Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 442*0Sstevel@tonic-gate return (NULL); 443*0Sstevel@tonic-gate } 444*0Sstevel@tonic-gate } else { 445*0Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 446*0Sstevel@tonic-gate return (NULL); 447*0Sstevel@tonic-gate } 448*0Sstevel@tonic-gate /* 449*0Sstevel@tonic-gate * Specific case: query for "::" 450*0Sstevel@tonic-gate */ 451*0Sstevel@tonic-gate if (type == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(addr6)) { 452*0Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 453*0Sstevel@tonic-gate return (NULL); 454*0Sstevel@tonic-gate } 455*0Sstevel@tonic-gate /* 456*0Sstevel@tonic-gate * Step 1: IPv4-mapped address or IPv4 Compat 457*0Sstevel@tonic-gate */ 458*0Sstevel@tonic-gate if ((type == AF_INET6 && len == 16) && 459*0Sstevel@tonic-gate ((IN6_IS_ADDR_V4MAPPED(addr6)) || 460*0Sstevel@tonic-gate (IN6_IS_ADDR_V4COMPAT(addr6)))) { 461*0Sstevel@tonic-gate if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 462*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 463*0Sstevel@tonic-gate return (NULL); 464*0Sstevel@tonic-gate } 465*0Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 466*0Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 467*0Sstevel@tonic-gate trace3(TR__getipnodebyaddr, 0, len, buf->buflen); 468*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 469*0Sstevel@tonic-gate __IPv6_cleanup(buf); 470*0Sstevel@tonic-gate return (NULL); 471*0Sstevel@tonic-gate } 472*0Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 473*0Sstevel@tonic-gate if (IN6_IS_ADDR_V4COMPAT(addr6)) { 474*0Sstevel@tonic-gate memcpy((void *)tmpbuf, (void *)addr6, 475*0Sstevel@tonic-gate sizeof (*addr6)); 476*0Sstevel@tonic-gate tmpbuf[10] = 0xffU; 477*0Sstevel@tonic-gate tmpbuf[11] = 0xffU; 478*0Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)tmpbuf; 479*0Sstevel@tonic-gate } else { 480*0Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)addr6; 481*0Sstevel@tonic-gate } 482*0Sstevel@tonic-gate nssin.arg.nss.host.len = sizeof (struct in6_addr); 483*0Sstevel@tonic-gate nssin.arg.nss.host.type = AF_INET6; 484*0Sstevel@tonic-gate nssin.arg.nss.host.buf = buf->buffer; 485*0Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf->buflen; 486*0Sstevel@tonic-gate 487*0Sstevel@tonic-gate nssout.nss.host.hent = buf->result; 488*0Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 489*0Sstevel@tonic-gate /* 490*0Sstevel@tonic-gate * We pass in nconf and let the implementation of the 491*0Sstevel@tonic-gate * long-named func decide whether to use the switch based on 492*0Sstevel@tonic-gate * nc_nlookups. 493*0Sstevel@tonic-gate */ 494*0Sstevel@tonic-gate neterr = 495*0Sstevel@tonic-gate _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout); 496*0Sstevel@tonic-gate 497*0Sstevel@tonic-gate (void) freenetconfigent(nconf); 498*0Sstevel@tonic-gate if (neterr != ND_OK) { 499*0Sstevel@tonic-gate /* Failover case, try hosts db for v4 address */ 500*0Sstevel@tonic-gate trace3(TR__getipnodebyaddr, 0, len, buf->buflen); 501*0Sstevel@tonic-gate if (!gethostbyaddr_r(((char *)addr6) + 12, 502*0Sstevel@tonic-gate sizeof (in_addr_t), AF_INET, buf->result, 503*0Sstevel@tonic-gate buf->buffer, buf->buflen, error_num)) { 504*0Sstevel@tonic-gate __IPv6_cleanup(buf); 505*0Sstevel@tonic-gate return (NULL); 506*0Sstevel@tonic-gate } 507*0Sstevel@tonic-gate /* Found one, now format it into mapped/compat addr */ 508*0Sstevel@tonic-gate if ((res = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 509*0Sstevel@tonic-gate __IPv6_cleanup(buf); 510*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 511*0Sstevel@tonic-gate return (NULL); 512*0Sstevel@tonic-gate } 513*0Sstevel@tonic-gate /* Convert IPv4 to mapped/compat address w/name */ 514*0Sstevel@tonic-gate hp = res->result; 515*0Sstevel@tonic-gate __mapv4tov6(buf->result, 0, res, 516*0Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED(addr6)); 517*0Sstevel@tonic-gate __IPv6_cleanup(buf); 518*0Sstevel@tonic-gate free(res); 519*0Sstevel@tonic-gate return (hp); 520*0Sstevel@tonic-gate } 521*0Sstevel@tonic-gate /* 522*0Sstevel@tonic-gate * At this point, we'll have a v4mapped hostent. If that's 523*0Sstevel@tonic-gate * what was passed in, just return. If the request was a compat, 524*0Sstevel@tonic-gate * twiggle the two bytes to make the mapped address a compat. 525*0Sstevel@tonic-gate */ 526*0Sstevel@tonic-gate hp = buf->result; 527*0Sstevel@tonic-gate if (IN6_IS_ADDR_V4COMPAT(addr6)) { 528*0Sstevel@tonic-gate addr6 = (struct in6_addr *)hp->h_addr_list[0]; 529*0Sstevel@tonic-gate addr6->s6_addr[10] = 0; 530*0Sstevel@tonic-gate addr6->s6_addr[11] = 0; 531*0Sstevel@tonic-gate } 532*0Sstevel@tonic-gate free(buf); 533*0Sstevel@tonic-gate return (hp); 534*0Sstevel@tonic-gate } 535*0Sstevel@tonic-gate /* 536*0Sstevel@tonic-gate * Step 2: AF_INET, v4 lookup. Since we're going to search the 537*0Sstevel@tonic-gate * ipnodes (v6) path first, we need to treat this as a v4mapped 538*0Sstevel@tonic-gate * address. nscd(1m) caches v4 from ipnodes as mapped v6's. The 539*0Sstevel@tonic-gate * switch backend knows to lookup v4's (not v4mapped) from the 540*0Sstevel@tonic-gate * name services. 541*0Sstevel@tonic-gate */ 542*0Sstevel@tonic-gate if (type == AF_INET) { 543*0Sstevel@tonic-gate struct in6_addr v4mapbuf; 544*0Sstevel@tonic-gate addr6 = &v4mapbuf; 545*0Sstevel@tonic-gate 546*0Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(addr4, addr6); 547*0Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 548*0Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 549*0Sstevel@tonic-gate trace3(TR__getipnodebyaddr, 0, len, buf->buflen); 550*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 551*0Sstevel@tonic-gate return (NULL); 552*0Sstevel@tonic-gate } 553*0Sstevel@tonic-gate if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 554*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 555*0Sstevel@tonic-gate freenetconfigent(nconf); 556*0Sstevel@tonic-gate return (NULL); 557*0Sstevel@tonic-gate } 558*0Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 559*0Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)addr6; 560*0Sstevel@tonic-gate nssin.arg.nss.host.len = sizeof (struct in6_addr); 561*0Sstevel@tonic-gate nssin.arg.nss.host.type = AF_INET6; 562*0Sstevel@tonic-gate nssin.arg.nss.host.buf = buf->buffer; 563*0Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf->buflen; 564*0Sstevel@tonic-gate 565*0Sstevel@tonic-gate nssout.nss.host.hent = buf->result; 566*0Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 567*0Sstevel@tonic-gate /* 568*0Sstevel@tonic-gate * We pass in nconf and let the implementation of the 569*0Sstevel@tonic-gate * long-named func decide whether to use the switch based on 570*0Sstevel@tonic-gate * nc_nlookups. 571*0Sstevel@tonic-gate */ 572*0Sstevel@tonic-gate neterr = 573*0Sstevel@tonic-gate _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout); 574*0Sstevel@tonic-gate 575*0Sstevel@tonic-gate (void) freenetconfigent(nconf); 576*0Sstevel@tonic-gate if (neterr != ND_OK) { 577*0Sstevel@tonic-gate /* Failover case, try hosts db for v4 address */ 578*0Sstevel@tonic-gate trace3(TR__getipnodebyaddr, 0, len, buf->buflen); 579*0Sstevel@tonic-gate hp = buf->result; 580*0Sstevel@tonic-gate if (!gethostbyaddr_r(src, len, type, buf->result, 581*0Sstevel@tonic-gate buf->buffer, buf->buflen, error_num)) { 582*0Sstevel@tonic-gate __IPv6_cleanup(buf); 583*0Sstevel@tonic-gate return (NULL); 584*0Sstevel@tonic-gate } 585*0Sstevel@tonic-gate free(buf); 586*0Sstevel@tonic-gate return (hp); 587*0Sstevel@tonic-gate } 588*0Sstevel@tonic-gate if ((hp = __mappedtov4(buf->result, error_num)) == NULL) { 589*0Sstevel@tonic-gate __IPv6_cleanup(buf); 590*0Sstevel@tonic-gate return (NULL); 591*0Sstevel@tonic-gate } 592*0Sstevel@tonic-gate __IPv6_cleanup(buf); 593*0Sstevel@tonic-gate return (hp); 594*0Sstevel@tonic-gate } 595*0Sstevel@tonic-gate /* 596*0Sstevel@tonic-gate * Step 3: AF_INET6, plain vanilla v6 getipnodebyaddr() call. 597*0Sstevel@tonic-gate */ 598*0Sstevel@tonic-gate if (type == AF_INET6) { 599*0Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 600*0Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 601*0Sstevel@tonic-gate trace3(TR__getipnodebyaddr, 0, len, buf->buflen); 602*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 603*0Sstevel@tonic-gate return (NULL); 604*0Sstevel@tonic-gate } 605*0Sstevel@tonic-gate if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 606*0Sstevel@tonic-gate *error_num = NO_RECOVERY; 607*0Sstevel@tonic-gate freenetconfigent(nconf); 608*0Sstevel@tonic-gate return (NULL); 609*0Sstevel@tonic-gate } 610*0Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 611*0Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)addr6; 612*0Sstevel@tonic-gate nssin.arg.nss.host.len = len; 613*0Sstevel@tonic-gate nssin.arg.nss.host.type = type; 614*0Sstevel@tonic-gate nssin.arg.nss.host.buf = buf->buffer; 615*0Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf->buflen; 616*0Sstevel@tonic-gate 617*0Sstevel@tonic-gate nssout.nss.host.hent = buf->result; 618*0Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 619*0Sstevel@tonic-gate /* 620*0Sstevel@tonic-gate * We pass in nconf and let the implementation of the 621*0Sstevel@tonic-gate * long-named func decide whether to use the switch based on 622*0Sstevel@tonic-gate * nc_nlookups. 623*0Sstevel@tonic-gate */ 624*0Sstevel@tonic-gate neterr = 625*0Sstevel@tonic-gate _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout); 626*0Sstevel@tonic-gate 627*0Sstevel@tonic-gate (void) freenetconfigent(nconf); 628*0Sstevel@tonic-gate if (neterr != ND_OK) { 629*0Sstevel@tonic-gate trace3(TR__getipnodebyaddr, 0, len, buf->buflen); 630*0Sstevel@tonic-gate __IPv6_cleanup(buf); 631*0Sstevel@tonic-gate return (NULL); 632*0Sstevel@tonic-gate } 633*0Sstevel@tonic-gate trace2(TR_gethostbyaddr, 1, len); 634*0Sstevel@tonic-gate free(buf); 635*0Sstevel@tonic-gate return (nssout.nss.host.hent); 636*0Sstevel@tonic-gate } 637*0Sstevel@tonic-gate /* 638*0Sstevel@tonic-gate * If we got here, unknown type. 639*0Sstevel@tonic-gate */ 640*0Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 641*0Sstevel@tonic-gate return (NULL); 642*0Sstevel@tonic-gate } 643*0Sstevel@tonic-gate 644*0Sstevel@tonic-gate void 645*0Sstevel@tonic-gate freehostent(struct hostent *hent) 646*0Sstevel@tonic-gate { 647*0Sstevel@tonic-gate free(hent); 648*0Sstevel@tonic-gate } 649*0Sstevel@tonic-gate 650*0Sstevel@tonic-gate static int 651*0Sstevel@tonic-gate __ai_addrconfig(int af) 652*0Sstevel@tonic-gate { 653*0Sstevel@tonic-gate struct lifnum lifn; 654*0Sstevel@tonic-gate hrtime_t now, *then; 655*0Sstevel@tonic-gate static hrtime_t then4, then6; /* the last time we updated ifnum# */ 656*0Sstevel@tonic-gate static int ifnum4 = -1, ifnum6 = -1; 657*0Sstevel@tonic-gate int *num; 658*0Sstevel@tonic-gate 659*0Sstevel@tonic-gate switch (af) { 660*0Sstevel@tonic-gate case AF_INET: 661*0Sstevel@tonic-gate num = &ifnum4; 662*0Sstevel@tonic-gate then = &then4; 663*0Sstevel@tonic-gate break; 664*0Sstevel@tonic-gate case AF_INET6: 665*0Sstevel@tonic-gate num = &ifnum6; 666*0Sstevel@tonic-gate then = &then6; 667*0Sstevel@tonic-gate break; 668*0Sstevel@tonic-gate default: 669*0Sstevel@tonic-gate return (0); 670*0Sstevel@tonic-gate } 671*0Sstevel@tonic-gate 672*0Sstevel@tonic-gate /* 673*0Sstevel@tonic-gate * We don't need to check this every time someone does a name 674*0Sstevel@tonic-gate * lookup. Do it every IFNUM_TIMEOUT for each address family. 675*0Sstevel@tonic-gate * 676*0Sstevel@tonic-gate * There's no need to protect all of this with a lock. The 677*0Sstevel@tonic-gate * worst that can happen is that we update the interface count 678*0Sstevel@tonic-gate * twice instead of once. That's no big deal. 679*0Sstevel@tonic-gate */ 680*0Sstevel@tonic-gate now = gethrtime(); 681*0Sstevel@tonic-gate if (*num == -1 || ((now - *then) >= IFNUM_TIMEOUT)) { 682*0Sstevel@tonic-gate lifn.lifn_family = af; 683*0Sstevel@tonic-gate /* 684*0Sstevel@tonic-gate * We want to determine if this machine knows anything 685*0Sstevel@tonic-gate * at all about the address family; the status of the 686*0Sstevel@tonic-gate * interface is less important. Hence, set 687*0Sstevel@tonic-gate * 'lifn_flags' to zero. 688*0Sstevel@tonic-gate */ 689*0Sstevel@tonic-gate lifn.lifn_flags = 0; 690*0Sstevel@tonic-gate if (nss_ioctl(af, SIOCGLIFNUM, &lifn) < 0) 691*0Sstevel@tonic-gate return (-1); 692*0Sstevel@tonic-gate 693*0Sstevel@tonic-gate *num = lifn.lifn_count; 694*0Sstevel@tonic-gate *then = now; 695*0Sstevel@tonic-gate } 696*0Sstevel@tonic-gate 697*0Sstevel@tonic-gate return (*num); 698*0Sstevel@tonic-gate } 699*0Sstevel@tonic-gate 700*0Sstevel@tonic-gate /* 701*0Sstevel@tonic-gate * This routine will either convert an IPv4 address to a mapped or compat 702*0Sstevel@tonic-gate * IPv6 (if he6 == NULL) or merge IPv6 (he6) addresses with mapped 703*0Sstevel@tonic-gate * v4 (he4) addresses. In either case, the results are returned in res. 704*0Sstevel@tonic-gate * Caller must provide all buffers. 705*0Sstevel@tonic-gate * Inputs: 706*0Sstevel@tonic-gate * he4 pointer to IPv4 buffer 707*0Sstevel@tonic-gate * he6 pointer to IPv6 buffer (NULL if not merging v4/v6 708*0Sstevel@tonic-gate * res pointer to results buffer 709*0Sstevel@tonic-gate * mapped mapped == 1, map IPv4 : mapped == 0, compat IPv4 710*0Sstevel@tonic-gate * mapped flag is ignored if he6 != NULL 711*0Sstevel@tonic-gate * 712*0Sstevel@tonic-gate * The results are packed into the res->buffer as follows: 713*0Sstevel@tonic-gate * <--------------- buffer + buflen --------------------------------------> 714*0Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 715*0Sstevel@tonic-gate * | pointers vector | pointers vector | aliases grow | addresses grow | 716*0Sstevel@tonic-gate * | for addresses | for aliases | | | 717*0Sstevel@tonic-gate * | this way -> | this way -> | <- this way |<- this way | 718*0Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 719*0Sstevel@tonic-gate * | grows in PASS 1 | grows in PASS2 | grows in PASS2 | grows in PASS 1| 720*0Sstevel@tonic-gate */ 721*0Sstevel@tonic-gate static struct hostent * 722*0Sstevel@tonic-gate __mapv4tov6(struct hostent *he4, struct hostent *he6, nss_XbyY_buf_t *res, 723*0Sstevel@tonic-gate int mapped) 724*0Sstevel@tonic-gate { 725*0Sstevel@tonic-gate char *buffer, *limit; 726*0Sstevel@tonic-gate int buflen = res->buflen; 727*0Sstevel@tonic-gate struct in6_addr *addr6p; 728*0Sstevel@tonic-gate char *buff_locp; 729*0Sstevel@tonic-gate struct hostent *host; 730*0Sstevel@tonic-gate int count = 0, len, i; 731*0Sstevel@tonic-gate char *h_namep; 732*0Sstevel@tonic-gate 733*0Sstevel@tonic-gate if (he4 == NULL || res == NULL) { 734*0Sstevel@tonic-gate return (NULL); 735*0Sstevel@tonic-gate } 736*0Sstevel@tonic-gate limit = res->buffer + buflen; 737*0Sstevel@tonic-gate host = (struct hostent *)res->result; 738*0Sstevel@tonic-gate buffer = res->buffer; 739*0Sstevel@tonic-gate 740*0Sstevel@tonic-gate buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in6_addr)); 741*0Sstevel@tonic-gate host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **)); 742*0Sstevel@tonic-gate if ((char *)host->h_addr_list >= limit || 743*0Sstevel@tonic-gate buff_locp <= (char *)host->h_addr_list) { 744*0Sstevel@tonic-gate return (NULL); 745*0Sstevel@tonic-gate } 746*0Sstevel@tonic-gate if (he6 == NULL) { 747*0Sstevel@tonic-gate /* 748*0Sstevel@tonic-gate * If he6==NULL, map the v4 address into the v6 address format. 749*0Sstevel@tonic-gate * This is used for getipnodebyaddr() (single address, mapped or 750*0Sstevel@tonic-gate * compatible) or for v4 mapped for getipnodebyname(), which 751*0Sstevel@tonic-gate * could be multiple addresses. This could also be a literal 752*0Sstevel@tonic-gate * address string, which is why there is a inet_addr() call. 753*0Sstevel@tonic-gate */ 754*0Sstevel@tonic-gate for (i = 0; he4->h_addr_list[i] != NULL; i++) { 755*0Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 756*0Sstevel@tonic-gate if (buff_locp <= 757*0Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) { 758*0Sstevel@tonic-gate /* 759*0Sstevel@tonic-gate * Has to be room for the pointer to the address we're 760*0Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 761*0Sstevel@tonic-gate */ 762*0Sstevel@tonic-gate return (NULL); 763*0Sstevel@tonic-gate } 764*0Sstevel@tonic-gate addr6p = (struct in6_addr *)buff_locp; 765*0Sstevel@tonic-gate host->h_addr_list[count] = (char *)addr6p; 766*0Sstevel@tonic-gate bzero(addr6p->s6_addr, sizeof (struct in6_addr)); 767*0Sstevel@tonic-gate if (mapped) { 768*0Sstevel@tonic-gate addr6p->s6_addr[10] = 0xff; 769*0Sstevel@tonic-gate addr6p->s6_addr[11] = 0xff; 770*0Sstevel@tonic-gate } 771*0Sstevel@tonic-gate bcopy((char *)he4->h_addr_list[i], 772*0Sstevel@tonic-gate &addr6p->s6_addr[12], sizeof (struct in_addr)); 773*0Sstevel@tonic-gate ++count; 774*0Sstevel@tonic-gate } 775*0Sstevel@tonic-gate /* 776*0Sstevel@tonic-gate * Set last array element to NULL and add cname as first alias 777*0Sstevel@tonic-gate */ 778*0Sstevel@tonic-gate host->h_addr_list[count] = NULL; 779*0Sstevel@tonic-gate host->h_aliases = host->h_addr_list + count + 1; 780*0Sstevel@tonic-gate count = 0; 781*0Sstevel@tonic-gate if ((int)(inet_addr(he4->h_name)) != -1) { 782*0Sstevel@tonic-gate /* 783*0Sstevel@tonic-gate * Literal address string, since we're mapping, we need the IPv6 784*0Sstevel@tonic-gate * V4 mapped literal address string for h_name. 785*0Sstevel@tonic-gate */ 786*0Sstevel@tonic-gate char tmpstr[128]; 787*0Sstevel@tonic-gate inet_ntop(AF_INET6, host->h_addr_list[0], tmpstr, 788*0Sstevel@tonic-gate sizeof (tmpstr)); 789*0Sstevel@tonic-gate buff_locp -= (len = strlen(tmpstr) + 1); 790*0Sstevel@tonic-gate h_namep = tmpstr; 791*0Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 792*0Sstevel@tonic-gate return (NULL); 793*0Sstevel@tonic-gate bcopy(h_namep, buff_locp, len); 794*0Sstevel@tonic-gate host->h_name = buff_locp; 795*0Sstevel@tonic-gate host->h_aliases = NULL; /* no aliases for literal */ 796*0Sstevel@tonic-gate host->h_length = sizeof (struct in6_addr); 797*0Sstevel@tonic-gate host->h_addrtype = AF_INET6; 798*0Sstevel@tonic-gate return (host); /* we're done, return result */ 799*0Sstevel@tonic-gate } 800*0Sstevel@tonic-gate /* 801*0Sstevel@tonic-gate * Not a literal address string, so just copy h_name. 802*0Sstevel@tonic-gate */ 803*0Sstevel@tonic-gate buff_locp -= (len = strlen(he4->h_name) + 1); 804*0Sstevel@tonic-gate h_namep = he4->h_name; 805*0Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 806*0Sstevel@tonic-gate return (NULL); 807*0Sstevel@tonic-gate bcopy(h_namep, buff_locp, len); 808*0Sstevel@tonic-gate host->h_name = buff_locp; 809*0Sstevel@tonic-gate /* 810*0Sstevel@tonic-gate * Pass 2 (IPv4 aliases): 811*0Sstevel@tonic-gate */ 812*0Sstevel@tonic-gate for (i = 0; he4->h_aliases[i] != NULL; i++) { 813*0Sstevel@tonic-gate buff_locp -= (len = strlen(he4->h_aliases[i]) + 1); 814*0Sstevel@tonic-gate if (buff_locp <= 815*0Sstevel@tonic-gate (char *)&(host->h_aliases[count + 1])) { 816*0Sstevel@tonic-gate /* 817*0Sstevel@tonic-gate * Has to be room for the pointer to the address we're 818*0Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 819*0Sstevel@tonic-gate */ 820*0Sstevel@tonic-gate return (NULL); 821*0Sstevel@tonic-gate } 822*0Sstevel@tonic-gate host->h_aliases[count] = buff_locp; 823*0Sstevel@tonic-gate bcopy((char *)he4->h_aliases[i], buff_locp, len); 824*0Sstevel@tonic-gate ++count; 825*0Sstevel@tonic-gate } 826*0Sstevel@tonic-gate host->h_aliases[count] = NULL; 827*0Sstevel@tonic-gate host->h_length = sizeof (struct in6_addr); 828*0Sstevel@tonic-gate host->h_addrtype = AF_INET6; 829*0Sstevel@tonic-gate return (host); 830*0Sstevel@tonic-gate } else { 831*0Sstevel@tonic-gate /* 832*0Sstevel@tonic-gate * Merge IPv4 mapped addresses with IPv6 addresses. The 833*0Sstevel@tonic-gate * IPv6 address will go in first, followed by the v4 mapped. 834*0Sstevel@tonic-gate * 835*0Sstevel@tonic-gate * Pass 1 (IPv6 addresses): 836*0Sstevel@tonic-gate */ 837*0Sstevel@tonic-gate for (i = 0; he6->h_addr_list[i] != NULL; i++) { 838*0Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 839*0Sstevel@tonic-gate if (buff_locp <= 840*0Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) { 841*0Sstevel@tonic-gate /* 842*0Sstevel@tonic-gate * Has to be room for the pointer to the address we're 843*0Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 844*0Sstevel@tonic-gate */ 845*0Sstevel@tonic-gate return (NULL); 846*0Sstevel@tonic-gate } 847*0Sstevel@tonic-gate host->h_addr_list[count] = buff_locp; 848*0Sstevel@tonic-gate bcopy((char *)he6->h_addr_list[i], buff_locp, 849*0Sstevel@tonic-gate sizeof (struct in6_addr)); 850*0Sstevel@tonic-gate ++count; 851*0Sstevel@tonic-gate } 852*0Sstevel@tonic-gate /* 853*0Sstevel@tonic-gate * Pass 1 (IPv4 mapped addresses): 854*0Sstevel@tonic-gate */ 855*0Sstevel@tonic-gate for (i = 0; he4->h_addr_list[i] != NULL; i++) { 856*0Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 857*0Sstevel@tonic-gate if (buff_locp <= 858*0Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) { 859*0Sstevel@tonic-gate /* 860*0Sstevel@tonic-gate * Has to be room for the pointer to the address we're 861*0Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 862*0Sstevel@tonic-gate */ 863*0Sstevel@tonic-gate return (NULL); 864*0Sstevel@tonic-gate } 865*0Sstevel@tonic-gate addr6p = (struct in6_addr *)buff_locp; 866*0Sstevel@tonic-gate host->h_addr_list[count] = (char *)addr6p; 867*0Sstevel@tonic-gate bzero(addr6p->s6_addr, sizeof (struct in6_addr)); 868*0Sstevel@tonic-gate addr6p->s6_addr[10] = 0xff; 869*0Sstevel@tonic-gate addr6p->s6_addr[11] = 0xff; 870*0Sstevel@tonic-gate bcopy(he4->h_addr_list[i], &addr6p->s6_addr[12], 871*0Sstevel@tonic-gate sizeof (struct in_addr)); 872*0Sstevel@tonic-gate ++count; 873*0Sstevel@tonic-gate } 874*0Sstevel@tonic-gate /* 875*0Sstevel@tonic-gate * Pass 2 (IPv6 aliases, host name first). We start h_aliases 876*0Sstevel@tonic-gate * one after where h_addr_list array ended. This is where cname 877*0Sstevel@tonic-gate * is put, followed by all aliases. Reset count to 0, for index 878*0Sstevel@tonic-gate * in the h_aliases array. 879*0Sstevel@tonic-gate */ 880*0Sstevel@tonic-gate host->h_addr_list[count] = NULL; 881*0Sstevel@tonic-gate host->h_aliases = host->h_addr_list + count + 1; 882*0Sstevel@tonic-gate count = 0; 883*0Sstevel@tonic-gate buff_locp -= (len = strlen(he6->h_name) + 1); 884*0Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 885*0Sstevel@tonic-gate return (NULL); 886*0Sstevel@tonic-gate bcopy(he6->h_name, buff_locp, len); 887*0Sstevel@tonic-gate host->h_name = buff_locp; 888*0Sstevel@tonic-gate for (i = 0; he6->h_aliases[i] != NULL; i++) { 889*0Sstevel@tonic-gate buff_locp -= (len = strlen(he6->h_aliases[i]) + 1); 890*0Sstevel@tonic-gate if (buff_locp <= 891*0Sstevel@tonic-gate (char *)&(host->h_aliases[count + 1])) { 892*0Sstevel@tonic-gate /* 893*0Sstevel@tonic-gate * Has to be room for the pointer to the address we're 894*0Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 895*0Sstevel@tonic-gate */ 896*0Sstevel@tonic-gate return (NULL); 897*0Sstevel@tonic-gate } 898*0Sstevel@tonic-gate host->h_aliases[count] = buff_locp; 899*0Sstevel@tonic-gate bcopy((char *)he6->h_aliases[i], buff_locp, len); 900*0Sstevel@tonic-gate ++count; 901*0Sstevel@tonic-gate } 902*0Sstevel@tonic-gate /* 903*0Sstevel@tonic-gate * Pass 2 (IPv4 aliases): 904*0Sstevel@tonic-gate */ 905*0Sstevel@tonic-gate for (i = 0; he4->h_aliases[i] != NULL; i++) { 906*0Sstevel@tonic-gate buff_locp -= (len = strlen(he4->h_aliases[i]) + 1); 907*0Sstevel@tonic-gate if (buff_locp <= 908*0Sstevel@tonic-gate (char *)&(host->h_aliases[count + 1])) { 909*0Sstevel@tonic-gate /* 910*0Sstevel@tonic-gate * Has to be room for the pointer to the address we're 911*0Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 912*0Sstevel@tonic-gate */ 913*0Sstevel@tonic-gate return (NULL); 914*0Sstevel@tonic-gate } 915*0Sstevel@tonic-gate host->h_aliases[count] = buff_locp; 916*0Sstevel@tonic-gate bcopy((char *)he4->h_aliases[i], buff_locp, len); 917*0Sstevel@tonic-gate ++count; 918*0Sstevel@tonic-gate } 919*0Sstevel@tonic-gate host->h_aliases[count] = NULL; 920*0Sstevel@tonic-gate host->h_length = sizeof (struct in6_addr); 921*0Sstevel@tonic-gate host->h_addrtype = AF_INET6; 922*0Sstevel@tonic-gate return (host); 923*0Sstevel@tonic-gate } 924*0Sstevel@tonic-gate } 925*0Sstevel@tonic-gate 926*0Sstevel@tonic-gate /* 927*0Sstevel@tonic-gate * This routine will convert a mapped v4 hostent (AF_INET6) to a 928*0Sstevel@tonic-gate * AF_INET hostent. If no mapped addrs found, then a NULL is returned. 929*0Sstevel@tonic-gate * If mapped addrs found, then a new buffer is alloc'd and all the v4 mapped 930*0Sstevel@tonic-gate * addresses are extracted and copied to it. On sucess, a pointer to a new 931*0Sstevel@tonic-gate * hostent is returned. 932*0Sstevel@tonic-gate * There are two possible errors in which case a NULL is returned. 933*0Sstevel@tonic-gate * One of two error codes are returned: 934*0Sstevel@tonic-gate * 935*0Sstevel@tonic-gate * NO_RECOVERY - a malloc failed or the like for which there's no recovery. 936*0Sstevel@tonic-gate * NO_ADDRESS - after filtering all the v4, there was nothing left! 937*0Sstevel@tonic-gate * 938*0Sstevel@tonic-gate * Inputs: 939*0Sstevel@tonic-gate * he pointer to hostent with mapped v4 addresses 940*0Sstevel@tonic-gate * filter_error pointer to return error code 941*0Sstevel@tonic-gate * Return: 942*0Sstevel@tonic-gate * pointer to a malloc'd hostent with v4 addresses. 943*0Sstevel@tonic-gate * 944*0Sstevel@tonic-gate * The results are packed into the res->buffer as follows: 945*0Sstevel@tonic-gate * <--------------- buffer + buflen --------------------------------------> 946*0Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 947*0Sstevel@tonic-gate * | pointers vector | pointers vector | aliases grow | addresses grow | 948*0Sstevel@tonic-gate * | for addresses | for aliases | | | 949*0Sstevel@tonic-gate * | this way -> | this way -> | <- this way |<- this way | 950*0Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 951*0Sstevel@tonic-gate * | grows in PASS 1 | grows in PASS2 | grows in PASS2 | grows in PASS 1| 952*0Sstevel@tonic-gate */ 953*0Sstevel@tonic-gate struct hostent * 954*0Sstevel@tonic-gate __mappedtov4(struct hostent *he, int *extract_error) 955*0Sstevel@tonic-gate { 956*0Sstevel@tonic-gate char *buffer, *limit; 957*0Sstevel@tonic-gate nss_XbyY_buf_t *res; 958*0Sstevel@tonic-gate int buflen = NSS_BUFLEN_HOSTS; 959*0Sstevel@tonic-gate struct in_addr *addr4p; 960*0Sstevel@tonic-gate char *buff_locp; 961*0Sstevel@tonic-gate struct hostent *host; 962*0Sstevel@tonic-gate int count = 0, len, i; 963*0Sstevel@tonic-gate char *h_namep; 964*0Sstevel@tonic-gate 965*0Sstevel@tonic-gate if (he == NULL) { 966*0Sstevel@tonic-gate *extract_error = NO_ADDRESS; 967*0Sstevel@tonic-gate return (NULL); 968*0Sstevel@tonic-gate } 969*0Sstevel@tonic-gate if ((__find_mapped(he, 0)) == 0) { 970*0Sstevel@tonic-gate *extract_error = NO_ADDRESS; 971*0Sstevel@tonic-gate return (NULL); 972*0Sstevel@tonic-gate } 973*0Sstevel@tonic-gate if ((res = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == 0) { 974*0Sstevel@tonic-gate *extract_error = NO_RECOVERY; 975*0Sstevel@tonic-gate return (NULL); 976*0Sstevel@tonic-gate } 977*0Sstevel@tonic-gate limit = res->buffer + buflen; 978*0Sstevel@tonic-gate host = (struct hostent *)res->result; 979*0Sstevel@tonic-gate buffer = res->buffer; 980*0Sstevel@tonic-gate 981*0Sstevel@tonic-gate buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in_addr)); 982*0Sstevel@tonic-gate host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **)); 983*0Sstevel@tonic-gate if ((char *)host->h_addr_list >= limit || 984*0Sstevel@tonic-gate buff_locp <= (char *)host->h_addr_list) 985*0Sstevel@tonic-gate goto cleanup; 986*0Sstevel@tonic-gate /* 987*0Sstevel@tonic-gate * "Unmap" the v4 mapped address(es) into a v4 hostent format. 988*0Sstevel@tonic-gate * This is used for getipnodebyaddr() (single address) or for 989*0Sstevel@tonic-gate * v4 mapped for getipnodebyname(), which could be multiple 990*0Sstevel@tonic-gate * addresses. This could also be a literal address string, 991*0Sstevel@tonic-gate * which is why there is a inet_addr() call. 992*0Sstevel@tonic-gate */ 993*0Sstevel@tonic-gate for (i = 0; he->h_addr_list[i] != NULL; i++) { 994*0Sstevel@tonic-gate if (!IN6_IS_ADDR_V4MAPPED((struct in6_addr *) 995*0Sstevel@tonic-gate he->h_addr_list[i])) 996*0Sstevel@tonic-gate continue; 997*0Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 998*0Sstevel@tonic-gate /* 999*0Sstevel@tonic-gate * Has to be room for the pointer to the address we're 1000*0Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 1001*0Sstevel@tonic-gate */ 1002*0Sstevel@tonic-gate if (buff_locp <= 1003*0Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) 1004*0Sstevel@tonic-gate goto cleanup; 1005*0Sstevel@tonic-gate addr4p = (struct in_addr *)buff_locp; 1006*0Sstevel@tonic-gate host->h_addr_list[count] = (char *)addr4p; 1007*0Sstevel@tonic-gate bzero((char *)&addr4p->s_addr, 1008*0Sstevel@tonic-gate sizeof (struct in_addr)); 1009*0Sstevel@tonic-gate IN6_V4MAPPED_TO_INADDR( 1010*0Sstevel@tonic-gate (struct in6_addr *)he->h_addr_list[i], 1011*0Sstevel@tonic-gate addr4p); 1012*0Sstevel@tonic-gate ++count; 1013*0Sstevel@tonic-gate } 1014*0Sstevel@tonic-gate /* 1015*0Sstevel@tonic-gate * Set last array element to NULL and add cname as first alias 1016*0Sstevel@tonic-gate */ 1017*0Sstevel@tonic-gate host->h_addr_list[count] = NULL; 1018*0Sstevel@tonic-gate host->h_aliases = host->h_addr_list + count + 1; 1019*0Sstevel@tonic-gate count = 0; 1020*0Sstevel@tonic-gate /* Copy official host name */ 1021*0Sstevel@tonic-gate buff_locp -= (len = strlen(he->h_name) + 1); 1022*0Sstevel@tonic-gate h_namep = he->h_name; 1023*0Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 1024*0Sstevel@tonic-gate goto cleanup; 1025*0Sstevel@tonic-gate bcopy(h_namep, buff_locp, len); 1026*0Sstevel@tonic-gate host->h_name = buff_locp; 1027*0Sstevel@tonic-gate /* 1028*0Sstevel@tonic-gate * Pass 2 (IPv4 aliases): 1029*0Sstevel@tonic-gate */ 1030*0Sstevel@tonic-gate for (i = 0; he->h_aliases[i] != NULL; i++) { 1031*0Sstevel@tonic-gate buff_locp -= (len = strlen(he->h_aliases[i]) + 1); 1032*0Sstevel@tonic-gate /* 1033*0Sstevel@tonic-gate * Has to be room for the pointer to the address we're 1034*0Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 1035*0Sstevel@tonic-gate */ 1036*0Sstevel@tonic-gate if (buff_locp <= 1037*0Sstevel@tonic-gate (char *)&(host->h_aliases[count + 1])) 1038*0Sstevel@tonic-gate goto cleanup; 1039*0Sstevel@tonic-gate host->h_aliases[count] = buff_locp; 1040*0Sstevel@tonic-gate bcopy((char *)he->h_aliases[i], buff_locp, len); 1041*0Sstevel@tonic-gate ++count; 1042*0Sstevel@tonic-gate } 1043*0Sstevel@tonic-gate host->h_aliases[count] = NULL; 1044*0Sstevel@tonic-gate host->h_length = sizeof (struct in_addr); 1045*0Sstevel@tonic-gate host->h_addrtype = AF_INET; 1046*0Sstevel@tonic-gate free(res); 1047*0Sstevel@tonic-gate return (host); 1048*0Sstevel@tonic-gate cleanup: 1049*0Sstevel@tonic-gate *extract_error = NO_RECOVERY; 1050*0Sstevel@tonic-gate (void) __IPv6_cleanup(res); 1051*0Sstevel@tonic-gate return (NULL); 1052*0Sstevel@tonic-gate } 1053*0Sstevel@tonic-gate 1054*0Sstevel@tonic-gate /* 1055*0Sstevel@tonic-gate * This routine takes as input a pointer to a hostent and filters out 1056*0Sstevel@tonic-gate * the type of addresses specified by the af argument. AF_INET 1057*0Sstevel@tonic-gate * indicates that the caller wishes to filter out IPv4-mapped 1058*0Sstevel@tonic-gate * addresses, and AF_INET6 indicates that the caller wishes to filter 1059*0Sstevel@tonic-gate * out IPv6 addresses which aren't IPv4-mapped. If filtering would 1060*0Sstevel@tonic-gate * result in all addresses being filtered out, a NULL pointer is returned. 1061*0Sstevel@tonic-gate * Otherwise, the he pointer passed in is returned, even if no addresses 1062*0Sstevel@tonic-gate * were filtered out. 1063*0Sstevel@tonic-gate */ 1064*0Sstevel@tonic-gate static struct hostent * 1065*0Sstevel@tonic-gate __filter_addresses(int af, struct hostent *he) 1066*0Sstevel@tonic-gate { 1067*0Sstevel@tonic-gate struct in6_addr **in6addrlist, **in6addr; 1068*0Sstevel@tonic-gate boolean_t isipv4mapped; 1069*0Sstevel@tonic-gate int i = 0; 1070*0Sstevel@tonic-gate char addrstr[INET6_ADDRSTRLEN]; 1071*0Sstevel@tonic-gate 1072*0Sstevel@tonic-gate if (he == NULL) 1073*0Sstevel@tonic-gate return (NULL); 1074*0Sstevel@tonic-gate 1075*0Sstevel@tonic-gate in6addrlist = (struct in6_addr **)he->h_addr_list; 1076*0Sstevel@tonic-gate for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) { 1077*0Sstevel@tonic-gate isipv4mapped = IN6_IS_ADDR_V4MAPPED(*in6addr); 1078*0Sstevel@tonic-gate 1079*0Sstevel@tonic-gate if ((af == AF_INET && !isipv4mapped) || 1080*0Sstevel@tonic-gate (af == AF_INET6 && isipv4mapped)) { 1081*0Sstevel@tonic-gate if (in6addrlist[i] != *in6addr) 1082*0Sstevel@tonic-gate in6addrlist[i] = *in6addr; 1083*0Sstevel@tonic-gate i++; 1084*0Sstevel@tonic-gate } 1085*0Sstevel@tonic-gate } 1086*0Sstevel@tonic-gate 1087*0Sstevel@tonic-gate if (i == 0) { 1088*0Sstevel@tonic-gate /* We filtered everything out. */ 1089*0Sstevel@tonic-gate return (NULL); 1090*0Sstevel@tonic-gate } else { 1091*0Sstevel@tonic-gate /* NULL terminate the list and return the hostent */ 1092*0Sstevel@tonic-gate in6addrlist[i] = NULL; 1093*0Sstevel@tonic-gate return (he); 1094*0Sstevel@tonic-gate } 1095*0Sstevel@tonic-gate } 1096*0Sstevel@tonic-gate 1097*0Sstevel@tonic-gate /* 1098*0Sstevel@tonic-gate * This routine searches a hostent for v4 mapped IPv6 addresses. 1099*0Sstevel@tonic-gate * he hostent structure to seach 1100*0Sstevel@tonic-gate * find_both flag indicating if only want mapped or both map'd and v6 1101*0Sstevel@tonic-gate * return values: 1102*0Sstevel@tonic-gate * 0 = No mapped addresses 1103*0Sstevel@tonic-gate * 1 = Mapped v4 address found (returns on first one found) 1104*0Sstevel@tonic-gate * 2 = Both v6 and v4 mapped are present 1105*0Sstevel@tonic-gate * 1106*0Sstevel@tonic-gate * If hostent passed in with no addresses, zero will be returned. 1107*0Sstevel@tonic-gate */ 1108*0Sstevel@tonic-gate 1109*0Sstevel@tonic-gate static int 1110*0Sstevel@tonic-gate __find_mapped(struct hostent *he, int find_both) 1111*0Sstevel@tonic-gate { 1112*0Sstevel@tonic-gate int i; 1113*0Sstevel@tonic-gate int mapd_found = 0; 1114*0Sstevel@tonic-gate int v6_found = 0; 1115*0Sstevel@tonic-gate 1116*0Sstevel@tonic-gate for (i = 0; he->h_addr_list[i] != NULL; i++) { 1117*0Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED( 1118*0Sstevel@tonic-gate (struct in6_addr *)he->h_addr_list[i])) { 1119*0Sstevel@tonic-gate if (find_both) 1120*0Sstevel@tonic-gate mapd_found = 1; 1121*0Sstevel@tonic-gate else 1122*0Sstevel@tonic-gate return (1); 1123*0Sstevel@tonic-gate } else { 1124*0Sstevel@tonic-gate v6_found = 1; 1125*0Sstevel@tonic-gate } 1126*0Sstevel@tonic-gate /* save some iterations once both found */ 1127*0Sstevel@tonic-gate if (mapd_found && v6_found) 1128*0Sstevel@tonic-gate return (2); 1129*0Sstevel@tonic-gate } 1130*0Sstevel@tonic-gate return (mapd_found); 1131*0Sstevel@tonic-gate } 1132*0Sstevel@tonic-gate 1133*0Sstevel@tonic-gate /* 1134*0Sstevel@tonic-gate * This routine was added specifically for the IPv6 getipnodeby*() APIs. This 1135*0Sstevel@tonic-gate * separates the result pointer (ptr to hostent+data buf) from the 1136*0Sstevel@tonic-gate * nss_XbyY_buf_t ptr (required for nsswitch API). The returned hostent ptr 1137*0Sstevel@tonic-gate * can be passed to freehostent() and freed independently. 1138*0Sstevel@tonic-gate * 1139*0Sstevel@tonic-gate * bufp->result bufp->buffer 1140*0Sstevel@tonic-gate * | | 1141*0Sstevel@tonic-gate * V V 1142*0Sstevel@tonic-gate * ------------------------------------------------...-- 1143*0Sstevel@tonic-gate * |struct hostent |addresses aliases | 1144*0Sstevel@tonic-gate * ------------------------------------------------...-- 1145*0Sstevel@tonic-gate * | |<--------bufp->buflen-------------->| 1146*0Sstevel@tonic-gate */ 1147*0Sstevel@tonic-gate 1148*0Sstevel@tonic-gate #define ALIGN(x) ((((long)(x)) + sizeof (long) - 1) & ~(sizeof (long) - 1)) 1149*0Sstevel@tonic-gate 1150*0Sstevel@tonic-gate static nss_XbyY_buf_t * 1151*0Sstevel@tonic-gate __IPv6_alloc(int bufsz) 1152*0Sstevel@tonic-gate { 1153*0Sstevel@tonic-gate nss_XbyY_buf_t *bufp; 1154*0Sstevel@tonic-gate 1155*0Sstevel@tonic-gate if ((bufp = malloc(sizeof (nss_XbyY_buf_t))) == NULL) 1156*0Sstevel@tonic-gate return (NULL); 1157*0Sstevel@tonic-gate 1158*0Sstevel@tonic-gate if ((bufp->result = malloc(ALIGN(sizeof (struct hostent)) + bufsz)) == 1159*0Sstevel@tonic-gate NULL) { 1160*0Sstevel@tonic-gate free(bufp); 1161*0Sstevel@tonic-gate return (NULL); 1162*0Sstevel@tonic-gate } 1163*0Sstevel@tonic-gate bufp->buffer = (char *)(bufp->result) + sizeof (struct hostent); 1164*0Sstevel@tonic-gate bufp->buflen = bufsz; 1165*0Sstevel@tonic-gate return (bufp); 1166*0Sstevel@tonic-gate } 1167*0Sstevel@tonic-gate 1168*0Sstevel@tonic-gate /* 1169*0Sstevel@tonic-gate * This routine is use only for error return cleanup. This will free the 1170*0Sstevel@tonic-gate * hostent pointer, so don't use for successful returns. 1171*0Sstevel@tonic-gate */ 1172*0Sstevel@tonic-gate static void 1173*0Sstevel@tonic-gate __IPv6_cleanup(nss_XbyY_buf_t *bufp) 1174*0Sstevel@tonic-gate { 1175*0Sstevel@tonic-gate if (bufp == NULL) 1176*0Sstevel@tonic-gate return; 1177*0Sstevel@tonic-gate if (bufp->result != NULL) 1178*0Sstevel@tonic-gate free(bufp->result); 1179*0Sstevel@tonic-gate free(bufp); 1180*0Sstevel@tonic-gate } 1181