1c398230bSWarner Losh /*- 22cc2df49SGarrett Wollman * Copyright 1998 Massachusetts Institute of Technology 32cc2df49SGarrett Wollman * 42cc2df49SGarrett Wollman * Permission to use, copy, modify, and distribute this software and 52cc2df49SGarrett Wollman * its documentation for any purpose and without fee is hereby 62cc2df49SGarrett Wollman * granted, provided that both the above copyright notice and this 72cc2df49SGarrett Wollman * permission notice appear in all copies, that both the above 82cc2df49SGarrett Wollman * copyright notice and this permission notice appear in all 92cc2df49SGarrett Wollman * supporting documentation, and that the name of M.I.T. not be used 102cc2df49SGarrett Wollman * in advertising or publicity pertaining to distribution of the 112cc2df49SGarrett Wollman * software without specific, written prior permission. M.I.T. makes 122cc2df49SGarrett Wollman * no representations about the suitability of this software for any 132cc2df49SGarrett Wollman * purpose. It is provided "as is" without express or implied 142cc2df49SGarrett Wollman * warranty. 152cc2df49SGarrett Wollman * 162cc2df49SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 172cc2df49SGarrett Wollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 182cc2df49SGarrett Wollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 192cc2df49SGarrett Wollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 202cc2df49SGarrett Wollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212cc2df49SGarrett Wollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222cc2df49SGarrett Wollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 232cc2df49SGarrett Wollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 242cc2df49SGarrett Wollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 252cc2df49SGarrett Wollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 262cc2df49SGarrett Wollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 272cc2df49SGarrett Wollman * SUCH DAMAGE. 282cc2df49SGarrett Wollman * 29c3aac50fSPeter Wemm * $FreeBSD$ 302cc2df49SGarrett Wollman */ 312cc2df49SGarrett Wollman 322cc2df49SGarrett Wollman /* 332cc2df49SGarrett Wollman * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 342cc2df49SGarrett Wollman * Might be extended some day to also handle IEEE 802.1p priority 352cc2df49SGarrett Wollman * tagging. This is sort of sneaky in the implementation, since 362cc2df49SGarrett Wollman * we need to pretend to be enough of an Ethernet implementation 372cc2df49SGarrett Wollman * to make arp work. The way we do this is by telling everyone 382cc2df49SGarrett Wollman * that we are an Ethernet, and then catch the packets that 3946a32e61SPhilippe Charnier * ether_output() left on our output queue when it calls 402cc2df49SGarrett Wollman * if_start(), rewrite them for use by the real outgoing interface, 412cc2df49SGarrett Wollman * and ask it to send them. 422cc2df49SGarrett Wollman */ 432cc2df49SGarrett Wollman 442cc2df49SGarrett Wollman #include "opt_inet.h" 452cc2df49SGarrett Wollman 462cc2df49SGarrett Wollman #include <sys/param.h> 472cc2df49SGarrett Wollman #include <sys/kernel.h> 48f731f104SBill Paul #include <sys/malloc.h> 492cc2df49SGarrett Wollman #include <sys/mbuf.h> 502b120974SPeter Wemm #include <sys/module.h> 51f731f104SBill Paul #include <sys/queue.h> 522cc2df49SGarrett Wollman #include <sys/socket.h> 532cc2df49SGarrett Wollman #include <sys/sockio.h> 542cc2df49SGarrett Wollman #include <sys/sysctl.h> 552cc2df49SGarrett Wollman #include <sys/systm.h> 562cc2df49SGarrett Wollman 572cc2df49SGarrett Wollman #include <net/bpf.h> 582cc2df49SGarrett Wollman #include <net/ethernet.h> 592cc2df49SGarrett Wollman #include <net/if.h> 60f889d2efSBrooks Davis #include <net/if_clone.h> 612cc2df49SGarrett Wollman #include <net/if_arp.h> 622cc2df49SGarrett Wollman #include <net/if_dl.h> 632cc2df49SGarrett Wollman #include <net/if_types.h> 642cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 652cc2df49SGarrett Wollman 662cc2df49SGarrett Wollman #ifdef INET 672cc2df49SGarrett Wollman #include <netinet/in.h> 682cc2df49SGarrett Wollman #include <netinet/if_ether.h> 692cc2df49SGarrett Wollman #endif 702cc2df49SGarrett Wollman 719d4fe4b2SBrooks Davis #define VLANNAME "vlan" 729d4fe4b2SBrooks Davis 73a3814acfSSam Leffler struct vlan_mc_entry { 74a3814acfSSam Leffler struct ether_addr mc_addr; 75a3814acfSSam Leffler SLIST_ENTRY(vlan_mc_entry) mc_entries; 76a3814acfSSam Leffler }; 77a3814acfSSam Leffler 78a3814acfSSam Leffler struct ifvlan { 79fc74a9f9SBrooks Davis struct ifnet *ifv_ifp; 80a3814acfSSam Leffler struct ifnet *ifv_p; /* parent inteface of this vlan */ 811cf236fbSYaroslav Tykhiy int ifv_pflags; /* special flags we have set on parent */ 82a3814acfSSam Leffler struct ifv_linkmib { 83a3814acfSSam Leffler int ifvm_parent; 84a3814acfSSam Leffler int ifvm_encaplen; /* encapsulation length */ 85a3814acfSSam Leffler int ifvm_mtufudge; /* MTU fudged by this much */ 86a3814acfSSam Leffler int ifvm_mintu; /* min transmission unit */ 87a3814acfSSam Leffler u_int16_t ifvm_proto; /* encapsulation ethertype */ 88a3814acfSSam Leffler u_int16_t ifvm_tag; /* tag to apply on packets leaving if */ 89a3814acfSSam Leffler } ifv_mib; 90a3814acfSSam Leffler SLIST_HEAD(__vlan_mchead, vlan_mc_entry) vlan_mc_listhead; 91a3814acfSSam Leffler LIST_ENTRY(ifvlan) ifv_list; 92a3814acfSSam Leffler }; 93a3814acfSSam Leffler #define ifv_tag ifv_mib.ifvm_tag 94a3814acfSSam Leffler #define ifv_encaplen ifv_mib.ifvm_encaplen 95a3814acfSSam Leffler #define ifv_mtufudge ifv_mib.ifvm_mtufudge 96a3814acfSSam Leffler #define ifv_mintu ifv_mib.ifvm_mintu 97a3814acfSSam Leffler 981cf236fbSYaroslav Tykhiy /* Special flags we should propagate to parent */ 991cf236fbSYaroslav Tykhiy static struct { 1001cf236fbSYaroslav Tykhiy int flag; 1011cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int); 1021cf236fbSYaroslav Tykhiy } vlan_pflags[] = { 1031cf236fbSYaroslav Tykhiy {IFF_PROMISC, ifpromisc}, 1041cf236fbSYaroslav Tykhiy {IFF_ALLMULTI, if_allmulti}, 1051cf236fbSYaroslav Tykhiy {0, NULL} 1061cf236fbSYaroslav Tykhiy }; 107a3814acfSSam Leffler 1084a408dcbSBill Paul SYSCTL_DECL(_net_link); 109d2a75853SBill Fenner SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 1102cc2df49SGarrett Wollman SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 1112cc2df49SGarrett Wollman 1125e17543aSBrooks Davis static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface"); 1139d4fe4b2SBrooks Davis static LIST_HEAD(, ifvlan) ifv_list; 1142cc2df49SGarrett Wollman 1154faedfe8SSam Leffler /* 1164faedfe8SSam Leffler * Locking: one lock is used to guard both the ifv_list and modification 1174faedfe8SSam Leffler * to vlan data structures. We are rather conservative here; probably 1184faedfe8SSam Leffler * more than necessary. 1194faedfe8SSam Leffler */ 1204faedfe8SSam Leffler static struct mtx ifv_mtx; 1215e17543aSBrooks Davis #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, VLANNAME, NULL, MTX_DEF) 1224faedfe8SSam Leffler #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx) 1234faedfe8SSam Leffler #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED) 1244faedfe8SSam Leffler #define VLAN_LOCK() mtx_lock(&ifv_mtx) 1254faedfe8SSam Leffler #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx) 1264faedfe8SSam Leffler 1272cc2df49SGarrett Wollman static void vlan_start(struct ifnet *ifp); 1282cc2df49SGarrett Wollman static void vlan_ifinit(void *foo); 129a3814acfSSam Leffler static void vlan_input(struct ifnet *ifp, struct mbuf *m); 130cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 1311cf236fbSYaroslav Tykhiy static int vlan_setflag(struct ifnet *ifp, int flag, int status, 1321cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)); 1331cf236fbSYaroslav Tykhiy static int vlan_setflags(struct ifnet *ifp, int status); 134f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 135f731f104SBill Paul static int vlan_unconfig(struct ifnet *ifp); 136f731f104SBill Paul static int vlan_config(struct ifvlan *ifv, struct ifnet *p); 137127d7b2dSAndre Oppermann static void vlan_link_state(struct ifnet *ifp, int link); 138f731f104SBill Paul 139f889d2efSBrooks Davis static struct ifnet *vlan_clone_match_ethertag(struct if_clone *, 140f889d2efSBrooks Davis const char *, int *); 141f889d2efSBrooks Davis static int vlan_clone_match(struct if_clone *, const char *); 142f889d2efSBrooks Davis static int vlan_clone_create(struct if_clone *, char *, size_t); 143f889d2efSBrooks Davis static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 144f889d2efSBrooks Davis 145c6e6ca3eSYaroslav Tykhiy static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL, 146c6e6ca3eSYaroslav Tykhiy IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy); 1479d4fe4b2SBrooks Davis 148f731f104SBill Paul /* 149f731f104SBill Paul * Program our multicast filter. What we're actually doing is 150f731f104SBill Paul * programming the multicast filter of the parent. This has the 151f731f104SBill Paul * side effect of causing the parent interface to receive multicast 152f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 153f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 154f731f104SBill Paul * to avoid this: there really is only one physical interface. 15529c2dfbeSBruce M Simpson * 15629c2dfbeSBruce M Simpson * XXX: There is a possible race here if more than one thread is 15729c2dfbeSBruce M Simpson * modifying the multicast state of the vlan interface at the same time. 158f731f104SBill Paul */ 1592b120974SPeter Wemm static int 1602b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 161f731f104SBill Paul { 162f731f104SBill Paul struct ifnet *ifp_p; 163f731f104SBill Paul struct ifmultiaddr *ifma, *rifma = NULL; 164f731f104SBill Paul struct ifvlan *sc; 165f731f104SBill Paul struct vlan_mc_entry *mc = NULL; 166f731f104SBill Paul struct sockaddr_dl sdl; 167f731f104SBill Paul int error; 168f731f104SBill Paul 16929c2dfbeSBruce M Simpson /*VLAN_LOCK_ASSERT();*/ 17029c2dfbeSBruce M Simpson 171f731f104SBill Paul /* Find the parent. */ 172f731f104SBill Paul sc = ifp->if_softc; 173f731f104SBill Paul ifp_p = sc->ifv_p; 174f731f104SBill Paul 1751b2a4f7aSBill Fenner /* 1761b2a4f7aSBill Fenner * If we don't have a parent, just remember the membership for 1771b2a4f7aSBill Fenner * when we do. 1781b2a4f7aSBill Fenner */ 1791b2a4f7aSBill Fenner if (ifp_p == NULL) 1801b2a4f7aSBill Fenner return (0); 1811b2a4f7aSBill Fenner 18215a66c21SBruce M Simpson bzero((char *)&sdl, sizeof(sdl)); 18315a66c21SBruce M Simpson sdl.sdl_len = sizeof(sdl); 184f731f104SBill Paul sdl.sdl_family = AF_LINK; 18526e30963SBill Fenner sdl.sdl_index = ifp_p->if_index; 18626e30963SBill Fenner sdl.sdl_type = IFT_ETHER; 18724993214SYaroslav Tykhiy sdl.sdl_alen = ETHER_ADDR_LEN; 188f731f104SBill Paul 189f731f104SBill Paul /* First, remove any existing filter entries. */ 19022f29826SPoul-Henning Kamp while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) { 19122f29826SPoul-Henning Kamp mc = SLIST_FIRST(&sc->vlan_mc_listhead); 192f731f104SBill Paul bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 193f731f104SBill Paul error = if_delmulti(ifp_p, (struct sockaddr *)&sdl); 194f731f104SBill Paul if (error) 195f731f104SBill Paul return (error); 196f731f104SBill Paul SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 1979d4fe4b2SBrooks Davis free(mc, M_VLAN); 198f731f104SBill Paul } 199f731f104SBill Paul 200f731f104SBill Paul /* Now program new ones. */ 2016817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 202f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 203f731f104SBill Paul continue; 20429c2dfbeSBruce M Simpson mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 20529c2dfbeSBruce M Simpson if (mc == NULL) 20629c2dfbeSBruce M Simpson return (ENOMEM); 207f731f104SBill Paul bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 208f731f104SBill Paul (char *)&mc->mc_addr, ETHER_ADDR_LEN); 209f731f104SBill Paul SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 21026e30963SBill Fenner bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 21126e30963SBill Fenner LLADDR(&sdl), ETHER_ADDR_LEN); 212f731f104SBill Paul error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 213f731f104SBill Paul if (error) 214f731f104SBill Paul return (error); 215f731f104SBill Paul } 216f731f104SBill Paul 217f731f104SBill Paul return (0); 218f731f104SBill Paul } 2192cc2df49SGarrett Wollman 220a3814acfSSam Leffler /* 221a3814acfSSam Leffler * VLAN support can be loaded as a module. The only place in the 222a3814acfSSam Leffler * system that's intimately aware of this is ether_input. We hook 223a3814acfSSam Leffler * into this code through vlan_input_p which is defined there and 224a3814acfSSam Leffler * set here. Noone else in the system should be aware of this so 225a3814acfSSam Leffler * we use an explicit reference here. 226a3814acfSSam Leffler * 227a3814acfSSam Leffler * NB: Noone should ever need to check if vlan_input_p is null or 228a3814acfSSam Leffler * not. This is because interfaces have a count of the number 229a3814acfSSam Leffler * of active vlans (if_nvlans) and this should never be bumped 230a3814acfSSam Leffler * except by vlan_config--which is in this module so therefore 231a3814acfSSam Leffler * the module must be loaded and vlan_input_p must be non-NULL. 232a3814acfSSam Leffler */ 233a3814acfSSam Leffler extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 234a3814acfSSam Leffler 235984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */ 236127d7b2dSAndre Oppermann extern void (*vlan_link_state_p)(struct ifnet *, int); 237127d7b2dSAndre Oppermann 2382b120974SPeter Wemm static int 2392b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 2402b120974SPeter Wemm { 2419d4fe4b2SBrooks Davis 2422b120974SPeter Wemm switch (type) { 2432b120974SPeter Wemm case MOD_LOAD: 2449d4fe4b2SBrooks Davis LIST_INIT(&ifv_list); 2454faedfe8SSam Leffler VLAN_LOCK_INIT(); 2469d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 247127d7b2dSAndre Oppermann vlan_link_state_p = vlan_link_state; 2489d4fe4b2SBrooks Davis if_clone_attach(&vlan_cloner); 2492b120974SPeter Wemm break; 2502b120974SPeter Wemm case MOD_UNLOAD: 2519d4fe4b2SBrooks Davis if_clone_detach(&vlan_cloner); 2529d4fe4b2SBrooks Davis vlan_input_p = NULL; 253127d7b2dSAndre Oppermann vlan_link_state_p = NULL; 2549d4fe4b2SBrooks Davis while (!LIST_EMPTY(&ifv_list)) 255f889d2efSBrooks Davis vlan_clone_destroy(&vlan_cloner, 256fc74a9f9SBrooks Davis LIST_FIRST(&ifv_list)->ifv_ifp); 2574faedfe8SSam Leffler VLAN_LOCK_DESTROY(); 2589d4fe4b2SBrooks Davis break; 2593e019deaSPoul-Henning Kamp default: 2603e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 2612b120974SPeter Wemm } 26215a66c21SBruce M Simpson return (0); 2632b120974SPeter Wemm } 2642b120974SPeter Wemm 2652b120974SPeter Wemm static moduledata_t vlan_mod = { 2662b120974SPeter Wemm "if_vlan", 2672b120974SPeter Wemm vlan_modevent, 2682b120974SPeter Wemm 0 2692b120974SPeter Wemm }; 2702b120974SPeter Wemm 2712b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 272d35bcd3bSRuslan Ermilov MODULE_DEPEND(if_vlan, miibus, 1, 1, 1); 2732cc2df49SGarrett Wollman 274f889d2efSBrooks Davis static struct ifnet * 275f889d2efSBrooks Davis vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag) 2769d4fe4b2SBrooks Davis { 277f889d2efSBrooks Davis const char *cp; 278f889d2efSBrooks Davis struct ifnet *ifp; 27915a66c21SBruce M Simpson int t = 0; 280f889d2efSBrooks Davis 281f889d2efSBrooks Davis /* Check for <etherif>.<vlan> style interface names. */ 282f889d2efSBrooks Davis IFNET_RLOCK(); 283f889d2efSBrooks Davis TAILQ_FOREACH(ifp, &ifnet, if_link) { 284f889d2efSBrooks Davis if (ifp->if_type != IFT_ETHER) 285f889d2efSBrooks Davis continue; 286f889d2efSBrooks Davis if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0) 287f889d2efSBrooks Davis continue; 288f889d2efSBrooks Davis cp = name + strlen(ifp->if_xname); 289f889d2efSBrooks Davis if (*cp != '.') 290f889d2efSBrooks Davis continue; 291f889d2efSBrooks Davis for(; *cp != '\0'; cp++) { 292f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 293f889d2efSBrooks Davis continue; 294f889d2efSBrooks Davis t = (t * 10) + (*cp - '0'); 295f889d2efSBrooks Davis } 296f889d2efSBrooks Davis if (tag != NULL) 297f889d2efSBrooks Davis *tag = t; 298f889d2efSBrooks Davis break; 299f889d2efSBrooks Davis } 300f889d2efSBrooks Davis IFNET_RUNLOCK(); 301f889d2efSBrooks Davis 30215a66c21SBruce M Simpson return (ifp); 303f889d2efSBrooks Davis } 304f889d2efSBrooks Davis 305f889d2efSBrooks Davis static int 306f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name) 307f889d2efSBrooks Davis { 308f889d2efSBrooks Davis const char *cp; 309f889d2efSBrooks Davis 310f889d2efSBrooks Davis if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL) 311f889d2efSBrooks Davis return (1); 312f889d2efSBrooks Davis 313f889d2efSBrooks Davis if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0) 314f889d2efSBrooks Davis return (0); 315f889d2efSBrooks Davis for (cp = name + 4; *cp != '\0'; cp++) { 316f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 317f889d2efSBrooks Davis return (0); 318f889d2efSBrooks Davis } 319f889d2efSBrooks Davis 320f889d2efSBrooks Davis return (1); 321f889d2efSBrooks Davis } 322f889d2efSBrooks Davis 323f889d2efSBrooks Davis static int 324f889d2efSBrooks Davis vlan_clone_create(struct if_clone *ifc, char *name, size_t len) 325f889d2efSBrooks Davis { 326f889d2efSBrooks Davis char *dp; 327f889d2efSBrooks Davis int wildcard; 328f889d2efSBrooks Davis int unit; 329f889d2efSBrooks Davis int error; 330f889d2efSBrooks Davis int tag; 331f889d2efSBrooks Davis int ethertag; 3329d4fe4b2SBrooks Davis struct ifvlan *ifv; 3339d4fe4b2SBrooks Davis struct ifnet *ifp; 334f889d2efSBrooks Davis struct ifnet *p; 335fc74a9f9SBrooks Davis u_char eaddr[6] = {0,0,0,0,0,0}; 336f889d2efSBrooks Davis 337f889d2efSBrooks Davis if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) { 338f889d2efSBrooks Davis ethertag = 1; 339f889d2efSBrooks Davis unit = -1; 340f889d2efSBrooks Davis wildcard = 0; 341f889d2efSBrooks Davis 342f889d2efSBrooks Davis /* 343f889d2efSBrooks Davis * Don't let the caller set up a VLAN tag with 344f889d2efSBrooks Davis * anything except VLID bits. 345f889d2efSBrooks Davis */ 34615a66c21SBruce M Simpson if (tag & ~EVL_VLID_MASK) 347f889d2efSBrooks Davis return (EINVAL); 348f889d2efSBrooks Davis } else { 349f889d2efSBrooks Davis ethertag = 0; 350f889d2efSBrooks Davis 351f889d2efSBrooks Davis error = ifc_name2unit(name, &unit); 352f889d2efSBrooks Davis if (error != 0) 353f889d2efSBrooks Davis return (error); 354f889d2efSBrooks Davis 355f889d2efSBrooks Davis wildcard = (unit < 0); 356f889d2efSBrooks Davis } 357f889d2efSBrooks Davis 358f889d2efSBrooks Davis error = ifc_alloc_unit(ifc, &unit); 359f889d2efSBrooks Davis if (error != 0) 360f889d2efSBrooks Davis return (error); 361f889d2efSBrooks Davis 362f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 363f889d2efSBrooks Davis if (wildcard) { 364f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 365f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 366f889d2efSBrooks Davis len - (dp-name) - 1) { 367f889d2efSBrooks Davis panic("%s: interface name too long", __func__); 368f889d2efSBrooks Davis } 369f889d2efSBrooks Davis } 3709d4fe4b2SBrooks Davis 371a163d034SWarner Losh ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 372fc74a9f9SBrooks Davis ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 373fc74a9f9SBrooks Davis if (ifp == NULL) { 374fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 375fc74a9f9SBrooks Davis free(ifv, M_VLAN); 376fc74a9f9SBrooks Davis return (ENOSPC); 377fc74a9f9SBrooks Davis } 3789d4fe4b2SBrooks Davis SLIST_INIT(&ifv->vlan_mc_listhead); 3799d4fe4b2SBrooks Davis 3809d4fe4b2SBrooks Davis ifp->if_softc = ifv; 381f889d2efSBrooks Davis /* 382cab574d8SYaroslav Tykhiy * Set the name manually rather than using if_initname because 383f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 384f889d2efSBrooks Davis */ 385f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 386f889d2efSBrooks Davis ifp->if_dname = ifc->ifc_name; 387f889d2efSBrooks Davis ifp->if_dunit = unit; 3889d4fe4b2SBrooks Davis /* NB: flags are not set here */ 3899d4fe4b2SBrooks Davis ifp->if_linkmib = &ifv->ifv_mib; 39015a66c21SBruce M Simpson ifp->if_linkmiblen = sizeof(ifv->ifv_mib); 3919d4fe4b2SBrooks Davis /* NB: mtu is not set here */ 3929d4fe4b2SBrooks Davis 3939d4fe4b2SBrooks Davis ifp->if_init = vlan_ifinit; 3949d4fe4b2SBrooks Davis ifp->if_start = vlan_start; 3959d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 3969d4fe4b2SBrooks Davis ifp->if_snd.ifq_maxlen = ifqmaxlen; 397fc74a9f9SBrooks Davis ether_ifattach(ifp, eaddr); 3989d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 399211f625aSBill Fenner ifp->if_baudrate = 0; 400a3814acfSSam Leffler ifp->if_type = IFT_L2VLAN; 401a3814acfSSam Leffler ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 4029d4fe4b2SBrooks Davis 4034faedfe8SSam Leffler VLAN_LOCK(); 4044faedfe8SSam Leffler LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list); 4054faedfe8SSam Leffler VLAN_UNLOCK(); 4064faedfe8SSam Leffler 407f889d2efSBrooks Davis if (ethertag) { 408f889d2efSBrooks Davis VLAN_LOCK(); 409f889d2efSBrooks Davis error = vlan_config(ifv, p); 410f889d2efSBrooks Davis if (error != 0) { 411f889d2efSBrooks Davis /* 412f889d2efSBrooks Davis * Since we've partialy failed, we need to back 413f889d2efSBrooks Davis * out all the way, otherwise userland could get 414f889d2efSBrooks Davis * confused. Thus, we destroy the interface. 415f889d2efSBrooks Davis */ 416f889d2efSBrooks Davis LIST_REMOVE(ifv, ifv_list); 417f889d2efSBrooks Davis vlan_unconfig(ifp); 418f889d2efSBrooks Davis VLAN_UNLOCK(); 419f889d2efSBrooks Davis ether_ifdetach(ifp); 420fc74a9f9SBrooks Davis if_free_type(ifp, IFT_ETHER); 421f889d2efSBrooks Davis free(ifv, M_VLAN); 422f889d2efSBrooks Davis 423f889d2efSBrooks Davis return (error); 424f889d2efSBrooks Davis } 425f889d2efSBrooks Davis ifv->ifv_tag = tag; 42613f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 427f889d2efSBrooks Davis VLAN_UNLOCK(); 428f889d2efSBrooks Davis 4291cf236fbSYaroslav Tykhiy /* Update flags on the parent, if necessary. */ 4301cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 1); 431f889d2efSBrooks Davis } 432f889d2efSBrooks Davis 4339d4fe4b2SBrooks Davis return (0); 4349d4fe4b2SBrooks Davis } 4359d4fe4b2SBrooks Davis 436f889d2efSBrooks Davis static int 437f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 4389d4fe4b2SBrooks Davis { 439b4e9f837SBrooks Davis int unit; 4409d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 4419d4fe4b2SBrooks Davis 442b4e9f837SBrooks Davis unit = ifp->if_dunit; 443b4e9f837SBrooks Davis 4444faedfe8SSam Leffler VLAN_LOCK(); 4459d4fe4b2SBrooks Davis LIST_REMOVE(ifv, ifv_list); 4469d4fe4b2SBrooks Davis vlan_unconfig(ifp); 4474faedfe8SSam Leffler VLAN_UNLOCK(); 4489d4fe4b2SBrooks Davis 449a3814acfSSam Leffler ether_ifdetach(ifp); 450f3447eb4SBrooks Davis if_free_type(ifp, IFT_ETHER); 4519d4fe4b2SBrooks Davis 4529d4fe4b2SBrooks Davis free(ifv, M_VLAN); 453f889d2efSBrooks Davis 454b4e9f837SBrooks Davis ifc_free_unit(ifc, unit); 455b4e9f837SBrooks Davis 456f889d2efSBrooks Davis return (0); 4579d4fe4b2SBrooks Davis } 4589d4fe4b2SBrooks Davis 45915a66c21SBruce M Simpson /* 46015a66c21SBruce M Simpson * The ifp->if_init entry point for vlan(4) is a no-op. 46115a66c21SBruce M Simpson */ 4622cc2df49SGarrett Wollman static void 4632cc2df49SGarrett Wollman vlan_ifinit(void *foo) 4642cc2df49SGarrett Wollman { 46515a66c21SBruce M Simpson 4662cc2df49SGarrett Wollman } 4672cc2df49SGarrett Wollman 4686d3a3ab7SGleb Smirnoff /* 4696d3a3ab7SGleb Smirnoff * The if_start method for vlan(4) interface. It doesn't 4706d3a3ab7SGleb Smirnoff * raises the IFF_DRV_OACTIVE flag, since it is called 4716d3a3ab7SGleb Smirnoff * only from IFQ_HANDOFF() macro in ether_output_frame(). 4726d3a3ab7SGleb Smirnoff * If the interface queue is full, and vlan_start() is 4736d3a3ab7SGleb Smirnoff * not called, the queue would never get emptied and 4746d3a3ab7SGleb Smirnoff * interface would stall forever. 4756d3a3ab7SGleb Smirnoff */ 4762cc2df49SGarrett Wollman static void 4772cc2df49SGarrett Wollman vlan_start(struct ifnet *ifp) 4782cc2df49SGarrett Wollman { 4792cc2df49SGarrett Wollman struct ifvlan *ifv; 4802cc2df49SGarrett Wollman struct ifnet *p; 4812cc2df49SGarrett Wollman struct ether_vlan_header *evl; 4825326b23cSMatthew N. Dodd struct mbuf *m; 483affc907dSMax Laier int error; 4842cc2df49SGarrett Wollman 4852cc2df49SGarrett Wollman ifv = ifp->if_softc; 4862cc2df49SGarrett Wollman p = ifv->ifv_p; 4872cc2df49SGarrett Wollman 4882cc2df49SGarrett Wollman for (;;) { 4892cc2df49SGarrett Wollman IF_DEQUEUE(&ifp->if_snd, m); 4902cc2df49SGarrett Wollman if (m == 0) 4912cc2df49SGarrett Wollman break; 492a3814acfSSam Leffler BPF_MTAP(ifp, m); 4932cc2df49SGarrett Wollman 494f731f104SBill Paul /* 49524993214SYaroslav Tykhiy * Do not run parent's if_start() if the parent is not up, 49624993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 49724993214SYaroslav Tykhiy */ 49813f4c340SRobert Watson if (!((p->if_flags & IFF_UP) && 49913f4c340SRobert Watson (p->if_drv_flags & IFF_DRV_RUNNING))) { 50024993214SYaroslav Tykhiy m_freem(m); 501a3814acfSSam Leffler ifp->if_collisions++; 50224993214SYaroslav Tykhiy continue; 50324993214SYaroslav Tykhiy } 50424993214SYaroslav Tykhiy 50524993214SYaroslav Tykhiy /* 506a3814acfSSam Leffler * If underlying interface can do VLAN tag insertion itself, 507a3814acfSSam Leffler * just pass the packet along. However, we need some way to 508a3814acfSSam Leffler * tell the interface where the packet came from so that it 509a3814acfSSam Leffler * knows how to find the VLAN tag to use, so we attach a 510a3814acfSSam Leffler * packet tag that holds it. 511f731f104SBill Paul */ 512b08347a0SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_HWTAGGING) { 5135016de40SSam Leffler struct m_tag *mtag = m_tag_alloc(MTAG_VLAN, 5145016de40SSam Leffler MTAG_VLAN_TAG, 5155016de40SSam Leffler sizeof(u_int), 5165016de40SSam Leffler M_NOWAIT); 517a3814acfSSam Leffler if (mtag == NULL) { 518a3814acfSSam Leffler ifp->if_oerrors++; 519a3814acfSSam Leffler m_freem(m); 520a3814acfSSam Leffler continue; 521a3814acfSSam Leffler } 522eefbcf0eSYaroslav Tykhiy VLAN_TAG_VALUE(mtag) = ifv->ifv_tag; 523a3814acfSSam Leffler m_tag_prepend(m, mtag); 524ba26134bSGleb Smirnoff m->m_flags |= M_VLANTAG; 525f731f104SBill Paul } else { 526a163d034SWarner Losh M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT); 5274af90a4dSMatthew N. Dodd if (m == NULL) { 5286cbd3e99SYaroslav Tykhiy if_printf(ifp, 5296cbd3e99SYaroslav Tykhiy "unable to prepend VLAN header\n"); 53026f9b263SRuslan Ermilov ifp->if_oerrors++; 5312cc2df49SGarrett Wollman continue; 5324af90a4dSMatthew N. Dodd } 5332cc2df49SGarrett Wollman /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 5342cc2df49SGarrett Wollman 535a3814acfSSam Leffler if (m->m_len < sizeof(*evl)) { 536a3814acfSSam Leffler m = m_pullup(m, sizeof(*evl)); 5375326b23cSMatthew N. Dodd if (m == NULL) { 538a3814acfSSam Leffler if_printf(ifp, 5396cbd3e99SYaroslav Tykhiy "cannot pullup VLAN header\n"); 54026f9b263SRuslan Ermilov ifp->if_oerrors++; 5414af90a4dSMatthew N. Dodd continue; 5424af90a4dSMatthew N. Dodd } 543a3814acfSSam Leffler } 5444af90a4dSMatthew N. Dodd 5452cc2df49SGarrett Wollman /* 5462cc2df49SGarrett Wollman * Transform the Ethernet header into an Ethernet header 5472cc2df49SGarrett Wollman * with 802.1Q encapsulation. 5482cc2df49SGarrett Wollman */ 549a3814acfSSam Leffler bcopy(mtod(m, char *) + ifv->ifv_encaplen, 550797f247bSMatthew N. Dodd mtod(m, char *), ETHER_HDR_LEN); 5512cc2df49SGarrett Wollman evl = mtod(m, struct ether_vlan_header *); 5522cc2df49SGarrett Wollman evl->evl_proto = evl->evl_encap_proto; 5539d4fe4b2SBrooks Davis evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 5542cc2df49SGarrett Wollman evl->evl_tag = htons(ifv->ifv_tag); 555f731f104SBill Paul #ifdef DEBUG 55683908c65SRuslan Ermilov printf("%s: %*D\n", __func__, (int)sizeof(*evl), 55783908c65SRuslan Ermilov (unsigned char *)evl, ":"); 558f731f104SBill Paul #endif 559f731f104SBill Paul } 5602cc2df49SGarrett Wollman 5612cc2df49SGarrett Wollman /* 5622cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 5632cc2df49SGarrett Wollman * We are already running at splimp. 5642cc2df49SGarrett Wollman */ 565affc907dSMax Laier IFQ_HANDOFF(p, m, error); 566affc907dSMax Laier if (!error) 567f731f104SBill Paul ifp->if_opackets++; 568df5e1987SJonathan Lemon else 569df5e1987SJonathan Lemon ifp->if_oerrors++; 5702cc2df49SGarrett Wollman } 571f731f104SBill Paul } 572f731f104SBill Paul 573a3814acfSSam Leffler static void 574a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m) 575f731f104SBill Paul { 576a3814acfSSam Leffler struct ether_vlan_header *evl; 577f731f104SBill Paul struct ifvlan *ifv; 578a3814acfSSam Leffler struct m_tag *mtag; 579a3814acfSSam Leffler u_int tag; 580a3814acfSSam Leffler 581f4ec4126SYaroslav Tykhiy if (m->m_flags & M_VLANTAG) { 582a3814acfSSam Leffler /* 58314e98256SYaroslav Tykhiy * Packet is tagged, but m contains a normal 584a3814acfSSam Leffler * Ethernet frame; the tag is stored out-of-band. 585a3814acfSSam Leffler */ 586f4ec4126SYaroslav Tykhiy mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL); 587f4ec4126SYaroslav Tykhiy KASSERT(mtag != NULL, 588f4ec4126SYaroslav Tykhiy ("%s: M_VLANTAG without m_tag", __func__)); 58926f9b263SRuslan Ermilov tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)); 590a3814acfSSam Leffler m_tag_delete(m, mtag); 5916ee20ab5SRuslan Ermilov m->m_flags &= ~M_VLANTAG; 592a3814acfSSam Leffler } else { 59314e98256SYaroslav Tykhiy /* 59414e98256SYaroslav Tykhiy * Packet is tagged in-band as specified by 802.1q. 59514e98256SYaroslav Tykhiy */ 596f4ec4126SYaroslav Tykhiy mtag = NULL; 597a3814acfSSam Leffler switch (ifp->if_type) { 598a3814acfSSam Leffler case IFT_ETHER: 599a3814acfSSam Leffler if (m->m_len < sizeof(*evl) && 600a3814acfSSam Leffler (m = m_pullup(m, sizeof(*evl))) == NULL) { 601a3814acfSSam Leffler if_printf(ifp, "cannot pullup VLAN header\n"); 602a3814acfSSam Leffler return; 603a3814acfSSam Leffler } 604a3814acfSSam Leffler evl = mtod(m, struct ether_vlan_header *); 605a3814acfSSam Leffler KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN, 606ffdd61c3SYaroslav Tykhiy ("%s: bad encapsulation protocol (%u)", 607ffdd61c3SYaroslav Tykhiy __func__, ntohs(evl->evl_encap_proto))); 608a3814acfSSam Leffler 609fb88a3e0SBill Paul tag = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 610f731f104SBill Paul 6117a46ec8fSBrooks Davis /* 612a3814acfSSam Leffler * Restore the original ethertype. We'll remove 613a3814acfSSam Leffler * the encapsulation after we've found the vlan 614a3814acfSSam Leffler * interface corresponding to the tag. 6157a46ec8fSBrooks Davis */ 616a3814acfSSam Leffler evl->evl_encap_proto = evl->evl_proto; 617a3814acfSSam Leffler break; 618a3814acfSSam Leffler default: 619a3814acfSSam Leffler tag = (u_int) -1; 620ffdd61c3SYaroslav Tykhiy #ifdef INVARIANTS 621ffdd61c3SYaroslav Tykhiy panic("%s: unsupported if_type (%u)", 622ffdd61c3SYaroslav Tykhiy __func__, ifp->if_type); 623a3814acfSSam Leffler #endif 624a3814acfSSam Leffler break; 625a3814acfSSam Leffler } 6267a46ec8fSBrooks Davis } 6277a46ec8fSBrooks Davis 6284faedfe8SSam Leffler VLAN_LOCK(); 6294faedfe8SSam Leffler LIST_FOREACH(ifv, &ifv_list, ifv_list) 630a3814acfSSam Leffler if (ifp == ifv->ifv_p && tag == ifv->ifv_tag) 631f731f104SBill Paul break; 632f731f104SBill Paul 633fc74a9f9SBrooks Davis if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) { 6344faedfe8SSam Leffler VLAN_UNLOCK(); 6357d3e4c6eSLuigi Rizzo m_freem(m); 636a3814acfSSam Leffler ifp->if_noproto++; 637b46f884bSJoerg Wunsch #ifdef DEBUG 638ffdd61c3SYaroslav Tykhiy printf("%s: tag %d, no interface\n", __func__, tag); 639b46f884bSJoerg Wunsch #endif 640a3814acfSSam Leffler return; 641f731f104SBill Paul } 6424faedfe8SSam Leffler VLAN_UNLOCK(); /* XXX extend below? */ 643b46f884bSJoerg Wunsch #ifdef DEBUG 644ffdd61c3SYaroslav Tykhiy printf("%s: tag %d, parent %s\n", __func__, tag, ifv->ifv_p->if_xname); 645b46f884bSJoerg Wunsch #endif 646f731f104SBill Paul 647a3814acfSSam Leffler if (mtag == NULL) { 648f731f104SBill Paul /* 649a3814acfSSam Leffler * Packet had an in-line encapsulation header; 650a3814acfSSam Leffler * remove it. The original header has already 651a3814acfSSam Leffler * been fixed up above. 652f731f104SBill Paul */ 653a3814acfSSam Leffler bcopy(mtod(m, caddr_t), 654a3814acfSSam Leffler mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN, 655797f247bSMatthew N. Dodd ETHER_HDR_LEN); 656a3814acfSSam Leffler m_adj(m, ETHER_VLAN_ENCAP_LEN); 657a3814acfSSam Leffler } 658a3814acfSSam Leffler 659fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = ifv->ifv_ifp; 660fc74a9f9SBrooks Davis ifv->ifv_ifp->if_ipackets++; 6612cc2df49SGarrett Wollman 662a3814acfSSam Leffler /* Pass it back through the parent's input routine. */ 663fc74a9f9SBrooks Davis (*ifp->if_input)(ifv->ifv_ifp, m); 6642cc2df49SGarrett Wollman } 6652cc2df49SGarrett Wollman 6662cc2df49SGarrett Wollman static int 6672cc2df49SGarrett Wollman vlan_config(struct ifvlan *ifv, struct ifnet *p) 6682cc2df49SGarrett Wollman { 6692cc2df49SGarrett Wollman struct ifaddr *ifa1, *ifa2; 6701cf236fbSYaroslav Tykhiy struct ifnet *ifp; 6712cc2df49SGarrett Wollman struct sockaddr_dl *sdl1, *sdl2; 6722cc2df49SGarrett Wollman 6734faedfe8SSam Leffler VLAN_LOCK_ASSERT(); 6744faedfe8SSam Leffler 6751cf236fbSYaroslav Tykhiy if (p->if_type != IFT_ETHER) 67615a66c21SBruce M Simpson return (EPROTONOSUPPORT); 6772cc2df49SGarrett Wollman if (ifv->ifv_p) 67815a66c21SBruce M Simpson return (EBUSY); 6792cc2df49SGarrett Wollman 680a3814acfSSam Leffler ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 681a3814acfSSam Leffler ifv->ifv_mintu = ETHERMIN; 6821cf236fbSYaroslav Tykhiy ifv->ifv_pflags = 0; 683a3814acfSSam Leffler 684a3814acfSSam Leffler /* 685d6fcfb7aSYaroslav Tykhiy * The active VLAN counter on the parent is used 686d6fcfb7aSYaroslav Tykhiy * at various places to see if there is a vlan(4) 687d6fcfb7aSYaroslav Tykhiy * attached to this physical interface. 688d6fcfb7aSYaroslav Tykhiy */ 689d6fcfb7aSYaroslav Tykhiy p->if_nvlans++; 690d6fcfb7aSYaroslav Tykhiy 691d6fcfb7aSYaroslav Tykhiy /* 692a3814acfSSam Leffler * If the parent supports the VLAN_MTU capability, 693a3814acfSSam Leffler * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 694656acce4SYaroslav Tykhiy * use it. 695a3814acfSSam Leffler */ 696656acce4SYaroslav Tykhiy if (p->if_capenable & IFCAP_VLAN_MTU) { 697656acce4SYaroslav Tykhiy /* 698656acce4SYaroslav Tykhiy * No need to fudge the MTU since the parent can 699656acce4SYaroslav Tykhiy * handle extended frames. 700656acce4SYaroslav Tykhiy */ 701a3814acfSSam Leffler ifv->ifv_mtufudge = 0; 702656acce4SYaroslav Tykhiy } else { 703a3814acfSSam Leffler /* 704a3814acfSSam Leffler * Fudge the MTU by the encapsulation size. This 705a3814acfSSam Leffler * makes us incompatible with strictly compliant 706a3814acfSSam Leffler * 802.1Q implementations, but allows us to use 707a3814acfSSam Leffler * the feature with other NetBSD implementations, 708a3814acfSSam Leffler * which might still be useful. 709a3814acfSSam Leffler */ 710a3814acfSSam Leffler ifv->ifv_mtufudge = ifv->ifv_encaplen; 711a3814acfSSam Leffler } 712a3814acfSSam Leffler 713a3814acfSSam Leffler ifv->ifv_p = p; 7141cf236fbSYaroslav Tykhiy ifp = ifv->ifv_ifp; 7151cf236fbSYaroslav Tykhiy ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 7162cc2df49SGarrett Wollman /* 71724993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 71824993214SYaroslav Tykhiy * Other flags are none of our business. 7192cc2df49SGarrett Wollman */ 7201cf236fbSYaroslav Tykhiy #define VLAN_COPY_FLAGS \ 7211cf236fbSYaroslav Tykhiy (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT) 7221cf236fbSYaroslav Tykhiy ifp->if_flags &= ~VLAN_COPY_FLAGS; 7231cf236fbSYaroslav Tykhiy ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 7241cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS 7251cf236fbSYaroslav Tykhiy 7261cf236fbSYaroslav Tykhiy ifp->if_link_state = p->if_link_state; 7272cc2df49SGarrett Wollman 728b08347a0SYaroslav Tykhiy #if 0 729b08347a0SYaroslav Tykhiy /* 730b08347a0SYaroslav Tykhiy * Not ready yet. We need notification from the parent 731b08347a0SYaroslav Tykhiy * when hw checksumming flags in its if_capenable change. 732b08347a0SYaroslav Tykhiy * Flags set in if_capabilities only are useless. 733b08347a0SYaroslav Tykhiy */ 7342cc2df49SGarrett Wollman /* 735a3814acfSSam Leffler * If the parent interface can do hardware-assisted 736a3814acfSSam Leffler * VLAN encapsulation, then propagate its hardware- 737a3814acfSSam Leffler * assisted checksumming flags. 738a3814acfSSam Leffler */ 739a3814acfSSam Leffler if (p->if_capabilities & IFCAP_VLAN_HWTAGGING) 7401cf236fbSYaroslav Tykhiy ifp->if_capabilities |= p->if_capabilities & IFCAP_HWCSUM; 741b08347a0SYaroslav Tykhiy #endif 742a3814acfSSam Leffler 743a3814acfSSam Leffler /* 7442cc2df49SGarrett Wollman * Set up our ``Ethernet address'' to reflect the underlying 7452cc2df49SGarrett Wollman * physical interface's. 7462cc2df49SGarrett Wollman */ 7471cf236fbSYaroslav Tykhiy ifa1 = ifaddr_byindex(ifp->if_index); 748f9132cebSJonathan Lemon ifa2 = ifaddr_byindex(p->if_index); 7492cc2df49SGarrett Wollman sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr; 7502cc2df49SGarrett Wollman sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr; 7512cc2df49SGarrett Wollman sdl1->sdl_type = IFT_ETHER; 7522cc2df49SGarrett Wollman sdl1->sdl_alen = ETHER_ADDR_LEN; 7532cc2df49SGarrett Wollman bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN); 7541cf236fbSYaroslav Tykhiy bcopy(LLADDR(sdl2), IFP2ENADDR(ifp), ETHER_ADDR_LEN); 7551b2a4f7aSBill Fenner 7561b2a4f7aSBill Fenner /* 7571b2a4f7aSBill Fenner * Configure multicast addresses that may already be 7581b2a4f7aSBill Fenner * joined on the vlan device. 7591b2a4f7aSBill Fenner */ 7601cf236fbSYaroslav Tykhiy (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */ 7611b2a4f7aSBill Fenner 76215a66c21SBruce M Simpson return (0); 7632cc2df49SGarrett Wollman } 7642cc2df49SGarrett Wollman 7652cc2df49SGarrett Wollman static int 766f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 767f731f104SBill Paul { 768f731f104SBill Paul struct ifaddr *ifa; 769f731f104SBill Paul struct sockaddr_dl *sdl; 770f731f104SBill Paul struct vlan_mc_entry *mc; 771f731f104SBill Paul struct ifvlan *ifv; 772f731f104SBill Paul struct ifnet *p; 773f731f104SBill Paul int error; 774f731f104SBill Paul 7754faedfe8SSam Leffler VLAN_LOCK_ASSERT(); 7764faedfe8SSam Leffler 777f731f104SBill Paul ifv = ifp->if_softc; 778f731f104SBill Paul p = ifv->ifv_p; 779f731f104SBill Paul 7801b2a4f7aSBill Fenner if (p) { 7811b2a4f7aSBill Fenner struct sockaddr_dl sdl; 7821b2a4f7aSBill Fenner 783f731f104SBill Paul /* 784f731f104SBill Paul * Since the interface is being unconfigured, we need to 785f731f104SBill Paul * empty the list of multicast groups that we may have joined 7861b2a4f7aSBill Fenner * while we were alive from the parent's list. 787f731f104SBill Paul */ 78815a66c21SBruce M Simpson bzero((char *)&sdl, sizeof(sdl)); 78915a66c21SBruce M Simpson sdl.sdl_len = sizeof(sdl); 790f731f104SBill Paul sdl.sdl_family = AF_LINK; 7911b2a4f7aSBill Fenner sdl.sdl_index = p->if_index; 7921b2a4f7aSBill Fenner sdl.sdl_type = IFT_ETHER; 7931b2a4f7aSBill Fenner sdl.sdl_alen = ETHER_ADDR_LEN; 7941b2a4f7aSBill Fenner 7951b2a4f7aSBill Fenner while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) { 79622f29826SPoul-Henning Kamp mc = SLIST_FIRST(&ifv->vlan_mc_listhead); 79715a66c21SBruce M Simpson bcopy((char *)&mc->mc_addr, LLADDR(&sdl), 79815a66c21SBruce M Simpson ETHER_ADDR_LEN); 799f731f104SBill Paul error = if_delmulti(p, (struct sockaddr *)&sdl); 800f731f104SBill Paul if (error) 801f731f104SBill Paul return (error); 802f731f104SBill Paul SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 8039d4fe4b2SBrooks Davis free(mc, M_VLAN); 804f731f104SBill Paul } 805a3814acfSSam Leffler 8061cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 0); /* clear special flags on parent */ 807a3814acfSSam Leffler p->if_nvlans--; 8081b2a4f7aSBill Fenner } 809f731f104SBill Paul 810f731f104SBill Paul /* Disconnect from parent. */ 8111cf236fbSYaroslav Tykhiy if (ifv->ifv_pflags) 8121cf236fbSYaroslav Tykhiy if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 813f731f104SBill Paul ifv->ifv_p = NULL; 814fc74a9f9SBrooks Davis ifv->ifv_ifp->if_mtu = ETHERMTU; /* XXX why not 0? */ 815fc74a9f9SBrooks Davis ifv->ifv_ifp->if_link_state = LINK_STATE_UNKNOWN; 816f731f104SBill Paul 817f731f104SBill Paul /* Clear our MAC address. */ 818fc74a9f9SBrooks Davis ifa = ifaddr_byindex(ifv->ifv_ifp->if_index); 819f731f104SBill Paul sdl = (struct sockaddr_dl *)ifa->ifa_addr; 820f731f104SBill Paul sdl->sdl_type = IFT_ETHER; 821f731f104SBill Paul sdl->sdl_alen = ETHER_ADDR_LEN; 822f731f104SBill Paul bzero(LLADDR(sdl), ETHER_ADDR_LEN); 823fc74a9f9SBrooks Davis bzero(IFP2ENADDR(ifv->ifv_ifp), ETHER_ADDR_LEN); 824f731f104SBill Paul 82515a66c21SBruce M Simpson return (0); 826f731f104SBill Paul } 827f731f104SBill Paul 8281cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */ 829f731f104SBill Paul static int 8301cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status, 8311cf236fbSYaroslav Tykhiy int (*func)(struct ifnet *, int)) 832a3814acfSSam Leffler { 8331cf236fbSYaroslav Tykhiy struct ifvlan *ifv; 8341cf236fbSYaroslav Tykhiy int error; 835a3814acfSSam Leffler 8361cf236fbSYaroslav Tykhiy /* XXX VLAN_LOCK_ASSERT(); */ 837a3814acfSSam Leffler 8381cf236fbSYaroslav Tykhiy ifv = ifp->if_softc; 8391cf236fbSYaroslav Tykhiy status = status ? (ifp->if_flags & flag) : 0; 8401cf236fbSYaroslav Tykhiy /* Now "status" contains the flag value or 0 */ 8411cf236fbSYaroslav Tykhiy 8421cf236fbSYaroslav Tykhiy /* 8431cf236fbSYaroslav Tykhiy * See if recorded parent's status is different from what 8441cf236fbSYaroslav Tykhiy * we want it to be. If it is, flip it. We record parent's 8451cf236fbSYaroslav Tykhiy * status in ifv_pflags so that we won't clear parent's flag 8461cf236fbSYaroslav Tykhiy * we haven't set. In fact, we don't clear or set parent's 8471cf236fbSYaroslav Tykhiy * flags directly, but get or release references to them. 8481cf236fbSYaroslav Tykhiy * That's why we can be sure that recorded flags still are 8491cf236fbSYaroslav Tykhiy * in accord with actual parent's flags. 8501cf236fbSYaroslav Tykhiy */ 8511cf236fbSYaroslav Tykhiy if (status != (ifv->ifv_pflags & flag)) { 8521cf236fbSYaroslav Tykhiy error = (*func)(ifv->ifv_p, status); 8531cf236fbSYaroslav Tykhiy if (error) 854a3814acfSSam Leffler return (error); 8551cf236fbSYaroslav Tykhiy ifv->ifv_pflags &= ~flag; 8561cf236fbSYaroslav Tykhiy ifv->ifv_pflags |= status; 8571cf236fbSYaroslav Tykhiy } 8581cf236fbSYaroslav Tykhiy return (0); 8591cf236fbSYaroslav Tykhiy } 8601cf236fbSYaroslav Tykhiy 8611cf236fbSYaroslav Tykhiy /* 8621cf236fbSYaroslav Tykhiy * Handle IFF_* flags that require certain changes on the parent: 8631cf236fbSYaroslav Tykhiy * if "status" is true, update parent's flags respective to our if_flags; 8641cf236fbSYaroslav Tykhiy * if "status" is false, forcedly clear the flags set on parent. 8651cf236fbSYaroslav Tykhiy */ 8661cf236fbSYaroslav Tykhiy static int 8671cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status) 8681cf236fbSYaroslav Tykhiy { 8691cf236fbSYaroslav Tykhiy int error, i; 8701cf236fbSYaroslav Tykhiy 8711cf236fbSYaroslav Tykhiy for (i = 0; vlan_pflags[i].flag; i++) { 8721cf236fbSYaroslav Tykhiy error = vlan_setflag(ifp, vlan_pflags[i].flag, 8731cf236fbSYaroslav Tykhiy status, vlan_pflags[i].func); 8741cf236fbSYaroslav Tykhiy if (error) 8751cf236fbSYaroslav Tykhiy return (error); 8761cf236fbSYaroslav Tykhiy } 8771cf236fbSYaroslav Tykhiy return (0); 878a3814acfSSam Leffler } 879a3814acfSSam Leffler 880127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */ 881127d7b2dSAndre Oppermann static void 882127d7b2dSAndre Oppermann vlan_link_state(struct ifnet *ifp, int link) 883127d7b2dSAndre Oppermann { 884127d7b2dSAndre Oppermann struct ifvlan *ifv; 885127d7b2dSAndre Oppermann 886127d7b2dSAndre Oppermann VLAN_LOCK(); 887127d7b2dSAndre Oppermann LIST_FOREACH(ifv, &ifv_list, ifv_list) { 888984be3efSGleb Smirnoff if (ifv->ifv_p == ifp) 889fc74a9f9SBrooks Davis if_link_state_change(ifv->ifv_ifp, 890984be3efSGleb Smirnoff ifv->ifv_p->if_link_state); 891127d7b2dSAndre Oppermann } 892127d7b2dSAndre Oppermann VLAN_UNLOCK(); 893127d7b2dSAndre Oppermann } 894127d7b2dSAndre Oppermann 895a3814acfSSam Leffler static int 896cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 8972cc2df49SGarrett Wollman { 8982cc2df49SGarrett Wollman struct ifaddr *ifa; 8992cc2df49SGarrett Wollman struct ifnet *p; 9002cc2df49SGarrett Wollman struct ifreq *ifr; 9012cc2df49SGarrett Wollman struct ifvlan *ifv; 9022cc2df49SGarrett Wollman struct vlanreq vlr; 9032cc2df49SGarrett Wollman int error = 0; 9042cc2df49SGarrett Wollman 9052cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 9062cc2df49SGarrett Wollman ifa = (struct ifaddr *)data; 9072cc2df49SGarrett Wollman ifv = ifp->if_softc; 9082cc2df49SGarrett Wollman 9092cc2df49SGarrett Wollman switch (cmd) { 9102cc2df49SGarrett Wollman case SIOCSIFADDR: 9112cc2df49SGarrett Wollman ifp->if_flags |= IFF_UP; 9122cc2df49SGarrett Wollman 9132cc2df49SGarrett Wollman switch (ifa->ifa_addr->sa_family) { 9142cc2df49SGarrett Wollman #ifdef INET 9152cc2df49SGarrett Wollman case AF_INET: 916fc74a9f9SBrooks Davis arp_ifinit(ifv->ifv_ifp, ifa); 9172cc2df49SGarrett Wollman break; 9182cc2df49SGarrett Wollman #endif 9192cc2df49SGarrett Wollman default: 9202cc2df49SGarrett Wollman break; 9212cc2df49SGarrett Wollman } 9222cc2df49SGarrett Wollman break; 9232cc2df49SGarrett Wollman 9242cc2df49SGarrett Wollman case SIOCGIFADDR: 9252cc2df49SGarrett Wollman { 9262cc2df49SGarrett Wollman struct sockaddr *sa; 9272cc2df49SGarrett Wollman 9282cc2df49SGarrett Wollman sa = (struct sockaddr *) &ifr->ifr_data; 929fc74a9f9SBrooks Davis bcopy(IFP2ENADDR(ifp), (caddr_t)sa->sa_data, 93015a66c21SBruce M Simpson ETHER_ADDR_LEN); 9312cc2df49SGarrett Wollman } 9322cc2df49SGarrett Wollman break; 9332cc2df49SGarrett Wollman 934b3cca108SBill Fenner case SIOCGIFMEDIA: 9354faedfe8SSam Leffler VLAN_LOCK(); 936b3cca108SBill Fenner if (ifv->ifv_p != NULL) { 9374faedfe8SSam Leffler error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, 9384faedfe8SSam Leffler SIOCGIFMEDIA, data); 9394faedfe8SSam Leffler VLAN_UNLOCK(); 940b3cca108SBill Fenner /* Limit the result to the parent's current config. */ 941b3cca108SBill Fenner if (error == 0) { 942b3cca108SBill Fenner struct ifmediareq *ifmr; 943b3cca108SBill Fenner 944b3cca108SBill Fenner ifmr = (struct ifmediareq *)data; 945b3cca108SBill Fenner if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 946b3cca108SBill Fenner ifmr->ifm_count = 1; 947b3cca108SBill Fenner error = copyout(&ifmr->ifm_current, 948b3cca108SBill Fenner ifmr->ifm_ulist, 949b3cca108SBill Fenner sizeof(int)); 950b3cca108SBill Fenner } 951b3cca108SBill Fenner } 9524faedfe8SSam Leffler } else { 9534faedfe8SSam Leffler VLAN_UNLOCK(); 954b3cca108SBill Fenner error = EINVAL; 9554faedfe8SSam Leffler } 956b3cca108SBill Fenner break; 957b3cca108SBill Fenner 958b3cca108SBill Fenner case SIOCSIFMEDIA: 959b3cca108SBill Fenner error = EINVAL; 960b3cca108SBill Fenner break; 961b3cca108SBill Fenner 9622cc2df49SGarrett Wollman case SIOCSIFMTU: 9632cc2df49SGarrett Wollman /* 9642cc2df49SGarrett Wollman * Set the interface MTU. 9652cc2df49SGarrett Wollman */ 9664faedfe8SSam Leffler VLAN_LOCK(); 967a3814acfSSam Leffler if (ifv->ifv_p != NULL) { 968a3814acfSSam Leffler if (ifr->ifr_mtu > 969a3814acfSSam Leffler (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) || 970a3814acfSSam Leffler ifr->ifr_mtu < 971a3814acfSSam Leffler (ifv->ifv_mintu - ifv->ifv_mtufudge)) 9722cc2df49SGarrett Wollman error = EINVAL; 973a3814acfSSam Leffler else 9742cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 975a3814acfSSam Leffler } else 976a3814acfSSam Leffler error = EINVAL; 9774faedfe8SSam Leffler VLAN_UNLOCK(); 9782cc2df49SGarrett Wollman break; 9792cc2df49SGarrett Wollman 9802cc2df49SGarrett Wollman case SIOCSETVLAN: 98115a66c21SBruce M Simpson error = copyin(ifr->ifr_data, &vlr, sizeof(vlr)); 9822cc2df49SGarrett Wollman if (error) 9832cc2df49SGarrett Wollman break; 9842cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 9854faedfe8SSam Leffler VLAN_LOCK(); 986f731f104SBill Paul vlan_unconfig(ifp); 9874faedfe8SSam Leffler if (ifp->if_flags & IFF_UP) 9882cc2df49SGarrett Wollman if_down(ifp); 98913f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 9904faedfe8SSam Leffler VLAN_UNLOCK(); 9912cc2df49SGarrett Wollman break; 9922cc2df49SGarrett Wollman } 9932cc2df49SGarrett Wollman p = ifunit(vlr.vlr_parent); 9942cc2df49SGarrett Wollman if (p == 0) { 9952cc2df49SGarrett Wollman error = ENOENT; 9962cc2df49SGarrett Wollman break; 9972cc2df49SGarrett Wollman } 998fb88a3e0SBill Paul /* 999fb88a3e0SBill Paul * Don't let the caller set up a VLAN tag with 1000fb88a3e0SBill Paul * anything except VLID bits. 1001fb88a3e0SBill Paul */ 1002fb88a3e0SBill Paul if (vlr.vlr_tag & ~EVL_VLID_MASK) { 1003fb88a3e0SBill Paul error = EINVAL; 1004fb88a3e0SBill Paul break; 1005fb88a3e0SBill Paul } 10064faedfe8SSam Leffler VLAN_LOCK(); 10072cc2df49SGarrett Wollman error = vlan_config(ifv, p); 10084faedfe8SSam Leffler if (error) { 10094faedfe8SSam Leffler VLAN_UNLOCK(); 10102cc2df49SGarrett Wollman break; 10114faedfe8SSam Leffler } 10122cc2df49SGarrett Wollman ifv->ifv_tag = vlr.vlr_tag; 101313f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 10144faedfe8SSam Leffler VLAN_UNLOCK(); 1015a3814acfSSam Leffler 10161cf236fbSYaroslav Tykhiy /* Update flags on the parent, if necessary. */ 10171cf236fbSYaroslav Tykhiy vlan_setflags(ifp, 1); 10182cc2df49SGarrett Wollman break; 10192cc2df49SGarrett Wollman 10202cc2df49SGarrett Wollman case SIOCGETVLAN: 102115a66c21SBruce M Simpson bzero(&vlr, sizeof(vlr)); 10224faedfe8SSam Leffler VLAN_LOCK(); 10232cc2df49SGarrett Wollman if (ifv->ifv_p) { 10249bf40edeSBrooks Davis strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname, 10259bf40edeSBrooks Davis sizeof(vlr.vlr_parent)); 10262cc2df49SGarrett Wollman vlr.vlr_tag = ifv->ifv_tag; 10272cc2df49SGarrett Wollman } 10284faedfe8SSam Leffler VLAN_UNLOCK(); 102915a66c21SBruce M Simpson error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 10302cc2df49SGarrett Wollman break; 10312cc2df49SGarrett Wollman 10322cc2df49SGarrett Wollman case SIOCSIFFLAGS: 10332cc2df49SGarrett Wollman /* 10341cf236fbSYaroslav Tykhiy * We should propagate selected flags to the parent, 10351cf236fbSYaroslav Tykhiy * e.g., promiscuous mode. 10362cc2df49SGarrett Wollman */ 1037a3814acfSSam Leffler if (ifv->ifv_p != NULL) 10381cf236fbSYaroslav Tykhiy error = vlan_setflags(ifp, 1); 10392cc2df49SGarrett Wollman break; 1040a3814acfSSam Leffler 1041f731f104SBill Paul case SIOCADDMULTI: 1042f731f104SBill Paul case SIOCDELMULTI: 104329c2dfbeSBruce M Simpson /*VLAN_LOCK();*/ 1044f731f104SBill Paul error = vlan_setmulti(ifp); 104529c2dfbeSBruce M Simpson /*VLAN_UNLOCK();*/ 1046f731f104SBill Paul break; 10472cc2df49SGarrett Wollman default: 10482cc2df49SGarrett Wollman error = EINVAL; 10492cc2df49SGarrett Wollman } 105015a66c21SBruce M Simpson 105115a66c21SBruce M Simpson return (error); 10522cc2df49SGarrett Wollman } 1053