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->parent->if_vlantrunk = NULL;
383 LIST_REMOVE(trunk, trunk_entry);
385 TRUNK_LOCK_DESTROY(trunk);
390 * Program our multicast filter. What we're actually doing is
391 * programming the multicast filter of the parent. This has the
392 * side effect of causing the parent interface to receive multicast
393 * traffic that it doesn't really want, which ends up being discarded
394 * later by the upper protocol layers. Unfortunately, there's no way
395 * to avoid this: there really is only one physical interface.
397 * XXX: There is a possible race here if more than one thread is
398 * modifying the multicast state of the vlan interface at the same time.
401 vlan_setmulti(struct ifnet *ifp)
404 struct ifmultiaddr *ifma, *rifma = NULL;
406 struct vlan_mc_entry *mc = NULL;
407 struct sockaddr_dl sdl;
410 /*VLAN_LOCK_ASSERT();*/
412 /* Find the parent. */
416 bzero((char *)&sdl, sizeof(sdl));
417 sdl.sdl_len = sizeof(sdl);
418 sdl.sdl_family = AF_LINK;
419 sdl.sdl_index = ifp_p->if_index;
420 sdl.sdl_type = IFT_ETHER;
421 sdl.sdl_alen = ETHER_ADDR_LEN;
423 /* First, remove any existing filter entries. */
424 while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
425 mc = SLIST_FIRST(&sc->vlan_mc_listhead);
426 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
427 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
430 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
434 /* Now program new ones. */
435 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
436 if (ifma->ifma_addr->sa_family != AF_LINK)
438 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
441 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
442 (char *)&mc->mc_addr, ETHER_ADDR_LEN);
443 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
444 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
445 LLADDR(&sdl), ETHER_ADDR_LEN);
446 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
455 * VLAN support can be loaded as a module. The only place in the
456 * system that's intimately aware of this is ether_input. We hook
457 * into this code through vlan_input_p which is defined there and
458 * set here. Noone else in the system should be aware of this so
459 * we use an explicit reference here.
461 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
463 /* For if_link_state_change() eyes only... */
464 extern void (*vlan_link_state_p)(struct ifnet *, int);
467 vlan_modevent(module_t mod, int type, void *data)
472 LIST_INIT(&trunk_list);
474 vlan_input_p = vlan_input;
475 vlan_link_state_p = vlan_link_state;
476 vlan_trunk_cap_p = vlan_trunk_capabilities;
477 if_clone_attach(&vlan_cloner);
481 struct ifvlantrunk *trunk, *trunk1;
483 if_clone_detach(&vlan_cloner);
485 vlan_link_state_p = NULL;
486 vlan_trunk_cap_p = NULL;
488 LIST_FOREACH_SAFE(trunk, &trunk_list, trunk_entry, trunk1)
489 trunk_destroy(trunk);
500 static moduledata_t vlan_mod = {
506 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
507 MODULE_VERSION(if_vlan, 3);
508 MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
510 static struct ifnet *
511 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
517 /* Check for <etherif>.<vlan> style interface names. */
519 TAILQ_FOREACH(ifp, &ifnet, if_link) {
520 if (ifp->if_type != IFT_ETHER)
522 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
524 cp = name + strlen(ifp->if_xname);
527 for(; *cp != '\0'; cp++) {
528 if (*cp < '0' || *cp > '9')
530 t = (t * 10) + (*cp - '0');
542 vlan_clone_match(struct if_clone *ifc, const char *name)
546 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
549 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
551 for (cp = name + 4; *cp != '\0'; cp++) {
552 if (*cp < '0' || *cp > '9')
560 vlan_clone_create(struct if_clone *ifc, char *name, size_t len)
571 u_char eaddr[6] = {0,0,0,0,0,0};
573 if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
579 * Don't let the caller set up a VLAN tag with
580 * anything except VLID bits.
582 if (tag & ~EVL_VLID_MASK)
587 error = ifc_name2unit(name, &unit);
591 wildcard = (unit < 0);
594 error = ifc_alloc_unit(ifc, &unit);
598 /* In the wildcard case, we need to update the name. */
600 for (dp = name; *dp != '\0'; dp++);
601 if (snprintf(dp, len - (dp-name), "%d", unit) >
602 len - (dp-name) - 1) {
603 panic("%s: interface name too long", __func__);
607 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
608 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
610 ifc_free_unit(ifc, unit);
614 SLIST_INIT(&ifv->vlan_mc_listhead);
618 * Set the name manually rather than using if_initname because
619 * we don't conform to the default naming convention for interfaces.
621 strlcpy(ifp->if_xname, name, IFNAMSIZ);
622 ifp->if_dname = ifc->ifc_name;
623 ifp->if_dunit = unit;
624 /* NB: flags are not set here */
625 ifp->if_linkmib = &ifv->ifv_mib;
626 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
627 /* NB: mtu is not set here */
629 ifp->if_init = vlan_ifinit;
630 ifp->if_start = vlan_start;
631 ifp->if_ioctl = vlan_ioctl;
632 ifp->if_snd.ifq_maxlen = ifqmaxlen;
633 ifp->if_flags = VLAN_IFFLAGS;
634 ether_ifattach(ifp, eaddr);
635 /* Now undo some of the damage... */
636 ifp->if_baudrate = 0;
637 ifp->if_type = IFT_L2VLAN;
638 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
641 error = vlan_config(ifv, p, tag);
644 * Since we've partialy failed, we need to back
645 * out all the way, otherwise userland could get
646 * confused. Thus, we destroy the interface.
650 if_free_type(ifp, IFT_ETHER);
655 ifp->if_drv_flags |= IFF_DRV_RUNNING;
657 /* Update flags on the parent, if necessary. */
658 vlan_setflags(ifp, 1);
665 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
668 struct ifvlan *ifv = ifp->if_softc;
670 unit = ifp->if_dunit;
675 if_free_type(ifp, IFT_ETHER);
679 ifc_free_unit(ifc, unit);
685 * The ifp->if_init entry point for vlan(4) is a no-op.
688 vlan_ifinit(void *foo)
694 * The if_start method for vlan(4) interface. It doesn't
695 * raises the IFF_DRV_OACTIVE flag, since it is called
696 * only from IFQ_HANDOFF() macro in ether_output_frame().
697 * If the interface queue is full, and vlan_start() is
698 * not called, the queue would never get emptied and
699 * interface would stall forever.
702 vlan_start(struct ifnet *ifp)
713 IF_DEQUEUE(&ifp->if_snd, m);
719 * Do not run parent's if_start() if the parent is not up,
720 * or parent's driver will cause a system crash.
722 if (!((p->if_flags & IFF_UP) &&
723 (p->if_drv_flags & IFF_DRV_RUNNING))) {
725 ifp->if_collisions++;
730 * If underlying interface can do VLAN tag insertion itself,
731 * just pass the packet along. However, we need some way to
732 * tell the interface where the packet came from so that it
733 * knows how to find the VLAN tag to use, so we attach a
734 * packet tag that holds it.
736 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
737 struct m_tag *mtag = (struct m_tag *)
738 uma_zalloc(zone_mtag_vlan, M_NOWAIT);
744 VLAN_TAG_VALUE(mtag) = ifv->ifv_tag;
745 m_tag_prepend(m, mtag);
746 m->m_flags |= M_VLANTAG;
748 struct ether_vlan_header *evl;
750 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
753 "unable to prepend VLAN header\n");
757 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
759 if (m->m_len < sizeof(*evl)) {
760 m = m_pullup(m, sizeof(*evl));
763 "cannot pullup VLAN header\n");
770 * Transform the Ethernet header into an Ethernet header
771 * with 802.1Q encapsulation.
773 bcopy(mtod(m, char *) + ifv->ifv_encaplen,
774 mtod(m, char *), ETHER_HDR_LEN);
775 evl = mtod(m, struct ether_vlan_header *);
776 evl->evl_proto = evl->evl_encap_proto;
777 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
778 evl->evl_tag = htons(ifv->ifv_tag);
780 printf("%s: %*D\n", __func__, (int)sizeof(*evl),
781 (unsigned char *)evl, ":");
786 * Send it, precisely as ether_output() would have.
787 * We are already running at splimp.
789 IFQ_HANDOFF(p, m, error);
798 vlan_input(struct ifnet *ifp, struct mbuf *m)
800 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
805 KASSERT(trunk != NULL, ("%s: no trunk", __func__));
807 if (m->m_flags & M_VLANTAG) {
809 * Packet is tagged, but m contains a normal
810 * Ethernet frame; the tag is stored out-of-band.
812 mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
813 KASSERT(mtag != NULL,
814 ("%s: M_VLANTAG without m_tag", __func__));
815 tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag));
816 m_tag_delete(m, mtag);
817 m->m_flags &= ~M_VLANTAG;
819 struct ether_vlan_header *evl;
822 * Packet is tagged in-band as specified by 802.1q.
825 switch (ifp->if_type) {
827 if (m->m_len < sizeof(*evl) &&
828 (m = m_pullup(m, sizeof(*evl))) == NULL) {
829 if_printf(ifp, "cannot pullup VLAN header\n");
832 evl = mtod(m, struct ether_vlan_header *);
833 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN,
834 ("%s: bad encapsulation protocol (%u)",
835 __func__, ntohs(evl->evl_encap_proto)));
837 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
840 * Restore the original ethertype. We'll remove
841 * the encapsulation after we've found the vlan
842 * interface corresponding to the tag.
844 evl->evl_encap_proto = evl->evl_proto;
849 panic("%s: unsupported if_type (%u)",
850 __func__, ifp->if_type);
857 * In VLAN_ARRAY case we proceed completely lockless.
860 ifv = trunk->vlans[tag];
861 if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
868 ifv = vlan_gethash(trunk, tag);
869 if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
870 TRUNK_RUNLOCK(trunk);
875 TRUNK_RUNLOCK(trunk);
880 * Packet had an in-line encapsulation header;
881 * remove it. The original header has already
882 * been fixed up above.
884 bcopy(mtod(m, caddr_t),
885 mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
887 m_adj(m, ETHER_VLAN_ENCAP_LEN);
890 m->m_pkthdr.rcvif = ifv->ifv_ifp;
891 ifv->ifv_ifp->if_ipackets++;
893 /* Pass it back through the parent's input routine. */
894 (*ifp->if_input)(ifv->ifv_ifp, m);
898 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
900 struct ifvlantrunk *trunk;
904 /* VID numbers 0x0 and 0xFFF are reserved */
905 if (tag == 0 || tag == 0xFFF)
907 if (p->if_type != IFT_ETHER)
908 return (EPROTONOSUPPORT);
909 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
910 return (EPROTONOSUPPORT);
914 if (p->if_vlantrunk == NULL) {
915 trunk = malloc(sizeof(struct ifvlantrunk),
916 M_VLAN, M_WAITOK | M_ZERO);
918 vlan_inithash(trunk);
921 if (p->if_vlantrunk != NULL) {
922 /* A race that that is very unlikely to be hit. */
924 vlan_freehash(trunk);
929 TRUNK_LOCK_INIT(trunk);
930 LIST_INSERT_HEAD(&trunk_list, trunk, trunk_entry);
932 p->if_vlantrunk = trunk;
937 trunk = p->if_vlantrunk;
943 if (trunk->vlans[tag] != NULL)
946 error = vlan_inshash(trunk, ifv);
951 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
952 ifv->ifv_mintu = ETHERMIN;
956 * If the parent supports the VLAN_MTU capability,
957 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
960 if (p->if_capenable & IFCAP_VLAN_MTU) {
962 * No need to fudge the MTU since the parent can
963 * handle extended frames.
965 ifv->ifv_mtufudge = 0;
968 * Fudge the MTU by the encapsulation size. This
969 * makes us incompatible with strictly compliant
970 * 802.1Q implementations, but allows us to use
971 * the feature with other NetBSD implementations,
972 * which might still be useful.
974 ifv->ifv_mtufudge = ifv->ifv_encaplen;
977 ifv->ifv_trunk = trunk;
979 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
980 ifp->if_baudrate = p->if_baudrate;
982 * Copy only a selected subset of flags from the parent.
983 * Other flags are none of our business.
985 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
986 ifp->if_flags &= ~VLAN_COPY_FLAGS;
987 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
988 #undef VLAN_COPY_FLAGS
990 ifp->if_link_state = p->if_link_state;
992 vlan_capabilities(ifv);
995 * Set up our ``Ethernet address'' to reflect the underlying
996 * physical interface's.
998 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
1001 * Configure multicast addresses that may already be
1002 * joined on the vlan device.
1004 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1007 atomic_store_rel_ptr((uintptr_t *)&trunk->vlans[tag], (uintptr_t)ifv);
1011 TRUNK_UNLOCK(trunk);
1018 vlan_unconfig(struct ifnet *ifp)
1020 struct ifvlantrunk *trunk;
1021 struct vlan_mc_entry *mc;
1027 ifv = ifp->if_softc;
1028 trunk = ifv->ifv_trunk;
1031 struct sockaddr_dl sdl;
1032 struct ifnet *p = trunk->parent;
1036 atomic_store_rel_ptr((uintptr_t *)&trunk->vlans[ifv->ifv_tag],
1042 * Since the interface is being unconfigured, we need to
1043 * empty the list of multicast groups that we may have joined
1044 * while we were alive from the parent's list.
1046 bzero((char *)&sdl, sizeof(sdl));
1047 sdl.sdl_len = sizeof(sdl);
1048 sdl.sdl_family = AF_LINK;
1049 sdl.sdl_index = p->if_index;
1050 sdl.sdl_type = IFT_ETHER;
1051 sdl.sdl_alen = ETHER_ADDR_LEN;
1053 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
1054 mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
1055 bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
1057 error = if_delmulti(p, (struct sockaddr *)&sdl);
1060 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1064 vlan_setflags(ifp, 0); /* clear special flags on parent */
1066 vlan_remhash(trunk, ifv);
1068 ifv->ifv_trunk = NULL;
1071 * Check if we were the last.
1073 if (trunk->refcnt == 0) {
1074 atomic_store_rel_ptr((uintptr_t *)
1075 &trunk->parent->if_vlantrunk,
1078 * XXXGL: If some ithread has already entered
1079 * vlan_input() and is now blocked on the trunk
1080 * lock, then it should preempt us right after
1081 * unlock and finish its work. Then we will acquire
1082 * lock again in trunk_destroy().
1083 * XXX: not true in case of VLAN_ARRAY
1085 TRUNK_UNLOCK(trunk);
1086 trunk_destroy(trunk);
1088 TRUNK_UNLOCK(trunk);
1091 /* Disconnect from parent. */
1092 if (ifv->ifv_pflags)
1093 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1094 ifv->ifv_ifp->if_mtu = ETHERMTU; /* XXX why not 0? */
1095 ifv->ifv_ifp->if_link_state = LINK_STATE_UNKNOWN;
1097 /* Clear our MAC address. */
1098 bzero(IF_LLADDR(ifv->ifv_ifp), ETHER_ADDR_LEN);
1105 /* Handle a reference counted flag that should be set on the parent as well */
1107 vlan_setflag(struct ifnet *ifp, int flag, int status,
1108 int (*func)(struct ifnet *, int))
1113 /* XXX VLAN_LOCK_ASSERT(); */
1115 ifv = ifp->if_softc;
1116 status = status ? (ifp->if_flags & flag) : 0;
1117 /* Now "status" contains the flag value or 0 */
1120 * See if recorded parent's status is different from what
1121 * we want it to be. If it is, flip it. We record parent's
1122 * status in ifv_pflags so that we won't clear parent's flag
1123 * we haven't set. In fact, we don't clear or set parent's
1124 * flags directly, but get or release references to them.
1125 * That's why we can be sure that recorded flags still are
1126 * in accord with actual parent's flags.
1128 if (status != (ifv->ifv_pflags & flag)) {
1129 error = (*func)(PARENT(ifv), status);
1132 ifv->ifv_pflags &= ~flag;
1133 ifv->ifv_pflags |= status;
1139 * Handle IFF_* flags that require certain changes on the parent:
1140 * if "status" is true, update parent's flags respective to our if_flags;
1141 * if "status" is false, forcedly clear the flags set on parent.
1144 vlan_setflags(struct ifnet *ifp, int status)
1148 for (i = 0; vlan_pflags[i].flag; i++) {
1149 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1150 status, vlan_pflags[i].func);
1157 /* Inform all vlans that their parent has changed link state */
1159 vlan_link_state(struct ifnet *ifp, int link)
1161 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1167 for (i = 0; i < EVL_VLID_MASK+1; i++)
1168 if (trunk->vlans[i] != NULL) {
1169 ifv = trunk->vlans[i];
1171 for (i = 0; i < (1 << trunk->hwidth); i++) {
1172 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1174 if_link_state_change(ifv->ifv_ifp,
1175 trunk->parent->if_link_state);
1177 TRUNK_UNLOCK(trunk);
1181 vlan_capabilities(struct ifvlan *ifv)
1183 struct ifnet *p = PARENT(ifv);
1184 struct ifnet *ifp = ifv->ifv_ifp;
1186 TRUNK_LOCK_ASSERT(TRUNK(ifv));
1189 * If the parent interface can do checksum offloading
1190 * on VLANs, then propagate its hardware-assisted
1191 * checksumming flags. Also assert that checksum
1192 * offloading requires hardware VLAN tagging.
1194 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1195 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1197 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1198 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1199 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1200 ifp->if_hwassist = p->if_hwassist;
1202 ifp->if_capenable = 0;
1203 ifp->if_hwassist = 0;
1208 vlan_trunk_capabilities(struct ifnet *ifp)
1210 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1216 for (i = 0; i < EVL_VLID_MASK+1; i++)
1217 if (trunk->vlans[i] != NULL) {
1218 ifv = trunk->vlans[i];
1220 for (i = 0; i < (1 << trunk->hwidth); i++) {
1221 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1223 vlan_capabilities(ifv);
1225 TRUNK_UNLOCK(trunk);
1229 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1238 ifr = (struct ifreq *)data;
1239 ifa = (struct ifaddr *)data;
1240 ifv = ifp->if_softc;
1244 ifp->if_flags |= IFF_UP;
1246 switch (ifa->ifa_addr->sa_family) {
1249 arp_ifinit(ifv->ifv_ifp, ifa);
1259 struct sockaddr *sa;
1261 sa = (struct sockaddr *) &ifr->ifr_data;
1262 bcopy(IF_LLADDR(ifp), (caddr_t)sa->sa_data,
1269 if (TRUNK(ifv) != NULL) {
1270 error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv),
1271 SIOCGIFMEDIA, data);
1273 /* Limit the result to the parent's current config. */
1275 struct ifmediareq *ifmr;
1277 ifmr = (struct ifmediareq *)data;
1278 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1279 ifmr->ifm_count = 1;
1280 error = copyout(&ifmr->ifm_current,
1297 * Set the interface MTU.
1300 if (TRUNK(ifv) != NULL) {
1302 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1304 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1307 ifp->if_mtu = ifr->ifr_mtu;
1314 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1317 if (vlr.vlr_parent[0] == '\0') {
1319 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1322 p = ifunit(vlr.vlr_parent);
1328 * Don't let the caller set up a VLAN tag with
1329 * anything except VLID bits.
1331 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1335 error = vlan_config(ifv, p, vlr.vlr_tag);
1338 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1340 /* Update flags on the parent, if necessary. */
1341 vlan_setflags(ifp, 1);
1345 bzero(&vlr, sizeof(vlr));
1347 if (TRUNK(ifv) != NULL) {
1348 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1349 sizeof(vlr.vlr_parent));
1350 vlr.vlr_tag = ifv->ifv_tag;
1353 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1358 * We should propagate selected flags to the parent,
1359 * e.g., promiscuous mode.
1361 if (TRUNK(ifv) != NULL)
1362 error = vlan_setflags(ifp, 1);
1368 * If we don't have a parent, just remember the membership for
1371 if (TRUNK(ifv) != NULL)
1372 error = vlan_setmulti(ifp);