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> 752c2b37adSJustin Hibbits #include <net/if_private.h> 76f889d2efSBrooks Davis #include <net/if_clone.h> 772cc2df49SGarrett Wollman #include <net/if_dl.h> 782cc2df49SGarrett Wollman #include <net/if_types.h> 792cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 8084becee1SAlexander Motin #include <net/route.h> 814b79449eSBjoern A. Zeeb #include <net/vnet.h> 822cc2df49SGarrett Wollman 832c5b403eSOleg Bulyzhin #ifdef INET 842c5b403eSOleg Bulyzhin #include <netinet/in.h> 852c5b403eSOleg Bulyzhin #include <netinet/if_ether.h> 862c5b403eSOleg Bulyzhin #endif 872c5b403eSOleg Bulyzhin 8884becee1SAlexander Motin #ifdef INET6 8984becee1SAlexander Motin /* 9084becee1SAlexander Motin * XXX: declare here to avoid to include many inet6 related files.. 9184becee1SAlexander Motin * should be more generalized? 9284becee1SAlexander Motin */ 9384becee1SAlexander Motin extern void nd6_setmtu(struct ifnet *); 9484becee1SAlexander Motin #endif 9584becee1SAlexander Motin 9675ee267cSGleb Smirnoff #define VLAN_DEF_HWIDTH 4 9764a17d2eSYaroslav Tykhiy #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 9875ee267cSGleb Smirnoff 992dc879b3SYaroslav Tykhiy #define UP_AND_RUNNING(ifp) \ 1002dc879b3SYaroslav Tykhiy ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 1012dc879b3SYaroslav Tykhiy 102b08d611dSMatt Macy CK_SLIST_HEAD(ifvlanhead, ifvlan); 10375ee267cSGleb Smirnoff 10475ee267cSGleb Smirnoff struct ifvlantrunk { 10575ee267cSGleb Smirnoff struct ifnet *parent; /* parent interface of this trunk */ 106b08d611dSMatt Macy struct mtx lock; 10775ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 1085cb8c31aSYaroslav Tykhiy #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 1095cb8c31aSYaroslav Tykhiy struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 11075ee267cSGleb Smirnoff #else 11175ee267cSGleb Smirnoff struct ifvlanhead *hash; /* dynamic hash-list table */ 11275ee267cSGleb Smirnoff uint16_t hmask; 11375ee267cSGleb Smirnoff uint16_t hwidth; 11475ee267cSGleb Smirnoff #endif 11575ee267cSGleb Smirnoff int refcnt; 11675ee267cSGleb Smirnoff }; 1179d4fe4b2SBrooks Davis 118b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 119fb3bc596SJohn Baldwin struct vlan_snd_tag { 120fb3bc596SJohn Baldwin struct m_snd_tag com; 121fb3bc596SJohn Baldwin struct m_snd_tag *tag; 122fb3bc596SJohn Baldwin }; 123fb3bc596SJohn Baldwin 124fb3bc596SJohn Baldwin static inline struct vlan_snd_tag * 125fb3bc596SJohn Baldwin mst_to_vst(struct m_snd_tag *mst) 126fb3bc596SJohn Baldwin { 127fb3bc596SJohn Baldwin 128fb3bc596SJohn Baldwin return (__containerof(mst, struct vlan_snd_tag, com)); 129fb3bc596SJohn Baldwin } 130fb3bc596SJohn Baldwin #endif 131fb3bc596SJohn Baldwin 132d148c2a2SMatt Joras /* 133d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk with 134d148c2a2SMatt Joras * the assumption that none will be added/removed during iteration. 135d148c2a2SMatt Joras */ 136d148c2a2SMatt Joras #ifdef VLAN_ARRAY 137d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 138d148c2a2SMatt Joras size_t _i; \ 139d148c2a2SMatt Joras for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \ 140d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i]) != NULL) 141d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 142d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 143d148c2a2SMatt Joras struct ifvlan *_next; \ 144d148c2a2SMatt Joras size_t _i; \ 145d148c2a2SMatt Joras for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \ 146b08d611dSMatt Macy CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next) 147d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 148d148c2a2SMatt Joras 149d148c2a2SMatt Joras /* 150d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk while 151d148c2a2SMatt Joras * also modifying the number of vlans on the trunk. The iteration continues 152d148c2a2SMatt Joras * until some condition is met or there are no more vlans on the trunk. 153d148c2a2SMatt Joras */ 154d148c2a2SMatt Joras #ifdef VLAN_ARRAY 155d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */ 156d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 157d148c2a2SMatt Joras size_t _i; \ 158d148c2a2SMatt Joras for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \ 159d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i])) 160d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 161d148c2a2SMatt Joras /* 162d148c2a2SMatt Joras * The hash table case is more complicated. We allow for the hash table to be 163d148c2a2SMatt Joras * modified (i.e. vlans removed) while we are iterating over it. To allow for 164d148c2a2SMatt Joras * this we must restart the iteration every time we "touch" something during 165d148c2a2SMatt Joras * the iteration, since removal will resize the hash table and invalidate our 166d148c2a2SMatt Joras * current position. If acting on the touched element causes the trunk to be 167d148c2a2SMatt Joras * emptied, then iteration also stops. 168d148c2a2SMatt Joras */ 169d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 170d148c2a2SMatt Joras size_t _i; \ 171d148c2a2SMatt Joras bool _touch = false; \ 172d148c2a2SMatt Joras for (_i = 0; \ 173d148c2a2SMatt Joras !(_cond) && _i < (1 << (_trunk)->hwidth); \ 174d148c2a2SMatt Joras _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \ 175b08d611dSMatt Macy if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \ 176d148c2a2SMatt Joras (_touch = true)) 177d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 178d148c2a2SMatt Joras 179a3814acfSSam Leffler struct vlan_mc_entry { 180e4cd31ddSJeff Roberson struct sockaddr_dl mc_addr; 181b08d611dSMatt Macy CK_SLIST_ENTRY(vlan_mc_entry) mc_entries; 182c32a9d66SHans Petter Selasky struct epoch_context mc_epoch_ctx; 183a3814acfSSam Leffler }; 184a3814acfSSam Leffler 185a3814acfSSam Leffler struct ifvlan { 18675ee267cSGleb Smirnoff struct ifvlantrunk *ifv_trunk; 187fc74a9f9SBrooks Davis struct ifnet *ifv_ifp; 18875ee267cSGleb Smirnoff #define TRUNK(ifv) ((ifv)->ifv_trunk) 189c7cffd65SAlexander V. Chernikov #define PARENT(ifv) (TRUNK(ifv)->parent) 1906667db31SAlexander V. Chernikov void *ifv_cookie; 1911cf236fbSYaroslav Tykhiy int ifv_pflags; /* special flags we have set on parent */ 192d89baa5aSAlexander Motin int ifv_capenable; 19372755d28SMark Johnston int ifv_encaplen; /* encapsulation length */ 19472755d28SMark Johnston int ifv_mtufudge; /* MTU fudged by this much */ 19572755d28SMark Johnston int ifv_mintu; /* min transmission unit */ 196c7cffd65SAlexander V. Chernikov struct ether_8021q_tag ifv_qtag; 197c7cffd65SAlexander V. Chernikov #define ifv_proto ifv_qtag.proto 198c7cffd65SAlexander V. Chernikov #define ifv_vid ifv_qtag.vid 199c7cffd65SAlexander V. Chernikov #define ifv_pcp ifv_qtag.pcp 200d148c2a2SMatt Joras struct task lladdr_task; 201b08d611dSMatt Macy CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 202c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY 203b08d611dSMatt Macy CK_SLIST_ENTRY(ifvlan) ifv_list; 204c0cb022bSYaroslav Tykhiy #endif 205a3814acfSSam Leffler }; 206a3814acfSSam Leffler 20775ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */ 2081cf236fbSYaroslav Tykhiy static struct { 2091cf236fbSYaroslav Tykhiy int flag; 2101cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 2111cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 2121cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 2131cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 2141cf236fbSYaroslav Tykhiy {0, NULL} 2151cf236fbSYaroslav Tykhiy }; 216a3814acfSSam Leffler 21778bc3d5eSKristof Provost VNET_DECLARE(int, vlan_mtag_pcp); 21878bc3d5eSKristof Provost #define V_vlan_mtag_pcp VNET(vlan_mtag_pcp) 2192ccbbd06SMarcelo Araujo 22042a58907SGleb Smirnoff static const char vlanname[] = "vlan"; 22142a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface"); 2222cc2df49SGarrett Wollman 2235cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag; 224ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag; 225f2ab9160SAndrey V. Elsukov static eventhandler_tag ifevent_tag; 2265cb8c31aSYaroslav Tykhiy 2274faedfe8SSam Leffler /* 228b08d611dSMatt Macy * if_vlan uses two module-level synchronizations primitives to allow concurrent 229b08d611dSMatt Macy * modification of vlan interfaces and (mostly) allow for vlans to be destroyed 230b08d611dSMatt Macy * while they are being used for tx/rx. To accomplish this in a way that has 231b08d611dSMatt Macy * acceptable performance and cooperation with other parts of the network stack 232b08d611dSMatt Macy * there is a non-sleepable epoch(9) and an sx(9). 23375ee267cSGleb Smirnoff * 234b08d611dSMatt Macy * The performance-sensitive paths that warrant using the epoch(9) are 235d148c2a2SMatt Joras * vlan_transmit and vlan_input. Both have to check for the vlan interface's 236d148c2a2SMatt Joras * existence using if_vlantrunk, and being in the network tx/rx paths the use 237b08d611dSMatt Macy * of an epoch(9) gives a measureable improvement in performance. 23875ee267cSGleb Smirnoff * 239d148c2a2SMatt Joras * The reason for having an sx(9) is mostly because there are still areas that 240d148c2a2SMatt Joras * must be sleepable and also have safe concurrent access to a vlan interface. 241d148c2a2SMatt Joras * Since the sx(9) exists, it is used by default in most paths unless sleeping 242d148c2a2SMatt Joras * is not permitted, or if it is not clear whether sleeping is permitted. 243d148c2a2SMatt Joras * 2444faedfe8SSam Leffler */ 245d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx 246d148c2a2SMatt Joras 247d148c2a2SMatt Joras static struct sx _VLAN_SX_ID; 248d148c2a2SMatt Joras 249d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \ 250c7cffd65SAlexander V. Chernikov sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE) 251d148c2a2SMatt Joras 252d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \ 253d148c2a2SMatt Joras sx_destroy(&_VLAN_SX_ID) 254d148c2a2SMatt Joras 255d148c2a2SMatt Joras #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID) 256d148c2a2SMatt Joras #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID) 257d148c2a2SMatt Joras #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID) 258d148c2a2SMatt Joras #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID) 259d148c2a2SMatt Joras #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED) 260d148c2a2SMatt Joras #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED) 261d148c2a2SMatt Joras #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED) 262d148c2a2SMatt Joras 263d148c2a2SMatt Joras /* 264b08d611dSMatt Macy * We also have a per-trunk mutex that should be acquired when changing 265b08d611dSMatt Macy * its state. 266d148c2a2SMatt Joras */ 267b08d611dSMatt Macy #define TRUNK_LOCK_INIT(trunk) mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF) 268b08d611dSMatt Macy #define TRUNK_LOCK_DESTROY(trunk) mtx_destroy(&(trunk)->lock) 269b08d611dSMatt Macy #define TRUNK_WLOCK(trunk) mtx_lock(&(trunk)->lock) 270b08d611dSMatt Macy #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock) 271b08d611dSMatt Macy #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED); 27275ee267cSGleb Smirnoff 273d148c2a2SMatt Joras /* 274d148c2a2SMatt Joras * The VLAN_ARRAY substitutes the dynamic hash with a static array 275d148c2a2SMatt Joras * with 4096 entries. In theory this can give a boost in processing, 276d148c2a2SMatt Joras * however in practice it does not. Probably this is because the array 277d148c2a2SMatt Joras * is too big to fit into CPU cache. 278d148c2a2SMatt Joras */ 27975ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 28075ee267cSGleb Smirnoff static void vlan_inithash(struct ifvlantrunk *trunk); 28175ee267cSGleb Smirnoff static void vlan_freehash(struct ifvlantrunk *trunk); 28275ee267cSGleb Smirnoff static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 28375ee267cSGleb Smirnoff static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 28475ee267cSGleb Smirnoff static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 28575ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 2867983103aSRobert Watson uint16_t vid); 28775ee267cSGleb Smirnoff #endif 28875ee267cSGleb Smirnoff static void trunk_destroy(struct ifvlantrunk *trunk); 2894faedfe8SSam Leffler 290114c608cSYaroslav Tykhiy static void vlan_init(void *foo); 291a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 292cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 293b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 294f3e7afe2SHans Petter Selasky static int vlan_snd_tag_alloc(struct ifnet *, 295f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *, struct m_snd_tag **); 296fb3bc596SJohn Baldwin static int vlan_snd_tag_modify(struct m_snd_tag *, 297fb3bc596SJohn Baldwin union if_snd_tag_modify_params *); 298fb3bc596SJohn Baldwin static int vlan_snd_tag_query(struct m_snd_tag *, 299fb3bc596SJohn Baldwin union if_snd_tag_query_params *); 300fa91f845SRandall Stewart static void vlan_snd_tag_free(struct m_snd_tag *); 3011a714ff2SRandall Stewart static struct m_snd_tag *vlan_next_snd_tag(struct m_snd_tag *); 3021a714ff2SRandall Stewart static void vlan_ratelimit_query(struct ifnet *, 3031a714ff2SRandall Stewart struct if_ratelimit_query_results *); 304f3e7afe2SHans Petter Selasky #endif 305d9b1d615SJohn Baldwin static void vlan_qflush(struct ifnet *ifp); 3061cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 3071cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 3081cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 309f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 310d9b1d615SJohn Baldwin static int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 3112e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 3122e5ff01dSLuiz Otavio O Souza static void vlan_altq_start(struct ifnet *ifp); 3132e5ff01dSLuiz Otavio O Souza static int vlan_altq_transmit(struct ifnet *ifp, struct mbuf *m); 3142e5ff01dSLuiz Otavio O Souza #endif 31516cf6bdbSMatt Joras static int vlan_output(struct ifnet *ifp, struct mbuf *m, 31616cf6bdbSMatt Joras const struct sockaddr *dst, struct route *ro); 3176f359e28SJohn Baldwin static void vlan_unconfig(struct ifnet *ifp); 31828cc4d37SJohn Baldwin static void vlan_unconfig_locked(struct ifnet *ifp, int departing); 319c7cffd65SAlexander V. Chernikov static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag, 320c7cffd65SAlexander V. Chernikov uint16_t proto); 321a6fffd6cSBrooks Davis static void vlan_link_state(struct ifnet *ifp); 32275ee267cSGleb Smirnoff static void vlan_capabilities(struct ifvlan *ifv); 32375ee267cSGleb Smirnoff static void vlan_trunk_capabilities(struct ifnet *ifp); 324f731f104SBill Paul 325f941c31aSGleb Smirnoff static struct ifnet *vlan_clone_match_ethervid(const char *, int *); 326f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 32791ebcbe0SAlexander V. Chernikov static int vlan_clone_create(struct if_clone *, char *, size_t, 32891ebcbe0SAlexander V. Chernikov struct ifc_data *, struct ifnet **); 32991ebcbe0SAlexander V. Chernikov static int vlan_clone_destroy(struct if_clone *, struct ifnet *, uint32_t); 330f889d2efSBrooks Davis 3315cb8c31aSYaroslav Tykhiy static void vlan_ifdetach(void *arg, struct ifnet *ifp); 332ea4ca115SAndrew Thompson static void vlan_iflladdr(void *arg, struct ifnet *ifp); 333f2ab9160SAndrey V. Elsukov static void vlan_ifevent(void *arg, struct ifnet *ifp, int event); 3345cb8c31aSYaroslav Tykhiy 335d148c2a2SMatt Joras static void vlan_lladdr_fn(void *arg, int pending); 336d148c2a2SMatt Joras 33742a58907SGleb Smirnoff static struct if_clone *vlan_cloner; 3389d4fe4b2SBrooks Davis 339ccf7ba97SMarko Zec #ifdef VIMAGE 3405f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner); 341ccf7ba97SMarko Zec #define V_vlan_cloner VNET(vlan_cloner) 342ccf7ba97SMarko Zec #endif 343ccf7ba97SMarko Zec 344c782ea8bSJohn Baldwin #ifdef RATELIMIT 345c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_ul_sw = { 346c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 347c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 348c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 349c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 350c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_UNLIMITED 351c782ea8bSJohn Baldwin }; 352c782ea8bSJohn Baldwin 353c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_rl_sw = { 354c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 355c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 356c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 357c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 358c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_RATE_LIMIT 359c782ea8bSJohn Baldwin }; 360c782ea8bSJohn Baldwin #endif 361c782ea8bSJohn Baldwin 362c782ea8bSJohn Baldwin #ifdef KERN_TLS 363c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_tls_sw = { 364c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 365c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 366c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 367c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 368c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_TLS 369c782ea8bSJohn Baldwin }; 370c782ea8bSJohn Baldwin 371c782ea8bSJohn Baldwin #ifdef RATELIMIT 372c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_tls_rl_sw = { 373c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 374c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 375c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 376c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 377c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT 378c782ea8bSJohn Baldwin }; 379c782ea8bSJohn Baldwin #endif 380c782ea8bSJohn Baldwin #endif 381c782ea8bSJohn Baldwin 38275ee267cSGleb Smirnoff static void 383c32a9d66SHans Petter Selasky vlan_mc_free(struct epoch_context *ctx) 384c32a9d66SHans Petter Selasky { 385c32a9d66SHans Petter Selasky struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx); 386c32a9d66SHans Petter Selasky free(mc, M_VLAN); 387c32a9d66SHans Petter Selasky } 388c32a9d66SHans Petter Selasky 389cac30248SOleg Bulyzhin #ifndef VLAN_ARRAY 390cac30248SOleg Bulyzhin #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 391cac30248SOleg Bulyzhin 392c32a9d66SHans Petter Selasky static void 39375ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk) 39475ee267cSGleb Smirnoff { 39575ee267cSGleb Smirnoff int i, n; 39675ee267cSGleb Smirnoff 39775ee267cSGleb Smirnoff /* 39875ee267cSGleb Smirnoff * The trunk must not be locked here since we call malloc(M_WAITOK). 39975ee267cSGleb Smirnoff * It is OK in case this function is called before the trunk struct 40075ee267cSGleb Smirnoff * gets hooked up and becomes visible from other threads. 40175ee267cSGleb Smirnoff */ 40275ee267cSGleb Smirnoff 40375ee267cSGleb Smirnoff KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 40475ee267cSGleb Smirnoff ("%s: hash already initialized", __func__)); 40575ee267cSGleb Smirnoff 40675ee267cSGleb Smirnoff trunk->hwidth = VLAN_DEF_HWIDTH; 40775ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 40875ee267cSGleb Smirnoff trunk->hmask = n - 1; 40975ee267cSGleb Smirnoff trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 41075ee267cSGleb Smirnoff for (i = 0; i < n; i++) 411b08d611dSMatt Macy CK_SLIST_INIT(&trunk->hash[i]); 41275ee267cSGleb Smirnoff } 41375ee267cSGleb Smirnoff 41475ee267cSGleb Smirnoff static void 41575ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk) 41675ee267cSGleb Smirnoff { 41775ee267cSGleb Smirnoff #ifdef INVARIANTS 41875ee267cSGleb Smirnoff int i; 41975ee267cSGleb Smirnoff 42075ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 42175ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 422b08d611dSMatt Macy KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]), 42375ee267cSGleb Smirnoff ("%s: hash table not empty", __func__)); 42475ee267cSGleb Smirnoff #endif 42575ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 42675ee267cSGleb Smirnoff trunk->hash = NULL; 42775ee267cSGleb Smirnoff trunk->hwidth = trunk->hmask = 0; 42875ee267cSGleb Smirnoff } 42975ee267cSGleb Smirnoff 43075ee267cSGleb Smirnoff static int 43175ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 43275ee267cSGleb Smirnoff { 43375ee267cSGleb Smirnoff int i, b; 43475ee267cSGleb Smirnoff struct ifvlan *ifv2; 43575ee267cSGleb Smirnoff 436b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 43775ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 43875ee267cSGleb Smirnoff 43975ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 4407983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 441b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 4427983103aSRobert Watson if (ifv->ifv_vid == ifv2->ifv_vid) 44375ee267cSGleb Smirnoff return (EEXIST); 44475ee267cSGleb Smirnoff 44575ee267cSGleb Smirnoff /* 44675ee267cSGleb Smirnoff * Grow the hash when the number of vlans exceeds half of the number of 44775ee267cSGleb Smirnoff * hash buckets squared. This will make the average linked-list length 44875ee267cSGleb Smirnoff * buckets/2. 44975ee267cSGleb Smirnoff */ 45075ee267cSGleb Smirnoff if (trunk->refcnt > (b * b) / 2) { 45175ee267cSGleb Smirnoff vlan_growhash(trunk, 1); 4527983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 45375ee267cSGleb Smirnoff } 454b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 45575ee267cSGleb Smirnoff trunk->refcnt++; 45675ee267cSGleb Smirnoff 45775ee267cSGleb Smirnoff return (0); 45875ee267cSGleb Smirnoff } 45975ee267cSGleb Smirnoff 46075ee267cSGleb Smirnoff static int 46175ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 46275ee267cSGleb Smirnoff { 46375ee267cSGleb Smirnoff int i, b; 46475ee267cSGleb Smirnoff struct ifvlan *ifv2; 46575ee267cSGleb Smirnoff 466b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 46775ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 46875ee267cSGleb Smirnoff 469151abc80SKristof Provost b = 1 << (trunk->hwidth - 1); 4707983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 471b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 47275ee267cSGleb Smirnoff if (ifv2 == ifv) { 47375ee267cSGleb Smirnoff trunk->refcnt--; 474b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list); 47575ee267cSGleb Smirnoff if (trunk->refcnt < (b * b) / 2) 47675ee267cSGleb Smirnoff vlan_growhash(trunk, -1); 47775ee267cSGleb Smirnoff return (0); 47875ee267cSGleb Smirnoff } 47975ee267cSGleb Smirnoff 48075ee267cSGleb Smirnoff panic("%s: vlan not found\n", __func__); 48175ee267cSGleb Smirnoff return (ENOENT); /*NOTREACHED*/ 48275ee267cSGleb Smirnoff } 48375ee267cSGleb Smirnoff 48475ee267cSGleb Smirnoff /* 48575ee267cSGleb Smirnoff * Grow the hash larger or smaller if memory permits. 48675ee267cSGleb Smirnoff */ 48775ee267cSGleb Smirnoff static void 48875ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 48975ee267cSGleb Smirnoff { 49075ee267cSGleb Smirnoff struct ifvlan *ifv; 49175ee267cSGleb Smirnoff struct ifvlanhead *hash2; 49275ee267cSGleb Smirnoff int hwidth2, i, j, n, n2; 49375ee267cSGleb Smirnoff 494b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 49575ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 49675ee267cSGleb Smirnoff 49775ee267cSGleb Smirnoff if (howmuch == 0) { 49875ee267cSGleb Smirnoff /* Harmless yet obvious coding error */ 49975ee267cSGleb Smirnoff printf("%s: howmuch is 0\n", __func__); 50075ee267cSGleb Smirnoff return; 50175ee267cSGleb Smirnoff } 50275ee267cSGleb Smirnoff 50375ee267cSGleb Smirnoff hwidth2 = trunk->hwidth + howmuch; 50475ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 50575ee267cSGleb Smirnoff n2 = 1 << hwidth2; 50675ee267cSGleb Smirnoff /* Do not shrink the table below the default */ 50775ee267cSGleb Smirnoff if (hwidth2 < VLAN_DEF_HWIDTH) 50875ee267cSGleb Smirnoff return; 50975ee267cSGleb Smirnoff 510b08d611dSMatt Macy hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK); 51175ee267cSGleb Smirnoff if (hash2 == NULL) { 51275ee267cSGleb Smirnoff printf("%s: out of memory -- hash size not changed\n", 51375ee267cSGleb Smirnoff __func__); 51475ee267cSGleb Smirnoff return; /* We can live with the old hash table */ 51575ee267cSGleb Smirnoff } 51675ee267cSGleb Smirnoff for (j = 0; j < n2; j++) 517b08d611dSMatt Macy CK_SLIST_INIT(&hash2[j]); 51875ee267cSGleb Smirnoff for (i = 0; i < n; i++) 519b08d611dSMatt Macy while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) { 520b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list); 5217983103aSRobert Watson j = HASH(ifv->ifv_vid, n2 - 1); 522b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 52375ee267cSGleb Smirnoff } 524b08d611dSMatt Macy NET_EPOCH_WAIT(); 52575ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 52675ee267cSGleb Smirnoff trunk->hash = hash2; 52775ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 52875ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 529f84b2d69SYaroslav Tykhiy 530f84b2d69SYaroslav Tykhiy if (bootverbose) 531f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 532f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 53375ee267cSGleb Smirnoff } 53475ee267cSGleb Smirnoff 53575ee267cSGleb Smirnoff static __inline struct ifvlan * 5367983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 53775ee267cSGleb Smirnoff { 53875ee267cSGleb Smirnoff struct ifvlan *ifv; 53975ee267cSGleb Smirnoff 540a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 54175ee267cSGleb Smirnoff 542b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list) 5437983103aSRobert Watson if (ifv->ifv_vid == vid) 54475ee267cSGleb Smirnoff return (ifv); 54575ee267cSGleb Smirnoff return (NULL); 54675ee267cSGleb Smirnoff } 54775ee267cSGleb Smirnoff 54875ee267cSGleb Smirnoff #if 0 54975ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 55075ee267cSGleb Smirnoff static void 55175ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 55275ee267cSGleb Smirnoff { 55375ee267cSGleb Smirnoff int i; 55475ee267cSGleb Smirnoff struct ifvlan *ifv; 55575ee267cSGleb Smirnoff 55675ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 55775ee267cSGleb Smirnoff printf("%d: ", i); 558b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 55975ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 56075ee267cSGleb Smirnoff printf("\n"); 56175ee267cSGleb Smirnoff } 56275ee267cSGleb Smirnoff } 56375ee267cSGleb Smirnoff #endif /* 0 */ 564e4cd31ddSJeff Roberson #else 565e4cd31ddSJeff Roberson 566e4cd31ddSJeff Roberson static __inline struct ifvlan * 5677983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 568e4cd31ddSJeff Roberson { 569e4cd31ddSJeff Roberson 5707983103aSRobert Watson return trunk->vlans[vid]; 571e4cd31ddSJeff Roberson } 572e4cd31ddSJeff Roberson 573e4cd31ddSJeff Roberson static __inline int 574e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 575e4cd31ddSJeff Roberson { 576e4cd31ddSJeff Roberson 5777983103aSRobert Watson if (trunk->vlans[ifv->ifv_vid] != NULL) 578e4cd31ddSJeff Roberson return EEXIST; 5797983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = ifv; 580e4cd31ddSJeff Roberson trunk->refcnt++; 581e4cd31ddSJeff Roberson 582e4cd31ddSJeff Roberson return (0); 583e4cd31ddSJeff Roberson } 584e4cd31ddSJeff Roberson 585e4cd31ddSJeff Roberson static __inline int 586e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 587e4cd31ddSJeff Roberson { 588e4cd31ddSJeff Roberson 5897983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = NULL; 590e4cd31ddSJeff Roberson trunk->refcnt--; 591e4cd31ddSJeff Roberson 592e4cd31ddSJeff Roberson return (0); 593e4cd31ddSJeff Roberson } 594e4cd31ddSJeff Roberson 595e4cd31ddSJeff Roberson static __inline void 596e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk) 597e4cd31ddSJeff Roberson { 598e4cd31ddSJeff Roberson } 599e4cd31ddSJeff Roberson 600e4cd31ddSJeff Roberson static __inline void 601e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk) 602e4cd31ddSJeff Roberson { 603e4cd31ddSJeff Roberson } 604e4cd31ddSJeff Roberson 60575ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 60675ee267cSGleb Smirnoff 60775ee267cSGleb Smirnoff static void 60875ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 60975ee267cSGleb Smirnoff { 610d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 61175ee267cSGleb Smirnoff 61275ee267cSGleb Smirnoff vlan_freehash(trunk); 61375ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 61433499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 6159bcf3ae4SAlexander Motin if_rele(trunk->parent); 61675ee267cSGleb Smirnoff free(trunk, M_VLAN); 61775ee267cSGleb Smirnoff } 61875ee267cSGleb Smirnoff 619f731f104SBill Paul /* 620f731f104SBill Paul * Program our multicast filter. What we're actually doing is 621f731f104SBill Paul * programming the multicast filter of the parent. This has the 622f731f104SBill Paul * side effect of causing the parent interface to receive multicast 623f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 624f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 625f731f104SBill Paul * to avoid this: there really is only one physical interface. 626f731f104SBill Paul */ 6272b120974SPeter Wemm static int 6282b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 629f731f104SBill Paul { 630f731f104SBill Paul struct ifnet *ifp_p; 6312d222cb7SAlexander Motin struct ifmultiaddr *ifma; 632f731f104SBill Paul struct ifvlan *sc; 633c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 634f731f104SBill Paul int error; 635f731f104SBill Paul 636b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 637d148c2a2SMatt Joras 638f731f104SBill Paul /* Find the parent. */ 639f731f104SBill Paul sc = ifp->if_softc; 64075ee267cSGleb Smirnoff ifp_p = PARENT(sc); 6411b2a4f7aSBill Fenner 6428b615593SMarko Zec CURVNET_SET_QUIET(ifp_p->if_vnet); 6438b615593SMarko Zec 644f731f104SBill Paul /* First, remove any existing filter entries. */ 645b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 646b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 6472d222cb7SAlexander Motin (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 6482a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 649f731f104SBill Paul } 650f731f104SBill Paul 651f731f104SBill Paul /* Now program new ones. */ 6522d222cb7SAlexander Motin IF_ADDR_WLOCK(ifp); 653d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 654f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 655f731f104SBill Paul continue; 65629c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 6572d222cb7SAlexander Motin if (mc == NULL) { 6582d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 659c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 66029c2dfbeSBruce M Simpson return (ENOMEM); 6612d222cb7SAlexander Motin } 662e4cd31ddSJeff Roberson bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 663e4cd31ddSJeff Roberson mc->mc_addr.sdl_index = ifp_p->if_index; 664b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 6652d222cb7SAlexander Motin } 6662d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 667b08d611dSMatt Macy CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) { 668e4cd31ddSJeff Roberson error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 6692d222cb7SAlexander Motin NULL); 670c6b2d024SGeorge V. Neville-Neil if (error) { 671c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 672f731f104SBill Paul return (error); 673f731f104SBill Paul } 674c6b2d024SGeorge V. Neville-Neil } 675f731f104SBill Paul 6768b615593SMarko Zec CURVNET_RESTORE(); 677f731f104SBill Paul return (0); 678f731f104SBill Paul } 6792cc2df49SGarrett Wollman 680a3814acfSSam Leffler /* 681f2ab9160SAndrey V. Elsukov * A handler for interface ifnet events. 682f2ab9160SAndrey V. Elsukov */ 683f2ab9160SAndrey V. Elsukov static void 684f2ab9160SAndrey V. Elsukov vlan_ifevent(void *arg __unused, struct ifnet *ifp, int event) 685f2ab9160SAndrey V. Elsukov { 686f2ab9160SAndrey V. Elsukov struct epoch_tracker et; 687f2ab9160SAndrey V. Elsukov struct ifvlan *ifv; 688f2ab9160SAndrey V. Elsukov struct ifvlantrunk *trunk; 689f2ab9160SAndrey V. Elsukov 690f2ab9160SAndrey V. Elsukov if (event != IFNET_EVENT_UPDATE_BAUDRATE) 691f2ab9160SAndrey V. Elsukov return; 692f2ab9160SAndrey V. Elsukov 693f2ab9160SAndrey V. Elsukov NET_EPOCH_ENTER(et); 694f2ab9160SAndrey V. Elsukov trunk = ifp->if_vlantrunk; 695f2ab9160SAndrey V. Elsukov if (trunk == NULL) { 696f2ab9160SAndrey V. Elsukov NET_EPOCH_EXIT(et); 697f2ab9160SAndrey V. Elsukov return; 698f2ab9160SAndrey V. Elsukov } 699f2ab9160SAndrey V. Elsukov 700f2ab9160SAndrey V. Elsukov TRUNK_WLOCK(trunk); 701f2ab9160SAndrey V. Elsukov VLAN_FOREACH(ifv, trunk) { 702f2ab9160SAndrey V. Elsukov ifv->ifv_ifp->if_baudrate = ifp->if_baudrate; 703f2ab9160SAndrey V. Elsukov } 704f2ab9160SAndrey V. Elsukov TRUNK_WUNLOCK(trunk); 705f2ab9160SAndrey V. Elsukov NET_EPOCH_EXIT(et); 706f2ab9160SAndrey V. Elsukov } 707f2ab9160SAndrey V. Elsukov 708f2ab9160SAndrey V. Elsukov /* 709ea4ca115SAndrew Thompson * A handler for parent interface link layer address changes. 710ea4ca115SAndrew Thompson * If the parent interface link layer address is changed we 711ea4ca115SAndrew Thompson * should also change it on all children vlans. 712ea4ca115SAndrew Thompson */ 713ea4ca115SAndrew Thompson static void 714ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 715ea4ca115SAndrew Thompson { 716a68cc388SGleb Smirnoff struct epoch_tracker et; 717ea4ca115SAndrew Thompson struct ifvlan *ifv; 718d148c2a2SMatt Joras struct ifnet *ifv_ifp; 719d148c2a2SMatt Joras struct ifvlantrunk *trunk; 720d148c2a2SMatt Joras struct sockaddr_dl *sdl; 721ea4ca115SAndrew Thompson 7227b7f772fSGleb Smirnoff /* Need the epoch since this is run on taskqueue_swi. */ 723a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 724d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 725d148c2a2SMatt Joras if (trunk == NULL) { 726a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 727ea4ca115SAndrew Thompson return; 728d148c2a2SMatt Joras } 729ea4ca115SAndrew Thompson 730ea4ca115SAndrew Thompson /* 731ea4ca115SAndrew Thompson * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 732d148c2a2SMatt Joras * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR 733d148c2a2SMatt Joras * ioctl calls on the parent garbling the lladdr of the child vlan. 734ea4ca115SAndrew Thompson */ 735d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 736d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 737d148c2a2SMatt Joras /* 738d148c2a2SMatt Joras * Copy new new lladdr into the ifv_ifp, enqueue a task 739d148c2a2SMatt Joras * to actually call if_setlladdr. if_setlladdr needs to 740d148c2a2SMatt Joras * be deferred to a taskqueue because it will call into 741d148c2a2SMatt Joras * the if_vlan ioctl path and try to acquire the global 742d148c2a2SMatt Joras * lock. 743d148c2a2SMatt Joras */ 744d148c2a2SMatt Joras ifv_ifp = ifv->ifv_ifp; 745d148c2a2SMatt Joras bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp), 746e4cd31ddSJeff Roberson ifp->if_addrlen); 747d148c2a2SMatt Joras sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr; 748d148c2a2SMatt Joras sdl->sdl_alen = ifp->if_addrlen; 749d148c2a2SMatt Joras taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 7506117727bSAndrew Thompson } 751d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 752a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 753ea4ca115SAndrew Thompson } 754ea4ca115SAndrew Thompson 755ea4ca115SAndrew Thompson /* 7565cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 7575cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 7585cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 7595428776eSJohn Baldwin * by ejecting its hot-plug card. However, if an ifnet is simply 7605428776eSJohn Baldwin * being renamed, then there's no need to tear down the state. 7615cb8c31aSYaroslav Tykhiy */ 7625cb8c31aSYaroslav Tykhiy static void 7635cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 7645cb8c31aSYaroslav Tykhiy { 7655cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 766d148c2a2SMatt Joras struct ifvlantrunk *trunk; 7675cb8c31aSYaroslav Tykhiy 7685428776eSJohn Baldwin /* If the ifnet is just being renamed, don't do anything. */ 7695428776eSJohn Baldwin if (ifp->if_flags & IFF_RENAMING) 7705428776eSJohn Baldwin return; 771d148c2a2SMatt Joras VLAN_XLOCK(); 772d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 773d148c2a2SMatt Joras if (trunk == NULL) { 774d148c2a2SMatt Joras VLAN_XUNLOCK(); 775d148c2a2SMatt Joras return; 776d148c2a2SMatt Joras } 7775428776eSJohn Baldwin 7785cb8c31aSYaroslav Tykhiy /* 7795cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 7805cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 7815cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 7825cb8c31aSYaroslav Tykhiy */ 783d148c2a2SMatt Joras VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk, 784d148c2a2SMatt Joras ifp->if_vlantrunk == NULL) 78528cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 786d148c2a2SMatt Joras 7875cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 7885cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 789d148c2a2SMatt Joras VLAN_XUNLOCK(); 7905cb8c31aSYaroslav Tykhiy } 7915cb8c31aSYaroslav Tykhiy 7925cb8c31aSYaroslav Tykhiy /* 793e4cd31ddSJeff Roberson * Return the trunk device for a virtual interface. 794e4cd31ddSJeff Roberson */ 795e4cd31ddSJeff Roberson static struct ifnet * 796e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp) 797e4cd31ddSJeff Roberson { 798e4cd31ddSJeff Roberson struct ifvlan *ifv; 799e4cd31ddSJeff Roberson 800b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 801b8a6e03fSGleb Smirnoff 802e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 803e4cd31ddSJeff Roberson return (NULL); 804d148c2a2SMatt Joras 805e4cd31ddSJeff Roberson ifv = ifp->if_softc; 806e4cd31ddSJeff Roberson ifp = NULL; 807e4cd31ddSJeff Roberson if (ifv->ifv_trunk) 808e4cd31ddSJeff Roberson ifp = PARENT(ifv); 809e4cd31ddSJeff Roberson return (ifp); 810e4cd31ddSJeff Roberson } 811e4cd31ddSJeff Roberson 812e4cd31ddSJeff Roberson /* 8137983103aSRobert Watson * Return the 12-bit VLAN VID for this interface, for use by external 8147983103aSRobert Watson * components such as Infiniband. 8157983103aSRobert Watson * 8167983103aSRobert Watson * XXXRW: Note that the function name here is historical; it should be named 8177983103aSRobert Watson * vlan_vid(). 818e4cd31ddSJeff Roberson */ 819e4cd31ddSJeff Roberson static int 8207983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp) 821e4cd31ddSJeff Roberson { 822e4cd31ddSJeff Roberson struct ifvlan *ifv; 823e4cd31ddSJeff Roberson 824e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 825e4cd31ddSJeff Roberson return (EINVAL); 826e4cd31ddSJeff Roberson ifv = ifp->if_softc; 8277983103aSRobert Watson *vidp = ifv->ifv_vid; 828e4cd31ddSJeff Roberson return (0); 829e4cd31ddSJeff Roberson } 830e4cd31ddSJeff Roberson 83132d2623aSNavdeep Parhar static int 83232d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp) 83332d2623aSNavdeep Parhar { 83432d2623aSNavdeep Parhar struct ifvlan *ifv; 83532d2623aSNavdeep Parhar 83632d2623aSNavdeep Parhar if (ifp->if_type != IFT_L2VLAN) 83732d2623aSNavdeep Parhar return (EINVAL); 83832d2623aSNavdeep Parhar ifv = ifp->if_softc; 83932d2623aSNavdeep Parhar *pcpp = ifv->ifv_pcp; 84032d2623aSNavdeep Parhar return (0); 84132d2623aSNavdeep Parhar } 84232d2623aSNavdeep Parhar 843e4cd31ddSJeff Roberson /* 844e4cd31ddSJeff Roberson * Return a driver specific cookie for this interface. Synchronization 845e4cd31ddSJeff Roberson * with setcookie must be provided by the driver. 846e4cd31ddSJeff Roberson */ 847e4cd31ddSJeff Roberson static void * 848e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp) 849e4cd31ddSJeff Roberson { 850e4cd31ddSJeff Roberson struct ifvlan *ifv; 851e4cd31ddSJeff Roberson 852e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 853e4cd31ddSJeff Roberson return (NULL); 854e4cd31ddSJeff Roberson ifv = ifp->if_softc; 855e4cd31ddSJeff Roberson return (ifv->ifv_cookie); 856e4cd31ddSJeff Roberson } 857e4cd31ddSJeff Roberson 858e4cd31ddSJeff Roberson /* 859e4cd31ddSJeff Roberson * Store a cookie in our softc that drivers can use to store driver 860e4cd31ddSJeff Roberson * private per-instance data in. 861e4cd31ddSJeff Roberson */ 862e4cd31ddSJeff Roberson static int 863e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie) 864e4cd31ddSJeff Roberson { 865e4cd31ddSJeff Roberson struct ifvlan *ifv; 866e4cd31ddSJeff Roberson 867e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 868e4cd31ddSJeff Roberson return (EINVAL); 869e4cd31ddSJeff Roberson ifv = ifp->if_softc; 870e4cd31ddSJeff Roberson ifv->ifv_cookie = cookie; 871e4cd31ddSJeff Roberson return (0); 872e4cd31ddSJeff Roberson } 873e4cd31ddSJeff Roberson 874e4cd31ddSJeff Roberson /* 8757983103aSRobert Watson * Return the vlan device present at the specific VID. 876e4cd31ddSJeff Roberson */ 877e4cd31ddSJeff Roberson static struct ifnet * 8787983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid) 879e4cd31ddSJeff Roberson { 880e4cd31ddSJeff Roberson struct ifvlantrunk *trunk; 881e4cd31ddSJeff Roberson struct ifvlan *ifv; 882e4cd31ddSJeff Roberson 883b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 884b8a6e03fSGleb Smirnoff 885e4cd31ddSJeff Roberson trunk = ifp->if_vlantrunk; 886b8a6e03fSGleb Smirnoff if (trunk == NULL) 887e4cd31ddSJeff Roberson return (NULL); 888e4cd31ddSJeff Roberson ifp = NULL; 8897983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 890e4cd31ddSJeff Roberson if (ifv) 891e4cd31ddSJeff Roberson ifp = ifv->ifv_ifp; 892e4cd31ddSJeff Roberson return (ifp); 893e4cd31ddSJeff Roberson } 894e4cd31ddSJeff Roberson 895e4cd31ddSJeff Roberson /* 896a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 897a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 898a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 899a3814acfSSam Leffler * set here. No one else in the system should be aware of this so 900a3814acfSSam Leffler * we use an explicit reference here. 901a3814acfSSam Leffler */ 902a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 903a3814acfSSam Leffler 904984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 905a6fffd6cSBrooks Davis extern void (*vlan_link_state_p)(struct ifnet *); 906127d7b2dSAndre Oppermann 90791ebcbe0SAlexander V. Chernikov static struct if_clone_addreq vlan_addreq = { 90891ebcbe0SAlexander V. Chernikov .match_f = vlan_clone_match, 90991ebcbe0SAlexander V. Chernikov .create_f = vlan_clone_create, 91091ebcbe0SAlexander V. Chernikov .destroy_f = vlan_clone_destroy, 91191ebcbe0SAlexander V. Chernikov }; 91291ebcbe0SAlexander V. Chernikov 9132b120974SPeter Wemm static int 9142b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 9152b120974SPeter Wemm { 9169d4fe4b2SBrooks Davis 9172b120974SPeter Wemm switch (type) { 9182b120974SPeter Wemm case MOD_LOAD: 9195cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 9205cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 9215cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 9225cb8c31aSYaroslav Tykhiy return (ENOMEM); 923ea4ca115SAndrew Thompson iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 924ea4ca115SAndrew Thompson vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 925ea4ca115SAndrew Thompson if (iflladdr_tag == NULL) 926ea4ca115SAndrew Thompson return (ENOMEM); 927f2ab9160SAndrey V. Elsukov ifevent_tag = EVENTHANDLER_REGISTER(ifnet_event, 928f2ab9160SAndrey V. Elsukov vlan_ifevent, NULL, EVENTHANDLER_PRI_ANY); 929f2ab9160SAndrey V. Elsukov if (ifevent_tag == NULL) 930f2ab9160SAndrey V. Elsukov return (ENOMEM); 931d148c2a2SMatt Joras VLAN_LOCKING_INIT(); 9329d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 933127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 93475ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 935e4cd31ddSJeff Roberson vlan_trunkdev_p = vlan_trunkdev; 936e4cd31ddSJeff Roberson vlan_cookie_p = vlan_cookie; 937e4cd31ddSJeff Roberson vlan_setcookie_p = vlan_setcookie; 938e4cd31ddSJeff Roberson vlan_tag_p = vlan_tag; 93932d2623aSNavdeep Parhar vlan_pcp_p = vlan_pcp; 940e4cd31ddSJeff Roberson vlan_devat_p = vlan_devat; 941ccf7ba97SMarko Zec #ifndef VIMAGE 94291ebcbe0SAlexander V. Chernikov vlan_cloner = ifc_attach_cloner(vlanname, &vlan_addreq); 943ccf7ba97SMarko Zec #endif 94425c0f7b3SYaroslav Tykhiy if (bootverbose) 94525c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 94625c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 94725c0f7b3SYaroslav Tykhiy "full-size arrays" 94825c0f7b3SYaroslav Tykhiy #else 94925c0f7b3SYaroslav Tykhiy "hash tables with chaining" 95025c0f7b3SYaroslav Tykhiy #endif 95125c0f7b3SYaroslav Tykhiy 95225c0f7b3SYaroslav Tykhiy "\n"); 9532b120974SPeter Wemm break; 9542b120974SPeter Wemm case MOD_UNLOAD: 955ccf7ba97SMarko Zec #ifndef VIMAGE 95691ebcbe0SAlexander V. Chernikov ifc_detach_cloner(vlan_cloner); 957ccf7ba97SMarko Zec #endif 9585cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 959ea4ca115SAndrew Thompson EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 960f2ab9160SAndrey V. Elsukov EVENTHANDLER_DEREGISTER(ifnet_event, ifevent_tag); 9619d4fe4b2SBrooks Davis vlan_input_p = NULL; 962127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 96375ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 964e4cd31ddSJeff Roberson vlan_trunkdev_p = NULL; 965e4cd31ddSJeff Roberson vlan_tag_p = NULL; 96609fe6320SNavdeep Parhar vlan_cookie_p = NULL; 96709fe6320SNavdeep Parhar vlan_setcookie_p = NULL; 968e4cd31ddSJeff Roberson vlan_devat_p = NULL; 969d148c2a2SMatt Joras VLAN_LOCKING_DESTROY(); 97025c0f7b3SYaroslav Tykhiy if (bootverbose) 97125c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 9729d4fe4b2SBrooks Davis break; 9733e019deaSPoul-Henning Kamp default: 9743e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 9752b120974SPeter Wemm } 97615a66c21SBruce M Simpson return (0); 9772b120974SPeter Wemm } 9782b120974SPeter Wemm 9792b120974SPeter Wemm static moduledata_t vlan_mod = { 9802b120974SPeter Wemm "if_vlan", 9812b120974SPeter Wemm vlan_modevent, 9829823d527SKevin Lo 0 9832b120974SPeter Wemm }; 9842b120974SPeter Wemm 9852b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 98611edc477SEd Maste MODULE_VERSION(if_vlan, 3); 9872cc2df49SGarrett Wollman 988ccf7ba97SMarko Zec #ifdef VIMAGE 989ccf7ba97SMarko Zec static void 990ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused) 991ccf7ba97SMarko Zec { 99291ebcbe0SAlexander V. Chernikov vlan_cloner = ifc_attach_cloner(vlanname, &vlan_addreq); 993ccf7ba97SMarko Zec V_vlan_cloner = vlan_cloner; 994ccf7ba97SMarko Zec } 995ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 996ccf7ba97SMarko Zec vnet_vlan_init, NULL); 997ccf7ba97SMarko Zec 998ccf7ba97SMarko Zec static void 999ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused) 1000ccf7ba97SMarko Zec { 1001ccf7ba97SMarko Zec 100291ebcbe0SAlexander V. Chernikov ifc_detach_cloner(V_vlan_cloner); 1003ccf7ba97SMarko Zec } 1004eb03a443SKristof Provost VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 1005ccf7ba97SMarko Zec vnet_vlan_uninit, NULL); 1006ccf7ba97SMarko Zec #endif 1007ccf7ba97SMarko Zec 1008f941c31aSGleb Smirnoff /* 1009c7cffd65SAlexander V. Chernikov * Check for <etherif>.<vlan>[.<vlan> ...] style interface names. 1010f941c31aSGleb Smirnoff */ 1011f889d2efSBrooks Davis static struct ifnet * 1012f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp) 10139d4fe4b2SBrooks Davis { 1014f941c31aSGleb Smirnoff char ifname[IFNAMSIZ]; 1015f941c31aSGleb Smirnoff char *cp; 1016f889d2efSBrooks Davis struct ifnet *ifp; 10177983103aSRobert Watson int vid; 1018f889d2efSBrooks Davis 1019f941c31aSGleb Smirnoff strlcpy(ifname, name, IFNAMSIZ); 1020c7cffd65SAlexander V. Chernikov if ((cp = strrchr(ifname, '.')) == NULL) 1021f941c31aSGleb Smirnoff return (NULL); 1022f941c31aSGleb Smirnoff *cp = '\0'; 10239bcf3ae4SAlexander Motin if ((ifp = ifunit_ref(ifname)) == NULL) 1024f941c31aSGleb Smirnoff return (NULL); 1025f941c31aSGleb Smirnoff /* Parse VID. */ 10269bcf3ae4SAlexander Motin if (*++cp == '\0') { 10279bcf3ae4SAlexander Motin if_rele(ifp); 1028f941c31aSGleb Smirnoff return (NULL); 10299bcf3ae4SAlexander Motin } 10307983103aSRobert Watson vid = 0; 1031fb92ad4aSJohn Baldwin for(; *cp >= '0' && *cp <= '9'; cp++) 10327983103aSRobert Watson vid = (vid * 10) + (*cp - '0'); 10339bcf3ae4SAlexander Motin if (*cp != '\0') { 10349bcf3ae4SAlexander Motin if_rele(ifp); 1035f941c31aSGleb Smirnoff return (NULL); 10369bcf3ae4SAlexander Motin } 10377983103aSRobert Watson if (vidp != NULL) 10387983103aSRobert Watson *vidp = vid; 1039f889d2efSBrooks Davis 104015a66c21SBruce M Simpson return (ifp); 1041f889d2efSBrooks Davis } 1042f889d2efSBrooks Davis 1043f889d2efSBrooks Davis static int 1044f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 1045f889d2efSBrooks Davis { 10464be465abSAlexander V. Chernikov struct ifnet *ifp; 1047f889d2efSBrooks Davis const char *cp; 1048f889d2efSBrooks Davis 10494be465abSAlexander V. Chernikov ifp = vlan_clone_match_ethervid(name, NULL); 10504be465abSAlexander V. Chernikov if (ifp != NULL) { 10514be465abSAlexander V. Chernikov if_rele(ifp); 1052f889d2efSBrooks Davis return (1); 10534be465abSAlexander V. Chernikov } 1054f889d2efSBrooks Davis 105542a58907SGleb Smirnoff if (strncmp(vlanname, name, strlen(vlanname)) != 0) 1056f889d2efSBrooks Davis return (0); 1057f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 1058f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 1059f889d2efSBrooks Davis return (0); 1060f889d2efSBrooks Davis } 1061f889d2efSBrooks Davis 1062f889d2efSBrooks Davis return (1); 1063f889d2efSBrooks Davis } 1064f889d2efSBrooks Davis 1065f889d2efSBrooks Davis static int 106691ebcbe0SAlexander V. Chernikov vlan_clone_create(struct if_clone *ifc, char *name, size_t len, 106791ebcbe0SAlexander V. Chernikov struct ifc_data *ifd, struct ifnet **ifpp) 1068f889d2efSBrooks Davis { 1069f889d2efSBrooks Davis char *dp; 107053729367SAlexander V. Chernikov bool wildcard = false; 107153729367SAlexander V. Chernikov bool subinterface = false; 1072f889d2efSBrooks Davis int unit; 1073f889d2efSBrooks Davis int error; 107453729367SAlexander V. Chernikov int vid = 0; 107553729367SAlexander V. Chernikov uint16_t proto = ETHERTYPE_VLAN; 10769d4fe4b2SBrooks Davis struct ifvlan *ifv; 10779d4fe4b2SBrooks Davis struct ifnet *ifp; 107853729367SAlexander V. Chernikov struct ifnet *p = NULL; 10793ba24fdeSJohn Baldwin struct ifaddr *ifa; 10803ba24fdeSJohn Baldwin struct sockaddr_dl *sdl; 10816b7330e2SSam Leffler struct vlanreq vlr; 108265239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 1083f889d2efSBrooks Davis 1084c7cffd65SAlexander V. Chernikov 10856b7330e2SSam Leffler /* 108653729367SAlexander V. Chernikov * There are three ways to specify the cloned device: 10876b7330e2SSam Leffler * o pass a parameter block with the clone request. 108853729367SAlexander V. Chernikov * o specify parameters in the text of the clone device name 10896b7330e2SSam Leffler * o specify no parameters and get an unattached device that 10906b7330e2SSam Leffler * must be configured separately. 109153729367SAlexander V. Chernikov * The first technique is preferred; the latter two are supported 1092c7cffd65SAlexander V. Chernikov * for backwards compatibility. 10937983103aSRobert Watson * 10947983103aSRobert Watson * XXXRW: Note historic use of the word "tag" here. New ioctls may be 10957983103aSRobert Watson * called for. 10966b7330e2SSam Leffler */ 109753729367SAlexander V. Chernikov 109891ebcbe0SAlexander V. Chernikov if (ifd->params != NULL) { 109991ebcbe0SAlexander V. Chernikov error = ifc_copyin(ifd, &vlr, sizeof(vlr)); 11006b7330e2SSam Leffler if (error) 11016b7330e2SSam Leffler return error; 110253729367SAlexander V. Chernikov vid = vlr.vlr_tag; 110353729367SAlexander V. Chernikov proto = vlr.vlr_proto; 110453729367SAlexander V. Chernikov 1105afbb64f1SAlexander V. Chernikov #ifdef COMPAT_FREEBSD12 1106afbb64f1SAlexander V. Chernikov if (proto == 0) 1107afbb64f1SAlexander V. Chernikov proto = ETHERTYPE_VLAN; 1108afbb64f1SAlexander V. Chernikov #endif 11099bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 11106b7330e2SSam Leffler if (p == NULL) 1111b1828acfSGleb Smirnoff return (ENXIO); 111253729367SAlexander V. Chernikov } 111353729367SAlexander V. Chernikov 111453729367SAlexander V. Chernikov if ((error = ifc_name2unit(name, &unit)) == 0) { 111553729367SAlexander V. Chernikov 111653729367SAlexander V. Chernikov /* 111753729367SAlexander V. Chernikov * vlanX interface. Set wildcard to true if the unit number 111853729367SAlexander V. Chernikov * is not fixed (-1) 111953729367SAlexander V. Chernikov */ 112053729367SAlexander V. Chernikov wildcard = (unit < 0); 112153729367SAlexander V. Chernikov } else { 112253729367SAlexander V. Chernikov struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid); 112353729367SAlexander V. Chernikov if (p_tmp != NULL) { 112453729367SAlexander V. Chernikov error = 0; 112553729367SAlexander V. Chernikov subinterface = true; 112653729367SAlexander V. Chernikov unit = IF_DUNIT_NONE; 112753729367SAlexander V. Chernikov wildcard = false; 112853729367SAlexander V. Chernikov if (p != NULL) { 112953729367SAlexander V. Chernikov if_rele(p_tmp); 113053729367SAlexander V. Chernikov if (p != p_tmp) 113153729367SAlexander V. Chernikov error = EINVAL; 113253729367SAlexander V. Chernikov } else 113353729367SAlexander V. Chernikov p = p_tmp; 113453729367SAlexander V. Chernikov } else 113553729367SAlexander V. Chernikov error = ENXIO; 113653729367SAlexander V. Chernikov } 113753729367SAlexander V. Chernikov 11389bcf3ae4SAlexander Motin if (error != 0) { 113953729367SAlexander V. Chernikov if (p != NULL) 11409bcf3ae4SAlexander Motin if_rele(p); 11416b7330e2SSam Leffler return (error); 11429bcf3ae4SAlexander Motin } 1143f889d2efSBrooks Davis 114453729367SAlexander V. Chernikov if (!subinterface) { 114553729367SAlexander V. Chernikov /* vlanX interface, mark X as busy or allocate new unit # */ 1146f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 11479bcf3ae4SAlexander Motin if (error != 0) { 11489bcf3ae4SAlexander Motin if (p != NULL) 11499bcf3ae4SAlexander Motin if_rele(p); 1150f889d2efSBrooks Davis return (error); 11519bcf3ae4SAlexander Motin } 115253729367SAlexander V. Chernikov } 1153f889d2efSBrooks Davis 1154f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 1155f889d2efSBrooks Davis if (wildcard) { 1156f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 1157f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 1158f889d2efSBrooks Davis len - (dp-name) - 1) { 1159f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 1160f889d2efSBrooks Davis } 1161f889d2efSBrooks Davis } 11629d4fe4b2SBrooks Davis 1163a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 1164fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 1165fc74a9f9SBrooks Davis if (ifp == NULL) { 116653729367SAlexander V. Chernikov if (!subinterface) 1167fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 1168fc74a9f9SBrooks Davis free(ifv, M_VLAN); 11699bcf3ae4SAlexander Motin if (p != NULL) 11709bcf3ae4SAlexander Motin if_rele(p); 1171fc74a9f9SBrooks Davis return (ENOSPC); 1172fc74a9f9SBrooks Davis } 1173b08d611dSMatt Macy CK_SLIST_INIT(&ifv->vlan_mc_listhead); 11749d4fe4b2SBrooks Davis ifp->if_softc = ifv; 1175f889d2efSBrooks Davis /* 1176cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 1177f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 1178f889d2efSBrooks Davis */ 1179f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 118042a58907SGleb Smirnoff ifp->if_dname = vlanname; 1181f889d2efSBrooks Davis ifp->if_dunit = unit; 11829d4fe4b2SBrooks Davis 1183114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 11842e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 11852e5ff01dSLuiz Otavio O Souza ifp->if_start = vlan_altq_start; 11862e5ff01dSLuiz Otavio O Souza ifp->if_transmit = vlan_altq_transmit; 11872e5ff01dSLuiz Otavio O Souza IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 11882e5ff01dSLuiz Otavio O Souza ifp->if_snd.ifq_drv_maxlen = 0; 11892e5ff01dSLuiz Otavio O Souza IFQ_SET_READY(&ifp->if_snd); 11902e5ff01dSLuiz Otavio O Souza #else 1191d9b1d615SJohn Baldwin ifp->if_transmit = vlan_transmit; 11922e5ff01dSLuiz Otavio O Souza #endif 1193d9b1d615SJohn Baldwin ifp->if_qflush = vlan_qflush; 11949d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 1195b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1196f3e7afe2SHans Petter Selasky ifp->if_snd_tag_alloc = vlan_snd_tag_alloc; 11971a714ff2SRandall Stewart ifp->if_ratelimit_query = vlan_ratelimit_query; 1198f3e7afe2SHans Petter Selasky #endif 119964a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 1200fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 12019d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 1202211f625aSBill Fenner ifp->if_baudrate = 0; 1203a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 1204a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 12053ba24fdeSJohn Baldwin ifa = ifp->if_addr; 12063ba24fdeSJohn Baldwin sdl = (struct sockaddr_dl *)ifa->ifa_addr; 12073ba24fdeSJohn Baldwin sdl->sdl_type = IFT_L2VLAN; 12089d4fe4b2SBrooks Davis 12099bcf3ae4SAlexander Motin if (p != NULL) { 1210c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vid, proto); 12119bcf3ae4SAlexander Motin if_rele(p); 1212f889d2efSBrooks Davis if (error != 0) { 1213f889d2efSBrooks Davis /* 121428cc4d37SJohn Baldwin * Since we've partially failed, we need to back 1215f889d2efSBrooks Davis * out all the way, otherwise userland could get 1216f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 1217f889d2efSBrooks Davis */ 1218f889d2efSBrooks Davis ether_ifdetach(ifp); 1219249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 12204b22573aSBrooks Davis if_free(ifp); 122153729367SAlexander V. Chernikov if (!subinterface) 122296c8ef3aSMaxim Konovalov ifc_free_unit(ifc, unit); 1223f889d2efSBrooks Davis free(ifv, M_VLAN); 1224f889d2efSBrooks Davis 1225f889d2efSBrooks Davis return (error); 1226f889d2efSBrooks Davis } 1227f889d2efSBrooks Davis } 122891ebcbe0SAlexander V. Chernikov *ifpp = ifp; 1229f889d2efSBrooks Davis 12309d4fe4b2SBrooks Davis return (0); 12319d4fe4b2SBrooks Davis } 12329d4fe4b2SBrooks Davis 1233f889d2efSBrooks Davis static int 123491ebcbe0SAlexander V. Chernikov vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 12359d4fe4b2SBrooks Davis { 12369d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 123753729367SAlexander V. Chernikov int unit = ifp->if_dunit; 1238c7cffd65SAlexander V. Chernikov 1239c7cffd65SAlexander V. Chernikov if (ifp->if_vlantrunk) 1240c7cffd65SAlexander V. Chernikov return (EBUSY); 1241b4e9f837SBrooks Davis 12422e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 12432e5ff01dSLuiz Otavio O Souza IFQ_PURGE(&ifp->if_snd); 12442e5ff01dSLuiz Otavio O Souza #endif 1245249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 1246249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 1247d148c2a2SMatt Joras /* 1248d148c2a2SMatt Joras * We should have the only reference to the ifv now, so we can now 1249d148c2a2SMatt Joras * drain any remaining lladdr task before freeing the ifnet and the 1250d148c2a2SMatt Joras * ifvlan. 1251d148c2a2SMatt Joras */ 1252d148c2a2SMatt Joras taskqueue_drain(taskqueue_thread, &ifv->lladdr_task); 1253b08d611dSMatt Macy NET_EPOCH_WAIT(); 12544b22573aSBrooks Davis if_free(ifp); 12559d4fe4b2SBrooks Davis free(ifv, M_VLAN); 125653729367SAlexander V. Chernikov if (unit != IF_DUNIT_NONE) 125753729367SAlexander V. Chernikov ifc_free_unit(ifc, unit); 1258b4e9f837SBrooks Davis 1259f889d2efSBrooks Davis return (0); 12609d4fe4b2SBrooks Davis } 12619d4fe4b2SBrooks Davis 126215a66c21SBruce M Simpson /* 126315a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 126415a66c21SBruce M Simpson */ 12652cc2df49SGarrett Wollman static void 1266114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 12672cc2df49SGarrett Wollman { 12682cc2df49SGarrett Wollman } 12692cc2df49SGarrett Wollman 12706d3a3ab7SGleb Smirnoff /* 1271d9b1d615SJohn Baldwin * The if_transmit method for vlan(4) interface. 12726d3a3ab7SGleb Smirnoff */ 1273d9b1d615SJohn Baldwin static int 1274d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m) 12752cc2df49SGarrett Wollman { 12762cc2df49SGarrett Wollman struct ifvlan *ifv; 12772cc2df49SGarrett Wollman struct ifnet *p; 12781ad7a257SPyun YongHyeon int error, len, mcast; 12792cc2df49SGarrett Wollman 1280b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1281b8a6e03fSGleb Smirnoff 12822cc2df49SGarrett Wollman ifv = ifp->if_softc; 1283d148c2a2SMatt Joras if (TRUNK(ifv) == NULL) { 1284d148c2a2SMatt Joras if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1285d148c2a2SMatt Joras m_freem(m); 1286d148c2a2SMatt Joras return (ENETDOWN); 1287d148c2a2SMatt Joras } 128875ee267cSGleb Smirnoff p = PARENT(ifv); 12891ad7a257SPyun YongHyeon len = m->m_pkthdr.len; 12901ad7a257SPyun YongHyeon mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 12912cc2df49SGarrett Wollman 1292a3814acfSSam Leffler BPF_MTAP(ifp, m); 12932cc2df49SGarrett Wollman 1294b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1295fb3bc596SJohn Baldwin if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) { 1296fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 1297fb3bc596SJohn Baldwin struct m_snd_tag *mst; 1298fb3bc596SJohn Baldwin 1299fb3bc596SJohn Baldwin MPASS(m->m_pkthdr.snd_tag->ifp == ifp); 1300fb3bc596SJohn Baldwin mst = m->m_pkthdr.snd_tag; 1301fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 1302fb3bc596SJohn Baldwin if (vst->tag->ifp != p) { 1303fb3bc596SJohn Baldwin if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1304fb3bc596SJohn Baldwin m_freem(m); 1305fb3bc596SJohn Baldwin return (EAGAIN); 1306fb3bc596SJohn Baldwin } 1307fb3bc596SJohn Baldwin 1308fb3bc596SJohn Baldwin m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag); 1309fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 1310fb3bc596SJohn Baldwin } 1311fb3bc596SJohn Baldwin #endif 1312fb3bc596SJohn Baldwin 1313f731f104SBill Paul /* 1314d9b1d615SJohn Baldwin * Do not run parent's if_transmit() if the parent is not up, 131524993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 131624993214SYaroslav Tykhiy */ 13172dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 1318a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1319d148c2a2SMatt Joras m_freem(m); 13208b20f6cfSHiroki Sato return (ENETDOWN); 132124993214SYaroslav Tykhiy } 132224993214SYaroslav Tykhiy 1323c7cffd65SAlexander V. Chernikov if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) { 1324a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1325d9b1d615SJohn Baldwin return (0); 13264af90a4dSMatthew N. Dodd } 13272cc2df49SGarrett Wollman 13282cc2df49SGarrett Wollman /* 13292cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 13302cc2df49SGarrett Wollman */ 1331aea78d20SKip Macy error = (p->if_transmit)(p, m); 1332299153b5SAlexander V. Chernikov if (error == 0) { 1333a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1334a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 1335a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast); 13361ad7a257SPyun YongHyeon } else 1337a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1338d9b1d615SJohn Baldwin return (error); 13392cc2df49SGarrett Wollman } 1340d9b1d615SJohn Baldwin 134116cf6bdbSMatt Joras static int 134216cf6bdbSMatt Joras vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 134316cf6bdbSMatt Joras struct route *ro) 134416cf6bdbSMatt Joras { 134516cf6bdbSMatt Joras struct ifvlan *ifv; 134616cf6bdbSMatt Joras struct ifnet *p; 134716cf6bdbSMatt Joras 1348b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1349b8a6e03fSGleb Smirnoff 1350c7cffd65SAlexander V. Chernikov /* 1351c7cffd65SAlexander V. Chernikov * Find the first non-VLAN parent interface. 1352c7cffd65SAlexander V. Chernikov */ 135316cf6bdbSMatt Joras ifv = ifp->if_softc; 1354c7cffd65SAlexander V. Chernikov do { 135516cf6bdbSMatt Joras if (TRUNK(ifv) == NULL) { 135616cf6bdbSMatt Joras m_freem(m); 135716cf6bdbSMatt Joras return (ENETDOWN); 135816cf6bdbSMatt Joras } 135916cf6bdbSMatt Joras p = PARENT(ifv); 1360c7cffd65SAlexander V. Chernikov ifv = p->if_softc; 1361c7cffd65SAlexander V. Chernikov } while (p->if_type == IFT_L2VLAN); 1362c7cffd65SAlexander V. Chernikov 136316cf6bdbSMatt Joras return p->if_output(ifp, m, dst, ro); 136416cf6bdbSMatt Joras } 136516cf6bdbSMatt Joras 13662e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 13672e5ff01dSLuiz Otavio O Souza static void 13682e5ff01dSLuiz Otavio O Souza vlan_altq_start(if_t ifp) 13692e5ff01dSLuiz Otavio O Souza { 13702e5ff01dSLuiz Otavio O Souza struct ifaltq *ifq = &ifp->if_snd; 13712e5ff01dSLuiz Otavio O Souza struct mbuf *m; 13722e5ff01dSLuiz Otavio O Souza 13732e5ff01dSLuiz Otavio O Souza IFQ_LOCK(ifq); 13742e5ff01dSLuiz Otavio O Souza IFQ_DEQUEUE_NOLOCK(ifq, m); 13752e5ff01dSLuiz Otavio O Souza while (m != NULL) { 13762e5ff01dSLuiz Otavio O Souza vlan_transmit(ifp, m); 13772e5ff01dSLuiz Otavio O Souza IFQ_DEQUEUE_NOLOCK(ifq, m); 13782e5ff01dSLuiz Otavio O Souza } 13792e5ff01dSLuiz Otavio O Souza IFQ_UNLOCK(ifq); 13802e5ff01dSLuiz Otavio O Souza } 13812e5ff01dSLuiz Otavio O Souza 13822e5ff01dSLuiz Otavio O Souza static int 13832e5ff01dSLuiz Otavio O Souza vlan_altq_transmit(if_t ifp, struct mbuf *m) 13842e5ff01dSLuiz Otavio O Souza { 13852e5ff01dSLuiz Otavio O Souza int err; 13862e5ff01dSLuiz Otavio O Souza 13872e5ff01dSLuiz Otavio O Souza if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 13882e5ff01dSLuiz Otavio O Souza IFQ_ENQUEUE(&ifp->if_snd, m, err); 13892e5ff01dSLuiz Otavio O Souza if (err == 0) 13902e5ff01dSLuiz Otavio O Souza vlan_altq_start(ifp); 13912e5ff01dSLuiz Otavio O Souza } else 13922e5ff01dSLuiz Otavio O Souza err = vlan_transmit(ifp, m); 13932e5ff01dSLuiz Otavio O Souza 13942e5ff01dSLuiz Otavio O Souza return (err); 13952e5ff01dSLuiz Otavio O Souza } 13962e5ff01dSLuiz Otavio O Souza #endif /* ALTQ */ 13972e5ff01dSLuiz Otavio O Souza 1398d9b1d615SJohn Baldwin /* 1399d9b1d615SJohn Baldwin * The ifp->if_qflush entry point for vlan(4) is a no-op. 1400d9b1d615SJohn Baldwin */ 1401d9b1d615SJohn Baldwin static void 1402d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused) 1403d9b1d615SJohn Baldwin { 1404f731f104SBill Paul } 1405f731f104SBill Paul 1406a3814acfSSam Leffler static void 1407a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 1408f731f104SBill Paul { 1409d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1410f731f104SBill Paul struct ifvlan *ifv; 14112ccbbd06SMarcelo Araujo struct m_tag *mtag; 14122ccbbd06SMarcelo Araujo uint16_t vid, tag; 141375ee267cSGleb Smirnoff 1414b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1415b8a6e03fSGleb Smirnoff 1416d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1417d148c2a2SMatt Joras if (trunk == NULL) { 1418d148c2a2SMatt Joras m_freem(m); 1419d148c2a2SMatt Joras return; 1420d148c2a2SMatt Joras } 1421a3814acfSSam Leffler 1422f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 1423a3814acfSSam Leffler /* 142414e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 1425a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 1426a3814acfSSam Leffler */ 14272ccbbd06SMarcelo Araujo tag = m->m_pkthdr.ether_vtag; 14286ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 1429a3814acfSSam Leffler } else { 143075ee267cSGleb Smirnoff struct ether_vlan_header *evl; 143175ee267cSGleb Smirnoff 143214e98256SYaroslav Tykhiy /* 143314e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 143414e98256SYaroslav Tykhiy */ 1435a3814acfSSam Leffler switch (ifp->if_type) { 1436a3814acfSSam Leffler case IFT_ETHER: 1437a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 1438a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 1439a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 1440a3814acfSSam Leffler return; 1441a3814acfSSam Leffler } 1442a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 14432ccbbd06SMarcelo Araujo tag = ntohs(evl->evl_tag); 1444db8b5973SYaroslav Tykhiy 1445db8b5973SYaroslav Tykhiy /* 14462dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 14472dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 14482dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 14492dc879b3SYaroslav Tykhiy * type field is already in place. 1450db8b5973SYaroslav Tykhiy */ 14512dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 14522dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 14532dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 1454a3814acfSSam Leffler break; 14552dc879b3SYaroslav Tykhiy 1456a3814acfSSam Leffler default: 1457db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 145860c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 145960c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 1460a3814acfSSam Leffler #endif 14613751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1462d148c2a2SMatt Joras m_freem(m); 146360c60618SYaroslav Tykhiy return; 1464a3814acfSSam Leffler } 14657a46ec8fSBrooks Davis } 14667a46ec8fSBrooks Davis 14672ccbbd06SMarcelo Araujo vid = EVL_VLANOFTAG(tag); 14682ccbbd06SMarcelo Araujo 14697983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 14702dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 1471b08d611dSMatt Macy if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1472d148c2a2SMatt Joras m_freem(m); 147375ee267cSGleb Smirnoff return; 147475ee267cSGleb Smirnoff } 1475f731f104SBill Paul 147678bc3d5eSKristof Provost if (V_vlan_mtag_pcp) { 14772ccbbd06SMarcelo Araujo /* 14782ccbbd06SMarcelo Araujo * While uncommon, it is possible that we will find a 802.1q 14792ccbbd06SMarcelo Araujo * packet encapsulated inside another packet that also had an 14802ccbbd06SMarcelo Araujo * 802.1q header. For example, ethernet tunneled over IPSEC 14812ccbbd06SMarcelo Araujo * arriving over ethernet. In that case, we replace the 14822ccbbd06SMarcelo Araujo * existing 802.1q PCP m_tag value. 14832ccbbd06SMarcelo Araujo */ 14842ccbbd06SMarcelo Araujo mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 14852ccbbd06SMarcelo Araujo if (mtag == NULL) { 14862ccbbd06SMarcelo Araujo mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN, 14872ccbbd06SMarcelo Araujo sizeof(uint8_t), M_NOWAIT); 14882ccbbd06SMarcelo Araujo if (mtag == NULL) { 14892ccbbd06SMarcelo Araujo if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1490d148c2a2SMatt Joras m_freem(m); 14912ccbbd06SMarcelo Araujo return; 14922ccbbd06SMarcelo Araujo } 14932ccbbd06SMarcelo Araujo m_tag_prepend(m, mtag); 14942ccbbd06SMarcelo Araujo } 14952ccbbd06SMarcelo Araujo *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag); 14962ccbbd06SMarcelo Araujo } 14972ccbbd06SMarcelo Araujo 1498fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 1499c0304424SGleb Smirnoff if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1); 15002cc2df49SGarrett Wollman 1501a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 1502fdbf1174SMatt Joras (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m); 15032cc2df49SGarrett Wollman } 15042cc2df49SGarrett Wollman 1505d148c2a2SMatt Joras static void 1506d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused) 1507d148c2a2SMatt Joras { 1508d148c2a2SMatt Joras struct ifvlan *ifv; 1509d148c2a2SMatt Joras struct ifnet *ifp; 1510d148c2a2SMatt Joras 1511d148c2a2SMatt Joras ifv = (struct ifvlan *)arg; 1512d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 15135191a3aeSKristof Provost 15145191a3aeSKristof Provost CURVNET_SET(ifp->if_vnet); 15155191a3aeSKristof Provost 1516d148c2a2SMatt Joras /* The ifv_ifp already has the lladdr copied in. */ 1517d148c2a2SMatt Joras if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen); 15185191a3aeSKristof Provost 15195191a3aeSKristof Provost CURVNET_RESTORE(); 1520d148c2a2SMatt Joras } 1521d148c2a2SMatt Joras 15222cc2df49SGarrett Wollman static int 1523c7cffd65SAlexander V. Chernikov vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid, 1524c7cffd65SAlexander V. Chernikov uint16_t proto) 15252cc2df49SGarrett Wollman { 15266dcec895SGleb Smirnoff struct epoch_tracker et; 152775ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 15281cf236fbSYaroslav Tykhiy struct ifnet *ifp; 152975ee267cSGleb Smirnoff int error = 0; 15302cc2df49SGarrett Wollman 1531b1828acfSGleb Smirnoff /* 1532b1828acfSGleb Smirnoff * We can handle non-ethernet hardware types as long as 1533b1828acfSGleb Smirnoff * they handle the tagging and headers themselves. 1534b1828acfSGleb Smirnoff */ 1535e4cd31ddSJeff Roberson if (p->if_type != IFT_ETHER && 1536c7cffd65SAlexander V. Chernikov p->if_type != IFT_L2VLAN && 1537e4cd31ddSJeff Roberson (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 153815a66c21SBruce M Simpson return (EPROTONOSUPPORT); 153964a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 154064a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 1541b1828acfSGleb Smirnoff /* 1542b1828acfSGleb Smirnoff * Don't let the caller set up a VLAN VID with 1543b1828acfSGleb Smirnoff * anything except VLID bits. 1544b1828acfSGleb Smirnoff * VID numbers 0x0 and 0xFFF are reserved. 1545b1828acfSGleb Smirnoff */ 1546b1828acfSGleb Smirnoff if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK)) 1547b1828acfSGleb Smirnoff return (EINVAL); 1548663f556bSKristof Provost if (ifv->ifv_trunk) { 1549663f556bSKristof Provost trunk = ifv->ifv_trunk; 1550663f556bSKristof Provost if (trunk->parent != p) 155115a66c21SBruce M Simpson return (EBUSY); 15522cc2df49SGarrett Wollman 1553d148c2a2SMatt Joras VLAN_XLOCK(); 1554663f556bSKristof Provost 1555663f556bSKristof Provost ifv->ifv_proto = proto; 1556663f556bSKristof Provost 1557663f556bSKristof Provost if (ifv->ifv_vid != vid) { 1558663f556bSKristof Provost /* Re-hash */ 1559663f556bSKristof Provost vlan_remhash(trunk, ifv); 1560663f556bSKristof Provost ifv->ifv_vid = vid; 1561663f556bSKristof Provost error = vlan_inshash(trunk, ifv); 1562663f556bSKristof Provost } 1563663f556bSKristof Provost /* Will unlock */ 1564663f556bSKristof Provost goto done; 1565663f556bSKristof Provost } 1566663f556bSKristof Provost 1567663f556bSKristof Provost VLAN_XLOCK(); 156875ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 156975ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 157075ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 157175ee267cSGleb Smirnoff vlan_inithash(trunk); 157275ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 1573d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 157475ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 157575ee267cSGleb Smirnoff trunk->parent = p; 15769bcf3ae4SAlexander Motin if_ref(trunk->parent); 1577b08d611dSMatt Macy TRUNK_WUNLOCK(trunk); 157875ee267cSGleb Smirnoff } else { 157975ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 158075ee267cSGleb Smirnoff } 158175ee267cSGleb Smirnoff 15827983103aSRobert Watson ifv->ifv_vid = vid; /* must set this before vlan_inshash() */ 15832ccbbd06SMarcelo Araujo ifv->ifv_pcp = 0; /* Default: best effort delivery. */ 158475ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 158575ee267cSGleb Smirnoff if (error) 158675ee267cSGleb Smirnoff goto done; 1587c7cffd65SAlexander V. Chernikov ifv->ifv_proto = proto; 1588a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1589a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 15901cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1591d89baa5aSAlexander Motin ifv->ifv_capenable = -1; 1592a3814acfSSam Leffler 1593a3814acfSSam Leffler /* 1594a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1595a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1596656acce4SYaroslav Tykhiy * use it. 1597a3814acfSSam Leffler */ 1598656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1599656acce4SYaroslav Tykhiy /* 1600656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1601656acce4SYaroslav Tykhiy * handle extended frames. 1602656acce4SYaroslav Tykhiy */ 1603a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1604656acce4SYaroslav Tykhiy } else { 1605a3814acfSSam Leffler /* 1606a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1607a3814acfSSam Leffler * makes us incompatible with strictly compliant 1608a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1609a3814acfSSam Leffler * the feature with other NetBSD implementations, 1610a3814acfSSam Leffler * which might still be useful. 1611a3814acfSSam Leffler */ 1612a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1613a3814acfSSam Leffler } 1614a3814acfSSam Leffler 161575ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 16161cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 1617e4cd31ddSJeff Roberson /* 1618e4cd31ddSJeff Roberson * Initialize fields from our parent. This duplicates some 1619e4cd31ddSJeff Roberson * work with ether_ifattach() but allows for non-ethernet 1620e4cd31ddSJeff Roberson * interfaces to also work. 1621e4cd31ddSJeff Roberson */ 16221cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 162375ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 1624e4cd31ddSJeff Roberson ifp->if_input = p->if_input; 1625e4cd31ddSJeff Roberson ifp->if_resolvemulti = p->if_resolvemulti; 1626e4cd31ddSJeff Roberson ifp->if_addrlen = p->if_addrlen; 1627e4cd31ddSJeff Roberson ifp->if_broadcastaddr = p->if_broadcastaddr; 162832a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 1629e4cd31ddSJeff Roberson 16302cc2df49SGarrett Wollman /* 163116cf6bdbSMatt Joras * We wrap the parent's if_output using vlan_output to ensure that it 163216cf6bdbSMatt Joras * can't become stale. 163316cf6bdbSMatt Joras */ 163416cf6bdbSMatt Joras ifp->if_output = vlan_output; 163516cf6bdbSMatt Joras 163616cf6bdbSMatt Joras /* 163724993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 163824993214SYaroslav Tykhiy * Other flags are none of our business. 16392cc2df49SGarrett Wollman */ 164064a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 16411cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 16421cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 16431cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 16441cf236fbSYaroslav Tykhiy 16451cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 16462cc2df49SGarrett Wollman 16476dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 164875ee267cSGleb Smirnoff vlan_capabilities(ifv); 16496dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 1650a3814acfSSam Leffler 1651a3814acfSSam Leffler /* 1652e4cd31ddSJeff Roberson * Set up our interface address to reflect the underlying 16532cc2df49SGarrett Wollman * physical interface's. 16542cc2df49SGarrett Wollman */ 1655a961401eSAndrey V. Elsukov TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv); 1656e4cd31ddSJeff Roberson ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1657e4cd31ddSJeff Roberson p->if_addrlen; 16581b2a4f7aSBill Fenner 1659a961401eSAndrey V. Elsukov /* 1660a961401eSAndrey V. Elsukov * Do not schedule link address update if it was the same 1661a961401eSAndrey V. Elsukov * as previous parent's. This helps avoid updating for each 1662a961401eSAndrey V. Elsukov * associated llentry. 1663a961401eSAndrey V. Elsukov */ 1664a961401eSAndrey V. Elsukov if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) { 1665a961401eSAndrey V. Elsukov bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1666a961401eSAndrey V. Elsukov taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 1667a961401eSAndrey V. Elsukov } 16682ada9747SYaroslav Tykhiy 16692ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 16702ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 1671d148c2a2SMatt Joras 1672d148c2a2SMatt Joras /* Update flags on the parent, if necessary. */ 1673d148c2a2SMatt Joras vlan_setflags(ifp, 1); 1674b08d611dSMatt Macy 1675d148c2a2SMatt Joras /* 1676b08d611dSMatt Macy * Configure multicast addresses that may already be 1677b08d611dSMatt Macy * joined on the vlan device. 1678d148c2a2SMatt Joras */ 1679b08d611dSMatt Macy (void)vlan_setmulti(ifp); 1680b08d611dSMatt Macy 1681b08d611dSMatt Macy done: 1682c725524cSJack F Vogel if (error == 0) 16837983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid); 1684d148c2a2SMatt Joras VLAN_XUNLOCK(); 168575ee267cSGleb Smirnoff 168675ee267cSGleb Smirnoff return (error); 16872cc2df49SGarrett Wollman } 16882cc2df49SGarrett Wollman 16896f359e28SJohn Baldwin static void 1690f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1691f731f104SBill Paul { 16925cb8c31aSYaroslav Tykhiy 1693d148c2a2SMatt Joras VLAN_XLOCK(); 169428cc4d37SJohn Baldwin vlan_unconfig_locked(ifp, 0); 1695d148c2a2SMatt Joras VLAN_XUNLOCK(); 16965cb8c31aSYaroslav Tykhiy } 16975cb8c31aSYaroslav Tykhiy 16986f359e28SJohn Baldwin static void 169928cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing) 17005cb8c31aSYaroslav Tykhiy { 170175ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1702f731f104SBill Paul struct vlan_mc_entry *mc; 1703f731f104SBill Paul struct ifvlan *ifv; 1704c725524cSJack F Vogel struct ifnet *parent; 170528cc4d37SJohn Baldwin int error; 1706f731f104SBill Paul 1707d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 17084faedfe8SSam Leffler 1709f731f104SBill Paul ifv = ifp->if_softc; 171075ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 171122893351SJack F Vogel parent = NULL; 1712f731f104SBill Paul 171322893351SJack F Vogel if (trunk != NULL) { 171422893351SJack F Vogel parent = trunk->parent; 17151b2a4f7aSBill Fenner 1716f731f104SBill Paul /* 1717f731f104SBill Paul * Since the interface is being unconfigured, we need to 1718f731f104SBill Paul * empty the list of multicast groups that we may have joined 17191b2a4f7aSBill Fenner * while we were alive from the parent's list. 1720f731f104SBill Paul */ 1721b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 17226f359e28SJohn Baldwin /* 172328cc4d37SJohn Baldwin * If the parent interface is being detached, 1724b90dde2fSJohn Baldwin * all its multicast addresses have already 172528cc4d37SJohn Baldwin * been removed. Warn about errors if 172628cc4d37SJohn Baldwin * if_delmulti() does fail, but don't abort as 172728cc4d37SJohn Baldwin * all callers expect vlan destruction to 172828cc4d37SJohn Baldwin * succeed. 17296f359e28SJohn Baldwin */ 173028cc4d37SJohn Baldwin if (!departing) { 173128cc4d37SJohn Baldwin error = if_delmulti(parent, 1732e4cd31ddSJeff Roberson (struct sockaddr *)&mc->mc_addr); 173328cc4d37SJohn Baldwin if (error) 173428cc4d37SJohn Baldwin if_printf(ifp, 173528cc4d37SJohn Baldwin "Failed to delete multicast address from parent: %d\n", 173628cc4d37SJohn Baldwin error); 173728cc4d37SJohn Baldwin } 1738b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 17392a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 1740f731f104SBill Paul } 1741a3814acfSSam Leffler 17421cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 1743d148c2a2SMatt Joras 174475ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 174575ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 174675ee267cSGleb Smirnoff 174775ee267cSGleb Smirnoff /* 174875ee267cSGleb Smirnoff * Check if we were the last. 174975ee267cSGleb Smirnoff */ 175075ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 17512d222cb7SAlexander Motin parent->if_vlantrunk = NULL; 1752b08d611dSMatt Macy NET_EPOCH_WAIT(); 175375ee267cSGleb Smirnoff trunk_destroy(trunk); 1754d148c2a2SMatt Joras } 17551b2a4f7aSBill Fenner } 1756f731f104SBill Paul 1757f731f104SBill Paul /* Disconnect from parent. */ 17581cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 17591cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 17605cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 17615cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 17625cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1763f731f104SBill Paul 176422893351SJack F Vogel /* 176522893351SJack F Vogel * Only dispatch an event if vlan was 176622893351SJack F Vogel * attached, otherwise there is nothing 176722893351SJack F Vogel * to cleanup anyway. 176822893351SJack F Vogel */ 176922893351SJack F Vogel if (parent != NULL) 17707983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid); 1771f731f104SBill Paul } 1772f731f104SBill Paul 17731cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1774f731f104SBill Paul static int 17751cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 17761cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1777a3814acfSSam Leffler { 17781cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 17791cf236fbSYaroslav Tykhiy int error; 1780a3814acfSSam Leffler 1781d148c2a2SMatt Joras VLAN_SXLOCK_ASSERT(); 1782a3814acfSSam Leffler 17831cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 17841cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 17851cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 17861cf236fbSYaroslav Tykhiy 17871cf236fbSYaroslav Tykhiy /* 17881cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 17891cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 17901cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 17911cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 17921cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 17931cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 17941cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 17951cf236fbSYaroslav Tykhiy */ 17961cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 179775ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 17981cf236fbSYaroslav Tykhiy if (error) 1799a3814acfSSam Leffler return (error); 18001cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 18011cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 18021cf236fbSYaroslav Tykhiy } 18031cf236fbSYaroslav Tykhiy return (0); 18041cf236fbSYaroslav Tykhiy } 18051cf236fbSYaroslav Tykhiy 18061cf236fbSYaroslav Tykhiy /* 18071cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 18081cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 18091cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 18101cf236fbSYaroslav Tykhiy */ 18111cf236fbSYaroslav Tykhiy static int 18121cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 18131cf236fbSYaroslav Tykhiy { 18141cf236fbSYaroslav Tykhiy int error, i; 18151cf236fbSYaroslav Tykhiy 18161cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 18171cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 18181cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 18191cf236fbSYaroslav Tykhiy if (error) 18201cf236fbSYaroslav Tykhiy return (error); 18211cf236fbSYaroslav Tykhiy } 18221cf236fbSYaroslav Tykhiy return (0); 1823a3814acfSSam Leffler } 1824a3814acfSSam Leffler 1825127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1826127d7b2dSAndre Oppermann static void 1827a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp) 1828127d7b2dSAndre Oppermann { 1829b2807792SGleb Smirnoff struct epoch_tracker et; 1830d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1831127d7b2dSAndre Oppermann struct ifvlan *ifv; 1832127d7b2dSAndre Oppermann 1833b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 1834d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1835b2807792SGleb Smirnoff if (trunk == NULL) { 1836b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 1837d148c2a2SMatt Joras return; 1838b2807792SGleb Smirnoff } 1839d148c2a2SMatt Joras 1840d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 1841d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 1842aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1843fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 184475ee267cSGleb Smirnoff trunk->parent->if_link_state); 1845127d7b2dSAndre Oppermann } 1846d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 1847b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 184875ee267cSGleb Smirnoff } 184975ee267cSGleb Smirnoff 185075ee267cSGleb Smirnoff static void 185175ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 185275ee267cSGleb Smirnoff { 1853d148c2a2SMatt Joras struct ifnet *p; 1854d148c2a2SMatt Joras struct ifnet *ifp; 18559fd573c3SHans Petter Selasky struct ifnet_hw_tsomax hw_tsomax; 1856d89baa5aSAlexander Motin int cap = 0, ena = 0, mena; 1857d89baa5aSAlexander Motin u_long hwa = 0; 185875ee267cSGleb Smirnoff 1859a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 1860b8a6e03fSGleb Smirnoff VLAN_SXLOCK_ASSERT(); 1861b8a6e03fSGleb Smirnoff 1862d148c2a2SMatt Joras p = PARENT(ifv); 1863d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 186475ee267cSGleb Smirnoff 1865d89baa5aSAlexander Motin /* Mask parent interface enabled capabilities disabled by user. */ 1866d89baa5aSAlexander Motin mena = p->if_capenable & ifv->ifv_capenable; 1867d89baa5aSAlexander Motin 186875ee267cSGleb Smirnoff /* 186975ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 187075ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 187175ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 187275ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 187375ee267cSGleb Smirnoff */ 187475ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1875d89baa5aSAlexander Motin cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 187675ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 187775ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1878d89baa5aSAlexander Motin ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 1879d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM) 1880d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP | 1881d89baa5aSAlexander Motin CSUM_UDP | CSUM_SCTP); 1882d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM_IPV6) 1883d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_TCP_IPV6 | 1884d89baa5aSAlexander Motin CSUM_UDP_IPV6 | CSUM_SCTP_IPV6); 188575ee267cSGleb Smirnoff } 1886d89baa5aSAlexander Motin 18879b76d9cbSPyun YongHyeon /* 18889b76d9cbSPyun YongHyeon * If the parent interface can do TSO on VLANs then 18899b76d9cbSPyun YongHyeon * propagate the hardware-assisted flag. TSO on VLANs 18909b76d9cbSPyun YongHyeon * does not necessarily require hardware VLAN tagging. 18919b76d9cbSPyun YongHyeon */ 18929fd573c3SHans Petter Selasky memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 18939fd573c3SHans Petter Selasky if_hw_tsomax_common(p, &hw_tsomax); 18949fd573c3SHans Petter Selasky if_hw_tsomax_update(ifp, &hw_tsomax); 18959b76d9cbSPyun YongHyeon if (p->if_capabilities & IFCAP_VLAN_HWTSO) 1896d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TSO; 18979b76d9cbSPyun YongHyeon if (p->if_capenable & IFCAP_VLAN_HWTSO) { 1898d89baa5aSAlexander Motin ena |= mena & IFCAP_TSO; 1899d89baa5aSAlexander Motin if (ena & IFCAP_TSO) 1900d89baa5aSAlexander Motin hwa |= p->if_hwassist & CSUM_TSO; 19019b76d9cbSPyun YongHyeon } 190209fe6320SNavdeep Parhar 190309fe6320SNavdeep Parhar /* 190459150e91SAlexander Motin * If the parent interface can do LRO and checksum offloading on 190559150e91SAlexander Motin * VLANs, then guess it may do LRO on VLANs. False positive here 190659150e91SAlexander Motin * cost nothing, while false negative may lead to some confusions. 190759150e91SAlexander Motin */ 190859150e91SAlexander Motin if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 190959150e91SAlexander Motin cap |= p->if_capabilities & IFCAP_LRO; 191059150e91SAlexander Motin if (p->if_capenable & IFCAP_VLAN_HWCSUM) 191159150e91SAlexander Motin ena |= p->if_capenable & IFCAP_LRO; 191259150e91SAlexander Motin 191359150e91SAlexander Motin /* 191409fe6320SNavdeep Parhar * If the parent interface can offload TCP connections over VLANs then 191509fe6320SNavdeep Parhar * propagate its TOE capability to the VLAN interface. 191609fe6320SNavdeep Parhar * 191709fe6320SNavdeep Parhar * All TOE drivers in the tree today can deal with VLANs. If this 191809fe6320SNavdeep Parhar * changes then IFCAP_VLAN_TOE should be promoted to a full capability 191909fe6320SNavdeep Parhar * with its own bit. 192009fe6320SNavdeep Parhar */ 192109fe6320SNavdeep Parhar #define IFCAP_VLAN_TOE IFCAP_TOE 192209fe6320SNavdeep Parhar if (p->if_capabilities & IFCAP_VLAN_TOE) 1923d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TOE; 192409fe6320SNavdeep Parhar if (p->if_capenable & IFCAP_VLAN_TOE) { 1925*c255d1a4SJustin Hibbits SETTOEDEV(ifp, TOEDEV(p)); 1926d89baa5aSAlexander Motin ena |= mena & IFCAP_TOE; 192709fe6320SNavdeep Parhar } 1928f3e7afe2SHans Petter Selasky 1929d89baa5aSAlexander Motin /* 1930d89baa5aSAlexander Motin * If the parent interface supports dynamic link state, so does the 1931d89baa5aSAlexander Motin * VLAN interface. 1932d89baa5aSAlexander Motin */ 1933d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_LINKSTATE); 1934d89baa5aSAlexander Motin ena |= (mena & IFCAP_LINKSTATE); 1935d89baa5aSAlexander Motin 1936f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1937f3e7afe2SHans Petter Selasky /* 1938f3e7afe2SHans Petter Selasky * If the parent interface supports ratelimiting, so does the 1939f3e7afe2SHans Petter Selasky * VLAN interface. 1940f3e7afe2SHans Petter Selasky */ 1941d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_TXRTLMT); 1942d89baa5aSAlexander Motin ena |= (mena & IFCAP_TXRTLMT); 1943f3e7afe2SHans Petter Selasky #endif 1944d89baa5aSAlexander Motin 194566d0c056SJohn Baldwin /* 194666d0c056SJohn Baldwin * If the parent interface supports unmapped mbufs, so does 194766d0c056SJohn Baldwin * the VLAN interface. Note that this should be fine even for 194866d0c056SJohn Baldwin * interfaces that don't support hardware tagging as headers 194966d0c056SJohn Baldwin * are prepended in normal mbufs to unmapped mbufs holding 195066d0c056SJohn Baldwin * payload data. 195166d0c056SJohn Baldwin */ 19523f43ada9SGleb Smirnoff cap |= (p->if_capabilities & IFCAP_MEXTPG); 19533f43ada9SGleb Smirnoff ena |= (mena & IFCAP_MEXTPG); 195466d0c056SJohn Baldwin 1955b2e60773SJohn Baldwin /* 1956b2e60773SJohn Baldwin * If the parent interface can offload encryption and segmentation 1957b2e60773SJohn Baldwin * of TLS records over TCP, propagate it's capability to the VLAN 1958b2e60773SJohn Baldwin * interface. 1959b2e60773SJohn Baldwin * 1960b2e60773SJohn Baldwin * All TLS drivers in the tree today can deal with VLANs. If 1961b2e60773SJohn Baldwin * this ever changes, then a new IFCAP_VLAN_TXTLS can be 1962b2e60773SJohn Baldwin * defined. 1963b2e60773SJohn Baldwin */ 1964521eac97SJohn Baldwin if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 1965521eac97SJohn Baldwin cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 1966521eac97SJohn Baldwin if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 1967521eac97SJohn Baldwin ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 1968b2e60773SJohn Baldwin 1969d89baa5aSAlexander Motin ifp->if_capabilities = cap; 1970d89baa5aSAlexander Motin ifp->if_capenable = ena; 1971d89baa5aSAlexander Motin ifp->if_hwassist = hwa; 197275ee267cSGleb Smirnoff } 197375ee267cSGleb Smirnoff 197475ee267cSGleb Smirnoff static void 197575ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 197675ee267cSGleb Smirnoff { 1977b2807792SGleb Smirnoff struct epoch_tracker et; 1978d148c2a2SMatt Joras struct ifvlantrunk *trunk; 197975ee267cSGleb Smirnoff struct ifvlan *ifv; 198075ee267cSGleb Smirnoff 1981d148c2a2SMatt Joras VLAN_SLOCK(); 1982d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1983d148c2a2SMatt Joras if (trunk == NULL) { 1984d148c2a2SMatt Joras VLAN_SUNLOCK(); 1985d148c2a2SMatt Joras return; 1986d148c2a2SMatt Joras } 1987b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 1988b8a6e03fSGleb Smirnoff VLAN_FOREACH(ifv, trunk) 198975ee267cSGleb Smirnoff vlan_capabilities(ifv); 1990b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 1991d148c2a2SMatt Joras VLAN_SUNLOCK(); 1992127d7b2dSAndre Oppermann } 1993127d7b2dSAndre Oppermann 1994a3814acfSSam Leffler static int 1995cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 19962cc2df49SGarrett Wollman { 19972cc2df49SGarrett Wollman struct ifnet *p; 19982cc2df49SGarrett Wollman struct ifreq *ifr; 19992884a936SJohn Baldwin #ifdef INET 2000e4cd31ddSJeff Roberson struct ifaddr *ifa; 20012884a936SJohn Baldwin #endif 20022cc2df49SGarrett Wollman struct ifvlan *ifv; 20032d222cb7SAlexander Motin struct ifvlantrunk *trunk; 20042cc2df49SGarrett Wollman struct vlanreq vlr; 200584becee1SAlexander Motin int error = 0, oldmtu; 20062cc2df49SGarrett Wollman 20072cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 20082884a936SJohn Baldwin #ifdef INET 2009e4cd31ddSJeff Roberson ifa = (struct ifaddr *) data; 20102884a936SJohn Baldwin #endif 20112cc2df49SGarrett Wollman ifv = ifp->if_softc; 20122cc2df49SGarrett Wollman 20132cc2df49SGarrett Wollman switch (cmd) { 2014e4cd31ddSJeff Roberson case SIOCSIFADDR: 2015e4cd31ddSJeff Roberson ifp->if_flags |= IFF_UP; 2016e4cd31ddSJeff Roberson #ifdef INET 2017e4cd31ddSJeff Roberson if (ifa->ifa_addr->sa_family == AF_INET) 2018e4cd31ddSJeff Roberson arp_ifinit(ifp, ifa); 2019e4cd31ddSJeff Roberson #endif 2020e4cd31ddSJeff Roberson break; 2021e4cd31ddSJeff Roberson case SIOCGIFADDR: 202238d958a6SBrooks Davis bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 202338d958a6SBrooks Davis ifp->if_addrlen); 2024e4cd31ddSJeff Roberson break; 2025b3cca108SBill Fenner case SIOCGIFMEDIA: 2026d148c2a2SMatt Joras VLAN_SLOCK(); 202775ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 2028d8564efdSEd Maste p = PARENT(ifv); 20299bcf3ae4SAlexander Motin if_ref(p); 2030d8564efdSEd Maste error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 20319bcf3ae4SAlexander Motin if_rele(p); 2032b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 2033b3cca108SBill Fenner if (error == 0) { 2034b3cca108SBill Fenner struct ifmediareq *ifmr; 2035b3cca108SBill Fenner 2036b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 2037b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 2038b3cca108SBill Fenner ifmr->ifm_count = 1; 2039b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 2040b3cca108SBill Fenner ifmr->ifm_ulist, 2041b3cca108SBill Fenner sizeof(int)); 2042b3cca108SBill Fenner } 2043b3cca108SBill Fenner } 20444faedfe8SSam Leffler } else { 2045b3cca108SBill Fenner error = EINVAL; 20464faedfe8SSam Leffler } 2047d148c2a2SMatt Joras VLAN_SUNLOCK(); 2048b3cca108SBill Fenner break; 2049b3cca108SBill Fenner 2050b3cca108SBill Fenner case SIOCSIFMEDIA: 2051b3cca108SBill Fenner error = EINVAL; 2052b3cca108SBill Fenner break; 2053b3cca108SBill Fenner 20542cc2df49SGarrett Wollman case SIOCSIFMTU: 20552cc2df49SGarrett Wollman /* 20562cc2df49SGarrett Wollman * Set the interface MTU. 20572cc2df49SGarrett Wollman */ 2058d148c2a2SMatt Joras VLAN_SLOCK(); 2059d148c2a2SMatt Joras trunk = TRUNK(ifv); 2060d148c2a2SMatt Joras if (trunk != NULL) { 2061d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 2062a3814acfSSam Leffler if (ifr->ifr_mtu > 206375ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 2064a3814acfSSam Leffler ifr->ifr_mtu < 2065a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 20662cc2df49SGarrett Wollman error = EINVAL; 2067a3814acfSSam Leffler else 20682cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 2069d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 2070a3814acfSSam Leffler } else 2071a3814acfSSam Leffler error = EINVAL; 2072d148c2a2SMatt Joras VLAN_SUNLOCK(); 20732cc2df49SGarrett Wollman break; 20742cc2df49SGarrett Wollman 20752cc2df49SGarrett Wollman case SIOCSETVLAN: 2076ccf7ba97SMarko Zec #ifdef VIMAGE 207715f6780eSRobert Watson /* 207815f6780eSRobert Watson * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN 207915f6780eSRobert Watson * interface to be delegated to a jail without allowing the 208015f6780eSRobert Watson * jail to change what underlying interface/VID it is 208115f6780eSRobert Watson * associated with. We are not entirely convinced that this 20825a39f779SRobert Watson * is the right way to accomplish that policy goal. 208315f6780eSRobert Watson */ 2084ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 2085ccf7ba97SMarko Zec error = EPERM; 2086ccf7ba97SMarko Zec break; 2087ccf7ba97SMarko Zec } 2088ccf7ba97SMarko Zec #endif 2089541d96aaSBrooks Davis error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr)); 20902cc2df49SGarrett Wollman if (error) 20912cc2df49SGarrett Wollman break; 20922cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 2093f731f104SBill Paul vlan_unconfig(ifp); 20942cc2df49SGarrett Wollman break; 20952cc2df49SGarrett Wollman } 20969bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 20971bdc73d3SEd Maste if (p == NULL) { 20982cc2df49SGarrett Wollman error = ENOENT; 20992cc2df49SGarrett Wollman break; 21002cc2df49SGarrett Wollman } 2101afbb64f1SAlexander V. Chernikov #ifdef COMPAT_FREEBSD12 2102afbb64f1SAlexander V. Chernikov if (vlr.vlr_proto == 0) 2103afbb64f1SAlexander V. Chernikov vlr.vlr_proto = ETHERTYPE_VLAN; 2104afbb64f1SAlexander V. Chernikov #endif 210584becee1SAlexander Motin oldmtu = ifp->if_mtu; 2106c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto); 21079bcf3ae4SAlexander Motin if_rele(p); 210884becee1SAlexander Motin 210984becee1SAlexander Motin /* 211084becee1SAlexander Motin * VLAN MTU may change during addition of the vlandev. 211184becee1SAlexander Motin * If it did, do network layer specific procedure. 211284becee1SAlexander Motin */ 211384becee1SAlexander Motin if (ifp->if_mtu != oldmtu) { 211484becee1SAlexander Motin #ifdef INET6 211584becee1SAlexander Motin nd6_setmtu(ifp); 211684becee1SAlexander Motin #endif 211784becee1SAlexander Motin rt_updatemtu(ifp); 211884becee1SAlexander Motin } 21192cc2df49SGarrett Wollman break; 21202cc2df49SGarrett Wollman 21212cc2df49SGarrett Wollman case SIOCGETVLAN: 2122ccf7ba97SMarko Zec #ifdef VIMAGE 2123ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 2124ccf7ba97SMarko Zec error = EPERM; 2125ccf7ba97SMarko Zec break; 2126ccf7ba97SMarko Zec } 2127ccf7ba97SMarko Zec #endif 212815a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 2129d148c2a2SMatt Joras VLAN_SLOCK(); 213075ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 213175ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 21329bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 21337983103aSRobert Watson vlr.vlr_tag = ifv->ifv_vid; 2134c7cffd65SAlexander V. Chernikov vlr.vlr_proto = ifv->ifv_proto; 21352cc2df49SGarrett Wollman } 2136d148c2a2SMatt Joras VLAN_SUNLOCK(); 2137541d96aaSBrooks Davis error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr)); 21382cc2df49SGarrett Wollman break; 21392cc2df49SGarrett Wollman 21402cc2df49SGarrett Wollman case SIOCSIFFLAGS: 21412cc2df49SGarrett Wollman /* 21421cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 21431cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 21442cc2df49SGarrett Wollman */ 2145d148c2a2SMatt Joras VLAN_XLOCK(); 214675ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 21471cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 2148d148c2a2SMatt Joras VLAN_XUNLOCK(); 21492cc2df49SGarrett Wollman break; 2150a3814acfSSam Leffler 2151f731f104SBill Paul case SIOCADDMULTI: 2152f731f104SBill Paul case SIOCDELMULTI: 215375ee267cSGleb Smirnoff /* 215475ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 215575ee267cSGleb Smirnoff * when we do. 2156d148c2a2SMatt Joras * 2157d148c2a2SMatt Joras * XXX We need the rmlock here to avoid sleeping while 2158d148c2a2SMatt Joras * holding in6_multi_mtx. 215975ee267cSGleb Smirnoff */ 2160b08d611dSMatt Macy VLAN_XLOCK(); 21612d222cb7SAlexander Motin trunk = TRUNK(ifv); 2162b08d611dSMatt Macy if (trunk != NULL) 2163f731f104SBill Paul error = vlan_setmulti(ifp); 2164b08d611dSMatt Macy VLAN_XUNLOCK(); 216575ee267cSGleb Smirnoff 2166b08d611dSMatt Macy break; 21672ccbbd06SMarcelo Araujo case SIOCGVLANPCP: 21682ccbbd06SMarcelo Araujo #ifdef VIMAGE 21692ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 21702ccbbd06SMarcelo Araujo error = EPERM; 21712ccbbd06SMarcelo Araujo break; 21722ccbbd06SMarcelo Araujo } 21732ccbbd06SMarcelo Araujo #endif 21742ccbbd06SMarcelo Araujo ifr->ifr_vlan_pcp = ifv->ifv_pcp; 21752ccbbd06SMarcelo Araujo break; 21762ccbbd06SMarcelo Araujo 21772ccbbd06SMarcelo Araujo case SIOCSVLANPCP: 21782ccbbd06SMarcelo Araujo #ifdef VIMAGE 21792ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 21802ccbbd06SMarcelo Araujo error = EPERM; 21812ccbbd06SMarcelo Araujo break; 21822ccbbd06SMarcelo Araujo } 21832ccbbd06SMarcelo Araujo #endif 21842ccbbd06SMarcelo Araujo error = priv_check(curthread, PRIV_NET_SETVLANPCP); 21852ccbbd06SMarcelo Araujo if (error) 21862ccbbd06SMarcelo Araujo break; 21879ef8cd0bSKristof Provost if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) { 21882ccbbd06SMarcelo Araujo error = EINVAL; 21892ccbbd06SMarcelo Araujo break; 21902ccbbd06SMarcelo Araujo } 21912ccbbd06SMarcelo Araujo ifv->ifv_pcp = ifr->ifr_vlan_pcp; 219232a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 21934a381a9eSHans Petter Selasky /* broadcast event about PCP change */ 21944a381a9eSHans Petter Selasky EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 21952ccbbd06SMarcelo Araujo break; 21962ccbbd06SMarcelo Araujo 2197d89baa5aSAlexander Motin case SIOCSIFCAP: 2198d148c2a2SMatt Joras VLAN_SLOCK(); 2199d89baa5aSAlexander Motin ifv->ifv_capenable = ifr->ifr_reqcap; 2200d89baa5aSAlexander Motin trunk = TRUNK(ifv); 22016dcec895SGleb Smirnoff if (trunk != NULL) { 22026dcec895SGleb Smirnoff struct epoch_tracker et; 22036dcec895SGleb Smirnoff 22046dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 2205d89baa5aSAlexander Motin vlan_capabilities(ifv); 22066dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 22076dcec895SGleb Smirnoff } 2208d148c2a2SMatt Joras VLAN_SUNLOCK(); 2209d89baa5aSAlexander Motin break; 2210d89baa5aSAlexander Motin 22112cc2df49SGarrett Wollman default: 2212e4cd31ddSJeff Roberson error = EINVAL; 2213e4cd31ddSJeff Roberson break; 22142cc2df49SGarrett Wollman } 221515a66c21SBruce M Simpson 221615a66c21SBruce M Simpson return (error); 22172cc2df49SGarrett Wollman } 2218f3e7afe2SHans Petter Selasky 2219b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 2220f3e7afe2SHans Petter Selasky static int 2221f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp, 2222f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *params, 2223f3e7afe2SHans Petter Selasky struct m_snd_tag **ppmt) 2224f3e7afe2SHans Petter Selasky { 2225fb3bc596SJohn Baldwin struct epoch_tracker et; 2226c782ea8bSJohn Baldwin const struct if_snd_tag_sw *sw; 2227fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2228fb3bc596SJohn Baldwin struct ifvlan *ifv; 2229fb3bc596SJohn Baldwin struct ifnet *parent; 2230892eded5SHans Petter Selasky struct m_snd_tag *mst; 2231fb3bc596SJohn Baldwin int error; 2232f3e7afe2SHans Petter Selasky 2233892eded5SHans Petter Selasky NET_EPOCH_ENTER(et); 2234892eded5SHans Petter Selasky ifv = ifp->if_softc; 2235892eded5SHans Petter Selasky 2236c782ea8bSJohn Baldwin switch (params->hdr.type) { 2237c782ea8bSJohn Baldwin #ifdef RATELIMIT 2238c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_UNLIMITED: 2239c782ea8bSJohn Baldwin sw = &vlan_snd_tag_ul_sw; 2240c782ea8bSJohn Baldwin break; 2241c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_RATE_LIMIT: 2242c782ea8bSJohn Baldwin sw = &vlan_snd_tag_rl_sw; 2243c782ea8bSJohn Baldwin break; 2244c782ea8bSJohn Baldwin #endif 2245c782ea8bSJohn Baldwin #ifdef KERN_TLS 2246c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_TLS: 2247c782ea8bSJohn Baldwin sw = &vlan_snd_tag_tls_sw; 2248c782ea8bSJohn Baldwin break; 2249892eded5SHans Petter Selasky case IF_SND_TAG_TYPE_TLS_RX: 2250892eded5SHans Petter Selasky sw = NULL; 2251892eded5SHans Petter Selasky if (params->tls_rx.vlan_id != 0) 2252892eded5SHans Petter Selasky goto failure; 2253892eded5SHans Petter Selasky params->tls_rx.vlan_id = ifv->ifv_vid; 2254892eded5SHans Petter Selasky break; 2255c782ea8bSJohn Baldwin #ifdef RATELIMIT 2256c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_TLS_RATE_LIMIT: 2257c782ea8bSJohn Baldwin sw = &vlan_snd_tag_tls_rl_sw; 2258c782ea8bSJohn Baldwin break; 2259c782ea8bSJohn Baldwin #endif 2260c782ea8bSJohn Baldwin #endif 2261c782ea8bSJohn Baldwin default: 2262892eded5SHans Petter Selasky goto failure; 2263c782ea8bSJohn Baldwin } 2264c782ea8bSJohn Baldwin 2265fb3bc596SJohn Baldwin if (ifv->ifv_trunk != NULL) 2266fb3bc596SJohn Baldwin parent = PARENT(ifv); 2267fb3bc596SJohn Baldwin else 2268fb3bc596SJohn Baldwin parent = NULL; 2269892eded5SHans Petter Selasky if (parent == NULL) 2270892eded5SHans Petter Selasky goto failure; 2271fb3bc596SJohn Baldwin if_ref(parent); 2272fb3bc596SJohn Baldwin NET_EPOCH_EXIT(et); 2273fb3bc596SJohn Baldwin 2274892eded5SHans Petter Selasky if (sw != NULL) { 2275fb3bc596SJohn Baldwin vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT); 2276fb3bc596SJohn Baldwin if (vst == NULL) { 2277fb3bc596SJohn Baldwin if_rele(parent); 2278fb3bc596SJohn Baldwin return (ENOMEM); 2279fb3bc596SJohn Baldwin } 2280892eded5SHans Petter Selasky } else 2281892eded5SHans Petter Selasky vst = NULL; 2282fb3bc596SJohn Baldwin 2283892eded5SHans Petter Selasky error = m_snd_tag_alloc(parent, params, &mst); 2284fb3bc596SJohn Baldwin if_rele(parent); 2285fb3bc596SJohn Baldwin if (error) { 2286fb3bc596SJohn Baldwin free(vst, M_VLAN); 2287fb3bc596SJohn Baldwin return (error); 2288fb3bc596SJohn Baldwin } 2289fb3bc596SJohn Baldwin 2290892eded5SHans Petter Selasky if (sw != NULL) { 2291c782ea8bSJohn Baldwin m_snd_tag_init(&vst->com, ifp, sw); 2292892eded5SHans Petter Selasky vst->tag = mst; 2293fb3bc596SJohn Baldwin 2294fb3bc596SJohn Baldwin *ppmt = &vst->com; 2295892eded5SHans Petter Selasky } else 2296892eded5SHans Petter Selasky *ppmt = mst; 2297892eded5SHans Petter Selasky 2298fb3bc596SJohn Baldwin return (0); 2299892eded5SHans Petter Selasky failure: 2300892eded5SHans Petter Selasky NET_EPOCH_EXIT(et); 2301892eded5SHans Petter Selasky return (EOPNOTSUPP); 2302fb3bc596SJohn Baldwin } 2303fb3bc596SJohn Baldwin 23041a714ff2SRandall Stewart static struct m_snd_tag * 23051a714ff2SRandall Stewart vlan_next_snd_tag(struct m_snd_tag *mst) 23061a714ff2SRandall Stewart { 23071a714ff2SRandall Stewart struct vlan_snd_tag *vst; 23081a714ff2SRandall Stewart 23091a714ff2SRandall Stewart vst = mst_to_vst(mst); 23101a714ff2SRandall Stewart return (vst->tag); 23111a714ff2SRandall Stewart } 23121a714ff2SRandall Stewart 2313fb3bc596SJohn Baldwin static int 2314fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst, 2315fb3bc596SJohn Baldwin union if_snd_tag_modify_params *params) 2316fb3bc596SJohn Baldwin { 2317fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2318fb3bc596SJohn Baldwin 2319fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2320c782ea8bSJohn Baldwin return (vst->tag->sw->snd_tag_modify(vst->tag, params)); 2321fb3bc596SJohn Baldwin } 2322fb3bc596SJohn Baldwin 2323fb3bc596SJohn Baldwin static int 2324fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst, 2325fb3bc596SJohn Baldwin union if_snd_tag_query_params *params) 2326fb3bc596SJohn Baldwin { 2327fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2328fb3bc596SJohn Baldwin 2329fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2330c782ea8bSJohn Baldwin return (vst->tag->sw->snd_tag_query(vst->tag, params)); 2331f3e7afe2SHans Petter Selasky } 2332fa91f845SRandall Stewart 2333fa91f845SRandall Stewart static void 2334fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst) 2335fa91f845SRandall Stewart { 2336fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2337fb3bc596SJohn Baldwin 2338fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2339fb3bc596SJohn Baldwin m_snd_tag_rele(vst->tag); 2340fb3bc596SJohn Baldwin free(vst, M_VLAN); 2341fa91f845SRandall Stewart } 23421a714ff2SRandall Stewart 23431a714ff2SRandall Stewart static void 23441a714ff2SRandall Stewart vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q) 23451a714ff2SRandall Stewart { 23461a714ff2SRandall Stewart /* 23471a714ff2SRandall Stewart * For vlan, we have an indirect 23481a714ff2SRandall Stewart * interface. The caller needs to 23491a714ff2SRandall Stewart * get a ratelimit tag on the actual 23501a714ff2SRandall Stewart * interface the flow will go on. 23511a714ff2SRandall Stewart */ 23521a714ff2SRandall Stewart q->rate_table = NULL; 23531a714ff2SRandall Stewart q->flags = RT_IS_INDIRECT; 23541a714ff2SRandall Stewart q->max_flows = 0; 23551a714ff2SRandall Stewart q->number_of_rates = 0; 23561a714ff2SRandall Stewart } 23571a714ff2SRandall Stewart 2358f3e7afe2SHans Petter Selasky #endif 2359