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>
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>
71 #include <netinet/in.h>
72 #include <netinet/if_ether.h>
75 #define VLANNAME "vlan"
76 #define VLAN_DEF_HWIDTH 4
77 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
79 #define UP_AND_RUNNING(ifp) \
80 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
82 LIST_HEAD(ifvlanhead, ifvlan);
85 struct ifnet *parent; /* parent interface of this trunk */
88 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
89 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
91 struct ifvlanhead *hash; /* dynamic hash-list table */
98 struct vlan_mc_entry {
99 struct sockaddr_dl mc_addr;
100 SLIST_ENTRY(vlan_mc_entry) mc_entries;
104 struct ifvlantrunk *ifv_trunk;
105 struct ifnet *ifv_ifp;
107 #define TRUNK(ifv) ((ifv)->ifv_trunk)
108 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
109 int ifv_pflags; /* special flags we have set on parent */
111 int ifvm_encaplen; /* encapsulation length */
112 int ifvm_mtufudge; /* MTU fudged by this much */
113 int ifvm_mintu; /* min transmission unit */
114 uint16_t ifvm_proto; /* encapsulation ethertype */
115 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
117 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
119 LIST_ENTRY(ifvlan) ifv_list;
122 #define ifv_proto ifv_mib.ifvm_proto
123 #define ifv_tag ifv_mib.ifvm_tag
124 #define ifv_encaplen ifv_mib.ifvm_encaplen
125 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
126 #define ifv_mintu ifv_mib.ifvm_mintu
128 /* Special flags we should propagate to parent. */
131 int (*func)(struct ifnet *, int);
133 {IFF_PROMISC, ifpromisc},
134 {IFF_ALLMULTI, if_allmulti},
138 SYSCTL_DECL(_net_link);
139 static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0,
141 static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0,
144 static int soft_pad = 0;
145 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
146 "pad short frames before tagging");
148 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
150 static eventhandler_tag ifdetach_tag;
151 static eventhandler_tag iflladdr_tag;
154 * We have a global mutex, that is used to serialize configuration
155 * changes and isn't used in normal packet delivery.
157 * We also have a per-trunk rwlock, that is locked shared on packet
158 * processing and exclusive when configuration is changed.
160 * The VLAN_ARRAY substitutes the dynamic hash with a static array
161 * with 4096 entries. In theory this can give a boost in processing,
162 * however on practice it does not. Probably this is because array
163 * is too big to fit into CPU cache.
165 static struct sx ifv_lock;
166 #define VLAN_LOCK_INIT() sx_init(&ifv_lock, "vlan_global")
167 #define VLAN_LOCK_DESTROY() sx_destroy(&ifv_lock)
168 #define VLAN_LOCK_ASSERT() sx_assert(&ifv_lock, SA_LOCKED)
169 #define VLAN_LOCK() sx_xlock(&ifv_lock)
170 #define VLAN_UNLOCK() sx_xunlock(&ifv_lock)
171 #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME)
172 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
173 #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw)
174 #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw)
175 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
176 #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw)
177 #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw)
178 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
181 static void vlan_inithash(struct ifvlantrunk *trunk);
182 static void vlan_freehash(struct ifvlantrunk *trunk);
183 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
184 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
185 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
186 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
189 static void trunk_destroy(struct ifvlantrunk *trunk);
191 static void vlan_init(void *foo);
192 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
193 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
194 static void vlan_qflush(struct ifnet *ifp);
195 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
196 int (*func)(struct ifnet *, int));
197 static int vlan_setflags(struct ifnet *ifp, int status);
198 static int vlan_setmulti(struct ifnet *ifp);
199 static int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
200 static void vlan_unconfig(struct ifnet *ifp);
201 static void vlan_unconfig_locked(struct ifnet *ifp, int departing);
202 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
203 static void vlan_link_state(struct ifnet *ifp);
204 static void vlan_capabilities(struct ifvlan *ifv);
205 static void vlan_trunk_capabilities(struct ifnet *ifp);
207 static struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
208 const char *, int *);
209 static int vlan_clone_match(struct if_clone *, const char *);
210 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
211 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
213 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
214 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
216 static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
217 IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
220 static VNET_DEFINE(struct if_clone, vlan_cloner);
221 #define V_vlan_cloner VNET(vlan_cloner)
225 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
228 vlan_inithash(struct ifvlantrunk *trunk)
233 * The trunk must not be locked here since we call malloc(M_WAITOK).
234 * It is OK in case this function is called before the trunk struct
235 * gets hooked up and becomes visible from other threads.
238 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
239 ("%s: hash already initialized", __func__));
241 trunk->hwidth = VLAN_DEF_HWIDTH;
242 n = 1 << trunk->hwidth;
243 trunk->hmask = n - 1;
244 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
245 for (i = 0; i < n; i++)
246 LIST_INIT(&trunk->hash[i]);
250 vlan_freehash(struct ifvlantrunk *trunk)
255 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
256 for (i = 0; i < (1 << trunk->hwidth); i++)
257 KASSERT(LIST_EMPTY(&trunk->hash[i]),
258 ("%s: hash table not empty", __func__));
260 free(trunk->hash, M_VLAN);
262 trunk->hwidth = trunk->hmask = 0;
266 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
271 TRUNK_LOCK_ASSERT(trunk);
272 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
274 b = 1 << trunk->hwidth;
275 i = HASH(ifv->ifv_tag, trunk->hmask);
276 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
277 if (ifv->ifv_tag == ifv2->ifv_tag)
281 * Grow the hash when the number of vlans exceeds half of the number of
282 * hash buckets squared. This will make the average linked-list length
285 if (trunk->refcnt > (b * b) / 2) {
286 vlan_growhash(trunk, 1);
287 i = HASH(ifv->ifv_tag, trunk->hmask);
289 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
296 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
301 TRUNK_LOCK_ASSERT(trunk);
302 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
304 b = 1 << trunk->hwidth;
305 i = HASH(ifv->ifv_tag, trunk->hmask);
306 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
309 LIST_REMOVE(ifv2, ifv_list);
310 if (trunk->refcnt < (b * b) / 2)
311 vlan_growhash(trunk, -1);
315 panic("%s: vlan not found\n", __func__);
316 return (ENOENT); /*NOTREACHED*/
320 * Grow the hash larger or smaller if memory permits.
323 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
326 struct ifvlanhead *hash2;
327 int hwidth2, i, j, n, n2;
329 TRUNK_LOCK_ASSERT(trunk);
330 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
333 /* Harmless yet obvious coding error */
334 printf("%s: howmuch is 0\n", __func__);
338 hwidth2 = trunk->hwidth + howmuch;
339 n = 1 << trunk->hwidth;
341 /* Do not shrink the table below the default */
342 if (hwidth2 < VLAN_DEF_HWIDTH)
345 /* M_NOWAIT because we're called with trunk mutex held */
346 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
348 printf("%s: out of memory -- hash size not changed\n",
350 return; /* We can live with the old hash table */
352 for (j = 0; j < n2; j++)
353 LIST_INIT(&hash2[j]);
354 for (i = 0; i < n; i++)
355 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
356 LIST_REMOVE(ifv, ifv_list);
357 j = HASH(ifv->ifv_tag, n2 - 1);
358 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
360 free(trunk->hash, M_VLAN);
362 trunk->hwidth = hwidth2;
363 trunk->hmask = n2 - 1;
366 if_printf(trunk->parent,
367 "VLAN hash table resized from %d to %d buckets\n", n, n2);
370 static __inline struct ifvlan *
371 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
375 TRUNK_LOCK_RASSERT(trunk);
377 LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
378 if (ifv->ifv_tag == tag)
384 /* Debugging code to view the hashtables. */
386 vlan_dumphash(struct ifvlantrunk *trunk)
391 for (i = 0; i < (1 << trunk->hwidth); i++) {
393 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
394 printf("%s ", ifv->ifv_ifp->if_xname);
401 static __inline struct ifvlan *
402 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
405 return trunk->vlans[tag];
409 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
412 if (trunk->vlans[ifv->ifv_tag] != NULL)
414 trunk->vlans[ifv->ifv_tag] = ifv;
421 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
424 trunk->vlans[ifv->ifv_tag] = NULL;
431 vlan_freehash(struct ifvlantrunk *trunk)
436 vlan_inithash(struct ifvlantrunk *trunk)
440 #endif /* !VLAN_ARRAY */
443 trunk_destroy(struct ifvlantrunk *trunk)
448 vlan_freehash(trunk);
449 trunk->parent->if_vlantrunk = NULL;
451 TRUNK_LOCK_DESTROY(trunk);
456 * Program our multicast filter. What we're actually doing is
457 * programming the multicast filter of the parent. This has the
458 * side effect of causing the parent interface to receive multicast
459 * traffic that it doesn't really want, which ends up being discarded
460 * later by the upper protocol layers. Unfortunately, there's no way
461 * to avoid this: there really is only one physical interface.
463 * XXX: There is a possible race here if more than one thread is
464 * modifying the multicast state of the vlan interface at the same time.
467 vlan_setmulti(struct ifnet *ifp)
470 struct ifmultiaddr *ifma, *rifma = NULL;
472 struct vlan_mc_entry *mc;
475 /*VLAN_LOCK_ASSERT();*/
477 /* Find the parent. */
481 CURVNET_SET_QUIET(ifp_p->if_vnet);
483 /* First, remove any existing filter entries. */
484 while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
485 error = if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
488 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
492 /* Now program new ones. */
493 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
494 if (ifma->ifma_addr->sa_family != AF_LINK)
496 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
499 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
500 mc->mc_addr.sdl_index = ifp_p->if_index;
501 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
502 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
513 * A handler for parent interface link layer address changes.
514 * If the parent interface link layer address is changed we
515 * should also change it on all children vlans.
518 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
527 * Check if it's a trunk interface first of all
528 * to avoid needless locking.
530 if (ifp->if_vlantrunk == NULL)
535 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
538 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
539 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
540 #else /* VLAN_ARRAY */
541 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
542 LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) {
543 #endif /* VLAN_ARRAY */
545 if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp),
554 * A handler for network interface departure events.
555 * Track departure of trunks here so that we don't access invalid
556 * pointers or whatever if a trunk is ripped from under us, e.g.,
557 * by ejecting its hot-plug card. However, if an ifnet is simply
558 * being renamed, then there's no need to tear down the state.
561 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
567 * Check if it's a trunk interface first of all
568 * to avoid needless locking.
570 if (ifp->if_vlantrunk == NULL)
573 /* If the ifnet is just being renamed, don't do anything. */
574 if (ifp->if_flags & IFF_RENAMING)
579 * OK, it's a trunk. Loop over and detach all vlan's on it.
580 * Check trunk pointer after each vlan_unconfig() as it will
581 * free it and set to NULL after the last vlan was detached.
584 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
585 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
586 vlan_unconfig_locked(ifv->ifv_ifp, 1);
587 if (ifp->if_vlantrunk == NULL)
590 #else /* VLAN_ARRAY */
592 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
593 if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
594 vlan_unconfig_locked(ifv->ifv_ifp, 1);
595 if (ifp->if_vlantrunk)
596 goto restart; /* trunk->hwidth can change */
600 #endif /* VLAN_ARRAY */
601 /* Trunk should have been destroyed in vlan_unconfig(). */
602 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
607 * Return the trunk device for a virtual interface.
609 static struct ifnet *
610 vlan_trunkdev(struct ifnet *ifp)
614 if (ifp->if_type != IFT_L2VLAN)
626 * Return the 16bit vlan tag for this interface.
629 vlan_tag(struct ifnet *ifp, uint16_t *tagp)
633 if (ifp->if_type != IFT_L2VLAN)
636 *tagp = ifv->ifv_tag;
641 * Return a driver specific cookie for this interface. Synchronization
642 * with setcookie must be provided by the driver.
645 vlan_cookie(struct ifnet *ifp)
649 if (ifp->if_type != IFT_L2VLAN)
652 return (ifv->ifv_cookie);
656 * Store a cookie in our softc that drivers can use to store driver
657 * private per-instance data in.
660 vlan_setcookie(struct ifnet *ifp, void *cookie)
664 if (ifp->if_type != IFT_L2VLAN)
667 ifv->ifv_cookie = cookie;
672 * Return the vlan device present at the specific tag.
674 static struct ifnet *
675 vlan_devat(struct ifnet *ifp, uint16_t tag)
677 struct ifvlantrunk *trunk;
680 trunk = ifp->if_vlantrunk;
685 ifv = vlan_gethash(trunk, tag);
688 TRUNK_RUNLOCK(trunk);
693 * VLAN support can be loaded as a module. The only place in the
694 * system that's intimately aware of this is ether_input. We hook
695 * into this code through vlan_input_p which is defined there and
696 * set here. Noone else in the system should be aware of this so
697 * we use an explicit reference here.
699 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
701 /* For if_link_state_change() eyes only... */
702 extern void (*vlan_link_state_p)(struct ifnet *);
705 vlan_modevent(module_t mod, int type, void *data)
710 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
711 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
712 if (ifdetach_tag == NULL)
714 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
715 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
716 if (iflladdr_tag == NULL)
719 vlan_input_p = vlan_input;
720 vlan_link_state_p = vlan_link_state;
721 vlan_trunk_cap_p = vlan_trunk_capabilities;
722 vlan_trunkdev_p = vlan_trunkdev;
723 vlan_cookie_p = vlan_cookie;
724 vlan_setcookie_p = vlan_setcookie;
725 vlan_tag_p = vlan_tag;
726 vlan_devat_p = vlan_devat;
728 if_clone_attach(&vlan_cloner);
731 printf("vlan: initialized, using "
735 "hash tables with chaining"
742 if_clone_detach(&vlan_cloner);
744 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
745 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
747 vlan_link_state_p = NULL;
748 vlan_trunk_cap_p = NULL;
749 vlan_trunkdev_p = NULL;
751 vlan_cookie_p = vlan_cookie;
752 vlan_setcookie_p = vlan_setcookie;
756 printf("vlan: unloaded\n");
764 static moduledata_t vlan_mod = {
770 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
771 MODULE_VERSION(if_vlan, 3);
775 vnet_vlan_init(const void *unused __unused)
778 V_vlan_cloner = vlan_cloner;
779 if_clone_attach(&V_vlan_cloner);
781 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
782 vnet_vlan_init, NULL);
785 vnet_vlan_uninit(const void *unused __unused)
788 if_clone_detach(&V_vlan_cloner);
790 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
791 vnet_vlan_uninit, NULL);
794 static struct ifnet *
795 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
801 /* Check for <etherif>.<vlan> style interface names. */
802 IFNET_RLOCK_NOSLEEP();
803 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
805 * We can handle non-ethernet hardware types as long as
806 * they handle the tagging and headers themselves.
808 if (ifp->if_type != IFT_ETHER &&
809 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
811 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
813 cp = name + strlen(ifp->if_xname);
819 for(; *cp >= '0' && *cp <= '9'; cp++)
820 t = (t * 10) + (*cp - '0');
827 IFNET_RUNLOCK_NOSLEEP();
833 vlan_clone_match(struct if_clone *ifc, const char *name)
837 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
840 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
842 for (cp = name + 4; *cp != '\0'; cp++) {
843 if (*cp < '0' || *cp > '9')
851 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
863 struct sockaddr_dl *sdl;
865 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
868 * There are 3 (ugh) ways to specify the cloned device:
869 * o pass a parameter block with the clone request.
870 * o specify parameters in the text of the clone device name
871 * o specify no parameters and get an unattached device that
872 * must be configured separately.
873 * The first technique is preferred; the latter two are
874 * supported for backwards compatibilty.
877 error = copyin(params, &vlr, sizeof(vlr));
880 p = ifunit(vlr.vlr_parent);
884 * Don't let the caller set up a VLAN tag with
885 * anything except VLID bits.
887 if (vlr.vlr_tag & ~EVL_VLID_MASK)
889 error = ifc_name2unit(name, &unit);
895 wildcard = (unit < 0);
896 } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
902 * Don't let the caller set up a VLAN tag with
903 * anything except VLID bits.
905 if (tag & ~EVL_VLID_MASK)
910 error = ifc_name2unit(name, &unit);
914 wildcard = (unit < 0);
917 error = ifc_alloc_unit(ifc, &unit);
921 /* In the wildcard case, we need to update the name. */
923 for (dp = name; *dp != '\0'; dp++);
924 if (snprintf(dp, len - (dp-name), "%d", unit) >
925 len - (dp-name) - 1) {
926 panic("%s: interface name too long", __func__);
930 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
931 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
933 ifc_free_unit(ifc, unit);
937 SLIST_INIT(&ifv->vlan_mc_listhead);
941 * Set the name manually rather than using if_initname because
942 * we don't conform to the default naming convention for interfaces.
944 strlcpy(ifp->if_xname, name, IFNAMSIZ);
945 ifp->if_dname = ifc->ifc_name;
946 ifp->if_dunit = unit;
947 /* NB: flags are not set here */
948 ifp->if_linkmib = &ifv->ifv_mib;
949 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
950 /* NB: mtu is not set here */
952 ifp->if_init = vlan_init;
953 ifp->if_transmit = vlan_transmit;
954 ifp->if_qflush = vlan_qflush;
955 ifp->if_ioctl = vlan_ioctl;
956 ifp->if_flags = VLAN_IFFLAGS;
957 ether_ifattach(ifp, eaddr);
958 /* Now undo some of the damage... */
959 ifp->if_baudrate = 0;
960 ifp->if_type = IFT_L2VLAN;
961 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
963 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
964 sdl->sdl_type = IFT_L2VLAN;
967 error = vlan_config(ifv, p, tag);
970 * Since we've partially failed, we need to back
971 * out all the way, otherwise userland could get
972 * confused. Thus, we destroy the interface.
976 if_free_type(ifp, IFT_ETHER);
977 ifc_free_unit(ifc, unit);
983 /* Update flags on the parent, if necessary. */
984 vlan_setflags(ifp, 1);
991 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
993 struct ifvlan *ifv = ifp->if_softc;
994 int unit = ifp->if_dunit;
996 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
997 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
998 if_free_type(ifp, IFT_ETHER);
1000 ifc_free_unit(ifc, unit);
1006 * The ifp->if_init entry point for vlan(4) is a no-op.
1009 vlan_init(void *foo __unused)
1014 * The if_transmit method for vlan(4) interface.
1017 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1021 int error, len, mcast;
1023 ifv = ifp->if_softc;
1025 len = m->m_pkthdr.len;
1026 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1031 * Do not run parent's if_transmit() if the parent is not up,
1032 * or parent's driver will cause a system crash.
1034 if (!UP_AND_RUNNING(p)) {
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);
1082 if_printf(ifp, "unable to prepend VLAN header\n");
1089 * Send it, precisely as ether_output() would have.
1091 error = (p->if_transmit)(p, m);
1094 ifp->if_omcasts += mcast;
1095 ifp->if_obytes += len;
1102 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1105 vlan_qflush(struct ifnet *ifp __unused)
1110 vlan_input(struct ifnet *ifp, struct mbuf *m)
1112 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1116 KASSERT(trunk != NULL, ("%s: no trunk", __func__));
1118 if (m->m_flags & M_VLANTAG) {
1120 * Packet is tagged, but m contains a normal
1121 * Ethernet frame; the tag is stored out-of-band.
1123 tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
1124 m->m_flags &= ~M_VLANTAG;
1126 struct ether_vlan_header *evl;
1129 * Packet is tagged in-band as specified by 802.1q.
1131 switch (ifp->if_type) {
1133 if (m->m_len < sizeof(*evl) &&
1134 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1135 if_printf(ifp, "cannot pullup VLAN header\n");
1138 evl = mtod(m, struct ether_vlan_header *);
1139 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
1142 * Remove the 802.1q header by copying the Ethernet
1143 * addresses over it and adjusting the beginning of
1144 * the data in the mbuf. The encapsulated Ethernet
1145 * type field is already in place.
1147 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1148 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1149 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1154 panic("%s: %s has unsupported if_type %u",
1155 __func__, ifp->if_xname, ifp->if_type);
1164 ifv = vlan_gethash(trunk, tag);
1165 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1166 TRUNK_RUNLOCK(trunk);
1171 TRUNK_RUNLOCK(trunk);
1173 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1174 ifv->ifv_ifp->if_ipackets++;
1176 /* Pass it back through the parent's input routine. */
1177 (*ifp->if_input)(ifv->ifv_ifp, m);
1181 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
1183 struct ifvlantrunk *trunk;
1187 /* VID numbers 0x0 and 0xFFF are reserved */
1188 if (tag == 0 || tag == 0xFFF)
1190 if (p->if_type != IFT_ETHER &&
1191 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1192 return (EPROTONOSUPPORT);
1193 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1194 return (EPROTONOSUPPORT);
1198 if (p->if_vlantrunk == NULL) {
1199 trunk = malloc(sizeof(struct ifvlantrunk),
1200 M_VLAN, M_WAITOK | M_ZERO);
1201 vlan_inithash(trunk);
1203 if (p->if_vlantrunk != NULL) {
1204 /* A race that that is very unlikely to be hit. */
1205 vlan_freehash(trunk);
1206 free(trunk, M_VLAN);
1209 TRUNK_LOCK_INIT(trunk);
1211 p->if_vlantrunk = trunk;
1216 trunk = p->if_vlantrunk;
1220 ifv->ifv_tag = tag; /* must set this before vlan_inshash() */
1221 error = vlan_inshash(trunk, ifv);
1224 ifv->ifv_proto = ETHERTYPE_VLAN;
1225 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1226 ifv->ifv_mintu = ETHERMIN;
1227 ifv->ifv_pflags = 0;
1230 * If the parent supports the VLAN_MTU capability,
1231 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1234 if (p->if_capenable & IFCAP_VLAN_MTU) {
1236 * No need to fudge the MTU since the parent can
1237 * handle extended frames.
1239 ifv->ifv_mtufudge = 0;
1242 * Fudge the MTU by the encapsulation size. This
1243 * makes us incompatible with strictly compliant
1244 * 802.1Q implementations, but allows us to use
1245 * the feature with other NetBSD implementations,
1246 * which might still be useful.
1248 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1251 ifv->ifv_trunk = trunk;
1254 * Initialize fields from our parent. This duplicates some
1255 * work with ether_ifattach() but allows for non-ethernet
1256 * interfaces to also work.
1258 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1259 ifp->if_baudrate = p->if_baudrate;
1260 ifp->if_output = p->if_output;
1261 ifp->if_input = p->if_input;
1262 ifp->if_resolvemulti = p->if_resolvemulti;
1263 ifp->if_addrlen = p->if_addrlen;
1264 ifp->if_broadcastaddr = p->if_broadcastaddr;
1267 * Copy only a selected subset of flags from the parent.
1268 * Other flags are none of our business.
1270 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1271 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1272 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1273 #undef VLAN_COPY_FLAGS
1275 ifp->if_link_state = p->if_link_state;
1277 vlan_capabilities(ifv);
1280 * Set up our interface address to reflect the underlying
1281 * physical interface's.
1283 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1284 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1288 * Configure multicast addresses that may already be
1289 * joined on the vlan device.
1291 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1293 /* We are ready for operation now. */
1294 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1296 TRUNK_UNLOCK(trunk);
1298 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag);
1305 vlan_unconfig(struct ifnet *ifp)
1309 vlan_unconfig_locked(ifp, 0);
1314 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1316 struct ifvlantrunk *trunk;
1317 struct vlan_mc_entry *mc;
1319 struct ifnet *parent;
1324 ifv = ifp->if_softc;
1325 trunk = ifv->ifv_trunk;
1328 if (trunk != NULL) {
1331 parent = trunk->parent;
1334 * Since the interface is being unconfigured, we need to
1335 * empty the list of multicast groups that we may have joined
1336 * while we were alive from the parent's list.
1338 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1340 * If the parent interface is being detached,
1341 * all its multicast addresses have already
1342 * been removed. Warn about errors if
1343 * if_delmulti() does fail, but don't abort as
1344 * all callers expect vlan destruction to
1348 error = if_delmulti(parent,
1349 (struct sockaddr *)&mc->mc_addr);
1352 "Failed to delete multicast address from parent: %d\n",
1355 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1359 vlan_setflags(ifp, 0); /* clear special flags on parent */
1360 vlan_remhash(trunk, ifv);
1361 ifv->ifv_trunk = NULL;
1364 * Check if we were the last.
1366 if (trunk->refcnt == 0) {
1367 trunk->parent->if_vlantrunk = NULL;
1369 * XXXGL: If some ithread has already entered
1370 * vlan_input() and is now blocked on the trunk
1371 * lock, then it should preempt us right after
1372 * unlock and finish its work. Then we will acquire
1373 * lock again in trunk_destroy().
1375 TRUNK_UNLOCK(trunk);
1376 trunk_destroy(trunk);
1378 TRUNK_UNLOCK(trunk);
1381 /* Disconnect from parent. */
1382 if (ifv->ifv_pflags)
1383 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1384 ifp->if_mtu = ETHERMTU;
1385 ifp->if_link_state = LINK_STATE_UNKNOWN;
1386 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1389 * Only dispatch an event if vlan was
1390 * attached, otherwise there is nothing
1391 * to cleanup anyway.
1394 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag);
1397 /* Handle a reference counted flag that should be set on the parent as well */
1399 vlan_setflag(struct ifnet *ifp, int flag, int status,
1400 int (*func)(struct ifnet *, int))
1405 /* XXX VLAN_LOCK_ASSERT(); */
1407 ifv = ifp->if_softc;
1408 status = status ? (ifp->if_flags & flag) : 0;
1409 /* Now "status" contains the flag value or 0 */
1412 * See if recorded parent's status is different from what
1413 * we want it to be. If it is, flip it. We record parent's
1414 * status in ifv_pflags so that we won't clear parent's flag
1415 * we haven't set. In fact, we don't clear or set parent's
1416 * flags directly, but get or release references to them.
1417 * That's why we can be sure that recorded flags still are
1418 * in accord with actual parent's flags.
1420 if (status != (ifv->ifv_pflags & flag)) {
1421 error = (*func)(PARENT(ifv), status);
1424 ifv->ifv_pflags &= ~flag;
1425 ifv->ifv_pflags |= status;
1431 * Handle IFF_* flags that require certain changes on the parent:
1432 * if "status" is true, update parent's flags respective to our if_flags;
1433 * if "status" is false, forcedly clear the flags set on parent.
1436 vlan_setflags(struct ifnet *ifp, int status)
1440 for (i = 0; vlan_pflags[i].flag; i++) {
1441 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1442 status, vlan_pflags[i].func);
1449 /* Inform all vlans that their parent has changed link state */
1451 vlan_link_state(struct ifnet *ifp)
1453 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1459 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1460 if (trunk->vlans[i] != NULL) {
1461 ifv = trunk->vlans[i];
1463 for (i = 0; i < (1 << trunk->hwidth); i++)
1464 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
1466 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1467 if_link_state_change(ifv->ifv_ifp,
1468 trunk->parent->if_link_state);
1470 TRUNK_UNLOCK(trunk);
1474 vlan_capabilities(struct ifvlan *ifv)
1476 struct ifnet *p = PARENT(ifv);
1477 struct ifnet *ifp = ifv->ifv_ifp;
1479 TRUNK_LOCK_ASSERT(TRUNK(ifv));
1482 * If the parent interface can do checksum offloading
1483 * on VLANs, then propagate its hardware-assisted
1484 * checksumming flags. Also assert that checksum
1485 * offloading requires hardware VLAN tagging.
1487 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1488 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1490 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1491 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1492 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1493 ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP |
1494 CSUM_UDP | CSUM_SCTP | CSUM_IP_FRAGS | CSUM_FRAGMENT);
1496 ifp->if_capenable = 0;
1497 ifp->if_hwassist = 0;
1500 * If the parent interface can do TSO on VLANs then
1501 * propagate the hardware-assisted flag. TSO on VLANs
1502 * does not necessarily require hardware VLAN tagging.
1504 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1505 ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO;
1506 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1507 ifp->if_capenable |= p->if_capenable & IFCAP_TSO;
1508 ifp->if_hwassist |= p->if_hwassist & CSUM_TSO;
1510 ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO);
1511 ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO);
1516 vlan_trunk_capabilities(struct ifnet *ifp)
1518 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1524 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1525 if (trunk->vlans[i] != NULL) {
1526 ifv = trunk->vlans[i];
1528 for (i = 0; i < (1 << trunk->hwidth); i++) {
1529 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1531 vlan_capabilities(ifv);
1533 TRUNK_UNLOCK(trunk);
1537 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1546 ifr = (struct ifreq *)data;
1547 ifa = (struct ifaddr *) data;
1548 ifv = ifp->if_softc;
1552 ifp->if_flags |= IFF_UP;
1554 if (ifa->ifa_addr->sa_family == AF_INET)
1555 arp_ifinit(ifp, ifa);
1560 struct sockaddr *sa;
1562 sa = (struct sockaddr *)&ifr->ifr_data;
1563 bcopy(IF_LLADDR(ifp), sa->sa_data, ifp->if_addrlen);
1568 if (TRUNK(ifv) != NULL) {
1571 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1572 /* Limit the result to the parent's current config. */
1574 struct ifmediareq *ifmr;
1576 ifmr = (struct ifmediareq *)data;
1577 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1578 ifmr->ifm_count = 1;
1579 error = copyout(&ifmr->ifm_current,
1596 * Set the interface MTU.
1599 if (TRUNK(ifv) != NULL) {
1601 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1603 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1606 ifp->if_mtu = ifr->ifr_mtu;
1614 if (ifp->if_vnet != ifp->if_home_vnet) {
1619 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1622 if (vlr.vlr_parent[0] == '\0') {
1626 p = ifunit(vlr.vlr_parent);
1632 * Don't let the caller set up a VLAN tag with
1633 * anything except VLID bits.
1635 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1639 error = vlan_config(ifv, p, vlr.vlr_tag);
1643 /* Update flags on the parent, if necessary. */
1644 vlan_setflags(ifp, 1);
1649 if (ifp->if_vnet != ifp->if_home_vnet) {
1654 bzero(&vlr, sizeof(vlr));
1656 if (TRUNK(ifv) != NULL) {
1657 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1658 sizeof(vlr.vlr_parent));
1659 vlr.vlr_tag = ifv->ifv_tag;
1662 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1667 * We should propagate selected flags to the parent,
1668 * e.g., promiscuous mode.
1670 if (TRUNK(ifv) != NULL)
1671 error = vlan_setflags(ifp, 1);
1677 * If we don't have a parent, just remember the membership for
1680 if (TRUNK(ifv) != NULL)
1681 error = vlan_setmulti(ifp);