xref: /titanic_51/usr/src/lib/libnsl/rpc/clnt_dg.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
28 /*
29  * Portions of this source code were derived from Berkeley
30  * 4.3 BSD under license from the Regents of the University of
31  * California.
32  */
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 /*
37  * Implements a connectionless client side RPC.
38  */
39 
40 
41 #include "mt.h"
42 #include "rpc_mt.h"
43 #include <assert.h>
44 #include <rpc/rpc.h>
45 #include <rpc/trace.h>
46 #include <errno.h>
47 #include <sys/poll.h>
48 #include <syslog.h>
49 #include <sys/types.h>
50 #include <sys/kstat.h>
51 #include <sys/time.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <strings.h>
57 
58 
59 extern int __rpc_timeval_to_msec();
60 extern bool_t xdr_opaque_auth();
61 extern bool_t __rpc_gss_unwrap();
62 
63 static struct clnt_ops *clnt_dg_ops();
64 static bool_t time_not_ok();
65 
66 /*
67  *	This machinery implements per-fd locks for MT-safety.  It is not
68  *	sufficient to do per-CLIENT handle locks for MT-safety because a
69  *	user may create more than one CLIENT handle with the same fd behind
70  *	it.
71  *
72  *	The current implementation holds locks across the entire RPC and reply,
73  *	including retransmissions.  Yes, this is silly, and as soon as this
74  *	code is proven to work, this should be the first thing fixed.  One step
75  *	at a time.
76  */
77 
78 /*
79  * FD Lock handle used by various MT sync. routines
80  */
81 static mutex_t dgtbl_lock = DEFAULTMUTEX;
82 static	void	*dgtbl = NULL;
83 
84 static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
85 
86 
87 #define	MCALL_MSG_SIZE 24
88 
89 /*
90  * Private data kept per client handle
91  */
92 struct cu_data {
93 	int			cu_fd;		/* connections fd */
94 	bool_t			cu_closeit;	/* opened by library */
95 	struct netbuf		cu_raddr;	/* remote address */
96 	struct timeval		cu_wait;	/* retransmit interval */
97 	struct timeval		cu_total;	/* total time for the call */
98 	struct rpc_err		cu_error;
99 	struct t_unitdata	*cu_tr_data;
100 	XDR			cu_outxdrs;
101 	char			*cu_outbuf_start;
102 	char			cu_outbuf[MCALL_MSG_SIZE];
103 	uint_t			cu_xdrpos;
104 	uint_t			cu_sendsz;	/* send size */
105 	uint_t			cu_recvsz;	/* recv size */
106 	struct pollfd		pfdp;
107 	char			cu_inbuf[1];
108 };
109 
110 static int _rcv_unitdata_err(struct cu_data *cu);
111 
112 /*
113  * Connection less client creation returns with client handle parameters.
114  * Default options are set, which the user can change using clnt_control().
115  * fd should be open and bound.
116  * NB: The rpch->cl_auth is initialized to null authentication.
117  * 	Caller may wish to set this something more useful.
118  *
119  * sendsz and recvsz are the maximum allowable packet sizes that can be
120  * sent and received. Normally they are the same, but they can be
121  * changed to improve the program efficiency and buffer allocation.
122  * If they are 0, use the transport default.
123  *
124  * If svcaddr is NULL, returns NULL.
125  */
126 CLIENT *
127 clnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz)
128 	int fd;				/* open file descriptor */
129 	struct netbuf *svcaddr;		/* servers address */
130 	rpcprog_t program;		/* program number */
131 	rpcvers_t version;		/* version number */
132 	uint_t sendsz;			/* buffer recv size */
133 	uint_t recvsz;			/* buffer send size */
134 {
135 	CLIENT *cl = NULL;		/* client handle */
136 	struct cu_data *cu = NULL;	/* private data */
137 	struct t_unitdata *tr_data;
138 	struct t_info tinfo;
139 	struct timeval now;
140 	struct rpc_msg call_msg;
141 
142 	trace5(TR_clnt_dg_create, 0, program, version, sendsz, recvsz);
143 
144 	sig_mutex_lock(&dgtbl_lock);
145 	if ((dgtbl == NULL) && ((dgtbl = rpc_fd_init()) == NULL)) {
146 		sig_mutex_unlock(&dgtbl_lock);
147 		goto err1;
148 	}
149 	sig_mutex_unlock(&dgtbl_lock);
150 
151 	if (svcaddr == (struct netbuf *)NULL) {
152 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
153 		trace3(TR_clnt_dg_create, 1, program, version);
154 		return ((CLIENT *)NULL);
155 	}
156 	if (t_getinfo(fd, &tinfo) == -1) {
157 		rpc_createerr.cf_stat = RPC_TLIERROR;
158 		rpc_createerr.cf_error.re_errno = 0;
159 		rpc_createerr.cf_error.re_terrno = t_errno;
160 		trace3(TR_clnt_dg_create, 1, program, version);
161 		return ((CLIENT *)NULL);
162 	}
163 	/*
164 	 * Setup to rcv datagram error, we ignore any errors returned from
165 	 * __rpc_tli_set_options() as SO_DGRAM_ERRIND is only relevant to
166 	 * udp/udp6 transports and this point in the code we only know that
167 	 * we are using a connection less transport.
168 	 */
169 	if (tinfo.servtype == T_CLTS)
170 		(void) __rpc_tli_set_options(fd, SOL_SOCKET, SO_DGRAM_ERRIND,
171 					    1);
172 	/*
173 	 * Find the receive and the send size
174 	 */
175 	sendsz = __rpc_get_t_size((int)sendsz, tinfo.tsdu);
176 	recvsz = __rpc_get_t_size((int)recvsz, tinfo.tsdu);
177 	if ((sendsz == 0) || (recvsz == 0)) {
178 		rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
179 		rpc_createerr.cf_error.re_errno = 0;
180 		rpc_createerr.cf_error.re_terrno = 0;
181 		trace3(TR_clnt_dg_create, 1, program, version);
182 		return ((CLIENT *)NULL);
183 	}
184 
185 	if ((cl = (CLIENT *)mem_alloc(sizeof (CLIENT))) == (CLIENT *)NULL)
186 		goto err1;
187 	/*
188 	 * Should be multiple of 4 for XDR.
189 	 */
190 	sendsz = ((sendsz + 3) / 4) * 4;
191 	recvsz = ((recvsz + 3) / 4) * 4;
192 	cu = (struct cu_data *)mem_alloc(sizeof (*cu) + sendsz + recvsz);
193 	if (cu == (struct cu_data *)NULL)
194 		goto err1;
195 	if ((cu->cu_raddr.buf = mem_alloc(svcaddr->len)) == NULL)
196 		goto err1;
197 	(void) memcpy(cu->cu_raddr.buf, svcaddr->buf, (int)svcaddr->len);
198 	cu->cu_raddr.len = cu->cu_raddr.maxlen = svcaddr->len;
199 	cu->cu_outbuf_start = &cu->cu_inbuf[recvsz];
200 	/* Other values can also be set through clnt_control() */
201 	cu->cu_wait.tv_sec = 15;	/* heuristically chosen */
202 	cu->cu_wait.tv_usec = 0;
203 	cu->cu_total.tv_sec = -1;
204 	cu->cu_total.tv_usec = -1;
205 	cu->cu_sendsz = sendsz;
206 	cu->cu_recvsz = recvsz;
207 	(void) gettimeofday(&now, (struct timezone *)NULL);
208 	call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
209 	call_msg.rm_call.cb_prog = program;
210 	call_msg.rm_call.cb_vers = version;
211 	xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
212 	if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
213 		rpc_createerr.cf_stat = RPC_CANTENCODEARGS;  /* XXX */
214 		rpc_createerr.cf_error.re_errno = 0;
215 		rpc_createerr.cf_error.re_terrno = 0;
216 		goto err2;
217 	}
218 	cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
219 	XDR_DESTROY(&(cu->cu_outxdrs));
220 	xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf_start, sendsz,
221 								XDR_ENCODE);
222 /* LINTED pointer alignment */
223 	tr_data = (struct t_unitdata *)t_alloc(fd,
224 				T_UNITDATA, T_ADDR | T_OPT);
225 	if (tr_data == (struct t_unitdata *)NULL) {
226 		goto err1;
227 	}
228 	tr_data->udata.maxlen = cu->cu_recvsz;
229 	tr_data->udata.buf = cu->cu_inbuf;
230 	cu->cu_tr_data = tr_data;
231 
232 	/*
233 	 * By default, closeit is always FALSE. It is users responsibility
234 	 * to do a t_close on it, else the user may use clnt_control
235 	 * to let clnt_destroy do it for him/her.
236 	 */
237 	cu->cu_closeit = FALSE;
238 	cu->cu_fd = fd;
239 	cl->cl_ops = clnt_dg_ops();
240 	cl->cl_private = (caddr_t)cu;
241 	cl->cl_auth = authnone_create();
242 	cl->cl_tp = (char *)NULL;
243 	cl->cl_netid = (char *)NULL;
244 	cu->pfdp.fd = cu->cu_fd;
245 	cu->pfdp.events = MASKVAL;
246 	trace3(TR_clnt_dg_create, 1, program, version);
247 	return (cl);
248 err1:
249 	(void) syslog(LOG_ERR, mem_err_clnt_dg);
250 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
251 	rpc_createerr.cf_error.re_errno = errno;
252 	rpc_createerr.cf_error.re_terrno = 0;
253 err2:
254 	if (cl) {
255 		mem_free((caddr_t)cl, sizeof (CLIENT));
256 		if (cu) {
257 			mem_free(cu->cu_raddr.buf, cu->cu_raddr.len);
258 			mem_free((caddr_t)cu, sizeof (*cu) + sendsz + recvsz);
259 		}
260 	}
261 	trace3(TR_clnt_dg_create, 1, program, version);
262 	return ((CLIENT *)NULL);
263 }
264 
265 static enum clnt_stat
266 clnt_dg_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
267 	CLIENT	*cl;			/* client handle */
268 	rpcproc_t	proc;		/* procedure number */
269 	xdrproc_t	xargs;		/* xdr routine for args */
270 	caddr_t		argsp;		/* pointer to args */
271 	xdrproc_t	xresults;	/* xdr routine for results */
272 	caddr_t		resultsp;	/* pointer to results */
273 	struct timeval	utimeout;	/* seconds to wait before giving up */
274 {
275 /* LINTED pointer alignment */
276 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
277 	XDR *xdrs;
278 	int outlen;
279 	struct rpc_msg reply_msg;
280 	XDR reply_xdrs;
281 	struct timeval time_waited;
282 	bool_t ok;
283 	int nrefreshes = 2;		/* number of times to refresh cred */
284 	struct timeval timeout;
285 	struct timeval retransmit_time;
286 	struct timeval poll_time;
287 	struct timeval startime, curtime;
288 	struct t_unitdata tu_data;
289 	int res;			/* result of operations */
290 	uint32_t x_id;
291 
292 	trace3(TR_clnt_dg_call, 0, cl, proc);
293 
294 	if (rpc_fd_lock(dgtbl, cu->cu_fd)) {
295 		rpc_callerr.re_status = RPC_FAILED;
296 		rpc_callerr.re_errno = errno;
297 		rpc_fd_unlock(dgtbl, cu->cu_fd);
298 		return (RPC_FAILED);
299 	}
300 
301 	if (cu->cu_total.tv_usec == -1) {
302 		timeout = utimeout;	/* use supplied timeout */
303 	} else {
304 		timeout = cu->cu_total;	/* use default timeout */
305 	}
306 
307 	time_waited.tv_sec = 0;
308 	time_waited.tv_usec = 0;
309 	retransmit_time = cu->cu_wait;
310 
311 	tu_data.addr = cu->cu_raddr;
312 
313 call_again:
314 	xdrs = &(cu->cu_outxdrs);
315 	xdrs->x_op = XDR_ENCODE;
316 	XDR_SETPOS(xdrs, 0);
317 /* LINTED pointer alignment */
318 	/*
319 	 * Due to little endian byte order, it is necessary to convert to host
320 	 * format before incrementing xid.
321 	 */
322 	x_id = ntohl(*(uint32_t *)(cu->cu_outbuf)) + 1;		/* set XID */
323 	*(uint32_t *)cu->cu_outbuf = htonl(x_id);
324 
325 	if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
326 		if ((! XDR_PUTBYTES(xdrs, cu->cu_outbuf, cu->cu_xdrpos)) ||
327 				(! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
328 				(! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
329 				(! xargs(xdrs, argsp))) {
330 			rpc_fd_unlock(dgtbl, cu->cu_fd);
331 			trace2(TR_clnt_dg_call, 1, cl);
332 			return (rpc_callerr.re_status = RPC_CANTENCODEARGS);
333 		}
334 	} else {
335 /* LINTED pointer alignment */
336 		uint32_t *u = (uint32_t *)&cu->cu_outbuf[cu->cu_xdrpos];
337 		IXDR_PUT_U_INT32(u, proc);
338 		if (!__rpc_gss_wrap(cl->cl_auth, cu->cu_outbuf,
339 		    ((char *)u) - cu->cu_outbuf, xdrs, xargs, argsp)) {
340 			rpc_fd_unlock(dgtbl, cu->cu_fd);
341 			trace2(TR_clnt_dg_call, 1, cl);
342 			return (rpc_callerr.re_status = RPC_CANTENCODEARGS);
343 		}
344 	}
345 	outlen = (int)XDR_GETPOS(xdrs);
346 
347 send_again:
348 	tu_data.udata.buf = cu->cu_outbuf_start;
349 	tu_data.udata.len = outlen;
350 	tu_data.opt.len = 0;
351 	if (t_sndudata(cu->cu_fd, &tu_data) == -1) {
352 		rpc_callerr.re_terrno = t_errno;
353 		rpc_callerr.re_errno = errno;
354 		rpc_fd_unlock(dgtbl, cu->cu_fd);
355 		trace2(TR_clnt_dg_call, 1, cl);
356 		return (rpc_callerr.re_status = RPC_CANTSEND);
357 	}
358 
359 	/*
360 	 * Hack to provide rpc-based message passing
361 	 */
362 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
363 		rpc_fd_unlock(dgtbl, cu->cu_fd);
364 		trace2(TR_clnt_dg_call, 1, cl);
365 		return (rpc_callerr.re_status = RPC_TIMEDOUT);
366 	}
367 	/*
368 	 * sub-optimal code appears here because we have
369 	 * some clock time to spare while the packets are in flight.
370 	 * (We assume that this is actually only executed once.)
371 	 */
372 	reply_msg.acpted_rply.ar_verf = _null_auth;
373 	reply_msg.acpted_rply.ar_results.where = NULL;
374 	reply_msg.acpted_rply.ar_results.proc = xdr_void;
375 
376 	/*
377 	 * Set polling time so that we don't wait for
378 	 * longer than specified by the total time to wait,
379 	 * or the retransmit time.
380 	 */
381 	poll_time.tv_sec = timeout.tv_sec - time_waited.tv_sec;
382 	poll_time.tv_usec = timeout.tv_usec - time_waited.tv_usec;
383 	while (poll_time.tv_usec < 0) {
384 		poll_time.tv_usec += 1000000;
385 		poll_time.tv_sec--;
386 	}
387 
388 	if (poll_time.tv_sec < 0 || (poll_time.tv_sec == 0 &&
389 					poll_time.tv_usec == 0)) {
390 		/*
391 		 * this could happen if time_waited >= timeout
392 		 */
393 		rpc_fd_unlock(dgtbl, cu->cu_fd);
394 		trace2(TR_clnt_dg_call, 1, cl);
395 		return (rpc_callerr.re_status = RPC_TIMEDOUT);
396 	}
397 
398 	if (poll_time.tv_sec > retransmit_time.tv_sec ||
399 			(poll_time.tv_sec == retransmit_time.tv_sec &&
400 				poll_time.tv_usec > retransmit_time.tv_usec))
401 		poll_time = retransmit_time;
402 
403 
404 	for (;;) {
405 
406 		(void) gettimeofday(&startime, NULL);
407 
408 		switch (poll(&cu->pfdp, 1,
409 				__rpc_timeval_to_msec(&poll_time))) {
410 		case -1:
411 			if (errno != EINTR && errno != EAGAIN) {
412 				rpc_callerr.re_errno = errno;
413 				rpc_callerr.re_terrno = 0;
414 				rpc_fd_unlock(dgtbl, cu->cu_fd);
415 				trace2(TR_clnt_dg_call, 1, cl);
416 				return (rpc_callerr.re_status = RPC_CANTRECV);
417 			}
418 			/*FALLTHROUGH*/
419 
420 		case 0:
421 			/*
422 			 * update time waited
423 			 */
424 timeout:			(void) gettimeofday(&curtime, NULL);
425 			time_waited.tv_sec += curtime.tv_sec - startime.tv_sec;
426 			time_waited.tv_usec += curtime.tv_usec -
427 							startime.tv_usec;
428 			while (time_waited.tv_usec >= 1000000) {
429 				time_waited.tv_usec -= 1000000;
430 				time_waited.tv_sec++;
431 			}
432 			while (time_waited.tv_usec < 0) {
433 				time_waited.tv_usec += 1000000;
434 				time_waited.tv_sec--;
435 			}
436 
437 			/*
438 			 * decrement time left to poll by same amount
439 			 */
440 			poll_time.tv_sec -= curtime.tv_sec - startime.tv_sec;
441 			poll_time.tv_usec -= curtime.tv_usec - startime.tv_usec;
442 			while (poll_time.tv_usec >= 1000000) {
443 				poll_time.tv_usec -= 1000000;
444 				poll_time.tv_sec++;
445 			}
446 			while (poll_time.tv_usec < 0) {
447 				poll_time.tv_usec += 1000000;
448 				poll_time.tv_sec--;
449 			}
450 
451 			/*
452 			 * if there's time left to poll, poll again
453 			 */
454 			if (poll_time.tv_sec > 0 ||
455 					(poll_time.tv_sec == 0 &&
456 						poll_time.tv_usec > 0))
457 				continue;
458 
459 			/*
460 			 * if there's more time left, retransmit;
461 			 * otherwise, return timeout error
462 			 */
463 			if (time_waited.tv_sec < timeout.tv_sec ||
464 				(time_waited.tv_sec == timeout.tv_sec &&
465 				    time_waited.tv_usec < timeout.tv_usec)) {
466 				/*
467 				 * update retransmit_time
468 				 */
469 				retransmit_time.tv_usec *= 2;
470 				retransmit_time.tv_sec *= 2;
471 				while (retransmit_time.tv_usec >= 1000000) {
472 					retransmit_time.tv_usec -= 1000000;
473 					retransmit_time.tv_sec++;
474 				}
475 				if (retransmit_time.tv_sec >= RPC_MAX_BACKOFF) {
476 					retransmit_time.tv_sec =
477 							RPC_MAX_BACKOFF;
478 					retransmit_time.tv_usec = 0;
479 				}
480 				/*
481 				 * redo AUTH_MARSHAL if AUTH_DES or RPCSEC_GSS.
482 				 */
483 				if (cl->cl_auth->ah_cred.oa_flavor ==
484 					AUTH_DES ||
485 					cl->cl_auth->ah_cred.oa_flavor ==
486 					RPCSEC_GSS)
487 					goto call_again;
488 				else
489 					goto send_again;
490 			}
491 			rpc_fd_unlock(dgtbl, cu->cu_fd);
492 			trace2(TR_clnt_dg_call, 1, cl);
493 			return (rpc_callerr.re_status = RPC_TIMEDOUT);
494 
495 		default:
496 			break;
497 		}
498 
499 		if (cu->pfdp.revents & POLLNVAL || (cu->pfdp.revents == 0)) {
500 			rpc_callerr.re_status = RPC_CANTRECV;
501 			/*
502 			 *	Note:  we're faking errno here because we
503 			 *	previously would have expected select() to
504 			 *	return -1 with errno EBADF.  Poll(BA_OS)
505 			 *	returns 0 and sets the POLLNVAL revents flag
506 			 *	instead.
507 			 */
508 			rpc_callerr.re_errno = errno = EBADF;
509 			rpc_fd_unlock(dgtbl, cu->cu_fd);
510 			trace2(TR_clnt_dg_call, 1, cl);
511 			return (-1);
512 		}
513 
514 		/* We have some data now */
515 		do {
516 			int moreflag;		/* flag indicating more data */
517 
518 			moreflag = 0;
519 
520 			res = t_rcvudata(cu->cu_fd, cu->cu_tr_data, &moreflag);
521 
522 			if (moreflag & T_MORE) {
523 				/*
524 				 * Drop this packet. I aint got any
525 				 * more space.
526 				 */
527 				res = -1;
528 				/* I should not really be doing this */
529 				errno = 0;
530 				/*
531 				 * XXX: Not really Buffer overflow in the
532 				 * sense of TLI.
533 				 */
534 				t_errno = TBUFOVFLW;
535 			}
536 		} while (res < 0 && (t_errno == TSYSERR && errno == EINTR));
537 		if (res < 0) {
538 			int err, errnoflag = FALSE;
539 #ifdef sun
540 			if (t_errno == TSYSERR && errno == EWOULDBLOCK)
541 #else
542 			if (t_errno == TSYSERR && errno == EAGAIN)
543 #endif
544 				continue;
545 			if (t_errno == TLOOK) {
546 				if ((err = _rcv_unitdata_err(cu)) == 0)
547 					continue;
548 				else if (err == 1)
549 					errnoflag = TRUE;
550 			} else {
551 				rpc_callerr.re_terrno = t_errno;
552 			}
553 			if (errnoflag == FALSE)
554 				rpc_callerr.re_errno = errno;
555 			rpc_fd_unlock(dgtbl, cu->cu_fd);
556 			trace2(TR_clnt_dg_call, 1, cl);
557 			return (rpc_callerr.re_status = RPC_CANTRECV);
558 		}
559 		if (cu->cu_tr_data->udata.len < (uint_t)sizeof (uint32_t))
560 			continue;
561 		/* see if reply transaction id matches sent id */
562 /* LINTED pointer alignment */
563 		if (*((uint32_t *)(cu->cu_inbuf)) !=
564 				*((uint32_t *)(cu->cu_outbuf)))
565 			goto timeout;
566 		/* we now assume we have the proper reply */
567 		break;
568 	}
569 
570 	/*
571 	 * now decode and validate the response
572 	 */
573 
574 	xdrmem_create(&reply_xdrs, cu->cu_inbuf,
575 		(uint_t)cu->cu_tr_data->udata.len, XDR_DECODE);
576 	ok = xdr_replymsg(&reply_xdrs, &reply_msg);
577 	/* XDR_DESTROY(&reply_xdrs);	save a few cycles on noop destroy */
578 	if (ok) {
579 		if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
580 			(reply_msg.acpted_rply.ar_stat == SUCCESS))
581 			rpc_callerr.re_status = RPC_SUCCESS;
582 		else
583 			__seterr_reply(&reply_msg, &(rpc_callerr));
584 
585 		if (rpc_callerr.re_status == RPC_SUCCESS) {
586 			if (! AUTH_VALIDATE(cl->cl_auth,
587 					    &reply_msg.acpted_rply.ar_verf)) {
588 				rpc_callerr.re_status = RPC_AUTHERROR;
589 				rpc_callerr.re_why = AUTH_INVALIDRESP;
590 			} else if (cl->cl_auth->ah_cred.oa_flavor !=
591 								RPCSEC_GSS) {
592 				if (!(*xresults)(&reply_xdrs, resultsp)) {
593 				    if (rpc_callerr.re_status == RPC_SUCCESS)
594 					rpc_callerr.re_status =
595 							RPC_CANTDECODERES;
596 				}
597 			} else if (!__rpc_gss_unwrap(cl->cl_auth, &reply_xdrs,
598 						xresults, resultsp)) {
599 				if (rpc_callerr.re_status == RPC_SUCCESS)
600 				    rpc_callerr.re_status = RPC_CANTDECODERES;
601 			}
602 		}		/* end successful completion */
603 		/*
604 		 * If unsuccesful AND error is an authentication error
605 		 * then refresh credentials and try again, else break
606 		 */
607 		else if (rpc_callerr.re_status == RPC_AUTHERROR)
608 			/* maybe our credentials need to be refreshed ... */
609 			if (nrefreshes-- &&
610 			    AUTH_REFRESH(cl->cl_auth, &reply_msg))
611 				goto call_again;
612 			else
613 				/*
614 				 * We are setting rpc_callerr here given that
615 				 * libnsl is not reentrant thereby
616 				 * reinitializing the TSD.  If not set here then
617 				 * success could be returned even though refresh
618 				 * failed.
619 				 */
620 				rpc_callerr.re_status = RPC_AUTHERROR;
621 
622 		/* end of unsuccessful completion */
623 		/* free verifier */
624 		if (reply_msg.rm_reply.rp_stat == MSG_ACCEPTED &&
625 				reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
626 			xdrs->x_op = XDR_FREE;
627 			(void) xdr_opaque_auth(xdrs,
628 					&(reply_msg.acpted_rply.ar_verf));
629 		}
630 	}	/* end of valid reply message */
631 	else {
632 		rpc_callerr.re_status = RPC_CANTDECODERES;
633 
634 	}
635 	rpc_fd_unlock(dgtbl, cu->cu_fd);
636 	trace2(TR_clnt_dg_call, 1, cl);
637 	return (rpc_callerr.re_status);
638 }
639 
640 static enum clnt_stat
641 clnt_dg_send(cl, proc, xargs, argsp)
642 	CLIENT		*cl;		/* client handle */
643 	rpcproc_t	proc;		/* procedure number */
644 	xdrproc_t	xargs;		/* xdr routine for args */
645 	caddr_t		argsp;		/* pointer to args */
646 {
647 /* LINTED pointer alignment */
648 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
649 	XDR *xdrs;
650 	int outlen;
651 	struct t_unitdata tu_data;
652 	uint32_t x_id;
653 
654 	trace3(TR_clnt_dg_send, 0, cl, proc);
655 
656 	if (rpc_fd_lock(dgtbl, cu->cu_fd)) {
657 		rpc_callerr.re_status = RPC_FAILED;
658 		rpc_callerr.re_errno = errno;
659 		rpc_fd_unlock(dgtbl, cu->cu_fd);
660 		return (RPC_FAILED);
661 	}
662 
663 	tu_data.addr = cu->cu_raddr;
664 
665 	xdrs = &(cu->cu_outxdrs);
666 	xdrs->x_op = XDR_ENCODE;
667 	XDR_SETPOS(xdrs, 0);
668 /* LINTED pointer alignment */
669 	/*
670 	 * Due to little endian byte order, it is necessary to convert to host
671 	 * format before incrementing xid.
672 	 */
673 	x_id = ntohl(*(uint32_t *)(cu->cu_outbuf)) + 1;		/* set XID */
674 	*(uint32_t *)cu->cu_outbuf = htonl(x_id);
675 
676 	if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
677 		if ((! XDR_PUTBYTES(xdrs, cu->cu_outbuf, cu->cu_xdrpos)) ||
678 				(! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
679 				(! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
680 				(! xargs(xdrs, argsp))) {
681 			rpc_fd_unlock(dgtbl, cu->cu_fd);
682 			trace2(TR_clnt_dg_send, 1, cl);
683 			return (rpc_callerr.re_status = RPC_CANTENCODEARGS);
684 		}
685 	} else {
686 /* LINTED pointer alignment */
687 		uint32_t *u = (uint32_t *)&cu->cu_outbuf[cu->cu_xdrpos];
688 		IXDR_PUT_U_INT32(u, proc);
689 		if (!__rpc_gss_wrap(cl->cl_auth, cu->cu_outbuf,
690 		    ((char *)u) - cu->cu_outbuf, xdrs, xargs, argsp)) {
691 			rpc_fd_unlock(dgtbl, cu->cu_fd);
692 			trace2(TR_clnt_dg_send, 1, cl);
693 			return (rpc_callerr.re_status = RPC_CANTENCODEARGS);
694 		}
695 	}
696 	outlen = (int)XDR_GETPOS(xdrs);
697 
698 	tu_data.udata.buf = cu->cu_outbuf_start;
699 	tu_data.udata.len = outlen;
700 	tu_data.opt.len = 0;
701 	if (t_sndudata(cu->cu_fd, &tu_data) == -1) {
702 		rpc_callerr.re_terrno = t_errno;
703 		rpc_callerr.re_errno = errno;
704 		rpc_fd_unlock(dgtbl, cu->cu_fd);
705 		trace2(TR_clnt_dg_send, 1, cl);
706 		return (rpc_callerr.re_status = RPC_CANTSEND);
707 	}
708 
709 	rpc_fd_unlock(dgtbl, cu->cu_fd);
710 	return (rpc_callerr.re_status = RPC_SUCCESS);
711 }
712 
713 static void
714 clnt_dg_geterr(CLIENT *cl, struct rpc_err *errp)
715 {
716 /* LINTED pointer alignment */
717 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
718 
719 	trace2(TR_clnt_dg_geterr, 0, cl);
720 	*errp = rpc_callerr;
721 	trace2(TR_clnt_dg_geterr, 1, cl);
722 }
723 
724 static bool_t
725 clnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
726 {
727 /* LINTED pointer alignment */
728 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
729 	XDR *xdrs = &(cu->cu_outxdrs);
730 	bool_t dummy;
731 
732 	trace2(TR_clnt_dg_freeres, 0, cl);
733 	rpc_fd_lock(dgtbl, cu->cu_fd);
734 	xdrs->x_op = XDR_FREE;
735 	dummy = (*xdr_res)(xdrs, res_ptr);
736 	rpc_fd_unlock(dgtbl, cu->cu_fd);
737 	trace2(TR_clnt_dg_freeres, 1, cl);
738 	return (dummy);
739 }
740 
741 static void
742 clnt_dg_abort(/* h */)
743 	/* CLIENT *h; */
744 {
745 	trace1(TR_clnt_dg_abort, 0);
746 	trace1(TR_clnt_dg_abort, 1);
747 }
748 
749 static bool_t
750 clnt_dg_control(CLIENT *cl, int request, char *info)
751 {
752 /* LINTED pointer alignment */
753 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
754 	struct netbuf *addr;
755 	trace3(TR_clnt_dg_control, 0, cl, request);
756 
757 	if (rpc_fd_lock(dgtbl, cu->cu_fd)) {
758 		rpc_fd_unlock(dgtbl, cu->cu_fd);
759 		return (RPC_FAILED);
760 	}
761 
762 	switch (request) {
763 	case CLSET_FD_CLOSE:
764 		cu->cu_closeit = TRUE;
765 		rpc_fd_unlock(dgtbl, cu->cu_fd);
766 		trace2(TR_clnt_dg_control, 1, cl);
767 		return (TRUE);
768 	case CLSET_FD_NCLOSE:
769 		cu->cu_closeit = FALSE;
770 		rpc_fd_unlock(dgtbl, cu->cu_fd);
771 		trace2(TR_clnt_dg_control, 1, cl);
772 		return (TRUE);
773 	}
774 
775 	/* for other requests which use info */
776 	if (info == NULL) {
777 		rpc_fd_unlock(dgtbl, cu->cu_fd);
778 		trace2(TR_clnt_dg_control, 1, cl);
779 		return (FALSE);
780 	}
781 	switch (request) {
782 	case CLSET_TIMEOUT:
783 /* LINTED pointer alignment */
784 		if (time_not_ok((struct timeval *)info)) {
785 			rpc_fd_unlock(dgtbl, cu->cu_fd);
786 			trace2(TR_clnt_dg_control, 1, cl);
787 			return (FALSE);
788 		}
789 /* LINTED pointer alignment */
790 		cu->cu_total = *(struct timeval *)info;
791 		break;
792 	case CLGET_TIMEOUT:
793 /* LINTED pointer alignment */
794 		*(struct timeval *)info = cu->cu_total;
795 		break;
796 	case CLGET_SERVER_ADDR:		/* Give him the fd address */
797 		/* Now obsolete. Only for backword compatibility */
798 		(void) memcpy(info, cu->cu_raddr.buf, (int)cu->cu_raddr.len);
799 		break;
800 	case CLSET_RETRY_TIMEOUT:
801 /* LINTED pointer alignment */
802 		if (time_not_ok((struct timeval *)info)) {
803 			rpc_fd_unlock(dgtbl, cu->cu_fd);
804 			trace2(TR_clnt_dg_control, 1, cl);
805 			return (FALSE);
806 		}
807 /* LINTED pointer alignment */
808 		cu->cu_wait = *(struct timeval *)info;
809 		break;
810 	case CLGET_RETRY_TIMEOUT:
811 /* LINTED pointer alignment */
812 		*(struct timeval *)info = cu->cu_wait;
813 		break;
814 	case CLGET_FD:
815 /* LINTED pointer alignment */
816 		*(int *)info = cu->cu_fd;
817 		break;
818 	case CLGET_SVC_ADDR:
819 /* LINTED pointer alignment */
820 		*(struct netbuf *)info = cu->cu_raddr;
821 		break;
822 	case CLSET_SVC_ADDR:		/* set to new address */
823 /* LINTED pointer alignment */
824 		addr = (struct netbuf *)info;
825 		if (cu->cu_raddr.maxlen < addr->len) {
826 			mem_free(cu->cu_raddr.buf, cu->cu_raddr.maxlen);
827 			if ((cu->cu_raddr.buf = mem_alloc(addr->len)) == NULL) {
828 				rpc_fd_unlock(dgtbl, cu->cu_fd);
829 				trace2(TR_clnt_dg_control, 1, cl);
830 				return (FALSE);
831 			}
832 			cu->cu_raddr.maxlen = addr->len;
833 		}
834 		cu->cu_raddr.len = addr->len;
835 		(void) memcpy(cu->cu_raddr.buf, addr->buf, addr->len);
836 		break;
837 	case CLGET_XID:
838 		/*
839 		 * use the knowledge that xid is the
840 		 * first element in the call structure *.
841 		 * This will get the xid of the PREVIOUS call
842 		 */
843 /* LINTED pointer alignment */
844 		*(uint32_t *)info = ntohl(*(uint32_t *)cu->cu_outbuf);
845 		break;
846 
847 	case CLSET_XID:
848 		/* This will set the xid of the NEXT call */
849 /* LINTED pointer alignment */
850 		*(uint32_t *)cu->cu_outbuf =  htonl(*(uint32_t *)info - 1);
851 		/* decrement by 1 as clnt_dg_call() increments once */
852 		break;
853 
854 	case CLGET_VERS:
855 		/*
856 		 * This RELIES on the information that, in the call body,
857 		 * the version number field is the fifth field from the
858 		 * begining of the RPC header. MUST be changed if the
859 		 * call_struct is changed
860 		 */
861 /* LINTED pointer alignment */
862 		*(uint32_t *)info = ntohl(*(uint32_t *)(cu->cu_outbuf +
863 						    4 * BYTES_PER_XDR_UNIT));
864 		break;
865 
866 	case CLSET_VERS:
867 /* LINTED pointer alignment */
868 		*(uint32_t *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT) =
869 /* LINTED pointer alignment */
870 			htonl(*(uint32_t *)info);
871 		break;
872 
873 	case CLGET_PROG:
874 		/*
875 		 * This RELIES on the information that, in the call body,
876 		 * the program number field is the fourth field from the
877 		 * begining of the RPC header. MUST be changed if the
878 		 * call_struct is changed
879 		 */
880 /* LINTED pointer alignment */
881 		*(uint32_t *)info = ntohl(*(uint32_t *)(cu->cu_outbuf +
882 						    3 * BYTES_PER_XDR_UNIT));
883 		break;
884 
885 	case CLSET_PROG:
886 /* LINTED pointer alignment */
887 		*(uint32_t *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT) =
888 /* LINTED pointer alignment */
889 			htonl(*(uint32_t *)info);
890 		break;
891 
892 	default:
893 		rpc_fd_unlock(dgtbl, cu->cu_fd);
894 		trace2(TR_clnt_dg_control, 1, cl);
895 		return (FALSE);
896 	}
897 	rpc_fd_unlock(dgtbl, cu->cu_fd);
898 	trace2(TR_clnt_dg_control, 1, cl);
899 	return (TRUE);
900 }
901 
902 static void
903 clnt_dg_destroy(CLIENT *cl)
904 {
905 /* LINTED pointer alignment */
906 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
907 	int cu_fd = cu->cu_fd;
908 
909 	trace2(TR_clnt_dg_destroy, 0, cl);
910 	rpc_fd_lock(dgtbl, cu_fd);
911 	if (cu->cu_closeit)
912 		(void) t_close(cu_fd);
913 	XDR_DESTROY(&(cu->cu_outxdrs));
914 	cu->cu_tr_data->udata.buf = NULL;
915 	(void) t_free((char *)cu->cu_tr_data, T_UNITDATA);
916 	mem_free(cu->cu_raddr.buf, cu->cu_raddr.len);
917 	mem_free((caddr_t)cu,
918 		(sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
919 	if (cl->cl_netid && cl->cl_netid[0])
920 		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
921 	if (cl->cl_tp && cl->cl_tp[0])
922 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
923 	mem_free((caddr_t)cl, sizeof (CLIENT));
924 	rpc_fd_unlock(dgtbl, cu_fd);
925 	trace2(TR_clnt_dg_destroy, 1, cl);
926 }
927 
928 static struct clnt_ops *
929 clnt_dg_ops(void)
930 {
931 	static struct clnt_ops ops;
932 	extern mutex_t	ops_lock;
933 
934 /* VARIABLES PROTECTED BY ops_lock: ops */
935 
936 	trace1(TR_clnt_dg_ops, 0);
937 	sig_mutex_lock(&ops_lock);
938 	if (ops.cl_call == NULL) {
939 		ops.cl_call = clnt_dg_call;
940 		ops.cl_send = clnt_dg_send;
941 		ops.cl_abort = clnt_dg_abort;
942 		ops.cl_geterr = clnt_dg_geterr;
943 		ops.cl_freeres = clnt_dg_freeres;
944 		ops.cl_destroy = clnt_dg_destroy;
945 		ops.cl_control = clnt_dg_control;
946 	}
947 	sig_mutex_unlock(&ops_lock);
948 	trace1(TR_clnt_dg_ops, 1);
949 	return (&ops);
950 }
951 
952 /*
953  * Make sure that the time is not garbage.  -1 value is allowed.
954  */
955 static bool_t
956 time_not_ok(struct timeval *t)
957 {
958 	trace1(TR_time_not_ok, 0);
959 	trace1(TR_time_not_ok, 1);
960 	return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
961 		t->tv_usec < -1 || t->tv_usec > 1000000);
962 }
963 
964 /*
965  * Receive a unit data error indication.
966  * Below even when t_alloc() fails we pass uderr=NULL to t_rcvuderr()
967  * so as to just clear the error indication.
968  */
969 
970 static int
971 _rcv_unitdata_err(struct cu_data *cu)
972 {
973 	int old;
974 	struct t_uderr *uderr;
975 
976 	old = t_errno;
977 	uderr = (struct t_uderr *)
978 		t_alloc(cu->cu_fd, T_UDERROR, T_ADDR);
979 
980 	if (t_rcvuderr(cu->cu_fd, uderr) == 0) {
981 		if (uderr == NULL)
982 			return (0);
983 
984 		if (uderr->addr.len != cu->cu_raddr.len ||
985 		    (memcmp(uderr->addr.buf, cu->cu_raddr.buf,
986 		    cu->cu_raddr.len))) {
987 			t_free((char *)uderr, T_UDERROR);
988 			return (0);
989 		}
990 		rpc_callerr.re_errno = uderr->error;
991 		rpc_callerr.re_terrno = TSYSERR;
992 		t_free((char *)uderr, T_UDERROR);
993 		return (1);
994 	} else {
995 		rpc_callerr.re_terrno = old;
996 		if (uderr)
997 			t_free((char *)uderr, T_UDERROR);
998 		return (-1);
999 	}
1000 }
1001