10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1676Sjpk * Common Development and Distribution License (the "License"). 6*1676Sjpk * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*1676Sjpk * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* 270Sstevel@tonic-gate * nfs_tbind.c, common part for nfsd and lockd. 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <tiuser.h> 330Sstevel@tonic-gate #include <fcntl.h> 340Sstevel@tonic-gate #include <netconfig.h> 350Sstevel@tonic-gate #include <stropts.h> 360Sstevel@tonic-gate #include <errno.h> 370Sstevel@tonic-gate #include <syslog.h> 380Sstevel@tonic-gate #include <rpc/rpc.h> 390Sstevel@tonic-gate #include <sys/time.h> 400Sstevel@tonic-gate #include <sys/resource.h> 410Sstevel@tonic-gate #include <signal.h> 420Sstevel@tonic-gate #include <netdir.h> 430Sstevel@tonic-gate #include <unistd.h> 440Sstevel@tonic-gate #include <string.h> 450Sstevel@tonic-gate #include <netinet/tcp.h> 460Sstevel@tonic-gate #include <malloc.h> 470Sstevel@tonic-gate #include <stdlib.h> 480Sstevel@tonic-gate #include "nfs_tbind.h" 490Sstevel@tonic-gate #include <nfs/nfs.h> 500Sstevel@tonic-gate #include <nfs/nfs_acl.h> 510Sstevel@tonic-gate #include <nfs/nfssys.h> 520Sstevel@tonic-gate #include <nfs/nfs4.h> 53*1676Sjpk #include <zone.h> 54*1676Sjpk #include <sys/socket.h> 55*1676Sjpk #include <tsol/label.h> 560Sstevel@tonic-gate 570Sstevel@tonic-gate /* 580Sstevel@tonic-gate * Determine valid semantics for most applications. 590Sstevel@tonic-gate */ 600Sstevel@tonic-gate #define OK_TPI_TYPE(_nconf) \ 610Sstevel@tonic-gate (_nconf->nc_semantics == NC_TPI_CLTS || \ 620Sstevel@tonic-gate _nconf->nc_semantics == NC_TPI_COTS || \ 630Sstevel@tonic-gate _nconf->nc_semantics == NC_TPI_COTS_ORD) 640Sstevel@tonic-gate 650Sstevel@tonic-gate #define BE32_TO_U32(a) \ 660Sstevel@tonic-gate ((((ulong_t)((uchar_t *)a)[0] & 0xFF) << (ulong_t)24) | \ 670Sstevel@tonic-gate (((ulong_t)((uchar_t *)a)[1] & 0xFF) << (ulong_t)16) | \ 680Sstevel@tonic-gate (((ulong_t)((uchar_t *)a)[2] & 0xFF) << (ulong_t)8) | \ 690Sstevel@tonic-gate ((ulong_t)((uchar_t *)a)[3] & 0xFF)) 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* 720Sstevel@tonic-gate * Number of elements to add to the poll array on each allocation. 730Sstevel@tonic-gate */ 740Sstevel@tonic-gate #define POLL_ARRAY_INC_SIZE 64 750Sstevel@tonic-gate 760Sstevel@tonic-gate /* 770Sstevel@tonic-gate * Number of file descriptors by which the process soft limit may be 780Sstevel@tonic-gate * increased on each call to nofile_increase(0). 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate #define NOFILE_INC_SIZE 64 810Sstevel@tonic-gate 820Sstevel@tonic-gate struct conn_ind { 830Sstevel@tonic-gate struct conn_ind *conn_next; 840Sstevel@tonic-gate struct conn_ind *conn_prev; 850Sstevel@tonic-gate struct t_call *conn_call; 860Sstevel@tonic-gate }; 870Sstevel@tonic-gate 880Sstevel@tonic-gate struct conn_entry { 890Sstevel@tonic-gate bool_t closing; 900Sstevel@tonic-gate struct netconfig nc; 910Sstevel@tonic-gate }; 920Sstevel@tonic-gate 930Sstevel@tonic-gate /* 940Sstevel@tonic-gate * this file contains transport routines common to nfsd and lockd 950Sstevel@tonic-gate */ 960Sstevel@tonic-gate static int nofile_increase(int); 970Sstevel@tonic-gate static int reuseaddr(int); 98*1676Sjpk static int recvucred(int); 99*1676Sjpk static int anonmlp(int); 1000Sstevel@tonic-gate static void add_to_poll_list(int, struct netconfig *); 1010Sstevel@tonic-gate static char *serv_name_to_port_name(char *); 1020Sstevel@tonic-gate static int bind_to_proto(char *, char *, struct netbuf **, 1030Sstevel@tonic-gate struct netconfig **); 1040Sstevel@tonic-gate static int bind_to_provider(char *, char *, struct netbuf **, 1050Sstevel@tonic-gate struct netconfig **); 1060Sstevel@tonic-gate static void conn_close_oldest(void); 1070Sstevel@tonic-gate static boolean_t conn_get(int, struct netconfig *, struct conn_ind **); 1080Sstevel@tonic-gate static void cots_listen_event(int, int); 1090Sstevel@tonic-gate static int discon_get(int, struct netconfig *, struct conn_ind **); 1100Sstevel@tonic-gate static int do_poll_clts_action(int, int); 1110Sstevel@tonic-gate static int do_poll_cots_action(int, int); 1120Sstevel@tonic-gate static void remove_from_poll_list(int); 1130Sstevel@tonic-gate static int set_addrmask(int, struct netconfig *, struct netbuf *); 1140Sstevel@tonic-gate static int is_listen_fd_index(int); 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate static struct pollfd *poll_array; 1170Sstevel@tonic-gate static struct conn_entry *conn_polled; 1180Sstevel@tonic-gate static int num_conns; /* Current number of connections */ 1190Sstevel@tonic-gate int (*Mysvc4)(int, struct netbuf *, struct netconfig *, int, 1200Sstevel@tonic-gate struct netbuf *); 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate /* 1230Sstevel@tonic-gate * Called to create and prepare a transport descriptor for in-kernel 1240Sstevel@tonic-gate * RPC service. 1250Sstevel@tonic-gate * Returns -1 on failure and a valid descriptor on success. 1260Sstevel@tonic-gate */ 1270Sstevel@tonic-gate int 1280Sstevel@tonic-gate nfslib_transport_open(struct netconfig *nconf) 1290Sstevel@tonic-gate { 1300Sstevel@tonic-gate int fd; 1310Sstevel@tonic-gate struct strioctl strioc; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate if ((nconf == (struct netconfig *)NULL) || 1340Sstevel@tonic-gate (nconf->nc_device == (char *)NULL)) { 1350Sstevel@tonic-gate syslog(LOG_ERR, "no netconfig device"); 1360Sstevel@tonic-gate return (-1); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate /* 1400Sstevel@tonic-gate * Open the transport device. 1410Sstevel@tonic-gate */ 1420Sstevel@tonic-gate fd = t_open(nconf->nc_device, O_RDWR, (struct t_info *)NULL); 1430Sstevel@tonic-gate if (fd == -1) { 1440Sstevel@tonic-gate if (t_errno == TSYSERR && errno == EMFILE && 1450Sstevel@tonic-gate (nofile_increase(0) == 0)) { 1460Sstevel@tonic-gate /* Try again with a higher NOFILE limit. */ 1470Sstevel@tonic-gate fd = t_open(nconf->nc_device, O_RDWR, 1480Sstevel@tonic-gate (struct t_info *)NULL); 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate if (fd == -1) { 1510Sstevel@tonic-gate syslog(LOG_ERR, "t_open %s failed: t_errno %d, %m", 1520Sstevel@tonic-gate nconf->nc_device, t_errno); 1530Sstevel@tonic-gate return (-1); 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* 1580Sstevel@tonic-gate * Pop timod because the RPC module must be as close as possible 1590Sstevel@tonic-gate * to the transport. 1600Sstevel@tonic-gate */ 1610Sstevel@tonic-gate if (ioctl(fd, I_POP, 0) < 0) { 1620Sstevel@tonic-gate syslog(LOG_ERR, "I_POP of timod failed: %m"); 1630Sstevel@tonic-gate (void) t_close(fd); 1640Sstevel@tonic-gate return (-1); 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * Common code for CLTS and COTS transports 1690Sstevel@tonic-gate */ 1700Sstevel@tonic-gate if (ioctl(fd, I_PUSH, "rpcmod") < 0) { 1710Sstevel@tonic-gate syslog(LOG_ERR, "I_PUSH of rpcmod failed: %m"); 1720Sstevel@tonic-gate (void) t_close(fd); 1730Sstevel@tonic-gate return (-1); 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate strioc.ic_cmd = RPC_SERVER; 1770Sstevel@tonic-gate strioc.ic_dp = (char *)0; 1780Sstevel@tonic-gate strioc.ic_len = 0; 1790Sstevel@tonic-gate strioc.ic_timout = -1; 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* Tell rpcmod to act like a server stream. */ 1820Sstevel@tonic-gate if (ioctl(fd, I_STR, &strioc) < 0) { 1830Sstevel@tonic-gate syslog(LOG_ERR, "rpcmod set-up ioctl failed: %m"); 1840Sstevel@tonic-gate (void) t_close(fd); 1850Sstevel@tonic-gate return (-1); 1860Sstevel@tonic-gate } 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate /* 1890Sstevel@tonic-gate * Re-push timod so that we will still be doing TLI 1900Sstevel@tonic-gate * operations on the descriptor. 1910Sstevel@tonic-gate */ 1920Sstevel@tonic-gate if (ioctl(fd, I_PUSH, "timod") < 0) { 1930Sstevel@tonic-gate syslog(LOG_ERR, "I_PUSH of timod failed: %m"); 1940Sstevel@tonic-gate (void) t_close(fd); 1950Sstevel@tonic-gate return (-1); 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate return (fd); 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate static int 2020Sstevel@tonic-gate nofile_increase(int limit) 2030Sstevel@tonic-gate { 2040Sstevel@tonic-gate struct rlimit rl; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rl) == -1) { 2070Sstevel@tonic-gate syslog(LOG_ERR, "getrlimit of NOFILE failed: %m"); 2080Sstevel@tonic-gate return (-1); 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate if (limit > 0) 2120Sstevel@tonic-gate rl.rlim_cur = limit; 2130Sstevel@tonic-gate else 2140Sstevel@tonic-gate rl.rlim_cur += NOFILE_INC_SIZE; 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate if (rl.rlim_cur > rl.rlim_max && 2170Sstevel@tonic-gate rl.rlim_max != RLIM_INFINITY) 2180Sstevel@tonic-gate rl.rlim_max = rl.rlim_cur; 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rl) == -1) { 2210Sstevel@tonic-gate syslog(LOG_ERR, "setrlimit of NOFILE to %d failed: %m", 2220Sstevel@tonic-gate rl.rlim_cur); 2230Sstevel@tonic-gate return (-1); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate return (0); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate int 2300Sstevel@tonic-gate nfslib_bindit(struct netconfig *nconf, struct netbuf **addr, 2310Sstevel@tonic-gate struct nd_hostserv *hs, int backlog) 2320Sstevel@tonic-gate { 2330Sstevel@tonic-gate int fd; 2340Sstevel@tonic-gate struct t_bind *ntb; 2350Sstevel@tonic-gate struct t_bind tb; 2360Sstevel@tonic-gate struct nd_addrlist *addrlist; 2370Sstevel@tonic-gate struct t_optmgmt req, resp; 2380Sstevel@tonic-gate struct opthdr *opt; 2390Sstevel@tonic-gate char reqbuf[128]; 2400Sstevel@tonic-gate bool_t use_any = FALSE; 241*1676Sjpk bool_t gzone = TRUE; 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate if ((fd = nfslib_transport_open(nconf)) == -1) { 2440Sstevel@tonic-gate syslog(LOG_ERR, "cannot establish transport service over %s", 2450Sstevel@tonic-gate nconf->nc_device); 2460Sstevel@tonic-gate return (-1); 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate addrlist = (struct nd_addrlist *)NULL; 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* nfs4_callback service does not used a fieed port number */ 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate if (strcmp(hs->h_serv, "nfs4_callback") == 0) { 2540Sstevel@tonic-gate tb.addr.maxlen = 0; 2550Sstevel@tonic-gate tb.addr.len = 0; 2560Sstevel@tonic-gate tb.addr.buf = 0; 2570Sstevel@tonic-gate use_any = TRUE; 258*1676Sjpk gzone = (getzoneid() == GLOBAL_ZONEID); 2590Sstevel@tonic-gate } else if (netdir_getbyname(nconf, hs, &addrlist) != 0) { 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate syslog(LOG_ERR, 2620Sstevel@tonic-gate "Cannot get address for transport %s host %s service %s", 2630Sstevel@tonic-gate nconf->nc_netid, hs->h_host, hs->h_serv); 2640Sstevel@tonic-gate (void) t_close(fd); 2650Sstevel@tonic-gate return (-1); 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate if (strcmp(nconf->nc_proto, "tcp") == 0) { 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * If we're running over TCP, then set the 2710Sstevel@tonic-gate * SO_REUSEADDR option so that we can bind 2720Sstevel@tonic-gate * to our preferred address even if previously 2730Sstevel@tonic-gate * left connections exist in FIN_WAIT states. 2740Sstevel@tonic-gate * This is somewhat bogus, but otherwise you have 2750Sstevel@tonic-gate * to wait 2 minutes to restart after killing it. 2760Sstevel@tonic-gate */ 2770Sstevel@tonic-gate if (reuseaddr(fd) == -1) { 2780Sstevel@tonic-gate syslog(LOG_WARNING, 2790Sstevel@tonic-gate "couldn't set SO_REUSEADDR option on transport"); 2800Sstevel@tonic-gate } 281*1676Sjpk } else if (strcmp(nconf->nc_proto, "udp") == 0) { 282*1676Sjpk /* 283*1676Sjpk * In order to run MLP on UDP, we need to handle creds. 284*1676Sjpk */ 285*1676Sjpk if (recvucred(fd) == -1) { 286*1676Sjpk syslog(LOG_WARNING, 287*1676Sjpk "couldn't set SO_RECVUCRED option on transport"); 288*1676Sjpk } 289*1676Sjpk } 290*1676Sjpk 291*1676Sjpk /* 292*1676Sjpk * Make non global zone nfs4_callback port MLP 293*1676Sjpk */ 294*1676Sjpk if (use_any && is_system_labeled() && !gzone) { 295*1676Sjpk if (anonmlp(fd) == -1) { 296*1676Sjpk /* 297*1676Sjpk * failing to set this option means nfs4_callback 298*1676Sjpk * could fail silently later. So fail it with 299*1676Sjpk * with an error message now. 300*1676Sjpk */ 301*1676Sjpk syslog(LOG_ERR, 302*1676Sjpk "couldn't set SO_ANON_MLP option on transport"); 303*1676Sjpk (void) t_close(fd); 304*1676Sjpk return (-1); 305*1676Sjpk } 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate if (nconf->nc_semantics == NC_TPI_CLTS) 3090Sstevel@tonic-gate tb.qlen = 0; 3100Sstevel@tonic-gate else 3110Sstevel@tonic-gate tb.qlen = backlog; 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate /* LINTED pointer alignment */ 3140Sstevel@tonic-gate ntb = (struct t_bind *)t_alloc(fd, T_BIND, T_ALL); 3150Sstevel@tonic-gate if (ntb == (struct t_bind *)NULL) { 3160Sstevel@tonic-gate syslog(LOG_ERR, "t_alloc failed: t_errno %d, %m", t_errno); 3170Sstevel@tonic-gate (void) t_close(fd); 3180Sstevel@tonic-gate netdir_free((void *)addrlist, ND_ADDRLIST); 3190Sstevel@tonic-gate return (-1); 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate /* 3230Sstevel@tonic-gate * XXX - what about the space tb->addr.buf points to? This should 3240Sstevel@tonic-gate * be either a memcpy() to/from the buf fields, or t_alloc(fd,T_BIND,) 3250Sstevel@tonic-gate * should't be called with T_ALL. 3260Sstevel@tonic-gate */ 3270Sstevel@tonic-gate if (addrlist) 3280Sstevel@tonic-gate tb.addr = *(addrlist->n_addrs); /* structure copy */ 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate if (t_bind(fd, &tb, ntb) == -1) { 3310Sstevel@tonic-gate syslog(LOG_ERR, "t_bind failed: t_errno %d, %m", t_errno); 3320Sstevel@tonic-gate (void) t_free((char *)ntb, T_BIND); 3330Sstevel@tonic-gate netdir_free((void *)addrlist, ND_ADDRLIST); 3340Sstevel@tonic-gate (void) t_close(fd); 3350Sstevel@tonic-gate return (-1); 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate /* make sure we bound to the right address */ 3390Sstevel@tonic-gate if (use_any == FALSE && 3400Sstevel@tonic-gate (tb.addr.len != ntb->addr.len || 3410Sstevel@tonic-gate memcmp(tb.addr.buf, ntb->addr.buf, tb.addr.len) != 0)) { 3420Sstevel@tonic-gate syslog(LOG_ERR, "t_bind to wrong address"); 3430Sstevel@tonic-gate (void) t_free((char *)ntb, T_BIND); 3440Sstevel@tonic-gate netdir_free((void *)addrlist, ND_ADDRLIST); 3450Sstevel@tonic-gate (void) t_close(fd); 3460Sstevel@tonic-gate return (-1); 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate /* 3500Sstevel@tonic-gate * Call nfs4svc_setport so that the kernel can be 3510Sstevel@tonic-gate * informed what port number the daemon is listing 3520Sstevel@tonic-gate * for incoming connection requests. 3530Sstevel@tonic-gate */ 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate if ((nconf->nc_semantics == NC_TPI_COTS || 3560Sstevel@tonic-gate nconf->nc_semantics == NC_TPI_COTS_ORD) && Mysvc4 != NULL) 3570Sstevel@tonic-gate (*Mysvc4)(fd, NULL, nconf, NFS4_SETPORT, &ntb->addr); 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate *addr = &ntb->addr; 3600Sstevel@tonic-gate netdir_free((void *)addrlist, ND_ADDRLIST); 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate if (strcmp(nconf->nc_proto, "tcp") == 0) { 3630Sstevel@tonic-gate /* 3640Sstevel@tonic-gate * Disable the Nagle algorithm on TCP connections. 3650Sstevel@tonic-gate * Connections accepted from this listener will 3660Sstevel@tonic-gate * inherit the listener options. 3670Sstevel@tonic-gate */ 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate /* LINTED pointer alignment */ 3700Sstevel@tonic-gate opt = (struct opthdr *)reqbuf; 3710Sstevel@tonic-gate opt->level = IPPROTO_TCP; 3720Sstevel@tonic-gate opt->name = TCP_NODELAY; 3730Sstevel@tonic-gate opt->len = sizeof (int); 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate /* LINTED pointer alignment */ 3760Sstevel@tonic-gate *(int *)((char *)opt + sizeof (*opt)) = 1; 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate req.flags = T_NEGOTIATE; 3790Sstevel@tonic-gate req.opt.len = sizeof (*opt) + opt->len; 3800Sstevel@tonic-gate req.opt.buf = (char *)opt; 3810Sstevel@tonic-gate resp.flags = 0; 3820Sstevel@tonic-gate resp.opt.buf = reqbuf; 3830Sstevel@tonic-gate resp.opt.maxlen = sizeof (reqbuf); 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate if (t_optmgmt(fd, &req, &resp) < 0 || 3860Sstevel@tonic-gate resp.flags != T_SUCCESS) { 3870Sstevel@tonic-gate syslog(LOG_ERR, 3880Sstevel@tonic-gate "couldn't set NODELAY option for proto %s: t_errno = %d, %m", 3890Sstevel@tonic-gate nconf->nc_proto, t_errno); 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate return (fd); 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate static int 397*1676Sjpk setopt(int fd, int level, int name, int value) 3980Sstevel@tonic-gate { 3990Sstevel@tonic-gate struct t_optmgmt req, resp; 400*1676Sjpk struct { 401*1676Sjpk struct opthdr opt; 402*1676Sjpk int value; 403*1676Sjpk } reqbuf; 4040Sstevel@tonic-gate 405*1676Sjpk reqbuf.opt.level = level; 406*1676Sjpk reqbuf.opt.name = name; 407*1676Sjpk reqbuf.opt.len = sizeof (int); 4080Sstevel@tonic-gate 409*1676Sjpk reqbuf.value = value; 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate req.flags = T_NEGOTIATE; 412*1676Sjpk req.opt.len = sizeof (reqbuf); 413*1676Sjpk req.opt.buf = (char *)&reqbuf; 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate resp.flags = 0; 416*1676Sjpk resp.opt.buf = (char *)&reqbuf; 4170Sstevel@tonic-gate resp.opt.maxlen = sizeof (reqbuf); 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) { 4200Sstevel@tonic-gate t_error("t_optmgmt"); 4210Sstevel@tonic-gate return (-1); 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate return (0); 4240Sstevel@tonic-gate } 4250Sstevel@tonic-gate 426*1676Sjpk static int 427*1676Sjpk reuseaddr(int fd) 428*1676Sjpk { 429*1676Sjpk return (setopt(fd, SOL_SOCKET, SO_REUSEADDR, 1)); 430*1676Sjpk } 431*1676Sjpk 432*1676Sjpk static int 433*1676Sjpk recvucred(int fd) 434*1676Sjpk { 435*1676Sjpk return (setopt(fd, SOL_SOCKET, SO_RECVUCRED, 1)); 436*1676Sjpk } 437*1676Sjpk 438*1676Sjpk static int 439*1676Sjpk anonmlp(int fd) 440*1676Sjpk { 441*1676Sjpk return (setopt(fd, SOL_SOCKET, SO_ANON_MLP, 1)); 442*1676Sjpk } 443*1676Sjpk 4440Sstevel@tonic-gate void 4450Sstevel@tonic-gate nfslib_log_tli_error(char *tli_name, int fd, struct netconfig *nconf) 4460Sstevel@tonic-gate { 4470Sstevel@tonic-gate int error; 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate /* 4500Sstevel@tonic-gate * Save the error code across syslog(), just in case syslog() 4510Sstevel@tonic-gate * gets its own error and, therefore, overwrites errno. 4520Sstevel@tonic-gate */ 4530Sstevel@tonic-gate error = errno; 4540Sstevel@tonic-gate if (t_errno == TSYSERR) { 4550Sstevel@tonic-gate syslog(LOG_ERR, "%s(file descriptor %d/transport %s) %m", 4560Sstevel@tonic-gate tli_name, fd, nconf->nc_proto); 4570Sstevel@tonic-gate } else { 4580Sstevel@tonic-gate syslog(LOG_ERR, 4590Sstevel@tonic-gate "%s(file descriptor %d/transport %s) TLI error %d", 4600Sstevel@tonic-gate tli_name, fd, nconf->nc_proto, t_errno); 4610Sstevel@tonic-gate } 4620Sstevel@tonic-gate errno = error; 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate /* 4660Sstevel@tonic-gate * Called to set up service over a particular transport. 4670Sstevel@tonic-gate */ 4680Sstevel@tonic-gate void 4690Sstevel@tonic-gate do_one(char *provider, NETSELDECL(proto), struct protob *protobp0, 4700Sstevel@tonic-gate int (*svc)(int, struct netbuf, struct netconfig *)) 4710Sstevel@tonic-gate { 4720Sstevel@tonic-gate register int sock; 4730Sstevel@tonic-gate struct protob *protobp; 4740Sstevel@tonic-gate struct netbuf *retaddr; 4750Sstevel@tonic-gate struct netconfig *retnconf; 4760Sstevel@tonic-gate struct netbuf addrmask; 4770Sstevel@tonic-gate int vers; 4780Sstevel@tonic-gate int err; 4790Sstevel@tonic-gate int l; 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate if (provider) 4820Sstevel@tonic-gate sock = bind_to_provider(provider, protobp0->serv, &retaddr, 4830Sstevel@tonic-gate &retnconf); 4840Sstevel@tonic-gate else 4850Sstevel@tonic-gate sock = bind_to_proto(proto, protobp0->serv, &retaddr, 4860Sstevel@tonic-gate &retnconf); 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate if (sock == -1) { 4890Sstevel@tonic-gate (void) syslog(LOG_ERR, 4900Sstevel@tonic-gate "Cannot establish %s service over %s: transport setup problem.", 4910Sstevel@tonic-gate protobp0->serv, provider ? provider : proto); 4920Sstevel@tonic-gate return; 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate if (set_addrmask(sock, retnconf, &addrmask) < 0) { 4960Sstevel@tonic-gate (void) syslog(LOG_ERR, 4970Sstevel@tonic-gate "Cannot set address mask for %s", retnconf->nc_netid); 4980Sstevel@tonic-gate return; 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate /* 5020Sstevel@tonic-gate * Register all versions of the programs in the protocol block list. 5030Sstevel@tonic-gate */ 5040Sstevel@tonic-gate l = strlen(NC_UDP); 5050Sstevel@tonic-gate for (protobp = protobp0; protobp; protobp = protobp->next) { 5060Sstevel@tonic-gate for (vers = protobp->versmin; vers <= protobp->versmax; 5070Sstevel@tonic-gate vers++) { 5080Sstevel@tonic-gate if ((protobp->program == NFS_PROGRAM || 5090Sstevel@tonic-gate protobp->program == NFS_ACL_PROGRAM) && 5100Sstevel@tonic-gate vers == NFS_V4 && 5110Sstevel@tonic-gate strncasecmp(retnconf->nc_proto, NC_UDP, l) == 0) 5120Sstevel@tonic-gate continue; 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate (void) rpcb_unset(protobp->program, vers, retnconf); 5150Sstevel@tonic-gate (void) rpcb_set(protobp->program, vers, retnconf, 5160Sstevel@tonic-gate retaddr); 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate if (retnconf->nc_semantics == NC_TPI_CLTS) { 5210Sstevel@tonic-gate /* Don't drop core if supporting module(s) aren't loaded. */ 5220Sstevel@tonic-gate (void) signal(SIGSYS, SIG_IGN); 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate /* 5250Sstevel@tonic-gate * svc() doesn't block, it returns success or failure. 5260Sstevel@tonic-gate */ 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate if (svc == NULL && Mysvc4 != NULL) 5290Sstevel@tonic-gate err = (*Mysvc4)(sock, &addrmask, retnconf, 5300Sstevel@tonic-gate NFS4_SETPORT|NFS4_KRPC_START, retaddr); 5310Sstevel@tonic-gate else 5320Sstevel@tonic-gate err = (*svc)(sock, addrmask, retnconf); 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate if (err < 0) { 5350Sstevel@tonic-gate (void) syslog(LOG_ERR, 5360Sstevel@tonic-gate "Cannot establish %s service over <file desc." 5370Sstevel@tonic-gate " %d, protocol %s> : %m. Exiting", 5380Sstevel@tonic-gate protobp0->serv, sock, retnconf->nc_proto); 5390Sstevel@tonic-gate exit(1); 5400Sstevel@tonic-gate } 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate /* 5440Sstevel@tonic-gate * We successfully set up the server over this transport. 5450Sstevel@tonic-gate * Add this descriptor to the one being polled on. 5460Sstevel@tonic-gate */ 5470Sstevel@tonic-gate add_to_poll_list(sock, retnconf); 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate /* 5500Sstevel@tonic-gate * Set up the NFS service over all the available transports. 5510Sstevel@tonic-gate * Returns -1 for failure, 0 for success. 5520Sstevel@tonic-gate */ 5530Sstevel@tonic-gate int 5540Sstevel@tonic-gate do_all(struct protob *protobp, 5550Sstevel@tonic-gate int (*svc)(int, struct netbuf, struct netconfig *)) 5560Sstevel@tonic-gate { 5570Sstevel@tonic-gate struct netconfig *nconf; 5580Sstevel@tonic-gate NCONF_HANDLE *nc; 5590Sstevel@tonic-gate int l; 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate if ((nc = setnetconfig()) == (NCONF_HANDLE *)NULL) { 5620Sstevel@tonic-gate syslog(LOG_ERR, "setnetconfig failed: %m"); 5630Sstevel@tonic-gate return (-1); 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate l = strlen(NC_UDP); 5660Sstevel@tonic-gate while (nconf = getnetconfig(nc)) { 5670Sstevel@tonic-gate if ((nconf->nc_flag & NC_VISIBLE) && 5680Sstevel@tonic-gate strcmp(nconf->nc_protofmly, NC_LOOPBACK) != 0 && 5690Sstevel@tonic-gate OK_TPI_TYPE(nconf) && 5700Sstevel@tonic-gate (protobp->program != NFS4_CALLBACK || 5710Sstevel@tonic-gate strncasecmp(nconf->nc_proto, NC_UDP, l) != 0)) 5720Sstevel@tonic-gate do_one(nconf->nc_device, nconf->nc_proto, 5730Sstevel@tonic-gate protobp, svc); 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate (void) endnetconfig(nc); 5760Sstevel@tonic-gate return (0); 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate /* 5800Sstevel@tonic-gate * poll on the open transport descriptors for events and errors. 5810Sstevel@tonic-gate */ 5820Sstevel@tonic-gate void 5830Sstevel@tonic-gate poll_for_action(void) 5840Sstevel@tonic-gate { 5850Sstevel@tonic-gate int nfds; 5860Sstevel@tonic-gate int i; 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate /* 5890Sstevel@tonic-gate * Keep polling until all transports have been closed. When this 5900Sstevel@tonic-gate * happens, we return. 5910Sstevel@tonic-gate */ 5920Sstevel@tonic-gate while ((int)num_fds > 0) { 5930Sstevel@tonic-gate nfds = poll(poll_array, num_fds, INFTIM); 5940Sstevel@tonic-gate switch (nfds) { 5950Sstevel@tonic-gate case 0: 5960Sstevel@tonic-gate continue; 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate case -1: 5990Sstevel@tonic-gate /* 6000Sstevel@tonic-gate * Some errors from poll could be 6010Sstevel@tonic-gate * due to temporary conditions, and we try to 6020Sstevel@tonic-gate * be robust in the face of them. Other 6030Sstevel@tonic-gate * errors (should never happen in theory) 6040Sstevel@tonic-gate * are fatal (eg. EINVAL, EFAULT). 6050Sstevel@tonic-gate */ 6060Sstevel@tonic-gate switch (errno) { 6070Sstevel@tonic-gate case EINTR: 6080Sstevel@tonic-gate continue; 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate case EAGAIN: 6110Sstevel@tonic-gate case ENOMEM: 6120Sstevel@tonic-gate (void) sleep(10); 6130Sstevel@tonic-gate continue; 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate default: 6160Sstevel@tonic-gate (void) syslog(LOG_ERR, 6170Sstevel@tonic-gate "poll failed: %m. Exiting"); 6180Sstevel@tonic-gate exit(1); 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate default: 6210Sstevel@tonic-gate break; 6220Sstevel@tonic-gate } 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate /* 6250Sstevel@tonic-gate * Go through the poll list looking for events. 6260Sstevel@tonic-gate */ 6270Sstevel@tonic-gate for (i = 0; i < num_fds && nfds > 0; i++) { 6280Sstevel@tonic-gate if (poll_array[i].revents) { 6290Sstevel@tonic-gate nfds--; 6300Sstevel@tonic-gate /* 6310Sstevel@tonic-gate * We have a message, so try to read it. 6320Sstevel@tonic-gate * Record the error return in errno, 6330Sstevel@tonic-gate * so that syslog(LOG_ERR, "...%m") 6340Sstevel@tonic-gate * dumps the corresponding error string. 6350Sstevel@tonic-gate */ 6360Sstevel@tonic-gate if (conn_polled[i].nc.nc_semantics == 6370Sstevel@tonic-gate NC_TPI_CLTS) { 6380Sstevel@tonic-gate errno = do_poll_clts_action( 6390Sstevel@tonic-gate poll_array[i].fd, i); 6400Sstevel@tonic-gate } else { 6410Sstevel@tonic-gate errno = do_poll_cots_action( 6420Sstevel@tonic-gate poll_array[i].fd, i); 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate if (errno == 0) 6460Sstevel@tonic-gate continue; 6470Sstevel@tonic-gate /* 6480Sstevel@tonic-gate * Most returned error codes mean that there is 6490Sstevel@tonic-gate * fatal condition which we can only deal with 6500Sstevel@tonic-gate * by closing the transport. 6510Sstevel@tonic-gate */ 6520Sstevel@tonic-gate if (errno != EAGAIN && errno != ENOMEM) { 6530Sstevel@tonic-gate (void) syslog(LOG_ERR, 6540Sstevel@tonic-gate "Error (%m) reading descriptor %d/transport %s. Closing it.", 6550Sstevel@tonic-gate poll_array[i].fd, 6560Sstevel@tonic-gate conn_polled[i].nc.nc_proto); 6570Sstevel@tonic-gate (void) t_close(poll_array[i].fd); 6580Sstevel@tonic-gate remove_from_poll_list(poll_array[i].fd); 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate } else if (errno == ENOMEM) 6610Sstevel@tonic-gate (void) sleep(5); 6620Sstevel@tonic-gate } 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate (void) syslog(LOG_ERR, 6670Sstevel@tonic-gate "All transports have been closed with errors. Exiting."); 6680Sstevel@tonic-gate } 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate /* 6710Sstevel@tonic-gate * Allocate poll/transport array entries for this descriptor. 6720Sstevel@tonic-gate */ 6730Sstevel@tonic-gate static void 6740Sstevel@tonic-gate add_to_poll_list(int fd, struct netconfig *nconf) 6750Sstevel@tonic-gate { 6760Sstevel@tonic-gate static int poll_array_size = 0; 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate /* 6790Sstevel@tonic-gate * If the arrays are full, allocate new ones. 6800Sstevel@tonic-gate */ 6810Sstevel@tonic-gate if (num_fds == poll_array_size) { 6820Sstevel@tonic-gate struct pollfd *tpa; 6830Sstevel@tonic-gate struct conn_entry *tnp; 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate if (poll_array_size != 0) { 6860Sstevel@tonic-gate tpa = poll_array; 6870Sstevel@tonic-gate tnp = conn_polled; 6880Sstevel@tonic-gate } else 6890Sstevel@tonic-gate tpa = (struct pollfd *)0; 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate poll_array_size += POLL_ARRAY_INC_SIZE; 6920Sstevel@tonic-gate /* 6930Sstevel@tonic-gate * Allocate new arrays. 6940Sstevel@tonic-gate */ 6950Sstevel@tonic-gate poll_array = (struct pollfd *) 6960Sstevel@tonic-gate malloc(poll_array_size * sizeof (struct pollfd) + 256); 6970Sstevel@tonic-gate conn_polled = (struct conn_entry *) 6980Sstevel@tonic-gate malloc(poll_array_size * sizeof (struct conn_entry) + 256); 6990Sstevel@tonic-gate if (poll_array == (struct pollfd *)NULL || 7000Sstevel@tonic-gate conn_polled == (struct conn_entry *)NULL) { 7010Sstevel@tonic-gate syslog(LOG_ERR, "malloc failed for poll array"); 7020Sstevel@tonic-gate exit(1); 7030Sstevel@tonic-gate } 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate /* 7060Sstevel@tonic-gate * Copy the data of the old ones into new arrays, and 7070Sstevel@tonic-gate * free the old ones. 7080Sstevel@tonic-gate */ 7090Sstevel@tonic-gate if (tpa) { 7100Sstevel@tonic-gate (void) memcpy((void *)poll_array, (void *)tpa, 7110Sstevel@tonic-gate num_fds * sizeof (struct pollfd)); 7120Sstevel@tonic-gate (void) memcpy((void *)conn_polled, (void *)tnp, 7130Sstevel@tonic-gate num_fds * sizeof (struct conn_entry)); 7140Sstevel@tonic-gate free((void *)tpa); 7150Sstevel@tonic-gate free((void *)tnp); 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate /* 7200Sstevel@tonic-gate * Set the descriptor and event list. All possible events are 7210Sstevel@tonic-gate * polled for. 7220Sstevel@tonic-gate */ 7230Sstevel@tonic-gate poll_array[num_fds].fd = fd; 7240Sstevel@tonic-gate poll_array[num_fds].events = POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI; 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate /* 7270Sstevel@tonic-gate * Copy the transport data over too. 7280Sstevel@tonic-gate */ 7290Sstevel@tonic-gate conn_polled[num_fds].nc = *nconf; 7300Sstevel@tonic-gate conn_polled[num_fds].closing = 0; 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate /* 7330Sstevel@tonic-gate * Set the descriptor to non-blocking. Avoids a race 7340Sstevel@tonic-gate * between data arriving on the stream and then having it 7350Sstevel@tonic-gate * flushed before we can read it. 7360Sstevel@tonic-gate */ 7370Sstevel@tonic-gate if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { 7380Sstevel@tonic-gate (void) syslog(LOG_ERR, 7390Sstevel@tonic-gate "fcntl(file desc. %d/transport %s, F_SETFL, O_NONBLOCK): %m. Exiting", 7400Sstevel@tonic-gate num_fds, nconf->nc_proto); 7410Sstevel@tonic-gate exit(1); 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * Count this descriptor. 7460Sstevel@tonic-gate */ 7470Sstevel@tonic-gate ++num_fds; 7480Sstevel@tonic-gate } 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate static void 7510Sstevel@tonic-gate remove_from_poll_list(int fd) 7520Sstevel@tonic-gate { 7530Sstevel@tonic-gate int i; 7540Sstevel@tonic-gate int num_to_copy; 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate for (i = 0; i < num_fds; i++) { 7570Sstevel@tonic-gate if (poll_array[i].fd == fd) { 7580Sstevel@tonic-gate --num_fds; 7590Sstevel@tonic-gate num_to_copy = num_fds - i; 7600Sstevel@tonic-gate (void) memcpy((void *)&poll_array[i], 7610Sstevel@tonic-gate (void *)&poll_array[i+1], 7620Sstevel@tonic-gate num_to_copy * sizeof (struct pollfd)); 7630Sstevel@tonic-gate (void) memset((void *)&poll_array[num_fds], 0, 7640Sstevel@tonic-gate sizeof (struct pollfd)); 7650Sstevel@tonic-gate (void) memcpy((void *)&conn_polled[i], 7660Sstevel@tonic-gate (void *)&conn_polled[i+1], 7670Sstevel@tonic-gate num_to_copy * sizeof (struct conn_entry)); 7680Sstevel@tonic-gate (void) memset((void *)&conn_polled[num_fds], 0, 7690Sstevel@tonic-gate sizeof (struct conn_entry)); 7700Sstevel@tonic-gate return; 7710Sstevel@tonic-gate } 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate syslog(LOG_ERR, "attempt to remove nonexistent fd from poll list"); 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate /* 7780Sstevel@tonic-gate * Called to read and interpret the event on a connectionless descriptor. 7790Sstevel@tonic-gate * Returns 0 if successful, or a UNIX error code if failure. 7800Sstevel@tonic-gate */ 7810Sstevel@tonic-gate static int 7820Sstevel@tonic-gate do_poll_clts_action(int fd, int conn_index) 7830Sstevel@tonic-gate { 7840Sstevel@tonic-gate int error; 7850Sstevel@tonic-gate int ret; 7860Sstevel@tonic-gate int flags; 7870Sstevel@tonic-gate struct netconfig *nconf = &conn_polled[conn_index].nc; 7880Sstevel@tonic-gate static struct t_unitdata *unitdata = NULL; 7890Sstevel@tonic-gate static struct t_uderr *uderr = NULL; 7900Sstevel@tonic-gate static int oldfd = -1; 7910Sstevel@tonic-gate struct nd_hostservlist *host = NULL; 7920Sstevel@tonic-gate struct strbuf ctl[1], data[1]; 7930Sstevel@tonic-gate /* 7940Sstevel@tonic-gate * We just need to have some space to consume the 7950Sstevel@tonic-gate * message in the event we can't use the TLI interface to do the 7960Sstevel@tonic-gate * job. 7970Sstevel@tonic-gate * 7980Sstevel@tonic-gate * We flush the message using getmsg(). For the control part 7990Sstevel@tonic-gate * we allocate enough for any TPI header plus 32 bytes for address 8000Sstevel@tonic-gate * and options. For the data part, there is nothing magic about 8010Sstevel@tonic-gate * the size of the array, but 256 bytes is probably better than 8020Sstevel@tonic-gate * 1 byte, and we don't expect any data portion anyway. 8030Sstevel@tonic-gate * 8040Sstevel@tonic-gate * If the array sizes are too small, we handle this because getmsg() 8050Sstevel@tonic-gate * (called to consume the message) will return MOREDATA|MORECTL. 8060Sstevel@tonic-gate * Thus we just call getmsg() until it's read the message. 8070Sstevel@tonic-gate */ 8080Sstevel@tonic-gate char ctlbuf[sizeof (union T_primitives) + 32]; 8090Sstevel@tonic-gate char databuf[256]; 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate /* 8120Sstevel@tonic-gate * If this is the same descriptor as the last time 8130Sstevel@tonic-gate * do_poll_clts_action was called, we can save some 8140Sstevel@tonic-gate * de-allocation and allocation. 8150Sstevel@tonic-gate */ 8160Sstevel@tonic-gate if (oldfd != fd) { 8170Sstevel@tonic-gate oldfd = fd; 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate if (unitdata) { 8200Sstevel@tonic-gate (void) t_free((char *)unitdata, T_UNITDATA); 8210Sstevel@tonic-gate unitdata = NULL; 8220Sstevel@tonic-gate } 8230Sstevel@tonic-gate if (uderr) { 8240Sstevel@tonic-gate (void) t_free((char *)uderr, T_UDERROR); 8250Sstevel@tonic-gate uderr = NULL; 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate } 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate /* 8300Sstevel@tonic-gate * Allocate a unitdata structure for receiving the event. 8310Sstevel@tonic-gate */ 8320Sstevel@tonic-gate if (unitdata == NULL) { 8330Sstevel@tonic-gate /* LINTED pointer alignment */ 8340Sstevel@tonic-gate unitdata = (struct t_unitdata *)t_alloc(fd, T_UNITDATA, T_ALL); 8350Sstevel@tonic-gate if (unitdata == NULL) { 8360Sstevel@tonic-gate if (t_errno == TSYSERR) { 8370Sstevel@tonic-gate /* 8380Sstevel@tonic-gate * Save the error code across 8390Sstevel@tonic-gate * syslog(), just in case 8400Sstevel@tonic-gate * syslog() gets its own error 8410Sstevel@tonic-gate * and therefore overwrites errno. 8420Sstevel@tonic-gate */ 8430Sstevel@tonic-gate error = errno; 8440Sstevel@tonic-gate (void) syslog(LOG_ERR, 8450Sstevel@tonic-gate "t_alloc(file descriptor %d/transport %s, T_UNITDATA) failed: %m", 8460Sstevel@tonic-gate fd, nconf->nc_proto); 8470Sstevel@tonic-gate return (error); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate (void) syslog(LOG_ERR, 8500Sstevel@tonic-gate "t_alloc(file descriptor %d/transport %s, T_UNITDATA) failed TLI error %d", 8510Sstevel@tonic-gate fd, nconf->nc_proto, t_errno); 8520Sstevel@tonic-gate goto flush_it; 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate } 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate try_again: 8570Sstevel@tonic-gate flags = 0; 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate /* 8600Sstevel@tonic-gate * The idea is we wait for T_UNITDATA_IND's. Of course, 8610Sstevel@tonic-gate * we don't get any, because rpcmod filters them out. 8620Sstevel@tonic-gate * However, we need to call t_rcvudata() to let TLI 8630Sstevel@tonic-gate * tell us we have a T_UDERROR_IND. 8640Sstevel@tonic-gate * 8650Sstevel@tonic-gate * algorithm is: 8660Sstevel@tonic-gate * t_rcvudata(), expecting TLOOK. 8670Sstevel@tonic-gate * t_look(), expecting T_UDERR. 8680Sstevel@tonic-gate * t_rcvuderr(), expecting success (0). 8690Sstevel@tonic-gate * expand destination address into ASCII, 8700Sstevel@tonic-gate * and dump it. 8710Sstevel@tonic-gate */ 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate ret = t_rcvudata(fd, unitdata, &flags); 8740Sstevel@tonic-gate if (ret == 0 || t_errno == TBUFOVFLW) { 8750Sstevel@tonic-gate (void) syslog(LOG_WARNING, 8760Sstevel@tonic-gate "t_rcvudata(file descriptor %d/transport %s) got unexpected data, %d bytes", 8770Sstevel@tonic-gate fd, nconf->nc_proto, unitdata->udata.len); 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate /* 8800Sstevel@tonic-gate * Even though we don't expect any data, in case we do, 8810Sstevel@tonic-gate * keep reading until there is no more. 8820Sstevel@tonic-gate */ 8830Sstevel@tonic-gate if (flags & T_MORE) 8840Sstevel@tonic-gate goto try_again; 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate return (0); 8870Sstevel@tonic-gate } 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate switch (t_errno) { 8900Sstevel@tonic-gate case TNODATA: 8910Sstevel@tonic-gate return (0); 8920Sstevel@tonic-gate case TSYSERR: 8930Sstevel@tonic-gate /* 8940Sstevel@tonic-gate * System errors are returned to caller. 8950Sstevel@tonic-gate * Save the error code across 8960Sstevel@tonic-gate * syslog(), just in case 8970Sstevel@tonic-gate * syslog() gets its own error 8980Sstevel@tonic-gate * and therefore overwrites errno. 8990Sstevel@tonic-gate */ 9000Sstevel@tonic-gate error = errno; 9010Sstevel@tonic-gate (void) syslog(LOG_ERR, 9020Sstevel@tonic-gate "t_rcvudata(file descriptor %d/transport %s) %m", 9030Sstevel@tonic-gate fd, nconf->nc_proto); 9040Sstevel@tonic-gate return (error); 9050Sstevel@tonic-gate case TLOOK: 9060Sstevel@tonic-gate break; 9070Sstevel@tonic-gate default: 9080Sstevel@tonic-gate (void) syslog(LOG_ERR, 9090Sstevel@tonic-gate "t_rcvudata(file descriptor %d/transport %s) TLI error %d", 9100Sstevel@tonic-gate fd, nconf->nc_proto, t_errno); 9110Sstevel@tonic-gate goto flush_it; 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate ret = t_look(fd); 9150Sstevel@tonic-gate switch (ret) { 9160Sstevel@tonic-gate case 0: 9170Sstevel@tonic-gate return (0); 9180Sstevel@tonic-gate case -1: 9190Sstevel@tonic-gate /* 9200Sstevel@tonic-gate * System errors are returned to caller. 9210Sstevel@tonic-gate */ 9220Sstevel@tonic-gate if (t_errno == TSYSERR) { 9230Sstevel@tonic-gate /* 9240Sstevel@tonic-gate * Save the error code across 9250Sstevel@tonic-gate * syslog(), just in case 9260Sstevel@tonic-gate * syslog() gets its own error 9270Sstevel@tonic-gate * and therefore overwrites errno. 9280Sstevel@tonic-gate */ 9290Sstevel@tonic-gate error = errno; 9300Sstevel@tonic-gate (void) syslog(LOG_ERR, 9310Sstevel@tonic-gate "t_look(file descriptor %d/transport %s) %m", 9320Sstevel@tonic-gate fd, nconf->nc_proto); 9330Sstevel@tonic-gate return (error); 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate (void) syslog(LOG_ERR, 9360Sstevel@tonic-gate "t_look(file descriptor %d/transport %s) TLI error %d", 9370Sstevel@tonic-gate fd, nconf->nc_proto, t_errno); 9380Sstevel@tonic-gate goto flush_it; 9390Sstevel@tonic-gate case T_UDERR: 9400Sstevel@tonic-gate break; 9410Sstevel@tonic-gate default: 9420Sstevel@tonic-gate (void) syslog(LOG_WARNING, 9430Sstevel@tonic-gate "t_look(file descriptor %d/transport %s) returned %d not T_UDERR (%d)", 9440Sstevel@tonic-gate fd, nconf->nc_proto, ret, T_UDERR); 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate if (uderr == NULL) { 9480Sstevel@tonic-gate /* LINTED pointer alignment */ 9490Sstevel@tonic-gate uderr = (struct t_uderr *)t_alloc(fd, T_UDERROR, T_ALL); 9500Sstevel@tonic-gate if (uderr == NULL) { 9510Sstevel@tonic-gate if (t_errno == TSYSERR) { 9520Sstevel@tonic-gate /* 9530Sstevel@tonic-gate * Save the error code across 9540Sstevel@tonic-gate * syslog(), just in case 9550Sstevel@tonic-gate * syslog() gets its own error 9560Sstevel@tonic-gate * and therefore overwrites errno. 9570Sstevel@tonic-gate */ 9580Sstevel@tonic-gate error = errno; 9590Sstevel@tonic-gate (void) syslog(LOG_ERR, 9600Sstevel@tonic-gate "t_alloc(file descriptor %d/transport %s, T_UDERROR) failed: %m", 9610Sstevel@tonic-gate fd, nconf->nc_proto); 9620Sstevel@tonic-gate return (error); 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate (void) syslog(LOG_ERR, 9650Sstevel@tonic-gate "t_alloc(file descriptor %d/transport %s, T_UDERROR) failed TLI error: %d", 9660Sstevel@tonic-gate fd, nconf->nc_proto, t_errno); 9670Sstevel@tonic-gate goto flush_it; 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate } 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate ret = t_rcvuderr(fd, uderr); 9720Sstevel@tonic-gate if (ret == 0) { 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate /* 9750Sstevel@tonic-gate * Save the datagram error in errno, so that the 9760Sstevel@tonic-gate * %m argument to syslog picks up the error string. 9770Sstevel@tonic-gate */ 9780Sstevel@tonic-gate errno = uderr->error; 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate /* 9810Sstevel@tonic-gate * Log the datagram error, then log the host that 9820Sstevel@tonic-gate * probably triggerred. Cannot log both in the 9830Sstevel@tonic-gate * same transaction because of packet size limitations 9840Sstevel@tonic-gate * in /dev/log. 9850Sstevel@tonic-gate */ 9860Sstevel@tonic-gate (void) syslog((errno == ECONNREFUSED) ? LOG_DEBUG : LOG_WARNING, 9870Sstevel@tonic-gate "NFS response over <file descriptor %d/transport %s> generated error: %m", 9880Sstevel@tonic-gate fd, nconf->nc_proto); 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate /* 9910Sstevel@tonic-gate * Try to map the client's address back to a 9920Sstevel@tonic-gate * name. 9930Sstevel@tonic-gate */ 9940Sstevel@tonic-gate ret = netdir_getbyaddr(nconf, &host, &uderr->addr); 9950Sstevel@tonic-gate if (ret != -1 && host && host->h_cnt > 0 && 9960Sstevel@tonic-gate host->h_hostservs) { 9970Sstevel@tonic-gate (void) syslog((errno == ECONNREFUSED) ? LOG_DEBUG : LOG_WARNING, 9980Sstevel@tonic-gate "Bad NFS response was sent to client with host name: %s; service port: %s", 9990Sstevel@tonic-gate host->h_hostservs->h_host, 10000Sstevel@tonic-gate host->h_hostservs->h_serv); 10010Sstevel@tonic-gate } else { 10020Sstevel@tonic-gate int i, j; 10030Sstevel@tonic-gate char *buf; 10040Sstevel@tonic-gate char *hex = "0123456789abcdef"; 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate /* 10070Sstevel@tonic-gate * Mapping failed, print the whole thing 10080Sstevel@tonic-gate * in ASCII hex. 10090Sstevel@tonic-gate */ 10100Sstevel@tonic-gate buf = (char *)malloc(uderr->addr.len * 2 + 1); 10110Sstevel@tonic-gate for (i = 0, j = 0; i < uderr->addr.len; i++, j += 2) { 10120Sstevel@tonic-gate buf[j] = hex[((uderr->addr.buf[i]) >> 4) & 0xf]; 10130Sstevel@tonic-gate buf[j+1] = hex[uderr->addr.buf[i] & 0xf]; 10140Sstevel@tonic-gate } 10150Sstevel@tonic-gate buf[j] = '\0'; 10160Sstevel@tonic-gate (void) syslog((errno == ECONNREFUSED) ? LOG_DEBUG : LOG_WARNING, 10170Sstevel@tonic-gate "Bad NFS response was sent to client with transport address: 0x%s", 10180Sstevel@tonic-gate buf); 10190Sstevel@tonic-gate free((void *)buf); 10200Sstevel@tonic-gate } 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate if (ret == 0 && host != NULL) 10230Sstevel@tonic-gate netdir_free((void *)host, ND_HOSTSERVLIST); 10240Sstevel@tonic-gate return (0); 10250Sstevel@tonic-gate } 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate switch (t_errno) { 10280Sstevel@tonic-gate case TNOUDERR: 10290Sstevel@tonic-gate goto flush_it; 10300Sstevel@tonic-gate case TSYSERR: 10310Sstevel@tonic-gate /* 10320Sstevel@tonic-gate * System errors are returned to caller. 10330Sstevel@tonic-gate * Save the error code across 10340Sstevel@tonic-gate * syslog(), just in case 10350Sstevel@tonic-gate * syslog() gets its own error 10360Sstevel@tonic-gate * and therefore overwrites errno. 10370Sstevel@tonic-gate */ 10380Sstevel@tonic-gate error = errno; 10390Sstevel@tonic-gate (void) syslog(LOG_ERR, 10400Sstevel@tonic-gate "t_rcvuderr(file descriptor %d/transport %s) %m", 10410Sstevel@tonic-gate fd, nconf->nc_proto); 10420Sstevel@tonic-gate return (error); 10430Sstevel@tonic-gate default: 10440Sstevel@tonic-gate (void) syslog(LOG_ERR, 10450Sstevel@tonic-gate "t_rcvuderr(file descriptor %d/transport %s) TLI error %d", 10460Sstevel@tonic-gate fd, nconf->nc_proto, t_errno); 10470Sstevel@tonic-gate goto flush_it; 10480Sstevel@tonic-gate } 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate flush_it: 10510Sstevel@tonic-gate /* 10520Sstevel@tonic-gate * If we get here, then we could not cope with whatever message 10530Sstevel@tonic-gate * we attempted to read, so flush it. If we did read a message, 10540Sstevel@tonic-gate * and one isn't present, that is all right, because fd is in 10550Sstevel@tonic-gate * nonblocking mode. 10560Sstevel@tonic-gate */ 10570Sstevel@tonic-gate (void) syslog(LOG_ERR, 10580Sstevel@tonic-gate "Flushing one input message from <file descriptor %d/transport %s>", 10590Sstevel@tonic-gate fd, nconf->nc_proto); 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate /* 10620Sstevel@tonic-gate * Read and discard the message. Do this this until there is 10630Sstevel@tonic-gate * no more control/data in the message or until we get an error. 10640Sstevel@tonic-gate */ 10650Sstevel@tonic-gate do { 10660Sstevel@tonic-gate ctl->maxlen = sizeof (ctlbuf); 10670Sstevel@tonic-gate ctl->buf = ctlbuf; 10680Sstevel@tonic-gate data->maxlen = sizeof (databuf); 10690Sstevel@tonic-gate data->buf = databuf; 10700Sstevel@tonic-gate flags = 0; 10710Sstevel@tonic-gate ret = getmsg(fd, ctl, data, &flags); 10720Sstevel@tonic-gate if (ret == -1) 10730Sstevel@tonic-gate return (errno); 10740Sstevel@tonic-gate } while (ret != 0); 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate return (0); 10770Sstevel@tonic-gate } 10780Sstevel@tonic-gate 10790Sstevel@tonic-gate static void 10800Sstevel@tonic-gate conn_close_oldest(void) 10810Sstevel@tonic-gate { 10820Sstevel@tonic-gate int fd; 10830Sstevel@tonic-gate int i1; 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate /* 10860Sstevel@tonic-gate * Find the oldest connection that is not already in the 10870Sstevel@tonic-gate * process of shutting down. 10880Sstevel@tonic-gate */ 10890Sstevel@tonic-gate for (i1 = end_listen_fds; /* no conditional expression */; i1++) { 10900Sstevel@tonic-gate if (i1 >= num_fds) 10910Sstevel@tonic-gate return; 10920Sstevel@tonic-gate if (conn_polled[i1].closing == 0) 10930Sstevel@tonic-gate break; 10940Sstevel@tonic-gate } 10950Sstevel@tonic-gate #ifdef DEBUG 10960Sstevel@tonic-gate printf("too many connections (%d), releasing oldest (%d)\n", 10970Sstevel@tonic-gate num_conns, poll_array[i1].fd); 10980Sstevel@tonic-gate #else 10990Sstevel@tonic-gate syslog(LOG_WARNING, "too many connections (%d), releasing oldest (%d)", 11000Sstevel@tonic-gate num_conns, poll_array[i1].fd); 11010Sstevel@tonic-gate #endif 11020Sstevel@tonic-gate fd = poll_array[i1].fd; 11030Sstevel@tonic-gate if (conn_polled[i1].nc.nc_semantics == NC_TPI_COTS) { 11040Sstevel@tonic-gate /* 11050Sstevel@tonic-gate * For politeness, send a T_DISCON_REQ to the transport 11060Sstevel@tonic-gate * provider. We close the stream anyway. 11070Sstevel@tonic-gate */ 11080Sstevel@tonic-gate (void) t_snddis(fd, (struct t_call *)0); 11090Sstevel@tonic-gate num_conns--; 11100Sstevel@tonic-gate remove_from_poll_list(fd); 11110Sstevel@tonic-gate (void) t_close(fd); 11120Sstevel@tonic-gate } else { 11130Sstevel@tonic-gate /* 11140Sstevel@tonic-gate * For orderly release, we do not close the stream 11150Sstevel@tonic-gate * until the T_ORDREL_IND arrives to complete 11160Sstevel@tonic-gate * the handshake. 11170Sstevel@tonic-gate */ 11180Sstevel@tonic-gate if (t_sndrel(fd) == 0) 11190Sstevel@tonic-gate conn_polled[i1].closing = 1; 11200Sstevel@tonic-gate } 11210Sstevel@tonic-gate } 11220Sstevel@tonic-gate 11230Sstevel@tonic-gate static boolean_t 11240Sstevel@tonic-gate conn_get(int fd, struct netconfig *nconf, struct conn_ind **connp) 11250Sstevel@tonic-gate { 11260Sstevel@tonic-gate struct conn_ind *conn; 11270Sstevel@tonic-gate struct conn_ind *next_conn; 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate conn = (struct conn_ind *)malloc(sizeof (*conn)); 11300Sstevel@tonic-gate if (conn == NULL) { 11310Sstevel@tonic-gate syslog(LOG_ERR, "malloc for listen indication failed"); 11320Sstevel@tonic-gate return (FALSE); 11330Sstevel@tonic-gate } 11340Sstevel@tonic-gate 11350Sstevel@tonic-gate /* LINTED pointer alignment */ 11360Sstevel@tonic-gate conn->conn_call = (struct t_call *)t_alloc(fd, T_CALL, T_ALL); 11370Sstevel@tonic-gate if (conn->conn_call == NULL) { 11380Sstevel@tonic-gate free((char *)conn); 11390Sstevel@tonic-gate nfslib_log_tli_error("t_alloc", fd, nconf); 11400Sstevel@tonic-gate return (FALSE); 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate if (t_listen(fd, conn->conn_call) == -1) { 11440Sstevel@tonic-gate nfslib_log_tli_error("t_listen", fd, nconf); 11450Sstevel@tonic-gate (void) t_free((char *)conn->conn_call, T_CALL); 11460Sstevel@tonic-gate free((char *)conn); 11470Sstevel@tonic-gate return (FALSE); 11480Sstevel@tonic-gate } 11490Sstevel@tonic-gate 11500Sstevel@tonic-gate if (conn->conn_call->udata.len > 0) { 11510Sstevel@tonic-gate syslog(LOG_WARNING, 11520Sstevel@tonic-gate "rejecting inbound connection(%s) with %d bytes of connect data", 11530Sstevel@tonic-gate nconf->nc_proto, conn->conn_call->udata.len); 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate conn->conn_call->udata.len = 0; 11560Sstevel@tonic-gate (void) t_snddis(fd, conn->conn_call); 11570Sstevel@tonic-gate (void) t_free((char *)conn->conn_call, T_CALL); 11580Sstevel@tonic-gate free((char *)conn); 11590Sstevel@tonic-gate return (FALSE); 11600Sstevel@tonic-gate } 11610Sstevel@tonic-gate 11620Sstevel@tonic-gate if ((next_conn = *connp) != NULL) { 11630Sstevel@tonic-gate next_conn->conn_prev->conn_next = conn; 11640Sstevel@tonic-gate conn->conn_next = next_conn; 11650Sstevel@tonic-gate conn->conn_prev = next_conn->conn_prev; 11660Sstevel@tonic-gate next_conn->conn_prev = conn; 11670Sstevel@tonic-gate } else { 11680Sstevel@tonic-gate conn->conn_next = conn; 11690Sstevel@tonic-gate conn->conn_prev = conn; 11700Sstevel@tonic-gate *connp = conn; 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate return (TRUE); 11730Sstevel@tonic-gate } 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate static int 11760Sstevel@tonic-gate discon_get(int fd, struct netconfig *nconf, struct conn_ind **connp) 11770Sstevel@tonic-gate { 11780Sstevel@tonic-gate struct conn_ind *conn; 11790Sstevel@tonic-gate struct t_discon discon; 11800Sstevel@tonic-gate 11810Sstevel@tonic-gate discon.udata.buf = (char *)0; 11820Sstevel@tonic-gate discon.udata.maxlen = 0; 11830Sstevel@tonic-gate if (t_rcvdis(fd, &discon) == -1) { 11840Sstevel@tonic-gate nfslib_log_tli_error("t_rcvdis", fd, nconf); 11850Sstevel@tonic-gate return (-1); 11860Sstevel@tonic-gate } 11870Sstevel@tonic-gate 11880Sstevel@tonic-gate conn = *connp; 11890Sstevel@tonic-gate if (conn == NULL) 11900Sstevel@tonic-gate return (0); 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate do { 11930Sstevel@tonic-gate if (conn->conn_call->sequence == discon.sequence) { 11940Sstevel@tonic-gate if (conn->conn_next == conn) 11950Sstevel@tonic-gate *connp = (struct conn_ind *)0; 11960Sstevel@tonic-gate else { 11970Sstevel@tonic-gate if (conn == *connp) { 11980Sstevel@tonic-gate *connp = conn->conn_next; 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate conn->conn_next->conn_prev = conn->conn_prev; 12010Sstevel@tonic-gate conn->conn_prev->conn_next = conn->conn_next; 12020Sstevel@tonic-gate } 12030Sstevel@tonic-gate free((char *)conn); 12040Sstevel@tonic-gate break; 12050Sstevel@tonic-gate } 12060Sstevel@tonic-gate conn = conn->conn_next; 12070Sstevel@tonic-gate } while (conn != *connp); 12080Sstevel@tonic-gate 12090Sstevel@tonic-gate return (0); 12100Sstevel@tonic-gate } 12110Sstevel@tonic-gate 12120Sstevel@tonic-gate static void 12130Sstevel@tonic-gate cots_listen_event(int fd, int conn_index) 12140Sstevel@tonic-gate { 12150Sstevel@tonic-gate struct t_call *call; 12160Sstevel@tonic-gate struct conn_ind *conn; 12170Sstevel@tonic-gate struct conn_ind *conn_head; 12180Sstevel@tonic-gate int event; 12190Sstevel@tonic-gate struct netconfig *nconf = &conn_polled[conn_index].nc; 12200Sstevel@tonic-gate int new_fd; 12210Sstevel@tonic-gate struct netbuf addrmask; 12220Sstevel@tonic-gate int ret = 0; 12230Sstevel@tonic-gate char *clnt; 12240Sstevel@tonic-gate char *clnt_uaddr = NULL; 12250Sstevel@tonic-gate struct nd_hostservlist *clnt_serv = NULL; 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate conn_head = (struct conn_ind *)0; 12280Sstevel@tonic-gate (void) conn_get(fd, nconf, &conn_head); 12290Sstevel@tonic-gate 12300Sstevel@tonic-gate while ((conn = conn_head) != NULL) { 12310Sstevel@tonic-gate conn_head = conn->conn_next; 12320Sstevel@tonic-gate if (conn_head == conn) 12330Sstevel@tonic-gate conn_head = (struct conn_ind *)0; 12340Sstevel@tonic-gate else { 12350Sstevel@tonic-gate conn_head->conn_prev = conn->conn_prev; 12360Sstevel@tonic-gate conn->conn_prev->conn_next = conn_head; 12370Sstevel@tonic-gate } 12380Sstevel@tonic-gate call = conn->conn_call; 12390Sstevel@tonic-gate free((char *)conn); 12400Sstevel@tonic-gate 12410Sstevel@tonic-gate /* 12420Sstevel@tonic-gate * If we have already accepted the maximum number of 12430Sstevel@tonic-gate * connections allowed on the command line, then drop 12440Sstevel@tonic-gate * the oldest connection (for any protocol) before 12450Sstevel@tonic-gate * accepting the new connection. Unless explicitly 12460Sstevel@tonic-gate * set on the command line, max_conns_allowed is -1. 12470Sstevel@tonic-gate */ 12480Sstevel@tonic-gate if (max_conns_allowed != -1 && num_conns >= max_conns_allowed) 12490Sstevel@tonic-gate conn_close_oldest(); 12500Sstevel@tonic-gate 12510Sstevel@tonic-gate /* 12520Sstevel@tonic-gate * Create a new transport endpoint for the same proto as 12530Sstevel@tonic-gate * the listener. 12540Sstevel@tonic-gate */ 12550Sstevel@tonic-gate new_fd = nfslib_transport_open(nconf); 12560Sstevel@tonic-gate if (new_fd == -1) { 12570Sstevel@tonic-gate call->udata.len = 0; 12580Sstevel@tonic-gate (void) t_snddis(fd, call); 12590Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 12600Sstevel@tonic-gate syslog(LOG_ERR, "Cannot establish transport over %s", 12610Sstevel@tonic-gate nconf->nc_device); 12620Sstevel@tonic-gate continue; 12630Sstevel@tonic-gate } 12640Sstevel@tonic-gate 12650Sstevel@tonic-gate /* Bind to a generic address/port for the accepting stream. */ 12660Sstevel@tonic-gate if (t_bind(new_fd, (struct t_bind *)NULL, 12670Sstevel@tonic-gate (struct t_bind *)NULL) == -1) { 12680Sstevel@tonic-gate nfslib_log_tli_error("t_bind", new_fd, nconf); 12690Sstevel@tonic-gate call->udata.len = 0; 12700Sstevel@tonic-gate (void) t_snddis(fd, call); 12710Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 12720Sstevel@tonic-gate (void) t_close(new_fd); 12730Sstevel@tonic-gate continue; 12740Sstevel@tonic-gate } 12750Sstevel@tonic-gate 12760Sstevel@tonic-gate while (t_accept(fd, new_fd, call) == -1) { 12770Sstevel@tonic-gate if (t_errno != TLOOK) { 12780Sstevel@tonic-gate #ifdef DEBUG 12790Sstevel@tonic-gate nfslib_log_tli_error("t_accept", fd, nconf); 12800Sstevel@tonic-gate #endif 12810Sstevel@tonic-gate call->udata.len = 0; 12820Sstevel@tonic-gate (void) t_snddis(fd, call); 12830Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 12840Sstevel@tonic-gate (void) t_close(new_fd); 12850Sstevel@tonic-gate goto do_next_conn; 12860Sstevel@tonic-gate } 12870Sstevel@tonic-gate while (event = t_look(fd)) { 12880Sstevel@tonic-gate switch (event) { 12890Sstevel@tonic-gate case T_LISTEN: 12900Sstevel@tonic-gate #ifdef DEBUG 12910Sstevel@tonic-gate printf( 12920Sstevel@tonic-gate "cots_listen_event(%s): T_LISTEN during accept processing\n", nconf->nc_proto); 12930Sstevel@tonic-gate #endif 12940Sstevel@tonic-gate (void) conn_get(fd, nconf, &conn_head); 12950Sstevel@tonic-gate continue; 12960Sstevel@tonic-gate case T_DISCONNECT: 12970Sstevel@tonic-gate #ifdef DEBUG 12980Sstevel@tonic-gate printf( 12990Sstevel@tonic-gate "cots_listen_event(%s): T_DISCONNECT during accept processing\n", 13000Sstevel@tonic-gate nconf->nc_proto); 13010Sstevel@tonic-gate #endif 13020Sstevel@tonic-gate (void) discon_get(fd, nconf, 13030Sstevel@tonic-gate &conn_head); 13040Sstevel@tonic-gate continue; 13050Sstevel@tonic-gate default: 13060Sstevel@tonic-gate syslog(LOG_ERR, 13070Sstevel@tonic-gate "unexpected event 0x%x during accept processing (%s)", 13080Sstevel@tonic-gate event, nconf->nc_proto); 13090Sstevel@tonic-gate call->udata.len = 0; 13100Sstevel@tonic-gate (void) t_snddis(fd, call); 13110Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 13120Sstevel@tonic-gate (void) t_close(new_fd); 13130Sstevel@tonic-gate goto do_next_conn; 13140Sstevel@tonic-gate } 13150Sstevel@tonic-gate } 13160Sstevel@tonic-gate } 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate if (set_addrmask(new_fd, nconf, &addrmask) < 0) { 13190Sstevel@tonic-gate (void) syslog(LOG_ERR, 13200Sstevel@tonic-gate "Cannot set address mask for %s", 13210Sstevel@tonic-gate nconf->nc_netid); 13220Sstevel@tonic-gate return; 13230Sstevel@tonic-gate } 13240Sstevel@tonic-gate 13250Sstevel@tonic-gate /* Tell KRPC about the new stream. */ 13260Sstevel@tonic-gate if (Mysvc4 != NULL) 13270Sstevel@tonic-gate ret = (*Mysvc4)(new_fd, &addrmask, nconf, 13280Sstevel@tonic-gate NFS4_KRPC_START, &call->addr); 13290Sstevel@tonic-gate else 13300Sstevel@tonic-gate ret = (*Mysvc)(new_fd, addrmask, nconf); 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate if (ret < 0) { 13330Sstevel@tonic-gate if (errno != ENOTCONN) { 13340Sstevel@tonic-gate syslog(LOG_ERR, 13350Sstevel@tonic-gate "unable to register new connection: %m"); 13360Sstevel@tonic-gate } else { 13370Sstevel@tonic-gate /* 13380Sstevel@tonic-gate * This is the only error that could be 13390Sstevel@tonic-gate * caused by the client, so who was it? 13400Sstevel@tonic-gate */ 13410Sstevel@tonic-gate if (netdir_getbyaddr(nconf, &clnt_serv, 13420Sstevel@tonic-gate &(call->addr)) == ND_OK && 13430Sstevel@tonic-gate clnt_serv->h_cnt > 0) 13440Sstevel@tonic-gate clnt = clnt_serv->h_hostservs->h_host; 13450Sstevel@tonic-gate else 13460Sstevel@tonic-gate clnt = clnt_uaddr = taddr2uaddr(nconf, 13470Sstevel@tonic-gate &(call->addr)); 13480Sstevel@tonic-gate /* 13490Sstevel@tonic-gate * If we don't know who the client was, 13500Sstevel@tonic-gate * remain silent. 13510Sstevel@tonic-gate */ 13520Sstevel@tonic-gate if (clnt) 13530Sstevel@tonic-gate syslog(LOG_ERR, 13540Sstevel@tonic-gate "unable to register new connection: client %s has dropped connection", clnt); 13550Sstevel@tonic-gate if (clnt_serv) 13560Sstevel@tonic-gate netdir_free(clnt_serv, ND_HOSTSERVLIST); 13570Sstevel@tonic-gate if (clnt_uaddr) 13580Sstevel@tonic-gate free(clnt_uaddr); 13590Sstevel@tonic-gate } 13600Sstevel@tonic-gate free(addrmask.buf); 13610Sstevel@tonic-gate (void) t_snddis(new_fd, (struct t_call *)0); 13620Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 13630Sstevel@tonic-gate (void) t_close(new_fd); 13640Sstevel@tonic-gate goto do_next_conn; 13650Sstevel@tonic-gate } 13660Sstevel@tonic-gate 13670Sstevel@tonic-gate free(addrmask.buf); 13680Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 13690Sstevel@tonic-gate 13700Sstevel@tonic-gate /* 13710Sstevel@tonic-gate * Poll on the new descriptor so that we get disconnect 13720Sstevel@tonic-gate * and orderly release indications. 13730Sstevel@tonic-gate */ 13740Sstevel@tonic-gate num_conns++; 13750Sstevel@tonic-gate add_to_poll_list(new_fd, nconf); 13760Sstevel@tonic-gate 13770Sstevel@tonic-gate /* Reset nconf in case it has been moved. */ 13780Sstevel@tonic-gate nconf = &conn_polled[conn_index].nc; 13790Sstevel@tonic-gate do_next_conn:; 13800Sstevel@tonic-gate } 13810Sstevel@tonic-gate } 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate static int 13840Sstevel@tonic-gate do_poll_cots_action(int fd, int conn_index) 13850Sstevel@tonic-gate { 13860Sstevel@tonic-gate char buf[256]; 13870Sstevel@tonic-gate int event; 13880Sstevel@tonic-gate int i1; 13890Sstevel@tonic-gate int flags; 13900Sstevel@tonic-gate struct conn_entry *connent = &conn_polled[conn_index]; 13910Sstevel@tonic-gate struct netconfig *nconf = &(connent->nc); 13920Sstevel@tonic-gate const char *errorstr; 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate while (event = t_look(fd)) { 13950Sstevel@tonic-gate switch (event) { 13960Sstevel@tonic-gate case T_LISTEN: 13970Sstevel@tonic-gate #ifdef DEBUG 13980Sstevel@tonic-gate printf("do_poll_cots_action(%s,%d): T_LISTEN event\n", nconf->nc_proto, fd); 13990Sstevel@tonic-gate #endif 14000Sstevel@tonic-gate cots_listen_event(fd, conn_index); 14010Sstevel@tonic-gate break; 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate case T_DATA: 14040Sstevel@tonic-gate #ifdef DEBUG 14050Sstevel@tonic-gate printf("do_poll_cots_action(%d,%s): T_DATA event\n", fd, nconf->nc_proto); 14060Sstevel@tonic-gate #endif 14070Sstevel@tonic-gate /* 14080Sstevel@tonic-gate * Receive a private notification from CONS rpcmod. 14090Sstevel@tonic-gate */ 14100Sstevel@tonic-gate i1 = t_rcv(fd, buf, sizeof (buf), &flags); 14110Sstevel@tonic-gate if (i1 == -1) { 14120Sstevel@tonic-gate syslog(LOG_ERR, "t_rcv failed"); 14130Sstevel@tonic-gate break; 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate if (i1 < sizeof (int)) 14160Sstevel@tonic-gate break; 14170Sstevel@tonic-gate i1 = BE32_TO_U32(buf); 14180Sstevel@tonic-gate if (i1 == 1 || i1 == 2) { 14190Sstevel@tonic-gate /* 14200Sstevel@tonic-gate * This connection has been idle for too long, 14210Sstevel@tonic-gate * so release it as politely as we can. If we 14220Sstevel@tonic-gate * have already initiated an orderly release 14230Sstevel@tonic-gate * and we get notified that the stream is 14240Sstevel@tonic-gate * still idle, pull the plug. This prevents 14250Sstevel@tonic-gate * hung connections from continuing to consume 14260Sstevel@tonic-gate * resources. 14270Sstevel@tonic-gate */ 14280Sstevel@tonic-gate #ifdef DEBUG 14290Sstevel@tonic-gate printf("do_poll_cots_action(%s,%d): ", nconf->nc_proto, fd); 14300Sstevel@tonic-gate printf("initiating orderly release of idle connection\n"); 14310Sstevel@tonic-gate #endif 14320Sstevel@tonic-gate if (nconf->nc_semantics == NC_TPI_COTS || 14330Sstevel@tonic-gate connent->closing != 0) { 14340Sstevel@tonic-gate (void) t_snddis(fd, (struct t_call *)0); 14350Sstevel@tonic-gate goto fdclose; 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate /* 14380Sstevel@tonic-gate * For NC_TPI_COTS_ORD, the stream is closed 14390Sstevel@tonic-gate * and removed from the poll list when the 14400Sstevel@tonic-gate * T_ORDREL is received from the provider. We 14410Sstevel@tonic-gate * don't wait for it here because it may take 14420Sstevel@tonic-gate * a while for the transport to shut down. 14430Sstevel@tonic-gate */ 14440Sstevel@tonic-gate if (t_sndrel(fd) == -1) { 14450Sstevel@tonic-gate syslog(LOG_ERR, 14460Sstevel@tonic-gate "unable to send orderly release %m"); 14470Sstevel@tonic-gate } 14480Sstevel@tonic-gate connent->closing = 1; 14490Sstevel@tonic-gate } else 14500Sstevel@tonic-gate syslog(LOG_ERR, 14510Sstevel@tonic-gate "unexpected event from CONS rpcmod %d", i1); 14520Sstevel@tonic-gate break; 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate case T_ORDREL: 14550Sstevel@tonic-gate #ifdef DEBUG 14560Sstevel@tonic-gate printf("do_poll_cots_action(%s,%d): T_ORDREL event\n", nconf->nc_proto, fd); 14570Sstevel@tonic-gate #endif 14580Sstevel@tonic-gate /* Perform an orderly release. */ 14590Sstevel@tonic-gate if (t_rcvrel(fd) == 0) { 14600Sstevel@tonic-gate /* T_ORDREL on listen fd's should be ignored */ 14610Sstevel@tonic-gate if (!is_listen_fd_index(conn_index)) { 14620Sstevel@tonic-gate (void) t_sndrel(fd); 14630Sstevel@tonic-gate goto fdclose; 14640Sstevel@tonic-gate } 14650Sstevel@tonic-gate break; 14660Sstevel@tonic-gate 14670Sstevel@tonic-gate } else if (t_errno == TLOOK) { 14680Sstevel@tonic-gate break; 14690Sstevel@tonic-gate } else { 14700Sstevel@tonic-gate nfslib_log_tli_error("t_rcvrel", fd, nconf); 14710Sstevel@tonic-gate 14720Sstevel@tonic-gate /* 14730Sstevel@tonic-gate * check to make sure we do not close 14740Sstevel@tonic-gate * listen fd 14750Sstevel@tonic-gate */ 14760Sstevel@tonic-gate if (is_listen_fd_index(conn_index)) 14770Sstevel@tonic-gate break; 14780Sstevel@tonic-gate else 14790Sstevel@tonic-gate goto fdclose; 14800Sstevel@tonic-gate } 14810Sstevel@tonic-gate 14820Sstevel@tonic-gate case T_DISCONNECT: 14830Sstevel@tonic-gate #ifdef DEBUG 14840Sstevel@tonic-gate printf("do_poll_cots_action(%s,%d): T_DISCONNECT event\n", nconf->nc_proto, fd); 14850Sstevel@tonic-gate #endif 14860Sstevel@tonic-gate if (t_rcvdis(fd, (struct t_discon *)NULL) == -1) 14870Sstevel@tonic-gate nfslib_log_tli_error("t_rcvdis", fd, nconf); 14880Sstevel@tonic-gate 14890Sstevel@tonic-gate /* 14900Sstevel@tonic-gate * T_DISCONNECT on listen fd's should be ignored. 14910Sstevel@tonic-gate */ 14920Sstevel@tonic-gate if (is_listen_fd_index(conn_index)) 14930Sstevel@tonic-gate break; 14940Sstevel@tonic-gate else 14950Sstevel@tonic-gate goto fdclose; 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate case T_ERROR: 14980Sstevel@tonic-gate default: 14990Sstevel@tonic-gate if (event == T_ERROR || t_errno == TSYSERR) { 15000Sstevel@tonic-gate if ((errorstr = strerror(errno)) == NULL) { 15010Sstevel@tonic-gate (void) sprintf(buf, "Unknown error num %d", 15020Sstevel@tonic-gate errno); 15030Sstevel@tonic-gate errorstr = (const char *) buf; 15040Sstevel@tonic-gate } 15050Sstevel@tonic-gate } else if (event == -1) 15060Sstevel@tonic-gate errorstr = t_strerror(t_errno); 15070Sstevel@tonic-gate else 15080Sstevel@tonic-gate errorstr = ""; 15090Sstevel@tonic-gate syslog(LOG_ERR, 15100Sstevel@tonic-gate "unexpected TLI event (0x%x) on " 15110Sstevel@tonic-gate "connection-oriented transport(%s,%d):%s", 15120Sstevel@tonic-gate event, nconf->nc_proto, fd, errorstr); 15130Sstevel@tonic-gate fdclose: 15140Sstevel@tonic-gate num_conns--; 15150Sstevel@tonic-gate remove_from_poll_list(fd); 15160Sstevel@tonic-gate (void) t_close(fd); 15170Sstevel@tonic-gate return (0); 15180Sstevel@tonic-gate } 15190Sstevel@tonic-gate } 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate return (0); 15220Sstevel@tonic-gate } 15230Sstevel@tonic-gate 15240Sstevel@tonic-gate static char * 15250Sstevel@tonic-gate serv_name_to_port_name(char *name) 15260Sstevel@tonic-gate { 15270Sstevel@tonic-gate /* 15280Sstevel@tonic-gate * Map service names (used primarily in logging) to 15290Sstevel@tonic-gate * RPC port names (used by netdir_*() routines). 15300Sstevel@tonic-gate */ 15310Sstevel@tonic-gate if (strcmp(name, "NFS") == 0) { 15320Sstevel@tonic-gate return ("nfs"); 15330Sstevel@tonic-gate } else if (strcmp(name, "NLM") == 0) { 15340Sstevel@tonic-gate return ("lockd"); 15350Sstevel@tonic-gate } else if (strcmp(name, "NFS4_CALLBACK") == 0) { 15360Sstevel@tonic-gate return ("nfs4_callback"); 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate return ("unrecognized"); 15400Sstevel@tonic-gate } 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate static int 15430Sstevel@tonic-gate bind_to_provider(char *provider, char *serv, struct netbuf **addr, 15440Sstevel@tonic-gate struct netconfig **retnconf) 15450Sstevel@tonic-gate { 15460Sstevel@tonic-gate struct netconfig *nconf; 15470Sstevel@tonic-gate NCONF_HANDLE *nc; 15480Sstevel@tonic-gate struct nd_hostserv hs; 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate hs.h_host = HOST_SELF; 15510Sstevel@tonic-gate hs.h_serv = serv_name_to_port_name(serv); 15520Sstevel@tonic-gate 15530Sstevel@tonic-gate if ((nc = setnetconfig()) == (NCONF_HANDLE *)NULL) { 15540Sstevel@tonic-gate syslog(LOG_ERR, "setnetconfig failed: %m"); 15550Sstevel@tonic-gate return (-1); 15560Sstevel@tonic-gate } 15570Sstevel@tonic-gate while (nconf = getnetconfig(nc)) { 15580Sstevel@tonic-gate if (OK_TPI_TYPE(nconf) && 15590Sstevel@tonic-gate strcmp(nconf->nc_device, provider) == 0) { 15600Sstevel@tonic-gate *retnconf = nconf; 15610Sstevel@tonic-gate return (nfslib_bindit(nconf, addr, &hs, 15620Sstevel@tonic-gate listen_backlog)); 15630Sstevel@tonic-gate } 15640Sstevel@tonic-gate } 15650Sstevel@tonic-gate (void) endnetconfig(nc); 15660Sstevel@tonic-gate 15670Sstevel@tonic-gate syslog(LOG_ERR, "couldn't find netconfig entry for provider %s", 15680Sstevel@tonic-gate provider); 15690Sstevel@tonic-gate return (-1); 15700Sstevel@tonic-gate } 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate static int 15730Sstevel@tonic-gate bind_to_proto(NETSELDECL(proto), char *serv, struct netbuf **addr, 15740Sstevel@tonic-gate struct netconfig **retnconf) 15750Sstevel@tonic-gate { 15760Sstevel@tonic-gate struct netconfig *nconf; 15770Sstevel@tonic-gate NCONF_HANDLE *nc = NULL; 15780Sstevel@tonic-gate struct nd_hostserv hs; 15790Sstevel@tonic-gate 15800Sstevel@tonic-gate hs.h_host = HOST_SELF; 15810Sstevel@tonic-gate hs.h_serv = serv_name_to_port_name(serv); 15820Sstevel@tonic-gate 15830Sstevel@tonic-gate if ((nc = setnetconfig()) == (NCONF_HANDLE *)NULL) { 15840Sstevel@tonic-gate syslog(LOG_ERR, "setnetconfig failed: %m"); 15850Sstevel@tonic-gate return (-1); 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate while (nconf = getnetconfig(nc)) { 15880Sstevel@tonic-gate if (OK_TPI_TYPE(nconf) && NETSELEQ(nconf->nc_proto, proto)) { 15890Sstevel@tonic-gate *retnconf = nconf; 15900Sstevel@tonic-gate return (nfslib_bindit(nconf, addr, &hs, 15910Sstevel@tonic-gate listen_backlog)); 15920Sstevel@tonic-gate } 15930Sstevel@tonic-gate } 15940Sstevel@tonic-gate (void) endnetconfig(nc); 15950Sstevel@tonic-gate 15960Sstevel@tonic-gate syslog(LOG_ERR, "couldn't find netconfig entry for protocol %s", 15970Sstevel@tonic-gate proto); 15980Sstevel@tonic-gate return (-1); 15990Sstevel@tonic-gate } 16000Sstevel@tonic-gate 16010Sstevel@tonic-gate #include <netinet/in.h> 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate /* 16040Sstevel@tonic-gate * Create an address mask appropriate for the transport. 16050Sstevel@tonic-gate * The mask is used to obtain the host-specific part of 16060Sstevel@tonic-gate * a network address when comparing addresses. 16070Sstevel@tonic-gate * For an internet address the host-specific part is just 16080Sstevel@tonic-gate * the 32 bit IP address and this part of the mask is set 16090Sstevel@tonic-gate * to all-ones. The port number part of the mask is zeroes. 16100Sstevel@tonic-gate */ 16110Sstevel@tonic-gate static int 16120Sstevel@tonic-gate set_addrmask(fd, nconf, mask) 16130Sstevel@tonic-gate struct netconfig *nconf; 16140Sstevel@tonic-gate struct netbuf *mask; 16150Sstevel@tonic-gate { 16160Sstevel@tonic-gate struct t_info info; 16170Sstevel@tonic-gate 16180Sstevel@tonic-gate /* 16190Sstevel@tonic-gate * Find the size of the address we need to mask. 16200Sstevel@tonic-gate */ 16210Sstevel@tonic-gate if (t_getinfo(fd, &info) < 0) { 16220Sstevel@tonic-gate t_error("t_getinfo"); 16230Sstevel@tonic-gate return (-1); 16240Sstevel@tonic-gate } 16250Sstevel@tonic-gate mask->len = mask->maxlen = info.addr; 16260Sstevel@tonic-gate if (info.addr <= 0) { 16270Sstevel@tonic-gate syslog(LOG_ERR, "set_addrmask: address size: %ld", 16280Sstevel@tonic-gate info.addr); 16290Sstevel@tonic-gate return (-1); 16300Sstevel@tonic-gate } 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate mask->buf = (char *)malloc(mask->len); 16330Sstevel@tonic-gate if (mask->buf == NULL) { 16340Sstevel@tonic-gate syslog(LOG_ERR, "set_addrmask: no memory"); 16350Sstevel@tonic-gate return (-1); 16360Sstevel@tonic-gate } 16370Sstevel@tonic-gate (void) memset(mask->buf, 0, mask->len); /* reset all mask bits */ 16380Sstevel@tonic-gate 16390Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_INET) == 0) { 16400Sstevel@tonic-gate /* 16410Sstevel@tonic-gate * Set the mask so that the port is ignored. 16420Sstevel@tonic-gate */ 16430Sstevel@tonic-gate /* LINTED pointer alignment */ 16440Sstevel@tonic-gate ((struct sockaddr_in *)mask->buf)->sin_addr.s_addr = 16450Sstevel@tonic-gate (ulong_t)~0; 16460Sstevel@tonic-gate /* LINTED pointer alignment */ 16470Sstevel@tonic-gate ((struct sockaddr_in *)mask->buf)->sin_family = 16480Sstevel@tonic-gate (ushort_t)~0; 16490Sstevel@tonic-gate } else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) { 16500Sstevel@tonic-gate /* LINTED pointer alignment */ 16510Sstevel@tonic-gate (void) memset(&((struct sockaddr_in6 *)mask->buf)->sin6_addr, 16520Sstevel@tonic-gate (uchar_t)~0, sizeof (struct in6_addr)); 16530Sstevel@tonic-gate /* LINTED pointer alignment */ 16540Sstevel@tonic-gate ((struct sockaddr_in6 *)mask->buf)->sin6_family = 16550Sstevel@tonic-gate (ushort_t)~0; 16560Sstevel@tonic-gate } else { 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate /* 16590Sstevel@tonic-gate * Set all mask bits. 16600Sstevel@tonic-gate */ 16610Sstevel@tonic-gate (void) memset(mask->buf, 0xFF, mask->len); 16620Sstevel@tonic-gate } 16630Sstevel@tonic-gate return (0); 16640Sstevel@tonic-gate } 16650Sstevel@tonic-gate 16660Sstevel@tonic-gate /* 16670Sstevel@tonic-gate * For listen fd's index is always less than end_listen_fds. 16680Sstevel@tonic-gate * end_listen_fds is defined externally in the daemon that uses this library. 16690Sstevel@tonic-gate * It's value is equal to the number of open file descriptors after the 16700Sstevel@tonic-gate * last listen end point was opened but before any connection was accepted. 16710Sstevel@tonic-gate */ 16720Sstevel@tonic-gate static int 16730Sstevel@tonic-gate is_listen_fd_index(int index) 16740Sstevel@tonic-gate { 16750Sstevel@tonic-gate return (index < end_listen_fds); 16760Sstevel@tonic-gate } 1677