10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1914Scasper * Common Development and Distribution License (the "License"). 6*1914Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21132Srobinson 220Sstevel@tonic-gate /* 231229Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 251229Sraf */ 261229Sraf 271229Sraf /* 280Sstevel@tonic-gate * This is where we have chosen to combine every useful bit of code for 290Sstevel@tonic-gate * all the Solaris frontends to lookup hosts, services, and netdir information 300Sstevel@tonic-gate * for inet family (udp, tcp) transports. gethostbyYY(), getservbyYY(), and 310Sstevel@tonic-gate * netdir_getbyYY() are all implemented on top of this code. Similarly, 320Sstevel@tonic-gate * netdir_options, taddr2uaddr, and uaddr2taddr for inet transports also 330Sstevel@tonic-gate * find a home here. 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * If the netconfig structure supplied has NO nametoaddr libs (i.e. a "-" 360Sstevel@tonic-gate * in /etc/netconfig), this code calls the name service switch, and 370Sstevel@tonic-gate * therefore, /etc/nsswitch.conf is effectively the only place that 380Sstevel@tonic-gate * dictates hosts/serv lookup policy. 390Sstevel@tonic-gate * If an administrator chooses to bypass the name service switch by 400Sstevel@tonic-gate * specifying third party supplied nametoaddr libs in /etc/netconfig, this 410Sstevel@tonic-gate * implementation does NOT call the name service switch, it merely loops 420Sstevel@tonic-gate * through the nametoaddr libs. In this case, if this code was called 430Sstevel@tonic-gate * from gethost/servbyYY() we marshal the inet specific struct into 440Sstevel@tonic-gate * transport independent netbuf or hostserv, and unmarshal the resulting 450Sstevel@tonic-gate * nd_addrlist or hostservlist back into hostent and servent, as the case 460Sstevel@tonic-gate * may be. 470Sstevel@tonic-gate * 480Sstevel@tonic-gate * Goes without saying that most of the future bugs in gethost/servbyYY 490Sstevel@tonic-gate * and netdir_getbyYY are lurking somewhere here. 500Sstevel@tonic-gate */ 510Sstevel@tonic-gate 520Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 530Sstevel@tonic-gate 540Sstevel@tonic-gate #include "mt.h" 550Sstevel@tonic-gate #include <ctype.h> 560Sstevel@tonic-gate #include <stdio.h> 570Sstevel@tonic-gate #include <stdlib.h> 580Sstevel@tonic-gate #include <string.h> 590Sstevel@tonic-gate #include <unistd.h> 600Sstevel@tonic-gate #include <stropts.h> 610Sstevel@tonic-gate #include <sys/types.h> 620Sstevel@tonic-gate #include <sys/byteorder.h> 630Sstevel@tonic-gate #include <sys/ioctl.h> 640Sstevel@tonic-gate #include <sys/param.h> 650Sstevel@tonic-gate #include <sys/time.h> 660Sstevel@tonic-gate #include <errno.h> 670Sstevel@tonic-gate #include <fcntl.h> 680Sstevel@tonic-gate #include <thread.h> 690Sstevel@tonic-gate #include <synch.h> 700Sstevel@tonic-gate #include <sys/utsname.h> 710Sstevel@tonic-gate #include <netdb.h> 720Sstevel@tonic-gate #include <netconfig.h> 730Sstevel@tonic-gate #include <netdir.h> 740Sstevel@tonic-gate #include <tiuser.h> 750Sstevel@tonic-gate #include <sys/socket.h> 760Sstevel@tonic-gate #include <sys/sockio.h> 770Sstevel@tonic-gate #include <netinet/in.h> 780Sstevel@tonic-gate #include <arpa/inet.h> 790Sstevel@tonic-gate #include <net/if.h> 800Sstevel@tonic-gate #include <inet/ip.h> 810Sstevel@tonic-gate #include <inet/ip6_asp.h> 820Sstevel@tonic-gate #include <sys/dlpi.h> 830Sstevel@tonic-gate #include <nss_dbdefs.h> 840Sstevel@tonic-gate #include <nss_netdir.h> 850Sstevel@tonic-gate #include <syslog.h> 860Sstevel@tonic-gate #include <nsswitch.h> 870Sstevel@tonic-gate #include "nss.h" 880Sstevel@tonic-gate 890Sstevel@tonic-gate #define MAXIFS 32 900Sstevel@tonic-gate #define UDPDEV "/dev/udp" 910Sstevel@tonic-gate #define UDP6DEV "/dev/udp6" 920Sstevel@tonic-gate 930Sstevel@tonic-gate #ifdef PIC 940Sstevel@tonic-gate #define DOOR_GETHOSTBYNAME_R _door_gethostbyname_r 950Sstevel@tonic-gate #define DOOR_GETHOSTBYADDR_R _door_gethostbyaddr_r 960Sstevel@tonic-gate #define DOOR_GETIPNODEBYNAME_R _door_getipnodebyname_r 970Sstevel@tonic-gate #define DOOR_GETIPNODEBYADDR_R _door_getipnodebyaddr_r 980Sstevel@tonic-gate #else 990Sstevel@tonic-gate #define DOOR_GETHOSTBYNAME_R _switch_gethostbyname_r 1000Sstevel@tonic-gate #define DOOR_GETHOSTBYADDR_R _switch_gethostbyaddr_r 1010Sstevel@tonic-gate #define DOOR_GETIPNODEBYNAME_R _switch_getipnodebyname_r 1020Sstevel@tonic-gate #define DOOR_GETIPNODEBYADDR_R _switch_getipnodebyaddr_r 1030Sstevel@tonic-gate #endif /* PIC */ 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate #define DONT_SORT "SORT_ADDRS=NO" 1060Sstevel@tonic-gate #define DONT_SORT2 "SORT_ADDRS=FALSE" 1070Sstevel@tonic-gate #define LINESIZE 100 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* 1100Sstevel@tonic-gate * constant values of addresses for HOST_SELF_BIND, HOST_SELF_CONNECT 1110Sstevel@tonic-gate * and localhost. 1120Sstevel@tonic-gate * 1130Sstevel@tonic-gate * The following variables are static to the extent that they should 1140Sstevel@tonic-gate * not be visible outside of this file. 1150Sstevel@tonic-gate */ 1160Sstevel@tonic-gate static char *localaddr[] = {"\000\000\000\000", NULL}; 1170Sstevel@tonic-gate static char *connectaddr[] = {"\177\000\000\001", NULL}; 1180Sstevel@tonic-gate static char *localaddr6[] = 1190Sstevel@tonic-gate {"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000", NULL}; 1200Sstevel@tonic-gate static char *connectaddr6[] = 1210Sstevel@tonic-gate {"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001", NULL}; 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate /* IPv4 nd_addrlist */ 1240Sstevel@tonic-gate static mutex_t nd_addr_lock = DEFAULTMUTEX; 1250Sstevel@tonic-gate static struct sockaddr_in sa_con; 1260Sstevel@tonic-gate static struct netbuf nd_conbuf = {sizeof (sa_con),\ 1270Sstevel@tonic-gate sizeof (sa_con), (char *)&sa_con}; 1280Sstevel@tonic-gate static struct nd_addrlist nd_conaddrlist = {1, &nd_conbuf}; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* IPv6 nd_addrlist */ 1310Sstevel@tonic-gate static mutex_t nd6_addr_lock = DEFAULTMUTEX; 1320Sstevel@tonic-gate static struct sockaddr_in6 sa6_con; 1330Sstevel@tonic-gate static struct netbuf nd6_conbuf = {sizeof (sa6_con),\ 1340Sstevel@tonic-gate sizeof (sa6_con), (char *)&sa6_con}; 1350Sstevel@tonic-gate static struct nd_addrlist nd6_conaddrlist = {1, &nd6_conbuf}; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate #define LOCALHOST "localhost" 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate struct servent *_switch_getservbyname_r(const char *, const char *, 1400Sstevel@tonic-gate struct servent *, char *, int); 1410Sstevel@tonic-gate struct servent *_switch_getservbyport_r(int, const char *, struct servent *, 1420Sstevel@tonic-gate char *, int); 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate static int __herrno2netdir(int h_errnop); 1450Sstevel@tonic-gate static struct ifinfo *get_local_info(void); 1460Sstevel@tonic-gate static int getbroadcastnets(struct netconfig *, struct in_addr **); 1470Sstevel@tonic-gate static int hent2ndaddr(int, char **, int *, struct nd_addrlist **); 1480Sstevel@tonic-gate static int ndaddr2hent(int, const char *, struct nd_addrlist *, 1490Sstevel@tonic-gate struct hostent *, char *, int); 1500Sstevel@tonic-gate static int hsents2ndhostservs(struct hostent *, struct servent *, ushort_t, 1510Sstevel@tonic-gate struct nd_hostservlist **); 1520Sstevel@tonic-gate static int ndaddr2srent(const char *, const char *, ushort_t, struct servent *, 1530Sstevel@tonic-gate char *, int); 1540Sstevel@tonic-gate static int ndhostserv2hent(struct netbuf *, struct nd_hostservlist *, 1550Sstevel@tonic-gate struct hostent *, char *, int); 1560Sstevel@tonic-gate static int ndhostserv2srent(int, const char *, struct nd_hostservlist *, 1570Sstevel@tonic-gate struct servent *, char *, int); 1580Sstevel@tonic-gate static int nd2herrno(int nerr); 1590Sstevel@tonic-gate static void order_haddrlist_inet(char **haddrlist, size_t addrcount); 1600Sstevel@tonic-gate static void order_haddrlist_inet6(char **haddrlist, size_t addrcount); 1610Sstevel@tonic-gate static int dstcmp(const void *, const void *); 1620Sstevel@tonic-gate static int nss_strioctl(int af, int cmd, void *ptr, int ilen); 1630Sstevel@tonic-gate static struct in_addr _inet_makeaddr(in_addr_t, in_addr_t); 1640Sstevel@tonic-gate static boolean_t _read_nsw_file(void); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate /* 1670Sstevel@tonic-gate * Begin: PART I 1680Sstevel@tonic-gate * Top Level Interfaces that gethost/serv/netdir funnel through. 1690Sstevel@tonic-gate */ 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate /* 1720Sstevel@tonic-gate * gethost/servbyname always call this function; if they call 1730Sstevel@tonic-gate * with nametoaddr libs in nconf, we call netdir_getbyname 1740Sstevel@tonic-gate * implementation: __classic_netdir_getbyname, otherwise nsswitch. 1750Sstevel@tonic-gate * 1760Sstevel@tonic-gate * netdir_getbyname calls this only if nametoaddr libs are NOT 1770Sstevel@tonic-gate * specified for inet transports; i.e. it's supposed to follow 1780Sstevel@tonic-gate * the name service switch. 1790Sstevel@tonic-gate */ 1800Sstevel@tonic-gate int 1810Sstevel@tonic-gate _get_hostserv_inetnetdir_byname(struct netconfig *nconf, 1820Sstevel@tonic-gate struct nss_netdirbyname_in *args, union nss_netdirbyname_out *res) 1830Sstevel@tonic-gate { 1840Sstevel@tonic-gate int server_port; 1850Sstevel@tonic-gate int *servp = &server_port; 1860Sstevel@tonic-gate char **haddrlist; 1870Sstevel@tonic-gate uint32_t dotnameaddr; 1880Sstevel@tonic-gate char *dotnamelist[2]; 1890Sstevel@tonic-gate struct in_addr *inaddrs = NULL; 1900Sstevel@tonic-gate struct in6_addr v6nameaddr; 1910Sstevel@tonic-gate char **baddrlist = NULL; 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate if (nconf == NULL) { 1950Sstevel@tonic-gate _nderror = ND_BADARG; 1960Sstevel@tonic-gate return (ND_BADARG); 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * 1. gethostbyname()/netdir_getbyname() special cases: 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate switch (args->op_t) { 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate case NSS_HOST: 2050Sstevel@tonic-gate /* 2060Sstevel@tonic-gate * Worth the performance gain -- assuming a lot of inet apps 2070Sstevel@tonic-gate * actively use "localhost". 2080Sstevel@tonic-gate */ 2090Sstevel@tonic-gate if (strcmp(args->arg.nss.host.name, LOCALHOST) == 0) { 2100Sstevel@tonic-gate 211132Srobinson (void) mutex_lock(&nd_addr_lock); 2120Sstevel@tonic-gate IN_SET_LOOPBACK_ADDR(&sa_con); 2130Sstevel@tonic-gate _nderror = ndaddr2hent(AF_INET, args->arg.nss.host.name, 2140Sstevel@tonic-gate &nd_conaddrlist, res->nss.host.hent, 2150Sstevel@tonic-gate args->arg.nss.host.buf, 2160Sstevel@tonic-gate args->arg.nss.host.buflen); 217132Srobinson (void) mutex_unlock(&nd_addr_lock); 2180Sstevel@tonic-gate if (_nderror != ND_OK) 2190Sstevel@tonic-gate *(res->nss.host.herrno_p) = 2200Sstevel@tonic-gate nd2herrno(_nderror); 2210Sstevel@tonic-gate return (_nderror); 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate /* 2240Sstevel@tonic-gate * If the caller passed in a dot separated IP notation to 2250Sstevel@tonic-gate * gethostbyname, return that back as the address. 2260Sstevel@tonic-gate * The nd_addr_lock mutex was added to be truely re-entrant. 2270Sstevel@tonic-gate */ 2281229Sraf if (inet_aton(args->arg.nss.host.name, 2290Sstevel@tonic-gate (struct in_addr *)&dotnameaddr)) { 230132Srobinson (void) mutex_lock(&nd_addr_lock); 231132Srobinson (void) memset(&sa_con, 0, sizeof (sa_con)); 2320Sstevel@tonic-gate sa_con.sin_family = AF_INET; 2330Sstevel@tonic-gate sa_con.sin_addr.s_addr = dotnameaddr; 2340Sstevel@tonic-gate _nderror = ndaddr2hent(AF_INET, args->arg.nss.host.name, 2350Sstevel@tonic-gate &nd_conaddrlist, res->nss.host.hent, 2360Sstevel@tonic-gate args->arg.nss.host.buf, 2370Sstevel@tonic-gate args->arg.nss.host.buflen); 238132Srobinson (void) mutex_unlock(&nd_addr_lock); 2390Sstevel@tonic-gate if (_nderror != ND_OK) 2400Sstevel@tonic-gate *(res->nss.host.herrno_p) = 2410Sstevel@tonic-gate nd2herrno(_nderror); 2420Sstevel@tonic-gate return (_nderror); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate break; 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate case NSS_HOST6: 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * Handle case of literal address string. 2490Sstevel@tonic-gate */ 2500Sstevel@tonic-gate if (strchr(args->arg.nss.host6.name, ':') != NULL && 2510Sstevel@tonic-gate (inet_pton(AF_INET6, args->arg.nss.host6.name, 2520Sstevel@tonic-gate &v6nameaddr) != 0)) { 2530Sstevel@tonic-gate int ret; 2540Sstevel@tonic-gate 255132Srobinson (void) mutex_lock(&nd6_addr_lock); 256132Srobinson (void) memset(&sa6_con, 0, sizeof (sa6_con)); 2570Sstevel@tonic-gate sa6_con.sin6_family = AF_INET6; 258132Srobinson (void) memcpy(&(sa6_con.sin6_addr.s6_addr), 2590Sstevel@tonic-gate &v6nameaddr, sizeof (struct in6_addr)); 2600Sstevel@tonic-gate ret = ndaddr2hent(AF_INET6, 2610Sstevel@tonic-gate args->arg.nss.host6.name, 2620Sstevel@tonic-gate &nd6_conaddrlist, res->nss.host.hent, 2630Sstevel@tonic-gate args->arg.nss.host6.buf, 2640Sstevel@tonic-gate args->arg.nss.host6.buflen); 265132Srobinson (void) mutex_unlock(&nd6_addr_lock); 2660Sstevel@tonic-gate if (ret != ND_OK) 2670Sstevel@tonic-gate *(res->nss.host.herrno_p) = nd2herrno(ret); 2680Sstevel@tonic-gate else 2690Sstevel@tonic-gate res->nss.host.hent->h_aliases = NULL; 2700Sstevel@tonic-gate return (ret); 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate break; 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate case NETDIR_BY: 2750Sstevel@tonic-gate if (args->arg.nd_hs == 0) { 2760Sstevel@tonic-gate _nderror = ND_BADARG; 2770Sstevel@tonic-gate return (ND_BADARG); 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate /* 2800Sstevel@tonic-gate * If servname is NULL, return 0 as the port number 2810Sstevel@tonic-gate * If servname is rpcbind, return 111 as the port number 2820Sstevel@tonic-gate * If servname is a number, return it back as the port 2830Sstevel@tonic-gate * number. 2840Sstevel@tonic-gate */ 2850Sstevel@tonic-gate if (args->arg.nd_hs->h_serv == 0) { 2860Sstevel@tonic-gate *servp = htons(0); 2870Sstevel@tonic-gate } else if (strcmp(args->arg.nd_hs->h_serv, "rpcbind") 2880Sstevel@tonic-gate == 0) { 2890Sstevel@tonic-gate *servp = htons(111); 2900Sstevel@tonic-gate } else if (strspn(args->arg.nd_hs->h_serv, "0123456789") 2910Sstevel@tonic-gate == strlen(args->arg.nd_hs->h_serv)) { 2920Sstevel@tonic-gate *servp = htons(atoi(args->arg.nd_hs->h_serv)); 2930Sstevel@tonic-gate } else { 2940Sstevel@tonic-gate /* i.e. need to call a name service on this */ 2950Sstevel@tonic-gate servp = NULL; 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* 2990Sstevel@tonic-gate * If the hostname is HOST_SELF_BIND, we return 0.0.0.0 3000Sstevel@tonic-gate * so the binding can be contacted through all 3010Sstevel@tonic-gate * interfaces. If the hostname is HOST_SELF_CONNECT, 3020Sstevel@tonic-gate * we return 127.0.0.1 so the address can be connected 3030Sstevel@tonic-gate * to locally. If the hostname is HOST_ANY, we return 3040Sstevel@tonic-gate * no addresses because IP doesn't know how to specify 3050Sstevel@tonic-gate * a service without a host. And finally if we specify 3060Sstevel@tonic-gate * HOST_BROADCAST then we ask a tli fd to tell us what 3070Sstevel@tonic-gate * the broadcast addresses are for any udp 3080Sstevel@tonic-gate * interfaces on this machine. 3090Sstevel@tonic-gate */ 3100Sstevel@tonic-gate if (args->arg.nd_hs->h_host == 0) { 3110Sstevel@tonic-gate _nderror = ND_NOHOST; 3120Sstevel@tonic-gate return (ND_NOHOST); 3130Sstevel@tonic-gate } else if ((strcmp(args->arg.nd_hs->h_host, 3140Sstevel@tonic-gate HOST_SELF_BIND) == 0)) { 3150Sstevel@tonic-gate haddrlist = localaddr; 3160Sstevel@tonic-gate } else if ((strcmp(args->arg.nd_hs->h_host, 3170Sstevel@tonic-gate HOST_SELF_CONNECT) == 0)) { 3180Sstevel@tonic-gate haddrlist = connectaddr; 3190Sstevel@tonic-gate } else if ((strcmp(args->arg.nd_hs->h_host, 3200Sstevel@tonic-gate LOCALHOST) == 0)) { 3210Sstevel@tonic-gate haddrlist = connectaddr; 3220Sstevel@tonic-gate } else if ((int)(dotnameaddr = 3230Sstevel@tonic-gate inet_addr(args->arg.nd_hs->h_host)) != -1) { 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * If the caller passed in a dot separated IP 3260Sstevel@tonic-gate * notation to netdir_getbyname, convert that 3270Sstevel@tonic-gate * back into address. 3280Sstevel@tonic-gate */ 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate dotnamelist[0] = (char *)&dotnameaddr; 3310Sstevel@tonic-gate dotnamelist[1] = NULL; 3320Sstevel@tonic-gate haddrlist = dotnamelist; 3330Sstevel@tonic-gate } else if ((strcmp(args->arg.nd_hs->h_host, 3340Sstevel@tonic-gate HOST_BROADCAST) == 0)) { 3350Sstevel@tonic-gate /* 3360Sstevel@tonic-gate * Now that inaddrs and baddrlist are 3370Sstevel@tonic-gate * dynamically allocated, care must be 3380Sstevel@tonic-gate * taken in freeing up the 3390Sstevel@tonic-gate * memory at each 'return()' point. 3400Sstevel@tonic-gate * 3410Sstevel@tonic-gate * Early return protection (using 3420Sstevel@tonic-gate * FREE_return()) is needed only in NETDIR_BY 3430Sstevel@tonic-gate * cases because dynamic allocation is used 3440Sstevel@tonic-gate * when args->op_t == NETDIR_BY. 3450Sstevel@tonic-gate * 3460Sstevel@tonic-gate * Early return protection is not needed in 3470Sstevel@tonic-gate * haddrlist==0 conditionals because dynamic 3480Sstevel@tonic-gate * allocation guarantees haddrlist!=0. 3490Sstevel@tonic-gate * 3500Sstevel@tonic-gate * Early return protection is not needed in most 3510Sstevel@tonic-gate * servp!=0 conditionals because this is handled 3520Sstevel@tonic-gate * (and returned) first. 3530Sstevel@tonic-gate */ 3540Sstevel@tonic-gate #define FREE_return(ret) \ 3550Sstevel@tonic-gate { \ 3560Sstevel@tonic-gate if (inaddrs) \ 3570Sstevel@tonic-gate free(inaddrs); \ 3580Sstevel@tonic-gate if (baddrlist) \ 3590Sstevel@tonic-gate free(baddrlist); \ 3600Sstevel@tonic-gate _nderror = ret; \ 3610Sstevel@tonic-gate return (ret); \ 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate int i, bnets; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate bnets = getbroadcastnets(nconf, &inaddrs); 3660Sstevel@tonic-gate if (bnets == 0) { 3670Sstevel@tonic-gate _nderror = ND_NOHOST; 3680Sstevel@tonic-gate return (ND_NOHOST); 3690Sstevel@tonic-gate } 370132Srobinson baddrlist = malloc((bnets+1)*sizeof (char *)); 3710Sstevel@tonic-gate if (baddrlist == NULL) 3720Sstevel@tonic-gate FREE_return(ND_NOMEM); 3730Sstevel@tonic-gate for (i = 0; i < bnets; i++) 3740Sstevel@tonic-gate baddrlist[i] = (char *)&inaddrs[i]; 3750Sstevel@tonic-gate baddrlist[i] = NULL; 3760Sstevel@tonic-gate haddrlist = baddrlist; 3770Sstevel@tonic-gate } else { 3780Sstevel@tonic-gate /* i.e. need to call a name service on this */ 3790Sstevel@tonic-gate haddrlist = 0; 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate if (haddrlist && servp) { 3830Sstevel@tonic-gate int ret; 3840Sstevel@tonic-gate /* 3850Sstevel@tonic-gate * Convert h_addr_list into nd_addrlist. 3860Sstevel@tonic-gate * malloc's will be done, freed using 3870Sstevel@tonic-gate * netdir_free. 3880Sstevel@tonic-gate */ 3890Sstevel@tonic-gate ret = hent2ndaddr(AF_INET, haddrlist, servp, 3900Sstevel@tonic-gate res->nd_alist); 3910Sstevel@tonic-gate FREE_return(ret) 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate break; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate case NETDIR_BY6: 3970Sstevel@tonic-gate if (args->arg.nd_hs == 0) { 3980Sstevel@tonic-gate _nderror = ND_BADARG; 3990Sstevel@tonic-gate return (ND_BADARG); 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate /* 4020Sstevel@tonic-gate * If servname is NULL, return 0 as the port number. 4030Sstevel@tonic-gate * If servname is rpcbind, return 111 as the port number 4040Sstevel@tonic-gate * If servname is a number, return it back as the port 4050Sstevel@tonic-gate * number. 4060Sstevel@tonic-gate */ 4070Sstevel@tonic-gate if (args->arg.nd_hs->h_serv == 0) { 4080Sstevel@tonic-gate *servp = htons(0); 4090Sstevel@tonic-gate } else if (strcmp(args->arg.nd_hs->h_serv, 4100Sstevel@tonic-gate "rpcbind") == 0) { 4110Sstevel@tonic-gate *servp = htons(111); 4120Sstevel@tonic-gate } else if (strspn(args->arg.nd_hs->h_serv, "0123456789") 4130Sstevel@tonic-gate == strlen(args->arg.nd_hs->h_serv)) { 4140Sstevel@tonic-gate *servp = htons(atoi(args->arg.nd_hs->h_serv)); 4150Sstevel@tonic-gate } else { 4160Sstevel@tonic-gate /* i.e. need to call a name service on this */ 4170Sstevel@tonic-gate servp = NULL; 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate /* 4210Sstevel@tonic-gate * If the hostname is HOST_SELF_BIND, we return ipv6 4220Sstevel@tonic-gate * localaddress so the binding can be contacted through 4230Sstevel@tonic-gate * all interfaces. 4240Sstevel@tonic-gate * If the hostname is HOST_SELF_CONNECT, we return 4250Sstevel@tonic-gate * ipv6 loopback address so the address can be connected 4260Sstevel@tonic-gate * to locally. 4270Sstevel@tonic-gate * If the hostname is HOST_ANY, we return no addresses 4280Sstevel@tonic-gate * because IP doesn't know how to specify a service 4290Sstevel@tonic-gate * without a host. 4300Sstevel@tonic-gate * And finally if we specify HOST_BROADCAST then we 4310Sstevel@tonic-gate * disallow since IPV6 does not have any 4320Sstevel@tonic-gate * broadcast concept. 4330Sstevel@tonic-gate */ 4340Sstevel@tonic-gate if (args->arg.nd_hs->h_host == 0) { 4350Sstevel@tonic-gate return (ND_NOHOST); 4360Sstevel@tonic-gate } else if ((strcmp(args->arg.nd_hs->h_host, 4370Sstevel@tonic-gate HOST_SELF_BIND) == 0)) { 4380Sstevel@tonic-gate haddrlist = localaddr6; 4390Sstevel@tonic-gate } else if ((strcmp(args->arg.nd_hs->h_host, 4400Sstevel@tonic-gate HOST_SELF_CONNECT) == 0)) { 4410Sstevel@tonic-gate haddrlist = connectaddr6; 4420Sstevel@tonic-gate } else if ((strcmp(args->arg.nd_hs->h_host, 4430Sstevel@tonic-gate LOCALHOST) == 0)) { 4440Sstevel@tonic-gate haddrlist = connectaddr6; 4450Sstevel@tonic-gate } else if (strchr(args->arg.nd_hs->h_host, ':') 4460Sstevel@tonic-gate != NULL) { 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate /* 4490Sstevel@tonic-gate * If the caller passed in a dot separated IP notation 4500Sstevel@tonic-gate * to netdir_getbyname, convert that back into address. 4510Sstevel@tonic-gate */ 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate if ((inet_pton(AF_INET6, 4540Sstevel@tonic-gate args->arg.nd_hs->h_host, 4550Sstevel@tonic-gate &v6nameaddr)) != 0) { 4560Sstevel@tonic-gate dotnamelist[0] = (char *)&v6nameaddr; 4570Sstevel@tonic-gate dotnamelist[1] = NULL; 4580Sstevel@tonic-gate haddrlist = dotnamelist; 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate else 4610Sstevel@tonic-gate /* not sure what to return */ 4620Sstevel@tonic-gate return (ND_NOHOST); 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate } else if ((strcmp(args->arg.nd_hs->h_host, 4650Sstevel@tonic-gate HOST_BROADCAST) == 0)) { 4660Sstevel@tonic-gate /* 4670Sstevel@tonic-gate * Don't support broadcast in 4680Sstevel@tonic-gate * IPV6 4690Sstevel@tonic-gate */ 4700Sstevel@tonic-gate return (ND_NOHOST); 4710Sstevel@tonic-gate } else { 4720Sstevel@tonic-gate /* i.e. need to call a name service on this */ 4730Sstevel@tonic-gate haddrlist = 0; 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate if (haddrlist && servp) { 4770Sstevel@tonic-gate int ret; 4780Sstevel@tonic-gate /* 4790Sstevel@tonic-gate * Convert h_addr_list into nd_addrlist. 4800Sstevel@tonic-gate * malloc's will be done, freed 4810Sstevel@tonic-gate * using netdir_free. 4820Sstevel@tonic-gate */ 4830Sstevel@tonic-gate ret = hent2ndaddr(AF_INET6, haddrlist, 4840Sstevel@tonic-gate servp, res->nd_alist); 4850Sstevel@tonic-gate FREE_return(ret) 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate break; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate } 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate /* 4930Sstevel@tonic-gate * 2. Most common scenario. This is the way we ship /etc/netconfig. 4940Sstevel@tonic-gate * Emphasis on improving performance in the "if" part. 4950Sstevel@tonic-gate */ 4960Sstevel@tonic-gate if (nconf->nc_nlookups == 0) { 4970Sstevel@tonic-gate struct hostent *he = NULL, *tmphe; 4980Sstevel@tonic-gate struct servent *se; 4990Sstevel@tonic-gate int ret; 5000Sstevel@tonic-gate nss_XbyY_buf_t *ndbuf4switch = 0; 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate switch (args->op_t) { 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate case NSS_HOST: 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate he = DOOR_GETHOSTBYNAME_R(args->arg.nss.host.name, 5070Sstevel@tonic-gate res->nss.host.hent, args->arg.nss.host.buf, 5080Sstevel@tonic-gate args->arg.nss.host.buflen, 5090Sstevel@tonic-gate res->nss.host.herrno_p); 5100Sstevel@tonic-gate if (he == NULL) 5110Sstevel@tonic-gate return (_nderror = ND_NOHOST); 5120Sstevel@tonic-gate return (_nderror = ND_OK); 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate case NSS_HOST6: 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate he = DOOR_GETIPNODEBYNAME_R(args->arg.nss.host6.name, 5170Sstevel@tonic-gate res->nss.host.hent, args->arg.nss.host.buf, 5180Sstevel@tonic-gate args->arg.nss.host6.buflen, 5190Sstevel@tonic-gate args->arg.nss.host6.af_family, 5200Sstevel@tonic-gate args->arg.nss.host6.flags, 5210Sstevel@tonic-gate res->nss.host.herrno_p); 5220Sstevel@tonic-gate 523132Srobinson if (he == NULL) 5240Sstevel@tonic-gate return (_nderror = ND_NOHOST); 5250Sstevel@tonic-gate return (_nderror = ND_OK); 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate case NSS_SERV: 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate se = _switch_getservbyname_r(args->arg.nss.serv.name, 5300Sstevel@tonic-gate args->arg.nss.serv.proto, 5310Sstevel@tonic-gate res->nss.serv, args->arg.nss.serv.buf, 5320Sstevel@tonic-gate args->arg.nss.serv.buflen); 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate _nderror = ND_OK; 5350Sstevel@tonic-gate if (se == 0) 5360Sstevel@tonic-gate _nderror = ND_NOSERV; 5370Sstevel@tonic-gate return (_nderror); 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate case NETDIR_BY: 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate if (servp == 0) { 5420Sstevel@tonic-gate char *proto = 5430Sstevel@tonic-gate (strcmp(nconf->nc_proto, NC_TCP) == 0) ? NC_TCP : NC_UDP; 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate /* 5460Sstevel@tonic-gate * We go through all this for just one port number, 5470Sstevel@tonic-gate * which is most often constant. How about linking in 5480Sstevel@tonic-gate * an indexed database of well-known ports in the name 5490Sstevel@tonic-gate * of performance ? 5500Sstevel@tonic-gate */ 551132Srobinson ndbuf4switch = _nss_XbyY_buf_alloc( 552132Srobinson sizeof (struct servent), NSS_BUFLEN_SERVICES); 5530Sstevel@tonic-gate if (ndbuf4switch == 0) 5540Sstevel@tonic-gate FREE_return(ND_NOMEM); 5550Sstevel@tonic-gate se = _switch_getservbyname_r(args->arg.nd_hs->h_serv, 5560Sstevel@tonic-gate proto, ndbuf4switch->result, 5570Sstevel@tonic-gate ndbuf4switch->buffer, ndbuf4switch->buflen); 5580Sstevel@tonic-gate if (!se) { 5590Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4switch); 5600Sstevel@tonic-gate FREE_return(ND_NOSERV) 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate server_port = se->s_port; 5630Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4switch); 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate if (haddrlist == 0) { 5670Sstevel@tonic-gate int h_errnop = 0; 5680Sstevel@tonic-gate 569132Srobinson ndbuf4switch = _nss_XbyY_buf_alloc( 570132Srobinson sizeof (struct hostent), 571132Srobinson NSS_BUFLEN_HOSTS); 5720Sstevel@tonic-gate if (ndbuf4switch == 0) { 5730Sstevel@tonic-gate _nderror = ND_NOMEM; 5740Sstevel@tonic-gate return (ND_NOMEM); 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate /* 5770Sstevel@tonic-gate * Search the ipnodes (v6) path first, 5780Sstevel@tonic-gate * search will return the v4 addresses 5790Sstevel@tonic-gate * as v4mapped addresses. 5800Sstevel@tonic-gate */ 5810Sstevel@tonic-gate if ((tmphe = DOOR_GETIPNODEBYNAME_R( 5820Sstevel@tonic-gate args->arg.nd_hs->h_host, 5830Sstevel@tonic-gate ndbuf4switch->result, ndbuf4switch->buffer, 5840Sstevel@tonic-gate ndbuf4switch->buflen, args->arg.nss.host6.af_family, 5850Sstevel@tonic-gate args->arg.nss.host6.flags, &h_errnop)) != NULL) 5860Sstevel@tonic-gate he = __mappedtov4(tmphe, &h_errnop); 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate if (he == NULL) { 5890Sstevel@tonic-gate /* Failover case, try hosts db for v4 address */ 5900Sstevel@tonic-gate he = DOOR_GETHOSTBYNAME_R( 5910Sstevel@tonic-gate args->arg.nd_hs->h_host, 5920Sstevel@tonic-gate ndbuf4switch->result, ndbuf4switch->buffer, 5930Sstevel@tonic-gate ndbuf4switch->buflen, &h_errnop); 5940Sstevel@tonic-gate if (he == NULL) { 5950Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4switch); 5960Sstevel@tonic-gate _nderror = h_errnop ? 5970Sstevel@tonic-gate __herrno2netdir(h_errnop) : 5980Sstevel@tonic-gate ND_NOHOST; 5990Sstevel@tonic-gate return (_nderror); 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate /* 6020Sstevel@tonic-gate * Convert h_addr_list into nd_addrlist. 6030Sstevel@tonic-gate * malloc's will be done, freed using 6040Sstevel@tonic-gate * netdir_free. 6050Sstevel@tonic-gate */ 6060Sstevel@tonic-gate ret = hent2ndaddr(AF_INET, he->h_addr_list, 6070Sstevel@tonic-gate &server_port, res->nd_alist); 6080Sstevel@tonic-gate } else { 6090Sstevel@tonic-gate /* 6100Sstevel@tonic-gate * Convert h_addr_list into nd_addrlist. 6110Sstevel@tonic-gate * malloc's will be done, freed using 6120Sstevel@tonic-gate * netdir_free. 6130Sstevel@tonic-gate */ 6140Sstevel@tonic-gate ret = hent2ndaddr(AF_INET, he->h_addr_list, 6150Sstevel@tonic-gate &server_port, res->nd_alist); 6160Sstevel@tonic-gate freehostent(he); 6170Sstevel@tonic-gate } 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate _nderror = ret; 6200Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4switch); 6210Sstevel@tonic-gate return (ret); 6220Sstevel@tonic-gate } else { 6230Sstevel@tonic-gate int ret; 6240Sstevel@tonic-gate /* 6250Sstevel@tonic-gate * Convert h_addr_list into nd_addrlist. 6260Sstevel@tonic-gate * malloc's will be done, freed using netdir_free. 6270Sstevel@tonic-gate */ 6280Sstevel@tonic-gate ret = hent2ndaddr(AF_INET, haddrlist, 6290Sstevel@tonic-gate &server_port, res->nd_alist); 6300Sstevel@tonic-gate FREE_return(ret) 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate case NETDIR_BY6: 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate if (servp == 0) { 6370Sstevel@tonic-gate char *proto = 6380Sstevel@tonic-gate (strcmp(nconf->nc_proto, NC_TCP) == 0) ? NC_TCP : NC_UDP; 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * We go through all this for just 6420Sstevel@tonic-gate * one port number, 6430Sstevel@tonic-gate * which is most often constant. 6440Sstevel@tonic-gate * How about linking in 6450Sstevel@tonic-gate * an indexed database of well-known 6460Sstevel@tonic-gate * ports in the name 6470Sstevel@tonic-gate * of performance ? 6480Sstevel@tonic-gate */ 649132Srobinson ndbuf4switch = _nss_XbyY_buf_alloc( 650132Srobinson sizeof (struct servent), 651132Srobinson NSS_BUFLEN_SERVICES); 6520Sstevel@tonic-gate if (ndbuf4switch == 0) 6530Sstevel@tonic-gate FREE_return(ND_NOMEM); 6540Sstevel@tonic-gate se = _switch_getservbyname_r( 6550Sstevel@tonic-gate args->arg.nd_hs->h_serv, 6560Sstevel@tonic-gate proto, ndbuf4switch->result, 6570Sstevel@tonic-gate ndbuf4switch->buffer, ndbuf4switch->buflen); 6580Sstevel@tonic-gate if (!se) { 6590Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4switch); 6600Sstevel@tonic-gate FREE_return(ND_NOSERV) 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate server_port = se->s_port; 6630Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4switch); 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate if (haddrlist == 0) { 6670Sstevel@tonic-gate int h_errnop = 0; 6680Sstevel@tonic-gate 669132Srobinson ndbuf4switch = _nss_XbyY_buf_alloc( 670132Srobinson sizeof (struct hostent), 671132Srobinson NSS_BUFLEN_HOSTS); 6720Sstevel@tonic-gate if (ndbuf4switch == 0) { 6730Sstevel@tonic-gate _nderror = ND_NOMEM; 6740Sstevel@tonic-gate return (ND_NOMEM); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate he = DOOR_GETIPNODEBYNAME_R( 6770Sstevel@tonic-gate args->arg.nd_hs->h_host, 6780Sstevel@tonic-gate ndbuf4switch->result, ndbuf4switch->buffer, 6790Sstevel@tonic-gate ndbuf4switch->buflen, 6800Sstevel@tonic-gate args->arg.nss.host6.af_family, 6810Sstevel@tonic-gate args->arg.nss.host6.flags, &h_errnop); 6820Sstevel@tonic-gate if (he == NULL) { 6830Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4switch); 6840Sstevel@tonic-gate _nderror = h_errnop ? 6850Sstevel@tonic-gate __herrno2netdir(h_errnop) : 6860Sstevel@tonic-gate ND_NOHOST; 6870Sstevel@tonic-gate return (_nderror); 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate /* 6900Sstevel@tonic-gate * Convert h_addr_list into nd_addrlist. 6910Sstevel@tonic-gate * malloc's will be done, 6920Sstevel@tonic-gate * freed using netdir_free. 6930Sstevel@tonic-gate */ 6940Sstevel@tonic-gate ret = hent2ndaddr(AF_INET6, 6950Sstevel@tonic-gate ((struct hostent *)(ndbuf4switch->result))->h_addr_list, 6960Sstevel@tonic-gate &server_port, res->nd_alist); 6970Sstevel@tonic-gate _nderror = ret; 6980Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4switch); 6990Sstevel@tonic-gate return (ret); 7000Sstevel@tonic-gate } else { 7010Sstevel@tonic-gate int ret; 7020Sstevel@tonic-gate /* 7030Sstevel@tonic-gate * Convert h_addr_list into nd_addrlist. 7040Sstevel@tonic-gate * malloc's will be done, 7050Sstevel@tonic-gate * freed using netdir_free. 7060Sstevel@tonic-gate */ 7070Sstevel@tonic-gate ret = hent2ndaddr(AF_INET6, haddrlist, 7080Sstevel@tonic-gate &server_port, res->nd_alist); 7090Sstevel@tonic-gate FREE_return(ret) 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate 7120Sstevel@tonic-gate default: 7130Sstevel@tonic-gate _nderror = ND_BADARG; 7140Sstevel@tonic-gate return (ND_BADARG); /* should never happen */ 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate } else { 7180Sstevel@tonic-gate /* haddrlist is no longer used, so clean up */ 7190Sstevel@tonic-gate if (inaddrs) 7200Sstevel@tonic-gate free(inaddrs); 7210Sstevel@tonic-gate if (baddrlist) 7220Sstevel@tonic-gate free(baddrlist); 7230Sstevel@tonic-gate } 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate /* 7260Sstevel@tonic-gate * 3. We come this far only if nametoaddr libs are specified for 7270Sstevel@tonic-gate * inet transports and we are called by gethost/servbyname only. 7280Sstevel@tonic-gate */ 7290Sstevel@tonic-gate switch (args->op_t) { 7300Sstevel@tonic-gate struct nd_hostserv service; 7310Sstevel@tonic-gate struct nd_addrlist *addrs; 7320Sstevel@tonic-gate int ret; 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate case NSS_HOST: 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate service.h_host = (char *)args->arg.nss.host.name; 7370Sstevel@tonic-gate service.h_serv = NULL; 7380Sstevel@tonic-gate if ((_nderror = __classic_netdir_getbyname(nconf, 7390Sstevel@tonic-gate &service, &addrs)) != ND_OK) { 7400Sstevel@tonic-gate *(res->nss.host.herrno_p) = nd2herrno(_nderror); 7410Sstevel@tonic-gate return (_nderror); 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate /* 7440Sstevel@tonic-gate * convert addresses back into sockaddr for gethostbyname. 7450Sstevel@tonic-gate */ 7460Sstevel@tonic-gate ret = ndaddr2hent(AF_INET, service.h_host, addrs, 7470Sstevel@tonic-gate res->nss.host.hent, args->arg.nss.host.buf, 7480Sstevel@tonic-gate args->arg.nss.host.buflen); 7490Sstevel@tonic-gate if (ret != ND_OK) 7500Sstevel@tonic-gate *(res->nss.host.herrno_p) = nd2herrno(ret); 7510Sstevel@tonic-gate netdir_free((char *)addrs, ND_ADDRLIST); 7520Sstevel@tonic-gate _nderror = ret; 7530Sstevel@tonic-gate return (ret); 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate case NSS_SERV: 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate if (args->arg.nss.serv.proto == NULL) { 7580Sstevel@tonic-gate /* 7590Sstevel@tonic-gate * A similar HACK showed up in Solaris 2.3. 7600Sstevel@tonic-gate * The caller wild-carded proto -- i.e. will 7610Sstevel@tonic-gate * accept a match using tcp or udp for the port 7620Sstevel@tonic-gate * number. Since we have no hope of getting 7630Sstevel@tonic-gate * directly to a name service switch backend 7640Sstevel@tonic-gate * from here that understands this semantics, 7650Sstevel@tonic-gate * we try calling the netdir interfaces first 7660Sstevel@tonic-gate * with "tcp" and then "udp". 7670Sstevel@tonic-gate */ 7680Sstevel@tonic-gate args->arg.nss.serv.proto = "tcp"; 7690Sstevel@tonic-gate _nderror = _get_hostserv_inetnetdir_byname(nconf, args, 7700Sstevel@tonic-gate res); 7710Sstevel@tonic-gate if (_nderror != ND_OK) { 7720Sstevel@tonic-gate args->arg.nss.serv.proto = "udp"; 7730Sstevel@tonic-gate _nderror = 7740Sstevel@tonic-gate _get_hostserv_inetnetdir_byname(nconf, 7750Sstevel@tonic-gate args, res); 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate return (_nderror); 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate /* 7810Sstevel@tonic-gate * Third-parties should optimize their nametoaddr 7820Sstevel@tonic-gate * libraries for the HOST_SELF case. 7830Sstevel@tonic-gate */ 7840Sstevel@tonic-gate service.h_host = HOST_SELF; 7850Sstevel@tonic-gate service.h_serv = (char *)args->arg.nss.serv.name; 7860Sstevel@tonic-gate if ((_nderror = __classic_netdir_getbyname(nconf, 7870Sstevel@tonic-gate &service, &addrs)) != ND_OK) { 7880Sstevel@tonic-gate return (_nderror); 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate /* 7910Sstevel@tonic-gate * convert addresses back into servent for getservbyname. 7920Sstevel@tonic-gate */ 7930Sstevel@tonic-gate _nderror = ndaddr2srent(service.h_serv, 7940Sstevel@tonic-gate args->arg.nss.serv.proto, 795132Srobinson /* LINTED pointer cast */ 7960Sstevel@tonic-gate ((struct sockaddr_in *)addrs->n_addrs->buf)->sin_port, 7970Sstevel@tonic-gate res->nss.serv, 7980Sstevel@tonic-gate args->arg.nss.serv.buf, args->arg.nss.serv.buflen); 7990Sstevel@tonic-gate netdir_free((char *)addrs, ND_ADDRLIST); 8000Sstevel@tonic-gate return (_nderror); 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate default: 8030Sstevel@tonic-gate _nderror = ND_BADARG; 8040Sstevel@tonic-gate return (ND_BADARG); /* should never happen */ 8050Sstevel@tonic-gate } 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate /* 8090Sstevel@tonic-gate * gethostbyaddr/servbyport always call this function; if they call 8100Sstevel@tonic-gate * with nametoaddr libs in nconf, we call netdir_getbyaddr 8110Sstevel@tonic-gate * implementation __classic_netdir_getbyaddr, otherwise nsswitch. 8120Sstevel@tonic-gate * 8130Sstevel@tonic-gate * netdir_getbyaddr calls this only if nametoaddr libs are NOT 8140Sstevel@tonic-gate * specified for inet transports; i.e. it's supposed to follow 8150Sstevel@tonic-gate * the name service switch. 8160Sstevel@tonic-gate */ 8170Sstevel@tonic-gate int 8180Sstevel@tonic-gate _get_hostserv_inetnetdir_byaddr(struct netconfig *nconf, 8190Sstevel@tonic-gate struct nss_netdirbyaddr_in *args, union nss_netdirbyaddr_out *res) 8200Sstevel@tonic-gate { 8210Sstevel@tonic-gate if (nconf == 0) { 8220Sstevel@tonic-gate _nderror = ND_BADARG; 8230Sstevel@tonic-gate return (_nderror); 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate /* 8270Sstevel@tonic-gate * 1. gethostbyaddr()/netdir_getbyaddr() special cases: 8280Sstevel@tonic-gate */ 8290Sstevel@tonic-gate switch (args->op_t) { 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate case NSS_HOST: 8320Sstevel@tonic-gate /* 8330Sstevel@tonic-gate * Worth the performance gain: assuming a lot of inet apps 8340Sstevel@tonic-gate * actively use "127.0.0.1". 8350Sstevel@tonic-gate */ 836132Srobinson /* LINTED pointer cast */ 8370Sstevel@tonic-gate if (*(uint32_t *)(args->arg.nss.host.addr) == 8380Sstevel@tonic-gate htonl(INADDR_LOOPBACK)) { 839132Srobinson (void) mutex_lock(&nd_addr_lock); 8400Sstevel@tonic-gate IN_SET_LOOPBACK_ADDR(&sa_con); 8410Sstevel@tonic-gate _nderror = ndaddr2hent(AF_INET, LOCALHOST, 8420Sstevel@tonic-gate &nd_conaddrlist, res->nss.host.hent, 8430Sstevel@tonic-gate args->arg.nss.host.buf, 8440Sstevel@tonic-gate args->arg.nss.host.buflen); 845132Srobinson (void) mutex_unlock(&nd_addr_lock); 8460Sstevel@tonic-gate if (_nderror != ND_OK) 8470Sstevel@tonic-gate *(res->nss.host.herrno_p) = 8480Sstevel@tonic-gate nd2herrno(_nderror); 8490Sstevel@tonic-gate return (_nderror); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate break; 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate case NETDIR_BY: 8540Sstevel@tonic-gate case NETDIR_BY_NOSRV: 8550Sstevel@tonic-gate { 8560Sstevel@tonic-gate struct sockaddr_in *sin; 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate if (args->arg.nd_nbuf == NULL) { 8590Sstevel@tonic-gate _nderror = ND_BADARG; 8600Sstevel@tonic-gate return (_nderror); 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate /* 8640Sstevel@tonic-gate * Validate the address which was passed 8650Sstevel@tonic-gate * as the request. 8660Sstevel@tonic-gate */ 867132Srobinson /* LINTED pointer cast */ 8680Sstevel@tonic-gate sin = (struct sockaddr_in *)args->arg.nd_nbuf->buf; 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate if ((args->arg.nd_nbuf->len != 8710Sstevel@tonic-gate sizeof (struct sockaddr_in)) || 8720Sstevel@tonic-gate (sin->sin_family != AF_INET)) { 8730Sstevel@tonic-gate _nderror = ND_BADARG; 8740Sstevel@tonic-gate return (_nderror); 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate } 8770Sstevel@tonic-gate break; 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate case NETDIR_BY6: 8800Sstevel@tonic-gate case NETDIR_BY_NOSRV6: 8810Sstevel@tonic-gate { 8820Sstevel@tonic-gate struct sockaddr_in6 *sin6; 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate if (args->arg.nd_nbuf == NULL) { 8850Sstevel@tonic-gate _nderror = ND_BADARG; 8860Sstevel@tonic-gate return (_nderror); 8870Sstevel@tonic-gate } 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate /* 8900Sstevel@tonic-gate * Validate the address which was passed 8910Sstevel@tonic-gate * as the request. 8920Sstevel@tonic-gate */ 893132Srobinson /* LINTED pointer cast */ 8940Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)args->arg.nd_nbuf->buf; 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate if ((args->arg.nd_nbuf->len != 8970Sstevel@tonic-gate sizeof (struct sockaddr_in6)) || 8980Sstevel@tonic-gate (sin6->sin6_family != AF_INET6)) { 8990Sstevel@tonic-gate _nderror = ND_BADARG; 9000Sstevel@tonic-gate return (_nderror); 9010Sstevel@tonic-gate } 9020Sstevel@tonic-gate } 9030Sstevel@tonic-gate break; 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate } 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate /* 9080Sstevel@tonic-gate * 2. Most common scenario. This is the way we ship /etc/netconfig. 9090Sstevel@tonic-gate * Emphasis on improving performance in the "if" part. 9100Sstevel@tonic-gate */ 9110Sstevel@tonic-gate if (nconf->nc_nlookups == 0) { 9120Sstevel@tonic-gate struct hostent *he = NULL, *tmphe; 9130Sstevel@tonic-gate struct servent *se = NULL; 9140Sstevel@tonic-gate nss_XbyY_buf_t *ndbuf4host = 0; 9150Sstevel@tonic-gate nss_XbyY_buf_t *ndbuf4serv = 0; 9160Sstevel@tonic-gate char *proto = 9170Sstevel@tonic-gate (strcmp(nconf->nc_proto, NC_TCP) == 0) ? NC_TCP : NC_UDP; 9180Sstevel@tonic-gate struct sockaddr_in *sa; 9190Sstevel@tonic-gate struct sockaddr_in6 *sin6; 9200Sstevel@tonic-gate struct in_addr *addr4 = 0; 9210Sstevel@tonic-gate struct in6_addr v4mapbuf; 9220Sstevel@tonic-gate int h_errnop; 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate switch (args->op_t) { 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate case NSS_HOST: 9270Sstevel@tonic-gate 9280Sstevel@tonic-gate he = DOOR_GETHOSTBYADDR_R(args->arg.nss.host.addr, 9290Sstevel@tonic-gate args->arg.nss.host.len, args->arg.nss.host.type, 9300Sstevel@tonic-gate res->nss.host.hent, args->arg.nss.host.buf, 9310Sstevel@tonic-gate args->arg.nss.host.buflen, 9320Sstevel@tonic-gate res->nss.host.herrno_p); 9330Sstevel@tonic-gate if (he == 0) 9340Sstevel@tonic-gate _nderror = ND_NOHOST; 9350Sstevel@tonic-gate else 9360Sstevel@tonic-gate _nderror = ND_OK; 9370Sstevel@tonic-gate return (_nderror); 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate case NSS_HOST6: 9410Sstevel@tonic-gate he = DOOR_GETIPNODEBYADDR_R(args->arg.nss.host.addr, 9420Sstevel@tonic-gate args->arg.nss.host.len, args->arg.nss.host.type, 9430Sstevel@tonic-gate res->nss.host.hent, args->arg.nss.host.buf, 9440Sstevel@tonic-gate args->arg.nss.host.buflen, 9450Sstevel@tonic-gate res->nss.host.herrno_p); 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate if (he == 0) 9480Sstevel@tonic-gate return (ND_NOHOST); 9490Sstevel@tonic-gate return (ND_OK); 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate case NSS_SERV: 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate se = _switch_getservbyport_r(args->arg.nss.serv.port, 9550Sstevel@tonic-gate args->arg.nss.serv.proto, 9560Sstevel@tonic-gate res->nss.serv, args->arg.nss.serv.buf, 9570Sstevel@tonic-gate args->arg.nss.serv.buflen); 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate if (se == 0) 9600Sstevel@tonic-gate _nderror = ND_NOSERV; 9610Sstevel@tonic-gate else 9620Sstevel@tonic-gate _nderror = ND_OK; 9630Sstevel@tonic-gate return (_nderror); 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate case NETDIR_BY: 9660Sstevel@tonic-gate case NETDIR_BY_NOSRV: 9670Sstevel@tonic-gate 968132Srobinson ndbuf4serv = _nss_XbyY_buf_alloc(sizeof (struct servent), 969132Srobinson NSS_BUFLEN_SERVICES); 9700Sstevel@tonic-gate if (ndbuf4serv == 0) { 9710Sstevel@tonic-gate _nderror = ND_NOMEM; 9720Sstevel@tonic-gate return (_nderror); 9730Sstevel@tonic-gate } 974132Srobinson /* LINTED pointer cast */ 9750Sstevel@tonic-gate sa = (struct sockaddr_in *)(args->arg.nd_nbuf->buf); 9760Sstevel@tonic-gate addr4 = (struct in_addr *)&(sa->sin_addr); 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate /* 9790Sstevel@tonic-gate * if NETDIR_BY_NOSRV or port == 0 skip the service 9800Sstevel@tonic-gate * lookup. 9810Sstevel@tonic-gate */ 9820Sstevel@tonic-gate if (args->op_t != NETDIR_BY_NOSRV && sa->sin_port != 0) { 9830Sstevel@tonic-gate se = _switch_getservbyport_r(sa->sin_port, proto, 9840Sstevel@tonic-gate ndbuf4serv->result, ndbuf4serv->buffer, 9850Sstevel@tonic-gate ndbuf4serv->buflen); 9860Sstevel@tonic-gate if (!se) { 9870Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4serv); 9880Sstevel@tonic-gate /* 9890Sstevel@tonic-gate * We can live with this - i.e. the address 9900Sstevel@tonic-gate * does not 9910Sstevel@tonic-gate * belong to a well known service. The caller 9920Sstevel@tonic-gate * traditionally accepts a stringified port 9930Sstevel@tonic-gate * number 9940Sstevel@tonic-gate * as the service name. The state of se is used 9950Sstevel@tonic-gate * ahead to indicate the same. 9960Sstevel@tonic-gate * However, we do not tolerate this nonsense 9970Sstevel@tonic-gate * when we cannot get a host name. See below. 9980Sstevel@tonic-gate */ 9990Sstevel@tonic-gate } 10000Sstevel@tonic-gate } 10010Sstevel@tonic-gate 1002132Srobinson ndbuf4host = _nss_XbyY_buf_alloc(sizeof (struct hostent), 1003132Srobinson NSS_BUFLEN_HOSTS); 10040Sstevel@tonic-gate if (ndbuf4host == 0) { 10050Sstevel@tonic-gate if (ndbuf4serv) 10060Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4serv); 10070Sstevel@tonic-gate _nderror = ND_NOMEM; 10080Sstevel@tonic-gate return (_nderror); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate /* 10120Sstevel@tonic-gate * Since we're going to search the ipnodes (v6) path first, 10130Sstevel@tonic-gate * we need to treat the address as a v4mapped address. 10140Sstevel@tonic-gate */ 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(addr4, &v4mapbuf); 10170Sstevel@tonic-gate if ((tmphe = DOOR_GETIPNODEBYADDR_R((char *)&v4mapbuf, 10180Sstevel@tonic-gate 16, AF_INET6, ndbuf4host->result, 10190Sstevel@tonic-gate ndbuf4host->buffer, 10200Sstevel@tonic-gate ndbuf4host->buflen, &h_errnop)) != NULL) 10210Sstevel@tonic-gate he = __mappedtov4(tmphe, &h_errnop); 10220Sstevel@tonic-gate 10230Sstevel@tonic-gate if (!he) { 10240Sstevel@tonic-gate /* Failover case, try hosts db for v4 address */ 10250Sstevel@tonic-gate he = DOOR_GETHOSTBYADDR_R((char *) 10260Sstevel@tonic-gate &(sa->sin_addr.s_addr), 4, 10270Sstevel@tonic-gate sa->sin_family, ndbuf4host->result, 10280Sstevel@tonic-gate ndbuf4host->buffer, ndbuf4host->buflen, 10290Sstevel@tonic-gate &h_errnop); 10300Sstevel@tonic-gate if (!he) { 10310Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4host); 10320Sstevel@tonic-gate if (ndbuf4serv) 10330Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4serv); 10340Sstevel@tonic-gate _nderror = __herrno2netdir(h_errnop); 10350Sstevel@tonic-gate return (_nderror); 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate /* 10380Sstevel@tonic-gate * Convert host names and service names into hostserv 10390Sstevel@tonic-gate * pairs. malloc's will be done, freed using 10400Sstevel@tonic-gate * netdir_free. 10410Sstevel@tonic-gate */ 10420Sstevel@tonic-gate h_errnop = hsents2ndhostservs(he, se, 10430Sstevel@tonic-gate sa->sin_port, res->nd_hslist); 10440Sstevel@tonic-gate } else { 10450Sstevel@tonic-gate /* 10460Sstevel@tonic-gate * Convert host names and service names into hostserv 10470Sstevel@tonic-gate * pairs. malloc's will be done, freed using 10480Sstevel@tonic-gate * netdir_free. 10490Sstevel@tonic-gate */ 10500Sstevel@tonic-gate h_errnop = hsents2ndhostservs(he, se, 10510Sstevel@tonic-gate sa->sin_port, res->nd_hslist); 10520Sstevel@tonic-gate freehostent(he); 10530Sstevel@tonic-gate } 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4host); 10560Sstevel@tonic-gate if (ndbuf4serv) 10570Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4serv); 10580Sstevel@tonic-gate _nderror = __herrno2netdir(h_errnop); 10590Sstevel@tonic-gate return (_nderror); 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate case NETDIR_BY6: 10620Sstevel@tonic-gate case NETDIR_BY_NOSRV6: 10630Sstevel@tonic-gate 1064132Srobinson ndbuf4serv = _nss_XbyY_buf_alloc(sizeof (struct servent), 1065132Srobinson NSS_BUFLEN_SERVICES); 10660Sstevel@tonic-gate if (ndbuf4serv == 0) { 10670Sstevel@tonic-gate _nderror = ND_NOMEM; 10680Sstevel@tonic-gate return (ND_NOMEM); 10690Sstevel@tonic-gate } 1070132Srobinson /* LINTED pointer cast */ 10710Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(args->arg.nd_nbuf->buf); 10720Sstevel@tonic-gate 10730Sstevel@tonic-gate /* 10740Sstevel@tonic-gate * if NETDIR_BY_NOSRV6 or port == 0 skip the service 10750Sstevel@tonic-gate * lookup. 10760Sstevel@tonic-gate */ 10770Sstevel@tonic-gate if (args->op_t != NETDIR_BY_NOSRV6 && sin6->sin6_port == 0) { 10780Sstevel@tonic-gate se = _switch_getservbyport_r(sin6->sin6_port, proto, 10790Sstevel@tonic-gate ndbuf4serv->result, ndbuf4serv->buffer, 10800Sstevel@tonic-gate ndbuf4serv->buflen); 10810Sstevel@tonic-gate if (!se) { 10820Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4serv); 10830Sstevel@tonic-gate /* 10840Sstevel@tonic-gate * We can live with this - i.e. the address does 10850Sstevel@tonic-gate * not * belong to a well known service. The 10860Sstevel@tonic-gate * caller traditionally accepts a stringified 10870Sstevel@tonic-gate * port number 10880Sstevel@tonic-gate * as the service name. The state of se is used 10890Sstevel@tonic-gate * ahead to indicate the same. 10900Sstevel@tonic-gate * However, we do not tolerate this nonsense 10910Sstevel@tonic-gate * when we cannot get a host name. See below. 10920Sstevel@tonic-gate */ 10930Sstevel@tonic-gate } 10940Sstevel@tonic-gate } 10950Sstevel@tonic-gate 1096132Srobinson ndbuf4host = _nss_XbyY_buf_alloc(sizeof (struct hostent), 1097132Srobinson NSS_BUFLEN_HOSTS); 10980Sstevel@tonic-gate if (ndbuf4host == 0) { 10990Sstevel@tonic-gate if (ndbuf4serv) 11000Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4serv); 11010Sstevel@tonic-gate _nderror = ND_NOMEM; 11020Sstevel@tonic-gate return (_nderror); 11030Sstevel@tonic-gate } 11040Sstevel@tonic-gate he = DOOR_GETIPNODEBYADDR_R((char *)&(sin6->sin6_addr), 11050Sstevel@tonic-gate 16, sin6->sin6_family, ndbuf4host->result, 11060Sstevel@tonic-gate ndbuf4host->buffer, 11070Sstevel@tonic-gate ndbuf4host->buflen, &h_errnop); 11080Sstevel@tonic-gate if (!he) { 11090Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4host); 11100Sstevel@tonic-gate if (ndbuf4serv) 11110Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4serv); 11120Sstevel@tonic-gate _nderror = __herrno2netdir(h_errnop); 11130Sstevel@tonic-gate return (_nderror); 11140Sstevel@tonic-gate } 11150Sstevel@tonic-gate /* 11160Sstevel@tonic-gate * Convert host names and service names into hostserv 11170Sstevel@tonic-gate * pairs. malloc's will be done, freed using netdir_free. 11180Sstevel@tonic-gate */ 11190Sstevel@tonic-gate h_errnop = hsents2ndhostservs(he, se, 11200Sstevel@tonic-gate sin6->sin6_port, res->nd_hslist); 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4host); 11230Sstevel@tonic-gate if (ndbuf4serv) 11240Sstevel@tonic-gate NSS_XbyY_FREE(&ndbuf4serv); 11250Sstevel@tonic-gate _nderror = __herrno2netdir(h_errnop); 11260Sstevel@tonic-gate return (_nderror); 11270Sstevel@tonic-gate 11280Sstevel@tonic-gate default: 11290Sstevel@tonic-gate _nderror = ND_BADARG; 11300Sstevel@tonic-gate return (_nderror); /* should never happen */ 11310Sstevel@tonic-gate } 11320Sstevel@tonic-gate 11330Sstevel@tonic-gate } 11340Sstevel@tonic-gate /* 11350Sstevel@tonic-gate * 3. We come this far only if nametoaddr libs are specified for 11360Sstevel@tonic-gate * inet transports and we are called by gethost/servbyname only. 11370Sstevel@tonic-gate */ 11380Sstevel@tonic-gate switch (args->op_t) { 11390Sstevel@tonic-gate struct netbuf nbuf; 11400Sstevel@tonic-gate struct nd_hostservlist *addrs; 11410Sstevel@tonic-gate struct sockaddr_in sa; 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate case NSS_HOST: 11440Sstevel@tonic-gate 1145132Srobinson /* LINTED pointer cast */ 11460Sstevel@tonic-gate sa.sin_addr.s_addr = *(uint32_t *)args->arg.nss.host.addr; 11470Sstevel@tonic-gate sa.sin_family = AF_INET; 11480Sstevel@tonic-gate /* Hopefully, third-parties get this optimization */ 11490Sstevel@tonic-gate sa.sin_port = 0; 11500Sstevel@tonic-gate nbuf.buf = (char *)&sa; 11510Sstevel@tonic-gate nbuf.len = nbuf.maxlen = sizeof (sa); 11520Sstevel@tonic-gate if ((_nderror = __classic_netdir_getbyaddr(nconf, 11530Sstevel@tonic-gate &addrs, &nbuf)) != 0) { 11540Sstevel@tonic-gate *(res->nss.host.herrno_p) = nd2herrno(_nderror); 11550Sstevel@tonic-gate return (_nderror); 11560Sstevel@tonic-gate } 11570Sstevel@tonic-gate /* 11580Sstevel@tonic-gate * convert the host-serv pairs into h_aliases and hent. 11590Sstevel@tonic-gate */ 11600Sstevel@tonic-gate _nderror = ndhostserv2hent(&nbuf, addrs, res->nss.host.hent, 11610Sstevel@tonic-gate args->arg.nss.host.buf, args->arg.nss.host.buflen); 11620Sstevel@tonic-gate if (_nderror != ND_OK) 11630Sstevel@tonic-gate *(res->nss.host.herrno_p) = nd2herrno(_nderror); 11640Sstevel@tonic-gate netdir_free((char *)addrs, ND_HOSTSERVLIST); 11650Sstevel@tonic-gate return (_nderror); 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate case NSS_SERV: 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate if (args->arg.nss.serv.proto == NULL) { 11700Sstevel@tonic-gate /* 11710Sstevel@tonic-gate * A similar HACK showed up in Solaris 2.3. 11720Sstevel@tonic-gate * The caller wild-carded proto -- i.e. will 11730Sstevel@tonic-gate * accept a match on tcp or udp for the port 11740Sstevel@tonic-gate * number. Since we have no hope of getting 11750Sstevel@tonic-gate * directly to a name service switch backend 11760Sstevel@tonic-gate * from here that understands this semantics, 11770Sstevel@tonic-gate * we try calling the netdir interfaces first 11780Sstevel@tonic-gate * with "tcp" and then "udp". 11790Sstevel@tonic-gate */ 11800Sstevel@tonic-gate args->arg.nss.serv.proto = "tcp"; 11810Sstevel@tonic-gate _nderror = _get_hostserv_inetnetdir_byaddr(nconf, args, 11820Sstevel@tonic-gate res); 11830Sstevel@tonic-gate if (_nderror != ND_OK) { 11840Sstevel@tonic-gate args->arg.nss.serv.proto = "udp"; 11850Sstevel@tonic-gate _nderror = 11860Sstevel@tonic-gate _get_hostserv_inetnetdir_byaddr(nconf, 11870Sstevel@tonic-gate args, res); 11880Sstevel@tonic-gate } 11890Sstevel@tonic-gate return (_nderror); 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate /* 11930Sstevel@tonic-gate * Third-party nametoaddr_libs should be optimized for 11940Sstevel@tonic-gate * this case. It also gives a special semantics twist to 11950Sstevel@tonic-gate * netdir_getbyaddr. Only for the INADDR_ANY case, it gives 11960Sstevel@tonic-gate * higher priority to service lookups (over host lookups). 11970Sstevel@tonic-gate * If service lookup fails, the backend returns ND_NOSERV to 11980Sstevel@tonic-gate * facilitate lookup in the "next" naming service. 11990Sstevel@tonic-gate * BugId: 1075403. 12000Sstevel@tonic-gate */ 12010Sstevel@tonic-gate sa.sin_addr.s_addr = INADDR_ANY; 12020Sstevel@tonic-gate sa.sin_family = AF_INET; 12030Sstevel@tonic-gate sa.sin_port = (ushort_t)args->arg.nss.serv.port; 12040Sstevel@tonic-gate sa.sin_zero[0] = '\0'; 12050Sstevel@tonic-gate nbuf.buf = (char *)&sa; 12060Sstevel@tonic-gate nbuf.len = nbuf.maxlen = sizeof (sa); 12070Sstevel@tonic-gate if ((_nderror = __classic_netdir_getbyaddr(nconf, 12080Sstevel@tonic-gate &addrs, &nbuf)) != ND_OK) { 12090Sstevel@tonic-gate return (_nderror); 12100Sstevel@tonic-gate } 12110Sstevel@tonic-gate /* 12120Sstevel@tonic-gate * convert the host-serv pairs into s_aliases and servent. 12130Sstevel@tonic-gate */ 12140Sstevel@tonic-gate _nderror = ndhostserv2srent(args->arg.nss.serv.port, 12150Sstevel@tonic-gate args->arg.nss.serv.proto, addrs, res->nss.serv, 12160Sstevel@tonic-gate args->arg.nss.serv.buf, args->arg.nss.serv.buflen); 12170Sstevel@tonic-gate netdir_free((char *)addrs, ND_HOSTSERVLIST); 12180Sstevel@tonic-gate return (_nderror); 12190Sstevel@tonic-gate 12200Sstevel@tonic-gate default: 12210Sstevel@tonic-gate _nderror = ND_BADARG; 12220Sstevel@tonic-gate return (_nderror); /* should never happen */ 12230Sstevel@tonic-gate } 12240Sstevel@tonic-gate } 12250Sstevel@tonic-gate 12260Sstevel@tonic-gate /* 12270Sstevel@tonic-gate * Part II: Name Service Switch interfacing routines. 12280Sstevel@tonic-gate */ 12290Sstevel@tonic-gate 12300Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root_hosts); 12310Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root_ipnodes); 12320Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root_services); 12330Sstevel@tonic-gate 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate /* 12360Sstevel@tonic-gate * There is a copy of __nss2herrno() in nsswitch/files/gethostent.c. 12370Sstevel@tonic-gate * It is there because /etc/lib/nss_files.so.1 cannot call 12380Sstevel@tonic-gate * routines in libnsl. Care should be taken to keep the two copies 12390Sstevel@tonic-gate * in sync. 12400Sstevel@tonic-gate */ 12410Sstevel@tonic-gate int 12420Sstevel@tonic-gate __nss2herrno(nss_status_t nsstat) 12430Sstevel@tonic-gate { 12440Sstevel@tonic-gate switch (nsstat) { 12450Sstevel@tonic-gate case NSS_SUCCESS: 12460Sstevel@tonic-gate /* no macro-defined success code for h_errno */ 12470Sstevel@tonic-gate return (0); 12480Sstevel@tonic-gate case NSS_NOTFOUND: 12490Sstevel@tonic-gate return (HOST_NOT_FOUND); 12500Sstevel@tonic-gate case NSS_TRYAGAIN: 12510Sstevel@tonic-gate return (TRY_AGAIN); 12520Sstevel@tonic-gate case NSS_UNAVAIL: 12530Sstevel@tonic-gate return (NO_RECOVERY); 12540Sstevel@tonic-gate } 12550Sstevel@tonic-gate /* NOTREACHED */ 12560Sstevel@tonic-gate return (0); /* keep gcc happy */ 12570Sstevel@tonic-gate } 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate nss_status_t 12600Sstevel@tonic-gate _herrno2nss(int h_errno) 12610Sstevel@tonic-gate { 12620Sstevel@tonic-gate switch (h_errno) { 12630Sstevel@tonic-gate case 0: 12640Sstevel@tonic-gate return (NSS_SUCCESS); 12650Sstevel@tonic-gate case TRY_AGAIN: 12660Sstevel@tonic-gate return (NSS_TRYAGAIN); 12670Sstevel@tonic-gate case NO_RECOVERY: 12680Sstevel@tonic-gate case NETDB_INTERNAL: 12690Sstevel@tonic-gate return (NSS_UNAVAIL); 12700Sstevel@tonic-gate case HOST_NOT_FOUND: 12710Sstevel@tonic-gate case NO_DATA: 12720Sstevel@tonic-gate default: 12730Sstevel@tonic-gate return (NSS_NOTFOUND); 12740Sstevel@tonic-gate } 12750Sstevel@tonic-gate } 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate static int 12780Sstevel@tonic-gate __herrno2netdir(int h_errnop) 12790Sstevel@tonic-gate { 12800Sstevel@tonic-gate switch (h_errnop) { 12810Sstevel@tonic-gate case 0: 12820Sstevel@tonic-gate return (ND_OK); 12830Sstevel@tonic-gate case HOST_NOT_FOUND: 12840Sstevel@tonic-gate return (ND_NOHOST); 12850Sstevel@tonic-gate case TRY_AGAIN: 12860Sstevel@tonic-gate return (ND_TRY_AGAIN); 12870Sstevel@tonic-gate case NO_RECOVERY: 12880Sstevel@tonic-gate case NETDB_INTERNAL: 12890Sstevel@tonic-gate return (ND_NO_RECOVERY); 12900Sstevel@tonic-gate case NO_DATA: 12910Sstevel@tonic-gate return (ND_NO_DATA); 12920Sstevel@tonic-gate default: 12930Sstevel@tonic-gate return (ND_NOHOST); 12940Sstevel@tonic-gate } 12950Sstevel@tonic-gate } 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate /* 12980Sstevel@tonic-gate * The _switch_getXXbyYY_r() routines should be static. They used to 12990Sstevel@tonic-gate * be exported in SunOS 5.3, and in fact publicised as work-around 13000Sstevel@tonic-gate * interfaces for getting CNAME/aliases, and therefore, we preserve 13010Sstevel@tonic-gate * their signatures here. Just in case. 13020Sstevel@tonic-gate */ 13030Sstevel@tonic-gate 13040Sstevel@tonic-gate struct hostent * 13050Sstevel@tonic-gate _switch_gethostbyname_r(const char *name, struct hostent *result, char *buffer, 13060Sstevel@tonic-gate int buflen, int *h_errnop) 13070Sstevel@tonic-gate { 13080Sstevel@tonic-gate nss_XbyY_args_t arg; 13090Sstevel@tonic-gate nss_status_t res; 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent); 13120Sstevel@tonic-gate arg.key.name = name; 13130Sstevel@tonic-gate arg.stayopen = 0; 13140Sstevel@tonic-gate res = nss_search(&db_root_hosts, _nss_initf_hosts, 13150Sstevel@tonic-gate NSS_DBOP_HOSTS_BYNAME, &arg); 13160Sstevel@tonic-gate arg.status = res; 13170Sstevel@tonic-gate *h_errnop = arg.h_errno; 13180Sstevel@tonic-gate if (arg.returnval != NULL) 13190Sstevel@tonic-gate order_haddrlist_af(result->h_addrtype, result->h_addr_list); 13200Sstevel@tonic-gate return ((struct hostent *)NSS_XbyY_FINI(&arg)); 13210Sstevel@tonic-gate } 13220Sstevel@tonic-gate 13230Sstevel@tonic-gate struct hostent * 13240Sstevel@tonic-gate _switch_getipnodebyname_r(const char *name, struct hostent *result, 13250Sstevel@tonic-gate char *buffer, int buflen, int af_family, int flags, int *h_errnop) 13260Sstevel@tonic-gate { 13270Sstevel@tonic-gate nss_XbyY_args_t arg; 13280Sstevel@tonic-gate nss_status_t res; 13290Sstevel@tonic-gate 13300Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent6); 13310Sstevel@tonic-gate arg.key.ipnode.name = name; 13320Sstevel@tonic-gate arg.key.ipnode.af_family = af_family; 13330Sstevel@tonic-gate arg.key.ipnode.flags = flags; 13340Sstevel@tonic-gate arg.stayopen = 0; 13350Sstevel@tonic-gate res = nss_search(&db_root_ipnodes, _nss_initf_ipnodes, 13360Sstevel@tonic-gate NSS_DBOP_IPNODES_BYNAME, &arg); 13370Sstevel@tonic-gate arg.status = res; 13380Sstevel@tonic-gate *h_errnop = arg.h_errno; 13390Sstevel@tonic-gate if (arg.returnval != NULL) 13400Sstevel@tonic-gate order_haddrlist_af(result->h_addrtype, result->h_addr_list); 13410Sstevel@tonic-gate return ((struct hostent *)NSS_XbyY_FINI(&arg)); 13420Sstevel@tonic-gate } 13430Sstevel@tonic-gate 13440Sstevel@tonic-gate struct hostent * 13450Sstevel@tonic-gate _switch_gethostbyaddr_r(const char *addr, int len, int type, 13460Sstevel@tonic-gate struct hostent *result, char *buffer, int buflen, int *h_errnop) 13470Sstevel@tonic-gate { 13480Sstevel@tonic-gate nss_XbyY_args_t arg; 13490Sstevel@tonic-gate nss_status_t res; 13500Sstevel@tonic-gate 13510Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent); 13520Sstevel@tonic-gate arg.key.hostaddr.addr = addr; 13530Sstevel@tonic-gate arg.key.hostaddr.len = len; 13540Sstevel@tonic-gate arg.key.hostaddr.type = type; 13550Sstevel@tonic-gate arg.stayopen = 0; 13560Sstevel@tonic-gate res = nss_search(&db_root_hosts, _nss_initf_hosts, 13570Sstevel@tonic-gate NSS_DBOP_HOSTS_BYADDR, &arg); 13580Sstevel@tonic-gate arg.status = res; 13590Sstevel@tonic-gate *h_errnop = arg.h_errno; 13600Sstevel@tonic-gate return (struct hostent *)NSS_XbyY_FINI(&arg); 13610Sstevel@tonic-gate } 13620Sstevel@tonic-gate 13630Sstevel@tonic-gate struct hostent * 13640Sstevel@tonic-gate _switch_getipnodebyaddr_r(const char *addr, int len, int type, 13650Sstevel@tonic-gate struct hostent *result, char *buffer, int buflen, int *h_errnop) 13660Sstevel@tonic-gate { 13670Sstevel@tonic-gate nss_XbyY_args_t arg; 13680Sstevel@tonic-gate nss_status_t res; 13690Sstevel@tonic-gate 13700Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent6); 13710Sstevel@tonic-gate arg.key.hostaddr.addr = addr; 13720Sstevel@tonic-gate arg.key.hostaddr.len = len; 13730Sstevel@tonic-gate arg.key.hostaddr.type = type; 13740Sstevel@tonic-gate arg.stayopen = 0; 13750Sstevel@tonic-gate res = nss_search(&db_root_ipnodes, _nss_initf_ipnodes, 13760Sstevel@tonic-gate NSS_DBOP_IPNODES_BYADDR, &arg); 13770Sstevel@tonic-gate arg.status = res; 13780Sstevel@tonic-gate *h_errnop = arg.h_errno; 13790Sstevel@tonic-gate return (struct hostent *)NSS_XbyY_FINI(&arg); 13800Sstevel@tonic-gate } 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate static void 13830Sstevel@tonic-gate _nss_initf_services(nss_db_params_t *p) 13840Sstevel@tonic-gate { 13850Sstevel@tonic-gate p->name = NSS_DBNAM_SERVICES; 13860Sstevel@tonic-gate p->default_config = NSS_DEFCONF_SERVICES; 13870Sstevel@tonic-gate } 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate struct servent * 13900Sstevel@tonic-gate _switch_getservbyname_r(const char *name, const char *proto, 13910Sstevel@tonic-gate struct servent *result, char *buffer, int buflen) 13920Sstevel@tonic-gate { 13930Sstevel@tonic-gate nss_XbyY_args_t arg; 13940Sstevel@tonic-gate nss_status_t res; 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2servent); 13970Sstevel@tonic-gate arg.key.serv.serv.name = name; 13980Sstevel@tonic-gate arg.key.serv.proto = proto; 13990Sstevel@tonic-gate arg.stayopen = 0; 14000Sstevel@tonic-gate res = nss_search(&db_root_services, _nss_initf_services, 14010Sstevel@tonic-gate NSS_DBOP_SERVICES_BYNAME, &arg); 14020Sstevel@tonic-gate arg.status = res; 14030Sstevel@tonic-gate return ((struct servent *)NSS_XbyY_FINI(&arg)); 14040Sstevel@tonic-gate } 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate struct servent * 14070Sstevel@tonic-gate _switch_getservbyport_r(int port, const char *proto, struct servent *result, 14080Sstevel@tonic-gate char *buffer, int buflen) 14090Sstevel@tonic-gate { 14100Sstevel@tonic-gate nss_XbyY_args_t arg; 14110Sstevel@tonic-gate nss_status_t res; 14120Sstevel@tonic-gate 14130Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2servent); 14140Sstevel@tonic-gate arg.key.serv.serv.port = port; 14150Sstevel@tonic-gate arg.key.serv.proto = proto; 14160Sstevel@tonic-gate arg.stayopen = 0; 14170Sstevel@tonic-gate res = nss_search(&db_root_services, _nss_initf_services, 14180Sstevel@tonic-gate NSS_DBOP_SERVICES_BYPORT, &arg); 14190Sstevel@tonic-gate arg.status = res; 14200Sstevel@tonic-gate return ((struct servent *)NSS_XbyY_FINI(&arg)); 14210Sstevel@tonic-gate } 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate 14240Sstevel@tonic-gate /* 14250Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 14260Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 14270Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 14280Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 14290Sstevel@tonic-gate * 14300Sstevel@tonic-gate * Defined here because we need it and we (libnsl) cannot have a dependency 14310Sstevel@tonic-gate * on libsocket (however, libsocket always depends on libnsl). 14320Sstevel@tonic-gate */ 14330Sstevel@tonic-gate int 14340Sstevel@tonic-gate str2servent(const char *instr, int lenstr, void *ent, char *buffer, int buflen) 14350Sstevel@tonic-gate { 14360Sstevel@tonic-gate struct servent *serv = (struct servent *)ent; 14370Sstevel@tonic-gate const char *p, *fieldstart, *limit, *namestart; 14380Sstevel@tonic-gate ssize_t fieldlen, namelen = 0; 14390Sstevel@tonic-gate char numbuf[12]; 14400Sstevel@tonic-gate char *numend; 14410Sstevel@tonic-gate 14420Sstevel@tonic-gate if ((instr >= buffer && (buffer + buflen) > instr) || 14430Sstevel@tonic-gate (buffer >= instr && (instr + lenstr) > buffer)) { 14440Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 14450Sstevel@tonic-gate } 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate p = instr; 14480Sstevel@tonic-gate limit = p + lenstr; 14490Sstevel@tonic-gate 14500Sstevel@tonic-gate while (p < limit && isspace(*p)) { 14510Sstevel@tonic-gate p++; 14520Sstevel@tonic-gate } 14530Sstevel@tonic-gate namestart = p; 14540Sstevel@tonic-gate while (p < limit && !isspace(*p)) { 14550Sstevel@tonic-gate p++; /* Skip over the canonical name */ 14560Sstevel@tonic-gate } 14570Sstevel@tonic-gate namelen = p - namestart; 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate if (buflen <= namelen) { /* not enough buffer */ 14600Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 14610Sstevel@tonic-gate } 14620Sstevel@tonic-gate (void) memcpy(buffer, namestart, namelen); 14630Sstevel@tonic-gate buffer[namelen] = '\0'; 14640Sstevel@tonic-gate serv->s_name = buffer; 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate while (p < limit && isspace(*p)) { 14670Sstevel@tonic-gate p++; 14680Sstevel@tonic-gate } 14690Sstevel@tonic-gate 14700Sstevel@tonic-gate fieldstart = p; 14710Sstevel@tonic-gate do { 14720Sstevel@tonic-gate if (p > limit || isspace(*p)) { 14730Sstevel@tonic-gate /* Syntax error -- no port/proto */ 14740Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 14750Sstevel@tonic-gate } 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate while (*p++ != '/'); 14780Sstevel@tonic-gate fieldlen = p - fieldstart - 1; 14790Sstevel@tonic-gate if (fieldlen == 0 || fieldlen >= sizeof (numbuf)) { 14800Sstevel@tonic-gate /* Syntax error -- supposed number is empty or too long */ 14810Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 14820Sstevel@tonic-gate } 14830Sstevel@tonic-gate (void) memcpy(numbuf, fieldstart, fieldlen); 14840Sstevel@tonic-gate numbuf[fieldlen] = '\0'; 14850Sstevel@tonic-gate serv->s_port = htons((int)strtol(numbuf, &numend, 10)); 14860Sstevel@tonic-gate if (*numend != '\0') { 14870Sstevel@tonic-gate /* Syntax error -- port number isn't a number */ 14880Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 14890Sstevel@tonic-gate } 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate fieldstart = p; 14920Sstevel@tonic-gate while (p < limit && !isspace(*p)) { 14930Sstevel@tonic-gate p++; /* Scan the protocol name */ 14940Sstevel@tonic-gate } 14950Sstevel@tonic-gate fieldlen = p - fieldstart + 1; /* Include '\0' this time */ 14960Sstevel@tonic-gate if (fieldlen > buflen - namelen - 1) { 14970Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 14980Sstevel@tonic-gate } 14990Sstevel@tonic-gate serv->s_proto = buffer + namelen + 1; 15000Sstevel@tonic-gate (void) memcpy(serv->s_proto, fieldstart, fieldlen - 1); 15010Sstevel@tonic-gate serv->s_proto[fieldlen - 1] = '\0'; 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate while (p < limit && isspace(*p)) { 15040Sstevel@tonic-gate p++; 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate /* 15070Sstevel@tonic-gate * Although nss_files_XY_all calls us with # stripped, 15080Sstevel@tonic-gate * we should be able to deal with it here in order to 15090Sstevel@tonic-gate * be more useful. 15100Sstevel@tonic-gate */ 15110Sstevel@tonic-gate if (p >= limit || *p == '#') { /* no aliases, no problem */ 15120Sstevel@tonic-gate char **ptr; 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate ptr = (char **)ROUND_UP(buffer + namelen + 1 + fieldlen, 15150Sstevel@tonic-gate sizeof (char *)); 15160Sstevel@tonic-gate if ((char *)ptr >= buffer + buflen) { 15170Sstevel@tonic-gate /* hope they don't try to peek in */ 15180Sstevel@tonic-gate serv->s_aliases = 0; 15190Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 15200Sstevel@tonic-gate } else { 15210Sstevel@tonic-gate *ptr = 0; 15220Sstevel@tonic-gate serv->s_aliases = ptr; 15230Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 15240Sstevel@tonic-gate } 15250Sstevel@tonic-gate } 15260Sstevel@tonic-gate serv->s_aliases = _nss_netdb_aliases(p, (int)(lenstr - (p - instr)), 15270Sstevel@tonic-gate buffer + namelen + 1 + fieldlen, 15280Sstevel@tonic-gate (int)(buflen - namelen - 1 - fieldlen)); 15290Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 15300Sstevel@tonic-gate } 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate /* 15330Sstevel@tonic-gate * Part III: All `n sundry routines that are useful only in this 15340Sstevel@tonic-gate * module. In the interest of keeping this source file shorter, 15350Sstevel@tonic-gate * we would create them a new module only if the linker allowed 15360Sstevel@tonic-gate * "library-static" functions. 15370Sstevel@tonic-gate * 15380Sstevel@tonic-gate * Routines to order addresses based on local interfaces and netmasks, 15390Sstevel@tonic-gate * to get and check reserved ports, and to get broadcast nets. 15400Sstevel@tonic-gate */ 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate union __v4v6addr { 15430Sstevel@tonic-gate struct in6_addr in6; 15440Sstevel@tonic-gate struct in_addr in4; 15450Sstevel@tonic-gate }; 15460Sstevel@tonic-gate 15470Sstevel@tonic-gate struct __ifaddr { 15480Sstevel@tonic-gate sa_family_t af; 15490Sstevel@tonic-gate union __v4v6addr addr; 15500Sstevel@tonic-gate union __v4v6addr mask; 15510Sstevel@tonic-gate }; 15520Sstevel@tonic-gate 15530Sstevel@tonic-gate struct ifinfo { 15540Sstevel@tonic-gate int count; 15550Sstevel@tonic-gate struct __ifaddr *addresses; 15560Sstevel@tonic-gate }; 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate typedef enum {ADDR_ONLINK = 0, ADDR_OFFLINK} addr_class_t; 15590Sstevel@tonic-gate #define ADDR_NUMCLASSES 2 15600Sstevel@tonic-gate 15610Sstevel@tonic-gate typedef enum {IF_ADDR, IF_MASK} __ifaddr_type; 15620Sstevel@tonic-gate static int __inet_ifassign(sa_family_t, struct __ifaddr *, __ifaddr_type, 15630Sstevel@tonic-gate void *); 15640Sstevel@tonic-gate int __inet_address_is_local_af(void *, sa_family_t, void *); 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate #define ifaf(index) (localinfo->addresses[index].af) 15670Sstevel@tonic-gate #define ifaddr4(index) (localinfo->addresses[index].addr.in4) 15680Sstevel@tonic-gate #define ifaddr6(index) (localinfo->addresses[index].addr.in6) 15690Sstevel@tonic-gate #define ifmask4(index) (localinfo->addresses[index].mask.in4) 15700Sstevel@tonic-gate #define ifmask6(index) (localinfo->addresses[index].mask.in6) 15710Sstevel@tonic-gate #define ifinfosize(n) (sizeof (struct ifinfo) + (n)*sizeof (struct __ifaddr)) 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate #define lifraddrp(lifr) ((lifr.lifr_addr.ss_family == AF_INET6) ? \ 15740Sstevel@tonic-gate (void *)&((struct sockaddr_in6 *)&lifr.lifr_addr)->sin6_addr : \ 15750Sstevel@tonic-gate (void *)&((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr) 15760Sstevel@tonic-gate 15770Sstevel@tonic-gate #define ifassign(lifr, index, type) \ 15780Sstevel@tonic-gate __inet_ifassign(lifr.lifr_addr.ss_family, \ 15790Sstevel@tonic-gate &localinfo->addresses[index], type, \ 15800Sstevel@tonic-gate lifraddrp(lifr)) 15810Sstevel@tonic-gate 15820Sstevel@tonic-gate /* 15830Sstevel@tonic-gate * The number of nanoseconds the order_haddrlist_inet() function waits 15840Sstevel@tonic-gate * to retreive IP interface information. The default is five minutes. 15850Sstevel@tonic-gate */ 15860Sstevel@tonic-gate #define IFINFOTIMEOUT ((hrtime_t)300 * NANOSEC) 15870Sstevel@tonic-gate 15880Sstevel@tonic-gate /* 15890Sstevel@tonic-gate * Sort the addresses in haddrlist. Since the sorting algorithms are 15900Sstevel@tonic-gate * address-family specific, the work is done in the address-family 15910Sstevel@tonic-gate * specific order_haddrlist_<family> functions. 15920Sstevel@tonic-gate * 15930Sstevel@tonic-gate * Do not sort addresses if SORT_ADDRS variable is set to NO or FALSE 15940Sstevel@tonic-gate * in the configuration file /etc/default/nss. This is useful in case 15950Sstevel@tonic-gate * the order of addresses returned by the nameserver needs to be 15960Sstevel@tonic-gate * maintained. (DNS round robin feature is one example) 15970Sstevel@tonic-gate */ 15980Sstevel@tonic-gate void 15990Sstevel@tonic-gate order_haddrlist_af(sa_family_t af, char **haddrlist) 16000Sstevel@tonic-gate { 16010Sstevel@tonic-gate size_t addrcount; 16020Sstevel@tonic-gate char **addrptr; 16030Sstevel@tonic-gate static boolean_t checksortcfg = B_TRUE; 16040Sstevel@tonic-gate static boolean_t nosort = B_FALSE; 16050Sstevel@tonic-gate static mutex_t checksortcfg_lock = DEFAULTMUTEX; 16060Sstevel@tonic-gate 16070Sstevel@tonic-gate if (haddrlist == NULL) 16080Sstevel@tonic-gate return; 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate /* 16110Sstevel@tonic-gate * Check if SORT_ADDRS is set to NO or FALSE in the configuration 16120Sstevel@tonic-gate * file. We do not have to sort addresses in that case. 16130Sstevel@tonic-gate */ 16140Sstevel@tonic-gate (void) mutex_lock(&checksortcfg_lock); 16150Sstevel@tonic-gate if (checksortcfg == B_TRUE) { 16160Sstevel@tonic-gate checksortcfg = B_FALSE; 16170Sstevel@tonic-gate nosort = _read_nsw_file(); 16180Sstevel@tonic-gate } 16190Sstevel@tonic-gate (void) mutex_unlock(&checksortcfg_lock); 16200Sstevel@tonic-gate 16210Sstevel@tonic-gate if (nosort) 16220Sstevel@tonic-gate return; 16230Sstevel@tonic-gate 16240Sstevel@tonic-gate /* Count the addresses to sort */ 16250Sstevel@tonic-gate addrcount = 0; 16260Sstevel@tonic-gate for (addrptr = haddrlist; *addrptr != NULL; addrptr++) 16270Sstevel@tonic-gate addrcount++; 16280Sstevel@tonic-gate 16290Sstevel@tonic-gate /* 16300Sstevel@tonic-gate * If there's only one address or no addresses to sort, then 16310Sstevel@tonic-gate * there's nothing for us to do. 16320Sstevel@tonic-gate */ 16330Sstevel@tonic-gate if (addrcount <= 1) 16340Sstevel@tonic-gate return; 16350Sstevel@tonic-gate 16360Sstevel@tonic-gate /* Call the address-family specific sorting functions. */ 16370Sstevel@tonic-gate switch (af) { 16380Sstevel@tonic-gate case AF_INET: 16390Sstevel@tonic-gate order_haddrlist_inet(haddrlist, addrcount); 16400Sstevel@tonic-gate break; 16410Sstevel@tonic-gate case AF_INET6: 16420Sstevel@tonic-gate order_haddrlist_inet6(haddrlist, addrcount); 16430Sstevel@tonic-gate break; 16440Sstevel@tonic-gate default: 16450Sstevel@tonic-gate break; 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate } 16480Sstevel@tonic-gate 16490Sstevel@tonic-gate /* 16500Sstevel@tonic-gate * Move any local (on-link) addresses toward the beginning of haddrlist. 16510Sstevel@tonic-gate * The order within these two classes is preserved. 16520Sstevel@tonic-gate * 16530Sstevel@tonic-gate * The interface list is retrieved no more often than every 16540Sstevel@tonic-gate * IFINFOTIMEOUT nanoseconds. Access to the interface list is 16550Sstevel@tonic-gate * protected by an RW lock. 16560Sstevel@tonic-gate * 16570Sstevel@tonic-gate * If this function encounters an error, haddrlist is unaltered. 16580Sstevel@tonic-gate */ 16590Sstevel@tonic-gate static void 16600Sstevel@tonic-gate order_haddrlist_inet(char **haddrlist, size_t addrcount) 16610Sstevel@tonic-gate { 16620Sstevel@tonic-gate static struct ifinfo *localinfo = NULL; 16630Sstevel@tonic-gate static hrtime_t then = 0; /* the last time localinfo was updated */ 16640Sstevel@tonic-gate hrtime_t now; 16650Sstevel@tonic-gate static rwlock_t localinfo_lock = DEFAULTRWLOCK; 16660Sstevel@tonic-gate uint8_t *sortbuf; 16670Sstevel@tonic-gate size_t sortbuf_size; 16680Sstevel@tonic-gate struct in_addr **inaddrlist = (struct in_addr **)haddrlist; 16690Sstevel@tonic-gate struct in_addr **sorted; 16700Sstevel@tonic-gate struct in_addr **classnext[ADDR_NUMCLASSES]; 16710Sstevel@tonic-gate uint_t classcount[ADDR_NUMCLASSES]; 16720Sstevel@tonic-gate addr_class_t *sortclass; 16730Sstevel@tonic-gate int i; 16740Sstevel@tonic-gate int rc; 16750Sstevel@tonic-gate 16760Sstevel@tonic-gate 16770Sstevel@tonic-gate /* 16780Sstevel@tonic-gate * The classes in the sortclass array correspond to the class 16790Sstevel@tonic-gate * of the address in the haddrlist list of the same index. 16800Sstevel@tonic-gate * The classes are: 16810Sstevel@tonic-gate * 16820Sstevel@tonic-gate * ADDR_ONLINK on-link address 16830Sstevel@tonic-gate * ADDR_OFFLINK off-link address 16840Sstevel@tonic-gate */ 16850Sstevel@tonic-gate sortbuf_size = addrcount * 16860Sstevel@tonic-gate (sizeof (struct in_addr *) + sizeof (addr_class_t)); 16870Sstevel@tonic-gate if ((sortbuf = malloc(sortbuf_size)) == NULL) 16880Sstevel@tonic-gate return; 1689132Srobinson /* LINTED pointer cast */ 16900Sstevel@tonic-gate sorted = (struct in_addr **)sortbuf; 1691132Srobinson /* LINTED pointer cast */ 16920Sstevel@tonic-gate sortclass = (addr_class_t *)(sortbuf + 16930Sstevel@tonic-gate (addrcount * sizeof (struct in_addr *))); 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate /* 16960Sstevel@tonic-gate * Get a read lock, and check if the interface information 16970Sstevel@tonic-gate * is too old. 16980Sstevel@tonic-gate */ 16990Sstevel@tonic-gate (void) rw_rdlock(&localinfo_lock); 17000Sstevel@tonic-gate now = gethrtime(); 17010Sstevel@tonic-gate if (localinfo == NULL || ((now - then) > IFINFOTIMEOUT)) { 17020Sstevel@tonic-gate /* Need to update I/F info. Upgrade to write lock. */ 17030Sstevel@tonic-gate (void) rw_unlock(&localinfo_lock); 17040Sstevel@tonic-gate (void) rw_wrlock(&localinfo_lock); 17050Sstevel@tonic-gate /* 17060Sstevel@tonic-gate * Another thread might have updated "then" between 17070Sstevel@tonic-gate * the rw_unlock() and rw_wrlock() calls above, so 17080Sstevel@tonic-gate * re-check the timeout. 17090Sstevel@tonic-gate */ 17100Sstevel@tonic-gate if (localinfo == NULL || ((now - then) > IFINFOTIMEOUT)) { 17110Sstevel@tonic-gate if (localinfo != NULL) 17120Sstevel@tonic-gate free(localinfo); 17130Sstevel@tonic-gate if ((localinfo = get_local_info()) == NULL) { 17140Sstevel@tonic-gate (void) rw_unlock(&localinfo_lock); 17150Sstevel@tonic-gate free(sortbuf); 17160Sstevel@tonic-gate return; 17170Sstevel@tonic-gate } 17180Sstevel@tonic-gate then = now; 17190Sstevel@tonic-gate } 17200Sstevel@tonic-gate /* Downgrade to read lock */ 17210Sstevel@tonic-gate (void) rw_unlock(&localinfo_lock); 17220Sstevel@tonic-gate (void) rw_rdlock(&localinfo_lock); 17230Sstevel@tonic-gate /* 17240Sstevel@tonic-gate * Another thread may have updated the I/F info, 17250Sstevel@tonic-gate * so verify that the 'localinfo' pointer still 17260Sstevel@tonic-gate * is non-NULL. 17270Sstevel@tonic-gate */ 17280Sstevel@tonic-gate if (localinfo == NULL) { 17290Sstevel@tonic-gate (void) rw_unlock(&localinfo_lock); 17300Sstevel@tonic-gate free(sortbuf); 17310Sstevel@tonic-gate return; 17320Sstevel@tonic-gate } 17330Sstevel@tonic-gate } 17340Sstevel@tonic-gate 17350Sstevel@tonic-gate /* 17360Sstevel@tonic-gate * Classify the addresses. We also maintain the classcount 17370Sstevel@tonic-gate * array to keep track of the number of addresses in each 17380Sstevel@tonic-gate * class. 17390Sstevel@tonic-gate */ 1740132Srobinson (void) memset(classcount, 0, sizeof (classcount)); 17410Sstevel@tonic-gate for (i = 0; i < addrcount; i++) { 17420Sstevel@tonic-gate if (__inet_address_is_local_af(localinfo, AF_INET, 17430Sstevel@tonic-gate inaddrlist[i])) 17440Sstevel@tonic-gate sortclass[i] = ADDR_ONLINK; 17450Sstevel@tonic-gate else 17460Sstevel@tonic-gate sortclass[i] = ADDR_OFFLINK; 17470Sstevel@tonic-gate classcount[sortclass[i]]++; 17480Sstevel@tonic-gate } 17490Sstevel@tonic-gate 17500Sstevel@tonic-gate /* Don't need the interface list anymore in this call */ 17510Sstevel@tonic-gate (void) rw_unlock(&localinfo_lock); 17520Sstevel@tonic-gate 17530Sstevel@tonic-gate /* 17540Sstevel@tonic-gate * Each element in the classnext array points to the next 17550Sstevel@tonic-gate * element for that class in the sorted address list. 'rc' is 17560Sstevel@tonic-gate * the running count of elements as we sum the class 17570Sstevel@tonic-gate * sub-totals. 17580Sstevel@tonic-gate */ 17590Sstevel@tonic-gate for (rc = 0, i = 0; i < ADDR_NUMCLASSES; i++) { 17600Sstevel@tonic-gate classnext[i] = &sorted[rc]; 17610Sstevel@tonic-gate rc += classcount[i]; 17620Sstevel@tonic-gate } 17630Sstevel@tonic-gate 17640Sstevel@tonic-gate /* Now for the actual rearrangement of the addresses */ 17650Sstevel@tonic-gate for (i = 0; i < addrcount; i++) { 17660Sstevel@tonic-gate *(classnext[sortclass[i]]) = inaddrlist[i]; 17670Sstevel@tonic-gate classnext[sortclass[i]]++; 17680Sstevel@tonic-gate } 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate /* Copy the sorted list to inaddrlist */ 17710Sstevel@tonic-gate (void) memcpy(inaddrlist, sorted, 17720Sstevel@tonic-gate addrcount * sizeof (struct in_addr *)); 17730Sstevel@tonic-gate free(sortbuf); 17740Sstevel@tonic-gate } 17750Sstevel@tonic-gate 17760Sstevel@tonic-gate /* 17770Sstevel@tonic-gate * This function implements the IPv6 Default Address Selection's 17780Sstevel@tonic-gate * destination address ordering mechanism. The algorithm is described 17790Sstevel@tonic-gate * in getaddrinfo(3SOCKET). 17800Sstevel@tonic-gate */ 17810Sstevel@tonic-gate static void 17820Sstevel@tonic-gate order_haddrlist_inet6(char **haddrlist, size_t addrcount) 17830Sstevel@tonic-gate { 17840Sstevel@tonic-gate struct dstinforeq *dinfo, *dinfoptr; 17850Sstevel@tonic-gate struct in6_addr **in6addrlist = (struct in6_addr **)haddrlist; 17860Sstevel@tonic-gate struct in6_addr **in6addr; 17870Sstevel@tonic-gate 17880Sstevel@tonic-gate if ((dinfo = calloc(addrcount, sizeof (struct dstinforeq))) == NULL) 17890Sstevel@tonic-gate return; 17900Sstevel@tonic-gate 17910Sstevel@tonic-gate /* Initialize the dstinfo array we'll use for SIOCGDSTINFO */ 17920Sstevel@tonic-gate dinfoptr = dinfo; 17930Sstevel@tonic-gate for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) { 17940Sstevel@tonic-gate dinfoptr->dir_daddr = **in6addr; 17950Sstevel@tonic-gate dinfoptr++; 17960Sstevel@tonic-gate } 17970Sstevel@tonic-gate 17980Sstevel@tonic-gate if (nss_strioctl(AF_INET6, SIOCGDSTINFO, dinfo, 17990Sstevel@tonic-gate addrcount * sizeof (struct dstinforeq)) < 0) { 18000Sstevel@tonic-gate free(dinfo); 18010Sstevel@tonic-gate return; 18020Sstevel@tonic-gate } 18030Sstevel@tonic-gate 18040Sstevel@tonic-gate /* Sort the dinfo array */ 18050Sstevel@tonic-gate qsort(dinfo, addrcount, sizeof (struct dstinforeq), dstcmp); 18060Sstevel@tonic-gate 18070Sstevel@tonic-gate /* Copy the addresses back into in6addrlist */ 18080Sstevel@tonic-gate dinfoptr = dinfo; 18090Sstevel@tonic-gate for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) { 18100Sstevel@tonic-gate **in6addr = dinfoptr->dir_daddr; 18110Sstevel@tonic-gate dinfoptr++; 18120Sstevel@tonic-gate } 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate free(dinfo); 18150Sstevel@tonic-gate } 18160Sstevel@tonic-gate 18170Sstevel@tonic-gate /* 18180Sstevel@tonic-gate * Determine number of leading bits that are common between two addresses. 18190Sstevel@tonic-gate * Only consider bits which fall within the prefix length plen. 18200Sstevel@tonic-gate */ 18210Sstevel@tonic-gate static uint_t 18220Sstevel@tonic-gate ip_addr_commonbits_v6(const in6_addr_t *a1, const in6_addr_t *a2) 18230Sstevel@tonic-gate { 18240Sstevel@tonic-gate uint_t bits; 18250Sstevel@tonic-gate uint_t i; 18260Sstevel@tonic-gate uint32_t diff; /* Bits that differ */ 18270Sstevel@tonic-gate 18280Sstevel@tonic-gate for (i = 0; i < 4; i++) { 18290Sstevel@tonic-gate if (a1->_S6_un._S6_u32[i] != a2->_S6_un._S6_u32[i]) 18300Sstevel@tonic-gate break; 18310Sstevel@tonic-gate } 18320Sstevel@tonic-gate bits = i * 32; 18330Sstevel@tonic-gate 18340Sstevel@tonic-gate if (bits == IPV6_ABITS) 18350Sstevel@tonic-gate return (IPV6_ABITS); 18360Sstevel@tonic-gate 18370Sstevel@tonic-gate /* 18380Sstevel@tonic-gate * Find number of leading common bits in the word which might 18390Sstevel@tonic-gate * have some common bits by searching for the first one from the left 18400Sstevel@tonic-gate * in the xor of the two addresses. 18410Sstevel@tonic-gate */ 18420Sstevel@tonic-gate diff = ntohl(a1->_S6_un._S6_u32[i] ^ a2->_S6_un._S6_u32[i]); 18430Sstevel@tonic-gate if (diff & 0xffff0000ul) 18440Sstevel@tonic-gate diff >>= 16; 18450Sstevel@tonic-gate else 18460Sstevel@tonic-gate bits += 16; 18470Sstevel@tonic-gate if (diff & 0xff00) 18480Sstevel@tonic-gate diff >>= 8; 18490Sstevel@tonic-gate else 18500Sstevel@tonic-gate bits += 8; 18510Sstevel@tonic-gate if (diff & 0xf0) 18520Sstevel@tonic-gate diff >>= 4; 18530Sstevel@tonic-gate else 18540Sstevel@tonic-gate bits += 4; 18550Sstevel@tonic-gate if (diff & 0xc) 18560Sstevel@tonic-gate diff >>= 2; 18570Sstevel@tonic-gate else 18580Sstevel@tonic-gate bits += 2; 18590Sstevel@tonic-gate if (!(diff & 2)) 18600Sstevel@tonic-gate bits++; 18610Sstevel@tonic-gate 18620Sstevel@tonic-gate /* 18630Sstevel@tonic-gate * We don't need to shift and check for the last bit. The 18640Sstevel@tonic-gate * check for IPV6_ABITS above would have caught that. 18650Sstevel@tonic-gate */ 18660Sstevel@tonic-gate 18670Sstevel@tonic-gate return (bits); 18680Sstevel@tonic-gate } 18690Sstevel@tonic-gate 18700Sstevel@tonic-gate 18710Sstevel@tonic-gate /* 18720Sstevel@tonic-gate * The following group of functions named rule_*() are individual 18730Sstevel@tonic-gate * sorting rules for the AF_INET6 address sorting algorithm. The 18740Sstevel@tonic-gate * functions compare two addresses (described by two dstinforeq 18750Sstevel@tonic-gate * structures), and determines if one is "greater" than the other, or 18760Sstevel@tonic-gate * if the two are equal according to that rule. 18770Sstevel@tonic-gate */ 18780Sstevel@tonic-gate typedef int (*rulef_t)(const struct dstinforeq *, const struct dstinforeq *); 18790Sstevel@tonic-gate 18800Sstevel@tonic-gate /* 18810Sstevel@tonic-gate * These values of these constants are no accident. Since qsort() 18820Sstevel@tonic-gate * implements the AF_INET6 address sorting, the comparison function 18830Sstevel@tonic-gate * must return an integer less than, equal to, or greater than zero to 18840Sstevel@tonic-gate * indicate if the first address is considered "less than", "equal 18850Sstevel@tonic-gate * to", or "greater than" the second one. Since we want the best 18860Sstevel@tonic-gate * addresses first on the list, "less than" is considered preferrable. 18870Sstevel@tonic-gate */ 18880Sstevel@tonic-gate #define RULE_PREFER_DA -1 18890Sstevel@tonic-gate #define RULE_PREFER_DB 1 18900Sstevel@tonic-gate #define RULE_EQUAL 0 18910Sstevel@tonic-gate 18920Sstevel@tonic-gate /* Prefer the addresses that is reachable. */ 18930Sstevel@tonic-gate static int 18940Sstevel@tonic-gate rule_reachable(const struct dstinforeq *da, const struct dstinforeq *db) 18950Sstevel@tonic-gate { 18960Sstevel@tonic-gate if (da->dir_dreachable == db->dir_dreachable) 18970Sstevel@tonic-gate return (RULE_EQUAL); 18980Sstevel@tonic-gate if (da->dir_dreachable) 18990Sstevel@tonic-gate return (RULE_PREFER_DA); 19000Sstevel@tonic-gate return (RULE_PREFER_DB); 19010Sstevel@tonic-gate } 19020Sstevel@tonic-gate 19030Sstevel@tonic-gate /* Prefer the address whose scope matches that of its source address. */ 19040Sstevel@tonic-gate static int 19050Sstevel@tonic-gate rule_matchscope(const struct dstinforeq *da, const struct dstinforeq *db) 19060Sstevel@tonic-gate { 19070Sstevel@tonic-gate boolean_t da_scope_match, db_scope_match; 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate da_scope_match = da->dir_dscope == da->dir_sscope; 19100Sstevel@tonic-gate db_scope_match = db->dir_dscope == db->dir_sscope; 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate if (da_scope_match == db_scope_match) 19130Sstevel@tonic-gate return (RULE_EQUAL); 19140Sstevel@tonic-gate if (da_scope_match) 19150Sstevel@tonic-gate return (RULE_PREFER_DA); 19160Sstevel@tonic-gate return (RULE_PREFER_DB); 19170Sstevel@tonic-gate } 19180Sstevel@tonic-gate 19190Sstevel@tonic-gate /* Avoid the address with the link local source address. */ 19200Sstevel@tonic-gate static int 19210Sstevel@tonic-gate rule_avoidlinklocal(const struct dstinforeq *da, const struct dstinforeq *db) 19220Sstevel@tonic-gate { 19230Sstevel@tonic-gate if (da->dir_sscope == IP6_SCOPE_LINKLOCAL && 19240Sstevel@tonic-gate da->dir_dscope != IP6_SCOPE_LINKLOCAL && 19250Sstevel@tonic-gate db->dir_sscope != IP6_SCOPE_LINKLOCAL) 19260Sstevel@tonic-gate return (RULE_PREFER_DB); 19270Sstevel@tonic-gate if (db->dir_sscope == IP6_SCOPE_LINKLOCAL && 19280Sstevel@tonic-gate db->dir_dscope != IP6_SCOPE_LINKLOCAL && 19290Sstevel@tonic-gate da->dir_sscope != IP6_SCOPE_LINKLOCAL) 19300Sstevel@tonic-gate return (RULE_PREFER_DA); 19310Sstevel@tonic-gate return (RULE_EQUAL); 19320Sstevel@tonic-gate } 19330Sstevel@tonic-gate 19340Sstevel@tonic-gate /* Prefer the address whose source address isn't deprecated. */ 19350Sstevel@tonic-gate static int 19360Sstevel@tonic-gate rule_deprecated(const struct dstinforeq *da, const struct dstinforeq *db) 19370Sstevel@tonic-gate { 19380Sstevel@tonic-gate if (da->dir_sdeprecated == db->dir_sdeprecated) 19390Sstevel@tonic-gate return (RULE_EQUAL); 19400Sstevel@tonic-gate if (db->dir_sdeprecated) 19410Sstevel@tonic-gate return (RULE_PREFER_DA); 19420Sstevel@tonic-gate return (RULE_PREFER_DB); 19430Sstevel@tonic-gate } 19440Sstevel@tonic-gate 19450Sstevel@tonic-gate /* Prefer the address whose label matches that of its source address. */ 19460Sstevel@tonic-gate static int 19470Sstevel@tonic-gate rule_label(const struct dstinforeq *da, const struct dstinforeq *db) 19480Sstevel@tonic-gate { 19490Sstevel@tonic-gate if (da->dir_labelmatch == db->dir_labelmatch) 19500Sstevel@tonic-gate return (RULE_EQUAL); 19510Sstevel@tonic-gate if (da->dir_labelmatch) 19520Sstevel@tonic-gate return (RULE_PREFER_DA); 19530Sstevel@tonic-gate return (RULE_PREFER_DB); 19540Sstevel@tonic-gate } 19550Sstevel@tonic-gate 19560Sstevel@tonic-gate /* Prefer the address with the higher precedence. */ 19570Sstevel@tonic-gate static int 19580Sstevel@tonic-gate rule_precedence(const struct dstinforeq *da, const struct dstinforeq *db) 19590Sstevel@tonic-gate { 19600Sstevel@tonic-gate if (da->dir_precedence == db->dir_precedence) 19610Sstevel@tonic-gate return (RULE_EQUAL); 19620Sstevel@tonic-gate if (da->dir_precedence > db->dir_precedence) 19630Sstevel@tonic-gate return (RULE_PREFER_DA); 19640Sstevel@tonic-gate return (RULE_PREFER_DB); 19650Sstevel@tonic-gate } 19660Sstevel@tonic-gate 19670Sstevel@tonic-gate /* Prefer the address whose output interface isn't an IP tunnel */ 19680Sstevel@tonic-gate static int 19690Sstevel@tonic-gate rule_native(const struct dstinforeq *da, const struct dstinforeq *db) 19700Sstevel@tonic-gate { 19710Sstevel@tonic-gate boolean_t isatun, isbtun; 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate /* Get the common case out of the way early */ 19740Sstevel@tonic-gate if (da->dir_dmactype == db->dir_dmactype) 19750Sstevel@tonic-gate return (RULE_EQUAL); 19760Sstevel@tonic-gate 19770Sstevel@tonic-gate isatun = da->dir_dmactype == DL_IPV4 || da->dir_dmactype == DL_IPV6; 19780Sstevel@tonic-gate isbtun = db->dir_dmactype == DL_IPV4 || db->dir_dmactype == DL_IPV6; 19790Sstevel@tonic-gate 19800Sstevel@tonic-gate if (isatun == isbtun) 19810Sstevel@tonic-gate return (RULE_EQUAL); 19820Sstevel@tonic-gate if (isbtun) 19830Sstevel@tonic-gate return (RULE_PREFER_DA); 19840Sstevel@tonic-gate return (RULE_PREFER_DB); 19850Sstevel@tonic-gate } 19860Sstevel@tonic-gate 19870Sstevel@tonic-gate /* Prefer the address with the smaller scope. */ 19880Sstevel@tonic-gate static int 19890Sstevel@tonic-gate rule_scope(const struct dstinforeq *da, const struct dstinforeq *db) 19900Sstevel@tonic-gate { 19910Sstevel@tonic-gate if (da->dir_dscope == db->dir_dscope) 19920Sstevel@tonic-gate return (RULE_EQUAL); 19930Sstevel@tonic-gate if (da->dir_dscope < db->dir_dscope) 19940Sstevel@tonic-gate return (RULE_PREFER_DA); 19950Sstevel@tonic-gate return (RULE_PREFER_DB); 19960Sstevel@tonic-gate } 19970Sstevel@tonic-gate 19980Sstevel@tonic-gate /* 19990Sstevel@tonic-gate * Prefer the address that has the most leading bits in common with its 20000Sstevel@tonic-gate * source address. 20010Sstevel@tonic-gate */ 20020Sstevel@tonic-gate static int 20030Sstevel@tonic-gate rule_prefix(const struct dstinforeq *da, const struct dstinforeq *db) 20040Sstevel@tonic-gate { 20050Sstevel@tonic-gate uint_t da_commonbits, db_commonbits; 20060Sstevel@tonic-gate boolean_t da_isipv4, db_isipv4; 20070Sstevel@tonic-gate 20080Sstevel@tonic-gate da_isipv4 = IN6_IS_ADDR_V4MAPPED(&da->dir_daddr); 20090Sstevel@tonic-gate db_isipv4 = IN6_IS_ADDR_V4MAPPED(&db->dir_daddr); 20100Sstevel@tonic-gate 20110Sstevel@tonic-gate /* 20120Sstevel@tonic-gate * At this point, the order doesn't matter if the two addresses 20130Sstevel@tonic-gate * aren't of the same address family. 20140Sstevel@tonic-gate */ 20150Sstevel@tonic-gate if (da_isipv4 != db_isipv4) 20160Sstevel@tonic-gate return (RULE_EQUAL); 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate da_commonbits = ip_addr_commonbits_v6(&da->dir_daddr, &da->dir_saddr); 20190Sstevel@tonic-gate db_commonbits = ip_addr_commonbits_v6(&db->dir_daddr, &db->dir_saddr); 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate if (da_commonbits > db_commonbits) 20220Sstevel@tonic-gate return (RULE_PREFER_DA); 20230Sstevel@tonic-gate if (da_commonbits < db_commonbits) 20240Sstevel@tonic-gate return (RULE_PREFER_DB); 20250Sstevel@tonic-gate return (RULE_EQUAL); 20260Sstevel@tonic-gate } 20270Sstevel@tonic-gate 20280Sstevel@tonic-gate /* 20290Sstevel@tonic-gate * This is the function passed to qsort() that does the AF_INET6 20300Sstevel@tonic-gate * address comparisons. It compares two addresses using a list of 20310Sstevel@tonic-gate * rules. The rules are applied in order until one prefers one 20320Sstevel@tonic-gate * address over the other. 20330Sstevel@tonic-gate */ 20340Sstevel@tonic-gate static int 20350Sstevel@tonic-gate dstcmp(const void *da, const void *db) 20360Sstevel@tonic-gate { 20370Sstevel@tonic-gate int index, result; 20380Sstevel@tonic-gate rulef_t rules[] = { 20390Sstevel@tonic-gate rule_reachable, 20400Sstevel@tonic-gate rule_matchscope, 20410Sstevel@tonic-gate rule_avoidlinklocal, 20420Sstevel@tonic-gate rule_deprecated, 20430Sstevel@tonic-gate rule_label, 20440Sstevel@tonic-gate rule_precedence, 20450Sstevel@tonic-gate rule_native, 20460Sstevel@tonic-gate rule_scope, 20470Sstevel@tonic-gate rule_prefix, 20480Sstevel@tonic-gate NULL 20490Sstevel@tonic-gate }; 20500Sstevel@tonic-gate 20510Sstevel@tonic-gate result = 0; 20520Sstevel@tonic-gate for (index = 0; rules[index] != NULL; index++) { 20530Sstevel@tonic-gate result = (rules[index])(da, db); 20540Sstevel@tonic-gate if (result != RULE_EQUAL) 20550Sstevel@tonic-gate break; 20560Sstevel@tonic-gate } 20570Sstevel@tonic-gate 20580Sstevel@tonic-gate return (result); 20590Sstevel@tonic-gate } 20600Sstevel@tonic-gate 20610Sstevel@tonic-gate /* 20620Sstevel@tonic-gate * Given haddrlist and a port number, mallocs and populates a new 20630Sstevel@tonic-gate * nd_addrlist. The new nd_addrlist maintains the order of the addresses 20640Sstevel@tonic-gate * in haddrlist, which have already been sorted by order_haddrlist_inet() 20650Sstevel@tonic-gate * or order_haddrlist_inet6(). For IPv6 this function filters out 20660Sstevel@tonic-gate * IPv4-mapped IPv6 addresses. 20670Sstevel@tonic-gate */ 20680Sstevel@tonic-gate int 20690Sstevel@tonic-gate hent2ndaddr(int af, char **haddrlist, int *servp, struct nd_addrlist **nd_alist) 20700Sstevel@tonic-gate { 20710Sstevel@tonic-gate struct nd_addrlist *result; 20720Sstevel@tonic-gate int num; 20730Sstevel@tonic-gate struct netbuf *na; 20740Sstevel@tonic-gate struct sockaddr_in *sinbuf, *sin; 20750Sstevel@tonic-gate struct sockaddr_in6 *sin6buf, *sin6; 20760Sstevel@tonic-gate struct in_addr **inaddr, **inaddrlist; 20770Sstevel@tonic-gate struct in6_addr **in6addr, **in6addrlist; 20780Sstevel@tonic-gate 20790Sstevel@tonic-gate /* Address count */ 20800Sstevel@tonic-gate num = 0; 20810Sstevel@tonic-gate if (af == AF_INET6) { 20820Sstevel@tonic-gate in6addrlist = (struct in6_addr **)haddrlist; 20830Sstevel@tonic-gate 20840Sstevel@tonic-gate /* 20850Sstevel@tonic-gate * Exclude IPv4-mapped IPv6 addresses from the count, as 20860Sstevel@tonic-gate * these are not included in the nd_addrlist we return. 20870Sstevel@tonic-gate */ 20880Sstevel@tonic-gate for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) 20890Sstevel@tonic-gate if (!IN6_IS_ADDR_V4MAPPED(*in6addr)) 20900Sstevel@tonic-gate num++; 20910Sstevel@tonic-gate } else { 20920Sstevel@tonic-gate inaddrlist = (struct in_addr **)haddrlist; 20930Sstevel@tonic-gate 20940Sstevel@tonic-gate for (inaddr = inaddrlist; *inaddr != NULL; inaddr++) 20950Sstevel@tonic-gate num++; 20960Sstevel@tonic-gate } 20970Sstevel@tonic-gate if (num == 0) 20980Sstevel@tonic-gate return (ND_NOHOST); 20990Sstevel@tonic-gate 21000Sstevel@tonic-gate result = malloc(sizeof (struct nd_addrlist)); 21010Sstevel@tonic-gate if (result == 0) 21020Sstevel@tonic-gate return (ND_NOMEM); 21030Sstevel@tonic-gate 21040Sstevel@tonic-gate result->n_cnt = num; 21050Sstevel@tonic-gate result->n_addrs = calloc(num, sizeof (struct netbuf)); 21060Sstevel@tonic-gate if (result->n_addrs == 0) { 21070Sstevel@tonic-gate free(result); 21080Sstevel@tonic-gate return (ND_NOMEM); 21090Sstevel@tonic-gate } 21100Sstevel@tonic-gate 21110Sstevel@tonic-gate na = result->n_addrs; 21120Sstevel@tonic-gate if (af == AF_INET) { 21130Sstevel@tonic-gate sinbuf = calloc(num, sizeof (struct sockaddr_in)); 21140Sstevel@tonic-gate if (sinbuf == NULL) { 21150Sstevel@tonic-gate free(result->n_addrs); 21160Sstevel@tonic-gate free(result); 21170Sstevel@tonic-gate return (ND_NOMEM); 21180Sstevel@tonic-gate } 21190Sstevel@tonic-gate 21200Sstevel@tonic-gate sin = sinbuf; 21210Sstevel@tonic-gate for (inaddr = inaddrlist; *inaddr != NULL; inaddr++) { 21220Sstevel@tonic-gate na->len = na->maxlen = sizeof (struct sockaddr_in); 21230Sstevel@tonic-gate na->buf = (char *)sin; 21240Sstevel@tonic-gate sin->sin_family = AF_INET; 21250Sstevel@tonic-gate sin->sin_addr = **inaddr; 21260Sstevel@tonic-gate sin->sin_port = *servp; 21270Sstevel@tonic-gate na++; 21280Sstevel@tonic-gate sin++; 21290Sstevel@tonic-gate } 21300Sstevel@tonic-gate } else if (af == AF_INET6) { 21310Sstevel@tonic-gate sin6buf = calloc(num, sizeof (struct sockaddr_in6)); 21320Sstevel@tonic-gate if (sin6buf == NULL) { 21330Sstevel@tonic-gate free(result->n_addrs); 21340Sstevel@tonic-gate free(result); 21350Sstevel@tonic-gate return (ND_NOMEM); 21360Sstevel@tonic-gate } 21370Sstevel@tonic-gate 21380Sstevel@tonic-gate sin6 = sin6buf; 21390Sstevel@tonic-gate for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) { 21400Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(*in6addr)) 21410Sstevel@tonic-gate continue; 21420Sstevel@tonic-gate 21430Sstevel@tonic-gate na->len = na->maxlen = sizeof (struct sockaddr_in6); 21440Sstevel@tonic-gate na->buf = (char *)sin6; 21450Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 21460Sstevel@tonic-gate sin6->sin6_addr = **in6addr; 21470Sstevel@tonic-gate sin6->sin6_port = *servp; 21480Sstevel@tonic-gate na++; 21490Sstevel@tonic-gate sin6++; 21500Sstevel@tonic-gate } 21510Sstevel@tonic-gate } 21520Sstevel@tonic-gate *(nd_alist) = result; 21530Sstevel@tonic-gate return (ND_OK); 21540Sstevel@tonic-gate } 21550Sstevel@tonic-gate 21560Sstevel@tonic-gate /* 21570Sstevel@tonic-gate * Given a hostent and a servent, mallocs and populates 21580Sstevel@tonic-gate * a new nd_hostservlist with host and service names. 21590Sstevel@tonic-gate * 21600Sstevel@tonic-gate * We could be passed in a NULL servent, in which case stringify port. 21610Sstevel@tonic-gate */ 21620Sstevel@tonic-gate int 21630Sstevel@tonic-gate hsents2ndhostservs(struct hostent *he, struct servent *se, 21640Sstevel@tonic-gate ushort_t port, struct nd_hostservlist **hslist) 21650Sstevel@tonic-gate { 21660Sstevel@tonic-gate struct nd_hostservlist *result; 21670Sstevel@tonic-gate struct nd_hostserv *hs; 21680Sstevel@tonic-gate int hosts, servs, i, j; 21690Sstevel@tonic-gate char **hn, **sn; 21700Sstevel@tonic-gate 2171132Srobinson if ((result = malloc(sizeof (struct nd_hostservlist))) == 0) 21720Sstevel@tonic-gate return (ND_NOMEM); 21730Sstevel@tonic-gate 21740Sstevel@tonic-gate /* 21750Sstevel@tonic-gate * We initialize the counters to 1 rather than zero because 21760Sstevel@tonic-gate * we have to count the "official" name as well as the aliases. 21770Sstevel@tonic-gate */ 21780Sstevel@tonic-gate for (hn = he->h_aliases, hosts = 1; hn && *hn; hn++, hosts++); 21790Sstevel@tonic-gate if (se) 21800Sstevel@tonic-gate for (sn = se->s_aliases, servs = 1; sn && *sn; sn++, servs++); 21810Sstevel@tonic-gate else 21820Sstevel@tonic-gate servs = 1; 21830Sstevel@tonic-gate 2184132Srobinson if ((hs = calloc(hosts * servs, sizeof (struct nd_hostserv))) == 0) { 2185132Srobinson free(result); 21860Sstevel@tonic-gate return (ND_NOMEM); 21870Sstevel@tonic-gate } 21880Sstevel@tonic-gate 21890Sstevel@tonic-gate result->h_cnt = servs * hosts; 21900Sstevel@tonic-gate result->h_hostservs = hs; 21910Sstevel@tonic-gate 21920Sstevel@tonic-gate for (i = 0, hn = he->h_aliases; i < hosts; i++) { 21930Sstevel@tonic-gate sn = se ? se->s_aliases : NULL; 21940Sstevel@tonic-gate 21950Sstevel@tonic-gate for (j = 0; j < servs; j++) { 21960Sstevel@tonic-gate if (i == 0) 21970Sstevel@tonic-gate hs->h_host = strdup(he->h_name); 21980Sstevel@tonic-gate else 21990Sstevel@tonic-gate hs->h_host = strdup(*hn); 22000Sstevel@tonic-gate if (j == 0) { 22010Sstevel@tonic-gate if (se) 22020Sstevel@tonic-gate hs->h_serv = strdup(se->s_name); 22030Sstevel@tonic-gate else { 22040Sstevel@tonic-gate /* Convert to a number string */ 22050Sstevel@tonic-gate char stmp[16]; 22060Sstevel@tonic-gate 22070Sstevel@tonic-gate (void) sprintf(stmp, "%d", port); 22080Sstevel@tonic-gate hs->h_serv = strdup(stmp); 22090Sstevel@tonic-gate } 22100Sstevel@tonic-gate } else 22110Sstevel@tonic-gate hs->h_serv = strdup(*sn++); 22120Sstevel@tonic-gate 22130Sstevel@tonic-gate if ((hs->h_host == 0) || (hs->h_serv == 0)) { 2214132Srobinson free(result->h_hostservs); 2215132Srobinson free(result); 22160Sstevel@tonic-gate return (ND_NOMEM); 22170Sstevel@tonic-gate } 22180Sstevel@tonic-gate hs++; 22190Sstevel@tonic-gate } 22200Sstevel@tonic-gate if (i) 22210Sstevel@tonic-gate hn++; 22220Sstevel@tonic-gate } 22230Sstevel@tonic-gate *(hslist) = result; 22240Sstevel@tonic-gate return (ND_OK); 22250Sstevel@tonic-gate } 22260Sstevel@tonic-gate 22270Sstevel@tonic-gate /* 22280Sstevel@tonic-gate * Process results from nd_addrlist ( returned by netdir_getbyname) 22290Sstevel@tonic-gate * into a hostent using buf. 22300Sstevel@tonic-gate * *** ASSUMES that nd_addrlist->n_addrs->buf contains IP addresses in 22310Sstevel@tonic-gate * sockaddr_in's *** 22320Sstevel@tonic-gate */ 22330Sstevel@tonic-gate int 22340Sstevel@tonic-gate ndaddr2hent(int af, const char *nam, struct nd_addrlist *addrs, 22350Sstevel@tonic-gate struct hostent *result, char *buffer, int buflen) 22360Sstevel@tonic-gate { 22370Sstevel@tonic-gate int i, count; 22380Sstevel@tonic-gate struct in_addr *addrp; 22390Sstevel@tonic-gate struct in6_addr *addr6p; 22400Sstevel@tonic-gate char **addrvec; 22410Sstevel@tonic-gate struct netbuf *na; 22420Sstevel@tonic-gate size_t len; 22430Sstevel@tonic-gate 22440Sstevel@tonic-gate result->h_name = buffer; 22450Sstevel@tonic-gate result->h_addrtype = af; 22460Sstevel@tonic-gate result->h_length = (af == AF_INET) ? sizeof (*addrp): 22470Sstevel@tonic-gate sizeof (*addr6p); 22480Sstevel@tonic-gate 22490Sstevel@tonic-gate /* 22500Sstevel@tonic-gate * Build addrlist at start of buffer (after name); store the 22510Sstevel@tonic-gate * addresses themselves at the end of the buffer. 22520Sstevel@tonic-gate */ 22530Sstevel@tonic-gate len = strlen(nam) + 1; 22540Sstevel@tonic-gate addrvec = (char **)ROUND_UP(buffer + len, sizeof (*addrvec)); 22550Sstevel@tonic-gate result->h_addr_list = addrvec; 22560Sstevel@tonic-gate 22570Sstevel@tonic-gate if (af == AF_INET) { 22580Sstevel@tonic-gate addrp = (struct in_addr *)ROUND_DOWN(buffer + buflen, 22590Sstevel@tonic-gate sizeof (*addrp)); 22600Sstevel@tonic-gate 22610Sstevel@tonic-gate count = addrs->n_cnt; 22620Sstevel@tonic-gate if ((char *)(&addrvec[count + 1]) > (char *)(&addrp[-count])) 22630Sstevel@tonic-gate return (ND_NOMEM); 22640Sstevel@tonic-gate 22650Sstevel@tonic-gate (void) memcpy(buffer, nam, len); 22660Sstevel@tonic-gate 22670Sstevel@tonic-gate for (na = addrs->n_addrs, i = 0; i < count; na++, i++) { 22680Sstevel@tonic-gate --addrp; 22690Sstevel@tonic-gate (void) memcpy(addrp, 2270132Srobinson /* LINTED pointer cast */ 22710Sstevel@tonic-gate &((struct sockaddr_in *)na->buf)->sin_addr, 22720Sstevel@tonic-gate sizeof (*addrp)); 22730Sstevel@tonic-gate *addrvec++ = (char *)addrp; 22740Sstevel@tonic-gate } 22750Sstevel@tonic-gate } else { 22760Sstevel@tonic-gate addr6p = (struct in6_addr *)ROUND_DOWN(buffer + buflen, 22770Sstevel@tonic-gate sizeof (*addr6p)); 22780Sstevel@tonic-gate 22790Sstevel@tonic-gate count = addrs->n_cnt; 22800Sstevel@tonic-gate if ((char *)(&addrvec[count + 1]) > (char *)(&addr6p[-count])) 22810Sstevel@tonic-gate return (ND_NOMEM); 22820Sstevel@tonic-gate 22830Sstevel@tonic-gate (void) memcpy(buffer, nam, len); 22840Sstevel@tonic-gate 22850Sstevel@tonic-gate for (na = addrs->n_addrs, i = 0; i < count; na++, i++) { 22860Sstevel@tonic-gate --addr6p; 22870Sstevel@tonic-gate (void) memcpy(addr6p, 2288132Srobinson /* LINTED pointer cast */ 22890Sstevel@tonic-gate &((struct sockaddr_in6 *)na->buf)->sin6_addr, 22900Sstevel@tonic-gate sizeof (*addr6p)); 22910Sstevel@tonic-gate *addrvec++ = (char *)addr6p; 22920Sstevel@tonic-gate } 22930Sstevel@tonic-gate } 22940Sstevel@tonic-gate *addrvec = 0; 22950Sstevel@tonic-gate result->h_aliases = addrvec; 22960Sstevel@tonic-gate 22970Sstevel@tonic-gate return (ND_OK); 22980Sstevel@tonic-gate } 22990Sstevel@tonic-gate 23000Sstevel@tonic-gate /* 23010Sstevel@tonic-gate * Process results from nd_addrlist ( returned by netdir_getbyname) 23020Sstevel@tonic-gate * into a servent using buf. 23030Sstevel@tonic-gate */ 23040Sstevel@tonic-gate int 23050Sstevel@tonic-gate ndaddr2srent(const char *name, const char *proto, ushort_t port, 23060Sstevel@tonic-gate struct servent *result, char *buffer, int buflen) 23070Sstevel@tonic-gate { 23080Sstevel@tonic-gate size_t i; 23090Sstevel@tonic-gate char *bufend = (buffer + buflen); 23100Sstevel@tonic-gate 23110Sstevel@tonic-gate result->s_port = (int)port; 23120Sstevel@tonic-gate 23130Sstevel@tonic-gate result->s_aliases = 23140Sstevel@tonic-gate (char **)ROUND_UP(buffer, sizeof (char *)); 23150Sstevel@tonic-gate result->s_aliases[0] = NULL; 23160Sstevel@tonic-gate buffer = (char *)&result->s_aliases[1]; 23170Sstevel@tonic-gate result->s_name = buffer; 23180Sstevel@tonic-gate i = strlen(name) + 1; 23190Sstevel@tonic-gate if ((buffer + i) > bufend) 23200Sstevel@tonic-gate return (ND_NOMEM); 23210Sstevel@tonic-gate (void) memcpy(buffer, name, i); 23220Sstevel@tonic-gate buffer += i; 23230Sstevel@tonic-gate 23240Sstevel@tonic-gate result->s_proto = buffer; 23250Sstevel@tonic-gate i = strlen(proto) + 1; 23260Sstevel@tonic-gate if ((buffer + i) > bufend) 23270Sstevel@tonic-gate return (ND_NOMEM); 23280Sstevel@tonic-gate (void) memcpy(buffer, proto, i); 23290Sstevel@tonic-gate buffer += i; 23300Sstevel@tonic-gate 23310Sstevel@tonic-gate return (ND_OK); 23320Sstevel@tonic-gate } 23330Sstevel@tonic-gate 23340Sstevel@tonic-gate /* 23350Sstevel@tonic-gate * Process results from nd_hostservlist ( returned by netdir_getbyaddr) 23360Sstevel@tonic-gate * into a hostent using buf. 23370Sstevel@tonic-gate * *** ASSUMES that nd_buf->buf is a sockaddr_in *** 23380Sstevel@tonic-gate */ 23390Sstevel@tonic-gate int 23400Sstevel@tonic-gate ndhostserv2hent(struct netbuf *nbuf, struct nd_hostservlist *addrs, 23410Sstevel@tonic-gate struct hostent *result, char *buffer, int buflen) 23420Sstevel@tonic-gate { 23430Sstevel@tonic-gate int i, count; 23440Sstevel@tonic-gate char *aliasp; 23450Sstevel@tonic-gate char **aliasvec; 23460Sstevel@tonic-gate struct sockaddr_in *sa; 23470Sstevel@tonic-gate struct nd_hostserv *hs; 23480Sstevel@tonic-gate const char *la; 23490Sstevel@tonic-gate size_t length; 23500Sstevel@tonic-gate 23510Sstevel@tonic-gate /* First, give the lonely address a specious home in h_addr_list. */ 23520Sstevel@tonic-gate aliasp = (char *)ROUND_UP(buffer, sizeof (sa->sin_addr)); 2353132Srobinson /* LINTED pointer cast */ 23540Sstevel@tonic-gate sa = (struct sockaddr_in *)nbuf->buf; 2355132Srobinson (void) memcpy(aliasp, &(sa->sin_addr), sizeof (sa->sin_addr)); 23560Sstevel@tonic-gate aliasvec = (char **)ROUND_UP(aliasp + sizeof (sa->sin_addr), 23570Sstevel@tonic-gate sizeof (*aliasvec)); 23580Sstevel@tonic-gate result->h_addr_list = aliasvec; 23590Sstevel@tonic-gate *aliasvec++ = aliasp; 23600Sstevel@tonic-gate *aliasvec++ = 0; 23610Sstevel@tonic-gate 23620Sstevel@tonic-gate /* 23630Sstevel@tonic-gate * Build h_aliases at start of buffer (after addr and h_addr_list); 23640Sstevel@tonic-gate * store the alias strings at the end of the buffer (before h_name). 23650Sstevel@tonic-gate */ 23660Sstevel@tonic-gate 23670Sstevel@tonic-gate aliasp = buffer + buflen; 23680Sstevel@tonic-gate 23690Sstevel@tonic-gate result->h_aliases = aliasvec; 23700Sstevel@tonic-gate 23710Sstevel@tonic-gate hs = addrs->h_hostservs; 2372132Srobinson if (!hs) 23730Sstevel@tonic-gate return (ND_NOHOST); 23740Sstevel@tonic-gate 23750Sstevel@tonic-gate length = strlen(hs->h_host) + 1; 23760Sstevel@tonic-gate aliasp -= length; 23770Sstevel@tonic-gate if ((char *)(&aliasvec[1]) > aliasp) 23780Sstevel@tonic-gate return (ND_NOMEM); 23790Sstevel@tonic-gate (void) memcpy(aliasp, hs->h_host, length); 23800Sstevel@tonic-gate 23810Sstevel@tonic-gate result->h_name = aliasp; 23820Sstevel@tonic-gate result->h_addrtype = AF_INET; 23830Sstevel@tonic-gate result->h_length = sizeof (sa->sin_addr); 23840Sstevel@tonic-gate 23850Sstevel@tonic-gate /* 23860Sstevel@tonic-gate * Assumption: the netdir nametoaddr_libs 23870Sstevel@tonic-gate * sort the vector of (host, serv) pairs in such a way that 23880Sstevel@tonic-gate * all pairs with the same host name are contiguous. 23890Sstevel@tonic-gate */ 23900Sstevel@tonic-gate la = hs->h_host; 23910Sstevel@tonic-gate count = addrs->h_cnt; 23920Sstevel@tonic-gate for (i = 0; i < count; i++, hs++) 23930Sstevel@tonic-gate if (strcmp(la, hs->h_host) != 0) { 23940Sstevel@tonic-gate size_t len = strlen(hs->h_host) + 1; 23950Sstevel@tonic-gate 23960Sstevel@tonic-gate aliasp -= len; 23970Sstevel@tonic-gate if ((char *)(&aliasvec[2]) > aliasp) 23980Sstevel@tonic-gate return (ND_NOMEM); 23990Sstevel@tonic-gate (void) memcpy(aliasp, hs->h_host, len); 24000Sstevel@tonic-gate *aliasvec++ = aliasp; 24010Sstevel@tonic-gate la = hs->h_host; 24020Sstevel@tonic-gate } 24030Sstevel@tonic-gate *aliasvec = 0; 24040Sstevel@tonic-gate 24050Sstevel@tonic-gate return (ND_OK); 24060Sstevel@tonic-gate } 24070Sstevel@tonic-gate 24080Sstevel@tonic-gate /* 24090Sstevel@tonic-gate * Process results from nd_hostservlist ( returned by netdir_getbyaddr) 24100Sstevel@tonic-gate * into a servent using buf. 24110Sstevel@tonic-gate */ 24120Sstevel@tonic-gate int 24130Sstevel@tonic-gate ndhostserv2srent(int port, const char *proto, struct nd_hostservlist *addrs, 24140Sstevel@tonic-gate struct servent *result, char *buffer, int buflen) 24150Sstevel@tonic-gate { 24160Sstevel@tonic-gate int i, count; 24170Sstevel@tonic-gate char *aliasp; 24180Sstevel@tonic-gate char **aliasvec; 24190Sstevel@tonic-gate struct nd_hostserv *hs; 24200Sstevel@tonic-gate const char *host_cname; 24210Sstevel@tonic-gate size_t leni, lenj; 24220Sstevel@tonic-gate 24230Sstevel@tonic-gate result->s_port = port; 24240Sstevel@tonic-gate /* 24250Sstevel@tonic-gate * Build s_aliases at start of buffer; 24260Sstevel@tonic-gate * store proto and aliases at the end of the buffer (before h_name). 24270Sstevel@tonic-gate */ 24280Sstevel@tonic-gate 24290Sstevel@tonic-gate aliasp = buffer + buflen; 24300Sstevel@tonic-gate aliasvec = (char **)ROUND_UP(buffer, sizeof (char *)); 24310Sstevel@tonic-gate 24320Sstevel@tonic-gate result->s_aliases = aliasvec; 24330Sstevel@tonic-gate 24340Sstevel@tonic-gate hs = addrs->h_hostservs; 2435132Srobinson if (!hs) 24360Sstevel@tonic-gate return (ND_NOHOST); 24370Sstevel@tonic-gate host_cname = hs->h_host; 24380Sstevel@tonic-gate 24390Sstevel@tonic-gate leni = strlen(proto) + 1; 24400Sstevel@tonic-gate lenj = strlen(hs->h_serv) + 1; 24410Sstevel@tonic-gate if ((char *)(&aliasvec[2]) > (aliasp - leni - lenj)) 24420Sstevel@tonic-gate return (ND_NOMEM); 24430Sstevel@tonic-gate 24440Sstevel@tonic-gate aliasp -= leni; 24450Sstevel@tonic-gate (void) memcpy(aliasp, proto, leni); 24460Sstevel@tonic-gate result->s_proto = aliasp; 24470Sstevel@tonic-gate 24480Sstevel@tonic-gate aliasp -= lenj; 24490Sstevel@tonic-gate (void) memcpy(aliasp, hs->h_serv, lenj); 24500Sstevel@tonic-gate result->s_name = aliasp; 24510Sstevel@tonic-gate 24520Sstevel@tonic-gate /* 24530Sstevel@tonic-gate * Assumption: the netdir nametoaddr_libs 24540Sstevel@tonic-gate * do a host aliases first and serv aliases next 24550Sstevel@tonic-gate * enumeration for creating the list of hostserv 24560Sstevel@tonic-gate * structures. 24570Sstevel@tonic-gate */ 24580Sstevel@tonic-gate count = addrs->h_cnt; 24590Sstevel@tonic-gate for (i = 0; 24600Sstevel@tonic-gate i < count && hs->h_serv && strcmp(hs->h_host, host_cname) == 0; 24610Sstevel@tonic-gate i++, hs++) { 24620Sstevel@tonic-gate size_t len = strlen(hs->h_serv) + 1; 24630Sstevel@tonic-gate 24640Sstevel@tonic-gate aliasp -= len; 24650Sstevel@tonic-gate if ((char *)(&aliasvec[2]) > aliasp) 24660Sstevel@tonic-gate return (ND_NOMEM); 24670Sstevel@tonic-gate (void) memcpy(aliasp, hs->h_serv, len); 24680Sstevel@tonic-gate *aliasvec++ = aliasp; 24690Sstevel@tonic-gate } 24700Sstevel@tonic-gate *aliasvec = NULL; 24710Sstevel@tonic-gate 24720Sstevel@tonic-gate return (ND_OK); 24730Sstevel@tonic-gate } 24740Sstevel@tonic-gate 24750Sstevel@tonic-gate 24760Sstevel@tonic-gate static int 24770Sstevel@tonic-gate nd2herrno(int nerr) 24780Sstevel@tonic-gate { 24790Sstevel@tonic-gate switch (nerr) { 24800Sstevel@tonic-gate case ND_OK: 24810Sstevel@tonic-gate return (0); 24820Sstevel@tonic-gate case ND_TRY_AGAIN: 24830Sstevel@tonic-gate return (TRY_AGAIN); 24840Sstevel@tonic-gate case ND_NO_RECOVERY: 24850Sstevel@tonic-gate case ND_BADARG: 24860Sstevel@tonic-gate case ND_NOMEM: 24870Sstevel@tonic-gate return (NO_RECOVERY); 24880Sstevel@tonic-gate case ND_NO_DATA: 24890Sstevel@tonic-gate return (NO_DATA); 24900Sstevel@tonic-gate case ND_NOHOST: 24910Sstevel@tonic-gate case ND_NOSERV: 24920Sstevel@tonic-gate return (HOST_NOT_FOUND); 24930Sstevel@tonic-gate default: 24940Sstevel@tonic-gate return (NO_RECOVERY); 24950Sstevel@tonic-gate } 24960Sstevel@tonic-gate } 24970Sstevel@tonic-gate 24980Sstevel@tonic-gate /* 24990Sstevel@tonic-gate * This is a utility function so that various parts of libnsl can 25000Sstevel@tonic-gate * easily send ioctls down to ip. 25010Sstevel@tonic-gate * 25020Sstevel@tonic-gate */ 25030Sstevel@tonic-gate int 25040Sstevel@tonic-gate nss_ioctl(int af, int cmd, void *arg) 25050Sstevel@tonic-gate { 25060Sstevel@tonic-gate int fd; 25070Sstevel@tonic-gate char *devpath; 25080Sstevel@tonic-gate int retv; 25090Sstevel@tonic-gate 25100Sstevel@tonic-gate switch (af) { 25110Sstevel@tonic-gate case AF_INET6: 25120Sstevel@tonic-gate devpath = UDP6DEV; 25130Sstevel@tonic-gate break; 25140Sstevel@tonic-gate case AF_INET: 25150Sstevel@tonic-gate case AF_UNSPEC: 25160Sstevel@tonic-gate default: 25170Sstevel@tonic-gate devpath = UDPDEV; 25180Sstevel@tonic-gate } 25190Sstevel@tonic-gate if ((fd = open(devpath, O_RDONLY)) < 0) { 25200Sstevel@tonic-gate return (-1); 25210Sstevel@tonic-gate } 25220Sstevel@tonic-gate while ((retv = ioctl(fd, cmd, arg)) == -1) { 25230Sstevel@tonic-gate if (errno != EINTR) 25240Sstevel@tonic-gate break; 25250Sstevel@tonic-gate } 2526132Srobinson (void) close(fd); 25270Sstevel@tonic-gate return (retv); 25280Sstevel@tonic-gate } 25290Sstevel@tonic-gate 25300Sstevel@tonic-gate static int 25310Sstevel@tonic-gate nss_strioctl(int af, int cmd, void *ptr, int ilen) 25320Sstevel@tonic-gate { 25330Sstevel@tonic-gate struct strioctl str; 25340Sstevel@tonic-gate 25350Sstevel@tonic-gate str.ic_cmd = cmd; 25360Sstevel@tonic-gate str.ic_timout = 0; 25370Sstevel@tonic-gate str.ic_len = ilen; 25380Sstevel@tonic-gate str.ic_dp = ptr; 25390Sstevel@tonic-gate 25400Sstevel@tonic-gate return (nss_ioctl(af, I_STR, &str)); 25410Sstevel@tonic-gate } 25420Sstevel@tonic-gate 25430Sstevel@tonic-gate static struct ifinfo * 25440Sstevel@tonic-gate get_local_info(void) 25450Sstevel@tonic-gate { 25460Sstevel@tonic-gate int numifs; 25470Sstevel@tonic-gate int n; 25480Sstevel@tonic-gate char *buf = NULL; 25490Sstevel@tonic-gate size_t needed; 25500Sstevel@tonic-gate struct lifconf lifc; 25510Sstevel@tonic-gate struct lifreq lifreq, *lifr; 25520Sstevel@tonic-gate struct lifnum lifn; 25530Sstevel@tonic-gate struct ifinfo *localinfo; 25540Sstevel@tonic-gate 25550Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC; 25560Sstevel@tonic-gate lifn.lifn_flags = 0; 25570Sstevel@tonic-gate 25580Sstevel@tonic-gate getifnum: 25590Sstevel@tonic-gate if (nss_ioctl(AF_UNSPEC, SIOCGLIFNUM, &lifn) == -1) { 25600Sstevel@tonic-gate numifs = MAXIFS; 25610Sstevel@tonic-gate } else { 25620Sstevel@tonic-gate numifs = lifn.lifn_count; 25630Sstevel@tonic-gate } 25640Sstevel@tonic-gate 25650Sstevel@tonic-gate /* 25660Sstevel@tonic-gate * Add a small fudge factor in case interfaces get plumbed between 25670Sstevel@tonic-gate * the call to SIOCGLIFNUM and SIOCGLIFCONF. 25680Sstevel@tonic-gate */ 25690Sstevel@tonic-gate needed = (numifs + 4) * sizeof (lifreq); 25700Sstevel@tonic-gate if (buf == NULL) 25710Sstevel@tonic-gate buf = malloc(needed); 25720Sstevel@tonic-gate else 25730Sstevel@tonic-gate buf = realloc(buf, needed); 25740Sstevel@tonic-gate if (buf == NULL) { 25750Sstevel@tonic-gate (void) syslog(LOG_ERR, "n2a get_local_info: malloc failed: %m"); 25760Sstevel@tonic-gate _nderror = ND_NOMEM; 25770Sstevel@tonic-gate return (NULL); 25780Sstevel@tonic-gate } 25790Sstevel@tonic-gate lifc.lifc_family = AF_UNSPEC; 25800Sstevel@tonic-gate lifc.lifc_flags = 0; 25810Sstevel@tonic-gate lifc.lifc_len = needed; 25820Sstevel@tonic-gate lifc.lifc_buf = buf; 25830Sstevel@tonic-gate if (nss_ioctl(AF_UNSPEC, SIOCGLIFCONF, &lifc) == -1) { 25840Sstevel@tonic-gate /* 25850Sstevel@tonic-gate * IP returns EINVAL if the buffer was too small to fit 25860Sstevel@tonic-gate * all of the entries. If that's the case, go back and 25870Sstevel@tonic-gate * try again. 25880Sstevel@tonic-gate */ 25890Sstevel@tonic-gate if (errno == EINVAL) 25900Sstevel@tonic-gate goto getifnum; 25910Sstevel@tonic-gate 25920Sstevel@tonic-gate (void) syslog(LOG_ERR, "n2a get_local_info: " 25930Sstevel@tonic-gate "ioctl (get interface configuration): %m"); 25940Sstevel@tonic-gate free(buf); 25950Sstevel@tonic-gate _nderror = ND_SYSTEM; 25960Sstevel@tonic-gate return (NULL); 25970Sstevel@tonic-gate } 2598132Srobinson /* LINTED pointer cast */ 25990Sstevel@tonic-gate lifr = (struct lifreq *)buf; 26000Sstevel@tonic-gate numifs = lifc.lifc_len/sizeof (lifreq); 2601132Srobinson localinfo = malloc(ifinfosize(numifs)); 26020Sstevel@tonic-gate if (localinfo == NULL) { 26030Sstevel@tonic-gate (void) syslog(LOG_ERR, "n2a get_local_info: malloc failed: %m"); 26040Sstevel@tonic-gate free(buf); 26050Sstevel@tonic-gate _nderror = ND_SYSTEM; 26060Sstevel@tonic-gate return (NULL); 26070Sstevel@tonic-gate } 26080Sstevel@tonic-gate 2609132Srobinson /* LINTED pointer cast */ 26100Sstevel@tonic-gate localinfo->addresses = (struct __ifaddr *) 26110Sstevel@tonic-gate ((char *)localinfo + sizeof (struct ifinfo)); 26120Sstevel@tonic-gate 26130Sstevel@tonic-gate for (localinfo->count = 0, n = numifs; n > 0; n--, lifr++) { 26140Sstevel@tonic-gate int af; 26150Sstevel@tonic-gate 26160Sstevel@tonic-gate lifreq = *lifr; 26170Sstevel@tonic-gate af = lifreq.lifr_addr.ss_family; 26180Sstevel@tonic-gate 26190Sstevel@tonic-gate /* Squirrel away the address */ 26200Sstevel@tonic-gate if (ifassign(lifreq, localinfo->count, IF_ADDR) == 0) 26210Sstevel@tonic-gate continue; 26220Sstevel@tonic-gate 26230Sstevel@tonic-gate if (nss_ioctl(af, SIOCGLIFFLAGS, &lifreq) < 0) { 26240Sstevel@tonic-gate (void) syslog(LOG_ERR, 26250Sstevel@tonic-gate "n2a get_local_info: " 26260Sstevel@tonic-gate "ioctl (get interface flags): %m"); 26270Sstevel@tonic-gate continue; 26280Sstevel@tonic-gate } 26290Sstevel@tonic-gate if (!(lifreq.lifr_flags & IFF_UP)) 26300Sstevel@tonic-gate continue; 26310Sstevel@tonic-gate 26320Sstevel@tonic-gate if (nss_ioctl(af, SIOCGLIFNETMASK, &lifreq) < 0) { 26330Sstevel@tonic-gate (void) syslog(LOG_ERR, 26340Sstevel@tonic-gate "n2a get_local_info: " 26350Sstevel@tonic-gate "ioctl (get interface netmask): %m"); 26360Sstevel@tonic-gate continue; 26370Sstevel@tonic-gate } 26380Sstevel@tonic-gate 26390Sstevel@tonic-gate if (ifassign(lifreq, localinfo->count, IF_MASK) == 0) 26400Sstevel@tonic-gate continue; 26410Sstevel@tonic-gate 26420Sstevel@tonic-gate localinfo->count++; 26430Sstevel@tonic-gate } 26440Sstevel@tonic-gate 26450Sstevel@tonic-gate free(buf); 26460Sstevel@tonic-gate return (localinfo); 26470Sstevel@tonic-gate } 26480Sstevel@tonic-gate 26490Sstevel@tonic-gate static int 26500Sstevel@tonic-gate __inet_ifassign(sa_family_t af, struct __ifaddr *ifa, __ifaddr_type type, 26510Sstevel@tonic-gate void *addr) { 26520Sstevel@tonic-gate switch (type) { 26530Sstevel@tonic-gate case IF_ADDR: 26540Sstevel@tonic-gate ifa->af = af; 26550Sstevel@tonic-gate if (af == AF_INET6) { 26560Sstevel@tonic-gate ifa->addr.in6 = *(struct in6_addr *)addr; 26570Sstevel@tonic-gate } else { 26580Sstevel@tonic-gate ifa->addr.in4 = *(struct in_addr *)addr; 26590Sstevel@tonic-gate } 26600Sstevel@tonic-gate break; 26610Sstevel@tonic-gate case IF_MASK: 26620Sstevel@tonic-gate if (ifa->af == af) { 26630Sstevel@tonic-gate if (af == AF_INET6) { 26640Sstevel@tonic-gate ifa->mask.in6 = *(struct in6_addr *)addr; 26650Sstevel@tonic-gate } else { 26660Sstevel@tonic-gate ifa->mask.in4 = *(struct in_addr *)addr; 26670Sstevel@tonic-gate } 26680Sstevel@tonic-gate } else { 26690Sstevel@tonic-gate return (0); 26700Sstevel@tonic-gate } 26710Sstevel@tonic-gate break; 26720Sstevel@tonic-gate default: 26730Sstevel@tonic-gate return (0); 26740Sstevel@tonic-gate } 26750Sstevel@tonic-gate 26760Sstevel@tonic-gate return (1); 26770Sstevel@tonic-gate } 26780Sstevel@tonic-gate 26790Sstevel@tonic-gate /* 26800Sstevel@tonic-gate * Some higher-level routines for determining if an address is 26810Sstevel@tonic-gate * on a local network. 26820Sstevel@tonic-gate * 26830Sstevel@tonic-gate * __inet_get_local_interfaces() - get an opaque handle with 26840Sstevel@tonic-gate * with a list of local interfaces 26850Sstevel@tonic-gate * __inet_address_is_local() - return 1 if an address is 26860Sstevel@tonic-gate * on a local network; 0 otherwise 26870Sstevel@tonic-gate * __inet_free_local_interfaces() - free handle that was 26880Sstevel@tonic-gate * returned by __inet_get_local_interfaces() 26890Sstevel@tonic-gate * 26900Sstevel@tonic-gate * A typical calling sequence is: 26910Sstevel@tonic-gate * 26920Sstevel@tonic-gate * p = __inet_get_local_interfaces(); 26930Sstevel@tonic-gate * if (__inet_address_is_local(p, inaddr)) { 26940Sstevel@tonic-gate * ... 26950Sstevel@tonic-gate * } 26960Sstevel@tonic-gate * __inet_free_local_interfaces(p); 26970Sstevel@tonic-gate */ 26980Sstevel@tonic-gate 26990Sstevel@tonic-gate /* 27000Sstevel@tonic-gate * Return an opaque pointer to a list of configured interfaces. 27010Sstevel@tonic-gate */ 27020Sstevel@tonic-gate void * 27030Sstevel@tonic-gate __inet_get_local_interfaces(void) 27040Sstevel@tonic-gate { 27050Sstevel@tonic-gate return (get_local_info()); 27060Sstevel@tonic-gate } 27070Sstevel@tonic-gate 27080Sstevel@tonic-gate /* 27090Sstevel@tonic-gate * Free memory allocated by inet_local_interfaces(). 27100Sstevel@tonic-gate */ 27110Sstevel@tonic-gate void 27120Sstevel@tonic-gate __inet_free_local_interfaces(void *p) 27130Sstevel@tonic-gate { 27140Sstevel@tonic-gate free(p); 27150Sstevel@tonic-gate } 27160Sstevel@tonic-gate 27170Sstevel@tonic-gate /* 27180Sstevel@tonic-gate * Determine if an address is on a local network. 27190Sstevel@tonic-gate * 27200Sstevel@tonic-gate * Might have made sense to use SIOCTONLINK, except that it doesn't 27210Sstevel@tonic-gate * handle matching on IPv4 network addresses. 27220Sstevel@tonic-gate */ 27230Sstevel@tonic-gate int 27240Sstevel@tonic-gate __inet_address_is_local_af(void *p, sa_family_t af, void *addr) { 27250Sstevel@tonic-gate 27260Sstevel@tonic-gate struct ifinfo *localinfo = (struct ifinfo *)p; 27270Sstevel@tonic-gate int i, a; 27280Sstevel@tonic-gate struct in_addr v4addr; 27290Sstevel@tonic-gate 27300Sstevel@tonic-gate if (localinfo == 0) 27310Sstevel@tonic-gate return (0); 27320Sstevel@tonic-gate 27330Sstevel@tonic-gate if (af == AF_INET6 && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)addr)) { 27340Sstevel@tonic-gate IN6_V4MAPPED_TO_INADDR((struct in6_addr *)addr, &v4addr); 27350Sstevel@tonic-gate af = AF_INET; 27360Sstevel@tonic-gate addr = (void *)&v4addr; 27370Sstevel@tonic-gate } 27380Sstevel@tonic-gate 27390Sstevel@tonic-gate for (i = 0; i < localinfo->count; i++) { 27400Sstevel@tonic-gate if (ifaf(i) == af) { 27410Sstevel@tonic-gate if (af == AF_INET6) { 27420Sstevel@tonic-gate struct in6_addr *a6 = (struct in6_addr *)addr; 27430Sstevel@tonic-gate for (a = 0; a < sizeof (a6->s6_addr); a++) { 27440Sstevel@tonic-gate if ((a6->s6_addr[a] & 27450Sstevel@tonic-gate ifmask6(i).s6_addr[a]) != 27460Sstevel@tonic-gate (ifaddr6(i).s6_addr[a] & 27470Sstevel@tonic-gate ifmask6(i).s6_addr[a])) 27480Sstevel@tonic-gate break; 27490Sstevel@tonic-gate } 27500Sstevel@tonic-gate if (a >= sizeof (a6->s6_addr)) 27510Sstevel@tonic-gate return (1); 27520Sstevel@tonic-gate } else { 27530Sstevel@tonic-gate if ((((struct in_addr *)addr)->s_addr & 27540Sstevel@tonic-gate ifmask4(i).s_addr) == 27550Sstevel@tonic-gate (ifaddr4(i).s_addr & 27560Sstevel@tonic-gate ifmask4(i).s_addr)) 27570Sstevel@tonic-gate return (1); 27580Sstevel@tonic-gate } 27590Sstevel@tonic-gate } 27600Sstevel@tonic-gate } 27610Sstevel@tonic-gate 27620Sstevel@tonic-gate return (0); 27630Sstevel@tonic-gate } 27640Sstevel@tonic-gate 27650Sstevel@tonic-gate int 27660Sstevel@tonic-gate __inet_address_is_local(void *p, struct in_addr addr) 27670Sstevel@tonic-gate { 27680Sstevel@tonic-gate return (__inet_address_is_local_af(p, AF_INET, &addr)); 27690Sstevel@tonic-gate } 27700Sstevel@tonic-gate 27710Sstevel@tonic-gate int 27720Sstevel@tonic-gate __inet_uaddr_is_local(void *p, struct netconfig *nc, char *uaddr) 27730Sstevel@tonic-gate { 27740Sstevel@tonic-gate struct netbuf *taddr; 27750Sstevel@tonic-gate sa_family_t af; 27760Sstevel@tonic-gate int ret; 27770Sstevel@tonic-gate 27780Sstevel@tonic-gate taddr = uaddr2taddr(nc, uaddr); 27790Sstevel@tonic-gate if (taddr == 0) 27800Sstevel@tonic-gate return (0); 27810Sstevel@tonic-gate 2782132Srobinson /* LINTED pointer cast */ 27830Sstevel@tonic-gate af = ((struct sockaddr *)taddr->buf)->sa_family; 27840Sstevel@tonic-gate 27850Sstevel@tonic-gate ret = __inet_address_is_local_af(p, af, 27860Sstevel@tonic-gate (af == AF_INET6) ? 2787132Srobinson /* LINTED pointer cast */ 27880Sstevel@tonic-gate (void *)&((struct sockaddr_in6 *)taddr->buf)->sin6_addr : 2789132Srobinson /* LINTED pointer cast */ 27900Sstevel@tonic-gate (void *)&((struct sockaddr_in *)taddr->buf)->sin_addr); 27910Sstevel@tonic-gate 27920Sstevel@tonic-gate netdir_free(taddr, ND_ADDR); 27930Sstevel@tonic-gate return (ret); 27940Sstevel@tonic-gate } 27950Sstevel@tonic-gate 27960Sstevel@tonic-gate 27970Sstevel@tonic-gate int 27980Sstevel@tonic-gate __inet_address_count(void *p) 27990Sstevel@tonic-gate { 28000Sstevel@tonic-gate struct ifinfo *lp = (struct ifinfo *)p; 28010Sstevel@tonic-gate 28020Sstevel@tonic-gate if (lp != 0) { 28030Sstevel@tonic-gate return (lp->count); 28040Sstevel@tonic-gate } else { 28050Sstevel@tonic-gate return (0); 28060Sstevel@tonic-gate } 28070Sstevel@tonic-gate } 28080Sstevel@tonic-gate 28090Sstevel@tonic-gate uint32_t 28100Sstevel@tonic-gate __inet_get_addr(void *p, int n) 28110Sstevel@tonic-gate { 28120Sstevel@tonic-gate struct ifinfo *localinfo = (struct ifinfo *)p; 28130Sstevel@tonic-gate 28140Sstevel@tonic-gate if (localinfo == 0 || n >= localinfo->count || ifaf(n) != AF_INET) 28150Sstevel@tonic-gate return (0); 28160Sstevel@tonic-gate 28170Sstevel@tonic-gate return (ifaddr4(n).s_addr); 28180Sstevel@tonic-gate } 28190Sstevel@tonic-gate 28200Sstevel@tonic-gate uint32_t 28210Sstevel@tonic-gate __inet_get_network(void *p, int n) 28220Sstevel@tonic-gate { 28230Sstevel@tonic-gate struct ifinfo *localinfo = (struct ifinfo *)p; 28240Sstevel@tonic-gate 28250Sstevel@tonic-gate if (localinfo == 0 || n >= localinfo->count || ifaf(n) != AF_INET) 28260Sstevel@tonic-gate return (0); 28270Sstevel@tonic-gate 28280Sstevel@tonic-gate return (ifaddr4(n).s_addr & ifmask4(n).s_addr); 28290Sstevel@tonic-gate } 28300Sstevel@tonic-gate 28310Sstevel@tonic-gate char * 28320Sstevel@tonic-gate __inet_get_uaddr(void *p, struct netconfig *nc, int n) 28330Sstevel@tonic-gate { 28340Sstevel@tonic-gate struct ifinfo *localinfo = (struct ifinfo *)p; 28350Sstevel@tonic-gate char *uaddr; 28360Sstevel@tonic-gate struct sockaddr_in sin4; 28370Sstevel@tonic-gate struct sockaddr_in6 sin6; 28380Sstevel@tonic-gate struct netbuf nb; 28390Sstevel@tonic-gate 28400Sstevel@tonic-gate if (localinfo == 0 || nc == 0 || n >= localinfo->count) 28410Sstevel@tonic-gate return (0); 28420Sstevel@tonic-gate 28430Sstevel@tonic-gate if (ifaf(n) == AF_INET6) { 28440Sstevel@tonic-gate if (strcmp(NC_INET6, nc->nc_protofmly) != 0) 28450Sstevel@tonic-gate return (0); 2846132Srobinson (void) memset(&sin6, 0, sizeof (sin6)); 28470Sstevel@tonic-gate sin6.sin6_family = AF_INET6; 28480Sstevel@tonic-gate sin6.sin6_addr = ifaddr6(n); 28490Sstevel@tonic-gate nb.buf = (char *)&sin6; 28500Sstevel@tonic-gate nb.len = sizeof (sin6); 28510Sstevel@tonic-gate } else { 28520Sstevel@tonic-gate if (strcmp(NC_INET, nc->nc_protofmly) != 0) 28530Sstevel@tonic-gate return (0); 2854132Srobinson (void) memset(&sin4, 0, sizeof (sin4)); 28550Sstevel@tonic-gate sin4.sin_family = AF_INET; 28560Sstevel@tonic-gate sin4.sin_addr = ifaddr4(n); 28570Sstevel@tonic-gate nb.buf = (char *)&sin4; 28580Sstevel@tonic-gate nb.len = sizeof (sin4); 28590Sstevel@tonic-gate } 28600Sstevel@tonic-gate 28610Sstevel@tonic-gate nb.maxlen = nb.len; 28620Sstevel@tonic-gate 28630Sstevel@tonic-gate uaddr = taddr2uaddr(nc, &nb); 28640Sstevel@tonic-gate return (uaddr); 28650Sstevel@tonic-gate } 28660Sstevel@tonic-gate 28670Sstevel@tonic-gate char * 28680Sstevel@tonic-gate __inet_get_networka(void *p, int n) 28690Sstevel@tonic-gate { 28700Sstevel@tonic-gate struct ifinfo *localinfo = (struct ifinfo *)p; 28710Sstevel@tonic-gate 28720Sstevel@tonic-gate if (localinfo == 0 || n >= localinfo->count) 28730Sstevel@tonic-gate return (0); 28740Sstevel@tonic-gate 28750Sstevel@tonic-gate if (ifaf(n) == AF_INET6) { 28760Sstevel@tonic-gate char buf[INET6_ADDRSTRLEN]; 28770Sstevel@tonic-gate struct in6_addr in6; 28780Sstevel@tonic-gate int i; 28790Sstevel@tonic-gate 28800Sstevel@tonic-gate for (i = 0; i < sizeof (in6.s6_addr); i++) { 28810Sstevel@tonic-gate in6.s6_addr[i] = ifaddr6(n).s6_addr[i] & 28820Sstevel@tonic-gate ifmask6(n).s6_addr[i]; 28830Sstevel@tonic-gate } 28840Sstevel@tonic-gate return (strdup(inet_ntop(AF_INET6, &in6, buf, sizeof (buf)))); 28850Sstevel@tonic-gate } else { 28860Sstevel@tonic-gate struct in_addr in4; 28870Sstevel@tonic-gate 28880Sstevel@tonic-gate in4.s_addr = ifaddr4(n).s_addr & ifmask4(n).s_addr; 28890Sstevel@tonic-gate return (strdup(inet_ntoa(in4))); 28900Sstevel@tonic-gate } 28910Sstevel@tonic-gate } 28920Sstevel@tonic-gate 28930Sstevel@tonic-gate static int 28940Sstevel@tonic-gate in_list(struct in_addr *addrs, int n, struct in_addr a) 28950Sstevel@tonic-gate { 28960Sstevel@tonic-gate int i; 28970Sstevel@tonic-gate 28980Sstevel@tonic-gate for (i = 0; i < n; i++) { 28990Sstevel@tonic-gate if (addrs[i].s_addr == a.s_addr) 29000Sstevel@tonic-gate return (1); 29010Sstevel@tonic-gate } 29020Sstevel@tonic-gate return (0); 29030Sstevel@tonic-gate } 29040Sstevel@tonic-gate 29050Sstevel@tonic-gate static int 29060Sstevel@tonic-gate getbroadcastnets(struct netconfig *tp, struct in_addr **addrs) 29070Sstevel@tonic-gate { 29080Sstevel@tonic-gate struct ifconf ifc; 29090Sstevel@tonic-gate struct ifreq ifreq, *ifr; 29100Sstevel@tonic-gate struct sockaddr_in *sin; 29110Sstevel@tonic-gate struct in_addr a; 29120Sstevel@tonic-gate int fd; 29130Sstevel@tonic-gate int n, i, numifs; 29140Sstevel@tonic-gate char *buf; 29150Sstevel@tonic-gate int use_loopback = 0; 29160Sstevel@tonic-gate 29170Sstevel@tonic-gate _nderror = ND_SYSTEM; 29180Sstevel@tonic-gate fd = open(tp->nc_device, O_RDONLY); 29190Sstevel@tonic-gate if (fd < 0) { 29200Sstevel@tonic-gate (void) syslog(LOG_ERR, 29210Sstevel@tonic-gate "broadcast: open to get interface configuration: %m"); 29220Sstevel@tonic-gate return (0); 29230Sstevel@tonic-gate } 29240Sstevel@tonic-gate if (ioctl(fd, SIOCGIFNUM, (char *)&numifs) < 0) 29250Sstevel@tonic-gate numifs = MAXIFS; 2926132Srobinson buf = malloc(numifs * sizeof (struct ifreq)); 29270Sstevel@tonic-gate if (buf == NULL) { 29280Sstevel@tonic-gate (void) syslog(LOG_ERR, "broadcast: malloc failed: %m"); 29290Sstevel@tonic-gate (void) close(fd); 29300Sstevel@tonic-gate return (0); 29310Sstevel@tonic-gate } 2932132Srobinson *addrs = malloc(numifs * sizeof (struct in_addr)); 29330Sstevel@tonic-gate if (*addrs == NULL) { 29340Sstevel@tonic-gate (void) syslog(LOG_ERR, "broadcast: malloc failed: %m"); 29350Sstevel@tonic-gate free(buf); 29360Sstevel@tonic-gate (void) close(fd); 29370Sstevel@tonic-gate return (0); 29380Sstevel@tonic-gate } 29390Sstevel@tonic-gate ifc.ifc_len = numifs * (int)sizeof (struct ifreq); 29400Sstevel@tonic-gate ifc.ifc_buf = buf; 29410Sstevel@tonic-gate /* 29420Sstevel@tonic-gate * Ideally, this ioctl should also tell me, how many bytes were 29430Sstevel@tonic-gate * finally allocated, but it doesnt. 29440Sstevel@tonic-gate */ 29450Sstevel@tonic-gate if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0) { 29460Sstevel@tonic-gate (void) syslog(LOG_ERR, 29470Sstevel@tonic-gate "broadcast: ioctl (get interface configuration): %m"); 29480Sstevel@tonic-gate free(buf); 29490Sstevel@tonic-gate free(*addrs); 29500Sstevel@tonic-gate (void) close(fd); 29510Sstevel@tonic-gate return (0); 29520Sstevel@tonic-gate } 29530Sstevel@tonic-gate 29540Sstevel@tonic-gate retry: 2955132Srobinson /* LINTED pointer cast */ 29560Sstevel@tonic-gate ifr = (struct ifreq *)buf; 29570Sstevel@tonic-gate for (i = 0, n = ifc.ifc_len / (int)sizeof (struct ifreq); 29580Sstevel@tonic-gate n > 0; n--, ifr++) { 29590Sstevel@tonic-gate ifreq = *ifr; 29600Sstevel@tonic-gate if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 29610Sstevel@tonic-gate (void) syslog(LOG_ERR, 29620Sstevel@tonic-gate "broadcast: ioctl (get interface flags): %m"); 29630Sstevel@tonic-gate continue; 29640Sstevel@tonic-gate } 29650Sstevel@tonic-gate if (!(ifreq.ifr_flags & IFF_UP) || 29660Sstevel@tonic-gate (ifr->ifr_addr.sa_family != AF_INET)) 29670Sstevel@tonic-gate continue; 29680Sstevel@tonic-gate if (ifreq.ifr_flags & IFF_BROADCAST) { 2969132Srobinson /* LINTED pointer cast */ 29700Sstevel@tonic-gate sin = (struct sockaddr_in *)&ifr->ifr_addr; 29710Sstevel@tonic-gate if (ioctl(fd, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { 29720Sstevel@tonic-gate /* May not work with other implementation */ 29730Sstevel@tonic-gate a = _inet_makeaddr( 29740Sstevel@tonic-gate inet_netof(sin->sin_addr), 29750Sstevel@tonic-gate INADDR_ANY); 29760Sstevel@tonic-gate if (!in_list(*addrs, i, a)) 29770Sstevel@tonic-gate (*addrs)[i++] = a; 29780Sstevel@tonic-gate } else { 2979132Srobinson /* LINTED pointer cast */ 29800Sstevel@tonic-gate a = ((struct sockaddr_in *) 29810Sstevel@tonic-gate &ifreq.ifr_addr)->sin_addr; 29820Sstevel@tonic-gate if (!in_list(*addrs, i, a)) 29830Sstevel@tonic-gate (*addrs)[i++] = a; 29840Sstevel@tonic-gate } 29850Sstevel@tonic-gate continue; 29860Sstevel@tonic-gate } 29870Sstevel@tonic-gate if (use_loopback && (ifreq.ifr_flags & IFF_LOOPBACK)) { 2988132Srobinson /* LINTED pointer cast */ 29890Sstevel@tonic-gate sin = (struct sockaddr_in *)&ifr->ifr_addr; 29900Sstevel@tonic-gate a = sin->sin_addr; 29910Sstevel@tonic-gate if (!in_list(*addrs, i, a)) 29920Sstevel@tonic-gate (*addrs)[i++] = a; 29930Sstevel@tonic-gate continue; 29940Sstevel@tonic-gate } 29950Sstevel@tonic-gate if (ifreq.ifr_flags & IFF_POINTOPOINT) { 29960Sstevel@tonic-gate if (ioctl(fd, SIOCGIFDSTADDR, (char *)&ifreq) < 0) 29970Sstevel@tonic-gate continue; 2998132Srobinson /* LINTED pointer cast */ 29990Sstevel@tonic-gate a = ((struct sockaddr_in *) 30000Sstevel@tonic-gate &ifreq.ifr_addr)->sin_addr; 30010Sstevel@tonic-gate if (!in_list(*addrs, i, a)) 30020Sstevel@tonic-gate (*addrs)[i++] = a; 30030Sstevel@tonic-gate continue; 30040Sstevel@tonic-gate } 30050Sstevel@tonic-gate } 30060Sstevel@tonic-gate if (i == 0 && !use_loopback) { 30070Sstevel@tonic-gate use_loopback = 1; 30080Sstevel@tonic-gate goto retry; 30090Sstevel@tonic-gate } 30100Sstevel@tonic-gate free(buf); 30110Sstevel@tonic-gate (void) close(fd); 30120Sstevel@tonic-gate if (i) 30130Sstevel@tonic-gate _nderror = ND_OK; 30140Sstevel@tonic-gate else 30150Sstevel@tonic-gate free(*addrs); 30160Sstevel@tonic-gate return (i); 30170Sstevel@tonic-gate } 30180Sstevel@tonic-gate 30190Sstevel@tonic-gate /* 30200Sstevel@tonic-gate * This is lifted straight from libsocket/inet/inet_mkaddr.c. 30210Sstevel@tonic-gate * Copied here to avoid our dependency on libsocket. More importantly, 30220Sstevel@tonic-gate * to make sure partially static apps that use libnsl, but not 30230Sstevel@tonic-gate * libsocket, don't get screwed up. 30240Sstevel@tonic-gate * If you understand the above paragraph, try to get rid of 30250Sstevel@tonic-gate * this copy of inet_makeaddr; if you don;t, leave it alone. 30260Sstevel@tonic-gate * 30270Sstevel@tonic-gate * Formulate an Internet address from network + host. Used in 30280Sstevel@tonic-gate * building addresses stored in the ifnet structure. 30290Sstevel@tonic-gate */ 30300Sstevel@tonic-gate static struct in_addr 30310Sstevel@tonic-gate _inet_makeaddr(in_addr_t net, in_addr_t host) 30320Sstevel@tonic-gate { 30330Sstevel@tonic-gate in_addr_t addr; 30340Sstevel@tonic-gate struct in_addr inaddr; 30350Sstevel@tonic-gate 30360Sstevel@tonic-gate if (net < 128) 30370Sstevel@tonic-gate addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST); 30380Sstevel@tonic-gate else if (net < 65536) 30390Sstevel@tonic-gate addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST); 30400Sstevel@tonic-gate else if (net < 16777216L) 30410Sstevel@tonic-gate addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST); 30420Sstevel@tonic-gate else 30430Sstevel@tonic-gate addr = net | host; 30440Sstevel@tonic-gate inaddr.s_addr = htonl(addr); 30450Sstevel@tonic-gate return (inaddr); 30460Sstevel@tonic-gate } 30470Sstevel@tonic-gate 30480Sstevel@tonic-gate /* 30490Sstevel@tonic-gate * Routine to read the default configuration file and check if SORT_ADDRS 30500Sstevel@tonic-gate * is set to NO or FALSE. This routine is called by order_haddrlist_af() 30510Sstevel@tonic-gate * to determine if the addresses need to be sorted. 30520Sstevel@tonic-gate */ 30530Sstevel@tonic-gate static boolean_t 30540Sstevel@tonic-gate _read_nsw_file(void) 30550Sstevel@tonic-gate { 30560Sstevel@tonic-gate char defval[LINESIZE]; 3057*1914Scasper FILE *defl; 30580Sstevel@tonic-gate boolean_t nosort = B_FALSE; 30590Sstevel@tonic-gate 30600Sstevel@tonic-gate 30610Sstevel@tonic-gate do { 3062*1914Scasper defl = fopen(__NSW_DEFAULT_FILE, "rF"); 30630Sstevel@tonic-gate } while ((defl == NULL) && (errno == EINTR)); 30640Sstevel@tonic-gate 30650Sstevel@tonic-gate if (defl == NULL) 30660Sstevel@tonic-gate return (B_FALSE); 30670Sstevel@tonic-gate 3068*1914Scasper while (fgets(defval, sizeof (defval), defl) != NULL) { 30690Sstevel@tonic-gate if ((strncmp(DONT_SORT, defval, sizeof (DONT_SORT) - 1) == 0) || 30700Sstevel@tonic-gate (strncmp(DONT_SORT2, defval, 30710Sstevel@tonic-gate sizeof (DONT_SORT2) - 1) == 0)) { 30720Sstevel@tonic-gate nosort = B_TRUE; 30730Sstevel@tonic-gate break; 30740Sstevel@tonic-gate } 30750Sstevel@tonic-gate } 3076*1914Scasper (void) fclose(defl); 30770Sstevel@tonic-gate return (nosort); 30780Sstevel@tonic-gate } 3079