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.29 2008/03/10 11:44:57 sephe 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/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
68 #include <sys/module.h>
69 #include <sys/queue.h>
70 #include <sys/socket.h>
71 #include <sys/sockio.h>
72 #include <sys/sysctl.h>
74 #include <sys/thread2.h>
77 #include <net/ethernet.h>
79 #include <net/if_arp.h>
80 #include <net/if_dl.h>
81 #include <net/if_types.h>
82 #include <net/ifq_var.h>
83 #include <net/if_clone.h>
84 #include <net/netmsg2.h>
87 #include <netinet/in.h>
88 #include <netinet/if_ether.h>
91 #include <net/vlan/if_vlan_var.h>
92 #include <net/vlan/if_vlan_ether.h>
94 struct vlan_mc_entry {
95 struct ether_addr mc_addr;
96 SLIST_ENTRY(vlan_mc_entry) mc_entries;
100 struct arpcom ifv_ac; /* make this an interface */
101 struct ifnet *ifv_p; /* parent inteface of this vlan */
104 u_int16_t ifvm_proto; /* encapsulation ethertype */
105 u_int16_t ifvm_tag; /* tag to apply on packets leaving if */
107 SLIST_HEAD(__vlan_mchead, vlan_mc_entry) vlan_mc_listhead;
108 LIST_ENTRY(ifvlan) ifv_list;
109 struct resource *r_unit; /* resource allocated for this unit */
111 #define ifv_if ifv_ac.ac_if
112 #define ifv_tag ifv_mib.ifvm_tag
114 #define VLANNAME "vlan"
116 SYSCTL_DECL(_net_link);
117 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
118 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
120 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
121 static LIST_HEAD(, ifvlan) ifv_list;
123 static int vlan_clone_create(struct if_clone *, int);
124 static void vlan_clone_destroy(struct ifnet *);
125 static void vlan_start(struct ifnet *ifp);
126 static void vlan_ifinit(void *foo);
127 static int vlan_input(const struct ether_header *eh, struct mbuf *m);
128 static int vlan_input_tag(struct mbuf *m, uint16_t t);
129 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr,
131 static int vlan_setmulti(struct ifnet *ifp);
132 static int vlan_unconfig(struct ifnet *ifp);
133 static int vlan_config(struct ifvlan *ifv, struct ifnet *p);
135 struct if_clone vlan_cloner = IF_CLONE_INITIALIZER("vlan", vlan_clone_create,
136 vlan_clone_destroy, NVLAN, IF_MAXUNIT);
139 * Program our multicast filter. What we're actually doing is
140 * programming the multicast filter of the parent. This has the
141 * side effect of causing the parent interface to receive multicast
142 * traffic that it doesn't really want, which ends up being discarded
143 * later by the upper protocol layers. Unfortunately, there's no way
144 * to avoid this: there really is only one physical interface.
147 vlan_setmulti(struct ifnet *ifp)
150 struct ifmultiaddr *ifma, *rifma = NULL;
152 struct vlan_mc_entry *mc = NULL;
153 struct sockaddr_dl sdl;
156 /* Find the parent. */
161 * If we don't have a parent, just remember the membership for
167 bzero((char *)&sdl, sizeof sdl);
168 sdl.sdl_len = sizeof sdl;
169 sdl.sdl_family = AF_LINK;
170 sdl.sdl_index = ifp_p->if_index;
171 sdl.sdl_type = IFT_ETHER;
172 sdl.sdl_alen = ETHER_ADDR_LEN;
174 /* First, remove any existing filter entries. */
175 while(SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
176 mc = SLIST_FIRST(&sc->vlan_mc_listhead);
177 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
178 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
181 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
185 /* Now program new ones. */
186 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
187 if (ifma->ifma_addr->sa_family != AF_LINK)
189 mc = kmalloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
190 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
191 (char *)&mc->mc_addr, ETHER_ADDR_LEN);
192 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
193 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
194 LLADDR(&sdl), ETHER_ADDR_LEN);
195 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
204 vlan_modevent(module_t mod, int type, void *data)
209 LIST_INIT(&ifv_list);
210 vlan_input_p = vlan_input;
211 vlan_input_tag_p = vlan_input_tag;
212 if_clone_attach(&vlan_cloner);
215 if_clone_detach(&vlan_cloner);
217 vlan_input_tag_p = NULL;
218 while (!LIST_EMPTY(&ifv_list))
219 vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
225 static moduledata_t vlan_mod = {
231 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
234 vlan_clone_create(struct if_clone *ifc, int unit)
239 ifv = kmalloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
241 SLIST_INIT(&ifv->vlan_mc_listhead);
244 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
248 if_initname(ifp, "vlan", unit);
249 /* NB: flags are not set here */
250 ifp->if_linkmib = &ifv->ifv_mib;
251 ifp->if_linkmiblen = sizeof ifv->ifv_mib;
252 /* NB: mtu is not set here */
254 ifp->if_init = vlan_ifinit;
255 ifp->if_start = vlan_start;
256 ifp->if_ioctl = vlan_ioctl;
257 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
258 ifq_set_ready(&ifp->if_snd);
259 ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr, NULL);
260 /* Now undo some of the damage... */
261 ifp->if_data.ifi_type = IFT_L2VLAN;
262 ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
268 vlan_clone_destroy(struct ifnet *ifp)
270 struct ifvlan *ifv = ifp->if_softc;
274 LIST_REMOVE(ifv, ifv_list);
284 vlan_ifinit(void *foo)
290 vlan_start(struct ifnet *ifp)
299 ifp->if_flags |= IFF_OACTIVE;
301 struct netmsg_packet *nmp;
303 struct lwkt_port *port;
305 m = ifq_dequeue(&ifp->if_snd, NULL);
311 * Do not run parent's if_start() if the parent is not up,
312 * or parent's driver will cause a system crash.
314 if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) !=
315 (IFF_UP | IFF_RUNNING)) {
317 ifp->if_data.ifi_collisions++;
322 * We need some way to tell the interface where the packet
323 * came from so that it knows how to find the VLAN tag to
324 * use, so we set the ether_vlantag in the mbuf packet header
325 * to our vlan tag. We also set the M_VLANTAG flag in the
326 * mbuf to let the parent driver know that the ether_vlantag
329 m->m_pkthdr.ether_vlantag = ifv->ifv_tag;
330 m->m_flags |= M_VLANTAG;
332 nmp = &m->m_hdr.mh_netmsg;
333 nmsg = &nmp->nm_netmsg;
335 netmsg_init(nmsg, &netisr_apanic_rport, 0, vlan_start_dispatch);
337 nmsg->nm_lmsg.u.ms_resultp = ifp_p;
339 port = cpu_portfn(ifp_p->if_index % ncpus /* XXX */);
340 lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
343 ifp->if_flags &= ~IFF_OACTIVE;
347 vlan_input_tag(struct mbuf *m, uint16_t t)
353 rcvif = m->m_pkthdr.rcvif;
355 ASSERT_SERIALIZED(rcvif->if_serializer);
358 * Fake up a header and send the packet to the physical interface's
361 if ((bif = rcvif->if_bpf) != NULL)
362 vlan_ether_ptap(bif, m, t);
364 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
365 ifv = LIST_NEXT(ifv, ifv_list)) {
366 if (rcvif == ifv->ifv_p && ifv->ifv_tag == t)
370 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
372 return -1; /* So the parent can take note */
376 * Having found a valid vlan interface corresponding to
377 * the given source interface and vlan tag, run the
378 * the real packet through ether_input().
380 m->m_pkthdr.rcvif = &ifv->ifv_if;
382 ifv->ifv_if.if_ipackets++;
383 lwkt_serialize_exit(rcvif->if_serializer);
384 lwkt_serialize_enter(ifv->ifv_if.if_serializer);
385 ether_input(&ifv->ifv_if, m);
386 lwkt_serialize_exit(ifv->ifv_if.if_serializer);
387 lwkt_serialize_enter(rcvif->if_serializer);
392 vlan_input(const struct ether_header *eh, struct mbuf *m)
396 struct ether_header eh_copy;
398 rcvif = m->m_pkthdr.rcvif;
399 ASSERT_SERIALIZED(rcvif->if_serializer);
401 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
402 ifv = LIST_NEXT(ifv, ifv_list)) {
403 if (rcvif == ifv->ifv_p
404 && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *)))
409 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
412 return -1; /* so ether_input can take note */
416 * Having found a valid vlan interface corresponding to
417 * the given source interface and vlan tag, remove the
418 * remaining encapsulation (ether_vlan_header minus the ether_header
419 * that had already been removed) and run the real packet
420 * through ether_input() a second time (it had better be
424 eh_copy.ether_type = mtod(m, u_int16_t *)[1]; /* evl_proto */
425 m->m_pkthdr.rcvif = &ifv->ifv_if;
426 m_adj(m, EVL_ENCAPLEN);
427 M_PREPEND(m, ETHER_HDR_LEN, MB_WAIT);
428 *(struct ether_header *)mtod(m, void *) = eh_copy;
430 ifv->ifv_if.if_ipackets++;
431 lwkt_serialize_exit(rcvif->if_serializer);
432 lwkt_serialize_enter(ifv->ifv_if.if_serializer);
433 ether_input(&ifv->ifv_if, m);
434 lwkt_serialize_exit(ifv->ifv_if.if_serializer);
435 lwkt_serialize_enter(rcvif->if_serializer);
440 vlan_config(struct ifvlan *ifv, struct ifnet *p)
442 struct sockaddr_dl *sdl1, *sdl2;
444 if (p->if_data.ifi_type != IFT_ETHER)
445 return EPROTONOSUPPORT;
449 if (p->if_capenable & IFCAP_VLAN_MTU)
450 ifv->ifv_if.if_mtu = p->if_mtu;
452 ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN;
455 * Copy only a selected subset of flags from the parent.
456 * Other flags are none of our business.
458 ifv->ifv_if.if_flags = (p->if_flags &
459 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT));
462 * Set up our ``Ethernet address'' to reflect the underlying
463 * physical interface's.
465 sdl1 = IF_LLSOCKADDR(&ifv->ifv_if);
466 sdl2 = IF_LLSOCKADDR(p);
467 sdl1->sdl_type = IFT_ETHER;
468 sdl1->sdl_alen = ETHER_ADDR_LEN;
469 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
470 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
473 * Configure multicast addresses that may already be
474 * joined on the vlan device.
476 vlan_setmulti(&ifv->ifv_if);
482 vlan_unconfig(struct ifnet *ifp)
484 struct sockaddr_dl *sdl;
485 struct vlan_mc_entry *mc;
494 struct sockaddr_dl sdl;
497 * Since the interface is being unconfigured, we need to
498 * empty the list of multicast groups that we may have joined
499 * while we were alive from the parent's list.
501 bzero((char *)&sdl, sizeof sdl);
502 sdl.sdl_len = sizeof sdl;
503 sdl.sdl_family = AF_LINK;
504 sdl.sdl_index = p->if_index;
505 sdl.sdl_type = IFT_ETHER;
506 sdl.sdl_alen = ETHER_ADDR_LEN;
508 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
509 mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
510 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
511 error = if_delmulti(p, (struct sockaddr *)&sdl);
514 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
519 /* Disconnect from parent. */
521 ifv->ifv_if.if_mtu = ETHERMTU;
523 /* Clear our MAC address. */
524 sdl = IF_LLSOCKADDR(&ifv->ifv_if);
525 sdl->sdl_type = IFT_ETHER;
526 sdl->sdl_alen = ETHER_ADDR_LEN;
527 bzero(LLADDR(sdl), ETHER_ADDR_LEN);
528 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
534 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
543 ifr = (struct ifreq *)data;
544 ifa = (struct ifaddr *)data;
547 ASSERT_SERIALIZED(ifp->if_serializer);
552 if (ifv->ifv_p != NULL) {
553 lwkt_serialize_exit(ifp->if_serializer);
554 lwkt_serialize_enter(ifv->ifv_p->if_serializer);
555 error = ifv->ifv_p->if_ioctl(ifv->ifv_p,
556 SIOCGIFMEDIA, data, cr);
557 lwkt_serialize_exit(ifv->ifv_p->if_serializer);
558 lwkt_serialize_enter(ifp->if_serializer);
559 /* Limit the result to the parent's current config. */
561 struct ifmediareq *ifmr;
563 ifmr = (struct ifmediareq *) data;
564 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
566 error = copyout(&ifmr->ifm_current,
580 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
583 if (vlr.vlr_parent[0] == '\0') {
585 if (ifp->if_flags & IFF_UP)
587 ifp->if_flags &= ~IFF_RUNNING;
590 p = ifunit(vlr.vlr_parent);
595 error = vlan_config(ifv, p);
598 ifv->ifv_tag = vlr.vlr_tag;
599 ifp->if_flags |= IFF_RUNNING;
603 bzero(&vlr, sizeof vlr);
605 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
606 sizeof(vlr.vlr_parent));
607 vlr.vlr_tag = ifv->ifv_tag;
609 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
614 * We don't support promiscuous mode
615 * right now because it would require help from the
616 * underlying drivers, which hasn't been implemented.
618 if (ifr->ifr_flags & (IFF_PROMISC)) {
619 ifp->if_flags &= ~(IFF_PROMISC);
626 error = vlan_setmulti(ifp);
630 error = ether_ioctl(ifp, cmd, data);