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 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 *);
119 static int fake_wchan;
122 * Create a client handle for a connection.
123 * Default options are set, which the user can change using clnt_control()'s.
124 * The rpc/vc package does buffering similar to stdio, so the client
125 * must pick send and receive buffer sizes, 0 => use the default.
126 * NB: fd is copied into a private area.
127 * NB: The rpch->cl_auth is set null authentication. Caller may wish to
128 * set this something more useful.
130 * fd should be an open socket
134 struct socket *so, /* open file descriptor */
135 struct sockaddr *raddr, /* servers address */
136 const rpcprog_t prog, /* program number */
137 const rpcvers_t vers, /* version number */
138 size_t sendsz, /* buffer recv size */
139 size_t recvsz, /* buffer send size */
140 int intrflag) /* interruptible */
142 CLIENT *cl; /* client handle */
143 struct ct_data *ct = NULL; /* client handle */
145 struct rpc_msg call_msg;
146 static uint32_t disrupt;
147 struct __rpc_sockinfo si;
149 int error, interrupted, one = 1, sleep_flag;
153 disrupt = (uint32_t)(long)raddr;
155 cl = (CLIENT *)mem_alloc(sizeof (*cl));
156 ct = (struct ct_data *)mem_alloc(sizeof (*ct));
158 mtx_init(&ct->ct_lock, "ct->ct_lock", NULL, MTX_DEF);
160 ct->ct_closing = FALSE;
161 ct->ct_closed = FALSE;
162 ct->ct_upcallrefs = 0;
163 ct->ct_rcvstate = RPCRCVSTATE_NORMAL;
165 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
166 error = soconnect(so, raddr, curthread);
171 sleep_flag |= PCATCH;
172 while ((so->so_state & SS_ISCONNECTING)
173 && so->so_error == 0) {
174 error = msleep(&so->so_timeo, SOCK_MTX(so),
175 sleep_flag, "connec", 0);
177 if (error == EINTR || error == ERESTART)
183 error = so->so_error;
189 so->so_state &= ~SS_ISCONNECTING;
190 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
191 rpc_createerr.cf_error.re_errno = error;
196 if (!__rpc_socket2sockinfo(so, &si)) {
200 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
201 bzero(&sopt, sizeof(sopt));
202 sopt.sopt_dir = SOPT_SET;
203 sopt.sopt_level = SOL_SOCKET;
204 sopt.sopt_name = SO_KEEPALIVE;
205 sopt.sopt_val = &one;
206 sopt.sopt_valsize = sizeof(one);
210 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
211 bzero(&sopt, sizeof(sopt));
212 sopt.sopt_dir = SOPT_SET;
213 sopt.sopt_level = IPPROTO_TCP;
214 sopt.sopt_name = TCP_NODELAY;
215 sopt.sopt_val = &one;
216 sopt.sopt_valsize = sizeof(one);
220 ct->ct_closeit = FALSE;
223 * Set up private data struct
226 ct->ct_wait.tv_sec = -1;
227 ct->ct_wait.tv_usec = -1;
228 memcpy(&ct->ct_addr, raddr, raddr->sa_len);
231 * Initialize call message
234 ct->ct_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now);
235 call_msg.rm_xid = ct->ct_xid;
236 call_msg.rm_direction = CALL;
237 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
238 call_msg.rm_call.cb_prog = (uint32_t)prog;
239 call_msg.rm_call.cb_vers = (uint32_t)vers;
242 * pre-serialize the static part of the call msg and stash it away
244 xdrmem_create(&xdrs, ct->ct_mcallc, MCALL_MSG_SIZE,
246 if (! xdr_callhdr(&xdrs, &call_msg)) {
247 if (ct->ct_closeit) {
248 soclose(ct->ct_socket);
252 ct->ct_mpos = XDR_GETPOS(&xdrs);
254 ct->ct_waitchan = "rpcrecv";
258 * Create a client handle which uses xdrrec for serialization
259 * and authnone for authentication.
261 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
262 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
263 error = soreserve(ct->ct_socket, sendsz, recvsz);
265 if (ct->ct_closeit) {
266 soclose(ct->ct_socket);
271 cl->cl_ops = &clnt_vc_ops;
273 cl->cl_auth = authnone_create();
275 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
276 soupcall_set(ct->ct_socket, SO_RCV, clnt_vc_soupcall, ct);
277 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
280 ct->ct_record = NULL;
281 ct->ct_record_resid = 0;
283 TAILQ_INIT(&ct->ct_pending);
287 mtx_destroy(&ct->ct_lock);
288 mem_free(ct, sizeof (struct ct_data));
289 mem_free(cl, sizeof (CLIENT));
291 return ((CLIENT *)NULL);
294 static enum clnt_stat
296 CLIENT *cl, /* client handle */
297 struct rpc_callextra *ext, /* call metadata */
298 rpcproc_t proc, /* procedure number */
299 struct mbuf *args, /* pointer to args */
300 struct mbuf **resultsp, /* pointer to results */
301 struct timeval utimeout)
303 struct ct_data *ct = (struct ct_data *) cl->cl_private;
305 struct rpc_err *errp;
308 struct rpc_msg reply_msg;
310 int nrefreshes = 2; /* number of times to refresh cred */
311 struct timeval timeout;
313 struct mbuf *mreq = NULL, *results;
314 struct ct_request *cr;
315 int error, maxextsiz, trycnt;
320 cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
322 mtx_lock(&ct->ct_lock);
324 if (ct->ct_closing || ct->ct_closed) {
325 mtx_unlock(&ct->ct_lock);
327 return (RPC_CANTSEND);
336 errp = &ct->ct_error;
342 if (ct->ct_wait.tv_usec == -1) {
343 timeout = utimeout; /* use supplied timeout */
345 timeout = ct->ct_wait; /* use default timeout */
349 * After 15sec of looping, allow it to return RPC_CANTSEND, which will
350 * cause the clnt_reconnect layer to create a new TCP connection.
354 mtx_assert(&ct->ct_lock, MA_OWNED);
355 if (ct->ct_closing || ct->ct_closed) {
358 mtx_unlock(&ct->ct_lock);
360 return (RPC_CANTSEND);
366 mtx_unlock(&ct->ct_lock);
369 * Leave space to pre-pend the record mark.
371 mreq = m_gethdr(M_WAITOK, MT_DATA);
372 mreq->m_data += sizeof(uint32_t);
373 KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN,
374 ("RPC header too big"));
375 bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos);
376 mreq->m_len = ct->ct_mpos;
379 * The XID is the first thing in the request.
381 *mtod(mreq, uint32_t *) = htonl(xid);
383 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
385 errp->re_status = stat = RPC_SUCCESS;
387 if ((! XDR_PUTINT32(&xdrs, &proc)) ||
388 (! AUTH_MARSHALL(auth, xid, &xdrs,
389 m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
390 errp->re_status = stat = RPC_CANTENCODEARGS;
391 mtx_lock(&ct->ct_lock);
394 mreq->m_pkthdr.len = m_length(mreq, NULL);
397 * Prepend a record marker containing the packet length.
399 M_PREPEND(mreq, sizeof(uint32_t), M_WAITOK);
400 *mtod(mreq, uint32_t *) =
401 htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t)));
404 mtx_lock(&ct->ct_lock);
406 * Check to see if the other end has already started to close down
407 * the connection. The upcall will have set ct_error.re_status
408 * to RPC_CANTRECV if this is the case.
409 * If the other end starts to close down the connection after this
410 * point, it will be detected later when cr_error is checked,
411 * since the request is in the ct_pending queue.
413 if (ct->ct_error.re_status == RPC_CANTRECV) {
414 if (errp != &ct->ct_error) {
415 errp->re_errno = ct->ct_error.re_errno;
416 errp->re_status = RPC_CANTRECV;
422 /* For TLS, wait for an upcall to be done, as required. */
423 while ((ct->ct_rcvstate & (RPCRCVSTATE_NORMAL |
424 RPCRCVSTATE_NONAPPDATA)) == 0)
425 msleep(&ct->ct_rcvstate, &ct->ct_lock, 0, "rpcrcvst", hz);
427 TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link);
428 mtx_unlock(&ct->ct_lock);
430 if (ct->ct_sslrefno != 0) {
432 * Copy the mbuf chain to a chain of ext_pgs mbuf(s)
433 * as required by KERN_TLS.
435 maxextsiz = TLS_MAX_MSG_SIZE_V10_2;
437 if (rpctls_getinfo(&maxlen, false, false))
438 maxextsiz = min(maxextsiz, maxlen);
440 mreq = _rpc_copym_into_ext_pgs(mreq, maxextsiz);
443 * sosend consumes mreq.
445 error = sosend(ct->ct_socket, NULL, NULL, mreq, NULL, 0, curthread);
447 if (error == EMSGSIZE || (error == ERESTART &&
448 (ct->ct_waitflag & PCATCH) == 0 && trycnt-- > 0)) {
449 SOCKBUF_LOCK(&ct->ct_socket->so_snd);
450 sbwait(&ct->ct_socket->so_snd);
451 SOCKBUF_UNLOCK(&ct->ct_socket->so_snd);
452 AUTH_VALIDATE(auth, xid, NULL, NULL);
453 mtx_lock(&ct->ct_lock);
454 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
455 /* Sleep for 1 clock tick before trying the sosend() again. */
456 msleep(&fake_wchan, &ct->ct_lock, 0, "rpclpsnd", 1);
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;
871 mtx_lock(&ct->ct_lock);
872 xprt = ct->ct_backchannelxprt;
873 ct->ct_backchannelxprt = NULL;
875 mtx_unlock(&ct->ct_lock); /* To avoid a LOR. */
876 sx_xlock(&xprt->xp_lock);
877 mtx_lock(&ct->ct_lock);
879 sx_xunlock(&xprt->xp_lock);
883 if (ct->ct_closeit) {
888 /* Wait for the upcall kthread to terminate. */
889 while ((ct->ct_rcvstate & RPCRCVSTATE_UPCALLTHREAD) != 0)
890 msleep(&ct->ct_sslrefno, &ct->ct_lock, 0,
892 mtx_unlock(&ct->ct_lock);
894 mtx_destroy(&ct->ct_lock);
896 if (ct->ct_sslrefno != 0) {
898 * If the TLS handshake is in progress, the upcall
899 * will fail, but the socket should be closed by the
900 * daemon, since the connect upcall has just failed.
902 if (ct->ct_sslrefno != RPCTLS_REFNO_HANDSHAKE) {
904 * If the upcall fails, the socket has
905 * probably been closed via the rpctlscd
906 * daemon having crashed or been
907 * restarted, so ignore return stat.
909 stat = rpctls_cl_disconnect(ct->ct_sslsec,
910 ct->ct_sslusec, ct->ct_sslrefno,
913 /* Must sorele() to get rid of reference. */
914 CURVNET_SET(so->so_vnet);
919 soshutdown(so, SHUT_WR);
923 m_freem(ct->ct_record);
925 mem_free(ct, sizeof(struct ct_data));
926 if (cl->cl_netid && cl->cl_netid[0])
927 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
928 if (cl->cl_tp && cl->cl_tp[0])
929 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
930 mem_free(cl, sizeof(CLIENT));
934 * Make sure that the time is not garbage. -1 value is disallowed.
935 * Note this is different from time_not_ok in clnt_dg.c
938 time_not_ok(struct timeval *t)
940 return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
941 t->tv_usec <= -1 || t->tv_usec > 1000000);
945 clnt_vc_soupcall(struct socket *so, void *arg, int waitflag)
947 struct ct_data *ct = (struct ct_data *) arg;
949 struct mbuf *m, *m2, **ctrlp;
950 struct ct_request *cr;
951 int error, rcvflag, foundreq;
952 uint32_t xid_plus_direction[2], header;
956 struct cmsghdr *cmsg;
957 struct tls_get_record tgr;
960 * RPC-over-TLS needs to block reception during
961 * upcalls since the upcall will be doing I/O on
962 * the socket via openssl library calls.
964 mtx_lock(&ct->ct_lock);
965 if ((ct->ct_rcvstate & (RPCRCVSTATE_NORMAL |
966 RPCRCVSTATE_NONAPPDATA)) == 0) {
967 /* Mark that a socket upcall needs to be done. */
968 if ((ct->ct_rcvstate & (RPCRCVSTATE_UPCALLNEEDED |
969 RPCRCVSTATE_UPCALLINPROG)) != 0)
970 ct->ct_rcvstate |= RPCRCVSTATE_SOUPCALLNEEDED;
971 mtx_unlock(&ct->ct_lock);
974 mtx_unlock(&ct->ct_lock);
977 * If another thread is already here, it must be in
978 * soreceive(), so just return to avoid races with it.
979 * ct_upcallrefs is protected by the SOCKBUF_LOCK(),
980 * which is held in this function, except when
981 * soreceive() is called.
983 if (ct->ct_upcallrefs > 0)
988 * Read as much as possible off the socket and link it
992 uio.uio_resid = 1000000000;
993 uio.uio_td = curthread;
995 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
996 if (ct->ct_sslrefno != 0 && (ct->ct_rcvstate &
997 RPCRCVSTATE_NORMAL) != 0) {
998 rcvflag |= MSG_TLSAPPDATA;
1002 SOCKBUF_UNLOCK(&so->so_rcv);
1003 error = soreceive(so, NULL, &uio, &m, ctrlp, &rcvflag);
1004 SOCKBUF_LOCK(&so->so_rcv);
1006 if (error == EWOULDBLOCK) {
1008 * We must re-test for readability after
1009 * taking the lock to protect us in the case
1010 * where a new packet arrives on the socket
1011 * after our call to soreceive fails with
1015 if (!soreadable(so))
1019 if (error == 0 && m == NULL) {
1021 * We must have got EOF trying
1022 * to read from the stream.
1028 * A return of ENXIO indicates that there is a
1029 * non-application data record at the head of the
1030 * socket's receive queue, for TLS connections.
1031 * This record needs to be handled in userland
1032 * via an SSL_read() call, so do an upcall to the daemon.
1034 if (ct->ct_sslrefno != 0 && error == ENXIO) {
1035 /* Disable reception, marking an upcall needed. */
1036 mtx_lock(&ct->ct_lock);
1037 ct->ct_rcvstate |= RPCRCVSTATE_UPCALLNEEDED;
1039 * If an upcall in needed, wake up the kthread
1040 * that runs clnt_vc_dotlsupcall().
1042 wakeup(&ct->ct_sslrefno);
1043 mtx_unlock(&ct->ct_lock);
1049 /* Process any record header(s). */
1051 cmsg = mtod(m2, struct cmsghdr *);
1052 if (cmsg->cmsg_type == TLS_GET_RECORD &&
1053 cmsg->cmsg_len == CMSG_LEN(sizeof(tgr))) {
1054 memcpy(&tgr, CMSG_DATA(cmsg), sizeof(tgr));
1056 * This should have been handled by
1057 * setting RPCRCVSTATE_UPCALLNEEDED in
1058 * ct_rcvstate but if not, all we can do
1061 if (tgr.tls_type != TLS_RLTYPE_APP) {
1064 mtx_lock(&ct->ct_lock);
1066 ~RPCRCVSTATE_NONAPPDATA;
1067 ct->ct_rcvstate |= RPCRCVSTATE_NORMAL;
1068 mtx_unlock(&ct->ct_lock);
1075 if (ct->ct_raw != NULL)
1076 m_last(ct->ct_raw)->m_next = m;
1080 rawlen = m_length(ct->ct_raw, NULL);
1082 /* Now, process as much of ct_raw as possible. */
1085 * If ct_record_resid is zero, we are waiting for a
1088 if (ct->ct_record_resid == 0) {
1089 if (rawlen < sizeof(uint32_t))
1091 m_copydata(ct->ct_raw, 0, sizeof(uint32_t),
1093 header = ntohl(header);
1094 ct->ct_record_resid = header & 0x7fffffff;
1095 ct->ct_record_eor = ((header & 0x80000000) != 0);
1096 m_adj(ct->ct_raw, sizeof(uint32_t));
1097 rawlen -= sizeof(uint32_t);
1100 * Move as much of the record as possible to
1105 if (rawlen <= ct->ct_record_resid) {
1106 if (ct->ct_record != NULL)
1107 m_last(ct->ct_record)->m_next =
1110 ct->ct_record = ct->ct_raw;
1112 ct->ct_record_resid -= rawlen;
1115 m = m_split(ct->ct_raw, ct->ct_record_resid,
1119 if (ct->ct_record != NULL)
1120 m_last(ct->ct_record)->m_next =
1123 ct->ct_record = ct->ct_raw;
1124 rawlen -= ct->ct_record_resid;
1125 ct->ct_record_resid = 0;
1128 if (ct->ct_record_resid > 0)
1132 * If we have the entire record, see if we can
1133 * match it to a request.
1135 if (ct->ct_record_eor) {
1137 * The XID is in the first uint32_t of
1138 * the reply and the message direction
1139 * is the second one.
1141 if (ct->ct_record->m_len <
1142 sizeof(xid_plus_direction) &&
1143 m_length(ct->ct_record, NULL) <
1144 sizeof(xid_plus_direction)) {
1147 * The data in the TCP stream is
1148 * corrupted such that there is no
1149 * valid RPC message to parse.
1150 * I think it best to close this
1151 * connection and allow
1152 * clnt_reconnect_call() to try
1153 * and establish a new one.
1155 printf("clnt_vc_soupcall: "
1156 "connection data corrupted\n");
1160 m_copydata(ct->ct_record, 0,
1161 sizeof(xid_plus_direction),
1162 (char *)xid_plus_direction);
1163 xid_plus_direction[0] =
1164 ntohl(xid_plus_direction[0]);
1165 xid_plus_direction[1] =
1166 ntohl(xid_plus_direction[1]);
1167 /* Check message direction. */
1168 if (xid_plus_direction[1] == CALL) {
1169 /* This is a backchannel request. */
1170 mtx_lock(&ct->ct_lock);
1171 xprt = ct->ct_backchannelxprt;
1173 mtx_unlock(&ct->ct_lock);
1174 /* Just throw it away. */
1175 m_freem(ct->ct_record);
1176 ct->ct_record = NULL;
1178 cd = (struct cf_conn *)
1182 * The requests are chained
1183 * in the m_nextpkt list.
1185 while (m2 != NULL &&
1186 m2->m_nextpkt != NULL)
1187 /* Find end of list. */
1195 ct->ct_record->m_nextpkt =
1197 ct->ct_record = NULL;
1199 mtx_unlock(&ct->ct_lock);
1202 mtx_lock(&ct->ct_lock);
1204 TAILQ_FOREACH(cr, &ct->ct_pending,
1207 xid_plus_direction[0]) {
1213 * the XID to zero so
1214 * that we will ignore
1227 mtx_unlock(&ct->ct_lock);
1230 m_freem(ct->ct_record);
1231 ct->ct_record = NULL;
1240 * This socket is broken, so mark that it cannot
1241 * receive and fail all RPCs waiting for a reply
1242 * on it, so that they will be retried on a new
1243 * TCP connection created by clnt_reconnect_X().
1245 mtx_lock(&ct->ct_lock);
1246 ct->ct_error.re_status = RPC_CANTRECV;
1247 ct->ct_error.re_errno = error;
1248 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
1249 cr->cr_error = error;
1252 mtx_unlock(&ct->ct_lock);
1255 ct->ct_upcallrefs--;
1256 if (ct->ct_upcallrefs < 0)
1257 panic("rpcvc upcall refcnt");
1258 if (ct->ct_upcallrefs == 0)
1259 wakeup(&ct->ct_upcallrefs);
1264 * Wait for all upcalls in progress to complete.
1267 clnt_vc_upcallsdone(struct ct_data *ct)
1270 SOCKBUF_LOCK_ASSERT(&ct->ct_socket->so_rcv);
1272 while (ct->ct_upcallrefs > 0)
1273 (void) msleep(&ct->ct_upcallrefs,
1274 SOCKBUF_MTX(&ct->ct_socket->so_rcv), 0, "rpcvcup", 0);
1278 * Do a TLS upcall to the rpctlscd daemon, as required.
1279 * This function runs as a kthread.
1282 clnt_vc_dotlsupcall(void *data)
1284 struct ct_data *ct = (struct ct_data *)data;
1288 mtx_lock(&ct->ct_lock);
1289 ct->ct_rcvstate |= RPCRCVSTATE_UPCALLTHREAD;
1290 while (!ct->ct_closed) {
1291 if ((ct->ct_rcvstate & RPCRCVSTATE_UPCALLNEEDED) != 0) {
1292 ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLNEEDED;
1293 ct->ct_rcvstate |= RPCRCVSTATE_UPCALLINPROG;
1294 if (ct->ct_sslrefno != 0 && ct->ct_sslrefno !=
1295 RPCTLS_REFNO_HANDSHAKE) {
1296 mtx_unlock(&ct->ct_lock);
1297 ret = rpctls_cl_handlerecord(ct->ct_sslsec,
1298 ct->ct_sslusec, ct->ct_sslrefno, &reterr);
1299 mtx_lock(&ct->ct_lock);
1301 ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLINPROG;
1302 if (ret == RPC_SUCCESS && reterr == RPCTLSERR_OK)
1303 ct->ct_rcvstate |= RPCRCVSTATE_NORMAL;
1305 ct->ct_rcvstate |= RPCRCVSTATE_NONAPPDATA;
1306 wakeup(&ct->ct_rcvstate);
1308 if ((ct->ct_rcvstate & RPCRCVSTATE_SOUPCALLNEEDED) != 0) {
1309 ct->ct_rcvstate &= ~RPCRCVSTATE_SOUPCALLNEEDED;
1310 mtx_unlock(&ct->ct_lock);
1311 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
1312 clnt_vc_soupcall(ct->ct_socket, ct, M_NOWAIT);
1313 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
1314 mtx_lock(&ct->ct_lock);
1316 msleep(&ct->ct_sslrefno, &ct->ct_lock, 0, "clntvcdu", hz);
1318 ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLTHREAD;
1319 wakeup(&ct->ct_sslrefno);
1320 mtx_unlock(&ct->ct_lock);