xref: /freebsd/sys/rpc/clnt_dg.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
1 /*	$NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 /*
32  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33  */
34 
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #ident	"@(#)clnt_dg.c	1.23	94/04/22 SMI"
37 static char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro";
38 #endif
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 /*
43  * Implements a connectionless client side RPC.
44  */
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/mutex.h>
52 #include <sys/pcpu.h>
53 #include <sys/proc.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/time.h>
57 #include <sys/uio.h>
58 
59 #include <rpc/rpc.h>
60 #include <rpc/rpc_com.h>
61 
62 
63 #ifdef _FREEFALL_CONFIG
64 /*
65  * Disable RPC exponential back-off for FreeBSD.org systems.
66  */
67 #define	RPC_MAX_BACKOFF		1 /* second */
68 #else
69 #define	RPC_MAX_BACKOFF		30 /* seconds */
70 #endif
71 
72 static bool_t time_not_ok(struct timeval *);
73 static enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
74 	    xdrproc_t, void *, struct timeval);
75 static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
76 static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *);
77 static void clnt_dg_abort(CLIENT *);
78 static bool_t clnt_dg_control(CLIENT *, u_int, void *);
79 static void clnt_dg_destroy(CLIENT *);
80 static void clnt_dg_soupcall(struct socket *so, void *arg, int waitflag);
81 
82 static struct clnt_ops clnt_dg_ops = {
83 	.cl_call =	clnt_dg_call,
84 	.cl_abort =	clnt_dg_abort,
85 	.cl_geterr =	clnt_dg_geterr,
86 	.cl_freeres =	clnt_dg_freeres,
87 	.cl_destroy =	clnt_dg_destroy,
88 	.cl_control =	clnt_dg_control
89 };
90 
91 static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
92 
93 /*
94  * A pending RPC request which awaits a reply.
95  */
96 struct cu_request {
97 	TAILQ_ENTRY(cu_request) cr_link;
98 	uint32_t		cr_xid;		/* XID of request */
99 	struct mbuf		*cr_mrep;	/* reply received by upcall */
100 	int			cr_error;	/* any error from upcall */
101 };
102 
103 TAILQ_HEAD(cu_request_list, cu_request);
104 
105 #define MCALL_MSG_SIZE 24
106 
107 /*
108  * This structure is pointed to by the socket's so_upcallarg
109  * member. It is separate from the client private data to facilitate
110  * multiple clients sharing the same socket. The cs_lock mutex is used
111  * to protect all fields of this structure, the socket's receive
112  * buffer SOCKBUF_LOCK is used to ensure that exactly one of these
113  * structures is installed on the socket.
114  */
115 struct cu_socket {
116 	struct mtx		cs_lock;
117 	int			cs_refs;	/* Count of clients */
118 	struct cu_request_list	cs_pending;	/* Requests awaiting replies */
119 
120 };
121 
122 /*
123  * Private data kept per client handle
124  */
125 struct cu_data {
126 	struct socket		*cu_socket;	/* connection socket */
127 	bool_t			cu_closeit;	/* opened by library */
128 	struct sockaddr_storage	cu_raddr;	/* remote address */
129 	int			cu_rlen;
130 	struct timeval		cu_wait;	/* retransmit interval */
131 	struct timeval		cu_total;	/* total time for the call */
132 	struct rpc_err		cu_error;
133 	uint32_t		cu_xid;
134 	char			cu_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */
135 	size_t			cu_mcalllen;
136 	size_t			cu_sendsz;	/* send size */
137 	size_t			cu_recvsz;	/* recv size */
138 	int			cu_async;
139 	int			cu_connect;	/* Use connect(). */
140 	int			cu_connected;	/* Have done connect(). */
141 	const char		*cu_waitchan;
142 	int			cu_waitflag;
143 };
144 
145 /*
146  * Connection less client creation returns with client handle parameters.
147  * Default options are set, which the user can change using clnt_control().
148  * fd should be open and bound.
149  * NB: The rpch->cl_auth is initialized to null authentication.
150  * 	Caller may wish to set this something more useful.
151  *
152  * sendsz and recvsz are the maximum allowable packet sizes that can be
153  * sent and received. Normally they are the same, but they can be
154  * changed to improve the program efficiency and buffer allocation.
155  * If they are 0, use the transport default.
156  *
157  * If svcaddr is NULL, returns NULL.
158  */
159 CLIENT *
160 clnt_dg_create(
161 	struct socket *so,
162 	struct sockaddr *svcaddr,	/* servers address */
163 	rpcprog_t program,		/* program number */
164 	rpcvers_t version,		/* version number */
165 	size_t sendsz,			/* buffer recv size */
166 	size_t recvsz)			/* buffer send size */
167 {
168 	CLIENT *cl = NULL;		/* client handle */
169 	struct cu_data *cu = NULL;	/* private data */
170 	struct cu_socket *cs = NULL;
171 	struct timeval now;
172 	struct rpc_msg call_msg;
173 	struct __rpc_sockinfo si;
174 	XDR xdrs;
175 
176 	if (svcaddr == NULL) {
177 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
178 		return (NULL);
179 	}
180 
181 	if (!__rpc_socket2sockinfo(so, &si)) {
182 		rpc_createerr.cf_stat = RPC_TLIERROR;
183 		rpc_createerr.cf_error.re_errno = 0;
184 		return (NULL);
185 	}
186 
187 	/*
188 	 * Find the receive and the send size
189 	 */
190 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
191 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
192 	if ((sendsz == 0) || (recvsz == 0)) {
193 		rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
194 		rpc_createerr.cf_error.re_errno = 0;
195 		return (NULL);
196 	}
197 
198 	cl = mem_alloc(sizeof (CLIENT));
199 
200 	/*
201 	 * Should be multiple of 4 for XDR.
202 	 */
203 	sendsz = ((sendsz + 3) / 4) * 4;
204 	recvsz = ((recvsz + 3) / 4) * 4;
205 	cu = mem_alloc(sizeof (*cu));
206 	(void) memcpy(&cu->cu_raddr, svcaddr, (size_t)svcaddr->sa_len);
207 	cu->cu_rlen = svcaddr->sa_len;
208 	/* Other values can also be set through clnt_control() */
209 	cu->cu_wait.tv_sec = 15;	/* heuristically chosen */
210 	cu->cu_wait.tv_usec = 0;
211 	cu->cu_total.tv_sec = -1;
212 	cu->cu_total.tv_usec = -1;
213 	cu->cu_sendsz = sendsz;
214 	cu->cu_recvsz = recvsz;
215 	cu->cu_async = FALSE;
216 	cu->cu_connect = FALSE;
217 	cu->cu_connected = FALSE;
218 	cu->cu_waitchan = "rpcrecv";
219 	cu->cu_waitflag = 0;
220 	(void) getmicrotime(&now);
221 	cu->cu_xid = __RPC_GETXID(&now);
222 	call_msg.rm_xid = cu->cu_xid;
223 	call_msg.rm_call.cb_prog = program;
224 	call_msg.rm_call.cb_vers = version;
225 	xdrmem_create(&xdrs, cu->cu_mcallc, MCALL_MSG_SIZE, XDR_ENCODE);
226 	if (! xdr_callhdr(&xdrs, &call_msg)) {
227 		rpc_createerr.cf_stat = RPC_CANTENCODEARGS;  /* XXX */
228 		rpc_createerr.cf_error.re_errno = 0;
229 		goto err2;
230 	}
231 	cu->cu_mcalllen = XDR_GETPOS(&xdrs);;
232 
233 	/*
234 	 * By default, closeit is always FALSE. It is users responsibility
235 	 * to do a close on it, else the user may use clnt_control
236 	 * to let clnt_destroy do it for him/her.
237 	 */
238 	cu->cu_closeit = FALSE;
239 	cu->cu_socket = so;
240 
241 	SOCKBUF_LOCK(&so->so_rcv);
242 recheck_socket:
243 	if (so->so_upcall) {
244 		if (so->so_upcall != clnt_dg_soupcall) {
245 			SOCKBUF_UNLOCK(&so->so_rcv);
246 			printf("clnt_dg_create(): socket already has an incompatible upcall\n");
247 			goto err2;
248 		}
249 		cs = (struct cu_socket *) so->so_upcallarg;
250 		mtx_lock(&cs->cs_lock);
251 		cs->cs_refs++;
252 		mtx_unlock(&cs->cs_lock);
253 	} else {
254 		/*
255 		 * We are the first on this socket - allocate the
256 		 * structure and install it in the socket.
257 		 */
258 		SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv);
259 		cs = mem_alloc(sizeof(*cs));
260 		SOCKBUF_LOCK(&cu->cu_socket->so_rcv);
261 		if (so->so_upcall) {
262 			/*
263 			 * We have lost a race with some other client.
264 			 */
265 			mem_free(cs, sizeof(*cs));
266 			goto recheck_socket;
267 		}
268 		mtx_init(&cs->cs_lock, "cs->cs_lock", NULL, MTX_DEF);
269 		cs->cs_refs = 1;
270 		TAILQ_INIT(&cs->cs_pending);
271 		so->so_upcallarg = cs;
272 		so->so_upcall = clnt_dg_soupcall;
273 		so->so_rcv.sb_flags |= SB_UPCALL;
274 	}
275 	SOCKBUF_UNLOCK(&so->so_rcv);
276 
277 	cl->cl_ops = &clnt_dg_ops;
278 	cl->cl_private = (caddr_t)(void *)cu;
279 	cl->cl_auth = authnone_create();
280 	cl->cl_tp = NULL;
281 	cl->cl_netid = NULL;
282 	return (cl);
283 err2:
284 	if (cl) {
285 		mem_free(cl, sizeof (CLIENT));
286 		if (cu)
287 			mem_free(cu, sizeof (*cu));
288 	}
289 	return (NULL);
290 }
291 
292 static enum clnt_stat
293 clnt_dg_call(
294 	CLIENT	*cl,			/* client handle */
295 	rpcproc_t	proc,		/* procedure number */
296 	xdrproc_t	xargs,		/* xdr routine for args */
297 	void		*argsp,		/* pointer to args */
298 	xdrproc_t	xresults,	/* xdr routine for results */
299 	void		*resultsp,	/* pointer to results */
300 	struct timeval	utimeout)	/* seconds to wait before giving up */
301 {
302 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
303 	struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg;
304 	XDR xdrs;
305 	struct rpc_msg reply_msg;
306 	bool_t ok;
307 	int nrefreshes = 2;		/* number of times to refresh cred */
308 	struct timeval timeout;
309 	struct timeval retransmit_time;
310 	struct timeval next_sendtime, starttime, time_waited, tv;
311 	struct sockaddr *sa;
312 	socklen_t salen;
313 	uint32_t xid;
314 	struct mbuf *mreq = NULL;
315 	struct cu_request cr;
316 	int error;
317 
318 	mtx_lock(&cs->cs_lock);
319 
320 	cr.cr_mrep = NULL;
321 	cr.cr_error = 0;
322 
323 	if (cu->cu_total.tv_usec == -1) {
324 		timeout = utimeout;	/* use supplied timeout */
325 	} else {
326 		timeout = cu->cu_total;	/* use default timeout */
327 	}
328 
329 	if (cu->cu_connect && !cu->cu_connected) {
330 		mtx_unlock(&cs->cs_lock);
331 		error = soconnect(cu->cu_socket,
332 		    (struct sockaddr *)&cu->cu_raddr, curthread);
333 		mtx_lock(&cs->cs_lock);
334 		if (error) {
335 			cu->cu_error.re_errno = error;
336 			cu->cu_error.re_status = RPC_CANTSEND;
337 			goto out;
338 		}
339 		cu->cu_connected = 1;
340 	}
341 	if (cu->cu_connected) {
342 		sa = NULL;
343 		salen = 0;
344 	} else {
345 		sa = (struct sockaddr *)&cu->cu_raddr;
346 		salen = cu->cu_rlen;
347 	}
348 	time_waited.tv_sec = 0;
349 	time_waited.tv_usec = 0;
350 	retransmit_time = next_sendtime = cu->cu_wait;
351 
352 	getmicrotime(&starttime);
353 
354 call_again:
355 	mtx_assert(&cs->cs_lock, MA_OWNED);
356 
357 	cu->cu_xid++;
358 	xid = cu->cu_xid;
359 
360 send_again:
361 	mtx_unlock(&cs->cs_lock);
362 
363 	MGETHDR(mreq, M_WAIT, MT_DATA);
364 	MCLGET(mreq, M_WAIT);
365 	mreq->m_len = 0;
366 	m_append(mreq, cu->cu_mcalllen, cu->cu_mcallc);
367 
368 	/*
369 	 * The XID is the first thing in the request.
370 	 */
371 	*mtod(mreq, uint32_t *) = htonl(xid);
372 
373 	xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
374 
375 	if (cu->cu_async == TRUE && xargs == NULL)
376 		goto get_reply;
377 
378 	if ((! XDR_PUTINT32(&xdrs, &proc)) ||
379 	    (! AUTH_MARSHALL(cl->cl_auth, &xdrs)) ||
380 	    (! (*xargs)(&xdrs, argsp))) {
381 		cu->cu_error.re_status = RPC_CANTENCODEARGS;
382 		mtx_lock(&cs->cs_lock);
383 		goto out;
384 	}
385 	m_fixhdr(mreq);
386 
387 	cr.cr_xid = xid;
388 	mtx_lock(&cs->cs_lock);
389 	TAILQ_INSERT_TAIL(&cs->cs_pending, &cr, cr_link);
390 	mtx_unlock(&cs->cs_lock);
391 
392 	/*
393 	 * sosend consumes mreq.
394 	 */
395 	error = sosend(cu->cu_socket, sa, NULL, mreq, NULL, 0, curthread);
396 	mreq = NULL;
397 
398 	/*
399 	 * sub-optimal code appears here because we have
400 	 * some clock time to spare while the packets are in flight.
401 	 * (We assume that this is actually only executed once.)
402 	 */
403 	reply_msg.acpted_rply.ar_verf = _null_auth;
404 	reply_msg.acpted_rply.ar_results.where = resultsp;
405 	reply_msg.acpted_rply.ar_results.proc = xresults;
406 
407 	mtx_lock(&cs->cs_lock);
408 	if (error) {
409 		TAILQ_REMOVE(&cs->cs_pending, &cr, cr_link);
410 
411 		cu->cu_error.re_errno = error;
412 		cu->cu_error.re_status = RPC_CANTSEND;
413 		goto out;
414 	}
415 
416 	/*
417 	 * Check to see if we got an upcall while waiting for the
418 	 * lock. In both these cases, the request has been removed
419 	 * from cs->cs_pending.
420 	 */
421 	if (cr.cr_error) {
422 		cu->cu_error.re_errno = cr.cr_error;
423 		cu->cu_error.re_status = RPC_CANTRECV;
424 		goto out;
425 	}
426 	if (cr.cr_mrep) {
427 		goto got_reply;
428 	}
429 
430 	/*
431 	 * Hack to provide rpc-based message passing
432 	 */
433 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
434 		if (cr.cr_xid)
435 			TAILQ_REMOVE(&cs->cs_pending, &cr, cr_link);
436 		cu->cu_error.re_status = RPC_TIMEDOUT;
437 		goto out;
438 	}
439 
440 get_reply:
441 	for (;;) {
442 		/* Decide how long to wait. */
443 		if (timevalcmp(&next_sendtime, &timeout, <)) {
444 			tv = next_sendtime;
445 		} else {
446 			tv = timeout;
447 		}
448 		timevalsub(&tv, &time_waited);
449 		if (tv.tv_sec < 0 || tv.tv_usec < 0)
450 			tv.tv_sec = tv.tv_usec = 0;
451 
452 		error = msleep(&cr, &cs->cs_lock, cu->cu_waitflag,
453 		    cu->cu_waitchan, tvtohz(&tv));
454 
455 		if (!error) {
456 			/*
457 			 * We were woken up by the upcall.  If the
458 			 * upcall had a receive error, report that,
459 			 * otherwise we have a reply.
460 			 */
461 			if (cr.cr_error) {
462 				cu->cu_error.re_errno = cr.cr_error;
463 				cu->cu_error.re_status = RPC_CANTRECV;
464 				goto out;
465 			}
466 			break;
467 		}
468 
469 		/*
470 		 * The sleep returned an error so our request is still
471 		 * on the list. If we got EWOULDBLOCK, we may want to
472 		 * re-send the request.
473 		 */
474 		if (error != EWOULDBLOCK) {
475 			if (cr.cr_xid)
476 				TAILQ_REMOVE(&cs->cs_pending, &cr, cr_link);
477 			cu->cu_error.re_errno = error;
478 			if (error == EINTR)
479 				cu->cu_error.re_status = RPC_INTR;
480 			else
481 				cu->cu_error.re_status = RPC_CANTRECV;
482 			goto out;
483 		}
484 
485 		getmicrotime(&tv);
486 		time_waited = tv;
487 		timevalsub(&time_waited, &starttime);
488 
489 		/* Check for timeout. */
490 		if (timevalcmp(&time_waited, &timeout, >)) {
491 			if (cr.cr_xid)
492 				TAILQ_REMOVE(&cs->cs_pending, &cr, cr_link);
493 			cu->cu_error.re_errno = EWOULDBLOCK;
494 			cu->cu_error.re_status = RPC_TIMEDOUT;
495 			goto out;
496 		}
497 
498 		/* Retransmit if necessary. */
499 		if (timevalcmp(&time_waited, &next_sendtime, >)) {
500 			if (cr.cr_xid)
501 				TAILQ_REMOVE(&cs->cs_pending, &cr, cr_link);
502 			/* update retransmit_time */
503 			if (retransmit_time.tv_sec < RPC_MAX_BACKOFF)
504 				timevaladd(&retransmit_time, &retransmit_time);
505 			timevaladd(&next_sendtime, &retransmit_time);
506 			goto send_again;
507 		}
508 	}
509 
510 got_reply:
511 	/*
512 	 * Now decode and validate the response. We need to drop the
513 	 * lock since xdr_replymsg may end up sleeping in malloc.
514 	 */
515 	mtx_unlock(&cs->cs_lock);
516 
517 	xdrmbuf_create(&xdrs, cr.cr_mrep, XDR_DECODE);
518 	ok = xdr_replymsg(&xdrs, &reply_msg);
519 	XDR_DESTROY(&xdrs);
520 	cr.cr_mrep = NULL;
521 
522 	mtx_lock(&cs->cs_lock);
523 
524 	if (ok) {
525 		if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
526 			(reply_msg.acpted_rply.ar_stat == SUCCESS))
527 			cu->cu_error.re_status = RPC_SUCCESS;
528 		else
529 			_seterr_reply(&reply_msg, &(cu->cu_error));
530 
531 		if (cu->cu_error.re_status == RPC_SUCCESS) {
532 			if (! AUTH_VALIDATE(cl->cl_auth,
533 					    &reply_msg.acpted_rply.ar_verf)) {
534 				cu->cu_error.re_status = RPC_AUTHERROR;
535 				cu->cu_error.re_why = AUTH_INVALIDRESP;
536 			}
537 			if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
538 				xdrs.x_op = XDR_FREE;
539 				(void) xdr_opaque_auth(&xdrs,
540 					&(reply_msg.acpted_rply.ar_verf));
541 			}
542 		}		/* end successful completion */
543 		/*
544 		 * If unsuccesful AND error is an authentication error
545 		 * then refresh credentials and try again, else break
546 		 */
547 		else if (cu->cu_error.re_status == RPC_AUTHERROR)
548 			/* maybe our credentials need to be refreshed ... */
549 			if (nrefreshes > 0 &&
550 			    AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
551 				nrefreshes--;
552 				goto call_again;
553 			}
554 		/* end of unsuccessful completion */
555 	}	/* end of valid reply message */
556 	else {
557 		cu->cu_error.re_status = RPC_CANTDECODERES;
558 
559 	}
560 out:
561 	mtx_assert(&cs->cs_lock, MA_OWNED);
562 
563 	if (mreq)
564 		m_freem(mreq);
565 	if (cr.cr_mrep)
566 		m_freem(cr.cr_mrep);
567 
568 	mtx_unlock(&cs->cs_lock);
569 	return (cu->cu_error.re_status);
570 }
571 
572 static void
573 clnt_dg_geterr(CLIENT *cl, struct rpc_err *errp)
574 {
575 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
576 
577 	*errp = cu->cu_error;
578 }
579 
580 static bool_t
581 clnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
582 {
583 	XDR xdrs;
584 	bool_t dummy;
585 
586 	xdrs.x_op = XDR_FREE;
587 	dummy = (*xdr_res)(&xdrs, res_ptr);
588 
589 	return (dummy);
590 }
591 
592 /*ARGSUSED*/
593 static void
594 clnt_dg_abort(CLIENT *h)
595 {
596 }
597 
598 static bool_t
599 clnt_dg_control(CLIENT *cl, u_int request, void *info)
600 {
601 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
602 	struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg;
603 	struct sockaddr *addr;
604 
605 	mtx_lock(&cs->cs_lock);
606 
607 	switch (request) {
608 	case CLSET_FD_CLOSE:
609 		cu->cu_closeit = TRUE;
610 		mtx_unlock(&cs->cs_lock);
611 		return (TRUE);
612 	case CLSET_FD_NCLOSE:
613 		cu->cu_closeit = FALSE;
614 		mtx_unlock(&cs->cs_lock);
615 		return (TRUE);
616 	}
617 
618 	/* for other requests which use info */
619 	if (info == NULL) {
620 		mtx_unlock(&cs->cs_lock);
621 		return (FALSE);
622 	}
623 	switch (request) {
624 	case CLSET_TIMEOUT:
625 		if (time_not_ok((struct timeval *)info)) {
626 			mtx_unlock(&cs->cs_lock);
627 			return (FALSE);
628 		}
629 		cu->cu_total = *(struct timeval *)info;
630 		break;
631 	case CLGET_TIMEOUT:
632 		*(struct timeval *)info = cu->cu_total;
633 		break;
634 	case CLSET_RETRY_TIMEOUT:
635 		if (time_not_ok((struct timeval *)info)) {
636 			mtx_unlock(&cs->cs_lock);
637 			return (FALSE);
638 		}
639 		cu->cu_wait = *(struct timeval *)info;
640 		break;
641 	case CLGET_RETRY_TIMEOUT:
642 		*(struct timeval *)info = cu->cu_wait;
643 		break;
644 	case CLGET_SVC_ADDR:
645 		/*
646 		 * Slightly different semantics to userland - we use
647 		 * sockaddr instead of netbuf.
648 		 */
649 		memcpy(info, &cu->cu_raddr, cu->cu_raddr.ss_len);
650 		break;
651 	case CLSET_SVC_ADDR:		/* set to new address */
652 		addr = (struct sockaddr *)info;
653 		(void) memcpy(&cu->cu_raddr, addr, addr->sa_len);
654 		break;
655 	case CLGET_XID:
656 		*(uint32_t *)info = cu->cu_xid;
657 		break;
658 
659 	case CLSET_XID:
660 		/* This will set the xid of the NEXT call */
661 		/* decrement by 1 as clnt_dg_call() increments once */
662 		cu->cu_xid = *(uint32_t *)info - 1;
663 		break;
664 
665 	case CLGET_VERS:
666 		/*
667 		 * This RELIES on the information that, in the call body,
668 		 * the version number field is the fifth field from the
669 		 * begining of the RPC header. MUST be changed if the
670 		 * call_struct is changed
671 		 */
672 		*(uint32_t *)info =
673 		    ntohl(*(uint32_t *)(void *)(cu->cu_mcallc +
674 		    4 * BYTES_PER_XDR_UNIT));
675 		break;
676 
677 	case CLSET_VERS:
678 		*(uint32_t *)(void *)(cu->cu_mcallc + 4 * BYTES_PER_XDR_UNIT)
679 			= htonl(*(uint32_t *)info);
680 		break;
681 
682 	case CLGET_PROG:
683 		/*
684 		 * This RELIES on the information that, in the call body,
685 		 * the program number field is the fourth field from the
686 		 * begining of the RPC header. MUST be changed if the
687 		 * call_struct is changed
688 		 */
689 		*(uint32_t *)info =
690 		    ntohl(*(uint32_t *)(void *)(cu->cu_mcallc +
691 		    3 * BYTES_PER_XDR_UNIT));
692 		break;
693 
694 	case CLSET_PROG:
695 		*(uint32_t *)(void *)(cu->cu_mcallc + 3 * BYTES_PER_XDR_UNIT)
696 			= htonl(*(uint32_t *)info);
697 		break;
698 	case CLSET_ASYNC:
699 		cu->cu_async = *(int *)info;
700 		break;
701 	case CLSET_CONNECT:
702 		cu->cu_connect = *(int *)info;
703 		break;
704 	case CLSET_WAITCHAN:
705 		cu->cu_waitchan = *(const char **)info;
706 		break;
707 	case CLGET_WAITCHAN:
708 		*(const char **) info = cu->cu_waitchan;
709 		break;
710 	case CLSET_INTERRUPTIBLE:
711 		if (*(int *) info)
712 			cu->cu_waitflag = PCATCH;
713 		else
714 			cu->cu_waitflag = 0;
715 		break;
716 	case CLGET_INTERRUPTIBLE:
717 		if (cu->cu_waitflag)
718 			*(int *) info = TRUE;
719 		else
720 			*(int *) info = FALSE;
721 		break;
722 	default:
723 		mtx_unlock(&cs->cs_lock);
724 		return (FALSE);
725 	}
726 	mtx_unlock(&cs->cs_lock);
727 	return (TRUE);
728 }
729 
730 static void
731 clnt_dg_destroy(CLIENT *cl)
732 {
733 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
734 	struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg;
735 	struct socket *so = NULL;
736 	bool_t lastsocketref;
737 
738 	SOCKBUF_LOCK(&cu->cu_socket->so_rcv);
739 
740 	mtx_lock(&cs->cs_lock);
741 	cs->cs_refs--;
742 	if (cs->cs_refs == 0) {
743 		cu->cu_socket->so_upcallarg = NULL;
744 		cu->cu_socket->so_upcall = NULL;
745 		cu->cu_socket->so_rcv.sb_flags &= ~SB_UPCALL;
746 		mtx_destroy(&cs->cs_lock);
747 		SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv);
748 		mem_free(cs, sizeof(*cs));
749 		lastsocketref = TRUE;
750 	} else {
751 		mtx_unlock(&cs->cs_lock);
752 		SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv);
753 		lastsocketref = FALSE;
754 	}
755 
756 	if (cu->cu_closeit) {
757 		KASSERT(lastsocketref, ("clnt_dg_destroy(): closing a socket "
758 			"shared with other clients"));
759 		so = cu->cu_socket;
760 		cu->cu_socket = NULL;
761 	}
762 
763 	if (so)
764 		soclose(so);
765 
766 	if (cl->cl_netid && cl->cl_netid[0])
767 		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
768 	if (cl->cl_tp && cl->cl_tp[0])
769 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
770 	mem_free(cu, sizeof (*cu));
771 	mem_free(cl, sizeof (CLIENT));
772 }
773 
774 /*
775  * Make sure that the time is not garbage.  -1 value is allowed.
776  */
777 static bool_t
778 time_not_ok(struct timeval *t)
779 {
780 	return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
781 		t->tv_usec < -1 || t->tv_usec > 1000000);
782 }
783 
784 void
785 clnt_dg_soupcall(struct socket *so, void *arg, int waitflag)
786 {
787 	struct cu_socket *cs = (struct cu_socket *) arg;
788 	struct uio uio;
789 	struct mbuf *m;
790 	struct mbuf *control;
791 	struct cu_request *cr;
792 	int error, rcvflag, foundreq;
793 	uint32_t xid;
794 
795 	uio.uio_resid = 1000000000;
796 	uio.uio_td = curthread;
797 	do {
798 		m = NULL;
799 		control = NULL;
800 		rcvflag = MSG_DONTWAIT;
801 		error = soreceive(so, NULL, &uio, &m, &control, &rcvflag);
802 		if (control)
803 			m_freem(control);
804 
805 		if (error == EWOULDBLOCK)
806 			break;
807 
808 		/*
809 		 * If there was an error, wake up all pending
810 		 * requests.
811 		 */
812 		if (error) {
813 			mtx_lock(&cs->cs_lock);
814 			TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) {
815 				cr->cr_error = error;
816 				wakeup(cr);
817 			}
818 			TAILQ_INIT(&cs->cs_pending);
819 			mtx_unlock(&cs->cs_lock);
820 			break;
821 		}
822 
823 		/*
824 		 * The XID is in the first uint32_t of the reply.
825 		 */
826 		m = m_pullup(m, sizeof(xid));
827 		if (!m)
828 			break;
829 		xid = ntohl(*mtod(m, uint32_t *));
830 
831 		/*
832 		 * Attempt to match this reply with a pending request.
833 		 */
834 		mtx_lock(&cs->cs_lock);
835 		foundreq = 0;
836 		TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) {
837 			if (cr->cr_xid == xid) {
838 				/*
839 				 * This one matches. We snip it out of
840 				 * the pending list and leave the
841 				 * reply mbuf in cr->cr_mrep. Set the
842 				 * XID to zero so that clnt_dg_call
843 				 * can know not to repeat the
844 				 * TAILQ_REMOVE.
845 				 */
846 				TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
847 				cr->cr_xid = 0;
848 				cr->cr_mrep = m;
849 				cr->cr_error = 0;
850 				foundreq = 1;
851 				wakeup(cr);
852 				break;
853 			}
854 		}
855 		mtx_unlock(&cs->cs_lock);
856 
857 		/*
858 		 * If we didn't find the matching request, just drop
859 		 * it - its probably a repeated reply.
860 		 */
861 		if (!foundreq)
862 			m_freem(m);
863 	} while (m);
864 }
865 
866