1c398230bSWarner Losh /*- 22cc2df49SGarrett Wollman * Copyright 1998 Massachusetts Institute of Technology 32ccbbd06SMarcelo Araujo * Copyright 2012 ADARA Networks, Inc. 4d148c2a2SMatt Joras * Copyright 2017 Dell EMC Isilon 52ccbbd06SMarcelo Araujo * 62ccbbd06SMarcelo Araujo * Portions of this software were developed by Robert N. M. Watson under 72ccbbd06SMarcelo Araujo * contract to ADARA Networks, Inc. 82cc2df49SGarrett Wollman * 92cc2df49SGarrett Wollman * Permission to use, copy, modify, and distribute this software and 102cc2df49SGarrett Wollman * its documentation for any purpose and without fee is hereby 112cc2df49SGarrett Wollman * granted, provided that both the above copyright notice and this 122cc2df49SGarrett Wollman * permission notice appear in all copies, that both the above 132cc2df49SGarrett Wollman * copyright notice and this permission notice appear in all 142cc2df49SGarrett Wollman * supporting documentation, and that the name of M.I.T. not be used 152cc2df49SGarrett Wollman * in advertising or publicity pertaining to distribution of the 162cc2df49SGarrett Wollman * software without specific, written prior permission. M.I.T. makes 172cc2df49SGarrett Wollman * no representations about the suitability of this software for any 182cc2df49SGarrett Wollman * purpose. It is provided "as is" without express or implied 192cc2df49SGarrett Wollman * warranty. 202cc2df49SGarrett Wollman * 212cc2df49SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 222cc2df49SGarrett Wollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 232cc2df49SGarrett Wollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 242cc2df49SGarrett Wollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 252cc2df49SGarrett Wollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 262cc2df49SGarrett Wollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 272cc2df49SGarrett Wollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 282cc2df49SGarrett Wollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 292cc2df49SGarrett Wollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 302cc2df49SGarrett Wollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 312cc2df49SGarrett Wollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 322cc2df49SGarrett Wollman * SUCH DAMAGE. 332cc2df49SGarrett Wollman */ 342cc2df49SGarrett Wollman 352cc2df49SGarrett Wollman /* 362cc2df49SGarrett Wollman * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 372ccbbd06SMarcelo Araujo * This is sort of sneaky in the implementation, since 382cc2df49SGarrett Wollman * we need to pretend to be enough of an Ethernet implementation 392cc2df49SGarrett Wollman * to make arp work. The way we do this is by telling everyone 402cc2df49SGarrett Wollman * that we are an Ethernet, and then catch the packets that 41d9b1d615SJohn Baldwin * ether_output() sends to us via if_transmit(), rewrite them for 42d9b1d615SJohn Baldwin * use by the real outgoing interface, and ask it to send them. 432cc2df49SGarrett Wollman */ 442cc2df49SGarrett Wollman 458b2d9181SPyun YongHyeon #include <sys/cdefs.h> 468b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$"); 478b2d9181SPyun YongHyeon 482c5b403eSOleg Bulyzhin #include "opt_inet.h" 4984becee1SAlexander Motin #include "opt_inet6.h" 50b2e60773SJohn Baldwin #include "opt_kern_tls.h" 5175ee267cSGleb Smirnoff #include "opt_vlan.h" 52f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 532cc2df49SGarrett Wollman 542cc2df49SGarrett Wollman #include <sys/param.h> 55c3322cb9SGleb Smirnoff #include <sys/eventhandler.h> 562cc2df49SGarrett Wollman #include <sys/kernel.h> 5775ee267cSGleb Smirnoff #include <sys/lock.h> 58f731f104SBill Paul #include <sys/malloc.h> 592cc2df49SGarrett Wollman #include <sys/mbuf.h> 602b120974SPeter Wemm #include <sys/module.h> 61772b000fSAlexander V. Chernikov #include <sys/rmlock.h> 622ccbbd06SMarcelo Araujo #include <sys/priv.h> 63f731f104SBill Paul #include <sys/queue.h> 642cc2df49SGarrett Wollman #include <sys/socket.h> 652cc2df49SGarrett Wollman #include <sys/sockio.h> 662cc2df49SGarrett Wollman #include <sys/sysctl.h> 672cc2df49SGarrett Wollman #include <sys/systm.h> 68e4cd31ddSJeff Roberson #include <sys/sx.h> 69d148c2a2SMatt Joras #include <sys/taskqueue.h> 702cc2df49SGarrett Wollman 712cc2df49SGarrett Wollman #include <net/bpf.h> 722cc2df49SGarrett Wollman #include <net/ethernet.h> 732cc2df49SGarrett Wollman #include <net/if.h> 7476039bc8SGleb Smirnoff #include <net/if_var.h> 75f889d2efSBrooks Davis #include <net/if_clone.h> 762cc2df49SGarrett Wollman #include <net/if_dl.h> 772cc2df49SGarrett Wollman #include <net/if_types.h> 782cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 7984becee1SAlexander Motin #include <net/route.h> 804b79449eSBjoern A. Zeeb #include <net/vnet.h> 812cc2df49SGarrett Wollman 822c5b403eSOleg Bulyzhin #ifdef INET 832c5b403eSOleg Bulyzhin #include <netinet/in.h> 842c5b403eSOleg Bulyzhin #include <netinet/if_ether.h> 852c5b403eSOleg Bulyzhin #endif 862c5b403eSOleg Bulyzhin 8784becee1SAlexander Motin #ifdef INET6 8884becee1SAlexander Motin /* 8984becee1SAlexander Motin * XXX: declare here to avoid to include many inet6 related files.. 9084becee1SAlexander Motin * should be more generalized? 9184becee1SAlexander Motin */ 9284becee1SAlexander Motin extern void nd6_setmtu(struct ifnet *); 9384becee1SAlexander Motin #endif 9484becee1SAlexander Motin 9575ee267cSGleb Smirnoff #define VLAN_DEF_HWIDTH 4 9664a17d2eSYaroslav Tykhiy #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 9775ee267cSGleb Smirnoff 982dc879b3SYaroslav Tykhiy #define UP_AND_RUNNING(ifp) \ 992dc879b3SYaroslav Tykhiy ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 1002dc879b3SYaroslav Tykhiy 101b08d611dSMatt Macy CK_SLIST_HEAD(ifvlanhead, ifvlan); 10275ee267cSGleb Smirnoff 10375ee267cSGleb Smirnoff struct ifvlantrunk { 10475ee267cSGleb Smirnoff struct ifnet *parent; /* parent interface of this trunk */ 105b08d611dSMatt Macy struct mtx lock; 10675ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 1075cb8c31aSYaroslav Tykhiy #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 1085cb8c31aSYaroslav Tykhiy struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 10975ee267cSGleb Smirnoff #else 11075ee267cSGleb Smirnoff struct ifvlanhead *hash; /* dynamic hash-list table */ 11175ee267cSGleb Smirnoff uint16_t hmask; 11275ee267cSGleb Smirnoff uint16_t hwidth; 11375ee267cSGleb Smirnoff #endif 11475ee267cSGleb Smirnoff int refcnt; 11575ee267cSGleb Smirnoff }; 1169d4fe4b2SBrooks Davis 117b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 118fb3bc596SJohn Baldwin struct vlan_snd_tag { 119fb3bc596SJohn Baldwin struct m_snd_tag com; 120fb3bc596SJohn Baldwin struct m_snd_tag *tag; 121fb3bc596SJohn Baldwin }; 122fb3bc596SJohn Baldwin 123fb3bc596SJohn Baldwin static inline struct vlan_snd_tag * 124fb3bc596SJohn Baldwin mst_to_vst(struct m_snd_tag *mst) 125fb3bc596SJohn Baldwin { 126fb3bc596SJohn Baldwin 127fb3bc596SJohn Baldwin return (__containerof(mst, struct vlan_snd_tag, com)); 128fb3bc596SJohn Baldwin } 129fb3bc596SJohn Baldwin #endif 130fb3bc596SJohn Baldwin 131d148c2a2SMatt Joras /* 132d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk with 133d148c2a2SMatt Joras * the assumption that none will be added/removed during iteration. 134d148c2a2SMatt Joras */ 135d148c2a2SMatt Joras #ifdef VLAN_ARRAY 136d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 137d148c2a2SMatt Joras size_t _i; \ 138d148c2a2SMatt Joras for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \ 139d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i]) != NULL) 140d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 141d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 142d148c2a2SMatt Joras struct ifvlan *_next; \ 143d148c2a2SMatt Joras size_t _i; \ 144d148c2a2SMatt Joras for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \ 145b08d611dSMatt Macy CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next) 146d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 147d148c2a2SMatt Joras 148d148c2a2SMatt Joras /* 149d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk while 150d148c2a2SMatt Joras * also modifying the number of vlans on the trunk. The iteration continues 151d148c2a2SMatt Joras * until some condition is met or there are no more vlans on the trunk. 152d148c2a2SMatt Joras */ 153d148c2a2SMatt Joras #ifdef VLAN_ARRAY 154d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */ 155d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 156d148c2a2SMatt Joras size_t _i; \ 157d148c2a2SMatt Joras for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \ 158d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i])) 159d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 160d148c2a2SMatt Joras /* 161d148c2a2SMatt Joras * The hash table case is more complicated. We allow for the hash table to be 162d148c2a2SMatt Joras * modified (i.e. vlans removed) while we are iterating over it. To allow for 163d148c2a2SMatt Joras * this we must restart the iteration every time we "touch" something during 164d148c2a2SMatt Joras * the iteration, since removal will resize the hash table and invalidate our 165d148c2a2SMatt Joras * current position. If acting on the touched element causes the trunk to be 166d148c2a2SMatt Joras * emptied, then iteration also stops. 167d148c2a2SMatt Joras */ 168d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 169d148c2a2SMatt Joras size_t _i; \ 170d148c2a2SMatt Joras bool _touch = false; \ 171d148c2a2SMatt Joras for (_i = 0; \ 172d148c2a2SMatt Joras !(_cond) && _i < (1 << (_trunk)->hwidth); \ 173d148c2a2SMatt Joras _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \ 174b08d611dSMatt Macy if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \ 175d148c2a2SMatt Joras (_touch = true)) 176d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 177d148c2a2SMatt Joras 178a3814acfSSam Leffler struct vlan_mc_entry { 179e4cd31ddSJeff Roberson struct sockaddr_dl mc_addr; 180b08d611dSMatt Macy CK_SLIST_ENTRY(vlan_mc_entry) mc_entries; 181c32a9d66SHans Petter Selasky struct epoch_context mc_epoch_ctx; 182a3814acfSSam Leffler }; 183a3814acfSSam Leffler 184a3814acfSSam Leffler struct ifvlan { 18575ee267cSGleb Smirnoff struct ifvlantrunk *ifv_trunk; 186fc74a9f9SBrooks Davis struct ifnet *ifv_ifp; 18775ee267cSGleb Smirnoff #define TRUNK(ifv) ((ifv)->ifv_trunk) 188c7cffd65SAlexander V. Chernikov #define PARENT(ifv) (TRUNK(ifv)->parent) 1896667db31SAlexander V. Chernikov void *ifv_cookie; 1901cf236fbSYaroslav Tykhiy int ifv_pflags; /* special flags we have set on parent */ 191d89baa5aSAlexander Motin int ifv_capenable; 19272755d28SMark Johnston int ifv_encaplen; /* encapsulation length */ 19372755d28SMark Johnston int ifv_mtufudge; /* MTU fudged by this much */ 19472755d28SMark Johnston int ifv_mintu; /* min transmission unit */ 195c7cffd65SAlexander V. Chernikov struct ether_8021q_tag ifv_qtag; 196c7cffd65SAlexander V. Chernikov #define ifv_proto ifv_qtag.proto 197c7cffd65SAlexander V. Chernikov #define ifv_vid ifv_qtag.vid 198c7cffd65SAlexander V. Chernikov #define ifv_pcp ifv_qtag.pcp 199d148c2a2SMatt Joras struct task lladdr_task; 200b08d611dSMatt Macy CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 201c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY 202b08d611dSMatt Macy CK_SLIST_ENTRY(ifvlan) ifv_list; 203c0cb022bSYaroslav Tykhiy #endif 204a3814acfSSam Leffler }; 205a3814acfSSam Leffler 20675ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */ 2071cf236fbSYaroslav Tykhiy static struct { 2081cf236fbSYaroslav Tykhiy int flag; 2091cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 2101cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 2111cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 2121cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 2131cf236fbSYaroslav Tykhiy {0, NULL} 2141cf236fbSYaroslav Tykhiy }; 215a3814acfSSam Leffler 21678bc3d5eSKristof Provost VNET_DECLARE(int, vlan_mtag_pcp); 21778bc3d5eSKristof Provost #define V_vlan_mtag_pcp VNET(vlan_mtag_pcp) 2182ccbbd06SMarcelo Araujo 21942a58907SGleb Smirnoff static const char vlanname[] = "vlan"; 22042a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface"); 2212cc2df49SGarrett Wollman 2225cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag; 223ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag; 224f2ab9160SAndrey V. Elsukov static eventhandler_tag ifevent_tag; 2255cb8c31aSYaroslav Tykhiy 2264faedfe8SSam Leffler /* 227b08d611dSMatt Macy * if_vlan uses two module-level synchronizations primitives to allow concurrent 228b08d611dSMatt Macy * modification of vlan interfaces and (mostly) allow for vlans to be destroyed 229b08d611dSMatt Macy * while they are being used for tx/rx. To accomplish this in a way that has 230b08d611dSMatt Macy * acceptable performance and cooperation with other parts of the network stack 231b08d611dSMatt Macy * there is a non-sleepable epoch(9) and an sx(9). 23275ee267cSGleb Smirnoff * 233b08d611dSMatt Macy * The performance-sensitive paths that warrant using the epoch(9) are 234d148c2a2SMatt Joras * vlan_transmit and vlan_input. Both have to check for the vlan interface's 235d148c2a2SMatt Joras * existence using if_vlantrunk, and being in the network tx/rx paths the use 236b08d611dSMatt Macy * of an epoch(9) gives a measureable improvement in performance. 23775ee267cSGleb Smirnoff * 238d148c2a2SMatt Joras * The reason for having an sx(9) is mostly because there are still areas that 239d148c2a2SMatt Joras * must be sleepable and also have safe concurrent access to a vlan interface. 240d148c2a2SMatt Joras * Since the sx(9) exists, it is used by default in most paths unless sleeping 241d148c2a2SMatt Joras * is not permitted, or if it is not clear whether sleeping is permitted. 242d148c2a2SMatt Joras * 2434faedfe8SSam Leffler */ 244d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx 245d148c2a2SMatt Joras 246d148c2a2SMatt Joras static struct sx _VLAN_SX_ID; 247d148c2a2SMatt Joras 248d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \ 249c7cffd65SAlexander V. Chernikov sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE) 250d148c2a2SMatt Joras 251d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \ 252d148c2a2SMatt Joras sx_destroy(&_VLAN_SX_ID) 253d148c2a2SMatt Joras 254d148c2a2SMatt Joras #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID) 255d148c2a2SMatt Joras #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID) 256d148c2a2SMatt Joras #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID) 257d148c2a2SMatt Joras #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID) 258d148c2a2SMatt Joras #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED) 259d148c2a2SMatt Joras #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED) 260d148c2a2SMatt Joras #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED) 261d148c2a2SMatt Joras 262d148c2a2SMatt Joras /* 263b08d611dSMatt Macy * We also have a per-trunk mutex that should be acquired when changing 264b08d611dSMatt Macy * its state. 265d148c2a2SMatt Joras */ 266b08d611dSMatt Macy #define TRUNK_LOCK_INIT(trunk) mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF) 267b08d611dSMatt Macy #define TRUNK_LOCK_DESTROY(trunk) mtx_destroy(&(trunk)->lock) 268b08d611dSMatt Macy #define TRUNK_WLOCK(trunk) mtx_lock(&(trunk)->lock) 269b08d611dSMatt Macy #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock) 270b08d611dSMatt Macy #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED); 27175ee267cSGleb Smirnoff 272d148c2a2SMatt Joras /* 273d148c2a2SMatt Joras * The VLAN_ARRAY substitutes the dynamic hash with a static array 274d148c2a2SMatt Joras * with 4096 entries. In theory this can give a boost in processing, 275d148c2a2SMatt Joras * however in practice it does not. Probably this is because the array 276d148c2a2SMatt Joras * is too big to fit into CPU cache. 277d148c2a2SMatt Joras */ 27875ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 27975ee267cSGleb Smirnoff static void vlan_inithash(struct ifvlantrunk *trunk); 28075ee267cSGleb Smirnoff static void vlan_freehash(struct ifvlantrunk *trunk); 28175ee267cSGleb Smirnoff static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 28275ee267cSGleb Smirnoff static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 28375ee267cSGleb Smirnoff static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 28475ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 2857983103aSRobert Watson uint16_t vid); 28675ee267cSGleb Smirnoff #endif 28775ee267cSGleb Smirnoff static void trunk_destroy(struct ifvlantrunk *trunk); 2884faedfe8SSam Leffler 289114c608cSYaroslav Tykhiy static void vlan_init(void *foo); 290a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 291cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 292b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 293f3e7afe2SHans Petter Selasky static int vlan_snd_tag_alloc(struct ifnet *, 294f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *, struct m_snd_tag **); 295fb3bc596SJohn Baldwin static int vlan_snd_tag_modify(struct m_snd_tag *, 296fb3bc596SJohn Baldwin union if_snd_tag_modify_params *); 297fb3bc596SJohn Baldwin static int vlan_snd_tag_query(struct m_snd_tag *, 298fb3bc596SJohn Baldwin union if_snd_tag_query_params *); 299fa91f845SRandall Stewart static void vlan_snd_tag_free(struct m_snd_tag *); 3001a714ff2SRandall Stewart static struct m_snd_tag *vlan_next_snd_tag(struct m_snd_tag *); 3011a714ff2SRandall Stewart static void vlan_ratelimit_query(struct ifnet *, 3021a714ff2SRandall Stewart struct if_ratelimit_query_results *); 303f3e7afe2SHans Petter Selasky #endif 304d9b1d615SJohn Baldwin static void vlan_qflush(struct ifnet *ifp); 3051cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 3061cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 3071cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 308f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 309d9b1d615SJohn Baldwin static int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 3102e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 3112e5ff01dSLuiz Otavio O Souza static void vlan_altq_start(struct ifnet *ifp); 3122e5ff01dSLuiz Otavio O Souza static int vlan_altq_transmit(struct ifnet *ifp, struct mbuf *m); 3132e5ff01dSLuiz Otavio O Souza #endif 31416cf6bdbSMatt Joras static int vlan_output(struct ifnet *ifp, struct mbuf *m, 31516cf6bdbSMatt Joras const struct sockaddr *dst, struct route *ro); 3166f359e28SJohn Baldwin static void vlan_unconfig(struct ifnet *ifp); 31728cc4d37SJohn Baldwin static void vlan_unconfig_locked(struct ifnet *ifp, int departing); 318c7cffd65SAlexander V. Chernikov static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag, 319c7cffd65SAlexander V. Chernikov uint16_t proto); 320a6fffd6cSBrooks Davis static void vlan_link_state(struct ifnet *ifp); 32175ee267cSGleb Smirnoff static void vlan_capabilities(struct ifvlan *ifv); 32275ee267cSGleb Smirnoff static void vlan_trunk_capabilities(struct ifnet *ifp); 323f731f104SBill Paul 324f941c31aSGleb Smirnoff static struct ifnet *vlan_clone_match_ethervid(const char *, int *); 325f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 3266b7330e2SSam Leffler static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 327f889d2efSBrooks Davis static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 328f889d2efSBrooks Davis 3295cb8c31aSYaroslav Tykhiy static void vlan_ifdetach(void *arg, struct ifnet *ifp); 330ea4ca115SAndrew Thompson static void vlan_iflladdr(void *arg, struct ifnet *ifp); 331f2ab9160SAndrey V. Elsukov static void vlan_ifevent(void *arg, struct ifnet *ifp, int event); 3325cb8c31aSYaroslav Tykhiy 333d148c2a2SMatt Joras static void vlan_lladdr_fn(void *arg, int pending); 334d148c2a2SMatt Joras 33542a58907SGleb Smirnoff static struct if_clone *vlan_cloner; 3369d4fe4b2SBrooks Davis 337ccf7ba97SMarko Zec #ifdef VIMAGE 3385f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner); 339ccf7ba97SMarko Zec #define V_vlan_cloner VNET(vlan_cloner) 340ccf7ba97SMarko Zec #endif 341ccf7ba97SMarko Zec 342c782ea8bSJohn Baldwin #ifdef RATELIMIT 343c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_ul_sw = { 344c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 345c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 346c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 347c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 348c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_UNLIMITED 349c782ea8bSJohn Baldwin }; 350c782ea8bSJohn Baldwin 351c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_rl_sw = { 352c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 353c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 354c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 355c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 356c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_RATE_LIMIT 357c782ea8bSJohn Baldwin }; 358c782ea8bSJohn Baldwin #endif 359c782ea8bSJohn Baldwin 360c782ea8bSJohn Baldwin #ifdef KERN_TLS 361c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_tls_sw = { 362c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 363c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 364c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 365c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 366c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_TLS 367c782ea8bSJohn Baldwin }; 368c782ea8bSJohn Baldwin 369c782ea8bSJohn Baldwin #ifdef RATELIMIT 370c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_tls_rl_sw = { 371c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 372c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 373c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 374c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 375c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT 376c782ea8bSJohn Baldwin }; 377c782ea8bSJohn Baldwin #endif 378c782ea8bSJohn Baldwin #endif 379c782ea8bSJohn Baldwin 38075ee267cSGleb Smirnoff static void 381c32a9d66SHans Petter Selasky vlan_mc_free(struct epoch_context *ctx) 382c32a9d66SHans Petter Selasky { 383c32a9d66SHans Petter Selasky struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx); 384c32a9d66SHans Petter Selasky free(mc, M_VLAN); 385c32a9d66SHans Petter Selasky } 386c32a9d66SHans Petter Selasky 387cac30248SOleg Bulyzhin #ifndef VLAN_ARRAY 388cac30248SOleg Bulyzhin #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 389cac30248SOleg Bulyzhin 390c32a9d66SHans Petter Selasky static void 39175ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk) 39275ee267cSGleb Smirnoff { 39375ee267cSGleb Smirnoff int i, n; 39475ee267cSGleb Smirnoff 39575ee267cSGleb Smirnoff /* 39675ee267cSGleb Smirnoff * The trunk must not be locked here since we call malloc(M_WAITOK). 39775ee267cSGleb Smirnoff * It is OK in case this function is called before the trunk struct 39875ee267cSGleb Smirnoff * gets hooked up and becomes visible from other threads. 39975ee267cSGleb Smirnoff */ 40075ee267cSGleb Smirnoff 40175ee267cSGleb Smirnoff KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 40275ee267cSGleb Smirnoff ("%s: hash already initialized", __func__)); 40375ee267cSGleb Smirnoff 40475ee267cSGleb Smirnoff trunk->hwidth = VLAN_DEF_HWIDTH; 40575ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 40675ee267cSGleb Smirnoff trunk->hmask = n - 1; 40775ee267cSGleb Smirnoff trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 40875ee267cSGleb Smirnoff for (i = 0; i < n; i++) 409b08d611dSMatt Macy CK_SLIST_INIT(&trunk->hash[i]); 41075ee267cSGleb Smirnoff } 41175ee267cSGleb Smirnoff 41275ee267cSGleb Smirnoff static void 41375ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk) 41475ee267cSGleb Smirnoff { 41575ee267cSGleb Smirnoff #ifdef INVARIANTS 41675ee267cSGleb Smirnoff int i; 41775ee267cSGleb Smirnoff 41875ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 41975ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 420b08d611dSMatt Macy KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]), 42175ee267cSGleb Smirnoff ("%s: hash table not empty", __func__)); 42275ee267cSGleb Smirnoff #endif 42375ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 42475ee267cSGleb Smirnoff trunk->hash = NULL; 42575ee267cSGleb Smirnoff trunk->hwidth = trunk->hmask = 0; 42675ee267cSGleb Smirnoff } 42775ee267cSGleb Smirnoff 42875ee267cSGleb Smirnoff static int 42975ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 43075ee267cSGleb Smirnoff { 43175ee267cSGleb Smirnoff int i, b; 43275ee267cSGleb Smirnoff struct ifvlan *ifv2; 43375ee267cSGleb Smirnoff 434b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 43575ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 43675ee267cSGleb Smirnoff 43775ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 4387983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 439b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 4407983103aSRobert Watson if (ifv->ifv_vid == ifv2->ifv_vid) 44175ee267cSGleb Smirnoff return (EEXIST); 44275ee267cSGleb Smirnoff 44375ee267cSGleb Smirnoff /* 44475ee267cSGleb Smirnoff * Grow the hash when the number of vlans exceeds half of the number of 44575ee267cSGleb Smirnoff * hash buckets squared. This will make the average linked-list length 44675ee267cSGleb Smirnoff * buckets/2. 44775ee267cSGleb Smirnoff */ 44875ee267cSGleb Smirnoff if (trunk->refcnt > (b * b) / 2) { 44975ee267cSGleb Smirnoff vlan_growhash(trunk, 1); 4507983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 45175ee267cSGleb Smirnoff } 452b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 45375ee267cSGleb Smirnoff trunk->refcnt++; 45475ee267cSGleb Smirnoff 45575ee267cSGleb Smirnoff return (0); 45675ee267cSGleb Smirnoff } 45775ee267cSGleb Smirnoff 45875ee267cSGleb Smirnoff static int 45975ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 46075ee267cSGleb Smirnoff { 46175ee267cSGleb Smirnoff int i, b; 46275ee267cSGleb Smirnoff struct ifvlan *ifv2; 46375ee267cSGleb Smirnoff 464b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 46575ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 46675ee267cSGleb Smirnoff 46775ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 4687983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 469b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 47075ee267cSGleb Smirnoff if (ifv2 == ifv) { 47175ee267cSGleb Smirnoff trunk->refcnt--; 472b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list); 47375ee267cSGleb Smirnoff if (trunk->refcnt < (b * b) / 2) 47475ee267cSGleb Smirnoff vlan_growhash(trunk, -1); 47575ee267cSGleb Smirnoff return (0); 47675ee267cSGleb Smirnoff } 47775ee267cSGleb Smirnoff 47875ee267cSGleb Smirnoff panic("%s: vlan not found\n", __func__); 47975ee267cSGleb Smirnoff return (ENOENT); /*NOTREACHED*/ 48075ee267cSGleb Smirnoff } 48175ee267cSGleb Smirnoff 48275ee267cSGleb Smirnoff /* 48375ee267cSGleb Smirnoff * Grow the hash larger or smaller if memory permits. 48475ee267cSGleb Smirnoff */ 48575ee267cSGleb Smirnoff static void 48675ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 48775ee267cSGleb Smirnoff { 48875ee267cSGleb Smirnoff struct ifvlan *ifv; 48975ee267cSGleb Smirnoff struct ifvlanhead *hash2; 49075ee267cSGleb Smirnoff int hwidth2, i, j, n, n2; 49175ee267cSGleb Smirnoff 492b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 49375ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 49475ee267cSGleb Smirnoff 49575ee267cSGleb Smirnoff if (howmuch == 0) { 49675ee267cSGleb Smirnoff /* Harmless yet obvious coding error */ 49775ee267cSGleb Smirnoff printf("%s: howmuch is 0\n", __func__); 49875ee267cSGleb Smirnoff return; 49975ee267cSGleb Smirnoff } 50075ee267cSGleb Smirnoff 50175ee267cSGleb Smirnoff hwidth2 = trunk->hwidth + howmuch; 50275ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 50375ee267cSGleb Smirnoff n2 = 1 << hwidth2; 50475ee267cSGleb Smirnoff /* Do not shrink the table below the default */ 50575ee267cSGleb Smirnoff if (hwidth2 < VLAN_DEF_HWIDTH) 50675ee267cSGleb Smirnoff return; 50775ee267cSGleb Smirnoff 508b08d611dSMatt Macy hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK); 50975ee267cSGleb Smirnoff if (hash2 == NULL) { 51075ee267cSGleb Smirnoff printf("%s: out of memory -- hash size not changed\n", 51175ee267cSGleb Smirnoff __func__); 51275ee267cSGleb Smirnoff return; /* We can live with the old hash table */ 51375ee267cSGleb Smirnoff } 51475ee267cSGleb Smirnoff for (j = 0; j < n2; j++) 515b08d611dSMatt Macy CK_SLIST_INIT(&hash2[j]); 51675ee267cSGleb Smirnoff for (i = 0; i < n; i++) 517b08d611dSMatt Macy while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) { 518b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list); 5197983103aSRobert Watson j = HASH(ifv->ifv_vid, n2 - 1); 520b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 52175ee267cSGleb Smirnoff } 522b08d611dSMatt Macy NET_EPOCH_WAIT(); 52375ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 52475ee267cSGleb Smirnoff trunk->hash = hash2; 52575ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 52675ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 527f84b2d69SYaroslav Tykhiy 528f84b2d69SYaroslav Tykhiy if (bootverbose) 529f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 530f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 53175ee267cSGleb Smirnoff } 53275ee267cSGleb Smirnoff 53375ee267cSGleb Smirnoff static __inline struct ifvlan * 5347983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 53575ee267cSGleb Smirnoff { 53675ee267cSGleb Smirnoff struct ifvlan *ifv; 53775ee267cSGleb Smirnoff 538a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 53975ee267cSGleb Smirnoff 540b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list) 5417983103aSRobert Watson if (ifv->ifv_vid == vid) 54275ee267cSGleb Smirnoff return (ifv); 54375ee267cSGleb Smirnoff return (NULL); 54475ee267cSGleb Smirnoff } 54575ee267cSGleb Smirnoff 54675ee267cSGleb Smirnoff #if 0 54775ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 54875ee267cSGleb Smirnoff static void 54975ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 55075ee267cSGleb Smirnoff { 55175ee267cSGleb Smirnoff int i; 55275ee267cSGleb Smirnoff struct ifvlan *ifv; 55375ee267cSGleb Smirnoff 55475ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 55575ee267cSGleb Smirnoff printf("%d: ", i); 556b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 55775ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 55875ee267cSGleb Smirnoff printf("\n"); 55975ee267cSGleb Smirnoff } 56075ee267cSGleb Smirnoff } 56175ee267cSGleb Smirnoff #endif /* 0 */ 562e4cd31ddSJeff Roberson #else 563e4cd31ddSJeff Roberson 564e4cd31ddSJeff Roberson static __inline struct ifvlan * 5657983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 566e4cd31ddSJeff Roberson { 567e4cd31ddSJeff Roberson 5687983103aSRobert Watson return trunk->vlans[vid]; 569e4cd31ddSJeff Roberson } 570e4cd31ddSJeff Roberson 571e4cd31ddSJeff Roberson static __inline int 572e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 573e4cd31ddSJeff Roberson { 574e4cd31ddSJeff Roberson 5757983103aSRobert Watson if (trunk->vlans[ifv->ifv_vid] != NULL) 576e4cd31ddSJeff Roberson return EEXIST; 5777983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = ifv; 578e4cd31ddSJeff Roberson trunk->refcnt++; 579e4cd31ddSJeff Roberson 580e4cd31ddSJeff Roberson return (0); 581e4cd31ddSJeff Roberson } 582e4cd31ddSJeff Roberson 583e4cd31ddSJeff Roberson static __inline int 584e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 585e4cd31ddSJeff Roberson { 586e4cd31ddSJeff Roberson 5877983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = NULL; 588e4cd31ddSJeff Roberson trunk->refcnt--; 589e4cd31ddSJeff Roberson 590e4cd31ddSJeff Roberson return (0); 591e4cd31ddSJeff Roberson } 592e4cd31ddSJeff Roberson 593e4cd31ddSJeff Roberson static __inline void 594e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk) 595e4cd31ddSJeff Roberson { 596e4cd31ddSJeff Roberson } 597e4cd31ddSJeff Roberson 598e4cd31ddSJeff Roberson static __inline void 599e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk) 600e4cd31ddSJeff Roberson { 601e4cd31ddSJeff Roberson } 602e4cd31ddSJeff Roberson 60375ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 60475ee267cSGleb Smirnoff 60575ee267cSGleb Smirnoff static void 60675ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 60775ee267cSGleb Smirnoff { 608d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 60975ee267cSGleb Smirnoff 61075ee267cSGleb Smirnoff vlan_freehash(trunk); 61175ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 61233499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 6139bcf3ae4SAlexander Motin if_rele(trunk->parent); 61475ee267cSGleb Smirnoff free(trunk, M_VLAN); 61575ee267cSGleb Smirnoff } 61675ee267cSGleb Smirnoff 617f731f104SBill Paul /* 618f731f104SBill Paul * Program our multicast filter. What we're actually doing is 619f731f104SBill Paul * programming the multicast filter of the parent. This has the 620f731f104SBill Paul * side effect of causing the parent interface to receive multicast 621f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 622f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 623f731f104SBill Paul * to avoid this: there really is only one physical interface. 624f731f104SBill Paul */ 6252b120974SPeter Wemm static int 6262b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 627f731f104SBill Paul { 628f731f104SBill Paul struct ifnet *ifp_p; 6292d222cb7SAlexander Motin struct ifmultiaddr *ifma; 630f731f104SBill Paul struct ifvlan *sc; 631c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 632f731f104SBill Paul int error; 633f731f104SBill Paul 634b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 635d148c2a2SMatt Joras 636f731f104SBill Paul /* Find the parent. */ 637f731f104SBill Paul sc = ifp->if_softc; 63875ee267cSGleb Smirnoff ifp_p = PARENT(sc); 6391b2a4f7aSBill Fenner 6408b615593SMarko Zec CURVNET_SET_QUIET(ifp_p->if_vnet); 6418b615593SMarko Zec 642f731f104SBill Paul /* First, remove any existing filter entries. */ 643b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 644b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 6452d222cb7SAlexander Motin (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 6462a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 647f731f104SBill Paul } 648f731f104SBill Paul 649f731f104SBill Paul /* Now program new ones. */ 6502d222cb7SAlexander Motin IF_ADDR_WLOCK(ifp); 651d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 652f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 653f731f104SBill Paul continue; 65429c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 6552d222cb7SAlexander Motin if (mc == NULL) { 6562d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 657c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 65829c2dfbeSBruce M Simpson return (ENOMEM); 6592d222cb7SAlexander Motin } 660e4cd31ddSJeff Roberson bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 661e4cd31ddSJeff Roberson mc->mc_addr.sdl_index = ifp_p->if_index; 662b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 6632d222cb7SAlexander Motin } 6642d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 665b08d611dSMatt Macy CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) { 666e4cd31ddSJeff Roberson error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 6672d222cb7SAlexander Motin NULL); 668c6b2d024SGeorge V. Neville-Neil if (error) { 669c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 670f731f104SBill Paul return (error); 671f731f104SBill Paul } 672c6b2d024SGeorge V. Neville-Neil } 673f731f104SBill Paul 6748b615593SMarko Zec CURVNET_RESTORE(); 675f731f104SBill Paul return (0); 676f731f104SBill Paul } 6772cc2df49SGarrett Wollman 678a3814acfSSam Leffler /* 679f2ab9160SAndrey V. Elsukov * A handler for interface ifnet events. 680f2ab9160SAndrey V. Elsukov */ 681f2ab9160SAndrey V. Elsukov static void 682f2ab9160SAndrey V. Elsukov vlan_ifevent(void *arg __unused, struct ifnet *ifp, int event) 683f2ab9160SAndrey V. Elsukov { 684f2ab9160SAndrey V. Elsukov struct epoch_tracker et; 685f2ab9160SAndrey V. Elsukov struct ifvlan *ifv; 686f2ab9160SAndrey V. Elsukov struct ifvlantrunk *trunk; 687f2ab9160SAndrey V. Elsukov 688f2ab9160SAndrey V. Elsukov if (event != IFNET_EVENT_UPDATE_BAUDRATE) 689f2ab9160SAndrey V. Elsukov return; 690f2ab9160SAndrey V. Elsukov 691f2ab9160SAndrey V. Elsukov NET_EPOCH_ENTER(et); 692f2ab9160SAndrey V. Elsukov trunk = ifp->if_vlantrunk; 693f2ab9160SAndrey V. Elsukov if (trunk == NULL) { 694f2ab9160SAndrey V. Elsukov NET_EPOCH_EXIT(et); 695f2ab9160SAndrey V. Elsukov return; 696f2ab9160SAndrey V. Elsukov } 697f2ab9160SAndrey V. Elsukov 698f2ab9160SAndrey V. Elsukov TRUNK_WLOCK(trunk); 699f2ab9160SAndrey V. Elsukov VLAN_FOREACH(ifv, trunk) { 700f2ab9160SAndrey V. Elsukov ifv->ifv_ifp->if_baudrate = ifp->if_baudrate; 701f2ab9160SAndrey V. Elsukov } 702f2ab9160SAndrey V. Elsukov TRUNK_WUNLOCK(trunk); 703f2ab9160SAndrey V. Elsukov NET_EPOCH_EXIT(et); 704f2ab9160SAndrey V. Elsukov } 705f2ab9160SAndrey V. Elsukov 706f2ab9160SAndrey V. Elsukov /* 707ea4ca115SAndrew Thompson * A handler for parent interface link layer address changes. 708ea4ca115SAndrew Thompson * If the parent interface link layer address is changed we 709ea4ca115SAndrew Thompson * should also change it on all children vlans. 710ea4ca115SAndrew Thompson */ 711ea4ca115SAndrew Thompson static void 712ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 713ea4ca115SAndrew Thompson { 714a68cc388SGleb Smirnoff struct epoch_tracker et; 715ea4ca115SAndrew Thompson struct ifvlan *ifv; 716d148c2a2SMatt Joras struct ifnet *ifv_ifp; 717d148c2a2SMatt Joras struct ifvlantrunk *trunk; 718d148c2a2SMatt Joras struct sockaddr_dl *sdl; 719ea4ca115SAndrew Thompson 7207b7f772fSGleb Smirnoff /* Need the epoch since this is run on taskqueue_swi. */ 721a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 722d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 723d148c2a2SMatt Joras if (trunk == NULL) { 724a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 725ea4ca115SAndrew Thompson return; 726d148c2a2SMatt Joras } 727ea4ca115SAndrew Thompson 728ea4ca115SAndrew Thompson /* 729ea4ca115SAndrew Thompson * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 730d148c2a2SMatt Joras * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR 731d148c2a2SMatt Joras * ioctl calls on the parent garbling the lladdr of the child vlan. 732ea4ca115SAndrew Thompson */ 733d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 734d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 735d148c2a2SMatt Joras /* 736d148c2a2SMatt Joras * Copy new new lladdr into the ifv_ifp, enqueue a task 737d148c2a2SMatt Joras * to actually call if_setlladdr. if_setlladdr needs to 738d148c2a2SMatt Joras * be deferred to a taskqueue because it will call into 739d148c2a2SMatt Joras * the if_vlan ioctl path and try to acquire the global 740d148c2a2SMatt Joras * lock. 741d148c2a2SMatt Joras */ 742d148c2a2SMatt Joras ifv_ifp = ifv->ifv_ifp; 743d148c2a2SMatt Joras bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp), 744e4cd31ddSJeff Roberson ifp->if_addrlen); 745d148c2a2SMatt Joras sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr; 746d148c2a2SMatt Joras sdl->sdl_alen = ifp->if_addrlen; 747d148c2a2SMatt Joras taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 7486117727bSAndrew Thompson } 749d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 750a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 751ea4ca115SAndrew Thompson } 752ea4ca115SAndrew Thompson 753ea4ca115SAndrew Thompson /* 7545cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 7555cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 7565cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 7575428776eSJohn Baldwin * by ejecting its hot-plug card. However, if an ifnet is simply 7585428776eSJohn Baldwin * being renamed, then there's no need to tear down the state. 7595cb8c31aSYaroslav Tykhiy */ 7605cb8c31aSYaroslav Tykhiy static void 7615cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 7625cb8c31aSYaroslav Tykhiy { 7635cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 764d148c2a2SMatt Joras struct ifvlantrunk *trunk; 7655cb8c31aSYaroslav Tykhiy 7665428776eSJohn Baldwin /* If the ifnet is just being renamed, don't do anything. */ 7675428776eSJohn Baldwin if (ifp->if_flags & IFF_RENAMING) 7685428776eSJohn Baldwin return; 769d148c2a2SMatt Joras VLAN_XLOCK(); 770d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 771d148c2a2SMatt Joras if (trunk == NULL) { 772d148c2a2SMatt Joras VLAN_XUNLOCK(); 773d148c2a2SMatt Joras return; 774d148c2a2SMatt Joras } 7755428776eSJohn Baldwin 7765cb8c31aSYaroslav Tykhiy /* 7775cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 7785cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 7795cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 7805cb8c31aSYaroslav Tykhiy */ 781d148c2a2SMatt Joras VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk, 782d148c2a2SMatt Joras ifp->if_vlantrunk == NULL) 78328cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 784d148c2a2SMatt Joras 7855cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 7865cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 787d148c2a2SMatt Joras VLAN_XUNLOCK(); 7885cb8c31aSYaroslav Tykhiy } 7895cb8c31aSYaroslav Tykhiy 7905cb8c31aSYaroslav Tykhiy /* 791e4cd31ddSJeff Roberson * Return the trunk device for a virtual interface. 792e4cd31ddSJeff Roberson */ 793e4cd31ddSJeff Roberson static struct ifnet * 794e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp) 795e4cd31ddSJeff Roberson { 796e4cd31ddSJeff Roberson struct ifvlan *ifv; 797e4cd31ddSJeff Roberson 798b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 799b8a6e03fSGleb Smirnoff 800e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 801e4cd31ddSJeff Roberson return (NULL); 802d148c2a2SMatt Joras 803e4cd31ddSJeff Roberson ifv = ifp->if_softc; 804e4cd31ddSJeff Roberson ifp = NULL; 805e4cd31ddSJeff Roberson if (ifv->ifv_trunk) 806e4cd31ddSJeff Roberson ifp = PARENT(ifv); 807e4cd31ddSJeff Roberson return (ifp); 808e4cd31ddSJeff Roberson } 809e4cd31ddSJeff Roberson 810e4cd31ddSJeff Roberson /* 8117983103aSRobert Watson * Return the 12-bit VLAN VID for this interface, for use by external 8127983103aSRobert Watson * components such as Infiniband. 8137983103aSRobert Watson * 8147983103aSRobert Watson * XXXRW: Note that the function name here is historical; it should be named 8157983103aSRobert Watson * vlan_vid(). 816e4cd31ddSJeff Roberson */ 817e4cd31ddSJeff Roberson static int 8187983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp) 819e4cd31ddSJeff Roberson { 820e4cd31ddSJeff Roberson struct ifvlan *ifv; 821e4cd31ddSJeff Roberson 822e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 823e4cd31ddSJeff Roberson return (EINVAL); 824e4cd31ddSJeff Roberson ifv = ifp->if_softc; 8257983103aSRobert Watson *vidp = ifv->ifv_vid; 826e4cd31ddSJeff Roberson return (0); 827e4cd31ddSJeff Roberson } 828e4cd31ddSJeff Roberson 82932d2623aSNavdeep Parhar static int 83032d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp) 83132d2623aSNavdeep Parhar { 83232d2623aSNavdeep Parhar struct ifvlan *ifv; 83332d2623aSNavdeep Parhar 83432d2623aSNavdeep Parhar if (ifp->if_type != IFT_L2VLAN) 83532d2623aSNavdeep Parhar return (EINVAL); 83632d2623aSNavdeep Parhar ifv = ifp->if_softc; 83732d2623aSNavdeep Parhar *pcpp = ifv->ifv_pcp; 83832d2623aSNavdeep Parhar return (0); 83932d2623aSNavdeep Parhar } 84032d2623aSNavdeep Parhar 841e4cd31ddSJeff Roberson /* 842e4cd31ddSJeff Roberson * Return a driver specific cookie for this interface. Synchronization 843e4cd31ddSJeff Roberson * with setcookie must be provided by the driver. 844e4cd31ddSJeff Roberson */ 845e4cd31ddSJeff Roberson static void * 846e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp) 847e4cd31ddSJeff Roberson { 848e4cd31ddSJeff Roberson struct ifvlan *ifv; 849e4cd31ddSJeff Roberson 850e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 851e4cd31ddSJeff Roberson return (NULL); 852e4cd31ddSJeff Roberson ifv = ifp->if_softc; 853e4cd31ddSJeff Roberson return (ifv->ifv_cookie); 854e4cd31ddSJeff Roberson } 855e4cd31ddSJeff Roberson 856e4cd31ddSJeff Roberson /* 857e4cd31ddSJeff Roberson * Store a cookie in our softc that drivers can use to store driver 858e4cd31ddSJeff Roberson * private per-instance data in. 859e4cd31ddSJeff Roberson */ 860e4cd31ddSJeff Roberson static int 861e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie) 862e4cd31ddSJeff Roberson { 863e4cd31ddSJeff Roberson struct ifvlan *ifv; 864e4cd31ddSJeff Roberson 865e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 866e4cd31ddSJeff Roberson return (EINVAL); 867e4cd31ddSJeff Roberson ifv = ifp->if_softc; 868e4cd31ddSJeff Roberson ifv->ifv_cookie = cookie; 869e4cd31ddSJeff Roberson return (0); 870e4cd31ddSJeff Roberson } 871e4cd31ddSJeff Roberson 872e4cd31ddSJeff Roberson /* 8737983103aSRobert Watson * Return the vlan device present at the specific VID. 874e4cd31ddSJeff Roberson */ 875e4cd31ddSJeff Roberson static struct ifnet * 8767983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid) 877e4cd31ddSJeff Roberson { 878e4cd31ddSJeff Roberson struct ifvlantrunk *trunk; 879e4cd31ddSJeff Roberson struct ifvlan *ifv; 880e4cd31ddSJeff Roberson 881b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 882b8a6e03fSGleb Smirnoff 883e4cd31ddSJeff Roberson trunk = ifp->if_vlantrunk; 884b8a6e03fSGleb Smirnoff if (trunk == NULL) 885e4cd31ddSJeff Roberson return (NULL); 886e4cd31ddSJeff Roberson ifp = NULL; 8877983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 888e4cd31ddSJeff Roberson if (ifv) 889e4cd31ddSJeff Roberson ifp = ifv->ifv_ifp; 890e4cd31ddSJeff Roberson return (ifp); 891e4cd31ddSJeff Roberson } 892e4cd31ddSJeff Roberson 893e4cd31ddSJeff Roberson /* 894a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 895a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 896a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 897a3814acfSSam Leffler * set here. No one else in the system should be aware of this so 898a3814acfSSam Leffler * we use an explicit reference here. 899a3814acfSSam Leffler */ 900a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 901a3814acfSSam Leffler 902984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 903a6fffd6cSBrooks Davis extern void (*vlan_link_state_p)(struct ifnet *); 904127d7b2dSAndre Oppermann 9052b120974SPeter Wemm static int 9062b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 9072b120974SPeter Wemm { 9089d4fe4b2SBrooks Davis 9092b120974SPeter Wemm switch (type) { 9102b120974SPeter Wemm case MOD_LOAD: 9115cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 9125cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 9135cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 9145cb8c31aSYaroslav Tykhiy return (ENOMEM); 915ea4ca115SAndrew Thompson iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 916ea4ca115SAndrew Thompson vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 917ea4ca115SAndrew Thompson if (iflladdr_tag == NULL) 918ea4ca115SAndrew Thompson return (ENOMEM); 919f2ab9160SAndrey V. Elsukov ifevent_tag = EVENTHANDLER_REGISTER(ifnet_event, 920f2ab9160SAndrey V. Elsukov vlan_ifevent, NULL, EVENTHANDLER_PRI_ANY); 921f2ab9160SAndrey V. Elsukov if (ifevent_tag == NULL) 922f2ab9160SAndrey V. Elsukov return (ENOMEM); 923d148c2a2SMatt Joras VLAN_LOCKING_INIT(); 9249d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 925127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 92675ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 927e4cd31ddSJeff Roberson vlan_trunkdev_p = vlan_trunkdev; 928e4cd31ddSJeff Roberson vlan_cookie_p = vlan_cookie; 929e4cd31ddSJeff Roberson vlan_setcookie_p = vlan_setcookie; 930e4cd31ddSJeff Roberson vlan_tag_p = vlan_tag; 93132d2623aSNavdeep Parhar vlan_pcp_p = vlan_pcp; 932e4cd31ddSJeff Roberson vlan_devat_p = vlan_devat; 933ccf7ba97SMarko Zec #ifndef VIMAGE 93442a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 93542a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 936ccf7ba97SMarko Zec #endif 93725c0f7b3SYaroslav Tykhiy if (bootverbose) 93825c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 93925c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 94025c0f7b3SYaroslav Tykhiy "full-size arrays" 94125c0f7b3SYaroslav Tykhiy #else 94225c0f7b3SYaroslav Tykhiy "hash tables with chaining" 94325c0f7b3SYaroslav Tykhiy #endif 94425c0f7b3SYaroslav Tykhiy 94525c0f7b3SYaroslav Tykhiy "\n"); 9462b120974SPeter Wemm break; 9472b120974SPeter Wemm case MOD_UNLOAD: 948ccf7ba97SMarko Zec #ifndef VIMAGE 94942a58907SGleb Smirnoff if_clone_detach(vlan_cloner); 950ccf7ba97SMarko Zec #endif 9515cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 952ea4ca115SAndrew Thompson EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 953f2ab9160SAndrey V. Elsukov EVENTHANDLER_DEREGISTER(ifnet_event, ifevent_tag); 9549d4fe4b2SBrooks Davis vlan_input_p = NULL; 955127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 95675ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 957e4cd31ddSJeff Roberson vlan_trunkdev_p = NULL; 958e4cd31ddSJeff Roberson vlan_tag_p = NULL; 95909fe6320SNavdeep Parhar vlan_cookie_p = NULL; 96009fe6320SNavdeep Parhar vlan_setcookie_p = NULL; 961e4cd31ddSJeff Roberson vlan_devat_p = NULL; 962d148c2a2SMatt Joras VLAN_LOCKING_DESTROY(); 96325c0f7b3SYaroslav Tykhiy if (bootverbose) 96425c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 9659d4fe4b2SBrooks Davis break; 9663e019deaSPoul-Henning Kamp default: 9673e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 9682b120974SPeter Wemm } 96915a66c21SBruce M Simpson return (0); 9702b120974SPeter Wemm } 9712b120974SPeter Wemm 9722b120974SPeter Wemm static moduledata_t vlan_mod = { 9732b120974SPeter Wemm "if_vlan", 9742b120974SPeter Wemm vlan_modevent, 9759823d527SKevin Lo 0 9762b120974SPeter Wemm }; 9772b120974SPeter Wemm 9782b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 97911edc477SEd Maste MODULE_VERSION(if_vlan, 3); 9802cc2df49SGarrett Wollman 981ccf7ba97SMarko Zec #ifdef VIMAGE 982ccf7ba97SMarko Zec static void 983ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused) 984ccf7ba97SMarko Zec { 985ccf7ba97SMarko Zec 98642a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 98742a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 988ccf7ba97SMarko Zec V_vlan_cloner = vlan_cloner; 989ccf7ba97SMarko Zec } 990ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 991ccf7ba97SMarko Zec vnet_vlan_init, NULL); 992ccf7ba97SMarko Zec 993ccf7ba97SMarko Zec static void 994ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused) 995ccf7ba97SMarko Zec { 996ccf7ba97SMarko Zec 99742a58907SGleb Smirnoff if_clone_detach(V_vlan_cloner); 998ccf7ba97SMarko Zec } 999eb03a443SKristof Provost VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 1000ccf7ba97SMarko Zec vnet_vlan_uninit, NULL); 1001ccf7ba97SMarko Zec #endif 1002ccf7ba97SMarko Zec 1003f941c31aSGleb Smirnoff /* 1004c7cffd65SAlexander V. Chernikov * Check for <etherif>.<vlan>[.<vlan> ...] style interface names. 1005f941c31aSGleb Smirnoff */ 1006f889d2efSBrooks Davis static struct ifnet * 1007f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp) 10089d4fe4b2SBrooks Davis { 1009f941c31aSGleb Smirnoff char ifname[IFNAMSIZ]; 1010f941c31aSGleb Smirnoff char *cp; 1011f889d2efSBrooks Davis struct ifnet *ifp; 10127983103aSRobert Watson int vid; 1013f889d2efSBrooks Davis 1014f941c31aSGleb Smirnoff strlcpy(ifname, name, IFNAMSIZ); 1015c7cffd65SAlexander V. Chernikov if ((cp = strrchr(ifname, '.')) == NULL) 1016f941c31aSGleb Smirnoff return (NULL); 1017f941c31aSGleb Smirnoff *cp = '\0'; 10189bcf3ae4SAlexander Motin if ((ifp = ifunit_ref(ifname)) == NULL) 1019f941c31aSGleb Smirnoff return (NULL); 1020f941c31aSGleb Smirnoff /* Parse VID. */ 10219bcf3ae4SAlexander Motin if (*++cp == '\0') { 10229bcf3ae4SAlexander Motin if_rele(ifp); 1023f941c31aSGleb Smirnoff return (NULL); 10249bcf3ae4SAlexander Motin } 10257983103aSRobert Watson vid = 0; 1026fb92ad4aSJohn Baldwin for(; *cp >= '0' && *cp <= '9'; cp++) 10277983103aSRobert Watson vid = (vid * 10) + (*cp - '0'); 10289bcf3ae4SAlexander Motin if (*cp != '\0') { 10299bcf3ae4SAlexander Motin if_rele(ifp); 1030f941c31aSGleb Smirnoff return (NULL); 10319bcf3ae4SAlexander Motin } 10327983103aSRobert Watson if (vidp != NULL) 10337983103aSRobert Watson *vidp = vid; 1034f889d2efSBrooks Davis 103515a66c21SBruce M Simpson return (ifp); 1036f889d2efSBrooks Davis } 1037f889d2efSBrooks Davis 1038f889d2efSBrooks Davis static int 1039f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 1040f889d2efSBrooks Davis { 10414be465abSAlexander V. Chernikov struct ifnet *ifp; 1042f889d2efSBrooks Davis const char *cp; 1043f889d2efSBrooks Davis 10444be465abSAlexander V. Chernikov ifp = vlan_clone_match_ethervid(name, NULL); 10454be465abSAlexander V. Chernikov if (ifp != NULL) { 10464be465abSAlexander V. Chernikov if_rele(ifp); 1047f889d2efSBrooks Davis return (1); 10484be465abSAlexander V. Chernikov } 1049f889d2efSBrooks Davis 105042a58907SGleb Smirnoff if (strncmp(vlanname, name, strlen(vlanname)) != 0) 1051f889d2efSBrooks Davis return (0); 1052f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 1053f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 1054f889d2efSBrooks Davis return (0); 1055f889d2efSBrooks Davis } 1056f889d2efSBrooks Davis 1057f889d2efSBrooks Davis return (1); 1058f889d2efSBrooks Davis } 1059f889d2efSBrooks Davis 1060f889d2efSBrooks Davis static int 10616b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 1062f889d2efSBrooks Davis { 1063f889d2efSBrooks Davis char *dp; 106453729367SAlexander V. Chernikov bool wildcard = false; 106553729367SAlexander V. Chernikov bool subinterface = false; 1066f889d2efSBrooks Davis int unit; 1067f889d2efSBrooks Davis int error; 106853729367SAlexander V. Chernikov int vid = 0; 106953729367SAlexander V. Chernikov uint16_t proto = ETHERTYPE_VLAN; 10709d4fe4b2SBrooks Davis struct ifvlan *ifv; 10719d4fe4b2SBrooks Davis struct ifnet *ifp; 107253729367SAlexander V. Chernikov struct ifnet *p = NULL; 10733ba24fdeSJohn Baldwin struct ifaddr *ifa; 10743ba24fdeSJohn Baldwin struct sockaddr_dl *sdl; 10756b7330e2SSam Leffler struct vlanreq vlr; 107665239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 1077f889d2efSBrooks Davis 1078c7cffd65SAlexander V. Chernikov 10796b7330e2SSam Leffler /* 108053729367SAlexander V. Chernikov * There are three ways to specify the cloned device: 10816b7330e2SSam Leffler * o pass a parameter block with the clone request. 108253729367SAlexander V. Chernikov * o specify parameters in the text of the clone device name 10836b7330e2SSam Leffler * o specify no parameters and get an unattached device that 10846b7330e2SSam Leffler * must be configured separately. 108553729367SAlexander V. Chernikov * The first technique is preferred; the latter two are supported 1086c7cffd65SAlexander V. Chernikov * for backwards compatibility. 10877983103aSRobert Watson * 10887983103aSRobert Watson * XXXRW: Note historic use of the word "tag" here. New ioctls may be 10897983103aSRobert Watson * called for. 10906b7330e2SSam Leffler */ 109153729367SAlexander V. Chernikov 10926b7330e2SSam Leffler if (params) { 10936b7330e2SSam Leffler error = copyin(params, &vlr, sizeof(vlr)); 10946b7330e2SSam Leffler if (error) 10956b7330e2SSam Leffler return error; 109653729367SAlexander V. Chernikov vid = vlr.vlr_tag; 109753729367SAlexander V. Chernikov proto = vlr.vlr_proto; 109853729367SAlexander V. Chernikov 1099afbb64f1SAlexander V. Chernikov #ifdef COMPAT_FREEBSD12 1100afbb64f1SAlexander V. Chernikov if (proto == 0) 1101afbb64f1SAlexander V. Chernikov proto = ETHERTYPE_VLAN; 1102afbb64f1SAlexander V. Chernikov #endif 11039bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 11046b7330e2SSam Leffler if (p == NULL) 1105b1828acfSGleb Smirnoff return (ENXIO); 110653729367SAlexander V. Chernikov } 110753729367SAlexander V. Chernikov 110853729367SAlexander V. Chernikov if ((error = ifc_name2unit(name, &unit)) == 0) { 110953729367SAlexander V. Chernikov 111053729367SAlexander V. Chernikov /* 111153729367SAlexander V. Chernikov * vlanX interface. Set wildcard to true if the unit number 111253729367SAlexander V. Chernikov * is not fixed (-1) 111353729367SAlexander V. Chernikov */ 111453729367SAlexander V. Chernikov wildcard = (unit < 0); 111553729367SAlexander V. Chernikov } else { 111653729367SAlexander V. Chernikov struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid); 111753729367SAlexander V. Chernikov if (p_tmp != NULL) { 111853729367SAlexander V. Chernikov error = 0; 111953729367SAlexander V. Chernikov subinterface = true; 112053729367SAlexander V. Chernikov unit = IF_DUNIT_NONE; 112153729367SAlexander V. Chernikov wildcard = false; 112253729367SAlexander V. Chernikov if (p != NULL) { 112353729367SAlexander V. Chernikov if_rele(p_tmp); 112453729367SAlexander V. Chernikov if (p != p_tmp) 112553729367SAlexander V. Chernikov error = EINVAL; 112653729367SAlexander V. Chernikov } else 112753729367SAlexander V. Chernikov p = p_tmp; 112853729367SAlexander V. Chernikov } else 112953729367SAlexander V. Chernikov error = ENXIO; 113053729367SAlexander V. Chernikov } 113153729367SAlexander V. Chernikov 11329bcf3ae4SAlexander Motin if (error != 0) { 113353729367SAlexander V. Chernikov if (p != NULL) 11349bcf3ae4SAlexander Motin if_rele(p); 11356b7330e2SSam Leffler return (error); 11369bcf3ae4SAlexander Motin } 1137f889d2efSBrooks Davis 113853729367SAlexander V. Chernikov if (!subinterface) { 113953729367SAlexander V. Chernikov /* vlanX interface, mark X as busy or allocate new unit # */ 1140f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 11419bcf3ae4SAlexander Motin if (error != 0) { 11429bcf3ae4SAlexander Motin if (p != NULL) 11439bcf3ae4SAlexander Motin if_rele(p); 1144f889d2efSBrooks Davis return (error); 11459bcf3ae4SAlexander Motin } 114653729367SAlexander V. Chernikov } 1147f889d2efSBrooks Davis 1148f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 1149f889d2efSBrooks Davis if (wildcard) { 1150f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 1151f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 1152f889d2efSBrooks Davis len - (dp-name) - 1) { 1153f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 1154f889d2efSBrooks Davis } 1155f889d2efSBrooks Davis } 11569d4fe4b2SBrooks Davis 1157a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 1158fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 1159fc74a9f9SBrooks Davis if (ifp == NULL) { 116053729367SAlexander V. Chernikov if (!subinterface) 1161fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 1162fc74a9f9SBrooks Davis free(ifv, M_VLAN); 11639bcf3ae4SAlexander Motin if (p != NULL) 11649bcf3ae4SAlexander Motin if_rele(p); 1165fc74a9f9SBrooks Davis return (ENOSPC); 1166fc74a9f9SBrooks Davis } 1167b08d611dSMatt Macy CK_SLIST_INIT(&ifv->vlan_mc_listhead); 11689d4fe4b2SBrooks Davis ifp->if_softc = ifv; 1169f889d2efSBrooks Davis /* 1170cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 1171f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 1172f889d2efSBrooks Davis */ 1173f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 117442a58907SGleb Smirnoff ifp->if_dname = vlanname; 1175f889d2efSBrooks Davis ifp->if_dunit = unit; 11769d4fe4b2SBrooks Davis 1177114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 11782e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 11792e5ff01dSLuiz Otavio O Souza ifp->if_start = vlan_altq_start; 11802e5ff01dSLuiz Otavio O Souza ifp->if_transmit = vlan_altq_transmit; 11812e5ff01dSLuiz Otavio O Souza IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 11822e5ff01dSLuiz Otavio O Souza ifp->if_snd.ifq_drv_maxlen = 0; 11832e5ff01dSLuiz Otavio O Souza IFQ_SET_READY(&ifp->if_snd); 11842e5ff01dSLuiz Otavio O Souza #else 1185d9b1d615SJohn Baldwin ifp->if_transmit = vlan_transmit; 11862e5ff01dSLuiz Otavio O Souza #endif 1187d9b1d615SJohn Baldwin ifp->if_qflush = vlan_qflush; 11889d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 1189b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1190f3e7afe2SHans Petter Selasky ifp->if_snd_tag_alloc = vlan_snd_tag_alloc; 11911a714ff2SRandall Stewart ifp->if_ratelimit_query = vlan_ratelimit_query; 1192f3e7afe2SHans Petter Selasky #endif 119364a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 1194fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 11959d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 1196211f625aSBill Fenner ifp->if_baudrate = 0; 1197a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 1198a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 11993ba24fdeSJohn Baldwin ifa = ifp->if_addr; 12003ba24fdeSJohn Baldwin sdl = (struct sockaddr_dl *)ifa->ifa_addr; 12013ba24fdeSJohn Baldwin sdl->sdl_type = IFT_L2VLAN; 12029d4fe4b2SBrooks Davis 12039bcf3ae4SAlexander Motin if (p != NULL) { 1204c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vid, proto); 12059bcf3ae4SAlexander Motin if_rele(p); 1206f889d2efSBrooks Davis if (error != 0) { 1207f889d2efSBrooks Davis /* 120828cc4d37SJohn Baldwin * Since we've partially failed, we need to back 1209f889d2efSBrooks Davis * out all the way, otherwise userland could get 1210f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 1211f889d2efSBrooks Davis */ 1212f889d2efSBrooks Davis ether_ifdetach(ifp); 1213249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 12144b22573aSBrooks Davis if_free(ifp); 121553729367SAlexander V. Chernikov if (!subinterface) 121696c8ef3aSMaxim Konovalov ifc_free_unit(ifc, unit); 1217f889d2efSBrooks Davis free(ifv, M_VLAN); 1218f889d2efSBrooks Davis 1219f889d2efSBrooks Davis return (error); 1220f889d2efSBrooks Davis } 1221f889d2efSBrooks Davis } 1222f889d2efSBrooks Davis 12239d4fe4b2SBrooks Davis return (0); 12249d4fe4b2SBrooks Davis } 12259d4fe4b2SBrooks Davis 1226f889d2efSBrooks Davis static int 1227f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 12289d4fe4b2SBrooks Davis { 12299d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 123053729367SAlexander V. Chernikov int unit = ifp->if_dunit; 1231c7cffd65SAlexander V. Chernikov 1232c7cffd65SAlexander V. Chernikov if (ifp->if_vlantrunk) 1233c7cffd65SAlexander V. Chernikov return (EBUSY); 1234b4e9f837SBrooks Davis 12352e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 12362e5ff01dSLuiz Otavio O Souza IFQ_PURGE(&ifp->if_snd); 12372e5ff01dSLuiz Otavio O Souza #endif 1238249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 1239249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 1240d148c2a2SMatt Joras /* 1241d148c2a2SMatt Joras * We should have the only reference to the ifv now, so we can now 1242d148c2a2SMatt Joras * drain any remaining lladdr task before freeing the ifnet and the 1243d148c2a2SMatt Joras * ifvlan. 1244d148c2a2SMatt Joras */ 1245d148c2a2SMatt Joras taskqueue_drain(taskqueue_thread, &ifv->lladdr_task); 1246b08d611dSMatt Macy NET_EPOCH_WAIT(); 12474b22573aSBrooks Davis if_free(ifp); 12489d4fe4b2SBrooks Davis free(ifv, M_VLAN); 124953729367SAlexander V. Chernikov if (unit != IF_DUNIT_NONE) 125053729367SAlexander V. Chernikov ifc_free_unit(ifc, unit); 1251b4e9f837SBrooks Davis 1252f889d2efSBrooks Davis return (0); 12539d4fe4b2SBrooks Davis } 12549d4fe4b2SBrooks Davis 125515a66c21SBruce M Simpson /* 125615a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 125715a66c21SBruce M Simpson */ 12582cc2df49SGarrett Wollman static void 1259114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 12602cc2df49SGarrett Wollman { 12612cc2df49SGarrett Wollman } 12622cc2df49SGarrett Wollman 12636d3a3ab7SGleb Smirnoff /* 1264d9b1d615SJohn Baldwin * The if_transmit method for vlan(4) interface. 12656d3a3ab7SGleb Smirnoff */ 1266d9b1d615SJohn Baldwin static int 1267d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m) 12682cc2df49SGarrett Wollman { 12692cc2df49SGarrett Wollman struct ifvlan *ifv; 12702cc2df49SGarrett Wollman struct ifnet *p; 12711ad7a257SPyun YongHyeon int error, len, mcast; 12722cc2df49SGarrett Wollman 1273b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1274b8a6e03fSGleb Smirnoff 12752cc2df49SGarrett Wollman ifv = ifp->if_softc; 1276d148c2a2SMatt Joras if (TRUNK(ifv) == NULL) { 1277d148c2a2SMatt Joras if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1278d148c2a2SMatt Joras m_freem(m); 1279d148c2a2SMatt Joras return (ENETDOWN); 1280d148c2a2SMatt Joras } 128175ee267cSGleb Smirnoff p = PARENT(ifv); 12821ad7a257SPyun YongHyeon len = m->m_pkthdr.len; 12831ad7a257SPyun YongHyeon mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 12842cc2df49SGarrett Wollman 1285a3814acfSSam Leffler BPF_MTAP(ifp, m); 12862cc2df49SGarrett Wollman 1287b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1288fb3bc596SJohn Baldwin if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) { 1289fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 1290fb3bc596SJohn Baldwin struct m_snd_tag *mst; 1291fb3bc596SJohn Baldwin 1292fb3bc596SJohn Baldwin MPASS(m->m_pkthdr.snd_tag->ifp == ifp); 1293fb3bc596SJohn Baldwin mst = m->m_pkthdr.snd_tag; 1294fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 1295fb3bc596SJohn Baldwin if (vst->tag->ifp != p) { 1296fb3bc596SJohn Baldwin if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1297fb3bc596SJohn Baldwin m_freem(m); 1298fb3bc596SJohn Baldwin return (EAGAIN); 1299fb3bc596SJohn Baldwin } 1300fb3bc596SJohn Baldwin 1301fb3bc596SJohn Baldwin m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag); 1302fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 1303fb3bc596SJohn Baldwin } 1304fb3bc596SJohn Baldwin #endif 1305fb3bc596SJohn Baldwin 1306f731f104SBill Paul /* 1307d9b1d615SJohn Baldwin * Do not run parent's if_transmit() if the parent is not up, 130824993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 130924993214SYaroslav Tykhiy */ 13102dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 1311a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1312d148c2a2SMatt Joras m_freem(m); 13138b20f6cfSHiroki Sato return (ENETDOWN); 131424993214SYaroslav Tykhiy } 131524993214SYaroslav Tykhiy 1316c7cffd65SAlexander V. Chernikov if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) { 1317a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1318d9b1d615SJohn Baldwin return (0); 13194af90a4dSMatthew N. Dodd } 13202cc2df49SGarrett Wollman 13212cc2df49SGarrett Wollman /* 13222cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 13232cc2df49SGarrett Wollman */ 1324aea78d20SKip Macy error = (p->if_transmit)(p, m); 1325299153b5SAlexander V. Chernikov if (error == 0) { 1326a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1327a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 1328a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast); 13291ad7a257SPyun YongHyeon } else 1330a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1331d9b1d615SJohn Baldwin return (error); 13322cc2df49SGarrett Wollman } 1333d9b1d615SJohn Baldwin 133416cf6bdbSMatt Joras static int 133516cf6bdbSMatt Joras vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 133616cf6bdbSMatt Joras struct route *ro) 133716cf6bdbSMatt Joras { 133816cf6bdbSMatt Joras struct ifvlan *ifv; 133916cf6bdbSMatt Joras struct ifnet *p; 134016cf6bdbSMatt Joras 1341b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1342b8a6e03fSGleb Smirnoff 1343c7cffd65SAlexander V. Chernikov /* 1344c7cffd65SAlexander V. Chernikov * Find the first non-VLAN parent interface. 1345c7cffd65SAlexander V. Chernikov */ 134616cf6bdbSMatt Joras ifv = ifp->if_softc; 1347c7cffd65SAlexander V. Chernikov do { 134816cf6bdbSMatt Joras if (TRUNK(ifv) == NULL) { 134916cf6bdbSMatt Joras m_freem(m); 135016cf6bdbSMatt Joras return (ENETDOWN); 135116cf6bdbSMatt Joras } 135216cf6bdbSMatt Joras p = PARENT(ifv); 1353c7cffd65SAlexander V. Chernikov ifv = p->if_softc; 1354c7cffd65SAlexander V. Chernikov } while (p->if_type == IFT_L2VLAN); 1355c7cffd65SAlexander V. Chernikov 135616cf6bdbSMatt Joras return p->if_output(ifp, m, dst, ro); 135716cf6bdbSMatt Joras } 135816cf6bdbSMatt Joras 13592e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 13602e5ff01dSLuiz Otavio O Souza static void 13612e5ff01dSLuiz Otavio O Souza vlan_altq_start(if_t ifp) 13622e5ff01dSLuiz Otavio O Souza { 13632e5ff01dSLuiz Otavio O Souza struct ifaltq *ifq = &ifp->if_snd; 13642e5ff01dSLuiz Otavio O Souza struct mbuf *m; 13652e5ff01dSLuiz Otavio O Souza 13662e5ff01dSLuiz Otavio O Souza IFQ_LOCK(ifq); 13672e5ff01dSLuiz Otavio O Souza IFQ_DEQUEUE_NOLOCK(ifq, m); 13682e5ff01dSLuiz Otavio O Souza while (m != NULL) { 13692e5ff01dSLuiz Otavio O Souza vlan_transmit(ifp, m); 13702e5ff01dSLuiz Otavio O Souza IFQ_DEQUEUE_NOLOCK(ifq, m); 13712e5ff01dSLuiz Otavio O Souza } 13722e5ff01dSLuiz Otavio O Souza IFQ_UNLOCK(ifq); 13732e5ff01dSLuiz Otavio O Souza } 13742e5ff01dSLuiz Otavio O Souza 13752e5ff01dSLuiz Otavio O Souza static int 13762e5ff01dSLuiz Otavio O Souza vlan_altq_transmit(if_t ifp, struct mbuf *m) 13772e5ff01dSLuiz Otavio O Souza { 13782e5ff01dSLuiz Otavio O Souza int err; 13792e5ff01dSLuiz Otavio O Souza 13802e5ff01dSLuiz Otavio O Souza if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 13812e5ff01dSLuiz Otavio O Souza IFQ_ENQUEUE(&ifp->if_snd, m, err); 13822e5ff01dSLuiz Otavio O Souza if (err == 0) 13832e5ff01dSLuiz Otavio O Souza vlan_altq_start(ifp); 13842e5ff01dSLuiz Otavio O Souza } else 13852e5ff01dSLuiz Otavio O Souza err = vlan_transmit(ifp, m); 13862e5ff01dSLuiz Otavio O Souza 13872e5ff01dSLuiz Otavio O Souza return (err); 13882e5ff01dSLuiz Otavio O Souza } 13892e5ff01dSLuiz Otavio O Souza #endif /* ALTQ */ 13902e5ff01dSLuiz Otavio O Souza 1391d9b1d615SJohn Baldwin /* 1392d9b1d615SJohn Baldwin * The ifp->if_qflush entry point for vlan(4) is a no-op. 1393d9b1d615SJohn Baldwin */ 1394d9b1d615SJohn Baldwin static void 1395d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused) 1396d9b1d615SJohn Baldwin { 1397f731f104SBill Paul } 1398f731f104SBill Paul 1399a3814acfSSam Leffler static void 1400a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 1401f731f104SBill Paul { 1402d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1403f731f104SBill Paul struct ifvlan *ifv; 14042ccbbd06SMarcelo Araujo struct m_tag *mtag; 14052ccbbd06SMarcelo Araujo uint16_t vid, tag; 140675ee267cSGleb Smirnoff 1407b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1408b8a6e03fSGleb Smirnoff 1409d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1410d148c2a2SMatt Joras if (trunk == NULL) { 1411d148c2a2SMatt Joras m_freem(m); 1412d148c2a2SMatt Joras return; 1413d148c2a2SMatt Joras } 1414a3814acfSSam Leffler 1415f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 1416a3814acfSSam Leffler /* 141714e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 1418a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 1419a3814acfSSam Leffler */ 14202ccbbd06SMarcelo Araujo tag = m->m_pkthdr.ether_vtag; 14216ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 1422a3814acfSSam Leffler } else { 142375ee267cSGleb Smirnoff struct ether_vlan_header *evl; 142475ee267cSGleb Smirnoff 142514e98256SYaroslav Tykhiy /* 142614e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 142714e98256SYaroslav Tykhiy */ 1428a3814acfSSam Leffler switch (ifp->if_type) { 1429a3814acfSSam Leffler case IFT_ETHER: 1430a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 1431a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 1432a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 1433a3814acfSSam Leffler return; 1434a3814acfSSam Leffler } 1435a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 14362ccbbd06SMarcelo Araujo tag = ntohs(evl->evl_tag); 1437db8b5973SYaroslav Tykhiy 1438db8b5973SYaroslav Tykhiy /* 14392dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 14402dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 14412dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 14422dc879b3SYaroslav Tykhiy * type field is already in place. 1443db8b5973SYaroslav Tykhiy */ 14442dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 14452dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 14462dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 1447a3814acfSSam Leffler break; 14482dc879b3SYaroslav Tykhiy 1449a3814acfSSam Leffler default: 1450db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 145160c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 145260c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 1453a3814acfSSam Leffler #endif 14543751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1455d148c2a2SMatt Joras m_freem(m); 145660c60618SYaroslav Tykhiy return; 1457a3814acfSSam Leffler } 14587a46ec8fSBrooks Davis } 14597a46ec8fSBrooks Davis 14602ccbbd06SMarcelo Araujo vid = EVL_VLANOFTAG(tag); 14612ccbbd06SMarcelo Araujo 14627983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 14632dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 1464b08d611dSMatt Macy if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1465d148c2a2SMatt Joras m_freem(m); 146675ee267cSGleb Smirnoff return; 146775ee267cSGleb Smirnoff } 1468f731f104SBill Paul 146978bc3d5eSKristof Provost if (V_vlan_mtag_pcp) { 14702ccbbd06SMarcelo Araujo /* 14712ccbbd06SMarcelo Araujo * While uncommon, it is possible that we will find a 802.1q 14722ccbbd06SMarcelo Araujo * packet encapsulated inside another packet that also had an 14732ccbbd06SMarcelo Araujo * 802.1q header. For example, ethernet tunneled over IPSEC 14742ccbbd06SMarcelo Araujo * arriving over ethernet. In that case, we replace the 14752ccbbd06SMarcelo Araujo * existing 802.1q PCP m_tag value. 14762ccbbd06SMarcelo Araujo */ 14772ccbbd06SMarcelo Araujo mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 14782ccbbd06SMarcelo Araujo if (mtag == NULL) { 14792ccbbd06SMarcelo Araujo mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN, 14802ccbbd06SMarcelo Araujo sizeof(uint8_t), M_NOWAIT); 14812ccbbd06SMarcelo Araujo if (mtag == NULL) { 14822ccbbd06SMarcelo Araujo if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1483d148c2a2SMatt Joras m_freem(m); 14842ccbbd06SMarcelo Araujo return; 14852ccbbd06SMarcelo Araujo } 14862ccbbd06SMarcelo Araujo m_tag_prepend(m, mtag); 14872ccbbd06SMarcelo Araujo } 14882ccbbd06SMarcelo Araujo *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag); 14892ccbbd06SMarcelo Araujo } 14902ccbbd06SMarcelo Araujo 1491fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 1492c0304424SGleb Smirnoff if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1); 14932cc2df49SGarrett Wollman 1494a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 1495fdbf1174SMatt Joras (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m); 14962cc2df49SGarrett Wollman } 14972cc2df49SGarrett Wollman 1498d148c2a2SMatt Joras static void 1499d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused) 1500d148c2a2SMatt Joras { 1501d148c2a2SMatt Joras struct ifvlan *ifv; 1502d148c2a2SMatt Joras struct ifnet *ifp; 1503d148c2a2SMatt Joras 1504d148c2a2SMatt Joras ifv = (struct ifvlan *)arg; 1505d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 15065191a3aeSKristof Provost 15075191a3aeSKristof Provost CURVNET_SET(ifp->if_vnet); 15085191a3aeSKristof Provost 1509d148c2a2SMatt Joras /* The ifv_ifp already has the lladdr copied in. */ 1510d148c2a2SMatt Joras if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen); 15115191a3aeSKristof Provost 15125191a3aeSKristof Provost CURVNET_RESTORE(); 1513d148c2a2SMatt Joras } 1514d148c2a2SMatt Joras 15152cc2df49SGarrett Wollman static int 1516c7cffd65SAlexander V. Chernikov vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid, 1517c7cffd65SAlexander V. Chernikov uint16_t proto) 15182cc2df49SGarrett Wollman { 15196dcec895SGleb Smirnoff struct epoch_tracker et; 152075ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 15211cf236fbSYaroslav Tykhiy struct ifnet *ifp; 152275ee267cSGleb Smirnoff int error = 0; 15232cc2df49SGarrett Wollman 1524b1828acfSGleb Smirnoff /* 1525b1828acfSGleb Smirnoff * We can handle non-ethernet hardware types as long as 1526b1828acfSGleb Smirnoff * they handle the tagging and headers themselves. 1527b1828acfSGleb Smirnoff */ 1528e4cd31ddSJeff Roberson if (p->if_type != IFT_ETHER && 1529c7cffd65SAlexander V. Chernikov p->if_type != IFT_L2VLAN && 1530e4cd31ddSJeff Roberson (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 153115a66c21SBruce M Simpson return (EPROTONOSUPPORT); 153264a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 153364a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 1534b1828acfSGleb Smirnoff /* 1535b1828acfSGleb Smirnoff * Don't let the caller set up a VLAN VID with 1536b1828acfSGleb Smirnoff * anything except VLID bits. 1537b1828acfSGleb Smirnoff * VID numbers 0x0 and 0xFFF are reserved. 1538b1828acfSGleb Smirnoff */ 1539b1828acfSGleb Smirnoff if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK)) 1540b1828acfSGleb Smirnoff return (EINVAL); 154175ee267cSGleb Smirnoff if (ifv->ifv_trunk) 154215a66c21SBruce M Simpson return (EBUSY); 15432cc2df49SGarrett Wollman 1544d148c2a2SMatt Joras VLAN_XLOCK(); 154575ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 154675ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 154775ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 154875ee267cSGleb Smirnoff vlan_inithash(trunk); 154975ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 1550d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 155175ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 155275ee267cSGleb Smirnoff trunk->parent = p; 15539bcf3ae4SAlexander Motin if_ref(trunk->parent); 1554b08d611dSMatt Macy TRUNK_WUNLOCK(trunk); 155575ee267cSGleb Smirnoff } else { 155675ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 155775ee267cSGleb Smirnoff } 155875ee267cSGleb Smirnoff 15597983103aSRobert Watson ifv->ifv_vid = vid; /* must set this before vlan_inshash() */ 15602ccbbd06SMarcelo Araujo ifv->ifv_pcp = 0; /* Default: best effort delivery. */ 156175ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 156275ee267cSGleb Smirnoff if (error) 156375ee267cSGleb Smirnoff goto done; 1564c7cffd65SAlexander V. Chernikov ifv->ifv_proto = proto; 1565a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1566a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 15671cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1568d89baa5aSAlexander Motin ifv->ifv_capenable = -1; 1569a3814acfSSam Leffler 1570a3814acfSSam Leffler /* 1571a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1572a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1573656acce4SYaroslav Tykhiy * use it. 1574a3814acfSSam Leffler */ 1575656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1576656acce4SYaroslav Tykhiy /* 1577656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1578656acce4SYaroslav Tykhiy * handle extended frames. 1579656acce4SYaroslav Tykhiy */ 1580a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1581656acce4SYaroslav Tykhiy } else { 1582a3814acfSSam Leffler /* 1583a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1584a3814acfSSam Leffler * makes us incompatible with strictly compliant 1585a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1586a3814acfSSam Leffler * the feature with other NetBSD implementations, 1587a3814acfSSam Leffler * which might still be useful. 1588a3814acfSSam Leffler */ 1589a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1590a3814acfSSam Leffler } 1591a3814acfSSam Leffler 159275ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 15931cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 1594e4cd31ddSJeff Roberson /* 1595e4cd31ddSJeff Roberson * Initialize fields from our parent. This duplicates some 1596e4cd31ddSJeff Roberson * work with ether_ifattach() but allows for non-ethernet 1597e4cd31ddSJeff Roberson * interfaces to also work. 1598e4cd31ddSJeff Roberson */ 15991cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 160075ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 1601e4cd31ddSJeff Roberson ifp->if_input = p->if_input; 1602e4cd31ddSJeff Roberson ifp->if_resolvemulti = p->if_resolvemulti; 1603e4cd31ddSJeff Roberson ifp->if_addrlen = p->if_addrlen; 1604e4cd31ddSJeff Roberson ifp->if_broadcastaddr = p->if_broadcastaddr; 160532a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 1606e4cd31ddSJeff Roberson 16072cc2df49SGarrett Wollman /* 160816cf6bdbSMatt Joras * We wrap the parent's if_output using vlan_output to ensure that it 160916cf6bdbSMatt Joras * can't become stale. 161016cf6bdbSMatt Joras */ 161116cf6bdbSMatt Joras ifp->if_output = vlan_output; 161216cf6bdbSMatt Joras 161316cf6bdbSMatt Joras /* 161424993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 161524993214SYaroslav Tykhiy * Other flags are none of our business. 16162cc2df49SGarrett Wollman */ 161764a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 16181cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 16191cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 16201cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 16211cf236fbSYaroslav Tykhiy 16221cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 16232cc2df49SGarrett Wollman 16246dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 162575ee267cSGleb Smirnoff vlan_capabilities(ifv); 16266dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 1627a3814acfSSam Leffler 1628a3814acfSSam Leffler /* 1629e4cd31ddSJeff Roberson * Set up our interface address to reflect the underlying 16302cc2df49SGarrett Wollman * physical interface's. 16312cc2df49SGarrett Wollman */ 1632a961401eSAndrey V. Elsukov TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv); 1633e4cd31ddSJeff Roberson ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1634e4cd31ddSJeff Roberson p->if_addrlen; 16351b2a4f7aSBill Fenner 1636a961401eSAndrey V. Elsukov /* 1637a961401eSAndrey V. Elsukov * Do not schedule link address update if it was the same 1638a961401eSAndrey V. Elsukov * as previous parent's. This helps avoid updating for each 1639a961401eSAndrey V. Elsukov * associated llentry. 1640a961401eSAndrey V. Elsukov */ 1641a961401eSAndrey V. Elsukov if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) { 1642a961401eSAndrey V. Elsukov bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1643a961401eSAndrey V. Elsukov taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 1644a961401eSAndrey V. Elsukov } 16452ada9747SYaroslav Tykhiy 16462ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 16472ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 1648d148c2a2SMatt Joras 1649d148c2a2SMatt Joras /* Update flags on the parent, if necessary. */ 1650d148c2a2SMatt Joras vlan_setflags(ifp, 1); 1651b08d611dSMatt Macy 1652d148c2a2SMatt Joras /* 1653b08d611dSMatt Macy * Configure multicast addresses that may already be 1654b08d611dSMatt Macy * joined on the vlan device. 1655d148c2a2SMatt Joras */ 1656b08d611dSMatt Macy (void)vlan_setmulti(ifp); 1657b08d611dSMatt Macy 1658b08d611dSMatt Macy done: 1659c725524cSJack F Vogel if (error == 0) 16607983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid); 1661d148c2a2SMatt Joras VLAN_XUNLOCK(); 166275ee267cSGleb Smirnoff 166375ee267cSGleb Smirnoff return (error); 16642cc2df49SGarrett Wollman } 16652cc2df49SGarrett Wollman 16666f359e28SJohn Baldwin static void 1667f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1668f731f104SBill Paul { 16695cb8c31aSYaroslav Tykhiy 1670d148c2a2SMatt Joras VLAN_XLOCK(); 167128cc4d37SJohn Baldwin vlan_unconfig_locked(ifp, 0); 1672d148c2a2SMatt Joras VLAN_XUNLOCK(); 16735cb8c31aSYaroslav Tykhiy } 16745cb8c31aSYaroslav Tykhiy 16756f359e28SJohn Baldwin static void 167628cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing) 16775cb8c31aSYaroslav Tykhiy { 167875ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1679f731f104SBill Paul struct vlan_mc_entry *mc; 1680f731f104SBill Paul struct ifvlan *ifv; 1681c725524cSJack F Vogel struct ifnet *parent; 168228cc4d37SJohn Baldwin int error; 1683f731f104SBill Paul 1684d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 16854faedfe8SSam Leffler 1686f731f104SBill Paul ifv = ifp->if_softc; 168775ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 168822893351SJack F Vogel parent = NULL; 1689f731f104SBill Paul 169022893351SJack F Vogel if (trunk != NULL) { 169122893351SJack F Vogel parent = trunk->parent; 16921b2a4f7aSBill Fenner 1693f731f104SBill Paul /* 1694f731f104SBill Paul * Since the interface is being unconfigured, we need to 1695f731f104SBill Paul * empty the list of multicast groups that we may have joined 16961b2a4f7aSBill Fenner * while we were alive from the parent's list. 1697f731f104SBill Paul */ 1698b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 16996f359e28SJohn Baldwin /* 170028cc4d37SJohn Baldwin * If the parent interface is being detached, 1701b90dde2fSJohn Baldwin * all its multicast addresses have already 170228cc4d37SJohn Baldwin * been removed. Warn about errors if 170328cc4d37SJohn Baldwin * if_delmulti() does fail, but don't abort as 170428cc4d37SJohn Baldwin * all callers expect vlan destruction to 170528cc4d37SJohn Baldwin * succeed. 17066f359e28SJohn Baldwin */ 170728cc4d37SJohn Baldwin if (!departing) { 170828cc4d37SJohn Baldwin error = if_delmulti(parent, 1709e4cd31ddSJeff Roberson (struct sockaddr *)&mc->mc_addr); 171028cc4d37SJohn Baldwin if (error) 171128cc4d37SJohn Baldwin if_printf(ifp, 171228cc4d37SJohn Baldwin "Failed to delete multicast address from parent: %d\n", 171328cc4d37SJohn Baldwin error); 171428cc4d37SJohn Baldwin } 1715b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 17162a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 1717f731f104SBill Paul } 1718a3814acfSSam Leffler 17191cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 1720d148c2a2SMatt Joras 172175ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 172275ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 172375ee267cSGleb Smirnoff 172475ee267cSGleb Smirnoff /* 172575ee267cSGleb Smirnoff * Check if we were the last. 172675ee267cSGleb Smirnoff */ 172775ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 17282d222cb7SAlexander Motin parent->if_vlantrunk = NULL; 1729b08d611dSMatt Macy NET_EPOCH_WAIT(); 173075ee267cSGleb Smirnoff trunk_destroy(trunk); 1731d148c2a2SMatt Joras } 17321b2a4f7aSBill Fenner } 1733f731f104SBill Paul 1734f731f104SBill Paul /* Disconnect from parent. */ 17351cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 17361cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 17375cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 17385cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 17395cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1740f731f104SBill Paul 174122893351SJack F Vogel /* 174222893351SJack F Vogel * Only dispatch an event if vlan was 174322893351SJack F Vogel * attached, otherwise there is nothing 174422893351SJack F Vogel * to cleanup anyway. 174522893351SJack F Vogel */ 174622893351SJack F Vogel if (parent != NULL) 17477983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid); 1748f731f104SBill Paul } 1749f731f104SBill Paul 17501cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1751f731f104SBill Paul static int 17521cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 17531cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1754a3814acfSSam Leffler { 17551cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 17561cf236fbSYaroslav Tykhiy int error; 1757a3814acfSSam Leffler 1758d148c2a2SMatt Joras VLAN_SXLOCK_ASSERT(); 1759a3814acfSSam Leffler 17601cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 17611cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 17621cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 17631cf236fbSYaroslav Tykhiy 17641cf236fbSYaroslav Tykhiy /* 17651cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 17661cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 17671cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 17681cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 17691cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 17701cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 17711cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 17721cf236fbSYaroslav Tykhiy */ 17731cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 177475ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 17751cf236fbSYaroslav Tykhiy if (error) 1776a3814acfSSam Leffler return (error); 17771cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 17781cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 17791cf236fbSYaroslav Tykhiy } 17801cf236fbSYaroslav Tykhiy return (0); 17811cf236fbSYaroslav Tykhiy } 17821cf236fbSYaroslav Tykhiy 17831cf236fbSYaroslav Tykhiy /* 17841cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 17851cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 17861cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 17871cf236fbSYaroslav Tykhiy */ 17881cf236fbSYaroslav Tykhiy static int 17891cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 17901cf236fbSYaroslav Tykhiy { 17911cf236fbSYaroslav Tykhiy int error, i; 17921cf236fbSYaroslav Tykhiy 17931cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 17941cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 17951cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 17961cf236fbSYaroslav Tykhiy if (error) 17971cf236fbSYaroslav Tykhiy return (error); 17981cf236fbSYaroslav Tykhiy } 17991cf236fbSYaroslav Tykhiy return (0); 1800a3814acfSSam Leffler } 1801a3814acfSSam Leffler 1802127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1803127d7b2dSAndre Oppermann static void 1804a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp) 1805127d7b2dSAndre Oppermann { 1806b2807792SGleb Smirnoff struct epoch_tracker et; 1807d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1808127d7b2dSAndre Oppermann struct ifvlan *ifv; 1809127d7b2dSAndre Oppermann 1810b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 1811d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1812b2807792SGleb Smirnoff if (trunk == NULL) { 1813b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 1814d148c2a2SMatt Joras return; 1815b2807792SGleb Smirnoff } 1816d148c2a2SMatt Joras 1817d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 1818d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 1819aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1820fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 182175ee267cSGleb Smirnoff trunk->parent->if_link_state); 1822127d7b2dSAndre Oppermann } 1823d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 1824b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 182575ee267cSGleb Smirnoff } 182675ee267cSGleb Smirnoff 182775ee267cSGleb Smirnoff static void 182875ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 182975ee267cSGleb Smirnoff { 1830d148c2a2SMatt Joras struct ifnet *p; 1831d148c2a2SMatt Joras struct ifnet *ifp; 18329fd573c3SHans Petter Selasky struct ifnet_hw_tsomax hw_tsomax; 1833d89baa5aSAlexander Motin int cap = 0, ena = 0, mena; 1834d89baa5aSAlexander Motin u_long hwa = 0; 183575ee267cSGleb Smirnoff 1836a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 1837b8a6e03fSGleb Smirnoff VLAN_SXLOCK_ASSERT(); 1838b8a6e03fSGleb Smirnoff 1839d148c2a2SMatt Joras p = PARENT(ifv); 1840d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 184175ee267cSGleb Smirnoff 1842d89baa5aSAlexander Motin /* Mask parent interface enabled capabilities disabled by user. */ 1843d89baa5aSAlexander Motin mena = p->if_capenable & ifv->ifv_capenable; 1844d89baa5aSAlexander Motin 184575ee267cSGleb Smirnoff /* 184675ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 184775ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 184875ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 184975ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 185075ee267cSGleb Smirnoff */ 185175ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1852d89baa5aSAlexander Motin cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 185375ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 185475ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1855d89baa5aSAlexander Motin ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 1856d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM) 1857d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP | 1858d89baa5aSAlexander Motin CSUM_UDP | CSUM_SCTP); 1859d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM_IPV6) 1860d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_TCP_IPV6 | 1861d89baa5aSAlexander Motin CSUM_UDP_IPV6 | CSUM_SCTP_IPV6); 186275ee267cSGleb Smirnoff } 1863d89baa5aSAlexander Motin 18649b76d9cbSPyun YongHyeon /* 18659b76d9cbSPyun YongHyeon * If the parent interface can do TSO on VLANs then 18669b76d9cbSPyun YongHyeon * propagate the hardware-assisted flag. TSO on VLANs 18679b76d9cbSPyun YongHyeon * does not necessarily require hardware VLAN tagging. 18689b76d9cbSPyun YongHyeon */ 18699fd573c3SHans Petter Selasky memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 18709fd573c3SHans Petter Selasky if_hw_tsomax_common(p, &hw_tsomax); 18719fd573c3SHans Petter Selasky if_hw_tsomax_update(ifp, &hw_tsomax); 18729b76d9cbSPyun YongHyeon if (p->if_capabilities & IFCAP_VLAN_HWTSO) 1873d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TSO; 18749b76d9cbSPyun YongHyeon if (p->if_capenable & IFCAP_VLAN_HWTSO) { 1875d89baa5aSAlexander Motin ena |= mena & IFCAP_TSO; 1876d89baa5aSAlexander Motin if (ena & IFCAP_TSO) 1877d89baa5aSAlexander Motin hwa |= p->if_hwassist & CSUM_TSO; 18789b76d9cbSPyun YongHyeon } 187909fe6320SNavdeep Parhar 188009fe6320SNavdeep Parhar /* 188159150e91SAlexander Motin * If the parent interface can do LRO and checksum offloading on 188259150e91SAlexander Motin * VLANs, then guess it may do LRO on VLANs. False positive here 188359150e91SAlexander Motin * cost nothing, while false negative may lead to some confusions. 188459150e91SAlexander Motin */ 188559150e91SAlexander Motin if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 188659150e91SAlexander Motin cap |= p->if_capabilities & IFCAP_LRO; 188759150e91SAlexander Motin if (p->if_capenable & IFCAP_VLAN_HWCSUM) 188859150e91SAlexander Motin ena |= p->if_capenable & IFCAP_LRO; 188959150e91SAlexander Motin 189059150e91SAlexander Motin /* 189109fe6320SNavdeep Parhar * If the parent interface can offload TCP connections over VLANs then 189209fe6320SNavdeep Parhar * propagate its TOE capability to the VLAN interface. 189309fe6320SNavdeep Parhar * 189409fe6320SNavdeep Parhar * All TOE drivers in the tree today can deal with VLANs. If this 189509fe6320SNavdeep Parhar * changes then IFCAP_VLAN_TOE should be promoted to a full capability 189609fe6320SNavdeep Parhar * with its own bit. 189709fe6320SNavdeep Parhar */ 189809fe6320SNavdeep Parhar #define IFCAP_VLAN_TOE IFCAP_TOE 189909fe6320SNavdeep Parhar if (p->if_capabilities & IFCAP_VLAN_TOE) 1900d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TOE; 190109fe6320SNavdeep Parhar if (p->if_capenable & IFCAP_VLAN_TOE) { 190209fe6320SNavdeep Parhar TOEDEV(ifp) = TOEDEV(p); 1903d89baa5aSAlexander Motin ena |= mena & IFCAP_TOE; 190409fe6320SNavdeep Parhar } 1905f3e7afe2SHans Petter Selasky 1906d89baa5aSAlexander Motin /* 1907d89baa5aSAlexander Motin * If the parent interface supports dynamic link state, so does the 1908d89baa5aSAlexander Motin * VLAN interface. 1909d89baa5aSAlexander Motin */ 1910d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_LINKSTATE); 1911d89baa5aSAlexander Motin ena |= (mena & IFCAP_LINKSTATE); 1912d89baa5aSAlexander Motin 1913f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1914f3e7afe2SHans Petter Selasky /* 1915f3e7afe2SHans Petter Selasky * If the parent interface supports ratelimiting, so does the 1916f3e7afe2SHans Petter Selasky * VLAN interface. 1917f3e7afe2SHans Petter Selasky */ 1918d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_TXRTLMT); 1919d89baa5aSAlexander Motin ena |= (mena & IFCAP_TXRTLMT); 1920f3e7afe2SHans Petter Selasky #endif 1921d89baa5aSAlexander Motin 192266d0c056SJohn Baldwin /* 192366d0c056SJohn Baldwin * If the parent interface supports unmapped mbufs, so does 192466d0c056SJohn Baldwin * the VLAN interface. Note that this should be fine even for 192566d0c056SJohn Baldwin * interfaces that don't support hardware tagging as headers 192666d0c056SJohn Baldwin * are prepended in normal mbufs to unmapped mbufs holding 192766d0c056SJohn Baldwin * payload data. 192866d0c056SJohn Baldwin */ 19293f43ada9SGleb Smirnoff cap |= (p->if_capabilities & IFCAP_MEXTPG); 19303f43ada9SGleb Smirnoff ena |= (mena & IFCAP_MEXTPG); 193166d0c056SJohn Baldwin 1932b2e60773SJohn Baldwin /* 1933b2e60773SJohn Baldwin * If the parent interface can offload encryption and segmentation 1934b2e60773SJohn Baldwin * of TLS records over TCP, propagate it's capability to the VLAN 1935b2e60773SJohn Baldwin * interface. 1936b2e60773SJohn Baldwin * 1937b2e60773SJohn Baldwin * All TLS drivers in the tree today can deal with VLANs. If 1938b2e60773SJohn Baldwin * this ever changes, then a new IFCAP_VLAN_TXTLS can be 1939b2e60773SJohn Baldwin * defined. 1940b2e60773SJohn Baldwin */ 1941521eac97SJohn Baldwin if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 1942521eac97SJohn Baldwin cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 1943521eac97SJohn Baldwin if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 1944521eac97SJohn Baldwin ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 1945b2e60773SJohn Baldwin 1946d89baa5aSAlexander Motin ifp->if_capabilities = cap; 1947d89baa5aSAlexander Motin ifp->if_capenable = ena; 1948d89baa5aSAlexander Motin ifp->if_hwassist = hwa; 194975ee267cSGleb Smirnoff } 195075ee267cSGleb Smirnoff 195175ee267cSGleb Smirnoff static void 195275ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 195375ee267cSGleb Smirnoff { 1954b2807792SGleb Smirnoff struct epoch_tracker et; 1955d148c2a2SMatt Joras struct ifvlantrunk *trunk; 195675ee267cSGleb Smirnoff struct ifvlan *ifv; 195775ee267cSGleb Smirnoff 1958d148c2a2SMatt Joras VLAN_SLOCK(); 1959d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1960d148c2a2SMatt Joras if (trunk == NULL) { 1961d148c2a2SMatt Joras VLAN_SUNLOCK(); 1962d148c2a2SMatt Joras return; 1963d148c2a2SMatt Joras } 1964b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 1965b8a6e03fSGleb Smirnoff VLAN_FOREACH(ifv, trunk) 196675ee267cSGleb Smirnoff vlan_capabilities(ifv); 1967b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 1968d148c2a2SMatt Joras VLAN_SUNLOCK(); 1969127d7b2dSAndre Oppermann } 1970127d7b2dSAndre Oppermann 1971a3814acfSSam Leffler static int 1972cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 19732cc2df49SGarrett Wollman { 19742cc2df49SGarrett Wollman struct ifnet *p; 19752cc2df49SGarrett Wollman struct ifreq *ifr; 19762884a936SJohn Baldwin #ifdef INET 1977e4cd31ddSJeff Roberson struct ifaddr *ifa; 19782884a936SJohn Baldwin #endif 19792cc2df49SGarrett Wollman struct ifvlan *ifv; 19802d222cb7SAlexander Motin struct ifvlantrunk *trunk; 19812cc2df49SGarrett Wollman struct vlanreq vlr; 198284becee1SAlexander Motin int error = 0, oldmtu; 19832cc2df49SGarrett Wollman 19842cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 19852884a936SJohn Baldwin #ifdef INET 1986e4cd31ddSJeff Roberson ifa = (struct ifaddr *) data; 19872884a936SJohn Baldwin #endif 19882cc2df49SGarrett Wollman ifv = ifp->if_softc; 19892cc2df49SGarrett Wollman 19902cc2df49SGarrett Wollman switch (cmd) { 1991e4cd31ddSJeff Roberson case SIOCSIFADDR: 1992e4cd31ddSJeff Roberson ifp->if_flags |= IFF_UP; 1993e4cd31ddSJeff Roberson #ifdef INET 1994e4cd31ddSJeff Roberson if (ifa->ifa_addr->sa_family == AF_INET) 1995e4cd31ddSJeff Roberson arp_ifinit(ifp, ifa); 1996e4cd31ddSJeff Roberson #endif 1997e4cd31ddSJeff Roberson break; 1998e4cd31ddSJeff Roberson case SIOCGIFADDR: 199938d958a6SBrooks Davis bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 200038d958a6SBrooks Davis ifp->if_addrlen); 2001e4cd31ddSJeff Roberson break; 2002b3cca108SBill Fenner case SIOCGIFMEDIA: 2003d148c2a2SMatt Joras VLAN_SLOCK(); 200475ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 2005d8564efdSEd Maste p = PARENT(ifv); 20069bcf3ae4SAlexander Motin if_ref(p); 2007d8564efdSEd Maste error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 20089bcf3ae4SAlexander Motin if_rele(p); 2009b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 2010b3cca108SBill Fenner if (error == 0) { 2011b3cca108SBill Fenner struct ifmediareq *ifmr; 2012b3cca108SBill Fenner 2013b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 2014b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 2015b3cca108SBill Fenner ifmr->ifm_count = 1; 2016b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 2017b3cca108SBill Fenner ifmr->ifm_ulist, 2018b3cca108SBill Fenner sizeof(int)); 2019b3cca108SBill Fenner } 2020b3cca108SBill Fenner } 20214faedfe8SSam Leffler } else { 2022b3cca108SBill Fenner error = EINVAL; 20234faedfe8SSam Leffler } 2024d148c2a2SMatt Joras VLAN_SUNLOCK(); 2025b3cca108SBill Fenner break; 2026b3cca108SBill Fenner 2027b3cca108SBill Fenner case SIOCSIFMEDIA: 2028b3cca108SBill Fenner error = EINVAL; 2029b3cca108SBill Fenner break; 2030b3cca108SBill Fenner 20312cc2df49SGarrett Wollman case SIOCSIFMTU: 20322cc2df49SGarrett Wollman /* 20332cc2df49SGarrett Wollman * Set the interface MTU. 20342cc2df49SGarrett Wollman */ 2035d148c2a2SMatt Joras VLAN_SLOCK(); 2036d148c2a2SMatt Joras trunk = TRUNK(ifv); 2037d148c2a2SMatt Joras if (trunk != NULL) { 2038d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 2039a3814acfSSam Leffler if (ifr->ifr_mtu > 204075ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 2041a3814acfSSam Leffler ifr->ifr_mtu < 2042a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 20432cc2df49SGarrett Wollman error = EINVAL; 2044a3814acfSSam Leffler else 20452cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 2046d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 2047a3814acfSSam Leffler } else 2048a3814acfSSam Leffler error = EINVAL; 2049d148c2a2SMatt Joras VLAN_SUNLOCK(); 20502cc2df49SGarrett Wollman break; 20512cc2df49SGarrett Wollman 20522cc2df49SGarrett Wollman case SIOCSETVLAN: 2053ccf7ba97SMarko Zec #ifdef VIMAGE 205415f6780eSRobert Watson /* 205515f6780eSRobert Watson * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN 205615f6780eSRobert Watson * interface to be delegated to a jail without allowing the 205715f6780eSRobert Watson * jail to change what underlying interface/VID it is 205815f6780eSRobert Watson * associated with. We are not entirely convinced that this 20595a39f779SRobert Watson * is the right way to accomplish that policy goal. 206015f6780eSRobert Watson */ 2061ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 2062ccf7ba97SMarko Zec error = EPERM; 2063ccf7ba97SMarko Zec break; 2064ccf7ba97SMarko Zec } 2065ccf7ba97SMarko Zec #endif 2066541d96aaSBrooks Davis error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr)); 20672cc2df49SGarrett Wollman if (error) 20682cc2df49SGarrett Wollman break; 20692cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 2070f731f104SBill Paul vlan_unconfig(ifp); 20712cc2df49SGarrett Wollman break; 20722cc2df49SGarrett Wollman } 20739bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 20741bdc73d3SEd Maste if (p == NULL) { 20752cc2df49SGarrett Wollman error = ENOENT; 20762cc2df49SGarrett Wollman break; 20772cc2df49SGarrett Wollman } 2078afbb64f1SAlexander V. Chernikov #ifdef COMPAT_FREEBSD12 2079afbb64f1SAlexander V. Chernikov if (vlr.vlr_proto == 0) 2080afbb64f1SAlexander V. Chernikov vlr.vlr_proto = ETHERTYPE_VLAN; 2081afbb64f1SAlexander V. Chernikov #endif 208284becee1SAlexander Motin oldmtu = ifp->if_mtu; 2083c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto); 20849bcf3ae4SAlexander Motin if_rele(p); 208584becee1SAlexander Motin 208684becee1SAlexander Motin /* 208784becee1SAlexander Motin * VLAN MTU may change during addition of the vlandev. 208884becee1SAlexander Motin * If it did, do network layer specific procedure. 208984becee1SAlexander Motin */ 209084becee1SAlexander Motin if (ifp->if_mtu != oldmtu) { 209184becee1SAlexander Motin #ifdef INET6 209284becee1SAlexander Motin nd6_setmtu(ifp); 209384becee1SAlexander Motin #endif 209484becee1SAlexander Motin rt_updatemtu(ifp); 209584becee1SAlexander Motin } 20962cc2df49SGarrett Wollman break; 20972cc2df49SGarrett Wollman 20982cc2df49SGarrett Wollman case SIOCGETVLAN: 2099ccf7ba97SMarko Zec #ifdef VIMAGE 2100ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 2101ccf7ba97SMarko Zec error = EPERM; 2102ccf7ba97SMarko Zec break; 2103ccf7ba97SMarko Zec } 2104ccf7ba97SMarko Zec #endif 210515a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 2106d148c2a2SMatt Joras VLAN_SLOCK(); 210775ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 210875ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 21099bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 21107983103aSRobert Watson vlr.vlr_tag = ifv->ifv_vid; 2111c7cffd65SAlexander V. Chernikov vlr.vlr_proto = ifv->ifv_proto; 21122cc2df49SGarrett Wollman } 2113d148c2a2SMatt Joras VLAN_SUNLOCK(); 2114541d96aaSBrooks Davis error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr)); 21152cc2df49SGarrett Wollman break; 21162cc2df49SGarrett Wollman 21172cc2df49SGarrett Wollman case SIOCSIFFLAGS: 21182cc2df49SGarrett Wollman /* 21191cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 21201cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 21212cc2df49SGarrett Wollman */ 2122d148c2a2SMatt Joras VLAN_XLOCK(); 212375ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 21241cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 2125d148c2a2SMatt Joras VLAN_XUNLOCK(); 21262cc2df49SGarrett Wollman break; 2127a3814acfSSam Leffler 2128f731f104SBill Paul case SIOCADDMULTI: 2129f731f104SBill Paul case SIOCDELMULTI: 213075ee267cSGleb Smirnoff /* 213175ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 213275ee267cSGleb Smirnoff * when we do. 2133d148c2a2SMatt Joras * 2134d148c2a2SMatt Joras * XXX We need the rmlock here to avoid sleeping while 2135d148c2a2SMatt Joras * holding in6_multi_mtx. 213675ee267cSGleb Smirnoff */ 2137b08d611dSMatt Macy VLAN_XLOCK(); 21382d222cb7SAlexander Motin trunk = TRUNK(ifv); 2139b08d611dSMatt Macy if (trunk != NULL) 2140f731f104SBill Paul error = vlan_setmulti(ifp); 2141b08d611dSMatt Macy VLAN_XUNLOCK(); 214275ee267cSGleb Smirnoff 2143b08d611dSMatt Macy break; 21442ccbbd06SMarcelo Araujo case SIOCGVLANPCP: 21452ccbbd06SMarcelo Araujo #ifdef VIMAGE 21462ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 21472ccbbd06SMarcelo Araujo error = EPERM; 21482ccbbd06SMarcelo Araujo break; 21492ccbbd06SMarcelo Araujo } 21502ccbbd06SMarcelo Araujo #endif 21512ccbbd06SMarcelo Araujo ifr->ifr_vlan_pcp = ifv->ifv_pcp; 21522ccbbd06SMarcelo Araujo break; 21532ccbbd06SMarcelo Araujo 21542ccbbd06SMarcelo Araujo case SIOCSVLANPCP: 21552ccbbd06SMarcelo Araujo #ifdef VIMAGE 21562ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 21572ccbbd06SMarcelo Araujo error = EPERM; 21582ccbbd06SMarcelo Araujo break; 21592ccbbd06SMarcelo Araujo } 21602ccbbd06SMarcelo Araujo #endif 21612ccbbd06SMarcelo Araujo error = priv_check(curthread, PRIV_NET_SETVLANPCP); 21622ccbbd06SMarcelo Araujo if (error) 21632ccbbd06SMarcelo Araujo break; 21649ef8cd0bSKristof Provost if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) { 21652ccbbd06SMarcelo Araujo error = EINVAL; 21662ccbbd06SMarcelo Araujo break; 21672ccbbd06SMarcelo Araujo } 21682ccbbd06SMarcelo Araujo ifv->ifv_pcp = ifr->ifr_vlan_pcp; 216932a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 21704a381a9eSHans Petter Selasky /* broadcast event about PCP change */ 21714a381a9eSHans Petter Selasky EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 21722ccbbd06SMarcelo Araujo break; 21732ccbbd06SMarcelo Araujo 2174d89baa5aSAlexander Motin case SIOCSIFCAP: 2175d148c2a2SMatt Joras VLAN_SLOCK(); 2176d89baa5aSAlexander Motin ifv->ifv_capenable = ifr->ifr_reqcap; 2177d89baa5aSAlexander Motin trunk = TRUNK(ifv); 21786dcec895SGleb Smirnoff if (trunk != NULL) { 21796dcec895SGleb Smirnoff struct epoch_tracker et; 21806dcec895SGleb Smirnoff 21816dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 2182d89baa5aSAlexander Motin vlan_capabilities(ifv); 21836dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 21846dcec895SGleb Smirnoff } 2185d148c2a2SMatt Joras VLAN_SUNLOCK(); 2186d89baa5aSAlexander Motin break; 2187d89baa5aSAlexander Motin 21882cc2df49SGarrett Wollman default: 2189e4cd31ddSJeff Roberson error = EINVAL; 2190e4cd31ddSJeff Roberson break; 21912cc2df49SGarrett Wollman } 219215a66c21SBruce M Simpson 219315a66c21SBruce M Simpson return (error); 21942cc2df49SGarrett Wollman } 2195f3e7afe2SHans Petter Selasky 2196b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 2197f3e7afe2SHans Petter Selasky static int 2198f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp, 2199f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *params, 2200f3e7afe2SHans Petter Selasky struct m_snd_tag **ppmt) 2201f3e7afe2SHans Petter Selasky { 2202fb3bc596SJohn Baldwin struct epoch_tracker et; 2203c782ea8bSJohn Baldwin const struct if_snd_tag_sw *sw; 2204fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2205fb3bc596SJohn Baldwin struct ifvlan *ifv; 2206fb3bc596SJohn Baldwin struct ifnet *parent; 2207*892eded5SHans Petter Selasky struct m_snd_tag *mst; 2208fb3bc596SJohn Baldwin int error; 2209f3e7afe2SHans Petter Selasky 2210*892eded5SHans Petter Selasky NET_EPOCH_ENTER(et); 2211*892eded5SHans Petter Selasky ifv = ifp->if_softc; 2212*892eded5SHans Petter Selasky 2213c782ea8bSJohn Baldwin switch (params->hdr.type) { 2214c782ea8bSJohn Baldwin #ifdef RATELIMIT 2215c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_UNLIMITED: 2216c782ea8bSJohn Baldwin sw = &vlan_snd_tag_ul_sw; 2217c782ea8bSJohn Baldwin break; 2218c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_RATE_LIMIT: 2219c782ea8bSJohn Baldwin sw = &vlan_snd_tag_rl_sw; 2220c782ea8bSJohn Baldwin break; 2221c782ea8bSJohn Baldwin #endif 2222c782ea8bSJohn Baldwin #ifdef KERN_TLS 2223c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_TLS: 2224c782ea8bSJohn Baldwin sw = &vlan_snd_tag_tls_sw; 2225c782ea8bSJohn Baldwin break; 2226*892eded5SHans Petter Selasky case IF_SND_TAG_TYPE_TLS_RX: 2227*892eded5SHans Petter Selasky sw = NULL; 2228*892eded5SHans Petter Selasky if (params->tls_rx.vlan_id != 0) 2229*892eded5SHans Petter Selasky goto failure; 2230*892eded5SHans Petter Selasky params->tls_rx.vlan_id = ifv->ifv_vid; 2231*892eded5SHans Petter Selasky break; 2232c782ea8bSJohn Baldwin #ifdef RATELIMIT 2233c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_TLS_RATE_LIMIT: 2234c782ea8bSJohn Baldwin sw = &vlan_snd_tag_tls_rl_sw; 2235c782ea8bSJohn Baldwin break; 2236c782ea8bSJohn Baldwin #endif 2237c782ea8bSJohn Baldwin #endif 2238c782ea8bSJohn Baldwin default: 2239*892eded5SHans Petter Selasky goto failure; 2240c782ea8bSJohn Baldwin } 2241c782ea8bSJohn Baldwin 2242fb3bc596SJohn Baldwin if (ifv->ifv_trunk != NULL) 2243fb3bc596SJohn Baldwin parent = PARENT(ifv); 2244fb3bc596SJohn Baldwin else 2245fb3bc596SJohn Baldwin parent = NULL; 2246*892eded5SHans Petter Selasky if (parent == NULL) 2247*892eded5SHans Petter Selasky goto failure; 2248fb3bc596SJohn Baldwin if_ref(parent); 2249fb3bc596SJohn Baldwin NET_EPOCH_EXIT(et); 2250fb3bc596SJohn Baldwin 2251*892eded5SHans Petter Selasky if (sw != NULL) { 2252fb3bc596SJohn Baldwin vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT); 2253fb3bc596SJohn Baldwin if (vst == NULL) { 2254fb3bc596SJohn Baldwin if_rele(parent); 2255fb3bc596SJohn Baldwin return (ENOMEM); 2256fb3bc596SJohn Baldwin } 2257*892eded5SHans Petter Selasky } else 2258*892eded5SHans Petter Selasky vst = NULL; 2259fb3bc596SJohn Baldwin 2260*892eded5SHans Petter Selasky error = m_snd_tag_alloc(parent, params, &mst); 2261fb3bc596SJohn Baldwin if_rele(parent); 2262fb3bc596SJohn Baldwin if (error) { 2263fb3bc596SJohn Baldwin free(vst, M_VLAN); 2264fb3bc596SJohn Baldwin return (error); 2265fb3bc596SJohn Baldwin } 2266fb3bc596SJohn Baldwin 2267*892eded5SHans Petter Selasky if (sw != NULL) { 2268c782ea8bSJohn Baldwin m_snd_tag_init(&vst->com, ifp, sw); 2269*892eded5SHans Petter Selasky vst->tag = mst; 2270fb3bc596SJohn Baldwin 2271fb3bc596SJohn Baldwin *ppmt = &vst->com; 2272*892eded5SHans Petter Selasky } else 2273*892eded5SHans Petter Selasky *ppmt = mst; 2274*892eded5SHans Petter Selasky 2275fb3bc596SJohn Baldwin return (0); 2276*892eded5SHans Petter Selasky failure: 2277*892eded5SHans Petter Selasky NET_EPOCH_EXIT(et); 2278*892eded5SHans Petter Selasky return (EOPNOTSUPP); 2279fb3bc596SJohn Baldwin } 2280fb3bc596SJohn Baldwin 22811a714ff2SRandall Stewart static struct m_snd_tag * 22821a714ff2SRandall Stewart vlan_next_snd_tag(struct m_snd_tag *mst) 22831a714ff2SRandall Stewart { 22841a714ff2SRandall Stewart struct vlan_snd_tag *vst; 22851a714ff2SRandall Stewart 22861a714ff2SRandall Stewart vst = mst_to_vst(mst); 22871a714ff2SRandall Stewart return (vst->tag); 22881a714ff2SRandall Stewart } 22891a714ff2SRandall Stewart 2290fb3bc596SJohn Baldwin static int 2291fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst, 2292fb3bc596SJohn Baldwin union if_snd_tag_modify_params *params) 2293fb3bc596SJohn Baldwin { 2294fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2295fb3bc596SJohn Baldwin 2296fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2297c782ea8bSJohn Baldwin return (vst->tag->sw->snd_tag_modify(vst->tag, params)); 2298fb3bc596SJohn Baldwin } 2299fb3bc596SJohn Baldwin 2300fb3bc596SJohn Baldwin static int 2301fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst, 2302fb3bc596SJohn Baldwin union if_snd_tag_query_params *params) 2303fb3bc596SJohn Baldwin { 2304fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2305fb3bc596SJohn Baldwin 2306fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2307c782ea8bSJohn Baldwin return (vst->tag->sw->snd_tag_query(vst->tag, params)); 2308f3e7afe2SHans Petter Selasky } 2309fa91f845SRandall Stewart 2310fa91f845SRandall Stewart static void 2311fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst) 2312fa91f845SRandall Stewart { 2313fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2314fb3bc596SJohn Baldwin 2315fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2316fb3bc596SJohn Baldwin m_snd_tag_rele(vst->tag); 2317fb3bc596SJohn Baldwin free(vst, M_VLAN); 2318fa91f845SRandall Stewart } 23191a714ff2SRandall Stewart 23201a714ff2SRandall Stewart static void 23211a714ff2SRandall Stewart vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q) 23221a714ff2SRandall Stewart { 23231a714ff2SRandall Stewart /* 23241a714ff2SRandall Stewart * For vlan, we have an indirect 23251a714ff2SRandall Stewart * interface. The caller needs to 23261a714ff2SRandall Stewart * get a ratelimit tag on the actual 23271a714ff2SRandall Stewart * interface the flow will go on. 23281a714ff2SRandall Stewart */ 23291a714ff2SRandall Stewart q->rate_table = NULL; 23301a714ff2SRandall Stewart q->flags = RT_IS_INDIRECT; 23311a714ff2SRandall Stewart q->max_flows = 0; 23321a714ff2SRandall Stewart q->number_of_rates = 0; 23331a714ff2SRandall Stewart } 23341a714ff2SRandall Stewart 2335f3e7afe2SHans Petter Selasky #endif 2336