17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * spppasyn.c - STREAMS module for doing PPP asynchronous HDLC.
37c478bd9Sstevel@tonic-gate  *
4002c70ffScarlsonj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
6*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software and its
97c478bd9Sstevel@tonic-gate  * documentation is hereby granted, provided that the above copyright
107c478bd9Sstevel@tonic-gate  * notice appears in all copies.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
137c478bd9Sstevel@tonic-gate  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
147c478bd9Sstevel@tonic-gate  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
157c478bd9Sstevel@tonic-gate  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  SUN SHALL NOT BE LIABLE FOR
167c478bd9Sstevel@tonic-gate  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
177c478bd9Sstevel@tonic-gate  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * Copyright (c) 1994 The Australian National University.
207c478bd9Sstevel@tonic-gate  * All rights reserved.
217c478bd9Sstevel@tonic-gate  *
227c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software and its
237c478bd9Sstevel@tonic-gate  * documentation is hereby granted, provided that the above copyright
247c478bd9Sstevel@tonic-gate  * notice appears in all copies.  This software is provided without any
257c478bd9Sstevel@tonic-gate  * warranty, express or implied. The Australian National University
267c478bd9Sstevel@tonic-gate  * makes no representations about the suitability of this software for
277c478bd9Sstevel@tonic-gate  * any purpose.
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
307c478bd9Sstevel@tonic-gate  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
317c478bd9Sstevel@tonic-gate  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
327c478bd9Sstevel@tonic-gate  * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
337c478bd9Sstevel@tonic-gate  * OF SUCH DAMAGE.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
367c478bd9Sstevel@tonic-gate  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
377c478bd9Sstevel@tonic-gate  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
387c478bd9Sstevel@tonic-gate  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
397c478bd9Sstevel@tonic-gate  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
407c478bd9Sstevel@tonic-gate  * OR MODIFICATIONS.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * $Id: ppp_ahdlc.c,v 1.16 2000/03/06 19:38:12 masputra Exp $
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/param.h>
477c478bd9Sstevel@tonic-gate #include <sys/stream.h>
487c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
497c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
507c478bd9Sstevel@tonic-gate #include <sys/errno.h>
517c478bd9Sstevel@tonic-gate #include <sys/conf.h>
527c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
537c478bd9Sstevel@tonic-gate #include <sys/crc32.h>
547c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
557c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <net/ppp_defs.h>
587c478bd9Sstevel@tonic-gate #include <net/pppio.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include "s_common.h"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #ifdef DEBUG
637c478bd9Sstevel@tonic-gate #define	REPORT_CRC_TYPE
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate #include "spppasyn.h"
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * This is used to tag official Solaris sources.  Please do not define
697c478bd9Sstevel@tonic-gate  * "INTERNAL_BUILD" when building this software outside of Sun
707c478bd9Sstevel@tonic-gate  * Microsystems.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate #ifdef INTERNAL_BUILD
737c478bd9Sstevel@tonic-gate /* MODINFO is limited to 32 characters. */
74002c70ffScarlsonj const char spppasyn_module_description[] = "PPP 4.0 AHDLC";
757c478bd9Sstevel@tonic-gate #else /* INTERNAL_BUILD */
767c478bd9Sstevel@tonic-gate const char spppasyn_module_description[] = "ANU PPP AHDLC $Revision: 1.16$";
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /* LINTED */
797c478bd9Sstevel@tonic-gate static const char buildtime[] = "Built " __DATE__ " at " __TIME__
807c478bd9Sstevel@tonic-gate #ifdef DEBUG
817c478bd9Sstevel@tonic-gate " DEBUG"
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate "\n";
847c478bd9Sstevel@tonic-gate #endif /* INTERNAL_BUILD */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static int	spppasyn_open(queue_t *, dev_t *, int, int, cred_t *);
877c478bd9Sstevel@tonic-gate static int	spppasyn_close(queue_t *, int, cred_t *);
887c478bd9Sstevel@tonic-gate static int	spppasyn_wput(queue_t *, mblk_t *);
897c478bd9Sstevel@tonic-gate static int	spppasyn_rput(queue_t *, mblk_t *);
907c478bd9Sstevel@tonic-gate static mblk_t	*ahdlc_encode(queue_t *, mblk_t *);
917c478bd9Sstevel@tonic-gate static mblk_t	*ahdlc_decode(queue_t *, mblk_t *);
927c478bd9Sstevel@tonic-gate static void	spppasyn_timer(void *);
937c478bd9Sstevel@tonic-gate static mblk_t	*spppasyn_inpkt(queue_t *, mblk_t *);
947c478bd9Sstevel@tonic-gate static mblk_t	*spppasyn_muxencode(queue_t *, mblk_t *);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	RESET_MUX_VALUES(x)	{	\
977c478bd9Sstevel@tonic-gate 	x->sa_mqhead = x->sa_mqtail = NULL;	\
987c478bd9Sstevel@tonic-gate 	x->sa_proto = 0;			\
997c478bd9Sstevel@tonic-gate 	x->sa_mqlen = 0;			\
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate #define	IS_XMUX_ENABLED(x)	\
1027c478bd9Sstevel@tonic-gate 	((x)->sa_flags & X_MUXMASK)
1037c478bd9Sstevel@tonic-gate #define	IS_RMUX_ENABLED(x)	\
1047c478bd9Sstevel@tonic-gate 	((x)->sa_flags & R_MUXMASK)
1057c478bd9Sstevel@tonic-gate #define	IS_COMP_AC(x)	\
1067c478bd9Sstevel@tonic-gate 	((x)->sa_flags & SAF_XCOMP_AC)
1077c478bd9Sstevel@tonic-gate #define	IS_COMP_PROT(x)	\
1087c478bd9Sstevel@tonic-gate 	((x)->sa_flags & SAF_XCOMP_PROT)
1097c478bd9Sstevel@tonic-gate #define	IS_DECOMP_PROT(x)	\
1107c478bd9Sstevel@tonic-gate 	((x)->sa_flags & SAF_RDECOMP_PROT)
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * Don't send HDLC start flag if last transmit is within 1.5 seconds -
1147c478bd9Sstevel@tonic-gate  * FLAG_TIME is defined in nanoseconds.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate #define	FLAG_TIME	1500000000ul
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * The usual AHDLC implementation enables the default escaping for all
1207c478bd9Sstevel@tonic-gate  * LCP frames.  LCP_USE_DFLT() is used in this implementation to
1217c478bd9Sstevel@tonic-gate  * modify this rule slightly.  If the code number happens to be
1227c478bd9Sstevel@tonic-gate  * Echo-Request, Echo-Reply, or Discard-Request (each of which may be
1237c478bd9Sstevel@tonic-gate  * sent only when LCP is in Opened state), then one may also use the
1247c478bd9Sstevel@tonic-gate  * negotiated ACCM; the RFC is silent on this.  The theory is that
1257c478bd9Sstevel@tonic-gate  * pppd can construct Echo-Request messages that are guaranteed to
1267c478bd9Sstevel@tonic-gate  * fail if the negotiated ACCM is bad.
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate #define	LCP_USE_DFLT(mp)	((code = MSG_BYTE((mp), 4)) < 9 || code > 11)
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * Extract bit c from map m, to determine if character c needs to be
1327c478bd9Sstevel@tonic-gate  * escaped.  Map 'm' is a pointer to a 256 bit map; 8 words of 32 bits
1337c478bd9Sstevel@tonic-gate  * each.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate #define	IN_TX_MAP(c, m)	\
1367c478bd9Sstevel@tonic-gate 	((m)[(c) >> 5] & (1 << ((c) & 0x1f)))
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * Checks the 32-bit receive ACCM to see if the byte should have been
1407c478bd9Sstevel@tonic-gate  * escaped by peer.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate #define	IN_RX_MAP(c, m)		(((c) < 0x20) && ((m) & (1 << (c))))
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static struct module_info spppasyn_modinfo = {
1457c478bd9Sstevel@tonic-gate 	AHDLC_MOD_ID,		/* mi_idnum */
1467c478bd9Sstevel@tonic-gate 	AHDLC_MOD_NAME,		/* mi_idname */
1477c478bd9Sstevel@tonic-gate 	0,			/* mi_minpsz */
1487c478bd9Sstevel@tonic-gate 	INFPSZ,			/* mi_maxpsz */
1497c478bd9Sstevel@tonic-gate 	0,			/* mi_hiwat */
1507c478bd9Sstevel@tonic-gate 	0			/* mi_lowat */
1517c478bd9Sstevel@tonic-gate };
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate static struct qinit spppasyn_rinit = {
1547c478bd9Sstevel@tonic-gate 	spppasyn_rput,		/* qi_putp */
1557c478bd9Sstevel@tonic-gate 	NULL,			/* qi_srvp */
1567c478bd9Sstevel@tonic-gate 	spppasyn_open,		/* qi_qopen */
1577c478bd9Sstevel@tonic-gate 	spppasyn_close,		/* qi_qclose */
1587c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
1597c478bd9Sstevel@tonic-gate 	&spppasyn_modinfo,	/* qi_minfo */
1607c478bd9Sstevel@tonic-gate 	NULL			/* qi_mstat */
1617c478bd9Sstevel@tonic-gate };
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static struct qinit spppasyn_winit = {
1647c478bd9Sstevel@tonic-gate 	spppasyn_wput,		/* qi_putp */
1657c478bd9Sstevel@tonic-gate 	NULL,			/* qi_srvp */
1667c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qopen */
1677c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qclose */
1687c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
1697c478bd9Sstevel@tonic-gate 	&spppasyn_modinfo,	/* qi_minfo */
1707c478bd9Sstevel@tonic-gate 	NULL			/* qi_mstat */
1717c478bd9Sstevel@tonic-gate };
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate struct streamtab spppasyn_tab = {
1747c478bd9Sstevel@tonic-gate 	&spppasyn_rinit,	/* st_rdinit */
1757c478bd9Sstevel@tonic-gate 	&spppasyn_winit,	/* st_wrinit */
1767c478bd9Sstevel@tonic-gate 	NULL,			/* st_muxrinit */
1777c478bd9Sstevel@tonic-gate 	NULL,			/* st_muxwinit */
1787c478bd9Sstevel@tonic-gate };
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /* Matches above structure. */
1817c478bd9Sstevel@tonic-gate static const char *kstat_names[] = {
1827c478bd9Sstevel@tonic-gate 	"ioctls", "ioctlsfwd", "ioctlserr", "ctls",
1837c478bd9Sstevel@tonic-gate 	"ctlsfwd", "ctlserr", "inbadchars", "inbadcharmask",
1847c478bd9Sstevel@tonic-gate 	"inaborts", "inrunts", "inallocfails", "intoolongs",
1857c478bd9Sstevel@tonic-gate 	"outrunts", "outallocfails", "incrcerrs", "unknownwrs",
1867c478bd9Sstevel@tonic-gate 	"unknownrds", "hangups", "datain", "dataout",
1877c478bd9Sstevel@tonic-gate 	"extrabufs", "sentmux", "recvmux", "inmuxerrs",
1887c478bd9Sstevel@tonic-gate #ifdef REPORT_CRC_TYPE
1897c478bd9Sstevel@tonic-gate 	"incrctype", "outcrctype",
1907c478bd9Sstevel@tonic-gate #endif
1917c478bd9Sstevel@tonic-gate };
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /* So.  This is why we have optimizing compilers. */
1947c478bd9Sstevel@tonic-gate #define	KVAL(vn)	state->sa_kstats.vn.value.ui32
1957c478bd9Sstevel@tonic-gate #define	KSET(vn, v)	KVAL(vn) = (v)
1967c478bd9Sstevel@tonic-gate #define	KADD(vn, v)	KSET(vn, KVAL(vn) + (v))
1977c478bd9Sstevel@tonic-gate #define	KOR(vn, v)	KSET(vn, KVAL(vn) | (v))
1987c478bd9Sstevel@tonic-gate #define	KINCR(vn)	KADD(vn, 1)
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static void ppp_dump_frame(sppp_ahdlc_t *state, mblk_t *mptr,
2017c478bd9Sstevel@tonic-gate     const char *msg);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * RCV_B7_1, etc., defined in net/pppio.h, are stored in flags also.
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate #define	RCV_FLAGS	(RCV_B7_1 | RCV_B7_0 | RCV_ODDP | RCV_EVNP)
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * FCS lookup table as calculated by genfcstab.
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate static ushort_t fcstab[256] = {
2127c478bd9Sstevel@tonic-gate 	0x0000,	0x1189,	0x2312,	0x329b,	0x4624,	0x57ad,	0x6536,	0x74bf,
2137c478bd9Sstevel@tonic-gate 	0x8c48,	0x9dc1,	0xaf5a,	0xbed3,	0xca6c,	0xdbe5,	0xe97e,	0xf8f7,
2147c478bd9Sstevel@tonic-gate 	0x1081,	0x0108,	0x3393,	0x221a,	0x56a5,	0x472c,	0x75b7,	0x643e,
2157c478bd9Sstevel@tonic-gate 	0x9cc9,	0x8d40,	0xbfdb,	0xae52,	0xdaed,	0xcb64,	0xf9ff,	0xe876,
2167c478bd9Sstevel@tonic-gate 	0x2102,	0x308b,	0x0210,	0x1399,	0x6726,	0x76af,	0x4434,	0x55bd,
2177c478bd9Sstevel@tonic-gate 	0xad4a,	0xbcc3,	0x8e58,	0x9fd1,	0xeb6e,	0xfae7,	0xc87c,	0xd9f5,
2187c478bd9Sstevel@tonic-gate 	0x3183,	0x200a,	0x1291,	0x0318,	0x77a7,	0x662e,	0x54b5,	0x453c,
2197c478bd9Sstevel@tonic-gate 	0xbdcb,	0xac42,	0x9ed9,	0x8f50,	0xfbef,	0xea66,	0xd8fd,	0xc974,
2207c478bd9Sstevel@tonic-gate 	0x4204,	0x538d,	0x6116,	0x709f,	0x0420,	0x15a9,	0x2732,	0x36bb,
2217c478bd9Sstevel@tonic-gate 	0xce4c,	0xdfc5,	0xed5e,	0xfcd7,	0x8868,	0x99e1,	0xab7a,	0xbaf3,
2227c478bd9Sstevel@tonic-gate 	0x5285,	0x430c,	0x7197,	0x601e,	0x14a1,	0x0528,	0x37b3,	0x263a,
2237c478bd9Sstevel@tonic-gate 	0xdecd,	0xcf44,	0xfddf,	0xec56,	0x98e9,	0x8960,	0xbbfb,	0xaa72,
2247c478bd9Sstevel@tonic-gate 	0x6306,	0x728f,	0x4014,	0x519d,	0x2522,	0x34ab,	0x0630,	0x17b9,
2257c478bd9Sstevel@tonic-gate 	0xef4e,	0xfec7,	0xcc5c,	0xddd5,	0xa96a,	0xb8e3,	0x8a78,	0x9bf1,
2267c478bd9Sstevel@tonic-gate 	0x7387,	0x620e,	0x5095,	0x411c,	0x35a3,	0x242a,	0x16b1,	0x0738,
2277c478bd9Sstevel@tonic-gate 	0xffcf,	0xee46,	0xdcdd,	0xcd54,	0xb9eb,	0xa862,	0x9af9,	0x8b70,
2287c478bd9Sstevel@tonic-gate 	0x8408,	0x9581,	0xa71a,	0xb693,	0xc22c,	0xd3a5,	0xe13e,	0xf0b7,
2297c478bd9Sstevel@tonic-gate 	0x0840,	0x19c9,	0x2b52,	0x3adb,	0x4e64,	0x5fed,	0x6d76,	0x7cff,
2307c478bd9Sstevel@tonic-gate 	0x9489,	0x8500,	0xb79b,	0xa612,	0xd2ad,	0xc324,	0xf1bf,	0xe036,
2317c478bd9Sstevel@tonic-gate 	0x18c1,	0x0948,	0x3bd3,	0x2a5a,	0x5ee5,	0x4f6c,	0x7df7,	0x6c7e,
2327c478bd9Sstevel@tonic-gate 	0xa50a,	0xb483,	0x8618,	0x9791,	0xe32e,	0xf2a7,	0xc03c,	0xd1b5,
2337c478bd9Sstevel@tonic-gate 	0x2942,	0x38cb,	0x0a50,	0x1bd9,	0x6f66,	0x7eef,	0x4c74,	0x5dfd,
2347c478bd9Sstevel@tonic-gate 	0xb58b,	0xa402,	0x9699,	0x8710,	0xf3af,	0xe226,	0xd0bd,	0xc134,
2357c478bd9Sstevel@tonic-gate 	0x39c3,	0x284a,	0x1ad1,	0x0b58,	0x7fe7,	0x6e6e,	0x5cf5,	0x4d7c,
2367c478bd9Sstevel@tonic-gate 	0xc60c,	0xd785,	0xe51e,	0xf497,	0x8028,	0x91a1,	0xa33a,	0xb2b3,
2377c478bd9Sstevel@tonic-gate 	0x4a44,	0x5bcd,	0x6956,	0x78df,	0x0c60,	0x1de9,	0x2f72,	0x3efb,
2387c478bd9Sstevel@tonic-gate 	0xd68d,	0xc704,	0xf59f,	0xe416,	0x90a9,	0x8120,	0xb3bb,	0xa232,
2397c478bd9Sstevel@tonic-gate 	0x5ac5,	0x4b4c,	0x79d7,	0x685e,	0x1ce1,	0x0d68,	0x3ff3,	0x2e7a,
2407c478bd9Sstevel@tonic-gate 	0xe70e,	0xf687,	0xc41c,	0xd595,	0xa12a,	0xb0a3,	0x8238,	0x93b1,
2417c478bd9Sstevel@tonic-gate 	0x6b46,	0x7acf,	0x4854,	0x59dd,	0x2d62,	0x3ceb,	0x0e70,	0x1ff9,
2427c478bd9Sstevel@tonic-gate 	0xf78f,	0xe606,	0xd49d,	0xc514,	0xb1ab,	0xa022,	0x92b9,	0x8330,
2437c478bd9Sstevel@tonic-gate 	0x7bc7,	0x6a4e,	0x58d5,	0x495c,	0x3de3,	0x2c6a,	0x1ef1,	0x0f78
2447c478bd9Sstevel@tonic-gate };
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * Per-character flags for accumulating input errors.  Flags are
2487c478bd9Sstevel@tonic-gate  * accumulated for bit 7 set to 0, bit 7 set to 1, even parity
2497c478bd9Sstevel@tonic-gate  * characters, and odd parity characters.  The link should see all
2507c478bd9Sstevel@tonic-gate  * four in the very first LCP Configure-Request if all is ok.  (C0 is
2517c478bd9Sstevel@tonic-gate  * even parity and has bit 7 set to 1, and 23 is odd parity and has
2527c478bd9Sstevel@tonic-gate  * bit 7 set to 0.)
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate static uchar_t charflags[256] = {
2557c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2567c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2577c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2587c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP,
2597c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2607c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2617c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2627c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2637c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2647c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2657c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2667c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP,
2677c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2687c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP,
2697c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP,
2707c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP,
2717c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2727c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2737c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2747c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP,
2757c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2767c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2777c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2787c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2797c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2807c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2817c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2827c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2837c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_EVNP,
2847c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2857c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2867c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2877c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2887c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2897c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2907c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP,
2917c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2927c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2937c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2947c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2957c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_ODDP,
2967c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_0|RCV_EVNP,
2977c478bd9Sstevel@tonic-gate 	RCV_B7_0|RCV_EVNP, RCV_B7_0|RCV_ODDP, RCV_B7_1|RCV_ODDP,
2987c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
2997c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3007c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3017c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3027c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3037c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3047c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP,
3057c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3067c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3077c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3087c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3097c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3107c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3117c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP,
3127c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3137c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3147c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3157c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3167c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3177c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3187c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3197c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3207c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP,
3217c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3227c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3237c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3247c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP,
3257c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP,
3267c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP,
3277c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3287c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP,
3297c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3307c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3317c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3327c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3337c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3347c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3357c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3367c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_EVNP,
3377c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3387c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP,
3397c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP, RCV_B7_1|RCV_ODDP, RCV_B7_1|RCV_ODDP,
3407c478bd9Sstevel@tonic-gate 	RCV_B7_1|RCV_EVNP
3417c478bd9Sstevel@tonic-gate };
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate  * Append two lists; preserve message boundaries.
3457c478bd9Sstevel@tonic-gate  * Warning: uses b_next.
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate static mblk_t *
sppp_mappend(mblk_t * m1,mblk_t * m2)3487c478bd9Sstevel@tonic-gate sppp_mappend(mblk_t *m1, mblk_t *m2)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	mblk_t *mret;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (m1 == NULL)
3537c478bd9Sstevel@tonic-gate 		return (m2);
3547c478bd9Sstevel@tonic-gate 	if (m2 == NULL)
3557c478bd9Sstevel@tonic-gate 		return (m1);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	mret = m1;
3587c478bd9Sstevel@tonic-gate 	while (m1->b_next != NULL)
3597c478bd9Sstevel@tonic-gate 		m1 = m1->b_next;
3607c478bd9Sstevel@tonic-gate 	m1->b_next = m2;
3617c478bd9Sstevel@tonic-gate 	return (mret);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * Concatenate two mblk lists.
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate static mblk_t *
sppp_mcat(mblk_t * m1,mblk_t * m2)3687c478bd9Sstevel@tonic-gate sppp_mcat(mblk_t *m1, mblk_t *m2)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	mblk_t *mret;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	if (m1 == NULL)
3737c478bd9Sstevel@tonic-gate 		return (m2);
3747c478bd9Sstevel@tonic-gate 	if (m2 == NULL)
3757c478bd9Sstevel@tonic-gate 		return (m1);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	mret = m1;
3787c478bd9Sstevel@tonic-gate 	while (m1->b_cont != NULL)
3797c478bd9Sstevel@tonic-gate 		m1 = m1->b_cont;
3807c478bd9Sstevel@tonic-gate 	m1->b_cont = m2;
3817c478bd9Sstevel@tonic-gate 	return (mret);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate  * spppasyn_open()
3867c478bd9Sstevel@tonic-gate  *
3877c478bd9Sstevel@tonic-gate  * STREAMS module open (entry) point.  Called when spppasyn is pushed
3887c478bd9Sstevel@tonic-gate  * onto an asynchronous serial stream.
3897c478bd9Sstevel@tonic-gate  */
3907c478bd9Sstevel@tonic-gate /* ARGSUSED */
3917c478bd9Sstevel@tonic-gate static int
spppasyn_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)3927c478bd9Sstevel@tonic-gate spppasyn_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t	*state;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (q->q_ptr != NULL) {
3997c478bd9Sstevel@tonic-gate 		return (0);		/* return if already opened */
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (sflag != MODOPEN) {
4037c478bd9Sstevel@tonic-gate 		return (EINVAL);	/* only open as a module */
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	state = (sppp_ahdlc_t *)kmem_zalloc(sizeof (sppp_ahdlc_t), KM_SLEEP);
4077c478bd9Sstevel@tonic-gate 	ASSERT(state != NULL);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	q->q_ptr = (caddr_t)state;
4107c478bd9Sstevel@tonic-gate 	WR(q)->q_ptr = (caddr_t)state;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	state->sa_xaccm[0] = 0xffffffff;	/* escape 0x00 through 0x1f */
4137c478bd9Sstevel@tonic-gate 	state->sa_xaccm[3] = 0x60000000;   /* escape 0x7d and 0x7e */
4147c478bd9Sstevel@tonic-gate 	state->sa_mru = PPP_MRU;		/* default of 1500 bytes */
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	qprocson(q);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	return (0);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate /*
4227c478bd9Sstevel@tonic-gate  * spppasyn_close()
4237c478bd9Sstevel@tonic-gate  *
4247c478bd9Sstevel@tonic-gate  * STREAMS module close (exit) point
4257c478bd9Sstevel@tonic-gate  */
4267c478bd9Sstevel@tonic-gate /* ARGSUSED */
4277c478bd9Sstevel@tonic-gate static int
spppasyn_close(queue_t * q,int flag,cred_t * credp)4287c478bd9Sstevel@tonic-gate spppasyn_close(queue_t *q, int flag, cred_t *credp)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t	*state;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
4337c478bd9Sstevel@tonic-gate 	state = (sppp_ahdlc_t *)q->q_ptr;
4347c478bd9Sstevel@tonic-gate 	ASSERT(state != NULL);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	/* We're leaving now.  No more calls, please. */
4377c478bd9Sstevel@tonic-gate 	qprocsoff(q);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (state->sa_rx_buf != NULL) {
4407c478bd9Sstevel@tonic-gate 		freemsg(state->sa_rx_buf);
4417c478bd9Sstevel@tonic-gate 		state->sa_rx_buf = NULL;
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if (state->sa_ksp != NULL) {
4457c478bd9Sstevel@tonic-gate 		kstat_delete(state->sa_ksp);
4467c478bd9Sstevel@tonic-gate 		state->sa_ksp = NULL;
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if (state->sa_mqhead != NULL)
4507c478bd9Sstevel@tonic-gate 		freemsg(state->sa_mqhead);
4517c478bd9Sstevel@tonic-gate 	/* remove the time out routine */
4527c478bd9Sstevel@tonic-gate 	if (state->sa_timeout_id != 0)
4537c478bd9Sstevel@tonic-gate 		(void) quntimeout(q, state->sa_timeout_id);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	q->q_ptr = NULL;
4567c478bd9Sstevel@tonic-gate 	WR(q)->q_ptr = NULL;
4577c478bd9Sstevel@tonic-gate 	kmem_free(state, sizeof (sppp_ahdlc_t));
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	return (0);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate  * Create the standard kernel statistics structure and attach it to
4647c478bd9Sstevel@tonic-gate  * the current state structure.  This can be called only after
4657c478bd9Sstevel@tonic-gate  * assigning the unit number.
4667c478bd9Sstevel@tonic-gate  */
4677c478bd9Sstevel@tonic-gate static void
create_kstats(sppp_ahdlc_t * state)4687c478bd9Sstevel@tonic-gate create_kstats(sppp_ahdlc_t *state)
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
4717c478bd9Sstevel@tonic-gate 	char unitname[KSTAT_STRLEN];
4727c478bd9Sstevel@tonic-gate 	int nstat, i;
4737c478bd9Sstevel@tonic-gate 	kstat_named_t *knt;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	nstat = sizeof (state->sa_kstats) / sizeof (kstat_named_t);
4767c478bd9Sstevel@tonic-gate 	knt = (kstat_named_t *)&state->sa_kstats;
4777c478bd9Sstevel@tonic-gate 	for (i = 0; i < nstat; i++, knt++) {
4787c478bd9Sstevel@tonic-gate #ifdef DEBUG
4797c478bd9Sstevel@tonic-gate 		/* Just in case I do something silly here. */
4807c478bd9Sstevel@tonic-gate 		if (i >= sizeof (kstat_names) / sizeof (kstat_names[0]))
4817c478bd9Sstevel@tonic-gate 			(void) sprintf(knt->name, "unknown%d", i);
4827c478bd9Sstevel@tonic-gate 		else
4837c478bd9Sstevel@tonic-gate #endif
4847c478bd9Sstevel@tonic-gate 			(void) strncpy(knt->name, kstat_names[i],
4857c478bd9Sstevel@tonic-gate 			    sizeof (knt->name));
4867c478bd9Sstevel@tonic-gate 		knt->data_type = KSTAT_DATA_UINT32;
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * sprintf is known to be safe here because KSTAT_STRLEN is
4907c478bd9Sstevel@tonic-gate 	 * 31, the maximum module name length is 8, and the maximum
4917c478bd9Sstevel@tonic-gate 	 * string length from %d is 11.  This was once snprintf, but
4927c478bd9Sstevel@tonic-gate 	 * that's not backward-compatible with Solaris 2.6.
4937c478bd9Sstevel@tonic-gate 	 */
494002c70ffScarlsonj 	(void) sprintf(unitname, "%s" "%d", AHDLC_MOD_NAME, state->sa_unit);
4957c478bd9Sstevel@tonic-gate 	ksp = kstat_create(AHDLC_MOD_NAME, state->sa_unit, unitname, "net",
4967c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, nstat, KSTAT_FLAG_VIRTUAL);
4977c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
4987c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *)&state->sa_kstats;
4997c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 	state->sa_ksp = ksp;
5027c478bd9Sstevel@tonic-gate #ifdef REPORT_CRC_TYPE
5037c478bd9Sstevel@tonic-gate 	KSET(pks_outcrctype, 16);
5047c478bd9Sstevel@tonic-gate 	KSET(pks_incrctype, 16);
5057c478bd9Sstevel@tonic-gate #endif
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate /*
5097c478bd9Sstevel@tonic-gate  * spppasyn_inner_ioctl
5107c478bd9Sstevel@tonic-gate  *
5117c478bd9Sstevel@tonic-gate  * MT-Perimeters:
5127c478bd9Sstevel@tonic-gate  *	exclusive inner
5137c478bd9Sstevel@tonic-gate  *
5147c478bd9Sstevel@tonic-gate  * Handle state-affecting ioctls.
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate static void
spppasyn_inner_ioctl(queue_t * q,mblk_t * mp)5177c478bd9Sstevel@tonic-gate spppasyn_inner_ioctl(queue_t *q, mblk_t *mp)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t		*state;
5207c478bd9Sstevel@tonic-gate 	struct iocblk		*iop;
5217c478bd9Sstevel@tonic-gate 	int			error;
5227c478bd9Sstevel@tonic-gate 	int			flagval;
5237c478bd9Sstevel@tonic-gate 	int			len;
5247c478bd9Sstevel@tonic-gate 	uint32_t		mux_flags;
5257c478bd9Sstevel@tonic-gate 	uint32_t		mask;
5267c478bd9Sstevel@tonic-gate 	int			flagmask;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL && mp != NULL);
5297c478bd9Sstevel@tonic-gate 	state = (sppp_ahdlc_t *)q->q_ptr;
5307c478bd9Sstevel@tonic-gate 	iop = (struct iocblk *)mp->b_rptr;
5317c478bd9Sstevel@tonic-gate 	ASSERT(state != NULL && iop != NULL);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	error = EINVAL;
5347c478bd9Sstevel@tonic-gate 	len = 0;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	switch (iop->ioc_cmd) {
5377c478bd9Sstevel@tonic-gate 	case PPPIO_XFCS:
5387c478bd9Sstevel@tonic-gate 		/* Check for valid option length */
5397c478bd9Sstevel@tonic-gate 		if (iop->ioc_count != sizeof (uint32_t) || mp->b_cont == NULL)
5407c478bd9Sstevel@tonic-gate 			break;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		/* Grab flag value */
5437c478bd9Sstevel@tonic-gate 		flagval = *(uint32_t *)mp->b_cont->b_rptr;
5447c478bd9Sstevel@tonic-gate 		if (flagval < PPPFCS_16 || flagval > PPPFCS_NONE)
5457c478bd9Sstevel@tonic-gate 			break;
5467c478bd9Sstevel@tonic-gate 		state->sa_flags &= ~SAF_XMITCRC32 & ~SAF_XMITCRCNONE;
5477c478bd9Sstevel@tonic-gate 		if (flagval == PPPFCS_32) {
5487c478bd9Sstevel@tonic-gate #ifdef REPORT_CRC_TYPE
5497c478bd9Sstevel@tonic-gate 			KSET(pks_outcrctype, 32);
5507c478bd9Sstevel@tonic-gate #endif
5517c478bd9Sstevel@tonic-gate 			state->sa_flags |= SAF_XMITCRC32;
5527c478bd9Sstevel@tonic-gate 		} else if (flagval == PPPFCS_NONE) {
5537c478bd9Sstevel@tonic-gate #ifdef REPORT_CRC_TYPE
5547c478bd9Sstevel@tonic-gate 			KSET(pks_outcrctype, 0);
5557c478bd9Sstevel@tonic-gate #endif
5567c478bd9Sstevel@tonic-gate 			state->sa_flags |= SAF_XMITCRCNONE;
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate #ifdef REPORT_CRC_TYPE
5597c478bd9Sstevel@tonic-gate 		else {
5607c478bd9Sstevel@tonic-gate 			KSET(pks_outcrctype, 16);
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate #endif
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		/* Return success */
5657c478bd9Sstevel@tonic-gate 		error = 0;
5667c478bd9Sstevel@tonic-gate 		break;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	case PPPIO_RFCS:
5697c478bd9Sstevel@tonic-gate 		/* Check for valid option length */
5707c478bd9Sstevel@tonic-gate 		if (iop->ioc_count != sizeof (uint32_t) || mp->b_cont == NULL)
5717c478bd9Sstevel@tonic-gate 			break;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 		/* Grab flag value */
5747c478bd9Sstevel@tonic-gate 		flagval = *(uint32_t *)mp->b_cont->b_rptr;
5757c478bd9Sstevel@tonic-gate 		if (flagval < PPPFCS_16 || flagval > PPPFCS_NONE)
5767c478bd9Sstevel@tonic-gate 			break;
5777c478bd9Sstevel@tonic-gate 		state->sa_flags &= ~SAF_RECVCRC32 & ~SAF_RECVCRCNONE;
5787c478bd9Sstevel@tonic-gate 		if (flagval == PPPFCS_32) {
5797c478bd9Sstevel@tonic-gate #ifdef REPORT_CRC_TYPE
5807c478bd9Sstevel@tonic-gate 			KSET(pks_incrctype, 32);
5817c478bd9Sstevel@tonic-gate #endif
5827c478bd9Sstevel@tonic-gate 			state->sa_flags |= SAF_RECVCRC32;
5837c478bd9Sstevel@tonic-gate 		} else if (flagval == PPPFCS_NONE) {
5847c478bd9Sstevel@tonic-gate #ifdef REPORT_CRC_TYPE
5857c478bd9Sstevel@tonic-gate 			KSET(pks_incrctype, 0);
5867c478bd9Sstevel@tonic-gate #endif
5877c478bd9Sstevel@tonic-gate 			state->sa_flags |= SAF_RECVCRCNONE;
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate #ifdef REPORT_CRC_TYPE
5907c478bd9Sstevel@tonic-gate 		else {
5917c478bd9Sstevel@tonic-gate 			KSET(pks_incrctype, 16);
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate #endif
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		/* Return success */
5967c478bd9Sstevel@tonic-gate 		error = 0;
5977c478bd9Sstevel@tonic-gate 		break;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	case PPPIO_XACCM:
6007c478bd9Sstevel@tonic-gate 		/* Check for valid asyncmap length */
6017c478bd9Sstevel@tonic-gate 		if (iop->ioc_count < sizeof (uint32_t) ||
6027c478bd9Sstevel@tonic-gate 		    iop->ioc_count > sizeof (ext_accm) ||
6037c478bd9Sstevel@tonic-gate 		    mp->b_cont == NULL)
6047c478bd9Sstevel@tonic-gate 			break;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		/* Copy user's asyncmap into our state structure. */
6077c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)mp->b_cont->b_rptr,
6087c478bd9Sstevel@tonic-gate 		    (caddr_t)state->sa_xaccm, iop->ioc_count);
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		state->sa_xaccm[2] &= ~0x40000000;	/* don't escape 0x5e */
6117c478bd9Sstevel@tonic-gate 		state->sa_xaccm[3] |= 0x60000000;	/* escape 0x7d, 0x7e */
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		error = 0;
6147c478bd9Sstevel@tonic-gate 		break;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	case PPPIO_RACCM:
6177c478bd9Sstevel@tonic-gate 		/* Check for valid asyncmap length (only ctrl chars) */
6187c478bd9Sstevel@tonic-gate 		if (iop->ioc_count != sizeof (uint32_t) ||
6197c478bd9Sstevel@tonic-gate 		    mp->b_cont == NULL)
6207c478bd9Sstevel@tonic-gate 			break;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 		state->sa_raccm = *(uint32_t *)mp->b_cont->b_rptr;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		error = 0;
6257c478bd9Sstevel@tonic-gate 		break;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	case PPPIO_LASTMOD:
6287c478bd9Sstevel@tonic-gate 		/* We already know this. */
6297c478bd9Sstevel@tonic-gate 		state->sa_flags |= SAF_LASTMOD;
6307c478bd9Sstevel@tonic-gate 		error = 0;
6317c478bd9Sstevel@tonic-gate 		break;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	case PPPIO_MUX:
6347c478bd9Sstevel@tonic-gate 		/* set the compression flags */
6357c478bd9Sstevel@tonic-gate 		if (iop->ioc_count != 2 * sizeof (uint32_t) ||
6367c478bd9Sstevel@tonic-gate 		    mp->b_cont == NULL)
6377c478bd9Sstevel@tonic-gate 			break;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		/* set the mux flags */
6407c478bd9Sstevel@tonic-gate 		mux_flags = ((uint32_t *)mp->b_cont->b_rptr)[0];
6417c478bd9Sstevel@tonic-gate 		mask = ((uint32_t *)mp->b_cont->b_rptr)[1];
6427c478bd9Sstevel@tonic-gate 		if (mux_flags != 0)
6437c478bd9Sstevel@tonic-gate 			state->sa_flags = (state->sa_flags & ~mask) | (mask);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		/* set the multiplexing timer value */
6467c478bd9Sstevel@tonic-gate 		if (mask & R_MUXMASK)
6477c478bd9Sstevel@tonic-gate 			state->sa_timeout_usec = mux_flags;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		error = 0;
6507c478bd9Sstevel@tonic-gate 		break;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	case PPPIO_CFLAGS:
6537c478bd9Sstevel@tonic-gate 		if (iop->ioc_count != 2 * sizeof (uint32_t) ||
6547c478bd9Sstevel@tonic-gate 		    mp->b_cont == NULL)
6557c478bd9Sstevel@tonic-gate 			break;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		flagval = (((uint32_t *)mp->b_cont->b_rptr)[0] << 20) &
6587c478bd9Sstevel@tonic-gate 		    (SAF_RDECOMP_PROT | SAF_RDECOMP_AC | SAF_XCOMP_PROT |
659002c70ffScarlsonj 		    SAF_XCOMP_AC);
6607c478bd9Sstevel@tonic-gate 		flagmask = (((uint32_t *)mp->b_cont->b_rptr)[1] << 20) &
6617c478bd9Sstevel@tonic-gate 		    (SAF_RDECOMP_PROT | SAF_RDECOMP_AC | SAF_XCOMP_PROT |
662002c70ffScarlsonj 		    SAF_XCOMP_AC);
6637c478bd9Sstevel@tonic-gate 		state->sa_flags = flagval | (state->sa_flags & ~flagmask);
6647c478bd9Sstevel@tonic-gate 		*(uint32_t *)mp->b_cont->b_rptr = state->sa_flags >> 20;
6657c478bd9Sstevel@tonic-gate 		len = sizeof (uint32_t);
6667c478bd9Sstevel@tonic-gate 		error = 0;
6677c478bd9Sstevel@tonic-gate 		break;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	case PPPIO_DEBUG:
6707c478bd9Sstevel@tonic-gate 		if (iop->ioc_count != sizeof (uint32_t) || mp->b_cont == NULL)
6717c478bd9Sstevel@tonic-gate 			break;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		flagval = *(uint32_t *)mp->b_cont->b_rptr;
6747c478bd9Sstevel@tonic-gate 		if (flagval != PPPDBG_LOG + PPPDBG_AHDLC) {
6757c478bd9Sstevel@tonic-gate 			putnext(q, mp);
6767c478bd9Sstevel@tonic-gate 			return;
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, AHDLC_MOD_NAME "%d: debug log enabled\n",
6797c478bd9Sstevel@tonic-gate 		    state->sa_unit);
6807c478bd9Sstevel@tonic-gate 		state->sa_flags |= SAF_XMITDUMP | SAF_RECVDUMP;
6817c478bd9Sstevel@tonic-gate 		error = 0;
6827c478bd9Sstevel@tonic-gate 		break;
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	if (error == 0) {
6867c478bd9Sstevel@tonic-gate 		/* Success; tell the user */
6877c478bd9Sstevel@tonic-gate 		if (mp->b_cont == NULL)
6887c478bd9Sstevel@tonic-gate 			len = 0;
6897c478bd9Sstevel@tonic-gate 		else
6907c478bd9Sstevel@tonic-gate 			mp->b_cont->b_wptr = mp->b_cont->b_rptr + len;
6917c478bd9Sstevel@tonic-gate 		miocack(q, mp, len, 0);
6927c478bd9Sstevel@tonic-gate 	} else {
6937c478bd9Sstevel@tonic-gate 		/* Failure; send error back upstream. */
6947c478bd9Sstevel@tonic-gate 		KINCR(pks_ioctlserr);
6957c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, error);
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate /*
7007c478bd9Sstevel@tonic-gate  * spppasyn_inner_mctl
7017c478bd9Sstevel@tonic-gate  *
7027c478bd9Sstevel@tonic-gate  * MT-Perimeters:
7037c478bd9Sstevel@tonic-gate  *	exclusive inner
7047c478bd9Sstevel@tonic-gate  *
7057c478bd9Sstevel@tonic-gate  * Handle state-affecting M_CTL messages.
7067c478bd9Sstevel@tonic-gate  */
7077c478bd9Sstevel@tonic-gate static void
spppasyn_inner_mctl(queue_t * q,mblk_t * mp)7087c478bd9Sstevel@tonic-gate spppasyn_inner_mctl(queue_t *q, mblk_t *mp)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t	*state;
7117c478bd9Sstevel@tonic-gate 	int		msglen;
7127c478bd9Sstevel@tonic-gate 	int		error;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL && mp != NULL);
7157c478bd9Sstevel@tonic-gate 	state = (sppp_ahdlc_t *)q->q_ptr;
7167c478bd9Sstevel@tonic-gate 	ASSERT(state != NULL);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	msglen = MBLKL(mp);
7197c478bd9Sstevel@tonic-gate 	error = 0;
7207c478bd9Sstevel@tonic-gate 	switch (*mp->b_rptr) {
7217c478bd9Sstevel@tonic-gate 	case PPPCTL_MTU:
7227c478bd9Sstevel@tonic-gate 				/* Just ignore the MTU */
7237c478bd9Sstevel@tonic-gate 		break;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	case PPPCTL_MRU:
7267c478bd9Sstevel@tonic-gate 		if (msglen != 4)
7277c478bd9Sstevel@tonic-gate 			error = EINVAL;
7287c478bd9Sstevel@tonic-gate 		else
7297c478bd9Sstevel@tonic-gate 			state->sa_mru =
7307c478bd9Sstevel@tonic-gate 			    ((ushort_t *)mp->b_rptr)[1];
7317c478bd9Sstevel@tonic-gate 		break;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	case PPPCTL_UNIT:
7347c478bd9Sstevel@tonic-gate 		if (state->sa_ksp != NULL) {
7357c478bd9Sstevel@tonic-gate 			error = EINVAL;
7367c478bd9Sstevel@tonic-gate 			break;
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 		if (msglen == 2)
7397c478bd9Sstevel@tonic-gate 			state->sa_unit = mp->b_rptr[1];
7407c478bd9Sstevel@tonic-gate 		else if (msglen == 8)
7417c478bd9Sstevel@tonic-gate 			state->sa_unit =
7427c478bd9Sstevel@tonic-gate 			    ((uint32_t *)mp->b_rptr)[1];
7437c478bd9Sstevel@tonic-gate 		else
7447c478bd9Sstevel@tonic-gate 			error = EINVAL;
7457c478bd9Sstevel@tonic-gate 		if (error == 0 && state->sa_ksp == NULL)
7467c478bd9Sstevel@tonic-gate 			create_kstats(state);
7477c478bd9Sstevel@tonic-gate 		break;
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if (error > 0) {
7517c478bd9Sstevel@tonic-gate 		KINCR(pks_ctlserr);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 	if (state->sa_flags & SAF_LASTMOD) {
7547c478bd9Sstevel@tonic-gate 		freemsg(mp);
7557c478bd9Sstevel@tonic-gate 	} else {
7567c478bd9Sstevel@tonic-gate 		KINCR(pks_ctlsfwd);
7577c478bd9Sstevel@tonic-gate 		putnext(q, mp);
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate /*
7627c478bd9Sstevel@tonic-gate  * spppasyn_wput()
7637c478bd9Sstevel@tonic-gate  *
7647c478bd9Sstevel@tonic-gate  * MT-Perimeters:
7657c478bd9Sstevel@tonic-gate  *	exclusive inner.
7667c478bd9Sstevel@tonic-gate  *
7677c478bd9Sstevel@tonic-gate  * Write side put routine.  This called by the modules above us (likely to
7687c478bd9Sstevel@tonic-gate  * be the compression module) to transmit data or pass along ioctls.
7697c478bd9Sstevel@tonic-gate  */
7707c478bd9Sstevel@tonic-gate static int
spppasyn_wput(queue_t * q,mblk_t * mp)7717c478bd9Sstevel@tonic-gate spppasyn_wput(queue_t *q, mblk_t *mp)
7727c478bd9Sstevel@tonic-gate {
7737c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t		*state;
7747c478bd9Sstevel@tonic-gate 	struct iocblk		*iop;
7757c478bd9Sstevel@tonic-gate 	int			error;
7767c478bd9Sstevel@tonic-gate 	mblk_t			*np;
7777c478bd9Sstevel@tonic-gate 	struct ppp_stats64	*psp;
7787c478bd9Sstevel@tonic-gate 	int			msglen;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL && mp != NULL);
7817c478bd9Sstevel@tonic-gate 	state = (sppp_ahdlc_t *)q->q_ptr;
7827c478bd9Sstevel@tonic-gate 	ASSERT(state != NULL);
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	switch (MTYPE(mp)) {
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	case M_DATA:
7877c478bd9Sstevel@tonic-gate 		/*
7887c478bd9Sstevel@tonic-gate 		 * A data packet - do character-stuffing and FCS, and
7897c478bd9Sstevel@tonic-gate 		 * send it onwards.  The blocks are freed as we go.
7907c478bd9Sstevel@tonic-gate 		 */
7917c478bd9Sstevel@tonic-gate 		if (IS_XMUX_ENABLED(state))
7927c478bd9Sstevel@tonic-gate 			mp = spppasyn_muxencode(q, mp);
7937c478bd9Sstevel@tonic-gate 		else
7947c478bd9Sstevel@tonic-gate 			mp = ahdlc_encode(q, mp);
7957c478bd9Sstevel@tonic-gate 		if (mp != NULL)
7967c478bd9Sstevel@tonic-gate 			putnext(q, mp);
7977c478bd9Sstevel@tonic-gate 		break;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	case M_IOCTL:
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 		KINCR(pks_ioctls);
8027c478bd9Sstevel@tonic-gate 		iop = (struct iocblk *)mp->b_rptr;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		msglen = 0;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 		switch (iop->ioc_cmd) {
8077c478bd9Sstevel@tonic-gate 		case PPPIO_XFCS:
8087c478bd9Sstevel@tonic-gate 		case PPPIO_RFCS:
8097c478bd9Sstevel@tonic-gate 		case PPPIO_XACCM:
8107c478bd9Sstevel@tonic-gate 		case PPPIO_RACCM:
8117c478bd9Sstevel@tonic-gate 		case PPPIO_LASTMOD:
8127c478bd9Sstevel@tonic-gate 		case PPPIO_DEBUG:
8137c478bd9Sstevel@tonic-gate 		case PPPIO_MUX:
8147c478bd9Sstevel@tonic-gate 		case PPPIO_CFLAGS:
8157c478bd9Sstevel@tonic-gate 			spppasyn_inner_ioctl(q, mp);
8167c478bd9Sstevel@tonic-gate 			return (0);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 		case PPPIO_GCLEAN:
8197c478bd9Sstevel@tonic-gate 			np = allocb(sizeof (uint32_t), BPRI_HI);
8207c478bd9Sstevel@tonic-gate 			if (np == NULL) {
8217c478bd9Sstevel@tonic-gate 				error = ENOSR;
8227c478bd9Sstevel@tonic-gate 				break;
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 			if (mp->b_cont != NULL) {
8257c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
8267c478bd9Sstevel@tonic-gate 			}
8277c478bd9Sstevel@tonic-gate 			mp->b_cont = np;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 			*(uint32_t *)np->b_wptr = state->sa_flags & RCV_FLAGS;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 			msglen = sizeof (uint32_t);
8327c478bd9Sstevel@tonic-gate 			np->b_wptr += msglen;
8337c478bd9Sstevel@tonic-gate 			error = 0;
8347c478bd9Sstevel@tonic-gate 			break;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		case PPPIO_GETSTAT:
8377c478bd9Sstevel@tonic-gate 			error = EINVAL;
8387c478bd9Sstevel@tonic-gate 			break;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 		case PPPIO_GETSTAT64:
8417c478bd9Sstevel@tonic-gate 			np = allocb(sizeof (*psp), BPRI_HI);
8427c478bd9Sstevel@tonic-gate 			if (np == NULL) {
8437c478bd9Sstevel@tonic-gate 				error = ENOSR;
8447c478bd9Sstevel@tonic-gate 				break;
8457c478bd9Sstevel@tonic-gate 			}
8467c478bd9Sstevel@tonic-gate 			if (mp->b_cont != NULL) {
8477c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
8487c478bd9Sstevel@tonic-gate 			}
8497c478bd9Sstevel@tonic-gate 			mp->b_cont = np;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 			psp = (struct ppp_stats64 *)np->b_wptr;
8527c478bd9Sstevel@tonic-gate 			bzero((caddr_t)psp, sizeof (*psp));
8537c478bd9Sstevel@tonic-gate 			psp->p = state->sa_stats;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 			msglen = sizeof (*psp);
8567c478bd9Sstevel@tonic-gate 			np->b_wptr += msglen;
8577c478bd9Sstevel@tonic-gate 			error = 0;
8587c478bd9Sstevel@tonic-gate 			break;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 		case PPPIO_GTYPE:
8617c478bd9Sstevel@tonic-gate 			np = allocb(sizeof (uint32_t), BPRI_HI);
8627c478bd9Sstevel@tonic-gate 			if (np == NULL) {
8637c478bd9Sstevel@tonic-gate 				error = ENOSR;
8647c478bd9Sstevel@tonic-gate 				break;
8657c478bd9Sstevel@tonic-gate 			}
8667c478bd9Sstevel@tonic-gate 			if (mp->b_cont != NULL) {
8677c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
8687c478bd9Sstevel@tonic-gate 			}
8697c478bd9Sstevel@tonic-gate 			mp->b_cont = np;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 			*(uint32_t *)np->b_wptr = PPPTYP_AHDLC;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 			msglen = sizeof (uint32_t);
8747c478bd9Sstevel@tonic-gate 			np->b_wptr += msglen;
8757c478bd9Sstevel@tonic-gate 			error = 0;
8767c478bd9Sstevel@tonic-gate 			break;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		default:
8797c478bd9Sstevel@tonic-gate 			/* Unknown ioctl -- forward along */
8807c478bd9Sstevel@tonic-gate 			KINCR(pks_ioctlsfwd);
8817c478bd9Sstevel@tonic-gate 			putnext(q, mp);
8827c478bd9Sstevel@tonic-gate 			return (0);
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		if (error == 0) {
8867c478bd9Sstevel@tonic-gate 			/* Success; tell the user */
8877c478bd9Sstevel@tonic-gate 			miocack(q, mp, msglen, 0);
8887c478bd9Sstevel@tonic-gate 		} else {
8897c478bd9Sstevel@tonic-gate 			/* Failure; send error back upstream. */
8907c478bd9Sstevel@tonic-gate 			KINCR(pks_ioctlserr);
8917c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		break;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	case M_CTL:
8977c478bd9Sstevel@tonic-gate 		KINCR(pks_ctls);
8987c478bd9Sstevel@tonic-gate 		spppasyn_inner_mctl(q, mp);
8997c478bd9Sstevel@tonic-gate 		break;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	default:
9027c478bd9Sstevel@tonic-gate 		if (state->sa_flags & (SAF_XMITDUMP|SAF_RECVDUMP))
9037c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
9047c478bd9Sstevel@tonic-gate 			    "spppasyn_wpur:  unknown buffer type %d",
9057c478bd9Sstevel@tonic-gate 			    MTYPE(mp));
9067c478bd9Sstevel@tonic-gate 		KINCR(pks_unknownwrs);
9077c478bd9Sstevel@tonic-gate 		putnext(q, mp);
9087c478bd9Sstevel@tonic-gate 		break;
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	return (0);
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate /*
9157c478bd9Sstevel@tonic-gate  * spppasyn_rput()
9167c478bd9Sstevel@tonic-gate  *
9177c478bd9Sstevel@tonic-gate  * MT-Perimeters:
9187c478bd9Sstevel@tonic-gate  *	exclusive inner.
9197c478bd9Sstevel@tonic-gate  *
9207c478bd9Sstevel@tonic-gate  * Read side put routine.  This is called by the async serial driver
9217c478bd9Sstevel@tonic-gate  * below us to handle received data and returned signals (like
9227c478bd9Sstevel@tonic-gate  * hang-up).
9237c478bd9Sstevel@tonic-gate  */
9247c478bd9Sstevel@tonic-gate static int
spppasyn_rput(queue_t * q,mblk_t * mp)9257c478bd9Sstevel@tonic-gate spppasyn_rput(queue_t *q, mblk_t  *mp)
9267c478bd9Sstevel@tonic-gate {
9277c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t	*state;
9287c478bd9Sstevel@tonic-gate 	mblk_t		*mpnext;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL && mp != NULL);
9317c478bd9Sstevel@tonic-gate 	state = (sppp_ahdlc_t *)q->q_ptr;
9327c478bd9Sstevel@tonic-gate 	ASSERT(state != NULL);
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	switch (MTYPE(mp)) {
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	case M_DATA:
9377c478bd9Sstevel@tonic-gate 		/* Note -- decoder frees the buffers */
9387c478bd9Sstevel@tonic-gate 		mp = ahdlc_decode(q, mp);
9397c478bd9Sstevel@tonic-gate 		while (mp != NULL) {
9407c478bd9Sstevel@tonic-gate 			mpnext = mp->b_next;
9417c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
9427c478bd9Sstevel@tonic-gate 			putnext(q, mp);
9437c478bd9Sstevel@tonic-gate 			mp = mpnext;
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 		break;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	case M_HANGUP:
9487c478bd9Sstevel@tonic-gate 		KINCR(pks_hangups);
9497c478bd9Sstevel@tonic-gate 		state->sa_flags |= SAF_IFLUSH;
9507c478bd9Sstevel@tonic-gate 		putnext(q, mp);
9517c478bd9Sstevel@tonic-gate 		break;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	default:
9547c478bd9Sstevel@tonic-gate 		if (state->sa_flags & (SAF_XMITDUMP|SAF_RECVDUMP)) {
9557c478bd9Sstevel@tonic-gate 			if (MTYPE(mp) == M_IOCTL)
9567c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
9577c478bd9Sstevel@tonic-gate 				    "spppasyn_rput:  unexpected ioctl %X",
9587c478bd9Sstevel@tonic-gate 				    ((struct iocblk *)mp->b_rptr)->ioc_cmd);
9597c478bd9Sstevel@tonic-gate 			else
9607c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
9617c478bd9Sstevel@tonic-gate 				    "spppasyn_rput:  unknown buffer type %d",
9627c478bd9Sstevel@tonic-gate 				    MTYPE(mp));
9637c478bd9Sstevel@tonic-gate 		}
9647c478bd9Sstevel@tonic-gate 		KINCR(pks_unknownrds);
9657c478bd9Sstevel@tonic-gate 		putnext(q, mp);
9667c478bd9Sstevel@tonic-gate 		break;
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	return (0);
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate /*
9737c478bd9Sstevel@tonic-gate  * ahdlc_encode
9747c478bd9Sstevel@tonic-gate  *
9757c478bd9Sstevel@tonic-gate  * Perform asynchronous HDLC framing on a given buffer and transmit
9767c478bd9Sstevel@tonic-gate  * the result.  The state structure must be valid.  The input buffers
9777c478bd9Sstevel@tonic-gate  * are freed as we go.
9787c478bd9Sstevel@tonic-gate  *
9797c478bd9Sstevel@tonic-gate  * This function is called by wput and just encodes the data.  Wput
9807c478bd9Sstevel@tonic-gate  * then calls putnext directly.  There's no service routine for this
9817c478bd9Sstevel@tonic-gate  * module, so flow control is asserted by the module below us up to
9827c478bd9Sstevel@tonic-gate  * our caller by the STREAMS framework.  This is by design -- this
9837c478bd9Sstevel@tonic-gate  * module does not queue anything so that other modules can make QoS
9847c478bd9Sstevel@tonic-gate  * decisions.
9857c478bd9Sstevel@tonic-gate  */
9867c478bd9Sstevel@tonic-gate static mblk_t *
ahdlc_encode(queue_t * q,mblk_t * mp)9877c478bd9Sstevel@tonic-gate ahdlc_encode(queue_t *q, mblk_t	*mp)
9887c478bd9Sstevel@tonic-gate {
9897c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t	*state;
9907c478bd9Sstevel@tonic-gate 	uint32_t	loc_xaccm[8];
9917c478bd9Sstevel@tonic-gate 	ushort_t	fcs16;
9927c478bd9Sstevel@tonic-gate 	uint32_t	fcs32;
9937c478bd9Sstevel@tonic-gate 	size_t		msglen;
9947c478bd9Sstevel@tonic-gate 	size_t		outmp_len;
9957c478bd9Sstevel@tonic-gate 	mblk_t		*outmp;
9967c478bd9Sstevel@tonic-gate 	mblk_t		*curout;
9977c478bd9Sstevel@tonic-gate 	mblk_t		*tmp;
9987c478bd9Sstevel@tonic-gate 	uchar_t		*ep;
9997c478bd9Sstevel@tonic-gate 	uchar_t		*dp;
10007c478bd9Sstevel@tonic-gate 	uchar_t		*tp;
10017c478bd9Sstevel@tonic-gate 	uchar_t		*tpmax;
10027c478bd9Sstevel@tonic-gate #if defined(lint) || defined(_lint)
10037c478bd9Sstevel@tonic-gate 	uchar_t		chr;	/* lint likes this */
10047c478bd9Sstevel@tonic-gate #else
10057c478bd9Sstevel@tonic-gate 	int		chr;	/* not uchar_t; more efficient this way */
10067c478bd9Sstevel@tonic-gate 				/* with WorkShop compiler */
10077c478bd9Sstevel@tonic-gate #endif
10087c478bd9Sstevel@tonic-gate 	int		is_lcp, is_ctrl;
10097c478bd9Sstevel@tonic-gate 	int		code;
10107c478bd9Sstevel@tonic-gate 	hrtime_t	hrtime;
10117c478bd9Sstevel@tonic-gate 	uint32_t	flags;	/* sampled copy of flags */
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	state = (sppp_ahdlc_t *)q->q_ptr;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	/* Don't transmit anything obviously silly. */
10167c478bd9Sstevel@tonic-gate 	msglen = msgsize(mp);
10177c478bd9Sstevel@tonic-gate 	if (msglen < 4) {
10187c478bd9Sstevel@tonic-gate 		KINCR(pks_outrunts);
10197c478bd9Sstevel@tonic-gate 		freemsg(mp);
10207c478bd9Sstevel@tonic-gate 		(void) putnextctl1(RD(q), M_CTL, PPPCTL_OERROR);
10217c478bd9Sstevel@tonic-gate 		return (NULL);
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	/*
10257c478bd9Sstevel@tonic-gate 	 * Allocate an output buffer just large enough for most cases.
10267c478bd9Sstevel@tonic-gate 	 * Based on original work in the ppp-2.2 AIX PPP driver, we
10277c478bd9Sstevel@tonic-gate 	 * estimate the output size as 1.25 * input message length
10287c478bd9Sstevel@tonic-gate 	 * plus 16.  If this turns out to be too small, then we'll
10297c478bd9Sstevel@tonic-gate 	 * allocate exactly one additional buffer with two times the
10307c478bd9Sstevel@tonic-gate 	 * remaining input length (the maximum that could possibly be
10317c478bd9Sstevel@tonic-gate 	 * required).
10327c478bd9Sstevel@tonic-gate 	 */
10337c478bd9Sstevel@tonic-gate 	outmp_len = msglen + (msglen >> 2) + 16;
10347c478bd9Sstevel@tonic-gate 	outmp = allocb(outmp_len, BPRI_MED);
10357c478bd9Sstevel@tonic-gate 	if (outmp == NULL)
10367c478bd9Sstevel@tonic-gate 		goto outallocfail;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	tp = outmp->b_wptr;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	/*
10417c478bd9Sstevel@tonic-gate 	 * Check if our last transmit happened within FLAG_TIME, using
10427c478bd9Sstevel@tonic-gate 	 * the system's hrtime.
10437c478bd9Sstevel@tonic-gate 	 */
10447c478bd9Sstevel@tonic-gate 	hrtime = gethrtime();
10457c478bd9Sstevel@tonic-gate 	if (ABS(hrtime - state->sa_hrtime) > FLAG_TIME) {
10467c478bd9Sstevel@tonic-gate 		*tp++ = PPP_FLAG;
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 	state->sa_hrtime = hrtime;
10497c478bd9Sstevel@tonic-gate 	bcopy((caddr_t)state->sa_xaccm, (caddr_t)loc_xaccm, sizeof (loc_xaccm));
10507c478bd9Sstevel@tonic-gate 	flags = state->sa_flags;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	/*
10537c478bd9Sstevel@tonic-gate 	 * LCP messages must be sent using the default escaping
10547c478bd9Sstevel@tonic-gate 	 * (ACCM).  We bend this rule a little to allow LCP
10557c478bd9Sstevel@tonic-gate 	 * Echo-Request through with the negotiated escaping so that
10567c478bd9Sstevel@tonic-gate 	 * we can detect bad negotiated ACCM values.  If the ACCM is
10577c478bd9Sstevel@tonic-gate 	 * bad, echos will fail and take down the link.
10587c478bd9Sstevel@tonic-gate 	 */
10597c478bd9Sstevel@tonic-gate 	is_lcp = is_ctrl = 0;
10607c478bd9Sstevel@tonic-gate 	code = MSG_BYTE(mp, 0);
10617c478bd9Sstevel@tonic-gate 	if (code == PPP_ALLSTATIONS) {
10627c478bd9Sstevel@tonic-gate 		if (MSG_BYTE(mp, 1) == PPP_UI) {
10637c478bd9Sstevel@tonic-gate 			code = MSG_BYTE(mp, 2);
10647c478bd9Sstevel@tonic-gate 			if (code == (PPP_LCP >> 8) &&
10657c478bd9Sstevel@tonic-gate 			    MSG_BYTE(mp, 3) == (PPP_LCP & 0xFF)) {
10667c478bd9Sstevel@tonic-gate 				if (LCP_USE_DFLT(mp))
10677c478bd9Sstevel@tonic-gate 					is_lcp = 2;
10687c478bd9Sstevel@tonic-gate 				else
10697c478bd9Sstevel@tonic-gate 					is_lcp = 1;	/* Echo-Request */
10707c478bd9Sstevel@tonic-gate 			} else if (!(code & 1) && code > 0x3F)
10717c478bd9Sstevel@tonic-gate 				is_ctrl = 1;
10727c478bd9Sstevel@tonic-gate 		}
10737c478bd9Sstevel@tonic-gate 	} else if (!(code & 1) && code > 0x3F)
10747c478bd9Sstevel@tonic-gate 		is_ctrl = 1;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	/*
10777c478bd9Sstevel@tonic-gate 	 * If it's LCP and not just an LCP Echo-Request, then we need
10787c478bd9Sstevel@tonic-gate 	 * to drop back to default escaping rules temporarily.
10797c478bd9Sstevel@tonic-gate 	 */
10807c478bd9Sstevel@tonic-gate 	if (is_lcp > 1) {
10817c478bd9Sstevel@tonic-gate 		/*
10827c478bd9Sstevel@tonic-gate 		 * force escape on 0x00 through 0x1f
10837c478bd9Sstevel@tonic-gate 		 * and, for RFC 1662 (and ISO 3309:1991), 0x80-0x9f.
10847c478bd9Sstevel@tonic-gate 		 */
10857c478bd9Sstevel@tonic-gate 		loc_xaccm[0] = 0xffffffff;
10867c478bd9Sstevel@tonic-gate 		loc_xaccm[4] = 0xffffffff;
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	fcs16 = PPPINITFCS16;		/* Initial FCS is 0xffff */
10907c478bd9Sstevel@tonic-gate 	fcs32 = PPPINITFCS32;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/*
10937c478bd9Sstevel@tonic-gate 	 * Process this block and the rest (if any) attached to this
10947c478bd9Sstevel@tonic-gate 	 * one.  Note that we quite intentionally ignore the type of
10957c478bd9Sstevel@tonic-gate 	 * the buffer.  The caller has checked that the first buffer
10967c478bd9Sstevel@tonic-gate 	 * is M_DATA; all others must be so, and any that are not are
10977c478bd9Sstevel@tonic-gate 	 * harmless driver errors.
10987c478bd9Sstevel@tonic-gate 	 */
10997c478bd9Sstevel@tonic-gate 	curout = outmp;
11007c478bd9Sstevel@tonic-gate 	tpmax = outmp->b_datap->db_lim;
11017c478bd9Sstevel@tonic-gate 	do {
11027c478bd9Sstevel@tonic-gate 		dp = mp->b_rptr;
11037c478bd9Sstevel@tonic-gate 		while (dp < (ep = mp->b_wptr)) {
11047c478bd9Sstevel@tonic-gate 			/*
11057c478bd9Sstevel@tonic-gate 			 * Calculate maximum safe run length for inner loop,
11067c478bd9Sstevel@tonic-gate 			 * regardless of escaping.
11077c478bd9Sstevel@tonic-gate 			 */
11087c478bd9Sstevel@tonic-gate 			outmp_len = (tpmax - tp) / 2;
11097c478bd9Sstevel@tonic-gate 			if (dp + outmp_len < ep)
11107c478bd9Sstevel@tonic-gate 				ep = dp + outmp_len;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 			/*
11137c478bd9Sstevel@tonic-gate 			 * Select out on CRC type here to make the
11147c478bd9Sstevel@tonic-gate 			 * inner byte loop more efficient.  (We could
11157c478bd9Sstevel@tonic-gate 			 * do both CRCs at all times if we wanted, but
11167c478bd9Sstevel@tonic-gate 			 * that ends up taking an extra 8 cycles per
11177c478bd9Sstevel@tonic-gate 			 * byte -- 47% overhead!)
11187c478bd9Sstevel@tonic-gate 			 */
11197c478bd9Sstevel@tonic-gate 			if (flags & SAF_XMITCRC32) {
11207c478bd9Sstevel@tonic-gate 				while (dp < ep) {
11217c478bd9Sstevel@tonic-gate 					chr = *dp++;
11227c478bd9Sstevel@tonic-gate 					fcs32 = PPPFCS32(fcs32, chr);
11237c478bd9Sstevel@tonic-gate 					if (IN_TX_MAP(chr, loc_xaccm)) {
11247c478bd9Sstevel@tonic-gate 						*tp++ = PPP_ESCAPE;
11257c478bd9Sstevel@tonic-gate 						chr ^= PPP_TRANS;
11267c478bd9Sstevel@tonic-gate 					}
11277c478bd9Sstevel@tonic-gate 					*tp++ = chr;
11287c478bd9Sstevel@tonic-gate 				}
11297c478bd9Sstevel@tonic-gate 			} else {
11307c478bd9Sstevel@tonic-gate 				while (dp < ep) {
11317c478bd9Sstevel@tonic-gate 					chr = *dp++;
11327c478bd9Sstevel@tonic-gate 					fcs16 = PPPFCS16(fcs16, chr);
11337c478bd9Sstevel@tonic-gate 					if (IN_TX_MAP(chr, loc_xaccm)) {
11347c478bd9Sstevel@tonic-gate 						*tp++ = PPP_ESCAPE;
11357c478bd9Sstevel@tonic-gate 						chr ^= PPP_TRANS;
11367c478bd9Sstevel@tonic-gate 					}
11377c478bd9Sstevel@tonic-gate 					*tp++ = chr;
11387c478bd9Sstevel@tonic-gate 				}
11397c478bd9Sstevel@tonic-gate 			}
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 			/*
11427c478bd9Sstevel@tonic-gate 			 * If we limited our run length and we're now low
11437c478bd9Sstevel@tonic-gate 			 * on output space, then allocate a new output buffer.
11447c478bd9Sstevel@tonic-gate 			 * This should rarely happen, unless the output data
11457c478bd9Sstevel@tonic-gate 			 * has a lot of escapes.
11467c478bd9Sstevel@tonic-gate 			 */
11477c478bd9Sstevel@tonic-gate 			if (ep != mp->b_wptr && tpmax - tp < 5) {
11487c478bd9Sstevel@tonic-gate 				KINCR(pks_extrabufs);
11497c478bd9Sstevel@tonic-gate 				/* Get remaining message length */
11507c478bd9Sstevel@tonic-gate 				outmp_len = (mp->b_wptr - dp) +
11517c478bd9Sstevel@tonic-gate 				    msgsize(mp->b_cont);
11527c478bd9Sstevel@tonic-gate 				/* Calculate maximum required space */
11537c478bd9Sstevel@tonic-gate 				outmp_len = (outmp_len + PPP_FCS32LEN) * 2 + 1;
11547c478bd9Sstevel@tonic-gate 				curout = allocb(outmp_len, BPRI_MED);
11557c478bd9Sstevel@tonic-gate 				if ((outmp->b_cont = curout) == NULL)
11567c478bd9Sstevel@tonic-gate 					goto outallocfail;
11577c478bd9Sstevel@tonic-gate 				outmp->b_wptr = tp;
11587c478bd9Sstevel@tonic-gate 				tp = curout->b_wptr;
11597c478bd9Sstevel@tonic-gate 				tpmax = curout->b_datap->db_lim;
11607c478bd9Sstevel@tonic-gate 			}
11617c478bd9Sstevel@tonic-gate 		}
11627c478bd9Sstevel@tonic-gate 		tmp = mp->b_cont;
11637c478bd9Sstevel@tonic-gate 		freeb(mp);
11647c478bd9Sstevel@tonic-gate 		mp = tmp;
11657c478bd9Sstevel@tonic-gate 	} while (mp != NULL);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	/*
11687c478bd9Sstevel@tonic-gate 	 * Make sure we have enough remaining room to add the CRC (if
11697c478bd9Sstevel@tonic-gate 	 * any) and a trailing flag byte.
11707c478bd9Sstevel@tonic-gate 	 */
11717c478bd9Sstevel@tonic-gate 	outmp_len = PPP_FCS32LEN * 2 + 1;
11727c478bd9Sstevel@tonic-gate 	if (tpmax - tp < outmp_len) {
11737c478bd9Sstevel@tonic-gate 		KINCR(pks_extrabufs);
11747c478bd9Sstevel@tonic-gate 		curout = allocb(outmp_len, BPRI_MED);
11757c478bd9Sstevel@tonic-gate 		if ((outmp->b_cont = curout) == NULL)
11767c478bd9Sstevel@tonic-gate 			goto outallocfail;
11777c478bd9Sstevel@tonic-gate 		outmp->b_wptr = tp;
11787c478bd9Sstevel@tonic-gate 		tp = curout->b_wptr;
11797c478bd9Sstevel@tonic-gate 		tpmax = curout->b_datap->db_lim;
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	/*
11837c478bd9Sstevel@tonic-gate 	 * Network layer data is the only thing that can be sent with
11847c478bd9Sstevel@tonic-gate 	 * no CRC at all.
11857c478bd9Sstevel@tonic-gate 	 */
11867c478bd9Sstevel@tonic-gate 	if ((flags & SAF_XMITCRCNONE) && !is_lcp && !is_ctrl)
11877c478bd9Sstevel@tonic-gate 		goto nocrc;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	if (!(flags & SAF_XMITCRC32))
11907c478bd9Sstevel@tonic-gate 		fcs32 = fcs16;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	/*
11937c478bd9Sstevel@tonic-gate 	 * Append the HDLC FCS, making sure that escaping is done on any
11947c478bd9Sstevel@tonic-gate 	 * necessary bytes. Note that the FCS bytes are in little-endian.
11957c478bd9Sstevel@tonic-gate 	 */
11967c478bd9Sstevel@tonic-gate 	fcs32 = ~fcs32;
11977c478bd9Sstevel@tonic-gate 	chr = fcs32 & 0xff;
11987c478bd9Sstevel@tonic-gate 	if (IN_TX_MAP(chr, loc_xaccm)) {
11997c478bd9Sstevel@tonic-gate 		*tp++ = PPP_ESCAPE;
12007c478bd9Sstevel@tonic-gate 		chr ^= PPP_TRANS;
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate 	*tp++ = chr;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	chr = (fcs32 >> 8) & 0xff;
12057c478bd9Sstevel@tonic-gate 	if (IN_TX_MAP(chr, loc_xaccm)) {
12067c478bd9Sstevel@tonic-gate 		*tp++ = PPP_ESCAPE;
12077c478bd9Sstevel@tonic-gate 		chr ^= PPP_TRANS;
12087c478bd9Sstevel@tonic-gate 	}
12097c478bd9Sstevel@tonic-gate 	*tp++ = chr;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	if (flags & SAF_XMITCRC32) {
12127c478bd9Sstevel@tonic-gate 		chr = (fcs32 >> 16) & 0xff;
12137c478bd9Sstevel@tonic-gate 		if (IN_TX_MAP(chr, loc_xaccm)) {
12147c478bd9Sstevel@tonic-gate 			*tp++ = PPP_ESCAPE;
12157c478bd9Sstevel@tonic-gate 			chr ^= PPP_TRANS;
12167c478bd9Sstevel@tonic-gate 		}
12177c478bd9Sstevel@tonic-gate 		*tp++ = chr;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 		chr = (fcs32 >> 24) & 0xff;
12207c478bd9Sstevel@tonic-gate 		if (IN_TX_MAP(chr, loc_xaccm)) {
12217c478bd9Sstevel@tonic-gate 			*tp++ = PPP_ESCAPE;
12227c478bd9Sstevel@tonic-gate 			chr ^= PPP_TRANS;
12237c478bd9Sstevel@tonic-gate 		}
12247c478bd9Sstevel@tonic-gate 		*tp++ = chr;
12257c478bd9Sstevel@tonic-gate 	}
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate nocrc:
12287c478bd9Sstevel@tonic-gate 	/*
12297c478bd9Sstevel@tonic-gate 	 * And finally append the HDLC flag, and send it away
12307c478bd9Sstevel@tonic-gate 	 */
12317c478bd9Sstevel@tonic-gate 	*tp++ = PPP_FLAG;
12327c478bd9Sstevel@tonic-gate 	ASSERT(tp < tpmax);
12337c478bd9Sstevel@tonic-gate 	curout->b_wptr = tp;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	state->sa_stats.ppp_obytes += msgsize(outmp);
12367c478bd9Sstevel@tonic-gate 	state->sa_stats.ppp_opackets++;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	if (state->sa_flags & SAF_XMITDUMP)
12397c478bd9Sstevel@tonic-gate 		ppp_dump_frame(state, outmp, "sent");
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	KINCR(pks_dataout);
12427c478bd9Sstevel@tonic-gate 	return (outmp);
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate outallocfail:
12457c478bd9Sstevel@tonic-gate 	KINCR(pks_outallocfails);
12467c478bd9Sstevel@tonic-gate 	state->sa_stats.ppp_oerrors++;
12477c478bd9Sstevel@tonic-gate 	freemsg(outmp);
12487c478bd9Sstevel@tonic-gate 	freemsg(mp);
12497c478bd9Sstevel@tonic-gate 	(void) putnextctl1(RD(q), M_CTL, PPPCTL_OERROR);
12507c478bd9Sstevel@tonic-gate 	return (NULL);
12517c478bd9Sstevel@tonic-gate }
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate /*
12547c478bd9Sstevel@tonic-gate  * Handle end-of-frame excitement.  This is here mostly because the Solaris
12557c478bd9Sstevel@tonic-gate  * C style rules require tab for indent and prohibit excessive indenting.
12567c478bd9Sstevel@tonic-gate  */
12577c478bd9Sstevel@tonic-gate static mblk_t *
receive_frame(queue_t * q,mblk_t * outmp,ushort_t fcs16,uint32_t fcs32)12587c478bd9Sstevel@tonic-gate receive_frame(queue_t *q, mblk_t *outmp, ushort_t fcs16, uint32_t fcs32)
12597c478bd9Sstevel@tonic-gate {
12607c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t *state = (sppp_ahdlc_t *)q->q_ptr;
12617c478bd9Sstevel@tonic-gate 	uchar_t *cp, *ep;
12627c478bd9Sstevel@tonic-gate 	int is_lcp, is_ctrl, crclen;
12637c478bd9Sstevel@tonic-gate 	ushort_t 	proto;
12647c478bd9Sstevel@tonic-gate 	int i;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	cp = outmp->b_rptr;
12677c478bd9Sstevel@tonic-gate 	if (cp[0] == PPP_ALLSTATIONS && cp[1] == PPP_UI)
12687c478bd9Sstevel@tonic-gate 		cp += 2;
12697c478bd9Sstevel@tonic-gate 	proto = *cp++;
12707c478bd9Sstevel@tonic-gate 	if ((proto & 1) == 0)
12717c478bd9Sstevel@tonic-gate 		proto = (proto << 8) + *cp++;
12727c478bd9Sstevel@tonic-gate 	is_lcp = (proto == PPP_LCP);
12737c478bd9Sstevel@tonic-gate 	is_ctrl = (proto >= 0x4000);
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	/*
12767c478bd9Sstevel@tonic-gate 	 * To allow for renegotiation, LCP accepts good CRCs of either
12777c478bd9Sstevel@tonic-gate 	 * type at any time.  Other control (non-network) packets must
12787c478bd9Sstevel@tonic-gate 	 * have either CRC-16 or CRC-32, as negotiated.  Network layer
12797c478bd9Sstevel@tonic-gate 	 * packets may additionally omit the CRC entirely, if that was
12807c478bd9Sstevel@tonic-gate 	 * negotiated.
12817c478bd9Sstevel@tonic-gate 	 */
12827c478bd9Sstevel@tonic-gate 	if ((is_lcp && (fcs16 == PPPGOODFCS16 || fcs32 == PPPGOODFCS32)) ||
12837c478bd9Sstevel@tonic-gate 	    ((fcs16 == PPPGOODFCS16 && !(state->sa_flags & SAF_RECVCRC32)) ||
1284002c70ffScarlsonj 	    (fcs32 == PPPGOODFCS32 &&
1285002c70ffScarlsonj 	    (state->sa_flags & SAF_RECVCRC32))) ||
12867c478bd9Sstevel@tonic-gate 	    (!is_ctrl && !is_lcp && (state->sa_flags & SAF_RECVCRCNONE))) {
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 		state->sa_stats.ppp_ipackets++;
12897c478bd9Sstevel@tonic-gate 		if (is_lcp) {
12907c478bd9Sstevel@tonic-gate 			crclen = (fcs16 == PPPGOODFCS16) ?
12917c478bd9Sstevel@tonic-gate 			    PPP_FCSLEN : PPP_FCS32LEN;
12927c478bd9Sstevel@tonic-gate 		} else {
12937c478bd9Sstevel@tonic-gate 			crclen = (state->sa_flags & SAF_RECVCRC32) ?
12947c478bd9Sstevel@tonic-gate 			    PPP_FCS32LEN : PPP_FCSLEN;
12957c478bd9Sstevel@tonic-gate 			if (!is_ctrl && (state->sa_flags & SAF_RECVCRCNONE))
12967c478bd9Sstevel@tonic-gate 				crclen = 0;
12977c478bd9Sstevel@tonic-gate 		}
12987c478bd9Sstevel@tonic-gate 		if (crclen != 0) {
12997c478bd9Sstevel@tonic-gate 			i = adjmsg(outmp, -crclen);
13007c478bd9Sstevel@tonic-gate 			ASSERT(i != 0);
13017c478bd9Sstevel@tonic-gate #if defined(lint) || defined(_lint)
13027c478bd9Sstevel@tonic-gate 			/* lint is happier this way in a non-DEBUG build */
13037c478bd9Sstevel@tonic-gate 			i = i;
13047c478bd9Sstevel@tonic-gate #endif
13057c478bd9Sstevel@tonic-gate 		}
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 		if (proto == PPP_MUX) {
13087c478bd9Sstevel@tonic-gate 			/* spppasyn_inpkt checks for PPP_MUX packets */
13097c478bd9Sstevel@tonic-gate 			KINCR(pks_recvmux);
13107c478bd9Sstevel@tonic-gate 			/* Remove headers */
13117c478bd9Sstevel@tonic-gate 			outmp->b_rptr = cp;
13127c478bd9Sstevel@tonic-gate 			return (spppasyn_inpkt(q, outmp));
13137c478bd9Sstevel@tonic-gate 		}
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 		/*
13167c478bd9Sstevel@tonic-gate 		 * Sniff the received data stream.  If we see an LCP
13177c478bd9Sstevel@tonic-gate 		 * Configure-Ack, then pick out the ACCM setting, if
13187c478bd9Sstevel@tonic-gate 		 * any, and configure now.  This allows us to stay in
13197c478bd9Sstevel@tonic-gate 		 * sync in case the peer is already out of Establish
13207c478bd9Sstevel@tonic-gate 		 * phase.
13217c478bd9Sstevel@tonic-gate 		 */
13227c478bd9Sstevel@tonic-gate 		if (is_lcp && *cp == 2) {
13237c478bd9Sstevel@tonic-gate 			ep = outmp->b_wptr;
13247c478bd9Sstevel@tonic-gate 			i = (cp[2] << 8) | cp[3];
13257c478bd9Sstevel@tonic-gate 			if (i > ep - cp)
13267c478bd9Sstevel@tonic-gate 				ep = cp;	/* Discard junk */
13277c478bd9Sstevel@tonic-gate 			else if (i < ep - cp)
13287c478bd9Sstevel@tonic-gate 				ep = cp + i;
13297c478bd9Sstevel@tonic-gate 			cp += 4;
13307c478bd9Sstevel@tonic-gate 			while (cp + 2 < ep) {
13317c478bd9Sstevel@tonic-gate 				if ((i = cp[1]) < 2)
13327c478bd9Sstevel@tonic-gate 					i = 2;
13337c478bd9Sstevel@tonic-gate 				if (cp + i > ep)
13347c478bd9Sstevel@tonic-gate 					i = ep - cp;
13357c478bd9Sstevel@tonic-gate 				if (cp[0] == 2 && i >= 6) {
13367c478bd9Sstevel@tonic-gate 					state->sa_raccm = (cp[2] << 24) |
13377c478bd9Sstevel@tonic-gate 					    (cp[3] << 16) | (cp[4] << 8) |
13387c478bd9Sstevel@tonic-gate 					    cp[5];
13397c478bd9Sstevel@tonic-gate 					break;
13407c478bd9Sstevel@tonic-gate 				}
13417c478bd9Sstevel@tonic-gate 				cp += i;
13427c478bd9Sstevel@tonic-gate 			}
13437c478bd9Sstevel@tonic-gate 		}
13447c478bd9Sstevel@tonic-gate 		return (outmp);
13457c478bd9Sstevel@tonic-gate 	} else {
13467c478bd9Sstevel@tonic-gate 		KINCR(pks_incrcerrs);
13477c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, PPP_DRV_NAME "%d: bad fcs (len=%ld)\n",
13487c478bd9Sstevel@tonic-gate 		    state->sa_unit, msgsize(outmp));
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 		if (state->sa_flags & SAF_RECVDUMP)
13517c478bd9Sstevel@tonic-gate 			ppp_dump_frame(state, outmp, "bad data");
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 		freemsg(outmp);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 		state->sa_stats.ppp_ierrors++;
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 		(void) putnextctl1(q, M_CTL, PPPCTL_IERROR);
13587c478bd9Sstevel@tonic-gate 		return (NULL);
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate }
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate /*
13637c478bd9Sstevel@tonic-gate  * ahdlc_decode()
13647c478bd9Sstevel@tonic-gate  *
13657c478bd9Sstevel@tonic-gate  * Process received characters.
13667c478bd9Sstevel@tonic-gate  *
13677c478bd9Sstevel@tonic-gate  * This is handled as exclusive inner so that we don't get confused
13687c478bd9Sstevel@tonic-gate  * about the state.  Returns a list of packets linked by b_next.
13697c478bd9Sstevel@tonic-gate  */
13707c478bd9Sstevel@tonic-gate static mblk_t *
ahdlc_decode(queue_t * q,mblk_t * mp)13717c478bd9Sstevel@tonic-gate ahdlc_decode(queue_t *q, mblk_t  *mp)
13727c478bd9Sstevel@tonic-gate {
13737c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t	*state;
13747c478bd9Sstevel@tonic-gate 	mblk_t		*retmp;		/* list of packets to return */
13757c478bd9Sstevel@tonic-gate 	mblk_t		*outmp;		/* buffer for decoded data */
13767c478bd9Sstevel@tonic-gate 	mblk_t		*mpnext;	/* temporary ptr for unlinking */
13777c478bd9Sstevel@tonic-gate 	uchar_t		*dp;		/* pointer to input data */
13787c478bd9Sstevel@tonic-gate 	uchar_t		*dpend;		/* end of input data */
13797c478bd9Sstevel@tonic-gate 	uchar_t		*tp;		/* pointer to decoded output data */
13807c478bd9Sstevel@tonic-gate 	uchar_t		*tpmax;		/* output buffer limit */
13817c478bd9Sstevel@tonic-gate 	int		flagtmp;	/* temporary cache of flags */
13827c478bd9Sstevel@tonic-gate #if defined(lint) || defined(_lint)
13837c478bd9Sstevel@tonic-gate 	uchar_t		chr;	/* lint likes this */
13847c478bd9Sstevel@tonic-gate #else
13857c478bd9Sstevel@tonic-gate 	int		chr;	/* not uchar_t; more efficient this way */
13867c478bd9Sstevel@tonic-gate 				/* with WorkShop compiler */
13877c478bd9Sstevel@tonic-gate #endif
13887c478bd9Sstevel@tonic-gate 	ushort_t	fcs16;		/* running CRC-16 */
13897c478bd9Sstevel@tonic-gate 	uint32_t	fcs32;		/* running CRC-32 */
13907c478bd9Sstevel@tonic-gate #ifdef HANDLE_ZERO_LENGTH
13917c478bd9Sstevel@tonic-gate 	size_t		nprocessed;
13927c478bd9Sstevel@tonic-gate #endif
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	state = (sppp_ahdlc_t *)q->q_ptr;
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	KINCR(pks_datain);
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	state->sa_stats.ppp_ibytes += msgsize(mp);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	if (state->sa_flags & SAF_RECVDUMP)
14017c478bd9Sstevel@tonic-gate 		ppp_dump_frame(state, mp, "rcvd");
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	flagtmp = state->sa_flags;
14047c478bd9Sstevel@tonic-gate 	fcs16 = state->sa_infcs16;
14057c478bd9Sstevel@tonic-gate 	fcs32 = state->sa_infcs32;
14067c478bd9Sstevel@tonic-gate 	outmp = state->sa_rx_buf;
14077c478bd9Sstevel@tonic-gate 	if (outmp == NULL) {
14087c478bd9Sstevel@tonic-gate 		tp = tpmax = NULL;
14097c478bd9Sstevel@tonic-gate 	} else {
14107c478bd9Sstevel@tonic-gate 		tp = outmp->b_wptr;
14117c478bd9Sstevel@tonic-gate 		tpmax = outmp->b_datap->db_lim;
14127c478bd9Sstevel@tonic-gate 	}
14137c478bd9Sstevel@tonic-gate #ifdef HANDLE_ZERO_LENGTH
14147c478bd9Sstevel@tonic-gate 	nprocessed = 0;
14157c478bd9Sstevel@tonic-gate #endif
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	/*
14187c478bd9Sstevel@tonic-gate 	 * Main input processing loop.  Loop over received buffers and
14197c478bd9Sstevel@tonic-gate 	 * each byte in each buffer.  Note that we quite intentionally
14207c478bd9Sstevel@tonic-gate 	 * ignore the type of the buffer.  The caller has checked that
14217c478bd9Sstevel@tonic-gate 	 * the first buffer is M_DATA; all others must be so, and any
14227c478bd9Sstevel@tonic-gate 	 * that are not are harmless driver errors.
14237c478bd9Sstevel@tonic-gate 	 */
14247c478bd9Sstevel@tonic-gate 	retmp = NULL;
14257c478bd9Sstevel@tonic-gate 	while (mp != NULL) {
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 		/* Innermost loop -- examine bytes in buffer. */
14287c478bd9Sstevel@tonic-gate 		dpend = mp->b_wptr;
14297c478bd9Sstevel@tonic-gate 		dp = mp->b_rptr;
14307c478bd9Sstevel@tonic-gate #ifdef HANDLE_ZERO_LENGTH
14317c478bd9Sstevel@tonic-gate 		nprocessed += dpend - dp;
14327c478bd9Sstevel@tonic-gate #endif
14337c478bd9Sstevel@tonic-gate 		for (; dp < dpend; dp++) {
14347c478bd9Sstevel@tonic-gate 			chr = *dp;
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 			/*
14377c478bd9Sstevel@tonic-gate 			 * This should detect the lack of an 8-bit
14387c478bd9Sstevel@tonic-gate 			 * communication channel, which is necessary
14397c478bd9Sstevel@tonic-gate 			 * for PPP to work.
14407c478bd9Sstevel@tonic-gate 			 */
14417c478bd9Sstevel@tonic-gate 			flagtmp |= charflags[chr];
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 			/*
14447c478bd9Sstevel@tonic-gate 			 * So we have a HDLC flag ...
14457c478bd9Sstevel@tonic-gate 			 */
14467c478bd9Sstevel@tonic-gate 			if (chr == PPP_FLAG) {
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 				/*
14497c478bd9Sstevel@tonic-gate 				 * If there's no received buffer, then
14507c478bd9Sstevel@tonic-gate 				 * just ignore this frame marker.
14517c478bd9Sstevel@tonic-gate 				 */
14527c478bd9Sstevel@tonic-gate 				if ((flagtmp & SAF_IFLUSH) || outmp == NULL) {
14537c478bd9Sstevel@tonic-gate 					flagtmp &= ~SAF_IFLUSH & ~SAF_ESCAPED;
14547c478bd9Sstevel@tonic-gate 					continue;
14557c478bd9Sstevel@tonic-gate 				}
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 				/*
14587c478bd9Sstevel@tonic-gate 				 * Per RFC 1662 -- silently discard
14597c478bd9Sstevel@tonic-gate 				 * runt frames (fewer than 4 octets
14607c478bd9Sstevel@tonic-gate 				 * with 16 bit CRC) and frames that
14617c478bd9Sstevel@tonic-gate 				 * end in 7D 7E (abort sequence).
14627c478bd9Sstevel@tonic-gate 				 * These are not counted as errors.
14637c478bd9Sstevel@tonic-gate 				 *
14647c478bd9Sstevel@tonic-gate 				 * (We could just reset the pointers
14657c478bd9Sstevel@tonic-gate 				 * and reuse the buffer, but this is a
14667c478bd9Sstevel@tonic-gate 				 * rarely used error path and not
14677c478bd9Sstevel@tonic-gate 				 * worth the optimization.)
14687c478bd9Sstevel@tonic-gate 				 */
14697c478bd9Sstevel@tonic-gate 				if ((flagtmp & SAF_ESCAPED) ||
14707c478bd9Sstevel@tonic-gate 				    tp - outmp->b_rptr < 2 + PPP_FCSLEN) {
14717c478bd9Sstevel@tonic-gate 					if (flagtmp & SAF_ESCAPED)
14727c478bd9Sstevel@tonic-gate 						KINCR(pks_inaborts);
14737c478bd9Sstevel@tonic-gate 					else
14747c478bd9Sstevel@tonic-gate 						KINCR(pks_inrunts);
14757c478bd9Sstevel@tonic-gate 					if (state->sa_flags & SAF_RECVDUMP) {
14767c478bd9Sstevel@tonic-gate 						outmp->b_wptr = tp;
14777c478bd9Sstevel@tonic-gate 						ppp_dump_frame(state, outmp,
14787c478bd9Sstevel@tonic-gate 						    "runt");
14797c478bd9Sstevel@tonic-gate 					}
14807c478bd9Sstevel@tonic-gate 					freemsg(outmp);
14817c478bd9Sstevel@tonic-gate 					flagtmp &= ~SAF_ESCAPED;
14827c478bd9Sstevel@tonic-gate 				} else {
14837c478bd9Sstevel@tonic-gate 					/* Handle the received frame */
14847c478bd9Sstevel@tonic-gate 					outmp->b_wptr = tp;
14857c478bd9Sstevel@tonic-gate 					outmp = receive_frame(q, outmp, fcs16,
14867c478bd9Sstevel@tonic-gate 					    fcs32);
14877c478bd9Sstevel@tonic-gate 					retmp = sppp_mappend(retmp, outmp);
14887c478bd9Sstevel@tonic-gate 				}
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 				outmp = NULL;
14917c478bd9Sstevel@tonic-gate 				tp = tpmax = NULL;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 				continue;
14947c478bd9Sstevel@tonic-gate 			}
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 			/* If we're waiting for a new frame, then drop data. */
14977c478bd9Sstevel@tonic-gate 			if (flagtmp & SAF_IFLUSH) {
14987c478bd9Sstevel@tonic-gate 				continue;
14997c478bd9Sstevel@tonic-gate 			}
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 			/*
15027c478bd9Sstevel@tonic-gate 			 * Start of new frame.  Allocate a receive
15037c478bd9Sstevel@tonic-gate 			 * buffer large enough to store a frame (after
15047c478bd9Sstevel@tonic-gate 			 * un-escaping) of at least 1500 octets plus
15057c478bd9Sstevel@tonic-gate 			 * the CRC.  If MRU is negotiated to be more
15067c478bd9Sstevel@tonic-gate 			 * than the default, then allocate that much.
15077c478bd9Sstevel@tonic-gate 			 * In addition, we add an extra 32-bytes for a
15087c478bd9Sstevel@tonic-gate 			 * fudge factor, in case the peer doesn't do
15097c478bd9Sstevel@tonic-gate 			 * arithmetic very well.
15107c478bd9Sstevel@tonic-gate 			 */
15117c478bd9Sstevel@tonic-gate 			if (outmp == NULL) {
15127c478bd9Sstevel@tonic-gate 				int maxlen;
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 				if ((maxlen = state->sa_mru) < PPP_MRU)
1515002c70ffScarlsonj 					maxlen = PPP_MRU;
15167c478bd9Sstevel@tonic-gate 				maxlen += PPP_FCS32LEN + 32;
15177c478bd9Sstevel@tonic-gate 				outmp = allocb(maxlen, BPRI_MED);
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 				/*
15207c478bd9Sstevel@tonic-gate 				 * If allocation fails, try again on
15217c478bd9Sstevel@tonic-gate 				 * the next frame.  (Go into discard
15227c478bd9Sstevel@tonic-gate 				 * mode.)
15237c478bd9Sstevel@tonic-gate 				 */
15247c478bd9Sstevel@tonic-gate 				if (outmp == NULL) {
15257c478bd9Sstevel@tonic-gate 					KINCR(pks_inallocfails);
15267c478bd9Sstevel@tonic-gate 					flagtmp |= SAF_IFLUSH;
15277c478bd9Sstevel@tonic-gate 					continue;
15287c478bd9Sstevel@tonic-gate 				}
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 				tp = outmp->b_wptr;
15317c478bd9Sstevel@tonic-gate 				tpmax = outmp->b_datap->db_lim;
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 				/* Neither flag can possibly be set here. */
15347c478bd9Sstevel@tonic-gate 				flagtmp &= ~(SAF_IFLUSH | SAF_ESCAPED);
15357c478bd9Sstevel@tonic-gate 				fcs16 = PPPINITFCS16;
15367c478bd9Sstevel@tonic-gate 				fcs32 = PPPINITFCS32;
15377c478bd9Sstevel@tonic-gate 			}
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 			/*
15407c478bd9Sstevel@tonic-gate 			 * If the peer sends us a character that's in
15417c478bd9Sstevel@tonic-gate 			 * our receive character map, then that's
15427c478bd9Sstevel@tonic-gate 			 * junk.  Discard it without changing state.
1543*48bbca81SDaniel Hoffman 			 * If they previously sent us an escape
15447c478bd9Sstevel@tonic-gate 			 * character, then toggle this one and
1545*48bbca81SDaniel Hoffman 			 * continue.  Otherwise, if they're now sending
15467c478bd9Sstevel@tonic-gate 			 * escape, set the flag for next time.
15477c478bd9Sstevel@tonic-gate 			 */
15487c478bd9Sstevel@tonic-gate 			if (IN_RX_MAP(chr, state->sa_raccm)) {
15497c478bd9Sstevel@tonic-gate 				KINCR(pks_inbadchars);
15507c478bd9Sstevel@tonic-gate 				KOR(pks_inbadcharmask, 1 << chr);
15517c478bd9Sstevel@tonic-gate 				continue;
15527c478bd9Sstevel@tonic-gate 			}
15537c478bd9Sstevel@tonic-gate 			if (flagtmp & SAF_ESCAPED) {
15547c478bd9Sstevel@tonic-gate 				chr ^= PPP_TRANS;
15557c478bd9Sstevel@tonic-gate 				flagtmp &= ~SAF_ESCAPED;
15567c478bd9Sstevel@tonic-gate 			} else if (chr == PPP_ESCAPE) {
15577c478bd9Sstevel@tonic-gate 				flagtmp |= SAF_ESCAPED;
15587c478bd9Sstevel@tonic-gate 				continue;
15597c478bd9Sstevel@tonic-gate 			}
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 			/*
15627c478bd9Sstevel@tonic-gate 			 * Unless the peer is confused about the
15637c478bd9Sstevel@tonic-gate 			 * negotiated MRU, we should never get a frame
15647c478bd9Sstevel@tonic-gate 			 * that is too long.  If it happens, toss it
15657c478bd9Sstevel@tonic-gate 			 * away and begin discarding data until we see
15667c478bd9Sstevel@tonic-gate 			 * the end of the frame.
15677c478bd9Sstevel@tonic-gate 			 */
15687c478bd9Sstevel@tonic-gate 			if (tp < tpmax) {
15697c478bd9Sstevel@tonic-gate 				fcs16 = PPPFCS16(fcs16, chr);
15707c478bd9Sstevel@tonic-gate 				fcs32 = PPPFCS32(fcs32, chr);
15717c478bd9Sstevel@tonic-gate 				*tp++ = chr;
15727c478bd9Sstevel@tonic-gate 			} else {
15737c478bd9Sstevel@tonic-gate 				KINCR(pks_intoolongs);
15747c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, PPP_DRV_NAME
15757c478bd9Sstevel@tonic-gate 				    "%d: frame too long (%d bytes)\n",
15767c478bd9Sstevel@tonic-gate 				    state->sa_unit,
15777c478bd9Sstevel@tonic-gate 				    (int)(tpmax - outmp->b_rptr));
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 				freemsg(outmp);
15807c478bd9Sstevel@tonic-gate 				outmp = NULL;
15817c478bd9Sstevel@tonic-gate 				tp = tpmax = NULL;
15827c478bd9Sstevel@tonic-gate 				flagtmp |= SAF_IFLUSH;
15837c478bd9Sstevel@tonic-gate 			}
15847c478bd9Sstevel@tonic-gate 		}
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 		/*
15877c478bd9Sstevel@tonic-gate 		 * Free the buffer we just processed and move on to
15887c478bd9Sstevel@tonic-gate 		 * the next one.
15897c478bd9Sstevel@tonic-gate 		 */
15907c478bd9Sstevel@tonic-gate 		mpnext = mp->b_cont;
15917c478bd9Sstevel@tonic-gate 		freeb(mp);
15927c478bd9Sstevel@tonic-gate 		mp = mpnext;
15937c478bd9Sstevel@tonic-gate 	}
15947c478bd9Sstevel@tonic-gate 	state->sa_flags = flagtmp;
15957c478bd9Sstevel@tonic-gate 	if ((state->sa_rx_buf = outmp) != NULL)
15967c478bd9Sstevel@tonic-gate 		outmp->b_wptr = tp;
15977c478bd9Sstevel@tonic-gate 	state->sa_infcs16 = fcs16;
15987c478bd9Sstevel@tonic-gate 	state->sa_infcs32 = fcs32;
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate #ifdef HANDLE_ZERO_LENGTH
16017c478bd9Sstevel@tonic-gate 	if (nprocessed <= 0) {
16027c478bd9Sstevel@tonic-gate 		outmp = allocb(0, BPRI_MED);
16037c478bd9Sstevel@tonic-gate 		if (outmp != NULL) {
16047c478bd9Sstevel@tonic-gate 			outmp->b_datap->db_type = M_HANGUP;
16057c478bd9Sstevel@tonic-gate 			retmp = sppp_mappend(retmp, outmp);
16067c478bd9Sstevel@tonic-gate 		}
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate #endif
16097c478bd9Sstevel@tonic-gate 	return (retmp);
16107c478bd9Sstevel@tonic-gate }
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate /*
16137c478bd9Sstevel@tonic-gate  * Nifty packet dumper; copied from AIX 4.1 port.  This routine dumps
16147c478bd9Sstevel@tonic-gate  * the raw received and transmitted data through syslog.  This allows
16157c478bd9Sstevel@tonic-gate  * debug of communications problems without resorting to a line
16167c478bd9Sstevel@tonic-gate  * analyzer.
16177c478bd9Sstevel@tonic-gate  *
16187c478bd9Sstevel@tonic-gate  * The expression "3*BYTES_PER_LINE" used frequently here represents
16197c478bd9Sstevel@tonic-gate  * the size of each hex value printed -- two hex digits and a space.
16207c478bd9Sstevel@tonic-gate  */
16217c478bd9Sstevel@tonic-gate #define	BYTES_PER_LINE	8
16227c478bd9Sstevel@tonic-gate static void
ppp_dump_frame(sppp_ahdlc_t * state,mblk_t * mptr,const char * msg)16237c478bd9Sstevel@tonic-gate ppp_dump_frame(sppp_ahdlc_t *state, mblk_t *mptr, const char *msg)
16247c478bd9Sstevel@tonic-gate {
16257c478bd9Sstevel@tonic-gate 	/*
16267c478bd9Sstevel@tonic-gate 	 * Buffer is big enough for hex digits, two spaces, ASCII output,
16277c478bd9Sstevel@tonic-gate 	 * and one NUL byte.
16287c478bd9Sstevel@tonic-gate 	 */
16297c478bd9Sstevel@tonic-gate 	char buf[3 * BYTES_PER_LINE + 2 + BYTES_PER_LINE + 1];
16307c478bd9Sstevel@tonic-gate 	uchar_t *rptr, *eptr;
16317c478bd9Sstevel@tonic-gate 	int i, chr;
16327c478bd9Sstevel@tonic-gate 	char *bp;
16337c478bd9Sstevel@tonic-gate 	static const char digits[] = "0123456789abcdef";
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "!ppp_async%d: %s %ld bytes\n", state->sa_unit,
16367c478bd9Sstevel@tonic-gate 	    msg, msgsize(mptr));
16377c478bd9Sstevel@tonic-gate 	i = 0;
16387c478bd9Sstevel@tonic-gate 	bp = buf;
16397c478bd9Sstevel@tonic-gate 	/* Add filler spaces between hex output and ASCII */
16407c478bd9Sstevel@tonic-gate 	buf[3 * BYTES_PER_LINE] = ' ';
16417c478bd9Sstevel@tonic-gate 	buf[3 * BYTES_PER_LINE + 1] = ' ';
16427c478bd9Sstevel@tonic-gate 	/* Add NUL byte at end */
16437c478bd9Sstevel@tonic-gate 	buf[sizeof (buf) - 1] = '\0';
16447c478bd9Sstevel@tonic-gate 	while (mptr != NULL) {
16457c478bd9Sstevel@tonic-gate 		rptr = mptr->b_rptr; /* get pointer to beginning  */
16467c478bd9Sstevel@tonic-gate 		eptr = mptr->b_wptr;
16477c478bd9Sstevel@tonic-gate 		while (rptr < eptr) {
16487c478bd9Sstevel@tonic-gate 			chr = *rptr++;
16497c478bd9Sstevel@tonic-gate 			/* convert byte to ascii hex */
16507c478bd9Sstevel@tonic-gate 			*bp++ = digits[chr >> 4];
16517c478bd9Sstevel@tonic-gate 			*bp++ = digits[chr & 0xf];
16527c478bd9Sstevel@tonic-gate 			*bp++ = ' ';
16537c478bd9Sstevel@tonic-gate 			/* Insert ASCII past hex output and filler */
16547c478bd9Sstevel@tonic-gate 			buf[3 * BYTES_PER_LINE + 2 + i] =
16557c478bd9Sstevel@tonic-gate 			    (chr >= 0x20 && chr <= 0x7E) ? (char)chr : '.';
16567c478bd9Sstevel@tonic-gate 			i++;
16577c478bd9Sstevel@tonic-gate 			if (i >= BYTES_PER_LINE) {
16587c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, "!ppp%d: %s\n", state->sa_unit,
16597c478bd9Sstevel@tonic-gate 				    buf);
16607c478bd9Sstevel@tonic-gate 				bp = buf;
16617c478bd9Sstevel@tonic-gate 				i = 0;
16627c478bd9Sstevel@tonic-gate 			}
16637c478bd9Sstevel@tonic-gate 		}
16647c478bd9Sstevel@tonic-gate 		mptr = mptr->b_cont;
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 	if (bp > buf) {
16677c478bd9Sstevel@tonic-gate 		/* fill over unused hex display positions */
16687c478bd9Sstevel@tonic-gate 		while (bp < buf + 3 * BYTES_PER_LINE)
16697c478bd9Sstevel@tonic-gate 			*bp++ = ' ';
16707c478bd9Sstevel@tonic-gate 		/* terminate ASCII string at right position */
16717c478bd9Sstevel@tonic-gate 		buf[3 * BYTES_PER_LINE + 2 + i] = '\0';
16727c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "!ppp%d: %s\n", state->sa_unit, buf);
16737c478bd9Sstevel@tonic-gate 	}
16747c478bd9Sstevel@tonic-gate }
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate static mblk_t *
spppasyn_muxencode(queue_t * q,mblk_t * mp)16777c478bd9Sstevel@tonic-gate spppasyn_muxencode(queue_t *q, mblk_t *mp)
16787c478bd9Sstevel@tonic-gate {
16797c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t	*state = (sppp_ahdlc_t *)q->q_ptr;
16807c478bd9Sstevel@tonic-gate 	uint32_t	len;
16817c478bd9Sstevel@tonic-gate 	uint32_t	nlen;
16827c478bd9Sstevel@tonic-gate 	ushort_t	protolen;
16837c478bd9Sstevel@tonic-gate 	uint32_t	hdrlen;
16847c478bd9Sstevel@tonic-gate 	ushort_t	proto;
16857c478bd9Sstevel@tonic-gate 	mblk_t		*new_frame;
16867c478bd9Sstevel@tonic-gate 	mblk_t		*tmp;
16877c478bd9Sstevel@tonic-gate 	mblk_t		*send_frame;
16887c478bd9Sstevel@tonic-gate 	ushort_t	i;
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	len = msgdsize(mp);
16917c478bd9Sstevel@tonic-gate 	i = 0;
16927c478bd9Sstevel@tonic-gate 	protolen = 1;
16937c478bd9Sstevel@tonic-gate 	proto = MSG_BYTE(mp, i);
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 	if (proto == PPP_ALLSTATIONS) {
16967c478bd9Sstevel@tonic-gate 		len -= 2;
16977c478bd9Sstevel@tonic-gate 		i += 2;
16987c478bd9Sstevel@tonic-gate 		proto = MSG_BYTE(mp, i);
16997c478bd9Sstevel@tonic-gate 	}
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	++i;
17027c478bd9Sstevel@tonic-gate 	if ((proto & 1) == 0) {
17037c478bd9Sstevel@tonic-gate 		proto = (proto << 8) + MSG_BYTE(mp, i);
17047c478bd9Sstevel@tonic-gate 		protolen++;
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	hdrlen = i - 1;
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	send_frame = NULL;
17107c478bd9Sstevel@tonic-gate 	if (len > PPP_MAX_MUX_LEN || (proto & 0x8000)) {
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 		/* send the queued frames */
17137c478bd9Sstevel@tonic-gate 		if (state->sa_mqhead != NULL) {
17147c478bd9Sstevel@tonic-gate 			/* increment counter if it is MUX pkt */
17157c478bd9Sstevel@tonic-gate 			if (state->sa_mqtail != NULL)
17167c478bd9Sstevel@tonic-gate 				KINCR(pks_sentmux);
17177c478bd9Sstevel@tonic-gate 			send_frame = ahdlc_encode(q, state->sa_mqhead);
17187c478bd9Sstevel@tonic-gate 		}
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 		/* send the current frame */
17217c478bd9Sstevel@tonic-gate 		mp = ahdlc_encode(q, mp);
17227c478bd9Sstevel@tonic-gate 		send_frame = sppp_mcat(send_frame, mp);
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 		/* reset the state values over here */
17257c478bd9Sstevel@tonic-gate 		RESET_MUX_VALUES(state);
17267c478bd9Sstevel@tonic-gate 		return (send_frame);
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	/* len + 1 , since we add the mux overhead */
17307c478bd9Sstevel@tonic-gate 	nlen = len + 1;
17317c478bd9Sstevel@tonic-gate 	/* subtract the protocol length if protocol matches */
17327c478bd9Sstevel@tonic-gate 	if (state->sa_proto == proto)
17337c478bd9Sstevel@tonic-gate 		nlen -= protolen;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	send_frame = NULL;
17367c478bd9Sstevel@tonic-gate 	if ((state->sa_mqlen + nlen) >= state->sa_mru) {
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 		/* send the existing queued frames */
17397c478bd9Sstevel@tonic-gate 		if (state->sa_mqhead != NULL) {
17407c478bd9Sstevel@tonic-gate 			/* increment counter if it is MUX pkt */
17417c478bd9Sstevel@tonic-gate 			if (state->sa_mqtail != NULL)
17427c478bd9Sstevel@tonic-gate 				KINCR(pks_sentmux);
17437c478bd9Sstevel@tonic-gate 			send_frame = ahdlc_encode(q, state->sa_mqhead);
17447c478bd9Sstevel@tonic-gate 		}
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 		/* reset state values */
17477c478bd9Sstevel@tonic-gate 		RESET_MUX_VALUES(state);
17487c478bd9Sstevel@tonic-gate 	}
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	/* add the current frame to the queue */
17517c478bd9Sstevel@tonic-gate 	if (state->sa_mqhead != NULL) {
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 		if (state->sa_mqtail == NULL) {
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 			/*
17567c478bd9Sstevel@tonic-gate 			 * this is the first mblk in the queue create
17577c478bd9Sstevel@tonic-gate 			 * a new frame to hold the PPP MUX header
17587c478bd9Sstevel@tonic-gate 			 */
17597c478bd9Sstevel@tonic-gate 			if ((new_frame = allocb(PPP_HDRLEN+1,
17607c478bd9Sstevel@tonic-gate 			    BPRI_MED)) == NULL) {
17617c478bd9Sstevel@tonic-gate 				return (send_frame);
17627c478bd9Sstevel@tonic-gate 			}
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 			if (!IS_COMP_AC(state)) {
17657c478bd9Sstevel@tonic-gate 				/* add the header */
17667c478bd9Sstevel@tonic-gate 				*new_frame->b_wptr++ = PPP_ALLSTATIONS;
17677c478bd9Sstevel@tonic-gate 				*new_frame->b_wptr++ = PPP_UI;
17687c478bd9Sstevel@tonic-gate 			}
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 			/* do protocol compression */
17717c478bd9Sstevel@tonic-gate 			if (IS_COMP_PROT(state)) {
17727c478bd9Sstevel@tonic-gate 				*new_frame->b_wptr++ = PPP_MUX;
17737c478bd9Sstevel@tonic-gate 			} else {
17747c478bd9Sstevel@tonic-gate 				*new_frame->b_wptr++ = 0;
17757c478bd9Sstevel@tonic-gate 				*new_frame->b_wptr++ = PPP_MUX;
17767c478bd9Sstevel@tonic-gate 			}
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 			*new_frame->b_wptr++ = PFF |
17797c478bd9Sstevel@tonic-gate 			    (state->sa_mqlen - protolen - 1);
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 			if (DB_REF(mp) > 1) {
17827c478bd9Sstevel@tonic-gate 				tmp = copymsg(state->sa_mqhead);
17837c478bd9Sstevel@tonic-gate 				freemsg(state->sa_mqhead);
17847c478bd9Sstevel@tonic-gate 				if ((state->sa_mqhead = tmp) == NULL) {
17857c478bd9Sstevel@tonic-gate 					return (send_frame);
17867c478bd9Sstevel@tonic-gate 				}
17877c478bd9Sstevel@tonic-gate 			}
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 			if (state->sa_mqhead->b_rptr[0] == PPP_ALLSTATIONS)
17907c478bd9Sstevel@tonic-gate 				state->sa_mqhead->b_rptr += 2;
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 			linkb(new_frame, state->sa_mqhead);
17937c478bd9Sstevel@tonic-gate 			state->sa_mqtail = state->sa_mqhead;
17947c478bd9Sstevel@tonic-gate 			/* point mqtail to the last mblk_t */
17957c478bd9Sstevel@tonic-gate 			while (state->sa_mqtail->b_cont != NULL)
17967c478bd9Sstevel@tonic-gate 				state->sa_mqtail = state->sa_mqtail->b_cont;
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 			/* change state->sa_mqhead */
17997c478bd9Sstevel@tonic-gate 			state->sa_mqhead = new_frame;
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 		}
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 		if (state->sa_proto == proto) {
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 			/* Check if the mblk_t is being referenced */
18067c478bd9Sstevel@tonic-gate 			if (DB_REF(mp) > 1) {
18077c478bd9Sstevel@tonic-gate 				tmp = copymsg(mp);
18087c478bd9Sstevel@tonic-gate 				freemsg(mp);
18097c478bd9Sstevel@tonic-gate 				if ((mp = tmp) == NULL) {
18107c478bd9Sstevel@tonic-gate 					return (send_frame);
18117c478bd9Sstevel@tonic-gate 				}
18127c478bd9Sstevel@tonic-gate 			}
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 			/*
18157c478bd9Sstevel@tonic-gate 			 * match,can remove the protocol field
18167c478bd9Sstevel@tonic-gate 			 * and write data there
18177c478bd9Sstevel@tonic-gate 			 */
18187c478bd9Sstevel@tonic-gate 			mp->b_rptr += hdrlen;
18197c478bd9Sstevel@tonic-gate 			/*
18207c478bd9Sstevel@tonic-gate 			 * protolen - 1 ,because the the byte with
18217c478bd9Sstevel@tonic-gate 			 * the PFF bit and the length field have
18227c478bd9Sstevel@tonic-gate 			 * to added
18237c478bd9Sstevel@tonic-gate 			 */
18247c478bd9Sstevel@tonic-gate 			mp->b_rptr += (protolen - 1);
18257c478bd9Sstevel@tonic-gate 			*mp->b_rptr = (len - protolen) & 0xff;
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 		} else {
18287c478bd9Sstevel@tonic-gate 			/*
18297c478bd9Sstevel@tonic-gate 			 * no match, there are three options
18307c478bd9Sstevel@tonic-gate 			 * 1. write in mp
18317c478bd9Sstevel@tonic-gate 			 * 2. write in mqtail
18327c478bd9Sstevel@tonic-gate 			 * 3. alloc a new blk for just one byte
18337c478bd9Sstevel@tonic-gate 			 */
18347c478bd9Sstevel@tonic-gate 			/* Check if the mblk_t is being referenced */
18357c478bd9Sstevel@tonic-gate 			if (DB_REF(mp) > 1) {
18367c478bd9Sstevel@tonic-gate 				tmp = copymsg(mp);
18377c478bd9Sstevel@tonic-gate 				freemsg(mp);
18387c478bd9Sstevel@tonic-gate 				if ((mp = tmp) == NULL) {
18397c478bd9Sstevel@tonic-gate 					return (send_frame);
18407c478bd9Sstevel@tonic-gate 				}
18417c478bd9Sstevel@tonic-gate 			}
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 			if (hdrlen != 0) {
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 				mp->b_rptr += (hdrlen-1);
18467c478bd9Sstevel@tonic-gate 				*mp->b_rptr = PFF | (len);
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 			} else if (state->sa_mqtail->b_wptr <
1849002c70ffScarlsonj 			    DB_LIM(state->sa_mqtail)) {
18507c478bd9Sstevel@tonic-gate 					*state->sa_mqtail->b_wptr++ = PFF |len;
18517c478bd9Sstevel@tonic-gate 			} else {
18527c478bd9Sstevel@tonic-gate 				/* allocate a new mblk & add the byte */
18537c478bd9Sstevel@tonic-gate 				/* write the data */
18547c478bd9Sstevel@tonic-gate 				if ((new_frame = allocb(1, BPRI_MED))
18557c478bd9Sstevel@tonic-gate 				    == NULL) {
18567c478bd9Sstevel@tonic-gate 					freemsg(mp);
18577c478bd9Sstevel@tonic-gate 					return (send_frame);
18587c478bd9Sstevel@tonic-gate 				}
18597c478bd9Sstevel@tonic-gate 				*new_frame->b_wptr++ = PFF | (len);
18607c478bd9Sstevel@tonic-gate 				linkb(state->sa_mqtail, new_frame);
18617c478bd9Sstevel@tonic-gate 			}
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 			/* update proto */
18647c478bd9Sstevel@tonic-gate 			state->sa_proto = proto;
18657c478bd9Sstevel@tonic-gate 		}
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 		linkb(state->sa_mqtail, mp);
18687c478bd9Sstevel@tonic-gate 		state->sa_mqtail = mp;
18697c478bd9Sstevel@tonic-gate 		while (state->sa_mqtail->b_cont != NULL)
18707c478bd9Sstevel@tonic-gate 			state->sa_mqtail = state->sa_mqtail->b_cont;
18717c478bd9Sstevel@tonic-gate 		state->sa_mqlen += nlen;
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	} else {
18747c478bd9Sstevel@tonic-gate 		state->sa_mqhead = mp;
18757c478bd9Sstevel@tonic-gate 		state->sa_mqlen = len + protolen + 1;
18767c478bd9Sstevel@tonic-gate 		state->sa_proto = proto;
18777c478bd9Sstevel@tonic-gate 	}
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 	if (state->sa_timeout_id == 0) {
18807c478bd9Sstevel@tonic-gate 		state->sa_timeout_id = qtimeout(q, spppasyn_timer, q,
18817c478bd9Sstevel@tonic-gate 		    (drv_usectohz(state->sa_timeout_usec)));
18827c478bd9Sstevel@tonic-gate 	}
18837c478bd9Sstevel@tonic-gate 	return (send_frame);
18847c478bd9Sstevel@tonic-gate }
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate /*
18877c478bd9Sstevel@tonic-gate  * Called from receive frame, this routine checks if it is a PPP_MUX
18887c478bd9Sstevel@tonic-gate  * packet and demuxes it.  The returned pointer is a chain of mblks
18897c478bd9Sstevel@tonic-gate  * using b_next and representing the demultiplexed packets.
18907c478bd9Sstevel@tonic-gate  */
18917c478bd9Sstevel@tonic-gate static mblk_t *
spppasyn_inpkt(queue_t * q,mblk_t * mp)18927c478bd9Sstevel@tonic-gate spppasyn_inpkt(queue_t *q, mblk_t *mp)
18937c478bd9Sstevel@tonic-gate {
18947c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t	*state = (sppp_ahdlc_t *)q->q_ptr;
18957c478bd9Sstevel@tonic-gate 	ushort_t	proto;
18967c478bd9Sstevel@tonic-gate 	ushort_t	prev_proto;
18977c478bd9Sstevel@tonic-gate 	uint32_t	len;		/* length of subframe */
18987c478bd9Sstevel@tonic-gate 	uchar_t		muxhdr;
18997c478bd9Sstevel@tonic-gate 	mblk_t		*hdrmp;
19007c478bd9Sstevel@tonic-gate 	mblk_t		*subframe;
19017c478bd9Sstevel@tonic-gate 	mblk_t		*retmp;
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	if (!(mp->b_rptr[0] & PFF)) {
19047c478bd9Sstevel@tonic-gate 		KINCR(pks_inmuxerrs);
19057c478bd9Sstevel@tonic-gate 		(void) putnextctl1(q, M_CTL, PPPCTL_IERROR);
19067c478bd9Sstevel@tonic-gate 		freemsg(mp);
19077c478bd9Sstevel@tonic-gate 		return (NULL);
19087c478bd9Sstevel@tonic-gate 	}
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	/* initialise the Last protocol and protocol length */
19117c478bd9Sstevel@tonic-gate 	prev_proto = 0;
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	/*
19147c478bd9Sstevel@tonic-gate 	 * Taking into granted that the decoded frame is contiguous
19157c478bd9Sstevel@tonic-gate 	 */
19167c478bd9Sstevel@tonic-gate 	retmp = NULL;
19177c478bd9Sstevel@tonic-gate 	while (mp->b_rptr < mp->b_wptr) {
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 		/*
19207c478bd9Sstevel@tonic-gate 		 * get the last protocol, protocol length
19217c478bd9Sstevel@tonic-gate 		 * and the length of the message
19227c478bd9Sstevel@tonic-gate 		 */
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 		/* protocol field flag and length */
19257c478bd9Sstevel@tonic-gate 		muxhdr = mp->b_rptr[0];
19267c478bd9Sstevel@tonic-gate 		len = muxhdr & ~PFF;
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 		mp->b_rptr++;
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 		/* check if there and enough bytes left in pkt */
19317c478bd9Sstevel@tonic-gate 		if (MBLKL(mp) < len) {
19327c478bd9Sstevel@tonic-gate 			KINCR(pks_inmuxerrs);
19337c478bd9Sstevel@tonic-gate 			(void) putnextctl1(q, M_CTL, PPPCTL_IERROR);
19347c478bd9Sstevel@tonic-gate 			break;
19357c478bd9Sstevel@tonic-gate 		}
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 		/* allocate memory for the header length */
19387c478bd9Sstevel@tonic-gate 		if ((hdrmp = allocb(PPP_HDRLEN, BPRI_MED)) == NULL) {
19397c478bd9Sstevel@tonic-gate 			KINCR(pks_inallocfails);
19407c478bd9Sstevel@tonic-gate 			break;
19417c478bd9Sstevel@tonic-gate 		}
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 		/* add the ppp header to the pkt */
19447c478bd9Sstevel@tonic-gate 		*hdrmp->b_wptr++ = PPP_ALLSTATIONS;
19457c478bd9Sstevel@tonic-gate 		*hdrmp->b_wptr++ = PPP_UI;
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 		/* check if the protocol field flag is set */
19487c478bd9Sstevel@tonic-gate 		if (muxhdr & PFF) {
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 			/* get the protocol */
19517c478bd9Sstevel@tonic-gate 			proto = MSG_BYTE(mp, 0);
19527c478bd9Sstevel@tonic-gate 			if ((proto & 1) == 0)
19537c478bd9Sstevel@tonic-gate 				proto = (proto << 8) + MSG_BYTE(mp, 1);
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 			/* reset values */
19567c478bd9Sstevel@tonic-gate 			prev_proto = proto;
19577c478bd9Sstevel@tonic-gate 		} else {
19587c478bd9Sstevel@tonic-gate 			if (!IS_DECOMP_PROT(state))
19597c478bd9Sstevel@tonic-gate 				*hdrmp->b_wptr++ = prev_proto >> 8;
19607c478bd9Sstevel@tonic-gate 			*hdrmp->b_wptr++ = (prev_proto & 0xff);
19617c478bd9Sstevel@tonic-gate 		}
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 		/* get the payload from the MUXed packet */
19647c478bd9Sstevel@tonic-gate 		subframe = dupmsg(mp);
19657c478bd9Sstevel@tonic-gate 		subframe->b_wptr = mp->b_rptr + len;
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 		/* link the subframe to the new frame */
19687c478bd9Sstevel@tonic-gate 		linkb(hdrmp, subframe);
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 		/* do a putnext */
19717c478bd9Sstevel@tonic-gate 		retmp = sppp_mappend(retmp, hdrmp);
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 		/* move the read pointer beyond this subframe */
19747c478bd9Sstevel@tonic-gate 		mp->b_rptr += len;
19757c478bd9Sstevel@tonic-gate 	}
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	freemsg(mp);
19787c478bd9Sstevel@tonic-gate 	return (retmp);
19797c478bd9Sstevel@tonic-gate }
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate /*
19837c478bd9Sstevel@tonic-gate  * timer routine which sends out the queued pkts *
19847c478bd9Sstevel@tonic-gate  */
19857c478bd9Sstevel@tonic-gate static void
spppasyn_timer(void * arg)19867c478bd9Sstevel@tonic-gate spppasyn_timer(void *arg)
19877c478bd9Sstevel@tonic-gate {
19887c478bd9Sstevel@tonic-gate 	queue_t *q;
19897c478bd9Sstevel@tonic-gate 	sppp_ahdlc_t *state;
19907c478bd9Sstevel@tonic-gate 	mblk_t *mp;
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	ASSERT(arg);
19937c478bd9Sstevel@tonic-gate 	q = (queue_t *)arg;
19947c478bd9Sstevel@tonic-gate 	state = (sppp_ahdlc_t *)q->q_ptr;
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	if (state->sa_mqhead != NULL) {
19977c478bd9Sstevel@tonic-gate 		/* increment counter */
19987c478bd9Sstevel@tonic-gate 		if (state->sa_mqtail != NULL)
19997c478bd9Sstevel@tonic-gate 			KINCR(pks_sentmux);
20007c478bd9Sstevel@tonic-gate 		if ((mp = ahdlc_encode(q, state->sa_mqhead)) != NULL)
20017c478bd9Sstevel@tonic-gate 			putnext(q, mp);
20027c478bd9Sstevel@tonic-gate 		/* reset the state values over here */
20037c478bd9Sstevel@tonic-gate 		RESET_MUX_VALUES(state);
20047c478bd9Sstevel@tonic-gate 	}
20057c478bd9Sstevel@tonic-gate 	/* clear timeout_id */
20067c478bd9Sstevel@tonic-gate 	state->sa_timeout_id = 0;
20077c478bd9Sstevel@tonic-gate }
2008