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