xref: /onnv-gate/usr/src/stand/lib/fs/nfs/clnt_btcp.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28*0Sstevel@tonic-gate /* All Rights Reserved */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * Boot subsystem client side rpc (TCP)
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <sys/salib.h>
42*0Sstevel@tonic-gate #include <sys/errno.h>
43*0Sstevel@tonic-gate #include <rpc/types.h>
44*0Sstevel@tonic-gate #include <sys/socket.h>
45*0Sstevel@tonic-gate #include <netinet/in.h>
46*0Sstevel@tonic-gate #include "socket_inet.h"
47*0Sstevel@tonic-gate #include "ipv4.h"
48*0Sstevel@tonic-gate #include "clnt.h"
49*0Sstevel@tonic-gate #include <rpc/rpc.h>
50*0Sstevel@tonic-gate #include "brpc.h"
51*0Sstevel@tonic-gate #include "pmap.h"
52*0Sstevel@tonic-gate #include <sys/promif.h>
53*0Sstevel@tonic-gate #include <rpc/xdr.h>
54*0Sstevel@tonic-gate #include <rpc/auth.h>
55*0Sstevel@tonic-gate #include <rpc/auth_sys.h>
56*0Sstevel@tonic-gate #include "auth_inet.h"
57*0Sstevel@tonic-gate #include <rpc/rpc_msg.h>
58*0Sstevel@tonic-gate #include <sys/bootdebug.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #define	dprintf if (boothowto & RB_DEBUG) printf
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #define	MCALL_MSG_SIZE 24
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate extern int errno;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate extern void xdrrec_create();
67*0Sstevel@tonic-gate extern bool_t xdrrec_endofrecord();
68*0Sstevel@tonic-gate extern bool_t xdrrec_skiprecord();
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate  * If we create another clnt type this should be
72*0Sstevel@tonic-gate  * moved to a common file
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate struct rpc_createerr rpc_createerr;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate static int readtcp();
77*0Sstevel@tonic-gate static int writetcp();
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate static struct clnt_ops *clntbtcp_ops();
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /*
82*0Sstevel@tonic-gate  * Private data kept per client handle
83*0Sstevel@tonic-gate  */
84*0Sstevel@tonic-gate struct ct_data {
85*0Sstevel@tonic-gate 	int			ct_sock;
86*0Sstevel@tonic-gate 	bool_t			ct_closeit;
87*0Sstevel@tonic-gate 	struct sockaddr_in	ct_raddr;
88*0Sstevel@tonic-gate 	uint_t			ct_wait_msec;
89*0Sstevel@tonic-gate 	struct timeval		ct_total;
90*0Sstevel@tonic-gate 	struct rpc_err		ct_error;
91*0Sstevel@tonic-gate 	XDR			ct_xdrs;
92*0Sstevel@tonic-gate 	char			ct_mcall[MCALL_MSG_SIZE];
93*0Sstevel@tonic-gate 	uint_t			ct_mpos;
94*0Sstevel@tonic-gate 	uint_t			ct_xdrpos;
95*0Sstevel@tonic-gate };
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate /*
98*0Sstevel@tonic-gate  * Create a TCP based client handle.
99*0Sstevel@tonic-gate  * If *sockp<0, *sockp is set to a newly created TCP socket.
100*0Sstevel@tonic-gate  * If raddr->sin_port is 0 a binder on the remote machine
101*0Sstevel@tonic-gate  * is consulted for the correct port number.
102*0Sstevel@tonic-gate  * NB: It is the clients responsibility to close *sockp.
103*0Sstevel@tonic-gate  * NB: The rpch->cl_auth is initialized to null authentication.
104*0Sstevel@tonic-gate  *	Caller may wish to set this something more useful.
105*0Sstevel@tonic-gate  *
106*0Sstevel@tonic-gate  * wait is the amount of time used between retransmitting a call if
107*0Sstevel@tonic-gate  * no response has been heard;  retransmition occurs until the actual
108*0Sstevel@tonic-gate  * rpc call times out.
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  * sendsz and recvsz are the maximum allowable packet sizes that can be
111*0Sstevel@tonic-gate  * sent and received.
112*0Sstevel@tonic-gate  */
113*0Sstevel@tonic-gate CLIENT *
clntbtcp_create(struct sockaddr_in * raddr,rpcprog_t program,rpcvers_t version,struct timeval wait,int * sockp,uint_t sendsz,uint_t recvsz)114*0Sstevel@tonic-gate clntbtcp_create(
115*0Sstevel@tonic-gate 	struct sockaddr_in *raddr,
116*0Sstevel@tonic-gate 	rpcprog_t program,
117*0Sstevel@tonic-gate 	rpcvers_t version,
118*0Sstevel@tonic-gate 	struct timeval wait,
119*0Sstevel@tonic-gate 	int *sockp,
120*0Sstevel@tonic-gate 	uint_t sendsz,
121*0Sstevel@tonic-gate 	uint_t recvsz)
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate 	CLIENT *cl;
124*0Sstevel@tonic-gate 	struct ct_data *ct;
125*0Sstevel@tonic-gate 	struct rpc_msg call_msg;
126*0Sstevel@tonic-gate #if 0	/* XXX not yet */
127*0Sstevel@tonic-gate 	int min_buf_sz;
128*0Sstevel@tonic-gate 	int pref_buf_sz = 64 * 1024; /* 64 KB */
129*0Sstevel@tonic-gate 	socklen_t optlen;
130*0Sstevel@tonic-gate #endif /* not yet */
131*0Sstevel@tonic-gate 	cl = (CLIENT *)bkmem_alloc(sizeof (CLIENT));
132*0Sstevel@tonic-gate 	if (cl == NULL) {
133*0Sstevel@tonic-gate 		errno = ENOMEM;
134*0Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
135*0Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = errno;
136*0Sstevel@tonic-gate 		return ((CLIENT *)NULL);
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	ct = (struct ct_data *)bkmem_alloc(sizeof (*ct));
140*0Sstevel@tonic-gate 	if (ct == NULL) {
141*0Sstevel@tonic-gate 		errno = ENOMEM;
142*0Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
143*0Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = errno;
144*0Sstevel@tonic-gate 		goto fooy;
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	if (raddr->sin_port == 0) {
148*0Sstevel@tonic-gate 		ushort_t port;
149*0Sstevel@tonic-gate 		if ((port = bpmap_getport(program, version,
150*0Sstevel@tonic-gate 				&(rpc_createerr.cf_stat), raddr, NULL)) == 0) {
151*0Sstevel@tonic-gate 			goto fooy;
152*0Sstevel@tonic-gate 		}
153*0Sstevel@tonic-gate 		raddr->sin_port = htons(port);
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	if (*sockp < 0) {
157*0Sstevel@tonic-gate 		struct sockaddr_in from;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 		*sockp = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
160*0Sstevel@tonic-gate 		if (*sockp < 0) {
161*0Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
162*0Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
163*0Sstevel@tonic-gate 			goto fooy;
164*0Sstevel@tonic-gate 		}
165*0Sstevel@tonic-gate 		/*
166*0Sstevel@tonic-gate 		 * Bootparams assumes a local net, so be sure to let lower
167*0Sstevel@tonic-gate 		 * layer protocols know not to route.
168*0Sstevel@tonic-gate 		 */
169*0Sstevel@tonic-gate 		if (dontroute) {
170*0Sstevel@tonic-gate 			(void) setsockopt(*sockp, SOL_SOCKET, SO_DONTROUTE,
171*0Sstevel@tonic-gate 				(const void *)&dontroute, sizeof (dontroute));
172*0Sstevel@tonic-gate 		}
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 		/* attempt to bind to priv port */
175*0Sstevel@tonic-gate 		from.sin_family = AF_INET;
176*0Sstevel@tonic-gate 		ipv4_getipaddr(&from.sin_addr);
177*0Sstevel@tonic-gate 		from.sin_addr.s_addr = htonl(from.sin_addr.s_addr);
178*0Sstevel@tonic-gate 		from.sin_port = get_source_port(TRUE);
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 		if (bind(*sockp, (struct sockaddr *)&from, sizeof (from)) < 0) {
181*0Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
182*0Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
183*0Sstevel@tonic-gate 			if (*sockp > 0)
184*0Sstevel@tonic-gate 				close(*sockp);
185*0Sstevel@tonic-gate 			goto fooy;
186*0Sstevel@tonic-gate 		}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		if (connect(*sockp, (struct sockaddr *)raddr,
189*0Sstevel@tonic-gate 			    sizeof (struct sockaddr_in)) < 0) {
190*0Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
191*0Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
192*0Sstevel@tonic-gate 			if (*sockp > 0)
193*0Sstevel@tonic-gate 				close(*sockp);
194*0Sstevel@tonic-gate 			goto fooy;
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate #if 0 /* XXX not yet */
198*0Sstevel@tonic-gate 		/*
199*0Sstevel@tonic-gate 		 * In the future we may want RPC to use larger transfer sizes
200*0Sstevel@tonic-gate 		 * over TCP.  In this case we will want to increase the
201*0Sstevel@tonic-gate 		 * window size.
202*0Sstevel@tonic-gate 		 */
203*0Sstevel@tonic-gate 		/*
204*0Sstevel@tonic-gate 		 * Resize the receive window if possible
205*0Sstevel@tonic-gate 		 */
206*0Sstevel@tonic-gate 		optlen = sizeof (int);
207*0Sstevel@tonic-gate 		if (getsockopt(*sockp, SOL_SOCKET, SO_RCVBUF,
208*0Sstevel@tonic-gate 				(void *)&min_buf_sz, &optlen) != 0)
209*0Sstevel@tonic-gate 			goto keep_going;
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 		if (min_buf_sz < pref_buf_sz)
212*0Sstevel@tonic-gate 			(void) setsockopt(*sockp, SOL_SOCKET, SO_RCVBUF,
213*0Sstevel@tonic-gate 				(const void *)&pref_buf_sz, sizeof (int));
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate keep_going:
216*0Sstevel@tonic-gate #endif		/* not yet */
217*0Sstevel@tonic-gate 		ct->ct_closeit = TRUE;
218*0Sstevel@tonic-gate 	} else
219*0Sstevel@tonic-gate 		ct->ct_closeit = FALSE;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	/*
222*0Sstevel@tonic-gate 	 * Set up the private data
223*0Sstevel@tonic-gate 	 */
224*0Sstevel@tonic-gate 	ct->ct_sock = *sockp;
225*0Sstevel@tonic-gate 	ct->ct_wait_msec = 0;
226*0Sstevel@tonic-gate 	ct->ct_total.tv_sec = wait.tv_sec;
227*0Sstevel@tonic-gate 	ct->ct_total.tv_usec = -1;
228*0Sstevel@tonic-gate 	ct->ct_raddr = *raddr;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	/*
231*0Sstevel@tonic-gate 	 * Initialize the call message
232*0Sstevel@tonic-gate 	 */
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	/*
235*0Sstevel@tonic-gate 	 * XXX - The xid might need to be randomized more.  Imagine if there
236*0Sstevel@tonic-gate 	 * are a rack of blade servers all booting at the same time.  They
237*0Sstevel@tonic-gate 	 * may cause havoc on the server with xid replays.
238*0Sstevel@tonic-gate 	 */
239*0Sstevel@tonic-gate 	call_msg.rm_xid = (uint_t)prom_gettime() + 1;
240*0Sstevel@tonic-gate 	call_msg.rm_direction = CALL;
241*0Sstevel@tonic-gate 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
242*0Sstevel@tonic-gate 	call_msg.rm_call.cb_prog = program;
243*0Sstevel@tonic-gate 	call_msg.rm_call.cb_vers = version;
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	/*
246*0Sstevel@tonic-gate 	 * pre-serialize the static part of the call msg and stash it away
247*0Sstevel@tonic-gate 	 */
248*0Sstevel@tonic-gate 	xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
249*0Sstevel@tonic-gate 			XDR_ENCODE);
250*0Sstevel@tonic-gate 	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
251*0Sstevel@tonic-gate 		if (ct->ct_closeit)
252*0Sstevel@tonic-gate 			(void) close(*sockp);
253*0Sstevel@tonic-gate 		goto fooy;
254*0Sstevel@tonic-gate 	}
255*0Sstevel@tonic-gate 	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
256*0Sstevel@tonic-gate 	XDR_DESTROY(&(ct->ct_xdrs));
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	/*
259*0Sstevel@tonic-gate 	 * XXX - Memory allocations can fail in xdrrec_create, so we need to
260*0Sstevel@tonic-gate 	 * be able to catch those errors.
261*0Sstevel@tonic-gate 	 */
262*0Sstevel@tonic-gate 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz, (caddr_t)ct, readtcp,
263*0Sstevel@tonic-gate 			writetcp);
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	cl->cl_ops = clntbtcp_ops();
266*0Sstevel@tonic-gate 	cl->cl_private = (caddr_t)ct;
267*0Sstevel@tonic-gate 	cl->cl_auth = authnone_create();
268*0Sstevel@tonic-gate 	return (cl);
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate fooy:
271*0Sstevel@tonic-gate 	if (ct)
272*0Sstevel@tonic-gate 		bkmem_free((caddr_t)ct, sizeof (*ct));
273*0Sstevel@tonic-gate 	if (cl)
274*0Sstevel@tonic-gate 		bkmem_free((caddr_t)cl, sizeof (CLIENT));
275*0Sstevel@tonic-gate 	return ((CLIENT *)NULL);
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate static enum clnt_stat
clntbtcp_call(CLIENT * cl,rpcproc_t proc,xdrproc_t xargs,caddr_t argsp,xdrproc_t xdr_results,caddr_t resultsp,struct timeval utimeout)279*0Sstevel@tonic-gate clntbtcp_call(
280*0Sstevel@tonic-gate 	CLIENT *cl,
281*0Sstevel@tonic-gate 	rpcproc_t proc,
282*0Sstevel@tonic-gate 	xdrproc_t xargs,
283*0Sstevel@tonic-gate 	caddr_t argsp,
284*0Sstevel@tonic-gate 	xdrproc_t xdr_results,
285*0Sstevel@tonic-gate 	caddr_t resultsp,
286*0Sstevel@tonic-gate 	struct timeval utimeout)
287*0Sstevel@tonic-gate {
288*0Sstevel@tonic-gate 	struct ct_data *ct;
289*0Sstevel@tonic-gate 	XDR *xdrs;
290*0Sstevel@tonic-gate 	struct rpc_msg reply_msg;
291*0Sstevel@tonic-gate 	uint32_t x_id;
292*0Sstevel@tonic-gate 	uint32_t *msg_x_id;
293*0Sstevel@tonic-gate 	bool_t shipnow;
294*0Sstevel@tonic-gate 	int nrefreshes = 2;	/* number of times to refresh cred */
295*0Sstevel@tonic-gate 	struct timeval timeout;
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	ct = (struct ct_data *)cl->cl_private;
298*0Sstevel@tonic-gate 	msg_x_id = (uint32_t *)ct->ct_mcall;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	xdrs = &(ct->ct_xdrs);
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	ct->ct_total = utimeout;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	/*
305*0Sstevel@tonic-gate 	 * We have to be able to wait for some non-zero period of time, so
306*0Sstevel@tonic-gate 	 * use a default timeout.
307*0Sstevel@tonic-gate 	 */
308*0Sstevel@tonic-gate 	if (ct->ct_total.tv_sec == 0)
309*0Sstevel@tonic-gate 		ct->ct_total.tv_sec = RPC_RCVWAIT_MSEC / 1000;
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	ct->ct_wait_msec = ct->ct_total.tv_sec * 1000 +
312*0Sstevel@tonic-gate 		ct->ct_total.tv_usec / 1000;
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	timeout = ct->ct_total;
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	shipnow = (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0 &&
317*0Sstevel@tonic-gate 			timeout.tv_usec == 0) ? FALSE : TRUE;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate call_again:
320*0Sstevel@tonic-gate 	xdrs->x_op = XDR_ENCODE;
321*0Sstevel@tonic-gate 	ct->ct_error.re_status = RPC_SUCCESS;
322*0Sstevel@tonic-gate 	x_id = ntohl(++(*msg_x_id));
323*0Sstevel@tonic-gate 	if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
324*0Sstevel@tonic-gate 	    (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
325*0Sstevel@tonic-gate 	    (! AUTH_MARSHALL(cl->cl_auth, xdrs, NULL)) ||
326*0Sstevel@tonic-gate 	    (! (*xargs)(xdrs, argsp))) {
327*0Sstevel@tonic-gate 		(void) xdrrec_endofrecord(xdrs, TRUE);
328*0Sstevel@tonic-gate 		ct->ct_error.re_status = RPC_CANTENCODEARGS;
329*0Sstevel@tonic-gate 		printf("clntbtcp_call: xdr encode args failed\n");
330*0Sstevel@tonic-gate 		return (ct->ct_error.re_status);
331*0Sstevel@tonic-gate 	}
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	if (!xdrrec_endofrecord(xdrs, shipnow)) {
334*0Sstevel@tonic-gate 		printf("clntbtcp_call: rpc cansend error\n");
335*0Sstevel@tonic-gate 		ct->ct_error.re_status = RPC_CANTSEND;
336*0Sstevel@tonic-gate 		return (ct->ct_error.re_status);
337*0Sstevel@tonic-gate 	}
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 	if (!shipnow)
340*0Sstevel@tonic-gate 		return (RPC_SUCCESS);
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
343*0Sstevel@tonic-gate 		ct->ct_error.re_status = RPC_TIMEDOUT;
344*0Sstevel@tonic-gate 		return (ct->ct_error.re_status);
345*0Sstevel@tonic-gate 	}
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	xdrs->x_op = XDR_DECODE;
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	/* CONSTCOND */
350*0Sstevel@tonic-gate 	while (TRUE) {
351*0Sstevel@tonic-gate 		reply_msg.acpted_rply.ar_verf = _null_auth;
352*0Sstevel@tonic-gate 		reply_msg.acpted_rply.ar_results.where = NULL;
353*0Sstevel@tonic-gate 		reply_msg.acpted_rply.ar_results.proc = xdr_void;
354*0Sstevel@tonic-gate 		if (!xdrrec_skiprecord(xdrs)) {
355*0Sstevel@tonic-gate 			return (ct->ct_error.re_status);
356*0Sstevel@tonic-gate 		}
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 		if (!xdr_replymsg(xdrs, &reply_msg)) {
359*0Sstevel@tonic-gate 			if (ct->ct_error.re_status == RPC_SUCCESS)
360*0Sstevel@tonic-gate 				continue;
361*0Sstevel@tonic-gate 			return (ct->ct_error.re_status);
362*0Sstevel@tonic-gate 		}
363*0Sstevel@tonic-gate 		if (reply_msg.rm_xid == x_id) {
364*0Sstevel@tonic-gate 			break;
365*0Sstevel@tonic-gate 		}
366*0Sstevel@tonic-gate 	}
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	/*
369*0Sstevel@tonic-gate 	 * process header
370*0Sstevel@tonic-gate 	 */
371*0Sstevel@tonic-gate 	_seterr_reply(&reply_msg, &(ct->ct_error));
372*0Sstevel@tonic-gate 	if (ct->ct_error.re_status == RPC_SUCCESS) {
373*0Sstevel@tonic-gate 		if (!AUTH_VALIDATE(cl->cl_auth,
374*0Sstevel@tonic-gate 				&reply_msg.acpted_rply.ar_verf)) {
375*0Sstevel@tonic-gate 			ct->ct_error.re_status = RPC_AUTHERROR;
376*0Sstevel@tonic-gate 			ct->ct_error.re_why = AUTH_INVALIDRESP;
377*0Sstevel@tonic-gate 		} else if (!(*xdr_results)(xdrs, resultsp)) {
378*0Sstevel@tonic-gate 			if (ct->ct_error.re_status == RPC_SUCCESS) {
379*0Sstevel@tonic-gate 				ct->ct_error.re_status = RPC_CANTDECODERES;
380*0Sstevel@tonic-gate 			}
381*0Sstevel@tonic-gate 		}
382*0Sstevel@tonic-gate 		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
383*0Sstevel@tonic-gate 			xdrs->x_op = XDR_FREE;
384*0Sstevel@tonic-gate 			(void) xdr_opaque_auth(xdrs,
385*0Sstevel@tonic-gate 				&(reply_msg.acpted_rply.ar_verf));
386*0Sstevel@tonic-gate 		}
387*0Sstevel@tonic-gate 	} else {
388*0Sstevel@tonic-gate 		if (nrefreshes-- && AUTH_REFRESH(cl->cl_auth, &reply_msg,
389*0Sstevel@tonic-gate 						NULL)) {
390*0Sstevel@tonic-gate 			goto call_again;
391*0Sstevel@tonic-gate 		}
392*0Sstevel@tonic-gate 	}
393*0Sstevel@tonic-gate 	return (ct->ct_error.re_status);
394*0Sstevel@tonic-gate }
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate /*
397*0Sstevel@tonic-gate  * Interface between xdr serializer and tcp connection.
398*0Sstevel@tonic-gate  * Behaves like the system calls, read & write, but keeps some error state
399*0Sstevel@tonic-gate  * around for the rpc level.
400*0Sstevel@tonic-gate  */
401*0Sstevel@tonic-gate static int
readtcp(struct ct_data * ct,caddr_t buf,int len)402*0Sstevel@tonic-gate readtcp(struct ct_data *ct,
403*0Sstevel@tonic-gate 	caddr_t buf,
404*0Sstevel@tonic-gate 	int len)
405*0Sstevel@tonic-gate {
406*0Sstevel@tonic-gate 	int inlen = 0;
407*0Sstevel@tonic-gate 	uint_t start, diff;
408*0Sstevel@tonic-gate 	struct sockaddr from;
409*0Sstevel@tonic-gate 	uint_t fromlen = sizeof (from);
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	if (len <= 0)
412*0Sstevel@tonic-gate 		return (0);
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	/*
415*0Sstevel@tonic-gate 	 * Do non-blocking reads here until we get some data or timeout
416*0Sstevel@tonic-gate 	 */
417*0Sstevel@tonic-gate 	start = prom_gettime();
418*0Sstevel@tonic-gate 	while ((inlen = recvfrom(ct->ct_sock, buf, len, 0, &from,
419*0Sstevel@tonic-gate 					&fromlen)) == 0) {
420*0Sstevel@tonic-gate 		diff = (uint_t)(prom_gettime() - start);
421*0Sstevel@tonic-gate 		if (diff > ct->ct_wait_msec) {
422*0Sstevel@tonic-gate 			errno = ETIMEDOUT;
423*0Sstevel@tonic-gate 			inlen = -1;
424*0Sstevel@tonic-gate 			break;
425*0Sstevel@tonic-gate 		}
426*0Sstevel@tonic-gate 	}
427*0Sstevel@tonic-gate #ifdef DEBUG
428*0Sstevel@tonic-gate 	printf("readtcp: inlen = %d\n", inlen);
429*0Sstevel@tonic-gate #endif
430*0Sstevel@tonic-gate 	switch (inlen) {
431*0Sstevel@tonic-gate 	case 0:
432*0Sstevel@tonic-gate 		/* premature eof */
433*0Sstevel@tonic-gate 		ct->ct_error.re_errno = ECONNRESET;
434*0Sstevel@tonic-gate 		ct->ct_error.re_status = RPC_CANTRECV;
435*0Sstevel@tonic-gate 		inlen = -1;  /* it's really an error */
436*0Sstevel@tonic-gate 		break;
437*0Sstevel@tonic-gate 	case -1:
438*0Sstevel@tonic-gate 		ct->ct_error.re_errno = errno;
439*0Sstevel@tonic-gate 		ct->ct_error.re_status = RPC_CANTRECV;
440*0Sstevel@tonic-gate 		break;
441*0Sstevel@tonic-gate 	}
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	return (inlen);
444*0Sstevel@tonic-gate }
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate static int
writetcp(ct,buf,len)447*0Sstevel@tonic-gate writetcp(ct, buf, len)
448*0Sstevel@tonic-gate 	struct ct_data *ct;
449*0Sstevel@tonic-gate 	caddr_t buf;
450*0Sstevel@tonic-gate 	int len;
451*0Sstevel@tonic-gate {
452*0Sstevel@tonic-gate 	register int i, cnt;
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
455*0Sstevel@tonic-gate 		if ((i = sendto(ct->ct_sock, (void *)buf, cnt, 0,
456*0Sstevel@tonic-gate 				(struct sockaddr *)&(ct->ct_raddr),
457*0Sstevel@tonic-gate 				sizeof (ct->ct_raddr))) == -1) {
458*0Sstevel@tonic-gate 			ct->ct_error.re_errno = errno;
459*0Sstevel@tonic-gate 			ct->ct_error.re_status = RPC_CANTSEND;
460*0Sstevel@tonic-gate 			return (-1);
461*0Sstevel@tonic-gate 		}
462*0Sstevel@tonic-gate 	}
463*0Sstevel@tonic-gate 	return (len);
464*0Sstevel@tonic-gate }
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate static void
clntbtcp_geterr(CLIENT * cl,struct rpc_err * errp)467*0Sstevel@tonic-gate clntbtcp_geterr(
468*0Sstevel@tonic-gate 	CLIENT *cl,
469*0Sstevel@tonic-gate 	struct rpc_err *errp)
470*0Sstevel@tonic-gate {
471*0Sstevel@tonic-gate 	struct ct_data *ct = (struct ct_data *)cl->cl_private;
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	*errp = ct->ct_error;
474*0Sstevel@tonic-gate }
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate static bool_t
clntbtcp_freeres(CLIENT * cl,xdrproc_t xdr_res,caddr_t res_ptr)478*0Sstevel@tonic-gate clntbtcp_freeres(
479*0Sstevel@tonic-gate 	CLIENT *cl,
480*0Sstevel@tonic-gate 	xdrproc_t xdr_res,
481*0Sstevel@tonic-gate 	caddr_t res_ptr)
482*0Sstevel@tonic-gate {
483*0Sstevel@tonic-gate 	struct ct_data *ct = (struct ct_data *)cl->cl_private;
484*0Sstevel@tonic-gate 	XDR *xdrs = &(ct->ct_xdrs);
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	xdrs->x_op = XDR_FREE;
487*0Sstevel@tonic-gate 	return ((*xdr_res)(xdrs, res_ptr));
488*0Sstevel@tonic-gate }
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate static void
clntbtcp_abort()491*0Sstevel@tonic-gate clntbtcp_abort()
492*0Sstevel@tonic-gate 	/* CLIENT *h; */
493*0Sstevel@tonic-gate {
494*0Sstevel@tonic-gate }
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate /* ARGSUSED */
497*0Sstevel@tonic-gate static bool_t
clntbtcp_control(CLIENT * cl,int request,char * info)498*0Sstevel@tonic-gate clntbtcp_control(
499*0Sstevel@tonic-gate 	CLIENT *cl,
500*0Sstevel@tonic-gate 	int request,
501*0Sstevel@tonic-gate 	char *info)
502*0Sstevel@tonic-gate {
503*0Sstevel@tonic-gate 	/* Not implemented in boot */
504*0Sstevel@tonic-gate 	return (FALSE);
505*0Sstevel@tonic-gate }
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate static void
clntbtcp_destroy(CLIENT * cl)508*0Sstevel@tonic-gate clntbtcp_destroy(CLIENT *cl)
509*0Sstevel@tonic-gate {
510*0Sstevel@tonic-gate 	struct ct_data *ct = (struct ct_data *)cl->cl_private;
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 	if (ct->ct_closeit) {
513*0Sstevel@tonic-gate 		(void) socket_close(ct->ct_sock);
514*0Sstevel@tonic-gate 	}
515*0Sstevel@tonic-gate 	XDR_DESTROY(&(ct->ct_xdrs));
516*0Sstevel@tonic-gate 	bkmem_free((caddr_t)ct, (sizeof (struct ct_data)));
517*0Sstevel@tonic-gate 	bkmem_free((caddr_t)cl, sizeof (CLIENT));
518*0Sstevel@tonic-gate }
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate static struct clnt_ops *
clntbtcp_ops()521*0Sstevel@tonic-gate clntbtcp_ops()
522*0Sstevel@tonic-gate {
523*0Sstevel@tonic-gate 	static struct clnt_ops ops;
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 	if (ops.cl_call == NULL) {
526*0Sstevel@tonic-gate 		ops.cl_call = clntbtcp_call;
527*0Sstevel@tonic-gate 		ops.cl_abort = clntbtcp_abort;
528*0Sstevel@tonic-gate 		ops.cl_geterr = clntbtcp_geterr;
529*0Sstevel@tonic-gate 		ops.cl_freeres = clntbtcp_freeres;
530*0Sstevel@tonic-gate 		ops.cl_destroy = clntbtcp_destroy;
531*0Sstevel@tonic-gate 		ops.cl_control = clntbtcp_control;
532*0Sstevel@tonic-gate 	}
533*0Sstevel@tonic-gate 	return (&ops);
534*0Sstevel@tonic-gate }
535