1c398230bSWarner Losh /*- 22cc2df49SGarrett Wollman * Copyright 1998 Massachusetts Institute of Technology 32ccbbd06SMarcelo Araujo * Copyright 2012 ADARA Networks, Inc. 42ccbbd06SMarcelo Araujo * 52ccbbd06SMarcelo Araujo * Portions of this software were developed by Robert N. M. Watson under 62ccbbd06SMarcelo Araujo * contract to ADARA Networks, Inc. 72cc2df49SGarrett Wollman * 82cc2df49SGarrett Wollman * Permission to use, copy, modify, and distribute this software and 92cc2df49SGarrett Wollman * its documentation for any purpose and without fee is hereby 102cc2df49SGarrett Wollman * granted, provided that both the above copyright notice and this 112cc2df49SGarrett Wollman * permission notice appear in all copies, that both the above 122cc2df49SGarrett Wollman * copyright notice and this permission notice appear in all 132cc2df49SGarrett Wollman * supporting documentation, and that the name of M.I.T. not be used 142cc2df49SGarrett Wollman * in advertising or publicity pertaining to distribution of the 152cc2df49SGarrett Wollman * software without specific, written prior permission. M.I.T. makes 162cc2df49SGarrett Wollman * no representations about the suitability of this software for any 172cc2df49SGarrett Wollman * purpose. It is provided "as is" without express or implied 182cc2df49SGarrett Wollman * warranty. 192cc2df49SGarrett Wollman * 202cc2df49SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 212cc2df49SGarrett Wollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 222cc2df49SGarrett Wollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 232cc2df49SGarrett Wollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 242cc2df49SGarrett Wollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 252cc2df49SGarrett Wollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 262cc2df49SGarrett Wollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 272cc2df49SGarrett Wollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 282cc2df49SGarrett Wollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 292cc2df49SGarrett Wollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 302cc2df49SGarrett Wollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 312cc2df49SGarrett Wollman * SUCH DAMAGE. 322cc2df49SGarrett Wollman */ 332cc2df49SGarrett Wollman 342cc2df49SGarrett Wollman /* 352cc2df49SGarrett Wollman * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 362ccbbd06SMarcelo Araujo * This is sort of sneaky in the implementation, since 372cc2df49SGarrett Wollman * we need to pretend to be enough of an Ethernet implementation 382cc2df49SGarrett Wollman * to make arp work. The way we do this is by telling everyone 392cc2df49SGarrett Wollman * that we are an Ethernet, and then catch the packets that 40d9b1d615SJohn Baldwin * ether_output() sends to us via if_transmit(), rewrite them for 41d9b1d615SJohn Baldwin * use by the real outgoing interface, and ask it to send them. 422cc2df49SGarrett Wollman */ 432cc2df49SGarrett Wollman 448b2d9181SPyun YongHyeon #include <sys/cdefs.h> 458b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$"); 468b2d9181SPyun YongHyeon 472c5b403eSOleg Bulyzhin #include "opt_inet.h" 4875ee267cSGleb Smirnoff #include "opt_vlan.h" 49*f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 502cc2df49SGarrett Wollman 512cc2df49SGarrett Wollman #include <sys/param.h> 52c3322cb9SGleb Smirnoff #include <sys/eventhandler.h> 532cc2df49SGarrett Wollman #include <sys/kernel.h> 5475ee267cSGleb Smirnoff #include <sys/lock.h> 55f731f104SBill Paul #include <sys/malloc.h> 562cc2df49SGarrett Wollman #include <sys/mbuf.h> 572b120974SPeter Wemm #include <sys/module.h> 58772b000fSAlexander V. Chernikov #include <sys/rmlock.h> 592ccbbd06SMarcelo Araujo #include <sys/priv.h> 60f731f104SBill Paul #include <sys/queue.h> 612cc2df49SGarrett Wollman #include <sys/socket.h> 622cc2df49SGarrett Wollman #include <sys/sockio.h> 632cc2df49SGarrett Wollman #include <sys/sysctl.h> 642cc2df49SGarrett Wollman #include <sys/systm.h> 65e4cd31ddSJeff Roberson #include <sys/sx.h> 662cc2df49SGarrett Wollman 672cc2df49SGarrett Wollman #include <net/bpf.h> 682cc2df49SGarrett Wollman #include <net/ethernet.h> 692cc2df49SGarrett Wollman #include <net/if.h> 7076039bc8SGleb Smirnoff #include <net/if_var.h> 71f889d2efSBrooks Davis #include <net/if_clone.h> 722cc2df49SGarrett Wollman #include <net/if_dl.h> 732cc2df49SGarrett Wollman #include <net/if_types.h> 742cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 754b79449eSBjoern A. Zeeb #include <net/vnet.h> 762cc2df49SGarrett Wollman 772c5b403eSOleg Bulyzhin #ifdef INET 782c5b403eSOleg Bulyzhin #include <netinet/in.h> 792c5b403eSOleg Bulyzhin #include <netinet/if_ether.h> 802c5b403eSOleg Bulyzhin #endif 812c5b403eSOleg Bulyzhin 8275ee267cSGleb Smirnoff #define VLAN_DEF_HWIDTH 4 8364a17d2eSYaroslav Tykhiy #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 8475ee267cSGleb Smirnoff 852dc879b3SYaroslav Tykhiy #define UP_AND_RUNNING(ifp) \ 862dc879b3SYaroslav Tykhiy ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 872dc879b3SYaroslav Tykhiy 8875ee267cSGleb Smirnoff LIST_HEAD(ifvlanhead, ifvlan); 8975ee267cSGleb Smirnoff 9075ee267cSGleb Smirnoff struct ifvlantrunk { 9175ee267cSGleb Smirnoff struct ifnet *parent; /* parent interface of this trunk */ 92772b000fSAlexander V. Chernikov struct rmlock lock; 9375ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 945cb8c31aSYaroslav Tykhiy #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 955cb8c31aSYaroslav Tykhiy struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 9675ee267cSGleb Smirnoff #else 9775ee267cSGleb Smirnoff struct ifvlanhead *hash; /* dynamic hash-list table */ 9875ee267cSGleb Smirnoff uint16_t hmask; 9975ee267cSGleb Smirnoff uint16_t hwidth; 10075ee267cSGleb Smirnoff #endif 10175ee267cSGleb Smirnoff int refcnt; 10275ee267cSGleb Smirnoff }; 1039d4fe4b2SBrooks Davis 104a3814acfSSam Leffler struct vlan_mc_entry { 105e4cd31ddSJeff Roberson struct sockaddr_dl mc_addr; 106a3814acfSSam Leffler SLIST_ENTRY(vlan_mc_entry) mc_entries; 107a3814acfSSam Leffler }; 108a3814acfSSam Leffler 109a3814acfSSam Leffler struct ifvlan { 11075ee267cSGleb Smirnoff struct ifvlantrunk *ifv_trunk; 111fc74a9f9SBrooks Davis struct ifnet *ifv_ifp; 11275ee267cSGleb Smirnoff #define TRUNK(ifv) ((ifv)->ifv_trunk) 11375ee267cSGleb Smirnoff #define PARENT(ifv) ((ifv)->ifv_trunk->parent) 1146667db31SAlexander V. Chernikov void *ifv_cookie; 1151cf236fbSYaroslav Tykhiy int ifv_pflags; /* special flags we have set on parent */ 116a3814acfSSam Leffler struct ifv_linkmib { 117a3814acfSSam Leffler int ifvm_encaplen; /* encapsulation length */ 118a3814acfSSam Leffler int ifvm_mtufudge; /* MTU fudged by this much */ 119a3814acfSSam Leffler int ifvm_mintu; /* min transmission unit */ 12073f2233dSYaroslav Tykhiy uint16_t ifvm_proto; /* encapsulation ethertype */ 12175ee267cSGleb Smirnoff uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 1222ccbbd06SMarcelo Araujo uint16_t ifvm_vid; /* VLAN ID */ 1232ccbbd06SMarcelo Araujo uint8_t ifvm_pcp; /* Priority Code Point (PCP). */ 124a3814acfSSam Leffler } ifv_mib; 125114c608cSYaroslav Tykhiy SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 126c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY 127a3814acfSSam Leffler LIST_ENTRY(ifvlan) ifv_list; 128c0cb022bSYaroslav Tykhiy #endif 129a3814acfSSam Leffler }; 13073f2233dSYaroslav Tykhiy #define ifv_proto ifv_mib.ifvm_proto 1312ccbbd06SMarcelo Araujo #define ifv_tag ifv_mib.ifvm_tag 1322ccbbd06SMarcelo Araujo #define ifv_vid ifv_mib.ifvm_vid 1332ccbbd06SMarcelo Araujo #define ifv_pcp ifv_mib.ifvm_pcp 134a3814acfSSam Leffler #define ifv_encaplen ifv_mib.ifvm_encaplen 135a3814acfSSam Leffler #define ifv_mtufudge ifv_mib.ifvm_mtufudge 136a3814acfSSam Leffler #define ifv_mintu ifv_mib.ifvm_mintu 137a3814acfSSam Leffler 13875ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */ 1391cf236fbSYaroslav Tykhiy static struct { 1401cf236fbSYaroslav Tykhiy int flag; 1411cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 1421cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 1431cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 1441cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 1451cf236fbSYaroslav Tykhiy {0, NULL} 1461cf236fbSYaroslav Tykhiy }; 147a3814acfSSam Leffler 1484a408dcbSBill Paul SYSCTL_DECL(_net_link); 1496472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, 1506472ac3dSEd Schouten "IEEE 802.1Q VLAN"); 1516472ac3dSEd Schouten static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, 1526472ac3dSEd Schouten "for consistency"); 1532cc2df49SGarrett Wollman 154478e0520SHiroki Sato static VNET_DEFINE(int, soft_pad); 155478e0520SHiroki Sato #define V_soft_pad VNET(soft_pad) 156478e0520SHiroki Sato SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW | CTLFLAG_VNET, 157478e0520SHiroki Sato &VNET_NAME(soft_pad), 0, "pad short frames before tagging"); 158f6e5e0adSYaroslav Tykhiy 1592ccbbd06SMarcelo Araujo /* 1602ccbbd06SMarcelo Araujo * For now, make preserving PCP via an mbuf tag optional, as it increases 1612ccbbd06SMarcelo Araujo * per-packet memory allocations and frees. In the future, it would be 1622ccbbd06SMarcelo Araujo * preferable to reuse ether_vtag for this, or similar. 1632ccbbd06SMarcelo Araujo */ 1642ccbbd06SMarcelo Araujo static int vlan_mtag_pcp = 0; 1652ccbbd06SMarcelo Araujo SYSCTL_INT(_net_link_vlan, OID_AUTO, mtag_pcp, CTLFLAG_RW, &vlan_mtag_pcp, 0, 1662ccbbd06SMarcelo Araujo "Retain VLAN PCP information as packets are passed up the stack"); 1672ccbbd06SMarcelo Araujo 16842a58907SGleb Smirnoff static const char vlanname[] = "vlan"; 16942a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface"); 1702cc2df49SGarrett Wollman 1715cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag; 172ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag; 1735cb8c31aSYaroslav Tykhiy 1744faedfe8SSam Leffler /* 17575ee267cSGleb Smirnoff * We have a global mutex, that is used to serialize configuration 17675ee267cSGleb Smirnoff * changes and isn't used in normal packet delivery. 17775ee267cSGleb Smirnoff * 17867975c79SGleb Smirnoff * We also have a per-trunk rmlock(9), that is locked shared on packet 17975ee267cSGleb Smirnoff * processing and exclusive when configuration is changed. 18075ee267cSGleb Smirnoff * 18175ee267cSGleb Smirnoff * The VLAN_ARRAY substitutes the dynamic hash with a static array 182ad387028SAndrew Thompson * with 4096 entries. In theory this can give a boost in processing, 18375ee267cSGleb Smirnoff * however on practice it does not. Probably this is because array 18475ee267cSGleb Smirnoff * is too big to fit into CPU cache. 1854faedfe8SSam Leffler */ 186e4cd31ddSJeff Roberson static struct sx ifv_lock; 187e4cd31ddSJeff Roberson #define VLAN_LOCK_INIT() sx_init(&ifv_lock, "vlan_global") 188e4cd31ddSJeff Roberson #define VLAN_LOCK_DESTROY() sx_destroy(&ifv_lock) 189e4cd31ddSJeff Roberson #define VLAN_LOCK_ASSERT() sx_assert(&ifv_lock, SA_LOCKED) 190e4cd31ddSJeff Roberson #define VLAN_LOCK() sx_xlock(&ifv_lock) 191e4cd31ddSJeff Roberson #define VLAN_UNLOCK() sx_xunlock(&ifv_lock) 192772b000fSAlexander V. Chernikov #define TRUNK_LOCK_INIT(trunk) rm_init(&(trunk)->lock, vlanname) 193772b000fSAlexander V. Chernikov #define TRUNK_LOCK_DESTROY(trunk) rm_destroy(&(trunk)->lock) 194772b000fSAlexander V. Chernikov #define TRUNK_LOCK(trunk) rm_wlock(&(trunk)->lock) 195772b000fSAlexander V. Chernikov #define TRUNK_UNLOCK(trunk) rm_wunlock(&(trunk)->lock) 196772b000fSAlexander V. Chernikov #define TRUNK_LOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_WLOCKED) 197772b000fSAlexander V. Chernikov #define TRUNK_RLOCK(trunk) rm_rlock(&(trunk)->lock, &tracker) 198772b000fSAlexander V. Chernikov #define TRUNK_RUNLOCK(trunk) rm_runlock(&(trunk)->lock, &tracker) 199772b000fSAlexander V. Chernikov #define TRUNK_LOCK_RASSERT(trunk) rm_assert(&(trunk)->lock, RA_RLOCKED) 200772b000fSAlexander V. Chernikov #define TRUNK_LOCK_READER struct rm_priotracker tracker 20175ee267cSGleb Smirnoff 20275ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 20375ee267cSGleb Smirnoff static void vlan_inithash(struct ifvlantrunk *trunk); 20475ee267cSGleb Smirnoff static void vlan_freehash(struct ifvlantrunk *trunk); 20575ee267cSGleb Smirnoff static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 20675ee267cSGleb Smirnoff static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 20775ee267cSGleb Smirnoff static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 20875ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 2097983103aSRobert Watson uint16_t vid); 21075ee267cSGleb Smirnoff #endif 21175ee267cSGleb Smirnoff static void trunk_destroy(struct ifvlantrunk *trunk); 2124faedfe8SSam Leffler 213114c608cSYaroslav Tykhiy static void vlan_init(void *foo); 214a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 215cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 216*f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 217*f3e7afe2SHans Petter Selasky static int vlan_snd_tag_alloc(struct ifnet *, 218*f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *, struct m_snd_tag **); 219*f3e7afe2SHans Petter Selasky #endif 220d9b1d615SJohn Baldwin static void vlan_qflush(struct ifnet *ifp); 2211cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 2221cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 2231cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 224f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 225d9b1d615SJohn Baldwin static int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 2266f359e28SJohn Baldwin static void vlan_unconfig(struct ifnet *ifp); 22728cc4d37SJohn Baldwin static void vlan_unconfig_locked(struct ifnet *ifp, int departing); 22875ee267cSGleb Smirnoff static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag); 229a6fffd6cSBrooks Davis static void vlan_link_state(struct ifnet *ifp); 23075ee267cSGleb Smirnoff static void vlan_capabilities(struct ifvlan *ifv); 23175ee267cSGleb Smirnoff static void vlan_trunk_capabilities(struct ifnet *ifp); 232f731f104SBill Paul 233f941c31aSGleb Smirnoff static struct ifnet *vlan_clone_match_ethervid(const char *, int *); 234f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 2356b7330e2SSam Leffler static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 236f889d2efSBrooks Davis static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 237f889d2efSBrooks Davis 2385cb8c31aSYaroslav Tykhiy static void vlan_ifdetach(void *arg, struct ifnet *ifp); 239ea4ca115SAndrew Thompson static void vlan_iflladdr(void *arg, struct ifnet *ifp); 2405cb8c31aSYaroslav Tykhiy 24142a58907SGleb Smirnoff static struct if_clone *vlan_cloner; 2429d4fe4b2SBrooks Davis 243ccf7ba97SMarko Zec #ifdef VIMAGE 24442a58907SGleb Smirnoff static VNET_DEFINE(struct if_clone *, vlan_cloner); 245ccf7ba97SMarko Zec #define V_vlan_cloner VNET(vlan_cloner) 246ccf7ba97SMarko Zec #endif 247ccf7ba97SMarko Zec 24875ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 24975ee267cSGleb Smirnoff #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 250114c608cSYaroslav Tykhiy 25175ee267cSGleb Smirnoff static void 25275ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk) 25375ee267cSGleb Smirnoff { 25475ee267cSGleb Smirnoff int i, n; 25575ee267cSGleb Smirnoff 25675ee267cSGleb Smirnoff /* 25775ee267cSGleb Smirnoff * The trunk must not be locked here since we call malloc(M_WAITOK). 25875ee267cSGleb Smirnoff * It is OK in case this function is called before the trunk struct 25975ee267cSGleb Smirnoff * gets hooked up and becomes visible from other threads. 26075ee267cSGleb Smirnoff */ 26175ee267cSGleb Smirnoff 26275ee267cSGleb Smirnoff KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 26375ee267cSGleb Smirnoff ("%s: hash already initialized", __func__)); 26475ee267cSGleb Smirnoff 26575ee267cSGleb Smirnoff trunk->hwidth = VLAN_DEF_HWIDTH; 26675ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 26775ee267cSGleb Smirnoff trunk->hmask = n - 1; 26875ee267cSGleb Smirnoff trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 26975ee267cSGleb Smirnoff for (i = 0; i < n; i++) 27075ee267cSGleb Smirnoff LIST_INIT(&trunk->hash[i]); 27175ee267cSGleb Smirnoff } 27275ee267cSGleb Smirnoff 27375ee267cSGleb Smirnoff static void 27475ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk) 27575ee267cSGleb Smirnoff { 27675ee267cSGleb Smirnoff #ifdef INVARIANTS 27775ee267cSGleb Smirnoff int i; 27875ee267cSGleb Smirnoff 27975ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 28075ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 28175ee267cSGleb Smirnoff KASSERT(LIST_EMPTY(&trunk->hash[i]), 28275ee267cSGleb Smirnoff ("%s: hash table not empty", __func__)); 28375ee267cSGleb Smirnoff #endif 28475ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 28575ee267cSGleb Smirnoff trunk->hash = NULL; 28675ee267cSGleb Smirnoff trunk->hwidth = trunk->hmask = 0; 28775ee267cSGleb Smirnoff } 28875ee267cSGleb Smirnoff 28975ee267cSGleb Smirnoff static int 29075ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 29175ee267cSGleb Smirnoff { 29275ee267cSGleb Smirnoff int i, b; 29375ee267cSGleb Smirnoff struct ifvlan *ifv2; 29475ee267cSGleb Smirnoff 29575ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 29675ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 29775ee267cSGleb Smirnoff 29875ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 2997983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 30075ee267cSGleb Smirnoff LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 3017983103aSRobert Watson if (ifv->ifv_vid == ifv2->ifv_vid) 30275ee267cSGleb Smirnoff return (EEXIST); 30375ee267cSGleb Smirnoff 30475ee267cSGleb Smirnoff /* 30575ee267cSGleb Smirnoff * Grow the hash when the number of vlans exceeds half of the number of 30675ee267cSGleb Smirnoff * hash buckets squared. This will make the average linked-list length 30775ee267cSGleb Smirnoff * buckets/2. 30875ee267cSGleb Smirnoff */ 30975ee267cSGleb Smirnoff if (trunk->refcnt > (b * b) / 2) { 31075ee267cSGleb Smirnoff vlan_growhash(trunk, 1); 3117983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 31275ee267cSGleb Smirnoff } 31375ee267cSGleb Smirnoff LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 31475ee267cSGleb Smirnoff trunk->refcnt++; 31575ee267cSGleb Smirnoff 31675ee267cSGleb Smirnoff return (0); 31775ee267cSGleb Smirnoff } 31875ee267cSGleb Smirnoff 31975ee267cSGleb Smirnoff static int 32075ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 32175ee267cSGleb Smirnoff { 32275ee267cSGleb Smirnoff int i, b; 32375ee267cSGleb Smirnoff struct ifvlan *ifv2; 32475ee267cSGleb Smirnoff 32575ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 32675ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 32775ee267cSGleb Smirnoff 32875ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 3297983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 33075ee267cSGleb Smirnoff LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 33175ee267cSGleb Smirnoff if (ifv2 == ifv) { 33275ee267cSGleb Smirnoff trunk->refcnt--; 33375ee267cSGleb Smirnoff LIST_REMOVE(ifv2, ifv_list); 33475ee267cSGleb Smirnoff if (trunk->refcnt < (b * b) / 2) 33575ee267cSGleb Smirnoff vlan_growhash(trunk, -1); 33675ee267cSGleb Smirnoff return (0); 33775ee267cSGleb Smirnoff } 33875ee267cSGleb Smirnoff 33975ee267cSGleb Smirnoff panic("%s: vlan not found\n", __func__); 34075ee267cSGleb Smirnoff return (ENOENT); /*NOTREACHED*/ 34175ee267cSGleb Smirnoff } 34275ee267cSGleb Smirnoff 34375ee267cSGleb Smirnoff /* 34475ee267cSGleb Smirnoff * Grow the hash larger or smaller if memory permits. 34575ee267cSGleb Smirnoff */ 34675ee267cSGleb Smirnoff static void 34775ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 34875ee267cSGleb Smirnoff { 34975ee267cSGleb Smirnoff struct ifvlan *ifv; 35075ee267cSGleb Smirnoff struct ifvlanhead *hash2; 35175ee267cSGleb Smirnoff int hwidth2, i, j, n, n2; 35275ee267cSGleb Smirnoff 35375ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 35475ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 35575ee267cSGleb Smirnoff 35675ee267cSGleb Smirnoff if (howmuch == 0) { 35775ee267cSGleb Smirnoff /* Harmless yet obvious coding error */ 35875ee267cSGleb Smirnoff printf("%s: howmuch is 0\n", __func__); 35975ee267cSGleb Smirnoff return; 36075ee267cSGleb Smirnoff } 36175ee267cSGleb Smirnoff 36275ee267cSGleb Smirnoff hwidth2 = trunk->hwidth + howmuch; 36375ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 36475ee267cSGleb Smirnoff n2 = 1 << hwidth2; 36575ee267cSGleb Smirnoff /* Do not shrink the table below the default */ 36675ee267cSGleb Smirnoff if (hwidth2 < VLAN_DEF_HWIDTH) 36775ee267cSGleb Smirnoff return; 36875ee267cSGleb Smirnoff 36975ee267cSGleb Smirnoff /* M_NOWAIT because we're called with trunk mutex held */ 37075ee267cSGleb Smirnoff hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT); 37175ee267cSGleb Smirnoff if (hash2 == NULL) { 37275ee267cSGleb Smirnoff printf("%s: out of memory -- hash size not changed\n", 37375ee267cSGleb Smirnoff __func__); 37475ee267cSGleb Smirnoff return; /* We can live with the old hash table */ 37575ee267cSGleb Smirnoff } 37675ee267cSGleb Smirnoff for (j = 0; j < n2; j++) 37775ee267cSGleb Smirnoff LIST_INIT(&hash2[j]); 37875ee267cSGleb Smirnoff for (i = 0; i < n; i++) 379c0cb022bSYaroslav Tykhiy while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) { 38075ee267cSGleb Smirnoff LIST_REMOVE(ifv, ifv_list); 3817983103aSRobert Watson j = HASH(ifv->ifv_vid, n2 - 1); 38275ee267cSGleb Smirnoff LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 38375ee267cSGleb Smirnoff } 38475ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 38575ee267cSGleb Smirnoff trunk->hash = hash2; 38675ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 38775ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 388f84b2d69SYaroslav Tykhiy 389f84b2d69SYaroslav Tykhiy if (bootverbose) 390f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 391f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 39275ee267cSGleb Smirnoff } 39375ee267cSGleb Smirnoff 39475ee267cSGleb Smirnoff static __inline struct ifvlan * 3957983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 39675ee267cSGleb Smirnoff { 39775ee267cSGleb Smirnoff struct ifvlan *ifv; 39875ee267cSGleb Smirnoff 39975ee267cSGleb Smirnoff TRUNK_LOCK_RASSERT(trunk); 40075ee267cSGleb Smirnoff 4017983103aSRobert Watson LIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list) 4027983103aSRobert Watson if (ifv->ifv_vid == vid) 40375ee267cSGleb Smirnoff return (ifv); 40475ee267cSGleb Smirnoff return (NULL); 40575ee267cSGleb Smirnoff } 40675ee267cSGleb Smirnoff 40775ee267cSGleb Smirnoff #if 0 40875ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 40975ee267cSGleb Smirnoff static void 41075ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 41175ee267cSGleb Smirnoff { 41275ee267cSGleb Smirnoff int i; 41375ee267cSGleb Smirnoff struct ifvlan *ifv; 41475ee267cSGleb Smirnoff 41575ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 41675ee267cSGleb Smirnoff printf("%d: ", i); 41775ee267cSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 41875ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 41975ee267cSGleb Smirnoff printf("\n"); 42075ee267cSGleb Smirnoff } 42175ee267cSGleb Smirnoff } 42275ee267cSGleb Smirnoff #endif /* 0 */ 423e4cd31ddSJeff Roberson #else 424e4cd31ddSJeff Roberson 425e4cd31ddSJeff Roberson static __inline struct ifvlan * 4267983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 427e4cd31ddSJeff Roberson { 428e4cd31ddSJeff Roberson 4297983103aSRobert Watson return trunk->vlans[vid]; 430e4cd31ddSJeff Roberson } 431e4cd31ddSJeff Roberson 432e4cd31ddSJeff Roberson static __inline int 433e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 434e4cd31ddSJeff Roberson { 435e4cd31ddSJeff Roberson 4367983103aSRobert Watson if (trunk->vlans[ifv->ifv_vid] != NULL) 437e4cd31ddSJeff Roberson return EEXIST; 4387983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = ifv; 439e4cd31ddSJeff Roberson trunk->refcnt++; 440e4cd31ddSJeff Roberson 441e4cd31ddSJeff Roberson return (0); 442e4cd31ddSJeff Roberson } 443e4cd31ddSJeff Roberson 444e4cd31ddSJeff Roberson static __inline int 445e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 446e4cd31ddSJeff Roberson { 447e4cd31ddSJeff Roberson 4487983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = NULL; 449e4cd31ddSJeff Roberson trunk->refcnt--; 450e4cd31ddSJeff Roberson 451e4cd31ddSJeff Roberson return (0); 452e4cd31ddSJeff Roberson } 453e4cd31ddSJeff Roberson 454e4cd31ddSJeff Roberson static __inline void 455e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk) 456e4cd31ddSJeff Roberson { 457e4cd31ddSJeff Roberson } 458e4cd31ddSJeff Roberson 459e4cd31ddSJeff Roberson static __inline void 460e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk) 461e4cd31ddSJeff Roberson { 462e4cd31ddSJeff Roberson } 463e4cd31ddSJeff Roberson 46475ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 46575ee267cSGleb Smirnoff 46675ee267cSGleb Smirnoff static void 46775ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 46875ee267cSGleb Smirnoff { 46975ee267cSGleb Smirnoff VLAN_LOCK_ASSERT(); 47075ee267cSGleb Smirnoff 47175ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 47275ee267cSGleb Smirnoff vlan_freehash(trunk); 47375ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 47433499e2aSYaroslav Tykhiy TRUNK_UNLOCK(trunk); 47533499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 47675ee267cSGleb Smirnoff free(trunk, M_VLAN); 47775ee267cSGleb Smirnoff } 47875ee267cSGleb Smirnoff 479f731f104SBill Paul /* 480f731f104SBill Paul * Program our multicast filter. What we're actually doing is 481f731f104SBill Paul * programming the multicast filter of the parent. This has the 482f731f104SBill Paul * side effect of causing the parent interface to receive multicast 483f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 484f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 485f731f104SBill Paul * to avoid this: there really is only one physical interface. 486f731f104SBill Paul */ 4872b120974SPeter Wemm static int 4882b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 489f731f104SBill Paul { 490f731f104SBill Paul struct ifnet *ifp_p; 4912d222cb7SAlexander Motin struct ifmultiaddr *ifma; 492f731f104SBill Paul struct ifvlan *sc; 493c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 494f731f104SBill Paul int error; 495f731f104SBill Paul 496f731f104SBill Paul /* Find the parent. */ 497f731f104SBill Paul sc = ifp->if_softc; 4982d222cb7SAlexander Motin TRUNK_LOCK_ASSERT(TRUNK(sc)); 49975ee267cSGleb Smirnoff ifp_p = PARENT(sc); 5001b2a4f7aSBill Fenner 5018b615593SMarko Zec CURVNET_SET_QUIET(ifp_p->if_vnet); 5028b615593SMarko Zec 503f731f104SBill Paul /* First, remove any existing filter entries. */ 504c0cb022bSYaroslav Tykhiy while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 505f731f104SBill Paul SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 5062d222cb7SAlexander Motin (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 5079d4fe4b2SBrooks Davis free(mc, M_VLAN); 508f731f104SBill Paul } 509f731f104SBill Paul 510f731f104SBill Paul /* Now program new ones. */ 5112d222cb7SAlexander Motin IF_ADDR_WLOCK(ifp); 5126817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 513f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 514f731f104SBill Paul continue; 51529c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 5162d222cb7SAlexander Motin if (mc == NULL) { 5172d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 51829c2dfbeSBruce M Simpson return (ENOMEM); 5192d222cb7SAlexander Motin } 520e4cd31ddSJeff Roberson bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 521e4cd31ddSJeff Roberson mc->mc_addr.sdl_index = ifp_p->if_index; 522f731f104SBill Paul SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 5232d222cb7SAlexander Motin } 5242d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 5252d222cb7SAlexander Motin SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) { 526e4cd31ddSJeff Roberson error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 5272d222cb7SAlexander Motin NULL); 528f731f104SBill Paul if (error) 529f731f104SBill Paul return (error); 530f731f104SBill Paul } 531f731f104SBill Paul 5328b615593SMarko Zec CURVNET_RESTORE(); 533f731f104SBill Paul return (0); 534f731f104SBill Paul } 5352cc2df49SGarrett Wollman 536a3814acfSSam Leffler /* 537ea4ca115SAndrew Thompson * A handler for parent interface link layer address changes. 538ea4ca115SAndrew Thompson * If the parent interface link layer address is changed we 539ea4ca115SAndrew Thompson * should also change it on all children vlans. 540ea4ca115SAndrew Thompson */ 541ea4ca115SAndrew Thompson static void 542ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 543ea4ca115SAndrew Thompson { 544ea4ca115SAndrew Thompson struct ifvlan *ifv; 5456117727bSAndrew Thompson #ifndef VLAN_ARRAY 5466117727bSAndrew Thompson struct ifvlan *next; 5476117727bSAndrew Thompson #endif 5488ad43f2dSAlexander V. Chernikov int i; 549ea4ca115SAndrew Thompson 550ea4ca115SAndrew Thompson /* 551ea4ca115SAndrew Thompson * Check if it's a trunk interface first of all 552ea4ca115SAndrew Thompson * to avoid needless locking. 553ea4ca115SAndrew Thompson */ 554ea4ca115SAndrew Thompson if (ifp->if_vlantrunk == NULL) 555ea4ca115SAndrew Thompson return; 556ea4ca115SAndrew Thompson 557ea4ca115SAndrew Thompson VLAN_LOCK(); 558ea4ca115SAndrew Thompson /* 559ea4ca115SAndrew Thompson * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 560ea4ca115SAndrew Thompson */ 561ea4ca115SAndrew Thompson #ifdef VLAN_ARRAY 562ea4ca115SAndrew Thompson for (i = 0; i < VLAN_ARRAY_SIZE; i++) 5636117727bSAndrew Thompson if ((ifv = ifp->if_vlantrunk->vlans[i])) { 564ea4ca115SAndrew Thompson #else /* VLAN_ARRAY */ 565ea4ca115SAndrew Thompson for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 5666117727bSAndrew Thompson LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) { 567ea4ca115SAndrew Thompson #endif /* VLAN_ARRAY */ 568ea4ca115SAndrew Thompson VLAN_UNLOCK(); 5698ad43f2dSAlexander V. Chernikov if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp), 570e4cd31ddSJeff Roberson ifp->if_addrlen); 5716117727bSAndrew Thompson VLAN_LOCK(); 5726117727bSAndrew Thompson } 5736117727bSAndrew Thompson VLAN_UNLOCK(); 574ea4ca115SAndrew Thompson 575ea4ca115SAndrew Thompson } 576ea4ca115SAndrew Thompson 577ea4ca115SAndrew Thompson /* 5785cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 5795cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 5805cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 5815428776eSJohn Baldwin * by ejecting its hot-plug card. However, if an ifnet is simply 5825428776eSJohn Baldwin * being renamed, then there's no need to tear down the state. 5835cb8c31aSYaroslav Tykhiy */ 5845cb8c31aSYaroslav Tykhiy static void 5855cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 5865cb8c31aSYaroslav Tykhiy { 5875cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 5885cb8c31aSYaroslav Tykhiy int i; 5895cb8c31aSYaroslav Tykhiy 5905cb8c31aSYaroslav Tykhiy /* 5915cb8c31aSYaroslav Tykhiy * Check if it's a trunk interface first of all 5925cb8c31aSYaroslav Tykhiy * to avoid needless locking. 5935cb8c31aSYaroslav Tykhiy */ 5945cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk == NULL) 5955cb8c31aSYaroslav Tykhiy return; 5965cb8c31aSYaroslav Tykhiy 5975428776eSJohn Baldwin /* If the ifnet is just being renamed, don't do anything. */ 5985428776eSJohn Baldwin if (ifp->if_flags & IFF_RENAMING) 5995428776eSJohn Baldwin return; 6005428776eSJohn Baldwin 6015cb8c31aSYaroslav Tykhiy VLAN_LOCK(); 6025cb8c31aSYaroslav Tykhiy /* 6035cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 6045cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 6055cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 6065cb8c31aSYaroslav Tykhiy */ 6075cb8c31aSYaroslav Tykhiy #ifdef VLAN_ARRAY 6085cb8c31aSYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 6095cb8c31aSYaroslav Tykhiy if ((ifv = ifp->if_vlantrunk->vlans[i])) { 61028cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 6115cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk == NULL) 6125cb8c31aSYaroslav Tykhiy break; 6135cb8c31aSYaroslav Tykhiy } 6145cb8c31aSYaroslav Tykhiy #else /* VLAN_ARRAY */ 6155cb8c31aSYaroslav Tykhiy restart: 6165cb8c31aSYaroslav Tykhiy for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 6175cb8c31aSYaroslav Tykhiy if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) { 61828cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 6195cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk) 6205cb8c31aSYaroslav Tykhiy goto restart; /* trunk->hwidth can change */ 6215cb8c31aSYaroslav Tykhiy else 6225cb8c31aSYaroslav Tykhiy break; 6235cb8c31aSYaroslav Tykhiy } 6245cb8c31aSYaroslav Tykhiy #endif /* VLAN_ARRAY */ 6255cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 6265cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 6275cb8c31aSYaroslav Tykhiy VLAN_UNLOCK(); 6285cb8c31aSYaroslav Tykhiy } 6295cb8c31aSYaroslav Tykhiy 6305cb8c31aSYaroslav Tykhiy /* 631e4cd31ddSJeff Roberson * Return the trunk device for a virtual interface. 632e4cd31ddSJeff Roberson */ 633e4cd31ddSJeff Roberson static struct ifnet * 634e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp) 635e4cd31ddSJeff Roberson { 636e4cd31ddSJeff Roberson struct ifvlan *ifv; 637e4cd31ddSJeff Roberson 638e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 639e4cd31ddSJeff Roberson return (NULL); 640e4cd31ddSJeff Roberson ifv = ifp->if_softc; 641e4cd31ddSJeff Roberson ifp = NULL; 642e4cd31ddSJeff Roberson VLAN_LOCK(); 643e4cd31ddSJeff Roberson if (ifv->ifv_trunk) 644e4cd31ddSJeff Roberson ifp = PARENT(ifv); 645e4cd31ddSJeff Roberson VLAN_UNLOCK(); 646e4cd31ddSJeff Roberson return (ifp); 647e4cd31ddSJeff Roberson } 648e4cd31ddSJeff Roberson 649e4cd31ddSJeff Roberson /* 6507983103aSRobert Watson * Return the 12-bit VLAN VID for this interface, for use by external 6517983103aSRobert Watson * components such as Infiniband. 6527983103aSRobert Watson * 6537983103aSRobert Watson * XXXRW: Note that the function name here is historical; it should be named 6547983103aSRobert Watson * vlan_vid(). 655e4cd31ddSJeff Roberson */ 656e4cd31ddSJeff Roberson static int 6577983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp) 658e4cd31ddSJeff Roberson { 659e4cd31ddSJeff Roberson struct ifvlan *ifv; 660e4cd31ddSJeff Roberson 661e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 662e4cd31ddSJeff Roberson return (EINVAL); 663e4cd31ddSJeff Roberson ifv = ifp->if_softc; 6647983103aSRobert Watson *vidp = ifv->ifv_vid; 665e4cd31ddSJeff Roberson return (0); 666e4cd31ddSJeff Roberson } 667e4cd31ddSJeff Roberson 668e4cd31ddSJeff Roberson /* 669e4cd31ddSJeff Roberson * Return a driver specific cookie for this interface. Synchronization 670e4cd31ddSJeff Roberson * with setcookie must be provided by the driver. 671e4cd31ddSJeff Roberson */ 672e4cd31ddSJeff Roberson static void * 673e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp) 674e4cd31ddSJeff Roberson { 675e4cd31ddSJeff Roberson struct ifvlan *ifv; 676e4cd31ddSJeff Roberson 677e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 678e4cd31ddSJeff Roberson return (NULL); 679e4cd31ddSJeff Roberson ifv = ifp->if_softc; 680e4cd31ddSJeff Roberson return (ifv->ifv_cookie); 681e4cd31ddSJeff Roberson } 682e4cd31ddSJeff Roberson 683e4cd31ddSJeff Roberson /* 684e4cd31ddSJeff Roberson * Store a cookie in our softc that drivers can use to store driver 685e4cd31ddSJeff Roberson * private per-instance data in. 686e4cd31ddSJeff Roberson */ 687e4cd31ddSJeff Roberson static int 688e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie) 689e4cd31ddSJeff Roberson { 690e4cd31ddSJeff Roberson struct ifvlan *ifv; 691e4cd31ddSJeff Roberson 692e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 693e4cd31ddSJeff Roberson return (EINVAL); 694e4cd31ddSJeff Roberson ifv = ifp->if_softc; 695e4cd31ddSJeff Roberson ifv->ifv_cookie = cookie; 696e4cd31ddSJeff Roberson return (0); 697e4cd31ddSJeff Roberson } 698e4cd31ddSJeff Roberson 699e4cd31ddSJeff Roberson /* 7007983103aSRobert Watson * Return the vlan device present at the specific VID. 701e4cd31ddSJeff Roberson */ 702e4cd31ddSJeff Roberson static struct ifnet * 7037983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid) 704e4cd31ddSJeff Roberson { 705e4cd31ddSJeff Roberson struct ifvlantrunk *trunk; 706e4cd31ddSJeff Roberson struct ifvlan *ifv; 707772b000fSAlexander V. Chernikov TRUNK_LOCK_READER; 708e4cd31ddSJeff Roberson 709e4cd31ddSJeff Roberson trunk = ifp->if_vlantrunk; 710e4cd31ddSJeff Roberson if (trunk == NULL) 711e4cd31ddSJeff Roberson return (NULL); 712e4cd31ddSJeff Roberson ifp = NULL; 713e4cd31ddSJeff Roberson TRUNK_RLOCK(trunk); 7147983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 715e4cd31ddSJeff Roberson if (ifv) 716e4cd31ddSJeff Roberson ifp = ifv->ifv_ifp; 717e4cd31ddSJeff Roberson TRUNK_RUNLOCK(trunk); 718e4cd31ddSJeff Roberson return (ifp); 719e4cd31ddSJeff Roberson } 720e4cd31ddSJeff Roberson 721e4cd31ddSJeff Roberson /* 7222ccbbd06SMarcelo Araujo * Recalculate the cached VLAN tag exposed via the MIB. 7232ccbbd06SMarcelo Araujo */ 7242ccbbd06SMarcelo Araujo static void 7252ccbbd06SMarcelo Araujo vlan_tag_recalculate(struct ifvlan *ifv) 7262ccbbd06SMarcelo Araujo { 7272ccbbd06SMarcelo Araujo 7282ccbbd06SMarcelo Araujo ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0); 7292ccbbd06SMarcelo Araujo } 7302ccbbd06SMarcelo Araujo 7312ccbbd06SMarcelo Araujo /* 732a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 733a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 734a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 735a3814acfSSam Leffler * set here. No one else in the system should be aware of this so 736a3814acfSSam Leffler * we use an explicit reference here. 737a3814acfSSam Leffler */ 738a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 739a3814acfSSam Leffler 740984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 741a6fffd6cSBrooks Davis extern void (*vlan_link_state_p)(struct ifnet *); 742127d7b2dSAndre Oppermann 7432b120974SPeter Wemm static int 7442b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 7452b120974SPeter Wemm { 7469d4fe4b2SBrooks Davis 7472b120974SPeter Wemm switch (type) { 7482b120974SPeter Wemm case MOD_LOAD: 7495cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 7505cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 7515cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 7525cb8c31aSYaroslav Tykhiy return (ENOMEM); 753ea4ca115SAndrew Thompson iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 754ea4ca115SAndrew Thompson vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 755ea4ca115SAndrew Thompson if (iflladdr_tag == NULL) 756ea4ca115SAndrew Thompson return (ENOMEM); 7574faedfe8SSam Leffler VLAN_LOCK_INIT(); 7589d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 759127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 76075ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 761e4cd31ddSJeff Roberson vlan_trunkdev_p = vlan_trunkdev; 762e4cd31ddSJeff Roberson vlan_cookie_p = vlan_cookie; 763e4cd31ddSJeff Roberson vlan_setcookie_p = vlan_setcookie; 764e4cd31ddSJeff Roberson vlan_tag_p = vlan_tag; 765e4cd31ddSJeff Roberson vlan_devat_p = vlan_devat; 766ccf7ba97SMarko Zec #ifndef VIMAGE 76742a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 76842a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 769ccf7ba97SMarko Zec #endif 77025c0f7b3SYaroslav Tykhiy if (bootverbose) 77125c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 77225c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 77325c0f7b3SYaroslav Tykhiy "full-size arrays" 77425c0f7b3SYaroslav Tykhiy #else 77525c0f7b3SYaroslav Tykhiy "hash tables with chaining" 77625c0f7b3SYaroslav Tykhiy #endif 77725c0f7b3SYaroslav Tykhiy 77825c0f7b3SYaroslav Tykhiy "\n"); 7792b120974SPeter Wemm break; 7802b120974SPeter Wemm case MOD_UNLOAD: 781ccf7ba97SMarko Zec #ifndef VIMAGE 78242a58907SGleb Smirnoff if_clone_detach(vlan_cloner); 783ccf7ba97SMarko Zec #endif 7845cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 785ea4ca115SAndrew Thompson EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 7869d4fe4b2SBrooks Davis vlan_input_p = NULL; 787127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 78875ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 789e4cd31ddSJeff Roberson vlan_trunkdev_p = NULL; 790e4cd31ddSJeff Roberson vlan_tag_p = NULL; 79109fe6320SNavdeep Parhar vlan_cookie_p = NULL; 79209fe6320SNavdeep Parhar vlan_setcookie_p = NULL; 793e4cd31ddSJeff Roberson vlan_devat_p = NULL; 7944faedfe8SSam Leffler VLAN_LOCK_DESTROY(); 79525c0f7b3SYaroslav Tykhiy if (bootverbose) 79625c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 7979d4fe4b2SBrooks Davis break; 7983e019deaSPoul-Henning Kamp default: 7993e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 8002b120974SPeter Wemm } 80115a66c21SBruce M Simpson return (0); 8022b120974SPeter Wemm } 8032b120974SPeter Wemm 8042b120974SPeter Wemm static moduledata_t vlan_mod = { 8052b120974SPeter Wemm "if_vlan", 8062b120974SPeter Wemm vlan_modevent, 8079823d527SKevin Lo 0 8082b120974SPeter Wemm }; 8092b120974SPeter Wemm 8102b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 81111edc477SEd Maste MODULE_VERSION(if_vlan, 3); 8122cc2df49SGarrett Wollman 813ccf7ba97SMarko Zec #ifdef VIMAGE 814ccf7ba97SMarko Zec static void 815ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused) 816ccf7ba97SMarko Zec { 817ccf7ba97SMarko Zec 81842a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 81942a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 820ccf7ba97SMarko Zec V_vlan_cloner = vlan_cloner; 821ccf7ba97SMarko Zec } 822ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 823ccf7ba97SMarko Zec vnet_vlan_init, NULL); 824ccf7ba97SMarko Zec 825ccf7ba97SMarko Zec static void 826ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused) 827ccf7ba97SMarko Zec { 828ccf7ba97SMarko Zec 82942a58907SGleb Smirnoff if_clone_detach(V_vlan_cloner); 830ccf7ba97SMarko Zec } 83189856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, 832ccf7ba97SMarko Zec vnet_vlan_uninit, NULL); 833ccf7ba97SMarko Zec #endif 834ccf7ba97SMarko Zec 835f941c31aSGleb Smirnoff /* 836f941c31aSGleb Smirnoff * Check for <etherif>.<vlan> style interface names. 837f941c31aSGleb Smirnoff */ 838f889d2efSBrooks Davis static struct ifnet * 839f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp) 8409d4fe4b2SBrooks Davis { 841f941c31aSGleb Smirnoff char ifname[IFNAMSIZ]; 842f941c31aSGleb Smirnoff char *cp; 843f889d2efSBrooks Davis struct ifnet *ifp; 8447983103aSRobert Watson int vid; 845f889d2efSBrooks Davis 846f941c31aSGleb Smirnoff strlcpy(ifname, name, IFNAMSIZ); 847f941c31aSGleb Smirnoff if ((cp = strchr(ifname, '.')) == NULL) 848f941c31aSGleb Smirnoff return (NULL); 849f941c31aSGleb Smirnoff *cp = '\0'; 850f941c31aSGleb Smirnoff if ((ifp = ifunit(ifname)) == NULL) 851f941c31aSGleb Smirnoff return (NULL); 852f941c31aSGleb Smirnoff /* Parse VID. */ 853f941c31aSGleb Smirnoff if (*++cp == '\0') 854f941c31aSGleb Smirnoff return (NULL); 8557983103aSRobert Watson vid = 0; 856fb92ad4aSJohn Baldwin for(; *cp >= '0' && *cp <= '9'; cp++) 8577983103aSRobert Watson vid = (vid * 10) + (*cp - '0'); 858fb92ad4aSJohn Baldwin if (*cp != '\0') 859f941c31aSGleb Smirnoff return (NULL); 8607983103aSRobert Watson if (vidp != NULL) 8617983103aSRobert Watson *vidp = vid; 862f889d2efSBrooks Davis 86315a66c21SBruce M Simpson return (ifp); 864f889d2efSBrooks Davis } 865f889d2efSBrooks Davis 866f889d2efSBrooks Davis static int 867f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 868f889d2efSBrooks Davis { 869f889d2efSBrooks Davis const char *cp; 870f889d2efSBrooks Davis 871f941c31aSGleb Smirnoff if (vlan_clone_match_ethervid(name, NULL) != NULL) 872f889d2efSBrooks Davis return (1); 873f889d2efSBrooks Davis 87442a58907SGleb Smirnoff if (strncmp(vlanname, name, strlen(vlanname)) != 0) 875f889d2efSBrooks Davis return (0); 876f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 877f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 878f889d2efSBrooks Davis return (0); 879f889d2efSBrooks Davis } 880f889d2efSBrooks Davis 881f889d2efSBrooks Davis return (1); 882f889d2efSBrooks Davis } 883f889d2efSBrooks Davis 884f889d2efSBrooks Davis static int 8856b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 886f889d2efSBrooks Davis { 887f889d2efSBrooks Davis char *dp; 888f889d2efSBrooks Davis int wildcard; 889f889d2efSBrooks Davis int unit; 890f889d2efSBrooks Davis int error; 8917983103aSRobert Watson int vid; 892f889d2efSBrooks Davis int ethertag; 8939d4fe4b2SBrooks Davis struct ifvlan *ifv; 8949d4fe4b2SBrooks Davis struct ifnet *ifp; 895f889d2efSBrooks Davis struct ifnet *p; 8963ba24fdeSJohn Baldwin struct ifaddr *ifa; 8973ba24fdeSJohn Baldwin struct sockaddr_dl *sdl; 8986b7330e2SSam Leffler struct vlanreq vlr; 89965239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 900f889d2efSBrooks Davis 9016b7330e2SSam Leffler /* 9026b7330e2SSam Leffler * There are 3 (ugh) ways to specify the cloned device: 9036b7330e2SSam Leffler * o pass a parameter block with the clone request. 9046b7330e2SSam Leffler * o specify parameters in the text of the clone device name 9056b7330e2SSam Leffler * o specify no parameters and get an unattached device that 9066b7330e2SSam Leffler * must be configured separately. 9076b7330e2SSam Leffler * The first technique is preferred; the latter two are 908a4641f4eSPedro F. Giffuni * supported for backwards compatibility. 9097983103aSRobert Watson * 9107983103aSRobert Watson * XXXRW: Note historic use of the word "tag" here. New ioctls may be 9117983103aSRobert Watson * called for. 9126b7330e2SSam Leffler */ 9136b7330e2SSam Leffler if (params) { 9146b7330e2SSam Leffler error = copyin(params, &vlr, sizeof(vlr)); 9156b7330e2SSam Leffler if (error) 9166b7330e2SSam Leffler return error; 9176b7330e2SSam Leffler p = ifunit(vlr.vlr_parent); 9186b7330e2SSam Leffler if (p == NULL) 919b1828acfSGleb Smirnoff return (ENXIO); 9206b7330e2SSam Leffler error = ifc_name2unit(name, &unit); 9216b7330e2SSam Leffler if (error != 0) 9226b7330e2SSam Leffler return (error); 9236b7330e2SSam Leffler 9246b7330e2SSam Leffler ethertag = 1; 9257983103aSRobert Watson vid = vlr.vlr_tag; 9266b7330e2SSam Leffler wildcard = (unit < 0); 927f941c31aSGleb Smirnoff } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) { 928f889d2efSBrooks Davis ethertag = 1; 929f889d2efSBrooks Davis unit = -1; 930f889d2efSBrooks Davis wildcard = 0; 931f889d2efSBrooks Davis } else { 932f889d2efSBrooks Davis ethertag = 0; 933f889d2efSBrooks Davis 934f889d2efSBrooks Davis error = ifc_name2unit(name, &unit); 935f889d2efSBrooks Davis if (error != 0) 936f889d2efSBrooks Davis return (error); 937f889d2efSBrooks Davis 938f889d2efSBrooks Davis wildcard = (unit < 0); 939f889d2efSBrooks Davis } 940f889d2efSBrooks Davis 941f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 942f889d2efSBrooks Davis if (error != 0) 943f889d2efSBrooks Davis return (error); 944f889d2efSBrooks Davis 945f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 946f889d2efSBrooks Davis if (wildcard) { 947f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 948f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 949f889d2efSBrooks Davis len - (dp-name) - 1) { 950f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 951f889d2efSBrooks Davis } 952f889d2efSBrooks Davis } 9539d4fe4b2SBrooks Davis 954a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 955fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 956fc74a9f9SBrooks Davis if (ifp == NULL) { 957fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 958fc74a9f9SBrooks Davis free(ifv, M_VLAN); 959fc74a9f9SBrooks Davis return (ENOSPC); 960fc74a9f9SBrooks Davis } 9619d4fe4b2SBrooks Davis SLIST_INIT(&ifv->vlan_mc_listhead); 9629d4fe4b2SBrooks Davis ifp->if_softc = ifv; 963f889d2efSBrooks Davis /* 964cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 965f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 966f889d2efSBrooks Davis */ 967f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 96842a58907SGleb Smirnoff ifp->if_dname = vlanname; 969f889d2efSBrooks Davis ifp->if_dunit = unit; 9709d4fe4b2SBrooks Davis /* NB: flags are not set here */ 9719d4fe4b2SBrooks Davis ifp->if_linkmib = &ifv->ifv_mib; 97215a66c21SBruce M Simpson ifp->if_linkmiblen = sizeof(ifv->ifv_mib); 9739d4fe4b2SBrooks Davis /* NB: mtu is not set here */ 9749d4fe4b2SBrooks Davis 975114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 976d9b1d615SJohn Baldwin ifp->if_transmit = vlan_transmit; 977d9b1d615SJohn Baldwin ifp->if_qflush = vlan_qflush; 9789d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 979*f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 980*f3e7afe2SHans Petter Selasky ifp->if_snd_tag_alloc = vlan_snd_tag_alloc; 981*f3e7afe2SHans Petter Selasky #endif 98264a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 983fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 9849d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 985211f625aSBill Fenner ifp->if_baudrate = 0; 986a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 987a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 9883ba24fdeSJohn Baldwin ifa = ifp->if_addr; 9893ba24fdeSJohn Baldwin sdl = (struct sockaddr_dl *)ifa->ifa_addr; 9903ba24fdeSJohn Baldwin sdl->sdl_type = IFT_L2VLAN; 9919d4fe4b2SBrooks Davis 992f889d2efSBrooks Davis if (ethertag) { 9937983103aSRobert Watson error = vlan_config(ifv, p, vid); 994f889d2efSBrooks Davis if (error != 0) { 995f889d2efSBrooks Davis /* 99628cc4d37SJohn Baldwin * Since we've partially failed, we need to back 997f889d2efSBrooks Davis * out all the way, otherwise userland could get 998f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 999f889d2efSBrooks Davis */ 1000f889d2efSBrooks Davis ether_ifdetach(ifp); 1001249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 10024b22573aSBrooks Davis if_free(ifp); 100396c8ef3aSMaxim Konovalov ifc_free_unit(ifc, unit); 1004f889d2efSBrooks Davis free(ifv, M_VLAN); 1005f889d2efSBrooks Davis 1006f889d2efSBrooks Davis return (error); 1007f889d2efSBrooks Davis } 1008f889d2efSBrooks Davis 10091cf236fbSYaroslav Tykhiy /* Update flags on the parent, if necessary. */ 10101cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 1); 1011f889d2efSBrooks Davis } 1012f889d2efSBrooks Davis 10139d4fe4b2SBrooks Davis return (0); 10149d4fe4b2SBrooks Davis } 10159d4fe4b2SBrooks Davis 1016f889d2efSBrooks Davis static int 1017f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 10189d4fe4b2SBrooks Davis { 10199d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 1020114c608cSYaroslav Tykhiy int unit = ifp->if_dunit; 1021b4e9f837SBrooks Davis 1022249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 1023249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 10244b22573aSBrooks Davis if_free(ifp); 10259d4fe4b2SBrooks Davis free(ifv, M_VLAN); 1026b4e9f837SBrooks Davis ifc_free_unit(ifc, unit); 1027b4e9f837SBrooks Davis 1028f889d2efSBrooks Davis return (0); 10299d4fe4b2SBrooks Davis } 10309d4fe4b2SBrooks Davis 103115a66c21SBruce M Simpson /* 103215a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 103315a66c21SBruce M Simpson */ 10342cc2df49SGarrett Wollman static void 1035114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 10362cc2df49SGarrett Wollman { 10372cc2df49SGarrett Wollman } 10382cc2df49SGarrett Wollman 10396d3a3ab7SGleb Smirnoff /* 1040d9b1d615SJohn Baldwin * The if_transmit method for vlan(4) interface. 10416d3a3ab7SGleb Smirnoff */ 1042d9b1d615SJohn Baldwin static int 1043d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m) 10442cc2df49SGarrett Wollman { 10452cc2df49SGarrett Wollman struct ifvlan *ifv; 10462cc2df49SGarrett Wollman struct ifnet *p; 10472ccbbd06SMarcelo Araujo struct m_tag *mtag; 10482ccbbd06SMarcelo Araujo uint16_t tag; 10491ad7a257SPyun YongHyeon int error, len, mcast; 10502cc2df49SGarrett Wollman 10512cc2df49SGarrett Wollman ifv = ifp->if_softc; 105275ee267cSGleb Smirnoff p = PARENT(ifv); 10531ad7a257SPyun YongHyeon len = m->m_pkthdr.len; 10541ad7a257SPyun YongHyeon mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 10552cc2df49SGarrett Wollman 1056a3814acfSSam Leffler BPF_MTAP(ifp, m); 10572cc2df49SGarrett Wollman 1058f731f104SBill Paul /* 1059d9b1d615SJohn Baldwin * Do not run parent's if_transmit() if the parent is not up, 106024993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 106124993214SYaroslav Tykhiy */ 10622dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 106324993214SYaroslav Tykhiy m_freem(m); 1064a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 10658b20f6cfSHiroki Sato return (ENETDOWN); 106624993214SYaroslav Tykhiy } 106724993214SYaroslav Tykhiy 106824993214SYaroslav Tykhiy /* 1069f6e5e0adSYaroslav Tykhiy * Pad the frame to the minimum size allowed if told to. 1070f6e5e0adSYaroslav Tykhiy * This option is in accord with IEEE Std 802.1Q, 2003 Ed., 1071f6e5e0adSYaroslav Tykhiy * paragraph C.4.4.3.b. It can help to work around buggy 1072f6e5e0adSYaroslav Tykhiy * bridges that violate paragraph C.4.4.3.a from the same 1073f6e5e0adSYaroslav Tykhiy * document, i.e., fail to pad short frames after untagging. 1074f6e5e0adSYaroslav Tykhiy * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but 1075f6e5e0adSYaroslav Tykhiy * untagging it will produce a 62-byte frame, which is a runt 1076f6e5e0adSYaroslav Tykhiy * and requires padding. There are VLAN-enabled network 1077f6e5e0adSYaroslav Tykhiy * devices that just discard such runts instead or mishandle 1078f6e5e0adSYaroslav Tykhiy * them somehow. 1079f6e5e0adSYaroslav Tykhiy */ 1080478e0520SHiroki Sato if (V_soft_pad && p->if_type == IFT_ETHER) { 1081f6e5e0adSYaroslav Tykhiy static char pad[8]; /* just zeros */ 1082f6e5e0adSYaroslav Tykhiy int n; 1083f6e5e0adSYaroslav Tykhiy 1084f6e5e0adSYaroslav Tykhiy for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len; 1085f6e5e0adSYaroslav Tykhiy n > 0; n -= sizeof(pad)) 1086f6e5e0adSYaroslav Tykhiy if (!m_append(m, min(n, sizeof(pad)), pad)) 1087f6e5e0adSYaroslav Tykhiy break; 1088f6e5e0adSYaroslav Tykhiy 1089f6e5e0adSYaroslav Tykhiy if (n > 0) { 1090f6e5e0adSYaroslav Tykhiy if_printf(ifp, "cannot pad short frame\n"); 1091a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1092f6e5e0adSYaroslav Tykhiy m_freem(m); 1093d9b1d615SJohn Baldwin return (0); 1094f6e5e0adSYaroslav Tykhiy } 1095f6e5e0adSYaroslav Tykhiy } 1096f6e5e0adSYaroslav Tykhiy 1097f6e5e0adSYaroslav Tykhiy /* 1098a3814acfSSam Leffler * If underlying interface can do VLAN tag insertion itself, 1099a3814acfSSam Leffler * just pass the packet along. However, we need some way to 1100a3814acfSSam Leffler * tell the interface where the packet came from so that it 1101a3814acfSSam Leffler * knows how to find the VLAN tag to use, so we attach a 1102a3814acfSSam Leffler * packet tag that holds it. 1103f731f104SBill Paul */ 11042ccbbd06SMarcelo Araujo if (vlan_mtag_pcp && (mtag = m_tag_locate(m, MTAG_8021Q, 11052ccbbd06SMarcelo Araujo MTAG_8021Q_PCP_OUT, NULL)) != NULL) 11062ccbbd06SMarcelo Araujo tag = EVL_MAKETAG(ifv->ifv_vid, *(uint8_t *)(mtag + 1), 0); 11072ccbbd06SMarcelo Araujo else 11082ccbbd06SMarcelo Araujo tag = ifv->ifv_tag; 1109b08347a0SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_HWTAGGING) { 11102ccbbd06SMarcelo Araujo m->m_pkthdr.ether_vtag = tag; 1111ba26134bSGleb Smirnoff m->m_flags |= M_VLANTAG; 1112f731f104SBill Paul } else { 11132ccbbd06SMarcelo Araujo m = ether_vlanencap(m, tag); 11144af90a4dSMatthew N. Dodd if (m == NULL) { 1115d9b1d615SJohn Baldwin if_printf(ifp, "unable to prepend VLAN header\n"); 1116a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1117d9b1d615SJohn Baldwin return (0); 11184af90a4dSMatthew N. Dodd } 1119f731f104SBill Paul } 11202cc2df49SGarrett Wollman 11212cc2df49SGarrett Wollman /* 11222cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 11232cc2df49SGarrett Wollman */ 1124aea78d20SKip Macy error = (p->if_transmit)(p, m); 1125299153b5SAlexander V. Chernikov if (error == 0) { 1126a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1127a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 1128a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast); 11291ad7a257SPyun YongHyeon } else 1130a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1131d9b1d615SJohn Baldwin return (error); 11322cc2df49SGarrett Wollman } 1133d9b1d615SJohn Baldwin 1134d9b1d615SJohn Baldwin /* 1135d9b1d615SJohn Baldwin * The ifp->if_qflush entry point for vlan(4) is a no-op. 1136d9b1d615SJohn Baldwin */ 1137d9b1d615SJohn Baldwin static void 1138d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused) 1139d9b1d615SJohn Baldwin { 1140f731f104SBill Paul } 1141f731f104SBill Paul 1142a3814acfSSam Leffler static void 1143a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 1144f731f104SBill Paul { 114575ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1146f731f104SBill Paul struct ifvlan *ifv; 1147772b000fSAlexander V. Chernikov TRUNK_LOCK_READER; 11482ccbbd06SMarcelo Araujo struct m_tag *mtag; 11492ccbbd06SMarcelo Araujo uint16_t vid, tag; 115075ee267cSGleb Smirnoff 115175ee267cSGleb Smirnoff KASSERT(trunk != NULL, ("%s: no trunk", __func__)); 1152a3814acfSSam Leffler 1153f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 1154a3814acfSSam Leffler /* 115514e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 1156a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 1157a3814acfSSam Leffler */ 11582ccbbd06SMarcelo Araujo tag = m->m_pkthdr.ether_vtag; 11596ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 1160a3814acfSSam Leffler } else { 116175ee267cSGleb Smirnoff struct ether_vlan_header *evl; 116275ee267cSGleb Smirnoff 116314e98256SYaroslav Tykhiy /* 116414e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 116514e98256SYaroslav Tykhiy */ 1166a3814acfSSam Leffler switch (ifp->if_type) { 1167a3814acfSSam Leffler case IFT_ETHER: 1168a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 1169a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 1170a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 1171a3814acfSSam Leffler return; 1172a3814acfSSam Leffler } 1173a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 11742ccbbd06SMarcelo Araujo tag = ntohs(evl->evl_tag); 1175db8b5973SYaroslav Tykhiy 1176db8b5973SYaroslav Tykhiy /* 11772dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 11782dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 11792dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 11802dc879b3SYaroslav Tykhiy * type field is already in place. 1181db8b5973SYaroslav Tykhiy */ 11822dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 11832dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 11842dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 1185a3814acfSSam Leffler break; 11862dc879b3SYaroslav Tykhiy 1187a3814acfSSam Leffler default: 1188db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 118960c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 119060c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 1191a3814acfSSam Leffler #endif 119260c60618SYaroslav Tykhiy m_freem(m); 11933751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 119460c60618SYaroslav Tykhiy return; 1195a3814acfSSam Leffler } 11967a46ec8fSBrooks Davis } 11977a46ec8fSBrooks Davis 11982ccbbd06SMarcelo Araujo vid = EVL_VLANOFTAG(tag); 11992ccbbd06SMarcelo Araujo 120015ed2fa1SYaroslav Tykhiy TRUNK_RLOCK(trunk); 12017983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 12022dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 120375ee267cSGleb Smirnoff TRUNK_RUNLOCK(trunk); 120475ee267cSGleb Smirnoff m_freem(m); 12053751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 120675ee267cSGleb Smirnoff return; 120775ee267cSGleb Smirnoff } 120875ee267cSGleb Smirnoff TRUNK_RUNLOCK(trunk); 1209f731f104SBill Paul 12102ccbbd06SMarcelo Araujo if (vlan_mtag_pcp) { 12112ccbbd06SMarcelo Araujo /* 12122ccbbd06SMarcelo Araujo * While uncommon, it is possible that we will find a 802.1q 12132ccbbd06SMarcelo Araujo * packet encapsulated inside another packet that also had an 12142ccbbd06SMarcelo Araujo * 802.1q header. For example, ethernet tunneled over IPSEC 12152ccbbd06SMarcelo Araujo * arriving over ethernet. In that case, we replace the 12162ccbbd06SMarcelo Araujo * existing 802.1q PCP m_tag value. 12172ccbbd06SMarcelo Araujo */ 12182ccbbd06SMarcelo Araujo mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 12192ccbbd06SMarcelo Araujo if (mtag == NULL) { 12202ccbbd06SMarcelo Araujo mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN, 12212ccbbd06SMarcelo Araujo sizeof(uint8_t), M_NOWAIT); 12222ccbbd06SMarcelo Araujo if (mtag == NULL) { 12232ccbbd06SMarcelo Araujo m_freem(m); 12242ccbbd06SMarcelo Araujo if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 12252ccbbd06SMarcelo Araujo return; 12262ccbbd06SMarcelo Araujo } 12272ccbbd06SMarcelo Araujo m_tag_prepend(m, mtag); 12282ccbbd06SMarcelo Araujo } 12292ccbbd06SMarcelo Araujo *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag); 12302ccbbd06SMarcelo Araujo } 12312ccbbd06SMarcelo Araujo 1232fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 1233c0304424SGleb Smirnoff if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1); 12342cc2df49SGarrett Wollman 1235a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 1236fc74a9f9SBrooks Davis (*ifp->if_input)(ifv->ifv_ifp, m); 12372cc2df49SGarrett Wollman } 12382cc2df49SGarrett Wollman 12392cc2df49SGarrett Wollman static int 12407983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid) 12412cc2df49SGarrett Wollman { 124275ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 12431cf236fbSYaroslav Tykhiy struct ifnet *ifp; 124475ee267cSGleb Smirnoff int error = 0; 12452cc2df49SGarrett Wollman 1246b1828acfSGleb Smirnoff /* 1247b1828acfSGleb Smirnoff * We can handle non-ethernet hardware types as long as 1248b1828acfSGleb Smirnoff * they handle the tagging and headers themselves. 1249b1828acfSGleb Smirnoff */ 1250e4cd31ddSJeff Roberson if (p->if_type != IFT_ETHER && 1251e4cd31ddSJeff Roberson (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 125215a66c21SBruce M Simpson return (EPROTONOSUPPORT); 125364a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 125464a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 1255b1828acfSGleb Smirnoff /* 1256b1828acfSGleb Smirnoff * Don't let the caller set up a VLAN VID with 1257b1828acfSGleb Smirnoff * anything except VLID bits. 1258b1828acfSGleb Smirnoff * VID numbers 0x0 and 0xFFF are reserved. 1259b1828acfSGleb Smirnoff */ 1260b1828acfSGleb Smirnoff if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK)) 1261b1828acfSGleb Smirnoff return (EINVAL); 126275ee267cSGleb Smirnoff if (ifv->ifv_trunk) 126315a66c21SBruce M Simpson return (EBUSY); 12642cc2df49SGarrett Wollman 126575ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 126675ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 126775ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 126875ee267cSGleb Smirnoff vlan_inithash(trunk); 126905a2398fSGleb Smirnoff VLAN_LOCK(); 127005a2398fSGleb Smirnoff if (p->if_vlantrunk != NULL) { 12712ccbbd06SMarcelo Araujo /* A race that is very unlikely to be hit. */ 127205a2398fSGleb Smirnoff vlan_freehash(trunk); 127305a2398fSGleb Smirnoff free(trunk, M_VLAN); 127405a2398fSGleb Smirnoff goto exists; 127505a2398fSGleb Smirnoff } 127675ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 127775ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 127875ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 127975ee267cSGleb Smirnoff trunk->parent = p; 128075ee267cSGleb Smirnoff } else { 128175ee267cSGleb Smirnoff VLAN_LOCK(); 128275ee267cSGleb Smirnoff exists: 128375ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 128475ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 128575ee267cSGleb Smirnoff } 128675ee267cSGleb Smirnoff 12877983103aSRobert Watson ifv->ifv_vid = vid; /* must set this before vlan_inshash() */ 12882ccbbd06SMarcelo Araujo ifv->ifv_pcp = 0; /* Default: best effort delivery. */ 12892ccbbd06SMarcelo Araujo vlan_tag_recalculate(ifv); 129075ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 129175ee267cSGleb Smirnoff if (error) 129275ee267cSGleb Smirnoff goto done; 129373f2233dSYaroslav Tykhiy ifv->ifv_proto = ETHERTYPE_VLAN; 1294a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1295a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 12961cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1297a3814acfSSam Leffler 1298a3814acfSSam Leffler /* 1299a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1300a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1301656acce4SYaroslav Tykhiy * use it. 1302a3814acfSSam Leffler */ 1303656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1304656acce4SYaroslav Tykhiy /* 1305656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1306656acce4SYaroslav Tykhiy * handle extended frames. 1307656acce4SYaroslav Tykhiy */ 1308a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1309656acce4SYaroslav Tykhiy } else { 1310a3814acfSSam Leffler /* 1311a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1312a3814acfSSam Leffler * makes us incompatible with strictly compliant 1313a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1314a3814acfSSam Leffler * the feature with other NetBSD implementations, 1315a3814acfSSam Leffler * which might still be useful. 1316a3814acfSSam Leffler */ 1317a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1318a3814acfSSam Leffler } 1319a3814acfSSam Leffler 132075ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 13211cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 1322e4cd31ddSJeff Roberson /* 1323e4cd31ddSJeff Roberson * Initialize fields from our parent. This duplicates some 1324e4cd31ddSJeff Roberson * work with ether_ifattach() but allows for non-ethernet 1325e4cd31ddSJeff Roberson * interfaces to also work. 1326e4cd31ddSJeff Roberson */ 13271cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 132875ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 1329e4cd31ddSJeff Roberson ifp->if_output = p->if_output; 1330e4cd31ddSJeff Roberson ifp->if_input = p->if_input; 1331e4cd31ddSJeff Roberson ifp->if_resolvemulti = p->if_resolvemulti; 1332e4cd31ddSJeff Roberson ifp->if_addrlen = p->if_addrlen; 1333e4cd31ddSJeff Roberson ifp->if_broadcastaddr = p->if_broadcastaddr; 1334e4cd31ddSJeff Roberson 13352cc2df49SGarrett Wollman /* 133624993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 133724993214SYaroslav Tykhiy * Other flags are none of our business. 13382cc2df49SGarrett Wollman */ 133964a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 13401cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 13411cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 13421cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 13431cf236fbSYaroslav Tykhiy 13441cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 13452cc2df49SGarrett Wollman 134675ee267cSGleb Smirnoff vlan_capabilities(ifv); 1347a3814acfSSam Leffler 1348a3814acfSSam Leffler /* 1349e4cd31ddSJeff Roberson * Set up our interface address to reflect the underlying 13502cc2df49SGarrett Wollman * physical interface's. 13512cc2df49SGarrett Wollman */ 1352e4cd31ddSJeff Roberson bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1353e4cd31ddSJeff Roberson ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1354e4cd31ddSJeff Roberson p->if_addrlen; 13551b2a4f7aSBill Fenner 13561b2a4f7aSBill Fenner /* 13571b2a4f7aSBill Fenner * Configure multicast addresses that may already be 13581b2a4f7aSBill Fenner * joined on the vlan device. 13591b2a4f7aSBill Fenner */ 13601cf236fbSYaroslav Tykhiy (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */ 13612ada9747SYaroslav Tykhiy 13622ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 13632ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 136475ee267cSGleb Smirnoff done: 136575ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 1366c725524cSJack F Vogel if (error == 0) 13677983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid); 136875ee267cSGleb Smirnoff VLAN_UNLOCK(); 136975ee267cSGleb Smirnoff 137075ee267cSGleb Smirnoff return (error); 13712cc2df49SGarrett Wollman } 13722cc2df49SGarrett Wollman 13736f359e28SJohn Baldwin static void 1374f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1375f731f104SBill Paul { 13765cb8c31aSYaroslav Tykhiy 13775cb8c31aSYaroslav Tykhiy VLAN_LOCK(); 137828cc4d37SJohn Baldwin vlan_unconfig_locked(ifp, 0); 13795cb8c31aSYaroslav Tykhiy VLAN_UNLOCK(); 13805cb8c31aSYaroslav Tykhiy } 13815cb8c31aSYaroslav Tykhiy 13826f359e28SJohn Baldwin static void 138328cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing) 13845cb8c31aSYaroslav Tykhiy { 138575ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1386f731f104SBill Paul struct vlan_mc_entry *mc; 1387f731f104SBill Paul struct ifvlan *ifv; 1388c725524cSJack F Vogel struct ifnet *parent; 138928cc4d37SJohn Baldwin int error; 1390f731f104SBill Paul 13915cb8c31aSYaroslav Tykhiy VLAN_LOCK_ASSERT(); 13924faedfe8SSam Leffler 1393f731f104SBill Paul ifv = ifp->if_softc; 139475ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 139522893351SJack F Vogel parent = NULL; 1396f731f104SBill Paul 139722893351SJack F Vogel if (trunk != NULL) { 139875ee267cSGleb Smirnoff 139975ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 140022893351SJack F Vogel parent = trunk->parent; 14011b2a4f7aSBill Fenner 1402f731f104SBill Paul /* 1403f731f104SBill Paul * Since the interface is being unconfigured, we need to 1404f731f104SBill Paul * empty the list of multicast groups that we may have joined 14051b2a4f7aSBill Fenner * while we were alive from the parent's list. 1406f731f104SBill Paul */ 1407c0cb022bSYaroslav Tykhiy while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 14086f359e28SJohn Baldwin /* 140928cc4d37SJohn Baldwin * If the parent interface is being detached, 1410b90dde2fSJohn Baldwin * all its multicast addresses have already 141128cc4d37SJohn Baldwin * been removed. Warn about errors if 141228cc4d37SJohn Baldwin * if_delmulti() does fail, but don't abort as 141328cc4d37SJohn Baldwin * all callers expect vlan destruction to 141428cc4d37SJohn Baldwin * succeed. 14156f359e28SJohn Baldwin */ 141628cc4d37SJohn Baldwin if (!departing) { 141728cc4d37SJohn Baldwin error = if_delmulti(parent, 1418e4cd31ddSJeff Roberson (struct sockaddr *)&mc->mc_addr); 141928cc4d37SJohn Baldwin if (error) 142028cc4d37SJohn Baldwin if_printf(ifp, 142128cc4d37SJohn Baldwin "Failed to delete multicast address from parent: %d\n", 142228cc4d37SJohn Baldwin error); 142328cc4d37SJohn Baldwin } 1424f731f104SBill Paul SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 14259d4fe4b2SBrooks Davis free(mc, M_VLAN); 1426f731f104SBill Paul } 1427a3814acfSSam Leffler 14281cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 142975ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 143075ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 143175ee267cSGleb Smirnoff 143275ee267cSGleb Smirnoff /* 143375ee267cSGleb Smirnoff * Check if we were the last. 143475ee267cSGleb Smirnoff */ 143575ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 14362d222cb7SAlexander Motin parent->if_vlantrunk = NULL; 143775ee267cSGleb Smirnoff /* 143875ee267cSGleb Smirnoff * XXXGL: If some ithread has already entered 143975ee267cSGleb Smirnoff * vlan_input() and is now blocked on the trunk 144075ee267cSGleb Smirnoff * lock, then it should preempt us right after 144175ee267cSGleb Smirnoff * unlock and finish its work. Then we will acquire 144275ee267cSGleb Smirnoff * lock again in trunk_destroy(). 144375ee267cSGleb Smirnoff */ 144475ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 144575ee267cSGleb Smirnoff trunk_destroy(trunk); 144675ee267cSGleb Smirnoff } else 144775ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 14481b2a4f7aSBill Fenner } 1449f731f104SBill Paul 1450f731f104SBill Paul /* Disconnect from parent. */ 14511cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 14521cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 14535cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 14545cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 14555cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1456f731f104SBill Paul 145722893351SJack F Vogel /* 145822893351SJack F Vogel * Only dispatch an event if vlan was 145922893351SJack F Vogel * attached, otherwise there is nothing 146022893351SJack F Vogel * to cleanup anyway. 146122893351SJack F Vogel */ 146222893351SJack F Vogel if (parent != NULL) 14637983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid); 1464f731f104SBill Paul } 1465f731f104SBill Paul 14661cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1467f731f104SBill Paul static int 14681cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 14691cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1470a3814acfSSam Leffler { 14711cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 14721cf236fbSYaroslav Tykhiy int error; 1473a3814acfSSam Leffler 14741cf236fbSYaroslav Tykhiy /* XXX VLAN_LOCK_ASSERT(); */ 1475a3814acfSSam Leffler 14761cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 14771cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 14781cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 14791cf236fbSYaroslav Tykhiy 14801cf236fbSYaroslav Tykhiy /* 14811cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 14821cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 14831cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 14841cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 14851cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 14861cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 14871cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 14881cf236fbSYaroslav Tykhiy */ 14891cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 149075ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 14911cf236fbSYaroslav Tykhiy if (error) 1492a3814acfSSam Leffler return (error); 14931cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 14941cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 14951cf236fbSYaroslav Tykhiy } 14961cf236fbSYaroslav Tykhiy return (0); 14971cf236fbSYaroslav Tykhiy } 14981cf236fbSYaroslav Tykhiy 14991cf236fbSYaroslav Tykhiy /* 15001cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 15011cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 15021cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 15031cf236fbSYaroslav Tykhiy */ 15041cf236fbSYaroslav Tykhiy static int 15051cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 15061cf236fbSYaroslav Tykhiy { 15071cf236fbSYaroslav Tykhiy int error, i; 15081cf236fbSYaroslav Tykhiy 15091cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 15101cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 15111cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 15121cf236fbSYaroslav Tykhiy if (error) 15131cf236fbSYaroslav Tykhiy return (error); 15141cf236fbSYaroslav Tykhiy } 15151cf236fbSYaroslav Tykhiy return (0); 1516a3814acfSSam Leffler } 1517a3814acfSSam Leffler 1518127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1519127d7b2dSAndre Oppermann static void 1520a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp) 1521127d7b2dSAndre Oppermann { 152275ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1523127d7b2dSAndre Oppermann struct ifvlan *ifv; 152475ee267cSGleb Smirnoff int i; 1525127d7b2dSAndre Oppermann 152675ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 152775ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 152815ed2fa1SYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 152975ee267cSGleb Smirnoff if (trunk->vlans[i] != NULL) { 153075ee267cSGleb Smirnoff ifv = trunk->vlans[i]; 153175ee267cSGleb Smirnoff #else 1532aad0be7aSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 1533aad0be7aSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) { 153475ee267cSGleb Smirnoff #endif 1535aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1536fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 153775ee267cSGleb Smirnoff trunk->parent->if_link_state); 1538127d7b2dSAndre Oppermann } 153975ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 154075ee267cSGleb Smirnoff } 154175ee267cSGleb Smirnoff 154275ee267cSGleb Smirnoff static void 154375ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 154475ee267cSGleb Smirnoff { 154575ee267cSGleb Smirnoff struct ifnet *p = PARENT(ifv); 154675ee267cSGleb Smirnoff struct ifnet *ifp = ifv->ifv_ifp; 15479fd573c3SHans Petter Selasky struct ifnet_hw_tsomax hw_tsomax; 154875ee267cSGleb Smirnoff 154975ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(TRUNK(ifv)); 155075ee267cSGleb Smirnoff 155175ee267cSGleb Smirnoff /* 155275ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 155375ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 155475ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 155575ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 155675ee267cSGleb Smirnoff */ 155775ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 155875ee267cSGleb Smirnoff ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM; 155975ee267cSGleb Smirnoff 156075ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 156175ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 156275ee267cSGleb Smirnoff ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM; 15639b76d9cbSPyun YongHyeon ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP | 1564bf7dcda3SGleb Smirnoff CSUM_UDP | CSUM_SCTP); 156575ee267cSGleb Smirnoff } else { 156675ee267cSGleb Smirnoff ifp->if_capenable = 0; 156775ee267cSGleb Smirnoff ifp->if_hwassist = 0; 156875ee267cSGleb Smirnoff } 15699b76d9cbSPyun YongHyeon /* 15709b76d9cbSPyun YongHyeon * If the parent interface can do TSO on VLANs then 15719b76d9cbSPyun YongHyeon * propagate the hardware-assisted flag. TSO on VLANs 15729b76d9cbSPyun YongHyeon * does not necessarily require hardware VLAN tagging. 15739b76d9cbSPyun YongHyeon */ 15749fd573c3SHans Petter Selasky memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 15759fd573c3SHans Petter Selasky if_hw_tsomax_common(p, &hw_tsomax); 15769fd573c3SHans Petter Selasky if_hw_tsomax_update(ifp, &hw_tsomax); 15779b76d9cbSPyun YongHyeon if (p->if_capabilities & IFCAP_VLAN_HWTSO) 15789b76d9cbSPyun YongHyeon ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO; 15799b76d9cbSPyun YongHyeon if (p->if_capenable & IFCAP_VLAN_HWTSO) { 15809b76d9cbSPyun YongHyeon ifp->if_capenable |= p->if_capenable & IFCAP_TSO; 15819b76d9cbSPyun YongHyeon ifp->if_hwassist |= p->if_hwassist & CSUM_TSO; 15829b76d9cbSPyun YongHyeon } else { 15839b76d9cbSPyun YongHyeon ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO); 15849b76d9cbSPyun YongHyeon ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO); 15859b76d9cbSPyun YongHyeon } 158609fe6320SNavdeep Parhar 158709fe6320SNavdeep Parhar /* 158809fe6320SNavdeep Parhar * If the parent interface can offload TCP connections over VLANs then 158909fe6320SNavdeep Parhar * propagate its TOE capability to the VLAN interface. 159009fe6320SNavdeep Parhar * 159109fe6320SNavdeep Parhar * All TOE drivers in the tree today can deal with VLANs. If this 159209fe6320SNavdeep Parhar * changes then IFCAP_VLAN_TOE should be promoted to a full capability 159309fe6320SNavdeep Parhar * with its own bit. 159409fe6320SNavdeep Parhar */ 159509fe6320SNavdeep Parhar #define IFCAP_VLAN_TOE IFCAP_TOE 159609fe6320SNavdeep Parhar if (p->if_capabilities & IFCAP_VLAN_TOE) 159709fe6320SNavdeep Parhar ifp->if_capabilities |= p->if_capabilities & IFCAP_TOE; 159809fe6320SNavdeep Parhar if (p->if_capenable & IFCAP_VLAN_TOE) { 159909fe6320SNavdeep Parhar TOEDEV(ifp) = TOEDEV(p); 160009fe6320SNavdeep Parhar ifp->if_capenable |= p->if_capenable & IFCAP_TOE; 160109fe6320SNavdeep Parhar } 1602*f3e7afe2SHans Petter Selasky 1603*f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1604*f3e7afe2SHans Petter Selasky /* 1605*f3e7afe2SHans Petter Selasky * If the parent interface supports ratelimiting, so does the 1606*f3e7afe2SHans Petter Selasky * VLAN interface. 1607*f3e7afe2SHans Petter Selasky */ 1608*f3e7afe2SHans Petter Selasky ifp->if_capabilities |= (p->if_capabilities & IFCAP_TXRTLMT); 1609*f3e7afe2SHans Petter Selasky ifp->if_capenable |= (p->if_capenable & IFCAP_TXRTLMT); 1610*f3e7afe2SHans Petter Selasky #endif 161175ee267cSGleb Smirnoff } 161275ee267cSGleb Smirnoff 161375ee267cSGleb Smirnoff static void 161475ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 161575ee267cSGleb Smirnoff { 161675ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 161775ee267cSGleb Smirnoff struct ifvlan *ifv; 161875ee267cSGleb Smirnoff int i; 161975ee267cSGleb Smirnoff 162075ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 162175ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 162215ed2fa1SYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 162375ee267cSGleb Smirnoff if (trunk->vlans[i] != NULL) { 162475ee267cSGleb Smirnoff ifv = trunk->vlans[i]; 162575ee267cSGleb Smirnoff #else 162675ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 162775ee267cSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 162875ee267cSGleb Smirnoff #endif 162975ee267cSGleb Smirnoff vlan_capabilities(ifv); 163075ee267cSGleb Smirnoff } 163175ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 1632127d7b2dSAndre Oppermann } 1633127d7b2dSAndre Oppermann 1634a3814acfSSam Leffler static int 1635cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 16362cc2df49SGarrett Wollman { 16372cc2df49SGarrett Wollman struct ifnet *p; 16382cc2df49SGarrett Wollman struct ifreq *ifr; 1639e4cd31ddSJeff Roberson struct ifaddr *ifa; 16402cc2df49SGarrett Wollman struct ifvlan *ifv; 16412d222cb7SAlexander Motin struct ifvlantrunk *trunk; 16422cc2df49SGarrett Wollman struct vlanreq vlr; 16432cc2df49SGarrett Wollman int error = 0; 16442cc2df49SGarrett Wollman 16452cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 1646e4cd31ddSJeff Roberson ifa = (struct ifaddr *) data; 16472cc2df49SGarrett Wollman ifv = ifp->if_softc; 16482cc2df49SGarrett Wollman 16492cc2df49SGarrett Wollman switch (cmd) { 1650e4cd31ddSJeff Roberson case SIOCSIFADDR: 1651e4cd31ddSJeff Roberson ifp->if_flags |= IFF_UP; 1652e4cd31ddSJeff Roberson #ifdef INET 1653e4cd31ddSJeff Roberson if (ifa->ifa_addr->sa_family == AF_INET) 1654e4cd31ddSJeff Roberson arp_ifinit(ifp, ifa); 1655e4cd31ddSJeff Roberson #endif 1656e4cd31ddSJeff Roberson break; 1657e4cd31ddSJeff Roberson case SIOCGIFADDR: 1658e4cd31ddSJeff Roberson { 1659e4cd31ddSJeff Roberson struct sockaddr *sa; 1660e4cd31ddSJeff Roberson 1661e4cd31ddSJeff Roberson sa = (struct sockaddr *)&ifr->ifr_data; 1662e4cd31ddSJeff Roberson bcopy(IF_LLADDR(ifp), sa->sa_data, ifp->if_addrlen); 1663e4cd31ddSJeff Roberson } 1664e4cd31ddSJeff Roberson break; 1665b3cca108SBill Fenner case SIOCGIFMEDIA: 16664faedfe8SSam Leffler VLAN_LOCK(); 166775ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 1668d8564efdSEd Maste p = PARENT(ifv); 16694faedfe8SSam Leffler VLAN_UNLOCK(); 1670d8564efdSEd Maste error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 1671b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 1672b3cca108SBill Fenner if (error == 0) { 1673b3cca108SBill Fenner struct ifmediareq *ifmr; 1674b3cca108SBill Fenner 1675b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 1676b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1677b3cca108SBill Fenner ifmr->ifm_count = 1; 1678b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 1679b3cca108SBill Fenner ifmr->ifm_ulist, 1680b3cca108SBill Fenner sizeof(int)); 1681b3cca108SBill Fenner } 1682b3cca108SBill Fenner } 16834faedfe8SSam Leffler } else { 16844faedfe8SSam Leffler VLAN_UNLOCK(); 1685b3cca108SBill Fenner error = EINVAL; 16864faedfe8SSam Leffler } 1687b3cca108SBill Fenner break; 1688b3cca108SBill Fenner 1689b3cca108SBill Fenner case SIOCSIFMEDIA: 1690b3cca108SBill Fenner error = EINVAL; 1691b3cca108SBill Fenner break; 1692b3cca108SBill Fenner 16932cc2df49SGarrett Wollman case SIOCSIFMTU: 16942cc2df49SGarrett Wollman /* 16952cc2df49SGarrett Wollman * Set the interface MTU. 16962cc2df49SGarrett Wollman */ 16974faedfe8SSam Leffler VLAN_LOCK(); 169875ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 1699a3814acfSSam Leffler if (ifr->ifr_mtu > 170075ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1701a3814acfSSam Leffler ifr->ifr_mtu < 1702a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 17032cc2df49SGarrett Wollman error = EINVAL; 1704a3814acfSSam Leffler else 17052cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 1706a3814acfSSam Leffler } else 1707a3814acfSSam Leffler error = EINVAL; 17084faedfe8SSam Leffler VLAN_UNLOCK(); 17092cc2df49SGarrett Wollman break; 17102cc2df49SGarrett Wollman 17112cc2df49SGarrett Wollman case SIOCSETVLAN: 1712ccf7ba97SMarko Zec #ifdef VIMAGE 171315f6780eSRobert Watson /* 171415f6780eSRobert Watson * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN 171515f6780eSRobert Watson * interface to be delegated to a jail without allowing the 171615f6780eSRobert Watson * jail to change what underlying interface/VID it is 171715f6780eSRobert Watson * associated with. We are not entirely convinced that this 17185a39f779SRobert Watson * is the right way to accomplish that policy goal. 171915f6780eSRobert Watson */ 1720ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 1721ccf7ba97SMarko Zec error = EPERM; 1722ccf7ba97SMarko Zec break; 1723ccf7ba97SMarko Zec } 1724ccf7ba97SMarko Zec #endif 172515a66c21SBruce M Simpson error = copyin(ifr->ifr_data, &vlr, sizeof(vlr)); 17262cc2df49SGarrett Wollman if (error) 17272cc2df49SGarrett Wollman break; 17282cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 1729f731f104SBill Paul vlan_unconfig(ifp); 17302cc2df49SGarrett Wollman break; 17312cc2df49SGarrett Wollman } 17322cc2df49SGarrett Wollman p = ifunit(vlr.vlr_parent); 17331bdc73d3SEd Maste if (p == NULL) { 17342cc2df49SGarrett Wollman error = ENOENT; 17352cc2df49SGarrett Wollman break; 17362cc2df49SGarrett Wollman } 173775ee267cSGleb Smirnoff error = vlan_config(ifv, p, vlr.vlr_tag); 173875ee267cSGleb Smirnoff if (error) 17392cc2df49SGarrett Wollman break; 1740a3814acfSSam Leffler 17411cf236fbSYaroslav Tykhiy /* Update flags on the parent, if necessary. */ 17421cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 1); 17432cc2df49SGarrett Wollman break; 17442cc2df49SGarrett Wollman 17452cc2df49SGarrett Wollman case SIOCGETVLAN: 1746ccf7ba97SMarko Zec #ifdef VIMAGE 1747ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 1748ccf7ba97SMarko Zec error = EPERM; 1749ccf7ba97SMarko Zec break; 1750ccf7ba97SMarko Zec } 1751ccf7ba97SMarko Zec #endif 175215a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 17534faedfe8SSam Leffler VLAN_LOCK(); 175475ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 175575ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 17569bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 17577983103aSRobert Watson vlr.vlr_tag = ifv->ifv_vid; 17582cc2df49SGarrett Wollman } 17594faedfe8SSam Leffler VLAN_UNLOCK(); 176015a66c21SBruce M Simpson error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 17612cc2df49SGarrett Wollman break; 17622cc2df49SGarrett Wollman 17632cc2df49SGarrett Wollman case SIOCSIFFLAGS: 17642cc2df49SGarrett Wollman /* 17651cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 17661cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 17672cc2df49SGarrett Wollman */ 176875ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 17691cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 17702cc2df49SGarrett Wollman break; 1771a3814acfSSam Leffler 1772f731f104SBill Paul case SIOCADDMULTI: 1773f731f104SBill Paul case SIOCDELMULTI: 177475ee267cSGleb Smirnoff /* 177575ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 177675ee267cSGleb Smirnoff * when we do. 177775ee267cSGleb Smirnoff */ 17782d222cb7SAlexander Motin trunk = TRUNK(ifv); 17792d222cb7SAlexander Motin if (trunk != NULL) { 17802d222cb7SAlexander Motin TRUNK_LOCK(trunk); 1781f731f104SBill Paul error = vlan_setmulti(ifp); 17822d222cb7SAlexander Motin TRUNK_UNLOCK(trunk); 17832d222cb7SAlexander Motin } 1784f731f104SBill Paul break; 178575ee267cSGleb Smirnoff 17862ccbbd06SMarcelo Araujo case SIOCGVLANPCP: 17872ccbbd06SMarcelo Araujo #ifdef VIMAGE 17882ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 17892ccbbd06SMarcelo Araujo error = EPERM; 17902ccbbd06SMarcelo Araujo break; 17912ccbbd06SMarcelo Araujo } 17922ccbbd06SMarcelo Araujo #endif 17932ccbbd06SMarcelo Araujo ifr->ifr_vlan_pcp = ifv->ifv_pcp; 17942ccbbd06SMarcelo Araujo break; 17952ccbbd06SMarcelo Araujo 17962ccbbd06SMarcelo Araujo case SIOCSVLANPCP: 17972ccbbd06SMarcelo Araujo #ifdef VIMAGE 17982ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 17992ccbbd06SMarcelo Araujo error = EPERM; 18002ccbbd06SMarcelo Araujo break; 18012ccbbd06SMarcelo Araujo } 18022ccbbd06SMarcelo Araujo #endif 18032ccbbd06SMarcelo Araujo error = priv_check(curthread, PRIV_NET_SETVLANPCP); 18042ccbbd06SMarcelo Araujo if (error) 18052ccbbd06SMarcelo Araujo break; 18062ccbbd06SMarcelo Araujo if (ifr->ifr_vlan_pcp > 7) { 18072ccbbd06SMarcelo Araujo error = EINVAL; 18082ccbbd06SMarcelo Araujo break; 18092ccbbd06SMarcelo Araujo } 18102ccbbd06SMarcelo Araujo ifv->ifv_pcp = ifr->ifr_vlan_pcp; 18112ccbbd06SMarcelo Araujo vlan_tag_recalculate(ifv); 18122ccbbd06SMarcelo Araujo break; 18132ccbbd06SMarcelo Araujo 18142cc2df49SGarrett Wollman default: 1815e4cd31ddSJeff Roberson error = EINVAL; 1816e4cd31ddSJeff Roberson break; 18172cc2df49SGarrett Wollman } 181815a66c21SBruce M Simpson 181915a66c21SBruce M Simpson return (error); 18202cc2df49SGarrett Wollman } 1821*f3e7afe2SHans Petter Selasky 1822*f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1823*f3e7afe2SHans Petter Selasky static int 1824*f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp, 1825*f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *params, 1826*f3e7afe2SHans Petter Selasky struct m_snd_tag **ppmt) 1827*f3e7afe2SHans Petter Selasky { 1828*f3e7afe2SHans Petter Selasky 1829*f3e7afe2SHans Petter Selasky /* get trunk device */ 1830*f3e7afe2SHans Petter Selasky ifp = vlan_trunkdev(ifp); 1831*f3e7afe2SHans Petter Selasky if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0) 1832*f3e7afe2SHans Petter Selasky return (EOPNOTSUPP); 1833*f3e7afe2SHans Petter Selasky /* forward allocation request */ 1834*f3e7afe2SHans Petter Selasky return (ifp->if_snd_tag_alloc(ifp, params, ppmt)); 1835*f3e7afe2SHans Petter Selasky } 1836*f3e7afe2SHans Petter Selasky #endif 1837