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