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