xref: /onnv-gate/usr/src/uts/common/io/dls/dls_link.c (revision 8833:8adf20bc60e3)
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 /*
22*8833SVenu.Iyer@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Data-Link Services Module
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
308275SEric Cheng #include	<sys/sysmacros.h>
318275SEric Cheng #include	<sys/strsubr.h>
320Sstevel@tonic-gate #include	<sys/strsun.h>
338275SEric Cheng #include	<sys/vlan.h>
348275SEric Cheng #include	<sys/dld_impl.h>
358275SEric Cheng #include	<sys/sdt.h>
360Sstevel@tonic-gate #include	<sys/atomic.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate static kmem_cache_t	*i_dls_link_cachep;
39*8833SVenu.Iyer@Sun.COM mod_hash_t		*i_dls_link_hash;
40269Sericheng static uint_t		i_dls_link_count;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #define		LINK_HASHSZ	67	/* prime */
430Sstevel@tonic-gate #define		IMPL_HASHSZ	67	/* prime */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Construct a hash key encompassing both DLSAP value and VLAN idenitifier.
470Sstevel@tonic-gate  */
488275SEric Cheng #define	MAKE_KEY(_sap)						\
498275SEric Cheng 	((mod_hash_key_t)(uintptr_t)((_sap) << VLAN_ID_SIZE))
500Sstevel@tonic-gate 
512311Sseb #define	DLS_STRIP_PADDING(pktsize, p) {			\
522311Sseb 	if (pktsize != 0) {				\
532311Sseb 		ssize_t delta = pktsize - msgdsize(p);	\
542311Sseb 							\
552311Sseb 		if (delta < 0)				\
562311Sseb 			(void) adjmsg(p, delta);	\
572311Sseb 	}						\
582311Sseb }
592311Sseb 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * Private functions.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*ARGSUSED*/
650Sstevel@tonic-gate static int
660Sstevel@tonic-gate i_dls_link_constructor(void *buf, void *arg, int kmflag)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	dls_link_t	*dlp = buf;
690Sstevel@tonic-gate 	char		name[MAXNAMELEN];
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	bzero(buf, sizeof (dls_link_t));
720Sstevel@tonic-gate 
735895Syz147064 	(void) snprintf(name, MAXNAMELEN, "dls_link_t_%p_hash", buf);
748275SEric Cheng 	dlp->dl_str_hash = mod_hash_create_idhash(name, IMPL_HASHSZ,
75269Sericheng 	    mod_hash_null_valdtor);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	return (0);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*ARGSUSED*/
810Sstevel@tonic-gate static void
820Sstevel@tonic-gate i_dls_link_destructor(void *buf, void *arg)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	dls_link_t	*dlp = buf;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	ASSERT(dlp->dl_ref == 0);
870Sstevel@tonic-gate 	ASSERT(dlp->dl_mh == NULL);
888275SEric Cheng 	ASSERT(dlp->dl_mah == NULL);
890Sstevel@tonic-gate 	ASSERT(dlp->dl_unknowns == 0);
900Sstevel@tonic-gate 
918275SEric Cheng 	mod_hash_destroy_idhash(dlp->dl_str_hash);
928275SEric Cheng 	dlp->dl_str_hash = NULL;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
962311Sseb /*
972760Sdg199075  * - Parse the mac header information of the given packet.
982760Sdg199075  * - Strip the padding and skip over the header. Note that because some
992760Sdg199075  *   DLS consumers only check the db_ref count of the first mblk, we
1003037Syz147064  *   pullup the message into a single mblk. Because the original message
1013037Syz147064  *   is freed as the result of message pulling up, dls_link_header_info()
1023037Syz147064  *   is called again to update the mhi_saddr and mhi_daddr pointers in the
1033037Syz147064  *   mhip. Further, the dls_link_header_info() function ensures that the
1043037Syz147064  *   size of the pulled message is greater than the MAC header size,
1053037Syz147064  *   therefore we can directly advance b_rptr to point at the payload.
1062760Sdg199075  *
1072760Sdg199075  * We choose to use a macro for performance reasons.
1082760Sdg199075  */
1092760Sdg199075 #define	DLS_PREPARE_PKT(dlp, mp, mhip, err) {				\
1102760Sdg199075 	mblk_t *nextp = (mp)->b_next;					\
1112760Sdg199075 	if (((err) = dls_link_header_info((dlp), (mp), (mhip))) == 0) {	\
1122760Sdg199075 		DLS_STRIP_PADDING((mhip)->mhi_pktsize, (mp));		\
1132760Sdg199075 		if (MBLKL((mp)) < (mhip)->mhi_hdrsize) {		\
1142760Sdg199075 			mblk_t *newmp;					\
1152760Sdg199075 			if ((newmp = msgpullup((mp), -1)) == NULL) {	\
1162760Sdg199075 				(err) = EINVAL;				\
1172760Sdg199075 			} else {					\
1183037Syz147064 				(mp)->b_next = NULL;			\
1192760Sdg199075 				freemsg((mp));				\
1202760Sdg199075 				(mp) = newmp;				\
1213037Syz147064 				VERIFY(dls_link_header_info((dlp),	\
1223037Syz147064 				    (mp), (mhip)) == 0);		\
1232760Sdg199075 				(mp)->b_next = nextp;			\
1242760Sdg199075 				(mp)->b_rptr += (mhip)->mhi_hdrsize;	\
1252760Sdg199075 			}						\
1262760Sdg199075 		} else {						\
1272760Sdg199075 			(mp)->b_rptr += (mhip)->mhi_hdrsize;		\
1282760Sdg199075 		}							\
1292760Sdg199075 	}								\
1302760Sdg199075 }
1312760Sdg199075 
1322760Sdg199075 /*
1332311Sseb  * Truncate the chain starting at mp such that all packets in the chain
1342760Sdg199075  * have identical source and destination addresses, saps, and tag types
1352760Sdg199075  * (see below).  It returns a pointer to the mblk following the chain,
1362760Sdg199075  * NULL if there is no further packet following the processed chain.
1372760Sdg199075  * The countp argument is set to the number of valid packets in the chain.
1382760Sdg199075  * Note that the whole MAC header (including the VLAN tag if any) in each
1392760Sdg199075  * packet will be stripped.
1402311Sseb  */
1410Sstevel@tonic-gate static mblk_t *
1422760Sdg199075 i_dls_link_subchain(dls_link_t *dlp, mblk_t *mp, const mac_header_info_t *mhip,
1432760Sdg199075     uint_t *countp)
1440Sstevel@tonic-gate {
1452760Sdg199075 	mblk_t		*prevp;
1462760Sdg199075 	uint_t		npacket = 1;
1472311Sseb 	size_t		addr_size = dlp->dl_mip->mi_addr_length;
1482760Sdg199075 	uint16_t	vid = VLAN_ID(mhip->mhi_tci);
1492760Sdg199075 	uint16_t	pri = VLAN_PRI(mhip->mhi_tci);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/*
1520Sstevel@tonic-gate 	 * Compare with subsequent headers until we find one that has
1531502Sericheng 	 * differing header information. After checking each packet
1541502Sericheng 	 * strip padding and skip over the header.
1550Sstevel@tonic-gate 	 */
1562760Sdg199075 	for (prevp = mp; (mp = mp->b_next) != NULL; prevp = mp) {
1572311Sseb 		mac_header_info_t cmhi;
1582760Sdg199075 		uint16_t cvid, cpri;
1592760Sdg199075 		int err;
1602311Sseb 
1612760Sdg199075 		DLS_PREPARE_PKT(dlp, mp, &cmhi, err);
1622760Sdg199075 		if (err != 0)
1630Sstevel@tonic-gate 			break;
1642311Sseb 
1652760Sdg199075 		prevp->b_next = mp;
1662760Sdg199075 
1672311Sseb 		/*
1685895Syz147064 		 * The source, destination, sap, vlan id and the MSGNOLOOP
1695895Syz147064 		 * flag must all match in a given subchain.
1702311Sseb 		 */
1712311Sseb 		if (memcmp(mhip->mhi_daddr, cmhi.mhi_daddr, addr_size) != 0 ||
1722311Sseb 		    memcmp(mhip->mhi_saddr, cmhi.mhi_saddr, addr_size) != 0 ||
1738275SEric Cheng 		    mhip->mhi_bindsap != cmhi.mhi_bindsap) {
1742760Sdg199075 			/*
1752760Sdg199075 			 * Note that we don't need to restore the padding.
1762760Sdg199075 			 */
1772760Sdg199075 			mp->b_rptr -= cmhi.mhi_hdrsize;
1782311Sseb 			break;
1792311Sseb 		}
1802311Sseb 
1812760Sdg199075 		cvid = VLAN_ID(cmhi.mhi_tci);
1822760Sdg199075 		cpri = VLAN_PRI(cmhi.mhi_tci);
1832311Sseb 
1842760Sdg199075 		/*
1852760Sdg199075 		 * There are several types of packets. Packets don't match
1862760Sdg199075 		 * if they are classified to different type or if they are
1872760Sdg199075 		 * VLAN packets but belong to different VLANs:
1882760Sdg199075 		 *
1892760Sdg199075 		 * packet type		tagged		vid		pri
1902760Sdg199075 		 * ---------------------------------------------------------
1912760Sdg199075 		 * untagged		No		zero		zero
1922760Sdg199075 		 * VLAN packets		Yes		non-zero	-
1932760Sdg199075 		 * priority tagged	Yes		zero		non-zero
1942760Sdg199075 		 * 0 tagged		Yes		zero		zero
1952760Sdg199075 		 */
1962760Sdg199075 		if ((mhip->mhi_istagged != cmhi.mhi_istagged) ||
1972760Sdg199075 		    (vid != cvid) || ((vid == VLAN_ID_NONE) &&
1982760Sdg199075 		    (((pri == 0) && (cpri != 0)) ||
1992760Sdg199075 		    ((pri != 0) && (cpri == 0))))) {
2002760Sdg199075 			mp->b_rptr -= cmhi.mhi_hdrsize;
2012760Sdg199075 			break;
2022760Sdg199075 		}
2032760Sdg199075 
2040Sstevel@tonic-gate 		npacket++;
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	/*
2080Sstevel@tonic-gate 	 * Break the chain at this point and return a pointer to the next
2090Sstevel@tonic-gate 	 * sub-chain.
2100Sstevel@tonic-gate 	 */
2112760Sdg199075 	prevp->b_next = NULL;
2120Sstevel@tonic-gate 	*countp = npacket;
2132760Sdg199075 	return (mp);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2168275SEric Cheng /* ARGSUSED */
2178275SEric Cheng static int
2188275SEric Cheng i_dls_head_hold(mod_hash_key_t key, mod_hash_val_t val)
219269Sericheng {
2208275SEric Cheng 	dls_head_t *dhp = (dls_head_t *)val;
2218275SEric Cheng 
2228275SEric Cheng 	/*
2238275SEric Cheng 	 * The lock order is  mod_hash's internal lock -> dh_lock as in the
2248275SEric Cheng 	 * call to i_dls_link_rx -> mod_hash_find_cb_rval -> i_dls_head_hold
2258275SEric Cheng 	 */
2268275SEric Cheng 	mutex_enter(&dhp->dh_lock);
2278275SEric Cheng 	if (dhp->dh_removing) {
2288275SEric Cheng 		mutex_exit(&dhp->dh_lock);
2298275SEric Cheng 		return (-1);
2308275SEric Cheng 	}
2318275SEric Cheng 	dhp->dh_ref++;
2328275SEric Cheng 	mutex_exit(&dhp->dh_lock);
2338275SEric Cheng 	return (0);
234269Sericheng }
235269Sericheng 
2368275SEric Cheng void
237269Sericheng i_dls_head_rele(dls_head_t *dhp)
238269Sericheng {
2398275SEric Cheng 	mutex_enter(&dhp->dh_lock);
2408275SEric Cheng 	dhp->dh_ref--;
2418275SEric Cheng 	if (dhp->dh_ref == 0 && dhp->dh_removing != 0)
2428275SEric Cheng 		cv_broadcast(&dhp->dh_cv);
2438275SEric Cheng 	mutex_exit(&dhp->dh_lock);
244269Sericheng }
245269Sericheng 
246269Sericheng static dls_head_t *
247269Sericheng i_dls_head_alloc(mod_hash_key_t key)
248269Sericheng {
249269Sericheng 	dls_head_t	*dhp;
250269Sericheng 
251269Sericheng 	dhp = kmem_zalloc(sizeof (dls_head_t), KM_SLEEP);
252269Sericheng 	dhp->dh_key = key;
253269Sericheng 	return (dhp);
254269Sericheng }
255269Sericheng 
256269Sericheng static void
257269Sericheng i_dls_head_free(dls_head_t *dhp)
258269Sericheng {
259269Sericheng 	ASSERT(dhp->dh_ref == 0);
260269Sericheng 	kmem_free(dhp, sizeof (dls_head_t));
261269Sericheng }
262269Sericheng 
2632760Sdg199075 /*
2642760Sdg199075  * Try to send mp up to the streams of the given sap and vid. Return B_TRUE
2652760Sdg199075  * if this message is sent to any streams.
2662760Sdg199075  * Note that this function will copy the message chain and the original
2672760Sdg199075  * mp will remain valid after this function
2682760Sdg199075  */
2692760Sdg199075 static uint_t
2702760Sdg199075 i_dls_link_rx_func(dls_link_t *dlp, mac_resource_handle_t mrh,
2718275SEric Cheng     mac_header_info_t *mhip, mblk_t *mp, uint32_t sap,
2722760Sdg199075     boolean_t (*acceptfunc)())
2732760Sdg199075 {
2748275SEric Cheng 	mod_hash_t	*hash = dlp->dl_str_hash;
2752760Sdg199075 	mod_hash_key_t	key;
2762760Sdg199075 	dls_head_t	*dhp;
2778275SEric Cheng 	dld_str_t	*dsp;
2782760Sdg199075 	mblk_t		*nmp;
2798275SEric Cheng 	dls_rx_t	ds_rx;
2808275SEric Cheng 	void		*ds_rx_arg;
2812760Sdg199075 	uint_t		naccepted = 0;
2828275SEric Cheng 	int		rval;
2832760Sdg199075 
2842760Sdg199075 	/*
2852760Sdg199075 	 * Construct a hash key from the VLAN identifier and the
2868275SEric Cheng 	 * DLSAP that represents dld_str_t in promiscuous mode.
2872760Sdg199075 	 */
2888275SEric Cheng 	key = MAKE_KEY(sap);
2892760Sdg199075 
2902760Sdg199075 	/*
2918275SEric Cheng 	 * Search the hash table for dld_str_t eligible to receive
2928275SEric Cheng 	 * a packet chain for this DLSAP/VLAN combination. The mod hash's
2938275SEric Cheng 	 * internal lock serializes find/insert/remove from the mod hash list.
2948275SEric Cheng 	 * Incrementing the dh_ref (while holding the mod hash lock) ensures
2958275SEric Cheng 	 * dls_link_remove will wait for the upcall to finish.
2962760Sdg199075 	 */
2978275SEric Cheng 	if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
2988275SEric Cheng 	    i_dls_head_hold, &rval) != 0 || (rval != 0)) {
2992760Sdg199075 		return (B_FALSE);
3002760Sdg199075 	}
3012760Sdg199075 
3022760Sdg199075 	/*
3038275SEric Cheng 	 * Find dld_str_t that will accept the sub-chain.
3042760Sdg199075 	 */
3058275SEric Cheng 	for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next) {
3068275SEric Cheng 		if (!acceptfunc(dsp, mhip, &ds_rx, &ds_rx_arg))
3072760Sdg199075 			continue;
3082760Sdg199075 
3092760Sdg199075 		/*
3102760Sdg199075 		 * We have at least one acceptor.
3112760Sdg199075 		 */
3128275SEric Cheng 		naccepted++;
3132760Sdg199075 
3142760Sdg199075 		/*
3158275SEric Cheng 		 * There will normally be at least more dld_str_t
3162760Sdg199075 		 * (since we've yet to check for non-promiscuous
3178275SEric Cheng 		 * dld_str_t) so dup the sub-chain.
3182760Sdg199075 		 */
3192760Sdg199075 		if ((nmp = copymsgchain(mp)) != NULL)
3208275SEric Cheng 			ds_rx(ds_rx_arg, mrh, nmp, mhip);
3212760Sdg199075 	}
3222760Sdg199075 
3232760Sdg199075 	/*
3248275SEric Cheng 	 * Release the hold on the dld_str_t chain now that we have
3252760Sdg199075 	 * finished walking it.
3262760Sdg199075 	 */
3272760Sdg199075 	i_dls_head_rele(dhp);
3282760Sdg199075 	return (naccepted);
3292760Sdg199075 }
3302760Sdg199075 
3318275SEric Cheng /* ARGSUSED */
3328275SEric Cheng void
3338275SEric Cheng i_dls_link_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
3348275SEric Cheng     boolean_t loopback)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	dls_link_t			*dlp = arg;
3378275SEric Cheng 	mod_hash_t			*hash = dlp->dl_str_hash;
3380Sstevel@tonic-gate 	mblk_t				*nextp;
3392311Sseb 	mac_header_info_t		mhi;
340269Sericheng 	dls_head_t			*dhp;
3418275SEric Cheng 	dld_str_t			*dsp;
3428275SEric Cheng 	dld_str_t			*ndsp;
3430Sstevel@tonic-gate 	mblk_t				*nmp;
344269Sericheng 	mod_hash_key_t			key;
3450Sstevel@tonic-gate 	uint_t				npacket;
3460Sstevel@tonic-gate 	boolean_t			accepted;
3478275SEric Cheng 	dls_rx_t			ds_rx, nds_rx;
3488275SEric Cheng 	void				*ds_rx_arg, *nds_rx_arg;
3492760Sdg199075 	uint16_t			vid;
3508275SEric Cheng 	int				err, rval;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/*
3530Sstevel@tonic-gate 	 * Walk the packet chain.
3540Sstevel@tonic-gate 	 */
3552760Sdg199075 	for (; mp != NULL; mp = nextp) {
3560Sstevel@tonic-gate 		/*
3570Sstevel@tonic-gate 		 * Wipe the accepted state.
3580Sstevel@tonic-gate 		 */
3590Sstevel@tonic-gate 		accepted = B_FALSE;
3600Sstevel@tonic-gate 
3612760Sdg199075 		DLS_PREPARE_PKT(dlp, mp, &mhi, err);
3622760Sdg199075 		if (err != 0) {
3632760Sdg199075 			atomic_add_32(&(dlp->dl_unknowns), 1);
3642760Sdg199075 			nextp = mp->b_next;
3653037Syz147064 			mp->b_next = NULL;
3662760Sdg199075 			freemsg(mp);
3672760Sdg199075 			continue;
3682760Sdg199075 		}
3692760Sdg199075 
3700Sstevel@tonic-gate 		/*
3710Sstevel@tonic-gate 		 * Grab the longest sub-chain we can process as a single
3720Sstevel@tonic-gate 		 * unit.
3730Sstevel@tonic-gate 		 */
3742760Sdg199075 		nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket);
3752760Sdg199075 		ASSERT(npacket != 0);
3760Sstevel@tonic-gate 
3772760Sdg199075 		vid = VLAN_ID(mhi.mhi_tci);
3782760Sdg199075 
3792760Sdg199075 		if (mhi.mhi_istagged) {
3802311Sseb 			/*
3812760Sdg199075 			 * If it is tagged traffic, send it upstream to
3828275SEric Cheng 			 * all dld_str_t which are attached to the physical
3832760Sdg199075 			 * link and bound to SAP 0x8100.
3842311Sseb 			 */
3852760Sdg199075 			if (i_dls_link_rx_func(dlp, mrh, &mhi, mp,
3868275SEric Cheng 			    ETHERTYPE_VLAN, dls_accept) > 0) {
3872760Sdg199075 				accepted = B_TRUE;
3882760Sdg199075 			}
3892760Sdg199075 
3902760Sdg199075 			/*
3912760Sdg199075 			 * Don't pass the packets up if they are tagged
3922760Sdg199075 			 * packets and:
3932760Sdg199075 			 *  - their VID and priority are both zero (invalid
3942760Sdg199075 			 *    packets).
3952760Sdg199075 			 *  - their sap is ETHERTYPE_VLAN and their VID is
3962760Sdg199075 			 *    zero as they have already been sent upstreams.
3972760Sdg199075 			 */
3982760Sdg199075 			if ((vid == VLAN_ID_NONE &&
3992760Sdg199075 			    VLAN_PRI(mhi.mhi_tci) == 0) ||
4002760Sdg199075 			    (mhi.mhi_bindsap == ETHERTYPE_VLAN &&
4012760Sdg199075 			    vid == VLAN_ID_NONE)) {
4022760Sdg199075 				freemsgchain(mp);
4032760Sdg199075 				goto loop;
4042760Sdg199075 			}
4052311Sseb 		}
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 		/*
4080Sstevel@tonic-gate 		 * Construct a hash key from the VLAN identifier and the
4090Sstevel@tonic-gate 		 * DLSAP.
4100Sstevel@tonic-gate 		 */
4118275SEric Cheng 		key = MAKE_KEY(mhi.mhi_bindsap);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 		/*
4148275SEric Cheng 		 * Search the has table for dld_str_t eligible to receive
4150Sstevel@tonic-gate 		 * a packet chain for this DLSAP/VLAN combination.
4160Sstevel@tonic-gate 		 */
4178275SEric Cheng 		if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
4188275SEric Cheng 		    i_dls_head_hold, &rval) != 0 || (rval != 0)) {
4190Sstevel@tonic-gate 			freemsgchain(mp);
4200Sstevel@tonic-gate 			goto loop;
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 		/*
4248275SEric Cheng 		 * Find the first dld_str_t that will accept the sub-chain.
4250Sstevel@tonic-gate 		 */
4268275SEric Cheng 		for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next)
4278275SEric Cheng 			if (dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
4280Sstevel@tonic-gate 				break;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		/*
4318275SEric Cheng 		 * If we did not find any dld_str_t willing to accept the
4320Sstevel@tonic-gate 		 * sub-chain then throw it away.
4330Sstevel@tonic-gate 		 */
4348275SEric Cheng 		if (dsp == NULL) {
435269Sericheng 			i_dls_head_rele(dhp);
4360Sstevel@tonic-gate 			freemsgchain(mp);
4370Sstevel@tonic-gate 			goto loop;
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 		/*
4410Sstevel@tonic-gate 		 * We have at least one acceptor.
4420Sstevel@tonic-gate 		 */
4430Sstevel@tonic-gate 		accepted = B_TRUE;
4440Sstevel@tonic-gate 		for (;;) {
4450Sstevel@tonic-gate 			/*
4468275SEric Cheng 			 * Find the next dld_str_t that will accept the
4470Sstevel@tonic-gate 			 * sub-chain.
4480Sstevel@tonic-gate 			 */
4498275SEric Cheng 			for (ndsp = dsp->ds_next; ndsp != NULL;
4508275SEric Cheng 			    ndsp = ndsp->ds_next)
4518275SEric Cheng 				if (dls_accept(ndsp, &mhi, &nds_rx,
4528275SEric Cheng 				    &nds_rx_arg))
4530Sstevel@tonic-gate 					break;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 			/*
4568275SEric Cheng 			 * If there are no more dld_str_t that are willing
4570Sstevel@tonic-gate 			 * to accept the sub-chain then we don't need to dup
4580Sstevel@tonic-gate 			 * it before handing it to the current one.
4590Sstevel@tonic-gate 			 */
4608275SEric Cheng 			if (ndsp == NULL) {
4618275SEric Cheng 				ds_rx(ds_rx_arg, mrh, mp, &mhi);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 				/*
4648275SEric Cheng 				 * Since there are no more dld_str_t, we're
4650Sstevel@tonic-gate 				 * done.
4660Sstevel@tonic-gate 				 */
4670Sstevel@tonic-gate 				break;
4680Sstevel@tonic-gate 			}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 			/*
4718275SEric Cheng 			 * There are more dld_str_t so dup the sub-chain.
4720Sstevel@tonic-gate 			 */
4730Sstevel@tonic-gate 			if ((nmp = copymsgchain(mp)) != NULL)
4748275SEric Cheng 				ds_rx(ds_rx_arg, mrh, nmp, &mhi);
4750Sstevel@tonic-gate 
4768275SEric Cheng 			dsp = ndsp;
4778275SEric Cheng 			ds_rx = nds_rx;
4788275SEric Cheng 			ds_rx_arg = nds_rx_arg;
4790Sstevel@tonic-gate 		}
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 		/*
4828275SEric Cheng 		 * Release the hold on the dld_str_t chain now that we have
4830Sstevel@tonic-gate 		 * finished walking it.
4840Sstevel@tonic-gate 		 */
485269Sericheng 		i_dls_head_rele(dhp);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate loop:
4880Sstevel@tonic-gate 		/*
4890Sstevel@tonic-gate 		 * If there were no acceptors then add the packet count to the
4900Sstevel@tonic-gate 		 * 'unknown' count.
4910Sstevel@tonic-gate 		 */
4920Sstevel@tonic-gate 		if (!accepted)
4930Sstevel@tonic-gate 			atomic_add_32(&(dlp->dl_unknowns), npacket);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4978275SEric Cheng /* ARGSUSED */
4988275SEric Cheng void
4998275SEric Cheng dls_rx_vlan_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
5008275SEric Cheng     boolean_t loopback)
5012760Sdg199075 {
5028275SEric Cheng 	dld_str_t			*dsp = arg;
5038275SEric Cheng 	dls_link_t			*dlp = dsp->ds_dlp;
5048275SEric Cheng 	mac_header_info_t		mhi;
5058275SEric Cheng 	dls_rx_t			ds_rx;
5068275SEric Cheng 	void				*ds_rx_arg;
5078275SEric Cheng 	int				err;
5088275SEric Cheng 
5098275SEric Cheng 	DLS_PREPARE_PKT(dlp, mp, &mhi, err);
5108275SEric Cheng 	if (err != 0)
5118275SEric Cheng 		goto drop;
5128275SEric Cheng 
5138275SEric Cheng 	/*
5148275SEric Cheng 	 * If there is promiscuous handle for vlan, we filter out the untagged
5158275SEric Cheng 	 * pkts and pkts that are not for the primary unicast address.
5168275SEric Cheng 	 */
5178275SEric Cheng 	if (dsp->ds_vlan_mph != NULL) {
5188275SEric Cheng 		uint8_t prim_addr[MAXMACADDRLEN];
5198275SEric Cheng 		size_t	addr_length = dsp->ds_mip->mi_addr_length;
5202760Sdg199075 
5218275SEric Cheng 		if (!(mhi.mhi_istagged))
5228275SEric Cheng 			goto drop;
5238275SEric Cheng 		ASSERT(dsp->ds_mh != NULL);
5248275SEric Cheng 		mac_unicast_primary_get(dsp->ds_mh, (uint8_t *)prim_addr);
5258275SEric Cheng 		if (memcmp(mhi.mhi_daddr, prim_addr, addr_length) != 0)
5268275SEric Cheng 			goto drop;
5278275SEric Cheng 
5288275SEric Cheng 		if (!dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
5298275SEric Cheng 			goto drop;
5308275SEric Cheng 
5318275SEric Cheng 		ds_rx(ds_rx_arg, NULL, mp, &mhi);
5328275SEric Cheng 		return;
5338275SEric Cheng 	}
5348275SEric Cheng 
5358275SEric Cheng drop:
5368275SEric Cheng 	atomic_add_32(&dlp->dl_unknowns, 1);
5378275SEric Cheng 	freemsg(mp);
5388275SEric Cheng }
5392760Sdg199075 
5408275SEric Cheng /* ARGSUSED */
5418275SEric Cheng void
5428275SEric Cheng dls_rx_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
5438275SEric Cheng     boolean_t loopback)
5448275SEric Cheng {
5458275SEric Cheng 	dld_str_t			*dsp = arg;
5468275SEric Cheng 	dls_link_t			*dlp = dsp->ds_dlp;
5478275SEric Cheng 	mac_header_info_t		mhi;
5488275SEric Cheng 	dls_rx_t			ds_rx;
5498275SEric Cheng 	void				*ds_rx_arg;
5508275SEric Cheng 	int				err;
5518275SEric Cheng 	dls_head_t			*dhp;
5528275SEric Cheng 	mod_hash_key_t			key;
5538275SEric Cheng 
5548275SEric Cheng 	DLS_PREPARE_PKT(dlp, mp, &mhi, err);
5558275SEric Cheng 	if (err != 0)
5568275SEric Cheng 		goto drop;
5578275SEric Cheng 
5588275SEric Cheng 	/*
5598275SEric Cheng 	 * In order to filter out sap pkt that no dls channel listens, search
5608275SEric Cheng 	 * the hash table trying to find a dld_str_t eligible to receive the pkt
5618275SEric Cheng 	 */
5628275SEric Cheng 	if ((dsp->ds_promisc & DLS_PROMISC_SAP) == 0) {
5638275SEric Cheng 		key = MAKE_KEY(mhi.mhi_bindsap);
5648275SEric Cheng 		if (mod_hash_find(dsp->ds_dlp->dl_str_hash, key,
5658275SEric Cheng 		    (mod_hash_val_t *)&dhp) != 0)
5668275SEric Cheng 			goto drop;
5672760Sdg199075 	}
5688275SEric Cheng 
5698275SEric Cheng 	if (!dls_accept_promisc(dsp, &mhi, &ds_rx, &ds_rx_arg, loopback))
5708275SEric Cheng 		goto drop;
5718275SEric Cheng 
5728275SEric Cheng 	ds_rx(ds_rx_arg, NULL, mp, &mhi);
5738275SEric Cheng 	return;
5748275SEric Cheng 
5758275SEric Cheng drop:
5768275SEric Cheng 	atomic_add_32(&dlp->dl_unknowns, 1);
5778275SEric Cheng 	freemsg(mp);
5782760Sdg199075 }
5792760Sdg199075 
5800Sstevel@tonic-gate static void
5818275SEric Cheng i_dls_link_destroy(dls_link_t *dlp)
5820Sstevel@tonic-gate {
5838275SEric Cheng 	ASSERT(dlp->dl_nactive == 0);
5848275SEric Cheng 	ASSERT(dlp->dl_impl_count == 0);
5858275SEric Cheng 	ASSERT(dlp->dl_zone_ref == 0);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5888275SEric Cheng 	 * Free the structure back to the cache.
5890Sstevel@tonic-gate 	 */
5908275SEric Cheng 	if (dlp->dl_mch != NULL)
5918275SEric Cheng 		mac_client_close(dlp->dl_mch, 0);
5920Sstevel@tonic-gate 
5938275SEric Cheng 	if (dlp->dl_mh != NULL) {
5948275SEric Cheng 		ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
5958275SEric Cheng 		mac_close(dlp->dl_mh);
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 
5988275SEric Cheng 	dlp->dl_mh = NULL;
5998275SEric Cheng 	dlp->dl_mch = NULL;
6008275SEric Cheng 	dlp->dl_mip = NULL;
6018275SEric Cheng 	dlp->dl_unknowns = 0;
6028275SEric Cheng 	kmem_cache_free(i_dls_link_cachep, dlp);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate static int
6065733Syz147064 i_dls_link_create(const char *name, dls_link_t **dlpp)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	dls_link_t		*dlp;
6098275SEric Cheng 	int			err;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	/*
6120Sstevel@tonic-gate 	 * Allocate a new dls_link_t structure.
6130Sstevel@tonic-gate 	 */
6140Sstevel@tonic-gate 	dlp = kmem_cache_alloc(i_dls_link_cachep, KM_SLEEP);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	/*
6170Sstevel@tonic-gate 	 * Name the dls_link_t after the MAC interface it represents.
6180Sstevel@tonic-gate 	 */
6192311Sseb 	(void) strlcpy(dlp->dl_name, name, sizeof (dlp->dl_name));
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	/*
6228275SEric Cheng 	 * First reference; hold open the MAC interface.
6230Sstevel@tonic-gate 	 */
6248275SEric Cheng 	ASSERT(dlp->dl_mh == NULL);
6258275SEric Cheng 	err = mac_open(dlp->dl_name, &dlp->dl_mh);
6268275SEric Cheng 	if (err != 0)
6278275SEric Cheng 		goto bail;
6288275SEric Cheng 
6298275SEric Cheng 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
6308275SEric Cheng 	dlp->dl_mip = mac_info(dlp->dl_mh);
6318275SEric Cheng 
6328275SEric Cheng 	/* DLS is the "primary" MAC client */
6338275SEric Cheng 	ASSERT(dlp->dl_mch == NULL);
6348275SEric Cheng 
6358275SEric Cheng 	err = mac_client_open(dlp->dl_mh, &dlp->dl_mch, NULL,
6368275SEric Cheng 	    MAC_OPEN_FLAGS_TAG_DISABLE | MAC_OPEN_FLAGS_DISABLE_TX_VID_CHECK |
6378275SEric Cheng 	    MAC_OPEN_FLAGS_USE_DATALINK_NAME);
6388275SEric Cheng 	if (err != 0)
6398275SEric Cheng 		goto bail;
6408275SEric Cheng 
6418275SEric Cheng 	DTRACE_PROBE2(dls__primary__client, char *, dlp->dl_name, void *,
6428275SEric Cheng 	    dlp->dl_mch);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	*dlpp = dlp;
6450Sstevel@tonic-gate 	return (0);
6460Sstevel@tonic-gate 
6478275SEric Cheng bail:
6488275SEric Cheng 	i_dls_link_destroy(dlp);
6498275SEric Cheng 	return (err);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate /*
6530Sstevel@tonic-gate  * Module initialization functions.
6540Sstevel@tonic-gate  */
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate void
6570Sstevel@tonic-gate dls_link_init(void)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	/*
6600Sstevel@tonic-gate 	 * Create a kmem_cache of dls_link_t structures.
6610Sstevel@tonic-gate 	 */
6620Sstevel@tonic-gate 	i_dls_link_cachep = kmem_cache_create("dls_link_cache",
6630Sstevel@tonic-gate 	    sizeof (dls_link_t), 0, i_dls_link_constructor,
6640Sstevel@tonic-gate 	    i_dls_link_destructor, NULL, NULL, NULL, 0);
6650Sstevel@tonic-gate 	ASSERT(i_dls_link_cachep != NULL);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	/*
668269Sericheng 	 * Create a dls_link_t hash table and associated lock.
6690Sstevel@tonic-gate 	 */
670269Sericheng 	i_dls_link_hash = mod_hash_create_extended("dls_link_hash",
671269Sericheng 	    IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
672269Sericheng 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
673269Sericheng 	i_dls_link_count = 0;
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate int
6770Sstevel@tonic-gate dls_link_fini(void)
6780Sstevel@tonic-gate {
679269Sericheng 	if (i_dls_link_count > 0)
680269Sericheng 		return (EBUSY);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	/*
6830Sstevel@tonic-gate 	 * Destroy the kmem_cache.
6840Sstevel@tonic-gate 	 */
6850Sstevel@tonic-gate 	kmem_cache_destroy(i_dls_link_cachep);
686269Sericheng 
687269Sericheng 	/*
688269Sericheng 	 * Destroy the hash table and associated lock.
689269Sericheng 	 */
690269Sericheng 	mod_hash_destroy_hash(i_dls_link_hash);
6910Sstevel@tonic-gate 	return (0);
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate /*
6950Sstevel@tonic-gate  * Exported functions.
6960Sstevel@tonic-gate  */
6970Sstevel@tonic-gate 
6988275SEric Cheng static int
6998275SEric Cheng dls_link_hold_common(const char *name, dls_link_t **dlpp, boolean_t create)
7000Sstevel@tonic-gate {
7010Sstevel@tonic-gate 	dls_link_t		*dlp;
7020Sstevel@tonic-gate 	int			err;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	/*
7058275SEric Cheng 	 * Look up a dls_link_t corresponding to the given macname in the
7068275SEric Cheng 	 * global hash table. The i_dls_link_hash itself is protected by the
7078275SEric Cheng 	 * mod_hash package's internal lock which synchronizes
7088275SEric Cheng 	 * find/insert/remove into the global mod_hash list. Assumes that
7098275SEric Cheng 	 * inserts and removes are single threaded on a per mac end point
7108275SEric Cheng 	 * by the mac perimeter.
7110Sstevel@tonic-gate 	 */
712269Sericheng 	if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
713269Sericheng 	    (mod_hash_val_t *)&dlp)) == 0)
7140Sstevel@tonic-gate 		goto done;
715269Sericheng 
716269Sericheng 	ASSERT(err == MH_ERR_NOTFOUND);
7178275SEric Cheng 	if (!create)
7188275SEric Cheng 		return (ENOENT);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	/*
7210Sstevel@tonic-gate 	 * We didn't find anything so we need to create one.
7220Sstevel@tonic-gate 	 */
7238275SEric Cheng 	if ((err = i_dls_link_create(name, &dlp)) != 0)
7240Sstevel@tonic-gate 		return (err);
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	/*
727269Sericheng 	 * Insert the dls_link_t.
7280Sstevel@tonic-gate 	 */
7295895Syz147064 	err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)dlp->dl_name,
730269Sericheng 	    (mod_hash_val_t)dlp);
7310Sstevel@tonic-gate 	ASSERT(err == 0);
7320Sstevel@tonic-gate 
7338275SEric Cheng 	atomic_add_32(&i_dls_link_count, 1);
734269Sericheng 	ASSERT(i_dls_link_count != 0);
735269Sericheng 
7360Sstevel@tonic-gate done:
7378275SEric Cheng 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
7380Sstevel@tonic-gate 	/*
7390Sstevel@tonic-gate 	 * Bump the reference count and hand back the reference.
7400Sstevel@tonic-gate 	 */
7410Sstevel@tonic-gate 	dlp->dl_ref++;
7420Sstevel@tonic-gate 	*dlpp = dlp;
743269Sericheng 	return (0);
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate 
7468275SEric Cheng int
7478275SEric Cheng dls_link_hold_create(const char *name, dls_link_t **dlpp)
7488275SEric Cheng {
7498275SEric Cheng 	return (dls_link_hold_common(name, dlpp, B_TRUE));
7508275SEric Cheng }
7518275SEric Cheng 
7528275SEric Cheng int
7538275SEric Cheng dls_link_hold(const char *name, dls_link_t **dlpp)
7548275SEric Cheng {
7558275SEric Cheng 	return (dls_link_hold_common(name, dlpp, B_FALSE));
7568275SEric Cheng }
7578275SEric Cheng 
7588275SEric Cheng dev_info_t *
7598275SEric Cheng dls_link_devinfo(dev_t dev)
7608275SEric Cheng {
7618275SEric Cheng 	dls_link_t	*dlp;
7628275SEric Cheng 	dev_info_t	*dip;
7638275SEric Cheng 	char	macname[MAXNAMELEN];
7648275SEric Cheng 	char	*drv;
7658275SEric Cheng 	mac_perim_handle_t	mph;
7668275SEric Cheng 
7678275SEric Cheng 	if ((drv = ddi_major_to_name(getmajor(dev))) == NULL)
7688275SEric Cheng 		return (NULL);
7698275SEric Cheng 	(void) snprintf(macname, MAXNAMELEN, "%s%d", drv, getminor(dev) - 1);
7708275SEric Cheng 
7718275SEric Cheng 	/*
7728275SEric Cheng 	 * The code below assumes that the name constructed above is the
7738275SEric Cheng 	 * macname. This is not the case for legacy devices. Currently this
7748275SEric Cheng 	 * is ok because this function is only called in the getinfo(9e) path,
7758275SEric Cheng 	 * which for a legacy device would directly end up in the driver's
7768275SEric Cheng 	 * getinfo, rather than here
7778275SEric Cheng 	 */
7788275SEric Cheng 	if (mac_perim_enter_by_macname(macname, &mph) != 0)
7798275SEric Cheng 		return (NULL);
7808275SEric Cheng 
7818275SEric Cheng 	if (dls_link_hold(macname, &dlp) != 0) {
7828275SEric Cheng 		mac_perim_exit(mph);
7838275SEric Cheng 		return (NULL);
7848275SEric Cheng 	}
7858275SEric Cheng 
7868275SEric Cheng 	dip = mac_devinfo_get(dlp->dl_mh);
7878275SEric Cheng 	dls_link_rele(dlp);
7888275SEric Cheng 	mac_perim_exit(mph);
7898275SEric Cheng 
7908275SEric Cheng 	return (dip);
7918275SEric Cheng }
7928275SEric Cheng 
7938275SEric Cheng dev_t
7948275SEric Cheng dls_link_dev(dls_link_t *dlp)
7958275SEric Cheng {
7968275SEric Cheng 	return (makedevice(ddi_driver_major(mac_devinfo_get(dlp->dl_mh)),
7978275SEric Cheng 	    mac_minor(dlp->dl_mh)));
7988275SEric Cheng }
7998275SEric Cheng 
8000Sstevel@tonic-gate void
8010Sstevel@tonic-gate dls_link_rele(dls_link_t *dlp)
8020Sstevel@tonic-gate {
803269Sericheng 	mod_hash_val_t	val;
8040Sstevel@tonic-gate 
8058275SEric Cheng 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
8060Sstevel@tonic-gate 	/*
8070Sstevel@tonic-gate 	 * Check if there are any more references.
8080Sstevel@tonic-gate 	 */
8098275SEric Cheng 	if (--dlp->dl_ref == 0) {
8108275SEric Cheng 		(void) mod_hash_remove(i_dls_link_hash,
8118275SEric Cheng 		    (mod_hash_key_t)dlp->dl_name, &val);
8128275SEric Cheng 		ASSERT(dlp == (dls_link_t *)val);
8138275SEric Cheng 
8140Sstevel@tonic-gate 		/*
8158275SEric Cheng 		 * Destroy the dls_link_t.
8160Sstevel@tonic-gate 		 */
8178275SEric Cheng 		i_dls_link_destroy(dlp);
8188275SEric Cheng 		ASSERT(i_dls_link_count > 0);
8198275SEric Cheng 		atomic_add_32(&i_dls_link_count, -1);
8208275SEric Cheng 	}
8218275SEric Cheng }
8228275SEric Cheng 
8238275SEric Cheng int
8248275SEric Cheng dls_link_rele_by_name(const char *name)
8258275SEric Cheng {
8268275SEric Cheng 	dls_link_t		*dlp;
8278275SEric Cheng 
8288275SEric Cheng 	if (mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
8298275SEric Cheng 	    (mod_hash_val_t *)&dlp) != 0)
8308275SEric Cheng 		return (ENOENT);
8318275SEric Cheng 
8328275SEric Cheng 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
8338275SEric Cheng 
8348275SEric Cheng 	/*
8358275SEric Cheng 	 * Must fail detach if mac client is busy.
8368275SEric Cheng 	 */
8378275SEric Cheng 	ASSERT(dlp->dl_ref > 0 && dlp->dl_mch != NULL);
8388275SEric Cheng 	if (mac_link_has_flows(dlp->dl_mch))
8398275SEric Cheng 		return (ENOTEMPTY);
8408275SEric Cheng 
8418275SEric Cheng 	dls_link_rele(dlp);
8428275SEric Cheng 	return (0);
8438275SEric Cheng }
8448275SEric Cheng 
8458275SEric Cheng int
8468275SEric Cheng dls_link_setzid(const char *name, zoneid_t zid)
8478275SEric Cheng {
8488275SEric Cheng 	dls_link_t	*dlp;
8498275SEric Cheng 	int		err = 0;
8508275SEric Cheng 	zoneid_t	old_zid;
8518275SEric Cheng 
8528275SEric Cheng 	if ((err = dls_link_hold_create(name, &dlp)) != 0)
8538275SEric Cheng 		return (err);
8548275SEric Cheng 
8558275SEric Cheng 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
8568275SEric Cheng 
8578275SEric Cheng 	if ((old_zid = dlp->dl_zid) == zid)
8588275SEric Cheng 		goto done;
8598275SEric Cheng 
8608275SEric Cheng 	/*
8618275SEric Cheng 	 * Check whether this dlp is used by its own zones, if yes,
8628275SEric Cheng 	 * we cannot change its zoneid.
8638275SEric Cheng 	 */
8648275SEric Cheng 	if (dlp->dl_zone_ref != 0) {
8658275SEric Cheng 		err = EBUSY;
8660Sstevel@tonic-gate 		goto done;
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8698275SEric Cheng 	if (zid == GLOBAL_ZONEID) {
8708275SEric Cheng 		/*
8718275SEric Cheng 		 * Move the link from the local zone to the global zone,
8728275SEric Cheng 		 * and release the reference to this link.  At the same time
8738275SEric Cheng 		 * reset the link's active state so that an aggregation is
8748275SEric Cheng 		 * allowed to be created over it.
8758275SEric Cheng 		 */
8768275SEric Cheng 		dlp->dl_zid = zid;
8778275SEric Cheng 		dls_mac_active_clear(dlp);
8788275SEric Cheng 		dls_link_rele(dlp);
8798275SEric Cheng 		goto done;
8808275SEric Cheng 	} else if (old_zid == GLOBAL_ZONEID) {
8818275SEric Cheng 		/*
8828275SEric Cheng 		 * Move the link from the global zone to the local zone,
8838275SEric Cheng 		 * and hold a reference to this link.  Also, set the link
8848275SEric Cheng 		 * to the "active" state so that the global zone is
8858275SEric Cheng 		 * not able to create an aggregation over this link.
8868275SEric Cheng 		 * TODO: revisit once we allow creating aggregations
8878275SEric Cheng 		 * within a local zone.
8888275SEric Cheng 		 */
8898275SEric Cheng 		if ((err = dls_mac_active_set(dlp)) != 0) {
8908275SEric Cheng 			if (err != ENXIO)
8918275SEric Cheng 				err = EBUSY;
8928275SEric Cheng 			goto done;
8935895Syz147064 		}
8948275SEric Cheng 		dlp->dl_zid = zid;
8958275SEric Cheng 		return (0);
8968275SEric Cheng 	} else {
8978275SEric Cheng 		/*
8988275SEric Cheng 		 * Move the link from a local zone to another local zone.
8998275SEric Cheng 		 */
9008275SEric Cheng 		dlp->dl_zid = zid;
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 
9038275SEric Cheng done:
9048275SEric Cheng 	dls_link_rele(dlp);
9050Sstevel@tonic-gate 	return (err);
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate void
9098275SEric Cheng dls_link_add(dls_link_t *dlp, uint32_t sap, dld_str_t *dsp)
9100Sstevel@tonic-gate {
9118275SEric Cheng 	mod_hash_t	*hash = dlp->dl_str_hash;
912269Sericheng 	mod_hash_key_t	key;
913269Sericheng 	dls_head_t	*dhp;
9148275SEric Cheng 	dld_str_t	*p;
9150Sstevel@tonic-gate 	int		err;
9168275SEric Cheng 
9178275SEric Cheng 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	/*
9208275SEric Cheng 	 * Generate a hash key based on the sap.
9210Sstevel@tonic-gate 	 */
9228275SEric Cheng 	key = MAKE_KEY(sap);
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	/*
925269Sericheng 	 * Search the table for a list head with this key.
9260Sstevel@tonic-gate 	 */
927269Sericheng 	if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) {
928269Sericheng 		ASSERT(err == MH_ERR_NOTFOUND);
9290Sstevel@tonic-gate 
930269Sericheng 		dhp = i_dls_head_alloc(key);
931269Sericheng 		err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp);
932269Sericheng 		ASSERT(err == 0);
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	/*
9368275SEric Cheng 	 * Add the dld_str_t to the head of the list. List walkers in
9378275SEric Cheng 	 * i_dls_link_rx_* bump up dh_ref to ensure the list does not change
9388275SEric Cheng 	 * while they walk the list. The membar below ensures that list walkers
9398275SEric Cheng 	 * see exactly the old list or the new list.
940269Sericheng 	 */
9418275SEric Cheng 	ASSERT(dsp->ds_next == NULL);
942269Sericheng 	p = dhp->dh_list;
9438275SEric Cheng 	dsp->ds_next = p;
9448275SEric Cheng 
9458275SEric Cheng 	membar_producer();
9468275SEric Cheng 
9478275SEric Cheng 	dhp->dh_list = dsp;
948269Sericheng 
949269Sericheng 	/*
950269Sericheng 	 * Save a pointer to the list head.
951269Sericheng 	 */
9528275SEric Cheng 	dsp->ds_head = dhp;
953269Sericheng 	dlp->dl_impl_count++;
954269Sericheng }
955269Sericheng 
956269Sericheng void
9578275SEric Cheng dls_link_remove(dls_link_t *dlp, dld_str_t *dsp)
958269Sericheng {
9598275SEric Cheng 	mod_hash_t	*hash = dlp->dl_str_hash;
9608275SEric Cheng 	dld_str_t	**pp;
9618275SEric Cheng 	dld_str_t	*p;
962269Sericheng 	dls_head_t	*dhp;
9630Sstevel@tonic-gate 
9648275SEric Cheng 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
9650Sstevel@tonic-gate 
966269Sericheng 	/*
9678275SEric Cheng 	 * We set dh_removing here to tell the receive callbacks not to pass
9688275SEric Cheng 	 * up packets anymore. Then wait till the current callbacks are done.
9698275SEric Cheng 	 * This happens either in the close path or in processing the
9708275SEric Cheng 	 * DL_UNBIND_REQ via a taskq thread, and it is ok to cv_wait in either.
9718275SEric Cheng 	 * The dh_ref ensures there aren't and there won't be any upcalls
9728275SEric Cheng 	 * walking or using the dh_list. The mod hash internal lock ensures
9738275SEric Cheng 	 * that the insert/remove of the dls_head_t itself synchronizes with
9748275SEric Cheng 	 * any i_dls_link_rx trying to locate it. The perimeter ensures that
9758275SEric Cheng 	 * there isn't another simultaneous dls_link_add/remove.
976269Sericheng 	 */
9778275SEric Cheng 	dhp = dsp->ds_head;
9788275SEric Cheng 
9798275SEric Cheng 	mutex_enter(&dhp->dh_lock);
9808275SEric Cheng 	dhp->dh_removing = B_TRUE;
9818275SEric Cheng 	while (dhp->dh_ref != 0)
9828275SEric Cheng 		cv_wait(&dhp->dh_cv, &dhp->dh_lock);
9838275SEric Cheng 	mutex_exit(&dhp->dh_lock);
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	/*
9868275SEric Cheng 	 * Walk the list and remove the dld_str_t.
9870Sstevel@tonic-gate 	 */
9888275SEric Cheng 	for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->ds_next)) {
9898275SEric Cheng 		if (p == dsp)
990269Sericheng 			break;
991269Sericheng 	}
992269Sericheng 	ASSERT(p != NULL);
9938275SEric Cheng 	*pp = p->ds_next;
9948275SEric Cheng 	p->ds_next = NULL;
9958275SEric Cheng 	p->ds_head = NULL;
996269Sericheng 
9978275SEric Cheng 	ASSERT(dlp->dl_impl_count != 0);
998269Sericheng 	dlp->dl_impl_count--;
9990Sstevel@tonic-gate 
1000269Sericheng 	if (dhp->dh_list == NULL) {
1001269Sericheng 		mod_hash_val_t	val = NULL;
1002269Sericheng 
1003269Sericheng 		/*
1004269Sericheng 		 * The list is empty so remove the hash table entry.
1005269Sericheng 		 */
1006269Sericheng 		(void) mod_hash_remove(hash, dhp->dh_key, &val);
1007269Sericheng 		ASSERT(dhp == (dls_head_t *)val);
1008269Sericheng 		i_dls_head_free(dhp);
10098275SEric Cheng 	} else {
10108275SEric Cheng 		mutex_enter(&dhp->dh_lock);
10118275SEric Cheng 		dhp->dh_removing = B_FALSE;
10128275SEric Cheng 		mutex_exit(&dhp->dh_lock);
1013269Sericheng 	}
10140Sstevel@tonic-gate }
10152311Sseb 
10162311Sseb int
10172760Sdg199075 dls_link_header_info(dls_link_t *dlp, mblk_t *mp, mac_header_info_t *mhip)
10182311Sseb {
10192311Sseb 	boolean_t	is_ethernet = (dlp->dl_mip->mi_media == DL_ETHER);
10202311Sseb 	int		err = 0;
10212311Sseb 
10222760Sdg199075 	/*
10232760Sdg199075 	 * Packets should always be at least 16 bit aligned.
10242760Sdg199075 	 */
10252760Sdg199075 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
10262760Sdg199075 
10272311Sseb 	if ((err = mac_header_info(dlp->dl_mh, mp, mhip)) != 0)
10282311Sseb 		return (err);
10292311Sseb 
10302311Sseb 	/*
10312311Sseb 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
10322760Sdg199075 	 * mac_header_info_t as returned by mac_header_info() is
10332760Sdg199075 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
10342311Sseb 	 */
10352760Sdg199075 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
10362311Sseb 		struct ether_vlan_header *evhp;
10372311Sseb 		uint16_t sap;
10382760Sdg199075 		mblk_t *tmp = NULL;
10392760Sdg199075 		size_t size;
10402311Sseb 
10412760Sdg199075 		size = sizeof (struct ether_vlan_header);
10422760Sdg199075 		if (MBLKL(mp) < size) {
10432760Sdg199075 			/*
10442760Sdg199075 			 * Pullup the message in order to get the MAC header
10452760Sdg199075 			 * infomation. Note that this is a read-only function,
10462760Sdg199075 			 * we keep the input packet intact.
10472760Sdg199075 			 */
10482760Sdg199075 			if ((tmp = msgpullup(mp, size)) == NULL)
10492760Sdg199075 				return (EINVAL);
10502760Sdg199075 
10512760Sdg199075 			mp = tmp;
10522760Sdg199075 		}
10532311Sseb 		evhp = (struct ether_vlan_header *)mp->b_rptr;
10542311Sseb 		sap = ntohs(evhp->ether_type);
10552311Sseb 		(void) mac_sap_verify(dlp->dl_mh, sap, &mhip->mhi_bindsap);
10562311Sseb 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
10572760Sdg199075 		mhip->mhi_tci = ntohs(evhp->ether_tci);
10582760Sdg199075 		mhip->mhi_istagged = B_TRUE;
10592760Sdg199075 		freemsg(tmp);
10602760Sdg199075 
10612760Sdg199075 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
10622760Sdg199075 			return (EINVAL);
10632760Sdg199075 	} else {
10642760Sdg199075 		mhip->mhi_istagged = B_FALSE;
10652760Sdg199075 		mhip->mhi_tci = 0;
10662311Sseb 	}
10675895Syz147064 
10682311Sseb 	return (0);
10692311Sseb }
1070