From 7da2447808dd5233fae946a91d1cbc895d9ed346 Mon Sep 17 00:00:00 2001 From: brooks Date: Wed, 5 Sep 2001 21:10:28 +0000 Subject: Make vlan(4) loadable, unloadable, and clonable. As a side effect, interfaces must now always enable VLAN support. Reviewed by: jlemon MFC after: 3 weeks --- sys/net/ethernet.h | 35 ++++++++++ sys/net/if_ethersubr.c | 20 ++---- sys/net/if_vlan.c | 180 +++++++++++++++++++++++++++++++++++-------------- sys/net/if_vlan_var.h | 10 +-- 4 files changed, 173 insertions(+), 72 deletions(-) (limited to 'sys/net') diff --git a/sys/net/ethernet.h b/sys/net/ethernet.h index 15cab6c..b5a0fae 100644 --- a/sys/net/ethernet.h +++ b/sys/net/ethernet.h @@ -99,6 +99,41 @@ extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); extern void (*ng_ether_attach_p)(struct ifnet *ifp); extern void (*ng_ether_detach_p)(struct ifnet *ifp); +extern int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m); +extern int (*vlan_input_tag_p)(struct ether_header *eh, struct mbuf *m, + u_int16_t t); +#define _VLAN_INPUT(ifp, eh, m) do { \ + if (vlan_input_p != NULL) { \ + if ((*vlan_input_p)(eh, m) == -1) \ + (ifp)->if_noproto++; \ + } else { \ + m_free(m); \ + (ifp)->if_noproto++; \ + } \ +} while (0) + +#define VLAN_INPUT(ifp, eh, m) do { \ + /* XXX: lock */ \ + _VLAN_INPUT(ifp, eh, m); \ + /* XXX: unlock */ \ +} while (0) + +#define _VLAN_INPUT_TAG(ifp, eh, m, t) do { \ + if (vlan_input_tag_p != NULL) { \ + if ((*vlan_input_tag_p)(eh, m, t) == -1) \ + (ifp)->if_noproto++; \ + } else { \ + m_free(m); \ + (ifp)->if_noproto++; \ + } \ +} while (0) + +#define VLAN_INPUT_TAG(ifp, eh, m, t) do { \ + /* XXX: lock */ \ + _VLAN_INPUT_TAG(ifp, eh, m, t); \ + /* XXX: unlock */ \ +} while (0) + #else /* _KERNEL */ #include diff --git a/sys/net/if_ethersubr.c b/sys/net/if_ethersubr.c index 5b646b8..3a943bf 100644 --- a/sys/net/if_ethersubr.c +++ b/sys/net/if_ethersubr.c @@ -101,11 +101,6 @@ extern u_char aarp_org_code[3]; #include #endif -#include "vlan.h" -#if NVLAN > 0 -#include -#endif /* NVLAN > 0 */ - /* netgraph node hooks for ng_ether(4) */ void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp, struct ether_header *eh); @@ -115,6 +110,10 @@ int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); void (*ng_ether_attach_p)(struct ifnet *ifp); void (*ng_ether_detach_p)(struct ifnet *ifp); +int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m); +int (*vlan_input_tag_p)(struct ether_header *eh, struct mbuf *m, + u_int16_t t); + static int ether_resolvemulti __P((struct ifnet *, struct sockaddr **, struct sockaddr *)); u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; @@ -524,14 +523,6 @@ ether_demux(ifp, eh, m) ether_type = ntohs(eh->ether_type); -#if NVLAN > 0 - if (ether_type == vlan_proto) { - if (vlan_input(eh, m) < 0) - ifp->if_data.ifi_noproto++; - return; - } -#endif /* NVLAN > 0 */ - switch (ether_type) { #ifdef INET case ETHERTYPE_IP: @@ -582,6 +573,9 @@ ether_demux(ifp, eh, m) aarpinput(IFP2AC(ifp), m); /* XXX */ return; #endif NETATALK + case ETHERTYPE_VLAN: + VLAN_INPUT(ifp, eh, m); + return; default: #ifdef IPX if (ef_inputp && ef_inputp(ifp, eh, m) == 0) diff --git a/sys/net/if_vlan.c b/sys/net/if_vlan.c index 40067dc..815ae37 100644 --- a/sys/net/if_vlan.c +++ b/sys/net/if_vlan.c @@ -54,7 +54,6 @@ * tag value that goes with it. */ -#include "vlan.h" #include "opt_inet.h" #include @@ -67,6 +66,8 @@ #include #include #include +#include /* XXX: Shouldn't really be required! */ +#include #include #include @@ -81,23 +82,32 @@ #include #endif +#define VLANNAME "vlan" +#define VLAN_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */ + SYSCTL_DECL(_net_link); SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); -u_int vlan_proto = ETHERTYPE_VLAN; -SYSCTL_INT(_net_link_vlan_link, VLANCTL_PROTO, proto, CTLFLAG_RW, &vlan_proto, - 0, "Ethernet protocol used for VLAN encapsulation"); - -static struct ifvlan ifv_softc[NVLAN]; +static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface"); +static struct rman vlanunits[1]; +static LIST_HEAD(, ifvlan) ifv_list; +static int vlan_clone_create(struct if_clone *, int *); +static void vlan_clone_destroy(struct ifnet *); static void vlan_start(struct ifnet *ifp); static void vlan_ifinit(void *foo); +static int vlan_input(struct ether_header *eh, struct mbuf *m); +static int vlan_input_tag(struct ether_header *eh, struct mbuf *m, + u_int16_t t); static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); static int vlan_setmulti(struct ifnet *ifp); static int vlan_unconfig(struct ifnet *ifp); static int vlan_config(struct ifvlan *ifv, struct ifnet *p); +struct if_clone vlan_cloner = + IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy); + /* * Program our multicast filter. What we're actually doing is * programming the multicast filter of the parent. This has the @@ -142,14 +152,14 @@ vlan_setmulti(struct ifnet *ifp) if (error) return(error); SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); - free(mc, M_DEVBUF); + free(mc, M_VLAN); } /* Now program new ones. */ TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { if (ifma->ifma_addr->sa_family != AF_LINK) continue; - mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_WAITOK); + mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK); bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), (char *)&mc->mc_addr, ETHER_ADDR_LEN); SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); @@ -163,44 +173,40 @@ vlan_setmulti(struct ifnet *ifp) return(0); } -static void -vlaninit(void) -{ - int i; - - for (i = 0; i < NVLAN; i++) { - struct ifnet *ifp = &ifv_softc[i].ifv_if; - - ifp->if_softc = &ifv_softc[i]; - ifp->if_name = "vlan"; - ifp->if_unit = i; - /* NB: flags are not set here */ - ifp->if_linkmib = &ifv_softc[i].ifv_mib; - ifp->if_linkmiblen = sizeof ifv_softc[i].ifv_mib; - /* NB: mtu is not set here */ - - ifp->if_init = vlan_ifinit; - ifp->if_start = vlan_start; - ifp->if_ioctl = vlan_ioctl; - ifp->if_output = ether_output; - ifp->if_snd.ifq_maxlen = ifqmaxlen; - ether_ifattach(ifp, ETHER_BPF_SUPPORTED); - /* Now undo some of the damage... */ - ifp->if_data.ifi_type = IFT_L2VLAN; - ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN; - } -} - static int vlan_modevent(module_t mod, int type, void *data) { + int err; + switch (type) { case MOD_LOAD: - vlaninit(); + vlanunits->rm_type = RMAN_ARRAY; + vlanunits->rm_descr = "configurable if_vlan units"; + err = rman_init(vlanunits); + if (err != 0) + return (err); + err = rman_manage_region(vlanunits, 0, VLAN_MAXUNIT); + if (err != 0) { + printf("%s: vlanunits: rman_manage_region: Failed %d\n", + VLANNAME, err); + rman_fini(vlanunits); + return (err); + } + LIST_INIT(&ifv_list); + vlan_input_p = vlan_input; + vlan_input_tag_p = vlan_input_tag; + if_clone_attach(&vlan_cloner); break; case MOD_UNLOAD: - printf("if_vlan module unload - not possible for this module type\n"); - return EINVAL; + if_clone_detach(&vlan_cloner); + vlan_input_p = NULL; + vlan_input_tag_p = NULL; + while (!LIST_EMPTY(&ifv_list)) + vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if); + err = rman_fini(vlanunits); + if (err != 0) + return (err); + break; } return 0; } @@ -213,6 +219,80 @@ static moduledata_t vlan_mod = { DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); +static int +vlan_clone_create(struct if_clone *ifc, int *unit) +{ + struct resource *r; + struct ifvlan *ifv; + struct ifnet *ifp; + int s; + + if (*unit > VLAN_MAXUNIT) + return (ENXIO); + + if (*unit < 0) { + r = rman_reserve_resource(vlanunits, 0, VLAN_MAXUNIT, 1, + RF_ALLOCATED | RF_ACTIVE, NULL); + if (r == NULL) + return (ENOSPC); + *unit = rman_get_start(r); + } else { + r = rman_reserve_resource(vlanunits, *unit, *unit, 1, + RF_ALLOCATED | RF_ACTIVE, NULL); + if (r == NULL) + return (EEXIST); + } + + ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK); + memset(ifv, 0, sizeof(struct ifvlan)); + ifp = &ifv->ifv_if; + SLIST_INIT(&ifv->vlan_mc_listhead); + + s = splnet(); + LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list); + splx(s); + + ifp->if_softc = ifv; + ifp->if_name = "vlan"; + ifp->if_unit = *unit; + ifv->r_unit = r; + /* NB: flags are not set here */ + ifp->if_linkmib = &ifv->ifv_mib; + ifp->if_linkmiblen = sizeof ifv->ifv_mib; + /* NB: mtu is not set here */ + + ifp->if_init = vlan_ifinit; + ifp->if_start = vlan_start; + ifp->if_ioctl = vlan_ioctl; + ifp->if_output = ether_output; + ifp->if_snd.ifq_maxlen = ifqmaxlen; + ether_ifattach(ifp, ETHER_BPF_SUPPORTED); + /* Now undo some of the damage... */ + ifp->if_data.ifi_type = IFT_L2VLAN; + ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN; + + return (0); +} + +static void +vlan_clone_destroy(struct ifnet *ifp) +{ + struct ifvlan *ifv = ifp->if_softc; + int s; + int err; + + s = splnet(); + LIST_REMOVE(ifv, ifv_list); + vlan_unconfig(ifp); + splx(s); + + ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); + + err = rman_release_resource(ifv->r_unit); + KASSERT(err == 0, ("Unexpected error freeing resource")); + free(ifv, M_VLAN); +} + static void vlan_ifinit(void *foo) { @@ -294,7 +374,7 @@ vlan_start(struct ifnet *ifp) sizeof(struct ether_header)); evl = mtod(m, struct ether_vlan_header *); evl->evl_proto = evl->evl_encap_proto; - evl->evl_encap_proto = htons(vlan_proto); + evl->evl_encap_proto = htons(ETHERTYPE_VLAN); evl->evl_tag = htons(ifv->ifv_tag); #ifdef DEBUG printf("vlan_start: %*D\n", sizeof *evl, @@ -316,19 +396,18 @@ vlan_start(struct ifnet *ifp) return; } -int +static int vlan_input_tag(struct ether_header *eh, struct mbuf *m, u_int16_t t) { - int i; struct ifvlan *ifv; - for (i = 0; i < NVLAN; i++) { - ifv = &ifv_softc[i]; + for (ifv = LIST_FIRST(&ifv_list); ifv != NULL; + ifv = LIST_NEXT(ifv, ifv_list)) { if (ifv->ifv_tag == t) break; } - if (i >= NVLAN || (ifv->ifv_if.if_flags & IFF_UP) == 0) { + if (ifv !=NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) { m_free(m); return -1; /* So the parent can take note */ } @@ -345,21 +424,20 @@ vlan_input_tag(struct ether_header *eh, struct mbuf *m, u_int16_t t) return 0; } -int +static int vlan_input(struct ether_header *eh, struct mbuf *m) { - int i; struct ifvlan *ifv; - for (i = 0; i < NVLAN; i++) { - ifv = &ifv_softc[i]; + for (ifv = LIST_FIRST(&ifv_list); ifv != NULL; + ifv = LIST_NEXT(ifv, ifv_list)) { if (m->m_pkthdr.rcvif == ifv->ifv_p && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *))) == ifv->ifv_tag)) break; } - if (i >= NVLAN || (ifv->ifv_if.if_flags & IFF_UP) == 0) { + if (ifv != NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) { m_freem(m); return -1; /* so ether_input can take note */ } @@ -462,7 +540,7 @@ vlan_unconfig(struct ifnet *ifp) if (error) return(error); SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); - free(mc, M_DEVBUF); + free(mc, M_VLAN); } } diff --git a/sys/net/if_vlan_var.h b/sys/net/if_vlan_var.h index ca7f5ad..faaad57 100644 --- a/sys/net/if_vlan_var.h +++ b/sys/net/if_vlan_var.h @@ -47,6 +47,8 @@ struct ifvlan { u_int16_t ifvm_tag; /* tag to apply on packets leaving if */ } ifv_mib; SLIST_HEAD(__vlan_mchead, vlan_mc_entry) vlan_mc_listhead; + LIST_ENTRY(ifvlan) ifv_list; + struct resource *r_unit; /* resource allocated for this unit */ }; #define ifv_if ifv_ac.ac_if #define ifv_tag ifv_mib.ifvm_tag @@ -78,12 +80,4 @@ struct vlanreq { #define SIOCSETVLAN SIOCSIFGENERIC #define SIOCGETVLAN SIOCGIFGENERIC -#ifdef _KERNEL -/* shared with if_ethersubr.c: */ -extern u_int vlan_proto; -extern int vlan_input(struct ether_header *eh, struct mbuf *m); -extern int vlan_input_tag(struct ether_header *eh, - struct mbuf *m, u_int16_t t); -#endif - #endif /* _NET_IF_VLAN_VAR_H_ */ -- cgit v1.1