1 /* $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";
36 static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
44 * Copyright (C) 1984, Sun Microsystems, Inc.
46 * TCP based RPC supports 'batched calls'.
47 * A sequence of calls may be batched-up in a send buffer. The rpc call
48 * return immediately to the client even though the call was not necessarily
49 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
50 * the rpc timeout value is zero (see clnt.h, rpc).
52 * Clients should NOT casually batch calls that in fact return results; that is,
53 * the server side should be aware that a call is batched and not produce any
54 * return message. Batched calls that produce many result messages can
55 * deadlock (netlock) the client and the server....
57 * Now go hang yourself.
60 #include "opt_kern_tls.h"
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/kthread.h>
68 #include <sys/malloc.h>
70 #include <sys/mutex.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
77 #include <sys/syslog.h>
83 #include <netinet/tcp.h>
86 #include <rpc/rpc_com.h>
88 #include <rpc/rpcsec_tls.h>
92 struct cmsgcred cmcred;
95 static enum clnt_stat clnt_vc_call(CLIENT *, struct rpc_callextra *,
96 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
97 static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
98 static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *);
99 static void clnt_vc_abort(CLIENT *);
100 static bool_t clnt_vc_control(CLIENT *, u_int, void *);
101 static void clnt_vc_close(CLIENT *);
102 static void clnt_vc_destroy(CLIENT *);
103 static bool_t time_not_ok(struct timeval *);
104 static int clnt_vc_soupcall(struct socket *so, void *arg, int waitflag);
105 static void clnt_vc_dotlsupcall(void *data);
107 static const struct clnt_ops clnt_vc_ops = {
108 .cl_call = clnt_vc_call,
109 .cl_abort = clnt_vc_abort,
110 .cl_geterr = clnt_vc_geterr,
111 .cl_freeres = clnt_vc_freeres,
112 .cl_close = clnt_vc_close,
113 .cl_destroy = clnt_vc_destroy,
114 .cl_control = clnt_vc_control
117 static void clnt_vc_upcallsdone(struct ct_data *);
120 * Create a client handle for a connection.
121 * Default options are set, which the user can change using clnt_control()'s.
122 * The rpc/vc package does buffering similar to stdio, so the client
123 * must pick send and receive buffer sizes, 0 => use the default.
124 * NB: fd is copied into a private area.
125 * NB: The rpch->cl_auth is set null authentication. Caller may wish to
126 * set this something more useful.
128 * fd should be an open socket
132 struct socket *so, /* open file descriptor */
133 struct sockaddr *raddr, /* servers address */
134 const rpcprog_t prog, /* program number */
135 const rpcvers_t vers, /* version number */
136 size_t sendsz, /* buffer recv size */
137 size_t recvsz, /* buffer send size */
138 int intrflag) /* interruptible */
140 CLIENT *cl; /* client handle */
141 struct ct_data *ct = NULL; /* client handle */
143 struct rpc_msg call_msg;
144 static uint32_t disrupt;
145 struct __rpc_sockinfo si;
147 int error, interrupted, one = 1, sleep_flag;
151 disrupt = (uint32_t)(long)raddr;
153 cl = (CLIENT *)mem_alloc(sizeof (*cl));
154 ct = (struct ct_data *)mem_alloc(sizeof (*ct));
156 mtx_init(&ct->ct_lock, "ct->ct_lock", NULL, MTX_DEF);
158 ct->ct_closing = FALSE;
159 ct->ct_closed = FALSE;
160 ct->ct_upcallrefs = 0;
161 ct->ct_rcvstate = RPCRCVSTATE_NORMAL;
163 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
164 error = soconnect(so, raddr, curthread);
169 sleep_flag |= PCATCH;
170 while ((so->so_state & SS_ISCONNECTING)
171 && so->so_error == 0) {
172 error = msleep(&so->so_timeo, SOCK_MTX(so),
173 sleep_flag, "connec", 0);
175 if (error == EINTR || error == ERESTART)
181 error = so->so_error;
187 so->so_state &= ~SS_ISCONNECTING;
188 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
189 rpc_createerr.cf_error.re_errno = error;
194 if (!__rpc_socket2sockinfo(so, &si)) {
198 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
199 bzero(&sopt, sizeof(sopt));
200 sopt.sopt_dir = SOPT_SET;
201 sopt.sopt_level = SOL_SOCKET;
202 sopt.sopt_name = SO_KEEPALIVE;
203 sopt.sopt_val = &one;
204 sopt.sopt_valsize = sizeof(one);
208 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
209 bzero(&sopt, sizeof(sopt));
210 sopt.sopt_dir = SOPT_SET;
211 sopt.sopt_level = IPPROTO_TCP;
212 sopt.sopt_name = TCP_NODELAY;
213 sopt.sopt_val = &one;
214 sopt.sopt_valsize = sizeof(one);
218 ct->ct_closeit = FALSE;
221 * Set up private data struct
224 ct->ct_wait.tv_sec = -1;
225 ct->ct_wait.tv_usec = -1;
226 memcpy(&ct->ct_addr, raddr, raddr->sa_len);
229 * Initialize call message
232 ct->ct_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now);
233 call_msg.rm_xid = ct->ct_xid;
234 call_msg.rm_direction = CALL;
235 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
236 call_msg.rm_call.cb_prog = (uint32_t)prog;
237 call_msg.rm_call.cb_vers = (uint32_t)vers;
240 * pre-serialize the static part of the call msg and stash it away
242 xdrmem_create(&xdrs, ct->ct_mcallc, MCALL_MSG_SIZE,
244 if (! xdr_callhdr(&xdrs, &call_msg)) {
245 if (ct->ct_closeit) {
246 soclose(ct->ct_socket);
250 ct->ct_mpos = XDR_GETPOS(&xdrs);
252 ct->ct_waitchan = "rpcrecv";
256 * Create a client handle which uses xdrrec for serialization
257 * and authnone for authentication.
259 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
260 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
261 error = soreserve(ct->ct_socket, sendsz, recvsz);
263 if (ct->ct_closeit) {
264 soclose(ct->ct_socket);
269 cl->cl_ops = &clnt_vc_ops;
271 cl->cl_auth = authnone_create();
273 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
274 soupcall_set(ct->ct_socket, SO_RCV, clnt_vc_soupcall, ct);
275 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
278 ct->ct_record = NULL;
279 ct->ct_record_resid = 0;
281 TAILQ_INIT(&ct->ct_pending);
285 mtx_destroy(&ct->ct_lock);
286 mem_free(ct, sizeof (struct ct_data));
287 mem_free(cl, sizeof (CLIENT));
289 return ((CLIENT *)NULL);
292 static enum clnt_stat
294 CLIENT *cl, /* client handle */
295 struct rpc_callextra *ext, /* call metadata */
296 rpcproc_t proc, /* procedure number */
297 struct mbuf *args, /* pointer to args */
298 struct mbuf **resultsp, /* pointer to results */
299 struct timeval utimeout)
301 struct ct_data *ct = (struct ct_data *) cl->cl_private;
303 struct rpc_err *errp;
306 struct rpc_msg reply_msg;
308 int nrefreshes = 2; /* number of times to refresh cred */
309 struct timeval timeout;
311 struct mbuf *mreq = NULL, *results;
312 struct ct_request *cr;
313 int error, maxextsiz, trycnt;
318 cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
320 mtx_lock(&ct->ct_lock);
322 if (ct->ct_closing || ct->ct_closed) {
323 mtx_unlock(&ct->ct_lock);
325 return (RPC_CANTSEND);
334 errp = &ct->ct_error;
340 if (ct->ct_wait.tv_usec == -1) {
341 timeout = utimeout; /* use supplied timeout */
343 timeout = ct->ct_wait; /* use default timeout */
347 * After 15sec of looping, allow it to return RPC_CANTSEND, which will
348 * cause the clnt_reconnect layer to create a new TCP connection.
352 mtx_assert(&ct->ct_lock, MA_OWNED);
353 if (ct->ct_closing || ct->ct_closed) {
356 mtx_unlock(&ct->ct_lock);
358 return (RPC_CANTSEND);
364 mtx_unlock(&ct->ct_lock);
367 * Leave space to pre-pend the record mark.
369 mreq = m_gethdr(M_WAITOK, MT_DATA);
370 mreq->m_data += sizeof(uint32_t);
371 KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN,
372 ("RPC header too big"));
373 bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos);
374 mreq->m_len = ct->ct_mpos;
377 * The XID is the first thing in the request.
379 *mtod(mreq, uint32_t *) = htonl(xid);
381 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
383 errp->re_status = stat = RPC_SUCCESS;
385 if ((! XDR_PUTINT32(&xdrs, &proc)) ||
386 (! AUTH_MARSHALL(auth, xid, &xdrs,
387 m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
388 errp->re_status = stat = RPC_CANTENCODEARGS;
389 mtx_lock(&ct->ct_lock);
392 mreq->m_pkthdr.len = m_length(mreq, NULL);
395 * Prepend a record marker containing the packet length.
397 M_PREPEND(mreq, sizeof(uint32_t), M_WAITOK);
398 *mtod(mreq, uint32_t *) =
399 htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t)));
402 mtx_lock(&ct->ct_lock);
404 * Check to see if the other end has already started to close down
405 * the connection. The upcall will have set ct_error.re_status
406 * to RPC_CANTRECV if this is the case.
407 * If the other end starts to close down the connection after this
408 * point, it will be detected later when cr_error is checked,
409 * since the request is in the ct_pending queue.
411 if (ct->ct_error.re_status == RPC_CANTRECV) {
412 if (errp != &ct->ct_error) {
413 errp->re_errno = ct->ct_error.re_errno;
414 errp->re_status = RPC_CANTRECV;
420 /* For TLS, wait for an upcall to be done, as required. */
421 while ((ct->ct_rcvstate & (RPCRCVSTATE_NORMAL |
422 RPCRCVSTATE_NONAPPDATA)) == 0)
423 msleep(&ct->ct_rcvstate, &ct->ct_lock, 0, "rpcrcvst", hz);
425 TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link);
426 mtx_unlock(&ct->ct_lock);
428 if (ct->ct_sslrefno != 0) {
430 * Copy the mbuf chain to a chain of ext_pgs mbuf(s)
431 * as required by KERN_TLS.
433 maxextsiz = TLS_MAX_MSG_SIZE_V10_2;
435 if (rpctls_getinfo(&maxlen, false, false))
436 maxextsiz = min(maxextsiz, maxlen);
438 mreq = _rpc_copym_into_ext_pgs(mreq, maxextsiz);
441 * sosend consumes mreq.
443 error = sosend(ct->ct_socket, NULL, NULL, mreq, NULL, 0, curthread);
445 if (error == EMSGSIZE || (error == ERESTART &&
446 (ct->ct_waitflag & PCATCH) == 0 && trycnt-- > 0)) {
447 SOCKBUF_LOCK(&ct->ct_socket->so_snd);
448 sbwait(&ct->ct_socket->so_snd);
449 SOCKBUF_UNLOCK(&ct->ct_socket->so_snd);
450 AUTH_VALIDATE(auth, xid, NULL, NULL);
451 mtx_lock(&ct->ct_lock);
452 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
453 /* Sleep for 1 clock tick before trying the sosend() again. */
454 mtx_unlock(&ct->ct_lock);
455 pause("rpclpsnd", 1);
456 mtx_lock(&ct->ct_lock);
460 reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
461 reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
462 reply_msg.acpted_rply.ar_verf.oa_length = 0;
463 reply_msg.acpted_rply.ar_results.where = NULL;
464 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
466 mtx_lock(&ct->ct_lock);
468 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
469 errp->re_errno = error;
470 errp->re_status = stat = RPC_CANTSEND;
475 * Check to see if we got an upcall while waiting for the
476 * lock. In both these cases, the request has been removed
477 * from ct->ct_pending.
480 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
481 errp->re_errno = cr->cr_error;
482 errp->re_status = stat = RPC_CANTRECV;
486 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
491 * Hack to provide rpc-based message passing
493 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
494 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
495 errp->re_status = stat = RPC_TIMEDOUT;
499 error = msleep(cr, &ct->ct_lock, ct->ct_waitflag, ct->ct_waitchan,
502 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
506 * The sleep returned an error so our request is still
507 * on the list. Turn the error code into an
508 * appropriate client status.
510 errp->re_errno = error;
521 errp->re_status = stat;
525 * We were woken up by the upcall. If the
526 * upcall had a receive error, report that,
527 * otherwise we have a reply.
530 errp->re_errno = cr->cr_error;
531 errp->re_status = stat = RPC_CANTRECV;
538 * Now decode and validate the response. We need to drop the
539 * lock since xdr_replymsg may end up sleeping in malloc.
541 mtx_unlock(&ct->ct_lock);
543 if (ext && ext->rc_feedback)
544 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
546 xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
547 ok = xdr_replymsg(&xdrs, &reply_msg);
551 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
552 (reply_msg.acpted_rply.ar_stat == SUCCESS))
553 errp->re_status = stat = RPC_SUCCESS;
555 stat = _seterr_reply(&reply_msg, errp);
557 if (stat == RPC_SUCCESS) {
558 results = xdrmbuf_getall(&xdrs);
559 if (!AUTH_VALIDATE(auth, xid,
560 &reply_msg.acpted_rply.ar_verf,
562 errp->re_status = stat = RPC_AUTHERROR;
563 errp->re_why = AUTH_INVALIDRESP;
566 ("auth validated but no result"));
569 } /* end successful completion */
571 * If unsuccessful AND error is an authentication error
572 * then refresh credentials and try again, else break
574 else if (stat == RPC_AUTHERROR)
575 /* maybe our credentials need to be refreshed ... */
576 if (nrefreshes > 0 &&
577 AUTH_REFRESH(auth, &reply_msg)) {
580 mtx_lock(&ct->ct_lock);
583 /* end of unsuccessful completion */
584 } /* end of valid reply message */
586 errp->re_status = stat = RPC_CANTDECODERES;
589 mtx_lock(&ct->ct_lock);
591 mtx_assert(&ct->ct_lock, MA_OWNED);
593 KASSERT(stat != RPC_SUCCESS || *resultsp,
594 ("RPC_SUCCESS without reply"));
599 m_freem(cr->cr_mrep);
605 mtx_unlock(&ct->ct_lock);
607 if (auth && stat != RPC_SUCCESS)
608 AUTH_VALIDATE(auth, xid, NULL, NULL);
616 clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
618 struct ct_data *ct = (struct ct_data *) cl->cl_private;
620 *errp = ct->ct_error;
624 clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
629 xdrs.x_op = XDR_FREE;
630 dummy = (*xdr_res)(&xdrs, res_ptr);
637 clnt_vc_abort(CLIENT *cl)
642 clnt_vc_control(CLIENT *cl, u_int request, void *info)
644 struct ct_data *ct = (struct ct_data *)cl->cl_private;
649 static u_int thrdnum = 0;
651 mtx_lock(&ct->ct_lock);
655 ct->ct_closeit = TRUE;
656 mtx_unlock(&ct->ct_lock);
658 case CLSET_FD_NCLOSE:
659 ct->ct_closeit = FALSE;
660 mtx_unlock(&ct->ct_lock);
666 /* for other requests which use info */
668 mtx_unlock(&ct->ct_lock);
673 if (time_not_ok((struct timeval *)info)) {
674 mtx_unlock(&ct->ct_lock);
677 ct->ct_wait = *(struct timeval *)infop;
680 *(struct timeval *)infop = ct->ct_wait;
682 case CLGET_SERVER_ADDR:
683 (void) memcpy(info, &ct->ct_addr, (size_t)ct->ct_addr.ss_len);
687 * Slightly different semantics to userland - we use
688 * sockaddr instead of netbuf.
690 memcpy(info, &ct->ct_addr, ct->ct_addr.ss_len);
692 case CLSET_SVC_ADDR: /* set to new address */
693 mtx_unlock(&ct->ct_lock);
696 *(uint32_t *)info = ct->ct_xid;
699 /* This will set the xid of the NEXT call */
700 /* decrement by 1 as clnt_vc_call() increments once */
701 ct->ct_xid = *(uint32_t *)info - 1;
705 * This RELIES on the information that, in the call body,
706 * the version number field is the fifth field from the
707 * beginning of the RPC header. MUST be changed if the
708 * call_struct is changed
711 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
712 4 * BYTES_PER_XDR_UNIT));
716 *(uint32_t *)(void *)(ct->ct_mcallc +
717 4 * BYTES_PER_XDR_UNIT) =
718 htonl(*(uint32_t *)info);
723 * This RELIES on the information that, in the call body,
724 * the program number field is the fourth field from the
725 * beginning of the RPC header. MUST be changed if the
726 * call_struct is changed
729 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
730 3 * BYTES_PER_XDR_UNIT));
734 *(uint32_t *)(void *)(ct->ct_mcallc +
735 3 * BYTES_PER_XDR_UNIT) =
736 htonl(*(uint32_t *)info);
740 ct->ct_waitchan = (const char *)info;
744 *(const char **) info = ct->ct_waitchan;
747 case CLSET_INTERRUPTIBLE:
749 ct->ct_waitflag = PCATCH;
754 case CLGET_INTERRUPTIBLE:
756 *(int *) info = TRUE;
758 *(int *) info = FALSE;
761 case CLSET_BACKCHANNEL:
762 xprt = (SVCXPRT *)info;
763 if (ct->ct_backchannelxprt == NULL) {
765 if (ct->ct_sslrefno != 0)
766 xprt->xp_tls = RPCTLS_FLAGS_HANDSHAKE;
767 ct->ct_backchannelxprt = xprt;
772 p = (uint64_t *)info;
773 ct->ct_sslsec = *p++;
774 ct->ct_sslusec = *p++;
775 ct->ct_sslrefno = *p;
776 if (ct->ct_sslrefno != RPCTLS_REFNO_HANDSHAKE) {
777 mtx_unlock(&ct->ct_lock);
778 /* Start the kthread that handles upcalls. */
779 error = kthread_add(clnt_vc_dotlsupcall, ct,
780 NULL, NULL, 0, 0, "krpctls%u", thrdnum++);
782 panic("Can't add KRPC thread error %d", error);
784 mtx_unlock(&ct->ct_lock);
789 ct->ct_rcvstate &= ~RPCRCVSTATE_NORMAL;
790 ct->ct_rcvstate |= RPCRCVSTATE_TLSHANDSHAKE;
792 ct->ct_rcvstate &= ~RPCRCVSTATE_TLSHANDSHAKE;
793 ct->ct_rcvstate |= RPCRCVSTATE_NORMAL;
798 mtx_unlock(&ct->ct_lock);
802 mtx_unlock(&ct->ct_lock);
807 clnt_vc_close(CLIENT *cl)
809 struct ct_data *ct = (struct ct_data *) cl->cl_private;
810 struct ct_request *cr;
812 mtx_lock(&ct->ct_lock);
815 mtx_unlock(&ct->ct_lock);
819 if (ct->ct_closing) {
820 while (ct->ct_closing)
821 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
822 KASSERT(ct->ct_closed, ("client should be closed"));
823 mtx_unlock(&ct->ct_lock);
828 ct->ct_closing = TRUE;
829 mtx_unlock(&ct->ct_lock);
831 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
832 if (ct->ct_socket->so_rcv.sb_upcall != NULL) {
833 soupcall_clear(ct->ct_socket, SO_RCV);
834 clnt_vc_upcallsdone(ct);
836 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
839 * Abort any pending requests and wait until everyone
840 * has finished with clnt_vc_call.
842 mtx_lock(&ct->ct_lock);
843 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
845 cr->cr_error = ESHUTDOWN;
849 while (ct->ct_threads)
850 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
853 ct->ct_closing = FALSE;
854 ct->ct_closed = TRUE;
855 wakeup(&ct->ct_sslrefno);
856 mtx_unlock(&ct->ct_lock);
861 clnt_vc_destroy(CLIENT *cl)
863 struct ct_data *ct = (struct ct_data *) cl->cl_private;
864 struct socket *so = NULL;
870 mtx_lock(&ct->ct_lock);
871 xprt = ct->ct_backchannelxprt;
872 ct->ct_backchannelxprt = NULL;
874 mtx_unlock(&ct->ct_lock); /* To avoid a LOR. */
875 sx_xlock(&xprt->xp_lock);
876 mtx_lock(&ct->ct_lock);
878 sx_xunlock(&xprt->xp_lock);
882 if (ct->ct_closeit) {
887 /* Wait for the upcall kthread to terminate. */
888 while ((ct->ct_rcvstate & RPCRCVSTATE_UPCALLTHREAD) != 0)
889 msleep(&ct->ct_sslrefno, &ct->ct_lock, 0,
891 mtx_unlock(&ct->ct_lock);
893 mtx_destroy(&ct->ct_lock);
895 if (ct->ct_sslrefno != 0) {
897 * If the TLS handshake is in progress, the upcall
898 * will fail, but the socket should be closed by the
899 * daemon, since the connect upcall has just failed.
901 if (ct->ct_sslrefno != RPCTLS_REFNO_HANDSHAKE) {
903 * If the upcall fails, the socket has
904 * probably been closed via the rpctlscd
905 * daemon having crashed or been
906 * restarted, so ignore return stat.
908 rpctls_cl_disconnect(ct->ct_sslsec,
909 ct->ct_sslusec, ct->ct_sslrefno,
912 /* Must sorele() to get rid of reference. */
913 CURVNET_SET(so->so_vnet);
918 soshutdown(so, SHUT_WR);
922 m_freem(ct->ct_record);
924 mem_free(ct, sizeof(struct ct_data));
925 if (cl->cl_netid && cl->cl_netid[0])
926 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
927 if (cl->cl_tp && cl->cl_tp[0])
928 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
929 mem_free(cl, sizeof(CLIENT));
933 * Make sure that the time is not garbage. -1 value is disallowed.
934 * Note this is different from time_not_ok in clnt_dg.c
937 time_not_ok(struct timeval *t)
939 return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
940 t->tv_usec <= -1 || t->tv_usec > 1000000);
944 clnt_vc_soupcall(struct socket *so, void *arg, int waitflag)
946 struct ct_data *ct = (struct ct_data *) arg;
949 struct ct_request *cr;
950 int error, rcvflag, foundreq;
951 uint32_t xid_plus_direction[2], header;
955 struct cmsghdr *cmsg;
956 struct tls_get_record tgr;
959 * RPC-over-TLS needs to block reception during
960 * upcalls since the upcall will be doing I/O on
961 * the socket via openssl library calls.
963 mtx_lock(&ct->ct_lock);
964 if ((ct->ct_rcvstate & (RPCRCVSTATE_NORMAL |
965 RPCRCVSTATE_NONAPPDATA)) == 0) {
966 /* Mark that a socket upcall needs to be done. */
967 if ((ct->ct_rcvstate & (RPCRCVSTATE_UPCALLNEEDED |
968 RPCRCVSTATE_UPCALLINPROG)) != 0)
969 ct->ct_rcvstate |= RPCRCVSTATE_SOUPCALLNEEDED;
970 mtx_unlock(&ct->ct_lock);
973 mtx_unlock(&ct->ct_lock);
976 * If another thread is already here, it must be in
977 * soreceive(), so just return to avoid races with it.
978 * ct_upcallrefs is protected by the SOCKBUF_LOCK(),
979 * which is held in this function, except when
980 * soreceive() is called.
982 if (ct->ct_upcallrefs > 0)
987 * Read as much as possible off the socket and link it
991 uio.uio_resid = 1000000000;
992 uio.uio_td = curthread;
994 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
995 if (ct->ct_sslrefno != 0 && (ct->ct_rcvstate &
996 RPCRCVSTATE_NORMAL) != 0)
997 rcvflag |= MSG_TLSAPPDATA;
998 SOCKBUF_UNLOCK(&so->so_rcv);
999 error = soreceive(so, NULL, &uio, &m, &m2, &rcvflag);
1000 SOCKBUF_LOCK(&so->so_rcv);
1002 if (error == EWOULDBLOCK) {
1004 * We must re-test for readability after
1005 * taking the lock to protect us in the case
1006 * where a new packet arrives on the socket
1007 * after our call to soreceive fails with
1011 if (!soreadable(so))
1015 if (error == 0 && m == NULL) {
1017 * We must have got EOF trying
1018 * to read from the stream.
1024 * A return of ENXIO indicates that there is an
1025 * alert record at the head of the
1026 * socket's receive queue, for TLS connections.
1027 * This record needs to be handled in userland
1028 * via an SSL_read() call, so do an upcall to the daemon.
1030 if (ct->ct_sslrefno != 0 && error == ENXIO) {
1031 /* Disable reception, marking an upcall needed. */
1032 mtx_lock(&ct->ct_lock);
1033 ct->ct_rcvstate |= RPCRCVSTATE_UPCALLNEEDED;
1035 * If an upcall in needed, wake up the kthread
1036 * that runs clnt_vc_dotlsupcall().
1038 wakeup(&ct->ct_sslrefno);
1039 mtx_unlock(&ct->ct_lock);
1045 /* Process any record header(s). */
1047 cmsg = mtod(m2, struct cmsghdr *);
1048 if (cmsg->cmsg_type == TLS_GET_RECORD &&
1049 cmsg->cmsg_len == CMSG_LEN(sizeof(tgr))) {
1050 memcpy(&tgr, CMSG_DATA(cmsg), sizeof(tgr));
1052 * TLS_RLTYPE_ALERT records should be handled
1053 * since soreceive() would have returned
1054 * ENXIO. Just throw any other
1055 * non-TLS_RLTYPE_APP records away.
1057 if (tgr.tls_type != TLS_RLTYPE_APP) {
1060 mtx_lock(&ct->ct_lock);
1062 ~RPCRCVSTATE_NONAPPDATA;
1063 ct->ct_rcvstate |= RPCRCVSTATE_NORMAL;
1064 mtx_unlock(&ct->ct_lock);
1071 if (ct->ct_raw != NULL)
1072 m_last(ct->ct_raw)->m_next = m;
1076 rawlen = m_length(ct->ct_raw, NULL);
1078 /* Now, process as much of ct_raw as possible. */
1081 * If ct_record_resid is zero, we are waiting for a
1084 if (ct->ct_record_resid == 0) {
1085 if (rawlen < sizeof(uint32_t))
1087 m_copydata(ct->ct_raw, 0, sizeof(uint32_t),
1089 header = ntohl(header);
1090 ct->ct_record_resid = header & 0x7fffffff;
1091 ct->ct_record_eor = ((header & 0x80000000) != 0);
1092 m_adj(ct->ct_raw, sizeof(uint32_t));
1093 rawlen -= sizeof(uint32_t);
1096 * Move as much of the record as possible to
1101 if (rawlen <= ct->ct_record_resid) {
1102 if (ct->ct_record != NULL)
1103 m_last(ct->ct_record)->m_next =
1106 ct->ct_record = ct->ct_raw;
1108 ct->ct_record_resid -= rawlen;
1111 m = m_split(ct->ct_raw, ct->ct_record_resid,
1115 if (ct->ct_record != NULL)
1116 m_last(ct->ct_record)->m_next =
1119 ct->ct_record = ct->ct_raw;
1120 rawlen -= ct->ct_record_resid;
1121 ct->ct_record_resid = 0;
1124 if (ct->ct_record_resid > 0)
1128 * If we have the entire record, see if we can
1129 * match it to a request.
1131 if (ct->ct_record_eor) {
1133 * The XID is in the first uint32_t of
1134 * the reply and the message direction
1135 * is the second one.
1137 if (ct->ct_record->m_len <
1138 sizeof(xid_plus_direction) &&
1139 m_length(ct->ct_record, NULL) <
1140 sizeof(xid_plus_direction)) {
1143 * The data in the TCP stream is
1144 * corrupted such that there is no
1145 * valid RPC message to parse.
1146 * I think it best to close this
1147 * connection and allow
1148 * clnt_reconnect_call() to try
1149 * and establish a new one.
1151 printf("clnt_vc_soupcall: "
1152 "connection data corrupted\n");
1156 m_copydata(ct->ct_record, 0,
1157 sizeof(xid_plus_direction),
1158 (char *)xid_plus_direction);
1159 xid_plus_direction[0] =
1160 ntohl(xid_plus_direction[0]);
1161 xid_plus_direction[1] =
1162 ntohl(xid_plus_direction[1]);
1163 /* Check message direction. */
1164 if (xid_plus_direction[1] == CALL) {
1165 /* This is a backchannel request. */
1166 mtx_lock(&ct->ct_lock);
1167 xprt = ct->ct_backchannelxprt;
1169 mtx_unlock(&ct->ct_lock);
1170 /* Just throw it away. */
1171 m_freem(ct->ct_record);
1172 ct->ct_record = NULL;
1174 cd = (struct cf_conn *)
1178 * The requests are chained
1179 * in the m_nextpkt list.
1181 while (m2 != NULL &&
1182 m2->m_nextpkt != NULL)
1183 /* Find end of list. */
1191 ct->ct_record->m_nextpkt =
1193 ct->ct_record = NULL;
1195 mtx_unlock(&ct->ct_lock);
1198 mtx_lock(&ct->ct_lock);
1200 TAILQ_FOREACH(cr, &ct->ct_pending,
1203 xid_plus_direction[0]) {
1209 * the XID to zero so
1210 * that we will ignore
1223 mtx_unlock(&ct->ct_lock);
1226 m_freem(ct->ct_record);
1227 ct->ct_record = NULL;
1236 * This socket is broken, so mark that it cannot
1237 * receive and fail all RPCs waiting for a reply
1238 * on it, so that they will be retried on a new
1239 * TCP connection created by clnt_reconnect_X().
1241 mtx_lock(&ct->ct_lock);
1242 ct->ct_error.re_status = RPC_CANTRECV;
1243 ct->ct_error.re_errno = error;
1244 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
1245 cr->cr_error = error;
1248 mtx_unlock(&ct->ct_lock);
1251 ct->ct_upcallrefs--;
1252 if (ct->ct_upcallrefs < 0)
1253 panic("rpcvc upcall refcnt");
1254 if (ct->ct_upcallrefs == 0)
1255 wakeup(&ct->ct_upcallrefs);
1260 * Wait for all upcalls in progress to complete.
1263 clnt_vc_upcallsdone(struct ct_data *ct)
1266 SOCKBUF_LOCK_ASSERT(&ct->ct_socket->so_rcv);
1268 while (ct->ct_upcallrefs > 0)
1269 (void) msleep(&ct->ct_upcallrefs,
1270 SOCKBUF_MTX(&ct->ct_socket->so_rcv), 0, "rpcvcup", 0);
1274 * Do a TLS upcall to the rpctlscd daemon, as required.
1275 * This function runs as a kthread.
1278 clnt_vc_dotlsupcall(void *data)
1280 struct ct_data *ct = (struct ct_data *)data;
1284 mtx_lock(&ct->ct_lock);
1285 ct->ct_rcvstate |= RPCRCVSTATE_UPCALLTHREAD;
1286 while (!ct->ct_closed) {
1287 if ((ct->ct_rcvstate & RPCRCVSTATE_UPCALLNEEDED) != 0) {
1288 ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLNEEDED;
1289 ct->ct_rcvstate |= RPCRCVSTATE_UPCALLINPROG;
1290 if (ct->ct_sslrefno != 0 && ct->ct_sslrefno !=
1291 RPCTLS_REFNO_HANDSHAKE) {
1292 mtx_unlock(&ct->ct_lock);
1293 ret = rpctls_cl_handlerecord(ct->ct_sslsec,
1294 ct->ct_sslusec, ct->ct_sslrefno, &reterr);
1295 mtx_lock(&ct->ct_lock);
1297 ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLINPROG;
1298 if (ret == RPC_SUCCESS && reterr == RPCTLSERR_OK)
1299 ct->ct_rcvstate |= RPCRCVSTATE_NORMAL;
1301 ct->ct_rcvstate |= RPCRCVSTATE_NONAPPDATA;
1302 wakeup(&ct->ct_rcvstate);
1304 if ((ct->ct_rcvstate & RPCRCVSTATE_SOUPCALLNEEDED) != 0) {
1305 ct->ct_rcvstate &= ~RPCRCVSTATE_SOUPCALLNEEDED;
1306 mtx_unlock(&ct->ct_lock);
1307 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
1308 clnt_vc_soupcall(ct->ct_socket, ct, M_NOWAIT);
1309 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
1310 mtx_lock(&ct->ct_lock);
1312 msleep(&ct->ct_sslrefno, &ct->ct_lock, 0, "clntvcdu", hz);
1314 ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLTHREAD;
1315 wakeup(&ct->ct_sslrefno);
1316 mtx_unlock(&ct->ct_lock);