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 #include <sys/cdefs.h>
35 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
39 * TCP based RPC supports 'batched calls'.
40 * A sequence of calls may be batched-up in a send buffer. The rpc call
41 * return immediately to the client even though the call was not necessarily
42 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
43 * the rpc timeout value is zero (see clnt.h, rpc).
45 * Clients should NOT casually batch calls that in fact return results; that is,
46 * the server side should be aware that a call is batched and not produce any
47 * return message. Batched calls that produce many result messages can
48 * deadlock (netlock) the client and the server....
50 * Now go hang yourself.
53 #include "opt_kern_tls.h"
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
61 #include <sys/malloc.h>
63 #include <sys/mutex.h>
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
70 #include <sys/syslog.h>
76 #include <netinet/tcp.h>
79 #include <rpc/rpc_com.h>
81 #include <rpc/rpcsec_tls.h>
85 struct cmsgcred cmcred;
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 static void clnt_vc_dotlsupcall(void *data);
100 static const struct clnt_ops clnt_vc_ops = {
101 .cl_call = clnt_vc_call,
102 .cl_abort = clnt_vc_abort,
103 .cl_geterr = clnt_vc_geterr,
104 .cl_freeres = clnt_vc_freeres,
105 .cl_close = clnt_vc_close,
106 .cl_destroy = clnt_vc_destroy,
107 .cl_control = clnt_vc_control
110 static void clnt_vc_upcallsdone(struct ct_data *);
113 * Create a client handle for a connection.
114 * Default options are set, which the user can change using clnt_control()'s.
115 * The rpc/vc package does buffering similar to stdio, so the client
116 * must pick send and receive buffer sizes, 0 => use the default.
117 * NB: fd is copied into a private area.
118 * NB: The rpch->cl_auth is set null authentication. Caller may wish to
119 * set this something more useful.
121 * fd should be an open socket
125 struct socket *so, /* open file descriptor */
126 struct sockaddr *raddr, /* servers address */
127 const rpcprog_t prog, /* program number */
128 const rpcvers_t vers, /* version number */
129 size_t sendsz, /* buffer recv size */
130 size_t recvsz, /* buffer send size */
131 int intrflag) /* interruptible */
133 CLIENT *cl; /* client handle */
134 struct ct_data *ct = NULL; /* client handle */
136 struct rpc_msg call_msg;
137 static uint32_t disrupt;
138 struct __rpc_sockinfo si;
140 int error, interrupted, one = 1, sleep_flag;
144 disrupt = (uint32_t)(long)raddr;
146 cl = (CLIENT *)mem_alloc(sizeof (*cl));
147 ct = (struct ct_data *)mem_alloc(sizeof (*ct));
149 mtx_init(&ct->ct_lock, "ct->ct_lock", NULL, MTX_DEF);
151 ct->ct_closing = FALSE;
152 ct->ct_closed = FALSE;
153 ct->ct_upcallrefs = 0;
154 ct->ct_rcvstate = RPCRCVSTATE_NORMAL;
156 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
157 error = soconnect(so, raddr, curthread);
162 sleep_flag |= PCATCH;
163 while ((so->so_state & SS_ISCONNECTING)
164 && so->so_error == 0) {
165 error = msleep(&so->so_timeo, SOCK_MTX(so),
166 sleep_flag, "connec", 0);
168 if (error == EINTR || error == ERESTART)
174 error = so->so_error;
180 so->so_state &= ~SS_ISCONNECTING;
181 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
182 rpc_createerr.cf_error.re_errno = error;
187 if (!__rpc_socket2sockinfo(so, &si)) {
191 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
192 bzero(&sopt, sizeof(sopt));
193 sopt.sopt_dir = SOPT_SET;
194 sopt.sopt_level = SOL_SOCKET;
195 sopt.sopt_name = SO_KEEPALIVE;
196 sopt.sopt_val = &one;
197 sopt.sopt_valsize = sizeof(one);
201 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
202 bzero(&sopt, sizeof(sopt));
203 sopt.sopt_dir = SOPT_SET;
204 sopt.sopt_level = IPPROTO_TCP;
205 sopt.sopt_name = TCP_NODELAY;
206 sopt.sopt_val = &one;
207 sopt.sopt_valsize = sizeof(one);
211 ct->ct_closeit = FALSE;
214 * Set up private data struct
217 ct->ct_wait.tv_sec = -1;
218 ct->ct_wait.tv_usec = -1;
219 memcpy(&ct->ct_addr, raddr, raddr->sa_len);
222 * Initialize call message
225 ct->ct_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now);
226 call_msg.rm_xid = ct->ct_xid;
227 call_msg.rm_direction = CALL;
228 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
229 call_msg.rm_call.cb_prog = (uint32_t)prog;
230 call_msg.rm_call.cb_vers = (uint32_t)vers;
233 * pre-serialize the static part of the call msg and stash it away
235 xdrmem_create(&xdrs, ct->ct_mcallc, MCALL_MSG_SIZE,
237 if (! xdr_callhdr(&xdrs, &call_msg)) {
238 if (ct->ct_closeit) {
239 soclose(ct->ct_socket);
243 ct->ct_mpos = XDR_GETPOS(&xdrs);
245 ct->ct_waitchan = "rpcrecv";
249 * Create a client handle which uses xdrrec for serialization
250 * and authnone for authentication.
252 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
253 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
254 error = soreserve(ct->ct_socket, sendsz, recvsz);
256 if (ct->ct_closeit) {
257 soclose(ct->ct_socket);
262 cl->cl_ops = &clnt_vc_ops;
264 cl->cl_auth = authnone_create();
266 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
267 soupcall_set(ct->ct_socket, SO_RCV, clnt_vc_soupcall, ct);
268 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
271 ct->ct_record = NULL;
272 ct->ct_record_resid = 0;
274 TAILQ_INIT(&ct->ct_pending);
278 mtx_destroy(&ct->ct_lock);
279 mem_free(ct, sizeof (struct ct_data));
280 mem_free(cl, sizeof (CLIENT));
282 return ((CLIENT *)NULL);
285 static enum clnt_stat
287 CLIENT *cl, /* client handle */
288 struct rpc_callextra *ext, /* call metadata */
289 rpcproc_t proc, /* procedure number */
290 struct mbuf *args, /* pointer to args */
291 struct mbuf **resultsp, /* pointer to results */
292 struct timeval utimeout)
294 struct ct_data *ct = (struct ct_data *) cl->cl_private;
296 struct rpc_err *errp;
299 struct rpc_msg reply_msg;
301 int nrefreshes = 2; /* number of times to refresh cred */
302 struct timeval timeout;
304 struct mbuf *mreq = NULL, *results;
305 struct ct_request *cr;
306 int error, maxextsiz, trycnt;
311 cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
313 mtx_lock(&ct->ct_lock);
315 if (ct->ct_closing || ct->ct_closed) {
316 mtx_unlock(&ct->ct_lock);
318 return (RPC_CANTSEND);
327 errp = &ct->ct_error;
333 if (ct->ct_wait.tv_usec == -1) {
334 timeout = utimeout; /* use supplied timeout */
336 timeout = ct->ct_wait; /* use default timeout */
340 * After 15sec of looping, allow it to return RPC_CANTSEND, which will
341 * cause the clnt_reconnect layer to create a new TCP connection.
345 mtx_assert(&ct->ct_lock, MA_OWNED);
346 if (ct->ct_closing || ct->ct_closed) {
349 mtx_unlock(&ct->ct_lock);
351 return (RPC_CANTSEND);
357 mtx_unlock(&ct->ct_lock);
360 * Leave space to pre-pend the record mark.
362 mreq = m_gethdr(M_WAITOK, MT_DATA);
363 mreq->m_data += sizeof(uint32_t);
364 KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN,
365 ("RPC header too big"));
366 bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos);
367 mreq->m_len = ct->ct_mpos;
370 * The XID is the first thing in the request.
372 *mtod(mreq, uint32_t *) = htonl(xid);
374 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
376 errp->re_status = stat = RPC_SUCCESS;
378 if ((! XDR_PUTINT32(&xdrs, &proc)) ||
379 (! AUTH_MARSHALL(auth, xid, &xdrs,
380 m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
381 errp->re_status = stat = RPC_CANTENCODEARGS;
382 mtx_lock(&ct->ct_lock);
385 mreq->m_pkthdr.len = m_length(mreq, NULL);
388 * Prepend a record marker containing the packet length.
390 M_PREPEND(mreq, sizeof(uint32_t), M_WAITOK);
391 *mtod(mreq, uint32_t *) =
392 htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t)));
395 mtx_lock(&ct->ct_lock);
397 * Check to see if the other end has already started to close down
398 * the connection. The upcall will have set ct_error.re_status
399 * to RPC_CANTRECV if this is the case.
400 * If the other end starts to close down the connection after this
401 * point, it will be detected later when cr_error is checked,
402 * since the request is in the ct_pending queue.
404 if (ct->ct_error.re_status == RPC_CANTRECV) {
405 if (errp != &ct->ct_error) {
406 errp->re_errno = ct->ct_error.re_errno;
407 errp->re_status = RPC_CANTRECV;
413 /* For TLS, wait for an upcall to be done, as required. */
414 while ((ct->ct_rcvstate & (RPCRCVSTATE_NORMAL |
415 RPCRCVSTATE_NONAPPDATA)) == 0)
416 msleep(&ct->ct_rcvstate, &ct->ct_lock, 0, "rpcrcvst", hz);
418 TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link);
419 mtx_unlock(&ct->ct_lock);
421 if (ct->ct_sslrefno != 0) {
423 * Copy the mbuf chain to a chain of ext_pgs mbuf(s)
424 * as required by KERN_TLS.
426 maxextsiz = TLS_MAX_MSG_SIZE_V10_2;
428 if (rpctls_getinfo(&maxlen, false, false))
429 maxextsiz = min(maxextsiz, maxlen);
431 mreq = _rpc_copym_into_ext_pgs(mreq, maxextsiz);
434 * sosend consumes mreq.
436 error = sosend(ct->ct_socket, NULL, NULL, mreq, NULL, 0, curthread);
438 if (error == EMSGSIZE || (error == ERESTART &&
439 (ct->ct_waitflag & PCATCH) == 0 && trycnt-- > 0)) {
440 SOCKBUF_LOCK(&ct->ct_socket->so_snd);
441 sbwait(ct->ct_socket, SO_SND);
442 SOCKBUF_UNLOCK(&ct->ct_socket->so_snd);
443 AUTH_VALIDATE(auth, xid, NULL, NULL);
444 mtx_lock(&ct->ct_lock);
445 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
446 /* Sleep for 1 clock tick before trying the sosend() again. */
447 mtx_unlock(&ct->ct_lock);
448 pause("rpclpsnd", 1);
449 mtx_lock(&ct->ct_lock);
453 reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
454 reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
455 reply_msg.acpted_rply.ar_verf.oa_length = 0;
456 reply_msg.acpted_rply.ar_results.where = NULL;
457 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
459 mtx_lock(&ct->ct_lock);
461 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
462 errp->re_errno = error;
463 errp->re_status = stat = RPC_CANTSEND;
468 * Check to see if we got an upcall while waiting for the
469 * lock. In both these cases, the request has been removed
470 * from ct->ct_pending.
473 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
474 errp->re_errno = cr->cr_error;
475 errp->re_status = stat = RPC_CANTRECV;
479 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
484 * Hack to provide rpc-based message passing
486 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
487 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
488 errp->re_status = stat = RPC_TIMEDOUT;
492 error = msleep(cr, &ct->ct_lock, ct->ct_waitflag, ct->ct_waitchan,
495 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
499 * The sleep returned an error so our request is still
500 * on the list. Turn the error code into an
501 * appropriate client status.
503 errp->re_errno = error;
514 errp->re_status = stat;
518 * We were woken up by the upcall. If the
519 * upcall had a receive error, report that,
520 * otherwise we have a reply.
523 errp->re_errno = cr->cr_error;
524 errp->re_status = stat = RPC_CANTRECV;
531 * Now decode and validate the response. We need to drop the
532 * lock since xdr_replymsg may end up sleeping in malloc.
534 mtx_unlock(&ct->ct_lock);
536 if (ext && ext->rc_feedback)
537 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
539 xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
540 ok = xdr_replymsg(&xdrs, &reply_msg);
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;
548 stat = _seterr_reply(&reply_msg, errp);
550 if (stat == RPC_SUCCESS) {
551 results = xdrmbuf_getall(&xdrs);
552 if (!AUTH_VALIDATE(auth, xid,
553 &reply_msg.acpted_rply.ar_verf,
555 errp->re_status = stat = RPC_AUTHERROR;
556 errp->re_why = AUTH_INVALIDRESP;
559 ("auth validated but no result"));
562 } /* end successful completion */
564 * If unsuccessful AND error is an authentication error
565 * then refresh credentials and try again, else break
567 else if (stat == RPC_AUTHERROR)
568 /* maybe our credentials need to be refreshed ... */
569 if (nrefreshes > 0 &&
570 AUTH_REFRESH(auth, &reply_msg)) {
573 mtx_lock(&ct->ct_lock);
576 /* end of unsuccessful completion */
577 } /* end of valid reply message */
579 errp->re_status = stat = RPC_CANTDECODERES;
582 mtx_lock(&ct->ct_lock);
584 mtx_assert(&ct->ct_lock, MA_OWNED);
586 KASSERT(stat != RPC_SUCCESS || *resultsp,
587 ("RPC_SUCCESS without reply"));
592 m_freem(cr->cr_mrep);
598 mtx_unlock(&ct->ct_lock);
600 if (auth && stat != RPC_SUCCESS)
601 AUTH_VALIDATE(auth, xid, NULL, NULL);
609 clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
611 struct ct_data *ct = (struct ct_data *) cl->cl_private;
613 *errp = ct->ct_error;
617 clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
622 xdrs.x_op = XDR_FREE;
623 dummy = (*xdr_res)(&xdrs, res_ptr);
630 clnt_vc_abort(CLIENT *cl)
635 clnt_vc_control(CLIENT *cl, u_int request, void *info)
637 struct ct_data *ct = (struct ct_data *)cl->cl_private;
642 static u_int thrdnum = 0;
644 mtx_lock(&ct->ct_lock);
648 ct->ct_closeit = TRUE;
649 mtx_unlock(&ct->ct_lock);
651 case CLSET_FD_NCLOSE:
652 ct->ct_closeit = FALSE;
653 mtx_unlock(&ct->ct_lock);
659 /* for other requests which use info */
661 mtx_unlock(&ct->ct_lock);
666 if (time_not_ok((struct timeval *)info)) {
667 mtx_unlock(&ct->ct_lock);
670 ct->ct_wait = *(struct timeval *)infop;
673 *(struct timeval *)infop = ct->ct_wait;
675 case CLGET_SERVER_ADDR:
676 (void) memcpy(info, &ct->ct_addr, (size_t)ct->ct_addr.ss_len);
680 * Slightly different semantics to userland - we use
681 * sockaddr instead of netbuf.
683 memcpy(info, &ct->ct_addr, ct->ct_addr.ss_len);
685 case CLSET_SVC_ADDR: /* set to new address */
686 mtx_unlock(&ct->ct_lock);
689 *(uint32_t *)info = ct->ct_xid;
692 /* This will set the xid of the NEXT call */
693 /* decrement by 1 as clnt_vc_call() increments once */
694 ct->ct_xid = *(uint32_t *)info - 1;
698 * This RELIES on the information that, in the call body,
699 * the version number field is the fifth field from the
700 * beginning of the RPC header. MUST be changed if the
701 * call_struct is changed
704 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
705 4 * BYTES_PER_XDR_UNIT));
709 *(uint32_t *)(void *)(ct->ct_mcallc +
710 4 * BYTES_PER_XDR_UNIT) =
711 htonl(*(uint32_t *)info);
716 * This RELIES on the information that, in the call body,
717 * the program number field is the fourth field from the
718 * beginning of the RPC header. MUST be changed if the
719 * call_struct is changed
722 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
723 3 * BYTES_PER_XDR_UNIT));
727 *(uint32_t *)(void *)(ct->ct_mcallc +
728 3 * BYTES_PER_XDR_UNIT) =
729 htonl(*(uint32_t *)info);
733 ct->ct_waitchan = (const char *)info;
737 *(const char **) info = ct->ct_waitchan;
740 case CLSET_INTERRUPTIBLE:
742 ct->ct_waitflag = PCATCH;
747 case CLGET_INTERRUPTIBLE:
749 *(int *) info = TRUE;
751 *(int *) info = FALSE;
754 case CLSET_BACKCHANNEL:
755 xprt = (SVCXPRT *)info;
756 if (ct->ct_backchannelxprt == NULL) {
758 if (ct->ct_sslrefno != 0)
759 xprt->xp_tls = RPCTLS_FLAGS_HANDSHAKE;
760 ct->ct_backchannelxprt = xprt;
765 p = (uint64_t *)info;
766 ct->ct_sslsec = *p++;
767 ct->ct_sslusec = *p++;
768 ct->ct_sslrefno = *p;
769 if (ct->ct_sslrefno != RPCTLS_REFNO_HANDSHAKE) {
770 mtx_unlock(&ct->ct_lock);
771 /* Start the kthread that handles upcalls. */
772 error = kthread_add(clnt_vc_dotlsupcall, ct,
773 NULL, NULL, 0, 0, "krpctls%u", thrdnum++);
775 panic("Can't add KRPC thread error %d", error);
777 mtx_unlock(&ct->ct_lock);
782 ct->ct_rcvstate &= ~RPCRCVSTATE_NORMAL;
783 ct->ct_rcvstate |= RPCRCVSTATE_TLSHANDSHAKE;
785 ct->ct_rcvstate &= ~RPCRCVSTATE_TLSHANDSHAKE;
786 ct->ct_rcvstate |= RPCRCVSTATE_NORMAL;
791 mtx_unlock(&ct->ct_lock);
795 mtx_unlock(&ct->ct_lock);
800 clnt_vc_close(CLIENT *cl)
802 struct ct_data *ct = (struct ct_data *) cl->cl_private;
803 struct ct_request *cr;
805 mtx_lock(&ct->ct_lock);
808 mtx_unlock(&ct->ct_lock);
812 if (ct->ct_closing) {
813 while (ct->ct_closing)
814 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
815 KASSERT(ct->ct_closed, ("client should be closed"));
816 mtx_unlock(&ct->ct_lock);
821 ct->ct_closing = TRUE;
822 mtx_unlock(&ct->ct_lock);
824 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
825 if (ct->ct_socket->so_rcv.sb_upcall != NULL) {
826 soupcall_clear(ct->ct_socket, SO_RCV);
827 clnt_vc_upcallsdone(ct);
829 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
832 * Abort any pending requests and wait until everyone
833 * has finished with clnt_vc_call.
835 mtx_lock(&ct->ct_lock);
836 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
838 cr->cr_error = ESHUTDOWN;
842 while (ct->ct_threads)
843 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
846 ct->ct_closing = FALSE;
847 ct->ct_closed = TRUE;
848 wakeup(&ct->ct_sslrefno);
849 mtx_unlock(&ct->ct_lock);
854 clnt_vc_destroy(CLIENT *cl)
856 struct ct_data *ct = (struct ct_data *) cl->cl_private;
857 struct socket *so = NULL;
863 mtx_lock(&ct->ct_lock);
864 xprt = ct->ct_backchannelxprt;
865 ct->ct_backchannelxprt = NULL;
867 mtx_unlock(&ct->ct_lock); /* To avoid a LOR. */
868 sx_xlock(&xprt->xp_lock);
869 mtx_lock(&ct->ct_lock);
871 sx_xunlock(&xprt->xp_lock);
875 if (ct->ct_closeit) {
880 /* Wait for the upcall kthread to terminate. */
881 while ((ct->ct_rcvstate & RPCRCVSTATE_UPCALLTHREAD) != 0)
882 msleep(&ct->ct_sslrefno, &ct->ct_lock, 0,
884 mtx_unlock(&ct->ct_lock);
886 mtx_destroy(&ct->ct_lock);
888 if (ct->ct_sslrefno != 0) {
890 * If the TLS handshake is in progress, the upcall
891 * will fail, but the socket should be closed by the
892 * daemon, since the connect upcall has just failed.
894 if (ct->ct_sslrefno != RPCTLS_REFNO_HANDSHAKE) {
896 * If the upcall fails, the socket has
897 * probably been closed via the rpctlscd
898 * daemon having crashed or been
899 * restarted, so ignore return stat.
901 rpctls_cl_disconnect(ct->ct_sslsec,
902 ct->ct_sslusec, ct->ct_sslrefno,
905 /* Must sorele() to get rid of reference. */
906 CURVNET_SET(so->so_vnet);
910 soshutdown(so, SHUT_WR);
914 m_freem(ct->ct_record);
916 mem_free(ct, sizeof(struct ct_data));
917 if (cl->cl_netid && cl->cl_netid[0])
918 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
919 if (cl->cl_tp && cl->cl_tp[0])
920 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
921 mem_free(cl, sizeof(CLIENT));
925 * Make sure that the time is not garbage. -1 value is disallowed.
926 * Note this is different from time_not_ok in clnt_dg.c
929 time_not_ok(struct timeval *t)
931 return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
932 t->tv_usec <= -1 || t->tv_usec > 1000000);
936 clnt_vc_soupcall(struct socket *so, void *arg, int waitflag)
938 struct ct_data *ct = (struct ct_data *) arg;
941 struct ct_request *cr;
942 int error, rcvflag, foundreq;
943 uint32_t xid_plus_direction[2], header;
947 struct cmsghdr *cmsg;
948 struct tls_get_record tgr;
951 * RPC-over-TLS needs to block reception during
952 * upcalls since the upcall will be doing I/O on
953 * the socket via openssl library calls.
955 mtx_lock(&ct->ct_lock);
956 if ((ct->ct_rcvstate & (RPCRCVSTATE_NORMAL |
957 RPCRCVSTATE_NONAPPDATA)) == 0) {
958 /* Mark that a socket upcall needs to be done. */
959 if ((ct->ct_rcvstate & (RPCRCVSTATE_UPCALLNEEDED |
960 RPCRCVSTATE_UPCALLINPROG)) != 0)
961 ct->ct_rcvstate |= RPCRCVSTATE_SOUPCALLNEEDED;
962 mtx_unlock(&ct->ct_lock);
965 mtx_unlock(&ct->ct_lock);
968 * If another thread is already here, it must be in
969 * soreceive(), so just return to avoid races with it.
970 * ct_upcallrefs is protected by the SOCKBUF_LOCK(),
971 * which is held in this function, except when
972 * soreceive() is called.
974 if (ct->ct_upcallrefs > 0)
979 * Read as much as possible off the socket and link it
983 uio.uio_resid = 1000000000;
984 uio.uio_td = curthread;
986 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
987 if (ct->ct_sslrefno != 0 && (ct->ct_rcvstate &
988 RPCRCVSTATE_NORMAL) != 0)
989 rcvflag |= MSG_TLSAPPDATA;
990 SOCKBUF_UNLOCK(&so->so_rcv);
991 error = soreceive(so, NULL, &uio, &m, &m2, &rcvflag);
992 SOCKBUF_LOCK(&so->so_rcv);
994 if (error == EWOULDBLOCK) {
996 * We must re-test for readability after
997 * taking the lock to protect us in the case
998 * where a new packet arrives on the socket
999 * after our call to soreceive fails with
1003 if (!soreadable(so))
1007 if (error == 0 && m == NULL) {
1009 * We must have got EOF trying
1010 * to read from the stream.
1016 * A return of ENXIO indicates that there is an
1017 * alert record at the head of the
1018 * socket's receive queue, for TLS connections.
1019 * This record needs to be handled in userland
1020 * via an SSL_read() call, so do an upcall to the daemon.
1022 if (ct->ct_sslrefno != 0 && error == ENXIO) {
1023 /* Disable reception, marking an upcall needed. */
1024 mtx_lock(&ct->ct_lock);
1025 ct->ct_rcvstate |= RPCRCVSTATE_UPCALLNEEDED;
1027 * If an upcall in needed, wake up the kthread
1028 * that runs clnt_vc_dotlsupcall().
1030 wakeup(&ct->ct_sslrefno);
1031 mtx_unlock(&ct->ct_lock);
1037 /* Process any record header(s). */
1039 cmsg = mtod(m2, struct cmsghdr *);
1040 if (cmsg->cmsg_type == TLS_GET_RECORD &&
1041 cmsg->cmsg_len == CMSG_LEN(sizeof(tgr))) {
1042 memcpy(&tgr, CMSG_DATA(cmsg), sizeof(tgr));
1044 * TLS_RLTYPE_ALERT records should be handled
1045 * since soreceive() would have returned
1046 * ENXIO. Just throw any other
1047 * non-TLS_RLTYPE_APP records away.
1049 if (tgr.tls_type != TLS_RLTYPE_APP) {
1052 mtx_lock(&ct->ct_lock);
1054 ~RPCRCVSTATE_NONAPPDATA;
1055 ct->ct_rcvstate |= RPCRCVSTATE_NORMAL;
1056 mtx_unlock(&ct->ct_lock);
1063 if (ct->ct_raw != NULL)
1064 m_last(ct->ct_raw)->m_next = m;
1068 rawlen = m_length(ct->ct_raw, NULL);
1070 /* Now, process as much of ct_raw as possible. */
1073 * If ct_record_resid is zero, we are waiting for a
1076 if (ct->ct_record_resid == 0) {
1077 if (rawlen < sizeof(uint32_t))
1079 m_copydata(ct->ct_raw, 0, sizeof(uint32_t),
1081 header = ntohl(header);
1082 ct->ct_record_resid = header & 0x7fffffff;
1083 ct->ct_record_eor = ((header & 0x80000000) != 0);
1084 m_adj(ct->ct_raw, sizeof(uint32_t));
1085 rawlen -= sizeof(uint32_t);
1088 * Move as much of the record as possible to
1093 if (rawlen <= ct->ct_record_resid) {
1094 if (ct->ct_record != NULL)
1095 m_last(ct->ct_record)->m_next =
1098 ct->ct_record = ct->ct_raw;
1100 ct->ct_record_resid -= rawlen;
1103 m = m_split(ct->ct_raw, ct->ct_record_resid,
1107 if (ct->ct_record != NULL)
1108 m_last(ct->ct_record)->m_next =
1111 ct->ct_record = ct->ct_raw;
1112 rawlen -= ct->ct_record_resid;
1113 ct->ct_record_resid = 0;
1116 if (ct->ct_record_resid > 0)
1120 * If we have the entire record, see if we can
1121 * match it to a request.
1123 if (ct->ct_record_eor) {
1125 * The XID is in the first uint32_t of
1126 * the reply and the message direction
1127 * is the second one.
1129 if (ct->ct_record->m_len <
1130 sizeof(xid_plus_direction) &&
1131 m_length(ct->ct_record, NULL) <
1132 sizeof(xid_plus_direction)) {
1135 * The data in the TCP stream is
1136 * corrupted such that there is no
1137 * valid RPC message to parse.
1138 * I think it best to close this
1139 * connection and allow
1140 * clnt_reconnect_call() to try
1141 * and establish a new one.
1143 printf("clnt_vc_soupcall: "
1144 "connection data corrupted\n");
1148 m_copydata(ct->ct_record, 0,
1149 sizeof(xid_plus_direction),
1150 (char *)xid_plus_direction);
1151 xid_plus_direction[0] =
1152 ntohl(xid_plus_direction[0]);
1153 xid_plus_direction[1] =
1154 ntohl(xid_plus_direction[1]);
1155 /* Check message direction. */
1156 if (xid_plus_direction[1] == CALL) {
1157 /* This is a backchannel request. */
1158 mtx_lock(&ct->ct_lock);
1159 xprt = ct->ct_backchannelxprt;
1161 mtx_unlock(&ct->ct_lock);
1162 /* Just throw it away. */
1163 m_freem(ct->ct_record);
1164 ct->ct_record = NULL;
1166 cd = (struct cf_conn *)
1170 * The requests are chained
1171 * in the m_nextpkt list.
1173 while (m2 != NULL &&
1174 m2->m_nextpkt != NULL)
1175 /* Find end of list. */
1183 ct->ct_record->m_nextpkt =
1185 ct->ct_record = NULL;
1187 mtx_unlock(&ct->ct_lock);
1190 mtx_lock(&ct->ct_lock);
1192 TAILQ_FOREACH(cr, &ct->ct_pending,
1195 xid_plus_direction[0]) {
1201 * the XID to zero so
1202 * that we will ignore
1215 mtx_unlock(&ct->ct_lock);
1218 m_freem(ct->ct_record);
1219 ct->ct_record = NULL;
1228 * This socket is broken, so mark that it cannot
1229 * receive and fail all RPCs waiting for a reply
1230 * on it, so that they will be retried on a new
1231 * TCP connection created by clnt_reconnect_X().
1233 mtx_lock(&ct->ct_lock);
1234 ct->ct_error.re_status = RPC_CANTRECV;
1235 ct->ct_error.re_errno = error;
1236 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
1237 cr->cr_error = error;
1240 mtx_unlock(&ct->ct_lock);
1243 ct->ct_upcallrefs--;
1244 if (ct->ct_upcallrefs < 0)
1245 panic("rpcvc upcall refcnt");
1246 if (ct->ct_upcallrefs == 0)
1247 wakeup(&ct->ct_upcallrefs);
1252 * Wait for all upcalls in progress to complete.
1255 clnt_vc_upcallsdone(struct ct_data *ct)
1258 SOCKBUF_LOCK_ASSERT(&ct->ct_socket->so_rcv);
1260 while (ct->ct_upcallrefs > 0)
1261 (void) msleep(&ct->ct_upcallrefs,
1262 SOCKBUF_MTX(&ct->ct_socket->so_rcv), 0, "rpcvcup", 0);
1266 * Do a TLS upcall to the rpctlscd daemon, as required.
1267 * This function runs as a kthread.
1270 clnt_vc_dotlsupcall(void *data)
1272 struct ct_data *ct = (struct ct_data *)data;
1276 mtx_lock(&ct->ct_lock);
1277 ct->ct_rcvstate |= RPCRCVSTATE_UPCALLTHREAD;
1278 while (!ct->ct_closed) {
1279 if ((ct->ct_rcvstate & RPCRCVSTATE_UPCALLNEEDED) != 0) {
1280 ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLNEEDED;
1281 ct->ct_rcvstate |= RPCRCVSTATE_UPCALLINPROG;
1282 if (ct->ct_sslrefno != 0 && ct->ct_sslrefno !=
1283 RPCTLS_REFNO_HANDSHAKE) {
1284 mtx_unlock(&ct->ct_lock);
1285 ret = rpctls_cl_handlerecord(ct->ct_sslsec,
1286 ct->ct_sslusec, ct->ct_sslrefno, &reterr);
1287 mtx_lock(&ct->ct_lock);
1289 ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLINPROG;
1290 if (ret == RPC_SUCCESS && reterr == RPCTLSERR_OK)
1291 ct->ct_rcvstate |= RPCRCVSTATE_NORMAL;
1293 ct->ct_rcvstate |= RPCRCVSTATE_NONAPPDATA;
1294 wakeup(&ct->ct_rcvstate);
1296 if ((ct->ct_rcvstate & RPCRCVSTATE_SOUPCALLNEEDED) != 0) {
1297 ct->ct_rcvstate &= ~RPCRCVSTATE_SOUPCALLNEEDED;
1298 mtx_unlock(&ct->ct_lock);
1299 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
1300 clnt_vc_soupcall(ct->ct_socket, ct, M_NOWAIT);
1301 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
1302 mtx_lock(&ct->ct_lock);
1304 msleep(&ct->ct_sslrefno, &ct->ct_lock, 0, "clntvcdu", hz);
1306 ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLTHREAD;
1307 wakeup(&ct->ct_sslrefno);
1308 mtx_unlock(&ct->ct_lock);