xref: /onnv-gate/usr/src/uts/common/io/ppp/sppptun/sppptun.c (revision 8483:9f75fcc64d65)
1*8483Sjames.d.carlson@sun.com /*
2*8483Sjames.d.carlson@sun.com  * CDDL HEADER START
3*8483Sjames.d.carlson@sun.com  *
4*8483Sjames.d.carlson@sun.com  * The contents of this file are subject to the terms of the
5*8483Sjames.d.carlson@sun.com  * Common Development and Distribution License (the "License").
6*8483Sjames.d.carlson@sun.com  * You may not use this file except in compliance with the License.
7*8483Sjames.d.carlson@sun.com  *
8*8483Sjames.d.carlson@sun.com  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*8483Sjames.d.carlson@sun.com  * or http://www.opensolaris.org/os/licensing.
10*8483Sjames.d.carlson@sun.com  * See the License for the specific language governing permissions
11*8483Sjames.d.carlson@sun.com  * and limitations under the License.
12*8483Sjames.d.carlson@sun.com  *
13*8483Sjames.d.carlson@sun.com  * When distributing Covered Code, include this CDDL HEADER in each
14*8483Sjames.d.carlson@sun.com  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*8483Sjames.d.carlson@sun.com  * If applicable, add the following below this CDDL HEADER, with the
16*8483Sjames.d.carlson@sun.com  * fields enclosed by brackets "[]" replaced with your own identifying
17*8483Sjames.d.carlson@sun.com  * information: Portions Copyright [yyyy] [name of copyright owner]
18*8483Sjames.d.carlson@sun.com  *
19*8483Sjames.d.carlson@sun.com  * CDDL HEADER END
20*8483Sjames.d.carlson@sun.com  */
21*8483Sjames.d.carlson@sun.com 
220Sstevel@tonic-gate /*
237656SSherry.Moore@Sun.COM  * Copyright 2008 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
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 *
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
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 *
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 *
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
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 */
4510Sstevel@tonic-gate 		if (secpolicy_net_config(credp, B_FALSE) != 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);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 		/* Insert at end of list */
4610Sstevel@tonic-gate 		insque(&tll->tll_next, tunll_list.q_back);
4620Sstevel@tonic-gate 		q->q_ptr = WR(q)->q_ptr = tll;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 		tll->tll_style = PTS_PPPOE;
4650Sstevel@tonic-gate 		tll->tll_alen = sizeof (tll->tll_lcladdr.pta_pppoe);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		tll->tll_ksp = kstat_setup((kstat_named_t *)&tll->tll_kstats,
4680Sstevel@tonic-gate 		    tll_kstats_list, Dim(tll_kstats_list), "tll",
4690Sstevel@tonic-gate 		    tll->tll_index);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 		/*
4720Sstevel@tonic-gate 		 * Find the name of the driver somewhere beneath us.
4730Sstevel@tonic-gate 		 * Note that we have no driver under us until after
4740Sstevel@tonic-gate 		 * qprocson().
4750Sstevel@tonic-gate 		 */
4760Sstevel@tonic-gate 		qprocson(q);
4770Sstevel@tonic-gate 		for (q = WR(q); q->q_next != NULL; q = q->q_next)
4780Sstevel@tonic-gate 			;
4790Sstevel@tonic-gate 		cp = NULL;
4800Sstevel@tonic-gate 		if (q->q_qinfo != NULL && q->q_qinfo->qi_minfo != NULL)
4810Sstevel@tonic-gate 			cp = q->q_qinfo->qi_minfo->mi_idname;
4820Sstevel@tonic-gate 		if (cp != NULL && *cp == '\0')
4830Sstevel@tonic-gate 			cp = NULL;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 		/* Set initial name; user should overwrite. */
4860Sstevel@tonic-gate 		if (cp == NULL)
4870Sstevel@tonic-gate 			(void) snprintf(tll->tll_name, sizeof (tll->tll_name),
4880Sstevel@tonic-gate 			    PPP_TUN_NAME "%d", tll->tll_index);
4890Sstevel@tonic-gate 		else
4900Sstevel@tonic-gate 			(void) snprintf(tll->tll_name, sizeof (tll->tll_name),
4910Sstevel@tonic-gate 			    "%s:tun%d", cp, tll->tll_index);
4920Sstevel@tonic-gate 	} else {
4930Sstevel@tonic-gate 		tuncl_t	*tcl;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 		ASSERT(devp != NULL);
4960Sstevel@tonic-gate 		if (sflag & CLONEOPEN) {
4970Sstevel@tonic-gate 			tcl = tuncl_alloc(-1);
4980Sstevel@tonic-gate 		} else {
4990Sstevel@tonic-gate 			minor_t mn;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 			/*
5020Sstevel@tonic-gate 			 * Support of non-clone open (ie, mknod with
5030Sstevel@tonic-gate 			 * defined minor number) is supported for
5040Sstevel@tonic-gate 			 * testing purposes so that 'arbitrary' minor
5050Sstevel@tonic-gate 			 * numbers can be used.
5060Sstevel@tonic-gate 			 */
5070Sstevel@tonic-gate 			mn = getminor(*devp);
5080Sstevel@tonic-gate 			if (mn == 0 || (tcl = tcl_by_minor(mn)) != NULL) {
5090Sstevel@tonic-gate 				return (EPERM);
5100Sstevel@tonic-gate 			}
5110Sstevel@tonic-gate 			tcl = tuncl_alloc(mn);
5120Sstevel@tonic-gate 		}
5130Sstevel@tonic-gate 		if (tcl == NULL)
5140Sstevel@tonic-gate 			return (ENOSR);
5150Sstevel@tonic-gate 		tcl->tcl_rq = q;		/* save read queue pointer */
5160Sstevel@tonic-gate 		tcl->tcl_flags |= TCLF_ISCLIENT;	/* sanity check */
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		q->q_ptr = WR(q)->q_ptr = (caddr_t)tcl;
5190Sstevel@tonic-gate 		*devp = makedevice(getmajor(*devp), tcl->tcl_lsessid);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 		tcl->tcl_ksp = kstat_setup((kstat_named_t *)&tcl->tcl_kstats,
5220Sstevel@tonic-gate 		    tcl_kstats_list, Dim(tcl_kstats_list), "tcl",
5230Sstevel@tonic-gate 		    tcl->tcl_lsessid);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		qprocson(q);
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 	return (0);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate /*
5310Sstevel@tonic-gate  * Create an appropriate control message for this client event.
5320Sstevel@tonic-gate  */
5330Sstevel@tonic-gate static mblk_t *
5340Sstevel@tonic-gate make_control(tuncl_t *tclabout, tunll_t *tllabout, int action, tuncl_t *tclto)
5350Sstevel@tonic-gate {
5360Sstevel@tonic-gate 	struct ppptun_control *ptc;
5370Sstevel@tonic-gate 	mblk_t *mp = allocb(sizeof (*ptc), BPRI_HI);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	if (mp != NULL) {
5400Sstevel@tonic-gate 		MTYPE(mp) = M_PROTO;
5410Sstevel@tonic-gate 		ptc = (struct ppptun_control *)mp->b_wptr;
5420Sstevel@tonic-gate 		mp->b_wptr += sizeof (*ptc);
5430Sstevel@tonic-gate 		if (tclabout != NULL) {
5440Sstevel@tonic-gate 			ptc->ptc_rsessid = tclabout->tcl_rsessid;
5450Sstevel@tonic-gate 			ptc->ptc_address = tclabout->tcl_address;
5460Sstevel@tonic-gate 		} else {
5470Sstevel@tonic-gate 			bzero(ptc, sizeof (*ptc));
5480Sstevel@tonic-gate 		}
5490Sstevel@tonic-gate 		ptc->ptc_discrim = tclto->tcl_ctlval;
5500Sstevel@tonic-gate 		ptc->ptc_action = action;
5510Sstevel@tonic-gate 		(void) strncpy(ptc->ptc_name, tllabout->tll_name,
5520Sstevel@tonic-gate 		    sizeof (ptc->ptc_name));
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 	return (mp);
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate /*
5580Sstevel@tonic-gate  * Send an appropriate control message up this client session.
5590Sstevel@tonic-gate  */
5600Sstevel@tonic-gate static void
5610Sstevel@tonic-gate send_control(tuncl_t *tclabout, tunll_t *tllabout, int action, tuncl_t *tcl)
5620Sstevel@tonic-gate {
5630Sstevel@tonic-gate 	mblk_t *mp;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	if (tcl->tcl_rq != NULL) {
5660Sstevel@tonic-gate 		mp = make_control(tclabout, tllabout, action, tcl);
5670Sstevel@tonic-gate 		if (mp != NULL) {
5680Sstevel@tonic-gate 			KCINCR(cks_octrl_spec);
5690Sstevel@tonic-gate 			putnext(tcl->tcl_rq, mp);
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate /*
5750Sstevel@tonic-gate  * If a lower stream is being unplumbed, then the upper streams
5760Sstevel@tonic-gate  * connected to this lower stream must be disconnected.  This routine
5770Sstevel@tonic-gate  * accomplishes this by sending M_HANGUP to data streams and M_PROTO
5780Sstevel@tonic-gate  * messages to control streams.  This is called by vmem_walk, and
5790Sstevel@tonic-gate  * handles a span of minor node numbers.
5800Sstevel@tonic-gate  *
5810Sstevel@tonic-gate  * No need to update lks_clients here; the lower stream is on its way
5820Sstevel@tonic-gate  * out.
5830Sstevel@tonic-gate  */
5840Sstevel@tonic-gate static void
5850Sstevel@tonic-gate tclvm_remove_tll(void *arg, void *firstv, size_t numv)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate 	tunll_t *tll = (tunll_t *)arg;
5880Sstevel@tonic-gate 	int minorn = (int)(uintptr_t)firstv;
5890Sstevel@tonic-gate 	int minormax = minorn + numv;
5900Sstevel@tonic-gate 	tuncl_t *tcl;
5910Sstevel@tonic-gate 	mblk_t *mp;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	while (minorn < minormax) {
5940Sstevel@tonic-gate 		tcl = tcl_slots[minorn - 1];
5950Sstevel@tonic-gate 		ASSERT(tcl != NULL);
5960Sstevel@tonic-gate 		if (tcl->tcl_data_tll == tll && tcl->tcl_rq != NULL) {
5970Sstevel@tonic-gate 			tcl->tcl_data_tll = NULL;
5980Sstevel@tonic-gate 			mp = allocb(0, BPRI_HI);
5990Sstevel@tonic-gate 			if (mp != NULL) {
6000Sstevel@tonic-gate 				MTYPE(mp) = M_HANGUP;
6010Sstevel@tonic-gate 				putnext(tcl->tcl_rq, mp);
6020Sstevel@tonic-gate 				if (tcl->tcl_ctrl_tll == tll)
6030Sstevel@tonic-gate 					tcl->tcl_ctrl_tll = NULL;
6040Sstevel@tonic-gate 			}
6050Sstevel@tonic-gate 		}
6060Sstevel@tonic-gate 		if (tcl->tcl_ctrl_tll == tll) {
6070Sstevel@tonic-gate 			send_control(tcl, tll, PTCA_UNPLUMB, tcl);
6080Sstevel@tonic-gate 			tcl->tcl_ctrl_tll = NULL;
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 		minorn++;
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate /*
6150Sstevel@tonic-gate  * sppptun_close()
6160Sstevel@tonic-gate  *
6170Sstevel@tonic-gate  * MT-Perimeters:
6180Sstevel@tonic-gate  *    exclusive inner, exclusive outer.
6190Sstevel@tonic-gate  *
6200Sstevel@tonic-gate  * Description:
6210Sstevel@tonic-gate  *    Common close procedure for module and driver.
6220Sstevel@tonic-gate  */
6230Sstevel@tonic-gate static int
6240Sstevel@tonic-gate sppptun_close(queue_t *q)
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate 	int err;
6270Sstevel@tonic-gate 	void *qptr;
6280Sstevel@tonic-gate 	tunll_t *tll;
6290Sstevel@tonic-gate 	tuncl_t *tcl;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	qptr = q->q_ptr;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	err = 0;
6345640Scarlsonj 	tll = qptr;
6350Sstevel@tonic-gate 	if (!(tll->tll_flags & TLLF_NOTLOWER)) {
6360Sstevel@tonic-gate 		/* q_next is set on modules */
6370Sstevel@tonic-gate 		ASSERT(WR(q)->q_next != NULL);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 		/* unlink any clients using this lower layer. */
6400Sstevel@tonic-gate 		vmem_walk(tcl_minor_arena, VMEM_ALLOC, tclvm_remove_tll, tll);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		/* tell daemon that this has been removed. */
6430Sstevel@tonic-gate 		if ((tcl = tll->tll_defcl) != NULL)
6440Sstevel@tonic-gate 			send_control(NULL, tll, PTCA_UNPLUMB, tcl);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 		tll->tll_flags |= TLLF_CLOSING;
6470Sstevel@tonic-gate 		while (!(tll->tll_flags & TLLF_CLOSE_DONE)) {
6480Sstevel@tonic-gate 			qenable(tll->tll_wq);
6490Sstevel@tonic-gate 			qwait(tll->tll_wq);
6500Sstevel@tonic-gate 		}
6510Sstevel@tonic-gate 		tll->tll_error = 0;
6520Sstevel@tonic-gate 		while (!(tll->tll_flags & TLLF_SHUTDOWN_DONE)) {
6530Sstevel@tonic-gate 			if (!qwait_sig(tll->tll_wq))
6540Sstevel@tonic-gate 				break;
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 		qprocsoff(q);
6580Sstevel@tonic-gate 		q->q_ptr = WR(q)->q_ptr = NULL;
6590Sstevel@tonic-gate 		tll->tll_wq = NULL;
6600Sstevel@tonic-gate 		remque(&tll->tll_next);
6610Sstevel@tonic-gate 		err = tll->tll_error;
6620Sstevel@tonic-gate 		if (tll->tll_ksp != NULL)
6630Sstevel@tonic-gate 			kstat_delete(tll->tll_ksp);
6640Sstevel@tonic-gate 		kmem_free(tll, sizeof (*tll));
6650Sstevel@tonic-gate 	} else {
6665640Scarlsonj 		tcl = qptr;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		/* devices are end of line; no q_next. */
6690Sstevel@tonic-gate 		ASSERT(WR(q)->q_next == NULL);
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 		qprocsoff(q);
6725640Scarlsonj 		DTRACE_PROBE1(sppptun__client__close, tuncl_t *, tcl);
6730Sstevel@tonic-gate 		tcl->tcl_rq = NULL;
6740Sstevel@tonic-gate 		q->q_ptr = WR(q)->q_ptr = NULL;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		tll = TO_TLL(tunll_list.q_forw);
6770Sstevel@tonic-gate 		while (tll != TO_TLL(&tunll_list)) {
6780Sstevel@tonic-gate 			if (tll->tll_defcl == tcl)
6790Sstevel@tonic-gate 				tll->tll_defcl = NULL;
6800Sstevel@tonic-gate 			if (tll->tll_lastcl == tcl)
6810Sstevel@tonic-gate 				tll->tll_lastcl = NULL;
6820Sstevel@tonic-gate 			tll = TO_TLL(tll->tll_next);
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 		/*
6850Sstevel@tonic-gate 		 * If this was a normal session, then tell the daemon.
6860Sstevel@tonic-gate 		 */
6870Sstevel@tonic-gate 		if (!(tcl->tcl_flags & TCLF_DAEMON) &&
6880Sstevel@tonic-gate 		    (tll = tcl->tcl_ctrl_tll) != NULL &&
6890Sstevel@tonic-gate 		    tll->tll_defcl != NULL) {
6900Sstevel@tonic-gate 			send_control(tcl, tll, PTCA_DISCONNECT,
6910Sstevel@tonic-gate 			    tll->tll_defcl);
6920Sstevel@tonic-gate 		}
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		/* Update statistics for references being dropped. */
6950Sstevel@tonic-gate 		if ((tll = tcl->tcl_data_tll) != NULL) {
6960Sstevel@tonic-gate 			KLDECR(lks_clients);
6970Sstevel@tonic-gate 		}
6980Sstevel@tonic-gate 		if ((tll = tcl->tcl_ctrl_tll) != NULL) {
6990Sstevel@tonic-gate 			KLDECR(lks_clients);
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 		tuncl_free(tcl);
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	return (err);
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate /*
7090Sstevel@tonic-gate  * Allocate and initialize a DLPI or TPI template of the specified
7100Sstevel@tonic-gate  * length.
7110Sstevel@tonic-gate  */
7120Sstevel@tonic-gate static mblk_t *
7130Sstevel@tonic-gate pi_alloc(size_t len, int prim)
7140Sstevel@tonic-gate {
7150Sstevel@tonic-gate 	mblk_t	*mp;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	mp = allocb(len, BPRI_MED);
7180Sstevel@tonic-gate 	if (mp != NULL) {
7190Sstevel@tonic-gate 		MTYPE(mp) = M_PROTO;
7200Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + len;
7210Sstevel@tonic-gate 		bzero(mp->b_rptr, len);
7220Sstevel@tonic-gate 		*(int *)mp->b_rptr = prim;
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 	return (mp);
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate #define	dlpi_alloc(l, p)	pi_alloc((l), (p))
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /*
7300Sstevel@tonic-gate  * Prepend some room to an mblk.  Try to reuse the existing buffer, if
7310Sstevel@tonic-gate  * at all possible, rather than allocating a new one.  (Fast-path
7320Sstevel@tonic-gate  * output should be able to use this.)
7330Sstevel@tonic-gate  *
7340Sstevel@tonic-gate  * (XXX why isn't this a library function ...?)
7350Sstevel@tonic-gate  */
7360Sstevel@tonic-gate static mblk_t *
7370Sstevel@tonic-gate prependb(mblk_t *mp, size_t len, size_t align)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	mblk_t *newmp;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	if (align == 0)
7430Sstevel@tonic-gate 		align = 8;
7440Sstevel@tonic-gate 	if (DB_REF(mp) > 1 || mp->b_datap->db_base+len > mp->b_rptr ||
7450Sstevel@tonic-gate 	    ((uint_t)((uintptr_t)mp->b_rptr - len) % align) != 0) {
7460Sstevel@tonic-gate 		if ((newmp = allocb(len, BPRI_LO)) == NULL) {
7470Sstevel@tonic-gate 			freemsg(mp);
7480Sstevel@tonic-gate 			return (NULL);
7490Sstevel@tonic-gate 		}
7500Sstevel@tonic-gate 		newmp->b_wptr = newmp->b_rptr + len;
7510Sstevel@tonic-gate 		newmp->b_cont = mp;
7520Sstevel@tonic-gate 		return (newmp);
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 	mp->b_rptr -= len;
7550Sstevel@tonic-gate 	return (mp);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate /*
7590Sstevel@tonic-gate  * sppptun_outpkt()
7600Sstevel@tonic-gate  *
7610Sstevel@tonic-gate  * MT-Perimeters:
7620Sstevel@tonic-gate  *	shared inner, shared outer (if called from sppptun_uwput),
7630Sstevel@tonic-gate  *	exclusive inner, shared outer (if called from sppptun_uwsrv).
7640Sstevel@tonic-gate  *
7650Sstevel@tonic-gate  * Description:
7660Sstevel@tonic-gate  *    Called from sppptun_uwput or sppptun_uwsrv when processing a
7670Sstevel@tonic-gate  *    M_DATA, M_PROTO, or M_PCPROTO message.  For all cases, it tries
7680Sstevel@tonic-gate  *    to prepare the data to be sent to the module below this driver
7690Sstevel@tonic-gate  *    if there is a lower stream linked underneath.  If no lower
7700Sstevel@tonic-gate  *    stream exists, then the data will be discarded and an ENXIO
7710Sstevel@tonic-gate  *    error returned.
7720Sstevel@tonic-gate  *
7730Sstevel@tonic-gate  * Returns:
7740Sstevel@tonic-gate  *	pointer to queue if caller should do putnext, otherwise
7750Sstevel@tonic-gate  *	*mpp != NULL if message should be enqueued, otherwise
7760Sstevel@tonic-gate  *	*mpp == NULL if message is gone.
7770Sstevel@tonic-gate  */
7780Sstevel@tonic-gate static queue_t *
7790Sstevel@tonic-gate sppptun_outpkt(queue_t *q, mblk_t **mpp)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	mblk_t *mp;
7820Sstevel@tonic-gate 	tuncl_t *tcl;
7830Sstevel@tonic-gate 	tunll_t *tll;
7840Sstevel@tonic-gate 	mblk_t *encmb;
7850Sstevel@tonic-gate 	mblk_t *datamb;
7860Sstevel@tonic-gate 	dl_unitdata_req_t *dur;
7870Sstevel@tonic-gate 	queue_t *lowerq;
7880Sstevel@tonic-gate 	poep_t *poep;
7890Sstevel@tonic-gate 	int len;
7900Sstevel@tonic-gate 	ether_dest_t *edestp;
7910Sstevel@tonic-gate 	enum { luNone, luCopy, luSend } loopup;
7920Sstevel@tonic-gate 	boolean_t isdata;
7930Sstevel@tonic-gate 	struct ppptun_control *ptc;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	mp = *mpp;
7965640Scarlsonj 	tcl = q->q_ptr;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	*mpp = NULL;
7990Sstevel@tonic-gate 	if (!(tcl->tcl_flags & TCLF_ISCLIENT)) {
8000Sstevel@tonic-gate 		merror(q, mp, EINVAL);
8010Sstevel@tonic-gate 		return (NULL);
8020Sstevel@tonic-gate 	}
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	isdata = (MTYPE(mp) == M_DATA);
8050Sstevel@tonic-gate 	if (isdata) {
8060Sstevel@tonic-gate 		tll = tcl->tcl_data_tll;
8070Sstevel@tonic-gate 		ptc = NULL;
8080Sstevel@tonic-gate 	} else {
8090Sstevel@tonic-gate 		/*
8100Sstevel@tonic-gate 		 * If data are unaligned or otherwise unsuitable, then
8110Sstevel@tonic-gate 		 * discard.
8120Sstevel@tonic-gate 		 */
8130Sstevel@tonic-gate 		if (MBLKL(mp) != sizeof (*ptc) || DB_REF(mp) > 1 ||
8140Sstevel@tonic-gate 		    !IS_P2ALIGNED(mp->b_rptr, sizeof (ptc))) {
8150Sstevel@tonic-gate 			KCINCR(cks_octrl_drop);
8165640Scarlsonj 			DTRACE_PROBE2(sppptun__bad__control, tuncl_t *, tcl,
8175640Scarlsonj 			    mblk_t *, mp);
8180Sstevel@tonic-gate 			merror(q, mp, EINVAL);
8190Sstevel@tonic-gate 			return (NULL);
8200Sstevel@tonic-gate 		}
8210Sstevel@tonic-gate 		ptc = (struct ppptun_control *)mp->b_rptr;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 		/* Set stream discriminator value if not yet set. */
8240Sstevel@tonic-gate 		if (tcl->tcl_ctlval == 0)
8250Sstevel@tonic-gate 			tcl->tcl_ctlval = ptc->ptc_discrim;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 		/* If this is a test message, then reply to caller. */
8280Sstevel@tonic-gate 		if (ptc->ptc_action == PTCA_TEST) {
8295640Scarlsonj 			DTRACE_PROBE2(sppptun__test, tuncl_t *, tcl,
8305640Scarlsonj 			    struct ppptun_control *, ptc);
8310Sstevel@tonic-gate 			if (mp->b_cont != NULL) {
8320Sstevel@tonic-gate 				freemsg(mp->b_cont);
8330Sstevel@tonic-gate 				mp->b_cont = NULL;
8340Sstevel@tonic-gate 			}
8350Sstevel@tonic-gate 			ptc->ptc_discrim = tcl->tcl_ctlval;
8360Sstevel@tonic-gate 			putnext(RD(q), mp);
8370Sstevel@tonic-gate 			return (NULL);
8380Sstevel@tonic-gate 		}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 		/* If this one isn't for us, then discard it */
8410Sstevel@tonic-gate 		if (tcl->tcl_ctlval != ptc->ptc_discrim) {
8425640Scarlsonj 			DTRACE_PROBE2(sppptun__bad__discrim, tuncl_t *, tcl,
8435640Scarlsonj 			    struct ppptun_control *, ptc);
8440Sstevel@tonic-gate 			freemsg(mp);
8450Sstevel@tonic-gate 			return (NULL);
8460Sstevel@tonic-gate 		}
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 		/* Don't allow empty control packets. */
8490Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
8500Sstevel@tonic-gate 			KCINCR(cks_octrl_drop);
8510Sstevel@tonic-gate 			merror(q, mp, EINVAL);
8520Sstevel@tonic-gate 			return (NULL);
8530Sstevel@tonic-gate 		}
8540Sstevel@tonic-gate 		tll = tcl->tcl_ctrl_tll;
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	if (tll == NULL || (lowerq = tll->tll_wq) == NULL) {
8585640Scarlsonj 		DTRACE_PROBE3(sppptun__cannot__send, tuncl_t *, tcl,
8595640Scarlsonj 		    tunll_t *, tll, mblk_t *, mp);
8600Sstevel@tonic-gate 		merror(q, mp, ENXIO);
8610Sstevel@tonic-gate 		if (isdata) {
8620Sstevel@tonic-gate 			tcl->tcl_stats.ppp_oerrors++;
8630Sstevel@tonic-gate 		} else {
8640Sstevel@tonic-gate 			KCINCR(cks_octrl_drop);
8650Sstevel@tonic-gate 		}
8660Sstevel@tonic-gate 		return (NULL);
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	/*
8700Sstevel@tonic-gate 	 * If so, then try to send it down.  The lower queue is only
8710Sstevel@tonic-gate 	 * ever detached while holding an exclusive lock on the whole
8720Sstevel@tonic-gate 	 * driver, so we can be confident that the lower queue is
8730Sstevel@tonic-gate 	 * still there.
8740Sstevel@tonic-gate 	 */
8750Sstevel@tonic-gate 	if (!bcanputnext(lowerq, mp->b_band)) {
8765640Scarlsonj 		DTRACE_PROBE3(sppptun__flow__control, tuncl_t *, tcl,
8775640Scarlsonj 		    tunll_t *, tll, mblk_t *, mp);
8780Sstevel@tonic-gate 		*mpp = mp;
8790Sstevel@tonic-gate 		return (NULL);
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	/*
8830Sstevel@tonic-gate 	 * Note: DLPI and TPI expect that the first buffer contains
8840Sstevel@tonic-gate 	 * the control (unitdata-req) header, destination address, and
8850Sstevel@tonic-gate 	 * nothing else.  Any protocol headers must go in the next
8860Sstevel@tonic-gate 	 * buffer.
8870Sstevel@tonic-gate 	 */
8880Sstevel@tonic-gate 	loopup = luNone;
8890Sstevel@tonic-gate 	encmb = NULL;
8900Sstevel@tonic-gate 	if (isdata) {
8910Sstevel@tonic-gate 		if (tll->tll_alen != 0 &&
8920Sstevel@tonic-gate 		    bcmp(&tcl->tcl_address, &tll->tll_lcladdr,
8935640Scarlsonj 		    tll->tll_alen) == 0)
8940Sstevel@tonic-gate 			loopup = luSend;
8950Sstevel@tonic-gate 		switch (tll->tll_style) {
8960Sstevel@tonic-gate 		case PTS_PPPOE:
8970Sstevel@tonic-gate 			/* Strip address and control fields if present. */
8980Sstevel@tonic-gate 			if (mp->b_rptr[0] == 0xFF) {
8990Sstevel@tonic-gate 				if (MBLKL(mp) < 3) {
9000Sstevel@tonic-gate 					encmb = msgpullup(mp, 3);
9010Sstevel@tonic-gate 					freemsg(mp);
9020Sstevel@tonic-gate 					if ((mp = encmb) == NULL)
9030Sstevel@tonic-gate 						break;
9040Sstevel@tonic-gate 				}
9050Sstevel@tonic-gate 				mp->b_rptr += 2;
9060Sstevel@tonic-gate 			}
9070Sstevel@tonic-gate 			/* Broadcasting data is probably not a good idea. */
9080Sstevel@tonic-gate 			if (tcl->tcl_address.pta_pppoe.ptma_mac[0] & 1)
9090Sstevel@tonic-gate 				break;
9100Sstevel@tonic-gate 			encmb = dlpi_alloc(sizeof (*dur) + sizeof (*edestp),
9110Sstevel@tonic-gate 			    DL_UNITDATA_REQ);
9120Sstevel@tonic-gate 			if (encmb == NULL)
9130Sstevel@tonic-gate 				break;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 			dur = (dl_unitdata_req_t *)encmb->b_rptr;
9160Sstevel@tonic-gate 			dur->dl_dest_addr_length = sizeof (*edestp);
9170Sstevel@tonic-gate 			dur->dl_dest_addr_offset = sizeof (*dur);
9180Sstevel@tonic-gate 			edestp = (ether_dest_t *)(dur + 1);
9190Sstevel@tonic-gate 			ether_copy(tcl->tcl_address.pta_pppoe.ptma_mac,
9200Sstevel@tonic-gate 			    edestp->addr);
9210Sstevel@tonic-gate 			/* DLPI SAPs are in host byte order! */
9220Sstevel@tonic-gate 			edestp->type = ETHERTYPE_PPPOES;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 			/* Make sure the protocol field isn't compressed. */
9250Sstevel@tonic-gate 			len = (*mp->b_rptr & 1);
9260Sstevel@tonic-gate 			mp = prependb(mp, sizeof (*poep) + len, POE_HDR_ALIGN);
9270Sstevel@tonic-gate 			if (mp == NULL)
9280Sstevel@tonic-gate 				break;
9290Sstevel@tonic-gate 			poep = (poep_t *)mp->b_rptr;
9300Sstevel@tonic-gate 			poep->poep_version_type = POE_VERSION;
9310Sstevel@tonic-gate 			poep->poep_code = POECODE_DATA;
9320Sstevel@tonic-gate 			poep->poep_session_id = htons(tcl->tcl_rsessid);
9330Sstevel@tonic-gate 			poep->poep_length = htons(msgsize(mp) -
9340Sstevel@tonic-gate 			    sizeof (*poep));
9350Sstevel@tonic-gate 			if (len > 0)
9360Sstevel@tonic-gate 				*(char *)(poep + 1) = '\0';
9370Sstevel@tonic-gate 			break;
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 		default:
9400Sstevel@tonic-gate 			ASSERT(0);
9410Sstevel@tonic-gate 		}
9420Sstevel@tonic-gate 	} else {
9430Sstevel@tonic-gate 		/*
9440Sstevel@tonic-gate 		 * Control side encapsulation.
9450Sstevel@tonic-gate 		 */
9460Sstevel@tonic-gate 		if (bcmp(&ptc->ptc_address, &tll->tll_lcladdr, tll->tll_alen)
9470Sstevel@tonic-gate 		    == 0)
9480Sstevel@tonic-gate 			loopup = luSend;
9490Sstevel@tonic-gate 		datamb = mp->b_cont;
9500Sstevel@tonic-gate 		switch (tll->tll_style) {
9510Sstevel@tonic-gate 		case PTS_PPPOE:
9520Sstevel@tonic-gate 			/*
9530Sstevel@tonic-gate 			 * Don't allow a loopback session to establish
9540Sstevel@tonic-gate 			 * itself.  PPPoE is broken; it uses only one
9550Sstevel@tonic-gate 			 * session ID for both data directions, so the
9560Sstevel@tonic-gate 			 * loopback data path can simply never work.
9570Sstevel@tonic-gate 			 */
9580Sstevel@tonic-gate 			if (loopup == luSend &&
9590Sstevel@tonic-gate 			    ((poep_t *)datamb->b_rptr)->poep_code ==
9600Sstevel@tonic-gate 			    POECODE_PADR)
9610Sstevel@tonic-gate 				break;
9620Sstevel@tonic-gate 			encmb = dlpi_alloc(sizeof (*dur) + sizeof (*edestp),
9630Sstevel@tonic-gate 			    DL_UNITDATA_REQ);
9640Sstevel@tonic-gate 			if (encmb == NULL)
9650Sstevel@tonic-gate 				break;
9660Sstevel@tonic-gate 			dur = (dl_unitdata_req_t *)encmb->b_rptr;
9670Sstevel@tonic-gate 			dur->dl_dest_addr_length = sizeof (*edestp);
9680Sstevel@tonic-gate 			dur->dl_dest_addr_offset = sizeof (*dur);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 			edestp = (ether_dest_t *)(dur + 1);
9710Sstevel@tonic-gate 			/* DLPI SAPs are in host byte order! */
9720Sstevel@tonic-gate 			edestp->type = ETHERTYPE_PPPOED;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 			/*
9750Sstevel@tonic-gate 			 * If destination isn't set yet, then we have to
9760Sstevel@tonic-gate 			 * allow anything at all.  Otherwise, force use
9770Sstevel@tonic-gate 			 * of configured peer address.
9780Sstevel@tonic-gate 			 */
9790Sstevel@tonic-gate 			if (bcmp(tcl->tcl_address.pta_pppoe.ptma_mac,
9800Sstevel@tonic-gate 			    zero_mac_addr, sizeof (zero_mac_addr)) == 0 ||
9815640Scarlsonj 			    (tcl->tcl_flags & TCLF_DAEMON)) {
9820Sstevel@tonic-gate 				ether_copy(ptc->ptc_address.pta_pppoe.ptma_mac,
9830Sstevel@tonic-gate 				    edestp->addr);
9840Sstevel@tonic-gate 			} else {
9850Sstevel@tonic-gate 				ether_copy(tcl->tcl_address.pta_pppoe.ptma_mac,
9860Sstevel@tonic-gate 				    edestp->addr);
9870Sstevel@tonic-gate 			}
9880Sstevel@tonic-gate 			/* Reflect multicast/broadcast back up. */
9890Sstevel@tonic-gate 			if (edestp->addr[0] & 1)
9900Sstevel@tonic-gate 				loopup = luCopy;
9910Sstevel@tonic-gate 			break;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 		case PTS_PPTP:
9940Sstevel@tonic-gate 			/*
9950Sstevel@tonic-gate 			 * PPTP's control side is actually done over
9960Sstevel@tonic-gate 			 * separate TCP connections.
9970Sstevel@tonic-gate 			 */
9980Sstevel@tonic-gate 		default:
9990Sstevel@tonic-gate 			ASSERT(0);
10000Sstevel@tonic-gate 		}
10010Sstevel@tonic-gate 		freeb(mp);
10020Sstevel@tonic-gate 		mp = datamb;
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 	if (mp == NULL || encmb == NULL) {
10055640Scarlsonj 		DTRACE_PROBE1(sppptun__output__failure, tuncl_t *, tcl);
10060Sstevel@tonic-gate 		freemsg(mp);
10070Sstevel@tonic-gate 		freemsg(encmb);
10080Sstevel@tonic-gate 		if (isdata) {
10090Sstevel@tonic-gate 			tcl->tcl_stats.ppp_oerrors++;
10100Sstevel@tonic-gate 		} else {
10110Sstevel@tonic-gate 			KCINCR(cks_octrl_drop);
10120Sstevel@tonic-gate 			KLINCR(lks_octrl_drop);
10130Sstevel@tonic-gate 		}
10140Sstevel@tonic-gate 		lowerq = NULL;
10150Sstevel@tonic-gate 	} else {
10160Sstevel@tonic-gate 		if (isdata) {
10170Sstevel@tonic-gate 			tcl->tcl_stats.ppp_obytes += msgsize(mp);
10180Sstevel@tonic-gate 			tcl->tcl_stats.ppp_opackets++;
10190Sstevel@tonic-gate 		} else {
10200Sstevel@tonic-gate 			KCINCR(cks_octrls);
10210Sstevel@tonic-gate 			KLINCR(lks_octrls);
10220Sstevel@tonic-gate 		}
10230Sstevel@tonic-gate 		if (encmb != mp)
10240Sstevel@tonic-gate 			encmb->b_cont = mp;
10250Sstevel@tonic-gate 		switch (loopup) {
10260Sstevel@tonic-gate 		case luNone:
10270Sstevel@tonic-gate 			*mpp = encmb;
10280Sstevel@tonic-gate 			break;
10290Sstevel@tonic-gate 		case luCopy:
10300Sstevel@tonic-gate 			mp = copymsg(encmb);
10310Sstevel@tonic-gate 			if (mp != NULL)
10320Sstevel@tonic-gate 				sppptun_urput(RD(lowerq), mp);
10330Sstevel@tonic-gate 			*mpp = encmb;
10340Sstevel@tonic-gate 			break;
10350Sstevel@tonic-gate 		case luSend:
10360Sstevel@tonic-gate 			sppptun_urput(RD(lowerq), encmb);
10370Sstevel@tonic-gate 			lowerq = NULL;
10380Sstevel@tonic-gate 			break;
10390Sstevel@tonic-gate 		}
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 	return (lowerq);
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate /*
10450Sstevel@tonic-gate  * Enqueue a message to be sent when the lower stream is closed.  This
10460Sstevel@tonic-gate  * is done so that we're guaranteed that we always have the necessary
10470Sstevel@tonic-gate  * resources to properly detach ourselves from the system.  (If we
10480Sstevel@tonic-gate  * waited until the close was done to allocate these messages, then
10490Sstevel@tonic-gate  * the message allocation could fail, and we'd be unable to properly
10500Sstevel@tonic-gate  * detach.)
10510Sstevel@tonic-gate  */
10520Sstevel@tonic-gate static void
10530Sstevel@tonic-gate save_for_close(tunll_t *tll, mblk_t *mp)
10540Sstevel@tonic-gate {
10550Sstevel@tonic-gate 	mblk_t *onc;
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	if ((onc = tll->tll_onclose) == NULL)
10580Sstevel@tonic-gate 		tll->tll_onclose = mp;
10590Sstevel@tonic-gate 	else {
10600Sstevel@tonic-gate 		while (onc->b_next != NULL)
10610Sstevel@tonic-gate 			onc = onc->b_next;
10620Sstevel@tonic-gate 		onc->b_next = mp;
10630Sstevel@tonic-gate 	}
10640Sstevel@tonic-gate }
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate /*
10670Sstevel@tonic-gate  * Given the lower stream name, locate the state structure.  Note that
10680Sstevel@tonic-gate  * lookup of tcl pointers (and use of those pointers) is safe because
10690Sstevel@tonic-gate  * modification is done only when exclusive on both inner and outer
10700Sstevel@tonic-gate  * perimeters.
10710Sstevel@tonic-gate  */
10720Sstevel@tonic-gate static tunll_t *
10730Sstevel@tonic-gate tll_lookup_on_name(char *dname)
10740Sstevel@tonic-gate {
10750Sstevel@tonic-gate 	tunll_t *tll;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	tll = TO_TLL(tunll_list.q_forw);
10780Sstevel@tonic-gate 	for (; tll != TO_TLL(&tunll_list); tll = TO_TLL(tll->tll_next))
10790Sstevel@tonic-gate 		if (strcmp(dname, tll->tll_name) == 0)
10800Sstevel@tonic-gate 			return (tll);
10810Sstevel@tonic-gate 	return (NULL);
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate /*
10850Sstevel@tonic-gate  * sppptun_inner_ioctl()
10860Sstevel@tonic-gate  *
10870Sstevel@tonic-gate  * MT-Perimeters:
10880Sstevel@tonic-gate  *    exclusive inner, shared outer.
10890Sstevel@tonic-gate  *
10900Sstevel@tonic-gate  * Description:
10910Sstevel@tonic-gate  *    Called by qwriter from sppptun_ioctl as the result of receiving
10920Sstevel@tonic-gate  *    a handled ioctl.
10930Sstevel@tonic-gate  */
10940Sstevel@tonic-gate static void
10950Sstevel@tonic-gate sppptun_inner_ioctl(queue_t *q, mblk_t *mp)
10960Sstevel@tonic-gate {
10970Sstevel@tonic-gate 	struct iocblk *iop;
10980Sstevel@tonic-gate 	int rc = 0;
10990Sstevel@tonic-gate 	int len = 0;
11000Sstevel@tonic-gate 	int i;
11010Sstevel@tonic-gate 	tuncl_t *tcl;
11020Sstevel@tonic-gate 	tunll_t *tll;
11030Sstevel@tonic-gate 	union ppptun_name *ptn;
11040Sstevel@tonic-gate 	struct ppptun_info *pti;
11050Sstevel@tonic-gate 	struct ppptun_peer *ptp;
11060Sstevel@tonic-gate 	mblk_t *mptmp;
11070Sstevel@tonic-gate 	ppptun_atype *pap;
11080Sstevel@tonic-gate 	struct ppp_stats64 *psp;
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	iop = (struct iocblk *)mp->b_rptr;
11110Sstevel@tonic-gate 	tcl = NULL;
11125640Scarlsonj 	tll = q->q_ptr;
11130Sstevel@tonic-gate 	if (tll->tll_flags & TLLF_NOTLOWER) {
11140Sstevel@tonic-gate 		tcl = (tuncl_t *)tll;
11150Sstevel@tonic-gate 		tll = NULL;
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate 
11185640Scarlsonj 	DTRACE_PROBE3(sppptun__ioctl, tuncl_t *, tcl, tunll_t *, tll,
11195640Scarlsonj 	    struct iocblk *, iop);
11205640Scarlsonj 
11210Sstevel@tonic-gate 	switch (iop->ioc_cmd) {
11220Sstevel@tonic-gate 	case PPPIO_DEBUG:
11235640Scarlsonj 		/*
11245640Scarlsonj 		 * Debug requests are now ignored; use dtrace or wireshark
11255640Scarlsonj 		 * instead.
11265640Scarlsonj 		 */
11270Sstevel@tonic-gate 		break;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	case PPPIO_GETSTAT:
11300Sstevel@tonic-gate 		rc = EINVAL;
11310Sstevel@tonic-gate 		break;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	case PPPIO_GETSTAT64:
11340Sstevel@tonic-gate 		/* Client (device) side only */
11350Sstevel@tonic-gate 		if (tcl == NULL) {
11360Sstevel@tonic-gate 			rc = EINVAL;
11370Sstevel@tonic-gate 			break;
11380Sstevel@tonic-gate 		}
11390Sstevel@tonic-gate 		mptmp = allocb(sizeof (*psp), BPRI_HI);
11400Sstevel@tonic-gate 		if (mptmp == NULL) {
11410Sstevel@tonic-gate 			rc = ENOSR;
11420Sstevel@tonic-gate 			break;
11430Sstevel@tonic-gate 		}
11440Sstevel@tonic-gate 		freemsg(mp->b_cont);
11450Sstevel@tonic-gate 		mp->b_cont = mptmp;
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 		psp = (struct ppp_stats64 *)mptmp->b_wptr;
11480Sstevel@tonic-gate 		bzero((caddr_t)psp, sizeof (*psp));
11490Sstevel@tonic-gate 		psp->p = tcl->tcl_stats;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 		len = sizeof (*psp);
11520Sstevel@tonic-gate 		break;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	case PPPTUN_SNAME:
11550Sstevel@tonic-gate 		/* This is done on the *module* (lower level) side. */
11560Sstevel@tonic-gate 		if (tll == NULL || mp->b_cont == NULL ||
11570Sstevel@tonic-gate 		    iop->ioc_count != sizeof (*ptn) ||
11580Sstevel@tonic-gate 		    *mp->b_cont->b_rptr == '\0') {
11590Sstevel@tonic-gate 			rc = EINVAL;
11600Sstevel@tonic-gate 			break;
11610Sstevel@tonic-gate 		}
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 		ptn = (union ppptun_name *)mp->b_cont->b_rptr;
11640Sstevel@tonic-gate 		ptn->ptn_name[sizeof (ptn->ptn_name) - 1] = '\0';
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 		if ((tll = tll_lookup_on_name(ptn->ptn_name)) != NULL) {
11670Sstevel@tonic-gate 			rc = EEXIST;
11680Sstevel@tonic-gate 			break;
11690Sstevel@tonic-gate 		}
11700Sstevel@tonic-gate 		tll = (tunll_t *)q->q_ptr;
11710Sstevel@tonic-gate 		(void) strcpy(tll->tll_name, ptn->ptn_name);
11720Sstevel@tonic-gate 		break;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	case PPPTUN_GNAME:
11750Sstevel@tonic-gate 		/* This is done on the *module* (lower level) side. */
11760Sstevel@tonic-gate 		if (tll == NULL) {
11770Sstevel@tonic-gate 			rc = EINVAL;
11780Sstevel@tonic-gate 			break;
11790Sstevel@tonic-gate 		}
11800Sstevel@tonic-gate 		if (mp->b_cont != NULL)
11810Sstevel@tonic-gate 			freemsg(mp->b_cont);
11820Sstevel@tonic-gate 		if ((mp->b_cont = allocb(sizeof (*ptn), BPRI_HI)) == NULL) {
11830Sstevel@tonic-gate 			rc = ENOSR;
11840Sstevel@tonic-gate 			break;
11850Sstevel@tonic-gate 		}
11860Sstevel@tonic-gate 		ptn = (union ppptun_name *)mp->b_cont->b_rptr;
11870Sstevel@tonic-gate 		bcopy(tll->tll_name, ptn->ptn_name, sizeof (ptn->ptn_name));
11880Sstevel@tonic-gate 		len = sizeof (*ptn);
11890Sstevel@tonic-gate 		break;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	case PPPTUN_SINFO:
11920Sstevel@tonic-gate 	case PPPTUN_GINFO:
11930Sstevel@tonic-gate 		/* Either side */
11940Sstevel@tonic-gate 		if (mp->b_cont == NULL || iop->ioc_count != sizeof (*pti)) {
11950Sstevel@tonic-gate 			rc = EINVAL;
11960Sstevel@tonic-gate 			break;
11970Sstevel@tonic-gate 		}
11980Sstevel@tonic-gate 		pti = (struct ppptun_info *)mp->b_cont->b_rptr;
11990Sstevel@tonic-gate 		if (pti->pti_name[0] != '\0')
12000Sstevel@tonic-gate 			tll = tll_lookup_on_name(pti->pti_name);
12010Sstevel@tonic-gate 		if (tll == NULL) {
12020Sstevel@tonic-gate 			/* Driver (client) side must have name */
12035640Scarlsonj 			if (tcl != NULL && pti->pti_name[0] == '\0')
12040Sstevel@tonic-gate 				rc = EINVAL;
12055640Scarlsonj 			else
12060Sstevel@tonic-gate 				rc = ESRCH;
12070Sstevel@tonic-gate 			break;
12080Sstevel@tonic-gate 		}
12090Sstevel@tonic-gate 		if (iop->ioc_cmd == PPPTUN_GINFO) {
12100Sstevel@tonic-gate 			pti->pti_muxid = tll->tll_muxid;
12110Sstevel@tonic-gate 			pti->pti_style = tll->tll_style;
12120Sstevel@tonic-gate 			len = sizeof (*pti);
12130Sstevel@tonic-gate 			break;
12140Sstevel@tonic-gate 		}
12150Sstevel@tonic-gate 		tll->tll_muxid = pti->pti_muxid;
12160Sstevel@tonic-gate 		tll->tll_style = pti->pti_style;
12170Sstevel@tonic-gate 		switch (tll->tll_style) {
12180Sstevel@tonic-gate 		case PTS_PPPOE:		/* DLPI type */
12190Sstevel@tonic-gate 			tll->tll_alen = sizeof (tll->tll_lcladdr.pta_pppoe);
12200Sstevel@tonic-gate 			mptmp = dlpi_alloc(sizeof (dl_unbind_req_t),
12210Sstevel@tonic-gate 			    DL_UNBIND_REQ);
12220Sstevel@tonic-gate 			if (mptmp == NULL) {
12230Sstevel@tonic-gate 				rc = ENOSR;
12240Sstevel@tonic-gate 				break;
12250Sstevel@tonic-gate 			}
12260Sstevel@tonic-gate 			save_for_close(tll, mptmp);
12270Sstevel@tonic-gate 			mptmp = dlpi_alloc(sizeof (dl_detach_req_t),
12280Sstevel@tonic-gate 			    DL_DETACH_REQ);
12290Sstevel@tonic-gate 			if (mptmp == NULL) {
12300Sstevel@tonic-gate 				rc = ENOSR;
12310Sstevel@tonic-gate 				break;
12320Sstevel@tonic-gate 			}
12330Sstevel@tonic-gate 			save_for_close(tll, mptmp);
12340Sstevel@tonic-gate 			break;
12350Sstevel@tonic-gate 		default:
12360Sstevel@tonic-gate 			tll->tll_style = PTS_NONE;
12370Sstevel@tonic-gate 			tll->tll_alen = 0;
12380Sstevel@tonic-gate 			rc = EINVAL;
12390Sstevel@tonic-gate 			break;
12400Sstevel@tonic-gate 		}
12410Sstevel@tonic-gate 		break;
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	case PPPTUN_GNNAME:
12440Sstevel@tonic-gate 		/* This can be done on either side. */
12450Sstevel@tonic-gate 		if (mp->b_cont == NULL || iop->ioc_count < sizeof (uint32_t)) {
12460Sstevel@tonic-gate 			rc = EINVAL;
12470Sstevel@tonic-gate 			break;
12480Sstevel@tonic-gate 		}
12490Sstevel@tonic-gate 		ptn = (union ppptun_name *)mp->b_cont->b_rptr;
12500Sstevel@tonic-gate 		i = ptn->ptn_index;
12510Sstevel@tonic-gate 		tll = TO_TLL(tunll_list.q_forw);
12520Sstevel@tonic-gate 		while (--i >= 0 && tll != TO_TLL(&tunll_list))
12530Sstevel@tonic-gate 			tll = TO_TLL(tll->tll_next);
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';
13870Sstevel@tonic-gate 		tll = tll_lookup_on_name(ptn->ptn_name);
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';
14650Sstevel@tonic-gate 		tll = tll_lookup_on_name(ptn->ptn_name);
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 
14730Sstevel@tonic-gate 	default:
14740Sstevel@tonic-gate 		/* Caller should already have checked command value */
14750Sstevel@tonic-gate 		ASSERT(0);
14760Sstevel@tonic-gate 	}
14770Sstevel@tonic-gate 	if (rc != 0) {
14780Sstevel@tonic-gate 		miocnak(q, mp, 0, rc);
14790Sstevel@tonic-gate 	} else {
14800Sstevel@tonic-gate 		if (len > 0)
14810Sstevel@tonic-gate 			mp->b_cont->b_wptr = mp->b_cont->b_rptr + len;
14820Sstevel@tonic-gate 		miocack(q, mp, len, 0);
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate }
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate /*
14870Sstevel@tonic-gate  * sppptun_ioctl()
14880Sstevel@tonic-gate  *
14890Sstevel@tonic-gate  * MT-Perimeters:
14900Sstevel@tonic-gate  *    shared inner, shared outer.
14910Sstevel@tonic-gate  *
14920Sstevel@tonic-gate  * Description:
14930Sstevel@tonic-gate  *    Called by sppptun_uwput as the result of receiving a M_IOCTL command.
14940Sstevel@tonic-gate  */
14950Sstevel@tonic-gate static void
14960Sstevel@tonic-gate sppptun_ioctl(queue_t *q, mblk_t *mp)
14970Sstevel@tonic-gate {
14980Sstevel@tonic-gate 	struct iocblk *iop;
14990Sstevel@tonic-gate 	int rc = 0;
15000Sstevel@tonic-gate 	int len = 0;
15010Sstevel@tonic-gate 	uint32_t val = 0;
15020Sstevel@tonic-gate 	tunll_t *tll;
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	iop = (struct iocblk *)mp->b_rptr;
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	switch (iop->ioc_cmd) {
15070Sstevel@tonic-gate 	case PPPIO_DEBUG:
15080Sstevel@tonic-gate 	case PPPIO_GETSTAT:
15090Sstevel@tonic-gate 	case PPPIO_GETSTAT64:
15100Sstevel@tonic-gate 	case PPPTUN_SNAME:
15110Sstevel@tonic-gate 	case PPPTUN_GNAME:
15120Sstevel@tonic-gate 	case PPPTUN_SINFO:
15130Sstevel@tonic-gate 	case PPPTUN_GINFO:
15140Sstevel@tonic-gate 	case PPPTUN_GNNAME:
15150Sstevel@tonic-gate 	case PPPTUN_LCLADDR:
15160Sstevel@tonic-gate 	case PPPTUN_SPEER:
15170Sstevel@tonic-gate 	case PPPTUN_GPEER:
15180Sstevel@tonic-gate 	case PPPTUN_SDATA:
15190Sstevel@tonic-gate 	case PPPTUN_GDATA:
15200Sstevel@tonic-gate 	case PPPTUN_SCTL:
15210Sstevel@tonic-gate 	case PPPTUN_GCTL:
15220Sstevel@tonic-gate 	case PPPTUN_DCTL:
15230Sstevel@tonic-gate 		qwriter(q, mp, sppptun_inner_ioctl, PERIM_INNER);
15240Sstevel@tonic-gate 		return;
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	case PPPIO_GCLEAN:	/* always clean */
15270Sstevel@tonic-gate 		val = RCV_B7_1 | RCV_B7_0 | RCV_ODDP | RCV_EVNP;
15280Sstevel@tonic-gate 		len = sizeof (uint32_t);
15290Sstevel@tonic-gate 		break;
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	case PPPIO_GTYPE:	/* we look like an async driver. */
15320Sstevel@tonic-gate 		val = PPPTYP_AHDLC;
15330Sstevel@tonic-gate 		len = sizeof (uint32_t);
15340Sstevel@tonic-gate 		break;
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	case PPPIO_CFLAGS:	/* never compress headers */
15370Sstevel@tonic-gate 		val = 0;
15380Sstevel@tonic-gate 		len = sizeof (uint32_t);
15390Sstevel@tonic-gate 		break;
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 		/* quietly ack PPP things we don't need to do. */
15420Sstevel@tonic-gate 	case PPPIO_XFCS:
15430Sstevel@tonic-gate 	case PPPIO_RFCS:
15440Sstevel@tonic-gate 	case PPPIO_XACCM:
15450Sstevel@tonic-gate 	case PPPIO_RACCM:
15460Sstevel@tonic-gate 	case PPPIO_LASTMOD:
15470Sstevel@tonic-gate 	case PPPIO_MUX:
15480Sstevel@tonic-gate 	case I_PLINK:
15490Sstevel@tonic-gate 	case I_PUNLINK:
15500Sstevel@tonic-gate 	case I_LINK:
15510Sstevel@tonic-gate 	case I_UNLINK:
15520Sstevel@tonic-gate 		break;
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	default:
15550Sstevel@tonic-gate 		tll = (tunll_t *)q->q_ptr;
15560Sstevel@tonic-gate 		if (!(tll->tll_flags & TLLF_NOTLOWER)) {
15570Sstevel@tonic-gate 			/* module side; pass this through. */
15580Sstevel@tonic-gate 			putnext(q, mp);
15590Sstevel@tonic-gate 			return;
15600Sstevel@tonic-gate 		}
15610Sstevel@tonic-gate 		rc = EINVAL;
15620Sstevel@tonic-gate 		break;
15630Sstevel@tonic-gate 	}
15640Sstevel@tonic-gate 	if (rc == 0 && len == sizeof (uint32_t)) {
15650Sstevel@tonic-gate 		if (mp->b_cont != NULL)
15660Sstevel@tonic-gate 			freemsg(mp->b_cont);
15670Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (uint32_t), BPRI_HI);
15680Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
15690Sstevel@tonic-gate 			rc = ENOSR;
15700Sstevel@tonic-gate 		} else {
15710Sstevel@tonic-gate 			*(uint32_t *)mp->b_cont->b_wptr = val;
15720Sstevel@tonic-gate 			mp->b_cont->b_wptr += sizeof (uint32_t);
15730Sstevel@tonic-gate 		}
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 	if (rc == 0) {
15760Sstevel@tonic-gate 		miocack(q, mp, len, 0);
15770Sstevel@tonic-gate 	} else {
15780Sstevel@tonic-gate 		miocnak(q, mp, 0, rc);
15790Sstevel@tonic-gate 	}
15800Sstevel@tonic-gate }
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate /*
15830Sstevel@tonic-gate  * sppptun_inner_mctl()
15840Sstevel@tonic-gate  *
15850Sstevel@tonic-gate  * MT-Perimeters:
15860Sstevel@tonic-gate  *    exclusive inner, shared outer.
15870Sstevel@tonic-gate  *
15880Sstevel@tonic-gate  * Description:
15890Sstevel@tonic-gate  *    Called by qwriter (via sppptun_uwput) as the result of receiving
15900Sstevel@tonic-gate  *    an M_CTL.  Called only on the client (driver) side.
15910Sstevel@tonic-gate  */
15920Sstevel@tonic-gate static void
15930Sstevel@tonic-gate sppptun_inner_mctl(queue_t *q, mblk_t *mp)
15940Sstevel@tonic-gate {
15950Sstevel@tonic-gate 	int msglen;
15960Sstevel@tonic-gate 	tuncl_t *tcl;
15970Sstevel@tonic-gate 
15985640Scarlsonj 	tcl = q->q_ptr;
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	if (!(tcl->tcl_flags & TCLF_ISCLIENT)) {
16010Sstevel@tonic-gate 		freemsg(mp);
16020Sstevel@tonic-gate 		return;
16030Sstevel@tonic-gate 	}
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	msglen = MBLKL(mp);
16060Sstevel@tonic-gate 	switch (*mp->b_rptr) {
16070Sstevel@tonic-gate 	case PPPCTL_UNIT:
16080Sstevel@tonic-gate 		if (msglen == 2)
16090Sstevel@tonic-gate 			tcl->tcl_unit = mp->b_rptr[1];
16100Sstevel@tonic-gate 		else if (msglen == 8)
16110Sstevel@tonic-gate 			tcl->tcl_unit = ((uint32_t *)mp->b_rptr)[1];
16120Sstevel@tonic-gate 		break;
16130Sstevel@tonic-gate 	}
16140Sstevel@tonic-gate 	freemsg(mp);
16150Sstevel@tonic-gate }
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate /*
16180Sstevel@tonic-gate  * sppptun_uwput()
16190Sstevel@tonic-gate  *
16200Sstevel@tonic-gate  * MT-Perimeters:
16210Sstevel@tonic-gate  *    shared inner, shared outer.
16220Sstevel@tonic-gate  *
16230Sstevel@tonic-gate  * Description:
16240Sstevel@tonic-gate  *	Regular output data and controls pass through here.
16250Sstevel@tonic-gate  */
16260Sstevel@tonic-gate static void
16270Sstevel@tonic-gate sppptun_uwput(queue_t *q, mblk_t *mp)
16280Sstevel@tonic-gate {
16290Sstevel@tonic-gate 	queue_t *nextq;
16300Sstevel@tonic-gate 	tuncl_t *tcl;
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	ASSERT(q->q_ptr != NULL);
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	switch (MTYPE(mp)) {
16350Sstevel@tonic-gate 	case M_DATA:
16360Sstevel@tonic-gate 	case M_PROTO:
16370Sstevel@tonic-gate 	case M_PCPROTO:
16380Sstevel@tonic-gate 		if (q->q_first == NULL &&
16390Sstevel@tonic-gate 		    (nextq = sppptun_outpkt(q, &mp)) != NULL) {
16400Sstevel@tonic-gate 			putnext(nextq, mp);
16410Sstevel@tonic-gate 		} else if (mp != NULL && !putq(q, mp)) {
16420Sstevel@tonic-gate 			freemsg(mp);
16430Sstevel@tonic-gate 		}
16440Sstevel@tonic-gate 		break;
16450Sstevel@tonic-gate 	case M_IOCTL:
16460Sstevel@tonic-gate 		sppptun_ioctl(q, mp);
16470Sstevel@tonic-gate 		break;
16480Sstevel@tonic-gate 	case M_CTL:
16490Sstevel@tonic-gate 		qwriter(q, mp, sppptun_inner_mctl, PERIM_INNER);
16500Sstevel@tonic-gate 		break;
16510Sstevel@tonic-gate 	default:
16520Sstevel@tonic-gate 		tcl = (tuncl_t *)q->q_ptr;
16530Sstevel@tonic-gate 		/*
16540Sstevel@tonic-gate 		 * If we're the driver, then discard unknown junk.
16550Sstevel@tonic-gate 		 * Otherwise, if we're the module, then forward along.
16560Sstevel@tonic-gate 		 */
16570Sstevel@tonic-gate 		if (tcl->tcl_flags & TCLF_ISCLIENT)
16580Sstevel@tonic-gate 			freemsg(mp);
16590Sstevel@tonic-gate 		else
16600Sstevel@tonic-gate 			putnext(q, mp);
16610Sstevel@tonic-gate 		break;
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate }
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate /*
16660Sstevel@tonic-gate  * Send a DLPI/TPI control message to the driver but make sure there
16670Sstevel@tonic-gate  * is only one outstanding message.  Uses tll_msg_pending to tell when
16680Sstevel@tonic-gate  * it must queue.  sppptun_urput calls message_done() when an ACK or a
16690Sstevel@tonic-gate  * NAK is received to process the next queued message.
16700Sstevel@tonic-gate  */
16710Sstevel@tonic-gate static void
16720Sstevel@tonic-gate message_send(tunll_t *tll, mblk_t *mp)
16730Sstevel@tonic-gate {
16740Sstevel@tonic-gate 	mblk_t **mpp;
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	if (tll->tll_msg_pending) {
16770Sstevel@tonic-gate 		/* Must queue message. Tail insertion */
16780Sstevel@tonic-gate 		mpp = &tll->tll_msg_deferred;
16790Sstevel@tonic-gate 		while (*mpp != NULL)
16800Sstevel@tonic-gate 			mpp = &((*mpp)->b_next);
16810Sstevel@tonic-gate 		*mpp = mp;
16820Sstevel@tonic-gate 		return;
16830Sstevel@tonic-gate 	}
16840Sstevel@tonic-gate 	tll->tll_msg_pending = 1;
16850Sstevel@tonic-gate 	putnext(tll->tll_wq, mp);
16860Sstevel@tonic-gate }
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate /*
16890Sstevel@tonic-gate  * Called when an DLPI/TPI control message has been acked or nacked to
16900Sstevel@tonic-gate  * send down the next queued message (if any).
16910Sstevel@tonic-gate  */
16920Sstevel@tonic-gate static void
16930Sstevel@tonic-gate message_done(tunll_t *tll)
16940Sstevel@tonic-gate {
16950Sstevel@tonic-gate 	mblk_t *mp;
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	ASSERT(tll->tll_msg_pending);
16980Sstevel@tonic-gate 	tll->tll_msg_pending = 0;
16990Sstevel@tonic-gate 	mp = tll->tll_msg_deferred;
17000Sstevel@tonic-gate 	if (mp != NULL) {
17010Sstevel@tonic-gate 		tll->tll_msg_deferred = mp->b_next;
17020Sstevel@tonic-gate 		mp->b_next = NULL;
17030Sstevel@tonic-gate 		tll->tll_msg_pending = 1;
17040Sstevel@tonic-gate 		putnext(tll->tll_wq, mp);
17050Sstevel@tonic-gate 	}
17060Sstevel@tonic-gate }
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate /*
17090Sstevel@tonic-gate  * Send down queued "close" messages to lower stream.  These were
17100Sstevel@tonic-gate  * enqueued right after the stream was originally allocated, when the
17110Sstevel@tonic-gate  * tll_style was set by PPPTUN_SINFO.
17120Sstevel@tonic-gate  */
17130Sstevel@tonic-gate static int
17140Sstevel@tonic-gate tll_close_req(tunll_t *tll)
17150Sstevel@tonic-gate {
17160Sstevel@tonic-gate 	mblk_t *mb, *mbnext;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	if ((mb = tll->tll_onclose) == NULL)
17190Sstevel@tonic-gate 		tll->tll_flags |= TLLF_SHUTDOWN_DONE;
17200Sstevel@tonic-gate 	else {
17210Sstevel@tonic-gate 		tll->tll_onclose = NULL;
17220Sstevel@tonic-gate 		while (mb != NULL) {
17230Sstevel@tonic-gate 			mbnext = mb->b_next;
17240Sstevel@tonic-gate 			mb->b_next = NULL;
17250Sstevel@tonic-gate 			message_send(tll, mb);
17260Sstevel@tonic-gate 			mb = mbnext;
17270Sstevel@tonic-gate 		}
17280Sstevel@tonic-gate 	}
17290Sstevel@tonic-gate 	return (0);
17300Sstevel@tonic-gate }
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate /*
17335640Scarlsonj  * This function is called when a backenable occurs on the write side of a
17345640Scarlsonj  * lower stream.  It walks over the client streams, looking for ones that use
17355640Scarlsonj  * the given tunll_t lower stream.  Each client is then backenabled.
17365640Scarlsonj  */
17375640Scarlsonj static void
17385640Scarlsonj tclvm_backenable(void *arg, void *firstv, size_t numv)
17395640Scarlsonj {
17405640Scarlsonj 	tunll_t *tll = arg;
17415640Scarlsonj 	int minorn = (int)(uintptr_t)firstv;
17425640Scarlsonj 	int minormax = minorn + numv;
17435640Scarlsonj 	tuncl_t *tcl;
17445640Scarlsonj 	queue_t *q;
17455640Scarlsonj 
17465640Scarlsonj 	while (minorn < minormax) {
17475640Scarlsonj 		tcl = tcl_slots[minorn - 1];
17485640Scarlsonj 		if ((tcl->tcl_data_tll == tll ||
17495640Scarlsonj 		    tcl->tcl_ctrl_tll == tll) &&
17505640Scarlsonj 		    (q = tcl->tcl_rq) != NULL) {
17515640Scarlsonj 			qenable(OTHERQ(q));
17525640Scarlsonj 		}
17535640Scarlsonj 		minorn++;
17545640Scarlsonj 	}
17555640Scarlsonj }
17565640Scarlsonj 
17575640Scarlsonj /*
17580Sstevel@tonic-gate  * sppptun_uwsrv()
17590Sstevel@tonic-gate  *
17600Sstevel@tonic-gate  * MT-Perimeters:
17610Sstevel@tonic-gate  *    exclusive inner, shared outer.
17620Sstevel@tonic-gate  *
17630Sstevel@tonic-gate  * Description:
17640Sstevel@tonic-gate  *    Upper write-side service procedure.  In addition to the usual
17650Sstevel@tonic-gate  *    STREAMS queue service handling, this routine also handles the
17660Sstevel@tonic-gate  *    transmission of the unbind/detach messages to the lower stream
17670Sstevel@tonic-gate  *    driver when a lower stream is being closed.  (See the use of
17680Sstevel@tonic-gate  *    qenable/qwait in sppptun_close().)
17690Sstevel@tonic-gate  */
17700Sstevel@tonic-gate static int
17710Sstevel@tonic-gate sppptun_uwsrv(queue_t *q)
17720Sstevel@tonic-gate {
17730Sstevel@tonic-gate 	tuncl_t	*tcl;
17740Sstevel@tonic-gate 	mblk_t *mp;
17750Sstevel@tonic-gate 	queue_t *nextq;
17760Sstevel@tonic-gate 
17775640Scarlsonj 	tcl = q->q_ptr;
17780Sstevel@tonic-gate 	if (!(tcl->tcl_flags & TCLF_ISCLIENT)) {
17790Sstevel@tonic-gate 		tunll_t *tll = (tunll_t *)tcl;
17805640Scarlsonj 
17810Sstevel@tonic-gate 		if ((tll->tll_flags & (TLLF_CLOSING|TLLF_CLOSE_DONE)) ==
17820Sstevel@tonic-gate 		    TLLF_CLOSING) {
17830Sstevel@tonic-gate 			tll->tll_error = tll_close_req(tll);
17840Sstevel@tonic-gate 			tll->tll_flags |= TLLF_CLOSE_DONE;
17855640Scarlsonj 		} else {
17865640Scarlsonj 			/*
17875640Scarlsonj 			 * We've been enabled here because of a backenable on
17885640Scarlsonj 			 * output flow control.  Backenable clients using this
17895640Scarlsonj 			 * lower layer.
17905640Scarlsonj 			 */
17915640Scarlsonj 			vmem_walk(tcl_minor_arena, VMEM_ALLOC, tclvm_backenable,
17925640Scarlsonj 			    tll);
17930Sstevel@tonic-gate 		}
17940Sstevel@tonic-gate 		return (0);
17950Sstevel@tonic-gate 	}
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
17980Sstevel@tonic-gate 		if ((nextq = sppptun_outpkt(q, &mp)) != NULL) {
17990Sstevel@tonic-gate 			putnext(nextq, mp);
18000Sstevel@tonic-gate 		} else if (mp != NULL) {
18010Sstevel@tonic-gate 			(void) putbq(q, mp);
18020Sstevel@tonic-gate 			break;
18030Sstevel@tonic-gate 		}
18040Sstevel@tonic-gate 	}
18050Sstevel@tonic-gate 	return (0);
18060Sstevel@tonic-gate }
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate /*
18090Sstevel@tonic-gate  * sppptun_lwput()
18100Sstevel@tonic-gate  *
18110Sstevel@tonic-gate  * MT-Perimeters:
18120Sstevel@tonic-gate  *    shared inner, shared outer.
18130Sstevel@tonic-gate  *
18140Sstevel@tonic-gate  * Description:
18150Sstevel@tonic-gate  *    Lower write-side put procedure.  Nothing should be sending
18160Sstevel@tonic-gate  *    packets down this stream.
18170Sstevel@tonic-gate  */
18180Sstevel@tonic-gate static void
18190Sstevel@tonic-gate sppptun_lwput(queue_t *q, mblk_t *mp)
18200Sstevel@tonic-gate {
18210Sstevel@tonic-gate 	switch (MTYPE(mp)) {
18220Sstevel@tonic-gate 	case M_PROTO:
18230Sstevel@tonic-gate 		putnext(q, mp);
18240Sstevel@tonic-gate 		break;
18250Sstevel@tonic-gate 	default:
18260Sstevel@tonic-gate 		freemsg(mp);
18270Sstevel@tonic-gate 		break;
18280Sstevel@tonic-gate 	}
18290Sstevel@tonic-gate }
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate /*
18320Sstevel@tonic-gate  * sppptun_lrput()
18330Sstevel@tonic-gate  *
18340Sstevel@tonic-gate  * MT-Perimeters:
18350Sstevel@tonic-gate  *    shared inner, shared outer.
18360Sstevel@tonic-gate  *
18370Sstevel@tonic-gate  * Description:
18380Sstevel@tonic-gate  *    Lower read-side put procedure.  Nothing should arrive here.
18390Sstevel@tonic-gate  */
18400Sstevel@tonic-gate static void
18410Sstevel@tonic-gate sppptun_lrput(queue_t *q, mblk_t *mp)
18420Sstevel@tonic-gate {
18430Sstevel@tonic-gate 	tuncl_t *tcl;
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 	switch (MTYPE(mp)) {
18460Sstevel@tonic-gate 	case M_IOCTL:
18470Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
18480Sstevel@tonic-gate 		return;
18490Sstevel@tonic-gate 	case M_FLUSH:
18500Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
18510Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
18520Sstevel@tonic-gate 		}
18530Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
18540Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;
18550Sstevel@tonic-gate 			qreply(q, mp);
18560Sstevel@tonic-gate 		} else {
18570Sstevel@tonic-gate 			freemsg(mp);
18580Sstevel@tonic-gate 		}
18590Sstevel@tonic-gate 		return;
18600Sstevel@tonic-gate 	}
18610Sstevel@tonic-gate 	/*
18620Sstevel@tonic-gate 	 * Try to forward the message to the put procedure for the upper
18630Sstevel@tonic-gate 	 * control stream for this lower stream. If there are already messages
18640Sstevel@tonic-gate 	 * queued here, queue this one up to preserve message ordering.
18650Sstevel@tonic-gate 	 */
18660Sstevel@tonic-gate 	if ((tcl = (tuncl_t *)q->q_ptr) == NULL || tcl->tcl_rq == NULL) {
18670Sstevel@tonic-gate 		freemsg(mp);
18680Sstevel@tonic-gate 		return;
18690Sstevel@tonic-gate 	}
18700Sstevel@tonic-gate 	if (queclass(mp) == QPCTL ||
18710Sstevel@tonic-gate 	    (q->q_first == NULL && canput(tcl->tcl_rq))) {
18720Sstevel@tonic-gate 		put(tcl->tcl_rq, mp);
18730Sstevel@tonic-gate 	} else {
18740Sstevel@tonic-gate 		if (!putq(q, mp))
18750Sstevel@tonic-gate 			freemsg(mp);
18760Sstevel@tonic-gate 	}
18770Sstevel@tonic-gate }
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate /*
18800Sstevel@tonic-gate  * MT-Perimeters:
18810Sstevel@tonic-gate  *    shared inner, shared outer.
18820Sstevel@tonic-gate  *
18830Sstevel@tonic-gate  *    Handle non-data DLPI messages.  Used with PPPoE, which runs over
18840Sstevel@tonic-gate  *    Ethernet only.
18850Sstevel@tonic-gate  */
18860Sstevel@tonic-gate static void
18870Sstevel@tonic-gate urput_dlpi(queue_t *q, mblk_t *mp)
18880Sstevel@tonic-gate {
18890Sstevel@tonic-gate 	int err;
18900Sstevel@tonic-gate 	union DL_primitives *dlp = (union DL_primitives *)mp->b_rptr;
18915640Scarlsonj 	tunll_t *tll = q->q_ptr;
18925640Scarlsonj 	size_t mlen = MBLKL(mp);
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	switch (dlp->dl_primitive) {
18950Sstevel@tonic-gate 	case DL_UDERROR_IND:
18960Sstevel@tonic-gate 		break;
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	case DL_ERROR_ACK:
18995640Scarlsonj 		if (mlen < DL_ERROR_ACK_SIZE)
19005640Scarlsonj 			break;
19010Sstevel@tonic-gate 		err = dlp->error_ack.dl_unix_errno ?
19020Sstevel@tonic-gate 		    dlp->error_ack.dl_unix_errno : ENXIO;
19030Sstevel@tonic-gate 		switch (dlp->error_ack.dl_error_primitive) {
19040Sstevel@tonic-gate 		case DL_UNBIND_REQ:
19050Sstevel@tonic-gate 			message_done(tll);
19060Sstevel@tonic-gate 			break;
19070Sstevel@tonic-gate 		case DL_DETACH_REQ:
19080Sstevel@tonic-gate 			message_done(tll);
19090Sstevel@tonic-gate 			tll->tll_error = err;
19100Sstevel@tonic-gate 			tll->tll_flags |= TLLF_SHUTDOWN_DONE;
19110Sstevel@tonic-gate 			break;
19120Sstevel@tonic-gate 		case DL_PHYS_ADDR_REQ:
19130Sstevel@tonic-gate 			message_done(tll);
19140Sstevel@tonic-gate 			break;
19150Sstevel@tonic-gate 		case DL_INFO_REQ:
19160Sstevel@tonic-gate 		case DL_ATTACH_REQ:
19170Sstevel@tonic-gate 		case DL_BIND_REQ:
19180Sstevel@tonic-gate 			message_done(tll);
19190Sstevel@tonic-gate 			tll->tll_error = err;
19200Sstevel@tonic-gate 			break;
19210Sstevel@tonic-gate 		}
19220Sstevel@tonic-gate 		break;
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	case DL_INFO_ACK:
19250Sstevel@tonic-gate 		message_done(tll);
19260Sstevel@tonic-gate 		break;
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 	case DL_BIND_ACK:
19290Sstevel@tonic-gate 		message_done(tll);
19300Sstevel@tonic-gate 		break;
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	case DL_PHYS_ADDR_ACK:
19330Sstevel@tonic-gate 		break;
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 	case DL_OK_ACK:
19365640Scarlsonj 		if (mlen < DL_OK_ACK_SIZE)
19375640Scarlsonj 			break;
19380Sstevel@tonic-gate 		switch (dlp->ok_ack.dl_correct_primitive) {
19390Sstevel@tonic-gate 		case DL_UNBIND_REQ:
19400Sstevel@tonic-gate 			message_done(tll);
19410Sstevel@tonic-gate 			break;
19420Sstevel@tonic-gate 		case DL_DETACH_REQ:
19430Sstevel@tonic-gate 			tll->tll_flags |= TLLF_SHUTDOWN_DONE;
19440Sstevel@tonic-gate 			break;
19450Sstevel@tonic-gate 		case DL_ATTACH_REQ:
19460Sstevel@tonic-gate 			message_done(tll);
19470Sstevel@tonic-gate 			break;
19480Sstevel@tonic-gate 		}
19490Sstevel@tonic-gate 		break;
19500Sstevel@tonic-gate 	}
19510Sstevel@tonic-gate 	freemsg(mp);
19520Sstevel@tonic-gate }
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate /* Search structure used with PPPoE only; see tclvm_pppoe_search(). */
19550Sstevel@tonic-gate struct poedat {
19560Sstevel@tonic-gate 	uint_t sessid;
19570Sstevel@tonic-gate 	tunll_t *tll;
19585640Scarlsonj 	const void *srcaddr;
19590Sstevel@tonic-gate 	int isdata;
19600Sstevel@tonic-gate 	tuncl_t *tcl;
19610Sstevel@tonic-gate };
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate /*
19640Sstevel@tonic-gate  * This function is called by vmem_walk from within sppptun_recv.  It
19650Sstevel@tonic-gate  * iterates over a span of allocated minor node numbers to search for
19660Sstevel@tonic-gate  * the appropriate lower stream, session ID, and peer MAC address.
19670Sstevel@tonic-gate  *
19680Sstevel@tonic-gate  * (This is necessary due to a design flaw in the PPPoE protocol
19690Sstevel@tonic-gate  * itself.  The protocol assigns session IDs from the server side
19700Sstevel@tonic-gate  * only.  Both server and client use the same number.  Thus, if there
19710Sstevel@tonic-gate  * are multiple clients on a single host, there can be session ID
19720Sstevel@tonic-gate  * conflicts between servers and there's no way to detangle them
19730Sstevel@tonic-gate  * except by looking at the remote MAC address.)
19740Sstevel@tonic-gate  *
19750Sstevel@tonic-gate  * (This could have been handled by linking together sessions that
19760Sstevel@tonic-gate  * differ only in the remote MAC address.  This isn't done because it
19770Sstevel@tonic-gate  * would involve extra per-session storage and it's very unlikely that
19780Sstevel@tonic-gate  * PPPoE would be used this way.)
19790Sstevel@tonic-gate  */
19800Sstevel@tonic-gate static void
19810Sstevel@tonic-gate tclvm_pppoe_search(void *arg, void *firstv, size_t numv)
19820Sstevel@tonic-gate {
19830Sstevel@tonic-gate 	struct poedat *poedat = (struct poedat *)arg;
19840Sstevel@tonic-gate 	int minorn = (int)(uintptr_t)firstv;
19850Sstevel@tonic-gate 	int minormax = minorn + numv;
19860Sstevel@tonic-gate 	tuncl_t *tcl;
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	if (poedat->tcl != NULL)
19890Sstevel@tonic-gate 		return;
19900Sstevel@tonic-gate 	while (minorn < minormax) {
19910Sstevel@tonic-gate 		tcl = tcl_slots[minorn - 1];
19920Sstevel@tonic-gate 		ASSERT(tcl != NULL);
19930Sstevel@tonic-gate 		if (tcl->tcl_rsessid == poedat->sessid &&
19940Sstevel@tonic-gate 		    ((!poedat->isdata && tcl->tcl_ctrl_tll == poedat->tll) ||
19955640Scarlsonj 		    (poedat->isdata && tcl->tcl_data_tll == poedat->tll)) &&
19960Sstevel@tonic-gate 		    bcmp(tcl->tcl_address.pta_pppoe.ptma_mac,
19975640Scarlsonj 		    poedat->srcaddr,
19985640Scarlsonj 		    sizeof (tcl->tcl_address.pta_pppoe.ptma_mac)) == 0) {
19990Sstevel@tonic-gate 			poedat->tcl = tcl;
20000Sstevel@tonic-gate 			break;
20010Sstevel@tonic-gate 		}
20020Sstevel@tonic-gate 		minorn++;
20030Sstevel@tonic-gate 	}
20040Sstevel@tonic-gate }
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate /*
20070Sstevel@tonic-gate  * sppptun_recv()
20080Sstevel@tonic-gate  *
20090Sstevel@tonic-gate  * MT-Perimeters:
20100Sstevel@tonic-gate  *    shared inner, shared outer.
20110Sstevel@tonic-gate  *
20120Sstevel@tonic-gate  * Description:
20130Sstevel@tonic-gate  *    Receive function called by sppptun_urput, which is called when
20140Sstevel@tonic-gate  *    the lower read-side put or service procedure sends a message
20150Sstevel@tonic-gate  *    upstream to the a device user (PPP).  It attempts to find an
20160Sstevel@tonic-gate  *    appropriate queue on the module above us (depending on what the
20170Sstevel@tonic-gate  *    associated upper stream for the protocol would be), and if not
20180Sstevel@tonic-gate  *    possible, it will find an upper control stream for the protocol.
20190Sstevel@tonic-gate  *    Returns a pointer to the upper queue_t, or NULL if the message
20200Sstevel@tonic-gate  *    has been discarded.
20210Sstevel@tonic-gate  *
20220Sstevel@tonic-gate  * About demultiplexing:
20230Sstevel@tonic-gate  *
20240Sstevel@tonic-gate  *	All four protocols (L2F, PPTP, L2TP, and PPPoE) support a
20250Sstevel@tonic-gate  *	locally assigned ID for demultiplexing incoming traffic.  For
20260Sstevel@tonic-gate  *	L2F, this is called the Client ID, for PPTP the Call ID, for
20270Sstevel@tonic-gate  *	L2TP the Session ID, and for PPPoE the SESSION_ID.  This is a
20280Sstevel@tonic-gate  *	16 bit number for all four protocols, and is used to directly
20290Sstevel@tonic-gate  *	index into a list of upper streams.  With the upper stream in
20300Sstevel@tonic-gate  *	hand, we verify that this is the right stream and deliver the
20310Sstevel@tonic-gate  *	data.
20320Sstevel@tonic-gate  *
20330Sstevel@tonic-gate  *	L2TP has a Tunnel ID, which represents a bundle of PPP
20340Sstevel@tonic-gate  *	sessions between the peers.  Because we always assign unique
20350Sstevel@tonic-gate  *	session ID numbers, we merely check that the given ID matches
20360Sstevel@tonic-gate  *	the assigned ID for the upper stream.
20370Sstevel@tonic-gate  *
20380Sstevel@tonic-gate  *	L2F has a Multiplex ID, which is unique per connection.  It
20390Sstevel@tonic-gate  *	does not have L2TP's concept of multiple-connections-within-
20400Sstevel@tonic-gate  *	a-tunnel.  The same checking is done.
20410Sstevel@tonic-gate  *
20420Sstevel@tonic-gate  *	PPPoE is a horribly broken protocol.  Only one ID is assigned
20430Sstevel@tonic-gate  *	per connection.  The client must somehow demultiplex based on
20440Sstevel@tonic-gate  *	an ID number assigned by the server.  It's not necessarily
20450Sstevel@tonic-gate  *	unique.  The search is done based on {ID,peerEthernet} (using
20460Sstevel@tonic-gate  *	tcl_rsessid) for all packet types except PADI and PADS.
20470Sstevel@tonic-gate  *
20480Sstevel@tonic-gate  *	Neither PPPoE nor PPTP supports additional ID numbers.
20490Sstevel@tonic-gate  *
20500Sstevel@tonic-gate  *	Both L2F and L2TP come in over UDP.  They are distinguished by
20510Sstevel@tonic-gate  *	looking at the GRE version field -- 001 for L2F and 010 for
20520Sstevel@tonic-gate  *	L2TP.
20530Sstevel@tonic-gate  */
20540Sstevel@tonic-gate static queue_t *
20555640Scarlsonj sppptun_recv(queue_t *q, mblk_t **mpp, const void *srcaddr)
20560Sstevel@tonic-gate {
20570Sstevel@tonic-gate 	mblk_t *mp;
20580Sstevel@tonic-gate 	tunll_t *tll;
20590Sstevel@tonic-gate 	tuncl_t *tcl;
20600Sstevel@tonic-gate 	int sessid;
20610Sstevel@tonic-gate 	int remlen;
20620Sstevel@tonic-gate 	int msglen;
20630Sstevel@tonic-gate 	int isdata;
20640Sstevel@tonic-gate 	int i;
20655640Scarlsonj 	const uchar_t *ucp;
20665640Scarlsonj 	const poep_t *poep;
20670Sstevel@tonic-gate 	mblk_t *mnew;
20680Sstevel@tonic-gate 	ppptun_atype *pap;
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 	mp = *mpp;
20710Sstevel@tonic-gate 
20725640Scarlsonj 	tll = q->q_ptr;
20730Sstevel@tonic-gate 	ASSERT(!(tll->tll_flags & TLLF_NOTLOWER));
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	tcl = NULL;
20760Sstevel@tonic-gate 	switch (tll->tll_style) {
20770Sstevel@tonic-gate 	case PTS_PPPOE:
20785640Scarlsonj 		/* Note that poep_t alignment is uint16_t */
20795640Scarlsonj 		if ((!IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)) ||
20805640Scarlsonj 		    MBLKL(mp) < sizeof (poep_t)) &&
20815640Scarlsonj 		    !pullupmsg(mp, sizeof (poep_t)))
20825640Scarlsonj 			break;
20835640Scarlsonj 		poep = (const poep_t *)mp->b_rptr;
20840Sstevel@tonic-gate 		if (poep->poep_version_type != POE_VERSION)
20850Sstevel@tonic-gate 			break;
20865640Scarlsonj 		/*
20875640Scarlsonj 		 * First, extract a session ID number.  All protocols have
20885640Scarlsonj 		 * this.
20895640Scarlsonj 		 */
20900Sstevel@tonic-gate 		isdata = (poep->poep_code == POECODE_DATA);
20910Sstevel@tonic-gate 		sessid = ntohs(poep->poep_session_id);
20920Sstevel@tonic-gate 		remlen = sizeof (*poep);
20930Sstevel@tonic-gate 		msglen = ntohs(poep->poep_length);
20940Sstevel@tonic-gate 		i = poep->poep_code;
20950Sstevel@tonic-gate 		if (i == POECODE_PADI || i == POECODE_PADR) {
20960Sstevel@tonic-gate 			/* These go to the server daemon only. */
20970Sstevel@tonic-gate 			tcl = tll->tll_defcl;
20980Sstevel@tonic-gate 		} else if (i == POECODE_PADO || i == POECODE_PADS) {
20990Sstevel@tonic-gate 			/*
21000Sstevel@tonic-gate 			 * These go to a client only, and are demuxed
21010Sstevel@tonic-gate 			 * by the Host-Uniq field (into which we stuff
21020Sstevel@tonic-gate 			 * our local ID number when generating
21030Sstevel@tonic-gate 			 * PADI/PADR).
21040Sstevel@tonic-gate 			 */
21055640Scarlsonj 			ucp = (const uchar_t *)(poep + 1);
21060Sstevel@tonic-gate 			i = msglen;
21070Sstevel@tonic-gate 			while (i > POET_HDRLEN) {
21080Sstevel@tonic-gate 				if (POET_GET_TYPE(ucp) == POETT_END) {
21090Sstevel@tonic-gate 					i = 0;
21100Sstevel@tonic-gate 					break;
21110Sstevel@tonic-gate 				}
21120Sstevel@tonic-gate 				if (POET_GET_TYPE(ucp) == POETT_UNIQ &&
21130Sstevel@tonic-gate 				    POET_GET_LENG(ucp) >= sizeof (uint32_t))
21140Sstevel@tonic-gate 					break;
21150Sstevel@tonic-gate 				i -= POET_GET_LENG(ucp) + POET_HDRLEN;
21160Sstevel@tonic-gate 				ucp = POET_NEXT(ucp);
21170Sstevel@tonic-gate 			}
21180Sstevel@tonic-gate 			if (i >= POET_HDRLEN + 4)
21190Sstevel@tonic-gate 				sessid = GETLONG(ucp + POET_HDRLEN);
21200Sstevel@tonic-gate 			tcl = tcl_by_minor((minor_t)sessid);
21210Sstevel@tonic-gate 		} else {
21220Sstevel@tonic-gate 			/*
21230Sstevel@tonic-gate 			 * Try minor number as session ID first, since
21240Sstevel@tonic-gate 			 * it's used that way on server side.  It's
21250Sstevel@tonic-gate 			 * not used that way on the client, though, so
21260Sstevel@tonic-gate 			 * this might not work.  If this isn't the
21270Sstevel@tonic-gate 			 * right one, then try the tll cache.  If
21280Sstevel@tonic-gate 			 * neither is right, then search all open
21290Sstevel@tonic-gate 			 * clients.  Did I mention that the PPPoE
21300Sstevel@tonic-gate 			 * protocol is badly designed?
21310Sstevel@tonic-gate 			 */
21320Sstevel@tonic-gate 			tcl = tcl_by_minor((minor_t)sessid);
21330Sstevel@tonic-gate 			if (tcl == NULL ||
21340Sstevel@tonic-gate 			    (!isdata && tcl->tcl_ctrl_tll != tll) ||
21350Sstevel@tonic-gate 			    (isdata && tcl->tcl_data_tll != tll) ||
21360Sstevel@tonic-gate 			    sessid != tcl->tcl_rsessid ||
21370Sstevel@tonic-gate 			    bcmp(srcaddr, tcl->tcl_address.pta_pppoe.ptma_mac,
21385640Scarlsonj 			    sizeof (tcl->tcl_address.pta_pppoe.ptma_mac)) != 0)
21390Sstevel@tonic-gate 				tcl = tll->tll_lastcl;
21400Sstevel@tonic-gate 			if (tcl == NULL ||
21410Sstevel@tonic-gate 			    (!isdata && tcl->tcl_ctrl_tll != tll) ||
21420Sstevel@tonic-gate 			    (isdata && tcl->tcl_data_tll != tll) ||
21430Sstevel@tonic-gate 			    sessid != tcl->tcl_rsessid ||
21440Sstevel@tonic-gate 			    bcmp(srcaddr, tcl->tcl_address.pta_pppoe.ptma_mac,
21455640Scarlsonj 			    sizeof (tcl->tcl_address.pta_pppoe.ptma_mac)) != 0)
21460Sstevel@tonic-gate 				tcl = NULL;
21470Sstevel@tonic-gate 			if (tcl == NULL && sessid != 0) {
21480Sstevel@tonic-gate 				struct poedat poedat;
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 				/*
21510Sstevel@tonic-gate 				 * Slow mode.  Too bad.  If you don't like it,
21520Sstevel@tonic-gate 				 * you can always choose a better protocol.
21530Sstevel@tonic-gate 				 */
21540Sstevel@tonic-gate 				poedat.sessid = sessid;
21550Sstevel@tonic-gate 				poedat.tll = tll;
21560Sstevel@tonic-gate 				poedat.srcaddr = srcaddr;
21570Sstevel@tonic-gate 				poedat.tcl = NULL;
21580Sstevel@tonic-gate 				poedat.isdata = isdata;
21590Sstevel@tonic-gate 				vmem_walk(tcl_minor_arena, VMEM_ALLOC,
21600Sstevel@tonic-gate 				    tclvm_pppoe_search, &poedat);
21610Sstevel@tonic-gate 				KLINCR(lks_walks);
21620Sstevel@tonic-gate 				if ((tcl = poedat.tcl) != NULL) {
21630Sstevel@tonic-gate 					tll->tll_lastcl = tcl;
21640Sstevel@tonic-gate 					KCINCR(cks_walks);
21650Sstevel@tonic-gate 				}
21660Sstevel@tonic-gate 			}
21670Sstevel@tonic-gate 		}
21680Sstevel@tonic-gate 		break;
21690Sstevel@tonic-gate 	}
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 	if (tcl == NULL || tcl->tcl_rq == NULL) {
21725640Scarlsonj 		DTRACE_PROBE3(sppptun__recv__discard, int, sessid,
21735640Scarlsonj 		    tuncl_t *, tcl, mblk_t *, mp);
21740Sstevel@tonic-gate 		if (tcl == NULL) {
21750Sstevel@tonic-gate 			KLINCR(lks_in_nomatch);
21760Sstevel@tonic-gate 		}
21770Sstevel@tonic-gate 		if (isdata) {
21780Sstevel@tonic-gate 			KLINCR(lks_indata_drops);
21790Sstevel@tonic-gate 			if (tcl != NULL)
21800Sstevel@tonic-gate 				tcl->tcl_stats.ppp_ierrors++;
21810Sstevel@tonic-gate 		} else {
21820Sstevel@tonic-gate 			KLINCR(lks_inctrl_drops);
21830Sstevel@tonic-gate 			if (tcl != NULL) {
21840Sstevel@tonic-gate 				KCINCR(cks_inctrl_drops);
21850Sstevel@tonic-gate 			}
21860Sstevel@tonic-gate 		}
21870Sstevel@tonic-gate 		freemsg(mp);
21880Sstevel@tonic-gate 		return (NULL);
21890Sstevel@tonic-gate 	}
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	if (tcl->tcl_data_tll == tll && isdata) {
21920Sstevel@tonic-gate 		if (!adjmsg(mp, remlen) ||
21930Sstevel@tonic-gate 		    (i = msgsize(mp)) < msglen ||
21940Sstevel@tonic-gate 		    (i > msglen && !adjmsg(mp, msglen - i))) {
21950Sstevel@tonic-gate 			KLINCR(lks_indata_drops);
21960Sstevel@tonic-gate 			tcl->tcl_stats.ppp_ierrors++;
21970Sstevel@tonic-gate 			freemsg(mp);
21980Sstevel@tonic-gate 			return (NULL);
21990Sstevel@tonic-gate 		}
22000Sstevel@tonic-gate 		/* XXX -- address/control handling in pppd needs help. */
22010Sstevel@tonic-gate 		if (*mp->b_rptr != 0xFF) {
22020Sstevel@tonic-gate 			if ((mp = prependb(mp, 2, 1)) == NULL) {
22030Sstevel@tonic-gate 				KLINCR(lks_indata_drops);
22040Sstevel@tonic-gate 				tcl->tcl_stats.ppp_ierrors++;
22050Sstevel@tonic-gate 				return (NULL);
22060Sstevel@tonic-gate 			}
22070Sstevel@tonic-gate 			mp->b_rptr[0] = 0xFF;
22080Sstevel@tonic-gate 			mp->b_rptr[1] = 0x03;
22090Sstevel@tonic-gate 		}
22100Sstevel@tonic-gate 		MTYPE(mp) = M_DATA;
22110Sstevel@tonic-gate 		tcl->tcl_stats.ppp_ibytes += msgsize(mp);
22120Sstevel@tonic-gate 		tcl->tcl_stats.ppp_ipackets++;
22130Sstevel@tonic-gate 		KLINCR(lks_indata);
22140Sstevel@tonic-gate 	} else {
22150Sstevel@tonic-gate 		if (isdata || tcl->tcl_ctrl_tll != tll ||
22160Sstevel@tonic-gate 		    (mnew = make_control(tcl, tll, PTCA_CONTROL, tcl)) ==
22170Sstevel@tonic-gate 		    NULL) {
22180Sstevel@tonic-gate 			KLINCR(lks_inctrl_drops);
22190Sstevel@tonic-gate 			KCINCR(cks_inctrl_drops);
22200Sstevel@tonic-gate 			freemsg(mp);
22210Sstevel@tonic-gate 			return (NULL);
22220Sstevel@tonic-gate 		}
22230Sstevel@tonic-gate 		/* Fix up source address; peer might not be set yet. */
22240Sstevel@tonic-gate 		pap = &((struct ppptun_control *)mnew->b_rptr)->ptc_address;
22250Sstevel@tonic-gate 		bcopy(srcaddr, pap->pta_pppoe.ptma_mac,
22260Sstevel@tonic-gate 		    sizeof (pap->pta_pppoe.ptma_mac));
22270Sstevel@tonic-gate 		mnew->b_cont = mp;
22280Sstevel@tonic-gate 		mp = mnew;
22290Sstevel@tonic-gate 		KLINCR(lks_inctrls);
22300Sstevel@tonic-gate 		KCINCR(cks_inctrls);
22310Sstevel@tonic-gate 	}
22320Sstevel@tonic-gate 	*mpp = mp;
22330Sstevel@tonic-gate 	return (tcl->tcl_rq);
22340Sstevel@tonic-gate }
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate /*
22370Sstevel@tonic-gate  * sppptun_urput()
22380Sstevel@tonic-gate  *
22390Sstevel@tonic-gate  * MT-Perimeters:
22400Sstevel@tonic-gate  *    shared inner, shared outer.
22410Sstevel@tonic-gate  *
22420Sstevel@tonic-gate  * Description:
22430Sstevel@tonic-gate  *    Upper read-side put procedure.  Messages from the underlying
22440Sstevel@tonic-gate  *    lower stream driver arrive here.  See sppptun_recv for the
22450Sstevel@tonic-gate  *    demultiplexing logic.
22460Sstevel@tonic-gate  */
22470Sstevel@tonic-gate static void
22480Sstevel@tonic-gate sppptun_urput(queue_t *q, mblk_t *mp)
22490Sstevel@tonic-gate {
22500Sstevel@tonic-gate 	union DL_primitives *dlprim;
22510Sstevel@tonic-gate 	mblk_t *mpnext;
22520Sstevel@tonic-gate 	tunll_t *tll;
22530Sstevel@tonic-gate 	queue_t *nextq;
22540Sstevel@tonic-gate 
22555640Scarlsonj 	tll = q->q_ptr;
22560Sstevel@tonic-gate 	ASSERT(!(tll->tll_flags & TLLF_NOTLOWER));
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 	switch (MTYPE(mp)) {
22590Sstevel@tonic-gate 	case M_DATA:
22600Sstevel@tonic-gate 		/*
22610Sstevel@tonic-gate 		 * When we're bound over IP, data arrives here.  The
22620Sstevel@tonic-gate 		 * packet starts with the IP header itself.
22630Sstevel@tonic-gate 		 */
22645640Scarlsonj 		if ((nextq = sppptun_recv(q, &mp, NULL)) != NULL)
22650Sstevel@tonic-gate 			putnext(nextq, mp);
22660Sstevel@tonic-gate 		break;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 	case M_PROTO:
22690Sstevel@tonic-gate 	case M_PCPROTO:
22700Sstevel@tonic-gate 		/* Data arrives here for UDP or raw Ethernet, not IP. */
22710Sstevel@tonic-gate 		switch (tll->tll_style) {
22720Sstevel@tonic-gate 			/* PPTP control messages are over TCP only. */
22730Sstevel@tonic-gate 		case PTS_PPTP:
22740Sstevel@tonic-gate 		default:
22750Sstevel@tonic-gate 			ASSERT(0);	/* how'd that happen? */
22760Sstevel@tonic-gate 			break;
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 		case PTS_PPPOE:		/* DLPI message */
22795640Scarlsonj 			if (MBLKL(mp) < sizeof (t_uscalar_t))
22805640Scarlsonj 				break;
22810Sstevel@tonic-gate 			dlprim = (union DL_primitives *)mp->b_rptr;
22820Sstevel@tonic-gate 			switch (dlprim->dl_primitive) {
22835640Scarlsonj 			case DL_UNITDATA_IND: {
22845640Scarlsonj 				size_t mlen = MBLKL(mp);
22855640Scarlsonj 
22865640Scarlsonj 				if (mlen < DL_UNITDATA_IND_SIZE)
22875640Scarlsonj 					break;
22885640Scarlsonj 				if (dlprim->unitdata_ind.dl_src_addr_offset <
22895640Scarlsonj 				    DL_UNITDATA_IND_SIZE ||
22905640Scarlsonj 				    dlprim->unitdata_ind.dl_src_addr_offset +
22915640Scarlsonj 				    dlprim->unitdata_ind.dl_src_addr_length >
22925640Scarlsonj 				    mlen)
22935640Scarlsonj 					break;
22945640Scarlsonj 			}
22955640Scarlsonj 				/* FALLTHROUGH */
22965640Scarlsonj 			case DL_UNITDATA_REQ:	/* For loopback support. */
22975640Scarlsonj 				if (dlprim->dl_primitive == DL_UNITDATA_REQ &&
22985640Scarlsonj 				    MBLKL(mp) < DL_UNITDATA_REQ_SIZE)
22995640Scarlsonj 					break;
23005640Scarlsonj 				if ((mpnext = mp->b_cont) == NULL)
23015640Scarlsonj 					break;
23020Sstevel@tonic-gate 				MTYPE(mpnext) = M_DATA;
23030Sstevel@tonic-gate 				nextq = sppptun_recv(q, &mpnext,
23045640Scarlsonj 				    dlprim->dl_primitive == DL_UNITDATA_IND ?
23050Sstevel@tonic-gate 				    mp->b_rptr +
23065640Scarlsonj 				    dlprim->unitdata_ind.dl_src_addr_offset :
23070Sstevel@tonic-gate 				    tll->tll_lcladdr.pta_pppoe.ptma_mac);
23080Sstevel@tonic-gate 				if (nextq != NULL)
23090Sstevel@tonic-gate 					putnext(nextq, mpnext);
23100Sstevel@tonic-gate 				freeb(mp);
23115640Scarlsonj 				return;
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 			default:
23140Sstevel@tonic-gate 				urput_dlpi(q, mp);
23155640Scarlsonj 				return;
23160Sstevel@tonic-gate 			}
23170Sstevel@tonic-gate 			break;
23180Sstevel@tonic-gate 		}
23195640Scarlsonj 		freemsg(mp);
23200Sstevel@tonic-gate 		break;
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 	default:
23230Sstevel@tonic-gate 		freemsg(mp);
23240Sstevel@tonic-gate 		break;
23250Sstevel@tonic-gate 	}
23260Sstevel@tonic-gate }
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate /*
23290Sstevel@tonic-gate  * sppptun_ursrv()
23300Sstevel@tonic-gate  *
23310Sstevel@tonic-gate  * MT-Perimeters:
23320Sstevel@tonic-gate  *    exclusive inner, shared outer.
23330Sstevel@tonic-gate  *
23340Sstevel@tonic-gate  * Description:
23350Sstevel@tonic-gate  *    Upper read-side service procedure.  This procedure services the
23360Sstevel@tonic-gate  *    client streams.  We get here because the client (PPP) asserts
23370Sstevel@tonic-gate  *    flow control down to us.
23380Sstevel@tonic-gate  */
23390Sstevel@tonic-gate static int
23400Sstevel@tonic-gate sppptun_ursrv(queue_t *q)
23410Sstevel@tonic-gate {
23420Sstevel@tonic-gate 	mblk_t		*mp;
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 	ASSERT(q->q_ptr != NULL);
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
23470Sstevel@tonic-gate 		if (canputnext(q)) {
23480Sstevel@tonic-gate 			putnext(q, mp);
23490Sstevel@tonic-gate 		} else {
23500Sstevel@tonic-gate 			(void) putbq(q, mp);
23510Sstevel@tonic-gate 			break;
23520Sstevel@tonic-gate 		}
23530Sstevel@tonic-gate 	}
23540Sstevel@tonic-gate 	return (0);
23550Sstevel@tonic-gate }
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate /*
23580Sstevel@tonic-gate  * Dummy constructor/destructor functions for kmem_cache_create.
23590Sstevel@tonic-gate  * We're just using kmem as an allocator of integers, not real
23600Sstevel@tonic-gate  * storage.
23610Sstevel@tonic-gate  */
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate /*ARGSUSED*/
23640Sstevel@tonic-gate static int
23650Sstevel@tonic-gate tcl_constructor(void *maddr, void *arg, int kmflags)
23660Sstevel@tonic-gate {
23670Sstevel@tonic-gate 	return (0);
23680Sstevel@tonic-gate }
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate /*ARGSUSED*/
23710Sstevel@tonic-gate static void
23720Sstevel@tonic-gate tcl_destructor(void *maddr, void *arg)
23730Sstevel@tonic-gate {
23740Sstevel@tonic-gate }
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate /*
23770Sstevel@tonic-gate  * Total size occupied by one tunnel client.  Each tunnel client
23780Sstevel@tonic-gate  * consumes one pointer for tcl_slots array, one tuncl_t structure and
23790Sstevel@tonic-gate  * two messages preallocated for close.
23800Sstevel@tonic-gate  */
23810Sstevel@tonic-gate #define	TUNCL_SIZE (sizeof (tuncl_t) + sizeof (tuncl_t *) + \
23820Sstevel@tonic-gate 			2 * sizeof (dblk_t))
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate /*
23850Sstevel@tonic-gate  * Clear all bits of x except the highest bit
23860Sstevel@tonic-gate  */
23870Sstevel@tonic-gate #define	truncate(x) 	((x) <= 2 ? (x) : (1 << (highbit(x) - 1)))
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate /*
23900Sstevel@tonic-gate  * This function initializes some well-known global variables inside
23910Sstevel@tonic-gate  * the module.
23920Sstevel@tonic-gate  *
23930Sstevel@tonic-gate  * Called by sppptun_mod.c:_init() before installing the module.
23940Sstevel@tonic-gate  */
23950Sstevel@tonic-gate void
23960Sstevel@tonic-gate sppptun_init(void)
23970Sstevel@tonic-gate {
23980Sstevel@tonic-gate 	tunll_list.q_forw = tunll_list.q_back = &tunll_list;
23990Sstevel@tonic-gate }
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate /*
24020Sstevel@tonic-gate  * This function allocates the initial internal storage for the
24030Sstevel@tonic-gate  * sppptun driver.
24040Sstevel@tonic-gate  *
24050Sstevel@tonic-gate  * Called by sppptun_mod.c:_init() after installing module.
24060Sstevel@tonic-gate  */
24070Sstevel@tonic-gate void
24080Sstevel@tonic-gate sppptun_tcl_init(void)
24090Sstevel@tonic-gate {
24100Sstevel@tonic-gate 	uint_t i, j;
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 	rw_init(&tcl_rwlock, NULL, RW_DRIVER, NULL);
24130Sstevel@tonic-gate 	rw_enter(&tcl_rwlock, RW_WRITER);
24140Sstevel@tonic-gate 	tcl_nslots = sppptun_init_cnt;
24150Sstevel@tonic-gate 	tcl_slots = kmem_zalloc(tcl_nslots * sizeof (tuncl_t *), KM_SLEEP);
24160Sstevel@tonic-gate 
24170Sstevel@tonic-gate 	tcl_cache = kmem_cache_create("sppptun_map", sizeof (tuncl_t), 0,
24180Sstevel@tonic-gate 	    tcl_constructor, tcl_destructor, NULL, NULL, NULL, 0);
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 	/* Allocate integer space for minor numbers */
24210Sstevel@tonic-gate 	tcl_minor_arena = vmem_create("sppptun_minor", (void *)1, tcl_nslots,
24220Sstevel@tonic-gate 	    1, NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	/*
24250Sstevel@tonic-gate 	 * Calculate available number of tunnels - how many tunnels
24260Sstevel@tonic-gate 	 * can we allocate in sppptun_pctofmem % of available
24270Sstevel@tonic-gate 	 * memory.  The value is rounded up to the nearest power of 2.
24280Sstevel@tonic-gate 	 */
24290Sstevel@tonic-gate 	i = (sppptun_pctofmem * kmem_maxavail()) / (100 * TUNCL_SIZE);
24300Sstevel@tonic-gate 	j = truncate(i);	/* i with non-high bits stripped */
24310Sstevel@tonic-gate 	if (i != j)
24320Sstevel@tonic-gate 		j *= 2;
24330Sstevel@tonic-gate 	tcl_minormax = j;
24340Sstevel@tonic-gate 	rw_exit(&tcl_rwlock);
24350Sstevel@tonic-gate }
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate /*
24380Sstevel@tonic-gate  * This function checks that there are no plumbed streams or other users.
24390Sstevel@tonic-gate  *
24400Sstevel@tonic-gate  * Called by sppptun_mod.c:_fini().  Assumes that we're exclusive on
24410Sstevel@tonic-gate  * both perimeters.
24420Sstevel@tonic-gate  */
24430Sstevel@tonic-gate int
24440Sstevel@tonic-gate sppptun_tcl_fintest(void)
24450Sstevel@tonic-gate {
24465640Scarlsonj 	if (tunll_list.q_forw != &tunll_list || tcl_inuse > 0)
24470Sstevel@tonic-gate 		return (EBUSY);
24485640Scarlsonj 	else
24495640Scarlsonj 		return (0);
24500Sstevel@tonic-gate }
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate /*
24530Sstevel@tonic-gate  * If no lower streams are plumbed, then this function deallocates all
24540Sstevel@tonic-gate  * internal storage in preparation for unload.
24550Sstevel@tonic-gate  *
24560Sstevel@tonic-gate  * Called by sppptun_mod.c:_fini().  Assumes that we're exclusive on
24570Sstevel@tonic-gate  * both perimeters.
24580Sstevel@tonic-gate  */
24590Sstevel@tonic-gate void
24600Sstevel@tonic-gate sppptun_tcl_fini(void)
24610Sstevel@tonic-gate {
24620Sstevel@tonic-gate 	if (tcl_minor_arena != NULL) {
24630Sstevel@tonic-gate 		vmem_destroy(tcl_minor_arena);
24640Sstevel@tonic-gate 		tcl_minor_arena = NULL;
24650Sstevel@tonic-gate 	}
24660Sstevel@tonic-gate 	if (tcl_cache != NULL) {
24670Sstevel@tonic-gate 		kmem_cache_destroy(tcl_cache);
24680Sstevel@tonic-gate 		tcl_cache = NULL;
24690Sstevel@tonic-gate 	}
24700Sstevel@tonic-gate 	kmem_free(tcl_slots, tcl_nslots * sizeof (tuncl_t *));
24710Sstevel@tonic-gate 	tcl_slots = NULL;
24720Sstevel@tonic-gate 	rw_destroy(&tcl_rwlock);
24730Sstevel@tonic-gate 	ASSERT(tcl_slots == NULL);
24740Sstevel@tonic-gate 	ASSERT(tcl_cache == NULL);
24750Sstevel@tonic-gate 	ASSERT(tcl_minor_arena == NULL);
24760Sstevel@tonic-gate }
2477