xref: /onnv-gate/usr/src/lib/libnsl/rpc/svc_generic.c (revision 12648:61bf50c7467b)
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
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21132Srobinson 
220Sstevel@tonic-gate /*
23*12648SSurya.Prakki@Sun.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T */
270Sstevel@tonic-gate /*	All Rights Reserved   */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * svc_generic.c, Server side for RPC.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "mt.h"
35132Srobinson #include <stdlib.h>
360Sstevel@tonic-gate #include <sys/socket.h>
370Sstevel@tonic-gate #include <netinet/in.h>
380Sstevel@tonic-gate #include <netinet/tcp.h>
390Sstevel@tonic-gate #include <netinet/udp.h>
400Sstevel@tonic-gate #include <inttypes.h>
410Sstevel@tonic-gate #include "rpc_mt.h"
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <rpc/rpc.h>
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <syslog.h>
470Sstevel@tonic-gate #include <rpc/nettype.h>
480Sstevel@tonic-gate #include <malloc.h>
490Sstevel@tonic-gate #include <string.h>
500Sstevel@tonic-gate #include <stropts.h>
511676Sjpk #include <tsol/label.h>
521676Sjpk #include <nfs/nfs.h>
531676Sjpk #include <nfs/nfs_acl.h>
541676Sjpk #include <rpcsvc/mount.h>
551676Sjpk #include <rpcsvc/nsm_addr.h>
561676Sjpk #include <rpcsvc/rquota.h>
571676Sjpk #include <rpcsvc/sm_inter.h>
581676Sjpk #include <rpcsvc/nlm_prot.h>
590Sstevel@tonic-gate 
60132Srobinson extern int __svc_vc_setflag(SVCXPRT *, int);
610Sstevel@tonic-gate 
62132Srobinson extern SVCXPRT *svc_dg_create_private(int, uint_t, uint_t);
63132Srobinson extern SVCXPRT *svc_vc_create_private(int, uint_t, uint_t);
64132Srobinson extern SVCXPRT *svc_fd_create_private(int, uint_t, uint_t);
650Sstevel@tonic-gate 
66132Srobinson extern bool_t __svc_add_to_xlist(SVCXPRT_LIST **, SVCXPRT *, mutex_t *);
67132Srobinson extern void __svc_free_xlist(SVCXPRT_LIST **, mutex_t *);
68132Srobinson 
69132Srobinson extern bool_t __rpc_try_doors(const char *, bool_t *);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * The highest level interface for server creation.
730Sstevel@tonic-gate  * It tries for all the nettokens in that particular class of token
740Sstevel@tonic-gate  * and returns the number of handles it can create and/or find.
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  * It creates a link list of all the handles it could create.
770Sstevel@tonic-gate  * If svc_create() is called multiple times, it uses the handle
780Sstevel@tonic-gate  * created earlier instead of creating a new handle every time.
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate SVCXPRT_LIST *_svc_xprtlist = NULL;
840Sstevel@tonic-gate extern mutex_t xprtlist_lock;
850Sstevel@tonic-gate 
861676Sjpk static SVCXPRT * svc_tli_create_common(int, const struct netconfig *,
871676Sjpk     const struct t_bind *, uint_t, uint_t, boolean_t);
881676Sjpk 
891676Sjpk boolean_t
is_multilevel(rpcprog_t prognum)901676Sjpk is_multilevel(rpcprog_t prognum)
911676Sjpk {
921676Sjpk 	/* This is a list of identified multilevel service provider */
931676Sjpk 	if ((prognum == MOUNTPROG) || (prognum == NFS_PROGRAM) ||
941676Sjpk 	    (prognum == NFS_ACL_PROGRAM) || (prognum == NLM_PROG) ||
951676Sjpk 	    (prognum == NSM_ADDR_PROGRAM) || (prognum == RQUOTAPROG) ||
961676Sjpk 	    (prognum == SM_PROG))
971676Sjpk 		return (B_TRUE);
981676Sjpk 
991676Sjpk 	return (B_FALSE);
1001676Sjpk }
1011676Sjpk 
1020Sstevel@tonic-gate void
__svc_free_xprtlist(void)103132Srobinson __svc_free_xprtlist(void)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	__svc_free_xlist(&_svc_xprtlist, &xprtlist_lock);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate int
svc_create(void (* dispatch)(),const rpcprog_t prognum,const rpcvers_t versnum,const char * nettype)109132Srobinson svc_create(void (*dispatch)(), const rpcprog_t prognum, const rpcvers_t versnum,
110132Srobinson 							const char *nettype)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	SVCXPRT_LIST *l;
1130Sstevel@tonic-gate 	int num = 0;
1140Sstevel@tonic-gate 	SVCXPRT *xprt;
1150Sstevel@tonic-gate 	struct netconfig *nconf;
1160Sstevel@tonic-gate 	void *handle;
1170Sstevel@tonic-gate 	bool_t try_others;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	/*
1200Sstevel@tonic-gate 	 * Check if service should register over doors transport.
1210Sstevel@tonic-gate 	 */
1220Sstevel@tonic-gate 	if (__rpc_try_doors(nettype, &try_others)) {
1230Sstevel@tonic-gate 		if (svc_door_create(dispatch, prognum, versnum, 0) == NULL)
1240Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
1250Sstevel@tonic-gate 				"svc_create: could not register over doors");
1260Sstevel@tonic-gate 		else
1270Sstevel@tonic-gate 			num++;
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	if (!try_others)
1300Sstevel@tonic-gate 		return (num);
1310Sstevel@tonic-gate 	if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
1320Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "svc_create: unknown protocol");
1330Sstevel@tonic-gate 		return (0);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 	while (nconf = __rpc_getconf(handle)) {
136132Srobinson 		(void) mutex_lock(&xprtlist_lock);
1370Sstevel@tonic-gate 		for (l = _svc_xprtlist; l; l = l->next) {
1380Sstevel@tonic-gate 			if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
139*12648SSurya.Prakki@Sun.COM 				/* Found an old one, use it */
140*12648SSurya.Prakki@Sun.COM 				(void) rpcb_unset(prognum, versnum, nconf);
1410Sstevel@tonic-gate 				if (svc_reg(l->xprt, prognum, versnum,
1420Sstevel@tonic-gate 					dispatch, nconf) == FALSE)
1430Sstevel@tonic-gate 					(void) syslog(LOG_ERR,
1440Sstevel@tonic-gate 		"svc_create: could not register prog %d vers %d on %s",
1450Sstevel@tonic-gate 					prognum, versnum, nconf->nc_netid);
1460Sstevel@tonic-gate 				else
1470Sstevel@tonic-gate 					num++;
1480Sstevel@tonic-gate 				break;
1490Sstevel@tonic-gate 			}
1500Sstevel@tonic-gate 		}
151132Srobinson 		(void) mutex_unlock(&xprtlist_lock);
1520Sstevel@tonic-gate 		if (l == NULL) {
1530Sstevel@tonic-gate 			/* It was not found. Now create a new one */
1540Sstevel@tonic-gate 			xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
1550Sstevel@tonic-gate 			if (xprt) {
1560Sstevel@tonic-gate 				if (!__svc_add_to_xlist(&_svc_xprtlist, xprt,
1570Sstevel@tonic-gate 							&xprtlist_lock)) {
1580Sstevel@tonic-gate 					(void) syslog(LOG_ERR,
1590Sstevel@tonic-gate 						"svc_create: no memory");
1600Sstevel@tonic-gate 					return (0);
1610Sstevel@tonic-gate 				}
1620Sstevel@tonic-gate 				num++;
1630Sstevel@tonic-gate 			}
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 	__rpc_endconf(handle);
1670Sstevel@tonic-gate 	/*
1680Sstevel@tonic-gate 	 * In case of num == 0; the error messages are generated by the
1690Sstevel@tonic-gate 	 * underlying layers; and hence not needed here.
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 	return (num);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * The high level interface to svc_tli_create().
1760Sstevel@tonic-gate  * It tries to create a server for "nconf" and registers the service
1770Sstevel@tonic-gate  * with the rpcbind. It calls svc_tli_create();
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate SVCXPRT *
svc_tp_create(void (* dispatch)(),const rpcprog_t prognum,const rpcvers_t versnum,const struct netconfig * nconf)180132Srobinson svc_tp_create(void (*dispatch)(), const rpcprog_t prognum,
181132Srobinson 			const rpcvers_t versnum, const struct netconfig *nconf)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	SVCXPRT *xprt;
1841676Sjpk 	boolean_t anon_mlp = B_FALSE;
1850Sstevel@tonic-gate 
186132Srobinson 	if (nconf == NULL) {
1870Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
1880Sstevel@tonic-gate 	"svc_tp_create: invalid netconfig structure for prog %d vers %d",
1890Sstevel@tonic-gate 				prognum, versnum);
190132Srobinson 		return (NULL);
1910Sstevel@tonic-gate 	}
1921676Sjpk 
1931676Sjpk 	/* Some programs need to allocate MLP for multilevel services */
1941676Sjpk 	if (is_system_labeled() && is_multilevel(prognum))
1951676Sjpk 		anon_mlp = B_TRUE;
1961676Sjpk 	xprt = svc_tli_create_common(RPC_ANYFD, nconf, NULL, 0, 0, anon_mlp);
197132Srobinson 	if (xprt == NULL)
198132Srobinson 		return (NULL);
1992712Snn35248 
200*12648SSurya.Prakki@Sun.COM 	(void) rpcb_unset(prognum, versnum, (struct netconfig *)nconf);
2010Sstevel@tonic-gate 	if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
2020Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
2030Sstevel@tonic-gate 		"svc_tp_create: Could not register prog %d vers %d on %s",
2040Sstevel@tonic-gate 				prognum, versnum, nconf->nc_netid);
2050Sstevel@tonic-gate 		SVC_DESTROY(xprt);
206132Srobinson 		return (NULL);
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 	return (xprt);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2111676Sjpk SVCXPRT *
svc_tli_create(const int fd,const struct netconfig * nconf,const struct t_bind * bindaddr,const uint_t sendsz,const uint_t recvsz)2121676Sjpk svc_tli_create(const int fd, const struct netconfig *nconf,
2131676Sjpk     const struct t_bind *bindaddr, const uint_t sendsz, const uint_t recvsz)
2141676Sjpk {
2151676Sjpk 	return (svc_tli_create_common(fd, nconf, bindaddr, sendsz, recvsz, 0));
2161676Sjpk }
2171676Sjpk 
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate  * If fd is RPC_ANYFD, then it opens a fd for the given transport
2200Sstevel@tonic-gate  * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
2210Sstevel@tonic-gate  * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
2220Sstevel@tonic-gate  * NULL bindadr and Connection oriented transports, the value of qlen
2230Sstevel@tonic-gate  * is set arbitrarily.
2240Sstevel@tonic-gate  *
2250Sstevel@tonic-gate  * If sendsz or recvsz are zero, their default values are chosen.
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate SVCXPRT *
svc_tli_create_common(const int ofd,const struct netconfig * nconf,const struct t_bind * bindaddr,const uint_t sendsz,const uint_t recvsz,boolean_t mlp_flag)2281676Sjpk svc_tli_create_common(const int ofd, const struct netconfig *nconf,
2291676Sjpk 	const struct t_bind *bindaddr, const uint_t sendsz,
2301676Sjpk 	const uint_t recvsz, boolean_t mlp_flag)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	SVCXPRT *xprt = NULL;		/* service handle */
2330Sstevel@tonic-gate 	struct t_info tinfo;		/* transport info */
2340Sstevel@tonic-gate 	struct t_bind *tres = NULL;	/* bind info */
2350Sstevel@tonic-gate 	bool_t madefd = FALSE;		/* whether fd opened here  */
2360Sstevel@tonic-gate 	int state;			/* state of the transport provider */
237132Srobinson 	int fd = ofd;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (fd == RPC_ANYFD) {
240132Srobinson 		if (nconf == NULL) {
2410Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
2420Sstevel@tonic-gate 			    "svc_tli_create: invalid netconfig");
243132Srobinson 			return (NULL);
2440Sstevel@tonic-gate 		}
2450Sstevel@tonic-gate 		fd = t_open(nconf->nc_device, O_RDWR, &tinfo);
2460Sstevel@tonic-gate 		if (fd == -1) {
2470Sstevel@tonic-gate 			char errorstr[100];
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 			__tli_sys_strerror(errorstr, sizeof (errorstr),
2500Sstevel@tonic-gate 					t_errno, errno);
2510Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
2520Sstevel@tonic-gate 			"svc_tli_create: could not open connection for %s: %s",
2530Sstevel@tonic-gate 					nconf->nc_netid, errorstr);
254132Srobinson 			return (NULL);
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 		madefd = TRUE;
2570Sstevel@tonic-gate 		state = T_UNBND;
2580Sstevel@tonic-gate 	} else {
2590Sstevel@tonic-gate 		/*
2600Sstevel@tonic-gate 		 * It is an open descriptor. Sync it & get the transport info.
2610Sstevel@tonic-gate 		 */
2620Sstevel@tonic-gate 		if ((state = t_sync(fd)) == -1) {
2630Sstevel@tonic-gate 			char errorstr[100];
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 			__tli_sys_strerror(errorstr, sizeof (errorstr),
2660Sstevel@tonic-gate 					t_errno, errno);
2670Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
2680Sstevel@tonic-gate 		"svc_tli_create: could not do t_sync: %s",
2690Sstevel@tonic-gate 					errorstr);
270132Srobinson 			return (NULL);
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 		if (t_getinfo(fd, &tinfo) == -1) {
2730Sstevel@tonic-gate 			char errorstr[100];
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 			__tli_sys_strerror(errorstr, sizeof (errorstr),
2760Sstevel@tonic-gate 					t_errno, errno);
2770Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
2780Sstevel@tonic-gate 		"svc_tli_create: could not get transport information: %s",
2790Sstevel@tonic-gate 				    errorstr);
280132Srobinson 			return (NULL);
2810Sstevel@tonic-gate 		}
2820Sstevel@tonic-gate 		/* Enable options of returning the ip's for udp */
2830Sstevel@tonic-gate 		if (nconf) {
2840Sstevel@tonic-gate 		    int ret = 0;
2850Sstevel@tonic-gate 		    if (strcmp(nconf->nc_netid, "udp6") == 0) {
2860Sstevel@tonic-gate 			ret = __rpc_tli_set_options(fd, IPPROTO_IPV6,
2870Sstevel@tonic-gate 					IPV6_RECVPKTINFO, 1);
2880Sstevel@tonic-gate 			if (ret < 0) {
2890Sstevel@tonic-gate 			    char errorstr[100];
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 			    __tli_sys_strerror(errorstr, sizeof (errorstr),
2920Sstevel@tonic-gate 					t_errno, errno);
2930Sstevel@tonic-gate 			    (void) syslog(LOG_ERR,
2940Sstevel@tonic-gate 				"svc_tli_create: IPV6_RECVPKTINFO(1): %s",
2950Sstevel@tonic-gate 					errorstr);
296132Srobinson 			    return (NULL);
2970Sstevel@tonic-gate 			}
2980Sstevel@tonic-gate 		    } else if (strcmp(nconf->nc_netid, "udp") == 0) {
2990Sstevel@tonic-gate 			ret = __rpc_tli_set_options(fd, IPPROTO_IP,
3000Sstevel@tonic-gate 					IP_RECVDSTADDR, 1);
3010Sstevel@tonic-gate 			if (ret < 0) {
3020Sstevel@tonic-gate 			    char errorstr[100];
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 			    __tli_sys_strerror(errorstr, sizeof (errorstr),
3050Sstevel@tonic-gate 					t_errno, errno);
3060Sstevel@tonic-gate 			    (void) syslog(LOG_ERR,
3070Sstevel@tonic-gate 				"svc_tli_create: IP_RECVDSTADDR(1): %s",
3080Sstevel@tonic-gate 					errorstr);
309132Srobinson 			    return (NULL);
3100Sstevel@tonic-gate 			}
3110Sstevel@tonic-gate 		    }
3120Sstevel@tonic-gate 		}
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/*
3160Sstevel@tonic-gate 	 * If the fd is unbound, try to bind it.
3170Sstevel@tonic-gate 	 * In any case, try to get its bound info in tres
3180Sstevel@tonic-gate 	 */
3190Sstevel@tonic-gate /* LINTED pointer alignment */
3200Sstevel@tonic-gate 	tres = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
3210Sstevel@tonic-gate 	if (tres == NULL) {
3220Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "svc_tli_create: No memory!");
3230Sstevel@tonic-gate 		goto freedata;
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	switch (state) {
3270Sstevel@tonic-gate 		bool_t tcp, exclbind;
3280Sstevel@tonic-gate 	case T_UNBND:
3291676Sjpk 		/* If this is a labeled system, then ask for an MLP */
3301676Sjpk 		if (is_system_labeled() &&
3311676Sjpk 		    (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
3321676Sjpk 		    strcmp(nconf->nc_protofmly, NC_INET6) == 0)) {
3331676Sjpk 			(void) __rpc_tli_set_options(fd, SOL_SOCKET,
3341676Sjpk 			    SO_RECVUCRED, 1);
3351676Sjpk 			if (mlp_flag)
3361676Sjpk 				(void) __rpc_tli_set_options(fd, SOL_SOCKET,
3371676Sjpk 				    SO_ANON_MLP, 1);
3381676Sjpk 		}
3391676Sjpk 
3400Sstevel@tonic-gate 		/*
3412429Skcpoon 		 * SO_EXCLBIND has the following properties
3420Sstevel@tonic-gate 		 *    - an fd bound to port P via IPv4 will prevent an IPv6
3430Sstevel@tonic-gate 		 *    bind to port P (and vice versa)
3440Sstevel@tonic-gate 		 *    - an fd bound to a wildcard IP address for port P will
3450Sstevel@tonic-gate 		 *    prevent a more specific IP address bind to port P
3460Sstevel@tonic-gate 		 *    (see {tcp,udp}.c for details)
3470Sstevel@tonic-gate 		 *
3480Sstevel@tonic-gate 		 * We use the latter property to prevent hijacking of RPC
3490Sstevel@tonic-gate 		 * services that reside at non-privileged ports.
3500Sstevel@tonic-gate 		 */
3510Sstevel@tonic-gate 		tcp = nconf ? (strcmp(nconf->nc_proto, NC_TCP) == 0) : 0;
3520Sstevel@tonic-gate 		if (nconf &&
3530Sstevel@tonic-gate 		    (tcp || (strcmp(nconf->nc_proto, NC_UDP) == 0)) &&
3540Sstevel@tonic-gate 		    rpc_control(__RPC_SVC_EXCLBIND_GET, &exclbind)) {
3550Sstevel@tonic-gate 			if (exclbind) {
3562429Skcpoon 				if (__rpc_tli_set_options(fd, SOL_SOCKET,
3572429Skcpoon 				    SO_EXCLBIND, 1) < 0) {
3580Sstevel@tonic-gate 					syslog(LOG_ERR,
3590Sstevel@tonic-gate 			    "svc_tli_create: can't set EXCLBIND [netid='%s']",
3600Sstevel@tonic-gate 					    nconf->nc_netid);
3610Sstevel@tonic-gate 					goto freedata;
3620Sstevel@tonic-gate 				}
3630Sstevel@tonic-gate 			}
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 		if (bindaddr) {
3660Sstevel@tonic-gate 			if (t_bind(fd, (struct t_bind *)bindaddr,
3670Sstevel@tonic-gate 								tres) == -1) {
3680Sstevel@tonic-gate 				char errorstr[100];
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 				__tli_sys_strerror(errorstr, sizeof (errorstr),
3710Sstevel@tonic-gate 						t_errno, errno);
3720Sstevel@tonic-gate 				(void) syslog(LOG_ERR,
3730Sstevel@tonic-gate 					"svc_tli_create: could not bind: %s",
3740Sstevel@tonic-gate 					    errorstr);
3750Sstevel@tonic-gate 				goto freedata;
3760Sstevel@tonic-gate 			}
3770Sstevel@tonic-gate 			/*
3780Sstevel@tonic-gate 			 * Should compare the addresses only if addr.len
3790Sstevel@tonic-gate 			 * was non-zero
3800Sstevel@tonic-gate 			 */
3810Sstevel@tonic-gate 			if (bindaddr->addr.len &&
3820Sstevel@tonic-gate 				(memcmp(bindaddr->addr.buf, tres->addr.buf,
3830Sstevel@tonic-gate 					(int)tres->addr.len) != 0)) {
3840Sstevel@tonic-gate 				(void) syslog(LOG_ERR,
3850Sstevel@tonic-gate 		"svc_tli_create: could not bind to requested address: %s",
3860Sstevel@tonic-gate 						"address mismatch");
3870Sstevel@tonic-gate 				goto freedata;
3880Sstevel@tonic-gate 			}
3890Sstevel@tonic-gate 		} else {
3900Sstevel@tonic-gate 			tres->qlen = 64; /* Chosen Arbitrarily */
3910Sstevel@tonic-gate 			tres->addr.len = 0;
3920Sstevel@tonic-gate 			if (t_bind(fd, tres, tres) == -1) {
3930Sstevel@tonic-gate 				char errorstr[100];
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 				__tli_sys_strerror(errorstr, sizeof (errorstr),
3960Sstevel@tonic-gate 						t_errno, errno);
3970Sstevel@tonic-gate 				(void) syslog(LOG_ERR,
3980Sstevel@tonic-gate 					"svc_tli_create: could not bind: %s",
3990Sstevel@tonic-gate 						errorstr);
4000Sstevel@tonic-gate 				goto freedata;
4010Sstevel@tonic-gate 			}
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 		/* Enable options of returning the ip's for udp */
4050Sstevel@tonic-gate 		if (nconf) {
4060Sstevel@tonic-gate 		    int ret = 0;
4070Sstevel@tonic-gate 		    if (strcmp(nconf->nc_netid, "udp6") == 0) {
4080Sstevel@tonic-gate 			ret = __rpc_tli_set_options(fd, IPPROTO_IPV6,
4090Sstevel@tonic-gate 					IPV6_RECVPKTINFO, 1);
4100Sstevel@tonic-gate 			if (ret < 0) {
4110Sstevel@tonic-gate 			    char errorstr[100];
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 			    __tli_sys_strerror(errorstr, sizeof (errorstr),
4140Sstevel@tonic-gate 					t_errno, errno);
4150Sstevel@tonic-gate 			    (void) syslog(LOG_ERR,
4160Sstevel@tonic-gate 				"svc_tli_create: IPV6_RECVPKTINFO(2): %s",
4170Sstevel@tonic-gate 					errorstr);
4180Sstevel@tonic-gate 			    goto freedata;
4190Sstevel@tonic-gate 			}
4200Sstevel@tonic-gate 		    } else if (strcmp(nconf->nc_netid, "udp") == 0) {
4210Sstevel@tonic-gate 			ret = __rpc_tli_set_options(fd, IPPROTO_IP,
4220Sstevel@tonic-gate 					IP_RECVDSTADDR, 1);
4230Sstevel@tonic-gate 			if (ret < 0) {
4240Sstevel@tonic-gate 			    char errorstr[100];
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 			    __tli_sys_strerror(errorstr, sizeof (errorstr),
4270Sstevel@tonic-gate 					t_errno, errno);
4280Sstevel@tonic-gate 			    (void) syslog(LOG_ERR,
4290Sstevel@tonic-gate 				"svc_tli_create: IP_RECVDSTADDR(2): %s",
4300Sstevel@tonic-gate 					errorstr);
4310Sstevel@tonic-gate 			    goto freedata;
4320Sstevel@tonic-gate 			}
4330Sstevel@tonic-gate 		    }
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 		break;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	case T_IDLE:
4380Sstevel@tonic-gate 		if (bindaddr) {
4390Sstevel@tonic-gate 			/* Copy the entire stuff in tres */
4400Sstevel@tonic-gate 			if (tres->addr.maxlen < bindaddr->addr.len) {
4410Sstevel@tonic-gate 				(void) syslog(LOG_ERR,
4420Sstevel@tonic-gate 				"svc_tli_create: illegal netbuf length");
4430Sstevel@tonic-gate 				goto freedata;
4440Sstevel@tonic-gate 			}
4450Sstevel@tonic-gate 			tres->addr.len = bindaddr->addr.len;
4460Sstevel@tonic-gate 			(void) memcpy(tres->addr.buf, bindaddr->addr.buf,
4470Sstevel@tonic-gate 					(int)tres->addr.len);
4480Sstevel@tonic-gate 		} else
4490Sstevel@tonic-gate 			if (t_getname(fd, &(tres->addr), LOCALNAME) == -1)
4500Sstevel@tonic-gate 				tres->addr.len = 0;
4510Sstevel@tonic-gate 		break;
4520Sstevel@tonic-gate 	case T_INREL:
4530Sstevel@tonic-gate 		(void) t_rcvrel(fd);
4540Sstevel@tonic-gate 		(void) t_sndrel(fd);
4550Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
4560Sstevel@tonic-gate 			"svc_tli_create: other side wants to\
4570Sstevel@tonic-gate release connection");
4580Sstevel@tonic-gate 		goto freedata;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	case T_INCON:
4610Sstevel@tonic-gate 		/* Do nothing here. Assume this is handled in rendezvous */
4620Sstevel@tonic-gate 		break;
4630Sstevel@tonic-gate 	case T_DATAXFER:
4640Sstevel@tonic-gate 		/*
4650Sstevel@tonic-gate 		 * This takes care of the case where a fd
4660Sstevel@tonic-gate 		 * is passed on which a connection has already
4670Sstevel@tonic-gate 		 * been accepted.
4680Sstevel@tonic-gate 		 */
4690Sstevel@tonic-gate 		if (t_getname(fd, &(tres->addr), LOCALNAME) == -1)
4700Sstevel@tonic-gate 			tres->addr.len = 0;
4710Sstevel@tonic-gate 		break;
4720Sstevel@tonic-gate 	default:
4730Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
4740Sstevel@tonic-gate 		    "svc_tli_create: connection in a wierd state (%d)", state);
4750Sstevel@tonic-gate 		goto freedata;
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/*
4790Sstevel@tonic-gate 	 * call transport specific function.
4800Sstevel@tonic-gate 	 */
4810Sstevel@tonic-gate 	switch (tinfo.servtype) {
4820Sstevel@tonic-gate 		case T_COTS_ORD:
4830Sstevel@tonic-gate 		case T_COTS:
4840Sstevel@tonic-gate 			if (state == T_DATAXFER)
4850Sstevel@tonic-gate 				xprt = svc_fd_create_private(fd, sendsz,
4860Sstevel@tonic-gate 						recvsz);
4870Sstevel@tonic-gate 			else
4880Sstevel@tonic-gate 				xprt = svc_vc_create_private(fd, sendsz,
4890Sstevel@tonic-gate 						recvsz);
4900Sstevel@tonic-gate 			if (!nconf || !xprt)
4910Sstevel@tonic-gate 				break;
4920Sstevel@tonic-gate 			if ((tinfo.servtype == T_COTS_ORD) &&
4930Sstevel@tonic-gate 				(state != T_DATAXFER) &&
4940Sstevel@tonic-gate 				(strcmp(nconf->nc_protofmly, "inet") == 0))
4950Sstevel@tonic-gate 				(void) __svc_vc_setflag(xprt, TRUE);
4960Sstevel@tonic-gate 			break;
4970Sstevel@tonic-gate 		case T_CLTS:
4980Sstevel@tonic-gate 			xprt = svc_dg_create_private(fd, sendsz, recvsz);
4990Sstevel@tonic-gate 			break;
5000Sstevel@tonic-gate 		default:
5010Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
5020Sstevel@tonic-gate 			    "svc_tli_create: bad service type");
5030Sstevel@tonic-gate 			goto freedata;
5040Sstevel@tonic-gate 	}
505132Srobinson 	if (xprt == NULL)
5060Sstevel@tonic-gate 		/*
5070Sstevel@tonic-gate 		 * The error messages here are spitted out by the lower layers:
5080Sstevel@tonic-gate 		 * svc_vc_create(), svc_fd_create() and svc_dg_create().
5090Sstevel@tonic-gate 		 */
5100Sstevel@tonic-gate 		goto freedata;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/* fill in the other xprt information */
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	/* Assign the local bind address */
5150Sstevel@tonic-gate 	xprt->xp_ltaddr = tres->addr;
5160Sstevel@tonic-gate 	/* Fill in type of service */
5170Sstevel@tonic-gate 	xprt->xp_type = tinfo.servtype;
5180Sstevel@tonic-gate 	tres->addr.buf = NULL;
5190Sstevel@tonic-gate 	(void) t_free((char *)tres, T_BIND);
5200Sstevel@tonic-gate 	tres = NULL;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	xprt->xp_rtaddr.len = 0;
5230Sstevel@tonic-gate 	xprt->xp_rtaddr.maxlen = __rpc_get_a_size(tinfo.addr);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/* Allocate space for the remote bind info */
526132Srobinson 	if ((xprt->xp_rtaddr.buf = malloc(xprt->xp_rtaddr.maxlen)) == NULL) {
5270Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "svc_tli_create: No memory!");
5280Sstevel@tonic-gate 		goto freedata;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (nconf) {
5320Sstevel@tonic-gate 		xprt->xp_netid = strdup(nconf->nc_netid);
5330Sstevel@tonic-gate 		if (xprt->xp_netid == NULL) {
5340Sstevel@tonic-gate 			if (xprt->xp_rtaddr.buf)
5350Sstevel@tonic-gate 				free(xprt->xp_rtaddr.buf);
5360Sstevel@tonic-gate 			syslog(LOG_ERR, "svc_tli_create: strdup failed!");
5370Sstevel@tonic-gate 			goto freedata;
5380Sstevel@tonic-gate 		}
5390Sstevel@tonic-gate 		xprt->xp_tp = strdup(nconf->nc_device);
5400Sstevel@tonic-gate 		if (xprt->xp_tp == NULL) {
5410Sstevel@tonic-gate 			if (xprt->xp_rtaddr.buf)
5420Sstevel@tonic-gate 				free(xprt->xp_rtaddr.buf);
5430Sstevel@tonic-gate 			if (xprt->xp_netid)
5440Sstevel@tonic-gate 				free(xprt->xp_netid);
5450Sstevel@tonic-gate 			syslog(LOG_ERR, "svc_tli_create: strdup failed!");
5460Sstevel@tonic-gate 			goto freedata;
5470Sstevel@tonic-gate 		}
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate /*
5510Sstevel@tonic-gate  *	if (madefd && (tinfo.servtype == T_CLTS))
552132Srobinson  *		(void) ioctl(fd, I_POP, NULL);
5530Sstevel@tonic-gate  */
5540Sstevel@tonic-gate 	xprt_register(xprt);
5550Sstevel@tonic-gate 	return (xprt);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate freedata:
5580Sstevel@tonic-gate 	if (madefd)
5590Sstevel@tonic-gate 		(void) t_close(fd);
5600Sstevel@tonic-gate 	if (tres)
5610Sstevel@tonic-gate 		(void) t_free((char *)tres, T_BIND);
5620Sstevel@tonic-gate 	if (xprt) {
5630Sstevel@tonic-gate 		if (!madefd) /* so that svc_destroy doesnt close fd */
5640Sstevel@tonic-gate 			xprt->xp_fd = RPC_ANYFD;
5650Sstevel@tonic-gate 		SVC_DESTROY(xprt);
5660Sstevel@tonic-gate 	}
567132Srobinson 	return (NULL);
5680Sstevel@tonic-gate }
569