svc_vc.c revision 261050
1/*	$NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $	*/
2
3/*-
4 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 *   this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 *   this list of conditions and the following disclaimer in the documentation
13 *   and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 *   contributors may be used to endorse or promote products derived
16 *   from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#if defined(LIBC_SCCS) && !defined(lint)
32static char *sccsid2 = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
33static char *sccsid = "@(#)svc_tcp.c	2.2 88/08/01 4.0 RPCSRC";
34#endif
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: stable/10/sys/rpc/svc_vc.c 261050 2014-01-22 23:48:54Z mav $");
37
38/*
39 * svc_vc.c, Server side for Connection Oriented based RPC.
40 *
41 * Actually implements two flavors of transporter -
42 * a tcp rendezvouser (a listner and connection establisher)
43 * and a record/tcp stream.
44 */
45
46#include <sys/param.h>
47#include <sys/lock.h>
48#include <sys/kernel.h>
49#include <sys/malloc.h>
50#include <sys/mbuf.h>
51#include <sys/mutex.h>
52#include <sys/proc.h>
53#include <sys/protosw.h>
54#include <sys/queue.h>
55#include <sys/socket.h>
56#include <sys/socketvar.h>
57#include <sys/sx.h>
58#include <sys/systm.h>
59#include <sys/uio.h>
60
61#include <net/vnet.h>
62
63#include <netinet/tcp.h>
64
65#include <rpc/rpc.h>
66
67#include <rpc/krpc.h>
68#include <rpc/rpc_com.h>
69
70#include <security/mac/mac_framework.h>
71
72static bool_t svc_vc_rendezvous_recv(SVCXPRT *, struct rpc_msg *,
73    struct sockaddr **, struct mbuf **);
74static enum xprt_stat svc_vc_rendezvous_stat(SVCXPRT *);
75static void svc_vc_rendezvous_destroy(SVCXPRT *);
76static bool_t svc_vc_null(void);
77static void svc_vc_destroy(SVCXPRT *);
78static enum xprt_stat svc_vc_stat(SVCXPRT *);
79static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *,
80    struct sockaddr **, struct mbuf **);
81static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *,
82    struct sockaddr *, struct mbuf *);
83static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
84static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq,
85    void *in);
86static void svc_vc_backchannel_destroy(SVCXPRT *);
87static enum xprt_stat svc_vc_backchannel_stat(SVCXPRT *);
88static bool_t svc_vc_backchannel_recv(SVCXPRT *, struct rpc_msg *,
89    struct sockaddr **, struct mbuf **);
90static bool_t svc_vc_backchannel_reply(SVCXPRT *, struct rpc_msg *,
91    struct sockaddr *, struct mbuf *);
92static bool_t svc_vc_backchannel_control(SVCXPRT *xprt, const u_int rq,
93    void *in);
94static SVCXPRT *svc_vc_create_conn(SVCPOOL *pool, struct socket *so,
95    struct sockaddr *raddr);
96static int svc_vc_accept(struct socket *head, struct socket **sop);
97static int svc_vc_soupcall(struct socket *so, void *arg, int waitflag);
98
99static struct xp_ops svc_vc_rendezvous_ops = {
100	.xp_recv =	svc_vc_rendezvous_recv,
101	.xp_stat =	svc_vc_rendezvous_stat,
102	.xp_reply =	(bool_t (*)(SVCXPRT *, struct rpc_msg *,
103		struct sockaddr *, struct mbuf *))svc_vc_null,
104	.xp_destroy =	svc_vc_rendezvous_destroy,
105	.xp_control =	svc_vc_rendezvous_control
106};
107
108static struct xp_ops svc_vc_ops = {
109	.xp_recv =	svc_vc_recv,
110	.xp_stat =	svc_vc_stat,
111	.xp_reply =	svc_vc_reply,
112	.xp_destroy =	svc_vc_destroy,
113	.xp_control =	svc_vc_control
114};
115
116static struct xp_ops svc_vc_backchannel_ops = {
117	.xp_recv =	svc_vc_backchannel_recv,
118	.xp_stat =	svc_vc_backchannel_stat,
119	.xp_reply =	svc_vc_backchannel_reply,
120	.xp_destroy =	svc_vc_backchannel_destroy,
121	.xp_control =	svc_vc_backchannel_control
122};
123
124/*
125 * Usage:
126 *	xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
127 *
128 * Creates, registers, and returns a (rpc) tcp based transporter.
129 * Once *xprt is initialized, it is registered as a transporter
130 * see (svc.h, xprt_register).  This routine returns
131 * a NULL if a problem occurred.
132 *
133 * The filedescriptor passed in is expected to refer to a bound, but
134 * not yet connected socket.
135 *
136 * Since streams do buffered io similar to stdio, the caller can specify
137 * how big the send and receive buffers are via the second and third parms;
138 * 0 => use the system default.
139 */
140SVCXPRT *
141svc_vc_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
142    size_t recvsize)
143{
144	SVCXPRT *xprt;
145	struct sockaddr* sa;
146	int error;
147
148	SOCK_LOCK(so);
149	if (so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED)) {
150		SOCK_UNLOCK(so);
151		error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
152		if (error)
153			return (NULL);
154		xprt = svc_vc_create_conn(pool, so, sa);
155		free(sa, M_SONAME);
156		return (xprt);
157	}
158	SOCK_UNLOCK(so);
159
160	xprt = svc_xprt_alloc();
161	sx_init(&xprt->xp_lock, "xprt->xp_lock");
162	xprt->xp_pool = pool;
163	xprt->xp_socket = so;
164	xprt->xp_p1 = NULL;
165	xprt->xp_p2 = NULL;
166	xprt->xp_ops = &svc_vc_rendezvous_ops;
167
168	error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
169	if (error) {
170		goto cleanup_svc_vc_create;
171	}
172
173	memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
174	free(sa, M_SONAME);
175
176	xprt_register(xprt);
177
178	solisten(so, SOMAXCONN, curthread);
179
180	SOCKBUF_LOCK(&so->so_rcv);
181	xprt->xp_upcallset = 1;
182	soupcall_set(so, SO_RCV, svc_vc_soupcall, xprt);
183	SOCKBUF_UNLOCK(&so->so_rcv);
184
185	return (xprt);
186cleanup_svc_vc_create:
187	if (xprt)
188		svc_xprt_free(xprt);
189	return (NULL);
190}
191
192/*
193 * Create a new transport for a socket optained via soaccept().
194 */
195SVCXPRT *
196svc_vc_create_conn(SVCPOOL *pool, struct socket *so, struct sockaddr *raddr)
197{
198	SVCXPRT *xprt = NULL;
199	struct cf_conn *cd = NULL;
200	struct sockaddr* sa = NULL;
201	struct sockopt opt;
202	int one = 1;
203	int error;
204
205	bzero(&opt, sizeof(struct sockopt));
206	opt.sopt_dir = SOPT_SET;
207	opt.sopt_level = SOL_SOCKET;
208	opt.sopt_name = SO_KEEPALIVE;
209	opt.sopt_val = &one;
210	opt.sopt_valsize = sizeof(one);
211	error = sosetopt(so, &opt);
212	if (error) {
213		return (NULL);
214	}
215
216	if (so->so_proto->pr_protocol == IPPROTO_TCP) {
217		bzero(&opt, sizeof(struct sockopt));
218		opt.sopt_dir = SOPT_SET;
219		opt.sopt_level = IPPROTO_TCP;
220		opt.sopt_name = TCP_NODELAY;
221		opt.sopt_val = &one;
222		opt.sopt_valsize = sizeof(one);
223		error = sosetopt(so, &opt);
224		if (error) {
225			return (NULL);
226		}
227	}
228
229	cd = mem_alloc(sizeof(*cd));
230	cd->strm_stat = XPRT_IDLE;
231
232	xprt = svc_xprt_alloc();
233	sx_init(&xprt->xp_lock, "xprt->xp_lock");
234	xprt->xp_pool = pool;
235	xprt->xp_socket = so;
236	xprt->xp_p1 = cd;
237	xprt->xp_p2 = NULL;
238	xprt->xp_ops = &svc_vc_ops;
239
240	/*
241	 * See http://www.connectathon.org/talks96/nfstcp.pdf - client
242	 * has a 5 minute timer, server has a 6 minute timer.
243	 */
244	xprt->xp_idletimeout = 6 * 60;
245
246	memcpy(&xprt->xp_rtaddr, raddr, raddr->sa_len);
247
248	error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
249	if (error)
250		goto cleanup_svc_vc_create;
251
252	memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
253	free(sa, M_SONAME);
254
255	xprt_register(xprt);
256
257	SOCKBUF_LOCK(&so->so_rcv);
258	xprt->xp_upcallset = 1;
259	soupcall_set(so, SO_RCV, svc_vc_soupcall, xprt);
260	SOCKBUF_UNLOCK(&so->so_rcv);
261
262	/*
263	 * Throw the transport into the active list in case it already
264	 * has some data buffered.
265	 */
266	sx_xlock(&xprt->xp_lock);
267	xprt_active(xprt);
268	sx_xunlock(&xprt->xp_lock);
269
270	return (xprt);
271cleanup_svc_vc_create:
272	if (xprt) {
273		mem_free(xprt, sizeof(*xprt));
274	}
275	if (cd)
276		mem_free(cd, sizeof(*cd));
277	return (NULL);
278}
279
280/*
281 * Create a new transport for a backchannel on a clnt_vc socket.
282 */
283SVCXPRT *
284svc_vc_create_backchannel(SVCPOOL *pool)
285{
286	SVCXPRT *xprt = NULL;
287	struct cf_conn *cd = NULL;
288
289	cd = mem_alloc(sizeof(*cd));
290	cd->strm_stat = XPRT_IDLE;
291
292	xprt = svc_xprt_alloc();
293	sx_init(&xprt->xp_lock, "xprt->xp_lock");
294	xprt->xp_pool = pool;
295	xprt->xp_socket = NULL;
296	xprt->xp_p1 = cd;
297	xprt->xp_p2 = NULL;
298	xprt->xp_ops = &svc_vc_backchannel_ops;
299	return (xprt);
300}
301
302/*
303 * This does all of the accept except the final call to soaccept. The
304 * caller will call soaccept after dropping its locks (soaccept may
305 * call malloc).
306 */
307int
308svc_vc_accept(struct socket *head, struct socket **sop)
309{
310	int error = 0;
311	struct socket *so;
312
313	if ((head->so_options & SO_ACCEPTCONN) == 0) {
314		error = EINVAL;
315		goto done;
316	}
317#ifdef MAC
318	error = mac_socket_check_accept(curthread->td_ucred, head);
319	if (error != 0)
320		goto done;
321#endif
322	ACCEPT_LOCK();
323	if (TAILQ_EMPTY(&head->so_comp)) {
324		ACCEPT_UNLOCK();
325		error = EWOULDBLOCK;
326		goto done;
327	}
328	so = TAILQ_FIRST(&head->so_comp);
329	KASSERT(!(so->so_qstate & SQ_INCOMP), ("svc_vc_accept: so SQ_INCOMP"));
330	KASSERT(so->so_qstate & SQ_COMP, ("svc_vc_accept: so not SQ_COMP"));
331
332	/*
333	 * Before changing the flags on the socket, we have to bump the
334	 * reference count.  Otherwise, if the protocol calls sofree(),
335	 * the socket will be released due to a zero refcount.
336	 * XXX might not need soref() since this is simpler than kern_accept.
337	 */
338	SOCK_LOCK(so);			/* soref() and so_state update */
339	soref(so);			/* file descriptor reference */
340
341	TAILQ_REMOVE(&head->so_comp, so, so_list);
342	head->so_qlen--;
343	so->so_state |= (head->so_state & SS_NBIO);
344	so->so_qstate &= ~SQ_COMP;
345	so->so_head = NULL;
346
347	SOCK_UNLOCK(so);
348	ACCEPT_UNLOCK();
349
350	*sop = so;
351
352	/* connection has been removed from the listen queue */
353	KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
354done:
355	return (error);
356}
357
358/*ARGSUSED*/
359static bool_t
360svc_vc_rendezvous_recv(SVCXPRT *xprt, struct rpc_msg *msg,
361    struct sockaddr **addrp, struct mbuf **mp)
362{
363	struct socket *so = NULL;
364	struct sockaddr *sa = NULL;
365	int error;
366	SVCXPRT *new_xprt;
367
368	/*
369	 * The socket upcall calls xprt_active() which will eventually
370	 * cause the server to call us here. We attempt to accept a
371	 * connection from the socket and turn it into a new
372	 * transport. If the accept fails, we have drained all pending
373	 * connections so we call xprt_inactive().
374	 */
375	sx_xlock(&xprt->xp_lock);
376
377	error = svc_vc_accept(xprt->xp_socket, &so);
378
379	if (error == EWOULDBLOCK) {
380		/*
381		 * We must re-test for new connections after taking
382		 * the lock to protect us in the case where a new
383		 * connection arrives after our call to accept fails
384		 * with EWOULDBLOCK.
385		 */
386		ACCEPT_LOCK();
387		if (TAILQ_EMPTY(&xprt->xp_socket->so_comp))
388			xprt_inactive(xprt);
389		ACCEPT_UNLOCK();
390		sx_xunlock(&xprt->xp_lock);
391		return (FALSE);
392	}
393
394	if (error) {
395		SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
396		if (xprt->xp_upcallset) {
397			xprt->xp_upcallset = 0;
398			soupcall_clear(xprt->xp_socket, SO_RCV);
399		}
400		SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
401		xprt_inactive(xprt);
402		sx_xunlock(&xprt->xp_lock);
403		return (FALSE);
404	}
405
406	sx_xunlock(&xprt->xp_lock);
407
408	sa = 0;
409	error = soaccept(so, &sa);
410
411	if (error) {
412		/*
413		 * XXX not sure if I need to call sofree or soclose here.
414		 */
415		if (sa)
416			free(sa, M_SONAME);
417		return (FALSE);
418	}
419
420	/*
421	 * svc_vc_create_conn will call xprt_register - we don't need
422	 * to do anything with the new connection except derefence it.
423	 */
424	new_xprt = svc_vc_create_conn(xprt->xp_pool, so, sa);
425	if (!new_xprt) {
426		soclose(so);
427	} else {
428		SVC_RELEASE(new_xprt);
429	}
430
431	free(sa, M_SONAME);
432
433	return (FALSE); /* there is never an rpc msg to be processed */
434}
435
436/*ARGSUSED*/
437static enum xprt_stat
438svc_vc_rendezvous_stat(SVCXPRT *xprt)
439{
440
441	return (XPRT_IDLE);
442}
443
444static void
445svc_vc_destroy_common(SVCXPRT *xprt)
446{
447	SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
448	if (xprt->xp_upcallset) {
449		xprt->xp_upcallset = 0;
450		soupcall_clear(xprt->xp_socket, SO_RCV);
451	}
452	SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
453
454	sx_destroy(&xprt->xp_lock);
455	if (xprt->xp_socket)
456		(void)soclose(xprt->xp_socket);
457
458	if (xprt->xp_netid)
459		(void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
460	svc_xprt_free(xprt);
461}
462
463static void
464svc_vc_rendezvous_destroy(SVCXPRT *xprt)
465{
466
467	svc_vc_destroy_common(xprt);
468}
469
470static void
471svc_vc_destroy(SVCXPRT *xprt)
472{
473	struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1;
474
475	svc_vc_destroy_common(xprt);
476
477	if (cd->mreq)
478		m_freem(cd->mreq);
479	if (cd->mpending)
480		m_freem(cd->mpending);
481	mem_free(cd, sizeof(*cd));
482}
483
484static void
485svc_vc_backchannel_destroy(SVCXPRT *xprt)
486{
487	struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1;
488	struct mbuf *m, *m2;
489
490	svc_xprt_free(xprt);
491	m = cd->mreq;
492	while (m != NULL) {
493		m2 = m;
494		m = m->m_nextpkt;
495		m_freem(m2);
496	}
497	mem_free(cd, sizeof(*cd));
498}
499
500/*ARGSUSED*/
501static bool_t
502svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in)
503{
504	return (FALSE);
505}
506
507static bool_t
508svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, void *in)
509{
510
511	return (FALSE);
512}
513
514static bool_t
515svc_vc_backchannel_control(SVCXPRT *xprt, const u_int rq, void *in)
516{
517
518	return (FALSE);
519}
520
521static enum xprt_stat
522svc_vc_stat(SVCXPRT *xprt)
523{
524	struct cf_conn *cd;
525
526	cd = (struct cf_conn *)(xprt->xp_p1);
527
528	if (cd->strm_stat == XPRT_DIED)
529		return (XPRT_DIED);
530
531	if (cd->mreq != NULL && cd->resid == 0 && cd->eor)
532		return (XPRT_MOREREQS);
533
534	if (soreadable(xprt->xp_socket))
535		return (XPRT_MOREREQS);
536
537	return (XPRT_IDLE);
538}
539
540static enum xprt_stat
541svc_vc_backchannel_stat(SVCXPRT *xprt)
542{
543	struct cf_conn *cd;
544
545	cd = (struct cf_conn *)(xprt->xp_p1);
546
547	if (cd->mreq != NULL)
548		return (XPRT_MOREREQS);
549
550	return (XPRT_IDLE);
551}
552
553/*
554 * If we have an mbuf chain in cd->mpending, try to parse a record from it,
555 * leaving the result in cd->mreq. If we don't have a complete record, leave
556 * the partial result in cd->mreq and try to read more from the socket.
557 */
558static int
559svc_vc_process_pending(SVCXPRT *xprt)
560{
561	struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
562	struct socket *so = xprt->xp_socket;
563	struct mbuf *m;
564
565	/*
566	 * If cd->resid is non-zero, we have part of the
567	 * record already, otherwise we are expecting a record
568	 * marker.
569	 */
570	if (!cd->resid && cd->mpending) {
571		/*
572		 * See if there is enough data buffered to
573		 * make up a record marker. Make sure we can
574		 * handle the case where the record marker is
575		 * split across more than one mbuf.
576		 */
577		size_t n = 0;
578		uint32_t header;
579
580		m = cd->mpending;
581		while (n < sizeof(uint32_t) && m) {
582			n += m->m_len;
583			m = m->m_next;
584		}
585		if (n < sizeof(uint32_t)) {
586			so->so_rcv.sb_lowat = sizeof(uint32_t) - n;
587			return (FALSE);
588		}
589		m_copydata(cd->mpending, 0, sizeof(header),
590		    (char *)&header);
591		header = ntohl(header);
592		cd->eor = (header & 0x80000000) != 0;
593		cd->resid = header & 0x7fffffff;
594		m_adj(cd->mpending, sizeof(uint32_t));
595	}
596
597	/*
598	 * Start pulling off mbufs from cd->mpending
599	 * until we either have a complete record or
600	 * we run out of data. We use m_split to pull
601	 * data - it will pull as much as possible and
602	 * split the last mbuf if necessary.
603	 */
604	while (cd->mpending && cd->resid) {
605		m = cd->mpending;
606		if (cd->mpending->m_next
607		    || cd->mpending->m_len > cd->resid)
608			cd->mpending = m_split(cd->mpending,
609			    cd->resid, M_WAITOK);
610		else
611			cd->mpending = NULL;
612		if (cd->mreq)
613			m_last(cd->mreq)->m_next = m;
614		else
615			cd->mreq = m;
616		while (m) {
617			cd->resid -= m->m_len;
618			m = m->m_next;
619		}
620	}
621
622	so->so_rcv.sb_lowat = imax(1, imin(cd->resid, so->so_rcv.sb_hiwat / 2));
623	return (TRUE);
624}
625
626static bool_t
627svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg,
628    struct sockaddr **addrp, struct mbuf **mp)
629{
630	struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
631	struct uio uio;
632	struct mbuf *m;
633	struct socket* so = xprt->xp_socket;
634	XDR xdrs;
635	int error, rcvflag;
636
637	/*
638	 * Serialise access to the socket and our own record parsing
639	 * state.
640	 */
641	sx_xlock(&xprt->xp_lock);
642
643	for (;;) {
644		/* If we have no request ready, check pending queue. */
645		while (cd->mpending &&
646		    (cd->mreq == NULL || cd->resid != 0 || !cd->eor)) {
647			if (!svc_vc_process_pending(xprt))
648				break;
649		}
650
651		/* Process and return complete request in cd->mreq. */
652		if (cd->mreq != NULL && cd->resid == 0 && cd->eor) {
653
654			xdrmbuf_create(&xdrs, cd->mreq, XDR_DECODE);
655			cd->mreq = NULL;
656
657			/* Check for next request in a pending queue. */
658			svc_vc_process_pending(xprt);
659			if (cd->mreq == NULL || cd->resid != 0) {
660				SOCKBUF_LOCK(&so->so_rcv);
661				if (!soreadable(so))
662					xprt_inactive(xprt);
663				SOCKBUF_UNLOCK(&so->so_rcv);
664			}
665
666			sx_xunlock(&xprt->xp_lock);
667
668			if (! xdr_callmsg(&xdrs, msg)) {
669				XDR_DESTROY(&xdrs);
670				return (FALSE);
671			}
672
673			*addrp = NULL;
674			*mp = xdrmbuf_getall(&xdrs);
675			XDR_DESTROY(&xdrs);
676
677			return (TRUE);
678		}
679
680		/*
681		 * The socket upcall calls xprt_active() which will eventually
682		 * cause the server to call us here. We attempt to
683		 * read as much as possible from the socket and put
684		 * the result in cd->mpending. If the read fails,
685		 * we have drained both cd->mpending and the socket so
686		 * we can call xprt_inactive().
687		 */
688		uio.uio_resid = 1000000000;
689		uio.uio_td = curthread;
690		m = NULL;
691		rcvflag = MSG_DONTWAIT;
692		error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
693
694		if (error == EWOULDBLOCK) {
695			/*
696			 * We must re-test for readability after
697			 * taking the lock to protect us in the case
698			 * where a new packet arrives on the socket
699			 * after our call to soreceive fails with
700			 * EWOULDBLOCK.
701			 */
702			SOCKBUF_LOCK(&so->so_rcv);
703			if (!soreadable(so))
704				xprt_inactive(xprt);
705			SOCKBUF_UNLOCK(&so->so_rcv);
706			sx_xunlock(&xprt->xp_lock);
707			return (FALSE);
708		}
709
710		if (error) {
711			SOCKBUF_LOCK(&so->so_rcv);
712			if (xprt->xp_upcallset) {
713				xprt->xp_upcallset = 0;
714				soupcall_clear(so, SO_RCV);
715			}
716			SOCKBUF_UNLOCK(&so->so_rcv);
717			xprt_inactive(xprt);
718			cd->strm_stat = XPRT_DIED;
719			sx_xunlock(&xprt->xp_lock);
720			return (FALSE);
721		}
722
723		if (!m) {
724			/*
725			 * EOF - the other end has closed the socket.
726			 */
727			xprt_inactive(xprt);
728			cd->strm_stat = XPRT_DIED;
729			sx_xunlock(&xprt->xp_lock);
730			return (FALSE);
731		}
732
733		if (cd->mpending)
734			m_last(cd->mpending)->m_next = m;
735		else
736			cd->mpending = m;
737	}
738}
739
740static bool_t
741svc_vc_backchannel_recv(SVCXPRT *xprt, struct rpc_msg *msg,
742    struct sockaddr **addrp, struct mbuf **mp)
743{
744	struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
745	struct ct_data *ct;
746	struct mbuf *m;
747	XDR xdrs;
748
749	sx_xlock(&xprt->xp_lock);
750	ct = (struct ct_data *)xprt->xp_p2;
751	if (ct == NULL) {
752		sx_xunlock(&xprt->xp_lock);
753		return (FALSE);
754	}
755	mtx_lock(&ct->ct_lock);
756	m = cd->mreq;
757	if (m == NULL) {
758		xprt_inactive(xprt);
759		mtx_unlock(&ct->ct_lock);
760		sx_xunlock(&xprt->xp_lock);
761		return (FALSE);
762	}
763	cd->mreq = m->m_nextpkt;
764	mtx_unlock(&ct->ct_lock);
765	sx_xunlock(&xprt->xp_lock);
766
767	xdrmbuf_create(&xdrs, m, XDR_DECODE);
768	if (! xdr_callmsg(&xdrs, msg)) {
769		XDR_DESTROY(&xdrs);
770		return (FALSE);
771	}
772	*addrp = NULL;
773	*mp = xdrmbuf_getall(&xdrs);
774	XDR_DESTROY(&xdrs);
775	return (TRUE);
776}
777
778static bool_t
779svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg,
780    struct sockaddr *addr, struct mbuf *m)
781{
782	XDR xdrs;
783	struct mbuf *mrep;
784	bool_t stat = TRUE;
785	int error;
786
787	/*
788	 * Leave space for record mark.
789	 */
790	mrep = m_gethdr(M_WAITOK, MT_DATA);
791	mrep->m_data += sizeof(uint32_t);
792
793	xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
794
795	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
796	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
797		if (!xdr_replymsg(&xdrs, msg))
798			stat = FALSE;
799		else
800			xdrmbuf_append(&xdrs, m);
801	} else {
802		stat = xdr_replymsg(&xdrs, msg);
803	}
804
805	if (stat) {
806		m_fixhdr(mrep);
807
808		/*
809		 * Prepend a record marker containing the reply length.
810		 */
811		M_PREPEND(mrep, sizeof(uint32_t), M_WAITOK);
812		*mtod(mrep, uint32_t *) =
813			htonl(0x80000000 | (mrep->m_pkthdr.len
814				- sizeof(uint32_t)));
815		error = sosend(xprt->xp_socket, NULL, NULL, mrep, NULL,
816		    0, curthread);
817		if (!error) {
818			stat = TRUE;
819		}
820	} else {
821		m_freem(mrep);
822	}
823
824	XDR_DESTROY(&xdrs);
825	xprt->xp_p2 = NULL;
826
827	return (stat);
828}
829
830static bool_t
831svc_vc_backchannel_reply(SVCXPRT *xprt, struct rpc_msg *msg,
832    struct sockaddr *addr, struct mbuf *m)
833{
834	struct ct_data *ct;
835	XDR xdrs;
836	struct mbuf *mrep;
837	bool_t stat = TRUE;
838	int error;
839
840	/*
841	 * Leave space for record mark.
842	 */
843	mrep = m_gethdr(M_WAITOK, MT_DATA);
844	mrep->m_data += sizeof(uint32_t);
845
846	xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
847
848	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
849	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
850		if (!xdr_replymsg(&xdrs, msg))
851			stat = FALSE;
852		else
853			xdrmbuf_append(&xdrs, m);
854	} else {
855		stat = xdr_replymsg(&xdrs, msg);
856	}
857
858	if (stat) {
859		m_fixhdr(mrep);
860
861		/*
862		 * Prepend a record marker containing the reply length.
863		 */
864		M_PREPEND(mrep, sizeof(uint32_t), M_WAITOK);
865		*mtod(mrep, uint32_t *) =
866			htonl(0x80000000 | (mrep->m_pkthdr.len
867				- sizeof(uint32_t)));
868		sx_xlock(&xprt->xp_lock);
869		ct = (struct ct_data *)xprt->xp_p2;
870		if (ct != NULL)
871			error = sosend(ct->ct_socket, NULL, NULL, mrep, NULL,
872			    0, curthread);
873		else
874			error = EPIPE;
875		sx_xunlock(&xprt->xp_lock);
876		if (!error) {
877			stat = TRUE;
878		}
879	} else {
880		m_freem(mrep);
881	}
882
883	XDR_DESTROY(&xdrs);
884
885	return (stat);
886}
887
888static bool_t
889svc_vc_null()
890{
891
892	return (FALSE);
893}
894
895static int
896svc_vc_soupcall(struct socket *so, void *arg, int waitflag)
897{
898	SVCXPRT *xprt = (SVCXPRT *) arg;
899
900	if (soreadable(xprt->xp_socket))
901		xprt_active(xprt);
902	return (SU_OK);
903}
904
905#if 0
906/*
907 * Get the effective UID of the sending process. Used by rpcbind, keyserv
908 * and rpc.yppasswdd on AF_LOCAL.
909 */
910int
911__rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
912	int sock, ret;
913	gid_t egid;
914	uid_t euid;
915	struct sockaddr *sa;
916
917	sock = transp->xp_fd;
918	sa = (struct sockaddr *)transp->xp_rtaddr;
919	if (sa->sa_family == AF_LOCAL) {
920		ret = getpeereid(sock, &euid, &egid);
921		if (ret == 0)
922			*uid = euid;
923		return (ret);
924	} else
925		return (-1);
926}
927#endif
928