1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *  Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  *  Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * svc_clts.c
39*0Sstevel@tonic-gate  * Server side for RPC in the kernel.
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include <sys/param.h>
44*0Sstevel@tonic-gate #include <sys/types.h>
45*0Sstevel@tonic-gate #include <sys/sysmacros.h>
46*0Sstevel@tonic-gate #include <sys/file.h>
47*0Sstevel@tonic-gate #include <sys/stream.h>
48*0Sstevel@tonic-gate #include <sys/strsubr.h>
49*0Sstevel@tonic-gate #include <sys/tihdr.h>
50*0Sstevel@tonic-gate #include <sys/tiuser.h>
51*0Sstevel@tonic-gate #include <sys/t_kuser.h>
52*0Sstevel@tonic-gate #include <sys/fcntl.h>
53*0Sstevel@tonic-gate #include <sys/errno.h>
54*0Sstevel@tonic-gate #include <sys/kmem.h>
55*0Sstevel@tonic-gate #include <sys/systm.h>
56*0Sstevel@tonic-gate #include <sys/cmn_err.h>
57*0Sstevel@tonic-gate #include <sys/kstat.h>
58*0Sstevel@tonic-gate #include <sys/vtrace.h>
59*0Sstevel@tonic-gate #include <sys/debug.h>
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #include <rpc/types.h>
62*0Sstevel@tonic-gate #include <rpc/xdr.h>
63*0Sstevel@tonic-gate #include <rpc/auth.h>
64*0Sstevel@tonic-gate #include <rpc/clnt.h>
65*0Sstevel@tonic-gate #include <rpc/rpc_msg.h>
66*0Sstevel@tonic-gate #include <rpc/svc.h>
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * Routines exported through ops vector.
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate static bool_t		svc_clts_krecv(SVCXPRT *, mblk_t *, struct rpc_msg *);
72*0Sstevel@tonic-gate static bool_t		svc_clts_ksend(SVCXPRT *, struct rpc_msg *);
73*0Sstevel@tonic-gate static bool_t		svc_clts_kgetargs(SVCXPRT *, xdrproc_t, caddr_t);
74*0Sstevel@tonic-gate static bool_t		svc_clts_kfreeargs(SVCXPRT *, xdrproc_t, caddr_t);
75*0Sstevel@tonic-gate static void		svc_clts_kdestroy(SVCMASTERXPRT *);
76*0Sstevel@tonic-gate static int		svc_clts_kdup(struct svc_req *, caddr_t, int,
77*0Sstevel@tonic-gate 				struct dupreq **, bool_t *);
78*0Sstevel@tonic-gate static void		svc_clts_kdupdone(struct dupreq *, caddr_t,
79*0Sstevel@tonic-gate 				void (*)(), int, int);
80*0Sstevel@tonic-gate static int32_t		*svc_clts_kgetres(SVCXPRT *, int);
81*0Sstevel@tonic-gate static void		svc_clts_kclone_destroy(SVCXPRT *);
82*0Sstevel@tonic-gate static void		svc_clts_kfreeres(SVCXPRT *);
83*0Sstevel@tonic-gate static void		svc_clts_kstart(SVCMASTERXPRT *);
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate /*
86*0Sstevel@tonic-gate  * Server transport operations vector.
87*0Sstevel@tonic-gate  */
88*0Sstevel@tonic-gate struct svc_ops svc_clts_op = {
89*0Sstevel@tonic-gate 	svc_clts_krecv,		/* Get requests */
90*0Sstevel@tonic-gate 	svc_clts_kgetargs,	/* Deserialize arguments */
91*0Sstevel@tonic-gate 	svc_clts_ksend,		/* Send reply */
92*0Sstevel@tonic-gate 	svc_clts_kfreeargs,	/* Free argument data space */
93*0Sstevel@tonic-gate 	svc_clts_kdestroy,	/* Destroy transport handle */
94*0Sstevel@tonic-gate 	svc_clts_kdup,		/* Check entry in dup req cache */
95*0Sstevel@tonic-gate 	svc_clts_kdupdone,	/* Mark entry in dup req cache as done */
96*0Sstevel@tonic-gate 	svc_clts_kgetres,	/* Get pointer to response buffer */
97*0Sstevel@tonic-gate 	svc_clts_kfreeres,	/* Destroy pre-serialized response header */
98*0Sstevel@tonic-gate 	svc_clts_kclone_destroy, /* Destroy a clone xprt */
99*0Sstevel@tonic-gate 	svc_clts_kstart		/* Tell `ready-to-receive' to rpcmod */
100*0Sstevel@tonic-gate };
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate /*
103*0Sstevel@tonic-gate  * Transport private data.
104*0Sstevel@tonic-gate  * Kept in xprt->xp_p2buf.
105*0Sstevel@tonic-gate  */
106*0Sstevel@tonic-gate struct udp_data {
107*0Sstevel@tonic-gate 	mblk_t	*ud_resp;			/* buffer for response */
108*0Sstevel@tonic-gate 	mblk_t	*ud_inmp;			/* mblk chain of request */
109*0Sstevel@tonic-gate };
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate #define	UD_MAXSIZE	8800
112*0Sstevel@tonic-gate #define	UD_INITSIZE	2048
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate  * Connectionless server statistics
116*0Sstevel@tonic-gate  */
117*0Sstevel@tonic-gate static const struct rpc_clts_server {
118*0Sstevel@tonic-gate 	kstat_named_t	rscalls;
119*0Sstevel@tonic-gate 	kstat_named_t	rsbadcalls;
120*0Sstevel@tonic-gate 	kstat_named_t	rsnullrecv;
121*0Sstevel@tonic-gate 	kstat_named_t	rsbadlen;
122*0Sstevel@tonic-gate 	kstat_named_t	rsxdrcall;
123*0Sstevel@tonic-gate 	kstat_named_t	rsdupchecks;
124*0Sstevel@tonic-gate 	kstat_named_t	rsdupreqs;
125*0Sstevel@tonic-gate } clts_rsstat_tmpl = {
126*0Sstevel@tonic-gate 	{ "calls",	KSTAT_DATA_UINT64 },
127*0Sstevel@tonic-gate 	{ "badcalls",	KSTAT_DATA_UINT64 },
128*0Sstevel@tonic-gate 	{ "nullrecv",	KSTAT_DATA_UINT64 },
129*0Sstevel@tonic-gate 	{ "badlen",	KSTAT_DATA_UINT64 },
130*0Sstevel@tonic-gate 	{ "xdrcall",	KSTAT_DATA_UINT64 },
131*0Sstevel@tonic-gate 	{ "dupchecks",	KSTAT_DATA_UINT64 },
132*0Sstevel@tonic-gate 	{ "dupreqs",	KSTAT_DATA_UINT64 }
133*0Sstevel@tonic-gate };
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate static uint_t clts_rsstat_ndata =
136*0Sstevel@tonic-gate 	sizeof (clts_rsstat_tmpl) / sizeof (kstat_named_t);
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate #define	CLONE2STATS(clone_xprt)	\
139*0Sstevel@tonic-gate 	(struct rpc_clts_server *)(clone_xprt)->xp_master->xp_p2
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate #define	RSSTAT_INCR(stats, x)	\
142*0Sstevel@tonic-gate 	atomic_add_64(&(stats)->x.value.ui64, 1)
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate /*
145*0Sstevel@tonic-gate  * Create a transport record.
146*0Sstevel@tonic-gate  * The transport record, output buffer, and private data structure
147*0Sstevel@tonic-gate  * are allocated.  The output buffer is serialized into using xdrmem.
148*0Sstevel@tonic-gate  * There is one transport record per user process which implements a
149*0Sstevel@tonic-gate  * set of services.
150*0Sstevel@tonic-gate  */
151*0Sstevel@tonic-gate /* ARGSUSED */
152*0Sstevel@tonic-gate int
153*0Sstevel@tonic-gate svc_clts_kcreate(file_t *fp, uint_t sendsz, struct T_info_ack *tinfo,
154*0Sstevel@tonic-gate     SVCMASTERXPRT **nxprt)
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	SVCMASTERXPRT *xprt;
157*0Sstevel@tonic-gate 	struct rpcstat *rpcstat;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	if (nxprt == NULL)
160*0Sstevel@tonic-gate 		return (EINVAL);
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	rpcstat = zone_getspecific(rpcstat_zone_key, curproc->p_zone);
163*0Sstevel@tonic-gate 	ASSERT(rpcstat != NULL);
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	xprt = kmem_zalloc(sizeof (*xprt), KM_SLEEP);
166*0Sstevel@tonic-gate 	xprt->xp_p2 = (caddr_t)rpcstat->rpc_clts_server;
167*0Sstevel@tonic-gate 	xprt->xp_ops = &svc_clts_op;
168*0Sstevel@tonic-gate 	xprt->xp_msg_size = tinfo->TSDU_size;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	xprt->xp_rtaddr.buf = NULL;
171*0Sstevel@tonic-gate 	xprt->xp_rtaddr.maxlen = tinfo->ADDR_size;
172*0Sstevel@tonic-gate 	xprt->xp_rtaddr.len = 0;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	*nxprt = xprt;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	return (0);
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate /*
180*0Sstevel@tonic-gate  * Destroy a transport record.
181*0Sstevel@tonic-gate  * Frees the space allocated for a transport record.
182*0Sstevel@tonic-gate  */
183*0Sstevel@tonic-gate static void
184*0Sstevel@tonic-gate svc_clts_kdestroy(SVCMASTERXPRT *xprt)
185*0Sstevel@tonic-gate {
186*0Sstevel@tonic-gate 	if (xprt->xp_netid)
187*0Sstevel@tonic-gate 		kmem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
188*0Sstevel@tonic-gate 	if (xprt->xp_addrmask.maxlen)
189*0Sstevel@tonic-gate 		kmem_free(xprt->xp_addrmask.buf, xprt->xp_addrmask.maxlen);
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	mutex_destroy(&xprt->xp_req_lock);
192*0Sstevel@tonic-gate 	mutex_destroy(&xprt->xp_thread_lock);
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	kmem_free(xprt, sizeof (SVCMASTERXPRT));
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate /*
198*0Sstevel@tonic-gate  * Transport-type specific part of svc_xprt_cleanup().
199*0Sstevel@tonic-gate  * Frees the message buffer space allocated for a clone of a transport record
200*0Sstevel@tonic-gate  */
201*0Sstevel@tonic-gate static void
202*0Sstevel@tonic-gate svc_clts_kclone_destroy(SVCXPRT *clone_xprt)
203*0Sstevel@tonic-gate {
204*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
205*0Sstevel@tonic-gate 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	if (ud->ud_resp) {
208*0Sstevel@tonic-gate 		/*
209*0Sstevel@tonic-gate 		 * There should not be any left over results buffer.
210*0Sstevel@tonic-gate 		 */
211*0Sstevel@tonic-gate 		ASSERT(ud->ud_resp->b_cont == NULL);
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 		/*
214*0Sstevel@tonic-gate 		 * Free the T_UNITDATA_{REQ/IND} that svc_clts_krecv
215*0Sstevel@tonic-gate 		 * saved.
216*0Sstevel@tonic-gate 		 */
217*0Sstevel@tonic-gate 		freeb(ud->ud_resp);
218*0Sstevel@tonic-gate 	}
219*0Sstevel@tonic-gate 	if (ud->ud_inmp)
220*0Sstevel@tonic-gate 		freemsg(ud->ud_inmp);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate /*
224*0Sstevel@tonic-gate  * svc_tli_kcreate() calls this function at the end to tell
225*0Sstevel@tonic-gate  * rpcmod that the transport is ready to receive requests.
226*0Sstevel@tonic-gate  */
227*0Sstevel@tonic-gate /* ARGSUSED */
228*0Sstevel@tonic-gate static void
229*0Sstevel@tonic-gate svc_clts_kstart(SVCMASTERXPRT *xprt)
230*0Sstevel@tonic-gate {
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate /*
234*0Sstevel@tonic-gate  * Receive rpc requests.
235*0Sstevel@tonic-gate  * Pulls a request in off the socket, checks if the packet is intact,
236*0Sstevel@tonic-gate  * and deserializes the call packet.
237*0Sstevel@tonic-gate  */
238*0Sstevel@tonic-gate static bool_t
239*0Sstevel@tonic-gate svc_clts_krecv(SVCXPRT *clone_xprt, mblk_t *mp, struct rpc_msg *msg)
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
242*0Sstevel@tonic-gate 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
243*0Sstevel@tonic-gate 	XDR *xdrs = &clone_xprt->xp_xdrin;
244*0Sstevel@tonic-gate 	struct rpc_clts_server *stats = CLONE2STATS(clone_xprt);
245*0Sstevel@tonic-gate 	union T_primitives *pptr;
246*0Sstevel@tonic-gate 	int hdrsz;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_SVC_CLTS_KRECV_START,
249*0Sstevel@tonic-gate 	    "svc_clts_krecv_start:");
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	RSSTAT_INCR(stats, rscalls);
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	/*
254*0Sstevel@tonic-gate 	 * The incoming request should start with an M_PROTO message.
255*0Sstevel@tonic-gate 	 */
256*0Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_PROTO) {
257*0Sstevel@tonic-gate 		goto bad;
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	/*
261*0Sstevel@tonic-gate 	 * The incoming request should be an T_UNITDTA_IND.  There
262*0Sstevel@tonic-gate 	 * might be other messages coming up the stream, but we can
263*0Sstevel@tonic-gate 	 * ignore them.
264*0Sstevel@tonic-gate 	 */
265*0Sstevel@tonic-gate 	pptr = (union T_primitives *)mp->b_rptr;
266*0Sstevel@tonic-gate 	if (pptr->type != T_UNITDATA_IND) {
267*0Sstevel@tonic-gate 		goto bad;
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 	/*
270*0Sstevel@tonic-gate 	 * Do some checking to make sure that the header at least looks okay.
271*0Sstevel@tonic-gate 	 */
272*0Sstevel@tonic-gate 	hdrsz = (int)(mp->b_wptr - mp->b_rptr);
273*0Sstevel@tonic-gate 	if (hdrsz < TUNITDATAINDSZ ||
274*0Sstevel@tonic-gate 	    hdrsz < (pptr->unitdata_ind.OPT_offset +
275*0Sstevel@tonic-gate 		    pptr->unitdata_ind.OPT_length) ||
276*0Sstevel@tonic-gate 	    hdrsz < (pptr->unitdata_ind.SRC_offset +
277*0Sstevel@tonic-gate 		    pptr->unitdata_ind.SRC_length)) {
278*0Sstevel@tonic-gate 		goto bad;
279*0Sstevel@tonic-gate 	}
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	/*
282*0Sstevel@tonic-gate 	 * Make sure that the transport provided a usable address.
283*0Sstevel@tonic-gate 	 */
284*0Sstevel@tonic-gate 	if (pptr->unitdata_ind.SRC_length <= 0) {
285*0Sstevel@tonic-gate 		goto bad;
286*0Sstevel@tonic-gate 	}
287*0Sstevel@tonic-gate 	/*
288*0Sstevel@tonic-gate 	 * Point the remote transport address in the service_transport
289*0Sstevel@tonic-gate 	 * handle at the address in the request.
290*0Sstevel@tonic-gate 	 */
291*0Sstevel@tonic-gate 	clone_xprt->xp_rtaddr.buf = (char *)mp->b_rptr +
292*0Sstevel@tonic-gate 	    pptr->unitdata_ind.SRC_offset;
293*0Sstevel@tonic-gate 	clone_xprt->xp_rtaddr.len = pptr->unitdata_ind.SRC_length;
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	/*
296*0Sstevel@tonic-gate 	 * Save the first mblk which contains the T_unidata_ind in
297*0Sstevel@tonic-gate 	 * ud_resp.  It will be used to generate the T_unitdata_req
298*0Sstevel@tonic-gate 	 * during the reply.
299*0Sstevel@tonic-gate 	 */
300*0Sstevel@tonic-gate 	if (ud->ud_resp) {
301*0Sstevel@tonic-gate 		if (ud->ud_resp->b_cont != NULL) {
302*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "svc_clts_krecv: ud_resp %p, "
303*0Sstevel@tonic-gate 			    "b_cont %p", (void *)ud->ud_resp,
304*0Sstevel@tonic-gate 			    (void *)ud->ud_resp->b_cont);
305*0Sstevel@tonic-gate 		}
306*0Sstevel@tonic-gate 		freeb(ud->ud_resp);
307*0Sstevel@tonic-gate 	}
308*0Sstevel@tonic-gate 	ud->ud_resp = mp;
309*0Sstevel@tonic-gate 	mp = mp->b_cont;
310*0Sstevel@tonic-gate 	ud->ud_resp->b_cont = NULL;
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	xdrmblk_init(xdrs, mp, XDR_DECODE, 0);
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_XDR_CALLMSG_START,
315*0Sstevel@tonic-gate 	    "xdr_callmsg_start:");
316*0Sstevel@tonic-gate 	if (! xdr_callmsg(xdrs, msg)) {
317*0Sstevel@tonic-gate 		TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
318*0Sstevel@tonic-gate 		    "xdr_callmsg_end:(%S)", "bad");
319*0Sstevel@tonic-gate 		RSSTAT_INCR(stats, rsxdrcall);
320*0Sstevel@tonic-gate 		goto bad;
321*0Sstevel@tonic-gate 	}
322*0Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
323*0Sstevel@tonic-gate 	    "xdr_callmsg_end:(%S)", "good");
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	clone_xprt->xp_xid = msg->rm_xid;
326*0Sstevel@tonic-gate 	ud->ud_inmp = mp;
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KRECV_END,
329*0Sstevel@tonic-gate 	    "svc_clts_krecv_end:(%S)", "good");
330*0Sstevel@tonic-gate 	return (TRUE);
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate bad:
333*0Sstevel@tonic-gate 	if (mp)
334*0Sstevel@tonic-gate 		freemsg(mp);
335*0Sstevel@tonic-gate 	if (ud->ud_resp) {
336*0Sstevel@tonic-gate 		/*
337*0Sstevel@tonic-gate 		 * There should not be any left over results buffer.
338*0Sstevel@tonic-gate 		 */
339*0Sstevel@tonic-gate 		ASSERT(ud->ud_resp->b_cont == NULL);
340*0Sstevel@tonic-gate 		freeb(ud->ud_resp);
341*0Sstevel@tonic-gate 		ud->ud_resp = NULL;
342*0Sstevel@tonic-gate 	}
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	RSSTAT_INCR(stats, rsbadcalls);
345*0Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KRECV_END,
346*0Sstevel@tonic-gate 	    "svc_clts_krecv_end:(%S)", "bad");
347*0Sstevel@tonic-gate 	return (FALSE);
348*0Sstevel@tonic-gate }
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate /*
351*0Sstevel@tonic-gate  * Send rpc reply.
352*0Sstevel@tonic-gate  * Serialize the reply packet into the output buffer then
353*0Sstevel@tonic-gate  * call t_ksndudata to send it.
354*0Sstevel@tonic-gate  */
355*0Sstevel@tonic-gate static bool_t
356*0Sstevel@tonic-gate svc_clts_ksend(SVCXPRT *clone_xprt, struct rpc_msg *msg)
357*0Sstevel@tonic-gate {
358*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
359*0Sstevel@tonic-gate 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
360*0Sstevel@tonic-gate 	XDR *xdrs = &clone_xprt->xp_xdrout;
361*0Sstevel@tonic-gate 	int stat = FALSE;
362*0Sstevel@tonic-gate 	mblk_t *mp;
363*0Sstevel@tonic-gate 	int msgsz;
364*0Sstevel@tonic-gate 	struct T_unitdata_req *udreq;
365*0Sstevel@tonic-gate 	xdrproc_t xdr_results;
366*0Sstevel@tonic-gate 	caddr_t xdr_location;
367*0Sstevel@tonic-gate 	bool_t has_args;
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_SVC_CLTS_KSEND_START,
370*0Sstevel@tonic-gate 	    "svc_clts_ksend_start:");
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	ASSERT(ud->ud_resp != NULL);
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	/*
375*0Sstevel@tonic-gate 	 * If there is a result procedure specified in the reply message,
376*0Sstevel@tonic-gate 	 * it will be processed in the xdr_replymsg and SVCAUTH_WRAP.
377*0Sstevel@tonic-gate 	 * We need to make sure it won't be processed twice, so we null
378*0Sstevel@tonic-gate 	 * it for xdr_replymsg here.
379*0Sstevel@tonic-gate 	 */
380*0Sstevel@tonic-gate 	has_args = FALSE;
381*0Sstevel@tonic-gate 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
382*0Sstevel@tonic-gate 		msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
383*0Sstevel@tonic-gate 		if ((xdr_results = msg->acpted_rply.ar_results.proc) != NULL) {
384*0Sstevel@tonic-gate 			has_args = TRUE;
385*0Sstevel@tonic-gate 			xdr_location = msg->acpted_rply.ar_results.where;
386*0Sstevel@tonic-gate 			msg->acpted_rply.ar_results.proc = xdr_void;
387*0Sstevel@tonic-gate 			msg->acpted_rply.ar_results.where = NULL;
388*0Sstevel@tonic-gate 		}
389*0Sstevel@tonic-gate 	}
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	if (ud->ud_resp->b_cont == NULL) {
392*0Sstevel@tonic-gate 		/*
393*0Sstevel@tonic-gate 		 * Allocate an initial mblk for the response data.
394*0Sstevel@tonic-gate 		 */
395*0Sstevel@tonic-gate 		while ((mp = allocb(UD_INITSIZE, BPRI_LO)) == NULL) {
396*0Sstevel@tonic-gate 			if (strwaitbuf(UD_INITSIZE, BPRI_LO)) {
397*0Sstevel@tonic-gate 				TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KSEND_END,
398*0Sstevel@tonic-gate 				    "svc_clts_ksend_end:(%S)", "strwaitbuf");
399*0Sstevel@tonic-gate 				return (FALSE);
400*0Sstevel@tonic-gate 			}
401*0Sstevel@tonic-gate 		}
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 		/*
404*0Sstevel@tonic-gate 		 * Initialize the XDR decode stream.  Additional mblks
405*0Sstevel@tonic-gate 		 * will be allocated if necessary.  They will be UD_MAXSIZE
406*0Sstevel@tonic-gate 		 * sized.
407*0Sstevel@tonic-gate 		 */
408*0Sstevel@tonic-gate 		xdrmblk_init(xdrs, mp, XDR_ENCODE, UD_MAXSIZE);
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 		/*
411*0Sstevel@tonic-gate 		 * Leave some space for protocol headers.
412*0Sstevel@tonic-gate 		 */
413*0Sstevel@tonic-gate 		(void) XDR_SETPOS(xdrs, 512);
414*0Sstevel@tonic-gate 		mp->b_rptr += 512;
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 		msg->rm_xid = clone_xprt->xp_xid;
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 		ud->ud_resp->b_cont = mp;
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 		TRACE_0(TR_FAC_KRPC, TR_XDR_REPLYMSG_START,
421*0Sstevel@tonic-gate 		    "xdr_replymsg_start:");
422*0Sstevel@tonic-gate 		if (!(xdr_replymsg(xdrs, msg) &&
423*0Sstevel@tonic-gate 			(!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
424*0Sstevel@tonic-gate 				xdr_results, xdr_location)))) {
425*0Sstevel@tonic-gate 			TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
426*0Sstevel@tonic-gate 			    "xdr_replymsg_end:(%S)", "bad");
427*0Sstevel@tonic-gate 			RPCLOG0(1, "xdr_replymsg/SVCAUTH_WRAP failed\n");
428*0Sstevel@tonic-gate 			goto out;
429*0Sstevel@tonic-gate 		}
430*0Sstevel@tonic-gate 		TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
431*0Sstevel@tonic-gate 		    "xdr_replymsg_end:(%S)", "good");
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	} else if (!(xdr_replymsg_body(xdrs, msg) &&
434*0Sstevel@tonic-gate 		    (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
435*0Sstevel@tonic-gate 				xdr_results, xdr_location)))) {
436*0Sstevel@tonic-gate 		RPCLOG0(1, "xdr_replymsg_body/SVCAUTH_WRAP failed\n");
437*0Sstevel@tonic-gate 		goto out;
438*0Sstevel@tonic-gate 	}
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	msgsz = (int)xmsgsize(ud->ud_resp->b_cont);
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 	if (msgsz <= 0 || (clone_xprt->xp_msg_size != -1 &&
443*0Sstevel@tonic-gate 	    msgsz > clone_xprt->xp_msg_size)) {
444*0Sstevel@tonic-gate #ifdef	DEBUG
445*0Sstevel@tonic-gate 		cmn_err(CE_NOTE,
446*0Sstevel@tonic-gate "KRPC: server response message of %d bytes; transport limits are [0, %d]",
447*0Sstevel@tonic-gate 			msgsz, clone_xprt->xp_msg_size);
448*0Sstevel@tonic-gate #endif
449*0Sstevel@tonic-gate 		goto out;
450*0Sstevel@tonic-gate 	}
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	/*
453*0Sstevel@tonic-gate 	 * Construct the T_unitdata_req.  We take advantage
454*0Sstevel@tonic-gate 	 * of the fact that T_unitdata_ind looks just like
455*0Sstevel@tonic-gate 	 * T_unitdata_req, except for the primitive type.
456*0Sstevel@tonic-gate 	 */
457*0Sstevel@tonic-gate 	udreq = (struct T_unitdata_req *)ud->ud_resp->b_rptr;
458*0Sstevel@tonic-gate 	udreq->PRIM_type = T_UNITDATA_REQ;
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 	put(clone_xprt->xp_wq, ud->ud_resp);
461*0Sstevel@tonic-gate 	stat = TRUE;
462*0Sstevel@tonic-gate 	ud->ud_resp = NULL;
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate out:
465*0Sstevel@tonic-gate 	if (stat == FALSE) {
466*0Sstevel@tonic-gate 		freemsg(ud->ud_resp);
467*0Sstevel@tonic-gate 		ud->ud_resp = NULL;
468*0Sstevel@tonic-gate 	}
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	/*
471*0Sstevel@tonic-gate 	 * This is completely disgusting.  If public is set it is
472*0Sstevel@tonic-gate 	 * a pointer to a structure whose first field is the address
473*0Sstevel@tonic-gate 	 * of the function to free that structure and any related
474*0Sstevel@tonic-gate 	 * stuff.  (see rrokfree in nfs_xdr.c).
475*0Sstevel@tonic-gate 	 */
476*0Sstevel@tonic-gate 	if (xdrs->x_public) {
477*0Sstevel@tonic-gate 		/* LINTED pointer alignment */
478*0Sstevel@tonic-gate 		(**((int (**)())xdrs->x_public))(xdrs->x_public);
479*0Sstevel@tonic-gate 	}
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KSEND_END,
482*0Sstevel@tonic-gate 	    "svc_clts_ksend_end:(%S)", "done");
483*0Sstevel@tonic-gate 	return (stat);
484*0Sstevel@tonic-gate }
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate /*
487*0Sstevel@tonic-gate  * Deserialize arguments.
488*0Sstevel@tonic-gate  */
489*0Sstevel@tonic-gate static bool_t
490*0Sstevel@tonic-gate svc_clts_kgetargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
491*0Sstevel@tonic-gate     caddr_t args_ptr)
492*0Sstevel@tonic-gate {
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
495*0Sstevel@tonic-gate 	return (SVCAUTH_UNWRAP(&clone_xprt->xp_auth, &clone_xprt->xp_xdrin,
496*0Sstevel@tonic-gate 				xdr_args, args_ptr));
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate }
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate static bool_t
501*0Sstevel@tonic-gate svc_clts_kfreeargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
502*0Sstevel@tonic-gate     caddr_t args_ptr)
503*0Sstevel@tonic-gate {
504*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
505*0Sstevel@tonic-gate 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
506*0Sstevel@tonic-gate 	XDR *xdrs = &clone_xprt->xp_xdrin;
507*0Sstevel@tonic-gate 	bool_t retval;
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 	if (args_ptr) {
510*0Sstevel@tonic-gate 		xdrs->x_op = XDR_FREE;
511*0Sstevel@tonic-gate 		retval = (*xdr_args)(xdrs, args_ptr);
512*0Sstevel@tonic-gate 	} else
513*0Sstevel@tonic-gate 		retval = TRUE;
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 	if (ud->ud_inmp) {
516*0Sstevel@tonic-gate 		freemsg(ud->ud_inmp);
517*0Sstevel@tonic-gate 		ud->ud_inmp = NULL;
518*0Sstevel@tonic-gate 	}
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 	return (retval);
521*0Sstevel@tonic-gate }
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate static int32_t *
524*0Sstevel@tonic-gate svc_clts_kgetres(SVCXPRT *clone_xprt, int size)
525*0Sstevel@tonic-gate {
526*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
527*0Sstevel@tonic-gate 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
528*0Sstevel@tonic-gate 	XDR *xdrs = &clone_xprt->xp_xdrout;
529*0Sstevel@tonic-gate 	mblk_t *mp;
530*0Sstevel@tonic-gate 	int32_t *buf;
531*0Sstevel@tonic-gate 	struct rpc_msg rply;
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 	/*
534*0Sstevel@tonic-gate 	 * Allocate an initial mblk for the response data.
535*0Sstevel@tonic-gate 	 */
536*0Sstevel@tonic-gate 	while ((mp = allocb(UD_INITSIZE, BPRI_LO)) == NULL) {
537*0Sstevel@tonic-gate 		if (strwaitbuf(UD_INITSIZE, BPRI_LO)) {
538*0Sstevel@tonic-gate 			return (FALSE);
539*0Sstevel@tonic-gate 		}
540*0Sstevel@tonic-gate 	}
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	mp->b_cont = NULL;
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	/*
545*0Sstevel@tonic-gate 	 * Initialize the XDR decode stream.  Additional mblks
546*0Sstevel@tonic-gate 	 * will be allocated if necessary.  They will be UD_MAXSIZE
547*0Sstevel@tonic-gate 	 * sized.
548*0Sstevel@tonic-gate 	 */
549*0Sstevel@tonic-gate 	xdrmblk_init(xdrs, mp, XDR_ENCODE, UD_MAXSIZE);
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 	/*
552*0Sstevel@tonic-gate 	 * Leave some space for protocol headers.
553*0Sstevel@tonic-gate 	 */
554*0Sstevel@tonic-gate 	(void) XDR_SETPOS(xdrs, 512);
555*0Sstevel@tonic-gate 	mp->b_rptr += 512;
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 	/*
558*0Sstevel@tonic-gate 	 * Assume a successful RPC since most of them are.
559*0Sstevel@tonic-gate 	 */
560*0Sstevel@tonic-gate 	rply.rm_xid = clone_xprt->xp_xid;
561*0Sstevel@tonic-gate 	rply.rm_direction = REPLY;
562*0Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
563*0Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
564*0Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = SUCCESS;
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 	if (!xdr_replymsg_hdr(xdrs, &rply)) {
567*0Sstevel@tonic-gate 		freeb(mp);
568*0Sstevel@tonic-gate 		return (NULL);
569*0Sstevel@tonic-gate 	}
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 	buf = XDR_INLINE(xdrs, size);
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 	if (buf == NULL)
574*0Sstevel@tonic-gate 		freeb(mp);
575*0Sstevel@tonic-gate 	else
576*0Sstevel@tonic-gate 		ud->ud_resp->b_cont = mp;
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 	return (buf);
579*0Sstevel@tonic-gate }
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate static void
582*0Sstevel@tonic-gate svc_clts_kfreeres(SVCXPRT *clone_xprt)
583*0Sstevel@tonic-gate {
584*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
585*0Sstevel@tonic-gate 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 	if (ud->ud_resp == NULL || ud->ud_resp->b_cont == NULL)
588*0Sstevel@tonic-gate 		return;
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 	/*
591*0Sstevel@tonic-gate 	 * SVC_FREERES() is called whenever the server decides not to
592*0Sstevel@tonic-gate 	 * send normal reply. Thus, we expect only one mblk to be allocated,
593*0Sstevel@tonic-gate 	 * because we have not attempted any XDR encoding.
594*0Sstevel@tonic-gate 	 * If we do any XDR encoding and we get an error, then SVC_REPLY()
595*0Sstevel@tonic-gate 	 * will freemsg(ud->ud_resp);
596*0Sstevel@tonic-gate 	 */
597*0Sstevel@tonic-gate 	ASSERT(ud->ud_resp->b_cont->b_cont == NULL);
598*0Sstevel@tonic-gate 	freeb(ud->ud_resp->b_cont);
599*0Sstevel@tonic-gate 	ud->ud_resp->b_cont = NULL;
600*0Sstevel@tonic-gate }
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate /*
603*0Sstevel@tonic-gate  * the dup cacheing routines below provide a cache of non-failure
604*0Sstevel@tonic-gate  * transaction id's.  rpc service routines can use this to detect
605*0Sstevel@tonic-gate  * retransmissions and re-send a non-failure response.
606*0Sstevel@tonic-gate  */
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate /*
609*0Sstevel@tonic-gate  * MAXDUPREQS is the number of cached items.  It should be adjusted
610*0Sstevel@tonic-gate  * to the service load so that there is likely to be a response entry
611*0Sstevel@tonic-gate  * when the first retransmission comes in.
612*0Sstevel@tonic-gate  */
613*0Sstevel@tonic-gate #define	MAXDUPREQS	1024
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate /*
616*0Sstevel@tonic-gate  * This should be appropriately scaled to MAXDUPREQS.
617*0Sstevel@tonic-gate  */
618*0Sstevel@tonic-gate #define	DRHASHSZ	257
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate #if ((DRHASHSZ & (DRHASHSZ - 1)) == 0)
621*0Sstevel@tonic-gate #define	XIDHASH(xid)	((xid) & (DRHASHSZ - 1))
622*0Sstevel@tonic-gate #else
623*0Sstevel@tonic-gate #define	XIDHASH(xid)	((xid) % DRHASHSZ)
624*0Sstevel@tonic-gate #endif
625*0Sstevel@tonic-gate #define	DRHASH(dr)	XIDHASH((dr)->dr_xid)
626*0Sstevel@tonic-gate #define	REQTOXID(req)	((req)->rq_xprt->xp_xid)
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate static int	ndupreqs = 0;
629*0Sstevel@tonic-gate static int	maxdupreqs = MAXDUPREQS;
630*0Sstevel@tonic-gate static kmutex_t dupreq_lock;
631*0Sstevel@tonic-gate static struct dupreq *drhashtbl[DRHASHSZ];
632*0Sstevel@tonic-gate static int	drhashstat[DRHASHSZ];
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate static void unhash(struct dupreq *);
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate /*
637*0Sstevel@tonic-gate  * drmru points to the head of a circular linked list in lru order.
638*0Sstevel@tonic-gate  * drmru->dr_next == drlru
639*0Sstevel@tonic-gate  */
640*0Sstevel@tonic-gate struct dupreq *drmru;
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate /*
643*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
644*0Sstevel@tonic-gate  * svc_clts_kdup
645*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
646*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
647*0Sstevel@tonic-gate  *
648*0Sstevel@tonic-gate  * svc_clts_kdup searches the request cache and returns 0 if the
649*0Sstevel@tonic-gate  * request is not found in the cache.  If it is found, then it
650*0Sstevel@tonic-gate  * returns the state of the request (in progress or done) and
651*0Sstevel@tonic-gate  * the status or attributes that were part of the original reply.
652*0Sstevel@tonic-gate  *
653*0Sstevel@tonic-gate  * If DUP_DONE (there is a duplicate) svc_clts_kdup copies over the
654*0Sstevel@tonic-gate  * value of the response. In that case, also return in *dupcachedp
655*0Sstevel@tonic-gate  * whether the response free routine is cached in the dupreq - in which case
656*0Sstevel@tonic-gate  * the caller should not be freeing it, because it will be done later
657*0Sstevel@tonic-gate  * in the svc_clts_kdup code when the dupreq is reused.
658*0Sstevel@tonic-gate  */
659*0Sstevel@tonic-gate static int
660*0Sstevel@tonic-gate svc_clts_kdup(struct svc_req *req, caddr_t res, int size, struct dupreq **drpp,
661*0Sstevel@tonic-gate 	bool_t *dupcachedp)
662*0Sstevel@tonic-gate {
663*0Sstevel@tonic-gate 	struct rpc_clts_server *stats = CLONE2STATS(req->rq_xprt);
664*0Sstevel@tonic-gate 	struct dupreq *dr;
665*0Sstevel@tonic-gate 	uint32_t xid;
666*0Sstevel@tonic-gate 	uint32_t drhash;
667*0Sstevel@tonic-gate 	int status;
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 	xid = REQTOXID(req);
670*0Sstevel@tonic-gate 	mutex_enter(&dupreq_lock);
671*0Sstevel@tonic-gate 	RSSTAT_INCR(stats, rsdupchecks);
672*0Sstevel@tonic-gate 	/*
673*0Sstevel@tonic-gate 	 * Check to see whether an entry already exists in the cache.
674*0Sstevel@tonic-gate 	 */
675*0Sstevel@tonic-gate 	dr = drhashtbl[XIDHASH(xid)];
676*0Sstevel@tonic-gate 	while (dr != NULL) {
677*0Sstevel@tonic-gate 		if (dr->dr_xid == xid &&
678*0Sstevel@tonic-gate 		    dr->dr_proc == req->rq_proc &&
679*0Sstevel@tonic-gate 		    dr->dr_prog == req->rq_prog &&
680*0Sstevel@tonic-gate 		    dr->dr_vers == req->rq_vers &&
681*0Sstevel@tonic-gate 		    dr->dr_addr.len == req->rq_xprt->xp_rtaddr.len &&
682*0Sstevel@tonic-gate 		    bcmp(dr->dr_addr.buf, req->rq_xprt->xp_rtaddr.buf,
683*0Sstevel@tonic-gate 		    dr->dr_addr.len) == 0) {
684*0Sstevel@tonic-gate 			status = dr->dr_status;
685*0Sstevel@tonic-gate 			if (status == DUP_DONE) {
686*0Sstevel@tonic-gate 				bcopy(dr->dr_resp.buf, res, size);
687*0Sstevel@tonic-gate 				if (dupcachedp != NULL)
688*0Sstevel@tonic-gate 					*dupcachedp = (dr->dr_resfree != NULL);
689*0Sstevel@tonic-gate 			} else {
690*0Sstevel@tonic-gate 				dr->dr_status = DUP_INPROGRESS;
691*0Sstevel@tonic-gate 				*drpp = dr;
692*0Sstevel@tonic-gate 			}
693*0Sstevel@tonic-gate 			RSSTAT_INCR(stats, rsdupreqs);
694*0Sstevel@tonic-gate 			mutex_exit(&dupreq_lock);
695*0Sstevel@tonic-gate 			return (status);
696*0Sstevel@tonic-gate 		}
697*0Sstevel@tonic-gate 		dr = dr->dr_chain;
698*0Sstevel@tonic-gate 	}
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 	/*
701*0Sstevel@tonic-gate 	 * There wasn't an entry, either allocate a new one or recycle
702*0Sstevel@tonic-gate 	 * an old one.
703*0Sstevel@tonic-gate 	 */
704*0Sstevel@tonic-gate 	if (ndupreqs < maxdupreqs) {
705*0Sstevel@tonic-gate 		dr = kmem_alloc(sizeof (*dr), KM_NOSLEEP);
706*0Sstevel@tonic-gate 		if (dr == NULL) {
707*0Sstevel@tonic-gate 			mutex_exit(&dupreq_lock);
708*0Sstevel@tonic-gate 			return (DUP_ERROR);
709*0Sstevel@tonic-gate 		}
710*0Sstevel@tonic-gate 		dr->dr_resp.buf = NULL;
711*0Sstevel@tonic-gate 		dr->dr_resp.maxlen = 0;
712*0Sstevel@tonic-gate 		dr->dr_addr.buf = NULL;
713*0Sstevel@tonic-gate 		dr->dr_addr.maxlen = 0;
714*0Sstevel@tonic-gate 		if (drmru) {
715*0Sstevel@tonic-gate 			dr->dr_next = drmru->dr_next;
716*0Sstevel@tonic-gate 			drmru->dr_next = dr;
717*0Sstevel@tonic-gate 		} else {
718*0Sstevel@tonic-gate 			dr->dr_next = dr;
719*0Sstevel@tonic-gate 		}
720*0Sstevel@tonic-gate 		ndupreqs++;
721*0Sstevel@tonic-gate 	} else {
722*0Sstevel@tonic-gate 		dr = drmru->dr_next;
723*0Sstevel@tonic-gate 		while (dr->dr_status == DUP_INPROGRESS) {
724*0Sstevel@tonic-gate 			dr = dr->dr_next;
725*0Sstevel@tonic-gate 			if (dr == drmru->dr_next) {
726*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "svc_clts_kdup no slots free");
727*0Sstevel@tonic-gate 				mutex_exit(&dupreq_lock);
728*0Sstevel@tonic-gate 				return (DUP_ERROR);
729*0Sstevel@tonic-gate 			}
730*0Sstevel@tonic-gate 		}
731*0Sstevel@tonic-gate 		unhash(dr);
732*0Sstevel@tonic-gate 		if (dr->dr_resfree) {
733*0Sstevel@tonic-gate 			(*dr->dr_resfree)(dr->dr_resp.buf);
734*0Sstevel@tonic-gate 		}
735*0Sstevel@tonic-gate 	}
736*0Sstevel@tonic-gate 	dr->dr_resfree = NULL;
737*0Sstevel@tonic-gate 	drmru = dr;
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate 	dr->dr_xid = REQTOXID(req);
740*0Sstevel@tonic-gate 	dr->dr_prog = req->rq_prog;
741*0Sstevel@tonic-gate 	dr->dr_vers = req->rq_vers;
742*0Sstevel@tonic-gate 	dr->dr_proc = req->rq_proc;
743*0Sstevel@tonic-gate 	if (dr->dr_addr.maxlen < req->rq_xprt->xp_rtaddr.len) {
744*0Sstevel@tonic-gate 		if (dr->dr_addr.buf != NULL)
745*0Sstevel@tonic-gate 			kmem_free(dr->dr_addr.buf, dr->dr_addr.maxlen);
746*0Sstevel@tonic-gate 		dr->dr_addr.maxlen = req->rq_xprt->xp_rtaddr.len;
747*0Sstevel@tonic-gate 		dr->dr_addr.buf = kmem_alloc(dr->dr_addr.maxlen,
748*0Sstevel@tonic-gate 		    KM_NOSLEEP);
749*0Sstevel@tonic-gate 		if (dr->dr_addr.buf == NULL) {
750*0Sstevel@tonic-gate 			dr->dr_addr.maxlen = 0;
751*0Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
752*0Sstevel@tonic-gate 			mutex_exit(&dupreq_lock);
753*0Sstevel@tonic-gate 			return (DUP_ERROR);
754*0Sstevel@tonic-gate 		}
755*0Sstevel@tonic-gate 	}
756*0Sstevel@tonic-gate 	dr->dr_addr.len = req->rq_xprt->xp_rtaddr.len;
757*0Sstevel@tonic-gate 	bcopy(req->rq_xprt->xp_rtaddr.buf, dr->dr_addr.buf, dr->dr_addr.len);
758*0Sstevel@tonic-gate 	if (dr->dr_resp.maxlen < size) {
759*0Sstevel@tonic-gate 		if (dr->dr_resp.buf != NULL)
760*0Sstevel@tonic-gate 			kmem_free(dr->dr_resp.buf, dr->dr_resp.maxlen);
761*0Sstevel@tonic-gate 		dr->dr_resp.maxlen = (unsigned int)size;
762*0Sstevel@tonic-gate 		dr->dr_resp.buf = kmem_alloc(size, KM_NOSLEEP);
763*0Sstevel@tonic-gate 		if (dr->dr_resp.buf == NULL) {
764*0Sstevel@tonic-gate 			dr->dr_resp.maxlen = 0;
765*0Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
766*0Sstevel@tonic-gate 			mutex_exit(&dupreq_lock);
767*0Sstevel@tonic-gate 			return (DUP_ERROR);
768*0Sstevel@tonic-gate 		}
769*0Sstevel@tonic-gate 	}
770*0Sstevel@tonic-gate 	dr->dr_status = DUP_INPROGRESS;
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
773*0Sstevel@tonic-gate 	dr->dr_chain = drhashtbl[drhash];
774*0Sstevel@tonic-gate 	drhashtbl[drhash] = dr;
775*0Sstevel@tonic-gate 	drhashstat[drhash]++;
776*0Sstevel@tonic-gate 	mutex_exit(&dupreq_lock);
777*0Sstevel@tonic-gate 	*drpp = dr;
778*0Sstevel@tonic-gate 	return (DUP_NEW);
779*0Sstevel@tonic-gate }
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate /*
782*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
783*0Sstevel@tonic-gate  * svc_clts_kdupdone
784*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
785*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
786*0Sstevel@tonic-gate  *
787*0Sstevel@tonic-gate  * svc_clts_kdupdone marks the request done (DUP_DONE or DUP_DROP)
788*0Sstevel@tonic-gate  * and stores the response.
789*0Sstevel@tonic-gate  */
790*0Sstevel@tonic-gate static void
791*0Sstevel@tonic-gate svc_clts_kdupdone(struct dupreq *dr, caddr_t res, void (*dis_resfree)(),
792*0Sstevel@tonic-gate 	int size, int status)
793*0Sstevel@tonic-gate {
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate 	ASSERT(dr->dr_resfree == NULL);
796*0Sstevel@tonic-gate 	if (status == DUP_DONE) {
797*0Sstevel@tonic-gate 		bcopy(res, dr->dr_resp.buf, size);
798*0Sstevel@tonic-gate 		dr->dr_resfree = dis_resfree;
799*0Sstevel@tonic-gate 	}
800*0Sstevel@tonic-gate 	dr->dr_status = status;
801*0Sstevel@tonic-gate }
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate /*
804*0Sstevel@tonic-gate  * This routine expects that the mutex, dupreq_lock, is already held.
805*0Sstevel@tonic-gate  */
806*0Sstevel@tonic-gate static void
807*0Sstevel@tonic-gate unhash(struct dupreq *dr)
808*0Sstevel@tonic-gate {
809*0Sstevel@tonic-gate 	struct dupreq *drt;
810*0Sstevel@tonic-gate 	struct dupreq *drtprev = NULL;
811*0Sstevel@tonic-gate 	uint32_t drhash;
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dupreq_lock));
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
816*0Sstevel@tonic-gate 	drt = drhashtbl[drhash];
817*0Sstevel@tonic-gate 	while (drt != NULL) {
818*0Sstevel@tonic-gate 		if (drt == dr) {
819*0Sstevel@tonic-gate 			drhashstat[drhash]--;
820*0Sstevel@tonic-gate 			if (drtprev == NULL) {
821*0Sstevel@tonic-gate 				drhashtbl[drhash] = drt->dr_chain;
822*0Sstevel@tonic-gate 			} else {
823*0Sstevel@tonic-gate 				drtprev->dr_chain = drt->dr_chain;
824*0Sstevel@tonic-gate 			}
825*0Sstevel@tonic-gate 			return;
826*0Sstevel@tonic-gate 		}
827*0Sstevel@tonic-gate 		drtprev = drt;
828*0Sstevel@tonic-gate 		drt = drt->dr_chain;
829*0Sstevel@tonic-gate 	}
830*0Sstevel@tonic-gate }
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate void
833*0Sstevel@tonic-gate svc_clts_stats_init(zoneid_t zoneid, struct rpc_clts_server **statsp)
834*0Sstevel@tonic-gate {
835*0Sstevel@tonic-gate 	kstat_t *ksp;
836*0Sstevel@tonic-gate 	kstat_named_t *knp;
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 	knp = rpcstat_zone_init_common(zoneid, "unix", "rpc_clts_server",
839*0Sstevel@tonic-gate 	    (const kstat_named_t *)&clts_rsstat_tmpl,
840*0Sstevel@tonic-gate 	    sizeof (clts_rsstat_tmpl));
841*0Sstevel@tonic-gate 	/*
842*0Sstevel@tonic-gate 	 * Backwards compatibility for old kstat clients
843*0Sstevel@tonic-gate 	 */
844*0Sstevel@tonic-gate 	ksp = kstat_create_zone("unix", 0, "rpc_server", "rpc",
845*0Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, clts_rsstat_ndata,
846*0Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE, zoneid);
847*0Sstevel@tonic-gate 	if (ksp) {
848*0Sstevel@tonic-gate 		ksp->ks_data = knp;
849*0Sstevel@tonic-gate 		kstat_install(ksp);
850*0Sstevel@tonic-gate 	}
851*0Sstevel@tonic-gate 	*statsp = (struct rpc_clts_server *)knp;
852*0Sstevel@tonic-gate }
853*0Sstevel@tonic-gate 
854*0Sstevel@tonic-gate void
855*0Sstevel@tonic-gate svc_clts_stats_fini(zoneid_t zoneid, struct rpc_clts_server **statsp)
856*0Sstevel@tonic-gate {
857*0Sstevel@tonic-gate 	rpcstat_zone_fini_common(zoneid, "unix", "rpc_clts_server");
858*0Sstevel@tonic-gate 	kstat_delete_byname_zone("unix", 0, "rpc_server", zoneid);
859*0Sstevel@tonic-gate 	kmem_free(*statsp, sizeof (clts_rsstat_tmpl));
860*0Sstevel@tonic-gate }
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate void
863*0Sstevel@tonic-gate svc_clts_init()
864*0Sstevel@tonic-gate {
865*0Sstevel@tonic-gate 	/*
866*0Sstevel@tonic-gate 	 * Check to make sure that the clts private data will fit into
867*0Sstevel@tonic-gate 	 * the stack buffer allocated by svc_run.  The compiler should
868*0Sstevel@tonic-gate 	 * remove this check, but it's a safety net if the udp_data
869*0Sstevel@tonic-gate 	 * structure ever changes.
870*0Sstevel@tonic-gate 	 */
871*0Sstevel@tonic-gate 	/*CONSTANTCONDITION*/
872*0Sstevel@tonic-gate 	ASSERT(sizeof (struct udp_data) <= SVC_P2LEN);
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate 	mutex_init(&dupreq_lock, NULL, MUTEX_DEFAULT, NULL);
875*0Sstevel@tonic-gate }
876