if_vlan.c revision 252828
119370Spst/*- 219370Spst * Copyright 1998 Massachusetts Institute of Technology 3130803Smarcel * 4130803Smarcel * Permission to use, copy, modify, and distribute this software and 5130803Smarcel * its documentation for any purpose and without fee is hereby 698944Sobrien * granted, provided that both the above copyright notice and this 719370Spst * permission notice appear in all copies, that both the above 898944Sobrien * copyright notice and this permission notice appear in all 998944Sobrien * supporting documentation, and that the name of M.I.T. not be used 1098944Sobrien * in advertising or publicity pertaining to distribution of the 1198944Sobrien * software without specific, written prior permission. M.I.T. makes 1219370Spst * no representations about the suitability of this software for any 1398944Sobrien * purpose. It is provided "as is" without express or implied 1498944Sobrien * warranty. 1598944Sobrien * 1698944Sobrien * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 1719370Spst * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 1898944Sobrien * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 1998944Sobrien * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 2098944Sobrien * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2198944Sobrien * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2219370Spst * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 2319370Spst * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24130803Smarcel * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2519370Spst * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 2619370Spst * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2719370Spst * SUCH DAMAGE. 2819370Spst */ 2919370Spst 3019370Spst/* 3119370Spst * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 3298944Sobrien * Might be extended some day to also handle IEEE 802.1p priority 33130803Smarcel * tagging. This is sort of sneaky in the implementation, since 3498944Sobrien * we need to pretend to be enough of an Ethernet implementation 3519370Spst * to make arp work. The way we do this is by telling everyone 3698944Sobrien * that we are an Ethernet, and then catch the packets that 3798944Sobrien * ether_output() sends to us via if_transmit(), rewrite them for 3898944Sobrien * use by the real outgoing interface, and ask it to send them. 3998944Sobrien */ 4098944Sobrien 4198944Sobrien#include <sys/cdefs.h> 42130803Smarcel__FBSDID("$FreeBSD: stable/9/sys/net/if_vlan.c 252828 2013-07-05 19:36:34Z andre $"); 43130803Smarcel 44130803Smarcel#include "opt_inet.h" 4598944Sobrien#include "opt_vlan.h" 4698944Sobrien 4798944Sobrien#include <sys/param.h> 4898944Sobrien#include <sys/kernel.h> 4998944Sobrien#include <sys/lock.h> 5098944Sobrien#include <sys/malloc.h> 5198944Sobrien#include <sys/mbuf.h> 5298944Sobrien#include <sys/module.h> 5398944Sobrien#include <sys/rwlock.h> 5498944Sobrien#include <sys/queue.h> 5598944Sobrien#include <sys/socket.h> 5698944Sobrien#include <sys/sockio.h> 5798944Sobrien#include <sys/sysctl.h> 5819370Spst#include <sys/systm.h> 5919370Spst#include <sys/sx.h> 6019370Spst 6119370Spst#include <net/bpf.h> 6219370Spst#include <net/ethernet.h> 6319370Spst#include <net/if.h> 6419370Spst#include <net/if_clone.h> 6519370Spst#include <net/if_dl.h> 6619370Spst#include <net/if_types.h> 6719370Spst#include <net/if_vlan_var.h> 6819370Spst#include <net/vnet.h> 6919370Spst 7019370Spst#ifdef INET 7119370Spst#include <netinet/in.h> 7298944Sobrien#include <netinet/if_ether.h> 7398944Sobrien#endif 7498944Sobrien 7519370Spst#define VLANNAME "vlan" 76130803Smarcel#define VLAN_DEF_HWIDTH 4 7719370Spst#define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 7819370Spst 7919370Spst#define UP_AND_RUNNING(ifp) \ 8019370Spst ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 8119370Spst 8219370SpstLIST_HEAD(ifvlanhead, ifvlan); 8319370Spst 8419370Spststruct ifvlantrunk { 8519370Spst struct ifnet *parent; /* parent interface of this trunk */ 8619370Spst struct rwlock rw; 8746283Sdfr#ifdef VLAN_ARRAY 8819370Spst#define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 8919370Spst struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 9019370Spst#else 9119370Spst struct ifvlanhead *hash; /* dynamic hash-list table */ 9219370Spst uint16_t hmask; 9319370Spst uint16_t hwidth; 9419370Spst#endif 9519370Spst int refcnt; 9619370Spst}; 9719370Spst 9819370Spststruct vlan_mc_entry { 9919370Spst struct sockaddr_dl mc_addr; 10019370Spst SLIST_ENTRY(vlan_mc_entry) mc_entries; 10119370Spst}; 10219370Spst 10319370Spststruct ifvlan { 10498944Sobrien struct ifvlantrunk *ifv_trunk; 10519370Spst struct ifnet *ifv_ifp; 10619370Spst void *ifv_cookie; 10798944Sobrien#define TRUNK(ifv) ((ifv)->ifv_trunk) 10898944Sobrien#define PARENT(ifv) ((ifv)->ifv_trunk->parent) 10919370Spst int ifv_pflags; /* special flags we have set on parent */ 11019370Spst struct ifv_linkmib { 11146283Sdfr int ifvm_encaplen; /* encapsulation length */ 11219370Spst int ifvm_mtufudge; /* MTU fudged by this much */ 11319370Spst int ifvm_mintu; /* min transmission unit */ 11419370Spst uint16_t ifvm_proto; /* encapsulation ethertype */ 11519370Spst uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 11698944Sobrien } ifv_mib; 11746283Sdfr SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 11819370Spst#ifndef VLAN_ARRAY 11919370Spst LIST_ENTRY(ifvlan) ifv_list; 12019370Spst#endif 12119370Spst}; 12219370Spst#define ifv_proto ifv_mib.ifvm_proto 12319370Spst#define ifv_tag ifv_mib.ifvm_tag 12498944Sobrien#define ifv_encaplen ifv_mib.ifvm_encaplen 12519370Spst#define ifv_mtufudge ifv_mib.ifvm_mtufudge 12619370Spst#define ifv_mintu ifv_mib.ifvm_mintu 12719370Spst 12819370Spst/* Special flags we should propagate to parent. */ 12919370Spststatic struct { 13019370Spst int flag; 13119370Spst int (*func)(struct ifnet *, int); 13219370Spst} vlan_pflags[] = { 13319370Spst {IFF_PROMISC, ifpromisc}, 13446283Sdfr {IFF_ALLMULTI, if_allmulti}, 13598944Sobrien {0, NULL} 13619370Spst}; 13719370Spst 13819370SpstSYSCTL_DECL(_net_link); 13919370Spststatic SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, 14019370Spst "IEEE 802.1Q VLAN"); 14119370Spststatic SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, 14219370Spst "for consistency"); 14319370Spst 14419370Spststatic int soft_pad = 0; 14519370SpstSYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0, 14619370Spst "pad short frames before tagging"); 14746283Sdfr 14819370Spststatic MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface"); 14919370Spst 15098944Sobrienstatic eventhandler_tag ifdetach_tag; 15119370Spststatic eventhandler_tag iflladdr_tag; 15298944Sobrien 15319370Spst/* 15419370Spst * We have a global mutex, that is used to serialize configuration 15598944Sobrien * changes and isn't used in normal packet delivery. 15698944Sobrien * 15798944Sobrien * We also have a per-trunk rwlock, that is locked shared on packet 15819370Spst * processing and exclusive when configuration is changed. 15919370Spst * 16019370Spst * The VLAN_ARRAY substitutes the dynamic hash with a static array 16119370Spst * with 4096 entries. In theory this can give a boost in processing, 16219370Spst * however on practice it does not. Probably this is because array 16346283Sdfr * is too big to fit into CPU cache. 16419370Spst */ 16519370Spststatic struct sx ifv_lock; 16619370Spst#define VLAN_LOCK_INIT() sx_init(&ifv_lock, "vlan_global") 16746283Sdfr#define VLAN_LOCK_DESTROY() sx_destroy(&ifv_lock) 16819370Spst#define VLAN_LOCK_ASSERT() sx_assert(&ifv_lock, SA_LOCKED) 16919370Spst#define VLAN_LOCK() sx_xlock(&ifv_lock) 17019370Spst#define VLAN_UNLOCK() sx_xunlock(&ifv_lock) 17119370Spst#define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME) 17219370Spst#define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw) 17346283Sdfr#define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw) 17419370Spst#define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw) 17519370Spst#define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED) 17619370Spst#define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw) 17719370Spst#define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw) 17819370Spst#define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED) 17998944Sobrien 18019370Spst#ifndef VLAN_ARRAY 18119370Spststatic void vlan_inithash(struct ifvlantrunk *trunk); 18219370Spststatic void vlan_freehash(struct ifvlantrunk *trunk); 18319370Spststatic int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 18419370Spststatic int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 18519370Spststatic void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 18619370Spststatic __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 18719370Spst uint16_t tag); 18819370Spst#endif 18919370Spststatic void trunk_destroy(struct ifvlantrunk *trunk); 19019370Spst 19146283Sdfrstatic void vlan_init(void *foo); 19246283Sdfrstatic void vlan_input(struct ifnet *ifp, struct mbuf *m); 19319370Spststatic int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 19419370Spststatic void vlan_qflush(struct ifnet *ifp); 19519370Spststatic int vlan_setflag(struct ifnet *ifp, int flag, int status, 19619370Spst int (*func)(struct ifnet *, int)); 19719370Spststatic int vlan_setflags(struct ifnet *ifp, int status); 19846283Sdfrstatic int vlan_setmulti(struct ifnet *ifp); 19919370Spststatic int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 20098944Sobrienstatic void vlan_unconfig(struct ifnet *ifp); 20198944Sobrienstatic void vlan_unconfig_locked(struct ifnet *ifp, int departing); 20219370Spststatic int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag); 20346283Sdfrstatic void vlan_link_state(struct ifnet *ifp); 20419370Spststatic void vlan_capabilities(struct ifvlan *ifv); 20519370Spststatic void vlan_trunk_capabilities(struct ifnet *ifp); 20698944Sobrien 20719370Spststatic struct ifnet *vlan_clone_match_ethertag(struct if_clone *, 20819370Spst const char *, int *); 20919370Spststatic int vlan_clone_match(struct if_clone *, const char *); 21019370Spststatic int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 211130803Smarcelstatic int vlan_clone_destroy(struct if_clone *, struct ifnet *); 21219370Spst 21319370Spststatic void vlan_ifdetach(void *arg, struct ifnet *ifp); 21419370Spststatic void vlan_iflladdr(void *arg, struct ifnet *ifp); 21598944Sobrien 21698944Sobrienstatic struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL, 21798944Sobrien IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy); 21898944Sobrien 21998944Sobrien#ifdef VIMAGE 22019370Spststatic VNET_DEFINE(struct if_clone, vlan_cloner); 22119370Spst#define V_vlan_cloner VNET(vlan_cloner) 22219370Spst#endif 223130803Smarcel 224130803Smarcel#ifndef VLAN_ARRAY 22598944Sobrien#define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 22619370Spst 22719370Spststatic void 22898944Sobrienvlan_inithash(struct ifvlantrunk *trunk) 22919370Spst{ 23019370Spst int i, n; 23119370Spst 23298944Sobrien /* 23319370Spst * The trunk must not be locked here since we call malloc(M_WAITOK). 23446283Sdfr * It is OK in case this function is called before the trunk struct 235242936Semaste * gets hooked up and becomes visible from other threads. 236242936Semaste */ 23719370Spst 23819370Spst KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 23919370Spst ("%s: hash already initialized", __func__)); 24019370Spst 24119370Spst trunk->hwidth = VLAN_DEF_HWIDTH; 24298944Sobrien n = 1 << trunk->hwidth; 24398944Sobrien trunk->hmask = n - 1; 24419370Spst trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 24519370Spst for (i = 0; i < n; i++) 24619370Spst LIST_INIT(&trunk->hash[i]); 24719370Spst} 24819370Spst 24919370Spststatic void 25019370Spstvlan_freehash(struct ifvlantrunk *trunk) 25119370Spst{ 25219370Spst#ifdef INVARIANTS 25319370Spst int i; 25419370Spst 25519370Spst KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 25619370Spst for (i = 0; i < (1 << trunk->hwidth); i++) 25719370Spst KASSERT(LIST_EMPTY(&trunk->hash[i]), 25819370Spst ("%s: hash table not empty", __func__)); 25998944Sobrien#endif 26046283Sdfr free(trunk->hash, M_VLAN); 26119370Spst trunk->hash = NULL; 26219370Spst trunk->hwidth = trunk->hmask = 0; 26319370Spst} 26419370Spst 26519370Spststatic int 26698944Sobrienvlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 26798944Sobrien{ 26898944Sobrien int i, b; 26919370Spst struct ifvlan *ifv2; 27098944Sobrien 27119370Spst TRUNK_LOCK_ASSERT(trunk); 27219370Spst KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 27398944Sobrien 27419370Spst b = 1 << trunk->hwidth; 27519370Spst i = HASH(ifv->ifv_tag, trunk->hmask); 27619370Spst LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 27719370Spst if (ifv->ifv_tag == ifv2->ifv_tag) 27819370Spst return (EEXIST); 27998944Sobrien 28098944Sobrien /* 28198944Sobrien * Grow the hash when the number of vlans exceeds half of the number of 28298944Sobrien * hash buckets squared. This will make the average linked-list length 28398944Sobrien * buckets/2. 28498944Sobrien */ 285242936Semaste if (trunk->refcnt > (b * b) / 2) { 286242936Semaste vlan_growhash(trunk, 1); 28719370Spst i = HASH(ifv->ifv_tag, trunk->hmask); 28819370Spst } 28919370Spst LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 29019370Spst trunk->refcnt++; 29119370Spst 29219370Spst return (0); 29319370Spst} 29419370Spst 29519370Spststatic int 29619370Spstvlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 29719370Spst{ 29819370Spst int i, b; 29919370Spst struct ifvlan *ifv2; 30019370Spst 30198944Sobrien TRUNK_LOCK_ASSERT(trunk); 30298944Sobrien KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 30319370Spst 30498944Sobrien b = 1 << trunk->hwidth; 30519370Spst i = HASH(ifv->ifv_tag, trunk->hmask); 30619370Spst LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 30798944Sobrien if (ifv2 == ifv) { 30898944Sobrien trunk->refcnt--; 30998944Sobrien LIST_REMOVE(ifv2, ifv_list); 31098944Sobrien if (trunk->refcnt < (b * b) / 2) 31198944Sobrien vlan_growhash(trunk, -1); 31298944Sobrien return (0); 31398944Sobrien } 31419370Spst 31519370Spst panic("%s: vlan not found\n", __func__); 31646283Sdfr return (ENOENT); /*NOTREACHED*/ 31719370Spst} 31819370Spst 31919370Spst/* 32019370Spst * Grow the hash larger or smaller if memory permits. 32119370Spst */ 32219370Spststatic void 32346283Sdfrvlan_growhash(struct ifvlantrunk *trunk, int howmuch) 32419370Spst{ 32519370Spst struct ifvlan *ifv; 32619370Spst struct ifvlanhead *hash2; 32746283Sdfr int hwidth2, i, j, n, n2; 32819370Spst 32919370Spst TRUNK_LOCK_ASSERT(trunk); 33019370Spst KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 33119370Spst 33219370Spst if (howmuch == 0) { 33319370Spst /* Harmless yet obvious coding error */ 33419370Spst printf("%s: howmuch is 0\n", __func__); 33519370Spst return; 33619370Spst } 33719370Spst 33819370Spst hwidth2 = trunk->hwidth + howmuch; 33919370Spst n = 1 << trunk->hwidth; 34019370Spst n2 = 1 << hwidth2; 34119370Spst /* Do not shrink the table below the default */ 34219370Spst if (hwidth2 < VLAN_DEF_HWIDTH) 34319370Spst return; 34419370Spst 34519370Spst /* M_NOWAIT because we're called with trunk mutex held */ 34619370Spst hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT); 34719370Spst if (hash2 == NULL) { 34819370Spst printf("%s: out of memory -- hash size not changed\n", 34946283Sdfr __func__); 35019370Spst return; /* We can live with the old hash table */ 35119370Spst } 35219370Spst for (j = 0; j < n2; j++) 35398944Sobrien LIST_INIT(&hash2[j]); 35419370Spst for (i = 0; i < n; i++) 35519370Spst while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) { 35619370Spst LIST_REMOVE(ifv, ifv_list); 35719370Spst j = HASH(ifv->ifv_tag, n2 - 1); 35819370Spst LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 35919370Spst } 36019370Spst free(trunk->hash, M_VLAN); 36119370Spst trunk->hash = hash2; 36219370Spst trunk->hwidth = hwidth2; 36319370Spst trunk->hmask = n2 - 1; 36446283Sdfr 36519370Spst if (bootverbose) 36619370Spst if_printf(trunk->parent, 36746283Sdfr "VLAN hash table resized from %d to %d buckets\n", n, n2); 36819370Spst} 36919370Spst 37019370Spststatic __inline struct ifvlan * 37119370Spstvlan_gethash(struct ifvlantrunk *trunk, uint16_t tag) 37219370Spst{ 37319370Spst struct ifvlan *ifv; 37419370Spst 37519370Spst TRUNK_LOCK_RASSERT(trunk); 37619370Spst 37719370Spst LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list) 37819370Spst if (ifv->ifv_tag == tag) 37998944Sobrien return (ifv); 38098944Sobrien return (NULL); 38198944Sobrien} 38219370Spst 38398944Sobrien#if 0 38498944Sobrien/* Debugging code to view the hashtables. */ 38519370Spststatic void 38619370Spstvlan_dumphash(struct ifvlantrunk *trunk) 38719370Spst{ 38819370Spst int i; 38919370Spst struct ifvlan *ifv; 39019370Spst 39146283Sdfr for (i = 0; i < (1 << trunk->hwidth); i++) { 39219370Spst printf("%d: ", i); 39319370Spst LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 39419370Spst printf("%s ", ifv->ifv_ifp->if_xname); 39546283Sdfr printf("\n"); 39619370Spst } 39719370Spst} 39819370Spst#endif /* 0 */ 39919370Spst#else 40019370Spst 40119370Spststatic __inline struct ifvlan * 40219370Spstvlan_gethash(struct ifvlantrunk *trunk, uint16_t tag) 40346283Sdfr{ 40419370Spst 40519370Spst return trunk->vlans[tag]; 40619370Spst} 40719370Spst 40819370Spststatic __inline int 40919370Spstvlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 41019370Spst{ 41119370Spst 41219370Spst if (trunk->vlans[ifv->ifv_tag] != NULL) 41346283Sdfr return EEXIST; 41419370Spst trunk->vlans[ifv->ifv_tag] = ifv; 41519370Spst trunk->refcnt++; 41619370Spst 41798944Sobrien return (0); 41898944Sobrien} 41998944Sobrien 42098944Sobrienstatic __inline int 42198944Sobrienvlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 42219370Spst{ 42398944Sobrien 42419370Spst trunk->vlans[ifv->ifv_tag] = NULL; 42519370Spst trunk->refcnt--; 42619370Spst 42719370Spst return (0); 42819370Spst} 42919370Spst 43046283Sdfrstatic __inline void 43119370Spstvlan_freehash(struct ifvlantrunk *trunk) 43219370Spst{ 43319370Spst} 43446283Sdfr 43519370Spststatic __inline void 43619370Spstvlan_inithash(struct ifvlantrunk *trunk) 43719370Spst{ 43846283Sdfr} 43998944Sobrien 44098944Sobrien#endif /* !VLAN_ARRAY */ 44198944Sobrien 44298944Sobrienstatic void 44398944Sobrientrunk_destroy(struct ifvlantrunk *trunk) 44498944Sobrien{ 44546283Sdfr VLAN_LOCK_ASSERT(); 44619370Spst 44719370Spst TRUNK_LOCK(trunk); 44819370Spst vlan_freehash(trunk); 44919370Spst trunk->parent->if_vlantrunk = NULL; 45019370Spst TRUNK_UNLOCK(trunk); 45119370Spst TRUNK_LOCK_DESTROY(trunk); 45219370Spst free(trunk, M_VLAN); 45319370Spst} 45419370Spst 45519370Spst/* 45698944Sobrien * Program our multicast filter. What we're actually doing is 45798944Sobrien * programming the multicast filter of the parent. This has the 45819370Spst * side effect of causing the parent interface to receive multicast 45919370Spst * traffic that it doesn't really want, which ends up being discarded 46019370Spst * later by the upper protocol layers. Unfortunately, there's no way 46198944Sobrien * to avoid this: there really is only one physical interface. 46298944Sobrien * 46398944Sobrien * XXX: There is a possible race here if more than one thread is 46498944Sobrien * modifying the multicast state of the vlan interface at the same time. 46598944Sobrien */ 46698944Sobrienstatic int 46798944Sobrienvlan_setmulti(struct ifnet *ifp) 46898944Sobrien{ 46998944Sobrien struct ifnet *ifp_p; 47098944Sobrien struct ifmultiaddr *ifma, *rifma = NULL; 47198944Sobrien struct ifvlan *sc; 47298944Sobrien struct vlan_mc_entry *mc; 47398944Sobrien int error; 47498944Sobrien 47598944Sobrien /*VLAN_LOCK_ASSERT();*/ 47698944Sobrien 47798944Sobrien /* Find the parent. */ 47898944Sobrien sc = ifp->if_softc; 47998944Sobrien ifp_p = PARENT(sc); 48098944Sobrien 48198944Sobrien CURVNET_SET_QUIET(ifp_p->if_vnet); 48298944Sobrien 48319370Spst /* First, remove any existing filter entries. */ 48419370Spst while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 48519370Spst error = if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 48619370Spst if (error) 48719370Spst return (error); 48819370Spst SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 48919370Spst free(mc, M_VLAN); 49019370Spst } 49198944Sobrien 49298944Sobrien /* Now program new ones. */ 49319370Spst TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 49419370Spst if (ifma->ifma_addr->sa_family != AF_LINK) 49598944Sobrien continue; 49646283Sdfr mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 49798944Sobrien if (mc == NULL) 49819370Spst return (ENOMEM); 49919370Spst bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 50019370Spst mc->mc_addr.sdl_index = ifp_p->if_index; 50119370Spst SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 50219370Spst error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 50319370Spst &rifma); 50419370Spst if (error) 50519370Spst return (error); 50619370Spst } 50719370Spst 50898944Sobrien CURVNET_RESTORE(); 50919370Spst return (0); 51019370Spst} 51119370Spst 512130803Smarcel/* 51319370Spst * A handler for parent interface link layer address changes. 51419370Spst * If the parent interface link layer address is changed we 51519370Spst * should also change it on all children vlans. 51646283Sdfr */ 51798944Sobrienstatic void 51898944Sobrienvlan_iflladdr(void *arg __unused, struct ifnet *ifp) 51998944Sobrien{ 52098944Sobrien struct ifvlan *ifv; 52198944Sobrien#ifndef VLAN_ARRAY 52298944Sobrien struct ifvlan *next; 52398944Sobrien#endif 52498944Sobrien int i; 52598944Sobrien 52698944Sobrien /* 52798944Sobrien * Check if it's a trunk interface first of all 52898944Sobrien * to avoid needless locking. 52998944Sobrien */ 53046283Sdfr if (ifp->if_vlantrunk == NULL) 53198944Sobrien return; 53298944Sobrien 53398944Sobrien VLAN_LOCK(); 53498944Sobrien /* 53598944Sobrien * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 53698944Sobrien */ 53798944Sobrien#ifdef VLAN_ARRAY 53898944Sobrien for (i = 0; i < VLAN_ARRAY_SIZE; i++) 53998944Sobrien if ((ifv = ifp->if_vlantrunk->vlans[i])) { 54098944Sobrien#else /* VLAN_ARRAY */ 54198944Sobrien for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 54298944Sobrien LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) { 54398944Sobrien#endif /* VLAN_ARRAY */ 54498944Sobrien VLAN_UNLOCK(); 54598944Sobrien if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp), 54698944Sobrien ifp->if_addrlen); 54798944Sobrien VLAN_LOCK(); 54898944Sobrien } 54998944Sobrien VLAN_UNLOCK(); 55046283Sdfr 55198944Sobrien} 55246283Sdfr 55398944Sobrien/* 55419370Spst * A handler for network interface departure events. 55519370Spst * Track departure of trunks here so that we don't access invalid 55698944Sobrien * pointers or whatever if a trunk is ripped from under us, e.g., 55719370Spst * by ejecting its hot-plug card. However, if an ifnet is simply 55819370Spst * being renamed, then there's no need to tear down the state. 55919370Spst */ 56019370Spststatic void 56119370Spstvlan_ifdetach(void *arg __unused, struct ifnet *ifp) 56246283Sdfr{ 56346283Sdfr struct ifvlan *ifv; 56446283Sdfr int i; 56546283Sdfr 56698944Sobrien /* 56798944Sobrien * Check if it's a trunk interface first of all 56898944Sobrien * to avoid needless locking. 56998944Sobrien */ 57098944Sobrien if (ifp->if_vlantrunk == NULL) 57198944Sobrien return; 57298944Sobrien 57398944Sobrien /* If the ifnet is just being renamed, don't do anything. */ 57498944Sobrien if (ifp->if_flags & IFF_RENAMING) 57598944Sobrien return; 57698944Sobrien 57798944Sobrien VLAN_LOCK(); 57898944Sobrien /* 57946283Sdfr * OK, it's a trunk. Loop over and detach all vlan's on it. 58098944Sobrien * Check trunk pointer after each vlan_unconfig() as it will 58198944Sobrien * free it and set to NULL after the last vlan was detached. 58298944Sobrien */ 58398944Sobrien#ifdef VLAN_ARRAY 58498944Sobrien for (i = 0; i < VLAN_ARRAY_SIZE; i++) 58598944Sobrien if ((ifv = ifp->if_vlantrunk->vlans[i])) { 58698944Sobrien vlan_unconfig_locked(ifv->ifv_ifp, 1); 58746283Sdfr if (ifp->if_vlantrunk == NULL) 58846283Sdfr break; 58998944Sobrien } 590130803Smarcel#else /* VLAN_ARRAY */ 591130803Smarcelrestart: 592130803Smarcel for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 59346283Sdfr if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) { 59419370Spst vlan_unconfig_locked(ifv->ifv_ifp, 1); 595 if (ifp->if_vlantrunk) 596 goto restart; /* trunk->hwidth can change */ 597 else 598 break; 599 } 600#endif /* VLAN_ARRAY */ 601 /* Trunk should have been destroyed in vlan_unconfig(). */ 602 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 603 VLAN_UNLOCK(); 604} 605 606/* 607 * Return the trunk device for a virtual interface. 608 */ 609static struct ifnet * 610vlan_trunkdev(struct ifnet *ifp) 611{ 612 struct ifvlan *ifv; 613 614 if (ifp->if_type != IFT_L2VLAN) 615 return (NULL); 616 ifv = ifp->if_softc; 617 ifp = NULL; 618 VLAN_LOCK(); 619 if (ifv->ifv_trunk) 620 ifp = PARENT(ifv); 621 VLAN_UNLOCK(); 622 return (ifp); 623} 624 625/* 626 * Return the 16bit vlan tag for this interface. 627 */ 628static int 629vlan_tag(struct ifnet *ifp, uint16_t *tagp) 630{ 631 struct ifvlan *ifv; 632 633 if (ifp->if_type != IFT_L2VLAN) 634 return (EINVAL); 635 ifv = ifp->if_softc; 636 *tagp = ifv->ifv_tag; 637 return (0); 638} 639 640/* 641 * Return a driver specific cookie for this interface. Synchronization 642 * with setcookie must be provided by the driver. 643 */ 644static void * 645vlan_cookie(struct ifnet *ifp) 646{ 647 struct ifvlan *ifv; 648 649 if (ifp->if_type != IFT_L2VLAN) 650 return (NULL); 651 ifv = ifp->if_softc; 652 return (ifv->ifv_cookie); 653} 654 655/* 656 * Store a cookie in our softc that drivers can use to store driver 657 * private per-instance data in. 658 */ 659static int 660vlan_setcookie(struct ifnet *ifp, void *cookie) 661{ 662 struct ifvlan *ifv; 663 664 if (ifp->if_type != IFT_L2VLAN) 665 return (EINVAL); 666 ifv = ifp->if_softc; 667 ifv->ifv_cookie = cookie; 668 return (0); 669} 670 671/* 672 * Return the vlan device present at the specific tag. 673 */ 674static struct ifnet * 675vlan_devat(struct ifnet *ifp, uint16_t tag) 676{ 677 struct ifvlantrunk *trunk; 678 struct ifvlan *ifv; 679 680 trunk = ifp->if_vlantrunk; 681 if (trunk == NULL) 682 return (NULL); 683 ifp = NULL; 684 TRUNK_RLOCK(trunk); 685 ifv = vlan_gethash(trunk, tag); 686 if (ifv) 687 ifp = ifv->ifv_ifp; 688 TRUNK_RUNLOCK(trunk); 689 return (ifp); 690} 691 692/* 693 * VLAN support can be loaded as a module. The only place in the 694 * system that's intimately aware of this is ether_input. We hook 695 * into this code through vlan_input_p which is defined there and 696 * set here. Noone else in the system should be aware of this so 697 * we use an explicit reference here. 698 */ 699extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 700 701/* For if_link_state_change() eyes only... */ 702extern void (*vlan_link_state_p)(struct ifnet *); 703 704static int 705vlan_modevent(module_t mod, int type, void *data) 706{ 707 708 switch (type) { 709 case MOD_LOAD: 710 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 711 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 712 if (ifdetach_tag == NULL) 713 return (ENOMEM); 714 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 715 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 716 if (iflladdr_tag == NULL) 717 return (ENOMEM); 718 VLAN_LOCK_INIT(); 719 vlan_input_p = vlan_input; 720 vlan_link_state_p = vlan_link_state; 721 vlan_trunk_cap_p = vlan_trunk_capabilities; 722 vlan_trunkdev_p = vlan_trunkdev; 723 vlan_cookie_p = vlan_cookie; 724 vlan_setcookie_p = vlan_setcookie; 725 vlan_tag_p = vlan_tag; 726 vlan_devat_p = vlan_devat; 727#ifndef VIMAGE 728 if_clone_attach(&vlan_cloner); 729#endif 730 if (bootverbose) 731 printf("vlan: initialized, using " 732#ifdef VLAN_ARRAY 733 "full-size arrays" 734#else 735 "hash tables with chaining" 736#endif 737 738 "\n"); 739 break; 740 case MOD_UNLOAD: 741#ifndef VIMAGE 742 if_clone_detach(&vlan_cloner); 743#endif 744 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 745 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 746 vlan_input_p = NULL; 747 vlan_link_state_p = NULL; 748 vlan_trunk_cap_p = NULL; 749 vlan_trunkdev_p = NULL; 750 vlan_tag_p = NULL; 751 vlan_cookie_p = NULL; 752 vlan_setcookie_p = NULL; 753 vlan_devat_p = NULL; 754 VLAN_LOCK_DESTROY(); 755 if (bootverbose) 756 printf("vlan: unloaded\n"); 757 break; 758 default: 759 return (EOPNOTSUPP); 760 } 761 return (0); 762} 763 764static moduledata_t vlan_mod = { 765 "if_vlan", 766 vlan_modevent, 767 0 768}; 769 770DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 771MODULE_VERSION(if_vlan, 3); 772 773#ifdef VIMAGE 774static void 775vnet_vlan_init(const void *unused __unused) 776{ 777 778 V_vlan_cloner = vlan_cloner; 779 if_clone_attach(&V_vlan_cloner); 780} 781VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 782 vnet_vlan_init, NULL); 783 784static void 785vnet_vlan_uninit(const void *unused __unused) 786{ 787 788 if_clone_detach(&V_vlan_cloner); 789} 790VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, 791 vnet_vlan_uninit, NULL); 792#endif 793 794static struct ifnet * 795vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag) 796{ 797 const char *cp; 798 struct ifnet *ifp; 799 int t; 800 801 /* Check for <etherif>.<vlan> style interface names. */ 802 IFNET_RLOCK_NOSLEEP(); 803 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 804 /* 805 * We can handle non-ethernet hardware types as long as 806 * they handle the tagging and headers themselves. 807 */ 808 if (ifp->if_type != IFT_ETHER && 809 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 810 continue; 811 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0) 812 continue; 813 cp = name + strlen(ifp->if_xname); 814 if (*cp++ != '.') 815 continue; 816 if (*cp == '\0') 817 continue; 818 t = 0; 819 for(; *cp >= '0' && *cp <= '9'; cp++) 820 t = (t * 10) + (*cp - '0'); 821 if (*cp != '\0') 822 continue; 823 if (tag != NULL) 824 *tag = t; 825 break; 826 } 827 IFNET_RUNLOCK_NOSLEEP(); 828 829 return (ifp); 830} 831 832static int 833vlan_clone_match(struct if_clone *ifc, const char *name) 834{ 835 const char *cp; 836 837 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL) 838 return (1); 839 840 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0) 841 return (0); 842 for (cp = name + 4; *cp != '\0'; cp++) { 843 if (*cp < '0' || *cp > '9') 844 return (0); 845 } 846 847 return (1); 848} 849 850static int 851vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 852{ 853 char *dp; 854 int wildcard; 855 int unit; 856 int error; 857 int tag; 858 int ethertag; 859 struct ifvlan *ifv; 860 struct ifnet *ifp; 861 struct ifnet *p; 862 struct ifaddr *ifa; 863 struct sockaddr_dl *sdl; 864 struct vlanreq vlr; 865 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 866 867 /* 868 * There are 3 (ugh) ways to specify the cloned device: 869 * o pass a parameter block with the clone request. 870 * o specify parameters in the text of the clone device name 871 * o specify no parameters and get an unattached device that 872 * must be configured separately. 873 * The first technique is preferred; the latter two are 874 * supported for backwards compatibilty. 875 */ 876 if (params) { 877 error = copyin(params, &vlr, sizeof(vlr)); 878 if (error) 879 return error; 880 p = ifunit(vlr.vlr_parent); 881 if (p == NULL) 882 return ENXIO; 883 /* 884 * Don't let the caller set up a VLAN tag with 885 * anything except VLID bits. 886 */ 887 if (vlr.vlr_tag & ~EVL_VLID_MASK) 888 return (EINVAL); 889 error = ifc_name2unit(name, &unit); 890 if (error != 0) 891 return (error); 892 893 ethertag = 1; 894 tag = vlr.vlr_tag; 895 wildcard = (unit < 0); 896 } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) { 897 ethertag = 1; 898 unit = -1; 899 wildcard = 0; 900 901 /* 902 * Don't let the caller set up a VLAN tag with 903 * anything except VLID bits. 904 */ 905 if (tag & ~EVL_VLID_MASK) 906 return (EINVAL); 907 } else { 908 ethertag = 0; 909 910 error = ifc_name2unit(name, &unit); 911 if (error != 0) 912 return (error); 913 914 wildcard = (unit < 0); 915 } 916 917 error = ifc_alloc_unit(ifc, &unit); 918 if (error != 0) 919 return (error); 920 921 /* In the wildcard case, we need to update the name. */ 922 if (wildcard) { 923 for (dp = name; *dp != '\0'; dp++); 924 if (snprintf(dp, len - (dp-name), "%d", unit) > 925 len - (dp-name) - 1) { 926 panic("%s: interface name too long", __func__); 927 } 928 } 929 930 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 931 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 932 if (ifp == NULL) { 933 ifc_free_unit(ifc, unit); 934 free(ifv, M_VLAN); 935 return (ENOSPC); 936 } 937 SLIST_INIT(&ifv->vlan_mc_listhead); 938 939 ifp->if_softc = ifv; 940 /* 941 * Set the name manually rather than using if_initname because 942 * we don't conform to the default naming convention for interfaces. 943 */ 944 strlcpy(ifp->if_xname, name, IFNAMSIZ); 945 ifp->if_dname = ifc->ifc_name; 946 ifp->if_dunit = unit; 947 /* NB: flags are not set here */ 948 ifp->if_linkmib = &ifv->ifv_mib; 949 ifp->if_linkmiblen = sizeof(ifv->ifv_mib); 950 /* NB: mtu is not set here */ 951 952 ifp->if_init = vlan_init; 953 ifp->if_transmit = vlan_transmit; 954 ifp->if_qflush = vlan_qflush; 955 ifp->if_ioctl = vlan_ioctl; 956 ifp->if_flags = VLAN_IFFLAGS; 957 ether_ifattach(ifp, eaddr); 958 /* Now undo some of the damage... */ 959 ifp->if_baudrate = 0; 960 ifp->if_type = IFT_L2VLAN; 961 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 962 ifa = ifp->if_addr; 963 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 964 sdl->sdl_type = IFT_L2VLAN; 965 966 if (ethertag) { 967 error = vlan_config(ifv, p, tag); 968 if (error != 0) { 969 /* 970 * Since we've partially failed, we need to back 971 * out all the way, otherwise userland could get 972 * confused. Thus, we destroy the interface. 973 */ 974 ether_ifdetach(ifp); 975 vlan_unconfig(ifp); 976 if_free_type(ifp, IFT_ETHER); 977 ifc_free_unit(ifc, unit); 978 free(ifv, M_VLAN); 979 980 return (error); 981 } 982 983 /* Update flags on the parent, if necessary. */ 984 vlan_setflags(ifp, 1); 985 } 986 987 return (0); 988} 989 990static int 991vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 992{ 993 struct ifvlan *ifv = ifp->if_softc; 994 int unit = ifp->if_dunit; 995 996 ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 997 vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 998 if_free_type(ifp, IFT_ETHER); 999 free(ifv, M_VLAN); 1000 ifc_free_unit(ifc, unit); 1001 1002 return (0); 1003} 1004 1005/* 1006 * The ifp->if_init entry point for vlan(4) is a no-op. 1007 */ 1008static void 1009vlan_init(void *foo __unused) 1010{ 1011} 1012 1013/* 1014 * The if_transmit method for vlan(4) interface. 1015 */ 1016static int 1017vlan_transmit(struct ifnet *ifp, struct mbuf *m) 1018{ 1019 struct ifvlan *ifv; 1020 struct ifnet *p; 1021 int error, len, mcast; 1022 1023 ifv = ifp->if_softc; 1024 p = PARENT(ifv); 1025 len = m->m_pkthdr.len; 1026 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 1027 1028 BPF_MTAP(ifp, m); 1029 1030 /* 1031 * Do not run parent's if_transmit() if the parent is not up, 1032 * or parent's driver will cause a system crash. 1033 */ 1034 if (!UP_AND_RUNNING(p)) { 1035 m_freem(m); 1036 ifp->if_oerrors++; 1037 return (ENETDOWN); 1038 } 1039 1040 /* 1041 * Pad the frame to the minimum size allowed if told to. 1042 * This option is in accord with IEEE Std 802.1Q, 2003 Ed., 1043 * paragraph C.4.4.3.b. It can help to work around buggy 1044 * bridges that violate paragraph C.4.4.3.a from the same 1045 * document, i.e., fail to pad short frames after untagging. 1046 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but 1047 * untagging it will produce a 62-byte frame, which is a runt 1048 * and requires padding. There are VLAN-enabled network 1049 * devices that just discard such runts instead or mishandle 1050 * them somehow. 1051 */ 1052 if (soft_pad && p->if_type == IFT_ETHER) { 1053 static char pad[8]; /* just zeros */ 1054 int n; 1055 1056 for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len; 1057 n > 0; n -= sizeof(pad)) 1058 if (!m_append(m, min(n, sizeof(pad)), pad)) 1059 break; 1060 1061 if (n > 0) { 1062 if_printf(ifp, "cannot pad short frame\n"); 1063 ifp->if_oerrors++; 1064 m_freem(m); 1065 return (0); 1066 } 1067 } 1068 1069 /* 1070 * If underlying interface can do VLAN tag insertion itself, 1071 * just pass the packet along. However, we need some way to 1072 * tell the interface where the packet came from so that it 1073 * knows how to find the VLAN tag to use, so we attach a 1074 * packet tag that holds it. 1075 */ 1076 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1077 m->m_pkthdr.ether_vtag = ifv->ifv_tag; 1078 m->m_flags |= M_VLANTAG; 1079 } else { 1080 m = ether_vlanencap(m, ifv->ifv_tag); 1081 if (m == NULL) { 1082 if_printf(ifp, "unable to prepend VLAN header\n"); 1083 ifp->if_oerrors++; 1084 return (0); 1085 } 1086 } 1087 1088 /* 1089 * Send it, precisely as ether_output() would have. 1090 */ 1091 error = (p->if_transmit)(p, m); 1092 if (!error) { 1093 ifp->if_opackets++; 1094 ifp->if_omcasts += mcast; 1095 ifp->if_obytes += len; 1096 } else 1097 ifp->if_oerrors++; 1098 return (error); 1099} 1100 1101/* 1102 * The ifp->if_qflush entry point for vlan(4) is a no-op. 1103 */ 1104static void 1105vlan_qflush(struct ifnet *ifp __unused) 1106{ 1107} 1108 1109static void 1110vlan_input(struct ifnet *ifp, struct mbuf *m) 1111{ 1112 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1113 struct ifvlan *ifv; 1114 uint16_t tag; 1115 1116 KASSERT(trunk != NULL, ("%s: no trunk", __func__)); 1117 1118 if (m->m_flags & M_VLANTAG) { 1119 /* 1120 * Packet is tagged, but m contains a normal 1121 * Ethernet frame; the tag is stored out-of-band. 1122 */ 1123 tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag); 1124 m->m_flags &= ~M_VLANTAG; 1125 } else { 1126 struct ether_vlan_header *evl; 1127 1128 /* 1129 * Packet is tagged in-band as specified by 802.1q. 1130 */ 1131 switch (ifp->if_type) { 1132 case IFT_ETHER: 1133 if (m->m_len < sizeof(*evl) && 1134 (m = m_pullup(m, sizeof(*evl))) == NULL) { 1135 if_printf(ifp, "cannot pullup VLAN header\n"); 1136 return; 1137 } 1138 evl = mtod(m, struct ether_vlan_header *); 1139 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 1140 1141 /* 1142 * Remove the 802.1q header by copying the Ethernet 1143 * addresses over it and adjusting the beginning of 1144 * the data in the mbuf. The encapsulated Ethernet 1145 * type field is already in place. 1146 */ 1147 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 1148 ETHER_HDR_LEN - ETHER_TYPE_LEN); 1149 m_adj(m, ETHER_VLAN_ENCAP_LEN); 1150 break; 1151 1152 default: 1153#ifdef INVARIANTS 1154 panic("%s: %s has unsupported if_type %u", 1155 __func__, ifp->if_xname, ifp->if_type); 1156#endif 1157 m_freem(m); 1158 ifp->if_noproto++; 1159 return; 1160 } 1161 } 1162 1163 TRUNK_RLOCK(trunk); 1164 ifv = vlan_gethash(trunk, tag); 1165 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 1166 TRUNK_RUNLOCK(trunk); 1167 m_freem(m); 1168 ifp->if_noproto++; 1169 return; 1170 } 1171 TRUNK_RUNLOCK(trunk); 1172 1173 m->m_pkthdr.rcvif = ifv->ifv_ifp; 1174 ifv->ifv_ifp->if_ipackets++; 1175 1176 /* Pass it back through the parent's input routine. */ 1177 (*ifp->if_input)(ifv->ifv_ifp, m); 1178} 1179 1180static int 1181vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag) 1182{ 1183 struct ifvlantrunk *trunk; 1184 struct ifnet *ifp; 1185 int error = 0; 1186 1187 /* VID numbers 0x0 and 0xFFF are reserved */ 1188 if (tag == 0 || tag == 0xFFF) 1189 return (EINVAL); 1190 if (p->if_type != IFT_ETHER && 1191 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 1192 return (EPROTONOSUPPORT); 1193 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 1194 return (EPROTONOSUPPORT); 1195 if (ifv->ifv_trunk) 1196 return (EBUSY); 1197 1198 if (p->if_vlantrunk == NULL) { 1199 trunk = malloc(sizeof(struct ifvlantrunk), 1200 M_VLAN, M_WAITOK | M_ZERO); 1201 vlan_inithash(trunk); 1202 VLAN_LOCK(); 1203 if (p->if_vlantrunk != NULL) { 1204 /* A race that that is very unlikely to be hit. */ 1205 vlan_freehash(trunk); 1206 free(trunk, M_VLAN); 1207 goto exists; 1208 } 1209 TRUNK_LOCK_INIT(trunk); 1210 TRUNK_LOCK(trunk); 1211 p->if_vlantrunk = trunk; 1212 trunk->parent = p; 1213 } else { 1214 VLAN_LOCK(); 1215exists: 1216 trunk = p->if_vlantrunk; 1217 TRUNK_LOCK(trunk); 1218 } 1219 1220 ifv->ifv_tag = tag; /* must set this before vlan_inshash() */ 1221 error = vlan_inshash(trunk, ifv); 1222 if (error) 1223 goto done; 1224 ifv->ifv_proto = ETHERTYPE_VLAN; 1225 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1226 ifv->ifv_mintu = ETHERMIN; 1227 ifv->ifv_pflags = 0; 1228 1229 /* 1230 * If the parent supports the VLAN_MTU capability, 1231 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1232 * use it. 1233 */ 1234 if (p->if_capenable & IFCAP_VLAN_MTU) { 1235 /* 1236 * No need to fudge the MTU since the parent can 1237 * handle extended frames. 1238 */ 1239 ifv->ifv_mtufudge = 0; 1240 } else { 1241 /* 1242 * Fudge the MTU by the encapsulation size. This 1243 * makes us incompatible with strictly compliant 1244 * 802.1Q implementations, but allows us to use 1245 * the feature with other NetBSD implementations, 1246 * which might still be useful. 1247 */ 1248 ifv->ifv_mtufudge = ifv->ifv_encaplen; 1249 } 1250 1251 ifv->ifv_trunk = trunk; 1252 ifp = ifv->ifv_ifp; 1253 /* 1254 * Initialize fields from our parent. This duplicates some 1255 * work with ether_ifattach() but allows for non-ethernet 1256 * interfaces to also work. 1257 */ 1258 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 1259 ifp->if_baudrate = p->if_baudrate; 1260 ifp->if_output = p->if_output; 1261 ifp->if_input = p->if_input; 1262 ifp->if_resolvemulti = p->if_resolvemulti; 1263 ifp->if_addrlen = p->if_addrlen; 1264 ifp->if_broadcastaddr = p->if_broadcastaddr; 1265 1266 /* 1267 * Copy only a selected subset of flags from the parent. 1268 * Other flags are none of our business. 1269 */ 1270#define VLAN_COPY_FLAGS (IFF_SIMPLEX) 1271 ifp->if_flags &= ~VLAN_COPY_FLAGS; 1272 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 1273#undef VLAN_COPY_FLAGS 1274 1275 ifp->if_link_state = p->if_link_state; 1276 1277 vlan_capabilities(ifv); 1278 1279 /* 1280 * Set up our interface address to reflect the underlying 1281 * physical interface's. 1282 */ 1283 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1284 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1285 p->if_addrlen; 1286 1287 /* 1288 * Configure multicast addresses that may already be 1289 * joined on the vlan device. 1290 */ 1291 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */ 1292 1293 /* We are ready for operation now. */ 1294 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1295done: 1296 TRUNK_UNLOCK(trunk); 1297 if (error == 0) 1298 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag); 1299 VLAN_UNLOCK(); 1300 1301 return (error); 1302} 1303 1304static void 1305vlan_unconfig(struct ifnet *ifp) 1306{ 1307 1308 VLAN_LOCK(); 1309 vlan_unconfig_locked(ifp, 0); 1310 VLAN_UNLOCK(); 1311} 1312 1313static void 1314vlan_unconfig_locked(struct ifnet *ifp, int departing) 1315{ 1316 struct ifvlantrunk *trunk; 1317 struct vlan_mc_entry *mc; 1318 struct ifvlan *ifv; 1319 struct ifnet *parent; 1320 int error; 1321 1322 VLAN_LOCK_ASSERT(); 1323 1324 ifv = ifp->if_softc; 1325 trunk = ifv->ifv_trunk; 1326 parent = NULL; 1327 1328 if (trunk != NULL) { 1329 1330 TRUNK_LOCK(trunk); 1331 parent = trunk->parent; 1332 1333 /* 1334 * Since the interface is being unconfigured, we need to 1335 * empty the list of multicast groups that we may have joined 1336 * while we were alive from the parent's list. 1337 */ 1338 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 1339 /* 1340 * If the parent interface is being detached, 1341 * all its multicast addresses have already 1342 * been removed. Warn about errors if 1343 * if_delmulti() does fail, but don't abort as 1344 * all callers expect vlan destruction to 1345 * succeed. 1346 */ 1347 if (!departing) { 1348 error = if_delmulti(parent, 1349 (struct sockaddr *)&mc->mc_addr); 1350 if (error) 1351 if_printf(ifp, 1352 "Failed to delete multicast address from parent: %d\n", 1353 error); 1354 } 1355 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 1356 free(mc, M_VLAN); 1357 } 1358 1359 vlan_setflags(ifp, 0); /* clear special flags on parent */ 1360 vlan_remhash(trunk, ifv); 1361 ifv->ifv_trunk = NULL; 1362 1363 /* 1364 * Check if we were the last. 1365 */ 1366 if (trunk->refcnt == 0) { 1367 trunk->parent->if_vlantrunk = NULL; 1368 /* 1369 * XXXGL: If some ithread has already entered 1370 * vlan_input() and is now blocked on the trunk 1371 * lock, then it should preempt us right after 1372 * unlock and finish its work. Then we will acquire 1373 * lock again in trunk_destroy(). 1374 */ 1375 TRUNK_UNLOCK(trunk); 1376 trunk_destroy(trunk); 1377 } else 1378 TRUNK_UNLOCK(trunk); 1379 } 1380 1381 /* Disconnect from parent. */ 1382 if (ifv->ifv_pflags) 1383 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 1384 ifp->if_mtu = ETHERMTU; 1385 ifp->if_link_state = LINK_STATE_UNKNOWN; 1386 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1387 1388 /* 1389 * Only dispatch an event if vlan was 1390 * attached, otherwise there is nothing 1391 * to cleanup anyway. 1392 */ 1393 if (parent != NULL) 1394 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag); 1395} 1396 1397/* Handle a reference counted flag that should be set on the parent as well */ 1398static int 1399vlan_setflag(struct ifnet *ifp, int flag, int status, 1400 int (*func)(struct ifnet *, int)) 1401{ 1402 struct ifvlan *ifv; 1403 int error; 1404 1405 /* XXX VLAN_LOCK_ASSERT(); */ 1406 1407 ifv = ifp->if_softc; 1408 status = status ? (ifp->if_flags & flag) : 0; 1409 /* Now "status" contains the flag value or 0 */ 1410 1411 /* 1412 * See if recorded parent's status is different from what 1413 * we want it to be. If it is, flip it. We record parent's 1414 * status in ifv_pflags so that we won't clear parent's flag 1415 * we haven't set. In fact, we don't clear or set parent's 1416 * flags directly, but get or release references to them. 1417 * That's why we can be sure that recorded flags still are 1418 * in accord with actual parent's flags. 1419 */ 1420 if (status != (ifv->ifv_pflags & flag)) { 1421 error = (*func)(PARENT(ifv), status); 1422 if (error) 1423 return (error); 1424 ifv->ifv_pflags &= ~flag; 1425 ifv->ifv_pflags |= status; 1426 } 1427 return (0); 1428} 1429 1430/* 1431 * Handle IFF_* flags that require certain changes on the parent: 1432 * if "status" is true, update parent's flags respective to our if_flags; 1433 * if "status" is false, forcedly clear the flags set on parent. 1434 */ 1435static int 1436vlan_setflags(struct ifnet *ifp, int status) 1437{ 1438 int error, i; 1439 1440 for (i = 0; vlan_pflags[i].flag; i++) { 1441 error = vlan_setflag(ifp, vlan_pflags[i].flag, 1442 status, vlan_pflags[i].func); 1443 if (error) 1444 return (error); 1445 } 1446 return (0); 1447} 1448 1449/* Inform all vlans that their parent has changed link state */ 1450static void 1451vlan_link_state(struct ifnet *ifp) 1452{ 1453 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1454 struct ifvlan *ifv; 1455 int i; 1456 1457 TRUNK_LOCK(trunk); 1458#ifdef VLAN_ARRAY 1459 for (i = 0; i < VLAN_ARRAY_SIZE; i++) 1460 if (trunk->vlans[i] != NULL) { 1461 ifv = trunk->vlans[i]; 1462#else 1463 for (i = 0; i < (1 << trunk->hwidth); i++) 1464 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) { 1465#endif 1466 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1467 if_link_state_change(ifv->ifv_ifp, 1468 trunk->parent->if_link_state); 1469 } 1470 TRUNK_UNLOCK(trunk); 1471} 1472 1473static void 1474vlan_capabilities(struct ifvlan *ifv) 1475{ 1476 struct ifnet *p = PARENT(ifv); 1477 struct ifnet *ifp = ifv->ifv_ifp; 1478 1479 TRUNK_LOCK_ASSERT(TRUNK(ifv)); 1480 1481 /* 1482 * If the parent interface can do checksum offloading 1483 * on VLANs, then propagate its hardware-assisted 1484 * checksumming flags. Also assert that checksum 1485 * offloading requires hardware VLAN tagging. 1486 */ 1487 if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1488 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM; 1489 1490 if (p->if_capenable & IFCAP_VLAN_HWCSUM && 1491 p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1492 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM; 1493 ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP | 1494 CSUM_UDP | CSUM_SCTP | CSUM_FRAGMENT); 1495 } else { 1496 ifp->if_capenable = 0; 1497 ifp->if_hwassist = 0; 1498 } 1499 /* 1500 * If the parent interface can do TSO on VLANs then 1501 * propagate the hardware-assisted flag. TSO on VLANs 1502 * does not necessarily require hardware VLAN tagging. 1503 */ 1504 if (p->if_capabilities & IFCAP_VLAN_HWTSO) 1505 ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO; 1506 if (p->if_capenable & IFCAP_VLAN_HWTSO) { 1507 ifp->if_capenable |= p->if_capenable & IFCAP_TSO; 1508 ifp->if_hwassist |= p->if_hwassist & CSUM_TSO; 1509 } else { 1510 ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO); 1511 ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO); 1512 } 1513 1514 /* 1515 * If the parent interface can offload TCP connections over VLANs then 1516 * propagate its TOE capability to the VLAN interface. 1517 * 1518 * All TOE drivers in the tree today can deal with VLANs. If this 1519 * changes then IFCAP_VLAN_TOE should be promoted to a full capability 1520 * with its own bit. 1521 */ 1522#define IFCAP_VLAN_TOE IFCAP_TOE 1523 if (p->if_capabilities & IFCAP_VLAN_TOE) 1524 ifp->if_capabilities |= p->if_capabilities & IFCAP_TOE; 1525 if (p->if_capenable & IFCAP_VLAN_TOE) { 1526 TOEDEV(ifp) = TOEDEV(p); 1527 ifp->if_capenable |= p->if_capenable & IFCAP_TOE; 1528 } 1529} 1530 1531static void 1532vlan_trunk_capabilities(struct ifnet *ifp) 1533{ 1534 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1535 struct ifvlan *ifv; 1536 int i; 1537 1538 TRUNK_LOCK(trunk); 1539#ifdef VLAN_ARRAY 1540 for (i = 0; i < VLAN_ARRAY_SIZE; i++) 1541 if (trunk->vlans[i] != NULL) { 1542 ifv = trunk->vlans[i]; 1543#else 1544 for (i = 0; i < (1 << trunk->hwidth); i++) { 1545 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 1546#endif 1547 vlan_capabilities(ifv); 1548 } 1549 TRUNK_UNLOCK(trunk); 1550} 1551 1552static int 1553vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1554{ 1555 struct ifnet *p; 1556 struct ifreq *ifr; 1557 struct ifaddr *ifa; 1558 struct ifvlan *ifv; 1559 struct vlanreq vlr; 1560 int error = 0; 1561 1562 ifr = (struct ifreq *)data; 1563 ifa = (struct ifaddr *) data; 1564 ifv = ifp->if_softc; 1565 1566 switch (cmd) { 1567 case SIOCSIFADDR: 1568 ifp->if_flags |= IFF_UP; 1569#ifdef INET 1570 if (ifa->ifa_addr->sa_family == AF_INET) 1571 arp_ifinit(ifp, ifa); 1572#endif 1573 break; 1574 case SIOCGIFADDR: 1575 { 1576 struct sockaddr *sa; 1577 1578 sa = (struct sockaddr *)&ifr->ifr_data; 1579 bcopy(IF_LLADDR(ifp), sa->sa_data, ifp->if_addrlen); 1580 } 1581 break; 1582 case SIOCGIFMEDIA: 1583 VLAN_LOCK(); 1584 if (TRUNK(ifv) != NULL) { 1585 p = PARENT(ifv); 1586 VLAN_UNLOCK(); 1587 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 1588 /* Limit the result to the parent's current config. */ 1589 if (error == 0) { 1590 struct ifmediareq *ifmr; 1591 1592 ifmr = (struct ifmediareq *)data; 1593 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1594 ifmr->ifm_count = 1; 1595 error = copyout(&ifmr->ifm_current, 1596 ifmr->ifm_ulist, 1597 sizeof(int)); 1598 } 1599 } 1600 } else { 1601 VLAN_UNLOCK(); 1602 error = EINVAL; 1603 } 1604 break; 1605 1606 case SIOCSIFMEDIA: 1607 error = EINVAL; 1608 break; 1609 1610 case SIOCSIFMTU: 1611 /* 1612 * Set the interface MTU. 1613 */ 1614 VLAN_LOCK(); 1615 if (TRUNK(ifv) != NULL) { 1616 if (ifr->ifr_mtu > 1617 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1618 ifr->ifr_mtu < 1619 (ifv->ifv_mintu - ifv->ifv_mtufudge)) 1620 error = EINVAL; 1621 else 1622 ifp->if_mtu = ifr->ifr_mtu; 1623 } else 1624 error = EINVAL; 1625 VLAN_UNLOCK(); 1626 break; 1627 1628 case SIOCSETVLAN: 1629#ifdef VIMAGE 1630 if (ifp->if_vnet != ifp->if_home_vnet) { 1631 error = EPERM; 1632 break; 1633 } 1634#endif 1635 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr)); 1636 if (error) 1637 break; 1638 if (vlr.vlr_parent[0] == '\0') { 1639 vlan_unconfig(ifp); 1640 break; 1641 } 1642 p = ifunit(vlr.vlr_parent); 1643 if (p == NULL) { 1644 error = ENOENT; 1645 break; 1646 } 1647 /* 1648 * Don't let the caller set up a VLAN tag with 1649 * anything except VLID bits. 1650 */ 1651 if (vlr.vlr_tag & ~EVL_VLID_MASK) { 1652 error = EINVAL; 1653 break; 1654 } 1655 error = vlan_config(ifv, p, vlr.vlr_tag); 1656 if (error) 1657 break; 1658 1659 /* Update flags on the parent, if necessary. */ 1660 vlan_setflags(ifp, 1); 1661 break; 1662 1663 case SIOCGETVLAN: 1664#ifdef VIMAGE 1665 if (ifp->if_vnet != ifp->if_home_vnet) { 1666 error = EPERM; 1667 break; 1668 } 1669#endif 1670 bzero(&vlr, sizeof(vlr)); 1671 VLAN_LOCK(); 1672 if (TRUNK(ifv) != NULL) { 1673 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 1674 sizeof(vlr.vlr_parent)); 1675 vlr.vlr_tag = ifv->ifv_tag; 1676 } 1677 VLAN_UNLOCK(); 1678 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 1679 break; 1680 1681 case SIOCSIFFLAGS: 1682 /* 1683 * We should propagate selected flags to the parent, 1684 * e.g., promiscuous mode. 1685 */ 1686 if (TRUNK(ifv) != NULL) 1687 error = vlan_setflags(ifp, 1); 1688 break; 1689 1690 case SIOCADDMULTI: 1691 case SIOCDELMULTI: 1692 /* 1693 * If we don't have a parent, just remember the membership for 1694 * when we do. 1695 */ 1696 if (TRUNK(ifv) != NULL) 1697 error = vlan_setmulti(ifp); 1698 break; 1699 1700 default: 1701 error = EINVAL; 1702 break; 1703 } 1704 1705 return (error); 1706} 1707