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 24631689d25SAndrew R. Reiter ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 2479d4fe4b2SBrooks Davis ifp = &ifv->ifv_if; 2489d4fe4b2SBrooks Davis SLIST_INIT(&ifv->vlan_mc_listhead); 2499d4fe4b2SBrooks Davis 2509d4fe4b2SBrooks Davis s = splnet(); 2519d4fe4b2SBrooks Davis LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list); 2529d4fe4b2SBrooks Davis splx(s); 2539d4fe4b2SBrooks Davis 2549d4fe4b2SBrooks Davis ifp->if_softc = ifv; 2559d4fe4b2SBrooks Davis ifp->if_name = "vlan"; 2569d4fe4b2SBrooks Davis ifp->if_unit = *unit; 2579d4fe4b2SBrooks Davis ifv->r_unit = r; 2589d4fe4b2SBrooks Davis /* NB: flags are not set here */ 2599d4fe4b2SBrooks Davis ifp->if_linkmib = &ifv->ifv_mib; 2609d4fe4b2SBrooks Davis ifp->if_linkmiblen = sizeof ifv->ifv_mib; 2619d4fe4b2SBrooks Davis /* NB: mtu is not set here */ 2629d4fe4b2SBrooks Davis 2639d4fe4b2SBrooks Davis ifp->if_init = vlan_ifinit; 2649d4fe4b2SBrooks Davis ifp->if_start = vlan_start; 2659d4fe4b2SBrooks Davis ifp->if_ioctl = vlan_ioctl; 2669d4fe4b2SBrooks Davis ifp->if_output = ether_output; 2679d4fe4b2SBrooks Davis ifp->if_snd.ifq_maxlen = ifqmaxlen; 2689d4fe4b2SBrooks Davis ether_ifattach(ifp, ETHER_BPF_SUPPORTED); 2699d4fe4b2SBrooks Davis /* Now undo some of the damage... */ 270211f625aSBill Fenner ifp->if_baudrate = 0; 2719d4fe4b2SBrooks Davis ifp->if_data.ifi_type = IFT_L2VLAN; 2729d4fe4b2SBrooks Davis ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN; 2739d4fe4b2SBrooks Davis 2749d4fe4b2SBrooks Davis return (0); 2759d4fe4b2SBrooks Davis } 2769d4fe4b2SBrooks Davis 2779d4fe4b2SBrooks Davis static void 2789d4fe4b2SBrooks Davis vlan_clone_destroy(struct ifnet *ifp) 2799d4fe4b2SBrooks Davis { 2809d4fe4b2SBrooks Davis struct ifvlan *ifv = ifp->if_softc; 2819d4fe4b2SBrooks Davis int s; 2829d4fe4b2SBrooks Davis int err; 2839d4fe4b2SBrooks Davis 2849d4fe4b2SBrooks Davis s = splnet(); 2859d4fe4b2SBrooks Davis LIST_REMOVE(ifv, ifv_list); 2869d4fe4b2SBrooks Davis vlan_unconfig(ifp); 2879d4fe4b2SBrooks Davis splx(s); 2889d4fe4b2SBrooks Davis 2899d4fe4b2SBrooks Davis ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); 2909d4fe4b2SBrooks Davis 2919d4fe4b2SBrooks Davis err = rman_release_resource(ifv->r_unit); 2929d4fe4b2SBrooks Davis KASSERT(err == 0, ("Unexpected error freeing resource")); 2939d4fe4b2SBrooks Davis free(ifv, M_VLAN); 2949d4fe4b2SBrooks Davis } 2959d4fe4b2SBrooks Davis 2962cc2df49SGarrett Wollman static void 2972cc2df49SGarrett Wollman vlan_ifinit(void *foo) 2982cc2df49SGarrett Wollman { 299f731f104SBill Paul return; 3002cc2df49SGarrett Wollman } 3012cc2df49SGarrett Wollman 3022cc2df49SGarrett Wollman static void 3032cc2df49SGarrett Wollman vlan_start(struct ifnet *ifp) 3042cc2df49SGarrett Wollman { 3052cc2df49SGarrett Wollman struct ifvlan *ifv; 3062cc2df49SGarrett Wollman struct ifnet *p; 3072cc2df49SGarrett Wollman struct ether_vlan_header *evl; 3085326b23cSMatthew N. Dodd struct mbuf *m; 3092cc2df49SGarrett Wollman 3102cc2df49SGarrett Wollman ifv = ifp->if_softc; 3112cc2df49SGarrett Wollman p = ifv->ifv_p; 3122cc2df49SGarrett Wollman 3132cc2df49SGarrett Wollman ifp->if_flags |= IFF_OACTIVE; 3142cc2df49SGarrett Wollman for (;;) { 3152cc2df49SGarrett Wollman IF_DEQUEUE(&ifp->if_snd, m); 3162cc2df49SGarrett Wollman if (m == 0) 3172cc2df49SGarrett Wollman break; 3182cc2df49SGarrett Wollman if (ifp->if_bpf) 319eb92a347SGarrett Wollman bpf_mtap(ifp, m); 3202cc2df49SGarrett Wollman 321f731f104SBill Paul /* 32224993214SYaroslav Tykhiy * Do not run parent's if_start() if the parent is not up, 32324993214SYaroslav Tykhiy * or parent's driver will cause a system crash. 32424993214SYaroslav Tykhiy */ 32524993214SYaroslav Tykhiy if ((p->if_flags & (IFF_UP | IFF_RUNNING)) != 32624993214SYaroslav Tykhiy (IFF_UP | IFF_RUNNING)) { 32724993214SYaroslav Tykhiy m_freem(m); 32824993214SYaroslav Tykhiy ifp->if_data.ifi_collisions++; 32924993214SYaroslav Tykhiy continue; 33024993214SYaroslav Tykhiy } 33124993214SYaroslav Tykhiy 33224993214SYaroslav Tykhiy /* 333f731f104SBill Paul * If the LINK0 flag is set, it means the underlying interface 334f731f104SBill Paul * can do VLAN tag insertion itself and doesn't require us to 335f731f104SBill Paul * create a special header for it. In this case, we just pass 336f731f104SBill Paul * the packet along. However, we need some way to tell the 337f731f104SBill Paul * interface where the packet came from so that it knows how 338f731f104SBill Paul * to find the VLAN tag to use, so we set the rcvif in the 339f731f104SBill Paul * mbuf header to our ifnet. 340f731f104SBill Paul * 341f731f104SBill Paul * Note: we also set the M_PROTO1 flag in the mbuf to let 342f731f104SBill Paul * the parent driver know that the rcvif pointer is really 343f731f104SBill Paul * valid. We need to do this because sometimes mbufs will 344f731f104SBill Paul * be allocated by other parts of the system that contain 345f731f104SBill Paul * garbage in the rcvif pointer. Using the M_PROTO1 flag 346f731f104SBill Paul * lets the driver perform a proper sanity check and avoid 347f731f104SBill Paul * following potentially bogus rcvif pointers off into 348f731f104SBill Paul * never-never land. 349f731f104SBill Paul */ 350f731f104SBill Paul if (ifp->if_flags & IFF_LINK0) { 351f731f104SBill Paul m->m_pkthdr.rcvif = ifp; 352f731f104SBill Paul m->m_flags |= M_PROTO1; 353f731f104SBill Paul } else { 3542cc2df49SGarrett Wollman M_PREPEND(m, EVL_ENCAPLEN, M_DONTWAIT); 3554af90a4dSMatthew N. Dodd if (m == NULL) { 3564af90a4dSMatthew N. Dodd printf("vlan%d: M_PREPEND failed", ifp->if_unit); 3574af90a4dSMatthew N. Dodd ifp->if_ierrors++; 3582cc2df49SGarrett Wollman continue; 3594af90a4dSMatthew N. Dodd } 3602cc2df49SGarrett Wollman /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 3612cc2df49SGarrett Wollman 3625326b23cSMatthew N. Dodd m = m_pullup(m, ETHER_HDR_LEN + EVL_ENCAPLEN); 3635326b23cSMatthew N. Dodd if (m == NULL) { 3644af90a4dSMatthew N. Dodd printf("vlan%d: m_pullup failed", ifp->if_unit); 3654af90a4dSMatthew N. Dodd ifp->if_ierrors++; 3664af90a4dSMatthew N. Dodd continue; 3674af90a4dSMatthew N. Dodd } 3684af90a4dSMatthew N. Dodd 3692cc2df49SGarrett Wollman /* 3702cc2df49SGarrett Wollman * Transform the Ethernet header into an Ethernet header 3712cc2df49SGarrett Wollman * with 802.1Q encapsulation. 3722cc2df49SGarrett Wollman */ 3732cc2df49SGarrett Wollman bcopy(mtod(m, char *) + EVL_ENCAPLEN, mtod(m, char *), 3742cc2df49SGarrett Wollman sizeof(struct ether_header)); 3752cc2df49SGarrett Wollman evl = mtod(m, struct ether_vlan_header *); 3762cc2df49SGarrett Wollman evl->evl_proto = evl->evl_encap_proto; 3779d4fe4b2SBrooks Davis evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 3782cc2df49SGarrett Wollman evl->evl_tag = htons(ifv->ifv_tag); 379f731f104SBill Paul #ifdef DEBUG 380f731f104SBill Paul printf("vlan_start: %*D\n", sizeof *evl, 38165f1bb65SPeter Wemm (unsigned char *)evl, ":"); 382f731f104SBill Paul #endif 383f731f104SBill Paul } 3842cc2df49SGarrett Wollman 3852cc2df49SGarrett Wollman /* 3862cc2df49SGarrett Wollman * Send it, precisely as ether_output() would have. 3872cc2df49SGarrett Wollman * We are already running at splimp. 3882cc2df49SGarrett Wollman */ 389df5e1987SJonathan Lemon if (IF_HANDOFF(&p->if_snd, m, p)) 390f731f104SBill Paul ifp->if_opackets++; 391df5e1987SJonathan Lemon else 392df5e1987SJonathan Lemon ifp->if_oerrors++; 3932cc2df49SGarrett Wollman } 3942cc2df49SGarrett Wollman ifp->if_flags &= ~IFF_OACTIVE; 395f731f104SBill Paul 396f731f104SBill Paul return; 397f731f104SBill Paul } 398f731f104SBill Paul 3999d4fe4b2SBrooks Davis static int 400f731f104SBill Paul vlan_input_tag(struct ether_header *eh, struct mbuf *m, u_int16_t t) 401f731f104SBill Paul { 402f731f104SBill Paul struct ifvlan *ifv; 403f731f104SBill Paul 4047a46ec8fSBrooks Davis /* 4057a46ec8fSBrooks Davis * Fake up a header and send the packet to the physical interface's 4067a46ec8fSBrooks Davis * bpf tap if active. 4077a46ec8fSBrooks Davis */ 4087a46ec8fSBrooks Davis if (m->m_pkthdr.rcvif->if_bpf != NULL) { 4097a46ec8fSBrooks Davis struct m_hdr mh; 4107a46ec8fSBrooks Davis struct ether_vlan_header evh; 4117a46ec8fSBrooks Davis 4127a46ec8fSBrooks Davis bcopy(eh, &evh, 2*ETHER_ADDR_LEN); 4137a46ec8fSBrooks Davis evh.evl_encap_proto = htons(ETHERTYPE_VLAN); 4147a46ec8fSBrooks Davis evh.evl_tag = htons(t); 4157a46ec8fSBrooks Davis evh.evl_proto = eh->ether_type; 4167a46ec8fSBrooks Davis 4177a46ec8fSBrooks Davis /* This kludge is OK; BPF treats the "mbuf" as read-only */ 4187a46ec8fSBrooks Davis mh.mh_next = m; 4197a46ec8fSBrooks Davis mh.mh_data = (char *)&evh; 4207a46ec8fSBrooks Davis mh.mh_len = ETHER_HDR_LEN + EVL_ENCAPLEN; 4217a46ec8fSBrooks Davis bpf_mtap(m->m_pkthdr.rcvif, (struct mbuf *)&mh); 4227a46ec8fSBrooks Davis } 4237a46ec8fSBrooks Davis 4249d4fe4b2SBrooks Davis for (ifv = LIST_FIRST(&ifv_list); ifv != NULL; 4259d4fe4b2SBrooks Davis ifv = LIST_NEXT(ifv, ifv_list)) { 426242c766bSBill Fenner if (m->m_pkthdr.rcvif == ifv->ifv_p 427242c766bSBill Fenner && ifv->ifv_tag == t) 428f731f104SBill Paul break; 429f731f104SBill Paul } 430f731f104SBill Paul 431242c766bSBill Fenner if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 432c0230c1bSJordan K. Hubbard m_free(m); 433c0230c1bSJordan K. Hubbard return -1; /* So the parent can take note */ 434f731f104SBill Paul } 435f731f104SBill Paul 436f731f104SBill Paul /* 437f731f104SBill Paul * Having found a valid vlan interface corresponding to 438f731f104SBill Paul * the given source interface and vlan tag, run the 439242c766bSBill Fenner * the real packet through ether_input(). 440f731f104SBill Paul */ 441f731f104SBill Paul m->m_pkthdr.rcvif = &ifv->ifv_if; 442f731f104SBill Paul 443f731f104SBill Paul ifv->ifv_if.if_ipackets++; 444f731f104SBill Paul ether_input(&ifv->ifv_if, eh, m); 445c0230c1bSJordan K. Hubbard return 0; 4462cc2df49SGarrett Wollman } 4472cc2df49SGarrett Wollman 4489d4fe4b2SBrooks Davis static int 4492cc2df49SGarrett Wollman vlan_input(struct ether_header *eh, struct mbuf *m) 4502cc2df49SGarrett Wollman { 4512cc2df49SGarrett Wollman struct ifvlan *ifv; 4522cc2df49SGarrett Wollman 4539d4fe4b2SBrooks Davis for (ifv = LIST_FIRST(&ifv_list); ifv != NULL; 4549d4fe4b2SBrooks Davis ifv = LIST_NEXT(ifv, ifv_list)) { 4552cc2df49SGarrett Wollman if (m->m_pkthdr.rcvif == ifv->ifv_p 4562cc2df49SGarrett Wollman && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *))) 4572cc2df49SGarrett Wollman == ifv->ifv_tag)) 4582cc2df49SGarrett Wollman break; 4592cc2df49SGarrett Wollman } 4602cc2df49SGarrett Wollman 461242c766bSBill Fenner if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 4622cc2df49SGarrett Wollman m_freem(m); 4632cc2df49SGarrett Wollman return -1; /* so ether_input can take note */ 4642cc2df49SGarrett Wollman } 4652cc2df49SGarrett Wollman 4662cc2df49SGarrett Wollman /* 4672cc2df49SGarrett Wollman * Having found a valid vlan interface corresponding to 4682cc2df49SGarrett Wollman * the given source interface and vlan tag, remove the 4692cc2df49SGarrett Wollman * encapsulation, and run the real packet through 4702cc2df49SGarrett Wollman * ether_input() a second time (it had better be 4712cc2df49SGarrett Wollman * reentrant!). 4722cc2df49SGarrett Wollman */ 4732cc2df49SGarrett Wollman m->m_pkthdr.rcvif = &ifv->ifv_if; 4742cc2df49SGarrett Wollman eh->ether_type = mtod(m, u_int16_t *)[1]; 4752cc2df49SGarrett Wollman m->m_data += EVL_ENCAPLEN; 4762cc2df49SGarrett Wollman m->m_len -= EVL_ENCAPLEN; 4772cc2df49SGarrett Wollman m->m_pkthdr.len -= EVL_ENCAPLEN; 4782cc2df49SGarrett Wollman 479f731f104SBill Paul ifv->ifv_if.if_ipackets++; 4802cc2df49SGarrett Wollman ether_input(&ifv->ifv_if, eh, m); 4812cc2df49SGarrett Wollman return 0; 4822cc2df49SGarrett Wollman } 4832cc2df49SGarrett Wollman 4842cc2df49SGarrett Wollman static int 4852cc2df49SGarrett Wollman vlan_config(struct ifvlan *ifv, struct ifnet *p) 4862cc2df49SGarrett Wollman { 4872cc2df49SGarrett Wollman struct ifaddr *ifa1, *ifa2; 4882cc2df49SGarrett Wollman struct sockaddr_dl *sdl1, *sdl2; 4892cc2df49SGarrett Wollman 4902cc2df49SGarrett Wollman if (p->if_data.ifi_type != IFT_ETHER) 4912cc2df49SGarrett Wollman return EPROTONOSUPPORT; 4922cc2df49SGarrett Wollman if (ifv->ifv_p) 4932cc2df49SGarrett Wollman return EBUSY; 4942cc2df49SGarrett Wollman ifv->ifv_p = p; 4952cc2df49SGarrett Wollman if (p->if_data.ifi_hdrlen == sizeof(struct ether_vlan_header)) 4962cc2df49SGarrett Wollman ifv->ifv_if.if_mtu = p->if_mtu; 4972cc2df49SGarrett Wollman else 4982cc2df49SGarrett Wollman ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN; 4992cc2df49SGarrett Wollman 5002cc2df49SGarrett Wollman /* 50124993214SYaroslav Tykhiy * Copy only a selected subset of flags from the parent. 50224993214SYaroslav Tykhiy * Other flags are none of our business. 5032cc2df49SGarrett Wollman */ 50424993214SYaroslav Tykhiy ifv->ifv_if.if_flags = (p->if_flags & 50524993214SYaroslav Tykhiy (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT)); 5062cc2df49SGarrett Wollman 5072cc2df49SGarrett Wollman /* 5082cc2df49SGarrett Wollman * Set up our ``Ethernet address'' to reflect the underlying 5092cc2df49SGarrett Wollman * physical interface's. 5102cc2df49SGarrett Wollman */ 511f9132cebSJonathan Lemon ifa1 = ifaddr_byindex(ifv->ifv_if.if_index); 512f9132cebSJonathan Lemon ifa2 = ifaddr_byindex(p->if_index); 5132cc2df49SGarrett Wollman sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr; 5142cc2df49SGarrett Wollman sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr; 5152cc2df49SGarrett Wollman sdl1->sdl_type = IFT_ETHER; 5162cc2df49SGarrett Wollman sdl1->sdl_alen = ETHER_ADDR_LEN; 5172cc2df49SGarrett Wollman bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN); 5182cc2df49SGarrett Wollman bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 5191b2a4f7aSBill Fenner 5201b2a4f7aSBill Fenner /* 5211b2a4f7aSBill Fenner * Configure multicast addresses that may already be 5221b2a4f7aSBill Fenner * joined on the vlan device. 5231b2a4f7aSBill Fenner */ 5241b2a4f7aSBill Fenner (void)vlan_setmulti(&ifv->ifv_if); 5251b2a4f7aSBill Fenner 5262cc2df49SGarrett Wollman return 0; 5272cc2df49SGarrett Wollman } 5282cc2df49SGarrett Wollman 5292cc2df49SGarrett Wollman static int 530f731f104SBill Paul vlan_unconfig(struct ifnet *ifp) 531f731f104SBill Paul { 532f731f104SBill Paul struct ifaddr *ifa; 533f731f104SBill Paul struct sockaddr_dl *sdl; 534f731f104SBill Paul struct vlan_mc_entry *mc; 535f731f104SBill Paul struct ifvlan *ifv; 536f731f104SBill Paul struct ifnet *p; 537f731f104SBill Paul int error; 538f731f104SBill Paul 539f731f104SBill Paul ifv = ifp->if_softc; 540f731f104SBill Paul p = ifv->ifv_p; 541f731f104SBill Paul 5421b2a4f7aSBill Fenner if (p) { 5431b2a4f7aSBill Fenner struct sockaddr_dl sdl; 5441b2a4f7aSBill Fenner 545f731f104SBill Paul /* 546f731f104SBill Paul * Since the interface is being unconfigured, we need to 547f731f104SBill Paul * empty the list of multicast groups that we may have joined 5481b2a4f7aSBill Fenner * while we were alive from the parent's list. 549f731f104SBill Paul */ 5501b2a4f7aSBill Fenner bzero((char *)&sdl, sizeof sdl); 5511b2a4f7aSBill Fenner sdl.sdl_len = sizeof sdl; 552f731f104SBill Paul sdl.sdl_family = AF_LINK; 5531b2a4f7aSBill Fenner sdl.sdl_index = p->if_index; 5541b2a4f7aSBill Fenner sdl.sdl_type = IFT_ETHER; 5551b2a4f7aSBill Fenner sdl.sdl_alen = ETHER_ADDR_LEN; 5561b2a4f7aSBill Fenner 5571b2a4f7aSBill Fenner while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) { 55822f29826SPoul-Henning Kamp mc = SLIST_FIRST(&ifv->vlan_mc_listhead); 559f731f104SBill Paul bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 560f731f104SBill Paul error = if_delmulti(p, (struct sockaddr *)&sdl); 561f731f104SBill Paul if (error) 562f731f104SBill Paul return(error); 563f731f104SBill Paul SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 5649d4fe4b2SBrooks Davis free(mc, M_VLAN); 565f731f104SBill Paul } 5661b2a4f7aSBill Fenner } 567f731f104SBill Paul 568f731f104SBill Paul /* Disconnect from parent. */ 569f731f104SBill Paul ifv->ifv_p = NULL; 570f731f104SBill Paul ifv->ifv_if.if_mtu = ETHERMTU; 571f731f104SBill Paul 572f731f104SBill Paul /* Clear our MAC address. */ 573f9132cebSJonathan Lemon ifa = ifaddr_byindex(ifv->ifv_if.if_index); 574f731f104SBill Paul sdl = (struct sockaddr_dl *)ifa->ifa_addr; 575f731f104SBill Paul sdl->sdl_type = IFT_ETHER; 576f731f104SBill Paul sdl->sdl_alen = ETHER_ADDR_LEN; 577f731f104SBill Paul bzero(LLADDR(sdl), ETHER_ADDR_LEN); 578f731f104SBill Paul bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 579f731f104SBill Paul 580f731f104SBill Paul return 0; 581f731f104SBill Paul } 582f731f104SBill Paul 583f731f104SBill Paul static int 584cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 5852cc2df49SGarrett Wollman { 5862cc2df49SGarrett Wollman struct ifaddr *ifa; 5872cc2df49SGarrett Wollman struct ifnet *p; 5882cc2df49SGarrett Wollman struct ifreq *ifr; 5892cc2df49SGarrett Wollman struct ifvlan *ifv; 5902cc2df49SGarrett Wollman struct vlanreq vlr; 5912cc2df49SGarrett Wollman int error = 0; 5922cc2df49SGarrett Wollman 5932cc2df49SGarrett Wollman ifr = (struct ifreq *)data; 5942cc2df49SGarrett Wollman ifa = (struct ifaddr *)data; 5952cc2df49SGarrett Wollman ifv = ifp->if_softc; 5962cc2df49SGarrett Wollman 5972cc2df49SGarrett Wollman switch (cmd) { 5982cc2df49SGarrett Wollman case SIOCSIFADDR: 5992cc2df49SGarrett Wollman ifp->if_flags |= IFF_UP; 6002cc2df49SGarrett Wollman 6012cc2df49SGarrett Wollman switch (ifa->ifa_addr->sa_family) { 6022cc2df49SGarrett Wollman #ifdef INET 6032cc2df49SGarrett Wollman case AF_INET: 604322dcb8dSMax Khon arp_ifinit(&ifv->ifv_if, ifa); 6052cc2df49SGarrett Wollman break; 6062cc2df49SGarrett Wollman #endif 6072cc2df49SGarrett Wollman default: 6082cc2df49SGarrett Wollman break; 6092cc2df49SGarrett Wollman } 6102cc2df49SGarrett Wollman break; 6112cc2df49SGarrett Wollman 6122cc2df49SGarrett Wollman case SIOCGIFADDR: 6132cc2df49SGarrett Wollman { 6142cc2df49SGarrett Wollman struct sockaddr *sa; 6152cc2df49SGarrett Wollman 6162cc2df49SGarrett Wollman sa = (struct sockaddr *) &ifr->ifr_data; 6172cc2df49SGarrett Wollman bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, 6182cc2df49SGarrett Wollman (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 6192cc2df49SGarrett Wollman } 6202cc2df49SGarrett Wollman break; 6212cc2df49SGarrett Wollman 6222cc2df49SGarrett Wollman case SIOCSIFMTU: 6232cc2df49SGarrett Wollman /* 6242cc2df49SGarrett Wollman * Set the interface MTU. 625f731f104SBill Paul * This is bogus. The underlying interface might support 626f731f104SBill Paul * jumbo frames. 6272cc2df49SGarrett Wollman */ 6282cc2df49SGarrett Wollman if (ifr->ifr_mtu > ETHERMTU) { 6292cc2df49SGarrett Wollman error = EINVAL; 6302cc2df49SGarrett Wollman } else { 6312cc2df49SGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 6322cc2df49SGarrett Wollman } 6332cc2df49SGarrett Wollman break; 6342cc2df49SGarrett Wollman 6352cc2df49SGarrett Wollman case SIOCSETVLAN: 6362cc2df49SGarrett Wollman error = copyin(ifr->ifr_data, &vlr, sizeof vlr); 6372cc2df49SGarrett Wollman if (error) 6382cc2df49SGarrett Wollman break; 6392cc2df49SGarrett Wollman if (vlr.vlr_parent[0] == '\0') { 640f731f104SBill Paul vlan_unconfig(ifp); 64124993214SYaroslav Tykhiy if (ifp->if_flags & IFF_UP) { 64224993214SYaroslav Tykhiy int s = splimp(); 6432cc2df49SGarrett Wollman if_down(ifp); 64424993214SYaroslav Tykhiy splx(s); 64524993214SYaroslav Tykhiy } 64624993214SYaroslav Tykhiy ifp->if_flags &= ~IFF_RUNNING; 6472cc2df49SGarrett Wollman break; 6482cc2df49SGarrett Wollman } 6492cc2df49SGarrett Wollman p = ifunit(vlr.vlr_parent); 6502cc2df49SGarrett Wollman if (p == 0) { 6512cc2df49SGarrett Wollman error = ENOENT; 6522cc2df49SGarrett Wollman break; 6532cc2df49SGarrett Wollman } 6542cc2df49SGarrett Wollman error = vlan_config(ifv, p); 6552cc2df49SGarrett Wollman if (error) 6562cc2df49SGarrett Wollman break; 6572cc2df49SGarrett Wollman ifv->ifv_tag = vlr.vlr_tag; 658ae290324SJordan K. Hubbard ifp->if_flags |= IFF_RUNNING; 6592cc2df49SGarrett Wollman break; 6602cc2df49SGarrett Wollman 6612cc2df49SGarrett Wollman case SIOCGETVLAN: 6622cc2df49SGarrett Wollman bzero(&vlr, sizeof vlr); 6632cc2df49SGarrett Wollman if (ifv->ifv_p) { 6642127f260SArchie Cobbs snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), 6652127f260SArchie Cobbs "%s%d", ifv->ifv_p->if_name, ifv->ifv_p->if_unit); 6662cc2df49SGarrett Wollman vlr.vlr_tag = ifv->ifv_tag; 6672cc2df49SGarrett Wollman } 6682cc2df49SGarrett Wollman error = copyout(&vlr, ifr->ifr_data, sizeof vlr); 6692cc2df49SGarrett Wollman break; 6702cc2df49SGarrett Wollman 6712cc2df49SGarrett Wollman case SIOCSIFFLAGS: 6722cc2df49SGarrett Wollman /* 673f731f104SBill Paul * We don't support promiscuous mode 6742cc2df49SGarrett Wollman * right now because it would require help from the 6752cc2df49SGarrett Wollman * underlying drivers, which hasn't been implemented. 6762cc2df49SGarrett Wollman */ 677f731f104SBill Paul if (ifr->ifr_flags & (IFF_PROMISC)) { 678f731f104SBill Paul ifp->if_flags &= ~(IFF_PROMISC); 6792cc2df49SGarrett Wollman error = EINVAL; 6802cc2df49SGarrett Wollman } 6812cc2df49SGarrett Wollman break; 682f731f104SBill Paul case SIOCADDMULTI: 683f731f104SBill Paul case SIOCDELMULTI: 684f731f104SBill Paul error = vlan_setmulti(ifp); 685f731f104SBill Paul break; 6862cc2df49SGarrett Wollman default: 6872cc2df49SGarrett Wollman error = EINVAL; 6882cc2df49SGarrett Wollman } 6892cc2df49SGarrett Wollman return error; 6902cc2df49SGarrett Wollman } 691