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>
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>
69 #define VLANNAME "vlan"
70 #define VLAN_DEF_HWIDTH 4
71 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
73 #define UP_AND_RUNNING(ifp) \
74 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
76 LIST_HEAD(ifvlanhead, ifvlan);
79 struct ifnet *parent; /* parent interface of this trunk */
82 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
83 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
85 struct ifvlanhead *hash; /* dynamic hash-list table */
92 struct vlan_mc_entry {
93 struct ether_addr mc_addr;
94 SLIST_ENTRY(vlan_mc_entry) mc_entries;
98 struct ifvlantrunk *ifv_trunk;
99 struct ifnet *ifv_ifp;
100 #define TRUNK(ifv) ((ifv)->ifv_trunk)
101 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
102 int ifv_pflags; /* special flags we have set on parent */
104 int ifvm_encaplen; /* encapsulation length */
105 int ifvm_mtufudge; /* MTU fudged by this much */
106 int ifvm_mintu; /* min transmission unit */
107 uint16_t ifvm_proto; /* encapsulation ethertype */
108 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
110 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
112 LIST_ENTRY(ifvlan) ifv_list;
115 #define ifv_proto ifv_mib.ifvm_proto
116 #define ifv_tag ifv_mib.ifvm_tag
117 #define ifv_encaplen ifv_mib.ifvm_encaplen
118 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
119 #define ifv_mintu ifv_mib.ifvm_mintu
121 /* Special flags we should propagate to parent. */
124 int (*func)(struct ifnet *, int);
126 {IFF_PROMISC, ifpromisc},
127 {IFF_ALLMULTI, if_allmulti},
131 SYSCTL_DECL(_net_link);
132 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
133 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
135 static int soft_pad = 0;
136 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
137 "pad short frames before tagging");
139 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
141 static eventhandler_tag ifdetach_tag;
142 static eventhandler_tag iflladdr_tag;
145 * We have a global mutex, that is used to serialize configuration
146 * changes and isn't used in normal packet delivery.
148 * We also have a per-trunk rwlock, that is locked shared on packet
149 * processing and exclusive when configuration is changed.
151 * The VLAN_ARRAY substitutes the dynamic hash with a static array
152 * with 4096 entries. In theory this can give a boost in processing,
153 * however on practice it does not. Probably this is because array
154 * is too big to fit into CPU cache.
156 static struct mtx ifv_mtx;
157 #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
158 #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx)
159 #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED)
160 #define VLAN_LOCK() mtx_lock(&ifv_mtx)
161 #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx)
162 #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME)
163 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
164 #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw)
165 #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw)
166 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
167 #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw)
168 #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw)
169 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
172 static void vlan_inithash(struct ifvlantrunk *trunk);
173 static void vlan_freehash(struct ifvlantrunk *trunk);
174 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
175 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
176 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
177 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
180 static void trunk_destroy(struct ifvlantrunk *trunk);
182 static void vlan_start(struct ifnet *ifp);
183 static void vlan_init(void *foo);
184 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
185 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
186 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
187 int (*func)(struct ifnet *, int));
188 static int vlan_setflags(struct ifnet *ifp, int status);
189 static int vlan_setmulti(struct ifnet *ifp);
190 static void vlan_unconfig(struct ifnet *ifp);
191 static void vlan_unconfig_locked(struct ifnet *ifp);
192 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
193 static void vlan_link_state(struct ifnet *ifp, int link);
194 static void vlan_capabilities(struct ifvlan *ifv);
195 static void vlan_trunk_capabilities(struct ifnet *ifp);
197 static struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
198 const char *, int *);
199 static int vlan_clone_match(struct if_clone *, const char *);
200 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
201 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
203 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
204 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
206 static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
207 IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
210 static VNET_DEFINE(struct if_clone, vlan_cloner);
211 #define V_vlan_cloner VNET(vlan_cloner)
215 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
218 vlan_inithash(struct ifvlantrunk *trunk)
223 * The trunk must not be locked here since we call malloc(M_WAITOK).
224 * It is OK in case this function is called before the trunk struct
225 * gets hooked up and becomes visible from other threads.
228 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
229 ("%s: hash already initialized", __func__));
231 trunk->hwidth = VLAN_DEF_HWIDTH;
232 n = 1 << trunk->hwidth;
233 trunk->hmask = n - 1;
234 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
235 for (i = 0; i < n; i++)
236 LIST_INIT(&trunk->hash[i]);
240 vlan_freehash(struct ifvlantrunk *trunk)
245 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
246 for (i = 0; i < (1 << trunk->hwidth); i++)
247 KASSERT(LIST_EMPTY(&trunk->hash[i]),
248 ("%s: hash table not empty", __func__));
250 free(trunk->hash, M_VLAN);
252 trunk->hwidth = trunk->hmask = 0;
256 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
261 TRUNK_LOCK_ASSERT(trunk);
262 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
264 b = 1 << trunk->hwidth;
265 i = HASH(ifv->ifv_tag, trunk->hmask);
266 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
267 if (ifv->ifv_tag == ifv2->ifv_tag)
271 * Grow the hash when the number of vlans exceeds half of the number of
272 * hash buckets squared. This will make the average linked-list length
275 if (trunk->refcnt > (b * b) / 2) {
276 vlan_growhash(trunk, 1);
277 i = HASH(ifv->ifv_tag, trunk->hmask);
279 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
286 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
291 TRUNK_LOCK_ASSERT(trunk);
292 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
294 b = 1 << trunk->hwidth;
295 i = HASH(ifv->ifv_tag, trunk->hmask);
296 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
299 LIST_REMOVE(ifv2, ifv_list);
300 if (trunk->refcnt < (b * b) / 2)
301 vlan_growhash(trunk, -1);
305 panic("%s: vlan not found\n", __func__);
306 return (ENOENT); /*NOTREACHED*/
310 * Grow the hash larger or smaller if memory permits.
313 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
316 struct ifvlanhead *hash2;
317 int hwidth2, i, j, n, n2;
319 TRUNK_LOCK_ASSERT(trunk);
320 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
323 /* Harmless yet obvious coding error */
324 printf("%s: howmuch is 0\n", __func__);
328 hwidth2 = trunk->hwidth + howmuch;
329 n = 1 << trunk->hwidth;
331 /* Do not shrink the table below the default */
332 if (hwidth2 < VLAN_DEF_HWIDTH)
335 /* M_NOWAIT because we're called with trunk mutex held */
336 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
338 printf("%s: out of memory -- hash size not changed\n",
340 return; /* We can live with the old hash table */
342 for (j = 0; j < n2; j++)
343 LIST_INIT(&hash2[j]);
344 for (i = 0; i < n; i++)
345 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
346 LIST_REMOVE(ifv, ifv_list);
347 j = HASH(ifv->ifv_tag, n2 - 1);
348 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
350 free(trunk->hash, M_VLAN);
352 trunk->hwidth = hwidth2;
353 trunk->hmask = n2 - 1;
356 if_printf(trunk->parent,
357 "VLAN hash table resized from %d to %d buckets\n", n, n2);
360 static __inline struct ifvlan *
361 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
365 TRUNK_LOCK_RASSERT(trunk);
367 LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
368 if (ifv->ifv_tag == tag)
374 /* Debugging code to view the hashtables. */
376 vlan_dumphash(struct ifvlantrunk *trunk)
381 for (i = 0; i < (1 << trunk->hwidth); i++) {
383 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
384 printf("%s ", ifv->ifv_ifp->if_xname);
389 #endif /* !VLAN_ARRAY */
392 trunk_destroy(struct ifvlantrunk *trunk)
398 vlan_freehash(trunk);
400 trunk->parent->if_vlantrunk = NULL;
402 TRUNK_LOCK_DESTROY(trunk);
407 * Program our multicast filter. What we're actually doing is
408 * programming the multicast filter of the parent. This has the
409 * side effect of causing the parent interface to receive multicast
410 * traffic that it doesn't really want, which ends up being discarded
411 * later by the upper protocol layers. Unfortunately, there's no way
412 * to avoid this: there really is only one physical interface.
414 * XXX: There is a possible race here if more than one thread is
415 * modifying the multicast state of the vlan interface at the same time.
418 vlan_setmulti(struct ifnet *ifp)
421 struct ifmultiaddr *ifma, *rifma = NULL;
423 struct vlan_mc_entry *mc;
424 struct sockaddr_dl sdl;
427 /*VLAN_LOCK_ASSERT();*/
429 /* Find the parent. */
433 CURVNET_SET_QUIET(ifp_p->if_vnet);
435 bzero((char *)&sdl, sizeof(sdl));
436 sdl.sdl_len = sizeof(sdl);
437 sdl.sdl_family = AF_LINK;
438 sdl.sdl_index = ifp_p->if_index;
439 sdl.sdl_type = IFT_ETHER;
440 sdl.sdl_alen = ETHER_ADDR_LEN;
442 /* First, remove any existing filter entries. */
443 while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
444 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
445 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
448 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
452 /* Now program new ones. */
453 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
454 if (ifma->ifma_addr->sa_family != AF_LINK)
456 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
459 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
460 (char *)&mc->mc_addr, ETHER_ADDR_LEN);
461 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
462 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
463 LLADDR(&sdl), ETHER_ADDR_LEN);
464 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
474 * A handler for parent interface link layer address changes.
475 * If the parent interface link layer address is changed we
476 * should also change it on all children vlans.
479 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
488 * Check if it's a trunk interface first of all
489 * to avoid needless locking.
491 if (ifp->if_vlantrunk == NULL)
496 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
499 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
500 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
501 #else /* VLAN_ARRAY */
502 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
503 LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) {
504 #endif /* VLAN_ARRAY */
506 if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
514 * A handler for network interface departure events.
515 * Track departure of trunks here so that we don't access invalid
516 * pointers or whatever if a trunk is ripped from under us, e.g.,
517 * by ejecting its hot-plug card. However, if an ifnet is simply
518 * being renamed, then there's no need to tear down the state.
521 vlan_ifdetach(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)
533 /* If the ifnet is just being renamed, don't do anything. */
534 if (ifp->if_flags & IFF_RENAMING)
539 * OK, it's a trunk. Loop over and detach all vlan's on it.
540 * Check trunk pointer after each vlan_unconfig() as it will
541 * free it and set to NULL after the last vlan was detached.
544 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
545 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
546 vlan_unconfig_locked(ifv->ifv_ifp);
547 if (ifp->if_vlantrunk == NULL)
550 #else /* VLAN_ARRAY */
552 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
553 if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
554 vlan_unconfig_locked(ifv->ifv_ifp);
555 if (ifp->if_vlantrunk)
556 goto restart; /* trunk->hwidth can change */
560 #endif /* VLAN_ARRAY */
561 /* Trunk should have been destroyed in vlan_unconfig(). */
562 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
567 * VLAN support can be loaded as a module. The only place in the
568 * system that's intimately aware of this is ether_input. We hook
569 * into this code through vlan_input_p which is defined there and
570 * set here. Noone else in the system should be aware of this so
571 * we use an explicit reference here.
573 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
575 /* For if_link_state_change() eyes only... */
576 extern void (*vlan_link_state_p)(struct ifnet *, int);
579 vlan_modevent(module_t mod, int type, void *data)
584 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
585 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
586 if (ifdetach_tag == NULL)
588 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
589 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
590 if (iflladdr_tag == NULL)
593 vlan_input_p = vlan_input;
594 vlan_link_state_p = vlan_link_state;
595 vlan_trunk_cap_p = vlan_trunk_capabilities;
597 if_clone_attach(&vlan_cloner);
600 printf("vlan: initialized, using "
604 "hash tables with chaining"
611 if_clone_detach(&vlan_cloner);
613 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
614 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
616 vlan_link_state_p = NULL;
617 vlan_trunk_cap_p = NULL;
620 printf("vlan: unloaded\n");
628 static moduledata_t vlan_mod = {
634 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
635 MODULE_VERSION(if_vlan, 3);
639 vnet_vlan_init(const void *unused __unused)
642 V_vlan_cloner = vlan_cloner;
643 if_clone_attach(&V_vlan_cloner);
645 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
646 vnet_vlan_init, NULL);
649 vnet_vlan_uninit(const void *unused __unused)
652 if_clone_detach(&V_vlan_cloner);
654 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
655 vnet_vlan_uninit, NULL);
658 static struct ifnet *
659 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
665 /* Check for <etherif>.<vlan> style interface names. */
666 IFNET_RLOCK_NOSLEEP();
667 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
668 if (ifp->if_type != IFT_ETHER)
670 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
672 cp = name + strlen(ifp->if_xname);
678 for(; *cp >= '0' && *cp <= '9'; cp++)
679 t = (t * 10) + (*cp - '0');
686 IFNET_RUNLOCK_NOSLEEP();
692 vlan_clone_match(struct if_clone *ifc, const char *name)
696 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
699 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
701 for (cp = name + 4; *cp != '\0'; cp++) {
702 if (*cp < '0' || *cp > '9')
710 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
722 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
725 * There are 3 (ugh) ways to specify the cloned device:
726 * o pass a parameter block with the clone request.
727 * o specify parameters in the text of the clone device name
728 * o specify no parameters and get an unattached device that
729 * must be configured separately.
730 * The first technique is preferred; the latter two are
731 * supported for backwards compatibilty.
734 error = copyin(params, &vlr, sizeof(vlr));
737 p = ifunit(vlr.vlr_parent);
741 * Don't let the caller set up a VLAN tag with
742 * anything except VLID bits.
744 if (vlr.vlr_tag & ~EVL_VLID_MASK)
746 error = ifc_name2unit(name, &unit);
752 wildcard = (unit < 0);
753 } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
759 * Don't let the caller set up a VLAN tag with
760 * anything except VLID bits.
762 if (tag & ~EVL_VLID_MASK)
767 error = ifc_name2unit(name, &unit);
771 wildcard = (unit < 0);
774 error = ifc_alloc_unit(ifc, &unit);
778 /* In the wildcard case, we need to update the name. */
780 for (dp = name; *dp != '\0'; dp++);
781 if (snprintf(dp, len - (dp-name), "%d", unit) >
782 len - (dp-name) - 1) {
783 panic("%s: interface name too long", __func__);
787 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
788 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
790 ifc_free_unit(ifc, unit);
794 SLIST_INIT(&ifv->vlan_mc_listhead);
798 * Set the name manually rather than using if_initname because
799 * we don't conform to the default naming convention for interfaces.
801 strlcpy(ifp->if_xname, name, IFNAMSIZ);
802 ifp->if_dname = ifc->ifc_name;
803 ifp->if_dunit = unit;
804 /* NB: flags are not set here */
805 ifp->if_linkmib = &ifv->ifv_mib;
806 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
807 /* NB: mtu is not set here */
809 ifp->if_init = vlan_init;
810 ifp->if_start = vlan_start;
811 ifp->if_ioctl = vlan_ioctl;
812 ifp->if_snd.ifq_maxlen = ifqmaxlen;
813 ifp->if_flags = VLAN_IFFLAGS;
814 ether_ifattach(ifp, eaddr);
815 /* Now undo some of the damage... */
816 ifp->if_baudrate = 0;
817 ifp->if_type = IFT_L2VLAN;
818 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
821 error = vlan_config(ifv, p, tag);
824 * Since we've partialy failed, we need to back
825 * out all the way, otherwise userland could get
826 * confused. Thus, we destroy the interface.
830 if_free_type(ifp, IFT_ETHER);
831 ifc_free_unit(ifc, unit);
837 /* Update flags on the parent, if necessary. */
838 vlan_setflags(ifp, 1);
845 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
847 struct ifvlan *ifv = ifp->if_softc;
848 int unit = ifp->if_dunit;
850 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
851 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
852 if_free_type(ifp, IFT_ETHER);
854 ifc_free_unit(ifc, unit);
860 * The ifp->if_init entry point for vlan(4) is a no-op.
863 vlan_init(void *foo __unused)
868 * The if_start method for vlan(4) interface. It doesn't
869 * raises the IFF_DRV_OACTIVE flag, since it is called
870 * only from IFQ_HANDOFF() macro in ether_output_frame().
871 * If the interface queue is full, and vlan_start() is
872 * not called, the queue would never get emptied and
873 * interface would stall forever.
876 vlan_start(struct ifnet *ifp)
887 IF_DEQUEUE(&ifp->if_snd, m);
893 * Do not run parent's if_start() if the parent is not up,
894 * or parent's driver will cause a system crash.
896 if (!UP_AND_RUNNING(p)) {
898 ifp->if_collisions++;
903 * Pad the frame to the minimum size allowed if told to.
904 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
905 * paragraph C.4.4.3.b. It can help to work around buggy
906 * bridges that violate paragraph C.4.4.3.a from the same
907 * document, i.e., fail to pad short frames after untagging.
908 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
909 * untagging it will produce a 62-byte frame, which is a runt
910 * and requires padding. There are VLAN-enabled network
911 * devices that just discard such runts instead or mishandle
915 static char pad[8]; /* just zeros */
918 for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
919 n > 0; n -= sizeof(pad))
920 if (!m_append(m, min(n, sizeof(pad)), pad))
924 if_printf(ifp, "cannot pad short frame\n");
932 * If underlying interface can do VLAN tag insertion itself,
933 * just pass the packet along. However, we need some way to
934 * tell the interface where the packet came from so that it
935 * knows how to find the VLAN tag to use, so we attach a
936 * packet tag that holds it.
938 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
939 m->m_pkthdr.ether_vtag = ifv->ifv_tag;
940 m->m_flags |= M_VLANTAG;
942 m = ether_vlanencap(m, ifv->ifv_tag);
945 "unable to prepend VLAN header\n");
952 * Send it, precisely as ether_output() would have.
953 * We are already running at splimp.
955 error = (p->if_transmit)(p, m);
964 vlan_input(struct ifnet *ifp, struct mbuf *m)
966 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
970 KASSERT(trunk != NULL, ("%s: no trunk", __func__));
972 if (m->m_flags & M_VLANTAG) {
974 * Packet is tagged, but m contains a normal
975 * Ethernet frame; the tag is stored out-of-band.
977 tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
978 m->m_flags &= ~M_VLANTAG;
980 struct ether_vlan_header *evl;
983 * Packet is tagged in-band as specified by 802.1q.
985 switch (ifp->if_type) {
987 if (m->m_len < sizeof(*evl) &&
988 (m = m_pullup(m, sizeof(*evl))) == NULL) {
989 if_printf(ifp, "cannot pullup VLAN header\n");
992 evl = mtod(m, struct ether_vlan_header *);
993 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
996 * Remove the 802.1q header by copying the Ethernet
997 * addresses over it and adjusting the beginning of
998 * the data in the mbuf. The encapsulated Ethernet
999 * type field is already in place.
1001 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1002 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1003 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1008 panic("%s: %s has unsupported if_type %u",
1009 __func__, ifp->if_xname, ifp->if_type);
1019 ifv = trunk->vlans[tag];
1021 ifv = vlan_gethash(trunk, tag);
1023 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1024 TRUNK_RUNLOCK(trunk);
1029 TRUNK_RUNLOCK(trunk);
1031 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1032 ifv->ifv_ifp->if_ipackets++;
1034 /* Pass it back through the parent's input routine. */
1035 (*ifp->if_input)(ifv->ifv_ifp, m);
1039 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
1041 struct ifvlantrunk *trunk;
1045 /* VID numbers 0x0 and 0xFFF are reserved */
1046 if (tag == 0 || tag == 0xFFF)
1048 if (p->if_type != IFT_ETHER)
1049 return (EPROTONOSUPPORT);
1050 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1051 return (EPROTONOSUPPORT);
1055 if (p->if_vlantrunk == NULL) {
1056 trunk = malloc(sizeof(struct ifvlantrunk),
1057 M_VLAN, M_WAITOK | M_ZERO);
1059 vlan_inithash(trunk);
1062 if (p->if_vlantrunk != NULL) {
1063 /* A race that that is very unlikely to be hit. */
1065 vlan_freehash(trunk);
1067 free(trunk, M_VLAN);
1070 TRUNK_LOCK_INIT(trunk);
1072 p->if_vlantrunk = trunk;
1077 trunk = p->if_vlantrunk;
1081 ifv->ifv_tag = tag; /* must set this before vlan_inshash() */
1083 if (trunk->vlans[tag] != NULL) {
1087 trunk->vlans[tag] = ifv;
1090 error = vlan_inshash(trunk, ifv);
1094 ifv->ifv_proto = ETHERTYPE_VLAN;
1095 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1096 ifv->ifv_mintu = ETHERMIN;
1097 ifv->ifv_pflags = 0;
1100 * If the parent supports the VLAN_MTU capability,
1101 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1104 if (p->if_capenable & IFCAP_VLAN_MTU) {
1106 * No need to fudge the MTU since the parent can
1107 * handle extended frames.
1109 ifv->ifv_mtufudge = 0;
1112 * Fudge the MTU by the encapsulation size. This
1113 * makes us incompatible with strictly compliant
1114 * 802.1Q implementations, but allows us to use
1115 * the feature with other NetBSD implementations,
1116 * which might still be useful.
1118 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1121 ifv->ifv_trunk = trunk;
1123 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1124 ifp->if_baudrate = p->if_baudrate;
1126 * Copy only a selected subset of flags from the parent.
1127 * Other flags are none of our business.
1129 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1130 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1131 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1132 #undef VLAN_COPY_FLAGS
1134 ifp->if_link_state = p->if_link_state;
1136 vlan_capabilities(ifv);
1139 * Set up our ``Ethernet address'' to reflect the underlying
1140 * physical interface's.
1142 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
1145 * Configure multicast addresses that may already be
1146 * joined on the vlan device.
1148 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1150 /* We are ready for operation now. */
1151 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1153 TRUNK_UNLOCK(trunk);
1155 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag);
1162 vlan_unconfig(struct ifnet *ifp)
1166 vlan_unconfig_locked(ifp);
1171 vlan_unconfig_locked(struct ifnet *ifp)
1173 struct ifvlantrunk *trunk;
1174 struct vlan_mc_entry *mc;
1176 struct ifnet *parent;
1180 ifv = ifp->if_softc;
1181 trunk = ifv->ifv_trunk;
1184 if (trunk != NULL) {
1185 struct sockaddr_dl sdl;
1188 parent = trunk->parent;
1191 * Since the interface is being unconfigured, we need to
1192 * empty the list of multicast groups that we may have joined
1193 * while we were alive from the parent's list.
1195 bzero((char *)&sdl, sizeof(sdl));
1196 sdl.sdl_len = sizeof(sdl);
1197 sdl.sdl_family = AF_LINK;
1198 sdl.sdl_index = parent->if_index;
1199 sdl.sdl_type = IFT_ETHER;
1200 sdl.sdl_alen = ETHER_ADDR_LEN;
1202 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1203 bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
1207 * This may fail if the parent interface is
1208 * being detached. Regardless, we should do a
1209 * best effort to free this interface as much
1210 * as possible as all callers expect vlan
1211 * destruction to succeed.
1213 (void)if_delmulti(parent, (struct sockaddr *)&sdl);
1214 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1218 vlan_setflags(ifp, 0); /* clear special flags on parent */
1220 trunk->vlans[ifv->ifv_tag] = NULL;
1223 vlan_remhash(trunk, ifv);
1225 ifv->ifv_trunk = NULL;
1228 * Check if we were the last.
1230 if (trunk->refcnt == 0) {
1231 trunk->parent->if_vlantrunk = NULL;
1233 * XXXGL: If some ithread has already entered
1234 * vlan_input() and is now blocked on the trunk
1235 * lock, then it should preempt us right after
1236 * unlock and finish its work. Then we will acquire
1237 * lock again in trunk_destroy().
1239 TRUNK_UNLOCK(trunk);
1240 trunk_destroy(trunk);
1242 TRUNK_UNLOCK(trunk);
1245 /* Disconnect from parent. */
1246 if (ifv->ifv_pflags)
1247 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1248 ifp->if_mtu = ETHERMTU;
1249 ifp->if_link_state = LINK_STATE_UNKNOWN;
1250 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1253 * Only dispatch an event if vlan was
1254 * attached, otherwise there is nothing
1255 * to cleanup anyway.
1258 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag);
1261 /* Handle a reference counted flag that should be set on the parent as well */
1263 vlan_setflag(struct ifnet *ifp, int flag, int status,
1264 int (*func)(struct ifnet *, int))
1269 /* XXX VLAN_LOCK_ASSERT(); */
1271 ifv = ifp->if_softc;
1272 status = status ? (ifp->if_flags & flag) : 0;
1273 /* Now "status" contains the flag value or 0 */
1276 * See if recorded parent's status is different from what
1277 * we want it to be. If it is, flip it. We record parent's
1278 * status in ifv_pflags so that we won't clear parent's flag
1279 * we haven't set. In fact, we don't clear or set parent's
1280 * flags directly, but get or release references to them.
1281 * That's why we can be sure that recorded flags still are
1282 * in accord with actual parent's flags.
1284 if (status != (ifv->ifv_pflags & flag)) {
1285 error = (*func)(PARENT(ifv), status);
1288 ifv->ifv_pflags &= ~flag;
1289 ifv->ifv_pflags |= status;
1295 * Handle IFF_* flags that require certain changes on the parent:
1296 * if "status" is true, update parent's flags respective to our if_flags;
1297 * if "status" is false, forcedly clear the flags set on parent.
1300 vlan_setflags(struct ifnet *ifp, int status)
1304 for (i = 0; vlan_pflags[i].flag; i++) {
1305 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1306 status, vlan_pflags[i].func);
1313 /* Inform all vlans that their parent has changed link state */
1315 vlan_link_state(struct ifnet *ifp, int link)
1317 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1323 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1324 if (trunk->vlans[i] != NULL) {
1325 ifv = trunk->vlans[i];
1327 for (i = 0; i < (1 << trunk->hwidth); i++)
1328 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
1330 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1331 if_link_state_change(ifv->ifv_ifp,
1332 trunk->parent->if_link_state);
1334 TRUNK_UNLOCK(trunk);
1338 vlan_capabilities(struct ifvlan *ifv)
1340 struct ifnet *p = PARENT(ifv);
1341 struct ifnet *ifp = ifv->ifv_ifp;
1343 TRUNK_LOCK_ASSERT(TRUNK(ifv));
1346 * If the parent interface can do checksum offloading
1347 * on VLANs, then propagate its hardware-assisted
1348 * checksumming flags. Also assert that checksum
1349 * offloading requires hardware VLAN tagging.
1351 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1352 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1354 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1355 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1356 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1357 ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP |
1358 CSUM_UDP | CSUM_SCTP | CSUM_IP_FRAGS | CSUM_FRAGMENT);
1360 ifp->if_capenable = 0;
1361 ifp->if_hwassist = 0;
1364 * If the parent interface can do TSO on VLANs then
1365 * propagate the hardware-assisted flag. TSO on VLANs
1366 * does not necessarily require hardware VLAN tagging.
1368 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1369 ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO;
1370 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1371 ifp->if_capenable |= p->if_capenable & IFCAP_TSO;
1372 ifp->if_hwassist |= p->if_hwassist & CSUM_TSO;
1374 ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO);
1375 ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO);
1380 vlan_trunk_capabilities(struct ifnet *ifp)
1382 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1388 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1389 if (trunk->vlans[i] != NULL) {
1390 ifv = trunk->vlans[i];
1392 for (i = 0; i < (1 << trunk->hwidth); i++) {
1393 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1395 vlan_capabilities(ifv);
1397 TRUNK_UNLOCK(trunk);
1401 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1409 ifr = (struct ifreq *)data;
1410 ifv = ifp->if_softc;
1415 if (TRUNK(ifv) != NULL) {
1418 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1419 /* Limit the result to the parent's current config. */
1421 struct ifmediareq *ifmr;
1423 ifmr = (struct ifmediareq *)data;
1424 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1425 ifmr->ifm_count = 1;
1426 error = copyout(&ifmr->ifm_current,
1443 * Set the interface MTU.
1446 if (TRUNK(ifv) != NULL) {
1448 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1450 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1453 ifp->if_mtu = ifr->ifr_mtu;
1461 if (ifp->if_vnet != ifp->if_home_vnet) {
1466 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1469 if (vlr.vlr_parent[0] == '\0') {
1473 p = ifunit(vlr.vlr_parent);
1479 * Don't let the caller set up a VLAN tag with
1480 * anything except VLID bits.
1482 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1486 error = vlan_config(ifv, p, vlr.vlr_tag);
1490 /* Update flags on the parent, if necessary. */
1491 vlan_setflags(ifp, 1);
1496 if (ifp->if_vnet != ifp->if_home_vnet) {
1501 bzero(&vlr, sizeof(vlr));
1503 if (TRUNK(ifv) != NULL) {
1504 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1505 sizeof(vlr.vlr_parent));
1506 vlr.vlr_tag = ifv->ifv_tag;
1509 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1514 * We should propagate selected flags to the parent,
1515 * e.g., promiscuous mode.
1517 if (TRUNK(ifv) != NULL)
1518 error = vlan_setflags(ifp, 1);
1524 * If we don't have a parent, just remember the membership for
1527 if (TRUNK(ifv) != NULL)
1528 error = vlan_setmulti(ifp);
1532 error = ether_ioctl(ifp, cmd, data);