1 /* $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
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.
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.
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";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
42 * Copyright (C) 1984, Sun Microsystems, Inc.
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).
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....
55 * Now go hang yourself.
59 * This code handles the special case of a NFSv4.n backchannel for
60 * callback RPCs. It is similar to clnt_vc.c, but uses the TCP
61 * connection provided by the client to the server.
64 #include <sys/param.h>
65 #include <sys/systm.h>
67 #include <sys/malloc.h>
69 #include <sys/mutex.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
76 #include <sys/syslog.h>
82 #include <netinet/tcp.h>
85 #include <rpc/rpc_com.h>
90 struct cmsgcred cmcred;
93 static void clnt_bck_geterr(CLIENT *, struct rpc_err *);
94 static bool_t clnt_bck_freeres(CLIENT *, xdrproc_t, void *);
95 static void clnt_bck_abort(CLIENT *);
96 static bool_t clnt_bck_control(CLIENT *, u_int, void *);
97 static void clnt_bck_close(CLIENT *);
98 static void clnt_bck_destroy(CLIENT *);
100 static struct clnt_ops clnt_bck_ops = {
101 .cl_abort = clnt_bck_abort,
102 .cl_geterr = clnt_bck_geterr,
103 .cl_freeres = clnt_bck_freeres,
104 .cl_close = clnt_bck_close,
105 .cl_destroy = clnt_bck_destroy,
106 .cl_control = clnt_bck_control
110 * Create a client handle for a connection.
111 * Default options are set, which the user can change using clnt_control()'s.
112 * This code handles the special case of an NFSv4.1 session backchannel
113 * call, which is sent on a TCP connection created against the server
118 struct socket *so, /* Server transport socket. */
119 const rpcprog_t prog, /* program number */
120 const rpcvers_t vers) /* version number */
122 CLIENT *cl; /* client handle */
123 struct ct_data *ct = NULL; /* client handle */
125 struct rpc_msg call_msg;
126 static uint32_t disrupt;
130 disrupt = (uint32_t)(long)so;
132 cl = (CLIENT *)mem_alloc(sizeof (*cl));
133 ct = (struct ct_data *)mem_alloc(sizeof (*ct));
135 mtx_init(&ct->ct_lock, "ct->ct_lock", NULL, MTX_DEF);
137 ct->ct_closing = FALSE;
138 ct->ct_closed = FALSE;
139 ct->ct_upcallrefs = 0;
140 ct->ct_closeit = FALSE;
143 * Set up private data struct
145 ct->ct_wait.tv_sec = -1;
146 ct->ct_wait.tv_usec = -1;
149 * Initialize call message
152 ct->ct_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now);
153 call_msg.rm_xid = ct->ct_xid;
154 call_msg.rm_direction = CALL;
155 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
156 call_msg.rm_call.cb_prog = (uint32_t)prog;
157 call_msg.rm_call.cb_vers = (uint32_t)vers;
160 * pre-serialize the static part of the call msg and stash it away
162 xdrmem_create(&xdrs, ct->ct_mcallc, MCALL_MSG_SIZE,
164 if (!xdr_callhdr(&xdrs, &call_msg))
166 ct->ct_mpos = XDR_GETPOS(&xdrs);
168 ct->ct_waitchan = "rpcbck";
171 cl->cl_ops = &clnt_bck_ops;
173 cl->cl_auth = authnone_create();
174 TAILQ_INIT(&ct->ct_pending);
180 mtx_destroy(&ct->ct_lock);
181 mem_free(ct, sizeof (struct ct_data));
184 mem_free(cl, sizeof (CLIENT));
191 CLIENT *cl, /* client handle */
192 struct rpc_callextra *ext, /* call metadata */
193 rpcproc_t proc, /* procedure number */
194 struct mbuf *args, /* pointer to args */
195 struct mbuf **resultsp, /* pointer to results */
196 struct timeval utimeout,
199 struct ct_data *ct = (struct ct_data *) cl->cl_private;
201 struct rpc_err *errp;
204 struct rpc_msg reply_msg;
206 int nrefreshes = 2; /* number of times to refresh cred */
207 struct timeval timeout;
209 struct mbuf *mreq = NULL, *results;
210 struct ct_request *cr;
213 cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
215 mtx_lock(&ct->ct_lock);
217 if (ct->ct_closing || ct->ct_closed) {
218 mtx_unlock(&ct->ct_lock);
220 return (RPC_CANTSEND);
229 errp = &ct->ct_error;
235 if (ct->ct_wait.tv_usec == -1)
236 timeout = utimeout; /* use supplied timeout */
238 timeout = ct->ct_wait; /* use default timeout */
241 mtx_assert(&ct->ct_lock, MA_OWNED);
246 mtx_unlock(&ct->ct_lock);
249 * Leave space to pre-pend the record mark.
251 mreq = m_gethdr(M_WAITOK, MT_DATA);
252 mreq->m_data += sizeof(uint32_t);
253 KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN,
254 ("RPC header too big"));
255 bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos);
256 mreq->m_len = ct->ct_mpos;
259 * The XID is the first thing in the request.
261 *mtod(mreq, uint32_t *) = htonl(xid);
263 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
265 errp->re_status = stat = RPC_SUCCESS;
267 if ((!XDR_PUTINT32(&xdrs, &proc)) ||
268 (!AUTH_MARSHALL(auth, xid, &xdrs,
269 m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
270 errp->re_status = stat = RPC_CANTENCODEARGS;
271 mtx_lock(&ct->ct_lock);
274 mreq->m_pkthdr.len = m_length(mreq, NULL);
277 * Prepend a record marker containing the packet length.
279 M_PREPEND(mreq, sizeof(uint32_t), M_WAITOK);
280 *mtod(mreq, uint32_t *) =
281 htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t)));
284 mtx_lock(&ct->ct_lock);
286 * Check to see if the client end has already started to close down
287 * the connection. The svc code will have set ct_error.re_status
288 * to RPC_CANTRECV if this is the case.
289 * If the client starts to close down the connection after this
290 * point, it will be detected later when cr_error is checked,
291 * since the request is in the ct_pending queue.
293 if (ct->ct_error.re_status == RPC_CANTRECV) {
294 if (errp != &ct->ct_error) {
295 errp->re_errno = ct->ct_error.re_errno;
296 errp->re_status = RPC_CANTRECV;
301 TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link);
302 mtx_unlock(&ct->ct_lock);
305 * sosend consumes mreq.
307 sx_xlock(&xprt->xp_lock);
308 error = sosend(xprt->xp_socket, NULL, NULL, mreq, NULL, 0, curthread);
309 if (error != 0) printf("sosend=%d\n", error);
311 if (error == EMSGSIZE) {
312 printf("emsgsize\n");
313 SOCKBUF_LOCK(&xprt->xp_socket->so_snd);
314 sbwait(&xprt->xp_socket->so_snd);
315 SOCKBUF_UNLOCK(&xprt->xp_socket->so_snd);
316 sx_xunlock(&xprt->xp_lock);
317 AUTH_VALIDATE(auth, xid, NULL, NULL);
318 mtx_lock(&ct->ct_lock);
319 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
322 sx_xunlock(&xprt->xp_lock);
324 reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
325 reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
326 reply_msg.acpted_rply.ar_verf.oa_length = 0;
327 reply_msg.acpted_rply.ar_results.where = NULL;
328 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
330 mtx_lock(&ct->ct_lock);
332 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
333 errp->re_errno = error;
334 errp->re_status = stat = RPC_CANTSEND;
339 * Check to see if we got an upcall while waiting for the
340 * lock. In both these cases, the request has been removed
341 * from ct->ct_pending.
344 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
345 errp->re_errno = cr->cr_error;
346 errp->re_status = stat = RPC_CANTRECV;
350 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
355 * Hack to provide rpc-based message passing
357 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
358 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
359 errp->re_status = stat = RPC_TIMEDOUT;
363 error = msleep(cr, &ct->ct_lock, ct->ct_waitflag, ct->ct_waitchan,
366 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
370 * The sleep returned an error so our request is still
371 * on the list. Turn the error code into an
372 * appropriate client status.
374 errp->re_errno = error;
385 errp->re_status = stat;
389 * We were woken up by the svc thread. If the
390 * upcall had a receive error, report that,
391 * otherwise we have a reply.
394 errp->re_errno = cr->cr_error;
395 errp->re_status = stat = RPC_CANTRECV;
402 * Now decode and validate the response. We need to drop the
403 * lock since xdr_replymsg may end up sleeping in malloc.
405 mtx_unlock(&ct->ct_lock);
407 if (ext && ext->rc_feedback)
408 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
410 xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
411 ok = xdr_replymsg(&xdrs, &reply_msg);
415 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
416 (reply_msg.acpted_rply.ar_stat == SUCCESS))
417 errp->re_status = stat = RPC_SUCCESS;
419 stat = _seterr_reply(&reply_msg, errp);
421 if (stat == RPC_SUCCESS) {
422 results = xdrmbuf_getall(&xdrs);
423 if (!AUTH_VALIDATE(auth, xid,
424 &reply_msg.acpted_rply.ar_verf, &results)) {
425 errp->re_status = stat = RPC_AUTHERROR;
426 errp->re_why = AUTH_INVALIDRESP;
429 ("auth validated but no result"));
432 } /* end successful completion */
434 * If unsuccesful AND error is an authentication error
435 * then refresh credentials and try again, else break
437 else if (stat == RPC_AUTHERROR)
438 /* maybe our credentials need to be refreshed ... */
439 if (nrefreshes > 0 && AUTH_REFRESH(auth, &reply_msg)) {
442 mtx_lock(&ct->ct_lock);
445 /* end of unsuccessful completion */
446 /* end of valid reply message */
448 errp->re_status = stat = RPC_CANTDECODERES;
450 mtx_lock(&ct->ct_lock);
452 mtx_assert(&ct->ct_lock, MA_OWNED);
454 KASSERT(stat != RPC_SUCCESS || *resultsp,
455 ("RPC_SUCCESS without reply"));
459 if (cr->cr_mrep != NULL)
460 m_freem(cr->cr_mrep);
466 mtx_unlock(&ct->ct_lock);
468 if (auth && stat != RPC_SUCCESS)
469 AUTH_VALIDATE(auth, xid, NULL, NULL);
477 clnt_bck_geterr(CLIENT *cl, struct rpc_err *errp)
479 struct ct_data *ct = (struct ct_data *) cl->cl_private;
481 *errp = ct->ct_error;
485 clnt_bck_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
490 xdrs.x_op = XDR_FREE;
491 dummy = (*xdr_res)(&xdrs, res_ptr);
498 clnt_bck_abort(CLIENT *cl)
503 clnt_bck_control(CLIENT *cl, u_int request, void *info)
510 clnt_bck_close(CLIENT *cl)
512 struct ct_data *ct = (struct ct_data *) cl->cl_private;
514 mtx_lock(&ct->ct_lock);
517 mtx_unlock(&ct->ct_lock);
521 if (ct->ct_closing) {
522 while (ct->ct_closing)
523 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
524 KASSERT(ct->ct_closed, ("client should be closed"));
525 mtx_unlock(&ct->ct_lock);
529 ct->ct_closing = FALSE;
530 ct->ct_closed = TRUE;
531 mtx_unlock(&ct->ct_lock);
536 clnt_bck_destroy(CLIENT *cl)
538 struct ct_data *ct = (struct ct_data *) cl->cl_private;
542 mtx_destroy(&ct->ct_lock);
543 mem_free(ct, sizeof(struct ct_data));
544 if (cl->cl_netid && cl->cl_netid[0])
545 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
546 if (cl->cl_tp && cl->cl_tp[0])
547 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
548 mem_free(cl, sizeof(CLIENT));
552 * This call is done by the svc code when a backchannel RPC reply is
556 clnt_bck_svccall(void *arg, struct mbuf *mrep, uint32_t xid)
558 struct ct_data *ct = (struct ct_data *)arg;
559 struct ct_request *cr;
562 mtx_lock(&ct->ct_lock);
565 * See if we can match this reply to a request.
568 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
569 if (cr->cr_xid == xid) {
571 * This one matches. We leave the reply mbuf list in
572 * cr->cr_mrep. Set the XID to zero so that we will
573 * ignore any duplicated replies.
585 if (ct->ct_upcallrefs < 0)
586 panic("rpcvc svccall refcnt");
587 if (ct->ct_upcallrefs == 0)
588 wakeup(&ct->ct_upcallrefs);
589 mtx_unlock(&ct->ct_lock);