1c398230bSWarner Losh /*- 22cc2df49SGarrett Wollman * Copyright 1998 Massachusetts Institute of Technology 32ccbbd06SMarcelo Araujo * Copyright 2012 ADARA Networks, Inc. 4d148c2a2SMatt Joras * Copyright 2017 Dell EMC Isilon 52ccbbd06SMarcelo Araujo * 62ccbbd06SMarcelo Araujo * Portions of this software were developed by Robert N. M. Watson under 72ccbbd06SMarcelo Araujo * contract to ADARA Networks, Inc. 82cc2df49SGarrett Wollman * 92cc2df49SGarrett Wollman * Permission to use, copy, modify, and distribute this software and 102cc2df49SGarrett Wollman * its documentation for any purpose and without fee is hereby 112cc2df49SGarrett Wollman * granted, provided that both the above copyright notice and this 122cc2df49SGarrett Wollman * permission notice appear in all copies, that both the above 132cc2df49SGarrett Wollman * copyright notice and this permission notice appear in all 142cc2df49SGarrett Wollman * supporting documentation, and that the name of M.I.T. not be used 152cc2df49SGarrett Wollman * in advertising or publicity pertaining to distribution of the 162cc2df49SGarrett Wollman * software without specific, written prior permission. M.I.T. makes 172cc2df49SGarrett Wollman * no representations about the suitability of this software for any 182cc2df49SGarrett Wollman * purpose. It is provided "as is" without express or implied 192cc2df49SGarrett Wollman * warranty. 202cc2df49SGarrett Wollman * 212cc2df49SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 222cc2df49SGarrett Wollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 232cc2df49SGarrett Wollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 242cc2df49SGarrett Wollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 252cc2df49SGarrett Wollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 262cc2df49SGarrett Wollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 272cc2df49SGarrett Wollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 282cc2df49SGarrett Wollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 292cc2df49SGarrett Wollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 302cc2df49SGarrett Wollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 312cc2df49SGarrett Wollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 322cc2df49SGarrett Wollman * SUCH DAMAGE. 332cc2df49SGarrett Wollman */ 342cc2df49SGarrett Wollman 352cc2df49SGarrett Wollman /* 362cc2df49SGarrett Wollman * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 372ccbbd06SMarcelo Araujo * This is sort of sneaky in the implementation, since 382cc2df49SGarrett Wollman * we need to pretend to be enough of an Ethernet implementation 392cc2df49SGarrett Wollman * to make arp work. The way we do this is by telling everyone 402cc2df49SGarrett Wollman * that we are an Ethernet, and then catch the packets that 41d9b1d615SJohn Baldwin * ether_output() sends to us via if_transmit(), rewrite them for 42d9b1d615SJohn Baldwin * use by the real outgoing interface, and ask it to send them. 432cc2df49SGarrett Wollman */ 442cc2df49SGarrett Wollman 458b2d9181SPyun YongHyeon #include <sys/cdefs.h> 468b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$"); 478b2d9181SPyun YongHyeon 482c5b403eSOleg Bulyzhin #include "opt_inet.h" 4975ee267cSGleb Smirnoff #include "opt_vlan.h" 50f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 512cc2df49SGarrett Wollman 522cc2df49SGarrett Wollman #include <sys/param.h> 53c3322cb9SGleb Smirnoff #include <sys/eventhandler.h> 542cc2df49SGarrett Wollman #include <sys/kernel.h> 5575ee267cSGleb Smirnoff #include <sys/lock.h> 56f731f104SBill Paul #include <sys/malloc.h> 572cc2df49SGarrett Wollman #include <sys/mbuf.h> 582b120974SPeter Wemm #include <sys/module.h> 59772b000fSAlexander V. Chernikov #include <sys/rmlock.h> 602ccbbd06SMarcelo Araujo #include <sys/priv.h> 61f731f104SBill Paul #include <sys/queue.h> 622cc2df49SGarrett Wollman #include <sys/socket.h> 632cc2df49SGarrett Wollman #include <sys/sockio.h> 642cc2df49SGarrett Wollman #include <sys/sysctl.h> 652cc2df49SGarrett Wollman #include <sys/systm.h> 66e4cd31ddSJeff Roberson #include <sys/sx.h> 67d148c2a2SMatt Joras #include <sys/taskqueue.h> 682cc2df49SGarrett Wollman 692cc2df49SGarrett Wollman #include <net/bpf.h> 702cc2df49SGarrett Wollman #include <net/ethernet.h> 712cc2df49SGarrett Wollman #include <net/if.h> 7276039bc8SGleb Smirnoff #include <net/if_var.h> 73f889d2efSBrooks Davis #include <net/if_clone.h> 742cc2df49SGarrett Wollman #include <net/if_dl.h> 752cc2df49SGarrett Wollman #include <net/if_types.h> 762cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 774b79449eSBjoern A. Zeeb #include <net/vnet.h> 782cc2df49SGarrett Wollman 792c5b403eSOleg Bulyzhin #ifdef INET 802c5b403eSOleg Bulyzhin #include <netinet/in.h> 812c5b403eSOleg Bulyzhin #include <netinet/if_ether.h> 822c5b403eSOleg Bulyzhin #endif 832c5b403eSOleg Bulyzhin 8475ee267cSGleb Smirnoff #define VLAN_DEF_HWIDTH 4 8564a17d2eSYaroslav Tykhiy #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 8675ee267cSGleb Smirnoff 872dc879b3SYaroslav Tykhiy #define UP_AND_RUNNING(ifp) \ 882dc879b3SYaroslav Tykhiy ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 892dc879b3SYaroslav Tykhiy 90b08d611dSMatt Macy CK_SLIST_HEAD(ifvlanhead, ifvlan); 9175ee267cSGleb Smirnoff 9275ee267cSGleb Smirnoff struct ifvlantrunk { 9375ee267cSGleb Smirnoff struct ifnet *parent; /* parent interface of this trunk */ 94b08d611dSMatt Macy struct mtx lock; 9575ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 965cb8c31aSYaroslav Tykhiy #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 975cb8c31aSYaroslav Tykhiy struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 9875ee267cSGleb Smirnoff #else 9975ee267cSGleb Smirnoff struct ifvlanhead *hash; /* dynamic hash-list table */ 10075ee267cSGleb Smirnoff uint16_t hmask; 10175ee267cSGleb Smirnoff uint16_t hwidth; 10275ee267cSGleb Smirnoff #endif 10375ee267cSGleb Smirnoff int refcnt; 10475ee267cSGleb Smirnoff }; 1059d4fe4b2SBrooks Davis 106d148c2a2SMatt Joras /* 107d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk with 108d148c2a2SMatt Joras * the assumption that none will be added/removed during iteration. 109d148c2a2SMatt Joras */ 110d148c2a2SMatt Joras #ifdef VLAN_ARRAY 111d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 112d148c2a2SMatt Joras size_t _i; \ 113d148c2a2SMatt Joras for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \ 114d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i]) != NULL) 115d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 116d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 117d148c2a2SMatt Joras struct ifvlan *_next; \ 118d148c2a2SMatt Joras size_t _i; \ 119d148c2a2SMatt Joras for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \ 120b08d611dSMatt Macy CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next) 121d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 122d148c2a2SMatt Joras 123d148c2a2SMatt Joras /* 124d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk while 125d148c2a2SMatt Joras * also modifying the number of vlans on the trunk. The iteration continues 126d148c2a2SMatt Joras * until some condition is met or there are no more vlans on the trunk. 127d148c2a2SMatt Joras */ 128d148c2a2SMatt Joras #ifdef VLAN_ARRAY 129d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */ 130d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 131d148c2a2SMatt Joras size_t _i; \ 132d148c2a2SMatt Joras for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \ 133d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i])) 134d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 135d148c2a2SMatt Joras /* 136d148c2a2SMatt Joras * The hash table case is more complicated. We allow for the hash table to be 137d148c2a2SMatt Joras * modified (i.e. vlans removed) while we are iterating over it. To allow for 138d148c2a2SMatt Joras * this we must restart the iteration every time we "touch" something during 139d148c2a2SMatt Joras * the iteration, since removal will resize the hash table and invalidate our 140d148c2a2SMatt Joras * current position. If acting on the touched element causes the trunk to be 141d148c2a2SMatt Joras * emptied, then iteration also stops. 142d148c2a2SMatt Joras */ 143d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 144d148c2a2SMatt Joras size_t _i; \ 145d148c2a2SMatt Joras bool _touch = false; \ 146d148c2a2SMatt Joras for (_i = 0; \ 147d148c2a2SMatt Joras !(_cond) && _i < (1 << (_trunk)->hwidth); \ 148d148c2a2SMatt Joras _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \ 149b08d611dSMatt Macy if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \ 150d148c2a2SMatt Joras (_touch = true)) 151d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 152d148c2a2SMatt Joras 153a3814acfSSam Leffler struct vlan_mc_entry { 154e4cd31ddSJeff Roberson struct sockaddr_dl mc_addr; 155b08d611dSMatt Macy CK_SLIST_ENTRY(vlan_mc_entry) mc_entries; 156c32a9d66SHans Petter Selasky struct epoch_context mc_epoch_ctx; 157a3814acfSSam Leffler }; 158a3814acfSSam Leffler 159a3814acfSSam Leffler struct ifvlan { 16075ee267cSGleb Smirnoff struct ifvlantrunk *ifv_trunk; 161fc74a9f9SBrooks Davis struct ifnet *ifv_ifp; 16275ee267cSGleb Smirnoff #define TRUNK(ifv) ((ifv)->ifv_trunk) 16375ee267cSGleb Smirnoff #define PARENT(ifv) ((ifv)->ifv_trunk->parent) 1646667db31SAlexander V. Chernikov void *ifv_cookie; 1651cf236fbSYaroslav Tykhiy int ifv_pflags; /* special flags we have set on parent */ 166d89baa5aSAlexander Motin int ifv_capenable; 167*72755d28SMark Johnston int ifv_encaplen; /* encapsulation length */ 168*72755d28SMark Johnston int ifv_mtufudge; /* MTU fudged by this much */ 169*72755d28SMark Johnston int ifv_mintu; /* min transmission unit */ 170*72755d28SMark Johnston uint16_t ifv_proto; /* encapsulation ethertype */ 171*72755d28SMark Johnston uint16_t ifv_tag; /* tag to apply on packets leaving if */ 172*72755d28SMark Johnston uint16_t ifv_vid; /* VLAN ID */ 173*72755d28SMark Johnston uint8_t ifv_pcp; /* Priority Code Point (PCP). */ 174d148c2a2SMatt Joras struct task lladdr_task; 175b08d611dSMatt Macy CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 176c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY 177b08d611dSMatt Macy CK_SLIST_ENTRY(ifvlan) ifv_list; 178c0cb022bSYaroslav Tykhiy #endif 179a3814acfSSam Leffler }; 180a3814acfSSam Leffler 18175ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */ 1821cf236fbSYaroslav Tykhiy static struct { 1831cf236fbSYaroslav Tykhiy int flag; 1841cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 1851cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 1861cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 1871cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 1881cf236fbSYaroslav Tykhiy {0, NULL} 1891cf236fbSYaroslav Tykhiy }; 190a3814acfSSam Leffler 191f1379734SKonstantin Belousov extern int vlan_mtag_pcp; 1922ccbbd06SMarcelo Araujo 19342a58907SGleb Smirnoff static const char vlanname[] = "vlan"; 19442a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface"); 1952cc2df49SGarrett Wollman 1965cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag; 197ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag; 1985cb8c31aSYaroslav Tykhiy 1994faedfe8SSam Leffler /* 200b08d611dSMatt Macy * if_vlan uses two module-level synchronizations primitives to allow concurrent 201b08d611dSMatt Macy * modification of vlan interfaces and (mostly) allow for vlans to be destroyed 202b08d611dSMatt Macy * while they are being used for tx/rx. To accomplish this in a way that has 203b08d611dSMatt Macy * acceptable performance and cooperation with other parts of the network stack 204b08d611dSMatt Macy * there is a non-sleepable epoch(9) and an sx(9). 20575ee267cSGleb Smirnoff * 206b08d611dSMatt Macy * The performance-sensitive paths that warrant using the epoch(9) are 207d148c2a2SMatt Joras * vlan_transmit and vlan_input. Both have to check for the vlan interface's 208d148c2a2SMatt Joras * existence using if_vlantrunk, and being in the network tx/rx paths the use 209b08d611dSMatt Macy * of an epoch(9) gives a measureable improvement in performance. 21075ee267cSGleb Smirnoff * 211d148c2a2SMatt Joras * The reason for having an sx(9) is mostly because there are still areas that 212d148c2a2SMatt Joras * must be sleepable and also have safe concurrent access to a vlan interface. 213d148c2a2SMatt Joras * Since the sx(9) exists, it is used by default in most paths unless sleeping 214d148c2a2SMatt Joras * is not permitted, or if it is not clear whether sleeping is permitted. 215d148c2a2SMatt Joras * 2164faedfe8SSam Leffler */ 217d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx 218d148c2a2SMatt Joras 219d148c2a2SMatt Joras static struct sx _VLAN_SX_ID; 220d148c2a2SMatt Joras 221d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \ 222d148c2a2SMatt Joras sx_init(&_VLAN_SX_ID, "vlan_sx") 223d148c2a2SMatt Joras 224d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \ 225d148c2a2SMatt Joras sx_destroy(&_VLAN_SX_ID) 226d148c2a2SMatt Joras 227d148c2a2SMatt Joras #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID) 228d148c2a2SMatt Joras #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID) 229d148c2a2SMatt Joras #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID) 230d148c2a2SMatt Joras #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID) 231d148c2a2SMatt Joras #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED) 232d148c2a2SMatt Joras #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED) 233d148c2a2SMatt Joras #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED) 234d148c2a2SMatt Joras 235d148c2a2SMatt Joras 236d148c2a2SMatt Joras /* 237b08d611dSMatt Macy * We also have a per-trunk mutex that should be acquired when changing 238b08d611dSMatt Macy * its state. 239d148c2a2SMatt Joras */ 240b08d611dSMatt Macy #define TRUNK_LOCK_INIT(trunk) mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF) 241b08d611dSMatt Macy #define TRUNK_LOCK_DESTROY(trunk) mtx_destroy(&(trunk)->lock) 242b08d611dSMatt Macy #define TRUNK_WLOCK(trunk) mtx_lock(&(trunk)->lock) 243b08d611dSMatt Macy #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock) 244b08d611dSMatt Macy #define TRUNK_LOCK_ASSERT(trunk) MPASS(in_epoch(net_epoch_preempt) || mtx_owned(&(trunk)->lock)) 245b08d611dSMatt Macy #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED); 24675ee267cSGleb Smirnoff 247d148c2a2SMatt Joras /* 248d148c2a2SMatt Joras * The VLAN_ARRAY substitutes the dynamic hash with a static array 249d148c2a2SMatt Joras * with 4096 entries. In theory this can give a boost in processing, 250d148c2a2SMatt Joras * however in practice it does not. Probably this is because the array 251d148c2a2SMatt Joras * is too big to fit into CPU cache. 252d148c2a2SMatt Joras */ 25375ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 25475ee267cSGleb Smirnoff static void vlan_inithash(struct ifvlantrunk *trunk); 25575ee267cSGleb Smirnoff static void vlan_freehash(struct ifvlantrunk *trunk); 25675ee267cSGleb Smirnoff static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 25775ee267cSGleb Smirnoff static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 25875ee267cSGleb Smirnoff static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 25975ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 2607983103aSRobert Watson uint16_t vid); 26175ee267cSGleb Smirnoff #endif 26275ee267cSGleb Smirnoff static void trunk_destroy(struct ifvlantrunk *trunk); 2634faedfe8SSam Leffler 264114c608cSYaroslav Tykhiy static void vlan_init(void *foo); 265a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 266cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 267f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 268f3e7afe2SHans Petter Selasky static int vlan_snd_tag_alloc(struct ifnet *, 269f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *, struct m_snd_tag **); 270f3e7afe2SHans Petter Selasky #endif 271d9b1d615SJohn Baldwin static void vlan_qflush(struct ifnet *ifp); 2721cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 2731cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 2741cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 275f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 276d9b1d615SJohn Baldwin static int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 2776f359e28SJohn Baldwin static void vlan_unconfig(struct ifnet *ifp); 27828cc4d37SJohn Baldwin static void vlan_unconfig_locked(struct ifnet *ifp, int departing); 27975ee267cSGleb Smirnoff static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag); 280a6fffd6cSBrooks Davis static void vlan_link_state(struct ifnet *ifp); 28175ee267cSGleb Smirnoff static void vlan_capabilities(struct ifvlan *ifv); 28275ee267cSGleb Smirnoff static void vlan_trunk_capabilities(struct ifnet *ifp); 283f731f104SBill Paul 284f941c31aSGleb Smirnoff static struct ifnet *vlan_clone_match_ethervid(const char *, int *); 285f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 2866b7330e2SSam Leffler static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 287f889d2efSBrooks Davis static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 288f889d2efSBrooks Davis 2895cb8c31aSYaroslav Tykhiy static void vlan_ifdetach(void *arg, struct ifnet *ifp); 290ea4ca115SAndrew Thompson static void vlan_iflladdr(void *arg, struct ifnet *ifp); 2915cb8c31aSYaroslav Tykhiy 292d148c2a2SMatt Joras static void vlan_lladdr_fn(void *arg, int pending); 293d148c2a2SMatt Joras 29442a58907SGleb Smirnoff static struct if_clone *vlan_cloner; 2959d4fe4b2SBrooks Davis 296ccf7ba97SMarko Zec #ifdef VIMAGE 2975f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner); 298ccf7ba97SMarko Zec #define V_vlan_cloner VNET(vlan_cloner) 299ccf7ba97SMarko Zec #endif 300ccf7ba97SMarko Zec 30175ee267cSGleb Smirnoff static void 302c32a9d66SHans Petter Selasky vlan_mc_free(struct epoch_context *ctx) 303c32a9d66SHans Petter Selasky { 304c32a9d66SHans Petter Selasky struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx); 305c32a9d66SHans Petter Selasky free(mc, M_VLAN); 306c32a9d66SHans Petter Selasky } 307c32a9d66SHans Petter Selasky 308cac30248SOleg Bulyzhin #ifndef VLAN_ARRAY 309cac30248SOleg Bulyzhin #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 310cac30248SOleg Bulyzhin 311c32a9d66SHans Petter Selasky static void 31275ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk) 31375ee267cSGleb Smirnoff { 31475ee267cSGleb Smirnoff int i, n; 31575ee267cSGleb Smirnoff 31675ee267cSGleb Smirnoff /* 31775ee267cSGleb Smirnoff * The trunk must not be locked here since we call malloc(M_WAITOK). 31875ee267cSGleb Smirnoff * It is OK in case this function is called before the trunk struct 31975ee267cSGleb Smirnoff * gets hooked up and becomes visible from other threads. 32075ee267cSGleb Smirnoff */ 32175ee267cSGleb Smirnoff 32275ee267cSGleb Smirnoff KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 32375ee267cSGleb Smirnoff ("%s: hash already initialized", __func__)); 32475ee267cSGleb Smirnoff 32575ee267cSGleb Smirnoff trunk->hwidth = VLAN_DEF_HWIDTH; 32675ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 32775ee267cSGleb Smirnoff trunk->hmask = n - 1; 32875ee267cSGleb Smirnoff trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 32975ee267cSGleb Smirnoff for (i = 0; i < n; i++) 330b08d611dSMatt Macy CK_SLIST_INIT(&trunk->hash[i]); 33175ee267cSGleb Smirnoff } 33275ee267cSGleb Smirnoff 33375ee267cSGleb Smirnoff static void 33475ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk) 33575ee267cSGleb Smirnoff { 33675ee267cSGleb Smirnoff #ifdef INVARIANTS 33775ee267cSGleb Smirnoff int i; 33875ee267cSGleb Smirnoff 33975ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 34075ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 341b08d611dSMatt Macy KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]), 34275ee267cSGleb Smirnoff ("%s: hash table not empty", __func__)); 34375ee267cSGleb Smirnoff #endif 34475ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 34575ee267cSGleb Smirnoff trunk->hash = NULL; 34675ee267cSGleb Smirnoff trunk->hwidth = trunk->hmask = 0; 34775ee267cSGleb Smirnoff } 34875ee267cSGleb Smirnoff 34975ee267cSGleb Smirnoff static int 35075ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 35175ee267cSGleb Smirnoff { 35275ee267cSGleb Smirnoff int i, b; 35375ee267cSGleb Smirnoff struct ifvlan *ifv2; 35475ee267cSGleb Smirnoff 355b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 35675ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 35775ee267cSGleb Smirnoff 35875ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 3597983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 360b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 3617983103aSRobert Watson if (ifv->ifv_vid == ifv2->ifv_vid) 36275ee267cSGleb Smirnoff return (EEXIST); 36375ee267cSGleb Smirnoff 36475ee267cSGleb Smirnoff /* 36575ee267cSGleb Smirnoff * Grow the hash when the number of vlans exceeds half of the number of 36675ee267cSGleb Smirnoff * hash buckets squared. This will make the average linked-list length 36775ee267cSGleb Smirnoff * buckets/2. 36875ee267cSGleb Smirnoff */ 36975ee267cSGleb Smirnoff if (trunk->refcnt > (b * b) / 2) { 37075ee267cSGleb Smirnoff vlan_growhash(trunk, 1); 3717983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 37275ee267cSGleb Smirnoff } 373b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 37475ee267cSGleb Smirnoff trunk->refcnt++; 37575ee267cSGleb Smirnoff 37675ee267cSGleb Smirnoff return (0); 37775ee267cSGleb Smirnoff } 37875ee267cSGleb Smirnoff 37975ee267cSGleb Smirnoff static int 38075ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 38175ee267cSGleb Smirnoff { 38275ee267cSGleb Smirnoff int i, b; 38375ee267cSGleb Smirnoff struct ifvlan *ifv2; 38475ee267cSGleb Smirnoff 385b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 38675ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 38775ee267cSGleb Smirnoff 38875ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 3897983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 390b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 39175ee267cSGleb Smirnoff if (ifv2 == ifv) { 39275ee267cSGleb Smirnoff trunk->refcnt--; 393b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list); 39475ee267cSGleb Smirnoff if (trunk->refcnt < (b * b) / 2) 39575ee267cSGleb Smirnoff vlan_growhash(trunk, -1); 39675ee267cSGleb Smirnoff return (0); 39775ee267cSGleb Smirnoff } 39875ee267cSGleb Smirnoff 39975ee267cSGleb Smirnoff panic("%s: vlan not found\n", __func__); 40075ee267cSGleb Smirnoff return (ENOENT); /*NOTREACHED*/ 40175ee267cSGleb Smirnoff } 40275ee267cSGleb Smirnoff 40375ee267cSGleb Smirnoff /* 40475ee267cSGleb Smirnoff * Grow the hash larger or smaller if memory permits. 40575ee267cSGleb Smirnoff */ 40675ee267cSGleb Smirnoff static void 40775ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 40875ee267cSGleb Smirnoff { 40975ee267cSGleb Smirnoff struct ifvlan *ifv; 41075ee267cSGleb Smirnoff struct ifvlanhead *hash2; 41175ee267cSGleb Smirnoff int hwidth2, i, j, n, n2; 41275ee267cSGleb Smirnoff 413b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 41475ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 41575ee267cSGleb Smirnoff 41675ee267cSGleb Smirnoff if (howmuch == 0) { 41775ee267cSGleb Smirnoff /* Harmless yet obvious coding error */ 41875ee267cSGleb Smirnoff printf("%s: howmuch is 0\n", __func__); 41975ee267cSGleb Smirnoff return; 42075ee267cSGleb Smirnoff } 42175ee267cSGleb Smirnoff 42275ee267cSGleb Smirnoff hwidth2 = trunk->hwidth + howmuch; 42375ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 42475ee267cSGleb Smirnoff n2 = 1 << hwidth2; 42575ee267cSGleb Smirnoff /* Do not shrink the table below the default */ 42675ee267cSGleb Smirnoff if (hwidth2 < VLAN_DEF_HWIDTH) 42775ee267cSGleb Smirnoff return; 42875ee267cSGleb Smirnoff 429b08d611dSMatt Macy hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK); 43075ee267cSGleb Smirnoff if (hash2 == NULL) { 43175ee267cSGleb Smirnoff printf("%s: out of memory -- hash size not changed\n", 43275ee267cSGleb Smirnoff __func__); 43375ee267cSGleb Smirnoff return; /* We can live with the old hash table */ 43475ee267cSGleb Smirnoff } 43575ee267cSGleb Smirnoff for (j = 0; j < n2; j++) 436b08d611dSMatt Macy CK_SLIST_INIT(&hash2[j]); 43775ee267cSGleb Smirnoff for (i = 0; i < n; i++) 438b08d611dSMatt Macy while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) { 439b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list); 4407983103aSRobert Watson j = HASH(ifv->ifv_vid, n2 - 1); 441b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 44275ee267cSGleb Smirnoff } 443b08d611dSMatt Macy NET_EPOCH_WAIT(); 44475ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 44575ee267cSGleb Smirnoff trunk->hash = hash2; 44675ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 44775ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 448f84b2d69SYaroslav Tykhiy 449f84b2d69SYaroslav Tykhiy if (bootverbose) 450f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 451f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 45275ee267cSGleb Smirnoff } 45375ee267cSGleb Smirnoff 45475ee267cSGleb Smirnoff static __inline struct ifvlan * 4557983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 45675ee267cSGleb Smirnoff { 45775ee267cSGleb Smirnoff struct ifvlan *ifv; 45875ee267cSGleb Smirnoff 459a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 46075ee267cSGleb Smirnoff 461b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list) 4627983103aSRobert Watson if (ifv->ifv_vid == vid) 46375ee267cSGleb Smirnoff return (ifv); 46475ee267cSGleb Smirnoff return (NULL); 46575ee267cSGleb Smirnoff } 46675ee267cSGleb Smirnoff 46775ee267cSGleb Smirnoff #if 0 46875ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 46975ee267cSGleb Smirnoff static void 47075ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 47175ee267cSGleb Smirnoff { 47275ee267cSGleb Smirnoff int i; 47375ee267cSGleb Smirnoff struct ifvlan *ifv; 47475ee267cSGleb Smirnoff 47575ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 47675ee267cSGleb Smirnoff printf("%d: ", i); 477b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 47875ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 47975ee267cSGleb Smirnoff printf("\n"); 48075ee267cSGleb Smirnoff } 48175ee267cSGleb Smirnoff } 48275ee267cSGleb Smirnoff #endif /* 0 */ 483e4cd31ddSJeff Roberson #else 484e4cd31ddSJeff Roberson 485e4cd31ddSJeff Roberson static __inline struct ifvlan * 4867983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 487e4cd31ddSJeff Roberson { 488e4cd31ddSJeff Roberson 4897983103aSRobert Watson return trunk->vlans[vid]; 490e4cd31ddSJeff Roberson } 491e4cd31ddSJeff Roberson 492e4cd31ddSJeff Roberson static __inline int 493e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 494e4cd31ddSJeff Roberson { 495e4cd31ddSJeff Roberson 4967983103aSRobert Watson if (trunk->vlans[ifv->ifv_vid] != NULL) 497e4cd31ddSJeff Roberson return EEXIST; 4987983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = ifv; 499e4cd31ddSJeff Roberson trunk->refcnt++; 500e4cd31ddSJeff Roberson 501e4cd31ddSJeff Roberson return (0); 502e4cd31ddSJeff Roberson } 503e4cd31ddSJeff Roberson 504e4cd31ddSJeff Roberson static __inline int 505e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 506e4cd31ddSJeff Roberson { 507e4cd31ddSJeff Roberson 5087983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = NULL; 509e4cd31ddSJeff Roberson trunk->refcnt--; 510e4cd31ddSJeff Roberson 511e4cd31ddSJeff Roberson return (0); 512e4cd31ddSJeff Roberson } 513e4cd31ddSJeff Roberson 514e4cd31ddSJeff Roberson static __inline void 515e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk) 516e4cd31ddSJeff Roberson { 517e4cd31ddSJeff Roberson } 518e4cd31ddSJeff Roberson 519e4cd31ddSJeff Roberson static __inline void 520e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk) 521e4cd31ddSJeff Roberson { 522e4cd31ddSJeff Roberson } 523e4cd31ddSJeff Roberson 52475ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 52575ee267cSGleb Smirnoff 52675ee267cSGleb Smirnoff static void 52775ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 52875ee267cSGleb Smirnoff { 529d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 53075ee267cSGleb Smirnoff 53175ee267cSGleb Smirnoff vlan_freehash(trunk); 53275ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 53333499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 5349bcf3ae4SAlexander Motin if_rele(trunk->parent); 53575ee267cSGleb Smirnoff free(trunk, M_VLAN); 53675ee267cSGleb Smirnoff } 53775ee267cSGleb Smirnoff 538f731f104SBill Paul /* 539f731f104SBill Paul * Program our multicast filter. What we're actually doing is 540f731f104SBill Paul * programming the multicast filter of the parent. This has the 541f731f104SBill Paul * side effect of causing the parent interface to receive multicast 542f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 543f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 544f731f104SBill Paul * to avoid this: there really is only one physical interface. 545f731f104SBill Paul */ 5462b120974SPeter Wemm static int 5472b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 548f731f104SBill Paul { 549f731f104SBill Paul struct ifnet *ifp_p; 5502d222cb7SAlexander Motin struct ifmultiaddr *ifma; 551f731f104SBill Paul struct ifvlan *sc; 552c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 553f731f104SBill Paul int error; 554f731f104SBill Paul 555b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 556d148c2a2SMatt Joras 557f731f104SBill Paul /* Find the parent. */ 558f731f104SBill Paul sc = ifp->if_softc; 55975ee267cSGleb Smirnoff ifp_p = PARENT(sc); 5601b2a4f7aSBill Fenner 5618b615593SMarko Zec CURVNET_SET_QUIET(ifp_p->if_vnet); 5628b615593SMarko Zec 563f731f104SBill Paul /* First, remove any existing filter entries. */ 564b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 565b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 5662d222cb7SAlexander Motin (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 567c32a9d66SHans Petter Selasky epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free); 568f731f104SBill Paul } 569f731f104SBill Paul 570f731f104SBill Paul /* Now program new ones. */ 5712d222cb7SAlexander Motin IF_ADDR_WLOCK(ifp); 572d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 573f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 574f731f104SBill Paul continue; 57529c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 5762d222cb7SAlexander Motin if (mc == NULL) { 5772d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 57829c2dfbeSBruce M Simpson return (ENOMEM); 5792d222cb7SAlexander Motin } 580e4cd31ddSJeff Roberson bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 581e4cd31ddSJeff Roberson mc->mc_addr.sdl_index = ifp_p->if_index; 582b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 5832d222cb7SAlexander Motin } 5842d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 585b08d611dSMatt Macy CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) { 586e4cd31ddSJeff Roberson error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 5872d222cb7SAlexander Motin NULL); 588f731f104SBill Paul if (error) 589f731f104SBill Paul return (error); 590f731f104SBill Paul } 591f731f104SBill Paul 5928b615593SMarko Zec CURVNET_RESTORE(); 593f731f104SBill Paul return (0); 594f731f104SBill Paul } 5952cc2df49SGarrett Wollman 596a3814acfSSam Leffler /* 597ea4ca115SAndrew Thompson * A handler for parent interface link layer address changes. 598ea4ca115SAndrew Thompson * If the parent interface link layer address is changed we 599ea4ca115SAndrew Thompson * should also change it on all children vlans. 600ea4ca115SAndrew Thompson */ 601ea4ca115SAndrew Thompson static void 602ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 603ea4ca115SAndrew Thompson { 604a68cc388SGleb Smirnoff struct epoch_tracker et; 605ea4ca115SAndrew Thompson struct ifvlan *ifv; 606d148c2a2SMatt Joras struct ifnet *ifv_ifp; 607d148c2a2SMatt Joras struct ifvlantrunk *trunk; 608d148c2a2SMatt Joras struct sockaddr_dl *sdl; 609ea4ca115SAndrew Thompson 610d148c2a2SMatt Joras /* Need the rmlock since this is run on taskqueue_swi. */ 611a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 612d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 613d148c2a2SMatt Joras if (trunk == NULL) { 614a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 615ea4ca115SAndrew Thompson return; 616d148c2a2SMatt Joras } 617ea4ca115SAndrew Thompson 618ea4ca115SAndrew Thompson /* 619ea4ca115SAndrew Thompson * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 620d148c2a2SMatt Joras * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR 621d148c2a2SMatt Joras * ioctl calls on the parent garbling the lladdr of the child vlan. 622ea4ca115SAndrew Thompson */ 623d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 624d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 625d148c2a2SMatt Joras /* 626d148c2a2SMatt Joras * Copy new new lladdr into the ifv_ifp, enqueue a task 627d148c2a2SMatt Joras * to actually call if_setlladdr. if_setlladdr needs to 628d148c2a2SMatt Joras * be deferred to a taskqueue because it will call into 629d148c2a2SMatt Joras * the if_vlan ioctl path and try to acquire the global 630d148c2a2SMatt Joras * lock. 631d148c2a2SMatt Joras */ 632d148c2a2SMatt Joras ifv_ifp = ifv->ifv_ifp; 633d148c2a2SMatt Joras bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp), 634e4cd31ddSJeff Roberson ifp->if_addrlen); 635d148c2a2SMatt Joras sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr; 636d148c2a2SMatt Joras sdl->sdl_alen = ifp->if_addrlen; 637d148c2a2SMatt Joras taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 6386117727bSAndrew Thompson } 639d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 640a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 641ea4ca115SAndrew Thompson } 642ea4ca115SAndrew Thompson 643ea4ca115SAndrew Thompson /* 6445cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 6455cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 6465cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 6475428776eSJohn Baldwin * by ejecting its hot-plug card. However, if an ifnet is simply 6485428776eSJohn Baldwin * being renamed, then there's no need to tear down the state. 6495cb8c31aSYaroslav Tykhiy */ 6505cb8c31aSYaroslav Tykhiy static void 6515cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 6525cb8c31aSYaroslav Tykhiy { 6535cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 654d148c2a2SMatt Joras struct ifvlantrunk *trunk; 6555cb8c31aSYaroslav Tykhiy 6565428776eSJohn Baldwin /* If the ifnet is just being renamed, don't do anything. */ 6575428776eSJohn Baldwin if (ifp->if_flags & IFF_RENAMING) 6585428776eSJohn Baldwin return; 659d148c2a2SMatt Joras VLAN_XLOCK(); 660d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 661d148c2a2SMatt Joras if (trunk == NULL) { 662d148c2a2SMatt Joras VLAN_XUNLOCK(); 663d148c2a2SMatt Joras return; 664d148c2a2SMatt Joras } 6655428776eSJohn Baldwin 6665cb8c31aSYaroslav Tykhiy /* 6675cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 6685cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 6695cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 6705cb8c31aSYaroslav Tykhiy */ 671d148c2a2SMatt Joras VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk, 672d148c2a2SMatt Joras ifp->if_vlantrunk == NULL) 67328cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 674d148c2a2SMatt Joras 6755cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 6765cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 677d148c2a2SMatt Joras VLAN_XUNLOCK(); 6785cb8c31aSYaroslav Tykhiy } 6795cb8c31aSYaroslav Tykhiy 6805cb8c31aSYaroslav Tykhiy /* 681e4cd31ddSJeff Roberson * Return the trunk device for a virtual interface. 682e4cd31ddSJeff Roberson */ 683e4cd31ddSJeff Roberson static struct ifnet * 684e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp) 685e4cd31ddSJeff Roberson { 686a68cc388SGleb Smirnoff struct epoch_tracker et; 687e4cd31ddSJeff Roberson struct ifvlan *ifv; 688e4cd31ddSJeff Roberson 689e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 690e4cd31ddSJeff Roberson return (NULL); 691d148c2a2SMatt Joras 692a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 693e4cd31ddSJeff Roberson ifv = ifp->if_softc; 694e4cd31ddSJeff Roberson ifp = NULL; 695e4cd31ddSJeff Roberson if (ifv->ifv_trunk) 696e4cd31ddSJeff Roberson ifp = PARENT(ifv); 697a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 698e4cd31ddSJeff Roberson return (ifp); 699e4cd31ddSJeff Roberson } 700e4cd31ddSJeff Roberson 701e4cd31ddSJeff Roberson /* 7027983103aSRobert Watson * Return the 12-bit VLAN VID for this interface, for use by external 7037983103aSRobert Watson * components such as Infiniband. 7047983103aSRobert Watson * 7057983103aSRobert Watson * XXXRW: Note that the function name here is historical; it should be named 7067983103aSRobert Watson * vlan_vid(). 707e4cd31ddSJeff Roberson */ 708e4cd31ddSJeff Roberson static int 7097983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp) 710e4cd31ddSJeff Roberson { 711e4cd31ddSJeff Roberson struct ifvlan *ifv; 712e4cd31ddSJeff Roberson 713e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 714e4cd31ddSJeff Roberson return (EINVAL); 715e4cd31ddSJeff Roberson ifv = ifp->if_softc; 7167983103aSRobert Watson *vidp = ifv->ifv_vid; 717e4cd31ddSJeff Roberson return (0); 718e4cd31ddSJeff Roberson } 719e4cd31ddSJeff Roberson 72032d2623aSNavdeep Parhar static int 72132d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp) 72232d2623aSNavdeep Parhar { 72332d2623aSNavdeep Parhar struct ifvlan *ifv; 72432d2623aSNavdeep Parhar 72532d2623aSNavdeep Parhar if (ifp->if_type != IFT_L2VLAN) 72632d2623aSNavdeep Parhar return (EINVAL); 72732d2623aSNavdeep Parhar ifv = ifp->if_softc; 72832d2623aSNavdeep Parhar *pcpp = ifv->ifv_pcp; 72932d2623aSNavdeep Parhar return (0); 73032d2623aSNavdeep Parhar } 73132d2623aSNavdeep Parhar 732e4cd31ddSJeff Roberson /* 733e4cd31ddSJeff Roberson * Return a driver specific cookie for this interface. Synchronization 734e4cd31ddSJeff Roberson * with setcookie must be provided by the driver. 735e4cd31ddSJeff Roberson */ 736e4cd31ddSJeff Roberson static void * 737e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp) 738e4cd31ddSJeff Roberson { 739e4cd31ddSJeff Roberson struct ifvlan *ifv; 740e4cd31ddSJeff Roberson 741e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 742e4cd31ddSJeff Roberson return (NULL); 743e4cd31ddSJeff Roberson ifv = ifp->if_softc; 744e4cd31ddSJeff Roberson return (ifv->ifv_cookie); 745e4cd31ddSJeff Roberson } 746e4cd31ddSJeff Roberson 747e4cd31ddSJeff Roberson /* 748e4cd31ddSJeff Roberson * Store a cookie in our softc that drivers can use to store driver 749e4cd31ddSJeff Roberson * private per-instance data in. 750e4cd31ddSJeff Roberson */ 751e4cd31ddSJeff Roberson static int 752e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie) 753e4cd31ddSJeff Roberson { 754e4cd31ddSJeff Roberson struct ifvlan *ifv; 755e4cd31ddSJeff Roberson 756e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 757e4cd31ddSJeff Roberson return (EINVAL); 758e4cd31ddSJeff Roberson ifv = ifp->if_softc; 759e4cd31ddSJeff Roberson ifv->ifv_cookie = cookie; 760e4cd31ddSJeff Roberson return (0); 761e4cd31ddSJeff Roberson } 762e4cd31ddSJeff Roberson 763e4cd31ddSJeff Roberson /* 7647983103aSRobert Watson * Return the vlan device present at the specific VID. 765e4cd31ddSJeff Roberson */ 766e4cd31ddSJeff Roberson static struct ifnet * 7677983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid) 768e4cd31ddSJeff Roberson { 769a68cc388SGleb Smirnoff struct epoch_tracker et; 770e4cd31ddSJeff Roberson struct ifvlantrunk *trunk; 771e4cd31ddSJeff Roberson struct ifvlan *ifv; 772e4cd31ddSJeff Roberson 773a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 774e4cd31ddSJeff Roberson trunk = ifp->if_vlantrunk; 775d148c2a2SMatt Joras if (trunk == NULL) { 776a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 777e4cd31ddSJeff Roberson return (NULL); 778d148c2a2SMatt Joras } 779e4cd31ddSJeff Roberson ifp = NULL; 7807983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 781e4cd31ddSJeff Roberson if (ifv) 782e4cd31ddSJeff Roberson ifp = ifv->ifv_ifp; 783a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 784e4cd31ddSJeff Roberson return (ifp); 785e4cd31ddSJeff Roberson } 786e4cd31ddSJeff Roberson 787e4cd31ddSJeff Roberson /* 7882ccbbd06SMarcelo Araujo * Recalculate the cached VLAN tag exposed via the MIB. 7892ccbbd06SMarcelo Araujo */ 7902ccbbd06SMarcelo Araujo static void 7912ccbbd06SMarcelo Araujo vlan_tag_recalculate(struct ifvlan *ifv) 7922ccbbd06SMarcelo Araujo { 7932ccbbd06SMarcelo Araujo 7942ccbbd06SMarcelo Araujo ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0); 7952ccbbd06SMarcelo Araujo } 7962ccbbd06SMarcelo Araujo 7972ccbbd06SMarcelo Araujo /* 798a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 799a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 800a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 801a3814acfSSam Leffler * set here. No one else in the system should be aware of this so 802a3814acfSSam Leffler * we use an explicit reference here. 803a3814acfSSam Leffler */ 804a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 805a3814acfSSam Leffler 806984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 807a6fffd6cSBrooks Davis extern void (*vlan_link_state_p)(struct ifnet *); 808127d7b2dSAndre Oppermann 8092b120974SPeter Wemm static int 8102b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 8112b120974SPeter Wemm { 8129d4fe4b2SBrooks Davis 8132b120974SPeter Wemm switch (type) { 8142b120974SPeter Wemm case MOD_LOAD: 8155cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 8165cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 8175cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 8185cb8c31aSYaroslav Tykhiy return (ENOMEM); 819ea4ca115SAndrew Thompson iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 820ea4ca115SAndrew Thompson vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 821ea4ca115SAndrew Thompson if (iflladdr_tag == NULL) 822ea4ca115SAndrew Thompson return (ENOMEM); 823d148c2a2SMatt Joras VLAN_LOCKING_INIT(); 8249d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 825127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 82675ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 827e4cd31ddSJeff Roberson vlan_trunkdev_p = vlan_trunkdev; 828e4cd31ddSJeff Roberson vlan_cookie_p = vlan_cookie; 829e4cd31ddSJeff Roberson vlan_setcookie_p = vlan_setcookie; 830e4cd31ddSJeff Roberson vlan_tag_p = vlan_tag; 83132d2623aSNavdeep Parhar vlan_pcp_p = vlan_pcp; 832e4cd31ddSJeff Roberson vlan_devat_p = vlan_devat; 833ccf7ba97SMarko Zec #ifndef VIMAGE 83442a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 83542a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 836ccf7ba97SMarko Zec #endif 83725c0f7b3SYaroslav Tykhiy if (bootverbose) 83825c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 83925c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 84025c0f7b3SYaroslav Tykhiy "full-size arrays" 84125c0f7b3SYaroslav Tykhiy #else 84225c0f7b3SYaroslav Tykhiy "hash tables with chaining" 84325c0f7b3SYaroslav Tykhiy #endif 84425c0f7b3SYaroslav Tykhiy 84525c0f7b3SYaroslav Tykhiy "\n"); 8462b120974SPeter Wemm break; 8472b120974SPeter Wemm case MOD_UNLOAD: 848ccf7ba97SMarko Zec #ifndef VIMAGE 84942a58907SGleb Smirnoff if_clone_detach(vlan_cloner); 850ccf7ba97SMarko Zec #endif 8515cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 852ea4ca115SAndrew Thompson EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 8539d4fe4b2SBrooks Davis vlan_input_p = NULL; 854127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 85575ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 856e4cd31ddSJeff Roberson vlan_trunkdev_p = NULL; 857e4cd31ddSJeff Roberson vlan_tag_p = NULL; 85809fe6320SNavdeep Parhar vlan_cookie_p = NULL; 85909fe6320SNavdeep Parhar vlan_setcookie_p = NULL; 860e4cd31ddSJeff Roberson vlan_devat_p = NULL; 861d148c2a2SMatt Joras VLAN_LOCKING_DESTROY(); 86225c0f7b3SYaroslav Tykhiy if (bootverbose) 86325c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 8649d4fe4b2SBrooks Davis break; 8653e019deaSPoul-Henning Kamp default: 8663e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 8672b120974SPeter Wemm } 86815a66c21SBruce M Simpson return (0); 8692b120974SPeter Wemm } 8702b120974SPeter Wemm 8712b120974SPeter Wemm static moduledata_t vlan_mod = { 8722b120974SPeter Wemm "if_vlan", 8732b120974SPeter Wemm vlan_modevent, 8749823d527SKevin Lo 0 8752b120974SPeter Wemm }; 8762b120974SPeter Wemm 8772b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 87811edc477SEd Maste MODULE_VERSION(if_vlan, 3); 8792cc2df49SGarrett Wollman 880ccf7ba97SMarko Zec #ifdef VIMAGE 881ccf7ba97SMarko Zec static void 882ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused) 883ccf7ba97SMarko Zec { 884ccf7ba97SMarko Zec 88542a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 88642a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 887ccf7ba97SMarko Zec V_vlan_cloner = vlan_cloner; 888ccf7ba97SMarko Zec } 889ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 890ccf7ba97SMarko Zec vnet_vlan_init, NULL); 891ccf7ba97SMarko Zec 892ccf7ba97SMarko Zec static void 893ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused) 894ccf7ba97SMarko Zec { 895ccf7ba97SMarko Zec 89642a58907SGleb Smirnoff if_clone_detach(V_vlan_cloner); 897ccf7ba97SMarko Zec } 89889856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, 899ccf7ba97SMarko Zec vnet_vlan_uninit, NULL); 900ccf7ba97SMarko Zec #endif 901ccf7ba97SMarko Zec 902f941c31aSGleb Smirnoff /* 903f941c31aSGleb Smirnoff * Check for <etherif>.<vlan> style interface names. 904f941c31aSGleb Smirnoff */ 905f889d2efSBrooks Davis static struct ifnet * 906f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp) 9079d4fe4b2SBrooks Davis { 908f941c31aSGleb Smirnoff char ifname[IFNAMSIZ]; 909f941c31aSGleb Smirnoff char *cp; 910f889d2efSBrooks Davis struct ifnet *ifp; 9117983103aSRobert Watson int vid; 912f889d2efSBrooks Davis 913f941c31aSGleb Smirnoff strlcpy(ifname, name, IFNAMSIZ); 914f941c31aSGleb Smirnoff if ((cp = strchr(ifname, '.')) == NULL) 915f941c31aSGleb Smirnoff return (NULL); 916f941c31aSGleb Smirnoff *cp = '\0'; 9179bcf3ae4SAlexander Motin if ((ifp = ifunit_ref(ifname)) == NULL) 918f941c31aSGleb Smirnoff return (NULL); 919f941c31aSGleb Smirnoff /* Parse VID. */ 9209bcf3ae4SAlexander Motin if (*++cp == '\0') { 9219bcf3ae4SAlexander Motin if_rele(ifp); 922f941c31aSGleb Smirnoff return (NULL); 9239bcf3ae4SAlexander Motin } 9247983103aSRobert Watson vid = 0; 925fb92ad4aSJohn Baldwin for(; *cp >= '0' && *cp <= '9'; cp++) 9267983103aSRobert Watson vid = (vid * 10) + (*cp - '0'); 9279bcf3ae4SAlexander Motin if (*cp != '\0') { 9289bcf3ae4SAlexander Motin if_rele(ifp); 929f941c31aSGleb Smirnoff return (NULL); 9309bcf3ae4SAlexander Motin } 9317983103aSRobert Watson if (vidp != NULL) 9327983103aSRobert Watson *vidp = vid; 933f889d2efSBrooks Davis 93415a66c21SBruce M Simpson return (ifp); 935f889d2efSBrooks Davis } 936f889d2efSBrooks Davis 937f889d2efSBrooks Davis static int 938f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 939f889d2efSBrooks Davis { 940f889d2efSBrooks Davis const char *cp; 941f889d2efSBrooks Davis 942f941c31aSGleb Smirnoff if (vlan_clone_match_ethervid(name, NULL) != NULL) 943f889d2efSBrooks Davis return (1); 944f889d2efSBrooks Davis 94542a58907SGleb Smirnoff if (strncmp(vlanname, name, strlen(vlanname)) != 0) 946f889d2efSBrooks Davis return (0); 947f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 948f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 949f889d2efSBrooks Davis return (0); 950f889d2efSBrooks Davis } 951f889d2efSBrooks Davis 952f889d2efSBrooks Davis return (1); 953f889d2efSBrooks Davis } 954f889d2efSBrooks Davis 955f889d2efSBrooks Davis static int 9566b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 957f889d2efSBrooks Davis { 958f889d2efSBrooks Davis char *dp; 959f889d2efSBrooks Davis int wildcard; 960f889d2efSBrooks Davis int unit; 961f889d2efSBrooks Davis int error; 9627983103aSRobert Watson int vid; 9639d4fe4b2SBrooks Davis struct ifvlan *ifv; 9649d4fe4b2SBrooks Davis struct ifnet *ifp; 965f889d2efSBrooks Davis struct ifnet *p; 9663ba24fdeSJohn Baldwin struct ifaddr *ifa; 9673ba24fdeSJohn Baldwin struct sockaddr_dl *sdl; 9686b7330e2SSam Leffler struct vlanreq vlr; 96965239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 970f889d2efSBrooks Davis 9716b7330e2SSam Leffler /* 9726b7330e2SSam Leffler * There are 3 (ugh) ways to specify the cloned device: 9736b7330e2SSam Leffler * o pass a parameter block with the clone request. 9746b7330e2SSam Leffler * o specify parameters in the text of the clone device name 9756b7330e2SSam Leffler * o specify no parameters and get an unattached device that 9766b7330e2SSam Leffler * must be configured separately. 9776b7330e2SSam Leffler * The first technique is preferred; the latter two are 978a4641f4eSPedro F. Giffuni * supported for backwards compatibility. 9797983103aSRobert Watson * 9807983103aSRobert Watson * XXXRW: Note historic use of the word "tag" here. New ioctls may be 9817983103aSRobert Watson * called for. 9826b7330e2SSam Leffler */ 9836b7330e2SSam Leffler if (params) { 9846b7330e2SSam Leffler error = copyin(params, &vlr, sizeof(vlr)); 9856b7330e2SSam Leffler if (error) 9866b7330e2SSam Leffler return error; 9879bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 9886b7330e2SSam Leffler if (p == NULL) 989b1828acfSGleb Smirnoff return (ENXIO); 9906b7330e2SSam Leffler error = ifc_name2unit(name, &unit); 9919bcf3ae4SAlexander Motin if (error != 0) { 9929bcf3ae4SAlexander Motin if_rele(p); 9936b7330e2SSam Leffler return (error); 9949bcf3ae4SAlexander Motin } 9957983103aSRobert Watson vid = vlr.vlr_tag; 9966b7330e2SSam Leffler wildcard = (unit < 0); 997f941c31aSGleb Smirnoff } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) { 998f889d2efSBrooks Davis unit = -1; 999f889d2efSBrooks Davis wildcard = 0; 1000f889d2efSBrooks Davis } else { 10019bcf3ae4SAlexander Motin p = NULL; 1002f889d2efSBrooks Davis error = ifc_name2unit(name, &unit); 1003f889d2efSBrooks Davis if (error != 0) 1004f889d2efSBrooks Davis return (error); 1005f889d2efSBrooks Davis 1006f889d2efSBrooks Davis wildcard = (unit < 0); 1007f889d2efSBrooks Davis } 1008f889d2efSBrooks Davis 1009f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 10109bcf3ae4SAlexander Motin if (error != 0) { 10119bcf3ae4SAlexander Motin if (p != NULL) 10129bcf3ae4SAlexander Motin if_rele(p); 1013f889d2efSBrooks Davis return (error); 10149bcf3ae4SAlexander Motin } 1015f889d2efSBrooks Davis 1016f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 1017f889d2efSBrooks Davis if (wildcard) { 1018f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 1019f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 1020f889d2efSBrooks Davis len - (dp-name) - 1) { 1021f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 1022f889d2efSBrooks Davis } 1023f889d2efSBrooks Davis } 10249d4fe4b2SBrooks Davis 1025a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 1026fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 1027fc74a9f9SBrooks Davis if (ifp == NULL) { 1028fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 1029fc74a9f9SBrooks Davis free(ifv, M_VLAN); 10309bcf3ae4SAlexander Motin if (p != NULL) 10319bcf3ae4SAlexander Motin if_rele(p); 1032fc74a9f9SBrooks Davis return (ENOSPC); 1033fc74a9f9SBrooks Davis } 1034b08d611dSMatt Macy CK_SLIST_INIT(&ifv->vlan_mc_listhead); 10359d4fe4b2SBrooks Davis ifp->if_softc = ifv; 1036f889d2efSBrooks Davis /* 1037cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 1038f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 1039f889d2efSBrooks Davis */ 1040f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 104142a58907SGleb Smirnoff ifp->if_dname = vlanname; 1042f889d2efSBrooks Davis ifp->if_dunit = unit; 10439d4fe4b2SBrooks Davis 1044114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 1045d9b1d615SJohn Baldwin ifp->if_transmit = vlan_transmit; 1046d9b1d615SJohn Baldwin ifp->if_qflush = vlan_qflush; 10479d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 1048f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1049f3e7afe2SHans Petter Selasky ifp->if_snd_tag_alloc = vlan_snd_tag_alloc; 1050f3e7afe2SHans Petter Selasky #endif 105164a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 1052fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 10539d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 1054211f625aSBill Fenner ifp->if_baudrate = 0; 1055a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 1056a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 10573ba24fdeSJohn Baldwin ifa = ifp->if_addr; 10583ba24fdeSJohn Baldwin sdl = (struct sockaddr_dl *)ifa->ifa_addr; 10593ba24fdeSJohn Baldwin sdl->sdl_type = IFT_L2VLAN; 10609d4fe4b2SBrooks Davis 10619bcf3ae4SAlexander Motin if (p != NULL) { 10627983103aSRobert Watson error = vlan_config(ifv, p, vid); 10639bcf3ae4SAlexander Motin if_rele(p); 1064f889d2efSBrooks Davis if (error != 0) { 1065f889d2efSBrooks Davis /* 106628cc4d37SJohn Baldwin * Since we've partially failed, we need to back 1067f889d2efSBrooks Davis * out all the way, otherwise userland could get 1068f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 1069f889d2efSBrooks Davis */ 1070f889d2efSBrooks Davis ether_ifdetach(ifp); 1071249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 10724b22573aSBrooks Davis if_free(ifp); 107396c8ef3aSMaxim Konovalov ifc_free_unit(ifc, unit); 1074f889d2efSBrooks Davis free(ifv, M_VLAN); 1075f889d2efSBrooks Davis 1076f889d2efSBrooks Davis return (error); 1077f889d2efSBrooks Davis } 1078f889d2efSBrooks Davis } 1079f889d2efSBrooks Davis 10809d4fe4b2SBrooks Davis return (0); 10819d4fe4b2SBrooks Davis } 10829d4fe4b2SBrooks Davis 1083f889d2efSBrooks Davis static int 1084f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 10859d4fe4b2SBrooks Davis { 10869d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 1087114c608cSYaroslav Tykhiy int unit = ifp->if_dunit; 1088b4e9f837SBrooks Davis 1089249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 1090249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 1091d148c2a2SMatt Joras /* 1092d148c2a2SMatt Joras * We should have the only reference to the ifv now, so we can now 1093d148c2a2SMatt Joras * drain any remaining lladdr task before freeing the ifnet and the 1094d148c2a2SMatt Joras * ifvlan. 1095d148c2a2SMatt Joras */ 1096d148c2a2SMatt Joras taskqueue_drain(taskqueue_thread, &ifv->lladdr_task); 1097b08d611dSMatt Macy NET_EPOCH_WAIT(); 10984b22573aSBrooks Davis if_free(ifp); 10999d4fe4b2SBrooks Davis free(ifv, M_VLAN); 1100b4e9f837SBrooks Davis ifc_free_unit(ifc, unit); 1101b4e9f837SBrooks Davis 1102f889d2efSBrooks Davis return (0); 11039d4fe4b2SBrooks Davis } 11049d4fe4b2SBrooks Davis 110515a66c21SBruce M Simpson /* 110615a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 110715a66c21SBruce M Simpson */ 11082cc2df49SGarrett Wollman static void 1109114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 11102cc2df49SGarrett Wollman { 11112cc2df49SGarrett Wollman } 11122cc2df49SGarrett Wollman 11136d3a3ab7SGleb Smirnoff /* 1114d9b1d615SJohn Baldwin * The if_transmit method for vlan(4) interface. 11156d3a3ab7SGleb Smirnoff */ 1116d9b1d615SJohn Baldwin static int 1117d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m) 11182cc2df49SGarrett Wollman { 1119a68cc388SGleb Smirnoff struct epoch_tracker et; 11202cc2df49SGarrett Wollman struct ifvlan *ifv; 11212cc2df49SGarrett Wollman struct ifnet *p; 11221ad7a257SPyun YongHyeon int error, len, mcast; 11232cc2df49SGarrett Wollman 1124a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 11252cc2df49SGarrett Wollman ifv = ifp->if_softc; 1126d148c2a2SMatt Joras if (TRUNK(ifv) == NULL) { 1127d148c2a2SMatt Joras if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1128a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1129d148c2a2SMatt Joras m_freem(m); 1130d148c2a2SMatt Joras return (ENETDOWN); 1131d148c2a2SMatt Joras } 113275ee267cSGleb Smirnoff p = PARENT(ifv); 11331ad7a257SPyun YongHyeon len = m->m_pkthdr.len; 11341ad7a257SPyun YongHyeon mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 11352cc2df49SGarrett Wollman 1136a3814acfSSam Leffler BPF_MTAP(ifp, m); 11372cc2df49SGarrett Wollman 1138f731f104SBill Paul /* 1139d9b1d615SJohn Baldwin * Do not run parent's if_transmit() if the parent is not up, 114024993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 114124993214SYaroslav Tykhiy */ 11422dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 1143a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1144a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1145d148c2a2SMatt Joras m_freem(m); 11468b20f6cfSHiroki Sato return (ENETDOWN); 114724993214SYaroslav Tykhiy } 114824993214SYaroslav Tykhiy 1149f1379734SKonstantin Belousov if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) { 1150a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1151a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1152d9b1d615SJohn Baldwin return (0); 11534af90a4dSMatthew N. Dodd } 11542cc2df49SGarrett Wollman 11552cc2df49SGarrett Wollman /* 11562cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 11572cc2df49SGarrett Wollman */ 1158aea78d20SKip Macy error = (p->if_transmit)(p, m); 1159299153b5SAlexander V. Chernikov if (error == 0) { 1160a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1161a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 1162a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast); 11631ad7a257SPyun YongHyeon } else 1164a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1165a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1166d9b1d615SJohn Baldwin return (error); 11672cc2df49SGarrett Wollman } 1168d9b1d615SJohn Baldwin 1169d9b1d615SJohn Baldwin /* 1170d9b1d615SJohn Baldwin * The ifp->if_qflush entry point for vlan(4) is a no-op. 1171d9b1d615SJohn Baldwin */ 1172d9b1d615SJohn Baldwin static void 1173d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused) 1174d9b1d615SJohn Baldwin { 1175f731f104SBill Paul } 1176f731f104SBill Paul 1177a3814acfSSam Leffler static void 1178a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 1179f731f104SBill Paul { 1180a68cc388SGleb Smirnoff struct epoch_tracker et; 1181d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1182f731f104SBill Paul struct ifvlan *ifv; 11832ccbbd06SMarcelo Araujo struct m_tag *mtag; 11842ccbbd06SMarcelo Araujo uint16_t vid, tag; 118575ee267cSGleb Smirnoff 1186a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 1187d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1188d148c2a2SMatt Joras if (trunk == NULL) { 1189a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1190d148c2a2SMatt Joras m_freem(m); 1191d148c2a2SMatt Joras return; 1192d148c2a2SMatt Joras } 1193a3814acfSSam Leffler 1194f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 1195a3814acfSSam Leffler /* 119614e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 1197a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 1198a3814acfSSam Leffler */ 11992ccbbd06SMarcelo Araujo tag = m->m_pkthdr.ether_vtag; 12006ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 1201a3814acfSSam Leffler } else { 120275ee267cSGleb Smirnoff struct ether_vlan_header *evl; 120375ee267cSGleb Smirnoff 120414e98256SYaroslav Tykhiy /* 120514e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 120614e98256SYaroslav Tykhiy */ 1207a3814acfSSam Leffler switch (ifp->if_type) { 1208a3814acfSSam Leffler case IFT_ETHER: 1209a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 1210a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 1211a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 1212a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1213a3814acfSSam Leffler return; 1214a3814acfSSam Leffler } 1215a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 12162ccbbd06SMarcelo Araujo tag = ntohs(evl->evl_tag); 1217db8b5973SYaroslav Tykhiy 1218db8b5973SYaroslav Tykhiy /* 12192dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 12202dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 12212dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 12222dc879b3SYaroslav Tykhiy * type field is already in place. 1223db8b5973SYaroslav Tykhiy */ 12242dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 12252dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 12262dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 1227a3814acfSSam Leffler break; 12282dc879b3SYaroslav Tykhiy 1229a3814acfSSam Leffler default: 1230db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 123160c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 123260c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 1233a3814acfSSam Leffler #endif 12343751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1235a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1236d148c2a2SMatt Joras m_freem(m); 123760c60618SYaroslav Tykhiy return; 1238a3814acfSSam Leffler } 12397a46ec8fSBrooks Davis } 12407a46ec8fSBrooks Davis 12412ccbbd06SMarcelo Araujo vid = EVL_VLANOFTAG(tag); 12422ccbbd06SMarcelo Araujo 12437983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 12442dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 1245a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1246b08d611dSMatt Macy if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1247d148c2a2SMatt Joras m_freem(m); 124875ee267cSGleb Smirnoff return; 124975ee267cSGleb Smirnoff } 1250f731f104SBill Paul 12512ccbbd06SMarcelo Araujo if (vlan_mtag_pcp) { 12522ccbbd06SMarcelo Araujo /* 12532ccbbd06SMarcelo Araujo * While uncommon, it is possible that we will find a 802.1q 12542ccbbd06SMarcelo Araujo * packet encapsulated inside another packet that also had an 12552ccbbd06SMarcelo Araujo * 802.1q header. For example, ethernet tunneled over IPSEC 12562ccbbd06SMarcelo Araujo * arriving over ethernet. In that case, we replace the 12572ccbbd06SMarcelo Araujo * existing 802.1q PCP m_tag value. 12582ccbbd06SMarcelo Araujo */ 12592ccbbd06SMarcelo Araujo mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 12602ccbbd06SMarcelo Araujo if (mtag == NULL) { 12612ccbbd06SMarcelo Araujo mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN, 12622ccbbd06SMarcelo Araujo sizeof(uint8_t), M_NOWAIT); 12632ccbbd06SMarcelo Araujo if (mtag == NULL) { 12642ccbbd06SMarcelo Araujo if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1265a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1266d148c2a2SMatt Joras m_freem(m); 12672ccbbd06SMarcelo Araujo return; 12682ccbbd06SMarcelo Araujo } 12692ccbbd06SMarcelo Araujo m_tag_prepend(m, mtag); 12702ccbbd06SMarcelo Araujo } 12712ccbbd06SMarcelo Araujo *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag); 12722ccbbd06SMarcelo Araujo } 12732ccbbd06SMarcelo Araujo 1274fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 1275c0304424SGleb Smirnoff if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1); 1276a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 12772cc2df49SGarrett Wollman 1278a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 1279fdbf1174SMatt Joras (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m); 12802cc2df49SGarrett Wollman } 12812cc2df49SGarrett Wollman 1282d148c2a2SMatt Joras static void 1283d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused) 1284d148c2a2SMatt Joras { 1285d148c2a2SMatt Joras struct ifvlan *ifv; 1286d148c2a2SMatt Joras struct ifnet *ifp; 1287d148c2a2SMatt Joras 1288d148c2a2SMatt Joras ifv = (struct ifvlan *)arg; 1289d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 12905191a3aeSKristof Provost 12915191a3aeSKristof Provost CURVNET_SET(ifp->if_vnet); 12925191a3aeSKristof Provost 1293d148c2a2SMatt Joras /* The ifv_ifp already has the lladdr copied in. */ 1294d148c2a2SMatt Joras if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen); 12955191a3aeSKristof Provost 12965191a3aeSKristof Provost CURVNET_RESTORE(); 1297d148c2a2SMatt Joras } 1298d148c2a2SMatt Joras 12992cc2df49SGarrett Wollman static int 13007983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid) 13012cc2df49SGarrett Wollman { 1302a68cc388SGleb Smirnoff struct epoch_tracker et; 130375ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 13041cf236fbSYaroslav Tykhiy struct ifnet *ifp; 130575ee267cSGleb Smirnoff int error = 0; 13062cc2df49SGarrett Wollman 1307b1828acfSGleb Smirnoff /* 1308b1828acfSGleb Smirnoff * We can handle non-ethernet hardware types as long as 1309b1828acfSGleb Smirnoff * they handle the tagging and headers themselves. 1310b1828acfSGleb Smirnoff */ 1311e4cd31ddSJeff Roberson if (p->if_type != IFT_ETHER && 1312e4cd31ddSJeff Roberson (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 131315a66c21SBruce M Simpson return (EPROTONOSUPPORT); 131464a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 131564a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 1316b1828acfSGleb Smirnoff /* 1317b1828acfSGleb Smirnoff * Don't let the caller set up a VLAN VID with 1318b1828acfSGleb Smirnoff * anything except VLID bits. 1319b1828acfSGleb Smirnoff * VID numbers 0x0 and 0xFFF are reserved. 1320b1828acfSGleb Smirnoff */ 1321b1828acfSGleb Smirnoff if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK)) 1322b1828acfSGleb Smirnoff return (EINVAL); 132375ee267cSGleb Smirnoff if (ifv->ifv_trunk) 132415a66c21SBruce M Simpson return (EBUSY); 13252cc2df49SGarrett Wollman 1326d148c2a2SMatt Joras VLAN_XLOCK(); 132775ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 132875ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 132975ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 133075ee267cSGleb Smirnoff vlan_inithash(trunk); 133175ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 1332d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 133375ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 133475ee267cSGleb Smirnoff trunk->parent = p; 13359bcf3ae4SAlexander Motin if_ref(trunk->parent); 1336b08d611dSMatt Macy TRUNK_WUNLOCK(trunk); 133775ee267cSGleb Smirnoff } else { 133875ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 133975ee267cSGleb Smirnoff } 134075ee267cSGleb Smirnoff 13417983103aSRobert Watson ifv->ifv_vid = vid; /* must set this before vlan_inshash() */ 13422ccbbd06SMarcelo Araujo ifv->ifv_pcp = 0; /* Default: best effort delivery. */ 13432ccbbd06SMarcelo Araujo vlan_tag_recalculate(ifv); 134475ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 134575ee267cSGleb Smirnoff if (error) 134675ee267cSGleb Smirnoff goto done; 134773f2233dSYaroslav Tykhiy ifv->ifv_proto = ETHERTYPE_VLAN; 1348a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1349a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 13501cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1351d89baa5aSAlexander Motin ifv->ifv_capenable = -1; 1352a3814acfSSam Leffler 1353a3814acfSSam Leffler /* 1354a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1355a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1356656acce4SYaroslav Tykhiy * use it. 1357a3814acfSSam Leffler */ 1358656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1359656acce4SYaroslav Tykhiy /* 1360656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1361656acce4SYaroslav Tykhiy * handle extended frames. 1362656acce4SYaroslav Tykhiy */ 1363a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1364656acce4SYaroslav Tykhiy } else { 1365a3814acfSSam Leffler /* 1366a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1367a3814acfSSam Leffler * makes us incompatible with strictly compliant 1368a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1369a3814acfSSam Leffler * the feature with other NetBSD implementations, 1370a3814acfSSam Leffler * which might still be useful. 1371a3814acfSSam Leffler */ 1372a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1373a3814acfSSam Leffler } 1374a3814acfSSam Leffler 137575ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 13761cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 1377e4cd31ddSJeff Roberson /* 1378e4cd31ddSJeff Roberson * Initialize fields from our parent. This duplicates some 1379e4cd31ddSJeff Roberson * work with ether_ifattach() but allows for non-ethernet 1380e4cd31ddSJeff Roberson * interfaces to also work. 1381e4cd31ddSJeff Roberson */ 13821cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 138375ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 1384e4cd31ddSJeff Roberson ifp->if_output = p->if_output; 1385e4cd31ddSJeff Roberson ifp->if_input = p->if_input; 1386e4cd31ddSJeff Roberson ifp->if_resolvemulti = p->if_resolvemulti; 1387e4cd31ddSJeff Roberson ifp->if_addrlen = p->if_addrlen; 1388e4cd31ddSJeff Roberson ifp->if_broadcastaddr = p->if_broadcastaddr; 138932a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 1390e4cd31ddSJeff Roberson 13912cc2df49SGarrett Wollman /* 139224993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 139324993214SYaroslav Tykhiy * Other flags are none of our business. 13942cc2df49SGarrett Wollman */ 139564a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 13961cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 13971cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 13981cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 13991cf236fbSYaroslav Tykhiy 14001cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 14012cc2df49SGarrett Wollman 1402a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 140375ee267cSGleb Smirnoff vlan_capabilities(ifv); 1404a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1405a3814acfSSam Leffler 1406a3814acfSSam Leffler /* 1407e4cd31ddSJeff Roberson * Set up our interface address to reflect the underlying 14082cc2df49SGarrett Wollman * physical interface's. 14092cc2df49SGarrett Wollman */ 1410e4cd31ddSJeff Roberson bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1411e4cd31ddSJeff Roberson ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1412e4cd31ddSJeff Roberson p->if_addrlen; 14131b2a4f7aSBill Fenner 1414d148c2a2SMatt Joras TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv); 14152ada9747SYaroslav Tykhiy 14162ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 14172ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 1418d148c2a2SMatt Joras 1419d148c2a2SMatt Joras /* Update flags on the parent, if necessary. */ 1420d148c2a2SMatt Joras vlan_setflags(ifp, 1); 1421b08d611dSMatt Macy 1422d148c2a2SMatt Joras /* 1423b08d611dSMatt Macy * Configure multicast addresses that may already be 1424b08d611dSMatt Macy * joined on the vlan device. 1425d148c2a2SMatt Joras */ 1426b08d611dSMatt Macy (void)vlan_setmulti(ifp); 1427b08d611dSMatt Macy 1428b08d611dSMatt Macy done: 1429c725524cSJack F Vogel if (error == 0) 14307983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid); 1431d148c2a2SMatt Joras VLAN_XUNLOCK(); 143275ee267cSGleb Smirnoff 143375ee267cSGleb Smirnoff return (error); 14342cc2df49SGarrett Wollman } 14352cc2df49SGarrett Wollman 14366f359e28SJohn Baldwin static void 1437f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1438f731f104SBill Paul { 14395cb8c31aSYaroslav Tykhiy 1440d148c2a2SMatt Joras VLAN_XLOCK(); 144128cc4d37SJohn Baldwin vlan_unconfig_locked(ifp, 0); 1442d148c2a2SMatt Joras VLAN_XUNLOCK(); 14435cb8c31aSYaroslav Tykhiy } 14445cb8c31aSYaroslav Tykhiy 14456f359e28SJohn Baldwin static void 144628cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing) 14475cb8c31aSYaroslav Tykhiy { 144875ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1449f731f104SBill Paul struct vlan_mc_entry *mc; 1450f731f104SBill Paul struct ifvlan *ifv; 1451c725524cSJack F Vogel struct ifnet *parent; 145228cc4d37SJohn Baldwin int error; 1453f731f104SBill Paul 1454d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 14554faedfe8SSam Leffler 1456f731f104SBill Paul ifv = ifp->if_softc; 145775ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 145822893351SJack F Vogel parent = NULL; 1459f731f104SBill Paul 146022893351SJack F Vogel if (trunk != NULL) { 146122893351SJack F Vogel parent = trunk->parent; 14621b2a4f7aSBill Fenner 1463f731f104SBill Paul /* 1464f731f104SBill Paul * Since the interface is being unconfigured, we need to 1465f731f104SBill Paul * empty the list of multicast groups that we may have joined 14661b2a4f7aSBill Fenner * while we were alive from the parent's list. 1467f731f104SBill Paul */ 1468b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 14696f359e28SJohn Baldwin /* 147028cc4d37SJohn Baldwin * If the parent interface is being detached, 1471b90dde2fSJohn Baldwin * all its multicast addresses have already 147228cc4d37SJohn Baldwin * been removed. Warn about errors if 147328cc4d37SJohn Baldwin * if_delmulti() does fail, but don't abort as 147428cc4d37SJohn Baldwin * all callers expect vlan destruction to 147528cc4d37SJohn Baldwin * succeed. 14766f359e28SJohn Baldwin */ 147728cc4d37SJohn Baldwin if (!departing) { 147828cc4d37SJohn Baldwin error = if_delmulti(parent, 1479e4cd31ddSJeff Roberson (struct sockaddr *)&mc->mc_addr); 148028cc4d37SJohn Baldwin if (error) 148128cc4d37SJohn Baldwin if_printf(ifp, 148228cc4d37SJohn Baldwin "Failed to delete multicast address from parent: %d\n", 148328cc4d37SJohn Baldwin error); 148428cc4d37SJohn Baldwin } 1485b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 1486c32a9d66SHans Petter Selasky epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free); 1487f731f104SBill Paul } 1488a3814acfSSam Leffler 14891cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 1490d148c2a2SMatt Joras 149175ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 149275ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 149375ee267cSGleb Smirnoff 149475ee267cSGleb Smirnoff /* 149575ee267cSGleb Smirnoff * Check if we were the last. 149675ee267cSGleb Smirnoff */ 149775ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 14982d222cb7SAlexander Motin parent->if_vlantrunk = NULL; 1499b08d611dSMatt Macy NET_EPOCH_WAIT(); 150075ee267cSGleb Smirnoff trunk_destroy(trunk); 1501d148c2a2SMatt Joras } 15021b2a4f7aSBill Fenner } 1503f731f104SBill Paul 1504f731f104SBill Paul /* Disconnect from parent. */ 15051cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 15061cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 15075cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 15085cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 15095cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1510f731f104SBill Paul 151122893351SJack F Vogel /* 151222893351SJack F Vogel * Only dispatch an event if vlan was 151322893351SJack F Vogel * attached, otherwise there is nothing 151422893351SJack F Vogel * to cleanup anyway. 151522893351SJack F Vogel */ 151622893351SJack F Vogel if (parent != NULL) 15177983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid); 1518f731f104SBill Paul } 1519f731f104SBill Paul 15201cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1521f731f104SBill Paul static int 15221cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 15231cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1524a3814acfSSam Leffler { 15251cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 15261cf236fbSYaroslav Tykhiy int error; 1527a3814acfSSam Leffler 1528d148c2a2SMatt Joras VLAN_SXLOCK_ASSERT(); 1529a3814acfSSam Leffler 15301cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 15311cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 15321cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 15331cf236fbSYaroslav Tykhiy 15341cf236fbSYaroslav Tykhiy /* 15351cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 15361cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 15371cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 15381cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 15391cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 15401cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 15411cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 15421cf236fbSYaroslav Tykhiy */ 15431cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 154475ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 15451cf236fbSYaroslav Tykhiy if (error) 1546a3814acfSSam Leffler return (error); 15471cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 15481cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 15491cf236fbSYaroslav Tykhiy } 15501cf236fbSYaroslav Tykhiy return (0); 15511cf236fbSYaroslav Tykhiy } 15521cf236fbSYaroslav Tykhiy 15531cf236fbSYaroslav Tykhiy /* 15541cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 15551cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 15561cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 15571cf236fbSYaroslav Tykhiy */ 15581cf236fbSYaroslav Tykhiy static int 15591cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 15601cf236fbSYaroslav Tykhiy { 15611cf236fbSYaroslav Tykhiy int error, i; 15621cf236fbSYaroslav Tykhiy 15631cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 15641cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 15651cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 15661cf236fbSYaroslav Tykhiy if (error) 15671cf236fbSYaroslav Tykhiy return (error); 15681cf236fbSYaroslav Tykhiy } 15691cf236fbSYaroslav Tykhiy return (0); 1570a3814acfSSam Leffler } 1571a3814acfSSam Leffler 1572127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1573127d7b2dSAndre Oppermann static void 1574a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp) 1575127d7b2dSAndre Oppermann { 1576a68cc388SGleb Smirnoff struct epoch_tracker et; 1577d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1578127d7b2dSAndre Oppermann struct ifvlan *ifv; 1579127d7b2dSAndre Oppermann 1580d148c2a2SMatt Joras /* Called from a taskqueue_swi task, so we cannot sleep. */ 1581a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 1582d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1583d148c2a2SMatt Joras if (trunk == NULL) { 1584a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1585d148c2a2SMatt Joras return; 1586d148c2a2SMatt Joras } 1587d148c2a2SMatt Joras 1588d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 1589d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 1590aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1591fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 159275ee267cSGleb Smirnoff trunk->parent->if_link_state); 1593127d7b2dSAndre Oppermann } 1594d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 1595a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 159675ee267cSGleb Smirnoff } 159775ee267cSGleb Smirnoff 159875ee267cSGleb Smirnoff static void 159975ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 160075ee267cSGleb Smirnoff { 1601d148c2a2SMatt Joras struct ifnet *p; 1602d148c2a2SMatt Joras struct ifnet *ifp; 16039fd573c3SHans Petter Selasky struct ifnet_hw_tsomax hw_tsomax; 1604d89baa5aSAlexander Motin int cap = 0, ena = 0, mena; 1605d89baa5aSAlexander Motin u_long hwa = 0; 160675ee267cSGleb Smirnoff 1607d148c2a2SMatt Joras VLAN_SXLOCK_ASSERT(); 1608a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 1609d148c2a2SMatt Joras p = PARENT(ifv); 1610d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 161175ee267cSGleb Smirnoff 1612d89baa5aSAlexander Motin /* Mask parent interface enabled capabilities disabled by user. */ 1613d89baa5aSAlexander Motin mena = p->if_capenable & ifv->ifv_capenable; 1614d89baa5aSAlexander Motin 161575ee267cSGleb Smirnoff /* 161675ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 161775ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 161875ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 161975ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 162075ee267cSGleb Smirnoff */ 162175ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1622d89baa5aSAlexander Motin cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 162375ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 162475ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1625d89baa5aSAlexander Motin ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 1626d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM) 1627d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP | 1628d89baa5aSAlexander Motin CSUM_UDP | CSUM_SCTP); 1629d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM_IPV6) 1630d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_TCP_IPV6 | 1631d89baa5aSAlexander Motin CSUM_UDP_IPV6 | CSUM_SCTP_IPV6); 163275ee267cSGleb Smirnoff } 1633d89baa5aSAlexander Motin 16349b76d9cbSPyun YongHyeon /* 16359b76d9cbSPyun YongHyeon * If the parent interface can do TSO on VLANs then 16369b76d9cbSPyun YongHyeon * propagate the hardware-assisted flag. TSO on VLANs 16379b76d9cbSPyun YongHyeon * does not necessarily require hardware VLAN tagging. 16389b76d9cbSPyun YongHyeon */ 16399fd573c3SHans Petter Selasky memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 16409fd573c3SHans Petter Selasky if_hw_tsomax_common(p, &hw_tsomax); 16419fd573c3SHans Petter Selasky if_hw_tsomax_update(ifp, &hw_tsomax); 16429b76d9cbSPyun YongHyeon if (p->if_capabilities & IFCAP_VLAN_HWTSO) 1643d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TSO; 16449b76d9cbSPyun YongHyeon if (p->if_capenable & IFCAP_VLAN_HWTSO) { 1645d89baa5aSAlexander Motin ena |= mena & IFCAP_TSO; 1646d89baa5aSAlexander Motin if (ena & IFCAP_TSO) 1647d89baa5aSAlexander Motin hwa |= p->if_hwassist & CSUM_TSO; 16489b76d9cbSPyun YongHyeon } 164909fe6320SNavdeep Parhar 165009fe6320SNavdeep Parhar /* 165159150e91SAlexander Motin * If the parent interface can do LRO and checksum offloading on 165259150e91SAlexander Motin * VLANs, then guess it may do LRO on VLANs. False positive here 165359150e91SAlexander Motin * cost nothing, while false negative may lead to some confusions. 165459150e91SAlexander Motin */ 165559150e91SAlexander Motin if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 165659150e91SAlexander Motin cap |= p->if_capabilities & IFCAP_LRO; 165759150e91SAlexander Motin if (p->if_capenable & IFCAP_VLAN_HWCSUM) 165859150e91SAlexander Motin ena |= p->if_capenable & IFCAP_LRO; 165959150e91SAlexander Motin 166059150e91SAlexander Motin /* 166109fe6320SNavdeep Parhar * If the parent interface can offload TCP connections over VLANs then 166209fe6320SNavdeep Parhar * propagate its TOE capability to the VLAN interface. 166309fe6320SNavdeep Parhar * 166409fe6320SNavdeep Parhar * All TOE drivers in the tree today can deal with VLANs. If this 166509fe6320SNavdeep Parhar * changes then IFCAP_VLAN_TOE should be promoted to a full capability 166609fe6320SNavdeep Parhar * with its own bit. 166709fe6320SNavdeep Parhar */ 166809fe6320SNavdeep Parhar #define IFCAP_VLAN_TOE IFCAP_TOE 166909fe6320SNavdeep Parhar if (p->if_capabilities & IFCAP_VLAN_TOE) 1670d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TOE; 167109fe6320SNavdeep Parhar if (p->if_capenable & IFCAP_VLAN_TOE) { 167209fe6320SNavdeep Parhar TOEDEV(ifp) = TOEDEV(p); 1673d89baa5aSAlexander Motin ena |= mena & IFCAP_TOE; 167409fe6320SNavdeep Parhar } 1675f3e7afe2SHans Petter Selasky 1676d89baa5aSAlexander Motin /* 1677d89baa5aSAlexander Motin * If the parent interface supports dynamic link state, so does the 1678d89baa5aSAlexander Motin * VLAN interface. 1679d89baa5aSAlexander Motin */ 1680d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_LINKSTATE); 1681d89baa5aSAlexander Motin ena |= (mena & IFCAP_LINKSTATE); 1682d89baa5aSAlexander Motin 1683f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1684f3e7afe2SHans Petter Selasky /* 1685f3e7afe2SHans Petter Selasky * If the parent interface supports ratelimiting, so does the 1686f3e7afe2SHans Petter Selasky * VLAN interface. 1687f3e7afe2SHans Petter Selasky */ 1688d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_TXRTLMT); 1689d89baa5aSAlexander Motin ena |= (mena & IFCAP_TXRTLMT); 1690f3e7afe2SHans Petter Selasky #endif 1691d89baa5aSAlexander Motin 1692d89baa5aSAlexander Motin ifp->if_capabilities = cap; 1693d89baa5aSAlexander Motin ifp->if_capenable = ena; 1694d89baa5aSAlexander Motin ifp->if_hwassist = hwa; 169575ee267cSGleb Smirnoff } 169675ee267cSGleb Smirnoff 169775ee267cSGleb Smirnoff static void 169875ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 169975ee267cSGleb Smirnoff { 1700a68cc388SGleb Smirnoff struct epoch_tracker et; 1701d148c2a2SMatt Joras struct ifvlantrunk *trunk; 170275ee267cSGleb Smirnoff struct ifvlan *ifv; 170375ee267cSGleb Smirnoff 1704d148c2a2SMatt Joras VLAN_SLOCK(); 1705d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1706d148c2a2SMatt Joras if (trunk == NULL) { 1707d148c2a2SMatt Joras VLAN_SUNLOCK(); 1708d148c2a2SMatt Joras return; 1709d148c2a2SMatt Joras } 1710a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 1711d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 171275ee267cSGleb Smirnoff vlan_capabilities(ifv); 171375ee267cSGleb Smirnoff } 1714a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1715d148c2a2SMatt Joras VLAN_SUNLOCK(); 1716127d7b2dSAndre Oppermann } 1717127d7b2dSAndre Oppermann 1718a3814acfSSam Leffler static int 1719cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 17202cc2df49SGarrett Wollman { 17212cc2df49SGarrett Wollman struct ifnet *p; 17222cc2df49SGarrett Wollman struct ifreq *ifr; 1723e4cd31ddSJeff Roberson struct ifaddr *ifa; 17242cc2df49SGarrett Wollman struct ifvlan *ifv; 17252d222cb7SAlexander Motin struct ifvlantrunk *trunk; 17262cc2df49SGarrett Wollman struct vlanreq vlr; 17272cc2df49SGarrett Wollman int error = 0; 17282cc2df49SGarrett Wollman 17292cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 1730e4cd31ddSJeff Roberson ifa = (struct ifaddr *) data; 17312cc2df49SGarrett Wollman ifv = ifp->if_softc; 17322cc2df49SGarrett Wollman 17332cc2df49SGarrett Wollman switch (cmd) { 1734e4cd31ddSJeff Roberson case SIOCSIFADDR: 1735e4cd31ddSJeff Roberson ifp->if_flags |= IFF_UP; 1736e4cd31ddSJeff Roberson #ifdef INET 1737e4cd31ddSJeff Roberson if (ifa->ifa_addr->sa_family == AF_INET) 1738e4cd31ddSJeff Roberson arp_ifinit(ifp, ifa); 1739e4cd31ddSJeff Roberson #endif 1740e4cd31ddSJeff Roberson break; 1741e4cd31ddSJeff Roberson case SIOCGIFADDR: 174238d958a6SBrooks Davis bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 174338d958a6SBrooks Davis ifp->if_addrlen); 1744e4cd31ddSJeff Roberson break; 1745b3cca108SBill Fenner case SIOCGIFMEDIA: 1746d148c2a2SMatt Joras VLAN_SLOCK(); 174775ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 1748d8564efdSEd Maste p = PARENT(ifv); 17499bcf3ae4SAlexander Motin if_ref(p); 1750d8564efdSEd Maste error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 17519bcf3ae4SAlexander Motin if_rele(p); 1752b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 1753b3cca108SBill Fenner if (error == 0) { 1754b3cca108SBill Fenner struct ifmediareq *ifmr; 1755b3cca108SBill Fenner 1756b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 1757b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1758b3cca108SBill Fenner ifmr->ifm_count = 1; 1759b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 1760b3cca108SBill Fenner ifmr->ifm_ulist, 1761b3cca108SBill Fenner sizeof(int)); 1762b3cca108SBill Fenner } 1763b3cca108SBill Fenner } 17644faedfe8SSam Leffler } else { 1765b3cca108SBill Fenner error = EINVAL; 17664faedfe8SSam Leffler } 1767d148c2a2SMatt Joras VLAN_SUNLOCK(); 1768b3cca108SBill Fenner break; 1769b3cca108SBill Fenner 1770b3cca108SBill Fenner case SIOCSIFMEDIA: 1771b3cca108SBill Fenner error = EINVAL; 1772b3cca108SBill Fenner break; 1773b3cca108SBill Fenner 17742cc2df49SGarrett Wollman case SIOCSIFMTU: 17752cc2df49SGarrett Wollman /* 17762cc2df49SGarrett Wollman * Set the interface MTU. 17772cc2df49SGarrett Wollman */ 1778d148c2a2SMatt Joras VLAN_SLOCK(); 1779d148c2a2SMatt Joras trunk = TRUNK(ifv); 1780d148c2a2SMatt Joras if (trunk != NULL) { 1781d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 1782a3814acfSSam Leffler if (ifr->ifr_mtu > 178375ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1784a3814acfSSam Leffler ifr->ifr_mtu < 1785a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 17862cc2df49SGarrett Wollman error = EINVAL; 1787a3814acfSSam Leffler else 17882cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 1789d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 1790a3814acfSSam Leffler } else 1791a3814acfSSam Leffler error = EINVAL; 1792d148c2a2SMatt Joras VLAN_SUNLOCK(); 17932cc2df49SGarrett Wollman break; 17942cc2df49SGarrett Wollman 17952cc2df49SGarrett Wollman case SIOCSETVLAN: 1796ccf7ba97SMarko Zec #ifdef VIMAGE 179715f6780eSRobert Watson /* 179815f6780eSRobert Watson * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN 179915f6780eSRobert Watson * interface to be delegated to a jail without allowing the 180015f6780eSRobert Watson * jail to change what underlying interface/VID it is 180115f6780eSRobert Watson * associated with. We are not entirely convinced that this 18025a39f779SRobert Watson * is the right way to accomplish that policy goal. 180315f6780eSRobert Watson */ 1804ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 1805ccf7ba97SMarko Zec error = EPERM; 1806ccf7ba97SMarko Zec break; 1807ccf7ba97SMarko Zec } 1808ccf7ba97SMarko Zec #endif 1809541d96aaSBrooks Davis error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr)); 18102cc2df49SGarrett Wollman if (error) 18112cc2df49SGarrett Wollman break; 18122cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 1813f731f104SBill Paul vlan_unconfig(ifp); 18142cc2df49SGarrett Wollman break; 18152cc2df49SGarrett Wollman } 18169bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 18171bdc73d3SEd Maste if (p == NULL) { 18182cc2df49SGarrett Wollman error = ENOENT; 18192cc2df49SGarrett Wollman break; 18202cc2df49SGarrett Wollman } 182175ee267cSGleb Smirnoff error = vlan_config(ifv, p, vlr.vlr_tag); 18229bcf3ae4SAlexander Motin if_rele(p); 18232cc2df49SGarrett Wollman break; 18242cc2df49SGarrett Wollman 18252cc2df49SGarrett Wollman case SIOCGETVLAN: 1826ccf7ba97SMarko Zec #ifdef VIMAGE 1827ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 1828ccf7ba97SMarko Zec error = EPERM; 1829ccf7ba97SMarko Zec break; 1830ccf7ba97SMarko Zec } 1831ccf7ba97SMarko Zec #endif 183215a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 1833d148c2a2SMatt Joras VLAN_SLOCK(); 183475ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 183575ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 18369bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 18377983103aSRobert Watson vlr.vlr_tag = ifv->ifv_vid; 18382cc2df49SGarrett Wollman } 1839d148c2a2SMatt Joras VLAN_SUNLOCK(); 1840541d96aaSBrooks Davis error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr)); 18412cc2df49SGarrett Wollman break; 18422cc2df49SGarrett Wollman 18432cc2df49SGarrett Wollman case SIOCSIFFLAGS: 18442cc2df49SGarrett Wollman /* 18451cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 18461cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 18472cc2df49SGarrett Wollman */ 1848d148c2a2SMatt Joras VLAN_XLOCK(); 184975ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 18501cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 1851d148c2a2SMatt Joras VLAN_XUNLOCK(); 18522cc2df49SGarrett Wollman break; 1853a3814acfSSam Leffler 1854f731f104SBill Paul case SIOCADDMULTI: 1855f731f104SBill Paul case SIOCDELMULTI: 185675ee267cSGleb Smirnoff /* 185775ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 185875ee267cSGleb Smirnoff * when we do. 1859d148c2a2SMatt Joras * 1860d148c2a2SMatt Joras * XXX We need the rmlock here to avoid sleeping while 1861d148c2a2SMatt Joras * holding in6_multi_mtx. 186275ee267cSGleb Smirnoff */ 1863b08d611dSMatt Macy VLAN_XLOCK(); 18642d222cb7SAlexander Motin trunk = TRUNK(ifv); 1865b08d611dSMatt Macy if (trunk != NULL) 1866f731f104SBill Paul error = vlan_setmulti(ifp); 1867b08d611dSMatt Macy VLAN_XUNLOCK(); 186875ee267cSGleb Smirnoff 1869b08d611dSMatt Macy break; 18702ccbbd06SMarcelo Araujo case SIOCGVLANPCP: 18712ccbbd06SMarcelo Araujo #ifdef VIMAGE 18722ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 18732ccbbd06SMarcelo Araujo error = EPERM; 18742ccbbd06SMarcelo Araujo break; 18752ccbbd06SMarcelo Araujo } 18762ccbbd06SMarcelo Araujo #endif 18772ccbbd06SMarcelo Araujo ifr->ifr_vlan_pcp = ifv->ifv_pcp; 18782ccbbd06SMarcelo Araujo break; 18792ccbbd06SMarcelo Araujo 18802ccbbd06SMarcelo Araujo case SIOCSVLANPCP: 18812ccbbd06SMarcelo Araujo #ifdef VIMAGE 18822ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 18832ccbbd06SMarcelo Araujo error = EPERM; 18842ccbbd06SMarcelo Araujo break; 18852ccbbd06SMarcelo Araujo } 18862ccbbd06SMarcelo Araujo #endif 18872ccbbd06SMarcelo Araujo error = priv_check(curthread, PRIV_NET_SETVLANPCP); 18882ccbbd06SMarcelo Araujo if (error) 18892ccbbd06SMarcelo Araujo break; 18902ccbbd06SMarcelo Araujo if (ifr->ifr_vlan_pcp > 7) { 18912ccbbd06SMarcelo Araujo error = EINVAL; 18922ccbbd06SMarcelo Araujo break; 18932ccbbd06SMarcelo Araujo } 18942ccbbd06SMarcelo Araujo ifv->ifv_pcp = ifr->ifr_vlan_pcp; 189532a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 18962ccbbd06SMarcelo Araujo vlan_tag_recalculate(ifv); 18974a381a9eSHans Petter Selasky /* broadcast event about PCP change */ 18984a381a9eSHans Petter Selasky EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 18992ccbbd06SMarcelo Araujo break; 19002ccbbd06SMarcelo Araujo 1901d89baa5aSAlexander Motin case SIOCSIFCAP: 1902d148c2a2SMatt Joras VLAN_SLOCK(); 1903d89baa5aSAlexander Motin ifv->ifv_capenable = ifr->ifr_reqcap; 1904d89baa5aSAlexander Motin trunk = TRUNK(ifv); 1905d89baa5aSAlexander Motin if (trunk != NULL) { 1906a68cc388SGleb Smirnoff struct epoch_tracker et; 1907a68cc388SGleb Smirnoff 1908a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 1909d89baa5aSAlexander Motin vlan_capabilities(ifv); 1910a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 1911d89baa5aSAlexander Motin } 1912d148c2a2SMatt Joras VLAN_SUNLOCK(); 1913d89baa5aSAlexander Motin break; 1914d89baa5aSAlexander Motin 19152cc2df49SGarrett Wollman default: 1916e4cd31ddSJeff Roberson error = EINVAL; 1917e4cd31ddSJeff Roberson break; 19182cc2df49SGarrett Wollman } 191915a66c21SBruce M Simpson 192015a66c21SBruce M Simpson return (error); 19212cc2df49SGarrett Wollman } 1922f3e7afe2SHans Petter Selasky 1923f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1924f3e7afe2SHans Petter Selasky static int 1925f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp, 1926f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *params, 1927f3e7afe2SHans Petter Selasky struct m_snd_tag **ppmt) 1928f3e7afe2SHans Petter Selasky { 1929f3e7afe2SHans Petter Selasky 1930f3e7afe2SHans Petter Selasky /* get trunk device */ 1931f3e7afe2SHans Petter Selasky ifp = vlan_trunkdev(ifp); 1932f3e7afe2SHans Petter Selasky if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0) 1933f3e7afe2SHans Petter Selasky return (EOPNOTSUPP); 1934f3e7afe2SHans Petter Selasky /* forward allocation request */ 1935f3e7afe2SHans Petter Selasky return (ifp->if_snd_tag_alloc(ifp, params, ppmt)); 1936f3e7afe2SHans Petter Selasky } 1937f3e7afe2SHans Petter Selasky #endif 1938