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" 51089104e0SAlexander V. Chernikov #include "opt_netlink.h" 5275ee267cSGleb Smirnoff #include "opt_vlan.h" 53f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 542cc2df49SGarrett Wollman 552cc2df49SGarrett Wollman #include <sys/param.h> 56c3322cb9SGleb Smirnoff #include <sys/eventhandler.h> 572cc2df49SGarrett Wollman #include <sys/kernel.h> 5875ee267cSGleb Smirnoff #include <sys/lock.h> 59f731f104SBill Paul #include <sys/malloc.h> 602cc2df49SGarrett Wollman #include <sys/mbuf.h> 612b120974SPeter Wemm #include <sys/module.h> 62772b000fSAlexander V. Chernikov #include <sys/rmlock.h> 632ccbbd06SMarcelo Araujo #include <sys/priv.h> 64f731f104SBill Paul #include <sys/queue.h> 652cc2df49SGarrett Wollman #include <sys/socket.h> 662cc2df49SGarrett Wollman #include <sys/sockio.h> 672cc2df49SGarrett Wollman #include <sys/sysctl.h> 682cc2df49SGarrett Wollman #include <sys/systm.h> 69e4cd31ddSJeff Roberson #include <sys/sx.h> 70d148c2a2SMatt Joras #include <sys/taskqueue.h> 712cc2df49SGarrett Wollman 722cc2df49SGarrett Wollman #include <net/bpf.h> 732cc2df49SGarrett Wollman #include <net/ethernet.h> 742cc2df49SGarrett Wollman #include <net/if.h> 7576039bc8SGleb Smirnoff #include <net/if_var.h> 762c2b37adSJustin Hibbits #include <net/if_private.h> 77f889d2efSBrooks Davis #include <net/if_clone.h> 782cc2df49SGarrett Wollman #include <net/if_dl.h> 792cc2df49SGarrett Wollman #include <net/if_types.h> 802cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 8184becee1SAlexander Motin #include <net/route.h> 824b79449eSBjoern A. Zeeb #include <net/vnet.h> 832cc2df49SGarrett Wollman 842c5b403eSOleg Bulyzhin #ifdef INET 852c5b403eSOleg Bulyzhin #include <netinet/in.h> 862c5b403eSOleg Bulyzhin #include <netinet/if_ether.h> 872c5b403eSOleg Bulyzhin #endif 882c5b403eSOleg Bulyzhin 89089104e0SAlexander V. Chernikov #include <netlink/netlink.h> 90089104e0SAlexander V. Chernikov #include <netlink/netlink_ctl.h> 91089104e0SAlexander V. Chernikov #include <netlink/netlink_route.h> 92089104e0SAlexander V. Chernikov #include <netlink/route/route_var.h> 93089104e0SAlexander V. Chernikov 9475ee267cSGleb Smirnoff #define VLAN_DEF_HWIDTH 4 9564a17d2eSYaroslav Tykhiy #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 9675ee267cSGleb Smirnoff 972dc879b3SYaroslav Tykhiy #define UP_AND_RUNNING(ifp) \ 982dc879b3SYaroslav Tykhiy ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 992dc879b3SYaroslav Tykhiy 100b08d611dSMatt Macy CK_SLIST_HEAD(ifvlanhead, ifvlan); 10175ee267cSGleb Smirnoff 10275ee267cSGleb Smirnoff struct ifvlantrunk { 10375ee267cSGleb Smirnoff struct ifnet *parent; /* parent interface of this trunk */ 104b08d611dSMatt Macy struct mtx lock; 10575ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 1065cb8c31aSYaroslav Tykhiy #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 1075cb8c31aSYaroslav Tykhiy struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 10875ee267cSGleb Smirnoff #else 10975ee267cSGleb Smirnoff struct ifvlanhead *hash; /* dynamic hash-list table */ 11075ee267cSGleb Smirnoff uint16_t hmask; 11175ee267cSGleb Smirnoff uint16_t hwidth; 11275ee267cSGleb Smirnoff #endif 11375ee267cSGleb Smirnoff int refcnt; 11475ee267cSGleb Smirnoff }; 1159d4fe4b2SBrooks Davis 116b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 117fb3bc596SJohn Baldwin struct vlan_snd_tag { 118fb3bc596SJohn Baldwin struct m_snd_tag com; 119fb3bc596SJohn Baldwin struct m_snd_tag *tag; 120fb3bc596SJohn Baldwin }; 121fb3bc596SJohn Baldwin 122fb3bc596SJohn Baldwin static inline struct vlan_snd_tag * 123fb3bc596SJohn Baldwin mst_to_vst(struct m_snd_tag *mst) 124fb3bc596SJohn Baldwin { 125fb3bc596SJohn Baldwin 126fb3bc596SJohn Baldwin return (__containerof(mst, struct vlan_snd_tag, com)); 127fb3bc596SJohn Baldwin } 128fb3bc596SJohn Baldwin #endif 129fb3bc596SJohn Baldwin 130d148c2a2SMatt Joras /* 131d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk with 132d148c2a2SMatt Joras * the assumption that none will be added/removed during iteration. 133d148c2a2SMatt Joras */ 134d148c2a2SMatt Joras #ifdef VLAN_ARRAY 135d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 136d148c2a2SMatt Joras size_t _i; \ 137d148c2a2SMatt Joras for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \ 138d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i]) != NULL) 139d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 140d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 141d148c2a2SMatt Joras struct ifvlan *_next; \ 142d148c2a2SMatt Joras size_t _i; \ 143d148c2a2SMatt Joras for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \ 144b08d611dSMatt Macy CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next) 145d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 146d148c2a2SMatt Joras 147d148c2a2SMatt Joras /* 148d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk while 149d148c2a2SMatt Joras * also modifying the number of vlans on the trunk. The iteration continues 150d148c2a2SMatt Joras * until some condition is met or there are no more vlans on the trunk. 151d148c2a2SMatt Joras */ 152d148c2a2SMatt Joras #ifdef VLAN_ARRAY 153d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */ 154d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 155d148c2a2SMatt Joras size_t _i; \ 156d148c2a2SMatt Joras for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \ 157d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i])) 158d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 159d148c2a2SMatt Joras /* 160d148c2a2SMatt Joras * The hash table case is more complicated. We allow for the hash table to be 161d148c2a2SMatt Joras * modified (i.e. vlans removed) while we are iterating over it. To allow for 162d148c2a2SMatt Joras * this we must restart the iteration every time we "touch" something during 163d148c2a2SMatt Joras * the iteration, since removal will resize the hash table and invalidate our 164d148c2a2SMatt Joras * current position. If acting on the touched element causes the trunk to be 165d148c2a2SMatt Joras * emptied, then iteration also stops. 166d148c2a2SMatt Joras */ 167d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 168d148c2a2SMatt Joras size_t _i; \ 169d148c2a2SMatt Joras bool _touch = false; \ 170d148c2a2SMatt Joras for (_i = 0; \ 171d148c2a2SMatt Joras !(_cond) && _i < (1 << (_trunk)->hwidth); \ 172d148c2a2SMatt Joras _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \ 173b08d611dSMatt Macy if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \ 174d148c2a2SMatt Joras (_touch = true)) 175d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 176d148c2a2SMatt Joras 177a3814acfSSam Leffler struct vlan_mc_entry { 178e4cd31ddSJeff Roberson struct sockaddr_dl mc_addr; 179b08d611dSMatt Macy CK_SLIST_ENTRY(vlan_mc_entry) mc_entries; 180c32a9d66SHans Petter Selasky struct epoch_context mc_epoch_ctx; 181a3814acfSSam Leffler }; 182a3814acfSSam Leffler 183a3814acfSSam Leffler struct ifvlan { 18475ee267cSGleb Smirnoff struct ifvlantrunk *ifv_trunk; 185fc74a9f9SBrooks Davis struct ifnet *ifv_ifp; 18675ee267cSGleb Smirnoff #define TRUNK(ifv) ((ifv)->ifv_trunk) 187c7cffd65SAlexander V. Chernikov #define PARENT(ifv) (TRUNK(ifv)->parent) 1886667db31SAlexander V. Chernikov void *ifv_cookie; 1891cf236fbSYaroslav Tykhiy int ifv_pflags; /* special flags we have set on parent */ 190d89baa5aSAlexander Motin int ifv_capenable; 19172755d28SMark Johnston int ifv_encaplen; /* encapsulation length */ 19272755d28SMark Johnston int ifv_mtufudge; /* MTU fudged by this much */ 19372755d28SMark Johnston int ifv_mintu; /* min transmission unit */ 194c7cffd65SAlexander V. Chernikov struct ether_8021q_tag ifv_qtag; 195c7cffd65SAlexander V. Chernikov #define ifv_proto ifv_qtag.proto 196c7cffd65SAlexander V. Chernikov #define ifv_vid ifv_qtag.vid 197c7cffd65SAlexander V. Chernikov #define ifv_pcp ifv_qtag.pcp 198d148c2a2SMatt Joras struct task lladdr_task; 199b08d611dSMatt Macy CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 200c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY 201b08d611dSMatt Macy CK_SLIST_ENTRY(ifvlan) ifv_list; 202c0cb022bSYaroslav Tykhiy #endif 203a3814acfSSam Leffler }; 204a3814acfSSam Leffler 20575ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */ 2061cf236fbSYaroslav Tykhiy static struct { 2071cf236fbSYaroslav Tykhiy int flag; 2081cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 2091cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 2101cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 2111cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 2121cf236fbSYaroslav Tykhiy {0, NULL} 2131cf236fbSYaroslav Tykhiy }; 214a3814acfSSam Leffler 21578bc3d5eSKristof Provost VNET_DECLARE(int, vlan_mtag_pcp); 21678bc3d5eSKristof Provost #define V_vlan_mtag_pcp VNET(vlan_mtag_pcp) 2172ccbbd06SMarcelo Araujo 21842a58907SGleb Smirnoff static const char vlanname[] = "vlan"; 21942a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface"); 2202cc2df49SGarrett Wollman 2215cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag; 222ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag; 223f2ab9160SAndrey V. Elsukov static eventhandler_tag ifevent_tag; 2245cb8c31aSYaroslav Tykhiy 2254faedfe8SSam Leffler /* 226b08d611dSMatt Macy * if_vlan uses two module-level synchronizations primitives to allow concurrent 227b08d611dSMatt Macy * modification of vlan interfaces and (mostly) allow for vlans to be destroyed 228b08d611dSMatt Macy * while they are being used for tx/rx. To accomplish this in a way that has 229b08d611dSMatt Macy * acceptable performance and cooperation with other parts of the network stack 230b08d611dSMatt Macy * there is a non-sleepable epoch(9) and an sx(9). 23175ee267cSGleb Smirnoff * 232b08d611dSMatt Macy * The performance-sensitive paths that warrant using the epoch(9) are 233d148c2a2SMatt Joras * vlan_transmit and vlan_input. Both have to check for the vlan interface's 234d148c2a2SMatt Joras * existence using if_vlantrunk, and being in the network tx/rx paths the use 235b08d611dSMatt Macy * of an epoch(9) gives a measureable improvement in performance. 23675ee267cSGleb Smirnoff * 237d148c2a2SMatt Joras * The reason for having an sx(9) is mostly because there are still areas that 238d148c2a2SMatt Joras * must be sleepable and also have safe concurrent access to a vlan interface. 239d148c2a2SMatt Joras * Since the sx(9) exists, it is used by default in most paths unless sleeping 240d148c2a2SMatt Joras * is not permitted, or if it is not clear whether sleeping is permitted. 241d148c2a2SMatt Joras * 2424faedfe8SSam Leffler */ 243d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx 244d148c2a2SMatt Joras 245d148c2a2SMatt Joras static struct sx _VLAN_SX_ID; 246d148c2a2SMatt Joras 247d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \ 248c7cffd65SAlexander V. Chernikov sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE) 249d148c2a2SMatt Joras 250d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \ 251d148c2a2SMatt Joras sx_destroy(&_VLAN_SX_ID) 252d148c2a2SMatt Joras 253d148c2a2SMatt Joras #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID) 254d148c2a2SMatt Joras #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID) 255d148c2a2SMatt Joras #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID) 256d148c2a2SMatt Joras #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID) 257d148c2a2SMatt Joras #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED) 258d148c2a2SMatt Joras #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED) 259d148c2a2SMatt Joras #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED) 260d148c2a2SMatt Joras 261d148c2a2SMatt Joras /* 262b08d611dSMatt Macy * We also have a per-trunk mutex that should be acquired when changing 263b08d611dSMatt Macy * its state. 264d148c2a2SMatt Joras */ 265b08d611dSMatt Macy #define TRUNK_LOCK_INIT(trunk) mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF) 266b08d611dSMatt Macy #define TRUNK_LOCK_DESTROY(trunk) mtx_destroy(&(trunk)->lock) 267b08d611dSMatt Macy #define TRUNK_WLOCK(trunk) mtx_lock(&(trunk)->lock) 268b08d611dSMatt Macy #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock) 269b08d611dSMatt Macy #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED); 27075ee267cSGleb Smirnoff 271d148c2a2SMatt Joras /* 272d148c2a2SMatt Joras * The VLAN_ARRAY substitutes the dynamic hash with a static array 273d148c2a2SMatt Joras * with 4096 entries. In theory this can give a boost in processing, 274d148c2a2SMatt Joras * however in practice it does not. Probably this is because the array 275d148c2a2SMatt Joras * is too big to fit into CPU cache. 276d148c2a2SMatt Joras */ 27775ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 27875ee267cSGleb Smirnoff static void vlan_inithash(struct ifvlantrunk *trunk); 27975ee267cSGleb Smirnoff static void vlan_freehash(struct ifvlantrunk *trunk); 28075ee267cSGleb Smirnoff static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 28175ee267cSGleb Smirnoff static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 28275ee267cSGleb Smirnoff static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 28375ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 2847983103aSRobert Watson uint16_t vid); 28575ee267cSGleb Smirnoff #endif 28675ee267cSGleb Smirnoff static void trunk_destroy(struct ifvlantrunk *trunk); 2874faedfe8SSam Leffler 288114c608cSYaroslav Tykhiy static void vlan_init(void *foo); 289a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 290cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 291b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 292f3e7afe2SHans Petter Selasky static int vlan_snd_tag_alloc(struct ifnet *, 293f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *, struct m_snd_tag **); 294fb3bc596SJohn Baldwin static int vlan_snd_tag_modify(struct m_snd_tag *, 295fb3bc596SJohn Baldwin union if_snd_tag_modify_params *); 296fb3bc596SJohn Baldwin static int vlan_snd_tag_query(struct m_snd_tag *, 297fb3bc596SJohn Baldwin union if_snd_tag_query_params *); 298fa91f845SRandall Stewart static void vlan_snd_tag_free(struct m_snd_tag *); 2991a714ff2SRandall Stewart static struct m_snd_tag *vlan_next_snd_tag(struct m_snd_tag *); 3001a714ff2SRandall Stewart static void vlan_ratelimit_query(struct ifnet *, 3011a714ff2SRandall Stewart struct if_ratelimit_query_results *); 302f3e7afe2SHans Petter Selasky #endif 303d9b1d615SJohn Baldwin static void vlan_qflush(struct ifnet *ifp); 3041cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 3051cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 3061cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 307f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 308d9b1d615SJohn Baldwin static int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 3092e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 3102e5ff01dSLuiz Otavio O Souza static void vlan_altq_start(struct ifnet *ifp); 3112e5ff01dSLuiz Otavio O Souza static int vlan_altq_transmit(struct ifnet *ifp, struct mbuf *m); 3122e5ff01dSLuiz Otavio O Souza #endif 31316cf6bdbSMatt Joras static int vlan_output(struct ifnet *ifp, struct mbuf *m, 31416cf6bdbSMatt Joras const struct sockaddr *dst, struct route *ro); 3156f359e28SJohn Baldwin static void vlan_unconfig(struct ifnet *ifp); 31628cc4d37SJohn Baldwin static void vlan_unconfig_locked(struct ifnet *ifp, int departing); 317c7cffd65SAlexander V. Chernikov static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag, 318c7cffd65SAlexander V. Chernikov uint16_t proto); 319a6fffd6cSBrooks Davis static void vlan_link_state(struct ifnet *ifp); 32075ee267cSGleb Smirnoff static void vlan_capabilities(struct ifvlan *ifv); 32175ee267cSGleb Smirnoff static void vlan_trunk_capabilities(struct ifnet *ifp); 322f731f104SBill Paul 323f941c31aSGleb Smirnoff static struct ifnet *vlan_clone_match_ethervid(const char *, int *); 324f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 32591ebcbe0SAlexander V. Chernikov static int vlan_clone_create(struct if_clone *, char *, size_t, 32691ebcbe0SAlexander V. Chernikov struct ifc_data *, struct ifnet **); 32791ebcbe0SAlexander V. Chernikov static int vlan_clone_destroy(struct if_clone *, struct ifnet *, uint32_t); 328f889d2efSBrooks Davis 329089104e0SAlexander V. Chernikov static int vlan_clone_create_nl(struct if_clone *ifc, char *name, size_t len, 330089104e0SAlexander V. Chernikov struct ifc_data_nl *ifd); 331089104e0SAlexander V. Chernikov static int vlan_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd); 332089104e0SAlexander V. Chernikov static void vlan_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw); 333089104e0SAlexander V. Chernikov 3345cb8c31aSYaroslav Tykhiy static void vlan_ifdetach(void *arg, struct ifnet *ifp); 335ea4ca115SAndrew Thompson static void vlan_iflladdr(void *arg, struct ifnet *ifp); 336f2ab9160SAndrey V. Elsukov static void vlan_ifevent(void *arg, struct ifnet *ifp, int event); 3375cb8c31aSYaroslav Tykhiy 338d148c2a2SMatt Joras static void vlan_lladdr_fn(void *arg, int pending); 339d148c2a2SMatt Joras 34042a58907SGleb Smirnoff static struct if_clone *vlan_cloner; 3419d4fe4b2SBrooks Davis 342ccf7ba97SMarko Zec #ifdef VIMAGE 3435f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner); 344ccf7ba97SMarko Zec #define V_vlan_cloner VNET(vlan_cloner) 345ccf7ba97SMarko Zec #endif 346ccf7ba97SMarko Zec 347c782ea8bSJohn Baldwin #ifdef RATELIMIT 348c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_ul_sw = { 349c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 350c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 351c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 352c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 353c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_UNLIMITED 354c782ea8bSJohn Baldwin }; 355c782ea8bSJohn Baldwin 356c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_rl_sw = { 357c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 358c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 359c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 360c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 361c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_RATE_LIMIT 362c782ea8bSJohn Baldwin }; 363c782ea8bSJohn Baldwin #endif 364c782ea8bSJohn Baldwin 365c782ea8bSJohn Baldwin #ifdef KERN_TLS 366c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_tls_sw = { 367c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 368c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 369c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 370c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 371c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_TLS 372c782ea8bSJohn Baldwin }; 373c782ea8bSJohn Baldwin 374c782ea8bSJohn Baldwin #ifdef RATELIMIT 375c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_tls_rl_sw = { 376c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 377c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 378c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 379c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 380c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT 381c782ea8bSJohn Baldwin }; 382c782ea8bSJohn Baldwin #endif 383c782ea8bSJohn Baldwin #endif 384c782ea8bSJohn Baldwin 38575ee267cSGleb Smirnoff static void 386c32a9d66SHans Petter Selasky vlan_mc_free(struct epoch_context *ctx) 387c32a9d66SHans Petter Selasky { 388c32a9d66SHans Petter Selasky struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx); 389c32a9d66SHans Petter Selasky free(mc, M_VLAN); 390c32a9d66SHans Petter Selasky } 391c32a9d66SHans Petter Selasky 392cac30248SOleg Bulyzhin #ifndef VLAN_ARRAY 393cac30248SOleg Bulyzhin #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 394cac30248SOleg Bulyzhin 395c32a9d66SHans Petter Selasky static void 39675ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk) 39775ee267cSGleb Smirnoff { 39875ee267cSGleb Smirnoff int i, n; 39975ee267cSGleb Smirnoff 40075ee267cSGleb Smirnoff /* 40175ee267cSGleb Smirnoff * The trunk must not be locked here since we call malloc(M_WAITOK). 40275ee267cSGleb Smirnoff * It is OK in case this function is called before the trunk struct 40375ee267cSGleb Smirnoff * gets hooked up and becomes visible from other threads. 40475ee267cSGleb Smirnoff */ 40575ee267cSGleb Smirnoff 40675ee267cSGleb Smirnoff KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 40775ee267cSGleb Smirnoff ("%s: hash already initialized", __func__)); 40875ee267cSGleb Smirnoff 40975ee267cSGleb Smirnoff trunk->hwidth = VLAN_DEF_HWIDTH; 41075ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 41175ee267cSGleb Smirnoff trunk->hmask = n - 1; 41275ee267cSGleb Smirnoff trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 41375ee267cSGleb Smirnoff for (i = 0; i < n; i++) 414b08d611dSMatt Macy CK_SLIST_INIT(&trunk->hash[i]); 41575ee267cSGleb Smirnoff } 41675ee267cSGleb Smirnoff 41775ee267cSGleb Smirnoff static void 41875ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk) 41975ee267cSGleb Smirnoff { 42075ee267cSGleb Smirnoff #ifdef INVARIANTS 42175ee267cSGleb Smirnoff int i; 42275ee267cSGleb Smirnoff 42375ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 42475ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 425b08d611dSMatt Macy KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]), 42675ee267cSGleb Smirnoff ("%s: hash table not empty", __func__)); 42775ee267cSGleb Smirnoff #endif 42875ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 42975ee267cSGleb Smirnoff trunk->hash = NULL; 43075ee267cSGleb Smirnoff trunk->hwidth = trunk->hmask = 0; 43175ee267cSGleb Smirnoff } 43275ee267cSGleb Smirnoff 43375ee267cSGleb Smirnoff static int 43475ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 43575ee267cSGleb Smirnoff { 43675ee267cSGleb Smirnoff int i, b; 43775ee267cSGleb Smirnoff struct ifvlan *ifv2; 43875ee267cSGleb Smirnoff 439b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 44075ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 44175ee267cSGleb Smirnoff 44275ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 4437983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 444b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 4457983103aSRobert Watson if (ifv->ifv_vid == ifv2->ifv_vid) 44675ee267cSGleb Smirnoff return (EEXIST); 44775ee267cSGleb Smirnoff 44875ee267cSGleb Smirnoff /* 44975ee267cSGleb Smirnoff * Grow the hash when the number of vlans exceeds half of the number of 45075ee267cSGleb Smirnoff * hash buckets squared. This will make the average linked-list length 45175ee267cSGleb Smirnoff * buckets/2. 45275ee267cSGleb Smirnoff */ 45375ee267cSGleb Smirnoff if (trunk->refcnt > (b * b) / 2) { 45475ee267cSGleb Smirnoff vlan_growhash(trunk, 1); 4557983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 45675ee267cSGleb Smirnoff } 457b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 45875ee267cSGleb Smirnoff trunk->refcnt++; 45975ee267cSGleb Smirnoff 46075ee267cSGleb Smirnoff return (0); 46175ee267cSGleb Smirnoff } 46275ee267cSGleb Smirnoff 46375ee267cSGleb Smirnoff static int 46475ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 46575ee267cSGleb Smirnoff { 46675ee267cSGleb Smirnoff int i, b; 46775ee267cSGleb Smirnoff struct ifvlan *ifv2; 46875ee267cSGleb Smirnoff 469b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 47075ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 47175ee267cSGleb Smirnoff 472151abc80SKristof Provost b = 1 << (trunk->hwidth - 1); 4737983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 474b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 47575ee267cSGleb Smirnoff if (ifv2 == ifv) { 47675ee267cSGleb Smirnoff trunk->refcnt--; 477b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list); 47875ee267cSGleb Smirnoff if (trunk->refcnt < (b * b) / 2) 47975ee267cSGleb Smirnoff vlan_growhash(trunk, -1); 48075ee267cSGleb Smirnoff return (0); 48175ee267cSGleb Smirnoff } 48275ee267cSGleb Smirnoff 48375ee267cSGleb Smirnoff panic("%s: vlan not found\n", __func__); 48475ee267cSGleb Smirnoff return (ENOENT); /*NOTREACHED*/ 48575ee267cSGleb Smirnoff } 48675ee267cSGleb Smirnoff 48775ee267cSGleb Smirnoff /* 48875ee267cSGleb Smirnoff * Grow the hash larger or smaller if memory permits. 48975ee267cSGleb Smirnoff */ 49075ee267cSGleb Smirnoff static void 49175ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 49275ee267cSGleb Smirnoff { 49375ee267cSGleb Smirnoff struct ifvlan *ifv; 49475ee267cSGleb Smirnoff struct ifvlanhead *hash2; 49575ee267cSGleb Smirnoff int hwidth2, i, j, n, n2; 49675ee267cSGleb Smirnoff 497b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 49875ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 49975ee267cSGleb Smirnoff 50075ee267cSGleb Smirnoff if (howmuch == 0) { 50175ee267cSGleb Smirnoff /* Harmless yet obvious coding error */ 50275ee267cSGleb Smirnoff printf("%s: howmuch is 0\n", __func__); 50375ee267cSGleb Smirnoff return; 50475ee267cSGleb Smirnoff } 50575ee267cSGleb Smirnoff 50675ee267cSGleb Smirnoff hwidth2 = trunk->hwidth + howmuch; 50775ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 50875ee267cSGleb Smirnoff n2 = 1 << hwidth2; 50975ee267cSGleb Smirnoff /* Do not shrink the table below the default */ 51075ee267cSGleb Smirnoff if (hwidth2 < VLAN_DEF_HWIDTH) 51175ee267cSGleb Smirnoff return; 51275ee267cSGleb Smirnoff 513b08d611dSMatt Macy hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK); 51475ee267cSGleb Smirnoff if (hash2 == NULL) { 51575ee267cSGleb Smirnoff printf("%s: out of memory -- hash size not changed\n", 51675ee267cSGleb Smirnoff __func__); 51775ee267cSGleb Smirnoff return; /* We can live with the old hash table */ 51875ee267cSGleb Smirnoff } 51975ee267cSGleb Smirnoff for (j = 0; j < n2; j++) 520b08d611dSMatt Macy CK_SLIST_INIT(&hash2[j]); 52175ee267cSGleb Smirnoff for (i = 0; i < n; i++) 522b08d611dSMatt Macy while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) { 523b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list); 5247983103aSRobert Watson j = HASH(ifv->ifv_vid, n2 - 1); 525b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 52675ee267cSGleb Smirnoff } 527b08d611dSMatt Macy NET_EPOCH_WAIT(); 52875ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 52975ee267cSGleb Smirnoff trunk->hash = hash2; 53075ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 53175ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 532f84b2d69SYaroslav Tykhiy 533f84b2d69SYaroslav Tykhiy if (bootverbose) 534f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 535f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 53675ee267cSGleb Smirnoff } 53775ee267cSGleb Smirnoff 53875ee267cSGleb Smirnoff static __inline struct ifvlan * 5397983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 54075ee267cSGleb Smirnoff { 54175ee267cSGleb Smirnoff struct ifvlan *ifv; 54275ee267cSGleb Smirnoff 543a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 54475ee267cSGleb Smirnoff 545b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list) 5467983103aSRobert Watson if (ifv->ifv_vid == vid) 54775ee267cSGleb Smirnoff return (ifv); 54875ee267cSGleb Smirnoff return (NULL); 54975ee267cSGleb Smirnoff } 55075ee267cSGleb Smirnoff 55175ee267cSGleb Smirnoff #if 0 55275ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 55375ee267cSGleb Smirnoff static void 55475ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 55575ee267cSGleb Smirnoff { 55675ee267cSGleb Smirnoff int i; 55775ee267cSGleb Smirnoff struct ifvlan *ifv; 55875ee267cSGleb Smirnoff 55975ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 56075ee267cSGleb Smirnoff printf("%d: ", i); 561b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 56275ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 56375ee267cSGleb Smirnoff printf("\n"); 56475ee267cSGleb Smirnoff } 56575ee267cSGleb Smirnoff } 56675ee267cSGleb Smirnoff #endif /* 0 */ 567e4cd31ddSJeff Roberson #else 568e4cd31ddSJeff Roberson 569e4cd31ddSJeff Roberson static __inline struct ifvlan * 5707983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 571e4cd31ddSJeff Roberson { 572e4cd31ddSJeff Roberson 5737983103aSRobert Watson return trunk->vlans[vid]; 574e4cd31ddSJeff Roberson } 575e4cd31ddSJeff Roberson 576e4cd31ddSJeff Roberson static __inline int 577e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 578e4cd31ddSJeff Roberson { 579e4cd31ddSJeff Roberson 5807983103aSRobert Watson if (trunk->vlans[ifv->ifv_vid] != NULL) 581e4cd31ddSJeff Roberson return EEXIST; 5827983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = ifv; 583e4cd31ddSJeff Roberson trunk->refcnt++; 584e4cd31ddSJeff Roberson 585e4cd31ddSJeff Roberson return (0); 586e4cd31ddSJeff Roberson } 587e4cd31ddSJeff Roberson 588e4cd31ddSJeff Roberson static __inline int 589e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 590e4cd31ddSJeff Roberson { 591e4cd31ddSJeff Roberson 5927983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = NULL; 593e4cd31ddSJeff Roberson trunk->refcnt--; 594e4cd31ddSJeff Roberson 595e4cd31ddSJeff Roberson return (0); 596e4cd31ddSJeff Roberson } 597e4cd31ddSJeff Roberson 598e4cd31ddSJeff Roberson static __inline void 599e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk) 600e4cd31ddSJeff Roberson { 601e4cd31ddSJeff Roberson } 602e4cd31ddSJeff Roberson 603e4cd31ddSJeff Roberson static __inline void 604e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk) 605e4cd31ddSJeff Roberson { 606e4cd31ddSJeff Roberson } 607e4cd31ddSJeff Roberson 60875ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 60975ee267cSGleb Smirnoff 61075ee267cSGleb Smirnoff static void 61175ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 61275ee267cSGleb Smirnoff { 613d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 61475ee267cSGleb Smirnoff 61575ee267cSGleb Smirnoff vlan_freehash(trunk); 61675ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 61733499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 6189bcf3ae4SAlexander Motin if_rele(trunk->parent); 61975ee267cSGleb Smirnoff free(trunk, M_VLAN); 62075ee267cSGleb Smirnoff } 62175ee267cSGleb Smirnoff 622f731f104SBill Paul /* 623f731f104SBill Paul * Program our multicast filter. What we're actually doing is 624f731f104SBill Paul * programming the multicast filter of the parent. This has the 625f731f104SBill Paul * side effect of causing the parent interface to receive multicast 626f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 627f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 628f731f104SBill Paul * to avoid this: there really is only one physical interface. 629f731f104SBill Paul */ 6302b120974SPeter Wemm static int 6312b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 632f731f104SBill Paul { 633f731f104SBill Paul struct ifnet *ifp_p; 6342d222cb7SAlexander Motin struct ifmultiaddr *ifma; 635f731f104SBill Paul struct ifvlan *sc; 636c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 637f731f104SBill Paul int error; 638f731f104SBill Paul 639b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 640d148c2a2SMatt Joras 641f731f104SBill Paul /* Find the parent. */ 642f731f104SBill Paul sc = ifp->if_softc; 64375ee267cSGleb Smirnoff ifp_p = PARENT(sc); 6441b2a4f7aSBill Fenner 6458b615593SMarko Zec CURVNET_SET_QUIET(ifp_p->if_vnet); 6468b615593SMarko Zec 647f731f104SBill Paul /* First, remove any existing filter entries. */ 648b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 649b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 6502d222cb7SAlexander Motin (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 6512a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 652f731f104SBill Paul } 653f731f104SBill Paul 654f731f104SBill Paul /* Now program new ones. */ 6552d222cb7SAlexander Motin IF_ADDR_WLOCK(ifp); 656d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 657f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 658f731f104SBill Paul continue; 65929c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 6602d222cb7SAlexander Motin if (mc == NULL) { 6612d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 662c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 66329c2dfbeSBruce M Simpson return (ENOMEM); 6642d222cb7SAlexander Motin } 665e4cd31ddSJeff Roberson bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 666e4cd31ddSJeff Roberson mc->mc_addr.sdl_index = ifp_p->if_index; 667b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 6682d222cb7SAlexander Motin } 6692d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 670b08d611dSMatt Macy CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) { 671e4cd31ddSJeff Roberson error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 6722d222cb7SAlexander Motin NULL); 673c6b2d024SGeorge V. Neville-Neil if (error) { 674c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 675f731f104SBill Paul return (error); 676f731f104SBill Paul } 677c6b2d024SGeorge V. Neville-Neil } 678f731f104SBill Paul 6798b615593SMarko Zec CURVNET_RESTORE(); 680f731f104SBill Paul return (0); 681f731f104SBill Paul } 6822cc2df49SGarrett Wollman 683a3814acfSSam Leffler /* 684f2ab9160SAndrey V. Elsukov * A handler for interface ifnet events. 685f2ab9160SAndrey V. Elsukov */ 686f2ab9160SAndrey V. Elsukov static void 687f2ab9160SAndrey V. Elsukov vlan_ifevent(void *arg __unused, struct ifnet *ifp, int event) 688f2ab9160SAndrey V. Elsukov { 689f2ab9160SAndrey V. Elsukov struct epoch_tracker et; 690f2ab9160SAndrey V. Elsukov struct ifvlan *ifv; 691f2ab9160SAndrey V. Elsukov struct ifvlantrunk *trunk; 692f2ab9160SAndrey V. Elsukov 693f2ab9160SAndrey V. Elsukov if (event != IFNET_EVENT_UPDATE_BAUDRATE) 694f2ab9160SAndrey V. Elsukov return; 695f2ab9160SAndrey V. Elsukov 696f2ab9160SAndrey V. Elsukov NET_EPOCH_ENTER(et); 697f2ab9160SAndrey V. Elsukov trunk = ifp->if_vlantrunk; 698f2ab9160SAndrey V. Elsukov if (trunk == NULL) { 699f2ab9160SAndrey V. Elsukov NET_EPOCH_EXIT(et); 700f2ab9160SAndrey V. Elsukov return; 701f2ab9160SAndrey V. Elsukov } 702f2ab9160SAndrey V. Elsukov 703f2ab9160SAndrey V. Elsukov TRUNK_WLOCK(trunk); 704f2ab9160SAndrey V. Elsukov VLAN_FOREACH(ifv, trunk) { 705f2ab9160SAndrey V. Elsukov ifv->ifv_ifp->if_baudrate = ifp->if_baudrate; 706f2ab9160SAndrey V. Elsukov } 707f2ab9160SAndrey V. Elsukov TRUNK_WUNLOCK(trunk); 708f2ab9160SAndrey V. Elsukov NET_EPOCH_EXIT(et); 709f2ab9160SAndrey V. Elsukov } 710f2ab9160SAndrey V. Elsukov 711f2ab9160SAndrey V. Elsukov /* 712ea4ca115SAndrew Thompson * A handler for parent interface link layer address changes. 713ea4ca115SAndrew Thompson * If the parent interface link layer address is changed we 714ea4ca115SAndrew Thompson * should also change it on all children vlans. 715ea4ca115SAndrew Thompson */ 716ea4ca115SAndrew Thompson static void 717ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 718ea4ca115SAndrew Thompson { 719a68cc388SGleb Smirnoff struct epoch_tracker et; 720ea4ca115SAndrew Thompson struct ifvlan *ifv; 721d148c2a2SMatt Joras struct ifnet *ifv_ifp; 722d148c2a2SMatt Joras struct ifvlantrunk *trunk; 723d148c2a2SMatt Joras struct sockaddr_dl *sdl; 724ea4ca115SAndrew Thompson 7257b7f772fSGleb Smirnoff /* Need the epoch since this is run on taskqueue_swi. */ 726a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 727d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 728d148c2a2SMatt Joras if (trunk == NULL) { 729a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 730ea4ca115SAndrew Thompson return; 731d148c2a2SMatt Joras } 732ea4ca115SAndrew Thompson 733ea4ca115SAndrew Thompson /* 734ea4ca115SAndrew Thompson * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 735d148c2a2SMatt Joras * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR 736d148c2a2SMatt Joras * ioctl calls on the parent garbling the lladdr of the child vlan. 737ea4ca115SAndrew Thompson */ 738d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 739d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 740d148c2a2SMatt Joras /* 741d148c2a2SMatt Joras * Copy new new lladdr into the ifv_ifp, enqueue a task 742d148c2a2SMatt Joras * to actually call if_setlladdr. if_setlladdr needs to 743d148c2a2SMatt Joras * be deferred to a taskqueue because it will call into 744d148c2a2SMatt Joras * the if_vlan ioctl path and try to acquire the global 745d148c2a2SMatt Joras * lock. 746d148c2a2SMatt Joras */ 747d148c2a2SMatt Joras ifv_ifp = ifv->ifv_ifp; 748d148c2a2SMatt Joras bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp), 749e4cd31ddSJeff Roberson ifp->if_addrlen); 750d148c2a2SMatt Joras sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr; 751d148c2a2SMatt Joras sdl->sdl_alen = ifp->if_addrlen; 752d148c2a2SMatt Joras taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 7536117727bSAndrew Thompson } 754d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 755a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 756ea4ca115SAndrew Thompson } 757ea4ca115SAndrew Thompson 758ea4ca115SAndrew Thompson /* 7595cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 7605cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 7615cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 7625428776eSJohn Baldwin * by ejecting its hot-plug card. However, if an ifnet is simply 7635428776eSJohn Baldwin * being renamed, then there's no need to tear down the state. 7645cb8c31aSYaroslav Tykhiy */ 7655cb8c31aSYaroslav Tykhiy static void 7665cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 7675cb8c31aSYaroslav Tykhiy { 7685cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 769d148c2a2SMatt Joras struct ifvlantrunk *trunk; 7705cb8c31aSYaroslav Tykhiy 7715428776eSJohn Baldwin /* If the ifnet is just being renamed, don't do anything. */ 7725428776eSJohn Baldwin if (ifp->if_flags & IFF_RENAMING) 7735428776eSJohn Baldwin return; 774d148c2a2SMatt Joras VLAN_XLOCK(); 775d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 776d148c2a2SMatt Joras if (trunk == NULL) { 777d148c2a2SMatt Joras VLAN_XUNLOCK(); 778d148c2a2SMatt Joras return; 779d148c2a2SMatt Joras } 7805428776eSJohn Baldwin 7815cb8c31aSYaroslav Tykhiy /* 7825cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 7835cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 7845cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 7855cb8c31aSYaroslav Tykhiy */ 786d148c2a2SMatt Joras VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk, 787d148c2a2SMatt Joras ifp->if_vlantrunk == NULL) 78828cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 789d148c2a2SMatt Joras 7905cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 7915cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 792d148c2a2SMatt Joras VLAN_XUNLOCK(); 7935cb8c31aSYaroslav Tykhiy } 7945cb8c31aSYaroslav Tykhiy 7955cb8c31aSYaroslav Tykhiy /* 796e4cd31ddSJeff Roberson * Return the trunk device for a virtual interface. 797e4cd31ddSJeff Roberson */ 798e4cd31ddSJeff Roberson static struct ifnet * 799e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp) 800e4cd31ddSJeff Roberson { 801e4cd31ddSJeff Roberson struct ifvlan *ifv; 802e4cd31ddSJeff Roberson 803b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 804b8a6e03fSGleb Smirnoff 805e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 806e4cd31ddSJeff Roberson return (NULL); 807d148c2a2SMatt Joras 808e4cd31ddSJeff Roberson ifv = ifp->if_softc; 809e4cd31ddSJeff Roberson ifp = NULL; 810e4cd31ddSJeff Roberson if (ifv->ifv_trunk) 811e4cd31ddSJeff Roberson ifp = PARENT(ifv); 812e4cd31ddSJeff Roberson return (ifp); 813e4cd31ddSJeff Roberson } 814e4cd31ddSJeff Roberson 815e4cd31ddSJeff Roberson /* 8167983103aSRobert Watson * Return the 12-bit VLAN VID for this interface, for use by external 8177983103aSRobert Watson * components such as Infiniband. 8187983103aSRobert Watson * 8197983103aSRobert Watson * XXXRW: Note that the function name here is historical; it should be named 8207983103aSRobert Watson * vlan_vid(). 821e4cd31ddSJeff Roberson */ 822e4cd31ddSJeff Roberson static int 8237983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp) 824e4cd31ddSJeff Roberson { 825e4cd31ddSJeff Roberson struct ifvlan *ifv; 826e4cd31ddSJeff Roberson 827e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 828e4cd31ddSJeff Roberson return (EINVAL); 829e4cd31ddSJeff Roberson ifv = ifp->if_softc; 8307983103aSRobert Watson *vidp = ifv->ifv_vid; 831e4cd31ddSJeff Roberson return (0); 832e4cd31ddSJeff Roberson } 833e4cd31ddSJeff Roberson 83432d2623aSNavdeep Parhar static int 83532d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp) 83632d2623aSNavdeep Parhar { 83732d2623aSNavdeep Parhar struct ifvlan *ifv; 83832d2623aSNavdeep Parhar 83932d2623aSNavdeep Parhar if (ifp->if_type != IFT_L2VLAN) 84032d2623aSNavdeep Parhar return (EINVAL); 84132d2623aSNavdeep Parhar ifv = ifp->if_softc; 84232d2623aSNavdeep Parhar *pcpp = ifv->ifv_pcp; 84332d2623aSNavdeep Parhar return (0); 84432d2623aSNavdeep Parhar } 84532d2623aSNavdeep Parhar 846e4cd31ddSJeff Roberson /* 847e4cd31ddSJeff Roberson * Return a driver specific cookie for this interface. Synchronization 848e4cd31ddSJeff Roberson * with setcookie must be provided by the driver. 849e4cd31ddSJeff Roberson */ 850e4cd31ddSJeff Roberson static void * 851e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp) 852e4cd31ddSJeff Roberson { 853e4cd31ddSJeff Roberson struct ifvlan *ifv; 854e4cd31ddSJeff Roberson 855e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 856e4cd31ddSJeff Roberson return (NULL); 857e4cd31ddSJeff Roberson ifv = ifp->if_softc; 858e4cd31ddSJeff Roberson return (ifv->ifv_cookie); 859e4cd31ddSJeff Roberson } 860e4cd31ddSJeff Roberson 861e4cd31ddSJeff Roberson /* 862e4cd31ddSJeff Roberson * Store a cookie in our softc that drivers can use to store driver 863e4cd31ddSJeff Roberson * private per-instance data in. 864e4cd31ddSJeff Roberson */ 865e4cd31ddSJeff Roberson static int 866e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie) 867e4cd31ddSJeff Roberson { 868e4cd31ddSJeff Roberson struct ifvlan *ifv; 869e4cd31ddSJeff Roberson 870e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 871e4cd31ddSJeff Roberson return (EINVAL); 872e4cd31ddSJeff Roberson ifv = ifp->if_softc; 873e4cd31ddSJeff Roberson ifv->ifv_cookie = cookie; 874e4cd31ddSJeff Roberson return (0); 875e4cd31ddSJeff Roberson } 876e4cd31ddSJeff Roberson 877e4cd31ddSJeff Roberson /* 8787983103aSRobert Watson * Return the vlan device present at the specific VID. 879e4cd31ddSJeff Roberson */ 880e4cd31ddSJeff Roberson static struct ifnet * 8817983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid) 882e4cd31ddSJeff Roberson { 883e4cd31ddSJeff Roberson struct ifvlantrunk *trunk; 884e4cd31ddSJeff Roberson struct ifvlan *ifv; 885e4cd31ddSJeff Roberson 886b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 887b8a6e03fSGleb Smirnoff 888e4cd31ddSJeff Roberson trunk = ifp->if_vlantrunk; 889b8a6e03fSGleb Smirnoff if (trunk == NULL) 890e4cd31ddSJeff Roberson return (NULL); 891e4cd31ddSJeff Roberson ifp = NULL; 8927983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 893e4cd31ddSJeff Roberson if (ifv) 894e4cd31ddSJeff Roberson ifp = ifv->ifv_ifp; 895e4cd31ddSJeff Roberson return (ifp); 896e4cd31ddSJeff Roberson } 897e4cd31ddSJeff Roberson 898e4cd31ddSJeff Roberson /* 899a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 900a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 901a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 902a3814acfSSam Leffler * set here. No one else in the system should be aware of this so 903a3814acfSSam Leffler * we use an explicit reference here. 904a3814acfSSam Leffler */ 905a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 906a3814acfSSam Leffler 907984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 908a6fffd6cSBrooks Davis extern void (*vlan_link_state_p)(struct ifnet *); 909127d7b2dSAndre Oppermann 910089104e0SAlexander V. Chernikov static struct if_clone_addreq_v2 vlan_addreq = { 911089104e0SAlexander V. Chernikov .version = 2, 91291ebcbe0SAlexander V. Chernikov .match_f = vlan_clone_match, 91391ebcbe0SAlexander V. Chernikov .create_f = vlan_clone_create, 91491ebcbe0SAlexander V. Chernikov .destroy_f = vlan_clone_destroy, 915089104e0SAlexander V. Chernikov .create_nl_f = vlan_clone_create_nl, 916089104e0SAlexander V. Chernikov .modify_nl_f = vlan_clone_modify_nl, 917089104e0SAlexander V. Chernikov .dump_nl_f = vlan_clone_dump_nl, 91891ebcbe0SAlexander V. Chernikov }; 91991ebcbe0SAlexander V. Chernikov 9202b120974SPeter Wemm static int 9212b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 9222b120974SPeter Wemm { 9239d4fe4b2SBrooks Davis 9242b120974SPeter Wemm switch (type) { 9252b120974SPeter Wemm case MOD_LOAD: 9265cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 9275cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 9285cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 9295cb8c31aSYaroslav Tykhiy return (ENOMEM); 930ea4ca115SAndrew Thompson iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 931ea4ca115SAndrew Thompson vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 932ea4ca115SAndrew Thompson if (iflladdr_tag == NULL) 933ea4ca115SAndrew Thompson return (ENOMEM); 934f2ab9160SAndrey V. Elsukov ifevent_tag = EVENTHANDLER_REGISTER(ifnet_event, 935f2ab9160SAndrey V. Elsukov vlan_ifevent, NULL, EVENTHANDLER_PRI_ANY); 936f2ab9160SAndrey V. Elsukov if (ifevent_tag == NULL) 937f2ab9160SAndrey V. Elsukov return (ENOMEM); 938d148c2a2SMatt Joras VLAN_LOCKING_INIT(); 9399d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 940127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 94175ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 942e4cd31ddSJeff Roberson vlan_trunkdev_p = vlan_trunkdev; 943e4cd31ddSJeff Roberson vlan_cookie_p = vlan_cookie; 944e4cd31ddSJeff Roberson vlan_setcookie_p = vlan_setcookie; 945e4cd31ddSJeff Roberson vlan_tag_p = vlan_tag; 94632d2623aSNavdeep Parhar vlan_pcp_p = vlan_pcp; 947e4cd31ddSJeff Roberson vlan_devat_p = vlan_devat; 948ccf7ba97SMarko Zec #ifndef VIMAGE 949089104e0SAlexander V. Chernikov vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq); 950ccf7ba97SMarko Zec #endif 95125c0f7b3SYaroslav Tykhiy if (bootverbose) 95225c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 95325c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 95425c0f7b3SYaroslav Tykhiy "full-size arrays" 95525c0f7b3SYaroslav Tykhiy #else 95625c0f7b3SYaroslav Tykhiy "hash tables with chaining" 95725c0f7b3SYaroslav Tykhiy #endif 95825c0f7b3SYaroslav Tykhiy 95925c0f7b3SYaroslav Tykhiy "\n"); 9602b120974SPeter Wemm break; 9612b120974SPeter Wemm case MOD_UNLOAD: 962ccf7ba97SMarko Zec #ifndef VIMAGE 96391ebcbe0SAlexander V. Chernikov ifc_detach_cloner(vlan_cloner); 964ccf7ba97SMarko Zec #endif 9655cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 966ea4ca115SAndrew Thompson EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 967f2ab9160SAndrey V. Elsukov EVENTHANDLER_DEREGISTER(ifnet_event, ifevent_tag); 9689d4fe4b2SBrooks Davis vlan_input_p = NULL; 969127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 97075ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 971e4cd31ddSJeff Roberson vlan_trunkdev_p = NULL; 972e4cd31ddSJeff Roberson vlan_tag_p = NULL; 97309fe6320SNavdeep Parhar vlan_cookie_p = NULL; 97409fe6320SNavdeep Parhar vlan_setcookie_p = NULL; 975e4cd31ddSJeff Roberson vlan_devat_p = NULL; 976d148c2a2SMatt Joras VLAN_LOCKING_DESTROY(); 97725c0f7b3SYaroslav Tykhiy if (bootverbose) 97825c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 9799d4fe4b2SBrooks Davis break; 9803e019deaSPoul-Henning Kamp default: 9813e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 9822b120974SPeter Wemm } 98315a66c21SBruce M Simpson return (0); 9842b120974SPeter Wemm } 9852b120974SPeter Wemm 9862b120974SPeter Wemm static moduledata_t vlan_mod = { 9872b120974SPeter Wemm "if_vlan", 9882b120974SPeter Wemm vlan_modevent, 9899823d527SKevin Lo 0 9902b120974SPeter Wemm }; 9912b120974SPeter Wemm 9922b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 99311edc477SEd Maste MODULE_VERSION(if_vlan, 3); 9942cc2df49SGarrett Wollman 995ccf7ba97SMarko Zec #ifdef VIMAGE 996ccf7ba97SMarko Zec static void 997ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused) 998ccf7ba97SMarko Zec { 999089104e0SAlexander V. Chernikov vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq); 1000ccf7ba97SMarko Zec V_vlan_cloner = vlan_cloner; 1001ccf7ba97SMarko Zec } 1002ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 1003ccf7ba97SMarko Zec vnet_vlan_init, NULL); 1004ccf7ba97SMarko Zec 1005ccf7ba97SMarko Zec static void 1006ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused) 1007ccf7ba97SMarko Zec { 1008ccf7ba97SMarko Zec 100991ebcbe0SAlexander V. Chernikov ifc_detach_cloner(V_vlan_cloner); 1010ccf7ba97SMarko Zec } 1011eb03a443SKristof Provost VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 1012ccf7ba97SMarko Zec vnet_vlan_uninit, NULL); 1013ccf7ba97SMarko Zec #endif 1014ccf7ba97SMarko Zec 1015f941c31aSGleb Smirnoff /* 1016c7cffd65SAlexander V. Chernikov * Check for <etherif>.<vlan>[.<vlan> ...] style interface names. 1017f941c31aSGleb Smirnoff */ 1018f889d2efSBrooks Davis static struct ifnet * 1019f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp) 10209d4fe4b2SBrooks Davis { 1021f941c31aSGleb Smirnoff char ifname[IFNAMSIZ]; 1022f941c31aSGleb Smirnoff char *cp; 1023f889d2efSBrooks Davis struct ifnet *ifp; 10247983103aSRobert Watson int vid; 1025f889d2efSBrooks Davis 1026f941c31aSGleb Smirnoff strlcpy(ifname, name, IFNAMSIZ); 1027c7cffd65SAlexander V. Chernikov if ((cp = strrchr(ifname, '.')) == NULL) 1028f941c31aSGleb Smirnoff return (NULL); 1029f941c31aSGleb Smirnoff *cp = '\0'; 10309bcf3ae4SAlexander Motin if ((ifp = ifunit_ref(ifname)) == NULL) 1031f941c31aSGleb Smirnoff return (NULL); 1032f941c31aSGleb Smirnoff /* Parse VID. */ 10339bcf3ae4SAlexander Motin if (*++cp == '\0') { 10349bcf3ae4SAlexander Motin if_rele(ifp); 1035f941c31aSGleb Smirnoff return (NULL); 10369bcf3ae4SAlexander Motin } 10377983103aSRobert Watson vid = 0; 1038fb92ad4aSJohn Baldwin for(; *cp >= '0' && *cp <= '9'; cp++) 10397983103aSRobert Watson vid = (vid * 10) + (*cp - '0'); 10409bcf3ae4SAlexander Motin if (*cp != '\0') { 10419bcf3ae4SAlexander Motin if_rele(ifp); 1042f941c31aSGleb Smirnoff return (NULL); 10439bcf3ae4SAlexander Motin } 10447983103aSRobert Watson if (vidp != NULL) 10457983103aSRobert Watson *vidp = vid; 1046f889d2efSBrooks Davis 104715a66c21SBruce M Simpson return (ifp); 1048f889d2efSBrooks Davis } 1049f889d2efSBrooks Davis 1050f889d2efSBrooks Davis static int 1051f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 1052f889d2efSBrooks Davis { 10534be465abSAlexander V. Chernikov struct ifnet *ifp; 1054f889d2efSBrooks Davis const char *cp; 1055f889d2efSBrooks Davis 10564be465abSAlexander V. Chernikov ifp = vlan_clone_match_ethervid(name, NULL); 10574be465abSAlexander V. Chernikov if (ifp != NULL) { 10584be465abSAlexander V. Chernikov if_rele(ifp); 1059f889d2efSBrooks Davis return (1); 10604be465abSAlexander V. Chernikov } 1061f889d2efSBrooks Davis 106242a58907SGleb Smirnoff if (strncmp(vlanname, name, strlen(vlanname)) != 0) 1063f889d2efSBrooks Davis return (0); 1064f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 1065f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 1066f889d2efSBrooks Davis return (0); 1067f889d2efSBrooks Davis } 1068f889d2efSBrooks Davis 1069f889d2efSBrooks Davis return (1); 1070f889d2efSBrooks Davis } 1071f889d2efSBrooks Davis 1072f889d2efSBrooks Davis static int 107391ebcbe0SAlexander V. Chernikov vlan_clone_create(struct if_clone *ifc, char *name, size_t len, 107491ebcbe0SAlexander V. Chernikov struct ifc_data *ifd, struct ifnet **ifpp) 1075f889d2efSBrooks Davis { 1076f889d2efSBrooks Davis char *dp; 107753729367SAlexander V. Chernikov bool wildcard = false; 107853729367SAlexander V. Chernikov bool subinterface = false; 1079f889d2efSBrooks Davis int unit; 1080f889d2efSBrooks Davis int error; 108153729367SAlexander V. Chernikov int vid = 0; 108253729367SAlexander V. Chernikov uint16_t proto = ETHERTYPE_VLAN; 10839d4fe4b2SBrooks Davis struct ifvlan *ifv; 10849d4fe4b2SBrooks Davis struct ifnet *ifp; 108553729367SAlexander V. Chernikov struct ifnet *p = NULL; 10863ba24fdeSJohn Baldwin struct ifaddr *ifa; 10873ba24fdeSJohn Baldwin struct sockaddr_dl *sdl; 10886b7330e2SSam Leffler struct vlanreq vlr; 108965239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 1090f889d2efSBrooks Davis 1091c7cffd65SAlexander V. Chernikov 10926b7330e2SSam Leffler /* 109353729367SAlexander V. Chernikov * There are three ways to specify the cloned device: 10946b7330e2SSam Leffler * o pass a parameter block with the clone request. 109553729367SAlexander V. Chernikov * o specify parameters in the text of the clone device name 10966b7330e2SSam Leffler * o specify no parameters and get an unattached device that 10976b7330e2SSam Leffler * must be configured separately. 109853729367SAlexander V. Chernikov * The first technique is preferred; the latter two are supported 1099c7cffd65SAlexander V. Chernikov * for backwards compatibility. 11007983103aSRobert Watson * 11017983103aSRobert Watson * XXXRW: Note historic use of the word "tag" here. New ioctls may be 11027983103aSRobert Watson * called for. 11036b7330e2SSam Leffler */ 110453729367SAlexander V. Chernikov 110591ebcbe0SAlexander V. Chernikov if (ifd->params != NULL) { 110691ebcbe0SAlexander V. Chernikov error = ifc_copyin(ifd, &vlr, sizeof(vlr)); 11076b7330e2SSam Leffler if (error) 11086b7330e2SSam Leffler return error; 110953729367SAlexander V. Chernikov vid = vlr.vlr_tag; 111053729367SAlexander V. Chernikov proto = vlr.vlr_proto; 111153729367SAlexander V. Chernikov 1112afbb64f1SAlexander V. Chernikov #ifdef COMPAT_FREEBSD12 1113afbb64f1SAlexander V. Chernikov if (proto == 0) 1114afbb64f1SAlexander V. Chernikov proto = ETHERTYPE_VLAN; 1115afbb64f1SAlexander V. Chernikov #endif 11169bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 11176b7330e2SSam Leffler if (p == NULL) 1118b1828acfSGleb Smirnoff return (ENXIO); 111953729367SAlexander V. Chernikov } 112053729367SAlexander V. Chernikov 112153729367SAlexander V. Chernikov if ((error = ifc_name2unit(name, &unit)) == 0) { 112253729367SAlexander V. Chernikov 112353729367SAlexander V. Chernikov /* 112453729367SAlexander V. Chernikov * vlanX interface. Set wildcard to true if the unit number 112553729367SAlexander V. Chernikov * is not fixed (-1) 112653729367SAlexander V. Chernikov */ 112753729367SAlexander V. Chernikov wildcard = (unit < 0); 112853729367SAlexander V. Chernikov } else { 112953729367SAlexander V. Chernikov struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid); 113053729367SAlexander V. Chernikov if (p_tmp != NULL) { 113153729367SAlexander V. Chernikov error = 0; 113253729367SAlexander V. Chernikov subinterface = true; 113353729367SAlexander V. Chernikov unit = IF_DUNIT_NONE; 113453729367SAlexander V. Chernikov wildcard = false; 113553729367SAlexander V. Chernikov if (p != NULL) { 113653729367SAlexander V. Chernikov if_rele(p_tmp); 113753729367SAlexander V. Chernikov if (p != p_tmp) 113853729367SAlexander V. Chernikov error = EINVAL; 113953729367SAlexander V. Chernikov } else 114053729367SAlexander V. Chernikov p = p_tmp; 114153729367SAlexander V. Chernikov } else 114253729367SAlexander V. Chernikov error = ENXIO; 114353729367SAlexander V. Chernikov } 114453729367SAlexander V. Chernikov 11459bcf3ae4SAlexander Motin if (error != 0) { 114653729367SAlexander V. Chernikov if (p != NULL) 11479bcf3ae4SAlexander Motin if_rele(p); 11486b7330e2SSam Leffler return (error); 11499bcf3ae4SAlexander Motin } 1150f889d2efSBrooks Davis 115153729367SAlexander V. Chernikov if (!subinterface) { 115253729367SAlexander V. Chernikov /* vlanX interface, mark X as busy or allocate new unit # */ 1153f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 11549bcf3ae4SAlexander Motin if (error != 0) { 11559bcf3ae4SAlexander Motin if (p != NULL) 11569bcf3ae4SAlexander Motin if_rele(p); 1157f889d2efSBrooks Davis return (error); 11589bcf3ae4SAlexander Motin } 115953729367SAlexander V. Chernikov } 1160f889d2efSBrooks Davis 1161f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 1162f889d2efSBrooks Davis if (wildcard) { 1163f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 1164f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 1165f889d2efSBrooks Davis len - (dp-name) - 1) { 1166f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 1167f889d2efSBrooks Davis } 1168f889d2efSBrooks Davis } 11699d4fe4b2SBrooks Davis 1170a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 1171fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 1172fc74a9f9SBrooks Davis if (ifp == NULL) { 117353729367SAlexander V. Chernikov if (!subinterface) 1174fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 1175fc74a9f9SBrooks Davis free(ifv, M_VLAN); 11769bcf3ae4SAlexander Motin if (p != NULL) 11779bcf3ae4SAlexander Motin if_rele(p); 1178fc74a9f9SBrooks Davis return (ENOSPC); 1179fc74a9f9SBrooks Davis } 1180b08d611dSMatt Macy CK_SLIST_INIT(&ifv->vlan_mc_listhead); 11819d4fe4b2SBrooks Davis ifp->if_softc = ifv; 1182f889d2efSBrooks Davis /* 1183cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 1184f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 1185f889d2efSBrooks Davis */ 1186f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 118742a58907SGleb Smirnoff ifp->if_dname = vlanname; 1188f889d2efSBrooks Davis ifp->if_dunit = unit; 11899d4fe4b2SBrooks Davis 1190114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 11912e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 11922e5ff01dSLuiz Otavio O Souza ifp->if_start = vlan_altq_start; 11932e5ff01dSLuiz Otavio O Souza ifp->if_transmit = vlan_altq_transmit; 11942e5ff01dSLuiz Otavio O Souza IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 11952e5ff01dSLuiz Otavio O Souza ifp->if_snd.ifq_drv_maxlen = 0; 11962e5ff01dSLuiz Otavio O Souza IFQ_SET_READY(&ifp->if_snd); 11972e5ff01dSLuiz Otavio O Souza #else 1198d9b1d615SJohn Baldwin ifp->if_transmit = vlan_transmit; 11992e5ff01dSLuiz Otavio O Souza #endif 1200d9b1d615SJohn Baldwin ifp->if_qflush = vlan_qflush; 12019d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 1202b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1203f3e7afe2SHans Petter Selasky ifp->if_snd_tag_alloc = vlan_snd_tag_alloc; 12041a714ff2SRandall Stewart ifp->if_ratelimit_query = vlan_ratelimit_query; 1205f3e7afe2SHans Petter Selasky #endif 120664a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 1207fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 12089d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 1209211f625aSBill Fenner ifp->if_baudrate = 0; 1210a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 1211a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 12123ba24fdeSJohn Baldwin ifa = ifp->if_addr; 12133ba24fdeSJohn Baldwin sdl = (struct sockaddr_dl *)ifa->ifa_addr; 12143ba24fdeSJohn Baldwin sdl->sdl_type = IFT_L2VLAN; 12159d4fe4b2SBrooks Davis 12169bcf3ae4SAlexander Motin if (p != NULL) { 1217c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vid, proto); 12189bcf3ae4SAlexander Motin if_rele(p); 1219f889d2efSBrooks Davis if (error != 0) { 1220f889d2efSBrooks Davis /* 122128cc4d37SJohn Baldwin * Since we've partially failed, we need to back 1222f889d2efSBrooks Davis * out all the way, otherwise userland could get 1223f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 1224f889d2efSBrooks Davis */ 1225f889d2efSBrooks Davis ether_ifdetach(ifp); 1226249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 12274b22573aSBrooks Davis if_free(ifp); 122853729367SAlexander V. Chernikov if (!subinterface) 122996c8ef3aSMaxim Konovalov ifc_free_unit(ifc, unit); 1230f889d2efSBrooks Davis free(ifv, M_VLAN); 1231f889d2efSBrooks Davis 1232f889d2efSBrooks Davis return (error); 1233f889d2efSBrooks Davis } 1234f889d2efSBrooks Davis } 123591ebcbe0SAlexander V. Chernikov *ifpp = ifp; 1236f889d2efSBrooks Davis 12379d4fe4b2SBrooks Davis return (0); 12389d4fe4b2SBrooks Davis } 12399d4fe4b2SBrooks Davis 1240089104e0SAlexander V. Chernikov /* 1241089104e0SAlexander V. Chernikov * 1242089104e0SAlexander V. Chernikov * Parsers of IFLA_INFO_DATA inside IFLA_LINKINFO of RTM_NEWLINK 1243089104e0SAlexander V. Chernikov * {{nla_len=8, nla_type=IFLA_LINK}, 2}, 1244089104e0SAlexander V. Chernikov * {{nla_len=12, nla_type=IFLA_IFNAME}, "xvlan22"}, 1245089104e0SAlexander V. Chernikov * {{nla_len=24, nla_type=IFLA_LINKINFO}, 1246089104e0SAlexander V. Chernikov * [ 1247089104e0SAlexander V. Chernikov * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...}, 1248089104e0SAlexander V. Chernikov * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]} 1249089104e0SAlexander V. Chernikov */ 1250089104e0SAlexander V. Chernikov 1251089104e0SAlexander V. Chernikov struct nl_parsed_vlan { 1252089104e0SAlexander V. Chernikov uint16_t vlan_id; 1253089104e0SAlexander V. Chernikov uint16_t vlan_proto; 1254089104e0SAlexander V. Chernikov struct ifla_vlan_flags vlan_flags; 1255089104e0SAlexander V. Chernikov }; 1256089104e0SAlexander V. Chernikov 1257089104e0SAlexander V. Chernikov #define _OUT(_field) offsetof(struct nl_parsed_vlan, _field) 1258089104e0SAlexander V. Chernikov static const struct nlattr_parser nla_p_vlan[] = { 1259089104e0SAlexander V. Chernikov { .type = IFLA_VLAN_ID, .off = _OUT(vlan_id), .cb = nlattr_get_uint16 }, 1260089104e0SAlexander V. Chernikov { .type = IFLA_VLAN_FLAGS, .off = _OUT(vlan_flags), .cb = nlattr_get_nla }, 1261089104e0SAlexander V. Chernikov { .type = IFLA_VLAN_PROTOCOL, .off = _OUT(vlan_proto), .cb = nlattr_get_uint16 }, 1262089104e0SAlexander V. Chernikov }; 1263089104e0SAlexander V. Chernikov #undef _OUT 1264089104e0SAlexander V. Chernikov NL_DECLARE_ATTR_PARSER(vlan_parser, nla_p_vlan); 1265089104e0SAlexander V. Chernikov 1266089104e0SAlexander V. Chernikov static int 1267089104e0SAlexander V. Chernikov vlan_clone_create_nl(struct if_clone *ifc, char *name, size_t len, 1268089104e0SAlexander V. Chernikov struct ifc_data_nl *ifd) 1269089104e0SAlexander V. Chernikov { 1270089104e0SAlexander V. Chernikov struct epoch_tracker et; 1271089104e0SAlexander V. Chernikov struct ifnet *ifp_parent; 1272089104e0SAlexander V. Chernikov struct nl_pstate *npt = ifd->npt; 1273089104e0SAlexander V. Chernikov struct nl_parsed_link *lattrs = ifd->lattrs; 1274089104e0SAlexander V. Chernikov int error; 1275089104e0SAlexander V. Chernikov 1276089104e0SAlexander V. Chernikov /* 1277089104e0SAlexander V. Chernikov * lattrs.ifla_ifname is the new interface name 1278089104e0SAlexander V. Chernikov * lattrs.ifi_index contains parent interface index 1279089104e0SAlexander V. Chernikov * lattrs.ifla_idata contains un-parsed vlan data 1280089104e0SAlexander V. Chernikov */ 1281089104e0SAlexander V. Chernikov struct nl_parsed_vlan attrs = { 1282089104e0SAlexander V. Chernikov .vlan_id = 0xFEFE, 1283089104e0SAlexander V. Chernikov .vlan_proto = ETHERTYPE_VLAN 1284089104e0SAlexander V. Chernikov }; 1285089104e0SAlexander V. Chernikov 1286089104e0SAlexander V. Chernikov if (lattrs->ifla_idata == NULL) { 1287089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "vlan id is required, guessing not supported"); 1288089104e0SAlexander V. Chernikov return (ENOTSUP); 1289089104e0SAlexander V. Chernikov } 1290089104e0SAlexander V. Chernikov 1291089104e0SAlexander V. Chernikov error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, npt, &attrs); 1292089104e0SAlexander V. Chernikov if (error != 0) 1293089104e0SAlexander V. Chernikov return (error); 1294089104e0SAlexander V. Chernikov if (attrs.vlan_id > 4095) { 1295089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "Invalid VID: %d", attrs.vlan_id); 1296089104e0SAlexander V. Chernikov return (EINVAL); 1297089104e0SAlexander V. Chernikov } 1298089104e0SAlexander V. Chernikov if (attrs.vlan_proto != ETHERTYPE_VLAN && attrs.vlan_proto != ETHERTYPE_QINQ) { 1299089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "Unsupported ethertype: 0x%04X", attrs.vlan_proto); 1300089104e0SAlexander V. Chernikov return (ENOTSUP); 1301089104e0SAlexander V. Chernikov } 1302089104e0SAlexander V. Chernikov 1303089104e0SAlexander V. Chernikov struct vlanreq params = { 1304089104e0SAlexander V. Chernikov .vlr_tag = attrs.vlan_id, 1305089104e0SAlexander V. Chernikov .vlr_proto = attrs.vlan_proto, 1306089104e0SAlexander V. Chernikov }; 1307089104e0SAlexander V. Chernikov struct ifc_data ifd_new = { .flags = IFC_F_SYSSPACE, .unit = ifd->unit, .params = ¶ms }; 1308089104e0SAlexander V. Chernikov 1309089104e0SAlexander V. Chernikov NET_EPOCH_ENTER(et); 1310089104e0SAlexander V. Chernikov ifp_parent = ifnet_byindex(lattrs->ifi_index); 1311089104e0SAlexander V. Chernikov if (ifp_parent != NULL) 1312089104e0SAlexander V. Chernikov strlcpy(params.vlr_parent, if_name(ifp_parent), sizeof(params.vlr_parent)); 1313089104e0SAlexander V. Chernikov NET_EPOCH_EXIT(et); 1314089104e0SAlexander V. Chernikov 1315089104e0SAlexander V. Chernikov if (ifp_parent == NULL) { 1316089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "unable to find parent interface %u", lattrs->ifi_index); 1317089104e0SAlexander V. Chernikov return (ENOENT); 1318089104e0SAlexander V. Chernikov } 1319089104e0SAlexander V. Chernikov 1320089104e0SAlexander V. Chernikov error = vlan_clone_create(ifc, name, len, &ifd_new, &ifd->ifp); 1321089104e0SAlexander V. Chernikov 1322089104e0SAlexander V. Chernikov return (error); 1323089104e0SAlexander V. Chernikov } 1324089104e0SAlexander V. Chernikov 1325089104e0SAlexander V. Chernikov static int 1326089104e0SAlexander V. Chernikov vlan_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd) 1327089104e0SAlexander V. Chernikov { 1328089104e0SAlexander V. Chernikov struct nl_parsed_link *lattrs = ifd->lattrs; 1329089104e0SAlexander V. Chernikov 1330089104e0SAlexander V. Chernikov if ((lattrs->ifla_idata != NULL) && ((ifd->flags & IFC_F_CREATE) == 0)) { 1331089104e0SAlexander V. Chernikov struct epoch_tracker et; 1332089104e0SAlexander V. Chernikov struct nl_parsed_vlan attrs = { 1333089104e0SAlexander V. Chernikov .vlan_proto = ETHERTYPE_VLAN, 1334089104e0SAlexander V. Chernikov }; 1335089104e0SAlexander V. Chernikov int error; 1336089104e0SAlexander V. Chernikov 1337089104e0SAlexander V. Chernikov error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, ifd->npt, &attrs); 1338089104e0SAlexander V. Chernikov if (error != 0) 1339089104e0SAlexander V. Chernikov return (error); 1340089104e0SAlexander V. Chernikov 1341089104e0SAlexander V. Chernikov NET_EPOCH_ENTER(et); 1342089104e0SAlexander V. Chernikov struct ifnet *ifp_parent = ifnet_byindex_ref(lattrs->ifla_link); 1343089104e0SAlexander V. Chernikov NET_EPOCH_EXIT(et); 1344089104e0SAlexander V. Chernikov 1345089104e0SAlexander V. Chernikov if (ifp_parent == NULL) { 1346089104e0SAlexander V. Chernikov nlmsg_report_err_msg(ifd->npt, "unable to find parent interface %u", 1347089104e0SAlexander V. Chernikov lattrs->ifla_link); 1348089104e0SAlexander V. Chernikov return (ENOENT); 1349089104e0SAlexander V. Chernikov } 1350089104e0SAlexander V. Chernikov 1351089104e0SAlexander V. Chernikov struct ifvlan *ifv = ifp->if_softc; 1352089104e0SAlexander V. Chernikov error = vlan_config(ifv, ifp_parent, attrs.vlan_id, attrs.vlan_proto); 1353089104e0SAlexander V. Chernikov 1354089104e0SAlexander V. Chernikov if_rele(ifp_parent); 1355089104e0SAlexander V. Chernikov if (error != 0) 1356089104e0SAlexander V. Chernikov return (error); 1357089104e0SAlexander V. Chernikov } 1358089104e0SAlexander V. Chernikov 1359089104e0SAlexander V. Chernikov return (nl_modify_ifp_generic(ifp, ifd->lattrs, ifd->bm, ifd->npt)); 1360089104e0SAlexander V. Chernikov } 1361089104e0SAlexander V. Chernikov 1362089104e0SAlexander V. Chernikov /* 1363089104e0SAlexander V. Chernikov * {{nla_len=24, nla_type=IFLA_LINKINFO}, 1364089104e0SAlexander V. Chernikov * [ 1365089104e0SAlexander V. Chernikov * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...}, 1366089104e0SAlexander V. Chernikov * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]} 1367089104e0SAlexander V. Chernikov */ 1368089104e0SAlexander V. Chernikov static void 1369089104e0SAlexander V. Chernikov vlan_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw) 1370089104e0SAlexander V. Chernikov { 1371089104e0SAlexander V. Chernikov uint32_t parent_index = 0; 1372089104e0SAlexander V. Chernikov uint16_t vlan_id = 0; 1373089104e0SAlexander V. Chernikov uint16_t vlan_proto = 0; 1374089104e0SAlexander V. Chernikov 1375089104e0SAlexander V. Chernikov VLAN_SLOCK(); 1376089104e0SAlexander V. Chernikov struct ifvlan *ifv = ifp->if_softc; 1377089104e0SAlexander V. Chernikov if (TRUNK(ifv) != NULL) 1378089104e0SAlexander V. Chernikov parent_index = PARENT(ifv)->if_index; 1379089104e0SAlexander V. Chernikov vlan_id = ifv->ifv_vid; 1380089104e0SAlexander V. Chernikov vlan_proto = ifv->ifv_proto; 1381089104e0SAlexander V. Chernikov VLAN_SUNLOCK(); 1382089104e0SAlexander V. Chernikov 1383089104e0SAlexander V. Chernikov if (parent_index != 0) 1384089104e0SAlexander V. Chernikov nlattr_add_u32(nw, IFLA_LINK, parent_index); 1385089104e0SAlexander V. Chernikov 1386089104e0SAlexander V. Chernikov int off = nlattr_add_nested(nw, IFLA_LINKINFO); 1387089104e0SAlexander V. Chernikov if (off != 0) { 1388089104e0SAlexander V. Chernikov nlattr_add_string(nw, IFLA_INFO_KIND, "vlan"); 1389089104e0SAlexander V. Chernikov int off2 = nlattr_add_nested(nw, IFLA_INFO_DATA); 1390089104e0SAlexander V. Chernikov if (off2 != 0) { 1391089104e0SAlexander V. Chernikov nlattr_add_u16(nw, IFLA_VLAN_ID, vlan_id); 1392089104e0SAlexander V. Chernikov nlattr_add_u16(nw, IFLA_VLAN_PROTOCOL, vlan_proto); 1393089104e0SAlexander V. Chernikov nlattr_set_len(nw, off2); 1394089104e0SAlexander V. Chernikov } 1395089104e0SAlexander V. Chernikov nlattr_set_len(nw, off); 1396089104e0SAlexander V. Chernikov } 1397089104e0SAlexander V. Chernikov } 1398089104e0SAlexander V. Chernikov 1399f889d2efSBrooks Davis static int 140091ebcbe0SAlexander V. Chernikov vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 14019d4fe4b2SBrooks Davis { 14029d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 140353729367SAlexander V. Chernikov int unit = ifp->if_dunit; 1404c7cffd65SAlexander V. Chernikov 1405c7cffd65SAlexander V. Chernikov if (ifp->if_vlantrunk) 1406c7cffd65SAlexander V. Chernikov return (EBUSY); 1407b4e9f837SBrooks Davis 14082e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 14092e5ff01dSLuiz Otavio O Souza IFQ_PURGE(&ifp->if_snd); 14102e5ff01dSLuiz Otavio O Souza #endif 1411249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 1412249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 1413d148c2a2SMatt Joras /* 1414d148c2a2SMatt Joras * We should have the only reference to the ifv now, so we can now 1415d148c2a2SMatt Joras * drain any remaining lladdr task before freeing the ifnet and the 1416d148c2a2SMatt Joras * ifvlan. 1417d148c2a2SMatt Joras */ 1418d148c2a2SMatt Joras taskqueue_drain(taskqueue_thread, &ifv->lladdr_task); 1419b08d611dSMatt Macy NET_EPOCH_WAIT(); 14204b22573aSBrooks Davis if_free(ifp); 14219d4fe4b2SBrooks Davis free(ifv, M_VLAN); 142253729367SAlexander V. Chernikov if (unit != IF_DUNIT_NONE) 142353729367SAlexander V. Chernikov ifc_free_unit(ifc, unit); 1424b4e9f837SBrooks Davis 1425f889d2efSBrooks Davis return (0); 14269d4fe4b2SBrooks Davis } 14279d4fe4b2SBrooks Davis 142815a66c21SBruce M Simpson /* 142915a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 143015a66c21SBruce M Simpson */ 14312cc2df49SGarrett Wollman static void 1432114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 14332cc2df49SGarrett Wollman { 14342cc2df49SGarrett Wollman } 14352cc2df49SGarrett Wollman 14366d3a3ab7SGleb Smirnoff /* 1437d9b1d615SJohn Baldwin * The if_transmit method for vlan(4) interface. 14386d3a3ab7SGleb Smirnoff */ 1439d9b1d615SJohn Baldwin static int 1440d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m) 14412cc2df49SGarrett Wollman { 14422cc2df49SGarrett Wollman struct ifvlan *ifv; 14432cc2df49SGarrett Wollman struct ifnet *p; 14441ad7a257SPyun YongHyeon int error, len, mcast; 14452cc2df49SGarrett Wollman 1446b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1447b8a6e03fSGleb Smirnoff 14482cc2df49SGarrett Wollman ifv = ifp->if_softc; 1449d148c2a2SMatt Joras if (TRUNK(ifv) == NULL) { 1450d148c2a2SMatt Joras if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1451d148c2a2SMatt Joras m_freem(m); 1452d148c2a2SMatt Joras return (ENETDOWN); 1453d148c2a2SMatt Joras } 145475ee267cSGleb Smirnoff p = PARENT(ifv); 14551ad7a257SPyun YongHyeon len = m->m_pkthdr.len; 14561ad7a257SPyun YongHyeon mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 14572cc2df49SGarrett Wollman 1458a3814acfSSam Leffler BPF_MTAP(ifp, m); 14592cc2df49SGarrett Wollman 1460b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1461fb3bc596SJohn Baldwin if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) { 1462fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 1463fb3bc596SJohn Baldwin struct m_snd_tag *mst; 1464fb3bc596SJohn Baldwin 1465fb3bc596SJohn Baldwin MPASS(m->m_pkthdr.snd_tag->ifp == ifp); 1466fb3bc596SJohn Baldwin mst = m->m_pkthdr.snd_tag; 1467fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 1468fb3bc596SJohn Baldwin if (vst->tag->ifp != p) { 1469fb3bc596SJohn Baldwin if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1470fb3bc596SJohn Baldwin m_freem(m); 1471fb3bc596SJohn Baldwin return (EAGAIN); 1472fb3bc596SJohn Baldwin } 1473fb3bc596SJohn Baldwin 1474fb3bc596SJohn Baldwin m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag); 1475fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 1476fb3bc596SJohn Baldwin } 1477fb3bc596SJohn Baldwin #endif 1478fb3bc596SJohn Baldwin 1479f731f104SBill Paul /* 1480d9b1d615SJohn Baldwin * Do not run parent's if_transmit() if the parent is not up, 148124993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 148224993214SYaroslav Tykhiy */ 14832dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 1484a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1485d148c2a2SMatt Joras m_freem(m); 14868b20f6cfSHiroki Sato return (ENETDOWN); 148724993214SYaroslav Tykhiy } 148824993214SYaroslav Tykhiy 1489c7cffd65SAlexander V. Chernikov if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) { 1490a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1491d9b1d615SJohn Baldwin return (0); 14924af90a4dSMatthew N. Dodd } 14932cc2df49SGarrett Wollman 14942cc2df49SGarrett Wollman /* 14952cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 14962cc2df49SGarrett Wollman */ 1497aea78d20SKip Macy error = (p->if_transmit)(p, m); 1498299153b5SAlexander V. Chernikov if (error == 0) { 1499a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1500a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 1501a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast); 15021ad7a257SPyun YongHyeon } else 1503a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1504d9b1d615SJohn Baldwin return (error); 15052cc2df49SGarrett Wollman } 1506d9b1d615SJohn Baldwin 150716cf6bdbSMatt Joras static int 150816cf6bdbSMatt Joras vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 150916cf6bdbSMatt Joras struct route *ro) 151016cf6bdbSMatt Joras { 151116cf6bdbSMatt Joras struct ifvlan *ifv; 151216cf6bdbSMatt Joras struct ifnet *p; 151316cf6bdbSMatt Joras 1514b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1515b8a6e03fSGleb Smirnoff 1516c7cffd65SAlexander V. Chernikov /* 1517c7cffd65SAlexander V. Chernikov * Find the first non-VLAN parent interface. 1518c7cffd65SAlexander V. Chernikov */ 151916cf6bdbSMatt Joras ifv = ifp->if_softc; 1520c7cffd65SAlexander V. Chernikov do { 152116cf6bdbSMatt Joras if (TRUNK(ifv) == NULL) { 152216cf6bdbSMatt Joras m_freem(m); 152316cf6bdbSMatt Joras return (ENETDOWN); 152416cf6bdbSMatt Joras } 152516cf6bdbSMatt Joras p = PARENT(ifv); 1526c7cffd65SAlexander V. Chernikov ifv = p->if_softc; 1527c7cffd65SAlexander V. Chernikov } while (p->if_type == IFT_L2VLAN); 1528c7cffd65SAlexander V. Chernikov 152916cf6bdbSMatt Joras return p->if_output(ifp, m, dst, ro); 153016cf6bdbSMatt Joras } 153116cf6bdbSMatt Joras 15322e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 15332e5ff01dSLuiz Otavio O Souza static void 15342e5ff01dSLuiz Otavio O Souza vlan_altq_start(if_t ifp) 15352e5ff01dSLuiz Otavio O Souza { 15362e5ff01dSLuiz Otavio O Souza struct ifaltq *ifq = &ifp->if_snd; 15372e5ff01dSLuiz Otavio O Souza struct mbuf *m; 15382e5ff01dSLuiz Otavio O Souza 15392e5ff01dSLuiz Otavio O Souza IFQ_LOCK(ifq); 15402e5ff01dSLuiz Otavio O Souza IFQ_DEQUEUE_NOLOCK(ifq, m); 15412e5ff01dSLuiz Otavio O Souza while (m != NULL) { 15422e5ff01dSLuiz Otavio O Souza vlan_transmit(ifp, m); 15432e5ff01dSLuiz Otavio O Souza IFQ_DEQUEUE_NOLOCK(ifq, m); 15442e5ff01dSLuiz Otavio O Souza } 15452e5ff01dSLuiz Otavio O Souza IFQ_UNLOCK(ifq); 15462e5ff01dSLuiz Otavio O Souza } 15472e5ff01dSLuiz Otavio O Souza 15482e5ff01dSLuiz Otavio O Souza static int 15492e5ff01dSLuiz Otavio O Souza vlan_altq_transmit(if_t ifp, struct mbuf *m) 15502e5ff01dSLuiz Otavio O Souza { 15512e5ff01dSLuiz Otavio O Souza int err; 15522e5ff01dSLuiz Otavio O Souza 15532e5ff01dSLuiz Otavio O Souza if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 15542e5ff01dSLuiz Otavio O Souza IFQ_ENQUEUE(&ifp->if_snd, m, err); 15552e5ff01dSLuiz Otavio O Souza if (err == 0) 15562e5ff01dSLuiz Otavio O Souza vlan_altq_start(ifp); 15572e5ff01dSLuiz Otavio O Souza } else 15582e5ff01dSLuiz Otavio O Souza err = vlan_transmit(ifp, m); 15592e5ff01dSLuiz Otavio O Souza 15602e5ff01dSLuiz Otavio O Souza return (err); 15612e5ff01dSLuiz Otavio O Souza } 15622e5ff01dSLuiz Otavio O Souza #endif /* ALTQ */ 15632e5ff01dSLuiz Otavio O Souza 1564d9b1d615SJohn Baldwin /* 1565d9b1d615SJohn Baldwin * The ifp->if_qflush entry point for vlan(4) is a no-op. 1566d9b1d615SJohn Baldwin */ 1567d9b1d615SJohn Baldwin static void 1568d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused) 1569d9b1d615SJohn Baldwin { 1570f731f104SBill Paul } 1571f731f104SBill Paul 1572a3814acfSSam Leffler static void 1573a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 1574f731f104SBill Paul { 1575d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1576f731f104SBill Paul struct ifvlan *ifv; 15772ccbbd06SMarcelo Araujo struct m_tag *mtag; 15782ccbbd06SMarcelo Araujo uint16_t vid, tag; 157975ee267cSGleb Smirnoff 1580b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1581b8a6e03fSGleb Smirnoff 1582d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1583d148c2a2SMatt Joras if (trunk == NULL) { 1584d148c2a2SMatt Joras m_freem(m); 1585d148c2a2SMatt Joras return; 1586d148c2a2SMatt Joras } 1587a3814acfSSam Leffler 1588f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 1589a3814acfSSam Leffler /* 159014e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 1591a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 1592a3814acfSSam Leffler */ 15932ccbbd06SMarcelo Araujo tag = m->m_pkthdr.ether_vtag; 15946ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 1595a3814acfSSam Leffler } else { 159675ee267cSGleb Smirnoff struct ether_vlan_header *evl; 159775ee267cSGleb Smirnoff 159814e98256SYaroslav Tykhiy /* 159914e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 160014e98256SYaroslav Tykhiy */ 1601a3814acfSSam Leffler switch (ifp->if_type) { 1602a3814acfSSam Leffler case IFT_ETHER: 1603a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 1604a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 1605a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 1606a3814acfSSam Leffler return; 1607a3814acfSSam Leffler } 1608a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 16092ccbbd06SMarcelo Araujo tag = ntohs(evl->evl_tag); 1610db8b5973SYaroslav Tykhiy 1611db8b5973SYaroslav Tykhiy /* 16122dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 16132dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 16142dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 16152dc879b3SYaroslav Tykhiy * type field is already in place. 1616db8b5973SYaroslav Tykhiy */ 16172dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 16182dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 16192dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 1620a3814acfSSam Leffler break; 16212dc879b3SYaroslav Tykhiy 1622a3814acfSSam Leffler default: 1623db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 162460c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 162560c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 1626a3814acfSSam Leffler #endif 16273751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1628d148c2a2SMatt Joras m_freem(m); 162960c60618SYaroslav Tykhiy return; 1630a3814acfSSam Leffler } 16317a46ec8fSBrooks Davis } 16327a46ec8fSBrooks Davis 16332ccbbd06SMarcelo Araujo vid = EVL_VLANOFTAG(tag); 16342ccbbd06SMarcelo Araujo 16357983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 16362dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 1637b08d611dSMatt Macy if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1638d148c2a2SMatt Joras m_freem(m); 163975ee267cSGleb Smirnoff return; 164075ee267cSGleb Smirnoff } 1641f731f104SBill Paul 164278bc3d5eSKristof Provost if (V_vlan_mtag_pcp) { 16432ccbbd06SMarcelo Araujo /* 16442ccbbd06SMarcelo Araujo * While uncommon, it is possible that we will find a 802.1q 16452ccbbd06SMarcelo Araujo * packet encapsulated inside another packet that also had an 16462ccbbd06SMarcelo Araujo * 802.1q header. For example, ethernet tunneled over IPSEC 16472ccbbd06SMarcelo Araujo * arriving over ethernet. In that case, we replace the 16482ccbbd06SMarcelo Araujo * existing 802.1q PCP m_tag value. 16492ccbbd06SMarcelo Araujo */ 16502ccbbd06SMarcelo Araujo mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 16512ccbbd06SMarcelo Araujo if (mtag == NULL) { 16522ccbbd06SMarcelo Araujo mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN, 16532ccbbd06SMarcelo Araujo sizeof(uint8_t), M_NOWAIT); 16542ccbbd06SMarcelo Araujo if (mtag == NULL) { 16552ccbbd06SMarcelo Araujo if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1656d148c2a2SMatt Joras m_freem(m); 16572ccbbd06SMarcelo Araujo return; 16582ccbbd06SMarcelo Araujo } 16592ccbbd06SMarcelo Araujo m_tag_prepend(m, mtag); 16602ccbbd06SMarcelo Araujo } 16612ccbbd06SMarcelo Araujo *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag); 16622ccbbd06SMarcelo Araujo } 16632ccbbd06SMarcelo Araujo 1664fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 1665c0304424SGleb Smirnoff if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1); 16662cc2df49SGarrett Wollman 1667a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 1668fdbf1174SMatt Joras (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m); 16692cc2df49SGarrett Wollman } 16702cc2df49SGarrett Wollman 1671d148c2a2SMatt Joras static void 1672d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused) 1673d148c2a2SMatt Joras { 1674d148c2a2SMatt Joras struct ifvlan *ifv; 1675d148c2a2SMatt Joras struct ifnet *ifp; 1676d148c2a2SMatt Joras 1677d148c2a2SMatt Joras ifv = (struct ifvlan *)arg; 1678d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 16795191a3aeSKristof Provost 16805191a3aeSKristof Provost CURVNET_SET(ifp->if_vnet); 16815191a3aeSKristof Provost 1682d148c2a2SMatt Joras /* The ifv_ifp already has the lladdr copied in. */ 1683d148c2a2SMatt Joras if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen); 16845191a3aeSKristof Provost 16855191a3aeSKristof Provost CURVNET_RESTORE(); 1686d148c2a2SMatt Joras } 1687d148c2a2SMatt Joras 16882cc2df49SGarrett Wollman static int 1689c7cffd65SAlexander V. Chernikov vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid, 1690c7cffd65SAlexander V. Chernikov uint16_t proto) 16912cc2df49SGarrett Wollman { 16926dcec895SGleb Smirnoff struct epoch_tracker et; 169375ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 16941cf236fbSYaroslav Tykhiy struct ifnet *ifp; 169575ee267cSGleb Smirnoff int error = 0; 16962cc2df49SGarrett Wollman 1697b1828acfSGleb Smirnoff /* 1698b1828acfSGleb Smirnoff * We can handle non-ethernet hardware types as long as 1699b1828acfSGleb Smirnoff * they handle the tagging and headers themselves. 1700b1828acfSGleb Smirnoff */ 1701e4cd31ddSJeff Roberson if (p->if_type != IFT_ETHER && 1702c7cffd65SAlexander V. Chernikov p->if_type != IFT_L2VLAN && 1703e4cd31ddSJeff Roberson (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 170415a66c21SBruce M Simpson return (EPROTONOSUPPORT); 170564a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 170664a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 1707b1828acfSGleb Smirnoff /* 1708b1828acfSGleb Smirnoff * Don't let the caller set up a VLAN VID with 1709b1828acfSGleb Smirnoff * anything except VLID bits. 1710b1828acfSGleb Smirnoff * VID numbers 0x0 and 0xFFF are reserved. 1711b1828acfSGleb Smirnoff */ 1712b1828acfSGleb Smirnoff if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK)) 1713b1828acfSGleb Smirnoff return (EINVAL); 1714663f556bSKristof Provost if (ifv->ifv_trunk) { 1715663f556bSKristof Provost trunk = ifv->ifv_trunk; 1716663f556bSKristof Provost if (trunk->parent != p) 171715a66c21SBruce M Simpson return (EBUSY); 17182cc2df49SGarrett Wollman 1719d148c2a2SMatt Joras VLAN_XLOCK(); 1720663f556bSKristof Provost 1721663f556bSKristof Provost ifv->ifv_proto = proto; 1722663f556bSKristof Provost 1723663f556bSKristof Provost if (ifv->ifv_vid != vid) { 1724663f556bSKristof Provost /* Re-hash */ 1725663f556bSKristof Provost vlan_remhash(trunk, ifv); 1726663f556bSKristof Provost ifv->ifv_vid = vid; 1727663f556bSKristof Provost error = vlan_inshash(trunk, ifv); 1728663f556bSKristof Provost } 1729663f556bSKristof Provost /* Will unlock */ 1730663f556bSKristof Provost goto done; 1731663f556bSKristof Provost } 1732663f556bSKristof Provost 1733663f556bSKristof Provost VLAN_XLOCK(); 173475ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 173575ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 173675ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 173775ee267cSGleb Smirnoff vlan_inithash(trunk); 173875ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 1739d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 174075ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 174175ee267cSGleb Smirnoff trunk->parent = p; 17429bcf3ae4SAlexander Motin if_ref(trunk->parent); 1743b08d611dSMatt Macy TRUNK_WUNLOCK(trunk); 174475ee267cSGleb Smirnoff } else { 174575ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 174675ee267cSGleb Smirnoff } 174775ee267cSGleb Smirnoff 17487983103aSRobert Watson ifv->ifv_vid = vid; /* must set this before vlan_inshash() */ 17492ccbbd06SMarcelo Araujo ifv->ifv_pcp = 0; /* Default: best effort delivery. */ 175075ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 175175ee267cSGleb Smirnoff if (error) 175275ee267cSGleb Smirnoff goto done; 1753c7cffd65SAlexander V. Chernikov ifv->ifv_proto = proto; 1754a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1755a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 17561cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1757d89baa5aSAlexander Motin ifv->ifv_capenable = -1; 1758a3814acfSSam Leffler 1759a3814acfSSam Leffler /* 1760a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1761a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1762656acce4SYaroslav Tykhiy * use it. 1763a3814acfSSam Leffler */ 1764656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1765656acce4SYaroslav Tykhiy /* 1766656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1767656acce4SYaroslav Tykhiy * handle extended frames. 1768656acce4SYaroslav Tykhiy */ 1769a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1770656acce4SYaroslav Tykhiy } else { 1771a3814acfSSam Leffler /* 1772a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1773a3814acfSSam Leffler * makes us incompatible with strictly compliant 1774a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1775a3814acfSSam Leffler * the feature with other NetBSD implementations, 1776a3814acfSSam Leffler * which might still be useful. 1777a3814acfSSam Leffler */ 1778a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1779a3814acfSSam Leffler } 1780a3814acfSSam Leffler 178175ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 17821cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 1783e4cd31ddSJeff Roberson /* 1784e4cd31ddSJeff Roberson * Initialize fields from our parent. This duplicates some 1785e4cd31ddSJeff Roberson * work with ether_ifattach() but allows for non-ethernet 1786e4cd31ddSJeff Roberson * interfaces to also work. 1787e4cd31ddSJeff Roberson */ 17881cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 178975ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 1790e4cd31ddSJeff Roberson ifp->if_input = p->if_input; 1791e4cd31ddSJeff Roberson ifp->if_resolvemulti = p->if_resolvemulti; 1792e4cd31ddSJeff Roberson ifp->if_addrlen = p->if_addrlen; 1793e4cd31ddSJeff Roberson ifp->if_broadcastaddr = p->if_broadcastaddr; 179432a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 1795e4cd31ddSJeff Roberson 17962cc2df49SGarrett Wollman /* 179716cf6bdbSMatt Joras * We wrap the parent's if_output using vlan_output to ensure that it 179816cf6bdbSMatt Joras * can't become stale. 179916cf6bdbSMatt Joras */ 180016cf6bdbSMatt Joras ifp->if_output = vlan_output; 180116cf6bdbSMatt Joras 180216cf6bdbSMatt Joras /* 180324993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 180424993214SYaroslav Tykhiy * Other flags are none of our business. 18052cc2df49SGarrett Wollman */ 180664a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 18071cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 18081cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 18091cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 18101cf236fbSYaroslav Tykhiy 18111cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 18122cc2df49SGarrett Wollman 18136dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 181475ee267cSGleb Smirnoff vlan_capabilities(ifv); 18156dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 1816a3814acfSSam Leffler 1817a3814acfSSam Leffler /* 1818e4cd31ddSJeff Roberson * Set up our interface address to reflect the underlying 18192cc2df49SGarrett Wollman * physical interface's. 18202cc2df49SGarrett Wollman */ 1821a961401eSAndrey V. Elsukov TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv); 1822e4cd31ddSJeff Roberson ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1823e4cd31ddSJeff Roberson p->if_addrlen; 18241b2a4f7aSBill Fenner 1825a961401eSAndrey V. Elsukov /* 1826a961401eSAndrey V. Elsukov * Do not schedule link address update if it was the same 1827a961401eSAndrey V. Elsukov * as previous parent's. This helps avoid updating for each 1828a961401eSAndrey V. Elsukov * associated llentry. 1829a961401eSAndrey V. Elsukov */ 1830a961401eSAndrey V. Elsukov if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) { 1831a961401eSAndrey V. Elsukov bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1832a961401eSAndrey V. Elsukov taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 1833a961401eSAndrey V. Elsukov } 18342ada9747SYaroslav Tykhiy 18352ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 18362ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 1837d148c2a2SMatt Joras 1838d148c2a2SMatt Joras /* Update flags on the parent, if necessary. */ 1839d148c2a2SMatt Joras vlan_setflags(ifp, 1); 1840b08d611dSMatt Macy 1841d148c2a2SMatt Joras /* 1842b08d611dSMatt Macy * Configure multicast addresses that may already be 1843b08d611dSMatt Macy * joined on the vlan device. 1844d148c2a2SMatt Joras */ 1845b08d611dSMatt Macy (void)vlan_setmulti(ifp); 1846b08d611dSMatt Macy 1847b08d611dSMatt Macy done: 1848c725524cSJack F Vogel if (error == 0) 18497983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid); 1850d148c2a2SMatt Joras VLAN_XUNLOCK(); 185175ee267cSGleb Smirnoff 185275ee267cSGleb Smirnoff return (error); 18532cc2df49SGarrett Wollman } 18542cc2df49SGarrett Wollman 18556f359e28SJohn Baldwin static void 1856f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1857f731f104SBill Paul { 18585cb8c31aSYaroslav Tykhiy 1859d148c2a2SMatt Joras VLAN_XLOCK(); 186028cc4d37SJohn Baldwin vlan_unconfig_locked(ifp, 0); 1861d148c2a2SMatt Joras VLAN_XUNLOCK(); 18625cb8c31aSYaroslav Tykhiy } 18635cb8c31aSYaroslav Tykhiy 18646f359e28SJohn Baldwin static void 186528cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing) 18665cb8c31aSYaroslav Tykhiy { 186775ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1868f731f104SBill Paul struct vlan_mc_entry *mc; 1869f731f104SBill Paul struct ifvlan *ifv; 1870c725524cSJack F Vogel struct ifnet *parent; 187128cc4d37SJohn Baldwin int error; 1872f731f104SBill Paul 1873d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 18744faedfe8SSam Leffler 1875f731f104SBill Paul ifv = ifp->if_softc; 187675ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 187722893351SJack F Vogel parent = NULL; 1878f731f104SBill Paul 187922893351SJack F Vogel if (trunk != NULL) { 188022893351SJack F Vogel parent = trunk->parent; 18811b2a4f7aSBill Fenner 1882f731f104SBill Paul /* 1883f731f104SBill Paul * Since the interface is being unconfigured, we need to 1884f731f104SBill Paul * empty the list of multicast groups that we may have joined 18851b2a4f7aSBill Fenner * while we were alive from the parent's list. 1886f731f104SBill Paul */ 1887b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 18886f359e28SJohn Baldwin /* 188928cc4d37SJohn Baldwin * If the parent interface is being detached, 1890b90dde2fSJohn Baldwin * all its multicast addresses have already 189128cc4d37SJohn Baldwin * been removed. Warn about errors if 189228cc4d37SJohn Baldwin * if_delmulti() does fail, but don't abort as 189328cc4d37SJohn Baldwin * all callers expect vlan destruction to 189428cc4d37SJohn Baldwin * succeed. 18956f359e28SJohn Baldwin */ 189628cc4d37SJohn Baldwin if (!departing) { 189728cc4d37SJohn Baldwin error = if_delmulti(parent, 1898e4cd31ddSJeff Roberson (struct sockaddr *)&mc->mc_addr); 189928cc4d37SJohn Baldwin if (error) 190028cc4d37SJohn Baldwin if_printf(ifp, 190128cc4d37SJohn Baldwin "Failed to delete multicast address from parent: %d\n", 190228cc4d37SJohn Baldwin error); 190328cc4d37SJohn Baldwin } 1904b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 19052a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 1906f731f104SBill Paul } 1907a3814acfSSam Leffler 19081cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 1909d148c2a2SMatt Joras 191075ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 191175ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 191275ee267cSGleb Smirnoff 191375ee267cSGleb Smirnoff /* 191475ee267cSGleb Smirnoff * Check if we were the last. 191575ee267cSGleb Smirnoff */ 191675ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 19172d222cb7SAlexander Motin parent->if_vlantrunk = NULL; 1918b08d611dSMatt Macy NET_EPOCH_WAIT(); 191975ee267cSGleb Smirnoff trunk_destroy(trunk); 1920d148c2a2SMatt Joras } 19211b2a4f7aSBill Fenner } 1922f731f104SBill Paul 1923f731f104SBill Paul /* Disconnect from parent. */ 19241cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 19251cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 19265cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 19275cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 19285cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1929f731f104SBill Paul 193022893351SJack F Vogel /* 193122893351SJack F Vogel * Only dispatch an event if vlan was 193222893351SJack F Vogel * attached, otherwise there is nothing 193322893351SJack F Vogel * to cleanup anyway. 193422893351SJack F Vogel */ 193522893351SJack F Vogel if (parent != NULL) 19367983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid); 1937f731f104SBill Paul } 1938f731f104SBill Paul 19391cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1940f731f104SBill Paul static int 19411cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 19421cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1943a3814acfSSam Leffler { 19441cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 19451cf236fbSYaroslav Tykhiy int error; 1946a3814acfSSam Leffler 1947d148c2a2SMatt Joras VLAN_SXLOCK_ASSERT(); 1948a3814acfSSam Leffler 19491cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 19501cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 19511cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 19521cf236fbSYaroslav Tykhiy 19531cf236fbSYaroslav Tykhiy /* 19541cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 19551cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 19561cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 19571cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 19581cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 19591cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 19601cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 19611cf236fbSYaroslav Tykhiy */ 19621cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 196375ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 19641cf236fbSYaroslav Tykhiy if (error) 1965a3814acfSSam Leffler return (error); 19661cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 19671cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 19681cf236fbSYaroslav Tykhiy } 19691cf236fbSYaroslav Tykhiy return (0); 19701cf236fbSYaroslav Tykhiy } 19711cf236fbSYaroslav Tykhiy 19721cf236fbSYaroslav Tykhiy /* 19731cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 19741cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 19751cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 19761cf236fbSYaroslav Tykhiy */ 19771cf236fbSYaroslav Tykhiy static int 19781cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 19791cf236fbSYaroslav Tykhiy { 19801cf236fbSYaroslav Tykhiy int error, i; 19811cf236fbSYaroslav Tykhiy 19821cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 19831cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 19841cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 19851cf236fbSYaroslav Tykhiy if (error) 19861cf236fbSYaroslav Tykhiy return (error); 19871cf236fbSYaroslav Tykhiy } 19881cf236fbSYaroslav Tykhiy return (0); 1989a3814acfSSam Leffler } 1990a3814acfSSam Leffler 1991127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1992127d7b2dSAndre Oppermann static void 1993a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp) 1994127d7b2dSAndre Oppermann { 1995b2807792SGleb Smirnoff struct epoch_tracker et; 1996d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1997127d7b2dSAndre Oppermann struct ifvlan *ifv; 1998127d7b2dSAndre Oppermann 1999b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 2000d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 2001b2807792SGleb Smirnoff if (trunk == NULL) { 2002b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 2003d148c2a2SMatt Joras return; 2004b2807792SGleb Smirnoff } 2005d148c2a2SMatt Joras 2006d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 2007d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 2008aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 2009fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 201075ee267cSGleb Smirnoff trunk->parent->if_link_state); 2011127d7b2dSAndre Oppermann } 2012d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 2013b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 201475ee267cSGleb Smirnoff } 201575ee267cSGleb Smirnoff 201675ee267cSGleb Smirnoff static void 201775ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 201875ee267cSGleb Smirnoff { 2019d148c2a2SMatt Joras struct ifnet *p; 2020d148c2a2SMatt Joras struct ifnet *ifp; 20219fd573c3SHans Petter Selasky struct ifnet_hw_tsomax hw_tsomax; 2022d89baa5aSAlexander Motin int cap = 0, ena = 0, mena; 2023d89baa5aSAlexander Motin u_long hwa = 0; 202475ee267cSGleb Smirnoff 2025a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 2026b8a6e03fSGleb Smirnoff VLAN_SXLOCK_ASSERT(); 2027b8a6e03fSGleb Smirnoff 2028d148c2a2SMatt Joras p = PARENT(ifv); 2029d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 203075ee267cSGleb Smirnoff 2031d89baa5aSAlexander Motin /* Mask parent interface enabled capabilities disabled by user. */ 2032d89baa5aSAlexander Motin mena = p->if_capenable & ifv->ifv_capenable; 2033d89baa5aSAlexander Motin 203475ee267cSGleb Smirnoff /* 203575ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 203675ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 203775ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 203875ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 203975ee267cSGleb Smirnoff */ 204075ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 2041d89baa5aSAlexander Motin cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 204275ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 204375ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 2044d89baa5aSAlexander Motin ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 2045d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM) 2046d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP | 2047d89baa5aSAlexander Motin CSUM_UDP | CSUM_SCTP); 2048d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM_IPV6) 2049d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_TCP_IPV6 | 2050d89baa5aSAlexander Motin CSUM_UDP_IPV6 | CSUM_SCTP_IPV6); 205175ee267cSGleb Smirnoff } 2052d89baa5aSAlexander Motin 20539b76d9cbSPyun YongHyeon /* 20549b76d9cbSPyun YongHyeon * If the parent interface can do TSO on VLANs then 20559b76d9cbSPyun YongHyeon * propagate the hardware-assisted flag. TSO on VLANs 20569b76d9cbSPyun YongHyeon * does not necessarily require hardware VLAN tagging. 20579b76d9cbSPyun YongHyeon */ 20589fd573c3SHans Petter Selasky memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 20599fd573c3SHans Petter Selasky if_hw_tsomax_common(p, &hw_tsomax); 20609fd573c3SHans Petter Selasky if_hw_tsomax_update(ifp, &hw_tsomax); 20619b76d9cbSPyun YongHyeon if (p->if_capabilities & IFCAP_VLAN_HWTSO) 2062d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TSO; 20639b76d9cbSPyun YongHyeon if (p->if_capenable & IFCAP_VLAN_HWTSO) { 2064d89baa5aSAlexander Motin ena |= mena & IFCAP_TSO; 2065d89baa5aSAlexander Motin if (ena & IFCAP_TSO) 2066d89baa5aSAlexander Motin hwa |= p->if_hwassist & CSUM_TSO; 20679b76d9cbSPyun YongHyeon } 206809fe6320SNavdeep Parhar 206909fe6320SNavdeep Parhar /* 2070fb69ed39SKristof Provost * If the parent interface can do LRO and checksum offloading on 2071fb69ed39SKristof Provost * VLANs, then guess it may do LRO on VLANs. False positive here 2072fb69ed39SKristof Provost * cost nothing, while false negative may lead to some confusions. 207359150e91SAlexander Motin */ 207459150e91SAlexander Motin if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 207559150e91SAlexander Motin cap |= p->if_capabilities & IFCAP_LRO; 207659150e91SAlexander Motin if (p->if_capenable & IFCAP_VLAN_HWCSUM) 2077*b1a39c31SKevin Bowling ena |= mena & IFCAP_LRO; 207859150e91SAlexander Motin 207959150e91SAlexander Motin /* 208009fe6320SNavdeep Parhar * If the parent interface can offload TCP connections over VLANs then 208109fe6320SNavdeep Parhar * propagate its TOE capability to the VLAN interface. 208209fe6320SNavdeep Parhar * 208309fe6320SNavdeep Parhar * All TOE drivers in the tree today can deal with VLANs. If this 208409fe6320SNavdeep Parhar * changes then IFCAP_VLAN_TOE should be promoted to a full capability 208509fe6320SNavdeep Parhar * with its own bit. 208609fe6320SNavdeep Parhar */ 208709fe6320SNavdeep Parhar #define IFCAP_VLAN_TOE IFCAP_TOE 208809fe6320SNavdeep Parhar if (p->if_capabilities & IFCAP_VLAN_TOE) 2089d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TOE; 209009fe6320SNavdeep Parhar if (p->if_capenable & IFCAP_VLAN_TOE) { 2091c255d1a4SJustin Hibbits SETTOEDEV(ifp, TOEDEV(p)); 2092d89baa5aSAlexander Motin ena |= mena & IFCAP_TOE; 209309fe6320SNavdeep Parhar } 2094f3e7afe2SHans Petter Selasky 2095d89baa5aSAlexander Motin /* 2096d89baa5aSAlexander Motin * If the parent interface supports dynamic link state, so does the 2097d89baa5aSAlexander Motin * VLAN interface. 2098d89baa5aSAlexander Motin */ 2099d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_LINKSTATE); 2100d89baa5aSAlexander Motin ena |= (mena & IFCAP_LINKSTATE); 2101d89baa5aSAlexander Motin 2102f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 2103f3e7afe2SHans Petter Selasky /* 2104f3e7afe2SHans Petter Selasky * If the parent interface supports ratelimiting, so does the 2105f3e7afe2SHans Petter Selasky * VLAN interface. 2106f3e7afe2SHans Petter Selasky */ 2107d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_TXRTLMT); 2108d89baa5aSAlexander Motin ena |= (mena & IFCAP_TXRTLMT); 2109f3e7afe2SHans Petter Selasky #endif 2110d89baa5aSAlexander Motin 211166d0c056SJohn Baldwin /* 211266d0c056SJohn Baldwin * If the parent interface supports unmapped mbufs, so does 211366d0c056SJohn Baldwin * the VLAN interface. Note that this should be fine even for 211466d0c056SJohn Baldwin * interfaces that don't support hardware tagging as headers 211566d0c056SJohn Baldwin * are prepended in normal mbufs to unmapped mbufs holding 211666d0c056SJohn Baldwin * payload data. 211766d0c056SJohn Baldwin */ 21183f43ada9SGleb Smirnoff cap |= (p->if_capabilities & IFCAP_MEXTPG); 21193f43ada9SGleb Smirnoff ena |= (mena & IFCAP_MEXTPG); 212066d0c056SJohn Baldwin 2121b2e60773SJohn Baldwin /* 2122b2e60773SJohn Baldwin * If the parent interface can offload encryption and segmentation 2123b2e60773SJohn Baldwin * of TLS records over TCP, propagate it's capability to the VLAN 2124b2e60773SJohn Baldwin * interface. 2125b2e60773SJohn Baldwin * 2126b2e60773SJohn Baldwin * All TLS drivers in the tree today can deal with VLANs. If 2127b2e60773SJohn Baldwin * this ever changes, then a new IFCAP_VLAN_TXTLS can be 2128b2e60773SJohn Baldwin * defined. 2129b2e60773SJohn Baldwin */ 2130521eac97SJohn Baldwin if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 2131521eac97SJohn Baldwin cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 2132521eac97SJohn Baldwin if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 2133521eac97SJohn Baldwin ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 2134b2e60773SJohn Baldwin 2135d89baa5aSAlexander Motin ifp->if_capabilities = cap; 2136d89baa5aSAlexander Motin ifp->if_capenable = ena; 2137d89baa5aSAlexander Motin ifp->if_hwassist = hwa; 213875ee267cSGleb Smirnoff } 213975ee267cSGleb Smirnoff 214075ee267cSGleb Smirnoff static void 214175ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 214275ee267cSGleb Smirnoff { 2143b2807792SGleb Smirnoff struct epoch_tracker et; 2144d148c2a2SMatt Joras struct ifvlantrunk *trunk; 214575ee267cSGleb Smirnoff struct ifvlan *ifv; 214675ee267cSGleb Smirnoff 2147d148c2a2SMatt Joras VLAN_SLOCK(); 2148d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 2149d148c2a2SMatt Joras if (trunk == NULL) { 2150d148c2a2SMatt Joras VLAN_SUNLOCK(); 2151d148c2a2SMatt Joras return; 2152d148c2a2SMatt Joras } 2153b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 2154b8a6e03fSGleb Smirnoff VLAN_FOREACH(ifv, trunk) 215575ee267cSGleb Smirnoff vlan_capabilities(ifv); 2156b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 2157d148c2a2SMatt Joras VLAN_SUNLOCK(); 2158127d7b2dSAndre Oppermann } 2159127d7b2dSAndre Oppermann 2160a3814acfSSam Leffler static int 2161cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 21622cc2df49SGarrett Wollman { 21632cc2df49SGarrett Wollman struct ifnet *p; 21642cc2df49SGarrett Wollman struct ifreq *ifr; 21652884a936SJohn Baldwin #ifdef INET 2166e4cd31ddSJeff Roberson struct ifaddr *ifa; 21672884a936SJohn Baldwin #endif 21682cc2df49SGarrett Wollman struct ifvlan *ifv; 21692d222cb7SAlexander Motin struct ifvlantrunk *trunk; 21702cc2df49SGarrett Wollman struct vlanreq vlr; 217184becee1SAlexander Motin int error = 0, oldmtu; 21722cc2df49SGarrett Wollman 21732cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 21742884a936SJohn Baldwin #ifdef INET 2175e4cd31ddSJeff Roberson ifa = (struct ifaddr *) data; 21762884a936SJohn Baldwin #endif 21772cc2df49SGarrett Wollman ifv = ifp->if_softc; 21782cc2df49SGarrett Wollman 21792cc2df49SGarrett Wollman switch (cmd) { 2180e4cd31ddSJeff Roberson case SIOCSIFADDR: 2181e4cd31ddSJeff Roberson ifp->if_flags |= IFF_UP; 2182e4cd31ddSJeff Roberson #ifdef INET 2183e4cd31ddSJeff Roberson if (ifa->ifa_addr->sa_family == AF_INET) 2184e4cd31ddSJeff Roberson arp_ifinit(ifp, ifa); 2185e4cd31ddSJeff Roberson #endif 2186e4cd31ddSJeff Roberson break; 2187e4cd31ddSJeff Roberson case SIOCGIFADDR: 218838d958a6SBrooks Davis bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 218938d958a6SBrooks Davis ifp->if_addrlen); 2190e4cd31ddSJeff Roberson break; 2191b3cca108SBill Fenner case SIOCGIFMEDIA: 2192d148c2a2SMatt Joras VLAN_SLOCK(); 219375ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 2194d8564efdSEd Maste p = PARENT(ifv); 21959bcf3ae4SAlexander Motin if_ref(p); 2196d8564efdSEd Maste error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 21979bcf3ae4SAlexander Motin if_rele(p); 2198b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 2199b3cca108SBill Fenner if (error == 0) { 2200b3cca108SBill Fenner struct ifmediareq *ifmr; 2201b3cca108SBill Fenner 2202b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 2203b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 2204b3cca108SBill Fenner ifmr->ifm_count = 1; 2205b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 2206b3cca108SBill Fenner ifmr->ifm_ulist, 2207b3cca108SBill Fenner sizeof(int)); 2208b3cca108SBill Fenner } 2209b3cca108SBill Fenner } 22104faedfe8SSam Leffler } else { 2211b3cca108SBill Fenner error = EINVAL; 22124faedfe8SSam Leffler } 2213d148c2a2SMatt Joras VLAN_SUNLOCK(); 2214b3cca108SBill Fenner break; 2215b3cca108SBill Fenner 2216b3cca108SBill Fenner case SIOCSIFMEDIA: 2217b3cca108SBill Fenner error = EINVAL; 2218b3cca108SBill Fenner break; 2219b3cca108SBill Fenner 22202cc2df49SGarrett Wollman case SIOCSIFMTU: 22212cc2df49SGarrett Wollman /* 22222cc2df49SGarrett Wollman * Set the interface MTU. 22232cc2df49SGarrett Wollman */ 2224d148c2a2SMatt Joras VLAN_SLOCK(); 2225d148c2a2SMatt Joras trunk = TRUNK(ifv); 2226d148c2a2SMatt Joras if (trunk != NULL) { 2227d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 2228a3814acfSSam Leffler if (ifr->ifr_mtu > 222975ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 2230a3814acfSSam Leffler ifr->ifr_mtu < 2231a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 22322cc2df49SGarrett Wollman error = EINVAL; 2233a3814acfSSam Leffler else 22342cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 2235d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 2236a3814acfSSam Leffler } else 2237a3814acfSSam Leffler error = EINVAL; 2238d148c2a2SMatt Joras VLAN_SUNLOCK(); 22392cc2df49SGarrett Wollman break; 22402cc2df49SGarrett Wollman 22412cc2df49SGarrett Wollman case SIOCSETVLAN: 2242ccf7ba97SMarko Zec #ifdef VIMAGE 224315f6780eSRobert Watson /* 224415f6780eSRobert Watson * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN 224515f6780eSRobert Watson * interface to be delegated to a jail without allowing the 224615f6780eSRobert Watson * jail to change what underlying interface/VID it is 224715f6780eSRobert Watson * associated with. We are not entirely convinced that this 22485a39f779SRobert Watson * is the right way to accomplish that policy goal. 224915f6780eSRobert Watson */ 2250ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 2251ccf7ba97SMarko Zec error = EPERM; 2252ccf7ba97SMarko Zec break; 2253ccf7ba97SMarko Zec } 2254ccf7ba97SMarko Zec #endif 2255541d96aaSBrooks Davis error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr)); 22562cc2df49SGarrett Wollman if (error) 22572cc2df49SGarrett Wollman break; 22582cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 2259f731f104SBill Paul vlan_unconfig(ifp); 22602cc2df49SGarrett Wollman break; 22612cc2df49SGarrett Wollman } 22629bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 22631bdc73d3SEd Maste if (p == NULL) { 22642cc2df49SGarrett Wollman error = ENOENT; 22652cc2df49SGarrett Wollman break; 22662cc2df49SGarrett Wollman } 2267afbb64f1SAlexander V. Chernikov #ifdef COMPAT_FREEBSD12 2268afbb64f1SAlexander V. Chernikov if (vlr.vlr_proto == 0) 2269afbb64f1SAlexander V. Chernikov vlr.vlr_proto = ETHERTYPE_VLAN; 2270afbb64f1SAlexander V. Chernikov #endif 227184becee1SAlexander Motin oldmtu = ifp->if_mtu; 2272c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto); 22739bcf3ae4SAlexander Motin if_rele(p); 227484becee1SAlexander Motin 227584becee1SAlexander Motin /* 227684becee1SAlexander Motin * VLAN MTU may change during addition of the vlandev. 227784becee1SAlexander Motin * If it did, do network layer specific procedure. 227884becee1SAlexander Motin */ 227966bdbcd5SAlexander V. Chernikov if (ifp->if_mtu != oldmtu) 228066bdbcd5SAlexander V. Chernikov if_notifymtu(ifp); 22812cc2df49SGarrett Wollman break; 22822cc2df49SGarrett Wollman 22832cc2df49SGarrett Wollman case SIOCGETVLAN: 2284ccf7ba97SMarko Zec #ifdef VIMAGE 2285ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 2286ccf7ba97SMarko Zec error = EPERM; 2287ccf7ba97SMarko Zec break; 2288ccf7ba97SMarko Zec } 2289ccf7ba97SMarko Zec #endif 229015a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 2291d148c2a2SMatt Joras VLAN_SLOCK(); 229275ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 229375ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 22949bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 22957983103aSRobert Watson vlr.vlr_tag = ifv->ifv_vid; 2296c7cffd65SAlexander V. Chernikov vlr.vlr_proto = ifv->ifv_proto; 22972cc2df49SGarrett Wollman } 2298d148c2a2SMatt Joras VLAN_SUNLOCK(); 2299541d96aaSBrooks Davis error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr)); 23002cc2df49SGarrett Wollman break; 23012cc2df49SGarrett Wollman 23022cc2df49SGarrett Wollman case SIOCSIFFLAGS: 23032cc2df49SGarrett Wollman /* 23041cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 23051cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 23062cc2df49SGarrett Wollman */ 230792c23f6dSKristof Provost VLAN_SLOCK(); 230875ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 23091cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 231092c23f6dSKristof Provost VLAN_SUNLOCK(); 23112cc2df49SGarrett Wollman break; 2312a3814acfSSam Leffler 2313f731f104SBill Paul case SIOCADDMULTI: 2314f731f104SBill Paul case SIOCDELMULTI: 231575ee267cSGleb Smirnoff /* 231675ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 231775ee267cSGleb Smirnoff * when we do. 2318d148c2a2SMatt Joras * 2319d148c2a2SMatt Joras * XXX We need the rmlock here to avoid sleeping while 2320d148c2a2SMatt Joras * holding in6_multi_mtx. 232175ee267cSGleb Smirnoff */ 2322b08d611dSMatt Macy VLAN_XLOCK(); 23232d222cb7SAlexander Motin trunk = TRUNK(ifv); 2324b08d611dSMatt Macy if (trunk != NULL) 2325f731f104SBill Paul error = vlan_setmulti(ifp); 2326b08d611dSMatt Macy VLAN_XUNLOCK(); 232775ee267cSGleb Smirnoff 2328b08d611dSMatt Macy break; 23292ccbbd06SMarcelo Araujo case SIOCGVLANPCP: 23302ccbbd06SMarcelo Araujo #ifdef VIMAGE 23312ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 23322ccbbd06SMarcelo Araujo error = EPERM; 23332ccbbd06SMarcelo Araujo break; 23342ccbbd06SMarcelo Araujo } 23352ccbbd06SMarcelo Araujo #endif 23362ccbbd06SMarcelo Araujo ifr->ifr_vlan_pcp = ifv->ifv_pcp; 23372ccbbd06SMarcelo Araujo break; 23382ccbbd06SMarcelo Araujo 23392ccbbd06SMarcelo Araujo case SIOCSVLANPCP: 23402ccbbd06SMarcelo Araujo #ifdef VIMAGE 23412ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 23422ccbbd06SMarcelo Araujo error = EPERM; 23432ccbbd06SMarcelo Araujo break; 23442ccbbd06SMarcelo Araujo } 23452ccbbd06SMarcelo Araujo #endif 23462ccbbd06SMarcelo Araujo error = priv_check(curthread, PRIV_NET_SETVLANPCP); 23472ccbbd06SMarcelo Araujo if (error) 23482ccbbd06SMarcelo Araujo break; 23499ef8cd0bSKristof Provost if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) { 23502ccbbd06SMarcelo Araujo error = EINVAL; 23512ccbbd06SMarcelo Araujo break; 23522ccbbd06SMarcelo Araujo } 23532ccbbd06SMarcelo Araujo ifv->ifv_pcp = ifr->ifr_vlan_pcp; 235432a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 23554a381a9eSHans Petter Selasky /* broadcast event about PCP change */ 23564a381a9eSHans Petter Selasky EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 23572ccbbd06SMarcelo Araujo break; 23582ccbbd06SMarcelo Araujo 2359d89baa5aSAlexander Motin case SIOCSIFCAP: 2360d148c2a2SMatt Joras VLAN_SLOCK(); 2361d89baa5aSAlexander Motin ifv->ifv_capenable = ifr->ifr_reqcap; 2362d89baa5aSAlexander Motin trunk = TRUNK(ifv); 23636dcec895SGleb Smirnoff if (trunk != NULL) { 23646dcec895SGleb Smirnoff struct epoch_tracker et; 23656dcec895SGleb Smirnoff 23666dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 2367d89baa5aSAlexander Motin vlan_capabilities(ifv); 23686dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 23696dcec895SGleb Smirnoff } 2370d148c2a2SMatt Joras VLAN_SUNLOCK(); 2371d89baa5aSAlexander Motin break; 2372d89baa5aSAlexander Motin 23732cc2df49SGarrett Wollman default: 2374e4cd31ddSJeff Roberson error = EINVAL; 2375e4cd31ddSJeff Roberson break; 23762cc2df49SGarrett Wollman } 237715a66c21SBruce M Simpson 237815a66c21SBruce M Simpson return (error); 23792cc2df49SGarrett Wollman } 2380f3e7afe2SHans Petter Selasky 2381b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 2382f3e7afe2SHans Petter Selasky static int 2383f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp, 2384f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *params, 2385f3e7afe2SHans Petter Selasky struct m_snd_tag **ppmt) 2386f3e7afe2SHans Petter Selasky { 2387fb3bc596SJohn Baldwin struct epoch_tracker et; 2388c782ea8bSJohn Baldwin const struct if_snd_tag_sw *sw; 2389fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2390fb3bc596SJohn Baldwin struct ifvlan *ifv; 2391fb3bc596SJohn Baldwin struct ifnet *parent; 2392892eded5SHans Petter Selasky struct m_snd_tag *mst; 2393fb3bc596SJohn Baldwin int error; 2394f3e7afe2SHans Petter Selasky 2395892eded5SHans Petter Selasky NET_EPOCH_ENTER(et); 2396892eded5SHans Petter Selasky ifv = ifp->if_softc; 2397892eded5SHans Petter Selasky 2398c782ea8bSJohn Baldwin switch (params->hdr.type) { 2399c782ea8bSJohn Baldwin #ifdef RATELIMIT 2400c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_UNLIMITED: 2401c782ea8bSJohn Baldwin sw = &vlan_snd_tag_ul_sw; 2402c782ea8bSJohn Baldwin break; 2403c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_RATE_LIMIT: 2404c782ea8bSJohn Baldwin sw = &vlan_snd_tag_rl_sw; 2405c782ea8bSJohn Baldwin break; 2406c782ea8bSJohn Baldwin #endif 2407c782ea8bSJohn Baldwin #ifdef KERN_TLS 2408c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_TLS: 2409c782ea8bSJohn Baldwin sw = &vlan_snd_tag_tls_sw; 2410c782ea8bSJohn Baldwin break; 2411892eded5SHans Petter Selasky case IF_SND_TAG_TYPE_TLS_RX: 2412892eded5SHans Petter Selasky sw = NULL; 2413892eded5SHans Petter Selasky if (params->tls_rx.vlan_id != 0) 2414892eded5SHans Petter Selasky goto failure; 2415892eded5SHans Petter Selasky params->tls_rx.vlan_id = ifv->ifv_vid; 2416892eded5SHans Petter Selasky break; 2417c782ea8bSJohn Baldwin #ifdef RATELIMIT 2418c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_TLS_RATE_LIMIT: 2419c782ea8bSJohn Baldwin sw = &vlan_snd_tag_tls_rl_sw; 2420c782ea8bSJohn Baldwin break; 2421c782ea8bSJohn Baldwin #endif 2422c782ea8bSJohn Baldwin #endif 2423c782ea8bSJohn Baldwin default: 2424892eded5SHans Petter Selasky goto failure; 2425c782ea8bSJohn Baldwin } 2426c782ea8bSJohn Baldwin 2427fb3bc596SJohn Baldwin if (ifv->ifv_trunk != NULL) 2428fb3bc596SJohn Baldwin parent = PARENT(ifv); 2429fb3bc596SJohn Baldwin else 2430fb3bc596SJohn Baldwin parent = NULL; 2431892eded5SHans Petter Selasky if (parent == NULL) 2432892eded5SHans Petter Selasky goto failure; 2433fb3bc596SJohn Baldwin if_ref(parent); 2434fb3bc596SJohn Baldwin NET_EPOCH_EXIT(et); 2435fb3bc596SJohn Baldwin 2436892eded5SHans Petter Selasky if (sw != NULL) { 2437fb3bc596SJohn Baldwin vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT); 2438fb3bc596SJohn Baldwin if (vst == NULL) { 2439fb3bc596SJohn Baldwin if_rele(parent); 2440fb3bc596SJohn Baldwin return (ENOMEM); 2441fb3bc596SJohn Baldwin } 2442892eded5SHans Petter Selasky } else 2443892eded5SHans Petter Selasky vst = NULL; 2444fb3bc596SJohn Baldwin 2445892eded5SHans Petter Selasky error = m_snd_tag_alloc(parent, params, &mst); 2446fb3bc596SJohn Baldwin if_rele(parent); 2447fb3bc596SJohn Baldwin if (error) { 2448fb3bc596SJohn Baldwin free(vst, M_VLAN); 2449fb3bc596SJohn Baldwin return (error); 2450fb3bc596SJohn Baldwin } 2451fb3bc596SJohn Baldwin 2452892eded5SHans Petter Selasky if (sw != NULL) { 2453c782ea8bSJohn Baldwin m_snd_tag_init(&vst->com, ifp, sw); 2454892eded5SHans Petter Selasky vst->tag = mst; 2455fb3bc596SJohn Baldwin 2456fb3bc596SJohn Baldwin *ppmt = &vst->com; 2457892eded5SHans Petter Selasky } else 2458892eded5SHans Petter Selasky *ppmt = mst; 2459892eded5SHans Petter Selasky 2460fb3bc596SJohn Baldwin return (0); 2461892eded5SHans Petter Selasky failure: 2462892eded5SHans Petter Selasky NET_EPOCH_EXIT(et); 2463892eded5SHans Petter Selasky return (EOPNOTSUPP); 2464fb3bc596SJohn Baldwin } 2465fb3bc596SJohn Baldwin 24661a714ff2SRandall Stewart static struct m_snd_tag * 24671a714ff2SRandall Stewart vlan_next_snd_tag(struct m_snd_tag *mst) 24681a714ff2SRandall Stewart { 24691a714ff2SRandall Stewart struct vlan_snd_tag *vst; 24701a714ff2SRandall Stewart 24711a714ff2SRandall Stewart vst = mst_to_vst(mst); 24721a714ff2SRandall Stewart return (vst->tag); 24731a714ff2SRandall Stewart } 24741a714ff2SRandall Stewart 2475fb3bc596SJohn Baldwin static int 2476fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst, 2477fb3bc596SJohn Baldwin union if_snd_tag_modify_params *params) 2478fb3bc596SJohn Baldwin { 2479fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2480fb3bc596SJohn Baldwin 2481fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2482c782ea8bSJohn Baldwin return (vst->tag->sw->snd_tag_modify(vst->tag, params)); 2483fb3bc596SJohn Baldwin } 2484fb3bc596SJohn Baldwin 2485fb3bc596SJohn Baldwin static int 2486fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst, 2487fb3bc596SJohn Baldwin union if_snd_tag_query_params *params) 2488fb3bc596SJohn Baldwin { 2489fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2490fb3bc596SJohn Baldwin 2491fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2492c782ea8bSJohn Baldwin return (vst->tag->sw->snd_tag_query(vst->tag, params)); 2493f3e7afe2SHans Petter Selasky } 2494fa91f845SRandall Stewart 2495fa91f845SRandall Stewart static void 2496fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst) 2497fa91f845SRandall Stewart { 2498fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2499fb3bc596SJohn Baldwin 2500fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2501fb3bc596SJohn Baldwin m_snd_tag_rele(vst->tag); 2502fb3bc596SJohn Baldwin free(vst, M_VLAN); 2503fa91f845SRandall Stewart } 25041a714ff2SRandall Stewart 25051a714ff2SRandall Stewart static void 25061a714ff2SRandall Stewart vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q) 25071a714ff2SRandall Stewart { 25081a714ff2SRandall Stewart /* 25091a714ff2SRandall Stewart * For vlan, we have an indirect 25101a714ff2SRandall Stewart * interface. The caller needs to 25111a714ff2SRandall Stewart * get a ratelimit tag on the actual 25121a714ff2SRandall Stewart * interface the flow will go on. 25131a714ff2SRandall Stewart */ 25141a714ff2SRandall Stewart q->rate_table = NULL; 25151a714ff2SRandall Stewart q->flags = RT_IS_INDIRECT; 25161a714ff2SRandall Stewart q->max_flows = 0; 25171a714ff2SRandall Stewart q->number_of_rates = 0; 25181a714ff2SRandall Stewart } 25191a714ff2SRandall Stewart 2520f3e7afe2SHans Petter Selasky #endif 2521