1c398230bSWarner Losh /*- 22cc2df49SGarrett Wollman * Copyright 1998 Massachusetts Institute of Technology 32cc2df49SGarrett Wollman * 42cc2df49SGarrett Wollman * Permission to use, copy, modify, and distribute this software and 52cc2df49SGarrett Wollman * its documentation for any purpose and without fee is hereby 62cc2df49SGarrett Wollman * granted, provided that both the above copyright notice and this 72cc2df49SGarrett Wollman * permission notice appear in all copies, that both the above 82cc2df49SGarrett Wollman * copyright notice and this permission notice appear in all 92cc2df49SGarrett Wollman * supporting documentation, and that the name of M.I.T. not be used 102cc2df49SGarrett Wollman * in advertising or publicity pertaining to distribution of the 112cc2df49SGarrett Wollman * software without specific, written prior permission. M.I.T. makes 122cc2df49SGarrett Wollman * no representations about the suitability of this software for any 132cc2df49SGarrett Wollman * purpose. It is provided "as is" without express or implied 142cc2df49SGarrett Wollman * warranty. 152cc2df49SGarrett Wollman * 162cc2df49SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 172cc2df49SGarrett Wollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 182cc2df49SGarrett Wollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 192cc2df49SGarrett Wollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 202cc2df49SGarrett Wollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212cc2df49SGarrett Wollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222cc2df49SGarrett Wollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 232cc2df49SGarrett Wollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 242cc2df49SGarrett Wollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 252cc2df49SGarrett Wollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 262cc2df49SGarrett Wollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 272cc2df49SGarrett Wollman * SUCH DAMAGE. 282cc2df49SGarrett Wollman * 29c3aac50fSPeter Wemm * $FreeBSD$ 302cc2df49SGarrett Wollman */ 312cc2df49SGarrett Wollman 322cc2df49SGarrett Wollman /* 332cc2df49SGarrett Wollman * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 342cc2df49SGarrett Wollman * Might be extended some day to also handle IEEE 802.1p priority 352cc2df49SGarrett Wollman * tagging. This is sort of sneaky in the implementation, since 362cc2df49SGarrett Wollman * we need to pretend to be enough of an Ethernet implementation 372cc2df49SGarrett Wollman * to make arp work. The way we do this is by telling everyone 382cc2df49SGarrett Wollman * that we are an Ethernet, and then catch the packets that 3946a32e61SPhilippe Charnier * ether_output() left on our output queue when it calls 402cc2df49SGarrett Wollman * if_start(), rewrite them for use by the real outgoing interface, 412cc2df49SGarrett Wollman * and ask it to send them. 422cc2df49SGarrett Wollman */ 432cc2df49SGarrett Wollman 4475ee267cSGleb Smirnoff #include "opt_vlan.h" 452cc2df49SGarrett Wollman 462cc2df49SGarrett Wollman #include <sys/param.h> 472cc2df49SGarrett Wollman #include <sys/kernel.h> 4875ee267cSGleb Smirnoff #include <sys/lock.h> 49f731f104SBill Paul #include <sys/malloc.h> 502cc2df49SGarrett Wollman #include <sys/mbuf.h> 512b120974SPeter Wemm #include <sys/module.h> 5275ee267cSGleb Smirnoff #include <sys/rwlock.h> 53f731f104SBill Paul #include <sys/queue.h> 542cc2df49SGarrett Wollman #include <sys/socket.h> 552cc2df49SGarrett Wollman #include <sys/sockio.h> 562cc2df49SGarrett Wollman #include <sys/sysctl.h> 572cc2df49SGarrett Wollman #include <sys/systm.h> 58603724d3SBjoern A. Zeeb #include <sys/vimage.h> 592cc2df49SGarrett Wollman 602cc2df49SGarrett Wollman #include <net/bpf.h> 612cc2df49SGarrett Wollman #include <net/ethernet.h> 622cc2df49SGarrett Wollman #include <net/if.h> 63f889d2efSBrooks Davis #include <net/if_clone.h> 642cc2df49SGarrett Wollman #include <net/if_dl.h> 652cc2df49SGarrett Wollman #include <net/if_types.h> 662cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 672cc2df49SGarrett Wollman 689d4fe4b2SBrooks Davis #define VLANNAME "vlan" 6975ee267cSGleb Smirnoff #define VLAN_DEF_HWIDTH 4 7064a17d2eSYaroslav Tykhiy #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 7175ee267cSGleb Smirnoff 722dc879b3SYaroslav Tykhiy #define UP_AND_RUNNING(ifp) \ 732dc879b3SYaroslav Tykhiy ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 742dc879b3SYaroslav Tykhiy 7575ee267cSGleb Smirnoff LIST_HEAD(ifvlanhead, ifvlan); 7675ee267cSGleb Smirnoff 7775ee267cSGleb Smirnoff struct ifvlantrunk { 7875ee267cSGleb Smirnoff struct ifnet *parent; /* parent interface of this trunk */ 7975ee267cSGleb Smirnoff struct rwlock rw; 8075ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 815cb8c31aSYaroslav Tykhiy #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 825cb8c31aSYaroslav Tykhiy struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 8375ee267cSGleb Smirnoff #else 8475ee267cSGleb Smirnoff struct ifvlanhead *hash; /* dynamic hash-list table */ 8575ee267cSGleb Smirnoff uint16_t hmask; 8675ee267cSGleb Smirnoff uint16_t hwidth; 8775ee267cSGleb Smirnoff #endif 8875ee267cSGleb Smirnoff int refcnt; 8975ee267cSGleb Smirnoff }; 909d4fe4b2SBrooks Davis 91a3814acfSSam Leffler struct vlan_mc_entry { 92a3814acfSSam Leffler struct ether_addr mc_addr; 93a3814acfSSam Leffler SLIST_ENTRY(vlan_mc_entry) mc_entries; 94a3814acfSSam Leffler }; 95a3814acfSSam Leffler 96a3814acfSSam Leffler struct ifvlan { 9775ee267cSGleb Smirnoff struct ifvlantrunk *ifv_trunk; 98fc74a9f9SBrooks Davis struct ifnet *ifv_ifp; 9975ee267cSGleb Smirnoff #define TRUNK(ifv) ((ifv)->ifv_trunk) 10075ee267cSGleb Smirnoff #define PARENT(ifv) ((ifv)->ifv_trunk->parent) 1011cf236fbSYaroslav Tykhiy int ifv_pflags; /* special flags we have set on parent */ 102a3814acfSSam Leffler struct ifv_linkmib { 103a3814acfSSam Leffler int ifvm_encaplen; /* encapsulation length */ 104a3814acfSSam Leffler int ifvm_mtufudge; /* MTU fudged by this much */ 105a3814acfSSam Leffler int ifvm_mintu; /* min transmission unit */ 10673f2233dSYaroslav Tykhiy uint16_t ifvm_proto; /* encapsulation ethertype */ 10775ee267cSGleb Smirnoff uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 108a3814acfSSam Leffler } ifv_mib; 109114c608cSYaroslav Tykhiy SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 110c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY 111a3814acfSSam Leffler LIST_ENTRY(ifvlan) ifv_list; 112c0cb022bSYaroslav Tykhiy #endif 113a3814acfSSam Leffler }; 11473f2233dSYaroslav Tykhiy #define ifv_proto ifv_mib.ifvm_proto 115a3814acfSSam Leffler #define ifv_tag ifv_mib.ifvm_tag 116a3814acfSSam Leffler #define ifv_encaplen ifv_mib.ifvm_encaplen 117a3814acfSSam Leffler #define ifv_mtufudge ifv_mib.ifvm_mtufudge 118a3814acfSSam Leffler #define ifv_mintu ifv_mib.ifvm_mintu 119a3814acfSSam Leffler 12075ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */ 1211cf236fbSYaroslav Tykhiy static struct { 1221cf236fbSYaroslav Tykhiy int flag; 1231cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 1241cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 1251cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 1261cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 1271cf236fbSYaroslav Tykhiy {0, NULL} 1281cf236fbSYaroslav Tykhiy }; 129a3814acfSSam Leffler 1304a408dcbSBill Paul SYSCTL_DECL(_net_link); 131d2a75853SBill Fenner SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 1322cc2df49SGarrett Wollman SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 1332cc2df49SGarrett Wollman 134f6e5e0adSYaroslav Tykhiy static int soft_pad = 0; 135f6e5e0adSYaroslav Tykhiy SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0, 136f6e5e0adSYaroslav Tykhiy "pad short frames before tagging"); 137f6e5e0adSYaroslav Tykhiy 1385e17543aSBrooks Davis static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface"); 1392cc2df49SGarrett Wollman 1405cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag; 1415cb8c31aSYaroslav Tykhiy 1424faedfe8SSam Leffler /* 14375ee267cSGleb Smirnoff * We have a global mutex, that is used to serialize configuration 14475ee267cSGleb Smirnoff * changes and isn't used in normal packet delivery. 14575ee267cSGleb Smirnoff * 14675ee267cSGleb Smirnoff * We also have a per-trunk rwlock, that is locked shared on packet 14775ee267cSGleb Smirnoff * processing and exclusive when configuration is changed. 14875ee267cSGleb Smirnoff * 14975ee267cSGleb Smirnoff * The VLAN_ARRAY substitutes the dynamic hash with a static array 150ad387028SAndrew Thompson * with 4096 entries. In theory this can give a boost in processing, 15175ee267cSGleb Smirnoff * however on practice it does not. Probably this is because array 15275ee267cSGleb Smirnoff * is too big to fit into CPU cache. 1534faedfe8SSam Leffler */ 1544faedfe8SSam Leffler static struct mtx ifv_mtx; 15575ee267cSGleb Smirnoff #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF) 1564faedfe8SSam Leffler #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx) 1574faedfe8SSam Leffler #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED) 1584faedfe8SSam Leffler #define VLAN_LOCK() mtx_lock(&ifv_mtx) 1594faedfe8SSam Leffler #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx) 16075ee267cSGleb Smirnoff #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME) 16175ee267cSGleb Smirnoff #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw) 16275ee267cSGleb Smirnoff #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw) 16375ee267cSGleb Smirnoff #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw) 16475ee267cSGleb Smirnoff #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED) 16575ee267cSGleb Smirnoff #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw) 16675ee267cSGleb Smirnoff #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw) 16775ee267cSGleb Smirnoff #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED) 16875ee267cSGleb Smirnoff 16975ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 17075ee267cSGleb Smirnoff static void vlan_inithash(struct ifvlantrunk *trunk); 17175ee267cSGleb Smirnoff static void vlan_freehash(struct ifvlantrunk *trunk); 17275ee267cSGleb Smirnoff static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 17375ee267cSGleb Smirnoff static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 17475ee267cSGleb Smirnoff static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 17575ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 17675ee267cSGleb Smirnoff uint16_t tag); 17775ee267cSGleb Smirnoff #endif 17875ee267cSGleb Smirnoff static void trunk_destroy(struct ifvlantrunk *trunk); 1794faedfe8SSam Leffler 1802cc2df49SGarrett Wollman static void vlan_start(struct ifnet *ifp); 181114c608cSYaroslav Tykhiy static void vlan_init(void *foo); 182a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 183cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 1841cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 1851cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 1861cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 187f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 188f731f104SBill Paul static int vlan_unconfig(struct ifnet *ifp); 1895cb8c31aSYaroslav Tykhiy static int vlan_unconfig_locked(struct ifnet *ifp); 19075ee267cSGleb Smirnoff static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag); 191127d7b2dSAndre Oppermann static void vlan_link_state(struct ifnet *ifp, int link); 19275ee267cSGleb Smirnoff static void vlan_capabilities(struct ifvlan *ifv); 19375ee267cSGleb Smirnoff static void vlan_trunk_capabilities(struct ifnet *ifp); 194f731f104SBill Paul 195f889d2efSBrooks Davis static struct ifnet *vlan_clone_match_ethertag(struct if_clone *, 196f889d2efSBrooks Davis const char *, int *); 197f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 1986b7330e2SSam Leffler static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 199f889d2efSBrooks Davis static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 200f889d2efSBrooks Davis 2015cb8c31aSYaroslav Tykhiy static void vlan_ifdetach(void *arg, struct ifnet *ifp); 2025cb8c31aSYaroslav Tykhiy 203c6e6ca3eSYaroslav Tykhiy static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL, 204c6e6ca3eSYaroslav Tykhiy IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy); 2059d4fe4b2SBrooks Davis 20675ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 20775ee267cSGleb Smirnoff #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 208114c608cSYaroslav Tykhiy 20975ee267cSGleb Smirnoff static void 21075ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk) 21175ee267cSGleb Smirnoff { 21275ee267cSGleb Smirnoff int i, n; 21375ee267cSGleb Smirnoff 21475ee267cSGleb Smirnoff /* 21575ee267cSGleb Smirnoff * The trunk must not be locked here since we call malloc(M_WAITOK). 21675ee267cSGleb Smirnoff * It is OK in case this function is called before the trunk struct 21775ee267cSGleb Smirnoff * gets hooked up and becomes visible from other threads. 21875ee267cSGleb Smirnoff */ 21975ee267cSGleb Smirnoff 22075ee267cSGleb Smirnoff KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 22175ee267cSGleb Smirnoff ("%s: hash already initialized", __func__)); 22275ee267cSGleb Smirnoff 22375ee267cSGleb Smirnoff trunk->hwidth = VLAN_DEF_HWIDTH; 22475ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 22575ee267cSGleb Smirnoff trunk->hmask = n - 1; 22675ee267cSGleb Smirnoff trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 22775ee267cSGleb Smirnoff for (i = 0; i < n; i++) 22875ee267cSGleb Smirnoff LIST_INIT(&trunk->hash[i]); 22975ee267cSGleb Smirnoff } 23075ee267cSGleb Smirnoff 23175ee267cSGleb Smirnoff static void 23275ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk) 23375ee267cSGleb Smirnoff { 23475ee267cSGleb Smirnoff #ifdef INVARIANTS 23575ee267cSGleb Smirnoff int i; 23675ee267cSGleb Smirnoff 23775ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 23875ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 23975ee267cSGleb Smirnoff KASSERT(LIST_EMPTY(&trunk->hash[i]), 24075ee267cSGleb Smirnoff ("%s: hash table not empty", __func__)); 24175ee267cSGleb Smirnoff #endif 24275ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 24375ee267cSGleb Smirnoff trunk->hash = NULL; 24475ee267cSGleb Smirnoff trunk->hwidth = trunk->hmask = 0; 24575ee267cSGleb Smirnoff } 24675ee267cSGleb Smirnoff 24775ee267cSGleb Smirnoff static int 24875ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 24975ee267cSGleb Smirnoff { 25075ee267cSGleb Smirnoff int i, b; 25175ee267cSGleb Smirnoff struct ifvlan *ifv2; 25275ee267cSGleb Smirnoff 25375ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 25475ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 25575ee267cSGleb Smirnoff 25675ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 25775ee267cSGleb Smirnoff i = HASH(ifv->ifv_tag, trunk->hmask); 25875ee267cSGleb Smirnoff LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 25975ee267cSGleb Smirnoff if (ifv->ifv_tag == ifv2->ifv_tag) 26075ee267cSGleb Smirnoff return (EEXIST); 26175ee267cSGleb Smirnoff 26275ee267cSGleb Smirnoff /* 26375ee267cSGleb Smirnoff * Grow the hash when the number of vlans exceeds half of the number of 26475ee267cSGleb Smirnoff * hash buckets squared. This will make the average linked-list length 26575ee267cSGleb Smirnoff * buckets/2. 26675ee267cSGleb Smirnoff */ 26775ee267cSGleb Smirnoff if (trunk->refcnt > (b * b) / 2) { 26875ee267cSGleb Smirnoff vlan_growhash(trunk, 1); 26975ee267cSGleb Smirnoff i = HASH(ifv->ifv_tag, trunk->hmask); 27075ee267cSGleb Smirnoff } 27175ee267cSGleb Smirnoff LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 27275ee267cSGleb Smirnoff trunk->refcnt++; 27375ee267cSGleb Smirnoff 27475ee267cSGleb Smirnoff return (0); 27575ee267cSGleb Smirnoff } 27675ee267cSGleb Smirnoff 27775ee267cSGleb Smirnoff static int 27875ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 27975ee267cSGleb Smirnoff { 28075ee267cSGleb Smirnoff int i, b; 28175ee267cSGleb Smirnoff struct ifvlan *ifv2; 28275ee267cSGleb Smirnoff 28375ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 28475ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 28575ee267cSGleb Smirnoff 28675ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 28775ee267cSGleb Smirnoff i = HASH(ifv->ifv_tag, trunk->hmask); 28875ee267cSGleb Smirnoff LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 28975ee267cSGleb Smirnoff if (ifv2 == ifv) { 29075ee267cSGleb Smirnoff trunk->refcnt--; 29175ee267cSGleb Smirnoff LIST_REMOVE(ifv2, ifv_list); 29275ee267cSGleb Smirnoff if (trunk->refcnt < (b * b) / 2) 29375ee267cSGleb Smirnoff vlan_growhash(trunk, -1); 29475ee267cSGleb Smirnoff return (0); 29575ee267cSGleb Smirnoff } 29675ee267cSGleb Smirnoff 29775ee267cSGleb Smirnoff panic("%s: vlan not found\n", __func__); 29875ee267cSGleb Smirnoff return (ENOENT); /*NOTREACHED*/ 29975ee267cSGleb Smirnoff } 30075ee267cSGleb Smirnoff 30175ee267cSGleb Smirnoff /* 30275ee267cSGleb Smirnoff * Grow the hash larger or smaller if memory permits. 30375ee267cSGleb Smirnoff */ 30475ee267cSGleb Smirnoff static void 30575ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 30675ee267cSGleb Smirnoff { 30775ee267cSGleb Smirnoff struct ifvlan *ifv; 30875ee267cSGleb Smirnoff struct ifvlanhead *hash2; 30975ee267cSGleb Smirnoff int hwidth2, i, j, n, n2; 31075ee267cSGleb Smirnoff 31175ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 31275ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 31375ee267cSGleb Smirnoff 31475ee267cSGleb Smirnoff if (howmuch == 0) { 31575ee267cSGleb Smirnoff /* Harmless yet obvious coding error */ 31675ee267cSGleb Smirnoff printf("%s: howmuch is 0\n", __func__); 31775ee267cSGleb Smirnoff return; 31875ee267cSGleb Smirnoff } 31975ee267cSGleb Smirnoff 32075ee267cSGleb Smirnoff hwidth2 = trunk->hwidth + howmuch; 32175ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 32275ee267cSGleb Smirnoff n2 = 1 << hwidth2; 32375ee267cSGleb Smirnoff /* Do not shrink the table below the default */ 32475ee267cSGleb Smirnoff if (hwidth2 < VLAN_DEF_HWIDTH) 32575ee267cSGleb Smirnoff return; 32675ee267cSGleb Smirnoff 32775ee267cSGleb Smirnoff /* M_NOWAIT because we're called with trunk mutex held */ 32875ee267cSGleb Smirnoff hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT); 32975ee267cSGleb Smirnoff if (hash2 == NULL) { 33075ee267cSGleb Smirnoff printf("%s: out of memory -- hash size not changed\n", 33175ee267cSGleb Smirnoff __func__); 33275ee267cSGleb Smirnoff return; /* We can live with the old hash table */ 33375ee267cSGleb Smirnoff } 33475ee267cSGleb Smirnoff for (j = 0; j < n2; j++) 33575ee267cSGleb Smirnoff LIST_INIT(&hash2[j]); 33675ee267cSGleb Smirnoff for (i = 0; i < n; i++) 337c0cb022bSYaroslav Tykhiy while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) { 33875ee267cSGleb Smirnoff LIST_REMOVE(ifv, ifv_list); 33975ee267cSGleb Smirnoff j = HASH(ifv->ifv_tag, n2 - 1); 34075ee267cSGleb Smirnoff LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 34175ee267cSGleb Smirnoff } 34275ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 34375ee267cSGleb Smirnoff trunk->hash = hash2; 34475ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 34575ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 346f84b2d69SYaroslav Tykhiy 347f84b2d69SYaroslav Tykhiy if (bootverbose) 348f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 349f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 35075ee267cSGleb Smirnoff } 35175ee267cSGleb Smirnoff 35275ee267cSGleb Smirnoff static __inline struct ifvlan * 35375ee267cSGleb Smirnoff vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag) 35475ee267cSGleb Smirnoff { 35575ee267cSGleb Smirnoff struct ifvlan *ifv; 35675ee267cSGleb Smirnoff 35775ee267cSGleb Smirnoff TRUNK_LOCK_RASSERT(trunk); 35875ee267cSGleb Smirnoff 35975ee267cSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list) 36075ee267cSGleb Smirnoff if (ifv->ifv_tag == tag) 36175ee267cSGleb Smirnoff return (ifv); 36275ee267cSGleb Smirnoff return (NULL); 36375ee267cSGleb Smirnoff } 36475ee267cSGleb Smirnoff 36575ee267cSGleb Smirnoff #if 0 36675ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 36775ee267cSGleb Smirnoff static void 36875ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 36975ee267cSGleb Smirnoff { 37075ee267cSGleb Smirnoff int i; 37175ee267cSGleb Smirnoff struct ifvlan *ifv; 37275ee267cSGleb Smirnoff 37375ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 37475ee267cSGleb Smirnoff printf("%d: ", i); 37575ee267cSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 37675ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 37775ee267cSGleb Smirnoff printf("\n"); 37875ee267cSGleb Smirnoff } 37975ee267cSGleb Smirnoff } 38075ee267cSGleb Smirnoff #endif /* 0 */ 38175ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 38275ee267cSGleb Smirnoff 38375ee267cSGleb Smirnoff static void 38475ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 38575ee267cSGleb Smirnoff { 38675ee267cSGleb Smirnoff VLAN_LOCK_ASSERT(); 38775ee267cSGleb Smirnoff 38875ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 38975ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 39075ee267cSGleb Smirnoff vlan_freehash(trunk); 39175ee267cSGleb Smirnoff #endif 39275ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 39333499e2aSYaroslav Tykhiy TRUNK_UNLOCK(trunk); 39433499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 39575ee267cSGleb Smirnoff free(trunk, M_VLAN); 39675ee267cSGleb Smirnoff } 39775ee267cSGleb Smirnoff 398f731f104SBill Paul /* 399f731f104SBill Paul * Program our multicast filter. What we're actually doing is 400f731f104SBill Paul * programming the multicast filter of the parent. This has the 401f731f104SBill Paul * side effect of causing the parent interface to receive multicast 402f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 403f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 404f731f104SBill Paul * to avoid this: there really is only one physical interface. 40529c2dfbeSBruce M Simpson * 40629c2dfbeSBruce M Simpson * XXX: There is a possible race here if more than one thread is 40729c2dfbeSBruce M Simpson * modifying the multicast state of the vlan interface at the same time. 408f731f104SBill Paul */ 4092b120974SPeter Wemm static int 4102b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 411f731f104SBill Paul { 412f731f104SBill Paul struct ifnet *ifp_p; 413f731f104SBill Paul struct ifmultiaddr *ifma, *rifma = NULL; 414f731f104SBill Paul struct ifvlan *sc; 415c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 416f731f104SBill Paul struct sockaddr_dl sdl; 417f731f104SBill Paul int error; 418f731f104SBill Paul 41929c2dfbeSBruce M Simpson /*VLAN_LOCK_ASSERT();*/ 42029c2dfbeSBruce M Simpson 421f731f104SBill Paul /* Find the parent. */ 422f731f104SBill Paul sc = ifp->if_softc; 42375ee267cSGleb Smirnoff ifp_p = PARENT(sc); 4241b2a4f7aSBill Fenner 42515a66c21SBruce M Simpson bzero((char *)&sdl, sizeof(sdl)); 42615a66c21SBruce M Simpson sdl.sdl_len = sizeof(sdl); 427f731f104SBill Paul sdl.sdl_family = AF_LINK; 42826e30963SBill Fenner sdl.sdl_index = ifp_p->if_index; 42926e30963SBill Fenner sdl.sdl_type = IFT_ETHER; 43024993214SYaroslav Tykhiy sdl.sdl_alen = ETHER_ADDR_LEN; 431f731f104SBill Paul 432f731f104SBill Paul /* First, remove any existing filter entries. */ 433c0cb022bSYaroslav Tykhiy while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 434f731f104SBill Paul bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 435f731f104SBill Paul error = if_delmulti(ifp_p, (struct sockaddr *)&sdl); 436f731f104SBill Paul if (error) 437f731f104SBill Paul return (error); 438f731f104SBill Paul SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 4399d4fe4b2SBrooks Davis free(mc, M_VLAN); 440f731f104SBill Paul } 441f731f104SBill Paul 442f731f104SBill Paul /* Now program new ones. */ 4436817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 444f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 445f731f104SBill Paul continue; 44629c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 44729c2dfbeSBruce M Simpson if (mc == NULL) 44829c2dfbeSBruce M Simpson return (ENOMEM); 449f731f104SBill Paul bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 450f731f104SBill Paul (char *)&mc->mc_addr, ETHER_ADDR_LEN); 451f731f104SBill Paul SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 45226e30963SBill Fenner bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 45326e30963SBill Fenner LLADDR(&sdl), ETHER_ADDR_LEN); 454f731f104SBill Paul error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 455f731f104SBill Paul if (error) 456f731f104SBill Paul return (error); 457f731f104SBill Paul } 458f731f104SBill Paul 459f731f104SBill Paul return (0); 460f731f104SBill Paul } 4612cc2df49SGarrett Wollman 462a3814acfSSam Leffler /* 4635cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 4645cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 4655cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 4665cb8c31aSYaroslav Tykhiy * by ejecting its hot-plug card. 4675cb8c31aSYaroslav Tykhiy */ 4685cb8c31aSYaroslav Tykhiy static void 4695cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 4705cb8c31aSYaroslav Tykhiy { 4715cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 4725cb8c31aSYaroslav Tykhiy int i; 4735cb8c31aSYaroslav Tykhiy 4745cb8c31aSYaroslav Tykhiy /* 4755cb8c31aSYaroslav Tykhiy * Check if it's a trunk interface first of all 4765cb8c31aSYaroslav Tykhiy * to avoid needless locking. 4775cb8c31aSYaroslav Tykhiy */ 4785cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk == NULL) 4795cb8c31aSYaroslav Tykhiy return; 4805cb8c31aSYaroslav Tykhiy 4815cb8c31aSYaroslav Tykhiy VLAN_LOCK(); 4825cb8c31aSYaroslav Tykhiy /* 4835cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 4845cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 4855cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 4865cb8c31aSYaroslav Tykhiy */ 4875cb8c31aSYaroslav Tykhiy #ifdef VLAN_ARRAY 4885cb8c31aSYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 4895cb8c31aSYaroslav Tykhiy if ((ifv = ifp->if_vlantrunk->vlans[i])) { 4905cb8c31aSYaroslav Tykhiy vlan_unconfig_locked(ifv->ifv_ifp); 4915cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk == NULL) 4925cb8c31aSYaroslav Tykhiy break; 4935cb8c31aSYaroslav Tykhiy } 4945cb8c31aSYaroslav Tykhiy #else /* VLAN_ARRAY */ 4955cb8c31aSYaroslav Tykhiy restart: 4965cb8c31aSYaroslav Tykhiy for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 4975cb8c31aSYaroslav Tykhiy if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) { 4985cb8c31aSYaroslav Tykhiy vlan_unconfig_locked(ifv->ifv_ifp); 4995cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk) 5005cb8c31aSYaroslav Tykhiy goto restart; /* trunk->hwidth can change */ 5015cb8c31aSYaroslav Tykhiy else 5025cb8c31aSYaroslav Tykhiy break; 5035cb8c31aSYaroslav Tykhiy } 5045cb8c31aSYaroslav Tykhiy #endif /* VLAN_ARRAY */ 5055cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 5065cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 5075cb8c31aSYaroslav Tykhiy VLAN_UNLOCK(); 5085cb8c31aSYaroslav Tykhiy } 5095cb8c31aSYaroslav Tykhiy 5105cb8c31aSYaroslav Tykhiy /* 511a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 512a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 513a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 514a3814acfSSam Leffler * set here. Noone else in the system should be aware of this so 515a3814acfSSam Leffler * we use an explicit reference here. 516a3814acfSSam Leffler */ 517a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 518a3814acfSSam Leffler 519984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 520127d7b2dSAndre Oppermann extern void (*vlan_link_state_p)(struct ifnet *, int); 521127d7b2dSAndre Oppermann 5222b120974SPeter Wemm static int 5232b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 5242b120974SPeter Wemm { 5259d4fe4b2SBrooks Davis 5262b120974SPeter Wemm switch (type) { 5272b120974SPeter Wemm case MOD_LOAD: 5285cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 5295cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 5305cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 5315cb8c31aSYaroslav Tykhiy return (ENOMEM); 5324faedfe8SSam Leffler VLAN_LOCK_INIT(); 5339d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 534127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 53575ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 5369d4fe4b2SBrooks Davis if_clone_attach(&vlan_cloner); 53725c0f7b3SYaroslav Tykhiy if (bootverbose) 53825c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 53925c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 54025c0f7b3SYaroslav Tykhiy "full-size arrays" 54125c0f7b3SYaroslav Tykhiy #else 54225c0f7b3SYaroslav Tykhiy "hash tables with chaining" 54325c0f7b3SYaroslav Tykhiy #endif 54425c0f7b3SYaroslav Tykhiy 54525c0f7b3SYaroslav Tykhiy "\n"); 5462b120974SPeter Wemm break; 5472b120974SPeter Wemm case MOD_UNLOAD: 5489d4fe4b2SBrooks Davis if_clone_detach(&vlan_cloner); 5495cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 5509d4fe4b2SBrooks Davis vlan_input_p = NULL; 551127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 55275ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 5534faedfe8SSam Leffler VLAN_LOCK_DESTROY(); 55425c0f7b3SYaroslav Tykhiy if (bootverbose) 55525c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 5569d4fe4b2SBrooks Davis break; 5573e019deaSPoul-Henning Kamp default: 5583e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 5592b120974SPeter Wemm } 56015a66c21SBruce M Simpson return (0); 5612b120974SPeter Wemm } 5622b120974SPeter Wemm 5632b120974SPeter Wemm static moduledata_t vlan_mod = { 5642b120974SPeter Wemm "if_vlan", 5652b120974SPeter Wemm vlan_modevent, 5662b120974SPeter Wemm 0 5672b120974SPeter Wemm }; 5682b120974SPeter Wemm 5692b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 57011edc477SEd Maste MODULE_VERSION(if_vlan, 3); 571d35bcd3bSRuslan Ermilov MODULE_DEPEND(if_vlan, miibus, 1, 1, 1); 5722cc2df49SGarrett Wollman 573f889d2efSBrooks Davis static struct ifnet * 574f889d2efSBrooks Davis vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag) 5759d4fe4b2SBrooks Davis { 576f889d2efSBrooks Davis const char *cp; 577f889d2efSBrooks Davis struct ifnet *ifp; 57815a66c21SBruce M Simpson int t = 0; 579f889d2efSBrooks Davis 580f889d2efSBrooks Davis /* Check for <etherif>.<vlan> style interface names. */ 581f889d2efSBrooks Davis IFNET_RLOCK(); 582603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 583f889d2efSBrooks Davis if (ifp->if_type != IFT_ETHER) 584f889d2efSBrooks Davis continue; 585f889d2efSBrooks Davis if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0) 586f889d2efSBrooks Davis continue; 587f889d2efSBrooks Davis cp = name + strlen(ifp->if_xname); 588f889d2efSBrooks Davis if (*cp != '.') 589f889d2efSBrooks Davis continue; 590f889d2efSBrooks Davis for(; *cp != '\0'; cp++) { 591f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 592f889d2efSBrooks Davis continue; 593f889d2efSBrooks Davis t = (t * 10) + (*cp - '0'); 594f889d2efSBrooks Davis } 595f889d2efSBrooks Davis if (tag != NULL) 596f889d2efSBrooks Davis *tag = t; 597f889d2efSBrooks Davis break; 598f889d2efSBrooks Davis } 599f889d2efSBrooks Davis IFNET_RUNLOCK(); 600f889d2efSBrooks Davis 60115a66c21SBruce M Simpson return (ifp); 602f889d2efSBrooks Davis } 603f889d2efSBrooks Davis 604f889d2efSBrooks Davis static int 605f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 606f889d2efSBrooks Davis { 607f889d2efSBrooks Davis const char *cp; 608f889d2efSBrooks Davis 609f889d2efSBrooks Davis if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL) 610f889d2efSBrooks Davis return (1); 611f889d2efSBrooks Davis 612f889d2efSBrooks Davis if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0) 613f889d2efSBrooks Davis return (0); 614f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 615f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 616f889d2efSBrooks Davis return (0); 617f889d2efSBrooks Davis } 618f889d2efSBrooks Davis 619f889d2efSBrooks Davis return (1); 620f889d2efSBrooks Davis } 621f889d2efSBrooks Davis 622f889d2efSBrooks Davis static int 6236b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 624f889d2efSBrooks Davis { 625f889d2efSBrooks Davis char *dp; 626f889d2efSBrooks Davis int wildcard; 627f889d2efSBrooks Davis int unit; 628f889d2efSBrooks Davis int error; 629f889d2efSBrooks Davis int tag; 630f889d2efSBrooks Davis int ethertag; 6319d4fe4b2SBrooks Davis struct ifvlan *ifv; 6329d4fe4b2SBrooks Davis struct ifnet *ifp; 633f889d2efSBrooks Davis struct ifnet *p; 6346b7330e2SSam Leffler struct vlanreq vlr; 63565239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 636f889d2efSBrooks Davis 6376b7330e2SSam Leffler /* 6386b7330e2SSam Leffler * There are 3 (ugh) ways to specify the cloned device: 6396b7330e2SSam Leffler * o pass a parameter block with the clone request. 6406b7330e2SSam Leffler * o specify parameters in the text of the clone device name 6416b7330e2SSam Leffler * o specify no parameters and get an unattached device that 6426b7330e2SSam Leffler * must be configured separately. 6436b7330e2SSam Leffler * The first technique is preferred; the latter two are 6446b7330e2SSam Leffler * supported for backwards compatibilty. 6456b7330e2SSam Leffler */ 6466b7330e2SSam Leffler if (params) { 6476b7330e2SSam Leffler error = copyin(params, &vlr, sizeof(vlr)); 6486b7330e2SSam Leffler if (error) 6496b7330e2SSam Leffler return error; 6506b7330e2SSam Leffler p = ifunit(vlr.vlr_parent); 6516b7330e2SSam Leffler if (p == NULL) 6526b7330e2SSam Leffler return ENXIO; 6536b7330e2SSam Leffler /* 6546b7330e2SSam Leffler * Don't let the caller set up a VLAN tag with 6556b7330e2SSam Leffler * anything except VLID bits. 6566b7330e2SSam Leffler */ 6576b7330e2SSam Leffler if (vlr.vlr_tag & ~EVL_VLID_MASK) 6586b7330e2SSam Leffler return (EINVAL); 6596b7330e2SSam Leffler error = ifc_name2unit(name, &unit); 6606b7330e2SSam Leffler if (error != 0) 6616b7330e2SSam Leffler return (error); 6626b7330e2SSam Leffler 6636b7330e2SSam Leffler ethertag = 1; 6646b7330e2SSam Leffler tag = vlr.vlr_tag; 6656b7330e2SSam Leffler wildcard = (unit < 0); 6666b7330e2SSam Leffler } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) { 667f889d2efSBrooks Davis ethertag = 1; 668f889d2efSBrooks Davis unit = -1; 669f889d2efSBrooks Davis wildcard = 0; 670f889d2efSBrooks Davis 671f889d2efSBrooks Davis /* 672f889d2efSBrooks Davis * Don't let the caller set up a VLAN tag with 673f889d2efSBrooks Davis * anything except VLID bits. 674f889d2efSBrooks Davis */ 67515a66c21SBruce M Simpson if (tag & ~EVL_VLID_MASK) 676f889d2efSBrooks Davis return (EINVAL); 677f889d2efSBrooks Davis } else { 678f889d2efSBrooks Davis ethertag = 0; 679f889d2efSBrooks Davis 680f889d2efSBrooks Davis error = ifc_name2unit(name, &unit); 681f889d2efSBrooks Davis if (error != 0) 682f889d2efSBrooks Davis return (error); 683f889d2efSBrooks Davis 684f889d2efSBrooks Davis wildcard = (unit < 0); 685f889d2efSBrooks Davis } 686f889d2efSBrooks Davis 687f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 688f889d2efSBrooks Davis if (error != 0) 689f889d2efSBrooks Davis return (error); 690f889d2efSBrooks Davis 691f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 692f889d2efSBrooks Davis if (wildcard) { 693f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 694f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 695f889d2efSBrooks Davis len - (dp-name) - 1) { 696f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 697f889d2efSBrooks Davis } 698f889d2efSBrooks Davis } 6999d4fe4b2SBrooks Davis 700a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 701fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 702fc74a9f9SBrooks Davis if (ifp == NULL) { 703fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 704fc74a9f9SBrooks Davis free(ifv, M_VLAN); 705fc74a9f9SBrooks Davis return (ENOSPC); 706fc74a9f9SBrooks Davis } 7079d4fe4b2SBrooks Davis SLIST_INIT(&ifv->vlan_mc_listhead); 7089d4fe4b2SBrooks Davis 7099d4fe4b2SBrooks Davis ifp->if_softc = ifv; 710f889d2efSBrooks Davis /* 711cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 712f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 713f889d2efSBrooks Davis */ 714f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 715f889d2efSBrooks Davis ifp->if_dname = ifc->ifc_name; 716f889d2efSBrooks Davis ifp->if_dunit = unit; 7179d4fe4b2SBrooks Davis /* NB: flags are not set here */ 7189d4fe4b2SBrooks Davis ifp->if_linkmib = &ifv->ifv_mib; 71915a66c21SBruce M Simpson ifp->if_linkmiblen = sizeof(ifv->ifv_mib); 7209d4fe4b2SBrooks Davis /* NB: mtu is not set here */ 7219d4fe4b2SBrooks Davis 722114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 7239d4fe4b2SBrooks Davis ifp->if_start = vlan_start; 7249d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 7259d4fe4b2SBrooks Davis ifp->if_snd.ifq_maxlen = ifqmaxlen; 72664a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 727fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 7289d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 729211f625aSBill Fenner ifp->if_baudrate = 0; 730a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 731a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 7329d4fe4b2SBrooks Davis 733f889d2efSBrooks Davis if (ethertag) { 73475ee267cSGleb Smirnoff error = vlan_config(ifv, p, tag); 735f889d2efSBrooks Davis if (error != 0) { 736f889d2efSBrooks Davis /* 737f889d2efSBrooks Davis * Since we've partialy failed, we need to back 738f889d2efSBrooks Davis * out all the way, otherwise userland could get 739f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 740f889d2efSBrooks Davis */ 741f889d2efSBrooks Davis ether_ifdetach(ifp); 742249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 743fc74a9f9SBrooks Davis if_free_type(ifp, IFT_ETHER); 744f889d2efSBrooks Davis free(ifv, M_VLAN); 745f889d2efSBrooks Davis 746f889d2efSBrooks Davis return (error); 747f889d2efSBrooks Davis } 748f889d2efSBrooks Davis 7491cf236fbSYaroslav Tykhiy /* Update flags on the parent, if necessary. */ 7501cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 1); 751f889d2efSBrooks Davis } 752f889d2efSBrooks Davis 7539d4fe4b2SBrooks Davis return (0); 7549d4fe4b2SBrooks Davis } 7559d4fe4b2SBrooks Davis 756f889d2efSBrooks Davis static int 757f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 7589d4fe4b2SBrooks Davis { 7599d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 760114c608cSYaroslav Tykhiy int unit = ifp->if_dunit; 761b4e9f837SBrooks Davis 762249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 763249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 764f3447eb4SBrooks Davis if_free_type(ifp, IFT_ETHER); 7659d4fe4b2SBrooks Davis free(ifv, M_VLAN); 766b4e9f837SBrooks Davis ifc_free_unit(ifc, unit); 767b4e9f837SBrooks Davis 768f889d2efSBrooks Davis return (0); 7699d4fe4b2SBrooks Davis } 7709d4fe4b2SBrooks Davis 77115a66c21SBruce M Simpson /* 77215a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 77315a66c21SBruce M Simpson */ 7742cc2df49SGarrett Wollman static void 775114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 7762cc2df49SGarrett Wollman { 7772cc2df49SGarrett Wollman } 7782cc2df49SGarrett Wollman 7796d3a3ab7SGleb Smirnoff /* 7806d3a3ab7SGleb Smirnoff * The if_start method for vlan(4) interface. It doesn't 7816d3a3ab7SGleb Smirnoff * raises the IFF_DRV_OACTIVE flag, since it is called 7826d3a3ab7SGleb Smirnoff * only from IFQ_HANDOFF() macro in ether_output_frame(). 7836d3a3ab7SGleb Smirnoff * If the interface queue is full, and vlan_start() is 7846d3a3ab7SGleb Smirnoff * not called, the queue would never get emptied and 7856d3a3ab7SGleb Smirnoff * interface would stall forever. 7866d3a3ab7SGleb Smirnoff */ 7872cc2df49SGarrett Wollman static void 7882cc2df49SGarrett Wollman vlan_start(struct ifnet *ifp) 7892cc2df49SGarrett Wollman { 7902cc2df49SGarrett Wollman struct ifvlan *ifv; 7912cc2df49SGarrett Wollman struct ifnet *p; 7925326b23cSMatthew N. Dodd struct mbuf *m; 793affc907dSMax Laier int error; 7942cc2df49SGarrett Wollman 7952cc2df49SGarrett Wollman ifv = ifp->if_softc; 79675ee267cSGleb Smirnoff p = PARENT(ifv); 7972cc2df49SGarrett Wollman 7982cc2df49SGarrett Wollman for (;;) { 7992cc2df49SGarrett Wollman IF_DEQUEUE(&ifp->if_snd, m); 8002dc879b3SYaroslav Tykhiy if (m == NULL) 8012cc2df49SGarrett Wollman break; 802a3814acfSSam Leffler BPF_MTAP(ifp, m); 8032cc2df49SGarrett Wollman 804f731f104SBill Paul /* 80524993214SYaroslav Tykhiy * Do not run parent's if_start() if the parent is not up, 80624993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 80724993214SYaroslav Tykhiy */ 8082dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 80924993214SYaroslav Tykhiy m_freem(m); 810a3814acfSSam Leffler ifp->if_collisions++; 81124993214SYaroslav Tykhiy continue; 81224993214SYaroslav Tykhiy } 81324993214SYaroslav Tykhiy 81424993214SYaroslav Tykhiy /* 815f6e5e0adSYaroslav Tykhiy * Pad the frame to the minimum size allowed if told to. 816f6e5e0adSYaroslav Tykhiy * This option is in accord with IEEE Std 802.1Q, 2003 Ed., 817f6e5e0adSYaroslav Tykhiy * paragraph C.4.4.3.b. It can help to work around buggy 818f6e5e0adSYaroslav Tykhiy * bridges that violate paragraph C.4.4.3.a from the same 819f6e5e0adSYaroslav Tykhiy * document, i.e., fail to pad short frames after untagging. 820f6e5e0adSYaroslav Tykhiy * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but 821f6e5e0adSYaroslav Tykhiy * untagging it will produce a 62-byte frame, which is a runt 822f6e5e0adSYaroslav Tykhiy * and requires padding. There are VLAN-enabled network 823f6e5e0adSYaroslav Tykhiy * devices that just discard such runts instead or mishandle 824f6e5e0adSYaroslav Tykhiy * them somehow. 825f6e5e0adSYaroslav Tykhiy */ 826f6e5e0adSYaroslav Tykhiy if (soft_pad) { 827f6e5e0adSYaroslav Tykhiy static char pad[8]; /* just zeros */ 828f6e5e0adSYaroslav Tykhiy int n; 829f6e5e0adSYaroslav Tykhiy 830f6e5e0adSYaroslav Tykhiy for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len; 831f6e5e0adSYaroslav Tykhiy n > 0; n -= sizeof(pad)) 832f6e5e0adSYaroslav Tykhiy if (!m_append(m, min(n, sizeof(pad)), pad)) 833f6e5e0adSYaroslav Tykhiy break; 834f6e5e0adSYaroslav Tykhiy 835f6e5e0adSYaroslav Tykhiy if (n > 0) { 836f6e5e0adSYaroslav Tykhiy if_printf(ifp, "cannot pad short frame\n"); 837f6e5e0adSYaroslav Tykhiy ifp->if_oerrors++; 838f6e5e0adSYaroslav Tykhiy m_freem(m); 839f6e5e0adSYaroslav Tykhiy continue; 840f6e5e0adSYaroslav Tykhiy } 841f6e5e0adSYaroslav Tykhiy } 842f6e5e0adSYaroslav Tykhiy 843f6e5e0adSYaroslav Tykhiy /* 844a3814acfSSam Leffler * If underlying interface can do VLAN tag insertion itself, 845a3814acfSSam Leffler * just pass the packet along. However, we need some way to 846a3814acfSSam Leffler * tell the interface where the packet came from so that it 847a3814acfSSam Leffler * knows how to find the VLAN tag to use, so we attach a 848a3814acfSSam Leffler * packet tag that holds it. 849f731f104SBill Paul */ 850b08347a0SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_HWTAGGING) { 85178ba57b9SAndre Oppermann m->m_pkthdr.ether_vtag = ifv->ifv_tag; 852ba26134bSGleb Smirnoff m->m_flags |= M_VLANTAG; 853f731f104SBill Paul } else { 85460e87ca8SAndrew Thompson m = ether_vlanencap(m, ifv->ifv_tag); 8554af90a4dSMatthew N. Dodd if (m == NULL) { 8566cbd3e99SYaroslav Tykhiy if_printf(ifp, 8576cbd3e99SYaroslav Tykhiy "unable to prepend VLAN header\n"); 85826f9b263SRuslan Ermilov ifp->if_oerrors++; 8592cc2df49SGarrett Wollman continue; 8604af90a4dSMatthew N. Dodd } 861f731f104SBill Paul } 8622cc2df49SGarrett Wollman 8632cc2df49SGarrett Wollman /* 8642cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 8652cc2df49SGarrett Wollman * We are already running at splimp. 8662cc2df49SGarrett Wollman */ 867affc907dSMax Laier IFQ_HANDOFF(p, m, error); 868affc907dSMax Laier if (!error) 869f731f104SBill Paul ifp->if_opackets++; 870df5e1987SJonathan Lemon else 871df5e1987SJonathan Lemon ifp->if_oerrors++; 8722cc2df49SGarrett Wollman } 873f731f104SBill Paul } 874f731f104SBill Paul 875a3814acfSSam Leffler static void 876a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 877f731f104SBill Paul { 87875ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 879f731f104SBill Paul struct ifvlan *ifv; 88075ee267cSGleb Smirnoff uint16_t tag; 88175ee267cSGleb Smirnoff 88275ee267cSGleb Smirnoff KASSERT(trunk != NULL, ("%s: no trunk", __func__)); 883a3814acfSSam Leffler 884f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 885a3814acfSSam Leffler /* 88614e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 887a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 888a3814acfSSam Leffler */ 88978ba57b9SAndre Oppermann tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag); 8906ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 891a3814acfSSam Leffler } else { 89275ee267cSGleb Smirnoff struct ether_vlan_header *evl; 89375ee267cSGleb Smirnoff 89414e98256SYaroslav Tykhiy /* 89514e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 89614e98256SYaroslav Tykhiy */ 897a3814acfSSam Leffler switch (ifp->if_type) { 898a3814acfSSam Leffler case IFT_ETHER: 899a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 900a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 901a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 902a3814acfSSam Leffler return; 903a3814acfSSam Leffler } 904a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 905fb88a3e0SBill Paul tag = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 906db8b5973SYaroslav Tykhiy 907db8b5973SYaroslav Tykhiy /* 9082dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 9092dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 9102dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 9112dc879b3SYaroslav Tykhiy * type field is already in place. 912db8b5973SYaroslav Tykhiy */ 9132dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 9142dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 9152dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 916a3814acfSSam Leffler break; 9172dc879b3SYaroslav Tykhiy 918a3814acfSSam Leffler default: 919db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 92060c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 92160c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 922a3814acfSSam Leffler #endif 92360c60618SYaroslav Tykhiy m_freem(m); 92460c60618SYaroslav Tykhiy ifp->if_noproto++; 92560c60618SYaroslav Tykhiy return; 926a3814acfSSam Leffler } 9277a46ec8fSBrooks Davis } 9287a46ec8fSBrooks Davis 92915ed2fa1SYaroslav Tykhiy TRUNK_RLOCK(trunk); 93075ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 93175ee267cSGleb Smirnoff ifv = trunk->vlans[tag]; 93275ee267cSGleb Smirnoff #else 93375ee267cSGleb Smirnoff ifv = vlan_gethash(trunk, tag); 93415ed2fa1SYaroslav Tykhiy #endif 9352dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 93675ee267cSGleb Smirnoff TRUNK_RUNLOCK(trunk); 93775ee267cSGleb Smirnoff m_freem(m); 93875ee267cSGleb Smirnoff ifp->if_noproto++; 93975ee267cSGleb Smirnoff return; 94075ee267cSGleb Smirnoff } 94175ee267cSGleb Smirnoff TRUNK_RUNLOCK(trunk); 942f731f104SBill Paul 943fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 944fc74a9f9SBrooks Davis ifv->ifv_ifp->if_ipackets++; 9452cc2df49SGarrett Wollman 946a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 947fc74a9f9SBrooks Davis (*ifp->if_input)(ifv->ifv_ifp, m); 9482cc2df49SGarrett Wollman } 9492cc2df49SGarrett Wollman 9502cc2df49SGarrett Wollman static int 95175ee267cSGleb Smirnoff vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag) 9522cc2df49SGarrett Wollman { 95375ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 9541cf236fbSYaroslav Tykhiy struct ifnet *ifp; 95575ee267cSGleb Smirnoff int error = 0; 9562cc2df49SGarrett Wollman 95775ee267cSGleb Smirnoff /* VID numbers 0x0 and 0xFFF are reserved */ 95875ee267cSGleb Smirnoff if (tag == 0 || tag == 0xFFF) 95975ee267cSGleb Smirnoff return (EINVAL); 9601cf236fbSYaroslav Tykhiy if (p->if_type != IFT_ETHER) 96115a66c21SBruce M Simpson return (EPROTONOSUPPORT); 96264a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 96364a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 96475ee267cSGleb Smirnoff if (ifv->ifv_trunk) 96515a66c21SBruce M Simpson return (EBUSY); 9662cc2df49SGarrett Wollman 96775ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 96875ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 96975ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 97075ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 97175ee267cSGleb Smirnoff vlan_inithash(trunk); 97275ee267cSGleb Smirnoff #endif 97305a2398fSGleb Smirnoff VLAN_LOCK(); 97405a2398fSGleb Smirnoff if (p->if_vlantrunk != NULL) { 97505a2398fSGleb Smirnoff /* A race that that is very unlikely to be hit. */ 97605a2398fSGleb Smirnoff #ifndef VLAN_ARRAY 97705a2398fSGleb Smirnoff vlan_freehash(trunk); 97805a2398fSGleb Smirnoff #endif 97905a2398fSGleb Smirnoff free(trunk, M_VLAN); 98005a2398fSGleb Smirnoff goto exists; 98105a2398fSGleb Smirnoff } 98275ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 98375ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 98475ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 98575ee267cSGleb Smirnoff trunk->parent = p; 98675ee267cSGleb Smirnoff } else { 98775ee267cSGleb Smirnoff VLAN_LOCK(); 98875ee267cSGleb Smirnoff exists: 98975ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 99075ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 99175ee267cSGleb Smirnoff } 99275ee267cSGleb Smirnoff 99315ed2fa1SYaroslav Tykhiy ifv->ifv_tag = tag; /* must set this before vlan_inshash() */ 99475ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 99515ed2fa1SYaroslav Tykhiy if (trunk->vlans[tag] != NULL) { 99675ee267cSGleb Smirnoff error = EEXIST; 99715ed2fa1SYaroslav Tykhiy goto done; 99815ed2fa1SYaroslav Tykhiy } 99915ed2fa1SYaroslav Tykhiy trunk->vlans[tag] = ifv; 100015ed2fa1SYaroslav Tykhiy trunk->refcnt++; 100175ee267cSGleb Smirnoff #else 100275ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 100375ee267cSGleb Smirnoff if (error) 100475ee267cSGleb Smirnoff goto done; 100515ed2fa1SYaroslav Tykhiy #endif 100673f2233dSYaroslav Tykhiy ifv->ifv_proto = ETHERTYPE_VLAN; 1007a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1008a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 10091cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1010a3814acfSSam Leffler 1011a3814acfSSam Leffler /* 1012a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1013a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1014656acce4SYaroslav Tykhiy * use it. 1015a3814acfSSam Leffler */ 1016656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1017656acce4SYaroslav Tykhiy /* 1018656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1019656acce4SYaroslav Tykhiy * handle extended frames. 1020656acce4SYaroslav Tykhiy */ 1021a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1022656acce4SYaroslav Tykhiy } else { 1023a3814acfSSam Leffler /* 1024a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1025a3814acfSSam Leffler * makes us incompatible with strictly compliant 1026a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1027a3814acfSSam Leffler * the feature with other NetBSD implementations, 1028a3814acfSSam Leffler * which might still be useful. 1029a3814acfSSam Leffler */ 1030a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1031a3814acfSSam Leffler } 1032a3814acfSSam Leffler 103375ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 10341cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 10351cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 103675ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 10372cc2df49SGarrett Wollman /* 103824993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 103924993214SYaroslav Tykhiy * Other flags are none of our business. 10402cc2df49SGarrett Wollman */ 104164a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 10421cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 10431cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 10441cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 10451cf236fbSYaroslav Tykhiy 10461cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 10472cc2df49SGarrett Wollman 104875ee267cSGleb Smirnoff vlan_capabilities(ifv); 1049a3814acfSSam Leffler 1050a3814acfSSam Leffler /* 10512cc2df49SGarrett Wollman * Set up our ``Ethernet address'' to reflect the underlying 10522cc2df49SGarrett Wollman * physical interface's. 10532cc2df49SGarrett Wollman */ 1054d09ed26fSRuslan Ermilov bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN); 10551b2a4f7aSBill Fenner 10561b2a4f7aSBill Fenner /* 10571b2a4f7aSBill Fenner * Configure multicast addresses that may already be 10581b2a4f7aSBill Fenner * joined on the vlan device. 10591b2a4f7aSBill Fenner */ 10601cf236fbSYaroslav Tykhiy (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */ 10612ada9747SYaroslav Tykhiy 10622ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 10632ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 106475ee267cSGleb Smirnoff done: 106575ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 1066c725524cSJack F Vogel if (error == 0) 1067c725524cSJack F Vogel EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag); 106875ee267cSGleb Smirnoff VLAN_UNLOCK(); 106975ee267cSGleb Smirnoff 107075ee267cSGleb Smirnoff return (error); 10712cc2df49SGarrett Wollman } 10722cc2df49SGarrett Wollman 10732cc2df49SGarrett Wollman static int 1074f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1075f731f104SBill Paul { 10765cb8c31aSYaroslav Tykhiy int ret; 10775cb8c31aSYaroslav Tykhiy 10785cb8c31aSYaroslav Tykhiy VLAN_LOCK(); 10795cb8c31aSYaroslav Tykhiy ret = vlan_unconfig_locked(ifp); 10805cb8c31aSYaroslav Tykhiy VLAN_UNLOCK(); 10815cb8c31aSYaroslav Tykhiy return (ret); 10825cb8c31aSYaroslav Tykhiy } 10835cb8c31aSYaroslav Tykhiy 10845cb8c31aSYaroslav Tykhiy static int 10855cb8c31aSYaroslav Tykhiy vlan_unconfig_locked(struct ifnet *ifp) 10865cb8c31aSYaroslav Tykhiy { 108775ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1088f731f104SBill Paul struct vlan_mc_entry *mc; 1089f731f104SBill Paul struct ifvlan *ifv; 1090c725524cSJack F Vogel struct ifnet *parent; 1091f731f104SBill Paul int error; 1092f731f104SBill Paul 10935cb8c31aSYaroslav Tykhiy VLAN_LOCK_ASSERT(); 10944faedfe8SSam Leffler 1095f731f104SBill Paul ifv = ifp->if_softc; 109675ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 109722893351SJack F Vogel parent = NULL; 1098f731f104SBill Paul 109922893351SJack F Vogel if (trunk != NULL) { 11001b2a4f7aSBill Fenner struct sockaddr_dl sdl; 110175ee267cSGleb Smirnoff 110275ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 110322893351SJack F Vogel parent = trunk->parent; 11041b2a4f7aSBill Fenner 1105f731f104SBill Paul /* 1106f731f104SBill Paul * Since the interface is being unconfigured, we need to 1107f731f104SBill Paul * empty the list of multicast groups that we may have joined 11081b2a4f7aSBill Fenner * while we were alive from the parent's list. 1109f731f104SBill Paul */ 111015a66c21SBruce M Simpson bzero((char *)&sdl, sizeof(sdl)); 111115a66c21SBruce M Simpson sdl.sdl_len = sizeof(sdl); 1112f731f104SBill Paul sdl.sdl_family = AF_LINK; 111322893351SJack F Vogel sdl.sdl_index = parent->if_index; 11141b2a4f7aSBill Fenner sdl.sdl_type = IFT_ETHER; 11151b2a4f7aSBill Fenner sdl.sdl_alen = ETHER_ADDR_LEN; 11161b2a4f7aSBill Fenner 1117c0cb022bSYaroslav Tykhiy while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 111815a66c21SBruce M Simpson bcopy((char *)&mc->mc_addr, LLADDR(&sdl), 111915a66c21SBruce M Simpson ETHER_ADDR_LEN); 112022893351SJack F Vogel error = if_delmulti(parent, (struct sockaddr *)&sdl); 1121f731f104SBill Paul if (error) 1122f731f104SBill Paul return (error); 1123f731f104SBill Paul SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 11249d4fe4b2SBrooks Davis free(mc, M_VLAN); 1125f731f104SBill Paul } 1126a3814acfSSam Leffler 11271cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 112815ed2fa1SYaroslav Tykhiy #ifdef VLAN_ARRAY 112915ed2fa1SYaroslav Tykhiy trunk->vlans[ifv->ifv_tag] = NULL; 113015ed2fa1SYaroslav Tykhiy trunk->refcnt--; 113115ed2fa1SYaroslav Tykhiy #else 113275ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 113375ee267cSGleb Smirnoff #endif 113475ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 113575ee267cSGleb Smirnoff 113675ee267cSGleb Smirnoff /* 113775ee267cSGleb Smirnoff * Check if we were the last. 113875ee267cSGleb Smirnoff */ 113975ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 114015ed2fa1SYaroslav Tykhiy trunk->parent->if_vlantrunk = NULL; 114175ee267cSGleb Smirnoff /* 114275ee267cSGleb Smirnoff * XXXGL: If some ithread has already entered 114375ee267cSGleb Smirnoff * vlan_input() and is now blocked on the trunk 114475ee267cSGleb Smirnoff * lock, then it should preempt us right after 114575ee267cSGleb Smirnoff * unlock and finish its work. Then we will acquire 114675ee267cSGleb Smirnoff * lock again in trunk_destroy(). 114775ee267cSGleb Smirnoff */ 114875ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 114975ee267cSGleb Smirnoff trunk_destroy(trunk); 115075ee267cSGleb Smirnoff } else 115175ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 11521b2a4f7aSBill Fenner } 1153f731f104SBill Paul 1154f731f104SBill Paul /* Disconnect from parent. */ 11551cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 11561cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 11575cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 11585cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 11595cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1160f731f104SBill Paul 116122893351SJack F Vogel /* 116222893351SJack F Vogel * Only dispatch an event if vlan was 116322893351SJack F Vogel * attached, otherwise there is nothing 116422893351SJack F Vogel * to cleanup anyway. 116522893351SJack F Vogel */ 116622893351SJack F Vogel if (parent != NULL) 1167c725524cSJack F Vogel EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag); 1168c725524cSJack F Vogel 116915a66c21SBruce M Simpson return (0); 1170f731f104SBill Paul } 1171f731f104SBill Paul 11721cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1173f731f104SBill Paul static int 11741cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 11751cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1176a3814acfSSam Leffler { 11771cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 11781cf236fbSYaroslav Tykhiy int error; 1179a3814acfSSam Leffler 11801cf236fbSYaroslav Tykhiy /* XXX VLAN_LOCK_ASSERT(); */ 1181a3814acfSSam Leffler 11821cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 11831cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 11841cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 11851cf236fbSYaroslav Tykhiy 11861cf236fbSYaroslav Tykhiy /* 11871cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 11881cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 11891cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 11901cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 11911cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 11921cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 11931cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 11941cf236fbSYaroslav Tykhiy */ 11951cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 119675ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 11971cf236fbSYaroslav Tykhiy if (error) 1198a3814acfSSam Leffler return (error); 11991cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 12001cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 12011cf236fbSYaroslav Tykhiy } 12021cf236fbSYaroslav Tykhiy return (0); 12031cf236fbSYaroslav Tykhiy } 12041cf236fbSYaroslav Tykhiy 12051cf236fbSYaroslav Tykhiy /* 12061cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 12071cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 12081cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 12091cf236fbSYaroslav Tykhiy */ 12101cf236fbSYaroslav Tykhiy static int 12111cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 12121cf236fbSYaroslav Tykhiy { 12131cf236fbSYaroslav Tykhiy int error, i; 12141cf236fbSYaroslav Tykhiy 12151cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 12161cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 12171cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 12181cf236fbSYaroslav Tykhiy if (error) 12191cf236fbSYaroslav Tykhiy return (error); 12201cf236fbSYaroslav Tykhiy } 12211cf236fbSYaroslav Tykhiy return (0); 1222a3814acfSSam Leffler } 1223a3814acfSSam Leffler 1224127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1225127d7b2dSAndre Oppermann static void 1226127d7b2dSAndre Oppermann vlan_link_state(struct ifnet *ifp, int link) 1227127d7b2dSAndre Oppermann { 122875ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1229127d7b2dSAndre Oppermann struct ifvlan *ifv; 123075ee267cSGleb Smirnoff int i; 1231127d7b2dSAndre Oppermann 123275ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 123375ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 123415ed2fa1SYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 123575ee267cSGleb Smirnoff if (trunk->vlans[i] != NULL) { 123675ee267cSGleb Smirnoff ifv = trunk->vlans[i]; 123775ee267cSGleb Smirnoff #else 1238aad0be7aSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 1239aad0be7aSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) { 124075ee267cSGleb Smirnoff #endif 1241aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1242fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 124375ee267cSGleb Smirnoff trunk->parent->if_link_state); 1244127d7b2dSAndre Oppermann } 124575ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 124675ee267cSGleb Smirnoff } 124775ee267cSGleb Smirnoff 124875ee267cSGleb Smirnoff static void 124975ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 125075ee267cSGleb Smirnoff { 125175ee267cSGleb Smirnoff struct ifnet *p = PARENT(ifv); 125275ee267cSGleb Smirnoff struct ifnet *ifp = ifv->ifv_ifp; 125375ee267cSGleb Smirnoff 125475ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(TRUNK(ifv)); 125575ee267cSGleb Smirnoff 125675ee267cSGleb Smirnoff /* 125775ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 125875ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 125975ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 126075ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 126175ee267cSGleb Smirnoff */ 126275ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 126375ee267cSGleb Smirnoff ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM; 126475ee267cSGleb Smirnoff 126575ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 126675ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 126775ee267cSGleb Smirnoff ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM; 126875ee267cSGleb Smirnoff ifp->if_hwassist = p->if_hwassist; 126975ee267cSGleb Smirnoff } else { 127075ee267cSGleb Smirnoff ifp->if_capenable = 0; 127175ee267cSGleb Smirnoff ifp->if_hwassist = 0; 127275ee267cSGleb Smirnoff } 127375ee267cSGleb Smirnoff } 127475ee267cSGleb Smirnoff 127575ee267cSGleb Smirnoff static void 127675ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 127775ee267cSGleb Smirnoff { 127875ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 127975ee267cSGleb Smirnoff struct ifvlan *ifv; 128075ee267cSGleb Smirnoff int i; 128175ee267cSGleb Smirnoff 128275ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 128375ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 128415ed2fa1SYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 128575ee267cSGleb Smirnoff if (trunk->vlans[i] != NULL) { 128675ee267cSGleb Smirnoff ifv = trunk->vlans[i]; 128775ee267cSGleb Smirnoff #else 128875ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 128975ee267cSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 129075ee267cSGleb Smirnoff #endif 129175ee267cSGleb Smirnoff vlan_capabilities(ifv); 129275ee267cSGleb Smirnoff } 129375ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 1294127d7b2dSAndre Oppermann } 1295127d7b2dSAndre Oppermann 1296a3814acfSSam Leffler static int 1297cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 12982cc2df49SGarrett Wollman { 12992cc2df49SGarrett Wollman struct ifaddr *ifa; 13002cc2df49SGarrett Wollman struct ifnet *p; 13012cc2df49SGarrett Wollman struct ifreq *ifr; 13022cc2df49SGarrett Wollman struct ifvlan *ifv; 13032cc2df49SGarrett Wollman struct vlanreq vlr; 13042cc2df49SGarrett Wollman int error = 0; 13052cc2df49SGarrett Wollman 13062cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 13072cc2df49SGarrett Wollman ifa = (struct ifaddr *)data; 13082cc2df49SGarrett Wollman ifv = ifp->if_softc; 13092cc2df49SGarrett Wollman 13102cc2df49SGarrett Wollman switch (cmd) { 1311b3cca108SBill Fenner case SIOCGIFMEDIA: 13124faedfe8SSam Leffler VLAN_LOCK(); 131375ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 131475ee267cSGleb Smirnoff error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv), 13154faedfe8SSam Leffler SIOCGIFMEDIA, data); 13164faedfe8SSam Leffler VLAN_UNLOCK(); 1317b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 1318b3cca108SBill Fenner if (error == 0) { 1319b3cca108SBill Fenner struct ifmediareq *ifmr; 1320b3cca108SBill Fenner 1321b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 1322b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1323b3cca108SBill Fenner ifmr->ifm_count = 1; 1324b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 1325b3cca108SBill Fenner ifmr->ifm_ulist, 1326b3cca108SBill Fenner sizeof(int)); 1327b3cca108SBill Fenner } 1328b3cca108SBill Fenner } 13294faedfe8SSam Leffler } else { 13304faedfe8SSam Leffler VLAN_UNLOCK(); 1331b3cca108SBill Fenner error = EINVAL; 13324faedfe8SSam Leffler } 1333b3cca108SBill Fenner break; 1334b3cca108SBill Fenner 1335b3cca108SBill Fenner case SIOCSIFMEDIA: 1336b3cca108SBill Fenner error = EINVAL; 1337b3cca108SBill Fenner break; 1338b3cca108SBill Fenner 13392cc2df49SGarrett Wollman case SIOCSIFMTU: 13402cc2df49SGarrett Wollman /* 13412cc2df49SGarrett Wollman * Set the interface MTU. 13422cc2df49SGarrett Wollman */ 13434faedfe8SSam Leffler VLAN_LOCK(); 134475ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 1345a3814acfSSam Leffler if (ifr->ifr_mtu > 134675ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1347a3814acfSSam Leffler ifr->ifr_mtu < 1348a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 13492cc2df49SGarrett Wollman error = EINVAL; 1350a3814acfSSam Leffler else 13512cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 1352a3814acfSSam Leffler } else 1353a3814acfSSam Leffler error = EINVAL; 13544faedfe8SSam Leffler VLAN_UNLOCK(); 13552cc2df49SGarrett Wollman break; 13562cc2df49SGarrett Wollman 13572cc2df49SGarrett Wollman case SIOCSETVLAN: 135815a66c21SBruce M Simpson error = copyin(ifr->ifr_data, &vlr, sizeof(vlr)); 13592cc2df49SGarrett Wollman if (error) 13602cc2df49SGarrett Wollman break; 13612cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 1362f731f104SBill Paul vlan_unconfig(ifp); 13632cc2df49SGarrett Wollman break; 13642cc2df49SGarrett Wollman } 13652cc2df49SGarrett Wollman p = ifunit(vlr.vlr_parent); 13662cc2df49SGarrett Wollman if (p == 0) { 13672cc2df49SGarrett Wollman error = ENOENT; 13682cc2df49SGarrett Wollman break; 13692cc2df49SGarrett Wollman } 1370fb88a3e0SBill Paul /* 1371fb88a3e0SBill Paul * Don't let the caller set up a VLAN tag with 1372fb88a3e0SBill Paul * anything except VLID bits. 1373fb88a3e0SBill Paul */ 1374fb88a3e0SBill Paul if (vlr.vlr_tag & ~EVL_VLID_MASK) { 1375fb88a3e0SBill Paul error = EINVAL; 1376fb88a3e0SBill Paul break; 1377fb88a3e0SBill Paul } 137875ee267cSGleb Smirnoff error = vlan_config(ifv, p, vlr.vlr_tag); 137975ee267cSGleb Smirnoff if (error) 13802cc2df49SGarrett Wollman break; 1381a3814acfSSam Leffler 13821cf236fbSYaroslav Tykhiy /* Update flags on the parent, if necessary. */ 13831cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 1); 13842cc2df49SGarrett Wollman break; 13852cc2df49SGarrett Wollman 13862cc2df49SGarrett Wollman case SIOCGETVLAN: 138715a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 13884faedfe8SSam Leffler VLAN_LOCK(); 138975ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 139075ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 13919bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 13922cc2df49SGarrett Wollman vlr.vlr_tag = ifv->ifv_tag; 13932cc2df49SGarrett Wollman } 13944faedfe8SSam Leffler VLAN_UNLOCK(); 139515a66c21SBruce M Simpson error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 13962cc2df49SGarrett Wollman break; 13972cc2df49SGarrett Wollman 13982cc2df49SGarrett Wollman case SIOCSIFFLAGS: 13992cc2df49SGarrett Wollman /* 14001cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 14011cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 14022cc2df49SGarrett Wollman */ 140375ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 14041cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 14052cc2df49SGarrett Wollman break; 1406a3814acfSSam Leffler 1407f731f104SBill Paul case SIOCADDMULTI: 1408f731f104SBill Paul case SIOCDELMULTI: 140975ee267cSGleb Smirnoff /* 141075ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 141175ee267cSGleb Smirnoff * when we do. 141275ee267cSGleb Smirnoff */ 141375ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 1414f731f104SBill Paul error = vlan_setmulti(ifp); 1415f731f104SBill Paul break; 141675ee267cSGleb Smirnoff 14172cc2df49SGarrett Wollman default: 14189c6dee24SYaroslav Tykhiy error = ether_ioctl(ifp, cmd, data); 14192cc2df49SGarrett Wollman } 142015a66c21SBruce M Simpson 142115a66c21SBruce M Simpson return (error); 14222cc2df49SGarrett Wollman } 1423