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 for (j = 0; j < n2; j++) 513b08d611dSMatt Macy CK_SLIST_INIT(&hash2[j]); 51475ee267cSGleb Smirnoff for (i = 0; i < n; i++) 515b08d611dSMatt Macy while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) { 516b08d611dSMatt Macy CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list); 5177983103aSRobert Watson j = HASH(ifv->ifv_vid, n2 - 1); 518b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 51975ee267cSGleb Smirnoff } 520b08d611dSMatt Macy NET_EPOCH_WAIT(); 52175ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 52275ee267cSGleb Smirnoff trunk->hash = hash2; 52375ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 52475ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 525f84b2d69SYaroslav Tykhiy 526f84b2d69SYaroslav Tykhiy if (bootverbose) 527f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 528f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 52975ee267cSGleb Smirnoff } 53075ee267cSGleb Smirnoff 53175ee267cSGleb Smirnoff static __inline struct ifvlan * 5327983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 53375ee267cSGleb Smirnoff { 53475ee267cSGleb Smirnoff struct ifvlan *ifv; 53575ee267cSGleb Smirnoff 536a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 53775ee267cSGleb Smirnoff 538b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list) 5397983103aSRobert Watson if (ifv->ifv_vid == vid) 54075ee267cSGleb Smirnoff return (ifv); 54175ee267cSGleb Smirnoff return (NULL); 54275ee267cSGleb Smirnoff } 54375ee267cSGleb Smirnoff 54475ee267cSGleb Smirnoff #if 0 54575ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 54675ee267cSGleb Smirnoff static void 54775ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 54875ee267cSGleb Smirnoff { 54975ee267cSGleb Smirnoff int i; 55075ee267cSGleb Smirnoff struct ifvlan *ifv; 55175ee267cSGleb Smirnoff 55275ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 55375ee267cSGleb Smirnoff printf("%d: ", i); 554b08d611dSMatt Macy CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 55575ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 55675ee267cSGleb Smirnoff printf("\n"); 55775ee267cSGleb Smirnoff } 55875ee267cSGleb Smirnoff } 55975ee267cSGleb Smirnoff #endif /* 0 */ 560e4cd31ddSJeff Roberson #else 561e4cd31ddSJeff Roberson 562e4cd31ddSJeff Roberson static __inline struct ifvlan * 5637983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 564e4cd31ddSJeff Roberson { 565e4cd31ddSJeff Roberson 5667983103aSRobert Watson return trunk->vlans[vid]; 567e4cd31ddSJeff Roberson } 568e4cd31ddSJeff Roberson 569e4cd31ddSJeff Roberson static __inline int 570e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 571e4cd31ddSJeff Roberson { 572e4cd31ddSJeff Roberson 5737983103aSRobert Watson if (trunk->vlans[ifv->ifv_vid] != NULL) 574e4cd31ddSJeff Roberson return EEXIST; 5757983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = ifv; 576e4cd31ddSJeff Roberson trunk->refcnt++; 577e4cd31ddSJeff Roberson 578e4cd31ddSJeff Roberson return (0); 579e4cd31ddSJeff Roberson } 580e4cd31ddSJeff Roberson 581e4cd31ddSJeff Roberson static __inline int 582e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 583e4cd31ddSJeff Roberson { 584e4cd31ddSJeff Roberson 5857983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = NULL; 586e4cd31ddSJeff Roberson trunk->refcnt--; 587e4cd31ddSJeff Roberson 588e4cd31ddSJeff Roberson return (0); 589e4cd31ddSJeff Roberson } 590e4cd31ddSJeff Roberson 591e4cd31ddSJeff Roberson static __inline void 592e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk) 593e4cd31ddSJeff Roberson { 594e4cd31ddSJeff Roberson } 595e4cd31ddSJeff Roberson 596e4cd31ddSJeff Roberson static __inline void 597e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk) 598e4cd31ddSJeff Roberson { 599e4cd31ddSJeff Roberson } 600e4cd31ddSJeff Roberson 60175ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 60275ee267cSGleb Smirnoff 60375ee267cSGleb Smirnoff static void 60475ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 60575ee267cSGleb Smirnoff { 606d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 60775ee267cSGleb Smirnoff 60875ee267cSGleb Smirnoff vlan_freehash(trunk); 60975ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 61033499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 6119bcf3ae4SAlexander Motin if_rele(trunk->parent); 61275ee267cSGleb Smirnoff free(trunk, M_VLAN); 61375ee267cSGleb Smirnoff } 61475ee267cSGleb Smirnoff 615f731f104SBill Paul /* 616f731f104SBill Paul * Program our multicast filter. What we're actually doing is 617f731f104SBill Paul * programming the multicast filter of the parent. This has the 618f731f104SBill Paul * side effect of causing the parent interface to receive multicast 619f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 620f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 621f731f104SBill Paul * to avoid this: there really is only one physical interface. 622f731f104SBill Paul */ 6232b120974SPeter Wemm static int 6242b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 625f731f104SBill Paul { 626f731f104SBill Paul struct ifnet *ifp_p; 6272d222cb7SAlexander Motin struct ifmultiaddr *ifma; 628f731f104SBill Paul struct ifvlan *sc; 629c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 630f731f104SBill Paul int error; 631f731f104SBill Paul 632b08d611dSMatt Macy VLAN_XLOCK_ASSERT(); 633d148c2a2SMatt Joras 634f731f104SBill Paul /* Find the parent. */ 635f731f104SBill Paul sc = ifp->if_softc; 63675ee267cSGleb Smirnoff ifp_p = PARENT(sc); 6371b2a4f7aSBill Fenner 6388b615593SMarko Zec CURVNET_SET_QUIET(ifp_p->if_vnet); 6398b615593SMarko Zec 640f731f104SBill Paul /* First, remove any existing filter entries. */ 641b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 642b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 6432d222cb7SAlexander Motin (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 6442a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 645f731f104SBill Paul } 646f731f104SBill Paul 647f731f104SBill Paul /* Now program new ones. */ 6482d222cb7SAlexander Motin IF_ADDR_WLOCK(ifp); 649d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 650f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 651f731f104SBill Paul continue; 65229c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 6532d222cb7SAlexander Motin if (mc == NULL) { 6542d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 655c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 65629c2dfbeSBruce M Simpson return (ENOMEM); 6572d222cb7SAlexander Motin } 658e4cd31ddSJeff Roberson bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 659e4cd31ddSJeff Roberson mc->mc_addr.sdl_index = ifp_p->if_index; 660b08d611dSMatt Macy CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 6612d222cb7SAlexander Motin } 6622d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 663b08d611dSMatt Macy CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) { 664e4cd31ddSJeff Roberson error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 6652d222cb7SAlexander Motin NULL); 666c6b2d024SGeorge V. Neville-Neil if (error) { 667c6b2d024SGeorge V. Neville-Neil CURVNET_RESTORE(); 668f731f104SBill Paul return (error); 669f731f104SBill Paul } 670c6b2d024SGeorge V. Neville-Neil } 671f731f104SBill Paul 6728b615593SMarko Zec CURVNET_RESTORE(); 673f731f104SBill Paul return (0); 674f731f104SBill Paul } 6752cc2df49SGarrett Wollman 676a3814acfSSam Leffler /* 677f2ab9160SAndrey V. Elsukov * A handler for interface ifnet events. 678f2ab9160SAndrey V. Elsukov */ 679f2ab9160SAndrey V. Elsukov static void 680f2ab9160SAndrey V. Elsukov vlan_ifevent(void *arg __unused, struct ifnet *ifp, int event) 681f2ab9160SAndrey V. Elsukov { 682f2ab9160SAndrey V. Elsukov struct epoch_tracker et; 683f2ab9160SAndrey V. Elsukov struct ifvlan *ifv; 684f2ab9160SAndrey V. Elsukov struct ifvlantrunk *trunk; 685f2ab9160SAndrey V. Elsukov 686f2ab9160SAndrey V. Elsukov if (event != IFNET_EVENT_UPDATE_BAUDRATE) 687f2ab9160SAndrey V. Elsukov return; 688f2ab9160SAndrey V. Elsukov 689f2ab9160SAndrey V. Elsukov NET_EPOCH_ENTER(et); 690f2ab9160SAndrey V. Elsukov trunk = ifp->if_vlantrunk; 691f2ab9160SAndrey V. Elsukov if (trunk == NULL) { 692f2ab9160SAndrey V. Elsukov NET_EPOCH_EXIT(et); 693f2ab9160SAndrey V. Elsukov return; 694f2ab9160SAndrey V. Elsukov } 695f2ab9160SAndrey V. Elsukov 696f2ab9160SAndrey V. Elsukov TRUNK_WLOCK(trunk); 697f2ab9160SAndrey V. Elsukov VLAN_FOREACH(ifv, trunk) { 698f2ab9160SAndrey V. Elsukov ifv->ifv_ifp->if_baudrate = ifp->if_baudrate; 699f2ab9160SAndrey V. Elsukov } 700f2ab9160SAndrey V. Elsukov TRUNK_WUNLOCK(trunk); 701f2ab9160SAndrey V. Elsukov NET_EPOCH_EXIT(et); 702f2ab9160SAndrey V. Elsukov } 703f2ab9160SAndrey V. Elsukov 704f2ab9160SAndrey V. Elsukov /* 705ea4ca115SAndrew Thompson * A handler for parent interface link layer address changes. 706ea4ca115SAndrew Thompson * If the parent interface link layer address is changed we 707ea4ca115SAndrew Thompson * should also change it on all children vlans. 708ea4ca115SAndrew Thompson */ 709ea4ca115SAndrew Thompson static void 710ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 711ea4ca115SAndrew Thompson { 712a68cc388SGleb Smirnoff struct epoch_tracker et; 713ea4ca115SAndrew Thompson struct ifvlan *ifv; 714d148c2a2SMatt Joras struct ifnet *ifv_ifp; 715d148c2a2SMatt Joras struct ifvlantrunk *trunk; 716d148c2a2SMatt Joras struct sockaddr_dl *sdl; 717ea4ca115SAndrew Thompson 7187b7f772fSGleb Smirnoff /* Need the epoch since this is run on taskqueue_swi. */ 719a68cc388SGleb Smirnoff NET_EPOCH_ENTER(et); 720d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 721d148c2a2SMatt Joras if (trunk == NULL) { 722a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 723ea4ca115SAndrew Thompson return; 724d148c2a2SMatt Joras } 725ea4ca115SAndrew Thompson 726ea4ca115SAndrew Thompson /* 727ea4ca115SAndrew Thompson * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 728d148c2a2SMatt Joras * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR 729d148c2a2SMatt Joras * ioctl calls on the parent garbling the lladdr of the child vlan. 730ea4ca115SAndrew Thompson */ 731d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 732d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 733d148c2a2SMatt Joras /* 734d148c2a2SMatt Joras * Copy new new lladdr into the ifv_ifp, enqueue a task 735d148c2a2SMatt Joras * to actually call if_setlladdr. if_setlladdr needs to 736d148c2a2SMatt Joras * be deferred to a taskqueue because it will call into 737d148c2a2SMatt Joras * the if_vlan ioctl path and try to acquire the global 738d148c2a2SMatt Joras * lock. 739d148c2a2SMatt Joras */ 740d148c2a2SMatt Joras ifv_ifp = ifv->ifv_ifp; 741d148c2a2SMatt Joras bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp), 742e4cd31ddSJeff Roberson ifp->if_addrlen); 743d148c2a2SMatt Joras sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr; 744d148c2a2SMatt Joras sdl->sdl_alen = ifp->if_addrlen; 745d148c2a2SMatt Joras taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 7466117727bSAndrew Thompson } 747d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 748a68cc388SGleb Smirnoff NET_EPOCH_EXIT(et); 749ea4ca115SAndrew Thompson } 750ea4ca115SAndrew Thompson 751ea4ca115SAndrew Thompson /* 7525cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 7535cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 7545cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 7555428776eSJohn Baldwin * by ejecting its hot-plug card. However, if an ifnet is simply 7565428776eSJohn Baldwin * being renamed, then there's no need to tear down the state. 7575cb8c31aSYaroslav Tykhiy */ 7585cb8c31aSYaroslav Tykhiy static void 7595cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 7605cb8c31aSYaroslav Tykhiy { 7615cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 762d148c2a2SMatt Joras struct ifvlantrunk *trunk; 7635cb8c31aSYaroslav Tykhiy 7645428776eSJohn Baldwin /* If the ifnet is just being renamed, don't do anything. */ 7655428776eSJohn Baldwin if (ifp->if_flags & IFF_RENAMING) 7665428776eSJohn Baldwin return; 767d148c2a2SMatt Joras VLAN_XLOCK(); 768d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 769d148c2a2SMatt Joras if (trunk == NULL) { 770d148c2a2SMatt Joras VLAN_XUNLOCK(); 771d148c2a2SMatt Joras return; 772d148c2a2SMatt Joras } 7735428776eSJohn Baldwin 7745cb8c31aSYaroslav Tykhiy /* 7755cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 7765cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 7775cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 7785cb8c31aSYaroslav Tykhiy */ 779d148c2a2SMatt Joras VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk, 780d148c2a2SMatt Joras ifp->if_vlantrunk == NULL) 78128cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 782d148c2a2SMatt Joras 7835cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 7845cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 785d148c2a2SMatt Joras VLAN_XUNLOCK(); 7865cb8c31aSYaroslav Tykhiy } 7875cb8c31aSYaroslav Tykhiy 7885cb8c31aSYaroslav Tykhiy /* 789e4cd31ddSJeff Roberson * Return the trunk device for a virtual interface. 790e4cd31ddSJeff Roberson */ 791e4cd31ddSJeff Roberson static struct ifnet * 792e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp) 793e4cd31ddSJeff Roberson { 794e4cd31ddSJeff Roberson struct ifvlan *ifv; 795e4cd31ddSJeff Roberson 796b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 797b8a6e03fSGleb Smirnoff 798e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 799e4cd31ddSJeff Roberson return (NULL); 800d148c2a2SMatt Joras 801e4cd31ddSJeff Roberson ifv = ifp->if_softc; 802e4cd31ddSJeff Roberson ifp = NULL; 803e4cd31ddSJeff Roberson if (ifv->ifv_trunk) 804e4cd31ddSJeff Roberson ifp = PARENT(ifv); 805e4cd31ddSJeff Roberson return (ifp); 806e4cd31ddSJeff Roberson } 807e4cd31ddSJeff Roberson 808e4cd31ddSJeff Roberson /* 8097983103aSRobert Watson * Return the 12-bit VLAN VID for this interface, for use by external 8107983103aSRobert Watson * components such as Infiniband. 8117983103aSRobert Watson * 8127983103aSRobert Watson * XXXRW: Note that the function name here is historical; it should be named 8137983103aSRobert Watson * vlan_vid(). 814e4cd31ddSJeff Roberson */ 815e4cd31ddSJeff Roberson static int 8167983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp) 817e4cd31ddSJeff Roberson { 818e4cd31ddSJeff Roberson struct ifvlan *ifv; 819e4cd31ddSJeff Roberson 820e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 821e4cd31ddSJeff Roberson return (EINVAL); 822e4cd31ddSJeff Roberson ifv = ifp->if_softc; 8237983103aSRobert Watson *vidp = ifv->ifv_vid; 824e4cd31ddSJeff Roberson return (0); 825e4cd31ddSJeff Roberson } 826e4cd31ddSJeff Roberson 82732d2623aSNavdeep Parhar static int 82832d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp) 82932d2623aSNavdeep Parhar { 83032d2623aSNavdeep Parhar struct ifvlan *ifv; 83132d2623aSNavdeep Parhar 83232d2623aSNavdeep Parhar if (ifp->if_type != IFT_L2VLAN) 83332d2623aSNavdeep Parhar return (EINVAL); 83432d2623aSNavdeep Parhar ifv = ifp->if_softc; 83532d2623aSNavdeep Parhar *pcpp = ifv->ifv_pcp; 83632d2623aSNavdeep Parhar return (0); 83732d2623aSNavdeep Parhar } 83832d2623aSNavdeep Parhar 839e4cd31ddSJeff Roberson /* 840e4cd31ddSJeff Roberson * Return a driver specific cookie for this interface. Synchronization 841e4cd31ddSJeff Roberson * with setcookie must be provided by the driver. 842e4cd31ddSJeff Roberson */ 843e4cd31ddSJeff Roberson static void * 844e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp) 845e4cd31ddSJeff Roberson { 846e4cd31ddSJeff Roberson struct ifvlan *ifv; 847e4cd31ddSJeff Roberson 848e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 849e4cd31ddSJeff Roberson return (NULL); 850e4cd31ddSJeff Roberson ifv = ifp->if_softc; 851e4cd31ddSJeff Roberson return (ifv->ifv_cookie); 852e4cd31ddSJeff Roberson } 853e4cd31ddSJeff Roberson 854e4cd31ddSJeff Roberson /* 855e4cd31ddSJeff Roberson * Store a cookie in our softc that drivers can use to store driver 856e4cd31ddSJeff Roberson * private per-instance data in. 857e4cd31ddSJeff Roberson */ 858e4cd31ddSJeff Roberson static int 859e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie) 860e4cd31ddSJeff Roberson { 861e4cd31ddSJeff Roberson struct ifvlan *ifv; 862e4cd31ddSJeff Roberson 863e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 864e4cd31ddSJeff Roberson return (EINVAL); 865e4cd31ddSJeff Roberson ifv = ifp->if_softc; 866e4cd31ddSJeff Roberson ifv->ifv_cookie = cookie; 867e4cd31ddSJeff Roberson return (0); 868e4cd31ddSJeff Roberson } 869e4cd31ddSJeff Roberson 870e4cd31ddSJeff Roberson /* 8717983103aSRobert Watson * Return the vlan device present at the specific VID. 872e4cd31ddSJeff Roberson */ 873e4cd31ddSJeff Roberson static struct ifnet * 8747983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid) 875e4cd31ddSJeff Roberson { 876e4cd31ddSJeff Roberson struct ifvlantrunk *trunk; 877e4cd31ddSJeff Roberson struct ifvlan *ifv; 878e4cd31ddSJeff Roberson 879b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 880b8a6e03fSGleb Smirnoff 881e4cd31ddSJeff Roberson trunk = ifp->if_vlantrunk; 882b8a6e03fSGleb Smirnoff if (trunk == NULL) 883e4cd31ddSJeff Roberson return (NULL); 884e4cd31ddSJeff Roberson ifp = NULL; 8857983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 886e4cd31ddSJeff Roberson if (ifv) 887e4cd31ddSJeff Roberson ifp = ifv->ifv_ifp; 888e4cd31ddSJeff Roberson return (ifp); 889e4cd31ddSJeff Roberson } 890e4cd31ddSJeff Roberson 891e4cd31ddSJeff Roberson /* 892a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 893a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 894a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 895a3814acfSSam Leffler * set here. No one else in the system should be aware of this so 896a3814acfSSam Leffler * we use an explicit reference here. 897a3814acfSSam Leffler */ 898a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 899a3814acfSSam Leffler 900984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 901a6fffd6cSBrooks Davis extern void (*vlan_link_state_p)(struct ifnet *); 902127d7b2dSAndre Oppermann 903089104e0SAlexander V. Chernikov static struct if_clone_addreq_v2 vlan_addreq = { 904089104e0SAlexander V. Chernikov .version = 2, 90591ebcbe0SAlexander V. Chernikov .match_f = vlan_clone_match, 90691ebcbe0SAlexander V. Chernikov .create_f = vlan_clone_create, 90791ebcbe0SAlexander V. Chernikov .destroy_f = vlan_clone_destroy, 908089104e0SAlexander V. Chernikov .create_nl_f = vlan_clone_create_nl, 909089104e0SAlexander V. Chernikov .modify_nl_f = vlan_clone_modify_nl, 910089104e0SAlexander V. Chernikov .dump_nl_f = vlan_clone_dump_nl, 91191ebcbe0SAlexander V. Chernikov }; 91291ebcbe0SAlexander V. Chernikov 9132b120974SPeter Wemm static int 9142b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 9152b120974SPeter Wemm { 9169d4fe4b2SBrooks Davis 9172b120974SPeter Wemm switch (type) { 9182b120974SPeter Wemm case MOD_LOAD: 9195cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 9205cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 9215cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 9225cb8c31aSYaroslav Tykhiy return (ENOMEM); 923ea4ca115SAndrew Thompson iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 924ea4ca115SAndrew Thompson vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 925ea4ca115SAndrew Thompson if (iflladdr_tag == NULL) 926ea4ca115SAndrew Thompson return (ENOMEM); 927f2ab9160SAndrey V. Elsukov ifevent_tag = EVENTHANDLER_REGISTER(ifnet_event, 928f2ab9160SAndrey V. Elsukov vlan_ifevent, NULL, EVENTHANDLER_PRI_ANY); 929f2ab9160SAndrey V. Elsukov if (ifevent_tag == NULL) 930f2ab9160SAndrey V. Elsukov return (ENOMEM); 931d148c2a2SMatt Joras VLAN_LOCKING_INIT(); 9329d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 933127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 93475ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 935e4cd31ddSJeff Roberson vlan_trunkdev_p = vlan_trunkdev; 936e4cd31ddSJeff Roberson vlan_cookie_p = vlan_cookie; 937e4cd31ddSJeff Roberson vlan_setcookie_p = vlan_setcookie; 938e4cd31ddSJeff Roberson vlan_tag_p = vlan_tag; 93932d2623aSNavdeep Parhar vlan_pcp_p = vlan_pcp; 940e4cd31ddSJeff Roberson vlan_devat_p = vlan_devat; 941ccf7ba97SMarko Zec #ifndef VIMAGE 942089104e0SAlexander V. Chernikov vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq); 943ccf7ba97SMarko Zec #endif 94425c0f7b3SYaroslav Tykhiy if (bootverbose) 94525c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 94625c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 94725c0f7b3SYaroslav Tykhiy "full-size arrays" 94825c0f7b3SYaroslav Tykhiy #else 94925c0f7b3SYaroslav Tykhiy "hash tables with chaining" 95025c0f7b3SYaroslav Tykhiy #endif 95125c0f7b3SYaroslav Tykhiy 95225c0f7b3SYaroslav Tykhiy "\n"); 9532b120974SPeter Wemm break; 9542b120974SPeter Wemm case MOD_UNLOAD: 955ccf7ba97SMarko Zec #ifndef VIMAGE 95691ebcbe0SAlexander V. Chernikov ifc_detach_cloner(vlan_cloner); 957ccf7ba97SMarko Zec #endif 9585cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 959ea4ca115SAndrew Thompson EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 960f2ab9160SAndrey V. Elsukov EVENTHANDLER_DEREGISTER(ifnet_event, ifevent_tag); 9619d4fe4b2SBrooks Davis vlan_input_p = NULL; 962127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 96375ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 964e4cd31ddSJeff Roberson vlan_trunkdev_p = NULL; 965e4cd31ddSJeff Roberson vlan_tag_p = NULL; 96609fe6320SNavdeep Parhar vlan_cookie_p = NULL; 96709fe6320SNavdeep Parhar vlan_setcookie_p = NULL; 968e4cd31ddSJeff Roberson vlan_devat_p = NULL; 969d148c2a2SMatt Joras VLAN_LOCKING_DESTROY(); 97025c0f7b3SYaroslav Tykhiy if (bootverbose) 97125c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 9729d4fe4b2SBrooks Davis break; 9733e019deaSPoul-Henning Kamp default: 9743e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 9752b120974SPeter Wemm } 97615a66c21SBruce M Simpson return (0); 9772b120974SPeter Wemm } 9782b120974SPeter Wemm 9792b120974SPeter Wemm static moduledata_t vlan_mod = { 9802b120974SPeter Wemm "if_vlan", 9812b120974SPeter Wemm vlan_modevent, 9829823d527SKevin Lo 0 9832b120974SPeter Wemm }; 9842b120974SPeter Wemm 9852b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 98611edc477SEd Maste MODULE_VERSION(if_vlan, 3); 9872cc2df49SGarrett Wollman 988ccf7ba97SMarko Zec #ifdef VIMAGE 989ccf7ba97SMarko Zec static void 990ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused) 991ccf7ba97SMarko Zec { 992089104e0SAlexander V. Chernikov vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq); 993ccf7ba97SMarko Zec V_vlan_cloner = vlan_cloner; 994ccf7ba97SMarko Zec } 995ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 996ccf7ba97SMarko Zec vnet_vlan_init, NULL); 997ccf7ba97SMarko Zec 998ccf7ba97SMarko Zec static void 999ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused) 1000ccf7ba97SMarko Zec { 1001ccf7ba97SMarko Zec 100291ebcbe0SAlexander V. Chernikov ifc_detach_cloner(V_vlan_cloner); 1003ccf7ba97SMarko Zec } 1004eb03a443SKristof Provost VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 1005ccf7ba97SMarko Zec vnet_vlan_uninit, NULL); 1006ccf7ba97SMarko Zec #endif 1007ccf7ba97SMarko Zec 1008f941c31aSGleb Smirnoff /* 1009c7cffd65SAlexander V. Chernikov * Check for <etherif>.<vlan>[.<vlan> ...] style interface names. 1010f941c31aSGleb Smirnoff */ 1011f889d2efSBrooks Davis static struct ifnet * 1012f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp) 10139d4fe4b2SBrooks Davis { 1014f941c31aSGleb Smirnoff char ifname[IFNAMSIZ]; 1015f941c31aSGleb Smirnoff char *cp; 1016f889d2efSBrooks Davis struct ifnet *ifp; 10177983103aSRobert Watson int vid; 1018f889d2efSBrooks Davis 1019f941c31aSGleb Smirnoff strlcpy(ifname, name, IFNAMSIZ); 1020c7cffd65SAlexander V. Chernikov if ((cp = strrchr(ifname, '.')) == NULL) 1021f941c31aSGleb Smirnoff return (NULL); 1022f941c31aSGleb Smirnoff *cp = '\0'; 10239bcf3ae4SAlexander Motin if ((ifp = ifunit_ref(ifname)) == NULL) 1024f941c31aSGleb Smirnoff return (NULL); 1025f941c31aSGleb Smirnoff /* Parse VID. */ 10269bcf3ae4SAlexander Motin if (*++cp == '\0') { 10279bcf3ae4SAlexander Motin if_rele(ifp); 1028f941c31aSGleb Smirnoff return (NULL); 10299bcf3ae4SAlexander Motin } 10307983103aSRobert Watson vid = 0; 1031fb92ad4aSJohn Baldwin for(; *cp >= '0' && *cp <= '9'; cp++) 10327983103aSRobert Watson vid = (vid * 10) + (*cp - '0'); 10339bcf3ae4SAlexander Motin if (*cp != '\0') { 10349bcf3ae4SAlexander Motin if_rele(ifp); 1035f941c31aSGleb Smirnoff return (NULL); 10369bcf3ae4SAlexander Motin } 10377983103aSRobert Watson if (vidp != NULL) 10387983103aSRobert Watson *vidp = vid; 1039f889d2efSBrooks Davis 104015a66c21SBruce M Simpson return (ifp); 1041f889d2efSBrooks Davis } 1042f889d2efSBrooks Davis 1043f889d2efSBrooks Davis static int 1044f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 1045f889d2efSBrooks Davis { 10464be465abSAlexander V. Chernikov struct ifnet *ifp; 1047f889d2efSBrooks Davis const char *cp; 1048f889d2efSBrooks Davis 10494be465abSAlexander V. Chernikov ifp = vlan_clone_match_ethervid(name, NULL); 10504be465abSAlexander V. Chernikov if (ifp != NULL) { 10514be465abSAlexander V. Chernikov if_rele(ifp); 1052f889d2efSBrooks Davis return (1); 10534be465abSAlexander V. Chernikov } 1054f889d2efSBrooks Davis 105542a58907SGleb Smirnoff if (strncmp(vlanname, name, strlen(vlanname)) != 0) 1056f889d2efSBrooks Davis return (0); 1057f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 1058f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 1059f889d2efSBrooks Davis return (0); 1060f889d2efSBrooks Davis } 1061f889d2efSBrooks Davis 1062f889d2efSBrooks Davis return (1); 1063f889d2efSBrooks Davis } 1064f889d2efSBrooks Davis 1065f889d2efSBrooks Davis static int 106691ebcbe0SAlexander V. Chernikov vlan_clone_create(struct if_clone *ifc, char *name, size_t len, 106791ebcbe0SAlexander V. Chernikov struct ifc_data *ifd, struct ifnet **ifpp) 1068f889d2efSBrooks Davis { 1069f889d2efSBrooks Davis char *dp; 107053729367SAlexander V. Chernikov bool wildcard = false; 107153729367SAlexander V. Chernikov bool subinterface = false; 1072f889d2efSBrooks Davis int unit; 1073f889d2efSBrooks Davis int error; 107453729367SAlexander V. Chernikov int vid = 0; 107553729367SAlexander V. Chernikov uint16_t proto = ETHERTYPE_VLAN; 10769d4fe4b2SBrooks Davis struct ifvlan *ifv; 10779d4fe4b2SBrooks Davis struct ifnet *ifp; 107853729367SAlexander V. Chernikov struct ifnet *p = NULL; 10793ba24fdeSJohn Baldwin struct ifaddr *ifa; 10803ba24fdeSJohn Baldwin struct sockaddr_dl *sdl; 10816b7330e2SSam Leffler struct vlanreq vlr; 108265239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 1083f889d2efSBrooks Davis 1084c7cffd65SAlexander V. Chernikov 10856b7330e2SSam Leffler /* 108653729367SAlexander V. Chernikov * There are three ways to specify the cloned device: 10876b7330e2SSam Leffler * o pass a parameter block with the clone request. 108853729367SAlexander V. Chernikov * o specify parameters in the text of the clone device name 10896b7330e2SSam Leffler * o specify no parameters and get an unattached device that 10906b7330e2SSam Leffler * must be configured separately. 109153729367SAlexander V. Chernikov * The first technique is preferred; the latter two are supported 1092c7cffd65SAlexander V. Chernikov * for backwards compatibility. 10937983103aSRobert Watson * 10947983103aSRobert Watson * XXXRW: Note historic use of the word "tag" here. New ioctls may be 10957983103aSRobert Watson * called for. 10966b7330e2SSam Leffler */ 109753729367SAlexander V. Chernikov 109891ebcbe0SAlexander V. Chernikov if (ifd->params != NULL) { 109991ebcbe0SAlexander V. Chernikov error = ifc_copyin(ifd, &vlr, sizeof(vlr)); 11006b7330e2SSam Leffler if (error) 11016b7330e2SSam Leffler return error; 110253729367SAlexander V. Chernikov vid = vlr.vlr_tag; 110353729367SAlexander V. Chernikov proto = vlr.vlr_proto; 1104afbb64f1SAlexander V. Chernikov if (proto == 0) 1105afbb64f1SAlexander V. Chernikov proto = ETHERTYPE_VLAN; 11069bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 11076b7330e2SSam Leffler if (p == NULL) 1108b1828acfSGleb Smirnoff return (ENXIO); 110953729367SAlexander V. Chernikov } 111053729367SAlexander V. Chernikov 111153729367SAlexander V. Chernikov if ((error = ifc_name2unit(name, &unit)) == 0) { 111253729367SAlexander V. Chernikov 111353729367SAlexander V. Chernikov /* 111453729367SAlexander V. Chernikov * vlanX interface. Set wildcard to true if the unit number 111553729367SAlexander V. Chernikov * is not fixed (-1) 111653729367SAlexander V. Chernikov */ 111753729367SAlexander V. Chernikov wildcard = (unit < 0); 111853729367SAlexander V. Chernikov } else { 111953729367SAlexander V. Chernikov struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid); 112053729367SAlexander V. Chernikov if (p_tmp != NULL) { 112153729367SAlexander V. Chernikov error = 0; 112253729367SAlexander V. Chernikov subinterface = true; 112353729367SAlexander V. Chernikov unit = IF_DUNIT_NONE; 112453729367SAlexander V. Chernikov wildcard = false; 112553729367SAlexander V. Chernikov if (p != NULL) { 112653729367SAlexander V. Chernikov if_rele(p_tmp); 112753729367SAlexander V. Chernikov if (p != p_tmp) 112853729367SAlexander V. Chernikov error = EINVAL; 112953729367SAlexander V. Chernikov } else 113053729367SAlexander V. Chernikov p = p_tmp; 113153729367SAlexander V. Chernikov } else 113253729367SAlexander V. Chernikov error = ENXIO; 113353729367SAlexander V. Chernikov } 113453729367SAlexander V. Chernikov 11359bcf3ae4SAlexander Motin if (error != 0) { 113653729367SAlexander V. Chernikov if (p != NULL) 11379bcf3ae4SAlexander Motin if_rele(p); 11386b7330e2SSam Leffler return (error); 11399bcf3ae4SAlexander Motin } 1140f889d2efSBrooks Davis 114153729367SAlexander V. Chernikov if (!subinterface) { 114253729367SAlexander V. Chernikov /* vlanX interface, mark X as busy or allocate new unit # */ 1143f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 11449bcf3ae4SAlexander Motin if (error != 0) { 11459bcf3ae4SAlexander Motin if (p != NULL) 11469bcf3ae4SAlexander Motin if_rele(p); 1147f889d2efSBrooks Davis return (error); 11489bcf3ae4SAlexander Motin } 114953729367SAlexander V. Chernikov } 1150f889d2efSBrooks Davis 1151f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 1152f889d2efSBrooks Davis if (wildcard) { 1153f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 1154f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 1155f889d2efSBrooks Davis len - (dp-name) - 1) { 1156f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 1157f889d2efSBrooks Davis } 1158f889d2efSBrooks Davis } 11599d4fe4b2SBrooks Davis 1160a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 1161fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 1162b08d611dSMatt Macy CK_SLIST_INIT(&ifv->vlan_mc_listhead); 11639d4fe4b2SBrooks Davis ifp->if_softc = ifv; 1164f889d2efSBrooks Davis /* 1165cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 1166f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 1167f889d2efSBrooks Davis */ 1168f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 116942a58907SGleb Smirnoff ifp->if_dname = vlanname; 1170f889d2efSBrooks Davis ifp->if_dunit = unit; 11719d4fe4b2SBrooks Davis 1172114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 11732e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 11742e5ff01dSLuiz Otavio O Souza ifp->if_start = vlan_altq_start; 11752e5ff01dSLuiz Otavio O Souza ifp->if_transmit = vlan_altq_transmit; 11762e5ff01dSLuiz Otavio O Souza IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 11772e5ff01dSLuiz Otavio O Souza ifp->if_snd.ifq_drv_maxlen = 0; 11782e5ff01dSLuiz Otavio O Souza IFQ_SET_READY(&ifp->if_snd); 11792e5ff01dSLuiz Otavio O Souza #else 1180d9b1d615SJohn Baldwin ifp->if_transmit = vlan_transmit; 11812e5ff01dSLuiz Otavio O Souza #endif 1182d9b1d615SJohn Baldwin ifp->if_qflush = vlan_qflush; 11839d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 1184b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1185f3e7afe2SHans Petter Selasky ifp->if_snd_tag_alloc = vlan_snd_tag_alloc; 11861a714ff2SRandall Stewart ifp->if_ratelimit_query = vlan_ratelimit_query; 1187f3e7afe2SHans Petter Selasky #endif 118864a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 1189fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 11909d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 1191211f625aSBill Fenner ifp->if_baudrate = 0; 1192a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 1193a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 11943ba24fdeSJohn Baldwin ifa = ifp->if_addr; 11953ba24fdeSJohn Baldwin sdl = (struct sockaddr_dl *)ifa->ifa_addr; 11963ba24fdeSJohn Baldwin sdl->sdl_type = IFT_L2VLAN; 11979d4fe4b2SBrooks Davis 11989bcf3ae4SAlexander Motin if (p != NULL) { 1199c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vid, proto); 12009bcf3ae4SAlexander Motin if_rele(p); 1201f889d2efSBrooks Davis if (error != 0) { 1202f889d2efSBrooks Davis /* 120328cc4d37SJohn Baldwin * Since we've partially failed, we need to back 1204f889d2efSBrooks Davis * out all the way, otherwise userland could get 1205f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 1206f889d2efSBrooks Davis */ 1207f889d2efSBrooks Davis ether_ifdetach(ifp); 1208249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 12094b22573aSBrooks Davis if_free(ifp); 121053729367SAlexander V. Chernikov if (!subinterface) 121196c8ef3aSMaxim Konovalov ifc_free_unit(ifc, unit); 1212f889d2efSBrooks Davis free(ifv, M_VLAN); 1213f889d2efSBrooks Davis 1214f889d2efSBrooks Davis return (error); 1215f889d2efSBrooks Davis } 1216f889d2efSBrooks Davis } 121791ebcbe0SAlexander V. Chernikov *ifpp = ifp; 1218f889d2efSBrooks Davis 12199d4fe4b2SBrooks Davis return (0); 12209d4fe4b2SBrooks Davis } 12219d4fe4b2SBrooks Davis 1222089104e0SAlexander V. Chernikov /* 1223089104e0SAlexander V. Chernikov * 1224089104e0SAlexander V. Chernikov * Parsers of IFLA_INFO_DATA inside IFLA_LINKINFO of RTM_NEWLINK 1225089104e0SAlexander V. Chernikov * {{nla_len=8, nla_type=IFLA_LINK}, 2}, 1226089104e0SAlexander V. Chernikov * {{nla_len=12, nla_type=IFLA_IFNAME}, "xvlan22"}, 1227089104e0SAlexander V. Chernikov * {{nla_len=24, nla_type=IFLA_LINKINFO}, 1228089104e0SAlexander V. Chernikov * [ 1229089104e0SAlexander V. Chernikov * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...}, 1230089104e0SAlexander V. Chernikov * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]} 1231089104e0SAlexander V. Chernikov */ 1232089104e0SAlexander V. Chernikov 1233089104e0SAlexander V. Chernikov struct nl_parsed_vlan { 1234089104e0SAlexander V. Chernikov uint16_t vlan_id; 1235089104e0SAlexander V. Chernikov uint16_t vlan_proto; 1236089104e0SAlexander V. Chernikov struct ifla_vlan_flags vlan_flags; 1237089104e0SAlexander V. Chernikov }; 1238089104e0SAlexander V. Chernikov 1239089104e0SAlexander V. Chernikov #define _OUT(_field) offsetof(struct nl_parsed_vlan, _field) 1240089104e0SAlexander V. Chernikov static const struct nlattr_parser nla_p_vlan[] = { 1241089104e0SAlexander V. Chernikov { .type = IFLA_VLAN_ID, .off = _OUT(vlan_id), .cb = nlattr_get_uint16 }, 1242089104e0SAlexander V. Chernikov { .type = IFLA_VLAN_FLAGS, .off = _OUT(vlan_flags), .cb = nlattr_get_nla }, 1243089104e0SAlexander V. Chernikov { .type = IFLA_VLAN_PROTOCOL, .off = _OUT(vlan_proto), .cb = nlattr_get_uint16 }, 1244089104e0SAlexander V. Chernikov }; 1245089104e0SAlexander V. Chernikov #undef _OUT 1246089104e0SAlexander V. Chernikov NL_DECLARE_ATTR_PARSER(vlan_parser, nla_p_vlan); 1247089104e0SAlexander V. Chernikov 1248089104e0SAlexander V. Chernikov static int 1249089104e0SAlexander V. Chernikov vlan_clone_create_nl(struct if_clone *ifc, char *name, size_t len, 1250089104e0SAlexander V. Chernikov struct ifc_data_nl *ifd) 1251089104e0SAlexander V. Chernikov { 1252089104e0SAlexander V. Chernikov struct epoch_tracker et; 1253089104e0SAlexander V. Chernikov struct ifnet *ifp_parent; 1254089104e0SAlexander V. Chernikov struct nl_pstate *npt = ifd->npt; 1255089104e0SAlexander V. Chernikov struct nl_parsed_link *lattrs = ifd->lattrs; 1256089104e0SAlexander V. Chernikov int error; 1257089104e0SAlexander V. Chernikov 1258089104e0SAlexander V. Chernikov /* 1259089104e0SAlexander V. Chernikov * lattrs.ifla_ifname is the new interface name 1260089104e0SAlexander V. Chernikov * lattrs.ifi_index contains parent interface index 1261089104e0SAlexander V. Chernikov * lattrs.ifla_idata contains un-parsed vlan data 1262089104e0SAlexander V. Chernikov */ 1263089104e0SAlexander V. Chernikov struct nl_parsed_vlan attrs = { 1264089104e0SAlexander V. Chernikov .vlan_id = 0xFEFE, 1265089104e0SAlexander V. Chernikov .vlan_proto = ETHERTYPE_VLAN 1266089104e0SAlexander V. Chernikov }; 1267089104e0SAlexander V. Chernikov 1268089104e0SAlexander V. Chernikov if (lattrs->ifla_idata == NULL) { 1269089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "vlan id is required, guessing not supported"); 1270089104e0SAlexander V. Chernikov return (ENOTSUP); 1271089104e0SAlexander V. Chernikov } 1272089104e0SAlexander V. Chernikov 1273089104e0SAlexander V. Chernikov error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, npt, &attrs); 1274089104e0SAlexander V. Chernikov if (error != 0) 1275089104e0SAlexander V. Chernikov return (error); 1276089104e0SAlexander V. Chernikov if (attrs.vlan_id > 4095) { 1277089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "Invalid VID: %d", attrs.vlan_id); 1278089104e0SAlexander V. Chernikov return (EINVAL); 1279089104e0SAlexander V. Chernikov } 1280089104e0SAlexander V. Chernikov if (attrs.vlan_proto != ETHERTYPE_VLAN && attrs.vlan_proto != ETHERTYPE_QINQ) { 1281089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "Unsupported ethertype: 0x%04X", attrs.vlan_proto); 1282089104e0SAlexander V. Chernikov return (ENOTSUP); 1283089104e0SAlexander V. Chernikov } 1284089104e0SAlexander V. Chernikov 1285089104e0SAlexander V. Chernikov struct vlanreq params = { 1286089104e0SAlexander V. Chernikov .vlr_tag = attrs.vlan_id, 1287089104e0SAlexander V. Chernikov .vlr_proto = attrs.vlan_proto, 1288089104e0SAlexander V. Chernikov }; 1289089104e0SAlexander V. Chernikov struct ifc_data ifd_new = { .flags = IFC_F_SYSSPACE, .unit = ifd->unit, .params = ¶ms }; 1290089104e0SAlexander V. Chernikov 1291089104e0SAlexander V. Chernikov NET_EPOCH_ENTER(et); 1292089104e0SAlexander V. Chernikov ifp_parent = ifnet_byindex(lattrs->ifi_index); 1293089104e0SAlexander V. Chernikov if (ifp_parent != NULL) 1294089104e0SAlexander V. Chernikov strlcpy(params.vlr_parent, if_name(ifp_parent), sizeof(params.vlr_parent)); 1295089104e0SAlexander V. Chernikov NET_EPOCH_EXIT(et); 1296089104e0SAlexander V. Chernikov 1297089104e0SAlexander V. Chernikov if (ifp_parent == NULL) { 1298089104e0SAlexander V. Chernikov nlmsg_report_err_msg(npt, "unable to find parent interface %u", lattrs->ifi_index); 1299089104e0SAlexander V. Chernikov return (ENOENT); 1300089104e0SAlexander V. Chernikov } 1301089104e0SAlexander V. Chernikov 1302089104e0SAlexander V. Chernikov error = vlan_clone_create(ifc, name, len, &ifd_new, &ifd->ifp); 1303089104e0SAlexander V. Chernikov 1304089104e0SAlexander V. Chernikov return (error); 1305089104e0SAlexander V. Chernikov } 1306089104e0SAlexander V. Chernikov 1307089104e0SAlexander V. Chernikov static int 1308089104e0SAlexander V. Chernikov vlan_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd) 1309089104e0SAlexander V. Chernikov { 1310089104e0SAlexander V. Chernikov struct nl_parsed_link *lattrs = ifd->lattrs; 1311089104e0SAlexander V. Chernikov 1312089104e0SAlexander V. Chernikov if ((lattrs->ifla_idata != NULL) && ((ifd->flags & IFC_F_CREATE) == 0)) { 1313089104e0SAlexander V. Chernikov struct epoch_tracker et; 1314089104e0SAlexander V. Chernikov struct nl_parsed_vlan attrs = { 1315089104e0SAlexander V. Chernikov .vlan_proto = ETHERTYPE_VLAN, 1316089104e0SAlexander V. Chernikov }; 1317089104e0SAlexander V. Chernikov int error; 1318089104e0SAlexander V. Chernikov 1319089104e0SAlexander V. Chernikov error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, ifd->npt, &attrs); 1320089104e0SAlexander V. Chernikov if (error != 0) 1321089104e0SAlexander V. Chernikov return (error); 1322089104e0SAlexander V. Chernikov 1323089104e0SAlexander V. Chernikov NET_EPOCH_ENTER(et); 1324089104e0SAlexander V. Chernikov struct ifnet *ifp_parent = ifnet_byindex_ref(lattrs->ifla_link); 1325089104e0SAlexander V. Chernikov NET_EPOCH_EXIT(et); 1326089104e0SAlexander V. Chernikov 1327089104e0SAlexander V. Chernikov if (ifp_parent == NULL) { 1328089104e0SAlexander V. Chernikov nlmsg_report_err_msg(ifd->npt, "unable to find parent interface %u", 1329089104e0SAlexander V. Chernikov lattrs->ifla_link); 1330089104e0SAlexander V. Chernikov return (ENOENT); 1331089104e0SAlexander V. Chernikov } 1332089104e0SAlexander V. Chernikov 1333089104e0SAlexander V. Chernikov struct ifvlan *ifv = ifp->if_softc; 1334089104e0SAlexander V. Chernikov error = vlan_config(ifv, ifp_parent, attrs.vlan_id, attrs.vlan_proto); 1335089104e0SAlexander V. Chernikov 1336089104e0SAlexander V. Chernikov if_rele(ifp_parent); 1337089104e0SAlexander V. Chernikov if (error != 0) 1338089104e0SAlexander V. Chernikov return (error); 1339089104e0SAlexander V. Chernikov } 1340089104e0SAlexander V. Chernikov 1341089104e0SAlexander V. Chernikov return (nl_modify_ifp_generic(ifp, ifd->lattrs, ifd->bm, ifd->npt)); 1342089104e0SAlexander V. Chernikov } 1343089104e0SAlexander V. Chernikov 1344089104e0SAlexander V. Chernikov /* 1345089104e0SAlexander V. Chernikov * {{nla_len=24, nla_type=IFLA_LINKINFO}, 1346089104e0SAlexander V. Chernikov * [ 1347089104e0SAlexander V. Chernikov * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...}, 1348089104e0SAlexander V. Chernikov * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]} 1349089104e0SAlexander V. Chernikov */ 1350089104e0SAlexander V. Chernikov static void 1351089104e0SAlexander V. Chernikov vlan_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw) 1352089104e0SAlexander V. Chernikov { 1353089104e0SAlexander V. Chernikov uint32_t parent_index = 0; 1354089104e0SAlexander V. Chernikov uint16_t vlan_id = 0; 1355089104e0SAlexander V. Chernikov uint16_t vlan_proto = 0; 1356089104e0SAlexander V. Chernikov 1357089104e0SAlexander V. Chernikov VLAN_SLOCK(); 1358089104e0SAlexander V. Chernikov struct ifvlan *ifv = ifp->if_softc; 1359089104e0SAlexander V. Chernikov if (TRUNK(ifv) != NULL) 1360089104e0SAlexander V. Chernikov parent_index = PARENT(ifv)->if_index; 1361089104e0SAlexander V. Chernikov vlan_id = ifv->ifv_vid; 1362089104e0SAlexander V. Chernikov vlan_proto = ifv->ifv_proto; 1363089104e0SAlexander V. Chernikov VLAN_SUNLOCK(); 1364089104e0SAlexander V. Chernikov 1365089104e0SAlexander V. Chernikov if (parent_index != 0) 1366089104e0SAlexander V. Chernikov nlattr_add_u32(nw, IFLA_LINK, parent_index); 1367089104e0SAlexander V. Chernikov 1368089104e0SAlexander V. Chernikov int off = nlattr_add_nested(nw, IFLA_LINKINFO); 1369089104e0SAlexander V. Chernikov if (off != 0) { 1370089104e0SAlexander V. Chernikov nlattr_add_string(nw, IFLA_INFO_KIND, "vlan"); 1371089104e0SAlexander V. Chernikov int off2 = nlattr_add_nested(nw, IFLA_INFO_DATA); 1372089104e0SAlexander V. Chernikov if (off2 != 0) { 1373089104e0SAlexander V. Chernikov nlattr_add_u16(nw, IFLA_VLAN_ID, vlan_id); 1374089104e0SAlexander V. Chernikov nlattr_add_u16(nw, IFLA_VLAN_PROTOCOL, vlan_proto); 1375089104e0SAlexander V. Chernikov nlattr_set_len(nw, off2); 1376089104e0SAlexander V. Chernikov } 1377089104e0SAlexander V. Chernikov nlattr_set_len(nw, off); 1378089104e0SAlexander V. Chernikov } 1379089104e0SAlexander V. Chernikov } 1380089104e0SAlexander V. Chernikov 1381f889d2efSBrooks Davis static int 138291ebcbe0SAlexander V. Chernikov vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 13839d4fe4b2SBrooks Davis { 13849d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 138553729367SAlexander V. Chernikov int unit = ifp->if_dunit; 1386c7cffd65SAlexander V. Chernikov 1387c7cffd65SAlexander V. Chernikov if (ifp->if_vlantrunk) 1388c7cffd65SAlexander V. Chernikov return (EBUSY); 1389b4e9f837SBrooks Davis 13902e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 13912e5ff01dSLuiz Otavio O Souza IFQ_PURGE(&ifp->if_snd); 13922e5ff01dSLuiz Otavio O Souza #endif 1393249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 1394249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 1395d148c2a2SMatt Joras /* 1396d148c2a2SMatt Joras * We should have the only reference to the ifv now, so we can now 1397d148c2a2SMatt Joras * drain any remaining lladdr task before freeing the ifnet and the 1398d148c2a2SMatt Joras * ifvlan. 1399d148c2a2SMatt Joras */ 1400d148c2a2SMatt Joras taskqueue_drain(taskqueue_thread, &ifv->lladdr_task); 1401b08d611dSMatt Macy NET_EPOCH_WAIT(); 14024b22573aSBrooks Davis if_free(ifp); 14039d4fe4b2SBrooks Davis free(ifv, M_VLAN); 140453729367SAlexander V. Chernikov if (unit != IF_DUNIT_NONE) 140553729367SAlexander V. Chernikov ifc_free_unit(ifc, unit); 1406b4e9f837SBrooks Davis 1407f889d2efSBrooks Davis return (0); 14089d4fe4b2SBrooks Davis } 14099d4fe4b2SBrooks Davis 141015a66c21SBruce M Simpson /* 141115a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 141215a66c21SBruce M Simpson */ 14132cc2df49SGarrett Wollman static void 1414114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 14152cc2df49SGarrett Wollman { 14162cc2df49SGarrett Wollman } 14172cc2df49SGarrett Wollman 14186d3a3ab7SGleb Smirnoff /* 1419d9b1d615SJohn Baldwin * The if_transmit method for vlan(4) interface. 14206d3a3ab7SGleb Smirnoff */ 1421d9b1d615SJohn Baldwin static int 1422d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m) 14232cc2df49SGarrett Wollman { 14242cc2df49SGarrett Wollman struct ifvlan *ifv; 14252cc2df49SGarrett Wollman struct ifnet *p; 14261ad7a257SPyun YongHyeon int error, len, mcast; 14272cc2df49SGarrett Wollman 1428b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1429b8a6e03fSGleb Smirnoff 14302cc2df49SGarrett Wollman ifv = ifp->if_softc; 1431d148c2a2SMatt Joras if (TRUNK(ifv) == NULL) { 1432d148c2a2SMatt Joras if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1433d148c2a2SMatt Joras m_freem(m); 1434d148c2a2SMatt Joras return (ENETDOWN); 1435d148c2a2SMatt Joras } 143675ee267cSGleb Smirnoff p = PARENT(ifv); 14371ad7a257SPyun YongHyeon len = m->m_pkthdr.len; 14381ad7a257SPyun YongHyeon mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 14392cc2df49SGarrett Wollman 1440a3814acfSSam Leffler BPF_MTAP(ifp, m); 14412cc2df49SGarrett Wollman 1442b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 1443fb3bc596SJohn Baldwin if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) { 1444fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 1445fb3bc596SJohn Baldwin struct m_snd_tag *mst; 1446fb3bc596SJohn Baldwin 1447fb3bc596SJohn Baldwin MPASS(m->m_pkthdr.snd_tag->ifp == ifp); 1448fb3bc596SJohn Baldwin mst = m->m_pkthdr.snd_tag; 1449fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 1450fb3bc596SJohn Baldwin if (vst->tag->ifp != p) { 1451fb3bc596SJohn Baldwin if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1452fb3bc596SJohn Baldwin m_freem(m); 1453fb3bc596SJohn Baldwin return (EAGAIN); 1454fb3bc596SJohn Baldwin } 1455fb3bc596SJohn Baldwin 1456fb3bc596SJohn Baldwin m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag); 1457fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 1458fb3bc596SJohn Baldwin } 1459fb3bc596SJohn Baldwin #endif 1460fb3bc596SJohn Baldwin 1461f731f104SBill Paul /* 1462d9b1d615SJohn Baldwin * Do not run parent's if_transmit() if the parent is not up, 146324993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 146424993214SYaroslav Tykhiy */ 14652dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 1466a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1467d148c2a2SMatt Joras m_freem(m); 14688b20f6cfSHiroki Sato return (ENETDOWN); 146924993214SYaroslav Tykhiy } 147024993214SYaroslav Tykhiy 1471c7cffd65SAlexander V. Chernikov if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) { 1472a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1473d9b1d615SJohn Baldwin return (0); 14744af90a4dSMatthew N. Dodd } 14752cc2df49SGarrett Wollman 14762cc2df49SGarrett Wollman /* 14772cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 14782cc2df49SGarrett Wollman */ 1479aea78d20SKip Macy error = (p->if_transmit)(p, m); 1480299153b5SAlexander V. Chernikov if (error == 0) { 1481a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1482a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 1483a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast); 14841ad7a257SPyun YongHyeon } else 1485a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1486d9b1d615SJohn Baldwin return (error); 14872cc2df49SGarrett Wollman } 1488d9b1d615SJohn Baldwin 148916cf6bdbSMatt Joras static int 149016cf6bdbSMatt Joras vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 149116cf6bdbSMatt Joras struct route *ro) 149216cf6bdbSMatt Joras { 149316cf6bdbSMatt Joras struct ifvlan *ifv; 149416cf6bdbSMatt Joras struct ifnet *p; 149516cf6bdbSMatt Joras 1496b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1497b8a6e03fSGleb Smirnoff 1498c7cffd65SAlexander V. Chernikov /* 1499c7cffd65SAlexander V. Chernikov * Find the first non-VLAN parent interface. 1500c7cffd65SAlexander V. Chernikov */ 150116cf6bdbSMatt Joras ifv = ifp->if_softc; 1502c7cffd65SAlexander V. Chernikov do { 150316cf6bdbSMatt Joras if (TRUNK(ifv) == NULL) { 150416cf6bdbSMatt Joras m_freem(m); 150516cf6bdbSMatt Joras return (ENETDOWN); 150616cf6bdbSMatt Joras } 150716cf6bdbSMatt Joras p = PARENT(ifv); 1508c7cffd65SAlexander V. Chernikov ifv = p->if_softc; 1509c7cffd65SAlexander V. Chernikov } while (p->if_type == IFT_L2VLAN); 1510c7cffd65SAlexander V. Chernikov 151116cf6bdbSMatt Joras return p->if_output(ifp, m, dst, ro); 151216cf6bdbSMatt Joras } 151316cf6bdbSMatt Joras 15142e5ff01dSLuiz Otavio O Souza #ifdef ALTQ 15152e5ff01dSLuiz Otavio O Souza static void 15162e5ff01dSLuiz Otavio O Souza vlan_altq_start(if_t ifp) 15172e5ff01dSLuiz Otavio O Souza { 15182e5ff01dSLuiz Otavio O Souza struct ifaltq *ifq = &ifp->if_snd; 15192e5ff01dSLuiz Otavio O Souza struct mbuf *m; 15202e5ff01dSLuiz Otavio O Souza 15212e5ff01dSLuiz Otavio O Souza IFQ_LOCK(ifq); 15222e5ff01dSLuiz Otavio O Souza IFQ_DEQUEUE_NOLOCK(ifq, m); 15232e5ff01dSLuiz Otavio O Souza while (m != NULL) { 15242e5ff01dSLuiz Otavio O Souza vlan_transmit(ifp, m); 15252e5ff01dSLuiz Otavio O Souza IFQ_DEQUEUE_NOLOCK(ifq, m); 15262e5ff01dSLuiz Otavio O Souza } 15272e5ff01dSLuiz Otavio O Souza IFQ_UNLOCK(ifq); 15282e5ff01dSLuiz Otavio O Souza } 15292e5ff01dSLuiz Otavio O Souza 15302e5ff01dSLuiz Otavio O Souza static int 15312e5ff01dSLuiz Otavio O Souza vlan_altq_transmit(if_t ifp, struct mbuf *m) 15322e5ff01dSLuiz Otavio O Souza { 15332e5ff01dSLuiz Otavio O Souza int err; 15342e5ff01dSLuiz Otavio O Souza 15352e5ff01dSLuiz Otavio O Souza if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 15362e5ff01dSLuiz Otavio O Souza IFQ_ENQUEUE(&ifp->if_snd, m, err); 15372e5ff01dSLuiz Otavio O Souza if (err == 0) 15382e5ff01dSLuiz Otavio O Souza vlan_altq_start(ifp); 15392e5ff01dSLuiz Otavio O Souza } else 15402e5ff01dSLuiz Otavio O Souza err = vlan_transmit(ifp, m); 15412e5ff01dSLuiz Otavio O Souza 15422e5ff01dSLuiz Otavio O Souza return (err); 15432e5ff01dSLuiz Otavio O Souza } 15442e5ff01dSLuiz Otavio O Souza #endif /* ALTQ */ 15452e5ff01dSLuiz Otavio O Souza 1546d9b1d615SJohn Baldwin /* 1547d9b1d615SJohn Baldwin * The ifp->if_qflush entry point for vlan(4) is a no-op. 1548d9b1d615SJohn Baldwin */ 1549d9b1d615SJohn Baldwin static void 1550d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused) 1551d9b1d615SJohn Baldwin { 1552f731f104SBill Paul } 1553f731f104SBill Paul 1554a3814acfSSam Leffler static void 1555a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 1556f731f104SBill Paul { 1557d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1558f731f104SBill Paul struct ifvlan *ifv; 15592ccbbd06SMarcelo Araujo struct m_tag *mtag; 15602ccbbd06SMarcelo Araujo uint16_t vid, tag; 156175ee267cSGleb Smirnoff 1562b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1563b8a6e03fSGleb Smirnoff 1564d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1565d148c2a2SMatt Joras if (trunk == NULL) { 1566d148c2a2SMatt Joras m_freem(m); 1567d148c2a2SMatt Joras return; 1568d148c2a2SMatt Joras } 1569a3814acfSSam Leffler 1570f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 1571a3814acfSSam Leffler /* 157214e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 1573a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 1574a3814acfSSam Leffler */ 15752ccbbd06SMarcelo Araujo tag = m->m_pkthdr.ether_vtag; 15766ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 1577a3814acfSSam Leffler } else { 157875ee267cSGleb Smirnoff struct ether_vlan_header *evl; 157975ee267cSGleb Smirnoff 158014e98256SYaroslav Tykhiy /* 158114e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 158214e98256SYaroslav Tykhiy */ 1583a3814acfSSam Leffler switch (ifp->if_type) { 1584a3814acfSSam Leffler case IFT_ETHER: 1585a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 1586a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 1587a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 1588a3814acfSSam Leffler return; 1589a3814acfSSam Leffler } 1590a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 15912ccbbd06SMarcelo Araujo tag = ntohs(evl->evl_tag); 1592db8b5973SYaroslav Tykhiy 1593db8b5973SYaroslav Tykhiy /* 15942dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 15952dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 15962dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 15972dc879b3SYaroslav Tykhiy * type field is already in place. 1598db8b5973SYaroslav Tykhiy */ 15992dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 16002dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 16012dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 1602a3814acfSSam Leffler break; 16032dc879b3SYaroslav Tykhiy 1604a3814acfSSam Leffler default: 1605db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 160660c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 160760c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 1608a3814acfSSam Leffler #endif 16093751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1610d148c2a2SMatt Joras m_freem(m); 161160c60618SYaroslav Tykhiy return; 1612a3814acfSSam Leffler } 16137a46ec8fSBrooks Davis } 16147a46ec8fSBrooks Davis 16152ccbbd06SMarcelo Araujo vid = EVL_VLANOFTAG(tag); 16162ccbbd06SMarcelo Araujo 16177983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 16182dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 1619b08d611dSMatt Macy if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1620d148c2a2SMatt Joras m_freem(m); 162175ee267cSGleb Smirnoff return; 162275ee267cSGleb Smirnoff } 1623f731f104SBill Paul 162478bc3d5eSKristof Provost if (V_vlan_mtag_pcp) { 16252ccbbd06SMarcelo Araujo /* 16262ccbbd06SMarcelo Araujo * While uncommon, it is possible that we will find a 802.1q 16272ccbbd06SMarcelo Araujo * packet encapsulated inside another packet that also had an 16282ccbbd06SMarcelo Araujo * 802.1q header. For example, ethernet tunneled over IPSEC 16292ccbbd06SMarcelo Araujo * arriving over ethernet. In that case, we replace the 16302ccbbd06SMarcelo Araujo * existing 802.1q PCP m_tag value. 16312ccbbd06SMarcelo Araujo */ 16322ccbbd06SMarcelo Araujo mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 16332ccbbd06SMarcelo Araujo if (mtag == NULL) { 16342ccbbd06SMarcelo Araujo mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN, 16352ccbbd06SMarcelo Araujo sizeof(uint8_t), M_NOWAIT); 16362ccbbd06SMarcelo Araujo if (mtag == NULL) { 16372ccbbd06SMarcelo Araujo if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1638d148c2a2SMatt Joras m_freem(m); 16392ccbbd06SMarcelo Araujo return; 16402ccbbd06SMarcelo Araujo } 16412ccbbd06SMarcelo Araujo m_tag_prepend(m, mtag); 16422ccbbd06SMarcelo Araujo } 16432ccbbd06SMarcelo Araujo *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag); 16442ccbbd06SMarcelo Araujo } 16452ccbbd06SMarcelo Araujo 1646fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 1647c0304424SGleb Smirnoff if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1); 16482cc2df49SGarrett Wollman 1649a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 1650fdbf1174SMatt Joras (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m); 16512cc2df49SGarrett Wollman } 16522cc2df49SGarrett Wollman 1653d148c2a2SMatt Joras static void 1654d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused) 1655d148c2a2SMatt Joras { 1656d148c2a2SMatt Joras struct ifvlan *ifv; 1657d148c2a2SMatt Joras struct ifnet *ifp; 1658d148c2a2SMatt Joras 1659d148c2a2SMatt Joras ifv = (struct ifvlan *)arg; 1660d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 16615191a3aeSKristof Provost 16625191a3aeSKristof Provost CURVNET_SET(ifp->if_vnet); 16635191a3aeSKristof Provost 1664d148c2a2SMatt Joras /* The ifv_ifp already has the lladdr copied in. */ 1665d148c2a2SMatt Joras if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen); 16665191a3aeSKristof Provost 16675191a3aeSKristof Provost CURVNET_RESTORE(); 1668d148c2a2SMatt Joras } 1669d148c2a2SMatt Joras 16702cc2df49SGarrett Wollman static int 1671c7cffd65SAlexander V. Chernikov vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid, 1672c7cffd65SAlexander V. Chernikov uint16_t proto) 16732cc2df49SGarrett Wollman { 16746dcec895SGleb Smirnoff struct epoch_tracker et; 167575ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 16761cf236fbSYaroslav Tykhiy struct ifnet *ifp; 167775ee267cSGleb Smirnoff int error = 0; 16782cc2df49SGarrett Wollman 1679b1828acfSGleb Smirnoff /* 1680b1828acfSGleb Smirnoff * We can handle non-ethernet hardware types as long as 1681b1828acfSGleb Smirnoff * they handle the tagging and headers themselves. 1682b1828acfSGleb Smirnoff */ 1683e4cd31ddSJeff Roberson if (p->if_type != IFT_ETHER && 1684c7cffd65SAlexander V. Chernikov p->if_type != IFT_L2VLAN && 1685e4cd31ddSJeff Roberson (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 168615a66c21SBruce M Simpson return (EPROTONOSUPPORT); 168764a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 168864a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 1689b1828acfSGleb Smirnoff /* 1690b1828acfSGleb Smirnoff * Don't let the caller set up a VLAN VID with 1691b1828acfSGleb Smirnoff * anything except VLID bits. 1692b1828acfSGleb Smirnoff * VID numbers 0x0 and 0xFFF are reserved. 1693b1828acfSGleb Smirnoff */ 1694b1828acfSGleb Smirnoff if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK)) 1695b1828acfSGleb Smirnoff return (EINVAL); 1696663f556bSKristof Provost if (ifv->ifv_trunk) { 1697663f556bSKristof Provost trunk = ifv->ifv_trunk; 1698663f556bSKristof Provost if (trunk->parent != p) 169915a66c21SBruce M Simpson return (EBUSY); 17002cc2df49SGarrett Wollman 1701d148c2a2SMatt Joras VLAN_XLOCK(); 1702663f556bSKristof Provost 1703663f556bSKristof Provost ifv->ifv_proto = proto; 1704663f556bSKristof Provost 1705663f556bSKristof Provost if (ifv->ifv_vid != vid) { 1706bdd12889SKristof Provost int oldvid = ifv->ifv_vid; 1707bdd12889SKristof Provost 1708663f556bSKristof Provost /* Re-hash */ 1709663f556bSKristof Provost vlan_remhash(trunk, ifv); 1710663f556bSKristof Provost ifv->ifv_vid = vid; 1711663f556bSKristof Provost error = vlan_inshash(trunk, ifv); 1712bdd12889SKristof Provost if (error) { 1713bdd12889SKristof Provost int ret __diagused; 1714bdd12889SKristof Provost 1715bdd12889SKristof Provost ifv->ifv_vid = oldvid; 1716bdd12889SKristof Provost /* Re-insert back where we found it. */ 1717bdd12889SKristof Provost ret = vlan_inshash(trunk, ifv); 1718bdd12889SKristof Provost MPASS(ret == 0); 1719bdd12889SKristof Provost } 1720663f556bSKristof Provost } 1721663f556bSKristof Provost /* Will unlock */ 1722663f556bSKristof Provost goto done; 1723663f556bSKristof Provost } 1724663f556bSKristof Provost 1725663f556bSKristof Provost VLAN_XLOCK(); 172675ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 172775ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 172875ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 172975ee267cSGleb Smirnoff vlan_inithash(trunk); 173075ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 1731d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 173275ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 173375ee267cSGleb Smirnoff trunk->parent = p; 17349bcf3ae4SAlexander Motin if_ref(trunk->parent); 1735b08d611dSMatt Macy TRUNK_WUNLOCK(trunk); 173675ee267cSGleb Smirnoff } else { 173775ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 173875ee267cSGleb Smirnoff } 173975ee267cSGleb Smirnoff 17407983103aSRobert Watson ifv->ifv_vid = vid; /* must set this before vlan_inshash() */ 17412ccbbd06SMarcelo Araujo ifv->ifv_pcp = 0; /* Default: best effort delivery. */ 174275ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 174375ee267cSGleb Smirnoff if (error) 174475ee267cSGleb Smirnoff goto done; 1745c7cffd65SAlexander V. Chernikov ifv->ifv_proto = proto; 1746a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1747a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 17481cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1749d89baa5aSAlexander Motin ifv->ifv_capenable = -1; 175084abf7e2SKonstantin Belousov ifv->ifv_capenable2 = -1; 1751a3814acfSSam Leffler 1752a3814acfSSam Leffler /* 1753a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1754a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1755656acce4SYaroslav Tykhiy * use it. 1756a3814acfSSam Leffler */ 1757656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1758656acce4SYaroslav Tykhiy /* 1759656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1760656acce4SYaroslav Tykhiy * handle extended frames. 1761656acce4SYaroslav Tykhiy */ 1762a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1763656acce4SYaroslav Tykhiy } else { 1764a3814acfSSam Leffler /* 1765a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1766a3814acfSSam Leffler * makes us incompatible with strictly compliant 1767a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1768a3814acfSSam Leffler * the feature with other NetBSD implementations, 1769a3814acfSSam Leffler * which might still be useful. 1770a3814acfSSam Leffler */ 1771a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1772a3814acfSSam Leffler } 1773a3814acfSSam Leffler 177475ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 17751cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 1776e4cd31ddSJeff Roberson /* 1777e4cd31ddSJeff Roberson * Initialize fields from our parent. This duplicates some 1778e4cd31ddSJeff Roberson * work with ether_ifattach() but allows for non-ethernet 1779e4cd31ddSJeff Roberson * interfaces to also work. 1780e4cd31ddSJeff Roberson */ 17811cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 178275ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 1783e4cd31ddSJeff Roberson ifp->if_input = p->if_input; 1784e4cd31ddSJeff Roberson ifp->if_resolvemulti = p->if_resolvemulti; 1785e4cd31ddSJeff Roberson ifp->if_addrlen = p->if_addrlen; 1786e4cd31ddSJeff Roberson ifp->if_broadcastaddr = p->if_broadcastaddr; 178732a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 1788e4cd31ddSJeff Roberson 17892cc2df49SGarrett Wollman /* 179016cf6bdbSMatt Joras * We wrap the parent's if_output using vlan_output to ensure that it 179116cf6bdbSMatt Joras * can't become stale. 179216cf6bdbSMatt Joras */ 179316cf6bdbSMatt Joras ifp->if_output = vlan_output; 179416cf6bdbSMatt Joras 179516cf6bdbSMatt Joras /* 179624993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 179724993214SYaroslav Tykhiy * Other flags are none of our business. 17982cc2df49SGarrett Wollman */ 179964a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 18001cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 18011cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 18021cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 18031cf236fbSYaroslav Tykhiy 18041cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 18052cc2df49SGarrett Wollman 18066dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 180775ee267cSGleb Smirnoff vlan_capabilities(ifv); 18086dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 1809a3814acfSSam Leffler 1810a3814acfSSam Leffler /* 1811e4cd31ddSJeff Roberson * Set up our interface address to reflect the underlying 18122cc2df49SGarrett Wollman * physical interface's. 18132cc2df49SGarrett Wollman */ 1814a961401eSAndrey V. Elsukov TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv); 1815e4cd31ddSJeff Roberson ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1816e4cd31ddSJeff Roberson p->if_addrlen; 18171b2a4f7aSBill Fenner 1818a961401eSAndrey V. Elsukov /* 1819a961401eSAndrey V. Elsukov * Do not schedule link address update if it was the same 1820a961401eSAndrey V. Elsukov * as previous parent's. This helps avoid updating for each 1821a961401eSAndrey V. Elsukov * associated llentry. 1822a961401eSAndrey V. Elsukov */ 1823a961401eSAndrey V. Elsukov if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) { 1824a961401eSAndrey V. Elsukov bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1825a961401eSAndrey V. Elsukov taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 1826a961401eSAndrey V. Elsukov } 18272ada9747SYaroslav Tykhiy 18282ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 18292ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 1830d148c2a2SMatt Joras 1831d148c2a2SMatt Joras /* Update flags on the parent, if necessary. */ 1832d148c2a2SMatt Joras vlan_setflags(ifp, 1); 1833b08d611dSMatt Macy 1834d148c2a2SMatt Joras /* 1835b08d611dSMatt Macy * Configure multicast addresses that may already be 1836b08d611dSMatt Macy * joined on the vlan device. 1837d148c2a2SMatt Joras */ 1838b08d611dSMatt Macy (void)vlan_setmulti(ifp); 1839b08d611dSMatt Macy 1840b08d611dSMatt Macy done: 1841c725524cSJack F Vogel if (error == 0) 18427983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid); 1843d148c2a2SMatt Joras VLAN_XUNLOCK(); 184475ee267cSGleb Smirnoff 184575ee267cSGleb Smirnoff return (error); 18462cc2df49SGarrett Wollman } 18472cc2df49SGarrett Wollman 18486f359e28SJohn Baldwin static void 1849f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1850f731f104SBill Paul { 18515cb8c31aSYaroslav Tykhiy 1852d148c2a2SMatt Joras VLAN_XLOCK(); 185328cc4d37SJohn Baldwin vlan_unconfig_locked(ifp, 0); 1854d148c2a2SMatt Joras VLAN_XUNLOCK(); 18555cb8c31aSYaroslav Tykhiy } 18565cb8c31aSYaroslav Tykhiy 18576f359e28SJohn Baldwin static void 185828cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing) 18595cb8c31aSYaroslav Tykhiy { 186075ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1861f731f104SBill Paul struct vlan_mc_entry *mc; 1862f731f104SBill Paul struct ifvlan *ifv; 1863c725524cSJack F Vogel struct ifnet *parent; 186428cc4d37SJohn Baldwin int error; 1865f731f104SBill Paul 1866d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 18674faedfe8SSam Leffler 1868f731f104SBill Paul ifv = ifp->if_softc; 186975ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 187022893351SJack F Vogel parent = NULL; 1871f731f104SBill Paul 187222893351SJack F Vogel if (trunk != NULL) { 187322893351SJack F Vogel parent = trunk->parent; 18741b2a4f7aSBill Fenner 1875f731f104SBill Paul /* 1876f731f104SBill Paul * Since the interface is being unconfigured, we need to 1877f731f104SBill Paul * empty the list of multicast groups that we may have joined 18781b2a4f7aSBill Fenner * while we were alive from the parent's list. 1879f731f104SBill Paul */ 1880b08d611dSMatt Macy while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 18816f359e28SJohn Baldwin /* 188228cc4d37SJohn Baldwin * If the parent interface is being detached, 1883b90dde2fSJohn Baldwin * all its multicast addresses have already 188428cc4d37SJohn Baldwin * been removed. Warn about errors if 188528cc4d37SJohn Baldwin * if_delmulti() does fail, but don't abort as 188628cc4d37SJohn Baldwin * all callers expect vlan destruction to 188728cc4d37SJohn Baldwin * succeed. 18886f359e28SJohn Baldwin */ 188928cc4d37SJohn Baldwin if (!departing) { 189028cc4d37SJohn Baldwin error = if_delmulti(parent, 1891e4cd31ddSJeff Roberson (struct sockaddr *)&mc->mc_addr); 189228cc4d37SJohn Baldwin if (error) 189328cc4d37SJohn Baldwin if_printf(ifp, 189428cc4d37SJohn Baldwin "Failed to delete multicast address from parent: %d\n", 189528cc4d37SJohn Baldwin error); 189628cc4d37SJohn Baldwin } 1897b08d611dSMatt Macy CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 18982a4bd982SGleb Smirnoff NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx); 1899f731f104SBill Paul } 1900a3814acfSSam Leffler 19011cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 1902d148c2a2SMatt Joras 190375ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 190475ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 190575ee267cSGleb Smirnoff 190675ee267cSGleb Smirnoff /* 190775ee267cSGleb Smirnoff * Check if we were the last. 190875ee267cSGleb Smirnoff */ 190975ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 19102d222cb7SAlexander Motin parent->if_vlantrunk = NULL; 1911b08d611dSMatt Macy NET_EPOCH_WAIT(); 191275ee267cSGleb Smirnoff trunk_destroy(trunk); 1913d148c2a2SMatt Joras } 19141b2a4f7aSBill Fenner } 1915f731f104SBill Paul 1916f731f104SBill Paul /* Disconnect from parent. */ 19171cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 19181cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 19195cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 19205cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 19215cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1922f731f104SBill Paul 192322893351SJack F Vogel /* 192422893351SJack F Vogel * Only dispatch an event if vlan was 192522893351SJack F Vogel * attached, otherwise there is nothing 192622893351SJack F Vogel * to cleanup anyway. 192722893351SJack F Vogel */ 192822893351SJack F Vogel if (parent != NULL) 19297983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid); 1930f731f104SBill Paul } 1931f731f104SBill Paul 19321cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1933f731f104SBill Paul static int 19341cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 19351cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1936a3814acfSSam Leffler { 19371cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 19381cf236fbSYaroslav Tykhiy int error; 1939a3814acfSSam Leffler 1940d148c2a2SMatt Joras VLAN_SXLOCK_ASSERT(); 1941a3814acfSSam Leffler 19421cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 19431cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 19441cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 19451cf236fbSYaroslav Tykhiy 19461cf236fbSYaroslav Tykhiy /* 19471cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 19481cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 19491cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 19501cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 19511cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 19521cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 19531cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 19541cf236fbSYaroslav Tykhiy */ 19551cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 195675ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 19571cf236fbSYaroslav Tykhiy if (error) 1958a3814acfSSam Leffler return (error); 19591cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 19601cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 19611cf236fbSYaroslav Tykhiy } 19621cf236fbSYaroslav Tykhiy return (0); 19631cf236fbSYaroslav Tykhiy } 19641cf236fbSYaroslav Tykhiy 19651cf236fbSYaroslav Tykhiy /* 19661cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 19671cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 19681cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 19691cf236fbSYaroslav Tykhiy */ 19701cf236fbSYaroslav Tykhiy static int 19711cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 19721cf236fbSYaroslav Tykhiy { 19731cf236fbSYaroslav Tykhiy int error, i; 19741cf236fbSYaroslav Tykhiy 19751cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 19761cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 19771cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 19781cf236fbSYaroslav Tykhiy if (error) 19791cf236fbSYaroslav Tykhiy return (error); 19801cf236fbSYaroslav Tykhiy } 19811cf236fbSYaroslav Tykhiy return (0); 1982a3814acfSSam Leffler } 1983a3814acfSSam Leffler 1984127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1985127d7b2dSAndre Oppermann static void 1986a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp) 1987127d7b2dSAndre Oppermann { 1988b2807792SGleb Smirnoff struct epoch_tracker et; 1989d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1990127d7b2dSAndre Oppermann struct ifvlan *ifv; 1991127d7b2dSAndre Oppermann 1992b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 1993d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1994b2807792SGleb Smirnoff if (trunk == NULL) { 1995b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 1996d148c2a2SMatt Joras return; 1997b2807792SGleb Smirnoff } 1998d148c2a2SMatt Joras 1999d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 2000d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 2001aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 2002fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 200375ee267cSGleb Smirnoff trunk->parent->if_link_state); 2004127d7b2dSAndre Oppermann } 2005d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 2006b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 200775ee267cSGleb Smirnoff } 200875ee267cSGleb Smirnoff 200984abf7e2SKonstantin Belousov #ifdef IPSEC_OFFLOAD 201084abf7e2SKonstantin Belousov #define VLAN_IPSEC_METHOD(exp) \ 201184abf7e2SKonstantin Belousov if_t p; \ 201284abf7e2SKonstantin Belousov struct ifvlan *ifv; \ 201384abf7e2SKonstantin Belousov int error; \ 201484abf7e2SKonstantin Belousov \ 201584abf7e2SKonstantin Belousov ifv = ifp->if_softc; \ 201684abf7e2SKonstantin Belousov VLAN_SLOCK(); \ 201784abf7e2SKonstantin Belousov if (TRUNK(ifv) != NULL) { \ 201884abf7e2SKonstantin Belousov p = PARENT(ifv); \ 201984abf7e2SKonstantin Belousov if_ref(p); \ 202084abf7e2SKonstantin Belousov error = p->if_ipsec_accel_m->exp; \ 202184abf7e2SKonstantin Belousov if_rele(p); \ 202284abf7e2SKonstantin Belousov } else { \ 202384abf7e2SKonstantin Belousov error = ENXIO; \ 202484abf7e2SKonstantin Belousov } \ 202584abf7e2SKonstantin Belousov VLAN_SUNLOCK(); \ 202684abf7e2SKonstantin Belousov return (error); 202784abf7e2SKonstantin Belousov 202884abf7e2SKonstantin Belousov 202984abf7e2SKonstantin Belousov static int 203084abf7e2SKonstantin Belousov vlan_if_spdadd(if_t ifp, void *sp, void *inp, void **priv) 203184abf7e2SKonstantin Belousov { 203284abf7e2SKonstantin Belousov VLAN_IPSEC_METHOD(if_spdadd(ifp, sp, inp, priv)); 203384abf7e2SKonstantin Belousov } 203484abf7e2SKonstantin Belousov 203584abf7e2SKonstantin Belousov static int 203684abf7e2SKonstantin Belousov vlan_if_spddel(if_t ifp, void *sp, void *priv) 203784abf7e2SKonstantin Belousov { 203884abf7e2SKonstantin Belousov VLAN_IPSEC_METHOD(if_spddel(ifp, sp, priv)); 203984abf7e2SKonstantin Belousov } 204084abf7e2SKonstantin Belousov 204184abf7e2SKonstantin Belousov static int 204284abf7e2SKonstantin Belousov vlan_if_sa_newkey(if_t ifp, void *sav, u_int drv_spi, void **privp) 204384abf7e2SKonstantin Belousov { 204484abf7e2SKonstantin Belousov VLAN_IPSEC_METHOD(if_sa_newkey(ifp, sav, drv_spi, privp)); 204584abf7e2SKonstantin Belousov } 204684abf7e2SKonstantin Belousov 204784abf7e2SKonstantin Belousov static int 204884abf7e2SKonstantin Belousov vlan_if_sa_deinstall(if_t ifp, u_int drv_spi, void *priv) 204984abf7e2SKonstantin Belousov { 205084abf7e2SKonstantin Belousov VLAN_IPSEC_METHOD(if_sa_deinstall(ifp, drv_spi, priv)); 205184abf7e2SKonstantin Belousov } 205284abf7e2SKonstantin Belousov 205384abf7e2SKonstantin Belousov static int 205484abf7e2SKonstantin Belousov vlan_if_sa_cnt(if_t ifp, void *sa, uint32_t drv_spi, void *priv, 205584abf7e2SKonstantin Belousov struct seclifetime *lt) 205684abf7e2SKonstantin Belousov { 205784abf7e2SKonstantin Belousov VLAN_IPSEC_METHOD(if_sa_cnt(ifp, sa, drv_spi, priv, lt)); 205884abf7e2SKonstantin Belousov } 205984abf7e2SKonstantin Belousov 206084abf7e2SKonstantin Belousov static int 206184abf7e2SKonstantin Belousov vlan_if_ipsec_hwassist(if_t ifp, void *sav, u_int drv_spi,void *priv) 206284abf7e2SKonstantin Belousov { 206384abf7e2SKonstantin Belousov if_t trunk; 206484abf7e2SKonstantin Belousov 206584abf7e2SKonstantin Belousov NET_EPOCH_ASSERT(); 206684abf7e2SKonstantin Belousov trunk = vlan_trunkdev(ifp); 206784abf7e2SKonstantin Belousov if (trunk == NULL) 206884abf7e2SKonstantin Belousov return (0); 206984abf7e2SKonstantin Belousov return (trunk->if_ipsec_accel_m->if_hwassist(trunk, sav, 207084abf7e2SKonstantin Belousov drv_spi, priv)); 207184abf7e2SKonstantin Belousov } 207284abf7e2SKonstantin Belousov 207384abf7e2SKonstantin Belousov static const struct if_ipsec_accel_methods vlan_if_ipsec_accel_methods = { 207484abf7e2SKonstantin Belousov .if_spdadd = vlan_if_spdadd, 207584abf7e2SKonstantin Belousov .if_spddel = vlan_if_spddel, 207684abf7e2SKonstantin Belousov .if_sa_newkey = vlan_if_sa_newkey, 207784abf7e2SKonstantin Belousov .if_sa_deinstall = vlan_if_sa_deinstall, 207884abf7e2SKonstantin Belousov .if_sa_cnt = vlan_if_sa_cnt, 207984abf7e2SKonstantin Belousov .if_hwassist = vlan_if_ipsec_hwassist, 208084abf7e2SKonstantin Belousov }; 208184abf7e2SKonstantin Belousov 208284abf7e2SKonstantin Belousov #undef VLAN_IPSEC_METHOD 208384abf7e2SKonstantin Belousov #endif /* IPSEC_OFFLOAD */ 208484abf7e2SKonstantin Belousov 208575ee267cSGleb Smirnoff static void 208675ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 208775ee267cSGleb Smirnoff { 2088d148c2a2SMatt Joras struct ifnet *p; 2089d148c2a2SMatt Joras struct ifnet *ifp; 20909fd573c3SHans Petter Selasky struct ifnet_hw_tsomax hw_tsomax; 209184abf7e2SKonstantin Belousov int cap = 0, ena = 0, mena, cap2 = 0, ena2 = 0; 209284abf7e2SKonstantin Belousov int mena2 __unused; 2093d89baa5aSAlexander Motin u_long hwa = 0; 209475ee267cSGleb Smirnoff 2095a68cc388SGleb Smirnoff NET_EPOCH_ASSERT(); 2096b8a6e03fSGleb Smirnoff VLAN_SXLOCK_ASSERT(); 2097b8a6e03fSGleb Smirnoff 2098d148c2a2SMatt Joras p = PARENT(ifv); 2099d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 210075ee267cSGleb Smirnoff 2101d89baa5aSAlexander Motin /* Mask parent interface enabled capabilities disabled by user. */ 2102d89baa5aSAlexander Motin mena = p->if_capenable & ifv->ifv_capenable; 210384abf7e2SKonstantin Belousov mena2 = p->if_capenable2 & ifv->ifv_capenable2; 2104d89baa5aSAlexander Motin 210575ee267cSGleb Smirnoff /* 210675ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 210775ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 210875ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 210975ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 211075ee267cSGleb Smirnoff */ 211175ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 2112d89baa5aSAlexander Motin cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 211375ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 211475ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 2115d89baa5aSAlexander Motin ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 2116d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM) 2117d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP | 2118d89baa5aSAlexander Motin CSUM_UDP | CSUM_SCTP); 2119d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM_IPV6) 2120d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_TCP_IPV6 | 2121d89baa5aSAlexander Motin CSUM_UDP_IPV6 | CSUM_SCTP_IPV6); 212275ee267cSGleb Smirnoff } 2123d89baa5aSAlexander Motin 21249b76d9cbSPyun YongHyeon /* 21259b76d9cbSPyun YongHyeon * If the parent interface can do TSO on VLANs then 21269b76d9cbSPyun YongHyeon * propagate the hardware-assisted flag. TSO on VLANs 21279b76d9cbSPyun YongHyeon * does not necessarily require hardware VLAN tagging. 21289b76d9cbSPyun YongHyeon */ 21299fd573c3SHans Petter Selasky memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 21309fd573c3SHans Petter Selasky if_hw_tsomax_common(p, &hw_tsomax); 21319fd573c3SHans Petter Selasky if_hw_tsomax_update(ifp, &hw_tsomax); 21329b76d9cbSPyun YongHyeon if (p->if_capabilities & IFCAP_VLAN_HWTSO) 2133d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TSO; 21349b76d9cbSPyun YongHyeon if (p->if_capenable & IFCAP_VLAN_HWTSO) { 2135d89baa5aSAlexander Motin ena |= mena & IFCAP_TSO; 2136d89baa5aSAlexander Motin if (ena & IFCAP_TSO) 2137d89baa5aSAlexander Motin hwa |= p->if_hwassist & CSUM_TSO; 21389b76d9cbSPyun YongHyeon } 213909fe6320SNavdeep Parhar 214009fe6320SNavdeep Parhar /* 2141fb69ed39SKristof Provost * If the parent interface can do LRO and checksum offloading on 2142fb69ed39SKristof Provost * VLANs, then guess it may do LRO on VLANs. False positive here 2143fb69ed39SKristof Provost * cost nothing, while false negative may lead to some confusions. 214459150e91SAlexander Motin */ 214559150e91SAlexander Motin if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 214659150e91SAlexander Motin cap |= p->if_capabilities & IFCAP_LRO; 214759150e91SAlexander Motin if (p->if_capenable & IFCAP_VLAN_HWCSUM) 2148b1a39c31SKevin Bowling ena |= mena & IFCAP_LRO; 214959150e91SAlexander Motin 215059150e91SAlexander Motin /* 215109fe6320SNavdeep Parhar * If the parent interface can offload TCP connections over VLANs then 215209fe6320SNavdeep Parhar * propagate its TOE capability to the VLAN interface. 215309fe6320SNavdeep Parhar * 215409fe6320SNavdeep Parhar * All TOE drivers in the tree today can deal with VLANs. If this 215509fe6320SNavdeep Parhar * changes then IFCAP_VLAN_TOE should be promoted to a full capability 215609fe6320SNavdeep Parhar * with its own bit. 215709fe6320SNavdeep Parhar */ 215809fe6320SNavdeep Parhar #define IFCAP_VLAN_TOE IFCAP_TOE 215909fe6320SNavdeep Parhar if (p->if_capabilities & IFCAP_VLAN_TOE) 2160d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TOE; 216109fe6320SNavdeep Parhar if (p->if_capenable & IFCAP_VLAN_TOE) { 2162c255d1a4SJustin Hibbits SETTOEDEV(ifp, TOEDEV(p)); 2163d89baa5aSAlexander Motin ena |= mena & IFCAP_TOE; 216409fe6320SNavdeep Parhar } 2165f3e7afe2SHans Petter Selasky 2166d89baa5aSAlexander Motin /* 2167d89baa5aSAlexander Motin * If the parent interface supports dynamic link state, so does the 2168d89baa5aSAlexander Motin * VLAN interface. 2169d89baa5aSAlexander Motin */ 2170d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_LINKSTATE); 2171d89baa5aSAlexander Motin ena |= (mena & IFCAP_LINKSTATE); 2172d89baa5aSAlexander Motin 2173f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 2174f3e7afe2SHans Petter Selasky /* 2175f3e7afe2SHans Petter Selasky * If the parent interface supports ratelimiting, so does the 2176f3e7afe2SHans Petter Selasky * VLAN interface. 2177f3e7afe2SHans Petter Selasky */ 2178d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_TXRTLMT); 2179d89baa5aSAlexander Motin ena |= (mena & IFCAP_TXRTLMT); 2180f3e7afe2SHans Petter Selasky #endif 2181d89baa5aSAlexander Motin 218266d0c056SJohn Baldwin /* 218366d0c056SJohn Baldwin * If the parent interface supports unmapped mbufs, so does 218466d0c056SJohn Baldwin * the VLAN interface. Note that this should be fine even for 218566d0c056SJohn Baldwin * interfaces that don't support hardware tagging as headers 218666d0c056SJohn Baldwin * are prepended in normal mbufs to unmapped mbufs holding 218766d0c056SJohn Baldwin * payload data. 218866d0c056SJohn Baldwin */ 21893f43ada9SGleb Smirnoff cap |= (p->if_capabilities & IFCAP_MEXTPG); 21903f43ada9SGleb Smirnoff ena |= (mena & IFCAP_MEXTPG); 219166d0c056SJohn Baldwin 2192b2e60773SJohn Baldwin /* 2193b2e60773SJohn Baldwin * If the parent interface can offload encryption and segmentation 2194b2e60773SJohn Baldwin * of TLS records over TCP, propagate it's capability to the VLAN 2195b2e60773SJohn Baldwin * interface. 2196b2e60773SJohn Baldwin * 2197b2e60773SJohn Baldwin * All TLS drivers in the tree today can deal with VLANs. If 2198b2e60773SJohn Baldwin * this ever changes, then a new IFCAP_VLAN_TXTLS can be 2199b2e60773SJohn Baldwin * defined. 2200b2e60773SJohn Baldwin */ 2201521eac97SJohn Baldwin if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 2202521eac97SJohn Baldwin cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 2203521eac97SJohn Baldwin if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT)) 2204521eac97SJohn Baldwin ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT); 2205b2e60773SJohn Baldwin 2206d89baa5aSAlexander Motin ifp->if_capabilities = cap; 2207d89baa5aSAlexander Motin ifp->if_capenable = ena; 2208d89baa5aSAlexander Motin ifp->if_hwassist = hwa; 220984abf7e2SKonstantin Belousov 221084abf7e2SKonstantin Belousov #ifdef IPSEC_OFFLOAD 221184abf7e2SKonstantin Belousov cap2 |= p->if_capabilities2 & IFCAP2_BIT(IFCAP2_IPSEC_OFFLOAD); 221284abf7e2SKonstantin Belousov ena2 |= mena2 & IFCAP2_BIT(IFCAP2_IPSEC_OFFLOAD); 221384abf7e2SKonstantin Belousov ifp->if_ipsec_accel_m = &vlan_if_ipsec_accel_methods; 221484abf7e2SKonstantin Belousov #endif 2215*828445ccSKonstantin Belousov 2216*828445ccSKonstantin Belousov ifp->if_capabilities2 = cap2; 2217*828445ccSKonstantin Belousov ifp->if_capenable2 = ena2; 221875ee267cSGleb Smirnoff } 221975ee267cSGleb Smirnoff 222075ee267cSGleb Smirnoff static void 222175ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 222275ee267cSGleb Smirnoff { 2223b2807792SGleb Smirnoff struct epoch_tracker et; 2224d148c2a2SMatt Joras struct ifvlantrunk *trunk; 222575ee267cSGleb Smirnoff struct ifvlan *ifv; 222675ee267cSGleb Smirnoff 2227d148c2a2SMatt Joras VLAN_SLOCK(); 2228d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 2229d148c2a2SMatt Joras if (trunk == NULL) { 2230d148c2a2SMatt Joras VLAN_SUNLOCK(); 2231d148c2a2SMatt Joras return; 2232d148c2a2SMatt Joras } 2233b2807792SGleb Smirnoff NET_EPOCH_ENTER(et); 2234b8a6e03fSGleb Smirnoff VLAN_FOREACH(ifv, trunk) 223575ee267cSGleb Smirnoff vlan_capabilities(ifv); 2236b2807792SGleb Smirnoff NET_EPOCH_EXIT(et); 2237d148c2a2SMatt Joras VLAN_SUNLOCK(); 2238127d7b2dSAndre Oppermann } 2239127d7b2dSAndre Oppermann 2240a3814acfSSam Leffler static int 2241cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 22422cc2df49SGarrett Wollman { 22432cc2df49SGarrett Wollman struct ifnet *p; 22442cc2df49SGarrett Wollman struct ifreq *ifr; 22452884a936SJohn Baldwin #ifdef INET 2246e4cd31ddSJeff Roberson struct ifaddr *ifa; 22472884a936SJohn Baldwin #endif 22482cc2df49SGarrett Wollman struct ifvlan *ifv; 22492d222cb7SAlexander Motin struct ifvlantrunk *trunk; 22502cc2df49SGarrett Wollman struct vlanreq vlr; 225184becee1SAlexander Motin int error = 0, oldmtu; 22522cc2df49SGarrett Wollman 22532cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 22542884a936SJohn Baldwin #ifdef INET 2255e4cd31ddSJeff Roberson ifa = (struct ifaddr *) data; 22562884a936SJohn Baldwin #endif 22572cc2df49SGarrett Wollman ifv = ifp->if_softc; 22582cc2df49SGarrett Wollman 22592cc2df49SGarrett Wollman switch (cmd) { 2260e4cd31ddSJeff Roberson case SIOCSIFADDR: 2261e4cd31ddSJeff Roberson ifp->if_flags |= IFF_UP; 2262e4cd31ddSJeff Roberson #ifdef INET 2263e4cd31ddSJeff Roberson if (ifa->ifa_addr->sa_family == AF_INET) 2264e4cd31ddSJeff Roberson arp_ifinit(ifp, ifa); 2265e4cd31ddSJeff Roberson #endif 2266e4cd31ddSJeff Roberson break; 2267e4cd31ddSJeff Roberson case SIOCGIFADDR: 226838d958a6SBrooks Davis bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 226938d958a6SBrooks Davis ifp->if_addrlen); 2270e4cd31ddSJeff Roberson break; 2271b3cca108SBill Fenner case SIOCGIFMEDIA: 2272d148c2a2SMatt Joras VLAN_SLOCK(); 227375ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 2274d8564efdSEd Maste p = PARENT(ifv); 22759bcf3ae4SAlexander Motin if_ref(p); 2276d8564efdSEd Maste error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 22779bcf3ae4SAlexander Motin if_rele(p); 2278b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 2279b3cca108SBill Fenner if (error == 0) { 2280b3cca108SBill Fenner struct ifmediareq *ifmr; 2281b3cca108SBill Fenner 2282b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 2283b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 2284b3cca108SBill Fenner ifmr->ifm_count = 1; 2285b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 2286b3cca108SBill Fenner ifmr->ifm_ulist, 2287b3cca108SBill Fenner sizeof(int)); 2288b3cca108SBill Fenner } 2289b3cca108SBill Fenner } 22904faedfe8SSam Leffler } else { 2291b3cca108SBill Fenner error = EINVAL; 22924faedfe8SSam Leffler } 2293d148c2a2SMatt Joras VLAN_SUNLOCK(); 2294b3cca108SBill Fenner break; 2295b3cca108SBill Fenner 2296b3cca108SBill Fenner case SIOCSIFMEDIA: 2297b3cca108SBill Fenner error = EINVAL; 2298b3cca108SBill Fenner break; 2299b3cca108SBill Fenner 23002cc2df49SGarrett Wollman case SIOCSIFMTU: 23012cc2df49SGarrett Wollman /* 23022cc2df49SGarrett Wollman * Set the interface MTU. 23032cc2df49SGarrett Wollman */ 2304d148c2a2SMatt Joras VLAN_SLOCK(); 2305d148c2a2SMatt Joras trunk = TRUNK(ifv); 2306d148c2a2SMatt Joras if (trunk != NULL) { 2307d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 2308a3814acfSSam Leffler if (ifr->ifr_mtu > 230975ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 2310a3814acfSSam Leffler ifr->ifr_mtu < 2311a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 23122cc2df49SGarrett Wollman error = EINVAL; 2313a3814acfSSam Leffler else 23142cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 2315d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 2316a3814acfSSam Leffler } else 2317a3814acfSSam Leffler error = EINVAL; 2318d148c2a2SMatt Joras VLAN_SUNLOCK(); 23192cc2df49SGarrett Wollman break; 23202cc2df49SGarrett Wollman 23212cc2df49SGarrett Wollman case SIOCSETVLAN: 2322ccf7ba97SMarko Zec #ifdef VIMAGE 232315f6780eSRobert Watson /* 232415f6780eSRobert Watson * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN 232515f6780eSRobert Watson * interface to be delegated to a jail without allowing the 232615f6780eSRobert Watson * jail to change what underlying interface/VID it is 232715f6780eSRobert Watson * associated with. We are not entirely convinced that this 23285a39f779SRobert Watson * is the right way to accomplish that policy goal. 232915f6780eSRobert Watson */ 2330ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 2331ccf7ba97SMarko Zec error = EPERM; 2332ccf7ba97SMarko Zec break; 2333ccf7ba97SMarko Zec } 2334ccf7ba97SMarko Zec #endif 2335541d96aaSBrooks Davis error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr)); 23362cc2df49SGarrett Wollman if (error) 23372cc2df49SGarrett Wollman break; 23382cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 2339f731f104SBill Paul vlan_unconfig(ifp); 23402cc2df49SGarrett Wollman break; 23412cc2df49SGarrett Wollman } 23429bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 23431bdc73d3SEd Maste if (p == NULL) { 23442cc2df49SGarrett Wollman error = ENOENT; 23452cc2df49SGarrett Wollman break; 23462cc2df49SGarrett Wollman } 2347afbb64f1SAlexander V. Chernikov if (vlr.vlr_proto == 0) 2348afbb64f1SAlexander V. Chernikov vlr.vlr_proto = ETHERTYPE_VLAN; 234984becee1SAlexander Motin oldmtu = ifp->if_mtu; 2350c7cffd65SAlexander V. Chernikov error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto); 23519bcf3ae4SAlexander Motin if_rele(p); 235284becee1SAlexander Motin 235384becee1SAlexander Motin /* 235484becee1SAlexander Motin * VLAN MTU may change during addition of the vlandev. 235584becee1SAlexander Motin * If it did, do network layer specific procedure. 235684becee1SAlexander Motin */ 235766bdbcd5SAlexander V. Chernikov if (ifp->if_mtu != oldmtu) 235866bdbcd5SAlexander V. Chernikov if_notifymtu(ifp); 23592cc2df49SGarrett Wollman break; 23602cc2df49SGarrett Wollman 23612cc2df49SGarrett Wollman case SIOCGETVLAN: 2362ccf7ba97SMarko Zec #ifdef VIMAGE 2363ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 2364ccf7ba97SMarko Zec error = EPERM; 2365ccf7ba97SMarko Zec break; 2366ccf7ba97SMarko Zec } 2367ccf7ba97SMarko Zec #endif 236815a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 2369d148c2a2SMatt Joras VLAN_SLOCK(); 237075ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 237175ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 23729bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 23737983103aSRobert Watson vlr.vlr_tag = ifv->ifv_vid; 2374c7cffd65SAlexander V. Chernikov vlr.vlr_proto = ifv->ifv_proto; 23752cc2df49SGarrett Wollman } 2376d148c2a2SMatt Joras VLAN_SUNLOCK(); 2377541d96aaSBrooks Davis error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr)); 23782cc2df49SGarrett Wollman break; 23792cc2df49SGarrett Wollman 23802cc2df49SGarrett Wollman case SIOCSIFFLAGS: 23812cc2df49SGarrett Wollman /* 23821cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 23831cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 23842cc2df49SGarrett Wollman */ 238592c23f6dSKristof Provost VLAN_SLOCK(); 238675ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 23871cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 238892c23f6dSKristof Provost VLAN_SUNLOCK(); 23892cc2df49SGarrett Wollman break; 2390a3814acfSSam Leffler 2391f731f104SBill Paul case SIOCADDMULTI: 2392f731f104SBill Paul case SIOCDELMULTI: 239375ee267cSGleb Smirnoff /* 239475ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 239575ee267cSGleb Smirnoff * when we do. 2396d148c2a2SMatt Joras * 2397d148c2a2SMatt Joras * XXX We need the rmlock here to avoid sleeping while 2398d148c2a2SMatt Joras * holding in6_multi_mtx. 239975ee267cSGleb Smirnoff */ 2400b08d611dSMatt Macy VLAN_XLOCK(); 24012d222cb7SAlexander Motin trunk = TRUNK(ifv); 2402b08d611dSMatt Macy if (trunk != NULL) 2403f731f104SBill Paul error = vlan_setmulti(ifp); 2404b08d611dSMatt Macy VLAN_XUNLOCK(); 240575ee267cSGleb Smirnoff 2406b08d611dSMatt Macy break; 24072ccbbd06SMarcelo Araujo case SIOCGVLANPCP: 24082ccbbd06SMarcelo Araujo #ifdef VIMAGE 24092ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 24102ccbbd06SMarcelo Araujo error = EPERM; 24112ccbbd06SMarcelo Araujo break; 24122ccbbd06SMarcelo Araujo } 24132ccbbd06SMarcelo Araujo #endif 24142ccbbd06SMarcelo Araujo ifr->ifr_vlan_pcp = ifv->ifv_pcp; 24152ccbbd06SMarcelo Araujo break; 24162ccbbd06SMarcelo Araujo 24172ccbbd06SMarcelo Araujo case SIOCSVLANPCP: 24182ccbbd06SMarcelo Araujo #ifdef VIMAGE 24192ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 24202ccbbd06SMarcelo Araujo error = EPERM; 24212ccbbd06SMarcelo Araujo break; 24222ccbbd06SMarcelo Araujo } 24232ccbbd06SMarcelo Araujo #endif 24242ccbbd06SMarcelo Araujo error = priv_check(curthread, PRIV_NET_SETVLANPCP); 24252ccbbd06SMarcelo Araujo if (error) 24262ccbbd06SMarcelo Araujo break; 24279ef8cd0bSKristof Provost if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) { 24282ccbbd06SMarcelo Araujo error = EINVAL; 24292ccbbd06SMarcelo Araujo break; 24302ccbbd06SMarcelo Araujo } 24312ccbbd06SMarcelo Araujo ifv->ifv_pcp = ifr->ifr_vlan_pcp; 243232a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 24334a381a9eSHans Petter Selasky /* broadcast event about PCP change */ 24344a381a9eSHans Petter Selasky EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 24352ccbbd06SMarcelo Araujo break; 24362ccbbd06SMarcelo Araujo 2437d89baa5aSAlexander Motin case SIOCSIFCAP: 2438d148c2a2SMatt Joras VLAN_SLOCK(); 2439d89baa5aSAlexander Motin ifv->ifv_capenable = ifr->ifr_reqcap; 2440d89baa5aSAlexander Motin trunk = TRUNK(ifv); 24416dcec895SGleb Smirnoff if (trunk != NULL) { 24426dcec895SGleb Smirnoff struct epoch_tracker et; 24436dcec895SGleb Smirnoff 24446dcec895SGleb Smirnoff NET_EPOCH_ENTER(et); 2445d89baa5aSAlexander Motin vlan_capabilities(ifv); 24466dcec895SGleb Smirnoff NET_EPOCH_EXIT(et); 24476dcec895SGleb Smirnoff } 2448d148c2a2SMatt Joras VLAN_SUNLOCK(); 2449d89baa5aSAlexander Motin break; 2450d89baa5aSAlexander Motin 24512cc2df49SGarrett Wollman default: 2452e4cd31ddSJeff Roberson error = EINVAL; 2453e4cd31ddSJeff Roberson break; 24542cc2df49SGarrett Wollman } 245515a66c21SBruce M Simpson 245615a66c21SBruce M Simpson return (error); 24572cc2df49SGarrett Wollman } 2458f3e7afe2SHans Petter Selasky 2459b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT) 2460f3e7afe2SHans Petter Selasky static int 2461f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp, 2462f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *params, 2463f3e7afe2SHans Petter Selasky struct m_snd_tag **ppmt) 2464f3e7afe2SHans Petter Selasky { 2465fb3bc596SJohn Baldwin struct epoch_tracker et; 2466c782ea8bSJohn Baldwin const struct if_snd_tag_sw *sw; 2467fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2468fb3bc596SJohn Baldwin struct ifvlan *ifv; 2469fb3bc596SJohn Baldwin struct ifnet *parent; 2470892eded5SHans Petter Selasky struct m_snd_tag *mst; 2471fb3bc596SJohn Baldwin int error; 2472f3e7afe2SHans Petter Selasky 2473892eded5SHans Petter Selasky NET_EPOCH_ENTER(et); 2474892eded5SHans Petter Selasky ifv = ifp->if_softc; 2475892eded5SHans Petter Selasky 2476c782ea8bSJohn Baldwin switch (params->hdr.type) { 2477c782ea8bSJohn Baldwin #ifdef RATELIMIT 2478c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_UNLIMITED: 2479c782ea8bSJohn Baldwin sw = &vlan_snd_tag_ul_sw; 2480c782ea8bSJohn Baldwin break; 2481c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_RATE_LIMIT: 2482c782ea8bSJohn Baldwin sw = &vlan_snd_tag_rl_sw; 2483c782ea8bSJohn Baldwin break; 2484c782ea8bSJohn Baldwin #endif 2485c782ea8bSJohn Baldwin #ifdef KERN_TLS 2486c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_TLS: 2487c782ea8bSJohn Baldwin sw = &vlan_snd_tag_tls_sw; 2488c782ea8bSJohn Baldwin break; 2489892eded5SHans Petter Selasky case IF_SND_TAG_TYPE_TLS_RX: 2490892eded5SHans Petter Selasky sw = NULL; 2491892eded5SHans Petter Selasky if (params->tls_rx.vlan_id != 0) 2492892eded5SHans Petter Selasky goto failure; 2493892eded5SHans Petter Selasky params->tls_rx.vlan_id = ifv->ifv_vid; 2494892eded5SHans Petter Selasky break; 2495c782ea8bSJohn Baldwin #ifdef RATELIMIT 2496c782ea8bSJohn Baldwin case IF_SND_TAG_TYPE_TLS_RATE_LIMIT: 2497c782ea8bSJohn Baldwin sw = &vlan_snd_tag_tls_rl_sw; 2498c782ea8bSJohn Baldwin break; 2499c782ea8bSJohn Baldwin #endif 2500c782ea8bSJohn Baldwin #endif 2501c782ea8bSJohn Baldwin default: 2502892eded5SHans Petter Selasky goto failure; 2503c782ea8bSJohn Baldwin } 2504c782ea8bSJohn Baldwin 2505fb3bc596SJohn Baldwin if (ifv->ifv_trunk != NULL) 2506fb3bc596SJohn Baldwin parent = PARENT(ifv); 2507fb3bc596SJohn Baldwin else 2508fb3bc596SJohn Baldwin parent = NULL; 2509892eded5SHans Petter Selasky if (parent == NULL) 2510892eded5SHans Petter Selasky goto failure; 2511fb3bc596SJohn Baldwin if_ref(parent); 2512fb3bc596SJohn Baldwin NET_EPOCH_EXIT(et); 2513fb3bc596SJohn Baldwin 2514892eded5SHans Petter Selasky if (sw != NULL) { 2515fb3bc596SJohn Baldwin vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT); 2516fb3bc596SJohn Baldwin if (vst == NULL) { 2517fb3bc596SJohn Baldwin if_rele(parent); 2518fb3bc596SJohn Baldwin return (ENOMEM); 2519fb3bc596SJohn Baldwin } 2520892eded5SHans Petter Selasky } else 2521892eded5SHans Petter Selasky vst = NULL; 2522fb3bc596SJohn Baldwin 2523892eded5SHans Petter Selasky error = m_snd_tag_alloc(parent, params, &mst); 2524fb3bc596SJohn Baldwin if_rele(parent); 2525fb3bc596SJohn Baldwin if (error) { 2526fb3bc596SJohn Baldwin free(vst, M_VLAN); 2527fb3bc596SJohn Baldwin return (error); 2528fb3bc596SJohn Baldwin } 2529fb3bc596SJohn Baldwin 2530892eded5SHans Petter Selasky if (sw != NULL) { 2531c782ea8bSJohn Baldwin m_snd_tag_init(&vst->com, ifp, sw); 2532892eded5SHans Petter Selasky vst->tag = mst; 2533fb3bc596SJohn Baldwin 2534fb3bc596SJohn Baldwin *ppmt = &vst->com; 2535892eded5SHans Petter Selasky } else 2536892eded5SHans Petter Selasky *ppmt = mst; 2537892eded5SHans Petter Selasky 2538fb3bc596SJohn Baldwin return (0); 2539892eded5SHans Petter Selasky failure: 2540892eded5SHans Petter Selasky NET_EPOCH_EXIT(et); 2541892eded5SHans Petter Selasky return (EOPNOTSUPP); 2542fb3bc596SJohn Baldwin } 2543fb3bc596SJohn Baldwin 25441a714ff2SRandall Stewart static struct m_snd_tag * 25451a714ff2SRandall Stewart vlan_next_snd_tag(struct m_snd_tag *mst) 25461a714ff2SRandall Stewart { 25471a714ff2SRandall Stewart struct vlan_snd_tag *vst; 25481a714ff2SRandall Stewart 25491a714ff2SRandall Stewart vst = mst_to_vst(mst); 25501a714ff2SRandall Stewart return (vst->tag); 25511a714ff2SRandall Stewart } 25521a714ff2SRandall Stewart 2553fb3bc596SJohn Baldwin static int 2554fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst, 2555fb3bc596SJohn Baldwin union if_snd_tag_modify_params *params) 2556fb3bc596SJohn Baldwin { 2557fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2558fb3bc596SJohn Baldwin 2559fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2560c782ea8bSJohn Baldwin return (vst->tag->sw->snd_tag_modify(vst->tag, params)); 2561fb3bc596SJohn Baldwin } 2562fb3bc596SJohn Baldwin 2563fb3bc596SJohn Baldwin static int 2564fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst, 2565fb3bc596SJohn Baldwin union if_snd_tag_query_params *params) 2566fb3bc596SJohn Baldwin { 2567fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2568fb3bc596SJohn Baldwin 2569fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2570c782ea8bSJohn Baldwin return (vst->tag->sw->snd_tag_query(vst->tag, params)); 2571f3e7afe2SHans Petter Selasky } 2572fa91f845SRandall Stewart 2573fa91f845SRandall Stewart static void 2574fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst) 2575fa91f845SRandall Stewart { 2576fb3bc596SJohn Baldwin struct vlan_snd_tag *vst; 2577fb3bc596SJohn Baldwin 2578fb3bc596SJohn Baldwin vst = mst_to_vst(mst); 2579fb3bc596SJohn Baldwin m_snd_tag_rele(vst->tag); 2580fb3bc596SJohn Baldwin free(vst, M_VLAN); 2581fa91f845SRandall Stewart } 25821a714ff2SRandall Stewart 25831a714ff2SRandall Stewart static void 25841a714ff2SRandall Stewart vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q) 25851a714ff2SRandall Stewart { 25861a714ff2SRandall Stewart /* 25871a714ff2SRandall Stewart * For vlan, we have an indirect 25881a714ff2SRandall Stewart * interface. The caller needs to 25891a714ff2SRandall Stewart * get a ratelimit tag on the actual 25901a714ff2SRandall Stewart * interface the flow will go on. 25911a714ff2SRandall Stewart */ 25921a714ff2SRandall Stewart q->rate_table = NULL; 25931a714ff2SRandall Stewart q->flags = RT_IS_INDIRECT; 25941a714ff2SRandall Stewart q->max_flows = 0; 25951a714ff2SRandall Stewart q->number_of_rates = 0; 25961a714ff2SRandall Stewart } 25971a714ff2SRandall Stewart 2598f3e7afe2SHans Petter Selasky #endif 2599