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() left on our output queue when it calls
38 * if_start(), rewrite them for use by the real outgoing interface,
39 * and ask it to send them.
42 #include <sys/cdefs.h>
43 __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>
62 #include <net/ethernet.h>
64 #include <net/if_clone.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/if_vlan_var.h>
70 #define VLANNAME "vlan"
71 #define VLAN_DEF_HWIDTH 4
72 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
74 #define UP_AND_RUNNING(ifp) \
75 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
77 LIST_HEAD(ifvlanhead, ifvlan);
80 struct ifnet *parent; /* parent interface of this trunk */
83 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
84 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
86 struct ifvlanhead *hash; /* dynamic hash-list table */
93 struct vlan_mc_entry {
94 struct sockaddr_dl mc_addr;
95 SLIST_ENTRY(vlan_mc_entry) mc_entries;
99 struct ifvlantrunk *ifv_trunk;
100 struct ifnet *ifv_ifp;
102 #define TRUNK(ifv) ((ifv)->ifv_trunk)
103 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
104 int ifv_pflags; /* special flags we have set on parent */
106 int ifvm_encaplen; /* encapsulation length */
107 int ifvm_mtufudge; /* MTU fudged by this much */
108 int ifvm_mintu; /* min transmission unit */
109 uint16_t ifvm_proto; /* encapsulation ethertype */
110 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
112 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
114 LIST_ENTRY(ifvlan) ifv_list;
117 #define ifv_proto ifv_mib.ifvm_proto
118 #define ifv_tag ifv_mib.ifvm_tag
119 #define ifv_encaplen ifv_mib.ifvm_encaplen
120 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
121 #define ifv_mintu ifv_mib.ifvm_mintu
123 /* Special flags we should propagate to parent. */
126 int (*func)(struct ifnet *, int);
128 {IFF_PROMISC, ifpromisc},
129 {IFF_ALLMULTI, if_allmulti},
133 SYSCTL_DECL(_net_link);
134 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
135 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
137 static int soft_pad = 0;
138 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
139 "pad short frames before tagging");
141 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
143 static eventhandler_tag ifdetach_tag;
144 static eventhandler_tag iflladdr_tag;
147 * We have a global mutex, that is used to serialize configuration
148 * changes and isn't used in normal packet delivery.
150 * We also have a per-trunk rwlock, that is locked shared on packet
151 * processing and exclusive when configuration is changed.
153 * The VLAN_ARRAY substitutes the dynamic hash with a static array
154 * with 4096 entries. In theory this can give a boost in processing,
155 * however on practice it does not. Probably this is because array
156 * is too big to fit into CPU cache.
158 static struct sx ifv_lock;
159 #define VLAN_LOCK_INIT() sx_init(&ifv_lock, "vlan_global")
160 #define VLAN_LOCK_DESTROY() sx_destroy(&ifv_lock)
161 #define VLAN_LOCK_ASSERT() sx_assert(&ifv_lock, SA_LOCKED)
162 #define VLAN_LOCK() sx_xlock(&ifv_lock)
163 #define VLAN_UNLOCK() sx_xunlock(&ifv_lock)
164 #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME)
165 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
166 #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw)
167 #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw)
168 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
169 #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw)
170 #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw)
171 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
174 static void vlan_inithash(struct ifvlantrunk *trunk);
175 static void vlan_freehash(struct ifvlantrunk *trunk);
176 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
177 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
178 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
179 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
182 static void trunk_destroy(struct ifvlantrunk *trunk);
184 static void vlan_start(struct ifnet *ifp);
185 static void vlan_init(void *foo);
186 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
187 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
188 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
189 int (*func)(struct ifnet *, int));
190 static int vlan_setflags(struct ifnet *ifp, int status);
191 static int vlan_setmulti(struct ifnet *ifp);
192 static void vlan_unconfig(struct ifnet *ifp);
193 static void vlan_unconfig_locked(struct ifnet *ifp);
194 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
195 static void vlan_link_state(struct ifnet *ifp);
196 static void vlan_capabilities(struct ifvlan *ifv);
197 static void vlan_trunk_capabilities(struct ifnet *ifp);
199 static struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
200 const char *, int *);
201 static int vlan_clone_match(struct if_clone *, const char *);
202 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
203 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
205 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
206 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
208 static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
209 IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
212 static VNET_DEFINE(struct if_clone, vlan_cloner);
213 #define V_vlan_cloner VNET(vlan_cloner)
217 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
220 vlan_inithash(struct ifvlantrunk *trunk)
225 * The trunk must not be locked here since we call malloc(M_WAITOK).
226 * It is OK in case this function is called before the trunk struct
227 * gets hooked up and becomes visible from other threads.
230 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
231 ("%s: hash already initialized", __func__));
233 trunk->hwidth = VLAN_DEF_HWIDTH;
234 n = 1 << trunk->hwidth;
235 trunk->hmask = n - 1;
236 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
237 for (i = 0; i < n; i++)
238 LIST_INIT(&trunk->hash[i]);
242 vlan_freehash(struct ifvlantrunk *trunk)
247 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
248 for (i = 0; i < (1 << trunk->hwidth); i++)
249 KASSERT(LIST_EMPTY(&trunk->hash[i]),
250 ("%s: hash table not empty", __func__));
252 free(trunk->hash, M_VLAN);
254 trunk->hwidth = trunk->hmask = 0;
258 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
263 TRUNK_LOCK_ASSERT(trunk);
264 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
266 b = 1 << trunk->hwidth;
267 i = HASH(ifv->ifv_tag, trunk->hmask);
268 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
269 if (ifv->ifv_tag == ifv2->ifv_tag)
273 * Grow the hash when the number of vlans exceeds half of the number of
274 * hash buckets squared. This will make the average linked-list length
277 if (trunk->refcnt > (b * b) / 2) {
278 vlan_growhash(trunk, 1);
279 i = HASH(ifv->ifv_tag, trunk->hmask);
281 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
288 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
293 TRUNK_LOCK_ASSERT(trunk);
294 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
296 b = 1 << trunk->hwidth;
297 i = HASH(ifv->ifv_tag, trunk->hmask);
298 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
301 LIST_REMOVE(ifv2, ifv_list);
302 if (trunk->refcnt < (b * b) / 2)
303 vlan_growhash(trunk, -1);
307 panic("%s: vlan not found\n", __func__);
308 return (ENOENT); /*NOTREACHED*/
312 * Grow the hash larger or smaller if memory permits.
315 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
318 struct ifvlanhead *hash2;
319 int hwidth2, i, j, n, n2;
321 TRUNK_LOCK_ASSERT(trunk);
322 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
325 /* Harmless yet obvious coding error */
326 printf("%s: howmuch is 0\n", __func__);
330 hwidth2 = trunk->hwidth + howmuch;
331 n = 1 << trunk->hwidth;
333 /* Do not shrink the table below the default */
334 if (hwidth2 < VLAN_DEF_HWIDTH)
337 /* M_NOWAIT because we're called with trunk mutex held */
338 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
340 printf("%s: out of memory -- hash size not changed\n",
342 return; /* We can live with the old hash table */
344 for (j = 0; j < n2; j++)
345 LIST_INIT(&hash2[j]);
346 for (i = 0; i < n; i++)
347 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
348 LIST_REMOVE(ifv, ifv_list);
349 j = HASH(ifv->ifv_tag, n2 - 1);
350 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
352 free(trunk->hash, M_VLAN);
354 trunk->hwidth = hwidth2;
355 trunk->hmask = n2 - 1;
358 if_printf(trunk->parent,
359 "VLAN hash table resized from %d to %d buckets\n", n, n2);
362 static __inline struct ifvlan *
363 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
367 TRUNK_LOCK_RASSERT(trunk);
369 LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
370 if (ifv->ifv_tag == tag)
376 /* Debugging code to view the hashtables. */
378 vlan_dumphash(struct ifvlantrunk *trunk)
383 for (i = 0; i < (1 << trunk->hwidth); i++) {
385 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
386 printf("%s ", ifv->ifv_ifp->if_xname);
393 static __inline struct ifvlan *
394 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
397 return trunk->vlans[tag];
401 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
404 if (trunk->vlans[ifv->ifv_tag] != NULL)
406 trunk->vlans[ifv->ifv_tag] = ifv;
413 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
416 trunk->vlans[ifv->ifv_tag] = NULL;
423 vlan_freehash(struct ifvlantrunk *trunk)
428 vlan_inithash(struct ifvlantrunk *trunk)
432 #endif /* !VLAN_ARRAY */
435 trunk_destroy(struct ifvlantrunk *trunk)
440 vlan_freehash(trunk);
441 trunk->parent->if_vlantrunk = NULL;
443 TRUNK_LOCK_DESTROY(trunk);
448 * Program our multicast filter. What we're actually doing is
449 * programming the multicast filter of the parent. This has the
450 * side effect of causing the parent interface to receive multicast
451 * traffic that it doesn't really want, which ends up being discarded
452 * later by the upper protocol layers. Unfortunately, there's no way
453 * to avoid this: there really is only one physical interface.
455 * XXX: There is a possible race here if more than one thread is
456 * modifying the multicast state of the vlan interface at the same time.
459 vlan_setmulti(struct ifnet *ifp)
462 struct ifmultiaddr *ifma, *rifma = NULL;
464 struct vlan_mc_entry *mc;
467 /*VLAN_LOCK_ASSERT();*/
469 /* Find the parent. */
473 CURVNET_SET_QUIET(ifp_p->if_vnet);
475 /* First, remove any existing filter entries. */
476 while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
477 error = if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
480 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
484 /* Now program new ones. */
485 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
486 if (ifma->ifma_addr->sa_family != AF_LINK)
488 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
491 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
492 mc->mc_addr.sdl_index = ifp_p->if_index;
493 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
494 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
505 * A handler for parent interface link layer address changes.
506 * If the parent interface link layer address is changed we
507 * should also change it on all children vlans.
510 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
519 * Check if it's a trunk interface first of all
520 * to avoid needless locking.
522 if (ifp->if_vlantrunk == NULL)
527 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
530 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
531 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
532 #else /* VLAN_ARRAY */
533 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
534 LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) {
535 #endif /* VLAN_ARRAY */
537 if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp),
546 * A handler for network interface departure events.
547 * Track departure of trunks here so that we don't access invalid
548 * pointers or whatever if a trunk is ripped from under us, e.g.,
549 * by ejecting its hot-plug card. However, if an ifnet is simply
550 * being renamed, then there's no need to tear down the state.
553 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
559 * Check if it's a trunk interface first of all
560 * to avoid needless locking.
562 if (ifp->if_vlantrunk == NULL)
565 /* If the ifnet is just being renamed, don't do anything. */
566 if (ifp->if_flags & IFF_RENAMING)
571 * OK, it's a trunk. Loop over and detach all vlan's on it.
572 * Check trunk pointer after each vlan_unconfig() as it will
573 * free it and set to NULL after the last vlan was detached.
576 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
577 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
578 vlan_unconfig_locked(ifv->ifv_ifp);
579 if (ifp->if_vlantrunk == NULL)
582 #else /* VLAN_ARRAY */
584 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
585 if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
586 vlan_unconfig_locked(ifv->ifv_ifp);
587 if (ifp->if_vlantrunk)
588 goto restart; /* trunk->hwidth can change */
592 #endif /* VLAN_ARRAY */
593 /* Trunk should have been destroyed in vlan_unconfig(). */
594 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
599 * Return the trunk device for a virtual interface.
601 static struct ifnet *
602 vlan_trunkdev(struct ifnet *ifp)
606 if (ifp->if_type != IFT_L2VLAN)
618 * Return the 16bit vlan tag for this interface.
621 vlan_tag(struct ifnet *ifp, uint16_t *tagp)
625 if (ifp->if_type != IFT_L2VLAN)
628 *tagp = ifv->ifv_tag;
633 * Return a driver specific cookie for this interface. Synchronization
634 * with setcookie must be provided by the driver.
637 vlan_cookie(struct ifnet *ifp)
641 if (ifp->if_type != IFT_L2VLAN)
644 return (ifv->ifv_cookie);
648 * Store a cookie in our softc that drivers can use to store driver
649 * private per-instance data in.
652 vlan_setcookie(struct ifnet *ifp, void *cookie)
656 if (ifp->if_type != IFT_L2VLAN)
659 ifv->ifv_cookie = cookie;
664 * Return the vlan device present at the specific tag.
666 static struct ifnet *
667 vlan_devat(struct ifnet *ifp, uint16_t tag)
669 struct ifvlantrunk *trunk;
672 trunk = ifp->if_vlantrunk;
677 ifv = vlan_gethash(trunk, tag);
680 TRUNK_RUNLOCK(trunk);
685 * VLAN support can be loaded as a module. The only place in the
686 * system that's intimately aware of this is ether_input. We hook
687 * into this code through vlan_input_p which is defined there and
688 * set here. Noone else in the system should be aware of this so
689 * we use an explicit reference here.
691 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
693 /* For if_link_state_change() eyes only... */
694 extern void (*vlan_link_state_p)(struct ifnet *);
697 vlan_modevent(module_t mod, int type, void *data)
702 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
703 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
704 if (ifdetach_tag == NULL)
706 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
707 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
708 if (iflladdr_tag == NULL)
711 vlan_input_p = vlan_input;
712 vlan_link_state_p = vlan_link_state;
713 vlan_trunk_cap_p = vlan_trunk_capabilities;
714 vlan_trunkdev_p = vlan_trunkdev;
715 vlan_cookie_p = vlan_cookie;
716 vlan_setcookie_p = vlan_setcookie;
717 vlan_tag_p = vlan_tag;
718 vlan_devat_p = vlan_devat;
720 if_clone_attach(&vlan_cloner);
723 printf("vlan: initialized, using "
727 "hash tables with chaining"
734 if_clone_detach(&vlan_cloner);
736 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
737 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
739 vlan_link_state_p = NULL;
740 vlan_trunk_cap_p = NULL;
741 vlan_trunkdev_p = NULL;
743 vlan_cookie_p = vlan_cookie;
744 vlan_setcookie_p = vlan_setcookie;
748 printf("vlan: unloaded\n");
756 static moduledata_t vlan_mod = {
762 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
763 MODULE_VERSION(if_vlan, 3);
767 vnet_vlan_init(const void *unused __unused)
770 V_vlan_cloner = vlan_cloner;
771 if_clone_attach(&V_vlan_cloner);
773 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
774 vnet_vlan_init, NULL);
777 vnet_vlan_uninit(const void *unused __unused)
780 if_clone_detach(&V_vlan_cloner);
782 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
783 vnet_vlan_uninit, NULL);
786 static struct ifnet *
787 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
793 /* Check for <etherif>.<vlan> style interface names. */
794 IFNET_RLOCK_NOSLEEP();
795 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
797 * We can handle non-ethernet hardware types as long as
798 * they handle the tagging and headers themselves.
800 if (ifp->if_type != IFT_ETHER &&
801 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
803 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
805 cp = name + strlen(ifp->if_xname);
811 for(; *cp >= '0' && *cp <= '9'; cp++)
812 t = (t * 10) + (*cp - '0');
819 IFNET_RUNLOCK_NOSLEEP();
825 vlan_clone_match(struct if_clone *ifc, const char *name)
829 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
832 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
834 for (cp = name + 4; *cp != '\0'; cp++) {
835 if (*cp < '0' || *cp > '9')
843 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
855 struct sockaddr_dl *sdl;
857 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
860 * There are 3 (ugh) ways to specify the cloned device:
861 * o pass a parameter block with the clone request.
862 * o specify parameters in the text of the clone device name
863 * o specify no parameters and get an unattached device that
864 * must be configured separately.
865 * The first technique is preferred; the latter two are
866 * supported for backwards compatibilty.
869 error = copyin(params, &vlr, sizeof(vlr));
872 p = ifunit(vlr.vlr_parent);
876 * Don't let the caller set up a VLAN tag with
877 * anything except VLID bits.
879 if (vlr.vlr_tag & ~EVL_VLID_MASK)
881 error = ifc_name2unit(name, &unit);
887 wildcard = (unit < 0);
888 } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
894 * Don't let the caller set up a VLAN tag with
895 * anything except VLID bits.
897 if (tag & ~EVL_VLID_MASK)
902 error = ifc_name2unit(name, &unit);
906 wildcard = (unit < 0);
909 error = ifc_alloc_unit(ifc, &unit);
913 /* In the wildcard case, we need to update the name. */
915 for (dp = name; *dp != '\0'; dp++);
916 if (snprintf(dp, len - (dp-name), "%d", unit) >
917 len - (dp-name) - 1) {
918 panic("%s: interface name too long", __func__);
922 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
923 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
925 ifc_free_unit(ifc, unit);
929 SLIST_INIT(&ifv->vlan_mc_listhead);
933 * Set the name manually rather than using if_initname because
934 * we don't conform to the default naming convention for interfaces.
936 strlcpy(ifp->if_xname, name, IFNAMSIZ);
937 ifp->if_dname = ifc->ifc_name;
938 ifp->if_dunit = unit;
939 /* NB: flags are not set here */
940 ifp->if_linkmib = &ifv->ifv_mib;
941 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
942 /* NB: mtu is not set here */
944 ifp->if_init = vlan_init;
945 ifp->if_start = vlan_start;
946 ifp->if_ioctl = vlan_ioctl;
947 ifp->if_snd.ifq_maxlen = ifqmaxlen;
948 ifp->if_flags = VLAN_IFFLAGS;
949 ether_ifattach(ifp, eaddr);
950 /* Now undo some of the damage... */
951 ifp->if_baudrate = 0;
952 ifp->if_type = IFT_L2VLAN;
953 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
955 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
956 sdl->sdl_type = IFT_L2VLAN;
959 error = vlan_config(ifv, p, tag);
962 * Since we've partialy failed, we need to back
963 * out all the way, otherwise userland could get
964 * confused. Thus, we destroy the interface.
968 if_free_type(ifp, IFT_ETHER);
969 ifc_free_unit(ifc, unit);
975 /* Update flags on the parent, if necessary. */
976 vlan_setflags(ifp, 1);
983 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
985 struct ifvlan *ifv = ifp->if_softc;
986 int unit = ifp->if_dunit;
988 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
989 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
990 if_free_type(ifp, IFT_ETHER);
992 ifc_free_unit(ifc, unit);
998 * The ifp->if_init entry point for vlan(4) is a no-op.
1001 vlan_init(void *foo __unused)
1006 * The if_start method for vlan(4) interface. It doesn't
1007 * raises the IFF_DRV_OACTIVE flag, since it is called
1008 * only from IFQ_HANDOFF() macro in ether_output_frame().
1009 * If the interface queue is full, and vlan_start() is
1010 * not called, the queue would never get emptied and
1011 * interface would stall forever.
1014 vlan_start(struct ifnet *ifp)
1021 ifv = ifp->if_softc;
1025 IF_DEQUEUE(&ifp->if_snd, m);
1031 * Do not run parent's if_start() if the parent is not up,
1032 * or parent's driver will cause a system crash.
1034 if (!UP_AND_RUNNING(p)) {
1036 ifp->if_collisions++;
1041 * Pad the frame to the minimum size allowed if told to.
1042 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
1043 * paragraph C.4.4.3.b. It can help to work around buggy
1044 * bridges that violate paragraph C.4.4.3.a from the same
1045 * document, i.e., fail to pad short frames after untagging.
1046 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
1047 * untagging it will produce a 62-byte frame, which is a runt
1048 * and requires padding. There are VLAN-enabled network
1049 * devices that just discard such runts instead or mishandle
1052 if (soft_pad && p->if_type == IFT_ETHER) {
1053 static char pad[8]; /* just zeros */
1056 for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
1057 n > 0; n -= sizeof(pad))
1058 if (!m_append(m, min(n, sizeof(pad)), pad))
1062 if_printf(ifp, "cannot pad short frame\n");
1070 * If underlying interface can do VLAN tag insertion itself,
1071 * just pass the packet along. However, we need some way to
1072 * tell the interface where the packet came from so that it
1073 * knows how to find the VLAN tag to use, so we attach a
1074 * packet tag that holds it.
1076 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1077 m->m_pkthdr.ether_vtag = ifv->ifv_tag;
1078 m->m_flags |= M_VLANTAG;
1080 m = ether_vlanencap(m, ifv->ifv_tag);
1083 "unable to prepend VLAN header\n");
1090 * Send it, precisely as ether_output() would have.
1091 * We are already running at splimp.
1093 error = (p->if_transmit)(p, m);
1102 vlan_input(struct ifnet *ifp, struct mbuf *m)
1104 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1108 KASSERT(trunk != NULL, ("%s: no trunk", __func__));
1110 if (m->m_flags & M_VLANTAG) {
1112 * Packet is tagged, but m contains a normal
1113 * Ethernet frame; the tag is stored out-of-band.
1115 tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
1116 m->m_flags &= ~M_VLANTAG;
1118 struct ether_vlan_header *evl;
1121 * Packet is tagged in-band as specified by 802.1q.
1123 switch (ifp->if_type) {
1125 if (m->m_len < sizeof(*evl) &&
1126 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1127 if_printf(ifp, "cannot pullup VLAN header\n");
1130 evl = mtod(m, struct ether_vlan_header *);
1131 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
1134 * Remove the 802.1q header by copying the Ethernet
1135 * addresses over it and adjusting the beginning of
1136 * the data in the mbuf. The encapsulated Ethernet
1137 * type field is already in place.
1139 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1140 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1141 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1146 panic("%s: %s has unsupported if_type %u",
1147 __func__, ifp->if_xname, ifp->if_type);
1156 ifv = vlan_gethash(trunk, tag);
1157 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1158 TRUNK_RUNLOCK(trunk);
1163 TRUNK_RUNLOCK(trunk);
1165 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1166 ifv->ifv_ifp->if_ipackets++;
1168 /* Pass it back through the parent's input routine. */
1169 (*ifp->if_input)(ifv->ifv_ifp, m);
1173 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
1175 struct ifvlantrunk *trunk;
1179 /* VID numbers 0x0 and 0xFFF are reserved */
1180 if (tag == 0 || tag == 0xFFF)
1182 if (p->if_type != IFT_ETHER &&
1183 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1184 return (EPROTONOSUPPORT);
1185 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1186 return (EPROTONOSUPPORT);
1190 if (p->if_vlantrunk == NULL) {
1191 trunk = malloc(sizeof(struct ifvlantrunk),
1192 M_VLAN, M_WAITOK | M_ZERO);
1193 vlan_inithash(trunk);
1195 if (p->if_vlantrunk != NULL) {
1196 /* A race that that is very unlikely to be hit. */
1197 vlan_freehash(trunk);
1198 free(trunk, M_VLAN);
1201 TRUNK_LOCK_INIT(trunk);
1203 p->if_vlantrunk = trunk;
1208 trunk = p->if_vlantrunk;
1212 ifv->ifv_tag = tag; /* must set this before vlan_inshash() */
1213 error = vlan_inshash(trunk, ifv);
1216 ifv->ifv_proto = ETHERTYPE_VLAN;
1217 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1218 ifv->ifv_mintu = ETHERMIN;
1219 ifv->ifv_pflags = 0;
1222 * If the parent supports the VLAN_MTU capability,
1223 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1226 if (p->if_capenable & IFCAP_VLAN_MTU) {
1228 * No need to fudge the MTU since the parent can
1229 * handle extended frames.
1231 ifv->ifv_mtufudge = 0;
1234 * Fudge the MTU by the encapsulation size. This
1235 * makes us incompatible with strictly compliant
1236 * 802.1Q implementations, but allows us to use
1237 * the feature with other NetBSD implementations,
1238 * which might still be useful.
1240 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1243 ifv->ifv_trunk = trunk;
1246 * Initialize fields from our parent. This duplicates some
1247 * work with ether_ifattach() but allows for non-ethernet
1248 * interfaces to also work.
1250 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1251 ifp->if_baudrate = p->if_baudrate;
1252 ifp->if_output = p->if_output;
1253 ifp->if_input = p->if_input;
1254 ifp->if_resolvemulti = p->if_resolvemulti;
1255 ifp->if_addrlen = p->if_addrlen;
1256 ifp->if_broadcastaddr = p->if_broadcastaddr;
1259 * Copy only a selected subset of flags from the parent.
1260 * Other flags are none of our business.
1262 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1263 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1264 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1265 #undef VLAN_COPY_FLAGS
1267 ifp->if_link_state = p->if_link_state;
1269 vlan_capabilities(ifv);
1272 * Set up our interface address to reflect the underlying
1273 * physical interface's.
1275 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1276 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1280 * Configure multicast addresses that may already be
1281 * joined on the vlan device.
1283 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1285 /* We are ready for operation now. */
1286 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1288 TRUNK_UNLOCK(trunk);
1290 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag);
1297 vlan_unconfig(struct ifnet *ifp)
1301 vlan_unconfig_locked(ifp);
1306 vlan_unconfig_locked(struct ifnet *ifp)
1308 struct ifvlantrunk *trunk;
1309 struct vlan_mc_entry *mc;
1311 struct ifnet *parent;
1315 ifv = ifp->if_softc;
1316 trunk = ifv->ifv_trunk;
1319 if (trunk != NULL) {
1322 parent = trunk->parent;
1325 * Since the interface is being unconfigured, we need to
1326 * empty the list of multicast groups that we may have joined
1327 * while we were alive from the parent's list.
1329 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1331 * This may fail if the parent interface is
1332 * being detached. Regardless, we should do a
1333 * best effort to free this interface as much
1334 * as possible as all callers expect vlan
1335 * destruction to succeed.
1337 (void)if_delmulti(parent,
1338 (struct sockaddr *)&mc->mc_addr);
1339 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1343 vlan_setflags(ifp, 0); /* clear special flags on parent */
1344 vlan_remhash(trunk, ifv);
1345 ifv->ifv_trunk = NULL;
1348 * Check if we were the last.
1350 if (trunk->refcnt == 0) {
1351 trunk->parent->if_vlantrunk = NULL;
1353 * XXXGL: If some ithread has already entered
1354 * vlan_input() and is now blocked on the trunk
1355 * lock, then it should preempt us right after
1356 * unlock and finish its work. Then we will acquire
1357 * lock again in trunk_destroy().
1359 TRUNK_UNLOCK(trunk);
1360 trunk_destroy(trunk);
1362 TRUNK_UNLOCK(trunk);
1365 /* Disconnect from parent. */
1366 if (ifv->ifv_pflags)
1367 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1368 ifp->if_mtu = ETHERMTU;
1369 ifp->if_link_state = LINK_STATE_UNKNOWN;
1370 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1373 * Only dispatch an event if vlan was
1374 * attached, otherwise there is nothing
1375 * to cleanup anyway.
1378 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag);
1381 /* Handle a reference counted flag that should be set on the parent as well */
1383 vlan_setflag(struct ifnet *ifp, int flag, int status,
1384 int (*func)(struct ifnet *, int))
1389 /* XXX VLAN_LOCK_ASSERT(); */
1391 ifv = ifp->if_softc;
1392 status = status ? (ifp->if_flags & flag) : 0;
1393 /* Now "status" contains the flag value or 0 */
1396 * See if recorded parent's status is different from what
1397 * we want it to be. If it is, flip it. We record parent's
1398 * status in ifv_pflags so that we won't clear parent's flag
1399 * we haven't set. In fact, we don't clear or set parent's
1400 * flags directly, but get or release references to them.
1401 * That's why we can be sure that recorded flags still are
1402 * in accord with actual parent's flags.
1404 if (status != (ifv->ifv_pflags & flag)) {
1405 error = (*func)(PARENT(ifv), status);
1408 ifv->ifv_pflags &= ~flag;
1409 ifv->ifv_pflags |= status;
1415 * Handle IFF_* flags that require certain changes on the parent:
1416 * if "status" is true, update parent's flags respective to our if_flags;
1417 * if "status" is false, forcedly clear the flags set on parent.
1420 vlan_setflags(struct ifnet *ifp, int status)
1424 for (i = 0; vlan_pflags[i].flag; i++) {
1425 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1426 status, vlan_pflags[i].func);
1433 /* Inform all vlans that their parent has changed link state */
1435 vlan_link_state(struct ifnet *ifp)
1437 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1443 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1444 if (trunk->vlans[i] != NULL) {
1445 ifv = trunk->vlans[i];
1447 for (i = 0; i < (1 << trunk->hwidth); i++)
1448 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
1450 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1451 if_link_state_change(ifv->ifv_ifp,
1452 trunk->parent->if_link_state);
1454 TRUNK_UNLOCK(trunk);
1458 vlan_capabilities(struct ifvlan *ifv)
1460 struct ifnet *p = PARENT(ifv);
1461 struct ifnet *ifp = ifv->ifv_ifp;
1463 TRUNK_LOCK_ASSERT(TRUNK(ifv));
1466 * If the parent interface can do checksum offloading
1467 * on VLANs, then propagate its hardware-assisted
1468 * checksumming flags. Also assert that checksum
1469 * offloading requires hardware VLAN tagging.
1471 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1472 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1474 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1475 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1476 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1477 ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP |
1478 CSUM_UDP | CSUM_SCTP | CSUM_IP_FRAGS | CSUM_FRAGMENT);
1480 ifp->if_capenable = 0;
1481 ifp->if_hwassist = 0;
1484 * If the parent interface can do TSO on VLANs then
1485 * propagate the hardware-assisted flag. TSO on VLANs
1486 * does not necessarily require hardware VLAN tagging.
1488 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1489 ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO;
1490 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1491 ifp->if_capenable |= p->if_capenable & IFCAP_TSO;
1492 ifp->if_hwassist |= p->if_hwassist & CSUM_TSO;
1494 ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO);
1495 ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO);
1500 vlan_trunk_capabilities(struct ifnet *ifp)
1502 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1508 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1509 if (trunk->vlans[i] != NULL) {
1510 ifv = trunk->vlans[i];
1512 for (i = 0; i < (1 << trunk->hwidth); i++) {
1513 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1515 vlan_capabilities(ifv);
1517 TRUNK_UNLOCK(trunk);
1521 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1530 ifr = (struct ifreq *)data;
1531 ifa = (struct ifaddr *) data;
1532 ifv = ifp->if_softc;
1536 ifp->if_flags |= IFF_UP;
1538 if (ifa->ifa_addr->sa_family == AF_INET)
1539 arp_ifinit(ifp, ifa);
1544 struct sockaddr *sa;
1546 sa = (struct sockaddr *)&ifr->ifr_data;
1547 bcopy(IF_LLADDR(ifp), sa->sa_data, ifp->if_addrlen);
1552 if (TRUNK(ifv) != NULL) {
1555 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1556 /* Limit the result to the parent's current config. */
1558 struct ifmediareq *ifmr;
1560 ifmr = (struct ifmediareq *)data;
1561 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1562 ifmr->ifm_count = 1;
1563 error = copyout(&ifmr->ifm_current,
1580 * Set the interface MTU.
1583 if (TRUNK(ifv) != NULL) {
1585 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1587 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1590 ifp->if_mtu = ifr->ifr_mtu;
1598 if (ifp->if_vnet != ifp->if_home_vnet) {
1603 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1606 if (vlr.vlr_parent[0] == '\0') {
1610 p = ifunit(vlr.vlr_parent);
1616 * Don't let the caller set up a VLAN tag with
1617 * anything except VLID bits.
1619 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1623 error = vlan_config(ifv, p, vlr.vlr_tag);
1627 /* Update flags on the parent, if necessary. */
1628 vlan_setflags(ifp, 1);
1633 if (ifp->if_vnet != ifp->if_home_vnet) {
1638 bzero(&vlr, sizeof(vlr));
1640 if (TRUNK(ifv) != NULL) {
1641 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1642 sizeof(vlr.vlr_parent));
1643 vlr.vlr_tag = ifv->ifv_tag;
1646 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1651 * We should propagate selected flags to the parent,
1652 * e.g., promiscuous mode.
1654 if (TRUNK(ifv) != NULL)
1655 error = vlan_setflags(ifp, 1);
1661 * If we don't have a parent, just remember the membership for
1664 if (TRUNK(ifv) != NULL)
1665 error = vlan_setmulti(ifp);