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" 4984becee1SAlexander Motin #include "opt_inet6.h" 50b2e60773SJohn Baldwin #include "opt_kern_tls.h" 5175ee267cSGleb Smirnoff #include "opt_vlan.h" 52f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 532cc2df49SGarrett Wollman 542cc2df49SGarrett Wollman #include <sys/param.h> 55c3322cb9SGleb Smirnoff #include <sys/eventhandler.h> 562cc2df49SGarrett Wollman #include <sys/kernel.h> 5775ee267cSGleb Smirnoff #include <sys/lock.h> 58f731f104SBill Paul #include <sys/malloc.h> 592cc2df49SGarrett Wollman #include <sys/mbuf.h> 602b120974SPeter Wemm #include <sys/module.h> 61772b000fSAlexander V. Chernikov #include <sys/rmlock.h> 622ccbbd06SMarcelo Araujo #include <sys/priv.h> 63f731f104SBill Paul #include <sys/queue.h> 642cc2df49SGarrett Wollman #include <sys/socket.h> 652cc2df49SGarrett Wollman #include <sys/sockio.h> 662cc2df49SGarrett Wollman #include <sys/sysctl.h> 672cc2df49SGarrett Wollman #include <sys/systm.h> 68e4cd31ddSJeff Roberson #include <sys/sx.h> 69d148c2a2SMatt Joras #include <sys/taskqueue.h> 702cc2df49SGarrett Wollman 712cc2df49SGarrett Wollman #include <net/bpf.h> 722cc2df49SGarrett Wollman #include <net/ethernet.h> 732cc2df49SGarrett Wollman #include <net/if.h> 7476039bc8SGleb Smirnoff #include <net/if_var.h> 75f889d2efSBrooks Davis #include <net/if_clone.h> 762cc2df49SGarrett Wollman #include <net/if_dl.h> 772cc2df49SGarrett Wollman #include <net/if_types.h> 782cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 7984becee1SAlexander Motin #include <net/route.h> 804b79449eSBjoern A. Zeeb #include <net/vnet.h> 812cc2df49SGarrett Wollman 822c5b403eSOleg Bulyzhin #ifdef INET 832c5b403eSOleg Bulyzhin #include <netinet/in.h> 842c5b403eSOleg Bulyzhin #include <netinet/if_ether.h> 852c5b403eSOleg Bulyzhin #endif 862c5b403eSOleg Bulyzhin 8784becee1SAlexander Motin #ifdef INET6 8884becee1SAlexander Motin /* 8984becee1SAlexander Motin * XXX: declare here to avoid to include many inet6 related files.. 9084becee1SAlexander Motin * should be more generalized? 9184becee1SAlexander Motin */ 9284becee1SAlexander Motin extern void nd6_setmtu(struct ifnet *); 9384becee1SAlexander Motin #endif 9484becee1SAlexander Motin 9575ee267cSGleb Smirnoff #define VLAN_DEF_HWIDTH 4 9664a17d2eSYaroslav Tykhiy #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 9775ee267cSGleb Smirnoff 982dc879b3SYaroslav Tykhiy #define UP_AND_RUNNING(ifp) \ 992dc879b3SYaroslav Tykhiy ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 1002dc879b3SYaroslav Tykhiy 101b08d611dSMatt Macy CK_SLIST_HEAD(ifvlanhead, ifvlan); 10275ee267cSGleb Smirnoff 10375ee267cSGleb Smirnoff struct ifvlantrunk { 10475ee267cSGleb Smirnoff struct ifnet *parent; /* parent interface of this trunk */ 105b08d611dSMatt Macy struct mtx lock; 10675ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 1075cb8c31aSYaroslav Tykhiy #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 1085cb8c31aSYaroslav Tykhiy struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 10975ee267cSGleb Smirnoff #else 11075ee267cSGleb Smirnoff struct ifvlanhead *hash; /* dynamic hash-list table */ 11175ee267cSGleb Smirnoff uint16_t hmask; 11275ee267cSGleb Smirnoff uint16_t hwidth; 11375ee267cSGleb Smirnoff #endif 11475ee267cSGleb Smirnoff int refcnt; 11575ee267cSGleb Smirnoff }; 1169d4fe4b2SBrooks Davis 117b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 118fb3bc596SJohn Baldwin struct vlan_snd_tag { 119fb3bc596SJohn Baldwin struct m_snd_tag com; 120fb3bc596SJohn Baldwin struct m_snd_tag *tag; 121fb3bc596SJohn Baldwin }; 122fb3bc596SJohn Baldwin 123fb3bc596SJohn Baldwin static inline struct vlan_snd_tag * 124fb3bc596SJohn Baldwin mst_to_vst(struct m_snd_tag *mst) 125fb3bc596SJohn Baldwin { 126fb3bc596SJohn Baldwin 127fb3bc596SJohn Baldwin return (__containerof(mst, struct vlan_snd_tag, com)); 128fb3bc596SJohn Baldwin } 129fb3bc596SJohn Baldwin #endif 130fb3bc596SJohn Baldwin 131d148c2a2SMatt Joras /* 132d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk with 133d148c2a2SMatt Joras * the assumption that none will be added/removed during iteration. 134d148c2a2SMatt Joras */ 135d148c2a2SMatt Joras #ifdef VLAN_ARRAY 136d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 137d148c2a2SMatt Joras size_t _i; \ 138d148c2a2SMatt Joras for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \ 139d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i]) != NULL) 140d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 141d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 142d148c2a2SMatt Joras struct ifvlan *_next; \ 143d148c2a2SMatt Joras size_t _i; \ 144d148c2a2SMatt Joras for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \ 145b08d611dSMatt Macy CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next) 146d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 147d148c2a2SMatt Joras 148d148c2a2SMatt Joras /* 149d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk while 150d148c2a2SMatt Joras * also modifying the number of vlans on the trunk. The iteration continues 151d148c2a2SMatt Joras * until some condition is met or there are no more vlans on the trunk. 152d148c2a2SMatt Joras */ 153d148c2a2SMatt Joras #ifdef VLAN_ARRAY 154d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */ 155d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 156d148c2a2SMatt Joras size_t _i; \ 157d148c2a2SMatt Joras for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \ 158d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i])) 159d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 160d148c2a2SMatt Joras /* 161d148c2a2SMatt Joras * The hash table case is more complicated. We allow for the hash table to be 162d148c2a2SMatt Joras * modified (i.e. vlans removed) while we are iterating over it. To allow for 163d148c2a2SMatt Joras * this we must restart the iteration every time we "touch" something during 164d148c2a2SMatt Joras * the iteration, since removal will resize the hash table and invalidate our 165d148c2a2SMatt Joras * current position. If acting on the touched element causes the trunk to be 166d148c2a2SMatt Joras * emptied, then iteration also stops. 167d148c2a2SMatt Joras */ 168d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 169d148c2a2SMatt Joras size_t _i; \ 170d148c2a2SMatt Joras bool _touch = false; \ 171d148c2a2SMatt Joras for (_i = 0; \ 172d148c2a2SMatt Joras !(_cond) && _i < (1 << (_trunk)->hwidth); \ 173d148c2a2SMatt Joras _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \ 174b08d611dSMatt Macy if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \ 175d148c2a2SMatt Joras (_touch = true)) 176d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 177d148c2a2SMatt Joras 178a3814acfSSam Leffler struct vlan_mc_entry { 179e4cd31ddSJeff Roberson struct sockaddr_dl mc_addr; 180b08d611dSMatt Macy CK_SLIST_ENTRY(vlan_mc_entry) mc_entries; 181c32a9d66SHans Petter Selasky struct epoch_context mc_epoch_ctx; 182a3814acfSSam Leffler }; 183a3814acfSSam Leffler 184a3814acfSSam Leffler struct ifvlan { 18575ee267cSGleb Smirnoff struct ifvlantrunk *ifv_trunk; 186fc74a9f9SBrooks Davis struct ifnet *ifv_ifp; 18775ee267cSGleb Smirnoff #define TRUNK(ifv) ((ifv)->ifv_trunk) 188c7cffd65SAlexander V. Chernikov #define PARENT(ifv) (TRUNK(ifv)->parent) 1896667db31SAlexander V. Chernikov void *ifv_cookie; 1901cf236fbSYaroslav Tykhiy int ifv_pflags; /* special flags we have set on parent */ 191d89baa5aSAlexander Motin int ifv_capenable; 19272755d28SMark Johnston int ifv_encaplen; /* encapsulation length */ 19372755d28SMark Johnston int ifv_mtufudge; /* MTU fudged by this much */ 19472755d28SMark Johnston int ifv_mintu; /* min transmission unit */ 195c7cffd65SAlexander V. Chernikov struct ether_8021q_tag ifv_qtag; 196c7cffd65SAlexander V. Chernikov #define ifv_proto ifv_qtag.proto 197c7cffd65SAlexander V. Chernikov #define ifv_vid ifv_qtag.vid 198c7cffd65SAlexander V. Chernikov #define ifv_pcp ifv_qtag.pcp 199d148c2a2SMatt Joras struct task lladdr_task; 200b08d611dSMatt Macy CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 201c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY 202b08d611dSMatt Macy CK_SLIST_ENTRY(ifvlan) ifv_list; 203c0cb022bSYaroslav Tykhiy #endif 204a3814acfSSam Leffler }; 205a3814acfSSam Leffler 20675ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */ 2071cf236fbSYaroslav Tykhiy static struct { 2081cf236fbSYaroslav Tykhiy int flag; 2091cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 2101cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 2111cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 2121cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 2131cf236fbSYaroslav Tykhiy {0, NULL} 2141cf236fbSYaroslav Tykhiy }; 215a3814acfSSam Leffler 216f1379734SKonstantin Belousov extern int vlan_mtag_pcp; 2172ccbbd06SMarcelo Araujo 21842a58907SGleb Smirnoff static const char vlanname[] = "vlan"; 21942a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface"); 2202cc2df49SGarrett Wollman 2215cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag; 222ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag; 2235cb8c31aSYaroslav Tykhiy 2244faedfe8SSam Leffler /* 225b08d611dSMatt Macy * if_vlan uses two module-level synchronizations primitives to allow concurrent 226b08d611dSMatt Macy * modification of vlan interfaces and (mostly) allow for vlans to be destroyed 227b08d611dSMatt Macy * while they are being used for tx/rx. To accomplish this in a way that has 228b08d611dSMatt Macy * acceptable performance and cooperation with other parts of the network stack 229b08d611dSMatt Macy * there is a non-sleepable epoch(9) and an sx(9). 23075ee267cSGleb Smirnoff * 231b08d611dSMatt Macy * The performance-sensitive paths that warrant using the epoch(9) are 232d148c2a2SMatt Joras * vlan_transmit and vlan_input. Both have to check for the vlan interface's 233d148c2a2SMatt Joras * existence using if_vlantrunk, and being in the network tx/rx paths the use 234b08d611dSMatt Macy * of an epoch(9) gives a measureable improvement in performance. 23575ee267cSGleb Smirnoff * 236d148c2a2SMatt Joras * The reason for having an sx(9) is mostly because there are still areas that 237d148c2a2SMatt Joras * must be sleepable and also have safe concurrent access to a vlan interface. 238d148c2a2SMatt Joras * Since the sx(9) exists, it is used by default in most paths unless sleeping 239d148c2a2SMatt Joras * is not permitted, or if it is not clear whether sleeping is permitted. 240d148c2a2SMatt Joras * 2414faedfe8SSam Leffler */ 242d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx 243d148c2a2SMatt Joras 244d148c2a2SMatt Joras static struct sx _VLAN_SX_ID; 245d148c2a2SMatt Joras 246d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \ 247c7cffd65SAlexander V. Chernikov sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE) 248d148c2a2SMatt Joras 249d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \ 250d148c2a2SMatt Joras sx_destroy(&_VLAN_SX_ID) 251d148c2a2SMatt Joras 252d148c2a2SMatt Joras #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID) 253d148c2a2SMatt Joras #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID) 254d148c2a2SMatt Joras #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID) 255d148c2a2SMatt Joras #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID) 256d148c2a2SMatt Joras #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED) 257d148c2a2SMatt Joras #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED) 258d148c2a2SMatt Joras #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED) 259d148c2a2SMatt Joras 260d148c2a2SMatt Joras /* 261b08d611dSMatt Macy * We also have a per-trunk mutex that should be acquired when changing 262b08d611dSMatt Macy * its state. 263d148c2a2SMatt Joras */ 264b08d611dSMatt Macy #define TRUNK_LOCK_INIT(trunk) mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF) 265b08d611dSMatt Macy #define TRUNK_LOCK_DESTROY(trunk) mtx_destroy(&(trunk)->lock) 266b08d611dSMatt Macy #define TRUNK_WLOCK(trunk) mtx_lock(&(trunk)->lock) 267b08d611dSMatt Macy #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock) 268b08d611dSMatt Macy #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED); 26975ee267cSGleb Smirnoff 270d148c2a2SMatt Joras /* 271d148c2a2SMatt Joras * The VLAN_ARRAY substitutes the dynamic hash with a static array 272d148c2a2SMatt Joras * with 4096 entries. In theory this can give a boost in processing, 273d148c2a2SMatt Joras * however in practice it does not. Probably this is because the array 274d148c2a2SMatt Joras * is too big to fit into CPU cache. 275d148c2a2SMatt Joras */ 27675ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 27775ee267cSGleb Smirnoff static void vlan_inithash(struct ifvlantrunk *trunk); 27875ee267cSGleb Smirnoff static void vlan_freehash(struct ifvlantrunk *trunk); 27975ee267cSGleb Smirnoff static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 28075ee267cSGleb Smirnoff static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 28175ee267cSGleb Smirnoff static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 28275ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 2837983103aSRobert Watson uint16_t vid); 28475ee267cSGleb Smirnoff #endif 28575ee267cSGleb Smirnoff static void trunk_destroy(struct ifvlantrunk *trunk); 2864faedfe8SSam Leffler 287114c608cSYaroslav Tykhiy static void vlan_init(void *foo); 288a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 289cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 290b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 291f3e7afe2SHans Petter Selasky static int vlan_snd_tag_alloc(struct ifnet *, 292f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *, struct m_snd_tag **); 293fb3bc596SJohn Baldwin static int vlan_snd_tag_modify(struct m_snd_tag *, 294fb3bc596SJohn Baldwin union if_snd_tag_modify_params *); 295fb3bc596SJohn Baldwin static int vlan_snd_tag_query(struct m_snd_tag *, 296fb3bc596SJohn Baldwin union if_snd_tag_query_params *); 297fa91f845SRandall Stewart static void vlan_snd_tag_free(struct m_snd_tag *); 2981a714ff2SRandall Stewart static struct m_snd_tag *vlan_next_snd_tag(struct m_snd_tag *); 2991a714ff2SRandall Stewart static void vlan_ratelimit_query(struct ifnet *, 3001a714ff2SRandall Stewart struct if_ratelimit_query_results *); 301f3e7afe2SHans Petter Selasky #endif 302d9b1d615SJohn Baldwin static void vlan_qflush(struct ifnet *ifp); 3031cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 3041cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 3051cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 306f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 307d9b1d615SJohn Baldwin static int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 30816cf6bdbSMatt Joras static int vlan_output(struct ifnet *ifp, struct mbuf *m, 30916cf6bdbSMatt Joras const struct sockaddr *dst, struct route *ro); 3106f359e28SJohn Baldwin static void vlan_unconfig(struct ifnet *ifp); 31128cc4d37SJohn Baldwin static void vlan_unconfig_locked(struct ifnet *ifp, int departing); 312c7cffd65SAlexander V. Chernikov static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag, 313c7cffd65SAlexander V. Chernikov uint16_t proto); 314a6fffd6cSBrooks Davis static void vlan_link_state(struct ifnet *ifp); 31575ee267cSGleb Smirnoff static void vlan_capabilities(struct ifvlan *ifv); 31675ee267cSGleb Smirnoff static void vlan_trunk_capabilities(struct ifnet *ifp); 317f731f104SBill Paul 318f941c31aSGleb Smirnoff static struct ifnet *vlan_clone_match_ethervid(const char *, int *); 319f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 3206b7330e2SSam Leffler static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 321f889d2efSBrooks Davis static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 322f889d2efSBrooks Davis 3235cb8c31aSYaroslav Tykhiy static void vlan_ifdetach(void *arg, struct ifnet *ifp); 324ea4ca115SAndrew Thompson static void vlan_iflladdr(void *arg, struct ifnet *ifp); 3255cb8c31aSYaroslav Tykhiy 326d148c2a2SMatt Joras static void vlan_lladdr_fn(void *arg, int pending); 327d148c2a2SMatt Joras 32842a58907SGleb Smirnoff static struct if_clone *vlan_cloner; 3299d4fe4b2SBrooks Davis 330ccf7ba97SMarko Zec #ifdef VIMAGE 3315f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner); 332ccf7ba97SMarko Zec #define V_vlan_cloner VNET(vlan_cloner) 333ccf7ba97SMarko Zec #endif 334ccf7ba97SMarko Zec 33575ee267cSGleb Smirnoff static void 336c32a9d66SHans Petter Selasky vlan_mc_free(struct epoch_context *ctx) 337c32a9d66SHans Petter Selasky { 338c32a9d66SHans Petter Selasky struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx); 339c32a9d66SHans Petter Selasky free(mc, M_VLAN); 340c32a9d66SHans Petter Selasky } 341c32a9d66SHans Petter Selasky 342cac30248SOleg Bulyzhin #ifndef VLAN_ARRAY 343cac30248SOleg Bulyzhin #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 344cac30248SOleg Bulyzhin 345c32a9d66SHans Petter Selasky static void 34675ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk) 34775ee267cSGleb Smirnoff { 34875ee267cSGleb Smirnoff int i, n; 34975ee267cSGleb Smirnoff 35075ee267cSGleb Smirnoff /* 35175ee267cSGleb Smirnoff * The trunk must not be locked here since we call malloc(M_WAITOK). 35275ee267cSGleb Smirnoff * It is OK in case this function is called before the trunk struct 35375ee267cSGleb Smirnoff * gets hooked up and becomes visible from other threads. 35475ee267cSGleb Smirnoff */ 35575ee267cSGleb Smirnoff 35675ee267cSGleb Smirnoff KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 35775ee267cSGleb Smirnoff ("%s: hash already initialized", __func__)); 35875ee267cSGleb Smirnoff 35975ee267cSGleb Smirnoff trunk->hwidth = VLAN_DEF_HWIDTH; 36075ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 36175ee267cSGleb Smirnoff trunk->hmask = n - 1; 36275ee267cSGleb Smirnoff trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 36375ee267cSGleb Smirnoff for (i = 0; i < n; i++) 364b08d611dSMatt Macy CK_SLIST_INIT(&trunk->hash[i]); 36575ee267cSGleb Smirnoff } 36675ee267cSGleb Smirnoff 36775ee267cSGleb Smirnoff static void 36875ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk) 36975ee267cSGleb Smirnoff { 37075ee267cSGleb Smirnoff #ifdef INVARIANTS 37175ee267cSGleb Smirnoff int i; 37275ee267cSGleb Smirnoff 37375ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 37475ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 375b08d611dSMatt Macy KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]), 37675ee267cSGleb Smirnoff ("%s: hash table not empty", __func__)); 37775ee267cSGleb Smirnoff #endif 37875ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 37975ee267cSGleb Smirnoff trunk->hash = NULL; 38075ee267cSGleb Smirnoff trunk->hwidth = trunk->hmask = 0; 38175ee267cSGleb Smirnoff } 38275ee267cSGleb Smirnoff 38375ee267cSGleb Smirnoff static int 38475ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 38575ee267cSGleb Smirnoff { 38675ee267cSGleb Smirnoff int i, b; 38775ee267cSGleb Smirnoff struct ifvlan *ifv2; 38875ee267cSGleb Smirnoff 389b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 39075ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 39175ee267cSGleb Smirnoff 39275ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 3937983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 394b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 3957983103aSRobert Watson if (ifv->ifv_vid == ifv2->ifv_vid) 39675ee267cSGleb Smirnoff return (EEXIST); 39775ee267cSGleb Smirnoff 39875ee267cSGleb Smirnoff /* 39975ee267cSGleb Smirnoff * Grow the hash when the number of vlans exceeds half of the number of 40075ee267cSGleb Smirnoff * hash buckets squared. This will make the average linked-list length 40175ee267cSGleb Smirnoff * buckets/2. 40275ee267cSGleb Smirnoff */ 40375ee267cSGleb Smirnoff if (trunk->refcnt > (b * b) / 2) { 40475ee267cSGleb Smirnoff vlan_growhash(trunk, 1); 4057983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 40675ee267cSGleb Smirnoff } 407b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 40875ee267cSGleb Smirnoff trunk->refcnt++; 40975ee267cSGleb Smirnoff 41075ee267cSGleb Smirnoff return (0); 41175ee267cSGleb Smirnoff } 41275ee267cSGleb Smirnoff 41375ee267cSGleb Smirnoff static int 41475ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 41575ee267cSGleb Smirnoff { 41675ee267cSGleb Smirnoff int i, b; 41775ee267cSGleb Smirnoff struct ifvlan *ifv2; 41875ee267cSGleb Smirnoff 419b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 42075ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 42175ee267cSGleb Smirnoff 42275ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 4237983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 424b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 42575ee267cSGleb Smirnoff if (ifv2 == ifv) { 42675ee267cSGleb Smirnoff trunk->refcnt--; 427b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list); 42875ee267cSGleb Smirnoff if (trunk->refcnt < (b * b) / 2) 42975ee267cSGleb Smirnoff vlan_growhash(trunk, -1); 43075ee267cSGleb Smirnoff return (0); 43175ee267cSGleb Smirnoff } 43275ee267cSGleb Smirnoff 43375ee267cSGleb Smirnoff panic("%s: vlan not found\n", __func__); 43475ee267cSGleb Smirnoff return (ENOENT); /*NOTREACHED*/ 43575ee267cSGleb Smirnoff } 43675ee267cSGleb Smirnoff 43775ee267cSGleb Smirnoff /* 43875ee267cSGleb Smirnoff * Grow the hash larger or smaller if memory permits. 43975ee267cSGleb Smirnoff */ 44075ee267cSGleb Smirnoff static void 44175ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 44275ee267cSGleb Smirnoff { 44375ee267cSGleb Smirnoff struct ifvlan *ifv; 44475ee267cSGleb Smirnoff struct ifvlanhead *hash2; 44575ee267cSGleb Smirnoff int hwidth2, i, j, n, n2; 44675ee267cSGleb Smirnoff 447b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 44875ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 44975ee267cSGleb Smirnoff 45075ee267cSGleb Smirnoff if (howmuch == 0) { 45175ee267cSGleb Smirnoff /* Harmless yet obvious coding error */ 45275ee267cSGleb Smirnoff printf("%s: howmuch is 0\n", __func__); 45375ee267cSGleb Smirnoff return; 45475ee267cSGleb Smirnoff } 45575ee267cSGleb Smirnoff 45675ee267cSGleb Smirnoff hwidth2 = trunk->hwidth + howmuch; 45775ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 45875ee267cSGleb Smirnoff n2 = 1 << hwidth2; 45975ee267cSGleb Smirnoff /* Do not shrink the table below the default */ 46075ee267cSGleb Smirnoff if (hwidth2 < VLAN_DEF_HWIDTH) 46175ee267cSGleb Smirnoff return; 46275ee267cSGleb Smirnoff 463b08d611dSMatt Macy hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK); 46475ee267cSGleb Smirnoff if (hash2 == NULL) { 46575ee267cSGleb Smirnoff printf("%s: out of memory -- hash size not changed\n", 46675ee267cSGleb Smirnoff __func__); 46775ee267cSGleb Smirnoff return; /* We can live with the old hash table */ 46875ee267cSGleb Smirnoff } 46975ee267cSGleb Smirnoff for (j = 0; j < n2; j++) 470b08d611dSMatt Macy CK_SLIST_INIT(&hash2[j]); 47175ee267cSGleb Smirnoff for (i = 0; i < n; i++) 472b08d611dSMatt Macy while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) { 473b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list); 4747983103aSRobert Watson j = HASH(ifv->ifv_vid, n2 - 1); 475b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 47675ee267cSGleb Smirnoff } 477b08d611dSMatt Macy NET_EPOCH_WAIT(); 47875ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 47975ee267cSGleb Smirnoff trunk->hash = hash2; 48075ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 48175ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 482f84b2d69SYaroslav Tykhiy 483f84b2d69SYaroslav Tykhiy if (bootverbose) 484f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 485f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 48675ee267cSGleb Smirnoff } 48775ee267cSGleb Smirnoff 48875ee267cSGleb Smirnoff static __inline struct ifvlan * 4897983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 49075ee267cSGleb Smirnoff { 49175ee267cSGleb Smirnoff struct ifvlan *ifv; 49275ee267cSGleb Smirnoff 493a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 49475ee267cSGleb Smirnoff 495b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list) 4967983103aSRobert Watson if (ifv->ifv_vid == vid) 49775ee267cSGleb Smirnoff return (ifv); 49875ee267cSGleb Smirnoff return (NULL); 49975ee267cSGleb Smirnoff } 50075ee267cSGleb Smirnoff 50175ee267cSGleb Smirnoff #if 0 50275ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 50375ee267cSGleb Smirnoff static void 50475ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 50575ee267cSGleb Smirnoff { 50675ee267cSGleb Smirnoff int i; 50775ee267cSGleb Smirnoff struct ifvlan *ifv; 50875ee267cSGleb Smirnoff 50975ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 51075ee267cSGleb Smirnoff printf("%d: ", i); 511b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 51275ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 51375ee267cSGleb Smirnoff printf("\n"); 51475ee267cSGleb Smirnoff } 51575ee267cSGleb Smirnoff } 51675ee267cSGleb Smirnoff #endif /* 0 */ 517e4cd31ddSJeff Roberson #else 518e4cd31ddSJeff Roberson 519e4cd31ddSJeff Roberson static __inline struct ifvlan * 5207983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 521e4cd31ddSJeff Roberson { 522e4cd31ddSJeff Roberson 5237983103aSRobert Watson return trunk->vlans[vid]; 524e4cd31ddSJeff Roberson } 525e4cd31ddSJeff Roberson 526e4cd31ddSJeff Roberson static __inline int 527e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 528e4cd31ddSJeff Roberson { 529e4cd31ddSJeff Roberson 5307983103aSRobert Watson if (trunk->vlans[ifv->ifv_vid] != NULL) 531e4cd31ddSJeff Roberson return EEXIST; 5327983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = ifv; 533e4cd31ddSJeff Roberson trunk->refcnt++; 534e4cd31ddSJeff Roberson 535e4cd31ddSJeff Roberson return (0); 536e4cd31ddSJeff Roberson } 537e4cd31ddSJeff Roberson 538e4cd31ddSJeff Roberson static __inline int 539e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 540e4cd31ddSJeff Roberson { 541e4cd31ddSJeff Roberson 5427983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = NULL; 543e4cd31ddSJeff Roberson trunk->refcnt--; 544e4cd31ddSJeff Roberson 545e4cd31ddSJeff Roberson return (0); 546e4cd31ddSJeff Roberson } 547e4cd31ddSJeff Roberson 548e4cd31ddSJeff Roberson static __inline void 549e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk) 550e4cd31ddSJeff Roberson { 551e4cd31ddSJeff Roberson } 552e4cd31ddSJeff Roberson 553e4cd31ddSJeff Roberson static __inline void 554e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk) 555e4cd31ddSJeff Roberson { 556e4cd31ddSJeff Roberson } 557e4cd31ddSJeff Roberson 55875ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 55975ee267cSGleb Smirnoff 56075ee267cSGleb Smirnoff static void 56175ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 56275ee267cSGleb Smirnoff { 563d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 56475ee267cSGleb Smirnoff 56575ee267cSGleb Smirnoff vlan_freehash(trunk); 56675ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 56733499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 5689bcf3ae4SAlexander Motin if_rele(trunk->parent); 56975ee267cSGleb Smirnoff free(trunk, M_VLAN); 57075ee267cSGleb Smirnoff } 57175ee267cSGleb Smirnoff 572f731f104SBill Paul /* 573f731f104SBill Paul * Program our multicast filter. What we're actually doing is 574f731f104SBill Paul * programming the multicast filter of the parent. This has the 575f731f104SBill Paul * side effect of causing the parent interface to receive multicast 576f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 577f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 578f731f104SBill Paul * to avoid this: there really is only one physical interface. 579f731f104SBill Paul */ 5802b120974SPeter Wemm static int 5812b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 582f731f104SBill Paul { 583f731f104SBill Paul struct ifnet *ifp_p; 5842d222cb7SAlexander Motin struct ifmultiaddr *ifma; 585f731f104SBill Paul struct ifvlan *sc; 586c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 587f731f104SBill Paul int error; 588f731f104SBill Paul 589b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 590d148c2a2SMatt Joras 591f731f104SBill Paul /* Find the parent. */ 592f731f104SBill Paul sc = ifp->if_softc; 59375ee267cSGleb Smirnoff ifp_p = PARENT(sc); 5941b2a4f7aSBill Fenner 5958b615593SMarko Zec CURVNET_SET_QUIET(ifp_p->if_vnet); 5968b615593SMarko Zec 597f731f104SBill Paul /* First, remove any existing filter entries. */ 598b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 599b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 6002d222cb7SAlexander Motin (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 6012a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 602f731f104SBill Paul } 603f731f104SBill Paul 604f731f104SBill Paul /* Now program new ones. */ 6052d222cb7SAlexander Motin IF_ADDR_WLOCK(ifp); 606d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 607f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 608f731f104SBill Paul continue; 60929c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 6102d222cb7SAlexander Motin if (mc == NULL) { 6112d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 612c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 61329c2dfbeSBruce M Simpson return (ENOMEM); 6142d222cb7SAlexander Motin } 615e4cd31ddSJeff Roberson bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 616e4cd31ddSJeff Roberson mc->mc_addr.sdl_index = ifp_p->if_index; 617b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 6182d222cb7SAlexander Motin } 6192d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 620b08d611dSMatt Macy CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) { 621e4cd31ddSJeff Roberson error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 6222d222cb7SAlexander Motin NULL); 623c6b2d024SGeorge V. Neville-Neil if (error) { 624c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 625f731f104SBill Paul return (error); 626f731f104SBill Paul } 627c6b2d024SGeorge V. Neville-Neil } 628f731f104SBill Paul 6298b615593SMarko Zec CURVNET_RESTORE(); 630f731f104SBill Paul return (0); 631f731f104SBill Paul } 6322cc2df49SGarrett Wollman 633a3814acfSSam Leffler /* 634ea4ca115SAndrew Thompson * A handler for parent interface link layer address changes. 635ea4ca115SAndrew Thompson * If the parent interface link layer address is changed we 636ea4ca115SAndrew Thompson * should also change it on all children vlans. 637ea4ca115SAndrew Thompson */ 638ea4ca115SAndrew Thompson static void 639ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 640ea4ca115SAndrew Thompson { 641a68cc388SGleb Smirnoff struct epoch_tracker et; 642ea4ca115SAndrew Thompson struct ifvlan *ifv; 643d148c2a2SMatt Joras struct ifnet *ifv_ifp; 644d148c2a2SMatt Joras struct ifvlantrunk *trunk; 645d148c2a2SMatt Joras struct sockaddr_dl *sdl; 646ea4ca115SAndrew Thompson 6477b7f772fSGleb Smirnoff /* Need the epoch since this is run on taskqueue_swi. */ 648a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 649d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 650d148c2a2SMatt Joras if (trunk == NULL) { 651a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 652ea4ca115SAndrew Thompson return; 653d148c2a2SMatt Joras } 654ea4ca115SAndrew Thompson 655ea4ca115SAndrew Thompson /* 656ea4ca115SAndrew Thompson * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 657d148c2a2SMatt Joras * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR 658d148c2a2SMatt Joras * ioctl calls on the parent garbling the lladdr of the child vlan. 659ea4ca115SAndrew Thompson */ 660d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 661d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 662d148c2a2SMatt Joras /* 663d148c2a2SMatt Joras * Copy new new lladdr into the ifv_ifp, enqueue a task 664d148c2a2SMatt Joras * to actually call if_setlladdr. if_setlladdr needs to 665d148c2a2SMatt Joras * be deferred to a taskqueue because it will call into 666d148c2a2SMatt Joras * the if_vlan ioctl path and try to acquire the global 667d148c2a2SMatt Joras * lock. 668d148c2a2SMatt Joras */ 669d148c2a2SMatt Joras ifv_ifp = ifv->ifv_ifp; 670d148c2a2SMatt Joras bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp), 671e4cd31ddSJeff Roberson ifp->if_addrlen); 672d148c2a2SMatt Joras sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr; 673d148c2a2SMatt Joras sdl->sdl_alen = ifp->if_addrlen; 674d148c2a2SMatt Joras taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 6756117727bSAndrew Thompson } 676d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 677a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 678ea4ca115SAndrew Thompson } 679ea4ca115SAndrew Thompson 680ea4ca115SAndrew Thompson /* 6815cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 6825cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 6835cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 6845428776eSJohn Baldwin * by ejecting its hot-plug card. However, if an ifnet is simply 6855428776eSJohn Baldwin * being renamed, then there's no need to tear down the state. 6865cb8c31aSYaroslav Tykhiy */ 6875cb8c31aSYaroslav Tykhiy static void 6885cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 6895cb8c31aSYaroslav Tykhiy { 6905cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 691d148c2a2SMatt Joras struct ifvlantrunk *trunk; 6925cb8c31aSYaroslav Tykhiy 6935428776eSJohn Baldwin /* If the ifnet is just being renamed, don't do anything. */ 6945428776eSJohn Baldwin if (ifp->if_flags & IFF_RENAMING) 6955428776eSJohn Baldwin return; 696d148c2a2SMatt Joras VLAN_XLOCK(); 697d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 698d148c2a2SMatt Joras if (trunk == NULL) { 699d148c2a2SMatt Joras VLAN_XUNLOCK(); 700d148c2a2SMatt Joras return; 701d148c2a2SMatt Joras } 7025428776eSJohn Baldwin 7035cb8c31aSYaroslav Tykhiy /* 7045cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 7055cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 7065cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 7075cb8c31aSYaroslav Tykhiy */ 708d148c2a2SMatt Joras VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk, 709d148c2a2SMatt Joras ifp->if_vlantrunk == NULL) 71028cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 711d148c2a2SMatt Joras 7125cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 7135cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 714d148c2a2SMatt Joras VLAN_XUNLOCK(); 7155cb8c31aSYaroslav Tykhiy } 7165cb8c31aSYaroslav Tykhiy 7175cb8c31aSYaroslav Tykhiy /* 718e4cd31ddSJeff Roberson * Return the trunk device for a virtual interface. 719e4cd31ddSJeff Roberson */ 720e4cd31ddSJeff Roberson static struct ifnet * 721e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp) 722e4cd31ddSJeff Roberson { 723e4cd31ddSJeff Roberson struct ifvlan *ifv; 724e4cd31ddSJeff Roberson 725b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 726b8a6e03fSGleb Smirnoff 727e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 728e4cd31ddSJeff Roberson return (NULL); 729d148c2a2SMatt Joras 730e4cd31ddSJeff Roberson ifv = ifp->if_softc; 731e4cd31ddSJeff Roberson ifp = NULL; 732e4cd31ddSJeff Roberson if (ifv->ifv_trunk) 733e4cd31ddSJeff Roberson ifp = PARENT(ifv); 734e4cd31ddSJeff Roberson return (ifp); 735e4cd31ddSJeff Roberson } 736e4cd31ddSJeff Roberson 737e4cd31ddSJeff Roberson /* 7387983103aSRobert Watson * Return the 12-bit VLAN VID for this interface, for use by external 7397983103aSRobert Watson * components such as Infiniband. 7407983103aSRobert Watson * 7417983103aSRobert Watson * XXXRW: Note that the function name here is historical; it should be named 7427983103aSRobert Watson * vlan_vid(). 743e4cd31ddSJeff Roberson */ 744e4cd31ddSJeff Roberson static int 7457983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp) 746e4cd31ddSJeff Roberson { 747e4cd31ddSJeff Roberson struct ifvlan *ifv; 748e4cd31ddSJeff Roberson 749e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 750e4cd31ddSJeff Roberson return (EINVAL); 751e4cd31ddSJeff Roberson ifv = ifp->if_softc; 7527983103aSRobert Watson *vidp = ifv->ifv_vid; 753e4cd31ddSJeff Roberson return (0); 754e4cd31ddSJeff Roberson } 755e4cd31ddSJeff Roberson 75632d2623aSNavdeep Parhar static int 75732d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp) 75832d2623aSNavdeep Parhar { 75932d2623aSNavdeep Parhar struct ifvlan *ifv; 76032d2623aSNavdeep Parhar 76132d2623aSNavdeep Parhar if (ifp->if_type != IFT_L2VLAN) 76232d2623aSNavdeep Parhar return (EINVAL); 76332d2623aSNavdeep Parhar ifv = ifp->if_softc; 76432d2623aSNavdeep Parhar *pcpp = ifv->ifv_pcp; 76532d2623aSNavdeep Parhar return (0); 76632d2623aSNavdeep Parhar } 76732d2623aSNavdeep Parhar 768e4cd31ddSJeff Roberson /* 769e4cd31ddSJeff Roberson * Return a driver specific cookie for this interface. Synchronization 770e4cd31ddSJeff Roberson * with setcookie must be provided by the driver. 771e4cd31ddSJeff Roberson */ 772e4cd31ddSJeff Roberson static void * 773e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp) 774e4cd31ddSJeff Roberson { 775e4cd31ddSJeff Roberson struct ifvlan *ifv; 776e4cd31ddSJeff Roberson 777e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 778e4cd31ddSJeff Roberson return (NULL); 779e4cd31ddSJeff Roberson ifv = ifp->if_softc; 780e4cd31ddSJeff Roberson return (ifv->ifv_cookie); 781e4cd31ddSJeff Roberson } 782e4cd31ddSJeff Roberson 783e4cd31ddSJeff Roberson /* 784e4cd31ddSJeff Roberson * Store a cookie in our softc that drivers can use to store driver 785e4cd31ddSJeff Roberson * private per-instance data in. 786e4cd31ddSJeff Roberson */ 787e4cd31ddSJeff Roberson static int 788e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie) 789e4cd31ddSJeff Roberson { 790e4cd31ddSJeff Roberson struct ifvlan *ifv; 791e4cd31ddSJeff Roberson 792e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 793e4cd31ddSJeff Roberson return (EINVAL); 794e4cd31ddSJeff Roberson ifv = ifp->if_softc; 795e4cd31ddSJeff Roberson ifv->ifv_cookie = cookie; 796e4cd31ddSJeff Roberson return (0); 797e4cd31ddSJeff Roberson } 798e4cd31ddSJeff Roberson 799e4cd31ddSJeff Roberson /* 8007983103aSRobert Watson * Return the vlan device present at the specific VID. 801e4cd31ddSJeff Roberson */ 802e4cd31ddSJeff Roberson static struct ifnet * 8037983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid) 804e4cd31ddSJeff Roberson { 805e4cd31ddSJeff Roberson struct ifvlantrunk *trunk; 806e4cd31ddSJeff Roberson struct ifvlan *ifv; 807e4cd31ddSJeff Roberson 808b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 809b8a6e03fSGleb Smirnoff 810e4cd31ddSJeff Roberson trunk = ifp->if_vlantrunk; 811b8a6e03fSGleb Smirnoff if (trunk == NULL) 812e4cd31ddSJeff Roberson return (NULL); 813e4cd31ddSJeff Roberson ifp = NULL; 8147983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 815e4cd31ddSJeff Roberson if (ifv) 816e4cd31ddSJeff Roberson ifp = ifv->ifv_ifp; 817e4cd31ddSJeff Roberson return (ifp); 818e4cd31ddSJeff Roberson } 819e4cd31ddSJeff Roberson 820e4cd31ddSJeff Roberson /* 821a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 822a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 823a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 824a3814acfSSam Leffler * set here. No one else in the system should be aware of this so 825a3814acfSSam Leffler * we use an explicit reference here. 826a3814acfSSam Leffler */ 827a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 828a3814acfSSam Leffler 829984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 830a6fffd6cSBrooks Davis extern void (*vlan_link_state_p)(struct ifnet *); 831127d7b2dSAndre Oppermann 8322b120974SPeter Wemm static int 8332b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 8342b120974SPeter Wemm { 8359d4fe4b2SBrooks Davis 8362b120974SPeter Wemm switch (type) { 8372b120974SPeter Wemm case MOD_LOAD: 8385cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 8395cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 8405cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 8415cb8c31aSYaroslav Tykhiy return (ENOMEM); 842ea4ca115SAndrew Thompson iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 843ea4ca115SAndrew Thompson vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 844ea4ca115SAndrew Thompson if (iflladdr_tag == NULL) 845ea4ca115SAndrew Thompson return (ENOMEM); 846d148c2a2SMatt Joras VLAN_LOCKING_INIT(); 8479d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 848127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 84975ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 850e4cd31ddSJeff Roberson vlan_trunkdev_p = vlan_trunkdev; 851e4cd31ddSJeff Roberson vlan_cookie_p = vlan_cookie; 852e4cd31ddSJeff Roberson vlan_setcookie_p = vlan_setcookie; 853e4cd31ddSJeff Roberson vlan_tag_p = vlan_tag; 85432d2623aSNavdeep Parhar vlan_pcp_p = vlan_pcp; 855e4cd31ddSJeff Roberson vlan_devat_p = vlan_devat; 856ccf7ba97SMarko Zec #ifndef VIMAGE 85742a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 85842a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 859ccf7ba97SMarko Zec #endif 86025c0f7b3SYaroslav Tykhiy if (bootverbose) 86125c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 86225c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 86325c0f7b3SYaroslav Tykhiy "full-size arrays" 86425c0f7b3SYaroslav Tykhiy #else 86525c0f7b3SYaroslav Tykhiy "hash tables with chaining" 86625c0f7b3SYaroslav Tykhiy #endif 86725c0f7b3SYaroslav Tykhiy 86825c0f7b3SYaroslav Tykhiy "\n"); 8692b120974SPeter Wemm break; 8702b120974SPeter Wemm case MOD_UNLOAD: 871ccf7ba97SMarko Zec #ifndef VIMAGE 87242a58907SGleb Smirnoff if_clone_detach(vlan_cloner); 873ccf7ba97SMarko Zec #endif 8745cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 875ea4ca115SAndrew Thompson EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 8769d4fe4b2SBrooks Davis vlan_input_p = NULL; 877127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 87875ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 879e4cd31ddSJeff Roberson vlan_trunkdev_p = NULL; 880e4cd31ddSJeff Roberson vlan_tag_p = NULL; 88109fe6320SNavdeep Parhar vlan_cookie_p = NULL; 88209fe6320SNavdeep Parhar vlan_setcookie_p = NULL; 883e4cd31ddSJeff Roberson vlan_devat_p = NULL; 884d148c2a2SMatt Joras VLAN_LOCKING_DESTROY(); 88525c0f7b3SYaroslav Tykhiy if (bootverbose) 88625c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 8879d4fe4b2SBrooks Davis break; 8883e019deaSPoul-Henning Kamp default: 8893e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 8902b120974SPeter Wemm } 89115a66c21SBruce M Simpson return (0); 8922b120974SPeter Wemm } 8932b120974SPeter Wemm 8942b120974SPeter Wemm static moduledata_t vlan_mod = { 8952b120974SPeter Wemm "if_vlan", 8962b120974SPeter Wemm vlan_modevent, 8979823d527SKevin Lo 0 8982b120974SPeter Wemm }; 8992b120974SPeter Wemm 9002b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 90111edc477SEd Maste MODULE_VERSION(if_vlan, 3); 9022cc2df49SGarrett Wollman 903ccf7ba97SMarko Zec #ifdef VIMAGE 904ccf7ba97SMarko Zec static void 905ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused) 906ccf7ba97SMarko Zec { 907ccf7ba97SMarko Zec 90842a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 90942a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 910ccf7ba97SMarko Zec V_vlan_cloner = vlan_cloner; 911ccf7ba97SMarko Zec } 912ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 913ccf7ba97SMarko Zec vnet_vlan_init, NULL); 914ccf7ba97SMarko Zec 915ccf7ba97SMarko Zec static void 916ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused) 917ccf7ba97SMarko Zec { 918ccf7ba97SMarko Zec 91942a58907SGleb Smirnoff if_clone_detach(V_vlan_cloner); 920ccf7ba97SMarko Zec } 921eb03a443SKristof Provost VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 922ccf7ba97SMarko Zec vnet_vlan_uninit, NULL); 923ccf7ba97SMarko Zec #endif 924ccf7ba97SMarko Zec 925f941c31aSGleb Smirnoff /* 926c7cffd65SAlexander V. Chernikov * Check for <etherif>.<vlan>[.<vlan> ...] style interface names. 927f941c31aSGleb Smirnoff */ 928f889d2efSBrooks Davis static struct ifnet * 929f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp) 9309d4fe4b2SBrooks Davis { 931f941c31aSGleb Smirnoff char ifname[IFNAMSIZ]; 932f941c31aSGleb Smirnoff char *cp; 933f889d2efSBrooks Davis struct ifnet *ifp; 9347983103aSRobert Watson int vid; 935f889d2efSBrooks Davis 936f941c31aSGleb Smirnoff strlcpy(ifname, name, IFNAMSIZ); 937c7cffd65SAlexander V. Chernikov if ((cp = strrchr(ifname, '.')) == NULL) 938f941c31aSGleb Smirnoff return (NULL); 939f941c31aSGleb Smirnoff *cp = '\0'; 9409bcf3ae4SAlexander Motin if ((ifp = ifunit_ref(ifname)) == NULL) 941f941c31aSGleb Smirnoff return (NULL); 942f941c31aSGleb Smirnoff /* Parse VID. */ 9439bcf3ae4SAlexander Motin if (*++cp == '\0') { 9449bcf3ae4SAlexander Motin if_rele(ifp); 945f941c31aSGleb Smirnoff return (NULL); 9469bcf3ae4SAlexander Motin } 9477983103aSRobert Watson vid = 0; 948fb92ad4aSJohn Baldwin for(; *cp >= '0' && *cp <= '9'; cp++) 9497983103aSRobert Watson vid = (vid * 10) + (*cp - '0'); 9509bcf3ae4SAlexander Motin if (*cp != '\0') { 9519bcf3ae4SAlexander Motin if_rele(ifp); 952f941c31aSGleb Smirnoff return (NULL); 9539bcf3ae4SAlexander Motin } 9547983103aSRobert Watson if (vidp != NULL) 9557983103aSRobert Watson *vidp = vid; 956f889d2efSBrooks Davis 95715a66c21SBruce M Simpson return (ifp); 958f889d2efSBrooks Davis } 959f889d2efSBrooks Davis 960f889d2efSBrooks Davis static int 961f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 962f889d2efSBrooks Davis { 9634be465abSAlexander V. Chernikov struct ifnet *ifp; 964f889d2efSBrooks Davis const char *cp; 965f889d2efSBrooks Davis 9664be465abSAlexander V. Chernikov ifp = vlan_clone_match_ethervid(name, NULL); 9674be465abSAlexander V. Chernikov if (ifp != NULL) { 9684be465abSAlexander V. Chernikov if_rele(ifp); 969f889d2efSBrooks Davis return (1); 9704be465abSAlexander V. Chernikov } 971f889d2efSBrooks Davis 97242a58907SGleb Smirnoff if (strncmp(vlanname, name, strlen(vlanname)) != 0) 973f889d2efSBrooks Davis return (0); 974f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 975f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 976f889d2efSBrooks Davis return (0); 977f889d2efSBrooks Davis } 978f889d2efSBrooks Davis 979f889d2efSBrooks Davis return (1); 980f889d2efSBrooks Davis } 981f889d2efSBrooks Davis 982f889d2efSBrooks Davis static int 9836b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 984f889d2efSBrooks Davis { 985f889d2efSBrooks Davis char *dp; 98653729367SAlexander V. Chernikov bool wildcard = false; 98753729367SAlexander V. Chernikov bool subinterface = false; 988f889d2efSBrooks Davis int unit; 989f889d2efSBrooks Davis int error; 99053729367SAlexander V. Chernikov int vid = 0; 99153729367SAlexander V. Chernikov uint16_t proto = ETHERTYPE_VLAN; 9929d4fe4b2SBrooks Davis struct ifvlan *ifv; 9939d4fe4b2SBrooks Davis struct ifnet *ifp; 99453729367SAlexander V. Chernikov struct ifnet *p = NULL; 9953ba24fdeSJohn Baldwin struct ifaddr *ifa; 9963ba24fdeSJohn Baldwin struct sockaddr_dl *sdl; 9976b7330e2SSam Leffler struct vlanreq vlr; 99865239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 999f889d2efSBrooks Davis 1000c7cffd65SAlexander V. Chernikov 10016b7330e2SSam Leffler /* 100253729367SAlexander V. Chernikov * There are three ways to specify the cloned device: 10036b7330e2SSam Leffler * o pass a parameter block with the clone request. 100453729367SAlexander V. Chernikov * o specify parameters in the text of the clone device name 10056b7330e2SSam Leffler * o specify no parameters and get an unattached device that 10066b7330e2SSam Leffler * must be configured separately. 100753729367SAlexander V. Chernikov * The first technique is preferred; the latter two are supported 1008c7cffd65SAlexander V. Chernikov * for backwards compatibility. 10097983103aSRobert Watson * 10107983103aSRobert Watson * XXXRW: Note historic use of the word "tag" here. New ioctls may be 10117983103aSRobert Watson * called for. 10126b7330e2SSam Leffler */ 101353729367SAlexander V. Chernikov 10146b7330e2SSam Leffler if (params) { 10156b7330e2SSam Leffler error = copyin(params, &vlr, sizeof(vlr)); 10166b7330e2SSam Leffler if (error) 10176b7330e2SSam Leffler return error; 101853729367SAlexander V. Chernikov vid = vlr.vlr_tag; 101953729367SAlexander V. Chernikov proto = vlr.vlr_proto; 102053729367SAlexander V. Chernikov 1021afbb64f1SAlexander V. Chernikov #ifdef COMPAT_FREEBSD12 1022afbb64f1SAlexander V. Chernikov if (proto == 0) 1023afbb64f1SAlexander V. Chernikov proto = ETHERTYPE_VLAN; 1024afbb64f1SAlexander V. Chernikov #endif 10259bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 10266b7330e2SSam Leffler if (p == NULL) 1027b1828acfSGleb Smirnoff return (ENXIO); 102853729367SAlexander V. Chernikov } 102953729367SAlexander V. Chernikov 103053729367SAlexander V. Chernikov if ((error = ifc_name2unit(name, &unit)) == 0) { 103153729367SAlexander V. Chernikov 103253729367SAlexander V. Chernikov /* 103353729367SAlexander V. Chernikov * vlanX interface. Set wildcard to true if the unit number 103453729367SAlexander V. Chernikov * is not fixed (-1) 103553729367SAlexander V. Chernikov */ 103653729367SAlexander V. Chernikov wildcard = (unit < 0); 103753729367SAlexander V. Chernikov } else { 103853729367SAlexander V. Chernikov struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid); 103953729367SAlexander V. Chernikov if (p_tmp != NULL) { 104053729367SAlexander V. Chernikov error = 0; 104153729367SAlexander V. Chernikov subinterface = true; 104253729367SAlexander V. Chernikov unit = IF_DUNIT_NONE; 104353729367SAlexander V. Chernikov wildcard = false; 104453729367SAlexander V. Chernikov if (p != NULL) { 104553729367SAlexander V. Chernikov if_rele(p_tmp); 104653729367SAlexander V. Chernikov if (p != p_tmp) 104753729367SAlexander V. Chernikov error = EINVAL; 104853729367SAlexander V. Chernikov } else 104953729367SAlexander V. Chernikov p = p_tmp; 105053729367SAlexander V. Chernikov } else 105153729367SAlexander V. Chernikov error = ENXIO; 105253729367SAlexander V. Chernikov } 105353729367SAlexander V. Chernikov 10549bcf3ae4SAlexander Motin if (error != 0) { 105553729367SAlexander V. Chernikov if (p != NULL) 10569bcf3ae4SAlexander Motin if_rele(p); 10576b7330e2SSam Leffler return (error); 10589bcf3ae4SAlexander Motin } 1059f889d2efSBrooks Davis 106053729367SAlexander V. Chernikov if (!subinterface) { 106153729367SAlexander V. Chernikov /* vlanX interface, mark X as busy or allocate new unit # */ 1062f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 10639bcf3ae4SAlexander Motin if (error != 0) { 10649bcf3ae4SAlexander Motin if (p != NULL) 10659bcf3ae4SAlexander Motin if_rele(p); 1066f889d2efSBrooks Davis return (error); 10679bcf3ae4SAlexander Motin } 106853729367SAlexander V. Chernikov } 1069f889d2efSBrooks Davis 1070f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 1071f889d2efSBrooks Davis if (wildcard) { 1072f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 1073f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 1074f889d2efSBrooks Davis len - (dp-name) - 1) { 1075f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 1076f889d2efSBrooks Davis } 1077f889d2efSBrooks Davis } 10789d4fe4b2SBrooks Davis 1079a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 1080fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 1081fc74a9f9SBrooks Davis if (ifp == NULL) { 108253729367SAlexander V. Chernikov if (!subinterface) 1083fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 1084fc74a9f9SBrooks Davis free(ifv, M_VLAN); 10859bcf3ae4SAlexander Motin if (p != NULL) 10869bcf3ae4SAlexander Motin if_rele(p); 1087fc74a9f9SBrooks Davis return (ENOSPC); 1088fc74a9f9SBrooks Davis } 1089b08d611dSMatt Macy CK_SLIST_INIT(&ifv->vlan_mc_listhead); 10909d4fe4b2SBrooks Davis ifp->if_softc = ifv; 1091f889d2efSBrooks Davis /* 1092cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 1093f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 1094f889d2efSBrooks Davis */ 1095f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 109642a58907SGleb Smirnoff ifp->if_dname = vlanname; 1097f889d2efSBrooks Davis ifp->if_dunit = unit; 10989d4fe4b2SBrooks Davis 1099114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 1100d9b1d615SJohn Baldwin ifp->if_transmit = vlan_transmit; 1101d9b1d615SJohn Baldwin ifp->if_qflush = vlan_qflush; 11029d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 1103b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1104f3e7afe2SHans Petter Selasky ifp->if_snd_tag_alloc = vlan_snd_tag_alloc; 1105fb3bc596SJohn Baldwin ifp->if_snd_tag_modify = vlan_snd_tag_modify; 1106fb3bc596SJohn Baldwin ifp->if_snd_tag_query = vlan_snd_tag_query; 1107fa91f845SRandall Stewart ifp->if_snd_tag_free = vlan_snd_tag_free; 11081a714ff2SRandall Stewart ifp->if_next_snd_tag = vlan_next_snd_tag; 11091a714ff2SRandall Stewart ifp->if_ratelimit_query = vlan_ratelimit_query; 1110f3e7afe2SHans Petter Selasky #endif 111164a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 1112fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 11139d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 1114211f625aSBill Fenner ifp->if_baudrate = 0; 1115a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 1116a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 11173ba24fdeSJohn Baldwin ifa = ifp->if_addr; 11183ba24fdeSJohn Baldwin sdl = (struct sockaddr_dl *)ifa->ifa_addr; 11193ba24fdeSJohn Baldwin sdl->sdl_type = IFT_L2VLAN; 11209d4fe4b2SBrooks Davis 11219bcf3ae4SAlexander Motin if (p != NULL) { 1122c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vid, proto); 11239bcf3ae4SAlexander Motin if_rele(p); 1124f889d2efSBrooks Davis if (error != 0) { 1125f889d2efSBrooks Davis /* 112628cc4d37SJohn Baldwin * Since we've partially failed, we need to back 1127f889d2efSBrooks Davis * out all the way, otherwise userland could get 1128f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 1129f889d2efSBrooks Davis */ 1130f889d2efSBrooks Davis ether_ifdetach(ifp); 1131249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 11324b22573aSBrooks Davis if_free(ifp); 113353729367SAlexander V. Chernikov if (!subinterface) 113496c8ef3aSMaxim Konovalov ifc_free_unit(ifc, unit); 1135f889d2efSBrooks Davis free(ifv, M_VLAN); 1136f889d2efSBrooks Davis 1137f889d2efSBrooks Davis return (error); 1138f889d2efSBrooks Davis } 1139f889d2efSBrooks Davis } 1140f889d2efSBrooks Davis 11419d4fe4b2SBrooks Davis return (0); 11429d4fe4b2SBrooks Davis } 11439d4fe4b2SBrooks Davis 1144f889d2efSBrooks Davis static int 1145f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 11469d4fe4b2SBrooks Davis { 11479d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 114853729367SAlexander V. Chernikov int unit = ifp->if_dunit; 1149c7cffd65SAlexander V. Chernikov 1150c7cffd65SAlexander V. Chernikov if (ifp->if_vlantrunk) 1151c7cffd65SAlexander V. Chernikov return (EBUSY); 1152b4e9f837SBrooks Davis 1153249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 1154249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 1155d148c2a2SMatt Joras /* 1156d148c2a2SMatt Joras * We should have the only reference to the ifv now, so we can now 1157d148c2a2SMatt Joras * drain any remaining lladdr task before freeing the ifnet and the 1158d148c2a2SMatt Joras * ifvlan. 1159d148c2a2SMatt Joras */ 1160d148c2a2SMatt Joras taskqueue_drain(taskqueue_thread, &ifv->lladdr_task); 1161b08d611dSMatt Macy NET_EPOCH_WAIT(); 11624b22573aSBrooks Davis if_free(ifp); 11639d4fe4b2SBrooks Davis free(ifv, M_VLAN); 116453729367SAlexander V. Chernikov if (unit != IF_DUNIT_NONE) 116553729367SAlexander V. Chernikov ifc_free_unit(ifc, unit); 1166b4e9f837SBrooks Davis 1167f889d2efSBrooks Davis return (0); 11689d4fe4b2SBrooks Davis } 11699d4fe4b2SBrooks Davis 117015a66c21SBruce M Simpson /* 117115a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 117215a66c21SBruce M Simpson */ 11732cc2df49SGarrett Wollman static void 1174114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 11752cc2df49SGarrett Wollman { 11762cc2df49SGarrett Wollman } 11772cc2df49SGarrett Wollman 11786d3a3ab7SGleb Smirnoff /* 1179d9b1d615SJohn Baldwin * The if_transmit method for vlan(4) interface. 11806d3a3ab7SGleb Smirnoff */ 1181d9b1d615SJohn Baldwin static int 1182d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m) 11832cc2df49SGarrett Wollman { 11842cc2df49SGarrett Wollman struct ifvlan *ifv; 11852cc2df49SGarrett Wollman struct ifnet *p; 11861ad7a257SPyun YongHyeon int error, len, mcast; 11872cc2df49SGarrett Wollman 1188b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1189b8a6e03fSGleb Smirnoff 11902cc2df49SGarrett Wollman ifv = ifp->if_softc; 1191d148c2a2SMatt Joras if (TRUNK(ifv) == NULL) { 1192d148c2a2SMatt Joras if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1193d148c2a2SMatt Joras m_freem(m); 1194d148c2a2SMatt Joras return (ENETDOWN); 1195d148c2a2SMatt Joras } 119675ee267cSGleb Smirnoff p = PARENT(ifv); 11971ad7a257SPyun YongHyeon len = m->m_pkthdr.len; 11981ad7a257SPyun YongHyeon mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 11992cc2df49SGarrett Wollman 1200a3814acfSSam Leffler BPF_MTAP(ifp, m); 12012cc2df49SGarrett Wollman 1202b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1203fb3bc596SJohn Baldwin if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) { 1204fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 1205fb3bc596SJohn Baldwin struct m_snd_tag *mst; 1206fb3bc596SJohn Baldwin 1207fb3bc596SJohn Baldwin MPASS(m->m_pkthdr.snd_tag->ifp == ifp); 1208fb3bc596SJohn Baldwin mst = m->m_pkthdr.snd_tag; 1209fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 1210fb3bc596SJohn Baldwin if (vst->tag->ifp != p) { 1211fb3bc596SJohn Baldwin if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1212fb3bc596SJohn Baldwin m_freem(m); 1213fb3bc596SJohn Baldwin return (EAGAIN); 1214fb3bc596SJohn Baldwin } 1215fb3bc596SJohn Baldwin 1216fb3bc596SJohn Baldwin m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag); 1217fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 1218fb3bc596SJohn Baldwin } 1219fb3bc596SJohn Baldwin #endif 1220fb3bc596SJohn Baldwin 1221f731f104SBill Paul /* 1222d9b1d615SJohn Baldwin * Do not run parent's if_transmit() if the parent is not up, 122324993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 122424993214SYaroslav Tykhiy */ 12252dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 1226a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1227d148c2a2SMatt Joras m_freem(m); 12288b20f6cfSHiroki Sato return (ENETDOWN); 122924993214SYaroslav Tykhiy } 123024993214SYaroslav Tykhiy 1231c7cffd65SAlexander V. Chernikov if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) { 1232a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1233d9b1d615SJohn Baldwin return (0); 12344af90a4dSMatthew N. Dodd } 12352cc2df49SGarrett Wollman 12362cc2df49SGarrett Wollman /* 12372cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 12382cc2df49SGarrett Wollman */ 1239aea78d20SKip Macy error = (p->if_transmit)(p, m); 1240299153b5SAlexander V. Chernikov if (error == 0) { 1241a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1242a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 1243a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast); 12441ad7a257SPyun YongHyeon } else 1245a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1246d9b1d615SJohn Baldwin return (error); 12472cc2df49SGarrett Wollman } 1248d9b1d615SJohn Baldwin 124916cf6bdbSMatt Joras static int 125016cf6bdbSMatt Joras vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 125116cf6bdbSMatt Joras struct route *ro) 125216cf6bdbSMatt Joras { 125316cf6bdbSMatt Joras struct ifvlan *ifv; 125416cf6bdbSMatt Joras struct ifnet *p; 125516cf6bdbSMatt Joras 1256b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1257b8a6e03fSGleb Smirnoff 1258c7cffd65SAlexander V. Chernikov /* 1259c7cffd65SAlexander V. Chernikov * Find the first non-VLAN parent interface. 1260c7cffd65SAlexander V. Chernikov */ 126116cf6bdbSMatt Joras ifv = ifp->if_softc; 1262c7cffd65SAlexander V. Chernikov do { 126316cf6bdbSMatt Joras if (TRUNK(ifv) == NULL) { 126416cf6bdbSMatt Joras m_freem(m); 126516cf6bdbSMatt Joras return (ENETDOWN); 126616cf6bdbSMatt Joras } 126716cf6bdbSMatt Joras p = PARENT(ifv); 1268c7cffd65SAlexander V. Chernikov ifv = p->if_softc; 1269c7cffd65SAlexander V. Chernikov } while (p->if_type == IFT_L2VLAN); 1270c7cffd65SAlexander V. Chernikov 127116cf6bdbSMatt Joras return p->if_output(ifp, m, dst, ro); 127216cf6bdbSMatt Joras } 127316cf6bdbSMatt Joras 1274d9b1d615SJohn Baldwin /* 1275d9b1d615SJohn Baldwin * The ifp->if_qflush entry point for vlan(4) is a no-op. 1276d9b1d615SJohn Baldwin */ 1277d9b1d615SJohn Baldwin static void 1278d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused) 1279d9b1d615SJohn Baldwin { 1280f731f104SBill Paul } 1281f731f104SBill Paul 1282a3814acfSSam Leffler static void 1283a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 1284f731f104SBill Paul { 1285d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1286f731f104SBill Paul struct ifvlan *ifv; 12872ccbbd06SMarcelo Araujo struct m_tag *mtag; 12882ccbbd06SMarcelo Araujo uint16_t vid, tag; 128975ee267cSGleb Smirnoff 1290b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1291b8a6e03fSGleb Smirnoff 1292d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1293d148c2a2SMatt Joras if (trunk == NULL) { 1294d148c2a2SMatt Joras m_freem(m); 1295d148c2a2SMatt Joras return; 1296d148c2a2SMatt Joras } 1297a3814acfSSam Leffler 1298f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 1299a3814acfSSam Leffler /* 130014e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 1301a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 1302a3814acfSSam Leffler */ 13032ccbbd06SMarcelo Araujo tag = m->m_pkthdr.ether_vtag; 13046ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 1305a3814acfSSam Leffler } else { 130675ee267cSGleb Smirnoff struct ether_vlan_header *evl; 130775ee267cSGleb Smirnoff 130814e98256SYaroslav Tykhiy /* 130914e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 131014e98256SYaroslav Tykhiy */ 1311a3814acfSSam Leffler switch (ifp->if_type) { 1312a3814acfSSam Leffler case IFT_ETHER: 1313a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 1314a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 1315a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 1316a3814acfSSam Leffler return; 1317a3814acfSSam Leffler } 1318a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 13192ccbbd06SMarcelo Araujo tag = ntohs(evl->evl_tag); 1320db8b5973SYaroslav Tykhiy 1321db8b5973SYaroslav Tykhiy /* 13222dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 13232dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 13242dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 13252dc879b3SYaroslav Tykhiy * type field is already in place. 1326db8b5973SYaroslav Tykhiy */ 13272dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 13282dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 13292dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 1330a3814acfSSam Leffler break; 13312dc879b3SYaroslav Tykhiy 1332a3814acfSSam Leffler default: 1333db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 133460c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 133560c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 1336a3814acfSSam Leffler #endif 13373751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1338d148c2a2SMatt Joras m_freem(m); 133960c60618SYaroslav Tykhiy return; 1340a3814acfSSam Leffler } 13417a46ec8fSBrooks Davis } 13427a46ec8fSBrooks Davis 13432ccbbd06SMarcelo Araujo vid = EVL_VLANOFTAG(tag); 13442ccbbd06SMarcelo Araujo 13457983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 13462dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 1347b08d611dSMatt Macy if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1348d148c2a2SMatt Joras m_freem(m); 134975ee267cSGleb Smirnoff return; 135075ee267cSGleb Smirnoff } 1351f731f104SBill Paul 13522ccbbd06SMarcelo Araujo if (vlan_mtag_pcp) { 13532ccbbd06SMarcelo Araujo /* 13542ccbbd06SMarcelo Araujo * While uncommon, it is possible that we will find a 802.1q 13552ccbbd06SMarcelo Araujo * packet encapsulated inside another packet that also had an 13562ccbbd06SMarcelo Araujo * 802.1q header. For example, ethernet tunneled over IPSEC 13572ccbbd06SMarcelo Araujo * arriving over ethernet. In that case, we replace the 13582ccbbd06SMarcelo Araujo * existing 802.1q PCP m_tag value. 13592ccbbd06SMarcelo Araujo */ 13602ccbbd06SMarcelo Araujo mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 13612ccbbd06SMarcelo Araujo if (mtag == NULL) { 13622ccbbd06SMarcelo Araujo mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN, 13632ccbbd06SMarcelo Araujo sizeof(uint8_t), M_NOWAIT); 13642ccbbd06SMarcelo Araujo if (mtag == NULL) { 13652ccbbd06SMarcelo Araujo if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1366d148c2a2SMatt Joras m_freem(m); 13672ccbbd06SMarcelo Araujo return; 13682ccbbd06SMarcelo Araujo } 13692ccbbd06SMarcelo Araujo m_tag_prepend(m, mtag); 13702ccbbd06SMarcelo Araujo } 13712ccbbd06SMarcelo Araujo *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag); 13722ccbbd06SMarcelo Araujo } 13732ccbbd06SMarcelo Araujo 1374fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 1375c0304424SGleb Smirnoff if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1); 13762cc2df49SGarrett Wollman 1377a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 1378fdbf1174SMatt Joras (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m); 13792cc2df49SGarrett Wollman } 13802cc2df49SGarrett Wollman 1381d148c2a2SMatt Joras static void 1382d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused) 1383d148c2a2SMatt Joras { 1384d148c2a2SMatt Joras struct ifvlan *ifv; 1385d148c2a2SMatt Joras struct ifnet *ifp; 1386d148c2a2SMatt Joras 1387d148c2a2SMatt Joras ifv = (struct ifvlan *)arg; 1388d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 13895191a3aeSKristof Provost 13905191a3aeSKristof Provost CURVNET_SET(ifp->if_vnet); 13915191a3aeSKristof Provost 1392d148c2a2SMatt Joras /* The ifv_ifp already has the lladdr copied in. */ 1393d148c2a2SMatt Joras if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen); 13945191a3aeSKristof Provost 13955191a3aeSKristof Provost CURVNET_RESTORE(); 1396d148c2a2SMatt Joras } 1397d148c2a2SMatt Joras 13982cc2df49SGarrett Wollman static int 1399c7cffd65SAlexander V. Chernikov vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid, 1400c7cffd65SAlexander V. Chernikov uint16_t proto) 14012cc2df49SGarrett Wollman { 14026dcec895SGleb Smirnoff struct epoch_tracker et; 140375ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 14041cf236fbSYaroslav Tykhiy struct ifnet *ifp; 140575ee267cSGleb Smirnoff int error = 0; 14062cc2df49SGarrett Wollman 1407b1828acfSGleb Smirnoff /* 1408b1828acfSGleb Smirnoff * We can handle non-ethernet hardware types as long as 1409b1828acfSGleb Smirnoff * they handle the tagging and headers themselves. 1410b1828acfSGleb Smirnoff */ 1411e4cd31ddSJeff Roberson if (p->if_type != IFT_ETHER && 1412c7cffd65SAlexander V. Chernikov p->if_type != IFT_L2VLAN && 1413e4cd31ddSJeff Roberson (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 141415a66c21SBruce M Simpson return (EPROTONOSUPPORT); 141564a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 141664a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 1417b1828acfSGleb Smirnoff /* 1418b1828acfSGleb Smirnoff * Don't let the caller set up a VLAN VID with 1419b1828acfSGleb Smirnoff * anything except VLID bits. 1420b1828acfSGleb Smirnoff * VID numbers 0x0 and 0xFFF are reserved. 1421b1828acfSGleb Smirnoff */ 1422b1828acfSGleb Smirnoff if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK)) 1423b1828acfSGleb Smirnoff return (EINVAL); 142475ee267cSGleb Smirnoff if (ifv->ifv_trunk) 142515a66c21SBruce M Simpson return (EBUSY); 14262cc2df49SGarrett Wollman 1427d148c2a2SMatt Joras VLAN_XLOCK(); 142875ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 142975ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 143075ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 143175ee267cSGleb Smirnoff vlan_inithash(trunk); 143275ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 1433d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 143475ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 143575ee267cSGleb Smirnoff trunk->parent = p; 14369bcf3ae4SAlexander Motin if_ref(trunk->parent); 1437b08d611dSMatt Macy TRUNK_WUNLOCK(trunk); 143875ee267cSGleb Smirnoff } else { 143975ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 144075ee267cSGleb Smirnoff } 144175ee267cSGleb Smirnoff 14427983103aSRobert Watson ifv->ifv_vid = vid; /* must set this before vlan_inshash() */ 14432ccbbd06SMarcelo Araujo ifv->ifv_pcp = 0; /* Default: best effort delivery. */ 144475ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 144575ee267cSGleb Smirnoff if (error) 144675ee267cSGleb Smirnoff goto done; 1447c7cffd65SAlexander V. Chernikov ifv->ifv_proto = proto; 1448a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1449a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 14501cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1451d89baa5aSAlexander Motin ifv->ifv_capenable = -1; 1452a3814acfSSam Leffler 1453a3814acfSSam Leffler /* 1454a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1455a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1456656acce4SYaroslav Tykhiy * use it. 1457a3814acfSSam Leffler */ 1458656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1459656acce4SYaroslav Tykhiy /* 1460656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1461656acce4SYaroslav Tykhiy * handle extended frames. 1462656acce4SYaroslav Tykhiy */ 1463a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1464656acce4SYaroslav Tykhiy } else { 1465a3814acfSSam Leffler /* 1466a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1467a3814acfSSam Leffler * makes us incompatible with strictly compliant 1468a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1469a3814acfSSam Leffler * the feature with other NetBSD implementations, 1470a3814acfSSam Leffler * which might still be useful. 1471a3814acfSSam Leffler */ 1472a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1473a3814acfSSam Leffler } 1474a3814acfSSam Leffler 147575ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 14761cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 1477e4cd31ddSJeff Roberson /* 1478e4cd31ddSJeff Roberson * Initialize fields from our parent. This duplicates some 1479e4cd31ddSJeff Roberson * work with ether_ifattach() but allows for non-ethernet 1480e4cd31ddSJeff Roberson * interfaces to also work. 1481e4cd31ddSJeff Roberson */ 14821cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 148375ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 1484e4cd31ddSJeff Roberson ifp->if_input = p->if_input; 1485e4cd31ddSJeff Roberson ifp->if_resolvemulti = p->if_resolvemulti; 1486e4cd31ddSJeff Roberson ifp->if_addrlen = p->if_addrlen; 1487e4cd31ddSJeff Roberson ifp->if_broadcastaddr = p->if_broadcastaddr; 148832a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 1489e4cd31ddSJeff Roberson 14902cc2df49SGarrett Wollman /* 149116cf6bdbSMatt Joras * We wrap the parent's if_output using vlan_output to ensure that it 149216cf6bdbSMatt Joras * can't become stale. 149316cf6bdbSMatt Joras */ 149416cf6bdbSMatt Joras ifp->if_output = vlan_output; 149516cf6bdbSMatt Joras 149616cf6bdbSMatt Joras /* 149724993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 149824993214SYaroslav Tykhiy * Other flags are none of our business. 14992cc2df49SGarrett Wollman */ 150064a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 15011cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 15021cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 15031cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 15041cf236fbSYaroslav Tykhiy 15051cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 15062cc2df49SGarrett Wollman 15076dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 150875ee267cSGleb Smirnoff vlan_capabilities(ifv); 15096dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 1510a3814acfSSam Leffler 1511a3814acfSSam Leffler /* 1512e4cd31ddSJeff Roberson * Set up our interface address to reflect the underlying 15132cc2df49SGarrett Wollman * physical interface's. 15142cc2df49SGarrett Wollman */ 1515a961401eSAndrey V. Elsukov TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv); 1516e4cd31ddSJeff Roberson ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1517e4cd31ddSJeff Roberson p->if_addrlen; 15181b2a4f7aSBill Fenner 1519a961401eSAndrey V. Elsukov /* 1520a961401eSAndrey V. Elsukov * Do not schedule link address update if it was the same 1521a961401eSAndrey V. Elsukov * as previous parent's. This helps avoid updating for each 1522a961401eSAndrey V. Elsukov * associated llentry. 1523a961401eSAndrey V. Elsukov */ 1524a961401eSAndrey V. Elsukov if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) { 1525a961401eSAndrey V. Elsukov bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1526a961401eSAndrey V. Elsukov taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 1527a961401eSAndrey V. Elsukov } 15282ada9747SYaroslav Tykhiy 15292ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 15302ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 1531d148c2a2SMatt Joras 1532d148c2a2SMatt Joras /* Update flags on the parent, if necessary. */ 1533d148c2a2SMatt Joras vlan_setflags(ifp, 1); 1534b08d611dSMatt Macy 1535d148c2a2SMatt Joras /* 1536b08d611dSMatt Macy * Configure multicast addresses that may already be 1537b08d611dSMatt Macy * joined on the vlan device. 1538d148c2a2SMatt Joras */ 1539b08d611dSMatt Macy (void)vlan_setmulti(ifp); 1540b08d611dSMatt Macy 1541b08d611dSMatt Macy done: 1542c725524cSJack F Vogel if (error == 0) 15437983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid); 1544d148c2a2SMatt Joras VLAN_XUNLOCK(); 154575ee267cSGleb Smirnoff 154675ee267cSGleb Smirnoff return (error); 15472cc2df49SGarrett Wollman } 15482cc2df49SGarrett Wollman 15496f359e28SJohn Baldwin static void 1550f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1551f731f104SBill Paul { 15525cb8c31aSYaroslav Tykhiy 1553d148c2a2SMatt Joras VLAN_XLOCK(); 155428cc4d37SJohn Baldwin vlan_unconfig_locked(ifp, 0); 1555d148c2a2SMatt Joras VLAN_XUNLOCK(); 15565cb8c31aSYaroslav Tykhiy } 15575cb8c31aSYaroslav Tykhiy 15586f359e28SJohn Baldwin static void 155928cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing) 15605cb8c31aSYaroslav Tykhiy { 156175ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1562f731f104SBill Paul struct vlan_mc_entry *mc; 1563f731f104SBill Paul struct ifvlan *ifv; 1564c725524cSJack F Vogel struct ifnet *parent; 156528cc4d37SJohn Baldwin int error; 1566f731f104SBill Paul 1567d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 15684faedfe8SSam Leffler 1569f731f104SBill Paul ifv = ifp->if_softc; 157075ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 157122893351SJack F Vogel parent = NULL; 1572f731f104SBill Paul 157322893351SJack F Vogel if (trunk != NULL) { 157422893351SJack F Vogel parent = trunk->parent; 15751b2a4f7aSBill Fenner 1576f731f104SBill Paul /* 1577f731f104SBill Paul * Since the interface is being unconfigured, we need to 1578f731f104SBill Paul * empty the list of multicast groups that we may have joined 15791b2a4f7aSBill Fenner * while we were alive from the parent's list. 1580f731f104SBill Paul */ 1581b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 15826f359e28SJohn Baldwin /* 158328cc4d37SJohn Baldwin * If the parent interface is being detached, 1584b90dde2fSJohn Baldwin * all its multicast addresses have already 158528cc4d37SJohn Baldwin * been removed. Warn about errors if 158628cc4d37SJohn Baldwin * if_delmulti() does fail, but don't abort as 158728cc4d37SJohn Baldwin * all callers expect vlan destruction to 158828cc4d37SJohn Baldwin * succeed. 15896f359e28SJohn Baldwin */ 159028cc4d37SJohn Baldwin if (!departing) { 159128cc4d37SJohn Baldwin error = if_delmulti(parent, 1592e4cd31ddSJeff Roberson (struct sockaddr *)&mc->mc_addr); 159328cc4d37SJohn Baldwin if (error) 159428cc4d37SJohn Baldwin if_printf(ifp, 159528cc4d37SJohn Baldwin "Failed to delete multicast address from parent: %d\n", 159628cc4d37SJohn Baldwin error); 159728cc4d37SJohn Baldwin } 1598b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 15992a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 1600f731f104SBill Paul } 1601a3814acfSSam Leffler 16021cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 1603d148c2a2SMatt Joras 160475ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 160575ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 160675ee267cSGleb Smirnoff 160775ee267cSGleb Smirnoff /* 160875ee267cSGleb Smirnoff * Check if we were the last. 160975ee267cSGleb Smirnoff */ 161075ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 16112d222cb7SAlexander Motin parent->if_vlantrunk = NULL; 1612b08d611dSMatt Macy NET_EPOCH_WAIT(); 161375ee267cSGleb Smirnoff trunk_destroy(trunk); 1614d148c2a2SMatt Joras } 16151b2a4f7aSBill Fenner } 1616f731f104SBill Paul 1617f731f104SBill Paul /* Disconnect from parent. */ 16181cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 16191cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 16205cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 16215cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 16225cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1623f731f104SBill Paul 162422893351SJack F Vogel /* 162522893351SJack F Vogel * Only dispatch an event if vlan was 162622893351SJack F Vogel * attached, otherwise there is nothing 162722893351SJack F Vogel * to cleanup anyway. 162822893351SJack F Vogel */ 162922893351SJack F Vogel if (parent != NULL) 16307983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid); 1631f731f104SBill Paul } 1632f731f104SBill Paul 16331cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1634f731f104SBill Paul static int 16351cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 16361cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1637a3814acfSSam Leffler { 16381cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 16391cf236fbSYaroslav Tykhiy int error; 1640a3814acfSSam Leffler 1641d148c2a2SMatt Joras VLAN_SXLOCK_ASSERT(); 1642a3814acfSSam Leffler 16431cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 16441cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 16451cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 16461cf236fbSYaroslav Tykhiy 16471cf236fbSYaroslav Tykhiy /* 16481cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 16491cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 16501cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 16511cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 16521cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 16531cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 16541cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 16551cf236fbSYaroslav Tykhiy */ 16561cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 165775ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 16581cf236fbSYaroslav Tykhiy if (error) 1659a3814acfSSam Leffler return (error); 16601cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 16611cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 16621cf236fbSYaroslav Tykhiy } 16631cf236fbSYaroslav Tykhiy return (0); 16641cf236fbSYaroslav Tykhiy } 16651cf236fbSYaroslav Tykhiy 16661cf236fbSYaroslav Tykhiy /* 16671cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 16681cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 16691cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 16701cf236fbSYaroslav Tykhiy */ 16711cf236fbSYaroslav Tykhiy static int 16721cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 16731cf236fbSYaroslav Tykhiy { 16741cf236fbSYaroslav Tykhiy int error, i; 16751cf236fbSYaroslav Tykhiy 16761cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 16771cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 16781cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 16791cf236fbSYaroslav Tykhiy if (error) 16801cf236fbSYaroslav Tykhiy return (error); 16811cf236fbSYaroslav Tykhiy } 16821cf236fbSYaroslav Tykhiy return (0); 1683a3814acfSSam Leffler } 1684a3814acfSSam Leffler 1685127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1686127d7b2dSAndre Oppermann static void 1687a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp) 1688127d7b2dSAndre Oppermann { 1689b2807792SGleb Smirnoff struct epoch_tracker et; 1690d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1691127d7b2dSAndre Oppermann struct ifvlan *ifv; 1692127d7b2dSAndre Oppermann 1693b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 1694d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1695b2807792SGleb Smirnoff if (trunk == NULL) { 1696b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 1697d148c2a2SMatt Joras return; 1698b2807792SGleb Smirnoff } 1699d148c2a2SMatt Joras 1700d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 1701d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 1702aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1703fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 170475ee267cSGleb Smirnoff trunk->parent->if_link_state); 1705127d7b2dSAndre Oppermann } 1706d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 1707b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 170875ee267cSGleb Smirnoff } 170975ee267cSGleb Smirnoff 171075ee267cSGleb Smirnoff static void 171175ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 171275ee267cSGleb Smirnoff { 1713d148c2a2SMatt Joras struct ifnet *p; 1714d148c2a2SMatt Joras struct ifnet *ifp; 17159fd573c3SHans Petter Selasky struct ifnet_hw_tsomax hw_tsomax; 1716d89baa5aSAlexander Motin int cap = 0, ena = 0, mena; 1717d89baa5aSAlexander Motin u_long hwa = 0; 171875ee267cSGleb Smirnoff 1719a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 1720b8a6e03fSGleb Smirnoff VLAN_SXLOCK_ASSERT(); 1721b8a6e03fSGleb Smirnoff 1722d148c2a2SMatt Joras p = PARENT(ifv); 1723d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 172475ee267cSGleb Smirnoff 1725d89baa5aSAlexander Motin /* Mask parent interface enabled capabilities disabled by user. */ 1726d89baa5aSAlexander Motin mena = p->if_capenable & ifv->ifv_capenable; 1727d89baa5aSAlexander Motin 172875ee267cSGleb Smirnoff /* 172975ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 173075ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 173175ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 173275ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 173375ee267cSGleb Smirnoff */ 173475ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1735d89baa5aSAlexander Motin cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 173675ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 173775ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1738d89baa5aSAlexander Motin ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 1739d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM) 1740d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP | 1741d89baa5aSAlexander Motin CSUM_UDP | CSUM_SCTP); 1742d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM_IPV6) 1743d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_TCP_IPV6 | 1744d89baa5aSAlexander Motin CSUM_UDP_IPV6 | CSUM_SCTP_IPV6); 174575ee267cSGleb Smirnoff } 1746d89baa5aSAlexander Motin 17479b76d9cbSPyun YongHyeon /* 17489b76d9cbSPyun YongHyeon * If the parent interface can do TSO on VLANs then 17499b76d9cbSPyun YongHyeon * propagate the hardware-assisted flag. TSO on VLANs 17509b76d9cbSPyun YongHyeon * does not necessarily require hardware VLAN tagging. 17519b76d9cbSPyun YongHyeon */ 17529fd573c3SHans Petter Selasky memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 17539fd573c3SHans Petter Selasky if_hw_tsomax_common(p, &hw_tsomax); 17549fd573c3SHans Petter Selasky if_hw_tsomax_update(ifp, &hw_tsomax); 17559b76d9cbSPyun YongHyeon if (p->if_capabilities & IFCAP_VLAN_HWTSO) 1756d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TSO; 17579b76d9cbSPyun YongHyeon if (p->if_capenable & IFCAP_VLAN_HWTSO) { 1758d89baa5aSAlexander Motin ena |= mena & IFCAP_TSO; 1759d89baa5aSAlexander Motin if (ena & IFCAP_TSO) 1760d89baa5aSAlexander Motin hwa |= p->if_hwassist & CSUM_TSO; 17619b76d9cbSPyun YongHyeon } 176209fe6320SNavdeep Parhar 176309fe6320SNavdeep Parhar /* 176459150e91SAlexander Motin * If the parent interface can do LRO and checksum offloading on 176559150e91SAlexander Motin * VLANs, then guess it may do LRO on VLANs. False positive here 176659150e91SAlexander Motin * cost nothing, while false negative may lead to some confusions. 176759150e91SAlexander Motin */ 176859150e91SAlexander Motin if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 176959150e91SAlexander Motin cap |= p->if_capabilities & IFCAP_LRO; 177059150e91SAlexander Motin if (p->if_capenable & IFCAP_VLAN_HWCSUM) 177159150e91SAlexander Motin ena |= p->if_capenable & IFCAP_LRO; 177259150e91SAlexander Motin 177359150e91SAlexander Motin /* 177409fe6320SNavdeep Parhar * If the parent interface can offload TCP connections over VLANs then 177509fe6320SNavdeep Parhar * propagate its TOE capability to the VLAN interface. 177609fe6320SNavdeep Parhar * 177709fe6320SNavdeep Parhar * All TOE drivers in the tree today can deal with VLANs. If this 177809fe6320SNavdeep Parhar * changes then IFCAP_VLAN_TOE should be promoted to a full capability 177909fe6320SNavdeep Parhar * with its own bit. 178009fe6320SNavdeep Parhar */ 178109fe6320SNavdeep Parhar #define IFCAP_VLAN_TOE IFCAP_TOE 178209fe6320SNavdeep Parhar if (p->if_capabilities & IFCAP_VLAN_TOE) 1783d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TOE; 178409fe6320SNavdeep Parhar if (p->if_capenable & IFCAP_VLAN_TOE) { 178509fe6320SNavdeep Parhar TOEDEV(ifp) = TOEDEV(p); 1786d89baa5aSAlexander Motin ena |= mena & IFCAP_TOE; 178709fe6320SNavdeep Parhar } 1788f3e7afe2SHans Petter Selasky 1789d89baa5aSAlexander Motin /* 1790d89baa5aSAlexander Motin * If the parent interface supports dynamic link state, so does the 1791d89baa5aSAlexander Motin * VLAN interface. 1792d89baa5aSAlexander Motin */ 1793d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_LINKSTATE); 1794d89baa5aSAlexander Motin ena |= (mena & IFCAP_LINKSTATE); 1795d89baa5aSAlexander Motin 1796f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1797f3e7afe2SHans Petter Selasky /* 1798f3e7afe2SHans Petter Selasky * If the parent interface supports ratelimiting, so does the 1799f3e7afe2SHans Petter Selasky * VLAN interface. 1800f3e7afe2SHans Petter Selasky */ 1801d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_TXRTLMT); 1802d89baa5aSAlexander Motin ena |= (mena & IFCAP_TXRTLMT); 1803f3e7afe2SHans Petter Selasky #endif 1804d89baa5aSAlexander Motin 180566d0c056SJohn Baldwin /* 180666d0c056SJohn Baldwin * If the parent interface supports unmapped mbufs, so does 180766d0c056SJohn Baldwin * the VLAN interface. Note that this should be fine even for 180866d0c056SJohn Baldwin * interfaces that don't support hardware tagging as headers 180966d0c056SJohn Baldwin * are prepended in normal mbufs to unmapped mbufs holding 181066d0c056SJohn Baldwin * payload data. 181166d0c056SJohn Baldwin */ 18123f43ada9SGleb Smirnoff cap |= (p->if_capabilities & IFCAP_MEXTPG); 18133f43ada9SGleb Smirnoff ena |= (mena & IFCAP_MEXTPG); 181466d0c056SJohn Baldwin 1815b2e60773SJohn Baldwin /* 1816b2e60773SJohn Baldwin * If the parent interface can offload encryption and segmentation 1817b2e60773SJohn Baldwin * of TLS records over TCP, propagate it's capability to the VLAN 1818b2e60773SJohn Baldwin * interface. 1819b2e60773SJohn Baldwin * 1820b2e60773SJohn Baldwin * All TLS drivers in the tree today can deal with VLANs. If 1821b2e60773SJohn Baldwin * this ever changes, then a new IFCAP_VLAN_TXTLS can be 1822b2e60773SJohn Baldwin * defined. 1823b2e60773SJohn Baldwin */ 1824521eac97SJohn Baldwin if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 1825521eac97SJohn Baldwin cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 1826521eac97SJohn Baldwin if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 1827521eac97SJohn Baldwin ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 1828b2e60773SJohn Baldwin 1829d89baa5aSAlexander Motin ifp->if_capabilities = cap; 1830d89baa5aSAlexander Motin ifp->if_capenable = ena; 1831d89baa5aSAlexander Motin ifp->if_hwassist = hwa; 183275ee267cSGleb Smirnoff } 183375ee267cSGleb Smirnoff 183475ee267cSGleb Smirnoff static void 183575ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 183675ee267cSGleb Smirnoff { 1837b2807792SGleb Smirnoff struct epoch_tracker et; 1838d148c2a2SMatt Joras struct ifvlantrunk *trunk; 183975ee267cSGleb Smirnoff struct ifvlan *ifv; 184075ee267cSGleb Smirnoff 1841d148c2a2SMatt Joras VLAN_SLOCK(); 1842d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1843d148c2a2SMatt Joras if (trunk == NULL) { 1844d148c2a2SMatt Joras VLAN_SUNLOCK(); 1845d148c2a2SMatt Joras return; 1846d148c2a2SMatt Joras } 1847b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 1848b8a6e03fSGleb Smirnoff VLAN_FOREACH(ifv, trunk) 184975ee267cSGleb Smirnoff vlan_capabilities(ifv); 1850b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 1851d148c2a2SMatt Joras VLAN_SUNLOCK(); 1852127d7b2dSAndre Oppermann } 1853127d7b2dSAndre Oppermann 1854a3814acfSSam Leffler static int 1855cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 18562cc2df49SGarrett Wollman { 18572cc2df49SGarrett Wollman struct ifnet *p; 18582cc2df49SGarrett Wollman struct ifreq *ifr; 1859e4cd31ddSJeff Roberson struct ifaddr *ifa; 18602cc2df49SGarrett Wollman struct ifvlan *ifv; 18612d222cb7SAlexander Motin struct ifvlantrunk *trunk; 18622cc2df49SGarrett Wollman struct vlanreq vlr; 186384becee1SAlexander Motin int error = 0, oldmtu; 18642cc2df49SGarrett Wollman 18652cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 1866e4cd31ddSJeff Roberson ifa = (struct ifaddr *) data; 18672cc2df49SGarrett Wollman ifv = ifp->if_softc; 18682cc2df49SGarrett Wollman 18692cc2df49SGarrett Wollman switch (cmd) { 1870e4cd31ddSJeff Roberson case SIOCSIFADDR: 1871e4cd31ddSJeff Roberson ifp->if_flags |= IFF_UP; 1872e4cd31ddSJeff Roberson #ifdef INET 1873e4cd31ddSJeff Roberson if (ifa->ifa_addr->sa_family == AF_INET) 1874e4cd31ddSJeff Roberson arp_ifinit(ifp, ifa); 1875e4cd31ddSJeff Roberson #endif 1876e4cd31ddSJeff Roberson break; 1877e4cd31ddSJeff Roberson case SIOCGIFADDR: 187838d958a6SBrooks Davis bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 187938d958a6SBrooks Davis ifp->if_addrlen); 1880e4cd31ddSJeff Roberson break; 1881b3cca108SBill Fenner case SIOCGIFMEDIA: 1882d148c2a2SMatt Joras VLAN_SLOCK(); 188375ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 1884d8564efdSEd Maste p = PARENT(ifv); 18859bcf3ae4SAlexander Motin if_ref(p); 1886d8564efdSEd Maste error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 18879bcf3ae4SAlexander Motin if_rele(p); 1888b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 1889b3cca108SBill Fenner if (error == 0) { 1890b3cca108SBill Fenner struct ifmediareq *ifmr; 1891b3cca108SBill Fenner 1892b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 1893b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1894b3cca108SBill Fenner ifmr->ifm_count = 1; 1895b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 1896b3cca108SBill Fenner ifmr->ifm_ulist, 1897b3cca108SBill Fenner sizeof(int)); 1898b3cca108SBill Fenner } 1899b3cca108SBill Fenner } 19004faedfe8SSam Leffler } else { 1901b3cca108SBill Fenner error = EINVAL; 19024faedfe8SSam Leffler } 1903d148c2a2SMatt Joras VLAN_SUNLOCK(); 1904b3cca108SBill Fenner break; 1905b3cca108SBill Fenner 1906b3cca108SBill Fenner case SIOCSIFMEDIA: 1907b3cca108SBill Fenner error = EINVAL; 1908b3cca108SBill Fenner break; 1909b3cca108SBill Fenner 19102cc2df49SGarrett Wollman case SIOCSIFMTU: 19112cc2df49SGarrett Wollman /* 19122cc2df49SGarrett Wollman * Set the interface MTU. 19132cc2df49SGarrett Wollman */ 1914d148c2a2SMatt Joras VLAN_SLOCK(); 1915d148c2a2SMatt Joras trunk = TRUNK(ifv); 1916d148c2a2SMatt Joras if (trunk != NULL) { 1917d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 1918a3814acfSSam Leffler if (ifr->ifr_mtu > 191975ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1920a3814acfSSam Leffler ifr->ifr_mtu < 1921a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 19222cc2df49SGarrett Wollman error = EINVAL; 1923a3814acfSSam Leffler else 19242cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 1925d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 1926a3814acfSSam Leffler } else 1927a3814acfSSam Leffler error = EINVAL; 1928d148c2a2SMatt Joras VLAN_SUNLOCK(); 19292cc2df49SGarrett Wollman break; 19302cc2df49SGarrett Wollman 19312cc2df49SGarrett Wollman case SIOCSETVLAN: 1932ccf7ba97SMarko Zec #ifdef VIMAGE 193315f6780eSRobert Watson /* 193415f6780eSRobert Watson * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN 193515f6780eSRobert Watson * interface to be delegated to a jail without allowing the 193615f6780eSRobert Watson * jail to change what underlying interface/VID it is 193715f6780eSRobert Watson * associated with. We are not entirely convinced that this 19385a39f779SRobert Watson * is the right way to accomplish that policy goal. 193915f6780eSRobert Watson */ 1940ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 1941ccf7ba97SMarko Zec error = EPERM; 1942ccf7ba97SMarko Zec break; 1943ccf7ba97SMarko Zec } 1944ccf7ba97SMarko Zec #endif 1945541d96aaSBrooks Davis error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr)); 19462cc2df49SGarrett Wollman if (error) 19472cc2df49SGarrett Wollman break; 19482cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 1949f731f104SBill Paul vlan_unconfig(ifp); 19502cc2df49SGarrett Wollman break; 19512cc2df49SGarrett Wollman } 19529bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 19531bdc73d3SEd Maste if (p == NULL) { 19542cc2df49SGarrett Wollman error = ENOENT; 19552cc2df49SGarrett Wollman break; 19562cc2df49SGarrett Wollman } 1957afbb64f1SAlexander V. Chernikov #ifdef COMPAT_FREEBSD12 1958afbb64f1SAlexander V. Chernikov if (vlr.vlr_proto == 0) 1959afbb64f1SAlexander V. Chernikov vlr.vlr_proto = ETHERTYPE_VLAN; 1960afbb64f1SAlexander V. Chernikov #endif 196184becee1SAlexander Motin oldmtu = ifp->if_mtu; 1962c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto); 19639bcf3ae4SAlexander Motin if_rele(p); 196484becee1SAlexander Motin 196584becee1SAlexander Motin /* 196684becee1SAlexander Motin * VLAN MTU may change during addition of the vlandev. 196784becee1SAlexander Motin * If it did, do network layer specific procedure. 196884becee1SAlexander Motin */ 196984becee1SAlexander Motin if (ifp->if_mtu != oldmtu) { 197084becee1SAlexander Motin #ifdef INET6 197184becee1SAlexander Motin nd6_setmtu(ifp); 197284becee1SAlexander Motin #endif 197384becee1SAlexander Motin rt_updatemtu(ifp); 197484becee1SAlexander Motin } 19752cc2df49SGarrett Wollman break; 19762cc2df49SGarrett Wollman 19772cc2df49SGarrett Wollman case SIOCGETVLAN: 1978ccf7ba97SMarko Zec #ifdef VIMAGE 1979ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 1980ccf7ba97SMarko Zec error = EPERM; 1981ccf7ba97SMarko Zec break; 1982ccf7ba97SMarko Zec } 1983ccf7ba97SMarko Zec #endif 198415a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 1985d148c2a2SMatt Joras VLAN_SLOCK(); 198675ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 198775ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 19889bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 19897983103aSRobert Watson vlr.vlr_tag = ifv->ifv_vid; 1990c7cffd65SAlexander V. Chernikov vlr.vlr_proto = ifv->ifv_proto; 19912cc2df49SGarrett Wollman } 1992d148c2a2SMatt Joras VLAN_SUNLOCK(); 1993541d96aaSBrooks Davis error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr)); 19942cc2df49SGarrett Wollman break; 19952cc2df49SGarrett Wollman 19962cc2df49SGarrett Wollman case SIOCSIFFLAGS: 19972cc2df49SGarrett Wollman /* 19981cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 19991cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 20002cc2df49SGarrett Wollman */ 2001d148c2a2SMatt Joras VLAN_XLOCK(); 200275ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 20031cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 2004d148c2a2SMatt Joras VLAN_XUNLOCK(); 20052cc2df49SGarrett Wollman break; 2006a3814acfSSam Leffler 2007f731f104SBill Paul case SIOCADDMULTI: 2008f731f104SBill Paul case SIOCDELMULTI: 200975ee267cSGleb Smirnoff /* 201075ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 201175ee267cSGleb Smirnoff * when we do. 2012d148c2a2SMatt Joras * 2013d148c2a2SMatt Joras * XXX We need the rmlock here to avoid sleeping while 2014d148c2a2SMatt Joras * holding in6_multi_mtx. 201575ee267cSGleb Smirnoff */ 2016b08d611dSMatt Macy VLAN_XLOCK(); 20172d222cb7SAlexander Motin trunk = TRUNK(ifv); 2018b08d611dSMatt Macy if (trunk != NULL) 2019f731f104SBill Paul error = vlan_setmulti(ifp); 2020b08d611dSMatt Macy VLAN_XUNLOCK(); 202175ee267cSGleb Smirnoff 2022b08d611dSMatt Macy break; 20232ccbbd06SMarcelo Araujo case SIOCGVLANPCP: 20242ccbbd06SMarcelo Araujo #ifdef VIMAGE 20252ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 20262ccbbd06SMarcelo Araujo error = EPERM; 20272ccbbd06SMarcelo Araujo break; 20282ccbbd06SMarcelo Araujo } 20292ccbbd06SMarcelo Araujo #endif 20302ccbbd06SMarcelo Araujo ifr->ifr_vlan_pcp = ifv->ifv_pcp; 20312ccbbd06SMarcelo Araujo break; 20322ccbbd06SMarcelo Araujo 20332ccbbd06SMarcelo Araujo case SIOCSVLANPCP: 20342ccbbd06SMarcelo Araujo #ifdef VIMAGE 20352ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 20362ccbbd06SMarcelo Araujo error = EPERM; 20372ccbbd06SMarcelo Araujo break; 20382ccbbd06SMarcelo Araujo } 20392ccbbd06SMarcelo Araujo #endif 20402ccbbd06SMarcelo Araujo error = priv_check(curthread, PRIV_NET_SETVLANPCP); 20412ccbbd06SMarcelo Araujo if (error) 20422ccbbd06SMarcelo Araujo break; 2043*9ef8cd0bSKristof Provost if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) { 20442ccbbd06SMarcelo Araujo error = EINVAL; 20452ccbbd06SMarcelo Araujo break; 20462ccbbd06SMarcelo Araujo } 20472ccbbd06SMarcelo Araujo ifv->ifv_pcp = ifr->ifr_vlan_pcp; 204832a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 20494a381a9eSHans Petter Selasky /* broadcast event about PCP change */ 20504a381a9eSHans Petter Selasky EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 20512ccbbd06SMarcelo Araujo break; 20522ccbbd06SMarcelo Araujo 2053d89baa5aSAlexander Motin case SIOCSIFCAP: 2054d148c2a2SMatt Joras VLAN_SLOCK(); 2055d89baa5aSAlexander Motin ifv->ifv_capenable = ifr->ifr_reqcap; 2056d89baa5aSAlexander Motin trunk = TRUNK(ifv); 20576dcec895SGleb Smirnoff if (trunk != NULL) { 20586dcec895SGleb Smirnoff struct epoch_tracker et; 20596dcec895SGleb Smirnoff 20606dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 2061d89baa5aSAlexander Motin vlan_capabilities(ifv); 20626dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 20636dcec895SGleb Smirnoff } 2064d148c2a2SMatt Joras VLAN_SUNLOCK(); 2065d89baa5aSAlexander Motin break; 2066d89baa5aSAlexander Motin 20672cc2df49SGarrett Wollman default: 2068e4cd31ddSJeff Roberson error = EINVAL; 2069e4cd31ddSJeff Roberson break; 20702cc2df49SGarrett Wollman } 207115a66c21SBruce M Simpson 207215a66c21SBruce M Simpson return (error); 20732cc2df49SGarrett Wollman } 2074f3e7afe2SHans Petter Selasky 2075b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 2076f3e7afe2SHans Petter Selasky static int 2077f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp, 2078f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *params, 2079f3e7afe2SHans Petter Selasky struct m_snd_tag **ppmt) 2080f3e7afe2SHans Petter Selasky { 2081fb3bc596SJohn Baldwin struct epoch_tracker et; 2082fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2083fb3bc596SJohn Baldwin struct ifvlan *ifv; 2084fb3bc596SJohn Baldwin struct ifnet *parent; 2085fb3bc596SJohn Baldwin int error; 2086f3e7afe2SHans Petter Selasky 2087fb3bc596SJohn Baldwin NET_EPOCH_ENTER(et); 2088fb3bc596SJohn Baldwin ifv = ifp->if_softc; 2089fb3bc596SJohn Baldwin if (ifv->ifv_trunk != NULL) 2090fb3bc596SJohn Baldwin parent = PARENT(ifv); 2091fb3bc596SJohn Baldwin else 2092fb3bc596SJohn Baldwin parent = NULL; 209336e0a362SJohn Baldwin if (parent == NULL) { 2094fb3bc596SJohn Baldwin NET_EPOCH_EXIT(et); 2095f3e7afe2SHans Petter Selasky return (EOPNOTSUPP); 2096fb3bc596SJohn Baldwin } 2097fb3bc596SJohn Baldwin if_ref(parent); 2098fb3bc596SJohn Baldwin NET_EPOCH_EXIT(et); 2099fb3bc596SJohn Baldwin 2100fb3bc596SJohn Baldwin vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT); 2101fb3bc596SJohn Baldwin if (vst == NULL) { 2102fb3bc596SJohn Baldwin if_rele(parent); 2103fb3bc596SJohn Baldwin return (ENOMEM); 2104fb3bc596SJohn Baldwin } 2105fb3bc596SJohn Baldwin 210636e0a362SJohn Baldwin error = m_snd_tag_alloc(parent, params, &vst->tag); 2107fb3bc596SJohn Baldwin if_rele(parent); 2108fb3bc596SJohn Baldwin if (error) { 2109fb3bc596SJohn Baldwin free(vst, M_VLAN); 2110fb3bc596SJohn Baldwin return (error); 2111fb3bc596SJohn Baldwin } 2112fb3bc596SJohn Baldwin 211356fb710fSJohn Baldwin m_snd_tag_init(&vst->com, ifp, vst->tag->type); 2114fb3bc596SJohn Baldwin 2115fb3bc596SJohn Baldwin *ppmt = &vst->com; 2116fb3bc596SJohn Baldwin return (0); 2117fb3bc596SJohn Baldwin } 2118fb3bc596SJohn Baldwin 21191a714ff2SRandall Stewart static struct m_snd_tag * 21201a714ff2SRandall Stewart vlan_next_snd_tag(struct m_snd_tag *mst) 21211a714ff2SRandall Stewart { 21221a714ff2SRandall Stewart struct vlan_snd_tag *vst; 21231a714ff2SRandall Stewart 21241a714ff2SRandall Stewart vst = mst_to_vst(mst); 21251a714ff2SRandall Stewart return (vst->tag); 21261a714ff2SRandall Stewart } 21271a714ff2SRandall Stewart 2128fb3bc596SJohn Baldwin static int 2129fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst, 2130fb3bc596SJohn Baldwin union if_snd_tag_modify_params *params) 2131fb3bc596SJohn Baldwin { 2132fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2133fb3bc596SJohn Baldwin 2134fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2135fb3bc596SJohn Baldwin return (vst->tag->ifp->if_snd_tag_modify(vst->tag, params)); 2136fb3bc596SJohn Baldwin } 2137fb3bc596SJohn Baldwin 2138fb3bc596SJohn Baldwin static int 2139fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst, 2140fb3bc596SJohn Baldwin union if_snd_tag_query_params *params) 2141fb3bc596SJohn Baldwin { 2142fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2143fb3bc596SJohn Baldwin 2144fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2145fb3bc596SJohn Baldwin return (vst->tag->ifp->if_snd_tag_query(vst->tag, params)); 2146f3e7afe2SHans Petter Selasky } 2147fa91f845SRandall Stewart 2148fa91f845SRandall Stewart static void 2149fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst) 2150fa91f845SRandall Stewart { 2151fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2152fb3bc596SJohn Baldwin 2153fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2154fb3bc596SJohn Baldwin m_snd_tag_rele(vst->tag); 2155fb3bc596SJohn Baldwin free(vst, M_VLAN); 2156fa91f845SRandall Stewart } 21571a714ff2SRandall Stewart 21581a714ff2SRandall Stewart static void 21591a714ff2SRandall Stewart vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q) 21601a714ff2SRandall Stewart { 21611a714ff2SRandall Stewart /* 21621a714ff2SRandall Stewart * For vlan, we have an indirect 21631a714ff2SRandall Stewart * interface. The caller needs to 21641a714ff2SRandall Stewart * get a ratelimit tag on the actual 21651a714ff2SRandall Stewart * interface the flow will go on. 21661a714ff2SRandall Stewart */ 21671a714ff2SRandall Stewart q->rate_table = NULL; 21681a714ff2SRandall Stewart q->flags = RT_IS_INDIRECT; 21691a714ff2SRandall Stewart q->max_flows = 0; 21701a714ff2SRandall Stewart q->number_of_rates = 0; 21711a714ff2SRandall Stewart } 21721a714ff2SRandall Stewart 2173f3e7afe2SHans Petter Selasky #endif 2174