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 55b79143cSGirish Moodalbail * Common Development and Distribution License (the "License"). 65b79143cSGirish 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 /* 23634e26ecSCasper H.S. Dik * Copyright 2010 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. 76*2f443e27SRobert Mustacchi * IPNODE_UNMAP The user doesn't want v4 mapped addresses if no IPv6 77*2f443e27SRobert Mustacchi * interfaces are plumbed on the system. 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate #define IPNODE_WANTIPV6 0x00000001u 807c478bd9Sstevel@tonic-gate #define IPNODE_WANTIPV4 0x00000002u 817c478bd9Sstevel@tonic-gate #define IPNODE_IPV4IFNOIPV6 0x00000004u 827c478bd9Sstevel@tonic-gate #define IPNODE_LOOKUPIPNODES 0x00000008u 837c478bd9Sstevel@tonic-gate #define IPNODE_LOOKUPHOSTS 0x00000010u 847c478bd9Sstevel@tonic-gate #define IPNODE_LITERAL 0x00000020u 85*2f443e27SRobert Mustacchi #define IPNODE_UNMAP 0x00000040u 867c478bd9Sstevel@tonic-gate #define IPNODE_IPV4 (IPNODE_WANTIPV4 | IPNODE_IPV4IFNOIPV6) 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* 89*2f443e27SRobert Mustacchi * The private flag between libsocket and libnsl. See 90*2f443e27SRobert Mustacchi * lib/libsocket/inet/getaddrinfo.c for more information. 91*2f443e27SRobert Mustacchi */ 92*2f443e27SRobert Mustacchi #define AI_ADDRINFO 0x8000 93*2f443e27SRobert Mustacchi 94*2f443e27SRobert Mustacchi /* 957c478bd9Sstevel@tonic-gate * The default set of bits corresponding to a getipnodebyname() flags 967c478bd9Sstevel@tonic-gate * argument of AI_DEFAULT. 977c478bd9Sstevel@tonic-gate */ 987c478bd9Sstevel@tonic-gate #define IPNODE_DEFAULT (IPNODE_WANTIPV6 | IPNODE_IPV4 | \ 997c478bd9Sstevel@tonic-gate IPNODE_LOOKUPIPNODES | IPNODE_LOOKUPHOSTS) 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate extern struct netconfig *__rpc_getconfip(char *); 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate static struct hostent *__mapv4tov6(struct hostent *, struct hostent *, 1047c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *, int); 1057c478bd9Sstevel@tonic-gate struct hostent *__mappedtov4(struct hostent *, int *); 1067c478bd9Sstevel@tonic-gate static struct hostent *__filter_addresses(int, struct hostent *); 1077c478bd9Sstevel@tonic-gate static int __find_mapped(struct hostent *, int); 1087c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *__IPv6_alloc(int); 1097c478bd9Sstevel@tonic-gate static void __IPv6_cleanup(nss_XbyY_buf_t *); 1107c478bd9Sstevel@tonic-gate static int __ai_addrconfig(int); 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate #ifdef PIC 1147c478bd9Sstevel@tonic-gate struct hostent * 1157c478bd9Sstevel@tonic-gate _uncached_getipnodebyname(const char *nam, struct hostent *result, 1167c478bd9Sstevel@tonic-gate char *buffer, int buflen, int af_family, int flags, int *h_errnop) 1177c478bd9Sstevel@tonic-gate { 11861961e0fSrobinson return (_switch_getipnodebyname_r(nam, result, buffer, buflen, 11961961e0fSrobinson af_family, flags, h_errnop)); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate struct hostent * 1237c478bd9Sstevel@tonic-gate _uncached_getipnodebyaddr(const char *addr, int length, int type, 1247c478bd9Sstevel@tonic-gate struct hostent *result, char *buffer, int buflen, int *h_errnop) 1257c478bd9Sstevel@tonic-gate { 1267c478bd9Sstevel@tonic-gate if (type == AF_INET) 1277c478bd9Sstevel@tonic-gate return (_switch_gethostbyaddr_r(addr, length, type, 1287c478bd9Sstevel@tonic-gate result, buffer, buflen, h_errnop)); 1297c478bd9Sstevel@tonic-gate else if (type == AF_INET6) 1307c478bd9Sstevel@tonic-gate return (_switch_getipnodebyaddr_r(addr, length, type, 1317c478bd9Sstevel@tonic-gate result, buffer, buflen, h_errnop)); 1327c478bd9Sstevel@tonic-gate return (NULL); 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate #endif 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate /* 1377c478bd9Sstevel@tonic-gate * Given a name, an address family, and a set of flags, return a 1387c478bd9Sstevel@tonic-gate * bitfield that getipnodebyname() will use. 1397c478bd9Sstevel@tonic-gate */ 1407c478bd9Sstevel@tonic-gate static uint_t 1417c478bd9Sstevel@tonic-gate getipnodebyname_processflags(const char *name, int af, int flags) 1427c478bd9Sstevel@tonic-gate { 1437c478bd9Sstevel@tonic-gate uint_t ipnode_bits = IPNODE_DEFAULT; 1447c478bd9Sstevel@tonic-gate boolean_t ipv6configured = B_FALSE; 1457c478bd9Sstevel@tonic-gate boolean_t ipv4configured = B_FALSE; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate /* 1487c478bd9Sstevel@tonic-gate * If AI_ADDRCONFIG is specified, we need to determine the number 1497c478bd9Sstevel@tonic-gate * of addresses of each address family configured on the system as 1507c478bd9Sstevel@tonic-gate * appropriate. 1517c478bd9Sstevel@tonic-gate */ 1527c478bd9Sstevel@tonic-gate if (flags & AI_ADDRCONFIG) { 1537c478bd9Sstevel@tonic-gate ipv6configured = (af == AF_INET6 && 1547c478bd9Sstevel@tonic-gate __ai_addrconfig(AF_INET6) > 0); 1557c478bd9Sstevel@tonic-gate ipv4configured = ((af == AF_INET || (flags & AI_V4MAPPED)) && 1567c478bd9Sstevel@tonic-gate __ai_addrconfig(AF_INET) > 0); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate /* 1607c478bd9Sstevel@tonic-gate * Determine what kinds of addresses the user is interested 1617c478bd9Sstevel@tonic-gate * in getting back. 1627c478bd9Sstevel@tonic-gate */ 1637c478bd9Sstevel@tonic-gate switch (af) { 1647c478bd9Sstevel@tonic-gate case AF_INET6: 1657c478bd9Sstevel@tonic-gate if ((flags & AI_ADDRCONFIG) && !ipv6configured) 1667c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_WANTIPV6; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (flags & AI_V4MAPPED) { 1697c478bd9Sstevel@tonic-gate if ((flags & AI_ADDRCONFIG) && !ipv4configured) { 1707c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4; 1717c478bd9Sstevel@tonic-gate } else if (flags & AI_ALL) { 1727c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4IFNOIPV6; 1737c478bd9Sstevel@tonic-gate } 174*2f443e27SRobert Mustacchi if ((flags & AI_ADDRCONFIG) && !ipv6configured && 175*2f443e27SRobert Mustacchi (flags & AI_ADDRINFO)) { 176*2f443e27SRobert Mustacchi ipnode_bits |= IPNODE_UNMAP; 177*2f443e27SRobert Mustacchi } 1787c478bd9Sstevel@tonic-gate } else { 1797c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4; 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate break; 1827c478bd9Sstevel@tonic-gate case AF_INET: 1837c478bd9Sstevel@tonic-gate if ((flags & AI_ADDRCONFIG) && !ipv4configured) 1847c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4; 1857c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_WANTIPV6; 1867c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_IPV4IFNOIPV6; 1877c478bd9Sstevel@tonic-gate break; 1887c478bd9Sstevel@tonic-gate default: 1897c478bd9Sstevel@tonic-gate ipnode_bits = 0; 1907c478bd9Sstevel@tonic-gate break; 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate /* 1947c478bd9Sstevel@tonic-gate * If we're not looking for IPv4 addresses, don't bother looking 1957c478bd9Sstevel@tonic-gate * in hosts. 1967c478bd9Sstevel@tonic-gate */ 1977c478bd9Sstevel@tonic-gate if (!(ipnode_bits & IPNODE_WANTIPV4)) 1987c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPHOSTS; 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * Determine if name is a literal IP address. This will 2027c478bd9Sstevel@tonic-gate * further narrow down what type of lookup we're going to do. 2037c478bd9Sstevel@tonic-gate */ 2047c478bd9Sstevel@tonic-gate if (strchr(name, IPV6_LITERAL_CHAR) != NULL) { 2057c478bd9Sstevel@tonic-gate /* Literal IPv6 address */ 2067c478bd9Sstevel@tonic-gate ipnode_bits |= IPNODE_LITERAL; 2077c478bd9Sstevel@tonic-gate /* 2087c478bd9Sstevel@tonic-gate * In s9 we accepted the literal without filtering independent 2097c478bd9Sstevel@tonic-gate * of what family was passed in hints. We continue to do 2107c478bd9Sstevel@tonic-gate * this. 2117c478bd9Sstevel@tonic-gate */ 2127c478bd9Sstevel@tonic-gate ipnode_bits |= (IPNODE_WANTIPV6 | IPNODE_WANTIPV4); 2137c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPHOSTS; 21461961e0fSrobinson } else if (inet_addr(name) != 0xffffffffU) { 2157c478bd9Sstevel@tonic-gate /* Literal IPv4 address */ 2167c478bd9Sstevel@tonic-gate ipnode_bits |= (IPNODE_LITERAL | IPNODE_WANTIPV4); 2177c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_WANTIPV6; 2187c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPIPNODES; 2197c478bd9Sstevel@tonic-gate } 2207c478bd9Sstevel@tonic-gate return (ipnode_bits); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate struct hostent * 2247c478bd9Sstevel@tonic-gate getipnodebyname(const char *name, int af, int flags, int *error_num) 2257c478bd9Sstevel@tonic-gate { 2267c478bd9Sstevel@tonic-gate struct hostent *hp = NULL; 2277c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *buf4 = NULL; 2287c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *buf6 = NULL; 2297c478bd9Sstevel@tonic-gate struct netconfig *nconf; 2307c478bd9Sstevel@tonic-gate struct nss_netdirbyname_in nssin; 2317c478bd9Sstevel@tonic-gate union nss_netdirbyname_out nssout; 2327c478bd9Sstevel@tonic-gate int ret; 2337c478bd9Sstevel@tonic-gate uint_t ipnode_bits; 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 2367c478bd9Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 2377c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 2387c478bd9Sstevel@tonic-gate return (NULL); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate ipnode_bits = getipnodebyname_processflags(name, af, flags); 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate /* Make sure we have something to look up. */ 2447c478bd9Sstevel@tonic-gate if (!(ipnode_bits & (IPNODE_WANTIPV6 | IPNODE_WANTIPV4))) { 2457c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 2467c478bd9Sstevel@tonic-gate goto cleanup; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate /* 2507c478bd9Sstevel@tonic-gate * Perform the requested lookups. We always look through 2517c478bd9Sstevel@tonic-gate * ipnodes first for both IPv4 and IPv6 addresses. Depending 2527c478bd9Sstevel@tonic-gate * on what was returned and what was needed, we either filter 2537c478bd9Sstevel@tonic-gate * out the garbage, or ask for more using hosts. 2547c478bd9Sstevel@tonic-gate */ 2557c478bd9Sstevel@tonic-gate if (ipnode_bits & IPNODE_LOOKUPIPNODES) { 2567c478bd9Sstevel@tonic-gate if ((buf6 = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == NULL) { 2577c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 2587c478bd9Sstevel@tonic-gate goto cleanup; 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 2617c478bd9Sstevel@tonic-gate nssin.arg.nss.host6.name = name; 2627c478bd9Sstevel@tonic-gate nssin.arg.nss.host6.buf = buf6->buffer; 2637c478bd9Sstevel@tonic-gate nssin.arg.nss.host6.buflen = buf6->buflen; 2647c478bd9Sstevel@tonic-gate nssin.arg.nss.host6.af_family = af; 2657c478bd9Sstevel@tonic-gate nssin.arg.nss.host6.flags = flags; 2667c478bd9Sstevel@tonic-gate nssout.nss.host.hent = buf6->result; 2677c478bd9Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 2687c478bd9Sstevel@tonic-gate ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout); 2697c478bd9Sstevel@tonic-gate if (ret != ND_OK) { 2707c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf6); 2717c478bd9Sstevel@tonic-gate buf6 = NULL; 2727c478bd9Sstevel@tonic-gate } else if (ipnode_bits & IPNODE_WANTIPV4) { 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * buf6 may have all that we need if we either 2757c478bd9Sstevel@tonic-gate * only wanted IPv4 addresses if there were no 2767c478bd9Sstevel@tonic-gate * IPv6 addresses returned, or if there are 2777c478bd9Sstevel@tonic-gate * IPv4-mapped addresses in buf6. If either 2787c478bd9Sstevel@tonic-gate * of these are true, then there's no need to 2797c478bd9Sstevel@tonic-gate * look in hosts. 2807c478bd9Sstevel@tonic-gate */ 2817c478bd9Sstevel@tonic-gate if (ipnode_bits & IPNODE_IPV4IFNOIPV6 || 2827c478bd9Sstevel@tonic-gate __find_mapped(buf6->result, 0) != 0) { 2837c478bd9Sstevel@tonic-gate ipnode_bits &= ~IPNODE_LOOKUPHOSTS; 2847c478bd9Sstevel@tonic-gate } else if (!(ipnode_bits & IPNODE_WANTIPV6)) { 2857c478bd9Sstevel@tonic-gate /* 2867c478bd9Sstevel@tonic-gate * If all we're looking for are IPv4 2877c478bd9Sstevel@tonic-gate * addresses and there are none in 2887c478bd9Sstevel@tonic-gate * buf6 then buf6 is now useless. 2897c478bd9Sstevel@tonic-gate */ 2907c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf6); 2917c478bd9Sstevel@tonic-gate buf6 = NULL; 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate } 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate if (ipnode_bits & IPNODE_LOOKUPHOSTS) { 2967c478bd9Sstevel@tonic-gate if ((buf4 = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == NULL) { 2977c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 2987c478bd9Sstevel@tonic-gate goto cleanup; 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate nssin.op_t = NSS_HOST; 3017c478bd9Sstevel@tonic-gate nssin.arg.nss.host.name = name; 3027c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buf = buf4->buffer; 3037c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf4->buflen; 3047c478bd9Sstevel@tonic-gate nssout.nss.host.hent = buf4->result; 3057c478bd9Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 3067c478bd9Sstevel@tonic-gate ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout); 3077c478bd9Sstevel@tonic-gate if (ret != ND_OK) { 3087c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf4); 3097c478bd9Sstevel@tonic-gate buf4 = NULL; 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate if (buf6 == NULL && buf4 == NULL) { 3147c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 3157c478bd9Sstevel@tonic-gate goto cleanup; 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /* Extract the appropriate addresses from the returned buffer(s). */ 3197c478bd9Sstevel@tonic-gate switch (af) { 3207c478bd9Sstevel@tonic-gate case AF_INET6: { 3217c478bd9Sstevel@tonic-gate if (buf4 != NULL) { 3227c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *mergebuf; 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate /* 3257c478bd9Sstevel@tonic-gate * The IPv4 results we have need to be 3267c478bd9Sstevel@tonic-gate * converted to IPv4-mapped addresses, 3277c478bd9Sstevel@tonic-gate * conditionally merged with the IPv6 3287c478bd9Sstevel@tonic-gate * results, and the end result needs to be 3297c478bd9Sstevel@tonic-gate * re-ordered. 3307c478bd9Sstevel@tonic-gate */ 3317c478bd9Sstevel@tonic-gate mergebuf = __IPv6_alloc(NSS_BUFLEN_IPNODES); 3327c478bd9Sstevel@tonic-gate if (mergebuf == NULL) { 3337c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 3347c478bd9Sstevel@tonic-gate goto cleanup; 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate hp = __mapv4tov6(buf4->result, 3377c478bd9Sstevel@tonic-gate ((buf6 != NULL) ? buf6->result : NULL), 3387c478bd9Sstevel@tonic-gate mergebuf, 1); 3397c478bd9Sstevel@tonic-gate if (hp != NULL) 3407c478bd9Sstevel@tonic-gate order_haddrlist_af(AF_INET6, hp->h_addr_list); 3417c478bd9Sstevel@tonic-gate else 3427c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 3437c478bd9Sstevel@tonic-gate free(mergebuf); 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate if (buf4 == NULL && buf6 != NULL) { 3477c478bd9Sstevel@tonic-gate hp = buf6->result; 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate /* 3507c478bd9Sstevel@tonic-gate * We have what we need in buf6, but we may need 3517c478bd9Sstevel@tonic-gate * to filter out some addresses depending on what 3527c478bd9Sstevel@tonic-gate * is being asked for. 3537c478bd9Sstevel@tonic-gate */ 3547c478bd9Sstevel@tonic-gate if (!(ipnode_bits & IPNODE_WANTIPV4)) 3557c478bd9Sstevel@tonic-gate hp = __filter_addresses(AF_INET, buf6->result); 3567c478bd9Sstevel@tonic-gate else if (!(ipnode_bits & IPNODE_WANTIPV6)) 3577c478bd9Sstevel@tonic-gate hp = __filter_addresses(AF_INET6, buf6->result); 3587c478bd9Sstevel@tonic-gate 359*2f443e27SRobert Mustacchi /* 360*2f443e27SRobert Mustacchi * We've been asked to unmap v4 addresses. This 361*2f443e27SRobert Mustacchi * situation implies IPNODE_WANTIPV4 and 362*2f443e27SRobert Mustacchi * !IPNODE_WANTIPV6. 363*2f443e27SRobert Mustacchi */ 364*2f443e27SRobert Mustacchi if (hp != NULL && (ipnode_bits & IPNODE_UNMAP)) { 365*2f443e27SRobert Mustacchi /* 366*2f443e27SRobert Mustacchi * Just set hp to a new value, cleanup: will 367*2f443e27SRobert Mustacchi * free the old one 368*2f443e27SRobert Mustacchi */ 369*2f443e27SRobert Mustacchi hp = __mappedtov4(hp, error_num); 370*2f443e27SRobert Mustacchi } else if (hp == NULL) 3717c478bd9Sstevel@tonic-gate *error_num = NO_ADDRESS; 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate break; 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate case AF_INET: 3787c478bd9Sstevel@tonic-gate /* We could have results in buf6 or buf4, not both */ 3797c478bd9Sstevel@tonic-gate if (buf6 != NULL) { 3807c478bd9Sstevel@tonic-gate /* 3817c478bd9Sstevel@tonic-gate * Extract the IPv4-mapped addresses from buf6 3827c478bd9Sstevel@tonic-gate * into hp. 3837c478bd9Sstevel@tonic-gate */ 3847c478bd9Sstevel@tonic-gate hp = __mappedtov4(buf6->result, error_num); 3857c478bd9Sstevel@tonic-gate } else { 3867c478bd9Sstevel@tonic-gate /* We have what we need in buf4. */ 3877c478bd9Sstevel@tonic-gate hp = buf4->result; 3887c478bd9Sstevel@tonic-gate if (ipnode_bits & IPNODE_LITERAL) { 3897c478bd9Sstevel@tonic-gate /* 3907c478bd9Sstevel@tonic-gate * There is a special case here for literal 3917c478bd9Sstevel@tonic-gate * IPv4 address strings. The hosts 3927c478bd9Sstevel@tonic-gate * front-end sets h_aliases to a one 3937c478bd9Sstevel@tonic-gate * element array containing a single NULL 3947c478bd9Sstevel@tonic-gate * pointer (in ndaddr2hent()), while 3957c478bd9Sstevel@tonic-gate * getipnodebyname() requires h_aliases to 3967c478bd9Sstevel@tonic-gate * be a NULL pointer itself. We're not 3977c478bd9Sstevel@tonic-gate * going to change the front-end since it 3987c478bd9Sstevel@tonic-gate * needs to remain backward compatible for 3997c478bd9Sstevel@tonic-gate * gethostbyname() and friends. Just set 4007c478bd9Sstevel@tonic-gate * h_aliases to NULL here instead. 4017c478bd9Sstevel@tonic-gate */ 4027c478bd9Sstevel@tonic-gate hp->h_aliases = NULL; 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate } 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate break; 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate default: 4097c478bd9Sstevel@tonic-gate break; 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate cleanup: 4137c478bd9Sstevel@tonic-gate /* 4147c478bd9Sstevel@tonic-gate * Free the memory we allocated, but make sure we don't free 4157c478bd9Sstevel@tonic-gate * the memory we're returning to the caller. 4167c478bd9Sstevel@tonic-gate */ 4177c478bd9Sstevel@tonic-gate if (buf6 != NULL) { 4187c478bd9Sstevel@tonic-gate if (buf6->result == hp) 4197c478bd9Sstevel@tonic-gate buf6->result = NULL; 4207c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf6); 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate if (buf4 != NULL) { 4237c478bd9Sstevel@tonic-gate if (buf4->result == hp) 4247c478bd9Sstevel@tonic-gate buf4->result = NULL; 4257c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf4); 4267c478bd9Sstevel@tonic-gate } 4277c478bd9Sstevel@tonic-gate (void) freenetconfigent(nconf); 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate return (hp); 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate /* 4337c478bd9Sstevel@tonic-gate * This is the IPv6 interface for "gethostbyaddr". 4347c478bd9Sstevel@tonic-gate */ 4357c478bd9Sstevel@tonic-gate struct hostent * 4367c478bd9Sstevel@tonic-gate getipnodebyaddr(const void *src, size_t len, int type, int *error_num) 4377c478bd9Sstevel@tonic-gate { 4387c478bd9Sstevel@tonic-gate struct in6_addr *addr6 = 0; 4397c478bd9Sstevel@tonic-gate struct in_addr *addr4 = 0; 4407c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *buf = 0; 4417c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *res = 0; 4427c478bd9Sstevel@tonic-gate struct netconfig *nconf; 4437c478bd9Sstevel@tonic-gate struct hostent *hp = 0; 4447c478bd9Sstevel@tonic-gate struct nss_netdirbyaddr_in nssin; 4457c478bd9Sstevel@tonic-gate union nss_netdirbyaddr_out nssout; 4467c478bd9Sstevel@tonic-gate int neterr; 4477c478bd9Sstevel@tonic-gate char tmpbuf[64]; 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate if (type == AF_INET6) { 4507c478bd9Sstevel@tonic-gate if ((addr6 = (struct in6_addr *)src) == NULL) { 4517c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 4527c478bd9Sstevel@tonic-gate return (NULL); 4537c478bd9Sstevel@tonic-gate } 4547c478bd9Sstevel@tonic-gate } else if (type == AF_INET) { 4557c478bd9Sstevel@tonic-gate if ((addr4 = (struct in_addr *)src) == NULL) { 4567c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 4577c478bd9Sstevel@tonic-gate return (NULL); 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate } else { 4607c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 4617c478bd9Sstevel@tonic-gate return (NULL); 4627c478bd9Sstevel@tonic-gate } 4637c478bd9Sstevel@tonic-gate /* 4647c478bd9Sstevel@tonic-gate * Specific case: query for "::" 4657c478bd9Sstevel@tonic-gate */ 4667c478bd9Sstevel@tonic-gate if (type == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(addr6)) { 4677c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 4687c478bd9Sstevel@tonic-gate return (NULL); 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate /* 4717c478bd9Sstevel@tonic-gate * Step 1: IPv4-mapped address or IPv4 Compat 4727c478bd9Sstevel@tonic-gate */ 4737c478bd9Sstevel@tonic-gate if ((type == AF_INET6 && len == 16) && 4747c478bd9Sstevel@tonic-gate ((IN6_IS_ADDR_V4MAPPED(addr6)) || 4757c478bd9Sstevel@tonic-gate (IN6_IS_ADDR_V4COMPAT(addr6)))) { 4767c478bd9Sstevel@tonic-gate if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 4777c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 4787c478bd9Sstevel@tonic-gate return (NULL); 4797c478bd9Sstevel@tonic-gate } 4807c478bd9Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 4817c478bd9Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 4827c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 4837c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 4847c478bd9Sstevel@tonic-gate return (NULL); 4857c478bd9Sstevel@tonic-gate } 4867c478bd9Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 4877c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4COMPAT(addr6)) { 48861961e0fSrobinson (void) memcpy(tmpbuf, addr6, sizeof (*addr6)); 4897c478bd9Sstevel@tonic-gate tmpbuf[10] = 0xffU; 4907c478bd9Sstevel@tonic-gate tmpbuf[11] = 0xffU; 4917c478bd9Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)tmpbuf; 4927c478bd9Sstevel@tonic-gate } else { 4937c478bd9Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)addr6; 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate nssin.arg.nss.host.len = sizeof (struct in6_addr); 4967c478bd9Sstevel@tonic-gate nssin.arg.nss.host.type = AF_INET6; 4977c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buf = buf->buffer; 4987c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf->buflen; 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate nssout.nss.host.hent = buf->result; 5017c478bd9Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 5027c478bd9Sstevel@tonic-gate /* 5037c478bd9Sstevel@tonic-gate * We pass in nconf and let the implementation of the 5047c478bd9Sstevel@tonic-gate * long-named func decide whether to use the switch based on 5057c478bd9Sstevel@tonic-gate * nc_nlookups. 5067c478bd9Sstevel@tonic-gate */ 5077c478bd9Sstevel@tonic-gate neterr = 5087c478bd9Sstevel@tonic-gate _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout); 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate (void) freenetconfigent(nconf); 5117c478bd9Sstevel@tonic-gate if (neterr != ND_OK) { 5127c478bd9Sstevel@tonic-gate /* Failover case, try hosts db for v4 address */ 5137c478bd9Sstevel@tonic-gate if (!gethostbyaddr_r(((char *)addr6) + 12, 5147c478bd9Sstevel@tonic-gate sizeof (in_addr_t), AF_INET, buf->result, 5157c478bd9Sstevel@tonic-gate buf->buffer, buf->buflen, error_num)) { 5167c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 5177c478bd9Sstevel@tonic-gate return (NULL); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate /* Found one, now format it into mapped/compat addr */ 5207c478bd9Sstevel@tonic-gate if ((res = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 5217c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 5227c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 5237c478bd9Sstevel@tonic-gate return (NULL); 5247c478bd9Sstevel@tonic-gate } 5257c478bd9Sstevel@tonic-gate /* Convert IPv4 to mapped/compat address w/name */ 5267c478bd9Sstevel@tonic-gate hp = res->result; 52761961e0fSrobinson (void) __mapv4tov6(buf->result, 0, res, 5287c478bd9Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED(addr6)); 5297c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 5307c478bd9Sstevel@tonic-gate free(res); 5317c478bd9Sstevel@tonic-gate return (hp); 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate /* 5347c478bd9Sstevel@tonic-gate * At this point, we'll have a v4mapped hostent. If that's 5357c478bd9Sstevel@tonic-gate * what was passed in, just return. If the request was a compat, 5367c478bd9Sstevel@tonic-gate * twiggle the two bytes to make the mapped address a compat. 5377c478bd9Sstevel@tonic-gate */ 5387c478bd9Sstevel@tonic-gate hp = buf->result; 5397c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4COMPAT(addr6)) { 54061961e0fSrobinson /* LINTED pointer cast */ 5417c478bd9Sstevel@tonic-gate addr6 = (struct in6_addr *)hp->h_addr_list[0]; 5427c478bd9Sstevel@tonic-gate addr6->s6_addr[10] = 0; 5437c478bd9Sstevel@tonic-gate addr6->s6_addr[11] = 0; 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate free(buf); 5467c478bd9Sstevel@tonic-gate return (hp); 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate /* 5497c478bd9Sstevel@tonic-gate * Step 2: AF_INET, v4 lookup. Since we're going to search the 5507c478bd9Sstevel@tonic-gate * ipnodes (v6) path first, we need to treat this as a v4mapped 5517c478bd9Sstevel@tonic-gate * address. nscd(1m) caches v4 from ipnodes as mapped v6's. The 5527c478bd9Sstevel@tonic-gate * switch backend knows to lookup v4's (not v4mapped) from the 5537c478bd9Sstevel@tonic-gate * name services. 5547c478bd9Sstevel@tonic-gate */ 5557c478bd9Sstevel@tonic-gate if (type == AF_INET) { 5567c478bd9Sstevel@tonic-gate struct in6_addr v4mapbuf; 5577c478bd9Sstevel@tonic-gate addr6 = &v4mapbuf; 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(addr4, addr6); 5607c478bd9Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 5617c478bd9Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 5627c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 5637c478bd9Sstevel@tonic-gate return (NULL); 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 5667c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 5677c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 5687c478bd9Sstevel@tonic-gate return (NULL); 5697c478bd9Sstevel@tonic-gate } 5707c478bd9Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 5717c478bd9Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)addr6; 5727c478bd9Sstevel@tonic-gate nssin.arg.nss.host.len = sizeof (struct in6_addr); 5737c478bd9Sstevel@tonic-gate nssin.arg.nss.host.type = AF_INET6; 5747c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buf = buf->buffer; 5757c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf->buflen; 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate nssout.nss.host.hent = buf->result; 5787c478bd9Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 5797c478bd9Sstevel@tonic-gate /* 5807c478bd9Sstevel@tonic-gate * We pass in nconf and let the implementation of the 5817c478bd9Sstevel@tonic-gate * long-named func decide whether to use the switch based on 5827c478bd9Sstevel@tonic-gate * nc_nlookups. 5837c478bd9Sstevel@tonic-gate */ 5847c478bd9Sstevel@tonic-gate neterr = 5857c478bd9Sstevel@tonic-gate _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout); 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate (void) freenetconfigent(nconf); 5887c478bd9Sstevel@tonic-gate if (neterr != ND_OK) { 5897c478bd9Sstevel@tonic-gate /* Failover case, try hosts db for v4 address */ 5907c478bd9Sstevel@tonic-gate hp = buf->result; 5917c478bd9Sstevel@tonic-gate if (!gethostbyaddr_r(src, len, type, buf->result, 5927c478bd9Sstevel@tonic-gate buf->buffer, buf->buflen, error_num)) { 5937c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 5947c478bd9Sstevel@tonic-gate return (NULL); 5957c478bd9Sstevel@tonic-gate } 5967c478bd9Sstevel@tonic-gate free(buf); 5977c478bd9Sstevel@tonic-gate return (hp); 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate if ((hp = __mappedtov4(buf->result, error_num)) == NULL) { 6007c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 6017c478bd9Sstevel@tonic-gate return (NULL); 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 6047c478bd9Sstevel@tonic-gate return (hp); 6057c478bd9Sstevel@tonic-gate } 6067c478bd9Sstevel@tonic-gate /* 6077c478bd9Sstevel@tonic-gate * Step 3: AF_INET6, plain vanilla v6 getipnodebyaddr() call. 6087c478bd9Sstevel@tonic-gate */ 6097c478bd9Sstevel@tonic-gate if (type == AF_INET6) { 6107c478bd9Sstevel@tonic-gate if ((nconf = __rpc_getconfip("udp")) == NULL && 6117c478bd9Sstevel@tonic-gate (nconf = __rpc_getconfip("tcp")) == NULL) { 6127c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 6137c478bd9Sstevel@tonic-gate return (NULL); 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) { 6167c478bd9Sstevel@tonic-gate *error_num = NO_RECOVERY; 6177c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 6187c478bd9Sstevel@tonic-gate return (NULL); 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate nssin.op_t = NSS_HOST6; 6217c478bd9Sstevel@tonic-gate nssin.arg.nss.host.addr = (const char *)addr6; 6227c478bd9Sstevel@tonic-gate nssin.arg.nss.host.len = len; 6237c478bd9Sstevel@tonic-gate nssin.arg.nss.host.type = type; 6247c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buf = buf->buffer; 6257c478bd9Sstevel@tonic-gate nssin.arg.nss.host.buflen = buf->buflen; 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate nssout.nss.host.hent = buf->result; 6287c478bd9Sstevel@tonic-gate nssout.nss.host.herrno_p = error_num; 6297c478bd9Sstevel@tonic-gate /* 6307c478bd9Sstevel@tonic-gate * We pass in nconf and let the implementation of the 6317c478bd9Sstevel@tonic-gate * long-named func decide whether to use the switch based on 6327c478bd9Sstevel@tonic-gate * nc_nlookups. 6337c478bd9Sstevel@tonic-gate */ 6347c478bd9Sstevel@tonic-gate neterr = 6357c478bd9Sstevel@tonic-gate _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout); 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate (void) freenetconfigent(nconf); 6387c478bd9Sstevel@tonic-gate if (neterr != ND_OK) { 6397c478bd9Sstevel@tonic-gate __IPv6_cleanup(buf); 6407c478bd9Sstevel@tonic-gate return (NULL); 6417c478bd9Sstevel@tonic-gate } 6427c478bd9Sstevel@tonic-gate free(buf); 6437c478bd9Sstevel@tonic-gate return (nssout.nss.host.hent); 6447c478bd9Sstevel@tonic-gate } 6457c478bd9Sstevel@tonic-gate /* 6467c478bd9Sstevel@tonic-gate * If we got here, unknown type. 6477c478bd9Sstevel@tonic-gate */ 6487c478bd9Sstevel@tonic-gate *error_num = HOST_NOT_FOUND; 6497c478bd9Sstevel@tonic-gate return (NULL); 6507c478bd9Sstevel@tonic-gate } 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate void 6537c478bd9Sstevel@tonic-gate freehostent(struct hostent *hent) 6547c478bd9Sstevel@tonic-gate { 6557c478bd9Sstevel@tonic-gate free(hent); 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate static int 6597c478bd9Sstevel@tonic-gate __ai_addrconfig(int af) 6607c478bd9Sstevel@tonic-gate { 6617c478bd9Sstevel@tonic-gate struct lifnum lifn; 66220d217c8SGirish Moodalbail struct lifconf lifc; 66320d217c8SGirish Moodalbail struct lifreq *lifp, *buf = NULL; 66420d217c8SGirish Moodalbail size_t bufsize; 6657c478bd9Sstevel@tonic-gate hrtime_t now, *then; 6667c478bd9Sstevel@tonic-gate static hrtime_t then4, then6; /* the last time we updated ifnum# */ 6677c478bd9Sstevel@tonic-gate static int ifnum4 = -1, ifnum6 = -1; 6687c478bd9Sstevel@tonic-gate int *num; 66920d217c8SGirish Moodalbail int nlifr, count = 0; 67020d217c8SGirish Moodalbail 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate switch (af) { 6737c478bd9Sstevel@tonic-gate case AF_INET: 6747c478bd9Sstevel@tonic-gate num = &ifnum4; 6757c478bd9Sstevel@tonic-gate then = &then4; 6767c478bd9Sstevel@tonic-gate break; 6777c478bd9Sstevel@tonic-gate case AF_INET6: 6787c478bd9Sstevel@tonic-gate num = &ifnum6; 6797c478bd9Sstevel@tonic-gate then = &then6; 6807c478bd9Sstevel@tonic-gate break; 6817c478bd9Sstevel@tonic-gate default: 6827c478bd9Sstevel@tonic-gate return (0); 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate 6857c478bd9Sstevel@tonic-gate /* 6867c478bd9Sstevel@tonic-gate * We don't need to check this every time someone does a name 6877c478bd9Sstevel@tonic-gate * lookup. Do it every IFNUM_TIMEOUT for each address family. 6887c478bd9Sstevel@tonic-gate * 6897c478bd9Sstevel@tonic-gate * There's no need to protect all of this with a lock. The 6907c478bd9Sstevel@tonic-gate * worst that can happen is that we update the interface count 6917c478bd9Sstevel@tonic-gate * twice instead of once. That's no big deal. 6927c478bd9Sstevel@tonic-gate */ 6937c478bd9Sstevel@tonic-gate now = gethrtime(); 6947c478bd9Sstevel@tonic-gate if (*num == -1 || ((now - *then) >= IFNUM_TIMEOUT)) { 6957c478bd9Sstevel@tonic-gate lifn.lifn_family = af; 6967c478bd9Sstevel@tonic-gate /* 6977c478bd9Sstevel@tonic-gate * We want to determine if this machine knows anything 6987c478bd9Sstevel@tonic-gate * at all about the address family; the status of the 6997c478bd9Sstevel@tonic-gate * interface is less important. Hence, set 7007c478bd9Sstevel@tonic-gate * 'lifn_flags' to zero. 7017c478bd9Sstevel@tonic-gate */ 7027c478bd9Sstevel@tonic-gate lifn.lifn_flags = 0; 70320d217c8SGirish Moodalbail again: 7047c478bd9Sstevel@tonic-gate if (nss_ioctl(af, SIOCGLIFNUM, &lifn) < 0) 70520d217c8SGirish Moodalbail goto fail; 7067c478bd9Sstevel@tonic-gate 70720d217c8SGirish Moodalbail if (lifn.lifn_count == 0) { 70820d217c8SGirish Moodalbail *num = 0; 7097c478bd9Sstevel@tonic-gate *then = now; 71020d217c8SGirish Moodalbail return (*num); 7117c478bd9Sstevel@tonic-gate } 7127c478bd9Sstevel@tonic-gate 71320d217c8SGirish Moodalbail /* 71420d217c8SGirish Moodalbail * Pad the interface count to detect when additional 71520d217c8SGirish Moodalbail * interfaces have been configured between SIOCGLIFNUM 71620d217c8SGirish Moodalbail * and SIOCGLIFCONF. 71720d217c8SGirish Moodalbail */ 71820d217c8SGirish Moodalbail lifn.lifn_count += 4; 71920d217c8SGirish Moodalbail 72020d217c8SGirish Moodalbail bufsize = lifn.lifn_count * sizeof (struct lifreq); 72120d217c8SGirish Moodalbail if ((buf = realloc(buf, bufsize)) == NULL) 72220d217c8SGirish Moodalbail goto fail; 72320d217c8SGirish Moodalbail 72420d217c8SGirish Moodalbail lifc.lifc_family = af; 72520d217c8SGirish Moodalbail lifc.lifc_flags = 0; 72620d217c8SGirish Moodalbail lifc.lifc_len = bufsize; 72720d217c8SGirish Moodalbail lifc.lifc_buf = (caddr_t)buf; 72820d217c8SGirish Moodalbail if (nss_ioctl(af, SIOCGLIFCONF, &lifc) < 0) 72920d217c8SGirish Moodalbail goto fail; 73020d217c8SGirish Moodalbail 73120d217c8SGirish Moodalbail nlifr = lifc.lifc_len / sizeof (struct lifreq); 73220d217c8SGirish Moodalbail if (nlifr >= lifn.lifn_count) 73320d217c8SGirish Moodalbail goto again; 73420d217c8SGirish Moodalbail /* 73520d217c8SGirish Moodalbail * Do not include any loopback addresses, 127.0.0.1 for AF_INET 73620d217c8SGirish Moodalbail * and ::1 for AF_INET6, while counting the number of available 73720d217c8SGirish Moodalbail * IPv4 or IPv6 addresses. (RFC 3493 requires this, whenever 73820d217c8SGirish Moodalbail * AI_ADDRCONFIG flag is set) 73920d217c8SGirish Moodalbail */ 74020d217c8SGirish Moodalbail for (lifp = buf; lifp < buf + nlifr; lifp++) { 74120d217c8SGirish Moodalbail switch (af) { 74220d217c8SGirish Moodalbail case AF_INET: { 74320d217c8SGirish Moodalbail struct sockaddr_in *in; 74420d217c8SGirish Moodalbail 74520d217c8SGirish Moodalbail in = (struct sockaddr_in *)&lifp->lifr_addr; 74620d217c8SGirish Moodalbail if (ntohl(in->sin_addr.s_addr) == 74720d217c8SGirish Moodalbail INADDR_LOOPBACK) { 74820d217c8SGirish Moodalbail count++; 74920d217c8SGirish Moodalbail } 75020d217c8SGirish Moodalbail break; 75120d217c8SGirish Moodalbail } 75220d217c8SGirish Moodalbail case AF_INET6: { 75320d217c8SGirish Moodalbail struct sockaddr_in6 *in6; 75420d217c8SGirish Moodalbail 75520d217c8SGirish Moodalbail in6 = (struct sockaddr_in6 *)&lifp->lifr_addr; 75620d217c8SGirish Moodalbail if (IN6_IS_ADDR_LOOPBACK(&in6->sin6_addr)) 75720d217c8SGirish Moodalbail count++; 75820d217c8SGirish Moodalbail break; 75920d217c8SGirish Moodalbail } 76020d217c8SGirish Moodalbail } 76120d217c8SGirish Moodalbail } 76220d217c8SGirish Moodalbail *num = nlifr - count; 76320d217c8SGirish Moodalbail *then = now; 76420d217c8SGirish Moodalbail free(buf); 76520d217c8SGirish Moodalbail } 7667c478bd9Sstevel@tonic-gate return (*num); 76720d217c8SGirish Moodalbail fail: 76820d217c8SGirish Moodalbail free(buf); 769634e26ecSCasper H.S. Dik /* 770634e26ecSCasper H.S. Dik * If the process is running without the NET_ACCESS basic privilege, 771634e26ecSCasper H.S. Dik * pretend we still have inet/inet6 interfaces. 772634e26ecSCasper H.S. Dik */ 773634e26ecSCasper H.S. Dik if (errno == EACCES) 774634e26ecSCasper H.S. Dik return (1); 77520d217c8SGirish Moodalbail return (-1); 7767c478bd9Sstevel@tonic-gate } 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate /* 7797c478bd9Sstevel@tonic-gate * This routine will either convert an IPv4 address to a mapped or compat 7807c478bd9Sstevel@tonic-gate * IPv6 (if he6 == NULL) or merge IPv6 (he6) addresses with mapped 7817c478bd9Sstevel@tonic-gate * v4 (he4) addresses. In either case, the results are returned in res. 7827c478bd9Sstevel@tonic-gate * Caller must provide all buffers. 7837c478bd9Sstevel@tonic-gate * Inputs: 7847c478bd9Sstevel@tonic-gate * he4 pointer to IPv4 buffer 7857c478bd9Sstevel@tonic-gate * he6 pointer to IPv6 buffer (NULL if not merging v4/v6 7867c478bd9Sstevel@tonic-gate * res pointer to results buffer 7877c478bd9Sstevel@tonic-gate * mapped mapped == 1, map IPv4 : mapped == 0, compat IPv4 7887c478bd9Sstevel@tonic-gate * mapped flag is ignored if he6 != NULL 7897c478bd9Sstevel@tonic-gate * 7907c478bd9Sstevel@tonic-gate * The results are packed into the res->buffer as follows: 7917c478bd9Sstevel@tonic-gate * <--------------- buffer + buflen --------------------------------------> 7927c478bd9Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 7937c478bd9Sstevel@tonic-gate * | pointers vector | pointers vector | aliases grow | addresses grow | 7947c478bd9Sstevel@tonic-gate * | for addresses | for aliases | | | 7957c478bd9Sstevel@tonic-gate * | this way -> | this way -> | <- this way |<- this way | 7967c478bd9Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 7977c478bd9Sstevel@tonic-gate * | grows in PASS 1 | grows in PASS2 | grows in PASS2 | grows in PASS 1| 7987c478bd9Sstevel@tonic-gate */ 7997c478bd9Sstevel@tonic-gate static struct hostent * 8007c478bd9Sstevel@tonic-gate __mapv4tov6(struct hostent *he4, struct hostent *he6, nss_XbyY_buf_t *res, 8017c478bd9Sstevel@tonic-gate int mapped) 8027c478bd9Sstevel@tonic-gate { 8037c478bd9Sstevel@tonic-gate char *buffer, *limit; 8047c478bd9Sstevel@tonic-gate int buflen = res->buflen; 8057c478bd9Sstevel@tonic-gate struct in6_addr *addr6p; 8067c478bd9Sstevel@tonic-gate char *buff_locp; 8077c478bd9Sstevel@tonic-gate struct hostent *host; 8087c478bd9Sstevel@tonic-gate int count = 0, len, i; 8097c478bd9Sstevel@tonic-gate char *h_namep; 8107c478bd9Sstevel@tonic-gate 8117c478bd9Sstevel@tonic-gate if (he4 == NULL || res == NULL) { 8127c478bd9Sstevel@tonic-gate return (NULL); 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate limit = res->buffer + buflen; 8157c478bd9Sstevel@tonic-gate host = (struct hostent *)res->result; 8167c478bd9Sstevel@tonic-gate buffer = res->buffer; 8177c478bd9Sstevel@tonic-gate 8187c478bd9Sstevel@tonic-gate buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in6_addr)); 8197c478bd9Sstevel@tonic-gate host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **)); 8207c478bd9Sstevel@tonic-gate if ((char *)host->h_addr_list >= limit || 8217c478bd9Sstevel@tonic-gate buff_locp <= (char *)host->h_addr_list) { 8227c478bd9Sstevel@tonic-gate return (NULL); 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate if (he6 == NULL) { 8257c478bd9Sstevel@tonic-gate /* 8267c478bd9Sstevel@tonic-gate * If he6==NULL, map the v4 address into the v6 address format. 8277c478bd9Sstevel@tonic-gate * This is used for getipnodebyaddr() (single address, mapped or 8287c478bd9Sstevel@tonic-gate * compatible) or for v4 mapped for getipnodebyname(), which 8297c478bd9Sstevel@tonic-gate * could be multiple addresses. This could also be a literal 8307c478bd9Sstevel@tonic-gate * address string, which is why there is a inet_addr() call. 8317c478bd9Sstevel@tonic-gate */ 8327c478bd9Sstevel@tonic-gate for (i = 0; he4->h_addr_list[i] != NULL; i++) { 8337c478bd9Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 8347c478bd9Sstevel@tonic-gate if (buff_locp <= 8357c478bd9Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) { 8367c478bd9Sstevel@tonic-gate /* 8377c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 8387c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 8397c478bd9Sstevel@tonic-gate */ 8407c478bd9Sstevel@tonic-gate return (NULL); 8417c478bd9Sstevel@tonic-gate } 84261961e0fSrobinson /* LINTED pointer cast */ 8437c478bd9Sstevel@tonic-gate addr6p = (struct in6_addr *)buff_locp; 8447c478bd9Sstevel@tonic-gate host->h_addr_list[count] = (char *)addr6p; 8457c478bd9Sstevel@tonic-gate bzero(addr6p->s6_addr, sizeof (struct in6_addr)); 8467c478bd9Sstevel@tonic-gate if (mapped) { 8477c478bd9Sstevel@tonic-gate addr6p->s6_addr[10] = 0xff; 8487c478bd9Sstevel@tonic-gate addr6p->s6_addr[11] = 0xff; 8497c478bd9Sstevel@tonic-gate } 8507c478bd9Sstevel@tonic-gate bcopy((char *)he4->h_addr_list[i], 8517c478bd9Sstevel@tonic-gate &addr6p->s6_addr[12], sizeof (struct in_addr)); 8527c478bd9Sstevel@tonic-gate ++count; 8537c478bd9Sstevel@tonic-gate } 8547c478bd9Sstevel@tonic-gate /* 8557c478bd9Sstevel@tonic-gate * Set last array element to NULL and add cname as first alias 8567c478bd9Sstevel@tonic-gate */ 8577c478bd9Sstevel@tonic-gate host->h_addr_list[count] = NULL; 8587c478bd9Sstevel@tonic-gate host->h_aliases = host->h_addr_list + count + 1; 8597c478bd9Sstevel@tonic-gate count = 0; 8607c478bd9Sstevel@tonic-gate if ((int)(inet_addr(he4->h_name)) != -1) { 8617c478bd9Sstevel@tonic-gate /* 8627c478bd9Sstevel@tonic-gate * Literal address string, since we're mapping, we need the IPv6 8637c478bd9Sstevel@tonic-gate * V4 mapped literal address string for h_name. 8647c478bd9Sstevel@tonic-gate */ 8657c478bd9Sstevel@tonic-gate char tmpstr[128]; 86661961e0fSrobinson (void) inet_ntop(AF_INET6, host->h_addr_list[0], tmpstr, 8677c478bd9Sstevel@tonic-gate sizeof (tmpstr)); 8687c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(tmpstr) + 1); 8697c478bd9Sstevel@tonic-gate h_namep = tmpstr; 8707c478bd9Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 8717c478bd9Sstevel@tonic-gate return (NULL); 8727c478bd9Sstevel@tonic-gate bcopy(h_namep, buff_locp, len); 8737c478bd9Sstevel@tonic-gate host->h_name = buff_locp; 8747c478bd9Sstevel@tonic-gate host->h_aliases = NULL; /* no aliases for literal */ 8757c478bd9Sstevel@tonic-gate host->h_length = sizeof (struct in6_addr); 8767c478bd9Sstevel@tonic-gate host->h_addrtype = AF_INET6; 8777c478bd9Sstevel@tonic-gate return (host); /* we're done, return result */ 8787c478bd9Sstevel@tonic-gate } 8797c478bd9Sstevel@tonic-gate /* 8807c478bd9Sstevel@tonic-gate * Not a literal address string, so just copy h_name. 8817c478bd9Sstevel@tonic-gate */ 8827c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he4->h_name) + 1); 8837c478bd9Sstevel@tonic-gate h_namep = he4->h_name; 8847c478bd9Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 8857c478bd9Sstevel@tonic-gate return (NULL); 8867c478bd9Sstevel@tonic-gate bcopy(h_namep, buff_locp, len); 8877c478bd9Sstevel@tonic-gate host->h_name = buff_locp; 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 } else { 9107c478bd9Sstevel@tonic-gate /* 9117c478bd9Sstevel@tonic-gate * Merge IPv4 mapped addresses with IPv6 addresses. The 9127c478bd9Sstevel@tonic-gate * IPv6 address will go in first, followed by the v4 mapped. 9137c478bd9Sstevel@tonic-gate * 9147c478bd9Sstevel@tonic-gate * Pass 1 (IPv6 addresses): 9157c478bd9Sstevel@tonic-gate */ 9167c478bd9Sstevel@tonic-gate for (i = 0; he6->h_addr_list[i] != NULL; i++) { 9177c478bd9Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 9187c478bd9Sstevel@tonic-gate if (buff_locp <= 9197c478bd9Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) { 9207c478bd9Sstevel@tonic-gate /* 9217c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 9227c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 9237c478bd9Sstevel@tonic-gate */ 9247c478bd9Sstevel@tonic-gate return (NULL); 9257c478bd9Sstevel@tonic-gate } 9267c478bd9Sstevel@tonic-gate host->h_addr_list[count] = buff_locp; 9277c478bd9Sstevel@tonic-gate bcopy((char *)he6->h_addr_list[i], buff_locp, 9287c478bd9Sstevel@tonic-gate sizeof (struct in6_addr)); 9297c478bd9Sstevel@tonic-gate ++count; 9307c478bd9Sstevel@tonic-gate } 9317c478bd9Sstevel@tonic-gate /* 9327c478bd9Sstevel@tonic-gate * Pass 1 (IPv4 mapped addresses): 9337c478bd9Sstevel@tonic-gate */ 9347c478bd9Sstevel@tonic-gate for (i = 0; he4->h_addr_list[i] != NULL; i++) { 9357c478bd9Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 9367c478bd9Sstevel@tonic-gate if (buff_locp <= 9377c478bd9Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) { 9387c478bd9Sstevel@tonic-gate /* 9397c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 9407c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 9417c478bd9Sstevel@tonic-gate */ 9427c478bd9Sstevel@tonic-gate return (NULL); 9437c478bd9Sstevel@tonic-gate } 94461961e0fSrobinson /* LINTED pointer cast */ 9457c478bd9Sstevel@tonic-gate addr6p = (struct in6_addr *)buff_locp; 9467c478bd9Sstevel@tonic-gate host->h_addr_list[count] = (char *)addr6p; 9477c478bd9Sstevel@tonic-gate bzero(addr6p->s6_addr, sizeof (struct in6_addr)); 9487c478bd9Sstevel@tonic-gate addr6p->s6_addr[10] = 0xff; 9497c478bd9Sstevel@tonic-gate addr6p->s6_addr[11] = 0xff; 9507c478bd9Sstevel@tonic-gate bcopy(he4->h_addr_list[i], &addr6p->s6_addr[12], 9517c478bd9Sstevel@tonic-gate sizeof (struct in_addr)); 9527c478bd9Sstevel@tonic-gate ++count; 9537c478bd9Sstevel@tonic-gate } 9547c478bd9Sstevel@tonic-gate /* 9557c478bd9Sstevel@tonic-gate * Pass 2 (IPv6 aliases, host name first). We start h_aliases 9567c478bd9Sstevel@tonic-gate * one after where h_addr_list array ended. This is where cname 9577c478bd9Sstevel@tonic-gate * is put, followed by all aliases. Reset count to 0, for index 9587c478bd9Sstevel@tonic-gate * in the h_aliases array. 9597c478bd9Sstevel@tonic-gate */ 9607c478bd9Sstevel@tonic-gate host->h_addr_list[count] = NULL; 9617c478bd9Sstevel@tonic-gate host->h_aliases = host->h_addr_list + count + 1; 9627c478bd9Sstevel@tonic-gate count = 0; 9637c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he6->h_name) + 1); 9647c478bd9Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 9657c478bd9Sstevel@tonic-gate return (NULL); 9667c478bd9Sstevel@tonic-gate bcopy(he6->h_name, buff_locp, len); 9677c478bd9Sstevel@tonic-gate host->h_name = buff_locp; 9687c478bd9Sstevel@tonic-gate for (i = 0; he6->h_aliases[i] != NULL; i++) { 9697c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he6->h_aliases[i]) + 1); 9707c478bd9Sstevel@tonic-gate if (buff_locp <= 9717c478bd9Sstevel@tonic-gate (char *)&(host->h_aliases[count + 1])) { 9727c478bd9Sstevel@tonic-gate /* 9737c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 9747c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 9757c478bd9Sstevel@tonic-gate */ 9767c478bd9Sstevel@tonic-gate return (NULL); 9777c478bd9Sstevel@tonic-gate } 9787c478bd9Sstevel@tonic-gate host->h_aliases[count] = buff_locp; 9797c478bd9Sstevel@tonic-gate bcopy((char *)he6->h_aliases[i], buff_locp, len); 9807c478bd9Sstevel@tonic-gate ++count; 9817c478bd9Sstevel@tonic-gate } 9827c478bd9Sstevel@tonic-gate /* 9837c478bd9Sstevel@tonic-gate * Pass 2 (IPv4 aliases): 9847c478bd9Sstevel@tonic-gate */ 9857c478bd9Sstevel@tonic-gate for (i = 0; he4->h_aliases[i] != NULL; i++) { 9867c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he4->h_aliases[i]) + 1); 9877c478bd9Sstevel@tonic-gate if (buff_locp <= 9887c478bd9Sstevel@tonic-gate (char *)&(host->h_aliases[count + 1])) { 9897c478bd9Sstevel@tonic-gate /* 9907c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 9917c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 9927c478bd9Sstevel@tonic-gate */ 9937c478bd9Sstevel@tonic-gate return (NULL); 9947c478bd9Sstevel@tonic-gate } 9957c478bd9Sstevel@tonic-gate host->h_aliases[count] = buff_locp; 9967c478bd9Sstevel@tonic-gate bcopy((char *)he4->h_aliases[i], buff_locp, len); 9977c478bd9Sstevel@tonic-gate ++count; 9987c478bd9Sstevel@tonic-gate } 9997c478bd9Sstevel@tonic-gate host->h_aliases[count] = NULL; 10007c478bd9Sstevel@tonic-gate host->h_length = sizeof (struct in6_addr); 10017c478bd9Sstevel@tonic-gate host->h_addrtype = AF_INET6; 10027c478bd9Sstevel@tonic-gate return (host); 10037c478bd9Sstevel@tonic-gate } 10047c478bd9Sstevel@tonic-gate } 10057c478bd9Sstevel@tonic-gate 10067c478bd9Sstevel@tonic-gate /* 10077c478bd9Sstevel@tonic-gate * This routine will convert a mapped v4 hostent (AF_INET6) to a 10087c478bd9Sstevel@tonic-gate * AF_INET hostent. If no mapped addrs found, then a NULL is returned. 10097c478bd9Sstevel@tonic-gate * If mapped addrs found, then a new buffer is alloc'd and all the v4 mapped 10107c478bd9Sstevel@tonic-gate * addresses are extracted and copied to it. On sucess, a pointer to a new 10117c478bd9Sstevel@tonic-gate * hostent is returned. 10127c478bd9Sstevel@tonic-gate * There are two possible errors in which case a NULL is returned. 10137c478bd9Sstevel@tonic-gate * One of two error codes are returned: 10147c478bd9Sstevel@tonic-gate * 10157c478bd9Sstevel@tonic-gate * NO_RECOVERY - a malloc failed or the like for which there's no recovery. 10167c478bd9Sstevel@tonic-gate * NO_ADDRESS - after filtering all the v4, there was nothing left! 10177c478bd9Sstevel@tonic-gate * 10187c478bd9Sstevel@tonic-gate * Inputs: 10197c478bd9Sstevel@tonic-gate * he pointer to hostent with mapped v4 addresses 10207c478bd9Sstevel@tonic-gate * filter_error pointer to return error code 10217c478bd9Sstevel@tonic-gate * Return: 10227c478bd9Sstevel@tonic-gate * pointer to a malloc'd hostent with v4 addresses. 10237c478bd9Sstevel@tonic-gate * 10247c478bd9Sstevel@tonic-gate * The results are packed into the res->buffer as follows: 10257c478bd9Sstevel@tonic-gate * <--------------- buffer + buflen --------------------------------------> 10267c478bd9Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 10277c478bd9Sstevel@tonic-gate * | pointers vector | pointers vector | aliases grow | addresses grow | 10287c478bd9Sstevel@tonic-gate * | for addresses | for aliases | | | 10297c478bd9Sstevel@tonic-gate * | this way -> | this way -> | <- this way |<- this way | 10307c478bd9Sstevel@tonic-gate * |-----------------|-----------------|----------------|----------------| 10317c478bd9Sstevel@tonic-gate * | grows in PASS 1 | grows in PASS2 | grows in PASS2 | grows in PASS 1| 10327c478bd9Sstevel@tonic-gate */ 10337c478bd9Sstevel@tonic-gate struct hostent * 10347c478bd9Sstevel@tonic-gate __mappedtov4(struct hostent *he, int *extract_error) 10357c478bd9Sstevel@tonic-gate { 10367c478bd9Sstevel@tonic-gate char *buffer, *limit; 10377c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *res; 10387c478bd9Sstevel@tonic-gate int buflen = NSS_BUFLEN_HOSTS; 10397c478bd9Sstevel@tonic-gate struct in_addr *addr4p; 10407c478bd9Sstevel@tonic-gate char *buff_locp; 10417c478bd9Sstevel@tonic-gate struct hostent *host; 10427c478bd9Sstevel@tonic-gate int count = 0, len, i; 10437c478bd9Sstevel@tonic-gate char *h_namep; 10447c478bd9Sstevel@tonic-gate 10457c478bd9Sstevel@tonic-gate if (he == NULL) { 10467c478bd9Sstevel@tonic-gate *extract_error = NO_ADDRESS; 10477c478bd9Sstevel@tonic-gate return (NULL); 10487c478bd9Sstevel@tonic-gate } 10497c478bd9Sstevel@tonic-gate if ((__find_mapped(he, 0)) == 0) { 10507c478bd9Sstevel@tonic-gate *extract_error = NO_ADDRESS; 10517c478bd9Sstevel@tonic-gate return (NULL); 10527c478bd9Sstevel@tonic-gate } 10537c478bd9Sstevel@tonic-gate if ((res = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == 0) { 10547c478bd9Sstevel@tonic-gate *extract_error = NO_RECOVERY; 10557c478bd9Sstevel@tonic-gate return (NULL); 10567c478bd9Sstevel@tonic-gate } 10577c478bd9Sstevel@tonic-gate limit = res->buffer + buflen; 10587c478bd9Sstevel@tonic-gate host = (struct hostent *)res->result; 10597c478bd9Sstevel@tonic-gate buffer = res->buffer; 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in_addr)); 10627c478bd9Sstevel@tonic-gate host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **)); 10637c478bd9Sstevel@tonic-gate if ((char *)host->h_addr_list >= limit || 10647c478bd9Sstevel@tonic-gate buff_locp <= (char *)host->h_addr_list) 10657c478bd9Sstevel@tonic-gate goto cleanup; 10667c478bd9Sstevel@tonic-gate /* 10677c478bd9Sstevel@tonic-gate * "Unmap" the v4 mapped address(es) into a v4 hostent format. 10687c478bd9Sstevel@tonic-gate * This is used for getipnodebyaddr() (single address) or for 10697c478bd9Sstevel@tonic-gate * v4 mapped for getipnodebyname(), which could be multiple 10707c478bd9Sstevel@tonic-gate * addresses. This could also be a literal address string, 10717c478bd9Sstevel@tonic-gate * which is why there is a inet_addr() call. 10727c478bd9Sstevel@tonic-gate */ 10737c478bd9Sstevel@tonic-gate for (i = 0; he->h_addr_list[i] != NULL; i++) { 107461961e0fSrobinson /* LINTED pointer cast */ 10757c478bd9Sstevel@tonic-gate if (!IN6_IS_ADDR_V4MAPPED((struct in6_addr *) 10767c478bd9Sstevel@tonic-gate he->h_addr_list[i])) 10777c478bd9Sstevel@tonic-gate continue; 10787c478bd9Sstevel@tonic-gate buff_locp -= sizeof (struct in6_addr); 10797c478bd9Sstevel@tonic-gate /* 10807c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 10817c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 10827c478bd9Sstevel@tonic-gate */ 10837c478bd9Sstevel@tonic-gate if (buff_locp <= 10847c478bd9Sstevel@tonic-gate (char *)&(host->h_addr_list[count + 1])) 10857c478bd9Sstevel@tonic-gate goto cleanup; 108661961e0fSrobinson /* LINTED pointer cast */ 10877c478bd9Sstevel@tonic-gate addr4p = (struct in_addr *)buff_locp; 10887c478bd9Sstevel@tonic-gate host->h_addr_list[count] = (char *)addr4p; 10897c478bd9Sstevel@tonic-gate bzero((char *)&addr4p->s_addr, 10907c478bd9Sstevel@tonic-gate sizeof (struct in_addr)); 109161961e0fSrobinson /* LINTED pointer cast */ 10927c478bd9Sstevel@tonic-gate IN6_V4MAPPED_TO_INADDR( 10935b79143cSGirish Moodalbail (struct in6_addr *)he->h_addr_list[i], addr4p); 10947c478bd9Sstevel@tonic-gate ++count; 10957c478bd9Sstevel@tonic-gate } 10967c478bd9Sstevel@tonic-gate /* 10977c478bd9Sstevel@tonic-gate * Set last array element to NULL and add cname as first alias 10987c478bd9Sstevel@tonic-gate */ 10997c478bd9Sstevel@tonic-gate host->h_addr_list[count] = NULL; 11007c478bd9Sstevel@tonic-gate host->h_aliases = host->h_addr_list + count + 1; 11017c478bd9Sstevel@tonic-gate count = 0; 11027c478bd9Sstevel@tonic-gate /* Copy official host name */ 11037c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he->h_name) + 1); 11047c478bd9Sstevel@tonic-gate h_namep = he->h_name; 11057c478bd9Sstevel@tonic-gate if (buff_locp <= (char *)(host->h_aliases)) 11067c478bd9Sstevel@tonic-gate goto cleanup; 11077c478bd9Sstevel@tonic-gate bcopy(h_namep, buff_locp, len); 11087c478bd9Sstevel@tonic-gate host->h_name = buff_locp; 11097c478bd9Sstevel@tonic-gate /* 11107c478bd9Sstevel@tonic-gate * Pass 2 (IPv4 aliases): 11117c478bd9Sstevel@tonic-gate */ 11125b79143cSGirish Moodalbail if (he->h_aliases != NULL) { 11137c478bd9Sstevel@tonic-gate for (i = 0; he->h_aliases[i] != NULL; i++) { 11147c478bd9Sstevel@tonic-gate buff_locp -= (len = strlen(he->h_aliases[i]) + 1); 11157c478bd9Sstevel@tonic-gate /* 11167c478bd9Sstevel@tonic-gate * Has to be room for the pointer to the address we're 11177c478bd9Sstevel@tonic-gate * about to add, as well as the final NULL ptr. 11187c478bd9Sstevel@tonic-gate */ 11197c478bd9Sstevel@tonic-gate if (buff_locp <= 11207c478bd9Sstevel@tonic-gate (char *)&(host->h_aliases[count + 1])) 11217c478bd9Sstevel@tonic-gate goto cleanup; 11227c478bd9Sstevel@tonic-gate host->h_aliases[count] = buff_locp; 11237c478bd9Sstevel@tonic-gate bcopy((char *)he->h_aliases[i], buff_locp, len); 11247c478bd9Sstevel@tonic-gate ++count; 11257c478bd9Sstevel@tonic-gate } 11265b79143cSGirish Moodalbail } 11277c478bd9Sstevel@tonic-gate host->h_aliases[count] = NULL; 11287c478bd9Sstevel@tonic-gate host->h_length = sizeof (struct in_addr); 11297c478bd9Sstevel@tonic-gate host->h_addrtype = AF_INET; 11307c478bd9Sstevel@tonic-gate free(res); 11317c478bd9Sstevel@tonic-gate return (host); 11327c478bd9Sstevel@tonic-gate cleanup: 11337c478bd9Sstevel@tonic-gate *extract_error = NO_RECOVERY; 11347c478bd9Sstevel@tonic-gate (void) __IPv6_cleanup(res); 11357c478bd9Sstevel@tonic-gate return (NULL); 11367c478bd9Sstevel@tonic-gate } 11377c478bd9Sstevel@tonic-gate 11387c478bd9Sstevel@tonic-gate /* 11397c478bd9Sstevel@tonic-gate * This routine takes as input a pointer to a hostent and filters out 11407c478bd9Sstevel@tonic-gate * the type of addresses specified by the af argument. AF_INET 11417c478bd9Sstevel@tonic-gate * indicates that the caller wishes to filter out IPv4-mapped 11427c478bd9Sstevel@tonic-gate * addresses, and AF_INET6 indicates that the caller wishes to filter 11437c478bd9Sstevel@tonic-gate * out IPv6 addresses which aren't IPv4-mapped. If filtering would 11447c478bd9Sstevel@tonic-gate * result in all addresses being filtered out, a NULL pointer is returned. 11457c478bd9Sstevel@tonic-gate * Otherwise, the he pointer passed in is returned, even if no addresses 11467c478bd9Sstevel@tonic-gate * were filtered out. 11477c478bd9Sstevel@tonic-gate */ 11487c478bd9Sstevel@tonic-gate static struct hostent * 11497c478bd9Sstevel@tonic-gate __filter_addresses(int af, struct hostent *he) 11507c478bd9Sstevel@tonic-gate { 11517c478bd9Sstevel@tonic-gate struct in6_addr **in6addrlist, **in6addr; 11527c478bd9Sstevel@tonic-gate boolean_t isipv4mapped; 11537c478bd9Sstevel@tonic-gate int i = 0; 11547c478bd9Sstevel@tonic-gate 11557c478bd9Sstevel@tonic-gate if (he == NULL) 11567c478bd9Sstevel@tonic-gate return (NULL); 11577c478bd9Sstevel@tonic-gate 11587c478bd9Sstevel@tonic-gate in6addrlist = (struct in6_addr **)he->h_addr_list; 11597c478bd9Sstevel@tonic-gate for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) { 11607c478bd9Sstevel@tonic-gate isipv4mapped = IN6_IS_ADDR_V4MAPPED(*in6addr); 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate if ((af == AF_INET && !isipv4mapped) || 11637c478bd9Sstevel@tonic-gate (af == AF_INET6 && isipv4mapped)) { 11647c478bd9Sstevel@tonic-gate if (in6addrlist[i] != *in6addr) 11657c478bd9Sstevel@tonic-gate in6addrlist[i] = *in6addr; 11667c478bd9Sstevel@tonic-gate i++; 11677c478bd9Sstevel@tonic-gate } 11687c478bd9Sstevel@tonic-gate } 11697c478bd9Sstevel@tonic-gate 11707c478bd9Sstevel@tonic-gate if (i == 0) { 11717c478bd9Sstevel@tonic-gate /* We filtered everything out. */ 11727c478bd9Sstevel@tonic-gate return (NULL); 11737c478bd9Sstevel@tonic-gate } else { 11747c478bd9Sstevel@tonic-gate /* NULL terminate the list and return the hostent */ 11757c478bd9Sstevel@tonic-gate in6addrlist[i] = NULL; 11767c478bd9Sstevel@tonic-gate return (he); 11777c478bd9Sstevel@tonic-gate } 11787c478bd9Sstevel@tonic-gate } 11797c478bd9Sstevel@tonic-gate 11807c478bd9Sstevel@tonic-gate /* 11817c478bd9Sstevel@tonic-gate * This routine searches a hostent for v4 mapped IPv6 addresses. 11827c478bd9Sstevel@tonic-gate * he hostent structure to seach 11837c478bd9Sstevel@tonic-gate * find_both flag indicating if only want mapped or both map'd and v6 11847c478bd9Sstevel@tonic-gate * return values: 11857c478bd9Sstevel@tonic-gate * 0 = No mapped addresses 11867c478bd9Sstevel@tonic-gate * 1 = Mapped v4 address found (returns on first one found) 11877c478bd9Sstevel@tonic-gate * 2 = Both v6 and v4 mapped are present 11887c478bd9Sstevel@tonic-gate * 11897c478bd9Sstevel@tonic-gate * If hostent passed in with no addresses, zero will be returned. 11907c478bd9Sstevel@tonic-gate */ 11917c478bd9Sstevel@tonic-gate 11927c478bd9Sstevel@tonic-gate static int 11937c478bd9Sstevel@tonic-gate __find_mapped(struct hostent *he, int find_both) 11947c478bd9Sstevel@tonic-gate { 11957c478bd9Sstevel@tonic-gate int i; 11967c478bd9Sstevel@tonic-gate int mapd_found = 0; 11977c478bd9Sstevel@tonic-gate int v6_found = 0; 11987c478bd9Sstevel@tonic-gate 11997c478bd9Sstevel@tonic-gate for (i = 0; he->h_addr_list[i] != NULL; i++) { 120061961e0fSrobinson /* LINTED pointer cast */ 12017c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED( 12027c478bd9Sstevel@tonic-gate (struct in6_addr *)he->h_addr_list[i])) { 12037c478bd9Sstevel@tonic-gate if (find_both) 12047c478bd9Sstevel@tonic-gate mapd_found = 1; 12057c478bd9Sstevel@tonic-gate else 12067c478bd9Sstevel@tonic-gate return (1); 12077c478bd9Sstevel@tonic-gate } else { 12087c478bd9Sstevel@tonic-gate v6_found = 1; 12097c478bd9Sstevel@tonic-gate } 12107c478bd9Sstevel@tonic-gate /* save some iterations once both found */ 12117c478bd9Sstevel@tonic-gate if (mapd_found && v6_found) 12127c478bd9Sstevel@tonic-gate return (2); 12137c478bd9Sstevel@tonic-gate } 12147c478bd9Sstevel@tonic-gate return (mapd_found); 12157c478bd9Sstevel@tonic-gate } 12167c478bd9Sstevel@tonic-gate 12177c478bd9Sstevel@tonic-gate /* 12187c478bd9Sstevel@tonic-gate * This routine was added specifically for the IPv6 getipnodeby*() APIs. This 12197c478bd9Sstevel@tonic-gate * separates the result pointer (ptr to hostent+data buf) from the 12207c478bd9Sstevel@tonic-gate * nss_XbyY_buf_t ptr (required for nsswitch API). The returned hostent ptr 12217c478bd9Sstevel@tonic-gate * can be passed to freehostent() and freed independently. 12227c478bd9Sstevel@tonic-gate * 12237c478bd9Sstevel@tonic-gate * bufp->result bufp->buffer 12247c478bd9Sstevel@tonic-gate * | | 12257c478bd9Sstevel@tonic-gate * V V 12267c478bd9Sstevel@tonic-gate * ------------------------------------------------...-- 12277c478bd9Sstevel@tonic-gate * |struct hostent |addresses aliases | 12287c478bd9Sstevel@tonic-gate * ------------------------------------------------...-- 12297c478bd9Sstevel@tonic-gate * | |<--------bufp->buflen-------------->| 12307c478bd9Sstevel@tonic-gate */ 12317c478bd9Sstevel@tonic-gate 12327c478bd9Sstevel@tonic-gate #define ALIGN(x) ((((long)(x)) + sizeof (long) - 1) & ~(sizeof (long) - 1)) 12337c478bd9Sstevel@tonic-gate 12347c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t * 12357c478bd9Sstevel@tonic-gate __IPv6_alloc(int bufsz) 12367c478bd9Sstevel@tonic-gate { 12377c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *bufp; 12387c478bd9Sstevel@tonic-gate 12397c478bd9Sstevel@tonic-gate if ((bufp = malloc(sizeof (nss_XbyY_buf_t))) == NULL) 12407c478bd9Sstevel@tonic-gate return (NULL); 12417c478bd9Sstevel@tonic-gate 12427c478bd9Sstevel@tonic-gate if ((bufp->result = malloc(ALIGN(sizeof (struct hostent)) + bufsz)) == 12437c478bd9Sstevel@tonic-gate NULL) { 12447c478bd9Sstevel@tonic-gate free(bufp); 12457c478bd9Sstevel@tonic-gate return (NULL); 12467c478bd9Sstevel@tonic-gate } 12477c478bd9Sstevel@tonic-gate bufp->buffer = (char *)(bufp->result) + sizeof (struct hostent); 12487c478bd9Sstevel@tonic-gate bufp->buflen = bufsz; 12497c478bd9Sstevel@tonic-gate return (bufp); 12507c478bd9Sstevel@tonic-gate } 12517c478bd9Sstevel@tonic-gate 12527c478bd9Sstevel@tonic-gate /* 12537c478bd9Sstevel@tonic-gate * This routine is use only for error return cleanup. This will free the 12547c478bd9Sstevel@tonic-gate * hostent pointer, so don't use for successful returns. 12557c478bd9Sstevel@tonic-gate */ 12567c478bd9Sstevel@tonic-gate static void 12577c478bd9Sstevel@tonic-gate __IPv6_cleanup(nss_XbyY_buf_t *bufp) 12587c478bd9Sstevel@tonic-gate { 12597c478bd9Sstevel@tonic-gate if (bufp == NULL) 12607c478bd9Sstevel@tonic-gate return; 12617c478bd9Sstevel@tonic-gate if (bufp->result != NULL) 12627c478bd9Sstevel@tonic-gate free(bufp->result); 12637c478bd9Sstevel@tonic-gate free(bufp); 12647c478bd9Sstevel@tonic-gate } 1265