xref: /onnv-gate/usr/src/uts/common/io/rlmod.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * This module implements the services provided by the rlogin daemon
31*0Sstevel@tonic-gate  * after the connection is set up.  Mainly this means responding to
32*0Sstevel@tonic-gate  * interrupts and window size changes.  It begins operation in "disabled"
33*0Sstevel@tonic-gate  * state, and sends a T_DATA_REQ to the daemon to indicate that it is
34*0Sstevel@tonic-gate  * in place and ready to be enabled.  The daemon can then know when all
35*0Sstevel@tonic-gate  * data which sneaked passed rlmod (before it was pushed) has been received.
36*0Sstevel@tonic-gate  * The daemon may process this data, or send data back to be inserted in
37*0Sstevel@tonic-gate  * the read queue at the head with the RL_IOC_ENABLE ioctl.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <sys/types.h>
41*0Sstevel@tonic-gate #include <sys/param.h>
42*0Sstevel@tonic-gate #include <sys/stream.h>
43*0Sstevel@tonic-gate #include <sys/stropts.h>
44*0Sstevel@tonic-gate #include <sys/strsun.h>
45*0Sstevel@tonic-gate #include <sys/kmem.h>
46*0Sstevel@tonic-gate #include <sys/errno.h>
47*0Sstevel@tonic-gate #include <sys/ddi.h>
48*0Sstevel@tonic-gate #include <sys/sunddi.h>
49*0Sstevel@tonic-gate #include <sys/tihdr.h>
50*0Sstevel@tonic-gate #include <sys/ptem.h>
51*0Sstevel@tonic-gate #include <sys/conf.h>
52*0Sstevel@tonic-gate #include <sys/debug.h>
53*0Sstevel@tonic-gate #include <sys/modctl.h>
54*0Sstevel@tonic-gate #include <sys/vtrace.h>
55*0Sstevel@tonic-gate #include <sys/rlioctl.h>
56*0Sstevel@tonic-gate #include <sys/termios.h>
57*0Sstevel@tonic-gate #include <sys/termio.h>
58*0Sstevel@tonic-gate #include <sys/byteorder.h>
59*0Sstevel@tonic-gate #include <sys/cmn_err.h>
60*0Sstevel@tonic-gate #include <sys/cryptmod.h>
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate extern struct streamtab rloginmodinfo;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static struct fmodsw fsw = {
65*0Sstevel@tonic-gate 	"rlmod",
66*0Sstevel@tonic-gate 	&rloginmodinfo,
67*0Sstevel@tonic-gate 	D_MTQPAIR | D_MP
68*0Sstevel@tonic-gate };
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate  * Module linkage information for the kernel.
72*0Sstevel@tonic-gate  */
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
75*0Sstevel@tonic-gate 	&mod_strmodops,
76*0Sstevel@tonic-gate 	"rloginmod module",
77*0Sstevel@tonic-gate 	&fsw
78*0Sstevel@tonic-gate };
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
81*0Sstevel@tonic-gate 	MODREV_1, &modlstrmod, NULL
82*0Sstevel@tonic-gate };
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate int
_init(void)86*0Sstevel@tonic-gate _init(void)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate int
_fini(void)92*0Sstevel@tonic-gate _fini(void)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate int
_info(struct modinfo * modinfop)98*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate struct rlmod_info; /* forward reference for function prototype */
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate static int		rlmodopen(queue_t *, dev_t *, int, int, cred_t *);
106*0Sstevel@tonic-gate static int		rlmodclose(queue_t *, int, cred_t *);
107*0Sstevel@tonic-gate static int		rlmodrput(queue_t *, mblk_t *);
108*0Sstevel@tonic-gate static int		rlmodrsrv(queue_t *);
109*0Sstevel@tonic-gate static int		rlmodwput(queue_t *, mblk_t *);
110*0Sstevel@tonic-gate static int		rlmodwsrv(queue_t *);
111*0Sstevel@tonic-gate static int		rlmodrmsg(queue_t *, mblk_t *);
112*0Sstevel@tonic-gate static mblk_t		*make_expmblk(char);
113*0Sstevel@tonic-gate static int 		rlwinctl(queue_t *, mblk_t *);
114*0Sstevel@tonic-gate static mblk_t		*rlwinsetup(queue_t *, mblk_t *, unsigned char *);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate static void		rlmod_timer(void *);
117*0Sstevel@tonic-gate static void		rlmod_buffer(void *);
118*0Sstevel@tonic-gate static boolean_t	tty_flow(queue_t *, struct rlmod_info *, mblk_t *);
119*0Sstevel@tonic-gate static boolean_t	rlmodwioctl(queue_t *, mblk_t *);
120*0Sstevel@tonic-gate static void		recover(queue_t *, mblk_t *, size_t);
121*0Sstevel@tonic-gate static void		recover1(queue_t *, size_t);
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate #define	RLMOD_ID	106
124*0Sstevel@tonic-gate #define	SIMWAIT		(1*hz)
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate /*
127*0Sstevel@tonic-gate  * Stream module data structure definitions.
128*0Sstevel@tonic-gate  * generally pushed onto tcp by rlogin daemon
129*0Sstevel@tonic-gate  *
130*0Sstevel@tonic-gate  */
131*0Sstevel@tonic-gate static	struct	module_info	rloginmodiinfo = {
132*0Sstevel@tonic-gate 	RLMOD_ID,				/* module id number */
133*0Sstevel@tonic-gate 	"rlmod",				/* module name */
134*0Sstevel@tonic-gate 	0,					/* minimum packet size */
135*0Sstevel@tonic-gate 	INFPSZ,					/* maximum packet size */
136*0Sstevel@tonic-gate 	512,					/* hi-water mark */
137*0Sstevel@tonic-gate 	256					/* lo-water mark */
138*0Sstevel@tonic-gate };
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate static	struct	qinit	rloginmodrinit = {
141*0Sstevel@tonic-gate 	rlmodrput,
142*0Sstevel@tonic-gate 	rlmodrsrv,
143*0Sstevel@tonic-gate 	rlmodopen,
144*0Sstevel@tonic-gate 	rlmodclose,
145*0Sstevel@tonic-gate 	nulldev,
146*0Sstevel@tonic-gate 	&rloginmodiinfo,
147*0Sstevel@tonic-gate 	NULL
148*0Sstevel@tonic-gate };
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate static	struct	qinit	rloginmodwinit = {
151*0Sstevel@tonic-gate 	rlmodwput,
152*0Sstevel@tonic-gate 	rlmodwsrv,
153*0Sstevel@tonic-gate 	NULL,
154*0Sstevel@tonic-gate 	NULL,
155*0Sstevel@tonic-gate 	nulldev,
156*0Sstevel@tonic-gate 	&rloginmodiinfo,
157*0Sstevel@tonic-gate 	NULL
158*0Sstevel@tonic-gate };
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate struct	streamtab	rloginmodinfo = {
161*0Sstevel@tonic-gate 	&rloginmodrinit,
162*0Sstevel@tonic-gate 	&rloginmodwinit,
163*0Sstevel@tonic-gate 	NULL,
164*0Sstevel@tonic-gate 	NULL
165*0Sstevel@tonic-gate };
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate /*
168*0Sstevel@tonic-gate  * Per-instance state struct for the rloginmod module.
169*0Sstevel@tonic-gate  */
170*0Sstevel@tonic-gate struct rlmod_info
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate 	int		flags;
173*0Sstevel@tonic-gate 	bufcall_id_t	wbufcid;
174*0Sstevel@tonic-gate 	bufcall_id_t	rbufcid;
175*0Sstevel@tonic-gate 	timeout_id_t	wtimoutid;
176*0Sstevel@tonic-gate 	timeout_id_t	rtimoutid;
177*0Sstevel@tonic-gate 	int		rl_expdat;
178*0Sstevel@tonic-gate 	int		stopmode;
179*0Sstevel@tonic-gate 	mblk_t		*unbind_mp;
180*0Sstevel@tonic-gate 	char		startc;
181*0Sstevel@tonic-gate 	char		stopc;
182*0Sstevel@tonic-gate 	char		oobdata[1];
183*0Sstevel@tonic-gate 	mblk_t		*wndw_sz_hd_mp;
184*0Sstevel@tonic-gate };
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate /*
187*0Sstevel@tonic-gate  * Flag used in flags
188*0Sstevel@tonic-gate  */
189*0Sstevel@tonic-gate #define	RL_DISABLED	0x1
190*0Sstevel@tonic-gate #define	RL_IOCPASSTHRU	0x2
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate /*ARGSUSED*/
193*0Sstevel@tonic-gate static void
dummy_callback(void * arg)194*0Sstevel@tonic-gate dummy_callback(void *arg)
195*0Sstevel@tonic-gate {}
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate /*
198*0Sstevel@tonic-gate  * rlmodopen - open routine gets called when the
199*0Sstevel@tonic-gate  *	    module gets pushed onto the stream.
200*0Sstevel@tonic-gate  */
201*0Sstevel@tonic-gate /*ARGSUSED*/
202*0Sstevel@tonic-gate static int
rlmodopen(queue_t * q,dev_t * devp,int oflag,int sflag,cred_t * cred)203*0Sstevel@tonic-gate rlmodopen(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *cred)
204*0Sstevel@tonic-gate {
205*0Sstevel@tonic-gate 	struct rlmod_info	*rmip;
206*0Sstevel@tonic-gate 	union T_primitives *tp;
207*0Sstevel@tonic-gate 	mblk_t *bp;
208*0Sstevel@tonic-gate 	int	error;
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	if (sflag != MODOPEN)
211*0Sstevel@tonic-gate 		return (EINVAL);
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	if (q->q_ptr != NULL) {
214*0Sstevel@tonic-gate 		/* It's already attached. */
215*0Sstevel@tonic-gate 		return (0);
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	/*
219*0Sstevel@tonic-gate 	 * Allocate state structure.
220*0Sstevel@tonic-gate 	 */
221*0Sstevel@tonic-gate 	rmip = kmem_zalloc(sizeof (*rmip), KM_SLEEP);
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	/*
224*0Sstevel@tonic-gate 	 * Cross-link.
225*0Sstevel@tonic-gate 	 */
226*0Sstevel@tonic-gate 	q->q_ptr = rmip;
227*0Sstevel@tonic-gate 	WR(q)->q_ptr = rmip;
228*0Sstevel@tonic-gate 	rmip->rl_expdat = 0;
229*0Sstevel@tonic-gate 	rmip->stopmode = TIOCPKT_DOSTOP;
230*0Sstevel@tonic-gate 	rmip->startc = CTRL('q');
231*0Sstevel@tonic-gate 	rmip->stopc = CTRL('s');
232*0Sstevel@tonic-gate 	rmip->oobdata[0] = (char)TIOCPKT_WINDOW;
233*0Sstevel@tonic-gate 	rmip->wndw_sz_hd_mp = NULL;
234*0Sstevel@tonic-gate 	/*
235*0Sstevel@tonic-gate 	 * Allow only non-M_DATA blocks to pass up to in.rlogind until
236*0Sstevel@tonic-gate 	 * it is ready for M_DATA (indicated by RL_IOC_ENABLE).
237*0Sstevel@tonic-gate 	 */
238*0Sstevel@tonic-gate 	rmip->flags |= RL_DISABLED;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	qprocson(q);
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	/*
243*0Sstevel@tonic-gate 	 * Since TCP operates in the TLI-inspired brain-dead fashion,
244*0Sstevel@tonic-gate 	 * the connection will revert to bound state if the connection
245*0Sstevel@tonic-gate 	 * is reset by the client.  We must send a T_UNBIND_REQ in
246*0Sstevel@tonic-gate 	 * that case so the port doesn't get "wedged" (preventing
247*0Sstevel@tonic-gate 	 * inetd from being able to restart the listener).  Allocate
248*0Sstevel@tonic-gate 	 * it here, so that we don't need to worry about allocb()
249*0Sstevel@tonic-gate 	 * failures later.
250*0Sstevel@tonic-gate 	 */
251*0Sstevel@tonic-gate 	while ((rmip->unbind_mp = allocb(sizeof (union T_primitives),
252*0Sstevel@tonic-gate 	    BPRI_HI)) == NULL) {
253*0Sstevel@tonic-gate 		bufcall_id_t id = qbufcall(q, sizeof (union T_primitives),
254*0Sstevel@tonic-gate 		    BPRI_HI, dummy_callback, NULL);
255*0Sstevel@tonic-gate 		if (!qwait_sig(q)) {
256*0Sstevel@tonic-gate 			qunbufcall(q, id);
257*0Sstevel@tonic-gate 			error = EINTR;
258*0Sstevel@tonic-gate 			goto fail;
259*0Sstevel@tonic-gate 		}
260*0Sstevel@tonic-gate 		qunbufcall(q, id);
261*0Sstevel@tonic-gate 	}
262*0Sstevel@tonic-gate 	rmip->unbind_mp->b_wptr = rmip->unbind_mp->b_rptr +
263*0Sstevel@tonic-gate 	    sizeof (struct T_unbind_req);
264*0Sstevel@tonic-gate 	rmip->unbind_mp->b_datap->db_type = M_PROTO;
265*0Sstevel@tonic-gate 	tp = (union T_primitives *)rmip->unbind_mp->b_rptr;
266*0Sstevel@tonic-gate 	tp->type = T_UNBIND_REQ;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	/*
269*0Sstevel@tonic-gate 	 * Send a M_PROTO msg of type T_DATA_REQ (this is unique for
270*0Sstevel@tonic-gate 	 * read queue since only write queue can get T_DATA_REQ).
271*0Sstevel@tonic-gate 	 * Readstream routine in the daemon will do a getmsg() till
272*0Sstevel@tonic-gate 	 * it receives this proto message.
273*0Sstevel@tonic-gate 	 */
274*0Sstevel@tonic-gate 	while ((bp = allocb(sizeof (union T_primitives), BPRI_HI)) == NULL) {
275*0Sstevel@tonic-gate 		bufcall_id_t id = qbufcall(q, sizeof (union T_primitives),
276*0Sstevel@tonic-gate 		    BPRI_HI, dummy_callback, NULL);
277*0Sstevel@tonic-gate 		if (!qwait_sig(q)) {
278*0Sstevel@tonic-gate 			qunbufcall(q, id);
279*0Sstevel@tonic-gate 			error = EINTR;
280*0Sstevel@tonic-gate 			goto fail;
281*0Sstevel@tonic-gate 		}
282*0Sstevel@tonic-gate 		qunbufcall(q, id);
283*0Sstevel@tonic-gate 	}
284*0Sstevel@tonic-gate 	bp->b_datap->db_type = M_PROTO;
285*0Sstevel@tonic-gate 	bp->b_wptr = bp->b_rptr + sizeof (union T_primitives);
286*0Sstevel@tonic-gate 	tp = (union T_primitives *)bp->b_rptr;
287*0Sstevel@tonic-gate 	tp->type = T_DATA_REQ;
288*0Sstevel@tonic-gate 	tp->data_req.MORE_flag = 0;
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	putnext(q, bp);
291*0Sstevel@tonic-gate 	return (0);
292*0Sstevel@tonic-gate fail:
293*0Sstevel@tonic-gate 	qprocsoff(q);
294*0Sstevel@tonic-gate 	if (rmip->unbind_mp != NULL) {
295*0Sstevel@tonic-gate 		freemsg(rmip->unbind_mp);
296*0Sstevel@tonic-gate 	}
297*0Sstevel@tonic-gate 	kmem_free(rmip, sizeof (struct rlmod_info));
298*0Sstevel@tonic-gate 	q->q_ptr = NULL;
299*0Sstevel@tonic-gate 	WR(q)->q_ptr = NULL;
300*0Sstevel@tonic-gate 	return (error);
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate /*
305*0Sstevel@tonic-gate  * rlmodclose - This routine gets called when the module
306*0Sstevel@tonic-gate  *	gets popped off of the stream.
307*0Sstevel@tonic-gate  */
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate /*ARGSUSED*/
310*0Sstevel@tonic-gate static int
rlmodclose(queue_t * q,int flag,cred_t * credp)311*0Sstevel@tonic-gate rlmodclose(queue_t *q, int flag, cred_t *credp)
312*0Sstevel@tonic-gate {
313*0Sstevel@tonic-gate 	struct rlmod_info   *rmip = (struct rlmod_info *)q->q_ptr;
314*0Sstevel@tonic-gate 	mblk_t  *mp;
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	/*
317*0Sstevel@tonic-gate 	 * Flush any write-side data downstream.  Ignoring flow
318*0Sstevel@tonic-gate 	 * control at this point is known to be safe because the
319*0Sstevel@tonic-gate 	 * M_HANGUP below poisons the stream such that no modules can
320*0Sstevel@tonic-gate 	 * be pushed again.
321*0Sstevel@tonic-gate 	 */
322*0Sstevel@tonic-gate 	while (mp = getq(WR(q)))
323*0Sstevel@tonic-gate 		putnext(WR(q), mp);
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	/* Poison the stream head so that we can't be pushed again. */
326*0Sstevel@tonic-gate 	(void) putnextctl(q, M_HANGUP);
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	qprocsoff(q);
329*0Sstevel@tonic-gate 	if (rmip->wbufcid) {
330*0Sstevel@tonic-gate 		qunbufcall(q, rmip->wbufcid);
331*0Sstevel@tonic-gate 		rmip->wbufcid = 0;
332*0Sstevel@tonic-gate 	}
333*0Sstevel@tonic-gate 	if (rmip->rbufcid) {
334*0Sstevel@tonic-gate 		qunbufcall(q, rmip->rbufcid);
335*0Sstevel@tonic-gate 		rmip->rbufcid = 0;
336*0Sstevel@tonic-gate 	}
337*0Sstevel@tonic-gate 	if (rmip->wtimoutid) {
338*0Sstevel@tonic-gate 		(void) quntimeout(q, rmip->wtimoutid);
339*0Sstevel@tonic-gate 		rmip->wtimoutid = 0;
340*0Sstevel@tonic-gate 	}
341*0Sstevel@tonic-gate 	if (rmip->rtimoutid) {
342*0Sstevel@tonic-gate 		(void) quntimeout(q, rmip->rtimoutid);
343*0Sstevel@tonic-gate 		rmip->rtimoutid = 0;
344*0Sstevel@tonic-gate 	}
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	if (rmip->unbind_mp != NULL) {
347*0Sstevel@tonic-gate 		freemsg(rmip->unbind_mp);
348*0Sstevel@tonic-gate 	}
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	if (rmip->wndw_sz_hd_mp != NULL) {
351*0Sstevel@tonic-gate 		freemsg(rmip->wndw_sz_hd_mp);
352*0Sstevel@tonic-gate 	}
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	kmem_free(q->q_ptr, sizeof (struct rlmod_info));
355*0Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
356*0Sstevel@tonic-gate 	return (0);
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate /*
360*0Sstevel@tonic-gate  * rlmodrput - Module read queue put procedure.
361*0Sstevel@tonic-gate  *	This is called from the module or
362*0Sstevel@tonic-gate  *	driver downstream.
363*0Sstevel@tonic-gate  */
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate static int
rlmodrput(queue_t * q,mblk_t * mp)366*0Sstevel@tonic-gate rlmodrput(queue_t *q, mblk_t *mp)
367*0Sstevel@tonic-gate {
368*0Sstevel@tonic-gate 	struct rlmod_info    *rmip = (struct rlmod_info *)q->q_ptr;
369*0Sstevel@tonic-gate 	union T_primitives *tip;
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 	TRACE_2(TR_FAC_RLOGINP, TR_RLOGINP_RPUT_IN, "rlmodrput start: "
372*0Sstevel@tonic-gate 	    "q %p, mp %p", q, mp);
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	/* if low (normal) priority... */
376*0Sstevel@tonic-gate 	if ((mp->b_datap->db_type < QPCTL) &&
377*0Sstevel@tonic-gate 	    /* ...and data is already queued... */
378*0Sstevel@tonic-gate 	    ((q->q_first) ||
379*0Sstevel@tonic-gate 		/* ...or currently disabled and this is M_DATA... */
380*0Sstevel@tonic-gate 		((rmip->flags & RL_DISABLED) &&
381*0Sstevel@tonic-gate 		    (mp->b_datap->db_type == M_DATA)))) {
382*0Sstevel@tonic-gate 		/* ...delay delivery of the message */
383*0Sstevel@tonic-gate 		(void) putq(q, mp);
384*0Sstevel@tonic-gate 		TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_RPUT_OUT,
385*0Sstevel@tonic-gate 		    "rlmodrput end: q %p, mp %p, %s", q, mp, "flow");
386*0Sstevel@tonic-gate 		return (0);
387*0Sstevel@tonic-gate 	}
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	case M_PROTO:
392*0Sstevel@tonic-gate 	case M_PCPROTO:
393*0Sstevel@tonic-gate 		tip = (union T_primitives *)mp->b_rptr;
394*0Sstevel@tonic-gate 		switch (tip->type) {
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 		case T_ORDREL_IND:
397*0Sstevel@tonic-gate 		case T_DISCON_IND:
398*0Sstevel@tonic-gate 			/* Make into M_HANGUP and putnext */
399*0Sstevel@tonic-gate 			mp->b_datap->db_type = M_HANGUP;
400*0Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr;
401*0Sstevel@tonic-gate 			if (mp->b_cont) {
402*0Sstevel@tonic-gate 				freemsg(mp->b_cont);
403*0Sstevel@tonic-gate 				mp->b_cont = NULL;
404*0Sstevel@tonic-gate 			}
405*0Sstevel@tonic-gate 			/*
406*0Sstevel@tonic-gate 			 * If we haven't already, send T_UNBIND_REQ to prevent
407*0Sstevel@tonic-gate 			 * TCP from going into "BOUND" state and locking up the
408*0Sstevel@tonic-gate 			 * port.
409*0Sstevel@tonic-gate 			 */
410*0Sstevel@tonic-gate 			if (tip->type == T_DISCON_IND && rmip->unbind_mp !=
411*0Sstevel@tonic-gate 			    NULL) {
412*0Sstevel@tonic-gate 				putnext(q, mp);
413*0Sstevel@tonic-gate 				qreply(q, rmip->unbind_mp);
414*0Sstevel@tonic-gate 				rmip->unbind_mp = NULL;
415*0Sstevel@tonic-gate 			} else {
416*0Sstevel@tonic-gate 				putnext(q, mp);
417*0Sstevel@tonic-gate 			}
418*0Sstevel@tonic-gate 			break;
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 		/*
421*0Sstevel@tonic-gate 		 * We only get T_OK_ACK when we issue the unbind, and it can
422*0Sstevel@tonic-gate 		 * be ignored safely.
423*0Sstevel@tonic-gate 		 */
424*0Sstevel@tonic-gate 		case T_OK_ACK:
425*0Sstevel@tonic-gate 			ASSERT(rmip->unbind_mp == NULL);
426*0Sstevel@tonic-gate 			freemsg(mp);
427*0Sstevel@tonic-gate 			break;
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 		default:
430*0Sstevel@tonic-gate 			cmn_err(CE_NOTE,
431*0Sstevel@tonic-gate 			    "rlmodrput: got 0x%x type M_PROTO/M_PCPROTO msg",
432*0Sstevel@tonic-gate 			    tip->type);
433*0Sstevel@tonic-gate 			freemsg(mp);
434*0Sstevel@tonic-gate 		}
435*0Sstevel@tonic-gate 		break;
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	case M_DATA:
438*0Sstevel@tonic-gate 		if (canputnext(q) && q->q_first == NULL) {
439*0Sstevel@tonic-gate 			(void) rlmodrmsg(q, mp);
440*0Sstevel@tonic-gate 		} else {
441*0Sstevel@tonic-gate 			(void) putq(q, mp);
442*0Sstevel@tonic-gate 		}
443*0Sstevel@tonic-gate 		break;
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	case M_FLUSH:
446*0Sstevel@tonic-gate 		/*
447*0Sstevel@tonic-gate 		 * Since M_FLUSH came from TCP, we mark it bound for
448*0Sstevel@tonic-gate 		 * daemon, not tty.  This only happens when TCP expects
449*0Sstevel@tonic-gate 		 * to do a connection reset.
450*0Sstevel@tonic-gate 		 */
451*0Sstevel@tonic-gate 		mp->b_flag |= MSGMARK;
452*0Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR)
453*0Sstevel@tonic-gate 			flushq(q, FLUSHALL);
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 		putnext(q, mp);
456*0Sstevel@tonic-gate 		break;
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	case M_PCSIG:
459*0Sstevel@tonic-gate 	case M_ERROR:
460*0Sstevel@tonic-gate 	case M_IOCACK:
461*0Sstevel@tonic-gate 	case M_IOCNAK:
462*0Sstevel@tonic-gate 	case M_SETOPTS:
463*0Sstevel@tonic-gate 		if (mp->b_datap->db_type <= QPCTL && !canputnext(q))
464*0Sstevel@tonic-gate 			(void) putq(q, mp);
465*0Sstevel@tonic-gate 		else
466*0Sstevel@tonic-gate 			putnext(q, mp);
467*0Sstevel@tonic-gate 		break;
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	default:
470*0Sstevel@tonic-gate #ifdef DEBUG
471*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rlmodrput: unexpected msg type 0x%x",
472*0Sstevel@tonic-gate 		    mp->b_datap->db_type);
473*0Sstevel@tonic-gate #endif
474*0Sstevel@tonic-gate 		freemsg(mp);
475*0Sstevel@tonic-gate 	}
476*0Sstevel@tonic-gate 	TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_RPUT_OUT, "rlmodrput end: q %p, "
477*0Sstevel@tonic-gate 		"mp %p, %s", q, mp, "done");
478*0Sstevel@tonic-gate 	return (0);
479*0Sstevel@tonic-gate }
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate /*
482*0Sstevel@tonic-gate  * rlmodrsrv - module read service procedure
483*0Sstevel@tonic-gate  */
484*0Sstevel@tonic-gate static int
rlmodrsrv(queue_t * q)485*0Sstevel@tonic-gate rlmodrsrv(queue_t *q)
486*0Sstevel@tonic-gate {
487*0Sstevel@tonic-gate 	mblk_t	*mp;
488*0Sstevel@tonic-gate 	struct rlmod_info    *rmip = (struct rlmod_info *)q->q_ptr;
489*0Sstevel@tonic-gate 	union T_primitives *tip;
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	TRACE_1(TR_FAC_RLOGINP, TR_RLOGINP_RSRV_IN, "rlmodrsrv start: "
492*0Sstevel@tonic-gate 	    "q %p", q);
493*0Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
496*0Sstevel@tonic-gate 		case M_DATA:
497*0Sstevel@tonic-gate 			if (rmip->flags & RL_DISABLED) {
498*0Sstevel@tonic-gate 				(void) putbq(q, mp);
499*0Sstevel@tonic-gate 				TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_RSRV_OUT,
500*0Sstevel@tonic-gate 					"rlmodrsrv end: q %p, mp %p, %s", q, mp,
501*0Sstevel@tonic-gate 					"disabled");
502*0Sstevel@tonic-gate 				return (0);
503*0Sstevel@tonic-gate 			}
504*0Sstevel@tonic-gate 			if (!canputnext(q)) {
505*0Sstevel@tonic-gate 				(void) putbq(q, mp);
506*0Sstevel@tonic-gate 				TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_RSRV_OUT,
507*0Sstevel@tonic-gate 				    "rlmodrsrv end: q %p, mp %p, %s",
508*0Sstevel@tonic-gate 				    q, mp, "!canputnext");
509*0Sstevel@tonic-gate 				return (0);
510*0Sstevel@tonic-gate 			}
511*0Sstevel@tonic-gate 			if (!rlmodrmsg(q, mp)) {
512*0Sstevel@tonic-gate 				TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_RSRV_OUT,
513*0Sstevel@tonic-gate 				    "rlmodrsrv end: q %p, mp %p, %s",
514*0Sstevel@tonic-gate 				    q, mp, "!rlmodrmsg");
515*0Sstevel@tonic-gate 				return (0);
516*0Sstevel@tonic-gate 			}
517*0Sstevel@tonic-gate 			break;
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 		case M_PROTO:
520*0Sstevel@tonic-gate 			tip = (union T_primitives *)mp->b_rptr;
521*0Sstevel@tonic-gate 			switch (tip->type) {
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 			case T_ORDREL_IND:
524*0Sstevel@tonic-gate 			case T_DISCON_IND:
525*0Sstevel@tonic-gate 				/* Make into M_HANGUP and putnext */
526*0Sstevel@tonic-gate 				mp->b_datap->db_type = M_HANGUP;
527*0Sstevel@tonic-gate 				mp->b_wptr = mp->b_rptr;
528*0Sstevel@tonic-gate 				if (mp->b_cont) {
529*0Sstevel@tonic-gate 					freemsg(mp->b_cont);
530*0Sstevel@tonic-gate 					mp->b_cont = NULL;
531*0Sstevel@tonic-gate 				}
532*0Sstevel@tonic-gate 				/*
533*0Sstevel@tonic-gate 				 * If we haven't already, send T_UNBIND_REQ
534*0Sstevel@tonic-gate 				 * to prevent TCP from going into "BOUND"
535*0Sstevel@tonic-gate 				 * state and locking up the port.
536*0Sstevel@tonic-gate 				 */
537*0Sstevel@tonic-gate 				if (tip->type == T_DISCON_IND &&
538*0Sstevel@tonic-gate 				    rmip->unbind_mp != NULL) {
539*0Sstevel@tonic-gate 					putnext(q, mp);
540*0Sstevel@tonic-gate 					qreply(q, rmip->unbind_mp);
541*0Sstevel@tonic-gate 					rmip->unbind_mp = NULL;
542*0Sstevel@tonic-gate 				} else {
543*0Sstevel@tonic-gate 					putnext(q, mp);
544*0Sstevel@tonic-gate 				}
545*0Sstevel@tonic-gate 				break;
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 			/*
548*0Sstevel@tonic-gate 			 * We only get T_OK_ACK when we issue the unbind, and
549*0Sstevel@tonic-gate 			 * it can be ignored safely.
550*0Sstevel@tonic-gate 			 */
551*0Sstevel@tonic-gate 			case T_OK_ACK:
552*0Sstevel@tonic-gate 				ASSERT(rmip->unbind_mp == NULL);
553*0Sstevel@tonic-gate 				freemsg(mp);
554*0Sstevel@tonic-gate 				break;
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 			default:
557*0Sstevel@tonic-gate 				cmn_err(CE_NOTE,
558*0Sstevel@tonic-gate 				    "rlmodrsrv: got 0x%x type PROTO msg",
559*0Sstevel@tonic-gate 				    tip->type);
560*0Sstevel@tonic-gate 				freemsg(mp);
561*0Sstevel@tonic-gate 			}
562*0Sstevel@tonic-gate 			break;
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 		case M_SETOPTS:
565*0Sstevel@tonic-gate 			if (!canputnext(q)) {
566*0Sstevel@tonic-gate 				(void) putbq(q, mp);
567*0Sstevel@tonic-gate 				TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_RSRV_OUT,
568*0Sstevel@tonic-gate 				    "rlmodrsrv end: q %p, mp %p, %s",
569*0Sstevel@tonic-gate 				    q, mp, "!canputnext M_SETOPTS");
570*0Sstevel@tonic-gate 				return (0);
571*0Sstevel@tonic-gate 			}
572*0Sstevel@tonic-gate 			putnext(q, mp);
573*0Sstevel@tonic-gate 			break;
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 		default:
576*0Sstevel@tonic-gate #ifdef DEBUG
577*0Sstevel@tonic-gate 			cmn_err(CE_NOTE,
578*0Sstevel@tonic-gate 			    "rlmodrsrv: unexpected msg type 0x%x",
579*0Sstevel@tonic-gate 			    mp->b_datap->db_type);
580*0Sstevel@tonic-gate #endif
581*0Sstevel@tonic-gate 			freemsg(mp);
582*0Sstevel@tonic-gate 		}
583*0Sstevel@tonic-gate 	}
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_RSRV_OUT, "rlmodrsrv end: q %p, "
586*0Sstevel@tonic-gate 	    "mp %p, %s", q, mp, "empty");
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 	return (0);
589*0Sstevel@tonic-gate }
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate /*
592*0Sstevel@tonic-gate  * rlmodwput - Module write queue put procedure.
593*0Sstevel@tonic-gate  *	All non-zero messages are send downstream unchanged
594*0Sstevel@tonic-gate  */
595*0Sstevel@tonic-gate static int
rlmodwput(queue_t * q,mblk_t * mp)596*0Sstevel@tonic-gate rlmodwput(queue_t *q, mblk_t *mp)
597*0Sstevel@tonic-gate {
598*0Sstevel@tonic-gate 	char cntl;
599*0Sstevel@tonic-gate 	struct rlmod_info *rmip = (struct rlmod_info *)q->q_ptr;
600*0Sstevel@tonic-gate 	mblk_t *tmpmp;
601*0Sstevel@tonic-gate 	int rw;
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate 	TRACE_2(TR_FAC_RLOGINP, TR_RLOGINP_WPUT_IN, "rlmodwput start: "
604*0Sstevel@tonic-gate 	    "q %p, mp %p", q, mp);
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	if (rmip->rl_expdat) {
607*0Sstevel@tonic-gate 		/*
608*0Sstevel@tonic-gate 		 * call make_expmblk to create an expedited
609*0Sstevel@tonic-gate 		 * message block.
610*0Sstevel@tonic-gate 		 */
611*0Sstevel@tonic-gate 		cntl = rmip->oobdata[0] | TIOCPKT_FLUSHWRITE;
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 		if (!canputnext(q)) {
614*0Sstevel@tonic-gate 			(void) putq(q, mp);
615*0Sstevel@tonic-gate 			TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_WPUT_OUT,
616*0Sstevel@tonic-gate 			    "rlmodwput end: q %p, mp %p, %s",
617*0Sstevel@tonic-gate 			    q, mp, "expdata && !canputnext");
618*0Sstevel@tonic-gate 			return (0);
619*0Sstevel@tonic-gate 		}
620*0Sstevel@tonic-gate 		if ((tmpmp = make_expmblk(cntl))) {
621*0Sstevel@tonic-gate 			putnext(q, tmpmp);
622*0Sstevel@tonic-gate 			rmip->rl_expdat = 0;
623*0Sstevel@tonic-gate 		} else {
624*0Sstevel@tonic-gate 			recover1(q, sizeof (mblk_t)); /* XXX.sparker */
625*0Sstevel@tonic-gate 		}
626*0Sstevel@tonic-gate 	}
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 	if ((q->q_first || rmip->rl_expdat) && mp->b_datap->db_type < QPCTL) {
629*0Sstevel@tonic-gate 		(void) putq(q, mp);
630*0Sstevel@tonic-gate 		TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_WPUT_OUT, "rlmodwput end: "
631*0Sstevel@tonic-gate 		    "q %p, mp %p, %s", q, mp, "queued data");
632*0Sstevel@tonic-gate 		return (0);
633*0Sstevel@tonic-gate 	}
634*0Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	case M_DATA:
637*0Sstevel@tonic-gate 		if (!canputnext(q))
638*0Sstevel@tonic-gate 			(void) putq(q, mp);
639*0Sstevel@tonic-gate 		else
640*0Sstevel@tonic-gate 			putnext(q, mp);
641*0Sstevel@tonic-gate 		break;
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	case M_FLUSH:
644*0Sstevel@tonic-gate 		/*
645*0Sstevel@tonic-gate 		 * We must take care to create and forward out-of-band data
646*0Sstevel@tonic-gate 		 * indicating the flush to the far side.
647*0Sstevel@tonic-gate 		 */
648*0Sstevel@tonic-gate 		rw = *mp->b_rptr;
649*0Sstevel@tonic-gate 		*mp->b_rptr &= ~FLUSHW;
650*0Sstevel@tonic-gate 		qreply(q, mp);
651*0Sstevel@tonic-gate 		if (rw & FLUSHW) {
652*0Sstevel@tonic-gate 			/*
653*0Sstevel@tonic-gate 			 * Since all rlogin protocol data is sent in this
654*0Sstevel@tonic-gate 			 * direction as urgent data, and TCP does not flush
655*0Sstevel@tonic-gate 			 * urgent data, it is okay to actually forward this
656*0Sstevel@tonic-gate 			 * flush.  (telmod cannot.)
657*0Sstevel@tonic-gate 			 */
658*0Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
659*0Sstevel@tonic-gate 			/*
660*0Sstevel@tonic-gate 			 * The putnextctl1() call can only fail if we're
661*0Sstevel@tonic-gate 			 * out of memory.  Ideally, we might set a state
662*0Sstevel@tonic-gate 			 * bit and reschedule ourselves when memory
663*0Sstevel@tonic-gate 			 * becomes available, so we make sure not to miss
664*0Sstevel@tonic-gate 			 * sending the FLUSHW to TCP before the urgent
665*0Sstevel@tonic-gate 			 * byte.  Not doing this just means in some cases
666*0Sstevel@tonic-gate 			 * a bit more trash passes before the flush takes
667*0Sstevel@tonic-gate 			 * hold.
668*0Sstevel@tonic-gate 			 */
669*0Sstevel@tonic-gate 			(void) putnextctl1(q, M_FLUSH, FLUSHW);
670*0Sstevel@tonic-gate 			/*
671*0Sstevel@tonic-gate 			 * Notify peer of the write flush request.
672*0Sstevel@tonic-gate 			 */
673*0Sstevel@tonic-gate 			cntl = rmip->oobdata[0] | TIOCPKT_FLUSHWRITE;
674*0Sstevel@tonic-gate 			if (!canputnext(q)) {
675*0Sstevel@tonic-gate 				TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_WPUT_OUT,
676*0Sstevel@tonic-gate 				    "rlmodwput end: q %p, mp %p, %s",
677*0Sstevel@tonic-gate 				    q, mp, "flushw && !canputnext");
678*0Sstevel@tonic-gate 				return (0);
679*0Sstevel@tonic-gate 			}
680*0Sstevel@tonic-gate 			if ((mp = make_expmblk(cntl)) == NULL) {
681*0Sstevel@tonic-gate 				rmip->rl_expdat = 1;
682*0Sstevel@tonic-gate 				recover1(q, sizeof (mblk_t));
683*0Sstevel@tonic-gate 				TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_WPUT_OUT,
684*0Sstevel@tonic-gate 				    "rlmodwput end: q %p, mp %p, %s",
685*0Sstevel@tonic-gate 				    q, mp, "!make_expmblk");
686*0Sstevel@tonic-gate 				return (0);
687*0Sstevel@tonic-gate 			}
688*0Sstevel@tonic-gate 			putnext(q, mp);
689*0Sstevel@tonic-gate 		}
690*0Sstevel@tonic-gate 		break;
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	case M_IOCTL:
693*0Sstevel@tonic-gate 		if (!rlmodwioctl(q, mp))
694*0Sstevel@tonic-gate 			(void) putq(q, mp);
695*0Sstevel@tonic-gate 		break;
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	case M_PROTO:
698*0Sstevel@tonic-gate 		switch (((union T_primitives *)mp->b_rptr)->type) {
699*0Sstevel@tonic-gate 		case T_EXDATA_REQ:
700*0Sstevel@tonic-gate 		case T_ORDREL_REQ:
701*0Sstevel@tonic-gate 		case T_DISCON_REQ:
702*0Sstevel@tonic-gate 			putnext(q, mp);
703*0Sstevel@tonic-gate 			break;
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 		default:
706*0Sstevel@tonic-gate #ifdef DEBUG
707*0Sstevel@tonic-gate 			cmn_err(CE_NOTE,
708*0Sstevel@tonic-gate 			    "rlmodwput: unexpected TPI primitive 0x%x",
709*0Sstevel@tonic-gate 			    ((union T_primitives *)mp->b_rptr)->type);
710*0Sstevel@tonic-gate #endif
711*0Sstevel@tonic-gate 			freemsg(mp);
712*0Sstevel@tonic-gate 		}
713*0Sstevel@tonic-gate 		break;
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate 	case M_PCPROTO:
716*0Sstevel@tonic-gate 		if (((struct T_exdata_req *)mp->b_rptr)->PRIM_type ==
717*0Sstevel@tonic-gate 		    T_DISCON_REQ) {
718*0Sstevel@tonic-gate 			putnext(q, mp);
719*0Sstevel@tonic-gate 		} else {
720*0Sstevel@tonic-gate 			/* XXX.sparker Log unexpected message */
721*0Sstevel@tonic-gate 			freemsg(mp);
722*0Sstevel@tonic-gate 		}
723*0Sstevel@tonic-gate 		break;
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate 	default:
726*0Sstevel@tonic-gate #ifdef DEBUG
727*0Sstevel@tonic-gate 		cmn_err(CE_NOTE,
728*0Sstevel@tonic-gate 		    "rlmodwput: unexpected msg type 0x%x",
729*0Sstevel@tonic-gate 		    mp->b_datap->db_type);
730*0Sstevel@tonic-gate #endif
731*0Sstevel@tonic-gate 		freemsg(mp);
732*0Sstevel@tonic-gate 		break;
733*0Sstevel@tonic-gate 	}
734*0Sstevel@tonic-gate 	TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_WPUT_OUT, "rlmodwput end: "
735*0Sstevel@tonic-gate 	    "q %p, mp %p, %s", q, mp, "done");
736*0Sstevel@tonic-gate 	return (0);
737*0Sstevel@tonic-gate }
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate /*
740*0Sstevel@tonic-gate  * rlmodwsrv - module write service procedure
741*0Sstevel@tonic-gate  */
742*0Sstevel@tonic-gate static int
rlmodwsrv(queue_t * q)743*0Sstevel@tonic-gate rlmodwsrv(queue_t *q)
744*0Sstevel@tonic-gate {
745*0Sstevel@tonic-gate 	mblk_t	*mp, *tmpmp;
746*0Sstevel@tonic-gate 	char cntl;
747*0Sstevel@tonic-gate 	struct rlmod_info *rmip = (struct rlmod_info *)q->q_ptr;
748*0Sstevel@tonic-gate 
749*0Sstevel@tonic-gate 	TRACE_1(TR_FAC_RLOGINP, TR_RLOGINP_WSRV_IN, "rlmodwsrv "
750*0Sstevel@tonic-gate 	    "start: q %p", q);
751*0Sstevel@tonic-gate 	if (rmip->rl_expdat) {
752*0Sstevel@tonic-gate 		/*
753*0Sstevel@tonic-gate 		 * call make_expmblk to create an expedited
754*0Sstevel@tonic-gate 		 * message block.
755*0Sstevel@tonic-gate 		 */
756*0Sstevel@tonic-gate 		cntl = rmip->oobdata[0] | TIOCPKT_FLUSHWRITE;
757*0Sstevel@tonic-gate 		if (!canputnext(q)) {
758*0Sstevel@tonic-gate 			TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_WSRV_OUT,
759*0Sstevel@tonic-gate 			    "rlmodwsrv end: q %p, mp %p, %s",
760*0Sstevel@tonic-gate 			    q, NULL, "!canputnext && expdat");
761*0Sstevel@tonic-gate 			return (0);
762*0Sstevel@tonic-gate 		}
763*0Sstevel@tonic-gate 		if ((tmpmp = make_expmblk(cntl))) {
764*0Sstevel@tonic-gate 			putnext(q, tmpmp);
765*0Sstevel@tonic-gate 			rmip->rl_expdat = 0;
766*0Sstevel@tonic-gate 		} else {
767*0Sstevel@tonic-gate 			recover1(q, sizeof (mblk_t));
768*0Sstevel@tonic-gate 			TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_WSRV_OUT,
769*0Sstevel@tonic-gate 			    "rlmodwsrv end: q %p, mp %p, %s",
770*0Sstevel@tonic-gate 			    q, NULL, "!make_expmblk");
771*0Sstevel@tonic-gate 			return (0);
772*0Sstevel@tonic-gate 		}
773*0Sstevel@tonic-gate 	}
774*0Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
775*0Sstevel@tonic-gate 
776*0Sstevel@tonic-gate 		if (!canputnext(q) || rmip->rl_expdat) {
777*0Sstevel@tonic-gate 			(void) putbq(q, mp);
778*0Sstevel@tonic-gate 			TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_WSRV_OUT,
779*0Sstevel@tonic-gate 			    "rlmodwsrv end: q %p, mp %p, %s",
780*0Sstevel@tonic-gate 			    q, mp, "!canputnext || expdat");
781*0Sstevel@tonic-gate 			return (0);
782*0Sstevel@tonic-gate 		}
783*0Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_IOCTL) {
784*0Sstevel@tonic-gate 			if (!rlmodwioctl(q, mp)) {
785*0Sstevel@tonic-gate 				TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_WSRV_OUT,
786*0Sstevel@tonic-gate 				    "rlmodwsrv end: q %p, mp %p, %s",
787*0Sstevel@tonic-gate 				    q, mp, "!rlmodwioctl");
788*0Sstevel@tonic-gate 				(void) putbq(q, mp);
789*0Sstevel@tonic-gate 				return (0);
790*0Sstevel@tonic-gate 			}
791*0Sstevel@tonic-gate 			continue;
792*0Sstevel@tonic-gate 		}
793*0Sstevel@tonic-gate 		putnext(q, mp);
794*0Sstevel@tonic-gate 	}
795*0Sstevel@tonic-gate 	TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_WSRV_OUT, "rlmodwsrv end: q %p, "
796*0Sstevel@tonic-gate 	    "mp %p, %s", q, mp, "done");
797*0Sstevel@tonic-gate 	return (0);
798*0Sstevel@tonic-gate }
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate /*
801*0Sstevel@tonic-gate  * This routine returns a message block with an expedited
802*0Sstevel@tonic-gate  * data request
803*0Sstevel@tonic-gate  */
804*0Sstevel@tonic-gate static mblk_t *
make_expmblk(char cntl)805*0Sstevel@tonic-gate make_expmblk(char cntl)
806*0Sstevel@tonic-gate {
807*0Sstevel@tonic-gate 	mblk_t *mp;
808*0Sstevel@tonic-gate 	mblk_t *bp;
809*0Sstevel@tonic-gate 	struct T_exdata_req	*data_req;
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 	bp = allocb(sizeof (struct T_exdata_req), BPRI_MED);
812*0Sstevel@tonic-gate 	if (bp == NULL)
813*0Sstevel@tonic-gate 		return (NULL);
814*0Sstevel@tonic-gate 	if ((mp = allocb(sizeof (char), BPRI_MED)) == NULL) {
815*0Sstevel@tonic-gate 		freeb(bp);
816*0Sstevel@tonic-gate 		return (NULL);
817*0Sstevel@tonic-gate 	}
818*0Sstevel@tonic-gate 	bp->b_datap->db_type = M_PROTO;
819*0Sstevel@tonic-gate 	data_req = (struct T_exdata_req *)bp->b_rptr;
820*0Sstevel@tonic-gate 	data_req->PRIM_type = T_EXDATA_REQ;
821*0Sstevel@tonic-gate 	data_req->MORE_flag = 0;
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 	bp->b_wptr += sizeof (struct T_exdata_req);
824*0Sstevel@tonic-gate 	/*
825*0Sstevel@tonic-gate 	 * Send a 1 byte data message block with appropriate
826*0Sstevel@tonic-gate 	 * control character.
827*0Sstevel@tonic-gate 	 */
828*0Sstevel@tonic-gate 	mp->b_datap->db_type = M_DATA;
829*0Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + 1;
830*0Sstevel@tonic-gate 	(*(char *)(mp->b_rptr)) = cntl;
831*0Sstevel@tonic-gate 	bp->b_cont = mp;
832*0Sstevel@tonic-gate 	return (bp);
833*0Sstevel@tonic-gate }
834*0Sstevel@tonic-gate /*
835*0Sstevel@tonic-gate  * This routine parses M_DATA messages checking for window size protocol
836*0Sstevel@tonic-gate  * from a given message block.  It returns TRUE if no resource exhaustion
837*0Sstevel@tonic-gate  * conditions are found.  This is for use in the service procedure, which
838*0Sstevel@tonic-gate  * needs to know whether to continue, or stop processing the queue.
839*0Sstevel@tonic-gate  */
840*0Sstevel@tonic-gate static int
rlmodrmsg(queue_t * q,mblk_t * mp)841*0Sstevel@tonic-gate rlmodrmsg(queue_t *q, mblk_t *mp)
842*0Sstevel@tonic-gate {
843*0Sstevel@tonic-gate 	unsigned char *tmp, *tmp1;
844*0Sstevel@tonic-gate 	mblk_t	*newmp;
845*0Sstevel@tonic-gate 	size_t	sz;
846*0Sstevel@tonic-gate 	ssize_t	count, newcount = 0;
847*0Sstevel@tonic-gate 	struct	rlmod_info	*rmip = (struct rlmod_info *)q->q_ptr;
848*0Sstevel@tonic-gate 
849*0Sstevel@tonic-gate 	/*
850*0Sstevel@tonic-gate 	 * Eliminate any zero length messages here, so we don't filter EOFs
851*0Sstevel@tonic-gate 	 * accidentally.
852*0Sstevel@tonic-gate 	 */
853*0Sstevel@tonic-gate 	if (msgdsize(mp) == 0) {
854*0Sstevel@tonic-gate 		ASSERT(rmip->wndw_sz_hd_mp == NULL);
855*0Sstevel@tonic-gate 		goto out;
856*0Sstevel@tonic-gate 	}
857*0Sstevel@tonic-gate 	/*
858*0Sstevel@tonic-gate 	 * Check if we have stored a previous message block because a window
859*0Sstevel@tonic-gate 	 * update was split over TCP segments. If so, append the new one to
860*0Sstevel@tonic-gate 	 * the stored one and process the stored one as if it just arrived.
861*0Sstevel@tonic-gate 	 */
862*0Sstevel@tonic-gate 	if (rmip->wndw_sz_hd_mp != NULL) {
863*0Sstevel@tonic-gate 		linkb(rmip->wndw_sz_hd_mp, mp);
864*0Sstevel@tonic-gate 		mp = rmip->wndw_sz_hd_mp;
865*0Sstevel@tonic-gate 		rmip->wndw_sz_hd_mp = NULL;
866*0Sstevel@tonic-gate 	}
867*0Sstevel@tonic-gate 	newmp = mp;
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate 	while (mp) {
870*0Sstevel@tonic-gate 		tmp = mp->b_rptr;
871*0Sstevel@tonic-gate 		/*
872*0Sstevel@tonic-gate 		 * scan through the entire message block
873*0Sstevel@tonic-gate 		 */
874*0Sstevel@tonic-gate 		while (tmp < mp->b_wptr) {
875*0Sstevel@tonic-gate 			/*
876*0Sstevel@tonic-gate 			 * check for FF (rlogin magic escape sequence)
877*0Sstevel@tonic-gate 			 */
878*0Sstevel@tonic-gate 			if (tmp[0] == RLOGIN_MAGIC) {
879*0Sstevel@tonic-gate 				/*
880*0Sstevel@tonic-gate 				 * Update bytes read so far.
881*0Sstevel@tonic-gate 				 */
882*0Sstevel@tonic-gate 				count = newcount + tmp - mp->b_rptr;
883*0Sstevel@tonic-gate 				/*
884*0Sstevel@tonic-gate 				 * Pull together message chain in case
885*0Sstevel@tonic-gate 				 * window escape is split across blocks.
886*0Sstevel@tonic-gate 				 */
887*0Sstevel@tonic-gate 				if ((pullupmsg(newmp, -1)) == 0) {
888*0Sstevel@tonic-gate 					sz = msgdsize(newmp);
889*0Sstevel@tonic-gate 					recover(q, newmp, sz);
890*0Sstevel@tonic-gate 					return (NULL);
891*0Sstevel@tonic-gate 				}
892*0Sstevel@tonic-gate 				/*
893*0Sstevel@tonic-gate 				 * pullupmsg results in newmp consuming
894*0Sstevel@tonic-gate 				 * all message blocks in this chain, and
895*0Sstevel@tonic-gate 				 * therefor mp wants updating.
896*0Sstevel@tonic-gate 				 */
897*0Sstevel@tonic-gate 				mp = newmp;
898*0Sstevel@tonic-gate 
899*0Sstevel@tonic-gate 				/*
900*0Sstevel@tonic-gate 				 * adjust tmp to where we
901*0Sstevel@tonic-gate 				 * stopped - count keeps track
902*0Sstevel@tonic-gate 				 * of bytes read so far.
903*0Sstevel@tonic-gate 				 * reset newcount = 0.
904*0Sstevel@tonic-gate 				 */
905*0Sstevel@tonic-gate 				tmp = mp->b_rptr + count;
906*0Sstevel@tonic-gate 				newcount = 0;
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 				/*
909*0Sstevel@tonic-gate 				 * Use the variable tmp1 to compute where
910*0Sstevel@tonic-gate 				 * the end of the window escape (currently
911*0Sstevel@tonic-gate 				 * the only rlogin protocol sequence), then
912*0Sstevel@tonic-gate 				 * check to see if we got all those bytes.
913*0Sstevel@tonic-gate 				 */
914*0Sstevel@tonic-gate 				tmp1 = tmp + 4 + sizeof (struct winsize);
915*0Sstevel@tonic-gate 
916*0Sstevel@tonic-gate 				if (tmp1 > mp->b_wptr) {
917*0Sstevel@tonic-gate 					/*
918*0Sstevel@tonic-gate 					 * All the window escape bytes aren't
919*0Sstevel@tonic-gate 					 * in this TCP segment. Store this
920*0Sstevel@tonic-gate 					 * mblk to one side so we can append
921*0Sstevel@tonic-gate 					 * the rest of the escape to it when
922*0Sstevel@tonic-gate 					 * its segment arrives.
923*0Sstevel@tonic-gate 					 */
924*0Sstevel@tonic-gate 					rmip->wndw_sz_hd_mp = mp;
925*0Sstevel@tonic-gate 					return (TRUE);
926*0Sstevel@tonic-gate 				}
927*0Sstevel@tonic-gate 				/*
928*0Sstevel@tonic-gate 				 * check for FF FF s s pattern
929*0Sstevel@tonic-gate 				 */
930*0Sstevel@tonic-gate 				if ((tmp[1] == RLOGIN_MAGIC) &&
931*0Sstevel@tonic-gate 				    (tmp[2] == 's') && (tmp[3] == 's')) {
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate 					/*
934*0Sstevel@tonic-gate 					 * If rlwinsetup returns an error,
935*0Sstevel@tonic-gate 					 * we do recover with newmp which
936*0Sstevel@tonic-gate 					 * points to new chain of mblks after
937*0Sstevel@tonic-gate 					 * doing window control ioctls.
938*0Sstevel@tonic-gate 					 * rlwinsetup returns newmp which
939*0Sstevel@tonic-gate 					 * contains only data part.
940*0Sstevel@tonic-gate 					 * Note that buried inside rlwinsetup
941*0Sstevel@tonic-gate 					 * is where we do the putnext.
942*0Sstevel@tonic-gate 					 */
943*0Sstevel@tonic-gate 					if (rlwinsetup(q, mp, tmp) == NULL) {
944*0Sstevel@tonic-gate 						sz = msgdsize(mp);
945*0Sstevel@tonic-gate 						recover(q, mp, sz);
946*0Sstevel@tonic-gate 						return (NULL);
947*0Sstevel@tonic-gate 					}
948*0Sstevel@tonic-gate 					/*
949*0Sstevel@tonic-gate 					 * We have successfully consumed the
950*0Sstevel@tonic-gate 					 * window sequence, but rlwinsetup()
951*0Sstevel@tonic-gate 					 * and its children have moved memory
952*0Sstevel@tonic-gate 					 * up underneath us.  This means that
953*0Sstevel@tonic-gate 					 * the byte underneath *tmp has not
954*0Sstevel@tonic-gate 					 * been scanned now.  We will now need
955*0Sstevel@tonic-gate 					 * to rescan it.
956*0Sstevel@tonic-gate 					 */
957*0Sstevel@tonic-gate 					continue;
958*0Sstevel@tonic-gate 				}
959*0Sstevel@tonic-gate 			}
960*0Sstevel@tonic-gate 			tmp++;
961*0Sstevel@tonic-gate 		}
962*0Sstevel@tonic-gate 		/*
963*0Sstevel@tonic-gate 		 * bump newcount to include size of this particular block.
964*0Sstevel@tonic-gate 		 */
965*0Sstevel@tonic-gate 		newcount += (mp->b_wptr - mp->b_rptr);
966*0Sstevel@tonic-gate 		mp = mp->b_cont;
967*0Sstevel@tonic-gate 	}
968*0Sstevel@tonic-gate 	/*
969*0Sstevel@tonic-gate 	 * If we trimmed the message down to nothing to forward, don't
970*0Sstevel@tonic-gate 	 * send any M_DATA message.  (Don't want to send EOF!)
971*0Sstevel@tonic-gate 	 */
972*0Sstevel@tonic-gate 	if (msgdsize(newmp) == 0) {
973*0Sstevel@tonic-gate 		freemsg(newmp);
974*0Sstevel@tonic-gate 		newmp = NULL;
975*0Sstevel@tonic-gate 	}
976*0Sstevel@tonic-gate out:
977*0Sstevel@tonic-gate 	if (newmp) {
978*0Sstevel@tonic-gate 		if (!canputnext(q)) {
979*0Sstevel@tonic-gate 			(void) putbq(q, newmp);
980*0Sstevel@tonic-gate 			return (NULL);
981*0Sstevel@tonic-gate 		} else {
982*0Sstevel@tonic-gate 			putnext(q, newmp);
983*0Sstevel@tonic-gate 		}
984*0Sstevel@tonic-gate 	}
985*0Sstevel@tonic-gate 	return (TRUE);
986*0Sstevel@tonic-gate }
987*0Sstevel@tonic-gate 
988*0Sstevel@tonic-gate 
989*0Sstevel@tonic-gate /*
990*0Sstevel@tonic-gate  * This routine is called to handle window size changes.
991*0Sstevel@tonic-gate  * The routine returns 1 on success and 0 on error (allocb failure).
992*0Sstevel@tonic-gate  */
993*0Sstevel@tonic-gate static int
rlwinctl(queue_t * q,mblk_t * mp)994*0Sstevel@tonic-gate rlwinctl(queue_t *q, mblk_t *mp)
995*0Sstevel@tonic-gate {
996*0Sstevel@tonic-gate 	mblk_t	*rl_msgp;
997*0Sstevel@tonic-gate 	struct	iocblk	*iocbp;
998*0Sstevel@tonic-gate 	struct	rlmod_info	*rmip = (struct rlmod_info *)q->q_ptr;
999*0Sstevel@tonic-gate 
1000*0Sstevel@tonic-gate 	TRACE_2(TR_FAC_RLOGINP, TR_RLOGINP_WINCTL_IN, "rlwinctl start: q %p, "
1001*0Sstevel@tonic-gate 	    "mp %p", q, mp);
1002*0Sstevel@tonic-gate 
1003*0Sstevel@tonic-gate 	rmip->oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */
1004*0Sstevel@tonic-gate 
1005*0Sstevel@tonic-gate 	if ((rl_msgp = mkiocb(TIOCSWINSZ)) == NULL) {
1006*0Sstevel@tonic-gate 		TRACE_2(TR_FAC_RLOGINP, TR_RLOGINP_WINCTL_OUT, "rlwinctl end: "
1007*0Sstevel@tonic-gate 		    "q %p, mp %p, allocb failed", q, mp);
1008*0Sstevel@tonic-gate 		return (0);
1009*0Sstevel@tonic-gate 	}
1010*0Sstevel@tonic-gate 
1011*0Sstevel@tonic-gate 	/*
1012*0Sstevel@tonic-gate 	 * create an M_IOCTL message type.
1013*0Sstevel@tonic-gate 	 */
1014*0Sstevel@tonic-gate 	rl_msgp->b_cont = mp;
1015*0Sstevel@tonic-gate 	iocbp = (struct iocblk *)rl_msgp->b_rptr;
1016*0Sstevel@tonic-gate 	iocbp->ioc_count = msgdsize(mp);
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate 	putnext(q, rl_msgp);
1019*0Sstevel@tonic-gate 	TRACE_2(TR_FAC_RLOGINP, TR_RLOGINP_WINCTL_OUT, "rlwinctl end: "
1020*0Sstevel@tonic-gate 	    "q %p, mp %p, done", q, mp);
1021*0Sstevel@tonic-gate 	return (1);
1022*0Sstevel@tonic-gate }
1023*0Sstevel@tonic-gate 
1024*0Sstevel@tonic-gate /*
1025*0Sstevel@tonic-gate  * This routine sets up window size change protocol.
1026*0Sstevel@tonic-gate  * The routine returns the new mblk after issuing rlwinctl
1027*0Sstevel@tonic-gate  * for window size changes. New mblk contains only data part
1028*0Sstevel@tonic-gate  * of the message block. The routine returns 0 on error.
1029*0Sstevel@tonic-gate  */
1030*0Sstevel@tonic-gate static mblk_t *
rlwinsetup(queue_t * q,mblk_t * mp,unsigned char * blk)1031*0Sstevel@tonic-gate rlwinsetup(queue_t *q, mblk_t *mp, unsigned char *blk)
1032*0Sstevel@tonic-gate {
1033*0Sstevel@tonic-gate 	mblk_t		*mp1;
1034*0Sstevel@tonic-gate 	unsigned char	*jmpmp;
1035*0Sstevel@tonic-gate 	ssize_t		left = 0;
1036*0Sstevel@tonic-gate 	struct winsize	win;
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate 	/*
1039*0Sstevel@tonic-gate 	 * Set jmpmp to where to jump, to get just past the end of the
1040*0Sstevel@tonic-gate 	 * window size protocol sequence.
1041*0Sstevel@tonic-gate 	 */
1042*0Sstevel@tonic-gate 	jmpmp = (blk + 4 + sizeof (struct winsize));
1043*0Sstevel@tonic-gate 	left = mp->b_wptr - jmpmp;
1044*0Sstevel@tonic-gate 
1045*0Sstevel@tonic-gate 	if ((mp1 = allocb(sizeof (struct winsize), BPRI_MED)) == NULL)
1046*0Sstevel@tonic-gate 		return (0);
1047*0Sstevel@tonic-gate 	mp1->b_datap->db_type = M_DATA;
1048*0Sstevel@tonic-gate 	mp1->b_wptr = mp1->b_rptr + sizeof (struct winsize);
1049*0Sstevel@tonic-gate 	bcopy(blk + 4, &win, sizeof (struct winsize));
1050*0Sstevel@tonic-gate 	win.ws_row = ntohs(win.ws_row);
1051*0Sstevel@tonic-gate 	win.ws_col = ntohs(win.ws_col);
1052*0Sstevel@tonic-gate 	win.ws_xpixel = ntohs(win.ws_xpixel);
1053*0Sstevel@tonic-gate 	win.ws_ypixel = ntohs(win.ws_ypixel);
1054*0Sstevel@tonic-gate 	bcopy(&win, mp1->b_rptr, sizeof (struct winsize));
1055*0Sstevel@tonic-gate 
1056*0Sstevel@tonic-gate 	if ((rlwinctl(q, mp1)) == NULL) {
1057*0Sstevel@tonic-gate 		freeb(mp1);
1058*0Sstevel@tonic-gate 		return (0);
1059*0Sstevel@tonic-gate 	}
1060*0Sstevel@tonic-gate 	if (left > 0) {
1061*0Sstevel@tonic-gate 		/*
1062*0Sstevel@tonic-gate 		 * Must delete the window size protocol sequence.  We do
1063*0Sstevel@tonic-gate 		 * this by sliding all the stuff after the sequence (jmpmp)
1064*0Sstevel@tonic-gate 		 * to where the sequence itself began (blk).
1065*0Sstevel@tonic-gate 		 */
1066*0Sstevel@tonic-gate 		bcopy(jmpmp, blk, left);
1067*0Sstevel@tonic-gate 		mp->b_wptr = blk + left;
1068*0Sstevel@tonic-gate 	} else
1069*0Sstevel@tonic-gate 		mp->b_wptr = blk;
1070*0Sstevel@tonic-gate 	return (mp);
1071*0Sstevel@tonic-gate }
1072*0Sstevel@tonic-gate 
1073*0Sstevel@tonic-gate /*
1074*0Sstevel@tonic-gate  * When an ioctl changes software flow control on the tty, we must notify
1075*0Sstevel@tonic-gate  * the rlogin client, so it can adjust its behavior appropriately.  This
1076*0Sstevel@tonic-gate  * routine, called from either the put or service routine, determines if
1077*0Sstevel@tonic-gate  * the flow handling has changed.  If so, it tries to send the indication
1078*0Sstevel@tonic-gate  * to the client.  It returns true or false depending upon whether the
1079*0Sstevel@tonic-gate  * message was fully processed.  If it wasn't fully processed it queues
1080*0Sstevel@tonic-gate  * the message for retry later when resources
1081*0Sstevel@tonic-gate  * (allocb/canputnext) are available.
1082*0Sstevel@tonic-gate  */
1083*0Sstevel@tonic-gate static boolean_t
tty_flow(queue_t * q,struct rlmod_info * rmip,mblk_t * mp)1084*0Sstevel@tonic-gate tty_flow(queue_t *q, struct rlmod_info *rmip, mblk_t *mp)
1085*0Sstevel@tonic-gate {
1086*0Sstevel@tonic-gate 	struct iocblk *ioc;
1087*0Sstevel@tonic-gate 	struct termios *tp;
1088*0Sstevel@tonic-gate 	struct termio *ti;
1089*0Sstevel@tonic-gate 	int stop, ixon;
1090*0Sstevel@tonic-gate 	mblk_t *tmpmp;
1091*0Sstevel@tonic-gate 	char cntl;
1092*0Sstevel@tonic-gate 	int error;
1093*0Sstevel@tonic-gate 
1094*0Sstevel@tonic-gate 	ioc = (struct iocblk *)mp->b_rptr;
1095*0Sstevel@tonic-gate 	switch (ioc->ioc_cmd) {
1096*0Sstevel@tonic-gate 
1097*0Sstevel@tonic-gate 	/*
1098*0Sstevel@tonic-gate 	 * If it is a tty ioctl, save the output flow
1099*0Sstevel@tonic-gate 	 * control flag and the start and stop flow control
1100*0Sstevel@tonic-gate 	 * characters if they are available.
1101*0Sstevel@tonic-gate 	 */
1102*0Sstevel@tonic-gate 	case TCSETS:
1103*0Sstevel@tonic-gate 	case TCSETSW:
1104*0Sstevel@tonic-gate 	case TCSETSF:
1105*0Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct termios));
1106*0Sstevel@tonic-gate 		if (error != 0) {
1107*0Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
1108*0Sstevel@tonic-gate 			return (B_TRUE);
1109*0Sstevel@tonic-gate 		}
1110*0Sstevel@tonic-gate 		tp = (struct termios *)(mp->b_cont->b_rptr);
1111*0Sstevel@tonic-gate 		rmip->stopc = tp->c_cc[VSTOP];
1112*0Sstevel@tonic-gate 		rmip->startc = tp->c_cc[VSTART];
1113*0Sstevel@tonic-gate 		ixon = tp->c_iflag & IXON;
1114*0Sstevel@tonic-gate 		break;
1115*0Sstevel@tonic-gate 
1116*0Sstevel@tonic-gate 	case TCSETA:
1117*0Sstevel@tonic-gate 	case TCSETAW:
1118*0Sstevel@tonic-gate 	case TCSETAF:
1119*0Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct termio));
1120*0Sstevel@tonic-gate 		if (error != 0) {
1121*0Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
1122*0Sstevel@tonic-gate 			return (B_TRUE);
1123*0Sstevel@tonic-gate 		}
1124*0Sstevel@tonic-gate 		ti = (struct termio *)(mp->b_cont->b_rptr);
1125*0Sstevel@tonic-gate 		ixon = ti->c_iflag & IXON;
1126*0Sstevel@tonic-gate 		break;
1127*0Sstevel@tonic-gate 
1128*0Sstevel@tonic-gate 	default:
1129*0Sstevel@tonic-gate 		/*
1130*0Sstevel@tonic-gate 		 * This function must never be called for an M_IOCTL
1131*0Sstevel@tonic-gate 		 * except the listed ones.
1132*0Sstevel@tonic-gate 		 */
1133*0Sstevel@tonic-gate #ifdef DEBUG
1134*0Sstevel@tonic-gate 		cmn_err(CE_PANIC,
1135*0Sstevel@tonic-gate 		    "rloginmod: tty_flow: bad ioctl 0x%x", ioc->ioc_cmd);
1136*0Sstevel@tonic-gate #else
1137*0Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
1138*0Sstevel@tonic-gate 		return (B_TRUE);
1139*0Sstevel@tonic-gate #endif
1140*0Sstevel@tonic-gate 	}
1141*0Sstevel@tonic-gate 	/*
1142*0Sstevel@tonic-gate 	 * If tty ioctl processing is done, check for stopmode
1143*0Sstevel@tonic-gate 	 */
1144*0Sstevel@tonic-gate 	stop = (ixon && (rmip->stopc == CTRL('s')) &&
1145*0Sstevel@tonic-gate 		(rmip->startc == CTRL('q')));
1146*0Sstevel@tonic-gate 	if (rmip->stopmode == TIOCPKT_NOSTOP) {
1147*0Sstevel@tonic-gate 		if (stop) {
1148*0Sstevel@tonic-gate 			cntl = rmip->oobdata[0] | TIOCPKT_DOSTOP;
1149*0Sstevel@tonic-gate 			if ((tmpmp = make_expmblk(cntl)) == NULL) {
1150*0Sstevel@tonic-gate 				recover(q, mp, sizeof (mblk_t));
1151*0Sstevel@tonic-gate 				return (B_FALSE);
1152*0Sstevel@tonic-gate 			}
1153*0Sstevel@tonic-gate 			if (!canputnext(q)) {
1154*0Sstevel@tonic-gate 				freemsg(tmpmp);
1155*0Sstevel@tonic-gate 				return (B_FALSE);
1156*0Sstevel@tonic-gate 			}
1157*0Sstevel@tonic-gate 			putnext(q, tmpmp);
1158*0Sstevel@tonic-gate 			rmip->stopmode = TIOCPKT_DOSTOP;
1159*0Sstevel@tonic-gate 		}
1160*0Sstevel@tonic-gate 	} else {
1161*0Sstevel@tonic-gate 		if (!stop) {
1162*0Sstevel@tonic-gate 			cntl = rmip->oobdata[0] | TIOCPKT_NOSTOP;
1163*0Sstevel@tonic-gate 			if ((tmpmp = make_expmblk(cntl)) == NULL) {
1164*0Sstevel@tonic-gate 				recover(q, mp, sizeof (mblk_t));
1165*0Sstevel@tonic-gate 				return (B_FALSE);
1166*0Sstevel@tonic-gate 			}
1167*0Sstevel@tonic-gate 			if (!canputnext(q)) {
1168*0Sstevel@tonic-gate 				freemsg(tmpmp);
1169*0Sstevel@tonic-gate 				return (B_FALSE);
1170*0Sstevel@tonic-gate 			}
1171*0Sstevel@tonic-gate 			putnext(q, tmpmp);
1172*0Sstevel@tonic-gate 			rmip->stopmode = TIOCPKT_NOSTOP;
1173*0Sstevel@tonic-gate 		}
1174*0Sstevel@tonic-gate 	}
1175*0Sstevel@tonic-gate 
1176*0Sstevel@tonic-gate 	miocack(q, mp, 0, 0);
1177*0Sstevel@tonic-gate 	return (B_TRUE);
1178*0Sstevel@tonic-gate }
1179*0Sstevel@tonic-gate 
1180*0Sstevel@tonic-gate /* rlmodwioctl - handle M_IOCTL messages on the write queue. */
1181*0Sstevel@tonic-gate 
1182*0Sstevel@tonic-gate static boolean_t
rlmodwioctl(queue_t * q,mblk_t * mp)1183*0Sstevel@tonic-gate rlmodwioctl(queue_t *q, mblk_t *mp)
1184*0Sstevel@tonic-gate {
1185*0Sstevel@tonic-gate 	struct iocblk *ioc;
1186*0Sstevel@tonic-gate 	struct rlmod_info *rmip = (struct rlmod_info *)q->q_ptr;
1187*0Sstevel@tonic-gate 	int error;
1188*0Sstevel@tonic-gate 
1189*0Sstevel@tonic-gate 	ioc = (struct iocblk *)mp->b_rptr;
1190*0Sstevel@tonic-gate 	switch (ioc->ioc_cmd) {
1191*0Sstevel@tonic-gate 
1192*0Sstevel@tonic-gate 	/*
1193*0Sstevel@tonic-gate 	 * This is a special ioctl to reenable the queue.
1194*0Sstevel@tonic-gate 	 * The initial data read from the stream head is
1195*0Sstevel@tonic-gate 	 * put back on the queue.
1196*0Sstevel@tonic-gate 	 */
1197*0Sstevel@tonic-gate 	case RL_IOC_ENABLE:
1198*0Sstevel@tonic-gate 		/*
1199*0Sstevel@tonic-gate 		 * Send negative ack if RL_DISABLED flag is not set
1200*0Sstevel@tonic-gate 		 */
1201*0Sstevel@tonic-gate 
1202*0Sstevel@tonic-gate 		if (!(rmip->flags & RL_DISABLED)) {
1203*0Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
1204*0Sstevel@tonic-gate 			break;
1205*0Sstevel@tonic-gate 		}
1206*0Sstevel@tonic-gate 		if (mp->b_cont) {
1207*0Sstevel@tonic-gate 			(void) putbq(RD(q), mp->b_cont);
1208*0Sstevel@tonic-gate 			mp->b_cont = NULL;
1209*0Sstevel@tonic-gate 		}
1210*0Sstevel@tonic-gate 
1211*0Sstevel@tonic-gate 		if (rmip->flags & RL_DISABLED)
1212*0Sstevel@tonic-gate 			rmip->flags &= ~RL_DISABLED;
1213*0Sstevel@tonic-gate 		qenable(RD(q));
1214*0Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
1215*0Sstevel@tonic-gate 		TRACE_3(TR_FAC_RLOGINP, TR_RLOGINP_WPUT_OUT,
1216*0Sstevel@tonic-gate 		    "rlmodwput end: q %p, mp %p, %s",
1217*0Sstevel@tonic-gate 		    q, mp, "IOCACK enable");
1218*0Sstevel@tonic-gate 		return (B_TRUE);
1219*0Sstevel@tonic-gate 
1220*0Sstevel@tonic-gate 	/*
1221*0Sstevel@tonic-gate 	 * If it is a tty ioctl, save the output flow
1222*0Sstevel@tonic-gate 	 * control flag and the start and stop flow control
1223*0Sstevel@tonic-gate 	 * characters if they are available.
1224*0Sstevel@tonic-gate 	 */
1225*0Sstevel@tonic-gate 	case TCSETS:
1226*0Sstevel@tonic-gate 	case TCSETSW:
1227*0Sstevel@tonic-gate 	case TCSETSF:
1228*0Sstevel@tonic-gate 	case TCSETA:
1229*0Sstevel@tonic-gate 	case TCSETAW:
1230*0Sstevel@tonic-gate 	case TCSETAF:
1231*0Sstevel@tonic-gate 		return (tty_flow(q, rmip, mp));
1232*0Sstevel@tonic-gate 
1233*0Sstevel@tonic-gate #ifdef DEBUG
1234*0Sstevel@tonic-gate 	case TIOCSWINSZ:
1235*0Sstevel@tonic-gate 	case TIOCSTI:
1236*0Sstevel@tonic-gate 	case TCSBRK:
1237*0Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
1238*0Sstevel@tonic-gate 		break;
1239*0Sstevel@tonic-gate #endif
1240*0Sstevel@tonic-gate 	case CRYPTPASSTHRU:
1241*0Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (uchar_t));
1242*0Sstevel@tonic-gate 		if (error != 0) {
1243*0Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
1244*0Sstevel@tonic-gate 			break;
1245*0Sstevel@tonic-gate 		}
1246*0Sstevel@tonic-gate 		if (*(mp->b_cont->b_rptr) == 0x01)
1247*0Sstevel@tonic-gate 			rmip->flags |= RL_IOCPASSTHRU;
1248*0Sstevel@tonic-gate 		else
1249*0Sstevel@tonic-gate 			rmip->flags &= ~RL_IOCPASSTHRU;
1250*0Sstevel@tonic-gate 
1251*0Sstevel@tonic-gate 		miocack(q, mp, NULL, 0);
1252*0Sstevel@tonic-gate 		break;
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate 	default:
1255*0Sstevel@tonic-gate 		if (rmip->flags & RL_IOCPASSTHRU) {
1256*0Sstevel@tonic-gate 			putnext(q, mp);
1257*0Sstevel@tonic-gate 		} else {
1258*0Sstevel@tonic-gate #ifdef DEBUG
1259*0Sstevel@tonic-gate 			cmn_err(CE_NOTE,
1260*0Sstevel@tonic-gate 				"rlmodwioctl: unexpected ioctl type 0x%x",
1261*0Sstevel@tonic-gate 				ioc->ioc_cmd);
1262*0Sstevel@tonic-gate #endif
1263*0Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
1264*0Sstevel@tonic-gate 		}
1265*0Sstevel@tonic-gate 	}
1266*0Sstevel@tonic-gate 	return (B_TRUE);
1267*0Sstevel@tonic-gate }
1268*0Sstevel@tonic-gate 
1269*0Sstevel@tonic-gate static void
rlmod_timer(void * arg)1270*0Sstevel@tonic-gate rlmod_timer(void *arg)
1271*0Sstevel@tonic-gate {
1272*0Sstevel@tonic-gate 	queue_t *q = arg;
1273*0Sstevel@tonic-gate 	struct rlmod_info	*rmip = (struct rlmod_info *)q->q_ptr;
1274*0Sstevel@tonic-gate 
1275*0Sstevel@tonic-gate 	ASSERT(rmip);
1276*0Sstevel@tonic-gate 	if (q->q_flag & QREADR) {
1277*0Sstevel@tonic-gate 		ASSERT(rmip->rtimoutid);
1278*0Sstevel@tonic-gate 		rmip->rtimoutid = 0;
1279*0Sstevel@tonic-gate 	} else {
1280*0Sstevel@tonic-gate 		ASSERT(rmip->wtimoutid);
1281*0Sstevel@tonic-gate 		rmip->wtimoutid = 0;
1282*0Sstevel@tonic-gate 	}
1283*0Sstevel@tonic-gate 	enableok(q);
1284*0Sstevel@tonic-gate 	qenable(q);
1285*0Sstevel@tonic-gate }
1286*0Sstevel@tonic-gate 
1287*0Sstevel@tonic-gate static void
rlmod_buffer(void * arg)1288*0Sstevel@tonic-gate rlmod_buffer(void *arg)
1289*0Sstevel@tonic-gate {
1290*0Sstevel@tonic-gate 	queue_t *q = arg;
1291*0Sstevel@tonic-gate 	struct rlmod_info	*rmip = (struct rlmod_info *)q->q_ptr;
1292*0Sstevel@tonic-gate 
1293*0Sstevel@tonic-gate 	ASSERT(rmip);
1294*0Sstevel@tonic-gate 	if (q->q_flag & QREADR) {
1295*0Sstevel@tonic-gate 		ASSERT(rmip->rbufcid);
1296*0Sstevel@tonic-gate 		rmip->rbufcid = 0;
1297*0Sstevel@tonic-gate 	} else {
1298*0Sstevel@tonic-gate 		ASSERT(rmip->wbufcid);
1299*0Sstevel@tonic-gate 		rmip->wbufcid = 0;
1300*0Sstevel@tonic-gate 	}
1301*0Sstevel@tonic-gate 	enableok(q);
1302*0Sstevel@tonic-gate 	qenable(q);
1303*0Sstevel@tonic-gate }
1304*0Sstevel@tonic-gate 
1305*0Sstevel@tonic-gate static void
recover(queue_t * q,mblk_t * mp,size_t size)1306*0Sstevel@tonic-gate recover(queue_t *q, mblk_t *mp, size_t size)
1307*0Sstevel@tonic-gate {
1308*0Sstevel@tonic-gate 	/*
1309*0Sstevel@tonic-gate 	 * Avoid re-enabling the queue.
1310*0Sstevel@tonic-gate 	 */
1311*0Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_type < QPCTL);
1312*0Sstevel@tonic-gate 
1313*0Sstevel@tonic-gate 	noenable(q);
1314*0Sstevel@tonic-gate 	(void) putbq(q, mp);
1315*0Sstevel@tonic-gate 	recover1(q, size);
1316*0Sstevel@tonic-gate }
1317*0Sstevel@tonic-gate 
1318*0Sstevel@tonic-gate static void
recover1(queue_t * q,size_t size)1319*0Sstevel@tonic-gate recover1(queue_t *q, size_t size)
1320*0Sstevel@tonic-gate {
1321*0Sstevel@tonic-gate 	struct rlmod_info	*rmip = (struct rlmod_info *)q->q_ptr;
1322*0Sstevel@tonic-gate 	timeout_id_t	tid;
1323*0Sstevel@tonic-gate 	bufcall_id_t	bid;
1324*0Sstevel@tonic-gate 
1325*0Sstevel@tonic-gate 	/*
1326*0Sstevel@tonic-gate 	 * Make sure there is at most one outstanding request per queue.
1327*0Sstevel@tonic-gate 	 */
1328*0Sstevel@tonic-gate 	if (q->q_flag & QREADR) {
1329*0Sstevel@tonic-gate 		if (rmip->rtimoutid || rmip->rbufcid)
1330*0Sstevel@tonic-gate 			return;
1331*0Sstevel@tonic-gate 	} else {
1332*0Sstevel@tonic-gate 		if (rmip->wtimoutid || rmip->wbufcid)
1333*0Sstevel@tonic-gate 			return;
1334*0Sstevel@tonic-gate 	}
1335*0Sstevel@tonic-gate 	if (!(bid = qbufcall(RD(q), size, BPRI_MED, rlmod_buffer, q))) {
1336*0Sstevel@tonic-gate 		tid = qtimeout(RD(q), rlmod_timer, q, SIMWAIT);
1337*0Sstevel@tonic-gate 		if (q->q_flag & QREADR)
1338*0Sstevel@tonic-gate 			rmip->rtimoutid = tid;
1339*0Sstevel@tonic-gate 		else
1340*0Sstevel@tonic-gate 			rmip->wtimoutid = tid;
1341*0Sstevel@tonic-gate 	} else	{
1342*0Sstevel@tonic-gate 		if (q->q_flag & QREADR)
1343*0Sstevel@tonic-gate 			rmip->rbufcid = bid;
1344*0Sstevel@tonic-gate 		else
1345*0Sstevel@tonic-gate 			rmip->wbufcid = bid;
1346*0Sstevel@tonic-gate 	}
1347*0Sstevel@tonic-gate }
1348