xref: /freebsd/lib/libc/rpc/clnt_vc.c (revision a7623790fb345e6dc986dfd31df0ace115e6f2e4)
1 /*	$NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 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 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)clnt_tcp.c	2.2 88/08/01 4.0 RPCSRC";
36 static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
37 #endif
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
43  *
44  * Copyright (C) 1984, Sun Microsystems, Inc.
45  *
46  * TCP based RPC supports 'batched calls'.
47  * A sequence of calls may be batched-up in a send buffer.  The rpc call
48  * return immediately to the client even though the call was not necessarily
49  * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
50  * the rpc timeout value is zero (see clnt.h, rpc).
51  *
52  * Clients should NOT casually batch calls that in fact return results; that is,
53  * the server side should be aware that a call is batched and not produce any
54  * return message.  Batched calls that produce many result messages can
55  * deadlock (netlock) the client and the server....
56  *
57  * Now go hang yourself.
58  */
59 
60 #include "namespace.h"
61 #include "reentrant.h"
62 #include <sys/types.h>
63 #include <sys/poll.h>
64 #include <sys/syslog.h>
65 #include <sys/socket.h>
66 #include <sys/un.h>
67 #include <sys/uio.h>
68 
69 #include <arpa/inet.h>
70 #include <assert.h>
71 #include <err.h>
72 #include <errno.h>
73 #include <netdb.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <unistd.h>
78 #include <signal.h>
79 
80 #include <rpc/rpc.h>
81 #include <rpc/rpcsec_gss.h>
82 #include "un-namespace.h"
83 #include "rpc_com.h"
84 #include "mt_misc.h"
85 
86 #define MCALL_MSG_SIZE 24
87 
88 struct cmessage {
89         struct cmsghdr cmsg;
90         struct cmsgcred cmcred;
91 };
92 
93 static enum clnt_stat clnt_vc_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
94     xdrproc_t, void *, struct timeval);
95 static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
96 static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *);
97 static void clnt_vc_abort(CLIENT *);
98 static bool_t clnt_vc_control(CLIENT *, u_int, void *);
99 static void clnt_vc_destroy(CLIENT *);
100 static struct clnt_ops *clnt_vc_ops(void);
101 static bool_t time_not_ok(struct timeval *);
102 static int read_vc(void *, void *, int);
103 static int write_vc(void *, void *, int);
104 static int __msgwrite(int, void *, size_t);
105 static int __msgread(int, void *, size_t);
106 
107 struct ct_data {
108 	int		ct_fd;		/* connection's fd */
109 	bool_t		ct_closeit;	/* close it on destroy */
110 	struct timeval	ct_wait;	/* wait interval in milliseconds */
111 	bool_t          ct_waitset;	/* wait set by clnt_control? */
112 	struct netbuf	ct_addr;	/* remote addr */
113 	struct rpc_err	ct_error;
114 	union {
115 		char	ct_mcallc[MCALL_MSG_SIZE];	/* marshalled callmsg */
116 		u_int32_t ct_mcalli;
117 	} ct_u;
118 	u_int		ct_mpos;	/* pos after marshal */
119 	XDR		ct_xdrs;	/* XDR stream */
120 };
121 
122 /*
123  *      This machinery implements per-fd locks for MT-safety.  It is not
124  *      sufficient to do per-CLIENT handle locks for MT-safety because a
125  *      user may create more than one CLIENT handle with the same fd behind
126  *      it.  Therfore, we allocate an array of flags (vc_fd_locks), protected
127  *      by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
128  *      similarly protected.  Vc_fd_lock[fd] == 1 => a call is activte on some
129  *      CLIENT handle created for that fd.
130  *      The current implementation holds locks across the entire RPC and reply.
131  *      Yes, this is silly, and as soon as this code is proven to work, this
132  *      should be the first thing fixed.  One step at a time.
133  */
134 static int      *vc_fd_locks;
135 static cond_t   *vc_cv;
136 #define release_fd_lock(fd, mask) {	\
137 	mutex_lock(&clnt_fd_lock);	\
138 	vc_fd_locks[fd] = 0;		\
139 	mutex_unlock(&clnt_fd_lock);	\
140 	thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);	\
141 	cond_signal(&vc_cv[fd]);	\
142 }
143 
144 static const char clnt_vc_errstr[] = "%s : %s";
145 static const char clnt_vc_str[] = "clnt_vc_create";
146 static const char __no_mem_str[] = "out of memory";
147 
148 /*
149  * Create a client handle for a connection.
150  * Default options are set, which the user can change using clnt_control()'s.
151  * The rpc/vc package does buffering similar to stdio, so the client
152  * must pick send and receive buffer sizes, 0 => use the default.
153  * NB: fd is copied into a private area.
154  * NB: The rpch->cl_auth is set null authentication. Caller may wish to
155  * set this something more useful.
156  *
157  * fd should be an open socket
158  *
159  * fd - open file descriptor
160  * raddr - servers address
161  * prog  - program number
162  * vers  - version number
163  * sendsz - buffer send size
164  * recvsz - buffer recv size
165  */
166 CLIENT *
167 clnt_vc_create(int fd, const struct netbuf *raddr, const rpcprog_t prog,
168     const rpcvers_t vers, u_int sendsz, u_int recvsz)
169 {
170 	CLIENT *cl;			/* client handle */
171 	struct ct_data *ct = NULL;	/* client handle */
172 	struct timeval now;
173 	struct rpc_msg call_msg;
174 	static u_int32_t disrupt;
175 	sigset_t mask;
176 	sigset_t newmask;
177 	struct sockaddr_storage ss;
178 	socklen_t slen;
179 	struct __rpc_sockinfo si;
180 
181 	if (disrupt == 0)
182 		disrupt = (u_int32_t)(long)raddr;
183 
184 	cl = (CLIENT *)mem_alloc(sizeof (*cl));
185 	ct = (struct ct_data *)mem_alloc(sizeof (*ct));
186 	if ((cl == (CLIENT *)NULL) || (ct == (struct ct_data *)NULL)) {
187 		(void) syslog(LOG_ERR, clnt_vc_errstr,
188 		    clnt_vc_str, __no_mem_str);
189 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
190 		rpc_createerr.cf_error.re_errno = errno;
191 		goto err;
192 	}
193 	ct->ct_addr.buf = NULL;
194 	sigfillset(&newmask);
195 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
196 	mutex_lock(&clnt_fd_lock);
197 	if (vc_fd_locks == (int *) NULL) {
198 		int cv_allocsz, fd_allocsz;
199 		int dtbsize = __rpc_dtbsize();
200 
201 		fd_allocsz = dtbsize * sizeof (int);
202 		vc_fd_locks = (int *) mem_alloc(fd_allocsz);
203 		if (vc_fd_locks == (int *) NULL) {
204 			mutex_unlock(&clnt_fd_lock);
205 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
206 			goto err;
207 		} else
208 			memset(vc_fd_locks, '\0', fd_allocsz);
209 
210 		assert(vc_cv == (cond_t *) NULL);
211 		cv_allocsz = dtbsize * sizeof (cond_t);
212 		vc_cv = (cond_t *) mem_alloc(cv_allocsz);
213 		if (vc_cv == (cond_t *) NULL) {
214 			mem_free(vc_fd_locks, fd_allocsz);
215 			vc_fd_locks = (int *) NULL;
216 			mutex_unlock(&clnt_fd_lock);
217 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
218 			goto err;
219 		} else {
220 			int i;
221 
222 			for (i = 0; i < dtbsize; i++)
223 				cond_init(&vc_cv[i], 0, (void *) 0);
224 		}
225 	} else
226 		assert(vc_cv != (cond_t *) NULL);
227 
228 	/*
229 	 * XXX - fvdl connecting while holding a mutex?
230 	 */
231 	slen = sizeof ss;
232 	if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
233 		if (errno != ENOTCONN) {
234 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
235 			rpc_createerr.cf_error.re_errno = errno;
236 			mutex_unlock(&clnt_fd_lock);
237 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
238 			goto err;
239 		}
240 		if (_connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
241 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
242 			rpc_createerr.cf_error.re_errno = errno;
243 			mutex_unlock(&clnt_fd_lock);
244 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
245 			goto err;
246 		}
247 	}
248 	mutex_unlock(&clnt_fd_lock);
249 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
250 	if (!__rpc_fd2sockinfo(fd, &si))
251 		goto err;
252 
253 	ct->ct_closeit = FALSE;
254 
255 	/*
256 	 * Set up private data struct
257 	 */
258 	ct->ct_fd = fd;
259 	ct->ct_wait.tv_usec = 0;
260 	ct->ct_waitset = FALSE;
261 	ct->ct_addr.buf = malloc(raddr->maxlen);
262 	if (ct->ct_addr.buf == NULL)
263 		goto err;
264 	memcpy(ct->ct_addr.buf, raddr->buf, raddr->len);
265 	ct->ct_addr.len = raddr->len;
266 	ct->ct_addr.maxlen = raddr->maxlen;
267 
268 	/*
269 	 * Initialize call message
270 	 */
271 	(void)gettimeofday(&now, NULL);
272 	call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now);
273 	call_msg.rm_direction = CALL;
274 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
275 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
276 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
277 
278 	/*
279 	 * pre-serialize the static part of the call msg and stash it away
280 	 */
281 	xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
282 	    XDR_ENCODE);
283 	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
284 		if (ct->ct_closeit) {
285 			(void)_close(fd);
286 		}
287 		goto err;
288 	}
289 	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
290 	XDR_DESTROY(&(ct->ct_xdrs));
291 	assert(ct->ct_mpos + sizeof(uint32_t) <= MCALL_MSG_SIZE);
292 
293 	/*
294 	 * Create a client handle which uses xdrrec for serialization
295 	 * and authnone for authentication.
296 	 */
297 	cl->cl_ops = clnt_vc_ops();
298 	cl->cl_private = ct;
299 	cl->cl_auth = authnone_create();
300 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
301 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
302 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
303 	    cl->cl_private, read_vc, write_vc);
304 	return (cl);
305 
306 err:
307 	if (ct) {
308 		if (ct->ct_addr.len)
309 			mem_free(ct->ct_addr.buf, ct->ct_addr.len);
310 		mem_free(ct, sizeof (struct ct_data));
311 	}
312 	if (cl)
313 		mem_free(cl, sizeof (CLIENT));
314 	return ((CLIENT *)NULL);
315 }
316 
317 static enum clnt_stat
318 clnt_vc_call(CLIENT *cl, rpcproc_t proc, xdrproc_t xdr_args, void *args_ptr,
319     xdrproc_t xdr_results, void *results_ptr, struct timeval timeout)
320 {
321 	struct ct_data *ct = (struct ct_data *) cl->cl_private;
322 	XDR *xdrs = &(ct->ct_xdrs);
323 	struct rpc_msg reply_msg;
324 	u_int32_t x_id;
325 	u_int32_t *msg_x_id = &ct->ct_u.ct_mcalli;    /* yuk */
326 	bool_t shipnow;
327 	int refreshes = 2;
328 	sigset_t mask, newmask;
329 	int rpc_lock_value;
330 	bool_t reply_stat;
331 
332 	assert(cl != NULL);
333 
334 	sigfillset(&newmask);
335 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
336 	mutex_lock(&clnt_fd_lock);
337 	while (vc_fd_locks[ct->ct_fd])
338 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
339 	if (__isthreaded)
340                 rpc_lock_value = 1;
341         else
342                 rpc_lock_value = 0;
343 	vc_fd_locks[ct->ct_fd] = rpc_lock_value;
344 	mutex_unlock(&clnt_fd_lock);
345 	if (!ct->ct_waitset) {
346 		/* If time is not within limits, we ignore it. */
347 		if (time_not_ok(&timeout) == FALSE)
348 			ct->ct_wait = timeout;
349 	}
350 
351 	shipnow =
352 	    (xdr_results == NULL && timeout.tv_sec == 0
353 	    && timeout.tv_usec == 0) ? FALSE : TRUE;
354 
355 call_again:
356 	xdrs->x_op = XDR_ENCODE;
357 	ct->ct_error.re_status = RPC_SUCCESS;
358 	x_id = ntohl(--(*msg_x_id));
359 
360 	if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
361 		if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
362 		    (! XDR_PUTINT32(xdrs, &proc)) ||
363 		    (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
364 		    (! (*xdr_args)(xdrs, args_ptr))) {
365 			if (ct->ct_error.re_status == RPC_SUCCESS)
366 				ct->ct_error.re_status = RPC_CANTENCODEARGS;
367 			(void)xdrrec_endofrecord(xdrs, TRUE);
368 			release_fd_lock(ct->ct_fd, mask);
369 			return (ct->ct_error.re_status);
370 		}
371 	} else {
372 		*(uint32_t *) &ct->ct_u.ct_mcallc[ct->ct_mpos] = htonl(proc);
373 		if (! __rpc_gss_wrap(cl->cl_auth, ct->ct_u.ct_mcallc,
374 			ct->ct_mpos + sizeof(uint32_t),
375 			xdrs, xdr_args, args_ptr)) {
376 			if (ct->ct_error.re_status == RPC_SUCCESS)
377 				ct->ct_error.re_status = RPC_CANTENCODEARGS;
378 			(void)xdrrec_endofrecord(xdrs, TRUE);
379 			release_fd_lock(ct->ct_fd, mask);
380 			return (ct->ct_error.re_status);
381 		}
382 	}
383 	if (! xdrrec_endofrecord(xdrs, shipnow)) {
384 		release_fd_lock(ct->ct_fd, mask);
385 		return (ct->ct_error.re_status = RPC_CANTSEND);
386 	}
387 	if (! shipnow) {
388 		release_fd_lock(ct->ct_fd, mask);
389 		return (RPC_SUCCESS);
390 	}
391 	/*
392 	 * Hack to provide rpc-based message passing
393 	 */
394 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
395 		release_fd_lock(ct->ct_fd, mask);
396 		return(ct->ct_error.re_status = RPC_TIMEDOUT);
397 	}
398 
399 
400 	/*
401 	 * Keep receiving until we get a valid transaction id
402 	 */
403 	xdrs->x_op = XDR_DECODE;
404 	while (TRUE) {
405 		reply_msg.acpted_rply.ar_verf = _null_auth;
406 		reply_msg.acpted_rply.ar_results.where = NULL;
407 		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
408 		if (! xdrrec_skiprecord(xdrs)) {
409 			release_fd_lock(ct->ct_fd, mask);
410 			return (ct->ct_error.re_status);
411 		}
412 		/* now decode and validate the response header */
413 		if (! xdr_replymsg(xdrs, &reply_msg)) {
414 			if (ct->ct_error.re_status == RPC_SUCCESS)
415 				continue;
416 			release_fd_lock(ct->ct_fd, mask);
417 			return (ct->ct_error.re_status);
418 		}
419 		if (reply_msg.rm_xid == x_id)
420 			break;
421 	}
422 
423 	/*
424 	 * process header
425 	 */
426 	_seterr_reply(&reply_msg, &(ct->ct_error));
427 	if (ct->ct_error.re_status == RPC_SUCCESS) {
428 		if (! AUTH_VALIDATE(cl->cl_auth,
429 		    &reply_msg.acpted_rply.ar_verf)) {
430 			ct->ct_error.re_status = RPC_AUTHERROR;
431 			ct->ct_error.re_why = AUTH_INVALIDRESP;
432 		} else {
433 			if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
434 				reply_stat = (*xdr_results)(xdrs, results_ptr);
435 			} else {
436 				reply_stat = __rpc_gss_unwrap(cl->cl_auth,
437 				    xdrs, xdr_results, results_ptr);
438 			}
439 			if (! reply_stat) {
440 				if (ct->ct_error.re_status == RPC_SUCCESS)
441 					ct->ct_error.re_status =
442 						RPC_CANTDECODERES;
443 			}
444 		}
445 		/* free verifier ... */
446 		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
447 			xdrs->x_op = XDR_FREE;
448 			(void)xdr_opaque_auth(xdrs,
449 			    &(reply_msg.acpted_rply.ar_verf));
450 		}
451 	}  /* end successful completion */
452 	else {
453 		/* maybe our credentials need to be refreshed ... */
454 		if (refreshes-- && AUTH_REFRESH(cl->cl_auth, &reply_msg))
455 			goto call_again;
456 	}  /* end of unsuccessful completion */
457 	release_fd_lock(ct->ct_fd, mask);
458 	return (ct->ct_error.re_status);
459 }
460 
461 static void
462 clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
463 {
464 	struct ct_data *ct;
465 
466 	assert(cl != NULL);
467 	assert(errp != NULL);
468 
469 	ct = (struct ct_data *) cl->cl_private;
470 	*errp = ct->ct_error;
471 }
472 
473 static bool_t
474 clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
475 {
476 	struct ct_data *ct;
477 	XDR *xdrs;
478 	bool_t dummy;
479 	sigset_t mask;
480 	sigset_t newmask;
481 
482 	assert(cl != NULL);
483 
484 	ct = (struct ct_data *)cl->cl_private;
485 	xdrs = &(ct->ct_xdrs);
486 
487 	sigfillset(&newmask);
488 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
489 	mutex_lock(&clnt_fd_lock);
490 	while (vc_fd_locks[ct->ct_fd])
491 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
492 	xdrs->x_op = XDR_FREE;
493 	dummy = (*xdr_res)(xdrs, res_ptr);
494 	mutex_unlock(&clnt_fd_lock);
495 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
496 	cond_signal(&vc_cv[ct->ct_fd]);
497 
498 	return dummy;
499 }
500 
501 /*ARGSUSED*/
502 static void
503 clnt_vc_abort(CLIENT *cl)
504 {
505 }
506 
507 static __inline void
508 htonlp(void *dst, const void *src, uint32_t incr)
509 {
510 	/* We are aligned, so we think */
511 	*(uint32_t *)dst = htonl(*(const uint32_t *)src + incr);
512 }
513 
514 static __inline void
515 ntohlp(void *dst, const void *src)
516 {
517 	/* We are aligned, so we think */
518 	*(uint32_t *)dst = htonl(*(const uint32_t *)src);
519 }
520 
521 static bool_t
522 clnt_vc_control(CLIENT *cl, u_int request, void *info)
523 {
524 	struct ct_data *ct;
525 	void *infop = info;
526 	sigset_t mask;
527 	sigset_t newmask;
528 	int rpc_lock_value;
529 
530 	assert(cl != NULL);
531 
532 	ct = (struct ct_data *)cl->cl_private;
533 
534 	sigfillset(&newmask);
535 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
536 	mutex_lock(&clnt_fd_lock);
537 	while (vc_fd_locks[ct->ct_fd])
538 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
539 	if (__isthreaded)
540                 rpc_lock_value = 1;
541         else
542                 rpc_lock_value = 0;
543 	vc_fd_locks[ct->ct_fd] = rpc_lock_value;
544 	mutex_unlock(&clnt_fd_lock);
545 
546 	switch (request) {
547 	case CLSET_FD_CLOSE:
548 		ct->ct_closeit = TRUE;
549 		release_fd_lock(ct->ct_fd, mask);
550 		return (TRUE);
551 	case CLSET_FD_NCLOSE:
552 		ct->ct_closeit = FALSE;
553 		release_fd_lock(ct->ct_fd, mask);
554 		return (TRUE);
555 	default:
556 		break;
557 	}
558 
559 	/* for other requests which use info */
560 	if (info == NULL) {
561 		release_fd_lock(ct->ct_fd, mask);
562 		return (FALSE);
563 	}
564 	switch (request) {
565 	case CLSET_TIMEOUT:
566 		if (time_not_ok((struct timeval *)info)) {
567 			release_fd_lock(ct->ct_fd, mask);
568 			return (FALSE);
569 		}
570 		ct->ct_wait = *(struct timeval *)infop;
571 		ct->ct_waitset = TRUE;
572 		break;
573 	case CLGET_TIMEOUT:
574 		*(struct timeval *)infop = ct->ct_wait;
575 		break;
576 	case CLGET_SERVER_ADDR:
577 		(void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
578 		break;
579 	case CLGET_FD:
580 		*(int *)info = ct->ct_fd;
581 		break;
582 	case CLGET_SVC_ADDR:
583 		/* The caller should not free this memory area */
584 		*(struct netbuf *)info = ct->ct_addr;
585 		break;
586 	case CLSET_SVC_ADDR:		/* set to new address */
587 		release_fd_lock(ct->ct_fd, mask);
588 		return (FALSE);
589 	case CLGET_XID:
590 		/*
591 		 * use the knowledge that xid is the
592 		 * first element in the call structure
593 		 * This will get the xid of the PREVIOUS call
594 		 */
595 		ntohlp(info, &ct->ct_u.ct_mcalli);
596 		break;
597 	case CLSET_XID:
598 		/* This will set the xid of the NEXT call */
599 		/* increment by 1 as clnt_vc_call() decrements once */
600 		htonlp(&ct->ct_u.ct_mcalli, info, 1);
601 		break;
602 	case CLGET_VERS:
603 		/*
604 		 * This RELIES on the information that, in the call body,
605 		 * the version number field is the fifth field from the
606 		 * beginning of the RPC header. MUST be changed if the
607 		 * call_struct is changed
608 		 */
609 		ntohlp(info, ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT);
610 		break;
611 
612 	case CLSET_VERS:
613 		htonlp(ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT, info, 0);
614 		break;
615 
616 	case CLGET_PROG:
617 		/*
618 		 * This RELIES on the information that, in the call body,
619 		 * the program number field is the fourth field from the
620 		 * beginning of the RPC header. MUST be changed if the
621 		 * call_struct is changed
622 		 */
623 		ntohlp(info, ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT);
624 		break;
625 
626 	case CLSET_PROG:
627 		htonlp(ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT, info, 0);
628 		break;
629 
630 	default:
631 		release_fd_lock(ct->ct_fd, mask);
632 		return (FALSE);
633 	}
634 	release_fd_lock(ct->ct_fd, mask);
635 	return (TRUE);
636 }
637 
638 
639 static void
640 clnt_vc_destroy(CLIENT *cl)
641 {
642 	struct ct_data *ct = (struct ct_data *) cl->cl_private;
643 	int ct_fd = ct->ct_fd;
644 	sigset_t mask;
645 	sigset_t newmask;
646 
647 	assert(cl != NULL);
648 
649 	ct = (struct ct_data *) cl->cl_private;
650 
651 	sigfillset(&newmask);
652 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
653 	mutex_lock(&clnt_fd_lock);
654 	while (vc_fd_locks[ct_fd])
655 		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
656 	if (ct->ct_closeit && ct->ct_fd != -1) {
657 		(void)_close(ct->ct_fd);
658 	}
659 	XDR_DESTROY(&(ct->ct_xdrs));
660 	free(ct->ct_addr.buf);
661 	mem_free(ct, sizeof(struct ct_data));
662 	if (cl->cl_netid && cl->cl_netid[0])
663 		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
664 	if (cl->cl_tp && cl->cl_tp[0])
665 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
666 	mem_free(cl, sizeof(CLIENT));
667 	mutex_unlock(&clnt_fd_lock);
668 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
669 	cond_signal(&vc_cv[ct_fd]);
670 }
671 
672 /*
673  * Interface between xdr serializer and tcp connection.
674  * Behaves like the system calls, read & write, but keeps some error state
675  * around for the rpc level.
676  */
677 static int
678 read_vc(void *ctp, void *buf, int len)
679 {
680 	struct sockaddr sa;
681 	socklen_t sal;
682 	struct ct_data *ct = (struct ct_data *)ctp;
683 	struct pollfd fd;
684 	int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) +
685 	    (ct->ct_wait.tv_usec / 1000));
686 
687 	if (len == 0)
688 		return (0);
689 	fd.fd = ct->ct_fd;
690 	fd.events = POLLIN;
691 	for (;;) {
692 		switch (_poll(&fd, 1, milliseconds)) {
693 		case 0:
694 			ct->ct_error.re_status = RPC_TIMEDOUT;
695 			return (-1);
696 
697 		case -1:
698 			if (errno == EINTR)
699 				continue;
700 			ct->ct_error.re_status = RPC_CANTRECV;
701 			ct->ct_error.re_errno = errno;
702 			return (-1);
703 		}
704 		break;
705 	}
706 
707 	sal = sizeof(sa);
708 	if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
709 	    (sa.sa_family == AF_LOCAL)) {
710 		len = __msgread(ct->ct_fd, buf, (size_t)len);
711 	} else {
712 		len = _read(ct->ct_fd, buf, (size_t)len);
713 	}
714 
715 	switch (len) {
716 	case 0:
717 		/* premature eof */
718 		ct->ct_error.re_errno = ECONNRESET;
719 		ct->ct_error.re_status = RPC_CANTRECV;
720 		len = -1;  /* it's really an error */
721 		break;
722 
723 	case -1:
724 		ct->ct_error.re_errno = errno;
725 		ct->ct_error.re_status = RPC_CANTRECV;
726 		break;
727 	}
728 	return (len);
729 }
730 
731 static int
732 write_vc(void *ctp, void *buf, int len)
733 {
734 	struct sockaddr sa;
735 	socklen_t sal;
736 	struct ct_data *ct = (struct ct_data *)ctp;
737 	int i, cnt;
738 
739 	sal = sizeof(sa);
740 	if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
741 	    (sa.sa_family == AF_LOCAL)) {
742 		for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
743 			if ((i = __msgwrite(ct->ct_fd, buf,
744 			     (size_t)cnt)) == -1) {
745 				ct->ct_error.re_errno = errno;
746 				ct->ct_error.re_status = RPC_CANTSEND;
747 				return (-1);
748 			}
749 		}
750 	} else {
751 		for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
752 			if ((i = _write(ct->ct_fd, buf, (size_t)cnt)) == -1) {
753 				ct->ct_error.re_errno = errno;
754 				ct->ct_error.re_status = RPC_CANTSEND;
755 				return (-1);
756 			}
757 		}
758 	}
759 	return (len);
760 }
761 
762 static struct clnt_ops *
763 clnt_vc_ops(void)
764 {
765 	static struct clnt_ops ops;
766 	sigset_t mask, newmask;
767 
768 	/* VARIABLES PROTECTED BY ops_lock: ops */
769 
770 	sigfillset(&newmask);
771 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
772 	mutex_lock(&ops_lock);
773 	if (ops.cl_call == NULL) {
774 		ops.cl_call = clnt_vc_call;
775 		ops.cl_abort = clnt_vc_abort;
776 		ops.cl_geterr = clnt_vc_geterr;
777 		ops.cl_freeres = clnt_vc_freeres;
778 		ops.cl_destroy = clnt_vc_destroy;
779 		ops.cl_control = clnt_vc_control;
780 	}
781 	mutex_unlock(&ops_lock);
782 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
783 	return (&ops);
784 }
785 
786 /*
787  * Make sure that the time is not garbage.   -1 value is disallowed.
788  * Note this is different from time_not_ok in clnt_dg.c
789  */
790 static bool_t
791 time_not_ok(struct timeval *t)
792 {
793 	return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
794 		t->tv_usec <= -1 || t->tv_usec > 1000000);
795 }
796 
797 static int
798 __msgread(int sock, void *buf, size_t cnt)
799 {
800 	struct iovec iov[1];
801 	struct msghdr msg;
802 	union {
803 		struct cmsghdr cmsg;
804 		char control[CMSG_SPACE(sizeof(struct cmsgcred))];
805 	} cm;
806 
807 	bzero((char *)&cm, sizeof(cm));
808 	iov[0].iov_base = buf;
809 	iov[0].iov_len = cnt;
810 
811 	msg.msg_iov = iov;
812 	msg.msg_iovlen = 1;
813 	msg.msg_name = NULL;
814 	msg.msg_namelen = 0;
815 	msg.msg_control = (caddr_t)&cm;
816 	msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
817 	msg.msg_flags = 0;
818 
819 	return(_recvmsg(sock, &msg, 0));
820 }
821 
822 static int
823 __msgwrite(int sock, void *buf, size_t cnt)
824 {
825 	struct iovec iov[1];
826 	struct msghdr msg;
827 	union {
828 		struct cmsghdr cmsg;
829 		char control[CMSG_SPACE(sizeof(struct cmsgcred))];
830 	} cm;
831 
832 	bzero((char *)&cm, sizeof(cm));
833 	iov[0].iov_base = buf;
834 	iov[0].iov_len = cnt;
835 
836 	cm.cmsg.cmsg_type = SCM_CREDS;
837 	cm.cmsg.cmsg_level = SOL_SOCKET;
838 	cm.cmsg.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
839 
840 	msg.msg_iov = iov;
841 	msg.msg_iovlen = 1;
842 	msg.msg_name = NULL;
843 	msg.msg_namelen = 0;
844 	msg.msg_control = (caddr_t)&cm;
845 	msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
846 	msg.msg_flags = 0;
847 
848 	return(_sendmsg(sock, &msg, 0));
849 }
850