2 * Copyright 1998 Massachusetts Institute of Technology
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
34 * Might be extended some day to also handle IEEE 802.1p priority
35 * tagging. This is sort of sneaky in the implementation, since
36 * we need to pretend to be enough of an Ethernet implementation
37 * to make arp work. The way we do this is by telling everyone
38 * that we are an Ethernet, and then catch the packets that
39 * ether_output() left on our output queue when it calls
40 * if_start(), rewrite them for use by the real outgoing interface,
41 * and ask it to send them.
44 * XXX It's incorrect to assume that we must always kludge up
45 * headers on the physical device's behalf: some devices support
46 * VLAN tag insertion and extraction in firmware. For these cases,
47 * one can change the behavior of the vlan interface by setting
48 * the LINK0 flag on it (that is setting the vlan interface's LINK0
49 * flag, _not_ the parent's LINK0 flag; we try to leave the parent
50 * alone). If the interface has the LINK0 flag set, then it will
51 * not modify the ethernet header on output, because the parent
52 * can do that for itself. On input, the parent can call vlan_input_tag()
53 * directly in order to supply us with an incoming mbuf and the vlan
54 * tag value that goes with it.
59 #include <sys/param.h>
60 #include <sys/kernel.h>
61 #include <sys/malloc.h>
63 #include <sys/module.h>
64 #include <sys/queue.h>
65 #include <sys/socket.h>
66 #include <sys/sockio.h>
67 #include <sys/sysctl.h>
68 #include <sys/systm.h>
71 #include <net/ethernet.h>
73 #include <net/if_arp.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <net/if_vlan_var.h>
79 #include <netinet/in.h>
80 #include <netinet/if_ether.h>
83 #define VLANNAME "vlan"
85 SYSCTL_DECL(_net_link);
86 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
87 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
89 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
90 static LIST_HEAD(, ifvlan) ifv_list;
92 static int vlan_clone_create(struct if_clone *, int);
93 static void vlan_clone_destroy(struct ifnet *);
94 static void vlan_start(struct ifnet *ifp);
95 static void vlan_ifinit(void *foo);
96 static int vlan_input(struct ether_header *eh, struct mbuf *m);
97 static int vlan_input_tag(struct ether_header *eh, struct mbuf *m,
99 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
100 static int vlan_setmulti(struct ifnet *ifp);
101 static int vlan_unconfig(struct ifnet *ifp);
102 static int vlan_config(struct ifvlan *ifv, struct ifnet *p);
104 struct if_clone vlan_cloner = IF_CLONE_INITIALIZER("vlan",
105 vlan_clone_create, vlan_clone_destroy, 0, IF_MAXUNIT);
108 * Program our multicast filter. What we're actually doing is
109 * programming the multicast filter of the parent. This has the
110 * side effect of causing the parent interface to receive multicast
111 * traffic that it doesn't really want, which ends up being discarded
112 * later by the upper protocol layers. Unfortunately, there's no way
113 * to avoid this: there really is only one physical interface.
116 vlan_setmulti(struct ifnet *ifp)
119 struct ifmultiaddr *ifma, *rifma = NULL;
121 struct vlan_mc_entry *mc = NULL;
122 struct sockaddr_dl sdl;
125 /* Find the parent. */
130 * If we don't have a parent, just remember the membership for
136 bzero((char *)&sdl, sizeof sdl);
137 sdl.sdl_len = sizeof sdl;
138 sdl.sdl_family = AF_LINK;
139 sdl.sdl_index = ifp_p->if_index;
140 sdl.sdl_type = IFT_ETHER;
141 sdl.sdl_alen = ETHER_ADDR_LEN;
143 /* First, remove any existing filter entries. */
144 while(SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
145 mc = SLIST_FIRST(&sc->vlan_mc_listhead);
146 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
147 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
150 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
154 /* Now program new ones. */
155 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
156 if (ifma->ifma_addr->sa_family != AF_LINK)
158 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
159 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
160 (char *)&mc->mc_addr, ETHER_ADDR_LEN);
161 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
162 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
163 LLADDR(&sdl), ETHER_ADDR_LEN);
164 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
173 vlan_modevent(module_t mod, int type, void *data)
178 LIST_INIT(&ifv_list);
179 vlan_input_p = vlan_input;
180 vlan_input_tag_p = vlan_input_tag;
181 if_clone_attach(&vlan_cloner);
184 if_clone_detach(&vlan_cloner);
186 vlan_input_tag_p = NULL;
187 while (!LIST_EMPTY(&ifv_list))
188 vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
194 static moduledata_t vlan_mod = {
200 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
203 vlan_clone_create(struct if_clone *ifc, int unit)
209 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
211 SLIST_INIT(&ifv->vlan_mc_listhead);
214 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
218 ifp->if_name = "vlan";
220 /* NB: flags are not set here */
221 ifp->if_linkmib = &ifv->ifv_mib;
222 ifp->if_linkmiblen = sizeof ifv->ifv_mib;
223 /* NB: mtu is not set here */
225 ifp->if_init = vlan_ifinit;
226 ifp->if_start = vlan_start;
227 ifp->if_ioctl = vlan_ioctl;
228 ifp->if_output = ether_output;
229 ifp->if_snd.ifq_maxlen = ifqmaxlen;
230 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
231 /* Now undo some of the damage... */
232 ifp->if_baudrate = 0;
233 ifp->if_data.ifi_type = IFT_L2VLAN;
234 ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
240 vlan_clone_destroy(struct ifnet *ifp)
242 struct ifvlan *ifv = ifp->if_softc;
246 LIST_REMOVE(ifv, ifv_list);
250 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
256 vlan_ifinit(void *foo)
262 vlan_start(struct ifnet *ifp)
266 struct ether_vlan_header *evl;
272 ifp->if_flags |= IFF_OACTIVE;
274 IF_DEQUEUE(&ifp->if_snd, m);
281 * Do not run parent's if_start() if the parent is not up,
282 * or parent's driver will cause a system crash.
284 if ((p->if_flags & (IFF_UP | IFF_RUNNING)) !=
285 (IFF_UP | IFF_RUNNING)) {
287 ifp->if_data.ifi_collisions++;
292 * If the LINK0 flag is set, it means the underlying interface
293 * can do VLAN tag insertion itself and doesn't require us to
294 * create a special header for it. In this case, we just pass
295 * the packet along. However, we need some way to tell the
296 * interface where the packet came from so that it knows how
297 * to find the VLAN tag to use, so we set the rcvif in the
298 * mbuf header to our ifnet.
300 * Note: we also set the M_PROTO1 flag in the mbuf to let
301 * the parent driver know that the rcvif pointer is really
302 * valid. We need to do this because sometimes mbufs will
303 * be allocated by other parts of the system that contain
304 * garbage in the rcvif pointer. Using the M_PROTO1 flag
305 * lets the driver perform a proper sanity check and avoid
306 * following potentially bogus rcvif pointers off into
309 if (ifp->if_flags & IFF_LINK0) {
310 m->m_pkthdr.rcvif = ifp;
311 m->m_flags |= M_PROTO1;
313 M_PREPEND(m, EVL_ENCAPLEN, M_DONTWAIT);
315 printf("vlan%d: M_PREPEND failed", ifp->if_unit);
319 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
321 m = m_pullup(m, ETHER_HDR_LEN + EVL_ENCAPLEN);
323 printf("vlan%d: m_pullup failed", ifp->if_unit);
329 * Transform the Ethernet header into an Ethernet header
330 * with 802.1Q encapsulation.
332 bcopy(mtod(m, char *) + EVL_ENCAPLEN, mtod(m, char *),
333 sizeof(struct ether_header));
334 evl = mtod(m, struct ether_vlan_header *);
335 evl->evl_proto = evl->evl_encap_proto;
336 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
337 evl->evl_tag = htons(ifv->ifv_tag);
339 printf("vlan_start: %*D\n", sizeof *evl,
340 (unsigned char *)evl, ":");
345 * Send it, precisely as ether_output() would have.
346 * We are already running at splimp.
348 if (IF_HANDOFF(&p->if_snd, m, p))
353 ifp->if_flags &= ~IFF_OACTIVE;
359 vlan_input_tag(struct ether_header *eh, struct mbuf *m, u_int16_t t)
364 * Fake up a header and send the packet to the physical interface's
367 if (m->m_pkthdr.rcvif->if_bpf != NULL) {
369 struct ether_vlan_header evh;
371 bcopy(eh, &evh, 2*ETHER_ADDR_LEN);
372 evh.evl_encap_proto = htons(ETHERTYPE_VLAN);
373 evh.evl_tag = htons(t);
374 evh.evl_proto = eh->ether_type;
376 /* This kludge is OK; BPF treats the "mbuf" as read-only */
378 mh.mh_data = (char *)&evh;
379 mh.mh_len = ETHER_HDR_LEN + EVL_ENCAPLEN;
380 bpf_mtap(m->m_pkthdr.rcvif, (struct mbuf *)&mh);
383 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
384 ifv = LIST_NEXT(ifv, ifv_list)) {
385 if (m->m_pkthdr.rcvif == ifv->ifv_p
386 && ifv->ifv_tag == t)
390 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
391 m->m_pkthdr.rcvif->if_noproto++;
393 return -1; /* So the parent can take note */
397 * Having found a valid vlan interface corresponding to
398 * the given source interface and vlan tag, run the
399 * the real packet through ether_input().
401 m->m_pkthdr.rcvif = &ifv->ifv_if;
403 ifv->ifv_if.if_ipackets++;
404 ether_input(&ifv->ifv_if, eh, m);
409 vlan_input(struct ether_header *eh, struct mbuf *m)
413 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
414 ifv = LIST_NEXT(ifv, ifv_list)) {
415 if (m->m_pkthdr.rcvif == ifv->ifv_p
416 && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *)))
421 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
422 m->m_pkthdr.rcvif->if_noproto++;
424 return -1; /* so ether_input can take note */
428 * Having found a valid vlan interface corresponding to
429 * the given source interface and vlan tag, remove the
430 * encapsulation, and run the real packet through
431 * ether_input() a second time (it had better be
434 m->m_pkthdr.rcvif = &ifv->ifv_if;
435 eh->ether_type = mtod(m, u_int16_t *)[1];
436 m->m_data += EVL_ENCAPLEN;
437 m->m_len -= EVL_ENCAPLEN;
438 m->m_pkthdr.len -= EVL_ENCAPLEN;
440 ifv->ifv_if.if_ipackets++;
441 ether_input(&ifv->ifv_if, eh, m);
446 vlan_config(struct ifvlan *ifv, struct ifnet *p)
448 struct ifaddr *ifa1, *ifa2;
449 struct sockaddr_dl *sdl1, *sdl2;
451 if (p->if_data.ifi_type != IFT_ETHER)
452 return EPROTONOSUPPORT;
456 if (p->if_data.ifi_hdrlen == sizeof(struct ether_vlan_header))
457 ifv->ifv_if.if_mtu = p->if_mtu;
459 ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN;
462 * Copy only a selected subset of flags from the parent.
463 * Other flags are none of our business.
465 ifv->ifv_if.if_flags = (p->if_flags &
466 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT));
469 * Set up our ``Ethernet address'' to reflect the underlying
470 * physical interface's.
472 ifa1 = ifaddr_byindex(ifv->ifv_if.if_index);
473 ifa2 = ifaddr_byindex(p->if_index);
474 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
475 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
476 sdl1->sdl_type = IFT_ETHER;
477 sdl1->sdl_alen = ETHER_ADDR_LEN;
478 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
479 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
482 * Configure multicast addresses that may already be
483 * joined on the vlan device.
485 (void)vlan_setmulti(&ifv->ifv_if);
491 vlan_unconfig(struct ifnet *ifp)
494 struct sockaddr_dl *sdl;
495 struct vlan_mc_entry *mc;
504 struct sockaddr_dl sdl;
507 * Since the interface is being unconfigured, we need to
508 * empty the list of multicast groups that we may have joined
509 * while we were alive from the parent's list.
511 bzero((char *)&sdl, sizeof sdl);
512 sdl.sdl_len = sizeof sdl;
513 sdl.sdl_family = AF_LINK;
514 sdl.sdl_index = p->if_index;
515 sdl.sdl_type = IFT_ETHER;
516 sdl.sdl_alen = ETHER_ADDR_LEN;
518 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
519 mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
520 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
521 error = if_delmulti(p, (struct sockaddr *)&sdl);
524 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
529 /* Disconnect from parent. */
531 ifv->ifv_if.if_mtu = ETHERMTU;
533 /* Clear our MAC address. */
534 ifa = ifaddr_byindex(ifv->ifv_if.if_index);
535 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
536 sdl->sdl_type = IFT_ETHER;
537 sdl->sdl_alen = ETHER_ADDR_LEN;
538 bzero(LLADDR(sdl), ETHER_ADDR_LEN);
539 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
545 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
554 ifr = (struct ifreq *)data;
555 ifa = (struct ifaddr *)data;
560 ifp->if_flags |= IFF_UP;
562 switch (ifa->ifa_addr->sa_family) {
565 arp_ifinit(&ifv->ifv_if, ifa);
577 sa = (struct sockaddr *) &ifr->ifr_data;
578 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
579 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
585 * Set the interface MTU.
586 * This is bogus. The underlying interface might support
589 if (ifr->ifr_mtu > ETHERMTU) {
592 ifp->if_mtu = ifr->ifr_mtu;
597 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
600 if (vlr.vlr_parent[0] == '\0') {
602 if (ifp->if_flags & IFF_UP) {
607 ifp->if_flags &= ~IFF_RUNNING;
610 p = ifunit(vlr.vlr_parent);
615 error = vlan_config(ifv, p);
618 ifv->ifv_tag = vlr.vlr_tag;
619 ifp->if_flags |= IFF_RUNNING;
623 bzero(&vlr, sizeof vlr);
625 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent),
626 "%s%d", ifv->ifv_p->if_name, ifv->ifv_p->if_unit);
627 vlr.vlr_tag = ifv->ifv_tag;
629 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
634 * We don't support promiscuous mode
635 * right now because it would require help from the
636 * underlying drivers, which hasn't been implemented.
638 if (ifr->ifr_flags & (IFF_PROMISC)) {
639 ifp->if_flags &= ~(IFF_PROMISC);
645 error = vlan_setmulti(ifp);