xref: /illumos-gate/usr/src/stand/lib/fs/nfs/clnt_budp.c (revision a2cd9e18)
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
59f1fc992Sss  * Common Development and Distribution License (the "License").
69f1fc992Sss  * 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  */
219f1fc992Sss 
227c478bd9Sstevel@tonic-gate /*
239f1fc992Sss  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * Boot subsystem client side rpc
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <rpc/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/socket.h>
427c478bd9Sstevel@tonic-gate #include <netinet/in.h>
437c478bd9Sstevel@tonic-gate #include "socket_inet.h"
447c478bd9Sstevel@tonic-gate #include "ipv4.h"
457c478bd9Sstevel@tonic-gate #include "clnt.h"
467c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
477c478bd9Sstevel@tonic-gate #include "brpc.h"
487c478bd9Sstevel@tonic-gate #include "pmap.h"
497c478bd9Sstevel@tonic-gate #include <sys/promif.h>
507c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
517c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
527c478bd9Sstevel@tonic-gate #include <rpc/auth_sys.h>
537c478bd9Sstevel@tonic-gate #include "auth_inet.h"
547c478bd9Sstevel@tonic-gate #include <rpc/rpc_msg.h>
557c478bd9Sstevel@tonic-gate #include <sys/salib.h>
567c478bd9Sstevel@tonic-gate #include <sys/bootdebug.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	dprintf if (boothowto & RB_DEBUG) printf
597c478bd9Sstevel@tonic-gate 
609f1fc992Sss /* retries to send RPC message when sendto fails */
619f1fc992Sss #define	RPC_UDP_SEND_RETRIES	3
629f1fc992Sss 
637c478bd9Sstevel@tonic-gate extern int errno;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * If we create another clnt type this should be
677c478bd9Sstevel@tonic-gate  * moved to a common file
687c478bd9Sstevel@tonic-gate  */
69*a2cd9e18SToomas Soome extern struct rpc_createerr rpc_createerr;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static struct clnt_ops *clntbudp_ops();
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Private data kept per client handle
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate struct cu_data {
777c478bd9Sstevel@tonic-gate 	int		   cu_sock;
787c478bd9Sstevel@tonic-gate 	bool_t		   cu_closeit;
797c478bd9Sstevel@tonic-gate 	struct sockaddr_in cu_raddr;
807c478bd9Sstevel@tonic-gate 	int		   cu_rlen;
817c478bd9Sstevel@tonic-gate 	struct timeval	   cu_wait;
827c478bd9Sstevel@tonic-gate 	struct timeval	   cu_total;
837c478bd9Sstevel@tonic-gate 	struct rpc_err	   cu_error;
847c478bd9Sstevel@tonic-gate 	XDR		   cu_outxdrs;
857c478bd9Sstevel@tonic-gate 	uint_t		   cu_xdrpos;
867c478bd9Sstevel@tonic-gate 	uint_t		   cu_sendsz;
877c478bd9Sstevel@tonic-gate 	char		   *cu_outbuf;
887c478bd9Sstevel@tonic-gate 	uint_t		   cu_recvsz;
897c478bd9Sstevel@tonic-gate 	char		   cu_inbuf[1];
907c478bd9Sstevel@tonic-gate };
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * Create a UDP based client handle.
947c478bd9Sstevel@tonic-gate  * If *sockp<0, *sockp is set to a newly created UPD socket.
957c478bd9Sstevel@tonic-gate  * If raddr->sin_port is 0 a binder on the remote machine
967c478bd9Sstevel@tonic-gate  * is consulted for the correct port number.
977c478bd9Sstevel@tonic-gate  * NB: It is the clients responsibility to close *sockp.
987c478bd9Sstevel@tonic-gate  * NB: The rpch->cl_auth is initialized to null authentication.
997c478bd9Sstevel@tonic-gate  *	Caller may wish to set this something more useful.
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  * wait is the amount of time used between retransmitting a call if
1027c478bd9Sstevel@tonic-gate  * no response has been heard;  retransmition occurs until the actual
1037c478bd9Sstevel@tonic-gate  * rpc call times out.
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  * sendsz and recvsz are the maximum allowable packet sizes that can be
1067c478bd9Sstevel@tonic-gate  * sent and received.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate CLIENT *
clntbudp_bufcreate(struct sockaddr_in * raddr,rpcprog_t program,rpcvers_t version,struct timeval wait,int * sockp,uint_t sendsz,uint_t recvsz)109*a2cd9e18SToomas Soome clntbudp_bufcreate(struct sockaddr_in *raddr, rpcprog_t program,
110*a2cd9e18SToomas Soome     rpcvers_t version, struct timeval wait, int *sockp, uint_t sendsz,
111*a2cd9e18SToomas Soome     uint_t recvsz)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	CLIENT *cl;
1147c478bd9Sstevel@tonic-gate 	struct cu_data *cu;
1157c478bd9Sstevel@tonic-gate 	struct rpc_msg call_msg;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	cl = (CLIENT *)bkmem_alloc(sizeof (CLIENT));
1187c478bd9Sstevel@tonic-gate 	if (cl == NULL) {
1197c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1207c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1217c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = errno;
1227c478bd9Sstevel@tonic-gate 		return ((CLIENT *)NULL);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	sendsz = ((sendsz + 3) / 4) * 4;
1257c478bd9Sstevel@tonic-gate 	recvsz = ((recvsz + 3) / 4) * 4;
1267c478bd9Sstevel@tonic-gate 	cu = (struct cu_data *)bkmem_alloc(sizeof (*cu) + sendsz + recvsz);
1277c478bd9Sstevel@tonic-gate 	if (cu == NULL) {
1287c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1297c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1307c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = errno;
1317c478bd9Sstevel@tonic-gate 		goto fooy;
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	cu->cu_outbuf = &cu->cu_inbuf[recvsz];
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if (raddr->sin_port == 0) {
1367c478bd9Sstevel@tonic-gate 		ushort_t port;
1377c478bd9Sstevel@tonic-gate 		if ((port = bpmap_getport(program, version,
138*a2cd9e18SToomas Soome 		    &(rpc_createerr.cf_stat), raddr, NULL)) == 0) {
1397c478bd9Sstevel@tonic-gate 			goto fooy;
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 		raddr->sin_port = htons(port);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	cl->cl_ops = clntbudp_ops();
1447c478bd9Sstevel@tonic-gate 	cl->cl_private = (caddr_t)cu;
1457c478bd9Sstevel@tonic-gate 	cu->cu_raddr = *raddr;
1467c478bd9Sstevel@tonic-gate 	cu->cu_rlen = sizeof (cu->cu_raddr);
1477c478bd9Sstevel@tonic-gate 	cu->cu_wait = wait;
1487c478bd9Sstevel@tonic-gate 	cu->cu_total.tv_sec = -1;
1497c478bd9Sstevel@tonic-gate 	cu->cu_total.tv_usec = -1;
1507c478bd9Sstevel@tonic-gate 	cu->cu_sendsz = sendsz;
1517c478bd9Sstevel@tonic-gate 	cu->cu_recvsz = recvsz;
1527c478bd9Sstevel@tonic-gate 	call_msg.rm_xid = (uint_t)prom_gettime() + 1;
1537c478bd9Sstevel@tonic-gate 	call_msg.rm_direction = CALL;
1547c478bd9Sstevel@tonic-gate 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
1557c478bd9Sstevel@tonic-gate 	call_msg.rm_call.cb_prog = program;
1567c478bd9Sstevel@tonic-gate 	call_msg.rm_call.cb_vers = version;
1577c478bd9Sstevel@tonic-gate 	xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
1587c478bd9Sstevel@tonic-gate 	    sendsz, XDR_ENCODE);
1597c478bd9Sstevel@tonic-gate 	if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
1607c478bd9Sstevel@tonic-gate 		goto fooy;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
1637c478bd9Sstevel@tonic-gate 	cu->cu_closeit = FALSE;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (*sockp < 0) {
1667c478bd9Sstevel@tonic-gate 		struct sockaddr_in from;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		*sockp = socket(PF_INET, SOCK_DGRAM, 0);
1697c478bd9Sstevel@tonic-gate 		if (*sockp < 0) {
1707c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1717c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
1727c478bd9Sstevel@tonic-gate 			goto fooy;
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		if (dontroute) {
1767c478bd9Sstevel@tonic-gate 			(void) setsockopt(*sockp, SOL_SOCKET, SO_DONTROUTE,
177*a2cd9e18SToomas Soome 			    (const void *)&dontroute, sizeof (dontroute));
1787c478bd9Sstevel@tonic-gate 		}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		/* attempt to bind to priv port */
1817c478bd9Sstevel@tonic-gate 		from.sin_family = AF_INET;
1827c478bd9Sstevel@tonic-gate 		ipv4_getipaddr(&from.sin_addr);
1837c478bd9Sstevel@tonic-gate 		from.sin_addr.s_addr = htonl(from.sin_addr.s_addr);
1847c478bd9Sstevel@tonic-gate 		from.sin_port = get_source_port(TRUE);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		if (bind(*sockp, (struct sockaddr *)&from, sizeof (from)) < 0) {
1877c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1887c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
1897c478bd9Sstevel@tonic-gate 			goto fooy;
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		cu->cu_closeit = TRUE;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	cu->cu_sock = *sockp;
1967c478bd9Sstevel@tonic-gate 	cl->cl_auth = authnone_create();
1977c478bd9Sstevel@tonic-gate 	return (cl);
1987c478bd9Sstevel@tonic-gate fooy:
1997c478bd9Sstevel@tonic-gate 	if (cu)
2007c478bd9Sstevel@tonic-gate 		bkmem_free((caddr_t)cu, sizeof (*cu) + sendsz + recvsz);
2017c478bd9Sstevel@tonic-gate 	if (cl)
2027c478bd9Sstevel@tonic-gate 		bkmem_free((caddr_t)cl, sizeof (CLIENT));
2037c478bd9Sstevel@tonic-gate 	return ((CLIENT *)NULL);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate CLIENT *
clntbudp_create(struct sockaddr_in * raddr,rpcprog_t program,rpcvers_t version,struct timeval wait,int * sockp)207*a2cd9e18SToomas Soome clntbudp_create(struct sockaddr_in *raddr, rpcprog_t program,
208*a2cd9e18SToomas Soome     rpcvers_t version, struct timeval wait, int *sockp)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	return (clntbudp_bufcreate(raddr, program, version, wait, sockp,
2127c478bd9Sstevel@tonic-gate 	    UDPMSGSIZE, UDPMSGSIZE));
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate static enum clnt_stat
clntbudp_call(CLIENT * cl,rpcproc_t proc,xdrproc_t xargs,caddr_t argsp,xdrproc_t xresults,caddr_t resultsp,struct timeval utimeout)216*a2cd9e18SToomas Soome clntbudp_call(CLIENT *cl, rpcproc_t proc, xdrproc_t xargs, caddr_t argsp,
217*a2cd9e18SToomas Soome     xdrproc_t  xresults, caddr_t resultsp, struct timeval utimeout)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	struct cu_data *cu;
2207c478bd9Sstevel@tonic-gate 	XDR *xdrs;
2217c478bd9Sstevel@tonic-gate 	int outlen;
2227c478bd9Sstevel@tonic-gate 	int inlen;
2237c478bd9Sstevel@tonic-gate 	socklen_t fromlen;
2247c478bd9Sstevel@tonic-gate 	struct sockaddr_in from;
2257c478bd9Sstevel@tonic-gate 	struct rpc_msg reply_msg;
2267c478bd9Sstevel@tonic-gate 	XDR reply_xdrs;
2277c478bd9Sstevel@tonic-gate 	uint_t xdelay;
2287c478bd9Sstevel@tonic-gate 	int wait_time;
2297c478bd9Sstevel@tonic-gate 	bool_t ok;
2307c478bd9Sstevel@tonic-gate 	int nrefreshes = 2;	/* number of times to refresh cred */
2317c478bd9Sstevel@tonic-gate 	struct timeval timeout;
2327c478bd9Sstevel@tonic-gate 	int errors;
2339f1fc992Sss 	short send_retries = RPC_UDP_SEND_RETRIES;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	cu = (struct cu_data *)cl->cl_private;
2367c478bd9Sstevel@tonic-gate 	if (cu->cu_total.tv_usec == -1)
2377c478bd9Sstevel@tonic-gate 		timeout = utimeout;	/* use supplied timeout */
2387c478bd9Sstevel@tonic-gate 	else
2397c478bd9Sstevel@tonic-gate 		timeout = cu->cu_total; /* use default timeout */
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * set a media level timeout
2437c478bd9Sstevel@tonic-gate 	 */
2447c478bd9Sstevel@tonic-gate 	xdelay = cu->cu_wait.tv_sec + 1000 + cu->cu_wait.tv_usec / 1000;
2457c478bd9Sstevel@tonic-gate 	(void) setsockopt(cu->cu_sock, SOL_SOCKET, SO_RCVTIMEO,
246*a2cd9e18SToomas Soome 	    (void *)&xdelay, sizeof (xdelay));
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	wait_time = (timeout.tv_sec * 1000) + (timeout.tv_usec / 1000);
2497c478bd9Sstevel@tonic-gate 	if (wait_time == 0)
2507c478bd9Sstevel@tonic-gate 		wait_time = RPC_RCVWAIT_MSEC;
2517c478bd9Sstevel@tonic-gate 	wait_time += prom_gettime();
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	errors = 0;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate call_again:
2567c478bd9Sstevel@tonic-gate 	xdrs = &(cu->cu_outxdrs);
2577c478bd9Sstevel@tonic-gate 	xdrs->x_op = XDR_ENCODE;
258b531f6d1SToomas Soome 	(void) XDR_SETPOS(xdrs, cu->cu_xdrpos);
2597c478bd9Sstevel@tonic-gate 	/*
2607c478bd9Sstevel@tonic-gate 	 * the transaction is the first thing in the out buffer
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	(*(ushort_t *)(cu->cu_outbuf))++;
2637c478bd9Sstevel@tonic-gate 	if ((! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
2647c478bd9Sstevel@tonic-gate 	    (! AUTH_MARSHALL(cl->cl_auth, xdrs, NULL)) ||
2657c478bd9Sstevel@tonic-gate 	    (! (*xargs)(xdrs, argsp)))
2667c478bd9Sstevel@tonic-gate 		return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
2677c478bd9Sstevel@tonic-gate 	outlen = (int)XDR_GETPOS(xdrs);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate send_again:
2707c478bd9Sstevel@tonic-gate 	if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
2717c478bd9Sstevel@tonic-gate 	    (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen)
2727c478bd9Sstevel@tonic-gate 	    != outlen) {
2739f1fc992Sss 		if (errno == ETIMEDOUT) {
2749f1fc992Sss 			/*
2759f1fc992Sss 			 * sendto() times out probably because
2769f1fc992Sss 			 * ARP times out while waiting for reply.
2779f1fc992Sss 			 * We retry sending RPC message again.
2789f1fc992Sss 			 */
2799f1fc992Sss 			if (send_retries-- > 0) {
2809f1fc992Sss 				dprintf("clntbudp_call: timedout, try sending"
2819f1fc992Sss 				    "RPC again\n");
2829f1fc992Sss 				errno = 0;
2839f1fc992Sss 				goto send_again;
2849f1fc992Sss 			}
2859f1fc992Sss 			cu->cu_error.re_status = RPC_TIMEDOUT;
2869f1fc992Sss 		} else {
2879f1fc992Sss 			cu->cu_error.re_status = RPC_CANTSEND;
2889f1fc992Sss 		}
2897c478bd9Sstevel@tonic-gate 		cu->cu_error.re_errno = errno;
2909f1fc992Sss 		return (cu->cu_error.re_status);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * sub-optimal code appears here because we have
2957c478bd9Sstevel@tonic-gate 	 * some clock time to spare while the packets are in flight.
2967c478bd9Sstevel@tonic-gate 	 * (We assume that this is actually only executed once.)
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate recv_again:
2997c478bd9Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_verf = _null_auth;
3007c478bd9Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_results.where = resultsp;
3017c478bd9Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_results.proc = xresults;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	for (;;) {
3047c478bd9Sstevel@tonic-gate 		if (errors >= RPC_ALLOWABLE_ERRORS)
3057c478bd9Sstevel@tonic-gate 			return (cu->cu_error.re_status);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		if (prom_gettime() >= wait_time) {
3087c478bd9Sstevel@tonic-gate 			cu->cu_error.re_errno = ETIMEDOUT;
3097c478bd9Sstevel@tonic-gate 			return (cu->cu_error.re_status = RPC_TIMEDOUT);
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 		/*
3137c478bd9Sstevel@tonic-gate 		 * Use MSG_DONTWAIT because we have set
3147c478bd9Sstevel@tonic-gate 		 * a media level timeout above.
3157c478bd9Sstevel@tonic-gate 		 */
3167c478bd9Sstevel@tonic-gate 		fromlen = sizeof (struct sockaddr);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
319*a2cd9e18SToomas Soome 		    (int)cu->cu_recvsz, MSG_DONTWAIT,
320*a2cd9e18SToomas Soome 		    (struct sockaddr *)&from, &fromlen);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		if (inlen < 0) {
3237c478bd9Sstevel@tonic-gate 			if (errno == EWOULDBLOCK) {
3247c478bd9Sstevel@tonic-gate 				/*
3257c478bd9Sstevel@tonic-gate 				 * Media level has timedout
3267c478bd9Sstevel@tonic-gate 				 * and no more data in buffers.
3277c478bd9Sstevel@tonic-gate 				 */
3287c478bd9Sstevel@tonic-gate 				goto send_again;
3297c478bd9Sstevel@tonic-gate 			}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 			cu->cu_error.re_status = RPC_CANTRECV;
3327c478bd9Sstevel@tonic-gate 			if (errno == ETIMEDOUT) {
3337c478bd9Sstevel@tonic-gate 				errno = ETIMEDOUT;
3347c478bd9Sstevel@tonic-gate 				cu->cu_error.re_status = RPC_TIMEDOUT;
3357c478bd9Sstevel@tonic-gate 			}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 			cu->cu_error.re_errno = errno;
3387c478bd9Sstevel@tonic-gate 			return (cu->cu_error.re_status);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		if (inlen < sizeof (uint32_t))
3427c478bd9Sstevel@tonic-gate 			continue;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		/* see if reply transaction id matches sent id */
3457c478bd9Sstevel@tonic-gate 		if (*((uint32_t *)(cu->cu_inbuf)) !=
346*a2cd9e18SToomas Soome 		    *((uint32_t *)(cu->cu_outbuf))) {
3477c478bd9Sstevel@tonic-gate 			dprintf("clntbudp_call: xid: 0x%x != 0x%x\n",
348*a2cd9e18SToomas Soome 			    *(uint32_t *)(cu->cu_inbuf),
349*a2cd9e18SToomas Soome 			    *(uint32_t *)(cu->cu_outbuf));
3507c478bd9Sstevel@tonic-gate 			continue;
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 		/* we now assume we have the proper reply */
3537c478bd9Sstevel@tonic-gate 		break;
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/*
3577c478bd9Sstevel@tonic-gate 	 * now decode and validate the response
3587c478bd9Sstevel@tonic-gate 	 */
3597c478bd9Sstevel@tonic-gate 	xdrmem_create(&reply_xdrs, cu->cu_inbuf, (uint_t)inlen, XDR_DECODE);
3607c478bd9Sstevel@tonic-gate 	ok = xdr_replymsg(&reply_xdrs, &reply_msg);
3617c478bd9Sstevel@tonic-gate 	/* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
3627c478bd9Sstevel@tonic-gate 	if (!ok) {
3637c478bd9Sstevel@tonic-gate 		cu->cu_error.re_status = RPC_CANTDECODERES;
3647c478bd9Sstevel@tonic-gate 		return (cu->cu_error.re_status);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	_seterr_reply(&reply_msg, &(cu->cu_error));
3687c478bd9Sstevel@tonic-gate 	if (cu->cu_error.re_status == RPC_SUCCESS) {
3697c478bd9Sstevel@tonic-gate 		if (! AUTH_VALIDATE(cl->cl_auth,
370*a2cd9e18SToomas Soome 		    &reply_msg.acpted_rply.ar_verf)) {
3717c478bd9Sstevel@tonic-gate 			cu->cu_error.re_status = RPC_AUTHERROR;
3727c478bd9Sstevel@tonic-gate 			cu->cu_error.re_why = AUTH_INVALIDRESP;
3737c478bd9Sstevel@tonic-gate 			errors++;
3747c478bd9Sstevel@tonic-gate 			goto call_again;
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
3777c478bd9Sstevel@tonic-gate 			xdrs->x_op = XDR_FREE;
3787c478bd9Sstevel@tonic-gate 			(void) xdr_opaque_auth(xdrs,
3797c478bd9Sstevel@tonic-gate 			    &(reply_msg.acpted_rply.ar_verf));
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 		return (cu->cu_error.re_status);
3827c478bd9Sstevel@tonic-gate 	}  /* end successful completion */
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (cu->cu_error.re_status == RPC_AUTHERROR) {
3857c478bd9Sstevel@tonic-gate 		/* maybe our credentials need to be refreshed ... */
3867c478bd9Sstevel@tonic-gate 		if (nrefreshes > 0 &&
387*a2cd9e18SToomas Soome 		    AUTH_REFRESH(cl->cl_auth, NULL, NULL)) {
3887c478bd9Sstevel@tonic-gate 			nrefreshes--;
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 		errors++;
3917c478bd9Sstevel@tonic-gate 		goto call_again;
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/* Just keep trying till there's no data... */
3957c478bd9Sstevel@tonic-gate 	errors++;
3967c478bd9Sstevel@tonic-gate 	dprintf("clntbudp_call: from: %s, error: ",
397*a2cd9e18SToomas Soome 	    inet_ntoa(from.sin_addr));
3987c478bd9Sstevel@tonic-gate 	rpc_disperr(&cu->cu_error);
3997c478bd9Sstevel@tonic-gate 	goto recv_again;
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate static void
clntbudp_geterr(CLIENT * cl,struct rpc_err * errp)403*a2cd9e18SToomas Soome clntbudp_geterr(CLIENT *cl, struct rpc_err *errp)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	*errp = cu->cu_error;
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate static bool_t
clntbudp_freeres(CLIENT * cl,xdrproc_t xdr_res,caddr_t res_ptr)412*a2cd9e18SToomas Soome clntbudp_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
4157c478bd9Sstevel@tonic-gate 	XDR *xdrs = &(cu->cu_outxdrs);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	xdrs->x_op = XDR_FREE;
4187c478bd9Sstevel@tonic-gate 	return ((*xdr_res)(xdrs, res_ptr));
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate static void
clntbudp_abort(void)422*a2cd9e18SToomas Soome clntbudp_abort(void)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /* ARGSUSED */
4277c478bd9Sstevel@tonic-gate static bool_t
clntbudp_control(CLIENT * cl,int request,char * info)428*a2cd9e18SToomas Soome clntbudp_control(CLIENT *cl, int request, char *info)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	/* CLNT_CONTROL is not used in boot */
4317c478bd9Sstevel@tonic-gate 	return (FALSE);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate static void
clntbudp_destroy(CLIENT * cl)435*a2cd9e18SToomas Soome clntbudp_destroy(CLIENT *cl)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (cu->cu_closeit) {
4407c478bd9Sstevel@tonic-gate 		(void) socket_close(cu->cu_sock);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 	XDR_DESTROY(&(cu->cu_outxdrs));
4437c478bd9Sstevel@tonic-gate 	bkmem_free((caddr_t)cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
4447c478bd9Sstevel@tonic-gate 	bkmem_free((caddr_t)cl, sizeof (CLIENT));
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate static struct clnt_ops *
clntbudp_ops(void)448*a2cd9e18SToomas Soome clntbudp_ops(void)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	static struct clnt_ops ops;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	if (ops.cl_call == NULL) {
4537c478bd9Sstevel@tonic-gate 		ops.cl_call = clntbudp_call;
4547c478bd9Sstevel@tonic-gate 		ops.cl_abort = clntbudp_abort;
4557c478bd9Sstevel@tonic-gate 		ops.cl_geterr = clntbudp_geterr;
4567c478bd9Sstevel@tonic-gate 		ops.cl_freeres = clntbudp_freeres;
4577c478bd9Sstevel@tonic-gate 		ops.cl_destroy = clntbudp_destroy;
4587c478bd9Sstevel@tonic-gate 		ops.cl_control = clntbudp_control;
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	return (&ops);
4617c478bd9Sstevel@tonic-gate }
462