xref: /titanic_53/usr/src/uts/common/io/timod.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
23*7c478bd9Sstevel@tonic-gate /*
24*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
26*7c478bd9Sstevel@tonic-gate  */
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  * Transport Interface Library cooperating module - issue 2
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
38*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
43*7c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
44*7c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/timod.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/suntpi.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
57*7c478bd9Sstevel@tonic-gate #include <c2/audit.h>
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate static struct streamtab timinfo;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate static struct fmodsw fsw = {
68*7c478bd9Sstevel@tonic-gate 	"timod",
69*7c478bd9Sstevel@tonic-gate 	&timinfo,
70*7c478bd9Sstevel@tonic-gate 	D_MTQPAIR | D_MP,
71*7c478bd9Sstevel@tonic-gate };
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /*
74*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
75*7c478bd9Sstevel@tonic-gate  */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
78*7c478bd9Sstevel@tonic-gate 	&mod_strmodops, "transport interface str mod", &fsw
79*7c478bd9Sstevel@tonic-gate };
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
82*7c478bd9Sstevel@tonic-gate 	MODREV_1, &modlstrmod, NULL
83*7c478bd9Sstevel@tonic-gate };
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate static krwlock_t	tim_list_rwlock;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /*
88*7c478bd9Sstevel@tonic-gate  * This module keeps track of capabilities of underlying transport. Information
89*7c478bd9Sstevel@tonic-gate  * is persistent through module invocations (open/close). Currently it remembers
90*7c478bd9Sstevel@tonic-gate  * whether underlying transport supports TI_GET{MY,PEER}NAME ioctls and
91*7c478bd9Sstevel@tonic-gate  * T_CAPABILITY_REQ message. This module either passes ioctl/messages to the
92*7c478bd9Sstevel@tonic-gate  * transport or emulates it when transport doesn't understand these
93*7c478bd9Sstevel@tonic-gate  * ioctl/messages.
94*7c478bd9Sstevel@tonic-gate  *
95*7c478bd9Sstevel@tonic-gate  * It is assumed that transport supports T_CAPABILITY_REQ when timod receives
96*7c478bd9Sstevel@tonic-gate  * T_CAPABILITY_ACK from the transport. There is no current standard describing
97*7c478bd9Sstevel@tonic-gate  * transport behaviour when it receives unknown message type, so following
98*7c478bd9Sstevel@tonic-gate  * reactions are expected and handled:
99*7c478bd9Sstevel@tonic-gate  *
100*7c478bd9Sstevel@tonic-gate  * 1) Transport drops unknown T_CAPABILITY_REQ message type. In this case timod
101*7c478bd9Sstevel@tonic-gate  *    will wait for tcap_wait time and assume that transport doesn't provide
102*7c478bd9Sstevel@tonic-gate  *    this message type. T_CAPABILITY_REQ should never travel over the wire, so
103*7c478bd9Sstevel@tonic-gate  *    timeout value should only take into consideration internal processing time
104*7c478bd9Sstevel@tonic-gate  *    for the message. From user standpoint it may mean that an application will
105*7c478bd9Sstevel@tonic-gate  *    hang for TCAP_WAIT time in the kernel the first time this message is used
106*7c478bd9Sstevel@tonic-gate  *    with some particular transport (e.g. TCP/IP) during system uptime.
107*7c478bd9Sstevel@tonic-gate  *
108*7c478bd9Sstevel@tonic-gate  * 2) Transport responds with T_ERROR_ACK specifying T_CAPABILITY_REQ as
109*7c478bd9Sstevel@tonic-gate  *    original message type. In this case it is assumed that transport doesn't
110*7c478bd9Sstevel@tonic-gate  *    support it (which may not always be true - some transports return
111*7c478bd9Sstevel@tonic-gate  *    T_ERROR_ACK in other cases like lack of system memory).
112*7c478bd9Sstevel@tonic-gate  *
113*7c478bd9Sstevel@tonic-gate  * 3) Transport responds with M_ERROR, effectively shutting down the
114*7c478bd9Sstevel@tonic-gate  *    stream. Unfortunately there is no standard way to pass the reason of
115*7c478bd9Sstevel@tonic-gate  *    M_ERROR message back to the caller, so it is assumed that if M_ERROR was
116*7c478bd9Sstevel@tonic-gate  *    sent in response to T_CAPABILITY_REQ message, transport doesn't support
117*7c478bd9Sstevel@tonic-gate  *    it.
118*7c478bd9Sstevel@tonic-gate  *
119*7c478bd9Sstevel@tonic-gate  * It is possible under certain circumstances that timod will incorrectly assume
120*7c478bd9Sstevel@tonic-gate  * that underlying transport doesn't provide T_CAPABILITY_REQ message type. In
121*7c478bd9Sstevel@tonic-gate  * this "worst-case" scenario timod will emulate its functionality by itself and
122*7c478bd9Sstevel@tonic-gate  * will provide only TC1_INFO capability. All other bits in CAP_bits1 field are
123*7c478bd9Sstevel@tonic-gate  * cleaned. TC1_INFO is emulated by sending T_INFO_REQ down to transport
124*7c478bd9Sstevel@tonic-gate  * provider.
125*7c478bd9Sstevel@tonic-gate  */
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /*
128*7c478bd9Sstevel@tonic-gate  * Notes about locking:
129*7c478bd9Sstevel@tonic-gate  *
130*7c478bd9Sstevel@tonic-gate  * tim_list_rwlock protects the list of tim_tim structures itself.  When this
131*7c478bd9Sstevel@tonic-gate  * lock is held, the list itself is stable, but the contents of the entries
132*7c478bd9Sstevel@tonic-gate  * themselves might not be.
133*7c478bd9Sstevel@tonic-gate  *
134*7c478bd9Sstevel@tonic-gate  * The rest of the members are generally protected by D_MTQPAIR, which
135*7c478bd9Sstevel@tonic-gate  * specifies a default exclusive inner perimeter.  If you're looking at
136*7c478bd9Sstevel@tonic-gate  * q->q_ptr, then it's stable.
137*7c478bd9Sstevel@tonic-gate  *
138*7c478bd9Sstevel@tonic-gate  * There's one exception to this rule: tim_peer{maxlen,len,name}.  These members
139*7c478bd9Sstevel@tonic-gate  * are touched without entering the associated STREAMS perimeter because we
140*7c478bd9Sstevel@tonic-gate  * get the pointer via tim_findlink() rather than q_ptr.  These are protected
141*7c478bd9Sstevel@tonic-gate  * by tim_mutex instead.  If you don't hold that lock, don't look at them.
142*7c478bd9Sstevel@tonic-gate  *
143*7c478bd9Sstevel@tonic-gate  * (It would be possible to separate out the 'set by T_CONN_RES' cases from the
144*7c478bd9Sstevel@tonic-gate  * others, but there appears to be no reason to do so.)
145*7c478bd9Sstevel@tonic-gate  */
146*7c478bd9Sstevel@tonic-gate struct tim_tim {
147*7c478bd9Sstevel@tonic-gate 	uint32_t	tim_flags;
148*7c478bd9Sstevel@tonic-gate 	t_uscalar_t	tim_backlog;
149*7c478bd9Sstevel@tonic-gate 	mblk_t		*tim_iocsave;
150*7c478bd9Sstevel@tonic-gate 	t_scalar_t	tim_mymaxlen;
151*7c478bd9Sstevel@tonic-gate 	t_scalar_t	tim_mylen;
152*7c478bd9Sstevel@tonic-gate 	caddr_t		tim_myname;
153*7c478bd9Sstevel@tonic-gate 	t_scalar_t	tim_peermaxlen;
154*7c478bd9Sstevel@tonic-gate 	t_scalar_t	tim_peerlen;
155*7c478bd9Sstevel@tonic-gate 	caddr_t		tim_peername;
156*7c478bd9Sstevel@tonic-gate 	cred_t		*tim_peercred;
157*7c478bd9Sstevel@tonic-gate 	mblk_t		*tim_consave;
158*7c478bd9Sstevel@tonic-gate 	bufcall_id_t	tim_wbufcid;
159*7c478bd9Sstevel@tonic-gate 	bufcall_id_t	tim_rbufcid;
160*7c478bd9Sstevel@tonic-gate 	timeout_id_t	tim_wtimoutid;
161*7c478bd9Sstevel@tonic-gate 	timeout_id_t	tim_rtimoutid;
162*7c478bd9Sstevel@tonic-gate 	/* Protected by the global tim_list_rwlock for all instances */
163*7c478bd9Sstevel@tonic-gate 	struct tim_tim	*tim_next;
164*7c478bd9Sstevel@tonic-gate 	struct tim_tim	**tim_ptpn;
165*7c478bd9Sstevel@tonic-gate 	t_uscalar_t	tim_acceptor;
166*7c478bd9Sstevel@tonic-gate 	t_scalar_t	tim_saved_prim;		/* Primitive from message */
167*7c478bd9Sstevel@tonic-gate 						/*  part of ioctl. */
168*7c478bd9Sstevel@tonic-gate 	timeout_id_t	tim_tcap_timoutid;	/* For T_CAP_REQ timeout */
169*7c478bd9Sstevel@tonic-gate 	tpi_provinfo_t	*tim_provinfo;		/* Transport description */
170*7c478bd9Sstevel@tonic-gate 	kmutex_t	tim_mutex;		/* protect tim_peer* */
171*7c478bd9Sstevel@tonic-gate 	pid_t		tim_cpid;
172*7c478bd9Sstevel@tonic-gate };
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate /*
176*7c478bd9Sstevel@tonic-gate  * Local flags used with tim_flags field in instance structure of
177*7c478bd9Sstevel@tonic-gate  * type 'struct _ti_user' declared above.
178*7c478bd9Sstevel@tonic-gate  * Historical note:
179*7c478bd9Sstevel@tonic-gate  * This namespace constants were previously declared in a
180*7c478bd9Sstevel@tonic-gate  * a very messed up namespace in timod.h
181*7c478bd9Sstevel@tonic-gate  *
182*7c478bd9Sstevel@tonic-gate  * There may be 3 states for transport:
183*7c478bd9Sstevel@tonic-gate  *
184*7c478bd9Sstevel@tonic-gate  * 1) It provides T_CAPABILITY_REQ
185*7c478bd9Sstevel@tonic-gate  * 2) It does not provide T_CAPABILITY_REQ
186*7c478bd9Sstevel@tonic-gate  * 3) It is not known yet whether transport provides T_CAPABILITY_REQ or not.
187*7c478bd9Sstevel@tonic-gate  *
188*7c478bd9Sstevel@tonic-gate  * It is assumed that the underlying transport either provides
189*7c478bd9Sstevel@tonic-gate  * T_CAPABILITY_REQ or not and this does not changes during the
190*7c478bd9Sstevel@tonic-gate  * system lifetime.
191*7c478bd9Sstevel@tonic-gate  *
192*7c478bd9Sstevel@tonic-gate  */
193*7c478bd9Sstevel@tonic-gate #define	PEEK_RDQ_EXPIND 0x0001	/* look for expinds on stream rd queues */
194*7c478bd9Sstevel@tonic-gate #define	WAITIOCACK	0x0002	/* waiting for info for ioctl act	*/
195*7c478bd9Sstevel@tonic-gate #define	CLTS		0x0004	/* connectionless transport		*/
196*7c478bd9Sstevel@tonic-gate #define	COTS		0x0008	/* connection-oriented transport	*/
197*7c478bd9Sstevel@tonic-gate #define	CONNWAIT	0x0010	/* waiting for connect confirmation	*/
198*7c478bd9Sstevel@tonic-gate #define	LOCORDREL	0x0020	/* local end has orderly released	*/
199*7c478bd9Sstevel@tonic-gate #define	REMORDREL	0x0040	/* remote end had orderly released	*/
200*7c478bd9Sstevel@tonic-gate #define	NAMEPROC	0x0080	/* processing a NAME ioctl		*/
201*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
202*7c478bd9Sstevel@tonic-gate #define	DO_MYNAME	0x0100	/* timod handles TI_GETMYNAME		*/
203*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
204*7c478bd9Sstevel@tonic-gate #define	DO_PEERNAME	0x0200	/* timod handles TI_GETPEERNAME		*/
205*7c478bd9Sstevel@tonic-gate #define	TI_CAP_RECVD	0x0400	/* TI_CAPABILITY received		*/
206*7c478bd9Sstevel@tonic-gate #define	CAP_WANTS_INFO	0x0800	/* TI_CAPABILITY has TC1_INFO set	*/
207*7c478bd9Sstevel@tonic-gate #define	WAIT_IOCINFOACK	0x1000	/* T_INFO_REQ generated from ioctl	*/
208*7c478bd9Sstevel@tonic-gate #define	WAIT_CONNRESACK	0x2000	/* waiting for T_OK_ACK to T_CONN_RES	*/
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate /* Debugging facilities */
212*7c478bd9Sstevel@tonic-gate /*
213*7c478bd9Sstevel@tonic-gate  * Logging needed for debugging timod should only appear in DEBUG kernel.
214*7c478bd9Sstevel@tonic-gate  */
215*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
216*7c478bd9Sstevel@tonic-gate #define	TILOG(msg, arg)		tilog((msg), (arg))
217*7c478bd9Sstevel@tonic-gate #define	TILOGP(msg, arg)	tilogp((msg), (arg))
218*7c478bd9Sstevel@tonic-gate #else
219*7c478bd9Sstevel@tonic-gate #define	TILOG(msg, arg)
220*7c478bd9Sstevel@tonic-gate #define	TILOGP(msg, arg)
221*7c478bd9Sstevel@tonic-gate #endif
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate /*
225*7c478bd9Sstevel@tonic-gate  * Sleep timeout for T_CAPABILITY_REQ. This message never travels across
226*7c478bd9Sstevel@tonic-gate  * network, so timeout value should be enough to cover all internal processing
227*7c478bd9Sstevel@tonic-gate  * time.
228*7c478bd9Sstevel@tonic-gate  */
229*7c478bd9Sstevel@tonic-gate clock_t tim_tcap_wait = 2;
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate /* Sleep timeout in tim_recover() */
232*7c478bd9Sstevel@tonic-gate #define	TIMWAIT	(1*hz)
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate /*
235*7c478bd9Sstevel@tonic-gate  * Return values for ti_doname().
236*7c478bd9Sstevel@tonic-gate  */
237*7c478bd9Sstevel@tonic-gate #define	DONAME_FAIL	0	/* failing ioctl (done) */
238*7c478bd9Sstevel@tonic-gate #define	DONAME_DONE	1	/* done processing */
239*7c478bd9Sstevel@tonic-gate #define	DONAME_CONT	2	/* continue proceesing (not done yet) */
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate /*
242*7c478bd9Sstevel@tonic-gate  * Function prototypes
243*7c478bd9Sstevel@tonic-gate  */
244*7c478bd9Sstevel@tonic-gate static int ti_doname(queue_t *, mblk_t *);
245*7c478bd9Sstevel@tonic-gate static int ti_expind_on_rdqueues(queue_t *);
246*7c478bd9Sstevel@tonic-gate static void tim_ioctl_send_reply(queue_t *, mblk_t *, mblk_t *);
247*7c478bd9Sstevel@tonic-gate static void tim_send_ioc_error_ack(queue_t *, struct tim_tim *, mblk_t *);
248*7c478bd9Sstevel@tonic-gate static void tim_tcap_timer(void *);
249*7c478bd9Sstevel@tonic-gate static void tim_tcap_genreply(queue_t *, struct tim_tim *);
250*7c478bd9Sstevel@tonic-gate static void tim_send_reply(queue_t *, mblk_t *, struct tim_tim *, t_scalar_t);
251*7c478bd9Sstevel@tonic-gate static void tim_answer_ti_sync(queue_t *, mblk_t *, struct tim_tim *,
252*7c478bd9Sstevel@tonic-gate     mblk_t *, uint32_t);
253*7c478bd9Sstevel@tonic-gate static void tim_send_ioctl_tpi_msg(queue_t *, mblk_t *, struct tim_tim *,
254*7c478bd9Sstevel@tonic-gate 	struct iocblk *);
255*7c478bd9Sstevel@tonic-gate static void tim_clear_peer(struct tim_tim *);
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate int
258*7c478bd9Sstevel@tonic-gate _init(void)
259*7c478bd9Sstevel@tonic-gate {
260*7c478bd9Sstevel@tonic-gate 	int	error;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	rw_init(&tim_list_rwlock, NULL, RW_DRIVER, NULL);
263*7c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
264*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
265*7c478bd9Sstevel@tonic-gate 		rw_destroy(&tim_list_rwlock);
266*7c478bd9Sstevel@tonic-gate 		return (error);
267*7c478bd9Sstevel@tonic-gate 	}
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	return (0);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate int
273*7c478bd9Sstevel@tonic-gate _fini(void)
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate 	int	error;
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
278*7c478bd9Sstevel@tonic-gate 	if (error != 0)
279*7c478bd9Sstevel@tonic-gate 		return (error);
280*7c478bd9Sstevel@tonic-gate 	rw_destroy(&tim_list_rwlock);
281*7c478bd9Sstevel@tonic-gate 	return (0);
282*7c478bd9Sstevel@tonic-gate }
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate int
285*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
286*7c478bd9Sstevel@tonic-gate {
287*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
288*7c478bd9Sstevel@tonic-gate }
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate /*
292*7c478bd9Sstevel@tonic-gate  * Hash list for all instances. Used to find tim_tim structure based on
293*7c478bd9Sstevel@tonic-gate  * ACCEPTOR_id in T_CONN_RES. Protected by tim_list_rwlock.
294*7c478bd9Sstevel@tonic-gate  */
295*7c478bd9Sstevel@tonic-gate #define	TIM_HASH_SIZE	256
296*7c478bd9Sstevel@tonic-gate #ifdef	_ILP32
297*7c478bd9Sstevel@tonic-gate #define	TIM_HASH(id) (((uintptr_t)(id) >> 8) % TIM_HASH_SIZE)
298*7c478bd9Sstevel@tonic-gate #else
299*7c478bd9Sstevel@tonic-gate #define	TIM_HASH(id) ((uintptr_t)(id) % TIM_HASH_SIZE)
300*7c478bd9Sstevel@tonic-gate #endif	/* _ILP32 */
301*7c478bd9Sstevel@tonic-gate static struct tim_tim	*tim_hash[TIM_HASH_SIZE];
302*7c478bd9Sstevel@tonic-gate int		tim_cnt = 0;
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate static void tilog(char *, t_scalar_t);
305*7c478bd9Sstevel@tonic-gate static void tilogp(char *, uintptr_t);
306*7c478bd9Sstevel@tonic-gate static mblk_t *tim_filladdr(queue_t *, mblk_t *, boolean_t);
307*7c478bd9Sstevel@tonic-gate static void tim_addlink(struct tim_tim	*);
308*7c478bd9Sstevel@tonic-gate static void tim_dellink(struct tim_tim	*);
309*7c478bd9Sstevel@tonic-gate static struct tim_tim *tim_findlink(t_uscalar_t);
310*7c478bd9Sstevel@tonic-gate static void tim_recover(queue_t *, mblk_t *, t_scalar_t);
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate int dotilog = 0;
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate #define	TIMOD_ID	3
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
317*7c478bd9Sstevel@tonic-gate static int timodopen(queue_t *, dev_t *, int, int, cred_t *);
318*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
319*7c478bd9Sstevel@tonic-gate static int timodclose(queue_t *, int, cred_t *);
320*7c478bd9Sstevel@tonic-gate static void timodwput(queue_t *, mblk_t *);
321*7c478bd9Sstevel@tonic-gate static void timodrput(queue_t *, mblk_t *);
322*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
323*7c478bd9Sstevel@tonic-gate static void timodrsrv(queue_t *);
324*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
325*7c478bd9Sstevel@tonic-gate static void timodwsrv(queue_t *);
326*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
327*7c478bd9Sstevel@tonic-gate static int timodrproc(queue_t *, mblk_t *);
328*7c478bd9Sstevel@tonic-gate static int timodwproc(queue_t *, mblk_t *);
329*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate /* stream data structure definitions */
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate static struct module_info timod_info =
334*7c478bd9Sstevel@tonic-gate 	{TIMOD_ID, "timod", 0, INFPSZ, 512, 128};
335*7c478bd9Sstevel@tonic-gate static struct qinit timodrinit = {
336*7c478bd9Sstevel@tonic-gate 	(int (*)())timodrput,
337*7c478bd9Sstevel@tonic-gate 	(int (*)())timodrsrv,
338*7c478bd9Sstevel@tonic-gate 	timodopen,
339*7c478bd9Sstevel@tonic-gate 	timodclose,
340*7c478bd9Sstevel@tonic-gate 	nulldev,
341*7c478bd9Sstevel@tonic-gate 	&timod_info,
342*7c478bd9Sstevel@tonic-gate 	NULL
343*7c478bd9Sstevel@tonic-gate };
344*7c478bd9Sstevel@tonic-gate static struct qinit timodwinit = {
345*7c478bd9Sstevel@tonic-gate 	(int (*)())timodwput,
346*7c478bd9Sstevel@tonic-gate 	(int (*)())timodwsrv,
347*7c478bd9Sstevel@tonic-gate 	timodopen,
348*7c478bd9Sstevel@tonic-gate 	timodclose,
349*7c478bd9Sstevel@tonic-gate 	nulldev,
350*7c478bd9Sstevel@tonic-gate 	&timod_info,
351*7c478bd9Sstevel@tonic-gate 	NULL
352*7c478bd9Sstevel@tonic-gate };
353*7c478bd9Sstevel@tonic-gate static struct streamtab timinfo = { &timodrinit, &timodwinit, NULL, NULL };
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
356*7c478bd9Sstevel@tonic-gate /*
357*7c478bd9Sstevel@tonic-gate  * timodopen -	open routine gets called when the module gets pushed
358*7c478bd9Sstevel@tonic-gate  *		onto the stream.
359*7c478bd9Sstevel@tonic-gate  */
360*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
361*7c478bd9Sstevel@tonic-gate static int
362*7c478bd9Sstevel@tonic-gate timodopen(
363*7c478bd9Sstevel@tonic-gate 	queue_t *q,
364*7c478bd9Sstevel@tonic-gate 	dev_t *devp,
365*7c478bd9Sstevel@tonic-gate 	int flag,
366*7c478bd9Sstevel@tonic-gate 	int sflag,
367*7c478bd9Sstevel@tonic-gate 	cred_t *crp)
368*7c478bd9Sstevel@tonic-gate {
369*7c478bd9Sstevel@tonic-gate 	struct tim_tim *tp;
370*7c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
371*7c478bd9Sstevel@tonic-gate 	mblk_t *bp;
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	if (q->q_ptr) {
376*7c478bd9Sstevel@tonic-gate 		return (0);
377*7c478bd9Sstevel@tonic-gate 	}
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	if ((bp = allocb(sizeof (struct stroptions), BPRI_MED)) == 0)
380*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 	tp = kmem_zalloc(sizeof (struct tim_tim), KM_SLEEP);
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 	tp->tim_cpid = -1;
385*7c478bd9Sstevel@tonic-gate 	tp->tim_saved_prim = -1;
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate 	mutex_init(&tp->tim_mutex, NULL, MUTEX_DEFAULT, NULL);
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	q->q_ptr = (caddr_t)tp;
390*7c478bd9Sstevel@tonic-gate 	WR(q)->q_ptr = (caddr_t)tp;
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	tilogp("timodopen: Allocated for tp %lx\n", (uintptr_t)tp);
393*7c478bd9Sstevel@tonic-gate 	tilogp("timodopen: Allocated for q %lx\n", (uintptr_t)q);
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	/* Must be done before tpi_findprov and _ILP32 q_next walk below */
396*7c478bd9Sstevel@tonic-gate 	qprocson(q);
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 	tp->tim_provinfo = tpi_findprov(q);
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 	/*
401*7c478bd9Sstevel@tonic-gate 	 * Defer allocation of the buffers for the local address and
402*7c478bd9Sstevel@tonic-gate 	 * the peer's address until we need them.
403*7c478bd9Sstevel@tonic-gate 	 * Assume that timod has to handle getname until we here
404*7c478bd9Sstevel@tonic-gate 	 * an iocack from the transport provider or we know that
405*7c478bd9Sstevel@tonic-gate 	 * transport provider doesn't understand it.
406*7c478bd9Sstevel@tonic-gate 	 */
407*7c478bd9Sstevel@tonic-gate 	if (tp->tim_provinfo->tpi_myname != PI_YES) {
408*7c478bd9Sstevel@tonic-gate 		TILOG("timodopen: setting DO_MYNAME\n", 0);
409*7c478bd9Sstevel@tonic-gate 		tp->tim_flags |= DO_MYNAME;
410*7c478bd9Sstevel@tonic-gate 	}
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	if (tp->tim_provinfo->tpi_peername != PI_YES) {
413*7c478bd9Sstevel@tonic-gate 		TILOG("timodopen: setting DO_PEERNAME\n", 0);
414*7c478bd9Sstevel@tonic-gate 		tp->tim_flags |= DO_PEERNAME;
415*7c478bd9Sstevel@tonic-gate 	}
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate #ifdef	_ILP32
418*7c478bd9Sstevel@tonic-gate 	{
419*7c478bd9Sstevel@tonic-gate 		queue_t *driverq;
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 		/*
422*7c478bd9Sstevel@tonic-gate 		 * Find my driver's read queue (for T_CONN_RES handling)
423*7c478bd9Sstevel@tonic-gate 		 */
424*7c478bd9Sstevel@tonic-gate 		driverq = WR(q);
425*7c478bd9Sstevel@tonic-gate 		while (SAMESTR(driverq))
426*7c478bd9Sstevel@tonic-gate 			driverq = driverq->q_next;
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 		tp->tim_acceptor = (t_uscalar_t)RD(driverq);
429*7c478bd9Sstevel@tonic-gate 	}
430*7c478bd9Sstevel@tonic-gate #else
431*7c478bd9Sstevel@tonic-gate 	tp->tim_acceptor = (t_uscalar_t)getminor(*devp);
432*7c478bd9Sstevel@tonic-gate #endif	/* _ILP32 */
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 	/*
435*7c478bd9Sstevel@tonic-gate 	 * Add this one to the list.
436*7c478bd9Sstevel@tonic-gate 	 */
437*7c478bd9Sstevel@tonic-gate 	tim_addlink(tp);
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	/*
440*7c478bd9Sstevel@tonic-gate 	 * Send M_SETOPTS to stream head to make sure M_PCPROTO messages
441*7c478bd9Sstevel@tonic-gate 	 * are not flushed. This prevents application deadlocks.
442*7c478bd9Sstevel@tonic-gate 	 */
443*7c478bd9Sstevel@tonic-gate 	bp->b_datap->db_type = M_SETOPTS;
444*7c478bd9Sstevel@tonic-gate 	bp->b_wptr += sizeof (struct stroptions);
445*7c478bd9Sstevel@tonic-gate 	sop = (struct stroptions *)bp->b_rptr;
446*7c478bd9Sstevel@tonic-gate 	sop->so_flags = SO_READOPT;
447*7c478bd9Sstevel@tonic-gate 	sop->so_readopt = RFLUSHPCPROT;
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 	putnext(q, bp);
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 	return (0);
452*7c478bd9Sstevel@tonic-gate }
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate static void
455*7c478bd9Sstevel@tonic-gate tim_timer(void *arg)
456*7c478bd9Sstevel@tonic-gate {
457*7c478bd9Sstevel@tonic-gate 	queue_t *q = arg;
458*7c478bd9Sstevel@tonic-gate 	struct tim_tim *tp = (struct tim_tim *)q->q_ptr;
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	ASSERT(tp);
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	if (q->q_flag & QREADR) {
463*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->tim_rtimoutid);
464*7c478bd9Sstevel@tonic-gate 		tp->tim_rtimoutid = 0;
465*7c478bd9Sstevel@tonic-gate 	} else {
466*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->tim_wtimoutid);
467*7c478bd9Sstevel@tonic-gate 		tp->tim_wtimoutid = 0;
468*7c478bd9Sstevel@tonic-gate 	}
469*7c478bd9Sstevel@tonic-gate 	enableok(q);
470*7c478bd9Sstevel@tonic-gate 	qenable(q);
471*7c478bd9Sstevel@tonic-gate }
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate static void
474*7c478bd9Sstevel@tonic-gate tim_buffer(void *arg)
475*7c478bd9Sstevel@tonic-gate {
476*7c478bd9Sstevel@tonic-gate 	queue_t *q = arg;
477*7c478bd9Sstevel@tonic-gate 	struct tim_tim *tp = (struct tim_tim *)q->q_ptr;
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 	ASSERT(tp);
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 	if (q->q_flag & QREADR) {
482*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->tim_rbufcid);
483*7c478bd9Sstevel@tonic-gate 		tp->tim_rbufcid = 0;
484*7c478bd9Sstevel@tonic-gate 	} else {
485*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->tim_wbufcid);
486*7c478bd9Sstevel@tonic-gate 		tp->tim_wbufcid = 0;
487*7c478bd9Sstevel@tonic-gate 	}
488*7c478bd9Sstevel@tonic-gate 	enableok(q);
489*7c478bd9Sstevel@tonic-gate 	qenable(q);
490*7c478bd9Sstevel@tonic-gate }
491*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate /*
494*7c478bd9Sstevel@tonic-gate  * timodclose - This routine gets called when the module gets popped
495*7c478bd9Sstevel@tonic-gate  * off of the stream.
496*7c478bd9Sstevel@tonic-gate  */
497*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
498*7c478bd9Sstevel@tonic-gate static int
499*7c478bd9Sstevel@tonic-gate timodclose(
500*7c478bd9Sstevel@tonic-gate 	queue_t *q,
501*7c478bd9Sstevel@tonic-gate 	int flag,
502*7c478bd9Sstevel@tonic-gate 	cred_t *crp)
503*7c478bd9Sstevel@tonic-gate {
504*7c478bd9Sstevel@tonic-gate 	struct tim_tim *tp;
505*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
506*7c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate 	tp = (struct tim_tim *)q->q_ptr;
511*7c478bd9Sstevel@tonic-gate 	q->q_ptr = NULL;
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 	ASSERT(tp != NULL);
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate 	tilogp("timodclose: Entered for tp %lx\n", (uintptr_t)tp);
516*7c478bd9Sstevel@tonic-gate 	tilogp("timodclose: Entered for q %lx\n", (uintptr_t)q);
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate 	qprocsoff(q);
519*7c478bd9Sstevel@tonic-gate 	tim_dellink(tp);
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate 	/*
522*7c478bd9Sstevel@tonic-gate 	 * Cancel any outstanding bufcall
523*7c478bd9Sstevel@tonic-gate 	 * or timeout requests.
524*7c478bd9Sstevel@tonic-gate 	 */
525*7c478bd9Sstevel@tonic-gate 	if (tp->tim_wbufcid) {
526*7c478bd9Sstevel@tonic-gate 		qunbufcall(q, tp->tim_wbufcid);
527*7c478bd9Sstevel@tonic-gate 		tp->tim_wbufcid = 0;
528*7c478bd9Sstevel@tonic-gate 	}
529*7c478bd9Sstevel@tonic-gate 	if (tp->tim_rbufcid) {
530*7c478bd9Sstevel@tonic-gate 		qunbufcall(q, tp->tim_rbufcid);
531*7c478bd9Sstevel@tonic-gate 		tp->tim_rbufcid = 0;
532*7c478bd9Sstevel@tonic-gate 	}
533*7c478bd9Sstevel@tonic-gate 	if (tp->tim_wtimoutid) {
534*7c478bd9Sstevel@tonic-gate 		(void) quntimeout(q, tp->tim_wtimoutid);
535*7c478bd9Sstevel@tonic-gate 		tp->tim_wtimoutid = 0;
536*7c478bd9Sstevel@tonic-gate 	}
537*7c478bd9Sstevel@tonic-gate 	if (tp->tim_rtimoutid) {
538*7c478bd9Sstevel@tonic-gate 		(void) quntimeout(q, tp->tim_rtimoutid);
539*7c478bd9Sstevel@tonic-gate 		tp->tim_rtimoutid = 0;
540*7c478bd9Sstevel@tonic-gate 	}
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 	if (tp->tim_tcap_timoutid != 0) {
543*7c478bd9Sstevel@tonic-gate 		(void) quntimeout(q, tp->tim_tcap_timoutid);
544*7c478bd9Sstevel@tonic-gate 		tp->tim_tcap_timoutid = 0;
545*7c478bd9Sstevel@tonic-gate 	}
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 	if (tp->tim_iocsave != NULL)
548*7c478bd9Sstevel@tonic-gate 		freemsg(tp->tim_iocsave);
549*7c478bd9Sstevel@tonic-gate 	mp = tp->tim_consave;
550*7c478bd9Sstevel@tonic-gate 	while (mp) {
551*7c478bd9Sstevel@tonic-gate 		nmp = mp->b_next;
552*7c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
553*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
554*7c478bd9Sstevel@tonic-gate 		mp = nmp;
555*7c478bd9Sstevel@tonic-gate 	}
556*7c478bd9Sstevel@tonic-gate 	ASSERT(tp->tim_mymaxlen >= 0);
557*7c478bd9Sstevel@tonic-gate 	if (tp->tim_mymaxlen != 0)
558*7c478bd9Sstevel@tonic-gate 		kmem_free(tp->tim_myname, (size_t)tp->tim_mymaxlen);
559*7c478bd9Sstevel@tonic-gate 	ASSERT(tp->tim_peermaxlen >= 0);
560*7c478bd9Sstevel@tonic-gate 	if (tp->tim_peermaxlen != 0)
561*7c478bd9Sstevel@tonic-gate 		kmem_free(tp->tim_peername, (size_t)tp->tim_peermaxlen);
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&tp->tim_mutex);
566*7c478bd9Sstevel@tonic-gate 
567*7c478bd9Sstevel@tonic-gate 	if (tp->tim_peercred != NULL)
568*7c478bd9Sstevel@tonic-gate 		crfree(tp->tim_peercred);
569*7c478bd9Sstevel@tonic-gate 
570*7c478bd9Sstevel@tonic-gate 	kmem_free(tp, sizeof (struct tim_tim));
571*7c478bd9Sstevel@tonic-gate 
572*7c478bd9Sstevel@tonic-gate 	return (0);
573*7c478bd9Sstevel@tonic-gate }
574*7c478bd9Sstevel@tonic-gate 
575*7c478bd9Sstevel@tonic-gate /*
576*7c478bd9Sstevel@tonic-gate  * timodrput -	Module read put procedure.  This is called from
577*7c478bd9Sstevel@tonic-gate  *		the module, driver, or stream head upstream/downstream.
578*7c478bd9Sstevel@tonic-gate  *		Handles M_FLUSH, M_DATA and some M_PROTO (T_DATA_IND,
579*7c478bd9Sstevel@tonic-gate  *		and T_UNITDATA_IND) messages. All others are queued to
580*7c478bd9Sstevel@tonic-gate  *		be handled by the service procedures.
581*7c478bd9Sstevel@tonic-gate  */
582*7c478bd9Sstevel@tonic-gate static void
583*7c478bd9Sstevel@tonic-gate timodrput(queue_t *q, mblk_t *mp)
584*7c478bd9Sstevel@tonic-gate {
585*7c478bd9Sstevel@tonic-gate 	union T_primitives *pptr;
586*7c478bd9Sstevel@tonic-gate 
587*7c478bd9Sstevel@tonic-gate 	/*
588*7c478bd9Sstevel@tonic-gate 	 * During flow control and other instances when messages
589*7c478bd9Sstevel@tonic-gate 	 * are on queue, queue up a non high priority message
590*7c478bd9Sstevel@tonic-gate 	 */
591*7c478bd9Sstevel@tonic-gate 	if (q->q_first != 0 && mp->b_datap->db_type < QPCTL) {
592*7c478bd9Sstevel@tonic-gate 		(void) putq(q, mp);
593*7c478bd9Sstevel@tonic-gate 		return;
594*7c478bd9Sstevel@tonic-gate 	}
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	/*
597*7c478bd9Sstevel@tonic-gate 	 * Inline processing of data (to avoid additional procedure call).
598*7c478bd9Sstevel@tonic-gate 	 * Rest is handled in timodrproc.
599*7c478bd9Sstevel@tonic-gate 	 */
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
602*7c478bd9Sstevel@tonic-gate 	case M_DATA:
603*7c478bd9Sstevel@tonic-gate 		if (bcanputnext(q, mp->b_band))
604*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
605*7c478bd9Sstevel@tonic-gate 		else
606*7c478bd9Sstevel@tonic-gate 			(void) putq(q, mp);
607*7c478bd9Sstevel@tonic-gate 		break;
608*7c478bd9Sstevel@tonic-gate 	case M_PROTO:
609*7c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
610*7c478bd9Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (t_scalar_t)) {
611*7c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_PCPROTO ||
612*7c478bd9Sstevel@tonic-gate 			    bcanputnext(q, mp->b_band)) {
613*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
614*7c478bd9Sstevel@tonic-gate 			} else {
615*7c478bd9Sstevel@tonic-gate 				(void) putq(q, mp);
616*7c478bd9Sstevel@tonic-gate 			}
617*7c478bd9Sstevel@tonic-gate 			break;
618*7c478bd9Sstevel@tonic-gate 		}
619*7c478bd9Sstevel@tonic-gate 		pptr = (union T_primitives *)mp->b_rptr;
620*7c478bd9Sstevel@tonic-gate 		switch (pptr->type) {
621*7c478bd9Sstevel@tonic-gate 		case T_EXDATA_IND:
622*7c478bd9Sstevel@tonic-gate 		case T_DATA_IND:
623*7c478bd9Sstevel@tonic-gate 		case T_UNITDATA_IND:
624*7c478bd9Sstevel@tonic-gate 			if (bcanputnext(q, mp->b_band))
625*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
626*7c478bd9Sstevel@tonic-gate 			else
627*7c478bd9Sstevel@tonic-gate 				(void) putq(q, mp);
628*7c478bd9Sstevel@tonic-gate 			break;
629*7c478bd9Sstevel@tonic-gate 		default:
630*7c478bd9Sstevel@tonic-gate 			(void) timodrproc(q, mp);
631*7c478bd9Sstevel@tonic-gate 			break;
632*7c478bd9Sstevel@tonic-gate 		}
633*7c478bd9Sstevel@tonic-gate 		break;
634*7c478bd9Sstevel@tonic-gate 	default:
635*7c478bd9Sstevel@tonic-gate 		(void) timodrproc(q, mp);
636*7c478bd9Sstevel@tonic-gate 		break;
637*7c478bd9Sstevel@tonic-gate 	}
638*7c478bd9Sstevel@tonic-gate }
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
641*7c478bd9Sstevel@tonic-gate /*
642*7c478bd9Sstevel@tonic-gate  * timodrsrv -	Module read queue service procedure.  This is called when
643*7c478bd9Sstevel@tonic-gate  *		messages are placed on an empty queue, when high priority
644*7c478bd9Sstevel@tonic-gate  *		messages are placed on the queue, and when flow control
645*7c478bd9Sstevel@tonic-gate  *		restrictions subside.  This code used to be included in a
646*7c478bd9Sstevel@tonic-gate  *		put procedure, but it was moved to a service procedure
647*7c478bd9Sstevel@tonic-gate  *		because several points were added where memory allocation
648*7c478bd9Sstevel@tonic-gate  *		could fail, and there is no reasonable recovery mechanism
649*7c478bd9Sstevel@tonic-gate  *		from the put procedure.
650*7c478bd9Sstevel@tonic-gate  */
651*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
652*7c478bd9Sstevel@tonic-gate static void
653*7c478bd9Sstevel@tonic-gate timodrsrv(queue_t *q)
654*7c478bd9Sstevel@tonic-gate {
655*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
656*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
657*7c478bd9Sstevel@tonic-gate 	struct tim_tim *tp;
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
660*7c478bd9Sstevel@tonic-gate 
661*7c478bd9Sstevel@tonic-gate 	tp = (struct tim_tim *)q->q_ptr;
662*7c478bd9Sstevel@tonic-gate 	if (!tp)
663*7c478bd9Sstevel@tonic-gate 	    return;
664*7c478bd9Sstevel@tonic-gate 
665*7c478bd9Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
666*7c478bd9Sstevel@tonic-gate 		if (timodrproc(q, mp)) {
667*7c478bd9Sstevel@tonic-gate 			/*
668*7c478bd9Sstevel@tonic-gate 			 * timodrproc did a putbq - stop processing
669*7c478bd9Sstevel@tonic-gate 			 * messages.
670*7c478bd9Sstevel@tonic-gate 			 */
671*7c478bd9Sstevel@tonic-gate 			return;
672*7c478bd9Sstevel@tonic-gate 		}
673*7c478bd9Sstevel@tonic-gate 	}
674*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
675*7c478bd9Sstevel@tonic-gate }
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate /*
678*7c478bd9Sstevel@tonic-gate  * Perform common processing when a T_CAPABILITY_ACK or T_INFO_ACK
679*7c478bd9Sstevel@tonic-gate  * arrive.  Set the queue properties and adjust the tim_flags according
680*7c478bd9Sstevel@tonic-gate  * to the service type.
681*7c478bd9Sstevel@tonic-gate  */
682*7c478bd9Sstevel@tonic-gate static void
683*7c478bd9Sstevel@tonic-gate timodprocessinfo(queue_t *q, struct tim_tim *tp, struct T_info_ack *tia)
684*7c478bd9Sstevel@tonic-gate {
685*7c478bd9Sstevel@tonic-gate 	TILOG("timodprocessinfo: strqset(%d)\n", tia->TIDU_size);
686*7c478bd9Sstevel@tonic-gate 	(void) strqset(q, QMAXPSZ, 0, tia->TIDU_size);
687*7c478bd9Sstevel@tonic-gate 	(void) strqset(OTHERQ(q), QMAXPSZ, 0, tia->TIDU_size);
688*7c478bd9Sstevel@tonic-gate 
689*7c478bd9Sstevel@tonic-gate 	if ((tia->SERV_type == T_COTS) || (tia->SERV_type == T_COTS_ORD))
690*7c478bd9Sstevel@tonic-gate 		tp->tim_flags = (tp->tim_flags & ~CLTS) | COTS;
691*7c478bd9Sstevel@tonic-gate 	else if (tia->SERV_type == T_CLTS)
692*7c478bd9Sstevel@tonic-gate 		tp->tim_flags = (tp->tim_flags & ~COTS) | CLTS;
693*7c478bd9Sstevel@tonic-gate }
694*7c478bd9Sstevel@tonic-gate 
695*7c478bd9Sstevel@tonic-gate static int
696*7c478bd9Sstevel@tonic-gate timodrproc(queue_t *q, mblk_t *mp)
697*7c478bd9Sstevel@tonic-gate {
698*7c478bd9Sstevel@tonic-gate 	union T_primitives *pptr;
699*7c478bd9Sstevel@tonic-gate 	struct tim_tim *tp;
700*7c478bd9Sstevel@tonic-gate 	struct iocblk *iocbp;
701*7c478bd9Sstevel@tonic-gate 	mblk_t *nbp;
702*7c478bd9Sstevel@tonic-gate 	size_t blen;
703*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate 	tp = (struct tim_tim *)q->q_ptr;
706*7c478bd9Sstevel@tonic-gate 
707*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
708*7c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
709*7c478bd9Sstevel@tonic-gate 	default:
710*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
711*7c478bd9Sstevel@tonic-gate 		break;
712*7c478bd9Sstevel@tonic-gate 
713*7c478bd9Sstevel@tonic-gate 	case M_ERROR:
714*7c478bd9Sstevel@tonic-gate 		TILOG("timodrproc: Got M_ERROR, flags = %x\n", tp->tim_flags);
715*7c478bd9Sstevel@tonic-gate 		/*
716*7c478bd9Sstevel@tonic-gate 		 * There is no specified standard response for driver when it
717*7c478bd9Sstevel@tonic-gate 		 * receives unknown message type and M_ERROR is one
718*7c478bd9Sstevel@tonic-gate 		 * possibility. If we send T_CAPABILITY_REQ down and transport
719*7c478bd9Sstevel@tonic-gate 		 * provider responds with M_ERROR we assume that it doesn't
720*7c478bd9Sstevel@tonic-gate 		 * understand this message type. This assumption may be
721*7c478bd9Sstevel@tonic-gate 		 * sometimes incorrect (transport may reply with M_ERROR for
722*7c478bd9Sstevel@tonic-gate 		 * some other reason) but there is no way for us to distinguish
723*7c478bd9Sstevel@tonic-gate 		 * between different cases. In the worst case timod and everyone
724*7c478bd9Sstevel@tonic-gate 		 * else sharing global transport description with it may end up
725*7c478bd9Sstevel@tonic-gate 		 * emulating T_CAPABILITY_REQ.
726*7c478bd9Sstevel@tonic-gate 		 */
727*7c478bd9Sstevel@tonic-gate 
728*7c478bd9Sstevel@tonic-gate 		/*
729*7c478bd9Sstevel@tonic-gate 		 * Check that we are waiting for T_CAPABILITY_ACK and
730*7c478bd9Sstevel@tonic-gate 		 * T_CAPABILITY_REQ is not implemented by transport or emulated
731*7c478bd9Sstevel@tonic-gate 		 * by timod.
732*7c478bd9Sstevel@tonic-gate 		 */
733*7c478bd9Sstevel@tonic-gate 		if ((tp->tim_provinfo->tpi_capability == PI_DONTKNOW) &&
734*7c478bd9Sstevel@tonic-gate 		    ((tp->tim_flags & TI_CAP_RECVD) != 0)) {
735*7c478bd9Sstevel@tonic-gate 			/*
736*7c478bd9Sstevel@tonic-gate 			 * Good chances that this transport doesn't provide
737*7c478bd9Sstevel@tonic-gate 			 * T_CAPABILITY_REQ. Mark this information  permanently
738*7c478bd9Sstevel@tonic-gate 			 * for the module + transport combination.
739*7c478bd9Sstevel@tonic-gate 			 */
740*7c478bd9Sstevel@tonic-gate 			PI_PROVLOCK(tp->tim_provinfo);
741*7c478bd9Sstevel@tonic-gate 			if (tp->tim_provinfo->tpi_capability == PI_DONTKNOW)
742*7c478bd9Sstevel@tonic-gate 				tp->tim_provinfo->tpi_capability = PI_NO;
743*7c478bd9Sstevel@tonic-gate 			PI_PROVUNLOCK(tp->tim_provinfo);
744*7c478bd9Sstevel@tonic-gate 			if (tp->tim_tcap_timoutid != 0) {
745*7c478bd9Sstevel@tonic-gate 				(void) quntimeout(q, tp->tim_tcap_timoutid);
746*7c478bd9Sstevel@tonic-gate 				tp->tim_tcap_timoutid = 0;
747*7c478bd9Sstevel@tonic-gate 			}
748*7c478bd9Sstevel@tonic-gate 		}
749*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
750*7c478bd9Sstevel@tonic-gate 		break;
751*7c478bd9Sstevel@tonic-gate 	case M_DATA:
752*7c478bd9Sstevel@tonic-gate 		if (!bcanputnext(q, mp->b_band)) {
753*7c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
754*7c478bd9Sstevel@tonic-gate 			return (1);
755*7c478bd9Sstevel@tonic-gate 		}
756*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
757*7c478bd9Sstevel@tonic-gate 		break;
758*7c478bd9Sstevel@tonic-gate 
759*7c478bd9Sstevel@tonic-gate 	case M_PROTO:
760*7c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
761*7c478bd9Sstevel@tonic-gate 		blen = MBLKL(mp);
762*7c478bd9Sstevel@tonic-gate 		if (blen < sizeof (t_scalar_t)) {
763*7c478bd9Sstevel@tonic-gate 			/*
764*7c478bd9Sstevel@tonic-gate 			 * Note: it's not actually possible to get
765*7c478bd9Sstevel@tonic-gate 			 * here with db_type M_PCPROTO, because
766*7c478bd9Sstevel@tonic-gate 			 * timodrput has already checked MBLKL, and
767*7c478bd9Sstevel@tonic-gate 			 * thus the assertion below.  If the length
768*7c478bd9Sstevel@tonic-gate 			 * was too short, then the message would have
769*7c478bd9Sstevel@tonic-gate 			 * already been putnext'd, and would thus
770*7c478bd9Sstevel@tonic-gate 			 * never appear here.  Just the same, the code
771*7c478bd9Sstevel@tonic-gate 			 * below handles the impossible case since
772*7c478bd9Sstevel@tonic-gate 			 * it's easy to do and saves future
773*7c478bd9Sstevel@tonic-gate 			 * maintainers from unfortunate accidents.
774*7c478bd9Sstevel@tonic-gate 			 */
775*7c478bd9Sstevel@tonic-gate 			ASSERT(mp->b_datap->db_type == M_PROTO);
776*7c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_PROTO &&
777*7c478bd9Sstevel@tonic-gate 			    !bcanputnext(q, mp->b_band)) {
778*7c478bd9Sstevel@tonic-gate 				(void) putbq(q, mp);
779*7c478bd9Sstevel@tonic-gate 				return (1);
780*7c478bd9Sstevel@tonic-gate 			}
781*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
782*7c478bd9Sstevel@tonic-gate 			break;
783*7c478bd9Sstevel@tonic-gate 		}
784*7c478bd9Sstevel@tonic-gate 
785*7c478bd9Sstevel@tonic-gate 		pptr = (union T_primitives *)mp->b_rptr;
786*7c478bd9Sstevel@tonic-gate 		switch (pptr->type) {
787*7c478bd9Sstevel@tonic-gate 		default:
788*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
791*7c478bd9Sstevel@tonic-gate 			if (audit_active)
792*7c478bd9Sstevel@tonic-gate 				audit_sock(T_UNITDATA_IND, q, mp, TIMOD_ID);
793*7c478bd9Sstevel@tonic-gate #endif
794*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
795*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
796*7c478bd9Sstevel@tonic-gate 			break;
797*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate 		case T_ERROR_ACK:
800*7c478bd9Sstevel@tonic-gate 			/* Restore db_type - recover() might have changed it */
801*7c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_PCPROTO;
802*7c478bd9Sstevel@tonic-gate 			if (blen < sizeof (struct T_error_ack)) {
803*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
804*7c478bd9Sstevel@tonic-gate 				break;
805*7c478bd9Sstevel@tonic-gate 			}
806*7c478bd9Sstevel@tonic-gate 
807*7c478bd9Sstevel@tonic-gate 			tilog("timodrproc: Got T_ERROR_ACK, flags = %x\n",
808*7c478bd9Sstevel@tonic-gate 			    tp->tim_flags);
809*7c478bd9Sstevel@tonic-gate 
810*7c478bd9Sstevel@tonic-gate 			if ((tp->tim_flags & WAIT_CONNRESACK) &&
811*7c478bd9Sstevel@tonic-gate 			    tp->tim_saved_prim == pptr->error_ack.ERROR_prim) {
812*7c478bd9Sstevel@tonic-gate 				tp->tim_flags &=
813*7c478bd9Sstevel@tonic-gate 				    ~(WAIT_CONNRESACK | WAITIOCACK);
814*7c478bd9Sstevel@tonic-gate 				freemsg(tp->tim_iocsave);
815*7c478bd9Sstevel@tonic-gate 				tp->tim_iocsave = NULL;
816*7c478bd9Sstevel@tonic-gate 				tp->tim_saved_prim = -1;
817*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
818*7c478bd9Sstevel@tonic-gate 			} else if (tp->tim_flags & WAITIOCACK) {
819*7c478bd9Sstevel@tonic-gate 				tim_send_ioc_error_ack(q, tp, mp);
820*7c478bd9Sstevel@tonic-gate 			} else {
821*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
822*7c478bd9Sstevel@tonic-gate 			}
823*7c478bd9Sstevel@tonic-gate 			break;
824*7c478bd9Sstevel@tonic-gate 
825*7c478bd9Sstevel@tonic-gate 		case T_OK_ACK:
826*7c478bd9Sstevel@tonic-gate 			if (blen < sizeof (pptr->ok_ack)) {
827*7c478bd9Sstevel@tonic-gate 				mp->b_datap->db_type = M_PCPROTO;
828*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
829*7c478bd9Sstevel@tonic-gate 				break;
830*7c478bd9Sstevel@tonic-gate 			}
831*7c478bd9Sstevel@tonic-gate 
832*7c478bd9Sstevel@tonic-gate 			tilog("timodrproc: Got T_OK_ACK\n", 0);
833*7c478bd9Sstevel@tonic-gate 
834*7c478bd9Sstevel@tonic-gate 			if (pptr->ok_ack.CORRECT_prim == T_UNBIND_REQ)
835*7c478bd9Sstevel@tonic-gate 				tp->tim_mylen = 0;
836*7c478bd9Sstevel@tonic-gate 
837*7c478bd9Sstevel@tonic-gate 			if ((tp->tim_flags & WAIT_CONNRESACK) &&
838*7c478bd9Sstevel@tonic-gate 			    tp->tim_saved_prim == pptr->ok_ack.CORRECT_prim) {
839*7c478bd9Sstevel@tonic-gate 				struct T_conn_res *resp;
840*7c478bd9Sstevel@tonic-gate 				struct T_conn_ind *indp;
841*7c478bd9Sstevel@tonic-gate 				struct tim_tim *ntp;
842*7c478bd9Sstevel@tonic-gate 				caddr_t ptr;
843*7c478bd9Sstevel@tonic-gate 
844*7c478bd9Sstevel@tonic-gate 				rw_enter(&tim_list_rwlock, RW_READER);
845*7c478bd9Sstevel@tonic-gate 				resp = (struct T_conn_res *)
846*7c478bd9Sstevel@tonic-gate 				    tp->tim_iocsave->b_rptr;
847*7c478bd9Sstevel@tonic-gate 				ntp = tim_findlink(resp->ACCEPTOR_id);
848*7c478bd9Sstevel@tonic-gate 				if (ntp == NULL)
849*7c478bd9Sstevel@tonic-gate 					goto cresackout;
850*7c478bd9Sstevel@tonic-gate 
851*7c478bd9Sstevel@tonic-gate 				mutex_enter(&ntp->tim_mutex);
852*7c478bd9Sstevel@tonic-gate 				if (ntp->tim_peercred != NULL)
853*7c478bd9Sstevel@tonic-gate 					crfree(ntp->tim_peercred);
854*7c478bd9Sstevel@tonic-gate 				ntp->tim_peercred =
855*7c478bd9Sstevel@tonic-gate 				    DB_CRED(tp->tim_iocsave->b_cont);
856*7c478bd9Sstevel@tonic-gate 				ntp->tim_cpid =
857*7c478bd9Sstevel@tonic-gate 				    DB_CPID(tp->tim_iocsave->b_cont);
858*7c478bd9Sstevel@tonic-gate 				if (ntp->tim_peercred != NULL)
859*7c478bd9Sstevel@tonic-gate 					crhold(ntp->tim_peercred);
860*7c478bd9Sstevel@tonic-gate 
861*7c478bd9Sstevel@tonic-gate 				if (!(ntp->tim_flags & DO_PEERNAME)) {
862*7c478bd9Sstevel@tonic-gate 					mutex_exit(&ntp->tim_mutex);
863*7c478bd9Sstevel@tonic-gate 					goto cresackout;
864*7c478bd9Sstevel@tonic-gate 				}
865*7c478bd9Sstevel@tonic-gate 
866*7c478bd9Sstevel@tonic-gate 				indp = (struct T_conn_ind *)
867*7c478bd9Sstevel@tonic-gate 				    tp->tim_iocsave->b_cont->b_rptr;
868*7c478bd9Sstevel@tonic-gate 				/* true as message is put on list */
869*7c478bd9Sstevel@tonic-gate 				ASSERT(indp->SRC_length >= 0);
870*7c478bd9Sstevel@tonic-gate 
871*7c478bd9Sstevel@tonic-gate 				if (indp->SRC_length > ntp->tim_peermaxlen) {
872*7c478bd9Sstevel@tonic-gate 					ptr = kmem_alloc(indp->SRC_length,
873*7c478bd9Sstevel@tonic-gate 					    KM_NOSLEEP);
874*7c478bd9Sstevel@tonic-gate 					if (ptr == NULL) {
875*7c478bd9Sstevel@tonic-gate 						mutex_exit(&ntp->tim_mutex);
876*7c478bd9Sstevel@tonic-gate 						rw_exit(&tim_list_rwlock);
877*7c478bd9Sstevel@tonic-gate 						tilog("timodwproc: kmem_alloc "
878*7c478bd9Sstevel@tonic-gate 						    "failed, attempting "
879*7c478bd9Sstevel@tonic-gate 						    "recovery\n", 0);
880*7c478bd9Sstevel@tonic-gate 						tim_recover(q, mp,
881*7c478bd9Sstevel@tonic-gate 						    indp->SRC_length);
882*7c478bd9Sstevel@tonic-gate 						return (1);
883*7c478bd9Sstevel@tonic-gate 					}
884*7c478bd9Sstevel@tonic-gate 					if (ntp->tim_peermaxlen > 0)
885*7c478bd9Sstevel@tonic-gate 						kmem_free(ntp->tim_peername,
886*7c478bd9Sstevel@tonic-gate 						    ntp->tim_peermaxlen);
887*7c478bd9Sstevel@tonic-gate 					ntp->tim_peername = ptr;
888*7c478bd9Sstevel@tonic-gate 					ntp->tim_peermaxlen = indp->SRC_length;
889*7c478bd9Sstevel@tonic-gate 				}
890*7c478bd9Sstevel@tonic-gate 				ntp->tim_peerlen = indp->SRC_length;
891*7c478bd9Sstevel@tonic-gate 				ptr = (caddr_t)indp + indp->SRC_offset;
892*7c478bd9Sstevel@tonic-gate 				bcopy(ptr, ntp->tim_peername, ntp->tim_peerlen);
893*7c478bd9Sstevel@tonic-gate 
894*7c478bd9Sstevel@tonic-gate 				mutex_exit(&ntp->tim_mutex);
895*7c478bd9Sstevel@tonic-gate 
896*7c478bd9Sstevel@tonic-gate 			cresackout:
897*7c478bd9Sstevel@tonic-gate 				rw_exit(&tim_list_rwlock);
898*7c478bd9Sstevel@tonic-gate 				tp->tim_flags &=
899*7c478bd9Sstevel@tonic-gate 				    ~(WAIT_CONNRESACK | WAITIOCACK);
900*7c478bd9Sstevel@tonic-gate 				freemsg(tp->tim_iocsave);
901*7c478bd9Sstevel@tonic-gate 				tp->tim_iocsave = NULL;
902*7c478bd9Sstevel@tonic-gate 				tp->tim_saved_prim = -1;
903*7c478bd9Sstevel@tonic-gate 			}
904*7c478bd9Sstevel@tonic-gate 
905*7c478bd9Sstevel@tonic-gate 			tim_send_reply(q, mp, tp, pptr->ok_ack.CORRECT_prim);
906*7c478bd9Sstevel@tonic-gate 			break;
907*7c478bd9Sstevel@tonic-gate 
908*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
909*7c478bd9Sstevel@tonic-gate 		case T_BIND_ACK: {
910*7c478bd9Sstevel@tonic-gate 			struct T_bind_ack *ackp =
911*7c478bd9Sstevel@tonic-gate 			    (struct T_bind_ack *)mp->b_rptr;
912*7c478bd9Sstevel@tonic-gate 
913*7c478bd9Sstevel@tonic-gate 			/* Restore db_type - recover() might have changed it */
914*7c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_PCPROTO;
915*7c478bd9Sstevel@tonic-gate 			if (blen < sizeof (*ackp)) {
916*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
917*7c478bd9Sstevel@tonic-gate 				break;
918*7c478bd9Sstevel@tonic-gate 			}
919*7c478bd9Sstevel@tonic-gate 
920*7c478bd9Sstevel@tonic-gate 			/* save negotiated backlog */
921*7c478bd9Sstevel@tonic-gate 			tp->tim_backlog = ackp->CONIND_number;
922*7c478bd9Sstevel@tonic-gate 
923*7c478bd9Sstevel@tonic-gate 			if (((tp->tim_flags & WAITIOCACK) == 0) ||
924*7c478bd9Sstevel@tonic-gate 			    ((tp->tim_saved_prim != O_T_BIND_REQ) &&
925*7c478bd9Sstevel@tonic-gate 				(tp->tim_saved_prim != T_BIND_REQ))) {
926*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
927*7c478bd9Sstevel@tonic-gate 				break;
928*7c478bd9Sstevel@tonic-gate 			}
929*7c478bd9Sstevel@tonic-gate 			ASSERT(tp->tim_iocsave != NULL);
930*7c478bd9Sstevel@tonic-gate 
931*7c478bd9Sstevel@tonic-gate 			if (tp->tim_flags & DO_MYNAME) {
932*7c478bd9Sstevel@tonic-gate 				caddr_t p;
933*7c478bd9Sstevel@tonic-gate 
934*7c478bd9Sstevel@tonic-gate 				if (ackp->ADDR_length < 0 ||
935*7c478bd9Sstevel@tonic-gate 				    mp->b_rptr + ackp->ADDR_offset +
936*7c478bd9Sstevel@tonic-gate 				    ackp->ADDR_length > mp->b_wptr) {
937*7c478bd9Sstevel@tonic-gate 					putnext(q, mp);
938*7c478bd9Sstevel@tonic-gate 					break;
939*7c478bd9Sstevel@tonic-gate 				}
940*7c478bd9Sstevel@tonic-gate 				if (ackp->ADDR_length > tp->tim_mymaxlen) {
941*7c478bd9Sstevel@tonic-gate 					p = kmem_alloc(ackp->ADDR_length,
942*7c478bd9Sstevel@tonic-gate 					    KM_NOSLEEP);
943*7c478bd9Sstevel@tonic-gate 					if (p == NULL) {
944*7c478bd9Sstevel@tonic-gate 						tilog("timodrproc: kmem_alloc "
945*7c478bd9Sstevel@tonic-gate 						    "failed attempt recovery",
946*7c478bd9Sstevel@tonic-gate 						    0);
947*7c478bd9Sstevel@tonic-gate 
948*7c478bd9Sstevel@tonic-gate 						tim_recover(q, mp,
949*7c478bd9Sstevel@tonic-gate 						    ackp->ADDR_length);
950*7c478bd9Sstevel@tonic-gate 						return (1);
951*7c478bd9Sstevel@tonic-gate 					}
952*7c478bd9Sstevel@tonic-gate 					ASSERT(tp->tim_mymaxlen >= 0);
953*7c478bd9Sstevel@tonic-gate 					if (tp->tim_mymaxlen != NULL) {
954*7c478bd9Sstevel@tonic-gate 						kmem_free(tp->tim_myname,
955*7c478bd9Sstevel@tonic-gate 						    tp->tim_mymaxlen);
956*7c478bd9Sstevel@tonic-gate 					}
957*7c478bd9Sstevel@tonic-gate 					tp->tim_myname = p;
958*7c478bd9Sstevel@tonic-gate 					tp->tim_mymaxlen = ackp->ADDR_length;
959*7c478bd9Sstevel@tonic-gate 				}
960*7c478bd9Sstevel@tonic-gate 				tp->tim_mylen = ackp->ADDR_length;
961*7c478bd9Sstevel@tonic-gate 				bcopy(mp->b_rptr + ackp->ADDR_offset,
962*7c478bd9Sstevel@tonic-gate 				    tp->tim_myname, tp->tim_mylen);
963*7c478bd9Sstevel@tonic-gate 			}
964*7c478bd9Sstevel@tonic-gate 			tim_ioctl_send_reply(q, tp->tim_iocsave, mp);
965*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave = NULL;
966*7c478bd9Sstevel@tonic-gate 			tp->tim_saved_prim = -1;
967*7c478bd9Sstevel@tonic-gate 			tp->tim_flags &= ~(WAITIOCACK | WAIT_IOCINFOACK |
968*7c478bd9Sstevel@tonic-gate 			    TI_CAP_RECVD | CAP_WANTS_INFO);
969*7c478bd9Sstevel@tonic-gate 			break;
970*7c478bd9Sstevel@tonic-gate 		}
971*7c478bd9Sstevel@tonic-gate 
972*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
973*7c478bd9Sstevel@tonic-gate 	    case T_OPTMGMT_ACK:
974*7c478bd9Sstevel@tonic-gate 
975*7c478bd9Sstevel@tonic-gate 		tilog("timodrproc: Got T_OPTMGMT_ACK\n", 0);
976*7c478bd9Sstevel@tonic-gate 
977*7c478bd9Sstevel@tonic-gate 		/* Restore db_type - recover() might have change it */
978*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_PCPROTO;
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate 		if (((tp->tim_flags & WAITIOCACK) == 0) ||
981*7c478bd9Sstevel@tonic-gate 		    ((tp->tim_saved_prim != T_SVR4_OPTMGMT_REQ) &&
982*7c478bd9Sstevel@tonic-gate 		    (tp->tim_saved_prim != T_OPTMGMT_REQ))) {
983*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
984*7c478bd9Sstevel@tonic-gate 		} else {
985*7c478bd9Sstevel@tonic-gate 			ASSERT(tp->tim_iocsave != NULL);
986*7c478bd9Sstevel@tonic-gate 			tim_ioctl_send_reply(q, tp->tim_iocsave, mp);
987*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave = NULL;
988*7c478bd9Sstevel@tonic-gate 			tp->tim_saved_prim = -1;
989*7c478bd9Sstevel@tonic-gate 			tp->tim_flags &= ~(WAITIOCACK | WAIT_IOCINFOACK |
990*7c478bd9Sstevel@tonic-gate 			    TI_CAP_RECVD | CAP_WANTS_INFO);
991*7c478bd9Sstevel@tonic-gate 		}
992*7c478bd9Sstevel@tonic-gate 		break;
993*7c478bd9Sstevel@tonic-gate 
994*7c478bd9Sstevel@tonic-gate 		case T_INFO_ACK: {
995*7c478bd9Sstevel@tonic-gate 		    struct T_info_ack *tia = (struct T_info_ack *)pptr;
996*7c478bd9Sstevel@tonic-gate 
997*7c478bd9Sstevel@tonic-gate 		    /* Restore db_type - recover() might have changed it */
998*7c478bd9Sstevel@tonic-gate 		    mp->b_datap->db_type = M_PCPROTO;
999*7c478bd9Sstevel@tonic-gate 
1000*7c478bd9Sstevel@tonic-gate 		    if (blen < sizeof (*tia)) {
1001*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
1002*7c478bd9Sstevel@tonic-gate 			break;
1003*7c478bd9Sstevel@tonic-gate 		    }
1004*7c478bd9Sstevel@tonic-gate 
1005*7c478bd9Sstevel@tonic-gate 		    tilog("timodrproc: Got T_INFO_ACK, flags = %x\n",
1006*7c478bd9Sstevel@tonic-gate 			tp->tim_flags);
1007*7c478bd9Sstevel@tonic-gate 
1008*7c478bd9Sstevel@tonic-gate 		    timodprocessinfo(q, tp, tia);
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate 		    TILOG("timodrproc: flags = %x\n", tp->tim_flags);
1011*7c478bd9Sstevel@tonic-gate 		    if ((tp->tim_flags & WAITIOCACK) != 0) {
1012*7c478bd9Sstevel@tonic-gate 			size_t	expected_ack_size;
1013*7c478bd9Sstevel@tonic-gate 			ssize_t	deficit;
1014*7c478bd9Sstevel@tonic-gate 			int	ioc_cmd;
1015*7c478bd9Sstevel@tonic-gate 			struct T_capability_ack *tcap;
1016*7c478bd9Sstevel@tonic-gate 
1017*7c478bd9Sstevel@tonic-gate 			/*
1018*7c478bd9Sstevel@tonic-gate 			 * The only case when T_INFO_ACK may be received back
1019*7c478bd9Sstevel@tonic-gate 			 * when we are waiting for ioctl to complete is when
1020*7c478bd9Sstevel@tonic-gate 			 * this ioctl sent T_INFO_REQ down.
1021*7c478bd9Sstevel@tonic-gate 			 */
1022*7c478bd9Sstevel@tonic-gate 			if (!(tp->tim_flags & WAIT_IOCINFOACK)) {
1023*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
1024*7c478bd9Sstevel@tonic-gate 				break;
1025*7c478bd9Sstevel@tonic-gate 			}
1026*7c478bd9Sstevel@tonic-gate 			ASSERT(tp->tim_iocsave != NULL);
1027*7c478bd9Sstevel@tonic-gate 
1028*7c478bd9Sstevel@tonic-gate 			iocbp = (struct iocblk *)tp->tim_iocsave->b_rptr;
1029*7c478bd9Sstevel@tonic-gate 			ioc_cmd = iocbp->ioc_cmd;
1030*7c478bd9Sstevel@tonic-gate 
1031*7c478bd9Sstevel@tonic-gate 			/*
1032*7c478bd9Sstevel@tonic-gate 			 * Was it sent from TI_CAPABILITY emulation?
1033*7c478bd9Sstevel@tonic-gate 			 */
1034*7c478bd9Sstevel@tonic-gate 			if (ioc_cmd == TI_CAPABILITY) {
1035*7c478bd9Sstevel@tonic-gate 				struct T_info_ack	saved_info;
1036*7c478bd9Sstevel@tonic-gate 
1037*7c478bd9Sstevel@tonic-gate 				/*
1038*7c478bd9Sstevel@tonic-gate 				 * Perform sanity checks. The only case when we
1039*7c478bd9Sstevel@tonic-gate 				 * send T_INFO_REQ from TI_CAPABILITY is when
1040*7c478bd9Sstevel@tonic-gate 				 * timod emulates T_CAPABILITY_REQ and CAP_bits1
1041*7c478bd9Sstevel@tonic-gate 				 * has TC1_INFO set.
1042*7c478bd9Sstevel@tonic-gate 				 */
1043*7c478bd9Sstevel@tonic-gate 				if ((tp->tim_flags &
1044*7c478bd9Sstevel@tonic-gate 				    (TI_CAP_RECVD | CAP_WANTS_INFO)) !=
1045*7c478bd9Sstevel@tonic-gate 				    (TI_CAP_RECVD | CAP_WANTS_INFO)) {
1046*7c478bd9Sstevel@tonic-gate 					putnext(q, mp);
1047*7c478bd9Sstevel@tonic-gate 					break;
1048*7c478bd9Sstevel@tonic-gate 				}
1049*7c478bd9Sstevel@tonic-gate 
1050*7c478bd9Sstevel@tonic-gate 				TILOG("timodrproc: emulating TI_CAPABILITY/"
1051*7c478bd9Sstevel@tonic-gate 				    "info\n", 0);
1052*7c478bd9Sstevel@tonic-gate 
1053*7c478bd9Sstevel@tonic-gate 				/* Save info & reuse mp for T_CAPABILITY_ACK */
1054*7c478bd9Sstevel@tonic-gate 				saved_info = *tia;
1055*7c478bd9Sstevel@tonic-gate 
1056*7c478bd9Sstevel@tonic-gate 				mp = tpi_ack_alloc(mp,
1057*7c478bd9Sstevel@tonic-gate 				    sizeof (struct T_capability_ack),
1058*7c478bd9Sstevel@tonic-gate 				    M_PCPROTO, T_CAPABILITY_ACK);
1059*7c478bd9Sstevel@tonic-gate 
1060*7c478bd9Sstevel@tonic-gate 				if (mp == NULL) {
1061*7c478bd9Sstevel@tonic-gate 					tilog("timodrproc: realloc failed, "
1062*7c478bd9Sstevel@tonic-gate 					    "no recovery attempted\n", 0);
1063*7c478bd9Sstevel@tonic-gate 					return (1);
1064*7c478bd9Sstevel@tonic-gate 				}
1065*7c478bd9Sstevel@tonic-gate 
1066*7c478bd9Sstevel@tonic-gate 				/*
1067*7c478bd9Sstevel@tonic-gate 				 * Copy T_INFO information into T_CAPABILITY_ACK
1068*7c478bd9Sstevel@tonic-gate 				 */
1069*7c478bd9Sstevel@tonic-gate 				tcap = (struct T_capability_ack *)mp->b_rptr;
1070*7c478bd9Sstevel@tonic-gate 				tcap->CAP_bits1 = TC1_INFO;
1071*7c478bd9Sstevel@tonic-gate 				tcap->INFO_ack = saved_info;
1072*7c478bd9Sstevel@tonic-gate 				tp->tim_flags &= ~(WAITIOCACK |
1073*7c478bd9Sstevel@tonic-gate 				    WAIT_IOCINFOACK | TI_CAP_RECVD |
1074*7c478bd9Sstevel@tonic-gate 				    CAP_WANTS_INFO);
1075*7c478bd9Sstevel@tonic-gate 				tim_ioctl_send_reply(q, tp->tim_iocsave, mp);
1076*7c478bd9Sstevel@tonic-gate 				tp->tim_iocsave = NULL;
1077*7c478bd9Sstevel@tonic-gate 				tp->tim_saved_prim = -1;
1078*7c478bd9Sstevel@tonic-gate 				break;
1079*7c478bd9Sstevel@tonic-gate 			}
1080*7c478bd9Sstevel@tonic-gate 
1081*7c478bd9Sstevel@tonic-gate 			/*
1082*7c478bd9Sstevel@tonic-gate 			 * The code for TI_SYNC/TI_GETINFO is left here only for
1083*7c478bd9Sstevel@tonic-gate 			 * backward compatibility with staticaly linked old
1084*7c478bd9Sstevel@tonic-gate 			 * applications. New TLI/XTI code should use
1085*7c478bd9Sstevel@tonic-gate 			 * TI_CAPABILITY for getting transport info and should
1086*7c478bd9Sstevel@tonic-gate 			 * not use TI_GETINFO/TI_SYNC for this purpose.
1087*7c478bd9Sstevel@tonic-gate 			 */
1088*7c478bd9Sstevel@tonic-gate 
1089*7c478bd9Sstevel@tonic-gate 			/*
1090*7c478bd9Sstevel@tonic-gate 			 * make sure the message sent back is the size of
1091*7c478bd9Sstevel@tonic-gate 			 * the "expected ack"
1092*7c478bd9Sstevel@tonic-gate 			 * For TI_GETINFO, expected ack size is
1093*7c478bd9Sstevel@tonic-gate 			 *	sizeof (T_info_ack)
1094*7c478bd9Sstevel@tonic-gate 			 * For TI_SYNC, expected ack size is
1095*7c478bd9Sstevel@tonic-gate 			 *	sizeof (struct ti_sync_ack);
1096*7c478bd9Sstevel@tonic-gate 			 */
1097*7c478bd9Sstevel@tonic-gate 			if (ioc_cmd != TI_GETINFO && ioc_cmd != TI_SYNC) {
1098*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
1099*7c478bd9Sstevel@tonic-gate 				break;
1100*7c478bd9Sstevel@tonic-gate 			}
1101*7c478bd9Sstevel@tonic-gate 
1102*7c478bd9Sstevel@tonic-gate 			expected_ack_size =
1103*7c478bd9Sstevel@tonic-gate 				sizeof (struct T_info_ack); /* TI_GETINFO */
1104*7c478bd9Sstevel@tonic-gate 			if (iocbp->ioc_cmd == TI_SYNC) {
1105*7c478bd9Sstevel@tonic-gate 				expected_ack_size = 2 * sizeof (uint32_t) +
1106*7c478bd9Sstevel@tonic-gate 				    sizeof (struct ti_sync_ack);
1107*7c478bd9Sstevel@tonic-gate 			}
1108*7c478bd9Sstevel@tonic-gate 			deficit = expected_ack_size - blen;
1109*7c478bd9Sstevel@tonic-gate 
1110*7c478bd9Sstevel@tonic-gate 			if (deficit != 0) {
1111*7c478bd9Sstevel@tonic-gate 				if (mp->b_datap->db_lim - mp->b_wptr <
1112*7c478bd9Sstevel@tonic-gate 				    deficit) {
1113*7c478bd9Sstevel@tonic-gate 				    mblk_t *tmp = allocb(expected_ack_size,
1114*7c478bd9Sstevel@tonic-gate 					BPRI_HI);
1115*7c478bd9Sstevel@tonic-gate 				    if (tmp == NULL) {
1116*7c478bd9Sstevel@tonic-gate 					ASSERT((mp->b_datap->db_lim -
1117*7c478bd9Sstevel@tonic-gate 						mp->b_datap->db_base) <
1118*7c478bd9Sstevel@tonic-gate 						sizeof (struct T_error_ack));
1119*7c478bd9Sstevel@tonic-gate 
1120*7c478bd9Sstevel@tonic-gate 					tilog("timodrproc: allocb failed no "
1121*7c478bd9Sstevel@tonic-gate 					    "recovery attempt\n", 0);
1122*7c478bd9Sstevel@tonic-gate 
1123*7c478bd9Sstevel@tonic-gate 					mp->b_rptr = mp->b_datap->db_base;
1124*7c478bd9Sstevel@tonic-gate 					pptr = (union T_primitives *)
1125*7c478bd9Sstevel@tonic-gate 						mp->b_rptr;
1126*7c478bd9Sstevel@tonic-gate 					pptr->error_ack.ERROR_prim = T_INFO_ACK;
1127*7c478bd9Sstevel@tonic-gate 					pptr->error_ack.TLI_error = TSYSERR;
1128*7c478bd9Sstevel@tonic-gate 					pptr->error_ack.UNIX_error = EAGAIN;
1129*7c478bd9Sstevel@tonic-gate 					pptr->error_ack.PRIM_type = T_ERROR_ACK;
1130*7c478bd9Sstevel@tonic-gate 					mp->b_datap->db_type = M_PCPROTO;
1131*7c478bd9Sstevel@tonic-gate 					tim_send_ioc_error_ack(q, tp, mp);
1132*7c478bd9Sstevel@tonic-gate 					break;
1133*7c478bd9Sstevel@tonic-gate 				    } else {
1134*7c478bd9Sstevel@tonic-gate 					bcopy(mp->b_rptr, tmp->b_rptr, blen);
1135*7c478bd9Sstevel@tonic-gate 					tmp->b_wptr += blen;
1136*7c478bd9Sstevel@tonic-gate 					pptr = (union T_primitives *)
1137*7c478bd9Sstevel@tonic-gate 					    tmp->b_rptr;
1138*7c478bd9Sstevel@tonic-gate 					freemsg(mp);
1139*7c478bd9Sstevel@tonic-gate 					mp = tmp;
1140*7c478bd9Sstevel@tonic-gate 				    }
1141*7c478bd9Sstevel@tonic-gate 				}
1142*7c478bd9Sstevel@tonic-gate 			}
1143*7c478bd9Sstevel@tonic-gate 			/*
1144*7c478bd9Sstevel@tonic-gate 			 * We now have "mp" which has enough space for an
1145*7c478bd9Sstevel@tonic-gate 			 * appropriate ack and contains struct T_info_ack
1146*7c478bd9Sstevel@tonic-gate 			 * that the transport provider returned. We now
1147*7c478bd9Sstevel@tonic-gate 			 * stuff it with more stuff to fullfill
1148*7c478bd9Sstevel@tonic-gate 			 * TI_SYNC ioctl needs, as necessary
1149*7c478bd9Sstevel@tonic-gate 			 */
1150*7c478bd9Sstevel@tonic-gate 			if (iocbp->ioc_cmd == TI_SYNC) {
1151*7c478bd9Sstevel@tonic-gate 				/*
1152*7c478bd9Sstevel@tonic-gate 				 * Assumes struct T_info_ack is first embedded
1153*7c478bd9Sstevel@tonic-gate 				 * type in struct ti_sync_ack so it is
1154*7c478bd9Sstevel@tonic-gate 				 * automatically there.
1155*7c478bd9Sstevel@tonic-gate 				 */
1156*7c478bd9Sstevel@tonic-gate 				struct ti_sync_ack *tsap =
1157*7c478bd9Sstevel@tonic-gate 				    (struct ti_sync_ack *)mp->b_rptr;
1158*7c478bd9Sstevel@tonic-gate 
1159*7c478bd9Sstevel@tonic-gate 				/*
1160*7c478bd9Sstevel@tonic-gate 				 * tsap->tsa_qlen needs to be set only if
1161*7c478bd9Sstevel@tonic-gate 				 * TSRF_QLEN_REQ flag is set, but for
1162*7c478bd9Sstevel@tonic-gate 				 * compatibility with statically linked
1163*7c478bd9Sstevel@tonic-gate 				 * applications it is set here regardless of the
1164*7c478bd9Sstevel@tonic-gate 				 * flag since old XTI library expected it to be
1165*7c478bd9Sstevel@tonic-gate 				 * set.
1166*7c478bd9Sstevel@tonic-gate 				 */
1167*7c478bd9Sstevel@tonic-gate 				tsap->tsa_qlen = tp->tim_backlog;
1168*7c478bd9Sstevel@tonic-gate 				tsap->tsa_flags = 0x0; /* intialize clear */
1169*7c478bd9Sstevel@tonic-gate 				if (tp->tim_flags & PEEK_RDQ_EXPIND) {
1170*7c478bd9Sstevel@tonic-gate 					/*
1171*7c478bd9Sstevel@tonic-gate 					 * Request to peek for EXPIND in
1172*7c478bd9Sstevel@tonic-gate 					 * rcvbuf.
1173*7c478bd9Sstevel@tonic-gate 					 */
1174*7c478bd9Sstevel@tonic-gate 					if (ti_expind_on_rdqueues(q)) {
1175*7c478bd9Sstevel@tonic-gate 						/*
1176*7c478bd9Sstevel@tonic-gate 						 * Expedited data is
1177*7c478bd9Sstevel@tonic-gate 						 * queued on the stream
1178*7c478bd9Sstevel@tonic-gate 						 * read side
1179*7c478bd9Sstevel@tonic-gate 						 */
1180*7c478bd9Sstevel@tonic-gate 						tsap->tsa_flags |=
1181*7c478bd9Sstevel@tonic-gate 						    TSAF_EXP_QUEUED;
1182*7c478bd9Sstevel@tonic-gate 					}
1183*7c478bd9Sstevel@tonic-gate 					tp->tim_flags &=
1184*7c478bd9Sstevel@tonic-gate 					    ~PEEK_RDQ_EXPIND;
1185*7c478bd9Sstevel@tonic-gate 				}
1186*7c478bd9Sstevel@tonic-gate 				mp->b_wptr += 2*sizeof (uint32_t);
1187*7c478bd9Sstevel@tonic-gate 			}
1188*7c478bd9Sstevel@tonic-gate 			tim_ioctl_send_reply(q, tp->tim_iocsave, mp);
1189*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave = NULL;
1190*7c478bd9Sstevel@tonic-gate 			tp->tim_saved_prim = -1;
1191*7c478bd9Sstevel@tonic-gate 			tp->tim_flags &= ~(WAITIOCACK | WAIT_IOCINFOACK |
1192*7c478bd9Sstevel@tonic-gate 			    TI_CAP_RECVD | CAP_WANTS_INFO);
1193*7c478bd9Sstevel@tonic-gate 			break;
1194*7c478bd9Sstevel@tonic-gate 		    }
1195*7c478bd9Sstevel@tonic-gate 	    }
1196*7c478bd9Sstevel@tonic-gate 
1197*7c478bd9Sstevel@tonic-gate 	    putnext(q, mp);
1198*7c478bd9Sstevel@tonic-gate 	    break;
1199*7c478bd9Sstevel@tonic-gate 
1200*7c478bd9Sstevel@tonic-gate 	    case T_ADDR_ACK:
1201*7c478bd9Sstevel@tonic-gate 		tilog("timodrproc: Got T_ADDR_ACK\n", 0);
1202*7c478bd9Sstevel@tonic-gate 		tim_send_reply(q, mp, tp, T_ADDR_REQ);
1203*7c478bd9Sstevel@tonic-gate 		break;
1204*7c478bd9Sstevel@tonic-gate 
1205*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
1206*7c478bd9Sstevel@tonic-gate 		case T_CONN_IND: {
1207*7c478bd9Sstevel@tonic-gate 			struct T_conn_ind *tcip =
1208*7c478bd9Sstevel@tonic-gate 			    (struct T_conn_ind *)mp->b_rptr;
1209*7c478bd9Sstevel@tonic-gate 
1210*7c478bd9Sstevel@tonic-gate 			tilog("timodrproc: Got T_CONN_IND\n", 0);
1211*7c478bd9Sstevel@tonic-gate 
1212*7c478bd9Sstevel@tonic-gate 			if (blen >= sizeof (*tcip) &&
1213*7c478bd9Sstevel@tonic-gate 			    MBLKIN(mp, tcip->SRC_offset, tcip->SRC_length)) {
1214*7c478bd9Sstevel@tonic-gate 				if (((nbp = dupmsg(mp)) != NULL) ||
1215*7c478bd9Sstevel@tonic-gate 				    ((nbp = copymsg(mp)) != NULL)) {
1216*7c478bd9Sstevel@tonic-gate 					nbp->b_next = tp->tim_consave;
1217*7c478bd9Sstevel@tonic-gate 					tp->tim_consave = nbp;
1218*7c478bd9Sstevel@tonic-gate 				} else {
1219*7c478bd9Sstevel@tonic-gate 					tim_recover(q, mp,
1220*7c478bd9Sstevel@tonic-gate 					    (t_scalar_t)sizeof (mblk_t));
1221*7c478bd9Sstevel@tonic-gate 					return (1);
1222*7c478bd9Sstevel@tonic-gate 				}
1223*7c478bd9Sstevel@tonic-gate 			}
1224*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
1225*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
1226*7c478bd9Sstevel@tonic-gate 			if (audit_active)
1227*7c478bd9Sstevel@tonic-gate 				audit_sock(T_CONN_IND, q, mp, TIMOD_ID);
1228*7c478bd9Sstevel@tonic-gate #endif
1229*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
1230*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
1231*7c478bd9Sstevel@tonic-gate 			break;
1232*7c478bd9Sstevel@tonic-gate 		}
1233*7c478bd9Sstevel@tonic-gate 
1234*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
1235*7c478bd9Sstevel@tonic-gate 	    case T_CONN_CON:
1236*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tp->tim_mutex);
1237*7c478bd9Sstevel@tonic-gate 		if (tp->tim_peercred != NULL)
1238*7c478bd9Sstevel@tonic-gate 			crfree(tp->tim_peercred);
1239*7c478bd9Sstevel@tonic-gate 		tp->tim_peercred = DB_CRED(mp);
1240*7c478bd9Sstevel@tonic-gate 		if (tp->tim_peercred != NULL)
1241*7c478bd9Sstevel@tonic-gate 			crhold(tp->tim_peercred);
1242*7c478bd9Sstevel@tonic-gate 		tp->tim_cpid = DB_CPID(mp);
1243*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tp->tim_mutex);
1244*7c478bd9Sstevel@tonic-gate 
1245*7c478bd9Sstevel@tonic-gate 		tilog("timodrproc: Got T_CONN_CON\n", 0);
1246*7c478bd9Sstevel@tonic-gate 
1247*7c478bd9Sstevel@tonic-gate 		tp->tim_flags &= ~CONNWAIT;
1248*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
1249*7c478bd9Sstevel@tonic-gate 		break;
1250*7c478bd9Sstevel@tonic-gate 
1251*7c478bd9Sstevel@tonic-gate 	    case T_DISCON_IND: {
1252*7c478bd9Sstevel@tonic-gate 		struct T_discon_ind *disp;
1253*7c478bd9Sstevel@tonic-gate 		struct T_conn_ind *conp;
1254*7c478bd9Sstevel@tonic-gate 		mblk_t *pbp = NULL;
1255*7c478bd9Sstevel@tonic-gate 
1256*7c478bd9Sstevel@tonic-gate 		if (q->q_first != 0)
1257*7c478bd9Sstevel@tonic-gate 			tilog("timodrput: T_DISCON_IND - flow control\n", 0);
1258*7c478bd9Sstevel@tonic-gate 
1259*7c478bd9Sstevel@tonic-gate 		if (blen < sizeof (*disp)) {
1260*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
1261*7c478bd9Sstevel@tonic-gate 			break;
1262*7c478bd9Sstevel@tonic-gate 		}
1263*7c478bd9Sstevel@tonic-gate 
1264*7c478bd9Sstevel@tonic-gate 		disp = (struct T_discon_ind *)mp->b_rptr;
1265*7c478bd9Sstevel@tonic-gate 
1266*7c478bd9Sstevel@tonic-gate 		tilog("timodrproc: Got T_DISCON_IND Reason: %d\n",
1267*7c478bd9Sstevel@tonic-gate 			disp->DISCON_reason);
1268*7c478bd9Sstevel@tonic-gate 
1269*7c478bd9Sstevel@tonic-gate 		tp->tim_flags &= ~(CONNWAIT|LOCORDREL|REMORDREL);
1270*7c478bd9Sstevel@tonic-gate 		tim_clear_peer(tp);
1271*7c478bd9Sstevel@tonic-gate 		for (nbp = tp->tim_consave; nbp; nbp = nbp->b_next) {
1272*7c478bd9Sstevel@tonic-gate 		    conp = (struct T_conn_ind *)nbp->b_rptr;
1273*7c478bd9Sstevel@tonic-gate 		    if (conp->SEQ_number == disp->SEQ_number)
1274*7c478bd9Sstevel@tonic-gate 			break;
1275*7c478bd9Sstevel@tonic-gate 		    pbp = nbp;
1276*7c478bd9Sstevel@tonic-gate 		}
1277*7c478bd9Sstevel@tonic-gate 		if (nbp) {
1278*7c478bd9Sstevel@tonic-gate 		    if (pbp)
1279*7c478bd9Sstevel@tonic-gate 			pbp->b_next = nbp->b_next;
1280*7c478bd9Sstevel@tonic-gate 		    else
1281*7c478bd9Sstevel@tonic-gate 			tp->tim_consave = nbp->b_next;
1282*7c478bd9Sstevel@tonic-gate 		    nbp->b_next = NULL;
1283*7c478bd9Sstevel@tonic-gate 		    freemsg(nbp);
1284*7c478bd9Sstevel@tonic-gate 		}
1285*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
1286*7c478bd9Sstevel@tonic-gate 		break;
1287*7c478bd9Sstevel@tonic-gate 	    }
1288*7c478bd9Sstevel@tonic-gate 
1289*7c478bd9Sstevel@tonic-gate 	    case T_ORDREL_IND:
1290*7c478bd9Sstevel@tonic-gate 
1291*7c478bd9Sstevel@tonic-gate 		tilog("timodrproc: Got T_ORDREL_IND\n", 0);
1292*7c478bd9Sstevel@tonic-gate 
1293*7c478bd9Sstevel@tonic-gate 		if (tp->tim_flags & LOCORDREL) {
1294*7c478bd9Sstevel@tonic-gate 			tp->tim_flags &= ~(LOCORDREL|REMORDREL);
1295*7c478bd9Sstevel@tonic-gate 			tim_clear_peer(tp);
1296*7c478bd9Sstevel@tonic-gate 		} else {
1297*7c478bd9Sstevel@tonic-gate 			tp->tim_flags |= REMORDREL;
1298*7c478bd9Sstevel@tonic-gate 		}
1299*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
1300*7c478bd9Sstevel@tonic-gate 		break;
1301*7c478bd9Sstevel@tonic-gate 
1302*7c478bd9Sstevel@tonic-gate 	    case T_EXDATA_IND:
1303*7c478bd9Sstevel@tonic-gate 	    case T_DATA_IND:
1304*7c478bd9Sstevel@tonic-gate 	    case T_UNITDATA_IND:
1305*7c478bd9Sstevel@tonic-gate 		if (pptr->type == T_EXDATA_IND)
1306*7c478bd9Sstevel@tonic-gate 			tilog("timodrproc: Got T_EXDATA_IND\n", 0);
1307*7c478bd9Sstevel@tonic-gate 
1308*7c478bd9Sstevel@tonic-gate 		if (!bcanputnext(q, mp->b_band)) {
1309*7c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
1310*7c478bd9Sstevel@tonic-gate 			return (1);
1311*7c478bd9Sstevel@tonic-gate 		}
1312*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
1313*7c478bd9Sstevel@tonic-gate 		break;
1314*7c478bd9Sstevel@tonic-gate 
1315*7c478bd9Sstevel@tonic-gate 	    case T_CAPABILITY_ACK: {
1316*7c478bd9Sstevel@tonic-gate 			struct T_capability_ack	*tca;
1317*7c478bd9Sstevel@tonic-gate 
1318*7c478bd9Sstevel@tonic-gate 			if (blen < sizeof (*tca)) {
1319*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
1320*7c478bd9Sstevel@tonic-gate 				break;
1321*7c478bd9Sstevel@tonic-gate 			}
1322*7c478bd9Sstevel@tonic-gate 
1323*7c478bd9Sstevel@tonic-gate 			/* This transport supports T_CAPABILITY_REQ */
1324*7c478bd9Sstevel@tonic-gate 			tilog("timodrproc: Got T_CAPABILITY_ACK\n", 0);
1325*7c478bd9Sstevel@tonic-gate 
1326*7c478bd9Sstevel@tonic-gate 			PI_PROVLOCK(tp->tim_provinfo);
1327*7c478bd9Sstevel@tonic-gate 			if (tp->tim_provinfo->tpi_capability != PI_YES)
1328*7c478bd9Sstevel@tonic-gate 				tp->tim_provinfo->tpi_capability = PI_YES;
1329*7c478bd9Sstevel@tonic-gate 			PI_PROVUNLOCK(tp->tim_provinfo);
1330*7c478bd9Sstevel@tonic-gate 
1331*7c478bd9Sstevel@tonic-gate 			/* Reset possible pending timeout */
1332*7c478bd9Sstevel@tonic-gate 			if (tp->tim_tcap_timoutid != 0) {
1333*7c478bd9Sstevel@tonic-gate 				(void) quntimeout(q, tp->tim_tcap_timoutid);
1334*7c478bd9Sstevel@tonic-gate 				tp->tim_tcap_timoutid = 0;
1335*7c478bd9Sstevel@tonic-gate 			}
1336*7c478bd9Sstevel@tonic-gate 
1337*7c478bd9Sstevel@tonic-gate 			tca = (struct T_capability_ack *)mp->b_rptr;
1338*7c478bd9Sstevel@tonic-gate 
1339*7c478bd9Sstevel@tonic-gate 			if (tca->CAP_bits1 & TC1_INFO)
1340*7c478bd9Sstevel@tonic-gate 				timodprocessinfo(q, tp, &tca->INFO_ack);
1341*7c478bd9Sstevel@tonic-gate 
1342*7c478bd9Sstevel@tonic-gate 			tim_send_reply(q, mp, tp, T_CAPABILITY_REQ);
1343*7c478bd9Sstevel@tonic-gate 		}
1344*7c478bd9Sstevel@tonic-gate 		break;
1345*7c478bd9Sstevel@tonic-gate 	    }
1346*7c478bd9Sstevel@tonic-gate 	    break;
1347*7c478bd9Sstevel@tonic-gate 
1348*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
1349*7c478bd9Sstevel@tonic-gate 	case M_FLUSH:
1350*7c478bd9Sstevel@tonic-gate 
1351*7c478bd9Sstevel@tonic-gate 		tilog("timodrproc: Got M_FLUSH\n", 0);
1352*7c478bd9Sstevel@tonic-gate 
1353*7c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
1354*7c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHBAND)
1355*7c478bd9Sstevel@tonic-gate 				flushband(q, *(mp->b_rptr + 1), FLUSHDATA);
1356*7c478bd9Sstevel@tonic-gate 			else
1357*7c478bd9Sstevel@tonic-gate 				flushq(q, FLUSHDATA);
1358*7c478bd9Sstevel@tonic-gate 		}
1359*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
1360*7c478bd9Sstevel@tonic-gate 		break;
1361*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
1362*7c478bd9Sstevel@tonic-gate 
1363*7c478bd9Sstevel@tonic-gate 	case M_IOCACK:
1364*7c478bd9Sstevel@tonic-gate 	    iocbp = (struct iocblk *)mp->b_rptr;
1365*7c478bd9Sstevel@tonic-gate 
1366*7c478bd9Sstevel@tonic-gate 	    tilog("timodrproc: Got M_IOCACK\n", 0);
1367*7c478bd9Sstevel@tonic-gate 
1368*7c478bd9Sstevel@tonic-gate 	    if (iocbp->ioc_cmd == TI_GETMYNAME) {
1369*7c478bd9Sstevel@tonic-gate 
1370*7c478bd9Sstevel@tonic-gate 		/*
1371*7c478bd9Sstevel@tonic-gate 		 * Transport provider supports this ioctl,
1372*7c478bd9Sstevel@tonic-gate 		 * so I don't have to.
1373*7c478bd9Sstevel@tonic-gate 		 */
1374*7c478bd9Sstevel@tonic-gate 		if ((tp->tim_flags & DO_MYNAME) != 0) {
1375*7c478bd9Sstevel@tonic-gate 			tp->tim_flags &= ~DO_MYNAME;
1376*7c478bd9Sstevel@tonic-gate 			PI_PROVLOCK(tp->tim_provinfo);
1377*7c478bd9Sstevel@tonic-gate 			tp->tim_provinfo->tpi_myname = PI_YES;
1378*7c478bd9Sstevel@tonic-gate 			PI_PROVUNLOCK(tp->tim_provinfo);
1379*7c478bd9Sstevel@tonic-gate 		}
1380*7c478bd9Sstevel@tonic-gate 
1381*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->tim_mymaxlen >= 0);
1382*7c478bd9Sstevel@tonic-gate 		if (tp->tim_mymaxlen != 0) {
1383*7c478bd9Sstevel@tonic-gate 			kmem_free(tp->tim_myname, (size_t)tp->tim_mymaxlen);
1384*7c478bd9Sstevel@tonic-gate 			tp->tim_myname = NULL;
1385*7c478bd9Sstevel@tonic-gate 			tp->tim_mymaxlen = 0;
1386*7c478bd9Sstevel@tonic-gate 		}
1387*7c478bd9Sstevel@tonic-gate 		/* tim_iocsave may already be overwritten. */
1388*7c478bd9Sstevel@tonic-gate 		if (tp->tim_saved_prim == -1) {
1389*7c478bd9Sstevel@tonic-gate 			freemsg(tp->tim_iocsave);
1390*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave = NULL;
1391*7c478bd9Sstevel@tonic-gate 		}
1392*7c478bd9Sstevel@tonic-gate 	    } else if (iocbp->ioc_cmd == TI_GETPEERNAME) {
1393*7c478bd9Sstevel@tonic-gate 		boolean_t clearit;
1394*7c478bd9Sstevel@tonic-gate 
1395*7c478bd9Sstevel@tonic-gate 		/*
1396*7c478bd9Sstevel@tonic-gate 		 * Transport provider supports this ioctl,
1397*7c478bd9Sstevel@tonic-gate 		 * so I don't have to.
1398*7c478bd9Sstevel@tonic-gate 		 */
1399*7c478bd9Sstevel@tonic-gate 		if ((tp->tim_flags & DO_PEERNAME) != 0) {
1400*7c478bd9Sstevel@tonic-gate 			tp->tim_flags &= ~DO_PEERNAME;
1401*7c478bd9Sstevel@tonic-gate 			PI_PROVLOCK(tp->tim_provinfo);
1402*7c478bd9Sstevel@tonic-gate 			tp->tim_provinfo->tpi_peername = PI_YES;
1403*7c478bd9Sstevel@tonic-gate 			PI_PROVUNLOCK(tp->tim_provinfo);
1404*7c478bd9Sstevel@tonic-gate 		}
1405*7c478bd9Sstevel@tonic-gate 
1406*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tp->tim_mutex);
1407*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->tim_peermaxlen >= 0);
1408*7c478bd9Sstevel@tonic-gate 		clearit = tp->tim_peermaxlen != 0;
1409*7c478bd9Sstevel@tonic-gate 		if (clearit) {
1410*7c478bd9Sstevel@tonic-gate 			kmem_free(tp->tim_peername, tp->tim_peermaxlen);
1411*7c478bd9Sstevel@tonic-gate 			tp->tim_peername = NULL;
1412*7c478bd9Sstevel@tonic-gate 			tp->tim_peermaxlen = 0;
1413*7c478bd9Sstevel@tonic-gate 			tp->tim_peerlen = 0;
1414*7c478bd9Sstevel@tonic-gate 		}
1415*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tp->tim_mutex);
1416*7c478bd9Sstevel@tonic-gate 		if (clearit) {
1417*7c478bd9Sstevel@tonic-gate 			mblk_t *bp;
1418*7c478bd9Sstevel@tonic-gate 
1419*7c478bd9Sstevel@tonic-gate 			bp = tp->tim_consave;
1420*7c478bd9Sstevel@tonic-gate 			while (bp != NULL) {
1421*7c478bd9Sstevel@tonic-gate 				nbp = bp->b_next;
1422*7c478bd9Sstevel@tonic-gate 				bp->b_next = NULL;
1423*7c478bd9Sstevel@tonic-gate 				freemsg(bp);
1424*7c478bd9Sstevel@tonic-gate 				bp = nbp;
1425*7c478bd9Sstevel@tonic-gate 			}
1426*7c478bd9Sstevel@tonic-gate 			tp->tim_consave = NULL;
1427*7c478bd9Sstevel@tonic-gate 		}
1428*7c478bd9Sstevel@tonic-gate 		/* tim_iocsave may already be overwritten. */
1429*7c478bd9Sstevel@tonic-gate 		if (tp->tim_saved_prim == -1) {
1430*7c478bd9Sstevel@tonic-gate 			freemsg(tp->tim_iocsave);
1431*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave = NULL;
1432*7c478bd9Sstevel@tonic-gate 		}
1433*7c478bd9Sstevel@tonic-gate 	    }
1434*7c478bd9Sstevel@tonic-gate 	    putnext(q, mp);
1435*7c478bd9Sstevel@tonic-gate 	    break;
1436*7c478bd9Sstevel@tonic-gate 
1437*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
1438*7c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
1439*7c478bd9Sstevel@tonic-gate 
1440*7c478bd9Sstevel@tonic-gate 	    tilog("timodrproc: Got M_IOCNAK\n", 0);
1441*7c478bd9Sstevel@tonic-gate 
1442*7c478bd9Sstevel@tonic-gate 	    iocbp = (struct iocblk *)mp->b_rptr;
1443*7c478bd9Sstevel@tonic-gate 	    if (((iocbp->ioc_cmd == TI_GETMYNAME) ||
1444*7c478bd9Sstevel@tonic-gate 		(iocbp->ioc_cmd == TI_GETPEERNAME)) &&
1445*7c478bd9Sstevel@tonic-gate 		((iocbp->ioc_error == EINVAL) || (iocbp->ioc_error == 0))) {
1446*7c478bd9Sstevel@tonic-gate 		    PI_PROVLOCK(tp->tim_provinfo);
1447*7c478bd9Sstevel@tonic-gate 		    if (iocbp->ioc_cmd == TI_GETMYNAME) {
1448*7c478bd9Sstevel@tonic-gate 			    if (tp->tim_provinfo->tpi_myname == PI_DONTKNOW)
1449*7c478bd9Sstevel@tonic-gate 				    tp->tim_provinfo->tpi_myname = PI_NO;
1450*7c478bd9Sstevel@tonic-gate 		    } else if (iocbp->ioc_cmd == TI_GETPEERNAME) {
1451*7c478bd9Sstevel@tonic-gate 			    if (tp->tim_provinfo->tpi_peername == PI_DONTKNOW)
1452*7c478bd9Sstevel@tonic-gate 				    tp->tim_provinfo->tpi_peername = PI_NO;
1453*7c478bd9Sstevel@tonic-gate 		    }
1454*7c478bd9Sstevel@tonic-gate 		    PI_PROVUNLOCK(tp->tim_provinfo);
1455*7c478bd9Sstevel@tonic-gate 		    /* tim_iocsave may already be overwritten. */
1456*7c478bd9Sstevel@tonic-gate 		    if ((tp->tim_iocsave != NULL) &&
1457*7c478bd9Sstevel@tonic-gate 			(tp->tim_saved_prim == -1)) {
1458*7c478bd9Sstevel@tonic-gate 			    freemsg(mp);
1459*7c478bd9Sstevel@tonic-gate 			    mp = tp->tim_iocsave;
1460*7c478bd9Sstevel@tonic-gate 			    tp->tim_iocsave = NULL;
1461*7c478bd9Sstevel@tonic-gate 			    tp->tim_flags |= NAMEPROC;
1462*7c478bd9Sstevel@tonic-gate 			    if (ti_doname(WR(q), mp) != DONAME_CONT) {
1463*7c478bd9Sstevel@tonic-gate 				    tp->tim_flags &= ~NAMEPROC;
1464*7c478bd9Sstevel@tonic-gate 			    }
1465*7c478bd9Sstevel@tonic-gate 			    break;
1466*7c478bd9Sstevel@tonic-gate 		    }
1467*7c478bd9Sstevel@tonic-gate 	    }
1468*7c478bd9Sstevel@tonic-gate 	    putnext(q, mp);
1469*7c478bd9Sstevel@tonic-gate 	    break;
1470*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
1471*7c478bd9Sstevel@tonic-gate 	}
1472*7c478bd9Sstevel@tonic-gate 
1473*7c478bd9Sstevel@tonic-gate 	return (0);
1474*7c478bd9Sstevel@tonic-gate }
1475*7c478bd9Sstevel@tonic-gate 
1476*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
1477*7c478bd9Sstevel@tonic-gate /*
1478*7c478bd9Sstevel@tonic-gate  * timodwput -	Module write put procedure.  This is called from
1479*7c478bd9Sstevel@tonic-gate  *		the module, driver, or stream head upstream/downstream.
1480*7c478bd9Sstevel@tonic-gate  *		Handles M_FLUSH, M_DATA and some M_PROTO (T_DATA_REQ,
1481*7c478bd9Sstevel@tonic-gate  *		and T_UNITDATA_REQ) messages. All others are queued to
1482*7c478bd9Sstevel@tonic-gate  *		be handled by the service procedures.
1483*7c478bd9Sstevel@tonic-gate  */
1484*7c478bd9Sstevel@tonic-gate 
1485*7c478bd9Sstevel@tonic-gate static void
1486*7c478bd9Sstevel@tonic-gate timodwput(queue_t *q, mblk_t *mp)
1487*7c478bd9Sstevel@tonic-gate {
1488*7c478bd9Sstevel@tonic-gate 	union T_primitives *pptr;
1489*7c478bd9Sstevel@tonic-gate 	struct tim_tim *tp;
1490*7c478bd9Sstevel@tonic-gate 	struct iocblk *iocbp;
1491*7c478bd9Sstevel@tonic-gate 
1492*7c478bd9Sstevel@tonic-gate 	/*
1493*7c478bd9Sstevel@tonic-gate 	 * Enqueue normal-priority messages if our queue already
1494*7c478bd9Sstevel@tonic-gate 	 * holds some messages for deferred processing but don't
1495*7c478bd9Sstevel@tonic-gate 	 * enqueue those M_IOCTLs which will result in an
1496*7c478bd9Sstevel@tonic-gate 	 * M_PCPROTO (ie, high priority) message being created.
1497*7c478bd9Sstevel@tonic-gate 	 */
1498*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
1499*7c478bd9Sstevel@tonic-gate 	if (q->q_first != 0 && mp->b_datap->db_type < QPCTL) {
1500*7c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_IOCTL) {
1501*7c478bd9Sstevel@tonic-gate 			iocbp = (struct iocblk *)mp->b_rptr;
1502*7c478bd9Sstevel@tonic-gate 			switch (iocbp->ioc_cmd) {
1503*7c478bd9Sstevel@tonic-gate 			default:
1504*7c478bd9Sstevel@tonic-gate 				(void) putq(q, mp);
1505*7c478bd9Sstevel@tonic-gate 				return;
1506*7c478bd9Sstevel@tonic-gate 
1507*7c478bd9Sstevel@tonic-gate 			case TI_GETINFO:
1508*7c478bd9Sstevel@tonic-gate 			case TI_SYNC:
1509*7c478bd9Sstevel@tonic-gate 			case TI_CAPABILITY:
1510*7c478bd9Sstevel@tonic-gate 				break;
1511*7c478bd9Sstevel@tonic-gate 			}
1512*7c478bd9Sstevel@tonic-gate 		} else {
1513*7c478bd9Sstevel@tonic-gate 			(void) putq(q, mp);
1514*7c478bd9Sstevel@tonic-gate 			return;
1515*7c478bd9Sstevel@tonic-gate 		}
1516*7c478bd9Sstevel@tonic-gate 	}
1517*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
1518*7c478bd9Sstevel@tonic-gate 	/*
1519*7c478bd9Sstevel@tonic-gate 	 * Inline processing of data (to avoid additional procedure call).
1520*7c478bd9Sstevel@tonic-gate 	 * Rest is handled in timodwproc.
1521*7c478bd9Sstevel@tonic-gate 	 */
1522*7c478bd9Sstevel@tonic-gate 
1523*7c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
1524*7c478bd9Sstevel@tonic-gate 	case M_DATA:
1525*7c478bd9Sstevel@tonic-gate 		tp = (struct tim_tim *)q->q_ptr;
1526*7c478bd9Sstevel@tonic-gate 		ASSERT(tp);
1527*7c478bd9Sstevel@tonic-gate 		if (tp->tim_flags & CLTS) {
1528*7c478bd9Sstevel@tonic-gate 			mblk_t	*tmp;
1529*7c478bd9Sstevel@tonic-gate 
1530*7c478bd9Sstevel@tonic-gate 			if ((tmp = tim_filladdr(q, mp, B_FALSE)) == NULL) {
1531*7c478bd9Sstevel@tonic-gate 				(void) putq(q, mp);
1532*7c478bd9Sstevel@tonic-gate 				break;
1533*7c478bd9Sstevel@tonic-gate 			} else {
1534*7c478bd9Sstevel@tonic-gate 				mp = tmp;
1535*7c478bd9Sstevel@tonic-gate 			}
1536*7c478bd9Sstevel@tonic-gate 		}
1537*7c478bd9Sstevel@tonic-gate 		if (bcanputnext(q, mp->b_band))
1538*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
1539*7c478bd9Sstevel@tonic-gate 		else
1540*7c478bd9Sstevel@tonic-gate 			(void) putq(q, mp);
1541*7c478bd9Sstevel@tonic-gate 		break;
1542*7c478bd9Sstevel@tonic-gate 	case M_PROTO:
1543*7c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
1544*7c478bd9Sstevel@tonic-gate 		pptr = (union T_primitives *)mp->b_rptr;
1545*7c478bd9Sstevel@tonic-gate 		switch (pptr->type) {
1546*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
1547*7c478bd9Sstevel@tonic-gate 		case T_UNITDATA_REQ:
1548*7c478bd9Sstevel@tonic-gate 			tp = (struct tim_tim *)q->q_ptr;
1549*7c478bd9Sstevel@tonic-gate 			ASSERT(tp);
1550*7c478bd9Sstevel@tonic-gate 			if (tp->tim_flags & CLTS) {
1551*7c478bd9Sstevel@tonic-gate 				mblk_t	*tmp;
1552*7c478bd9Sstevel@tonic-gate 
1553*7c478bd9Sstevel@tonic-gate 				tmp = tim_filladdr(q, mp, B_FALSE);
1554*7c478bd9Sstevel@tonic-gate 				if (tmp == NULL) {
1555*7c478bd9Sstevel@tonic-gate 					(void) putq(q, mp);
1556*7c478bd9Sstevel@tonic-gate 					break;
1557*7c478bd9Sstevel@tonic-gate 				} else {
1558*7c478bd9Sstevel@tonic-gate 					mp = tmp;
1559*7c478bd9Sstevel@tonic-gate 				}
1560*7c478bd9Sstevel@tonic-gate 			}
1561*7c478bd9Sstevel@tonic-gate 			if (bcanputnext(q, mp->b_band))
1562*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
1563*7c478bd9Sstevel@tonic-gate 			else
1564*7c478bd9Sstevel@tonic-gate 				(void) putq(q, mp);
1565*7c478bd9Sstevel@tonic-gate 			break;
1566*7c478bd9Sstevel@tonic-gate 
1567*7c478bd9Sstevel@tonic-gate 		case T_DATA_REQ:
1568*7c478bd9Sstevel@tonic-gate 		case T_EXDATA_REQ:
1569*7c478bd9Sstevel@tonic-gate 			if (bcanputnext(q, mp->b_band))
1570*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
1571*7c478bd9Sstevel@tonic-gate 			else
1572*7c478bd9Sstevel@tonic-gate 				(void) putq(q, mp);
1573*7c478bd9Sstevel@tonic-gate 			break;
1574*7c478bd9Sstevel@tonic-gate 		default:
1575*7c478bd9Sstevel@tonic-gate 			(void) timodwproc(q, mp);
1576*7c478bd9Sstevel@tonic-gate 			break;
1577*7c478bd9Sstevel@tonic-gate 		}
1578*7c478bd9Sstevel@tonic-gate 		break;
1579*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
1580*7c478bd9Sstevel@tonic-gate 	default:
1581*7c478bd9Sstevel@tonic-gate 		(void) timodwproc(q, mp);
1582*7c478bd9Sstevel@tonic-gate 		break;
1583*7c478bd9Sstevel@tonic-gate 	}
1584*7c478bd9Sstevel@tonic-gate }
1585*7c478bd9Sstevel@tonic-gate /*
1586*7c478bd9Sstevel@tonic-gate  * timodwsrv -	Module write queue service procedure.
1587*7c478bd9Sstevel@tonic-gate  *		This is called when messages are placed on an empty queue,
1588*7c478bd9Sstevel@tonic-gate  *		when high priority messages are placed on the queue, and
1589*7c478bd9Sstevel@tonic-gate  *		when flow control restrictions subside.  This code used to
1590*7c478bd9Sstevel@tonic-gate  *		be included in a put procedure, but it was moved to a
1591*7c478bd9Sstevel@tonic-gate  *		service procedure because several points were added where
1592*7c478bd9Sstevel@tonic-gate  *		memory allocation could fail, and there is no reasonable
1593*7c478bd9Sstevel@tonic-gate  *		recovery mechanism from the put procedure.
1594*7c478bd9Sstevel@tonic-gate  */
1595*7c478bd9Sstevel@tonic-gate static void
1596*7c478bd9Sstevel@tonic-gate timodwsrv(queue_t *q)
1597*7c478bd9Sstevel@tonic-gate {
1598*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
1599*7c478bd9Sstevel@tonic-gate 
1600*7c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
1601*7c478bd9Sstevel@tonic-gate 	if (q->q_ptr == NULL)
1602*7c478bd9Sstevel@tonic-gate 	    return;
1603*7c478bd9Sstevel@tonic-gate 
1604*7c478bd9Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
1605*7c478bd9Sstevel@tonic-gate 		if (timodwproc(q, mp)) {
1606*7c478bd9Sstevel@tonic-gate 			/*
1607*7c478bd9Sstevel@tonic-gate 			 * timodwproc did a putbq - stop processing
1608*7c478bd9Sstevel@tonic-gate 			 * messages.
1609*7c478bd9Sstevel@tonic-gate 			 */
1610*7c478bd9Sstevel@tonic-gate 			return;
1611*7c478bd9Sstevel@tonic-gate 		}
1612*7c478bd9Sstevel@tonic-gate 	}
1613*7c478bd9Sstevel@tonic-gate }
1614*7c478bd9Sstevel@tonic-gate 
1615*7c478bd9Sstevel@tonic-gate /*
1616*7c478bd9Sstevel@tonic-gate  * Common routine to process write side messages
1617*7c478bd9Sstevel@tonic-gate  */
1618*7c478bd9Sstevel@tonic-gate 
1619*7c478bd9Sstevel@tonic-gate static int
1620*7c478bd9Sstevel@tonic-gate timodwproc(queue_t *q, mblk_t *mp)
1621*7c478bd9Sstevel@tonic-gate {
1622*7c478bd9Sstevel@tonic-gate 	union T_primitives *pptr;
1623*7c478bd9Sstevel@tonic-gate 	struct tim_tim *tp;
1624*7c478bd9Sstevel@tonic-gate 	mblk_t *tmp;
1625*7c478bd9Sstevel@tonic-gate 	struct iocblk *iocbp;
1626*7c478bd9Sstevel@tonic-gate 	int error;
1627*7c478bd9Sstevel@tonic-gate 
1628*7c478bd9Sstevel@tonic-gate 	tp = (struct tim_tim *)q->q_ptr;
1629*7c478bd9Sstevel@tonic-gate 
1630*7c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
1631*7c478bd9Sstevel@tonic-gate 	default:
1632*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
1633*7c478bd9Sstevel@tonic-gate 		break;
1634*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
1635*7c478bd9Sstevel@tonic-gate 
1636*7c478bd9Sstevel@tonic-gate 	case M_DATA:
1637*7c478bd9Sstevel@tonic-gate 		if (tp->tim_flags & CLTS) {
1638*7c478bd9Sstevel@tonic-gate 			if ((tmp = tim_filladdr(q, mp, B_TRUE)) == NULL) {
1639*7c478bd9Sstevel@tonic-gate 				return (1);
1640*7c478bd9Sstevel@tonic-gate 			} else {
1641*7c478bd9Sstevel@tonic-gate 				mp = tmp;
1642*7c478bd9Sstevel@tonic-gate 			}
1643*7c478bd9Sstevel@tonic-gate 		}
1644*7c478bd9Sstevel@tonic-gate 		if (!bcanputnext(q, mp->b_band)) {
1645*7c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
1646*7c478bd9Sstevel@tonic-gate 			return (1);
1647*7c478bd9Sstevel@tonic-gate 		}
1648*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
1649*7c478bd9Sstevel@tonic-gate 		break;
1650*7c478bd9Sstevel@tonic-gate 
1651*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
1652*7c478bd9Sstevel@tonic-gate 	case M_IOCTL:
1653*7c478bd9Sstevel@tonic-gate 
1654*7c478bd9Sstevel@tonic-gate 		iocbp = (struct iocblk *)mp->b_rptr;
1655*7c478bd9Sstevel@tonic-gate 		TILOG("timodwproc: Got M_IOCTL(%d)\n", iocbp->ioc_cmd);
1656*7c478bd9Sstevel@tonic-gate 
1657*7c478bd9Sstevel@tonic-gate 		ASSERT(MBLKL(mp) == sizeof (struct iocblk));
1658*7c478bd9Sstevel@tonic-gate 
1659*7c478bd9Sstevel@tonic-gate 		/*
1660*7c478bd9Sstevel@tonic-gate 		 * TPI requires we await response to a previously sent message
1661*7c478bd9Sstevel@tonic-gate 		 * before handling another, put it back on the head of the queue
1662*7c478bd9Sstevel@tonic-gate 		 * putbq() schedules the queue for service.
1663*7c478bd9Sstevel@tonic-gate 		 */
1664*7c478bd9Sstevel@tonic-gate 		if (tp->tim_flags & WAITIOCACK) {
1665*7c478bd9Sstevel@tonic-gate 			TILOG("timodwproc: putbq M_IOCTL(%d)\n",
1666*7c478bd9Sstevel@tonic-gate 			    iocbp->ioc_cmd);
1667*7c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
1668*7c478bd9Sstevel@tonic-gate 			return (1);
1669*7c478bd9Sstevel@tonic-gate 		}
1670*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
1671*7c478bd9Sstevel@tonic-gate 
1672*7c478bd9Sstevel@tonic-gate 		switch (iocbp->ioc_cmd) {
1673*7c478bd9Sstevel@tonic-gate 		default:
1674*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
1675*7c478bd9Sstevel@tonic-gate 			break;
1676*7c478bd9Sstevel@tonic-gate 
1677*7c478bd9Sstevel@tonic-gate 		case _I_GETPEERCRED:
1678*7c478bd9Sstevel@tonic-gate 			if ((tp->tim_flags & COTS) == 0) {
1679*7c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOTSUP);
1680*7c478bd9Sstevel@tonic-gate 			} else {
1681*7c478bd9Sstevel@tonic-gate 				mblk_t *cmp = mp->b_cont;
1682*7c478bd9Sstevel@tonic-gate 				k_peercred_t *kp = NULL;
1683*7c478bd9Sstevel@tonic-gate 
1684*7c478bd9Sstevel@tonic-gate 				mutex_enter(&tp->tim_mutex);
1685*7c478bd9Sstevel@tonic-gate 				if (cmp != NULL &&
1686*7c478bd9Sstevel@tonic-gate 				    iocbp->ioc_flag == IOC_NATIVE &&
1687*7c478bd9Sstevel@tonic-gate 				    (tp->tim_flags &
1688*7c478bd9Sstevel@tonic-gate 					(CONNWAIT|LOCORDREL|REMORDREL)) == 0 &&
1689*7c478bd9Sstevel@tonic-gate 				    tp->tim_peercred != NULL &&
1690*7c478bd9Sstevel@tonic-gate 				    DB_TYPE(cmp) == M_DATA &&
1691*7c478bd9Sstevel@tonic-gate 				    MBLKL(cmp) == sizeof (k_peercred_t)) {
1692*7c478bd9Sstevel@tonic-gate 					kp = (k_peercred_t *)cmp->b_rptr;
1693*7c478bd9Sstevel@tonic-gate 					crhold(kp->pc_cr = tp->tim_peercred);
1694*7c478bd9Sstevel@tonic-gate 					kp->pc_cpid = tp->tim_cpid;
1695*7c478bd9Sstevel@tonic-gate 				}
1696*7c478bd9Sstevel@tonic-gate 				mutex_exit(&tp->tim_mutex);
1697*7c478bd9Sstevel@tonic-gate 				if (kp != NULL)
1698*7c478bd9Sstevel@tonic-gate 					miocack(q, mp, sizeof (*kp), 0);
1699*7c478bd9Sstevel@tonic-gate 				else
1700*7c478bd9Sstevel@tonic-gate 					miocnak(q, mp, 0, ENOTCONN);
1701*7c478bd9Sstevel@tonic-gate 			}
1702*7c478bd9Sstevel@tonic-gate 			break;
1703*7c478bd9Sstevel@tonic-gate 		case TI_BIND:
1704*7c478bd9Sstevel@tonic-gate 		case TI_UNBIND:
1705*7c478bd9Sstevel@tonic-gate 		case TI_OPTMGMT:
1706*7c478bd9Sstevel@tonic-gate 		case TI_GETADDRS:
1707*7c478bd9Sstevel@tonic-gate 			TILOG("timodwproc: TI_{BIND|UNBIND|OPTMGMT|GETADDRS}"
1708*7c478bd9Sstevel@tonic-gate 			    "\n", 0);
1709*7c478bd9Sstevel@tonic-gate 
1710*7c478bd9Sstevel@tonic-gate 			/*
1711*7c478bd9Sstevel@tonic-gate 			 * We know that tim_send_ioctl_tpi_msg() is only
1712*7c478bd9Sstevel@tonic-gate 			 * going to examine the `type' field, so we only
1713*7c478bd9Sstevel@tonic-gate 			 * check that we can access that much data.
1714*7c478bd9Sstevel@tonic-gate 			 */
1715*7c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (t_scalar_t));
1716*7c478bd9Sstevel@tonic-gate 			if (error != 0) {
1717*7c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
1718*7c478bd9Sstevel@tonic-gate 				break;
1719*7c478bd9Sstevel@tonic-gate 			}
1720*7c478bd9Sstevel@tonic-gate 			tim_send_ioctl_tpi_msg(q, mp, tp, iocbp);
1721*7c478bd9Sstevel@tonic-gate 			break;
1722*7c478bd9Sstevel@tonic-gate 
1723*7c478bd9Sstevel@tonic-gate 		case TI_GETINFO:
1724*7c478bd9Sstevel@tonic-gate 			TILOG("timodwproc: TI_GETINFO\n", 0);
1725*7c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (struct T_info_req));
1726*7c478bd9Sstevel@tonic-gate 			if (error != 0) {
1727*7c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
1728*7c478bd9Sstevel@tonic-gate 				break;
1729*7c478bd9Sstevel@tonic-gate 			}
1730*7c478bd9Sstevel@tonic-gate 			tp->tim_flags |= WAIT_IOCINFOACK;
1731*7c478bd9Sstevel@tonic-gate 			tim_send_ioctl_tpi_msg(q, mp, tp, iocbp);
1732*7c478bd9Sstevel@tonic-gate 			break;
1733*7c478bd9Sstevel@tonic-gate 
1734*7c478bd9Sstevel@tonic-gate 		case TI_SYNC: {
1735*7c478bd9Sstevel@tonic-gate 			mblk_t *tsr_mp;
1736*7c478bd9Sstevel@tonic-gate 			struct ti_sync_req *tsr;
1737*7c478bd9Sstevel@tonic-gate 			uint32_t tsr_flags;
1738*7c478bd9Sstevel@tonic-gate 
1739*7c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (struct ti_sync_req));
1740*7c478bd9Sstevel@tonic-gate 			if (error != 0) {
1741*7c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
1742*7c478bd9Sstevel@tonic-gate 				break;
1743*7c478bd9Sstevel@tonic-gate 			}
1744*7c478bd9Sstevel@tonic-gate 
1745*7c478bd9Sstevel@tonic-gate 			tsr_mp = mp->b_cont;
1746*7c478bd9Sstevel@tonic-gate 			tsr = (struct ti_sync_req *)tsr_mp->b_rptr;
1747*7c478bd9Sstevel@tonic-gate 			TILOG("timodwproc: TI_SYNC(%x)\n", tsr->tsr_flags);
1748*7c478bd9Sstevel@tonic-gate 
1749*7c478bd9Sstevel@tonic-gate 			/*
1750*7c478bd9Sstevel@tonic-gate 			 * Save out the value of tsr_flags, in case we
1751*7c478bd9Sstevel@tonic-gate 			 * reallocb() tsr_mp (below).
1752*7c478bd9Sstevel@tonic-gate 			 */
1753*7c478bd9Sstevel@tonic-gate 			tsr_flags = tsr->tsr_flags;
1754*7c478bd9Sstevel@tonic-gate 			if ((tsr_flags & TSRF_INFO_REQ) == 0) {
1755*7c478bd9Sstevel@tonic-gate 				mblk_t *ack_mp = reallocb(tsr_mp,
1756*7c478bd9Sstevel@tonic-gate 				    sizeof (struct ti_sync_ack), 0);
1757*7c478bd9Sstevel@tonic-gate 
1758*7c478bd9Sstevel@tonic-gate 				/* Can reply immediately. */
1759*7c478bd9Sstevel@tonic-gate 				mp->b_cont = NULL;
1760*7c478bd9Sstevel@tonic-gate 				if (ack_mp == NULL) {
1761*7c478bd9Sstevel@tonic-gate 					tilog("timodwproc: allocb failed no "
1762*7c478bd9Sstevel@tonic-gate 					    "recovery attempt\n", 0);
1763*7c478bd9Sstevel@tonic-gate 					freemsg(tsr_mp);
1764*7c478bd9Sstevel@tonic-gate 					miocnak(q, mp, 0, ENOMEM);
1765*7c478bd9Sstevel@tonic-gate 				} else {
1766*7c478bd9Sstevel@tonic-gate 					tim_answer_ti_sync(q, mp, tp,
1767*7c478bd9Sstevel@tonic-gate 					    ack_mp, tsr_flags);
1768*7c478bd9Sstevel@tonic-gate 				}
1769*7c478bd9Sstevel@tonic-gate 				break;
1770*7c478bd9Sstevel@tonic-gate 			}
1771*7c478bd9Sstevel@tonic-gate 
1772*7c478bd9Sstevel@tonic-gate 			/*
1773*7c478bd9Sstevel@tonic-gate 			 * This code is retained for compatibility with
1774*7c478bd9Sstevel@tonic-gate 			 * old statically linked applications. New code
1775*7c478bd9Sstevel@tonic-gate 			 * should use TI_CAPABILITY for all TPI
1776*7c478bd9Sstevel@tonic-gate 			 * information and should not use TSRF_INFO_REQ
1777*7c478bd9Sstevel@tonic-gate 			 * flag.
1778*7c478bd9Sstevel@tonic-gate 			 *
1779*7c478bd9Sstevel@tonic-gate 			 * defer processsing necessary to rput procedure
1780*7c478bd9Sstevel@tonic-gate 			 * as we need to get information from transport
1781*7c478bd9Sstevel@tonic-gate 			 * driver. Set flags that will tell the read
1782*7c478bd9Sstevel@tonic-gate 			 * side the work needed on this request.
1783*7c478bd9Sstevel@tonic-gate 			 */
1784*7c478bd9Sstevel@tonic-gate 
1785*7c478bd9Sstevel@tonic-gate 			if (tsr_flags & TSRF_IS_EXP_IN_RCVBUF)
1786*7c478bd9Sstevel@tonic-gate 				tp->tim_flags |= PEEK_RDQ_EXPIND;
1787*7c478bd9Sstevel@tonic-gate 
1788*7c478bd9Sstevel@tonic-gate 			/*
1789*7c478bd9Sstevel@tonic-gate 			 * Convert message to a T_INFO_REQ message; relies
1790*7c478bd9Sstevel@tonic-gate 			 * on sizeof (struct ti_sync_req) >= sizeof (struct
1791*7c478bd9Sstevel@tonic-gate 			 * T_info_req)).
1792*7c478bd9Sstevel@tonic-gate 			 */
1793*7c478bd9Sstevel@tonic-gate 			ASSERT(MBLKL(tsr_mp) >= sizeof (struct T_info_req));
1794*7c478bd9Sstevel@tonic-gate 
1795*7c478bd9Sstevel@tonic-gate 			((struct T_info_req *)tsr_mp->b_rptr)->PRIM_type =
1796*7c478bd9Sstevel@tonic-gate 			    T_INFO_REQ;
1797*7c478bd9Sstevel@tonic-gate 			tsr_mp->b_wptr = tsr_mp->b_rptr +
1798*7c478bd9Sstevel@tonic-gate 			    sizeof (struct T_info_req);
1799*7c478bd9Sstevel@tonic-gate 			tp->tim_flags |= WAIT_IOCINFOACK;
1800*7c478bd9Sstevel@tonic-gate 			tim_send_ioctl_tpi_msg(q, mp, tp, iocbp);
1801*7c478bd9Sstevel@tonic-gate 		}
1802*7c478bd9Sstevel@tonic-gate 		break;
1803*7c478bd9Sstevel@tonic-gate 
1804*7c478bd9Sstevel@tonic-gate 		case TI_CAPABILITY: {
1805*7c478bd9Sstevel@tonic-gate 			mblk_t *tcsr_mp;
1806*7c478bd9Sstevel@tonic-gate 			struct T_capability_req *tcr;
1807*7c478bd9Sstevel@tonic-gate 
1808*7c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (*tcr));
1809*7c478bd9Sstevel@tonic-gate 			if (error != 0) {
1810*7c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
1811*7c478bd9Sstevel@tonic-gate 				break;
1812*7c478bd9Sstevel@tonic-gate 			}
1813*7c478bd9Sstevel@tonic-gate 
1814*7c478bd9Sstevel@tonic-gate 			tcsr_mp = mp->b_cont;
1815*7c478bd9Sstevel@tonic-gate 			tcr = (struct T_capability_req *)tcsr_mp->b_rptr;
1816*7c478bd9Sstevel@tonic-gate 			TILOG("timodwproc: TI_CAPABILITY(CAP_bits1 = %x)\n",
1817*7c478bd9Sstevel@tonic-gate 			    tcr->CAP_bits1);
1818*7c478bd9Sstevel@tonic-gate 
1819*7c478bd9Sstevel@tonic-gate 			if (tcr->PRIM_type != T_CAPABILITY_REQ) {
1820*7c478bd9Sstevel@tonic-gate 				TILOG("timodwproc: invalid msg type %d\n",
1821*7c478bd9Sstevel@tonic-gate 				    tcr->PRIM_type);
1822*7c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, EPROTO);
1823*7c478bd9Sstevel@tonic-gate 				break;
1824*7c478bd9Sstevel@tonic-gate 			}
1825*7c478bd9Sstevel@tonic-gate 
1826*7c478bd9Sstevel@tonic-gate 			switch (tp->tim_provinfo->tpi_capability) {
1827*7c478bd9Sstevel@tonic-gate 			case PI_YES:
1828*7c478bd9Sstevel@tonic-gate 				/* Just send T_CAPABILITY_REQ down */
1829*7c478bd9Sstevel@tonic-gate 				tim_send_ioctl_tpi_msg(q, mp, tp, iocbp);
1830*7c478bd9Sstevel@tonic-gate 				break;
1831*7c478bd9Sstevel@tonic-gate 
1832*7c478bd9Sstevel@tonic-gate 			case PI_DONTKNOW:
1833*7c478bd9Sstevel@tonic-gate 				/*
1834*7c478bd9Sstevel@tonic-gate 				 * It is unknown yet whether transport provides
1835*7c478bd9Sstevel@tonic-gate 				 * T_CAPABILITY_REQ or not. Send message down
1836*7c478bd9Sstevel@tonic-gate 				 * and wait for reply.
1837*7c478bd9Sstevel@tonic-gate 				 */
1838*7c478bd9Sstevel@tonic-gate 
1839*7c478bd9Sstevel@tonic-gate 				ASSERT(tp->tim_tcap_timoutid == 0);
1840*7c478bd9Sstevel@tonic-gate 				if ((tcr->CAP_bits1 & TC1_INFO) == 0) {
1841*7c478bd9Sstevel@tonic-gate 					tp->tim_flags |= TI_CAP_RECVD;
1842*7c478bd9Sstevel@tonic-gate 				} else {
1843*7c478bd9Sstevel@tonic-gate 					tp->tim_flags |= (TI_CAP_RECVD |
1844*7c478bd9Sstevel@tonic-gate 					    CAP_WANTS_INFO);
1845*7c478bd9Sstevel@tonic-gate 				}
1846*7c478bd9Sstevel@tonic-gate 
1847*7c478bd9Sstevel@tonic-gate 				tp->tim_tcap_timoutid = qtimeout(q,
1848*7c478bd9Sstevel@tonic-gate 				    tim_tcap_timer, q, tim_tcap_wait * hz);
1849*7c478bd9Sstevel@tonic-gate 				tim_send_ioctl_tpi_msg(q, mp, tp, iocbp);
1850*7c478bd9Sstevel@tonic-gate 				break;
1851*7c478bd9Sstevel@tonic-gate 
1852*7c478bd9Sstevel@tonic-gate 			case PI_NO:
1853*7c478bd9Sstevel@tonic-gate 				/*
1854*7c478bd9Sstevel@tonic-gate 				 * Transport doesn't support T_CAPABILITY_REQ.
1855*7c478bd9Sstevel@tonic-gate 				 * Either reply immediately or send T_INFO_REQ
1856*7c478bd9Sstevel@tonic-gate 				 * if needed.
1857*7c478bd9Sstevel@tonic-gate 				 */
1858*7c478bd9Sstevel@tonic-gate 				if ((tcr->CAP_bits1 & TC1_INFO) != 0) {
1859*7c478bd9Sstevel@tonic-gate 					tp->tim_flags |= (TI_CAP_RECVD |
1860*7c478bd9Sstevel@tonic-gate 					    CAP_WANTS_INFO | WAIT_IOCINFOACK);
1861*7c478bd9Sstevel@tonic-gate 					TILOG("timodwproc: sending down "
1862*7c478bd9Sstevel@tonic-gate 					    "T_INFO_REQ, flags = %x\n",
1863*7c478bd9Sstevel@tonic-gate 					    tp->tim_flags);
1864*7c478bd9Sstevel@tonic-gate 
1865*7c478bd9Sstevel@tonic-gate 				/*
1866*7c478bd9Sstevel@tonic-gate 				 * Generate T_INFO_REQ message and send
1867*7c478bd9Sstevel@tonic-gate 				 * it down
1868*7c478bd9Sstevel@tonic-gate 				 */
1869*7c478bd9Sstevel@tonic-gate 					((struct T_info_req *)tcsr_mp->b_rptr)->
1870*7c478bd9Sstevel@tonic-gate 					    PRIM_type = T_INFO_REQ;
1871*7c478bd9Sstevel@tonic-gate 					tcsr_mp->b_wptr = tcsr_mp->b_rptr +
1872*7c478bd9Sstevel@tonic-gate 					    sizeof (struct T_info_req);
1873*7c478bd9Sstevel@tonic-gate 					tim_send_ioctl_tpi_msg(q, mp, tp,
1874*7c478bd9Sstevel@tonic-gate 					    iocbp);
1875*7c478bd9Sstevel@tonic-gate 					break;
1876*7c478bd9Sstevel@tonic-gate 				}
1877*7c478bd9Sstevel@tonic-gate 
1878*7c478bd9Sstevel@tonic-gate 
1879*7c478bd9Sstevel@tonic-gate 				/*
1880*7c478bd9Sstevel@tonic-gate 				 * Can reply immediately. Just send back
1881*7c478bd9Sstevel@tonic-gate 				 * T_CAPABILITY_ACK with CAP_bits1 set to 0.
1882*7c478bd9Sstevel@tonic-gate 				 */
1883*7c478bd9Sstevel@tonic-gate 				mp->b_cont = tcsr_mp = tpi_ack_alloc(mp->b_cont,
1884*7c478bd9Sstevel@tonic-gate 				    sizeof (struct T_capability_ack), M_PCPROTO,
1885*7c478bd9Sstevel@tonic-gate 				    T_CAPABILITY_ACK);
1886*7c478bd9Sstevel@tonic-gate 
1887*7c478bd9Sstevel@tonic-gate 				if (tcsr_mp == NULL) {
1888*7c478bd9Sstevel@tonic-gate 					tilog("timodwproc: allocb failed no "
1889*7c478bd9Sstevel@tonic-gate 					    "recovery attempt\n", 0);
1890*7c478bd9Sstevel@tonic-gate 					miocnak(q, mp, 0, ENOMEM);
1891*7c478bd9Sstevel@tonic-gate 					break;
1892*7c478bd9Sstevel@tonic-gate 				}
1893*7c478bd9Sstevel@tonic-gate 
1894*7c478bd9Sstevel@tonic-gate 				tp->tim_flags &= ~(WAITIOCACK | TI_CAP_RECVD |
1895*7c478bd9Sstevel@tonic-gate 				    WAIT_IOCINFOACK | CAP_WANTS_INFO);
1896*7c478bd9Sstevel@tonic-gate 				((struct T_capability_ack *)
1897*7c478bd9Sstevel@tonic-gate 				    tcsr_mp->b_rptr)->CAP_bits1 = 0;
1898*7c478bd9Sstevel@tonic-gate 				tim_ioctl_send_reply(q, mp, tcsr_mp);
1899*7c478bd9Sstevel@tonic-gate 
1900*7c478bd9Sstevel@tonic-gate 				/*
1901*7c478bd9Sstevel@tonic-gate 				 * It could happen when timod is awaiting ack
1902*7c478bd9Sstevel@tonic-gate 				 * for TI_GETPEERNAME/TI_GETMYNAME.
1903*7c478bd9Sstevel@tonic-gate 				 */
1904*7c478bd9Sstevel@tonic-gate 				if (tp->tim_iocsave != NULL) {
1905*7c478bd9Sstevel@tonic-gate 					freemsg(tp->tim_iocsave);
1906*7c478bd9Sstevel@tonic-gate 					tp->tim_iocsave = NULL;
1907*7c478bd9Sstevel@tonic-gate 					tp->tim_saved_prim = -1;
1908*7c478bd9Sstevel@tonic-gate 				}
1909*7c478bd9Sstevel@tonic-gate 				break;
1910*7c478bd9Sstevel@tonic-gate 
1911*7c478bd9Sstevel@tonic-gate 			default:
1912*7c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC,
1913*7c478bd9Sstevel@tonic-gate 				    "timodwproc: unknown tpi_capability value "
1914*7c478bd9Sstevel@tonic-gate 				    "%d\n", tp->tim_provinfo->tpi_capability);
1915*7c478bd9Sstevel@tonic-gate 				break;
1916*7c478bd9Sstevel@tonic-gate 			}
1917*7c478bd9Sstevel@tonic-gate 		}
1918*7c478bd9Sstevel@tonic-gate 		break;
1919*7c478bd9Sstevel@tonic-gate 
1920*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
1921*7c478bd9Sstevel@tonic-gate 		case TI_GETMYNAME:
1922*7c478bd9Sstevel@tonic-gate 
1923*7c478bd9Sstevel@tonic-gate 			tilog("timodwproc: Got TI_GETMYNAME\n", 0);
1924*7c478bd9Sstevel@tonic-gate 
1925*7c478bd9Sstevel@tonic-gate 			if (tp->tim_provinfo->tpi_myname == PI_YES) {
1926*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
1927*7c478bd9Sstevel@tonic-gate 				break;
1928*7c478bd9Sstevel@tonic-gate 			}
1929*7c478bd9Sstevel@tonic-gate 			goto getname;
1930*7c478bd9Sstevel@tonic-gate 
1931*7c478bd9Sstevel@tonic-gate 		case TI_GETPEERNAME:
1932*7c478bd9Sstevel@tonic-gate 
1933*7c478bd9Sstevel@tonic-gate 			tilog("timodwproc: Got TI_GETPEERNAME\n", 0);
1934*7c478bd9Sstevel@tonic-gate 
1935*7c478bd9Sstevel@tonic-gate 			if (tp->tim_provinfo->tpi_peername == PI_YES) {
1936*7c478bd9Sstevel@tonic-gate 				putnext(q, mp);
1937*7c478bd9Sstevel@tonic-gate 				break;
1938*7c478bd9Sstevel@tonic-gate 			}
1939*7c478bd9Sstevel@tonic-gate getname:
1940*7c478bd9Sstevel@tonic-gate 			if ((tmp = copymsg(mp)) == NULL) {
1941*7c478bd9Sstevel@tonic-gate 				tim_recover(q, mp, msgsize(mp));
1942*7c478bd9Sstevel@tonic-gate 				return (1);
1943*7c478bd9Sstevel@tonic-gate 			}
1944*7c478bd9Sstevel@tonic-gate 			/*
1945*7c478bd9Sstevel@tonic-gate 			 * tim_iocsave may be non-NULL when timod is awaiting
1946*7c478bd9Sstevel@tonic-gate 			 * ack for another TI_GETPEERNAME/TI_GETMYNAME.
1947*7c478bd9Sstevel@tonic-gate 			 */
1948*7c478bd9Sstevel@tonic-gate 			freemsg(tp->tim_iocsave);
1949*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave = mp;
1950*7c478bd9Sstevel@tonic-gate 			tp->tim_saved_prim = -1;
1951*7c478bd9Sstevel@tonic-gate 			putnext(q, tmp);
1952*7c478bd9Sstevel@tonic-gate 			break;
1953*7c478bd9Sstevel@tonic-gate 			}
1954*7c478bd9Sstevel@tonic-gate 		break;
1955*7c478bd9Sstevel@tonic-gate 
1956*7c478bd9Sstevel@tonic-gate 	case M_IOCDATA:
1957*7c478bd9Sstevel@tonic-gate 
1958*7c478bd9Sstevel@tonic-gate 		if (tp->tim_flags & NAMEPROC) {
1959*7c478bd9Sstevel@tonic-gate 			if (ti_doname(q, mp) != DONAME_CONT) {
1960*7c478bd9Sstevel@tonic-gate 				tp->tim_flags &= ~NAMEPROC;
1961*7c478bd9Sstevel@tonic-gate 			}
1962*7c478bd9Sstevel@tonic-gate 		} else
1963*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
1964*7c478bd9Sstevel@tonic-gate 		break;
1965*7c478bd9Sstevel@tonic-gate 
1966*7c478bd9Sstevel@tonic-gate 	case M_PROTO:
1967*7c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
1968*7c478bd9Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (t_scalar_t)) {
1969*7c478bd9Sstevel@tonic-gate 			merror(q, mp, EPROTO);
1970*7c478bd9Sstevel@tonic-gate 			return (1);
1971*7c478bd9Sstevel@tonic-gate 		}
1972*7c478bd9Sstevel@tonic-gate 
1973*7c478bd9Sstevel@tonic-gate 		pptr = (union T_primitives *)mp->b_rptr;
1974*7c478bd9Sstevel@tonic-gate 		switch (pptr->type) {
1975*7c478bd9Sstevel@tonic-gate 		default:
1976*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
1977*7c478bd9Sstevel@tonic-gate 			break;
1978*7c478bd9Sstevel@tonic-gate 
1979*7c478bd9Sstevel@tonic-gate 		case T_EXDATA_REQ:
1980*7c478bd9Sstevel@tonic-gate 		case T_DATA_REQ:
1981*7c478bd9Sstevel@tonic-gate 			if (pptr->type == T_EXDATA_REQ)
1982*7c478bd9Sstevel@tonic-gate 				tilog("timodwproc: Got T_EXDATA_REQ\n", 0);
1983*7c478bd9Sstevel@tonic-gate 
1984*7c478bd9Sstevel@tonic-gate 		if (!bcanputnext(q, mp->b_band)) {
1985*7c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
1986*7c478bd9Sstevel@tonic-gate 			return (1);
1987*7c478bd9Sstevel@tonic-gate 		}
1988*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
1989*7c478bd9Sstevel@tonic-gate 		break;
1990*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
1991*7c478bd9Sstevel@tonic-gate 
1992*7c478bd9Sstevel@tonic-gate 		case T_UNITDATA_REQ:
1993*7c478bd9Sstevel@tonic-gate 			if (tp->tim_flags & CLTS) {
1994*7c478bd9Sstevel@tonic-gate 				tmp = tim_filladdr(q, mp, B_TRUE);
1995*7c478bd9Sstevel@tonic-gate 				if (tmp == NULL) {
1996*7c478bd9Sstevel@tonic-gate 					return (1);
1997*7c478bd9Sstevel@tonic-gate 				} else {
1998*7c478bd9Sstevel@tonic-gate 					mp = tmp;
1999*7c478bd9Sstevel@tonic-gate 				}
2000*7c478bd9Sstevel@tonic-gate 			}
2001*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
2002*7c478bd9Sstevel@tonic-gate 			if (audit_active)
2003*7c478bd9Sstevel@tonic-gate 				audit_sock(T_UNITDATA_REQ, q, mp, TIMOD_ID);
2004*7c478bd9Sstevel@tonic-gate #endif
2005*7c478bd9Sstevel@tonic-gate 		if (!bcanputnext(q, mp->b_band)) {
2006*7c478bd9Sstevel@tonic-gate 				(void) putbq(q, mp);
2007*7c478bd9Sstevel@tonic-gate 				return (1);
2008*7c478bd9Sstevel@tonic-gate 			}
2009*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
2010*7c478bd9Sstevel@tonic-gate 			break;
2011*7c478bd9Sstevel@tonic-gate 
2012*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
2013*7c478bd9Sstevel@tonic-gate 		case T_CONN_REQ: {
2014*7c478bd9Sstevel@tonic-gate 			struct T_conn_req *reqp = (struct T_conn_req *)
2015*7c478bd9Sstevel@tonic-gate 			    mp->b_rptr;
2016*7c478bd9Sstevel@tonic-gate 			void *p;
2017*7c478bd9Sstevel@tonic-gate 
2018*7c478bd9Sstevel@tonic-gate 			tilog("timodwproc: Got T_CONN_REQ\n", 0);
2019*7c478bd9Sstevel@tonic-gate 
2020*7c478bd9Sstevel@tonic-gate 			if (MBLKL(mp) < sizeof (struct T_conn_req)) {
2021*7c478bd9Sstevel@tonic-gate 				merror(q, mp, EPROTO);
2022*7c478bd9Sstevel@tonic-gate 				return (1);
2023*7c478bd9Sstevel@tonic-gate 			}
2024*7c478bd9Sstevel@tonic-gate 
2025*7c478bd9Sstevel@tonic-gate 			if (tp->tim_flags & DO_PEERNAME) {
2026*7c478bd9Sstevel@tonic-gate 				if (!MBLKIN(mp, reqp->DEST_offset,
2027*7c478bd9Sstevel@tonic-gate 				    reqp->DEST_length)) {
2028*7c478bd9Sstevel@tonic-gate 					merror(q, mp, EPROTO);
2029*7c478bd9Sstevel@tonic-gate 					return (1);
2030*7c478bd9Sstevel@tonic-gate 				}
2031*7c478bd9Sstevel@tonic-gate 				ASSERT(reqp->DEST_length >= 0);
2032*7c478bd9Sstevel@tonic-gate 				mutex_enter(&tp->tim_mutex);
2033*7c478bd9Sstevel@tonic-gate 				if (reqp->DEST_length > tp->tim_peermaxlen) {
2034*7c478bd9Sstevel@tonic-gate 					p = kmem_alloc(reqp->DEST_length,
2035*7c478bd9Sstevel@tonic-gate 					    KM_NOSLEEP);
2036*7c478bd9Sstevel@tonic-gate 					if (p == NULL) {
2037*7c478bd9Sstevel@tonic-gate 						mutex_exit(&tp->tim_mutex);
2038*7c478bd9Sstevel@tonic-gate 						tilog("timodwproc: kmem_alloc "
2039*7c478bd9Sstevel@tonic-gate 						    "failed, attempting "
2040*7c478bd9Sstevel@tonic-gate 						    "recovery\n", 0);
2041*7c478bd9Sstevel@tonic-gate 						tim_recover(q, mp,
2042*7c478bd9Sstevel@tonic-gate 						    reqp->DEST_length);
2043*7c478bd9Sstevel@tonic-gate 						return (1);
2044*7c478bd9Sstevel@tonic-gate 					}
2045*7c478bd9Sstevel@tonic-gate 					if (tp->tim_peermaxlen)
2046*7c478bd9Sstevel@tonic-gate 						kmem_free(tp->tim_peername,
2047*7c478bd9Sstevel@tonic-gate 						    tp->tim_peermaxlen);
2048*7c478bd9Sstevel@tonic-gate 					tp->tim_peername = p;
2049*7c478bd9Sstevel@tonic-gate 					tp->tim_peermaxlen = reqp->DEST_length;
2050*7c478bd9Sstevel@tonic-gate 				}
2051*7c478bd9Sstevel@tonic-gate 				tp->tim_peerlen = reqp->DEST_length;
2052*7c478bd9Sstevel@tonic-gate 				p = mp->b_rptr + reqp->DEST_offset;
2053*7c478bd9Sstevel@tonic-gate 				bcopy(p, tp->tim_peername, tp->tim_peerlen);
2054*7c478bd9Sstevel@tonic-gate 				mutex_exit(&tp->tim_mutex);
2055*7c478bd9Sstevel@tonic-gate 			}
2056*7c478bd9Sstevel@tonic-gate 			if (tp->tim_flags & COTS)
2057*7c478bd9Sstevel@tonic-gate 				tp->tim_flags |= CONNWAIT;
2058*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
2059*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
2060*7c478bd9Sstevel@tonic-gate 			if (audit_active)
2061*7c478bd9Sstevel@tonic-gate 				audit_sock(T_CONN_REQ, q, mp, TIMOD_ID);
2062*7c478bd9Sstevel@tonic-gate #endif
2063*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
2064*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
2065*7c478bd9Sstevel@tonic-gate 		break;
2066*7c478bd9Sstevel@tonic-gate 	    }
2067*7c478bd9Sstevel@tonic-gate 
2068*7c478bd9Sstevel@tonic-gate 		case O_T_CONN_RES:
2069*7c478bd9Sstevel@tonic-gate 		case T_CONN_RES: {
2070*7c478bd9Sstevel@tonic-gate 			struct T_conn_res *resp;
2071*7c478bd9Sstevel@tonic-gate 			struct T_conn_ind *indp;
2072*7c478bd9Sstevel@tonic-gate 			mblk_t *pmp = NULL;
2073*7c478bd9Sstevel@tonic-gate 			mblk_t *nbp;
2074*7c478bd9Sstevel@tonic-gate 
2075*7c478bd9Sstevel@tonic-gate 			if (MBLKL(mp) < sizeof (struct T_conn_res) ||
2076*7c478bd9Sstevel@tonic-gate 			    (tp->tim_flags & WAITIOCACK)) {
2077*7c478bd9Sstevel@tonic-gate 				merror(q, mp, EPROTO);
2078*7c478bd9Sstevel@tonic-gate 				return (1);
2079*7c478bd9Sstevel@tonic-gate 			}
2080*7c478bd9Sstevel@tonic-gate 
2081*7c478bd9Sstevel@tonic-gate 			resp = (struct T_conn_res *)mp->b_rptr;
2082*7c478bd9Sstevel@tonic-gate 			for (tmp = tp->tim_consave; tmp != NULL;
2083*7c478bd9Sstevel@tonic-gate 			    tmp = tmp->b_next) {
2084*7c478bd9Sstevel@tonic-gate 				indp = (struct T_conn_ind *)tmp->b_rptr;
2085*7c478bd9Sstevel@tonic-gate 				if (indp->SEQ_number == resp->SEQ_number)
2086*7c478bd9Sstevel@tonic-gate 					break;
2087*7c478bd9Sstevel@tonic-gate 				pmp = tmp;
2088*7c478bd9Sstevel@tonic-gate 			}
2089*7c478bd9Sstevel@tonic-gate 			if (tmp == NULL)
2090*7c478bd9Sstevel@tonic-gate 				goto cresout;
2091*7c478bd9Sstevel@tonic-gate 
2092*7c478bd9Sstevel@tonic-gate 			if ((nbp = dupb(mp)) == NULL &&
2093*7c478bd9Sstevel@tonic-gate 			    (nbp = copyb(mp)) == NULL) {
2094*7c478bd9Sstevel@tonic-gate 				tim_recover(q, mp, msgsize(mp));
2095*7c478bd9Sstevel@tonic-gate 				return (1);
2096*7c478bd9Sstevel@tonic-gate 			}
2097*7c478bd9Sstevel@tonic-gate 
2098*7c478bd9Sstevel@tonic-gate 			if (pmp != NULL)
2099*7c478bd9Sstevel@tonic-gate 				pmp->b_next = tmp->b_next;
2100*7c478bd9Sstevel@tonic-gate 			else
2101*7c478bd9Sstevel@tonic-gate 				tp->tim_consave = tmp->b_next;
2102*7c478bd9Sstevel@tonic-gate 			tmp->b_next = NULL;
2103*7c478bd9Sstevel@tonic-gate 
2104*7c478bd9Sstevel@tonic-gate 			/*
2105*7c478bd9Sstevel@tonic-gate 			 * Construct a list with:
2106*7c478bd9Sstevel@tonic-gate 			 *	nbp - copy of user's original request
2107*7c478bd9Sstevel@tonic-gate 			 *	tmp - the extracted T_conn_ind
2108*7c478bd9Sstevel@tonic-gate 			 */
2109*7c478bd9Sstevel@tonic-gate 			nbp->b_cont = tmp;
2110*7c478bd9Sstevel@tonic-gate 			/*
2111*7c478bd9Sstevel@tonic-gate 			 * tim_iocsave may be non-NULL when timod is awaiting
2112*7c478bd9Sstevel@tonic-gate 			 * ack for TI_GETPEERNAME/TI_GETMYNAME.
2113*7c478bd9Sstevel@tonic-gate 			 */
2114*7c478bd9Sstevel@tonic-gate 			freemsg(tp->tim_iocsave);
2115*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave = nbp;
2116*7c478bd9Sstevel@tonic-gate 			tp->tim_saved_prim = pptr->type;
2117*7c478bd9Sstevel@tonic-gate 			tp->tim_flags |= WAIT_CONNRESACK | WAITIOCACK;
2118*7c478bd9Sstevel@tonic-gate 
2119*7c478bd9Sstevel@tonic-gate 		cresout:
2120*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
2121*7c478bd9Sstevel@tonic-gate 			break;
2122*7c478bd9Sstevel@tonic-gate 		}
2123*7c478bd9Sstevel@tonic-gate 
2124*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
2125*7c478bd9Sstevel@tonic-gate 		case T_DISCON_REQ: {
2126*7c478bd9Sstevel@tonic-gate 			struct T_discon_req *disp;
2127*7c478bd9Sstevel@tonic-gate 			struct T_conn_ind *conp;
2128*7c478bd9Sstevel@tonic-gate 			mblk_t *pmp = NULL;
2129*7c478bd9Sstevel@tonic-gate 
2130*7c478bd9Sstevel@tonic-gate 			if (MBLKL(mp) < sizeof (struct T_discon_req)) {
2131*7c478bd9Sstevel@tonic-gate 				merror(q, mp, EPROTO);
2132*7c478bd9Sstevel@tonic-gate 				return (1);
2133*7c478bd9Sstevel@tonic-gate 			}
2134*7c478bd9Sstevel@tonic-gate 
2135*7c478bd9Sstevel@tonic-gate 			disp = (struct T_discon_req *)mp->b_rptr;
2136*7c478bd9Sstevel@tonic-gate 			tp->tim_flags &= ~(CONNWAIT|LOCORDREL|REMORDREL);
2137*7c478bd9Sstevel@tonic-gate 			tim_clear_peer(tp);
2138*7c478bd9Sstevel@tonic-gate 
2139*7c478bd9Sstevel@tonic-gate 			/*
2140*7c478bd9Sstevel@tonic-gate 			 * If we are already connected, there won't
2141*7c478bd9Sstevel@tonic-gate 			 * be any messages on tim_consave.
2142*7c478bd9Sstevel@tonic-gate 			 */
2143*7c478bd9Sstevel@tonic-gate 			for (tmp = tp->tim_consave; tmp; tmp = tmp->b_next) {
2144*7c478bd9Sstevel@tonic-gate 				conp = (struct T_conn_ind *)tmp->b_rptr;
2145*7c478bd9Sstevel@tonic-gate 				if (conp->SEQ_number == disp->SEQ_number)
2146*7c478bd9Sstevel@tonic-gate 					break;
2147*7c478bd9Sstevel@tonic-gate 				pmp = tmp;
2148*7c478bd9Sstevel@tonic-gate 			}
2149*7c478bd9Sstevel@tonic-gate 			if (tmp) {
2150*7c478bd9Sstevel@tonic-gate 				if (pmp)
2151*7c478bd9Sstevel@tonic-gate 					pmp->b_next = tmp->b_next;
2152*7c478bd9Sstevel@tonic-gate 				else
2153*7c478bd9Sstevel@tonic-gate 					tp->tim_consave = tmp->b_next;
2154*7c478bd9Sstevel@tonic-gate 				tmp->b_next = NULL;
2155*7c478bd9Sstevel@tonic-gate 				freemsg(tmp);
2156*7c478bd9Sstevel@tonic-gate 			}
2157*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
2158*7c478bd9Sstevel@tonic-gate 			break;
2159*7c478bd9Sstevel@tonic-gate 		}
2160*7c478bd9Sstevel@tonic-gate 
2161*7c478bd9Sstevel@tonic-gate 		case T_ORDREL_REQ:
2162*7c478bd9Sstevel@tonic-gate 			if (tp->tim_flags & REMORDREL) {
2163*7c478bd9Sstevel@tonic-gate 				tp->tim_flags &= ~(LOCORDREL|REMORDREL);
2164*7c478bd9Sstevel@tonic-gate 				tim_clear_peer(tp);
2165*7c478bd9Sstevel@tonic-gate 			} else {
2166*7c478bd9Sstevel@tonic-gate 				tp->tim_flags |= LOCORDREL;
2167*7c478bd9Sstevel@tonic-gate 			}
2168*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
2169*7c478bd9Sstevel@tonic-gate 			break;
2170*7c478bd9Sstevel@tonic-gate 
2171*7c478bd9Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
2172*7c478bd9Sstevel@tonic-gate 			tilog("timodwproc: Got T_CAPABILITY_REQ\n", 0);
2173*7c478bd9Sstevel@tonic-gate 			/*
2174*7c478bd9Sstevel@tonic-gate 			 * XXX: We may know at this point whether transport
2175*7c478bd9Sstevel@tonic-gate 			 * provides T_CAPABILITY_REQ or not and we may utilise
2176*7c478bd9Sstevel@tonic-gate 			 * this knowledge here.
2177*7c478bd9Sstevel@tonic-gate 			 */
2178*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
2179*7c478bd9Sstevel@tonic-gate 			break;
2180*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
2181*7c478bd9Sstevel@tonic-gate 		}
2182*7c478bd9Sstevel@tonic-gate 		break;
2183*7c478bd9Sstevel@tonic-gate 	case M_FLUSH:
2184*7c478bd9Sstevel@tonic-gate 
2185*7c478bd9Sstevel@tonic-gate 		tilog("timodwproc: Got M_FLUSH\n", 0);
2186*7c478bd9Sstevel@tonic-gate 
2187*7c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
2188*7c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHBAND)
2189*7c478bd9Sstevel@tonic-gate 				flushband(q, *(mp->b_rptr + 1), FLUSHDATA);
2190*7c478bd9Sstevel@tonic-gate 			else
2191*7c478bd9Sstevel@tonic-gate 				flushq(q, FLUSHDATA);
2192*7c478bd9Sstevel@tonic-gate 		}
2193*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
2194*7c478bd9Sstevel@tonic-gate 		break;
2195*7c478bd9Sstevel@tonic-gate 	}
2196*7c478bd9Sstevel@tonic-gate 
2197*7c478bd9Sstevel@tonic-gate 	return (0);
2198*7c478bd9Sstevel@tonic-gate }
2199*7c478bd9Sstevel@tonic-gate 
2200*7c478bd9Sstevel@tonic-gate static void
2201*7c478bd9Sstevel@tonic-gate tilog(char *str, t_scalar_t arg)
2202*7c478bd9Sstevel@tonic-gate {
2203*7c478bd9Sstevel@tonic-gate 	if (dotilog) {
2204*7c478bd9Sstevel@tonic-gate 		if (dotilog & 2)
2205*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, str, arg);
2206*7c478bd9Sstevel@tonic-gate 		if (dotilog & 4)
2207*7c478bd9Sstevel@tonic-gate 			(void) strlog(TIMOD_ID, -1, 0, SL_TRACE | SL_ERROR,
2208*7c478bd9Sstevel@tonic-gate 			    str, arg);
2209*7c478bd9Sstevel@tonic-gate 		else
2210*7c478bd9Sstevel@tonic-gate 			(void) strlog(TIMOD_ID, -1, 0, SL_TRACE, str, arg);
2211*7c478bd9Sstevel@tonic-gate 	}
2212*7c478bd9Sstevel@tonic-gate }
2213*7c478bd9Sstevel@tonic-gate 
2214*7c478bd9Sstevel@tonic-gate static void
2215*7c478bd9Sstevel@tonic-gate tilogp(char *str, uintptr_t arg)
2216*7c478bd9Sstevel@tonic-gate {
2217*7c478bd9Sstevel@tonic-gate 	if (dotilog) {
2218*7c478bd9Sstevel@tonic-gate 		if (dotilog & 2)
2219*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, str, arg);
2220*7c478bd9Sstevel@tonic-gate 		if (dotilog & 4)
2221*7c478bd9Sstevel@tonic-gate 			(void) strlog(TIMOD_ID, -1, 0, SL_TRACE | SL_ERROR,
2222*7c478bd9Sstevel@tonic-gate 			    str, arg);
2223*7c478bd9Sstevel@tonic-gate 		else
2224*7c478bd9Sstevel@tonic-gate 			(void) strlog(TIMOD_ID, -1, 0, SL_TRACE, str, arg);
2225*7c478bd9Sstevel@tonic-gate 	}
2226*7c478bd9Sstevel@tonic-gate }
2227*7c478bd9Sstevel@tonic-gate 
2228*7c478bd9Sstevel@tonic-gate 
2229*7c478bd9Sstevel@tonic-gate /*
2230*7c478bd9Sstevel@tonic-gate  * Process the TI_GETNAME ioctl.  If no name exists, return len = 0
2231*7c478bd9Sstevel@tonic-gate  * in strbuf structures.  The state transitions are determined by what
2232*7c478bd9Sstevel@tonic-gate  * is hung of cq_private (cp_private) in the copyresp (copyreq) structure.
2233*7c478bd9Sstevel@tonic-gate  * The high-level steps in the ioctl processing are as follows:
2234*7c478bd9Sstevel@tonic-gate  *
2235*7c478bd9Sstevel@tonic-gate  * 1) we recieve an transparent M_IOCTL with the arg in the second message
2236*7c478bd9Sstevel@tonic-gate  *	block of the message.
2237*7c478bd9Sstevel@tonic-gate  * 2) we send up an M_COPYIN request for the strbuf structure pointed to
2238*7c478bd9Sstevel@tonic-gate  *	by arg.  The block containing arg is hung off cq_private.
2239*7c478bd9Sstevel@tonic-gate  * 3) we receive an M_IOCDATA response with cp->cp_private->b_cont == NULL.
2240*7c478bd9Sstevel@tonic-gate  *	This means that the strbuf structure is found in the message block
2241*7c478bd9Sstevel@tonic-gate  *	mp->b_cont.
2242*7c478bd9Sstevel@tonic-gate  * 4) we send up an M_COPYOUT request with the strbuf message hung off
2243*7c478bd9Sstevel@tonic-gate  *	cq_private->b_cont.  The address we are copying to is strbuf.buf.
2244*7c478bd9Sstevel@tonic-gate  *	we set strbuf.len to 0 to indicate that we should copy the strbuf
2245*7c478bd9Sstevel@tonic-gate  *	structure the next time.  The message mp->b_cont contains the
2246*7c478bd9Sstevel@tonic-gate  *	address info.
2247*7c478bd9Sstevel@tonic-gate  * 5) we receive an M_IOCDATA with cp_private->b_cont != NULL and
2248*7c478bd9Sstevel@tonic-gate  *	strbuf.len == 0.  Restore strbuf.len to either tp->tim_mylen or
2249*7c478bd9Sstevel@tonic-gate  *	tp->tim_peerlen.
2250*7c478bd9Sstevel@tonic-gate  * 6) we send up an M_COPYOUT request with a copy of the strbuf message
2251*7c478bd9Sstevel@tonic-gate  *	hung off mp->b_cont.  In the strbuf structure in the message hung
2252*7c478bd9Sstevel@tonic-gate  *	off cq_private->b_cont, we set strbuf.len to 0 and strbuf.maxlen
2253*7c478bd9Sstevel@tonic-gate  *	to 0.  This means that the next step is to ACK the ioctl.
2254*7c478bd9Sstevel@tonic-gate  * 7) we receive an M_IOCDATA message with cp_private->b_cont != NULL and
2255*7c478bd9Sstevel@tonic-gate  *	strbuf.len == 0 and strbuf.maxlen == 0.  Free up cp->private and
2256*7c478bd9Sstevel@tonic-gate  *	send an M_IOCACK upstream, and we are done.
2257*7c478bd9Sstevel@tonic-gate  *
2258*7c478bd9Sstevel@tonic-gate  */
2259*7c478bd9Sstevel@tonic-gate static int
2260*7c478bd9Sstevel@tonic-gate ti_doname(
2261*7c478bd9Sstevel@tonic-gate 	queue_t *q,		/* queue message arrived at */
2262*7c478bd9Sstevel@tonic-gate 	mblk_t *mp)		/* M_IOCTL or M_IOCDATA message only */
2263*7c478bd9Sstevel@tonic-gate {
2264*7c478bd9Sstevel@tonic-gate 	struct iocblk *iocp;
2265*7c478bd9Sstevel@tonic-gate 	struct copyreq *cqp;
2266*7c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(strbuf, sb);
2267*7c478bd9Sstevel@tonic-gate 	struct copyresp *csp;
2268*7c478bd9Sstevel@tonic-gate 	int ret;
2269*7c478bd9Sstevel@tonic-gate 	mblk_t *bp;
2270*7c478bd9Sstevel@tonic-gate 	struct tim_tim *tp = q->q_ptr;
2271*7c478bd9Sstevel@tonic-gate 	boolean_t getpeer;
2272*7c478bd9Sstevel@tonic-gate 
2273*7c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
2274*7c478bd9Sstevel@tonic-gate 	case M_IOCTL:
2275*7c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
2276*7c478bd9Sstevel@tonic-gate 		if ((iocp->ioc_cmd != TI_GETMYNAME) &&
2277*7c478bd9Sstevel@tonic-gate 		    (iocp->ioc_cmd != TI_GETPEERNAME)) {
2278*7c478bd9Sstevel@tonic-gate 			tilog("ti_doname: bad M_IOCTL command\n", 0);
2279*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
2280*7c478bd9Sstevel@tonic-gate 			ret = DONAME_FAIL;
2281*7c478bd9Sstevel@tonic-gate 			break;
2282*7c478bd9Sstevel@tonic-gate 		}
2283*7c478bd9Sstevel@tonic-gate 		if ((iocp->ioc_count != TRANSPARENT)) {
2284*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
2285*7c478bd9Sstevel@tonic-gate 			ret = DONAME_FAIL;
2286*7c478bd9Sstevel@tonic-gate 			break;
2287*7c478bd9Sstevel@tonic-gate 		}
2288*7c478bd9Sstevel@tonic-gate 
2289*7c478bd9Sstevel@tonic-gate 		cqp = (struct copyreq *)mp->b_rptr;
2290*7c478bd9Sstevel@tonic-gate 		cqp->cq_private = mp->b_cont;
2291*7c478bd9Sstevel@tonic-gate 		cqp->cq_addr = (caddr_t)*(intptr_t *)mp->b_cont->b_rptr;
2292*7c478bd9Sstevel@tonic-gate 		mp->b_cont = NULL;
2293*7c478bd9Sstevel@tonic-gate 		cqp->cq_size = SIZEOF_STRUCT(strbuf, iocp->ioc_flag);
2294*7c478bd9Sstevel@tonic-gate 		cqp->cq_flag = 0;
2295*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_COPYIN;
2296*7c478bd9Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct copyreq);
2297*7c478bd9Sstevel@tonic-gate 		qreply(q, mp);
2298*7c478bd9Sstevel@tonic-gate 		ret = DONAME_CONT;
2299*7c478bd9Sstevel@tonic-gate 		break;
2300*7c478bd9Sstevel@tonic-gate 
2301*7c478bd9Sstevel@tonic-gate 	case M_IOCDATA:
2302*7c478bd9Sstevel@tonic-gate 		csp = (struct copyresp *)mp->b_rptr;
2303*7c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
2304*7c478bd9Sstevel@tonic-gate 		cqp = (struct copyreq *)mp->b_rptr;
2305*7c478bd9Sstevel@tonic-gate 		if ((csp->cp_cmd != TI_GETMYNAME) &&
2306*7c478bd9Sstevel@tonic-gate 		    (csp->cp_cmd != TI_GETPEERNAME)) {
2307*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ti_doname: bad M_IOCDATA command\n");
2308*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
2309*7c478bd9Sstevel@tonic-gate 			ret = DONAME_FAIL;
2310*7c478bd9Sstevel@tonic-gate 			break;
2311*7c478bd9Sstevel@tonic-gate 		}
2312*7c478bd9Sstevel@tonic-gate 		if (csp->cp_rval) {	/* error */
2313*7c478bd9Sstevel@tonic-gate 			freemsg(csp->cp_private);
2314*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
2315*7c478bd9Sstevel@tonic-gate 			ret = DONAME_FAIL;
2316*7c478bd9Sstevel@tonic-gate 			break;
2317*7c478bd9Sstevel@tonic-gate 		}
2318*7c478bd9Sstevel@tonic-gate 		ASSERT(csp->cp_private != NULL);
2319*7c478bd9Sstevel@tonic-gate 		getpeer = csp->cp_cmd == TI_GETPEERNAME;
2320*7c478bd9Sstevel@tonic-gate 		if (getpeer)
2321*7c478bd9Sstevel@tonic-gate 			mutex_enter(&tp->tim_mutex);
2322*7c478bd9Sstevel@tonic-gate 		if (csp->cp_private->b_cont == NULL) {	/* got strbuf */
2323*7c478bd9Sstevel@tonic-gate 			ASSERT(mp->b_cont);
2324*7c478bd9Sstevel@tonic-gate 			STRUCT_SET_HANDLE(sb, iocp->ioc_flag,
2325*7c478bd9Sstevel@tonic-gate 			    (void *)mp->b_cont->b_rptr);
2326*7c478bd9Sstevel@tonic-gate 			if (getpeer) {
2327*7c478bd9Sstevel@tonic-gate 				if (tp->tim_peerlen == 0) {
2328*7c478bd9Sstevel@tonic-gate 					/* copy just strbuf */
2329*7c478bd9Sstevel@tonic-gate 					STRUCT_FSET(sb, len, 0);
2330*7c478bd9Sstevel@tonic-gate 				} else if (tp->tim_peerlen >
2331*7c478bd9Sstevel@tonic-gate 				    STRUCT_FGET(sb, maxlen)) {
2332*7c478bd9Sstevel@tonic-gate 					mutex_exit(&tp->tim_mutex);
2333*7c478bd9Sstevel@tonic-gate 					miocnak(q, mp, 0, ENAMETOOLONG);
2334*7c478bd9Sstevel@tonic-gate 					ret = DONAME_FAIL;
2335*7c478bd9Sstevel@tonic-gate 					break;
2336*7c478bd9Sstevel@tonic-gate 				} else {
2337*7c478bd9Sstevel@tonic-gate 					/* copy buffer */
2338*7c478bd9Sstevel@tonic-gate 					STRUCT_FSET(sb, len, tp->tim_peerlen);
2339*7c478bd9Sstevel@tonic-gate 				}
2340*7c478bd9Sstevel@tonic-gate 			} else {
2341*7c478bd9Sstevel@tonic-gate 				if (tp->tim_mylen == 0) {
2342*7c478bd9Sstevel@tonic-gate 					/* copy just strbuf */
2343*7c478bd9Sstevel@tonic-gate 					STRUCT_FSET(sb, len, 0);
2344*7c478bd9Sstevel@tonic-gate 				} else if (tp->tim_mylen >
2345*7c478bd9Sstevel@tonic-gate 				    STRUCT_FGET(sb, maxlen)) {
2346*7c478bd9Sstevel@tonic-gate 					freemsg(csp->cp_private);
2347*7c478bd9Sstevel@tonic-gate 					miocnak(q, mp, 0, ENAMETOOLONG);
2348*7c478bd9Sstevel@tonic-gate 					ret = DONAME_FAIL;
2349*7c478bd9Sstevel@tonic-gate 					break;
2350*7c478bd9Sstevel@tonic-gate 				} else {
2351*7c478bd9Sstevel@tonic-gate 					/* copy buffer */
2352*7c478bd9Sstevel@tonic-gate 					STRUCT_FSET(sb, len, tp->tim_mylen);
2353*7c478bd9Sstevel@tonic-gate 				}
2354*7c478bd9Sstevel@tonic-gate 			}
2355*7c478bd9Sstevel@tonic-gate 			csp->cp_private->b_cont = mp->b_cont;
2356*7c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
2357*7c478bd9Sstevel@tonic-gate 		}
2358*7c478bd9Sstevel@tonic-gate 		STRUCT_SET_HANDLE(sb, iocp->ioc_flag,
2359*7c478bd9Sstevel@tonic-gate 		    (void *)csp->cp_private->b_cont->b_rptr);
2360*7c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(sb, len) == 0) {
2361*7c478bd9Sstevel@tonic-gate 			/*
2362*7c478bd9Sstevel@tonic-gate 			 * restore strbuf.len
2363*7c478bd9Sstevel@tonic-gate 			 */
2364*7c478bd9Sstevel@tonic-gate 			if (getpeer)
2365*7c478bd9Sstevel@tonic-gate 				STRUCT_FSET(sb, len, tp->tim_peerlen);
2366*7c478bd9Sstevel@tonic-gate 			else
2367*7c478bd9Sstevel@tonic-gate 				STRUCT_FSET(sb, len, tp->tim_mylen);
2368*7c478bd9Sstevel@tonic-gate 
2369*7c478bd9Sstevel@tonic-gate 			if (getpeer)
2370*7c478bd9Sstevel@tonic-gate 				mutex_exit(&tp->tim_mutex);
2371*7c478bd9Sstevel@tonic-gate 			if (STRUCT_FGET(sb, maxlen) == 0) {
2372*7c478bd9Sstevel@tonic-gate 
2373*7c478bd9Sstevel@tonic-gate 				/*
2374*7c478bd9Sstevel@tonic-gate 				 * ack the ioctl
2375*7c478bd9Sstevel@tonic-gate 				 */
2376*7c478bd9Sstevel@tonic-gate 				freemsg(csp->cp_private);
2377*7c478bd9Sstevel@tonic-gate 				tim_ioctl_send_reply(q, mp, NULL);
2378*7c478bd9Sstevel@tonic-gate 				ret = DONAME_DONE;
2379*7c478bd9Sstevel@tonic-gate 				break;
2380*7c478bd9Sstevel@tonic-gate 			}
2381*7c478bd9Sstevel@tonic-gate 
2382*7c478bd9Sstevel@tonic-gate 			if ((bp = allocb(STRUCT_SIZE(sb), BPRI_MED)) == NULL) {
2383*7c478bd9Sstevel@tonic-gate 
2384*7c478bd9Sstevel@tonic-gate 				tilog(
2385*7c478bd9Sstevel@tonic-gate 			"ti_doname: allocb failed no recovery attempt\n", 0);
2386*7c478bd9Sstevel@tonic-gate 
2387*7c478bd9Sstevel@tonic-gate 				freemsg(csp->cp_private);
2388*7c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, EAGAIN);
2389*7c478bd9Sstevel@tonic-gate 				ret = DONAME_FAIL;
2390*7c478bd9Sstevel@tonic-gate 				break;
2391*7c478bd9Sstevel@tonic-gate 			}
2392*7c478bd9Sstevel@tonic-gate 			bp->b_wptr += STRUCT_SIZE(sb);
2393*7c478bd9Sstevel@tonic-gate 			bcopy(STRUCT_BUF(sb), bp->b_rptr, STRUCT_SIZE(sb));
2394*7c478bd9Sstevel@tonic-gate 			cqp->cq_addr =
2395*7c478bd9Sstevel@tonic-gate 			    (caddr_t)*(intptr_t *)csp->cp_private->b_rptr;
2396*7c478bd9Sstevel@tonic-gate 			cqp->cq_size = STRUCT_SIZE(sb);
2397*7c478bd9Sstevel@tonic-gate 			cqp->cq_flag = 0;
2398*7c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_COPYOUT;
2399*7c478bd9Sstevel@tonic-gate 			mp->b_cont = bp;
2400*7c478bd9Sstevel@tonic-gate 			STRUCT_FSET(sb, len, 0);
2401*7c478bd9Sstevel@tonic-gate 			STRUCT_FSET(sb, maxlen, 0); /* ack next time around */
2402*7c478bd9Sstevel@tonic-gate 			qreply(q, mp);
2403*7c478bd9Sstevel@tonic-gate 			ret = DONAME_CONT;
2404*7c478bd9Sstevel@tonic-gate 			break;
2405*7c478bd9Sstevel@tonic-gate 		}
2406*7c478bd9Sstevel@tonic-gate 
2407*7c478bd9Sstevel@tonic-gate 		/*
2408*7c478bd9Sstevel@tonic-gate 		 * copy the address to the user
2409*7c478bd9Sstevel@tonic-gate 		 */
2410*7c478bd9Sstevel@tonic-gate 		if ((bp = allocb((size_t)STRUCT_FGET(sb, len), BPRI_MED))
2411*7c478bd9Sstevel@tonic-gate 		    == NULL) {
2412*7c478bd9Sstevel@tonic-gate 			if (getpeer)
2413*7c478bd9Sstevel@tonic-gate 				mutex_exit(&tp->tim_mutex);
2414*7c478bd9Sstevel@tonic-gate 
2415*7c478bd9Sstevel@tonic-gate 			tilog("ti_doname: allocb failed no recovery attempt\n",
2416*7c478bd9Sstevel@tonic-gate 			    0);
2417*7c478bd9Sstevel@tonic-gate 
2418*7c478bd9Sstevel@tonic-gate 			freemsg(csp->cp_private);
2419*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EAGAIN);
2420*7c478bd9Sstevel@tonic-gate 			ret = DONAME_FAIL;
2421*7c478bd9Sstevel@tonic-gate 			break;
2422*7c478bd9Sstevel@tonic-gate 		}
2423*7c478bd9Sstevel@tonic-gate 		bp->b_wptr += STRUCT_FGET(sb, len);
2424*7c478bd9Sstevel@tonic-gate 		if (getpeer) {
2425*7c478bd9Sstevel@tonic-gate 			bcopy(tp->tim_peername, bp->b_rptr,
2426*7c478bd9Sstevel@tonic-gate 			    STRUCT_FGET(sb, len));
2427*7c478bd9Sstevel@tonic-gate 			mutex_exit(&tp->tim_mutex);
2428*7c478bd9Sstevel@tonic-gate 		} else {
2429*7c478bd9Sstevel@tonic-gate 			bcopy(tp->tim_myname, bp->b_rptr, STRUCT_FGET(sb, len));
2430*7c478bd9Sstevel@tonic-gate 		}
2431*7c478bd9Sstevel@tonic-gate 		cqp->cq_addr = (caddr_t)STRUCT_FGETP(sb, buf);
2432*7c478bd9Sstevel@tonic-gate 		cqp->cq_size = STRUCT_FGET(sb, len);
2433*7c478bd9Sstevel@tonic-gate 		cqp->cq_flag = 0;
2434*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_COPYOUT;
2435*7c478bd9Sstevel@tonic-gate 		mp->b_cont = bp;
2436*7c478bd9Sstevel@tonic-gate 		STRUCT_FSET(sb, len, 0); /* copy the strbuf next time around */
2437*7c478bd9Sstevel@tonic-gate 		qreply(q, mp);
2438*7c478bd9Sstevel@tonic-gate 		ret = DONAME_CONT;
2439*7c478bd9Sstevel@tonic-gate 		break;
2440*7c478bd9Sstevel@tonic-gate 
2441*7c478bd9Sstevel@tonic-gate 	default:
2442*7c478bd9Sstevel@tonic-gate 		tilog("ti_doname: freeing bad message type = %d\n",
2443*7c478bd9Sstevel@tonic-gate 		    mp->b_datap->db_type);
2444*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
2445*7c478bd9Sstevel@tonic-gate 		ret = DONAME_FAIL;
2446*7c478bd9Sstevel@tonic-gate 		break;
2447*7c478bd9Sstevel@tonic-gate 	}
2448*7c478bd9Sstevel@tonic-gate 	return (ret);
2449*7c478bd9Sstevel@tonic-gate }
2450*7c478bd9Sstevel@tonic-gate 
2451*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
2452*7c478bd9Sstevel@tonic-gate 
2453*7c478bd9Sstevel@tonic-gate /*
2454*7c478bd9Sstevel@tonic-gate  * Fill in the address of a connectionless data packet if a connect
2455*7c478bd9Sstevel@tonic-gate  * had been done on this endpoint.
2456*7c478bd9Sstevel@tonic-gate  */
2457*7c478bd9Sstevel@tonic-gate static mblk_t *
2458*7c478bd9Sstevel@tonic-gate tim_filladdr(queue_t *q, mblk_t *mp, boolean_t dorecover)
2459*7c478bd9Sstevel@tonic-gate {
2460*7c478bd9Sstevel@tonic-gate 	mblk_t *bp;
2461*7c478bd9Sstevel@tonic-gate 	struct tim_tim *tp;
2462*7c478bd9Sstevel@tonic-gate 	struct T_unitdata_req *up;
2463*7c478bd9Sstevel@tonic-gate 	struct T_unitdata_req *nup;
2464*7c478bd9Sstevel@tonic-gate 	size_t plen;
2465*7c478bd9Sstevel@tonic-gate 
2466*7c478bd9Sstevel@tonic-gate 	tp = (struct tim_tim *)q->q_ptr;
2467*7c478bd9Sstevel@tonic-gate 	if (mp->b_datap->db_type == M_DATA) {
2468*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tp->tim_mutex);
2469*7c478bd9Sstevel@tonic-gate 		bp = allocb(sizeof (struct T_unitdata_req) + tp->tim_peerlen,
2470*7c478bd9Sstevel@tonic-gate 		    BPRI_MED);
2471*7c478bd9Sstevel@tonic-gate 		if (bp != NULL) {
2472*7c478bd9Sstevel@tonic-gate 			bp->b_datap->db_type = M_PROTO;
2473*7c478bd9Sstevel@tonic-gate 			up = (struct T_unitdata_req *)bp->b_rptr;
2474*7c478bd9Sstevel@tonic-gate 			up->PRIM_type = T_UNITDATA_REQ;
2475*7c478bd9Sstevel@tonic-gate 			up->DEST_length = tp->tim_peerlen;
2476*7c478bd9Sstevel@tonic-gate 			bp->b_wptr += sizeof (struct T_unitdata_req);
2477*7c478bd9Sstevel@tonic-gate 			up->DEST_offset = sizeof (struct T_unitdata_req);
2478*7c478bd9Sstevel@tonic-gate 			up->OPT_length = 0;
2479*7c478bd9Sstevel@tonic-gate 			up->OPT_offset = 0;
2480*7c478bd9Sstevel@tonic-gate 			if (tp->tim_peerlen > 0) {
2481*7c478bd9Sstevel@tonic-gate 				bcopy(tp->tim_peername, bp->b_wptr,
2482*7c478bd9Sstevel@tonic-gate 				    tp->tim_peerlen);
2483*7c478bd9Sstevel@tonic-gate 				bp->b_wptr += tp->tim_peerlen;
2484*7c478bd9Sstevel@tonic-gate 			}
2485*7c478bd9Sstevel@tonic-gate 			bp->b_cont = mp;
2486*7c478bd9Sstevel@tonic-gate 		}
2487*7c478bd9Sstevel@tonic-gate 	} else {
2488*7c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_type == M_PROTO);
2489*7c478bd9Sstevel@tonic-gate 		up = (struct T_unitdata_req *)mp->b_rptr;
2490*7c478bd9Sstevel@tonic-gate 		ASSERT(up->PRIM_type == T_UNITDATA_REQ);
2491*7c478bd9Sstevel@tonic-gate 		if (up->DEST_length != 0)
2492*7c478bd9Sstevel@tonic-gate 			return (mp);
2493*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tp->tim_mutex);
2494*7c478bd9Sstevel@tonic-gate 		bp = allocb(sizeof (struct T_unitdata_req) + up->OPT_length +
2495*7c478bd9Sstevel@tonic-gate 		    tp->tim_peerlen, BPRI_MED);
2496*7c478bd9Sstevel@tonic-gate 		if (bp != NULL) {
2497*7c478bd9Sstevel@tonic-gate 			bp->b_datap->db_type = M_PROTO;
2498*7c478bd9Sstevel@tonic-gate 			nup = (struct T_unitdata_req *)bp->b_rptr;
2499*7c478bd9Sstevel@tonic-gate 			nup->PRIM_type = T_UNITDATA_REQ;
2500*7c478bd9Sstevel@tonic-gate 			nup->DEST_length = plen = tp->tim_peerlen;
2501*7c478bd9Sstevel@tonic-gate 			bp->b_wptr += sizeof (struct T_unitdata_req);
2502*7c478bd9Sstevel@tonic-gate 			nup->DEST_offset = sizeof (struct T_unitdata_req);
2503*7c478bd9Sstevel@tonic-gate 			if (plen > 0) {
2504*7c478bd9Sstevel@tonic-gate 				bcopy(tp->tim_peername, bp->b_wptr, plen);
2505*7c478bd9Sstevel@tonic-gate 				bp->b_wptr += plen;
2506*7c478bd9Sstevel@tonic-gate 			}
2507*7c478bd9Sstevel@tonic-gate 			mutex_exit(&tp->tim_mutex);
2508*7c478bd9Sstevel@tonic-gate 			if (up->OPT_length == 0) {
2509*7c478bd9Sstevel@tonic-gate 				nup->OPT_length = 0;
2510*7c478bd9Sstevel@tonic-gate 				nup->OPT_offset = 0;
2511*7c478bd9Sstevel@tonic-gate 			} else {
2512*7c478bd9Sstevel@tonic-gate 				nup->OPT_length = up->OPT_length;
2513*7c478bd9Sstevel@tonic-gate 				nup->OPT_offset =
2514*7c478bd9Sstevel@tonic-gate 				    sizeof (struct T_unitdata_req) + plen;
2515*7c478bd9Sstevel@tonic-gate 				bcopy((mp->b_wptr + up->OPT_offset), bp->b_wptr,
2516*7c478bd9Sstevel@tonic-gate 				    up->OPT_length);
2517*7c478bd9Sstevel@tonic-gate 				bp->b_wptr += up->OPT_length;
2518*7c478bd9Sstevel@tonic-gate 			}
2519*7c478bd9Sstevel@tonic-gate 			bp->b_cont = mp->b_cont;
2520*7c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
2521*7c478bd9Sstevel@tonic-gate 			freeb(mp);
2522*7c478bd9Sstevel@tonic-gate 			return (bp);
2523*7c478bd9Sstevel@tonic-gate 		}
2524*7c478bd9Sstevel@tonic-gate 	}
2525*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tp->tim_mutex));
2526*7c478bd9Sstevel@tonic-gate 	if (bp == NULL && dorecover) {
2527*7c478bd9Sstevel@tonic-gate 		tim_recover(q, mp,
2528*7c478bd9Sstevel@tonic-gate 		    sizeof (struct T_unitdata_req) + tp->tim_peerlen);
2529*7c478bd9Sstevel@tonic-gate 	}
2530*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->tim_mutex);
2531*7c478bd9Sstevel@tonic-gate 	return (bp);
2532*7c478bd9Sstevel@tonic-gate }
2533*7c478bd9Sstevel@tonic-gate 
2534*7c478bd9Sstevel@tonic-gate static void
2535*7c478bd9Sstevel@tonic-gate tim_addlink(struct tim_tim *tp)
2536*7c478bd9Sstevel@tonic-gate {
2537*7c478bd9Sstevel@tonic-gate 	struct tim_tim **tpp;
2538*7c478bd9Sstevel@tonic-gate 	struct tim_tim	*next;
2539*7c478bd9Sstevel@tonic-gate 
2540*7c478bd9Sstevel@tonic-gate 	tpp = &tim_hash[TIM_HASH(tp->tim_acceptor)];
2541*7c478bd9Sstevel@tonic-gate 	rw_enter(&tim_list_rwlock, RW_WRITER);
2542*7c478bd9Sstevel@tonic-gate 
2543*7c478bd9Sstevel@tonic-gate 	if ((next = *tpp) != NULL)
2544*7c478bd9Sstevel@tonic-gate 		next->tim_ptpn = &tp->tim_next;
2545*7c478bd9Sstevel@tonic-gate 	tp->tim_next = next;
2546*7c478bd9Sstevel@tonic-gate 	tp->tim_ptpn = tpp;
2547*7c478bd9Sstevel@tonic-gate 	*tpp = tp;
2548*7c478bd9Sstevel@tonic-gate 
2549*7c478bd9Sstevel@tonic-gate 	tim_cnt++;
2550*7c478bd9Sstevel@tonic-gate 
2551*7c478bd9Sstevel@tonic-gate 	rw_exit(&tim_list_rwlock);
2552*7c478bd9Sstevel@tonic-gate }
2553*7c478bd9Sstevel@tonic-gate 
2554*7c478bd9Sstevel@tonic-gate static void
2555*7c478bd9Sstevel@tonic-gate tim_dellink(struct tim_tim *tp)
2556*7c478bd9Sstevel@tonic-gate {
2557*7c478bd9Sstevel@tonic-gate 	struct tim_tim	*next;
2558*7c478bd9Sstevel@tonic-gate 
2559*7c478bd9Sstevel@tonic-gate 	rw_enter(&tim_list_rwlock, RW_WRITER);
2560*7c478bd9Sstevel@tonic-gate 
2561*7c478bd9Sstevel@tonic-gate 	if ((next = tp->tim_next) != NULL)
2562*7c478bd9Sstevel@tonic-gate 		next->tim_ptpn = tp->tim_ptpn;
2563*7c478bd9Sstevel@tonic-gate 	*(tp->tim_ptpn) = next;
2564*7c478bd9Sstevel@tonic-gate 
2565*7c478bd9Sstevel@tonic-gate 	tim_cnt--;
2566*7c478bd9Sstevel@tonic-gate 
2567*7c478bd9Sstevel@tonic-gate 	rw_exit(&tim_list_rwlock);
2568*7c478bd9Sstevel@tonic-gate }
2569*7c478bd9Sstevel@tonic-gate 
2570*7c478bd9Sstevel@tonic-gate static struct tim_tim *
2571*7c478bd9Sstevel@tonic-gate tim_findlink(t_uscalar_t id)
2572*7c478bd9Sstevel@tonic-gate {
2573*7c478bd9Sstevel@tonic-gate 	struct tim_tim	*tp;
2574*7c478bd9Sstevel@tonic-gate 
2575*7c478bd9Sstevel@tonic-gate 	ASSERT(rw_lock_held(&tim_list_rwlock));
2576*7c478bd9Sstevel@tonic-gate 
2577*7c478bd9Sstevel@tonic-gate 	for (tp = tim_hash[TIM_HASH(id)]; tp != NULL; tp = tp->tim_next) {
2578*7c478bd9Sstevel@tonic-gate 		if (tp->tim_acceptor == id) {
2579*7c478bd9Sstevel@tonic-gate 			break;
2580*7c478bd9Sstevel@tonic-gate 		}
2581*7c478bd9Sstevel@tonic-gate 	}
2582*7c478bd9Sstevel@tonic-gate 	return (tp);
2583*7c478bd9Sstevel@tonic-gate }
2584*7c478bd9Sstevel@tonic-gate 
2585*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
2586*7c478bd9Sstevel@tonic-gate static void
2587*7c478bd9Sstevel@tonic-gate tim_recover(queue_t *q, mblk_t *mp, t_scalar_t size)
2588*7c478bd9Sstevel@tonic-gate {
2589*7c478bd9Sstevel@tonic-gate 	struct tim_tim	*tp;
2590*7c478bd9Sstevel@tonic-gate 	bufcall_id_t	bid;
2591*7c478bd9Sstevel@tonic-gate 	timeout_id_t	tid;
2592*7c478bd9Sstevel@tonic-gate 
2593*7c478bd9Sstevel@tonic-gate 	tp = (struct tim_tim *)q->q_ptr;
2594*7c478bd9Sstevel@tonic-gate 
2595*7c478bd9Sstevel@tonic-gate 	/*
2596*7c478bd9Sstevel@tonic-gate 	 * Avoid re-enabling the queue.
2597*7c478bd9Sstevel@tonic-gate 	 */
2598*7c478bd9Sstevel@tonic-gate 	if (mp->b_datap->db_type == M_PCPROTO)
2599*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
2600*7c478bd9Sstevel@tonic-gate 	noenable(q);
2601*7c478bd9Sstevel@tonic-gate 	(void) putbq(q, mp);
2602*7c478bd9Sstevel@tonic-gate 
2603*7c478bd9Sstevel@tonic-gate 	/*
2604*7c478bd9Sstevel@tonic-gate 	 * Make sure there is at most one outstanding request per queue.
2605*7c478bd9Sstevel@tonic-gate 	 */
2606*7c478bd9Sstevel@tonic-gate 	if (q->q_flag & QREADR) {
2607*7c478bd9Sstevel@tonic-gate 		if (tp->tim_rtimoutid || tp->tim_rbufcid)
2608*7c478bd9Sstevel@tonic-gate 			return;
2609*7c478bd9Sstevel@tonic-gate 	} else {
2610*7c478bd9Sstevel@tonic-gate 		if (tp->tim_wtimoutid || tp->tim_wbufcid)
2611*7c478bd9Sstevel@tonic-gate 			return;
2612*7c478bd9Sstevel@tonic-gate 	}
2613*7c478bd9Sstevel@tonic-gate 	if (!(bid = qbufcall(RD(q), (size_t)size, BPRI_MED, tim_buffer, q))) {
2614*7c478bd9Sstevel@tonic-gate 		tid = qtimeout(RD(q), tim_timer, q, TIMWAIT);
2615*7c478bd9Sstevel@tonic-gate 		if (q->q_flag & QREADR)
2616*7c478bd9Sstevel@tonic-gate 			tp->tim_rtimoutid = tid;
2617*7c478bd9Sstevel@tonic-gate 		else
2618*7c478bd9Sstevel@tonic-gate 			tp->tim_wtimoutid = tid;
2619*7c478bd9Sstevel@tonic-gate 	} else	{
2620*7c478bd9Sstevel@tonic-gate 		if (q->q_flag & QREADR)
2621*7c478bd9Sstevel@tonic-gate 			tp->tim_rbufcid = bid;
2622*7c478bd9Sstevel@tonic-gate 		else
2623*7c478bd9Sstevel@tonic-gate 			tp->tim_wbufcid = bid;
2624*7c478bd9Sstevel@tonic-gate 	}
2625*7c478bd9Sstevel@tonic-gate }
2626*7c478bd9Sstevel@tonic-gate 
2627*7c478bd9Sstevel@tonic-gate /*
2628*7c478bd9Sstevel@tonic-gate  * Inspect the data on read queues starting from read queues passed as
2629*7c478bd9Sstevel@tonic-gate  * paramter (timod read queue) and traverse until
2630*7c478bd9Sstevel@tonic-gate  * q_next is NULL (stream head). Look for a TPI T_EXDATA_IND message
2631*7c478bd9Sstevel@tonic-gate  * reutrn 1 if found, 0 if not found.
2632*7c478bd9Sstevel@tonic-gate  */
2633*7c478bd9Sstevel@tonic-gate static int
2634*7c478bd9Sstevel@tonic-gate ti_expind_on_rdqueues(queue_t *rq)
2635*7c478bd9Sstevel@tonic-gate {
2636*7c478bd9Sstevel@tonic-gate 	mblk_t *bp;
2637*7c478bd9Sstevel@tonic-gate 	queue_t *q;
2638*7c478bd9Sstevel@tonic-gate 
2639*7c478bd9Sstevel@tonic-gate 	q = rq;
2640*7c478bd9Sstevel@tonic-gate 	/*
2641*7c478bd9Sstevel@tonic-gate 	 * We are going to walk q_next, so protect stream from plumbing
2642*7c478bd9Sstevel@tonic-gate 	 * changes.
2643*7c478bd9Sstevel@tonic-gate 	 */
2644*7c478bd9Sstevel@tonic-gate 	claimstr(q);
2645*7c478bd9Sstevel@tonic-gate 	do {
2646*7c478bd9Sstevel@tonic-gate 		/*
2647*7c478bd9Sstevel@tonic-gate 		 * Hold QLOCK while referencing data on queues
2648*7c478bd9Sstevel@tonic-gate 		 */
2649*7c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(rq));
2650*7c478bd9Sstevel@tonic-gate 		bp = rq->q_first;
2651*7c478bd9Sstevel@tonic-gate 		while (bp != NULL) {
2652*7c478bd9Sstevel@tonic-gate 			/*
2653*7c478bd9Sstevel@tonic-gate 			 * Walk the messages on the queue looking
2654*7c478bd9Sstevel@tonic-gate 			 * for a possible T_EXDATA_IND
2655*7c478bd9Sstevel@tonic-gate 			 */
2656*7c478bd9Sstevel@tonic-gate 			if ((bp->b_datap->db_type == M_PROTO) &&
2657*7c478bd9Sstevel@tonic-gate 			    ((bp->b_wptr - bp->b_rptr) >=
2658*7c478bd9Sstevel@tonic-gate 				sizeof (struct T_exdata_ind)) &&
2659*7c478bd9Sstevel@tonic-gate 			    (((struct T_exdata_ind *)bp->b_rptr)->PRIM_type
2660*7c478bd9Sstevel@tonic-gate 				== T_EXDATA_IND)) {
2661*7c478bd9Sstevel@tonic-gate 				/* bp is T_EXDATA_IND */
2662*7c478bd9Sstevel@tonic-gate 				mutex_exit(QLOCK(rq));
2663*7c478bd9Sstevel@tonic-gate 				releasestr(q); /* decrement sd_refcnt  */
2664*7c478bd9Sstevel@tonic-gate 				return (1); /* expdata is on a read queue */
2665*7c478bd9Sstevel@tonic-gate 			}
2666*7c478bd9Sstevel@tonic-gate 			bp = bp->b_next; /* next message */
2667*7c478bd9Sstevel@tonic-gate 		}
2668*7c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(rq));
2669*7c478bd9Sstevel@tonic-gate 		rq = rq->q_next;	/* next upstream queue */
2670*7c478bd9Sstevel@tonic-gate 	} while (rq != NULL);
2671*7c478bd9Sstevel@tonic-gate 	releasestr(q);
2672*7c478bd9Sstevel@tonic-gate 	return (0);		/* no expdata on read queues */
2673*7c478bd9Sstevel@tonic-gate }
2674*7c478bd9Sstevel@tonic-gate 
2675*7c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
2676*7c478bd9Sstevel@tonic-gate static void
2677*7c478bd9Sstevel@tonic-gate tim_tcap_timer(void *q_ptr)
2678*7c478bd9Sstevel@tonic-gate {
2679*7c478bd9Sstevel@tonic-gate 	queue_t *q = (queue_t *)q_ptr;
2680*7c478bd9Sstevel@tonic-gate 	struct tim_tim *tp = (struct tim_tim *)q->q_ptr;
2681*7c478bd9Sstevel@tonic-gate 
2682*7c478bd9Sstevel@tonic-gate 	ASSERT(tp != NULL && tp->tim_tcap_timoutid != 0);
2683*7c478bd9Sstevel@tonic-gate 	ASSERT((tp->tim_flags & TI_CAP_RECVD) != 0);
2684*7c478bd9Sstevel@tonic-gate 
2685*7c478bd9Sstevel@tonic-gate 	tp->tim_tcap_timoutid = 0;
2686*7c478bd9Sstevel@tonic-gate 	TILOG("tim_tcap_timer: fired\n", 0);
2687*7c478bd9Sstevel@tonic-gate 	tim_tcap_genreply(q, tp);
2688*7c478bd9Sstevel@tonic-gate }
2689*7c478bd9Sstevel@tonic-gate 
2690*7c478bd9Sstevel@tonic-gate /*
2691*7c478bd9Sstevel@tonic-gate  * tim_tcap_genreply() is called either from timeout routine or when
2692*7c478bd9Sstevel@tonic-gate  * T_ERROR_ACK is received. In both cases it means that underlying
2693*7c478bd9Sstevel@tonic-gate  * transport doesn't provide T_CAPABILITY_REQ.
2694*7c478bd9Sstevel@tonic-gate  */
2695*7c478bd9Sstevel@tonic-gate static void
2696*7c478bd9Sstevel@tonic-gate tim_tcap_genreply(queue_t *q, struct tim_tim *tp)
2697*7c478bd9Sstevel@tonic-gate {
2698*7c478bd9Sstevel@tonic-gate 	mblk_t		*mp = tp->tim_iocsave;
2699*7c478bd9Sstevel@tonic-gate 	struct iocblk	*iocbp;
2700*7c478bd9Sstevel@tonic-gate 
2701*7c478bd9Sstevel@tonic-gate 	TILOG("timodrproc: tim_tcap_genreply\n", 0);
2702*7c478bd9Sstevel@tonic-gate 
2703*7c478bd9Sstevel@tonic-gate 	ASSERT(tp == (struct tim_tim *)q->q_ptr);
2704*7c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
2705*7c478bd9Sstevel@tonic-gate 
2706*7c478bd9Sstevel@tonic-gate 	iocbp = (struct iocblk *)mp->b_rptr;
2707*7c478bd9Sstevel@tonic-gate 	ASSERT(iocbp != NULL);
2708*7c478bd9Sstevel@tonic-gate 	ASSERT(MBLKL(mp) == sizeof (struct iocblk));
2709*7c478bd9Sstevel@tonic-gate 	ASSERT(iocbp->ioc_cmd == TI_CAPABILITY);
2710*7c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_cont == NULL);
2711*7c478bd9Sstevel@tonic-gate 
2712*7c478bd9Sstevel@tonic-gate 	/* Save this information permanently in the module */
2713*7c478bd9Sstevel@tonic-gate 	PI_PROVLOCK(tp->tim_provinfo);
2714*7c478bd9Sstevel@tonic-gate 	if (tp->tim_provinfo->tpi_capability == PI_DONTKNOW)
2715*7c478bd9Sstevel@tonic-gate 		tp->tim_provinfo->tpi_capability = PI_NO;
2716*7c478bd9Sstevel@tonic-gate 	PI_PROVUNLOCK(tp->tim_provinfo);
2717*7c478bd9Sstevel@tonic-gate 
2718*7c478bd9Sstevel@tonic-gate 	if (tp->tim_tcap_timoutid != 0) {
2719*7c478bd9Sstevel@tonic-gate 		(void) quntimeout(q, tp->tim_tcap_timoutid);
2720*7c478bd9Sstevel@tonic-gate 		tp->tim_tcap_timoutid = 0;
2721*7c478bd9Sstevel@tonic-gate 	}
2722*7c478bd9Sstevel@tonic-gate 
2723*7c478bd9Sstevel@tonic-gate 	if ((tp->tim_flags & CAP_WANTS_INFO) != 0) {
2724*7c478bd9Sstevel@tonic-gate 		/* Send T_INFO_REQ down */
2725*7c478bd9Sstevel@tonic-gate 		mblk_t *tirmp = tpi_ack_alloc(NULL,
2726*7c478bd9Sstevel@tonic-gate 		    sizeof (struct T_info_req), M_PCPROTO, T_INFO_REQ);
2727*7c478bd9Sstevel@tonic-gate 
2728*7c478bd9Sstevel@tonic-gate 		if (tirmp != NULL) {
2729*7c478bd9Sstevel@tonic-gate 			/* Emulate TC1_INFO */
2730*7c478bd9Sstevel@tonic-gate 			TILOG("emulate_tcap_ioc_req: sending T_INFO_REQ\n", 0);
2731*7c478bd9Sstevel@tonic-gate 			tp->tim_flags |= WAIT_IOCINFOACK;
2732*7c478bd9Sstevel@tonic-gate 			putnext(WR(q), tirmp);
2733*7c478bd9Sstevel@tonic-gate 		} else {
2734*7c478bd9Sstevel@tonic-gate 			tilog("emulate_tcap_req: allocb fail, "
2735*7c478bd9Sstevel@tonic-gate 			    "no recovery attmpt\n", 0);
2736*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave = NULL;
2737*7c478bd9Sstevel@tonic-gate 			tp->tim_saved_prim = -1;
2738*7c478bd9Sstevel@tonic-gate 			tp->tim_flags &= ~(TI_CAP_RECVD | WAITIOCACK |
2739*7c478bd9Sstevel@tonic-gate 			    CAP_WANTS_INFO | WAIT_IOCINFOACK);
2740*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
2741*7c478bd9Sstevel@tonic-gate 		}
2742*7c478bd9Sstevel@tonic-gate 	} else {
2743*7c478bd9Sstevel@tonic-gate 		/* Reply immediately */
2744*7c478bd9Sstevel@tonic-gate 		mblk_t *ackmp = tpi_ack_alloc(NULL,
2745*7c478bd9Sstevel@tonic-gate 		    sizeof (struct T_capability_ack), M_PCPROTO,
2746*7c478bd9Sstevel@tonic-gate 		    T_CAPABILITY_ACK);
2747*7c478bd9Sstevel@tonic-gate 
2748*7c478bd9Sstevel@tonic-gate 		mp->b_cont = ackmp;
2749*7c478bd9Sstevel@tonic-gate 
2750*7c478bd9Sstevel@tonic-gate 		if (ackmp != NULL) {
2751*7c478bd9Sstevel@tonic-gate 			((struct T_capability_ack *)
2752*7c478bd9Sstevel@tonic-gate 			    ackmp->b_rptr)->CAP_bits1 = 0;
2753*7c478bd9Sstevel@tonic-gate 			tim_ioctl_send_reply(q, mp, ackmp);
2754*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave = NULL;
2755*7c478bd9Sstevel@tonic-gate 			tp->tim_saved_prim = -1;
2756*7c478bd9Sstevel@tonic-gate 			tp->tim_flags &= ~(WAITIOCACK | WAIT_IOCINFOACK |
2757*7c478bd9Sstevel@tonic-gate 			    TI_CAP_RECVD | CAP_WANTS_INFO);
2758*7c478bd9Sstevel@tonic-gate 		} else {
2759*7c478bd9Sstevel@tonic-gate 			tilog("timodwproc:allocb failed no "
2760*7c478bd9Sstevel@tonic-gate 			    "recovery attempt\n", 0);
2761*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave = NULL;
2762*7c478bd9Sstevel@tonic-gate 			tp->tim_saved_prim = -1;
2763*7c478bd9Sstevel@tonic-gate 			tp->tim_flags &= ~(TI_CAP_RECVD | WAITIOCACK |
2764*7c478bd9Sstevel@tonic-gate 			    CAP_WANTS_INFO | WAIT_IOCINFOACK);
2765*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
2766*7c478bd9Sstevel@tonic-gate 		}
2767*7c478bd9Sstevel@tonic-gate 	}
2768*7c478bd9Sstevel@tonic-gate }
2769*7c478bd9Sstevel@tonic-gate 
2770*7c478bd9Sstevel@tonic-gate 
2771*7c478bd9Sstevel@tonic-gate static void
2772*7c478bd9Sstevel@tonic-gate tim_ioctl_send_reply(queue_t *q, mblk_t *ioc_mp, mblk_t *mp)
2773*7c478bd9Sstevel@tonic-gate {
2774*7c478bd9Sstevel@tonic-gate 	struct iocblk	*iocbp;
2775*7c478bd9Sstevel@tonic-gate 
2776*7c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL && ioc_mp != NULL);
2777*7c478bd9Sstevel@tonic-gate 
2778*7c478bd9Sstevel@tonic-gate 	ioc_mp->b_datap->db_type = M_IOCACK;
2779*7c478bd9Sstevel@tonic-gate 	if (mp != NULL)
2780*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_DATA;
2781*7c478bd9Sstevel@tonic-gate 
2782*7c478bd9Sstevel@tonic-gate 	if (ioc_mp->b_cont != mp) {
2783*7c478bd9Sstevel@tonic-gate 		/* It is safe to call freemsg for NULL pointers */
2784*7c478bd9Sstevel@tonic-gate 		freemsg(ioc_mp->b_cont);
2785*7c478bd9Sstevel@tonic-gate 		ioc_mp->b_cont = mp;
2786*7c478bd9Sstevel@tonic-gate 	}
2787*7c478bd9Sstevel@tonic-gate 	iocbp = (struct iocblk *)ioc_mp->b_rptr;
2788*7c478bd9Sstevel@tonic-gate 	iocbp->ioc_error = 0;
2789*7c478bd9Sstevel@tonic-gate 	iocbp->ioc_rval = 0;
2790*7c478bd9Sstevel@tonic-gate 	/*
2791*7c478bd9Sstevel@tonic-gate 	 * All ioctl's may return more data than was specified by
2792*7c478bd9Sstevel@tonic-gate 	 * count arg. For TI_CAPABILITY count is treated as maximum data size.
2793*7c478bd9Sstevel@tonic-gate 	 */
2794*7c478bd9Sstevel@tonic-gate 	if (mp == NULL)
2795*7c478bd9Sstevel@tonic-gate 		iocbp->ioc_count = 0;
2796*7c478bd9Sstevel@tonic-gate 	else if (iocbp->ioc_cmd != TI_CAPABILITY)
2797*7c478bd9Sstevel@tonic-gate 		iocbp->ioc_count = msgsize(mp);
2798*7c478bd9Sstevel@tonic-gate 	else {
2799*7c478bd9Sstevel@tonic-gate 		iocbp->ioc_count = MIN(MBLKL(mp), iocbp->ioc_count);
2800*7c478bd9Sstevel@tonic-gate 		/* Truncate message if too large */
2801*7c478bd9Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + iocbp->ioc_count;
2802*7c478bd9Sstevel@tonic-gate 	}
2803*7c478bd9Sstevel@tonic-gate 
2804*7c478bd9Sstevel@tonic-gate 	TILOG("iosendreply: ioc_cmd = %d, ", iocbp->ioc_cmd);
2805*7c478bd9Sstevel@tonic-gate 	putnext(RD(q), ioc_mp);
2806*7c478bd9Sstevel@tonic-gate }
2807*7c478bd9Sstevel@tonic-gate 
2808*7c478bd9Sstevel@tonic-gate /*
2809*7c478bd9Sstevel@tonic-gate  * Send M_IOCACK for errors.
2810*7c478bd9Sstevel@tonic-gate  */
2811*7c478bd9Sstevel@tonic-gate static void
2812*7c478bd9Sstevel@tonic-gate tim_send_ioc_error_ack(queue_t *q, struct tim_tim *tp, mblk_t *mp)
2813*7c478bd9Sstevel@tonic-gate {
2814*7c478bd9Sstevel@tonic-gate 	struct T_error_ack *tea = (struct T_error_ack *)mp->b_rptr;
2815*7c478bd9Sstevel@tonic-gate 	t_scalar_t error_prim;
2816*7c478bd9Sstevel@tonic-gate 
2817*7c478bd9Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + sizeof (struct T_error_ack);
2818*7c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
2819*7c478bd9Sstevel@tonic-gate 	error_prim = tea->ERROR_prim;
2820*7c478bd9Sstevel@tonic-gate 
2821*7c478bd9Sstevel@tonic-gate 	ASSERT(tp->tim_iocsave != NULL);
2822*7c478bd9Sstevel@tonic-gate 	ASSERT(tp->tim_iocsave->b_cont != mp);
2823*7c478bd9Sstevel@tonic-gate 
2824*7c478bd9Sstevel@tonic-gate 	/* Always send this to the read side of the queue */
2825*7c478bd9Sstevel@tonic-gate 	q = RD(q);
2826*7c478bd9Sstevel@tonic-gate 
2827*7c478bd9Sstevel@tonic-gate 	TILOG("tim_send_ioc_error_ack: prim = %d\n", tp->tim_saved_prim);
2828*7c478bd9Sstevel@tonic-gate 
2829*7c478bd9Sstevel@tonic-gate 	if (tp->tim_saved_prim != error_prim) {
2830*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
2831*7c478bd9Sstevel@tonic-gate 	} else if (error_prim == T_CAPABILITY_REQ) {
2832*7c478bd9Sstevel@tonic-gate 		TILOG("timodrproc: T_ERROR_ACK/T_CAPABILITY_REQ\n", 0);
2833*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->tim_iocsave->b_cont == NULL);
2834*7c478bd9Sstevel@tonic-gate 
2835*7c478bd9Sstevel@tonic-gate 		tim_tcap_genreply(q, tp);
2836*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
2837*7c478bd9Sstevel@tonic-gate 	} else {
2838*7c478bd9Sstevel@tonic-gate 		struct iocblk *iocbp = (struct iocblk *)tp->tim_iocsave->b_rptr;
2839*7c478bd9Sstevel@tonic-gate 
2840*7c478bd9Sstevel@tonic-gate 		TILOG("tim_send_ioc_error_ack: T_ERROR_ACK: prim %d\n",
2841*7c478bd9Sstevel@tonic-gate 		    error_prim);
2842*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->tim_iocsave->b_cont == NULL);
2843*7c478bd9Sstevel@tonic-gate 
2844*7c478bd9Sstevel@tonic-gate 		switch (error_prim) {
2845*7c478bd9Sstevel@tonic-gate 		default:
2846*7c478bd9Sstevel@tonic-gate 			TILOG("timodrproc: Unknown T_ERROR_ACK:  tlierror %d\n",
2847*7c478bd9Sstevel@tonic-gate 			    tea->TLI_error);
2848*7c478bd9Sstevel@tonic-gate 
2849*7c478bd9Sstevel@tonic-gate 			putnext(q, mp);
2850*7c478bd9Sstevel@tonic-gate 			break;
2851*7c478bd9Sstevel@tonic-gate 
2852*7c478bd9Sstevel@tonic-gate 		case T_INFO_REQ:
2853*7c478bd9Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
2854*7c478bd9Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
2855*7c478bd9Sstevel@tonic-gate 		case O_T_BIND_REQ:
2856*7c478bd9Sstevel@tonic-gate 		case T_BIND_REQ:
2857*7c478bd9Sstevel@tonic-gate 		case T_UNBIND_REQ:
2858*7c478bd9Sstevel@tonic-gate 		case T_ADDR_REQ:
2859*7c478bd9Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
2860*7c478bd9Sstevel@tonic-gate 
2861*7c478bd9Sstevel@tonic-gate 			TILOG("ioc_err_ack: T_ERROR_ACK: tlierror %x\n",
2862*7c478bd9Sstevel@tonic-gate 			    tea->TLI_error);
2863*7c478bd9Sstevel@tonic-gate 
2864*7c478bd9Sstevel@tonic-gate 			/* get saved ioctl msg and set values */
2865*7c478bd9Sstevel@tonic-gate 			iocbp->ioc_count = 0;
2866*7c478bd9Sstevel@tonic-gate 			iocbp->ioc_error = 0;
2867*7c478bd9Sstevel@tonic-gate 			iocbp->ioc_rval = tea->TLI_error;
2868*7c478bd9Sstevel@tonic-gate 			if (iocbp->ioc_rval == TSYSERR)
2869*7c478bd9Sstevel@tonic-gate 				iocbp->ioc_rval |= tea->UNIX_error << 8;
2870*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave->b_datap->db_type = M_IOCACK;
2871*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
2872*7c478bd9Sstevel@tonic-gate 			putnext(q, tp->tim_iocsave);
2873*7c478bd9Sstevel@tonic-gate 			tp->tim_iocsave = NULL;
2874*7c478bd9Sstevel@tonic-gate 			tp->tim_saved_prim = -1;
2875*7c478bd9Sstevel@tonic-gate 			tp->tim_flags &= ~(WAITIOCACK | TI_CAP_RECVD |
2876*7c478bd9Sstevel@tonic-gate 			    CAP_WANTS_INFO | WAIT_IOCINFOACK);
2877*7c478bd9Sstevel@tonic-gate 			break;
2878*7c478bd9Sstevel@tonic-gate 		}
2879*7c478bd9Sstevel@tonic-gate 	}
2880*7c478bd9Sstevel@tonic-gate }
2881*7c478bd9Sstevel@tonic-gate 
2882*7c478bd9Sstevel@tonic-gate /*
2883*7c478bd9Sstevel@tonic-gate  * Send reply to a usual message or ioctl message upstream.
2884*7c478bd9Sstevel@tonic-gate  * Should be called from the read side only.
2885*7c478bd9Sstevel@tonic-gate  */
2886*7c478bd9Sstevel@tonic-gate static void
2887*7c478bd9Sstevel@tonic-gate tim_send_reply(queue_t *q, mblk_t *mp, struct tim_tim *tp, t_scalar_t prim)
2888*7c478bd9Sstevel@tonic-gate {
2889*7c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL && q != NULL && tp != NULL);
2890*7c478bd9Sstevel@tonic-gate 	ASSERT(q == RD(q));
2891*7c478bd9Sstevel@tonic-gate 
2892*7c478bd9Sstevel@tonic-gate 	/* Restore db_type - recover() might have changed it */
2893*7c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_PCPROTO;
2894*7c478bd9Sstevel@tonic-gate 
2895*7c478bd9Sstevel@tonic-gate 	if (((tp->tim_flags & WAITIOCACK) == 0) || (tp->tim_saved_prim != prim))
2896*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
2897*7c478bd9Sstevel@tonic-gate 	else {
2898*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->tim_iocsave != NULL);
2899*7c478bd9Sstevel@tonic-gate 		tim_ioctl_send_reply(q, tp->tim_iocsave, mp);
2900*7c478bd9Sstevel@tonic-gate 		tp->tim_iocsave = NULL;
2901*7c478bd9Sstevel@tonic-gate 		tp->tim_saved_prim = -1;
2902*7c478bd9Sstevel@tonic-gate 		tp->tim_flags &= ~(WAITIOCACK | WAIT_IOCINFOACK |
2903*7c478bd9Sstevel@tonic-gate 		    TI_CAP_RECVD | CAP_WANTS_INFO);
2904*7c478bd9Sstevel@tonic-gate 	}
2905*7c478bd9Sstevel@tonic-gate }
2906*7c478bd9Sstevel@tonic-gate 
2907*7c478bd9Sstevel@tonic-gate /*
2908*7c478bd9Sstevel@tonic-gate  * Reply to TI_SYNC reequest without sending anything downstream.
2909*7c478bd9Sstevel@tonic-gate  */
2910*7c478bd9Sstevel@tonic-gate static void
2911*7c478bd9Sstevel@tonic-gate tim_answer_ti_sync(queue_t *q, mblk_t *mp, struct tim_tim *tp,
2912*7c478bd9Sstevel@tonic-gate     mblk_t *ackmp, uint32_t tsr_flags)
2913*7c478bd9Sstevel@tonic-gate {
2914*7c478bd9Sstevel@tonic-gate 	struct ti_sync_ack *tsap;
2915*7c478bd9Sstevel@tonic-gate 
2916*7c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL && q == WR(q) && ackmp != NULL);
2917*7c478bd9Sstevel@tonic-gate 
2918*7c478bd9Sstevel@tonic-gate 	tsap = (struct ti_sync_ack *)ackmp->b_rptr;
2919*7c478bd9Sstevel@tonic-gate 	bzero(tsap, sizeof (struct ti_sync_ack));
2920*7c478bd9Sstevel@tonic-gate 	ackmp->b_wptr = ackmp->b_rptr + sizeof (struct ti_sync_ack);
2921*7c478bd9Sstevel@tonic-gate 
2922*7c478bd9Sstevel@tonic-gate 	if (tsr_flags == 0 ||
2923*7c478bd9Sstevel@tonic-gate 	    (tsr_flags & ~(TSRF_QLEN_REQ | TSRF_IS_EXP_IN_RCVBUF)) != 0) {
2924*7c478bd9Sstevel@tonic-gate 		/*
2925*7c478bd9Sstevel@tonic-gate 		 * unsupported/bad flag setting
2926*7c478bd9Sstevel@tonic-gate 		 * or no flag set.
2927*7c478bd9Sstevel@tonic-gate 		 */
2928*7c478bd9Sstevel@tonic-gate 		TILOG("timodwproc: unsupported/bad flag setting %x\n",
2929*7c478bd9Sstevel@tonic-gate 		    tsr_flags);
2930*7c478bd9Sstevel@tonic-gate 		freemsg(ackmp);
2931*7c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
2932*7c478bd9Sstevel@tonic-gate 		return;
2933*7c478bd9Sstevel@tonic-gate 	}
2934*7c478bd9Sstevel@tonic-gate 
2935*7c478bd9Sstevel@tonic-gate 	if ((tsr_flags & TSRF_QLEN_REQ) != 0)
2936*7c478bd9Sstevel@tonic-gate 		tsap->tsa_qlen = tp->tim_backlog;
2937*7c478bd9Sstevel@tonic-gate 
2938*7c478bd9Sstevel@tonic-gate 	if ((tsr_flags & TSRF_IS_EXP_IN_RCVBUF) != 0 &&
2939*7c478bd9Sstevel@tonic-gate 	    ti_expind_on_rdqueues(RD(q))) {
2940*7c478bd9Sstevel@tonic-gate 		/*
2941*7c478bd9Sstevel@tonic-gate 		 * Expedited data is queued on
2942*7c478bd9Sstevel@tonic-gate 		 * the stream read side
2943*7c478bd9Sstevel@tonic-gate 		 */
2944*7c478bd9Sstevel@tonic-gate 		tsap->tsa_flags |= TSAF_EXP_QUEUED;
2945*7c478bd9Sstevel@tonic-gate 	}
2946*7c478bd9Sstevel@tonic-gate 
2947*7c478bd9Sstevel@tonic-gate 	tim_ioctl_send_reply(q, mp, ackmp);
2948*7c478bd9Sstevel@tonic-gate 	tp->tim_iocsave = NULL;
2949*7c478bd9Sstevel@tonic-gate 	tp->tim_saved_prim = -1;
2950*7c478bd9Sstevel@tonic-gate 	tp->tim_flags &= ~(WAITIOCACK | WAIT_IOCINFOACK |
2951*7c478bd9Sstevel@tonic-gate 	    TI_CAP_RECVD | CAP_WANTS_INFO);
2952*7c478bd9Sstevel@tonic-gate }
2953*7c478bd9Sstevel@tonic-gate 
2954*7c478bd9Sstevel@tonic-gate /*
2955*7c478bd9Sstevel@tonic-gate  * Send TPI message from IOCTL message, ssave original ioctl header and TPI
2956*7c478bd9Sstevel@tonic-gate  * message type. Should be called from write side only.
2957*7c478bd9Sstevel@tonic-gate  */
2958*7c478bd9Sstevel@tonic-gate static void
2959*7c478bd9Sstevel@tonic-gate tim_send_ioctl_tpi_msg(queue_t *q, mblk_t *mp, struct tim_tim *tp,
2960*7c478bd9Sstevel@tonic-gate 	struct iocblk *iocb)
2961*7c478bd9Sstevel@tonic-gate {
2962*7c478bd9Sstevel@tonic-gate 	mblk_t *tmp;
2963*7c478bd9Sstevel@tonic-gate 	int ioc_cmd = iocb->ioc_cmd;
2964*7c478bd9Sstevel@tonic-gate 
2965*7c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL && mp != NULL && tp != NULL);
2966*7c478bd9Sstevel@tonic-gate 	ASSERT(q == WR(q));
2967*7c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_cont != NULL);
2968*7c478bd9Sstevel@tonic-gate 
2969*7c478bd9Sstevel@tonic-gate 	tp->tim_iocsave = mp;
2970*7c478bd9Sstevel@tonic-gate 	tmp = mp->b_cont;
2971*7c478bd9Sstevel@tonic-gate 
2972*7c478bd9Sstevel@tonic-gate 	mp->b_cont = NULL;
2973*7c478bd9Sstevel@tonic-gate 	tp->tim_flags |= WAITIOCACK;
2974*7c478bd9Sstevel@tonic-gate 	tp->tim_saved_prim = ((union T_primitives *)tmp->b_rptr)->type;
2975*7c478bd9Sstevel@tonic-gate 
2976*7c478bd9Sstevel@tonic-gate 	/*
2977*7c478bd9Sstevel@tonic-gate 	 * For TI_GETINFO, the attached message is a T_INFO_REQ
2978*7c478bd9Sstevel@tonic-gate 	 * For TI_SYNC, we generate the T_INFO_REQ message above
2979*7c478bd9Sstevel@tonic-gate 	 * For TI_CAPABILITY the attached message is either
2980*7c478bd9Sstevel@tonic-gate 	 * T_CAPABILITY_REQ or T_INFO_REQ.
2981*7c478bd9Sstevel@tonic-gate 	 * Among TPI request messages possible,
2982*7c478bd9Sstevel@tonic-gate 	 *	T_INFO_REQ/T_CAPABILITY_ACK messages are a M_PCPROTO, rest
2983*7c478bd9Sstevel@tonic-gate 	 *	are M_PROTO
2984*7c478bd9Sstevel@tonic-gate 	 */
2985*7c478bd9Sstevel@tonic-gate 	if (ioc_cmd == TI_GETINFO || ioc_cmd == TI_SYNC ||
2986*7c478bd9Sstevel@tonic-gate 	    ioc_cmd == TI_CAPABILITY) {
2987*7c478bd9Sstevel@tonic-gate 		tmp->b_datap->db_type = M_PCPROTO;
2988*7c478bd9Sstevel@tonic-gate 	} else {
2989*7c478bd9Sstevel@tonic-gate 		tmp->b_datap->db_type = M_PROTO;
2990*7c478bd9Sstevel@tonic-gate 	}
2991*7c478bd9Sstevel@tonic-gate 
2992*7c478bd9Sstevel@tonic-gate 	/* Verify credentials in STREAM */
2993*7c478bd9Sstevel@tonic-gate 	ASSERT(iocb->ioc_cr == NULL || iocb->ioc_cr == DB_CRED(tmp));
2994*7c478bd9Sstevel@tonic-gate 
2995*7c478bd9Sstevel@tonic-gate 	TILOG("timodwproc: sending down %d\n", tp->tim_saved_prim);
2996*7c478bd9Sstevel@tonic-gate 	putnext(q, tmp);
2997*7c478bd9Sstevel@tonic-gate }
2998*7c478bd9Sstevel@tonic-gate 
2999*7c478bd9Sstevel@tonic-gate static void
3000*7c478bd9Sstevel@tonic-gate tim_clear_peer(struct tim_tim *tp)
3001*7c478bd9Sstevel@tonic-gate {
3002*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->tim_mutex);
3003*7c478bd9Sstevel@tonic-gate 	if (tp->tim_peercred != NULL) {
3004*7c478bd9Sstevel@tonic-gate 		crfree(tp->tim_peercred);
3005*7c478bd9Sstevel@tonic-gate 		tp->tim_peercred = NULL;
3006*7c478bd9Sstevel@tonic-gate 	}
3007*7c478bd9Sstevel@tonic-gate 	tp->tim_peerlen = 0;
3008*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->tim_mutex);
3009*7c478bd9Sstevel@tonic-gate }
3010