xref: /freebsd/lib/libc/rpc/clnt_dg.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*	$NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34  */
35 
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #ident	"@(#)clnt_dg.c	1.23	94/04/22 SMI"
38 static char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro";
39 #endif
40 /*
41  * Implements a connectionless client side RPC.
42  */
43 
44 #include "namespace.h"
45 #include "reentrant.h"
46 #include <sys/types.h>
47 #include <sys/event.h>
48 #include <sys/time.h>
49 #include <sys/socket.h>
50 #include <sys/ioctl.h>
51 #include <arpa/inet.h>
52 #include <rpc/rpc.h>
53 #include <rpc/rpcsec_gss.h>
54 #include <errno.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <signal.h>
58 #include <unistd.h>
59 #include <err.h>
60 #include "un-namespace.h"
61 #include "rpc_com.h"
62 #include "mt_misc.h"
63 
64 
65 #ifdef _FREEFALL_CONFIG
66 /*
67  * Disable RPC exponential back-off for FreeBSD.org systems.
68  */
69 #define	RPC_MAX_BACKOFF		1 /* second */
70 #else
71 #define	RPC_MAX_BACKOFF		30 /* seconds */
72 #endif
73 
74 
75 static struct clnt_ops *clnt_dg_ops(void);
76 static bool_t time_not_ok(struct timeval *);
77 static enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
78 	    xdrproc_t, void *, struct timeval);
79 static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
80 static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *);
81 static void clnt_dg_abort(CLIENT *);
82 static bool_t clnt_dg_control(CLIENT *, u_int, void *);
83 static void clnt_dg_destroy(CLIENT *);
84 
85 
86 
87 
88 /*
89  *	This machinery implements per-fd locks for MT-safety.  It is not
90  *	sufficient to do per-CLIENT handle locks for MT-safety because a
91  *	user may create more than one CLIENT handle with the same fd behind
92  *	it.  Therefore, we allocate an array of flags (dg_fd_locks), protected
93  *	by the clnt_fd_lock mutex, and an array (dg_cv) of condition variables
94  *	similarly protected.  Dg_fd_lock[fd] == 1 => a call is active on some
95  *	CLIENT handle created for that fd.
96  *	The current implementation holds locks across the entire RPC and reply,
97  *	including retransmissions.  Yes, this is silly, and as soon as this
98  *	code is proven to work, this should be the first thing fixed.  One step
99  *	at a time.
100  */
101 static int	*dg_fd_locks;
102 static cond_t	*dg_cv;
103 #define	release_fd_lock(fd, mask) {		\
104 	mutex_lock(&clnt_fd_lock);	\
105 	dg_fd_locks[fd] = 0;		\
106 	mutex_unlock(&clnt_fd_lock);	\
107 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL); \
108 	cond_signal(&dg_cv[fd]);	\
109 }
110 
111 static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
112 
113 /* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd_locks, dg_cv */
114 
115 #define	MCALL_MSG_SIZE 24
116 
117 /*
118  * Private data kept per client handle
119  */
120 struct cu_data {
121 	int			cu_fd;		/* connections fd */
122 	bool_t			cu_closeit;	/* opened by library */
123 	struct sockaddr_storage	cu_raddr;	/* remote address */
124 	int			cu_rlen;
125 	struct timeval		cu_wait;	/* retransmit interval */
126 	struct timeval		cu_total;	/* total time for the call */
127 	struct rpc_err		cu_error;
128 	XDR			cu_outxdrs;
129 	u_int			cu_xdrpos;
130 	u_int			cu_sendsz;	/* send size */
131 	char			cu_outhdr[MCALL_MSG_SIZE];
132 	char			*cu_outbuf;
133 	u_int			cu_recvsz;	/* recv size */
134 	int			cu_async;
135 	int			cu_connect;	/* Use connect(). */
136 	int			cu_connected;	/* Have done connect(). */
137 	struct kevent		cu_kin;
138 	int			cu_kq;
139 	char			cu_inbuf[1];
140 };
141 
142 /*
143  * Connection less client creation returns with client handle parameters.
144  * Default options are set, which the user can change using clnt_control().
145  * fd should be open and bound.
146  * NB: The rpch->cl_auth is initialized to null authentication.
147  * 	Caller may wish to set this something more useful.
148  *
149  * sendsz and recvsz are the maximum allowable packet sizes that can be
150  * sent and received. Normally they are the same, but they can be
151  * changed to improve the program efficiency and buffer allocation.
152  * If they are 0, use the transport default.
153  *
154  * If svcaddr is NULL, returns NULL.
155  *
156  * fd      - open file descriptor
157  * svcaddr - servers address
158  * program - program number
159  * version - version number
160  * sendsz  - buffer recv size
161  * recvsz  - buffer send size
162  */
163 CLIENT *
164 clnt_dg_create(int fd, const struct netbuf *svcaddr, rpcprog_t program,
165     rpcvers_t version, u_int sendsz, u_int recvsz)
166 {
167 	CLIENT *cl = NULL;		/* client handle */
168 	struct cu_data *cu = NULL;	/* private data */
169 	struct timeval now;
170 	struct rpc_msg call_msg;
171 	sigset_t mask;
172 	sigset_t newmask;
173 	struct __rpc_sockinfo si;
174 	int one = 1;
175 
176 	sigfillset(&newmask);
177 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
178 	mutex_lock(&clnt_fd_lock);
179 	if (dg_fd_locks == (int *) NULL) {
180 		int cv_allocsz;
181 		size_t fd_allocsz;
182 		int dtbsize = __rpc_dtbsize();
183 
184 		fd_allocsz = dtbsize * sizeof (int);
185 		dg_fd_locks = (int *) mem_alloc(fd_allocsz);
186 		if (dg_fd_locks == (int *) NULL) {
187 			mutex_unlock(&clnt_fd_lock);
188 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
189 			goto err1;
190 		} else
191 			memset(dg_fd_locks, '\0', fd_allocsz);
192 
193 		cv_allocsz = dtbsize * sizeof (cond_t);
194 		dg_cv = (cond_t *) mem_alloc(cv_allocsz);
195 		if (dg_cv == (cond_t *) NULL) {
196 			mem_free(dg_fd_locks, fd_allocsz);
197 			dg_fd_locks = (int *) NULL;
198 			mutex_unlock(&clnt_fd_lock);
199 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
200 			goto err1;
201 		} else {
202 			int i;
203 
204 			for (i = 0; i < dtbsize; i++)
205 				cond_init(&dg_cv[i], 0, (void *) 0);
206 		}
207 	}
208 
209 	mutex_unlock(&clnt_fd_lock);
210 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
211 
212 	if (svcaddr == NULL) {
213 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
214 		return (NULL);
215 	}
216 
217 	if (!__rpc_fd2sockinfo(fd, &si)) {
218 		rpc_createerr.cf_stat = RPC_TLIERROR;
219 		rpc_createerr.cf_error.re_errno = 0;
220 		return (NULL);
221 	}
222 	/*
223 	 * Find the receive and the send size
224 	 */
225 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
226 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
227 	if ((sendsz == 0) || (recvsz == 0)) {
228 		rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
229 		rpc_createerr.cf_error.re_errno = 0;
230 		return (NULL);
231 	}
232 
233 	if ((cl = mem_alloc(sizeof (CLIENT))) == NULL)
234 		goto err1;
235 	/*
236 	 * Should be multiple of 4 for XDR.
237 	 */
238 	sendsz = ((sendsz + 3) / 4) * 4;
239 	recvsz = ((recvsz + 3) / 4) * 4;
240 	cu = mem_alloc(sizeof (*cu) + sendsz + recvsz);
241 	if (cu == NULL)
242 		goto err1;
243 	(void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len);
244 	cu->cu_rlen = svcaddr->len;
245 	cu->cu_outbuf = &cu->cu_inbuf[recvsz];
246 	/* Other values can also be set through clnt_control() */
247 	cu->cu_wait.tv_sec = 15;	/* heuristically chosen */
248 	cu->cu_wait.tv_usec = 0;
249 	cu->cu_total.tv_sec = -1;
250 	cu->cu_total.tv_usec = -1;
251 	cu->cu_sendsz = sendsz;
252 	cu->cu_recvsz = recvsz;
253 	cu->cu_async = FALSE;
254 	cu->cu_connect = FALSE;
255 	cu->cu_connected = FALSE;
256 	(void) gettimeofday(&now, NULL);
257 	call_msg.rm_xid = __RPC_GETXID(&now);
258 	call_msg.rm_call.cb_prog = program;
259 	call_msg.rm_call.cb_vers = version;
260 	xdrmem_create(&(cu->cu_outxdrs), cu->cu_outhdr, MCALL_MSG_SIZE,
261 	    XDR_ENCODE);
262 	if (! xdr_callhdr(&cu->cu_outxdrs, &call_msg)) {
263 		rpc_createerr.cf_stat = RPC_CANTENCODEARGS;  /* XXX */
264 		rpc_createerr.cf_error.re_errno = 0;
265 		goto err2;
266 	}
267 	cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
268 	XDR_DESTROY(&cu->cu_outxdrs);
269 	xdrmem_create(&cu->cu_outxdrs, cu->cu_outbuf, sendsz, XDR_ENCODE);
270 
271 	/* XXX fvdl - do we still want this? */
272 #if 0
273 	(void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
274 #endif
275 	_ioctl(fd, FIONBIO, (char *)(void *)&one);
276 
277 	/*
278 	 * By default, closeit is always FALSE. It is users responsibility
279 	 * to do a close on it, else the user may use clnt_control
280 	 * to let clnt_destroy do it for him/her.
281 	 */
282 	cu->cu_closeit = FALSE;
283 	cu->cu_fd = fd;
284 	cl->cl_ops = clnt_dg_ops();
285 	cl->cl_private = (caddr_t)(void *)cu;
286 	cl->cl_auth = authnone_create();
287 	cl->cl_tp = NULL;
288 	cl->cl_netid = NULL;
289 	cu->cu_kq = -1;
290 	EV_SET(&cu->cu_kin, cu->cu_fd, EVFILT_READ, EV_ADD, 0, 0, 0);
291 	return (cl);
292 err1:
293 	warnx(mem_err_clnt_dg);
294 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
295 	rpc_createerr.cf_error.re_errno = errno;
296 err2:
297 	if (cl) {
298 		mem_free(cl, sizeof (CLIENT));
299 		if (cu)
300 			mem_free(cu, sizeof (*cu) + sendsz + recvsz);
301 	}
302 	return (NULL);
303 }
304 
305 /*
306  * cl       - client handle
307  * proc     - procedure number
308  * xargs    - xdr routine for args
309  * argsp    - pointer to args
310  * xresults - xdr routine for results
311  * resultsp - pointer to results
312  * utimeout - seconds to wait before giving up
313  */
314 static enum clnt_stat
315 clnt_dg_call(CLIENT *cl, rpcproc_t proc, xdrproc_t xargs, void *argsp,
316     xdrproc_t xresults, void *resultsp, struct timeval utimeout)
317 {
318 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
319 	XDR *xdrs;
320 	size_t outlen = 0;
321 	struct rpc_msg reply_msg;
322 	XDR reply_xdrs;
323 	bool_t ok;
324 	int nrefreshes = 2;		/* number of times to refresh cred */
325 	int nretries = 0;		/* number of times we retransmitted */
326 	struct timeval timeout;
327 	struct timeval retransmit_time;
328 	struct timeval next_sendtime, starttime, time_waited, tv;
329 	struct timespec ts;
330 	struct kevent kv;
331 	struct sockaddr *sa;
332 	sigset_t mask;
333 	sigset_t newmask;
334 	socklen_t salen;
335 	ssize_t recvlen = 0;
336 	int kin_len, n, rpc_lock_value;
337 	u_int32_t xid;
338 
339 	outlen = 0;
340 	sigfillset(&newmask);
341 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
342 	mutex_lock(&clnt_fd_lock);
343 	while (dg_fd_locks[cu->cu_fd])
344 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
345 	if (__isthreaded)
346 		rpc_lock_value = 1;
347 	else
348 		rpc_lock_value = 0;
349 	dg_fd_locks[cu->cu_fd] = rpc_lock_value;
350 	mutex_unlock(&clnt_fd_lock);
351 	if (cu->cu_total.tv_usec == -1) {
352 		timeout = utimeout;	/* use supplied timeout */
353 	} else {
354 		timeout = cu->cu_total;	/* use default timeout */
355 	}
356 
357 	if (cu->cu_connect && !cu->cu_connected) {
358 		if (_connect(cu->cu_fd, (struct sockaddr *)&cu->cu_raddr,
359 		    cu->cu_rlen) < 0) {
360 			cu->cu_error.re_errno = errno;
361 			cu->cu_error.re_status = RPC_CANTSEND;
362 			goto out;
363 		}
364 		cu->cu_connected = 1;
365 	}
366 	if (cu->cu_connected) {
367 		sa = NULL;
368 		salen = 0;
369 	} else {
370 		sa = (struct sockaddr *)&cu->cu_raddr;
371 		salen = cu->cu_rlen;
372 	}
373 	time_waited.tv_sec = 0;
374 	time_waited.tv_usec = 0;
375 	retransmit_time = next_sendtime = cu->cu_wait;
376 	gettimeofday(&starttime, NULL);
377 
378 	/* Clean up in case the last call ended in a longjmp(3) call. */
379 	if (cu->cu_kq >= 0)
380 		_close(cu->cu_kq);
381 	if ((cu->cu_kq = kqueue()) < 0) {
382 		cu->cu_error.re_errno = errno;
383 		cu->cu_error.re_status = RPC_CANTSEND;
384 		goto out;
385 	}
386 	kin_len = 1;
387 
388 call_again:
389 	if (cu->cu_async == TRUE && xargs == NULL)
390 		goto get_reply;
391 	/*
392 	 * the transaction is the first thing in the out buffer
393 	 * XXX Yes, and it's in network byte order, so we should to
394 	 * be careful when we increment it, shouldn't we.
395 	 */
396 	xid = ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr));
397 	xid++;
398 	*(u_int32_t *)(void *)(cu->cu_outhdr) = htonl(xid);
399 call_again_same_xid:
400 	xdrs = &(cu->cu_outxdrs);
401 	xdrs->x_op = XDR_ENCODE;
402 	XDR_SETPOS(xdrs, 0);
403 
404 	if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
405 		if ((! XDR_PUTBYTES(xdrs, cu->cu_outhdr, cu->cu_xdrpos)) ||
406 		    (! XDR_PUTINT32(xdrs, &proc)) ||
407 		    (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
408 		    (! (*xargs)(xdrs, argsp))) {
409 			cu->cu_error.re_status = RPC_CANTENCODEARGS;
410 			goto out;
411 		}
412 	} else {
413 		*(uint32_t *) &cu->cu_outhdr[cu->cu_xdrpos] = htonl(proc);
414 		if (!__rpc_gss_wrap(cl->cl_auth, cu->cu_outhdr,
415 			cu->cu_xdrpos + sizeof(uint32_t),
416 			xdrs, xargs, argsp)) {
417 			cu->cu_error.re_status = RPC_CANTENCODEARGS;
418 			goto out;
419 		}
420 	}
421 	outlen = (size_t)XDR_GETPOS(xdrs);
422 
423 send_again:
424 	if (_sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0, sa, salen) != outlen) {
425 		cu->cu_error.re_errno = errno;
426 		cu->cu_error.re_status = RPC_CANTSEND;
427 		goto out;
428 	}
429 
430 	/*
431 	 * Hack to provide rpc-based message passing
432 	 */
433 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
434 		cu->cu_error.re_status = RPC_TIMEDOUT;
435 		goto out;
436 	}
437 
438 get_reply:
439 
440 	/*
441 	 * sub-optimal code appears here because we have
442 	 * some clock time to spare while the packets are in flight.
443 	 * (We assume that this is actually only executed once.)
444 	 */
445 	reply_msg.acpted_rply.ar_verf = _null_auth;
446 	if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
447 		reply_msg.acpted_rply.ar_results.where = resultsp;
448 		reply_msg.acpted_rply.ar_results.proc = xresults;
449 	} else {
450 		reply_msg.acpted_rply.ar_results.where = NULL;
451 		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
452 	}
453 
454 	for (;;) {
455 		/* Decide how long to wait. */
456 		if (timercmp(&next_sendtime, &timeout, <))
457 			timersub(&next_sendtime, &time_waited, &tv);
458 		else
459 			timersub(&timeout, &time_waited, &tv);
460 		if (tv.tv_sec < 0 || tv.tv_usec < 0)
461 			tv.tv_sec = tv.tv_usec = 0;
462 		TIMEVAL_TO_TIMESPEC(&tv, &ts);
463 
464 		n = _kevent(cu->cu_kq, &cu->cu_kin, kin_len, &kv, 1, &ts);
465 		/* We don't need to register the event again. */
466 		kin_len = 0;
467 
468 		if (n == 1) {
469 			if (kv.flags & EV_ERROR) {
470 				cu->cu_error.re_errno = kv.data;
471 				cu->cu_error.re_status = RPC_CANTRECV;
472 				goto out;
473 			}
474 			/* We have some data now */
475 			do {
476 				recvlen = _recvfrom(cu->cu_fd, cu->cu_inbuf,
477 				    cu->cu_recvsz, 0, NULL, NULL);
478 			} while (recvlen < 0 && errno == EINTR);
479 			if (recvlen < 0 && errno != EWOULDBLOCK) {
480 				cu->cu_error.re_errno = errno;
481 				cu->cu_error.re_status = RPC_CANTRECV;
482 				goto out;
483 			}
484 			if (recvlen >= sizeof(u_int32_t) &&
485 			    (cu->cu_async == TRUE ||
486 			    *((u_int32_t *)(void *)(cu->cu_inbuf)) ==
487 			    *((u_int32_t *)(void *)(cu->cu_outbuf)))) {
488 				/* We now assume we have the proper reply. */
489 				break;
490 			}
491 		}
492 		if (n == -1 && errno != EINTR) {
493 			cu->cu_error.re_errno = errno;
494 			cu->cu_error.re_status = RPC_CANTRECV;
495 			goto out;
496 		}
497 		gettimeofday(&tv, NULL);
498 		timersub(&tv, &starttime, &time_waited);
499 
500 		/* Check for timeout. */
501 		if (timercmp(&time_waited, &timeout, >)) {
502 			cu->cu_error.re_status = RPC_TIMEDOUT;
503 			goto out;
504 		}
505 
506 		/* Retransmit if necessary. */
507 		if (timercmp(&time_waited, &next_sendtime, >)) {
508 			/* update retransmit_time */
509 			if (retransmit_time.tv_sec < RPC_MAX_BACKOFF)
510 				timeradd(&retransmit_time, &retransmit_time,
511 				    &retransmit_time);
512 			timeradd(&next_sendtime, &retransmit_time,
513 			    &next_sendtime);
514 			nretries++;
515 
516 			/*
517 			 * When retransmitting a RPCSEC_GSS message,
518 			 * we must use a new sequence number (handled
519 			 * by __rpc_gss_wrap above).
520 			 */
521 			if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS)
522 				goto send_again;
523 			else
524 				goto call_again_same_xid;
525 		}
526 	}
527 
528 	/*
529 	 * now decode and validate the response
530 	 */
531 
532 	xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE);
533 	ok = xdr_replymsg(&reply_xdrs, &reply_msg);
534 	/* XDR_DESTROY(&reply_xdrs);	save a few cycles on noop destroy */
535 	if (ok) {
536 		if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
537 			(reply_msg.acpted_rply.ar_stat == SUCCESS))
538 			cu->cu_error.re_status = RPC_SUCCESS;
539 		else
540 			_seterr_reply(&reply_msg, &(cu->cu_error));
541 
542 		if (cu->cu_error.re_status == RPC_SUCCESS) {
543 			if (! AUTH_VALIDATE(cl->cl_auth,
544 					    &reply_msg.acpted_rply.ar_verf)) {
545 				if (nretries &&
546 				    cl->cl_auth->ah_cred.oa_flavor
547 				    == RPCSEC_GSS)
548 					/*
549 					 * If we retransmitted, its
550 					 * possible that we will
551 					 * receive a reply for one of
552 					 * the earlier transmissions
553 					 * (which will use an older
554 					 * RPCSEC_GSS sequence
555 					 * number). In this case, just
556 					 * go back and listen for a
557 					 * new reply. We could keep a
558 					 * record of all the seq
559 					 * numbers we have transmitted
560 					 * so far so that we could
561 					 * accept a reply for any of
562 					 * them here.
563 					 */
564 					goto get_reply;
565 				cu->cu_error.re_status = RPC_AUTHERROR;
566 				cu->cu_error.re_why = AUTH_INVALIDRESP;
567 			} else {
568 				if (cl->cl_auth->ah_cred.oa_flavor
569 				    == RPCSEC_GSS) {
570 					if (!__rpc_gss_unwrap(cl->cl_auth,
571 						&reply_xdrs, xresults,
572 						resultsp))
573 						cu->cu_error.re_status =
574 							RPC_CANTDECODERES;
575 				}
576 			}
577 			if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
578 				xdrs->x_op = XDR_FREE;
579 				(void) xdr_opaque_auth(xdrs,
580 					&(reply_msg.acpted_rply.ar_verf));
581 			}
582 		}		/* end successful completion */
583 		/*
584 		 * If unsuccessful AND error is an authentication error
585 		 * then refresh credentials and try again, else break
586 		 */
587 		else if (cu->cu_error.re_status == RPC_AUTHERROR)
588 			/* maybe our credentials need to be refreshed ... */
589 			if (nrefreshes > 0 &&
590 			    AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
591 				nrefreshes--;
592 				goto call_again;
593 			}
594 		/* end of unsuccessful completion */
595 	}	/* end of valid reply message */
596 	else {
597 		cu->cu_error.re_status = RPC_CANTDECODERES;
598 
599 	}
600 out:
601 	if (cu->cu_kq >= 0)
602 		_close(cu->cu_kq);
603 	cu->cu_kq = -1;
604 	release_fd_lock(cu->cu_fd, mask);
605 	return (cu->cu_error.re_status);
606 }
607 
608 static void
609 clnt_dg_geterr(CLIENT *cl, struct rpc_err *errp)
610 {
611 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
612 
613 	*errp = cu->cu_error;
614 }
615 
616 static bool_t
617 clnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
618 {
619 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
620 	XDR *xdrs = &(cu->cu_outxdrs);
621 	bool_t dummy;
622 	sigset_t mask;
623 	sigset_t newmask;
624 
625 	sigfillset(&newmask);
626 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
627 	mutex_lock(&clnt_fd_lock);
628 	while (dg_fd_locks[cu->cu_fd])
629 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
630 	xdrs->x_op = XDR_FREE;
631 	dummy = (*xdr_res)(xdrs, res_ptr);
632 	mutex_unlock(&clnt_fd_lock);
633 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
634 	cond_signal(&dg_cv[cu->cu_fd]);
635 	return (dummy);
636 }
637 
638 /*ARGSUSED*/
639 static void
640 clnt_dg_abort(CLIENT *h)
641 {
642 }
643 
644 static bool_t
645 clnt_dg_control(CLIENT *cl, u_int request, void *info)
646 {
647 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
648 	struct netbuf *addr;
649 	sigset_t mask;
650 	sigset_t newmask;
651 	int rpc_lock_value;
652 
653 	sigfillset(&newmask);
654 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
655 	mutex_lock(&clnt_fd_lock);
656 	while (dg_fd_locks[cu->cu_fd])
657 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
658 	if (__isthreaded)
659                 rpc_lock_value = 1;
660         else
661                 rpc_lock_value = 0;
662 	dg_fd_locks[cu->cu_fd] = rpc_lock_value;
663 	mutex_unlock(&clnt_fd_lock);
664 	switch (request) {
665 	case CLSET_FD_CLOSE:
666 		cu->cu_closeit = TRUE;
667 		release_fd_lock(cu->cu_fd, mask);
668 		return (TRUE);
669 	case CLSET_FD_NCLOSE:
670 		cu->cu_closeit = FALSE;
671 		release_fd_lock(cu->cu_fd, mask);
672 		return (TRUE);
673 	}
674 
675 	/* for other requests which use info */
676 	if (info == NULL) {
677 		release_fd_lock(cu->cu_fd, mask);
678 		return (FALSE);
679 	}
680 	switch (request) {
681 	case CLSET_TIMEOUT:
682 		if (time_not_ok((struct timeval *)info)) {
683 			release_fd_lock(cu->cu_fd, mask);
684 			return (FALSE);
685 		}
686 		cu->cu_total = *(struct timeval *)info;
687 		break;
688 	case CLGET_TIMEOUT:
689 		*(struct timeval *)info = cu->cu_total;
690 		break;
691 	case CLGET_SERVER_ADDR:		/* Give him the fd address */
692 		/* Now obsolete. Only for backward compatibility */
693 		(void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
694 		break;
695 	case CLSET_RETRY_TIMEOUT:
696 		if (time_not_ok((struct timeval *)info)) {
697 			release_fd_lock(cu->cu_fd, mask);
698 			return (FALSE);
699 		}
700 		cu->cu_wait = *(struct timeval *)info;
701 		break;
702 	case CLGET_RETRY_TIMEOUT:
703 		*(struct timeval *)info = cu->cu_wait;
704 		break;
705 	case CLGET_FD:
706 		*(int *)info = cu->cu_fd;
707 		break;
708 	case CLGET_SVC_ADDR:
709 		addr = (struct netbuf *)info;
710 		addr->buf = &cu->cu_raddr;
711 		addr->len = cu->cu_rlen;
712 		addr->maxlen = sizeof cu->cu_raddr;
713 		break;
714 	case CLSET_SVC_ADDR:		/* set to new address */
715 		addr = (struct netbuf *)info;
716 		if (addr->len < sizeof cu->cu_raddr) {
717 			release_fd_lock(cu->cu_fd, mask);
718 			return (FALSE);
719 		}
720 		(void) memcpy(&cu->cu_raddr, addr->buf, addr->len);
721 		cu->cu_rlen = addr->len;
722 		break;
723 	case CLGET_XID:
724 		/*
725 		 * use the knowledge that xid is the
726 		 * first element in the call structure *.
727 		 * This will get the xid of the PREVIOUS call
728 		 */
729 		*(u_int32_t *)info =
730 		    ntohl(*(u_int32_t *)(void *)cu->cu_outhdr);
731 		break;
732 
733 	case CLSET_XID:
734 		/* This will set the xid of the NEXT call */
735 		*(u_int32_t *)(void *)cu->cu_outhdr =
736 		    htonl(*(u_int32_t *)info - 1);
737 		/* decrement by 1 as clnt_dg_call() increments once */
738 		break;
739 
740 	case CLGET_VERS:
741 		/*
742 		 * This RELIES on the information that, in the call body,
743 		 * the version number field is the fifth field from the
744 		 * beginning of the RPC header. MUST be changed if the
745 		 * call_struct is changed
746 		 */
747 		*(u_int32_t *)info =
748 		    ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr +
749 		    4 * BYTES_PER_XDR_UNIT));
750 		break;
751 
752 	case CLSET_VERS:
753 		*(u_int32_t *)(void *)(cu->cu_outhdr + 4 * BYTES_PER_XDR_UNIT)
754 			= htonl(*(u_int32_t *)info);
755 		break;
756 
757 	case CLGET_PROG:
758 		/*
759 		 * This RELIES on the information that, in the call body,
760 		 * the program number field is the fourth field from the
761 		 * beginning of the RPC header. MUST be changed if the
762 		 * call_struct is changed
763 		 */
764 		*(u_int32_t *)info =
765 		    ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr +
766 		    3 * BYTES_PER_XDR_UNIT));
767 		break;
768 
769 	case CLSET_PROG:
770 		*(u_int32_t *)(void *)(cu->cu_outhdr + 3 * BYTES_PER_XDR_UNIT)
771 			= htonl(*(u_int32_t *)info);
772 		break;
773 	case CLSET_ASYNC:
774 		cu->cu_async = *(int *)info;
775 		break;
776 	case CLSET_CONNECT:
777 		cu->cu_connect = *(int *)info;
778 		break;
779 	default:
780 		release_fd_lock(cu->cu_fd, mask);
781 		return (FALSE);
782 	}
783 	release_fd_lock(cu->cu_fd, mask);
784 	return (TRUE);
785 }
786 
787 static void
788 clnt_dg_destroy(CLIENT *cl)
789 {
790 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
791 	int cu_fd = cu->cu_fd;
792 	sigset_t mask;
793 	sigset_t newmask;
794 
795 	sigfillset(&newmask);
796 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
797 	mutex_lock(&clnt_fd_lock);
798 	while (dg_fd_locks[cu_fd])
799 		cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
800 	if (cu->cu_closeit)
801 		(void)_close(cu_fd);
802 	if (cu->cu_kq >= 0)
803 		_close(cu->cu_kq);
804 	XDR_DESTROY(&(cu->cu_outxdrs));
805 	mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
806 	if (cl->cl_netid && cl->cl_netid[0])
807 		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
808 	if (cl->cl_tp && cl->cl_tp[0])
809 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
810 	mem_free(cl, sizeof (CLIENT));
811 	mutex_unlock(&clnt_fd_lock);
812 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
813 	cond_signal(&dg_cv[cu_fd]);
814 }
815 
816 static struct clnt_ops *
817 clnt_dg_ops(void)
818 {
819 	static struct clnt_ops ops;
820 	sigset_t mask;
821 	sigset_t newmask;
822 
823 /* VARIABLES PROTECTED BY ops_lock: ops */
824 
825 	sigfillset(&newmask);
826 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
827 	mutex_lock(&ops_lock);
828 	if (ops.cl_call == NULL) {
829 		ops.cl_call = clnt_dg_call;
830 		ops.cl_abort = clnt_dg_abort;
831 		ops.cl_geterr = clnt_dg_geterr;
832 		ops.cl_freeres = clnt_dg_freeres;
833 		ops.cl_destroy = clnt_dg_destroy;
834 		ops.cl_control = clnt_dg_control;
835 	}
836 	mutex_unlock(&ops_lock);
837 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
838 	return (&ops);
839 }
840 
841 /*
842  * Make sure that the time is not garbage.  -1 value is allowed.
843  */
844 static bool_t
845 time_not_ok(struct timeval *t)
846 {
847 	return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
848 		t->tv_usec < -1 || t->tv_usec > 1000000);
849 }
850 
851