1c398230bSWarner Losh /*- 22cc2df49SGarrett Wollman * Copyright 1998 Massachusetts Institute of Technology 32cc2df49SGarrett Wollman * 42cc2df49SGarrett Wollman * Permission to use, copy, modify, and distribute this software and 52cc2df49SGarrett Wollman * its documentation for any purpose and without fee is hereby 62cc2df49SGarrett Wollman * granted, provided that both the above copyright notice and this 72cc2df49SGarrett Wollman * permission notice appear in all copies, that both the above 82cc2df49SGarrett Wollman * copyright notice and this permission notice appear in all 92cc2df49SGarrett Wollman * supporting documentation, and that the name of M.I.T. not be used 102cc2df49SGarrett Wollman * in advertising or publicity pertaining to distribution of the 112cc2df49SGarrett Wollman * software without specific, written prior permission. M.I.T. makes 122cc2df49SGarrett Wollman * no representations about the suitability of this software for any 132cc2df49SGarrett Wollman * purpose. It is provided "as is" without express or implied 142cc2df49SGarrett Wollman * warranty. 152cc2df49SGarrett Wollman * 162cc2df49SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 172cc2df49SGarrett Wollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 182cc2df49SGarrett Wollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 192cc2df49SGarrett Wollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 202cc2df49SGarrett Wollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212cc2df49SGarrett Wollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222cc2df49SGarrett Wollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 232cc2df49SGarrett Wollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 242cc2df49SGarrett Wollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 252cc2df49SGarrett Wollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 262cc2df49SGarrett Wollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 272cc2df49SGarrett Wollman * SUCH DAMAGE. 282cc2df49SGarrett Wollman */ 292cc2df49SGarrett Wollman 302cc2df49SGarrett Wollman /* 312cc2df49SGarrett Wollman * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 322cc2df49SGarrett Wollman * Might be extended some day to also handle IEEE 802.1p priority 332cc2df49SGarrett Wollman * tagging. This is sort of sneaky in the implementation, since 342cc2df49SGarrett Wollman * we need to pretend to be enough of an Ethernet implementation 352cc2df49SGarrett Wollman * to make arp work. The way we do this is by telling everyone 362cc2df49SGarrett Wollman * that we are an Ethernet, and then catch the packets that 37d9b1d615SJohn Baldwin * ether_output() sends to us via if_transmit(), rewrite them for 38d9b1d615SJohn Baldwin * use by the real outgoing interface, and ask it to send them. 392cc2df49SGarrett Wollman */ 402cc2df49SGarrett Wollman 418b2d9181SPyun YongHyeon #include <sys/cdefs.h> 428b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$"); 438b2d9181SPyun YongHyeon 44*2c5b403eSOleg Bulyzhin #include "opt_inet.h" 4575ee267cSGleb Smirnoff #include "opt_vlan.h" 462cc2df49SGarrett Wollman 472cc2df49SGarrett Wollman #include <sys/param.h> 482cc2df49SGarrett Wollman #include <sys/kernel.h> 4975ee267cSGleb Smirnoff #include <sys/lock.h> 50f731f104SBill Paul #include <sys/malloc.h> 512cc2df49SGarrett Wollman #include <sys/mbuf.h> 522b120974SPeter Wemm #include <sys/module.h> 5375ee267cSGleb Smirnoff #include <sys/rwlock.h> 54f731f104SBill Paul #include <sys/queue.h> 552cc2df49SGarrett Wollman #include <sys/socket.h> 562cc2df49SGarrett Wollman #include <sys/sockio.h> 572cc2df49SGarrett Wollman #include <sys/sysctl.h> 582cc2df49SGarrett Wollman #include <sys/systm.h> 59e4cd31ddSJeff Roberson #include <sys/sx.h> 602cc2df49SGarrett Wollman 612cc2df49SGarrett Wollman #include <net/bpf.h> 622cc2df49SGarrett Wollman #include <net/ethernet.h> 632cc2df49SGarrett Wollman #include <net/if.h> 64f889d2efSBrooks Davis #include <net/if_clone.h> 652cc2df49SGarrett Wollman #include <net/if_dl.h> 662cc2df49SGarrett Wollman #include <net/if_types.h> 672cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 684b79449eSBjoern A. Zeeb #include <net/vnet.h> 692cc2df49SGarrett Wollman 70*2c5b403eSOleg Bulyzhin #ifdef INET 71*2c5b403eSOleg Bulyzhin #include <netinet/in.h> 72*2c5b403eSOleg Bulyzhin #include <netinet/if_ether.h> 73*2c5b403eSOleg Bulyzhin #endif 74*2c5b403eSOleg Bulyzhin 7575ee267cSGleb Smirnoff #define VLAN_DEF_HWIDTH 4 7664a17d2eSYaroslav Tykhiy #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 7775ee267cSGleb Smirnoff 782dc879b3SYaroslav Tykhiy #define UP_AND_RUNNING(ifp) \ 792dc879b3SYaroslav Tykhiy ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 802dc879b3SYaroslav Tykhiy 8175ee267cSGleb Smirnoff LIST_HEAD(ifvlanhead, ifvlan); 8275ee267cSGleb Smirnoff 8375ee267cSGleb Smirnoff struct ifvlantrunk { 8475ee267cSGleb Smirnoff struct ifnet *parent; /* parent interface of this trunk */ 8575ee267cSGleb Smirnoff struct rwlock rw; 8675ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 875cb8c31aSYaroslav Tykhiy #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 885cb8c31aSYaroslav Tykhiy struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 8975ee267cSGleb Smirnoff #else 9075ee267cSGleb Smirnoff struct ifvlanhead *hash; /* dynamic hash-list table */ 9175ee267cSGleb Smirnoff uint16_t hmask; 9275ee267cSGleb Smirnoff uint16_t hwidth; 9375ee267cSGleb Smirnoff #endif 9475ee267cSGleb Smirnoff int refcnt; 9575ee267cSGleb Smirnoff }; 969d4fe4b2SBrooks Davis 97a3814acfSSam Leffler struct vlan_mc_entry { 98e4cd31ddSJeff Roberson struct sockaddr_dl mc_addr; 99a3814acfSSam Leffler SLIST_ENTRY(vlan_mc_entry) mc_entries; 100a3814acfSSam Leffler }; 101a3814acfSSam Leffler 102a3814acfSSam Leffler struct ifvlan { 10375ee267cSGleb Smirnoff struct ifvlantrunk *ifv_trunk; 104fc74a9f9SBrooks Davis struct ifnet *ifv_ifp; 105e4cd31ddSJeff Roberson void *ifv_cookie; 10675ee267cSGleb Smirnoff #define TRUNK(ifv) ((ifv)->ifv_trunk) 10775ee267cSGleb Smirnoff #define PARENT(ifv) ((ifv)->ifv_trunk->parent) 1081cf236fbSYaroslav Tykhiy int ifv_pflags; /* special flags we have set on parent */ 109a3814acfSSam Leffler struct ifv_linkmib { 110a3814acfSSam Leffler int ifvm_encaplen; /* encapsulation length */ 111a3814acfSSam Leffler int ifvm_mtufudge; /* MTU fudged by this much */ 112a3814acfSSam Leffler int ifvm_mintu; /* min transmission unit */ 11373f2233dSYaroslav Tykhiy uint16_t ifvm_proto; /* encapsulation ethertype */ 11475ee267cSGleb Smirnoff uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 115a3814acfSSam Leffler } ifv_mib; 116114c608cSYaroslav Tykhiy SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 117c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY 118a3814acfSSam Leffler LIST_ENTRY(ifvlan) ifv_list; 119c0cb022bSYaroslav Tykhiy #endif 120a3814acfSSam Leffler }; 12173f2233dSYaroslav Tykhiy #define ifv_proto ifv_mib.ifvm_proto 1227983103aSRobert Watson #define ifv_vid ifv_mib.ifvm_tag 123a3814acfSSam Leffler #define ifv_encaplen ifv_mib.ifvm_encaplen 124a3814acfSSam Leffler #define ifv_mtufudge ifv_mib.ifvm_mtufudge 125a3814acfSSam Leffler #define ifv_mintu ifv_mib.ifvm_mintu 126a3814acfSSam Leffler 12775ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */ 1281cf236fbSYaroslav Tykhiy static struct { 1291cf236fbSYaroslav Tykhiy int flag; 1301cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 1311cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 1321cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 1331cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 1341cf236fbSYaroslav Tykhiy {0, NULL} 1351cf236fbSYaroslav Tykhiy }; 136a3814acfSSam Leffler 1374a408dcbSBill Paul SYSCTL_DECL(_net_link); 1386472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, 1396472ac3dSEd Schouten "IEEE 802.1Q VLAN"); 1406472ac3dSEd Schouten static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, 1416472ac3dSEd Schouten "for consistency"); 1422cc2df49SGarrett Wollman 143f6e5e0adSYaroslav Tykhiy static int soft_pad = 0; 144f6e5e0adSYaroslav Tykhiy SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0, 145f6e5e0adSYaroslav Tykhiy "pad short frames before tagging"); 146f6e5e0adSYaroslav Tykhiy 14742a58907SGleb Smirnoff static const char vlanname[] = "vlan"; 14842a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface"); 1492cc2df49SGarrett Wollman 1505cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag; 151ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag; 1525cb8c31aSYaroslav Tykhiy 1534faedfe8SSam Leffler /* 15475ee267cSGleb Smirnoff * We have a global mutex, that is used to serialize configuration 15575ee267cSGleb Smirnoff * changes and isn't used in normal packet delivery. 15675ee267cSGleb Smirnoff * 15775ee267cSGleb Smirnoff * We also have a per-trunk rwlock, that is locked shared on packet 15875ee267cSGleb Smirnoff * processing and exclusive when configuration is changed. 15975ee267cSGleb Smirnoff * 16075ee267cSGleb Smirnoff * The VLAN_ARRAY substitutes the dynamic hash with a static array 161ad387028SAndrew Thompson * with 4096 entries. In theory this can give a boost in processing, 16275ee267cSGleb Smirnoff * however on practice it does not. Probably this is because array 16375ee267cSGleb Smirnoff * is too big to fit into CPU cache. 1644faedfe8SSam Leffler */ 165e4cd31ddSJeff Roberson static struct sx ifv_lock; 166e4cd31ddSJeff Roberson #define VLAN_LOCK_INIT() sx_init(&ifv_lock, "vlan_global") 167e4cd31ddSJeff Roberson #define VLAN_LOCK_DESTROY() sx_destroy(&ifv_lock) 168e4cd31ddSJeff Roberson #define VLAN_LOCK_ASSERT() sx_assert(&ifv_lock, SA_LOCKED) 169e4cd31ddSJeff Roberson #define VLAN_LOCK() sx_xlock(&ifv_lock) 170e4cd31ddSJeff Roberson #define VLAN_UNLOCK() sx_xunlock(&ifv_lock) 17142a58907SGleb Smirnoff #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, vlanname) 17275ee267cSGleb Smirnoff #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw) 17375ee267cSGleb Smirnoff #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw) 17475ee267cSGleb Smirnoff #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw) 17575ee267cSGleb Smirnoff #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED) 17675ee267cSGleb Smirnoff #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw) 17775ee267cSGleb Smirnoff #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw) 17875ee267cSGleb Smirnoff #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED) 17975ee267cSGleb Smirnoff 18075ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 18175ee267cSGleb Smirnoff static void vlan_inithash(struct ifvlantrunk *trunk); 18275ee267cSGleb Smirnoff static void vlan_freehash(struct ifvlantrunk *trunk); 18375ee267cSGleb Smirnoff static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 18475ee267cSGleb Smirnoff static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 18575ee267cSGleb Smirnoff static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 18675ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 1877983103aSRobert Watson uint16_t vid); 18875ee267cSGleb Smirnoff #endif 18975ee267cSGleb Smirnoff static void trunk_destroy(struct ifvlantrunk *trunk); 1904faedfe8SSam Leffler 191114c608cSYaroslav Tykhiy static void vlan_init(void *foo); 192a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 193cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 194d9b1d615SJohn Baldwin static void vlan_qflush(struct ifnet *ifp); 1951cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 1961cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 1971cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 198f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 199d9b1d615SJohn Baldwin static int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 2006f359e28SJohn Baldwin static void vlan_unconfig(struct ifnet *ifp); 20128cc4d37SJohn Baldwin static void vlan_unconfig_locked(struct ifnet *ifp, int departing); 20275ee267cSGleb Smirnoff static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag); 203a6fffd6cSBrooks Davis static void vlan_link_state(struct ifnet *ifp); 20475ee267cSGleb Smirnoff static void vlan_capabilities(struct ifvlan *ifv); 20575ee267cSGleb Smirnoff static void vlan_trunk_capabilities(struct ifnet *ifp); 206f731f104SBill Paul 2077983103aSRobert Watson static struct ifnet *vlan_clone_match_ethervid(struct if_clone *, 208f889d2efSBrooks Davis const char *, int *); 209f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 2106b7330e2SSam Leffler static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 211f889d2efSBrooks Davis static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 212f889d2efSBrooks Davis 2135cb8c31aSYaroslav Tykhiy static void vlan_ifdetach(void *arg, struct ifnet *ifp); 214ea4ca115SAndrew Thompson static void vlan_iflladdr(void *arg, struct ifnet *ifp); 2155cb8c31aSYaroslav Tykhiy 21642a58907SGleb Smirnoff static struct if_clone *vlan_cloner; 2179d4fe4b2SBrooks Davis 218ccf7ba97SMarko Zec #ifdef VIMAGE 21942a58907SGleb Smirnoff static VNET_DEFINE(struct if_clone *, vlan_cloner); 220ccf7ba97SMarko Zec #define V_vlan_cloner VNET(vlan_cloner) 221ccf7ba97SMarko Zec #endif 222ccf7ba97SMarko Zec 22375ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 22475ee267cSGleb Smirnoff #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 225114c608cSYaroslav Tykhiy 22675ee267cSGleb Smirnoff static void 22775ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk) 22875ee267cSGleb Smirnoff { 22975ee267cSGleb Smirnoff int i, n; 23075ee267cSGleb Smirnoff 23175ee267cSGleb Smirnoff /* 23275ee267cSGleb Smirnoff * The trunk must not be locked here since we call malloc(M_WAITOK). 23375ee267cSGleb Smirnoff * It is OK in case this function is called before the trunk struct 23475ee267cSGleb Smirnoff * gets hooked up and becomes visible from other threads. 23575ee267cSGleb Smirnoff */ 23675ee267cSGleb Smirnoff 23775ee267cSGleb Smirnoff KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 23875ee267cSGleb Smirnoff ("%s: hash already initialized", __func__)); 23975ee267cSGleb Smirnoff 24075ee267cSGleb Smirnoff trunk->hwidth = VLAN_DEF_HWIDTH; 24175ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 24275ee267cSGleb Smirnoff trunk->hmask = n - 1; 24375ee267cSGleb Smirnoff trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 24475ee267cSGleb Smirnoff for (i = 0; i < n; i++) 24575ee267cSGleb Smirnoff LIST_INIT(&trunk->hash[i]); 24675ee267cSGleb Smirnoff } 24775ee267cSGleb Smirnoff 24875ee267cSGleb Smirnoff static void 24975ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk) 25075ee267cSGleb Smirnoff { 25175ee267cSGleb Smirnoff #ifdef INVARIANTS 25275ee267cSGleb Smirnoff int i; 25375ee267cSGleb Smirnoff 25475ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 25575ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 25675ee267cSGleb Smirnoff KASSERT(LIST_EMPTY(&trunk->hash[i]), 25775ee267cSGleb Smirnoff ("%s: hash table not empty", __func__)); 25875ee267cSGleb Smirnoff #endif 25975ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 26075ee267cSGleb Smirnoff trunk->hash = NULL; 26175ee267cSGleb Smirnoff trunk->hwidth = trunk->hmask = 0; 26275ee267cSGleb Smirnoff } 26375ee267cSGleb Smirnoff 26475ee267cSGleb Smirnoff static int 26575ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 26675ee267cSGleb Smirnoff { 26775ee267cSGleb Smirnoff int i, b; 26875ee267cSGleb Smirnoff struct ifvlan *ifv2; 26975ee267cSGleb Smirnoff 27075ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 27175ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 27275ee267cSGleb Smirnoff 27375ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 2747983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 27575ee267cSGleb Smirnoff LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 2767983103aSRobert Watson if (ifv->ifv_vid == ifv2->ifv_vid) 27775ee267cSGleb Smirnoff return (EEXIST); 27875ee267cSGleb Smirnoff 27975ee267cSGleb Smirnoff /* 28075ee267cSGleb Smirnoff * Grow the hash when the number of vlans exceeds half of the number of 28175ee267cSGleb Smirnoff * hash buckets squared. This will make the average linked-list length 28275ee267cSGleb Smirnoff * buckets/2. 28375ee267cSGleb Smirnoff */ 28475ee267cSGleb Smirnoff if (trunk->refcnt > (b * b) / 2) { 28575ee267cSGleb Smirnoff vlan_growhash(trunk, 1); 2867983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 28775ee267cSGleb Smirnoff } 28875ee267cSGleb Smirnoff LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 28975ee267cSGleb Smirnoff trunk->refcnt++; 29075ee267cSGleb Smirnoff 29175ee267cSGleb Smirnoff return (0); 29275ee267cSGleb Smirnoff } 29375ee267cSGleb Smirnoff 29475ee267cSGleb Smirnoff static int 29575ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 29675ee267cSGleb Smirnoff { 29775ee267cSGleb Smirnoff int i, b; 29875ee267cSGleb Smirnoff struct ifvlan *ifv2; 29975ee267cSGleb Smirnoff 30075ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 30175ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 30275ee267cSGleb Smirnoff 30375ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 3047983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 30575ee267cSGleb Smirnoff LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 30675ee267cSGleb Smirnoff if (ifv2 == ifv) { 30775ee267cSGleb Smirnoff trunk->refcnt--; 30875ee267cSGleb Smirnoff LIST_REMOVE(ifv2, ifv_list); 30975ee267cSGleb Smirnoff if (trunk->refcnt < (b * b) / 2) 31075ee267cSGleb Smirnoff vlan_growhash(trunk, -1); 31175ee267cSGleb Smirnoff return (0); 31275ee267cSGleb Smirnoff } 31375ee267cSGleb Smirnoff 31475ee267cSGleb Smirnoff panic("%s: vlan not found\n", __func__); 31575ee267cSGleb Smirnoff return (ENOENT); /*NOTREACHED*/ 31675ee267cSGleb Smirnoff } 31775ee267cSGleb Smirnoff 31875ee267cSGleb Smirnoff /* 31975ee267cSGleb Smirnoff * Grow the hash larger or smaller if memory permits. 32075ee267cSGleb Smirnoff */ 32175ee267cSGleb Smirnoff static void 32275ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 32375ee267cSGleb Smirnoff { 32475ee267cSGleb Smirnoff struct ifvlan *ifv; 32575ee267cSGleb Smirnoff struct ifvlanhead *hash2; 32675ee267cSGleb Smirnoff int hwidth2, i, j, n, n2; 32775ee267cSGleb Smirnoff 32875ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(trunk); 32975ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 33075ee267cSGleb Smirnoff 33175ee267cSGleb Smirnoff if (howmuch == 0) { 33275ee267cSGleb Smirnoff /* Harmless yet obvious coding error */ 33375ee267cSGleb Smirnoff printf("%s: howmuch is 0\n", __func__); 33475ee267cSGleb Smirnoff return; 33575ee267cSGleb Smirnoff } 33675ee267cSGleb Smirnoff 33775ee267cSGleb Smirnoff hwidth2 = trunk->hwidth + howmuch; 33875ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 33975ee267cSGleb Smirnoff n2 = 1 << hwidth2; 34075ee267cSGleb Smirnoff /* Do not shrink the table below the default */ 34175ee267cSGleb Smirnoff if (hwidth2 < VLAN_DEF_HWIDTH) 34275ee267cSGleb Smirnoff return; 34375ee267cSGleb Smirnoff 34475ee267cSGleb Smirnoff /* M_NOWAIT because we're called with trunk mutex held */ 34575ee267cSGleb Smirnoff hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT); 34675ee267cSGleb Smirnoff if (hash2 == NULL) { 34775ee267cSGleb Smirnoff printf("%s: out of memory -- hash size not changed\n", 34875ee267cSGleb Smirnoff __func__); 34975ee267cSGleb Smirnoff return; /* We can live with the old hash table */ 35075ee267cSGleb Smirnoff } 35175ee267cSGleb Smirnoff for (j = 0; j < n2; j++) 35275ee267cSGleb Smirnoff LIST_INIT(&hash2[j]); 35375ee267cSGleb Smirnoff for (i = 0; i < n; i++) 354c0cb022bSYaroslav Tykhiy while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) { 35575ee267cSGleb Smirnoff LIST_REMOVE(ifv, ifv_list); 3567983103aSRobert Watson j = HASH(ifv->ifv_vid, n2 - 1); 35775ee267cSGleb Smirnoff LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 35875ee267cSGleb Smirnoff } 35975ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 36075ee267cSGleb Smirnoff trunk->hash = hash2; 36175ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 36275ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 363f84b2d69SYaroslav Tykhiy 364f84b2d69SYaroslav Tykhiy if (bootverbose) 365f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 366f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 36775ee267cSGleb Smirnoff } 36875ee267cSGleb Smirnoff 36975ee267cSGleb Smirnoff static __inline struct ifvlan * 3707983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 37175ee267cSGleb Smirnoff { 37275ee267cSGleb Smirnoff struct ifvlan *ifv; 37375ee267cSGleb Smirnoff 37475ee267cSGleb Smirnoff TRUNK_LOCK_RASSERT(trunk); 37575ee267cSGleb Smirnoff 3767983103aSRobert Watson LIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list) 3777983103aSRobert Watson if (ifv->ifv_vid == vid) 37875ee267cSGleb Smirnoff return (ifv); 37975ee267cSGleb Smirnoff return (NULL); 38075ee267cSGleb Smirnoff } 38175ee267cSGleb Smirnoff 38275ee267cSGleb Smirnoff #if 0 38375ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 38475ee267cSGleb Smirnoff static void 38575ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 38675ee267cSGleb Smirnoff { 38775ee267cSGleb Smirnoff int i; 38875ee267cSGleb Smirnoff struct ifvlan *ifv; 38975ee267cSGleb Smirnoff 39075ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 39175ee267cSGleb Smirnoff printf("%d: ", i); 39275ee267cSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 39375ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 39475ee267cSGleb Smirnoff printf("\n"); 39575ee267cSGleb Smirnoff } 39675ee267cSGleb Smirnoff } 39775ee267cSGleb Smirnoff #endif /* 0 */ 398e4cd31ddSJeff Roberson #else 399e4cd31ddSJeff Roberson 400e4cd31ddSJeff Roberson static __inline struct ifvlan * 4017983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 402e4cd31ddSJeff Roberson { 403e4cd31ddSJeff Roberson 4047983103aSRobert Watson return trunk->vlans[vid]; 405e4cd31ddSJeff Roberson } 406e4cd31ddSJeff Roberson 407e4cd31ddSJeff Roberson static __inline int 408e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 409e4cd31ddSJeff Roberson { 410e4cd31ddSJeff Roberson 4117983103aSRobert Watson if (trunk->vlans[ifv->ifv_vid] != NULL) 412e4cd31ddSJeff Roberson return EEXIST; 4137983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = ifv; 414e4cd31ddSJeff Roberson trunk->refcnt++; 415e4cd31ddSJeff Roberson 416e4cd31ddSJeff Roberson return (0); 417e4cd31ddSJeff Roberson } 418e4cd31ddSJeff Roberson 419e4cd31ddSJeff Roberson static __inline int 420e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 421e4cd31ddSJeff Roberson { 422e4cd31ddSJeff Roberson 4237983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = NULL; 424e4cd31ddSJeff Roberson trunk->refcnt--; 425e4cd31ddSJeff Roberson 426e4cd31ddSJeff Roberson return (0); 427e4cd31ddSJeff Roberson } 428e4cd31ddSJeff Roberson 429e4cd31ddSJeff Roberson static __inline void 430e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk) 431e4cd31ddSJeff Roberson { 432e4cd31ddSJeff Roberson } 433e4cd31ddSJeff Roberson 434e4cd31ddSJeff Roberson static __inline void 435e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk) 436e4cd31ddSJeff Roberson { 437e4cd31ddSJeff Roberson } 438e4cd31ddSJeff Roberson 43975ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 44075ee267cSGleb Smirnoff 44175ee267cSGleb Smirnoff static void 44275ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 44375ee267cSGleb Smirnoff { 44475ee267cSGleb Smirnoff VLAN_LOCK_ASSERT(); 44575ee267cSGleb Smirnoff 44675ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 44775ee267cSGleb Smirnoff vlan_freehash(trunk); 44875ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 44933499e2aSYaroslav Tykhiy TRUNK_UNLOCK(trunk); 45033499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 45175ee267cSGleb Smirnoff free(trunk, M_VLAN); 45275ee267cSGleb Smirnoff } 45375ee267cSGleb Smirnoff 454f731f104SBill Paul /* 455f731f104SBill Paul * Program our multicast filter. What we're actually doing is 456f731f104SBill Paul * programming the multicast filter of the parent. This has the 457f731f104SBill Paul * side effect of causing the parent interface to receive multicast 458f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 459f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 460f731f104SBill Paul * to avoid this: there really is only one physical interface. 46129c2dfbeSBruce M Simpson * 46229c2dfbeSBruce M Simpson * XXX: There is a possible race here if more than one thread is 46329c2dfbeSBruce M Simpson * modifying the multicast state of the vlan interface at the same time. 464f731f104SBill Paul */ 4652b120974SPeter Wemm static int 4662b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 467f731f104SBill Paul { 468f731f104SBill Paul struct ifnet *ifp_p; 469f731f104SBill Paul struct ifmultiaddr *ifma, *rifma = NULL; 470f731f104SBill Paul struct ifvlan *sc; 471c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 472f731f104SBill Paul int error; 473f731f104SBill Paul 47429c2dfbeSBruce M Simpson /*VLAN_LOCK_ASSERT();*/ 47529c2dfbeSBruce M Simpson 476f731f104SBill Paul /* Find the parent. */ 477f731f104SBill Paul sc = ifp->if_softc; 47875ee267cSGleb Smirnoff ifp_p = PARENT(sc); 4791b2a4f7aSBill Fenner 4808b615593SMarko Zec CURVNET_SET_QUIET(ifp_p->if_vnet); 4818b615593SMarko Zec 482f731f104SBill Paul /* First, remove any existing filter entries. */ 483c0cb022bSYaroslav Tykhiy while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 484e4cd31ddSJeff Roberson error = if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 485f731f104SBill Paul if (error) 486f731f104SBill Paul return (error); 487f731f104SBill Paul SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 4889d4fe4b2SBrooks Davis free(mc, M_VLAN); 489f731f104SBill Paul } 490f731f104SBill Paul 491f731f104SBill Paul /* Now program new ones. */ 4926817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 493f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 494f731f104SBill Paul continue; 49529c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 49629c2dfbeSBruce M Simpson if (mc == NULL) 49729c2dfbeSBruce M Simpson return (ENOMEM); 498e4cd31ddSJeff Roberson bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 499e4cd31ddSJeff Roberson mc->mc_addr.sdl_index = ifp_p->if_index; 500f731f104SBill Paul SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 501e4cd31ddSJeff Roberson error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 502e4cd31ddSJeff Roberson &rifma); 503f731f104SBill Paul if (error) 504f731f104SBill Paul return (error); 505f731f104SBill Paul } 506f731f104SBill Paul 5078b615593SMarko Zec CURVNET_RESTORE(); 508f731f104SBill Paul return (0); 509f731f104SBill Paul } 5102cc2df49SGarrett Wollman 511a3814acfSSam Leffler /* 512ea4ca115SAndrew Thompson * A handler for parent interface link layer address changes. 513ea4ca115SAndrew Thompson * If the parent interface link layer address is changed we 514ea4ca115SAndrew Thompson * should also change it on all children vlans. 515ea4ca115SAndrew Thompson */ 516ea4ca115SAndrew Thompson static void 517ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 518ea4ca115SAndrew Thompson { 519ea4ca115SAndrew Thompson struct ifvlan *ifv; 5206117727bSAndrew Thompson #ifndef VLAN_ARRAY 5216117727bSAndrew Thompson struct ifvlan *next; 5226117727bSAndrew Thompson #endif 523ea4ca115SAndrew Thompson int i; 524ea4ca115SAndrew Thompson 525ea4ca115SAndrew Thompson /* 526ea4ca115SAndrew Thompson * Check if it's a trunk interface first of all 527ea4ca115SAndrew Thompson * to avoid needless locking. 528ea4ca115SAndrew Thompson */ 529ea4ca115SAndrew Thompson if (ifp->if_vlantrunk == NULL) 530ea4ca115SAndrew Thompson return; 531ea4ca115SAndrew Thompson 532ea4ca115SAndrew Thompson VLAN_LOCK(); 533ea4ca115SAndrew Thompson /* 534ea4ca115SAndrew Thompson * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 535ea4ca115SAndrew Thompson */ 536ea4ca115SAndrew Thompson #ifdef VLAN_ARRAY 537ea4ca115SAndrew Thompson for (i = 0; i < VLAN_ARRAY_SIZE; i++) 5386117727bSAndrew Thompson if ((ifv = ifp->if_vlantrunk->vlans[i])) { 539ea4ca115SAndrew Thompson #else /* VLAN_ARRAY */ 540ea4ca115SAndrew Thompson for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 5416117727bSAndrew Thompson LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) { 542ea4ca115SAndrew Thompson #endif /* VLAN_ARRAY */ 543ea4ca115SAndrew Thompson VLAN_UNLOCK(); 544e4cd31ddSJeff Roberson if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp), 545e4cd31ddSJeff Roberson ifp->if_addrlen); 5466117727bSAndrew Thompson VLAN_LOCK(); 5476117727bSAndrew Thompson } 5486117727bSAndrew Thompson VLAN_UNLOCK(); 549ea4ca115SAndrew Thompson 550ea4ca115SAndrew Thompson } 551ea4ca115SAndrew Thompson 552ea4ca115SAndrew Thompson /* 5535cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 5545cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 5555cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 5565428776eSJohn Baldwin * by ejecting its hot-plug card. However, if an ifnet is simply 5575428776eSJohn Baldwin * being renamed, then there's no need to tear down the state. 5585cb8c31aSYaroslav Tykhiy */ 5595cb8c31aSYaroslav Tykhiy static void 5605cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 5615cb8c31aSYaroslav Tykhiy { 5625cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 5635cb8c31aSYaroslav Tykhiy int i; 5645cb8c31aSYaroslav Tykhiy 5655cb8c31aSYaroslav Tykhiy /* 5665cb8c31aSYaroslav Tykhiy * Check if it's a trunk interface first of all 5675cb8c31aSYaroslav Tykhiy * to avoid needless locking. 5685cb8c31aSYaroslav Tykhiy */ 5695cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk == NULL) 5705cb8c31aSYaroslav Tykhiy return; 5715cb8c31aSYaroslav Tykhiy 5725428776eSJohn Baldwin /* If the ifnet is just being renamed, don't do anything. */ 5735428776eSJohn Baldwin if (ifp->if_flags & IFF_RENAMING) 5745428776eSJohn Baldwin return; 5755428776eSJohn Baldwin 5765cb8c31aSYaroslav Tykhiy VLAN_LOCK(); 5775cb8c31aSYaroslav Tykhiy /* 5785cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 5795cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 5805cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 5815cb8c31aSYaroslav Tykhiy */ 5825cb8c31aSYaroslav Tykhiy #ifdef VLAN_ARRAY 5835cb8c31aSYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 5845cb8c31aSYaroslav Tykhiy if ((ifv = ifp->if_vlantrunk->vlans[i])) { 58528cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 5865cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk == NULL) 5875cb8c31aSYaroslav Tykhiy break; 5885cb8c31aSYaroslav Tykhiy } 5895cb8c31aSYaroslav Tykhiy #else /* VLAN_ARRAY */ 5905cb8c31aSYaroslav Tykhiy restart: 5915cb8c31aSYaroslav Tykhiy for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 5925cb8c31aSYaroslav Tykhiy if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) { 59328cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 5945cb8c31aSYaroslav Tykhiy if (ifp->if_vlantrunk) 5955cb8c31aSYaroslav Tykhiy goto restart; /* trunk->hwidth can change */ 5965cb8c31aSYaroslav Tykhiy else 5975cb8c31aSYaroslav Tykhiy break; 5985cb8c31aSYaroslav Tykhiy } 5995cb8c31aSYaroslav Tykhiy #endif /* VLAN_ARRAY */ 6005cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 6015cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 6025cb8c31aSYaroslav Tykhiy VLAN_UNLOCK(); 6035cb8c31aSYaroslav Tykhiy } 6045cb8c31aSYaroslav Tykhiy 6055cb8c31aSYaroslav Tykhiy /* 606e4cd31ddSJeff Roberson * Return the trunk device for a virtual interface. 607e4cd31ddSJeff Roberson */ 608e4cd31ddSJeff Roberson static struct ifnet * 609e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp) 610e4cd31ddSJeff Roberson { 611e4cd31ddSJeff Roberson struct ifvlan *ifv; 612e4cd31ddSJeff Roberson 613e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 614e4cd31ddSJeff Roberson return (NULL); 615e4cd31ddSJeff Roberson ifv = ifp->if_softc; 616e4cd31ddSJeff Roberson ifp = NULL; 617e4cd31ddSJeff Roberson VLAN_LOCK(); 618e4cd31ddSJeff Roberson if (ifv->ifv_trunk) 619e4cd31ddSJeff Roberson ifp = PARENT(ifv); 620e4cd31ddSJeff Roberson VLAN_UNLOCK(); 621e4cd31ddSJeff Roberson return (ifp); 622e4cd31ddSJeff Roberson } 623e4cd31ddSJeff Roberson 624e4cd31ddSJeff Roberson /* 6257983103aSRobert Watson * Return the 12-bit VLAN VID for this interface, for use by external 6267983103aSRobert Watson * components such as Infiniband. 6277983103aSRobert Watson * 6287983103aSRobert Watson * XXXRW: Note that the function name here is historical; it should be named 6297983103aSRobert Watson * vlan_vid(). 630e4cd31ddSJeff Roberson */ 631e4cd31ddSJeff Roberson static int 6327983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp) 633e4cd31ddSJeff Roberson { 634e4cd31ddSJeff Roberson struct ifvlan *ifv; 635e4cd31ddSJeff Roberson 636e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 637e4cd31ddSJeff Roberson return (EINVAL); 638e4cd31ddSJeff Roberson ifv = ifp->if_softc; 6397983103aSRobert Watson *vidp = ifv->ifv_vid; 640e4cd31ddSJeff Roberson return (0); 641e4cd31ddSJeff Roberson } 642e4cd31ddSJeff Roberson 643e4cd31ddSJeff Roberson /* 644e4cd31ddSJeff Roberson * Return a driver specific cookie for this interface. Synchronization 645e4cd31ddSJeff Roberson * with setcookie must be provided by the driver. 646e4cd31ddSJeff Roberson */ 647e4cd31ddSJeff Roberson static void * 648e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp) 649e4cd31ddSJeff Roberson { 650e4cd31ddSJeff Roberson struct ifvlan *ifv; 651e4cd31ddSJeff Roberson 652e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 653e4cd31ddSJeff Roberson return (NULL); 654e4cd31ddSJeff Roberson ifv = ifp->if_softc; 655e4cd31ddSJeff Roberson return (ifv->ifv_cookie); 656e4cd31ddSJeff Roberson } 657e4cd31ddSJeff Roberson 658e4cd31ddSJeff Roberson /* 659e4cd31ddSJeff Roberson * Store a cookie in our softc that drivers can use to store driver 660e4cd31ddSJeff Roberson * private per-instance data in. 661e4cd31ddSJeff Roberson */ 662e4cd31ddSJeff Roberson static int 663e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie) 664e4cd31ddSJeff Roberson { 665e4cd31ddSJeff Roberson struct ifvlan *ifv; 666e4cd31ddSJeff Roberson 667e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 668e4cd31ddSJeff Roberson return (EINVAL); 669e4cd31ddSJeff Roberson ifv = ifp->if_softc; 670e4cd31ddSJeff Roberson ifv->ifv_cookie = cookie; 671e4cd31ddSJeff Roberson return (0); 672e4cd31ddSJeff Roberson } 673e4cd31ddSJeff Roberson 674e4cd31ddSJeff Roberson /* 6757983103aSRobert Watson * Return the vlan device present at the specific VID. 676e4cd31ddSJeff Roberson */ 677e4cd31ddSJeff Roberson static struct ifnet * 6787983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid) 679e4cd31ddSJeff Roberson { 680e4cd31ddSJeff Roberson struct ifvlantrunk *trunk; 681e4cd31ddSJeff Roberson struct ifvlan *ifv; 682e4cd31ddSJeff Roberson 683e4cd31ddSJeff Roberson trunk = ifp->if_vlantrunk; 684e4cd31ddSJeff Roberson if (trunk == NULL) 685e4cd31ddSJeff Roberson return (NULL); 686e4cd31ddSJeff Roberson ifp = NULL; 687e4cd31ddSJeff Roberson TRUNK_RLOCK(trunk); 6887983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 689e4cd31ddSJeff Roberson if (ifv) 690e4cd31ddSJeff Roberson ifp = ifv->ifv_ifp; 691e4cd31ddSJeff Roberson TRUNK_RUNLOCK(trunk); 692e4cd31ddSJeff Roberson return (ifp); 693e4cd31ddSJeff Roberson } 694e4cd31ddSJeff Roberson 695e4cd31ddSJeff Roberson /* 696a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 697a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 698a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 699a3814acfSSam Leffler * set here. Noone else in the system should be aware of this so 700a3814acfSSam Leffler * we use an explicit reference here. 701a3814acfSSam Leffler */ 702a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 703a3814acfSSam Leffler 704984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 705a6fffd6cSBrooks Davis extern void (*vlan_link_state_p)(struct ifnet *); 706127d7b2dSAndre Oppermann 7072b120974SPeter Wemm static int 7082b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 7092b120974SPeter Wemm { 7109d4fe4b2SBrooks Davis 7112b120974SPeter Wemm switch (type) { 7122b120974SPeter Wemm case MOD_LOAD: 7135cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 7145cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 7155cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 7165cb8c31aSYaroslav Tykhiy return (ENOMEM); 717ea4ca115SAndrew Thompson iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 718ea4ca115SAndrew Thompson vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 719ea4ca115SAndrew Thompson if (iflladdr_tag == NULL) 720ea4ca115SAndrew Thompson return (ENOMEM); 7214faedfe8SSam Leffler VLAN_LOCK_INIT(); 7229d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 723127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 72475ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 725e4cd31ddSJeff Roberson vlan_trunkdev_p = vlan_trunkdev; 726e4cd31ddSJeff Roberson vlan_cookie_p = vlan_cookie; 727e4cd31ddSJeff Roberson vlan_setcookie_p = vlan_setcookie; 728e4cd31ddSJeff Roberson vlan_tag_p = vlan_tag; 729e4cd31ddSJeff Roberson vlan_devat_p = vlan_devat; 730ccf7ba97SMarko Zec #ifndef VIMAGE 73142a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 73242a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 733ccf7ba97SMarko Zec #endif 73425c0f7b3SYaroslav Tykhiy if (bootverbose) 73525c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 73625c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 73725c0f7b3SYaroslav Tykhiy "full-size arrays" 73825c0f7b3SYaroslav Tykhiy #else 73925c0f7b3SYaroslav Tykhiy "hash tables with chaining" 74025c0f7b3SYaroslav Tykhiy #endif 74125c0f7b3SYaroslav Tykhiy 74225c0f7b3SYaroslav Tykhiy "\n"); 7432b120974SPeter Wemm break; 7442b120974SPeter Wemm case MOD_UNLOAD: 745ccf7ba97SMarko Zec #ifndef VIMAGE 74642a58907SGleb Smirnoff if_clone_detach(vlan_cloner); 747ccf7ba97SMarko Zec #endif 7485cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 749ea4ca115SAndrew Thompson EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 7509d4fe4b2SBrooks Davis vlan_input_p = NULL; 751127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 75275ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 753e4cd31ddSJeff Roberson vlan_trunkdev_p = NULL; 754e4cd31ddSJeff Roberson vlan_tag_p = NULL; 75509fe6320SNavdeep Parhar vlan_cookie_p = NULL; 75609fe6320SNavdeep Parhar vlan_setcookie_p = NULL; 757e4cd31ddSJeff Roberson vlan_devat_p = NULL; 7584faedfe8SSam Leffler VLAN_LOCK_DESTROY(); 75925c0f7b3SYaroslav Tykhiy if (bootverbose) 76025c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 7619d4fe4b2SBrooks Davis break; 7623e019deaSPoul-Henning Kamp default: 7633e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 7642b120974SPeter Wemm } 76515a66c21SBruce M Simpson return (0); 7662b120974SPeter Wemm } 7672b120974SPeter Wemm 7682b120974SPeter Wemm static moduledata_t vlan_mod = { 7692b120974SPeter Wemm "if_vlan", 7702b120974SPeter Wemm vlan_modevent, 7719823d527SKevin Lo 0 7722b120974SPeter Wemm }; 7732b120974SPeter Wemm 7742b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 77511edc477SEd Maste MODULE_VERSION(if_vlan, 3); 7762cc2df49SGarrett Wollman 777ccf7ba97SMarko Zec #ifdef VIMAGE 778ccf7ba97SMarko Zec static void 779ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused) 780ccf7ba97SMarko Zec { 781ccf7ba97SMarko Zec 78242a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 78342a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 784ccf7ba97SMarko Zec V_vlan_cloner = vlan_cloner; 785ccf7ba97SMarko Zec } 786ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 787ccf7ba97SMarko Zec vnet_vlan_init, NULL); 788ccf7ba97SMarko Zec 789ccf7ba97SMarko Zec static void 790ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused) 791ccf7ba97SMarko Zec { 792ccf7ba97SMarko Zec 79342a58907SGleb Smirnoff if_clone_detach(V_vlan_cloner); 794ccf7ba97SMarko Zec } 795ccf7ba97SMarko Zec VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, 796ccf7ba97SMarko Zec vnet_vlan_uninit, NULL); 797ccf7ba97SMarko Zec #endif 798ccf7ba97SMarko Zec 799f889d2efSBrooks Davis static struct ifnet * 8007983103aSRobert Watson vlan_clone_match_ethervid(struct if_clone *ifc, const char *name, int *vidp) 8019d4fe4b2SBrooks Davis { 802f889d2efSBrooks Davis const char *cp; 803f889d2efSBrooks Davis struct ifnet *ifp; 8047983103aSRobert Watson int vid; 805f889d2efSBrooks Davis 806f889d2efSBrooks Davis /* Check for <etherif>.<vlan> style interface names. */ 80777dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 808603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 809e4cd31ddSJeff Roberson /* 810e4cd31ddSJeff Roberson * We can handle non-ethernet hardware types as long as 811e4cd31ddSJeff Roberson * they handle the tagging and headers themselves. 812e4cd31ddSJeff Roberson */ 813e4cd31ddSJeff Roberson if (ifp->if_type != IFT_ETHER && 814e4cd31ddSJeff Roberson (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 815f889d2efSBrooks Davis continue; 816f889d2efSBrooks Davis if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0) 817f889d2efSBrooks Davis continue; 818f889d2efSBrooks Davis cp = name + strlen(ifp->if_xname); 819fb92ad4aSJohn Baldwin if (*cp++ != '.') 820f889d2efSBrooks Davis continue; 821fb92ad4aSJohn Baldwin if (*cp == '\0') 822f889d2efSBrooks Davis continue; 8237983103aSRobert Watson vid = 0; 824fb92ad4aSJohn Baldwin for(; *cp >= '0' && *cp <= '9'; cp++) 8257983103aSRobert Watson vid = (vid * 10) + (*cp - '0'); 826fb92ad4aSJohn Baldwin if (*cp != '\0') 827fb92ad4aSJohn Baldwin continue; 8287983103aSRobert Watson if (vidp != NULL) 8297983103aSRobert Watson *vidp = vid; 830f889d2efSBrooks Davis break; 831f889d2efSBrooks Davis } 83277dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 833f889d2efSBrooks Davis 83415a66c21SBruce M Simpson return (ifp); 835f889d2efSBrooks Davis } 836f889d2efSBrooks Davis 837f889d2efSBrooks Davis static int 838f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 839f889d2efSBrooks Davis { 840f889d2efSBrooks Davis const char *cp; 841f889d2efSBrooks Davis 8427983103aSRobert Watson if (vlan_clone_match_ethervid(ifc, name, NULL) != NULL) 843f889d2efSBrooks Davis return (1); 844f889d2efSBrooks Davis 84542a58907SGleb Smirnoff if (strncmp(vlanname, name, strlen(vlanname)) != 0) 846f889d2efSBrooks Davis return (0); 847f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 848f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 849f889d2efSBrooks Davis return (0); 850f889d2efSBrooks Davis } 851f889d2efSBrooks Davis 852f889d2efSBrooks Davis return (1); 853f889d2efSBrooks Davis } 854f889d2efSBrooks Davis 855f889d2efSBrooks Davis static int 8566b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 857f889d2efSBrooks Davis { 858f889d2efSBrooks Davis char *dp; 859f889d2efSBrooks Davis int wildcard; 860f889d2efSBrooks Davis int unit; 861f889d2efSBrooks Davis int error; 8627983103aSRobert Watson int vid; 863f889d2efSBrooks Davis int ethertag; 8649d4fe4b2SBrooks Davis struct ifvlan *ifv; 8659d4fe4b2SBrooks Davis struct ifnet *ifp; 866f889d2efSBrooks Davis struct ifnet *p; 8673ba24fdeSJohn Baldwin struct ifaddr *ifa; 8683ba24fdeSJohn Baldwin struct sockaddr_dl *sdl; 8696b7330e2SSam Leffler struct vlanreq vlr; 87065239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 871f889d2efSBrooks Davis 8726b7330e2SSam Leffler /* 8736b7330e2SSam Leffler * There are 3 (ugh) ways to specify the cloned device: 8746b7330e2SSam Leffler * o pass a parameter block with the clone request. 8756b7330e2SSam Leffler * o specify parameters in the text of the clone device name 8766b7330e2SSam Leffler * o specify no parameters and get an unattached device that 8776b7330e2SSam Leffler * must be configured separately. 8786b7330e2SSam Leffler * The first technique is preferred; the latter two are 8796b7330e2SSam Leffler * supported for backwards compatibilty. 8807983103aSRobert Watson * 8817983103aSRobert Watson * XXXRW: Note historic use of the word "tag" here. New ioctls may be 8827983103aSRobert Watson * called for. 8836b7330e2SSam Leffler */ 8846b7330e2SSam Leffler if (params) { 8856b7330e2SSam Leffler error = copyin(params, &vlr, sizeof(vlr)); 8866b7330e2SSam Leffler if (error) 8876b7330e2SSam Leffler return error; 8886b7330e2SSam Leffler p = ifunit(vlr.vlr_parent); 8896b7330e2SSam Leffler if (p == NULL) 8906b7330e2SSam Leffler return ENXIO; 8916b7330e2SSam Leffler /* 8927983103aSRobert Watson * Don't let the caller set up a VLAN VID with 8936b7330e2SSam Leffler * anything except VLID bits. 8946b7330e2SSam Leffler */ 8956b7330e2SSam Leffler if (vlr.vlr_tag & ~EVL_VLID_MASK) 8966b7330e2SSam Leffler return (EINVAL); 8976b7330e2SSam Leffler error = ifc_name2unit(name, &unit); 8986b7330e2SSam Leffler if (error != 0) 8996b7330e2SSam Leffler return (error); 9006b7330e2SSam Leffler 9016b7330e2SSam Leffler ethertag = 1; 9027983103aSRobert Watson vid = vlr.vlr_tag; 9036b7330e2SSam Leffler wildcard = (unit < 0); 9047983103aSRobert Watson } else if ((p = vlan_clone_match_ethervid(ifc, name, &vid)) != NULL) { 905f889d2efSBrooks Davis ethertag = 1; 906f889d2efSBrooks Davis unit = -1; 907f889d2efSBrooks Davis wildcard = 0; 908f889d2efSBrooks Davis 909f889d2efSBrooks Davis /* 9107983103aSRobert Watson * Don't let the caller set up a VLAN VID with 911f889d2efSBrooks Davis * anything except VLID bits. 912f889d2efSBrooks Davis */ 9137983103aSRobert Watson if (vid & ~EVL_VLID_MASK) 914f889d2efSBrooks Davis return (EINVAL); 915f889d2efSBrooks Davis } else { 916f889d2efSBrooks Davis ethertag = 0; 917f889d2efSBrooks Davis 918f889d2efSBrooks Davis error = ifc_name2unit(name, &unit); 919f889d2efSBrooks Davis if (error != 0) 920f889d2efSBrooks Davis return (error); 921f889d2efSBrooks Davis 922f889d2efSBrooks Davis wildcard = (unit < 0); 923f889d2efSBrooks Davis } 924f889d2efSBrooks Davis 925f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 926f889d2efSBrooks Davis if (error != 0) 927f889d2efSBrooks Davis return (error); 928f889d2efSBrooks Davis 929f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 930f889d2efSBrooks Davis if (wildcard) { 931f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 932f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 933f889d2efSBrooks Davis len - (dp-name) - 1) { 934f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 935f889d2efSBrooks Davis } 936f889d2efSBrooks Davis } 9379d4fe4b2SBrooks Davis 938a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 939fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 940fc74a9f9SBrooks Davis if (ifp == NULL) { 941fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 942fc74a9f9SBrooks Davis free(ifv, M_VLAN); 943fc74a9f9SBrooks Davis return (ENOSPC); 944fc74a9f9SBrooks Davis } 9459d4fe4b2SBrooks Davis SLIST_INIT(&ifv->vlan_mc_listhead); 9469d4fe4b2SBrooks Davis 9479d4fe4b2SBrooks Davis ifp->if_softc = ifv; 948f889d2efSBrooks Davis /* 949cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 950f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 951f889d2efSBrooks Davis */ 952f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 95342a58907SGleb Smirnoff ifp->if_dname = vlanname; 954f889d2efSBrooks Davis ifp->if_dunit = unit; 9559d4fe4b2SBrooks Davis /* NB: flags are not set here */ 9569d4fe4b2SBrooks Davis ifp->if_linkmib = &ifv->ifv_mib; 95715a66c21SBruce M Simpson ifp->if_linkmiblen = sizeof(ifv->ifv_mib); 9589d4fe4b2SBrooks Davis /* NB: mtu is not set here */ 9599d4fe4b2SBrooks Davis 960114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 961d9b1d615SJohn Baldwin ifp->if_transmit = vlan_transmit; 962d9b1d615SJohn Baldwin ifp->if_qflush = vlan_qflush; 9639d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 96464a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 965fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 9669d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 967211f625aSBill Fenner ifp->if_baudrate = 0; 968a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 969a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 9703ba24fdeSJohn Baldwin ifa = ifp->if_addr; 9713ba24fdeSJohn Baldwin sdl = (struct sockaddr_dl *)ifa->ifa_addr; 9723ba24fdeSJohn Baldwin sdl->sdl_type = IFT_L2VLAN; 9739d4fe4b2SBrooks Davis 974f889d2efSBrooks Davis if (ethertag) { 9757983103aSRobert Watson error = vlan_config(ifv, p, vid); 976f889d2efSBrooks Davis if (error != 0) { 977f889d2efSBrooks Davis /* 97828cc4d37SJohn Baldwin * Since we've partially failed, we need to back 979f889d2efSBrooks Davis * out all the way, otherwise userland could get 980f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 981f889d2efSBrooks Davis */ 982f889d2efSBrooks Davis ether_ifdetach(ifp); 983249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 9844b22573aSBrooks Davis if_free(ifp); 98596c8ef3aSMaxim Konovalov ifc_free_unit(ifc, unit); 986f889d2efSBrooks Davis free(ifv, M_VLAN); 987f889d2efSBrooks Davis 988f889d2efSBrooks Davis return (error); 989f889d2efSBrooks Davis } 990f889d2efSBrooks Davis 9911cf236fbSYaroslav Tykhiy /* Update flags on the parent, if necessary. */ 9921cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 1); 993f889d2efSBrooks Davis } 994f889d2efSBrooks Davis 9959d4fe4b2SBrooks Davis return (0); 9969d4fe4b2SBrooks Davis } 9979d4fe4b2SBrooks Davis 998f889d2efSBrooks Davis static int 999f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 10009d4fe4b2SBrooks Davis { 10019d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 1002114c608cSYaroslav Tykhiy int unit = ifp->if_dunit; 1003b4e9f837SBrooks Davis 1004249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 1005249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 10064b22573aSBrooks Davis if_free(ifp); 10079d4fe4b2SBrooks Davis free(ifv, M_VLAN); 1008b4e9f837SBrooks Davis ifc_free_unit(ifc, unit); 1009b4e9f837SBrooks Davis 1010f889d2efSBrooks Davis return (0); 10119d4fe4b2SBrooks Davis } 10129d4fe4b2SBrooks Davis 101315a66c21SBruce M Simpson /* 101415a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 101515a66c21SBruce M Simpson */ 10162cc2df49SGarrett Wollman static void 1017114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 10182cc2df49SGarrett Wollman { 10192cc2df49SGarrett Wollman } 10202cc2df49SGarrett Wollman 10216d3a3ab7SGleb Smirnoff /* 1022d9b1d615SJohn Baldwin * The if_transmit method for vlan(4) interface. 10236d3a3ab7SGleb Smirnoff */ 1024d9b1d615SJohn Baldwin static int 1025d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m) 10262cc2df49SGarrett Wollman { 10272cc2df49SGarrett Wollman struct ifvlan *ifv; 10282cc2df49SGarrett Wollman struct ifnet *p; 10291ad7a257SPyun YongHyeon int error, len, mcast; 10302cc2df49SGarrett Wollman 10312cc2df49SGarrett Wollman ifv = ifp->if_softc; 103275ee267cSGleb Smirnoff p = PARENT(ifv); 10331ad7a257SPyun YongHyeon len = m->m_pkthdr.len; 10341ad7a257SPyun YongHyeon mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 10352cc2df49SGarrett Wollman 1036a3814acfSSam Leffler BPF_MTAP(ifp, m); 10372cc2df49SGarrett Wollman 1038f731f104SBill Paul /* 1039d9b1d615SJohn Baldwin * Do not run parent's if_transmit() if the parent is not up, 104024993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 104124993214SYaroslav Tykhiy */ 10422dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 104324993214SYaroslav Tykhiy m_freem(m); 10441ad7a257SPyun YongHyeon ifp->if_oerrors++; 1045d9b1d615SJohn Baldwin return (0); 104624993214SYaroslav Tykhiy } 104724993214SYaroslav Tykhiy 104824993214SYaroslav Tykhiy /* 1049f6e5e0adSYaroslav Tykhiy * Pad the frame to the minimum size allowed if told to. 1050f6e5e0adSYaroslav Tykhiy * This option is in accord with IEEE Std 802.1Q, 2003 Ed., 1051f6e5e0adSYaroslav Tykhiy * paragraph C.4.4.3.b. It can help to work around buggy 1052f6e5e0adSYaroslav Tykhiy * bridges that violate paragraph C.4.4.3.a from the same 1053f6e5e0adSYaroslav Tykhiy * document, i.e., fail to pad short frames after untagging. 1054f6e5e0adSYaroslav Tykhiy * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but 1055f6e5e0adSYaroslav Tykhiy * untagging it will produce a 62-byte frame, which is a runt 1056f6e5e0adSYaroslav Tykhiy * and requires padding. There are VLAN-enabled network 1057f6e5e0adSYaroslav Tykhiy * devices that just discard such runts instead or mishandle 1058f6e5e0adSYaroslav Tykhiy * them somehow. 1059f6e5e0adSYaroslav Tykhiy */ 1060e4cd31ddSJeff Roberson if (soft_pad && p->if_type == IFT_ETHER) { 1061f6e5e0adSYaroslav Tykhiy static char pad[8]; /* just zeros */ 1062f6e5e0adSYaroslav Tykhiy int n; 1063f6e5e0adSYaroslav Tykhiy 1064f6e5e0adSYaroslav Tykhiy for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len; 1065f6e5e0adSYaroslav Tykhiy n > 0; n -= sizeof(pad)) 1066f6e5e0adSYaroslav Tykhiy if (!m_append(m, min(n, sizeof(pad)), pad)) 1067f6e5e0adSYaroslav Tykhiy break; 1068f6e5e0adSYaroslav Tykhiy 1069f6e5e0adSYaroslav Tykhiy if (n > 0) { 1070f6e5e0adSYaroslav Tykhiy if_printf(ifp, "cannot pad short frame\n"); 1071f6e5e0adSYaroslav Tykhiy ifp->if_oerrors++; 1072f6e5e0adSYaroslav Tykhiy m_freem(m); 1073d9b1d615SJohn Baldwin return (0); 1074f6e5e0adSYaroslav Tykhiy } 1075f6e5e0adSYaroslav Tykhiy } 1076f6e5e0adSYaroslav Tykhiy 1077f6e5e0adSYaroslav Tykhiy /* 1078a3814acfSSam Leffler * If underlying interface can do VLAN tag insertion itself, 1079a3814acfSSam Leffler * just pass the packet along. However, we need some way to 1080a3814acfSSam Leffler * tell the interface where the packet came from so that it 1081a3814acfSSam Leffler * knows how to find the VLAN tag to use, so we attach a 1082a3814acfSSam Leffler * packet tag that holds it. 1083f731f104SBill Paul */ 1084b08347a0SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_HWTAGGING) { 10857983103aSRobert Watson m->m_pkthdr.ether_vtag = ifv->ifv_vid; 1086ba26134bSGleb Smirnoff m->m_flags |= M_VLANTAG; 1087f731f104SBill Paul } else { 10887983103aSRobert Watson m = ether_vlanencap(m, ifv->ifv_vid); 10894af90a4dSMatthew N. Dodd if (m == NULL) { 1090d9b1d615SJohn Baldwin if_printf(ifp, "unable to prepend VLAN header\n"); 109126f9b263SRuslan Ermilov ifp->if_oerrors++; 1092d9b1d615SJohn Baldwin return (0); 10934af90a4dSMatthew N. Dodd } 1094f731f104SBill Paul } 10952cc2df49SGarrett Wollman 10962cc2df49SGarrett Wollman /* 10972cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 10982cc2df49SGarrett Wollman */ 1099aea78d20SKip Macy error = (p->if_transmit)(p, m); 11001ad7a257SPyun YongHyeon if (!error) { 1101f731f104SBill Paul ifp->if_opackets++; 11021ad7a257SPyun YongHyeon ifp->if_omcasts += mcast; 11031ad7a257SPyun YongHyeon ifp->if_obytes += len; 11041ad7a257SPyun YongHyeon } else 1105df5e1987SJonathan Lemon ifp->if_oerrors++; 1106d9b1d615SJohn Baldwin return (error); 11072cc2df49SGarrett Wollman } 1108d9b1d615SJohn Baldwin 1109d9b1d615SJohn Baldwin /* 1110d9b1d615SJohn Baldwin * The ifp->if_qflush entry point for vlan(4) is a no-op. 1111d9b1d615SJohn Baldwin */ 1112d9b1d615SJohn Baldwin static void 1113d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused) 1114d9b1d615SJohn Baldwin { 1115f731f104SBill Paul } 1116f731f104SBill Paul 1117a3814acfSSam Leffler static void 1118a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 1119f731f104SBill Paul { 112075ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1121f731f104SBill Paul struct ifvlan *ifv; 11227983103aSRobert Watson uint16_t vid; 112375ee267cSGleb Smirnoff 112475ee267cSGleb Smirnoff KASSERT(trunk != NULL, ("%s: no trunk", __func__)); 1125a3814acfSSam Leffler 1126f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 1127a3814acfSSam Leffler /* 112814e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 1129a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 1130a3814acfSSam Leffler */ 11317983103aSRobert Watson vid = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag); 11326ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 1133a3814acfSSam Leffler } else { 113475ee267cSGleb Smirnoff struct ether_vlan_header *evl; 113575ee267cSGleb Smirnoff 113614e98256SYaroslav Tykhiy /* 113714e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 113814e98256SYaroslav Tykhiy */ 1139a3814acfSSam Leffler switch (ifp->if_type) { 1140a3814acfSSam Leffler case IFT_ETHER: 1141a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 1142a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 1143a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 1144a3814acfSSam Leffler return; 1145a3814acfSSam Leffler } 1146a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 11477983103aSRobert Watson vid = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 1148db8b5973SYaroslav Tykhiy 1149db8b5973SYaroslav Tykhiy /* 11502dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 11512dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 11522dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 11532dc879b3SYaroslav Tykhiy * type field is already in place. 1154db8b5973SYaroslav Tykhiy */ 11552dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 11562dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 11572dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 1158a3814acfSSam Leffler break; 11592dc879b3SYaroslav Tykhiy 1160a3814acfSSam Leffler default: 1161db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 116260c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 116360c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 1164a3814acfSSam Leffler #endif 116560c60618SYaroslav Tykhiy m_freem(m); 116660c60618SYaroslav Tykhiy ifp->if_noproto++; 116760c60618SYaroslav Tykhiy return; 1168a3814acfSSam Leffler } 11697a46ec8fSBrooks Davis } 11707a46ec8fSBrooks Davis 117115ed2fa1SYaroslav Tykhiy TRUNK_RLOCK(trunk); 11727983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 11732dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 117475ee267cSGleb Smirnoff TRUNK_RUNLOCK(trunk); 117575ee267cSGleb Smirnoff m_freem(m); 117675ee267cSGleb Smirnoff ifp->if_noproto++; 117775ee267cSGleb Smirnoff return; 117875ee267cSGleb Smirnoff } 117975ee267cSGleb Smirnoff TRUNK_RUNLOCK(trunk); 1180f731f104SBill Paul 1181fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 1182fc74a9f9SBrooks Davis ifv->ifv_ifp->if_ipackets++; 11832cc2df49SGarrett Wollman 1184a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 1185fc74a9f9SBrooks Davis (*ifp->if_input)(ifv->ifv_ifp, m); 11862cc2df49SGarrett Wollman } 11872cc2df49SGarrett Wollman 11882cc2df49SGarrett Wollman static int 11897983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid) 11902cc2df49SGarrett Wollman { 119175ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 11921cf236fbSYaroslav Tykhiy struct ifnet *ifp; 119375ee267cSGleb Smirnoff int error = 0; 11942cc2df49SGarrett Wollman 119575ee267cSGleb Smirnoff /* VID numbers 0x0 and 0xFFF are reserved */ 11967983103aSRobert Watson if (vid == 0 || vid == 0xFFF) 119775ee267cSGleb Smirnoff return (EINVAL); 1198e4cd31ddSJeff Roberson if (p->if_type != IFT_ETHER && 1199e4cd31ddSJeff Roberson (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 120015a66c21SBruce M Simpson return (EPROTONOSUPPORT); 120164a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 120264a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 120375ee267cSGleb Smirnoff if (ifv->ifv_trunk) 120415a66c21SBruce M Simpson return (EBUSY); 12052cc2df49SGarrett Wollman 120675ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 120775ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 120875ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 120975ee267cSGleb Smirnoff vlan_inithash(trunk); 121005a2398fSGleb Smirnoff VLAN_LOCK(); 121105a2398fSGleb Smirnoff if (p->if_vlantrunk != NULL) { 121205a2398fSGleb Smirnoff /* A race that that is very unlikely to be hit. */ 121305a2398fSGleb Smirnoff vlan_freehash(trunk); 121405a2398fSGleb Smirnoff free(trunk, M_VLAN); 121505a2398fSGleb Smirnoff goto exists; 121605a2398fSGleb Smirnoff } 121775ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 121875ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 121975ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 122075ee267cSGleb Smirnoff trunk->parent = p; 122175ee267cSGleb Smirnoff } else { 122275ee267cSGleb Smirnoff VLAN_LOCK(); 122375ee267cSGleb Smirnoff exists: 122475ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 122575ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 122675ee267cSGleb Smirnoff } 122775ee267cSGleb Smirnoff 12287983103aSRobert Watson ifv->ifv_vid = vid; /* must set this before vlan_inshash() */ 122975ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 123075ee267cSGleb Smirnoff if (error) 123175ee267cSGleb Smirnoff goto done; 123273f2233dSYaroslav Tykhiy ifv->ifv_proto = ETHERTYPE_VLAN; 1233a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1234a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 12351cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1236a3814acfSSam Leffler 1237a3814acfSSam Leffler /* 1238a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1239a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1240656acce4SYaroslav Tykhiy * use it. 1241a3814acfSSam Leffler */ 1242656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1243656acce4SYaroslav Tykhiy /* 1244656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1245656acce4SYaroslav Tykhiy * handle extended frames. 1246656acce4SYaroslav Tykhiy */ 1247a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1248656acce4SYaroslav Tykhiy } else { 1249a3814acfSSam Leffler /* 1250a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1251a3814acfSSam Leffler * makes us incompatible with strictly compliant 1252a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1253a3814acfSSam Leffler * the feature with other NetBSD implementations, 1254a3814acfSSam Leffler * which might still be useful. 1255a3814acfSSam Leffler */ 1256a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1257a3814acfSSam Leffler } 1258a3814acfSSam Leffler 125975ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 12601cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 1261e4cd31ddSJeff Roberson /* 1262e4cd31ddSJeff Roberson * Initialize fields from our parent. This duplicates some 1263e4cd31ddSJeff Roberson * work with ether_ifattach() but allows for non-ethernet 1264e4cd31ddSJeff Roberson * interfaces to also work. 1265e4cd31ddSJeff Roberson */ 12661cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 126775ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 1268e4cd31ddSJeff Roberson ifp->if_output = p->if_output; 1269e4cd31ddSJeff Roberson ifp->if_input = p->if_input; 1270e4cd31ddSJeff Roberson ifp->if_resolvemulti = p->if_resolvemulti; 1271e4cd31ddSJeff Roberson ifp->if_addrlen = p->if_addrlen; 1272e4cd31ddSJeff Roberson ifp->if_broadcastaddr = p->if_broadcastaddr; 1273e4cd31ddSJeff Roberson 12742cc2df49SGarrett Wollman /* 127524993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 127624993214SYaroslav Tykhiy * Other flags are none of our business. 12772cc2df49SGarrett Wollman */ 127864a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 12791cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 12801cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 12811cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 12821cf236fbSYaroslav Tykhiy 12831cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 12842cc2df49SGarrett Wollman 128575ee267cSGleb Smirnoff vlan_capabilities(ifv); 1286a3814acfSSam Leffler 1287a3814acfSSam Leffler /* 1288e4cd31ddSJeff Roberson * Set up our interface address to reflect the underlying 12892cc2df49SGarrett Wollman * physical interface's. 12902cc2df49SGarrett Wollman */ 1291e4cd31ddSJeff Roberson bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1292e4cd31ddSJeff Roberson ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1293e4cd31ddSJeff Roberson p->if_addrlen; 12941b2a4f7aSBill Fenner 12951b2a4f7aSBill Fenner /* 12961b2a4f7aSBill Fenner * Configure multicast addresses that may already be 12971b2a4f7aSBill Fenner * joined on the vlan device. 12981b2a4f7aSBill Fenner */ 12991cf236fbSYaroslav Tykhiy (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */ 13002ada9747SYaroslav Tykhiy 13012ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 13022ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 130375ee267cSGleb Smirnoff done: 130475ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 1305c725524cSJack F Vogel if (error == 0) 13067983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid); 130775ee267cSGleb Smirnoff VLAN_UNLOCK(); 130875ee267cSGleb Smirnoff 130975ee267cSGleb Smirnoff return (error); 13102cc2df49SGarrett Wollman } 13112cc2df49SGarrett Wollman 13126f359e28SJohn Baldwin static void 1313f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1314f731f104SBill Paul { 13155cb8c31aSYaroslav Tykhiy 13165cb8c31aSYaroslav Tykhiy VLAN_LOCK(); 131728cc4d37SJohn Baldwin vlan_unconfig_locked(ifp, 0); 13185cb8c31aSYaroslav Tykhiy VLAN_UNLOCK(); 13195cb8c31aSYaroslav Tykhiy } 13205cb8c31aSYaroslav Tykhiy 13216f359e28SJohn Baldwin static void 132228cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing) 13235cb8c31aSYaroslav Tykhiy { 132475ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1325f731f104SBill Paul struct vlan_mc_entry *mc; 1326f731f104SBill Paul struct ifvlan *ifv; 1327c725524cSJack F Vogel struct ifnet *parent; 132828cc4d37SJohn Baldwin int error; 1329f731f104SBill Paul 13305cb8c31aSYaroslav Tykhiy VLAN_LOCK_ASSERT(); 13314faedfe8SSam Leffler 1332f731f104SBill Paul ifv = ifp->if_softc; 133375ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 133422893351SJack F Vogel parent = NULL; 1335f731f104SBill Paul 133622893351SJack F Vogel if (trunk != NULL) { 133775ee267cSGleb Smirnoff 133875ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 133922893351SJack F Vogel parent = trunk->parent; 13401b2a4f7aSBill Fenner 1341f731f104SBill Paul /* 1342f731f104SBill Paul * Since the interface is being unconfigured, we need to 1343f731f104SBill Paul * empty the list of multicast groups that we may have joined 13441b2a4f7aSBill Fenner * while we were alive from the parent's list. 1345f731f104SBill Paul */ 1346c0cb022bSYaroslav Tykhiy while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 13476f359e28SJohn Baldwin /* 134828cc4d37SJohn Baldwin * If the parent interface is being detached, 1349b90dde2fSJohn Baldwin * all its multicast addresses have already 135028cc4d37SJohn Baldwin * been removed. Warn about errors if 135128cc4d37SJohn Baldwin * if_delmulti() does fail, but don't abort as 135228cc4d37SJohn Baldwin * all callers expect vlan destruction to 135328cc4d37SJohn Baldwin * succeed. 13546f359e28SJohn Baldwin */ 135528cc4d37SJohn Baldwin if (!departing) { 135628cc4d37SJohn Baldwin error = if_delmulti(parent, 1357e4cd31ddSJeff Roberson (struct sockaddr *)&mc->mc_addr); 135828cc4d37SJohn Baldwin if (error) 135928cc4d37SJohn Baldwin if_printf(ifp, 136028cc4d37SJohn Baldwin "Failed to delete multicast address from parent: %d\n", 136128cc4d37SJohn Baldwin error); 136228cc4d37SJohn Baldwin } 1363f731f104SBill Paul SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 13649d4fe4b2SBrooks Davis free(mc, M_VLAN); 1365f731f104SBill Paul } 1366a3814acfSSam Leffler 13671cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 136875ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 136975ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 137075ee267cSGleb Smirnoff 137175ee267cSGleb Smirnoff /* 137275ee267cSGleb Smirnoff * Check if we were the last. 137375ee267cSGleb Smirnoff */ 137475ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 137515ed2fa1SYaroslav Tykhiy trunk->parent->if_vlantrunk = NULL; 137675ee267cSGleb Smirnoff /* 137775ee267cSGleb Smirnoff * XXXGL: If some ithread has already entered 137875ee267cSGleb Smirnoff * vlan_input() and is now blocked on the trunk 137975ee267cSGleb Smirnoff * lock, then it should preempt us right after 138075ee267cSGleb Smirnoff * unlock and finish its work. Then we will acquire 138175ee267cSGleb Smirnoff * lock again in trunk_destroy(). 138275ee267cSGleb Smirnoff */ 138375ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 138475ee267cSGleb Smirnoff trunk_destroy(trunk); 138575ee267cSGleb Smirnoff } else 138675ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 13871b2a4f7aSBill Fenner } 1388f731f104SBill Paul 1389f731f104SBill Paul /* Disconnect from parent. */ 13901cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 13911cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 13925cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 13935cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 13945cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1395f731f104SBill Paul 139622893351SJack F Vogel /* 139722893351SJack F Vogel * Only dispatch an event if vlan was 139822893351SJack F Vogel * attached, otherwise there is nothing 139922893351SJack F Vogel * to cleanup anyway. 140022893351SJack F Vogel */ 140122893351SJack F Vogel if (parent != NULL) 14027983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid); 1403f731f104SBill Paul } 1404f731f104SBill Paul 14051cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1406f731f104SBill Paul static int 14071cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 14081cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1409a3814acfSSam Leffler { 14101cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 14111cf236fbSYaroslav Tykhiy int error; 1412a3814acfSSam Leffler 14131cf236fbSYaroslav Tykhiy /* XXX VLAN_LOCK_ASSERT(); */ 1414a3814acfSSam Leffler 14151cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 14161cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 14171cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 14181cf236fbSYaroslav Tykhiy 14191cf236fbSYaroslav Tykhiy /* 14201cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 14211cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 14221cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 14231cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 14241cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 14251cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 14261cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 14271cf236fbSYaroslav Tykhiy */ 14281cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 142975ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 14301cf236fbSYaroslav Tykhiy if (error) 1431a3814acfSSam Leffler return (error); 14321cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 14331cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 14341cf236fbSYaroslav Tykhiy } 14351cf236fbSYaroslav Tykhiy return (0); 14361cf236fbSYaroslav Tykhiy } 14371cf236fbSYaroslav Tykhiy 14381cf236fbSYaroslav Tykhiy /* 14391cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 14401cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 14411cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 14421cf236fbSYaroslav Tykhiy */ 14431cf236fbSYaroslav Tykhiy static int 14441cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 14451cf236fbSYaroslav Tykhiy { 14461cf236fbSYaroslav Tykhiy int error, i; 14471cf236fbSYaroslav Tykhiy 14481cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 14491cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 14501cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 14511cf236fbSYaroslav Tykhiy if (error) 14521cf236fbSYaroslav Tykhiy return (error); 14531cf236fbSYaroslav Tykhiy } 14541cf236fbSYaroslav Tykhiy return (0); 1455a3814acfSSam Leffler } 1456a3814acfSSam Leffler 1457127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1458127d7b2dSAndre Oppermann static void 1459a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp) 1460127d7b2dSAndre Oppermann { 146175ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1462127d7b2dSAndre Oppermann struct ifvlan *ifv; 146375ee267cSGleb Smirnoff int i; 1464127d7b2dSAndre Oppermann 146575ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 146675ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 146715ed2fa1SYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 146875ee267cSGleb Smirnoff if (trunk->vlans[i] != NULL) { 146975ee267cSGleb Smirnoff ifv = trunk->vlans[i]; 147075ee267cSGleb Smirnoff #else 1471aad0be7aSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 1472aad0be7aSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) { 147375ee267cSGleb Smirnoff #endif 1474aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1475fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 147675ee267cSGleb Smirnoff trunk->parent->if_link_state); 1477127d7b2dSAndre Oppermann } 147875ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 147975ee267cSGleb Smirnoff } 148075ee267cSGleb Smirnoff 148175ee267cSGleb Smirnoff static void 148275ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 148375ee267cSGleb Smirnoff { 148475ee267cSGleb Smirnoff struct ifnet *p = PARENT(ifv); 148575ee267cSGleb Smirnoff struct ifnet *ifp = ifv->ifv_ifp; 148675ee267cSGleb Smirnoff 148775ee267cSGleb Smirnoff TRUNK_LOCK_ASSERT(TRUNK(ifv)); 148875ee267cSGleb Smirnoff 148975ee267cSGleb Smirnoff /* 149075ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 149175ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 149275ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 149375ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 149475ee267cSGleb Smirnoff */ 149575ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 149675ee267cSGleb Smirnoff ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM; 149775ee267cSGleb Smirnoff 149875ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 149975ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 150075ee267cSGleb Smirnoff ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM; 15019b76d9cbSPyun YongHyeon ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP | 1502da2299c5SAndre Oppermann CSUM_UDP | CSUM_SCTP | CSUM_FRAGMENT); 150375ee267cSGleb Smirnoff } else { 150475ee267cSGleb Smirnoff ifp->if_capenable = 0; 150575ee267cSGleb Smirnoff ifp->if_hwassist = 0; 150675ee267cSGleb Smirnoff } 15079b76d9cbSPyun YongHyeon /* 15089b76d9cbSPyun YongHyeon * If the parent interface can do TSO on VLANs then 15099b76d9cbSPyun YongHyeon * propagate the hardware-assisted flag. TSO on VLANs 15109b76d9cbSPyun YongHyeon * does not necessarily require hardware VLAN tagging. 15119b76d9cbSPyun YongHyeon */ 15129b76d9cbSPyun YongHyeon if (p->if_capabilities & IFCAP_VLAN_HWTSO) 15139b76d9cbSPyun YongHyeon ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO; 15149b76d9cbSPyun YongHyeon if (p->if_capenable & IFCAP_VLAN_HWTSO) { 15159b76d9cbSPyun YongHyeon ifp->if_capenable |= p->if_capenable & IFCAP_TSO; 15169b76d9cbSPyun YongHyeon ifp->if_hwassist |= p->if_hwassist & CSUM_TSO; 15179b76d9cbSPyun YongHyeon } else { 15189b76d9cbSPyun YongHyeon ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO); 15199b76d9cbSPyun YongHyeon ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO); 15209b76d9cbSPyun YongHyeon } 152109fe6320SNavdeep Parhar 152209fe6320SNavdeep Parhar /* 152309fe6320SNavdeep Parhar * If the parent interface can offload TCP connections over VLANs then 152409fe6320SNavdeep Parhar * propagate its TOE capability to the VLAN interface. 152509fe6320SNavdeep Parhar * 152609fe6320SNavdeep Parhar * All TOE drivers in the tree today can deal with VLANs. If this 152709fe6320SNavdeep Parhar * changes then IFCAP_VLAN_TOE should be promoted to a full capability 152809fe6320SNavdeep Parhar * with its own bit. 152909fe6320SNavdeep Parhar */ 153009fe6320SNavdeep Parhar #define IFCAP_VLAN_TOE IFCAP_TOE 153109fe6320SNavdeep Parhar if (p->if_capabilities & IFCAP_VLAN_TOE) 153209fe6320SNavdeep Parhar ifp->if_capabilities |= p->if_capabilities & IFCAP_TOE; 153309fe6320SNavdeep Parhar if (p->if_capenable & IFCAP_VLAN_TOE) { 153409fe6320SNavdeep Parhar TOEDEV(ifp) = TOEDEV(p); 153509fe6320SNavdeep Parhar ifp->if_capenable |= p->if_capenable & IFCAP_TOE; 153609fe6320SNavdeep Parhar } 153775ee267cSGleb Smirnoff } 153875ee267cSGleb Smirnoff 153975ee267cSGleb Smirnoff static void 154075ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 154175ee267cSGleb Smirnoff { 154275ee267cSGleb Smirnoff struct ifvlantrunk *trunk = ifp->if_vlantrunk; 154375ee267cSGleb Smirnoff struct ifvlan *ifv; 154475ee267cSGleb Smirnoff int i; 154575ee267cSGleb Smirnoff 154675ee267cSGleb Smirnoff TRUNK_LOCK(trunk); 154775ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 154815ed2fa1SYaroslav Tykhiy for (i = 0; i < VLAN_ARRAY_SIZE; i++) 154975ee267cSGleb Smirnoff if (trunk->vlans[i] != NULL) { 155075ee267cSGleb Smirnoff ifv = trunk->vlans[i]; 155175ee267cSGleb Smirnoff #else 155275ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 155375ee267cSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 155475ee267cSGleb Smirnoff #endif 155575ee267cSGleb Smirnoff vlan_capabilities(ifv); 155675ee267cSGleb Smirnoff } 155775ee267cSGleb Smirnoff TRUNK_UNLOCK(trunk); 1558127d7b2dSAndre Oppermann } 1559127d7b2dSAndre Oppermann 1560a3814acfSSam Leffler static int 1561cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 15622cc2df49SGarrett Wollman { 15632cc2df49SGarrett Wollman struct ifnet *p; 15642cc2df49SGarrett Wollman struct ifreq *ifr; 1565e4cd31ddSJeff Roberson struct ifaddr *ifa; 15662cc2df49SGarrett Wollman struct ifvlan *ifv; 15672cc2df49SGarrett Wollman struct vlanreq vlr; 15682cc2df49SGarrett Wollman int error = 0; 15692cc2df49SGarrett Wollman 15702cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 1571e4cd31ddSJeff Roberson ifa = (struct ifaddr *) data; 15722cc2df49SGarrett Wollman ifv = ifp->if_softc; 15732cc2df49SGarrett Wollman 15742cc2df49SGarrett Wollman switch (cmd) { 1575e4cd31ddSJeff Roberson case SIOCSIFADDR: 1576e4cd31ddSJeff Roberson ifp->if_flags |= IFF_UP; 1577e4cd31ddSJeff Roberson #ifdef INET 1578e4cd31ddSJeff Roberson if (ifa->ifa_addr->sa_family == AF_INET) 1579e4cd31ddSJeff Roberson arp_ifinit(ifp, ifa); 1580e4cd31ddSJeff Roberson #endif 1581e4cd31ddSJeff Roberson break; 1582e4cd31ddSJeff Roberson case SIOCGIFADDR: 1583e4cd31ddSJeff Roberson { 1584e4cd31ddSJeff Roberson struct sockaddr *sa; 1585e4cd31ddSJeff Roberson 1586e4cd31ddSJeff Roberson sa = (struct sockaddr *)&ifr->ifr_data; 1587e4cd31ddSJeff Roberson bcopy(IF_LLADDR(ifp), sa->sa_data, ifp->if_addrlen); 1588e4cd31ddSJeff Roberson } 1589e4cd31ddSJeff Roberson break; 1590b3cca108SBill Fenner case SIOCGIFMEDIA: 15914faedfe8SSam Leffler VLAN_LOCK(); 159275ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 1593d8564efdSEd Maste p = PARENT(ifv); 15944faedfe8SSam Leffler VLAN_UNLOCK(); 1595d8564efdSEd Maste error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 1596b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 1597b3cca108SBill Fenner if (error == 0) { 1598b3cca108SBill Fenner struct ifmediareq *ifmr; 1599b3cca108SBill Fenner 1600b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 1601b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1602b3cca108SBill Fenner ifmr->ifm_count = 1; 1603b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 1604b3cca108SBill Fenner ifmr->ifm_ulist, 1605b3cca108SBill Fenner sizeof(int)); 1606b3cca108SBill Fenner } 1607b3cca108SBill Fenner } 16084faedfe8SSam Leffler } else { 16094faedfe8SSam Leffler VLAN_UNLOCK(); 1610b3cca108SBill Fenner error = EINVAL; 16114faedfe8SSam Leffler } 1612b3cca108SBill Fenner break; 1613b3cca108SBill Fenner 1614b3cca108SBill Fenner case SIOCSIFMEDIA: 1615b3cca108SBill Fenner error = EINVAL; 1616b3cca108SBill Fenner break; 1617b3cca108SBill Fenner 16182cc2df49SGarrett Wollman case SIOCSIFMTU: 16192cc2df49SGarrett Wollman /* 16202cc2df49SGarrett Wollman * Set the interface MTU. 16212cc2df49SGarrett Wollman */ 16224faedfe8SSam Leffler VLAN_LOCK(); 162375ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 1624a3814acfSSam Leffler if (ifr->ifr_mtu > 162575ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1626a3814acfSSam Leffler ifr->ifr_mtu < 1627a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 16282cc2df49SGarrett Wollman error = EINVAL; 1629a3814acfSSam Leffler else 16302cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 1631a3814acfSSam Leffler } else 1632a3814acfSSam Leffler error = EINVAL; 16334faedfe8SSam Leffler VLAN_UNLOCK(); 16342cc2df49SGarrett Wollman break; 16352cc2df49SGarrett Wollman 16362cc2df49SGarrett Wollman case SIOCSETVLAN: 1637ccf7ba97SMarko Zec #ifdef VIMAGE 163815f6780eSRobert Watson /* 163915f6780eSRobert Watson * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN 164015f6780eSRobert Watson * interface to be delegated to a jail without allowing the 164115f6780eSRobert Watson * jail to change what underlying interface/VID it is 164215f6780eSRobert Watson * associated with. We are not entirely convinced that this 16435a39f779SRobert Watson * is the right way to accomplish that policy goal. 164415f6780eSRobert Watson */ 1645ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 1646ccf7ba97SMarko Zec error = EPERM; 1647ccf7ba97SMarko Zec break; 1648ccf7ba97SMarko Zec } 1649ccf7ba97SMarko Zec #endif 165015a66c21SBruce M Simpson error = copyin(ifr->ifr_data, &vlr, sizeof(vlr)); 16512cc2df49SGarrett Wollman if (error) 16522cc2df49SGarrett Wollman break; 16532cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 1654f731f104SBill Paul vlan_unconfig(ifp); 16552cc2df49SGarrett Wollman break; 16562cc2df49SGarrett Wollman } 16572cc2df49SGarrett Wollman p = ifunit(vlr.vlr_parent); 16581bdc73d3SEd Maste if (p == NULL) { 16592cc2df49SGarrett Wollman error = ENOENT; 16602cc2df49SGarrett Wollman break; 16612cc2df49SGarrett Wollman } 1662fb88a3e0SBill Paul /* 16637983103aSRobert Watson * Don't let the caller set up a VLAN VID with 1664fb88a3e0SBill Paul * anything except VLID bits. 1665fb88a3e0SBill Paul */ 1666fb88a3e0SBill Paul if (vlr.vlr_tag & ~EVL_VLID_MASK) { 1667fb88a3e0SBill Paul error = EINVAL; 1668fb88a3e0SBill Paul break; 1669fb88a3e0SBill Paul } 167075ee267cSGleb Smirnoff error = vlan_config(ifv, p, vlr.vlr_tag); 167175ee267cSGleb Smirnoff if (error) 16722cc2df49SGarrett Wollman break; 1673a3814acfSSam Leffler 16741cf236fbSYaroslav Tykhiy /* Update flags on the parent, if necessary. */ 16751cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 1); 16762cc2df49SGarrett Wollman break; 16772cc2df49SGarrett Wollman 16782cc2df49SGarrett Wollman case SIOCGETVLAN: 1679ccf7ba97SMarko Zec #ifdef VIMAGE 1680ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 1681ccf7ba97SMarko Zec error = EPERM; 1682ccf7ba97SMarko Zec break; 1683ccf7ba97SMarko Zec } 1684ccf7ba97SMarko Zec #endif 168515a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 16864faedfe8SSam Leffler VLAN_LOCK(); 168775ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 168875ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 16899bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 16907983103aSRobert Watson vlr.vlr_tag = ifv->ifv_vid; 16912cc2df49SGarrett Wollman } 16924faedfe8SSam Leffler VLAN_UNLOCK(); 169315a66c21SBruce M Simpson error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 16942cc2df49SGarrett Wollman break; 16952cc2df49SGarrett Wollman 16962cc2df49SGarrett Wollman case SIOCSIFFLAGS: 16972cc2df49SGarrett Wollman /* 16981cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 16991cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 17002cc2df49SGarrett Wollman */ 170175ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 17021cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 17032cc2df49SGarrett Wollman break; 1704a3814acfSSam Leffler 1705f731f104SBill Paul case SIOCADDMULTI: 1706f731f104SBill Paul case SIOCDELMULTI: 170775ee267cSGleb Smirnoff /* 170875ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 170975ee267cSGleb Smirnoff * when we do. 171075ee267cSGleb Smirnoff */ 171175ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 1712f731f104SBill Paul error = vlan_setmulti(ifp); 1713f731f104SBill Paul break; 171475ee267cSGleb Smirnoff 17152cc2df49SGarrett Wollman default: 1716e4cd31ddSJeff Roberson error = EINVAL; 1717e4cd31ddSJeff Roberson break; 17182cc2df49SGarrett Wollman } 171915a66c21SBruce M Simpson 172015a66c21SBruce M Simpson return (error); 17212cc2df49SGarrett Wollman } 1722