if_vlan.c revision 252555
14Srgrimes/*- 24Srgrimes * Copyright 1998 Massachusetts Institute of Technology 34Srgrimes * 44Srgrimes * Permission to use, copy, modify, and distribute this software and 54Srgrimes * its documentation for any purpose and without fee is hereby 64Srgrimes * granted, provided that both the above copyright notice and this 74Srgrimes * permission notice appear in all copies, that both the above 84Srgrimes * copyright notice and this permission notice appear in all 94Srgrimes * supporting documentation, and that the name of M.I.T. not be used 104Srgrimes * in advertising or publicity pertaining to distribution of the 114Srgrimes * software without specific, written prior permission. M.I.T. makes 124Srgrimes * no representations about the suitability of this software for any 134Srgrimes * purpose. It is provided "as is" without express or implied 144Srgrimes * warranty. 154Srgrimes * 164Srgrimes * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 174Srgrimes * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 184Srgrimes * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 194Srgrimes * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 204Srgrimes * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 214Srgrimes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 224Srgrimes * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 234Srgrimes * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 244Srgrimes * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 254Srgrimes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 264Srgrimes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 274Srgrimes * SUCH DAMAGE. 284Srgrimes */ 294Srgrimes 304Srgrimes/* 314Srgrimes * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 324Srgrimes * Might be extended some day to also handle IEEE 802.1p priority 334Srgrimes * tagging. This is sort of sneaky in the implementation, since 344Srgrimes * we need to pretend to be enough of an Ethernet implementation 354Srgrimes * to make arp work. The way we do this is by telling everyone 364Srgrimes * that we are an Ethernet, and then catch the packets that 374Srgrimes * ether_output() sends to us via if_transmit(), rewrite them for 38620Srgrimes * use by the real outgoing interface, and ask it to send them. 394Srgrimes */ 404Srgrimes 41116182Sobrien#include <sys/cdefs.h> 42116182Sobrien__FBSDID("$FreeBSD: stable/9/sys/net/if_vlan.c 252555 2013-07-03 09:25:29Z np $"); 43116182Sobrien 4487649Sguido#include "opt_inet.h" 4587649Sguido#include "opt_vlan.h" 462056Swollman 471549Srgrimes#include <sys/param.h> 485764Sbde#include <sys/kernel.h> 4956525Sbde#include <sys/lock.h> 5085448Sjlemon#include <sys/malloc.h> 5112675Sjulian#include <sys/mbuf.h> 5285373Sjlemon#include <sys/module.h> 53116663Siedowse#include <sys/rwlock.h> 5485373Sjlemon#include <sys/queue.h> 5569929Sobrien#include <sys/socket.h> 5685373Sjlemon#include <sys/sockio.h> 5718951Sjulian#include <sys/sysctl.h> 5812701Sphk#include <sys/systm.h> 592056Swollman#include <sys/sx.h> 6034924Sbde 6185373Sjlemon#include <net/bpf.h> 624Srgrimes#include <net/ethernet.h> 6387620Sguido#include <net/if.h> 6487620Sguido#include <net/if_clone.h> 6512701Sphk#include <net/if_dl.h> 664Srgrimes#include <net/if_types.h> 6712675Sjulian#include <net/if_vlan_var.h> 6812675Sjulian#include <net/vnet.h> 6912675Sjulian 7012675Sjulian#ifdef INET 7112675Sjulian#include <netinet/in.h> 7229368Speter#include <netinet/if_ether.h> 7372521Sjlemon#endif 7412675Sjulian 7547625Sphk#define VLANNAME "vlan" 76111815Sphk#define VLAN_DEF_HWIDTH 4 77111815Sphk#define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 78111815Sphk 79111815Sphk#define UP_AND_RUNNING(ifp) \ 80111815Sphk ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 81111815Sphk 82111815SphkLIST_HEAD(ifvlanhead, ifvlan); 83112035Sphk 84112035Sphkstruct ifvlantrunk { 85112035Sphk struct ifnet *parent; /* parent interface of this trunk */ 86112035Sphk struct rwlock rw; 87112035Sphk#ifdef VLAN_ARRAY 88112035Sphk#define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 89111821Sphk struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 90111815Sphk#else 9138485Sbde struct ifvlanhead *hash; /* dynamic hash-list table */ 9212675Sjulian uint16_t hmask; 9385373Sjlemon uint16_t hwidth; 9485373Sjlemon#endif 9585373Sjlemon int refcnt; 9685373Sjlemon}; 9785373Sjlemon 9885373Sjlemonstruct vlan_mc_entry { 9985373Sjlemon struct sockaddr_dl mc_addr; 10085373Sjlemon SLIST_ENTRY(vlan_mc_entry) mc_entries; 10185373Sjlemon}; 10285373Sjlemon 10385373Sjlemonstruct ifvlan { 10485373Sjlemon struct ifvlantrunk *ifv_trunk; 10585373Sjlemon struct ifnet *ifv_ifp; 10685373Sjlemon void *ifv_cookie; 10785373Sjlemon#define TRUNK(ifv) ((ifv)->ifv_trunk) 10885373Sjlemon#define PARENT(ifv) ((ifv)->ifv_trunk->parent) 10949049Syokota int ifv_pflags; /* special flags we have set on parent */ 11041612Seivind struct ifv_linkmib { 11149049Syokota int ifvm_encaplen; /* encapsulation length */ 11227982Sjulian int ifvm_mtufudge; /* MTU fudged by this much */ 1137680Sjoerg int ifvm_mintu; /* min transmission unit */ 1147680Sjoerg uint16_t ifvm_proto; /* encapsulation ethertype */ 1157680Sjoerg uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 1167680Sjoerg } ifv_mib; 11785373Sjlemon SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 11885373Sjlemon#ifndef VLAN_ARRAY 11985373Sjlemon LIST_ENTRY(ifvlan) ifv_list; 120116663Siedowse#endif 121116663Siedowse}; 122116663Siedowse#define ifv_proto ifv_mib.ifvm_proto 12387620Sguido#define ifv_tag ifv_mib.ifvm_tag 12487620Sguido#define ifv_encaplen ifv_mib.ifvm_encaplen 12587620Sguido#define ifv_mtufudge ifv_mib.ifvm_mtufudge 126116663Siedowse#define ifv_mintu ifv_mib.ifvm_mintu 1275764Sbde 12885448Sjlemon/* Special flags we should propagate to parent. */ 129116663Siedowsestatic struct { 13085448Sjlemon int flag; 13155823Syokota int (*func)(struct ifnet *, int); 13278161Speter} vlan_pflags[] = { 13342373Syokota {IFF_PROMISC, ifpromisc}, 134798Swollman {IFF_ALLMULTI, if_allmulti}, 13585373Sjlemon {0, NULL} 1364Srgrimes}; 13785373Sjlemon 1384SrgrimesSYSCTL_DECL(_net_link); 1394Srgrimesstatic SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, 14018951Sjulian "IEEE 802.1Q VLAN"); 14118951Sjulianstatic SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, 14218951Sjulian "for consistency"); 14318951Sjulian 14418951Sjulianstatic int soft_pad = 0; 14518951SjulianSYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0, 14618951Sjulian "pad short frames before tagging"); 14718951Sjulian 14818951Sjulianstatic MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface"); 14918951Sjulian 15085373Sjlemonstatic eventhandler_tag ifdetach_tag; 15118951Sjulianstatic eventhandler_tag iflladdr_tag; 15285373Sjlemon 1534Srgrimes/* 15485373Sjlemon * We have a global mutex, that is used to serialize configuration 15585373Sjlemon * changes and isn't used in normal packet delivery. 15685373Sjlemon * 157101436Sjake * We also have a per-trunk rwlock, that is locked shared on packet 15885373Sjlemon * processing and exclusive when configuration is changed. 15985373Sjlemon * 16085373Sjlemon * The VLAN_ARRAY substitutes the dynamic hash with a static array 16185373Sjlemon * with 4096 entries. In theory this can give a boost in processing, 16285373Sjlemon * however on practice it does not. Probably this is because array 16385373Sjlemon * is too big to fit into CPU cache. 16485373Sjlemon */ 16585373Sjlemonstatic struct sx ifv_lock; 16685373Sjlemon#define VLAN_LOCK_INIT() sx_init(&ifv_lock, "vlan_global") 16785373Sjlemon#define VLAN_LOCK_DESTROY() sx_destroy(&ifv_lock) 16885373Sjlemon#define VLAN_LOCK_ASSERT() sx_assert(&ifv_lock, SA_LOCKED) 16985373Sjlemon#define VLAN_LOCK() sx_xlock(&ifv_lock) 17085373Sjlemon#define VLAN_UNLOCK() sx_xunlock(&ifv_lock) 17185373Sjlemon#define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME) 17285373Sjlemon#define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw) 17385373Sjlemon#define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw) 1744Srgrimes#define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw) 17585373Sjlemon#define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED) 17685373Sjlemon#define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw) 17785373Sjlemon#define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw) 17810665Sbde#define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED) 17987620Sguido 18087620Sguido#ifndef VLAN_ARRAY 1814Srgrimesstatic void vlan_inithash(struct ifvlantrunk *trunk); 18285373Sjlemonstatic void vlan_freehash(struct ifvlantrunk *trunk); 18310665Sbdestatic int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 18485373Sjlemonstatic int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 18585373Sjlemonstatic void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 18685373Sjlemonstatic __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 18787620Sguido uint16_t tag); 18887620Sguido#endif 18987620Sguidostatic void trunk_destroy(struct ifvlantrunk *trunk); 19087620Sguido 19187620Sguidostatic void vlan_init(void *foo); 19287620Sguidostatic void vlan_input(struct ifnet *ifp, struct mbuf *m); 19385373Sjlemonstatic int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 19485373Sjlemonstatic void vlan_qflush(struct ifnet *ifp); 19585373Sjlemonstatic int vlan_setflag(struct ifnet *ifp, int flag, int status, 19685373Sjlemon int (*func)(struct ifnet *, int)); 19785373Sjlemonstatic int vlan_setflags(struct ifnet *ifp, int status); 19885373Sjlemonstatic int vlan_setmulti(struct ifnet *ifp); 19985373Sjlemonstatic int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 20085373Sjlemonstatic void vlan_unconfig(struct ifnet *ifp); 20185373Sjlemonstatic void vlan_unconfig_locked(struct ifnet *ifp, int departing); 20285373Sjlemonstatic int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag); 20385373Sjlemonstatic void vlan_link_state(struct ifnet *ifp); 20485373Sjlemonstatic void vlan_capabilities(struct ifvlan *ifv); 20585373Sjlemonstatic void vlan_trunk_capabilities(struct ifnet *ifp); 20685373Sjlemon 20748104Syokotastatic struct ifnet *vlan_clone_match_ethertag(struct if_clone *, 20885373Sjlemon const char *, int *); 20985373Sjlemonstatic int vlan_clone_match(struct if_clone *, const char *); 21085373Sjlemonstatic int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 211120456Sphkstatic int vlan_clone_destroy(struct if_clone *, struct ifnet *); 212120456Sphk 213120456Sphkstatic void vlan_ifdetach(void *arg, struct ifnet *ifp); 214120456Sphkstatic void vlan_iflladdr(void *arg, struct ifnet *ifp); 21585373Sjlemon 21685373Sjlemonstatic struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL, 21710665Sbde IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy); 21810665Sbde 21910665Sbde#ifdef VIMAGE 22085373Sjlemonstatic VNET_DEFINE(struct if_clone, vlan_cloner); 22110665Sbde#define V_vlan_cloner VNET(vlan_cloner) 22285373Sjlemon#endif 22310665Sbde 22485373Sjlemon#ifndef VLAN_ARRAY 22585373Sjlemon#define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 22685373Sjlemon 22785373Sjlemonstatic void 22885373Sjlemonvlan_inithash(struct ifvlantrunk *trunk) 22985373Sjlemon{ 23085373Sjlemon int i, n; 23185373Sjlemon 23285373Sjlemon /* 23385373Sjlemon * The trunk must not be locked here since we call malloc(M_WAITOK). 23485373Sjlemon * It is OK in case this function is called before the trunk struct 23585373Sjlemon * gets hooked up and becomes visible from other threads. 23685373Sjlemon */ 23785373Sjlemon 23885373Sjlemon KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 23985373Sjlemon ("%s: hash already initialized", __func__)); 24085373Sjlemon 24110665Sbde trunk->hwidth = VLAN_DEF_HWIDTH; 24256582Sbde n = 1 << trunk->hwidth; 2434Srgrimes trunk->hmask = n - 1; 2444Srgrimes trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 24585373Sjlemon for (i = 0; i < n; i++) 24685373Sjlemon LIST_INIT(&trunk->hash[i]); 24727982Sjulian} 24885373Sjlemon 24927982Sjulianstatic void 25085373Sjlemonvlan_freehash(struct ifvlantrunk *trunk) 25185373Sjlemon{ 25285373Sjlemon#ifdef INVARIANTS 25385373Sjlemon int i; 25485373Sjlemon 25585373Sjlemon KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 25685373Sjlemon for (i = 0; i < (1 << trunk->hwidth); i++) 25727982Sjulian KASSERT(LIST_EMPTY(&trunk->hash[i]), 25885373Sjlemon ("%s: hash table not empty", __func__)); 25985373Sjlemon#endif 26027982Sjulian free(trunk->hash, M_VLAN); 26185373Sjlemon trunk->hash = NULL; 26285373Sjlemon trunk->hwidth = trunk->hmask = 0; 26385373Sjlemon} 26485373Sjlemon 26585373Sjlemonstatic int 26685373Sjlemonvlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 26785373Sjlemon{ 26885373Sjlemon int i, b; 26985373Sjlemon struct ifvlan *ifv2; 27085373Sjlemon 27185373Sjlemon TRUNK_LOCK_ASSERT(trunk); 27285373Sjlemon KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 273120456Sphk 274120456Sphk b = 1 << trunk->hwidth; 275120456Sphk i = HASH(ifv->ifv_tag, trunk->hmask); 276120456Sphk LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 27785373Sjlemon if (ifv->ifv_tag == ifv2->ifv_tag) 27885373Sjlemon return (EEXIST); 27985373Sjlemon 28085373Sjlemon /* 28185373Sjlemon * Grow the hash when the number of vlans exceeds half of the number of 28285373Sjlemon * hash buckets squared. This will make the average linked-list length 28385373Sjlemon * buckets/2. 28485373Sjlemon */ 28585373Sjlemon if (trunk->refcnt > (b * b) / 2) { 28685373Sjlemon vlan_growhash(trunk, 1); 28785373Sjlemon i = HASH(ifv->ifv_tag, trunk->hmask); 288120456Sphk } 289120456Sphk LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 29056582Sbde trunk->refcnt++; 29185373Sjlemon 292120456Sphk return (0); 29385373Sjlemon} 294111119Simp 29585373Sjlemonstatic int 29685373Sjlemonvlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 297120456Sphk{ 29885373Sjlemon int i, b; 29985373Sjlemon struct ifvlan *ifv2; 30085373Sjlemon 301120456Sphk TRUNK_LOCK_ASSERT(trunk); 302120456Sphk KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 30385373Sjlemon 30485373Sjlemon b = 1 << trunk->hwidth; 30585373Sjlemon i = HASH(ifv->ifv_tag, trunk->hmask); 30685373Sjlemon LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 30785373Sjlemon if (ifv2 == ifv) { 30885373Sjlemon trunk->refcnt--; 30985373Sjlemon LIST_REMOVE(ifv2, ifv_list); 31085373Sjlemon if (trunk->refcnt < (b * b) / 2) 31185373Sjlemon vlan_growhash(trunk, -1); 31285373Sjlemon return (0); 31385373Sjlemon } 31485373Sjlemon 315120456Sphk panic("%s: vlan not found\n", __func__); 31685373Sjlemon return (ENOENT); /*NOTREACHED*/ 31785373Sjlemon} 31885373Sjlemon 31985373Sjlemon/* 32085373Sjlemon * Grow the hash larger or smaller if memory permits. 32185373Sjlemon */ 32285373Sjlemonstatic void 32385373Sjlemonvlan_growhash(struct ifvlantrunk *trunk, int howmuch) 32485373Sjlemon{ 32585373Sjlemon struct ifvlan *ifv; 32685373Sjlemon struct ifvlanhead *hash2; 32785373Sjlemon int hwidth2, i, j, n, n2; 32885373Sjlemon 32985373Sjlemon TRUNK_LOCK_ASSERT(trunk); 33027982Sjulian KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 33127982Sjulian 33285373Sjlemon if (howmuch == 0) { 33385373Sjlemon /* Harmless yet obvious coding error */ 33485373Sjlemon printf("%s: howmuch is 0\n", __func__); 33527982Sjulian return; 33627982Sjulian } 33727982Sjulian 33827982Sjulian hwidth2 = trunk->hwidth + howmuch; 33912675Sjulian n = 1 << trunk->hwidth; 34062573Sphk n2 = 1 << hwidth2; 34127982Sjulian /* Do not shrink the table below the default */ 34227982Sjulian if (hwidth2 < VLAN_DEF_HWIDTH) 34327982Sjulian return; 34427982Sjulian 34527982Sjulian /* M_NOWAIT because we're called with trunk mutex held */ 34627982Sjulian hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT); 34785373Sjlemon if (hash2 == NULL) { 34885373Sjlemon printf("%s: out of memory -- hash size not changed\n", 34985373Sjlemon __func__); 35085373Sjlemon return; /* We can live with the old hash table */ 35185373Sjlemon } 35285373Sjlemon for (j = 0; j < n2; j++) 35385373Sjlemon LIST_INIT(&hash2[j]); 35427982Sjulian for (i = 0; i < n; i++) 35527982Sjulian while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) { 35627982Sjulian LIST_REMOVE(ifv, ifv_list); 35727982Sjulian j = HASH(ifv->ifv_tag, n2 - 1); 35827982Sjulian LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 35985373Sjlemon } 36027982Sjulian free(trunk->hash, M_VLAN); 36127982Sjulian trunk->hash = hash2; 36285373Sjlemon trunk->hwidth = hwidth2; 36385373Sjlemon trunk->hmask = n2 - 1; 36485373Sjlemon 36585884Sjlemon if (bootverbose) 36685884Sjlemon if_printf(trunk->parent, 3674Srgrimes "VLAN hash table resized from %d to %d buckets\n", n, n2); 36885373Sjlemon} 3691007Sdg 37085884Sjlemonstatic __inline struct ifvlan * 37185884Sjlemonvlan_gethash(struct ifvlantrunk *trunk, uint16_t tag) 37285884Sjlemon{ 37385373Sjlemon struct ifvlan *ifv; 37427982Sjulian 37585373Sjlemon TRUNK_LOCK_RASSERT(trunk); 37691406Sjhb 3775764Sbde LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list) 378120456Sphk if (ifv->ifv_tag == tag) 37985373Sjlemon return (ifv); 380118094Sphk return (NULL); 38185373Sjlemon} 38285373Sjlemon 38385373Sjlemon#if 0 38485373Sjlemon/* Debugging code to view the hashtables. */ 38585373Sjlemonstatic void 38685373Sjlemonvlan_dumphash(struct ifvlantrunk *trunk) 38791406Sjhb{ 38885373Sjlemon int i; 38985373Sjlemon struct ifvlan *ifv; 3904Srgrimes 3918876Srgrimes for (i = 0; i < (1 << trunk->hwidth); i++) { 39212675Sjulian printf("%d: ", i); 39385373Sjlemon LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 3944Srgrimes printf("%s ", ifv->ifv_ifp->if_xname); 39585373Sjlemon printf("\n"); 3961007Sdg } 39785448Sjlemon} 39885373Sjlemon#endif /* 0 */ 39985373Sjlemon#else 4004Srgrimes 40185373Sjlemonstatic __inline struct ifvlan * 40285373Sjlemonvlan_gethash(struct ifvlantrunk *trunk, uint16_t tag) 40385373Sjlemon{ 40485373Sjlemon 40585373Sjlemon return trunk->vlans[tag]; 40685373Sjlemon} 40785373Sjlemon 40885373Sjlemonstatic __inline int 40985373Sjlemonvlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 41085458Sjlemon{ 41185373Sjlemon 41285373Sjlemon if (trunk->vlans[ifv->ifv_tag] != NULL) 41385458Sjlemon return EEXIST; 41485373Sjlemon trunk->vlans[ifv->ifv_tag] = ifv; 41585373Sjlemon trunk->refcnt++; 41691406Sjhb 4175764Sbde return (0); 41885373Sjlemon} 41927982Sjulian 4204Srgrimesstatic __inline int 4218876Srgrimesvlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 42212675Sjulian{ 42385373Sjlemon 4244Srgrimes trunk->vlans[ifv->ifv_tag] = NULL; 42585373Sjlemon trunk->refcnt--; 42656582Sbde 42785373Sjlemon return (0); 42885373Sjlemon} 4294Srgrimes 43085373Sjlemonstatic __inline void 43146676Sphkvlan_freehash(struct ifvlantrunk *trunk) 4324Srgrimes{ 4338876Srgrimes} 43412675Sjulian 43585373Sjlemonstatic __inline void 4364Srgrimesvlan_inithash(struct ifvlantrunk *trunk) 43785373Sjlemon{ 43856582Sbde} 43985373Sjlemon 44085373Sjlemon#endif /* !VLAN_ARRAY */ 44185373Sjlemon 4421021Sdgstatic void 4434Srgrimestrunk_destroy(struct ifvlantrunk *trunk) 4444Srgrimes{ 44585373Sjlemon VLAN_LOCK_ASSERT(); 44685373Sjlemon 44785373Sjlemon TRUNK_LOCK(trunk); 44885373Sjlemon vlan_freehash(trunk); 44985373Sjlemon trunk->parent->if_vlantrunk = NULL; 45085373Sjlemon TRUNK_UNLOCK(trunk); 45185373Sjlemon TRUNK_LOCK_DESTROY(trunk); 45285373Sjlemon free(trunk, M_VLAN); 4534Srgrimes} 4548876Srgrimes 45512675Sjulian/* 45685373Sjlemon * Program our multicast filter. What we're actually doing is 4574Srgrimes * programming the multicast filter of the parent. This has the 45885373Sjlemon * side effect of causing the parent interface to receive multicast 4594Srgrimes * traffic that it doesn't really want, which ends up being discarded 4604Srgrimes * later by the upper protocol layers. Unfortunately, there's no way 46185373Sjlemon * to avoid this: there really is only one physical interface. 46285373Sjlemon * 4634Srgrimes * XXX: There is a possible race here if more than one thread is 4644Srgrimes * modifying the multicast state of the vlan interface at the same time. 4654Srgrimes */ 4664Srgrimesstatic int 4674Srgrimesvlan_setmulti(struct ifnet *ifp) 4684Srgrimes{ 46993593Sjhb struct ifnet *ifp_p; 4704Srgrimes struct ifmultiaddr *ifma, *rifma = NULL; 4714Srgrimes struct ifvlan *sc; 4724Srgrimes struct vlan_mc_entry *mc; 4734Srgrimes int error; 4744Srgrimes 47585373Sjlemon /*VLAN_LOCK_ASSERT();*/ 47685373Sjlemon 47785373Sjlemon /* Find the parent. */ 47885373Sjlemon sc = ifp->if_softc; 4794Srgrimes ifp_p = PARENT(sc); 4804Srgrimes 48185373Sjlemon CURVNET_SET_QUIET(ifp_p->if_vnet); 48285373Sjlemon 48385373Sjlemon /* First, remove any existing filter entries. */ 48485373Sjlemon while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 48512675Sjulian error = if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 48685373Sjlemon if (error) 4874Srgrimes return (error); 48885373Sjlemon SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 4896712Spst free(mc, M_VLAN); 49085373Sjlemon } 49185373Sjlemon 49285373Sjlemon /* Now program new ones. */ 49385373Sjlemon TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 49485373Sjlemon if (ifma->ifma_addr->sa_family != AF_LINK) 49585373Sjlemon continue; 49685373Sjlemon mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 4974Srgrimes if (mc == NULL) 4984Srgrimes return (ENOMEM); 49972521Sjlemon bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 50085373Sjlemon mc->mc_addr.sdl_index = ifp_p->if_index; 50172521Sjlemon SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 50285373Sjlemon error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 50385373Sjlemon &rifma); 50485373Sjlemon if (error) 50585373Sjlemon return (error); 50672521Sjlemon } 50785373Sjlemon 50885373Sjlemon CURVNET_RESTORE(); 50972521Sjlemon return (0); 51072521Sjlemon} 51172521Sjlemon 51272521Sjlemon/* 51385373Sjlemon * A handler for parent interface link layer address changes. 51485373Sjlemon * If the parent interface link layer address is changed we 51585373Sjlemon * should also change it on all children vlans. 516798Swollman */ 51785373Sjlemonstatic void 5184Srgrimesvlan_iflladdr(void *arg __unused, struct ifnet *ifp) 5195160Sjoerg{ 52085373Sjlemon struct ifvlan *ifv; 52185373Sjlemon#ifndef VLAN_ARRAY 52219268Sjulian struct ifvlan *next; 52385373Sjlemon#endif 52485373Sjlemon int i; 52585373Sjlemon 52685373Sjlemon /* 5275160Sjoerg * Check if it's a trunk interface first of all 5284Srgrimes * to avoid needless locking. 5294Srgrimes */ 5303728Sphk if (ifp->if_vlantrunk == NULL) 53185373Sjlemon return; 5323728Sphk 53385373Sjlemon VLAN_LOCK(); 53485373Sjlemon /* 53585373Sjlemon * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 53685373Sjlemon */ 53785373Sjlemon#ifdef VLAN_ARRAY 53818287Sbde for (i = 0; i < VLAN_ARRAY_SIZE; i++) 53985373Sjlemon if ((ifv = ifp->if_vlantrunk->vlans[i])) { 54085373Sjlemon#else /* VLAN_ARRAY */ 541121182Srwatson for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 542121182Srwatson LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) { 543121182Srwatson#endif /* VLAN_ARRAY */ 544121182Srwatson VLAN_UNLOCK(); 545121182Srwatson if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp), 54685373Sjlemon ifp->if_addrlen); 54785373Sjlemon VLAN_LOCK(); 54885373Sjlemon } 5493728Sphk VLAN_UNLOCK(); 5503728Sphk 551798Swollman} 55285373Sjlemon 5534Srgrimes/* 55485373Sjlemon * A handler for network interface departure events. 55585373Sjlemon * Track departure of trunks here so that we don't access invalid 55687620Sguido * pointers or whatever if a trunk is ripped from under us, e.g., 55785373Sjlemon * by ejecting its hot-plug card. However, if an ifnet is simply 55885373Sjlemon * being renamed, then there's no need to tear down the state. 5594Srgrimes */ 56085373Sjlemonstatic void 56185373Sjlemonvlan_ifdetach(void *arg __unused, struct ifnet *ifp) 562121182Srwatson{ 563121182Srwatson struct ifvlan *ifv; 564121182Srwatson int i; 565121182Srwatson 566121182Srwatson /* 5674Srgrimes * Check if it's a trunk interface first of all 56887649Sguido * to avoid needless locking. 56987620Sguido */ 57087649Sguido if (ifp->if_vlantrunk == NULL) 57187649Sguido return; 57287649Sguido 57387620Sguido /* If the ifnet is just being renamed, don't do anything. */ 57487620Sguido if (ifp->if_flags & IFF_RENAMING) 57587620Sguido return; 57687620Sguido 57787620Sguido VLAN_LOCK(); 57887620Sguido /* 57987620Sguido * OK, it's a trunk. Loop over and detach all vlan's on it. 58087620Sguido * Check trunk pointer after each vlan_unconfig() as it will 58187620Sguido * free it and set to NULL after the last vlan was detached. 5824Srgrimes */ 5834Srgrimes#ifdef VLAN_ARRAY 58455823Syokota for (i = 0; i < VLAN_ARRAY_SIZE; i++) 58585373Sjlemon if ((ifv = ifp->if_vlantrunk->vlans[i])) { 58655823Syokota vlan_unconfig_locked(ifv->ifv_ifp, 1); 58785373Sjlemon if (ifp->if_vlantrunk == NULL) 58885373Sjlemon break; 58955823Syokota } 59055823Syokota#else /* VLAN_ARRAY */ 59155823Syokotarestart: 59255823Syokota for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 59385373Sjlemon if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) { 59485373Sjlemon vlan_unconfig_locked(ifv->ifv_ifp, 1); 59585373Sjlemon if (ifp->if_vlantrunk) 59685373Sjlemon goto restart; /* trunk->hwidth can change */ 597111194Sphk else 59885373Sjlemon break; 59955823Syokota } 60055823Syokota#endif /* VLAN_ARRAY */ 60155823Syokota /* Trunk should have been destroyed in vlan_unconfig(). */ 602112046Sphk KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 603116663Siedowse VLAN_UNLOCK(); 604116663Siedowse} 605116663Siedowse 606116663Siedowse/* 607116663Siedowse * Return the trunk device for a virtual interface. 608116663Siedowse */ 609116663Siedowsestatic struct ifnet * 610116663Siedowsevlan_trunkdev(struct ifnet *ifp) 611116663Siedowse{ 612116663Siedowse struct ifvlan *ifv; 613116663Siedowse 614116663Siedowse if (ifp->if_type != IFT_L2VLAN) 615116663Siedowse return (NULL); 616116663Siedowse ifv = ifp->if_softc; 617116663Siedowse ifp = NULL; 618116663Siedowse VLAN_LOCK(); 619116663Siedowse if (ifv->ifv_trunk) 620116663Siedowse ifp = PARENT(ifv); 621116663Siedowse VLAN_UNLOCK(); 622116663Siedowse return (ifp); 623116663Siedowse} 624116663Siedowse 625116663Siedowse/* 626116663Siedowse * Return the 16bit vlan tag for this interface. 627116663Siedowse */ 628116663Siedowsestatic int 629116663Siedowsevlan_tag(struct ifnet *ifp, uint16_t *tagp) 630116663Siedowse{ 631116663Siedowse struct ifvlan *ifv; 632116663Siedowse 633116663Siedowse if (ifp->if_type != IFT_L2VLAN) 634116663Siedowse return (EINVAL); 635116663Siedowse ifv = ifp->if_softc; 636116663Siedowse *tagp = ifv->ifv_tag; 637116663Siedowse return (0); 638116663Siedowse} 639116663Siedowse 640116663Siedowse/* 641116663Siedowse * Return a driver specific cookie for this interface. Synchronization 642116663Siedowse * with setcookie must be provided by the driver. 643116663Siedowse */ 644116663Siedowsestatic void * 645116663Siedowsevlan_cookie(struct ifnet *ifp) 646116663Siedowse{ 647116663Siedowse struct ifvlan *ifv; 648116663Siedowse 649112046Sphk if (ifp->if_type != IFT_L2VLAN) 650116663Siedowse return (NULL); 651116663Siedowse ifv = ifp->if_softc; 652116663Siedowse return (ifv->ifv_cookie); 653116663Siedowse} 654116663Siedowse 655116663Siedowse/* 656116663Siedowse * Store a cookie in our softc that drivers can use to store driver 657116663Siedowse * private per-instance data in. 658116663Siedowse */ 659116663Siedowsestatic int 660116663Siedowsevlan_setcookie(struct ifnet *ifp, void *cookie) 661116663Siedowse{ 662116663Siedowse struct ifvlan *ifv; 663116663Siedowse 664116663Siedowse if (ifp->if_type != IFT_L2VLAN) 665116663Siedowse return (EINVAL); 666116663Siedowse ifv = ifp->if_softc; 667116663Siedowse ifv->ifv_cookie = cookie; 668112046Sphk return (0); 669112046Sphk} 670112046Sphk 671112046Sphk/* 672112046Sphk * Return the vlan device present at the specific tag. 673112046Sphk */ 674112046Sphkstatic 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_IP_FRAGS | 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