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