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" 49f3e7afe2SHans 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 */ 116d89baa5aSAlexander Motin int ifv_capenable; 117a3814acfSSam Leffler struct ifv_linkmib { 118a3814acfSSam Leffler int ifvm_encaplen; /* encapsulation length */ 119a3814acfSSam Leffler int ifvm_mtufudge; /* MTU fudged by this much */ 120a3814acfSSam Leffler int ifvm_mintu; /* min transmission unit */ 12173f2233dSYaroslav Tykhiy uint16_t ifvm_proto; /* encapsulation ethertype */ 12275ee267cSGleb Smirnoff uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 1232ccbbd06SMarcelo Araujo uint16_t ifvm_vid; /* VLAN ID */ 1242ccbbd06SMarcelo Araujo uint8_t ifvm_pcp; /* Priority Code Point (PCP). */ 125a3814acfSSam Leffler } ifv_mib; 126114c608cSYaroslav Tykhiy SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 127c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY 128a3814acfSSam Leffler LIST_ENTRY(ifvlan) ifv_list; 129c0cb022bSYaroslav Tykhiy #endif 130a3814acfSSam Leffler }; 13173f2233dSYaroslav Tykhiy #define ifv_proto ifv_mib.ifvm_proto 1322ccbbd06SMarcelo Araujo #define ifv_tag ifv_mib.ifvm_tag 1332ccbbd06SMarcelo Araujo #define ifv_vid ifv_mib.ifvm_vid 1342ccbbd06SMarcelo Araujo #define ifv_pcp ifv_mib.ifvm_pcp 135a3814acfSSam Leffler #define ifv_encaplen ifv_mib.ifvm_encaplen 136a3814acfSSam Leffler #define ifv_mtufudge ifv_mib.ifvm_mtufudge 137a3814acfSSam Leffler #define ifv_mintu ifv_mib.ifvm_mintu 138a3814acfSSam Leffler 13975ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */ 1401cf236fbSYaroslav Tykhiy static struct { 1411cf236fbSYaroslav Tykhiy int flag; 1421cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 1431cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 1441cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 1451cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 1461cf236fbSYaroslav Tykhiy {0, NULL} 1471cf236fbSYaroslav Tykhiy }; 148a3814acfSSam Leffler 1494a408dcbSBill Paul SYSCTL_DECL(_net_link); 1506472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, 1516472ac3dSEd Schouten "IEEE 802.1Q VLAN"); 1526472ac3dSEd Schouten static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, 1536472ac3dSEd Schouten "for consistency"); 1542cc2df49SGarrett Wollman 155478e0520SHiroki Sato static VNET_DEFINE(int, soft_pad); 156478e0520SHiroki Sato #define V_soft_pad VNET(soft_pad) 157478e0520SHiroki Sato SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW | CTLFLAG_VNET, 158478e0520SHiroki Sato &VNET_NAME(soft_pad), 0, "pad short frames before tagging"); 159f6e5e0adSYaroslav Tykhiy 1602ccbbd06SMarcelo Araujo /* 1612ccbbd06SMarcelo Araujo * For now, make preserving PCP via an mbuf tag optional, as it increases 1622ccbbd06SMarcelo Araujo * per-packet memory allocations and frees. In the future, it would be 1632ccbbd06SMarcelo Araujo * preferable to reuse ether_vtag for this, or similar. 1642ccbbd06SMarcelo Araujo */ 1652ccbbd06SMarcelo Araujo static int vlan_mtag_pcp = 0; 1662ccbbd06SMarcelo Araujo SYSCTL_INT(_net_link_vlan, OID_AUTO, mtag_pcp, CTLFLAG_RW, &vlan_mtag_pcp, 0, 1672ccbbd06SMarcelo Araujo "Retain VLAN PCP information as packets are passed up the stack"); 1682ccbbd06SMarcelo Araujo 16942a58907SGleb Smirnoff static const char vlanname[] = "vlan"; 17042a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface"); 1712cc2df49SGarrett Wollman 1725cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag; 173ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag; 1745cb8c31aSYaroslav Tykhiy 1754faedfe8SSam Leffler /* 17675ee267cSGleb Smirnoff * We have a global mutex, that is used to serialize configuration 17775ee267cSGleb Smirnoff * changes and isn't used in normal packet delivery. 17875ee267cSGleb Smirnoff * 17967975c79SGleb Smirnoff * We also have a per-trunk rmlock(9), that is locked shared on packet 18075ee267cSGleb Smirnoff * processing and exclusive when configuration is changed. 18175ee267cSGleb Smirnoff * 18275ee267cSGleb Smirnoff * The VLAN_ARRAY substitutes the dynamic hash with a static array 183ad387028SAndrew Thompson * with 4096 entries. In theory this can give a boost in processing, 18475ee267cSGleb Smirnoff * however on practice it does not. Probably this is because array 18575ee267cSGleb Smirnoff * is too big to fit into CPU cache. 1864faedfe8SSam Leffler */ 187e4cd31ddSJeff Roberson static struct sx ifv_lock; 188e4cd31ddSJeff Roberson #define VLAN_LOCK_INIT() sx_init(&ifv_lock, "vlan_global") 189e4cd31ddSJeff Roberson #define VLAN_LOCK_DESTROY() sx_destroy(&ifv_lock) 190e4cd31ddSJeff Roberson #define VLAN_LOCK_ASSERT() sx_assert(&ifv_lock, SA_LOCKED) 191e4cd31ddSJeff Roberson #define VLAN_LOCK() sx_xlock(&ifv_lock) 192e4cd31ddSJeff Roberson #define VLAN_UNLOCK() sx_xunlock(&ifv_lock) 193772b000fSAlexander V. Chernikov #define TRUNK_LOCK_INIT(trunk) rm_init(&(trunk)->lock, vlanname) 194772b000fSAlexander V. Chernikov #define TRUNK_LOCK_DESTROY(trunk) rm_destroy(&(trunk)->lock) 195772b000fSAlexander V. Chernikov #define TRUNK_LOCK(trunk) rm_wlock(&(trunk)->lock) 196772b000fSAlexander V. Chernikov #define TRUNK_UNLOCK(trunk) rm_wunlock(&(trunk)->lock) 197772b000fSAlexander V. Chernikov #define TRUNK_LOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_WLOCKED) 198772b000fSAlexander V. Chernikov #define TRUNK_RLOCK(trunk) rm_rlock(&(trunk)->lock, &tracker) 199772b000fSAlexander V. Chernikov #define TRUNK_RUNLOCK(trunk) rm_runlock(&(trunk)->lock, &tracker) 200772b000fSAlexander V. Chernikov #define TRUNK_LOCK_RASSERT(trunk) rm_assert(&(trunk)->lock, RA_RLOCKED) 201772b000fSAlexander V. Chernikov #define TRUNK_LOCK_READER struct rm_priotracker tracker 20275ee267cSGleb Smirnoff 20375ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 20475ee267cSGleb Smirnoff static void vlan_inithash(struct ifvlantrunk *trunk); 20575ee267cSGleb Smirnoff static void vlan_freehash(struct ifvlantrunk *trunk); 20675ee267cSGleb Smirnoff static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 20775ee267cSGleb Smirnoff static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 20875ee267cSGleb Smirnoff static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 20975ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 2107983103aSRobert Watson uint16_t vid); 21175ee267cSGleb Smirnoff #endif 21275ee267cSGleb Smirnoff static void trunk_destroy(struct ifvlantrunk *trunk); 2134faedfe8SSam Leffler 214114c608cSYaroslav Tykhiy static void vlan_init(void *foo); 215a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 216cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 217f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 218f3e7afe2SHans Petter Selasky static int vlan_snd_tag_alloc(struct ifnet *, 219f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *, struct m_snd_tag **); 220f3e7afe2SHans Petter Selasky #endif 221d9b1d615SJohn Baldwin static void vlan_qflush(struct ifnet *ifp); 2221cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 2231cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 2241cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 225f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 226d9b1d615SJohn Baldwin static int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 2276f359e28SJohn Baldwin static void vlan_unconfig(struct ifnet *ifp); 22828cc4d37SJohn Baldwin static void vlan_unconfig_locked(struct ifnet *ifp, int departing); 22975ee267cSGleb Smirnoff static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag); 230a6fffd6cSBrooks Davis static void vlan_link_state(struct ifnet *ifp); 23175ee267cSGleb Smirnoff static void vlan_capabilities(struct ifvlan *ifv); 23275ee267cSGleb Smirnoff static void vlan_trunk_capabilities(struct ifnet *ifp); 233f731f104SBill Paul 234f941c31aSGleb Smirnoff static struct ifnet *vlan_clone_match_ethervid(const char *, int *); 235f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 2366b7330e2SSam Leffler static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 237f889d2efSBrooks Davis static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 238f889d2efSBrooks Davis 2395cb8c31aSYaroslav Tykhiy static void vlan_ifdetach(void *arg, struct ifnet *ifp); 240ea4ca115SAndrew Thompson static void vlan_iflladdr(void *arg, struct ifnet *ifp); 2415cb8c31aSYaroslav Tykhiy 24242a58907SGleb Smirnoff static struct if_clone *vlan_cloner; 2439d4fe4b2SBrooks Davis 244ccf7ba97SMarko Zec #ifdef VIMAGE 24542a58907SGleb Smirnoff static VNET_DEFINE(struct if_clone *, vlan_cloner); 246ccf7ba97SMarko Zec #define V_vlan_cloner VNET(vlan_cloner) 247ccf7ba97SMarko Zec #endif 248ccf7ba97SMarko Zec 24975ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 25075ee267cSGleb Smirnoff #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 251114c608cSYaroslav Tykhiy 25275ee267cSGleb Smirnoff static void 25375ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk) 25475ee267cSGleb Smirnoff { 25575ee267cSGleb Smirnoff int i, n; 25675ee267cSGleb Smirnoff 25775ee267cSGleb Smirnoff /* 25875ee267cSGleb Smirnoff * The trunk must not be locked here since we call malloc(M_WAITOK). 25975ee267cSGleb Smirnoff * It is OK in case this function is called before the trunk struct 26075ee267cSGleb Smirnoff * gets hooked up and becomes visible from other threads. 26175ee267cSGleb Smirnoff */ 26275ee267cSGleb Smirnoff 26375ee267cSGleb Smirnoff KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 26475ee267cSGleb Smirnoff ("%s: hash already initialized", __func__)); 26575ee267cSGleb Smirnoff 26675ee267cSGleb Smirnoff trunk->hwidth = VLAN_DEF_HWIDTH; 26775ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 26875ee267cSGleb Smirnoff trunk->hmask = n - 1; 26975ee267cSGleb Smirnoff trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 27075ee267cSGleb Smirnoff for (i = 0; i < n; i++) 27175ee267cSGleb Smirnoff LIST_INIT(&trunk->hash[i]); 27275ee267cSGleb Smirnoff } 27375ee267cSGleb Smirnoff 27475ee267cSGleb Smirnoff static void 27575ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk) 27675ee267cSGleb Smirnoff { 27775ee267cSGleb Smirnoff #ifdef INVARIANTS 27875ee267cSGleb Smirnoff int i; 27975ee267cSGleb Smirnoff 28075ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 28175ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 28275ee267cSGleb Smirnoff KASSERT(LIST_EMPTY(&trunk->hash[i]), 28375ee267cSGleb Smirnoff ("%s: hash table not empty", __func__)); 28475ee267cSGleb Smirnoff #endif 28575ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 28675ee267cSGleb Smirnoff trunk->hash = NULL; 28775ee267cSGleb Smirnoff trunk->hwidth = trunk->hmask = 0; 28875ee267cSGleb Smirnoff } 28975ee267cSGleb Smirnoff 29075ee267cSGleb Smirnoff static int 29175ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 29275ee267cSGleb Smirnoff { 29375ee267cSGleb Smirnoff int i, b; 29475ee267cSGleb Smirnoff struct ifvlan *ifv2; 29575ee267cSGleb Smirnoff 29675ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 29775ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 29875ee267cSGleb Smirnoff 29975ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 3007983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 30175ee267cSGleb Smirnoff LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 3027983103aSRobert Watson if (ifv->ifv_vid == ifv2->ifv_vid) 30375ee267cSGleb Smirnoff return (EEXIST); 30475ee267cSGleb Smirnoff 30575ee267cSGleb Smirnoff /* 30675ee267cSGleb Smirnoff * Grow the hash when the number of vlans exceeds half of the number of 30775ee267cSGleb Smirnoff * hash buckets squared. This will make the average linked-list length 30875ee267cSGleb Smirnoff * buckets/2. 30975ee267cSGleb Smirnoff */ 31075ee267cSGleb Smirnoff if (trunk->refcnt > (b * b) / 2) { 31175ee267cSGleb Smirnoff vlan_growhash(trunk, 1); 3127983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 31375ee267cSGleb Smirnoff } 31475ee267cSGleb Smirnoff LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 31575ee267cSGleb Smirnoff trunk->refcnt++; 31675ee267cSGleb Smirnoff 31775ee267cSGleb Smirnoff return (0); 31875ee267cSGleb Smirnoff } 31975ee267cSGleb Smirnoff 32075ee267cSGleb Smirnoff static int 32175ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 32275ee267cSGleb Smirnoff { 32375ee267cSGleb Smirnoff int i, b; 32475ee267cSGleb Smirnoff struct ifvlan *ifv2; 32575ee267cSGleb Smirnoff 32675ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 32775ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 32875ee267cSGleb Smirnoff 32975ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 3307983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 33175ee267cSGleb Smirnoff LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 33275ee267cSGleb Smirnoff if (ifv2 == ifv) { 33375ee267cSGleb Smirnoff trunk->refcnt--; 33475ee267cSGleb Smirnoff LIST_REMOVE(ifv2, ifv_list); 33575ee267cSGleb Smirnoff if (trunk->refcnt < (b * b) / 2) 33675ee267cSGleb Smirnoff vlan_growhash(trunk, -1); 33775ee267cSGleb Smirnoff return (0); 33875ee267cSGleb Smirnoff } 33975ee267cSGleb Smirnoff 34075ee267cSGleb Smirnoff panic("%s: vlan not found\n", __func__); 34175ee267cSGleb Smirnoff return (ENOENT); /*NOTREACHED*/ 34275ee267cSGleb Smirnoff } 34375ee267cSGleb Smirnoff 34475ee267cSGleb Smirnoff /* 34575ee267cSGleb Smirnoff * Grow the hash larger or smaller if memory permits. 34675ee267cSGleb Smirnoff */ 34775ee267cSGleb Smirnoff static void 34875ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 34975ee267cSGleb Smirnoff { 35075ee267cSGleb Smirnoff struct ifvlan *ifv; 35175ee267cSGleb Smirnoff struct ifvlanhead *hash2; 35275ee267cSGleb Smirnoff int hwidth2, i, j, n, n2; 35375ee267cSGleb Smirnoff 35475ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 35575ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 35675ee267cSGleb Smirnoff 35775ee267cSGleb Smirnoff if (howmuch == 0) { 35875ee267cSGleb Smirnoff /* Harmless yet obvious coding error */ 35975ee267cSGleb Smirnoff printf("%s: howmuch is 0\n", __func__); 36075ee267cSGleb Smirnoff return; 36175ee267cSGleb Smirnoff } 36275ee267cSGleb Smirnoff 36375ee267cSGleb Smirnoff hwidth2 = trunk->hwidth + howmuch; 36475ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 36575ee267cSGleb Smirnoff n2 = 1 << hwidth2; 36675ee267cSGleb Smirnoff /* Do not shrink the table below the default */ 36775ee267cSGleb Smirnoff if (hwidth2 < VLAN_DEF_HWIDTH) 36875ee267cSGleb Smirnoff return; 36975ee267cSGleb Smirnoff 37075ee267cSGleb Smirnoff /* M_NOWAIT because we're called with trunk mutex held */ 37175ee267cSGleb Smirnoff hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT); 37275ee267cSGleb Smirnoff if (hash2 == NULL) { 37375ee267cSGleb Smirnoff printf("%s: out of memory -- hash size not changed\n", 37475ee267cSGleb Smirnoff __func__); 37575ee267cSGleb Smirnoff return; /* We can live with the old hash table */ 37675ee267cSGleb Smirnoff } 37775ee267cSGleb Smirnoff for (j = 0; j < n2; j++) 37875ee267cSGleb Smirnoff LIST_INIT(&hash2[j]); 37975ee267cSGleb Smirnoff for (i = 0; i < n; i++) 380c0cb022bSYaroslav Tykhiy while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) { 38175ee267cSGleb Smirnoff LIST_REMOVE(ifv, ifv_list); 3827983103aSRobert Watson j = HASH(ifv->ifv_vid, n2 - 1); 38375ee267cSGleb Smirnoff LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 38475ee267cSGleb Smirnoff } 38575ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 38675ee267cSGleb Smirnoff trunk->hash = hash2; 38775ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 38875ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 389f84b2d69SYaroslav Tykhiy 390f84b2d69SYaroslav Tykhiy if (bootverbose) 391f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 392f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 39375ee267cSGleb Smirnoff } 39475ee267cSGleb Smirnoff 39575ee267cSGleb Smirnoff static __inline struct ifvlan * 3967983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 39775ee267cSGleb Smirnoff { 39875ee267cSGleb Smirnoff struct ifvlan *ifv; 39975ee267cSGleb Smirnoff 40075ee267cSGleb Smirnoff TRUNK_LOCK_RASSERT(trunk); 40175ee267cSGleb Smirnoff 4027983103aSRobert Watson LIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list) 4037983103aSRobert Watson if (ifv->ifv_vid == vid) 40475ee267cSGleb Smirnoff return (ifv); 40575ee267cSGleb Smirnoff return (NULL); 40675ee267cSGleb Smirnoff } 40775ee267cSGleb Smirnoff 40875ee267cSGleb Smirnoff #if 0 40975ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 41075ee267cSGleb Smirnoff static void 41175ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 41275ee267cSGleb Smirnoff { 41375ee267cSGleb Smirnoff int i; 41475ee267cSGleb Smirnoff struct ifvlan *ifv; 41575ee267cSGleb Smirnoff 41675ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 41775ee267cSGleb Smirnoff printf("%d: ", i); 41875ee267cSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 41975ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 42075ee267cSGleb Smirnoff printf("\n"); 42175ee267cSGleb Smirnoff } 42275ee267cSGleb Smirnoff } 42375ee267cSGleb Smirnoff #endif /* 0 */ 424e4cd31ddSJeff Roberson #else 425e4cd31ddSJeff Roberson 426e4cd31ddSJeff Roberson static __inline struct ifvlan * 4277983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 428e4cd31ddSJeff Roberson { 429e4cd31ddSJeff Roberson 4307983103aSRobert Watson return trunk->vlans[vid]; 431e4cd31ddSJeff Roberson } 432e4cd31ddSJeff Roberson 433e4cd31ddSJeff Roberson static __inline int 434e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 435e4cd31ddSJeff Roberson { 436e4cd31ddSJeff Roberson 4377983103aSRobert Watson if (trunk->vlans[ifv->ifv_vid] != NULL) 438e4cd31ddSJeff Roberson return EEXIST; 4397983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = ifv; 440e4cd31ddSJeff Roberson trunk->refcnt++; 441e4cd31ddSJeff Roberson 442e4cd31ddSJeff Roberson return (0); 443e4cd31ddSJeff Roberson } 444e4cd31ddSJeff Roberson 445e4cd31ddSJeff Roberson static __inline int 446e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 447e4cd31ddSJeff Roberson { 448e4cd31ddSJeff Roberson 4497983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = NULL; 450e4cd31ddSJeff Roberson trunk->refcnt--; 451e4cd31ddSJeff Roberson 452e4cd31ddSJeff Roberson return (0); 453e4cd31ddSJeff Roberson } 454e4cd31ddSJeff Roberson 455e4cd31ddSJeff Roberson static __inline void 456e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk) 457e4cd31ddSJeff Roberson { 458e4cd31ddSJeff Roberson } 459e4cd31ddSJeff Roberson 460e4cd31ddSJeff Roberson static __inline void 461e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk) 462e4cd31ddSJeff Roberson { 463e4cd31ddSJeff Roberson } 464e4cd31ddSJeff Roberson 46575ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 46675ee267cSGleb Smirnoff 46775ee267cSGleb Smirnoff static void 46875ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 46975ee267cSGleb Smirnoff { 47075ee267cSGleb Smirnoff VLAN_LOCK_ASSERT(); 47175ee267cSGleb Smirnoff 47275ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 47375ee267cSGleb Smirnoff vlan_freehash(trunk); 47475ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 47533499e2aSYaroslav Tykhiy TRUNK_UNLOCK(trunk); 47633499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 47775ee267cSGleb Smirnoff free(trunk, M_VLAN); 47875ee267cSGleb Smirnoff } 47975ee267cSGleb Smirnoff 480f731f104SBill Paul /* 481f731f104SBill Paul * Program our multicast filter. What we're actually doing is 482f731f104SBill Paul * programming the multicast filter of the parent. This has the 483f731f104SBill Paul * side effect of causing the parent interface to receive multicast 484f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 485f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 486f731f104SBill Paul * to avoid this: there really is only one physical interface. 487f731f104SBill Paul */ 4882b120974SPeter Wemm static int 4892b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 490f731f104SBill Paul { 491f731f104SBill Paul struct ifnet *ifp_p; 4922d222cb7SAlexander Motin struct ifmultiaddr *ifma; 493f731f104SBill Paul struct ifvlan *sc; 494c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 495f731f104SBill Paul int error; 496f731f104SBill Paul 497f731f104SBill Paul /* Find the parent. */ 498f731f104SBill Paul sc = ifp->if_softc; 4992d222cb7SAlexander Motin TRUNK_LOCK_ASSERT(TRUNK(sc)); 50075ee267cSGleb Smirnoff ifp_p = PARENT(sc); 5011b2a4f7aSBill Fenner 5028b615593SMarko Zec CURVNET_SET_QUIET(ifp_p->if_vnet); 5038b615593SMarko Zec 504f731f104SBill Paul /* First, remove any existing filter entries. */ 505c0cb022bSYaroslav Tykhiy while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 506f731f104SBill Paul SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 5072d222cb7SAlexander Motin (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 5089d4fe4b2SBrooks Davis free(mc, M_VLAN); 509f731f104SBill Paul } 510f731f104SBill Paul 511f731f104SBill Paul /* Now program new ones. */ 5122d222cb7SAlexander Motin IF_ADDR_WLOCK(ifp); 5136817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 514f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 515f731f104SBill Paul continue; 51629c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 5172d222cb7SAlexander Motin if (mc == NULL) { 5182d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 51929c2dfbeSBruce M Simpson return (ENOMEM); 5202d222cb7SAlexander Motin } 521e4cd31ddSJeff Roberson bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 522e4cd31ddSJeff Roberson mc->mc_addr.sdl_index = ifp_p->if_index; 523f731f104SBill Paul SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 5242d222cb7SAlexander Motin } 5252d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 5262d222cb7SAlexander Motin SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) { 527e4cd31ddSJeff Roberson error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 5282d222cb7SAlexander Motin NULL); 529f731f104SBill Paul if (error) 530f731f104SBill Paul return (error); 531f731f104SBill Paul } 532f731f104SBill Paul 5338b615593SMarko Zec CURVNET_RESTORE(); 534f731f104SBill Paul return (0); 535f731f104SBill Paul } 5362cc2df49SGarrett Wollman 537a3814acfSSam Leffler /* 538ea4ca115SAndrew Thompson * A handler for parent interface link layer address changes. 539ea4ca115SAndrew Thompson * If the parent interface link layer address is changed we 540ea4ca115SAndrew Thompson * should also change it on all children vlans. 541ea4ca115SAndrew Thompson */ 542ea4ca115SAndrew Thompson static void 543ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 544ea4ca115SAndrew Thompson { 545ea4ca115SAndrew Thompson struct ifvlan *ifv; 5466117727bSAndrew Thompson #ifndef VLAN_ARRAY 5476117727bSAndrew Thompson struct ifvlan *next; 5486117727bSAndrew Thompson #endif 5498ad43f2dSAlexander V. Chernikov int i; 550ea4ca115SAndrew Thompson 551ea4ca115SAndrew Thompson /* 552ea4ca115SAndrew Thompson * Check if it's a trunk interface first of all 553ea4ca115SAndrew Thompson * to avoid needless locking. 554ea4ca115SAndrew Thompson */ 555ea4ca115SAndrew Thompson if (ifp->if_vlantrunk == NULL) 556ea4ca115SAndrew Thompson return; 557ea4ca115SAndrew Thompson 558ea4ca115SAndrew Thompson VLAN_LOCK(); 559ea4ca115SAndrew Thompson /* 560ea4ca115SAndrew Thompson * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 561ea4ca115SAndrew Thompson */ 562ea4ca115SAndrew Thompson #ifdef VLAN_ARRAY 563ea4ca115SAndrew Thompson for (i = 0; i < VLAN_ARRAY_SIZE; i++) 5646117727bSAndrew Thompson if ((ifv = ifp->if_vlantrunk->vlans[i])) { 565ea4ca115SAndrew Thompson #else /* VLAN_ARRAY */ 566ea4ca115SAndrew Thompson for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 5676117727bSAndrew Thompson LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) { 568ea4ca115SAndrew Thompson #endif /* VLAN_ARRAY */ 569ea4ca115SAndrew Thompson VLAN_UNLOCK(); 5708ad43f2dSAlexander V. Chernikov if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp), 571e4cd31ddSJeff Roberson ifp->if_addrlen); 5726117727bSAndrew Thompson VLAN_LOCK(); 5736117727bSAndrew Thompson } 5746117727bSAndrew Thompson VLAN_UNLOCK(); 575ea4ca115SAndrew Thompson 576ea4ca115SAndrew Thompson } 577ea4ca115SAndrew Thompson 578ea4ca115SAndrew Thompson /* 5795cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 5805cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 5815cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 5825428776eSJohn Baldwin * by ejecting its hot-plug card. However, if an ifnet is simply 5835428776eSJohn Baldwin * being renamed, then there's no need to tear down the state. 5845cb8c31aSYaroslav Tykhiy */ 5855cb8c31aSYaroslav Tykhiy static void 5865cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 5875cb8c31aSYaroslav Tykhiy { 5885cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 5895cb8c31aSYaroslav Tykhiy int i; 5905cb8c31aSYaroslav Tykhiy 5915cb8c31aSYaroslav Tykhiy /* 5925cb8c31aSYaroslav Tykhiy * Check if it's a trunk interface first of all 5935cb8c31aSYaroslav Tykhiy * to avoid needless locking. 5945cb8c31aSYaroslav Tykhiy */ 5955cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk == NULL) 5965cb8c31aSYaroslav Tykhiy return; 5975cb8c31aSYaroslav Tykhiy 5985428776eSJohn Baldwin /* If the ifnet is just being renamed, don't do anything. */ 5995428776eSJohn Baldwin if (ifp->if_flags & IFF_RENAMING) 6005428776eSJohn Baldwin return; 6015428776eSJohn Baldwin 6025cb8c31aSYaroslav Tykhiy VLAN_LOCK(); 6035cb8c31aSYaroslav Tykhiy /* 6045cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 6055cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 6065cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 6075cb8c31aSYaroslav Tykhiy */ 6085cb8c31aSYaroslav Tykhiy #ifdef VLAN_ARRAY 6095cb8c31aSYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 6105cb8c31aSYaroslav Tykhiy if ((ifv = ifp->if_vlantrunk->vlans[i])) { 61128cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 6125cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk == NULL) 6135cb8c31aSYaroslav Tykhiy break; 6145cb8c31aSYaroslav Tykhiy } 6155cb8c31aSYaroslav Tykhiy #else /* VLAN_ARRAY */ 6165cb8c31aSYaroslav Tykhiy restart: 6175cb8c31aSYaroslav Tykhiy for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 6185cb8c31aSYaroslav Tykhiy if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) { 61928cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 6205cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk) 6215cb8c31aSYaroslav Tykhiy goto restart; /* trunk->hwidth can change */ 6225cb8c31aSYaroslav Tykhiy else 6235cb8c31aSYaroslav Tykhiy break; 6245cb8c31aSYaroslav Tykhiy } 6255cb8c31aSYaroslav Tykhiy #endif /* VLAN_ARRAY */ 6265cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 6275cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 6285cb8c31aSYaroslav Tykhiy VLAN_UNLOCK(); 6295cb8c31aSYaroslav Tykhiy } 6305cb8c31aSYaroslav Tykhiy 6315cb8c31aSYaroslav Tykhiy /* 632e4cd31ddSJeff Roberson * Return the trunk device for a virtual interface. 633e4cd31ddSJeff Roberson */ 634e4cd31ddSJeff Roberson static struct ifnet * 635e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp) 636e4cd31ddSJeff Roberson { 637e4cd31ddSJeff Roberson struct ifvlan *ifv; 638e4cd31ddSJeff Roberson 639e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 640e4cd31ddSJeff Roberson return (NULL); 641e4cd31ddSJeff Roberson ifv = ifp->if_softc; 642e4cd31ddSJeff Roberson ifp = NULL; 643e4cd31ddSJeff Roberson VLAN_LOCK(); 644e4cd31ddSJeff Roberson if (ifv->ifv_trunk) 645e4cd31ddSJeff Roberson ifp = PARENT(ifv); 646e4cd31ddSJeff Roberson VLAN_UNLOCK(); 647e4cd31ddSJeff Roberson return (ifp); 648e4cd31ddSJeff Roberson } 649e4cd31ddSJeff Roberson 650e4cd31ddSJeff Roberson /* 6517983103aSRobert Watson * Return the 12-bit VLAN VID for this interface, for use by external 6527983103aSRobert Watson * components such as Infiniband. 6537983103aSRobert Watson * 6547983103aSRobert Watson * XXXRW: Note that the function name here is historical; it should be named 6557983103aSRobert Watson * vlan_vid(). 656e4cd31ddSJeff Roberson */ 657e4cd31ddSJeff Roberson static int 6587983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp) 659e4cd31ddSJeff Roberson { 660e4cd31ddSJeff Roberson struct ifvlan *ifv; 661e4cd31ddSJeff Roberson 662e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 663e4cd31ddSJeff Roberson return (EINVAL); 664e4cd31ddSJeff Roberson ifv = ifp->if_softc; 6657983103aSRobert Watson *vidp = ifv->ifv_vid; 666e4cd31ddSJeff Roberson return (0); 667e4cd31ddSJeff Roberson } 668e4cd31ddSJeff Roberson 669e4cd31ddSJeff Roberson /* 670e4cd31ddSJeff Roberson * Return a driver specific cookie for this interface. Synchronization 671e4cd31ddSJeff Roberson * with setcookie must be provided by the driver. 672e4cd31ddSJeff Roberson */ 673e4cd31ddSJeff Roberson static void * 674e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp) 675e4cd31ddSJeff Roberson { 676e4cd31ddSJeff Roberson struct ifvlan *ifv; 677e4cd31ddSJeff Roberson 678e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 679e4cd31ddSJeff Roberson return (NULL); 680e4cd31ddSJeff Roberson ifv = ifp->if_softc; 681e4cd31ddSJeff Roberson return (ifv->ifv_cookie); 682e4cd31ddSJeff Roberson } 683e4cd31ddSJeff Roberson 684e4cd31ddSJeff Roberson /* 685e4cd31ddSJeff Roberson * Store a cookie in our softc that drivers can use to store driver 686e4cd31ddSJeff Roberson * private per-instance data in. 687e4cd31ddSJeff Roberson */ 688e4cd31ddSJeff Roberson static int 689e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie) 690e4cd31ddSJeff Roberson { 691e4cd31ddSJeff Roberson struct ifvlan *ifv; 692e4cd31ddSJeff Roberson 693e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 694e4cd31ddSJeff Roberson return (EINVAL); 695e4cd31ddSJeff Roberson ifv = ifp->if_softc; 696e4cd31ddSJeff Roberson ifv->ifv_cookie = cookie; 697e4cd31ddSJeff Roberson return (0); 698e4cd31ddSJeff Roberson } 699e4cd31ddSJeff Roberson 700e4cd31ddSJeff Roberson /* 7017983103aSRobert Watson * Return the vlan device present at the specific VID. 702e4cd31ddSJeff Roberson */ 703e4cd31ddSJeff Roberson static struct ifnet * 7047983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid) 705e4cd31ddSJeff Roberson { 706e4cd31ddSJeff Roberson struct ifvlantrunk *trunk; 707e4cd31ddSJeff Roberson struct ifvlan *ifv; 708772b000fSAlexander V. Chernikov TRUNK_LOCK_READER; 709e4cd31ddSJeff Roberson 710e4cd31ddSJeff Roberson trunk = ifp->if_vlantrunk; 711e4cd31ddSJeff Roberson if (trunk == NULL) 712e4cd31ddSJeff Roberson return (NULL); 713e4cd31ddSJeff Roberson ifp = NULL; 714e4cd31ddSJeff Roberson TRUNK_RLOCK(trunk); 7157983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 716e4cd31ddSJeff Roberson if (ifv) 717e4cd31ddSJeff Roberson ifp = ifv->ifv_ifp; 718e4cd31ddSJeff Roberson TRUNK_RUNLOCK(trunk); 719e4cd31ddSJeff Roberson return (ifp); 720e4cd31ddSJeff Roberson } 721e4cd31ddSJeff Roberson 722e4cd31ddSJeff Roberson /* 7232ccbbd06SMarcelo Araujo * Recalculate the cached VLAN tag exposed via the MIB. 7242ccbbd06SMarcelo Araujo */ 7252ccbbd06SMarcelo Araujo static void 7262ccbbd06SMarcelo Araujo vlan_tag_recalculate(struct ifvlan *ifv) 7272ccbbd06SMarcelo Araujo { 7282ccbbd06SMarcelo Araujo 7292ccbbd06SMarcelo Araujo ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0); 7302ccbbd06SMarcelo Araujo } 7312ccbbd06SMarcelo Araujo 7322ccbbd06SMarcelo Araujo /* 733a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 734a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 735a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 736a3814acfSSam Leffler * set here. No one else in the system should be aware of this so 737a3814acfSSam Leffler * we use an explicit reference here. 738a3814acfSSam Leffler */ 739a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 740a3814acfSSam Leffler 741984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 742a6fffd6cSBrooks Davis extern void (*vlan_link_state_p)(struct ifnet *); 743127d7b2dSAndre Oppermann 7442b120974SPeter Wemm static int 7452b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 7462b120974SPeter Wemm { 7479d4fe4b2SBrooks Davis 7482b120974SPeter Wemm switch (type) { 7492b120974SPeter Wemm case MOD_LOAD: 7505cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 7515cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 7525cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 7535cb8c31aSYaroslav Tykhiy return (ENOMEM); 754ea4ca115SAndrew Thompson iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 755ea4ca115SAndrew Thompson vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 756ea4ca115SAndrew Thompson if (iflladdr_tag == NULL) 757ea4ca115SAndrew Thompson return (ENOMEM); 7584faedfe8SSam Leffler VLAN_LOCK_INIT(); 7599d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 760127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 76175ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 762e4cd31ddSJeff Roberson vlan_trunkdev_p = vlan_trunkdev; 763e4cd31ddSJeff Roberson vlan_cookie_p = vlan_cookie; 764e4cd31ddSJeff Roberson vlan_setcookie_p = vlan_setcookie; 765e4cd31ddSJeff Roberson vlan_tag_p = vlan_tag; 766e4cd31ddSJeff Roberson vlan_devat_p = vlan_devat; 767ccf7ba97SMarko Zec #ifndef VIMAGE 76842a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 76942a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 770ccf7ba97SMarko Zec #endif 77125c0f7b3SYaroslav Tykhiy if (bootverbose) 77225c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 77325c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 77425c0f7b3SYaroslav Tykhiy "full-size arrays" 77525c0f7b3SYaroslav Tykhiy #else 77625c0f7b3SYaroslav Tykhiy "hash tables with chaining" 77725c0f7b3SYaroslav Tykhiy #endif 77825c0f7b3SYaroslav Tykhiy 77925c0f7b3SYaroslav Tykhiy "\n"); 7802b120974SPeter Wemm break; 7812b120974SPeter Wemm case MOD_UNLOAD: 782ccf7ba97SMarko Zec #ifndef VIMAGE 78342a58907SGleb Smirnoff if_clone_detach(vlan_cloner); 784ccf7ba97SMarko Zec #endif 7855cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 786ea4ca115SAndrew Thompson EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 7879d4fe4b2SBrooks Davis vlan_input_p = NULL; 788127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 78975ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 790e4cd31ddSJeff Roberson vlan_trunkdev_p = NULL; 791e4cd31ddSJeff Roberson vlan_tag_p = NULL; 79209fe6320SNavdeep Parhar vlan_cookie_p = NULL; 79309fe6320SNavdeep Parhar vlan_setcookie_p = NULL; 794e4cd31ddSJeff Roberson vlan_devat_p = NULL; 7954faedfe8SSam Leffler VLAN_LOCK_DESTROY(); 79625c0f7b3SYaroslav Tykhiy if (bootverbose) 79725c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 7989d4fe4b2SBrooks Davis break; 7993e019deaSPoul-Henning Kamp default: 8003e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 8012b120974SPeter Wemm } 80215a66c21SBruce M Simpson return (0); 8032b120974SPeter Wemm } 8042b120974SPeter Wemm 8052b120974SPeter Wemm static moduledata_t vlan_mod = { 8062b120974SPeter Wemm "if_vlan", 8072b120974SPeter Wemm vlan_modevent, 8089823d527SKevin Lo 0 8092b120974SPeter Wemm }; 8102b120974SPeter Wemm 8112b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 81211edc477SEd Maste MODULE_VERSION(if_vlan, 3); 8132cc2df49SGarrett Wollman 814ccf7ba97SMarko Zec #ifdef VIMAGE 815ccf7ba97SMarko Zec static void 816ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused) 817ccf7ba97SMarko Zec { 818ccf7ba97SMarko Zec 81942a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 82042a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 821ccf7ba97SMarko Zec V_vlan_cloner = vlan_cloner; 822ccf7ba97SMarko Zec } 823ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 824ccf7ba97SMarko Zec vnet_vlan_init, NULL); 825ccf7ba97SMarko Zec 826ccf7ba97SMarko Zec static void 827ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused) 828ccf7ba97SMarko Zec { 829ccf7ba97SMarko Zec 83042a58907SGleb Smirnoff if_clone_detach(V_vlan_cloner); 831ccf7ba97SMarko Zec } 83289856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, 833ccf7ba97SMarko Zec vnet_vlan_uninit, NULL); 834ccf7ba97SMarko Zec #endif 835ccf7ba97SMarko Zec 836f941c31aSGleb Smirnoff /* 837f941c31aSGleb Smirnoff * Check for <etherif>.<vlan> style interface names. 838f941c31aSGleb Smirnoff */ 839f889d2efSBrooks Davis static struct ifnet * 840f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp) 8419d4fe4b2SBrooks Davis { 842f941c31aSGleb Smirnoff char ifname[IFNAMSIZ]; 843f941c31aSGleb Smirnoff char *cp; 844f889d2efSBrooks Davis struct ifnet *ifp; 8457983103aSRobert Watson int vid; 846f889d2efSBrooks Davis 847f941c31aSGleb Smirnoff strlcpy(ifname, name, IFNAMSIZ); 848f941c31aSGleb Smirnoff if ((cp = strchr(ifname, '.')) == NULL) 849f941c31aSGleb Smirnoff return (NULL); 850f941c31aSGleb Smirnoff *cp = '\0'; 851f941c31aSGleb Smirnoff if ((ifp = ifunit(ifname)) == NULL) 852f941c31aSGleb Smirnoff return (NULL); 853f941c31aSGleb Smirnoff /* Parse VID. */ 854f941c31aSGleb Smirnoff if (*++cp == '\0') 855f941c31aSGleb Smirnoff return (NULL); 8567983103aSRobert Watson vid = 0; 857fb92ad4aSJohn Baldwin for(; *cp >= '0' && *cp <= '9'; cp++) 8587983103aSRobert Watson vid = (vid * 10) + (*cp - '0'); 859fb92ad4aSJohn Baldwin if (*cp != '\0') 860f941c31aSGleb Smirnoff return (NULL); 8617983103aSRobert Watson if (vidp != NULL) 8627983103aSRobert Watson *vidp = vid; 863f889d2efSBrooks Davis 86415a66c21SBruce M Simpson return (ifp); 865f889d2efSBrooks Davis } 866f889d2efSBrooks Davis 867f889d2efSBrooks Davis static int 868f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 869f889d2efSBrooks Davis { 870f889d2efSBrooks Davis const char *cp; 871f889d2efSBrooks Davis 872f941c31aSGleb Smirnoff if (vlan_clone_match_ethervid(name, NULL) != NULL) 873f889d2efSBrooks Davis return (1); 874f889d2efSBrooks Davis 87542a58907SGleb Smirnoff if (strncmp(vlanname, name, strlen(vlanname)) != 0) 876f889d2efSBrooks Davis return (0); 877f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 878f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 879f889d2efSBrooks Davis return (0); 880f889d2efSBrooks Davis } 881f889d2efSBrooks Davis 882f889d2efSBrooks Davis return (1); 883f889d2efSBrooks Davis } 884f889d2efSBrooks Davis 885f889d2efSBrooks Davis static int 8866b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 887f889d2efSBrooks Davis { 888f889d2efSBrooks Davis char *dp; 889f889d2efSBrooks Davis int wildcard; 890f889d2efSBrooks Davis int unit; 891f889d2efSBrooks Davis int error; 8927983103aSRobert Watson int vid; 893f889d2efSBrooks Davis int ethertag; 8949d4fe4b2SBrooks Davis struct ifvlan *ifv; 8959d4fe4b2SBrooks Davis struct ifnet *ifp; 896f889d2efSBrooks Davis struct ifnet *p; 8973ba24fdeSJohn Baldwin struct ifaddr *ifa; 8983ba24fdeSJohn Baldwin struct sockaddr_dl *sdl; 8996b7330e2SSam Leffler struct vlanreq vlr; 90065239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 901f889d2efSBrooks Davis 9026b7330e2SSam Leffler /* 9036b7330e2SSam Leffler * There are 3 (ugh) ways to specify the cloned device: 9046b7330e2SSam Leffler * o pass a parameter block with the clone request. 9056b7330e2SSam Leffler * o specify parameters in the text of the clone device name 9066b7330e2SSam Leffler * o specify no parameters and get an unattached device that 9076b7330e2SSam Leffler * must be configured separately. 9086b7330e2SSam Leffler * The first technique is preferred; the latter two are 909a4641f4eSPedro F. Giffuni * supported for backwards compatibility. 9107983103aSRobert Watson * 9117983103aSRobert Watson * XXXRW: Note historic use of the word "tag" here. New ioctls may be 9127983103aSRobert Watson * called for. 9136b7330e2SSam Leffler */ 9146b7330e2SSam Leffler if (params) { 9156b7330e2SSam Leffler error = copyin(params, &vlr, sizeof(vlr)); 9166b7330e2SSam Leffler if (error) 9176b7330e2SSam Leffler return error; 9186b7330e2SSam Leffler p = ifunit(vlr.vlr_parent); 9196b7330e2SSam Leffler if (p == NULL) 920b1828acfSGleb Smirnoff return (ENXIO); 9216b7330e2SSam Leffler error = ifc_name2unit(name, &unit); 9226b7330e2SSam Leffler if (error != 0) 9236b7330e2SSam Leffler return (error); 9246b7330e2SSam Leffler 9256b7330e2SSam Leffler ethertag = 1; 9267983103aSRobert Watson vid = vlr.vlr_tag; 9276b7330e2SSam Leffler wildcard = (unit < 0); 928f941c31aSGleb Smirnoff } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) { 929f889d2efSBrooks Davis ethertag = 1; 930f889d2efSBrooks Davis unit = -1; 931f889d2efSBrooks Davis wildcard = 0; 932f889d2efSBrooks Davis } else { 933f889d2efSBrooks Davis ethertag = 0; 934f889d2efSBrooks Davis 935f889d2efSBrooks Davis error = ifc_name2unit(name, &unit); 936f889d2efSBrooks Davis if (error != 0) 937f889d2efSBrooks Davis return (error); 938f889d2efSBrooks Davis 939f889d2efSBrooks Davis wildcard = (unit < 0); 940f889d2efSBrooks Davis } 941f889d2efSBrooks Davis 942f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 943f889d2efSBrooks Davis if (error != 0) 944f889d2efSBrooks Davis return (error); 945f889d2efSBrooks Davis 946f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 947f889d2efSBrooks Davis if (wildcard) { 948f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 949f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 950f889d2efSBrooks Davis len - (dp-name) - 1) { 951f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 952f889d2efSBrooks Davis } 953f889d2efSBrooks Davis } 9549d4fe4b2SBrooks Davis 955a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 956fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 957fc74a9f9SBrooks Davis if (ifp == NULL) { 958fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 959fc74a9f9SBrooks Davis free(ifv, M_VLAN); 960fc74a9f9SBrooks Davis return (ENOSPC); 961fc74a9f9SBrooks Davis } 9629d4fe4b2SBrooks Davis SLIST_INIT(&ifv->vlan_mc_listhead); 9639d4fe4b2SBrooks Davis ifp->if_softc = ifv; 964f889d2efSBrooks Davis /* 965cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 966f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 967f889d2efSBrooks Davis */ 968f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 96942a58907SGleb Smirnoff ifp->if_dname = vlanname; 970f889d2efSBrooks Davis ifp->if_dunit = unit; 9719d4fe4b2SBrooks Davis /* NB: flags are not set here */ 9729d4fe4b2SBrooks Davis ifp->if_linkmib = &ifv->ifv_mib; 97315a66c21SBruce M Simpson ifp->if_linkmiblen = sizeof(ifv->ifv_mib); 9749d4fe4b2SBrooks Davis /* NB: mtu is not set here */ 9759d4fe4b2SBrooks Davis 976114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 977d9b1d615SJohn Baldwin ifp->if_transmit = vlan_transmit; 978d9b1d615SJohn Baldwin ifp->if_qflush = vlan_qflush; 9799d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 980f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 981f3e7afe2SHans Petter Selasky ifp->if_snd_tag_alloc = vlan_snd_tag_alloc; 982f3e7afe2SHans Petter Selasky #endif 98364a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 984fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 9859d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 986211f625aSBill Fenner ifp->if_baudrate = 0; 987a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 988a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 9893ba24fdeSJohn Baldwin ifa = ifp->if_addr; 9903ba24fdeSJohn Baldwin sdl = (struct sockaddr_dl *)ifa->ifa_addr; 9913ba24fdeSJohn Baldwin sdl->sdl_type = IFT_L2VLAN; 9929d4fe4b2SBrooks Davis 993f889d2efSBrooks Davis if (ethertag) { 9947983103aSRobert Watson error = vlan_config(ifv, p, vid); 995f889d2efSBrooks Davis if (error != 0) { 996f889d2efSBrooks Davis /* 99728cc4d37SJohn Baldwin * Since we've partially failed, we need to back 998f889d2efSBrooks Davis * out all the way, otherwise userland could get 999f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 1000f889d2efSBrooks Davis */ 1001f889d2efSBrooks Davis ether_ifdetach(ifp); 1002249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 10034b22573aSBrooks Davis if_free(ifp); 100496c8ef3aSMaxim Konovalov ifc_free_unit(ifc, unit); 1005f889d2efSBrooks Davis free(ifv, M_VLAN); 1006f889d2efSBrooks Davis 1007f889d2efSBrooks Davis return (error); 1008f889d2efSBrooks Davis } 1009f889d2efSBrooks Davis 10101cf236fbSYaroslav Tykhiy /* Update flags on the parent, if necessary. */ 10111cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 1); 1012f889d2efSBrooks Davis } 1013f889d2efSBrooks Davis 10149d4fe4b2SBrooks Davis return (0); 10159d4fe4b2SBrooks Davis } 10169d4fe4b2SBrooks Davis 1017f889d2efSBrooks Davis static int 1018f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 10199d4fe4b2SBrooks Davis { 10209d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 1021114c608cSYaroslav Tykhiy int unit = ifp->if_dunit; 1022b4e9f837SBrooks Davis 1023249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 1024249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 10254b22573aSBrooks Davis if_free(ifp); 10269d4fe4b2SBrooks Davis free(ifv, M_VLAN); 1027b4e9f837SBrooks Davis ifc_free_unit(ifc, unit); 1028b4e9f837SBrooks Davis 1029f889d2efSBrooks Davis return (0); 10309d4fe4b2SBrooks Davis } 10319d4fe4b2SBrooks Davis 103215a66c21SBruce M Simpson /* 103315a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 103415a66c21SBruce M Simpson */ 10352cc2df49SGarrett Wollman static void 1036114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 10372cc2df49SGarrett Wollman { 10382cc2df49SGarrett Wollman } 10392cc2df49SGarrett Wollman 10406d3a3ab7SGleb Smirnoff /* 1041d9b1d615SJohn Baldwin * The if_transmit method for vlan(4) interface. 10426d3a3ab7SGleb Smirnoff */ 1043d9b1d615SJohn Baldwin static int 1044d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m) 10452cc2df49SGarrett Wollman { 10462cc2df49SGarrett Wollman struct ifvlan *ifv; 10472cc2df49SGarrett Wollman struct ifnet *p; 10482ccbbd06SMarcelo Araujo struct m_tag *mtag; 10492ccbbd06SMarcelo Araujo uint16_t tag; 10501ad7a257SPyun YongHyeon int error, len, mcast; 10512cc2df49SGarrett Wollman 10522cc2df49SGarrett Wollman ifv = ifp->if_softc; 105375ee267cSGleb Smirnoff p = PARENT(ifv); 10541ad7a257SPyun YongHyeon len = m->m_pkthdr.len; 10551ad7a257SPyun YongHyeon mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 10562cc2df49SGarrett Wollman 1057a3814acfSSam Leffler BPF_MTAP(ifp, m); 10582cc2df49SGarrett Wollman 1059f731f104SBill Paul /* 1060d9b1d615SJohn Baldwin * Do not run parent's if_transmit() if the parent is not up, 106124993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 106224993214SYaroslav Tykhiy */ 10632dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 106424993214SYaroslav Tykhiy m_freem(m); 1065a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 10668b20f6cfSHiroki Sato return (ENETDOWN); 106724993214SYaroslav Tykhiy } 106824993214SYaroslav Tykhiy 106924993214SYaroslav Tykhiy /* 1070f6e5e0adSYaroslav Tykhiy * Pad the frame to the minimum size allowed if told to. 1071f6e5e0adSYaroslav Tykhiy * This option is in accord with IEEE Std 802.1Q, 2003 Ed., 1072f6e5e0adSYaroslav Tykhiy * paragraph C.4.4.3.b. It can help to work around buggy 1073f6e5e0adSYaroslav Tykhiy * bridges that violate paragraph C.4.4.3.a from the same 1074f6e5e0adSYaroslav Tykhiy * document, i.e., fail to pad short frames after untagging. 1075f6e5e0adSYaroslav Tykhiy * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but 1076f6e5e0adSYaroslav Tykhiy * untagging it will produce a 62-byte frame, which is a runt 1077f6e5e0adSYaroslav Tykhiy * and requires padding. There are VLAN-enabled network 1078f6e5e0adSYaroslav Tykhiy * devices that just discard such runts instead or mishandle 1079f6e5e0adSYaroslav Tykhiy * them somehow. 1080f6e5e0adSYaroslav Tykhiy */ 1081478e0520SHiroki Sato if (V_soft_pad && p->if_type == IFT_ETHER) { 1082f6e5e0adSYaroslav Tykhiy static char pad[8]; /* just zeros */ 1083f6e5e0adSYaroslav Tykhiy int n; 1084f6e5e0adSYaroslav Tykhiy 1085f6e5e0adSYaroslav Tykhiy for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len; 1086f6e5e0adSYaroslav Tykhiy n > 0; n -= sizeof(pad)) 1087f6e5e0adSYaroslav Tykhiy if (!m_append(m, min(n, sizeof(pad)), pad)) 1088f6e5e0adSYaroslav Tykhiy break; 1089f6e5e0adSYaroslav Tykhiy 1090f6e5e0adSYaroslav Tykhiy if (n > 0) { 1091f6e5e0adSYaroslav Tykhiy if_printf(ifp, "cannot pad short frame\n"); 1092a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1093f6e5e0adSYaroslav Tykhiy m_freem(m); 1094d9b1d615SJohn Baldwin return (0); 1095f6e5e0adSYaroslav Tykhiy } 1096f6e5e0adSYaroslav Tykhiy } 1097f6e5e0adSYaroslav Tykhiy 1098f6e5e0adSYaroslav Tykhiy /* 1099a3814acfSSam Leffler * If underlying interface can do VLAN tag insertion itself, 1100a3814acfSSam Leffler * just pass the packet along. However, we need some way to 1101a3814acfSSam Leffler * tell the interface where the packet came from so that it 1102a3814acfSSam Leffler * knows how to find the VLAN tag to use, so we attach a 1103a3814acfSSam Leffler * packet tag that holds it. 1104f731f104SBill Paul */ 11052ccbbd06SMarcelo Araujo if (vlan_mtag_pcp && (mtag = m_tag_locate(m, MTAG_8021Q, 11062ccbbd06SMarcelo Araujo MTAG_8021Q_PCP_OUT, NULL)) != NULL) 11072ccbbd06SMarcelo Araujo tag = EVL_MAKETAG(ifv->ifv_vid, *(uint8_t *)(mtag + 1), 0); 11082ccbbd06SMarcelo Araujo else 11092ccbbd06SMarcelo Araujo tag = ifv->ifv_tag; 1110b08347a0SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_HWTAGGING) { 11112ccbbd06SMarcelo Araujo m->m_pkthdr.ether_vtag = tag; 1112ba26134bSGleb Smirnoff m->m_flags |= M_VLANTAG; 1113f731f104SBill Paul } else { 11142ccbbd06SMarcelo Araujo m = ether_vlanencap(m, tag); 11154af90a4dSMatthew N. Dodd if (m == NULL) { 1116d9b1d615SJohn Baldwin if_printf(ifp, "unable to prepend VLAN header\n"); 1117a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1118d9b1d615SJohn Baldwin return (0); 11194af90a4dSMatthew N. Dodd } 1120f731f104SBill Paul } 11212cc2df49SGarrett Wollman 11222cc2df49SGarrett Wollman /* 11232cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 11242cc2df49SGarrett Wollman */ 1125aea78d20SKip Macy error = (p->if_transmit)(p, m); 1126299153b5SAlexander V. Chernikov if (error == 0) { 1127a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1128a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 1129a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast); 11301ad7a257SPyun YongHyeon } else 1131a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1132d9b1d615SJohn Baldwin return (error); 11332cc2df49SGarrett Wollman } 1134d9b1d615SJohn Baldwin 1135d9b1d615SJohn Baldwin /* 1136d9b1d615SJohn Baldwin * The ifp->if_qflush entry point for vlan(4) is a no-op. 1137d9b1d615SJohn Baldwin */ 1138d9b1d615SJohn Baldwin static void 1139d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused) 1140d9b1d615SJohn Baldwin { 1141f731f104SBill Paul } 1142f731f104SBill Paul 1143a3814acfSSam Leffler static void 1144a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 1145f731f104SBill Paul { 114675ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1147f731f104SBill Paul struct ifvlan *ifv; 1148772b000fSAlexander V. Chernikov TRUNK_LOCK_READER; 11492ccbbd06SMarcelo Araujo struct m_tag *mtag; 11502ccbbd06SMarcelo Araujo uint16_t vid, tag; 115175ee267cSGleb Smirnoff 115275ee267cSGleb Smirnoff KASSERT(trunk != NULL, ("%s: no trunk", __func__)); 1153a3814acfSSam Leffler 1154f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 1155a3814acfSSam Leffler /* 115614e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 1157a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 1158a3814acfSSam Leffler */ 11592ccbbd06SMarcelo Araujo tag = m->m_pkthdr.ether_vtag; 11606ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 1161a3814acfSSam Leffler } else { 116275ee267cSGleb Smirnoff struct ether_vlan_header *evl; 116375ee267cSGleb Smirnoff 116414e98256SYaroslav Tykhiy /* 116514e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 116614e98256SYaroslav Tykhiy */ 1167a3814acfSSam Leffler switch (ifp->if_type) { 1168a3814acfSSam Leffler case IFT_ETHER: 1169a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 1170a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 1171a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 1172a3814acfSSam Leffler return; 1173a3814acfSSam Leffler } 1174a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 11752ccbbd06SMarcelo Araujo tag = ntohs(evl->evl_tag); 1176db8b5973SYaroslav Tykhiy 1177db8b5973SYaroslav Tykhiy /* 11782dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 11792dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 11802dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 11812dc879b3SYaroslav Tykhiy * type field is already in place. 1182db8b5973SYaroslav Tykhiy */ 11832dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 11842dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 11852dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 1186a3814acfSSam Leffler break; 11872dc879b3SYaroslav Tykhiy 1188a3814acfSSam Leffler default: 1189db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 119060c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 119160c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 1192a3814acfSSam Leffler #endif 119360c60618SYaroslav Tykhiy m_freem(m); 11943751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 119560c60618SYaroslav Tykhiy return; 1196a3814acfSSam Leffler } 11977a46ec8fSBrooks Davis } 11987a46ec8fSBrooks Davis 11992ccbbd06SMarcelo Araujo vid = EVL_VLANOFTAG(tag); 12002ccbbd06SMarcelo Araujo 120115ed2fa1SYaroslav Tykhiy TRUNK_RLOCK(trunk); 12027983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 12032dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 120475ee267cSGleb Smirnoff TRUNK_RUNLOCK(trunk); 120575ee267cSGleb Smirnoff m_freem(m); 12063751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 120775ee267cSGleb Smirnoff return; 120875ee267cSGleb Smirnoff } 120975ee267cSGleb Smirnoff TRUNK_RUNLOCK(trunk); 1210f731f104SBill Paul 12112ccbbd06SMarcelo Araujo if (vlan_mtag_pcp) { 12122ccbbd06SMarcelo Araujo /* 12132ccbbd06SMarcelo Araujo * While uncommon, it is possible that we will find a 802.1q 12142ccbbd06SMarcelo Araujo * packet encapsulated inside another packet that also had an 12152ccbbd06SMarcelo Araujo * 802.1q header. For example, ethernet tunneled over IPSEC 12162ccbbd06SMarcelo Araujo * arriving over ethernet. In that case, we replace the 12172ccbbd06SMarcelo Araujo * existing 802.1q PCP m_tag value. 12182ccbbd06SMarcelo Araujo */ 12192ccbbd06SMarcelo Araujo mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 12202ccbbd06SMarcelo Araujo if (mtag == NULL) { 12212ccbbd06SMarcelo Araujo mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN, 12222ccbbd06SMarcelo Araujo sizeof(uint8_t), M_NOWAIT); 12232ccbbd06SMarcelo Araujo if (mtag == NULL) { 12242ccbbd06SMarcelo Araujo m_freem(m); 12252ccbbd06SMarcelo Araujo if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 12262ccbbd06SMarcelo Araujo return; 12272ccbbd06SMarcelo Araujo } 12282ccbbd06SMarcelo Araujo m_tag_prepend(m, mtag); 12292ccbbd06SMarcelo Araujo } 12302ccbbd06SMarcelo Araujo *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag); 12312ccbbd06SMarcelo Araujo } 12322ccbbd06SMarcelo Araujo 1233fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 1234c0304424SGleb Smirnoff if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1); 12352cc2df49SGarrett Wollman 1236a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 1237fc74a9f9SBrooks Davis (*ifp->if_input)(ifv->ifv_ifp, m); 12382cc2df49SGarrett Wollman } 12392cc2df49SGarrett Wollman 12402cc2df49SGarrett Wollman static int 12417983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid) 12422cc2df49SGarrett Wollman { 124375ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 12441cf236fbSYaroslav Tykhiy struct ifnet *ifp; 124575ee267cSGleb Smirnoff int error = 0; 12462cc2df49SGarrett Wollman 1247b1828acfSGleb Smirnoff /* 1248b1828acfSGleb Smirnoff * We can handle non-ethernet hardware types as long as 1249b1828acfSGleb Smirnoff * they handle the tagging and headers themselves. 1250b1828acfSGleb Smirnoff */ 1251e4cd31ddSJeff Roberson if (p->if_type != IFT_ETHER && 1252e4cd31ddSJeff Roberson (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 125315a66c21SBruce M Simpson return (EPROTONOSUPPORT); 125464a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 125564a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 1256b1828acfSGleb Smirnoff /* 1257b1828acfSGleb Smirnoff * Don't let the caller set up a VLAN VID with 1258b1828acfSGleb Smirnoff * anything except VLID bits. 1259b1828acfSGleb Smirnoff * VID numbers 0x0 and 0xFFF are reserved. 1260b1828acfSGleb Smirnoff */ 1261b1828acfSGleb Smirnoff if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK)) 1262b1828acfSGleb Smirnoff return (EINVAL); 126375ee267cSGleb Smirnoff if (ifv->ifv_trunk) 126415a66c21SBruce M Simpson return (EBUSY); 12652cc2df49SGarrett Wollman 126675ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 126775ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 126875ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 126975ee267cSGleb Smirnoff vlan_inithash(trunk); 127005a2398fSGleb Smirnoff VLAN_LOCK(); 127105a2398fSGleb Smirnoff if (p->if_vlantrunk != NULL) { 12722ccbbd06SMarcelo Araujo /* A race that is very unlikely to be hit. */ 127305a2398fSGleb Smirnoff vlan_freehash(trunk); 127405a2398fSGleb Smirnoff free(trunk, M_VLAN); 127505a2398fSGleb Smirnoff goto exists; 127605a2398fSGleb Smirnoff } 127775ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 127875ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 127975ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 128075ee267cSGleb Smirnoff trunk->parent = p; 128175ee267cSGleb Smirnoff } else { 128275ee267cSGleb Smirnoff VLAN_LOCK(); 128375ee267cSGleb Smirnoff exists: 128475ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 128575ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 128675ee267cSGleb Smirnoff } 128775ee267cSGleb Smirnoff 12887983103aSRobert Watson ifv->ifv_vid = vid; /* must set this before vlan_inshash() */ 12892ccbbd06SMarcelo Araujo ifv->ifv_pcp = 0; /* Default: best effort delivery. */ 12902ccbbd06SMarcelo Araujo vlan_tag_recalculate(ifv); 129175ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 129275ee267cSGleb Smirnoff if (error) 129375ee267cSGleb Smirnoff goto done; 129473f2233dSYaroslav Tykhiy ifv->ifv_proto = ETHERTYPE_VLAN; 1295a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1296a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 12971cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1298d89baa5aSAlexander Motin ifv->ifv_capenable = -1; 1299a3814acfSSam Leffler 1300a3814acfSSam Leffler /* 1301a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1302a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1303656acce4SYaroslav Tykhiy * use it. 1304a3814acfSSam Leffler */ 1305656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1306656acce4SYaroslav Tykhiy /* 1307656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1308656acce4SYaroslav Tykhiy * handle extended frames. 1309656acce4SYaroslav Tykhiy */ 1310a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1311656acce4SYaroslav Tykhiy } else { 1312a3814acfSSam Leffler /* 1313a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1314a3814acfSSam Leffler * makes us incompatible with strictly compliant 1315a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1316a3814acfSSam Leffler * the feature with other NetBSD implementations, 1317a3814acfSSam Leffler * which might still be useful. 1318a3814acfSSam Leffler */ 1319a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1320a3814acfSSam Leffler } 1321a3814acfSSam Leffler 132275ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 13231cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 1324e4cd31ddSJeff Roberson /* 1325e4cd31ddSJeff Roberson * Initialize fields from our parent. This duplicates some 1326e4cd31ddSJeff Roberson * work with ether_ifattach() but allows for non-ethernet 1327e4cd31ddSJeff Roberson * interfaces to also work. 1328e4cd31ddSJeff Roberson */ 13291cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 133075ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 1331e4cd31ddSJeff Roberson ifp->if_output = p->if_output; 1332e4cd31ddSJeff Roberson ifp->if_input = p->if_input; 1333e4cd31ddSJeff Roberson ifp->if_resolvemulti = p->if_resolvemulti; 1334e4cd31ddSJeff Roberson ifp->if_addrlen = p->if_addrlen; 1335e4cd31ddSJeff Roberson ifp->if_broadcastaddr = p->if_broadcastaddr; 1336e4cd31ddSJeff Roberson 13372cc2df49SGarrett Wollman /* 133824993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 133924993214SYaroslav Tykhiy * Other flags are none of our business. 13402cc2df49SGarrett Wollman */ 134164a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 13421cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 13431cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 13441cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 13451cf236fbSYaroslav Tykhiy 13461cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 13472cc2df49SGarrett Wollman 134875ee267cSGleb Smirnoff vlan_capabilities(ifv); 1349a3814acfSSam Leffler 1350a3814acfSSam Leffler /* 1351e4cd31ddSJeff Roberson * Set up our interface address to reflect the underlying 13522cc2df49SGarrett Wollman * physical interface's. 13532cc2df49SGarrett Wollman */ 1354e4cd31ddSJeff Roberson bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1355e4cd31ddSJeff Roberson ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1356e4cd31ddSJeff Roberson p->if_addrlen; 13571b2a4f7aSBill Fenner 13581b2a4f7aSBill Fenner /* 13591b2a4f7aSBill Fenner * Configure multicast addresses that may already be 13601b2a4f7aSBill Fenner * joined on the vlan device. 13611b2a4f7aSBill Fenner */ 13621cf236fbSYaroslav Tykhiy (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */ 13632ada9747SYaroslav Tykhiy 13642ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 13652ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 136675ee267cSGleb Smirnoff done: 136775ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 1368c725524cSJack F Vogel if (error == 0) 13697983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid); 137075ee267cSGleb Smirnoff VLAN_UNLOCK(); 137175ee267cSGleb Smirnoff 137275ee267cSGleb Smirnoff return (error); 13732cc2df49SGarrett Wollman } 13742cc2df49SGarrett Wollman 13756f359e28SJohn Baldwin static void 1376f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1377f731f104SBill Paul { 13785cb8c31aSYaroslav Tykhiy 13795cb8c31aSYaroslav Tykhiy VLAN_LOCK(); 138028cc4d37SJohn Baldwin vlan_unconfig_locked(ifp, 0); 13815cb8c31aSYaroslav Tykhiy VLAN_UNLOCK(); 13825cb8c31aSYaroslav Tykhiy } 13835cb8c31aSYaroslav Tykhiy 13846f359e28SJohn Baldwin static void 138528cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing) 13865cb8c31aSYaroslav Tykhiy { 138775ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1388f731f104SBill Paul struct vlan_mc_entry *mc; 1389f731f104SBill Paul struct ifvlan *ifv; 1390c725524cSJack F Vogel struct ifnet *parent; 139128cc4d37SJohn Baldwin int error; 1392f731f104SBill Paul 13935cb8c31aSYaroslav Tykhiy VLAN_LOCK_ASSERT(); 13944faedfe8SSam Leffler 1395f731f104SBill Paul ifv = ifp->if_softc; 139675ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 139722893351SJack F Vogel parent = NULL; 1398f731f104SBill Paul 139922893351SJack F Vogel if (trunk != NULL) { 140075ee267cSGleb Smirnoff 140175ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 140222893351SJack F Vogel parent = trunk->parent; 14031b2a4f7aSBill Fenner 1404f731f104SBill Paul /* 1405f731f104SBill Paul * Since the interface is being unconfigured, we need to 1406f731f104SBill Paul * empty the list of multicast groups that we may have joined 14071b2a4f7aSBill Fenner * while we were alive from the parent's list. 1408f731f104SBill Paul */ 1409c0cb022bSYaroslav Tykhiy while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 14106f359e28SJohn Baldwin /* 141128cc4d37SJohn Baldwin * If the parent interface is being detached, 1412b90dde2fSJohn Baldwin * all its multicast addresses have already 141328cc4d37SJohn Baldwin * been removed. Warn about errors if 141428cc4d37SJohn Baldwin * if_delmulti() does fail, but don't abort as 141528cc4d37SJohn Baldwin * all callers expect vlan destruction to 141628cc4d37SJohn Baldwin * succeed. 14176f359e28SJohn Baldwin */ 141828cc4d37SJohn Baldwin if (!departing) { 141928cc4d37SJohn Baldwin error = if_delmulti(parent, 1420e4cd31ddSJeff Roberson (struct sockaddr *)&mc->mc_addr); 142128cc4d37SJohn Baldwin if (error) 142228cc4d37SJohn Baldwin if_printf(ifp, 142328cc4d37SJohn Baldwin "Failed to delete multicast address from parent: %d\n", 142428cc4d37SJohn Baldwin error); 142528cc4d37SJohn Baldwin } 1426f731f104SBill Paul SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 14279d4fe4b2SBrooks Davis free(mc, M_VLAN); 1428f731f104SBill Paul } 1429a3814acfSSam Leffler 14301cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 143175ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 143275ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 143375ee267cSGleb Smirnoff 143475ee267cSGleb Smirnoff /* 143575ee267cSGleb Smirnoff * Check if we were the last. 143675ee267cSGleb Smirnoff */ 143775ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 14382d222cb7SAlexander Motin parent->if_vlantrunk = NULL; 143975ee267cSGleb Smirnoff /* 144075ee267cSGleb Smirnoff * XXXGL: If some ithread has already entered 144175ee267cSGleb Smirnoff * vlan_input() and is now blocked on the trunk 144275ee267cSGleb Smirnoff * lock, then it should preempt us right after 144375ee267cSGleb Smirnoff * unlock and finish its work. Then we will acquire 144475ee267cSGleb Smirnoff * lock again in trunk_destroy(). 144575ee267cSGleb Smirnoff */ 144675ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 144775ee267cSGleb Smirnoff trunk_destroy(trunk); 144875ee267cSGleb Smirnoff } else 144975ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 14501b2a4f7aSBill Fenner } 1451f731f104SBill Paul 1452f731f104SBill Paul /* Disconnect from parent. */ 14531cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 14541cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 14555cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 14565cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 14575cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1458f731f104SBill Paul 145922893351SJack F Vogel /* 146022893351SJack F Vogel * Only dispatch an event if vlan was 146122893351SJack F Vogel * attached, otherwise there is nothing 146222893351SJack F Vogel * to cleanup anyway. 146322893351SJack F Vogel */ 146422893351SJack F Vogel if (parent != NULL) 14657983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid); 1466f731f104SBill Paul } 1467f731f104SBill Paul 14681cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1469f731f104SBill Paul static int 14701cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 14711cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1472a3814acfSSam Leffler { 14731cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 14741cf236fbSYaroslav Tykhiy int error; 1475a3814acfSSam Leffler 14761cf236fbSYaroslav Tykhiy /* XXX VLAN_LOCK_ASSERT(); */ 1477a3814acfSSam Leffler 14781cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 14791cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 14801cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 14811cf236fbSYaroslav Tykhiy 14821cf236fbSYaroslav Tykhiy /* 14831cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 14841cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 14851cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 14861cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 14871cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 14881cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 14891cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 14901cf236fbSYaroslav Tykhiy */ 14911cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 149275ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 14931cf236fbSYaroslav Tykhiy if (error) 1494a3814acfSSam Leffler return (error); 14951cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 14961cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 14971cf236fbSYaroslav Tykhiy } 14981cf236fbSYaroslav Tykhiy return (0); 14991cf236fbSYaroslav Tykhiy } 15001cf236fbSYaroslav Tykhiy 15011cf236fbSYaroslav Tykhiy /* 15021cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 15031cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 15041cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 15051cf236fbSYaroslav Tykhiy */ 15061cf236fbSYaroslav Tykhiy static int 15071cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 15081cf236fbSYaroslav Tykhiy { 15091cf236fbSYaroslav Tykhiy int error, i; 15101cf236fbSYaroslav Tykhiy 15111cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 15121cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 15131cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 15141cf236fbSYaroslav Tykhiy if (error) 15151cf236fbSYaroslav Tykhiy return (error); 15161cf236fbSYaroslav Tykhiy } 15171cf236fbSYaroslav Tykhiy return (0); 1518a3814acfSSam Leffler } 1519a3814acfSSam Leffler 1520127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1521127d7b2dSAndre Oppermann static void 1522a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp) 1523127d7b2dSAndre Oppermann { 152475ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1525127d7b2dSAndre Oppermann struct ifvlan *ifv; 152675ee267cSGleb Smirnoff int i; 1527127d7b2dSAndre Oppermann 152875ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 152975ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 153015ed2fa1SYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 153175ee267cSGleb Smirnoff if (trunk->vlans[i] != NULL) { 153275ee267cSGleb Smirnoff ifv = trunk->vlans[i]; 153375ee267cSGleb Smirnoff #else 1534aad0be7aSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 1535aad0be7aSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) { 153675ee267cSGleb Smirnoff #endif 1537aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1538fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 153975ee267cSGleb Smirnoff trunk->parent->if_link_state); 1540127d7b2dSAndre Oppermann } 154175ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 154275ee267cSGleb Smirnoff } 154375ee267cSGleb Smirnoff 154475ee267cSGleb Smirnoff static void 154575ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 154675ee267cSGleb Smirnoff { 154775ee267cSGleb Smirnoff struct ifnet *p = PARENT(ifv); 154875ee267cSGleb Smirnoff struct ifnet *ifp = ifv->ifv_ifp; 15499fd573c3SHans Petter Selasky struct ifnet_hw_tsomax hw_tsomax; 1550d89baa5aSAlexander Motin int cap = 0, ena = 0, mena; 1551d89baa5aSAlexander Motin u_long hwa = 0; 155275ee267cSGleb Smirnoff 155375ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(TRUNK(ifv)); 155475ee267cSGleb Smirnoff 1555d89baa5aSAlexander Motin /* Mask parent interface enabled capabilities disabled by user. */ 1556d89baa5aSAlexander Motin mena = p->if_capenable & ifv->ifv_capenable; 1557d89baa5aSAlexander Motin 155875ee267cSGleb Smirnoff /* 155975ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 156075ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 156175ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 156275ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 156375ee267cSGleb Smirnoff */ 156475ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1565d89baa5aSAlexander Motin cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 156675ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 156775ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1568d89baa5aSAlexander Motin ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 1569d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM) 1570d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP | 1571d89baa5aSAlexander Motin CSUM_UDP | CSUM_SCTP); 1572d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM_IPV6) 1573d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_TCP_IPV6 | 1574d89baa5aSAlexander Motin CSUM_UDP_IPV6 | CSUM_SCTP_IPV6); 157575ee267cSGleb Smirnoff } 1576d89baa5aSAlexander Motin 15779b76d9cbSPyun YongHyeon /* 15789b76d9cbSPyun YongHyeon * If the parent interface can do TSO on VLANs then 15799b76d9cbSPyun YongHyeon * propagate the hardware-assisted flag. TSO on VLANs 15809b76d9cbSPyun YongHyeon * does not necessarily require hardware VLAN tagging. 15819b76d9cbSPyun YongHyeon */ 15829fd573c3SHans Petter Selasky memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 15839fd573c3SHans Petter Selasky if_hw_tsomax_common(p, &hw_tsomax); 15849fd573c3SHans Petter Selasky if_hw_tsomax_update(ifp, &hw_tsomax); 15859b76d9cbSPyun YongHyeon if (p->if_capabilities & IFCAP_VLAN_HWTSO) 1586d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TSO; 15879b76d9cbSPyun YongHyeon if (p->if_capenable & IFCAP_VLAN_HWTSO) { 1588d89baa5aSAlexander Motin ena |= mena & IFCAP_TSO; 1589d89baa5aSAlexander Motin if (ena & IFCAP_TSO) 1590d89baa5aSAlexander Motin hwa |= p->if_hwassist & CSUM_TSO; 15919b76d9cbSPyun YongHyeon } 159209fe6320SNavdeep Parhar 159309fe6320SNavdeep Parhar /* 1594*59150e91SAlexander Motin * If the parent interface can do LRO and checksum offloading on 1595*59150e91SAlexander Motin * VLANs, then guess it may do LRO on VLANs. False positive here 1596*59150e91SAlexander Motin * cost nothing, while false negative may lead to some confusions. 1597*59150e91SAlexander Motin */ 1598*59150e91SAlexander Motin if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1599*59150e91SAlexander Motin cap |= p->if_capabilities & IFCAP_LRO; 1600*59150e91SAlexander Motin if (p->if_capenable & IFCAP_VLAN_HWCSUM) 1601*59150e91SAlexander Motin ena |= p->if_capenable & IFCAP_LRO; 1602*59150e91SAlexander Motin 1603*59150e91SAlexander Motin /* 160409fe6320SNavdeep Parhar * If the parent interface can offload TCP connections over VLANs then 160509fe6320SNavdeep Parhar * propagate its TOE capability to the VLAN interface. 160609fe6320SNavdeep Parhar * 160709fe6320SNavdeep Parhar * All TOE drivers in the tree today can deal with VLANs. If this 160809fe6320SNavdeep Parhar * changes then IFCAP_VLAN_TOE should be promoted to a full capability 160909fe6320SNavdeep Parhar * with its own bit. 161009fe6320SNavdeep Parhar */ 161109fe6320SNavdeep Parhar #define IFCAP_VLAN_TOE IFCAP_TOE 161209fe6320SNavdeep Parhar if (p->if_capabilities & IFCAP_VLAN_TOE) 1613d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TOE; 161409fe6320SNavdeep Parhar if (p->if_capenable & IFCAP_VLAN_TOE) { 161509fe6320SNavdeep Parhar TOEDEV(ifp) = TOEDEV(p); 1616d89baa5aSAlexander Motin ena |= mena & IFCAP_TOE; 161709fe6320SNavdeep Parhar } 1618f3e7afe2SHans Petter Selasky 1619d89baa5aSAlexander Motin /* 1620d89baa5aSAlexander Motin * If the parent interface supports dynamic link state, so does the 1621d89baa5aSAlexander Motin * VLAN interface. 1622d89baa5aSAlexander Motin */ 1623d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_LINKSTATE); 1624d89baa5aSAlexander Motin ena |= (mena & IFCAP_LINKSTATE); 1625d89baa5aSAlexander Motin 1626f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1627f3e7afe2SHans Petter Selasky /* 1628f3e7afe2SHans Petter Selasky * If the parent interface supports ratelimiting, so does the 1629f3e7afe2SHans Petter Selasky * VLAN interface. 1630f3e7afe2SHans Petter Selasky */ 1631d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_TXRTLMT); 1632d89baa5aSAlexander Motin ena |= (mena & IFCAP_TXRTLMT); 1633f3e7afe2SHans Petter Selasky #endif 1634d89baa5aSAlexander Motin 1635d89baa5aSAlexander Motin ifp->if_capabilities = cap; 1636d89baa5aSAlexander Motin ifp->if_capenable = ena; 1637d89baa5aSAlexander Motin ifp->if_hwassist = hwa; 163875ee267cSGleb Smirnoff } 163975ee267cSGleb Smirnoff 164075ee267cSGleb Smirnoff static void 164175ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 164275ee267cSGleb Smirnoff { 164375ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 164475ee267cSGleb Smirnoff struct ifvlan *ifv; 164575ee267cSGleb Smirnoff int i; 164675ee267cSGleb Smirnoff 164775ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 164875ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 164915ed2fa1SYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 165075ee267cSGleb Smirnoff if (trunk->vlans[i] != NULL) { 165175ee267cSGleb Smirnoff ifv = trunk->vlans[i]; 165275ee267cSGleb Smirnoff #else 165375ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 165475ee267cSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 165575ee267cSGleb Smirnoff #endif 165675ee267cSGleb Smirnoff vlan_capabilities(ifv); 165775ee267cSGleb Smirnoff } 165875ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 1659127d7b2dSAndre Oppermann } 1660127d7b2dSAndre Oppermann 1661a3814acfSSam Leffler static int 1662cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 16632cc2df49SGarrett Wollman { 16642cc2df49SGarrett Wollman struct ifnet *p; 16652cc2df49SGarrett Wollman struct ifreq *ifr; 1666e4cd31ddSJeff Roberson struct ifaddr *ifa; 16672cc2df49SGarrett Wollman struct ifvlan *ifv; 16682d222cb7SAlexander Motin struct ifvlantrunk *trunk; 16692cc2df49SGarrett Wollman struct vlanreq vlr; 16702cc2df49SGarrett Wollman int error = 0; 16712cc2df49SGarrett Wollman 16722cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 1673e4cd31ddSJeff Roberson ifa = (struct ifaddr *) data; 16742cc2df49SGarrett Wollman ifv = ifp->if_softc; 16752cc2df49SGarrett Wollman 16762cc2df49SGarrett Wollman switch (cmd) { 1677e4cd31ddSJeff Roberson case SIOCSIFADDR: 1678e4cd31ddSJeff Roberson ifp->if_flags |= IFF_UP; 1679e4cd31ddSJeff Roberson #ifdef INET 1680e4cd31ddSJeff Roberson if (ifa->ifa_addr->sa_family == AF_INET) 1681e4cd31ddSJeff Roberson arp_ifinit(ifp, ifa); 1682e4cd31ddSJeff Roberson #endif 1683e4cd31ddSJeff Roberson break; 1684e4cd31ddSJeff Roberson case SIOCGIFADDR: 1685e4cd31ddSJeff Roberson { 1686e4cd31ddSJeff Roberson struct sockaddr *sa; 1687e4cd31ddSJeff Roberson 1688e4cd31ddSJeff Roberson sa = (struct sockaddr *)&ifr->ifr_data; 1689e4cd31ddSJeff Roberson bcopy(IF_LLADDR(ifp), sa->sa_data, ifp->if_addrlen); 1690e4cd31ddSJeff Roberson } 1691e4cd31ddSJeff Roberson break; 1692b3cca108SBill Fenner case SIOCGIFMEDIA: 16934faedfe8SSam Leffler VLAN_LOCK(); 169475ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 1695d8564efdSEd Maste p = PARENT(ifv); 16964faedfe8SSam Leffler VLAN_UNLOCK(); 1697d8564efdSEd Maste error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 1698b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 1699b3cca108SBill Fenner if (error == 0) { 1700b3cca108SBill Fenner struct ifmediareq *ifmr; 1701b3cca108SBill Fenner 1702b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 1703b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1704b3cca108SBill Fenner ifmr->ifm_count = 1; 1705b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 1706b3cca108SBill Fenner ifmr->ifm_ulist, 1707b3cca108SBill Fenner sizeof(int)); 1708b3cca108SBill Fenner } 1709b3cca108SBill Fenner } 17104faedfe8SSam Leffler } else { 17114faedfe8SSam Leffler VLAN_UNLOCK(); 1712b3cca108SBill Fenner error = EINVAL; 17134faedfe8SSam Leffler } 1714b3cca108SBill Fenner break; 1715b3cca108SBill Fenner 1716b3cca108SBill Fenner case SIOCSIFMEDIA: 1717b3cca108SBill Fenner error = EINVAL; 1718b3cca108SBill Fenner break; 1719b3cca108SBill Fenner 17202cc2df49SGarrett Wollman case SIOCSIFMTU: 17212cc2df49SGarrett Wollman /* 17222cc2df49SGarrett Wollman * Set the interface MTU. 17232cc2df49SGarrett Wollman */ 17244faedfe8SSam Leffler VLAN_LOCK(); 172575ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 1726a3814acfSSam Leffler if (ifr->ifr_mtu > 172775ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1728a3814acfSSam Leffler ifr->ifr_mtu < 1729a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 17302cc2df49SGarrett Wollman error = EINVAL; 1731a3814acfSSam Leffler else 17322cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 1733a3814acfSSam Leffler } else 1734a3814acfSSam Leffler error = EINVAL; 17354faedfe8SSam Leffler VLAN_UNLOCK(); 17362cc2df49SGarrett Wollman break; 17372cc2df49SGarrett Wollman 17382cc2df49SGarrett Wollman case SIOCSETVLAN: 1739ccf7ba97SMarko Zec #ifdef VIMAGE 174015f6780eSRobert Watson /* 174115f6780eSRobert Watson * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN 174215f6780eSRobert Watson * interface to be delegated to a jail without allowing the 174315f6780eSRobert Watson * jail to change what underlying interface/VID it is 174415f6780eSRobert Watson * associated with. We are not entirely convinced that this 17455a39f779SRobert Watson * is the right way to accomplish that policy goal. 174615f6780eSRobert Watson */ 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 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr)); 17532cc2df49SGarrett Wollman if (error) 17542cc2df49SGarrett Wollman break; 17552cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 1756f731f104SBill Paul vlan_unconfig(ifp); 17572cc2df49SGarrett Wollman break; 17582cc2df49SGarrett Wollman } 17592cc2df49SGarrett Wollman p = ifunit(vlr.vlr_parent); 17601bdc73d3SEd Maste if (p == NULL) { 17612cc2df49SGarrett Wollman error = ENOENT; 17622cc2df49SGarrett Wollman break; 17632cc2df49SGarrett Wollman } 176475ee267cSGleb Smirnoff error = vlan_config(ifv, p, vlr.vlr_tag); 176575ee267cSGleb Smirnoff if (error) 17662cc2df49SGarrett Wollman break; 1767a3814acfSSam Leffler 17681cf236fbSYaroslav Tykhiy /* Update flags on the parent, if necessary. */ 17691cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 1); 17702cc2df49SGarrett Wollman break; 17712cc2df49SGarrett Wollman 17722cc2df49SGarrett Wollman case SIOCGETVLAN: 1773ccf7ba97SMarko Zec #ifdef VIMAGE 1774ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 1775ccf7ba97SMarko Zec error = EPERM; 1776ccf7ba97SMarko Zec break; 1777ccf7ba97SMarko Zec } 1778ccf7ba97SMarko Zec #endif 177915a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 17804faedfe8SSam Leffler VLAN_LOCK(); 178175ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 178275ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 17839bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 17847983103aSRobert Watson vlr.vlr_tag = ifv->ifv_vid; 17852cc2df49SGarrett Wollman } 17864faedfe8SSam Leffler VLAN_UNLOCK(); 178715a66c21SBruce M Simpson error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 17882cc2df49SGarrett Wollman break; 17892cc2df49SGarrett Wollman 17902cc2df49SGarrett Wollman case SIOCSIFFLAGS: 17912cc2df49SGarrett Wollman /* 17921cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 17931cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 17942cc2df49SGarrett Wollman */ 179575ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 17961cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 17972cc2df49SGarrett Wollman break; 1798a3814acfSSam Leffler 1799f731f104SBill Paul case SIOCADDMULTI: 1800f731f104SBill Paul case SIOCDELMULTI: 180175ee267cSGleb Smirnoff /* 180275ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 180375ee267cSGleb Smirnoff * when we do. 180475ee267cSGleb Smirnoff */ 18052d222cb7SAlexander Motin trunk = TRUNK(ifv); 18062d222cb7SAlexander Motin if (trunk != NULL) { 18072d222cb7SAlexander Motin TRUNK_LOCK(trunk); 1808f731f104SBill Paul error = vlan_setmulti(ifp); 18092d222cb7SAlexander Motin TRUNK_UNLOCK(trunk); 18102d222cb7SAlexander Motin } 1811f731f104SBill Paul break; 181275ee267cSGleb Smirnoff 18132ccbbd06SMarcelo Araujo case SIOCGVLANPCP: 18142ccbbd06SMarcelo Araujo #ifdef VIMAGE 18152ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 18162ccbbd06SMarcelo Araujo error = EPERM; 18172ccbbd06SMarcelo Araujo break; 18182ccbbd06SMarcelo Araujo } 18192ccbbd06SMarcelo Araujo #endif 18202ccbbd06SMarcelo Araujo ifr->ifr_vlan_pcp = ifv->ifv_pcp; 18212ccbbd06SMarcelo Araujo break; 18222ccbbd06SMarcelo Araujo 18232ccbbd06SMarcelo Araujo case SIOCSVLANPCP: 18242ccbbd06SMarcelo Araujo #ifdef VIMAGE 18252ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 18262ccbbd06SMarcelo Araujo error = EPERM; 18272ccbbd06SMarcelo Araujo break; 18282ccbbd06SMarcelo Araujo } 18292ccbbd06SMarcelo Araujo #endif 18302ccbbd06SMarcelo Araujo error = priv_check(curthread, PRIV_NET_SETVLANPCP); 18312ccbbd06SMarcelo Araujo if (error) 18322ccbbd06SMarcelo Araujo break; 18332ccbbd06SMarcelo Araujo if (ifr->ifr_vlan_pcp > 7) { 18342ccbbd06SMarcelo Araujo error = EINVAL; 18352ccbbd06SMarcelo Araujo break; 18362ccbbd06SMarcelo Araujo } 18372ccbbd06SMarcelo Araujo ifv->ifv_pcp = ifr->ifr_vlan_pcp; 18382ccbbd06SMarcelo Araujo vlan_tag_recalculate(ifv); 18392ccbbd06SMarcelo Araujo break; 18402ccbbd06SMarcelo Araujo 1841d89baa5aSAlexander Motin case SIOCSIFCAP: 1842d89baa5aSAlexander Motin VLAN_LOCK(); 1843d89baa5aSAlexander Motin ifv->ifv_capenable = ifr->ifr_reqcap; 1844d89baa5aSAlexander Motin trunk = TRUNK(ifv); 1845d89baa5aSAlexander Motin if (trunk != NULL) { 1846d89baa5aSAlexander Motin TRUNK_LOCK(trunk); 1847d89baa5aSAlexander Motin vlan_capabilities(ifv); 1848d89baa5aSAlexander Motin TRUNK_UNLOCK(trunk); 1849d89baa5aSAlexander Motin } 1850d89baa5aSAlexander Motin VLAN_UNLOCK(); 1851d89baa5aSAlexander Motin break; 1852d89baa5aSAlexander Motin 18532cc2df49SGarrett Wollman default: 1854e4cd31ddSJeff Roberson error = EINVAL; 1855e4cd31ddSJeff Roberson break; 18562cc2df49SGarrett Wollman } 185715a66c21SBruce M Simpson 185815a66c21SBruce M Simpson return (error); 18592cc2df49SGarrett Wollman } 1860f3e7afe2SHans Petter Selasky 1861f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1862f3e7afe2SHans Petter Selasky static int 1863f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp, 1864f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *params, 1865f3e7afe2SHans Petter Selasky struct m_snd_tag **ppmt) 1866f3e7afe2SHans Petter Selasky { 1867f3e7afe2SHans Petter Selasky 1868f3e7afe2SHans Petter Selasky /* get trunk device */ 1869f3e7afe2SHans Petter Selasky ifp = vlan_trunkdev(ifp); 1870f3e7afe2SHans Petter Selasky if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0) 1871f3e7afe2SHans Petter Selasky return (EOPNOTSUPP); 1872f3e7afe2SHans Petter Selasky /* forward allocation request */ 1873f3e7afe2SHans Petter Selasky return (ifp->if_snd_tag_alloc(ifp, params, ppmt)); 1874f3e7afe2SHans Petter Selasky } 1875f3e7afe2SHans Petter Selasky #endif 1876