12cc2df49SGarrett Wollman /* 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. 42f731f104SBill Paul * 4397ed1257SBill Paul * 44f731f104SBill Paul * XXX It's incorrect to assume that we must always kludge up 45f731f104SBill Paul * headers on the physical device's behalf: some devices support 46befdaf4eSJeroen Ruigrok van der Werven * VLAN tag insertion and extraction in firmware. For these cases, 47f731f104SBill Paul * one can change the behavior of the vlan interface by setting 48f731f104SBill Paul * the LINK0 flag on it (that is setting the vlan interface's LINK0 49f731f104SBill Paul * flag, _not_ the parent's LINK0 flag; we try to leave the parent 502d89d40aSJeroen Ruigrok van der Werven * alone). If the interface has the LINK0 flag set, then it will 512d89d40aSJeroen Ruigrok van der Werven * not modify the ethernet header on output, because the parent 52f731f104SBill Paul * can do that for itself. On input, the parent can call vlan_input_tag() 53f731f104SBill Paul * directly in order to supply us with an incoming mbuf and the vlan 54f731f104SBill Paul * tag value that goes with it. 552cc2df49SGarrett Wollman */ 562cc2df49SGarrett Wollman 572cc2df49SGarrett Wollman #include "opt_inet.h" 582cc2df49SGarrett Wollman 592cc2df49SGarrett Wollman #include <sys/param.h> 602cc2df49SGarrett Wollman #include <sys/kernel.h> 61f731f104SBill Paul #include <sys/malloc.h> 622cc2df49SGarrett Wollman #include <sys/mbuf.h> 632b120974SPeter Wemm #include <sys/module.h> 64f731f104SBill Paul #include <sys/queue.h> 652cc2df49SGarrett Wollman #include <sys/socket.h> 662cc2df49SGarrett Wollman #include <sys/sockio.h> 672cc2df49SGarrett Wollman #include <sys/sysctl.h> 682cc2df49SGarrett Wollman #include <sys/systm.h> 699d4fe4b2SBrooks Davis #include <machine/bus.h> /* XXX: Shouldn't really be required! */ 709d4fe4b2SBrooks Davis #include <sys/rman.h> 712cc2df49SGarrett Wollman 722cc2df49SGarrett Wollman #include <net/bpf.h> 732cc2df49SGarrett Wollman #include <net/ethernet.h> 742cc2df49SGarrett Wollman #include <net/if.h> 752cc2df49SGarrett Wollman #include <net/if_arp.h> 762cc2df49SGarrett Wollman #include <net/if_dl.h> 772cc2df49SGarrett Wollman #include <net/if_types.h> 782cc2df49SGarrett Wollman #include <net/if_vlan_var.h> 792cc2df49SGarrett Wollman 802cc2df49SGarrett Wollman #ifdef INET 812cc2df49SGarrett Wollman #include <netinet/in.h> 822cc2df49SGarrett Wollman #include <netinet/if_ether.h> 832cc2df49SGarrett Wollman #endif 842cc2df49SGarrett Wollman 859d4fe4b2SBrooks Davis #define VLANNAME "vlan" 869d4fe4b2SBrooks Davis #define VLAN_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */ 879d4fe4b2SBrooks Davis 884a408dcbSBill Paul SYSCTL_DECL(_net_link); 89d2a75853SBill Fenner SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 902cc2df49SGarrett Wollman SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 912cc2df49SGarrett Wollman 929d4fe4b2SBrooks Davis static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface"); 939d4fe4b2SBrooks Davis static struct rman vlanunits[1]; 949d4fe4b2SBrooks Davis static LIST_HEAD(, ifvlan) ifv_list; 952cc2df49SGarrett Wollman 969d4fe4b2SBrooks Davis static int vlan_clone_create(struct if_clone *, int *); 979d4fe4b2SBrooks Davis static void vlan_clone_destroy(struct ifnet *); 982cc2df49SGarrett Wollman static void vlan_start(struct ifnet *ifp); 992cc2df49SGarrett Wollman static void vlan_ifinit(void *foo); 1009d4fe4b2SBrooks Davis static int vlan_input(struct ether_header *eh, struct mbuf *m); 1019d4fe4b2SBrooks Davis static int vlan_input_tag(struct ether_header *eh, struct mbuf *m, 1029d4fe4b2SBrooks Davis u_int16_t t); 103cfe8b629SGarrett Wollman static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 104f731f104SBill Paul static int vlan_setmulti(struct ifnet *ifp); 105f731f104SBill Paul static int vlan_unconfig(struct ifnet *ifp); 106f731f104SBill Paul static int vlan_config(struct ifvlan *ifv, struct ifnet *p); 107f731f104SBill Paul 1089d4fe4b2SBrooks Davis struct if_clone vlan_cloner = 1099d4fe4b2SBrooks Davis IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy); 1109d4fe4b2SBrooks Davis 111f731f104SBill Paul /* 112f731f104SBill Paul * Program our multicast filter. What we're actually doing is 113f731f104SBill Paul * programming the multicast filter of the parent. This has the 114f731f104SBill Paul * side effect of causing the parent interface to receive multicast 115f731f104SBill Paul * traffic that it doesn't really want, which ends up being discarded 116f731f104SBill Paul * later by the upper protocol layers. Unfortunately, there's no way 117f731f104SBill Paul * to avoid this: there really is only one physical interface. 118f731f104SBill Paul */ 1192b120974SPeter Wemm static int 1202b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp) 121f731f104SBill Paul { 122f731f104SBill Paul struct ifnet *ifp_p; 123f731f104SBill Paul struct ifmultiaddr *ifma, *rifma = NULL; 124f731f104SBill Paul struct ifvlan *sc; 125f731f104SBill Paul struct vlan_mc_entry *mc = NULL; 126f731f104SBill Paul struct sockaddr_dl sdl; 127f731f104SBill Paul int error; 128f731f104SBill Paul 129f731f104SBill Paul /* Find the parent. */ 130f731f104SBill Paul sc = ifp->if_softc; 131f731f104SBill Paul ifp_p = sc->ifv_p; 132f731f104SBill Paul 1331b2a4f7aSBill Fenner /* 1341b2a4f7aSBill Fenner * If we don't have a parent, just remember the membership for 1351b2a4f7aSBill Fenner * when we do. 1361b2a4f7aSBill Fenner */ 1371b2a4f7aSBill Fenner if (ifp_p == NULL) 1381b2a4f7aSBill Fenner return(0); 1391b2a4f7aSBill Fenner 14024993214SYaroslav Tykhiy bzero((char *)&sdl, sizeof sdl); 14124993214SYaroslav Tykhiy sdl.sdl_len = sizeof sdl; 142f731f104SBill Paul sdl.sdl_family = AF_LINK; 14326e30963SBill Fenner sdl.sdl_index = ifp_p->if_index; 14426e30963SBill Fenner sdl.sdl_type = IFT_ETHER; 14524993214SYaroslav Tykhiy sdl.sdl_alen = ETHER_ADDR_LEN; 146f731f104SBill Paul 147f731f104SBill Paul /* First, remove any existing filter entries. */ 14822f29826SPoul-Henning Kamp while(SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) { 14922f29826SPoul-Henning Kamp mc = SLIST_FIRST(&sc->vlan_mc_listhead); 150f731f104SBill Paul bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 151f731f104SBill Paul error = if_delmulti(ifp_p, (struct sockaddr *)&sdl); 152f731f104SBill Paul if (error) 153f731f104SBill Paul return(error); 154f731f104SBill Paul SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 1559d4fe4b2SBrooks Davis free(mc, M_VLAN); 156f731f104SBill Paul } 157f731f104SBill Paul 158f731f104SBill Paul /* Now program new ones. */ 1596817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 160f731f104SBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 161f731f104SBill Paul continue; 1629d4fe4b2SBrooks Davis mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK); 163f731f104SBill Paul bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 164f731f104SBill Paul (char *)&mc->mc_addr, ETHER_ADDR_LEN); 165f731f104SBill Paul SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 16626e30963SBill Fenner bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 16726e30963SBill Fenner LLADDR(&sdl), ETHER_ADDR_LEN); 168f731f104SBill Paul error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 169f731f104SBill Paul if (error) 170f731f104SBill Paul return(error); 171f731f104SBill Paul } 172f731f104SBill Paul 173f731f104SBill Paul return(0); 174f731f104SBill Paul } 1752cc2df49SGarrett Wollman 1762b120974SPeter Wemm static int 1772b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data) 1782b120974SPeter Wemm { 1799d4fe4b2SBrooks Davis int err; 1809d4fe4b2SBrooks Davis 1812b120974SPeter Wemm switch (type) { 1822b120974SPeter Wemm case MOD_LOAD: 1839d4fe4b2SBrooks Davis vlanunits->rm_type = RMAN_ARRAY; 1849d4fe4b2SBrooks Davis vlanunits->rm_descr = "configurable if_vlan units"; 1859d4fe4b2SBrooks Davis err = rman_init(vlanunits); 1869d4fe4b2SBrooks Davis if (err != 0) 1879d4fe4b2SBrooks Davis return (err); 1889d4fe4b2SBrooks Davis err = rman_manage_region(vlanunits, 0, VLAN_MAXUNIT); 1899d4fe4b2SBrooks Davis if (err != 0) { 1909d4fe4b2SBrooks Davis printf("%s: vlanunits: rman_manage_region: Failed %d\n", 1919d4fe4b2SBrooks Davis VLANNAME, err); 1929d4fe4b2SBrooks Davis rman_fini(vlanunits); 1939d4fe4b2SBrooks Davis return (err); 1949d4fe4b2SBrooks Davis } 1959d4fe4b2SBrooks Davis LIST_INIT(&ifv_list); 1969d4fe4b2SBrooks Davis vlan_input_p = vlan_input; 1979d4fe4b2SBrooks Davis vlan_input_tag_p = vlan_input_tag; 1989d4fe4b2SBrooks Davis if_clone_attach(&vlan_cloner); 1992b120974SPeter Wemm break; 2002b120974SPeter Wemm case MOD_UNLOAD: 2019d4fe4b2SBrooks Davis if_clone_detach(&vlan_cloner); 2029d4fe4b2SBrooks Davis vlan_input_p = NULL; 2039d4fe4b2SBrooks Davis vlan_input_tag_p = NULL; 2049d4fe4b2SBrooks Davis while (!LIST_EMPTY(&ifv_list)) 2059d4fe4b2SBrooks Davis vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if); 2069d4fe4b2SBrooks Davis err = rman_fini(vlanunits); 2079d4fe4b2SBrooks Davis if (err != 0) 2089d4fe4b2SBrooks Davis return (err); 2099d4fe4b2SBrooks Davis break; 2102b120974SPeter Wemm } 2112b120974SPeter Wemm return 0; 2122b120974SPeter Wemm } 2132b120974SPeter Wemm 2142b120974SPeter Wemm static moduledata_t vlan_mod = { 2152b120974SPeter Wemm "if_vlan", 2162b120974SPeter Wemm vlan_modevent, 2172b120974SPeter Wemm 0 2182b120974SPeter Wemm }; 2192b120974SPeter Wemm 2202b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 2212cc2df49SGarrett Wollman 2229d4fe4b2SBrooks Davis static int 2239d4fe4b2SBrooks Davis vlan_clone_create(struct if_clone *ifc, int *unit) 2249d4fe4b2SBrooks Davis { 2259d4fe4b2SBrooks Davis struct resource *r; 2269d4fe4b2SBrooks Davis struct ifvlan *ifv; 2279d4fe4b2SBrooks Davis struct ifnet *ifp; 2289d4fe4b2SBrooks Davis int s; 2299d4fe4b2SBrooks Davis 2309d4fe4b2SBrooks Davis if (*unit > VLAN_MAXUNIT) 2319d4fe4b2SBrooks Davis return (ENXIO); 2329d4fe4b2SBrooks Davis 2339d4fe4b2SBrooks Davis if (*unit < 0) { 2349d4fe4b2SBrooks Davis r = rman_reserve_resource(vlanunits, 0, VLAN_MAXUNIT, 1, 2359d4fe4b2SBrooks Davis RF_ALLOCATED | RF_ACTIVE, NULL); 2369d4fe4b2SBrooks Davis if (r == NULL) 2379d4fe4b2SBrooks Davis return (ENOSPC); 2389d4fe4b2SBrooks Davis *unit = rman_get_start(r); 2399d4fe4b2SBrooks Davis } else { 2409d4fe4b2SBrooks Davis r = rman_reserve_resource(vlanunits, *unit, *unit, 1, 2419d4fe4b2SBrooks Davis RF_ALLOCATED | RF_ACTIVE, NULL); 2429d4fe4b2SBrooks Davis if (r == NULL) 2439d4fe4b2SBrooks Davis return (EEXIST); 2449d4fe4b2SBrooks Davis } 2459d4fe4b2SBrooks Davis 2469d4fe4b2SBrooks Davis ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK); 2479d4fe4b2SBrooks Davis memset(ifv, 0, sizeof(struct ifvlan)); 2489d4fe4b2SBrooks Davis ifp = &ifv->ifv_if; 2499d4fe4b2SBrooks Davis SLIST_INIT(&ifv->vlan_mc_listhead); 2509d4fe4b2SBrooks Davis 2519d4fe4b2SBrooks Davis s = splnet(); 2529d4fe4b2SBrooks Davis LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list); 2539d4fe4b2SBrooks Davis splx(s); 2549d4fe4b2SBrooks Davis 2559d4fe4b2SBrooks Davis ifp->if_softc = ifv; 2569d4fe4b2SBrooks Davis ifp->if_name = "vlan"; 2579d4fe4b2SBrooks Davis ifp->if_unit = *unit; 2589d4fe4b2SBrooks Davis ifv->r_unit = r; 2599d4fe4b2SBrooks Davis /* NB: flags are not set here */ 2609d4fe4b2SBrooks Davis ifp->if_linkmib = &ifv->ifv_mib; 2619d4fe4b2SBrooks Davis ifp->if_linkmiblen = sizeof ifv->ifv_mib; 2629d4fe4b2SBrooks Davis /* NB: mtu is not set here */ 2639d4fe4b2SBrooks Davis 2649d4fe4b2SBrooks Davis ifp->if_init = vlan_ifinit; 2659d4fe4b2SBrooks Davis ifp->if_start = vlan_start; 2669d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 2679d4fe4b2SBrooks Davis ifp->if_output = ether_output; 2689d4fe4b2SBrooks Davis ifp->if_snd.ifq_maxlen = ifqmaxlen; 2699d4fe4b2SBrooks Davis ether_ifattach(ifp, ETHER_BPF_SUPPORTED); 2709d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 271211f625aSBill Fenner ifp->if_baudrate = 0; 2729d4fe4b2SBrooks Davis ifp->if_data.ifi_type = IFT_L2VLAN; 2739d4fe4b2SBrooks Davis ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN; 2749d4fe4b2SBrooks Davis 2759d4fe4b2SBrooks Davis return (0); 2769d4fe4b2SBrooks Davis } 2779d4fe4b2SBrooks Davis 2789d4fe4b2SBrooks Davis static void 2799d4fe4b2SBrooks Davis vlan_clone_destroy(struct ifnet *ifp) 2809d4fe4b2SBrooks Davis { 2819d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 2829d4fe4b2SBrooks Davis int s; 2839d4fe4b2SBrooks Davis int err; 2849d4fe4b2SBrooks Davis 2859d4fe4b2SBrooks Davis s = splnet(); 2869d4fe4b2SBrooks Davis LIST_REMOVE(ifv, ifv_list); 2879d4fe4b2SBrooks Davis vlan_unconfig(ifp); 2889d4fe4b2SBrooks Davis splx(s); 2899d4fe4b2SBrooks Davis 2909d4fe4b2SBrooks Davis ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); 2919d4fe4b2SBrooks Davis 2929d4fe4b2SBrooks Davis err = rman_release_resource(ifv->r_unit); 2939d4fe4b2SBrooks Davis KASSERT(err == 0, ("Unexpected error freeing resource")); 2949d4fe4b2SBrooks Davis free(ifv, M_VLAN); 2959d4fe4b2SBrooks Davis } 2969d4fe4b2SBrooks Davis 2972cc2df49SGarrett Wollman static void 2982cc2df49SGarrett Wollman vlan_ifinit(void *foo) 2992cc2df49SGarrett Wollman { 300f731f104SBill Paul return; 3012cc2df49SGarrett Wollman } 3022cc2df49SGarrett Wollman 3032cc2df49SGarrett Wollman static void 3042cc2df49SGarrett Wollman vlan_start(struct ifnet *ifp) 3052cc2df49SGarrett Wollman { 3062cc2df49SGarrett Wollman struct ifvlan *ifv; 3072cc2df49SGarrett Wollman struct ifnet *p; 3082cc2df49SGarrett Wollman struct ether_vlan_header *evl; 3095326b23cSMatthew N. Dodd struct mbuf *m; 3102cc2df49SGarrett Wollman 3112cc2df49SGarrett Wollman ifv = ifp->if_softc; 3122cc2df49SGarrett Wollman p = ifv->ifv_p; 3132cc2df49SGarrett Wollman 3142cc2df49SGarrett Wollman ifp->if_flags |= IFF_OACTIVE; 3152cc2df49SGarrett Wollman for (;;) { 3162cc2df49SGarrett Wollman IF_DEQUEUE(&ifp->if_snd, m); 3172cc2df49SGarrett Wollman if (m == 0) 3182cc2df49SGarrett Wollman break; 3192cc2df49SGarrett Wollman if (ifp->if_bpf) 320eb92a347SGarrett Wollman bpf_mtap(ifp, m); 3212cc2df49SGarrett Wollman 322f731f104SBill Paul /* 32324993214SYaroslav Tykhiy * Do not run parent's if_start() if the parent is not up, 32424993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 32524993214SYaroslav Tykhiy */ 32624993214SYaroslav Tykhiy if ((p->if_flags & (IFF_UP | IFF_RUNNING)) != 32724993214SYaroslav Tykhiy (IFF_UP | IFF_RUNNING)) { 32824993214SYaroslav Tykhiy m_freem(m); 32924993214SYaroslav Tykhiy ifp->if_data.ifi_collisions++; 33024993214SYaroslav Tykhiy continue; 33124993214SYaroslav Tykhiy } 33224993214SYaroslav Tykhiy 33324993214SYaroslav Tykhiy /* 334f731f104SBill Paul * If the LINK0 flag is set, it means the underlying interface 335f731f104SBill Paul * can do VLAN tag insertion itself and doesn't require us to 336f731f104SBill Paul * create a special header for it. In this case, we just pass 337f731f104SBill Paul * the packet along. However, we need some way to tell the 338f731f104SBill Paul * interface where the packet came from so that it knows how 339f731f104SBill Paul * to find the VLAN tag to use, so we set the rcvif in the 340f731f104SBill Paul * mbuf header to our ifnet. 341f731f104SBill Paul * 342f731f104SBill Paul * Note: we also set the M_PROTO1 flag in the mbuf to let 343f731f104SBill Paul * the parent driver know that the rcvif pointer is really 344f731f104SBill Paul * valid. We need to do this because sometimes mbufs will 345f731f104SBill Paul * be allocated by other parts of the system that contain 346f731f104SBill Paul * garbage in the rcvif pointer. Using the M_PROTO1 flag 347f731f104SBill Paul * lets the driver perform a proper sanity check and avoid 348f731f104SBill Paul * following potentially bogus rcvif pointers off into 349f731f104SBill Paul * never-never land. 350f731f104SBill Paul */ 351f731f104SBill Paul if (ifp->if_flags & IFF_LINK0) { 352f731f104SBill Paul m->m_pkthdr.rcvif = ifp; 353f731f104SBill Paul m->m_flags |= M_PROTO1; 354f731f104SBill Paul } else { 3552cc2df49SGarrett Wollman M_PREPEND(m, EVL_ENCAPLEN, M_DONTWAIT); 3564af90a4dSMatthew N. Dodd if (m == NULL) { 3574af90a4dSMatthew N. Dodd printf("vlan%d: M_PREPEND failed", ifp->if_unit); 3584af90a4dSMatthew N. Dodd ifp->if_ierrors++; 3592cc2df49SGarrett Wollman continue; 3604af90a4dSMatthew N. Dodd } 3612cc2df49SGarrett Wollman /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 3622cc2df49SGarrett Wollman 3635326b23cSMatthew N. Dodd m = m_pullup(m, ETHER_HDR_LEN + EVL_ENCAPLEN); 3645326b23cSMatthew N. Dodd if (m == NULL) { 3654af90a4dSMatthew N. Dodd printf("vlan%d: m_pullup failed", ifp->if_unit); 3664af90a4dSMatthew N. Dodd ifp->if_ierrors++; 3674af90a4dSMatthew N. Dodd continue; 3684af90a4dSMatthew N. Dodd } 3694af90a4dSMatthew N. Dodd 3702cc2df49SGarrett Wollman /* 3712cc2df49SGarrett Wollman * Transform the Ethernet header into an Ethernet header 3722cc2df49SGarrett Wollman * with 802.1Q encapsulation. 3732cc2df49SGarrett Wollman */ 3742cc2df49SGarrett Wollman bcopy(mtod(m, char *) + EVL_ENCAPLEN, mtod(m, char *), 3752cc2df49SGarrett Wollman sizeof(struct ether_header)); 3762cc2df49SGarrett Wollman evl = mtod(m, struct ether_vlan_header *); 3772cc2df49SGarrett Wollman evl->evl_proto = evl->evl_encap_proto; 3789d4fe4b2SBrooks Davis evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 3792cc2df49SGarrett Wollman evl->evl_tag = htons(ifv->ifv_tag); 380f731f104SBill Paul #ifdef DEBUG 381f731f104SBill Paul printf("vlan_start: %*D\n", sizeof *evl, 38265f1bb65SPeter Wemm (unsigned char *)evl, ":"); 383f731f104SBill Paul #endif 384f731f104SBill Paul } 3852cc2df49SGarrett Wollman 3862cc2df49SGarrett Wollman /* 3872cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 3882cc2df49SGarrett Wollman * We are already running at splimp. 3892cc2df49SGarrett Wollman */ 390df5e1987SJonathan Lemon if (IF_HANDOFF(&p->if_snd, m, p)) 391f731f104SBill Paul ifp->if_opackets++; 392df5e1987SJonathan Lemon else 393df5e1987SJonathan Lemon ifp->if_oerrors++; 3942cc2df49SGarrett Wollman } 3952cc2df49SGarrett Wollman ifp->if_flags &= ~IFF_OACTIVE; 396f731f104SBill Paul 397f731f104SBill Paul return; 398f731f104SBill Paul } 399f731f104SBill Paul 4009d4fe4b2SBrooks Davis static int 401f731f104SBill Paul vlan_input_tag(struct ether_header *eh, struct mbuf *m, u_int16_t t) 402f731f104SBill Paul { 403f731f104SBill Paul struct ifvlan *ifv; 404f731f104SBill Paul 4059d4fe4b2SBrooks Davis for (ifv = LIST_FIRST(&ifv_list); ifv != NULL; 4069d4fe4b2SBrooks Davis ifv = LIST_NEXT(ifv, ifv_list)) { 407242c766bSBill Fenner if (m->m_pkthdr.rcvif == ifv->ifv_p 408242c766bSBill Fenner && ifv->ifv_tag == t) 409f731f104SBill Paul break; 410f731f104SBill Paul } 411f731f104SBill Paul 412242c766bSBill Fenner if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 413c0230c1bSJordan K. Hubbard m_free(m); 414c0230c1bSJordan K. Hubbard return -1; /* So the parent can take note */ 415f731f104SBill Paul } 416f731f104SBill Paul 417f731f104SBill Paul /* 418f731f104SBill Paul * Having found a valid vlan interface corresponding to 419f731f104SBill Paul * the given source interface and vlan tag, run the 420242c766bSBill Fenner * the real packet through ether_input(). 421f731f104SBill Paul */ 422f731f104SBill Paul m->m_pkthdr.rcvif = &ifv->ifv_if; 423f731f104SBill Paul 424f731f104SBill Paul ifv->ifv_if.if_ipackets++; 425f731f104SBill Paul ether_input(&ifv->ifv_if, eh, m); 426c0230c1bSJordan K. Hubbard return 0; 4272cc2df49SGarrett Wollman } 4282cc2df49SGarrett Wollman 4299d4fe4b2SBrooks Davis static int 4302cc2df49SGarrett Wollman vlan_input(struct ether_header *eh, struct mbuf *m) 4312cc2df49SGarrett Wollman { 4322cc2df49SGarrett Wollman struct ifvlan *ifv; 4332cc2df49SGarrett Wollman 4349d4fe4b2SBrooks Davis for (ifv = LIST_FIRST(&ifv_list); ifv != NULL; 4359d4fe4b2SBrooks Davis ifv = LIST_NEXT(ifv, ifv_list)) { 4362cc2df49SGarrett Wollman if (m->m_pkthdr.rcvif == ifv->ifv_p 4372cc2df49SGarrett Wollman && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *))) 4382cc2df49SGarrett Wollman == ifv->ifv_tag)) 4392cc2df49SGarrett Wollman break; 4402cc2df49SGarrett Wollman } 4412cc2df49SGarrett Wollman 442242c766bSBill Fenner if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 4432cc2df49SGarrett Wollman m_freem(m); 4442cc2df49SGarrett Wollman return -1; /* so ether_input can take note */ 4452cc2df49SGarrett Wollman } 4462cc2df49SGarrett Wollman 4472cc2df49SGarrett Wollman /* 4482cc2df49SGarrett Wollman * Having found a valid vlan interface corresponding to 4492cc2df49SGarrett Wollman * the given source interface and vlan tag, remove the 4502cc2df49SGarrett Wollman * encapsulation, and run the real packet through 4512cc2df49SGarrett Wollman * ether_input() a second time (it had better be 4522cc2df49SGarrett Wollman * reentrant!). 4532cc2df49SGarrett Wollman */ 4542cc2df49SGarrett Wollman m->m_pkthdr.rcvif = &ifv->ifv_if; 4552cc2df49SGarrett Wollman eh->ether_type = mtod(m, u_int16_t *)[1]; 4562cc2df49SGarrett Wollman m->m_data += EVL_ENCAPLEN; 4572cc2df49SGarrett Wollman m->m_len -= EVL_ENCAPLEN; 4582cc2df49SGarrett Wollman m->m_pkthdr.len -= EVL_ENCAPLEN; 4592cc2df49SGarrett Wollman 460f731f104SBill Paul ifv->ifv_if.if_ipackets++; 4612cc2df49SGarrett Wollman ether_input(&ifv->ifv_if, eh, m); 4622cc2df49SGarrett Wollman return 0; 4632cc2df49SGarrett Wollman } 4642cc2df49SGarrett Wollman 4652cc2df49SGarrett Wollman static int 4662cc2df49SGarrett Wollman vlan_config(struct ifvlan *ifv, struct ifnet *p) 4672cc2df49SGarrett Wollman { 4682cc2df49SGarrett Wollman struct ifaddr *ifa1, *ifa2; 4692cc2df49SGarrett Wollman struct sockaddr_dl *sdl1, *sdl2; 4702cc2df49SGarrett Wollman 4712cc2df49SGarrett Wollman if (p->if_data.ifi_type != IFT_ETHER) 4722cc2df49SGarrett Wollman return EPROTONOSUPPORT; 4732cc2df49SGarrett Wollman if (ifv->ifv_p) 4742cc2df49SGarrett Wollman return EBUSY; 4752cc2df49SGarrett Wollman ifv->ifv_p = p; 4762cc2df49SGarrett Wollman if (p->if_data.ifi_hdrlen == sizeof(struct ether_vlan_header)) 4772cc2df49SGarrett Wollman ifv->ifv_if.if_mtu = p->if_mtu; 4782cc2df49SGarrett Wollman else 4792cc2df49SGarrett Wollman ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN; 4802cc2df49SGarrett Wollman 4812cc2df49SGarrett Wollman /* 48224993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 48324993214SYaroslav Tykhiy * Other flags are none of our business. 4842cc2df49SGarrett Wollman */ 48524993214SYaroslav Tykhiy ifv->ifv_if.if_flags = (p->if_flags & 48624993214SYaroslav Tykhiy (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT)); 4872cc2df49SGarrett Wollman 4882cc2df49SGarrett Wollman /* 4892cc2df49SGarrett Wollman * Set up our ``Ethernet address'' to reflect the underlying 4902cc2df49SGarrett Wollman * physical interface's. 4912cc2df49SGarrett Wollman */ 492f9132cebSJonathan Lemon ifa1 = ifaddr_byindex(ifv->ifv_if.if_index); 493f9132cebSJonathan Lemon ifa2 = ifaddr_byindex(p->if_index); 4942cc2df49SGarrett Wollman sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr; 4952cc2df49SGarrett Wollman sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr; 4962cc2df49SGarrett Wollman sdl1->sdl_type = IFT_ETHER; 4972cc2df49SGarrett Wollman sdl1->sdl_alen = ETHER_ADDR_LEN; 4982cc2df49SGarrett Wollman bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN); 4992cc2df49SGarrett Wollman bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 5001b2a4f7aSBill Fenner 5011b2a4f7aSBill Fenner /* 5021b2a4f7aSBill Fenner * Configure multicast addresses that may already be 5031b2a4f7aSBill Fenner * joined on the vlan device. 5041b2a4f7aSBill Fenner */ 5051b2a4f7aSBill Fenner (void)vlan_setmulti(&ifv->ifv_if); 5061b2a4f7aSBill Fenner 5072cc2df49SGarrett Wollman return 0; 5082cc2df49SGarrett Wollman } 5092cc2df49SGarrett Wollman 5102cc2df49SGarrett Wollman static int 511f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 512f731f104SBill Paul { 513f731f104SBill Paul struct ifaddr *ifa; 514f731f104SBill Paul struct sockaddr_dl *sdl; 515f731f104SBill Paul struct vlan_mc_entry *mc; 516f731f104SBill Paul struct ifvlan *ifv; 517f731f104SBill Paul struct ifnet *p; 518f731f104SBill Paul int error; 519f731f104SBill Paul 520f731f104SBill Paul ifv = ifp->if_softc; 521f731f104SBill Paul p = ifv->ifv_p; 522f731f104SBill Paul 5231b2a4f7aSBill Fenner if (p) { 5241b2a4f7aSBill Fenner struct sockaddr_dl sdl; 5251b2a4f7aSBill Fenner 526f731f104SBill Paul /* 527f731f104SBill Paul * Since the interface is being unconfigured, we need to 528f731f104SBill Paul * empty the list of multicast groups that we may have joined 5291b2a4f7aSBill Fenner * while we were alive from the parent's list. 530f731f104SBill Paul */ 5311b2a4f7aSBill Fenner bzero((char *)&sdl, sizeof sdl); 5321b2a4f7aSBill Fenner sdl.sdl_len = sizeof sdl; 533f731f104SBill Paul sdl.sdl_family = AF_LINK; 5341b2a4f7aSBill Fenner sdl.sdl_index = p->if_index; 5351b2a4f7aSBill Fenner sdl.sdl_type = IFT_ETHER; 5361b2a4f7aSBill Fenner sdl.sdl_alen = ETHER_ADDR_LEN; 5371b2a4f7aSBill Fenner 5381b2a4f7aSBill Fenner while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) { 53922f29826SPoul-Henning Kamp mc = SLIST_FIRST(&ifv->vlan_mc_listhead); 540f731f104SBill Paul bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 541f731f104SBill Paul error = if_delmulti(p, (struct sockaddr *)&sdl); 542f731f104SBill Paul if (error) 543f731f104SBill Paul return(error); 544f731f104SBill Paul SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 5459d4fe4b2SBrooks Davis free(mc, M_VLAN); 546f731f104SBill Paul } 5471b2a4f7aSBill Fenner } 548f731f104SBill Paul 549f731f104SBill Paul /* Disconnect from parent. */ 550f731f104SBill Paul ifv->ifv_p = NULL; 551f731f104SBill Paul ifv->ifv_if.if_mtu = ETHERMTU; 552f731f104SBill Paul 553f731f104SBill Paul /* Clear our MAC address. */ 554f9132cebSJonathan Lemon ifa = ifaddr_byindex(ifv->ifv_if.if_index); 555f731f104SBill Paul sdl = (struct sockaddr_dl *)ifa->ifa_addr; 556f731f104SBill Paul sdl->sdl_type = IFT_ETHER; 557f731f104SBill Paul sdl->sdl_alen = ETHER_ADDR_LEN; 558f731f104SBill Paul bzero(LLADDR(sdl), ETHER_ADDR_LEN); 559f731f104SBill Paul bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 560f731f104SBill Paul 561f731f104SBill Paul return 0; 562f731f104SBill Paul } 563f731f104SBill Paul 564f731f104SBill Paul static int 565cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 5662cc2df49SGarrett Wollman { 5672cc2df49SGarrett Wollman struct ifaddr *ifa; 5682cc2df49SGarrett Wollman struct ifnet *p; 5692cc2df49SGarrett Wollman struct ifreq *ifr; 5702cc2df49SGarrett Wollman struct ifvlan *ifv; 5712cc2df49SGarrett Wollman struct vlanreq vlr; 5722cc2df49SGarrett Wollman int error = 0; 5732cc2df49SGarrett Wollman 5742cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 5752cc2df49SGarrett Wollman ifa = (struct ifaddr *)data; 5762cc2df49SGarrett Wollman ifv = ifp->if_softc; 5772cc2df49SGarrett Wollman 5782cc2df49SGarrett Wollman switch (cmd) { 5792cc2df49SGarrett Wollman case SIOCSIFADDR: 5802cc2df49SGarrett Wollman ifp->if_flags |= IFF_UP; 5812cc2df49SGarrett Wollman 5822cc2df49SGarrett Wollman switch (ifa->ifa_addr->sa_family) { 5832cc2df49SGarrett Wollman #ifdef INET 5842cc2df49SGarrett Wollman case AF_INET: 585322dcb8dSMax Khon arp_ifinit(&ifv->ifv_if, ifa); 5862cc2df49SGarrett Wollman break; 5872cc2df49SGarrett Wollman #endif 5882cc2df49SGarrett Wollman default: 5892cc2df49SGarrett Wollman break; 5902cc2df49SGarrett Wollman } 5912cc2df49SGarrett Wollman break; 5922cc2df49SGarrett Wollman 5932cc2df49SGarrett Wollman case SIOCGIFADDR: 5942cc2df49SGarrett Wollman { 5952cc2df49SGarrett Wollman struct sockaddr *sa; 5962cc2df49SGarrett Wollman 5972cc2df49SGarrett Wollman sa = (struct sockaddr *) &ifr->ifr_data; 5982cc2df49SGarrett Wollman bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, 5992cc2df49SGarrett Wollman (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 6002cc2df49SGarrett Wollman } 6012cc2df49SGarrett Wollman break; 6022cc2df49SGarrett Wollman 6032cc2df49SGarrett Wollman case SIOCSIFMTU: 6042cc2df49SGarrett Wollman /* 6052cc2df49SGarrett Wollman * Set the interface MTU. 606f731f104SBill Paul * This is bogus. The underlying interface might support 607f731f104SBill Paul * jumbo frames. 6082cc2df49SGarrett Wollman */ 6092cc2df49SGarrett Wollman if (ifr->ifr_mtu > ETHERMTU) { 6102cc2df49SGarrett Wollman error = EINVAL; 6112cc2df49SGarrett Wollman } else { 6122cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 6132cc2df49SGarrett Wollman } 6142cc2df49SGarrett Wollman break; 6152cc2df49SGarrett Wollman 6162cc2df49SGarrett Wollman case SIOCSETVLAN: 6172cc2df49SGarrett Wollman error = copyin(ifr->ifr_data, &vlr, sizeof vlr); 6182cc2df49SGarrett Wollman if (error) 6192cc2df49SGarrett Wollman break; 6202cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 621f731f104SBill Paul vlan_unconfig(ifp); 62224993214SYaroslav Tykhiy if (ifp->if_flags & IFF_UP) { 62324993214SYaroslav Tykhiy int s = splimp(); 6242cc2df49SGarrett Wollman if_down(ifp); 62524993214SYaroslav Tykhiy splx(s); 62624993214SYaroslav Tykhiy } 62724993214SYaroslav Tykhiy ifp->if_flags &= ~IFF_RUNNING; 6282cc2df49SGarrett Wollman break; 6292cc2df49SGarrett Wollman } 6302cc2df49SGarrett Wollman p = ifunit(vlr.vlr_parent); 6312cc2df49SGarrett Wollman if (p == 0) { 6322cc2df49SGarrett Wollman error = ENOENT; 6332cc2df49SGarrett Wollman break; 6342cc2df49SGarrett Wollman } 6352cc2df49SGarrett Wollman error = vlan_config(ifv, p); 6362cc2df49SGarrett Wollman if (error) 6372cc2df49SGarrett Wollman break; 6382cc2df49SGarrett Wollman ifv->ifv_tag = vlr.vlr_tag; 639ae290324SJordan K. Hubbard ifp->if_flags |= IFF_RUNNING; 6402cc2df49SGarrett Wollman break; 6412cc2df49SGarrett Wollman 6422cc2df49SGarrett Wollman case SIOCGETVLAN: 6432cc2df49SGarrett Wollman bzero(&vlr, sizeof vlr); 6442cc2df49SGarrett Wollman if (ifv->ifv_p) { 6452127f260SArchie Cobbs snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), 6462127f260SArchie Cobbs "%s%d", ifv->ifv_p->if_name, ifv->ifv_p->if_unit); 6472cc2df49SGarrett Wollman vlr.vlr_tag = ifv->ifv_tag; 6482cc2df49SGarrett Wollman } 6492cc2df49SGarrett Wollman error = copyout(&vlr, ifr->ifr_data, sizeof vlr); 6502cc2df49SGarrett Wollman break; 6512cc2df49SGarrett Wollman 6522cc2df49SGarrett Wollman case SIOCSIFFLAGS: 6532cc2df49SGarrett Wollman /* 654f731f104SBill Paul * We don't support promiscuous mode 6552cc2df49SGarrett Wollman * right now because it would require help from the 6562cc2df49SGarrett Wollman * underlying drivers, which hasn't been implemented. 6572cc2df49SGarrett Wollman */ 658f731f104SBill Paul if (ifr->ifr_flags & (IFF_PROMISC)) { 659f731f104SBill Paul ifp->if_flags &= ~(IFF_PROMISC); 6602cc2df49SGarrett Wollman error = EINVAL; 6612cc2df49SGarrett Wollman } 6622cc2df49SGarrett Wollman break; 663f731f104SBill Paul case SIOCADDMULTI: 664f731f104SBill Paul case SIOCDELMULTI: 665f731f104SBill Paul error = vlan_setmulti(ifp); 666f731f104SBill Paul break; 6672cc2df49SGarrett Wollman default: 6682cc2df49SGarrett Wollman error = EINVAL; 6692cc2df49SGarrett Wollman } 6702cc2df49SGarrett Wollman return error; 6712cc2df49SGarrett Wollman } 672