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.
47 #include <sys/param.h>
48 #include <sys/kernel.h>
50 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <sys/rwlock.h>
54 #include <sys/queue.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/systm.h>
61 #include <net/ethernet.h>
63 #include <net/if_clone.h>
64 #include <net/if_arp.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/if_vlan_var.h>
70 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
74 #define VLANNAME "vlan"
75 #define VLAN_DEF_HWIDTH 4
76 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
78 LIST_HEAD(ifvlanhead, ifvlan);
81 struct ifnet *parent; /* parent interface of this trunk */
84 struct ifvlan *vlans[EVL_VLID_MASK+1]; /* static table */
86 struct ifvlanhead *hash; /* dynamic hash-list table */
91 LIST_ENTRY(ifvlantrunk) trunk_entry;
93 static LIST_HEAD(, ifvlantrunk) trunk_list;
95 struct vlan_mc_entry {
96 struct ether_addr mc_addr;
97 SLIST_ENTRY(vlan_mc_entry) mc_entries;
101 struct ifvlantrunk *ifv_trunk;
102 struct ifnet *ifv_ifp;
103 #define TRUNK(ifv) ((ifv)->ifv_trunk)
104 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
105 int ifv_pflags; /* special flags we have set on parent */
108 int ifvm_encaplen; /* encapsulation length */
109 int ifvm_mtufudge; /* MTU fudged by this much */
110 int ifvm_mintu; /* min transmission unit */
111 uint16_t ifvm_proto; /* encapsulation ethertype */
112 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
114 SLIST_HEAD(__vlan_mchead, vlan_mc_entry) vlan_mc_listhead;
115 LIST_ENTRY(ifvlan) ifv_list;
117 #define ifv_tag ifv_mib.ifvm_tag
118 #define ifv_encaplen ifv_mib.ifvm_encaplen
119 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
120 #define ifv_mintu ifv_mib.ifvm_mintu
122 /* Special flags we should propagate to parent. */
125 int (*func)(struct ifnet *, int);
127 {IFF_PROMISC, ifpromisc},
128 {IFF_ALLMULTI, if_allmulti},
132 SYSCTL_DECL(_net_link);
133 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
134 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
136 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
139 * We have a global mutex, that is used to serialize configuration
140 * changes and isn't used in normal packet delivery.
142 * We also have a per-trunk rwlock, that is locked shared on packet
143 * processing and exclusive when configuration is changed.
145 * The VLAN_ARRAY substitutes the dynamic hash with a static array
146 * with 4096 entries. In theory this can give a boots in processing,
147 * however on practice it does not. Probably this is because array
148 * is too big to fit into CPU cache.
150 static struct mtx ifv_mtx;
151 #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
152 #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx)
153 #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED)
154 #define VLAN_LOCK() mtx_lock(&ifv_mtx)
155 #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx)
156 #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME)
157 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
158 #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw)
159 #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw)
160 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
161 #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw)
162 #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw)
163 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
166 static void vlan_inithash(struct ifvlantrunk *trunk);
167 static void vlan_freehash(struct ifvlantrunk *trunk);
168 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
169 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
170 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
171 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
174 static void trunk_destroy(struct ifvlantrunk *trunk);
176 static void vlan_start(struct ifnet *ifp);
177 static void vlan_ifinit(void *foo);
178 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
179 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
180 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
181 int (*func)(struct ifnet *, int));
182 static int vlan_setflags(struct ifnet *ifp, int status);
183 static int vlan_setmulti(struct ifnet *ifp);
184 static int vlan_unconfig(struct ifnet *ifp);
185 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
186 static void vlan_link_state(struct ifnet *ifp, int link);
187 static void vlan_capabilities(struct ifvlan *ifv);
188 static void vlan_trunk_capabilities(struct ifnet *ifp);
190 static struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
191 const char *, int *);
192 static int vlan_clone_match(struct if_clone *, const char *);
193 static int vlan_clone_create(struct if_clone *, char *, size_t);
194 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
196 static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
197 IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
200 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
202 vlan_inithash(struct ifvlantrunk *trunk)
207 * The trunk must not be locked here since we call malloc(M_WAITOK).
208 * It is OK in case this function is called before the trunk struct
209 * gets hooked up and becomes visible from other threads.
212 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
213 ("%s: hash already initialized", __func__));
215 trunk->hwidth = VLAN_DEF_HWIDTH;
216 n = 1 << trunk->hwidth;
217 trunk->hmask = n - 1;
218 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
219 for (i = 0; i < n; i++)
220 LIST_INIT(&trunk->hash[i]);
224 vlan_freehash(struct ifvlantrunk *trunk)
229 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
230 for (i = 0; i < (1 << trunk->hwidth); i++)
231 KASSERT(LIST_EMPTY(&trunk->hash[i]),
232 ("%s: hash table not empty", __func__));
234 free(trunk->hash, M_VLAN);
236 trunk->hwidth = trunk->hmask = 0;
240 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
245 TRUNK_LOCK_ASSERT(trunk);
246 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
248 b = 1 << trunk->hwidth;
249 i = HASH(ifv->ifv_tag, trunk->hmask);
250 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
251 if (ifv->ifv_tag == ifv2->ifv_tag)
255 * Grow the hash when the number of vlans exceeds half of the number of
256 * hash buckets squared. This will make the average linked-list length
259 if (trunk->refcnt > (b * b) / 2) {
260 vlan_growhash(trunk, 1);
261 i = HASH(ifv->ifv_tag, trunk->hmask);
263 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
270 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
275 TRUNK_LOCK_ASSERT(trunk);
276 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
278 b = 1 << trunk->hwidth;
279 i = HASH(ifv->ifv_tag, trunk->hmask);
280 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
283 LIST_REMOVE(ifv2, ifv_list);
284 if (trunk->refcnt < (b * b) / 2)
285 vlan_growhash(trunk, -1);
289 panic("%s: vlan not found\n", __func__);
290 return (ENOENT); /*NOTREACHED*/
294 * Grow the hash larger or smaller if memory permits.
297 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
301 struct ifvlanhead *hash2;
302 int hwidth2, i, j, n, n2;
304 TRUNK_LOCK_ASSERT(trunk);
305 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
308 /* Harmless yet obvious coding error */
309 printf("%s: howmuch is 0\n", __func__);
313 hwidth2 = trunk->hwidth + howmuch;
314 n = 1 << trunk->hwidth;
316 /* Do not shrink the table below the default */
317 if (hwidth2 < VLAN_DEF_HWIDTH)
320 /* M_NOWAIT because we're called with trunk mutex held */
321 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
323 printf("%s: out of memory -- hash size not changed\n",
325 return; /* We can live with the old hash table */
327 for (j = 0; j < n2; j++)
328 LIST_INIT(&hash2[j]);
329 for (i = 0; i < n; i++)
330 while (!LIST_EMPTY(&trunk->hash[i])) {
331 ifv = LIST_FIRST(&trunk->hash[i]);
332 LIST_REMOVE(ifv, ifv_list);
333 j = HASH(ifv->ifv_tag, n2 - 1);
334 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
336 free(trunk->hash, M_VLAN);
338 trunk->hwidth = hwidth2;
339 trunk->hmask = n2 - 1;
342 static __inline struct ifvlan *
343 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
347 TRUNK_LOCK_RASSERT(trunk);
349 LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
350 if (ifv->ifv_tag == tag)
356 /* Debugging code to view the hashtables. */
358 vlan_dumphash(struct ifvlantrunk *trunk)
363 for (i = 0; i < (1 << trunk->hwidth); i++) {
365 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
366 printf("%s ", ifv->ifv_ifp->if_xname);
371 #endif /* !VLAN_ARRAY */
374 trunk_destroy(struct ifvlantrunk *trunk)
380 vlan_freehash(trunk);
382 TRUNK_LOCK_DESTROY(trunk);
383 LIST_REMOVE(trunk, trunk_entry);
384 trunk->parent->if_vlantrunk = NULL;
389 * Program our multicast filter. What we're actually doing is
390 * programming the multicast filter of the parent. This has the
391 * side effect of causing the parent interface to receive multicast
392 * traffic that it doesn't really want, which ends up being discarded
393 * later by the upper protocol layers. Unfortunately, there's no way
394 * to avoid this: there really is only one physical interface.
396 * XXX: There is a possible race here if more than one thread is
397 * modifying the multicast state of the vlan interface at the same time.
400 vlan_setmulti(struct ifnet *ifp)
403 struct ifmultiaddr *ifma, *rifma = NULL;
405 struct vlan_mc_entry *mc = NULL;
406 struct sockaddr_dl sdl;
409 /*VLAN_LOCK_ASSERT();*/
411 /* Find the parent. */
415 bzero((char *)&sdl, sizeof(sdl));
416 sdl.sdl_len = sizeof(sdl);
417 sdl.sdl_family = AF_LINK;
418 sdl.sdl_index = ifp_p->if_index;
419 sdl.sdl_type = IFT_ETHER;
420 sdl.sdl_alen = ETHER_ADDR_LEN;
422 /* First, remove any existing filter entries. */
423 while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
424 mc = SLIST_FIRST(&sc->vlan_mc_listhead);
425 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
426 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
429 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
433 /* Now program new ones. */
434 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
435 if (ifma->ifma_addr->sa_family != AF_LINK)
437 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
440 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
441 (char *)&mc->mc_addr, ETHER_ADDR_LEN);
442 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
443 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
444 LLADDR(&sdl), ETHER_ADDR_LEN);
445 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
454 * VLAN support can be loaded as a module. The only place in the
455 * system that's intimately aware of this is ether_input. We hook
456 * into this code through vlan_input_p which is defined there and
457 * set here. Noone else in the system should be aware of this so
458 * we use an explicit reference here.
460 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
462 /* For if_link_state_change() eyes only... */
463 extern void (*vlan_link_state_p)(struct ifnet *, int);
466 vlan_modevent(module_t mod, int type, void *data)
471 LIST_INIT(&trunk_list);
473 vlan_input_p = vlan_input;
474 vlan_link_state_p = vlan_link_state;
475 vlan_trunk_cap_p = vlan_trunk_capabilities;
476 if_clone_attach(&vlan_cloner);
480 struct ifvlantrunk *trunk, *trunk1;
482 if_clone_detach(&vlan_cloner);
484 vlan_link_state_p = NULL;
485 vlan_trunk_cap_p = NULL;
487 LIST_FOREACH_SAFE(trunk, &trunk_list, trunk_entry, trunk1)
488 trunk_destroy(trunk);
499 static moduledata_t vlan_mod = {
505 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
506 MODULE_VERSION(if_vlan, 1);
507 MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
509 static struct ifnet *
510 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
516 /* Check for <etherif>.<vlan> style interface names. */
518 TAILQ_FOREACH(ifp, &ifnet, if_link) {
519 if (ifp->if_type != IFT_ETHER)
521 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
523 cp = name + strlen(ifp->if_xname);
526 for(; *cp != '\0'; cp++) {
527 if (*cp < '0' || *cp > '9')
529 t = (t * 10) + (*cp - '0');
541 vlan_clone_match(struct if_clone *ifc, const char *name)
545 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
548 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
550 for (cp = name + 4; *cp != '\0'; cp++) {
551 if (*cp < '0' || *cp > '9')
559 vlan_clone_create(struct if_clone *ifc, char *name, size_t len)
570 u_char eaddr[6] = {0,0,0,0,0,0};
572 if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
578 * Don't let the caller set up a VLAN tag with
579 * anything except VLID bits.
581 if (tag & ~EVL_VLID_MASK)
586 error = ifc_name2unit(name, &unit);
590 wildcard = (unit < 0);
593 error = ifc_alloc_unit(ifc, &unit);
597 /* In the wildcard case, we need to update the name. */
599 for (dp = name; *dp != '\0'; dp++);
600 if (snprintf(dp, len - (dp-name), "%d", unit) >
601 len - (dp-name) - 1) {
602 panic("%s: interface name too long", __func__);
606 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
607 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
609 ifc_free_unit(ifc, unit);
613 SLIST_INIT(&ifv->vlan_mc_listhead);
617 * Set the name manually rather than using if_initname because
618 * we don't conform to the default naming convention for interfaces.
620 strlcpy(ifp->if_xname, name, IFNAMSIZ);
621 ifp->if_dname = ifc->ifc_name;
622 ifp->if_dunit = unit;
623 /* NB: flags are not set here */
624 ifp->if_linkmib = &ifv->ifv_mib;
625 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
626 /* NB: mtu is not set here */
628 ifp->if_init = vlan_ifinit;
629 ifp->if_start = vlan_start;
630 ifp->if_ioctl = vlan_ioctl;
631 ifp->if_snd.ifq_maxlen = ifqmaxlen;
632 ifp->if_flags = VLAN_IFFLAGS;
633 ether_ifattach(ifp, eaddr);
634 /* Now undo some of the damage... */
635 ifp->if_baudrate = 0;
636 ifp->if_type = IFT_L2VLAN;
637 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
640 error = vlan_config(ifv, p, tag);
643 * Since we've partialy failed, we need to back
644 * out all the way, otherwise userland could get
645 * confused. Thus, we destroy the interface.
649 if_free_type(ifp, IFT_ETHER);
654 ifp->if_drv_flags |= IFF_DRV_RUNNING;
656 /* Update flags on the parent, if necessary. */
657 vlan_setflags(ifp, 1);
664 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
667 struct ifvlan *ifv = ifp->if_softc;
669 unit = ifp->if_dunit;
674 if_free_type(ifp, IFT_ETHER);
678 ifc_free_unit(ifc, unit);
684 * The ifp->if_init entry point for vlan(4) is a no-op.
687 vlan_ifinit(void *foo)
693 * The if_start method for vlan(4) interface. It doesn't
694 * raises the IFF_DRV_OACTIVE flag, since it is called
695 * only from IFQ_HANDOFF() macro in ether_output_frame().
696 * If the interface queue is full, and vlan_start() is
697 * not called, the queue would never get emptied and
698 * interface would stall forever.
701 vlan_start(struct ifnet *ifp)
712 IF_DEQUEUE(&ifp->if_snd, m);
718 * Do not run parent's if_start() if the parent is not up,
719 * or parent's driver will cause a system crash.
721 if (!((p->if_flags & IFF_UP) &&
722 (p->if_drv_flags & IFF_DRV_RUNNING))) {
724 ifp->if_collisions++;
729 * If underlying interface can do VLAN tag insertion itself,
730 * just pass the packet along. However, we need some way to
731 * tell the interface where the packet came from so that it
732 * knows how to find the VLAN tag to use, so we attach a
733 * packet tag that holds it.
735 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
736 struct m_tag *mtag = (struct m_tag *)
737 uma_zalloc(zone_mtag_vlan, M_NOWAIT);
743 VLAN_TAG_VALUE(mtag) = ifv->ifv_tag;
744 m_tag_prepend(m, mtag);
745 m->m_flags |= M_VLANTAG;
747 struct ether_vlan_header *evl;
749 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
752 "unable to prepend VLAN header\n");
756 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
758 if (m->m_len < sizeof(*evl)) {
759 m = m_pullup(m, sizeof(*evl));
762 "cannot pullup VLAN header\n");
769 * Transform the Ethernet header into an Ethernet header
770 * with 802.1Q encapsulation.
772 bcopy(mtod(m, char *) + ifv->ifv_encaplen,
773 mtod(m, char *), ETHER_HDR_LEN);
774 evl = mtod(m, struct ether_vlan_header *);
775 evl->evl_proto = evl->evl_encap_proto;
776 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
777 evl->evl_tag = htons(ifv->ifv_tag);
779 printf("%s: %*D\n", __func__, (int)sizeof(*evl),
780 (unsigned char *)evl, ":");
785 * Send it, precisely as ether_output() would have.
786 * We are already running at splimp.
788 IFQ_HANDOFF(p, m, error);
797 vlan_input(struct ifnet *ifp, struct mbuf *m)
799 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
804 KASSERT(trunk != NULL, ("%s: no trunk", __func__));
806 if (m->m_flags & M_VLANTAG) {
808 * Packet is tagged, but m contains a normal
809 * Ethernet frame; the tag is stored out-of-band.
811 mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
812 KASSERT(mtag != NULL,
813 ("%s: M_VLANTAG without m_tag", __func__));
814 tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag));
815 m_tag_delete(m, mtag);
816 m->m_flags &= ~M_VLANTAG;
818 struct ether_vlan_header *evl;
821 * Packet is tagged in-band as specified by 802.1q.
824 switch (ifp->if_type) {
826 if (m->m_len < sizeof(*evl) &&
827 (m = m_pullup(m, sizeof(*evl))) == NULL) {
828 if_printf(ifp, "cannot pullup VLAN header\n");
831 evl = mtod(m, struct ether_vlan_header *);
832 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN,
833 ("%s: bad encapsulation protocol (%u)",
834 __func__, ntohs(evl->evl_encap_proto)));
836 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
839 * Restore the original ethertype. We'll remove
840 * the encapsulation after we've found the vlan
841 * interface corresponding to the tag.
843 evl->evl_encap_proto = evl->evl_proto;
848 panic("%s: unsupported if_type (%u)",
849 __func__, ifp->if_type);
856 * In VLAN_ARRAY case we proceed completely lockless.
859 ifv = trunk->vlans[tag];
860 if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
867 ifv = vlan_gethash(trunk, tag);
868 if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
869 TRUNK_RUNLOCK(trunk);
874 TRUNK_RUNLOCK(trunk);
879 * Packet had an in-line encapsulation header;
880 * remove it. The original header has already
881 * been fixed up above.
883 bcopy(mtod(m, caddr_t),
884 mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
886 m_adj(m, ETHER_VLAN_ENCAP_LEN);
889 m->m_pkthdr.rcvif = ifv->ifv_ifp;
890 ifv->ifv_ifp->if_ipackets++;
892 /* Pass it back through the parent's input routine. */
893 (*ifp->if_input)(ifv->ifv_ifp, m);
897 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
899 struct ifvlantrunk *trunk;
903 /* VID numbers 0x0 and 0xFFF are reserved */
904 if (tag == 0 || tag == 0xFFF)
906 if (p->if_type != IFT_ETHER)
907 return (EPROTONOSUPPORT);
908 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
909 return (EPROTONOSUPPORT);
913 if (p->if_vlantrunk == NULL) {
914 trunk = malloc(sizeof(struct ifvlantrunk),
915 M_VLAN, M_WAITOK | M_ZERO);
917 vlan_inithash(trunk);
920 if (p->if_vlantrunk != NULL) {
921 /* A race that that is very unlikely to be hit. */
923 vlan_freehash(trunk);
928 TRUNK_LOCK_INIT(trunk);
929 LIST_INSERT_HEAD(&trunk_list, trunk, trunk_entry);
931 p->if_vlantrunk = trunk;
936 trunk = p->if_vlantrunk;
942 if (trunk->vlans[tag] != NULL)
945 error = vlan_inshash(trunk, ifv);
950 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
951 ifv->ifv_mintu = ETHERMIN;
955 * If the parent supports the VLAN_MTU capability,
956 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
959 if (p->if_capenable & IFCAP_VLAN_MTU) {
961 * No need to fudge the MTU since the parent can
962 * handle extended frames.
964 ifv->ifv_mtufudge = 0;
967 * Fudge the MTU by the encapsulation size. This
968 * makes us incompatible with strictly compliant
969 * 802.1Q implementations, but allows us to use
970 * the feature with other NetBSD implementations,
971 * which might still be useful.
973 ifv->ifv_mtufudge = ifv->ifv_encaplen;
976 ifv->ifv_trunk = trunk;
978 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
979 ifp->if_baudrate = p->if_baudrate;
981 * Copy only a selected subset of flags from the parent.
982 * Other flags are none of our business.
984 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
985 ifp->if_flags &= ~VLAN_COPY_FLAGS;
986 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
987 #undef VLAN_COPY_FLAGS
989 ifp->if_link_state = p->if_link_state;
991 vlan_capabilities(ifv);
994 * Set up our ``Ethernet address'' to reflect the underlying
995 * physical interface's.
997 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
1000 * Configure multicast addresses that may already be
1001 * joined on the vlan device.
1003 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1006 atomic_store_rel_ptr((uintptr_t *)&trunk->vlans[tag], (uintptr_t)ifv);
1010 TRUNK_UNLOCK(trunk);
1017 vlan_unconfig(struct ifnet *ifp)
1019 struct ifvlantrunk *trunk;
1020 struct vlan_mc_entry *mc;
1026 ifv = ifp->if_softc;
1027 trunk = ifv->ifv_trunk;
1030 struct sockaddr_dl sdl;
1031 struct ifnet *p = trunk->parent;
1035 atomic_store_rel_ptr((uintptr_t *)&trunk->vlans[ifv->ifv_tag],
1041 * Since the interface is being unconfigured, we need to
1042 * empty the list of multicast groups that we may have joined
1043 * while we were alive from the parent's list.
1045 bzero((char *)&sdl, sizeof(sdl));
1046 sdl.sdl_len = sizeof(sdl);
1047 sdl.sdl_family = AF_LINK;
1048 sdl.sdl_index = p->if_index;
1049 sdl.sdl_type = IFT_ETHER;
1050 sdl.sdl_alen = ETHER_ADDR_LEN;
1052 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
1053 mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
1054 bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
1056 error = if_delmulti(p, (struct sockaddr *)&sdl);
1059 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1063 vlan_setflags(ifp, 0); /* clear special flags on parent */
1065 vlan_remhash(trunk, ifv);
1067 ifv->ifv_trunk = NULL;
1070 * Check if we were the last.
1072 if (trunk->refcnt == 0) {
1073 atomic_store_rel_ptr((uintptr_t *)
1074 &trunk->parent->if_vlantrunk,
1077 * XXXGL: If some ithread has already entered
1078 * vlan_input() and is now blocked on the trunk
1079 * lock, then it should preempt us right after
1080 * unlock and finish its work. Then we will acquire
1081 * lock again in trunk_destroy().
1082 * XXX: not true in case of VLAN_ARRAY
1084 TRUNK_UNLOCK(trunk);
1085 trunk_destroy(trunk);
1087 TRUNK_UNLOCK(trunk);
1090 /* Disconnect from parent. */
1091 if (ifv->ifv_pflags)
1092 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1093 ifv->ifv_ifp->if_mtu = ETHERMTU; /* XXX why not 0? */
1094 ifv->ifv_ifp->if_link_state = LINK_STATE_UNKNOWN;
1096 /* Clear our MAC address. */
1097 bzero(IF_LLADDR(ifv->ifv_ifp), ETHER_ADDR_LEN);
1104 /* Handle a reference counted flag that should be set on the parent as well */
1106 vlan_setflag(struct ifnet *ifp, int flag, int status,
1107 int (*func)(struct ifnet *, int))
1112 /* XXX VLAN_LOCK_ASSERT(); */
1114 ifv = ifp->if_softc;
1115 status = status ? (ifp->if_flags & flag) : 0;
1116 /* Now "status" contains the flag value or 0 */
1119 * See if recorded parent's status is different from what
1120 * we want it to be. If it is, flip it. We record parent's
1121 * status in ifv_pflags so that we won't clear parent's flag
1122 * we haven't set. In fact, we don't clear or set parent's
1123 * flags directly, but get or release references to them.
1124 * That's why we can be sure that recorded flags still are
1125 * in accord with actual parent's flags.
1127 if (status != (ifv->ifv_pflags & flag)) {
1128 error = (*func)(PARENT(ifv), status);
1131 ifv->ifv_pflags &= ~flag;
1132 ifv->ifv_pflags |= status;
1138 * Handle IFF_* flags that require certain changes on the parent:
1139 * if "status" is true, update parent's flags respective to our if_flags;
1140 * if "status" is false, forcedly clear the flags set on parent.
1143 vlan_setflags(struct ifnet *ifp, int status)
1147 for (i = 0; vlan_pflags[i].flag; i++) {
1148 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1149 status, vlan_pflags[i].func);
1156 /* Inform all vlans that their parent has changed link state */
1158 vlan_link_state(struct ifnet *ifp, int link)
1160 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1166 for (i = 0; i < EVL_VLID_MASK+1; i++)
1167 if (trunk->vlans[i] != NULL) {
1168 ifv = trunk->vlans[i];
1170 for (i = 0; i < (1 << trunk->hwidth); i++) {
1171 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1173 if_link_state_change(ifv->ifv_ifp,
1174 trunk->parent->if_link_state);
1176 TRUNK_UNLOCK(trunk);
1180 vlan_capabilities(struct ifvlan *ifv)
1182 struct ifnet *p = PARENT(ifv);
1183 struct ifnet *ifp = ifv->ifv_ifp;
1185 TRUNK_LOCK_ASSERT(TRUNK(ifv));
1188 * If the parent interface can do checksum offloading
1189 * on VLANs, then propagate its hardware-assisted
1190 * checksumming flags. Also assert that checksum
1191 * offloading requires hardware VLAN tagging.
1193 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1194 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1196 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1197 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1198 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1199 ifp->if_hwassist = p->if_hwassist;
1201 ifp->if_capenable = 0;
1202 ifp->if_hwassist = 0;
1207 vlan_trunk_capabilities(struct ifnet *ifp)
1209 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1215 for (i = 0; i < EVL_VLID_MASK+1; i++)
1216 if (trunk->vlans[i] != NULL) {
1217 ifv = trunk->vlans[i];
1219 for (i = 0; i < (1 << trunk->hwidth); i++) {
1220 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1222 vlan_capabilities(ifv);
1224 TRUNK_UNLOCK(trunk);
1228 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1237 ifr = (struct ifreq *)data;
1238 ifa = (struct ifaddr *)data;
1239 ifv = ifp->if_softc;
1243 ifp->if_flags |= IFF_UP;
1245 switch (ifa->ifa_addr->sa_family) {
1248 arp_ifinit(ifv->ifv_ifp, ifa);
1258 struct sockaddr *sa;
1260 sa = (struct sockaddr *) &ifr->ifr_data;
1261 bcopy(IF_LLADDR(ifp), (caddr_t)sa->sa_data,
1268 if (TRUNK(ifv) != NULL) {
1269 error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv),
1270 SIOCGIFMEDIA, data);
1272 /* Limit the result to the parent's current config. */
1274 struct ifmediareq *ifmr;
1276 ifmr = (struct ifmediareq *)data;
1277 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1278 ifmr->ifm_count = 1;
1279 error = copyout(&ifmr->ifm_current,
1296 * Set the interface MTU.
1299 if (TRUNK(ifv) != NULL) {
1301 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1303 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1306 ifp->if_mtu = ifr->ifr_mtu;
1313 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1316 if (vlr.vlr_parent[0] == '\0') {
1319 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1323 p = ifunit(vlr.vlr_parent);
1329 * Don't let the caller set up a VLAN tag with
1330 * anything except VLID bits.
1332 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1336 error = vlan_config(ifv, p, vlr.vlr_tag);
1339 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1341 /* Update flags on the parent, if necessary. */
1342 vlan_setflags(ifp, 1);
1346 bzero(&vlr, sizeof(vlr));
1348 if (TRUNK(ifv) != NULL) {
1349 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1350 sizeof(vlr.vlr_parent));
1351 vlr.vlr_tag = ifv->ifv_tag;
1354 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1359 * We should propagate selected flags to the parent,
1360 * e.g., promiscuous mode.
1362 if (TRUNK(ifv) != NULL)
1363 error = vlan_setflags(ifp, 1);
1369 * If we don't have a parent, just remember the membership for
1372 if (TRUNK(ifv) != NULL)
1373 error = vlan_setmulti(ifp);