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