]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/rpc/clnt_vc.c
MFC r258578, r258580, r258581 (by hrs):
[FreeBSD/stable/9.git] / sys / rpc / clnt_vc.c
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$");
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/syslog.h>
70 #include <sys/time.h>
71 #include <sys/uio.h>
72
73 #include <net/vnet.h>
74
75 #include <netinet/tcp.h>
76
77 #include <rpc/rpc.h>
78 #include <rpc/rpc_com.h>
79
80 #define MCALL_MSG_SIZE 24
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 /*
109  * A pending RPC request which awaits a reply. Requests which have
110  * received their reply will have cr_xid set to zero and cr_mrep to
111  * the mbuf chain of the reply.
112  */
113 struct ct_request {
114         TAILQ_ENTRY(ct_request) cr_link;
115         uint32_t                cr_xid;         /* XID of request */
116         struct mbuf             *cr_mrep;       /* reply received by upcall */
117         int                     cr_error;       /* any error from upcall */
118         char                    cr_verf[MAX_AUTH_BYTES]; /* reply verf */
119 };
120
121 TAILQ_HEAD(ct_request_list, ct_request);
122
123 struct ct_data {
124         struct mtx      ct_lock;
125         int             ct_threads;     /* number of threads in clnt_vc_call */
126         bool_t          ct_closing;     /* TRUE if we are closing */
127         bool_t          ct_closed;      /* TRUE if we are closed */
128         struct socket   *ct_socket;     /* connection socket */
129         bool_t          ct_closeit;     /* close it on destroy */
130         struct timeval  ct_wait;        /* wait interval in milliseconds */
131         struct sockaddr_storage ct_addr; /* remote addr */
132         struct rpc_err  ct_error;
133         uint32_t        ct_xid;
134         char            ct_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */
135         size_t          ct_mpos;        /* pos after marshal */
136         const char      *ct_waitchan;
137         int             ct_waitflag;
138         struct mbuf     *ct_record;     /* current reply record */
139         size_t          ct_record_resid; /* how much left of reply to read */
140         bool_t          ct_record_eor;   /* true if reading last fragment */
141         struct ct_request_list ct_pending;
142         int             ct_upcallrefs;  /* Ref cnt of upcalls in prog. */
143 };
144
145 static void clnt_vc_upcallsdone(struct ct_data *);
146
147 /*
148  * Create a client handle for a connection.
149  * Default options are set, which the user can change using clnt_control()'s.
150  * The rpc/vc package does buffering similar to stdio, so the client
151  * must pick send and receive buffer sizes, 0 => use the default.
152  * NB: fd is copied into a private area.
153  * NB: The rpch->cl_auth is set null authentication. Caller may wish to
154  * set this something more useful.
155  *
156  * fd should be an open socket
157  */
158 CLIENT *
159 clnt_vc_create(
160         struct socket *so,              /* open file descriptor */
161         struct sockaddr *raddr,         /* servers address */
162         const rpcprog_t prog,           /* program number */
163         const rpcvers_t vers,           /* version number */
164         size_t sendsz,                  /* buffer recv size */
165         size_t recvsz,                  /* buffer send size */
166         int intrflag)                   /* interruptible */
167 {
168         CLIENT *cl;                     /* client handle */
169         struct ct_data *ct = NULL;      /* client handle */
170         struct timeval now;
171         struct rpc_msg call_msg;
172         static uint32_t disrupt;
173         struct __rpc_sockinfo si;
174         XDR xdrs;
175         int error, interrupted, one = 1, sleep_flag;
176         struct sockopt sopt;
177
178         if (disrupt == 0)
179                 disrupt = (uint32_t)(long)raddr;
180
181         cl = (CLIENT *)mem_alloc(sizeof (*cl));
182         ct = (struct ct_data *)mem_alloc(sizeof (*ct));
183
184         mtx_init(&ct->ct_lock, "ct->ct_lock", NULL, MTX_DEF);
185         ct->ct_threads = 0;
186         ct->ct_closing = FALSE;
187         ct->ct_closed = FALSE;
188         ct->ct_upcallrefs = 0;
189
190         if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
191                 error = soconnect(so, raddr, curthread);
192                 SOCK_LOCK(so);
193                 interrupted = 0;
194                 sleep_flag = PSOCK;
195                 if (intrflag != 0)
196                         sleep_flag |= (PCATCH | PBDRY);
197                 while ((so->so_state & SS_ISCONNECTING)
198                     && so->so_error == 0) {
199                         error = msleep(&so->so_timeo, SOCK_MTX(so),
200                             sleep_flag, "connec", 0);
201                         if (error) {
202                                 if (error == EINTR || error == ERESTART)
203                                         interrupted = 1;
204                                 break;
205                         }
206                 }
207                 if (error == 0) {
208                         error = so->so_error;
209                         so->so_error = 0;
210                 }
211                 SOCK_UNLOCK(so);
212                 if (error) {
213                         if (!interrupted)
214                                 so->so_state &= ~SS_ISCONNECTING;
215                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
216                         rpc_createerr.cf_error.re_errno = error;
217                         goto err;
218                 }
219         }
220
221         if (!__rpc_socket2sockinfo(so, &si)) {
222                 goto err;
223         }
224
225         if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
226                 bzero(&sopt, sizeof(sopt));
227                 sopt.sopt_dir = SOPT_SET;
228                 sopt.sopt_level = SOL_SOCKET;
229                 sopt.sopt_name = SO_KEEPALIVE;
230                 sopt.sopt_val = &one;
231                 sopt.sopt_valsize = sizeof(one);
232                 sosetopt(so, &sopt);
233         }
234
235         if (so->so_proto->pr_protocol == IPPROTO_TCP) {
236                 bzero(&sopt, sizeof(sopt));
237                 sopt.sopt_dir = SOPT_SET;
238                 sopt.sopt_level = IPPROTO_TCP;
239                 sopt.sopt_name = TCP_NODELAY;
240                 sopt.sopt_val = &one;
241                 sopt.sopt_valsize = sizeof(one);
242                 sosetopt(so, &sopt);
243         }
244
245         ct->ct_closeit = FALSE;
246
247         /*
248          * Set up private data struct
249          */
250         ct->ct_socket = so;
251         ct->ct_wait.tv_sec = -1;
252         ct->ct_wait.tv_usec = -1;
253         memcpy(&ct->ct_addr, raddr, raddr->sa_len);
254
255         /*
256          * Initialize call message
257          */
258         getmicrotime(&now);
259         ct->ct_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now);
260         call_msg.rm_xid = ct->ct_xid;
261         call_msg.rm_direction = CALL;
262         call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
263         call_msg.rm_call.cb_prog = (uint32_t)prog;
264         call_msg.rm_call.cb_vers = (uint32_t)vers;
265
266         /*
267          * pre-serialize the static part of the call msg and stash it away
268          */
269         xdrmem_create(&xdrs, ct->ct_mcallc, MCALL_MSG_SIZE,
270             XDR_ENCODE);
271         if (! xdr_callhdr(&xdrs, &call_msg)) {
272                 if (ct->ct_closeit) {
273                         soclose(ct->ct_socket);
274                 }
275                 goto err;
276         }
277         ct->ct_mpos = XDR_GETPOS(&xdrs);
278         XDR_DESTROY(&xdrs);
279         ct->ct_waitchan = "rpcrecv";
280         ct->ct_waitflag = 0;
281
282         /*
283          * Create a client handle which uses xdrrec for serialization
284          * and authnone for authentication.
285          */
286         sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
287         recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
288         error = soreserve(ct->ct_socket, sendsz, recvsz);
289         if (error != 0) {
290                 if (ct->ct_closeit) {
291                         soclose(ct->ct_socket);
292                 }
293                 goto err;
294         }
295         cl->cl_refs = 1;
296         cl->cl_ops = &clnt_vc_ops;
297         cl->cl_private = ct;
298         cl->cl_auth = authnone_create();
299
300         SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
301         soupcall_set(ct->ct_socket, SO_RCV, clnt_vc_soupcall, ct);
302         SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
303
304         ct->ct_record = NULL;
305         ct->ct_record_resid = 0;
306         TAILQ_INIT(&ct->ct_pending);
307         return (cl);
308
309 err:
310         if (cl) {
311                 if (ct) {
312                         mtx_destroy(&ct->ct_lock);
313                         mem_free(ct, sizeof (struct ct_data));
314                 }
315                 if (cl)
316                         mem_free(cl, sizeof (CLIENT));
317         }
318         return ((CLIENT *)NULL);
319 }
320
321 static enum clnt_stat
322 clnt_vc_call(
323         CLIENT          *cl,            /* client handle */
324         struct rpc_callextra *ext,      /* call metadata */
325         rpcproc_t       proc,           /* procedure number */
326         struct mbuf     *args,          /* pointer to args */
327         struct mbuf     **resultsp,     /* pointer to results */
328         struct timeval  utimeout)
329 {
330         struct ct_data *ct = (struct ct_data *) cl->cl_private;
331         AUTH *auth;
332         struct rpc_err *errp;
333         enum clnt_stat stat;
334         XDR xdrs;
335         struct rpc_msg reply_msg;
336         bool_t ok;
337         int nrefreshes = 2;             /* number of times to refresh cred */
338         struct timeval timeout;
339         uint32_t xid;
340         struct mbuf *mreq = NULL, *results;
341         struct ct_request *cr;
342         int error;
343
344         cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
345
346         mtx_lock(&ct->ct_lock);
347
348         if (ct->ct_closing || ct->ct_closed) {
349                 mtx_unlock(&ct->ct_lock);
350                 free(cr, M_RPC);
351                 return (RPC_CANTSEND);
352         }
353         ct->ct_threads++;
354
355         if (ext) {
356                 auth = ext->rc_auth;
357                 errp = &ext->rc_err;
358         } else {
359                 auth = cl->cl_auth;
360                 errp = &ct->ct_error;
361         }
362
363         cr->cr_mrep = NULL;
364         cr->cr_error = 0;
365
366         if (ct->ct_wait.tv_usec == -1) {
367                 timeout = utimeout;     /* use supplied timeout */
368         } else {
369                 timeout = ct->ct_wait;  /* use default timeout */
370         }
371
372 call_again:
373         mtx_assert(&ct->ct_lock, MA_OWNED);
374
375         ct->ct_xid++;
376         xid = ct->ct_xid;
377
378         mtx_unlock(&ct->ct_lock);
379
380         /*
381          * Leave space to pre-pend the record mark.
382          */
383         MGETHDR(mreq, M_WAIT, MT_DATA);
384         mreq->m_data += sizeof(uint32_t);
385         KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN,
386             ("RPC header too big"));
387         bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos);
388         mreq->m_len = ct->ct_mpos;
389
390         /*
391          * The XID is the first thing in the request.
392          */
393         *mtod(mreq, uint32_t *) = htonl(xid);
394
395         xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
396
397         errp->re_status = stat = RPC_SUCCESS;
398
399         if ((! XDR_PUTINT32(&xdrs, &proc)) ||
400             (! AUTH_MARSHALL(auth, xid, &xdrs,
401                 m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
402                 errp->re_status = stat = RPC_CANTENCODEARGS;
403                 mtx_lock(&ct->ct_lock);
404                 goto out;
405         }
406         mreq->m_pkthdr.len = m_length(mreq, NULL);
407
408         /*
409          * Prepend a record marker containing the packet length.
410          */
411         M_PREPEND(mreq, sizeof(uint32_t), M_WAIT);
412         *mtod(mreq, uint32_t *) =
413                 htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t)));
414
415         cr->cr_xid = xid;
416         mtx_lock(&ct->ct_lock);
417         /*
418          * Check to see if the other end has already started to close down
419          * the connection. The upcall will have set ct_error.re_status
420          * to RPC_CANTRECV if this is the case.
421          * If the other end starts to close down the connection after this
422          * point, it will be detected later when cr_error is checked,
423          * since the request is in the ct_pending queue.
424          */
425         if (ct->ct_error.re_status == RPC_CANTRECV) {
426                 if (errp != &ct->ct_error) {
427                         errp->re_errno = ct->ct_error.re_errno;
428                         errp->re_status = RPC_CANTRECV;
429                 }
430                 stat = RPC_CANTRECV;
431                 goto out;
432         }
433         TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link);
434         mtx_unlock(&ct->ct_lock);
435
436         /*
437          * sosend consumes mreq.
438          */
439         error = sosend(ct->ct_socket, NULL, NULL, mreq, NULL, 0, curthread);
440         mreq = NULL;
441         if (error == EMSGSIZE) {
442                 SOCKBUF_LOCK(&ct->ct_socket->so_snd);
443                 sbwait(&ct->ct_socket->so_snd);
444                 SOCKBUF_UNLOCK(&ct->ct_socket->so_snd);
445                 AUTH_VALIDATE(auth, xid, NULL, NULL);
446                 mtx_lock(&ct->ct_lock);
447                 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
448                 goto call_again;
449         }
450
451         reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
452         reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
453         reply_msg.acpted_rply.ar_verf.oa_length = 0;
454         reply_msg.acpted_rply.ar_results.where = NULL;
455         reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
456
457         mtx_lock(&ct->ct_lock);
458         if (error) {
459                 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
460                 errp->re_errno = error;
461                 errp->re_status = stat = RPC_CANTSEND;
462                 goto out;
463         }
464
465         /*
466          * Check to see if we got an upcall while waiting for the
467          * lock. In both these cases, the request has been removed
468          * from ct->ct_pending.
469          */
470         if (cr->cr_error) {
471                 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
472                 errp->re_errno = cr->cr_error;
473                 errp->re_status = stat = RPC_CANTRECV;
474                 goto out;
475         }
476         if (cr->cr_mrep) {
477                 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
478                 goto got_reply;
479         }
480
481         /*
482          * Hack to provide rpc-based message passing
483          */
484         if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
485                 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
486                 errp->re_status = stat = RPC_TIMEDOUT;
487                 goto out;
488         }
489
490         error = msleep(cr, &ct->ct_lock, ct->ct_waitflag, ct->ct_waitchan,
491             tvtohz(&timeout));
492
493         TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
494
495         if (error) {
496                 /*
497                  * The sleep returned an error so our request is still
498                  * on the list. Turn the error code into an
499                  * appropriate client status.
500                  */
501                 errp->re_errno = error;
502                 switch (error) {
503                 case EINTR:
504                 case ERESTART:
505                         stat = RPC_INTR;
506                         break;
507                 case EWOULDBLOCK:
508                         stat = RPC_TIMEDOUT;
509                         break;
510                 default:
511                         stat = RPC_CANTRECV;
512                 }
513                 errp->re_status = stat;
514                 goto out;
515         } else {
516                 /*
517                  * We were woken up by the upcall.  If the
518                  * upcall had a receive error, report that,
519                  * otherwise we have a reply.
520                  */
521                 if (cr->cr_error) {
522                         errp->re_errno = cr->cr_error;
523                         errp->re_status = stat = RPC_CANTRECV;
524                         goto out;
525                 }
526         }
527
528 got_reply:
529         /*
530          * Now decode and validate the response. We need to drop the
531          * lock since xdr_replymsg may end up sleeping in malloc.
532          */
533         mtx_unlock(&ct->ct_lock);
534
535         if (ext && ext->rc_feedback)
536                 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
537
538         xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
539         ok = xdr_replymsg(&xdrs, &reply_msg);
540         cr->cr_mrep = NULL;
541
542         if (ok) {
543                 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
544                     (reply_msg.acpted_rply.ar_stat == SUCCESS))
545                         errp->re_status = stat = RPC_SUCCESS;
546                 else
547                         stat = _seterr_reply(&reply_msg, errp);
548
549                 if (stat == RPC_SUCCESS) {
550                         results = xdrmbuf_getall(&xdrs);
551                         if (!AUTH_VALIDATE(auth, xid,
552                                 &reply_msg.acpted_rply.ar_verf,
553                                 &results)) {
554                                 errp->re_status = stat = RPC_AUTHERROR;
555                                 errp->re_why = AUTH_INVALIDRESP;
556                         } else {
557                                 KASSERT(results,
558                                     ("auth validated but no result"));
559                                 *resultsp = results;
560                         }
561                 }               /* end successful completion */
562                 /*
563                  * If unsuccesful AND error is an authentication error
564                  * then refresh credentials and try again, else break
565                  */
566                 else if (stat == RPC_AUTHERROR)
567                         /* maybe our credentials need to be refreshed ... */
568                         if (nrefreshes > 0 &&
569                             AUTH_REFRESH(auth, &reply_msg)) {
570                                 nrefreshes--;
571                                 XDR_DESTROY(&xdrs);
572                                 mtx_lock(&ct->ct_lock);
573                                 goto call_again;
574                         }
575                 /* end of unsuccessful completion */
576         }       /* end of valid reply message */
577         else {
578                 errp->re_status = stat = RPC_CANTDECODERES;
579         }
580         XDR_DESTROY(&xdrs);
581         mtx_lock(&ct->ct_lock);
582 out:
583         mtx_assert(&ct->ct_lock, MA_OWNED);
584
585         KASSERT(stat != RPC_SUCCESS || *resultsp,
586             ("RPC_SUCCESS without reply"));
587
588         if (mreq)
589                 m_freem(mreq);
590         if (cr->cr_mrep)
591                 m_freem(cr->cr_mrep);
592
593         ct->ct_threads--;
594         if (ct->ct_closing)
595                 wakeup(ct);
596                 
597         mtx_unlock(&ct->ct_lock);
598
599         if (auth && stat != RPC_SUCCESS)
600                 AUTH_VALIDATE(auth, xid, NULL, NULL);
601
602         free(cr, M_RPC);
603
604         return (stat);
605 }
606
607 static void
608 clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
609 {
610         struct ct_data *ct = (struct ct_data *) cl->cl_private;
611
612         *errp = ct->ct_error;
613 }
614
615 static bool_t
616 clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
617 {
618         XDR xdrs;
619         bool_t dummy;
620
621         xdrs.x_op = XDR_FREE;
622         dummy = (*xdr_res)(&xdrs, res_ptr);
623
624         return (dummy);
625 }
626
627 /*ARGSUSED*/
628 static void
629 clnt_vc_abort(CLIENT *cl)
630 {
631 }
632
633 static bool_t
634 clnt_vc_control(CLIENT *cl, u_int request, void *info)
635 {
636         struct ct_data *ct = (struct ct_data *)cl->cl_private;
637         void *infop = info;
638
639         mtx_lock(&ct->ct_lock);
640
641         switch (request) {
642         case CLSET_FD_CLOSE:
643                 ct->ct_closeit = TRUE;
644                 mtx_unlock(&ct->ct_lock);
645                 return (TRUE);
646         case CLSET_FD_NCLOSE:
647                 ct->ct_closeit = FALSE;
648                 mtx_unlock(&ct->ct_lock);
649                 return (TRUE);
650         default:
651                 break;
652         }
653
654         /* for other requests which use info */
655         if (info == NULL) {
656                 mtx_unlock(&ct->ct_lock);
657                 return (FALSE);
658         }
659         switch (request) {
660         case CLSET_TIMEOUT:
661                 if (time_not_ok((struct timeval *)info)) {
662                         mtx_unlock(&ct->ct_lock);
663                         return (FALSE);
664                 }
665                 ct->ct_wait = *(struct timeval *)infop;
666                 break;
667         case CLGET_TIMEOUT:
668                 *(struct timeval *)infop = ct->ct_wait;
669                 break;
670         case CLGET_SERVER_ADDR:
671                 (void) memcpy(info, &ct->ct_addr, (size_t)ct->ct_addr.ss_len);
672                 break;
673         case CLGET_SVC_ADDR:
674                 /*
675                  * Slightly different semantics to userland - we use
676                  * sockaddr instead of netbuf.
677                  */
678                 memcpy(info, &ct->ct_addr, ct->ct_addr.ss_len);
679                 break;
680         case CLSET_SVC_ADDR:            /* set to new address */
681                 mtx_unlock(&ct->ct_lock);
682                 return (FALSE);
683         case CLGET_XID:
684                 *(uint32_t *)info = ct->ct_xid;
685                 break;
686         case CLSET_XID:
687                 /* This will set the xid of the NEXT call */
688                 /* decrement by 1 as clnt_vc_call() increments once */
689                 ct->ct_xid = *(uint32_t *)info - 1;
690                 break;
691         case CLGET_VERS:
692                 /*
693                  * This RELIES on the information that, in the call body,
694                  * the version number field is the fifth field from the
695                  * begining of the RPC header. MUST be changed if the
696                  * call_struct is changed
697                  */
698                 *(uint32_t *)info =
699                     ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
700                     4 * BYTES_PER_XDR_UNIT));
701                 break;
702
703         case CLSET_VERS:
704                 *(uint32_t *)(void *)(ct->ct_mcallc +
705                     4 * BYTES_PER_XDR_UNIT) =
706                     htonl(*(uint32_t *)info);
707                 break;
708
709         case CLGET_PROG:
710                 /*
711                  * This RELIES on the information that, in the call body,
712                  * the program number field is the fourth field from the
713                  * begining of the RPC header. MUST be changed if the
714                  * call_struct is changed
715                  */
716                 *(uint32_t *)info =
717                     ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
718                     3 * BYTES_PER_XDR_UNIT));
719                 break;
720
721         case CLSET_PROG:
722                 *(uint32_t *)(void *)(ct->ct_mcallc +
723                     3 * BYTES_PER_XDR_UNIT) =
724                     htonl(*(uint32_t *)info);
725                 break;
726
727         case CLSET_WAITCHAN:
728                 ct->ct_waitchan = (const char *)info;
729                 break;
730
731         case CLGET_WAITCHAN:
732                 *(const char **) info = ct->ct_waitchan;
733                 break;
734
735         case CLSET_INTERRUPTIBLE:
736                 if (*(int *) info)
737                         ct->ct_waitflag = PCATCH | PBDRY;
738                 else
739                         ct->ct_waitflag = 0;
740                 break;
741
742         case CLGET_INTERRUPTIBLE:
743                 if (ct->ct_waitflag)
744                         *(int *) info = TRUE;
745                 else
746                         *(int *) info = FALSE;
747                 break;
748
749         default:
750                 mtx_unlock(&ct->ct_lock);
751                 return (FALSE);
752         }
753
754         mtx_unlock(&ct->ct_lock);
755         return (TRUE);
756 }
757
758 static void
759 clnt_vc_close(CLIENT *cl)
760 {
761         struct ct_data *ct = (struct ct_data *) cl->cl_private;
762         struct ct_request *cr;
763
764         mtx_lock(&ct->ct_lock);
765
766         if (ct->ct_closed) {
767                 mtx_unlock(&ct->ct_lock);
768                 return;
769         }
770
771         if (ct->ct_closing) {
772                 while (ct->ct_closing)
773                         msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
774                 KASSERT(ct->ct_closed, ("client should be closed"));
775                 mtx_unlock(&ct->ct_lock);
776                 return;
777         }
778
779         if (ct->ct_socket) {
780                 ct->ct_closing = TRUE;
781                 mtx_unlock(&ct->ct_lock);
782
783                 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
784                 soupcall_clear(ct->ct_socket, SO_RCV);
785                 clnt_vc_upcallsdone(ct);
786                 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
787
788                 /*
789                  * Abort any pending requests and wait until everyone
790                  * has finished with clnt_vc_call.
791                  */
792                 mtx_lock(&ct->ct_lock);
793                 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
794                         cr->cr_xid = 0;
795                         cr->cr_error = ESHUTDOWN;
796                         wakeup(cr);
797                 }
798
799                 while (ct->ct_threads)
800                         msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
801         }
802
803         ct->ct_closing = FALSE;
804         ct->ct_closed = TRUE;
805         mtx_unlock(&ct->ct_lock);
806         wakeup(ct);
807 }
808
809 static void
810 clnt_vc_destroy(CLIENT *cl)
811 {
812         struct ct_data *ct = (struct ct_data *) cl->cl_private;
813         struct socket *so = NULL;
814
815         clnt_vc_close(cl);
816
817         mtx_lock(&ct->ct_lock);
818
819         if (ct->ct_socket) {
820                 if (ct->ct_closeit) {
821                         so = ct->ct_socket;
822                 }
823         }
824
825         mtx_unlock(&ct->ct_lock);
826
827         mtx_destroy(&ct->ct_lock);
828         if (so) {
829                 soshutdown(so, SHUT_WR);
830                 soclose(so);
831         }
832         mem_free(ct, sizeof(struct ct_data));
833         if (cl->cl_netid && cl->cl_netid[0])
834                 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
835         if (cl->cl_tp && cl->cl_tp[0])
836                 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
837         mem_free(cl, sizeof(CLIENT));
838 }
839
840 /*
841  * Make sure that the time is not garbage.   -1 value is disallowed.
842  * Note this is different from time_not_ok in clnt_dg.c
843  */
844 static bool_t
845 time_not_ok(struct timeval *t)
846 {
847         return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
848                 t->tv_usec <= -1 || t->tv_usec > 1000000);
849 }
850
851 int
852 clnt_vc_soupcall(struct socket *so, void *arg, int waitflag)
853 {
854         struct ct_data *ct = (struct ct_data *) arg;
855         struct uio uio;
856         struct mbuf *m;
857         struct ct_request *cr;
858         int error, rcvflag, foundreq;
859         uint32_t xid, header;
860         bool_t do_read;
861
862         ct->ct_upcallrefs++;
863         uio.uio_td = curthread;
864         do {
865                 /*
866                  * If ct_record_resid is zero, we are waiting for a
867                  * record mark.
868                  */
869                 if (ct->ct_record_resid == 0) {
870
871                         /*
872                          * Make sure there is either a whole record
873                          * mark in the buffer or there is some other
874                          * error condition
875                          */
876                         do_read = FALSE;
877                         if (so->so_rcv.sb_cc >= sizeof(uint32_t)
878                             || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
879                             || so->so_error)
880                                 do_read = TRUE;
881
882                         if (!do_read)
883                                 break;
884
885                         SOCKBUF_UNLOCK(&so->so_rcv);
886                         uio.uio_resid = sizeof(uint32_t);
887                         m = NULL;
888                         rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
889                         error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
890                         SOCKBUF_LOCK(&so->so_rcv);
891
892                         if (error == EWOULDBLOCK)
893                                 break;
894                         
895                         /*
896                          * If there was an error, wake up all pending
897                          * requests.
898                          */
899                         if (error || uio.uio_resid > 0) {
900                         wakeup_all:
901                                 mtx_lock(&ct->ct_lock);
902                                 if (!error) {
903                                         /*
904                                          * We must have got EOF trying
905                                          * to read from the stream.
906                                          */
907                                         error = ECONNRESET;
908                                 }
909                                 ct->ct_error.re_status = RPC_CANTRECV;
910                                 ct->ct_error.re_errno = error;
911                                 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
912                                         cr->cr_error = error;
913                                         wakeup(cr);
914                                 }
915                                 mtx_unlock(&ct->ct_lock);
916                                 break;
917                         }
918                         m_copydata(m, 0, sizeof(uint32_t), (char *)&header);
919                         header = ntohl(header);
920                         ct->ct_record = NULL;
921                         ct->ct_record_resid = header & 0x7fffffff;
922                         ct->ct_record_eor = ((header & 0x80000000) != 0);
923                         m_freem(m);
924                 } else {
925                         /*
926                          * Wait until the socket has the whole record
927                          * buffered.
928                          */
929                         do_read = FALSE;
930                         if (so->so_rcv.sb_cc >= ct->ct_record_resid
931                             || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
932                             || so->so_error)
933                                 do_read = TRUE;
934
935                         if (!do_read)
936                                 break;
937
938                         /*
939                          * We have the record mark. Read as much as
940                          * the socket has buffered up to the end of
941                          * this record.
942                          */
943                         SOCKBUF_UNLOCK(&so->so_rcv);
944                         uio.uio_resid = ct->ct_record_resid;
945                         m = NULL;
946                         rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
947                         error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
948                         SOCKBUF_LOCK(&so->so_rcv);
949
950                         if (error == EWOULDBLOCK)
951                                 break;
952
953                         if (error || uio.uio_resid == ct->ct_record_resid)
954                                 goto wakeup_all;
955
956                         /*
957                          * If we have part of the record already,
958                          * chain this bit onto the end.
959                          */
960                         if (ct->ct_record)
961                                 m_last(ct->ct_record)->m_next = m;
962                         else
963                                 ct->ct_record = m;
964
965                         ct->ct_record_resid = uio.uio_resid;
966
967                         /*
968                          * If we have the entire record, see if we can
969                          * match it to a request.
970                          */
971                         if (ct->ct_record_resid == 0
972                             && ct->ct_record_eor) {
973                                 /*
974                                  * The XID is in the first uint32_t of
975                                  * the reply.
976                                  */
977                                 if (ct->ct_record->m_len < sizeof(xid) &&
978                                     m_length(ct->ct_record, NULL) <
979                                     sizeof(xid)) {
980                                         m_freem(ct->ct_record);
981                                         break;
982                                 }
983                                 m_copydata(ct->ct_record, 0, sizeof(xid),
984                                     (char *)&xid);
985                                 xid = ntohl(xid);
986
987                                 mtx_lock(&ct->ct_lock);
988                                 foundreq = 0;
989                                 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
990                                         if (cr->cr_xid == xid) {
991                                                 /*
992                                                  * This one
993                                                  * matches. We leave
994                                                  * the reply mbuf in
995                                                  * cr->cr_mrep. Set
996                                                  * the XID to zero so
997                                                  * that we will ignore
998                                                  * any duplicaed
999                                                  * replies.
1000                                                  */
1001                                                 cr->cr_xid = 0;
1002                                                 cr->cr_mrep = ct->ct_record;
1003                                                 cr->cr_error = 0;
1004                                                 foundreq = 1;
1005                                                 wakeup(cr);
1006                                                 break;
1007                                         }
1008                                 }
1009                                 mtx_unlock(&ct->ct_lock);
1010
1011                                 if (!foundreq)
1012                                         m_freem(ct->ct_record);
1013                                 ct->ct_record = NULL;
1014                         }
1015                 }
1016         } while (m);
1017         ct->ct_upcallrefs--;
1018         if (ct->ct_upcallrefs < 0)
1019                 panic("rpcvc upcall refcnt");
1020         if (ct->ct_upcallrefs == 0)
1021                 wakeup(&ct->ct_upcallrefs);
1022         return (SU_OK);
1023 }
1024
1025 /*
1026  * Wait for all upcalls in progress to complete.
1027  */
1028 static void
1029 clnt_vc_upcallsdone(struct ct_data *ct)
1030 {
1031
1032         SOCKBUF_LOCK_ASSERT(&ct->ct_socket->so_rcv);
1033
1034         while (ct->ct_upcallrefs > 0)
1035                 (void) msleep(&ct->ct_upcallrefs,
1036                     SOCKBUF_MTX(&ct->ct_socket->so_rcv), 0, "rpcvcup", 0);
1037 }