xref: /onnv-gate/usr/src/uts/common/io/ppp/sppptun/sppptun.c (revision 9751:8e29565352fc)
18483Sjames.d.carlson@sun.com /*
28483Sjames.d.carlson@sun.com  * CDDL HEADER START
38483Sjames.d.carlson@sun.com  *
48483Sjames.d.carlson@sun.com  * The contents of this file are subject to the terms of the
58483Sjames.d.carlson@sun.com  * Common Development and Distribution License (the "License").
68483Sjames.d.carlson@sun.com  * You may not use this file except in compliance with the License.
78483Sjames.d.carlson@sun.com  *
88483Sjames.d.carlson@sun.com  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98483Sjames.d.carlson@sun.com  * or http://www.opensolaris.org/os/licensing.
108483Sjames.d.carlson@sun.com  * See the License for the specific language governing permissions
118483Sjames.d.carlson@sun.com  * and limitations under the License.
128483Sjames.d.carlson@sun.com  *
138483Sjames.d.carlson@sun.com  * When distributing Covered Code, include this CDDL HEADER in each
148483Sjames.d.carlson@sun.com  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
158483Sjames.d.carlson@sun.com  * If applicable, add the following below this CDDL HEADER, with the
168483Sjames.d.carlson@sun.com  * fields enclosed by brackets "[]" replaced with your own identifying
178483Sjames.d.carlson@sun.com  * information: Portions Copyright [yyyy] [name of copyright owner]
188483Sjames.d.carlson@sun.com  *
198483Sjames.d.carlson@sun.com  * CDDL HEADER END
208483Sjames.d.carlson@sun.com  */
218483Sjames.d.carlson@sun.com 
220Sstevel@tonic-gate /*
23*9751Sjames.d.carlson@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/debug.h>
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/stat.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/socket.h>
330Sstevel@tonic-gate #include <sys/stream.h>
340Sstevel@tonic-gate #include <sys/stropts.h>
350Sstevel@tonic-gate #include <sys/errno.h>
360Sstevel@tonic-gate #include <sys/time.h>
370Sstevel@tonic-gate #include <sys/cmn_err.h>
385640Scarlsonj #include <sys/sdt.h>
390Sstevel@tonic-gate #include <sys/conf.h>
400Sstevel@tonic-gate #include <sys/dlpi.h>
410Sstevel@tonic-gate #include <sys/ddi.h>
420Sstevel@tonic-gate #include <sys/kstat.h>
430Sstevel@tonic-gate #include <sys/strsun.h>
440Sstevel@tonic-gate #include <sys/bitmap.h>
450Sstevel@tonic-gate #include <sys/sysmacros.h>
460Sstevel@tonic-gate #include <sys/note.h>
470Sstevel@tonic-gate #include <sys/policy.h>
480Sstevel@tonic-gate #include <net/ppp_defs.h>
490Sstevel@tonic-gate #include <net/pppio.h>
500Sstevel@tonic-gate #include <net/sppptun.h>
510Sstevel@tonic-gate #include <net/pppoe.h>
520Sstevel@tonic-gate #include <netinet/in.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include "s_common.h"
550Sstevel@tonic-gate #include "sppptun_mod.h"
560Sstevel@tonic-gate #include "sppptun_impl.h"
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define	NTUN_INITIAL 16			/* Initial number of sppptun slots */
590Sstevel@tonic-gate #define	NTUN_PERCENT 5			/* Percent of memory to use */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * This is used to tag official Solaris sources.  Please do not define
630Sstevel@tonic-gate  * "INTERNAL_BUILD" when building this software outside of Sun
640Sstevel@tonic-gate  * Microsystems.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate #ifdef INTERNAL_BUILD
670Sstevel@tonic-gate /* MODINFO is limited to 32 characters. */
685640Scarlsonj const char sppptun_driver_description[] = "PPP 4.0 tunnel driver";
695640Scarlsonj const char sppptun_module_description[] = "PPP 4.0 tunnel module";
700Sstevel@tonic-gate #else
717656SSherry.Moore@Sun.COM const char sppptun_driver_description[] = "ANU PPP tundrv";
727656SSherry.Moore@Sun.COM const char sppptun_module_description[] = "ANU PPP tunmod";
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /* LINTED */
750Sstevel@tonic-gate static const char buildtime[] = "Built " __DATE__ " at " __TIME__
760Sstevel@tonic-gate #ifdef DEBUG
770Sstevel@tonic-gate " DEBUG"
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate "\n";
800Sstevel@tonic-gate #endif
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * Tunable values; these are similar to the values used in ptms_conf.c.
840Sstevel@tonic-gate  * Override these settings via /etc/system.
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate uint_t	sppptun_cnt = 0;		/* Minimum number of tunnels */
870Sstevel@tonic-gate size_t	sppptun_max_pty = 0;		/* Maximum number of tunnels */
880Sstevel@tonic-gate uint_t	sppptun_init_cnt = NTUN_INITIAL; /* Initial number of tunnel slots */
890Sstevel@tonic-gate uint_t	sppptun_pctofmem = NTUN_PERCENT; /* Percent of memory to use */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate typedef struct ether_dest_s {
920Sstevel@tonic-gate 	ether_addr_t addr;
930Sstevel@tonic-gate 	ushort_t type;
940Sstevel@tonic-gate } ether_dest_t;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /* Allows unaligned access. */
970Sstevel@tonic-gate #define	GETLONG(x)	(((x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static const char *tll_kstats_list[] = { TLL_KSTATS_NAMES };
1000Sstevel@tonic-gate static const char *tcl_kstats_list[] = { TCL_KSTATS_NAMES };
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #define	KREF(p, m, vn)	p->m.vn.value.ui64
1030Sstevel@tonic-gate #define	KINCR(p, m, vn)	++KREF(p, m, vn)
1040Sstevel@tonic-gate #define	KDECR(p, m, vn)	--KREF(p, m, vn)
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #define	KLINCR(vn)	KINCR(tll, tll_kstats, vn)
1070Sstevel@tonic-gate #define	KLDECR(vn)	KDECR(tll, tll_kstats, vn)
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #define	KCINCR(vn)	KINCR(tcl, tcl_kstats, vn)
1100Sstevel@tonic-gate #define	KCDECR(vn)	KDECR(tcl, tcl_kstats, vn)
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static int	sppptun_open(queue_t *, dev_t *, int, int, cred_t *);
1130Sstevel@tonic-gate static int	sppptun_close(queue_t *);
1140Sstevel@tonic-gate static void	sppptun_urput(queue_t *, mblk_t *);
1150Sstevel@tonic-gate static void	sppptun_uwput(queue_t *, mblk_t *);
1160Sstevel@tonic-gate static int	sppptun_ursrv(queue_t *);
1170Sstevel@tonic-gate static int	sppptun_uwsrv(queue_t *);
1180Sstevel@tonic-gate static void	sppptun_lrput(queue_t *, mblk_t *);
1190Sstevel@tonic-gate static void	sppptun_lwput(queue_t *, mblk_t *);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  * This is the hash table of clients.  Clients are the programs that
1230Sstevel@tonic-gate  * open /dev/sppptun as a device.  There may be a large number of
1240Sstevel@tonic-gate  * these; one per tunneled PPP session.
1250Sstevel@tonic-gate  *
1260Sstevel@tonic-gate  * Note: slots are offset from minor node value by 1 because
1270Sstevel@tonic-gate  * vmem_alloc returns 0 for failure.
1280Sstevel@tonic-gate  *
1290Sstevel@tonic-gate  * The tcl_slots array entries are modified only when exclusive on
1300Sstevel@tonic-gate  * both inner and outer perimeters.  This ensures that threads on
1310Sstevel@tonic-gate  * shared perimeters always view this as unchanging memory with no
1320Sstevel@tonic-gate  * need to lock around accesses.  (Specifically, the tcl_slots array
1330Sstevel@tonic-gate  * is modified by entry to sppptun_open, sppptun_close, and _fini.)
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate static tuncl_t **tcl_slots = NULL;	/* Slots for tuncl_t */
1360Sstevel@tonic-gate static size_t tcl_nslots = 0;		/* Size of slot array */
1370Sstevel@tonic-gate static size_t tcl_minormax = 0;		/* Maximum number of tunnels */
1380Sstevel@tonic-gate static size_t tcl_inuse = 0;		/* # of tunnels currently allocated */
1390Sstevel@tonic-gate static krwlock_t tcl_rwlock;
1400Sstevel@tonic-gate static struct kmem_cache *tcl_cache = NULL;	/* tunnel cache */
1410Sstevel@tonic-gate static vmem_t *tcl_minor_arena = NULL; /* Arena for device minors */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * This is the simple list of lower layers.  For PPPoE, there is one
1450Sstevel@tonic-gate  * of these per Ethernet interface.  Lower layers are established by
1460Sstevel@tonic-gate  * "plumbing" -- using I_PLINK to connect the tunnel multiplexor to
1470Sstevel@tonic-gate  * the physical interface.
1480Sstevel@tonic-gate  */
1490Sstevel@tonic-gate static struct qelem tunll_list;
1500Sstevel@tonic-gate static int tunll_index;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /* Test value; if all zeroes, then address hasn't been set yet. */
1530Sstevel@tonic-gate static const ether_addr_t zero_mac_addr = { 0, 0, 0, 0, 0, 0 };
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate #define	MIN_SET_FASTPATH_UNITDATAREQ_SIZE	\
1560Sstevel@tonic-gate 	(sizeof (dl_unitdata_req_t) + 4)
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate #define	TUN_MI_ID	2104	/* officially allocated module ID */
1590Sstevel@tonic-gate #define	TUN_MI_MINPSZ	(0)
1600Sstevel@tonic-gate #define	TUN_MI_MAXPSZ	(PPP_MAXMTU)
1610Sstevel@tonic-gate #define	TUN_MI_HIWAT	(PPP_MTU * 8)
1620Sstevel@tonic-gate #define	TUN_MI_LOWAT	(128)
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static struct module_info sppptun_modinfo = {
1650Sstevel@tonic-gate 	TUN_MI_ID,		/* mi_idnum */
1660Sstevel@tonic-gate 	PPP_TUN_NAME,		/* mi_idname */
1670Sstevel@tonic-gate 	TUN_MI_MINPSZ,		/* mi_minpsz */
1680Sstevel@tonic-gate 	TUN_MI_MAXPSZ,		/* mi_maxpsz */
1690Sstevel@tonic-gate 	TUN_MI_HIWAT,		/* mi_hiwat */
1700Sstevel@tonic-gate 	TUN_MI_LOWAT		/* mi_lowat */
1710Sstevel@tonic-gate };
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static struct qinit sppptun_urinit = {
1740Sstevel@tonic-gate 	(int (*)())sppptun_urput, /* qi_putp */
1750Sstevel@tonic-gate 	sppptun_ursrv,		/* qi_srvp */
1760Sstevel@tonic-gate 	sppptun_open,		/* qi_qopen */
1770Sstevel@tonic-gate 	sppptun_close,		/* qi_qclose */
1780Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
1790Sstevel@tonic-gate 	&sppptun_modinfo,	/* qi_minfo */
1800Sstevel@tonic-gate 	NULL			/* qi_mstat */
1810Sstevel@tonic-gate };
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static struct qinit sppptun_uwinit = {
1840Sstevel@tonic-gate 	(int (*)())sppptun_uwput, /* qi_putp */
1850Sstevel@tonic-gate 	sppptun_uwsrv,		/* qi_srvp */
1860Sstevel@tonic-gate 	NULL,			/* qi_qopen */
1870Sstevel@tonic-gate 	NULL,			/* qi_qclose */
1880Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
1890Sstevel@tonic-gate 	&sppptun_modinfo,	/* qi_minfo */
1900Sstevel@tonic-gate 	NULL			/* qi_mstat */
1910Sstevel@tonic-gate };
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static struct qinit sppptun_lrinit = {
1940Sstevel@tonic-gate 	(int (*)())sppptun_lrput, /* qi_putp */
1950Sstevel@tonic-gate 	NULL,			/* qi_srvp */
1960Sstevel@tonic-gate 	NULL,			/* qi_qopen */
1970Sstevel@tonic-gate 	NULL,			/* qi_qclose */
1980Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
1990Sstevel@tonic-gate 	&sppptun_modinfo,	/* qi_minfo */
2000Sstevel@tonic-gate 	NULL			/* qi_mstat */
2010Sstevel@tonic-gate };
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate static struct qinit sppptun_lwinit = {
2040Sstevel@tonic-gate 	(int (*)())sppptun_lwput, /* qi_putp */
2050Sstevel@tonic-gate 	NULL,			/* qi_srvp */
2060Sstevel@tonic-gate 	NULL,			/* qi_qopen */
2070Sstevel@tonic-gate 	NULL,			/* qi_qclose */
2080Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
2090Sstevel@tonic-gate 	&sppptun_modinfo,	/* qi_minfo */
2100Sstevel@tonic-gate 	NULL			/* qi_mstat */
2110Sstevel@tonic-gate };
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * This is referenced in sppptun_mod.c.
2150Sstevel@tonic-gate  */
2160Sstevel@tonic-gate struct streamtab sppptun_tab = {
2170Sstevel@tonic-gate 	&sppptun_urinit,	/* st_rdinit */
2180Sstevel@tonic-gate 	&sppptun_uwinit,	/* st_wrinit */
2190Sstevel@tonic-gate 	&sppptun_lrinit,	/* st_muxrinit */
2200Sstevel@tonic-gate 	&sppptun_lwinit		/* st_muxwrinit */
2210Sstevel@tonic-gate };
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate  * Allocate another slot table twice as large as the original one
2250Sstevel@tonic-gate  * (limited to global maximum).  Migrate all tunnels to the new slot
2260Sstevel@tonic-gate  * table and free the original one.  Assumes we're exclusive on both
2270Sstevel@tonic-gate  * inner and outer perimeters, and thus there are no other users of
2280Sstevel@tonic-gate  * the tcl_slots array.
2290Sstevel@tonic-gate  */
2300Sstevel@tonic-gate static minor_t
tcl_grow(void)2310Sstevel@tonic-gate tcl_grow(void)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate 	minor_t old_size = tcl_nslots;
2340Sstevel@tonic-gate 	minor_t new_size = 2 * old_size;
2350Sstevel@tonic-gate 	tuncl_t **tcl_old = tcl_slots;
2360Sstevel@tonic-gate 	tuncl_t **tcl_new;
2370Sstevel@tonic-gate 	void  *vaddr;			/* vmem_add return value */
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&tcl_rwlock));
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/* Allocate new ptms array */
2420Sstevel@tonic-gate 	tcl_new = kmem_zalloc(new_size * sizeof (tuncl_t *), KM_NOSLEEP);
2430Sstevel@tonic-gate 	if (tcl_new == NULL)
2440Sstevel@tonic-gate 		return ((minor_t)0);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/* Increase clone index space */
2470Sstevel@tonic-gate 	vaddr = vmem_add(tcl_minor_arena, (void*)((uintptr_t)old_size + 1),
2480Sstevel@tonic-gate 	    new_size - old_size, VM_NOSLEEP);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (vaddr == NULL) {
2510Sstevel@tonic-gate 		kmem_free(tcl_new, new_size * sizeof (tuncl_t *));
2520Sstevel@tonic-gate 		return ((minor_t)0);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/* Migrate tuncl_t entries to a new location */
2560Sstevel@tonic-gate 	tcl_nslots = new_size;
2570Sstevel@tonic-gate 	bcopy(tcl_old, tcl_new, old_size * sizeof (tuncl_t *));
2580Sstevel@tonic-gate 	tcl_slots = tcl_new;
2590Sstevel@tonic-gate 	kmem_free(tcl_old, old_size * sizeof (tuncl_t *));
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/* Allocate minor number and return it */
2620Sstevel@tonic-gate 	return ((minor_t)(uintptr_t)vmem_alloc(tcl_minor_arena, 1, VM_NOSLEEP));
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate  * Allocate new minor number and tunnel client entry.  Returns the new
2670Sstevel@tonic-gate  * entry or NULL if no memory or maximum number of entries reached.
2680Sstevel@tonic-gate  * Assumes we're exclusive on both inner and outer perimeters, and
2690Sstevel@tonic-gate  * thus there are no other users of the tcl_slots array.
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate static tuncl_t *
tuncl_alloc(int wantminor)2720Sstevel@tonic-gate tuncl_alloc(int wantminor)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	minor_t dminor;
2750Sstevel@tonic-gate 	tuncl_t *tcl = NULL;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	rw_enter(&tcl_rwlock, RW_WRITER);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	ASSERT(tcl_slots != NULL);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/*
2820Sstevel@tonic-gate 	 * Always try to allocate new pty when sppptun_cnt minimum
2830Sstevel@tonic-gate 	 * limit is not achieved. If it is achieved, the maximum is
2840Sstevel@tonic-gate 	 * determined by either user-specified value (if it is
2850Sstevel@tonic-gate 	 * non-zero) or our memory estimations - whatever is less.
2860Sstevel@tonic-gate 	 */
2870Sstevel@tonic-gate 	if (tcl_inuse >= sppptun_cnt) {
2880Sstevel@tonic-gate 		/*
2890Sstevel@tonic-gate 		 * When system achieved required minimum of tunnels,
2900Sstevel@tonic-gate 		 * check for the denial of service limits.
2910Sstevel@tonic-gate 		 *
2920Sstevel@tonic-gate 		 * Get user-imposed maximum, if configured, or
2930Sstevel@tonic-gate 		 * calculated memory constraint.
2940Sstevel@tonic-gate 		 */
2950Sstevel@tonic-gate 		size_t user_max = (sppptun_max_pty == 0 ? tcl_minormax :
2960Sstevel@tonic-gate 		    min(sppptun_max_pty, tcl_minormax));
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 		/* Do not try to allocate more than allowed */
2990Sstevel@tonic-gate 		if (tcl_inuse >= user_max) {
3000Sstevel@tonic-gate 			rw_exit(&tcl_rwlock);
3010Sstevel@tonic-gate 			return (NULL);
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 	tcl_inuse++;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * Allocate new minor number. If this fails, all slots are
3080Sstevel@tonic-gate 	 * busy and we need to grow the hash.
3090Sstevel@tonic-gate 	 */
3100Sstevel@tonic-gate 	if (wantminor <= 0) {
3110Sstevel@tonic-gate 		dminor = (minor_t)(uintptr_t)vmem_alloc(tcl_minor_arena, 1,
3120Sstevel@tonic-gate 		    VM_NOSLEEP);
3130Sstevel@tonic-gate 		if (dminor == 0) {
3140Sstevel@tonic-gate 			/* Grow the cache and retry allocation */
3150Sstevel@tonic-gate 			dminor = tcl_grow();
3160Sstevel@tonic-gate 		}
3170Sstevel@tonic-gate 	} else {
3180Sstevel@tonic-gate 		dminor = (minor_t)(uintptr_t)vmem_xalloc(tcl_minor_arena, 1,
3190Sstevel@tonic-gate 		    0, 0, 0, (void *)(uintptr_t)wantminor,
3200Sstevel@tonic-gate 		    (void *)((uintptr_t)wantminor+1), VM_NOSLEEP);
3210Sstevel@tonic-gate 		if (dminor != 0 && dminor != wantminor) {
3220Sstevel@tonic-gate 			vmem_free(tcl_minor_arena, (void *)(uintptr_t)dminor,
3230Sstevel@tonic-gate 			    1);
3240Sstevel@tonic-gate 			dminor = 0;
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (dminor == 0) {
3290Sstevel@tonic-gate 		/* Not enough memory now */
3300Sstevel@tonic-gate 		tcl_inuse--;
3310Sstevel@tonic-gate 		rw_exit(&tcl_rwlock);
3320Sstevel@tonic-gate 		return (NULL);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	tcl = kmem_cache_alloc(tcl_cache, KM_NOSLEEP);
3360Sstevel@tonic-gate 	if (tcl == NULL) {
3370Sstevel@tonic-gate 		/* Not enough memory - this entry can't be used now. */
3380Sstevel@tonic-gate 		vmem_free(tcl_minor_arena, (void *)(uintptr_t)dminor, 1);
3390Sstevel@tonic-gate 		tcl_inuse--;
3400Sstevel@tonic-gate 	} else {
3410Sstevel@tonic-gate 		bzero(tcl, sizeof (*tcl));
3420Sstevel@tonic-gate 		tcl->tcl_lsessid = dminor;
3430Sstevel@tonic-gate 		ASSERT(tcl_slots[dminor - 1] == NULL);
3440Sstevel@tonic-gate 		tcl_slots[dminor - 1] = tcl;
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	rw_exit(&tcl_rwlock);
3480Sstevel@tonic-gate 	return (tcl);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /*
3520Sstevel@tonic-gate  * This routine frees an upper level (client) stream by removing it
3530Sstevel@tonic-gate  * from the minor number pool and freeing the state structure storage.
3540Sstevel@tonic-gate  * Assumes we're exclusive on both inner and outer perimeters, and
3550Sstevel@tonic-gate  * thus there are no other concurrent users of the tcl_slots array or
3560Sstevel@tonic-gate  * of any entry in that array.
3570Sstevel@tonic-gate  */
3580Sstevel@tonic-gate static void
tuncl_free(tuncl_t * tcl)3590Sstevel@tonic-gate tuncl_free(tuncl_t *tcl)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	rw_enter(&tcl_rwlock, RW_WRITER);
3620Sstevel@tonic-gate 	ASSERT(tcl->tcl_lsessid <= tcl_nslots);
3630Sstevel@tonic-gate 	ASSERT(tcl_slots[tcl->tcl_lsessid - 1] == tcl);
3640Sstevel@tonic-gate 	ASSERT(tcl_inuse > 0);
3650Sstevel@tonic-gate 	tcl_inuse--;
3660Sstevel@tonic-gate 	tcl_slots[tcl->tcl_lsessid - 1] = NULL;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (tcl->tcl_ksp != NULL) {
3690Sstevel@tonic-gate 		kstat_delete(tcl->tcl_ksp);
3700Sstevel@tonic-gate 		tcl->tcl_ksp = NULL;
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/* Return minor number to the pool of minors */
3740Sstevel@tonic-gate 	vmem_free(tcl_minor_arena, (void *)(uintptr_t)tcl->tcl_lsessid, 1);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	/* Return tuncl_t to the cache */
3770Sstevel@tonic-gate 	kmem_cache_free(tcl_cache, tcl);
3780Sstevel@tonic-gate 	rw_exit(&tcl_rwlock);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate /*
3820Sstevel@tonic-gate  * Get tuncl_t structure by minor number.  Returns NULL when minor is
3830Sstevel@tonic-gate  * out of range.  Note that lookup of tcl pointers (and use of those
3840Sstevel@tonic-gate  * pointers) is safe because modification is done only when exclusive
3850Sstevel@tonic-gate  * on both inner and outer perimeters.
3860Sstevel@tonic-gate  */
3870Sstevel@tonic-gate static tuncl_t *
tcl_by_minor(minor_t dminor)3880Sstevel@tonic-gate tcl_by_minor(minor_t dminor)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	tuncl_t *tcl = NULL;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if ((dminor >= 1) && (dminor <= tcl_nslots) && tcl_slots != NULL) {
3930Sstevel@tonic-gate 		tcl = tcl_slots[dminor - 1];
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	return (tcl);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate  * Set up kstats for upper or lower stream.
4010Sstevel@tonic-gate  */
4020Sstevel@tonic-gate static kstat_t *
kstat_setup(kstat_named_t * knt,const char ** names,int nstat,const char * modname,int unitnum)4030Sstevel@tonic-gate kstat_setup(kstat_named_t *knt, const char **names, int nstat,
4040Sstevel@tonic-gate     const char *modname, int unitnum)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	kstat_t *ksp;
4070Sstevel@tonic-gate 	char unitname[KSTAT_STRLEN];
4080Sstevel@tonic-gate 	int i;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	for (i = 0; i < nstat; i++) {
4112951Selowe 		kstat_set_string(knt[i].name, names[i]);
4120Sstevel@tonic-gate 		knt[i].data_type = KSTAT_DATA_UINT64;
4130Sstevel@tonic-gate 	}
4145640Scarlsonj 	(void) sprintf(unitname, "%s" "%d", modname, unitnum);
4152951Selowe 	ksp = kstat_create(modname, unitnum, unitname, "net",
4160Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, nstat, KSTAT_FLAG_VIRTUAL);
4170Sstevel@tonic-gate 	if (ksp != NULL) {
4180Sstevel@tonic-gate 		ksp->ks_data = (void *)knt;
4190Sstevel@tonic-gate 		kstat_install(ksp);
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 	return (ksp);
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate  * sppptun_open()
4260Sstevel@tonic-gate  *
4270Sstevel@tonic-gate  * MT-Perimeters:
4280Sstevel@tonic-gate  *    exclusive inner, exclusive outer.
4290Sstevel@tonic-gate  *
4300Sstevel@tonic-gate  * Description:
4310Sstevel@tonic-gate  *    Common open procedure for module and driver.
4320Sstevel@tonic-gate  */
4330Sstevel@tonic-gate static int
sppptun_open(queue_t * q,dev_t * devp,int oflag,int sflag,cred_t * credp)4340Sstevel@tonic-gate sppptun_open(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *credp)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate 	_NOTE(ARGUNUSED(oflag))
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	/* Allow a re-open */
4390Sstevel@tonic-gate 	if (q->q_ptr != NULL)
4400Sstevel@tonic-gate 		return (0);
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	/* In the off chance that we're on our way out, just return error */
4435640Scarlsonj 	if (tcl_slots == NULL)
4440Sstevel@tonic-gate 		return (EINVAL);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (sflag & MODOPEN) {
4470Sstevel@tonic-gate 		tunll_t *tll;
4480Sstevel@tonic-gate 		char *cp;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 		/* ordinary users have no need to push this module */
451*9751Sjames.d.carlson@sun.com 		if (secpolicy_ppp_config(credp) != 0)
4520Sstevel@tonic-gate 			return (EPERM);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		tll = kmem_zalloc(sizeof (tunll_t), KM_SLEEP);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 		tll->tll_index = tunll_index++;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		tll->tll_wq = WR(q);
459*9751Sjames.d.carlson@sun.com 		tll->tll_zoneid = crgetzoneid(credp);
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 		/* Insert at end of list */
4620Sstevel@tonic-gate 		insque(&tll->tll_next, tunll_list.q_back);
4630Sstevel@tonic-gate 		q->q_ptr = WR(q)->q_ptr = tll;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		tll->tll_style = PTS_PPPOE;
4660Sstevel@tonic-gate 		tll->tll_alen = sizeof (tll->tll_lcladdr.pta_pppoe);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		tll->tll_ksp = kstat_setup((kstat_named_t *)&tll->tll_kstats,
4690Sstevel@tonic-gate 		    tll_kstats_list, Dim(tll_kstats_list), "tll",
4700Sstevel@tonic-gate 		    tll->tll_index);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 		/*
4730Sstevel@tonic-gate 		 * Find the name of the driver somewhere beneath us.
4740Sstevel@tonic-gate 		 * Note that we have no driver under us until after
4750Sstevel@tonic-gate 		 * qprocson().
4760Sstevel@tonic-gate 		 */
4770Sstevel@tonic-gate 		qprocson(q);
4780Sstevel@tonic-gate 		for (q = WR(q); q->q_next != NULL; q = q->q_next)
4790Sstevel@tonic-gate 			;
4800Sstevel@tonic-gate 		cp = NULL;
4810Sstevel@tonic-gate 		if (q->q_qinfo != NULL && q->q_qinfo->qi_minfo != NULL)
4820Sstevel@tonic-gate 			cp = q->q_qinfo->qi_minfo->mi_idname;
4830Sstevel@tonic-gate 		if (cp != NULL && *cp == '\0')
4840Sstevel@tonic-gate 			cp = NULL;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 		/* Set initial name; user should overwrite. */
4870Sstevel@tonic-gate 		if (cp == NULL)
4880Sstevel@tonic-gate 			(void) snprintf(tll->tll_name, sizeof (tll->tll_name),
4890Sstevel@tonic-gate 			    PPP_TUN_NAME "%d", tll->tll_index);
4900Sstevel@tonic-gate 		else
4910Sstevel@tonic-gate 			(void) snprintf(tll->tll_name, sizeof (tll->tll_name),
4920Sstevel@tonic-gate 			    "%s:tun%d", cp, tll->tll_index);
4930Sstevel@tonic-gate 	} else {
4940Sstevel@tonic-gate 		tuncl_t	*tcl;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		ASSERT(devp != NULL);
4970Sstevel@tonic-gate 		if (sflag & CLONEOPEN) {
4980Sstevel@tonic-gate 			tcl = tuncl_alloc(-1);
4990Sstevel@tonic-gate 		} else {
5000Sstevel@tonic-gate 			minor_t mn;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 			/*
5030Sstevel@tonic-gate 			 * Support of non-clone open (ie, mknod with
5040Sstevel@tonic-gate 			 * defined minor number) is supported for
5050Sstevel@tonic-gate 			 * testing purposes so that 'arbitrary' minor
5060Sstevel@tonic-gate 			 * numbers can be used.
5070Sstevel@tonic-gate 			 */
5080Sstevel@tonic-gate 			mn = getminor(*devp);
5090Sstevel@tonic-gate 			if (mn == 0 || (tcl = tcl_by_minor(mn)) != NULL) {
5100Sstevel@tonic-gate 				return (EPERM);
5110Sstevel@tonic-gate 			}
5120Sstevel@tonic-gate 			tcl = tuncl_alloc(mn);
5130Sstevel@tonic-gate 		}
5140Sstevel@tonic-gate 		if (tcl == NULL)
5150Sstevel@tonic-gate 			return (ENOSR);
5160Sstevel@tonic-gate 		tcl->tcl_rq = q;		/* save read queue pointer */
5170Sstevel@tonic-gate 		tcl->tcl_flags |= TCLF_ISCLIENT;	/* sanity check */
518*9751Sjames.d.carlson@sun.com 		tcl->tcl_zoneid = crgetzoneid(credp);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 		q->q_ptr = WR(q)->q_ptr = (caddr_t)tcl;
5210Sstevel@tonic-gate 		*devp = makedevice(getmajor(*devp), tcl->tcl_lsessid);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 		tcl->tcl_ksp = kstat_setup((kstat_named_t *)&tcl->tcl_kstats,
5240Sstevel@tonic-gate 		    tcl_kstats_list, Dim(tcl_kstats_list), "tcl",
5250Sstevel@tonic-gate 		    tcl->tcl_lsessid);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 		qprocson(q);
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 	return (0);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate /*
5330Sstevel@tonic-gate  * Create an appropriate control message for this client event.
5340Sstevel@tonic-gate  */
5350Sstevel@tonic-gate static mblk_t *
make_control(tuncl_t * tclabout,tunll_t * tllabout,int action,tuncl_t * tclto)5360Sstevel@tonic-gate make_control(tuncl_t *tclabout, tunll_t *tllabout, int action, tuncl_t *tclto)
5370Sstevel@tonic-gate {
5380Sstevel@tonic-gate 	struct ppptun_control *ptc;
5390Sstevel@tonic-gate 	mblk_t *mp = allocb(sizeof (*ptc), BPRI_HI);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	if (mp != NULL) {
5420Sstevel@tonic-gate 		MTYPE(mp) = M_PROTO;
5430Sstevel@tonic-gate 		ptc = (struct ppptun_control *)mp->b_wptr;
544*9751Sjames.d.carlson@sun.com 		bzero(ptc, sizeof (*ptc));
5450Sstevel@tonic-gate 		mp->b_wptr += sizeof (*ptc);
5460Sstevel@tonic-gate 		if (tclabout != NULL) {
5470Sstevel@tonic-gate 			ptc->ptc_rsessid = tclabout->tcl_rsessid;
5480Sstevel@tonic-gate 			ptc->ptc_address = tclabout->tcl_address;
5490Sstevel@tonic-gate 		}
5500Sstevel@tonic-gate 		ptc->ptc_discrim = tclto->tcl_ctlval;
5510Sstevel@tonic-gate 		ptc->ptc_action = action;
552*9751Sjames.d.carlson@sun.com 		if (tllabout != NULL) {
553*9751Sjames.d.carlson@sun.com 			(void) strncpy(ptc->ptc_name, tllabout->tll_name,
554*9751Sjames.d.carlson@sun.com 			    sizeof (ptc->ptc_name));
555*9751Sjames.d.carlson@sun.com 		}
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 	return (mp);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate /*
5610Sstevel@tonic-gate  * Send an appropriate control message up this client session.
5620Sstevel@tonic-gate  */
5630Sstevel@tonic-gate static void
send_control(tuncl_t * tclabout,tunll_t * tllabout,int action,tuncl_t * tcl)5640Sstevel@tonic-gate send_control(tuncl_t *tclabout, tunll_t *tllabout, int action, tuncl_t *tcl)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate 	mblk_t *mp;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	if (tcl->tcl_rq != NULL) {
5690Sstevel@tonic-gate 		mp = make_control(tclabout, tllabout, action, tcl);
5700Sstevel@tonic-gate 		if (mp != NULL) {
5710Sstevel@tonic-gate 			KCINCR(cks_octrl_spec);
5720Sstevel@tonic-gate 			putnext(tcl->tcl_rq, mp);
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate  * If a lower stream is being unplumbed, then the upper streams
5790Sstevel@tonic-gate  * connected to this lower stream must be disconnected.  This routine
5800Sstevel@tonic-gate  * accomplishes this by sending M_HANGUP to data streams and M_PROTO
5810Sstevel@tonic-gate  * messages to control streams.  This is called by vmem_walk, and
5820Sstevel@tonic-gate  * handles a span of minor node numbers.
5830Sstevel@tonic-gate  *
5840Sstevel@tonic-gate  * No need to update lks_clients here; the lower stream is on its way
5850Sstevel@tonic-gate  * out.
5860Sstevel@tonic-gate  */
5870Sstevel@tonic-gate static void
tclvm_remove_tll(void * arg,void * firstv,size_t numv)5880Sstevel@tonic-gate tclvm_remove_tll(void *arg, void *firstv, size_t numv)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	tunll_t *tll = (tunll_t *)arg;
5910Sstevel@tonic-gate 	int minorn = (int)(uintptr_t)firstv;
5920Sstevel@tonic-gate 	int minormax = minorn + numv;
5930Sstevel@tonic-gate 	tuncl_t *tcl;
5940Sstevel@tonic-gate 	mblk_t *mp;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	while (minorn < minormax) {
5970Sstevel@tonic-gate 		tcl = tcl_slots[minorn - 1];
5980Sstevel@tonic-gate 		ASSERT(tcl != NULL);
5990Sstevel@tonic-gate 		if (tcl->tcl_data_tll == tll && tcl->tcl_rq != NULL) {
6000Sstevel@tonic-gate 			tcl->tcl_data_tll = NULL;
6010Sstevel@tonic-gate 			mp = allocb(0, BPRI_HI);
6020Sstevel@tonic-gate 			if (mp != NULL) {
6030Sstevel@tonic-gate 				MTYPE(mp) = M_HANGUP;
6040Sstevel@tonic-gate 				putnext(tcl->tcl_rq, mp);
6050Sstevel@tonic-gate 				if (tcl->tcl_ctrl_tll == tll)
6060Sstevel@tonic-gate 					tcl->tcl_ctrl_tll = NULL;
6070Sstevel@tonic-gate 			}
6080Sstevel@tonic-gate 		}
6090Sstevel@tonic-gate 		if (tcl->tcl_ctrl_tll == tll) {
6100Sstevel@tonic-gate 			send_control(tcl, tll, PTCA_UNPLUMB, tcl);
6110Sstevel@tonic-gate 			tcl->tcl_ctrl_tll = NULL;
6120Sstevel@tonic-gate 		}
6130Sstevel@tonic-gate 		minorn++;
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate /*
6180Sstevel@tonic-gate  * sppptun_close()
6190Sstevel@tonic-gate  *
6200Sstevel@tonic-gate  * MT-Perimeters:
6210Sstevel@tonic-gate  *    exclusive inner, exclusive outer.
6220Sstevel@tonic-gate  *
6230Sstevel@tonic-gate  * Description:
6240Sstevel@tonic-gate  *    Common close procedure for module and driver.
6250Sstevel@tonic-gate  */
6260Sstevel@tonic-gate static int
sppptun_close(queue_t * q)6270Sstevel@tonic-gate sppptun_close(queue_t *q)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate 	int err;
6300Sstevel@tonic-gate 	void *qptr;
6310Sstevel@tonic-gate 	tunll_t *tll;
6320Sstevel@tonic-gate 	tuncl_t *tcl;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	qptr = q->q_ptr;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	err = 0;
6375640Scarlsonj 	tll = qptr;
6380Sstevel@tonic-gate 	if (!(tll->tll_flags & TLLF_NOTLOWER)) {
6390Sstevel@tonic-gate 		/* q_next is set on modules */
6400Sstevel@tonic-gate 		ASSERT(WR(q)->q_next != NULL);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		/* unlink any clients using this lower layer. */
6430Sstevel@tonic-gate 		vmem_walk(tcl_minor_arena, VMEM_ALLOC, tclvm_remove_tll, tll);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		/* tell daemon that this has been removed. */
6460Sstevel@tonic-gate 		if ((tcl = tll->tll_defcl) != NULL)
6470Sstevel@tonic-gate 			send_control(NULL, tll, PTCA_UNPLUMB, tcl);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		tll->tll_flags |= TLLF_CLOSING;
6500Sstevel@tonic-gate 		while (!(tll->tll_flags & TLLF_CLOSE_DONE)) {
6510Sstevel@tonic-gate 			qenable(tll->tll_wq);
6520Sstevel@tonic-gate 			qwait(tll->tll_wq);
6530Sstevel@tonic-gate 		}
6540Sstevel@tonic-gate 		tll->tll_error = 0;
6550Sstevel@tonic-gate 		while (!(tll->tll_flags & TLLF_SHUTDOWN_DONE)) {
6560Sstevel@tonic-gate 			if (!qwait_sig(tll->tll_wq))
6570Sstevel@tonic-gate 				break;
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 		qprocsoff(q);
6610Sstevel@tonic-gate 		q->q_ptr = WR(q)->q_ptr = NULL;
6620Sstevel@tonic-gate 		tll->tll_wq = NULL;
6630Sstevel@tonic-gate 		remque(&tll->tll_next);
6640Sstevel@tonic-gate 		err = tll->tll_error;
6650Sstevel@tonic-gate 		if (tll->tll_ksp != NULL)
6660Sstevel@tonic-gate 			kstat_delete(tll->tll_ksp);
6670Sstevel@tonic-gate 		kmem_free(tll, sizeof (*tll));
6680Sstevel@tonic-gate 	} else {
6695640Scarlsonj 		tcl = qptr;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 		/* devices are end of line; no q_next. */
6720Sstevel@tonic-gate 		ASSERT(WR(q)->q_next == NULL);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 		qprocsoff(q);
6755640Scarlsonj 		DTRACE_PROBE1(sppptun__client__close, tuncl_t *, tcl);
6760Sstevel@tonic-gate 		tcl->tcl_rq = NULL;
6770Sstevel@tonic-gate 		q->q_ptr = WR(q)->q_ptr = NULL;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		tll = TO_TLL(tunll_list.q_forw);
6800Sstevel@tonic-gate 		while (tll != TO_TLL(&tunll_list)) {
6810Sstevel@tonic-gate 			if (tll->tll_defcl == tcl)
6820Sstevel@tonic-gate 				tll->tll_defcl = NULL;
6830Sstevel@tonic-gate 			if (tll->tll_lastcl == tcl)
6840Sstevel@tonic-gate 				tll->tll_lastcl = NULL;
6850Sstevel@tonic-gate 			tll = TO_TLL(tll->tll_next);
6860Sstevel@tonic-gate 		}
6870Sstevel@tonic-gate 		/*
6880Sstevel@tonic-gate 		 * If this was a normal session, then tell the daemon.
6890Sstevel@tonic-gate 		 */
6900Sstevel@tonic-gate 		if (!(tcl->tcl_flags & TCLF_DAEMON) &&
6910Sstevel@tonic-gate 		    (tll = tcl->tcl_ctrl_tll) != NULL &&
6920Sstevel@tonic-gate 		    tll->tll_defcl != NULL) {
6930Sstevel@tonic-gate 			send_control(tcl, tll, PTCA_DISCONNECT,
6940Sstevel@tonic-gate 			    tll->tll_defcl);
6950Sstevel@tonic-gate 		}
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		/* Update statistics for references being dropped. */
6980Sstevel@tonic-gate 		if ((tll = tcl->tcl_data_tll) != NULL) {
6990Sstevel@tonic-gate 			KLDECR(lks_clients);
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 		if ((tll = tcl->tcl_ctrl_tll) != NULL) {
7020Sstevel@tonic-gate 			KLDECR(lks_clients);
7030Sstevel@tonic-gate 		}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		tuncl_free(tcl);
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	return (err);
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate /*
7120Sstevel@tonic-gate  * Allocate and initialize a DLPI or TPI template of the specified
7130Sstevel@tonic-gate  * length.
7140Sstevel@tonic-gate  */
7150Sstevel@tonic-gate static mblk_t *
pi_alloc(size_t len,int prim)7160Sstevel@tonic-gate pi_alloc(size_t len, int prim)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	mblk_t	*mp;
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	mp = allocb(len, BPRI_MED);
7210Sstevel@tonic-gate 	if (mp != NULL) {
7220Sstevel@tonic-gate 		MTYPE(mp) = M_PROTO;
7230Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + len;
7240Sstevel@tonic-gate 		bzero(mp->b_rptr, len);
7250Sstevel@tonic-gate 		*(int *)mp->b_rptr = prim;
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 	return (mp);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate #define	dlpi_alloc(l, p)	pi_alloc((l), (p))
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate /*
7330Sstevel@tonic-gate  * Prepend some room to an mblk.  Try to reuse the existing buffer, if
7340Sstevel@tonic-gate  * at all possible, rather than allocating a new one.  (Fast-path
7350Sstevel@tonic-gate  * output should be able to use this.)
7360Sstevel@tonic-gate  *
7370Sstevel@tonic-gate  * (XXX why isn't this a library function ...?)
7380Sstevel@tonic-gate  */
7390Sstevel@tonic-gate static mblk_t *
prependb(mblk_t * mp,size_t len,size_t align)7400Sstevel@tonic-gate prependb(mblk_t *mp, size_t len, size_t align)
7410Sstevel@tonic-gate {
7420Sstevel@tonic-gate 	mblk_t *newmp;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	if (align == 0)
7460Sstevel@tonic-gate 		align = 8;
7470Sstevel@tonic-gate 	if (DB_REF(mp) > 1 || mp->b_datap->db_base+len > mp->b_rptr ||
7480Sstevel@tonic-gate 	    ((uint_t)((uintptr_t)mp->b_rptr - len) % align) != 0) {
7490Sstevel@tonic-gate 		if ((newmp = allocb(len, BPRI_LO)) == NULL) {
7500Sstevel@tonic-gate 			freemsg(mp);
7510Sstevel@tonic-gate 			return (NULL);
7520Sstevel@tonic-gate 		}
7530Sstevel@tonic-gate 		newmp->b_wptr = newmp->b_rptr + len;
7540Sstevel@tonic-gate 		newmp->b_cont = mp;
7550Sstevel@tonic-gate 		return (newmp);
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 	mp->b_rptr -= len;
7580Sstevel@tonic-gate 	return (mp);
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate /*
7620Sstevel@tonic-gate  * sppptun_outpkt()
7630Sstevel@tonic-gate  *
7640Sstevel@tonic-gate  * MT-Perimeters:
7650Sstevel@tonic-gate  *	shared inner, shared outer (if called from sppptun_uwput),
7660Sstevel@tonic-gate  *	exclusive inner, shared outer (if called from sppptun_uwsrv).
7670Sstevel@tonic-gate  *
7680Sstevel@tonic-gate  * Description:
7690Sstevel@tonic-gate  *    Called from sppptun_uwput or sppptun_uwsrv when processing a
7700Sstevel@tonic-gate  *    M_DATA, M_PROTO, or M_PCPROTO message.  For all cases, it tries
7710Sstevel@tonic-gate  *    to prepare the data to be sent to the module below this driver
7720Sstevel@tonic-gate  *    if there is a lower stream linked underneath.  If no lower
7730Sstevel@tonic-gate  *    stream exists, then the data will be discarded and an ENXIO
7740Sstevel@tonic-gate  *    error returned.
7750Sstevel@tonic-gate  *
7760Sstevel@tonic-gate  * Returns:
7770Sstevel@tonic-gate  *	pointer to queue if caller should do putnext, otherwise
7780Sstevel@tonic-gate  *	*mpp != NULL if message should be enqueued, otherwise
7790Sstevel@tonic-gate  *	*mpp == NULL if message is gone.
7800Sstevel@tonic-gate  */
7810Sstevel@tonic-gate static queue_t *
sppptun_outpkt(queue_t * q,mblk_t ** mpp)7820Sstevel@tonic-gate sppptun_outpkt(queue_t *q, mblk_t **mpp)
7830Sstevel@tonic-gate {
7840Sstevel@tonic-gate 	mblk_t *mp;
7850Sstevel@tonic-gate 	tuncl_t *tcl;
7860Sstevel@tonic-gate 	tunll_t *tll;
7870Sstevel@tonic-gate 	mblk_t *encmb;
7880Sstevel@tonic-gate 	mblk_t *datamb;
7890Sstevel@tonic-gate 	dl_unitdata_req_t *dur;
7900Sstevel@tonic-gate 	queue_t *lowerq;
7910Sstevel@tonic-gate 	poep_t *poep;
7920Sstevel@tonic-gate 	int len;
7930Sstevel@tonic-gate 	ether_dest_t *edestp;
7940Sstevel@tonic-gate 	enum { luNone, luCopy, luSend } loopup;
7950Sstevel@tonic-gate 	boolean_t isdata;
7960Sstevel@tonic-gate 	struct ppptun_control *ptc;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	mp = *mpp;
7995640Scarlsonj 	tcl = q->q_ptr;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	*mpp = NULL;
8020Sstevel@tonic-gate 	if (!(tcl->tcl_flags & TCLF_ISCLIENT)) {
803*9751Sjames.d.carlson@sun.com 		/* This should never happen on a lower layer stream */
804*9751Sjames.d.carlson@sun.com 		freemsg(mp);
8050Sstevel@tonic-gate 		return (NULL);
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	isdata = (MTYPE(mp) == M_DATA);
8090Sstevel@tonic-gate 	if (isdata) {
8100Sstevel@tonic-gate 		tll = tcl->tcl_data_tll;
8110Sstevel@tonic-gate 		ptc = NULL;
8120Sstevel@tonic-gate 	} else {
8130Sstevel@tonic-gate 		/*
8140Sstevel@tonic-gate 		 * If data are unaligned or otherwise unsuitable, then
8150Sstevel@tonic-gate 		 * discard.
8160Sstevel@tonic-gate 		 */
8170Sstevel@tonic-gate 		if (MBLKL(mp) != sizeof (*ptc) || DB_REF(mp) > 1 ||
8180Sstevel@tonic-gate 		    !IS_P2ALIGNED(mp->b_rptr, sizeof (ptc))) {
8190Sstevel@tonic-gate 			KCINCR(cks_octrl_drop);
8205640Scarlsonj 			DTRACE_PROBE2(sppptun__bad__control, tuncl_t *, tcl,
8215640Scarlsonj 			    mblk_t *, mp);
822*9751Sjames.d.carlson@sun.com 			send_control(tcl, tcl->tcl_ctrl_tll, PTCA_BADCTRL, tcl);
823*9751Sjames.d.carlson@sun.com 			freemsg(mp);
8240Sstevel@tonic-gate 			return (NULL);
8250Sstevel@tonic-gate 		}
8260Sstevel@tonic-gate 		ptc = (struct ppptun_control *)mp->b_rptr;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 		/* Set stream discriminator value if not yet set. */
8290Sstevel@tonic-gate 		if (tcl->tcl_ctlval == 0)
8300Sstevel@tonic-gate 			tcl->tcl_ctlval = ptc->ptc_discrim;
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 		/* If this is a test message, then reply to caller. */
8330Sstevel@tonic-gate 		if (ptc->ptc_action == PTCA_TEST) {
8345640Scarlsonj 			DTRACE_PROBE2(sppptun__test, tuncl_t *, tcl,
8355640Scarlsonj 			    struct ppptun_control *, ptc);
8360Sstevel@tonic-gate 			if (mp->b_cont != NULL) {
8370Sstevel@tonic-gate 				freemsg(mp->b_cont);
8380Sstevel@tonic-gate 				mp->b_cont = NULL;
8390Sstevel@tonic-gate 			}
8400Sstevel@tonic-gate 			ptc->ptc_discrim = tcl->tcl_ctlval;
8410Sstevel@tonic-gate 			putnext(RD(q), mp);
8420Sstevel@tonic-gate 			return (NULL);
8430Sstevel@tonic-gate 		}
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 		/* If this one isn't for us, then discard it */
8460Sstevel@tonic-gate 		if (tcl->tcl_ctlval != ptc->ptc_discrim) {
8475640Scarlsonj 			DTRACE_PROBE2(sppptun__bad__discrim, tuncl_t *, tcl,
8485640Scarlsonj 			    struct ppptun_control *, ptc);
8490Sstevel@tonic-gate 			freemsg(mp);
8500Sstevel@tonic-gate 			return (NULL);
8510Sstevel@tonic-gate 		}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 		/* Don't allow empty control packets. */
854*9751Sjames.d.carlson@sun.com 		tll = tcl->tcl_ctrl_tll;
8550Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
8560Sstevel@tonic-gate 			KCINCR(cks_octrl_drop);
857*9751Sjames.d.carlson@sun.com 			DTRACE_PROBE2(sppptun__bad__control, tuncl_t *, tcl,
858*9751Sjames.d.carlson@sun.com 			    mblk_t *, mp);
859*9751Sjames.d.carlson@sun.com 			send_control(tcl, tll, PTCA_BADCTRL, tcl);
860*9751Sjames.d.carlson@sun.com 			freemsg(mp);
8610Sstevel@tonic-gate 			return (NULL);
8620Sstevel@tonic-gate 		}
8630Sstevel@tonic-gate 	}
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	if (tll == NULL || (lowerq = tll->tll_wq) == NULL) {
8665640Scarlsonj 		DTRACE_PROBE3(sppptun__cannot__send, tuncl_t *, tcl,
8675640Scarlsonj 		    tunll_t *, tll, mblk_t *, mp);
868*9751Sjames.d.carlson@sun.com 		send_control(tcl, tll, PTCA_UNPLUMB, tcl);
869*9751Sjames.d.carlson@sun.com 		freemsg(mp);
8700Sstevel@tonic-gate 		if (isdata) {
8710Sstevel@tonic-gate 			tcl->tcl_stats.ppp_oerrors++;
8720Sstevel@tonic-gate 		} else {
8730Sstevel@tonic-gate 			KCINCR(cks_octrl_drop);
8740Sstevel@tonic-gate 		}
8750Sstevel@tonic-gate 		return (NULL);
8760Sstevel@tonic-gate 	}
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	/*
8790Sstevel@tonic-gate 	 * If so, then try to send it down.  The lower queue is only
8800Sstevel@tonic-gate 	 * ever detached while holding an exclusive lock on the whole
8810Sstevel@tonic-gate 	 * driver, so we can be confident that the lower queue is
8820Sstevel@tonic-gate 	 * still there.
8830Sstevel@tonic-gate 	 */
8840Sstevel@tonic-gate 	if (!bcanputnext(lowerq, mp->b_band)) {
8855640Scarlsonj 		DTRACE_PROBE3(sppptun__flow__control, tuncl_t *, tcl,
8865640Scarlsonj 		    tunll_t *, tll, mblk_t *, mp);
8870Sstevel@tonic-gate 		*mpp = mp;
8880Sstevel@tonic-gate 		return (NULL);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	/*
8920Sstevel@tonic-gate 	 * Note: DLPI and TPI expect that the first buffer contains
8930Sstevel@tonic-gate 	 * the control (unitdata-req) header, destination address, and
8940Sstevel@tonic-gate 	 * nothing else.  Any protocol headers must go in the next
8950Sstevel@tonic-gate 	 * buffer.
8960Sstevel@tonic-gate 	 */
8970Sstevel@tonic-gate 	loopup = luNone;
8980Sstevel@tonic-gate 	encmb = NULL;
8990Sstevel@tonic-gate 	if (isdata) {
9000Sstevel@tonic-gate 		if (tll->tll_alen != 0 &&
9010Sstevel@tonic-gate 		    bcmp(&tcl->tcl_address, &tll->tll_lcladdr,
9025640Scarlsonj 		    tll->tll_alen) == 0)
9030Sstevel@tonic-gate 			loopup = luSend;
9040Sstevel@tonic-gate 		switch (tll->tll_style) {
9050Sstevel@tonic-gate 		case PTS_PPPOE:
9060Sstevel@tonic-gate 			/* Strip address and control fields if present. */
9070Sstevel@tonic-gate 			if (mp->b_rptr[0] == 0xFF) {
9080Sstevel@tonic-gate 				if (MBLKL(mp) < 3) {
9090Sstevel@tonic-gate 					encmb = msgpullup(mp, 3);
9100Sstevel@tonic-gate 					freemsg(mp);
9110Sstevel@tonic-gate 					if ((mp = encmb) == NULL)
9120Sstevel@tonic-gate 						break;
9130Sstevel@tonic-gate 				}
9140Sstevel@tonic-gate 				mp->b_rptr += 2;
9150Sstevel@tonic-gate 			}
9160Sstevel@tonic-gate 			/* Broadcasting data is probably not a good idea. */
9170Sstevel@tonic-gate 			if (tcl->tcl_address.pta_pppoe.ptma_mac[0] & 1)
9180Sstevel@tonic-gate 				break;
9190Sstevel@tonic-gate 			encmb = dlpi_alloc(sizeof (*dur) + sizeof (*edestp),
9200Sstevel@tonic-gate 			    DL_UNITDATA_REQ);
9210Sstevel@tonic-gate 			if (encmb == NULL)
9220Sstevel@tonic-gate 				break;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 			dur = (dl_unitdata_req_t *)encmb->b_rptr;
9250Sstevel@tonic-gate 			dur->dl_dest_addr_length = sizeof (*edestp);
9260Sstevel@tonic-gate 			dur->dl_dest_addr_offset = sizeof (*dur);
9270Sstevel@tonic-gate 			edestp = (ether_dest_t *)(dur + 1);
9280Sstevel@tonic-gate 			ether_copy(tcl->tcl_address.pta_pppoe.ptma_mac,
9290Sstevel@tonic-gate 			    edestp->addr);
9300Sstevel@tonic-gate 			/* DLPI SAPs are in host byte order! */
931*9751Sjames.d.carlson@sun.com 			edestp->type = tll->tll_sap;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 			/* Make sure the protocol field isn't compressed. */
9340Sstevel@tonic-gate 			len = (*mp->b_rptr & 1);
9350Sstevel@tonic-gate 			mp = prependb(mp, sizeof (*poep) + len, POE_HDR_ALIGN);
9360Sstevel@tonic-gate 			if (mp == NULL)
9370Sstevel@tonic-gate 				break;
9380Sstevel@tonic-gate 			poep = (poep_t *)mp->b_rptr;
9390Sstevel@tonic-gate 			poep->poep_version_type = POE_VERSION;
9400Sstevel@tonic-gate 			poep->poep_code = POECODE_DATA;
9410Sstevel@tonic-gate 			poep->poep_session_id = htons(tcl->tcl_rsessid);
9420Sstevel@tonic-gate 			poep->poep_length = htons(msgsize(mp) -
9430Sstevel@tonic-gate 			    sizeof (*poep));
9440Sstevel@tonic-gate 			if (len > 0)
9450Sstevel@tonic-gate 				*(char *)(poep + 1) = '\0';
9460Sstevel@tonic-gate 			break;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		default:
9490Sstevel@tonic-gate 			ASSERT(0);
9500Sstevel@tonic-gate 		}
9510Sstevel@tonic-gate 	} else {
9520Sstevel@tonic-gate 		/*
9530Sstevel@tonic-gate 		 * Control side encapsulation.
9540Sstevel@tonic-gate 		 */
9550Sstevel@tonic-gate 		if (bcmp(&ptc->ptc_address, &tll->tll_lcladdr, tll->tll_alen)
9560Sstevel@tonic-gate 		    == 0)
9570Sstevel@tonic-gate 			loopup = luSend;
9580Sstevel@tonic-gate 		datamb = mp->b_cont;
9590Sstevel@tonic-gate 		switch (tll->tll_style) {
9600Sstevel@tonic-gate 		case PTS_PPPOE:
9610Sstevel@tonic-gate 			/*
9620Sstevel@tonic-gate 			 * Don't allow a loopback session to establish
9630Sstevel@tonic-gate 			 * itself.  PPPoE is broken; it uses only one
9640Sstevel@tonic-gate 			 * session ID for both data directions, so the
9650Sstevel@tonic-gate 			 * loopback data path can simply never work.
9660Sstevel@tonic-gate 			 */
9670Sstevel@tonic-gate 			if (loopup == luSend &&
9680Sstevel@tonic-gate 			    ((poep_t *)datamb->b_rptr)->poep_code ==
9690Sstevel@tonic-gate 			    POECODE_PADR)
9700Sstevel@tonic-gate 				break;
9710Sstevel@tonic-gate 			encmb = dlpi_alloc(sizeof (*dur) + sizeof (*edestp),
9720Sstevel@tonic-gate 			    DL_UNITDATA_REQ);
9730Sstevel@tonic-gate 			if (encmb == NULL)
9740Sstevel@tonic-gate 				break;
9750Sstevel@tonic-gate 			dur = (dl_unitdata_req_t *)encmb->b_rptr;
9760Sstevel@tonic-gate 			dur->dl_dest_addr_length = sizeof (*edestp);
9770Sstevel@tonic-gate 			dur->dl_dest_addr_offset = sizeof (*dur);
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 			edestp = (ether_dest_t *)(dur + 1);
9800Sstevel@tonic-gate 			/* DLPI SAPs are in host byte order! */
981*9751Sjames.d.carlson@sun.com 			edestp->type = tll->tll_sap;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 			/*
9840Sstevel@tonic-gate 			 * If destination isn't set yet, then we have to
9850Sstevel@tonic-gate 			 * allow anything at all.  Otherwise, force use
9860Sstevel@tonic-gate 			 * of configured peer address.
9870Sstevel@tonic-gate 			 */
9880Sstevel@tonic-gate 			if (bcmp(tcl->tcl_address.pta_pppoe.ptma_mac,
9890Sstevel@tonic-gate 			    zero_mac_addr, sizeof (zero_mac_addr)) == 0 ||
9905640Scarlsonj 			    (tcl->tcl_flags & TCLF_DAEMON)) {
9910Sstevel@tonic-gate 				ether_copy(ptc->ptc_address.pta_pppoe.ptma_mac,
9920Sstevel@tonic-gate 				    edestp->addr);
9930Sstevel@tonic-gate 			} else {
9940Sstevel@tonic-gate 				ether_copy(tcl->tcl_address.pta_pppoe.ptma_mac,
9950Sstevel@tonic-gate 				    edestp->addr);
9960Sstevel@tonic-gate 			}
9970Sstevel@tonic-gate 			/* Reflect multicast/broadcast back up. */
9980Sstevel@tonic-gate 			if (edestp->addr[0] & 1)
9990Sstevel@tonic-gate 				loopup = luCopy;
10000Sstevel@tonic-gate 			break;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 		case PTS_PPTP:
10030Sstevel@tonic-gate 			/*
10040Sstevel@tonic-gate 			 * PPTP's control side is actually done over
10050Sstevel@tonic-gate 			 * separate TCP connections.
10060Sstevel@tonic-gate 			 */
10070Sstevel@tonic-gate 		default:
10080Sstevel@tonic-gate 			ASSERT(0);
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 		freeb(mp);
10110Sstevel@tonic-gate 		mp = datamb;
10120Sstevel@tonic-gate 	}
10130Sstevel@tonic-gate 	if (mp == NULL || encmb == NULL) {
10145640Scarlsonj 		DTRACE_PROBE1(sppptun__output__failure, tuncl_t *, tcl);
10150Sstevel@tonic-gate 		freemsg(mp);
10160Sstevel@tonic-gate 		freemsg(encmb);
10170Sstevel@tonic-gate 		if (isdata) {
10180Sstevel@tonic-gate 			tcl->tcl_stats.ppp_oerrors++;
10190Sstevel@tonic-gate 		} else {
10200Sstevel@tonic-gate 			KCINCR(cks_octrl_drop);
10210Sstevel@tonic-gate 			KLINCR(lks_octrl_drop);
10220Sstevel@tonic-gate 		}
10230Sstevel@tonic-gate 		lowerq = NULL;
10240Sstevel@tonic-gate 	} else {
10250Sstevel@tonic-gate 		if (isdata) {
10260Sstevel@tonic-gate 			tcl->tcl_stats.ppp_obytes += msgsize(mp);
10270Sstevel@tonic-gate 			tcl->tcl_stats.ppp_opackets++;
10280Sstevel@tonic-gate 		} else {
10290Sstevel@tonic-gate 			KCINCR(cks_octrls);
10300Sstevel@tonic-gate 			KLINCR(lks_octrls);
10310Sstevel@tonic-gate 		}
10320Sstevel@tonic-gate 		if (encmb != mp)
10330Sstevel@tonic-gate 			encmb->b_cont = mp;
10340Sstevel@tonic-gate 		switch (loopup) {
10350Sstevel@tonic-gate 		case luNone:
10360Sstevel@tonic-gate 			*mpp = encmb;
10370Sstevel@tonic-gate 			break;
10380Sstevel@tonic-gate 		case luCopy:
10390Sstevel@tonic-gate 			mp = copymsg(encmb);
10400Sstevel@tonic-gate 			if (mp != NULL)
10410Sstevel@tonic-gate 				sppptun_urput(RD(lowerq), mp);
10420Sstevel@tonic-gate 			*mpp = encmb;
10430Sstevel@tonic-gate 			break;
10440Sstevel@tonic-gate 		case luSend:
10450Sstevel@tonic-gate 			sppptun_urput(RD(lowerq), encmb);
10460Sstevel@tonic-gate 			lowerq = NULL;
10470Sstevel@tonic-gate 			break;
10480Sstevel@tonic-gate 		}
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 	return (lowerq);
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate /*
10540Sstevel@tonic-gate  * Enqueue a message to be sent when the lower stream is closed.  This
10550Sstevel@tonic-gate  * is done so that we're guaranteed that we always have the necessary
10560Sstevel@tonic-gate  * resources to properly detach ourselves from the system.  (If we
10570Sstevel@tonic-gate  * waited until the close was done to allocate these messages, then
10580Sstevel@tonic-gate  * the message allocation could fail, and we'd be unable to properly
10590Sstevel@tonic-gate  * detach.)
10600Sstevel@tonic-gate  */
10610Sstevel@tonic-gate static void
save_for_close(tunll_t * tll,mblk_t * mp)10620Sstevel@tonic-gate save_for_close(tunll_t *tll, mblk_t *mp)
10630Sstevel@tonic-gate {
10640Sstevel@tonic-gate 	mblk_t *onc;
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	if ((onc = tll->tll_onclose) == NULL)
10670Sstevel@tonic-gate 		tll->tll_onclose = mp;
10680Sstevel@tonic-gate 	else {
10690Sstevel@tonic-gate 		while (onc->b_next != NULL)
10700Sstevel@tonic-gate 			onc = onc->b_next;
10710Sstevel@tonic-gate 		onc->b_next = mp;
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate }
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate /*
10760Sstevel@tonic-gate  * Given the lower stream name, locate the state structure.  Note that
10770Sstevel@tonic-gate  * lookup of tcl pointers (and use of those pointers) is safe because
10780Sstevel@tonic-gate  * modification is done only when exclusive on both inner and outer
10790Sstevel@tonic-gate  * perimeters.
10800Sstevel@tonic-gate  */
10810Sstevel@tonic-gate static tunll_t *
tll_lookup_on_name(const char * dname,zoneid_t zoneid)1082*9751Sjames.d.carlson@sun.com tll_lookup_on_name(const char *dname, zoneid_t zoneid)
10830Sstevel@tonic-gate {
10840Sstevel@tonic-gate 	tunll_t *tll;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	tll = TO_TLL(tunll_list.q_forw);
10870Sstevel@tonic-gate 	for (; tll != TO_TLL(&tunll_list); tll = TO_TLL(tll->tll_next))
1088*9751Sjames.d.carlson@sun.com 		if (tll->tll_zoneid == zoneid &&
1089*9751Sjames.d.carlson@sun.com 		    strcmp(dname, tll->tll_name) == 0)
10900Sstevel@tonic-gate 			return (tll);
10910Sstevel@tonic-gate 	return (NULL);
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate /*
10950Sstevel@tonic-gate  * sppptun_inner_ioctl()
10960Sstevel@tonic-gate  *
10970Sstevel@tonic-gate  * MT-Perimeters:
10980Sstevel@tonic-gate  *    exclusive inner, shared outer.
10990Sstevel@tonic-gate  *
11000Sstevel@tonic-gate  * Description:
11010Sstevel@tonic-gate  *    Called by qwriter from sppptun_ioctl as the result of receiving
11020Sstevel@tonic-gate  *    a handled ioctl.
11030Sstevel@tonic-gate  */
11040Sstevel@tonic-gate static void
sppptun_inner_ioctl(queue_t * q,mblk_t * mp)11050Sstevel@tonic-gate sppptun_inner_ioctl(queue_t *q, mblk_t *mp)
11060Sstevel@tonic-gate {
11070Sstevel@tonic-gate 	struct iocblk *iop;
11080Sstevel@tonic-gate 	int rc = 0;
11090Sstevel@tonic-gate 	int len = 0;
11100Sstevel@tonic-gate 	int i;
11110Sstevel@tonic-gate 	tuncl_t *tcl;
11120Sstevel@tonic-gate 	tunll_t *tll;
11130Sstevel@tonic-gate 	union ppptun_name *ptn;
11140Sstevel@tonic-gate 	struct ppptun_info *pti;
11150Sstevel@tonic-gate 	struct ppptun_peer *ptp;
11160Sstevel@tonic-gate 	mblk_t *mptmp;
11170Sstevel@tonic-gate 	ppptun_atype *pap;
11180Sstevel@tonic-gate 	struct ppp_stats64 *psp;
1119*9751Sjames.d.carlson@sun.com 	zoneid_t zoneid;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	iop = (struct iocblk *)mp->b_rptr;
11220Sstevel@tonic-gate 	tcl = NULL;
11235640Scarlsonj 	tll = q->q_ptr;
11240Sstevel@tonic-gate 	if (tll->tll_flags & TLLF_NOTLOWER) {
11250Sstevel@tonic-gate 		tcl = (tuncl_t *)tll;
11260Sstevel@tonic-gate 		tll = NULL;
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 
11295640Scarlsonj 	DTRACE_PROBE3(sppptun__ioctl, tuncl_t *, tcl, tunll_t *, tll,
11305640Scarlsonj 	    struct iocblk *, iop);
11315640Scarlsonj 
11320Sstevel@tonic-gate 	switch (iop->ioc_cmd) {
11330Sstevel@tonic-gate 	case PPPIO_DEBUG:
11345640Scarlsonj 		/*
11355640Scarlsonj 		 * Debug requests are now ignored; use dtrace or wireshark
11365640Scarlsonj 		 * instead.
11375640Scarlsonj 		 */
11380Sstevel@tonic-gate 		break;
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	case PPPIO_GETSTAT:
11410Sstevel@tonic-gate 		rc = EINVAL;
11420Sstevel@tonic-gate 		break;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	case PPPIO_GETSTAT64:
11450Sstevel@tonic-gate 		/* Client (device) side only */
11460Sstevel@tonic-gate 		if (tcl == NULL) {
11470Sstevel@tonic-gate 			rc = EINVAL;
11480Sstevel@tonic-gate 			break;
11490Sstevel@tonic-gate 		}
11500Sstevel@tonic-gate 		mptmp = allocb(sizeof (*psp), BPRI_HI);
11510Sstevel@tonic-gate 		if (mptmp == NULL) {
11520Sstevel@tonic-gate 			rc = ENOSR;
11530Sstevel@tonic-gate 			break;
11540Sstevel@tonic-gate 		}
11550Sstevel@tonic-gate 		freemsg(mp->b_cont);
11560Sstevel@tonic-gate 		mp->b_cont = mptmp;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 		psp = (struct ppp_stats64 *)mptmp->b_wptr;
11590Sstevel@tonic-gate 		bzero((caddr_t)psp, sizeof (*psp));
11600Sstevel@tonic-gate 		psp->p = tcl->tcl_stats;
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 		len = sizeof (*psp);
11630Sstevel@tonic-gate 		break;
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	case PPPTUN_SNAME:
11660Sstevel@tonic-gate 		/* This is done on the *module* (lower level) side. */
11670Sstevel@tonic-gate 		if (tll == NULL || mp->b_cont == NULL ||
11680Sstevel@tonic-gate 		    iop->ioc_count != sizeof (*ptn) ||
11690Sstevel@tonic-gate 		    *mp->b_cont->b_rptr == '\0') {
11700Sstevel@tonic-gate 			rc = EINVAL;
11710Sstevel@tonic-gate 			break;
11720Sstevel@tonic-gate 		}
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 		ptn = (union ppptun_name *)mp->b_cont->b_rptr;
11750Sstevel@tonic-gate 		ptn->ptn_name[sizeof (ptn->ptn_name) - 1] = '\0';
11760Sstevel@tonic-gate 
1177*9751Sjames.d.carlson@sun.com 		tll = tll_lookup_on_name(ptn->ptn_name, tll->tll_zoneid);
1178*9751Sjames.d.carlson@sun.com 		if (tll != NULL) {
11790Sstevel@tonic-gate 			rc = EEXIST;
11800Sstevel@tonic-gate 			break;
11810Sstevel@tonic-gate 		}
11820Sstevel@tonic-gate 		tll = (tunll_t *)q->q_ptr;
11830Sstevel@tonic-gate 		(void) strcpy(tll->tll_name, ptn->ptn_name);
11840Sstevel@tonic-gate 		break;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	case PPPTUN_SINFO:
11870Sstevel@tonic-gate 	case PPPTUN_GINFO:
11880Sstevel@tonic-gate 		/* Either side */
11890Sstevel@tonic-gate 		if (mp->b_cont == NULL || iop->ioc_count != sizeof (*pti)) {
11900Sstevel@tonic-gate 			rc = EINVAL;
11910Sstevel@tonic-gate 			break;
11920Sstevel@tonic-gate 		}
11930Sstevel@tonic-gate 		pti = (struct ppptun_info *)mp->b_cont->b_rptr;
11940Sstevel@tonic-gate 		if (pti->pti_name[0] != '\0')
1195*9751Sjames.d.carlson@sun.com 			tll = tll_lookup_on_name(pti->pti_name,
1196*9751Sjames.d.carlson@sun.com 			    tcl == NULL ? tll->tll_zoneid : tcl->tcl_zoneid);
11970Sstevel@tonic-gate 		if (tll == NULL) {
11980Sstevel@tonic-gate 			/* Driver (client) side must have name */
11995640Scarlsonj 			if (tcl != NULL && pti->pti_name[0] == '\0')
12000Sstevel@tonic-gate 				rc = EINVAL;
12015640Scarlsonj 			else
12020Sstevel@tonic-gate 				rc = ESRCH;
12030Sstevel@tonic-gate 			break;
12040Sstevel@tonic-gate 		}
12050Sstevel@tonic-gate 		if (iop->ioc_cmd == PPPTUN_GINFO) {
12060Sstevel@tonic-gate 			pti->pti_muxid = tll->tll_muxid;
12070Sstevel@tonic-gate 			pti->pti_style = tll->tll_style;
12080Sstevel@tonic-gate 			len = sizeof (*pti);
12090Sstevel@tonic-gate 			break;
12100Sstevel@tonic-gate 		}
12110Sstevel@tonic-gate 		tll->tll_muxid = pti->pti_muxid;
12120Sstevel@tonic-gate 		tll->tll_style = pti->pti_style;
12130Sstevel@tonic-gate 		switch (tll->tll_style) {
12140Sstevel@tonic-gate 		case PTS_PPPOE:		/* DLPI type */
12150Sstevel@tonic-gate 			tll->tll_alen = sizeof (tll->tll_lcladdr.pta_pppoe);
12160Sstevel@tonic-gate 			mptmp = dlpi_alloc(sizeof (dl_unbind_req_t),
12170Sstevel@tonic-gate 			    DL_UNBIND_REQ);
12180Sstevel@tonic-gate 			if (mptmp == NULL) {
12190Sstevel@tonic-gate 				rc = ENOSR;
12200Sstevel@tonic-gate 				break;
12210Sstevel@tonic-gate 			}
12220Sstevel@tonic-gate 			save_for_close(tll, mptmp);
12230Sstevel@tonic-gate 			mptmp = dlpi_alloc(sizeof (dl_detach_req_t),
12240Sstevel@tonic-gate 			    DL_DETACH_REQ);
12250Sstevel@tonic-gate 			if (mptmp == NULL) {
12260Sstevel@tonic-gate 				rc = ENOSR;
12270Sstevel@tonic-gate 				break;
12280Sstevel@tonic-gate 			}
12290Sstevel@tonic-gate 			save_for_close(tll, mptmp);
12300Sstevel@tonic-gate 			break;
12310Sstevel@tonic-gate 		default:
12320Sstevel@tonic-gate 			tll->tll_style = PTS_NONE;
12330Sstevel@tonic-gate 			tll->tll_alen = 0;
12340Sstevel@tonic-gate 			rc = EINVAL;
12350Sstevel@tonic-gate 			break;
12360Sstevel@tonic-gate 		}
12370Sstevel@tonic-gate 		break;
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	case PPPTUN_GNNAME:
12400Sstevel@tonic-gate 		/* This can be done on either side. */
12410Sstevel@tonic-gate 		if (mp->b_cont == NULL || iop->ioc_count < sizeof (uint32_t)) {
12420Sstevel@tonic-gate 			rc = EINVAL;
12430Sstevel@tonic-gate 			break;
12440Sstevel@tonic-gate 		}
1245*9751Sjames.d.carlson@sun.com 		zoneid = tcl == NULL ? tll->tll_zoneid : tcl->tcl_zoneid;
12460Sstevel@tonic-gate 		ptn = (union ppptun_name *)mp->b_cont->b_rptr;
12470Sstevel@tonic-gate 		i = ptn->ptn_index;
12480Sstevel@tonic-gate 		tll = TO_TLL(tunll_list.q_forw);
1249*9751Sjames.d.carlson@sun.com 		while (tll != TO_TLL(&tunll_list)) {
1250*9751Sjames.d.carlson@sun.com 			if (tll->tll_zoneid == zoneid && --i < 0)
1251*9751Sjames.d.carlson@sun.com 				break;
12520Sstevel@tonic-gate 			tll = TO_TLL(tll->tll_next);
1253*9751Sjames.d.carlson@sun.com 		}
12540Sstevel@tonic-gate 		if (tll != TO_TLL(&tunll_list)) {
12550Sstevel@tonic-gate 			bcopy(tll->tll_name, ptn->ptn_name,
12560Sstevel@tonic-gate 			    sizeof (ptn->ptn_name));
12570Sstevel@tonic-gate 		} else {
12580Sstevel@tonic-gate 			bzero(ptn, sizeof (*ptn));
12590Sstevel@tonic-gate 		}
12600Sstevel@tonic-gate 		len = sizeof (*ptn);
12610Sstevel@tonic-gate 		break;
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	case PPPTUN_LCLADDR:
12640Sstevel@tonic-gate 		/* This is done on the *module* (lower level) side. */
12650Sstevel@tonic-gate 		if (tll == NULL || mp->b_cont == NULL) {
12660Sstevel@tonic-gate 			rc = EINVAL;
12670Sstevel@tonic-gate 			break;
12680Sstevel@tonic-gate 		}
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 		pap = &tll->tll_lcladdr;
12710Sstevel@tonic-gate 		len = tll->tll_alen;
12720Sstevel@tonic-gate 		if (len == 0 || len > iop->ioc_count) {
12730Sstevel@tonic-gate 			rc = EINVAL;
12740Sstevel@tonic-gate 			break;
12750Sstevel@tonic-gate 		}
12760Sstevel@tonic-gate 		bcopy(mp->b_cont->b_rptr, pap, len);
12770Sstevel@tonic-gate 		len = 0;
12780Sstevel@tonic-gate 		break;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	case PPPTUN_SPEER:
12810Sstevel@tonic-gate 		/* Client (device) side only; before SDATA */
12820Sstevel@tonic-gate 		if (tcl == NULL || mp->b_cont == NULL ||
12830Sstevel@tonic-gate 		    iop->ioc_count != sizeof (*ptp)) {
12840Sstevel@tonic-gate 			rc = EINVAL;
12850Sstevel@tonic-gate 			break;
12860Sstevel@tonic-gate 		}
12870Sstevel@tonic-gate 		if (tcl->tcl_data_tll != NULL) {
12880Sstevel@tonic-gate 			rc = EINVAL;
12890Sstevel@tonic-gate 			break;
12900Sstevel@tonic-gate 		}
12910Sstevel@tonic-gate 		ptp = (struct ppptun_peer *)mp->b_cont->b_rptr;
12925640Scarlsonj 		DTRACE_PROBE2(sppptun__speer, tuncl_t *, tcl,
12935640Scarlsonj 		    struct ppptun_peer *, ptp);
12940Sstevel@tonic-gate 		/* Once set, the style cannot change. */
12950Sstevel@tonic-gate 		if (tcl->tcl_style != PTS_NONE &&
12960Sstevel@tonic-gate 		    tcl->tcl_style != ptp->ptp_style) {
12970Sstevel@tonic-gate 			rc = EINVAL;
12980Sstevel@tonic-gate 			break;
12990Sstevel@tonic-gate 		}
13000Sstevel@tonic-gate 		if (ptp->ptp_flags & PTPF_DAEMON) {
13010Sstevel@tonic-gate 			/* User requests registration for tunnel 0 */
13020Sstevel@tonic-gate 			if ((tcl->tcl_flags & TCLF_SPEER_DONE) ||
13030Sstevel@tonic-gate 			    ptp->ptp_ltunid != 0 || ptp->ptp_rtunid != 0 ||
13040Sstevel@tonic-gate 			    ptp->ptp_lsessid != 0 || ptp->ptp_rsessid != 0) {
13050Sstevel@tonic-gate 				rc = EINVAL;
13060Sstevel@tonic-gate 				break;
13070Sstevel@tonic-gate 			}
13080Sstevel@tonic-gate 			tcl->tcl_flags |= TCLF_DAEMON;
13090Sstevel@tonic-gate 		} else {
13100Sstevel@tonic-gate 			/* Normal client connection */
13110Sstevel@tonic-gate 			if (tcl->tcl_flags & TCLF_DAEMON) {
13120Sstevel@tonic-gate 				rc = EINVAL;
13130Sstevel@tonic-gate 				break;
13140Sstevel@tonic-gate 			}
13150Sstevel@tonic-gate 			if (ptp->ptp_lsessid != 0 &&
13160Sstevel@tonic-gate 			    ptp->ptp_lsessid != tcl->tcl_lsessid) {
13170Sstevel@tonic-gate 				rc = EINVAL;
13180Sstevel@tonic-gate 				break;
13190Sstevel@tonic-gate 			}
13200Sstevel@tonic-gate 			/*
13210Sstevel@tonic-gate 			 * If we're reassigning the peer data, then
13220Sstevel@tonic-gate 			 * the previous assignment must have been for
13230Sstevel@tonic-gate 			 * a client control connection.  Check that.
13240Sstevel@tonic-gate 			 */
13250Sstevel@tonic-gate 			if ((tcl->tcl_flags & TCLF_SPEER_DONE) &&
13260Sstevel@tonic-gate 			    ((tcl->tcl_ltunid != 0 &&
13275640Scarlsonj 			    tcl->tcl_ltunid != ptp->ptp_ltunid) ||
13285640Scarlsonj 			    (tcl->tcl_rtunid != 0 &&
13295640Scarlsonj 			    tcl->tcl_rtunid != ptp->ptp_rtunid) ||
13305640Scarlsonj 			    (tcl->tcl_rsessid != 0 &&
13315640Scarlsonj 			    tcl->tcl_rsessid != ptp->ptp_rsessid))) {
13320Sstevel@tonic-gate 				rc = EINVAL;
13330Sstevel@tonic-gate 				break;
13340Sstevel@tonic-gate 			}
13350Sstevel@tonic-gate 			if ((tcl->tcl_ltunid = ptp->ptp_ltunid) == 0 &&
13360Sstevel@tonic-gate 			    tcl->tcl_style == PTS_L2FTP)
13370Sstevel@tonic-gate 				tcl->tcl_ltunid = ptp->ptp_lsessid;
13380Sstevel@tonic-gate 			tcl->tcl_rtunid = ptp->ptp_rtunid;
13390Sstevel@tonic-gate 			tcl->tcl_rsessid = ptp->ptp_rsessid;
13400Sstevel@tonic-gate 		}
13410Sstevel@tonic-gate 		tcl->tcl_flags |= TCLF_SPEER_DONE;
13420Sstevel@tonic-gate 		tcl->tcl_style = ptp->ptp_style;
13430Sstevel@tonic-gate 		tcl->tcl_address = ptp->ptp_address;
13440Sstevel@tonic-gate 		goto fill_in_peer;
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	case PPPTUN_GPEER:
13470Sstevel@tonic-gate 		/* Client (device) side only */
13480Sstevel@tonic-gate 		if (tcl == NULL) {
13490Sstevel@tonic-gate 			rc = EINVAL;
13500Sstevel@tonic-gate 			break;
13510Sstevel@tonic-gate 		}
13520Sstevel@tonic-gate 		if (mp->b_cont != NULL)
13530Sstevel@tonic-gate 			freemsg(mp->b_cont);
13540Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (*ptp), BPRI_HI);
13550Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
13560Sstevel@tonic-gate 			rc = ENOSR;
13570Sstevel@tonic-gate 			break;
13580Sstevel@tonic-gate 		}
13590Sstevel@tonic-gate 		ptp = (struct ppptun_peer *)mp->b_cont->b_rptr;
13600Sstevel@tonic-gate 	fill_in_peer:
13610Sstevel@tonic-gate 		ptp->ptp_style = tcl->tcl_style;
13620Sstevel@tonic-gate 		ptp->ptp_flags = (tcl->tcl_flags & TCLF_DAEMON) ? PTPF_DAEMON :
13630Sstevel@tonic-gate 		    0;
13640Sstevel@tonic-gate 		ptp->ptp_ltunid = tcl->tcl_ltunid;
13650Sstevel@tonic-gate 		ptp->ptp_rtunid = tcl->tcl_rtunid;
13660Sstevel@tonic-gate 		ptp->ptp_lsessid = tcl->tcl_lsessid;
13670Sstevel@tonic-gate 		ptp->ptp_rsessid = tcl->tcl_rsessid;
13680Sstevel@tonic-gate 		ptp->ptp_address = tcl->tcl_address;
13690Sstevel@tonic-gate 		len = sizeof (*ptp);
13700Sstevel@tonic-gate 		break;
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	case PPPTUN_SDATA:
13730Sstevel@tonic-gate 	case PPPTUN_SCTL:
13740Sstevel@tonic-gate 		/* Client (device) side only; must do SPEER first */
13750Sstevel@tonic-gate 		if (tcl == NULL || mp->b_cont == NULL ||
13760Sstevel@tonic-gate 		    iop->ioc_count != sizeof (*ptn) ||
13770Sstevel@tonic-gate 		    *mp->b_cont->b_rptr == '\0') {
13780Sstevel@tonic-gate 			rc = EINVAL;
13790Sstevel@tonic-gate 			break;
13800Sstevel@tonic-gate 		}
13810Sstevel@tonic-gate 		if (!(tcl->tcl_flags & TCLF_SPEER_DONE)) {
13820Sstevel@tonic-gate 			rc = EINVAL;
13830Sstevel@tonic-gate 			break;
13840Sstevel@tonic-gate 		}
13850Sstevel@tonic-gate 		ptn = (union ppptun_name *)mp->b_cont->b_rptr;
13860Sstevel@tonic-gate 		ptn->ptn_name[sizeof (ptn->ptn_name) - 1] = '\0';
1387*9751Sjames.d.carlson@sun.com 		tll = tll_lookup_on_name(ptn->ptn_name, tcl->tcl_zoneid);
13880Sstevel@tonic-gate 		if (tll == NULL) {
13890Sstevel@tonic-gate 			rc = ESRCH;
13900Sstevel@tonic-gate 			break;
13910Sstevel@tonic-gate 		}
13920Sstevel@tonic-gate 		if (tll->tll_style != tcl->tcl_style) {
13930Sstevel@tonic-gate 			rc = ENXIO;
13940Sstevel@tonic-gate 			break;
13950Sstevel@tonic-gate 		}
13960Sstevel@tonic-gate 		if (iop->ioc_cmd == PPPTUN_SDATA) {
13970Sstevel@tonic-gate 			if (tcl->tcl_data_tll != NULL) {
13980Sstevel@tonic-gate 				rc = EEXIST;
13990Sstevel@tonic-gate 				break;
14000Sstevel@tonic-gate 			}
14010Sstevel@tonic-gate 			/* server daemons cannot use regular data */
14020Sstevel@tonic-gate 			if (tcl->tcl_flags & TCLF_DAEMON) {
14030Sstevel@tonic-gate 				rc = EINVAL;
14040Sstevel@tonic-gate 				break;
14050Sstevel@tonic-gate 			}
14060Sstevel@tonic-gate 			tcl->tcl_data_tll = tll;
14070Sstevel@tonic-gate 		} else if (tcl->tcl_flags & TCLF_DAEMON) {
14080Sstevel@tonic-gate 			if (tll->tll_defcl != NULL && tll->tll_defcl != tcl) {
14090Sstevel@tonic-gate 				rc = EEXIST;
14100Sstevel@tonic-gate 				break;
14110Sstevel@tonic-gate 			}
14120Sstevel@tonic-gate 			tll->tll_defcl = tcl;
14130Sstevel@tonic-gate 			if (tcl->tcl_ctrl_tll != NULL) {
14140Sstevel@tonic-gate 				KDECR(tcl->tcl_ctrl_tll, tll_kstats,
14150Sstevel@tonic-gate 				    lks_clients);
14160Sstevel@tonic-gate 			}
14170Sstevel@tonic-gate 			tcl->tcl_ctrl_tll = tll;
14180Sstevel@tonic-gate 		} else {
14190Sstevel@tonic-gate 			if (tcl->tcl_ctrl_tll != NULL) {
14200Sstevel@tonic-gate 				rc = EEXIST;
14210Sstevel@tonic-gate 				break;
14220Sstevel@tonic-gate 			}
14230Sstevel@tonic-gate 			tcl->tcl_ctrl_tll = tll;
14240Sstevel@tonic-gate 		}
14250Sstevel@tonic-gate 		KLINCR(lks_clients);
14260Sstevel@tonic-gate 		break;
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	case PPPTUN_GDATA:
14290Sstevel@tonic-gate 	case PPPTUN_GCTL:
14300Sstevel@tonic-gate 		/* Client (device) side only */
14310Sstevel@tonic-gate 		if (tcl == NULL) {
14320Sstevel@tonic-gate 			rc = EINVAL;
14330Sstevel@tonic-gate 			break;
14340Sstevel@tonic-gate 		}
14350Sstevel@tonic-gate 		if (mp->b_cont != NULL)
14360Sstevel@tonic-gate 			freemsg(mp->b_cont);
14370Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (*ptn), BPRI_HI);
14380Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
14390Sstevel@tonic-gate 			rc = ENOSR;
14400Sstevel@tonic-gate 			break;
14410Sstevel@tonic-gate 		}
14420Sstevel@tonic-gate 		ptn = (union ppptun_name *)mp->b_cont->b_rptr;
14430Sstevel@tonic-gate 		if (iop->ioc_cmd == PPPTUN_GDATA)
14440Sstevel@tonic-gate 			tll = tcl->tcl_data_tll;
14450Sstevel@tonic-gate 		else
14460Sstevel@tonic-gate 			tll = tcl->tcl_ctrl_tll;
14470Sstevel@tonic-gate 		if (tll == NULL)
14480Sstevel@tonic-gate 			bzero(ptn, sizeof (*ptn));
14490Sstevel@tonic-gate 		else
14500Sstevel@tonic-gate 			bcopy(tll->tll_name, ptn->ptn_name,
14510Sstevel@tonic-gate 			    sizeof (ptn->ptn_name));
14520Sstevel@tonic-gate 		len = sizeof (*ptn);
14530Sstevel@tonic-gate 		break;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	case PPPTUN_DCTL:
14560Sstevel@tonic-gate 		/* Client (device) side daemon mode only */
14570Sstevel@tonic-gate 		if (tcl == NULL || mp->b_cont == NULL ||
14580Sstevel@tonic-gate 		    iop->ioc_count != sizeof (*ptn) ||
14590Sstevel@tonic-gate 		    !(tcl->tcl_flags & TCLF_DAEMON)) {
14600Sstevel@tonic-gate 			rc = EINVAL;
14610Sstevel@tonic-gate 			break;
14620Sstevel@tonic-gate 		}
14630Sstevel@tonic-gate 		ptn = (union ppptun_name *)mp->b_cont->b_rptr;
14640Sstevel@tonic-gate 		ptn->ptn_name[sizeof (ptn->ptn_name) - 1] = '\0';
1465*9751Sjames.d.carlson@sun.com 		tll = tll_lookup_on_name(ptn->ptn_name, tcl->tcl_zoneid);
14660Sstevel@tonic-gate 		if (tll == NULL || tll->tll_defcl != tcl) {
14670Sstevel@tonic-gate 			rc = ESRCH;
14680Sstevel@tonic-gate 			break;
14690Sstevel@tonic-gate 		}
14700Sstevel@tonic-gate 		tll->tll_defcl = NULL;
14710Sstevel@tonic-gate 		break;
14720Sstevel@tonic-gate 
1473*9751Sjames.d.carlson@sun.com 	case PPPTUN_SSAP:
1474*9751Sjames.d.carlson@sun.com 		/* This is done on the *module* (lower level) side. */
1475*9751Sjames.d.carlson@sun.com 		if (tll == NULL || mp->b_cont == NULL ||
1476*9751Sjames.d.carlson@sun.com 		    iop->ioc_count != sizeof (uint_t)) {
1477*9751Sjames.d.carlson@sun.com 			rc = EINVAL;
1478*9751Sjames.d.carlson@sun.com 			break;
1479*9751Sjames.d.carlson@sun.com 		}
1480*9751Sjames.d.carlson@sun.com 
1481*9751Sjames.d.carlson@sun.com 		tll->tll_sap = *(uint_t *)mp->b_cont->b_rptr;
1482*9751Sjames.d.carlson@sun.com 		break;
1483*9751Sjames.d.carlson@sun.com 
14840Sstevel@tonic-gate 	default:
14850Sstevel@tonic-gate 		/* Caller should already have checked command value */
14860Sstevel@tonic-gate 		ASSERT(0);
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate 	if (rc != 0) {
14890Sstevel@tonic-gate 		miocnak(q, mp, 0, rc);
14900Sstevel@tonic-gate 	} else {
14910Sstevel@tonic-gate 		if (len > 0)
14920Sstevel@tonic-gate 			mp->b_cont->b_wptr = mp->b_cont->b_rptr + len;
14930Sstevel@tonic-gate 		miocack(q, mp, len, 0);
14940Sstevel@tonic-gate 	}
14950Sstevel@tonic-gate }
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate /*
14980Sstevel@tonic-gate  * sppptun_ioctl()
14990Sstevel@tonic-gate  *
15000Sstevel@tonic-gate  * MT-Perimeters:
15010Sstevel@tonic-gate  *    shared inner, shared outer.
15020Sstevel@tonic-gate  *
15030Sstevel@tonic-gate  * Description:
15040Sstevel@tonic-gate  *    Called by sppptun_uwput as the result of receiving a M_IOCTL command.
15050Sstevel@tonic-gate  */
15060Sstevel@tonic-gate static void
sppptun_ioctl(queue_t * q,mblk_t * mp)15070Sstevel@tonic-gate sppptun_ioctl(queue_t *q, mblk_t *mp)
15080Sstevel@tonic-gate {
15090Sstevel@tonic-gate 	struct iocblk *iop;
15100Sstevel@tonic-gate 	int rc = 0;
15110Sstevel@tonic-gate 	int len = 0;
15120Sstevel@tonic-gate 	uint32_t val = 0;
15130Sstevel@tonic-gate 	tunll_t *tll;
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	iop = (struct iocblk *)mp->b_rptr;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	switch (iop->ioc_cmd) {
15180Sstevel@tonic-gate 	case PPPIO_DEBUG:
15190Sstevel@tonic-gate 	case PPPIO_GETSTAT:
15200Sstevel@tonic-gate 	case PPPIO_GETSTAT64:
15210Sstevel@tonic-gate 	case PPPTUN_SNAME:
15220Sstevel@tonic-gate 	case PPPTUN_SINFO:
15230Sstevel@tonic-gate 	case PPPTUN_GINFO:
15240Sstevel@tonic-gate 	case PPPTUN_GNNAME:
15250Sstevel@tonic-gate 	case PPPTUN_LCLADDR:
15260Sstevel@tonic-gate 	case PPPTUN_SPEER:
15270Sstevel@tonic-gate 	case PPPTUN_GPEER:
15280Sstevel@tonic-gate 	case PPPTUN_SDATA:
15290Sstevel@tonic-gate 	case PPPTUN_GDATA:
15300Sstevel@tonic-gate 	case PPPTUN_SCTL:
15310Sstevel@tonic-gate 	case PPPTUN_GCTL:
15320Sstevel@tonic-gate 	case PPPTUN_DCTL:
1533*9751Sjames.d.carlson@sun.com 	case PPPTUN_SSAP:
15340Sstevel@tonic-gate 		qwriter(q, mp, sppptun_inner_ioctl, PERIM_INNER);
15350Sstevel@tonic-gate 		return;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	case PPPIO_GCLEAN:	/* always clean */
15380Sstevel@tonic-gate 		val = RCV_B7_1 | RCV_B7_0 | RCV_ODDP | RCV_EVNP;
15390Sstevel@tonic-gate 		len = sizeof (uint32_t);
15400Sstevel@tonic-gate 		break;
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	case PPPIO_GTYPE:	/* we look like an async driver. */
15430Sstevel@tonic-gate 		val = PPPTYP_AHDLC;
15440Sstevel@tonic-gate 		len = sizeof (uint32_t);
15450Sstevel@tonic-gate 		break;
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	case PPPIO_CFLAGS:	/* never compress headers */
15480Sstevel@tonic-gate 		val = 0;
15490Sstevel@tonic-gate 		len = sizeof (uint32_t);
15500Sstevel@tonic-gate 		break;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 		/* quietly ack PPP things we don't need to do. */
15530Sstevel@tonic-gate 	case PPPIO_XFCS:
15540Sstevel@tonic-gate 	case PPPIO_RFCS:
15550Sstevel@tonic-gate 	case PPPIO_XACCM:
15560Sstevel@tonic-gate 	case PPPIO_RACCM:
15570Sstevel@tonic-gate 	case PPPIO_LASTMOD:
15580Sstevel@tonic-gate 	case PPPIO_MUX:
15590Sstevel@tonic-gate 	case I_PLINK:
15600Sstevel@tonic-gate 	case I_PUNLINK:
15610Sstevel@tonic-gate 	case I_LINK:
15620Sstevel@tonic-gate 	case I_UNLINK:
15630Sstevel@tonic-gate 		break;
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	default:
15660Sstevel@tonic-gate 		tll = (tunll_t *)q->q_ptr;
15670Sstevel@tonic-gate 		if (!(tll->tll_flags & TLLF_NOTLOWER)) {
15680Sstevel@tonic-gate 			/* module side; pass this through. */
15690Sstevel@tonic-gate 			putnext(q, mp);
15700Sstevel@tonic-gate 			return;
15710Sstevel@tonic-gate 		}
15720Sstevel@tonic-gate 		rc = EINVAL;
15730Sstevel@tonic-gate 		break;
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 	if (rc == 0 && len == sizeof (uint32_t)) {
15760Sstevel@tonic-gate 		if (mp->b_cont != NULL)
15770Sstevel@tonic-gate 			freemsg(mp->b_cont);
15780Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (uint32_t), BPRI_HI);
15790Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
15800Sstevel@tonic-gate 			rc = ENOSR;
15810Sstevel@tonic-gate 		} else {
15820Sstevel@tonic-gate 			*(uint32_t *)mp->b_cont->b_wptr = val;
15830Sstevel@tonic-gate 			mp->b_cont->b_wptr += sizeof (uint32_t);
15840Sstevel@tonic-gate 		}
15850Sstevel@tonic-gate 	}
15860Sstevel@tonic-gate 	if (rc == 0) {
15870Sstevel@tonic-gate 		miocack(q, mp, len, 0);
15880Sstevel@tonic-gate 	} else {
15890Sstevel@tonic-gate 		miocnak(q, mp, 0, rc);
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate }
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate /*
15940Sstevel@tonic-gate  * sppptun_inner_mctl()
15950Sstevel@tonic-gate  *
15960Sstevel@tonic-gate  * MT-Perimeters:
15970Sstevel@tonic-gate  *    exclusive inner, shared outer.
15980Sstevel@tonic-gate  *
15990Sstevel@tonic-gate  * Description:
16000Sstevel@tonic-gate  *    Called by qwriter (via sppptun_uwput) as the result of receiving
16010Sstevel@tonic-gate  *    an M_CTL.  Called only on the client (driver) side.
16020Sstevel@tonic-gate  */
16030Sstevel@tonic-gate static void
sppptun_inner_mctl(queue_t * q,mblk_t * mp)16040Sstevel@tonic-gate sppptun_inner_mctl(queue_t *q, mblk_t *mp)
16050Sstevel@tonic-gate {
16060Sstevel@tonic-gate 	int msglen;
16070Sstevel@tonic-gate 	tuncl_t *tcl;
16080Sstevel@tonic-gate 
16095640Scarlsonj 	tcl = q->q_ptr;
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	if (!(tcl->tcl_flags & TCLF_ISCLIENT)) {
16120Sstevel@tonic-gate 		freemsg(mp);
16130Sstevel@tonic-gate 		return;
16140Sstevel@tonic-gate 	}
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	msglen = MBLKL(mp);
16170Sstevel@tonic-gate 	switch (*mp->b_rptr) {
16180Sstevel@tonic-gate 	case PPPCTL_UNIT:
16190Sstevel@tonic-gate 		if (msglen == 2)
16200Sstevel@tonic-gate 			tcl->tcl_unit = mp->b_rptr[1];
16210Sstevel@tonic-gate 		else if (msglen == 8)
16220Sstevel@tonic-gate 			tcl->tcl_unit = ((uint32_t *)mp->b_rptr)[1];
16230Sstevel@tonic-gate 		break;
16240Sstevel@tonic-gate 	}
16250Sstevel@tonic-gate 	freemsg(mp);
16260Sstevel@tonic-gate }
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate /*
16290Sstevel@tonic-gate  * sppptun_uwput()
16300Sstevel@tonic-gate  *
16310Sstevel@tonic-gate  * MT-Perimeters:
16320Sstevel@tonic-gate  *    shared inner, shared outer.
16330Sstevel@tonic-gate  *
16340Sstevel@tonic-gate  * Description:
16350Sstevel@tonic-gate  *	Regular output data and controls pass through here.
16360Sstevel@tonic-gate  */
16370Sstevel@tonic-gate static void
sppptun_uwput(queue_t * q,mblk_t * mp)16380Sstevel@tonic-gate sppptun_uwput(queue_t *q, mblk_t *mp)
16390Sstevel@tonic-gate {
16400Sstevel@tonic-gate 	queue_t *nextq;
16410Sstevel@tonic-gate 	tuncl_t *tcl;
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	ASSERT(q->q_ptr != NULL);
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 	switch (MTYPE(mp)) {
16460Sstevel@tonic-gate 	case M_DATA:
16470Sstevel@tonic-gate 	case M_PROTO:
16480Sstevel@tonic-gate 	case M_PCPROTO:
16490Sstevel@tonic-gate 		if (q->q_first == NULL &&
16500Sstevel@tonic-gate 		    (nextq = sppptun_outpkt(q, &mp)) != NULL) {
16510Sstevel@tonic-gate 			putnext(nextq, mp);
16520Sstevel@tonic-gate 		} else if (mp != NULL && !putq(q, mp)) {
16530Sstevel@tonic-gate 			freemsg(mp);
16540Sstevel@tonic-gate 		}
16550Sstevel@tonic-gate 		break;
16560Sstevel@tonic-gate 	case M_IOCTL:
16570Sstevel@tonic-gate 		sppptun_ioctl(q, mp);
16580Sstevel@tonic-gate 		break;
16590Sstevel@tonic-gate 	case M_CTL:
16600Sstevel@tonic-gate 		qwriter(q, mp, sppptun_inner_mctl, PERIM_INNER);
16610Sstevel@tonic-gate 		break;
16620Sstevel@tonic-gate 	default:
16630Sstevel@tonic-gate 		tcl = (tuncl_t *)q->q_ptr;
16640Sstevel@tonic-gate 		/*
16650Sstevel@tonic-gate 		 * If we're the driver, then discard unknown junk.
16660Sstevel@tonic-gate 		 * Otherwise, if we're the module, then forward along.
16670Sstevel@tonic-gate 		 */
16680Sstevel@tonic-gate 		if (tcl->tcl_flags & TCLF_ISCLIENT)
16690Sstevel@tonic-gate 			freemsg(mp);
16700Sstevel@tonic-gate 		else
16710Sstevel@tonic-gate 			putnext(q, mp);
16720Sstevel@tonic-gate 		break;
16730Sstevel@tonic-gate 	}
16740Sstevel@tonic-gate }
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate /*
16770Sstevel@tonic-gate  * Send a DLPI/TPI control message to the driver but make sure there
16780Sstevel@tonic-gate  * is only one outstanding message.  Uses tll_msg_pending to tell when
16790Sstevel@tonic-gate  * it must queue.  sppptun_urput calls message_done() when an ACK or a
16800Sstevel@tonic-gate  * NAK is received to process the next queued message.
16810Sstevel@tonic-gate  */
16820Sstevel@tonic-gate static void
message_send(tunll_t * tll,mblk_t * mp)16830Sstevel@tonic-gate message_send(tunll_t *tll, mblk_t *mp)
16840Sstevel@tonic-gate {
16850Sstevel@tonic-gate 	mblk_t **mpp;
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	if (tll->tll_msg_pending) {
16880Sstevel@tonic-gate 		/* Must queue message. Tail insertion */
16890Sstevel@tonic-gate 		mpp = &tll->tll_msg_deferred;
16900Sstevel@tonic-gate 		while (*mpp != NULL)
16910Sstevel@tonic-gate 			mpp = &((*mpp)->b_next);
16920Sstevel@tonic-gate 		*mpp = mp;
16930Sstevel@tonic-gate 		return;
16940Sstevel@tonic-gate 	}
16950Sstevel@tonic-gate 	tll->tll_msg_pending = 1;
16960Sstevel@tonic-gate 	putnext(tll->tll_wq, mp);
16970Sstevel@tonic-gate }
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate /*
17000Sstevel@tonic-gate  * Called when an DLPI/TPI control message has been acked or nacked to
17010Sstevel@tonic-gate  * send down the next queued message (if any).
17020Sstevel@tonic-gate  */
17030Sstevel@tonic-gate static void
message_done(tunll_t * tll)17040Sstevel@tonic-gate message_done(tunll_t *tll)
17050Sstevel@tonic-gate {
17060Sstevel@tonic-gate 	mblk_t *mp;
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	ASSERT(tll->tll_msg_pending);
17090Sstevel@tonic-gate 	tll->tll_msg_pending = 0;
17100Sstevel@tonic-gate 	mp = tll->tll_msg_deferred;
17110Sstevel@tonic-gate 	if (mp != NULL) {
17120Sstevel@tonic-gate 		tll->tll_msg_deferred = mp->b_next;
17130Sstevel@tonic-gate 		mp->b_next = NULL;
17140Sstevel@tonic-gate 		tll->tll_msg_pending = 1;
17150Sstevel@tonic-gate 		putnext(tll->tll_wq, mp);
17160Sstevel@tonic-gate 	}
17170Sstevel@tonic-gate }
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate /*
17200Sstevel@tonic-gate  * Send down queued "close" messages to lower stream.  These were
17210Sstevel@tonic-gate  * enqueued right after the stream was originally allocated, when the
17220Sstevel@tonic-gate  * tll_style was set by PPPTUN_SINFO.
17230Sstevel@tonic-gate  */
17240Sstevel@tonic-gate static int
tll_close_req(tunll_t * tll)17250Sstevel@tonic-gate tll_close_req(tunll_t *tll)
17260Sstevel@tonic-gate {
17270Sstevel@tonic-gate 	mblk_t *mb, *mbnext;
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	if ((mb = tll->tll_onclose) == NULL)
17300Sstevel@tonic-gate 		tll->tll_flags |= TLLF_SHUTDOWN_DONE;
17310Sstevel@tonic-gate 	else {
17320Sstevel@tonic-gate 		tll->tll_onclose = NULL;
17330Sstevel@tonic-gate 		while (mb != NULL) {
17340Sstevel@tonic-gate 			mbnext = mb->b_next;
17350Sstevel@tonic-gate 			mb->b_next = NULL;
17360Sstevel@tonic-gate 			message_send(tll, mb);
17370Sstevel@tonic-gate 			mb = mbnext;
17380Sstevel@tonic-gate 		}
17390Sstevel@tonic-gate 	}
17400Sstevel@tonic-gate 	return (0);
17410Sstevel@tonic-gate }
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate /*
17445640Scarlsonj  * This function is called when a backenable occurs on the write side of a
17455640Scarlsonj  * lower stream.  It walks over the client streams, looking for ones that use
17465640Scarlsonj  * the given tunll_t lower stream.  Each client is then backenabled.
17475640Scarlsonj  */
17485640Scarlsonj static void
tclvm_backenable(void * arg,void * firstv,size_t numv)17495640Scarlsonj tclvm_backenable(void *arg, void *firstv, size_t numv)
17505640Scarlsonj {
17515640Scarlsonj 	tunll_t *tll = arg;
17525640Scarlsonj 	int minorn = (int)(uintptr_t)firstv;
17535640Scarlsonj 	int minormax = minorn + numv;
17545640Scarlsonj 	tuncl_t *tcl;
17555640Scarlsonj 	queue_t *q;
17565640Scarlsonj 
17575640Scarlsonj 	while (minorn < minormax) {
17585640Scarlsonj 		tcl = tcl_slots[minorn - 1];
17595640Scarlsonj 		if ((tcl->tcl_data_tll == tll ||
17605640Scarlsonj 		    tcl->tcl_ctrl_tll == tll) &&
17615640Scarlsonj 		    (q = tcl->tcl_rq) != NULL) {
17625640Scarlsonj 			qenable(OTHERQ(q));
17635640Scarlsonj 		}
17645640Scarlsonj 		minorn++;
17655640Scarlsonj 	}
17665640Scarlsonj }
17675640Scarlsonj 
17685640Scarlsonj /*
17690Sstevel@tonic-gate  * sppptun_uwsrv()
17700Sstevel@tonic-gate  *
17710Sstevel@tonic-gate  * MT-Perimeters:
17720Sstevel@tonic-gate  *    exclusive inner, shared outer.
17730Sstevel@tonic-gate  *
17740Sstevel@tonic-gate  * Description:
17750Sstevel@tonic-gate  *    Upper write-side service procedure.  In addition to the usual
17760Sstevel@tonic-gate  *    STREAMS queue service handling, this routine also handles the
17770Sstevel@tonic-gate  *    transmission of the unbind/detach messages to the lower stream
17780Sstevel@tonic-gate  *    driver when a lower stream is being closed.  (See the use of
17790Sstevel@tonic-gate  *    qenable/qwait in sppptun_close().)
17800Sstevel@tonic-gate  */
17810Sstevel@tonic-gate static int
sppptun_uwsrv(queue_t * q)17820Sstevel@tonic-gate sppptun_uwsrv(queue_t *q)
17830Sstevel@tonic-gate {
17840Sstevel@tonic-gate 	tuncl_t	*tcl;
17850Sstevel@tonic-gate 	mblk_t *mp;
17860Sstevel@tonic-gate 	queue_t *nextq;
17870Sstevel@tonic-gate 
17885640Scarlsonj 	tcl = q->q_ptr;
17890Sstevel@tonic-gate 	if (!(tcl->tcl_flags & TCLF_ISCLIENT)) {
17900Sstevel@tonic-gate 		tunll_t *tll = (tunll_t *)tcl;
17915640Scarlsonj 
17920Sstevel@tonic-gate 		if ((tll->tll_flags & (TLLF_CLOSING|TLLF_CLOSE_DONE)) ==
17930Sstevel@tonic-gate 		    TLLF_CLOSING) {
17940Sstevel@tonic-gate 			tll->tll_error = tll_close_req(tll);
17950Sstevel@tonic-gate 			tll->tll_flags |= TLLF_CLOSE_DONE;
17965640Scarlsonj 		} else {
17975640Scarlsonj 			/*
17985640Scarlsonj 			 * We've been enabled here because of a backenable on
17995640Scarlsonj 			 * output flow control.  Backenable clients using this
18005640Scarlsonj 			 * lower layer.
18015640Scarlsonj 			 */
18025640Scarlsonj 			vmem_walk(tcl_minor_arena, VMEM_ALLOC, tclvm_backenable,
18035640Scarlsonj 			    tll);
18040Sstevel@tonic-gate 		}
18050Sstevel@tonic-gate 		return (0);
18060Sstevel@tonic-gate 	}
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
18090Sstevel@tonic-gate 		if ((nextq = sppptun_outpkt(q, &mp)) != NULL) {
18100Sstevel@tonic-gate 			putnext(nextq, mp);
18110Sstevel@tonic-gate 		} else if (mp != NULL) {
18120Sstevel@tonic-gate 			(void) putbq(q, mp);
18130Sstevel@tonic-gate 			break;
18140Sstevel@tonic-gate 		}
18150Sstevel@tonic-gate 	}
18160Sstevel@tonic-gate 	return (0);
18170Sstevel@tonic-gate }
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate /*
18200Sstevel@tonic-gate  * sppptun_lwput()
18210Sstevel@tonic-gate  *
18220Sstevel@tonic-gate  * MT-Perimeters:
18230Sstevel@tonic-gate  *    shared inner, shared outer.
18240Sstevel@tonic-gate  *
18250Sstevel@tonic-gate  * Description:
18260Sstevel@tonic-gate  *    Lower write-side put procedure.  Nothing should be sending
18270Sstevel@tonic-gate  *    packets down this stream.
18280Sstevel@tonic-gate  */
18290Sstevel@tonic-gate static void
sppptun_lwput(queue_t * q,mblk_t * mp)18300Sstevel@tonic-gate sppptun_lwput(queue_t *q, mblk_t *mp)
18310Sstevel@tonic-gate {
18320Sstevel@tonic-gate 	switch (MTYPE(mp)) {
18330Sstevel@tonic-gate 	case M_PROTO:
18340Sstevel@tonic-gate 		putnext(q, mp);
18350Sstevel@tonic-gate 		break;
18360Sstevel@tonic-gate 	default:
18370Sstevel@tonic-gate 		freemsg(mp);
18380Sstevel@tonic-gate 		break;
18390Sstevel@tonic-gate 	}
18400Sstevel@tonic-gate }
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate /*
18430Sstevel@tonic-gate  * sppptun_lrput()
18440Sstevel@tonic-gate  *
18450Sstevel@tonic-gate  * MT-Perimeters:
18460Sstevel@tonic-gate  *    shared inner, shared outer.
18470Sstevel@tonic-gate  *
18480Sstevel@tonic-gate  * Description:
18490Sstevel@tonic-gate  *    Lower read-side put procedure.  Nothing should arrive here.
18500Sstevel@tonic-gate  */
18510Sstevel@tonic-gate static void
sppptun_lrput(queue_t * q,mblk_t * mp)18520Sstevel@tonic-gate sppptun_lrput(queue_t *q, mblk_t *mp)
18530Sstevel@tonic-gate {
18540Sstevel@tonic-gate 	tuncl_t *tcl;
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	switch (MTYPE(mp)) {
18570Sstevel@tonic-gate 	case M_IOCTL:
18580Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
18590Sstevel@tonic-gate 		return;
18600Sstevel@tonic-gate 	case M_FLUSH:
18610Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
18620Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
18630Sstevel@tonic-gate 		}
18640Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
18650Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;
18660Sstevel@tonic-gate 			qreply(q, mp);
18670Sstevel@tonic-gate 		} else {
18680Sstevel@tonic-gate 			freemsg(mp);
18690Sstevel@tonic-gate 		}
18700Sstevel@tonic-gate 		return;
18710Sstevel@tonic-gate 	}
18720Sstevel@tonic-gate 	/*
18730Sstevel@tonic-gate 	 * Try to forward the message to the put procedure for the upper
18740Sstevel@tonic-gate 	 * control stream for this lower stream. If there are already messages
18750Sstevel@tonic-gate 	 * queued here, queue this one up to preserve message ordering.
18760Sstevel@tonic-gate 	 */
18770Sstevel@tonic-gate 	if ((tcl = (tuncl_t *)q->q_ptr) == NULL || tcl->tcl_rq == NULL) {
18780Sstevel@tonic-gate 		freemsg(mp);
18790Sstevel@tonic-gate 		return;
18800Sstevel@tonic-gate 	}
18810Sstevel@tonic-gate 	if (queclass(mp) == QPCTL ||
18820Sstevel@tonic-gate 	    (q->q_first == NULL && canput(tcl->tcl_rq))) {
18830Sstevel@tonic-gate 		put(tcl->tcl_rq, mp);
18840Sstevel@tonic-gate 	} else {
18850Sstevel@tonic-gate 		if (!putq(q, mp))
18860Sstevel@tonic-gate 			freemsg(mp);
18870Sstevel@tonic-gate 	}
18880Sstevel@tonic-gate }
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate /*
18910Sstevel@tonic-gate  * MT-Perimeters:
18920Sstevel@tonic-gate  *    shared inner, shared outer.
18930Sstevel@tonic-gate  *
18940Sstevel@tonic-gate  *    Handle non-data DLPI messages.  Used with PPPoE, which runs over
18950Sstevel@tonic-gate  *    Ethernet only.
18960Sstevel@tonic-gate  */
18970Sstevel@tonic-gate static void
urput_dlpi(queue_t * q,mblk_t * mp)18980Sstevel@tonic-gate urput_dlpi(queue_t *q, mblk_t *mp)
18990Sstevel@tonic-gate {
19000Sstevel@tonic-gate 	int err;
19010Sstevel@tonic-gate 	union DL_primitives *dlp = (union DL_primitives *)mp->b_rptr;
19025640Scarlsonj 	tunll_t *tll = q->q_ptr;
19035640Scarlsonj 	size_t mlen = MBLKL(mp);
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	switch (dlp->dl_primitive) {
19060Sstevel@tonic-gate 	case DL_UDERROR_IND:
19070Sstevel@tonic-gate 		break;
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	case DL_ERROR_ACK:
19105640Scarlsonj 		if (mlen < DL_ERROR_ACK_SIZE)
19115640Scarlsonj 			break;
19120Sstevel@tonic-gate 		err = dlp->error_ack.dl_unix_errno ?
19130Sstevel@tonic-gate 		    dlp->error_ack.dl_unix_errno : ENXIO;
19140Sstevel@tonic-gate 		switch (dlp->error_ack.dl_error_primitive) {
19150Sstevel@tonic-gate 		case DL_UNBIND_REQ:
19160Sstevel@tonic-gate 			message_done(tll);
19170Sstevel@tonic-gate 			break;
19180Sstevel@tonic-gate 		case DL_DETACH_REQ:
19190Sstevel@tonic-gate 			message_done(tll);
19200Sstevel@tonic-gate 			tll->tll_error = err;
19210Sstevel@tonic-gate 			tll->tll_flags |= TLLF_SHUTDOWN_DONE;
19220Sstevel@tonic-gate 			break;
19230Sstevel@tonic-gate 		case DL_PHYS_ADDR_REQ:
19240Sstevel@tonic-gate 			message_done(tll);
19250Sstevel@tonic-gate 			break;
19260Sstevel@tonic-gate 		case DL_INFO_REQ:
19270Sstevel@tonic-gate 		case DL_ATTACH_REQ:
19280Sstevel@tonic-gate 		case DL_BIND_REQ:
19290Sstevel@tonic-gate 			message_done(tll);
19300Sstevel@tonic-gate 			tll->tll_error = err;
19310Sstevel@tonic-gate 			break;
19320Sstevel@tonic-gate 		}
19330Sstevel@tonic-gate 		break;
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 	case DL_INFO_ACK:
19360Sstevel@tonic-gate 		message_done(tll);
19370Sstevel@tonic-gate 		break;
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	case DL_BIND_ACK:
19400Sstevel@tonic-gate 		message_done(tll);
19410Sstevel@tonic-gate 		break;
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 	case DL_PHYS_ADDR_ACK:
19440Sstevel@tonic-gate 		break;
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	case DL_OK_ACK:
19475640Scarlsonj 		if (mlen < DL_OK_ACK_SIZE)
19485640Scarlsonj 			break;
19490Sstevel@tonic-gate 		switch (dlp->ok_ack.dl_correct_primitive) {
19500Sstevel@tonic-gate 		case DL_UNBIND_REQ:
19510Sstevel@tonic-gate 			message_done(tll);
19520Sstevel@tonic-gate 			break;
19530Sstevel@tonic-gate 		case DL_DETACH_REQ:
19540Sstevel@tonic-gate 			tll->tll_flags |= TLLF_SHUTDOWN_DONE;
19550Sstevel@tonic-gate 			break;
19560Sstevel@tonic-gate 		case DL_ATTACH_REQ:
19570Sstevel@tonic-gate 			message_done(tll);
19580Sstevel@tonic-gate 			break;
19590Sstevel@tonic-gate 		}
19600Sstevel@tonic-gate 		break;
19610Sstevel@tonic-gate 	}
19620Sstevel@tonic-gate 	freemsg(mp);
19630Sstevel@tonic-gate }
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate /* Search structure used with PPPoE only; see tclvm_pppoe_search(). */
19660Sstevel@tonic-gate struct poedat {
19670Sstevel@tonic-gate 	uint_t sessid;
19680Sstevel@tonic-gate 	tunll_t *tll;
19695640Scarlsonj 	const void *srcaddr;
19700Sstevel@tonic-gate 	int isdata;
19710Sstevel@tonic-gate 	tuncl_t *tcl;
19720Sstevel@tonic-gate };
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate /*
19750Sstevel@tonic-gate  * This function is called by vmem_walk from within sppptun_recv.  It
19760Sstevel@tonic-gate  * iterates over a span of allocated minor node numbers to search for
19770Sstevel@tonic-gate  * the appropriate lower stream, session ID, and peer MAC address.
19780Sstevel@tonic-gate  *
19790Sstevel@tonic-gate  * (This is necessary due to a design flaw in the PPPoE protocol
19800Sstevel@tonic-gate  * itself.  The protocol assigns session IDs from the server side
19810Sstevel@tonic-gate  * only.  Both server and client use the same number.  Thus, if there
19820Sstevel@tonic-gate  * are multiple clients on a single host, there can be session ID
19830Sstevel@tonic-gate  * conflicts between servers and there's no way to detangle them
19840Sstevel@tonic-gate  * except by looking at the remote MAC address.)
19850Sstevel@tonic-gate  *
19860Sstevel@tonic-gate  * (This could have been handled by linking together sessions that
19870Sstevel@tonic-gate  * differ only in the remote MAC address.  This isn't done because it
19880Sstevel@tonic-gate  * would involve extra per-session storage and it's very unlikely that
19890Sstevel@tonic-gate  * PPPoE would be used this way.)
19900Sstevel@tonic-gate  */
19910Sstevel@tonic-gate static void
tclvm_pppoe_search(void * arg,void * firstv,size_t numv)19920Sstevel@tonic-gate tclvm_pppoe_search(void *arg, void *firstv, size_t numv)
19930Sstevel@tonic-gate {
19940Sstevel@tonic-gate 	struct poedat *poedat = (struct poedat *)arg;
19950Sstevel@tonic-gate 	int minorn = (int)(uintptr_t)firstv;
19960Sstevel@tonic-gate 	int minormax = minorn + numv;
19970Sstevel@tonic-gate 	tuncl_t *tcl;
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	if (poedat->tcl != NULL)
20000Sstevel@tonic-gate 		return;
20010Sstevel@tonic-gate 	while (minorn < minormax) {
20020Sstevel@tonic-gate 		tcl = tcl_slots[minorn - 1];
20030Sstevel@tonic-gate 		ASSERT(tcl != NULL);
20040Sstevel@tonic-gate 		if (tcl->tcl_rsessid == poedat->sessid &&
20050Sstevel@tonic-gate 		    ((!poedat->isdata && tcl->tcl_ctrl_tll == poedat->tll) ||
20065640Scarlsonj 		    (poedat->isdata && tcl->tcl_data_tll == poedat->tll)) &&
20070Sstevel@tonic-gate 		    bcmp(tcl->tcl_address.pta_pppoe.ptma_mac,
20085640Scarlsonj 		    poedat->srcaddr,
20095640Scarlsonj 		    sizeof (tcl->tcl_address.pta_pppoe.ptma_mac)) == 0) {
20100Sstevel@tonic-gate 			poedat->tcl = tcl;
20110Sstevel@tonic-gate 			break;
20120Sstevel@tonic-gate 		}
20130Sstevel@tonic-gate 		minorn++;
20140Sstevel@tonic-gate 	}
20150Sstevel@tonic-gate }
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate /*
20180Sstevel@tonic-gate  * sppptun_recv()
20190Sstevel@tonic-gate  *
20200Sstevel@tonic-gate  * MT-Perimeters:
20210Sstevel@tonic-gate  *    shared inner, shared outer.
20220Sstevel@tonic-gate  *
20230Sstevel@tonic-gate  * Description:
20240Sstevel@tonic-gate  *    Receive function called by sppptun_urput, which is called when
20250Sstevel@tonic-gate  *    the lower read-side put or service procedure sends a message
20260Sstevel@tonic-gate  *    upstream to the a device user (PPP).  It attempts to find an
20270Sstevel@tonic-gate  *    appropriate queue on the module above us (depending on what the
20280Sstevel@tonic-gate  *    associated upper stream for the protocol would be), and if not
20290Sstevel@tonic-gate  *    possible, it will find an upper control stream for the protocol.
20300Sstevel@tonic-gate  *    Returns a pointer to the upper queue_t, or NULL if the message
20310Sstevel@tonic-gate  *    has been discarded.
20320Sstevel@tonic-gate  *
20330Sstevel@tonic-gate  * About demultiplexing:
20340Sstevel@tonic-gate  *
20350Sstevel@tonic-gate  *	All four protocols (L2F, PPTP, L2TP, and PPPoE) support a
20360Sstevel@tonic-gate  *	locally assigned ID for demultiplexing incoming traffic.  For
20370Sstevel@tonic-gate  *	L2F, this is called the Client ID, for PPTP the Call ID, for
20380Sstevel@tonic-gate  *	L2TP the Session ID, and for PPPoE the SESSION_ID.  This is a
20390Sstevel@tonic-gate  *	16 bit number for all four protocols, and is used to directly
20400Sstevel@tonic-gate  *	index into a list of upper streams.  With the upper stream in
20410Sstevel@tonic-gate  *	hand, we verify that this is the right stream and deliver the
20420Sstevel@tonic-gate  *	data.
20430Sstevel@tonic-gate  *
20440Sstevel@tonic-gate  *	L2TP has a Tunnel ID, which represents a bundle of PPP
20450Sstevel@tonic-gate  *	sessions between the peers.  Because we always assign unique
20460Sstevel@tonic-gate  *	session ID numbers, we merely check that the given ID matches
20470Sstevel@tonic-gate  *	the assigned ID for the upper stream.
20480Sstevel@tonic-gate  *
20490Sstevel@tonic-gate  *	L2F has a Multiplex ID, which is unique per connection.  It
20500Sstevel@tonic-gate  *	does not have L2TP's concept of multiple-connections-within-
20510Sstevel@tonic-gate  *	a-tunnel.  The same checking is done.
20520Sstevel@tonic-gate  *
20530Sstevel@tonic-gate  *	PPPoE is a horribly broken protocol.  Only one ID is assigned
20540Sstevel@tonic-gate  *	per connection.  The client must somehow demultiplex based on
20550Sstevel@tonic-gate  *	an ID number assigned by the server.  It's not necessarily
20560Sstevel@tonic-gate  *	unique.  The search is done based on {ID,peerEthernet} (using
20570Sstevel@tonic-gate  *	tcl_rsessid) for all packet types except PADI and PADS.
20580Sstevel@tonic-gate  *
20590Sstevel@tonic-gate  *	Neither PPPoE nor PPTP supports additional ID numbers.
20600Sstevel@tonic-gate  *
20610Sstevel@tonic-gate  *	Both L2F and L2TP come in over UDP.  They are distinguished by
20620Sstevel@tonic-gate  *	looking at the GRE version field -- 001 for L2F and 010 for
20630Sstevel@tonic-gate  *	L2TP.
20640Sstevel@tonic-gate  */
20650Sstevel@tonic-gate static queue_t *
sppptun_recv(queue_t * q,mblk_t ** mpp,const void * srcaddr)20665640Scarlsonj sppptun_recv(queue_t *q, mblk_t **mpp, const void *srcaddr)
20670Sstevel@tonic-gate {
20680Sstevel@tonic-gate 	mblk_t *mp;
20690Sstevel@tonic-gate 	tunll_t *tll;
20700Sstevel@tonic-gate 	tuncl_t *tcl;
20710Sstevel@tonic-gate 	int sessid;
20720Sstevel@tonic-gate 	int remlen;
20730Sstevel@tonic-gate 	int msglen;
20740Sstevel@tonic-gate 	int isdata;
20750Sstevel@tonic-gate 	int i;
20765640Scarlsonj 	const uchar_t *ucp;
20775640Scarlsonj 	const poep_t *poep;
20780Sstevel@tonic-gate 	mblk_t *mnew;
20790Sstevel@tonic-gate 	ppptun_atype *pap;
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	mp = *mpp;
20820Sstevel@tonic-gate 
20835640Scarlsonj 	tll = q->q_ptr;
20840Sstevel@tonic-gate 	ASSERT(!(tll->tll_flags & TLLF_NOTLOWER));
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	tcl = NULL;
20870Sstevel@tonic-gate 	switch (tll->tll_style) {
20880Sstevel@tonic-gate 	case PTS_PPPOE:
20895640Scarlsonj 		/* Note that poep_t alignment is uint16_t */
20905640Scarlsonj 		if ((!IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)) ||
20915640Scarlsonj 		    MBLKL(mp) < sizeof (poep_t)) &&
20925640Scarlsonj 		    !pullupmsg(mp, sizeof (poep_t)))
20935640Scarlsonj 			break;
20945640Scarlsonj 		poep = (const poep_t *)mp->b_rptr;
20950Sstevel@tonic-gate 		if (poep->poep_version_type != POE_VERSION)
20960Sstevel@tonic-gate 			break;
20975640Scarlsonj 		/*
20985640Scarlsonj 		 * First, extract a session ID number.  All protocols have
20995640Scarlsonj 		 * this.
21005640Scarlsonj 		 */
21010Sstevel@tonic-gate 		isdata = (poep->poep_code == POECODE_DATA);
21020Sstevel@tonic-gate 		sessid = ntohs(poep->poep_session_id);
21030Sstevel@tonic-gate 		remlen = sizeof (*poep);
21040Sstevel@tonic-gate 		msglen = ntohs(poep->poep_length);
21050Sstevel@tonic-gate 		i = poep->poep_code;
21060Sstevel@tonic-gate 		if (i == POECODE_PADI || i == POECODE_PADR) {
21070Sstevel@tonic-gate 			/* These go to the server daemon only. */
21080Sstevel@tonic-gate 			tcl = tll->tll_defcl;
21090Sstevel@tonic-gate 		} else if (i == POECODE_PADO || i == POECODE_PADS) {
21100Sstevel@tonic-gate 			/*
21110Sstevel@tonic-gate 			 * These go to a client only, and are demuxed
21120Sstevel@tonic-gate 			 * by the Host-Uniq field (into which we stuff
21130Sstevel@tonic-gate 			 * our local ID number when generating
21140Sstevel@tonic-gate 			 * PADI/PADR).
21150Sstevel@tonic-gate 			 */
21165640Scarlsonj 			ucp = (const uchar_t *)(poep + 1);
21170Sstevel@tonic-gate 			i = msglen;
21180Sstevel@tonic-gate 			while (i > POET_HDRLEN) {
21190Sstevel@tonic-gate 				if (POET_GET_TYPE(ucp) == POETT_END) {
21200Sstevel@tonic-gate 					i = 0;
21210Sstevel@tonic-gate 					break;
21220Sstevel@tonic-gate 				}
21230Sstevel@tonic-gate 				if (POET_GET_TYPE(ucp) == POETT_UNIQ &&
21240Sstevel@tonic-gate 				    POET_GET_LENG(ucp) >= sizeof (uint32_t))
21250Sstevel@tonic-gate 					break;
21260Sstevel@tonic-gate 				i -= POET_GET_LENG(ucp) + POET_HDRLEN;
21270Sstevel@tonic-gate 				ucp = POET_NEXT(ucp);
21280Sstevel@tonic-gate 			}
21290Sstevel@tonic-gate 			if (i >= POET_HDRLEN + 4)
21300Sstevel@tonic-gate 				sessid = GETLONG(ucp + POET_HDRLEN);
21310Sstevel@tonic-gate 			tcl = tcl_by_minor((minor_t)sessid);
21320Sstevel@tonic-gate 		} else {
21330Sstevel@tonic-gate 			/*
21340Sstevel@tonic-gate 			 * Try minor number as session ID first, since
21350Sstevel@tonic-gate 			 * it's used that way on server side.  It's
21360Sstevel@tonic-gate 			 * not used that way on the client, though, so
21370Sstevel@tonic-gate 			 * this might not work.  If this isn't the
21380Sstevel@tonic-gate 			 * right one, then try the tll cache.  If
21390Sstevel@tonic-gate 			 * neither is right, then search all open
21400Sstevel@tonic-gate 			 * clients.  Did I mention that the PPPoE
21410Sstevel@tonic-gate 			 * protocol is badly designed?
21420Sstevel@tonic-gate 			 */
21430Sstevel@tonic-gate 			tcl = tcl_by_minor((minor_t)sessid);
21440Sstevel@tonic-gate 			if (tcl == NULL ||
21450Sstevel@tonic-gate 			    (!isdata && tcl->tcl_ctrl_tll != tll) ||
21460Sstevel@tonic-gate 			    (isdata && tcl->tcl_data_tll != tll) ||
21470Sstevel@tonic-gate 			    sessid != tcl->tcl_rsessid ||
21480Sstevel@tonic-gate 			    bcmp(srcaddr, tcl->tcl_address.pta_pppoe.ptma_mac,
21495640Scarlsonj 			    sizeof (tcl->tcl_address.pta_pppoe.ptma_mac)) != 0)
21500Sstevel@tonic-gate 				tcl = tll->tll_lastcl;
21510Sstevel@tonic-gate 			if (tcl == NULL ||
21520Sstevel@tonic-gate 			    (!isdata && tcl->tcl_ctrl_tll != tll) ||
21530Sstevel@tonic-gate 			    (isdata && tcl->tcl_data_tll != tll) ||
21540Sstevel@tonic-gate 			    sessid != tcl->tcl_rsessid ||
21550Sstevel@tonic-gate 			    bcmp(srcaddr, tcl->tcl_address.pta_pppoe.ptma_mac,
21565640Scarlsonj 			    sizeof (tcl->tcl_address.pta_pppoe.ptma_mac)) != 0)
21570Sstevel@tonic-gate 				tcl = NULL;
21580Sstevel@tonic-gate 			if (tcl == NULL && sessid != 0) {
21590Sstevel@tonic-gate 				struct poedat poedat;
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 				/*
21620Sstevel@tonic-gate 				 * Slow mode.  Too bad.  If you don't like it,
21630Sstevel@tonic-gate 				 * you can always choose a better protocol.
21640Sstevel@tonic-gate 				 */
21650Sstevel@tonic-gate 				poedat.sessid = sessid;
21660Sstevel@tonic-gate 				poedat.tll = tll;
21670Sstevel@tonic-gate 				poedat.srcaddr = srcaddr;
21680Sstevel@tonic-gate 				poedat.tcl = NULL;
21690Sstevel@tonic-gate 				poedat.isdata = isdata;
21700Sstevel@tonic-gate 				vmem_walk(tcl_minor_arena, VMEM_ALLOC,
21710Sstevel@tonic-gate 				    tclvm_pppoe_search, &poedat);
21720Sstevel@tonic-gate 				KLINCR(lks_walks);
21730Sstevel@tonic-gate 				if ((tcl = poedat.tcl) != NULL) {
21740Sstevel@tonic-gate 					tll->tll_lastcl = tcl;
21750Sstevel@tonic-gate 					KCINCR(cks_walks);
21760Sstevel@tonic-gate 				}
21770Sstevel@tonic-gate 			}
21780Sstevel@tonic-gate 		}
21790Sstevel@tonic-gate 		break;
21800Sstevel@tonic-gate 	}
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 	if (tcl == NULL || tcl->tcl_rq == NULL) {
21835640Scarlsonj 		DTRACE_PROBE3(sppptun__recv__discard, int, sessid,
21845640Scarlsonj 		    tuncl_t *, tcl, mblk_t *, mp);
21850Sstevel@tonic-gate 		if (tcl == NULL) {
21860Sstevel@tonic-gate 			KLINCR(lks_in_nomatch);
21870Sstevel@tonic-gate 		}
21880Sstevel@tonic-gate 		if (isdata) {
21890Sstevel@tonic-gate 			KLINCR(lks_indata_drops);
21900Sstevel@tonic-gate 			if (tcl != NULL)
21910Sstevel@tonic-gate 				tcl->tcl_stats.ppp_ierrors++;
21920Sstevel@tonic-gate 		} else {
21930Sstevel@tonic-gate 			KLINCR(lks_inctrl_drops);
21940Sstevel@tonic-gate 			if (tcl != NULL) {
21950Sstevel@tonic-gate 				KCINCR(cks_inctrl_drops);
21960Sstevel@tonic-gate 			}
21970Sstevel@tonic-gate 		}
21980Sstevel@tonic-gate 		freemsg(mp);
21990Sstevel@tonic-gate 		return (NULL);
22000Sstevel@tonic-gate 	}
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	if (tcl->tcl_data_tll == tll && isdata) {
22030Sstevel@tonic-gate 		if (!adjmsg(mp, remlen) ||
22040Sstevel@tonic-gate 		    (i = msgsize(mp)) < msglen ||
22050Sstevel@tonic-gate 		    (i > msglen && !adjmsg(mp, msglen - i))) {
22060Sstevel@tonic-gate 			KLINCR(lks_indata_drops);
22070Sstevel@tonic-gate 			tcl->tcl_stats.ppp_ierrors++;
22080Sstevel@tonic-gate 			freemsg(mp);
22090Sstevel@tonic-gate 			return (NULL);
22100Sstevel@tonic-gate 		}
22110Sstevel@tonic-gate 		/* XXX -- address/control handling in pppd needs help. */
22120Sstevel@tonic-gate 		if (*mp->b_rptr != 0xFF) {
22130Sstevel@tonic-gate 			if ((mp = prependb(mp, 2, 1)) == NULL) {
22140Sstevel@tonic-gate 				KLINCR(lks_indata_drops);
22150Sstevel@tonic-gate 				tcl->tcl_stats.ppp_ierrors++;
22160Sstevel@tonic-gate 				return (NULL);
22170Sstevel@tonic-gate 			}
22180Sstevel@tonic-gate 			mp->b_rptr[0] = 0xFF;
22190Sstevel@tonic-gate 			mp->b_rptr[1] = 0x03;
22200Sstevel@tonic-gate 		}
22210Sstevel@tonic-gate 		MTYPE(mp) = M_DATA;
22220Sstevel@tonic-gate 		tcl->tcl_stats.ppp_ibytes += msgsize(mp);
22230Sstevel@tonic-gate 		tcl->tcl_stats.ppp_ipackets++;
22240Sstevel@tonic-gate 		KLINCR(lks_indata);
22250Sstevel@tonic-gate 	} else {
22260Sstevel@tonic-gate 		if (isdata || tcl->tcl_ctrl_tll != tll ||
22270Sstevel@tonic-gate 		    (mnew = make_control(tcl, tll, PTCA_CONTROL, tcl)) ==
22280Sstevel@tonic-gate 		    NULL) {
22290Sstevel@tonic-gate 			KLINCR(lks_inctrl_drops);
22300Sstevel@tonic-gate 			KCINCR(cks_inctrl_drops);
22310Sstevel@tonic-gate 			freemsg(mp);
22320Sstevel@tonic-gate 			return (NULL);
22330Sstevel@tonic-gate 		}
22340Sstevel@tonic-gate 		/* Fix up source address; peer might not be set yet. */
22350Sstevel@tonic-gate 		pap = &((struct ppptun_control *)mnew->b_rptr)->ptc_address;
22360Sstevel@tonic-gate 		bcopy(srcaddr, pap->pta_pppoe.ptma_mac,
22370Sstevel@tonic-gate 		    sizeof (pap->pta_pppoe.ptma_mac));
22380Sstevel@tonic-gate 		mnew->b_cont = mp;
22390Sstevel@tonic-gate 		mp = mnew;
22400Sstevel@tonic-gate 		KLINCR(lks_inctrls);
22410Sstevel@tonic-gate 		KCINCR(cks_inctrls);
22420Sstevel@tonic-gate 	}
22430Sstevel@tonic-gate 	*mpp = mp;
22440Sstevel@tonic-gate 	return (tcl->tcl_rq);
22450Sstevel@tonic-gate }
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate /*
22480Sstevel@tonic-gate  * sppptun_urput()
22490Sstevel@tonic-gate  *
22500Sstevel@tonic-gate  * MT-Perimeters:
22510Sstevel@tonic-gate  *    shared inner, shared outer.
22520Sstevel@tonic-gate  *
22530Sstevel@tonic-gate  * Description:
22540Sstevel@tonic-gate  *    Upper read-side put procedure.  Messages from the underlying
22550Sstevel@tonic-gate  *    lower stream driver arrive here.  See sppptun_recv for the
22560Sstevel@tonic-gate  *    demultiplexing logic.
22570Sstevel@tonic-gate  */
22580Sstevel@tonic-gate static void
sppptun_urput(queue_t * q,mblk_t * mp)22590Sstevel@tonic-gate sppptun_urput(queue_t *q, mblk_t *mp)
22600Sstevel@tonic-gate {
22610Sstevel@tonic-gate 	union DL_primitives *dlprim;
22620Sstevel@tonic-gate 	mblk_t *mpnext;
22630Sstevel@tonic-gate 	tunll_t *tll;
22640Sstevel@tonic-gate 	queue_t *nextq;
22650Sstevel@tonic-gate 
22665640Scarlsonj 	tll = q->q_ptr;
22670Sstevel@tonic-gate 	ASSERT(!(tll->tll_flags & TLLF_NOTLOWER));
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	switch (MTYPE(mp)) {
22700Sstevel@tonic-gate 	case M_DATA:
22710Sstevel@tonic-gate 		/*
22720Sstevel@tonic-gate 		 * When we're bound over IP, data arrives here.  The
22730Sstevel@tonic-gate 		 * packet starts with the IP header itself.
22740Sstevel@tonic-gate 		 */
22755640Scarlsonj 		if ((nextq = sppptun_recv(q, &mp, NULL)) != NULL)
22760Sstevel@tonic-gate 			putnext(nextq, mp);
22770Sstevel@tonic-gate 		break;
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 	case M_PROTO:
22800Sstevel@tonic-gate 	case M_PCPROTO:
22810Sstevel@tonic-gate 		/* Data arrives here for UDP or raw Ethernet, not IP. */
22820Sstevel@tonic-gate 		switch (tll->tll_style) {
22830Sstevel@tonic-gate 			/* PPTP control messages are over TCP only. */
22840Sstevel@tonic-gate 		case PTS_PPTP:
22850Sstevel@tonic-gate 		default:
22860Sstevel@tonic-gate 			ASSERT(0);	/* how'd that happen? */
22870Sstevel@tonic-gate 			break;
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 		case PTS_PPPOE:		/* DLPI message */
22905640Scarlsonj 			if (MBLKL(mp) < sizeof (t_uscalar_t))
22915640Scarlsonj 				break;
22920Sstevel@tonic-gate 			dlprim = (union DL_primitives *)mp->b_rptr;
22930Sstevel@tonic-gate 			switch (dlprim->dl_primitive) {
22945640Scarlsonj 			case DL_UNITDATA_IND: {
22955640Scarlsonj 				size_t mlen = MBLKL(mp);
22965640Scarlsonj 
22975640Scarlsonj 				if (mlen < DL_UNITDATA_IND_SIZE)
22985640Scarlsonj 					break;
22995640Scarlsonj 				if (dlprim->unitdata_ind.dl_src_addr_offset <
23005640Scarlsonj 				    DL_UNITDATA_IND_SIZE ||
23015640Scarlsonj 				    dlprim->unitdata_ind.dl_src_addr_offset +
23025640Scarlsonj 				    dlprim->unitdata_ind.dl_src_addr_length >
23035640Scarlsonj 				    mlen)
23045640Scarlsonj 					break;
23055640Scarlsonj 			}
23065640Scarlsonj 				/* FALLTHROUGH */
23075640Scarlsonj 			case DL_UNITDATA_REQ:	/* For loopback support. */
23085640Scarlsonj 				if (dlprim->dl_primitive == DL_UNITDATA_REQ &&
23095640Scarlsonj 				    MBLKL(mp) < DL_UNITDATA_REQ_SIZE)
23105640Scarlsonj 					break;
23115640Scarlsonj 				if ((mpnext = mp->b_cont) == NULL)
23125640Scarlsonj 					break;
23130Sstevel@tonic-gate 				MTYPE(mpnext) = M_DATA;
23140Sstevel@tonic-gate 				nextq = sppptun_recv(q, &mpnext,
23155640Scarlsonj 				    dlprim->dl_primitive == DL_UNITDATA_IND ?
23160Sstevel@tonic-gate 				    mp->b_rptr +
23175640Scarlsonj 				    dlprim->unitdata_ind.dl_src_addr_offset :
23180Sstevel@tonic-gate 				    tll->tll_lcladdr.pta_pppoe.ptma_mac);
23190Sstevel@tonic-gate 				if (nextq != NULL)
23200Sstevel@tonic-gate 					putnext(nextq, mpnext);
23210Sstevel@tonic-gate 				freeb(mp);
23225640Scarlsonj 				return;
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 			default:
23250Sstevel@tonic-gate 				urput_dlpi(q, mp);
23265640Scarlsonj 				return;
23270Sstevel@tonic-gate 			}
23280Sstevel@tonic-gate 			break;
23290Sstevel@tonic-gate 		}
23305640Scarlsonj 		freemsg(mp);
23310Sstevel@tonic-gate 		break;
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	default:
23340Sstevel@tonic-gate 		freemsg(mp);
23350Sstevel@tonic-gate 		break;
23360Sstevel@tonic-gate 	}
23370Sstevel@tonic-gate }
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate /*
23400Sstevel@tonic-gate  * sppptun_ursrv()
23410Sstevel@tonic-gate  *
23420Sstevel@tonic-gate  * MT-Perimeters:
23430Sstevel@tonic-gate  *    exclusive inner, shared outer.
23440Sstevel@tonic-gate  *
23450Sstevel@tonic-gate  * Description:
23460Sstevel@tonic-gate  *    Upper read-side service procedure.  This procedure services the
23470Sstevel@tonic-gate  *    client streams.  We get here because the client (PPP) asserts
23480Sstevel@tonic-gate  *    flow control down to us.
23490Sstevel@tonic-gate  */
23500Sstevel@tonic-gate static int
sppptun_ursrv(queue_t * q)23510Sstevel@tonic-gate sppptun_ursrv(queue_t *q)
23520Sstevel@tonic-gate {
23530Sstevel@tonic-gate 	mblk_t		*mp;
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate 	ASSERT(q->q_ptr != NULL);
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
23580Sstevel@tonic-gate 		if (canputnext(q)) {
23590Sstevel@tonic-gate 			putnext(q, mp);
23600Sstevel@tonic-gate 		} else {
23610Sstevel@tonic-gate 			(void) putbq(q, mp);
23620Sstevel@tonic-gate 			break;
23630Sstevel@tonic-gate 		}
23640Sstevel@tonic-gate 	}
23650Sstevel@tonic-gate 	return (0);
23660Sstevel@tonic-gate }
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate /*
23690Sstevel@tonic-gate  * Dummy constructor/destructor functions for kmem_cache_create.
23700Sstevel@tonic-gate  * We're just using kmem as an allocator of integers, not real
23710Sstevel@tonic-gate  * storage.
23720Sstevel@tonic-gate  */
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate /*ARGSUSED*/
23750Sstevel@tonic-gate static int
tcl_constructor(void * maddr,void * arg,int kmflags)23760Sstevel@tonic-gate tcl_constructor(void *maddr, void *arg, int kmflags)
23770Sstevel@tonic-gate {
23780Sstevel@tonic-gate 	return (0);
23790Sstevel@tonic-gate }
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate /*ARGSUSED*/
23820Sstevel@tonic-gate static void
tcl_destructor(void * maddr,void * arg)23830Sstevel@tonic-gate tcl_destructor(void *maddr, void *arg)
23840Sstevel@tonic-gate {
23850Sstevel@tonic-gate }
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate /*
23880Sstevel@tonic-gate  * Total size occupied by one tunnel client.  Each tunnel client
23890Sstevel@tonic-gate  * consumes one pointer for tcl_slots array, one tuncl_t structure and
23900Sstevel@tonic-gate  * two messages preallocated for close.
23910Sstevel@tonic-gate  */
23920Sstevel@tonic-gate #define	TUNCL_SIZE (sizeof (tuncl_t) + sizeof (tuncl_t *) + \
23930Sstevel@tonic-gate 			2 * sizeof (dblk_t))
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate /*
23960Sstevel@tonic-gate  * Clear all bits of x except the highest bit
23970Sstevel@tonic-gate  */
23980Sstevel@tonic-gate #define	truncate(x) 	((x) <= 2 ? (x) : (1 << (highbit(x) - 1)))
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate /*
24010Sstevel@tonic-gate  * This function initializes some well-known global variables inside
24020Sstevel@tonic-gate  * the module.
24030Sstevel@tonic-gate  *
24040Sstevel@tonic-gate  * Called by sppptun_mod.c:_init() before installing the module.
24050Sstevel@tonic-gate  */
24060Sstevel@tonic-gate void
sppptun_init(void)24070Sstevel@tonic-gate sppptun_init(void)
24080Sstevel@tonic-gate {
24090Sstevel@tonic-gate 	tunll_list.q_forw = tunll_list.q_back = &tunll_list;
24100Sstevel@tonic-gate }
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate /*
24130Sstevel@tonic-gate  * This function allocates the initial internal storage for the
24140Sstevel@tonic-gate  * sppptun driver.
24150Sstevel@tonic-gate  *
24160Sstevel@tonic-gate  * Called by sppptun_mod.c:_init() after installing module.
24170Sstevel@tonic-gate  */
24180Sstevel@tonic-gate void
sppptun_tcl_init(void)24190Sstevel@tonic-gate sppptun_tcl_init(void)
24200Sstevel@tonic-gate {
24210Sstevel@tonic-gate 	uint_t i, j;
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	rw_init(&tcl_rwlock, NULL, RW_DRIVER, NULL);
24240Sstevel@tonic-gate 	rw_enter(&tcl_rwlock, RW_WRITER);
24250Sstevel@tonic-gate 	tcl_nslots = sppptun_init_cnt;
24260Sstevel@tonic-gate 	tcl_slots = kmem_zalloc(tcl_nslots * sizeof (tuncl_t *), KM_SLEEP);
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 	tcl_cache = kmem_cache_create("sppptun_map", sizeof (tuncl_t), 0,
24290Sstevel@tonic-gate 	    tcl_constructor, tcl_destructor, NULL, NULL, NULL, 0);
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 	/* Allocate integer space for minor numbers */
24320Sstevel@tonic-gate 	tcl_minor_arena = vmem_create("sppptun_minor", (void *)1, tcl_nslots,
24330Sstevel@tonic-gate 	    1, NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
24340Sstevel@tonic-gate 
24350Sstevel@tonic-gate 	/*
24360Sstevel@tonic-gate 	 * Calculate available number of tunnels - how many tunnels
24370Sstevel@tonic-gate 	 * can we allocate in sppptun_pctofmem % of available
24380Sstevel@tonic-gate 	 * memory.  The value is rounded up to the nearest power of 2.
24390Sstevel@tonic-gate 	 */
24400Sstevel@tonic-gate 	i = (sppptun_pctofmem * kmem_maxavail()) / (100 * TUNCL_SIZE);
24410Sstevel@tonic-gate 	j = truncate(i);	/* i with non-high bits stripped */
24420Sstevel@tonic-gate 	if (i != j)
24430Sstevel@tonic-gate 		j *= 2;
24440Sstevel@tonic-gate 	tcl_minormax = j;
24450Sstevel@tonic-gate 	rw_exit(&tcl_rwlock);
24460Sstevel@tonic-gate }
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate /*
24490Sstevel@tonic-gate  * This function checks that there are no plumbed streams or other users.
24500Sstevel@tonic-gate  *
24510Sstevel@tonic-gate  * Called by sppptun_mod.c:_fini().  Assumes that we're exclusive on
24520Sstevel@tonic-gate  * both perimeters.
24530Sstevel@tonic-gate  */
24540Sstevel@tonic-gate int
sppptun_tcl_fintest(void)24550Sstevel@tonic-gate sppptun_tcl_fintest(void)
24560Sstevel@tonic-gate {
24575640Scarlsonj 	if (tunll_list.q_forw != &tunll_list || tcl_inuse > 0)
24580Sstevel@tonic-gate 		return (EBUSY);
24595640Scarlsonj 	else
24605640Scarlsonj 		return (0);
24610Sstevel@tonic-gate }
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate /*
24640Sstevel@tonic-gate  * If no lower streams are plumbed, then this function deallocates all
24650Sstevel@tonic-gate  * internal storage in preparation for unload.
24660Sstevel@tonic-gate  *
24670Sstevel@tonic-gate  * Called by sppptun_mod.c:_fini().  Assumes that we're exclusive on
24680Sstevel@tonic-gate  * both perimeters.
24690Sstevel@tonic-gate  */
24700Sstevel@tonic-gate void
sppptun_tcl_fini(void)24710Sstevel@tonic-gate sppptun_tcl_fini(void)
24720Sstevel@tonic-gate {
24730Sstevel@tonic-gate 	if (tcl_minor_arena != NULL) {
24740Sstevel@tonic-gate 		vmem_destroy(tcl_minor_arena);
24750Sstevel@tonic-gate 		tcl_minor_arena = NULL;
24760Sstevel@tonic-gate 	}
24770Sstevel@tonic-gate 	if (tcl_cache != NULL) {
24780Sstevel@tonic-gate 		kmem_cache_destroy(tcl_cache);
24790Sstevel@tonic-gate 		tcl_cache = NULL;
24800Sstevel@tonic-gate 	}
24810Sstevel@tonic-gate 	kmem_free(tcl_slots, tcl_nslots * sizeof (tuncl_t *));
24820Sstevel@tonic-gate 	tcl_slots = NULL;
24830Sstevel@tonic-gate 	rw_destroy(&tcl_rwlock);
24840Sstevel@tonic-gate 	ASSERT(tcl_slots == NULL);
24850Sstevel@tonic-gate 	ASSERT(tcl_cache == NULL);
24860Sstevel@tonic-gate 	ASSERT(tcl_minor_arena == NULL);
24870Sstevel@tonic-gate }
2488