17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * spppcomp.c - STREAMS module for kernel-level compression and CCP support.
37c478bd9Sstevel@tonic-gate  *
4f53eecf5SJames Carlson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
648bbca81SDaniel 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  * This module is derived from the original SVR4 STREAMS PPP compression
437c478bd9Sstevel@tonic-gate  * module originally written by Paul Mackerras <paul.mackerras@cs.anu.edu.au>.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * James Carlson <james.d.carlson@sun.com> and Adi Masputra
467c478bd9Sstevel@tonic-gate  * <adi.masputra@sun.com> rewrote and restructured the code for improved
477c478bd9Sstevel@tonic-gate  * performance and scalability.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	RCSID	"$Id: spppcomp.c,v 1.0 2000/05/08 01:10:12 masputra Exp $"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate #include <sys/debug.h>
547c478bd9Sstevel@tonic-gate #include <sys/param.h>
557c478bd9Sstevel@tonic-gate #include <sys/stream.h>
567c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
577c478bd9Sstevel@tonic-gate #include <sys/errno.h>
587c478bd9Sstevel@tonic-gate #include <sys/conf.h>
597c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
607c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
617c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
627c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
637c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
647c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
657c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
667c478bd9Sstevel@tonic-gate #include <netinet/in.h>
677c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
687c478bd9Sstevel@tonic-gate #include <net/ppp_defs.h>
697c478bd9Sstevel@tonic-gate #include <net/pppio.h>
707c478bd9Sstevel@tonic-gate #include <net/vjcompress.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /* Defined for platform-neutral include file */
737c478bd9Sstevel@tonic-gate #define	PACKETPTR	mblk_t *
747c478bd9Sstevel@tonic-gate #include <net/ppp-comp.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include "s_common.h"
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #ifdef DEBUG
797c478bd9Sstevel@tonic-gate #define	SPC_DEBUG
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate #include "spppcomp.h"
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * This is used to tag official Solaris sources.  Please do not define
857c478bd9Sstevel@tonic-gate  * "INTERNAL_BUILD" when building this software outside of Sun
867c478bd9Sstevel@tonic-gate  * Microsystems.
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate #ifdef INTERNAL_BUILD
897c478bd9Sstevel@tonic-gate /* MODINFO is limited to 32 characters. */
90002c70ffScarlsonj const char spppcomp_module_description[] = "PPP 4.0 compression";
917c478bd9Sstevel@tonic-gate #else /* INTERNAL_BUILD */
927c478bd9Sstevel@tonic-gate const char spppcomp_module_description[] =
937c478bd9Sstevel@tonic-gate 	"ANU PPP compression $Revision: 1.16$ ";
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /* LINTED */
967c478bd9Sstevel@tonic-gate static const char buildtime[] = "Built " __DATE__ " at " __TIME__
977c478bd9Sstevel@tonic-gate #ifdef DEBUG
987c478bd9Sstevel@tonic-gate " DEBUG"
997c478bd9Sstevel@tonic-gate #endif
1007c478bd9Sstevel@tonic-gate "\n";
1017c478bd9Sstevel@tonic-gate #endif /* INTERNAL_BUILD */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static int	spppcomp_open(queue_t *, dev_t *, int, int, cred_t *);
1047c478bd9Sstevel@tonic-gate static int	spppcomp_close(queue_t *, int, cred_t *);
105*c4d76aa4SToomas Soome static int	spppcomp_rput(queue_t *, mblk_t *);
106*c4d76aa4SToomas Soome static int	spppcomp_rsrv(queue_t *);
107*c4d76aa4SToomas Soome static int	spppcomp_wput(queue_t *, mblk_t *);
108*c4d76aa4SToomas Soome static int	spppcomp_wsrv(queue_t *);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	PPPCOMP_MI_MINPSZ	(0)
1117c478bd9Sstevel@tonic-gate #define	PPPCOMP_MI_MAXPSZ	(INFPSZ)
1127c478bd9Sstevel@tonic-gate #define	PPPCOMP_MI_HIWAT	(PPP_MTU * 20)
1137c478bd9Sstevel@tonic-gate #define	PPPCOMP_MI_LOWAT	(PPP_MTU * 18)
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static struct module_info spppcomp_modinfo = {
1167c478bd9Sstevel@tonic-gate 	COMP_MOD_ID,		/* mi_idnum */
1177c478bd9Sstevel@tonic-gate 	COMP_MOD_NAME,		/* mi_idname */
1187c478bd9Sstevel@tonic-gate 	PPPCOMP_MI_MINPSZ,	/* mi_minpsz */
1197c478bd9Sstevel@tonic-gate 	PPPCOMP_MI_MAXPSZ,	/* mi_maxpsz */
1207c478bd9Sstevel@tonic-gate 	PPPCOMP_MI_HIWAT,	/* mi_hiwat */
1217c478bd9Sstevel@tonic-gate 	PPPCOMP_MI_LOWAT	/* mi_lowat */
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static struct qinit spppcomp_rinit = {
125*c4d76aa4SToomas Soome 	spppcomp_rput,		/* qi_putp */
126*c4d76aa4SToomas Soome 	spppcomp_rsrv,		/* qi_srvp */
1277c478bd9Sstevel@tonic-gate 	spppcomp_open,		/* qi_qopen */
1287c478bd9Sstevel@tonic-gate 	spppcomp_close,		/* qi_qclose */
1297c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
1307c478bd9Sstevel@tonic-gate 	&spppcomp_modinfo,	/* qi_minfo */
1317c478bd9Sstevel@tonic-gate 	NULL			/* qi_mstat */
1327c478bd9Sstevel@tonic-gate };
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static struct qinit spppcomp_winit = {
135*c4d76aa4SToomas Soome 	spppcomp_wput,		/* qi_putp */
136*c4d76aa4SToomas Soome 	spppcomp_wsrv,		/* qi_srvp */
1377c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qopen */
1387c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qclose */
1397c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
1407c478bd9Sstevel@tonic-gate 	&spppcomp_modinfo,	/* qi_minfo */
1417c478bd9Sstevel@tonic-gate 	NULL			/* qi_mstat */
1427c478bd9Sstevel@tonic-gate };
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate struct streamtab spppcomp_tab = {
1457c478bd9Sstevel@tonic-gate 	&spppcomp_rinit,	/* st_rdinit */
1467c478bd9Sstevel@tonic-gate 	&spppcomp_winit,	/* st_wrinit */
1477c478bd9Sstevel@tonic-gate 	NULL,			/* st_muxrinit */
1487c478bd9Sstevel@tonic-gate 	NULL			/* st_muxwinit */
1497c478bd9Sstevel@tonic-gate };
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /* Set non-zero to debug algorithm-specific problems alone. */
1527c478bd9Sstevel@tonic-gate #define	ALG_DEBUG	0
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #define	MAX_IPHLEN	(0x0f << 2)
1557c478bd9Sstevel@tonic-gate #define	MAX_TCPHLEN	(0x0f << 2)
1567c478bd9Sstevel@tonic-gate #define	MAX_TCPIPHLEN	(MAX_IPHLEN + MAX_TCPHLEN) /* max TCP/IP header size */
1577c478bd9Sstevel@tonic-gate #define	MAX_VJHDR	(20)		/* max VJ compressed header size (?) */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #if 0
1607c478bd9Sstevel@tonic-gate #define	DBGSTART	CE_CONT, COMP_MOD_NAME "%d: "
1617c478bd9Sstevel@tonic-gate #define	CKDEBUG(x)	cmn_err x
1627c478bd9Sstevel@tonic-gate #else
1637c478bd9Sstevel@tonic-gate #define	DBGSTART	COMP_MOD_NAME "%d: "
1647c478bd9Sstevel@tonic-gate #define	CKDEBUG(x)	printf x
1657c478bd9Sstevel@tonic-gate #endif
1667c478bd9Sstevel@tonic-gate #define	CPDEBUG(x)	(IS_CP_KDEBUG(cp) ? CKDEBUG(x) : (void)0)
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * List of compressors we know about.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate #if DO_BSD_COMPRESS
1727c478bd9Sstevel@tonic-gate extern struct compressor ppp_bsd_compress;
1737c478bd9Sstevel@tonic-gate #endif
1747c478bd9Sstevel@tonic-gate #if DO_DEFLATE
1757c478bd9Sstevel@tonic-gate extern struct compressor ppp_deflate;
1767c478bd9Sstevel@tonic-gate extern struct compressor ppp_deflate_draft;
1777c478bd9Sstevel@tonic-gate #endif
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate struct compressor *ppp_compressors[] = {
1807c478bd9Sstevel@tonic-gate #if DO_BSD_COMPRESS
1817c478bd9Sstevel@tonic-gate 	&ppp_bsd_compress,
1827c478bd9Sstevel@tonic-gate #endif
1837c478bd9Sstevel@tonic-gate #if DO_DEFLATE
1847c478bd9Sstevel@tonic-gate 	&ppp_deflate,
1857c478bd9Sstevel@tonic-gate 	&ppp_deflate_draft,
1867c478bd9Sstevel@tonic-gate #endif
1877c478bd9Sstevel@tonic-gate 	NULL
1887c478bd9Sstevel@tonic-gate };
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * LCP_USE_DFLT() removed by James Carlson.  RFC 1661 section 6.6 has
1927c478bd9Sstevel@tonic-gate  * this to say on the topic:
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  *    The Address and Control fields MUST NOT be compressed when sending
1957c478bd9Sstevel@tonic-gate  *    any LCP packet.  This rule guarantees unambiguous recognition of
1967c478bd9Sstevel@tonic-gate  *    LCP packets.
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static void	spppcomp_ioctl(queue_t *, mblk_t *, sppp_comp_t *);
2007c478bd9Sstevel@tonic-gate static int	spppcomp_mctl(queue_t *, mblk_t *);
2017c478bd9Sstevel@tonic-gate static mblk_t	*spppcomp_outpkt(queue_t *, mblk_t *);
2027c478bd9Sstevel@tonic-gate static mblk_t	*spppcomp_inpkt(queue_t *, mblk_t *);
2037c478bd9Sstevel@tonic-gate static int	spppcomp_kstat_update(kstat_t *, int);
2047c478bd9Sstevel@tonic-gate static void	comp_ccp(queue_t *, mblk_t *, sppp_comp_t *, boolean_t);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Values for checking inter-arrival times on interrupt stacks.  These
2087c478bd9Sstevel@tonic-gate  * are used to prevent CPU hogging in interrupt context.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate #define	MIN_ARRIVAL_TIME	5000000	/* interarrival time in nanoseconds */
2117c478bd9Sstevel@tonic-gate #define	MAX_FAST_ARRIVALS	10	/* maximum packet count */
2127c478bd9Sstevel@tonic-gate hrtime_t spppcomp_min_arrival = MIN_ARRIVAL_TIME;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static const char *kstats_names[] = {
2157c478bd9Sstevel@tonic-gate #ifdef SPCDEBUG_KSTATS_NAMES
2167c478bd9Sstevel@tonic-gate 	SPPPCOMP_KSTATS_NAMES,
2177c478bd9Sstevel@tonic-gate 	SPCDEBUG_KSTATS_NAMES
2187c478bd9Sstevel@tonic-gate #else
2197c478bd9Sstevel@tonic-gate 	SPPPCOMP_KSTATS_NAMES
2207c478bd9Sstevel@tonic-gate #endif
2217c478bd9Sstevel@tonic-gate };
2227c478bd9Sstevel@tonic-gate static const char *kstats64_names[] = { SPPPCOMP_KSTATS64_NAMES };
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * spppcomp_open()
2267c478bd9Sstevel@tonic-gate  *
2277c478bd9Sstevel@tonic-gate  * MT-Perimeters:
2287c478bd9Sstevel@tonic-gate  *    exclusive inner.
2297c478bd9Sstevel@tonic-gate  *
2307c478bd9Sstevel@tonic-gate  * Description:
2317c478bd9Sstevel@tonic-gate  *    Common open procedure for module.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate /* ARGSUSED */
2347c478bd9Sstevel@tonic-gate static int
spppcomp_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)2357c478bd9Sstevel@tonic-gate spppcomp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	sppp_comp_t	*cp;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (q->q_ptr != NULL) {
2407c478bd9Sstevel@tonic-gate 		return (0);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	if (sflag != MODOPEN) {
2437c478bd9Sstevel@tonic-gate 		return (EINVAL);
2447c478bd9Sstevel@tonic-gate 	}
245f53eecf5SJames Carlson 	cp = kmem_zalloc(sizeof (sppp_comp_t), KM_SLEEP);
2467c478bd9Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = (caddr_t)cp;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	cp->cp_mru = PPP_MRU;
2497c478bd9Sstevel@tonic-gate 	cp->cp_mtu = PPP_MTU;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	mutex_init(&cp->cp_pair_lock, NULL, MUTEX_DRIVER, NULL);
2527c478bd9Sstevel@tonic-gate 	vj_compress_init(&cp->cp_vj, -1);
2537c478bd9Sstevel@tonic-gate 	cp->cp_nxslots = -1;
2547c478bd9Sstevel@tonic-gate 	cp->cp_effort = -1;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	qprocson(q);
2577c478bd9Sstevel@tonic-gate 	return (0);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * spppcomp_close()
2627c478bd9Sstevel@tonic-gate  *
2637c478bd9Sstevel@tonic-gate  * MT-Perimeters:
2647c478bd9Sstevel@tonic-gate  *    exclusive inner.
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * Description:
2677c478bd9Sstevel@tonic-gate  *    Common close procedure for module.
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@tonic-gate /* ARGSUSED */
2707c478bd9Sstevel@tonic-gate static int
spppcomp_close(queue_t * q,int flag,cred_t * credp)2717c478bd9Sstevel@tonic-gate spppcomp_close(queue_t *q, int flag, cred_t *credp)
2727c478bd9Sstevel@tonic-gate {
273f53eecf5SJames Carlson 	sppp_comp_t	*cp = q->q_ptr;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	qprocsoff(q);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	CPDEBUG((DBGSTART "close flags=0x%b\n",
2787c478bd9Sstevel@tonic-gate 	    (IS_CP_HASUNIT(cp) ? cp->cp_unit : -1), cp->cp_flags,
2797c478bd9Sstevel@tonic-gate 	    CP_FLAGSSTR));
2807c478bd9Sstevel@tonic-gate 	mutex_destroy(&cp->cp_pair_lock);
2817c478bd9Sstevel@tonic-gate 	if (cp->cp_kstats) {
2827c478bd9Sstevel@tonic-gate 		ASSERT(IS_CP_HASUNIT(cp));
2837c478bd9Sstevel@tonic-gate 		kstat_delete(cp->cp_kstats);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	if (cp->cp_xstate != NULL) {
2867c478bd9Sstevel@tonic-gate 		(*cp->cp_xcomp->comp_free)(cp->cp_xstate);
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	if (cp->cp_rstate != NULL) {
2897c478bd9Sstevel@tonic-gate 		(*cp->cp_rcomp->decomp_free)(cp->cp_rstate);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	kmem_free(cp, sizeof (sppp_comp_t));
2927c478bd9Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	return (0);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate  * spppcomp_wput()
2997c478bd9Sstevel@tonic-gate  *
3007c478bd9Sstevel@tonic-gate  * MT-Perimeters:
3017c478bd9Sstevel@tonic-gate  *    exclusive inner.
3027c478bd9Sstevel@tonic-gate  *
3037c478bd9Sstevel@tonic-gate  * Description:
3047c478bd9Sstevel@tonic-gate  *    Write-side put procedure.  Packets from above us arrive here.
3057c478bd9Sstevel@tonic-gate  *
3067c478bd9Sstevel@tonic-gate  *	The data handling logic is a little tricky here.  We defer to
3077c478bd9Sstevel@tonic-gate  *	the service routine if q_first isn't NULL (to preserve message
3087c478bd9Sstevel@tonic-gate  *	ordering after deferring a previous message), bcanputnext() is
3097c478bd9Sstevel@tonic-gate  *	FALSE (to handle flow control), or we need a lot of processing
3107c478bd9Sstevel@tonic-gate  *	and we're in an interrupt context (on the theory that we're
3117c478bd9Sstevel@tonic-gate  *	already on a very long call stack at that point).  Since many
3127c478bd9Sstevel@tonic-gate  *	callers will be in a non-interrupt context, this means that
3137c478bd9Sstevel@tonic-gate  *	most processing will be performed here in-line, and deferral
3147c478bd9Sstevel@tonic-gate  *	occurs only when necessary.
3157c478bd9Sstevel@tonic-gate  */
316*c4d76aa4SToomas Soome static int
spppcomp_wput(queue_t * q,mblk_t * mp)3177c478bd9Sstevel@tonic-gate spppcomp_wput(queue_t *q, mblk_t *mp)
3187c478bd9Sstevel@tonic-gate {
319f53eecf5SJames Carlson 	sppp_comp_t *cp = q->q_ptr;
3207c478bd9Sstevel@tonic-gate 	int flag;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	switch (MTYPE(mp)) {
3237c478bd9Sstevel@tonic-gate 	case M_DATA:
3247c478bd9Sstevel@tonic-gate 		if (q->q_first != NULL || !bcanputnext(q, mp->b_band) ||
3257c478bd9Sstevel@tonic-gate 		    ((cp->cp_flags & (COMP_VJC|CCP_COMP_RUN)) &&
326002c70ffScarlsonj 		    servicing_interrupt())) {
3277c478bd9Sstevel@tonic-gate #ifdef SPC_DEBUG
3287c478bd9Sstevel@tonic-gate 			cp->cp_out_queued++;
3297c478bd9Sstevel@tonic-gate #endif
330f53eecf5SJames Carlson 			if (!putq(q, mp))
331f53eecf5SJames Carlson 				freemsg(mp);
3327c478bd9Sstevel@tonic-gate 		} else {
3337c478bd9Sstevel@tonic-gate #ifdef SPC_DEBUG
3347c478bd9Sstevel@tonic-gate 			cp->cp_out_handled++;
3357c478bd9Sstevel@tonic-gate #endif
336f53eecf5SJames Carlson 			if ((mp = spppcomp_outpkt(q, mp)) != NULL)
3377c478bd9Sstevel@tonic-gate 				putnext(q, mp);
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 		break;
3407c478bd9Sstevel@tonic-gate 	case M_IOCTL:
3417c478bd9Sstevel@tonic-gate 		spppcomp_ioctl(q, mp, cp);
3427c478bd9Sstevel@tonic-gate 		break;
3437c478bd9Sstevel@tonic-gate 	case M_CTL:
3447c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->cp_pair_lock);
3457c478bd9Sstevel@tonic-gate 		flag = spppcomp_mctl(q, mp);
3467c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->cp_pair_lock);
3477c478bd9Sstevel@tonic-gate 		if (flag != 0)
3487c478bd9Sstevel@tonic-gate 			putnext(q, mp);
3497c478bd9Sstevel@tonic-gate 		else
3507c478bd9Sstevel@tonic-gate 			freemsg(mp);
3517c478bd9Sstevel@tonic-gate 		break;
3527c478bd9Sstevel@tonic-gate 	case M_FLUSH:
3537c478bd9Sstevel@tonic-gate 		CPDEBUG((DBGSTART "wput M_FLUSH (0x%x) flags=0x%b\n",
3547c478bd9Sstevel@tonic-gate 		    (IS_CP_HASUNIT(cp) ? cp->cp_unit : -1),
3557c478bd9Sstevel@tonic-gate 		    *mp->b_rptr, cp->cp_flags,	CP_FLAGSSTR));
3567c478bd9Sstevel@tonic-gate 		/*
3577c478bd9Sstevel@tonic-gate 		 * Just discard pending data.  For CCP, any compressor
3587c478bd9Sstevel@tonic-gate 		 * dictionary sequencing problems caused by this will
3597c478bd9Sstevel@tonic-gate 		 * have to be handled by the compression protocol in
3607c478bd9Sstevel@tonic-gate 		 * use.  For VJ, we need to tell the compressor to
3617c478bd9Sstevel@tonic-gate 		 * start over.
3627c478bd9Sstevel@tonic-gate 		 */
3637c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
3647c478bd9Sstevel@tonic-gate 			mutex_enter(&cp->cp_pair_lock);
3657c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
3667c478bd9Sstevel@tonic-gate 			vj_compress_init(&cp->cp_vj, cp->cp_nxslots);
3677c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->cp_pair_lock);
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 		putnext(q, mp);
3707c478bd9Sstevel@tonic-gate 		break;
3717c478bd9Sstevel@tonic-gate 	default:
372f53eecf5SJames Carlson 		if (bcanputnext(q, mp->b_band))
373f53eecf5SJames Carlson 			putnext(q, mp);
374f53eecf5SJames Carlson 		else if (!putq(q, mp))
375f53eecf5SJames Carlson 			freemsg(mp);
3767c478bd9Sstevel@tonic-gate 		break;
3777c478bd9Sstevel@tonic-gate 	}
378*c4d76aa4SToomas Soome 	return (0);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * spppcomp_wsrv()
3837c478bd9Sstevel@tonic-gate  *
3847c478bd9Sstevel@tonic-gate  * MT-Perimeters:
3857c478bd9Sstevel@tonic-gate  *    exclusive inner
3867c478bd9Sstevel@tonic-gate  *
3877c478bd9Sstevel@tonic-gate  * Description:
3887c478bd9Sstevel@tonic-gate  *    Write-side service procedure.
3897c478bd9Sstevel@tonic-gate  */
390*c4d76aa4SToomas Soome static int
spppcomp_wsrv(queue_t * q)3917c478bd9Sstevel@tonic-gate spppcomp_wsrv(queue_t *q)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
3967c478bd9Sstevel@tonic-gate 		/*
3977c478bd9Sstevel@tonic-gate 		 * If the module below us is flow-controlled, then put
3987c478bd9Sstevel@tonic-gate 		 * this message back on the queue again.
3997c478bd9Sstevel@tonic-gate 		 */
4007c478bd9Sstevel@tonic-gate 		if (!bcanputnext(q, mp->b_band)) {
4017c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
4027c478bd9Sstevel@tonic-gate 			break;
4037c478bd9Sstevel@tonic-gate 		}
404f53eecf5SJames Carlson 		if (MTYPE(mp) != M_DATA ||
405f53eecf5SJames Carlson 		    (mp = spppcomp_outpkt(q, mp)) != NULL)
4067c478bd9Sstevel@tonic-gate 			putnext(q, mp);
4077c478bd9Sstevel@tonic-gate 	}
408*c4d76aa4SToomas Soome 	return (0);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate  * spppcomp_outpkt()
4137c478bd9Sstevel@tonic-gate  *
4147c478bd9Sstevel@tonic-gate  * MT-Perimeters:
4157c478bd9Sstevel@tonic-gate  *    exclusive inner
4167c478bd9Sstevel@tonic-gate  *
4177c478bd9Sstevel@tonic-gate  * Description:
4187c478bd9Sstevel@tonic-gate  *    Process outgoing packet.  Returns new mblk_t pointer on success
4197c478bd9Sstevel@tonic-gate  *    (caller should do putnext through q), NULL on failure (packet has
4207c478bd9Sstevel@tonic-gate  *    been discarded).
4217c478bd9Sstevel@tonic-gate  */
4227c478bd9Sstevel@tonic-gate static mblk_t *
spppcomp_outpkt(queue_t * q,mblk_t * mp)4237c478bd9Sstevel@tonic-gate spppcomp_outpkt(queue_t *q, mblk_t *mp)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	mblk_t		*zmp;
4267c478bd9Sstevel@tonic-gate 	int		len;
4277c478bd9Sstevel@tonic-gate 	ushort_t	proto;
428f53eecf5SJames Carlson 	sppp_comp_t	*cp = q->q_ptr;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	/*
4317c478bd9Sstevel@tonic-gate 	 * If the entire data size of the mblk is less than the length of the
4327c478bd9Sstevel@tonic-gate 	 * PPP header, then free it. We can't do much with such message anyway,
4337c478bd9Sstevel@tonic-gate 	 * since we can't determine what the PPP protocol is.
4347c478bd9Sstevel@tonic-gate 	 */
4357c478bd9Sstevel@tonic-gate 	len = msgsize(mp);
4367c478bd9Sstevel@tonic-gate 	if (MBLKL(mp) < PPP_HDRLEN) {
4377c478bd9Sstevel@tonic-gate #ifdef SPC_DEBUG
4387c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->cp_pair_lock);
4397c478bd9Sstevel@tonic-gate 		cp->cp_omsg_pull++;
4407c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->cp_pair_lock);
4417c478bd9Sstevel@tonic-gate #endif
4427c478bd9Sstevel@tonic-gate 		zmp = msgpullup(mp, PPP_HDRLEN);
4437c478bd9Sstevel@tonic-gate 		freemsg(mp);
4447c478bd9Sstevel@tonic-gate 		if ((mp = zmp) == NULL)
4457c478bd9Sstevel@tonic-gate 			goto msg_oerror;
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	proto = PPP_PROTOCOL(mp->b_rptr);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/*
4517c478bd9Sstevel@tonic-gate 	 * Do VJ compression if requested.
4527c478bd9Sstevel@tonic-gate 	 */
4537c478bd9Sstevel@tonic-gate 	if (proto == PPP_IP && IS_COMP_VJC(cp) &&
4547c478bd9Sstevel@tonic-gate 	    MSG_BYTE(mp, PPP_HDRLEN+offsetof(struct ip, ip_p)) ==
4557c478bd9Sstevel@tonic-gate 	    IPPROTO_TCP) {
4567c478bd9Sstevel@tonic-gate 		uchar_t		*vjhdr;
4577c478bd9Sstevel@tonic-gate 		int		type;
4587c478bd9Sstevel@tonic-gate 		uint32_t	indata[(PPP_HDRLEN+MAX_TCPIPHLEN) /
4597c478bd9Sstevel@tonic-gate 		    sizeof (uint32_t)];
4607c478bd9Sstevel@tonic-gate 		uchar_t		*dp;
4617c478bd9Sstevel@tonic-gate 		int		tocopy, copied;
4627c478bd9Sstevel@tonic-gate 		mblk_t		*fmb;
4637c478bd9Sstevel@tonic-gate 		void		*srcp;
4647c478bd9Sstevel@tonic-gate 		int		thislen;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		tocopy = copied = MIN(len, sizeof (indata));
4687c478bd9Sstevel@tonic-gate 		/*
4697c478bd9Sstevel@tonic-gate 		 * If we can alter this dblk, and there's enough data
4707c478bd9Sstevel@tonic-gate 		 * here to work with, and it's nicely aligned, then
4717c478bd9Sstevel@tonic-gate 		 * avoid the data copy.
4727c478bd9Sstevel@tonic-gate 		 */
4737c478bd9Sstevel@tonic-gate 		if (DB_REF(mp) == 1 && MBLKL(mp) >= tocopy &&
4747c478bd9Sstevel@tonic-gate 		    ((uintptr_t)mp->b_rptr & 3) == 0) {
4757c478bd9Sstevel@tonic-gate 			/* Save off the address/control */
4767c478bd9Sstevel@tonic-gate 			indata[0] = *(uint32_t *)mp->b_rptr;
4777c478bd9Sstevel@tonic-gate 			srcp = (void *)(mp->b_rptr + PPP_HDRLEN);
4787c478bd9Sstevel@tonic-gate 		} else {
4797c478bd9Sstevel@tonic-gate 			fmb = mp;
4807c478bd9Sstevel@tonic-gate 			dp = (uchar_t *)indata;
4817c478bd9Sstevel@tonic-gate 			while (tocopy > 0) {
4827c478bd9Sstevel@tonic-gate 				thislen = MBLKL(fmb);
4837c478bd9Sstevel@tonic-gate 				if (tocopy > thislen) {
4847c478bd9Sstevel@tonic-gate 					bcopy(fmb->b_rptr, dp, thislen);
4857c478bd9Sstevel@tonic-gate 					dp += thislen;
4867c478bd9Sstevel@tonic-gate 					tocopy -= thislen;
4877c478bd9Sstevel@tonic-gate 					fmb = fmb->b_cont;
4887c478bd9Sstevel@tonic-gate 				} else {
4897c478bd9Sstevel@tonic-gate 					bcopy(fmb->b_rptr, dp, tocopy);
4907c478bd9Sstevel@tonic-gate 					break;
4917c478bd9Sstevel@tonic-gate 				}
4927c478bd9Sstevel@tonic-gate 			}
4937c478bd9Sstevel@tonic-gate 			srcp = (void *)(indata + PPP_HDRLEN/sizeof (*indata));
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 		type = vj_compress_tcp((struct ip *)srcp, len - PPP_HDRLEN,
4977c478bd9Sstevel@tonic-gate 		    &cp->cp_vj, IS_COMP_VJCCID(cp), &vjhdr);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		/*
5007c478bd9Sstevel@tonic-gate 		 * If we're going to modify this packet, then we can't modify
5017c478bd9Sstevel@tonic-gate 		 * someone else's data.  Copy instead.
5027c478bd9Sstevel@tonic-gate 		 *
5037c478bd9Sstevel@tonic-gate 		 * (It would be nice to be able to avoid this data copy if CCP
5047c478bd9Sstevel@tonic-gate 		 * is also enabled.  That would require extensive
5057c478bd9Sstevel@tonic-gate 		 * modifications to the compression code.  Users should be
5067c478bd9Sstevel@tonic-gate 		 * told to disable VJ compression when using CCP.)
5077c478bd9Sstevel@tonic-gate 		 */
5087c478bd9Sstevel@tonic-gate 		if (type != TYPE_IP && DB_REF(mp) > 1) {
5097c478bd9Sstevel@tonic-gate #ifdef SPC_DEBUG
5107c478bd9Sstevel@tonic-gate 			mutex_enter(&cp->cp_pair_lock);
5117c478bd9Sstevel@tonic-gate 			cp->cp_omsg_dcopy++;
5127c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->cp_pair_lock);
5137c478bd9Sstevel@tonic-gate #endif
5147c478bd9Sstevel@tonic-gate 			/* Copy just altered portion. */
5157c478bd9Sstevel@tonic-gate 			zmp = msgpullup(mp, copied);
5167c478bd9Sstevel@tonic-gate 			freemsg(mp);
5177c478bd9Sstevel@tonic-gate 			if ((mp = zmp) == NULL)
5187c478bd9Sstevel@tonic-gate 				goto msg_oerror;
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		switch (type) {
5227c478bd9Sstevel@tonic-gate 		case TYPE_UNCOMPRESSED_TCP:
5237c478bd9Sstevel@tonic-gate 			mp->b_rptr[3] = proto = PPP_VJC_UNCOMP;
5247c478bd9Sstevel@tonic-gate 			/* No need to update if it was done in place. */
5257c478bd9Sstevel@tonic-gate 			if (srcp ==
5267c478bd9Sstevel@tonic-gate 			    (void *)(indata + PPP_HDRLEN / sizeof (*indata))) {
5277c478bd9Sstevel@tonic-gate 				thislen = PPP_HDRLEN +
5287c478bd9Sstevel@tonic-gate 				    offsetof(struct ip, ip_p);
5297c478bd9Sstevel@tonic-gate 				zmp = mp;
5307c478bd9Sstevel@tonic-gate 				while (zmp != NULL) {
5317c478bd9Sstevel@tonic-gate 					if (MBLKL(zmp) > thislen) {
5327c478bd9Sstevel@tonic-gate 						zmp->b_rptr[thislen] =
5337c478bd9Sstevel@tonic-gate 						    ((struct ip *)srcp)->ip_p;
5347c478bd9Sstevel@tonic-gate 						break;
5357c478bd9Sstevel@tonic-gate 					}
5367c478bd9Sstevel@tonic-gate 					thislen -= MBLKL(zmp);
5377c478bd9Sstevel@tonic-gate 					zmp = zmp->b_cont;
5387c478bd9Sstevel@tonic-gate 				}
5397c478bd9Sstevel@tonic-gate 			}
5407c478bd9Sstevel@tonic-gate 			break;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		case TYPE_COMPRESSED_TCP:
5437c478bd9Sstevel@tonic-gate 			/* Calculate amount to remove from front */
5447c478bd9Sstevel@tonic-gate 			thislen = vjhdr - (uchar_t *)srcp;
5457c478bd9Sstevel@tonic-gate 			ASSERT(thislen >= 0);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 			/* Try to do a cheap adjmsg by arithmetic first. */
5487c478bd9Sstevel@tonic-gate 			dp = mp->b_rptr + thislen;
5497c478bd9Sstevel@tonic-gate 			if (dp > mp->b_wptr) {
5507c478bd9Sstevel@tonic-gate 				if (!adjmsg(mp, thislen)) {
5517c478bd9Sstevel@tonic-gate 					freemsg(mp);
5527c478bd9Sstevel@tonic-gate 					goto msg_oerror;
5537c478bd9Sstevel@tonic-gate 				}
5547c478bd9Sstevel@tonic-gate 				dp = mp->b_rptr;
5557c478bd9Sstevel@tonic-gate 			}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 			/*
5587c478bd9Sstevel@tonic-gate 			 * Now make sure first block is big enough to
5597c478bd9Sstevel@tonic-gate 			 * receive modified data.  If we modified in
5607c478bd9Sstevel@tonic-gate 			 * place, then no need to check or copy.
5617c478bd9Sstevel@tonic-gate 			 */
5627c478bd9Sstevel@tonic-gate 			copied -= thislen;
5637c478bd9Sstevel@tonic-gate 			ASSERT(copied >= PPP_HDRLEN);
5647c478bd9Sstevel@tonic-gate 			if (srcp !=
5657c478bd9Sstevel@tonic-gate 			    (void *)(indata + PPP_HDRLEN / sizeof (*indata)))
5667c478bd9Sstevel@tonic-gate 				copied = 0;
5677c478bd9Sstevel@tonic-gate 			mp->b_rptr = dp;
5687c478bd9Sstevel@tonic-gate 			if (MBLKL(mp) < copied) {
5697c478bd9Sstevel@tonic-gate 				zmp = msgpullup(mp, copied);
5707c478bd9Sstevel@tonic-gate 				freemsg(mp);
5717c478bd9Sstevel@tonic-gate 				if ((mp = zmp) == NULL)
5727c478bd9Sstevel@tonic-gate 					goto msg_oerror;
5737c478bd9Sstevel@tonic-gate 				dp = mp->b_rptr;
5747c478bd9Sstevel@tonic-gate 			}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 			*dp++ = ((uchar_t *)indata)[0];	/* address */
5777c478bd9Sstevel@tonic-gate 			*dp++ = ((uchar_t *)indata)[1];	/* control  */
5787c478bd9Sstevel@tonic-gate 			*dp++ = 0;			/* protocol */
5797c478bd9Sstevel@tonic-gate 			*dp++ = proto = PPP_VJC_COMP;	/* protocol */
5807c478bd9Sstevel@tonic-gate 			copied -= PPP_HDRLEN;
5817c478bd9Sstevel@tonic-gate 			if (copied > 0) {
5827c478bd9Sstevel@tonic-gate 				bcopy(vjhdr, dp, copied);
5837c478bd9Sstevel@tonic-gate 			}
5847c478bd9Sstevel@tonic-gate 			break;
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	/*
5897c478bd9Sstevel@tonic-gate 	 * Do packet compression if enabled.
5907c478bd9Sstevel@tonic-gate 	 */
5917c478bd9Sstevel@tonic-gate 	if (proto == PPP_CCP) {
5927c478bd9Sstevel@tonic-gate 		/*
5937c478bd9Sstevel@tonic-gate 		 * Handle any negotiation packets by changing compressor
5947c478bd9Sstevel@tonic-gate 		 * state.  Doing this here rather than with an ioctl keeps
5957c478bd9Sstevel@tonic-gate 		 * the negotiation and the data flow in sync.
5967c478bd9Sstevel@tonic-gate 		 */
5977c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->cp_pair_lock);
5987c478bd9Sstevel@tonic-gate 		comp_ccp(q, mp, cp, B_FALSE);
5997c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->cp_pair_lock);
6007c478bd9Sstevel@tonic-gate 	} else if (proto != PPP_LCP && IS_CCP_COMP_RUN(cp) &&
601002c70ffScarlsonj 	    IS_CCP_ISUP(cp) && cp->cp_xstate != NULL) {
6027c478bd9Sstevel@tonic-gate 		mblk_t	*cmp = NULL;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		len = msgsize(mp);
6057c478bd9Sstevel@tonic-gate 		len = (*cp->cp_xcomp->compress)(cp->cp_xstate, &cmp, mp, len,
606002c70ffScarlsonj 		    cp->cp_mtu + PPP_HDRLEN);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		if (cmp != NULL) {
6097c478bd9Sstevel@tonic-gate 			/* Success!  Discard uncompressed version */
6107c478bd9Sstevel@tonic-gate 			cmp->b_band = mp->b_band;
6117c478bd9Sstevel@tonic-gate 			freemsg(mp);
6127c478bd9Sstevel@tonic-gate 			mp = cmp;
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 		if (len < 0) {
6157c478bd9Sstevel@tonic-gate 			/*
6167c478bd9Sstevel@tonic-gate 			 * Compressor failure; must discard this
6177c478bd9Sstevel@tonic-gate 			 * packet because the compressor dictionary is
6187c478bd9Sstevel@tonic-gate 			 * now corrupt.
6197c478bd9Sstevel@tonic-gate 			 */
6207c478bd9Sstevel@tonic-gate 			freemsg(mp);
6217c478bd9Sstevel@tonic-gate 			mutex_enter(&cp->cp_pair_lock);
6227c478bd9Sstevel@tonic-gate 			cp->cp_stats.ppp_oerrors++;
6237c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->cp_pair_lock);
6247c478bd9Sstevel@tonic-gate 			(void) putnextctl1(RD(q), M_CTL, PPPCTL_OERROR);
6257c478bd9Sstevel@tonic-gate 			return (NULL);
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * If either address and control field compression or protocol field
6317c478bd9Sstevel@tonic-gate 	 * compression is enabled, then we'll need a writable packet.  Copy if
6327c478bd9Sstevel@tonic-gate 	 * necessary.
6337c478bd9Sstevel@tonic-gate 	 */
6347c478bd9Sstevel@tonic-gate 	if ((cp->cp_flags & (COMP_AC|COMP_PROT)) && DB_REF(mp) > 1) {
6357c478bd9Sstevel@tonic-gate #ifdef SPC_DEBUG
6367c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->cp_pair_lock);
6377c478bd9Sstevel@tonic-gate 		cp->cp_omsg_dcopy++;
6387c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->cp_pair_lock);
6397c478bd9Sstevel@tonic-gate #endif
6407c478bd9Sstevel@tonic-gate 		zmp = copymsg(mp);
6417c478bd9Sstevel@tonic-gate 		freemsg(mp);
6427c478bd9Sstevel@tonic-gate 		if ((mp = zmp) == NULL)
6437c478bd9Sstevel@tonic-gate 			goto msg_oerror;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	/*
6477c478bd9Sstevel@tonic-gate 	 * Do address/control and protocol compression if enabled.
6487c478bd9Sstevel@tonic-gate 	 */
6497c478bd9Sstevel@tonic-gate 	if (IS_COMP_AC(cp) && (proto != PPP_LCP)) {
6507c478bd9Sstevel@tonic-gate 		mp->b_rptr += 2;	/* drop address & ctrl fields */
6517c478bd9Sstevel@tonic-gate 		/*
6527c478bd9Sstevel@tonic-gate 		 * Protocol field compression omits the first byte if
6537c478bd9Sstevel@tonic-gate 		 * it would be 0x00, thus the check for < 0x100.
6547c478bd9Sstevel@tonic-gate 		 */
6557c478bd9Sstevel@tonic-gate 		if (proto < 0x100 && IS_COMP_PROT(cp)) {
6567c478bd9Sstevel@tonic-gate 			++mp->b_rptr;	/* drop high protocol byte */
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 	} else if ((proto < 0x100) && IS_COMP_PROT(cp)) {
6597c478bd9Sstevel@tonic-gate 		/*
6607c478bd9Sstevel@tonic-gate 		 * shuffle up the address & ctrl fields
6617c478bd9Sstevel@tonic-gate 		 */
6627c478bd9Sstevel@tonic-gate 		mp->b_rptr[2] = mp->b_rptr[1];
6637c478bd9Sstevel@tonic-gate 		mp->b_rptr[1] = mp->b_rptr[0];
6647c478bd9Sstevel@tonic-gate 		++mp->b_rptr;
6657c478bd9Sstevel@tonic-gate 	}
6667c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cp_pair_lock);
6677c478bd9Sstevel@tonic-gate 	cp->cp_stats.ppp_opackets++;
6687c478bd9Sstevel@tonic-gate 	cp->cp_stats.ppp_obytes += msgsize(mp);
6697c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cp_pair_lock);
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	CPDEBUG((DBGSTART "send (%ld bytes) flags=0x%b\n",
6727c478bd9Sstevel@tonic-gate 	    (IS_CP_HASUNIT(cp) ? cp->cp_unit : -1), msgsize(mp),
6737c478bd9Sstevel@tonic-gate 	    cp->cp_flags, CP_FLAGSSTR));
6747c478bd9Sstevel@tonic-gate 	return (mp);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate msg_oerror:
6777c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cp_pair_lock);
6787c478bd9Sstevel@tonic-gate 	cp->cp_stats.ppp_oerrors++;
6797c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cp_pair_lock);
6807c478bd9Sstevel@tonic-gate 	(void) putnextctl1(RD(q), M_CTL, PPPCTL_OERROR);
6817c478bd9Sstevel@tonic-gate 	return (NULL);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate /*
6857c478bd9Sstevel@tonic-gate  * spppcomp_inner_ioctl()
6867c478bd9Sstevel@tonic-gate  *
6877c478bd9Sstevel@tonic-gate  * MT-Perimeters:
6887c478bd9Sstevel@tonic-gate  *    exclusive inner; queue pair lock held.
6897c478bd9Sstevel@tonic-gate  *
6907c478bd9Sstevel@tonic-gate  * Description:
6917c478bd9Sstevel@tonic-gate  *	Called by spppcomp_ioctl to handle state-affecting ioctls.
6927c478bd9Sstevel@tonic-gate  *	Returns -1 if caller should do putnext, 0 for miocack, or >0
6937c478bd9Sstevel@tonic-gate  *	for miocnak.  Must *NOT* do putnext in this routine, since
6947c478bd9Sstevel@tonic-gate  *	lock is held here.
6957c478bd9Sstevel@tonic-gate  */
6967c478bd9Sstevel@tonic-gate static int
spppcomp_inner_ioctl(queue_t * q,mblk_t * mp)6977c478bd9Sstevel@tonic-gate spppcomp_inner_ioctl(queue_t *q, mblk_t *mp)
6987c478bd9Sstevel@tonic-gate {
699f53eecf5SJames Carlson 	sppp_comp_t	*cp = q->q_ptr;
7007c478bd9Sstevel@tonic-gate 	int		flags;
7017c478bd9Sstevel@tonic-gate 	int		mask;
7027c478bd9Sstevel@tonic-gate 	int		rc;
7037c478bd9Sstevel@tonic-gate 	int		len;
7047c478bd9Sstevel@tonic-gate 	int		cmd;
7057c478bd9Sstevel@tonic-gate 	int		nxslots;
7067c478bd9Sstevel@tonic-gate 	int		nrslots;
7077c478bd9Sstevel@tonic-gate 	int		val;
7087c478bd9Sstevel@tonic-gate 	uchar_t		*opt_data;
7097c478bd9Sstevel@tonic-gate 	uint32_t	opt_len;
7107c478bd9Sstevel@tonic-gate 	struct compressor **comp;
7117c478bd9Sstevel@tonic-gate 	struct compressor *ccomp;
7127c478bd9Sstevel@tonic-gate 	struct iocblk	*iop;
7137c478bd9Sstevel@tonic-gate 	void		*xtemp;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	iop = (struct iocblk *)mp->b_rptr;
7167c478bd9Sstevel@tonic-gate 	rc = EINVAL;
7177c478bd9Sstevel@tonic-gate 	len = 0;
7187c478bd9Sstevel@tonic-gate 	switch (iop->ioc_cmd) {
7197c478bd9Sstevel@tonic-gate 	case PPPIO_CFLAGS:
7207c478bd9Sstevel@tonic-gate 		if (iop->ioc_count != 2 * sizeof (uint32_t) ||
7217c478bd9Sstevel@tonic-gate 		    mp->b_cont == NULL)
7227c478bd9Sstevel@tonic-gate 			break;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		flags = ((uint32_t *)mp->b_cont->b_rptr)[0];
7257c478bd9Sstevel@tonic-gate 		mask = ((uint32_t *)mp->b_cont->b_rptr)[1];
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 		cp->cp_flags = (cp->cp_flags & ~mask) | (flags & mask);
7287c478bd9Sstevel@tonic-gate 
729002c70ffScarlsonj 		if ((mask & CCP_ISOPEN) && !(flags & CCP_ISOPEN)) {
730002c70ffScarlsonj 			cp->cp_flags &= ~CCP_ISUP & ~CCP_COMP_RUN &
731002c70ffScarlsonj 			    ~CCP_DECOMP_RUN;
7327c478bd9Sstevel@tonic-gate 			if (cp->cp_xstate != NULL) {
7337c478bd9Sstevel@tonic-gate 				(*cp->cp_xcomp->comp_free)(cp->cp_xstate);
7347c478bd9Sstevel@tonic-gate 				cp->cp_xstate = NULL;
7357c478bd9Sstevel@tonic-gate 			}
7367c478bd9Sstevel@tonic-gate 			if (cp->cp_rstate != NULL) {
7377c478bd9Sstevel@tonic-gate 				(*cp->cp_rcomp->decomp_free)(cp->cp_rstate);
7387c478bd9Sstevel@tonic-gate 				cp->cp_rstate = NULL;
7397c478bd9Sstevel@tonic-gate 			}
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		CPDEBUG((DBGSTART
7437c478bd9Sstevel@tonic-gate 		    "PPPIO_CFLAGS xflags=0x%b xmask=0x%b flags=0x%b\n",
7447c478bd9Sstevel@tonic-gate 		    (IS_CP_HASUNIT(cp) ? cp->cp_unit : -1),
7457c478bd9Sstevel@tonic-gate 		    flags, CP_FLAGSSTR, mask,
7467c478bd9Sstevel@tonic-gate 		    CP_FLAGSSTR, cp->cp_flags, CP_FLAGSSTR));
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		/* If we're not the last PPP-speaker, then pass along. */
7497c478bd9Sstevel@tonic-gate 		if (!IS_CP_LASTMOD(cp)) {
7507c478bd9Sstevel@tonic-gate 			return (-1);	/* putnext */
7517c478bd9Sstevel@tonic-gate 		}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 		*(uint32_t *)mp->b_cont->b_rptr = cp->cp_flags;
7547c478bd9Sstevel@tonic-gate 		len = sizeof (uint32_t);
7557c478bd9Sstevel@tonic-gate 		rc = 0;
7567c478bd9Sstevel@tonic-gate 		break;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	case PPPIO_VJINIT:
7597c478bd9Sstevel@tonic-gate 		if (iop->ioc_count != 2 || mp->b_cont == NULL)
7607c478bd9Sstevel@tonic-gate 			break;
7617c478bd9Sstevel@tonic-gate 		/*
7627c478bd9Sstevel@tonic-gate 		 * Even though it's not passed along, we have to
7637c478bd9Sstevel@tonic-gate 		 * validate nrslots so that we don't agree to
7647c478bd9Sstevel@tonic-gate 		 * decompress anything we cannot.
7657c478bd9Sstevel@tonic-gate 		 */
7667c478bd9Sstevel@tonic-gate 		nxslots = mp->b_cont->b_rptr[0] + 1;
7677c478bd9Sstevel@tonic-gate 		nrslots = mp->b_cont->b_rptr[1] + 1;
7687c478bd9Sstevel@tonic-gate 		if (nxslots > MAX_STATES || nrslots > MAX_STATES)
7697c478bd9Sstevel@tonic-gate 			break;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 		/* No need to lock here; just reading a word is atomic */
7727c478bd9Sstevel@tonic-gate 		/* mutex_enter(&cp->cp_pair_lock); */
7737c478bd9Sstevel@tonic-gate 		cp->cp_vj_last_ierrors = cp->cp_stats.ppp_ierrors;
7747c478bd9Sstevel@tonic-gate 		/* mutex_exit(&cp->cp_pair_lock); */
7757c478bd9Sstevel@tonic-gate 		vj_compress_init(&cp->cp_vj, nxslots);
7767c478bd9Sstevel@tonic-gate 		cp->cp_nxslots = nxslots;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		CPDEBUG((DBGSTART
7797c478bd9Sstevel@tonic-gate 		    "PPPIO_VJINIT txslots=%d rxslots=%d flags=0x%b\n",
7807c478bd9Sstevel@tonic-gate 		    (IS_CP_HASUNIT(cp) ? cp->cp_unit : -1), nxslots,
7817c478bd9Sstevel@tonic-gate 		    nrslots, cp->cp_flags, CP_FLAGSSTR));
7827c478bd9Sstevel@tonic-gate 		rc = 0;
7837c478bd9Sstevel@tonic-gate 		break;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	case PPPIO_XCOMP:
7867c478bd9Sstevel@tonic-gate 	case PPPIO_RCOMP:
7877c478bd9Sstevel@tonic-gate 		if (iop->ioc_count < 2 || mp->b_cont == NULL)
7887c478bd9Sstevel@tonic-gate 			break;
7897c478bd9Sstevel@tonic-gate 		/*
7907c478bd9Sstevel@tonic-gate 		 * The input data here is the raw CCP algorithm option
7917c478bd9Sstevel@tonic-gate 		 * from negotiation.  The format is always one byte of
7927c478bd9Sstevel@tonic-gate 		 * algorithm number, one byte of length, and
7937c478bd9Sstevel@tonic-gate 		 * (length-2) bytes of algorithm-dependent data.  The
7947c478bd9Sstevel@tonic-gate 		 * alloc routine is expected to parse and validate
7957c478bd9Sstevel@tonic-gate 		 * this.
7967c478bd9Sstevel@tonic-gate 		 */
7977c478bd9Sstevel@tonic-gate 		opt_data = mp->b_cont->b_rptr;
7987c478bd9Sstevel@tonic-gate 		opt_len = mp->b_cont->b_wptr - opt_data;
7997c478bd9Sstevel@tonic-gate 		if (opt_len > iop->ioc_count) {
8007c478bd9Sstevel@tonic-gate 			opt_len = iop->ioc_count;
8017c478bd9Sstevel@tonic-gate 		}
8027c478bd9Sstevel@tonic-gate 		len = mp->b_cont->b_rptr[1];
8037c478bd9Sstevel@tonic-gate 		if (len < 2 || len > opt_len)
8047c478bd9Sstevel@tonic-gate 			break;
8057c478bd9Sstevel@tonic-gate 		len = 0;
8067c478bd9Sstevel@tonic-gate 		for (comp = ppp_compressors; *comp != NULL; ++comp) {
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 			if ((*comp)->compress_proto != opt_data[0]) {
8097c478bd9Sstevel@tonic-gate 				continue;
8107c478bd9Sstevel@tonic-gate 			}
8117c478bd9Sstevel@tonic-gate 			rc = 0;
8127c478bd9Sstevel@tonic-gate 			if (iop->ioc_cmd == PPPIO_XCOMP) {
8137c478bd9Sstevel@tonic-gate 				/*
8147c478bd9Sstevel@tonic-gate 				 * A previous call may have fetched
8157c478bd9Sstevel@tonic-gate 				 * memory for a compressor that's now
8167c478bd9Sstevel@tonic-gate 				 * being retired or reset.  Free it
8177c478bd9Sstevel@tonic-gate 				 * using its mechanism for freeing
8187c478bd9Sstevel@tonic-gate 				 * stuff.
8197c478bd9Sstevel@tonic-gate 				 */
8207c478bd9Sstevel@tonic-gate 				if ((xtemp = cp->cp_xstate) != NULL) {
821002c70ffScarlsonj 					cp->cp_flags &= ~CCP_ISUP &
822002c70ffScarlsonj 					    ~CCP_COMP_RUN;
8237c478bd9Sstevel@tonic-gate 					cp->cp_xstate = NULL;
8247c478bd9Sstevel@tonic-gate 					(*cp->cp_xcomp->comp_free)(xtemp);
8257c478bd9Sstevel@tonic-gate 				}
8267c478bd9Sstevel@tonic-gate 				cp->cp_xcomp = *comp;
8277c478bd9Sstevel@tonic-gate 				cp->cp_xstate = (*comp)->comp_alloc(opt_data,
8287c478bd9Sstevel@tonic-gate 				    opt_len);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 				if (cp->cp_xstate == NULL) {
8317c478bd9Sstevel@tonic-gate 					rc = ENOSR;
8327c478bd9Sstevel@tonic-gate 				}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 				CPDEBUG((DBGSTART "PPPIO_XCOMP opt_proto=0x%x "
8357c478bd9Sstevel@tonic-gate 				    "opt_len=0x%d flags=0x%b\n",
8367c478bd9Sstevel@tonic-gate 				    (IS_CP_HASUNIT(cp) ? cp->cp_unit : -1),
8377c478bd9Sstevel@tonic-gate 				    (uchar_t)opt_data[0], opt_len,
8387c478bd9Sstevel@tonic-gate 				    cp->cp_flags,
8397c478bd9Sstevel@tonic-gate 				    CP_FLAGSSTR));
8407c478bd9Sstevel@tonic-gate 			} else {
8417c478bd9Sstevel@tonic-gate 				if ((xtemp = cp->cp_rstate) != NULL) {
842002c70ffScarlsonj 					cp->cp_flags &= ~CCP_ISUP &
843002c70ffScarlsonj 					    ~CCP_DECOMP_RUN;
8447c478bd9Sstevel@tonic-gate 					cp->cp_rstate = NULL;
8457c478bd9Sstevel@tonic-gate 					(*cp->cp_rcomp->decomp_free)(xtemp);
8467c478bd9Sstevel@tonic-gate 				}
8477c478bd9Sstevel@tonic-gate 				cp->cp_rcomp = *comp;
8487c478bd9Sstevel@tonic-gate 				cp->cp_rstate =
8497c478bd9Sstevel@tonic-gate 				    (*comp)->decomp_alloc(opt_data, opt_len);
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 				if (cp->cp_rstate == NULL) {
8527c478bd9Sstevel@tonic-gate 					rc = ENOSR;
8537c478bd9Sstevel@tonic-gate 				}
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 				CPDEBUG((DBGSTART "PPPIO_RCOMP opt_proto=0x%x "
8567c478bd9Sstevel@tonic-gate 				    "opt_len=0x%d flags=0x%b\n",
8577c478bd9Sstevel@tonic-gate 				    (IS_CP_HASUNIT(cp) ? cp->cp_unit : -1),
8587c478bd9Sstevel@tonic-gate 				    (uchar_t)opt_data[0], opt_len,
8597c478bd9Sstevel@tonic-gate 				    cp->cp_flags,
8607c478bd9Sstevel@tonic-gate 				    CP_FLAGSSTR));
8617c478bd9Sstevel@tonic-gate 			}
8627c478bd9Sstevel@tonic-gate 			if (rc == 0 && (*comp)->set_effort != NULL) {
8637c478bd9Sstevel@tonic-gate 				rc = (*(*comp)->set_effort)(cp->
8647c478bd9Sstevel@tonic-gate 				    cp_xcomp == *comp ? cp->cp_xstate : NULL,
8657c478bd9Sstevel@tonic-gate 				    cp->cp_rcomp == *comp ? cp->cp_rstate :
8667c478bd9Sstevel@tonic-gate 				    NULL, cp->cp_effort);
8677c478bd9Sstevel@tonic-gate 				if (rc != 0) {
8687c478bd9Sstevel@tonic-gate 					CKDEBUG((DBGSTART
8697c478bd9Sstevel@tonic-gate 					    "cannot set effort %d",
8707c478bd9Sstevel@tonic-gate 					    cp->cp_unit, cp->cp_effort));
8717c478bd9Sstevel@tonic-gate 					rc = 0;
8727c478bd9Sstevel@tonic-gate 				}
8737c478bd9Sstevel@tonic-gate 			}
8747c478bd9Sstevel@tonic-gate 			break;
8757c478bd9Sstevel@tonic-gate 		}
8767c478bd9Sstevel@tonic-gate 		break;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	case PPPIO_DEBUG:
8797c478bd9Sstevel@tonic-gate 		if (iop->ioc_count != sizeof (uint32_t) || mp->b_cont == NULL)
8807c478bd9Sstevel@tonic-gate 			break;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		cmd = *(uint32_t *)mp->b_cont->b_rptr;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		/* If it's not for us, then pass along. */
8857c478bd9Sstevel@tonic-gate 		if (cmd != PPPDBG_LOG + PPPDBG_COMP) {
8867c478bd9Sstevel@tonic-gate 			return (-1);	/* putnext */
8877c478bd9Sstevel@tonic-gate 		}
8887c478bd9Sstevel@tonic-gate 		cp->cp_flags |= CP_KDEBUG;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 		CKDEBUG((DBGSTART "PPPIO_DEBUG log enabled flags=0x%b\n",
8917c478bd9Sstevel@tonic-gate 		    (IS_CP_HASUNIT(cp) ? cp->cp_unit : -1),
8927c478bd9Sstevel@tonic-gate 		    cp->cp_flags, CP_FLAGSSTR));
8937c478bd9Sstevel@tonic-gate 		rc = 0;
8947c478bd9Sstevel@tonic-gate 		break;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	case PPPIO_LASTMOD:
8977c478bd9Sstevel@tonic-gate 		cp->cp_flags |= CP_LASTMOD;
8987c478bd9Sstevel@tonic-gate 		CPDEBUG((DBGSTART "PPPIO_LASTMOD last module flags=0x%b\n",
8997c478bd9Sstevel@tonic-gate 		    (IS_CP_HASUNIT(cp) ? cp->cp_unit : -1),
9007c478bd9Sstevel@tonic-gate 		    cp->cp_flags, CP_FLAGSSTR));
9017c478bd9Sstevel@tonic-gate 		rc = 0;
9027c478bd9Sstevel@tonic-gate 		break;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	case PPPIO_COMPLEV:	/* set compression effort level */
9057c478bd9Sstevel@tonic-gate 		if (iop->ioc_count != sizeof (uint32_t) || mp->b_cont == NULL)
9067c478bd9Sstevel@tonic-gate 			break;
9077c478bd9Sstevel@tonic-gate 		val = *(uint32_t *)mp->b_cont->b_rptr;
9087c478bd9Sstevel@tonic-gate 		cp->cp_effort = val;
9097c478bd9Sstevel@tonic-gate 		/* Silently ignore if compressor doesn't understand this. */
9107c478bd9Sstevel@tonic-gate 		rc = 0;
9117c478bd9Sstevel@tonic-gate 		if ((ccomp = cp->cp_xcomp) != NULL &&
9127c478bd9Sstevel@tonic-gate 		    ccomp->set_effort != NULL) {
9137c478bd9Sstevel@tonic-gate 			rc = (*ccomp->set_effort)(cp->cp_xstate,
9147c478bd9Sstevel@tonic-gate 			    ccomp == cp->cp_rcomp ? cp->cp_rstate : NULL, val);
9157c478bd9Sstevel@tonic-gate 			if (rc != 0)
9167c478bd9Sstevel@tonic-gate 				break;
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 		if ((ccomp = cp->cp_rcomp) != NULL && ccomp != cp->cp_xcomp &&
9197c478bd9Sstevel@tonic-gate 		    ccomp->set_effort != NULL)
9207c478bd9Sstevel@tonic-gate 			rc = (*ccomp->set_effort)(NULL, cp->cp_rstate, val);
9217c478bd9Sstevel@tonic-gate 		break;
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 	if (rc == 0 && mp->b_cont != NULL)
9247c478bd9Sstevel@tonic-gate 		mp->b_cont->b_wptr = mp->b_cont->b_rptr + len;
9257c478bd9Sstevel@tonic-gate 	return (rc);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate /*
9297c478bd9Sstevel@tonic-gate  * spppcomp_getcstat()
9307c478bd9Sstevel@tonic-gate  *
9317c478bd9Sstevel@tonic-gate  * MT-Perimeters:
9327c478bd9Sstevel@tonic-gate  *    exclusive inner.
9337c478bd9Sstevel@tonic-gate  *
9347c478bd9Sstevel@tonic-gate  * Description:
9357c478bd9Sstevel@tonic-gate  *    Called by spppcomp_ioctl as the result of receiving a PPPIO_GETCSTAT.
9367c478bd9Sstevel@tonic-gate  */
9377c478bd9Sstevel@tonic-gate static void
spppcomp_getcstat(queue_t * q,mblk_t * mp,sppp_comp_t * cp)9387c478bd9Sstevel@tonic-gate spppcomp_getcstat(queue_t *q, mblk_t *mp, sppp_comp_t *cp)
9397c478bd9Sstevel@tonic-gate {
9407c478bd9Sstevel@tonic-gate 	mblk_t		*mpnext;
9417c478bd9Sstevel@tonic-gate 	struct ppp_comp_stats	*csp;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	ASSERT(cp != NULL);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	mpnext = allocb(sizeof (struct ppp_comp_stats), BPRI_MED);
9467c478bd9Sstevel@tonic-gate 	if (mpnext == NULL) {
9477c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOSR);
9487c478bd9Sstevel@tonic-gate 		return;
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 	if (mp->b_cont != NULL) {
9517c478bd9Sstevel@tonic-gate 		freemsg(mp->b_cont);
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 	mp->b_cont = mpnext;
9547c478bd9Sstevel@tonic-gate 	csp = (struct ppp_comp_stats *)mpnext->b_wptr;
9557c478bd9Sstevel@tonic-gate 	mpnext->b_wptr += sizeof (struct ppp_comp_stats);
9567c478bd9Sstevel@tonic-gate 	bzero((caddr_t)csp, sizeof (struct ppp_comp_stats));
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	if (cp->cp_xstate != NULL) {
9597c478bd9Sstevel@tonic-gate 		(*cp->cp_xcomp->comp_stat)(cp->cp_xstate, &csp->c);
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 	if (cp->cp_rstate != NULL) {
9627c478bd9Sstevel@tonic-gate 		(*cp->cp_rcomp->decomp_stat)(cp->cp_rstate, &csp->d);
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	miocack(q, mp, sizeof (struct ppp_comp_stats), 0);
9667c478bd9Sstevel@tonic-gate }
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate /*
9697c478bd9Sstevel@tonic-gate  * spppcomp_ioctl()
9707c478bd9Sstevel@tonic-gate  *
9717c478bd9Sstevel@tonic-gate  * MT-Perimeters:
9727c478bd9Sstevel@tonic-gate  *    exclusive inner.
9737c478bd9Sstevel@tonic-gate  *
9747c478bd9Sstevel@tonic-gate  * Description:
9757c478bd9Sstevel@tonic-gate  *    Called by spppcomp_wput as the result of receiving an M_IOCTL
9767c478bd9Sstevel@tonic-gate  *    command.
9777c478bd9Sstevel@tonic-gate  */
9787c478bd9Sstevel@tonic-gate static void
spppcomp_ioctl(queue_t * q,mblk_t * mp,sppp_comp_t * cp)9797c478bd9Sstevel@tonic-gate spppcomp_ioctl(queue_t *q, mblk_t *mp, sppp_comp_t *cp)
9807c478bd9Sstevel@tonic-gate {
9817c478bd9Sstevel@tonic-gate 	struct iocblk	*iop;
9827c478bd9Sstevel@tonic-gate 	int flag;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	ASSERT(cp != NULL);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	iop = (struct iocblk *)mp->b_rptr;
9877c478bd9Sstevel@tonic-gate 	switch (iop->ioc_cmd) {
9887c478bd9Sstevel@tonic-gate 	case PPPIO_CFLAGS:
9897c478bd9Sstevel@tonic-gate 	case PPPIO_VJINIT:
9907c478bd9Sstevel@tonic-gate 	case PPPIO_XCOMP:
9917c478bd9Sstevel@tonic-gate 	case PPPIO_RCOMP:
9927c478bd9Sstevel@tonic-gate 	case PPPIO_DEBUG:
9937c478bd9Sstevel@tonic-gate 	case PPPIO_LASTMOD:
9947c478bd9Sstevel@tonic-gate 	case PPPIO_COMPLEV:
9957c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->cp_pair_lock);
9967c478bd9Sstevel@tonic-gate 		flag = spppcomp_inner_ioctl(q, mp);
9977c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->cp_pair_lock);
9987c478bd9Sstevel@tonic-gate 		if (flag == -1) {
9997c478bd9Sstevel@tonic-gate 			putnext(q, mp);
10007c478bd9Sstevel@tonic-gate 		} else if (flag == 0) {
10017c478bd9Sstevel@tonic-gate 			miocack(q, mp,
10027c478bd9Sstevel@tonic-gate 			    mp->b_cont == NULL ? 0 : MBLKL(mp->b_cont), 0);
10037c478bd9Sstevel@tonic-gate 		} else {
10047c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, flag);
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 		break;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	case PPPIO_GETCSTAT:
10097c478bd9Sstevel@tonic-gate 		spppcomp_getcstat(q, mp, cp);
10107c478bd9Sstevel@tonic-gate 		break;
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	case PPPIO_GTYPE:	/* get existing driver type */
10137c478bd9Sstevel@tonic-gate 		if (!IS_CP_LASTMOD(cp)) {
10147c478bd9Sstevel@tonic-gate 			putnext(q, mp);
10157c478bd9Sstevel@tonic-gate 			break;
10167c478bd9Sstevel@tonic-gate 		}
10177c478bd9Sstevel@tonic-gate 		freemsg(mp->b_next);
10187c478bd9Sstevel@tonic-gate 		mp->b_next = allocb(sizeof (uint32_t), BPRI_MED);
10197c478bd9Sstevel@tonic-gate 		if (mp->b_next == NULL) {
10207c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOSR);
10217c478bd9Sstevel@tonic-gate 		} else {
10227c478bd9Sstevel@tonic-gate 			*(uint32_t *)mp->b_cont->b_wptr = PPPTYP_HC;
10237c478bd9Sstevel@tonic-gate 			mp->b_cont->b_wptr += sizeof (uint32_t);
10247c478bd9Sstevel@tonic-gate 			miocack(q, mp, sizeof (uint32_t), 0);
10257c478bd9Sstevel@tonic-gate 		}
10267c478bd9Sstevel@tonic-gate 		break;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	default:
10297c478bd9Sstevel@tonic-gate 		putnext(q, mp);
10307c478bd9Sstevel@tonic-gate 		break;
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate /*
10357c478bd9Sstevel@tonic-gate  * spppcomp_mctl()
10367c478bd9Sstevel@tonic-gate  *
10377c478bd9Sstevel@tonic-gate  * MT-Perimeters:
10387c478bd9Sstevel@tonic-gate  *    exclusive inner; queue pair lock held.
10397c478bd9Sstevel@tonic-gate  *
10407c478bd9Sstevel@tonic-gate  * Description:
10417c478bd9Sstevel@tonic-gate  *	Called by spppcomp_wput as the result of receiving an M_CTL
10427c478bd9Sstevel@tonic-gate  *	message from another STREAMS module, and returns non-zero if
10437c478bd9Sstevel@tonic-gate  *	caller should do putnext or zero for freemsg.  Must *NOT* do
10447c478bd9Sstevel@tonic-gate  *	putnext in this routine, since lock is held here.
10457c478bd9Sstevel@tonic-gate  */
10467c478bd9Sstevel@tonic-gate static int
spppcomp_mctl(queue_t * q,mblk_t * mp)10477c478bd9Sstevel@tonic-gate spppcomp_mctl(queue_t *q, mblk_t *mp)
10487c478bd9Sstevel@tonic-gate {
1049f53eecf5SJames Carlson 	sppp_comp_t		*cp = q->q_ptr;
10507c478bd9Sstevel@tonic-gate 	kstat_t			*ksp;
10517c478bd9Sstevel@tonic-gate 	char			unit[32];
10527c478bd9Sstevel@tonic-gate 	const char **cpp;
10537c478bd9Sstevel@tonic-gate 	kstat_named_t *knt;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	switch (*mp->b_rptr) {
10567c478bd9Sstevel@tonic-gate 	case PPPCTL_MTU:
10577c478bd9Sstevel@tonic-gate 		if (MBLKL(mp) < 4) {
10587c478bd9Sstevel@tonic-gate 			break;
10597c478bd9Sstevel@tonic-gate 		}
10607c478bd9Sstevel@tonic-gate 		cp->cp_mtu = ((ushort_t *)mp->b_rptr)[1];
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 		CPDEBUG((DBGSTART "PPPCTL_MTU (%d) flags=0x%b\n",
10637c478bd9Sstevel@tonic-gate 		    (IS_CP_HASUNIT(cp) ? cp->cp_unit : -1),
10647c478bd9Sstevel@tonic-gate 		    cp->cp_mtu, cp->cp_flags, CP_FLAGSSTR));
10657c478bd9Sstevel@tonic-gate 		break;
10667c478bd9Sstevel@tonic-gate 	case PPPCTL_MRU:
10677c478bd9Sstevel@tonic-gate 		if (MBLKL(mp) < 4) {
10687c478bd9Sstevel@tonic-gate 			break;
10697c478bd9Sstevel@tonic-gate 		}
10707c478bd9Sstevel@tonic-gate 		cp->cp_mru = ((ushort_t *)mp->b_rptr)[1];
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 		CPDEBUG((DBGSTART "PPPCTL_MRU (%d) flags=0x%b\n",
10737c478bd9Sstevel@tonic-gate 		    (IS_CP_HASUNIT(cp) ? cp->cp_unit : -1),
10747c478bd9Sstevel@tonic-gate 		    cp->cp_mru, cp->cp_flags, CP_FLAGSSTR));
10757c478bd9Sstevel@tonic-gate 		break;
10767c478bd9Sstevel@tonic-gate 	case PPPCTL_UNIT:
10777c478bd9Sstevel@tonic-gate 		if (MBLKL(mp) < 8) {
10787c478bd9Sstevel@tonic-gate 			break;
10797c478bd9Sstevel@tonic-gate 		}
10807c478bd9Sstevel@tonic-gate 		/* If PPPCTL_UNIT has already been issued, then ignore. */
10817c478bd9Sstevel@tonic-gate 		if (IS_CP_HASUNIT(cp)) {
10827c478bd9Sstevel@tonic-gate 			break;
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 		ASSERT(cp->cp_kstats == NULL);
10857c478bd9Sstevel@tonic-gate 		cp->cp_unit = ((uint32_t *)mp->b_rptr)[1];
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 		/* Create kstats for this unit. */
1088002c70ffScarlsonj 		(void) sprintf(unit, "%s" "%d", COMP_MOD_NAME, cp->cp_unit);
10897c478bd9Sstevel@tonic-gate 		ksp = kstat_create(COMP_MOD_NAME, cp->cp_unit, unit, "net",
1090002c70ffScarlsonj 		    KSTAT_TYPE_NAMED, sizeof (spppcomp_kstats_t) /
1091002c70ffScarlsonj 		    sizeof (kstat_named_t), 0);
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 		if (ksp != NULL) {
10947c478bd9Sstevel@tonic-gate 			cp->cp_flags |= CP_HASUNIT;
10957c478bd9Sstevel@tonic-gate 			cp->cp_kstats = ksp;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 			knt = (kstat_named_t *)ksp->ks_data;
10987c478bd9Sstevel@tonic-gate 			for (cpp = kstats_names;
10997c478bd9Sstevel@tonic-gate 			    cpp < kstats_names + Dim(kstats_names); cpp++) {
1100d624471bSelowe 				kstat_named_init(knt, *cpp,
11017c478bd9Sstevel@tonic-gate 				    KSTAT_DATA_UINT32);
11027c478bd9Sstevel@tonic-gate 				knt++;
11037c478bd9Sstevel@tonic-gate 			}
11047c478bd9Sstevel@tonic-gate 			for (cpp = kstats64_names;
11057c478bd9Sstevel@tonic-gate 			    cpp < kstats64_names + Dim(kstats64_names); cpp++) {
1106d624471bSelowe 				kstat_named_init(knt, *cpp,
11077c478bd9Sstevel@tonic-gate 				    KSTAT_DATA_UINT64);
11087c478bd9Sstevel@tonic-gate 				knt++;
11097c478bd9Sstevel@tonic-gate 			}
11107c478bd9