The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/rpc/clnt_vc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 2009, Sun Microsystems, Inc.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without 
    8  * modification, are permitted provided that the following conditions are met:
    9  * - Redistributions of source code must retain the above copyright notice, 
   10  *   this list of conditions and the following disclaimer.
   11  * - Redistributions in binary form must reproduce the above copyright notice, 
   12  *   this list of conditions and the following disclaimer in the documentation 
   13  *   and/or other materials provided with the distribution.
   14  * - Neither the name of Sun Microsystems, Inc. nor the names of its 
   15  *   contributors may be used to endorse or promote products derived 
   16  *   from this software without specific prior written permission.
   17  * 
   18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
   19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
   20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
   21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
   22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
   23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
   24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
   25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
   27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
   28  * POSSIBILITY OF SUCH DAMAGE.
   29  */
   30 
   31 #if defined(LIBC_SCCS) && !defined(lint)
   32 static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
   33 static char *sccsid = "@(#)clnt_tcp.c   2.2 88/08/01 4.0 RPCSRC";
   34 static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
   35 #endif
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD: releng/11.0/sys/rpc/clnt_vc.c 303691 2016-08-03 01:06:51Z ngie $");
   38  
   39 /*
   40  * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
   41  *
   42  * Copyright (C) 1984, Sun Microsystems, Inc.
   43  *
   44  * TCP based RPC supports 'batched calls'.
   45  * A sequence of calls may be batched-up in a send buffer.  The rpc call
   46  * return immediately to the client even though the call was not necessarily
   47  * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
   48  * the rpc timeout value is zero (see clnt.h, rpc).
   49  *
   50  * Clients should NOT casually batch calls that in fact return results; that is,
   51  * the server side should be aware that a call is batched and not produce any
   52  * return message.  Batched calls that produce many result messages can
   53  * deadlock (netlock) the client and the server....
   54  *
   55  * Now go hang yourself.
   56  */
   57 
   58 #include <sys/param.h>
   59 #include <sys/systm.h>
   60 #include <sys/lock.h>
   61 #include <sys/malloc.h>
   62 #include <sys/mbuf.h>
   63 #include <sys/mutex.h>
   64 #include <sys/pcpu.h>
   65 #include <sys/proc.h>
   66 #include <sys/protosw.h>
   67 #include <sys/socket.h>
   68 #include <sys/socketvar.h>
   69 #include <sys/sx.h>
   70 #include <sys/syslog.h>
   71 #include <sys/time.h>
   72 #include <sys/uio.h>
   73 
   74 #include <net/vnet.h>
   75 
   76 #include <netinet/tcp.h>
   77 
   78 #include <rpc/rpc.h>
   79 #include <rpc/rpc_com.h>
   80 #include <rpc/krpc.h>
   81 
   82 struct cmessage {
   83         struct cmsghdr cmsg;
   84         struct cmsgcred cmcred;
   85 };
   86 
   87 static enum clnt_stat clnt_vc_call(CLIENT *, struct rpc_callextra *,
   88     rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
   89 static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
   90 static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *);
   91 static void clnt_vc_abort(CLIENT *);
   92 static bool_t clnt_vc_control(CLIENT *, u_int, void *);
   93 static void clnt_vc_close(CLIENT *);
   94 static void clnt_vc_destroy(CLIENT *);
   95 static bool_t time_not_ok(struct timeval *);
   96 static int clnt_vc_soupcall(struct socket *so, void *arg, int waitflag);
   97 
   98 static struct clnt_ops clnt_vc_ops = {
   99         .cl_call =      clnt_vc_call,
  100         .cl_abort =     clnt_vc_abort,
  101         .cl_geterr =    clnt_vc_geterr,
  102         .cl_freeres =   clnt_vc_freeres,
  103         .cl_close =     clnt_vc_close,
  104         .cl_destroy =   clnt_vc_destroy,
  105         .cl_control =   clnt_vc_control
  106 };
  107 
  108 static void clnt_vc_upcallsdone(struct ct_data *);
  109 
  110 /*
  111  * Create a client handle for a connection.
  112  * Default options are set, which the user can change using clnt_control()'s.
  113  * The rpc/vc package does buffering similar to stdio, so the client
  114  * must pick send and receive buffer sizes, 0 => use the default.
  115  * NB: fd is copied into a private area.
  116  * NB: The rpch->cl_auth is set null authentication. Caller may wish to
  117  * set this something more useful.
  118  *
  119  * fd should be an open socket
  120  */
  121 CLIENT *
  122 clnt_vc_create(
  123         struct socket *so,              /* open file descriptor */
  124         struct sockaddr *raddr,         /* servers address */
  125         const rpcprog_t prog,           /* program number */
  126         const rpcvers_t vers,           /* version number */
  127         size_t sendsz,                  /* buffer recv size */
  128         size_t recvsz,                  /* buffer send size */
  129         int intrflag)                   /* interruptible */
  130 {
  131         CLIENT *cl;                     /* client handle */
  132         struct ct_data *ct = NULL;      /* client handle */
  133         struct timeval now;
  134         struct rpc_msg call_msg;
  135         static uint32_t disrupt;
  136         struct __rpc_sockinfo si;
  137         XDR xdrs;
  138         int error, interrupted, one = 1, sleep_flag;
  139         struct sockopt sopt;
  140 
  141         if (disrupt == 0)
  142                 disrupt = (uint32_t)(long)raddr;
  143 
  144         cl = (CLIENT *)mem_alloc(sizeof (*cl));
  145         ct = (struct ct_data *)mem_alloc(sizeof (*ct));
  146 
  147         mtx_init(&ct->ct_lock, "ct->ct_lock", NULL, MTX_DEF);
  148         ct->ct_threads = 0;
  149         ct->ct_closing = FALSE;
  150         ct->ct_closed = FALSE;
  151         ct->ct_upcallrefs = 0;
  152 
  153         if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
  154                 error = soconnect(so, raddr, curthread);
  155                 SOCK_LOCK(so);
  156                 interrupted = 0;
  157                 sleep_flag = PSOCK;
  158                 if (intrflag != 0)
  159                         sleep_flag |= PCATCH;
  160                 while ((so->so_state & SS_ISCONNECTING)
  161                     && so->so_error == 0) {
  162                         error = msleep(&so->so_timeo, SOCK_MTX(so),
  163                             sleep_flag, "connec", 0);
  164                         if (error) {
  165                                 if (error == EINTR || error == ERESTART)
  166                                         interrupted = 1;
  167                                 break;
  168                         }
  169                 }
  170                 if (error == 0) {
  171                         error = so->so_error;
  172                         so->so_error = 0;
  173                 }
  174                 SOCK_UNLOCK(so);
  175                 if (error) {
  176                         if (!interrupted)
  177                                 so->so_state &= ~SS_ISCONNECTING;
  178                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  179                         rpc_createerr.cf_error.re_errno = error;
  180                         goto err;
  181                 }
  182         }
  183 
  184         if (!__rpc_socket2sockinfo(so, &si)) {
  185                 goto err;
  186         }
  187 
  188         if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
  189                 bzero(&sopt, sizeof(sopt));
  190                 sopt.sopt_dir = SOPT_SET;
  191                 sopt.sopt_level = SOL_SOCKET;
  192                 sopt.sopt_name = SO_KEEPALIVE;
  193                 sopt.sopt_val = &one;
  194                 sopt.sopt_valsize = sizeof(one);
  195                 sosetopt(so, &sopt);
  196         }
  197 
  198         if (so->so_proto->pr_protocol == IPPROTO_TCP) {
  199                 bzero(&sopt, sizeof(sopt));
  200                 sopt.sopt_dir = SOPT_SET;
  201                 sopt.sopt_level = IPPROTO_TCP;
  202                 sopt.sopt_name = TCP_NODELAY;
  203                 sopt.sopt_val = &one;
  204                 sopt.sopt_valsize = sizeof(one);
  205                 sosetopt(so, &sopt);
  206         }
  207 
  208         ct->ct_closeit = FALSE;
  209 
  210         /*
  211          * Set up private data struct
  212          */
  213         ct->ct_socket = so;
  214         ct->ct_wait.tv_sec = -1;
  215         ct->ct_wait.tv_usec = -1;
  216         memcpy(&ct->ct_addr, raddr, raddr->sa_len);
  217 
  218         /*
  219          * Initialize call message
  220          */
  221         getmicrotime(&now);
  222         ct->ct_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now);
  223         call_msg.rm_xid = ct->ct_xid;
  224         call_msg.rm_direction = CALL;
  225         call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  226         call_msg.rm_call.cb_prog = (uint32_t)prog;
  227         call_msg.rm_call.cb_vers = (uint32_t)vers;
  228 
  229         /*
  230          * pre-serialize the static part of the call msg and stash it away
  231          */
  232         xdrmem_create(&xdrs, ct->ct_mcallc, MCALL_MSG_SIZE,
  233             XDR_ENCODE);
  234         if (! xdr_callhdr(&xdrs, &call_msg)) {
  235                 if (ct->ct_closeit) {
  236                         soclose(ct->ct_socket);
  237                 }
  238                 goto err;
  239         }
  240         ct->ct_mpos = XDR_GETPOS(&xdrs);
  241         XDR_DESTROY(&xdrs);
  242         ct->ct_waitchan = "rpcrecv";
  243         ct->ct_waitflag = 0;
  244 
  245         /*
  246          * Create a client handle which uses xdrrec for serialization
  247          * and authnone for authentication.
  248          */
  249         sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
  250         recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
  251         error = soreserve(ct->ct_socket, sendsz, recvsz);
  252         if (error != 0) {
  253                 if (ct->ct_closeit) {
  254                         soclose(ct->ct_socket);
  255                 }
  256                 goto err;
  257         }
  258         cl->cl_refs = 1;
  259         cl->cl_ops = &clnt_vc_ops;
  260         cl->cl_private = ct;
  261         cl->cl_auth = authnone_create();
  262 
  263         SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
  264         soupcall_set(ct->ct_socket, SO_RCV, clnt_vc_soupcall, ct);
  265         SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
  266 
  267         ct->ct_record = NULL;
  268         ct->ct_record_resid = 0;
  269         TAILQ_INIT(&ct->ct_pending);
  270         return (cl);
  271 
  272 err:
  273         mtx_destroy(&ct->ct_lock);
  274         mem_free(ct, sizeof (struct ct_data));
  275         mem_free(cl, sizeof (CLIENT));
  276 
  277         return ((CLIENT *)NULL);
  278 }
  279 
  280 static enum clnt_stat
  281 clnt_vc_call(
  282         CLIENT          *cl,            /* client handle */
  283         struct rpc_callextra *ext,      /* call metadata */
  284         rpcproc_t       proc,           /* procedure number */
  285         struct mbuf     *args,          /* pointer to args */
  286         struct mbuf     **resultsp,     /* pointer to results */
  287         struct timeval  utimeout)
  288 {
  289         struct ct_data *ct = (struct ct_data *) cl->cl_private;
  290         AUTH *auth;
  291         struct rpc_err *errp;
  292         enum clnt_stat stat;
  293         XDR xdrs;
  294         struct rpc_msg reply_msg;
  295         bool_t ok;
  296         int nrefreshes = 2;             /* number of times to refresh cred */
  297         struct timeval timeout;
  298         uint32_t xid;
  299         struct mbuf *mreq = NULL, *results;
  300         struct ct_request *cr;
  301         int error;
  302 
  303         cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
  304 
  305         mtx_lock(&ct->ct_lock);
  306 
  307         if (ct->ct_closing || ct->ct_closed) {
  308                 mtx_unlock(&ct->ct_lock);
  309                 free(cr, M_RPC);
  310                 return (RPC_CANTSEND);
  311         }
  312         ct->ct_threads++;
  313 
  314         if (ext) {
  315                 auth = ext->rc_auth;
  316                 errp = &ext->rc_err;
  317         } else {
  318                 auth = cl->cl_auth;
  319                 errp = &ct->ct_error;
  320         }
  321 
  322         cr->cr_mrep = NULL;
  323         cr->cr_error = 0;
  324 
  325         if (ct->ct_wait.tv_usec == -1) {
  326                 timeout = utimeout;     /* use supplied timeout */
  327         } else {
  328                 timeout = ct->ct_wait;  /* use default timeout */
  329         }
  330 
  331 call_again:
  332         mtx_assert(&ct->ct_lock, MA_OWNED);
  333 
  334         ct->ct_xid++;
  335         xid = ct->ct_xid;
  336 
  337         mtx_unlock(&ct->ct_lock);
  338 
  339         /*
  340          * Leave space to pre-pend the record mark.
  341          */
  342         mreq = m_gethdr(M_WAITOK, MT_DATA);
  343         mreq->m_data += sizeof(uint32_t);
  344         KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN,
  345             ("RPC header too big"));
  346         bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos);
  347         mreq->m_len = ct->ct_mpos;
  348 
  349         /*
  350          * The XID is the first thing in the request.
  351          */
  352         *mtod(mreq, uint32_t *) = htonl(xid);
  353 
  354         xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
  355 
  356         errp->re_status = stat = RPC_SUCCESS;
  357 
  358         if ((! XDR_PUTINT32(&xdrs, &proc)) ||
  359             (! AUTH_MARSHALL(auth, xid, &xdrs,
  360                 m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
  361                 errp->re_status = stat = RPC_CANTENCODEARGS;
  362                 mtx_lock(&ct->ct_lock);
  363                 goto out;
  364         }
  365         mreq->m_pkthdr.len = m_length(mreq, NULL);
  366 
  367         /*
  368          * Prepend a record marker containing the packet length.
  369          */
  370         M_PREPEND(mreq, sizeof(uint32_t), M_WAITOK);
  371         *mtod(mreq, uint32_t *) =
  372                 htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t)));
  373 
  374         cr->cr_xid = xid;
  375         mtx_lock(&ct->ct_lock);
  376         /*
  377          * Check to see if the other end has already started to close down
  378          * the connection. The upcall will have set ct_error.re_status
  379          * to RPC_CANTRECV if this is the case.
  380          * If the other end starts to close down the connection after this
  381          * point, it will be detected later when cr_error is checked,
  382          * since the request is in the ct_pending queue.
  383          */
  384         if (ct->ct_error.re_status == RPC_CANTRECV) {
  385                 if (errp != &ct->ct_error) {
  386                         errp->re_errno = ct->ct_error.re_errno;
  387                         errp->re_status = RPC_CANTRECV;
  388                 }
  389                 stat = RPC_CANTRECV;
  390                 goto out;
  391         }
  392         TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link);
  393         mtx_unlock(&ct->ct_lock);
  394 
  395         /*
  396          * sosend consumes mreq.
  397          */
  398         error = sosend(ct->ct_socket, NULL, NULL, mreq, NULL, 0, curthread);
  399         mreq = NULL;
  400         if (error == EMSGSIZE) {
  401                 SOCKBUF_LOCK(&ct->ct_socket->so_snd);
  402                 sbwait(&ct->ct_socket->so_snd);
  403                 SOCKBUF_UNLOCK(&ct->ct_socket->so_snd);
  404                 AUTH_VALIDATE(auth, xid, NULL, NULL);
  405                 mtx_lock(&ct->ct_lock);
  406                 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
  407                 goto call_again;
  408         }
  409 
  410         reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
  411         reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
  412         reply_msg.acpted_rply.ar_verf.oa_length = 0;
  413         reply_msg.acpted_rply.ar_results.where = NULL;
  414         reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
  415 
  416         mtx_lock(&ct->ct_lock);
  417         if (error) {
  418                 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
  419                 errp->re_errno = error;
  420                 errp->re_status = stat = RPC_CANTSEND;
  421                 goto out;
  422         }
  423 
  424         /*
  425          * Check to see if we got an upcall while waiting for the
  426          * lock. In both these cases, the request has been removed
  427          * from ct->ct_pending.
  428          */
  429         if (cr->cr_error) {
  430                 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
  431                 errp->re_errno = cr->cr_error;
  432                 errp->re_status = stat = RPC_CANTRECV;
  433                 goto out;
  434         }
  435         if (cr->cr_mrep) {
  436                 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
  437                 goto got_reply;
  438         }
  439 
  440         /*
  441          * Hack to provide rpc-based message passing
  442          */
  443         if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
  444                 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
  445                 errp->re_status = stat = RPC_TIMEDOUT;
  446                 goto out;
  447         }
  448 
  449         error = msleep(cr, &ct->ct_lock, ct->ct_waitflag, ct->ct_waitchan,
  450             tvtohz(&timeout));
  451 
  452         TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
  453 
  454         if (error) {
  455                 /*
  456                  * The sleep returned an error so our request is still
  457                  * on the list. Turn the error code into an
  458                  * appropriate client status.
  459                  */
  460                 errp->re_errno = error;
  461                 switch (error) {
  462                 case EINTR:
  463                         stat = RPC_INTR;
  464                         break;
  465                 case EWOULDBLOCK:
  466                         stat = RPC_TIMEDOUT;
  467                         break;
  468                 default:
  469                         stat = RPC_CANTRECV;
  470                 }
  471                 errp->re_status = stat;
  472                 goto out;
  473         } else {
  474                 /*
  475                  * We were woken up by the upcall.  If the
  476                  * upcall had a receive error, report that,
  477                  * otherwise we have a reply.
  478                  */
  479                 if (cr->cr_error) {
  480                         errp->re_errno = cr->cr_error;
  481                         errp->re_status = stat = RPC_CANTRECV;
  482                         goto out;
  483                 }
  484         }
  485 
  486 got_reply:
  487         /*
  488          * Now decode and validate the response. We need to drop the
  489          * lock since xdr_replymsg may end up sleeping in malloc.
  490          */
  491         mtx_unlock(&ct->ct_lock);
  492 
  493         if (ext && ext->rc_feedback)
  494                 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
  495 
  496         xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
  497         ok = xdr_replymsg(&xdrs, &reply_msg);
  498         cr->cr_mrep = NULL;
  499 
  500         if (ok) {
  501                 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
  502                     (reply_msg.acpted_rply.ar_stat == SUCCESS))
  503                         errp->re_status = stat = RPC_SUCCESS;
  504                 else
  505                         stat = _seterr_reply(&reply_msg, errp);
  506 
  507                 if (stat == RPC_SUCCESS) {
  508                         results = xdrmbuf_getall(&xdrs);
  509                         if (!AUTH_VALIDATE(auth, xid,
  510                                 &reply_msg.acpted_rply.ar_verf,
  511                                 &results)) {
  512                                 errp->re_status = stat = RPC_AUTHERROR;
  513                                 errp->re_why = AUTH_INVALIDRESP;
  514                         } else {
  515                                 KASSERT(results,
  516                                     ("auth validated but no result"));
  517                                 *resultsp = results;
  518                         }
  519                 }               /* end successful completion */
  520                 /*
  521                  * If unsuccessful AND error is an authentication error
  522                  * then refresh credentials and try again, else break
  523                  */
  524                 else if (stat == RPC_AUTHERROR)
  525                         /* maybe our credentials need to be refreshed ... */
  526                         if (nrefreshes > 0 &&
  527                             AUTH_REFRESH(auth, &reply_msg)) {
  528                                 nrefreshes--;
  529                                 XDR_DESTROY(&xdrs);
  530                                 mtx_lock(&ct->ct_lock);
  531                                 goto call_again;
  532                         }
  533                 /* end of unsuccessful completion */
  534         }       /* end of valid reply message */
  535         else {
  536                 errp->re_status = stat = RPC_CANTDECODERES;
  537         }
  538         XDR_DESTROY(&xdrs);
  539         mtx_lock(&ct->ct_lock);
  540 out:
  541         mtx_assert(&ct->ct_lock, MA_OWNED);
  542 
  543         KASSERT(stat != RPC_SUCCESS || *resultsp,
  544             ("RPC_SUCCESS without reply"));
  545 
  546         if (mreq)
  547                 m_freem(mreq);
  548         if (cr->cr_mrep)
  549                 m_freem(cr->cr_mrep);
  550 
  551         ct->ct_threads--;
  552         if (ct->ct_closing)
  553                 wakeup(ct);
  554                 
  555         mtx_unlock(&ct->ct_lock);
  556 
  557         if (auth && stat != RPC_SUCCESS)
  558                 AUTH_VALIDATE(auth, xid, NULL, NULL);
  559 
  560         free(cr, M_RPC);
  561 
  562         return (stat);
  563 }
  564 
  565 static void
  566 clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
  567 {
  568         struct ct_data *ct = (struct ct_data *) cl->cl_private;
  569 
  570         *errp = ct->ct_error;
  571 }
  572 
  573 static bool_t
  574 clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
  575 {
  576         XDR xdrs;
  577         bool_t dummy;
  578 
  579         xdrs.x_op = XDR_FREE;
  580         dummy = (*xdr_res)(&xdrs, res_ptr);
  581 
  582         return (dummy);
  583 }
  584 
  585 /*ARGSUSED*/
  586 static void
  587 clnt_vc_abort(CLIENT *cl)
  588 {
  589 }
  590 
  591 static bool_t
  592 clnt_vc_control(CLIENT *cl, u_int request, void *info)
  593 {
  594         struct ct_data *ct = (struct ct_data *)cl->cl_private;
  595         void *infop = info;
  596         SVCXPRT *xprt;
  597 
  598         mtx_lock(&ct->ct_lock);
  599 
  600         switch (request) {
  601         case CLSET_FD_CLOSE:
  602                 ct->ct_closeit = TRUE;
  603                 mtx_unlock(&ct->ct_lock);
  604                 return (TRUE);
  605         case CLSET_FD_NCLOSE:
  606                 ct->ct_closeit = FALSE;
  607                 mtx_unlock(&ct->ct_lock);
  608                 return (TRUE);
  609         default:
  610                 break;
  611         }
  612 
  613         /* for other requests which use info */
  614         if (info == NULL) {
  615                 mtx_unlock(&ct->ct_lock);
  616                 return (FALSE);
  617         }
  618         switch (request) {
  619         case CLSET_TIMEOUT:
  620                 if (time_not_ok((struct timeval *)info)) {
  621                         mtx_unlock(&ct->ct_lock);
  622                         return (FALSE);
  623                 }
  624                 ct->ct_wait = *(struct timeval *)infop;
  625                 break;
  626         case CLGET_TIMEOUT:
  627                 *(struct timeval *)infop = ct->ct_wait;
  628                 break;
  629         case CLGET_SERVER_ADDR:
  630                 (void) memcpy(info, &ct->ct_addr, (size_t)ct->ct_addr.ss_len);
  631                 break;
  632         case CLGET_SVC_ADDR:
  633                 /*
  634                  * Slightly different semantics to userland - we use
  635                  * sockaddr instead of netbuf.
  636                  */
  637                 memcpy(info, &ct->ct_addr, ct->ct_addr.ss_len);
  638                 break;
  639         case CLSET_SVC_ADDR:            /* set to new address */
  640                 mtx_unlock(&ct->ct_lock);
  641                 return (FALSE);
  642         case CLGET_XID:
  643                 *(uint32_t *)info = ct->ct_xid;
  644                 break;
  645         case CLSET_XID:
  646                 /* This will set the xid of the NEXT call */
  647                 /* decrement by 1 as clnt_vc_call() increments once */
  648                 ct->ct_xid = *(uint32_t *)info - 1;
  649                 break;
  650         case CLGET_VERS:
  651                 /*
  652                  * This RELIES on the information that, in the call body,
  653                  * the version number field is the fifth field from the
  654                  * beginning of the RPC header. MUST be changed if the
  655                  * call_struct is changed
  656                  */
  657                 *(uint32_t *)info =
  658                     ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
  659                     4 * BYTES_PER_XDR_UNIT));
  660                 break;
  661 
  662         case CLSET_VERS:
  663                 *(uint32_t *)(void *)(ct->ct_mcallc +
  664                     4 * BYTES_PER_XDR_UNIT) =
  665                     htonl(*(uint32_t *)info);
  666                 break;
  667 
  668         case CLGET_PROG:
  669                 /*
  670                  * This RELIES on the information that, in the call body,
  671                  * the program number field is the fourth field from the
  672                  * beginning of the RPC header. MUST be changed if the
  673                  * call_struct is changed
  674                  */
  675                 *(uint32_t *)info =
  676                     ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
  677                     3 * BYTES_PER_XDR_UNIT));
  678                 break;
  679 
  680         case CLSET_PROG:
  681                 *(uint32_t *)(void *)(ct->ct_mcallc +
  682                     3 * BYTES_PER_XDR_UNIT) =
  683                     htonl(*(uint32_t *)info);
  684                 break;
  685 
  686         case CLSET_WAITCHAN:
  687                 ct->ct_waitchan = (const char *)info;
  688                 break;
  689 
  690         case CLGET_WAITCHAN:
  691                 *(const char **) info = ct->ct_waitchan;
  692                 break;
  693 
  694         case CLSET_INTERRUPTIBLE:
  695                 if (*(int *) info)
  696                         ct->ct_waitflag = PCATCH;
  697                 else
  698                         ct->ct_waitflag = 0;
  699                 break;
  700 
  701         case CLGET_INTERRUPTIBLE:
  702                 if (ct->ct_waitflag)
  703                         *(int *) info = TRUE;
  704                 else
  705                         *(int *) info = FALSE;
  706                 break;
  707 
  708         case CLSET_BACKCHANNEL:
  709                 xprt = (SVCXPRT *)info;
  710                 if (ct->ct_backchannelxprt == NULL) {
  711                         xprt->xp_p2 = ct;
  712                         ct->ct_backchannelxprt = xprt;
  713                 }
  714                 break;
  715 
  716         default:
  717                 mtx_unlock(&ct->ct_lock);
  718                 return (FALSE);
  719         }
  720 
  721         mtx_unlock(&ct->ct_lock);
  722         return (TRUE);
  723 }
  724 
  725 static void
  726 clnt_vc_close(CLIENT *cl)
  727 {
  728         struct ct_data *ct = (struct ct_data *) cl->cl_private;
  729         struct ct_request *cr;
  730 
  731         mtx_lock(&ct->ct_lock);
  732 
  733         if (ct->ct_closed) {
  734                 mtx_unlock(&ct->ct_lock);
  735                 return;
  736         }
  737 
  738         if (ct->ct_closing) {
  739                 while (ct->ct_closing)
  740                         msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
  741                 KASSERT(ct->ct_closed, ("client should be closed"));
  742                 mtx_unlock(&ct->ct_lock);
  743                 return;
  744         }
  745 
  746         if (ct->ct_socket) {
  747                 ct->ct_closing = TRUE;
  748                 mtx_unlock(&ct->ct_lock);
  749 
  750                 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
  751                 soupcall_clear(ct->ct_socket, SO_RCV);
  752                 clnt_vc_upcallsdone(ct);
  753                 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
  754 
  755                 /*
  756                  * Abort any pending requests and wait until everyone
  757                  * has finished with clnt_vc_call.
  758                  */
  759                 mtx_lock(&ct->ct_lock);
  760                 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
  761                         cr->cr_xid = 0;
  762                         cr->cr_error = ESHUTDOWN;
  763                         wakeup(cr);
  764                 }
  765 
  766                 while (ct->ct_threads)
  767                         msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
  768         }
  769 
  770         ct->ct_closing = FALSE;
  771         ct->ct_closed = TRUE;
  772         mtx_unlock(&ct->ct_lock);
  773         wakeup(ct);
  774 }
  775 
  776 static void
  777 clnt_vc_destroy(CLIENT *cl)
  778 {
  779         struct ct_data *ct = (struct ct_data *) cl->cl_private;
  780         struct socket *so = NULL;
  781         SVCXPRT *xprt;
  782 
  783         clnt_vc_close(cl);
  784 
  785         mtx_lock(&ct->ct_lock);
  786         xprt = ct->ct_backchannelxprt;
  787         ct->ct_backchannelxprt = NULL;
  788         if (xprt != NULL) {
  789                 mtx_unlock(&ct->ct_lock);       /* To avoid a LOR. */
  790                 sx_xlock(&xprt->xp_lock);
  791                 mtx_lock(&ct->ct_lock);
  792                 xprt->xp_p2 = NULL;
  793                 xprt_unregister(xprt);
  794         }
  795 
  796         if (ct->ct_socket) {
  797                 if (ct->ct_closeit) {
  798                         so = ct->ct_socket;
  799                 }
  800         }
  801 
  802         mtx_unlock(&ct->ct_lock);
  803         if (xprt != NULL) {
  804                 sx_xunlock(&xprt->xp_lock);
  805                 SVC_RELEASE(xprt);
  806         }
  807 
  808         mtx_destroy(&ct->ct_lock);
  809         if (so) {
  810                 soshutdown(so, SHUT_WR);
  811                 soclose(so);
  812         }
  813         mem_free(ct, sizeof(struct ct_data));
  814         if (cl->cl_netid && cl->cl_netid[0])
  815                 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
  816         if (cl->cl_tp && cl->cl_tp[0])
  817                 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
  818         mem_free(cl, sizeof(CLIENT));
  819 }
  820 
  821 /*
  822  * Make sure that the time is not garbage.   -1 value is disallowed.
  823  * Note this is different from time_not_ok in clnt_dg.c
  824  */
  825 static bool_t
  826 time_not_ok(struct timeval *t)
  827 {
  828         return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
  829                 t->tv_usec <= -1 || t->tv_usec > 1000000);
  830 }
  831 
  832 int
  833 clnt_vc_soupcall(struct socket *so, void *arg, int waitflag)
  834 {
  835         struct ct_data *ct = (struct ct_data *) arg;
  836         struct uio uio;
  837         struct mbuf *m, *m2;
  838         struct ct_request *cr;
  839         int error, rcvflag, foundreq;
  840         uint32_t xid_plus_direction[2], header;
  841         bool_t do_read;
  842         SVCXPRT *xprt;
  843         struct cf_conn *cd;
  844 
  845         CTASSERT(sizeof(xid_plus_direction) == 2 * sizeof(uint32_t));
  846         ct->ct_upcallrefs++;
  847         uio.uio_td = curthread;
  848         do {
  849                 /*
  850                  * If ct_record_resid is zero, we are waiting for a
  851                  * record mark.
  852                  */
  853                 if (ct->ct_record_resid == 0) {
  854 
  855                         /*
  856                          * Make sure there is either a whole record
  857                          * mark in the buffer or there is some other
  858                          * error condition
  859                          */
  860                         do_read = FALSE;
  861                         if (sbavail(&so->so_rcv) >= sizeof(uint32_t)
  862                             || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
  863                             || so->so_error)
  864                                 do_read = TRUE;
  865 
  866                         if (!do_read)
  867                                 break;
  868 
  869                         SOCKBUF_UNLOCK(&so->so_rcv);
  870                         uio.uio_resid = sizeof(uint32_t);
  871                         m = NULL;
  872                         rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
  873                         error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
  874                         SOCKBUF_LOCK(&so->so_rcv);
  875 
  876                         if (error == EWOULDBLOCK)
  877                                 break;
  878                         
  879                         /*
  880                          * If there was an error, wake up all pending
  881                          * requests.
  882                          */
  883                         if (error || uio.uio_resid > 0) {
  884                         wakeup_all:
  885                                 mtx_lock(&ct->ct_lock);
  886                                 if (!error) {
  887                                         /*
  888                                          * We must have got EOF trying
  889                                          * to read from the stream.
  890                                          */
  891                                         error = ECONNRESET;
  892                                 }
  893                                 ct->ct_error.re_status = RPC_CANTRECV;
  894                                 ct->ct_error.re_errno = error;
  895                                 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
  896                                         cr->cr_error = error;
  897                                         wakeup(cr);
  898                                 }
  899                                 mtx_unlock(&ct->ct_lock);
  900                                 break;
  901                         }
  902                         m_copydata(m, 0, sizeof(uint32_t), (char *)&header);
  903                         header = ntohl(header);
  904                         ct->ct_record = NULL;
  905                         ct->ct_record_resid = header & 0x7fffffff;
  906                         ct->ct_record_eor = ((header & 0x80000000) != 0);
  907                         m_freem(m);
  908                 } else {
  909                         /*
  910                          * Wait until the socket has the whole record
  911                          * buffered.
  912                          */
  913                         do_read = FALSE;
  914                         if (sbavail(&so->so_rcv) >= ct->ct_record_resid
  915                             || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
  916                             || so->so_error)
  917                                 do_read = TRUE;
  918 
  919                         if (!do_read)
  920                                 break;
  921 
  922                         /*
  923                          * We have the record mark. Read as much as
  924                          * the socket has buffered up to the end of
  925                          * this record.
  926                          */
  927                         SOCKBUF_UNLOCK(&so->so_rcv);
  928                         uio.uio_resid = ct->ct_record_resid;
  929                         m = NULL;
  930                         rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
  931                         error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
  932                         SOCKBUF_LOCK(&so->so_rcv);
  933 
  934                         if (error == EWOULDBLOCK)
  935                                 break;
  936 
  937                         if (error || uio.uio_resid == ct->ct_record_resid)
  938                                 goto wakeup_all;
  939 
  940                         /*
  941                          * If we have part of the record already,
  942                          * chain this bit onto the end.
  943                          */
  944                         if (ct->ct_record)
  945                                 m_last(ct->ct_record)->m_next = m;
  946                         else
  947                                 ct->ct_record = m;
  948 
  949                         ct->ct_record_resid = uio.uio_resid;
  950 
  951                         /*
  952                          * If we have the entire record, see if we can
  953                          * match it to a request.
  954                          */
  955                         if (ct->ct_record_resid == 0
  956                             && ct->ct_record_eor) {
  957                                 /*
  958                                  * The XID is in the first uint32_t of
  959                                  * the reply and the message direction
  960                                  * is the second one.
  961                                  */
  962                                 if (ct->ct_record->m_len <
  963                                     sizeof(xid_plus_direction) &&
  964                                     m_length(ct->ct_record, NULL) <
  965                                     sizeof(xid_plus_direction)) {
  966                                         m_freem(ct->ct_record);
  967                                         break;
  968                                 }
  969                                 m_copydata(ct->ct_record, 0,
  970                                     sizeof(xid_plus_direction),
  971                                     (char *)xid_plus_direction);
  972                                 xid_plus_direction[0] =
  973                                     ntohl(xid_plus_direction[0]);
  974                                 xid_plus_direction[1] =
  975                                     ntohl(xid_plus_direction[1]);
  976                                 /* Check message direction. */
  977                                 if (xid_plus_direction[1] == CALL) {
  978                                         /* This is a backchannel request. */
  979                                         mtx_lock(&ct->ct_lock);
  980                                         xprt = ct->ct_backchannelxprt;
  981                                         if (xprt == NULL) {
  982                                                 mtx_unlock(&ct->ct_lock);
  983                                                 /* Just throw it away. */
  984                                                 m_freem(ct->ct_record);
  985                                                 ct->ct_record = NULL;
  986                                         } else {
  987                                                 cd = (struct cf_conn *)
  988                                                     xprt->xp_p1;
  989                                                 m2 = cd->mreq;
  990                                                 /*
  991                                                  * The requests are chained
  992                                                  * in the m_nextpkt list.
  993                                                  */
  994                                                 while (m2 != NULL &&
  995                                                     m2->m_nextpkt != NULL)
  996                                                         /* Find end of list. */
  997                                                         m2 = m2->m_nextpkt;
  998                                                 if (m2 != NULL)
  999                                                         m2->m_nextpkt =
 1000                                                             ct->ct_record;
 1001                                                 else
 1002                                                         cd->mreq =
 1003                                                             ct->ct_record;
 1004                                                 ct->ct_record->m_nextpkt =
 1005                                                     NULL;
 1006                                                 ct->ct_record = NULL;
 1007                                                 xprt_active(xprt);
 1008                                                 mtx_unlock(&ct->ct_lock);
 1009                                         }
 1010                                 } else {
 1011                                         mtx_lock(&ct->ct_lock);
 1012                                         foundreq = 0;
 1013                                         TAILQ_FOREACH(cr, &ct->ct_pending,
 1014                                             cr_link) {
 1015                                                 if (cr->cr_xid ==
 1016                                                     xid_plus_direction[0]) {
 1017                                                         /*
 1018                                                          * This one
 1019                                                          * matches. We leave
 1020                                                          * the reply mbuf in
 1021                                                          * cr->cr_mrep. Set
 1022                                                          * the XID to zero so
 1023                                                          * that we will ignore
 1024                                                          * any duplicated
 1025                                                          * replies.
 1026                                                          */
 1027                                                         cr->cr_xid = 0;
 1028                                                         cr->cr_mrep =
 1029                                                             ct->ct_record;
 1030                                                         cr->cr_error = 0;
 1031                                                         foundreq = 1;
 1032                                                         wakeup(cr);
 1033                                                         break;
 1034                                                 }
 1035                                         }
 1036                                         mtx_unlock(&ct->ct_lock);
 1037 
 1038                                         if (!foundreq)
 1039                                                 m_freem(ct->ct_record);
 1040                                         ct->ct_record = NULL;
 1041                                 }
 1042                         }
 1043                 }
 1044         } while (m);
 1045         ct->ct_upcallrefs--;
 1046         if (ct->ct_upcallrefs < 0)
 1047                 panic("rpcvc upcall refcnt");
 1048         if (ct->ct_upcallrefs == 0)
 1049                 wakeup(&ct->ct_upcallrefs);
 1050         return (SU_OK);
 1051 }
 1052 
 1053 /*
 1054  * Wait for all upcalls in progress to complete.
 1055  */
 1056 static void
 1057 clnt_vc_upcallsdone(struct ct_data *ct)
 1058 {
 1059 
 1060         SOCKBUF_LOCK_ASSERT(&ct->ct_socket->so_rcv);
 1061 
 1062         while (ct->ct_upcallrefs > 0)
 1063                 (void) msleep(&ct->ct_upcallrefs,
 1064                     SOCKBUF_MTX(&ct->ct_socket->so_rcv), 0, "rpcvcup", 0);
 1065 }

Cache object: 83514dc41c738be1d10563edf73f7255


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.