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
29 * $FreeBSD: src/sys/net/if_vlan.c,v 1.15.2.13 2003/02/14 22:25:58 fenner Exp $
30 * $DragonFly: src/sys/net/vlan/if_vlan.c,v 1.7 2004/03/14 15:36:54 joerg Exp $
34 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
35 * Might be extended some day to also handle IEEE 802.1p priority
36 * tagging. This is sort of sneaky in the implementation, since
37 * we need to pretend to be enough of an Ethernet implementation
38 * to make arp work. The way we do this is by telling everyone
39 * that we are an Ethernet, and then catch the packets that
40 * ether_output() left on our output queue queue when it calls
41 * if_start(), rewrite them for use by the real outgoing interface,
42 * and ask it to send them.
45 * XXX It's incorrect to assume that we must always kludge up
46 * headers on the physical device's behalf: some devices support
47 * VLAN tag insertion and extraction in firmware. For these cases,
48 * one can change the behavior of the vlan interface by setting
49 * the LINK0 flag on it (that is setting the vlan interface's LINK0
50 * flag, _not_ the parent's LINK0 flag; we try to leave the parent
51 * alone). If the interface has the LINK0 flag set, then it will
52 * not modify the ethernet header on output, because the parent
53 * can do that for itself. On input, the parent can call vlan_input_tag()
54 * directly in order to supply us with an incoming mbuf and the vlan
55 * tag value that goes with it.
63 #include <sys/param.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
67 #include <sys/module.h>
68 #include <sys/queue.h>
69 #include <sys/socket.h>
70 #include <sys/sockio.h>
71 #include <sys/sysctl.h>
72 #include <sys/systm.h>
73 #include <machine/bus.h> /* XXX: Shouldn't really be required! */
76 #include <net/ethernet.h>
78 #include <net/if_arp.h>
79 #include <net/if_dl.h>
80 #include <net/if_types.h>
81 #include "if_vlan_var.h"
84 #include <netinet/in.h>
85 #include <netinet/if_ether.h>
88 #define VLANNAME "vlan"
90 SYSCTL_DECL(_net_link);
91 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
92 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
94 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
95 static LIST_HEAD(, ifvlan) ifv_list;
97 static int vlan_clone_create(struct if_clone *, int);
98 static void vlan_clone_destroy(struct ifnet *);
99 static void vlan_start(struct ifnet *ifp);
100 static void vlan_ifinit(void *foo);
101 static int vlan_input(struct ether_header *eh, struct mbuf *m);
102 static int vlan_input_tag(struct ether_header *eh, struct mbuf *m,
104 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
105 static int vlan_setmulti(struct ifnet *ifp);
106 static int vlan_unconfig(struct ifnet *ifp);
107 static int vlan_config(struct ifvlan *ifv, struct ifnet *p);
109 struct if_clone vlan_cloner = IF_CLONE_INITIALIZER("vlan", vlan_clone_create,
110 vlan_clone_destroy, NVLAN, IF_MAXUNIT);
113 * Program our multicast filter. What we're actually doing is
114 * programming the multicast filter of the parent. This has the
115 * side effect of causing the parent interface to receive multicast
116 * traffic that it doesn't really want, which ends up being discarded
117 * later by the upper protocol layers. Unfortunately, there's no way
118 * to avoid this: there really is only one physical interface.
121 vlan_setmulti(struct ifnet *ifp)
124 struct ifmultiaddr *ifma, *rifma = NULL;
126 struct vlan_mc_entry *mc = NULL;
127 struct sockaddr_dl sdl;
130 /* Find the parent. */
135 * If we don't have a parent, just remember the membership for
141 bzero((char *)&sdl, sizeof sdl);
142 sdl.sdl_len = sizeof sdl;
143 sdl.sdl_family = AF_LINK;
144 sdl.sdl_index = ifp_p->if_index;
145 sdl.sdl_type = IFT_ETHER;
146 sdl.sdl_alen = ETHER_ADDR_LEN;
148 /* First, remove any existing filter entries. */
149 while(SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
150 mc = SLIST_FIRST(&sc->vlan_mc_listhead);
151 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
152 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
155 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
159 /* Now program new ones. */
160 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
161 if (ifma->ifma_addr->sa_family != AF_LINK)
163 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
164 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
165 (char *)&mc->mc_addr, ETHER_ADDR_LEN);
166 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
167 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
168 LLADDR(&sdl), ETHER_ADDR_LEN);
169 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
178 vlan_modevent(module_t mod, int type, void *data)
183 LIST_INIT(&ifv_list);
184 vlan_input_p = vlan_input;
185 vlan_input_tag_p = vlan_input_tag;
186 if_clone_attach(&vlan_cloner);
189 if_clone_detach(&vlan_cloner);
191 vlan_input_tag_p = NULL;
192 while (!LIST_EMPTY(&ifv_list))
193 vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
199 static moduledata_t vlan_mod = {
205 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
208 vlan_clone_create(struct if_clone *ifc, int unit)
214 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
216 SLIST_INIT(&ifv->vlan_mc_listhead);
219 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
223 if_initname(ifp, "vlan", unit);
224 /* NB: flags are not set here */
225 ifp->if_linkmib = &ifv->ifv_mib;
226 ifp->if_linkmiblen = sizeof ifv->ifv_mib;
227 /* NB: mtu is not set here */
229 ifp->if_init = vlan_ifinit;
230 ifp->if_start = vlan_start;
231 ifp->if_ioctl = vlan_ioctl;
232 ifp->if_output = ether_output;
233 ifp->if_snd.ifq_maxlen = ifqmaxlen;
234 ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr);
235 /* Now undo some of the damage... */
236 ifp->if_data.ifi_type = IFT_L2VLAN;
237 ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
243 vlan_clone_destroy(struct ifnet *ifp)
245 struct ifvlan *ifv = ifp->if_softc;
249 LIST_REMOVE(ifv, ifv_list);
259 vlan_ifinit(void *foo)
265 vlan_start(struct ifnet *ifp)
269 struct ether_vlan_header *evl;
275 ifp->if_flags |= IFF_OACTIVE;
277 IF_DEQUEUE(&ifp->if_snd, m);
284 * Do not run parent's if_start() if the parent is not up,
285 * or parent's driver will cause a system crash.
287 if ((p->if_flags & (IFF_UP | IFF_RUNNING)) !=
288 (IFF_UP | IFF_RUNNING)) {
290 ifp->if_data.ifi_collisions++;
295 * If the LINK0 flag is set, it means the underlying interface
296 * can do VLAN tag insertion itself and doesn't require us to
297 * create a special header for it. In this case, we just pass
298 * the packet along. However, we need some way to tell the
299 * interface where the packet came from so that it knows how
300 * to find the VLAN tag to use, so we set the rcvif in the
301 * mbuf header to our ifnet.
303 * Note: we also set the M_PROTO1 flag in the mbuf to let
304 * the parent driver know that the rcvif pointer is really
305 * valid. We need to do this because sometimes mbufs will
306 * be allocated by other parts of the system that contain
307 * garbage in the rcvif pointer. Using the M_PROTO1 flag
308 * lets the driver perform a proper sanity check and avoid
309 * following potentially bogus rcvif pointers off into
312 if (ifp->if_flags & IFF_LINK0) {
313 m->m_pkthdr.rcvif = ifp;
314 m->m_flags |= M_PROTO1;
316 M_PREPEND(m, EVL_ENCAPLEN, M_DONTWAIT);
318 printf("%s: M_PREPEND failed", ifp->if_xname);
322 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
324 m = m_pullup(m, ETHER_HDR_LEN + EVL_ENCAPLEN);
326 printf("%s: m_pullup failed", ifp->if_xname);
332 * Transform the Ethernet header into an Ethernet header
333 * with 802.1Q encapsulation.
335 bcopy(mtod(m, char *) + EVL_ENCAPLEN, mtod(m, char *),
336 sizeof(struct ether_header));
337 evl = mtod(m, struct ether_vlan_header *);
338 evl->evl_proto = evl->evl_encap_proto;
339 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
340 evl->evl_tag = htons(ifv->ifv_tag);
342 printf("vlan_start: %*D\n", sizeof *evl,
343 (unsigned char *)evl, ":");
348 * Send it, precisely as ether_output() would have.
349 * We are already running at splimp.
351 if (IF_QFULL(&p->if_snd)) {
358 IF_ENQUEUE(&p->if_snd, m);
360 p->if_obytes += m->m_pkthdr.len;
361 if (m->m_flags & M_MCAST)
363 if ((p->if_flags & IFF_OACTIVE) == 0)
366 ifp->if_flags &= ~IFF_OACTIVE;
372 vlan_input_tag(struct ether_header *eh, struct mbuf *m, u_int16_t t)
377 * Fake up a header and send the packet to the physical interface's
380 if (m->m_pkthdr.rcvif->if_bpf != NULL) {
382 struct ether_vlan_header evh;
384 bcopy(eh, &evh, 2*ETHER_ADDR_LEN);
385 evh.evl_encap_proto = htons(ETHERTYPE_VLAN);
386 evh.evl_tag = htons(t);
387 evh.evl_proto = eh->ether_type;
389 /* This kludge is OK; BPF treats the "mbuf" as read-only */
391 mh.mh_data = (char *)&evh;
392 mh.mh_len = ETHER_HDR_LEN + EVL_ENCAPLEN;
393 bpf_mtap(m->m_pkthdr.rcvif, (struct mbuf *)&mh);
396 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
397 ifv = LIST_NEXT(ifv, ifv_list)) {
398 if (m->m_pkthdr.rcvif == ifv->ifv_p
399 && ifv->ifv_tag == t)
403 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
405 return -1; /* So the parent can take note */
409 * Having found a valid vlan interface corresponding to
410 * the given source interface and vlan tag, run the
411 * the real packet through ether_input().
413 m->m_pkthdr.rcvif = &ifv->ifv_if;
415 ifv->ifv_if.if_ipackets++;
416 ether_input(&ifv->ifv_if, eh, m);
421 vlan_input(struct ether_header *eh, struct mbuf *m)
425 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
426 ifv = LIST_NEXT(ifv, ifv_list)) {
427 if (m->m_pkthdr.rcvif == ifv->ifv_p
428 && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *)))
433 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
434 m->m_pkthdr.rcvif->if_noproto++;
436 return -1; /* so ether_input can take note */
440 * Having found a valid vlan interface corresponding to
441 * the given source interface and vlan tag, remove the
442 * encapsulation, and run the real packet through
443 * ether_input() a second time (it had better be
446 m->m_pkthdr.rcvif = &ifv->ifv_if;
447 eh->ether_type = mtod(m, u_int16_t *)[1];
448 m->m_data += EVL_ENCAPLEN;
449 m->m_len -= EVL_ENCAPLEN;
450 m->m_pkthdr.len -= EVL_ENCAPLEN;
452 ifv->ifv_if.if_ipackets++;
453 ether_input(&ifv->ifv_if, eh, m);
458 vlan_config(struct ifvlan *ifv, struct ifnet *p)
460 struct ifaddr *ifa1, *ifa2;
461 struct sockaddr_dl *sdl1, *sdl2;
463 if (p->if_data.ifi_type != IFT_ETHER)
464 return EPROTONOSUPPORT;
468 if (p->if_data.ifi_hdrlen == sizeof(struct ether_vlan_header))
469 ifv->ifv_if.if_mtu = p->if_mtu;
471 ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN;
474 * Copy only a selected subset of flags from the parent.
475 * Other flags are none of our business.
477 ifv->ifv_if.if_flags = (p->if_flags &
478 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT));
481 * Set up our ``Ethernet address'' to reflect the underlying
482 * physical interface's.
484 ifa1 = ifnet_addrs[ifv->ifv_if.if_index - 1];
485 ifa2 = ifnet_addrs[p->if_index - 1];
486 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
487 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
488 sdl1->sdl_type = IFT_ETHER;
489 sdl1->sdl_alen = ETHER_ADDR_LEN;
490 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
491 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
494 * Configure multicast addresses that may already be
495 * joined on the vlan device.
497 (void)vlan_setmulti(&ifv->ifv_if);
503 vlan_unconfig(struct ifnet *ifp)
506 struct sockaddr_dl *sdl;
507 struct vlan_mc_entry *mc;
516 struct sockaddr_dl sdl;
519 * Since the interface is being unconfigured, we need to
520 * empty the list of multicast groups that we may have joined
521 * while we were alive from the parent's list.
523 bzero((char *)&sdl, sizeof sdl);
524 sdl.sdl_len = sizeof sdl;
525 sdl.sdl_family = AF_LINK;
526 sdl.sdl_index = p->if_index;
527 sdl.sdl_type = IFT_ETHER;
528 sdl.sdl_alen = ETHER_ADDR_LEN;
530 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
531 mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
532 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
533 error = if_delmulti(p, (struct sockaddr *)&sdl);
536 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
541 /* Disconnect from parent. */
543 ifv->ifv_if.if_mtu = ETHERMTU;
545 /* Clear our MAC address. */
546 ifa = ifnet_addrs[ifv->ifv_if.if_index - 1];
547 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
548 sdl->sdl_type = IFT_ETHER;
549 sdl->sdl_alen = ETHER_ADDR_LEN;
550 bzero(LLADDR(sdl), ETHER_ADDR_LEN);
551 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
557 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
566 ifr = (struct ifreq *)data;
567 ifa = (struct ifaddr *)data;
572 ifp->if_flags |= IFF_UP;
574 switch (ifa->ifa_addr->sa_family) {
577 arp_ifinit(&ifv->ifv_if, ifa);
589 sa = (struct sockaddr *) &ifr->ifr_data;
590 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
591 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
596 if (ifv->ifv_p != NULL) {
597 error = (ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCGIFMEDIA, data);
598 /* Limit the result to the parent's current config. */
600 struct ifmediareq *ifmr;
602 ifmr = (struct ifmediareq *) data;
603 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
605 error = copyout(&ifmr->ifm_current,
620 * Set the interface MTU.
621 * This is bogus. The underlying interface might support
624 if (ifr->ifr_mtu > ETHERMTU) {
627 ifp->if_mtu = ifr->ifr_mtu;
632 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
635 if (vlr.vlr_parent[0] == '\0') {
637 if (ifp->if_flags & IFF_UP) {
642 ifp->if_flags &= ~IFF_RUNNING;
645 p = ifunit(vlr.vlr_parent);
650 error = vlan_config(ifv, p);
653 ifv->ifv_tag = vlr.vlr_tag;
654 ifp->if_flags |= IFF_RUNNING;
658 bzero(&vlr, sizeof vlr);
660 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
661 sizeof(vlr.vlr_parent));
662 vlr.vlr_tag = ifv->ifv_tag;
664 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
669 * We don't support promiscuous mode
670 * right now because it would require help from the
671 * underlying drivers, which hasn't been implemented.
673 if (ifr->ifr_flags & (IFF_PROMISC)) {
674 ifp->if_flags &= ~(IFF_PROMISC);
680 error = vlan_setmulti(ifp);