1dfdcada3SDoug Rabson /* $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $ */
2dfdcada3SDoug Rabson
32e322d37SHiroki Sato /*-
451369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
551369649SPedro F. Giffuni *
62e322d37SHiroki Sato * Copyright (c) 2009, Sun Microsystems, Inc.
72e322d37SHiroki Sato * All rights reserved.
8dfdcada3SDoug Rabson *
92e322d37SHiroki Sato * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato * this list of conditions and the following disclaimer.
132e322d37SHiroki Sato * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato * this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato * and/or other materials provided with the distribution.
162e322d37SHiroki Sato * - Neither the name of Sun Microsystems, Inc. nor the names of its
172e322d37SHiroki Sato * contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato * from this software without specific prior written permission.
19dfdcada3SDoug Rabson *
202e322d37SHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato * POSSIBILITY OF SUCH DAMAGE.
31dfdcada3SDoug Rabson */
32dfdcada3SDoug Rabson
33dfdcada3SDoug Rabson #include <sys/cdefs.h>
34dfdcada3SDoug Rabson /*
35dfdcada3SDoug Rabson * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
36dfdcada3SDoug Rabson *
37dfdcada3SDoug Rabson * Copyright (C) 1984, Sun Microsystems, Inc.
38dfdcada3SDoug Rabson *
39dfdcada3SDoug Rabson * TCP based RPC supports 'batched calls'.
40dfdcada3SDoug Rabson * A sequence of calls may be batched-up in a send buffer. The rpc call
41dfdcada3SDoug Rabson * return immediately to the client even though the call was not necessarily
42dfdcada3SDoug Rabson * sent. The batching occurs if the results' xdr routine is NULL (0) AND
43dfdcada3SDoug Rabson * the rpc timeout value is zero (see clnt.h, rpc).
44dfdcada3SDoug Rabson *
45dfdcada3SDoug Rabson * Clients should NOT casually batch calls that in fact return results; that is,
46dfdcada3SDoug Rabson * the server side should be aware that a call is batched and not produce any
47dfdcada3SDoug Rabson * return message. Batched calls that produce many result messages can
48dfdcada3SDoug Rabson * deadlock (netlock) the client and the server....
49dfdcada3SDoug Rabson *
50dfdcada3SDoug Rabson * Now go hang yourself.
51dfdcada3SDoug Rabson */
52dfdcada3SDoug Rabson
53ab0c29afSRick Macklem #include "opt_kern_tls.h"
54ab0c29afSRick Macklem
55dfdcada3SDoug Rabson #include <sys/param.h>
56dfdcada3SDoug Rabson #include <sys/systm.h>
57dfd174d6SRick Macklem #include <sys/kernel.h>
58ab0c29afSRick Macklem #include <sys/kthread.h>
59ab0c29afSRick Macklem #include <sys/ktls.h>
60dfdcada3SDoug Rabson #include <sys/lock.h>
61dfdcada3SDoug Rabson #include <sys/malloc.h>
62dfdcada3SDoug Rabson #include <sys/mbuf.h>
63dfdcada3SDoug Rabson #include <sys/mutex.h>
64dfdcada3SDoug Rabson #include <sys/pcpu.h>
65dfdcada3SDoug Rabson #include <sys/proc.h>
66a9148abdSDoug Rabson #include <sys/protosw.h>
67dfdcada3SDoug Rabson #include <sys/socket.h>
68dfdcada3SDoug Rabson #include <sys/socketvar.h>
69e2adc47dSRick Macklem #include <sys/sx.h>
70dfdcada3SDoug Rabson #include <sys/syslog.h>
71dfdcada3SDoug Rabson #include <sys/time.h>
72dfdcada3SDoug Rabson #include <sys/uio.h>
730348c661SMarko Zec
740348c661SMarko Zec #include <net/vnet.h>
750348c661SMarko Zec
76a9148abdSDoug Rabson #include <netinet/tcp.h>
77dfdcada3SDoug Rabson
78dfdcada3SDoug Rabson #include <rpc/rpc.h>
79ee31b83aSDoug Rabson #include <rpc/rpc_com.h>
80e2adc47dSRick Macklem #include <rpc/krpc.h>
81ab0c29afSRick Macklem #include <rpc/rpcsec_tls.h>
82dfdcada3SDoug Rabson
83dfdcada3SDoug Rabson struct cmessage {
84dfdcada3SDoug Rabson struct cmsghdr cmsg;
85dfdcada3SDoug Rabson struct cmsgcred cmcred;
86dfdcada3SDoug Rabson };
87dfdcada3SDoug Rabson
88c675522fSDoug Rabson static enum clnt_stat clnt_vc_call(CLIENT *, struct rpc_callextra *,
89a9148abdSDoug Rabson rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
90dfdcada3SDoug Rabson static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
91dfdcada3SDoug Rabson static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *);
92dfdcada3SDoug Rabson static void clnt_vc_abort(CLIENT *);
93dfdcada3SDoug Rabson static bool_t clnt_vc_control(CLIENT *, u_int, void *);
94a9148abdSDoug Rabson static void clnt_vc_close(CLIENT *);
95dfdcada3SDoug Rabson static void clnt_vc_destroy(CLIENT *);
96dfdcada3SDoug Rabson static bool_t time_not_ok(struct timeval *);
9774fb0ba7SJohn Baldwin static int clnt_vc_soupcall(struct socket *so, void *arg, int waitflag);
98ab0c29afSRick Macklem static void clnt_vc_dotlsupcall(void *data);
99dfdcada3SDoug Rabson
10020d728b5SMark Johnston static const struct clnt_ops clnt_vc_ops = {
101dfdcada3SDoug Rabson .cl_call = clnt_vc_call,
102dfdcada3SDoug Rabson .cl_abort = clnt_vc_abort,
103dfdcada3SDoug Rabson .cl_geterr = clnt_vc_geterr,
104dfdcada3SDoug Rabson .cl_freeres = clnt_vc_freeres,
105a9148abdSDoug Rabson .cl_close = clnt_vc_close,
106dfdcada3SDoug Rabson .cl_destroy = clnt_vc_destroy,
107dfdcada3SDoug Rabson .cl_control = clnt_vc_control
108dfdcada3SDoug Rabson };
109dfdcada3SDoug Rabson
1103144f812SRick Macklem static void clnt_vc_upcallsdone(struct ct_data *);
1113144f812SRick Macklem
112dfdcada3SDoug Rabson /*
113dfdcada3SDoug Rabson * Create a client handle for a connection.
114dfdcada3SDoug Rabson * Default options are set, which the user can change using clnt_control()'s.
115dfdcada3SDoug Rabson * The rpc/vc package does buffering similar to stdio, so the client
116dfdcada3SDoug Rabson * must pick send and receive buffer sizes, 0 => use the default.
117dfdcada3SDoug Rabson * NB: fd is copied into a private area.
118dfdcada3SDoug Rabson * NB: The rpch->cl_auth is set null authentication. Caller may wish to
119dfdcada3SDoug Rabson * set this something more useful.
120dfdcada3SDoug Rabson *
121dfdcada3SDoug Rabson * fd should be an open socket
122dfdcada3SDoug Rabson */
123dfdcada3SDoug Rabson CLIENT *
clnt_vc_create(struct socket * so,struct sockaddr * raddr,const rpcprog_t prog,const rpcvers_t vers,size_t sendsz,size_t recvsz,int intrflag)124dfdcada3SDoug Rabson clnt_vc_create(
125dfdcada3SDoug Rabson struct socket *so, /* open file descriptor */
126dfdcada3SDoug Rabson struct sockaddr *raddr, /* servers address */
127dfdcada3SDoug Rabson const rpcprog_t prog, /* program number */
128dfdcada3SDoug Rabson const rpcvers_t vers, /* version number */
129dfdcada3SDoug Rabson size_t sendsz, /* buffer recv size */
1307b67bd9fSRick Macklem size_t recvsz, /* buffer send size */
1317b67bd9fSRick Macklem int intrflag) /* interruptible */
132dfdcada3SDoug Rabson {
133dfdcada3SDoug Rabson CLIENT *cl; /* client handle */
134dfdcada3SDoug Rabson struct ct_data *ct = NULL; /* client handle */
135dfdcada3SDoug Rabson struct timeval now;
136dfdcada3SDoug Rabson struct rpc_msg call_msg;
137dfdcada3SDoug Rabson static uint32_t disrupt;
138dfdcada3SDoug Rabson struct __rpc_sockinfo si;
139dfdcada3SDoug Rabson XDR xdrs;
1407b67bd9fSRick Macklem int error, interrupted, one = 1, sleep_flag;
141a9148abdSDoug Rabson struct sockopt sopt;
142dfdcada3SDoug Rabson
143dfdcada3SDoug Rabson if (disrupt == 0)
144dfdcada3SDoug Rabson disrupt = (uint32_t)(long)raddr;
145dfdcada3SDoug Rabson
146dfdcada3SDoug Rabson cl = (CLIENT *)mem_alloc(sizeof (*cl));
147dfdcada3SDoug Rabson ct = (struct ct_data *)mem_alloc(sizeof (*ct));
148dfdcada3SDoug Rabson
149dfdcada3SDoug Rabson mtx_init(&ct->ct_lock, "ct->ct_lock", NULL, MTX_DEF);
150c675522fSDoug Rabson ct->ct_threads = 0;
151c675522fSDoug Rabson ct->ct_closing = FALSE;
152a9148abdSDoug Rabson ct->ct_closed = FALSE;
1533144f812SRick Macklem ct->ct_upcallrefs = 0;
154ab0c29afSRick Macklem ct->ct_rcvstate = RPCRCVSTATE_NORMAL;
155dfdcada3SDoug Rabson
156f79a8585SGleb Smirnoff if ((so->so_state & SS_ISCONNECTED) == 0) {
157dfdcada3SDoug Rabson error = soconnect(so, raddr, curthread);
158c675522fSDoug Rabson SOCK_LOCK(so);
159c675522fSDoug Rabson interrupted = 0;
1607b67bd9fSRick Macklem sleep_flag = PSOCK;
1617b67bd9fSRick Macklem if (intrflag != 0)
1623b14c753SJohn Baldwin sleep_flag |= PCATCH;
163c675522fSDoug Rabson while ((so->so_state & SS_ISCONNECTING)
164c675522fSDoug Rabson && so->so_error == 0) {
165c675522fSDoug Rabson error = msleep(&so->so_timeo, SOCK_MTX(so),
1667b67bd9fSRick Macklem sleep_flag, "connec", 0);
167dfdcada3SDoug Rabson if (error) {
168c675522fSDoug Rabson if (error == EINTR || error == ERESTART)
169c675522fSDoug Rabson interrupted = 1;
170c675522fSDoug Rabson break;
171c675522fSDoug Rabson }
172c675522fSDoug Rabson }
173c675522fSDoug Rabson if (error == 0) {
174c675522fSDoug Rabson error = so->so_error;
175c675522fSDoug Rabson so->so_error = 0;
176c675522fSDoug Rabson }
177c675522fSDoug Rabson SOCK_UNLOCK(so);
178c675522fSDoug Rabson if (error) {
179c675522fSDoug Rabson if (!interrupted)
180c675522fSDoug Rabson so->so_state &= ~SS_ISCONNECTING;
181dfdcada3SDoug Rabson rpc_createerr.cf_stat = RPC_SYSTEMERROR;
182dfdcada3SDoug Rabson rpc_createerr.cf_error.re_errno = error;
183dfdcada3SDoug Rabson goto err;
184dfdcada3SDoug Rabson }
185dfdcada3SDoug Rabson }
186dfdcada3SDoug Rabson
1870348c661SMarko Zec if (!__rpc_socket2sockinfo(so, &si)) {
188dfdcada3SDoug Rabson goto err;
1890348c661SMarko Zec }
190dfdcada3SDoug Rabson
191a9148abdSDoug Rabson if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
192a9148abdSDoug Rabson bzero(&sopt, sizeof(sopt));
193a9148abdSDoug Rabson sopt.sopt_dir = SOPT_SET;
194a9148abdSDoug Rabson sopt.sopt_level = SOL_SOCKET;
195a9148abdSDoug Rabson sopt.sopt_name = SO_KEEPALIVE;
196a9148abdSDoug Rabson sopt.sopt_val = &one;
197a9148abdSDoug Rabson sopt.sopt_valsize = sizeof(one);
198a9148abdSDoug Rabson sosetopt(so, &sopt);
199a9148abdSDoug Rabson }
200a9148abdSDoug Rabson
201a9148abdSDoug Rabson if (so->so_proto->pr_protocol == IPPROTO_TCP) {
202a9148abdSDoug Rabson bzero(&sopt, sizeof(sopt));
203a9148abdSDoug Rabson sopt.sopt_dir = SOPT_SET;
204a9148abdSDoug Rabson sopt.sopt_level = IPPROTO_TCP;
205a9148abdSDoug Rabson sopt.sopt_name = TCP_NODELAY;
206a9148abdSDoug Rabson sopt.sopt_val = &one;
207a9148abdSDoug Rabson sopt.sopt_valsize = sizeof(one);
208a9148abdSDoug Rabson sosetopt(so, &sopt);
209a9148abdSDoug Rabson }
210a9148abdSDoug Rabson
211dfdcada3SDoug Rabson ct->ct_closeit = FALSE;
212dfdcada3SDoug Rabson
213dfdcada3SDoug Rabson /*
214dfdcada3SDoug Rabson * Set up private data struct
215dfdcada3SDoug Rabson */
216dfdcada3SDoug Rabson ct->ct_socket = so;
217dfdcada3SDoug Rabson ct->ct_wait.tv_sec = -1;
218dfdcada3SDoug Rabson ct->ct_wait.tv_usec = -1;
219dfdcada3SDoug Rabson memcpy(&ct->ct_addr, raddr, raddr->sa_len);
220dfdcada3SDoug Rabson
221dfdcada3SDoug Rabson /*
222dfdcada3SDoug Rabson * Initialize call message
223dfdcada3SDoug Rabson */
224dfdcada3SDoug Rabson getmicrotime(&now);
225dfdcada3SDoug Rabson ct->ct_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now);
226dfdcada3SDoug Rabson call_msg.rm_xid = ct->ct_xid;
227dfdcada3SDoug Rabson call_msg.rm_direction = CALL;
228dfdcada3SDoug Rabson call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
229dfdcada3SDoug Rabson call_msg.rm_call.cb_prog = (uint32_t)prog;
230dfdcada3SDoug Rabson call_msg.rm_call.cb_vers = (uint32_t)vers;
231dfdcada3SDoug Rabson
232dfdcada3SDoug Rabson /*
233dfdcada3SDoug Rabson * pre-serialize the static part of the call msg and stash it away
234dfdcada3SDoug Rabson */
235dfdcada3SDoug Rabson xdrmem_create(&xdrs, ct->ct_mcallc, MCALL_MSG_SIZE,
236dfdcada3SDoug Rabson XDR_ENCODE);
237dfdcada3SDoug Rabson if (! xdr_callhdr(&xdrs, &call_msg)) {
238dfdcada3SDoug Rabson if (ct->ct_closeit) {
239dfdcada3SDoug Rabson soclose(ct->ct_socket);
240dfdcada3SDoug Rabson }
241dfdcada3SDoug Rabson goto err;
242dfdcada3SDoug Rabson }
243dfdcada3SDoug Rabson ct->ct_mpos = XDR_GETPOS(&xdrs);
244dfdcada3SDoug Rabson XDR_DESTROY(&xdrs);
245dfdcada3SDoug Rabson ct->ct_waitchan = "rpcrecv";
246dfdcada3SDoug Rabson ct->ct_waitflag = 0;
247dfdcada3SDoug Rabson
248dfdcada3SDoug Rabson /*
249dfdcada3SDoug Rabson * Create a client handle which uses xdrrec for serialization
250dfdcada3SDoug Rabson * and authnone for authentication.
251dfdcada3SDoug Rabson */
252cec077bcSRick Macklem sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
253cec077bcSRick Macklem recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
254cec077bcSRick Macklem error = soreserve(ct->ct_socket, sendsz, recvsz);
255cec077bcSRick Macklem if (error != 0) {
256cec077bcSRick Macklem if (ct->ct_closeit) {
257cec077bcSRick Macklem soclose(ct->ct_socket);
258cec077bcSRick Macklem }
259cec077bcSRick Macklem goto err;
260cec077bcSRick Macklem }
261c675522fSDoug Rabson cl->cl_refs = 1;
262dfdcada3SDoug Rabson cl->cl_ops = &clnt_vc_ops;
263dfdcada3SDoug Rabson cl->cl_private = ct;
264dfdcada3SDoug Rabson cl->cl_auth = authnone_create();
265dfdcada3SDoug Rabson
266e205fd31SGleb Smirnoff SOCK_RECVBUF_LOCK(ct->ct_socket);
26774fb0ba7SJohn Baldwin soupcall_set(ct->ct_socket, SO_RCV, clnt_vc_soupcall, ct);
268e205fd31SGleb Smirnoff SOCK_RECVBUF_UNLOCK(ct->ct_socket);
269dfdcada3SDoug Rabson
2704302e8b6SRick Macklem ct->ct_raw = NULL;
271dfdcada3SDoug Rabson ct->ct_record = NULL;
272dfdcada3SDoug Rabson ct->ct_record_resid = 0;
273ab0c29afSRick Macklem ct->ct_sslrefno = 0;
274dfdcada3SDoug Rabson TAILQ_INIT(&ct->ct_pending);
275dfdcada3SDoug Rabson return (cl);
276dfdcada3SDoug Rabson
277dfdcada3SDoug Rabson err:
278a9ccfd56SDoug Rabson mtx_destroy(&ct->ct_lock);
279dfdcada3SDoug Rabson mem_free(ct, sizeof (struct ct_data));
280dfdcada3SDoug Rabson mem_free(cl, sizeof (CLIENT));
2817d3db235SEnji Cooper
282dfdcada3SDoug Rabson return ((CLIENT *)NULL);
283dfdcada3SDoug Rabson }
284dfdcada3SDoug Rabson
285dfdcada3SDoug Rabson static enum clnt_stat
clnt_vc_call(CLIENT * cl,struct rpc_callextra * ext,rpcproc_t proc,struct mbuf * args,struct mbuf ** resultsp,struct timeval utimeout)286dfdcada3SDoug Rabson clnt_vc_call(
287a9148abdSDoug Rabson CLIENT *cl, /* client handle */
288a9148abdSDoug Rabson struct rpc_callextra *ext, /* call metadata */
289a9148abdSDoug Rabson rpcproc_t proc, /* procedure number */
290a9148abdSDoug Rabson struct mbuf *args, /* pointer to args */
291a9148abdSDoug Rabson struct mbuf **resultsp, /* pointer to results */
292dfdcada3SDoug Rabson struct timeval utimeout)
293dfdcada3SDoug Rabson {
294dfdcada3SDoug Rabson struct ct_data *ct = (struct ct_data *) cl->cl_private;
295c675522fSDoug Rabson AUTH *auth;
296a9148abdSDoug Rabson struct rpc_err *errp;
297a9148abdSDoug Rabson enum clnt_stat stat;
298dfdcada3SDoug Rabson XDR xdrs;
299dfdcada3SDoug Rabson struct rpc_msg reply_msg;
300dfdcada3SDoug Rabson bool_t ok;
301dfdcada3SDoug Rabson int nrefreshes = 2; /* number of times to refresh cred */
302dfdcada3SDoug Rabson struct timeval timeout;
303dfdcada3SDoug Rabson uint32_t xid;
304a9148abdSDoug Rabson struct mbuf *mreq = NULL, *results;
305c675522fSDoug Rabson struct ct_request *cr;
306ab0c29afSRick Macklem int error, maxextsiz, trycnt;
307ab0c29afSRick Macklem #ifdef KERN_TLS
308ab0c29afSRick Macklem u_int maxlen;
309ab0c29afSRick Macklem #endif
310dfdcada3SDoug Rabson
311c675522fSDoug Rabson cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
312c675522fSDoug Rabson
313dfdcada3SDoug Rabson mtx_lock(&ct->ct_lock);
314dfdcada3SDoug Rabson
315a9148abdSDoug Rabson if (ct->ct_closing || ct->ct_closed) {
316c675522fSDoug Rabson mtx_unlock(&ct->ct_lock);
317c675522fSDoug Rabson free(cr, M_RPC);
318c675522fSDoug Rabson return (RPC_CANTSEND);
319c675522fSDoug Rabson }
320c675522fSDoug Rabson ct->ct_threads++;
321c675522fSDoug Rabson
322a9148abdSDoug Rabson if (ext) {
323c675522fSDoug Rabson auth = ext->rc_auth;
324a9148abdSDoug Rabson errp = &ext->rc_err;
325a9148abdSDoug Rabson } else {
326c675522fSDoug Rabson auth = cl->cl_auth;
327a9148abdSDoug Rabson errp = &ct->ct_error;
328a9148abdSDoug Rabson }
329c675522fSDoug Rabson
330c675522fSDoug Rabson cr->cr_mrep = NULL;
331c675522fSDoug Rabson cr->cr_error = 0;
332dfdcada3SDoug Rabson
333dfdcada3SDoug Rabson if (ct->ct_wait.tv_usec == -1) {
334dfdcada3SDoug Rabson timeout = utimeout; /* use supplied timeout */
335dfdcada3SDoug Rabson } else {
336dfdcada3SDoug Rabson timeout = ct->ct_wait; /* use default timeout */
337dfdcada3SDoug Rabson }
338dfdcada3SDoug Rabson
339dfd174d6SRick Macklem /*
340dfd174d6SRick Macklem * After 15sec of looping, allow it to return RPC_CANTSEND, which will
341dfd174d6SRick Macklem * cause the clnt_reconnect layer to create a new TCP connection.
342dfd174d6SRick Macklem */
343dfd174d6SRick Macklem trycnt = 15 * hz;
344dfdcada3SDoug Rabson call_again:
345dfdcada3SDoug Rabson mtx_assert(&ct->ct_lock, MA_OWNED);
346dfd174d6SRick Macklem if (ct->ct_closing || ct->ct_closed) {
347dfd174d6SRick Macklem ct->ct_threads--;
348dfd174d6SRick Macklem wakeup(ct);
349dfd174d6SRick Macklem mtx_unlock(&ct->ct_lock);
350dfd174d6SRick Macklem free(cr, M_RPC);
351dfd174d6SRick Macklem return (RPC_CANTSEND);
352dfd174d6SRick Macklem }
353dfdcada3SDoug Rabson
354dfdcada3SDoug Rabson ct->ct_xid++;
355dfdcada3SDoug Rabson xid = ct->ct_xid;
356dfdcada3SDoug Rabson
357dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
358dfdcada3SDoug Rabson
359dfdcada3SDoug Rabson /*
360dfdcada3SDoug Rabson * Leave space to pre-pend the record mark.
361dfdcada3SDoug Rabson */
362bd54830bSGleb Smirnoff mreq = m_gethdr(M_WAITOK, MT_DATA);
363dfdcada3SDoug Rabson mreq->m_data += sizeof(uint32_t);
364a9148abdSDoug Rabson KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN,
365a9148abdSDoug Rabson ("RPC header too big"));
366a9148abdSDoug Rabson bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos);
367a9148abdSDoug Rabson mreq->m_len = ct->ct_mpos;
368dfdcada3SDoug Rabson
369dfdcada3SDoug Rabson /*
370dfdcada3SDoug Rabson * The XID is the first thing in the request.
371dfdcada3SDoug Rabson */
372dfdcada3SDoug Rabson *mtod(mreq, uint32_t *) = htonl(xid);
373dfdcada3SDoug Rabson
374dfdcada3SDoug Rabson xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
375dfdcada3SDoug Rabson
376a9148abdSDoug Rabson errp->re_status = stat = RPC_SUCCESS;
377dfdcada3SDoug Rabson
378dfdcada3SDoug Rabson if ((! XDR_PUTINT32(&xdrs, &proc)) ||
379a9148abdSDoug Rabson (! AUTH_MARSHALL(auth, xid, &xdrs,
380a9148abdSDoug Rabson m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
381a9148abdSDoug Rabson errp->re_status = stat = RPC_CANTENCODEARGS;
382c675522fSDoug Rabson mtx_lock(&ct->ct_lock);
383c675522fSDoug Rabson goto out;
384dfdcada3SDoug Rabson }
385a9148abdSDoug Rabson mreq->m_pkthdr.len = m_length(mreq, NULL);
386dfdcada3SDoug Rabson
387dfdcada3SDoug Rabson /*
388dfdcada3SDoug Rabson * Prepend a record marker containing the packet length.
389dfdcada3SDoug Rabson */
390eb1b1807SGleb Smirnoff M_PREPEND(mreq, sizeof(uint32_t), M_WAITOK);
391dfdcada3SDoug Rabson *mtod(mreq, uint32_t *) =
392dfdcada3SDoug Rabson htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t)));
393dfdcada3SDoug Rabson
394c675522fSDoug Rabson cr->cr_xid = xid;
395dfdcada3SDoug Rabson mtx_lock(&ct->ct_lock);
396f9917533SRick Macklem /*
397f9917533SRick Macklem * Check to see if the other end has already started to close down
398f9917533SRick Macklem * the connection. The upcall will have set ct_error.re_status
399f9917533SRick Macklem * to RPC_CANTRECV if this is the case.
400f9917533SRick Macklem * If the other end starts to close down the connection after this
401f9917533SRick Macklem * point, it will be detected later when cr_error is checked,
402f9917533SRick Macklem * since the request is in the ct_pending queue.
403f9917533SRick Macklem */
404f9917533SRick Macklem if (ct->ct_error.re_status == RPC_CANTRECV) {
405f9917533SRick Macklem if (errp != &ct->ct_error) {
406f9917533SRick Macklem errp->re_errno = ct->ct_error.re_errno;
407f9917533SRick Macklem errp->re_status = RPC_CANTRECV;
408f9917533SRick Macklem }
409f9917533SRick Macklem stat = RPC_CANTRECV;
410f9917533SRick Macklem goto out;
411f9917533SRick Macklem }
412ab0c29afSRick Macklem
413ab0c29afSRick Macklem /* For TLS, wait for an upcall to be done, as required. */
414ab0c29afSRick Macklem while ((ct->ct_rcvstate & (RPCRCVSTATE_NORMAL |
415ab0c29afSRick Macklem RPCRCVSTATE_NONAPPDATA)) == 0)
416ab0c29afSRick Macklem msleep(&ct->ct_rcvstate, &ct->ct_lock, 0, "rpcrcvst", hz);
417ab0c29afSRick Macklem
418c675522fSDoug Rabson TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link);
419dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
420dfdcada3SDoug Rabson
421ab0c29afSRick Macklem if (ct->ct_sslrefno != 0) {
422ab0c29afSRick Macklem /*
423ab0c29afSRick Macklem * Copy the mbuf chain to a chain of ext_pgs mbuf(s)
424ab0c29afSRick Macklem * as required by KERN_TLS.
425ab0c29afSRick Macklem */
426ab0c29afSRick Macklem maxextsiz = TLS_MAX_MSG_SIZE_V10_2;
427ab0c29afSRick Macklem #ifdef KERN_TLS
428ab0c29afSRick Macklem if (rpctls_getinfo(&maxlen, false, false))
429ab0c29afSRick Macklem maxextsiz = min(maxextsiz, maxlen);
430ab0c29afSRick Macklem #endif
431ab0c29afSRick Macklem mreq = _rpc_copym_into_ext_pgs(mreq, maxextsiz);
432ab0c29afSRick Macklem }
433dfdcada3SDoug Rabson /*
434dfdcada3SDoug Rabson * sosend consumes mreq.
435dfdcada3SDoug Rabson */
436dfdcada3SDoug Rabson error = sosend(ct->ct_socket, NULL, NULL, mreq, NULL, 0, curthread);
437dfdcada3SDoug Rabson mreq = NULL;
438dfd174d6SRick Macklem if (error == EMSGSIZE || (error == ERESTART &&
439dfd174d6SRick Macklem (ct->ct_waitflag & PCATCH) == 0 && trycnt-- > 0)) {
440e205fd31SGleb Smirnoff SOCK_SENDBUF_LOCK(ct->ct_socket);
44143283184SGleb Smirnoff sbwait(ct->ct_socket, SO_SND);
442e205fd31SGleb Smirnoff SOCK_SENDBUF_UNLOCK(ct->ct_socket);
443a9148abdSDoug Rabson AUTH_VALIDATE(auth, xid, NULL, NULL);
444a9148abdSDoug Rabson mtx_lock(&ct->ct_lock);
445a9148abdSDoug Rabson TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
446dfd174d6SRick Macklem /* Sleep for 1 clock tick before trying the sosend() again. */
44782512c17SRick Macklem mtx_unlock(&ct->ct_lock);
44882512c17SRick Macklem pause("rpclpsnd", 1);
44982512c17SRick Macklem mtx_lock(&ct->ct_lock);
450a9148abdSDoug Rabson goto call_again;
451a9148abdSDoug Rabson }
452dfdcada3SDoug Rabson
453a9148abdSDoug Rabson reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
454a9148abdSDoug Rabson reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
455a9148abdSDoug Rabson reply_msg.acpted_rply.ar_verf.oa_length = 0;
456a9148abdSDoug Rabson reply_msg.acpted_rply.ar_results.where = NULL;
457a9148abdSDoug Rabson reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
458dfdcada3SDoug Rabson
459dfdcada3SDoug Rabson mtx_lock(&ct->ct_lock);
460dfdcada3SDoug Rabson if (error) {
461c675522fSDoug Rabson TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
462a9148abdSDoug Rabson errp->re_errno = error;
463a9148abdSDoug Rabson errp->re_status = stat = RPC_CANTSEND;
464dfdcada3SDoug Rabson goto out;
465dfdcada3SDoug Rabson }
466dfdcada3SDoug Rabson
467dfdcada3SDoug Rabson /*
468dfdcada3SDoug Rabson * Check to see if we got an upcall while waiting for the
469dfdcada3SDoug Rabson * lock. In both these cases, the request has been removed
470dfdcada3SDoug Rabson * from ct->ct_pending.
471dfdcada3SDoug Rabson */
472c675522fSDoug Rabson if (cr->cr_error) {
473c675522fSDoug Rabson TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
474a9148abdSDoug Rabson errp->re_errno = cr->cr_error;
475a9148abdSDoug Rabson errp->re_status = stat = RPC_CANTRECV;
476dfdcada3SDoug Rabson goto out;
477dfdcada3SDoug Rabson }
478c675522fSDoug Rabson if (cr->cr_mrep) {
479c675522fSDoug Rabson TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
480dfdcada3SDoug Rabson goto got_reply;
481dfdcada3SDoug Rabson }
482dfdcada3SDoug Rabson
483dfdcada3SDoug Rabson /*
484dfdcada3SDoug Rabson * Hack to provide rpc-based message passing
485dfdcada3SDoug Rabson */
486dfdcada3SDoug Rabson if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
487c675522fSDoug Rabson TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
488a9148abdSDoug Rabson errp->re_status = stat = RPC_TIMEDOUT;
489dfdcada3SDoug Rabson goto out;
490dfdcada3SDoug Rabson }
491dfdcada3SDoug Rabson
492c675522fSDoug Rabson error = msleep(cr, &ct->ct_lock, ct->ct_waitflag, ct->ct_waitchan,
493dfdcada3SDoug Rabson tvtohz(&timeout));
494dfdcada3SDoug Rabson
495c675522fSDoug Rabson TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
496c675522fSDoug Rabson
497dfdcada3SDoug Rabson if (error) {
498dfdcada3SDoug Rabson /*
499dfdcada3SDoug Rabson * The sleep returned an error so our request is still
500dfdcada3SDoug Rabson * on the list. Turn the error code into an
501dfdcada3SDoug Rabson * appropriate client status.
502dfdcada3SDoug Rabson */
503a9148abdSDoug Rabson errp->re_errno = error;
504dfdcada3SDoug Rabson switch (error) {
505dfdcada3SDoug Rabson case EINTR:
506a9148abdSDoug Rabson stat = RPC_INTR;
507dfdcada3SDoug Rabson break;
508dfdcada3SDoug Rabson case EWOULDBLOCK:
509a9148abdSDoug Rabson stat = RPC_TIMEDOUT;
510dfdcada3SDoug Rabson break;
511dfdcada3SDoug Rabson default:
512a9148abdSDoug Rabson stat = RPC_CANTRECV;
513dfdcada3SDoug Rabson }
514a9148abdSDoug Rabson errp->re_status = stat;
515dfdcada3SDoug Rabson goto out;
516dfdcada3SDoug Rabson } else {
517dfdcada3SDoug Rabson /*
518dfdcada3SDoug Rabson * We were woken up by the upcall. If the
519dfdcada3SDoug Rabson * upcall had a receive error, report that,
520dfdcada3SDoug Rabson * otherwise we have a reply.
521dfdcada3SDoug Rabson */
522c675522fSDoug Rabson if (cr->cr_error) {
523a9148abdSDoug Rabson errp->re_errno = cr->cr_error;
524a9148abdSDoug Rabson errp->re_status = stat = RPC_CANTRECV;
525dfdcada3SDoug Rabson goto out;
526dfdcada3SDoug Rabson }
527dfdcada3SDoug Rabson }
528dfdcada3SDoug Rabson
529dfdcada3SDoug Rabson got_reply:
530dfdcada3SDoug Rabson /*
531dfdcada3SDoug Rabson * Now decode and validate the response. We need to drop the
532dfdcada3SDoug Rabson * lock since xdr_replymsg may end up sleeping in malloc.
533dfdcada3SDoug Rabson */
534dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
535dfdcada3SDoug Rabson
536a9148abdSDoug Rabson if (ext && ext->rc_feedback)
537a9148abdSDoug Rabson ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
538a9148abdSDoug Rabson
539c675522fSDoug Rabson xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
540dfdcada3SDoug Rabson ok = xdr_replymsg(&xdrs, &reply_msg);
541c675522fSDoug Rabson cr->cr_mrep = NULL;
542dfdcada3SDoug Rabson
543dfdcada3SDoug Rabson if (ok) {
544dfdcada3SDoug Rabson if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
545dfdcada3SDoug Rabson (reply_msg.acpted_rply.ar_stat == SUCCESS))
546a9148abdSDoug Rabson errp->re_status = stat = RPC_SUCCESS;
547dfdcada3SDoug Rabson else
548a9148abdSDoug Rabson stat = _seterr_reply(&reply_msg, errp);
549dfdcada3SDoug Rabson
550a9148abdSDoug Rabson if (stat == RPC_SUCCESS) {
551a9148abdSDoug Rabson results = xdrmbuf_getall(&xdrs);
552a9148abdSDoug Rabson if (!AUTH_VALIDATE(auth, xid,
553a9148abdSDoug Rabson &reply_msg.acpted_rply.ar_verf,
554a9148abdSDoug Rabson &results)) {
555a9148abdSDoug Rabson errp->re_status = stat = RPC_AUTHERROR;
556a9148abdSDoug Rabson errp->re_why = AUTH_INVALIDRESP;
557a9148abdSDoug Rabson } else {
558a9148abdSDoug Rabson KASSERT(results,
559a9148abdSDoug Rabson ("auth validated but no result"));
560a9148abdSDoug Rabson *resultsp = results;
561dfdcada3SDoug Rabson }
562dfdcada3SDoug Rabson } /* end successful completion */
563dfdcada3SDoug Rabson /*
5646244c6e7SPedro F. Giffuni * If unsuccessful AND error is an authentication error
565dfdcada3SDoug Rabson * then refresh credentials and try again, else break
566dfdcada3SDoug Rabson */
567a9148abdSDoug Rabson else if (stat == RPC_AUTHERROR)
568dfdcada3SDoug Rabson /* maybe our credentials need to be refreshed ... */
569dfdcada3SDoug Rabson if (nrefreshes > 0 &&
570a9148abdSDoug Rabson AUTH_REFRESH(auth, &reply_msg)) {
571dfdcada3SDoug Rabson nrefreshes--;
572a9148abdSDoug Rabson XDR_DESTROY(&xdrs);
573a9148abdSDoug Rabson mtx_lock(&ct->ct_lock);
574dfdcada3SDoug Rabson goto call_again;
575dfdcada3SDoug Rabson }
576dfdcada3SDoug Rabson /* end of unsuccessful completion */
577dfdcada3SDoug Rabson } /* end of valid reply message */
578dfdcada3SDoug Rabson else {
579a9148abdSDoug Rabson errp->re_status = stat = RPC_CANTDECODERES;
580dfdcada3SDoug Rabson }
581a9148abdSDoug Rabson XDR_DESTROY(&xdrs);
582a9148abdSDoug Rabson mtx_lock(&ct->ct_lock);
583dfdcada3SDoug Rabson out:
584dfdcada3SDoug Rabson mtx_assert(&ct->ct_lock, MA_OWNED);
585dfdcada3SDoug Rabson
586a9148abdSDoug Rabson KASSERT(stat != RPC_SUCCESS || *resultsp,
587a9148abdSDoug Rabson ("RPC_SUCCESS without reply"));
588a9148abdSDoug Rabson
589dfdcada3SDoug Rabson if (mreq)
590dfdcada3SDoug Rabson m_freem(mreq);
591c675522fSDoug Rabson if (cr->cr_mrep)
592c675522fSDoug Rabson m_freem(cr->cr_mrep);
593c675522fSDoug Rabson
594c675522fSDoug Rabson ct->ct_threads--;
595c675522fSDoug Rabson if (ct->ct_closing)
596c675522fSDoug Rabson wakeup(ct);
597dfdcada3SDoug Rabson
598dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
599c675522fSDoug Rabson
600a9148abdSDoug Rabson if (auth && stat != RPC_SUCCESS)
601a9148abdSDoug Rabson AUTH_VALIDATE(auth, xid, NULL, NULL);
602a9148abdSDoug Rabson
603c675522fSDoug Rabson free(cr, M_RPC);
604c675522fSDoug Rabson
605a9148abdSDoug Rabson return (stat);
606dfdcada3SDoug Rabson }
607dfdcada3SDoug Rabson
608dfdcada3SDoug Rabson static void
clnt_vc_geterr(CLIENT * cl,struct rpc_err * errp)609dfdcada3SDoug Rabson clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
610dfdcada3SDoug Rabson {
611dfdcada3SDoug Rabson struct ct_data *ct = (struct ct_data *) cl->cl_private;
612dfdcada3SDoug Rabson
613dfdcada3SDoug Rabson *errp = ct->ct_error;
614dfdcada3SDoug Rabson }
615dfdcada3SDoug Rabson
616dfdcada3SDoug Rabson static bool_t
clnt_vc_freeres(CLIENT * cl,xdrproc_t xdr_res,void * res_ptr)617dfdcada3SDoug Rabson clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
618dfdcada3SDoug Rabson {
619dfdcada3SDoug Rabson XDR xdrs;
620dfdcada3SDoug Rabson bool_t dummy;
621dfdcada3SDoug Rabson
622dfdcada3SDoug Rabson xdrs.x_op = XDR_FREE;
623dfdcada3SDoug Rabson dummy = (*xdr_res)(&xdrs, res_ptr);
624dfdcada3SDoug Rabson
625dfdcada3SDoug Rabson return (dummy);
626dfdcada3SDoug Rabson }
627dfdcada3SDoug Rabson
628dfdcada3SDoug Rabson /*ARGSUSED*/
629dfdcada3SDoug Rabson static void
clnt_vc_abort(CLIENT * cl)630dfdcada3SDoug Rabson clnt_vc_abort(CLIENT *cl)
631dfdcada3SDoug Rabson {
632dfdcada3SDoug Rabson }
633dfdcada3SDoug Rabson
634dfdcada3SDoug Rabson static bool_t
clnt_vc_control(CLIENT * cl,u_int request,void * info)635dfdcada3SDoug Rabson clnt_vc_control(CLIENT *cl, u_int request, void *info)
636dfdcada3SDoug Rabson {
637dfdcada3SDoug Rabson struct ct_data *ct = (struct ct_data *)cl->cl_private;
638dfdcada3SDoug Rabson void *infop = info;
639e2adc47dSRick Macklem SVCXPRT *xprt;
640ab0c29afSRick Macklem uint64_t *p;
641ab0c29afSRick Macklem int error;
642ab0c29afSRick Macklem static u_int thrdnum = 0;
643dfdcada3SDoug Rabson
644dfdcada3SDoug Rabson mtx_lock(&ct->ct_lock);
645dfdcada3SDoug Rabson
646dfdcada3SDoug Rabson switch (request) {
647dfdcada3SDoug Rabson case CLSET_FD_CLOSE:
648dfdcada3SDoug Rabson ct->ct_closeit = TRUE;
649dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
650dfdcada3SDoug Rabson return (TRUE);
651dfdcada3SDoug Rabson case CLSET_FD_NCLOSE:
652dfdcada3SDoug Rabson ct->ct_closeit = FALSE;
653dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
654dfdcada3SDoug Rabson return (TRUE);
655dfdcada3SDoug Rabson default:
656dfdcada3SDoug Rabson break;
657dfdcada3SDoug Rabson }
658dfdcada3SDoug Rabson
659dfdcada3SDoug Rabson /* for other requests which use info */
660dfdcada3SDoug Rabson if (info == NULL) {
661dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
662dfdcada3SDoug Rabson return (FALSE);
663dfdcada3SDoug Rabson }
664dfdcada3SDoug Rabson switch (request) {
665dfdcada3SDoug Rabson case CLSET_TIMEOUT:
666dfdcada3SDoug Rabson if (time_not_ok((struct timeval *)info)) {
667dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
668dfdcada3SDoug Rabson return (FALSE);
669dfdcada3SDoug Rabson }
670dfdcada3SDoug Rabson ct->ct_wait = *(struct timeval *)infop;
671dfdcada3SDoug Rabson break;
672dfdcada3SDoug Rabson case CLGET_TIMEOUT:
673dfdcada3SDoug Rabson *(struct timeval *)infop = ct->ct_wait;
674dfdcada3SDoug Rabson break;
675dfdcada3SDoug Rabson case CLGET_SERVER_ADDR:
676dfdcada3SDoug Rabson (void) memcpy(info, &ct->ct_addr, (size_t)ct->ct_addr.ss_len);
677dfdcada3SDoug Rabson break;
678dfdcada3SDoug Rabson case CLGET_SVC_ADDR:
679dfdcada3SDoug Rabson /*
680dfdcada3SDoug Rabson * Slightly different semantics to userland - we use
681dfdcada3SDoug Rabson * sockaddr instead of netbuf.
682dfdcada3SDoug Rabson */
683dfdcada3SDoug Rabson memcpy(info, &ct->ct_addr, ct->ct_addr.ss_len);
684dfdcada3SDoug Rabson break;
685dfdcada3SDoug Rabson case CLSET_SVC_ADDR: /* set to new address */
686dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
687dfdcada3SDoug Rabson return (FALSE);
688dfdcada3SDoug Rabson case CLGET_XID:
689dfdcada3SDoug Rabson *(uint32_t *)info = ct->ct_xid;
690dfdcada3SDoug Rabson break;
691dfdcada3SDoug Rabson case CLSET_XID:
692dfdcada3SDoug Rabson /* This will set the xid of the NEXT call */
693dfdcada3SDoug Rabson /* decrement by 1 as clnt_vc_call() increments once */
694dfdcada3SDoug Rabson ct->ct_xid = *(uint32_t *)info - 1;
695dfdcada3SDoug Rabson break;
696dfdcada3SDoug Rabson case CLGET_VERS:
697dfdcada3SDoug Rabson /*
698dfdcada3SDoug Rabson * This RELIES on the information that, in the call body,
699dfdcada3SDoug Rabson * the version number field is the fifth field from the
7006244c6e7SPedro F. Giffuni * beginning of the RPC header. MUST be changed if the
701dfdcada3SDoug Rabson * call_struct is changed
702dfdcada3SDoug Rabson */
703dfdcada3SDoug Rabson *(uint32_t *)info =
704dfdcada3SDoug Rabson ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
705dfdcada3SDoug Rabson 4 * BYTES_PER_XDR_UNIT));
706dfdcada3SDoug Rabson break;
707dfdcada3SDoug Rabson
708dfdcada3SDoug Rabson case CLSET_VERS:
709dfdcada3SDoug Rabson *(uint32_t *)(void *)(ct->ct_mcallc +
710dfdcada3SDoug Rabson 4 * BYTES_PER_XDR_UNIT) =
711dfdcada3SDoug Rabson htonl(*(uint32_t *)info);
712dfdcada3SDoug Rabson break;
713dfdcada3SDoug Rabson
714dfdcada3SDoug Rabson case CLGET_PROG:
715dfdcada3SDoug Rabson /*
716dfdcada3SDoug Rabson * This RELIES on the information that, in the call body,
717dfdcada3SDoug Rabson * the program number field is the fourth field from the
7186244c6e7SPedro F. Giffuni * beginning of the RPC header. MUST be changed if the
719dfdcada3SDoug Rabson * call_struct is changed
720dfdcada3SDoug Rabson */
721dfdcada3SDoug Rabson *(uint32_t *)info =
722dfdcada3SDoug Rabson ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
723dfdcada3SDoug Rabson 3 * BYTES_PER_XDR_UNIT));
724dfdcada3SDoug Rabson break;
725dfdcada3SDoug Rabson
726dfdcada3SDoug Rabson case CLSET_PROG:
727dfdcada3SDoug Rabson *(uint32_t *)(void *)(ct->ct_mcallc +
728dfdcada3SDoug Rabson 3 * BYTES_PER_XDR_UNIT) =
729dfdcada3SDoug Rabson htonl(*(uint32_t *)info);
730dfdcada3SDoug Rabson break;
731dfdcada3SDoug Rabson
732dfdcada3SDoug Rabson case CLSET_WAITCHAN:
733a9148abdSDoug Rabson ct->ct_waitchan = (const char *)info;
734dfdcada3SDoug Rabson break;
735dfdcada3SDoug Rabson
736dfdcada3SDoug Rabson case CLGET_WAITCHAN:
737dfdcada3SDoug Rabson *(const char **) info = ct->ct_waitchan;
738dfdcada3SDoug Rabson break;
739dfdcada3SDoug Rabson
740dfdcada3SDoug Rabson case CLSET_INTERRUPTIBLE:
741dfdcada3SDoug Rabson if (*(int *) info)
7423b14c753SJohn Baldwin ct->ct_waitflag = PCATCH;
743dfdcada3SDoug Rabson else
744dfdcada3SDoug Rabson ct->ct_waitflag = 0;
745dfdcada3SDoug Rabson break;
746dfdcada3SDoug Rabson
747dfdcada3SDoug Rabson case CLGET_INTERRUPTIBLE:
748dfdcada3SDoug Rabson if (ct->ct_waitflag)
749dfdcada3SDoug Rabson *(int *) info = TRUE;
750dfdcada3SDoug Rabson else
751dfdcada3SDoug Rabson *(int *) info = FALSE;
752dfdcada3SDoug Rabson break;
753dfdcada3SDoug Rabson
754e2adc47dSRick Macklem case CLSET_BACKCHANNEL:
755e2adc47dSRick Macklem xprt = (SVCXPRT *)info;
756e2adc47dSRick Macklem if (ct->ct_backchannelxprt == NULL) {
757*4ba444deSRick Macklem SVC_ACQUIRE(xprt);
758e2adc47dSRick Macklem xprt->xp_p2 = ct;
759ab0c29afSRick Macklem if (ct->ct_sslrefno != 0)
760ab0c29afSRick Macklem xprt->xp_tls = RPCTLS_FLAGS_HANDSHAKE;
761e2adc47dSRick Macklem ct->ct_backchannelxprt = xprt;
762e2adc47dSRick Macklem }
763e2adc47dSRick Macklem break;
764e2adc47dSRick Macklem
765ab0c29afSRick Macklem case CLSET_TLS:
766ab0c29afSRick Macklem p = (uint64_t *)info;
767ab0c29afSRick Macklem ct->ct_sslsec = *p++;
768ab0c29afSRick Macklem ct->ct_sslusec = *p++;
769ab0c29afSRick Macklem ct->ct_sslrefno = *p;
770ab0c29afSRick Macklem if (ct->ct_sslrefno != RPCTLS_REFNO_HANDSHAKE) {
771*4ba444deSRick Macklem /* cl ref cnt is released by clnt_vc_dotlsupcall(). */
772*4ba444deSRick Macklem CLNT_ACQUIRE(cl);
773ab0c29afSRick Macklem mtx_unlock(&ct->ct_lock);
774ab0c29afSRick Macklem /* Start the kthread that handles upcalls. */
775*4ba444deSRick Macklem error = kthread_add(clnt_vc_dotlsupcall, cl,
776ab0c29afSRick Macklem NULL, NULL, 0, 0, "krpctls%u", thrdnum++);
777ab0c29afSRick Macklem if (error != 0)
778ab0c29afSRick Macklem panic("Can't add KRPC thread error %d", error);
779ab0c29afSRick Macklem } else
780ab0c29afSRick Macklem mtx_unlock(&ct->ct_lock);
781ab0c29afSRick Macklem return (TRUE);
782ab0c29afSRick Macklem
783ab0c29afSRick Macklem case CLSET_BLOCKRCV:
784ab0c29afSRick Macklem if (*(int *) info) {
785ab0c29afSRick Macklem ct->ct_rcvstate &= ~RPCRCVSTATE_NORMAL;
786ab0c29afSRick Macklem ct->ct_rcvstate |= RPCRCVSTATE_TLSHANDSHAKE;
787ab0c29afSRick Macklem } else {
788ab0c29afSRick Macklem ct->ct_rcvstate &= ~RPCRCVSTATE_TLSHANDSHAKE;
789ab0c29afSRick Macklem ct->ct_rcvstate |= RPCRCVSTATE_NORMAL;
790ab0c29afSRick Macklem }
791ab0c29afSRick Macklem break;
792ab0c29afSRick Macklem
793dfdcada3SDoug Rabson default:
794dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
795dfdcada3SDoug Rabson return (FALSE);
796dfdcada3SDoug Rabson }
797dfdcada3SDoug Rabson
798dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
799dfdcada3SDoug Rabson return (TRUE);
800dfdcada3SDoug Rabson }
801dfdcada3SDoug Rabson
802dfdcada3SDoug Rabson static void
clnt_vc_close(CLIENT * cl)803a9148abdSDoug Rabson clnt_vc_close(CLIENT *cl)
804dfdcada3SDoug Rabson {
805dfdcada3SDoug Rabson struct ct_data *ct = (struct ct_data *) cl->cl_private;
806c675522fSDoug Rabson struct ct_request *cr;
807dfdcada3SDoug Rabson
808dfdcada3SDoug Rabson mtx_lock(&ct->ct_lock);
809dfdcada3SDoug Rabson
810a9148abdSDoug Rabson if (ct->ct_closed) {
811a9148abdSDoug Rabson mtx_unlock(&ct->ct_lock);
812a9148abdSDoug Rabson return;
813a9148abdSDoug Rabson }
814a9148abdSDoug Rabson
815a9148abdSDoug Rabson if (ct->ct_closing) {
816a9148abdSDoug Rabson while (ct->ct_closing)
817a9148abdSDoug Rabson msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
818a9148abdSDoug Rabson KASSERT(ct->ct_closed, ("client should be closed"));
819a9148abdSDoug Rabson mtx_unlock(&ct->ct_lock);
820a9148abdSDoug Rabson return;
821a9148abdSDoug Rabson }
822a9148abdSDoug Rabson
823dfdcada3SDoug Rabson if (ct->ct_socket) {
82474fb0ba7SJohn Baldwin ct->ct_closing = TRUE;
82574fb0ba7SJohn Baldwin mtx_unlock(&ct->ct_lock);
82674fb0ba7SJohn Baldwin
827e205fd31SGleb Smirnoff SOCK_RECVBUF_LOCK(ct->ct_socket);
828ab0c29afSRick Macklem if (ct->ct_socket->so_rcv.sb_upcall != NULL) {
82974fb0ba7SJohn Baldwin soupcall_clear(ct->ct_socket, SO_RCV);
8303144f812SRick Macklem clnt_vc_upcallsdone(ct);
831ab0c29afSRick Macklem }
832e205fd31SGleb Smirnoff SOCK_RECVBUF_UNLOCK(ct->ct_socket);
833dfdcada3SDoug Rabson
834c675522fSDoug Rabson /*
835c675522fSDoug Rabson * Abort any pending requests and wait until everyone
836c675522fSDoug Rabson * has finished with clnt_vc_call.
837c675522fSDoug Rabson */
83874fb0ba7SJohn Baldwin mtx_lock(&ct->ct_lock);
839c675522fSDoug Rabson TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
840c675522fSDoug Rabson cr->cr_xid = 0;
841c675522fSDoug Rabson cr->cr_error = ESHUTDOWN;
842c675522fSDoug Rabson wakeup(cr);
843c675522fSDoug Rabson }
844c675522fSDoug Rabson
845c675522fSDoug Rabson while (ct->ct_threads)
846c675522fSDoug Rabson msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
847a9148abdSDoug Rabson }
848dfdcada3SDoug Rabson
849a9148abdSDoug Rabson ct->ct_closing = FALSE;
850a9148abdSDoug Rabson ct->ct_closed = TRUE;
851ab0c29afSRick Macklem wakeup(&ct->ct_sslrefno);
852a9148abdSDoug Rabson mtx_unlock(&ct->ct_lock);
853a9148abdSDoug Rabson wakeup(ct);
854a9148abdSDoug Rabson }
855a9148abdSDoug Rabson
856a9148abdSDoug Rabson static void
clnt_vc_destroy(CLIENT * cl)857a9148abdSDoug Rabson clnt_vc_destroy(CLIENT *cl)
858a9148abdSDoug Rabson {
859a9148abdSDoug Rabson struct ct_data *ct = (struct ct_data *) cl->cl_private;
860a9148abdSDoug Rabson struct socket *so = NULL;
861e2adc47dSRick Macklem SVCXPRT *xprt;
862ab0c29afSRick Macklem uint32_t reterr;
863a9148abdSDoug Rabson
864a9148abdSDoug Rabson clnt_vc_close(cl);
865a9148abdSDoug Rabson
866a9148abdSDoug Rabson mtx_lock(&ct->ct_lock);
867e2adc47dSRick Macklem xprt = ct->ct_backchannelxprt;
868e2adc47dSRick Macklem ct->ct_backchannelxprt = NULL;
869e2adc47dSRick Macklem if (xprt != NULL) {
870e2adc47dSRick Macklem mtx_unlock(&ct->ct_lock); /* To avoid a LOR. */
871e2adc47dSRick Macklem sx_xlock(&xprt->xp_lock);
872e2adc47dSRick Macklem mtx_lock(&ct->ct_lock);
873e2adc47dSRick Macklem xprt->xp_p2 = NULL;
87434f1fddbSRick Macklem sx_xunlock(&xprt->xp_lock);
875*4ba444deSRick Macklem SVC_RELEASE(xprt);
876e2adc47dSRick Macklem }
877a9148abdSDoug Rabson
878a9148abdSDoug Rabson if (ct->ct_socket) {
879dfdcada3SDoug Rabson if (ct->ct_closeit) {
880dfdcada3SDoug Rabson so = ct->ct_socket;
881dfdcada3SDoug Rabson }
882dfdcada3SDoug Rabson }
883dfdcada3SDoug Rabson
884ab0c29afSRick Macklem /* Wait for the upcall kthread to terminate. */
885ab0c29afSRick Macklem while ((ct->ct_rcvstate & RPCRCVSTATE_UPCALLTHREAD) != 0)
886ab0c29afSRick Macklem msleep(&ct->ct_sslrefno, &ct->ct_lock, 0,
887ab0c29afSRick Macklem "clntvccl", hz);
888dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
889dfdcada3SDoug Rabson
890dfdcada3SDoug Rabson mtx_destroy(&ct->ct_lock);
891dfdcada3SDoug Rabson if (so) {
892ab0c29afSRick Macklem if (ct->ct_sslrefno != 0) {
893ab0c29afSRick Macklem /*
894ab0c29afSRick Macklem * If the TLS handshake is in progress, the upcall
895ab0c29afSRick Macklem * will fail, but the socket should be closed by the
896ab0c29afSRick Macklem * daemon, since the connect upcall has just failed.
897ab0c29afSRick Macklem */
898ab0c29afSRick Macklem if (ct->ct_sslrefno != RPCTLS_REFNO_HANDSHAKE) {
899ab0c29afSRick Macklem /*
900ab0c29afSRick Macklem * If the upcall fails, the socket has
901ab0c29afSRick Macklem * probably been closed via the rpctlscd
902ab0c29afSRick Macklem * daemon having crashed or been
903ab0c29afSRick Macklem * restarted, so ignore return stat.
904ab0c29afSRick Macklem */
90577bc5890SWarner Losh rpctls_cl_disconnect(ct->ct_sslsec,
906ab0c29afSRick Macklem ct->ct_sslusec, ct->ct_sslrefno,
907ab0c29afSRick Macklem &reterr);
908ab0c29afSRick Macklem }
909ab0c29afSRick Macklem /* Must sorele() to get rid of reference. */
910ab0c29afSRick Macklem CURVNET_SET(so->so_vnet);
911ab0c29afSRick Macklem sorele(so);
912ab0c29afSRick Macklem CURVNET_RESTORE();
913ab0c29afSRick Macklem } else {
914dfdcada3SDoug Rabson soshutdown(so, SHUT_WR);
915dfdcada3SDoug Rabson soclose(so);
916dfdcada3SDoug Rabson }
917ab0c29afSRick Macklem }
9184302e8b6SRick Macklem m_freem(ct->ct_record);
9194302e8b6SRick Macklem m_freem(ct->ct_raw);
920dfdcada3SDoug Rabson mem_free(ct, sizeof(struct ct_data));
9210c2222baSPedro F. Giffuni if (cl->cl_netid && cl->cl_netid[0])
9220c2222baSPedro F. Giffuni mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
9230c2222baSPedro F. Giffuni if (cl->cl_tp && cl->cl_tp[0])
9240c2222baSPedro F. Giffuni mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
925dfdcada3SDoug Rabson mem_free(cl, sizeof(CLIENT));
926dfdcada3SDoug Rabson }
927dfdcada3SDoug Rabson
928dfdcada3SDoug Rabson /*
929dfdcada3SDoug Rabson * Make sure that the time is not garbage. -1 value is disallowed.
930dfdcada3SDoug Rabson * Note this is different from time_not_ok in clnt_dg.c
931dfdcada3SDoug Rabson */
932dfdcada3SDoug Rabson static bool_t
time_not_ok(struct timeval * t)933dfdcada3SDoug Rabson time_not_ok(struct timeval *t)
934dfdcada3SDoug Rabson {
935dfdcada3SDoug Rabson return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
936dfdcada3SDoug Rabson t->tv_usec <= -1 || t->tv_usec > 1000000);
937dfdcada3SDoug Rabson }
938dfdcada3SDoug Rabson
93974fb0ba7SJohn Baldwin int
clnt_vc_soupcall(struct socket * so,void * arg,int waitflag)940dfdcada3SDoug Rabson clnt_vc_soupcall(struct socket *so, void *arg, int waitflag)
941dfdcada3SDoug Rabson {
942dfdcada3SDoug Rabson struct ct_data *ct = (struct ct_data *) arg;
943dfdcada3SDoug Rabson struct uio uio;
9440b4f2ab0SRick Macklem struct mbuf *m, *m2;
945dfdcada3SDoug Rabson struct ct_request *cr;
946dfdcada3SDoug Rabson int error, rcvflag, foundreq;
947e2adc47dSRick Macklem uint32_t xid_plus_direction[2], header;
948e2adc47dSRick Macklem SVCXPRT *xprt;
949e2adc47dSRick Macklem struct cf_conn *cd;
9504302e8b6SRick Macklem u_int rawlen;
951ab0c29afSRick Macklem struct cmsghdr *cmsg;
952ab0c29afSRick Macklem struct tls_get_record tgr;
953ab0c29afSRick Macklem
954ab0c29afSRick Macklem /*
955ab0c29afSRick Macklem * RPC-over-TLS needs to block reception during
956ab0c29afSRick Macklem * upcalls since the upcall will be doing I/O on
957ab0c29afSRick Macklem * the socket via openssl library calls.
958ab0c29afSRick Macklem */
959ab0c29afSRick Macklem mtx_lock(&ct->ct_lock);
960ab0c29afSRick Macklem if ((ct->ct_rcvstate & (RPCRCVSTATE_NORMAL |
961ab0c29afSRick Macklem RPCRCVSTATE_NONAPPDATA)) == 0) {
962ab0c29afSRick Macklem /* Mark that a socket upcall needs to be done. */
963ab0c29afSRick Macklem if ((ct->ct_rcvstate & (RPCRCVSTATE_UPCALLNEEDED |
964ab0c29afSRick Macklem RPCRCVSTATE_UPCALLINPROG)) != 0)
965ab0c29afSRick Macklem ct->ct_rcvstate |= RPCRCVSTATE_SOUPCALLNEEDED;
966ab0c29afSRick Macklem mtx_unlock(&ct->ct_lock);
967ab0c29afSRick Macklem return (SU_OK);
968ab0c29afSRick Macklem }
969ab0c29afSRick Macklem mtx_unlock(&ct->ct_lock);
970dfdcada3SDoug Rabson
9714302e8b6SRick Macklem /*
9724302e8b6SRick Macklem * If another thread is already here, it must be in
9734302e8b6SRick Macklem * soreceive(), so just return to avoid races with it.
974e205fd31SGleb Smirnoff * ct_upcallrefs is protected by the socket receive buffer lock
9754302e8b6SRick Macklem * which is held in this function, except when
9764302e8b6SRick Macklem * soreceive() is called.
9774302e8b6SRick Macklem */
9784302e8b6SRick Macklem if (ct->ct_upcallrefs > 0)
9794302e8b6SRick Macklem return (SU_OK);
9803144f812SRick Macklem ct->ct_upcallrefs++;
9814302e8b6SRick Macklem
9824302e8b6SRick Macklem /*
9834302e8b6SRick Macklem * Read as much as possible off the socket and link it
9844302e8b6SRick Macklem * onto ct_raw.
9854302e8b6SRick Macklem */
9864302e8b6SRick Macklem for (;;) {
9874302e8b6SRick Macklem uio.uio_resid = 1000000000;
988dfdcada3SDoug Rabson uio.uio_td = curthread;
9894302e8b6SRick Macklem m2 = m = NULL;
990dfdcada3SDoug Rabson rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
991ab0c29afSRick Macklem if (ct->ct_sslrefno != 0 && (ct->ct_rcvstate &
9920b4f2ab0SRick Macklem RPCRCVSTATE_NORMAL) != 0)
993ab0c29afSRick Macklem rcvflag |= MSG_TLSAPPDATA;
994e205fd31SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so);
9950b4f2ab0SRick Macklem error = soreceive(so, NULL, &uio, &m, &m2, &rcvflag);
996e205fd31SGleb Smirnoff SOCK_RECVBUF_LOCK(so);
997dfdcada3SDoug Rabson
9984302e8b6SRick Macklem if (error == EWOULDBLOCK) {
999dfdcada3SDoug Rabson /*
10004302e8b6SRick Macklem * We must re-test for readability after
10014302e8b6SRick Macklem * taking the lock to protect us in the case
10024302e8b6SRick Macklem * where a new packet arrives on the socket
10034302e8b6SRick Macklem * after our call to soreceive fails with
10044302e8b6SRick Macklem * EWOULDBLOCK.
1005dfdcada3SDoug Rabson */
10064302e8b6SRick Macklem error = 0;
10074302e8b6SRick Macklem if (!soreadable(so))
10084302e8b6SRick Macklem break;
10094302e8b6SRick Macklem continue;
10104302e8b6SRick Macklem }
10114302e8b6SRick Macklem if (error == 0 && m == NULL) {
1012dfdcada3SDoug Rabson /*
1013dfdcada3SDoug Rabson * We must have got EOF trying
1014dfdcada3SDoug Rabson * to read from the stream.
1015dfdcada3SDoug Rabson */
1016dfdcada3SDoug Rabson error = ECONNRESET;
1017dfdcada3SDoug Rabson }
1018ab0c29afSRick Macklem
1019ab0c29afSRick Macklem /*
10200b4f2ab0SRick Macklem * A return of ENXIO indicates that there is an
10210b4f2ab0SRick Macklem * alert record at the head of the
1022ab0c29afSRick Macklem * socket's receive queue, for TLS connections.
1023ab0c29afSRick Macklem * This record needs to be handled in userland
1024ab0c29afSRick Macklem * via an SSL_read() call, so do an upcall to the daemon.
1025ab0c29afSRick Macklem */
1026ab0c29afSRick Macklem if (ct->ct_sslrefno != 0 && error == ENXIO) {
1027ab0c29afSRick Macklem /* Disable reception, marking an upcall needed. */
1028ab0c29afSRick Macklem mtx_lock(&ct->ct_lock);
1029ab0c29afSRick Macklem ct->ct_rcvstate |= RPCRCVSTATE_UPCALLNEEDED;
1030ab0c29afSRick Macklem /*
1031ab0c29afSRick Macklem * If an upcall in needed, wake up the kthread
1032ab0c29afSRick Macklem * that runs clnt_vc_dotlsupcall().
1033ab0c29afSRick Macklem */
1034ab0c29afSRick Macklem wakeup(&ct->ct_sslrefno);
1035ab0c29afSRick Macklem mtx_unlock(&ct->ct_lock);
1036ab0c29afSRick Macklem break;
1037ab0c29afSRick Macklem }
10384302e8b6SRick Macklem if (error != 0)
1039dfdcada3SDoug Rabson break;
10404302e8b6SRick Macklem
1041ab0c29afSRick Macklem /* Process any record header(s). */
1042ab0c29afSRick Macklem if (m2 != NULL) {
1043ab0c29afSRick Macklem cmsg = mtod(m2, struct cmsghdr *);
1044ab0c29afSRick Macklem if (cmsg->cmsg_type == TLS_GET_RECORD &&
1045ab0c29afSRick Macklem cmsg->cmsg_len == CMSG_LEN(sizeof(tgr))) {
1046ab0c29afSRick Macklem memcpy(&tgr, CMSG_DATA(cmsg), sizeof(tgr));
1047ab0c29afSRick Macklem /*
10480b4f2ab0SRick Macklem * TLS_RLTYPE_ALERT records should be handled
10490b4f2ab0SRick Macklem * since soreceive() would have returned
10500b4f2ab0SRick Macklem * ENXIO. Just throw any other
10510b4f2ab0SRick Macklem * non-TLS_RLTYPE_APP records away.
1052ab0c29afSRick Macklem */
1053ab0c29afSRick Macklem if (tgr.tls_type != TLS_RLTYPE_APP) {
1054ab0c29afSRick Macklem m_freem(m);
1055ab0c29afSRick Macklem m_free(m2);
1056ab0c29afSRick Macklem mtx_lock(&ct->ct_lock);
1057ab0c29afSRick Macklem ct->ct_rcvstate &=
1058ab0c29afSRick Macklem ~RPCRCVSTATE_NONAPPDATA;
1059ab0c29afSRick Macklem ct->ct_rcvstate |= RPCRCVSTATE_NORMAL;
1060ab0c29afSRick Macklem mtx_unlock(&ct->ct_lock);
1061ab0c29afSRick Macklem continue;
1062ab0c29afSRick Macklem }
1063ab0c29afSRick Macklem }
1064ab0c29afSRick Macklem m_free(m2);
1065ab0c29afSRick Macklem }
1066ab0c29afSRick Macklem
10674302e8b6SRick Macklem if (ct->ct_raw != NULL)
10684302e8b6SRick Macklem m_last(ct->ct_raw)->m_next = m;
10694302e8b6SRick Macklem else
10704302e8b6SRick Macklem ct->ct_raw = m;
1071dfdcada3SDoug Rabson }
10724302e8b6SRick Macklem rawlen = m_length(ct->ct_raw, NULL);
10734302e8b6SRick Macklem
10744302e8b6SRick Macklem /* Now, process as much of ct_raw as possible. */
10754302e8b6SRick Macklem for (;;) {
10764302e8b6SRick Macklem /*
10774302e8b6SRick Macklem * If ct_record_resid is zero, we are waiting for a
10784302e8b6SRick Macklem * record mark.
10794302e8b6SRick Macklem */
10804302e8b6SRick Macklem if (ct->ct_record_resid == 0) {
10814302e8b6SRick Macklem if (rawlen < sizeof(uint32_t))
10824302e8b6SRick Macklem break;
10834302e8b6SRick Macklem m_copydata(ct->ct_raw, 0, sizeof(uint32_t),
10844302e8b6SRick Macklem (char *)&header);
1085dfdcada3SDoug Rabson header = ntohl(header);
1086dfdcada3SDoug Rabson ct->ct_record_resid = header & 0x7fffffff;
1087dfdcada3SDoug Rabson ct->ct_record_eor = ((header & 0x80000000) != 0);
10884302e8b6SRick Macklem m_adj(ct->ct_raw, sizeof(uint32_t));
10894302e8b6SRick Macklem rawlen -= sizeof(uint32_t);
1090dfdcada3SDoug Rabson } else {
1091dfdcada3SDoug Rabson /*
10924302e8b6SRick Macklem * Move as much of the record as possible to
10934302e8b6SRick Macklem * ct_record.
1094a9148abdSDoug Rabson */
10954302e8b6SRick Macklem if (rawlen == 0)
10963144f812SRick Macklem break;
10974302e8b6SRick Macklem if (rawlen <= ct->ct_record_resid) {
10984302e8b6SRick Macklem if (ct->ct_record != NULL)
10994302e8b6SRick Macklem m_last(ct->ct_record)->m_next =
11004302e8b6SRick Macklem ct->ct_raw;
1101dfdcada3SDoug Rabson else
11024302e8b6SRick Macklem ct->ct_record = ct->ct_raw;
11034302e8b6SRick Macklem ct->ct_raw = NULL;
11044302e8b6SRick Macklem ct->ct_record_resid -= rawlen;
11054302e8b6SRick Macklem rawlen = 0;
11064302e8b6SRick Macklem } else {
11074302e8b6SRick Macklem m = m_split(ct->ct_raw, ct->ct_record_resid,
11084302e8b6SRick Macklem M_NOWAIT);
11094302e8b6SRick Macklem if (m == NULL)
11104302e8b6SRick Macklem break;
11114302e8b6SRick Macklem if (ct->ct_record != NULL)
11124302e8b6SRick Macklem m_last(ct->ct_record)->m_next =
11134302e8b6SRick Macklem ct->ct_raw;
11144302e8b6SRick Macklem else
11154302e8b6SRick Macklem ct->ct_record = ct->ct_raw;
11164302e8b6SRick Macklem rawlen -= ct->ct_record_resid;
11174302e8b6SRick Macklem ct->ct_record_resid = 0;
11184302e8b6SRick Macklem ct->ct_raw = m;
11194302e8b6SRick Macklem }
11204302e8b6SRick Macklem if (ct->ct_record_resid > 0)
11214302e8b6SRick Macklem break;
1122dfdcada3SDoug Rabson
1123dfdcada3SDoug Rabson /*
1124dfdcada3SDoug Rabson * If we have the entire record, see if we can
1125dfdcada3SDoug Rabson * match it to a request.
1126dfdcada3SDoug Rabson */
11274302e8b6SRick Macklem if (ct->ct_record_eor) {
1128dfdcada3SDoug Rabson /*
1129dfdcada3SDoug Rabson * The XID is in the first uint32_t of
1130e2adc47dSRick Macklem * the reply and the message direction
1131e2adc47dSRick Macklem * is the second one.
1132dfdcada3SDoug Rabson */
1133e2adc47dSRick Macklem if (ct->ct_record->m_len <
1134e2adc47dSRick Macklem sizeof(xid_plus_direction) &&
11355e8eb3cdSRick Macklem m_length(ct->ct_record, NULL) <
1136e2adc47dSRick Macklem sizeof(xid_plus_direction)) {
11374302e8b6SRick Macklem /*
11384302e8b6SRick Macklem * What to do now?
11394302e8b6SRick Macklem * The data in the TCP stream is
11404302e8b6SRick Macklem * corrupted such that there is no
11414302e8b6SRick Macklem * valid RPC message to parse.
11424302e8b6SRick Macklem * I think it best to close this
11434302e8b6SRick Macklem * connection and allow
1144b94b9a80SRick Macklem * clnt_reconnect_call() to try
11454302e8b6SRick Macklem * and establish a new one.
11464302e8b6SRick Macklem */
11474302e8b6SRick Macklem printf("clnt_vc_soupcall: "
11484302e8b6SRick Macklem "connection data corrupted\n");
11494302e8b6SRick Macklem error = ECONNRESET;
11504302e8b6SRick Macklem goto wakeup_all;
11515e8eb3cdSRick Macklem }
1152e2adc47dSRick Macklem m_copydata(ct->ct_record, 0,
1153e2adc47dSRick Macklem sizeof(xid_plus_direction),
1154e2adc47dSRick Macklem (char *)xid_plus_direction);
1155e2adc47dSRick Macklem xid_plus_direction[0] =
1156e2adc47dSRick Macklem ntohl(xid_plus_direction[0]);
1157e2adc47dSRick Macklem xid_plus_direction[1] =
1158e2adc47dSRick Macklem ntohl(xid_plus_direction[1]);
1159e2adc47dSRick Macklem /* Check message direction. */
1160e2adc47dSRick Macklem if (xid_plus_direction[1] == CALL) {
1161e2adc47dSRick Macklem /* This is a backchannel request. */
1162e2adc47dSRick Macklem mtx_lock(&ct->ct_lock);
1163e2adc47dSRick Macklem xprt = ct->ct_backchannelxprt;
1164e2adc47dSRick Macklem if (xprt == NULL) {
1165e2adc47dSRick Macklem mtx_unlock(&ct->ct_lock);
1166e2adc47dSRick Macklem /* Just throw it away. */
1167e2adc47dSRick Macklem m_freem(ct->ct_record);
1168e2adc47dSRick Macklem ct->ct_record = NULL;
1169e2adc47dSRick Macklem } else {
1170e2adc47dSRick Macklem cd = (struct cf_conn *)
1171e2adc47dSRick Macklem xprt->xp_p1;
1172e2adc47dSRick Macklem m2 = cd->mreq;
1173e2adc47dSRick Macklem /*
1174e2adc47dSRick Macklem * The requests are chained
1175e2adc47dSRick Macklem * in the m_nextpkt list.
1176e2adc47dSRick Macklem */
1177e2adc47dSRick Macklem while (m2 != NULL &&
1178e2adc47dSRick Macklem m2->m_nextpkt != NULL)
1179e2adc47dSRick Macklem /* Find end of list. */
1180e2adc47dSRick Macklem m2 = m2->m_nextpkt;
1181e2adc47dSRick Macklem if (m2 != NULL)
1182e2adc47dSRick Macklem m2->m_nextpkt =
1183e2adc47dSRick Macklem ct->ct_record;
1184e2adc47dSRick Macklem else
1185e2adc47dSRick Macklem cd->mreq =
1186e2adc47dSRick Macklem ct->ct_record;
1187e2adc47dSRick Macklem ct->ct_record->m_nextpkt =
1188e2adc47dSRick Macklem NULL;
1189e2adc47dSRick Macklem ct->ct_record = NULL;
1190e2adc47dSRick Macklem xprt_active(xprt);
1191e2adc47dSRick Macklem mtx_unlock(&ct->ct_lock);
1192e2adc47dSRick Macklem }
1193e2adc47dSRick Macklem } else {
1194dfdcada3SDoug Rabson mtx_lock(&ct->ct_lock);
1195dfdcada3SDoug Rabson foundreq = 0;
1196e2adc47dSRick Macklem TAILQ_FOREACH(cr, &ct->ct_pending,
1197e2adc47dSRick Macklem cr_link) {
1198e2adc47dSRick Macklem if (cr->cr_xid ==
1199e2adc47dSRick Macklem xid_plus_direction[0]) {
1200dfdcada3SDoug Rabson /*
1201dfdcada3SDoug Rabson * This one
1202c675522fSDoug Rabson * matches. We leave
1203c675522fSDoug Rabson * the reply mbuf in
1204dfdcada3SDoug Rabson * cr->cr_mrep. Set
1205dfdcada3SDoug Rabson * the XID to zero so
1206c675522fSDoug Rabson * that we will ignore
1207e2adc47dSRick Macklem * any duplicated
1208c675522fSDoug Rabson * replies.
1209dfdcada3SDoug Rabson */
1210dfdcada3SDoug Rabson cr->cr_xid = 0;
1211e2adc47dSRick Macklem cr->cr_mrep =
1212e2adc47dSRick Macklem ct->ct_record;
1213dfdcada3SDoug Rabson cr->cr_error = 0;
1214dfdcada3SDoug Rabson foundreq = 1;
1215dfdcada3SDoug Rabson wakeup(cr);
1216dfdcada3SDoug Rabson break;
1217dfdcada3SDoug Rabson }
1218dfdcada3SDoug Rabson }
1219dfdcada3SDoug Rabson mtx_unlock(&ct->ct_lock);
1220dfdcada3SDoug Rabson
1221dfdcada3SDoug Rabson if (!foundreq)
1222dfdcada3SDoug Rabson m_freem(ct->ct_record);
1223dfdcada3SDoug Rabson ct->ct_record = NULL;
1224dfdcada3SDoug Rabson }
1225dfdcada3SDoug Rabson }
1226e2adc47dSRick Macklem }
12274302e8b6SRick Macklem }
12284302e8b6SRick Macklem
12294302e8b6SRick Macklem if (error != 0) {
12304302e8b6SRick Macklem wakeup_all:
12314302e8b6SRick Macklem /*
12324302e8b6SRick Macklem * This socket is broken, so mark that it cannot
12334302e8b6SRick Macklem * receive and fail all RPCs waiting for a reply
12344302e8b6SRick Macklem * on it, so that they will be retried on a new
12354302e8b6SRick Macklem * TCP connection created by clnt_reconnect_X().
12364302e8b6SRick Macklem */
12374302e8b6SRick Macklem mtx_lock(&ct->ct_lock);
12384302e8b6SRick Macklem ct->ct_error.re_status = RPC_CANTRECV;
12394302e8b6SRick Macklem ct->ct_error.re_errno = error;
12404302e8b6SRick Macklem TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
12414302e8b6SRick Macklem cr->cr_error = error;
12424302e8b6SRick Macklem wakeup(cr);
12434302e8b6SRick Macklem }
12444302e8b6SRick Macklem mtx_unlock(&ct->ct_lock);
12454302e8b6SRick Macklem }
12464302e8b6SRick Macklem
12473144f812SRick Macklem ct->ct_upcallrefs--;
12483144f812SRick Macklem if (ct->ct_upcallrefs < 0)
12493144f812SRick Macklem panic("rpcvc upcall refcnt");
12503144f812SRick Macklem if (ct->ct_upcallrefs == 0)
12513144f812SRick Macklem wakeup(&ct->ct_upcallrefs);
125274fb0ba7SJohn Baldwin return (SU_OK);
1253dfdcada3SDoug Rabson }
12543144f812SRick Macklem
12553144f812SRick Macklem /*
12563144f812SRick Macklem * Wait for all upcalls in progress to complete.
12573144f812SRick Macklem */
12583144f812SRick Macklem static void
clnt_vc_upcallsdone(struct ct_data * ct)12593144f812SRick Macklem clnt_vc_upcallsdone(struct ct_data *ct)
12603144f812SRick Macklem {
12613144f812SRick Macklem
1262e205fd31SGleb Smirnoff SOCK_RECVBUF_LOCK_ASSERT(ct->ct_socket);
12633144f812SRick Macklem
12643144f812SRick Macklem while (ct->ct_upcallrefs > 0)
12653144f812SRick Macklem (void) msleep(&ct->ct_upcallrefs,
12663144f812SRick Macklem SOCKBUF_MTX(&ct->ct_socket->so_rcv), 0, "rpcvcup", 0);
12673144f812SRick Macklem }
1268ab0c29afSRick Macklem
1269ab0c29afSRick Macklem /*
1270ab0c29afSRick Macklem * Do a TLS upcall to the rpctlscd daemon, as required.
1271ab0c29afSRick Macklem * This function runs as a kthread.
1272ab0c29afSRick Macklem */
1273ab0c29afSRick Macklem static void
clnt_vc_dotlsupcall(void * data)1274ab0c29afSRick Macklem clnt_vc_dotlsupcall(void *data)
1275ab0c29afSRick Macklem {
1276*4ba444deSRick Macklem CLIENT *cl = (CLIENT *)data;
1277*4ba444deSRick Macklem struct ct_data *ct = (struct ct_data *)cl->cl_private;
1278ab0c29afSRick Macklem enum clnt_stat ret;
1279ab0c29afSRick Macklem uint32_t reterr;
1280ab0c29afSRick Macklem
1281ab0c29afSRick Macklem mtx_lock(&ct->ct_lock);
1282ab0c29afSRick Macklem ct->ct_rcvstate |= RPCRCVSTATE_UPCALLTHREAD;
1283ab0c29afSRick Macklem while (!ct->ct_closed) {
1284ab0c29afSRick Macklem if ((ct->ct_rcvstate & RPCRCVSTATE_UPCALLNEEDED) != 0) {
1285ab0c29afSRick Macklem ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLNEEDED;
1286ab0c29afSRick Macklem ct->ct_rcvstate |= RPCRCVSTATE_UPCALLINPROG;
1287ab0c29afSRick Macklem if (ct->ct_sslrefno != 0 && ct->ct_sslrefno !=
1288ab0c29afSRick Macklem RPCTLS_REFNO_HANDSHAKE) {
1289ab0c29afSRick Macklem mtx_unlock(&ct->ct_lock);
1290ab0c29afSRick Macklem ret = rpctls_cl_handlerecord(ct->ct_sslsec,
1291ab0c29afSRick Macklem ct->ct_sslusec, ct->ct_sslrefno, &reterr);
1292ab0c29afSRick Macklem mtx_lock(&ct->ct_lock);
1293ab0c29afSRick Macklem }
1294ab0c29afSRick Macklem ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLINPROG;
1295ab0c29afSRick Macklem if (ret == RPC_SUCCESS && reterr == RPCTLSERR_OK)
1296ab0c29afSRick Macklem ct->ct_rcvstate |= RPCRCVSTATE_NORMAL;
1297ab0c29afSRick Macklem else
1298ab0c29afSRick Macklem ct->ct_rcvstate |= RPCRCVSTATE_NONAPPDATA;
1299ab0c29afSRick Macklem wakeup(&ct->ct_rcvstate);
1300ab0c29afSRick Macklem }
1301ab0c29afSRick Macklem if ((ct->ct_rcvstate & RPCRCVSTATE_SOUPCALLNEEDED) != 0) {
1302ab0c29afSRick Macklem ct->ct_rcvstate &= ~RPCRCVSTATE_SOUPCALLNEEDED;
1303ab0c29afSRick Macklem mtx_unlock(&ct->ct_lock);
1304e205fd31SGleb Smirnoff SOCK_RECVBUF_LOCK(ct->ct_socket);
1305ab0c29afSRick Macklem clnt_vc_soupcall(ct->ct_socket, ct, M_NOWAIT);
1306e205fd31SGleb Smirnoff SOCK_RECVBUF_UNLOCK(ct->ct_socket);
1307ab0c29afSRick Macklem mtx_lock(&ct->ct_lock);
1308ab0c29afSRick Macklem }
1309ab0c29afSRick Macklem msleep(&ct->ct_sslrefno, &ct->ct_lock, 0, "clntvcdu", hz);
1310ab0c29afSRick Macklem }
1311ab0c29afSRick Macklem ct->ct_rcvstate &= ~RPCRCVSTATE_UPCALLTHREAD;
1312ab0c29afSRick Macklem wakeup(&ct->ct_sslrefno);
1313ab0c29afSRick Macklem mtx_unlock(&ct->ct_lock);
1314*4ba444deSRick Macklem CLNT_RELEASE(cl);
1315ab0c29afSRick Macklem kthread_exit();
1316ab0c29afSRick Macklem }
1317