xref: /onnv-gate/usr/src/uts/common/io/dls/dls_link.c (revision 5733:df23d322484c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51502Sericheng  * Common Development and Distribution License (the "License").
61502Sericheng  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
224913Sethindra  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Data-Link Services Module
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include	<sys/types.h>
330Sstevel@tonic-gate #include	<sys/stream.h>
340Sstevel@tonic-gate #include	<sys/strsun.h>
350Sstevel@tonic-gate #include	<sys/strsubr.h>
360Sstevel@tonic-gate #include	<sys/sysmacros.h>
370Sstevel@tonic-gate #include	<sys/atomic.h>
38269Sericheng #include	<sys/modhash.h>
390Sstevel@tonic-gate #include	<sys/dlpi.h>
400Sstevel@tonic-gate #include	<sys/ethernet.h>
410Sstevel@tonic-gate #include	<sys/byteorder.h>
420Sstevel@tonic-gate #include	<sys/vlan.h>
430Sstevel@tonic-gate #include	<sys/mac.h>
440Sstevel@tonic-gate #include	<sys/sdt.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include	<sys/dls.h>
470Sstevel@tonic-gate #include	<sys/dld_impl.h>
480Sstevel@tonic-gate #include	<sys/dls_impl.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static kmem_cache_t	*i_dls_link_cachep;
51269Sericheng static mod_hash_t	*i_dls_link_hash;
52269Sericheng static uint_t		i_dls_link_count;
53269Sericheng static krwlock_t	i_dls_link_lock;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define		LINK_HASHSZ	67	/* prime */
560Sstevel@tonic-gate #define		IMPL_HASHSZ	67	/* prime */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * Construct a hash key encompassing both DLSAP value and VLAN idenitifier.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate #define	MAKE_KEY(_sap, _vid)						\
62269Sericheng 	((mod_hash_key_t)(uintptr_t)					\
63269Sericheng 	(((_sap) << VLAN_ID_SIZE) | (_vid) & VLAN_ID_MASK))
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * Extract the DLSAP value from the hash key.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate #define	KEY_SAP(_key)							\
690Sstevel@tonic-gate 	(((uint32_t)(uintptr_t)(_key)) >> VLAN_ID_SIZE)
700Sstevel@tonic-gate 
712311Sseb #define	DLS_STRIP_PADDING(pktsize, p) {			\
722311Sseb 	if (pktsize != 0) {				\
732311Sseb 		ssize_t delta = pktsize - msgdsize(p);	\
742311Sseb 							\
752311Sseb 		if (delta < 0)				\
762311Sseb 			(void) adjmsg(p, delta);	\
772311Sseb 	}						\
782311Sseb }
792311Sseb 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * Private functions.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*ARGSUSED*/
850Sstevel@tonic-gate static int
860Sstevel@tonic-gate i_dls_link_constructor(void *buf, void *arg, int kmflag)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	dls_link_t	*dlp = buf;
890Sstevel@tonic-gate 	char		name[MAXNAMELEN];
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	bzero(buf, sizeof (dls_link_t));
920Sstevel@tonic-gate 
93269Sericheng 	(void) sprintf(name, "dls_link_t_%p_hash", buf);
94269Sericheng 	dlp->dl_impl_hash = mod_hash_create_idhash(name, IMPL_HASHSZ,
95269Sericheng 	    mod_hash_null_valdtor);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	mutex_init(&dlp->dl_lock, NULL, MUTEX_DEFAULT, NULL);
9856Smeem 	mutex_init(&dlp->dl_promisc_lock, NULL, MUTEX_DEFAULT, NULL);
99269Sericheng 	rw_init(&dlp->dl_impl_lock, NULL, RW_DEFAULT, NULL);
1000Sstevel@tonic-gate 	return (0);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*ARGSUSED*/
1040Sstevel@tonic-gate static void
1050Sstevel@tonic-gate i_dls_link_destructor(void *buf, void *arg)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	dls_link_t	*dlp = buf;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	ASSERT(dlp->dl_ref == 0);
1100Sstevel@tonic-gate 	ASSERT(dlp->dl_mh == NULL);
1110Sstevel@tonic-gate 	ASSERT(dlp->dl_unknowns == 0);
1120Sstevel@tonic-gate 
113269Sericheng 	mod_hash_destroy_idhash(dlp->dl_impl_hash);
114269Sericheng 	dlp->dl_impl_hash = NULL;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	mutex_destroy(&dlp->dl_lock);
11756Smeem 	mutex_destroy(&dlp->dl_promisc_lock);
118269Sericheng 	rw_destroy(&dlp->dl_impl_lock);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate 
1212311Sseb /*
1222760Sdg199075  * - Parse the mac header information of the given packet.
1232760Sdg199075  * - Strip the padding and skip over the header. Note that because some
1242760Sdg199075  *   DLS consumers only check the db_ref count of the first mblk, we
1253037Syz147064  *   pullup the message into a single mblk. Because the original message
1263037Syz147064  *   is freed as the result of message pulling up, dls_link_header_info()
1273037Syz147064  *   is called again to update the mhi_saddr and mhi_daddr pointers in the
1283037Syz147064  *   mhip. Further, the dls_link_header_info() function ensures that the
1293037Syz147064  *   size of the pulled message is greater than the MAC header size,
1303037Syz147064  *   therefore we can directly advance b_rptr to point at the payload.
1312760Sdg199075  *
1322760Sdg199075  * We choose to use a macro for performance reasons.
1332760Sdg199075  */
1342760Sdg199075 #define	DLS_PREPARE_PKT(dlp, mp, mhip, err) {				\
1352760Sdg199075 	mblk_t *nextp = (mp)->b_next;					\
1362760Sdg199075 	if (((err) = dls_link_header_info((dlp), (mp), (mhip))) == 0) {	\
1372760Sdg199075 		DLS_STRIP_PADDING((mhip)->mhi_pktsize, (mp));		\
1382760Sdg199075 		if (MBLKL((mp)) < (mhip)->mhi_hdrsize) {		\
1392760Sdg199075 			mblk_t *newmp;					\
1402760Sdg199075 			if ((newmp = msgpullup((mp), -1)) == NULL) {	\
1412760Sdg199075 				(err) = EINVAL;				\
1422760Sdg199075 			} else {					\
1433037Syz147064 				(mp)->b_next = NULL;			\
1442760Sdg199075 				freemsg((mp));				\
1452760Sdg199075 				(mp) = newmp;				\
1463037Syz147064 				VERIFY(dls_link_header_info((dlp),	\
1473037Syz147064 				    (mp), (mhip)) == 0);		\
1482760Sdg199075 				(mp)->b_next = nextp;			\
1492760Sdg199075 				(mp)->b_rptr += (mhip)->mhi_hdrsize;	\
1502760Sdg199075 			}						\
1512760Sdg199075 		} else {						\
1522760Sdg199075 			(mp)->b_rptr += (mhip)->mhi_hdrsize;		\
1532760Sdg199075 		}							\
1542760Sdg199075 	}								\
1552760Sdg199075 }
1562760Sdg199075 
1572760Sdg199075 /*
1582311Sseb  * Truncate the chain starting at mp such that all packets in the chain
1592760Sdg199075  * have identical source and destination addresses, saps, and tag types
1602760Sdg199075  * (see below).  It returns a pointer to the mblk following the chain,
1612760Sdg199075  * NULL if there is no further packet following the processed chain.
1622760Sdg199075  * The countp argument is set to the number of valid packets in the chain.
1632760Sdg199075  * Note that the whole MAC header (including the VLAN tag if any) in each
1642760Sdg199075  * packet will be stripped.
1652311Sseb  */
1660Sstevel@tonic-gate static mblk_t *
1672760Sdg199075 i_dls_link_subchain(dls_link_t *dlp, mblk_t *mp, const mac_header_info_t *mhip,
1682760Sdg199075     uint_t *countp)
1690Sstevel@tonic-gate {
1702760Sdg199075 	mblk_t		*prevp;
1712760Sdg199075 	uint_t		npacket = 1;
1722311Sseb 	size_t		addr_size = dlp->dl_mip->mi_addr_length;
1732760Sdg199075 	uint16_t	vid = VLAN_ID(mhip->mhi_tci);
1742760Sdg199075 	uint16_t	pri = VLAN_PRI(mhip->mhi_tci);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/*
1770Sstevel@tonic-gate 	 * Compare with subsequent headers until we find one that has
1781502Sericheng 	 * differing header information. After checking each packet
1791502Sericheng 	 * strip padding and skip over the header.
1800Sstevel@tonic-gate 	 */
1812760Sdg199075 	for (prevp = mp; (mp = mp->b_next) != NULL; prevp = mp) {
1822311Sseb 		mac_header_info_t cmhi;
1832760Sdg199075 		uint16_t cvid, cpri;
1842760Sdg199075 		int err;
1852311Sseb 
1862760Sdg199075 		DLS_PREPARE_PKT(dlp, mp, &cmhi, err);
1872760Sdg199075 		if (err != 0)
1880Sstevel@tonic-gate 			break;
1892311Sseb 
1902760Sdg199075 		prevp->b_next = mp;
1912760Sdg199075 
1922311Sseb 		/*
1932311Sseb 		 * The source, destination, sap, and vlan id must all match
1942311Sseb 		 * in a given subchain.
1952311Sseb 		 */
1962311Sseb 		if (memcmp(mhip->mhi_daddr, cmhi.mhi_daddr, addr_size) != 0 ||
1972311Sseb 		    memcmp(mhip->mhi_saddr, cmhi.mhi_saddr, addr_size) != 0 ||
1982311Sseb 		    mhip->mhi_bindsap != cmhi.mhi_bindsap) {
1992760Sdg199075 			/*
2002760Sdg199075 			 * Note that we don't need to restore the padding.
2012760Sdg199075 			 */
2022760Sdg199075 			mp->b_rptr -= cmhi.mhi_hdrsize;
2032311Sseb 			break;
2042311Sseb 		}
2052311Sseb 
2062760Sdg199075 		cvid = VLAN_ID(cmhi.mhi_tci);
2072760Sdg199075 		cpri = VLAN_PRI(cmhi.mhi_tci);
2082311Sseb 
2092760Sdg199075 		/*
2102760Sdg199075 		 * There are several types of packets. Packets don't match
2112760Sdg199075 		 * if they are classified to different type or if they are
2122760Sdg199075 		 * VLAN packets but belong to different VLANs:
2132760Sdg199075 		 *
2142760Sdg199075 		 * packet type		tagged		vid		pri
2152760Sdg199075 		 * ---------------------------------------------------------
2162760Sdg199075 		 * untagged		No		zero		zero
2172760Sdg199075 		 * VLAN packets		Yes		non-zero	-
2182760Sdg199075 		 * priority tagged	Yes		zero		non-zero
2192760Sdg199075 		 * 0 tagged		Yes		zero		zero
2202760Sdg199075 		 */
2212760Sdg199075 		if ((mhip->mhi_istagged != cmhi.mhi_istagged) ||
2222760Sdg199075 		    (vid != cvid) || ((vid == VLAN_ID_NONE) &&
2232760Sdg199075 		    (((pri == 0) && (cpri != 0)) ||
2242760Sdg199075 		    ((pri != 0) && (cpri == 0))))) {
2252760Sdg199075 			mp->b_rptr -= cmhi.mhi_hdrsize;
2262760Sdg199075 			break;
2272760Sdg199075 		}
2282760Sdg199075 
2290Sstevel@tonic-gate 		npacket++;
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	/*
2330Sstevel@tonic-gate 	 * Break the chain at this point and return a pointer to the next
2340Sstevel@tonic-gate 	 * sub-chain.
2350Sstevel@tonic-gate 	 */
2362760Sdg199075 	prevp->b_next = NULL;
2370Sstevel@tonic-gate 	*countp = npacket;
2382760Sdg199075 	return (mp);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static void
242269Sericheng i_dls_head_hold(dls_head_t *dhp)
243269Sericheng {
244269Sericheng 	atomic_inc_32(&dhp->dh_ref);
245269Sericheng }
246269Sericheng 
247269Sericheng static void
248269Sericheng i_dls_head_rele(dls_head_t *dhp)
249269Sericheng {
250269Sericheng 	atomic_dec_32(&dhp->dh_ref);
251269Sericheng }
252269Sericheng 
253269Sericheng static dls_head_t *
254269Sericheng i_dls_head_alloc(mod_hash_key_t key)
255269Sericheng {
256269Sericheng 	dls_head_t	*dhp;
257269Sericheng 
258269Sericheng 	dhp = kmem_zalloc(sizeof (dls_head_t), KM_SLEEP);
259269Sericheng 	dhp->dh_key = key;
260269Sericheng 	return (dhp);
261269Sericheng }
262269Sericheng 
263269Sericheng static void
264269Sericheng i_dls_head_free(dls_head_t *dhp)
265269Sericheng {
266269Sericheng 	ASSERT(dhp->dh_ref == 0);
267269Sericheng 	kmem_free(dhp, sizeof (dls_head_t));
268269Sericheng }
269269Sericheng 
2702760Sdg199075 /*
2712760Sdg199075  * Try to send mp up to the streams of the given sap and vid. Return B_TRUE
2722760Sdg199075  * if this message is sent to any streams.
2732760Sdg199075  * Note that this function will copy the message chain and the original
2742760Sdg199075  * mp will remain valid after this function
2752760Sdg199075  */
2762760Sdg199075 static uint_t
2772760Sdg199075 i_dls_link_rx_func(dls_link_t *dlp, mac_resource_handle_t mrh,
2782760Sdg199075     mac_header_info_t *mhip, mblk_t *mp, uint32_t sap, uint16_t vid,
2792760Sdg199075     boolean_t (*acceptfunc)())
2802760Sdg199075 {
2812760Sdg199075 	mod_hash_t	*hash = dlp->dl_impl_hash;
2822760Sdg199075 	mod_hash_key_t	key;
2832760Sdg199075 	dls_head_t	*dhp;
2842760Sdg199075 	dls_impl_t	*dip;
2852760Sdg199075 	mblk_t		*nmp;
2862760Sdg199075 	dls_rx_t	di_rx;
2872760Sdg199075 	void		*di_rx_arg;
2882760Sdg199075 	uint_t		naccepted = 0;
2892760Sdg199075 
2902760Sdg199075 	/*
2912760Sdg199075 	 * Construct a hash key from the VLAN identifier and the
2922760Sdg199075 	 * DLSAP that represents dls_impl_t in promiscuous mode.
2932760Sdg199075 	 */
2942760Sdg199075 	key = MAKE_KEY(sap, vid);
2952760Sdg199075 
2962760Sdg199075 	/*
2972760Sdg199075 	 * Search the hash table for dls_impl_t eligible to receive
2982760Sdg199075 	 * a packet chain for this DLSAP/VLAN combination.
2992760Sdg199075 	 */
3002760Sdg199075 	rw_enter(&dlp->dl_impl_lock, RW_READER);
3012760Sdg199075 	if (mod_hash_find(hash, key, (mod_hash_val_t *)&dhp) != 0) {
3022760Sdg199075 		rw_exit(&dlp->dl_impl_lock);
3032760Sdg199075 		return (B_FALSE);
3042760Sdg199075 	}
3052760Sdg199075 	i_dls_head_hold(dhp);
3062760Sdg199075 	rw_exit(&dlp->dl_impl_lock);
3072760Sdg199075 
3082760Sdg199075 	/*
3092760Sdg199075 	 * Find dls_impl_t that will accept the sub-chain.
3102760Sdg199075 	 */
3112760Sdg199075 	for (dip = dhp->dh_list; dip != NULL; dip = dip->di_nextp) {
3122760Sdg199075 		if (!acceptfunc(dip, mhip, &di_rx, &di_rx_arg))
3132760Sdg199075 			continue;
3142760Sdg199075 
3152760Sdg199075 		/*
3162760Sdg199075 		 * We have at least one acceptor.
3172760Sdg199075 		 */
3182760Sdg199075 		naccepted ++;
3192760Sdg199075 
3202760Sdg199075 		/*
3212760Sdg199075 		 * There will normally be at least more dls_impl_t
3222760Sdg199075 		 * (since we've yet to check for non-promiscuous
3232760Sdg199075 		 * dls_impl_t) so dup the sub-chain.
3242760Sdg199075 		 */
3252760Sdg199075 		if ((nmp = copymsgchain(mp)) != NULL)
3262760Sdg199075 			di_rx(di_rx_arg, mrh, nmp, mhip);
3272760Sdg199075 	}
3282760Sdg199075 
3292760Sdg199075 	/*
3302760Sdg199075 	 * Release the hold on the dls_impl_t chain now that we have
3312760Sdg199075 	 * finished walking it.
3322760Sdg199075 	 */
3332760Sdg199075 	i_dls_head_rele(dhp);
3342760Sdg199075 	return (naccepted);
3352760Sdg199075 }
3362760Sdg199075 
337269Sericheng static void
3382311Sseb i_dls_link_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	dls_link_t			*dlp = arg;
341269Sericheng 	mod_hash_t			*hash = dlp->dl_impl_hash;
3420Sstevel@tonic-gate 	mblk_t				*nextp;
3432311Sseb 	mac_header_info_t		mhi;
344269Sericheng 	dls_head_t			*dhp;
3450Sstevel@tonic-gate 	dls_impl_t			*dip;
3460Sstevel@tonic-gate 	dls_impl_t			*ndip;
3470Sstevel@tonic-gate 	mblk_t				*nmp;
348269Sericheng 	mod_hash_key_t			key;
3490Sstevel@tonic-gate 	uint_t				npacket;
3500Sstevel@tonic-gate 	boolean_t			accepted;
351449Sericheng 	dls_rx_t			di_rx, ndi_rx;
352449Sericheng 	void				*di_rx_arg, *ndi_rx_arg;
3532760Sdg199075 	uint16_t			vid;
3542760Sdg199075 	int				err;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/*
3570Sstevel@tonic-gate 	 * Walk the packet chain.
3580Sstevel@tonic-gate 	 */
3592760Sdg199075 	for (; mp != NULL; mp = nextp) {
3600Sstevel@tonic-gate 		/*
3610Sstevel@tonic-gate 		 * Wipe the accepted state.
3620Sstevel@tonic-gate 		 */
3630Sstevel@tonic-gate 		accepted = B_FALSE;
3640Sstevel@tonic-gate 
3652760Sdg199075 		DLS_PREPARE_PKT(dlp, mp, &mhi, err);
3662760Sdg199075 		if (err != 0) {
3672760Sdg199075 			atomic_add_32(&(dlp->dl_unknowns), 1);
3682760Sdg199075 			nextp = mp->b_next;
3693037Syz147064 			mp->b_next = NULL;
3702760Sdg199075 			freemsg(mp);
3712760Sdg199075 			continue;
3722760Sdg199075 		}
3732760Sdg199075 
3740Sstevel@tonic-gate 		/*
3750Sstevel@tonic-gate 		 * Grab the longest sub-chain we can process as a single
3760Sstevel@tonic-gate 		 * unit.
3770Sstevel@tonic-gate 		 */
3782760Sdg199075 		nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket);
3792760Sdg199075 		ASSERT(npacket != 0);
3800Sstevel@tonic-gate 
3812760Sdg199075 		vid = VLAN_ID(mhi.mhi_tci);
3822760Sdg199075 
3832760Sdg199075 		if (mhi.mhi_istagged) {
3842311Sseb 			/*
3852760Sdg199075 			 * If it is tagged traffic, send it upstream to
3862760Sdg199075 			 * all dls_impl_t which are attached to the physical
3872760Sdg199075 			 * link and bound to SAP 0x8100.
3882311Sseb 			 */
3892760Sdg199075 			if (i_dls_link_rx_func(dlp, mrh, &mhi, mp,
3902760Sdg199075 			    ETHERTYPE_VLAN, VLAN_ID_NONE, dls_accept) > 0) {
3912760Sdg199075 				accepted = B_TRUE;
3922760Sdg199075 			}
3932760Sdg199075 
3942760Sdg199075 			/*
3952760Sdg199075 			 * Don't pass the packets up if they are tagged
3962760Sdg199075 			 * packets and:
3972760Sdg199075 			 *  - their VID and priority are both zero (invalid
3982760Sdg199075 			 *    packets).
3992760Sdg199075 			 *  - their sap is ETHERTYPE_VLAN and their VID is
4002760Sdg199075 			 *    zero as they have already been sent upstreams.
4012760Sdg199075 			 */
4022760Sdg199075 			if ((vid == VLAN_ID_NONE &&
4032760Sdg199075 			    VLAN_PRI(mhi.mhi_tci) == 0) ||
4042760Sdg199075 			    (mhi.mhi_bindsap == ETHERTYPE_VLAN &&
4052760Sdg199075 			    vid == VLAN_ID_NONE)) {
4062760Sdg199075 				freemsgchain(mp);
4072760Sdg199075 				goto loop;
4082760Sdg199075 			}
4092311Sseb 		}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		/*
4120Sstevel@tonic-gate 		 * Construct a hash key from the VLAN identifier and the
4130Sstevel@tonic-gate 		 * DLSAP.
4140Sstevel@tonic-gate 		 */
4152311Sseb 		key = MAKE_KEY(mhi.mhi_bindsap, vid);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 		/*
4180Sstevel@tonic-gate 		 * Search the has table for dls_impl_t eligible to receive
4190Sstevel@tonic-gate 		 * a packet chain for this DLSAP/VLAN combination.
4200Sstevel@tonic-gate 		 */
421269Sericheng 		rw_enter(&dlp->dl_impl_lock, RW_READER);
422269Sericheng 		if (mod_hash_find(hash, key, (mod_hash_val_t *)&dhp) != 0) {
423269Sericheng 			rw_exit(&dlp->dl_impl_lock);
4240Sstevel@tonic-gate 			freemsgchain(mp);
4250Sstevel@tonic-gate 			goto loop;
4260Sstevel@tonic-gate 		}
427269Sericheng 		i_dls_head_hold(dhp);
428269Sericheng 		rw_exit(&dlp->dl_impl_lock);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		/*
4310Sstevel@tonic-gate 		 * Find the first dls_impl_t that will accept the sub-chain.
4320Sstevel@tonic-gate 		 */
433269Sericheng 		for (dip = dhp->dh_list; dip != NULL; dip = dip->di_nextp)
4342311Sseb 			if (dls_accept(dip, &mhi, &di_rx, &di_rx_arg))
4350Sstevel@tonic-gate 				break;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 		/*
4380Sstevel@tonic-gate 		 * If we did not find any dls_impl_t willing to accept the
4390Sstevel@tonic-gate 		 * sub-chain then throw it away.
4400Sstevel@tonic-gate 		 */
4410Sstevel@tonic-gate 		if (dip == NULL) {
442269Sericheng 			i_dls_head_rele(dhp);
4430Sstevel@tonic-gate 			freemsgchain(mp);
4440Sstevel@tonic-gate 			goto loop;
4450Sstevel@tonic-gate 		}
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		/*
4480Sstevel@tonic-gate 		 * We have at least one acceptor.
4490Sstevel@tonic-gate 		 */
4500Sstevel@tonic-gate 		accepted = B_TRUE;
4510Sstevel@tonic-gate 		for (;;) {
4520Sstevel@tonic-gate 			/*
4530Sstevel@tonic-gate 			 * Find the next dls_impl_t that will accept the
4540Sstevel@tonic-gate 			 * sub-chain.
4550Sstevel@tonic-gate 			 */
4560Sstevel@tonic-gate 			for (ndip = dip->di_nextp; ndip != NULL;
4570Sstevel@tonic-gate 			    ndip = ndip->di_nextp)
4582311Sseb 				if (dls_accept(ndip, &mhi, &ndi_rx,
459449Sericheng 				    &ndi_rx_arg))
4600Sstevel@tonic-gate 					break;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 			/*
4630Sstevel@tonic-gate 			 * If there are no more dls_impl_t that are willing
4640Sstevel@tonic-gate 			 * to accept the sub-chain then we don't need to dup
4650Sstevel@tonic-gate 			 * it before handing it to the current one.
4660Sstevel@tonic-gate 			 */
4670Sstevel@tonic-gate 			if (ndip == NULL) {
4682760Sdg199075 				di_rx(di_rx_arg, mrh, mp, &mhi);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 				/*
4710Sstevel@tonic-gate 				 * Since there are no more dls_impl_t, we're
4720Sstevel@tonic-gate 				 * done.
4730Sstevel@tonic-gate 				 */
4740Sstevel@tonic-gate 				break;
4750Sstevel@tonic-gate 			}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 			/*
4780Sstevel@tonic-gate 			 * There are more dls_impl_t so dup the sub-chain.
4790Sstevel@tonic-gate 			 */
4800Sstevel@tonic-gate 			if ((nmp = copymsgchain(mp)) != NULL)
4812760Sdg199075 				di_rx(di_rx_arg, mrh, nmp, &mhi);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 			dip = ndip;
484449Sericheng 			di_rx = ndi_rx;
485449Sericheng 			di_rx_arg = ndi_rx_arg;
4860Sstevel@tonic-gate 		}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		/*
4890Sstevel@tonic-gate 		 * Release the hold on the dls_impl_t chain now that we have
4900Sstevel@tonic-gate 		 * finished walking it.
4910Sstevel@tonic-gate 		 */
492269Sericheng 		i_dls_head_rele(dhp);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate loop:
4950Sstevel@tonic-gate 		/*
4960Sstevel@tonic-gate 		 * If there were no acceptors then add the packet count to the
4970Sstevel@tonic-gate 		 * 'unknown' count.
4980Sstevel@tonic-gate 		 */
4990Sstevel@tonic-gate 		if (!accepted)
5000Sstevel@tonic-gate 			atomic_add_32(&(dlp->dl_unknowns), npacket);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5042760Sdg199075 /*
5052760Sdg199075  * Try to send mp up to the DLS_SAP_PROMISC listeners. Return B_TRUE if this
5062760Sdg199075  * message is sent to any streams.
5072760Sdg199075  */
5082760Sdg199075 static uint_t
5092760Sdg199075 i_dls_link_rx_common_promisc(dls_link_t *dlp, mac_resource_handle_t mrh,
5102760Sdg199075     mac_header_info_t *mhip, mblk_t *mp, uint16_t vid,
5112760Sdg199075     boolean_t (*acceptfunc)())
5122760Sdg199075 {
5132760Sdg199075 	uint_t naccepted;
5142760Sdg199075 
5152760Sdg199075 	naccepted = i_dls_link_rx_func(dlp, mrh, mhip, mp, DLS_SAP_PROMISC,
5162760Sdg199075 	    vid, acceptfunc);
5172760Sdg199075 
5182760Sdg199075 	if (vid != VLAN_ID_NONE) {
5192760Sdg199075 		naccepted += i_dls_link_rx_func(dlp, mrh, mhip, mp,
5202760Sdg199075 		    DLS_SAP_PROMISC, VLAN_ID_NONE, acceptfunc);
5212760Sdg199075 	}
5222760Sdg199075 	return (naccepted);
5232760Sdg199075 }
5242760Sdg199075 
5250Sstevel@tonic-gate static void
5262760Sdg199075 i_dls_link_rx_common(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
5272760Sdg199075     boolean_t (*acceptfunc)())
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	dls_link_t			*dlp = arg;
530269Sericheng 	mod_hash_t			*hash = dlp->dl_impl_hash;
5310Sstevel@tonic-gate 	mblk_t				*nextp;
5322311Sseb 	mac_header_info_t		mhi;
5332760Sdg199075 	uint16_t			vid, vidkey, pri;
534269Sericheng 	dls_head_t			*dhp;
5350Sstevel@tonic-gate 	dls_impl_t			*dip;
5360Sstevel@tonic-gate 	mblk_t				*nmp;
537269Sericheng 	mod_hash_key_t			key;
5380Sstevel@tonic-gate 	uint_t				npacket;
5392760Sdg199075 	uint32_t			sap;
5400Sstevel@tonic-gate 	boolean_t			accepted;
5412760Sdg199075 	dls_rx_t			di_rx, fdi_rx;
5422760Sdg199075 	void				*di_rx_arg, *fdi_rx_arg;
5432760Sdg199075 	boolean_t			pass2;
5442760Sdg199075 	int				err;
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	/*
5470Sstevel@tonic-gate 	 * Walk the packet chain.
5480Sstevel@tonic-gate 	 */
5492760Sdg199075 	for (; mp != NULL; mp = nextp) {
5500Sstevel@tonic-gate 		/*
5512760Sdg199075 		 * Wipe the accepted state and the receive information of
5522760Sdg199075 		 * the first eligible dls_impl_t.
5530Sstevel@tonic-gate 		 */
5540Sstevel@tonic-gate 		accepted = B_FALSE;
5552760Sdg199075 		pass2 = B_FALSE;
5562760Sdg199075 		fdi_rx = NULL;
5572760Sdg199075 		fdi_rx_arg = NULL;
5582760Sdg199075 
5592760Sdg199075 		DLS_PREPARE_PKT(dlp, mp, &mhi, err);
5602760Sdg199075 		if (err != 0) {
5612760Sdg199075 			if (acceptfunc == dls_accept)
5622760Sdg199075 				atomic_add_32(&(dlp->dl_unknowns), 1);
5632760Sdg199075 			nextp = mp->b_next;
5643037Syz147064 			mp->b_next = NULL;
5652760Sdg199075 			freemsg(mp);
5662760Sdg199075 			continue;
5672760Sdg199075 		}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		/*
5700Sstevel@tonic-gate 		 * Grab the longest sub-chain we can process as a single
5710Sstevel@tonic-gate 		 * unit.
5720Sstevel@tonic-gate 		 */
5732760Sdg199075 		nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket);
5742760Sdg199075 		ASSERT(npacket != 0);
5752760Sdg199075 
5762760Sdg199075 		vid = VLAN_ID(mhi.mhi_tci);
5772760Sdg199075 		pri = VLAN_PRI(mhi.mhi_tci);
5782760Sdg199075 
5792760Sdg199075 		vidkey = vid;
5802311Sseb 
5812760Sdg199075 		/*
5822760Sdg199075 		 * Note that we need to first send to the dls_impl_t
5832760Sdg199075 		 * in promiscuous mode in order to avoid the packet reordering
5842760Sdg199075 		 * when snooping.
5852760Sdg199075 		 */
5862760Sdg199075 		if (i_dls_link_rx_common_promisc(dlp, mrh, &mhi, mp, vidkey,
5872760Sdg199075 		    acceptfunc) > 0) {
5882760Sdg199075 			accepted = B_TRUE;
5892311Sseb 		}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 		/*
5922760Sdg199075 		 * Non promisc case. Two passes:
5932760Sdg199075 		 *   1. send tagged packets to ETHERTYPE_VLAN listeners
5942760Sdg199075 		 *   2. send packets to listeners bound to the specific SAP.
5950Sstevel@tonic-gate 		 */
5962760Sdg199075 		if (mhi.mhi_istagged) {
5972760Sdg199075 			vidkey = VLAN_ID_NONE;
5982760Sdg199075 			sap = ETHERTYPE_VLAN;
5992760Sdg199075 		} else {
6002760Sdg199075 			goto non_promisc_loop;
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate non_promisc:
6030Sstevel@tonic-gate 		/*
6040Sstevel@tonic-gate 		 * Construct a hash key from the VLAN identifier and the
6050Sstevel@tonic-gate 		 * DLSAP.
6060Sstevel@tonic-gate 		 */
6072760Sdg199075 		key = MAKE_KEY(sap, vidkey);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 		/*
6100Sstevel@tonic-gate 		 * Search the has table for dls_impl_t eligible to receive
6110Sstevel@tonic-gate 		 * a packet chain for this DLSAP/VLAN combination.
6120Sstevel@tonic-gate 		 */
613269Sericheng 		rw_enter(&dlp->dl_impl_lock, RW_READER);
614269Sericheng 		if (mod_hash_find(hash, key, (mod_hash_val_t *)&dhp) != 0) {
615269Sericheng 			rw_exit(&dlp->dl_impl_lock);
6162760Sdg199075 			goto non_promisc_loop;
6170Sstevel@tonic-gate 		}
618269Sericheng 		i_dls_head_hold(dhp);
619269Sericheng 		rw_exit(&dlp->dl_impl_lock);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 		/*
6220Sstevel@tonic-gate 		 * Find the first dls_impl_t that will accept the sub-chain.
6230Sstevel@tonic-gate 		 */
6242760Sdg199075 		for (dip = dhp->dh_list; dip != NULL; dip = dip->di_nextp) {
6252760Sdg199075 			if (!acceptfunc(dip, &mhi, &di_rx, &di_rx_arg))
6262760Sdg199075 				continue;
6270Sstevel@tonic-gate 
6282760Sdg199075 			accepted = B_TRUE;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 			/*
6312760Sdg199075 			 * To avoid the extra copymsgchain(), if this
6322760Sdg199075 			 * is the first eligible dls_impl_t, remember required
6332760Sdg199075 			 * information and send up the message afterwards.
6340Sstevel@tonic-gate 			 */
6352760Sdg199075 			if (fdi_rx == NULL) {
6362760Sdg199075 				fdi_rx = di_rx;
6372760Sdg199075 				fdi_rx_arg = di_rx_arg;
6382760Sdg199075 				continue;
6390Sstevel@tonic-gate 			}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 			if ((nmp = copymsgchain(mp)) != NULL)
6422760Sdg199075 				di_rx(di_rx_arg, mrh, nmp, &mhi);
6430Sstevel@tonic-gate 		}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		/*
6460Sstevel@tonic-gate 		 * Release the hold on the dls_impl_t chain now that we have
6470Sstevel@tonic-gate 		 * finished walking it.
6480Sstevel@tonic-gate 		 */
649269Sericheng 		i_dls_head_rele(dhp);
6500Sstevel@tonic-gate 
6512760Sdg199075 non_promisc_loop:
6522760Sdg199075 		/*
6532760Sdg199075 		 * Don't pass the packets up again if:
6542760Sdg199075 		 * - First pass is done and the packets are tagged and their:
6552760Sdg199075 		 *	- VID and priority are both zero (invalid packets).
6562760Sdg199075 		 *	- their sap is ETHERTYPE_VLAN and their VID is zero
6572760Sdg199075 		 *	  (they have already been sent upstreams).
6582760Sdg199075 		 *  - Second pass is done:
6592760Sdg199075 		 */
6602760Sdg199075 		if (pass2 || (mhi.mhi_istagged &&
6612760Sdg199075 		    ((vid == VLAN_ID_NONE && pri == 0) ||
6622760Sdg199075 		    (mhi.mhi_bindsap == ETHERTYPE_VLAN &&
6632760Sdg199075 		    vid == VLAN_ID_NONE)))) {
6642760Sdg199075 			/*
6652760Sdg199075 			 * Send the message up to the first eligible dls_impl_t.
6662760Sdg199075 			 */
6672760Sdg199075 			if (fdi_rx != NULL)
6682760Sdg199075 				fdi_rx(fdi_rx_arg, mrh, mp, &mhi);
6692760Sdg199075 			else
6702760Sdg199075 				freemsgchain(mp);
6712760Sdg199075 		} else {
6722760Sdg199075 			vidkey = vid;
6732760Sdg199075 			sap = mhi.mhi_bindsap;
6742760Sdg199075 			pass2 = B_TRUE;
6752760Sdg199075 			goto non_promisc;
6762760Sdg199075 		}
6772760Sdg199075 
6780Sstevel@tonic-gate 		/*
6790Sstevel@tonic-gate 		 * If there were no acceptors then add the packet count to the
6800Sstevel@tonic-gate 		 * 'unknown' count.
6810Sstevel@tonic-gate 		 */
6822760Sdg199075 		if (!accepted && (acceptfunc == dls_accept))
6830Sstevel@tonic-gate 			atomic_add_32(&(dlp->dl_unknowns), npacket);
6842760Sdg199075 	}
6852760Sdg199075 }
6860Sstevel@tonic-gate 
6872760Sdg199075 static void
6882760Sdg199075 i_dls_link_rx_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
6892760Sdg199075 {
6902760Sdg199075 	i_dls_link_rx_common(arg, mrh, mp, dls_accept);
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate 
693*5733Syz147064 void
694*5733Syz147064 dls_link_txloop(void *arg, mblk_t *mp)
6950Sstevel@tonic-gate {
6962760Sdg199075 	i_dls_link_rx_common(arg, NULL, mp, dls_accept_loopback);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
699269Sericheng /*ARGSUSED*/
700269Sericheng static uint_t
701269Sericheng i_dls_link_walk(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate 	boolean_t	*promiscp = arg;
7040Sstevel@tonic-gate 	uint32_t	sap = KEY_SAP(key);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	if (sap == DLS_SAP_PROMISC) {
7070Sstevel@tonic-gate 		*promiscp = B_TRUE;
708269Sericheng 		return (MH_WALK_TERMINATE);
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
711269Sericheng 	return (MH_WALK_CONTINUE);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate static int
715*5733Syz147064 i_dls_link_create(const char *name, dls_link_t **dlpp)
7160Sstevel@tonic-gate {
7170Sstevel@tonic-gate 	dls_link_t		*dlp;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	/*
7200Sstevel@tonic-gate 	 * Allocate a new dls_link_t structure.
7210Sstevel@tonic-gate 	 */
7220Sstevel@tonic-gate 	dlp = kmem_cache_alloc(i_dls_link_cachep, KM_SLEEP);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	/*
7250Sstevel@tonic-gate 	 * Name the dls_link_t after the MAC interface it represents.
7260Sstevel@tonic-gate 	 */
7272311Sseb 	(void) strlcpy(dlp->dl_name, name, sizeof (dlp->dl_name));
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	/*
730*5733Syz147064 	 * Initialize promiscuous bookkeeping fields.
7310Sstevel@tonic-gate 	 */
7320Sstevel@tonic-gate 	dlp->dl_npromisc = 0;
7330Sstevel@tonic-gate 	dlp->dl_mth = NULL;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	*dlpp = dlp;
7360Sstevel@tonic-gate 	return (0);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate static void
7400Sstevel@tonic-gate i_dls_link_destroy(dls_link_t *dlp)
7410Sstevel@tonic-gate {
7420Sstevel@tonic-gate 	ASSERT(dlp->dl_npromisc == 0);
7430Sstevel@tonic-gate 	ASSERT(dlp->dl_nactive == 0);
7440Sstevel@tonic-gate 	ASSERT(dlp->dl_mth == NULL);
7450Sstevel@tonic-gate 	ASSERT(dlp->dl_macref == 0);
7460Sstevel@tonic-gate 	ASSERT(dlp->dl_mh == NULL);
7470Sstevel@tonic-gate 	ASSERT(dlp->dl_mip == NULL);
748269Sericheng 	ASSERT(dlp->dl_impl_count == 0);
749269Sericheng 	ASSERT(dlp->dl_mrh == NULL);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/*
7520Sstevel@tonic-gate 	 * Free the structure back to the cache.
7530Sstevel@tonic-gate 	 */
7540Sstevel@tonic-gate 	dlp->dl_unknowns = 0;
7550Sstevel@tonic-gate 	kmem_cache_free(i_dls_link_cachep, dlp);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate /*
7590Sstevel@tonic-gate  * Module initialization functions.
7600Sstevel@tonic-gate  */
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate void
7630Sstevel@tonic-gate dls_link_init(void)
7640Sstevel@tonic-gate {
7650Sstevel@tonic-gate 	/*
7660Sstevel@tonic-gate 	 * Create a kmem_cache of dls_link_t structures.
7670Sstevel@tonic-gate 	 */
7680Sstevel@tonic-gate 	i_dls_link_cachep = kmem_cache_create("dls_link_cache",
7690Sstevel@tonic-gate 	    sizeof (dls_link_t), 0, i_dls_link_constructor,
7700Sstevel@tonic-gate 	    i_dls_link_destructor, NULL, NULL, NULL, 0);
7710Sstevel@tonic-gate 	ASSERT(i_dls_link_cachep != NULL);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	/*
774269Sericheng 	 * Create a dls_link_t hash table and associated lock.
7750Sstevel@tonic-gate 	 */
776269Sericheng 	i_dls_link_hash = mod_hash_create_extended("dls_link_hash",
777269Sericheng 	    IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
778269Sericheng 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
779269Sericheng 	rw_init(&i_dls_link_lock, NULL, RW_DEFAULT, NULL);
780269Sericheng 	i_dls_link_count = 0;
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate int
7840Sstevel@tonic-gate dls_link_fini(void)
7850Sstevel@tonic-gate {
786269Sericheng 	if (i_dls_link_count > 0)
787269Sericheng 		return (EBUSY);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	/*
7900Sstevel@tonic-gate 	 * Destroy the kmem_cache.
7910Sstevel@tonic-gate 	 */
7920Sstevel@tonic-gate 	kmem_cache_destroy(i_dls_link_cachep);
793269Sericheng 
794269Sericheng 	/*
795269Sericheng 	 * Destroy the hash table and associated lock.
796269Sericheng 	 */
797269Sericheng 	mod_hash_destroy_hash(i_dls_link_hash);
798269Sericheng 	rw_destroy(&i_dls_link_lock);
7990Sstevel@tonic-gate 	return (0);
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate /*
8030Sstevel@tonic-gate  * Exported functions.
8040Sstevel@tonic-gate  */
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate int
807*5733Syz147064 dls_link_hold(const char *name, dls_link_t **dlpp)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate 	dls_link_t		*dlp;
8100Sstevel@tonic-gate 	int			err;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	/*
8130Sstevel@tonic-gate 	 * Look up a dls_link_t corresponding to the given mac_handle_t
814269Sericheng 	 * in the global hash table. We need to hold i_dls_link_lock in
815269Sericheng 	 * order to atomically find and insert a dls_link_t into the
816269Sericheng 	 * hash table.
8170Sstevel@tonic-gate 	 */
818269Sericheng 	rw_enter(&i_dls_link_lock, RW_WRITER);
819269Sericheng 	if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
820269Sericheng 	    (mod_hash_val_t *)&dlp)) == 0)
8210Sstevel@tonic-gate 		goto done;
822269Sericheng 
823269Sericheng 	ASSERT(err == MH_ERR_NOTFOUND);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/*
8260Sstevel@tonic-gate 	 * We didn't find anything so we need to create one.
8270Sstevel@tonic-gate 	 */
828*5733Syz147064 	if ((err = i_dls_link_create(name, &dlp)) != 0) {
829269Sericheng 		rw_exit(&i_dls_link_lock);
8300Sstevel@tonic-gate 		return (err);
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	/*
834269Sericheng 	 * Insert the dls_link_t.
8350Sstevel@tonic-gate 	 */
8362311Sseb 	err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)name,
837269Sericheng 	    (mod_hash_val_t)dlp);
8380Sstevel@tonic-gate 	ASSERT(err == 0);
8390Sstevel@tonic-gate 
840269Sericheng 	i_dls_link_count++;
841269Sericheng 	ASSERT(i_dls_link_count != 0);
842269Sericheng 
8430Sstevel@tonic-gate done:
8440Sstevel@tonic-gate 	/*
8450Sstevel@tonic-gate 	 * Bump the reference count and hand back the reference.
8460Sstevel@tonic-gate 	 */
8470Sstevel@tonic-gate 	dlp->dl_ref++;
8480Sstevel@tonic-gate 	*dlpp = dlp;
849269Sericheng 	rw_exit(&i_dls_link_lock);
850269Sericheng 	return (0);
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate void
8540Sstevel@tonic-gate dls_link_rele(dls_link_t *dlp)
8550Sstevel@tonic-gate {
856269Sericheng 	mod_hash_val_t	val;
8570Sstevel@tonic-gate 
858269Sericheng 	rw_enter(&i_dls_link_lock, RW_WRITER);
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	/*
8610Sstevel@tonic-gate 	 * Check if there are any more references.
8620Sstevel@tonic-gate 	 */
8630Sstevel@tonic-gate 	if (--dlp->dl_ref != 0) {
8640Sstevel@tonic-gate 		/*
8650Sstevel@tonic-gate 		 * There are more references so there's nothing more to do.
8660Sstevel@tonic-gate 		 */
8670Sstevel@tonic-gate 		goto done;
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
870269Sericheng 	(void) mod_hash_remove(i_dls_link_hash,
871269Sericheng 	    (mod_hash_key_t)dlp->dl_name, &val);
872269Sericheng 	ASSERT(dlp == (dls_link_t *)val);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	/*
8750Sstevel@tonic-gate 	 * Destroy the dls_link_t.
8760Sstevel@tonic-gate 	 */
8770Sstevel@tonic-gate 	i_dls_link_destroy(dlp);
878269Sericheng 	ASSERT(i_dls_link_count > 0);
879269Sericheng 	i_dls_link_count--;
8800Sstevel@tonic-gate done:
881269Sericheng 	rw_exit(&i_dls_link_lock);
8820Sstevel@tonic-gate }
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate int
8850Sstevel@tonic-gate dls_mac_hold(dls_link_t *dlp)
8860Sstevel@tonic-gate {
8870Sstevel@tonic-gate 	int err = 0;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	mutex_enter(&dlp->dl_lock);
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	ASSERT(IMPLY(dlp->dl_macref != 0, dlp->dl_mh != NULL));
8920Sstevel@tonic-gate 	ASSERT(IMPLY(dlp->dl_macref == 0, dlp->dl_mh == NULL));
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	if (dlp->dl_macref == 0) {
8950Sstevel@tonic-gate 		/*
8960Sstevel@tonic-gate 		 * First reference; hold open the MAC interface.
8970Sstevel@tonic-gate 		 */
898*5733Syz147064 		err = mac_open(dlp->dl_name, &dlp->dl_mh);
8990Sstevel@tonic-gate 		if (err != 0)
9000Sstevel@tonic-gate 			goto done;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 		dlp->dl_mip = mac_info(dlp->dl_mh);
9030Sstevel@tonic-gate 	}
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	dlp->dl_macref++;
9060Sstevel@tonic-gate done:
9070Sstevel@tonic-gate 	mutex_exit(&dlp->dl_lock);
9080Sstevel@tonic-gate 	return (err);
9090Sstevel@tonic-gate }
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate void
9120Sstevel@tonic-gate dls_mac_rele(dls_link_t *dlp)
9130Sstevel@tonic-gate {
9140Sstevel@tonic-gate 	mutex_enter(&dlp->dl_lock);
9150Sstevel@tonic-gate 	ASSERT(dlp->dl_mh != NULL);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	if (--dlp->dl_macref == 0) {
9184913Sethindra 		mac_rx_remove_wait(dlp->dl_mh);
9190Sstevel@tonic-gate 		mac_close(dlp->dl_mh);
9200Sstevel@tonic-gate 		dlp->dl_mh = NULL;
9210Sstevel@tonic-gate 		dlp->dl_mip = NULL;
9220Sstevel@tonic-gate 	}
9230Sstevel@tonic-gate 	mutex_exit(&dlp->dl_lock);
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate void
9270Sstevel@tonic-gate dls_link_add(dls_link_t *dlp, uint32_t sap, dls_impl_t *dip)
9280Sstevel@tonic-gate {
9290Sstevel@tonic-gate 	dls_vlan_t	*dvp = dip->di_dvp;
930269Sericheng 	mod_hash_t	*hash = dlp->dl_impl_hash;
931269Sericheng 	mod_hash_key_t	key;
932269Sericheng 	dls_head_t	*dhp;
9330Sstevel@tonic-gate 	dls_impl_t	*p;
9340Sstevel@tonic-gate 	mac_rx_t	rx;
9350Sstevel@tonic-gate 	int		err;
936269Sericheng 	boolean_t	promisc = B_FALSE;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/*
9392311Sseb 	 * Generate a hash key based on the sap and the VLAN id.
9400Sstevel@tonic-gate 	 */
9410Sstevel@tonic-gate 	key = MAKE_KEY(sap, dvp->dv_id);
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	/*
9440Sstevel@tonic-gate 	 * We need dl_lock here because we want to be able to walk
9450Sstevel@tonic-gate 	 * the hash table *and* set the mac rx func atomically. if
9460Sstevel@tonic-gate 	 * these two operations are separate, someone else could
947269Sericheng 	 * insert/remove dls_impl_t from the hash table after we
948269Sericheng 	 * drop the hash lock and this could cause our chosen rx
949269Sericheng 	 * func to be incorrect. note that we cannot call mac_rx_add
950269Sericheng 	 * when holding the hash lock because this can cause deadlock.
9510Sstevel@tonic-gate 	 */
9520Sstevel@tonic-gate 	mutex_enter(&dlp->dl_lock);
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	/*
955269Sericheng 	 * Search the table for a list head with this key.
9560Sstevel@tonic-gate 	 */
957269Sericheng 	rw_enter(&dlp->dl_impl_lock, RW_WRITER);
9580Sstevel@tonic-gate 
959269Sericheng 	if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) {
960269Sericheng 		ASSERT(err == MH_ERR_NOTFOUND);
9610Sstevel@tonic-gate 
962269Sericheng 		dhp = i_dls_head_alloc(key);
963269Sericheng 		err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp);
964269Sericheng 		ASSERT(err == 0);
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	/*
968269Sericheng 	 * Add the dls_impl_t to the head of the list.
969269Sericheng 	 */
970269Sericheng 	ASSERT(dip->di_nextp == NULL);
971269Sericheng 	p = dhp->dh_list;
972269Sericheng 	dip->di_nextp = p;
973269Sericheng 	dhp->dh_list = dip;
974269Sericheng 
975269Sericheng 	/*
976269Sericheng 	 * Save a pointer to the list head.
977269Sericheng 	 */
978269Sericheng 	dip->di_headp = dhp;
979269Sericheng 	dlp->dl_impl_count++;
980269Sericheng 
981269Sericheng 	/*
982269Sericheng 	 * Walk the bound dls_impl_t to see if there are any
983269Sericheng 	 * in promiscuous 'all sap' mode.
9840Sstevel@tonic-gate 	 */
985269Sericheng 	mod_hash_walk(hash, i_dls_link_walk, (void *)&promisc);
986269Sericheng 	rw_exit(&dlp->dl_impl_lock);
987269Sericheng 
988269Sericheng 	/*
989269Sericheng 	 * If there are then we need to use a receive routine
990269Sericheng 	 * which will route packets to those dls_impl_t as well
991269Sericheng 	 * as ones bound to the  DLSAP of the packet.
992269Sericheng 	 */
993269Sericheng 	if (promisc)
9942311Sseb 		rx = i_dls_link_rx_promisc;
995269Sericheng 	else
9962311Sseb 		rx = i_dls_link_rx;
997269Sericheng 
998269Sericheng 	/* Replace the existing receive function if there is one. */
999269Sericheng 	if (dlp->dl_mrh != NULL)
10004913Sethindra 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh, B_FALSE);
10015084Sjohnlev 	dlp->dl_mrh = mac_active_rx_add(dlp->dl_mh, rx, (void *)dlp);
1002269Sericheng 	mutex_exit(&dlp->dl_lock);
1003269Sericheng }
1004269Sericheng 
1005269Sericheng void
1006269Sericheng dls_link_remove(dls_link_t *dlp, dls_impl_t *dip)
1007269Sericheng {
1008269Sericheng 	mod_hash_t	*hash = dlp->dl_impl_hash;
1009269Sericheng 	dls_impl_t	**pp;
1010269Sericheng 	dls_impl_t	*p;
1011269Sericheng 	dls_head_t	*dhp;
1012269Sericheng 	mac_rx_t	rx;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	/*
1015269Sericheng 	 * We need dl_lock here because we want to be able to walk
1016269Sericheng 	 * the hash table *and* set the mac rx func atomically. if
1017269Sericheng 	 * these two operations are separate, someone else could
1018269Sericheng 	 * insert/remove dls_impl_t from the hash table after we
1019269Sericheng 	 * drop the hash lock and this could cause our chosen rx
1020269Sericheng 	 * func to be incorrect. note that we cannot call mac_rx_add
1021269Sericheng 	 * when holding the hash lock because this can cause deadlock.
10220Sstevel@tonic-gate 	 */
1023269Sericheng 	mutex_enter(&dlp->dl_lock);
1024269Sericheng 	rw_enter(&dlp->dl_impl_lock, RW_WRITER);
10250Sstevel@tonic-gate 
1026269Sericheng 	/*
1027269Sericheng 	 * Poll the hash table entry until all references have been dropped.
1028269Sericheng 	 * We need to drop all locks before sleeping because we don't want
1029269Sericheng 	 * the interrupt handler to block. We set di_removing here to
1030269Sericheng 	 * tell the receive callbacks not to pass up packets anymore.
1031269Sericheng 	 * This is only a hint to quicken the decrease of the refcnt so
1032269Sericheng 	 * the assignment need not be protected by any lock.
1033269Sericheng 	 */
1034269Sericheng 	dhp = dip->di_headp;
1035269Sericheng 	dip->di_removing = B_TRUE;
1036269Sericheng 	while (dhp->dh_ref != 0) {
1037269Sericheng 		rw_exit(&dlp->dl_impl_lock);
1038269Sericheng 		mutex_exit(&dlp->dl_lock);
1039269Sericheng 		delay(drv_usectohz(1000));	/* 1ms delay */
1040269Sericheng 		mutex_enter(&dlp->dl_lock);
1041269Sericheng 		rw_enter(&dlp->dl_impl_lock, RW_WRITER);
1042269Sericheng 	}
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	/*
1045269Sericheng 	 * Walk the list and remove the dls_impl_t.
10460Sstevel@tonic-gate 	 */
1047269Sericheng 	for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->di_nextp)) {
1048269Sericheng 		if (p == dip)
1049269Sericheng 			break;
1050269Sericheng 	}
1051269Sericheng 	ASSERT(p != NULL);
1052269Sericheng 	*pp = p->di_nextp;
1053269Sericheng 	p->di_nextp = NULL;
1054269Sericheng 
1055269Sericheng 	ASSERT(dlp->dl_impl_count > 0);
1056269Sericheng 	dlp->dl_impl_count--;
10570Sstevel@tonic-gate 
1058269Sericheng 	if (dhp->dh_list == NULL) {
1059269Sericheng 		mod_hash_val_t	val = NULL;
1060269Sericheng 
1061269Sericheng 		/*
1062269Sericheng 		 * The list is empty so remove the hash table entry.
1063269Sericheng 		 */
1064269Sericheng 		(void) mod_hash_remove(hash, dhp->dh_key, &val);
1065269Sericheng 		ASSERT(dhp == (dls_head_t *)val);
1066269Sericheng 		i_dls_head_free(dhp);
1067269Sericheng 	}
1068269Sericheng 	dip->di_removing = B_FALSE;
1069269Sericheng 
10700Sstevel@tonic-gate 	/*
1071269Sericheng 	 * If there are no dls_impl_t then there's no need to register a
1072269Sericheng 	 * receive function with the mac.
10730Sstevel@tonic-gate 	 */
1074269Sericheng 	if (dlp->dl_impl_count == 0) {
1075269Sericheng 		rw_exit(&dlp->dl_impl_lock);
10764913Sethindra 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh, B_FALSE);
1077269Sericheng 		dlp->dl_mrh = NULL;
10780Sstevel@tonic-gate 	} else {
10790Sstevel@tonic-gate 		boolean_t promisc = B_FALSE;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 		/*
10820Sstevel@tonic-gate 		 * Walk the bound dls_impl_t to see if there are any
10830Sstevel@tonic-gate 		 * in promiscuous 'all sap' mode.
10840Sstevel@tonic-gate 		 */
1085269Sericheng 		mod_hash_walk(hash, i_dls_link_walk, (void *)&promisc);
1086269Sericheng 		rw_exit(&dlp->dl_impl_lock);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 		/*
10890Sstevel@tonic-gate 		 * If there are then we need to use a receive routine
10900Sstevel@tonic-gate 		 * which will route packets to those dls_impl_t as well
10910Sstevel@tonic-gate 		 * as ones bound to the  DLSAP of the packet.
10920Sstevel@tonic-gate 		 */
10930Sstevel@tonic-gate 		if (promisc)
10942311Sseb 			rx = i_dls_link_rx_promisc;
10950Sstevel@tonic-gate 		else
10962311Sseb 			rx = i_dls_link_rx;
10970Sstevel@tonic-gate 
10984913Sethindra 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh, B_FALSE);
10995084Sjohnlev 		dlp->dl_mrh = mac_active_rx_add(dlp->dl_mh, rx, (void *)dlp);
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate 	mutex_exit(&dlp->dl_lock);
11020Sstevel@tonic-gate }
11032311Sseb 
11042311Sseb int
11052760Sdg199075 dls_link_header_info(dls_link_t *dlp, mblk_t *mp, mac_header_info_t *mhip)
11062311Sseb {
11072311Sseb 	boolean_t	is_ethernet = (dlp->dl_mip->mi_media == DL_ETHER);
11082311Sseb 	int		err = 0;
11092311Sseb 
11102760Sdg199075 	/*
11112760Sdg199075 	 * Packets should always be at least 16 bit aligned.
11122760Sdg199075 	 */
11132760Sdg199075 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
11142760Sdg199075 
11152311Sseb 	if ((err = mac_header_info(dlp->dl_mh, mp, mhip)) != 0)
11162311Sseb 		return (err);
11172311Sseb 
11182311Sseb 	/*
11192311Sseb 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
11202760Sdg199075 	 * mac_header_info_t as returned by mac_header_info() is
11212760Sdg199075 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
11222311Sseb 	 */
11232760Sdg199075 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
11242311Sseb 		struct ether_vlan_header *evhp;
11252311Sseb 		uint16_t sap;
11262760Sdg199075 		mblk_t *tmp = NULL;
11272760Sdg199075 		size_t size;
11282311Sseb 
11292760Sdg199075 		size = sizeof (struct ether_vlan_header);
11302760Sdg199075 		if (MBLKL(mp) < size) {
11312760Sdg199075 			/*
11322760Sdg199075 			 * Pullup the message in order to get the MAC header
11332760Sdg199075 			 * infomation. Note that this is a read-only function,
11342760Sdg199075 			 * we keep the input packet intact.
11352760Sdg199075 			 */
11362760Sdg199075 			if ((tmp = msgpullup(mp, size)) == NULL)
11372760Sdg199075 				return (EINVAL);
11382760Sdg199075 
11392760Sdg199075 			mp = tmp;
11402760Sdg199075 		}
11412311Sseb 		evhp = (struct ether_vlan_header *)mp->b_rptr;
11422311Sseb 		sap = ntohs(evhp->ether_type);
11432311Sseb 		(void) mac_sap_verify(dlp->dl_mh, sap, &mhip->mhi_bindsap);
11442311Sseb 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
11452760Sdg199075 		mhip->mhi_tci = ntohs(evhp->ether_tci);
11462760Sdg199075 		mhip->mhi_istagged = B_TRUE;
11472760Sdg199075 		freemsg(tmp);
11482760Sdg199075 
11492760Sdg199075 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
11502760Sdg199075 			return (EINVAL);
11512760Sdg199075 	} else {
11522760Sdg199075 		mhip->mhi_istagged = B_FALSE;
11532760Sdg199075 		mhip->mhi_tci = 0;
11542311Sseb 	}
11552311Sseb 	return (0);
11562311Sseb }
1157