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