xref: /onnv-gate/usr/src/stand/lib/fs/nfs/rpc.c (revision 1230:b5ac481933a3)
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*1230Sss146032  * Common Development and Distribution License (the "License").
6*1230Sss146032  * 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  */
21*1230Sss146032 
220Sstevel@tonic-gate /*
23*1230Sss146032  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  * This file contains a simple implementation of RPC. Standard XDR is
270Sstevel@tonic-gate  * used.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/sysmacros.h>
330Sstevel@tonic-gate #include <rpc/types.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <sys/socket.h>
360Sstevel@tonic-gate #include <netinet/in.h>
370Sstevel@tonic-gate #include "socket_inet.h"
380Sstevel@tonic-gate #include "ipv4.h"
390Sstevel@tonic-gate #include <rpc/xdr.h>
400Sstevel@tonic-gate #include <rpc/auth.h>
410Sstevel@tonic-gate #include <rpc/auth_sys.h>
420Sstevel@tonic-gate #include <rpc/rpc_msg.h>
430Sstevel@tonic-gate #include <sys/t_lock.h>
440Sstevel@tonic-gate #include <netdb.h>
450Sstevel@tonic-gate #include "clnt.h"
460Sstevel@tonic-gate #include <rpc/rpc.h>
470Sstevel@tonic-gate #include "brpc.h"
480Sstevel@tonic-gate #include "auth_inet.h"
490Sstevel@tonic-gate #include "pmap.h"
500Sstevel@tonic-gate #include <sys/promif.h>
510Sstevel@tonic-gate #include "nfs_inet.h"
520Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h>
530Sstevel@tonic-gate #include <rpc/auth_unix.h>
540Sstevel@tonic-gate #include <sys/salib.h>
550Sstevel@tonic-gate #include "mac.h"
560Sstevel@tonic-gate #include <sys/bootdebug.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define	dprintf	if (boothowto & RB_DEBUG) printf
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static struct in_addr cached_destination;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate void
rpc_disperr(struct rpc_err * stat)630Sstevel@tonic-gate rpc_disperr(struct rpc_err *stat)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	if (boothowto & RB_DEBUG) {
660Sstevel@tonic-gate 		switch (stat->re_status) {
670Sstevel@tonic-gate 		case RPC_CANTENCODEARGS:
680Sstevel@tonic-gate 			printf("RPC: Can't encode arguments.\n");
690Sstevel@tonic-gate 			break;
700Sstevel@tonic-gate 		case RPC_CANTDECODERES:
710Sstevel@tonic-gate 			printf("RPC: Can't decode result.\n");
720Sstevel@tonic-gate 			break;
730Sstevel@tonic-gate 		case RPC_CANTSEND:
740Sstevel@tonic-gate 			printf("RPC: Unable to send (%s).\n",
750Sstevel@tonic-gate 			    strerror(errno));
760Sstevel@tonic-gate 			break;
770Sstevel@tonic-gate 		case RPC_CANTRECV:
780Sstevel@tonic-gate 			printf("RPC: Unable to receive (%s).\n",
790Sstevel@tonic-gate 			    strerror(errno));
800Sstevel@tonic-gate 			break;
810Sstevel@tonic-gate 		case RPC_TIMEDOUT:
820Sstevel@tonic-gate 			printf("RPC: Timed out.\n");
830Sstevel@tonic-gate 			break;
840Sstevel@tonic-gate 		case RPC_VERSMISMATCH:
850Sstevel@tonic-gate 			printf("RPC: Incompatible versions of RPC.\n");
860Sstevel@tonic-gate 			break;
870Sstevel@tonic-gate 		case RPC_AUTHERROR:
880Sstevel@tonic-gate 			printf("RPC: Authentication error:\n");
890Sstevel@tonic-gate 			switch (stat->re_why) {
900Sstevel@tonic-gate 			case AUTH_BADCRED:
910Sstevel@tonic-gate 				printf("remote: bogus credentials "
920Sstevel@tonic-gate 				    "(seal broken).\n");
930Sstevel@tonic-gate 				break;
940Sstevel@tonic-gate 			case AUTH_REJECTEDCRED:
950Sstevel@tonic-gate 				printf("remote: client should begin new "
960Sstevel@tonic-gate 				    "session.\n");
970Sstevel@tonic-gate 				break;
980Sstevel@tonic-gate 			case AUTH_BADVERF:
990Sstevel@tonic-gate 				printf("remote: bogus verifier "
1000Sstevel@tonic-gate 				    "(seal broken).\n");
1010Sstevel@tonic-gate 				break;
1020Sstevel@tonic-gate 			case AUTH_REJECTEDVERF:
1030Sstevel@tonic-gate 				printf("remote: verifier expired or was "
1040Sstevel@tonic-gate 				    "replayed.\n");
1050Sstevel@tonic-gate 				break;
1060Sstevel@tonic-gate 			case AUTH_TOOWEAK:
1070Sstevel@tonic-gate 				printf("remote: rejected due to security "
1080Sstevel@tonic-gate 				    "reasons.\n");
1090Sstevel@tonic-gate 				break;
1100Sstevel@tonic-gate 			case AUTH_INVALIDRESP:
1110Sstevel@tonic-gate 				printf("local: bogus response verifier.\n");
1120Sstevel@tonic-gate 				break;
1130Sstevel@tonic-gate 			case AUTH_FAILED:
1140Sstevel@tonic-gate 				/* FALLTHRU */
1150Sstevel@tonic-gate 			default:
1160Sstevel@tonic-gate 				printf("local: unknown error.\n");
1170Sstevel@tonic-gate 				break;
1180Sstevel@tonic-gate 			}
1190Sstevel@tonic-gate 			break;
1200Sstevel@tonic-gate 		case RPC_PROGUNAVAIL:
1210Sstevel@tonic-gate 			printf("RPC: Program unavailable.\n");
1220Sstevel@tonic-gate 			break;
1230Sstevel@tonic-gate 		case RPC_PROGVERSMISMATCH:
1240Sstevel@tonic-gate 			printf("RPC: Program/version mismatch.\n");
1250Sstevel@tonic-gate 			break;
1260Sstevel@tonic-gate 		case RPC_PROCUNAVAIL:
1270Sstevel@tonic-gate 			printf("RPC: Procedure unavailable.\n");
1280Sstevel@tonic-gate 			break;
1290Sstevel@tonic-gate 		case RPC_CANTDECODEARGS:
1300Sstevel@tonic-gate 			printf("RPC: Server can't decode arguments.\n");
1310Sstevel@tonic-gate 			break;
1320Sstevel@tonic-gate 		case RPC_SYSTEMERROR:
1330Sstevel@tonic-gate 			printf("RPC: Remote system error.\n");
1340Sstevel@tonic-gate 			break;
1350Sstevel@tonic-gate 		case RPC_UNKNOWNHOST:
1360Sstevel@tonic-gate 			printf("RPC: Unknown host.\n");
1370Sstevel@tonic-gate 			break;
1380Sstevel@tonic-gate 		case RPC_UNKNOWNPROTO:
1390Sstevel@tonic-gate 			printf("RPC: Unknown protocol.\n");
1400Sstevel@tonic-gate 			break;
1410Sstevel@tonic-gate 		case RPC_PMAPFAILURE:
1420Sstevel@tonic-gate 			printf("RPC: Port mapper failure.\n");
1430Sstevel@tonic-gate 			break;
1440Sstevel@tonic-gate 		case RPC_PROGNOTREGISTERED:
1450Sstevel@tonic-gate 			printf("RPC: Program not registered.\n");
1460Sstevel@tonic-gate 			break;
1470Sstevel@tonic-gate 		case RPC_FAILED:
1480Sstevel@tonic-gate 			printf("RPC: Failed (unspecified error).\n");
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 		default:
1510Sstevel@tonic-gate 			printf("RPC: (unknown error code).\n");
1520Sstevel@tonic-gate 			break;
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * rpc_hdr: sets the fields in the rpc msg header.
1590Sstevel@tonic-gate  *
1600Sstevel@tonic-gate  * Returns: TRUE on success, FALSE if failure.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate /*ARGSUSED*/
1630Sstevel@tonic-gate static bool_t
rpc_hdr(XDR * xdrs,uint_t xid,rpcprog_t prog,rpcvers_t vers,rpcproc_t proc)1640Sstevel@tonic-gate rpc_hdr(XDR *xdrs, uint_t xid, rpcprog_t prog, rpcvers_t vers, rpcproc_t proc)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	struct rpc_msg call_msg;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/* setup header */
1690Sstevel@tonic-gate 	call_msg.rm_xid = xid;
1700Sstevel@tonic-gate 	call_msg.rm_direction = CALL;
1710Sstevel@tonic-gate 	call_msg.rm_call.cb_rpcvers = (rpcvers_t)RPC_MSG_VERSION;
1720Sstevel@tonic-gate 	call_msg.rm_call.cb_prog = prog;
1730Sstevel@tonic-gate 	call_msg.rm_call.cb_vers = vers;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/* xdr the header. */
1760Sstevel@tonic-gate 	if (xdr_callhdr(xdrs, &call_msg) == FALSE)
1770Sstevel@tonic-gate 		return (FALSE);
1780Sstevel@tonic-gate 	else
1790Sstevel@tonic-gate 		return (TRUE);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate  * our version of brpc_call(). We cache in portnumber in to->sin_port for
1840Sstevel@tonic-gate  * your convenience. to and from addresses are taken and received in network
1850Sstevel@tonic-gate  * order.
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate enum clnt_stat
brpc_call(rpcprog_t prog,rpcvers_t vers,rpcproc_t proc,xdrproc_t in_xdr,caddr_t args,xdrproc_t out_xdr,caddr_t ret,int rexmit,int wait_time,struct sockaddr_in * to,struct sockaddr_in * from_who,uint_t auth)1880Sstevel@tonic-gate brpc_call(
1890Sstevel@tonic-gate 	rpcprog_t	prog,		/* rpc program number to call. */
1900Sstevel@tonic-gate 	rpcvers_t	vers,		/* rpc program version */
1910Sstevel@tonic-gate 	rpcproc_t	proc,		/* rpc procedure to call */
1920Sstevel@tonic-gate 	xdrproc_t	in_xdr,		/* routine to serialize arguments */
1930Sstevel@tonic-gate 	caddr_t		args,		/* arg vector for remote call */
1940Sstevel@tonic-gate 	xdrproc_t	out_xdr,	/* routine to deserialize results */
1950Sstevel@tonic-gate 	caddr_t		ret,		/* addr of buf to place results in */
1960Sstevel@tonic-gate 	int		rexmit,		/* retransmission interval (secs) */
1970Sstevel@tonic-gate 	int		wait_time,	/* how long (secs) to wait (resp) */
1980Sstevel@tonic-gate 	struct sockaddr_in 	*to,		/* destination */
1990Sstevel@tonic-gate 	struct sockaddr_in	*from_who,	/* responder's port/address */
2000Sstevel@tonic-gate 	uint_t			auth)		/* type of auth wanted. */
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	int s;
2030Sstevel@tonic-gate 	char hostname[MAXHOSTNAMELEN];
2040Sstevel@tonic-gate 	struct sockaddr_in from;	/* us. */
2050Sstevel@tonic-gate 	socklen_t from_len;
2060Sstevel@tonic-gate 	XDR xmit_xdrs, rcv_xdrs;	/* xdr memory */
2070Sstevel@tonic-gate 	AUTH *xmit_auth;		/* our chosen auth cookie */
2080Sstevel@tonic-gate 	gid_t fake_gids = 1;		/* fake gids list for auth_unix */
2090Sstevel@tonic-gate 	caddr_t trm_msg, rcv_msg;	/* outgoing/incoming rpc mesgs */
2100Sstevel@tonic-gate 	struct rpc_msg reply;		/* our reply msg header */
2110Sstevel@tonic-gate 	int trm_len, rcv_len;
2120Sstevel@tonic-gate 	struct rpc_err rpc_error;	/* to store RPC errors in on rcv. */
2130Sstevel@tonic-gate 	static uint_t xid;		/* current xid */
2140Sstevel@tonic-gate 	uint_t xmit_len;		/* How much of the buffer we used */
2150Sstevel@tonic-gate 	int nrefreshes = 2;		/* # of times to refresh cred */
2160Sstevel@tonic-gate 	int flags = 0;			/* send flags */
2170Sstevel@tonic-gate 	uint_t xdelay;
2180Sstevel@tonic-gate 	int errors, preserve_errno;
2190Sstevel@tonic-gate 	uint32_t timeout;
2200Sstevel@tonic-gate 	socklen_t optlen;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	xmit_auth = NULL;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	trm_len = mac_get_mtu();
2250Sstevel@tonic-gate 	trm_msg = bkmem_alloc(trm_len);
2260Sstevel@tonic-gate 	rcv_msg = bkmem_alloc(NFSBUF_SIZE);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (trm_msg == NULL || rcv_msg == NULL) {
2290Sstevel@tonic-gate 		errno = ENOMEM;
2300Sstevel@tonic-gate 		rpc_error.re_status = RPC_CANTSEND;
2310Sstevel@tonic-gate 		goto gt_error;
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
2350Sstevel@tonic-gate 		rpc_error.re_status = RPC_CANTSEND;
2360Sstevel@tonic-gate 		goto gt_error;
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (dontroute) {
2400Sstevel@tonic-gate 		(void) setsockopt(s, SOL_SOCKET, SO_DONTROUTE,
2410Sstevel@tonic-gate 		    (const void *)&dontroute, sizeof (dontroute));
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	if (to->sin_addr.s_addr == cached_destination.s_addr) {
2450Sstevel@tonic-gate 		optlen = sizeof (timeout);
2460Sstevel@tonic-gate 		(void) getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
2470Sstevel@tonic-gate 		    &optlen);
2480Sstevel@tonic-gate 	} else {
2490Sstevel@tonic-gate 		cached_destination.s_addr = htonl(INADDR_ANY);
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/* Bind our endpoint. */
2530Sstevel@tonic-gate 	from.sin_family = AF_INET;
2540Sstevel@tonic-gate 	ipv4_getipaddr(&from.sin_addr);
2550Sstevel@tonic-gate 	from.sin_addr.s_addr = htonl(from.sin_addr.s_addr);
2560Sstevel@tonic-gate 	from.sin_port = get_source_port(B_TRUE);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	if (bind(s, (struct sockaddr *)&from, sizeof (from)) < 0) {
2590Sstevel@tonic-gate 		rpc_error.re_status = RPC_CANTSEND;
2600Sstevel@tonic-gate 		goto gt_error;
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	bzero((caddr_t)&rpc_error, sizeof (struct rpc_err));
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	/* initialize reply's rpc_msg struct, so we can decode later. */
2660Sstevel@tonic-gate 	reply.acpted_rply.ar_verf = _null_auth;	/* struct copy */
2670Sstevel@tonic-gate 	reply.acpted_rply.ar_results.where = ret;
2680Sstevel@tonic-gate 	reply.acpted_rply.ar_results.proc = out_xdr;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (ntohs(to->sin_port) == 0) {
2710Sstevel@tonic-gate 		/* snag the udp port we need. */
2720Sstevel@tonic-gate 		if ((to->sin_port = (in_port_t)bpmap_getport(prog, vers,
2730Sstevel@tonic-gate 		    &(rpc_error.re_status), to, NULL)) == 0)
2740Sstevel@tonic-gate 			goto gt_error;
2750Sstevel@tonic-gate 		to->sin_port = htons(to->sin_port);
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/* generate xid - increment */
2790Sstevel@tonic-gate 	if (xid == 0)
2800Sstevel@tonic-gate 		xid = (uint_t)(prom_gettime() / 1000) + 1;
2810Sstevel@tonic-gate 	else
2820Sstevel@tonic-gate 		xid++;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	/* set up outgoing pkt as xdr modified. */
2850Sstevel@tonic-gate 	xdrmem_create(&xmit_xdrs, trm_msg, trm_len, XDR_ENCODE);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	/* setup rpc header */
2880Sstevel@tonic-gate 	if (rpc_hdr(&xmit_xdrs, xid, prog, vers, proc) != TRUE) {
2890Sstevel@tonic-gate 		dprintf("brpc_call: cannot setup rpc header.\n");
2900Sstevel@tonic-gate 		rpc_error.re_status = RPC_FAILED;
2910Sstevel@tonic-gate 		goto gt_error;
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/* setup authentication */
2950Sstevel@tonic-gate 	switch (auth) {
2960Sstevel@tonic-gate 	case AUTH_NONE:
2970Sstevel@tonic-gate 		xmit_auth = authnone_create();
2980Sstevel@tonic-gate 		break;
2990Sstevel@tonic-gate 	case AUTH_UNIX:
3000Sstevel@tonic-gate 		/*
3010Sstevel@tonic-gate 		 * Assumes we've configured the stack and thus know our
3020Sstevel@tonic-gate 		 * IP address/hostname, either by using DHCP or rarp/bootparams.
3030Sstevel@tonic-gate 		 */
3040Sstevel@tonic-gate 		gethostname(hostname, sizeof (hostname));
3050Sstevel@tonic-gate 		xmit_auth = authunix_create(hostname, 0, 1, 1, &fake_gids);
3060Sstevel@tonic-gate 		break;
3070Sstevel@tonic-gate 	default:
3080Sstevel@tonic-gate 		dprintf("brpc_call: Unsupported authentication type: %d\n",
3090Sstevel@tonic-gate 		    auth);
3100Sstevel@tonic-gate 		rpc_error.re_status = RPC_AUTHERROR;
3110Sstevel@tonic-gate 		goto gt_error;
3120Sstevel@tonic-gate 	/*NOTREACHED*/
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/*
3160Sstevel@tonic-gate 	 * rpc_hdr puts everything in the xmit buffer for the header
3170Sstevel@tonic-gate 	 * EXCEPT the proc. Put it, and our authentication info into
3180Sstevel@tonic-gate 	 * it now, serializing as we go. We will be at the place where
3190Sstevel@tonic-gate 	 * we left off.
3200Sstevel@tonic-gate 	 */
3210Sstevel@tonic-gate 	xmit_xdrs.x_op = XDR_ENCODE;
3220Sstevel@tonic-gate 	if ((XDR_PUTINT32(&xmit_xdrs, (int32_t *)&proc) == FALSE) ||
3230Sstevel@tonic-gate 	    (AUTH_MARSHALL(xmit_auth, &xmit_xdrs, NULL) == FALSE) ||
3240Sstevel@tonic-gate 	    ((*in_xdr)(&xmit_xdrs, args) == FALSE)) {
3250Sstevel@tonic-gate 		rpc_error.re_status = RPC_CANTENCODEARGS;
3260Sstevel@tonic-gate 		goto gt_error;
3270Sstevel@tonic-gate 	} else
3280Sstevel@tonic-gate 		xmit_len = (int)XDR_GETPOS(&xmit_xdrs); /* for sendto */
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * Right now the outgoing packet should be all serialized and
3320Sstevel@tonic-gate 	 * ready to go... Set up timers.
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	xdelay = (rexmit == 0) ? RPC_REXMIT_MSEC : (rexmit * 1000);
3360Sstevel@tonic-gate 	(void) setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (void *)&xdelay,
3370Sstevel@tonic-gate 	    sizeof (xdelay));
3380Sstevel@tonic-gate 	wait_time = (wait_time == 0) ? RPC_RCVWAIT_MSEC : (wait_time * 1000);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	wait_time += prom_gettime();
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	/*
3430Sstevel@tonic-gate 	 * send out the request. The first item in the receive buffer will
3440Sstevel@tonic-gate 	 * be the xid. Check if it is correct.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	errors = 0;
3470Sstevel@tonic-gate 	rpc_error.re_status = RPC_TIMEDOUT;
3480Sstevel@tonic-gate 	do {
3490Sstevel@tonic-gate 		if (sendto(s, trm_msg, xmit_len, flags, (struct sockaddr *)to,
3500Sstevel@tonic-gate 		    sizeof (struct sockaddr_in)) < 0) {
351*1230Sss146032 			/*
352*1230Sss146032 			 * If errno is set to ETIMEDOUT, return
353*1230Sss146032 			 * with RPC status as RPC_TIMEDOUT. Calling
354*1230Sss146032 			 * funciton will take care of this error by
355*1230Sss146032 			 * retrying the RPC call.
356*1230Sss146032 			 */
357*1230Sss146032 			if (errno == ETIMEDOUT) {
358*1230Sss146032 				rpc_error.re_status = RPC_TIMEDOUT;
359*1230Sss146032 			} else {
360*1230Sss146032 				rpc_error.re_status = RPC_CANTSEND;
361*1230Sss146032 			}
3620Sstevel@tonic-gate 			goto gt_error;
3630Sstevel@tonic-gate 		}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 		from_len = sizeof (struct sockaddr_in);
3660Sstevel@tonic-gate 		while ((rcv_len = recvfrom(s, rcv_msg, NFSBUF_SIZE,
3670Sstevel@tonic-gate 		    MSG_DONTWAIT, (struct sockaddr *)from_who,
3680Sstevel@tonic-gate 		    &from_len)) > 0 || errors < RPC_ALLOWABLE_ERRORS) {
3690Sstevel@tonic-gate 			if (rcv_len < 0) {
3700Sstevel@tonic-gate 				if (errno == EWOULDBLOCK ||
3710Sstevel@tonic-gate 				    errno == ETIMEDOUT) {
3720Sstevel@tonic-gate 					break; /* timeout */
3730Sstevel@tonic-gate 				}
3740Sstevel@tonic-gate 				rpc_error.re_status = RPC_CANTRECV;
3750Sstevel@tonic-gate 				goto gt_error;
3760Sstevel@tonic-gate 			}
3770Sstevel@tonic-gate 			if (ntohl(*((uint32_t *)(rcv_msg))) != xid) {
3780Sstevel@tonic-gate 				dprintf("brpc_call: xid: 0x%x != 0x%x\n",
3790Sstevel@tonic-gate 				    *(uint32_t *)(rcv_msg), xid);
3800Sstevel@tonic-gate 				continue;
3810Sstevel@tonic-gate 			}
3820Sstevel@tonic-gate 			/*
3830Sstevel@tonic-gate 			 * Let's deserialize the data into our 'ret' buffer.
3840Sstevel@tonic-gate 			 */
3850Sstevel@tonic-gate 			xdrmem_create(&rcv_xdrs, rcv_msg, rcv_len, XDR_DECODE);
3860Sstevel@tonic-gate 			if (xdr_replymsg(&rcv_xdrs, &reply) == FALSE) {
3870Sstevel@tonic-gate 				rpc_error.re_status = RPC_CANTDECODERES;
3880Sstevel@tonic-gate 				goto gt_error;
3890Sstevel@tonic-gate 			}
3900Sstevel@tonic-gate 			_seterr_reply(&reply, &rpc_error);
3910Sstevel@tonic-gate 			switch (rpc_error.re_status) {
3920Sstevel@tonic-gate 			case RPC_SUCCESS:
3930Sstevel@tonic-gate 				/*
3940Sstevel@tonic-gate 				 * XXX - validate for unix and none
3950Sstevel@tonic-gate 				 * always return true.
3960Sstevel@tonic-gate 				 */
3970Sstevel@tonic-gate 				if (AUTH_VALIDATE(xmit_auth,
3980Sstevel@tonic-gate 				    &reply.acpted_rply.ar_verf) == FALSE) {
3990Sstevel@tonic-gate 					rpc_error.re_status = RPC_AUTHERROR;
4000Sstevel@tonic-gate 					rpc_error.re_why = AUTH_INVALIDRESP;
4010Sstevel@tonic-gate 					errors++;
4020Sstevel@tonic-gate 				}
4030Sstevel@tonic-gate 				if (reply.acpted_rply.ar_verf.oa_base !=
4040Sstevel@tonic-gate 				    0) {
4050Sstevel@tonic-gate 					xmit_xdrs.x_op = XDR_FREE;
4060Sstevel@tonic-gate 					(void) xdr_opaque_auth(
4070Sstevel@tonic-gate 					    &xmit_xdrs,
4080Sstevel@tonic-gate 					    &reply.acpted_rply.ar_verf);
4090Sstevel@tonic-gate 				}
4100Sstevel@tonic-gate 				break;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 			case RPC_AUTHERROR:
4130Sstevel@tonic-gate 				/*
4140Sstevel@tonic-gate 				 * Let's see if our credentials need
4150Sstevel@tonic-gate 				 * refreshing
4160Sstevel@tonic-gate 				 */
4170Sstevel@tonic-gate 				if (nrefreshes > 0 && AUTH_REFRESH(xmit_auth,
4180Sstevel@tonic-gate 				    NULL, NULL)) {
4190Sstevel@tonic-gate 					nrefreshes--;
4200Sstevel@tonic-gate 				}
4210Sstevel@tonic-gate 				errors++;
4220Sstevel@tonic-gate 				break;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 			case RPC_PROCUNAVAIL:
4250Sstevel@tonic-gate 				/*
4260Sstevel@tonic-gate 				 * Might be a silly portmapper implementation
4270Sstevel@tonic-gate 				 * erroneously responding to our rpc broadcast
4280Sstevel@tonic-gate 				 * indirect portmapper call. For this
4290Sstevel@tonic-gate 				 * particular case, we don't increment the
4300Sstevel@tonic-gate 				 * error counter because we want to keep
4310Sstevel@tonic-gate 				 * sifting for successful replies...
4320Sstevel@tonic-gate 				 */
4330Sstevel@tonic-gate 				if (to->sin_addr.s_addr !=
4340Sstevel@tonic-gate 				    ntohl(INADDR_BROADCAST))
4350Sstevel@tonic-gate 					errors++;
4360Sstevel@tonic-gate 				break;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 			case RPC_PROGVERSMISMATCH:
4390Sstevel@tonic-gate 				/*
4400Sstevel@tonic-gate 				 * Successfully talked to server, but they
4410Sstevel@tonic-gate 				 * don't speak our lingo.
4420Sstevel@tonic-gate 				 */
4430Sstevel@tonic-gate 				goto gt_error;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 			default:
4460Sstevel@tonic-gate 				/* Just keep trying till there's no data... */
4470Sstevel@tonic-gate 				errors++;
4480Sstevel@tonic-gate 				break;
4490Sstevel@tonic-gate 			}
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 			if (rpc_error.re_status != RPC_SUCCESS) {
4520Sstevel@tonic-gate 				dprintf("brpc_call: from: %s, error: ",
4530Sstevel@tonic-gate 				    inet_ntoa(from_who->sin_addr));
4540Sstevel@tonic-gate 				rpc_disperr(&rpc_error);
4550Sstevel@tonic-gate 			} else
4560Sstevel@tonic-gate 				break;
4570Sstevel@tonic-gate 		}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 		/*
4600Sstevel@tonic-gate 		 * If we're having trouble reassembling datagrams, let the
4610Sstevel@tonic-gate 		 * application know ASAP so that it can take the appropriate
4620Sstevel@tonic-gate 		 * actions.
4630Sstevel@tonic-gate 		 */
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	} while (rpc_error.re_status != RPC_SUCCESS && errno != ETIMEDOUT &&
4660Sstevel@tonic-gate 	    prom_gettime() < wait_time);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate gt_error:
4690Sstevel@tonic-gate 	if (xmit_auth != NULL)
4700Sstevel@tonic-gate 		AUTH_DESTROY(xmit_auth);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (trm_msg != NULL)
4730Sstevel@tonic-gate 		bkmem_free(trm_msg, trm_len);
4740Sstevel@tonic-gate 	if (rcv_msg != NULL)
4750Sstevel@tonic-gate 		bkmem_free(rcv_msg, NFSBUF_SIZE);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	if (rpc_error.re_status != RPC_SUCCESS)
4780Sstevel@tonic-gate 		rpc_disperr(&rpc_error);
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	/*
4810Sstevel@tonic-gate 	 * socket calls reset errno. Since we want to hold onto the errno
4820Sstevel@tonic-gate 	 * value if it is ETIMEDOUT to communicate to our caller that this
4830Sstevel@tonic-gate 	 * RPC_TIMEDOUT situation is due to a stack problem (we're getting
4840Sstevel@tonic-gate 	 * a reply, but the stack simply can't assemble it.), we need to
4850Sstevel@tonic-gate 	 * preserve errno's value over the socket_close().
4860Sstevel@tonic-gate 	 */
4870Sstevel@tonic-gate 	preserve_errno = (errno == ETIMEDOUT) ? errno : 0;
4880Sstevel@tonic-gate 	(void) socket_close(s);
4890Sstevel@tonic-gate 	errno = preserve_errno;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	return (rpc_error.re_status);
4920Sstevel@tonic-gate }
493