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
31 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
32 * Might be extended some day to also handle IEEE 802.1p priority
33 * tagging. This is sort of sneaky in the implementation, since
34 * we need to pretend to be enough of an Ethernet implementation
35 * to make arp work. The way we do this is by telling everyone
36 * that we are an Ethernet, and then catch the packets that
37 * ether_output() sends to us via if_transmit(), rewrite them for
38 * use by the real outgoing interface, and ask it to send them.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
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_dl.h>
65 #include <net/if_types.h>
66 #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 #define UP_AND_RUNNING(ifp) \
79 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
81 LIST_HEAD(ifvlanhead, ifvlan);
84 struct ifnet *parent; /* parent interface of this trunk */
87 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
88 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
90 struct ifvlanhead *hash; /* dynamic hash-list table */
97 struct vlan_mc_entry {
98 struct ether_addr mc_addr;
99 SLIST_ENTRY(vlan_mc_entry) mc_entries;
103 struct ifvlantrunk *ifv_trunk;
104 struct ifnet *ifv_ifp;
105 #define TRUNK(ifv) ((ifv)->ifv_trunk)
106 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
107 int ifv_pflags; /* special flags we have set on parent */
109 int ifvm_encaplen; /* encapsulation length */
110 int ifvm_mtufudge; /* MTU fudged by this much */
111 int ifvm_mintu; /* min transmission unit */
112 uint16_t ifvm_proto; /* encapsulation ethertype */
113 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
115 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
117 LIST_ENTRY(ifvlan) ifv_list;
120 #define ifv_proto ifv_mib.ifvm_proto
121 #define ifv_tag ifv_mib.ifvm_tag
122 #define ifv_encaplen ifv_mib.ifvm_encaplen
123 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
124 #define ifv_mintu ifv_mib.ifvm_mintu
126 /* Special flags we should propagate to parent. */
129 int (*func)(struct ifnet *, int);
131 {IFF_PROMISC, ifpromisc},
132 {IFF_ALLMULTI, if_allmulti},
136 SYSCTL_DECL(_net_link);
137 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
138 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
140 static int soft_pad = 0;
141 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
142 "pad short frames before tagging");
144 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
146 static eventhandler_tag ifdetach_tag;
147 static eventhandler_tag iflladdr_tag;
150 * We have a global mutex, that is used to serialize configuration
151 * changes and isn't used in normal packet delivery.
153 * We also have a per-trunk rwlock, that is locked shared on packet
154 * processing and exclusive when configuration is changed.
156 * The VLAN_ARRAY substitutes the dynamic hash with a static array
157 * with 4096 entries. In theory this can give a boost in processing,
158 * however on practice it does not. Probably this is because array
159 * is too big to fit into CPU cache.
161 static struct mtx ifv_mtx;
162 #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
163 #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx)
164 #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED)
165 #define VLAN_LOCK() mtx_lock(&ifv_mtx)
166 #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx)
167 #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME)
168 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
169 #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw)
170 #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw)
171 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
172 #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw)
173 #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw)
174 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
177 static void vlan_inithash(struct ifvlantrunk *trunk);
178 static void vlan_freehash(struct ifvlantrunk *trunk);
179 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
180 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
181 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
182 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
185 static void trunk_destroy(struct ifvlantrunk *trunk);
187 static void vlan_init(void *foo);
188 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
189 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
190 static void vlan_qflush(struct ifnet *ifp);
191 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
192 int (*func)(struct ifnet *, int));
193 static int vlan_setflags(struct ifnet *ifp, int status);
194 static int vlan_setmulti(struct ifnet *ifp);
195 static int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
196 static void vlan_unconfig(struct ifnet *ifp);
197 static void vlan_unconfig_locked(struct ifnet *ifp, int departing);
198 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
199 static void vlan_link_state(struct ifnet *ifp, int link);
200 static void vlan_capabilities(struct ifvlan *ifv);
201 static void vlan_trunk_capabilities(struct ifnet *ifp);
203 static struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
204 const char *, int *);
205 static int vlan_clone_match(struct if_clone *, const char *);
206 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
207 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
209 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
210 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
212 static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
213 IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
216 static VNET_DEFINE(struct if_clone, vlan_cloner);
217 #define V_vlan_cloner VNET(vlan_cloner)
221 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
224 vlan_inithash(struct ifvlantrunk *trunk)
229 * The trunk must not be locked here since we call malloc(M_WAITOK).
230 * It is OK in case this function is called before the trunk struct
231 * gets hooked up and becomes visible from other threads.
234 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
235 ("%s: hash already initialized", __func__));
237 trunk->hwidth = VLAN_DEF_HWIDTH;
238 n = 1 << trunk->hwidth;
239 trunk->hmask = n - 1;
240 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
241 for (i = 0; i < n; i++)
242 LIST_INIT(&trunk->hash[i]);
246 vlan_freehash(struct ifvlantrunk *trunk)
251 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
252 for (i = 0; i < (1 << trunk->hwidth); i++)
253 KASSERT(LIST_EMPTY(&trunk->hash[i]),
254 ("%s: hash table not empty", __func__));
256 free(trunk->hash, M_VLAN);
258 trunk->hwidth = trunk->hmask = 0;
262 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
267 TRUNK_LOCK_ASSERT(trunk);
268 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
270 b = 1 << trunk->hwidth;
271 i = HASH(ifv->ifv_tag, trunk->hmask);
272 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
273 if (ifv->ifv_tag == ifv2->ifv_tag)
277 * Grow the hash when the number of vlans exceeds half of the number of
278 * hash buckets squared. This will make the average linked-list length
281 if (trunk->refcnt > (b * b) / 2) {
282 vlan_growhash(trunk, 1);
283 i = HASH(ifv->ifv_tag, trunk->hmask);
285 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
292 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
297 TRUNK_LOCK_ASSERT(trunk);
298 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
300 b = 1 << trunk->hwidth;
301 i = HASH(ifv->ifv_tag, trunk->hmask);
302 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
305 LIST_REMOVE(ifv2, ifv_list);
306 if (trunk->refcnt < (b * b) / 2)
307 vlan_growhash(trunk, -1);
311 panic("%s: vlan not found\n", __func__);
312 return (ENOENT); /*NOTREACHED*/
316 * Grow the hash larger or smaller if memory permits.
319 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
322 struct ifvlanhead *hash2;
323 int hwidth2, i, j, n, n2;
325 TRUNK_LOCK_ASSERT(trunk);
326 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
329 /* Harmless yet obvious coding error */
330 printf("%s: howmuch is 0\n", __func__);
334 hwidth2 = trunk->hwidth + howmuch;
335 n = 1 << trunk->hwidth;
337 /* Do not shrink the table below the default */
338 if (hwidth2 < VLAN_DEF_HWIDTH)
341 /* M_NOWAIT because we're called with trunk mutex held */
342 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
344 printf("%s: out of memory -- hash size not changed\n",
346 return; /* We can live with the old hash table */
348 for (j = 0; j < n2; j++)
349 LIST_INIT(&hash2[j]);
350 for (i = 0; i < n; i++)
351 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
352 LIST_REMOVE(ifv, ifv_list);
353 j = HASH(ifv->ifv_tag, n2 - 1);
354 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
356 free(trunk->hash, M_VLAN);
358 trunk->hwidth = hwidth2;
359 trunk->hmask = n2 - 1;
362 if_printf(trunk->parent,
363 "VLAN hash table resized from %d to %d buckets\n", n, n2);
366 static __inline struct ifvlan *
367 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
371 TRUNK_LOCK_RASSERT(trunk);
373 LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
374 if (ifv->ifv_tag == tag)
380 /* Debugging code to view the hashtables. */
382 vlan_dumphash(struct ifvlantrunk *trunk)
387 for (i = 0; i < (1 << trunk->hwidth); i++) {
389 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
390 printf("%s ", ifv->ifv_ifp->if_xname);
395 #endif /* !VLAN_ARRAY */
398 trunk_destroy(struct ifvlantrunk *trunk)
404 vlan_freehash(trunk);
406 trunk->parent->if_vlantrunk = NULL;
408 TRUNK_LOCK_DESTROY(trunk);
413 * Program our multicast filter. What we're actually doing is
414 * programming the multicast filter of the parent. This has the
415 * side effect of causing the parent interface to receive multicast
416 * traffic that it doesn't really want, which ends up being discarded
417 * later by the upper protocol layers. Unfortunately, there's no way
418 * to avoid this: there really is only one physical interface.
420 * XXX: There is a possible race here if more than one thread is
421 * modifying the multicast state of the vlan interface at the same time.
424 vlan_setmulti(struct ifnet *ifp)
427 struct ifmultiaddr *ifma, *rifma = NULL;
429 struct vlan_mc_entry *mc;
430 struct sockaddr_dl sdl;
433 /*VLAN_LOCK_ASSERT();*/
435 /* Find the parent. */
439 CURVNET_SET_QUIET(ifp_p->if_vnet);
441 bzero((char *)&sdl, sizeof(sdl));
442 sdl.sdl_len = sizeof(sdl);
443 sdl.sdl_family = AF_LINK;
444 sdl.sdl_index = ifp_p->if_index;
445 sdl.sdl_type = IFT_ETHER;
446 sdl.sdl_alen = ETHER_ADDR_LEN;
448 /* First, remove any existing filter entries. */
449 while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
450 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
451 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
454 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
458 /* Now program new ones. */
459 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
460 if (ifma->ifma_addr->sa_family != AF_LINK)
462 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
465 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
466 (char *)&mc->mc_addr, ETHER_ADDR_LEN);
467 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
468 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
469 LLADDR(&sdl), ETHER_ADDR_LEN);
470 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
480 * A handler for parent interface link layer address changes.
481 * If the parent interface link layer address is changed we
482 * should also change it on all children vlans.
485 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
494 * Check if it's a trunk interface first of all
495 * to avoid needless locking.
497 if (ifp->if_vlantrunk == NULL)
502 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
505 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
506 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
507 #else /* VLAN_ARRAY */
508 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
509 LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) {
510 #endif /* VLAN_ARRAY */
512 if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
520 * A handler for network interface departure events.
521 * Track departure of trunks here so that we don't access invalid
522 * pointers or whatever if a trunk is ripped from under us, e.g.,
523 * by ejecting its hot-plug card. However, if an ifnet is simply
524 * being renamed, then there's no need to tear down the state.
527 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
533 * Check if it's a trunk interface first of all
534 * to avoid needless locking.
536 if (ifp->if_vlantrunk == NULL)
539 /* If the ifnet is just being renamed, don't do anything. */
540 if (ifp->if_flags & IFF_RENAMING)
545 * OK, it's a trunk. Loop over and detach all vlan's on it.
546 * Check trunk pointer after each vlan_unconfig() as it will
547 * free it and set to NULL after the last vlan was detached.
550 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
551 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
552 vlan_unconfig_locked(ifv->ifv_ifp, 1);
553 if (ifp->if_vlantrunk == NULL)
556 #else /* VLAN_ARRAY */
558 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
559 if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
560 vlan_unconfig_locked(ifv->ifv_ifp, 1);
561 if (ifp->if_vlantrunk)
562 goto restart; /* trunk->hwidth can change */
566 #endif /* VLAN_ARRAY */
567 /* Trunk should have been destroyed in vlan_unconfig(). */
568 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
573 * VLAN support can be loaded as a module. The only place in the
574 * system that's intimately aware of this is ether_input. We hook
575 * into this code through vlan_input_p which is defined there and
576 * set here. Noone else in the system should be aware of this so
577 * we use an explicit reference here.
579 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
581 /* For if_link_state_change() eyes only... */
582 extern void (*vlan_link_state_p)(struct ifnet *, int);
585 vlan_modevent(module_t mod, int type, void *data)
590 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
591 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
592 if (ifdetach_tag == NULL)
594 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
595 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
596 if (iflladdr_tag == NULL)
599 vlan_input_p = vlan_input;
600 vlan_link_state_p = vlan_link_state;
601 vlan_trunk_cap_p = vlan_trunk_capabilities;
603 if_clone_attach(&vlan_cloner);
606 printf("vlan: initialized, using "
610 "hash tables with chaining"
617 if_clone_detach(&vlan_cloner);
619 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
620 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
622 vlan_link_state_p = NULL;
623 vlan_trunk_cap_p = NULL;
626 printf("vlan: unloaded\n");
634 static moduledata_t vlan_mod = {
640 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
641 MODULE_VERSION(if_vlan, 3);
645 vnet_vlan_init(const void *unused __unused)
648 V_vlan_cloner = vlan_cloner;
649 if_clone_attach(&V_vlan_cloner);
651 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
652 vnet_vlan_init, NULL);
655 vnet_vlan_uninit(const void *unused __unused)
658 if_clone_detach(&V_vlan_cloner);
660 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
661 vnet_vlan_uninit, NULL);
664 static struct ifnet *
665 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
671 /* Check for <etherif>.<vlan> style interface names. */
672 IFNET_RLOCK_NOSLEEP();
673 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
674 if (ifp->if_type != IFT_ETHER)
676 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
678 cp = name + strlen(ifp->if_xname);
684 for(; *cp >= '0' && *cp <= '9'; cp++)
685 t = (t * 10) + (*cp - '0');
692 IFNET_RUNLOCK_NOSLEEP();
698 vlan_clone_match(struct if_clone *ifc, const char *name)
702 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
705 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
707 for (cp = name + 4; *cp != '\0'; cp++) {
708 if (*cp < '0' || *cp > '9')
716 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
728 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
731 * There are 3 (ugh) ways to specify the cloned device:
732 * o pass a parameter block with the clone request.
733 * o specify parameters in the text of the clone device name
734 * o specify no parameters and get an unattached device that
735 * must be configured separately.
736 * The first technique is preferred; the latter two are
737 * supported for backwards compatibilty.
740 error = copyin(params, &vlr, sizeof(vlr));
743 p = ifunit(vlr.vlr_parent);
747 * Don't let the caller set up a VLAN tag with
748 * anything except VLID bits.
750 if (vlr.vlr_tag & ~EVL_VLID_MASK)
752 error = ifc_name2unit(name, &unit);
758 wildcard = (unit < 0);
759 } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
765 * Don't let the caller set up a VLAN tag with
766 * anything except VLID bits.
768 if (tag & ~EVL_VLID_MASK)
773 error = ifc_name2unit(name, &unit);
777 wildcard = (unit < 0);
780 error = ifc_alloc_unit(ifc, &unit);
784 /* In the wildcard case, we need to update the name. */
786 for (dp = name; *dp != '\0'; dp++);
787 if (snprintf(dp, len - (dp-name), "%d", unit) >
788 len - (dp-name) - 1) {
789 panic("%s: interface name too long", __func__);
793 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
794 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
796 ifc_free_unit(ifc, unit);
800 SLIST_INIT(&ifv->vlan_mc_listhead);
804 * Set the name manually rather than using if_initname because
805 * we don't conform to the default naming convention for interfaces.
807 strlcpy(ifp->if_xname, name, IFNAMSIZ);
808 ifp->if_dname = ifc->ifc_name;
809 ifp->if_dunit = unit;
810 /* NB: flags are not set here */
811 ifp->if_linkmib = &ifv->ifv_mib;
812 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
813 /* NB: mtu is not set here */
815 ifp->if_init = vlan_init;
816 ifp->if_transmit = vlan_transmit;
817 ifp->if_qflush = vlan_qflush;
818 ifp->if_ioctl = vlan_ioctl;
819 ifp->if_flags = VLAN_IFFLAGS;
820 ether_ifattach(ifp, eaddr);
821 /* Now undo some of the damage... */
822 ifp->if_baudrate = 0;
823 ifp->if_type = IFT_L2VLAN;
824 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
827 error = vlan_config(ifv, p, tag);
830 * Since we've partially failed, we need to back
831 * out all the way, otherwise userland could get
832 * confused. Thus, we destroy the interface.
836 if_free_type(ifp, IFT_ETHER);
837 ifc_free_unit(ifc, unit);
843 /* Update flags on the parent, if necessary. */
844 vlan_setflags(ifp, 1);
851 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
853 struct ifvlan *ifv = ifp->if_softc;
854 int unit = ifp->if_dunit;
856 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
857 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
858 if_free_type(ifp, IFT_ETHER);
860 ifc_free_unit(ifc, unit);
866 * The ifp->if_init entry point for vlan(4) is a no-op.
869 vlan_init(void *foo __unused)
874 * The if_transmit method for vlan(4) interface.
877 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
881 int error, len, mcast;
885 len = m->m_pkthdr.len;
886 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
891 * Do not run parent's if_transmit() if the parent is not up,
892 * or parent's driver will cause a system crash.
894 if (!UP_AND_RUNNING(p)) {
901 * Pad the frame to the minimum size allowed if told to.
902 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
903 * paragraph C.4.4.3.b. It can help to work around buggy
904 * bridges that violate paragraph C.4.4.3.a from the same
905 * document, i.e., fail to pad short frames after untagging.
906 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
907 * untagging it will produce a 62-byte frame, which is a runt
908 * and requires padding. There are VLAN-enabled network
909 * devices that just discard such runts instead or mishandle
913 static char pad[8]; /* just zeros */
916 for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
917 n > 0; n -= sizeof(pad))
918 if (!m_append(m, min(n, sizeof(pad)), pad))
922 if_printf(ifp, "cannot pad short frame\n");
930 * If underlying interface can do VLAN tag insertion itself,
931 * just pass the packet along. However, we need some way to
932 * tell the interface where the packet came from so that it
933 * knows how to find the VLAN tag to use, so we attach a
934 * packet tag that holds it.
936 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
937 m->m_pkthdr.ether_vtag = ifv->ifv_tag;
938 m->m_flags |= M_VLANTAG;
940 m = ether_vlanencap(m, ifv->ifv_tag);
942 if_printf(ifp, "unable to prepend VLAN header\n");
949 * Send it, precisely as ether_output() would have.
951 error = (p->if_transmit)(p, m);
954 ifp->if_omcasts += mcast;
955 ifp->if_obytes += len;
962 * The ifp->if_qflush entry point for vlan(4) is a no-op.
965 vlan_qflush(struct ifnet *ifp __unused)
970 vlan_input(struct ifnet *ifp, struct mbuf *m)
972 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
976 KASSERT(trunk != NULL, ("%s: no trunk", __func__));
978 if (m->m_flags & M_VLANTAG) {
980 * Packet is tagged, but m contains a normal
981 * Ethernet frame; the tag is stored out-of-band.
983 tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
984 m->m_flags &= ~M_VLANTAG;
986 struct ether_vlan_header *evl;
989 * Packet is tagged in-band as specified by 802.1q.
991 switch (ifp->if_type) {
993 if (m->m_len < sizeof(*evl) &&
994 (m = m_pullup(m, sizeof(*evl))) == NULL) {
995 if_printf(ifp, "cannot pullup VLAN header\n");
998 evl = mtod(m, struct ether_vlan_header *);
999 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
1002 * Remove the 802.1q header by copying the Ethernet
1003 * addresses over it and adjusting the beginning of
1004 * the data in the mbuf. The encapsulated Ethernet
1005 * type field is already in place.
1007 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1008 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1009 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1014 panic("%s: %s has unsupported if_type %u",
1015 __func__, ifp->if_xname, ifp->if_type);
1025 ifv = trunk->vlans[tag];
1027 ifv = vlan_gethash(trunk, tag);
1029 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1030 TRUNK_RUNLOCK(trunk);
1035 TRUNK_RUNLOCK(trunk);
1037 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1038 ifv->ifv_ifp->if_ipackets++;
1040 /* Pass it back through the parent's input routine. */
1041 (*ifp->if_input)(ifv->ifv_ifp, m);
1045 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
1047 struct ifvlantrunk *trunk;
1051 /* VID numbers 0x0 and 0xFFF are reserved */
1052 if (tag == 0 || tag == 0xFFF)
1054 if (p->if_type != IFT_ETHER)
1055 return (EPROTONOSUPPORT);
1056 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1057 return (EPROTONOSUPPORT);
1061 if (p->if_vlantrunk == NULL) {
1062 trunk = malloc(sizeof(struct ifvlantrunk),
1063 M_VLAN, M_WAITOK | M_ZERO);
1065 vlan_inithash(trunk);
1068 if (p->if_vlantrunk != NULL) {
1069 /* A race that that is very unlikely to be hit. */
1071 vlan_freehash(trunk);
1073 free(trunk, M_VLAN);
1076 TRUNK_LOCK_INIT(trunk);
1078 p->if_vlantrunk = trunk;
1083 trunk = p->if_vlantrunk;
1087 ifv->ifv_tag = tag; /* must set this before vlan_inshash() */
1089 if (trunk->vlans[tag] != NULL) {
1093 trunk->vlans[tag] = ifv;
1096 error = vlan_inshash(trunk, ifv);
1100 ifv->ifv_proto = ETHERTYPE_VLAN;
1101 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1102 ifv->ifv_mintu = ETHERMIN;
1103 ifv->ifv_pflags = 0;
1106 * If the parent supports the VLAN_MTU capability,
1107 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1110 if (p->if_capenable & IFCAP_VLAN_MTU) {
1112 * No need to fudge the MTU since the parent can
1113 * handle extended frames.
1115 ifv->ifv_mtufudge = 0;
1118 * Fudge the MTU by the encapsulation size. This
1119 * makes us incompatible with strictly compliant
1120 * 802.1Q implementations, but allows us to use
1121 * the feature with other NetBSD implementations,
1122 * which might still be useful.
1124 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1127 ifv->ifv_trunk = trunk;
1129 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1130 ifp->if_baudrate = p->if_baudrate;
1132 * Copy only a selected subset of flags from the parent.
1133 * Other flags are none of our business.
1135 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1136 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1137 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1138 #undef VLAN_COPY_FLAGS
1140 ifp->if_link_state = p->if_link_state;
1142 vlan_capabilities(ifv);
1145 * Set up our ``Ethernet address'' to reflect the underlying
1146 * physical interface's.
1148 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
1151 * Configure multicast addresses that may already be
1152 * joined on the vlan device.
1154 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1156 /* We are ready for operation now. */
1157 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1159 TRUNK_UNLOCK(trunk);
1161 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag);
1168 vlan_unconfig(struct ifnet *ifp)
1172 vlan_unconfig_locked(ifp, 0);
1177 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1179 struct ifvlantrunk *trunk;
1180 struct vlan_mc_entry *mc;
1182 struct ifnet *parent;
1187 ifv = ifp->if_softc;
1188 trunk = ifv->ifv_trunk;
1191 if (trunk != NULL) {
1192 struct sockaddr_dl sdl;
1195 parent = trunk->parent;
1198 * Since the interface is being unconfigured, we need to
1199 * empty the list of multicast groups that we may have joined
1200 * while we were alive from the parent's list.
1202 bzero((char *)&sdl, sizeof(sdl));
1203 sdl.sdl_len = sizeof(sdl);
1204 sdl.sdl_family = AF_LINK;
1205 sdl.sdl_index = parent->if_index;
1206 sdl.sdl_type = IFT_ETHER;
1207 sdl.sdl_alen = ETHER_ADDR_LEN;
1209 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1210 bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
1214 * If the parent interface is being detached,
1215 * all its multicast addresses have already
1216 * been removed. Warn about errors if
1217 * if_delmulti() does fail, but don't abort as
1218 * all callers expect vlan destruction to
1222 error = if_delmulti(parent,
1223 (struct sockaddr *)&sdl);
1226 "Failed to delete multicast address from parent: %d\n",
1229 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1233 vlan_setflags(ifp, 0); /* clear special flags on parent */
1235 trunk->vlans[ifv->ifv_tag] = NULL;
1238 vlan_remhash(trunk, ifv);
1240 ifv->ifv_trunk = NULL;
1243 * Check if we were the last.
1245 if (trunk->refcnt == 0) {
1246 trunk->parent->if_vlantrunk = NULL;
1248 * XXXGL: If some ithread has already entered
1249 * vlan_input() and is now blocked on the trunk
1250 * lock, then it should preempt us right after
1251 * unlock and finish its work. Then we will acquire
1252 * lock again in trunk_destroy().
1254 TRUNK_UNLOCK(trunk);
1255 trunk_destroy(trunk);
1257 TRUNK_UNLOCK(trunk);
1260 /* Disconnect from parent. */
1261 if (ifv->ifv_pflags)
1262 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1263 ifp->if_mtu = ETHERMTU;
1264 ifp->if_link_state = LINK_STATE_UNKNOWN;
1265 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1268 * Only dispatch an event if vlan was
1269 * attached, otherwise there is nothing
1270 * to cleanup anyway.
1273 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag);
1276 /* Handle a reference counted flag that should be set on the parent as well */
1278 vlan_setflag(struct ifnet *ifp, int flag, int status,
1279 int (*func)(struct ifnet *, int))
1284 /* XXX VLAN_LOCK_ASSERT(); */
1286 ifv = ifp->if_softc;
1287 status = status ? (ifp->if_flags & flag) : 0;
1288 /* Now "status" contains the flag value or 0 */
1291 * See if recorded parent's status is different from what
1292 * we want it to be. If it is, flip it. We record parent's
1293 * status in ifv_pflags so that we won't clear parent's flag
1294 * we haven't set. In fact, we don't clear or set parent's
1295 * flags directly, but get or release references to them.
1296 * That's why we can be sure that recorded flags still are
1297 * in accord with actual parent's flags.
1299 if (status != (ifv->ifv_pflags & flag)) {
1300 error = (*func)(PARENT(ifv), status);
1303 ifv->ifv_pflags &= ~flag;
1304 ifv->ifv_pflags |= status;
1310 * Handle IFF_* flags that require certain changes on the parent:
1311 * if "status" is true, update parent's flags respective to our if_flags;
1312 * if "status" is false, forcedly clear the flags set on parent.
1315 vlan_setflags(struct ifnet *ifp, int status)
1319 for (i = 0; vlan_pflags[i].flag; i++) {
1320 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1321 status, vlan_pflags[i].func);
1328 /* Inform all vlans that their parent has changed link state */
1330 vlan_link_state(struct ifnet *ifp, int link)
1332 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1338 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1339 if (trunk->vlans[i] != NULL) {
1340 ifv = trunk->vlans[i];
1342 for (i = 0; i < (1 << trunk->hwidth); i++)
1343 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
1345 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1346 if_link_state_change(ifv->ifv_ifp,
1347 trunk->parent->if_link_state);
1349 TRUNK_UNLOCK(trunk);
1353 vlan_capabilities(struct ifvlan *ifv)
1355 struct ifnet *p = PARENT(ifv);
1356 struct ifnet *ifp = ifv->ifv_ifp;
1358 TRUNK_LOCK_ASSERT(TRUNK(ifv));
1361 * If the parent interface can do checksum offloading
1362 * on VLANs, then propagate its hardware-assisted
1363 * checksumming flags. Also assert that checksum
1364 * offloading requires hardware VLAN tagging.
1366 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1367 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1369 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1370 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1371 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1372 ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP |
1373 CSUM_UDP | CSUM_SCTP | CSUM_IP_FRAGS | CSUM_FRAGMENT);
1375 ifp->if_capenable = 0;
1376 ifp->if_hwassist = 0;
1379 * If the parent interface can do TSO on VLANs then
1380 * propagate the hardware-assisted flag. TSO on VLANs
1381 * does not necessarily require hardware VLAN tagging.
1383 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1384 ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO;
1385 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1386 ifp->if_capenable |= p->if_capenable & IFCAP_TSO;
1387 ifp->if_hwassist |= p->if_hwassist & CSUM_TSO;
1389 ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO);
1390 ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO);
1395 vlan_trunk_capabilities(struct ifnet *ifp)
1397 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1403 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1404 if (trunk->vlans[i] != NULL) {
1405 ifv = trunk->vlans[i];
1407 for (i = 0; i < (1 << trunk->hwidth); i++) {
1408 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1410 vlan_capabilities(ifv);
1412 TRUNK_UNLOCK(trunk);
1416 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1424 ifr = (struct ifreq *)data;
1425 ifv = ifp->if_softc;
1430 if (TRUNK(ifv) != NULL) {
1433 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1434 /* Limit the result to the parent's current config. */
1436 struct ifmediareq *ifmr;
1438 ifmr = (struct ifmediareq *)data;
1439 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1440 ifmr->ifm_count = 1;
1441 error = copyout(&ifmr->ifm_current,
1458 * Set the interface MTU.
1461 if (TRUNK(ifv) != NULL) {
1463 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1465 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1468 ifp->if_mtu = ifr->ifr_mtu;
1476 if (ifp->if_vnet != ifp->if_home_vnet) {
1481 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1484 if (vlr.vlr_parent[0] == '\0') {
1488 p = ifunit(vlr.vlr_parent);
1494 * Don't let the caller set up a VLAN tag with
1495 * anything except VLID bits.
1497 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1501 error = vlan_config(ifv, p, vlr.vlr_tag);
1505 /* Update flags on the parent, if necessary. */
1506 vlan_setflags(ifp, 1);
1511 if (ifp->if_vnet != ifp->if_home_vnet) {
1516 bzero(&vlr, sizeof(vlr));
1518 if (TRUNK(ifv) != NULL) {
1519 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1520 sizeof(vlr.vlr_parent));
1521 vlr.vlr_tag = ifv->ifv_tag;
1524 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1529 * We should propagate selected flags to the parent,
1530 * e.g., promiscuous mode.
1532 if (TRUNK(ifv) != NULL)
1533 error = vlan_setflags(ifp, 1);
1539 * If we don't have a parent, just remember the membership for
1542 if (TRUNK(ifv) != NULL)
1543 error = vlan_setmulti(ifp);
1547 error = ether_ioctl(ifp, cmd, data);