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