1c398230bSWarner Losh /*- 22cc2df49SGarrett Wollman * Copyright 1998 Massachusetts Institute of Technology 32ccbbd06SMarcelo Araujo * Copyright 2012 ADARA Networks, Inc. 4d148c2a2SMatt Joras * Copyright 2017 Dell EMC Isilon 52ccbbd06SMarcelo Araujo * 62ccbbd06SMarcelo Araujo * Portions of this software were developed by Robert N. M. Watson under 72ccbbd06SMarcelo Araujo * contract to ADARA Networks, Inc. 82cc2df49SGarrett Wollman * 92cc2df49SGarrett Wollman * Permission to use, copy, modify, and distribute this software and 102cc2df49SGarrett Wollman * its documentation for any purpose and without fee is hereby 112cc2df49SGarrett Wollman * granted, provided that both the above copyright notice and this 122cc2df49SGarrett Wollman * permission notice appear in all copies, that both the above 132cc2df49SGarrett Wollman * copyright notice and this permission notice appear in all 142cc2df49SGarrett Wollman * supporting documentation, and that the name of M.I.T. not be used 152cc2df49SGarrett Wollman * in advertising or publicity pertaining to distribution of the 162cc2df49SGarrett Wollman * software without specific, written prior permission. M.I.T. makes 172cc2df49SGarrett Wollman * no representations about the suitability of this software for any 182cc2df49SGarrett Wollman * purpose. It is provided "as is" without express or implied 192cc2df49SGarrett Wollman * warranty. 202cc2df49SGarrett Wollman * 212cc2df49SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 222cc2df49SGarrett Wollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 232cc2df49SGarrett Wollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 242cc2df49SGarrett Wollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 252cc2df49SGarrett Wollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 262cc2df49SGarrett Wollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 272cc2df49SGarrett Wollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 282cc2df49SGarrett Wollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 292cc2df49SGarrett Wollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 302cc2df49SGarrett Wollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 312cc2df49SGarrett Wollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 322cc2df49SGarrett Wollman * SUCH DAMAGE. 332cc2df49SGarrett Wollman */ 342cc2df49SGarrett Wollman 352cc2df49SGarrett Wollman /* 362cc2df49SGarrett Wollman * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 372ccbbd06SMarcelo Araujo * This is sort of sneaky in the implementation, since 382cc2df49SGarrett Wollman * we need to pretend to be enough of an Ethernet implementation 392cc2df49SGarrett Wollman * to make arp work. The way we do this is by telling everyone 402cc2df49SGarrett Wollman * that we are an Ethernet, and then catch the packets that 41d9b1d615SJohn Baldwin * ether_output() sends to us via if_transmit(), rewrite them for 42d9b1d615SJohn Baldwin * use by the real outgoing interface, and ask it to send them. 432cc2df49SGarrett Wollman */ 442cc2df49SGarrett Wollman 458b2d9181SPyun YongHyeon #include <sys/cdefs.h> 468b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$"); 478b2d9181SPyun YongHyeon 482c5b403eSOleg Bulyzhin #include "opt_inet.h" 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> 73f889d2efSBrooks Davis #include <net/if_clone.h> 742cc2df49SGarrett Wollman #include <net/if_dl.h> 752cc2df49SGarrett Wollman #include <net/if_types.h> 762cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 774b79449eSBjoern A. Zeeb #include <net/vnet.h> 782cc2df49SGarrett Wollman 792c5b403eSOleg Bulyzhin #ifdef INET 802c5b403eSOleg Bulyzhin #include <netinet/in.h> 812c5b403eSOleg Bulyzhin #include <netinet/if_ether.h> 822c5b403eSOleg Bulyzhin #endif 832c5b403eSOleg Bulyzhin 8475ee267cSGleb Smirnoff #define VLAN_DEF_HWIDTH 4 8564a17d2eSYaroslav Tykhiy #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 8675ee267cSGleb Smirnoff 872dc879b3SYaroslav Tykhiy #define UP_AND_RUNNING(ifp) \ 882dc879b3SYaroslav Tykhiy ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 892dc879b3SYaroslav Tykhiy 9075ee267cSGleb Smirnoff LIST_HEAD(ifvlanhead, ifvlan); 9175ee267cSGleb Smirnoff 9275ee267cSGleb Smirnoff struct ifvlantrunk { 9375ee267cSGleb Smirnoff struct ifnet *parent; /* parent interface of this trunk */ 94772b000fSAlexander V. Chernikov struct rmlock lock; 9575ee267cSGleb Smirnoff #ifdef VLAN_ARRAY 965cb8c31aSYaroslav Tykhiy #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 975cb8c31aSYaroslav Tykhiy struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 9875ee267cSGleb Smirnoff #else 9975ee267cSGleb Smirnoff struct ifvlanhead *hash; /* dynamic hash-list table */ 10075ee267cSGleb Smirnoff uint16_t hmask; 10175ee267cSGleb Smirnoff uint16_t hwidth; 10275ee267cSGleb Smirnoff #endif 10375ee267cSGleb Smirnoff int refcnt; 10475ee267cSGleb Smirnoff }; 1059d4fe4b2SBrooks Davis 106d148c2a2SMatt Joras /* 107d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk with 108d148c2a2SMatt Joras * the assumption that none will be added/removed during iteration. 109d148c2a2SMatt Joras */ 110d148c2a2SMatt Joras #ifdef VLAN_ARRAY 111d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 112d148c2a2SMatt Joras size_t _i; \ 113d148c2a2SMatt Joras for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \ 114d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i]) != NULL) 115d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 116d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \ 117d148c2a2SMatt Joras struct ifvlan *_next; \ 118d148c2a2SMatt Joras size_t _i; \ 119d148c2a2SMatt Joras for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \ 120d148c2a2SMatt Joras LIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next) 121d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 122d148c2a2SMatt Joras 123d148c2a2SMatt Joras /* 124d148c2a2SMatt Joras * This macro provides a facility to iterate over every vlan on a trunk while 125d148c2a2SMatt Joras * also modifying the number of vlans on the trunk. The iteration continues 126d148c2a2SMatt Joras * until some condition is met or there are no more vlans on the trunk. 127d148c2a2SMatt Joras */ 128d148c2a2SMatt Joras #ifdef VLAN_ARRAY 129d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */ 130d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 131d148c2a2SMatt Joras size_t _i; \ 132d148c2a2SMatt Joras for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \ 133d148c2a2SMatt Joras if (((_ifv) = (_trunk)->vlans[_i])) 134d148c2a2SMatt Joras #else /* VLAN_ARRAY */ 135d148c2a2SMatt Joras /* 136d148c2a2SMatt Joras * The hash table case is more complicated. We allow for the hash table to be 137d148c2a2SMatt Joras * modified (i.e. vlans removed) while we are iterating over it. To allow for 138d148c2a2SMatt Joras * this we must restart the iteration every time we "touch" something during 139d148c2a2SMatt Joras * the iteration, since removal will resize the hash table and invalidate our 140d148c2a2SMatt Joras * current position. If acting on the touched element causes the trunk to be 141d148c2a2SMatt Joras * emptied, then iteration also stops. 142d148c2a2SMatt Joras */ 143d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \ 144d148c2a2SMatt Joras size_t _i; \ 145d148c2a2SMatt Joras bool _touch = false; \ 146d148c2a2SMatt Joras for (_i = 0; \ 147d148c2a2SMatt Joras !(_cond) && _i < (1 << (_trunk)->hwidth); \ 148d148c2a2SMatt Joras _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \ 149d148c2a2SMatt Joras if (((_ifv) = LIST_FIRST(&(_trunk)->hash[_i])) != NULL && \ 150d148c2a2SMatt Joras (_touch = true)) 151d148c2a2SMatt Joras #endif /* VLAN_ARRAY */ 152d148c2a2SMatt Joras 153a3814acfSSam Leffler struct vlan_mc_entry { 154e4cd31ddSJeff Roberson struct sockaddr_dl mc_addr; 155a3814acfSSam Leffler SLIST_ENTRY(vlan_mc_entry) mc_entries; 156a3814acfSSam Leffler }; 157a3814acfSSam Leffler 158a3814acfSSam Leffler struct ifvlan { 15975ee267cSGleb Smirnoff struct ifvlantrunk *ifv_trunk; 160fc74a9f9SBrooks Davis struct ifnet *ifv_ifp; 16175ee267cSGleb Smirnoff #define TRUNK(ifv) ((ifv)->ifv_trunk) 16275ee267cSGleb Smirnoff #define PARENT(ifv) ((ifv)->ifv_trunk->parent) 1636667db31SAlexander V. Chernikov void *ifv_cookie; 1641cf236fbSYaroslav Tykhiy int ifv_pflags; /* special flags we have set on parent */ 165d89baa5aSAlexander Motin int ifv_capenable; 166a3814acfSSam Leffler struct ifv_linkmib { 167a3814acfSSam Leffler int ifvm_encaplen; /* encapsulation length */ 168a3814acfSSam Leffler int ifvm_mtufudge; /* MTU fudged by this much */ 169a3814acfSSam Leffler int ifvm_mintu; /* min transmission unit */ 17073f2233dSYaroslav Tykhiy uint16_t ifvm_proto; /* encapsulation ethertype */ 17175ee267cSGleb Smirnoff uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 1722ccbbd06SMarcelo Araujo uint16_t ifvm_vid; /* VLAN ID */ 1732ccbbd06SMarcelo Araujo uint8_t ifvm_pcp; /* Priority Code Point (PCP). */ 174a3814acfSSam Leffler } ifv_mib; 175d148c2a2SMatt Joras struct task lladdr_task; 176114c608cSYaroslav Tykhiy SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 177c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY 178a3814acfSSam Leffler LIST_ENTRY(ifvlan) ifv_list; 179c0cb022bSYaroslav Tykhiy #endif 180a3814acfSSam Leffler }; 18173f2233dSYaroslav Tykhiy #define ifv_proto ifv_mib.ifvm_proto 1822ccbbd06SMarcelo Araujo #define ifv_tag ifv_mib.ifvm_tag 1832ccbbd06SMarcelo Araujo #define ifv_vid ifv_mib.ifvm_vid 1842ccbbd06SMarcelo Araujo #define ifv_pcp ifv_mib.ifvm_pcp 185a3814acfSSam Leffler #define ifv_encaplen ifv_mib.ifvm_encaplen 186a3814acfSSam Leffler #define ifv_mtufudge ifv_mib.ifvm_mtufudge 187a3814acfSSam Leffler #define ifv_mintu ifv_mib.ifvm_mintu 188a3814acfSSam Leffler 18975ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */ 1901cf236fbSYaroslav Tykhiy static struct { 1911cf236fbSYaroslav Tykhiy int flag; 1921cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 1931cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 1941cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 1951cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 1961cf236fbSYaroslav Tykhiy {0, NULL} 1971cf236fbSYaroslav Tykhiy }; 198a3814acfSSam Leffler 199f1379734SKonstantin Belousov extern int vlan_mtag_pcp; 2002ccbbd06SMarcelo Araujo 20142a58907SGleb Smirnoff static const char vlanname[] = "vlan"; 20242a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface"); 2032cc2df49SGarrett Wollman 2045cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag; 205ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag; 2065cb8c31aSYaroslav Tykhiy 2074faedfe8SSam Leffler /* 208d148c2a2SMatt Joras * if_vlan uses two module-level locks to allow concurrent modification of vlan 209d148c2a2SMatt Joras * interfaces and (mostly) allow for vlans to be destroyed while they are being 210d148c2a2SMatt Joras * used for tx/rx. To accomplish this in a way that has acceptable performance 211d148c2a2SMatt Joras * and cooperation with other parts of the network stack there is a 212d148c2a2SMatt Joras * non-sleepable rmlock(9) and an sx(9). Both locks are exclusively acquired 213d148c2a2SMatt Joras * when destroying a vlan interface, i.e. when the if_vlantrunk field of struct 214d148c2a2SMatt Joras * ifnet is de-allocated and NULL'd. Thus a reader holding either lock has a 215d148c2a2SMatt Joras * guarantee that the struct ifvlantrunk references a valid vlan trunk. 21675ee267cSGleb Smirnoff * 217d148c2a2SMatt Joras * The performance-sensitive paths that warrant using the rmlock(9) are 218d148c2a2SMatt Joras * vlan_transmit and vlan_input. Both have to check for the vlan interface's 219d148c2a2SMatt Joras * existence using if_vlantrunk, and being in the network tx/rx paths the use 220d148c2a2SMatt Joras * of an rmlock(9) gives a measureable improvement in performance. 22175ee267cSGleb Smirnoff * 222d148c2a2SMatt Joras * The reason for having an sx(9) is mostly because there are still areas that 223d148c2a2SMatt Joras * must be sleepable and also have safe concurrent access to a vlan interface. 224d148c2a2SMatt Joras * Since the sx(9) exists, it is used by default in most paths unless sleeping 225d148c2a2SMatt Joras * is not permitted, or if it is not clear whether sleeping is permitted. 226d148c2a2SMatt Joras * 227d148c2a2SMatt Joras * Note that despite these protections, there is still an inherent race in the 228d148c2a2SMatt Joras * destruction of vlans since there's no guarantee that the ifnet hasn't been 229d148c2a2SMatt Joras * freed/reused when the tx/rx functions are called by the stack. This can only 230d148c2a2SMatt Joras * be fixed by addressing ifnet's lifetime issues. 2314faedfe8SSam Leffler */ 232d148c2a2SMatt Joras #define _VLAN_RM_ID ifv_rm_lock 233d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx 234d148c2a2SMatt Joras 235d148c2a2SMatt Joras static struct rmlock _VLAN_RM_ID; 236d148c2a2SMatt Joras static struct sx _VLAN_SX_ID; 237d148c2a2SMatt Joras 238d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \ 239d148c2a2SMatt Joras rm_init(&_VLAN_RM_ID, "vlan_rm"); \ 240d148c2a2SMatt Joras sx_init(&_VLAN_SX_ID, "vlan_sx") 241d148c2a2SMatt Joras 242d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \ 243d148c2a2SMatt Joras rm_destroy(&_VLAN_RM_ID); \ 244d148c2a2SMatt Joras sx_destroy(&_VLAN_SX_ID) 245d148c2a2SMatt Joras 246d148c2a2SMatt Joras #define _VLAN_RM_TRACKER _vlan_rm_tracker 247d148c2a2SMatt Joras #define VLAN_RLOCK() rm_rlock(&_VLAN_RM_ID, \ 248d148c2a2SMatt Joras &_VLAN_RM_TRACKER) 249d148c2a2SMatt Joras #define VLAN_RUNLOCK() rm_runlock(&_VLAN_RM_ID, \ 250d148c2a2SMatt Joras &_VLAN_RM_TRACKER) 251d148c2a2SMatt Joras #define VLAN_WLOCK() rm_wlock(&_VLAN_RM_ID) 252d148c2a2SMatt Joras #define VLAN_WUNLOCK() rm_wunlock(&_VLAN_RM_ID) 253d148c2a2SMatt Joras #define VLAN_RLOCK_ASSERT() rm_assert(&_VLAN_RM_ID, RA_RLOCKED) 254d148c2a2SMatt Joras #define VLAN_WLOCK_ASSERT() rm_assert(&_VLAN_RM_ID, RA_WLOCKED) 255d148c2a2SMatt Joras #define VLAN_RWLOCK_ASSERT() rm_assert(&_VLAN_RM_ID, RA_LOCKED) 256d148c2a2SMatt Joras #define VLAN_LOCK_READER struct rm_priotracker _VLAN_RM_TRACKER 257d148c2a2SMatt Joras 258d148c2a2SMatt Joras #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID) 259d148c2a2SMatt Joras #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID) 260d148c2a2SMatt Joras #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID) 261d148c2a2SMatt Joras #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID) 262d148c2a2SMatt Joras #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED) 263d148c2a2SMatt Joras #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED) 264d148c2a2SMatt Joras #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED) 265d148c2a2SMatt Joras 266d148c2a2SMatt Joras 267d148c2a2SMatt Joras /* 268d148c2a2SMatt Joras * We also have a per-trunk rmlock(9), that is locked shared on packet 269d148c2a2SMatt Joras * processing and exclusive when configuration is changed. Note: This should 270d148c2a2SMatt Joras * only be acquired while there is a shared lock on either of the global locks 271d148c2a2SMatt Joras * via VLAN_SLOCK or VLAN_RLOCK. Thus, an exclusive lock on the global locks 272d148c2a2SMatt Joras * makes a call to TRUNK_RLOCK/TRUNK_WLOCK technically superfluous. 273d148c2a2SMatt Joras */ 274d148c2a2SMatt Joras #define _TRUNK_RM_TRACKER _trunk_rm_tracker 275772b000fSAlexander V. Chernikov #define TRUNK_LOCK_INIT(trunk) rm_init(&(trunk)->lock, vlanname) 276772b000fSAlexander V. Chernikov #define TRUNK_LOCK_DESTROY(trunk) rm_destroy(&(trunk)->lock) 277d148c2a2SMatt Joras #define TRUNK_RLOCK(trunk) rm_rlock(&(trunk)->lock, \ 278d148c2a2SMatt Joras &_TRUNK_RM_TRACKER) 279d148c2a2SMatt Joras #define TRUNK_WLOCK(trunk) rm_wlock(&(trunk)->lock) 280d148c2a2SMatt Joras #define TRUNK_RUNLOCK(trunk) rm_runlock(&(trunk)->lock, \ 281d148c2a2SMatt Joras &_TRUNK_RM_TRACKER) 282d148c2a2SMatt Joras #define TRUNK_WUNLOCK(trunk) rm_wunlock(&(trunk)->lock) 283d148c2a2SMatt Joras #define TRUNK_RLOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_RLOCKED) 284d148c2a2SMatt Joras #define TRUNK_LOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_LOCKED) 285d148c2a2SMatt Joras #define TRUNK_WLOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_WLOCKED) 286d148c2a2SMatt Joras #define TRUNK_LOCK_READER struct rm_priotracker _TRUNK_RM_TRACKER 28775ee267cSGleb Smirnoff 288d148c2a2SMatt Joras /* 289d148c2a2SMatt Joras * The VLAN_ARRAY substitutes the dynamic hash with a static array 290d148c2a2SMatt Joras * with 4096 entries. In theory this can give a boost in processing, 291d148c2a2SMatt Joras * however in practice it does not. Probably this is because the array 292d148c2a2SMatt Joras * is too big to fit into CPU cache. 293d148c2a2SMatt Joras */ 29475ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 29575ee267cSGleb Smirnoff static void vlan_inithash(struct ifvlantrunk *trunk); 29675ee267cSGleb Smirnoff static void vlan_freehash(struct ifvlantrunk *trunk); 29775ee267cSGleb Smirnoff static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 29875ee267cSGleb Smirnoff static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 29975ee267cSGleb Smirnoff static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 30075ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 3017983103aSRobert Watson uint16_t vid); 30275ee267cSGleb Smirnoff #endif 30375ee267cSGleb Smirnoff static void trunk_destroy(struct ifvlantrunk *trunk); 3044faedfe8SSam Leffler 305114c608cSYaroslav Tykhiy static void vlan_init(void *foo); 306a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 307cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 308f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 309f3e7afe2SHans Petter Selasky static int vlan_snd_tag_alloc(struct ifnet *, 310f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *, struct m_snd_tag **); 311f3e7afe2SHans Petter Selasky #endif 312d9b1d615SJohn Baldwin static void vlan_qflush(struct ifnet *ifp); 3131cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 3141cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 3151cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 316f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 317d9b1d615SJohn Baldwin static int vlan_transmit(struct ifnet *ifp, struct mbuf *m); 3186f359e28SJohn Baldwin static void vlan_unconfig(struct ifnet *ifp); 31928cc4d37SJohn Baldwin static void vlan_unconfig_locked(struct ifnet *ifp, int departing); 32075ee267cSGleb Smirnoff static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag); 321a6fffd6cSBrooks Davis static void vlan_link_state(struct ifnet *ifp); 32275ee267cSGleb Smirnoff static void vlan_capabilities(struct ifvlan *ifv); 32375ee267cSGleb Smirnoff static void vlan_trunk_capabilities(struct ifnet *ifp); 324f731f104SBill Paul 325f941c31aSGleb Smirnoff static struct ifnet *vlan_clone_match_ethervid(const char *, int *); 326f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 3276b7330e2SSam Leffler static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 328f889d2efSBrooks Davis static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 329f889d2efSBrooks Davis 3305cb8c31aSYaroslav Tykhiy static void vlan_ifdetach(void *arg, struct ifnet *ifp); 331ea4ca115SAndrew Thompson static void vlan_iflladdr(void *arg, struct ifnet *ifp); 3325cb8c31aSYaroslav Tykhiy 333d148c2a2SMatt Joras static void vlan_lladdr_fn(void *arg, int pending); 334d148c2a2SMatt Joras 33542a58907SGleb Smirnoff static struct if_clone *vlan_cloner; 3369d4fe4b2SBrooks Davis 337ccf7ba97SMarko Zec #ifdef VIMAGE 3385f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner); 339ccf7ba97SMarko Zec #define V_vlan_cloner VNET(vlan_cloner) 340ccf7ba97SMarko Zec #endif 341ccf7ba97SMarko Zec 34275ee267cSGleb Smirnoff #ifndef VLAN_ARRAY 34375ee267cSGleb Smirnoff #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 344114c608cSYaroslav Tykhiy 34575ee267cSGleb Smirnoff static void 34675ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk) 34775ee267cSGleb Smirnoff { 34875ee267cSGleb Smirnoff int i, n; 34975ee267cSGleb Smirnoff 35075ee267cSGleb Smirnoff /* 35175ee267cSGleb Smirnoff * The trunk must not be locked here since we call malloc(M_WAITOK). 35275ee267cSGleb Smirnoff * It is OK in case this function is called before the trunk struct 35375ee267cSGleb Smirnoff * gets hooked up and becomes visible from other threads. 35475ee267cSGleb Smirnoff */ 35575ee267cSGleb Smirnoff 35675ee267cSGleb Smirnoff KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 35775ee267cSGleb Smirnoff ("%s: hash already initialized", __func__)); 35875ee267cSGleb Smirnoff 35975ee267cSGleb Smirnoff trunk->hwidth = VLAN_DEF_HWIDTH; 36075ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 36175ee267cSGleb Smirnoff trunk->hmask = n - 1; 36275ee267cSGleb Smirnoff trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 36375ee267cSGleb Smirnoff for (i = 0; i < n; i++) 36475ee267cSGleb Smirnoff LIST_INIT(&trunk->hash[i]); 36575ee267cSGleb Smirnoff } 36675ee267cSGleb Smirnoff 36775ee267cSGleb Smirnoff static void 36875ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk) 36975ee267cSGleb Smirnoff { 37075ee267cSGleb Smirnoff #ifdef INVARIANTS 37175ee267cSGleb Smirnoff int i; 37275ee267cSGleb Smirnoff 37375ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 37475ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) 37575ee267cSGleb Smirnoff KASSERT(LIST_EMPTY(&trunk->hash[i]), 37675ee267cSGleb Smirnoff ("%s: hash table not empty", __func__)); 37775ee267cSGleb Smirnoff #endif 37875ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 37975ee267cSGleb Smirnoff trunk->hash = NULL; 38075ee267cSGleb Smirnoff trunk->hwidth = trunk->hmask = 0; 38175ee267cSGleb Smirnoff } 38275ee267cSGleb Smirnoff 38375ee267cSGleb Smirnoff static int 38475ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 38575ee267cSGleb Smirnoff { 38675ee267cSGleb Smirnoff int i, b; 38775ee267cSGleb Smirnoff struct ifvlan *ifv2; 38875ee267cSGleb Smirnoff 389d148c2a2SMatt Joras TRUNK_WLOCK_ASSERT(trunk); 39075ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 39175ee267cSGleb Smirnoff 39275ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 3937983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 39475ee267cSGleb Smirnoff LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 3957983103aSRobert Watson if (ifv->ifv_vid == ifv2->ifv_vid) 39675ee267cSGleb Smirnoff return (EEXIST); 39775ee267cSGleb Smirnoff 39875ee267cSGleb Smirnoff /* 39975ee267cSGleb Smirnoff * Grow the hash when the number of vlans exceeds half of the number of 40075ee267cSGleb Smirnoff * hash buckets squared. This will make the average linked-list length 40175ee267cSGleb Smirnoff * buckets/2. 40275ee267cSGleb Smirnoff */ 40375ee267cSGleb Smirnoff if (trunk->refcnt > (b * b) / 2) { 40475ee267cSGleb Smirnoff vlan_growhash(trunk, 1); 4057983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 40675ee267cSGleb Smirnoff } 40775ee267cSGleb Smirnoff LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 40875ee267cSGleb Smirnoff trunk->refcnt++; 40975ee267cSGleb Smirnoff 41075ee267cSGleb Smirnoff return (0); 41175ee267cSGleb Smirnoff } 41275ee267cSGleb Smirnoff 41375ee267cSGleb Smirnoff static int 41475ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 41575ee267cSGleb Smirnoff { 41675ee267cSGleb Smirnoff int i, b; 41775ee267cSGleb Smirnoff struct ifvlan *ifv2; 41875ee267cSGleb Smirnoff 419d148c2a2SMatt Joras TRUNK_WLOCK_ASSERT(trunk); 42075ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 42175ee267cSGleb Smirnoff 42275ee267cSGleb Smirnoff b = 1 << trunk->hwidth; 4237983103aSRobert Watson i = HASH(ifv->ifv_vid, trunk->hmask); 42475ee267cSGleb Smirnoff LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 42575ee267cSGleb Smirnoff if (ifv2 == ifv) { 42675ee267cSGleb Smirnoff trunk->refcnt--; 42775ee267cSGleb Smirnoff LIST_REMOVE(ifv2, ifv_list); 42875ee267cSGleb Smirnoff if (trunk->refcnt < (b * b) / 2) 42975ee267cSGleb Smirnoff vlan_growhash(trunk, -1); 43075ee267cSGleb Smirnoff return (0); 43175ee267cSGleb Smirnoff } 43275ee267cSGleb Smirnoff 43375ee267cSGleb Smirnoff panic("%s: vlan not found\n", __func__); 43475ee267cSGleb Smirnoff return (ENOENT); /*NOTREACHED*/ 43575ee267cSGleb Smirnoff } 43675ee267cSGleb Smirnoff 43775ee267cSGleb Smirnoff /* 43875ee267cSGleb Smirnoff * Grow the hash larger or smaller if memory permits. 43975ee267cSGleb Smirnoff */ 44075ee267cSGleb Smirnoff static void 44175ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 44275ee267cSGleb Smirnoff { 44375ee267cSGleb Smirnoff struct ifvlan *ifv; 44475ee267cSGleb Smirnoff struct ifvlanhead *hash2; 44575ee267cSGleb Smirnoff int hwidth2, i, j, n, n2; 44675ee267cSGleb Smirnoff 447d148c2a2SMatt Joras TRUNK_WLOCK_ASSERT(trunk); 44875ee267cSGleb Smirnoff KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 44975ee267cSGleb Smirnoff 45075ee267cSGleb Smirnoff if (howmuch == 0) { 45175ee267cSGleb Smirnoff /* Harmless yet obvious coding error */ 45275ee267cSGleb Smirnoff printf("%s: howmuch is 0\n", __func__); 45375ee267cSGleb Smirnoff return; 45475ee267cSGleb Smirnoff } 45575ee267cSGleb Smirnoff 45675ee267cSGleb Smirnoff hwidth2 = trunk->hwidth + howmuch; 45775ee267cSGleb Smirnoff n = 1 << trunk->hwidth; 45875ee267cSGleb Smirnoff n2 = 1 << hwidth2; 45975ee267cSGleb Smirnoff /* Do not shrink the table below the default */ 46075ee267cSGleb Smirnoff if (hwidth2 < VLAN_DEF_HWIDTH) 46175ee267cSGleb Smirnoff return; 46275ee267cSGleb Smirnoff 46375ee267cSGleb Smirnoff /* M_NOWAIT because we're called with trunk mutex held */ 464ac2fffa4SPedro F. Giffuni hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT); 46575ee267cSGleb Smirnoff if (hash2 == NULL) { 46675ee267cSGleb Smirnoff printf("%s: out of memory -- hash size not changed\n", 46775ee267cSGleb Smirnoff __func__); 46875ee267cSGleb Smirnoff return; /* We can live with the old hash table */ 46975ee267cSGleb Smirnoff } 47075ee267cSGleb Smirnoff for (j = 0; j < n2; j++) 47175ee267cSGleb Smirnoff LIST_INIT(&hash2[j]); 47275ee267cSGleb Smirnoff for (i = 0; i < n; i++) 473c0cb022bSYaroslav Tykhiy while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) { 47475ee267cSGleb Smirnoff LIST_REMOVE(ifv, ifv_list); 4757983103aSRobert Watson j = HASH(ifv->ifv_vid, n2 - 1); 47675ee267cSGleb Smirnoff LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 47775ee267cSGleb Smirnoff } 47875ee267cSGleb Smirnoff free(trunk->hash, M_VLAN); 47975ee267cSGleb Smirnoff trunk->hash = hash2; 48075ee267cSGleb Smirnoff trunk->hwidth = hwidth2; 48175ee267cSGleb Smirnoff trunk->hmask = n2 - 1; 482f84b2d69SYaroslav Tykhiy 483f84b2d69SYaroslav Tykhiy if (bootverbose) 484f84b2d69SYaroslav Tykhiy if_printf(trunk->parent, 485f84b2d69SYaroslav Tykhiy "VLAN hash table resized from %d to %d buckets\n", n, n2); 48675ee267cSGleb Smirnoff } 48775ee267cSGleb Smirnoff 48875ee267cSGleb Smirnoff static __inline struct ifvlan * 4897983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 49075ee267cSGleb Smirnoff { 49175ee267cSGleb Smirnoff struct ifvlan *ifv; 49275ee267cSGleb Smirnoff 493d148c2a2SMatt Joras TRUNK_RLOCK_ASSERT(trunk); 49475ee267cSGleb Smirnoff 4957983103aSRobert Watson LIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list) 4967983103aSRobert Watson if (ifv->ifv_vid == vid) 49775ee267cSGleb Smirnoff return (ifv); 49875ee267cSGleb Smirnoff return (NULL); 49975ee267cSGleb Smirnoff } 50075ee267cSGleb Smirnoff 50175ee267cSGleb Smirnoff #if 0 50275ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */ 50375ee267cSGleb Smirnoff static void 50475ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk) 50575ee267cSGleb Smirnoff { 50675ee267cSGleb Smirnoff int i; 50775ee267cSGleb Smirnoff struct ifvlan *ifv; 50875ee267cSGleb Smirnoff 50975ee267cSGleb Smirnoff for (i = 0; i < (1 << trunk->hwidth); i++) { 51075ee267cSGleb Smirnoff printf("%d: ", i); 51175ee267cSGleb Smirnoff LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 51275ee267cSGleb Smirnoff printf("%s ", ifv->ifv_ifp->if_xname); 51375ee267cSGleb Smirnoff printf("\n"); 51475ee267cSGleb Smirnoff } 51575ee267cSGleb Smirnoff } 51675ee267cSGleb Smirnoff #endif /* 0 */ 517e4cd31ddSJeff Roberson #else 518e4cd31ddSJeff Roberson 519e4cd31ddSJeff Roberson static __inline struct ifvlan * 5207983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid) 521e4cd31ddSJeff Roberson { 522e4cd31ddSJeff Roberson 5237983103aSRobert Watson return trunk->vlans[vid]; 524e4cd31ddSJeff Roberson } 525e4cd31ddSJeff Roberson 526e4cd31ddSJeff Roberson static __inline int 527e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 528e4cd31ddSJeff Roberson { 529e4cd31ddSJeff Roberson 5307983103aSRobert Watson if (trunk->vlans[ifv->ifv_vid] != NULL) 531e4cd31ddSJeff Roberson return EEXIST; 5327983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = ifv; 533e4cd31ddSJeff Roberson trunk->refcnt++; 534e4cd31ddSJeff Roberson 535e4cd31ddSJeff Roberson return (0); 536e4cd31ddSJeff Roberson } 537e4cd31ddSJeff Roberson 538e4cd31ddSJeff Roberson static __inline int 539e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 540e4cd31ddSJeff Roberson { 541e4cd31ddSJeff Roberson 5427983103aSRobert Watson trunk->vlans[ifv->ifv_vid] = NULL; 543e4cd31ddSJeff Roberson trunk->refcnt--; 544e4cd31ddSJeff Roberson 545e4cd31ddSJeff Roberson return (0); 546e4cd31ddSJeff Roberson } 547e4cd31ddSJeff Roberson 548e4cd31ddSJeff Roberson static __inline void 549e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk) 550e4cd31ddSJeff Roberson { 551e4cd31ddSJeff Roberson } 552e4cd31ddSJeff Roberson 553e4cd31ddSJeff Roberson static __inline void 554e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk) 555e4cd31ddSJeff Roberson { 556e4cd31ddSJeff Roberson } 557e4cd31ddSJeff Roberson 55875ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */ 55975ee267cSGleb Smirnoff 56075ee267cSGleb Smirnoff static void 56175ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk) 56275ee267cSGleb Smirnoff { 563d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 564d148c2a2SMatt Joras VLAN_WLOCK_ASSERT(); 56575ee267cSGleb Smirnoff 56675ee267cSGleb Smirnoff vlan_freehash(trunk); 56775ee267cSGleb Smirnoff trunk->parent->if_vlantrunk = NULL; 56833499e2aSYaroslav Tykhiy TRUNK_LOCK_DESTROY(trunk); 5699bcf3ae4SAlexander Motin if_rele(trunk->parent); 57075ee267cSGleb Smirnoff free(trunk, M_VLAN); 57175ee267cSGleb Smirnoff } 57275ee267cSGleb Smirnoff 573f731f104SBill Paul /* 574f731f104SBill Paul * Program our multicast filter. What we're actually doing is 575f731f104SBill Paul * programming the multicast filter of the parent. This has the 576f731f104SBill Paul * side effect of causing the parent interface to receive multicast 577f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 578f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 579f731f104SBill Paul * to avoid this: there really is only one physical interface. 580f731f104SBill Paul */ 5812b120974SPeter Wemm static int 5822b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 583f731f104SBill Paul { 584f731f104SBill Paul struct ifnet *ifp_p; 5852d222cb7SAlexander Motin struct ifmultiaddr *ifma; 586f731f104SBill Paul struct ifvlan *sc; 587c0cb022bSYaroslav Tykhiy struct vlan_mc_entry *mc; 588f731f104SBill Paul int error; 589f731f104SBill Paul 590d148c2a2SMatt Joras /* 591d148c2a2SMatt Joras * XXX This stupidly needs the rmlock to avoid sleeping while holding 592d148c2a2SMatt Joras * the in6_multi_mtx (see in6_mc_join_locked). 593d148c2a2SMatt Joras */ 594d148c2a2SMatt Joras VLAN_RWLOCK_ASSERT(); 595d148c2a2SMatt Joras 596f731f104SBill Paul /* Find the parent. */ 597f731f104SBill Paul sc = ifp->if_softc; 598d148c2a2SMatt Joras TRUNK_WLOCK_ASSERT(TRUNK(sc)); 59975ee267cSGleb Smirnoff ifp_p = PARENT(sc); 6001b2a4f7aSBill Fenner 6018b615593SMarko Zec CURVNET_SET_QUIET(ifp_p->if_vnet); 6028b615593SMarko Zec 603f731f104SBill Paul /* First, remove any existing filter entries. */ 604c0cb022bSYaroslav Tykhiy while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 605f731f104SBill Paul SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 6062d222cb7SAlexander Motin (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr); 6079d4fe4b2SBrooks Davis free(mc, M_VLAN); 608f731f104SBill Paul } 609f731f104SBill Paul 610f731f104SBill Paul /* Now program new ones. */ 6112d222cb7SAlexander Motin IF_ADDR_WLOCK(ifp); 612d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 613f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 614f731f104SBill Paul continue; 61529c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 6162d222cb7SAlexander Motin if (mc == NULL) { 6172d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 61829c2dfbeSBruce M Simpson return (ENOMEM); 6192d222cb7SAlexander Motin } 620e4cd31ddSJeff Roberson bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len); 621e4cd31ddSJeff Roberson mc->mc_addr.sdl_index = ifp_p->if_index; 622f731f104SBill Paul SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 6232d222cb7SAlexander Motin } 6242d222cb7SAlexander Motin IF_ADDR_WUNLOCK(ifp); 6252d222cb7SAlexander Motin SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) { 626e4cd31ddSJeff Roberson error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr, 6272d222cb7SAlexander Motin NULL); 628f731f104SBill Paul if (error) 629f731f104SBill Paul return (error); 630f731f104SBill Paul } 631f731f104SBill Paul 6328b615593SMarko Zec CURVNET_RESTORE(); 633f731f104SBill Paul return (0); 634f731f104SBill Paul } 6352cc2df49SGarrett Wollman 636a3814acfSSam Leffler /* 637ea4ca115SAndrew Thompson * A handler for parent interface link layer address changes. 638ea4ca115SAndrew Thompson * If the parent interface link layer address is changed we 639ea4ca115SAndrew Thompson * should also change it on all children vlans. 640ea4ca115SAndrew Thompson */ 641ea4ca115SAndrew Thompson static void 642ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 643ea4ca115SAndrew Thompson { 644ea4ca115SAndrew Thompson struct ifvlan *ifv; 645d148c2a2SMatt Joras struct ifnet *ifv_ifp; 646d148c2a2SMatt Joras struct ifvlantrunk *trunk; 647d148c2a2SMatt Joras struct sockaddr_dl *sdl; 648d148c2a2SMatt Joras VLAN_LOCK_READER; 649ea4ca115SAndrew Thompson 650d148c2a2SMatt Joras /* Need the rmlock since this is run on taskqueue_swi. */ 651d148c2a2SMatt Joras VLAN_RLOCK(); 652d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 653d148c2a2SMatt Joras if (trunk == NULL) { 654d148c2a2SMatt Joras VLAN_RUNLOCK(); 655ea4ca115SAndrew Thompson return; 656d148c2a2SMatt Joras } 657ea4ca115SAndrew Thompson 658ea4ca115SAndrew Thompson /* 659ea4ca115SAndrew Thompson * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 660d148c2a2SMatt Joras * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR 661d148c2a2SMatt Joras * ioctl calls on the parent garbling the lladdr of the child vlan. 662ea4ca115SAndrew Thompson */ 663d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 664d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 665d148c2a2SMatt Joras /* 666d148c2a2SMatt Joras * Copy new new lladdr into the ifv_ifp, enqueue a task 667d148c2a2SMatt Joras * to actually call if_setlladdr. if_setlladdr needs to 668d148c2a2SMatt Joras * be deferred to a taskqueue because it will call into 669d148c2a2SMatt Joras * the if_vlan ioctl path and try to acquire the global 670d148c2a2SMatt Joras * lock. 671d148c2a2SMatt Joras */ 672d148c2a2SMatt Joras ifv_ifp = ifv->ifv_ifp; 673d148c2a2SMatt Joras bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp), 674e4cd31ddSJeff Roberson ifp->if_addrlen); 675d148c2a2SMatt Joras sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr; 676d148c2a2SMatt Joras sdl->sdl_alen = ifp->if_addrlen; 677d148c2a2SMatt Joras taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task); 6786117727bSAndrew Thompson } 679d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 680d148c2a2SMatt Joras VLAN_RUNLOCK(); 681ea4ca115SAndrew Thompson } 682ea4ca115SAndrew Thompson 683ea4ca115SAndrew Thompson /* 6845cb8c31aSYaroslav Tykhiy * A handler for network interface departure events. 6855cb8c31aSYaroslav Tykhiy * Track departure of trunks here so that we don't access invalid 6865cb8c31aSYaroslav Tykhiy * pointers or whatever if a trunk is ripped from under us, e.g., 6875428776eSJohn Baldwin * by ejecting its hot-plug card. However, if an ifnet is simply 6885428776eSJohn Baldwin * being renamed, then there's no need to tear down the state. 6895cb8c31aSYaroslav Tykhiy */ 6905cb8c31aSYaroslav Tykhiy static void 6915cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 6925cb8c31aSYaroslav Tykhiy { 6935cb8c31aSYaroslav Tykhiy struct ifvlan *ifv; 694d148c2a2SMatt Joras struct ifvlantrunk *trunk; 6955cb8c31aSYaroslav Tykhiy 6965428776eSJohn Baldwin /* If the ifnet is just being renamed, don't do anything. */ 6975428776eSJohn Baldwin if (ifp->if_flags & IFF_RENAMING) 6985428776eSJohn Baldwin return; 699d148c2a2SMatt Joras VLAN_XLOCK(); 700d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 701d148c2a2SMatt Joras if (trunk == NULL) { 702d148c2a2SMatt Joras VLAN_XUNLOCK(); 703d148c2a2SMatt Joras return; 704d148c2a2SMatt Joras } 7055428776eSJohn Baldwin 7065cb8c31aSYaroslav Tykhiy /* 7075cb8c31aSYaroslav Tykhiy * OK, it's a trunk. Loop over and detach all vlan's on it. 7085cb8c31aSYaroslav Tykhiy * Check trunk pointer after each vlan_unconfig() as it will 7095cb8c31aSYaroslav Tykhiy * free it and set to NULL after the last vlan was detached. 7105cb8c31aSYaroslav Tykhiy */ 711d148c2a2SMatt Joras VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk, 712d148c2a2SMatt Joras ifp->if_vlantrunk == NULL) 71328cc4d37SJohn Baldwin vlan_unconfig_locked(ifv->ifv_ifp, 1); 714d148c2a2SMatt Joras 7155cb8c31aSYaroslav Tykhiy /* Trunk should have been destroyed in vlan_unconfig(). */ 7165cb8c31aSYaroslav Tykhiy KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 717d148c2a2SMatt Joras VLAN_XUNLOCK(); 7185cb8c31aSYaroslav Tykhiy } 7195cb8c31aSYaroslav Tykhiy 7205cb8c31aSYaroslav Tykhiy /* 721e4cd31ddSJeff Roberson * Return the trunk device for a virtual interface. 722e4cd31ddSJeff Roberson */ 723e4cd31ddSJeff Roberson static struct ifnet * 724e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp) 725e4cd31ddSJeff Roberson { 726e4cd31ddSJeff Roberson struct ifvlan *ifv; 727d148c2a2SMatt Joras VLAN_LOCK_READER; 728e4cd31ddSJeff Roberson 729e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 730e4cd31ddSJeff Roberson return (NULL); 731d148c2a2SMatt Joras 732d148c2a2SMatt Joras /* Not clear if callers are sleepable, so acquire the rmlock. */ 733d148c2a2SMatt Joras VLAN_RLOCK(); 734e4cd31ddSJeff Roberson ifv = ifp->if_softc; 735e4cd31ddSJeff Roberson ifp = NULL; 736e4cd31ddSJeff Roberson if (ifv->ifv_trunk) 737e4cd31ddSJeff Roberson ifp = PARENT(ifv); 738d148c2a2SMatt Joras VLAN_RUNLOCK(); 739e4cd31ddSJeff Roberson return (ifp); 740e4cd31ddSJeff Roberson } 741e4cd31ddSJeff Roberson 742e4cd31ddSJeff Roberson /* 7437983103aSRobert Watson * Return the 12-bit VLAN VID for this interface, for use by external 7447983103aSRobert Watson * components such as Infiniband. 7457983103aSRobert Watson * 7467983103aSRobert Watson * XXXRW: Note that the function name here is historical; it should be named 7477983103aSRobert Watson * vlan_vid(). 748e4cd31ddSJeff Roberson */ 749e4cd31ddSJeff Roberson static int 7507983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp) 751e4cd31ddSJeff Roberson { 752e4cd31ddSJeff Roberson struct ifvlan *ifv; 753e4cd31ddSJeff Roberson 754e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 755e4cd31ddSJeff Roberson return (EINVAL); 756e4cd31ddSJeff Roberson ifv = ifp->if_softc; 7577983103aSRobert Watson *vidp = ifv->ifv_vid; 758e4cd31ddSJeff Roberson return (0); 759e4cd31ddSJeff Roberson } 760e4cd31ddSJeff Roberson 76132d2623aSNavdeep Parhar static int 76232d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp) 76332d2623aSNavdeep Parhar { 76432d2623aSNavdeep Parhar struct ifvlan *ifv; 76532d2623aSNavdeep Parhar 76632d2623aSNavdeep Parhar if (ifp->if_type != IFT_L2VLAN) 76732d2623aSNavdeep Parhar return (EINVAL); 76832d2623aSNavdeep Parhar ifv = ifp->if_softc; 76932d2623aSNavdeep Parhar *pcpp = ifv->ifv_pcp; 77032d2623aSNavdeep Parhar return (0); 77132d2623aSNavdeep Parhar } 77232d2623aSNavdeep Parhar 773e4cd31ddSJeff Roberson /* 774e4cd31ddSJeff Roberson * Return a driver specific cookie for this interface. Synchronization 775e4cd31ddSJeff Roberson * with setcookie must be provided by the driver. 776e4cd31ddSJeff Roberson */ 777e4cd31ddSJeff Roberson static void * 778e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp) 779e4cd31ddSJeff Roberson { 780e4cd31ddSJeff Roberson struct ifvlan *ifv; 781e4cd31ddSJeff Roberson 782e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 783e4cd31ddSJeff Roberson return (NULL); 784e4cd31ddSJeff Roberson ifv = ifp->if_softc; 785e4cd31ddSJeff Roberson return (ifv->ifv_cookie); 786e4cd31ddSJeff Roberson } 787e4cd31ddSJeff Roberson 788e4cd31ddSJeff Roberson /* 789e4cd31ddSJeff Roberson * Store a cookie in our softc that drivers can use to store driver 790e4cd31ddSJeff Roberson * private per-instance data in. 791e4cd31ddSJeff Roberson */ 792e4cd31ddSJeff Roberson static int 793e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie) 794e4cd31ddSJeff Roberson { 795e4cd31ddSJeff Roberson struct ifvlan *ifv; 796e4cd31ddSJeff Roberson 797e4cd31ddSJeff Roberson if (ifp->if_type != IFT_L2VLAN) 798e4cd31ddSJeff Roberson return (EINVAL); 799e4cd31ddSJeff Roberson ifv = ifp->if_softc; 800e4cd31ddSJeff Roberson ifv->ifv_cookie = cookie; 801e4cd31ddSJeff Roberson return (0); 802e4cd31ddSJeff Roberson } 803e4cd31ddSJeff Roberson 804e4cd31ddSJeff Roberson /* 8057983103aSRobert Watson * Return the vlan device present at the specific VID. 806e4cd31ddSJeff Roberson */ 807e4cd31ddSJeff Roberson static struct ifnet * 8087983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid) 809e4cd31ddSJeff Roberson { 810e4cd31ddSJeff Roberson struct ifvlantrunk *trunk; 811e4cd31ddSJeff Roberson struct ifvlan *ifv; 812d148c2a2SMatt Joras VLAN_LOCK_READER; 813772b000fSAlexander V. Chernikov TRUNK_LOCK_READER; 814e4cd31ddSJeff Roberson 815d148c2a2SMatt Joras /* Not clear if callers are sleepable, so acquire the rmlock. */ 816d148c2a2SMatt Joras VLAN_RLOCK(); 817e4cd31ddSJeff Roberson trunk = ifp->if_vlantrunk; 818d148c2a2SMatt Joras if (trunk == NULL) { 819d148c2a2SMatt Joras VLAN_RUNLOCK(); 820e4cd31ddSJeff Roberson return (NULL); 821d148c2a2SMatt Joras } 822e4cd31ddSJeff Roberson ifp = NULL; 823e4cd31ddSJeff Roberson TRUNK_RLOCK(trunk); 8247983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 825e4cd31ddSJeff Roberson if (ifv) 826e4cd31ddSJeff Roberson ifp = ifv->ifv_ifp; 827e4cd31ddSJeff Roberson TRUNK_RUNLOCK(trunk); 828d148c2a2SMatt Joras VLAN_RUNLOCK(); 829e4cd31ddSJeff Roberson return (ifp); 830e4cd31ddSJeff Roberson } 831e4cd31ddSJeff Roberson 832e4cd31ddSJeff Roberson /* 8332ccbbd06SMarcelo Araujo * Recalculate the cached VLAN tag exposed via the MIB. 8342ccbbd06SMarcelo Araujo */ 8352ccbbd06SMarcelo Araujo static void 8362ccbbd06SMarcelo Araujo vlan_tag_recalculate(struct ifvlan *ifv) 8372ccbbd06SMarcelo Araujo { 8382ccbbd06SMarcelo Araujo 8392ccbbd06SMarcelo Araujo ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0); 8402ccbbd06SMarcelo Araujo } 8412ccbbd06SMarcelo Araujo 8422ccbbd06SMarcelo Araujo /* 843a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 844a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 845a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 846a3814acfSSam Leffler * set here. No one else in the system should be aware of this so 847a3814acfSSam Leffler * we use an explicit reference here. 848a3814acfSSam Leffler */ 849a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 850a3814acfSSam Leffler 851984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 852a6fffd6cSBrooks Davis extern void (*vlan_link_state_p)(struct ifnet *); 853127d7b2dSAndre Oppermann 8542b120974SPeter Wemm static int 8552b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 8562b120974SPeter Wemm { 8579d4fe4b2SBrooks Davis 8582b120974SPeter Wemm switch (type) { 8592b120974SPeter Wemm case MOD_LOAD: 8605cb8c31aSYaroslav Tykhiy ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 8615cb8c31aSYaroslav Tykhiy vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 8625cb8c31aSYaroslav Tykhiy if (ifdetach_tag == NULL) 8635cb8c31aSYaroslav Tykhiy return (ENOMEM); 864ea4ca115SAndrew Thompson iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 865ea4ca115SAndrew Thompson vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 866ea4ca115SAndrew Thompson if (iflladdr_tag == NULL) 867ea4ca115SAndrew Thompson return (ENOMEM); 868d148c2a2SMatt Joras VLAN_LOCKING_INIT(); 8699d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 870127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 87175ee267cSGleb Smirnoff vlan_trunk_cap_p = vlan_trunk_capabilities; 872e4cd31ddSJeff Roberson vlan_trunkdev_p = vlan_trunkdev; 873e4cd31ddSJeff Roberson vlan_cookie_p = vlan_cookie; 874e4cd31ddSJeff Roberson vlan_setcookie_p = vlan_setcookie; 875e4cd31ddSJeff Roberson vlan_tag_p = vlan_tag; 87632d2623aSNavdeep Parhar vlan_pcp_p = vlan_pcp; 877e4cd31ddSJeff Roberson vlan_devat_p = vlan_devat; 878ccf7ba97SMarko Zec #ifndef VIMAGE 87942a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 88042a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 881ccf7ba97SMarko Zec #endif 88225c0f7b3SYaroslav Tykhiy if (bootverbose) 88325c0f7b3SYaroslav Tykhiy printf("vlan: initialized, using " 88425c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY 88525c0f7b3SYaroslav Tykhiy "full-size arrays" 88625c0f7b3SYaroslav Tykhiy #else 88725c0f7b3SYaroslav Tykhiy "hash tables with chaining" 88825c0f7b3SYaroslav Tykhiy #endif 88925c0f7b3SYaroslav Tykhiy 89025c0f7b3SYaroslav Tykhiy "\n"); 8912b120974SPeter Wemm break; 8922b120974SPeter Wemm case MOD_UNLOAD: 893ccf7ba97SMarko Zec #ifndef VIMAGE 89442a58907SGleb Smirnoff if_clone_detach(vlan_cloner); 895ccf7ba97SMarko Zec #endif 8965cb8c31aSYaroslav Tykhiy EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 897ea4ca115SAndrew Thompson EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 8989d4fe4b2SBrooks Davis vlan_input_p = NULL; 899127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 90075ee267cSGleb Smirnoff vlan_trunk_cap_p = NULL; 901e4cd31ddSJeff Roberson vlan_trunkdev_p = NULL; 902e4cd31ddSJeff Roberson vlan_tag_p = NULL; 90309fe6320SNavdeep Parhar vlan_cookie_p = NULL; 90409fe6320SNavdeep Parhar vlan_setcookie_p = NULL; 905e4cd31ddSJeff Roberson vlan_devat_p = NULL; 906d148c2a2SMatt Joras VLAN_LOCKING_DESTROY(); 90725c0f7b3SYaroslav Tykhiy if (bootverbose) 90825c0f7b3SYaroslav Tykhiy printf("vlan: unloaded\n"); 9099d4fe4b2SBrooks Davis break; 9103e019deaSPoul-Henning Kamp default: 9113e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 9122b120974SPeter Wemm } 91315a66c21SBruce M Simpson return (0); 9142b120974SPeter Wemm } 9152b120974SPeter Wemm 9162b120974SPeter Wemm static moduledata_t vlan_mod = { 9172b120974SPeter Wemm "if_vlan", 9182b120974SPeter Wemm vlan_modevent, 9199823d527SKevin Lo 0 9202b120974SPeter Wemm }; 9212b120974SPeter Wemm 9222b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 92311edc477SEd Maste MODULE_VERSION(if_vlan, 3); 9242cc2df49SGarrett Wollman 925ccf7ba97SMarko Zec #ifdef VIMAGE 926ccf7ba97SMarko Zec static void 927ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused) 928ccf7ba97SMarko Zec { 929ccf7ba97SMarko Zec 93042a58907SGleb Smirnoff vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match, 93142a58907SGleb Smirnoff vlan_clone_create, vlan_clone_destroy); 932ccf7ba97SMarko Zec V_vlan_cloner = vlan_cloner; 933ccf7ba97SMarko Zec } 934ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 935ccf7ba97SMarko Zec vnet_vlan_init, NULL); 936ccf7ba97SMarko Zec 937ccf7ba97SMarko Zec static void 938ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused) 939ccf7ba97SMarko Zec { 940ccf7ba97SMarko Zec 94142a58907SGleb Smirnoff if_clone_detach(V_vlan_cloner); 942ccf7ba97SMarko Zec } 94389856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, 944ccf7ba97SMarko Zec vnet_vlan_uninit, NULL); 945ccf7ba97SMarko Zec #endif 946ccf7ba97SMarko Zec 947f941c31aSGleb Smirnoff /* 948f941c31aSGleb Smirnoff * Check for <etherif>.<vlan> style interface names. 949f941c31aSGleb Smirnoff */ 950f889d2efSBrooks Davis static struct ifnet * 951f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp) 9529d4fe4b2SBrooks Davis { 953f941c31aSGleb Smirnoff char ifname[IFNAMSIZ]; 954f941c31aSGleb Smirnoff char *cp; 955f889d2efSBrooks Davis struct ifnet *ifp; 9567983103aSRobert Watson int vid; 957f889d2efSBrooks Davis 958f941c31aSGleb Smirnoff strlcpy(ifname, name, IFNAMSIZ); 959f941c31aSGleb Smirnoff if ((cp = strchr(ifname, '.')) == NULL) 960f941c31aSGleb Smirnoff return (NULL); 961f941c31aSGleb Smirnoff *cp = '\0'; 9629bcf3ae4SAlexander Motin if ((ifp = ifunit_ref(ifname)) == NULL) 963f941c31aSGleb Smirnoff return (NULL); 964f941c31aSGleb Smirnoff /* Parse VID. */ 9659bcf3ae4SAlexander Motin if (*++cp == '\0') { 9669bcf3ae4SAlexander Motin if_rele(ifp); 967f941c31aSGleb Smirnoff return (NULL); 9689bcf3ae4SAlexander Motin } 9697983103aSRobert Watson vid = 0; 970fb92ad4aSJohn Baldwin for(; *cp >= '0' && *cp <= '9'; cp++) 9717983103aSRobert Watson vid = (vid * 10) + (*cp - '0'); 9729bcf3ae4SAlexander Motin if (*cp != '\0') { 9739bcf3ae4SAlexander Motin if_rele(ifp); 974f941c31aSGleb Smirnoff return (NULL); 9759bcf3ae4SAlexander Motin } 9767983103aSRobert Watson if (vidp != NULL) 9777983103aSRobert Watson *vidp = vid; 978f889d2efSBrooks Davis 97915a66c21SBruce M Simpson return (ifp); 980f889d2efSBrooks Davis } 981f889d2efSBrooks Davis 982f889d2efSBrooks Davis static int 983f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 984f889d2efSBrooks Davis { 985f889d2efSBrooks Davis const char *cp; 986f889d2efSBrooks Davis 987f941c31aSGleb Smirnoff if (vlan_clone_match_ethervid(name, NULL) != NULL) 988f889d2efSBrooks Davis return (1); 989f889d2efSBrooks Davis 99042a58907SGleb Smirnoff if (strncmp(vlanname, name, strlen(vlanname)) != 0) 991f889d2efSBrooks Davis return (0); 992f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 993f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 994f889d2efSBrooks Davis return (0); 995f889d2efSBrooks Davis } 996f889d2efSBrooks Davis 997f889d2efSBrooks Davis return (1); 998f889d2efSBrooks Davis } 999f889d2efSBrooks Davis 1000f889d2efSBrooks Davis static int 10016b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 1002f889d2efSBrooks Davis { 1003f889d2efSBrooks Davis char *dp; 1004f889d2efSBrooks Davis int wildcard; 1005f889d2efSBrooks Davis int unit; 1006f889d2efSBrooks Davis int error; 10077983103aSRobert Watson int vid; 10089d4fe4b2SBrooks Davis struct ifvlan *ifv; 10099d4fe4b2SBrooks Davis struct ifnet *ifp; 1010f889d2efSBrooks Davis struct ifnet *p; 10113ba24fdeSJohn Baldwin struct ifaddr *ifa; 10123ba24fdeSJohn Baldwin struct sockaddr_dl *sdl; 10136b7330e2SSam Leffler struct vlanreq vlr; 101465239942SYaroslav Tykhiy static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 1015f889d2efSBrooks Davis 10166b7330e2SSam Leffler /* 10176b7330e2SSam Leffler * There are 3 (ugh) ways to specify the cloned device: 10186b7330e2SSam Leffler * o pass a parameter block with the clone request. 10196b7330e2SSam Leffler * o specify parameters in the text of the clone device name 10206b7330e2SSam Leffler * o specify no parameters and get an unattached device that 10216b7330e2SSam Leffler * must be configured separately. 10226b7330e2SSam Leffler * The first technique is preferred; the latter two are 1023a4641f4eSPedro F. Giffuni * supported for backwards compatibility. 10247983103aSRobert Watson * 10257983103aSRobert Watson * XXXRW: Note historic use of the word "tag" here. New ioctls may be 10267983103aSRobert Watson * called for. 10276b7330e2SSam Leffler */ 10286b7330e2SSam Leffler if (params) { 10296b7330e2SSam Leffler error = copyin(params, &vlr, sizeof(vlr)); 10306b7330e2SSam Leffler if (error) 10316b7330e2SSam Leffler return error; 10329bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 10336b7330e2SSam Leffler if (p == NULL) 1034b1828acfSGleb Smirnoff return (ENXIO); 10356b7330e2SSam Leffler error = ifc_name2unit(name, &unit); 10369bcf3ae4SAlexander Motin if (error != 0) { 10379bcf3ae4SAlexander Motin if_rele(p); 10386b7330e2SSam Leffler return (error); 10399bcf3ae4SAlexander Motin } 10407983103aSRobert Watson vid = vlr.vlr_tag; 10416b7330e2SSam Leffler wildcard = (unit < 0); 1042f941c31aSGleb Smirnoff } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) { 1043f889d2efSBrooks Davis unit = -1; 1044f889d2efSBrooks Davis wildcard = 0; 1045f889d2efSBrooks Davis } else { 10469bcf3ae4SAlexander Motin p = NULL; 1047f889d2efSBrooks Davis error = ifc_name2unit(name, &unit); 1048f889d2efSBrooks Davis if (error != 0) 1049f889d2efSBrooks Davis return (error); 1050f889d2efSBrooks Davis 1051f889d2efSBrooks Davis wildcard = (unit < 0); 1052f889d2efSBrooks Davis } 1053f889d2efSBrooks Davis 1054f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 10559bcf3ae4SAlexander Motin if (error != 0) { 10569bcf3ae4SAlexander Motin if (p != NULL) 10579bcf3ae4SAlexander Motin if_rele(p); 1058f889d2efSBrooks Davis return (error); 10599bcf3ae4SAlexander Motin } 1060f889d2efSBrooks Davis 1061f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 1062f889d2efSBrooks Davis if (wildcard) { 1063f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 1064f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 1065f889d2efSBrooks Davis len - (dp-name) - 1) { 1066f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 1067f889d2efSBrooks Davis } 1068f889d2efSBrooks Davis } 10699d4fe4b2SBrooks Davis 1070a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 1071fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 1072fc74a9f9SBrooks Davis if (ifp == NULL) { 1073fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 1074fc74a9f9SBrooks Davis free(ifv, M_VLAN); 10759bcf3ae4SAlexander Motin if (p != NULL) 10769bcf3ae4SAlexander Motin if_rele(p); 1077fc74a9f9SBrooks Davis return (ENOSPC); 1078fc74a9f9SBrooks Davis } 10799d4fe4b2SBrooks Davis SLIST_INIT(&ifv->vlan_mc_listhead); 10809d4fe4b2SBrooks Davis ifp->if_softc = ifv; 1081f889d2efSBrooks Davis /* 1082cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 1083f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 1084f889d2efSBrooks Davis */ 1085f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 108642a58907SGleb Smirnoff ifp->if_dname = vlanname; 1087f889d2efSBrooks Davis ifp->if_dunit = unit; 10889d4fe4b2SBrooks Davis /* NB: flags are not set here */ 10899d4fe4b2SBrooks Davis ifp->if_linkmib = &ifv->ifv_mib; 109015a66c21SBruce M Simpson ifp->if_linkmiblen = sizeof(ifv->ifv_mib); 10919d4fe4b2SBrooks Davis /* NB: mtu is not set here */ 10929d4fe4b2SBrooks Davis 1093114c608cSYaroslav Tykhiy ifp->if_init = vlan_init; 1094d9b1d615SJohn Baldwin ifp->if_transmit = vlan_transmit; 1095d9b1d615SJohn Baldwin ifp->if_qflush = vlan_qflush; 10969d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 1097f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1098f3e7afe2SHans Petter Selasky ifp->if_snd_tag_alloc = vlan_snd_tag_alloc; 1099f3e7afe2SHans Petter Selasky #endif 110064a17d2eSYaroslav Tykhiy ifp->if_flags = VLAN_IFFLAGS; 1101fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 11029d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 1103211f625aSBill Fenner ifp->if_baudrate = 0; 1104a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 1105a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 11063ba24fdeSJohn Baldwin ifa = ifp->if_addr; 11073ba24fdeSJohn Baldwin sdl = (struct sockaddr_dl *)ifa->ifa_addr; 11083ba24fdeSJohn Baldwin sdl->sdl_type = IFT_L2VLAN; 11099d4fe4b2SBrooks Davis 11109bcf3ae4SAlexander Motin if (p != NULL) { 11117983103aSRobert Watson error = vlan_config(ifv, p, vid); 11129bcf3ae4SAlexander Motin if_rele(p); 1113f889d2efSBrooks Davis if (error != 0) { 1114f889d2efSBrooks Davis /* 111528cc4d37SJohn Baldwin * Since we've partially failed, we need to back 1116f889d2efSBrooks Davis * out all the way, otherwise userland could get 1117f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 1118f889d2efSBrooks Davis */ 1119f889d2efSBrooks Davis ether_ifdetach(ifp); 1120249f4297SYaroslav Tykhiy vlan_unconfig(ifp); 11214b22573aSBrooks Davis if_free(ifp); 112296c8ef3aSMaxim Konovalov ifc_free_unit(ifc, unit); 1123f889d2efSBrooks Davis free(ifv, M_VLAN); 1124f889d2efSBrooks Davis 1125f889d2efSBrooks Davis return (error); 1126f889d2efSBrooks Davis } 1127f889d2efSBrooks Davis } 1128f889d2efSBrooks Davis 11299d4fe4b2SBrooks Davis return (0); 11309d4fe4b2SBrooks Davis } 11319d4fe4b2SBrooks Davis 1132f889d2efSBrooks Davis static int 1133f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 11349d4fe4b2SBrooks Davis { 11359d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 1136114c608cSYaroslav Tykhiy int unit = ifp->if_dunit; 1137b4e9f837SBrooks Davis 1138249f4297SYaroslav Tykhiy ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 1139249f4297SYaroslav Tykhiy vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 1140d148c2a2SMatt Joras /* 1141d148c2a2SMatt Joras * We should have the only reference to the ifv now, so we can now 1142d148c2a2SMatt Joras * drain any remaining lladdr task before freeing the ifnet and the 1143d148c2a2SMatt Joras * ifvlan. 1144d148c2a2SMatt Joras */ 1145d148c2a2SMatt Joras taskqueue_drain(taskqueue_thread, &ifv->lladdr_task); 11464b22573aSBrooks Davis if_free(ifp); 11479d4fe4b2SBrooks Davis free(ifv, M_VLAN); 1148b4e9f837SBrooks Davis ifc_free_unit(ifc, unit); 1149b4e9f837SBrooks Davis 1150f889d2efSBrooks Davis return (0); 11519d4fe4b2SBrooks Davis } 11529d4fe4b2SBrooks Davis 115315a66c21SBruce M Simpson /* 115415a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 115515a66c21SBruce M Simpson */ 11562cc2df49SGarrett Wollman static void 1157114c608cSYaroslav Tykhiy vlan_init(void *foo __unused) 11582cc2df49SGarrett Wollman { 11592cc2df49SGarrett Wollman } 11602cc2df49SGarrett Wollman 11616d3a3ab7SGleb Smirnoff /* 1162d9b1d615SJohn Baldwin * The if_transmit method for vlan(4) interface. 11636d3a3ab7SGleb Smirnoff */ 1164d9b1d615SJohn Baldwin static int 1165d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m) 11662cc2df49SGarrett Wollman { 11672cc2df49SGarrett Wollman struct ifvlan *ifv; 11682cc2df49SGarrett Wollman struct ifnet *p; 11691ad7a257SPyun YongHyeon int error, len, mcast; 1170d148c2a2SMatt Joras VLAN_LOCK_READER; 11712cc2df49SGarrett Wollman 1172d148c2a2SMatt Joras VLAN_RLOCK(); 11732cc2df49SGarrett Wollman ifv = ifp->if_softc; 1174d148c2a2SMatt Joras if (TRUNK(ifv) == NULL) { 1175d148c2a2SMatt Joras if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1176d148c2a2SMatt Joras VLAN_RUNLOCK(); 1177d148c2a2SMatt Joras m_freem(m); 1178d148c2a2SMatt Joras return (ENETDOWN); 1179d148c2a2SMatt Joras } 118075ee267cSGleb Smirnoff p = PARENT(ifv); 11811ad7a257SPyun YongHyeon len = m->m_pkthdr.len; 11821ad7a257SPyun YongHyeon mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 11832cc2df49SGarrett Wollman 1184a3814acfSSam Leffler BPF_MTAP(ifp, m); 11852cc2df49SGarrett Wollman 1186f731f104SBill Paul /* 1187d9b1d615SJohn Baldwin * Do not run parent's if_transmit() if the parent is not up, 118824993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 118924993214SYaroslav Tykhiy */ 11902dc879b3SYaroslav Tykhiy if (!UP_AND_RUNNING(p)) { 1191a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1192d148c2a2SMatt Joras VLAN_RUNLOCK(); 1193d148c2a2SMatt Joras m_freem(m); 11948b20f6cfSHiroki Sato return (ENETDOWN); 119524993214SYaroslav Tykhiy } 119624993214SYaroslav Tykhiy 1197f1379734SKonstantin Belousov if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) { 1198a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1199d148c2a2SMatt Joras VLAN_RUNLOCK(); 1200d9b1d615SJohn Baldwin return (0); 12014af90a4dSMatthew N. Dodd } 12022cc2df49SGarrett Wollman 12032cc2df49SGarrett Wollman /* 12042cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 12052cc2df49SGarrett Wollman */ 1206aea78d20SKip Macy error = (p->if_transmit)(p, m); 1207299153b5SAlexander V. Chernikov if (error == 0) { 1208a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1209a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 1210a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast); 12111ad7a257SPyun YongHyeon } else 1212a58ea6b1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1213d148c2a2SMatt Joras VLAN_RUNLOCK(); 1214d9b1d615SJohn Baldwin return (error); 12152cc2df49SGarrett Wollman } 1216d9b1d615SJohn Baldwin 1217d9b1d615SJohn Baldwin /* 1218d9b1d615SJohn Baldwin * The ifp->if_qflush entry point for vlan(4) is a no-op. 1219d9b1d615SJohn Baldwin */ 1220d9b1d615SJohn Baldwin static void 1221d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused) 1222d9b1d615SJohn Baldwin { 1223f731f104SBill Paul } 1224f731f104SBill Paul 1225a3814acfSSam Leffler static void 1226a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 1227f731f104SBill Paul { 1228d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1229f731f104SBill Paul struct ifvlan *ifv; 1230d148c2a2SMatt Joras VLAN_LOCK_READER; 1231772b000fSAlexander V. Chernikov TRUNK_LOCK_READER; 12322ccbbd06SMarcelo Araujo struct m_tag *mtag; 12332ccbbd06SMarcelo Araujo uint16_t vid, tag; 123475ee267cSGleb Smirnoff 1235d148c2a2SMatt Joras VLAN_RLOCK(); 1236d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1237d148c2a2SMatt Joras if (trunk == NULL) { 1238d148c2a2SMatt Joras VLAN_RUNLOCK(); 1239d148c2a2SMatt Joras m_freem(m); 1240d148c2a2SMatt Joras return; 1241d148c2a2SMatt Joras } 1242a3814acfSSam Leffler 1243f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 1244a3814acfSSam Leffler /* 124514e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 1246a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 1247a3814acfSSam Leffler */ 12482ccbbd06SMarcelo Araujo tag = m->m_pkthdr.ether_vtag; 12496ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 1250a3814acfSSam Leffler } else { 125175ee267cSGleb Smirnoff struct ether_vlan_header *evl; 125275ee267cSGleb Smirnoff 125314e98256SYaroslav Tykhiy /* 125414e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 125514e98256SYaroslav Tykhiy */ 1256a3814acfSSam Leffler switch (ifp->if_type) { 1257a3814acfSSam Leffler case IFT_ETHER: 1258a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 1259a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 1260a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 1261d148c2a2SMatt Joras VLAN_RUNLOCK(); 1262a3814acfSSam Leffler return; 1263a3814acfSSam Leffler } 1264a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 12652ccbbd06SMarcelo Araujo tag = ntohs(evl->evl_tag); 1266db8b5973SYaroslav Tykhiy 1267db8b5973SYaroslav Tykhiy /* 12682dc879b3SYaroslav Tykhiy * Remove the 802.1q header by copying the Ethernet 12692dc879b3SYaroslav Tykhiy * addresses over it and adjusting the beginning of 12702dc879b3SYaroslav Tykhiy * the data in the mbuf. The encapsulated Ethernet 12712dc879b3SYaroslav Tykhiy * type field is already in place. 1272db8b5973SYaroslav Tykhiy */ 12732dc879b3SYaroslav Tykhiy bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 12742dc879b3SYaroslav Tykhiy ETHER_HDR_LEN - ETHER_TYPE_LEN); 12752dc879b3SYaroslav Tykhiy m_adj(m, ETHER_VLAN_ENCAP_LEN); 1276a3814acfSSam Leffler break; 12772dc879b3SYaroslav Tykhiy 1278a3814acfSSam Leffler default: 1279db8b5973SYaroslav Tykhiy #ifdef INVARIANTS 128060c60618SYaroslav Tykhiy panic("%s: %s has unsupported if_type %u", 128160c60618SYaroslav Tykhiy __func__, ifp->if_xname, ifp->if_type); 1282a3814acfSSam Leffler #endif 12833751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1284d148c2a2SMatt Joras VLAN_RUNLOCK(); 1285d148c2a2SMatt Joras m_freem(m); 128660c60618SYaroslav Tykhiy return; 1287a3814acfSSam Leffler } 12887a46ec8fSBrooks Davis } 12897a46ec8fSBrooks Davis 12902ccbbd06SMarcelo Araujo vid = EVL_VLANOFTAG(tag); 12912ccbbd06SMarcelo Araujo 129215ed2fa1SYaroslav Tykhiy TRUNK_RLOCK(trunk); 12937983103aSRobert Watson ifv = vlan_gethash(trunk, vid); 12942dc879b3SYaroslav Tykhiy if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 129575ee267cSGleb Smirnoff TRUNK_RUNLOCK(trunk); 12963751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 1297d148c2a2SMatt Joras VLAN_RUNLOCK(); 1298d148c2a2SMatt Joras m_freem(m); 129975ee267cSGleb Smirnoff return; 130075ee267cSGleb Smirnoff } 130175ee267cSGleb Smirnoff TRUNK_RUNLOCK(trunk); 1302f731f104SBill Paul 13032ccbbd06SMarcelo Araujo if (vlan_mtag_pcp) { 13042ccbbd06SMarcelo Araujo /* 13052ccbbd06SMarcelo Araujo * While uncommon, it is possible that we will find a 802.1q 13062ccbbd06SMarcelo Araujo * packet encapsulated inside another packet that also had an 13072ccbbd06SMarcelo Araujo * 802.1q header. For example, ethernet tunneled over IPSEC 13082ccbbd06SMarcelo Araujo * arriving over ethernet. In that case, we replace the 13092ccbbd06SMarcelo Araujo * existing 802.1q PCP m_tag value. 13102ccbbd06SMarcelo Araujo */ 13112ccbbd06SMarcelo Araujo mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 13122ccbbd06SMarcelo Araujo if (mtag == NULL) { 13132ccbbd06SMarcelo Araujo mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN, 13142ccbbd06SMarcelo Araujo sizeof(uint8_t), M_NOWAIT); 13152ccbbd06SMarcelo Araujo if (mtag == NULL) { 13162ccbbd06SMarcelo Araujo if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1317d148c2a2SMatt Joras VLAN_RUNLOCK(); 1318d148c2a2SMatt Joras m_freem(m); 13192ccbbd06SMarcelo Araujo return; 13202ccbbd06SMarcelo Araujo } 13212ccbbd06SMarcelo Araujo m_tag_prepend(m, mtag); 13222ccbbd06SMarcelo Araujo } 13232ccbbd06SMarcelo Araujo *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag); 13242ccbbd06SMarcelo Araujo } 13252ccbbd06SMarcelo Araujo 1326fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 1327c0304424SGleb Smirnoff if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1); 1328d148c2a2SMatt Joras VLAN_RUNLOCK(); 13292cc2df49SGarrett Wollman 1330a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 1331fdbf1174SMatt Joras (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m); 13322cc2df49SGarrett Wollman } 13332cc2df49SGarrett Wollman 1334d148c2a2SMatt Joras static void 1335d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused) 1336d148c2a2SMatt Joras { 1337d148c2a2SMatt Joras struct ifvlan *ifv; 1338d148c2a2SMatt Joras struct ifnet *ifp; 1339d148c2a2SMatt Joras 1340d148c2a2SMatt Joras ifv = (struct ifvlan *)arg; 1341d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 1342d148c2a2SMatt Joras /* The ifv_ifp already has the lladdr copied in. */ 1343d148c2a2SMatt Joras if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen); 1344d148c2a2SMatt Joras } 1345d148c2a2SMatt Joras 13462cc2df49SGarrett Wollman static int 13477983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid) 13482cc2df49SGarrett Wollman { 134975ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 13501cf236fbSYaroslav Tykhiy struct ifnet *ifp; 135175ee267cSGleb Smirnoff int error = 0; 13522cc2df49SGarrett Wollman 1353b1828acfSGleb Smirnoff /* 1354b1828acfSGleb Smirnoff * We can handle non-ethernet hardware types as long as 1355b1828acfSGleb Smirnoff * they handle the tagging and headers themselves. 1356b1828acfSGleb Smirnoff */ 1357e4cd31ddSJeff Roberson if (p->if_type != IFT_ETHER && 1358e4cd31ddSJeff Roberson (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 135915a66c21SBruce M Simpson return (EPROTONOSUPPORT); 136064a17d2eSYaroslav Tykhiy if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 136164a17d2eSYaroslav Tykhiy return (EPROTONOSUPPORT); 1362b1828acfSGleb Smirnoff /* 1363b1828acfSGleb Smirnoff * Don't let the caller set up a VLAN VID with 1364b1828acfSGleb Smirnoff * anything except VLID bits. 1365b1828acfSGleb Smirnoff * VID numbers 0x0 and 0xFFF are reserved. 1366b1828acfSGleb Smirnoff */ 1367b1828acfSGleb Smirnoff if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK)) 1368b1828acfSGleb Smirnoff return (EINVAL); 136975ee267cSGleb Smirnoff if (ifv->ifv_trunk) 137015a66c21SBruce M Simpson return (EBUSY); 13712cc2df49SGarrett Wollman 1372d148c2a2SMatt Joras /* Acquire rmlock after the branch so we can M_WAITOK. */ 1373d148c2a2SMatt Joras VLAN_XLOCK(); 137475ee267cSGleb Smirnoff if (p->if_vlantrunk == NULL) { 137575ee267cSGleb Smirnoff trunk = malloc(sizeof(struct ifvlantrunk), 137675ee267cSGleb Smirnoff M_VLAN, M_WAITOK | M_ZERO); 137775ee267cSGleb Smirnoff vlan_inithash(trunk); 137875ee267cSGleb Smirnoff TRUNK_LOCK_INIT(trunk); 1379d148c2a2SMatt Joras VLAN_WLOCK(); 1380d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 138175ee267cSGleb Smirnoff p->if_vlantrunk = trunk; 138275ee267cSGleb Smirnoff trunk->parent = p; 13839bcf3ae4SAlexander Motin if_ref(trunk->parent); 138475ee267cSGleb Smirnoff } else { 1385d148c2a2SMatt Joras VLAN_WLOCK(); 138675ee267cSGleb Smirnoff trunk = p->if_vlantrunk; 1387d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 138875ee267cSGleb Smirnoff } 138975ee267cSGleb Smirnoff 13907983103aSRobert Watson ifv->ifv_vid = vid; /* must set this before vlan_inshash() */ 13912ccbbd06SMarcelo Araujo ifv->ifv_pcp = 0; /* Default: best effort delivery. */ 13922ccbbd06SMarcelo Araujo vlan_tag_recalculate(ifv); 139375ee267cSGleb Smirnoff error = vlan_inshash(trunk, ifv); 139475ee267cSGleb Smirnoff if (error) 139575ee267cSGleb Smirnoff goto done; 139673f2233dSYaroslav Tykhiy ifv->ifv_proto = ETHERTYPE_VLAN; 1397a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1398a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 13991cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 1400d89baa5aSAlexander Motin ifv->ifv_capenable = -1; 1401a3814acfSSam Leffler 1402a3814acfSSam Leffler /* 1403a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 1404a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1405656acce4SYaroslav Tykhiy * use it. 1406a3814acfSSam Leffler */ 1407656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 1408656acce4SYaroslav Tykhiy /* 1409656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 1410656acce4SYaroslav Tykhiy * handle extended frames. 1411656acce4SYaroslav Tykhiy */ 1412a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 1413656acce4SYaroslav Tykhiy } else { 1414a3814acfSSam Leffler /* 1415a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 1416a3814acfSSam Leffler * makes us incompatible with strictly compliant 1417a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 1418a3814acfSSam Leffler * the feature with other NetBSD implementations, 1419a3814acfSSam Leffler * which might still be useful. 1420a3814acfSSam Leffler */ 1421a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 1422a3814acfSSam Leffler } 1423a3814acfSSam Leffler 142475ee267cSGleb Smirnoff ifv->ifv_trunk = trunk; 14251cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 1426e4cd31ddSJeff Roberson /* 1427e4cd31ddSJeff Roberson * Initialize fields from our parent. This duplicates some 1428e4cd31ddSJeff Roberson * work with ether_ifattach() but allows for non-ethernet 1429e4cd31ddSJeff Roberson * interfaces to also work. 1430e4cd31ddSJeff Roberson */ 14311cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 143275ee267cSGleb Smirnoff ifp->if_baudrate = p->if_baudrate; 1433e4cd31ddSJeff Roberson ifp->if_output = p->if_output; 1434e4cd31ddSJeff Roberson ifp->if_input = p->if_input; 1435e4cd31ddSJeff Roberson ifp->if_resolvemulti = p->if_resolvemulti; 1436e4cd31ddSJeff Roberson ifp->if_addrlen = p->if_addrlen; 1437e4cd31ddSJeff Roberson ifp->if_broadcastaddr = p->if_broadcastaddr; 1438*32a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 1439e4cd31ddSJeff Roberson 14402cc2df49SGarrett Wollman /* 144124993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 144224993214SYaroslav Tykhiy * Other flags are none of our business. 14432cc2df49SGarrett Wollman */ 144464a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 14451cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 14461cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 14471cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 14481cf236fbSYaroslav Tykhiy 14491cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 14502cc2df49SGarrett Wollman 145175ee267cSGleb Smirnoff vlan_capabilities(ifv); 1452a3814acfSSam Leffler 1453a3814acfSSam Leffler /* 1454e4cd31ddSJeff Roberson * Set up our interface address to reflect the underlying 14552cc2df49SGarrett Wollman * physical interface's. 14562cc2df49SGarrett Wollman */ 1457e4cd31ddSJeff Roberson bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen); 1458e4cd31ddSJeff Roberson ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen = 1459e4cd31ddSJeff Roberson p->if_addrlen; 14601b2a4f7aSBill Fenner 14611b2a4f7aSBill Fenner /* 14621b2a4f7aSBill Fenner * Configure multicast addresses that may already be 14631b2a4f7aSBill Fenner * joined on the vlan device. 14641b2a4f7aSBill Fenner */ 1465d148c2a2SMatt Joras (void)vlan_setmulti(ifp); 1466d148c2a2SMatt Joras 1467d148c2a2SMatt Joras TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv); 14682ada9747SYaroslav Tykhiy 14692ada9747SYaroslav Tykhiy /* We are ready for operation now. */ 14702ada9747SYaroslav Tykhiy ifp->if_drv_flags |= IFF_DRV_RUNNING; 1471d148c2a2SMatt Joras 1472d148c2a2SMatt Joras /* Update flags on the parent, if necessary. */ 1473d148c2a2SMatt Joras vlan_setflags(ifp, 1); 147475ee267cSGleb Smirnoff done: 1475d148c2a2SMatt Joras /* 1476d148c2a2SMatt Joras * We need to drop the non-sleepable rmlock so that the underlying 1477d148c2a2SMatt Joras * devices can sleep in their vlan_config hooks. 1478d148c2a2SMatt Joras */ 1479d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 1480d148c2a2SMatt Joras VLAN_WUNLOCK(); 1481c725524cSJack F Vogel if (error == 0) 14827983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid); 1483d148c2a2SMatt Joras VLAN_XUNLOCK(); 148475ee267cSGleb Smirnoff 148575ee267cSGleb Smirnoff return (error); 14862cc2df49SGarrett Wollman } 14872cc2df49SGarrett Wollman 14886f359e28SJohn Baldwin static void 1489f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 1490f731f104SBill Paul { 14915cb8c31aSYaroslav Tykhiy 1492d148c2a2SMatt Joras VLAN_XLOCK(); 149328cc4d37SJohn Baldwin vlan_unconfig_locked(ifp, 0); 1494d148c2a2SMatt Joras VLAN_XUNLOCK(); 14955cb8c31aSYaroslav Tykhiy } 14965cb8c31aSYaroslav Tykhiy 14976f359e28SJohn Baldwin static void 149828cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing) 14995cb8c31aSYaroslav Tykhiy { 150075ee267cSGleb Smirnoff struct ifvlantrunk *trunk; 1501f731f104SBill Paul struct vlan_mc_entry *mc; 1502f731f104SBill Paul struct ifvlan *ifv; 1503c725524cSJack F Vogel struct ifnet *parent; 150428cc4d37SJohn Baldwin int error; 1505f731f104SBill Paul 1506d148c2a2SMatt Joras VLAN_XLOCK_ASSERT(); 15074faedfe8SSam Leffler 1508f731f104SBill Paul ifv = ifp->if_softc; 150975ee267cSGleb Smirnoff trunk = ifv->ifv_trunk; 151022893351SJack F Vogel parent = NULL; 1511f731f104SBill Paul 151222893351SJack F Vogel if (trunk != NULL) { 1513d148c2a2SMatt Joras /* 1514d148c2a2SMatt Joras * Both vlan_transmit and vlan_input rely on the trunk fields 1515d148c2a2SMatt Joras * being NULL to determine whether to bail, so we need to get 1516d148c2a2SMatt Joras * an exclusive lock here to prevent them from using bad 1517d148c2a2SMatt Joras * ifvlans. 1518d148c2a2SMatt Joras */ 1519d148c2a2SMatt Joras VLAN_WLOCK(); 152022893351SJack F Vogel parent = trunk->parent; 15211b2a4f7aSBill Fenner 1522f731f104SBill Paul /* 1523f731f104SBill Paul * Since the interface is being unconfigured, we need to 1524f731f104SBill Paul * empty the list of multicast groups that we may have joined 15251b2a4f7aSBill Fenner * while we were alive from the parent's list. 1526f731f104SBill Paul */ 1527c0cb022bSYaroslav Tykhiy while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 15286f359e28SJohn Baldwin /* 152928cc4d37SJohn Baldwin * If the parent interface is being detached, 1530b90dde2fSJohn Baldwin * all its multicast addresses have already 153128cc4d37SJohn Baldwin * been removed. Warn about errors if 153228cc4d37SJohn Baldwin * if_delmulti() does fail, but don't abort as 153328cc4d37SJohn Baldwin * all callers expect vlan destruction to 153428cc4d37SJohn Baldwin * succeed. 15356f359e28SJohn Baldwin */ 153628cc4d37SJohn Baldwin if (!departing) { 153728cc4d37SJohn Baldwin error = if_delmulti(parent, 1538e4cd31ddSJeff Roberson (struct sockaddr *)&mc->mc_addr); 153928cc4d37SJohn Baldwin if (error) 154028cc4d37SJohn Baldwin if_printf(ifp, 154128cc4d37SJohn Baldwin "Failed to delete multicast address from parent: %d\n", 154228cc4d37SJohn Baldwin error); 154328cc4d37SJohn Baldwin } 1544f731f104SBill Paul SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 15459d4fe4b2SBrooks Davis free(mc, M_VLAN); 1546f731f104SBill Paul } 1547a3814acfSSam Leffler 15481cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 1549d148c2a2SMatt Joras 1550d148c2a2SMatt Joras /* 1551d148c2a2SMatt Joras * The trunk lock isn't actually required here, but 1552d148c2a2SMatt Joras * vlan_remhash expects it. 1553d148c2a2SMatt Joras */ 1554d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 155575ee267cSGleb Smirnoff vlan_remhash(trunk, ifv); 1556d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 155775ee267cSGleb Smirnoff ifv->ifv_trunk = NULL; 155875ee267cSGleb Smirnoff 155975ee267cSGleb Smirnoff /* 156075ee267cSGleb Smirnoff * Check if we were the last. 156175ee267cSGleb Smirnoff */ 156275ee267cSGleb Smirnoff if (trunk->refcnt == 0) { 15632d222cb7SAlexander Motin parent->if_vlantrunk = NULL; 156475ee267cSGleb Smirnoff trunk_destroy(trunk); 1565d148c2a2SMatt Joras } 1566d148c2a2SMatt Joras VLAN_WUNLOCK(); 15671b2a4f7aSBill Fenner } 1568f731f104SBill Paul 1569f731f104SBill Paul /* Disconnect from parent. */ 15701cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 15711cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 15725cb8c31aSYaroslav Tykhiy ifp->if_mtu = ETHERMTU; 15735cb8c31aSYaroslav Tykhiy ifp->if_link_state = LINK_STATE_UNKNOWN; 15745cb8c31aSYaroslav Tykhiy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1575f731f104SBill Paul 157622893351SJack F Vogel /* 157722893351SJack F Vogel * Only dispatch an event if vlan was 157822893351SJack F Vogel * attached, otherwise there is nothing 157922893351SJack F Vogel * to cleanup anyway. 158022893351SJack F Vogel */ 158122893351SJack F Vogel if (parent != NULL) 15827983103aSRobert Watson EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid); 1583f731f104SBill Paul } 1584f731f104SBill Paul 15851cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 1586f731f104SBill Paul static int 15871cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 15881cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 1589a3814acfSSam Leffler { 15901cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 15911cf236fbSYaroslav Tykhiy int error; 1592a3814acfSSam Leffler 1593d148c2a2SMatt Joras VLAN_SXLOCK_ASSERT(); 1594a3814acfSSam Leffler 15951cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 15961cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 15971cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 15981cf236fbSYaroslav Tykhiy 15991cf236fbSYaroslav Tykhiy /* 16001cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 16011cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 16021cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 16031cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 16041cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 16051cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 16061cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 16071cf236fbSYaroslav Tykhiy */ 16081cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 160975ee267cSGleb Smirnoff error = (*func)(PARENT(ifv), status); 16101cf236fbSYaroslav Tykhiy if (error) 1611a3814acfSSam Leffler return (error); 16121cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 16131cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 16141cf236fbSYaroslav Tykhiy } 16151cf236fbSYaroslav Tykhiy return (0); 16161cf236fbSYaroslav Tykhiy } 16171cf236fbSYaroslav Tykhiy 16181cf236fbSYaroslav Tykhiy /* 16191cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 16201cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 16211cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 16221cf236fbSYaroslav Tykhiy */ 16231cf236fbSYaroslav Tykhiy static int 16241cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 16251cf236fbSYaroslav Tykhiy { 16261cf236fbSYaroslav Tykhiy int error, i; 16271cf236fbSYaroslav Tykhiy 16281cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 16291cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 16301cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 16311cf236fbSYaroslav Tykhiy if (error) 16321cf236fbSYaroslav Tykhiy return (error); 16331cf236fbSYaroslav Tykhiy } 16341cf236fbSYaroslav Tykhiy return (0); 1635a3814acfSSam Leffler } 1636a3814acfSSam Leffler 1637127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 1638127d7b2dSAndre Oppermann static void 1639a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp) 1640127d7b2dSAndre Oppermann { 1641d148c2a2SMatt Joras struct ifvlantrunk *trunk; 1642127d7b2dSAndre Oppermann struct ifvlan *ifv; 1643d148c2a2SMatt Joras VLAN_LOCK_READER; 1644127d7b2dSAndre Oppermann 1645d148c2a2SMatt Joras /* Called from a taskqueue_swi task, so we cannot sleep. */ 1646d148c2a2SMatt Joras VLAN_RLOCK(); 1647d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1648d148c2a2SMatt Joras if (trunk == NULL) { 1649d148c2a2SMatt Joras VLAN_RUNLOCK(); 1650d148c2a2SMatt Joras return; 1651d148c2a2SMatt Joras } 1652d148c2a2SMatt Joras 1653d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 1654d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 1655aad0be7aSGleb Smirnoff ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1656fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 165775ee267cSGleb Smirnoff trunk->parent->if_link_state); 1658127d7b2dSAndre Oppermann } 1659d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 1660d148c2a2SMatt Joras VLAN_RUNLOCK(); 166175ee267cSGleb Smirnoff } 166275ee267cSGleb Smirnoff 166375ee267cSGleb Smirnoff static void 166475ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv) 166575ee267cSGleb Smirnoff { 1666d148c2a2SMatt Joras struct ifnet *p; 1667d148c2a2SMatt Joras struct ifnet *ifp; 16689fd573c3SHans Petter Selasky struct ifnet_hw_tsomax hw_tsomax; 1669d89baa5aSAlexander Motin int cap = 0, ena = 0, mena; 1670d89baa5aSAlexander Motin u_long hwa = 0; 167175ee267cSGleb Smirnoff 1672d148c2a2SMatt Joras VLAN_SXLOCK_ASSERT(); 1673d148c2a2SMatt Joras TRUNK_WLOCK_ASSERT(TRUNK(ifv)); 1674d148c2a2SMatt Joras p = PARENT(ifv); 1675d148c2a2SMatt Joras ifp = ifv->ifv_ifp; 167675ee267cSGleb Smirnoff 1677d89baa5aSAlexander Motin /* Mask parent interface enabled capabilities disabled by user. */ 1678d89baa5aSAlexander Motin mena = p->if_capenable & ifv->ifv_capenable; 1679d89baa5aSAlexander Motin 168075ee267cSGleb Smirnoff /* 168175ee267cSGleb Smirnoff * If the parent interface can do checksum offloading 168275ee267cSGleb Smirnoff * on VLANs, then propagate its hardware-assisted 168375ee267cSGleb Smirnoff * checksumming flags. Also assert that checksum 168475ee267cSGleb Smirnoff * offloading requires hardware VLAN tagging. 168575ee267cSGleb Smirnoff */ 168675ee267cSGleb Smirnoff if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1687d89baa5aSAlexander Motin cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 168875ee267cSGleb Smirnoff if (p->if_capenable & IFCAP_VLAN_HWCSUM && 168975ee267cSGleb Smirnoff p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1690d89baa5aSAlexander Motin ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6); 1691d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM) 1692d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP | 1693d89baa5aSAlexander Motin CSUM_UDP | CSUM_SCTP); 1694d89baa5aSAlexander Motin if (ena & IFCAP_TXCSUM_IPV6) 1695d89baa5aSAlexander Motin hwa |= p->if_hwassist & (CSUM_TCP_IPV6 | 1696d89baa5aSAlexander Motin CSUM_UDP_IPV6 | CSUM_SCTP_IPV6); 169775ee267cSGleb Smirnoff } 1698d89baa5aSAlexander Motin 16999b76d9cbSPyun YongHyeon /* 17009b76d9cbSPyun YongHyeon * If the parent interface can do TSO on VLANs then 17019b76d9cbSPyun YongHyeon * propagate the hardware-assisted flag. TSO on VLANs 17029b76d9cbSPyun YongHyeon * does not necessarily require hardware VLAN tagging. 17039b76d9cbSPyun YongHyeon */ 17049fd573c3SHans Petter Selasky memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 17059fd573c3SHans Petter Selasky if_hw_tsomax_common(p, &hw_tsomax); 17069fd573c3SHans Petter Selasky if_hw_tsomax_update(ifp, &hw_tsomax); 17079b76d9cbSPyun YongHyeon if (p->if_capabilities & IFCAP_VLAN_HWTSO) 1708d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TSO; 17099b76d9cbSPyun YongHyeon if (p->if_capenable & IFCAP_VLAN_HWTSO) { 1710d89baa5aSAlexander Motin ena |= mena & IFCAP_TSO; 1711d89baa5aSAlexander Motin if (ena & IFCAP_TSO) 1712d89baa5aSAlexander Motin hwa |= p->if_hwassist & CSUM_TSO; 17139b76d9cbSPyun YongHyeon } 171409fe6320SNavdeep Parhar 171509fe6320SNavdeep Parhar /* 171659150e91SAlexander Motin * If the parent interface can do LRO and checksum offloading on 171759150e91SAlexander Motin * VLANs, then guess it may do LRO on VLANs. False positive here 171859150e91SAlexander Motin * cost nothing, while false negative may lead to some confusions. 171959150e91SAlexander Motin */ 172059150e91SAlexander Motin if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 172159150e91SAlexander Motin cap |= p->if_capabilities & IFCAP_LRO; 172259150e91SAlexander Motin if (p->if_capenable & IFCAP_VLAN_HWCSUM) 172359150e91SAlexander Motin ena |= p->if_capenable & IFCAP_LRO; 172459150e91SAlexander Motin 172559150e91SAlexander Motin /* 172609fe6320SNavdeep Parhar * If the parent interface can offload TCP connections over VLANs then 172709fe6320SNavdeep Parhar * propagate its TOE capability to the VLAN interface. 172809fe6320SNavdeep Parhar * 172909fe6320SNavdeep Parhar * All TOE drivers in the tree today can deal with VLANs. If this 173009fe6320SNavdeep Parhar * changes then IFCAP_VLAN_TOE should be promoted to a full capability 173109fe6320SNavdeep Parhar * with its own bit. 173209fe6320SNavdeep Parhar */ 173309fe6320SNavdeep Parhar #define IFCAP_VLAN_TOE IFCAP_TOE 173409fe6320SNavdeep Parhar if (p->if_capabilities & IFCAP_VLAN_TOE) 1735d89baa5aSAlexander Motin cap |= p->if_capabilities & IFCAP_TOE; 173609fe6320SNavdeep Parhar if (p->if_capenable & IFCAP_VLAN_TOE) { 173709fe6320SNavdeep Parhar TOEDEV(ifp) = TOEDEV(p); 1738d89baa5aSAlexander Motin ena |= mena & IFCAP_TOE; 173909fe6320SNavdeep Parhar } 1740f3e7afe2SHans Petter Selasky 1741d89baa5aSAlexander Motin /* 1742d89baa5aSAlexander Motin * If the parent interface supports dynamic link state, so does the 1743d89baa5aSAlexander Motin * VLAN interface. 1744d89baa5aSAlexander Motin */ 1745d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_LINKSTATE); 1746d89baa5aSAlexander Motin ena |= (mena & IFCAP_LINKSTATE); 1747d89baa5aSAlexander Motin 1748f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1749f3e7afe2SHans Petter Selasky /* 1750f3e7afe2SHans Petter Selasky * If the parent interface supports ratelimiting, so does the 1751f3e7afe2SHans Petter Selasky * VLAN interface. 1752f3e7afe2SHans Petter Selasky */ 1753d89baa5aSAlexander Motin cap |= (p->if_capabilities & IFCAP_TXRTLMT); 1754d89baa5aSAlexander Motin ena |= (mena & IFCAP_TXRTLMT); 1755f3e7afe2SHans Petter Selasky #endif 1756d89baa5aSAlexander Motin 1757d89baa5aSAlexander Motin ifp->if_capabilities = cap; 1758d89baa5aSAlexander Motin ifp->if_capenable = ena; 1759d89baa5aSAlexander Motin ifp->if_hwassist = hwa; 176075ee267cSGleb Smirnoff } 176175ee267cSGleb Smirnoff 176275ee267cSGleb Smirnoff static void 176375ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp) 176475ee267cSGleb Smirnoff { 1765d148c2a2SMatt Joras struct ifvlantrunk *trunk; 176675ee267cSGleb Smirnoff struct ifvlan *ifv; 176775ee267cSGleb Smirnoff 1768d148c2a2SMatt Joras VLAN_SLOCK(); 1769d148c2a2SMatt Joras trunk = ifp->if_vlantrunk; 1770d148c2a2SMatt Joras if (trunk == NULL) { 1771d148c2a2SMatt Joras VLAN_SUNLOCK(); 1772d148c2a2SMatt Joras return; 1773d148c2a2SMatt Joras } 1774d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 1775d148c2a2SMatt Joras VLAN_FOREACH(ifv, trunk) { 177675ee267cSGleb Smirnoff vlan_capabilities(ifv); 177775ee267cSGleb Smirnoff } 1778d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 1779d148c2a2SMatt Joras VLAN_SUNLOCK(); 1780127d7b2dSAndre Oppermann } 1781127d7b2dSAndre Oppermann 1782a3814acfSSam Leffler static int 1783cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 17842cc2df49SGarrett Wollman { 17852cc2df49SGarrett Wollman struct ifnet *p; 17862cc2df49SGarrett Wollman struct ifreq *ifr; 1787e4cd31ddSJeff Roberson struct ifaddr *ifa; 17882cc2df49SGarrett Wollman struct ifvlan *ifv; 17892d222cb7SAlexander Motin struct ifvlantrunk *trunk; 17902cc2df49SGarrett Wollman struct vlanreq vlr; 17912cc2df49SGarrett Wollman int error = 0; 1792d148c2a2SMatt Joras VLAN_LOCK_READER; 17932cc2df49SGarrett Wollman 17942cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 1795e4cd31ddSJeff Roberson ifa = (struct ifaddr *) data; 17962cc2df49SGarrett Wollman ifv = ifp->if_softc; 17972cc2df49SGarrett Wollman 17982cc2df49SGarrett Wollman switch (cmd) { 1799e4cd31ddSJeff Roberson case SIOCSIFADDR: 1800e4cd31ddSJeff Roberson ifp->if_flags |= IFF_UP; 1801e4cd31ddSJeff Roberson #ifdef INET 1802e4cd31ddSJeff Roberson if (ifa->ifa_addr->sa_family == AF_INET) 1803e4cd31ddSJeff Roberson arp_ifinit(ifp, ifa); 1804e4cd31ddSJeff Roberson #endif 1805e4cd31ddSJeff Roberson break; 1806e4cd31ddSJeff Roberson case SIOCGIFADDR: 180738d958a6SBrooks Davis bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 180838d958a6SBrooks Davis ifp->if_addrlen); 1809e4cd31ddSJeff Roberson break; 1810b3cca108SBill Fenner case SIOCGIFMEDIA: 1811d148c2a2SMatt Joras VLAN_SLOCK(); 181275ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 1813d8564efdSEd Maste p = PARENT(ifv); 18149bcf3ae4SAlexander Motin if_ref(p); 1815d8564efdSEd Maste error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 18169bcf3ae4SAlexander Motin if_rele(p); 1817b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 1818b3cca108SBill Fenner if (error == 0) { 1819b3cca108SBill Fenner struct ifmediareq *ifmr; 1820b3cca108SBill Fenner 1821b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 1822b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1823b3cca108SBill Fenner ifmr->ifm_count = 1; 1824b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 1825b3cca108SBill Fenner ifmr->ifm_ulist, 1826b3cca108SBill Fenner sizeof(int)); 1827b3cca108SBill Fenner } 1828b3cca108SBill Fenner } 18294faedfe8SSam Leffler } else { 1830b3cca108SBill Fenner error = EINVAL; 18314faedfe8SSam Leffler } 1832d148c2a2SMatt Joras VLAN_SUNLOCK(); 1833b3cca108SBill Fenner break; 1834b3cca108SBill Fenner 1835b3cca108SBill Fenner case SIOCSIFMEDIA: 1836b3cca108SBill Fenner error = EINVAL; 1837b3cca108SBill Fenner break; 1838b3cca108SBill Fenner 18392cc2df49SGarrett Wollman case SIOCSIFMTU: 18402cc2df49SGarrett Wollman /* 18412cc2df49SGarrett Wollman * Set the interface MTU. 18422cc2df49SGarrett Wollman */ 1843d148c2a2SMatt Joras VLAN_SLOCK(); 1844d148c2a2SMatt Joras trunk = TRUNK(ifv); 1845d148c2a2SMatt Joras if (trunk != NULL) { 1846d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 1847a3814acfSSam Leffler if (ifr->ifr_mtu > 184875ee267cSGleb Smirnoff (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1849a3814acfSSam Leffler ifr->ifr_mtu < 1850a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 18512cc2df49SGarrett Wollman error = EINVAL; 1852a3814acfSSam Leffler else 18532cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 1854d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 1855a3814acfSSam Leffler } else 1856a3814acfSSam Leffler error = EINVAL; 1857d148c2a2SMatt Joras VLAN_SUNLOCK(); 18582cc2df49SGarrett Wollman break; 18592cc2df49SGarrett Wollman 18602cc2df49SGarrett Wollman case SIOCSETVLAN: 1861ccf7ba97SMarko Zec #ifdef VIMAGE 186215f6780eSRobert Watson /* 186315f6780eSRobert Watson * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN 186415f6780eSRobert Watson * interface to be delegated to a jail without allowing the 186515f6780eSRobert Watson * jail to change what underlying interface/VID it is 186615f6780eSRobert Watson * associated with. We are not entirely convinced that this 18675a39f779SRobert Watson * is the right way to accomplish that policy goal. 186815f6780eSRobert Watson */ 1869ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 1870ccf7ba97SMarko Zec error = EPERM; 1871ccf7ba97SMarko Zec break; 1872ccf7ba97SMarko Zec } 1873ccf7ba97SMarko Zec #endif 1874541d96aaSBrooks Davis error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr)); 18752cc2df49SGarrett Wollman if (error) 18762cc2df49SGarrett Wollman break; 18772cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 1878f731f104SBill Paul vlan_unconfig(ifp); 18792cc2df49SGarrett Wollman break; 18802cc2df49SGarrett Wollman } 18819bcf3ae4SAlexander Motin p = ifunit_ref(vlr.vlr_parent); 18821bdc73d3SEd Maste if (p == NULL) { 18832cc2df49SGarrett Wollman error = ENOENT; 18842cc2df49SGarrett Wollman break; 18852cc2df49SGarrett Wollman } 188675ee267cSGleb Smirnoff error = vlan_config(ifv, p, vlr.vlr_tag); 18879bcf3ae4SAlexander Motin if_rele(p); 18882cc2df49SGarrett Wollman break; 18892cc2df49SGarrett Wollman 18902cc2df49SGarrett Wollman case SIOCGETVLAN: 1891ccf7ba97SMarko Zec #ifdef VIMAGE 1892ccf7ba97SMarko Zec if (ifp->if_vnet != ifp->if_home_vnet) { 1893ccf7ba97SMarko Zec error = EPERM; 1894ccf7ba97SMarko Zec break; 1895ccf7ba97SMarko Zec } 1896ccf7ba97SMarko Zec #endif 189715a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 1898d148c2a2SMatt Joras VLAN_SLOCK(); 189975ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) { 190075ee267cSGleb Smirnoff strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 19019bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 19027983103aSRobert Watson vlr.vlr_tag = ifv->ifv_vid; 19032cc2df49SGarrett Wollman } 1904d148c2a2SMatt Joras VLAN_SUNLOCK(); 1905541d96aaSBrooks Davis error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr)); 19062cc2df49SGarrett Wollman break; 19072cc2df49SGarrett Wollman 19082cc2df49SGarrett Wollman case SIOCSIFFLAGS: 19092cc2df49SGarrett Wollman /* 19101cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 19111cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 19122cc2df49SGarrett Wollman */ 1913d148c2a2SMatt Joras VLAN_XLOCK(); 191475ee267cSGleb Smirnoff if (TRUNK(ifv) != NULL) 19151cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 1916d148c2a2SMatt Joras VLAN_XUNLOCK(); 19172cc2df49SGarrett Wollman break; 1918a3814acfSSam Leffler 1919f731f104SBill Paul case SIOCADDMULTI: 1920f731f104SBill Paul case SIOCDELMULTI: 192175ee267cSGleb Smirnoff /* 192275ee267cSGleb Smirnoff * If we don't have a parent, just remember the membership for 192375ee267cSGleb Smirnoff * when we do. 1924d148c2a2SMatt Joras * 1925d148c2a2SMatt Joras * XXX We need the rmlock here to avoid sleeping while 1926d148c2a2SMatt Joras * holding in6_multi_mtx. 192775ee267cSGleb Smirnoff */ 1928d148c2a2SMatt Joras VLAN_RLOCK(); 19292d222cb7SAlexander Motin trunk = TRUNK(ifv); 19302d222cb7SAlexander Motin if (trunk != NULL) { 1931d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 1932f731f104SBill Paul error = vlan_setmulti(ifp); 1933d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 19342d222cb7SAlexander Motin } 1935d148c2a2SMatt Joras VLAN_RUNLOCK(); 1936f731f104SBill Paul break; 193775ee267cSGleb Smirnoff 19382ccbbd06SMarcelo Araujo case SIOCGVLANPCP: 19392ccbbd06SMarcelo Araujo #ifdef VIMAGE 19402ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 19412ccbbd06SMarcelo Araujo error = EPERM; 19422ccbbd06SMarcelo Araujo break; 19432ccbbd06SMarcelo Araujo } 19442ccbbd06SMarcelo Araujo #endif 19452ccbbd06SMarcelo Araujo ifr->ifr_vlan_pcp = ifv->ifv_pcp; 19462ccbbd06SMarcelo Araujo break; 19472ccbbd06SMarcelo Araujo 19482ccbbd06SMarcelo Araujo case SIOCSVLANPCP: 19492ccbbd06SMarcelo Araujo #ifdef VIMAGE 19502ccbbd06SMarcelo Araujo if (ifp->if_vnet != ifp->if_home_vnet) { 19512ccbbd06SMarcelo Araujo error = EPERM; 19522ccbbd06SMarcelo Araujo break; 19532ccbbd06SMarcelo Araujo } 19542ccbbd06SMarcelo Araujo #endif 19552ccbbd06SMarcelo Araujo error = priv_check(curthread, PRIV_NET_SETVLANPCP); 19562ccbbd06SMarcelo Araujo if (error) 19572ccbbd06SMarcelo Araujo break; 19582ccbbd06SMarcelo Araujo if (ifr->ifr_vlan_pcp > 7) { 19592ccbbd06SMarcelo Araujo error = EINVAL; 19602ccbbd06SMarcelo Araujo break; 19612ccbbd06SMarcelo Araujo } 19622ccbbd06SMarcelo Araujo ifv->ifv_pcp = ifr->ifr_vlan_pcp; 1963*32a52e9eSNavdeep Parhar ifp->if_pcp = ifv->ifv_pcp; 19642ccbbd06SMarcelo Araujo vlan_tag_recalculate(ifv); 19654a381a9eSHans Petter Selasky /* broadcast event about PCP change */ 19664a381a9eSHans Petter Selasky EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 19672ccbbd06SMarcelo Araujo break; 19682ccbbd06SMarcelo Araujo 1969d89baa5aSAlexander Motin case SIOCSIFCAP: 1970d148c2a2SMatt Joras VLAN_SLOCK(); 1971d89baa5aSAlexander Motin ifv->ifv_capenable = ifr->ifr_reqcap; 1972d89baa5aSAlexander Motin trunk = TRUNK(ifv); 1973d89baa5aSAlexander Motin if (trunk != NULL) { 1974d148c2a2SMatt Joras TRUNK_WLOCK(trunk); 1975d89baa5aSAlexander Motin vlan_capabilities(ifv); 1976d148c2a2SMatt Joras TRUNK_WUNLOCK(trunk); 1977d89baa5aSAlexander Motin } 1978d148c2a2SMatt Joras VLAN_SUNLOCK(); 1979d89baa5aSAlexander Motin break; 1980d89baa5aSAlexander Motin 19812cc2df49SGarrett Wollman default: 1982e4cd31ddSJeff Roberson error = EINVAL; 1983e4cd31ddSJeff Roberson break; 19842cc2df49SGarrett Wollman } 198515a66c21SBruce M Simpson 198615a66c21SBruce M Simpson return (error); 19872cc2df49SGarrett Wollman } 1988f3e7afe2SHans Petter Selasky 1989f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1990f3e7afe2SHans Petter Selasky static int 1991f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp, 1992f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params *params, 1993f3e7afe2SHans Petter Selasky struct m_snd_tag **ppmt) 1994f3e7afe2SHans Petter Selasky { 1995f3e7afe2SHans Petter Selasky 1996f3e7afe2SHans Petter Selasky /* get trunk device */ 1997f3e7afe2SHans Petter Selasky ifp = vlan_trunkdev(ifp); 1998f3e7afe2SHans Petter Selasky if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0) 1999f3e7afe2SHans Petter Selasky return (EOPNOTSUPP); 2000f3e7afe2SHans Petter Selasky /* forward allocation request */ 2001f3e7afe2SHans Petter Selasky return (ifp->if_snd_tag_alloc(ifp, params, ppmt)); 2002f3e7afe2SHans Petter Selasky } 2003f3e7afe2SHans Petter Selasky #endif 2004