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 /* 228833SVenu.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 3810639SDarren.Reed@Sun.COM static void dls_bpf_newzone(dls_link_t *dlp, zoneid_t zid); 3910639SDarren.Reed@Sun.COM 400Sstevel@tonic-gate static kmem_cache_t *i_dls_link_cachep; 418833SVenu.Iyer@Sun.COM mod_hash_t *i_dls_link_hash; 42269Sericheng static uint_t i_dls_link_count; 430Sstevel@tonic-gate 440Sstevel@tonic-gate #define LINK_HASHSZ 67 /* prime */ 450Sstevel@tonic-gate #define IMPL_HASHSZ 67 /* prime */ 460Sstevel@tonic-gate 470Sstevel@tonic-gate /* 480Sstevel@tonic-gate * Construct a hash key encompassing both DLSAP value and VLAN idenitifier. 490Sstevel@tonic-gate */ 508275SEric Cheng #define MAKE_KEY(_sap) \ 518275SEric Cheng ((mod_hash_key_t)(uintptr_t)((_sap) << VLAN_ID_SIZE)) 520Sstevel@tonic-gate 532311Sseb #define DLS_STRIP_PADDING(pktsize, p) { \ 542311Sseb if (pktsize != 0) { \ 552311Sseb ssize_t delta = pktsize - msgdsize(p); \ 562311Sseb \ 572311Sseb if (delta < 0) \ 582311Sseb (void) adjmsg(p, delta); \ 592311Sseb } \ 602311Sseb } 612311Sseb 620Sstevel@tonic-gate /* 630Sstevel@tonic-gate * Private functions. 640Sstevel@tonic-gate */ 650Sstevel@tonic-gate 660Sstevel@tonic-gate /*ARGSUSED*/ 670Sstevel@tonic-gate static int 680Sstevel@tonic-gate i_dls_link_constructor(void *buf, void *arg, int kmflag) 690Sstevel@tonic-gate { 700Sstevel@tonic-gate dls_link_t *dlp = buf; 710Sstevel@tonic-gate char name[MAXNAMELEN]; 720Sstevel@tonic-gate 730Sstevel@tonic-gate bzero(buf, sizeof (dls_link_t)); 740Sstevel@tonic-gate 755895Syz147064 (void) snprintf(name, MAXNAMELEN, "dls_link_t_%p_hash", buf); 768275SEric Cheng dlp->dl_str_hash = mod_hash_create_idhash(name, IMPL_HASHSZ, 77269Sericheng mod_hash_null_valdtor); 780Sstevel@tonic-gate 790Sstevel@tonic-gate return (0); 800Sstevel@tonic-gate } 810Sstevel@tonic-gate 820Sstevel@tonic-gate /*ARGSUSED*/ 830Sstevel@tonic-gate static void 840Sstevel@tonic-gate i_dls_link_destructor(void *buf, void *arg) 850Sstevel@tonic-gate { 860Sstevel@tonic-gate dls_link_t *dlp = buf; 870Sstevel@tonic-gate 880Sstevel@tonic-gate ASSERT(dlp->dl_ref == 0); 890Sstevel@tonic-gate ASSERT(dlp->dl_mh == NULL); 908275SEric Cheng ASSERT(dlp->dl_mah == NULL); 910Sstevel@tonic-gate ASSERT(dlp->dl_unknowns == 0); 920Sstevel@tonic-gate 938275SEric Cheng mod_hash_destroy_idhash(dlp->dl_str_hash); 948275SEric Cheng dlp->dl_str_hash = NULL; 950Sstevel@tonic-gate 960Sstevel@tonic-gate } 970Sstevel@tonic-gate 982311Sseb /* 992760Sdg199075 * - Parse the mac header information of the given packet. 1002760Sdg199075 * - Strip the padding and skip over the header. Note that because some 1012760Sdg199075 * DLS consumers only check the db_ref count of the first mblk, we 1023037Syz147064 * pullup the message into a single mblk. Because the original message 10310734SEric Cheng * is freed as the result of message pulling up, mac_vlan_header_info() 1043037Syz147064 * is called again to update the mhi_saddr and mhi_daddr pointers in the 10510734SEric Cheng * mhip. Further, the mac_vlan_header_info() function ensures that the 1063037Syz147064 * size of the pulled message is greater than the MAC header size, 1073037Syz147064 * therefore we can directly advance b_rptr to point at the payload. 1082760Sdg199075 * 1092760Sdg199075 * We choose to use a macro for performance reasons. 1102760Sdg199075 */ 11110734SEric Cheng #define DLS_PREPARE_PKT(mh, mp, mhip, err) { \ 1122760Sdg199075 mblk_t *nextp = (mp)->b_next; \ 11310734SEric Cheng if (((err) = mac_vlan_header_info((mh), (mp), (mhip))) == 0) { \ 1142760Sdg199075 DLS_STRIP_PADDING((mhip)->mhi_pktsize, (mp)); \ 1152760Sdg199075 if (MBLKL((mp)) < (mhip)->mhi_hdrsize) { \ 1162760Sdg199075 mblk_t *newmp; \ 1172760Sdg199075 if ((newmp = msgpullup((mp), -1)) == NULL) { \ 1182760Sdg199075 (err) = EINVAL; \ 1192760Sdg199075 } else { \ 1203037Syz147064 (mp)->b_next = NULL; \ 1212760Sdg199075 freemsg((mp)); \ 1222760Sdg199075 (mp) = newmp; \ 12310734SEric Cheng VERIFY(mac_vlan_header_info((mh), \ 1243037Syz147064 (mp), (mhip)) == 0); \ 1252760Sdg199075 (mp)->b_next = nextp; \ 1262760Sdg199075 (mp)->b_rptr += (mhip)->mhi_hdrsize; \ 1272760Sdg199075 } \ 1282760Sdg199075 } else { \ 1292760Sdg199075 (mp)->b_rptr += (mhip)->mhi_hdrsize; \ 1302760Sdg199075 } \ 1312760Sdg199075 } \ 1322760Sdg199075 } 1332760Sdg199075 1342760Sdg199075 /* 1352311Sseb * Truncate the chain starting at mp such that all packets in the chain 1362760Sdg199075 * have identical source and destination addresses, saps, and tag types 1372760Sdg199075 * (see below). It returns a pointer to the mblk following the chain, 1382760Sdg199075 * NULL if there is no further packet following the processed chain. 1392760Sdg199075 * The countp argument is set to the number of valid packets in the chain. 1402760Sdg199075 * Note that the whole MAC header (including the VLAN tag if any) in each 1412760Sdg199075 * packet will be stripped. 1422311Sseb */ 1430Sstevel@tonic-gate static mblk_t * 1442760Sdg199075 i_dls_link_subchain(dls_link_t *dlp, mblk_t *mp, const mac_header_info_t *mhip, 1452760Sdg199075 uint_t *countp) 1460Sstevel@tonic-gate { 1472760Sdg199075 mblk_t *prevp; 1482760Sdg199075 uint_t npacket = 1; 1492311Sseb size_t addr_size = dlp->dl_mip->mi_addr_length; 1502760Sdg199075 uint16_t vid = VLAN_ID(mhip->mhi_tci); 1512760Sdg199075 uint16_t pri = VLAN_PRI(mhip->mhi_tci); 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate /* 1540Sstevel@tonic-gate * Compare with subsequent headers until we find one that has 1551502Sericheng * differing header information. After checking each packet 1561502Sericheng * strip padding and skip over the header. 1570Sstevel@tonic-gate */ 1582760Sdg199075 for (prevp = mp; (mp = mp->b_next) != NULL; prevp = mp) { 1592311Sseb mac_header_info_t cmhi; 1602760Sdg199075 uint16_t cvid, cpri; 1612760Sdg199075 int err; 1622311Sseb 16310734SEric Cheng DLS_PREPARE_PKT(dlp->dl_mh, mp, &cmhi, err); 1642760Sdg199075 if (err != 0) 1650Sstevel@tonic-gate break; 1662311Sseb 1672760Sdg199075 prevp->b_next = mp; 1682760Sdg199075 1692311Sseb /* 17010700SEric Cheng * The source, destination, sap, vlan tag must all match in 17110700SEric Cheng * a given subchain. 1722311Sseb */ 17310700SEric Cheng if (mhip->mhi_saddr == NULL || cmhi.mhi_saddr == NULL || 17410700SEric Cheng memcmp(mhip->mhi_daddr, cmhi.mhi_daddr, addr_size) != 0 || 1752311Sseb memcmp(mhip->mhi_saddr, cmhi.mhi_saddr, addr_size) != 0 || 1768275SEric Cheng mhip->mhi_bindsap != cmhi.mhi_bindsap) { 1772760Sdg199075 /* 1782760Sdg199075 * Note that we don't need to restore the padding. 1792760Sdg199075 */ 1802760Sdg199075 mp->b_rptr -= cmhi.mhi_hdrsize; 1812311Sseb break; 1822311Sseb } 1832311Sseb 1842760Sdg199075 cvid = VLAN_ID(cmhi.mhi_tci); 1852760Sdg199075 cpri = VLAN_PRI(cmhi.mhi_tci); 1862311Sseb 1872760Sdg199075 /* 1882760Sdg199075 * There are several types of packets. Packets don't match 1892760Sdg199075 * if they are classified to different type or if they are 1902760Sdg199075 * VLAN packets but belong to different VLANs: 1912760Sdg199075 * 1922760Sdg199075 * packet type tagged vid pri 1932760Sdg199075 * --------------------------------------------------------- 1942760Sdg199075 * untagged No zero zero 1952760Sdg199075 * VLAN packets Yes non-zero - 1962760Sdg199075 * priority tagged Yes zero non-zero 1972760Sdg199075 * 0 tagged Yes zero zero 1982760Sdg199075 */ 1992760Sdg199075 if ((mhip->mhi_istagged != cmhi.mhi_istagged) || 2002760Sdg199075 (vid != cvid) || ((vid == VLAN_ID_NONE) && 2012760Sdg199075 (((pri == 0) && (cpri != 0)) || 2022760Sdg199075 ((pri != 0) && (cpri == 0))))) { 2032760Sdg199075 mp->b_rptr -= cmhi.mhi_hdrsize; 2042760Sdg199075 break; 2052760Sdg199075 } 2062760Sdg199075 2070Sstevel@tonic-gate npacket++; 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate /* 2110Sstevel@tonic-gate * Break the chain at this point and return a pointer to the next 2120Sstevel@tonic-gate * sub-chain. 2130Sstevel@tonic-gate */ 2142760Sdg199075 prevp->b_next = NULL; 2150Sstevel@tonic-gate *countp = npacket; 2162760Sdg199075 return (mp); 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2198275SEric Cheng /* ARGSUSED */ 2208275SEric Cheng static int 2218275SEric Cheng i_dls_head_hold(mod_hash_key_t key, mod_hash_val_t val) 222269Sericheng { 2238275SEric Cheng dls_head_t *dhp = (dls_head_t *)val; 2248275SEric Cheng 2258275SEric Cheng /* 2268275SEric Cheng * The lock order is mod_hash's internal lock -> dh_lock as in the 2278275SEric Cheng * call to i_dls_link_rx -> mod_hash_find_cb_rval -> i_dls_head_hold 2288275SEric Cheng */ 2298275SEric Cheng mutex_enter(&dhp->dh_lock); 2308275SEric Cheng if (dhp->dh_removing) { 2318275SEric Cheng mutex_exit(&dhp->dh_lock); 2328275SEric Cheng return (-1); 2338275SEric Cheng } 2348275SEric Cheng dhp->dh_ref++; 2358275SEric Cheng mutex_exit(&dhp->dh_lock); 2368275SEric Cheng return (0); 237269Sericheng } 238269Sericheng 2398275SEric Cheng void 240269Sericheng i_dls_head_rele(dls_head_t *dhp) 241269Sericheng { 2428275SEric Cheng mutex_enter(&dhp->dh_lock); 2438275SEric Cheng dhp->dh_ref--; 2448275SEric Cheng if (dhp->dh_ref == 0 && dhp->dh_removing != 0) 2458275SEric Cheng cv_broadcast(&dhp->dh_cv); 2468275SEric Cheng mutex_exit(&dhp->dh_lock); 247269Sericheng } 248269Sericheng 249269Sericheng static dls_head_t * 250269Sericheng i_dls_head_alloc(mod_hash_key_t key) 251269Sericheng { 252269Sericheng dls_head_t *dhp; 253269Sericheng 254269Sericheng dhp = kmem_zalloc(sizeof (dls_head_t), KM_SLEEP); 255269Sericheng dhp->dh_key = key; 256269Sericheng return (dhp); 257269Sericheng } 258269Sericheng 259269Sericheng static void 260269Sericheng i_dls_head_free(dls_head_t *dhp) 261269Sericheng { 262269Sericheng ASSERT(dhp->dh_ref == 0); 263269Sericheng kmem_free(dhp, sizeof (dls_head_t)); 264269Sericheng } 265269Sericheng 2662760Sdg199075 /* 2672760Sdg199075 * Try to send mp up to the streams of the given sap and vid. Return B_TRUE 2682760Sdg199075 * if this message is sent to any streams. 2692760Sdg199075 * Note that this function will copy the message chain and the original 2702760Sdg199075 * mp will remain valid after this function 2712760Sdg199075 */ 2722760Sdg199075 static uint_t 2732760Sdg199075 i_dls_link_rx_func(dls_link_t *dlp, mac_resource_handle_t mrh, 2748275SEric Cheng mac_header_info_t *mhip, mblk_t *mp, uint32_t sap, 2752760Sdg199075 boolean_t (*acceptfunc)()) 2762760Sdg199075 { 2778275SEric Cheng mod_hash_t *hash = dlp->dl_str_hash; 2782760Sdg199075 mod_hash_key_t key; 2792760Sdg199075 dls_head_t *dhp; 2808275SEric Cheng dld_str_t *dsp; 2812760Sdg199075 mblk_t *nmp; 2828275SEric Cheng dls_rx_t ds_rx; 2838275SEric Cheng void *ds_rx_arg; 2842760Sdg199075 uint_t naccepted = 0; 2858275SEric Cheng int rval; 2862760Sdg199075 2872760Sdg199075 /* 2882760Sdg199075 * Construct a hash key from the VLAN identifier and the 2898275SEric Cheng * DLSAP that represents dld_str_t in promiscuous mode. 2902760Sdg199075 */ 2918275SEric Cheng key = MAKE_KEY(sap); 2922760Sdg199075 2932760Sdg199075 /* 2948275SEric Cheng * Search the hash table for dld_str_t eligible to receive 2958275SEric Cheng * a packet chain for this DLSAP/VLAN combination. The mod hash's 2968275SEric Cheng * internal lock serializes find/insert/remove from the mod hash list. 2978275SEric Cheng * Incrementing the dh_ref (while holding the mod hash lock) ensures 2988275SEric Cheng * dls_link_remove will wait for the upcall to finish. 2992760Sdg199075 */ 3008275SEric Cheng if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp, 3018275SEric Cheng i_dls_head_hold, &rval) != 0 || (rval != 0)) { 3022760Sdg199075 return (B_FALSE); 3032760Sdg199075 } 3042760Sdg199075 3052760Sdg199075 /* 3068275SEric Cheng * Find dld_str_t that will accept the sub-chain. 3072760Sdg199075 */ 3088275SEric Cheng for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next) { 3098275SEric Cheng if (!acceptfunc(dsp, mhip, &ds_rx, &ds_rx_arg)) 3102760Sdg199075 continue; 3112760Sdg199075 3122760Sdg199075 /* 3132760Sdg199075 * We have at least one acceptor. 3142760Sdg199075 */ 3158275SEric Cheng naccepted++; 3162760Sdg199075 3172760Sdg199075 /* 3188275SEric Cheng * There will normally be at least more dld_str_t 3192760Sdg199075 * (since we've yet to check for non-promiscuous 3208275SEric Cheng * dld_str_t) so dup the sub-chain. 3212760Sdg199075 */ 3222760Sdg199075 if ((nmp = copymsgchain(mp)) != NULL) 3238275SEric Cheng ds_rx(ds_rx_arg, mrh, nmp, mhip); 3242760Sdg199075 } 3252760Sdg199075 3262760Sdg199075 /* 3278275SEric Cheng * Release the hold on the dld_str_t chain now that we have 3282760Sdg199075 * finished walking it. 3292760Sdg199075 */ 3302760Sdg199075 i_dls_head_rele(dhp); 3312760Sdg199075 return (naccepted); 3322760Sdg199075 } 3332760Sdg199075 3348275SEric Cheng /* ARGSUSED */ 3358275SEric Cheng void 3368275SEric Cheng i_dls_link_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp, 3378275SEric Cheng boolean_t loopback) 3380Sstevel@tonic-gate { 3390Sstevel@tonic-gate dls_link_t *dlp = arg; 3408275SEric Cheng mod_hash_t *hash = dlp->dl_str_hash; 3410Sstevel@tonic-gate mblk_t *nextp; 3422311Sseb mac_header_info_t mhi; 343269Sericheng dls_head_t *dhp; 3448275SEric Cheng dld_str_t *dsp; 3458275SEric Cheng dld_str_t *ndsp; 3460Sstevel@tonic-gate mblk_t *nmp; 347269Sericheng mod_hash_key_t key; 3480Sstevel@tonic-gate uint_t npacket; 3490Sstevel@tonic-gate boolean_t accepted; 3508275SEric Cheng dls_rx_t ds_rx, nds_rx; 3518275SEric Cheng void *ds_rx_arg, *nds_rx_arg; 3522760Sdg199075 uint16_t vid; 3538275SEric Cheng int err, rval; 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate /* 3560Sstevel@tonic-gate * Walk the packet chain. 3570Sstevel@tonic-gate */ 3582760Sdg199075 for (; mp != NULL; mp = nextp) { 3590Sstevel@tonic-gate /* 3600Sstevel@tonic-gate * Wipe the accepted state. 3610Sstevel@tonic-gate */ 3620Sstevel@tonic-gate accepted = B_FALSE; 3630Sstevel@tonic-gate 36410734SEric Cheng DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err); 3652760Sdg199075 if (err != 0) { 3662760Sdg199075 atomic_add_32(&(dlp->dl_unknowns), 1); 3672760Sdg199075 nextp = mp->b_next; 3683037Syz147064 mp->b_next = NULL; 3692760Sdg199075 freemsg(mp); 3702760Sdg199075 continue; 3712760Sdg199075 } 3722760Sdg199075 3730Sstevel@tonic-gate /* 3740Sstevel@tonic-gate * Grab the longest sub-chain we can process as a single 3750Sstevel@tonic-gate * unit. 3760Sstevel@tonic-gate */ 3772760Sdg199075 nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket); 3782760Sdg199075 ASSERT(npacket != 0); 3790Sstevel@tonic-gate 3802760Sdg199075 vid = VLAN_ID(mhi.mhi_tci); 3812760Sdg199075 3822760Sdg199075 if (mhi.mhi_istagged) { 3832311Sseb /* 3842760Sdg199075 * If it is tagged traffic, send it upstream to 3858275SEric Cheng * all dld_str_t which are attached to the physical 3862760Sdg199075 * link and bound to SAP 0x8100. 3872311Sseb */ 3882760Sdg199075 if (i_dls_link_rx_func(dlp, mrh, &mhi, mp, 3898275SEric Cheng ETHERTYPE_VLAN, dls_accept) > 0) { 3902760Sdg199075 accepted = B_TRUE; 3912760Sdg199075 } 3922760Sdg199075 3932760Sdg199075 /* 3942760Sdg199075 * Don't pass the packets up if they are tagged 3952760Sdg199075 * packets and: 39610491SRishi.Srivatsavai@Sun.COM * - their VID and priority are both zero and the 39710491SRishi.Srivatsavai@Sun.COM * original packet isn't using the PVID (invalid 3982760Sdg199075 * packets). 3992760Sdg199075 * - their sap is ETHERTYPE_VLAN and their VID is 4002760Sdg199075 * zero as they have already been sent upstreams. 4012760Sdg199075 */ 40210491SRishi.Srivatsavai@Sun.COM if ((vid == VLAN_ID_NONE && !mhi.mhi_ispvid && 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 */ 4158275SEric Cheng key = MAKE_KEY(mhi.mhi_bindsap); 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate /* 4188275SEric Cheng * Search the has table for dld_str_t eligible to receive 4190Sstevel@tonic-gate * a packet chain for this DLSAP/VLAN combination. 4200Sstevel@tonic-gate */ 4218275SEric Cheng if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp, 4228275SEric Cheng i_dls_head_hold, &rval) != 0 || (rval != 0)) { 4230Sstevel@tonic-gate freemsgchain(mp); 4240Sstevel@tonic-gate goto loop; 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate /* 4288275SEric Cheng * Find the first dld_str_t that will accept the sub-chain. 4290Sstevel@tonic-gate */ 4308275SEric Cheng for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next) 4318275SEric Cheng if (dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg)) 4320Sstevel@tonic-gate break; 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate /* 4358275SEric Cheng * If we did not find any dld_str_t willing to accept the 4360Sstevel@tonic-gate * sub-chain then throw it away. 4370Sstevel@tonic-gate */ 4388275SEric Cheng if (dsp == NULL) { 439269Sericheng i_dls_head_rele(dhp); 4400Sstevel@tonic-gate freemsgchain(mp); 4410Sstevel@tonic-gate goto loop; 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate /* 4450Sstevel@tonic-gate * We have at least one acceptor. 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate accepted = B_TRUE; 4480Sstevel@tonic-gate for (;;) { 4490Sstevel@tonic-gate /* 4508275SEric Cheng * Find the next dld_str_t that will accept the 4510Sstevel@tonic-gate * sub-chain. 4520Sstevel@tonic-gate */ 4538275SEric Cheng for (ndsp = dsp->ds_next; ndsp != NULL; 4548275SEric Cheng ndsp = ndsp->ds_next) 4558275SEric Cheng if (dls_accept(ndsp, &mhi, &nds_rx, 4568275SEric Cheng &nds_rx_arg)) 4570Sstevel@tonic-gate break; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate /* 4608275SEric Cheng * If there are no more dld_str_t that are willing 4610Sstevel@tonic-gate * to accept the sub-chain then we don't need to dup 4620Sstevel@tonic-gate * it before handing it to the current one. 4630Sstevel@tonic-gate */ 4648275SEric Cheng if (ndsp == NULL) { 4658275SEric Cheng ds_rx(ds_rx_arg, mrh, mp, &mhi); 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate /* 4688275SEric Cheng * Since there are no more dld_str_t, we're 4690Sstevel@tonic-gate * done. 4700Sstevel@tonic-gate */ 4710Sstevel@tonic-gate break; 4720Sstevel@tonic-gate } 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate /* 4758275SEric Cheng * There are more dld_str_t so dup the sub-chain. 4760Sstevel@tonic-gate */ 4770Sstevel@tonic-gate if ((nmp = copymsgchain(mp)) != NULL) 4788275SEric Cheng ds_rx(ds_rx_arg, mrh, nmp, &mhi); 4790Sstevel@tonic-gate 4808275SEric Cheng dsp = ndsp; 4818275SEric Cheng ds_rx = nds_rx; 4828275SEric Cheng ds_rx_arg = nds_rx_arg; 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate /* 4868275SEric Cheng * Release the hold on the dld_str_t chain now that we have 4870Sstevel@tonic-gate * finished walking it. 4880Sstevel@tonic-gate */ 489269Sericheng i_dls_head_rele(dhp); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate loop: 4920Sstevel@tonic-gate /* 4930Sstevel@tonic-gate * If there were no acceptors then add the packet count to the 4940Sstevel@tonic-gate * 'unknown' count. 4950Sstevel@tonic-gate */ 4960Sstevel@tonic-gate if (!accepted) 4970Sstevel@tonic-gate atomic_add_32(&(dlp->dl_unknowns), npacket); 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate 5018275SEric Cheng /* ARGSUSED */ 5028275SEric Cheng void 5038275SEric Cheng dls_rx_vlan_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp, 5048275SEric Cheng boolean_t loopback) 5052760Sdg199075 { 5068275SEric Cheng dld_str_t *dsp = arg; 5078275SEric Cheng dls_link_t *dlp = dsp->ds_dlp; 5088275SEric Cheng mac_header_info_t mhi; 5098275SEric Cheng dls_rx_t ds_rx; 5108275SEric Cheng void *ds_rx_arg; 5118275SEric Cheng int err; 5128275SEric Cheng 51310734SEric Cheng DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err); 5148275SEric Cheng if (err != 0) 5158275SEric Cheng goto drop; 5168275SEric Cheng 5178275SEric Cheng /* 5188275SEric Cheng * If there is promiscuous handle for vlan, we filter out the untagged 5198275SEric Cheng * pkts and pkts that are not for the primary unicast address. 5208275SEric Cheng */ 5218275SEric Cheng if (dsp->ds_vlan_mph != NULL) { 5228275SEric Cheng uint8_t prim_addr[MAXMACADDRLEN]; 5238275SEric Cheng size_t addr_length = dsp->ds_mip->mi_addr_length; 5242760Sdg199075 5258275SEric Cheng if (!(mhi.mhi_istagged)) 5268275SEric Cheng goto drop; 5278275SEric Cheng ASSERT(dsp->ds_mh != NULL); 5288275SEric Cheng mac_unicast_primary_get(dsp->ds_mh, (uint8_t *)prim_addr); 5298275SEric Cheng if (memcmp(mhi.mhi_daddr, prim_addr, addr_length) != 0) 5308275SEric Cheng goto drop; 5318275SEric Cheng 5328275SEric Cheng if (!dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg)) 5338275SEric Cheng goto drop; 5348275SEric Cheng 5358275SEric Cheng ds_rx(ds_rx_arg, NULL, mp, &mhi); 5368275SEric Cheng return; 5378275SEric Cheng } 5388275SEric Cheng 5398275SEric Cheng drop: 5408275SEric Cheng atomic_add_32(&dlp->dl_unknowns, 1); 5418275SEric Cheng freemsg(mp); 5428275SEric Cheng } 5432760Sdg199075 5448275SEric Cheng /* ARGSUSED */ 5458275SEric Cheng void 5468275SEric Cheng dls_rx_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp, 5478275SEric Cheng boolean_t loopback) 5488275SEric Cheng { 5498275SEric Cheng dld_str_t *dsp = arg; 5508275SEric Cheng dls_link_t *dlp = dsp->ds_dlp; 5518275SEric Cheng mac_header_info_t mhi; 5528275SEric Cheng dls_rx_t ds_rx; 5538275SEric Cheng void *ds_rx_arg; 5548275SEric Cheng int err; 5558275SEric Cheng dls_head_t *dhp; 5568275SEric Cheng mod_hash_key_t key; 5578275SEric Cheng 55810734SEric Cheng DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err); 5598275SEric Cheng if (err != 0) 5608275SEric Cheng goto drop; 5618275SEric Cheng 5628275SEric Cheng /* 5638275SEric Cheng * In order to filter out sap pkt that no dls channel listens, search 5648275SEric Cheng * the hash table trying to find a dld_str_t eligible to receive the pkt 5658275SEric Cheng */ 5668275SEric Cheng if ((dsp->ds_promisc & DLS_PROMISC_SAP) == 0) { 5678275SEric Cheng key = MAKE_KEY(mhi.mhi_bindsap); 5688275SEric Cheng if (mod_hash_find(dsp->ds_dlp->dl_str_hash, key, 5698275SEric Cheng (mod_hash_val_t *)&dhp) != 0) 5708275SEric Cheng goto drop; 5712760Sdg199075 } 5728275SEric Cheng 5738275SEric Cheng if (!dls_accept_promisc(dsp, &mhi, &ds_rx, &ds_rx_arg, loopback)) 5748275SEric Cheng goto drop; 5758275SEric Cheng 5768275SEric Cheng ds_rx(ds_rx_arg, NULL, mp, &mhi); 5778275SEric Cheng return; 5788275SEric Cheng 5798275SEric Cheng drop: 5808275SEric Cheng atomic_add_32(&dlp->dl_unknowns, 1); 5818275SEric Cheng freemsg(mp); 5822760Sdg199075 } 5832760Sdg199075 5840Sstevel@tonic-gate static void 5858275SEric Cheng i_dls_link_destroy(dls_link_t *dlp) 5860Sstevel@tonic-gate { 5878275SEric Cheng ASSERT(dlp->dl_nactive == 0); 5888275SEric Cheng ASSERT(dlp->dl_impl_count == 0); 5898275SEric Cheng ASSERT(dlp->dl_zone_ref == 0); 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate /* 5928275SEric Cheng * Free the structure back to the cache. 5930Sstevel@tonic-gate */ 5948275SEric Cheng if (dlp->dl_mch != NULL) 5958275SEric Cheng mac_client_close(dlp->dl_mch, 0); 5960Sstevel@tonic-gate 5978275SEric Cheng if (dlp->dl_mh != NULL) { 5988275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 5998275SEric Cheng mac_close(dlp->dl_mh); 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate 6028275SEric Cheng dlp->dl_mh = NULL; 6038275SEric Cheng dlp->dl_mch = NULL; 6048275SEric Cheng dlp->dl_mip = NULL; 6058275SEric Cheng dlp->dl_unknowns = 0; 606*11021SEric.Cheng@Sun.COM dlp->dl_nonip_cnt = 0; 6078275SEric Cheng kmem_cache_free(i_dls_link_cachep, dlp); 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate static int 6115733Syz147064 i_dls_link_create(const char *name, dls_link_t **dlpp) 6120Sstevel@tonic-gate { 6130Sstevel@tonic-gate dls_link_t *dlp; 6148275SEric Cheng int err; 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate /* 6170Sstevel@tonic-gate * Allocate a new dls_link_t structure. 6180Sstevel@tonic-gate */ 6190Sstevel@tonic-gate dlp = kmem_cache_alloc(i_dls_link_cachep, KM_SLEEP); 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate /* 6220Sstevel@tonic-gate * Name the dls_link_t after the MAC interface it represents. 6230Sstevel@tonic-gate */ 6242311Sseb (void) strlcpy(dlp->dl_name, name, sizeof (dlp->dl_name)); 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate /* 6278275SEric Cheng * First reference; hold open the MAC interface. 6280Sstevel@tonic-gate */ 6298275SEric Cheng ASSERT(dlp->dl_mh == NULL); 6308275SEric Cheng err = mac_open(dlp->dl_name, &dlp->dl_mh); 6318275SEric Cheng if (err != 0) 6328275SEric Cheng goto bail; 6338275SEric Cheng 6348275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 6358275SEric Cheng dlp->dl_mip = mac_info(dlp->dl_mh); 6368275SEric Cheng 6378275SEric Cheng /* DLS is the "primary" MAC client */ 6388275SEric Cheng ASSERT(dlp->dl_mch == NULL); 6398275SEric Cheng 6408275SEric Cheng err = mac_client_open(dlp->dl_mh, &dlp->dl_mch, NULL, 6418275SEric Cheng MAC_OPEN_FLAGS_USE_DATALINK_NAME); 6428275SEric Cheng if (err != 0) 6438275SEric Cheng goto bail; 6448275SEric Cheng 6458275SEric Cheng DTRACE_PROBE2(dls__primary__client, char *, dlp->dl_name, void *, 6468275SEric Cheng dlp->dl_mch); 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate *dlpp = dlp; 6490Sstevel@tonic-gate return (0); 6500Sstevel@tonic-gate 6518275SEric Cheng bail: 6528275SEric Cheng i_dls_link_destroy(dlp); 6538275SEric Cheng return (err); 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate /* 6570Sstevel@tonic-gate * Module initialization functions. 6580Sstevel@tonic-gate */ 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate void 6610Sstevel@tonic-gate dls_link_init(void) 6620Sstevel@tonic-gate { 6630Sstevel@tonic-gate /* 6640Sstevel@tonic-gate * Create a kmem_cache of dls_link_t structures. 6650Sstevel@tonic-gate */ 6660Sstevel@tonic-gate i_dls_link_cachep = kmem_cache_create("dls_link_cache", 6670Sstevel@tonic-gate sizeof (dls_link_t), 0, i_dls_link_constructor, 6680Sstevel@tonic-gate i_dls_link_destructor, NULL, NULL, NULL, 0); 6690Sstevel@tonic-gate ASSERT(i_dls_link_cachep != NULL); 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate /* 672269Sericheng * Create a dls_link_t hash table and associated lock. 6730Sstevel@tonic-gate */ 674269Sericheng i_dls_link_hash = mod_hash_create_extended("dls_link_hash", 675269Sericheng IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor, 676269Sericheng mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP); 677269Sericheng i_dls_link_count = 0; 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate int 6810Sstevel@tonic-gate dls_link_fini(void) 6820Sstevel@tonic-gate { 683269Sericheng if (i_dls_link_count > 0) 684269Sericheng return (EBUSY); 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate /* 6870Sstevel@tonic-gate * Destroy the kmem_cache. 6880Sstevel@tonic-gate */ 6890Sstevel@tonic-gate kmem_cache_destroy(i_dls_link_cachep); 690269Sericheng 691269Sericheng /* 692269Sericheng * Destroy the hash table and associated lock. 693269Sericheng */ 694269Sericheng mod_hash_destroy_hash(i_dls_link_hash); 6950Sstevel@tonic-gate return (0); 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate /* 6990Sstevel@tonic-gate * Exported functions. 7000Sstevel@tonic-gate */ 7010Sstevel@tonic-gate 7028275SEric Cheng static int 7038275SEric Cheng dls_link_hold_common(const char *name, dls_link_t **dlpp, boolean_t create) 7040Sstevel@tonic-gate { 7050Sstevel@tonic-gate dls_link_t *dlp; 7060Sstevel@tonic-gate int err; 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate /* 7098275SEric Cheng * Look up a dls_link_t corresponding to the given macname in the 7108275SEric Cheng * global hash table. The i_dls_link_hash itself is protected by the 7118275SEric Cheng * mod_hash package's internal lock which synchronizes 7128275SEric Cheng * find/insert/remove into the global mod_hash list. Assumes that 7138275SEric Cheng * inserts and removes are single threaded on a per mac end point 7148275SEric Cheng * by the mac perimeter. 7150Sstevel@tonic-gate */ 716269Sericheng if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name, 717269Sericheng (mod_hash_val_t *)&dlp)) == 0) 7180Sstevel@tonic-gate goto done; 719269Sericheng 720269Sericheng ASSERT(err == MH_ERR_NOTFOUND); 7218275SEric Cheng if (!create) 7228275SEric Cheng return (ENOENT); 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate /* 7250Sstevel@tonic-gate * We didn't find anything so we need to create one. 7260Sstevel@tonic-gate */ 7278275SEric Cheng if ((err = i_dls_link_create(name, &dlp)) != 0) 7280Sstevel@tonic-gate return (err); 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate /* 731269Sericheng * Insert the dls_link_t. 7320Sstevel@tonic-gate */ 7335895Syz147064 err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)dlp->dl_name, 734269Sericheng (mod_hash_val_t)dlp); 7350Sstevel@tonic-gate ASSERT(err == 0); 7360Sstevel@tonic-gate 7378275SEric Cheng atomic_add_32(&i_dls_link_count, 1); 738269Sericheng ASSERT(i_dls_link_count != 0); 739269Sericheng 7400Sstevel@tonic-gate done: 7418275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 7420Sstevel@tonic-gate /* 7430Sstevel@tonic-gate * Bump the reference count and hand back the reference. 7440Sstevel@tonic-gate */ 7450Sstevel@tonic-gate dlp->dl_ref++; 7460Sstevel@tonic-gate *dlpp = dlp; 747269Sericheng return (0); 7480Sstevel@tonic-gate } 7490Sstevel@tonic-gate 7508275SEric Cheng int 7518275SEric Cheng dls_link_hold_create(const char *name, dls_link_t **dlpp) 7528275SEric Cheng { 7538275SEric Cheng return (dls_link_hold_common(name, dlpp, B_TRUE)); 7548275SEric Cheng } 7558275SEric Cheng 7568275SEric Cheng int 7578275SEric Cheng dls_link_hold(const char *name, dls_link_t **dlpp) 7588275SEric Cheng { 7598275SEric Cheng return (dls_link_hold_common(name, dlpp, B_FALSE)); 7608275SEric Cheng } 7618275SEric Cheng 7628275SEric Cheng dev_info_t * 7638275SEric Cheng dls_link_devinfo(dev_t dev) 7648275SEric Cheng { 7658275SEric Cheng dls_link_t *dlp; 7668275SEric Cheng dev_info_t *dip; 7678275SEric Cheng char macname[MAXNAMELEN]; 7688275SEric Cheng char *drv; 7698275SEric Cheng mac_perim_handle_t mph; 7708275SEric Cheng 7718275SEric Cheng if ((drv = ddi_major_to_name(getmajor(dev))) == NULL) 7728275SEric Cheng return (NULL); 77310654SGarrett.Damore@Sun.COM (void) snprintf(macname, MAXNAMELEN, "%s%d", drv, 77410654SGarrett.Damore@Sun.COM DLS_MINOR2INST(getminor(dev))); 7758275SEric Cheng 7768275SEric Cheng /* 7778275SEric Cheng * The code below assumes that the name constructed above is the 7788275SEric Cheng * macname. This is not the case for legacy devices. Currently this 7798275SEric Cheng * is ok because this function is only called in the getinfo(9e) path, 7808275SEric Cheng * which for a legacy device would directly end up in the driver's 7818275SEric Cheng * getinfo, rather than here 7828275SEric Cheng */ 7838275SEric Cheng if (mac_perim_enter_by_macname(macname, &mph) != 0) 7848275SEric Cheng return (NULL); 7858275SEric Cheng 7868275SEric Cheng if (dls_link_hold(macname, &dlp) != 0) { 7878275SEric Cheng mac_perim_exit(mph); 7888275SEric Cheng return (NULL); 7898275SEric Cheng } 7908275SEric Cheng 7918275SEric Cheng dip = mac_devinfo_get(dlp->dl_mh); 7928275SEric Cheng dls_link_rele(dlp); 7938275SEric Cheng mac_perim_exit(mph); 7948275SEric Cheng 7958275SEric Cheng return (dip); 7968275SEric Cheng } 7978275SEric Cheng 7988275SEric Cheng dev_t 7998275SEric Cheng dls_link_dev(dls_link_t *dlp) 8008275SEric Cheng { 8018275SEric Cheng return (makedevice(ddi_driver_major(mac_devinfo_get(dlp->dl_mh)), 8028275SEric Cheng mac_minor(dlp->dl_mh))); 8038275SEric Cheng } 8048275SEric Cheng 8050Sstevel@tonic-gate void 8060Sstevel@tonic-gate dls_link_rele(dls_link_t *dlp) 8070Sstevel@tonic-gate { 808269Sericheng mod_hash_val_t val; 8090Sstevel@tonic-gate 8108275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 8110Sstevel@tonic-gate /* 8120Sstevel@tonic-gate * Check if there are any more references. 8130Sstevel@tonic-gate */ 8148275SEric Cheng if (--dlp->dl_ref == 0) { 8158275SEric Cheng (void) mod_hash_remove(i_dls_link_hash, 8168275SEric Cheng (mod_hash_key_t)dlp->dl_name, &val); 8178275SEric Cheng ASSERT(dlp == (dls_link_t *)val); 8188275SEric Cheng 8190Sstevel@tonic-gate /* 8208275SEric Cheng * Destroy the dls_link_t. 8210Sstevel@tonic-gate */ 8228275SEric Cheng i_dls_link_destroy(dlp); 8238275SEric Cheng ASSERT(i_dls_link_count > 0); 8248275SEric Cheng atomic_add_32(&i_dls_link_count, -1); 8258275SEric Cheng } 8268275SEric Cheng } 8278275SEric Cheng 8288275SEric Cheng int 8298275SEric Cheng dls_link_rele_by_name(const char *name) 8308275SEric Cheng { 8318275SEric Cheng dls_link_t *dlp; 8328275SEric Cheng 8338275SEric Cheng if (mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name, 8348275SEric Cheng (mod_hash_val_t *)&dlp) != 0) 8358275SEric Cheng return (ENOENT); 8368275SEric Cheng 8378275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 8388275SEric Cheng 8398275SEric Cheng /* 8408275SEric Cheng * Must fail detach if mac client is busy. 8418275SEric Cheng */ 8428275SEric Cheng ASSERT(dlp->dl_ref > 0 && dlp->dl_mch != NULL); 8438275SEric Cheng if (mac_link_has_flows(dlp->dl_mch)) 8448275SEric Cheng return (ENOTEMPTY); 8458275SEric Cheng 8468275SEric Cheng dls_link_rele(dlp); 8478275SEric Cheng return (0); 8488275SEric Cheng } 8498275SEric Cheng 8508275SEric Cheng int 8518275SEric Cheng dls_link_setzid(const char *name, zoneid_t zid) 8528275SEric Cheng { 8538275SEric Cheng dls_link_t *dlp; 8548275SEric Cheng int err = 0; 8558275SEric Cheng zoneid_t old_zid; 8568275SEric Cheng 8578275SEric Cheng if ((err = dls_link_hold_create(name, &dlp)) != 0) 8588275SEric Cheng return (err); 8598275SEric Cheng 8608275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 8618275SEric Cheng 8628275SEric Cheng if ((old_zid = dlp->dl_zid) == zid) 8638275SEric Cheng goto done; 8648275SEric Cheng 8658275SEric Cheng /* 86610616SSebastien.Roy@Sun.COM * Check whether this dlp is used by its own zone. If yes, we cannot 86710616SSebastien.Roy@Sun.COM * change its zoneid. 8688275SEric Cheng */ 8698275SEric Cheng if (dlp->dl_zone_ref != 0) { 8708275SEric Cheng err = EBUSY; 8710Sstevel@tonic-gate goto done; 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate 87410639SDarren.Reed@Sun.COM dls_bpf_newzone(dlp, zid); 87510616SSebastien.Roy@Sun.COM dlp->dl_zid = zid; 87610616SSebastien.Roy@Sun.COM 8778275SEric Cheng if (zid == GLOBAL_ZONEID) { 8788275SEric Cheng /* 87910616SSebastien.Roy@Sun.COM * The link is moving from a non-global zone to the global 88010616SSebastien.Roy@Sun.COM * zone, so we need to release the reference that was held 88110616SSebastien.Roy@Sun.COM * when the link was originally assigned to the non-global 88210616SSebastien.Roy@Sun.COM * zone. 8838275SEric Cheng */ 8848275SEric Cheng dls_link_rele(dlp); 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate 8878275SEric Cheng done: 88810616SSebastien.Roy@Sun.COM /* 88910616SSebastien.Roy@Sun.COM * We only keep the reference to this link open if the link has 89010616SSebastien.Roy@Sun.COM * successfully moved from the global zone to a non-global zone. 89110616SSebastien.Roy@Sun.COM */ 89210616SSebastien.Roy@Sun.COM if (err != 0 || old_zid != GLOBAL_ZONEID) 89310616SSebastien.Roy@Sun.COM dls_link_rele(dlp); 8940Sstevel@tonic-gate return (err); 8950Sstevel@tonic-gate } 8960Sstevel@tonic-gate 89710639SDarren.Reed@Sun.COM 89810639SDarren.Reed@Sun.COM /* 89910639SDarren.Reed@Sun.COM * When a NIC changes zone, that change needs to be communicated to BPF 90010639SDarren.Reed@Sun.COM * so that it can correctly enforce access rights on it via BPF. In the 90110639SDarren.Reed@Sun.COM * absence of a function from BPF to just change the zoneid, this is 90210639SDarren.Reed@Sun.COM * done with a detach followed by an attach. 90310639SDarren.Reed@Sun.COM */ 90410639SDarren.Reed@Sun.COM static void 90510639SDarren.Reed@Sun.COM dls_bpf_newzone(dls_link_t *dlp, zoneid_t zid) 90610639SDarren.Reed@Sun.COM { 90710639SDarren.Reed@Sun.COM if (dls_bpfdetach_fn != NULL) 90810639SDarren.Reed@Sun.COM dls_bpfdetach_fn((uintptr_t)dlp->dl_mh); 90910639SDarren.Reed@Sun.COM 91010639SDarren.Reed@Sun.COM if (dls_bpfattach_fn != NULL) 91110639SDarren.Reed@Sun.COM dls_bpfattach_fn((uintptr_t)dlp->dl_mh, mac_type(dlp->dl_mh), 91210639SDarren.Reed@Sun.COM zid, BPR_MAC); 91310639SDarren.Reed@Sun.COM } 91410639SDarren.Reed@Sun.COM 91510639SDarren.Reed@Sun.COM int 91610639SDarren.Reed@Sun.COM dls_link_getzid(const char *name, zoneid_t *zidp) 91710639SDarren.Reed@Sun.COM { 91810639SDarren.Reed@Sun.COM dls_link_t *dlp; 91910639SDarren.Reed@Sun.COM int err = 0; 92010639SDarren.Reed@Sun.COM 92110639SDarren.Reed@Sun.COM if ((err = dls_link_hold(name, &dlp)) != 0) 92210639SDarren.Reed@Sun.COM return (err); 92310639SDarren.Reed@Sun.COM 92410639SDarren.Reed@Sun.COM ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 92510639SDarren.Reed@Sun.COM 92610639SDarren.Reed@Sun.COM *zidp = dlp->dl_zid; 92710639SDarren.Reed@Sun.COM 92810639SDarren.Reed@Sun.COM dls_link_rele(dlp); 92910639SDarren.Reed@Sun.COM return (0); 93010639SDarren.Reed@Sun.COM } 93110639SDarren.Reed@Sun.COM 9320Sstevel@tonic-gate void 9338275SEric Cheng dls_link_add(dls_link_t *dlp, uint32_t sap, dld_str_t *dsp) 9340Sstevel@tonic-gate { 9358275SEric Cheng mod_hash_t *hash = dlp->dl_str_hash; 936269Sericheng mod_hash_key_t key; 937269Sericheng dls_head_t *dhp; 9388275SEric Cheng dld_str_t *p; 9390Sstevel@tonic-gate int err; 9408275SEric Cheng 9418275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate /* 9448275SEric Cheng * Generate a hash key based on the sap. 9450Sstevel@tonic-gate */ 9468275SEric Cheng key = MAKE_KEY(sap); 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate /* 949269Sericheng * Search the table for a list head with this key. 9500Sstevel@tonic-gate */ 951269Sericheng if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) { 952269Sericheng ASSERT(err == MH_ERR_NOTFOUND); 9530Sstevel@tonic-gate 954269Sericheng dhp = i_dls_head_alloc(key); 955269Sericheng err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp); 956269Sericheng ASSERT(err == 0); 9570Sstevel@tonic-gate } 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate /* 9608275SEric Cheng * Add the dld_str_t to the head of the list. List walkers in 9618275SEric Cheng * i_dls_link_rx_* bump up dh_ref to ensure the list does not change 9628275SEric Cheng * while they walk the list. The membar below ensures that list walkers 9638275SEric Cheng * see exactly the old list or the new list. 964269Sericheng */ 9658275SEric Cheng ASSERT(dsp->ds_next == NULL); 966269Sericheng p = dhp->dh_list; 9678275SEric Cheng dsp->ds_next = p; 9688275SEric Cheng 9698275SEric Cheng membar_producer(); 9708275SEric Cheng 9718275SEric Cheng dhp->dh_list = dsp; 972269Sericheng 973269Sericheng /* 974269Sericheng * Save a pointer to the list head. 975269Sericheng */ 9768275SEric Cheng dsp->ds_head = dhp; 977269Sericheng dlp->dl_impl_count++; 978269Sericheng } 979269Sericheng 980269Sericheng void 9818275SEric Cheng dls_link_remove(dls_link_t *dlp, dld_str_t *dsp) 982269Sericheng { 9838275SEric Cheng mod_hash_t *hash = dlp->dl_str_hash; 9848275SEric Cheng dld_str_t **pp; 9858275SEric Cheng dld_str_t *p; 986269Sericheng dls_head_t *dhp; 9870Sstevel@tonic-gate 9888275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 9890Sstevel@tonic-gate 990269Sericheng /* 9918275SEric Cheng * We set dh_removing here to tell the receive callbacks not to pass 9928275SEric Cheng * up packets anymore. Then wait till the current callbacks are done. 9938275SEric Cheng * This happens either in the close path or in processing the 9948275SEric Cheng * DL_UNBIND_REQ via a taskq thread, and it is ok to cv_wait in either. 9958275SEric Cheng * The dh_ref ensures there aren't and there won't be any upcalls 9968275SEric Cheng * walking or using the dh_list. The mod hash internal lock ensures 9978275SEric Cheng * that the insert/remove of the dls_head_t itself synchronizes with 9988275SEric Cheng * any i_dls_link_rx trying to locate it. The perimeter ensures that 9998275SEric Cheng * there isn't another simultaneous dls_link_add/remove. 1000269Sericheng */ 10018275SEric Cheng dhp = dsp->ds_head; 10028275SEric Cheng 10038275SEric Cheng mutex_enter(&dhp->dh_lock); 10048275SEric Cheng dhp->dh_removing = B_TRUE; 10058275SEric Cheng while (dhp->dh_ref != 0) 10068275SEric Cheng cv_wait(&dhp->dh_cv, &dhp->dh_lock); 10078275SEric Cheng mutex_exit(&dhp->dh_lock); 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate /* 10108275SEric Cheng * Walk the list and remove the dld_str_t. 10110Sstevel@tonic-gate */ 10128275SEric Cheng for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->ds_next)) { 10138275SEric Cheng if (p == dsp) 1014269Sericheng break; 1015269Sericheng } 1016269Sericheng ASSERT(p != NULL); 10178275SEric Cheng *pp = p->ds_next; 10188275SEric Cheng p->ds_next = NULL; 10198275SEric Cheng p->ds_head = NULL; 1020269Sericheng 10218275SEric Cheng ASSERT(dlp->dl_impl_count != 0); 1022269Sericheng dlp->dl_impl_count--; 10230Sstevel@tonic-gate 1024269Sericheng if (dhp->dh_list == NULL) { 1025269Sericheng mod_hash_val_t val = NULL; 1026269Sericheng 1027269Sericheng /* 1028269Sericheng * The list is empty so remove the hash table entry. 1029269Sericheng */ 1030269Sericheng (void) mod_hash_remove(hash, dhp->dh_key, &val); 1031269Sericheng ASSERT(dhp == (dls_head_t *)val); 1032269Sericheng i_dls_head_free(dhp); 10338275SEric Cheng } else { 10348275SEric Cheng mutex_enter(&dhp->dh_lock); 10358275SEric Cheng dhp->dh_removing = B_FALSE; 10368275SEric Cheng mutex_exit(&dhp->dh_lock); 1037269Sericheng } 10380Sstevel@tonic-gate } 1039