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 452c5b403eSOleg Bulyzhin #include "opt_inet.h" 4684becee1SAlexander Motin #include "opt_inet6.h" 4784abf7e2SKonstantin Belousov #include "opt_ipsec.h" 48b2e60773SJohn Baldwin #include "opt_kern_tls.h" 4975ee267cSGleb Smirnoff #include "opt_vlan.h" 50f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 512cc2df49SGarrett Wollman 522cc2df49SGarrett Wollman #include <sys/param.h> 53c3322cb9SGleb Smirnoff #include <sys/eventhandler.h> 542cc2df49SGarrett Wollman #include <sys/kernel.h> 5575ee267cSGleb Smirnoff #include <sys/lock.h> 56f731f104SBill Paul #include <sys/malloc.h> 572cc2df49SGarrett Wollman #include <sys/mbuf.h> 582b120974SPeter Wemm #include <sys/module.h> 59772b000fSAlexander V. Chernikov #include <sys/rmlock.h> 602ccbbd06SMarcelo Araujo #include <sys/priv.h> 61f731f104SBill Paul #include <sys/queue.h> 622cc2df49SGarrett Wollman #include <sys/socket.h> 632cc2df49SGarrett Wollman #include <sys/sockio.h> 642cc2df49SGarrett Wollman #include <sys/sysctl.h> 652cc2df49SGarrett Wollman #include <sys/systm.h> 66e4cd31ddSJeff Roberson #include <sys/sx.h> 67d148c2a2SMatt Joras #include <sys/taskqueue.h> 682cc2df49SGarrett Wollman 692cc2df49SGarrett Wollman #include <net/bpf.h> 702cc2df49SGarrett Wollman #include <net/ethernet.h> 712cc2df49SGarrett Wollman #include <net/if.h> 7276039bc8SGleb Smirnoff #include <net/if_var.h> 732c2b37adSJustin Hibbits #include <net/if_private.h> 74f889d2efSBrooks Davis #include <net/if_clone.h> 752cc2df49SGarrett Wollman #include <net/if_dl.h> 762cc2df49SGarrett Wollman #include <net/if_types.h> 772cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 7884becee1SAlexander Motin #include <net/route.h> 794b79449eSBjoern A. Zeeb #include <net/vnet.h> 802cc2df49SGarrett Wollman 812c5b403eSOleg Bulyzhin #ifdef INET 822c5b403eSOleg Bulyzhin #include <netinet/in.h> 832c5b403eSOleg Bulyzhin #include <netinet/if_ether.h> 842c5b403eSOleg Bulyzhin #endif 852c5b403eSOleg Bulyzhin 86089104e0SAlexander V. Chernikov #include <netlink/netlink.h> 87089104e0SAlexander V. Chernikov #include <netlink/netlink_ctl.h> 88089104e0SAlexander V. Chernikov #include <netlink/netlink_route.h> 89089104e0SAlexander V. Chernikov #include <netlink/route/route_var.h> 90089104e0SAlexander V. Chernikov 9175ee267cSGleb Smirnoff #define VLAN_DEF_HWIDTH 4 9264a17d2eSYaroslav Tykhiy #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 9375ee267cSGleb Smirnoff 942dc879b3SYaroslav Tykhiy #define UP_AND_RUNNING(ifp) \ 952dc879b3SYaroslav Tykhiy ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 962dc879b3SYaroslav Tykhiy 97b08d611dSMatt Macy CK_SLIST_HEAD(ifvlanhead, ifvlan); 9875ee267cSGleb Smirnoff 9975ee267cSGleb Smirnoff struct ifvlantrunk { 10075ee267cSGleb Smirnoff struct ifnet *parent; /* parent interface of this trunk */ 101b08d611dSMatt Macy struct mtx lock; 10275ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 1035cb8c31aSYaroslav Tykhiy #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 1045cb8c31aSYaroslav Tykhiy struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 10575ee267cSGleb Smirnoff #else 10675ee267cSGleb Smirnoff struct ifvlanhead *hash; /* dynamic hash-list table */ 10775ee267cSGleb Smirnoff uint16_t hmask; 10875ee267cSGleb Smirnoff uint16_t hwidth; 10975ee267cSGleb Smirnoff #endif 11075ee267cSGleb Smirnoff int refcnt; 11175ee267cSGleb Smirnoff }; 1129d4fe4b2SBrooks Davis 113b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 114fb3bc596SJohn Baldwin struct vlan_snd_tag { 115fb3bc596SJohn Baldwin struct m_snd_tag com; 116fb3bc596SJohn Baldwin struct m_snd_tag *tag; 117fb3bc596SJohn Baldwin }; 118fb3bc596SJohn Baldwin 119fb3bc596SJohn Baldwin static inline struct vlan_snd_tag * 120fb3bc596SJohn Baldwin mst_to_vst(struct m_snd_tag *mst) 121fb3bc596SJohn Baldwin { 122fb3bc596SJohn Baldwin 123fb3bc596SJohn Baldwin return (__containerof(mst, struct vlan_snd_tag, com)); 124fb3bc596SJohn Baldwin } 125fb3bc596SJohn Baldwin #endif 126fb3bc596SJohn Baldwin 127d148c2a2SMatt Joras /* 128d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk with 129d148c2a2SMatt Joras * the assumption that none will be added/removed during iteration. 130d148c2a2SMatt Joras */ 131d148c2a2SMatt Joras #ifdef VLAN_ARRAY 132d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 133d148c2a2SMatt Joras size_t _i; \ 134d148c2a2SMatt Joras for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \ 135d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i]) != NULL) 136d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 137d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 138d148c2a2SMatt Joras struct ifvlan *_next; \ 139d148c2a2SMatt Joras size_t _i; \ 140d148c2a2SMatt Joras for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \ 141b08d611dSMatt Macy CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next) 142d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 143d148c2a2SMatt Joras 144d148c2a2SMatt Joras /* 145d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk while 146d148c2a2SMatt Joras * also modifying the number of vlans on the trunk. The iteration continues 147d148c2a2SMatt Joras * until some condition is met or there are no more vlans on the trunk. 148d148c2a2SMatt Joras */ 149d148c2a2SMatt Joras #ifdef VLAN_ARRAY 150d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */ 151d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 152d148c2a2SMatt Joras size_t _i; \ 153d148c2a2SMatt Joras for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \ 154d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i])) 155d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 156d148c2a2SMatt Joras /* 157d148c2a2SMatt Joras * The hash table case is more complicated. We allow for the hash table to be 158d148c2a2SMatt Joras * modified (i.e. vlans removed) while we are iterating over it. To allow for 159d148c2a2SMatt Joras * this we must restart the iteration every time we "touch" something during 160d148c2a2SMatt Joras * the iteration, since removal will resize the hash table and invalidate our 161d148c2a2SMatt Joras * current position. If acting on the touched element causes the trunk to be 162d148c2a2SMatt Joras * emptied, then iteration also stops. 163d148c2a2SMatt Joras */ 164d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 165d148c2a2SMatt Joras size_t _i; \ 166d148c2a2SMatt Joras bool _touch = false; \ 167d148c2a2SMatt Joras for (_i = 0; \ 168d148c2a2SMatt Joras !(_cond) && _i < (1 << (_trunk)->hwidth); \ 169d148c2a2SMatt Joras _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \ 170b08d611dSMatt Macy if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \ 171d148c2a2SMatt Joras (_touch = true)) 172d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 173d148c2a2SMatt Joras 174a3814acfSSam Leffler struct vlan_mc_entry { 175e4cd31ddSJeff Roberson struct sockaddr_dl mc_addr; 176b08d611dSMatt Macy CK_SLIST_ENTRY(vlan_mc_entry) mc_entries; 177c32a9d66SHans Petter Selasky struct epoch_context mc_epoch_ctx; 178a3814acfSSam Leffler }; 179a3814acfSSam Leffler 180a3814acfSSam Leffler struct ifvlan { 18175ee267cSGleb Smirnoff struct ifvlantrunk *ifv_trunk; 182fc74a9f9SBrooks Davis struct ifnet *ifv_ifp; 18375ee267cSGleb Smirnoff #define TRUNK(ifv) ((ifv)->ifv_trunk) 184c7cffd65SAlexander V. Chernikov #define PARENT(ifv) (TRUNK(ifv)->parent) 1856667db31SAlexander V. Chernikov void *ifv_cookie; 1861cf236fbSYaroslav Tykhiy int ifv_pflags; /* special flags we have set on parent */ 187d89baa5aSAlexander Motin int ifv_capenable; 18884abf7e2SKonstantin Belousov int ifv_capenable2; 18972755d28SMark Johnston int ifv_encaplen; /* encapsulation length */ 19072755d28SMark Johnston int ifv_mtufudge; /* MTU fudged by this much */ 19172755d28SMark Johnston int ifv_mintu; /* min transmission unit */ 192c7cffd65SAlexander V. Chernikov struct ether_8021q_tag ifv_qtag; 193c7cffd65SAlexander V. Chernikov #define ifv_proto ifv_qtag.proto 194c7cffd65SAlexander V. Chernikov #define ifv_vid ifv_qtag.vid 195c7cffd65SAlexander V. Chernikov #define ifv_pcp ifv_qtag.pcp 196d148c2a2SMatt Joras struct task lladdr_task; 197b08d611dSMatt Macy CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 198c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY 199b08d611dSMatt Macy CK_SLIST_ENTRY(ifvlan) ifv_list; 200c0cb022bSYaroslav Tykhiy #endif 201a3814acfSSam Leffler }; 202a3814acfSSam Leffler 20375ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */ 2041cf236fbSYaroslav Tykhiy static struct { 2051cf236fbSYaroslav Tykhiy int flag; 2061cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 2071cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 2081cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 2091cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 2101cf236fbSYaroslav Tykhiy {0, NULL} 2111cf236fbSYaroslav Tykhiy }; 212a3814acfSSam Leffler 21378bc3d5eSKristof Provost VNET_DECLARE(int, vlan_mtag_pcp); 21478bc3d5eSKristof Provost #define V_vlan_mtag_pcp VNET(vlan_mtag_pcp) 2152ccbbd06SMarcelo Araujo 21642a58907SGleb Smirnoff static const char vlanname[] = "vlan"; 21742a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface"); 2182cc2df49SGarrett Wollman 2195cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag; 220ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag; 221f2ab9160SAndrey V. Elsukov static eventhandler_tag ifevent_tag; 2225cb8c31aSYaroslav Tykhiy 2234faedfe8SSam Leffler /* 224b08d611dSMatt Macy * if_vlan uses two module-level synchronizations primitives to allow concurrent 225b08d611dSMatt Macy * modification of vlan interfaces and (mostly) allow for vlans to be destroyed 226b08d611dSMatt Macy * while they are being used for tx/rx. To accomplish this in a way that has 227b08d611dSMatt Macy * acceptable performance and cooperation with other parts of the network stack 228b08d611dSMatt Macy * there is a non-sleepable epoch(9) and an sx(9). 22975ee267cSGleb Smirnoff * 230b08d611dSMatt Macy * The performance-sensitive paths that warrant using the epoch(9) are 231d148c2a2SMatt Joras * vlan_transmit and vlan_input. Both have to check for the vlan interface's 232d148c2a2SMatt Joras * existence using if_vlantrunk, and being in the network tx/rx paths the use 233b08d611dSMatt Macy * of an epoch(9) gives a measureable improvement in performance. 23475ee267cSGleb Smirnoff * 235d148c2a2SMatt Joras * The reason for having an sx(9) is mostly because there are still areas that 236d148c2a2SMatt Joras * must be sleepable and also have safe concurrent access to a vlan interface. 237d148c2a2SMatt Joras * Since the sx(9) exists, it is used by default in most paths unless sleeping 238d148c2a2SMatt Joras * is not permitted, or if it is not clear whether sleeping is permitted. 239d148c2a2SMatt Joras * 2404faedfe8SSam Leffler */ 241d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx 242d148c2a2SMatt Joras 243d148c2a2SMatt Joras static struct sx _VLAN_SX_ID; 244d148c2a2SMatt Joras 245d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \ 246c7cffd65SAlexander V. Chernikov sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE) 247d148c2a2SMatt Joras 248d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \ 249d148c2a2SMatt Joras sx_destroy(&_VLAN_SX_ID) 250d148c2a2SMatt Joras 251d148c2a2SMatt Joras #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID) 252d148c2a2SMatt Joras #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID) 253d148c2a2SMatt Joras #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID) 254d148c2a2SMatt Joras #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID) 255d148c2a2SMatt Joras #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED) 256d148c2a2SMatt Joras #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED) 257d148c2a2SMatt Joras #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED) 258d148c2a2SMatt Joras 259d148c2a2SMatt Joras /* 260b08d611dSMatt Macy * We also have a per-trunk mutex that should be acquired when changing 261b08d611dSMatt Macy * its state. 262d148c2a2SMatt Joras */ 263b08d611dSMatt Macy #define TRUNK_LOCK_INIT(trunk) mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF) 264b08d611dSMatt Macy #define TRUNK_LOCK_DESTROY(trunk) mtx_destroy(&(trunk)->lock) 265b08d611dSMatt Macy #define TRUNK_WLOCK(trunk) mtx_lock(&(trunk)->lock) 266b08d611dSMatt Macy #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock) 267b08d611dSMatt Macy #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED); 26875ee267cSGleb Smirnoff 269d148c2a2SMatt Joras /* 270d148c2a2SMatt Joras * The VLAN_ARRAY substitutes the dynamic hash with a static array 271d148c2a2SMatt Joras * with 4096 entries. In theory this can give a boost in processing, 272d148c2a2SMatt Joras * however in practice it does not. Probably this is because the array 273d148c2a2SMatt Joras * is too big to fit into CPU cache. 274d148c2a2SMatt Joras */ 27575ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 27675ee267cSGleb Smirnoff static void vlan_inithash(struct ifvlantrunk *trunk); 27775ee267cSGleb Smirnoff static void vlan_freehash(struct ifvlantrunk *trunk); 27875ee267cSGleb Smirnoff static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 27975ee267cSGleb Smirnoff static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 28075ee267cSGleb Smirnoff static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 28175ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 2827983103aSRobert Watson uint16_t vid); 28375ee267cSGleb Smirnoff #endif 28475ee267cSGleb Smirnoff static void trunk_destroy(struct ifvlantrunk *trunk); 2854faedfe8SSam Leffler 286114c608cSYaroslav Tykhiy static void vlan_init(void *foo); 287a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 288cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 289b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 290f3e7afe2SHans Petter Selasky static int vlan_snd_tag_alloc(struct ifnet *, 291f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *, struct m_snd_tag **); 292fb3bc596SJohn Baldwin static int vlan_snd_tag_modify(struct m_snd_tag *, 293fb3bc596SJohn Baldwin union if_snd_tag_modify_params *); 294fb3bc596SJohn Baldwin static int vlan_snd_tag_query(struct m_snd_tag *, 295fb3bc596SJohn Baldwin union if_snd_tag_query_params *); 296fa91f845SRandall Stewart static void vlan_snd_tag_free(struct m_snd_tag *); 2971a714ff2SRandall Stewart static struct m_snd_tag *vlan_next_snd_tag(struct m_snd_tag *); 2981a714ff2SRandall Stewart static void vlan_ratelimit_query(struct ifnet *, 2991a714ff2SRandall Stewart struct if_ratelimit_query_results *); 300f3e7afe2SHans Petter Selasky #endif 301d9b1d615SJohn Baldwin static void vlan_qflush(struct ifnet *ifp); 3021cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 3031cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 3041cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 305f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 306d9b1d615SJohn Baldwin static int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 3072e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 3082e5ff01dSLuiz Otavio O Souza static void vlan_altq_start(struct ifnet *ifp); 3092e5ff01dSLuiz Otavio O Souza static int vlan_altq_transmit(struct ifnet *ifp, struct mbuf *m); 3102e5ff01dSLuiz Otavio O Souza #endif 31116cf6bdbSMatt Joras static int vlan_output(struct ifnet *ifp, struct mbuf *m, 31216cf6bdbSMatt Joras const struct sockaddr *dst, struct route *ro); 3136f359e28SJohn Baldwin static void vlan_unconfig(struct ifnet *ifp); 31428cc4d37SJohn Baldwin static void vlan_unconfig_locked(struct ifnet *ifp, int departing); 315c7cffd65SAlexander V. Chernikov static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag, 316c7cffd65SAlexander V. Chernikov uint16_t proto); 317a6fffd6cSBrooks Davis static void vlan_link_state(struct ifnet *ifp); 31875ee267cSGleb Smirnoff static void vlan_capabilities(struct ifvlan *ifv); 31975ee267cSGleb Smirnoff static void vlan_trunk_capabilities(struct ifnet *ifp); 320f731f104SBill Paul 321f941c31aSGleb Smirnoff static struct ifnet *vlan_clone_match_ethervid(const char *, int *); 322f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 32391ebcbe0SAlexander V. Chernikov static int vlan_clone_create(struct if_clone *, char *, size_t, 32491ebcbe0SAlexander V. Chernikov struct ifc_data *, struct ifnet **); 32591ebcbe0SAlexander V. Chernikov static int vlan_clone_destroy(struct if_clone *, struct ifnet *, uint32_t); 326f889d2efSBrooks Davis 327089104e0SAlexander V. Chernikov static int vlan_clone_create_nl(struct if_clone *ifc, char *name, size_t len, 328089104e0SAlexander V. Chernikov struct ifc_data_nl *ifd); 329089104e0SAlexander V. Chernikov static int vlan_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd); 330089104e0SAlexander V. Chernikov static void vlan_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw); 331089104e0SAlexander V. Chernikov 3325cb8c31aSYaroslav Tykhiy static void vlan_ifdetach(void *arg, struct ifnet *ifp); 333ea4ca115SAndrew Thompson static void vlan_iflladdr(void *arg, struct ifnet *ifp); 334f2ab9160SAndrey V. Elsukov static void vlan_ifevent(void *arg, struct ifnet *ifp, int event); 3355cb8c31aSYaroslav Tykhiy 336d148c2a2SMatt Joras static void vlan_lladdr_fn(void *arg, int pending); 337d148c2a2SMatt Joras 33842a58907SGleb Smirnoff static struct if_clone *vlan_cloner; 3399d4fe4b2SBrooks Davis 340ccf7ba97SMarko Zec #ifdef VIMAGE 3415f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner); 342ccf7ba97SMarko Zec #define V_vlan_cloner VNET(vlan_cloner) 343ccf7ba97SMarko Zec #endif 344ccf7ba97SMarko Zec 345c782ea8bSJohn Baldwin #ifdef RATELIMIT 346c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_ul_sw = { 347c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 348c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 349c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 350c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 351c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_UNLIMITED 352c782ea8bSJohn Baldwin }; 353c782ea8bSJohn Baldwin 354c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_rl_sw = { 355c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 356c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 357c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 358c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 359c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_RATE_LIMIT 360c782ea8bSJohn Baldwin }; 361c782ea8bSJohn Baldwin #endif 362c782ea8bSJohn Baldwin 363c782ea8bSJohn Baldwin #ifdef KERN_TLS 364c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_tls_sw = { 365c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 366c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 367c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 368c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 369c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_TLS 370c782ea8bSJohn Baldwin }; 371c782ea8bSJohn Baldwin 372c782ea8bSJohn Baldwin #ifdef RATELIMIT 373c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_tls_rl_sw = { 374c782ea8bSJohn Baldwin .snd_tag_modify = vlan_snd_tag_modify, 375c782ea8bSJohn Baldwin .snd_tag_query = vlan_snd_tag_query, 376c782ea8bSJohn Baldwin .snd_tag_free = vlan_snd_tag_free, 377c782ea8bSJohn Baldwin .next_snd_tag = vlan_next_snd_tag, 378c782ea8bSJohn Baldwin .type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT 379c782ea8bSJohn Baldwin }; 380c782ea8bSJohn Baldwin #endif 381c782ea8bSJohn Baldwin #endif 382c782ea8bSJohn Baldwin 38375ee267cSGleb Smirnoff static void 384c32a9d66SHans Petter Selasky vlan_mc_free(struct epoch_context *ctx) 385c32a9d66SHans Petter Selasky { 386c32a9d66SHans Petter Selasky struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx); 387c32a9d66SHans Petter Selasky free(mc, M_VLAN); 388c32a9d66SHans Petter Selasky } 389c32a9d66SHans Petter Selasky 390cac30248SOleg Bulyzhin #ifndef VLAN_ARRAY 391cac30248SOleg Bulyzhin #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 392cac30248SOleg Bulyzhin 393c32a9d66SHans Petter Selasky static void 39475ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk) 39575ee267cSGleb Smirnoff { 39675ee267cSGleb Smirnoff int i, n; 39775ee267cSGleb Smirnoff 39875ee267cSGleb Smirnoff /* 39975ee267cSGleb Smirnoff * The trunk must not be locked here since we call malloc(M_WAITOK). 40075ee267cSGleb Smirnoff * It is OK in case this function is called before the trunk struct 40175ee267cSGleb Smirnoff * gets hooked up and becomes visible from other threads. 40275ee267cSGleb Smirnoff */ 40375ee267cSGleb Smirnoff 40475ee267cSGleb Smirnoff KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 40575ee267cSGleb Smirnoff ("%s: hash already initialized", __func__)); 40675ee267cSGleb Smirnoff 40775ee267cSGleb Smirnoff trunk->hwidth = VLAN_DEF_HWIDTH; 40875ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 40975ee267cSGleb Smirnoff trunk->hmask = n - 1; 41075ee267cSGleb Smirnoff trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 41175ee267cSGleb Smirnoff for (i = 0; i < n; i++) 412b08d611dSMatt Macy CK_SLIST_INIT(&trunk->hash[i]); 41375ee267cSGleb Smirnoff } 41475ee267cSGleb Smirnoff 41575ee267cSGleb Smirnoff static void 41675ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk) 41775ee267cSGleb Smirnoff { 41875ee267cSGleb Smirnoff #ifdef INVARIANTS 41975ee267cSGleb Smirnoff int i; 42075ee267cSGleb Smirnoff 42175ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 42275ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 423b08d611dSMatt Macy KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]), 42475ee267cSGleb Smirnoff ("%s: hash table not empty", __func__)); 42575ee267cSGleb Smirnoff #endif 42675ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 42775ee267cSGleb Smirnoff trunk->hash = NULL; 42875ee267cSGleb Smirnoff trunk->hwidth = trunk->hmask = 0; 42975ee267cSGleb Smirnoff } 43075ee267cSGleb Smirnoff 43175ee267cSGleb Smirnoff static int 43275ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 43375ee267cSGleb Smirnoff { 43475ee267cSGleb Smirnoff int i, b; 43575ee267cSGleb Smirnoff struct ifvlan *ifv2; 43675ee267cSGleb Smirnoff 437b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 43875ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 43975ee267cSGleb Smirnoff 44075ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 4417983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 442b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 4437983103aSRobert Watson if (ifv->ifv_vid == ifv2->ifv_vid) 44475ee267cSGleb Smirnoff return (EEXIST); 44575ee267cSGleb Smirnoff 44675ee267cSGleb Smirnoff /* 44775ee267cSGleb Smirnoff * Grow the hash when the number of vlans exceeds half of the number of 44875ee267cSGleb Smirnoff * hash buckets squared. This will make the average linked-list length 44975ee267cSGleb Smirnoff * buckets/2. 45075ee267cSGleb Smirnoff */ 45175ee267cSGleb Smirnoff if (trunk->refcnt > (b * b) / 2) { 45275ee267cSGleb Smirnoff vlan_growhash(trunk, 1); 4537983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 45475ee267cSGleb Smirnoff } 455b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 45675ee267cSGleb Smirnoff trunk->refcnt++; 45775ee267cSGleb Smirnoff 45875ee267cSGleb Smirnoff return (0); 45975ee267cSGleb Smirnoff } 46075ee267cSGleb Smirnoff 46175ee267cSGleb Smirnoff static int 46275ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 46375ee267cSGleb Smirnoff { 46475ee267cSGleb Smirnoff int i, b; 46575ee267cSGleb Smirnoff struct ifvlan *ifv2; 46675ee267cSGleb Smirnoff 467b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 46875ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 46975ee267cSGleb Smirnoff 470151abc80SKristof Provost b = 1 << (trunk->hwidth - 1); 4717983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 472b08d611dSMatt Macy CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 47375ee267cSGleb Smirnoff if (ifv2 == ifv) { 47475ee267cSGleb Smirnoff trunk->refcnt--; 475b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list); 47675ee267cSGleb Smirnoff if (trunk->refcnt < (b * b) / 2) 47775ee267cSGleb Smirnoff vlan_growhash(trunk, -1); 47875ee267cSGleb Smirnoff return (0); 47975ee267cSGleb Smirnoff } 48075ee267cSGleb Smirnoff 48175ee267cSGleb Smirnoff panic("%s: vlan not found\n", __func__); 48275ee267cSGleb Smirnoff return (ENOENT); /*NOTREACHED*/ 48375ee267cSGleb Smirnoff } 48475ee267cSGleb Smirnoff 48575ee267cSGleb Smirnoff /* 48675ee267cSGleb Smirnoff * Grow the hash larger or smaller if memory permits. 48775ee267cSGleb Smirnoff */ 48875ee267cSGleb Smirnoff static void 48975ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 49075ee267cSGleb Smirnoff { 49175ee267cSGleb Smirnoff struct ifvlan *ifv; 49275ee267cSGleb Smirnoff struct ifvlanhead *hash2; 49375ee267cSGleb Smirnoff int hwidth2, i, j, n, n2; 49475ee267cSGleb Smirnoff 495b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 49675ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 49775ee267cSGleb Smirnoff 49875ee267cSGleb Smirnoff if (howmuch == 0) { 49975ee267cSGleb Smirnoff /* Harmless yet obvious coding error */ 50075ee267cSGleb Smirnoff printf("%s: howmuch is 0\n", __func__); 50175ee267cSGleb Smirnoff return; 50275ee267cSGleb Smirnoff } 50375ee267cSGleb Smirnoff 50475ee267cSGleb Smirnoff hwidth2 = trunk->hwidth + howmuch; 50575ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 50675ee267cSGleb Smirnoff n2 = 1 << hwidth2; 50775ee267cSGleb Smirnoff /* Do not shrink the table below the default */ 50875ee267cSGleb Smirnoff if (hwidth2 < VLAN_DEF_HWIDTH) 50975ee267cSGleb Smirnoff return; 51075ee267cSGleb Smirnoff 511b08d611dSMatt Macy hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK); 51275ee267cSGleb Smirnoff if (hash2 == NULL) { 51375ee267cSGleb Smirnoff printf("%s: out of memory -- hash size not changed\n", 51475ee267cSGleb Smirnoff __func__); 51575ee267cSGleb Smirnoff return; /* We can live with the old hash table */ 51675ee267cSGleb Smirnoff } 51775ee267cSGleb Smirnoff for (j = 0; j < n2; j++) 518b08d611dSMatt Macy CK_SLIST_INIT(&hash2[j]); 51975ee267cSGleb Smirnoff for (i = 0; i < n; i++) 520b08d611dSMatt Macy while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) { 521b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list); 5227983103aSRobert Watson j = HASH(ifv->ifv_vid, n2 - 1); 523b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 52475ee267cSGleb Smirnoff } 525b08d611dSMatt Macy NET_EPOCH_WAIT(); 52675ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 52775ee267cSGleb Smirnoff trunk->hash = hash2; 52875ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 52975ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 530f84b2d69SYaroslav Tykhiy 531f84b2d69SYaroslav Tykhiy if (bootverbose) 532f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 533f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 53475ee267cSGleb Smirnoff } 53575ee267cSGleb Smirnoff 53675ee267cSGleb Smirnoff static __inline struct ifvlan * 5377983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 53875ee267cSGleb Smirnoff { 53975ee267cSGleb Smirnoff struct ifvlan *ifv; 54075ee267cSGleb Smirnoff 541a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 54275ee267cSGleb Smirnoff 543b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list) 5447983103aSRobert Watson if (ifv->ifv_vid == vid) 54575ee267cSGleb Smirnoff return (ifv); 54675ee267cSGleb Smirnoff return (NULL); 54775ee267cSGleb Smirnoff } 54875ee267cSGleb Smirnoff 54975ee267cSGleb Smirnoff #if 0 55075ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 55175ee267cSGleb Smirnoff static void 55275ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 55375ee267cSGleb Smirnoff { 55475ee267cSGleb Smirnoff int i; 55575ee267cSGleb Smirnoff struct ifvlan *ifv; 55675ee267cSGleb Smirnoff 55775ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 55875ee267cSGleb Smirnoff printf("%d: ", i); 559b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 56075ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 56175ee267cSGleb Smirnoff printf("\n"); 56275ee267cSGleb Smirnoff } 56375ee267cSGleb Smirnoff } 56475ee267cSGleb Smirnoff #endif /* 0 */ 565e4cd31ddSJeff Roberson #else 566e4cd31ddSJeff Roberson 567e4cd31ddSJeff Roberson static __inline struct ifvlan * 5687983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 569e4cd31ddSJeff Roberson { 570e4cd31ddSJeff Roberson 5717983103aSRobert Watson return trunk->vlans[vid]; 572e4cd31ddSJeff Roberson } 573e4cd31ddSJeff Roberson 574e4cd31ddSJeff Roberson static __inline int 575e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 576e4cd31ddSJeff Roberson { 577e4cd31ddSJeff Roberson 5787983103aSRobert Watson if (trunk->vlans[ifv->ifv_vid] != NULL) 579e4cd31ddSJeff Roberson return EEXIST; 5807983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = ifv; 581e4cd31ddSJeff Roberson trunk->refcnt++; 582e4cd31ddSJeff Roberson 583e4cd31ddSJeff Roberson return (0); 584e4cd31ddSJeff Roberson } 585e4cd31ddSJeff Roberson 586e4cd31ddSJeff Roberson static __inline int 587e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 588e4cd31ddSJeff Roberson { 589e4cd31ddSJeff Roberson 5907983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = NULL; 591e4cd31ddSJeff Roberson trunk->refcnt--; 592e4cd31ddSJeff Roberson 593e4cd31ddSJeff Roberson return (0); 594e4cd31ddSJeff Roberson } 595e4cd31ddSJeff Roberson 596e4cd31ddSJeff Roberson static __inline void 597e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk) 598e4cd31ddSJeff Roberson { 599e4cd31ddSJeff Roberson } 600e4cd31ddSJeff Roberson 601e4cd31ddSJeff Roberson static __inline void 602e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk) 603e4cd31ddSJeff Roberson { 604e4cd31ddSJeff Roberson } 605e4cd31ddSJeff Roberson 60675ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 60775ee267cSGleb Smirnoff 60875ee267cSGleb Smirnoff static void 60975ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 61075ee267cSGleb Smirnoff { 611d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 61275ee267cSGleb Smirnoff 61375ee267cSGleb Smirnoff vlan_freehash(trunk); 61475ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 61533499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 6169bcf3ae4SAlexander Motin if_rele(trunk->parent); 61775ee267cSGleb Smirnoff free(trunk, M_VLAN); 61875ee267cSGleb Smirnoff } 61975ee267cSGleb Smirnoff 620f731f104SBill Paul /* 621f731f104SBill Paul * Program our multicast filter. What we're actually doing is 622f731f104SBill Paul * programming the multicast filter of the parent. This has the 623f731f104SBill Paul * side effect of causing the parent interface to receive multicast 624f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 625f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 626f731f104SBill Paul * to avoid this: there really is only one physical interface. 627f731f104SBill Paul */ 6282b120974SPeter Wemm static int 6292b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 630f731f104SBill Paul { 631f731f104SBill Paul struct ifnet *ifp_p; 6322d222cb7SAlexander Motin struct ifmultiaddr *ifma; 633f731f104SBill Paul struct ifvlan *sc; 634c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 635f731f104SBill Paul int error; 636f731f104SBill Paul 637b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 638d148c2a2SMatt Joras 639f731f104SBill Paul /* Find the parent. */ 640f731f104SBill Paul sc = ifp->if_softc; 64175ee267cSGleb Smirnoff ifp_p = PARENT(sc); 6421b2a4f7aSBill Fenner 6438b615593SMarko Zec CURVNET_SET_QUIET(ifp_p->if_vnet); 6448b615593SMarko Zec 645f731f104SBill Paul /* First, remove any existing filter entries. */ 646b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 647b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 6482d222cb7SAlexander Motin (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 6492a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 650f731f104SBill Paul } 651f731f104SBill Paul 652f731f104SBill Paul /* Now program new ones. */ 6532d222cb7SAlexander Motin IF_ADDR_WLOCK(ifp); 654d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 655f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 656f731f104SBill Paul continue; 65729c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 6582d222cb7SAlexander Motin if (mc == NULL) { 6592d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 660c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 66129c2dfbeSBruce M Simpson return (ENOMEM); 6622d222cb7SAlexander Motin } 663e4cd31ddSJeff Roberson bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 664e4cd31ddSJeff Roberson mc->mc_addr.sdl_index = ifp_p->if_index; 665b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 6662d222cb7SAlexander Motin } 6672d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 668b08d611dSMatt Macy CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) { 669e4cd31ddSJeff Roberson error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 6702d222cb7SAlexander Motin NULL); 671c6b2d024SGeorge V. Neville-Neil if (error) { 672c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 673f731f104SBill Paul return (error); 674f731f104SBill Paul } 675c6b2d024SGeorge V. Neville-Neil } 676f731f104SBill Paul 6778b615593SMarko Zec CURVNET_RESTORE(); 678f731f104SBill Paul return (0); 679f731f104SBill Paul } 6802cc2df49SGarrett Wollman 681a3814acfSSam Leffler /* 682f2ab9160SAndrey V. Elsukov * A handler for interface ifnet events. 683f2ab9160SAndrey V. Elsukov */ 684f2ab9160SAndrey V. Elsukov static void 685f2ab9160SAndrey V. Elsukov vlan_ifevent(void *arg __unused, struct ifnet *ifp, int event) 686f2ab9160SAndrey V. Elsukov { 687f2ab9160SAndrey V. Elsukov struct epoch_tracker et; 688f2ab9160SAndrey V. Elsukov struct ifvlan *ifv; 689f2ab9160SAndrey V. Elsukov struct ifvlantrunk *trunk; 690f2ab9160SAndrey V. Elsukov 691f2ab9160SAndrey V. Elsukov if (event != IFNET_EVENT_UPDATE_BAUDRATE) 692f2ab9160SAndrey V. Elsukov return; 693f2ab9160SAndrey V. Elsukov 694f2ab9160SAndrey V. Elsukov NET_EPOCH_ENTER(et); 695f2ab9160SAndrey V. Elsukov trunk = ifp->if_vlantrunk; 696f2ab9160SAndrey V. Elsukov if (trunk == NULL) { 697f2ab9160SAndrey V. Elsukov NET_EPOCH_EXIT(et); 698f2ab9160SAndrey V. Elsukov return; 699f2ab9160SAndrey V. Elsukov } 700f2ab9160SAndrey V. Elsukov 701f2ab9160SAndrey V. Elsukov TRUNK_WLOCK(trunk); 702f2ab9160SAndrey V. Elsukov VLAN_FOREACH(ifv, trunk) { 703f2ab9160SAndrey V. Elsukov ifv->ifv_ifp->if_baudrate = ifp->if_baudrate; 704f2ab9160SAndrey V. Elsukov } 705f2ab9160SAndrey V. Elsukov TRUNK_WUNLOCK(trunk); 706f2ab9160SAndrey V. Elsukov NET_EPOCH_EXIT(et); 707f2ab9160SAndrey V. Elsukov } 708f2ab9160SAndrey V. Elsukov 709f2ab9160SAndrey V. Elsukov /* 710ea4ca115SAndrew Thompson * A handler for parent interface link layer address changes. 711ea4ca115SAndrew Thompson * If the parent interface link layer address is changed we 712ea4ca115SAndrew Thompson * should also change it on all children vlans. 713ea4ca115SAndrew Thompson */ 714ea4ca115SAndrew Thompson static void 715ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 716ea4ca115SAndrew Thompson { 717a68cc388SGleb Smirnoff struct epoch_tracker et; 718ea4ca115SAndrew Thompson struct ifvlan *ifv; 719d148c2a2SMatt Joras struct ifnet *ifv_ifp; 720d148c2a2SMatt Joras struct ifvlantrunk *trunk; 721d148c2a2SMatt Joras struct sockaddr_dl *sdl; 722ea4ca115SAndrew Thompson 7237b7f772fSGleb Smirnoff /* Need the epoch since this is run on taskqueue_swi. */ 724a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 725d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 726d148c2a2SMatt Joras if (trunk == NULL) { 727a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 728ea4ca115SAndrew Thompson return; 729d148c2a2SMatt Joras } 730ea4ca115SAndrew Thompson 731ea4ca115SAndrew Thompson /* 732ea4ca115SAndrew Thompson * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 733d148c2a2SMatt Joras * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR 734d148c2a2SMatt Joras * ioctl calls on the parent garbling the lladdr of the child vlan. 735ea4ca115SAndrew Thompson */ 736d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 737d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 738d148c2a2SMatt Joras /* 739d148c2a2SMatt Joras * Copy new new lladdr into the ifv_ifp, enqueue a task 740d148c2a2SMatt Joras * to actually call if_setlladdr. if_setlladdr needs to 741d148c2a2SMatt Joras * be deferred to a taskqueue because it will call into 742d148c2a2SMatt Joras * the if_vlan ioctl path and try to acquire the global 743d148c2a2SMatt Joras * lock. 744d148c2a2SMatt Joras */ 745d148c2a2SMatt Joras ifv_ifp = ifv->ifv_ifp; 746d148c2a2SMatt Joras bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp), 747e4cd31ddSJeff Roberson ifp->if_addrlen); 748d148c2a2SMatt Joras sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr; 749d148c2a2SMatt Joras sdl->sdl_alen = ifp->if_addrlen; 750d148c2a2SMatt Joras taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 7516117727bSAndrew Thompson } 752d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 753a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 754ea4ca115SAndrew Thompson } 755ea4ca115SAndrew Thompson 756ea4ca115SAndrew Thompson /* 7575cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 7585cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 7595cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 7605428776eSJohn Baldwin * by ejecting its hot-plug card. However, if an ifnet is simply 7615428776eSJohn Baldwin * being renamed, then there's no need to tear down the state. 7625cb8c31aSYaroslav Tykhiy */ 7635cb8c31aSYaroslav Tykhiy static void 7645cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 7655cb8c31aSYaroslav Tykhiy { 7665cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 767d148c2a2SMatt Joras struct ifvlantrunk *trunk; 7685cb8c31aSYaroslav Tykhiy 7695428776eSJohn Baldwin /* If the ifnet is just being renamed, don't do anything. */ 7705428776eSJohn Baldwin if (ifp->if_flags & IFF_RENAMING) 7715428776eSJohn Baldwin return; 772d148c2a2SMatt Joras VLAN_XLOCK(); 773d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 774d148c2a2SMatt Joras if (trunk == NULL) { 775d148c2a2SMatt Joras VLAN_XUNLOCK(); 776d148c2a2SMatt Joras return; 777d148c2a2SMatt Joras } 7785428776eSJohn Baldwin 7795cb8c31aSYaroslav Tykhiy /* 7805cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 7815cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 7825cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 7835cb8c31aSYaroslav Tykhiy */ 784d148c2a2SMatt Joras VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk, 785d148c2a2SMatt Joras ifp->if_vlantrunk == NULL) 78628cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 787d148c2a2SMatt Joras 7885cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 7895cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 790d148c2a2SMatt Joras VLAN_XUNLOCK(); 7915cb8c31aSYaroslav Tykhiy } 7925cb8c31aSYaroslav Tykhiy 7935cb8c31aSYaroslav Tykhiy /* 794e4cd31ddSJeff Roberson * Return the trunk device for a virtual interface. 795e4cd31ddSJeff Roberson */ 796e4cd31ddSJeff Roberson static struct ifnet * 797e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp) 798e4cd31ddSJeff Roberson { 799e4cd31ddSJeff Roberson struct ifvlan *ifv; 800e4cd31ddSJeff Roberson 801b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 802b8a6e03fSGleb Smirnoff 803e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 804e4cd31ddSJeff Roberson return (NULL); 805d148c2a2SMatt Joras 806e4cd31ddSJeff Roberson ifv = ifp->if_softc; 807e4cd31ddSJeff Roberson ifp = NULL; 808e4cd31ddSJeff Roberson if (ifv->ifv_trunk) 809e4cd31ddSJeff Roberson ifp = PARENT(ifv); 810e4cd31ddSJeff Roberson return (ifp); 811e4cd31ddSJeff Roberson } 812e4cd31ddSJeff Roberson 813e4cd31ddSJeff Roberson /* 8147983103aSRobert Watson * Return the 12-bit VLAN VID for this interface, for use by external 8157983103aSRobert Watson * components such as Infiniband. 8167983103aSRobert Watson * 8177983103aSRobert Watson * XXXRW: Note that the function name here is historical; it should be named 8187983103aSRobert Watson * vlan_vid(). 819e4cd31ddSJeff Roberson */ 820e4cd31ddSJeff Roberson static int 8217983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp) 822e4cd31ddSJeff Roberson { 823e4cd31ddSJeff Roberson struct ifvlan *ifv; 824e4cd31ddSJeff Roberson 825e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 826e4cd31ddSJeff Roberson return (EINVAL); 827e4cd31ddSJeff Roberson ifv = ifp->if_softc; 8287983103aSRobert Watson *vidp = ifv->ifv_vid; 829e4cd31ddSJeff Roberson return (0); 830e4cd31ddSJeff Roberson } 831e4cd31ddSJeff Roberson 83232d2623aSNavdeep Parhar static int 83332d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp) 83432d2623aSNavdeep Parhar { 83532d2623aSNavdeep Parhar struct ifvlan *ifv; 83632d2623aSNavdeep Parhar 83732d2623aSNavdeep Parhar if (ifp->if_type != IFT_L2VLAN) 83832d2623aSNavdeep Parhar return (EINVAL); 83932d2623aSNavdeep Parhar ifv = ifp->if_softc; 84032d2623aSNavdeep Parhar *pcpp = ifv->ifv_pcp; 84132d2623aSNavdeep Parhar return (0); 84232d2623aSNavdeep Parhar } 84332d2623aSNavdeep Parhar 844e4cd31ddSJeff Roberson /* 845e4cd31ddSJeff Roberson * Return a driver specific cookie for this interface. Synchronization 846e4cd31ddSJeff Roberson * with setcookie must be provided by the driver. 847e4cd31ddSJeff Roberson */ 848e4cd31ddSJeff Roberson static void * 849e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp) 850e4cd31ddSJeff Roberson { 851e4cd31ddSJeff Roberson struct ifvlan *ifv; 852e4cd31ddSJeff Roberson 853e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 854e4cd31ddSJeff Roberson return (NULL); 855e4cd31ddSJeff Roberson ifv = ifp->if_softc; 856e4cd31ddSJeff Roberson return (ifv->ifv_cookie); 857e4cd31ddSJeff Roberson } 858e4cd31ddSJeff Roberson 859e4cd31ddSJeff Roberson /* 860e4cd31ddSJeff Roberson * Store a cookie in our softc that drivers can use to store driver 861e4cd31ddSJeff Roberson * private per-instance data in. 862e4cd31ddSJeff Roberson */ 863e4cd31ddSJeff Roberson static int 864e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie) 865e4cd31ddSJeff Roberson { 866e4cd31ddSJeff Roberson struct ifvlan *ifv; 867e4cd31ddSJeff Roberson 868e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 869e4cd31ddSJeff Roberson return (EINVAL); 870e4cd31ddSJeff Roberson ifv = ifp->if_softc; 871e4cd31ddSJeff Roberson ifv->ifv_cookie = cookie; 872e4cd31ddSJeff Roberson return (0); 873e4cd31ddSJeff Roberson } 874e4cd31ddSJeff Roberson 875e4cd31ddSJeff Roberson /* 8767983103aSRobert Watson * Return the vlan device present at the specific VID. 877e4cd31ddSJeff Roberson */ 878e4cd31ddSJeff Roberson static struct ifnet * 8797983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid) 880e4cd31ddSJeff Roberson { 881e4cd31ddSJeff Roberson struct ifvlantrunk *trunk; 882e4cd31ddSJeff Roberson struct ifvlan *ifv; 883e4cd31ddSJeff Roberson 884b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 885b8a6e03fSGleb Smirnoff 886e4cd31ddSJeff Roberson trunk = ifp->if_vlantrunk; 887b8a6e03fSGleb Smirnoff if (trunk == NULL) 888e4cd31ddSJeff Roberson return (NULL); 889e4cd31ddSJeff Roberson ifp = NULL; 8907983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 891e4cd31ddSJeff Roberson if (ifv) 892e4cd31ddSJeff Roberson ifp = ifv->ifv_ifp; 893e4cd31ddSJeff Roberson return (ifp); 894e4cd31ddSJeff Roberson } 895e4cd31ddSJeff Roberson 896e4cd31ddSJeff Roberson /* 897a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 898a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 899a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 900a3814acfSSam Leffler * set here. No one else in the system should be aware of this so 901a3814acfSSam Leffler * we use an explicit reference here. 902a3814acfSSam Leffler */ 903a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 904a3814acfSSam Leffler 905984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 906a6fffd6cSBrooks Davis extern void (*vlan_link_state_p)(struct ifnet *); 907127d7b2dSAndre Oppermann 908089104e0SAlexander V. Chernikov static struct if_clone_addreq_v2 vlan_addreq = { 909089104e0SAlexander V. Chernikov .version = 2, 91091ebcbe0SAlexander V. Chernikov .match_f = vlan_clone_match, 91191ebcbe0SAlexander V. Chernikov .create_f = vlan_clone_create, 91291ebcbe0SAlexander V. Chernikov .destroy_f = vlan_clone_destroy, 913089104e0SAlexander V. Chernikov .create_nl_f = vlan_clone_create_nl, 914089104e0SAlexander V. Chernikov .modify_nl_f = vlan_clone_modify_nl, 915089104e0SAlexander V. Chernikov .dump_nl_f = vlan_clone_dump_nl, 91691ebcbe0SAlexander V. Chernikov }; 91791ebcbe0SAlexander V. Chernikov 9182b120974SPeter Wemm static int 9192b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 9202b120974SPeter Wemm { 9219d4fe4b2SBrooks Davis 9222b120974SPeter Wemm switch (type) { 9232b120974SPeter Wemm case MOD_LOAD: 9245cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 9255cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 9265cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 9275cb8c31aSYaroslav Tykhiy return (ENOMEM); 928ea4ca115SAndrew Thompson iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 929ea4ca115SAndrew Thompson vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 930ea4ca115SAndrew Thompson if (iflladdr_tag == NULL) 931ea4ca115SAndrew Thompson return (ENOMEM); 932f2ab9160SAndrey V. Elsukov ifevent_tag = EVENTHANDLER_REGISTER(ifnet_event, 933f2ab9160SAndrey V. Elsukov vlan_ifevent, NULL, EVENTHANDLER_PRI_ANY); 934f2ab9160SAndrey V. Elsukov if (ifevent_tag == NULL) 935f2ab9160SAndrey V. Elsukov return (ENOMEM); 936d148c2a2SMatt Joras VLAN_LOCKING_INIT(); 9379d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 938127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 93975ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 940e4cd31ddSJeff Roberson vlan_trunkdev_p = vlan_trunkdev; 941e4cd31ddSJeff Roberson vlan_cookie_p = vlan_cookie; 942e4cd31ddSJeff Roberson vlan_setcookie_p = vlan_setcookie; 943e4cd31ddSJeff Roberson vlan_tag_p = vlan_tag; 94432d2623aSNavdeep Parhar vlan_pcp_p = vlan_pcp; 945e4cd31ddSJeff Roberson vlan_devat_p = vlan_devat; 946ccf7ba97SMarko Zec #ifndef VIMAGE 947089104e0SAlexander V. Chernikov vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq); 948ccf7ba97SMarko Zec #endif 94925c0f7b3SYaroslav Tykhiy if (bootverbose) 95025c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 95125c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 95225c0f7b3SYaroslav Tykhiy "full-size arrays" 95325c0f7b3SYaroslav Tykhiy #else 95425c0f7b3SYaroslav Tykhiy "hash tables with chaining" 95525c0f7b3SYaroslav Tykhiy #endif 95625c0f7b3SYaroslav Tykhiy 95725c0f7b3SYaroslav Tykhiy "\n"); 9582b120974SPeter Wemm break; 9592b120974SPeter Wemm case MOD_UNLOAD: 960ccf7ba97SMarko Zec #ifndef VIMAGE 96191ebcbe0SAlexander V. Chernikov ifc_detach_cloner(vlan_cloner); 962ccf7ba97SMarko Zec #endif 9635cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 964ea4ca115SAndrew Thompson EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 965f2ab9160SAndrey V. Elsukov EVENTHANDLER_DEREGISTER(ifnet_event, ifevent_tag); 9669d4fe4b2SBrooks Davis vlan_input_p = NULL; 967127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 96875ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 969e4cd31ddSJeff Roberson vlan_trunkdev_p = NULL; 970e4cd31ddSJeff Roberson vlan_tag_p = NULL; 97109fe6320SNavdeep Parhar vlan_cookie_p = NULL; 97209fe6320SNavdeep Parhar vlan_setcookie_p = NULL; 973e4cd31ddSJeff Roberson vlan_devat_p = NULL; 974d148c2a2SMatt Joras VLAN_LOCKING_DESTROY(); 97525c0f7b3SYaroslav Tykhiy if (bootverbose) 97625c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 9779d4fe4b2SBrooks Davis break; 9783e019deaSPoul-Henning Kamp default: 9793e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 9802b120974SPeter Wemm } 98115a66c21SBruce M Simpson return (0); 9822b120974SPeter Wemm } 9832b120974SPeter Wemm 9842b120974SPeter Wemm static moduledata_t vlan_mod = { 9852b120974SPeter Wemm "if_vlan", 9862b120974SPeter Wemm vlan_modevent, 9879823d527SKevin Lo 0 9882b120974SPeter Wemm }; 9892b120974SPeter Wemm 9902b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 99111edc477SEd Maste MODULE_VERSION(if_vlan, 3); 9922cc2df49SGarrett Wollman 993ccf7ba97SMarko Zec #ifdef VIMAGE 994ccf7ba97SMarko Zec static void 995ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused) 996ccf7ba97SMarko Zec { 997089104e0SAlexander V. Chernikov vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq); 998ccf7ba97SMarko Zec V_vlan_cloner = vlan_cloner; 999ccf7ba97SMarko Zec } 1000ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 1001ccf7ba97SMarko Zec vnet_vlan_init, NULL); 1002ccf7ba97SMarko Zec 1003ccf7ba97SMarko Zec static void 1004ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused) 1005ccf7ba97SMarko Zec { 1006ccf7ba97SMarko Zec 100791ebcbe0SAlexander V. Chernikov ifc_detach_cloner(V_vlan_cloner); 1008ccf7ba97SMarko Zec } 1009eb03a443SKristof Provost VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 1010ccf7ba97SMarko Zec vnet_vlan_uninit, NULL); 1011ccf7ba97SMarko Zec #endif 1012ccf7ba97SMarko Zec 1013f941c31aSGleb Smirnoff /* 1014c7cffd65SAlexander V. Chernikov * Check for <etherif>.<vlan>[.<vlan> ...] style interface names. 1015f941c31aSGleb Smirnoff */ 1016f889d2efSBrooks Davis static struct ifnet * 1017f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp) 10189d4fe4b2SBrooks Davis { 1019f941c31aSGleb Smirnoff char ifname[IFNAMSIZ]; 1020f941c31aSGleb Smirnoff char *cp; 1021f889d2efSBrooks Davis struct ifnet *ifp; 10227983103aSRobert Watson int vid; 1023f889d2efSBrooks Davis 1024f941c31aSGleb Smirnoff strlcpy(ifname, name, IFNAMSIZ); 1025c7cffd65SAlexander V. Chernikov if ((cp = strrchr(ifname, '.')) == NULL) 1026f941c31aSGleb Smirnoff return (NULL); 1027f941c31aSGleb Smirnoff *cp = '\0'; 10289bcf3ae4SAlexander Motin if ((ifp = ifunit_ref(ifname)) == NULL) 1029f941c31aSGleb Smirnoff return (NULL); 1030f941c31aSGleb Smirnoff /* Parse VID. */ 10319bcf3ae4SAlexander Motin if (*++cp == '\0') { 10329bcf3ae4SAlexander Motin if_rele(ifp); 1033f941c31aSGleb Smirnoff return (NULL); 10349bcf3ae4SAlexander Motin } 10357983103aSRobert Watson vid = 0; 1036fb92ad4aSJohn Baldwin for(; *cp >= '0' && *cp <= '9'; cp++) 10377983103aSRobert Watson vid = (vid * 10) + (*cp - '0'); 10389bcf3ae4SAlexander Motin if (*cp != '\0') { 10399bcf3ae4SAlexander Motin if_rele(ifp); 1040f941c31aSGleb Smirnoff return (NULL); 10419bcf3ae4SAlexander Motin } 10427983103aSRobert Watson if (vidp != NULL) 10437983103aSRobert Watson *vidp = vid; 1044f889d2efSBrooks Davis 104515a66c21SBruce M Simpson return (ifp); 1046f889d2efSBrooks Davis } 1047f889d2efSBrooks Davis 1048f889d2efSBrooks Davis static int 1049f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 1050f889d2efSBrooks Davis { 10514be465abSAlexander V. Chernikov struct ifnet *ifp; 1052f889d2efSBrooks Davis const char *cp; 1053f889d2efSBrooks Davis 10544be465abSAlexander V. Chernikov ifp = vlan_clone_match_ethervid(name, NULL); 10554be465abSAlexander V. Chernikov if (ifp != NULL) { 10564be465abSAlexander V. Chernikov if_rele(ifp); 1057f889d2efSBrooks Davis return (1); 10584be465abSAlexander V. Chernikov } 1059f889d2efSBrooks Davis 106042a58907SGleb Smirnoff if (strncmp(vlanname, name, strlen(vlanname)) != 0) 1061f889d2efSBrooks Davis return (0); 1062f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 1063f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 1064f889d2efSBrooks Davis return (0); 1065f889d2efSBrooks Davis } 1066f889d2efSBrooks Davis 1067f889d2efSBrooks Davis return (1); 1068f889d2efSBrooks Davis } 1069f889d2efSBrooks Davis 1070f889d2efSBrooks Davis static int 107191ebcbe0SAlexander V. Chernikov vlan_clone_create(struct if_clone *ifc, char *name, size_t len, 107291ebcbe0SAlexander V. Chernikov struct ifc_data *ifd, struct ifnet **ifpp) 1073f889d2efSBrooks Davis { 1074f889d2efSBrooks Davis char *dp; 107553729367SAlexander V. Chernikov bool wildcard = false; 107653729367SAlexander V. Chernikov bool subinterface = false; 1077f889d2efSBrooks Davis int unit; 1078f889d2efSBrooks Davis int error; 107953729367SAlexander V. Chernikov int vid = 0; 108053729367SAlexander V. Chernikov uint16_t proto = ETHERTYPE_VLAN; 10819d4fe4b2SBrooks Davis struct ifvlan *ifv; 10829d4fe4b2SBrooks Davis struct ifnet *ifp; 108353729367SAlexander V. Chernikov struct ifnet *p = NULL; 10843ba24fdeSJohn Baldwin struct ifaddr *ifa; 10853ba24fdeSJohn Baldwin struct sockaddr_dl *sdl; 10866b7330e2SSam Leffler struct vlanreq vlr; 108765239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 1088f889d2efSBrooks Davis 1089c7cffd65SAlexander V. Chernikov 10906b7330e2SSam Leffler /* 109153729367SAlexander V. Chernikov * There are three ways to specify the cloned device: 10926b7330e2SSam Leffler * o pass a parameter block with the clone request. 109353729367SAlexander V. Chernikov * o specify parameters in the text of the clone device name 10946b7330e2SSam Leffler * o specify no parameters and get an unattached device that 10956b7330e2SSam Leffler * must be configured separately. 109653729367SAlexander V. Chernikov * The first technique is preferred; the latter two are supported 1097c7cffd65SAlexander V. Chernikov * for backwards compatibility. 10987983103aSRobert Watson * 10997983103aSRobert Watson * XXXRW: Note historic use of the word "tag" here. New ioctls may be 11007983103aSRobert Watson * called for. 11016b7330e2SSam Leffler */ 110253729367SAlexander V. Chernikov 110391ebcbe0SAlexander V. Chernikov if (ifd->params != NULL) { 110491ebcbe0SAlexander V. Chernikov error = ifc_copyin(ifd, &vlr, sizeof(vlr)); 11056b7330e2SSam Leffler if (error) 11066b7330e2SSam Leffler return error; 110753729367SAlexander V. Chernikov vid = vlr.vlr_tag; 110853729367SAlexander V. Chernikov proto = vlr.vlr_proto; 1109afbb64f1SAlexander V. Chernikov if (proto == 0) 1110afbb64f1SAlexander V. Chernikov proto = ETHERTYPE_VLAN; 11119bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 11126b7330e2SSam Leffler if (p == NULL) 1113b1828acfSGleb Smirnoff return (ENXIO); 111453729367SAlexander V. Chernikov } 111553729367SAlexander V. Chernikov 111653729367SAlexander V. Chernikov if ((error = ifc_name2unit(name, &unit)) == 0) { 111753729367SAlexander V. Chernikov 111853729367SAlexander V. Chernikov /* 111953729367SAlexander V. Chernikov * vlanX interface. Set wildcard to true if the unit number 112053729367SAlexander V. Chernikov * is not fixed (-1) 112153729367SAlexander V. Chernikov */ 112253729367SAlexander V. Chernikov wildcard = (unit < 0); 112353729367SAlexander V. Chernikov } else { 112453729367SAlexander V. Chernikov struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid); 112553729367SAlexander V. Chernikov if (p_tmp != NULL) { 112653729367SAlexander V. Chernikov error = 0; 112753729367SAlexander V. Chernikov subinterface = true; 112853729367SAlexander V. Chernikov unit = IF_DUNIT_NONE; 112953729367SAlexander V. Chernikov wildcard = false; 113053729367SAlexander V. Chernikov if (p != NULL) { 113153729367SAlexander V. Chernikov if_rele(p_tmp); 113253729367SAlexander V. Chernikov if (p != p_tmp) 113353729367SAlexander V. Chernikov error = EINVAL; 113453729367SAlexander V. Chernikov } else 113553729367SAlexander V. Chernikov p = p_tmp; 113653729367SAlexander V. Chernikov } else 113753729367SAlexander V. Chernikov error = ENXIO; 113853729367SAlexander V. Chernikov } 113953729367SAlexander V. Chernikov 11409bcf3ae4SAlexander Motin if (error != 0) { 114153729367SAlexander V. Chernikov if (p != NULL) 11429bcf3ae4SAlexander Motin if_rele(p); 11436b7330e2SSam Leffler return (error); 11449bcf3ae4SAlexander Motin } 1145f889d2efSBrooks Davis 114653729367SAlexander V. Chernikov if (!subinterface) { 114753729367SAlexander V. Chernikov /* vlanX interface, mark X as busy or allocate new unit # */ 1148f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 11499bcf3ae4SAlexander Motin if (error != 0) { 11509bcf3ae4SAlexander Motin if (p != NULL) 11519bcf3ae4SAlexander Motin if_rele(p); 1152f889d2efSBrooks Davis return (error); 11539bcf3ae4SAlexander Motin } 115453729367SAlexander V. Chernikov } 1155f889d2efSBrooks Davis 1156f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 1157f889d2efSBrooks Davis if (wildcard) { 1158f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 1159f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 1160f889d2efSBrooks Davis len - (dp-name) - 1) { 1161f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 1162f889d2efSBrooks Davis } 1163f889d2efSBrooks Davis } 11649d4fe4b2SBrooks Davis 1165a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 1166fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 1167b08d611dSMatt Macy CK_SLIST_INIT(&ifv->vlan_mc_listhead); 11689d4fe4b2SBrooks Davis ifp->if_softc = ifv; 1169f889d2efSBrooks Davis /* 1170cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 1171f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 1172f889d2efSBrooks Davis */ 1173f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 117442a58907SGleb Smirnoff ifp->if_dname = vlanname; 1175f889d2efSBrooks Davis ifp->if_dunit = unit; 11769d4fe4b2SBrooks Davis 1177114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 11782e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 11792e5ff01dSLuiz Otavio O Souza ifp->if_start = vlan_altq_start; 11802e5ff01dSLuiz Otavio O Souza ifp->if_transmit = vlan_altq_transmit; 11812e5ff01dSLuiz Otavio O Souza IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 11822e5ff01dSLuiz Otavio O Souza ifp->if_snd.ifq_drv_maxlen = 0; 11832e5ff01dSLuiz Otavio O Souza IFQ_SET_READY(&ifp->if_snd); 11842e5ff01dSLuiz Otavio O Souza #else 1185d9b1d615SJohn Baldwin ifp->if_transmit = vlan_transmit; 11862e5ff01dSLuiz Otavio O Souza #endif 1187d9b1d615SJohn Baldwin ifp->if_qflush = vlan_qflush; 11889d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 1189b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1190f3e7afe2SHans Petter Selasky ifp->if_snd_tag_alloc = vlan_snd_tag_alloc; 11911a714ff2SRandall Stewart ifp->if_ratelimit_query = vlan_ratelimit_query; 1192f3e7afe2SHans Petter Selasky #endif 119364a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 1194fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 11959d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 1196211f625aSBill Fenner ifp->if_baudrate = 0; 1197a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 1198a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 11993ba24fdeSJohn Baldwin ifa = ifp->if_addr; 12003ba24fdeSJohn Baldwin sdl = (struct sockaddr_dl *)ifa->ifa_addr; 12013ba24fdeSJohn Baldwin sdl->sdl_type = IFT_L2VLAN; 12029d4fe4b2SBrooks Davis 12039bcf3ae4SAlexander Motin if (p != NULL) { 1204c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vid, proto); 12059bcf3ae4SAlexander Motin if_rele(p); 1206f889d2efSBrooks Davis if (error != 0) { 1207f889d2efSBrooks Davis /* 120828cc4d37SJohn Baldwin * Since we've partially failed, we need to back 1209f889d2efSBrooks Davis * out all the way, otherwise userland could get 1210f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 1211f889d2efSBrooks Davis */ 1212f889d2efSBrooks Davis ether_ifdetach(ifp); 1213249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 12144b22573aSBrooks Davis if_free(ifp); 121553729367SAlexander V. Chernikov if (!subinterface) 121696c8ef3aSMaxim Konovalov ifc_free_unit(ifc, unit); 1217f889d2efSBrooks Davis free(ifv, M_VLAN); 1218f889d2efSBrooks Davis 1219f889d2efSBrooks Davis return (error); 1220f889d2efSBrooks Davis } 1221f889d2efSBrooks Davis } 122291ebcbe0SAlexander V. Chernikov *ifpp = ifp; 1223f889d2efSBrooks Davis 12249d4fe4b2SBrooks Davis return (0); 12259d4fe4b2SBrooks Davis } 12269d4fe4b2SBrooks Davis 1227089104e0SAlexander V. Chernikov /* 1228089104e0SAlexander V. Chernikov * 1229089104e0SAlexander V. Chernikov * Parsers of IFLA_INFO_DATA inside IFLA_LINKINFO of RTM_NEWLINK 1230089104e0SAlexander V. Chernikov * {{nla_len=8, nla_type=IFLA_LINK}, 2}, 1231089104e0SAlexander V. Chernikov * {{nla_len=12, nla_type=IFLA_IFNAME}, "xvlan22"}, 1232089104e0SAlexander V. Chernikov * {{nla_len=24, nla_type=IFLA_LINKINFO}, 1233089104e0SAlexander V. Chernikov * [ 1234089104e0SAlexander V. Chernikov * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...}, 1235089104e0SAlexander V. Chernikov * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]} 1236089104e0SAlexander V. Chernikov */ 1237089104e0SAlexander V. Chernikov 1238089104e0SAlexander V. Chernikov struct nl_parsed_vlan { 1239089104e0SAlexander V. Chernikov uint16_t vlan_id; 1240089104e0SAlexander V. Chernikov uint16_t vlan_proto; 1241089104e0SAlexander V. Chernikov struct ifla_vlan_flags vlan_flags; 1242089104e0SAlexander V. Chernikov }; 1243089104e0SAlexander V. Chernikov 1244089104e0SAlexander V. Chernikov #define _OUT(_field) offsetof(struct nl_parsed_vlan, _field) 1245089104e0SAlexander V. Chernikov static const struct nlattr_parser nla_p_vlan[] = { 1246089104e0SAlexander V. Chernikov { .type = IFLA_VLAN_ID, .off = _OUT(vlan_id), .cb = nlattr_get_uint16 }, 1247089104e0SAlexander V. Chernikov { .type = IFLA_VLAN_FLAGS, .off = _OUT(vlan_flags), .cb = nlattr_get_nla }, 1248089104e0SAlexander V. Chernikov { .type = IFLA_VLAN_PROTOCOL, .off = _OUT(vlan_proto), .cb = nlattr_get_uint16 }, 1249089104e0SAlexander V. Chernikov }; 1250089104e0SAlexander V. Chernikov #undef _OUT 1251089104e0SAlexander V. Chernikov NL_DECLARE_ATTR_PARSER(vlan_parser, nla_p_vlan); 1252089104e0SAlexander V. Chernikov 1253089104e0SAlexander V. Chernikov static int 1254089104e0SAlexander V. Chernikov vlan_clone_create_nl(struct if_clone *ifc, char *name, size_t len, 1255089104e0SAlexander V. Chernikov struct ifc_data_nl *ifd) 1256089104e0SAlexander V. Chernikov { 1257089104e0SAlexander V. Chernikov struct epoch_tracker et; 1258089104e0SAlexander V. Chernikov struct ifnet *ifp_parent; 1259089104e0SAlexander V. Chernikov struct nl_pstate *npt = ifd->npt; 1260089104e0SAlexander V. Chernikov struct nl_parsed_link *lattrs = ifd->lattrs; 1261089104e0SAlexander V. Chernikov int error; 1262089104e0SAlexander V. Chernikov 1263089104e0SAlexander V. Chernikov /* 1264089104e0SAlexander V. Chernikov * lattrs.ifla_ifname is the new interface name 1265089104e0SAlexander V. Chernikov * lattrs.ifi_index contains parent interface index 1266089104e0SAlexander V. Chernikov * lattrs.ifla_idata contains un-parsed vlan data 1267089104e0SAlexander V. Chernikov */ 1268089104e0SAlexander V. Chernikov struct nl_parsed_vlan attrs = { 1269089104e0SAlexander V. Chernikov .vlan_id = 0xFEFE, 1270089104e0SAlexander V. Chernikov .vlan_proto = ETHERTYPE_VLAN 1271089104e0SAlexander V. Chernikov }; 1272089104e0SAlexander V. Chernikov 1273089104e0SAlexander V. Chernikov if (lattrs->ifla_idata == NULL) { 1274089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "vlan id is required, guessing not supported"); 1275089104e0SAlexander V. Chernikov return (ENOTSUP); 1276089104e0SAlexander V. Chernikov } 1277089104e0SAlexander V. Chernikov 1278089104e0SAlexander V. Chernikov error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, npt, &attrs); 1279089104e0SAlexander V. Chernikov if (error != 0) 1280089104e0SAlexander V. Chernikov return (error); 1281089104e0SAlexander V. Chernikov if (attrs.vlan_id > 4095) { 1282089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "Invalid VID: %d", attrs.vlan_id); 1283089104e0SAlexander V. Chernikov return (EINVAL); 1284089104e0SAlexander V. Chernikov } 1285089104e0SAlexander V. Chernikov if (attrs.vlan_proto != ETHERTYPE_VLAN && attrs.vlan_proto != ETHERTYPE_QINQ) { 1286089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "Unsupported ethertype: 0x%04X", attrs.vlan_proto); 1287089104e0SAlexander V. Chernikov return (ENOTSUP); 1288089104e0SAlexander V. Chernikov } 1289089104e0SAlexander V. Chernikov 1290089104e0SAlexander V. Chernikov struct vlanreq params = { 1291089104e0SAlexander V. Chernikov .vlr_tag = attrs.vlan_id, 1292089104e0SAlexander V. Chernikov .vlr_proto = attrs.vlan_proto, 1293089104e0SAlexander V. Chernikov }; 1294089104e0SAlexander V. Chernikov struct ifc_data ifd_new = { .flags = IFC_F_SYSSPACE, .unit = ifd->unit, .params = ¶ms }; 1295089104e0SAlexander V. Chernikov 1296089104e0SAlexander V. Chernikov NET_EPOCH_ENTER(et); 1297089104e0SAlexander V. Chernikov ifp_parent = ifnet_byindex(lattrs->ifi_index); 1298089104e0SAlexander V. Chernikov if (ifp_parent != NULL) 1299089104e0SAlexander V. Chernikov strlcpy(params.vlr_parent, if_name(ifp_parent), sizeof(params.vlr_parent)); 1300089104e0SAlexander V. Chernikov NET_EPOCH_EXIT(et); 1301089104e0SAlexander V. Chernikov 1302089104e0SAlexander V. Chernikov if (ifp_parent == NULL) { 1303089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "unable to find parent interface %u", lattrs->ifi_index); 1304089104e0SAlexander V. Chernikov return (ENOENT); 1305089104e0SAlexander V. Chernikov } 1306089104e0SAlexander V. Chernikov 1307089104e0SAlexander V. Chernikov error = vlan_clone_create(ifc, name, len, &ifd_new, &ifd->ifp); 1308089104e0SAlexander V. Chernikov 1309089104e0SAlexander V. Chernikov return (error); 1310089104e0SAlexander V. Chernikov } 1311089104e0SAlexander V. Chernikov 1312089104e0SAlexander V. Chernikov static int 1313089104e0SAlexander V. Chernikov vlan_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd) 1314089104e0SAlexander V. Chernikov { 1315089104e0SAlexander V. Chernikov struct nl_parsed_link *lattrs = ifd->lattrs; 1316089104e0SAlexander V. Chernikov 1317089104e0SAlexander V. Chernikov if ((lattrs->ifla_idata != NULL) && ((ifd->flags & IFC_F_CREATE) == 0)) { 1318089104e0SAlexander V. Chernikov struct epoch_tracker et; 1319089104e0SAlexander V. Chernikov struct nl_parsed_vlan attrs = { 1320089104e0SAlexander V. Chernikov .vlan_proto = ETHERTYPE_VLAN, 1321089104e0SAlexander V. Chernikov }; 1322089104e0SAlexander V. Chernikov int error; 1323089104e0SAlexander V. Chernikov 1324089104e0SAlexander V. Chernikov error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, ifd->npt, &attrs); 1325089104e0SAlexander V. Chernikov if (error != 0) 1326089104e0SAlexander V. Chernikov return (error); 1327089104e0SAlexander V. Chernikov 1328089104e0SAlexander V. Chernikov NET_EPOCH_ENTER(et); 1329089104e0SAlexander V. Chernikov struct ifnet *ifp_parent = ifnet_byindex_ref(lattrs->ifla_link); 1330089104e0SAlexander V. Chernikov NET_EPOCH_EXIT(et); 1331089104e0SAlexander V. Chernikov 1332089104e0SAlexander V. Chernikov if (ifp_parent == NULL) { 1333089104e0SAlexander V. Chernikov nlmsg_report_err_msg(ifd->npt, "unable to find parent interface %u", 1334089104e0SAlexander V. Chernikov lattrs->ifla_link); 1335089104e0SAlexander V. Chernikov return (ENOENT); 1336089104e0SAlexander V. Chernikov } 1337089104e0SAlexander V. Chernikov 1338089104e0SAlexander V. Chernikov struct ifvlan *ifv = ifp->if_softc; 1339089104e0SAlexander V. Chernikov error = vlan_config(ifv, ifp_parent, attrs.vlan_id, attrs.vlan_proto); 1340089104e0SAlexander V. Chernikov 1341089104e0SAlexander V. Chernikov if_rele(ifp_parent); 1342089104e0SAlexander V. Chernikov if (error != 0) 1343089104e0SAlexander V. Chernikov return (error); 1344089104e0SAlexander V. Chernikov } 1345089104e0SAlexander V. Chernikov 1346089104e0SAlexander V. Chernikov return (nl_modify_ifp_generic(ifp, ifd->lattrs, ifd->bm, ifd->npt)); 1347089104e0SAlexander V. Chernikov } 1348089104e0SAlexander V. Chernikov 1349089104e0SAlexander V. Chernikov /* 1350089104e0SAlexander V. Chernikov * {{nla_len=24, nla_type=IFLA_LINKINFO}, 1351089104e0SAlexander V. Chernikov * [ 1352089104e0SAlexander V. Chernikov * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...}, 1353089104e0SAlexander V. Chernikov * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]} 1354089104e0SAlexander V. Chernikov */ 1355089104e0SAlexander V. Chernikov static void 1356089104e0SAlexander V. Chernikov vlan_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw) 1357089104e0SAlexander V. Chernikov { 1358089104e0SAlexander V. Chernikov uint32_t parent_index = 0; 1359089104e0SAlexander V. Chernikov uint16_t vlan_id = 0; 1360089104e0SAlexander V. Chernikov uint16_t vlan_proto = 0; 1361089104e0SAlexander V. Chernikov 1362089104e0SAlexander V. Chernikov VLAN_SLOCK(); 1363089104e0SAlexander V. Chernikov struct ifvlan *ifv = ifp->if_softc; 1364089104e0SAlexander V. Chernikov if (TRUNK(ifv) != NULL) 1365089104e0SAlexander V. Chernikov parent_index = PARENT(ifv)->if_index; 1366089104e0SAlexander V. Chernikov vlan_id = ifv->ifv_vid; 1367089104e0SAlexander V. Chernikov vlan_proto = ifv->ifv_proto; 1368089104e0SAlexander V. Chernikov VLAN_SUNLOCK(); 1369089104e0SAlexander V. Chernikov 1370089104e0SAlexander V. Chernikov if (parent_index != 0) 1371089104e0SAlexander V. Chernikov nlattr_add_u32(nw, IFLA_LINK, parent_index); 1372089104e0SAlexander V. Chernikov 1373089104e0SAlexander V. Chernikov int off = nlattr_add_nested(nw, IFLA_LINKINFO); 1374089104e0SAlexander V. Chernikov if (off != 0) { 1375089104e0SAlexander V. Chernikov nlattr_add_string(nw, IFLA_INFO_KIND, "vlan"); 1376089104e0SAlexander V. Chernikov int off2 = nlattr_add_nested(nw, IFLA_INFO_DATA); 1377089104e0SAlexander V. Chernikov if (off2 != 0) { 1378089104e0SAlexander V. Chernikov nlattr_add_u16(nw, IFLA_VLAN_ID, vlan_id); 1379089104e0SAlexander V. Chernikov nlattr_add_u16(nw, IFLA_VLAN_PROTOCOL, vlan_proto); 1380089104e0SAlexander V. Chernikov nlattr_set_len(nw, off2); 1381089104e0SAlexander V. Chernikov } 1382089104e0SAlexander V. Chernikov nlattr_set_len(nw, off); 1383089104e0SAlexander V. Chernikov } 1384089104e0SAlexander V. Chernikov } 1385089104e0SAlexander V. Chernikov 1386f889d2efSBrooks Davis static int 138791ebcbe0SAlexander V. Chernikov vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 13889d4fe4b2SBrooks Davis { 13899d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 139053729367SAlexander V. Chernikov int unit = ifp->if_dunit; 1391c7cffd65SAlexander V. Chernikov 1392c7cffd65SAlexander V. Chernikov if (ifp->if_vlantrunk) 1393c7cffd65SAlexander V. Chernikov return (EBUSY); 1394b4e9f837SBrooks Davis 13952e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 13962e5ff01dSLuiz Otavio O Souza IFQ_PURGE(&ifp->if_snd); 13972e5ff01dSLuiz Otavio O Souza #endif 1398249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 1399249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 1400d148c2a2SMatt Joras /* 1401d148c2a2SMatt Joras * We should have the only reference to the ifv now, so we can now 1402d148c2a2SMatt Joras * drain any remaining lladdr task before freeing the ifnet and the 1403d148c2a2SMatt Joras * ifvlan. 1404d148c2a2SMatt Joras */ 1405d148c2a2SMatt Joras taskqueue_drain(taskqueue_thread, &ifv->lladdr_task); 1406b08d611dSMatt Macy NET_EPOCH_WAIT(); 14074b22573aSBrooks Davis if_free(ifp); 14089d4fe4b2SBrooks Davis free(ifv, M_VLAN); 140953729367SAlexander V. Chernikov if (unit != IF_DUNIT_NONE) 141053729367SAlexander V. Chernikov ifc_free_unit(ifc, unit); 1411b4e9f837SBrooks Davis 1412f889d2efSBrooks Davis return (0); 14139d4fe4b2SBrooks Davis } 14149d4fe4b2SBrooks Davis 141515a66c21SBruce M Simpson /* 141615a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 141715a66c21SBruce M Simpson */ 14182cc2df49SGarrett Wollman static void 1419114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 14202cc2df49SGarrett Wollman { 14212cc2df49SGarrett Wollman } 14222cc2df49SGarrett Wollman 14236d3a3ab7SGleb Smirnoff /* 1424d9b1d615SJohn Baldwin * The if_transmit method for vlan(4) interface. 14256d3a3ab7SGleb Smirnoff */ 1426d9b1d615SJohn Baldwin static int 1427d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m) 14282cc2df49SGarrett Wollman { 14292cc2df49SGarrett Wollman struct ifvlan *ifv; 14302cc2df49SGarrett Wollman struct ifnet *p; 14311ad7a257SPyun YongHyeon int error, len, mcast; 14322cc2df49SGarrett Wollman 1433b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1434b8a6e03fSGleb Smirnoff 14352cc2df49SGarrett Wollman ifv = ifp->if_softc; 1436d148c2a2SMatt Joras if (TRUNK(ifv) == NULL) { 1437d148c2a2SMatt Joras if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1438d148c2a2SMatt Joras m_freem(m); 1439d148c2a2SMatt Joras return (ENETDOWN); 1440d148c2a2SMatt Joras } 144175ee267cSGleb Smirnoff p = PARENT(ifv); 14421ad7a257SPyun YongHyeon len = m->m_pkthdr.len; 14431ad7a257SPyun YongHyeon mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 14442cc2df49SGarrett Wollman 1445a3814acfSSam Leffler BPF_MTAP(ifp, m); 14462cc2df49SGarrett Wollman 1447b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1448fb3bc596SJohn Baldwin if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) { 1449fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 1450fb3bc596SJohn Baldwin struct m_snd_tag *mst; 1451fb3bc596SJohn Baldwin 1452fb3bc596SJohn Baldwin MPASS(m->m_pkthdr.snd_tag->ifp == ifp); 1453fb3bc596SJohn Baldwin mst = m->m_pkthdr.snd_tag; 1454fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 1455fb3bc596SJohn Baldwin if (vst->tag->ifp != p) { 1456fb3bc596SJohn Baldwin if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1457fb3bc596SJohn Baldwin m_freem(m); 1458fb3bc596SJohn Baldwin return (EAGAIN); 1459fb3bc596SJohn Baldwin } 1460fb3bc596SJohn Baldwin 1461fb3bc596SJohn Baldwin m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag); 1462fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 1463fb3bc596SJohn Baldwin } 1464fb3bc596SJohn Baldwin #endif 1465fb3bc596SJohn Baldwin 1466f731f104SBill Paul /* 1467d9b1d615SJohn Baldwin * Do not run parent's if_transmit() if the parent is not up, 146824993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 146924993214SYaroslav Tykhiy */ 14702dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 1471a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1472d148c2a2SMatt Joras m_freem(m); 14738b20f6cfSHiroki Sato return (ENETDOWN); 147424993214SYaroslav Tykhiy } 147524993214SYaroslav Tykhiy 1476c7cffd65SAlexander V. Chernikov if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) { 1477a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1478d9b1d615SJohn Baldwin return (0); 14794af90a4dSMatthew N. Dodd } 14802cc2df49SGarrett Wollman 14812cc2df49SGarrett Wollman /* 14822cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 14832cc2df49SGarrett Wollman */ 1484aea78d20SKip Macy error = (p->if_transmit)(p, m); 1485299153b5SAlexander V. Chernikov if (error == 0) { 1486a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1487a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 1488a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast); 14891ad7a257SPyun YongHyeon } else 1490a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1491d9b1d615SJohn Baldwin return (error); 14922cc2df49SGarrett Wollman } 1493d9b1d615SJohn Baldwin 149416cf6bdbSMatt Joras static int 149516cf6bdbSMatt Joras vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 149616cf6bdbSMatt Joras struct route *ro) 149716cf6bdbSMatt Joras { 149816cf6bdbSMatt Joras struct ifvlan *ifv; 149916cf6bdbSMatt Joras struct ifnet *p; 150016cf6bdbSMatt Joras 1501b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1502b8a6e03fSGleb Smirnoff 1503c7cffd65SAlexander V. Chernikov /* 1504c7cffd65SAlexander V. Chernikov * Find the first non-VLAN parent interface. 1505c7cffd65SAlexander V. Chernikov */ 150616cf6bdbSMatt Joras ifv = ifp->if_softc; 1507c7cffd65SAlexander V. Chernikov do { 150816cf6bdbSMatt Joras if (TRUNK(ifv) == NULL) { 150916cf6bdbSMatt Joras m_freem(m); 151016cf6bdbSMatt Joras return (ENETDOWN); 151116cf6bdbSMatt Joras } 151216cf6bdbSMatt Joras p = PARENT(ifv); 1513c7cffd65SAlexander V. Chernikov ifv = p->if_softc; 1514c7cffd65SAlexander V. Chernikov } while (p->if_type == IFT_L2VLAN); 1515c7cffd65SAlexander V. Chernikov 151616cf6bdbSMatt Joras return p->if_output(ifp, m, dst, ro); 151716cf6bdbSMatt Joras } 151816cf6bdbSMatt Joras 15192e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 15202e5ff01dSLuiz Otavio O Souza static void 15212e5ff01dSLuiz Otavio O Souza vlan_altq_start(if_t ifp) 15222e5ff01dSLuiz Otavio O Souza { 15232e5ff01dSLuiz Otavio O Souza struct ifaltq *ifq = &ifp->if_snd; 15242e5ff01dSLuiz Otavio O Souza struct mbuf *m; 15252e5ff01dSLuiz Otavio O Souza 15262e5ff01dSLuiz Otavio O Souza IFQ_LOCK(ifq); 15272e5ff01dSLuiz Otavio O Souza IFQ_DEQUEUE_NOLOCK(ifq, m); 15282e5ff01dSLuiz Otavio O Souza while (m != NULL) { 15292e5ff01dSLuiz Otavio O Souza vlan_transmit(ifp, m); 15302e5ff01dSLuiz Otavio O Souza IFQ_DEQUEUE_NOLOCK(ifq, m); 15312e5ff01dSLuiz Otavio O Souza } 15322e5ff01dSLuiz Otavio O Souza IFQ_UNLOCK(ifq); 15332e5ff01dSLuiz Otavio O Souza } 15342e5ff01dSLuiz Otavio O Souza 15352e5ff01dSLuiz Otavio O Souza static int 15362e5ff01dSLuiz Otavio O Souza vlan_altq_transmit(if_t ifp, struct mbuf *m) 15372e5ff01dSLuiz Otavio O Souza { 15382e5ff01dSLuiz Otavio O Souza int err; 15392e5ff01dSLuiz Otavio O Souza 15402e5ff01dSLuiz Otavio O Souza if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 15412e5ff01dSLuiz Otavio O Souza IFQ_ENQUEUE(&ifp->if_snd, m, err); 15422e5ff01dSLuiz Otavio O Souza if (err == 0) 15432e5ff01dSLuiz Otavio O Souza vlan_altq_start(ifp); 15442e5ff01dSLuiz Otavio O Souza } else 15452e5ff01dSLuiz Otavio O Souza err = vlan_transmit(ifp, m); 15462e5ff01dSLuiz Otavio O Souza 15472e5ff01dSLuiz Otavio O Souza return (err); 15482e5ff01dSLuiz Otavio O Souza } 15492e5ff01dSLuiz Otavio O Souza #endif /* ALTQ */ 15502e5ff01dSLuiz Otavio O Souza 1551d9b1d615SJohn Baldwin /* 1552d9b1d615SJohn Baldwin * The ifp->if_qflush entry point for vlan(4) is a no-op. 1553d9b1d615SJohn Baldwin */ 1554d9b1d615SJohn Baldwin static void 1555d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused) 1556d9b1d615SJohn Baldwin { 1557f731f104SBill Paul } 1558f731f104SBill Paul 1559a3814acfSSam Leffler static void 1560a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 1561f731f104SBill Paul { 1562d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1563f731f104SBill Paul struct ifvlan *ifv; 15642ccbbd06SMarcelo Araujo struct m_tag *mtag; 15652ccbbd06SMarcelo Araujo uint16_t vid, tag; 156675ee267cSGleb Smirnoff 1567b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1568b8a6e03fSGleb Smirnoff 1569d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1570d148c2a2SMatt Joras if (trunk == NULL) { 1571d148c2a2SMatt Joras m_freem(m); 1572d148c2a2SMatt Joras return; 1573d148c2a2SMatt Joras } 1574a3814acfSSam Leffler 1575f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 1576a3814acfSSam Leffler /* 157714e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 1578a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 1579a3814acfSSam Leffler */ 15802ccbbd06SMarcelo Araujo tag = m->m_pkthdr.ether_vtag; 15816ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 1582a3814acfSSam Leffler } else { 158375ee267cSGleb Smirnoff struct ether_vlan_header *evl; 158475ee267cSGleb Smirnoff 158514e98256SYaroslav Tykhiy /* 158614e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 158714e98256SYaroslav Tykhiy */ 1588a3814acfSSam Leffler switch (ifp->if_type) { 1589a3814acfSSam Leffler case IFT_ETHER: 1590a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 1591a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 1592a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 1593a3814acfSSam Leffler return; 1594a3814acfSSam Leffler } 1595a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 15962ccbbd06SMarcelo Araujo tag = ntohs(evl->evl_tag); 1597db8b5973SYaroslav Tykhiy 1598db8b5973SYaroslav Tykhiy /* 15992dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 16002dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 16012dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 16022dc879b3SYaroslav Tykhiy * type field is already in place. 1603db8b5973SYaroslav Tykhiy */ 16042dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 16052dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 16062dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 1607a3814acfSSam Leffler break; 16082dc879b3SYaroslav Tykhiy 1609a3814acfSSam Leffler default: 1610db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 161160c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 161260c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 1613a3814acfSSam Leffler #endif 16143751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1615d148c2a2SMatt Joras m_freem(m); 161660c60618SYaroslav Tykhiy return; 1617a3814acfSSam Leffler } 16187a46ec8fSBrooks Davis } 16197a46ec8fSBrooks Davis 16202ccbbd06SMarcelo Araujo vid = EVL_VLANOFTAG(tag); 16212ccbbd06SMarcelo Araujo 16227983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 16232dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 1624b08d611dSMatt Macy if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1625d148c2a2SMatt Joras m_freem(m); 162675ee267cSGleb Smirnoff return; 162775ee267cSGleb Smirnoff } 1628f731f104SBill Paul 162978bc3d5eSKristof Provost if (V_vlan_mtag_pcp) { 16302ccbbd06SMarcelo Araujo /* 16312ccbbd06SMarcelo Araujo * While uncommon, it is possible that we will find a 802.1q 16322ccbbd06SMarcelo Araujo * packet encapsulated inside another packet that also had an 16332ccbbd06SMarcelo Araujo * 802.1q header. For example, ethernet tunneled over IPSEC 16342ccbbd06SMarcelo Araujo * arriving over ethernet. In that case, we replace the 16352ccbbd06SMarcelo Araujo * existing 802.1q PCP m_tag value. 16362ccbbd06SMarcelo Araujo */ 16372ccbbd06SMarcelo Araujo mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 16382ccbbd06SMarcelo Araujo if (mtag == NULL) { 16392ccbbd06SMarcelo Araujo mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN, 16402ccbbd06SMarcelo Araujo sizeof(uint8_t), M_NOWAIT); 16412ccbbd06SMarcelo Araujo if (mtag == NULL) { 16422ccbbd06SMarcelo Araujo if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1643d148c2a2SMatt Joras m_freem(m); 16442ccbbd06SMarcelo Araujo return; 16452ccbbd06SMarcelo Araujo } 16462ccbbd06SMarcelo Araujo m_tag_prepend(m, mtag); 16472ccbbd06SMarcelo Araujo } 16482ccbbd06SMarcelo Araujo *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag); 16492ccbbd06SMarcelo Araujo } 16502ccbbd06SMarcelo Araujo 1651fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 1652c0304424SGleb Smirnoff if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1); 16532cc2df49SGarrett Wollman 1654a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 1655fdbf1174SMatt Joras (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m); 16562cc2df49SGarrett Wollman } 16572cc2df49SGarrett Wollman 1658d148c2a2SMatt Joras static void 1659d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused) 1660d148c2a2SMatt Joras { 1661d148c2a2SMatt Joras struct ifvlan *ifv; 1662d148c2a2SMatt Joras struct ifnet *ifp; 1663d148c2a2SMatt Joras 1664d148c2a2SMatt Joras ifv = (struct ifvlan *)arg; 1665d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 16665191a3aeSKristof Provost 16675191a3aeSKristof Provost CURVNET_SET(ifp->if_vnet); 16685191a3aeSKristof Provost 1669d148c2a2SMatt Joras /* The ifv_ifp already has the lladdr copied in. */ 1670d148c2a2SMatt Joras if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen); 16715191a3aeSKristof Provost 16725191a3aeSKristof Provost CURVNET_RESTORE(); 1673d148c2a2SMatt Joras } 1674d148c2a2SMatt Joras 16752cc2df49SGarrett Wollman static int 1676c7cffd65SAlexander V. Chernikov vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid, 1677c7cffd65SAlexander V. Chernikov uint16_t proto) 16782cc2df49SGarrett Wollman { 16796dcec895SGleb Smirnoff struct epoch_tracker et; 168075ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 16811cf236fbSYaroslav Tykhiy struct ifnet *ifp; 168275ee267cSGleb Smirnoff int error = 0; 16832cc2df49SGarrett Wollman 1684b1828acfSGleb Smirnoff /* 1685b1828acfSGleb Smirnoff * We can handle non-ethernet hardware types as long as 1686b1828acfSGleb Smirnoff * they handle the tagging and headers themselves. 1687b1828acfSGleb Smirnoff */ 1688e4cd31ddSJeff Roberson if (p->if_type != IFT_ETHER && 1689c7cffd65SAlexander V. Chernikov p->if_type != IFT_L2VLAN && 1690e4cd31ddSJeff Roberson (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 169115a66c21SBruce M Simpson return (EPROTONOSUPPORT); 169264a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 169364a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 1694b1828acfSGleb Smirnoff /* 1695b1828acfSGleb Smirnoff * Don't let the caller set up a VLAN VID with 1696b1828acfSGleb Smirnoff * anything except VLID bits. 1697b1828acfSGleb Smirnoff * VID numbers 0x0 and 0xFFF are reserved. 1698b1828acfSGleb Smirnoff */ 1699b1828acfSGleb Smirnoff if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK)) 1700b1828acfSGleb Smirnoff return (EINVAL); 1701663f556bSKristof Provost if (ifv->ifv_trunk) { 1702663f556bSKristof Provost trunk = ifv->ifv_trunk; 1703663f556bSKristof Provost if (trunk->parent != p) 170415a66c21SBruce M Simpson return (EBUSY); 17052cc2df49SGarrett Wollman 1706d148c2a2SMatt Joras VLAN_XLOCK(); 1707663f556bSKristof Provost 1708663f556bSKristof Provost ifv->ifv_proto = proto; 1709663f556bSKristof Provost 1710663f556bSKristof Provost if (ifv->ifv_vid != vid) { 1711bdd12889SKristof Provost int oldvid = ifv->ifv_vid; 1712bdd12889SKristof Provost 1713663f556bSKristof Provost /* Re-hash */ 1714663f556bSKristof Provost vlan_remhash(trunk, ifv); 1715663f556bSKristof Provost ifv->ifv_vid = vid; 1716663f556bSKristof Provost error = vlan_inshash(trunk, ifv); 1717bdd12889SKristof Provost if (error) { 1718bdd12889SKristof Provost int ret __diagused; 1719bdd12889SKristof Provost 1720bdd12889SKristof Provost ifv->ifv_vid = oldvid; 1721bdd12889SKristof Provost /* Re-insert back where we found it. */ 1722bdd12889SKristof Provost ret = vlan_inshash(trunk, ifv); 1723bdd12889SKristof Provost MPASS(ret == 0); 1724bdd12889SKristof Provost } 1725663f556bSKristof Provost } 1726663f556bSKristof Provost /* Will unlock */ 1727663f556bSKristof Provost goto done; 1728663f556bSKristof Provost } 1729663f556bSKristof Provost 1730663f556bSKristof Provost VLAN_XLOCK(); 173175ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 173275ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 173375ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 173475ee267cSGleb Smirnoff vlan_inithash(trunk); 173575ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 1736d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 173775ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 173875ee267cSGleb Smirnoff trunk->parent = p; 17399bcf3ae4SAlexander Motin if_ref(trunk->parent); 1740b08d611dSMatt Macy TRUNK_WUNLOCK(trunk); 174175ee267cSGleb Smirnoff } else { 174275ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 174375ee267cSGleb Smirnoff } 174475ee267cSGleb Smirnoff 17457983103aSRobert Watson ifv->ifv_vid = vid; /* must set this before vlan_inshash() */ 17462ccbbd06SMarcelo Araujo ifv->ifv_pcp = 0; /* Default: best effort delivery. */ 174775ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 174875ee267cSGleb Smirnoff if (error) 174975ee267cSGleb Smirnoff goto done; 1750c7cffd65SAlexander V. Chernikov ifv->ifv_proto = proto; 1751a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1752a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 17531cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1754d89baa5aSAlexander Motin ifv->ifv_capenable = -1; 175584abf7e2SKonstantin Belousov ifv->ifv_capenable2 = -1; 1756a3814acfSSam Leffler 1757a3814acfSSam Leffler /* 1758a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1759a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1760656acce4SYaroslav Tykhiy * use it. 1761a3814acfSSam Leffler */ 1762656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1763656acce4SYaroslav Tykhiy /* 1764656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1765656acce4SYaroslav Tykhiy * handle extended frames. 1766656acce4SYaroslav Tykhiy */ 1767a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1768656acce4SYaroslav Tykhiy } else { 1769a3814acfSSam Leffler /* 1770a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1771a3814acfSSam Leffler * makes us incompatible with strictly compliant 1772a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1773a3814acfSSam Leffler * the feature with other NetBSD implementations, 1774a3814acfSSam Leffler * which might still be useful. 1775a3814acfSSam Leffler */ 1776a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1777a3814acfSSam Leffler } 1778a3814acfSSam Leffler 177975ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 17801cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 1781e4cd31ddSJeff Roberson /* 1782e4cd31ddSJeff Roberson * Initialize fields from our parent. This duplicates some 1783e4cd31ddSJeff Roberson * work with ether_ifattach() but allows for non-ethernet 1784e4cd31ddSJeff Roberson * interfaces to also work. 1785e4cd31ddSJeff Roberson */ 17861cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 178775ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 1788e4cd31ddSJeff Roberson ifp->if_input = p->if_input; 1789e4cd31ddSJeff Roberson ifp->if_resolvemulti = p->if_resolvemulti; 1790e4cd31ddSJeff Roberson ifp->if_addrlen = p->if_addrlen; 1791e4cd31ddSJeff Roberson ifp->if_broadcastaddr = p->if_broadcastaddr; 179232a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 1793e4cd31ddSJeff Roberson 17942cc2df49SGarrett Wollman /* 179516cf6bdbSMatt Joras * We wrap the parent's if_output using vlan_output to ensure that it 179616cf6bdbSMatt Joras * can't become stale. 179716cf6bdbSMatt Joras */ 179816cf6bdbSMatt Joras ifp->if_output = vlan_output; 179916cf6bdbSMatt Joras 180016cf6bdbSMatt Joras /* 180124993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 180224993214SYaroslav Tykhiy * Other flags are none of our business. 18032cc2df49SGarrett Wollman */ 180464a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 18051cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 18061cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 18071cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 18081cf236fbSYaroslav Tykhiy 18091cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 18102cc2df49SGarrett Wollman 18116dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 181275ee267cSGleb Smirnoff vlan_capabilities(ifv); 18136dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 1814a3814acfSSam Leffler 1815a3814acfSSam Leffler /* 1816e4cd31ddSJeff Roberson * Set up our interface address to reflect the underlying 18172cc2df49SGarrett Wollman * physical interface's. 18182cc2df49SGarrett Wollman */ 1819a961401eSAndrey V. Elsukov TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv); 1820e4cd31ddSJeff Roberson ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1821e4cd31ddSJeff Roberson p->if_addrlen; 18221b2a4f7aSBill Fenner 1823a961401eSAndrey V. Elsukov /* 1824a961401eSAndrey V. Elsukov * Do not schedule link address update if it was the same 1825a961401eSAndrey V. Elsukov * as previous parent's. This helps avoid updating for each 1826a961401eSAndrey V. Elsukov * associated llentry. 1827a961401eSAndrey V. Elsukov */ 1828a961401eSAndrey V. Elsukov if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) { 1829a961401eSAndrey V. Elsukov bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1830a961401eSAndrey V. Elsukov taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 1831a961401eSAndrey V. Elsukov } 18322ada9747SYaroslav Tykhiy 18332ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 18342ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 1835d148c2a2SMatt Joras 1836d148c2a2SMatt Joras /* Update flags on the parent, if necessary. */ 1837d148c2a2SMatt Joras vlan_setflags(ifp, 1); 1838b08d611dSMatt Macy 1839d148c2a2SMatt Joras /* 1840b08d611dSMatt Macy * Configure multicast addresses that may already be 1841b08d611dSMatt Macy * joined on the vlan device. 1842d148c2a2SMatt Joras */ 1843b08d611dSMatt Macy (void)vlan_setmulti(ifp); 1844b08d611dSMatt Macy 1845b08d611dSMatt Macy done: 1846c725524cSJack F Vogel if (error == 0) 18477983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid); 1848d148c2a2SMatt Joras VLAN_XUNLOCK(); 184975ee267cSGleb Smirnoff 185075ee267cSGleb Smirnoff return (error); 18512cc2df49SGarrett Wollman } 18522cc2df49SGarrett Wollman 18536f359e28SJohn Baldwin static void 1854f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1855f731f104SBill Paul { 18565cb8c31aSYaroslav Tykhiy 1857d148c2a2SMatt Joras VLAN_XLOCK(); 185828cc4d37SJohn Baldwin vlan_unconfig_locked(ifp, 0); 1859d148c2a2SMatt Joras VLAN_XUNLOCK(); 18605cb8c31aSYaroslav Tykhiy } 18615cb8c31aSYaroslav Tykhiy 18626f359e28SJohn Baldwin static void 186328cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing) 18645cb8c31aSYaroslav Tykhiy { 186575ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1866f731f104SBill Paul struct vlan_mc_entry *mc; 1867f731f104SBill Paul struct ifvlan *ifv; 1868c725524cSJack F Vogel struct ifnet *parent; 186928cc4d37SJohn Baldwin int error; 1870f731f104SBill Paul 1871d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 18724faedfe8SSam Leffler 1873f731f104SBill Paul ifv = ifp->if_softc; 187475ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 187522893351SJack F Vogel parent = NULL; 1876f731f104SBill Paul 187722893351SJack F Vogel if (trunk != NULL) { 187822893351SJack F Vogel parent = trunk->parent; 18791b2a4f7aSBill Fenner 1880f731f104SBill Paul /* 1881f731f104SBill Paul * Since the interface is being unconfigured, we need to 1882f731f104SBill Paul * empty the list of multicast groups that we may have joined 18831b2a4f7aSBill Fenner * while we were alive from the parent's list. 1884f731f104SBill Paul */ 1885b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 18866f359e28SJohn Baldwin /* 188728cc4d37SJohn Baldwin * If the parent interface is being detached, 1888b90dde2fSJohn Baldwin * all its multicast addresses have already 188928cc4d37SJohn Baldwin * been removed. Warn about errors if 189028cc4d37SJohn Baldwin * if_delmulti() does fail, but don't abort as 189128cc4d37SJohn Baldwin * all callers expect vlan destruction to 189228cc4d37SJohn Baldwin * succeed. 18936f359e28SJohn Baldwin */ 189428cc4d37SJohn Baldwin if (!departing) { 189528cc4d37SJohn Baldwin error = if_delmulti(parent, 1896e4cd31ddSJeff Roberson (struct sockaddr *)&mc->mc_addr); 189728cc4d37SJohn Baldwin if (error) 189828cc4d37SJohn Baldwin if_printf(ifp, 189928cc4d37SJohn Baldwin "Failed to delete multicast address from parent: %d\n", 190028cc4d37SJohn Baldwin error); 190128cc4d37SJohn Baldwin } 1902b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 19032a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 1904f731f104SBill Paul } 1905a3814acfSSam Leffler 19061cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 1907d148c2a2SMatt Joras 190875ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 190975ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 191075ee267cSGleb Smirnoff 191175ee267cSGleb Smirnoff /* 191275ee267cSGleb Smirnoff * Check if we were the last. 191375ee267cSGleb Smirnoff */ 191475ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 19152d222cb7SAlexander Motin parent->if_vlantrunk = NULL; 1916b08d611dSMatt Macy NET_EPOCH_WAIT(); 191775ee267cSGleb Smirnoff trunk_destroy(trunk); 1918d148c2a2SMatt Joras } 19191b2a4f7aSBill Fenner } 1920f731f104SBill Paul 1921f731f104SBill Paul /* Disconnect from parent. */ 19221cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 19231cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 19245cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 19255cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 19265cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1927f731f104SBill Paul 192822893351SJack F Vogel /* 192922893351SJack F Vogel * Only dispatch an event if vlan was 193022893351SJack F Vogel * attached, otherwise there is nothing 193122893351SJack F Vogel * to cleanup anyway. 193222893351SJack F Vogel */ 193322893351SJack F Vogel if (parent != NULL) 19347983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid); 1935f731f104SBill Paul } 1936f731f104SBill Paul 19371cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1938f731f104SBill Paul static int 19391cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 19401cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1941a3814acfSSam Leffler { 19421cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 19431cf236fbSYaroslav Tykhiy int error; 1944a3814acfSSam Leffler 1945d148c2a2SMatt Joras VLAN_SXLOCK_ASSERT(); 1946a3814acfSSam Leffler 19471cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 19481cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 19491cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 19501cf236fbSYaroslav Tykhiy 19511cf236fbSYaroslav Tykhiy /* 19521cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 19531cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 19541cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 19551cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 19561cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 19571cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 19581cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 19591cf236fbSYaroslav Tykhiy */ 19601cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 196175ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 19621cf236fbSYaroslav Tykhiy if (error) 1963a3814acfSSam Leffler return (error); 19641cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 19651cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 19661cf236fbSYaroslav Tykhiy } 19671cf236fbSYaroslav Tykhiy return (0); 19681cf236fbSYaroslav Tykhiy } 19691cf236fbSYaroslav Tykhiy 19701cf236fbSYaroslav Tykhiy /* 19711cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 19721cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 19731cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 19741cf236fbSYaroslav Tykhiy */ 19751cf236fbSYaroslav Tykhiy static int 19761cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 19771cf236fbSYaroslav Tykhiy { 19781cf236fbSYaroslav Tykhiy int error, i; 19791cf236fbSYaroslav Tykhiy 19801cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 19811cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 19821cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 19831cf236fbSYaroslav Tykhiy if (error) 19841cf236fbSYaroslav Tykhiy return (error); 19851cf236fbSYaroslav Tykhiy } 19861cf236fbSYaroslav Tykhiy return (0); 1987a3814acfSSam Leffler } 1988a3814acfSSam Leffler 1989127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1990127d7b2dSAndre Oppermann static void 1991a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp) 1992127d7b2dSAndre Oppermann { 1993b2807792SGleb Smirnoff struct epoch_tracker et; 1994d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1995127d7b2dSAndre Oppermann struct ifvlan *ifv; 1996127d7b2dSAndre Oppermann 1997b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 1998d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1999b2807792SGleb Smirnoff if (trunk == NULL) { 2000b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 2001d148c2a2SMatt Joras return; 2002b2807792SGleb Smirnoff } 2003d148c2a2SMatt Joras 2004d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 2005d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 2006aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 2007fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 200875ee267cSGleb Smirnoff trunk->parent->if_link_state); 2009127d7b2dSAndre Oppermann } 2010d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 2011b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 201275ee267cSGleb Smirnoff } 201375ee267cSGleb Smirnoff 201484abf7e2SKonstantin Belousov #ifdef IPSEC_OFFLOAD 201584abf7e2SKonstantin Belousov #define VLAN_IPSEC_METHOD(exp) \ 201684abf7e2SKonstantin Belousov if_t p; \ 201784abf7e2SKonstantin Belousov struct ifvlan *ifv; \ 201884abf7e2SKonstantin Belousov int error; \ 201984abf7e2SKonstantin Belousov \ 202084abf7e2SKonstantin Belousov ifv = ifp->if_softc; \ 202184abf7e2SKonstantin Belousov VLAN_SLOCK(); \ 202284abf7e2SKonstantin Belousov if (TRUNK(ifv) != NULL) { \ 202384abf7e2SKonstantin Belousov p = PARENT(ifv); \ 202484abf7e2SKonstantin Belousov if_ref(p); \ 202584abf7e2SKonstantin Belousov error = p->if_ipsec_accel_m->exp; \ 202684abf7e2SKonstantin Belousov if_rele(p); \ 202784abf7e2SKonstantin Belousov } else { \ 202884abf7e2SKonstantin Belousov error = ENXIO; \ 202984abf7e2SKonstantin Belousov } \ 203084abf7e2SKonstantin Belousov VLAN_SUNLOCK(); \ 203184abf7e2SKonstantin Belousov return (error); 203284abf7e2SKonstantin Belousov 203384abf7e2SKonstantin Belousov 203484abf7e2SKonstantin Belousov static int 203584abf7e2SKonstantin Belousov vlan_if_spdadd(if_t ifp, void *sp, void *inp, void **priv) 203684abf7e2SKonstantin Belousov { 203784abf7e2SKonstantin Belousov VLAN_IPSEC_METHOD(if_spdadd(ifp, sp, inp, priv)); 203884abf7e2SKonstantin Belousov } 203984abf7e2SKonstantin Belousov 204084abf7e2SKonstantin Belousov static int 204184abf7e2SKonstantin Belousov vlan_if_spddel(if_t ifp, void *sp, void *priv) 204284abf7e2SKonstantin Belousov { 204384abf7e2SKonstantin Belousov VLAN_IPSEC_METHOD(if_spddel(ifp, sp, priv)); 204484abf7e2SKonstantin Belousov } 204584abf7e2SKonstantin Belousov 204684abf7e2SKonstantin Belousov static int 204784abf7e2SKonstantin Belousov vlan_if_sa_newkey(if_t ifp, void *sav, u_int drv_spi, void **privp) 204884abf7e2SKonstantin Belousov { 204984abf7e2SKonstantin Belousov VLAN_IPSEC_METHOD(if_sa_newkey(ifp, sav, drv_spi, privp)); 205084abf7e2SKonstantin Belousov } 205184abf7e2SKonstantin Belousov 205284abf7e2SKonstantin Belousov static int 205384abf7e2SKonstantin Belousov vlan_if_sa_deinstall(if_t ifp, u_int drv_spi, void *priv) 205484abf7e2SKonstantin Belousov { 205584abf7e2SKonstantin Belousov VLAN_IPSEC_METHOD(if_sa_deinstall(ifp, drv_spi, priv)); 205684abf7e2SKonstantin Belousov } 205784abf7e2SKonstantin Belousov 205884abf7e2SKonstantin Belousov static int 205984abf7e2SKonstantin Belousov vlan_if_sa_cnt(if_t ifp, void *sa, uint32_t drv_spi, void *priv, 206084abf7e2SKonstantin Belousov struct seclifetime *lt) 206184abf7e2SKonstantin Belousov { 206284abf7e2SKonstantin Belousov VLAN_IPSEC_METHOD(if_sa_cnt(ifp, sa, drv_spi, priv, lt)); 206384abf7e2SKonstantin Belousov } 206484abf7e2SKonstantin Belousov 206584abf7e2SKonstantin Belousov static int 206684abf7e2SKonstantin Belousov vlan_if_ipsec_hwassist(if_t ifp, void *sav, u_int drv_spi,void *priv) 206784abf7e2SKonstantin Belousov { 206884abf7e2SKonstantin Belousov if_t trunk; 206984abf7e2SKonstantin Belousov 207084abf7e2SKonstantin Belousov NET_EPOCH_ASSERT(); 207184abf7e2SKonstantin Belousov trunk = vlan_trunkdev(ifp); 207284abf7e2SKonstantin Belousov if (trunk == NULL) 207384abf7e2SKonstantin Belousov return (0); 207484abf7e2SKonstantin Belousov return (trunk->if_ipsec_accel_m->if_hwassist(trunk, sav, 207584abf7e2SKonstantin Belousov drv_spi, priv)); 207684abf7e2SKonstantin Belousov } 207784abf7e2SKonstantin Belousov 207884abf7e2SKonstantin Belousov static const struct if_ipsec_accel_methods vlan_if_ipsec_accel_methods = { 207984abf7e2SKonstantin Belousov .if_spdadd = vlan_if_spdadd, 208084abf7e2SKonstantin Belousov .if_spddel = vlan_if_spddel, 208184abf7e2SKonstantin Belousov .if_sa_newkey = vlan_if_sa_newkey, 208284abf7e2SKonstantin Belousov .if_sa_deinstall = vlan_if_sa_deinstall, 208384abf7e2SKonstantin Belousov .if_sa_cnt = vlan_if_sa_cnt, 208484abf7e2SKonstantin Belousov .if_hwassist = vlan_if_ipsec_hwassist, 208584abf7e2SKonstantin Belousov }; 208684abf7e2SKonstantin Belousov 208784abf7e2SKonstantin Belousov #undef VLAN_IPSEC_METHOD 208884abf7e2SKonstantin Belousov #endif /* IPSEC_OFFLOAD */ 208984abf7e2SKonstantin Belousov 209075ee267cSGleb Smirnoff static void 209175ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 209275ee267cSGleb Smirnoff { 2093d148c2a2SMatt Joras struct ifnet *p; 2094d148c2a2SMatt Joras struct ifnet *ifp; 20959fd573c3SHans Petter Selasky struct ifnet_hw_tsomax hw_tsomax; 209684abf7e2SKonstantin Belousov int cap = 0, ena = 0, mena, cap2 = 0, ena2 = 0; 209784abf7e2SKonstantin Belousov int mena2 __unused; 2098d89baa5aSAlexander Motin u_long hwa = 0; 209975ee267cSGleb Smirnoff 2100a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 2101b8a6e03fSGleb Smirnoff VLAN_SXLOCK_ASSERT(); 2102b8a6e03fSGleb Smirnoff 2103d148c2a2SMatt Joras p = PARENT(ifv); 2104d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 210575ee267cSGleb Smirnoff 2106d89baa5aSAlexander Motin /* Mask parent interface enabled capabilities disabled by user. */ 2107d89baa5aSAlexander Motin mena = p->if_capenable & ifv->ifv_capenable; 210884abf7e2SKonstantin Belousov mena2 = p->if_capenable2 & ifv->ifv_capenable2; 2109d89baa5aSAlexander Motin 211075ee267cSGleb Smirnoff /* 211175ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 211275ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 211375ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 211475ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 211575ee267cSGleb Smirnoff */ 211675ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 2117d89baa5aSAlexander Motin cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 211875ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 211975ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 2120d89baa5aSAlexander Motin ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 2121d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM) 2122d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP | 2123d89baa5aSAlexander Motin CSUM_UDP | CSUM_SCTP); 2124d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM_IPV6) 2125d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_TCP_IPV6 | 2126d89baa5aSAlexander Motin CSUM_UDP_IPV6 | CSUM_SCTP_IPV6); 212775ee267cSGleb Smirnoff } 2128d89baa5aSAlexander Motin 21299b76d9cbSPyun YongHyeon /* 21309b76d9cbSPyun YongHyeon * If the parent interface can do TSO on VLANs then 21319b76d9cbSPyun YongHyeon * propagate the hardware-assisted flag. TSO on VLANs 21329b76d9cbSPyun YongHyeon * does not necessarily require hardware VLAN tagging. 21339b76d9cbSPyun YongHyeon */ 21349fd573c3SHans Petter Selasky memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 21359fd573c3SHans Petter Selasky if_hw_tsomax_common(p, &hw_tsomax); 21369fd573c3SHans Petter Selasky if_hw_tsomax_update(ifp, &hw_tsomax); 21379b76d9cbSPyun YongHyeon if (p->if_capabilities & IFCAP_VLAN_HWTSO) 2138d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TSO; 21399b76d9cbSPyun YongHyeon if (p->if_capenable & IFCAP_VLAN_HWTSO) { 2140d89baa5aSAlexander Motin ena |= mena & IFCAP_TSO; 2141d89baa5aSAlexander Motin if (ena & IFCAP_TSO) 2142d89baa5aSAlexander Motin hwa |= p->if_hwassist & CSUM_TSO; 21439b76d9cbSPyun YongHyeon } 214409fe6320SNavdeep Parhar 214509fe6320SNavdeep Parhar /* 2146fb69ed39SKristof Provost * If the parent interface can do LRO and checksum offloading on 2147fb69ed39SKristof Provost * VLANs, then guess it may do LRO on VLANs. False positive here 2148fb69ed39SKristof Provost * cost nothing, while false negative may lead to some confusions. 214959150e91SAlexander Motin */ 215059150e91SAlexander Motin if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 215159150e91SAlexander Motin cap |= p->if_capabilities & IFCAP_LRO; 215259150e91SAlexander Motin if (p->if_capenable & IFCAP_VLAN_HWCSUM) 2153b1a39c31SKevin Bowling ena |= mena & IFCAP_LRO; 215459150e91SAlexander Motin 215559150e91SAlexander Motin /* 215609fe6320SNavdeep Parhar * If the parent interface can offload TCP connections over VLANs then 215709fe6320SNavdeep Parhar * propagate its TOE capability to the VLAN interface. 215809fe6320SNavdeep Parhar * 215909fe6320SNavdeep Parhar * All TOE drivers in the tree today can deal with VLANs. If this 216009fe6320SNavdeep Parhar * changes then IFCAP_VLAN_TOE should be promoted to a full capability 216109fe6320SNavdeep Parhar * with its own bit. 216209fe6320SNavdeep Parhar */ 216309fe6320SNavdeep Parhar #define IFCAP_VLAN_TOE IFCAP_TOE 216409fe6320SNavdeep Parhar if (p->if_capabilities & IFCAP_VLAN_TOE) 2165d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TOE; 216609fe6320SNavdeep Parhar if (p->if_capenable & IFCAP_VLAN_TOE) { 2167c255d1a4SJustin Hibbits SETTOEDEV(ifp, TOEDEV(p)); 2168d89baa5aSAlexander Motin ena |= mena & IFCAP_TOE; 216909fe6320SNavdeep Parhar } 2170f3e7afe2SHans Petter Selasky 2171d89baa5aSAlexander Motin /* 2172d89baa5aSAlexander Motin * If the parent interface supports dynamic link state, so does the 2173d89baa5aSAlexander Motin * VLAN interface. 2174d89baa5aSAlexander Motin */ 2175d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_LINKSTATE); 2176d89baa5aSAlexander Motin ena |= (mena & IFCAP_LINKSTATE); 2177d89baa5aSAlexander Motin 2178f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 2179f3e7afe2SHans Petter Selasky /* 2180f3e7afe2SHans Petter Selasky * If the parent interface supports ratelimiting, so does the 2181f3e7afe2SHans Petter Selasky * VLAN interface. 2182f3e7afe2SHans Petter Selasky */ 2183d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_TXRTLMT); 2184d89baa5aSAlexander Motin ena |= (mena & IFCAP_TXRTLMT); 2185f3e7afe2SHans Petter Selasky #endif 2186d89baa5aSAlexander Motin 218766d0c056SJohn Baldwin /* 218866d0c056SJohn Baldwin * If the parent interface supports unmapped mbufs, so does 218966d0c056SJohn Baldwin * the VLAN interface. Note that this should be fine even for 219066d0c056SJohn Baldwin * interfaces that don't support hardware tagging as headers 219166d0c056SJohn Baldwin * are prepended in normal mbufs to unmapped mbufs holding 219266d0c056SJohn Baldwin * payload data. 219366d0c056SJohn Baldwin */ 21943f43ada9SGleb Smirnoff cap |= (p->if_capabilities & IFCAP_MEXTPG); 21953f43ada9SGleb Smirnoff ena |= (mena & IFCAP_MEXTPG); 219666d0c056SJohn Baldwin 2197b2e60773SJohn Baldwin /* 2198b2e60773SJohn Baldwin * If the parent interface can offload encryption and segmentation 2199b2e60773SJohn Baldwin * of TLS records over TCP, propagate it's capability to the VLAN 2200b2e60773SJohn Baldwin * interface. 2201b2e60773SJohn Baldwin * 2202b2e60773SJohn Baldwin * All TLS drivers in the tree today can deal with VLANs. If 2203b2e60773SJohn Baldwin * this ever changes, then a new IFCAP_VLAN_TXTLS can be 2204b2e60773SJohn Baldwin * defined. 2205b2e60773SJohn Baldwin */ 2206521eac97SJohn Baldwin if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 2207521eac97SJohn Baldwin cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 2208521eac97SJohn Baldwin if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 2209521eac97SJohn Baldwin ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 2210b2e60773SJohn Baldwin 2211d89baa5aSAlexander Motin ifp->if_capabilities = cap; 2212d89baa5aSAlexander Motin ifp->if_capenable = ena; 2213d89baa5aSAlexander Motin ifp->if_hwassist = hwa; 221484abf7e2SKonstantin Belousov 221584abf7e2SKonstantin Belousov #ifdef IPSEC_OFFLOAD 221684abf7e2SKonstantin Belousov cap2 |= p->if_capabilities2 & IFCAP2_BIT(IFCAP2_IPSEC_OFFLOAD); 221784abf7e2SKonstantin Belousov ena2 |= mena2 & IFCAP2_BIT(IFCAP2_IPSEC_OFFLOAD); 221884abf7e2SKonstantin Belousov ifp->if_ipsec_accel_m = &vlan_if_ipsec_accel_methods; 221984abf7e2SKonstantin Belousov #endif 2220*828445ccSKonstantin Belousov 2221*828445ccSKonstantin Belousov ifp->if_capabilities2 = cap2; 2222*828445ccSKonstantin Belousov ifp->if_capenable2 = ena2; 222375ee267cSGleb Smirnoff } 222475ee267cSGleb Smirnoff 222575ee267cSGleb Smirnoff static void 222675ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 222775ee267cSGleb Smirnoff { 2228b2807792SGleb Smirnoff struct epoch_tracker et; 2229d148c2a2SMatt Joras struct ifvlantrunk *trunk; 223075ee267cSGleb Smirnoff struct ifvlan *ifv; 223175ee267cSGleb Smirnoff 2232d148c2a2SMatt Joras VLAN_SLOCK(); 2233d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 2234d148c2a2SMatt Joras if (trunk == NULL) { 2235d148c2a2SMatt Joras VLAN_SUNLOCK(); 2236d148c2a2SMatt Joras return; 2237d148c2a2SMatt Joras } 2238b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 2239b8a6e03fSGleb Smirnoff VLAN_FOREACH(ifv, trunk) 224075ee267cSGleb Smirnoff vlan_capabilities(ifv); 2241b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 2242d148c2a2SMatt Joras VLAN_SUNLOCK(); 2243127d7b2dSAndre Oppermann } 2244127d7b2dSAndre Oppermann 2245a3814acfSSam Leffler static int 2246cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 22472cc2df49SGarrett Wollman { 22482cc2df49SGarrett Wollman struct ifnet *p; 22492cc2df49SGarrett Wollman struct ifreq *ifr; 22502884a936SJohn Baldwin #ifdef INET 2251e4cd31ddSJeff Roberson struct ifaddr *ifa; 22522884a936SJohn Baldwin #endif 22532cc2df49SGarrett Wollman struct ifvlan *ifv; 22542d222cb7SAlexander Motin struct ifvlantrunk *trunk; 22552cc2df49SGarrett Wollman struct vlanreq vlr; 225684becee1SAlexander Motin int error = 0, oldmtu; 22572cc2df49SGarrett Wollman 22582cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 22592884a936SJohn Baldwin #ifdef INET 2260e4cd31ddSJeff Roberson ifa = (struct ifaddr *) data; 22612884a936SJohn Baldwin #endif 22622cc2df49SGarrett Wollman ifv = ifp->if_softc; 22632cc2df49SGarrett Wollman 22642cc2df49SGarrett Wollman switch (cmd) { 2265e4cd31ddSJeff Roberson case SIOCSIFADDR: 2266e4cd31ddSJeff Roberson ifp->if_flags |= IFF_UP; 2267e4cd31ddSJeff Roberson #ifdef INET 2268e4cd31ddSJeff Roberson if (ifa->ifa_addr->sa_family == AF_INET) 2269e4cd31ddSJeff Roberson arp_ifinit(ifp, ifa); 2270e4cd31ddSJeff Roberson #endif 2271e4cd31ddSJeff Roberson break; 2272e4cd31ddSJeff Roberson case SIOCGIFADDR: 227338d958a6SBrooks Davis bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 227438d958a6SBrooks Davis ifp->if_addrlen); 2275e4cd31ddSJeff Roberson break; 2276b3cca108SBill Fenner case SIOCGIFMEDIA: 2277d148c2a2SMatt Joras VLAN_SLOCK(); 227875ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 2279d8564efdSEd Maste p = PARENT(ifv); 22809bcf3ae4SAlexander Motin if_ref(p); 2281d8564efdSEd Maste error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 22829bcf3ae4SAlexander Motin if_rele(p); 2283b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 2284b3cca108SBill Fenner if (error == 0) { 2285b3cca108SBill Fenner struct ifmediareq *ifmr; 2286b3cca108SBill Fenner 2287b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 2288b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 2289b3cca108SBill Fenner ifmr->ifm_count = 1; 2290b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 2291b3cca108SBill Fenner ifmr->ifm_ulist, 2292b3cca108SBill Fenner sizeof(int)); 2293b3cca108SBill Fenner } 2294b3cca108SBill Fenner } 22954faedfe8SSam Leffler } else { 2296b3cca108SBill Fenner error = EINVAL; 22974faedfe8SSam Leffler } 2298d148c2a2SMatt Joras VLAN_SUNLOCK(); 2299b3cca108SBill Fenner break; 2300b3cca108SBill Fenner 2301b3cca108SBill Fenner case SIOCSIFMEDIA: 2302b3cca108SBill Fenner error = EINVAL; 2303b3cca108SBill Fenner break; 2304b3cca108SBill Fenner 23052cc2df49SGarrett Wollman case SIOCSIFMTU: 23062cc2df49SGarrett Wollman /* 23072cc2df49SGarrett Wollman * Set the interface MTU. 23082cc2df49SGarrett Wollman */ 2309d148c2a2SMatt Joras VLAN_SLOCK(); 2310d148c2a2SMatt Joras trunk = TRUNK(ifv); 2311d148c2a2SMatt Joras if (trunk != NULL) { 2312d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 2313a3814acfSSam Leffler if (ifr->ifr_mtu > 231475ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 2315a3814acfSSam Leffler ifr->ifr_mtu < 2316a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 23172cc2df49SGarrett Wollman error = EINVAL; 2318a3814acfSSam Leffler else 23192cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 2320d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 2321a3814acfSSam Leffler } else 2322a3814acfSSam Leffler error = EINVAL; 2323d148c2a2SMatt Joras VLAN_SUNLOCK(); 23242cc2df49SGarrett Wollman break; 23252cc2df49SGarrett Wollman 23262cc2df49SGarrett Wollman case SIOCSETVLAN: 2327ccf7ba97SMarko Zec #ifdef VIMAGE 232815f6780eSRobert Watson /* 232915f6780eSRobert Watson * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN 233015f6780eSRobert Watson * interface to be delegated to a jail without allowing the 233115f6780eSRobert Watson * jail to change what underlying interface/VID it is 233215f6780eSRobert Watson * associated with. We are not entirely convinced that this 23335a39f779SRobert Watson * is the right way to accomplish that policy goal. 233415f6780eSRobert Watson */ 2335ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 2336ccf7ba97SMarko Zec error = EPERM; 2337ccf7ba97SMarko Zec break; 2338ccf7ba97SMarko Zec } 2339ccf7ba97SMarko Zec #endif 2340541d96aaSBrooks Davis error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr)); 23412cc2df49SGarrett Wollman if (error) 23422cc2df49SGarrett Wollman break; 23432cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 2344f731f104SBill Paul vlan_unconfig(ifp); 23452cc2df49SGarrett Wollman break; 23462cc2df49SGarrett Wollman } 23479bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 23481bdc73d3SEd Maste if (p == NULL) { 23492cc2df49SGarrett Wollman error = ENOENT; 23502cc2df49SGarrett Wollman break; 23512cc2df49SGarrett Wollman } 2352afbb64f1SAlexander V. Chernikov if (vlr.vlr_proto == 0) 2353afbb64f1SAlexander V. Chernikov vlr.vlr_proto = ETHERTYPE_VLAN; 235484becee1SAlexander Motin oldmtu = ifp->if_mtu; 2355c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto); 23569bcf3ae4SAlexander Motin if_rele(p); 235784becee1SAlexander Motin 235884becee1SAlexander Motin /* 235984becee1SAlexander Motin * VLAN MTU may change during addition of the vlandev. 236084becee1SAlexander Motin * If it did, do network layer specific procedure. 236184becee1SAlexander Motin */ 236266bdbcd5SAlexander V. Chernikov if (ifp->if_mtu != oldmtu) 236366bdbcd5SAlexander V. Chernikov if_notifymtu(ifp); 23642cc2df49SGarrett Wollman break; 23652cc2df49SGarrett Wollman 23662cc2df49SGarrett Wollman case SIOCGETVLAN: 2367ccf7ba97SMarko Zec #ifdef VIMAGE 2368ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 2369ccf7ba97SMarko Zec error = EPERM; 2370ccf7ba97SMarko Zec break; 2371ccf7ba97SMarko Zec } 2372ccf7ba97SMarko Zec #endif 237315a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 2374d148c2a2SMatt Joras VLAN_SLOCK(); 237575ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 237675ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 23779bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 23787983103aSRobert Watson vlr.vlr_tag = ifv->ifv_vid; 2379c7cffd65SAlexander V. Chernikov vlr.vlr_proto = ifv->ifv_proto; 23802cc2df49SGarrett Wollman } 2381d148c2a2SMatt Joras VLAN_SUNLOCK(); 2382541d96aaSBrooks Davis error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr)); 23832cc2df49SGarrett Wollman break; 23842cc2df49SGarrett Wollman 23852cc2df49SGarrett Wollman case SIOCSIFFLAGS: 23862cc2df49SGarrett Wollman /* 23871cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 23881cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 23892cc2df49SGarrett Wollman */ 239092c23f6dSKristof Provost VLAN_SLOCK(); 239175ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 23921cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 239392c23f6dSKristof Provost VLAN_SUNLOCK(); 23942cc2df49SGarrett Wollman break; 2395a3814acfSSam Leffler 2396f731f104SBill Paul case SIOCADDMULTI: 2397f731f104SBill Paul case SIOCDELMULTI: 239875ee267cSGleb Smirnoff /* 239975ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 240075ee267cSGleb Smirnoff * when we do. 2401d148c2a2SMatt Joras * 2402d148c2a2SMatt Joras * XXX We need the rmlock here to avoid sleeping while 2403d148c2a2SMatt Joras * holding in6_multi_mtx. 240475ee267cSGleb Smirnoff */ 2405b08d611dSMatt Macy VLAN_XLOCK(); 24062d222cb7SAlexander Motin trunk = TRUNK(ifv); 2407b08d611dSMatt Macy if (trunk != NULL) 2408f731f104SBill Paul error = vlan_setmulti(ifp); 2409b08d611dSMatt Macy VLAN_XUNLOCK(); 241075ee267cSGleb Smirnoff 2411b08d611dSMatt Macy break; 24122ccbbd06SMarcelo Araujo case SIOCGVLANPCP: 24132ccbbd06SMarcelo Araujo #ifdef VIMAGE 24142ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 24152ccbbd06SMarcelo Araujo error = EPERM; 24162ccbbd06SMarcelo Araujo break; 24172ccbbd06SMarcelo Araujo } 24182ccbbd06SMarcelo Araujo #endif 24192ccbbd06SMarcelo Araujo ifr->ifr_vlan_pcp = ifv->ifv_pcp; 24202ccbbd06SMarcelo Araujo break; 24212ccbbd06SMarcelo Araujo 24222ccbbd06SMarcelo Araujo case SIOCSVLANPCP: 24232ccbbd06SMarcelo Araujo #ifdef VIMAGE 24242ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 24252ccbbd06SMarcelo Araujo error = EPERM; 24262ccbbd06SMarcelo Araujo break; 24272ccbbd06SMarcelo Araujo } 24282ccbbd06SMarcelo Araujo #endif 24292ccbbd06SMarcelo Araujo error = priv_check(curthread, PRIV_NET_SETVLANPCP); 24302ccbbd06SMarcelo Araujo if (error) 24312ccbbd06SMarcelo Araujo break; 24329ef8cd0bSKristof Provost if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) { 24332ccbbd06SMarcelo Araujo error = EINVAL; 24342ccbbd06SMarcelo Araujo break; 24352ccbbd06SMarcelo Araujo } 24362ccbbd06SMarcelo Araujo ifv->ifv_pcp = ifr->ifr_vlan_pcp; 243732a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 24384a381a9eSHans Petter Selasky /* broadcast event about PCP change */ 24394a381a9eSHans Petter Selasky EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 24402ccbbd06SMarcelo Araujo break; 24412ccbbd06SMarcelo Araujo 2442d89baa5aSAlexander Motin case SIOCSIFCAP: 2443d148c2a2SMatt Joras VLAN_SLOCK(); 2444d89baa5aSAlexander Motin ifv->ifv_capenable = ifr->ifr_reqcap; 2445d89baa5aSAlexander Motin trunk = TRUNK(ifv); 24466dcec895SGleb Smirnoff if (trunk != NULL) { 24476dcec895SGleb Smirnoff struct epoch_tracker et; 24486dcec895SGleb Smirnoff 24496dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 2450d89baa5aSAlexander Motin vlan_capabilities(ifv); 24516dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 24526dcec895SGleb Smirnoff } 2453d148c2a2SMatt Joras VLAN_SUNLOCK(); 2454d89baa5aSAlexander Motin break; 2455d89baa5aSAlexander Motin 24562cc2df49SGarrett Wollman default: 2457e4cd31ddSJeff Roberson error = EINVAL; 2458e4cd31ddSJeff Roberson break; 24592cc2df49SGarrett Wollman } 246015a66c21SBruce M Simpson 246115a66c21SBruce M Simpson return (error); 24622cc2df49SGarrett Wollman } 2463f3e7afe2SHans Petter Selasky 2464b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 2465f3e7afe2SHans Petter Selasky static int 2466f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp, 2467f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *params, 2468f3e7afe2SHans Petter Selasky struct m_snd_tag **ppmt) 2469f3e7afe2SHans Petter Selasky { 2470fb3bc596SJohn Baldwin struct epoch_tracker et; 2471c782ea8bSJohn Baldwin const struct if_snd_tag_sw *sw; 2472fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2473fb3bc596SJohn Baldwin struct ifvlan *ifv; 2474fb3bc596SJohn Baldwin struct ifnet *parent; 2475892eded5SHans Petter Selasky struct m_snd_tag *mst; 2476fb3bc596SJohn Baldwin int error; 2477f3e7afe2SHans Petter Selasky 2478892eded5SHans Petter Selasky NET_EPOCH_ENTER(et); 2479892eded5SHans Petter Selasky ifv = ifp->if_softc; 2480892eded5SHans Petter Selasky 2481c782ea8bSJohn Baldwin switch (params->hdr.type) { 2482c782ea8bSJohn Baldwin #ifdef RATELIMIT 2483c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_UNLIMITED: 2484c782ea8bSJohn Baldwin sw = &vlan_snd_tag_ul_sw; 2485c782ea8bSJohn Baldwin break; 2486c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_RATE_LIMIT: 2487c782ea8bSJohn Baldwin sw = &vlan_snd_tag_rl_sw; 2488c782ea8bSJohn Baldwin break; 2489c782ea8bSJohn Baldwin #endif 2490c782ea8bSJohn Baldwin #ifdef KERN_TLS 2491c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_TLS: 2492c782ea8bSJohn Baldwin sw = &vlan_snd_tag_tls_sw; 2493c782ea8bSJohn Baldwin break; 2494892eded5SHans Petter Selasky case IF_SND_TAG_TYPE_TLS_RX: 2495892eded5SHans Petter Selasky sw = NULL; 2496892eded5SHans Petter Selasky if (params->tls_rx.vlan_id != 0) 2497892eded5SHans Petter Selasky goto failure; 2498892eded5SHans Petter Selasky params->tls_rx.vlan_id = ifv->ifv_vid; 2499892eded5SHans Petter Selasky break; 2500c782ea8bSJohn Baldwin #ifdef RATELIMIT 2501c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_TLS_RATE_LIMIT: 2502c782ea8bSJohn Baldwin sw = &vlan_snd_tag_tls_rl_sw; 2503c782ea8bSJohn Baldwin break; 2504c782ea8bSJohn Baldwin #endif 2505c782ea8bSJohn Baldwin #endif 2506c782ea8bSJohn Baldwin default: 2507892eded5SHans Petter Selasky goto failure; 2508c782ea8bSJohn Baldwin } 2509c782ea8bSJohn Baldwin 2510fb3bc596SJohn Baldwin if (ifv->ifv_trunk != NULL) 2511fb3bc596SJohn Baldwin parent = PARENT(ifv); 2512fb3bc596SJohn Baldwin else 2513fb3bc596SJohn Baldwin parent = NULL; 2514892eded5SHans Petter Selasky if (parent == NULL) 2515892eded5SHans Petter Selasky goto failure; 2516fb3bc596SJohn Baldwin if_ref(parent); 2517fb3bc596SJohn Baldwin NET_EPOCH_EXIT(et); 2518fb3bc596SJohn Baldwin 2519892eded5SHans Petter Selasky if (sw != NULL) { 2520fb3bc596SJohn Baldwin vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT); 2521fb3bc596SJohn Baldwin if (vst == NULL) { 2522fb3bc596SJohn Baldwin if_rele(parent); 2523fb3bc596SJohn Baldwin return (ENOMEM); 2524fb3bc596SJohn Baldwin } 2525892eded5SHans Petter Selasky } else 2526892eded5SHans Petter Selasky vst = NULL; 2527fb3bc596SJohn Baldwin 2528892eded5SHans Petter Selasky error = m_snd_tag_alloc(parent, params, &mst); 2529fb3bc596SJohn Baldwin if_rele(parent); 2530fb3bc596SJohn Baldwin if (error) { 2531fb3bc596SJohn Baldwin free(vst, M_VLAN); 2532fb3bc596SJohn Baldwin return (error); 2533fb3bc596SJohn Baldwin } 2534fb3bc596SJohn Baldwin 2535892eded5SHans Petter Selasky if (sw != NULL) { 2536c782ea8bSJohn Baldwin m_snd_tag_init(&vst->com, ifp, sw); 2537892eded5SHans Petter Selasky vst->tag = mst; 2538fb3bc596SJohn Baldwin 2539fb3bc596SJohn Baldwin *ppmt = &vst->com; 2540892eded5SHans Petter Selasky } else 2541892eded5SHans Petter Selasky *ppmt = mst; 2542892eded5SHans Petter Selasky 2543fb3bc596SJohn Baldwin return (0); 2544892eded5SHans Petter Selasky failure: 2545892eded5SHans Petter Selasky NET_EPOCH_EXIT(et); 2546892eded5SHans Petter Selasky return (EOPNOTSUPP); 2547fb3bc596SJohn Baldwin } 2548fb3bc596SJohn Baldwin 25491a714ff2SRandall Stewart static struct m_snd_tag * 25501a714ff2SRandall Stewart vlan_next_snd_tag(struct m_snd_tag *mst) 25511a714ff2SRandall Stewart { 25521a714ff2SRandall Stewart struct vlan_snd_tag *vst; 25531a714ff2SRandall Stewart 25541a714ff2SRandall Stewart vst = mst_to_vst(mst); 25551a714ff2SRandall Stewart return (vst->tag); 25561a714ff2SRandall Stewart } 25571a714ff2SRandall Stewart 2558fb3bc596SJohn Baldwin static int 2559fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst, 2560fb3bc596SJohn Baldwin union if_snd_tag_modify_params *params) 2561fb3bc596SJohn Baldwin { 2562fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2563fb3bc596SJohn Baldwin 2564fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2565c782ea8bSJohn Baldwin return (vst->tag->sw->snd_tag_modify(vst->tag, params)); 2566fb3bc596SJohn Baldwin } 2567fb3bc596SJohn Baldwin 2568fb3bc596SJohn Baldwin static int 2569fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst, 2570fb3bc596SJohn Baldwin union if_snd_tag_query_params *params) 2571fb3bc596SJohn Baldwin { 2572fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2573fb3bc596SJohn Baldwin 2574fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2575c782ea8bSJohn Baldwin return (vst->tag->sw->snd_tag_query(vst->tag, params)); 2576f3e7afe2SHans Petter Selasky } 2577fa91f845SRandall Stewart 2578fa91f845SRandall Stewart static void 2579fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst) 2580fa91f845SRandall Stewart { 2581fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2582fb3bc596SJohn Baldwin 2583fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2584fb3bc596SJohn Baldwin m_snd_tag_rele(vst->tag); 2585fb3bc596SJohn Baldwin free(vst, M_VLAN); 2586fa91f845SRandall Stewart } 25871a714ff2SRandall Stewart 25881a714ff2SRandall Stewart static void 25891a714ff2SRandall Stewart vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q) 25901a714ff2SRandall Stewart { 25911a714ff2SRandall Stewart /* 25921a714ff2SRandall Stewart * For vlan, we have an indirect 25931a714ff2SRandall Stewart * interface. The caller needs to 25941a714ff2SRandall Stewart * get a ratelimit tag on the actual 25951a714ff2SRandall Stewart * interface the flow will go on. 25961a714ff2SRandall Stewart */ 25971a714ff2SRandall Stewart q->rate_table = NULL; 25981a714ff2SRandall Stewart q->flags = RT_IS_INDIRECT; 25991a714ff2SRandall Stewart q->max_flows = 0; 26001a714ff2SRandall Stewart q->number_of_rates = 0; 26011a714ff2SRandall Stewart } 26021a714ff2SRandall Stewart 2603f3e7afe2SHans Petter Selasky #endif 2604