xref: /illumos-gate/usr/src/uts/common/rpc/clnt_gen.c (revision 2d6eb4a5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5dc3879f9Sjarrett  * Common Development and Distribution License (the "License").
6dc3879f9Sjarrett  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22dc3879f9Sjarrett  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <rpc/types.h>
377c478bd9Sstevel@tonic-gate #include <netinet/in.h>
387c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
397c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
407c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
417c478bd9Sstevel@tonic-gate #include <sys/t_kuser.h>
427c478bd9Sstevel@tonic-gate #include <rpc/svc.h>
437c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
447c478bd9Sstevel@tonic-gate #include <sys/file.h>
457c478bd9Sstevel@tonic-gate #include <sys/user.h>
467c478bd9Sstevel@tonic-gate #include <sys/proc.h>
477c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
487c478bd9Sstevel@tonic-gate #include <sys/stream.h>
497c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
507c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
517c478bd9Sstevel@tonic-gate #include <sys/socket.h>
527c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
537c478bd9Sstevel@tonic-gate #include <sys/errno.h>
547c478bd9Sstevel@tonic-gate #include <sys/cred.h>
557c478bd9Sstevel@tonic-gate #include <sys/systm.h>
567c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	NC_INET		"inet"
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	MAX_PRIV	(IPPORT_RESERVED-1)
617c478bd9Sstevel@tonic-gate #define	MIN_PRIV	(IPPORT_RESERVED/2)
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate ushort_t clnt_udp_last_used = MIN_PRIV;
647c478bd9Sstevel@tonic-gate ushort_t clnt_tcp_last_used = MIN_PRIV;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
687c478bd9Sstevel@tonic-gate  * clnt_tli_kcreate
697c478bd9Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
707c478bd9Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate int
clnt_tli_kcreate(struct knetconfig * config,struct netbuf * svcaddr,rpcprog_t prog,rpcvers_t vers,uint_t max_msgsize,int retries,struct cred * cred,CLIENT ** ncl)737c478bd9Sstevel@tonic-gate clnt_tli_kcreate(
747c478bd9Sstevel@tonic-gate 	struct knetconfig	*config,
757c478bd9Sstevel@tonic-gate 	struct netbuf		*svcaddr,	/* Servers address */
767c478bd9Sstevel@tonic-gate 	rpcprog_t		prog,		/* Program number */
777c478bd9Sstevel@tonic-gate 	rpcvers_t		vers,		/* Version number */
787c478bd9Sstevel@tonic-gate 	uint_t			max_msgsize,
797c478bd9Sstevel@tonic-gate 	int			retries,
807c478bd9Sstevel@tonic-gate 	struct cred		*cred,
817c478bd9Sstevel@tonic-gate 	CLIENT			**ncl)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	CLIENT			*cl;		/* Client handle */
847c478bd9Sstevel@tonic-gate 	int			error;
857c478bd9Sstevel@tonic-gate 	int			family = AF_UNSPEC;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	error = 0;
887c478bd9Sstevel@tonic-gate 	cl = NULL;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	RPCLOG(8, "clnt_tli_kcreate: prog %x", prog);
917c478bd9Sstevel@tonic-gate 	RPCLOG(8, ", vers %d", vers);
927c478bd9Sstevel@tonic-gate 	RPCLOG(8, ", knc_semantics %d", config->knc_semantics);
937c478bd9Sstevel@tonic-gate 	RPCLOG(8, ", knc_protofmly %s", config->knc_protofmly);
947c478bd9Sstevel@tonic-gate 	RPCLOG(8, ", knc_proto %s\n", config->knc_proto);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (config == NULL || config->knc_protofmly == NULL || ncl == NULL) {
977c478bd9Sstevel@tonic-gate 		RPCLOG0(1, "clnt_tli_kcreate: bad config or handle\n");
987c478bd9Sstevel@tonic-gate 		return (EINVAL);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	switch (config->knc_semantics) {
1027c478bd9Sstevel@tonic-gate 	case NC_TPI_CLTS:
1037c478bd9Sstevel@tonic-gate 		RPCLOG0(8, "clnt_tli_kcreate: CLTS selected\n");
1047c478bd9Sstevel@tonic-gate 		error = clnt_clts_kcreate(config, svcaddr, prog, vers,
1057c478bd9Sstevel@tonic-gate 						retries, cred, &cl);
1067c478bd9Sstevel@tonic-gate 		if (error != 0) {
1077c478bd9Sstevel@tonic-gate 			RPCLOG(1,
1087c478bd9Sstevel@tonic-gate 			"clnt_tli_kcreate: clnt_clts_kcreate failed error %d\n",
1097c478bd9Sstevel@tonic-gate 			    error);
1107c478bd9Sstevel@tonic-gate 			return (error);
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 		break;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	case NC_TPI_COTS:
1157c478bd9Sstevel@tonic-gate 	case NC_TPI_COTS_ORD:
1167c478bd9Sstevel@tonic-gate 		RPCLOG0(8, "clnt_tli_kcreate: COTS selected\n");
1177c478bd9Sstevel@tonic-gate 		if (strcmp(config->knc_protofmly, NC_INET) == 0)
1187c478bd9Sstevel@tonic-gate 			family = AF_INET;
1197c478bd9Sstevel@tonic-gate 		else if (strcmp(config->knc_protofmly, NC_INET6) == 0)
1207c478bd9Sstevel@tonic-gate 			family = AF_INET6;
1217c478bd9Sstevel@tonic-gate 		error = clnt_cots_kcreate(config->knc_rdev, svcaddr, family,
1227c478bd9Sstevel@tonic-gate 		    prog, vers, max_msgsize, cred, &cl);
1237c478bd9Sstevel@tonic-gate 		if (error != 0) {
1247c478bd9Sstevel@tonic-gate 			RPCLOG(1,
1257c478bd9Sstevel@tonic-gate 			"clnt_tli_kcreate: clnt_cots_kcreate failed error %d\n",
1267c478bd9Sstevel@tonic-gate 			error);
1277c478bd9Sstevel@tonic-gate 			return (error);
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 		break;
1307c478bd9Sstevel@tonic-gate 	case NC_TPI_RDMA:
1317c478bd9Sstevel@tonic-gate 		RPCLOG0(8, "clnt_tli_kcreate: RDMA selected\n");
132dc3879f9Sjarrett 		/*
133dc3879f9Sjarrett 		 * RDMA doesn't support TSOL. It's better to
134dc3879f9Sjarrett 		 * disallow it here.
135dc3879f9Sjarrett 		 */
136dc3879f9Sjarrett 		if (is_system_labeled()) {
137dc3879f9Sjarrett 			RPCLOG0(1, "clnt_tli_kcreate: tsol not supported\n");
138dc3879f9Sjarrett 			return (EPROTONOSUPPORT);
139dc3879f9Sjarrett 		}
140dc3879f9Sjarrett 
1417c478bd9Sstevel@tonic-gate 		if (strcmp(config->knc_protofmly, NC_INET) == 0)
1427c478bd9Sstevel@tonic-gate 			family = AF_INET;
1437c478bd9Sstevel@tonic-gate 		else if (strcmp(config->knc_protofmly, NC_INET6) == 0)
1447c478bd9Sstevel@tonic-gate 			family = AF_INET6;
1457c478bd9Sstevel@tonic-gate 		error = clnt_rdma_kcreate(config->knc_proto,
1467c478bd9Sstevel@tonic-gate 		    (void *)config->knc_rdev, svcaddr, family, prog, vers, cred,
1477c478bd9Sstevel@tonic-gate 		    &cl);
1487c478bd9Sstevel@tonic-gate 		if (error != 0) {
1497c478bd9Sstevel@tonic-gate 			RPCLOG(1,
1507c478bd9Sstevel@tonic-gate 			"clnt_tli_kcreate: clnt_rdma_kcreate failed error %d\n",
1517c478bd9Sstevel@tonic-gate 			error);
1527c478bd9Sstevel@tonic-gate 			return (error);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 		break;
1557c478bd9Sstevel@tonic-gate 	default:
1567c478bd9Sstevel@tonic-gate 		error = EINVAL;
1577c478bd9Sstevel@tonic-gate 		RPCLOG(1, "clnt_tli_kcreate: Bad service type %d\n",
1587c478bd9Sstevel@tonic-gate 		    config->knc_semantics);
1597c478bd9Sstevel@tonic-gate 		return (error);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	*ncl = cl;
1627c478bd9Sstevel@tonic-gate 	return (0);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * "Kinit" a client handle by calling the appropriate cots or clts routine.
1677c478bd9Sstevel@tonic-gate  *
1687c478bd9Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
1697c478bd9Sstevel@tonic-gate  * clnt_tli_kinit
1707c478bd9Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
1717c478bd9Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate int
clnt_tli_kinit(CLIENT * h,struct knetconfig * config,struct netbuf * addr,uint_t max_msgsize,int retries,struct cred * cred)1747c478bd9Sstevel@tonic-gate clnt_tli_kinit(
1757c478bd9Sstevel@tonic-gate 	CLIENT		*h,
1767c478bd9Sstevel@tonic-gate 	struct knetconfig *config,
1777c478bd9Sstevel@tonic-gate 	struct netbuf	*addr,
1787c478bd9Sstevel@tonic-gate 	uint_t		max_msgsize,
1797c478bd9Sstevel@tonic-gate 	int		retries,
1807c478bd9Sstevel@tonic-gate 	struct cred	*cred)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	int error = 0;
1837c478bd9Sstevel@tonic-gate 	int family = AF_UNSPEC;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	switch (config->knc_semantics) {
1867c478bd9Sstevel@tonic-gate 	case NC_TPI_CLTS:
1877c478bd9Sstevel@tonic-gate 		clnt_clts_kinit(h, addr, retries, cred);
1887c478bd9Sstevel@tonic-gate 		break;
1897c478bd9Sstevel@tonic-gate 	case NC_TPI_COTS:
1907c478bd9Sstevel@tonic-gate 	case NC_TPI_COTS_ORD:
1917c478bd9Sstevel@tonic-gate 		RPCLOG0(2, "clnt_tli_kinit: COTS selected\n");
1927c478bd9Sstevel@tonic-gate 		if (strcmp(config->knc_protofmly, NC_INET) == 0)
1937c478bd9Sstevel@tonic-gate 			family = AF_INET;
1947c478bd9Sstevel@tonic-gate 		else if (strcmp(config->knc_protofmly, NC_INET6) == 0)
1957c478bd9Sstevel@tonic-gate 			family = AF_INET6;
1967c478bd9Sstevel@tonic-gate 		clnt_cots_kinit(h, config->knc_rdev, family,
1977c478bd9Sstevel@tonic-gate 		    addr, max_msgsize, cred);
1987c478bd9Sstevel@tonic-gate 		break;
1997c478bd9Sstevel@tonic-gate 	case NC_TPI_RDMA:
2007c478bd9Sstevel@tonic-gate 		RPCLOG0(2, "clnt_tli_kinit: RDMA selected\n");
2017c478bd9Sstevel@tonic-gate 		clnt_rdma_kinit(h, config->knc_proto,
2027c478bd9Sstevel@tonic-gate 		    (void *)config->knc_rdev, addr, cred);
2037c478bd9Sstevel@tonic-gate 		break;
2047c478bd9Sstevel@tonic-gate 	default:
2057c478bd9Sstevel@tonic-gate 		error = EINVAL;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	return (error);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * try to bind to a reserved port
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate int
bindresvport(TIUSER * tiptr,struct netbuf * addr,struct netbuf * bound_addr,bool_t tcp)2167c478bd9Sstevel@tonic-gate bindresvport(
2177c478bd9Sstevel@tonic-gate 	TIUSER		*tiptr,
2187c478bd9Sstevel@tonic-gate 	struct netbuf	*addr,
2197c478bd9Sstevel@tonic-gate 	struct netbuf	*bound_addr,
2207c478bd9Sstevel@tonic-gate 	bool_t		tcp)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin;
2237c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
2247c478bd9Sstevel@tonic-gate 	bool_t 			ipv6_flag = 0;
2257c478bd9Sstevel@tonic-gate 	int			i;
2267c478bd9Sstevel@tonic-gate 	struct t_bind		*req;
2277c478bd9Sstevel@tonic-gate 	struct t_bind		*ret;
2287c478bd9Sstevel@tonic-gate 	int			error;
2297c478bd9Sstevel@tonic-gate 	bool_t			loop_twice;
2307c478bd9Sstevel@tonic-gate 	int			start;
2317c478bd9Sstevel@tonic-gate 	int			stop;
2327c478bd9Sstevel@tonic-gate 	ushort_t			*last_used;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if ((error = t_kalloc(tiptr, T_BIND, T_ADDR, (char **)&req)) != 0) {
2357c478bd9Sstevel@tonic-gate 		RPCLOG(1, "bindresvport: t_kalloc %d\n", error);
2367c478bd9Sstevel@tonic-gate 		return (error);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if ((error = t_kalloc(tiptr, T_BIND, T_ADDR, (char **)&ret)) != 0) {
2407c478bd9Sstevel@tonic-gate 		RPCLOG(1, "bindresvport: t_kalloc %d\n", error);
2417c478bd9Sstevel@tonic-gate 		(void) t_kfree(tiptr, (char *)req, T_BIND);
2427c478bd9Sstevel@tonic-gate 		return (error);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/* now separate IPv4 and IPv6 by looking at len of tiptr.addr */
2467c478bd9Sstevel@tonic-gate 	if (tiptr->tp_info.addr == sizeof (struct sockaddr_in6)) {
2477c478bd9Sstevel@tonic-gate 		/* it's IPv6 */
2487c478bd9Sstevel@tonic-gate 		ipv6_flag = 1;
2497c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)req->addr.buf;
2507c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
2517c478bd9Sstevel@tonic-gate 		bzero((char *)&sin6->sin6_addr, sizeof (struct in6_addr));
2527c478bd9Sstevel@tonic-gate 		req->addr.len = sizeof (struct sockaddr_in6);
2537c478bd9Sstevel@tonic-gate 	} else {
2547c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
2557c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)req->addr.buf;
2567c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
2577c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = INADDR_ANY;
2587c478bd9Sstevel@tonic-gate 		req->addr.len = sizeof (struct sockaddr_in);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/*
2627c478bd9Sstevel@tonic-gate 	 * Caller wants to bind to a specific port, so don't bother with the
2637c478bd9Sstevel@tonic-gate 	 * loop that binds to the next free one.
2647c478bd9Sstevel@tonic-gate 	 */
2657c478bd9Sstevel@tonic-gate 	if (addr) {
2667c478bd9Sstevel@tonic-gate 		if (ipv6_flag) {
2677c478bd9Sstevel@tonic-gate 			sin6->sin6_port =
2687c478bd9Sstevel@tonic-gate 				((struct sockaddr_in6 *)addr->buf)->sin6_port;
2697c478bd9Sstevel@tonic-gate 		} else {
2707c478bd9Sstevel@tonic-gate 			sin->sin_port =
2717c478bd9Sstevel@tonic-gate 				((struct sockaddr_in *)addr->buf)->sin_port;
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 		RPCLOG(8, "bindresvport: calling t_kbind tiptr = %p\n",
2747c478bd9Sstevel@tonic-gate 		    (void *)tiptr);
2757c478bd9Sstevel@tonic-gate 		if ((error = t_kbind(tiptr, req, ret)) != 0) {
2767c478bd9Sstevel@tonic-gate 			RPCLOG(1, "bindresvport: t_kbind: %d\n", error);
2777c478bd9Sstevel@tonic-gate 			/*
2787c478bd9Sstevel@tonic-gate 			 * The unbind is called in case the bind failed
2797c478bd9Sstevel@tonic-gate 			 * with an EINTR potentially leaving the
2807c478bd9Sstevel@tonic-gate 			 * transport in bound state.
2817c478bd9Sstevel@tonic-gate 			 */
2827c478bd9Sstevel@tonic-gate 			if (error == EINTR)
2837c478bd9Sstevel@tonic-gate 				(void) t_kunbind(tiptr);
2847c478bd9Sstevel@tonic-gate 		} else if (bcmp(req->addr.buf, ret->addr.buf,
2857c478bd9Sstevel@tonic-gate 				ret->addr.len) != 0) {
2867c478bd9Sstevel@tonic-gate 			RPCLOG0(1, "bindresvport: bcmp error\n");
2877c478bd9Sstevel@tonic-gate 			(void) t_kunbind(tiptr);
2887c478bd9Sstevel@tonic-gate 			error = EADDRINUSE;
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 	} else {
2917c478bd9Sstevel@tonic-gate 		if (tcp)
2927c478bd9Sstevel@tonic-gate 			last_used = &clnt_tcp_last_used;
2937c478bd9Sstevel@tonic-gate 		else
2947c478bd9Sstevel@tonic-gate 			last_used = &clnt_udp_last_used;
2957c478bd9Sstevel@tonic-gate 		error = EADDRINUSE;
2967c478bd9Sstevel@tonic-gate 		stop = MIN_PRIV;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		start = (*last_used == MIN_PRIV ? MAX_PRIV : *last_used - 1);
2997c478bd9Sstevel@tonic-gate 		loop_twice = (start < MAX_PRIV ? TRUE : FALSE);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate bindresvport_again:
3027c478bd9Sstevel@tonic-gate 		for (i = start;
3037c478bd9Sstevel@tonic-gate 		    (error == EADDRINUSE || error == EADDRNOTAVAIL) &&
3047c478bd9Sstevel@tonic-gate 		    i >= stop; i--) {
3057c478bd9Sstevel@tonic-gate 			if (ipv6_flag)
3067c478bd9Sstevel@tonic-gate 				sin6->sin6_port = htons(i);
3077c478bd9Sstevel@tonic-gate 			else
3087c478bd9Sstevel@tonic-gate 				sin->sin_port = htons(i);
3097c478bd9Sstevel@tonic-gate 			RPCLOG(8, "bindresvport: calling t_kbind tiptr = 0%p\n",
3107c478bd9Sstevel@tonic-gate 			    (void *)tiptr);
3117c478bd9Sstevel@tonic-gate 			if ((error = t_kbind(tiptr, req, ret)) != 0) {
3127c478bd9Sstevel@tonic-gate 				RPCLOG(1, "bindresvport: t_kbind: %d\n", error);
3137c478bd9Sstevel@tonic-gate 				/*
3147c478bd9Sstevel@tonic-gate 				 * The unbind is called in case the bind failed
3157c478bd9Sstevel@tonic-gate 				 * with an EINTR potentially leaving the
3167c478bd9Sstevel@tonic-gate 				 * transport in bound state.
3177c478bd9Sstevel@tonic-gate 				 */
3187c478bd9Sstevel@tonic-gate 				if (error == EINTR)
3197c478bd9Sstevel@tonic-gate 					(void) t_kunbind(tiptr);
3207c478bd9Sstevel@tonic-gate 			} else if (bcmp(req->addr.buf, ret->addr.buf,
3217c478bd9Sstevel@tonic-gate 			    ret->addr.len) != 0) {
3227c478bd9Sstevel@tonic-gate 				RPCLOG0(1, "bindresvport: bcmp error\n");
3237c478bd9Sstevel@tonic-gate 				(void) t_kunbind(tiptr);
3247c478bd9Sstevel@tonic-gate 				error = EADDRINUSE;
3257c478bd9Sstevel@tonic-gate 			} else
3267c478bd9Sstevel@tonic-gate 				error = 0;
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 		if (!error) {
3297c478bd9Sstevel@tonic-gate 			if (ipv6_flag) {
3307c478bd9Sstevel@tonic-gate 				RPCLOG(8, "bindresvport: port assigned %d\n",
3317c478bd9Sstevel@tonic-gate 					sin6->sin6_port);
3327c478bd9Sstevel@tonic-gate 				*last_used = ntohs(sin6->sin6_port);
3337c478bd9Sstevel@tonic-gate 			} else {
3347c478bd9Sstevel@tonic-gate 				RPCLOG(8, "bindresvport: port assigned %d\n",
3357c478bd9Sstevel@tonic-gate 					sin->sin_port);
3367c478bd9Sstevel@tonic-gate 				*last_used = ntohs(sin->sin_port);
3377c478bd9Sstevel@tonic-gate 			}
3387c478bd9Sstevel@tonic-gate 		} else if (loop_twice) {
3397c478bd9Sstevel@tonic-gate 			loop_twice = FALSE;
3407c478bd9Sstevel@tonic-gate 			start = MAX_PRIV;
3417c478bd9Sstevel@tonic-gate 			stop = *last_used + 1;
3427c478bd9Sstevel@tonic-gate 			goto bindresvport_again;
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (!error && bound_addr) {
347*9acbbeafSnn 		if (bound_addr->maxlen < ret->addr.len) {
348*9acbbeafSnn 			kmem_free(bound_addr->buf, bound_addr->maxlen);
349*9acbbeafSnn 			bound_addr->buf = kmem_zalloc(ret->addr.len, KM_SLEEP);
350*9acbbeafSnn 			bound_addr->maxlen = ret->addr.len;
351*9acbbeafSnn 		}
3527c478bd9Sstevel@tonic-gate 		bcopy(ret->addr.buf, bound_addr->buf, ret->addr.len);
3537c478bd9Sstevel@tonic-gate 		bound_addr->len = ret->addr.len;
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 	(void) t_kfree(tiptr, (char *)req, T_BIND);
3567c478bd9Sstevel@tonic-gate 	(void) t_kfree(tiptr, (char *)ret, T_BIND);
3577c478bd9Sstevel@tonic-gate 	return (error);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate void
clnt_init(void)3617c478bd9Sstevel@tonic-gate clnt_init(void)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	clnt_cots_init();
3647c478bd9Sstevel@tonic-gate 	clnt_clts_init();
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate void
clnt_fini(void)3687c478bd9Sstevel@tonic-gate clnt_fini(void)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	clnt_clts_fini();
3717c478bd9Sstevel@tonic-gate 	clnt_cots_fini();
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate call_table_t *
call_table_init(int size)3757c478bd9Sstevel@tonic-gate call_table_init(int size)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	call_table_t *ctp;
3787c478bd9Sstevel@tonic-gate 	int i;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	ctp = kmem_alloc(sizeof (call_table_t) * size, KM_SLEEP);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
3837c478bd9Sstevel@tonic-gate 		ctp[i].ct_call_next = (calllist_t *)&ctp[i];
3847c478bd9Sstevel@tonic-gate 		ctp[i].ct_call_prev = (calllist_t *)&ctp[i];
3857c478bd9Sstevel@tonic-gate 		mutex_init(&ctp[i].ct_lock, NULL, MUTEX_DEFAULT, NULL);
3867c478bd9Sstevel@tonic-gate 		ctp[i].ct_len = 0;
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	return (ctp);
3907c478bd9Sstevel@tonic-gate }
391