17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*5b79143cSGirish Moodalbail * Common Development and Distribution License (the "License"). 6*5b79143cSGirish Moodalbail * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 2161961e0fSrobinson 227c478bd9Sstevel@tonic-gate /* 23*5b79143cSGirish Moodalbail * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate * 267c478bd9Sstevel@tonic-gate * This file defines and implements the re-entrant getipnodebyname(), 277c478bd9Sstevel@tonic-gate * getipnodebyaddr(), and freehostent() routines for IPv6. These routines 287c478bd9Sstevel@tonic-gate * follow use the netdir_getbyYY() (see netdir_inet.c). 297c478bd9Sstevel@tonic-gate * 307c478bd9Sstevel@tonic-gate * lib/libnsl/nss/getipnodeby.c 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include "mt.h" 347c478bd9Sstevel@tonic-gate #include <stdlib.h> 357c478bd9Sstevel@tonic-gate #include <unistd.h> 367c478bd9Sstevel@tonic-gate #include <stropts.h> 377c478bd9Sstevel@tonic-gate #include <ctype.h> 387c478bd9Sstevel@tonic-gate #include <string.h> 397c478bd9Sstevel@tonic-gate #include <strings.h> 407c478bd9Sstevel@tonic-gate #include <netdb.h> 417c478bd9Sstevel@tonic-gate #include <stdio.h> 427c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 437c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h> 447c478bd9Sstevel@tonic-gate #include <netinet/in.h> 457c478bd9Sstevel@tonic-gate #include <sys/socket.h> 467c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 477c478bd9Sstevel@tonic-gate #include <nss_netdir.h> 487c478bd9Sstevel@tonic-gate #include <net/if.h> 497c478bd9Sstevel@tonic-gate #include <netinet/in.h> 507c478bd9Sstevel@tonic-gate #include <netdir.h> 517c478bd9Sstevel@tonic-gate #include <thread.h> 527c478bd9Sstevel@tonic-gate #include <synch.h> 537c478bd9Sstevel@tonic-gate #include <fcntl.h> 547c478bd9Sstevel@tonic-gate #include <sys/time.h> 557c478bd9Sstevel@tonic-gate #include "nss.h" 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #define IPV6_LITERAL_CHAR ':' 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate /* 607c478bd9Sstevel@tonic-gate * The number of nanoseconds getipnodebyname() waits before getting 617c478bd9Sstevel@tonic-gate * fresh interface count information with SIOCGLIFNUM. The default is 627c478bd9Sstevel@tonic-gate * five minutes. 637c478bd9Sstevel@tonic-gate */ 647c478bd9Sstevel@tonic-gate #define IFNUM_TIMEOUT ((hrtime_t)300 * NANOSEC) 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate /* 677c478bd9Sstevel@tonic-gate * Bits in the bitfield returned by getipnodebyname_processflags(). 687c478bd9Sstevel@tonic-gate * 697c478bd9Sstevel@tonic-gate * IPNODE_WANTIPV6 The user wants IPv6 addresses returned. 707c478bd9Sstevel@tonic-gate * IPNODE_WANTIPV4 The user wants IPv4 addresses returned. 717c478bd9Sstevel@tonic-gate * IPNODE_IPV4IFNOIPV6 The user only wants IPv4 addresses returned if no IPv6 727c478bd9Sstevel@tonic-gate * addresses are returned. 737c478bd9Sstevel@tonic-gate * IPNODE_LOOKUPIPNODES getipnodebyname() needs to lookup the name in ipnodes. 747c478bd9Sstevel@tonic-gate * IPNODE_LOOKUPHOSTS getipnodebyname() needs to lookup the name in hosts. 757c478bd9Sstevel@tonic-gate * IPNODE_ISLITERAL The name supplied is a literal address string. 767c478bd9Sstevel@tonic-gate */ 777c478bd9Sstevel@tonic-gate #define IPNODE_WANTIPV6 0x00000001u 787c478bd9Sstevel@tonic-gate #define IPNODE_WANTIPV4 0x00000002u 797c478bd9Sstevel@tonic-gate #define IPNODE_IPV4IFNOIPV6 0x00000004u 807c478bd9Sstevel@tonic-gate #define IPNODE_LOOKUPIPNODES 0x00000008u 817c478bd9Sstevel@tonic-gate #define IPNODE_LOOKUPHOSTS 0x00000010u 827c478bd9Sstevel@tonic-gate #define IPNODE_LITERAL 0x00000020u 837c478bd9Sstevel@tonic-gate #define IPNODE_IPV4 (IPNODE_WANTIPV4 | IPNODE_IPV4IFNOIPV6) 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate /* 867c478bd9Sstevel@tonic-gate * The default set of bits corresponding to a getipnodebyname() flags 877c478bd9Sstevel@tonic-gate * argument of AI_DEFAULT. 887c478bd9Sstevel@tonic-gate */ 897c478bd9Sstevel@tonic-gate #define IPNODE_DEFAULT (IPNODE_WANTIPV6 | IPNODE_IPV4 | \ 907c478bd9Sstevel@tonic-gate IPNODE_LOOKUPIPNODES | IPNODE_LOOKUPHOSTS) 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate extern struct netconfig *__rpc_getconfip(char *); 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate static struct hostent *__mapv4tov6(struct hostent *, struct hostent *, 957c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *, int); 967c478bd9Sstevel@tonic-gate struct hostent *__mappedtov4(struct hostent *, int *); 977c478bd9Sstevel@tonic-gate static struct hostent *__filter_addresses(int, struct hostent *); 987c478bd9Sstevel@tonic-gate static int __find_mapped(struct hostent *, int); 997c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *__IPv6_alloc(int); 1007c478bd9Sstevel@tonic-gate static void __IPv6_cleanup(nss_XbyY_buf_t *); 1017c478bd9Sstevel@tonic-gate static int __ai_addrconfig(int); 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate #ifdef PIC 1057c478bd9Sstevel@tonic-gate struct hostent * 1067c478bd9Sstevel@tonic-gate _uncached_getipnodebyname(const char *nam, struct hostent *result, 1077c478bd9Sstevel@tonic-gate char *buffer, int buflen, int af_family, int flags, int *h_errnop) 1087c478bd9Sstevel@tonic-gate { 10961961e0fSrobinson return (_switch_getipnodebyname_r(nam, result, buffer, buflen, 11061961e0fSrobinson af_family, flags, h_errnop)); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate struct hostent * 1147c478bd9Sstevel@tonic-gate _uncached_getipnodebyaddr(const char *addr, int length, int type, 1157c478bd9Sstevel@tonic-gate struct hostent *result, char *buffer, int buflen, int *h_errnop) 1167c478bd9Sstevel@tonic-gate { 1177c478bd9Sstevel@tonic-gate if (type == AF_INET) 1187c478bd9Sstevel@tonic-gate return (_switch_gethostbyaddr_r(addr, length, type, 1197c478bd9Sstevel@tonic-gate result, buffer, buflen, h_errnop)); 1207c478bd9Sstevel@tonic-gate else if (type == AF_INET6) 1217c478bd9Sstevel@tonic-gate return (_switch_getipnodebyaddr_r(addr, length, type, 1227c478bd9Sstevel@tonic-gate result, buffer, buflen, h_errnop)); 1237c478bd9Sstevel@tonic-gate return (NULL); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate #endif 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * Given a name, an address family, and a set of flags, return a 1297c478bd9Sstevel@tonic-gate * bitfield that getipnodebyname() will use. 1307c478bd9Sstevel@tonic-gate */ 1317c478bd9Sstevel@tonic-gate static uint_t 1327c478bd9Sstevel@tonic-gate getipnodebyname_processflags(const char *name, int af, int flags) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate uint_t ipnode_bits = IPNODE_DEFAULT; 1357c478bd9Sstevel@tonic-gate boolean_t ipv6configured = B_FALSE; 1367c478bd9Sstevel@tonic-gate boolean_t ipv4configured = B_FALSE; 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate /* 1397c478bd9Sstevel@tonic-gate * If AI_ADDRCONFIG is specified, we need to determine the number 1407c478bd9Sstevel@tonic-gate * of addresses of each address family configured on the system as 1417c478bd9Sstevel@tonic-gate * appropriate. 1427c478bd9Sstevel@tonic-gate */ 1437c478bd9Sstevel@tonic-gate if (flags & AI_ADDRCONFIG) { 1447c478bd9Sstevel@tonic-gate ipv6configured = (af == AF_INET6 && 1457c478bd9Sstevel@tonic-gate __ai_addrconfig(AF_INET6) > 0); 1467c478bd9Sstevel@tonic-gate ipv4configured = ((af == AF_INET || (flags & AI_V4MAPPED)) && 1477c478bd9Sstevel@tonic-gate __ai_addrconfig(AF_INET) > 0); 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate /* 1517c478bd9Sstevel@tonic-gate * Determine what kinds of addresses the user is interested 1527c478bd9Sstevel@tonic-gate * in getting back. 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate switch (af) { 1557c478bd9Sstevel@tonic-gate case AF_INET6: 1567c478bd9Sstevel@tonic-gate if ((flags & AI_ADDRCONFIG) && !ipv6configured) 1577c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_WANTIPV6; 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate if (flags & AI_V4MAPPED) { 1607c478bd9Sstevel@tonic-gate if ((flags & AI_ADDRCONFIG) && !ipv4configured) { 1617c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4; 1627c478bd9Sstevel@tonic-gate } else if (flags & AI_ALL) { 1637c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4IFNOIPV6; 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate } else { 1667c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4; 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate break; 1697c478bd9Sstevel@tonic-gate case AF_INET: 1707c478bd9Sstevel@tonic-gate if ((flags & AI_ADDRCONFIG) && !ipv4configured) 1717c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4; 1727c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_WANTIPV6; 1737c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4IFNOIPV6; 1747c478bd9Sstevel@tonic-gate break; 1757c478bd9Sstevel@tonic-gate default: 1767c478bd9Sstevel@tonic-gate ipnode_bits = 0; 1777c478bd9Sstevel@tonic-gate break; 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate /* 1817c478bd9Sstevel@tonic-gate * If we're not looking for IPv4 addresses, don't bother looking 1827c478bd9Sstevel@tonic-gate * in hosts. 1837c478bd9Sstevel@tonic-gate */ 1847c478bd9Sstevel@tonic-gate if (!(ipnode_bits & IPNODE_WANTIPV4)) 1857c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPHOSTS; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate /* 1887c478bd9Sstevel@tonic-gate * Determine if name is a literal IP address. This will 1897c478bd9Sstevel@tonic-gate * further narrow down what type of lookup we're going to do. 1907c478bd9Sstevel@tonic-gate */ 1917c478bd9Sstevel@tonic-gate if (strchr(name, IPV6_LITERAL_CHAR) != NULL) { 1927c478bd9Sstevel@tonic-gate /* Literal IPv6 address */ 1937c478bd9Sstevel@tonic-gate ipnode_bits |= IPNODE_LITERAL; 1947c478bd9Sstevel@tonic-gate /* 1957c478bd9Sstevel@tonic-gate * In s9 we accepted the literal without filtering independent 1967c478bd9Sstevel@tonic-gate * of what family was passed in hints. We continue to do 1977c478bd9Sstevel@tonic-gate * this. 1987c478bd9Sstevel@tonic-gate */ 1997c478bd9Sstevel@tonic-gate ipnode_bits |= (IPNODE_WANTIPV6 | IPNODE_WANTIPV4); 2007c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPHOSTS; 20161961e0fSrobinson } else if (inet_addr(name) != 0xffffffffU) { 2027c478bd9Sstevel@tonic-gate /* Literal IPv4 address */ 2037c478bd9Sstevel@tonic-gate ipnode_bits |= (IPNODE_LITERAL | IPNODE_WANTIPV4); 2047c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_WANTIPV6; 2057c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPIPNODES; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate return (ipnode_bits); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate struct hostent * 2117c478bd9Sstevel@tonic-gate getipnodebyname(const char *name, int af, int flags, int *error_num) 2127c478bd9Sstevel@tonic-gate { 2137c478bd9Sstevel@tonic-gate struct hostent *hp = NULL; 2147c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *buf4 = NULL; 2157c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *buf6 = NULL; 2167c478bd9Sstevel@tonic-gate struct netconfig *nconf; 2177c478bd9Sstevel@tonic-gate struct nss_netdirbyname_in nssin; 2187c478bd9Sstevel@tonic-gate union nss_netdirbyname_out nssout; 2197c478bd9Sstevel@tonic-gate int ret; 2207c478bd9Sstevel@tonic-gate uint_t ipnode_bits; 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 2237c478bd9Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 2247c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 2257c478bd9Sstevel@tonic-gate return (NULL); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate ipnode_bits = getipnodebyname_processflags(name, af, flags); 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate /* Make sure we have something to look up. */ 2317c478bd9Sstevel@tonic-gate if (!(ipnode_bits & (IPNODE_WANTIPV6 | IPNODE_WANTIPV4))) { 2327c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 2337c478bd9Sstevel@tonic-gate goto cleanup; 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate /* 2377c478bd9Sstevel@tonic-gate * Perform the requested lookups. We always look through 2387c478bd9Sstevel@tonic-gate * ipnodes first for both IPv4 and IPv6 addresses. Depending 2397c478bd9Sstevel@tonic-gate * on what was returned and what was needed, we either filter 2407c478bd9Sstevel@tonic-gate * out the garbage, or ask for more using hosts. 2417c478bd9Sstevel@tonic-gate */ 2427c478bd9Sstevel@tonic-gate if (ipnode_bits & IPNODE_LOOKUPIPNODES) { 2437c478bd9Sstevel@tonic-gate if ((buf6 = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == NULL) { 2447c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 2457c478bd9Sstevel@tonic-gate goto cleanup; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 2487c478bd9Sstevel@tonic-gate nssin.arg.nss.host6.name = name; 2497c478bd9Sstevel@tonic-gate nssin.arg.nss.host6.buf = buf6->buffer; 2507c478bd9Sstevel@tonic-gate nssin.arg.nss.host6.buflen = buf6->buflen; 2517c478bd9Sstevel@tonic-gate nssin.arg.nss.host6.af_family = af; 2527c478bd9Sstevel@tonic-gate nssin.arg.nss.host6.flags = flags; 2537c478bd9Sstevel@tonic-gate nssout.nss.host.hent = buf6->result; 2547c478bd9Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 2557c478bd9Sstevel@tonic-gate ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout); 2567c478bd9Sstevel@tonic-gate if (ret != ND_OK) { 2577c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf6); 2587c478bd9Sstevel@tonic-gate buf6 = NULL; 2597c478bd9Sstevel@tonic-gate } else if (ipnode_bits & IPNODE_WANTIPV4) { 2607c478bd9Sstevel@tonic-gate /* 2617c478bd9Sstevel@tonic-gate * buf6 may have all that we need if we either 2627c478bd9Sstevel@tonic-gate * only wanted IPv4 addresses if there were no 2637c478bd9Sstevel@tonic-gate * IPv6 addresses returned, or if there are 2647c478bd9Sstevel@tonic-gate * IPv4-mapped addresses in buf6. If either 2657c478bd9Sstevel@tonic-gate * of these are true, then there's no need to 2667c478bd9Sstevel@tonic-gate * look in hosts. 2677c478bd9Sstevel@tonic-gate */ 2687c478bd9Sstevel@tonic-gate if (ipnode_bits & IPNODE_IPV4IFNOIPV6 || 2697c478bd9Sstevel@tonic-gate __find_mapped(buf6->result, 0) != 0) { 2707c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPHOSTS; 2717c478bd9Sstevel@tonic-gate } else if (!(ipnode_bits & IPNODE_WANTIPV6)) { 2727c478bd9Sstevel@tonic-gate /* 2737c478bd9Sstevel@tonic-gate * If all we're looking for are IPv4 2747c478bd9Sstevel@tonic-gate * addresses and there are none in 2757c478bd9Sstevel@tonic-gate * buf6 then buf6 is now useless. 2767c478bd9Sstevel@tonic-gate */ 2777c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf6); 2787c478bd9Sstevel@tonic-gate buf6 = NULL; 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate if (ipnode_bits & IPNODE_LOOKUPHOSTS) { 2837c478bd9Sstevel@tonic-gate if ((buf4 = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == NULL) { 2847c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 2857c478bd9Sstevel@tonic-gate goto cleanup; 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate nssin.op_t = NSS_HOST; 2887c478bd9Sstevel@tonic-gate nssin.arg.nss.host.name = name; 2897c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buf = buf4->buffer; 2907c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf4->buflen; 2917c478bd9Sstevel@tonic-gate nssout.nss.host.hent = buf4->result; 2927c478bd9Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 2937c478bd9Sstevel@tonic-gate ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout); 2947c478bd9Sstevel@tonic-gate if (ret != ND_OK) { 2957c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf4); 2967c478bd9Sstevel@tonic-gate buf4 = NULL; 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate if (buf6 == NULL && buf4 == NULL) { 3017c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 3027c478bd9Sstevel@tonic-gate goto cleanup; 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate /* Extract the appropriate addresses from the returned buffer(s). */ 3067c478bd9Sstevel@tonic-gate switch (af) { 3077c478bd9Sstevel@tonic-gate case AF_INET6: { 3087c478bd9Sstevel@tonic-gate if (buf4 != NULL) { 3097c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *mergebuf; 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate /* 3127c478bd9Sstevel@tonic-gate * The IPv4 results we have need to be 3137c478bd9Sstevel@tonic-gate * converted to IPv4-mapped addresses, 3147c478bd9Sstevel@tonic-gate * conditionally merged with the IPv6 3157c478bd9Sstevel@tonic-gate * results, and the end result needs to be 3167c478bd9Sstevel@tonic-gate * re-ordered. 3177c478bd9Sstevel@tonic-gate */ 3187c478bd9Sstevel@tonic-gate mergebuf = __IPv6_alloc(NSS_BUFLEN_IPNODES); 3197c478bd9Sstevel@tonic-gate if (mergebuf == NULL) { 3207c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 3217c478bd9Sstevel@tonic-gate goto cleanup; 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate hp = __mapv4tov6(buf4->result, 3247c478bd9Sstevel@tonic-gate ((buf6 != NULL) ? buf6->result : NULL), 3257c478bd9Sstevel@tonic-gate mergebuf, 1); 3267c478bd9Sstevel@tonic-gate if (hp != NULL) 3277c478bd9Sstevel@tonic-gate order_haddrlist_af(AF_INET6, hp->h_addr_list); 3287c478bd9Sstevel@tonic-gate else 3297c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 3307c478bd9Sstevel@tonic-gate free(mergebuf); 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate if (buf4 == NULL && buf6 != NULL) { 3347c478bd9Sstevel@tonic-gate hp = buf6->result; 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate /* 3377c478bd9Sstevel@tonic-gate * We have what we need in buf6, but we may need 3387c478bd9Sstevel@tonic-gate * to filter out some addresses depending on what 3397c478bd9Sstevel@tonic-gate * is being asked for. 3407c478bd9Sstevel@tonic-gate */ 3417c478bd9Sstevel@tonic-gate if (!(ipnode_bits & IPNODE_WANTIPV4)) 3427c478bd9Sstevel@tonic-gate hp = __filter_addresses(AF_INET, buf6->result); 3437c478bd9Sstevel@tonic-gate else if (!(ipnode_bits & IPNODE_WANTIPV6)) 3447c478bd9Sstevel@tonic-gate hp = __filter_addresses(AF_INET6, buf6->result); 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate if (hp == NULL) 3477c478bd9Sstevel@tonic-gate *error_num = NO_ADDRESS; 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate break; 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate case AF_INET: 3547c478bd9Sstevel@tonic-gate /* We could have results in buf6 or buf4, not both */ 3557c478bd9Sstevel@tonic-gate if (buf6 != NULL) { 3567c478bd9Sstevel@tonic-gate /* 3577c478bd9Sstevel@tonic-gate * Extract the IPv4-mapped addresses from buf6 3587c478bd9Sstevel@tonic-gate * into hp. 3597c478bd9Sstevel@tonic-gate */ 3607c478bd9Sstevel@tonic-gate hp = __mappedtov4(buf6->result, error_num); 3617c478bd9Sstevel@tonic-gate } else { 3627c478bd9Sstevel@tonic-gate /* We have what we need in buf4. */ 3637c478bd9Sstevel@tonic-gate hp = buf4->result; 3647c478bd9Sstevel@tonic-gate if (ipnode_bits & IPNODE_LITERAL) { 3657c478bd9Sstevel@tonic-gate /* 3667c478bd9Sstevel@tonic-gate * There is a special case here for literal 3677c478bd9Sstevel@tonic-gate * IPv4 address strings. The hosts 3687c478bd9Sstevel@tonic-gate * front-end sets h_aliases to a one 3697c478bd9Sstevel@tonic-gate * element array containing a single NULL 3707c478bd9Sstevel@tonic-gate * pointer (in ndaddr2hent()), while 3717c478bd9Sstevel@tonic-gate * getipnodebyname() requires h_aliases to 3727c478bd9Sstevel@tonic-gate * be a NULL pointer itself. We're not 3737c478bd9Sstevel@tonic-gate * going to change the front-end since it 3747c478bd9Sstevel@tonic-gate * needs to remain backward compatible for 3757c478bd9Sstevel@tonic-gate * gethostbyname() and friends. Just set 3767c478bd9Sstevel@tonic-gate * h_aliases to NULL here instead. 3777c478bd9Sstevel@tonic-gate */ 3787c478bd9Sstevel@tonic-gate hp->h_aliases = NULL; 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate break; 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate default: 3857c478bd9Sstevel@tonic-gate break; 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate cleanup: 3897c478bd9Sstevel@tonic-gate /* 3907c478bd9Sstevel@tonic-gate * Free the memory we allocated, but make sure we don't free 3917c478bd9Sstevel@tonic-gate * the memory we're returning to the caller. 3927c478bd9Sstevel@tonic-gate */ 3937c478bd9Sstevel@tonic-gate if (buf6 != NULL) { 3947c478bd9Sstevel@tonic-gate if (buf6->result == hp) 3957c478bd9Sstevel@tonic-gate buf6->result = NULL; 3967c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf6); 3977c478bd9Sstevel@tonic-gate } 3987c478bd9Sstevel@tonic-gate if (buf4 != NULL) { 3997c478bd9Sstevel@tonic-gate if (buf4->result == hp) 4007c478bd9Sstevel@tonic-gate buf4->result = NULL; 4017c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf4); 4027c478bd9Sstevel@tonic-gate } 4037c478bd9Sstevel@tonic-gate (void) freenetconfigent(nconf); 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate return (hp); 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate /* 4097c478bd9Sstevel@tonic-gate * This is the IPv6 interface for "gethostbyaddr". 4107c478bd9Sstevel@tonic-gate */ 4117c478bd9Sstevel@tonic-gate struct hostent * 4127c478bd9Sstevel@tonic-gate getipnodebyaddr(const void *src, size_t len, int type, int *error_num) 4137c478bd9Sstevel@tonic-gate { 4147c478bd9Sstevel@tonic-gate struct in6_addr *addr6 = 0; 4157c478bd9Sstevel@tonic-gate struct in_addr *addr4 = 0; 4167c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *buf = 0; 4177c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *res = 0; 4187c478bd9Sstevel@tonic-gate struct netconfig *nconf; 4197c478bd9Sstevel@tonic-gate struct hostent *hp = 0; 4207c478bd9Sstevel@tonic-gate struct nss_netdirbyaddr_in nssin; 4217c478bd9Sstevel@tonic-gate union nss_netdirbyaddr_out nssout; 4227c478bd9Sstevel@tonic-gate int neterr; 4237c478bd9Sstevel@tonic-gate char tmpbuf[64]; 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate if (type == AF_INET6) { 4267c478bd9Sstevel@tonic-gate if ((addr6 = (struct in6_addr *)src) == NULL) { 4277c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 4287c478bd9Sstevel@tonic-gate return (NULL); 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate } else if (type == AF_INET) { 4317c478bd9Sstevel@tonic-gate if ((addr4 = (struct in_addr *)src) == NULL) { 4327c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 4337c478bd9Sstevel@tonic-gate return (NULL); 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate } else { 4367c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 4377c478bd9Sstevel@tonic-gate return (NULL); 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate /* 4407c478bd9Sstevel@tonic-gate * Specific case: query for "::" 4417c478bd9Sstevel@tonic-gate */ 4427c478bd9Sstevel@tonic-gate if (type == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(addr6)) { 4437c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 4447c478bd9Sstevel@tonic-gate return (NULL); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate /* 4477c478bd9Sstevel@tonic-gate * Step 1: IPv4-mapped address or IPv4 Compat 4487c478bd9Sstevel@tonic-gate */ 4497c478bd9Sstevel@tonic-gate if ((type == AF_INET6 && len == 16) && 4507c478bd9Sstevel@tonic-gate ((IN6_IS_ADDR_V4MAPPED(addr6)) || 4517c478bd9Sstevel@tonic-gate (IN6_IS_ADDR_V4COMPAT(addr6)))) { 4527c478bd9Sstevel@tonic-gate if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 4537c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 4547c478bd9Sstevel@tonic-gate return (NULL); 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 4577c478bd9Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 4587c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 4597c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 4607c478bd9Sstevel@tonic-gate return (NULL); 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 4637c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4COMPAT(addr6)) { 46461961e0fSrobinson (void) memcpy(tmpbuf, addr6, sizeof (*addr6)); 4657c478bd9Sstevel@tonic-gate tmpbuf[10] = 0xffU; 4667c478bd9Sstevel@tonic-gate tmpbuf[11] = 0xffU; 4677c478bd9Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)tmpbuf; 4687c478bd9Sstevel@tonic-gate } else { 4697c478bd9Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)addr6; 4707c478bd9Sstevel@tonic-gate } 4717c478bd9Sstevel@tonic-gate nssin.arg.nss.host.len = sizeof (struct in6_addr); 4727c478bd9Sstevel@tonic-gate nssin.arg.nss.host.type = AF_INET6; 4737c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buf = buf->buffer; 4747c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf->buflen; 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate nssout.nss.host.hent = buf->result; 4777c478bd9Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 4787c478bd9Sstevel@tonic-gate /* 4797c478bd9Sstevel@tonic-gate * We pass in nconf and let the implementation of the 4807c478bd9Sstevel@tonic-gate * long-named func decide whether to use the switch based on 4817c478bd9Sstevel@tonic-gate * nc_nlookups. 4827c478bd9Sstevel@tonic-gate */ 4837c478bd9Sstevel@tonic-gate neterr = 4847c478bd9Sstevel@tonic-gate _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout); 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate (void) freenetconfigent(nconf); 4877c478bd9Sstevel@tonic-gate if (neterr != ND_OK) { 4887c478bd9Sstevel@tonic-gate /* Failover case, try hosts db for v4 address */ 4897c478bd9Sstevel@tonic-gate if (!gethostbyaddr_r(((char *)addr6) + 12, 4907c478bd9Sstevel@tonic-gate sizeof (in_addr_t), AF_INET, buf->result, 4917c478bd9Sstevel@tonic-gate buf->buffer, buf->buflen, error_num)) { 4927c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 4937c478bd9Sstevel@tonic-gate return (NULL); 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate /* Found one, now format it into mapped/compat addr */ 4967c478bd9Sstevel@tonic-gate if ((res = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 4977c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 4987c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 4997c478bd9Sstevel@tonic-gate return (NULL); 5007c478bd9Sstevel@tonic-gate } 5017c478bd9Sstevel@tonic-gate /* Convert IPv4 to mapped/compat address w/name */ 5027c478bd9Sstevel@tonic-gate hp = res->result; 50361961e0fSrobinson (void) __mapv4tov6(buf->result, 0, res, 5047c478bd9Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED(addr6)); 5057c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 5067c478bd9Sstevel@tonic-gate free(res); 5077c478bd9Sstevel@tonic-gate return (hp); 5087c478bd9Sstevel@tonic-gate } 5097c478bd9Sstevel@tonic-gate /* 5107c478bd9Sstevel@tonic-gate * At this point, we'll have a v4mapped hostent. If that's 5117c478bd9Sstevel@tonic-gate * what was passed in, just return. If the request was a compat, 5127c478bd9Sstevel@tonic-gate * twiggle the two bytes to make the mapped address a compat. 5137c478bd9Sstevel@tonic-gate */ 5147c478bd9Sstevel@tonic-gate hp = buf->result; 5157c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4COMPAT(addr6)) { 51661961e0fSrobinson /* LINTED pointer cast */ 5177c478bd9Sstevel@tonic-gate addr6 = (struct in6_addr *)hp->h_addr_list[0]; 5187c478bd9Sstevel@tonic-gate addr6->s6_addr[10] = 0; 5197c478bd9Sstevel@tonic-gate addr6->s6_addr[11] = 0; 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate free(buf); 5227c478bd9Sstevel@tonic-gate return (hp); 5237c478bd9Sstevel@tonic-gate } 5247c478bd9Sstevel@tonic-gate /* 5257c478bd9Sstevel@tonic-gate * Step 2: AF_INET, v4 lookup. Since we're going to search the 5267c478bd9Sstevel@tonic-gate * ipnodes (v6) path first, we need to treat this as a v4mapped 5277c478bd9Sstevel@tonic-gate * address. nscd(1m) caches v4 from ipnodes as mapped v6's. The 5287c478bd9Sstevel@tonic-gate * switch backend knows to lookup v4's (not v4mapped) from the 5297c478bd9Sstevel@tonic-gate * name services. 5307c478bd9Sstevel@tonic-gate */ 5317c478bd9Sstevel@tonic-gate if (type == AF_INET) { 5327c478bd9Sstevel@tonic-gate struct in6_addr v4mapbuf; 5337c478bd9Sstevel@tonic-gate addr6 = &v4mapbuf; 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(addr4, addr6); 5367c478bd9Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 5377c478bd9Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 5387c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 5397c478bd9Sstevel@tonic-gate return (NULL); 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 5427c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 5437c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 5447c478bd9Sstevel@tonic-gate return (NULL); 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 5477c478bd9Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)addr6; 5487c478bd9Sstevel@tonic-gate nssin.arg.nss.host.len = sizeof (struct in6_addr); 5497c478bd9Sstevel@tonic-gate nssin.arg.nss.host.type = AF_INET6; 5507c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buf = buf->buffer; 5517c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf->buflen; 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate nssout.nss.host.hent = buf->result; 5547c478bd9Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 5557c478bd9Sstevel@tonic-gate /* 5567c478bd9Sstevel@tonic-gate * We pass in nconf and let the implementation of the 5577c478bd9Sstevel@tonic-gate * long-named func decide whether to use the switch based on 5587c478bd9Sstevel@tonic-gate * nc_nlookups. 5597c478bd9Sstevel@tonic-gate */ 5607c478bd9Sstevel@tonic-gate neterr = 5617c478bd9Sstevel@tonic-gate _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout); 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate (void) freenetconfigent(nconf); 5647c478bd9Sstevel@tonic-gate if (neterr != ND_OK) { 5657c478bd9Sstevel@tonic-gate /* Failover case, try hosts db for v4 address */ 5667c478bd9Sstevel@tonic-gate hp = buf->result; 5677c478bd9Sstevel@tonic-gate if (!gethostbyaddr_r(src, len, type, buf->result, 5687c478bd9Sstevel@tonic-gate buf->buffer, buf->buflen, error_num)) { 5697c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 5707c478bd9Sstevel@tonic-gate return (NULL); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate free(buf); 5737c478bd9Sstevel@tonic-gate return (hp); 5747c478bd9Sstevel@tonic-gate } 5757c478bd9Sstevel@tonic-gate if ((hp = __mappedtov4(buf->result, error_num)) == NULL) { 5767c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 5777c478bd9Sstevel@tonic-gate return (NULL); 5787c478bd9Sstevel@tonic-gate } 5797c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 5807c478bd9Sstevel@tonic-gate return (hp); 5817c478bd9Sstevel@tonic-gate } 5827c478bd9Sstevel@tonic-gate /* 5837c478bd9Sstevel@tonic-gate * Step 3: AF_INET6, plain vanilla v6 getipnodebyaddr() call. 5847c478bd9Sstevel@tonic-gate */ 5857c478bd9Sstevel@tonic-gate if (type == AF_INET6) { 5867c478bd9Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 5877c478bd9Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 5887c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 5897c478bd9Sstevel@tonic-gate return (NULL); 5907c478bd9Sstevel@tonic-gate } 5917c478bd9Sstevel@tonic-gate if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 5927c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 5937c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 5947c478bd9Sstevel@tonic-gate return (NULL); 5957c478bd9Sstevel@tonic-gate } 5967c478bd9Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 5977c478bd9Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)addr6; 5987c478bd9Sstevel@tonic-gate nssin.arg.nss.host.len = len; 5997c478bd9Sstevel@tonic-gate nssin.arg.nss.host.type = type; 6007c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buf = buf->buffer; 6017c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf->buflen; 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate nssout.nss.host.hent = buf->result; 6047c478bd9Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 6057c478bd9Sstevel@tonic-gate /* 6067c478bd9Sstevel@tonic-gate * We pass in nconf and let the implementation of the 6077c478bd9Sstevel@tonic-gate * long-named func decide whether to use the switch based on 6087c478bd9Sstevel@tonic-gate * nc_nlookups. 6097c478bd9Sstevel@tonic-gate */ 6107c478bd9Sstevel@tonic-gate neterr = 6117c478bd9Sstevel@tonic-gate _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout); 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate (void) freenetconfigent(nconf); 6147c478bd9Sstevel@tonic-gate if (neterr != ND_OK) { 6157c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 6167c478bd9Sstevel@tonic-gate return (NULL); 6177c478bd9Sstevel@tonic-gate } 6187c478bd9Sstevel@tonic-gate free(buf); 6197c478bd9Sstevel@tonic-gate return (nssout.nss.host.hent); 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate /* 6227c478bd9Sstevel@tonic-gate * If we got here, unknown type. 6237c478bd9Sstevel@tonic-gate */ 6247c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 6257c478bd9Sstevel@tonic-gate return (NULL); 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate void 6297c478bd9Sstevel@tonic-gate freehostent(struct hostent *hent) 6307c478bd9Sstevel@tonic-gate { 6317c478bd9Sstevel@tonic-gate free(hent); 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate static int 6357c478bd9Sstevel@tonic-gate __ai_addrconfig(int af) 6367c478bd9Sstevel@tonic-gate { 6377c478bd9Sstevel@tonic-gate struct lifnum lifn; 6387c478bd9Sstevel@tonic-gate hrtime_t now, *then; 6397c478bd9Sstevel@tonic-gate static hrtime_t then4, then6; /* the last time we updated ifnum# */ 6407c478bd9Sstevel@tonic-gate static int ifnum4 = -1, ifnum6 = -1; 6417c478bd9Sstevel@tonic-gate int *num; 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate switch (af) { 6447c478bd9Sstevel@tonic-gate case AF_INET: 6457c478bd9Sstevel@tonic-gate num = &ifnum4; 6467c478bd9Sstevel@tonic-gate then = &then4; 6477c478bd9Sstevel@tonic-gate break; 6487c478bd9Sstevel@tonic-gate case AF_INET6: 6497c478bd9Sstevel@tonic-gate num = &ifnum6; 6507c478bd9Sstevel@tonic-gate then = &then6; 6517c478bd9Sstevel@tonic-gate break; 6527c478bd9Sstevel@tonic-gate default: 6537c478bd9Sstevel@tonic-gate return (0); 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate /* 6577c478bd9Sstevel@tonic-gate * We don't need to check this every time someone does a name 6587c478bd9Sstevel@tonic-gate * lookup. Do it every IFNUM_TIMEOUT for each address family. 6597c478bd9Sstevel@tonic-gate * 6607c478bd9Sstevel@tonic-gate * There's no need to protect all of this with a lock. The 6617c478bd9Sstevel@tonic-gate * worst that can happen is that we update the interface count 6627c478bd9Sstevel@tonic-gate * twice instead of once. That's no big deal. 6637c478bd9Sstevel@tonic-gate */ 6647c478bd9Sstevel@tonic-gate now = gethrtime(); 6657c478bd9Sstevel@tonic-gate if (*num == -1 || ((now - *then) >= IFNUM_TIMEOUT)) { 6667c478bd9Sstevel@tonic-gate lifn.lifn_family = af; 6677c478bd9Sstevel@tonic-gate /* 6687c478bd9Sstevel@tonic-gate * We want to determine if this machine knows anything 6697c478bd9Sstevel@tonic-gate * at all about the address family; the status of the 6707c478bd9Sstevel@tonic-gate * interface is less important. Hence, set 6717c478bd9Sstevel@tonic-gate * 'lifn_flags' to zero. 6727c478bd9Sstevel@tonic-gate */ 6737c478bd9Sstevel@tonic-gate lifn.lifn_flags = 0; 6747c478bd9Sstevel@tonic-gate if (nss_ioctl(af, SIOCGLIFNUM, &lifn) < 0) 6757c478bd9Sstevel@tonic-gate return (-1); 6767c478bd9Sstevel@tonic-gate 6777c478bd9Sstevel@tonic-gate *num = lifn.lifn_count; 6787c478bd9Sstevel@tonic-gate *then = now; 6797c478bd9Sstevel@tonic-gate } 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate return (*num); 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate /* 6857c478bd9Sstevel@tonic-gate * This routine will either convert an IPv4 address to a mapped or compat 6867c478bd9Sstevel@tonic-gate * IPv6 (if he6 == NULL) or merge IPv6 (he6) addresses with mapped 6877c478bd9Sstevel@tonic-gate * v4 (he4) addresses. In either case, the results are returned in res. 6887c478bd9Sstevel@tonic-gate * Caller must provide all buffers. 6897c478bd9Sstevel@tonic-gate * Inputs: 6907c478bd9Sstevel@tonic-gate * he4 pointer to IPv4 buffer 6917c478bd9Sstevel@tonic-gate * he6 pointer to IPv6 buffer (NULL if not merging v4/v6 6927c478bd9Sstevel@tonic-gate * res pointer to results buffer 6937c478bd9Sstevel@tonic-gate * mapped mapped == 1, map IPv4 : mapped == 0, compat IPv4 6947c478bd9Sstevel@tonic-gate * mapped flag is ignored if he6 != NULL 6957c478bd9Sstevel@tonic-gate * 6967c478bd9Sstevel@tonic-gate * The results are packed into the res->buffer as follows: 6977c478bd9Sstevel@tonic-gate * <--------------- buffer + buflen --------------------------------------> 6987c478bd9Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 6997c478bd9Sstevel@tonic-gate * | pointers vector | pointers vector | aliases grow | addresses grow | 7007c478bd9Sstevel@tonic-gate * | for addresses | for aliases | | | 7017c478bd9Sstevel@tonic-gate * | this way -> | this way -> | <- this way |<- this way | 7027c478bd9Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 7037c478bd9Sstevel@tonic-gate * | grows in PASS 1 | grows in PASS2 | grows in PASS2 | grows in PASS 1| 7047c478bd9Sstevel@tonic-gate */ 7057c478bd9Sstevel@tonic-gate static struct hostent * 7067c478bd9Sstevel@tonic-gate __mapv4tov6(struct hostent *he4, struct hostent *he6, nss_XbyY_buf_t *res, 7077c478bd9Sstevel@tonic-gate int mapped) 7087c478bd9Sstevel@tonic-gate { 7097c478bd9Sstevel@tonic-gate char *buffer, *limit; 7107c478bd9Sstevel@tonic-gate int buflen = res->buflen; 7117c478bd9Sstevel@tonic-gate struct in6_addr *addr6p; 7127c478bd9Sstevel@tonic-gate char *buff_locp; 7137c478bd9Sstevel@tonic-gate struct hostent *host; 7147c478bd9Sstevel@tonic-gate int count = 0, len, i; 7157c478bd9Sstevel@tonic-gate char *h_namep; 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate if (he4 == NULL || res == NULL) { 7187c478bd9Sstevel@tonic-gate return (NULL); 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate limit = res->buffer + buflen; 7217c478bd9Sstevel@tonic-gate host = (struct hostent *)res->result; 7227c478bd9Sstevel@tonic-gate buffer = res->buffer; 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in6_addr)); 7257c478bd9Sstevel@tonic-gate host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **)); 7267c478bd9Sstevel@tonic-gate if ((char *)host->h_addr_list >= limit || 7277c478bd9Sstevel@tonic-gate buff_locp <= (char *)host->h_addr_list) { 7287c478bd9Sstevel@tonic-gate return (NULL); 7297c478bd9Sstevel@tonic-gate } 7307c478bd9Sstevel@tonic-gate if (he6 == NULL) { 7317c478bd9Sstevel@tonic-gate /* 7327c478bd9Sstevel@tonic-gate * If he6==NULL, map the v4 address into the v6 address format. 7337c478bd9Sstevel@tonic-gate * This is used for getipnodebyaddr() (single address, mapped or 7347c478bd9Sstevel@tonic-gate * compatible) or for v4 mapped for getipnodebyname(), which 7357c478bd9Sstevel@tonic-gate * could be multiple addresses. This could also be a literal 7367c478bd9Sstevel@tonic-gate * address string, which is why there is a inet_addr() call. 7377c478bd9Sstevel@tonic-gate */ 7387c478bd9Sstevel@tonic-gate for (i = 0; he4->h_addr_list[i] != NULL; i++) { 7397c478bd9Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 7407c478bd9Sstevel@tonic-gate if (buff_locp <= 7417c478bd9Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) { 7427c478bd9Sstevel@tonic-gate /* 7437c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 7447c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 7457c478bd9Sstevel@tonic-gate */ 7467c478bd9Sstevel@tonic-gate return (NULL); 7477c478bd9Sstevel@tonic-gate } 74861961e0fSrobinson /* LINTED pointer cast */ 7497c478bd9Sstevel@tonic-gate addr6p = (struct in6_addr *)buff_locp; 7507c478bd9Sstevel@tonic-gate host->h_addr_list[count] = (char *)addr6p; 7517c478bd9Sstevel@tonic-gate bzero(addr6p->s6_addr, sizeof (struct in6_addr)); 7527c478bd9Sstevel@tonic-gate if (mapped) { 7537c478bd9Sstevel@tonic-gate addr6p->s6_addr[10] = 0xff; 7547c478bd9Sstevel@tonic-gate addr6p->s6_addr[11] = 0xff; 7557c478bd9Sstevel@tonic-gate } 7567c478bd9Sstevel@tonic-gate bcopy((char *)he4->h_addr_list[i], 7577c478bd9Sstevel@tonic-gate &addr6p->s6_addr[12], sizeof (struct in_addr)); 7587c478bd9Sstevel@tonic-gate ++count; 7597c478bd9Sstevel@tonic-gate } 7607c478bd9Sstevel@tonic-gate /* 7617c478bd9Sstevel@tonic-gate * Set last array element to NULL and add cname as first alias 7627c478bd9Sstevel@tonic-gate */ 7637c478bd9Sstevel@tonic-gate host->h_addr_list[count] = NULL; 7647c478bd9Sstevel@tonic-gate host->h_aliases = host->h_addr_list + count + 1; 7657c478bd9Sstevel@tonic-gate count = 0; 7667c478bd9Sstevel@tonic-gate if ((int)(inet_addr(he4->h_name)) != -1) { 7677c478bd9Sstevel@tonic-gate /* 7687c478bd9Sstevel@tonic-gate * Literal address string, since we're mapping, we need the IPv6 7697c478bd9Sstevel@tonic-gate * V4 mapped literal address string for h_name. 7707c478bd9Sstevel@tonic-gate */ 7717c478bd9Sstevel@tonic-gate char tmpstr[128]; 77261961e0fSrobinson (void) inet_ntop(AF_INET6, host->h_addr_list[0], tmpstr, 7737c478bd9Sstevel@tonic-gate sizeof (tmpstr)); 7747c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(tmpstr) + 1); 7757c478bd9Sstevel@tonic-gate h_namep = tmpstr; 7767c478bd9Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 7777c478bd9Sstevel@tonic-gate return (NULL); 7787c478bd9Sstevel@tonic-gate bcopy(h_namep, buff_locp, len); 7797c478bd9Sstevel@tonic-gate host->h_name = buff_locp; 7807c478bd9Sstevel@tonic-gate host->h_aliases = NULL; /* no aliases for literal */ 7817c478bd9Sstevel@tonic-gate host->h_length = sizeof (struct in6_addr); 7827c478bd9Sstevel@tonic-gate host->h_addrtype = AF_INET6; 7837c478bd9Sstevel@tonic-gate return (host); /* we're done, return result */ 7847c478bd9Sstevel@tonic-gate } 7857c478bd9Sstevel@tonic-gate /* 7867c478bd9Sstevel@tonic-gate * Not a literal address string, so just copy h_name. 7877c478bd9Sstevel@tonic-gate */ 7887c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he4->h_name) + 1); 7897c478bd9Sstevel@tonic-gate h_namep = he4->h_name; 7907c478bd9Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 7917c478bd9Sstevel@tonic-gate return (NULL); 7927c478bd9Sstevel@tonic-gate bcopy(h_namep, buff_locp, len); 7937c478bd9Sstevel@tonic-gate host->h_name = buff_locp; 7947c478bd9Sstevel@tonic-gate /* 7957c478bd9Sstevel@tonic-gate * Pass 2 (IPv4 aliases): 7967c478bd9Sstevel@tonic-gate */ 7977c478bd9Sstevel@tonic-gate for (i = 0; he4->h_aliases[i] != NULL; i++) { 7987c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he4->h_aliases[i]) + 1); 7997c478bd9Sstevel@tonic-gate if (buff_locp <= 8007c478bd9Sstevel@tonic-gate (char *)&(host->h_aliases[count + 1])) { 8017c478bd9Sstevel@tonic-gate /* 8027c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 8037c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 8047c478bd9Sstevel@tonic-gate */ 8057c478bd9Sstevel@tonic-gate return (NULL); 8067c478bd9Sstevel@tonic-gate } 8077c478bd9Sstevel@tonic-gate host->h_aliases[count] = buff_locp; 8087c478bd9Sstevel@tonic-gate bcopy((char *)he4->h_aliases[i], buff_locp, len); 8097c478bd9Sstevel@tonic-gate ++count; 8107c478bd9Sstevel@tonic-gate } 8117c478bd9Sstevel@tonic-gate host->h_aliases[count] = NULL; 8127c478bd9Sstevel@tonic-gate host->h_length = sizeof (struct in6_addr); 8137c478bd9Sstevel@tonic-gate host->h_addrtype = AF_INET6; 8147c478bd9Sstevel@tonic-gate return (host); 8157c478bd9Sstevel@tonic-gate } else { 8167c478bd9Sstevel@tonic-gate /* 8177c478bd9Sstevel@tonic-gate * Merge IPv4 mapped addresses with IPv6 addresses. The 8187c478bd9Sstevel@tonic-gate * IPv6 address will go in first, followed by the v4 mapped. 8197c478bd9Sstevel@tonic-gate * 8207c478bd9Sstevel@tonic-gate * Pass 1 (IPv6 addresses): 8217c478bd9Sstevel@tonic-gate */ 8227c478bd9Sstevel@tonic-gate for (i = 0; he6->h_addr_list[i] != NULL; i++) { 8237c478bd9Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 8247c478bd9Sstevel@tonic-gate if (buff_locp <= 8257c478bd9Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) { 8267c478bd9Sstevel@tonic-gate /* 8277c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 8287c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 8297c478bd9Sstevel@tonic-gate */ 8307c478bd9Sstevel@tonic-gate return (NULL); 8317c478bd9Sstevel@tonic-gate } 8327c478bd9Sstevel@tonic-gate host->h_addr_list[count] = buff_locp; 8337c478bd9Sstevel@tonic-gate bcopy((char *)he6->h_addr_list[i], buff_locp, 8347c478bd9Sstevel@tonic-gate sizeof (struct in6_addr)); 8357c478bd9Sstevel@tonic-gate ++count; 8367c478bd9Sstevel@tonic-gate } 8377c478bd9Sstevel@tonic-gate /* 8387c478bd9Sstevel@tonic-gate * Pass 1 (IPv4 mapped addresses): 8397c478bd9Sstevel@tonic-gate */ 8407c478bd9Sstevel@tonic-gate for (i = 0; he4->h_addr_list[i] != NULL; i++) { 8417c478bd9Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 8427c478bd9Sstevel@tonic-gate if (buff_locp <= 8437c478bd9Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) { 8447c478bd9Sstevel@tonic-gate /* 8457c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 8467c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 8477c478bd9Sstevel@tonic-gate */ 8487c478bd9Sstevel@tonic-gate return (NULL); 8497c478bd9Sstevel@tonic-gate } 85061961e0fSrobinson /* LINTED pointer cast */ 8517c478bd9Sstevel@tonic-gate addr6p = (struct in6_addr *)buff_locp; 8527c478bd9Sstevel@tonic-gate host->h_addr_list[count] = (char *)addr6p; 8537c478bd9Sstevel@tonic-gate bzero(addr6p->s6_addr, sizeof (struct in6_addr)); 8547c478bd9Sstevel@tonic-gate addr6p->s6_addr[10] = 0xff; 8557c478bd9Sstevel@tonic-gate addr6p->s6_addr[11] = 0xff; 8567c478bd9Sstevel@tonic-gate bcopy(he4->h_addr_list[i], &addr6p->s6_addr[12], 8577c478bd9Sstevel@tonic-gate sizeof (struct in_addr)); 8587c478bd9Sstevel@tonic-gate ++count; 8597c478bd9Sstevel@tonic-gate } 8607c478bd9Sstevel@tonic-gate /* 8617c478bd9Sstevel@tonic-gate * Pass 2 (IPv6 aliases, host name first). We start h_aliases 8627c478bd9Sstevel@tonic-gate * one after where h_addr_list array ended. This is where cname 8637c478bd9Sstevel@tonic-gate * is put, followed by all aliases. Reset count to 0, for index 8647c478bd9Sstevel@tonic-gate * in the h_aliases array. 8657c478bd9Sstevel@tonic-gate */ 8667c478bd9Sstevel@tonic-gate host->h_addr_list[count] = NULL; 8677c478bd9Sstevel@tonic-gate host->h_aliases = host->h_addr_list + count + 1; 8687c478bd9Sstevel@tonic-gate count = 0; 8697c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he6->h_name) + 1); 8707c478bd9Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 8717c478bd9Sstevel@tonic-gate return (NULL); 8727c478bd9Sstevel@tonic-gate bcopy(he6->h_name, buff_locp, len); 8737c478bd9Sstevel@tonic-gate host->h_name = buff_locp; 8747c478bd9Sstevel@tonic-gate for (i = 0; he6->h_aliases[i] != NULL; i++) { 8757c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he6->h_aliases[i]) + 1); 8767c478bd9Sstevel@tonic-gate if (buff_locp <= 8777c478bd9Sstevel@tonic-gate (char *)&(host->h_aliases[count + 1])) { 8787c478bd9Sstevel@tonic-gate /* 8797c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 8807c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 8817c478bd9Sstevel@tonic-gate */ 8827c478bd9Sstevel@tonic-gate return (NULL); 8837c478bd9Sstevel@tonic-gate } 8847c478bd9Sstevel@tonic-gate host->h_aliases[count] = buff_locp; 8857c478bd9Sstevel@tonic-gate bcopy((char *)he6->h_aliases[i], buff_locp, len); 8867c478bd9Sstevel@tonic-gate ++count; 8877c478bd9Sstevel@tonic-gate } 8887c478bd9Sstevel@tonic-gate /* 8897c478bd9Sstevel@tonic-gate * Pass 2 (IPv4 aliases): 8907c478bd9Sstevel@tonic-gate */ 8917c478bd9Sstevel@tonic-gate for (i = 0; he4->h_aliases[i] != NULL; i++) { 8927c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he4->h_aliases[i]) + 1); 8937c478bd9Sstevel@tonic-gate if (buff_locp <= 8947c478bd9Sstevel@tonic-gate (char *)&(host->h_aliases[count + 1])) { 8957c478bd9Sstevel@tonic-gate /* 8967c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 8977c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 8987c478bd9Sstevel@tonic-gate */ 8997c478bd9Sstevel@tonic-gate return (NULL); 9007c478bd9Sstevel@tonic-gate } 9017c478bd9Sstevel@tonic-gate host->h_aliases[count] = buff_locp; 9027c478bd9Sstevel@tonic-gate bcopy((char *)he4->h_aliases[i], buff_locp, len); 9037c478bd9Sstevel@tonic-gate ++count; 9047c478bd9Sstevel@tonic-gate } 9057c478bd9Sstevel@tonic-gate host->h_aliases[count] = NULL; 9067c478bd9Sstevel@tonic-gate host->h_length = sizeof (struct in6_addr); 9077c478bd9Sstevel@tonic-gate host->h_addrtype = AF_INET6; 9087c478bd9Sstevel@tonic-gate return (host); 9097c478bd9Sstevel@tonic-gate } 9107c478bd9Sstevel@tonic-gate } 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate /* 9137c478bd9Sstevel@tonic-gate * This routine will convert a mapped v4 hostent (AF_INET6) to a 9147c478bd9Sstevel@tonic-gate * AF_INET hostent. If no mapped addrs found, then a NULL is returned. 9157c478bd9Sstevel@tonic-gate * If mapped addrs found, then a new buffer is alloc'd and all the v4 mapped 9167c478bd9Sstevel@tonic-gate * addresses are extracted and copied to it. On sucess, a pointer to a new 9177c478bd9Sstevel@tonic-gate * hostent is returned. 9187c478bd9Sstevel@tonic-gate * There are two possible errors in which case a NULL is returned. 9197c478bd9Sstevel@tonic-gate * One of two error codes are returned: 9207c478bd9Sstevel@tonic-gate * 9217c478bd9Sstevel@tonic-gate * NO_RECOVERY - a malloc failed or the like for which there's no recovery. 9227c478bd9Sstevel@tonic-gate * NO_ADDRESS - after filtering all the v4, there was nothing left! 9237c478bd9Sstevel@tonic-gate * 9247c478bd9Sstevel@tonic-gate * Inputs: 9257c478bd9Sstevel@tonic-gate * he pointer to hostent with mapped v4 addresses 9267c478bd9Sstevel@tonic-gate * filter_error pointer to return error code 9277c478bd9Sstevel@tonic-gate * Return: 9287c478bd9Sstevel@tonic-gate * pointer to a malloc'd hostent with v4 addresses. 9297c478bd9Sstevel@tonic-gate * 9307c478bd9Sstevel@tonic-gate * The results are packed into the res->buffer as follows: 9317c478bd9Sstevel@tonic-gate * <--------------- buffer + buflen --------------------------------------> 9327c478bd9Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 9337c478bd9Sstevel@tonic-gate * | pointers vector | pointers vector | aliases grow | addresses grow | 9347c478bd9Sstevel@tonic-gate * | for addresses | for aliases | | | 9357c478bd9Sstevel@tonic-gate * | this way -> | this way -> | <- this way |<- this way | 9367c478bd9Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 9377c478bd9Sstevel@tonic-gate * | grows in PASS 1 | grows in PASS2 | grows in PASS2 | grows in PASS 1| 9387c478bd9Sstevel@tonic-gate */ 9397c478bd9Sstevel@tonic-gate struct hostent * 9407c478bd9Sstevel@tonic-gate __mappedtov4(struct hostent *he, int *extract_error) 9417c478bd9Sstevel@tonic-gate { 9427c478bd9Sstevel@tonic-gate char *buffer, *limit; 9437c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *res; 9447c478bd9Sstevel@tonic-gate int buflen = NSS_BUFLEN_HOSTS; 9457c478bd9Sstevel@tonic-gate struct in_addr *addr4p; 9467c478bd9Sstevel@tonic-gate char *buff_locp; 9477c478bd9Sstevel@tonic-gate struct hostent *host; 9487c478bd9Sstevel@tonic-gate int count = 0, len, i; 9497c478bd9Sstevel@tonic-gate char *h_namep; 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate if (he == NULL) { 9527c478bd9Sstevel@tonic-gate *extract_error = NO_ADDRESS; 9537c478bd9Sstevel@tonic-gate return (NULL); 9547c478bd9Sstevel@tonic-gate } 9557c478bd9Sstevel@tonic-gate if ((__find_mapped(he, 0)) == 0) { 9567c478bd9Sstevel@tonic-gate *extract_error = NO_ADDRESS; 9577c478bd9Sstevel@tonic-gate return (NULL); 9587c478bd9Sstevel@tonic-gate } 9597c478bd9Sstevel@tonic-gate if ((res = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == 0) { 9607c478bd9Sstevel@tonic-gate *extract_error = NO_RECOVERY; 9617c478bd9Sstevel@tonic-gate return (NULL); 9627c478bd9Sstevel@tonic-gate } 9637c478bd9Sstevel@tonic-gate limit = res->buffer + buflen; 9647c478bd9Sstevel@tonic-gate host = (struct hostent *)res->result; 9657c478bd9Sstevel@tonic-gate buffer = res->buffer; 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in_addr)); 9687c478bd9Sstevel@tonic-gate host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **)); 9697c478bd9Sstevel@tonic-gate if ((char *)host->h_addr_list >= limit || 9707c478bd9Sstevel@tonic-gate buff_locp <= (char *)host->h_addr_list) 9717c478bd9Sstevel@tonic-gate goto cleanup; 9727c478bd9Sstevel@tonic-gate /* 9737c478bd9Sstevel@tonic-gate * "Unmap" the v4 mapped address(es) into a v4 hostent format. 9747c478bd9Sstevel@tonic-gate * This is used for getipnodebyaddr() (single address) or for 9757c478bd9Sstevel@tonic-gate * v4 mapped for getipnodebyname(), which could be multiple 9767c478bd9Sstevel@tonic-gate * addresses. This could also be a literal address string, 9777c478bd9Sstevel@tonic-gate * which is why there is a inet_addr() call. 9787c478bd9Sstevel@tonic-gate */ 9797c478bd9Sstevel@tonic-gate for (i = 0; he->h_addr_list[i] != NULL; i++) { 98061961e0fSrobinson /* LINTED pointer cast */ 9817c478bd9Sstevel@tonic-gate if (!IN6_IS_ADDR_V4MAPPED((struct in6_addr *) 9827c478bd9Sstevel@tonic-gate he->h_addr_list[i])) 9837c478bd9Sstevel@tonic-gate continue; 9847c478bd9Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 9857c478bd9Sstevel@tonic-gate /* 9867c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 9877c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 9887c478bd9Sstevel@tonic-gate */ 9897c478bd9Sstevel@tonic-gate if (buff_locp <= 9907c478bd9Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) 9917c478bd9Sstevel@tonic-gate goto cleanup; 99261961e0fSrobinson /* LINTED pointer cast */ 9937c478bd9Sstevel@tonic-gate addr4p = (struct in_addr *)buff_locp; 9947c478bd9Sstevel@tonic-gate host->h_addr_list[count] = (char *)addr4p; 9957c478bd9Sstevel@tonic-gate bzero((char *)&addr4p->s_addr, 9967c478bd9Sstevel@tonic-gate sizeof (struct in_addr)); 99761961e0fSrobinson /* LINTED pointer cast */ 9987c478bd9Sstevel@tonic-gate IN6_V4MAPPED_TO_INADDR( 999*5b79143cSGirish Moodalbail (struct in6_addr *)he->h_addr_list[i], addr4p); 10007c478bd9Sstevel@tonic-gate ++count; 10017c478bd9Sstevel@tonic-gate } 10027c478bd9Sstevel@tonic-gate /* 10037c478bd9Sstevel@tonic-gate * Set last array element to NULL and add cname as first alias 10047c478bd9Sstevel@tonic-gate */ 10057c478bd9Sstevel@tonic-gate host->h_addr_list[count] = NULL; 10067c478bd9Sstevel@tonic-gate host->h_aliases = host->h_addr_list + count + 1; 10077c478bd9Sstevel@tonic-gate count = 0; 10087c478bd9Sstevel@tonic-gate /* Copy official host name */ 10097c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he->h_name) + 1); 10107c478bd9Sstevel@tonic-gate h_namep = he->h_name; 10117c478bd9Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 10127c478bd9Sstevel@tonic-gate goto cleanup; 10137c478bd9Sstevel@tonic-gate bcopy(h_namep, buff_locp, len); 10147c478bd9Sstevel@tonic-gate host->h_name = buff_locp; 10157c478bd9Sstevel@tonic-gate /* 10167c478bd9Sstevel@tonic-gate * Pass 2 (IPv4 aliases): 10177c478bd9Sstevel@tonic-gate */ 1018*5b79143cSGirish Moodalbail if (he->h_aliases != NULL) { 10197c478bd9Sstevel@tonic-gate for (i = 0; he->h_aliases[i] != NULL; i++) { 10207c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he->h_aliases[i]) + 1); 10217c478bd9Sstevel@tonic-gate /* 10227c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 10237c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 10247c478bd9Sstevel@tonic-gate */ 10257c478bd9Sstevel@tonic-gate if (buff_locp <= 10267c478bd9Sstevel@tonic-gate (char *)&(host->h_aliases[count + 1])) 10277c478bd9Sstevel@tonic-gate goto cleanup; 10287c478bd9Sstevel@tonic-gate host->h_aliases[count] = buff_locp; 10297c478bd9Sstevel@tonic-gate bcopy((char *)he->h_aliases[i], buff_locp, len); 10307c478bd9Sstevel@tonic-gate ++count; 10317c478bd9Sstevel@tonic-gate } 1032*5b79143cSGirish Moodalbail } 10337c478bd9Sstevel@tonic-gate host->h_aliases[count] = NULL; 10347c478bd9Sstevel@tonic-gate host->h_length = sizeof (struct in_addr); 10357c478bd9Sstevel@tonic-gate host->h_addrtype = AF_INET; 10367c478bd9Sstevel@tonic-gate free(res); 10377c478bd9Sstevel@tonic-gate return (host); 10387c478bd9Sstevel@tonic-gate cleanup: 10397c478bd9Sstevel@tonic-gate *extract_error = NO_RECOVERY; 10407c478bd9Sstevel@tonic-gate (void) __IPv6_cleanup(res); 10417c478bd9Sstevel@tonic-gate return (NULL); 10427c478bd9Sstevel@tonic-gate } 10437c478bd9Sstevel@tonic-gate 10447c478bd9Sstevel@tonic-gate /* 10457c478bd9Sstevel@tonic-gate * This routine takes as input a pointer to a hostent and filters out 10467c478bd9Sstevel@tonic-gate * the type of addresses specified by the af argument. AF_INET 10477c478bd9Sstevel@tonic-gate * indicates that the caller wishes to filter out IPv4-mapped 10487c478bd9Sstevel@tonic-gate * addresses, and AF_INET6 indicates that the caller wishes to filter 10497c478bd9Sstevel@tonic-gate * out IPv6 addresses which aren't IPv4-mapped. If filtering would 10507c478bd9Sstevel@tonic-gate * result in all addresses being filtered out, a NULL pointer is returned. 10517c478bd9Sstevel@tonic-gate * Otherwise, the he pointer passed in is returned, even if no addresses 10527c478bd9Sstevel@tonic-gate * were filtered out. 10537c478bd9Sstevel@tonic-gate */ 10547c478bd9Sstevel@tonic-gate static struct hostent * 10557c478bd9Sstevel@tonic-gate __filter_addresses(int af, struct hostent *he) 10567c478bd9Sstevel@tonic-gate { 10577c478bd9Sstevel@tonic-gate struct in6_addr **in6addrlist, **in6addr; 10587c478bd9Sstevel@tonic-gate boolean_t isipv4mapped; 10597c478bd9Sstevel@tonic-gate int i = 0; 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate if (he == NULL) 10627c478bd9Sstevel@tonic-gate return (NULL); 10637c478bd9Sstevel@tonic-gate 10647c478bd9Sstevel@tonic-gate in6addrlist = (struct in6_addr **)he->h_addr_list; 10657c478bd9Sstevel@tonic-gate for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) { 10667c478bd9Sstevel@tonic-gate isipv4mapped = IN6_IS_ADDR_V4MAPPED(*in6addr); 10677c478bd9Sstevel@tonic-gate 10687c478bd9Sstevel@tonic-gate if ((af == AF_INET && !isipv4mapped) || 10697c478bd9Sstevel@tonic-gate (af == AF_INET6 && isipv4mapped)) { 10707c478bd9Sstevel@tonic-gate if (in6addrlist[i] != *in6addr) 10717c478bd9Sstevel@tonic-gate in6addrlist[i] = *in6addr; 10727c478bd9Sstevel@tonic-gate i++; 10737c478bd9Sstevel@tonic-gate } 10747c478bd9Sstevel@tonic-gate } 10757c478bd9Sstevel@tonic-gate 10767c478bd9Sstevel@tonic-gate if (i == 0) { 10777c478bd9Sstevel@tonic-gate /* We filtered everything out. */ 10787c478bd9Sstevel@tonic-gate return (NULL); 10797c478bd9Sstevel@tonic-gate } else { 10807c478bd9Sstevel@tonic-gate /* NULL terminate the list and return the hostent */ 10817c478bd9Sstevel@tonic-gate in6addrlist[i] = NULL; 10827c478bd9Sstevel@tonic-gate return (he); 10837c478bd9Sstevel@tonic-gate } 10847c478bd9Sstevel@tonic-gate } 10857c478bd9Sstevel@tonic-gate 10867c478bd9Sstevel@tonic-gate /* 10877c478bd9Sstevel@tonic-gate * This routine searches a hostent for v4 mapped IPv6 addresses. 10887c478bd9Sstevel@tonic-gate * he hostent structure to seach 10897c478bd9Sstevel@tonic-gate * find_both flag indicating if only want mapped or both map'd and v6 10907c478bd9Sstevel@tonic-gate * return values: 10917c478bd9Sstevel@tonic-gate * 0 = No mapped addresses 10927c478bd9Sstevel@tonic-gate * 1 = Mapped v4 address found (returns on first one found) 10937c478bd9Sstevel@tonic-gate * 2 = Both v6 and v4 mapped are present 10947c478bd9Sstevel@tonic-gate * 10957c478bd9Sstevel@tonic-gate * If hostent passed in with no addresses, zero will be returned. 10967c478bd9Sstevel@tonic-gate */ 10977c478bd9Sstevel@tonic-gate 10987c478bd9Sstevel@tonic-gate static int 10997c478bd9Sstevel@tonic-gate __find_mapped(struct hostent *he, int find_both) 11007c478bd9Sstevel@tonic-gate { 11017c478bd9Sstevel@tonic-gate int i; 11027c478bd9Sstevel@tonic-gate int mapd_found = 0; 11037c478bd9Sstevel@tonic-gate int v6_found = 0; 11047c478bd9Sstevel@tonic-gate 11057c478bd9Sstevel@tonic-gate for (i = 0; he->h_addr_list[i] != NULL; i++) { 110661961e0fSrobinson /* LINTED pointer cast */ 11077c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED( 11087c478bd9Sstevel@tonic-gate (struct in6_addr *)he->h_addr_list[i])) { 11097c478bd9Sstevel@tonic-gate if (find_both) 11107c478bd9Sstevel@tonic-gate mapd_found = 1; 11117c478bd9Sstevel@tonic-gate else 11127c478bd9Sstevel@tonic-gate return (1); 11137c478bd9Sstevel@tonic-gate } else { 11147c478bd9Sstevel@tonic-gate v6_found = 1; 11157c478bd9Sstevel@tonic-gate } 11167c478bd9Sstevel@tonic-gate /* save some iterations once both found */ 11177c478bd9Sstevel@tonic-gate if (mapd_found && v6_found) 11187c478bd9Sstevel@tonic-gate return (2); 11197c478bd9Sstevel@tonic-gate } 11207c478bd9Sstevel@tonic-gate return (mapd_found); 11217c478bd9Sstevel@tonic-gate } 11227c478bd9Sstevel@tonic-gate 11237c478bd9Sstevel@tonic-gate /* 11247c478bd9Sstevel@tonic-gate * This routine was added specifically for the IPv6 getipnodeby*() APIs. This 11257c478bd9Sstevel@tonic-gate * separates the result pointer (ptr to hostent+data buf) from the 11267c478bd9Sstevel@tonic-gate * nss_XbyY_buf_t ptr (required for nsswitch API). The returned hostent ptr 11277c478bd9Sstevel@tonic-gate * can be passed to freehostent() and freed independently. 11287c478bd9Sstevel@tonic-gate * 11297c478bd9Sstevel@tonic-gate * bufp->result bufp->buffer 11307c478bd9Sstevel@tonic-gate * | | 11317c478bd9Sstevel@tonic-gate * V V 11327c478bd9Sstevel@tonic-gate * ------------------------------------------------...-- 11337c478bd9Sstevel@tonic-gate * |struct hostent |addresses aliases | 11347c478bd9Sstevel@tonic-gate * ------------------------------------------------...-- 11357c478bd9Sstevel@tonic-gate * | |<--------bufp->buflen-------------->| 11367c478bd9Sstevel@tonic-gate */ 11377c478bd9Sstevel@tonic-gate 11387c478bd9Sstevel@tonic-gate #define ALIGN(x) ((((long)(x)) + sizeof (long) - 1) & ~(sizeof (long) - 1)) 11397c478bd9Sstevel@tonic-gate 11407c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t * 11417c478bd9Sstevel@tonic-gate __IPv6_alloc(int bufsz) 11427c478bd9Sstevel@tonic-gate { 11437c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *bufp; 11447c478bd9Sstevel@tonic-gate 11457c478bd9Sstevel@tonic-gate if ((bufp = malloc(sizeof (nss_XbyY_buf_t))) == NULL) 11467c478bd9Sstevel@tonic-gate return (NULL); 11477c478bd9Sstevel@tonic-gate 11487c478bd9Sstevel@tonic-gate if ((bufp->result = malloc(ALIGN(sizeof (struct hostent)) + bufsz)) == 11497c478bd9Sstevel@tonic-gate NULL) { 11507c478bd9Sstevel@tonic-gate free(bufp); 11517c478bd9Sstevel@tonic-gate return (NULL); 11527c478bd9Sstevel@tonic-gate } 11537c478bd9Sstevel@tonic-gate bufp->buffer = (char *)(bufp->result) + sizeof (struct hostent); 11547c478bd9Sstevel@tonic-gate bufp->buflen = bufsz; 11557c478bd9Sstevel@tonic-gate return (bufp); 11567c478bd9Sstevel@tonic-gate } 11577c478bd9Sstevel@tonic-gate 11587c478bd9Sstevel@tonic-gate /* 11597c478bd9Sstevel@tonic-gate * This routine is use only for error return cleanup. This will free the 11607c478bd9Sstevel@tonic-gate * hostent pointer, so don't use for successful returns. 11617c478bd9Sstevel@tonic-gate */ 11627c478bd9Sstevel@tonic-gate static void 11637c478bd9Sstevel@tonic-gate __IPv6_cleanup(nss_XbyY_buf_t *bufp) 11647c478bd9Sstevel@tonic-gate { 11657c478bd9Sstevel@tonic-gate if (bufp == NULL) 11667c478bd9Sstevel@tonic-gate return; 11677c478bd9Sstevel@tonic-gate if (bufp->result != NULL) 11687c478bd9Sstevel@tonic-gate free(bufp->result); 11697c478bd9Sstevel@tonic-gate free(bufp); 11707c478bd9Sstevel@tonic-gate } 1171