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;
144 * We have a global mutex, that is used to serialize configuration
145 * changes and isn't used in normal packet delivery.
147 * We also have a per-trunk rwlock, that is locked shared on packet
148 * processing and exclusive when configuration is changed.
150 * The VLAN_ARRAY substitutes the dynamic hash with a static array
151 * with 4096 entries. In theory this can give a boost in processing,
152 * however on practice it does not. Probably this is because array
153 * is too big to fit into CPU cache.
155 static struct mtx ifv_mtx;
156 #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
157 #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx)
158 #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED)
159 #define VLAN_LOCK() mtx_lock(&ifv_mtx)
160 #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx)
161 #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME)
162 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
163 #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw)
164 #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw)
165 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
166 #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw)
167 #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw)
168 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
171 static void vlan_inithash(struct ifvlantrunk *trunk);
172 static void vlan_freehash(struct ifvlantrunk *trunk);
173 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
174 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
175 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
176 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
179 static void trunk_destroy(struct ifvlantrunk *trunk);
181 static void vlan_start(struct ifnet *ifp);
182 static void vlan_init(void *foo);
183 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
184 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
185 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
186 int (*func)(struct ifnet *, int));
187 static int vlan_setflags(struct ifnet *ifp, int status);
188 static int vlan_setmulti(struct ifnet *ifp);
189 static int vlan_unconfig(struct ifnet *ifp);
190 static int vlan_unconfig_locked(struct ifnet *ifp);
191 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
192 static void vlan_link_state(struct ifnet *ifp, int link);
193 static void vlan_capabilities(struct ifvlan *ifv);
194 static void vlan_trunk_capabilities(struct ifnet *ifp);
196 static struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
197 const char *, int *);
198 static int vlan_clone_match(struct if_clone *, const char *);
199 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
200 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
202 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
204 static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
205 IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
208 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
211 vlan_inithash(struct ifvlantrunk *trunk)
216 * The trunk must not be locked here since we call malloc(M_WAITOK).
217 * It is OK in case this function is called before the trunk struct
218 * gets hooked up and becomes visible from other threads.
221 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
222 ("%s: hash already initialized", __func__));
224 trunk->hwidth = VLAN_DEF_HWIDTH;
225 n = 1 << trunk->hwidth;
226 trunk->hmask = n - 1;
227 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
228 for (i = 0; i < n; i++)
229 LIST_INIT(&trunk->hash[i]);
233 vlan_freehash(struct ifvlantrunk *trunk)
238 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
239 for (i = 0; i < (1 << trunk->hwidth); i++)
240 KASSERT(LIST_EMPTY(&trunk->hash[i]),
241 ("%s: hash table not empty", __func__));
243 free(trunk->hash, M_VLAN);
245 trunk->hwidth = trunk->hmask = 0;
249 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
254 TRUNK_LOCK_ASSERT(trunk);
255 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
257 b = 1 << trunk->hwidth;
258 i = HASH(ifv->ifv_tag, trunk->hmask);
259 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
260 if (ifv->ifv_tag == ifv2->ifv_tag)
264 * Grow the hash when the number of vlans exceeds half of the number of
265 * hash buckets squared. This will make the average linked-list length
268 if (trunk->refcnt > (b * b) / 2) {
269 vlan_growhash(trunk, 1);
270 i = HASH(ifv->ifv_tag, trunk->hmask);
272 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
279 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
284 TRUNK_LOCK_ASSERT(trunk);
285 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
287 b = 1 << trunk->hwidth;
288 i = HASH(ifv->ifv_tag, trunk->hmask);
289 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
292 LIST_REMOVE(ifv2, ifv_list);
293 if (trunk->refcnt < (b * b) / 2)
294 vlan_growhash(trunk, -1);
298 panic("%s: vlan not found\n", __func__);
299 return (ENOENT); /*NOTREACHED*/
303 * Grow the hash larger or smaller if memory permits.
306 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
309 struct ifvlanhead *hash2;
310 int hwidth2, i, j, n, n2;
312 TRUNK_LOCK_ASSERT(trunk);
313 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
316 /* Harmless yet obvious coding error */
317 printf("%s: howmuch is 0\n", __func__);
321 hwidth2 = trunk->hwidth + howmuch;
322 n = 1 << trunk->hwidth;
324 /* Do not shrink the table below the default */
325 if (hwidth2 < VLAN_DEF_HWIDTH)
328 /* M_NOWAIT because we're called with trunk mutex held */
329 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
331 printf("%s: out of memory -- hash size not changed\n",
333 return; /* We can live with the old hash table */
335 for (j = 0; j < n2; j++)
336 LIST_INIT(&hash2[j]);
337 for (i = 0; i < n; i++)
338 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
339 LIST_REMOVE(ifv, ifv_list);
340 j = HASH(ifv->ifv_tag, n2 - 1);
341 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
343 free(trunk->hash, M_VLAN);
345 trunk->hwidth = hwidth2;
346 trunk->hmask = n2 - 1;
349 if_printf(trunk->parent,
350 "VLAN hash table resized from %d to %d buckets\n", n, n2);
353 static __inline struct ifvlan *
354 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
358 TRUNK_LOCK_RASSERT(trunk);
360 LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
361 if (ifv->ifv_tag == tag)
367 /* Debugging code to view the hashtables. */
369 vlan_dumphash(struct ifvlantrunk *trunk)
374 for (i = 0; i < (1 << trunk->hwidth); i++) {
376 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
377 printf("%s ", ifv->ifv_ifp->if_xname);
382 #endif /* !VLAN_ARRAY */
385 trunk_destroy(struct ifvlantrunk *trunk)
391 vlan_freehash(trunk);
393 trunk->parent->if_vlantrunk = NULL;
395 TRUNK_LOCK_DESTROY(trunk);
400 * Program our multicast filter. What we're actually doing is
401 * programming the multicast filter of the parent. This has the
402 * side effect of causing the parent interface to receive multicast
403 * traffic that it doesn't really want, which ends up being discarded
404 * later by the upper protocol layers. Unfortunately, there's no way
405 * to avoid this: there really is only one physical interface.
407 * XXX: There is a possible race here if more than one thread is
408 * modifying the multicast state of the vlan interface at the same time.
411 vlan_setmulti(struct ifnet *ifp)
414 struct ifmultiaddr *ifma, *rifma = NULL;
416 struct vlan_mc_entry *mc;
417 struct sockaddr_dl sdl;
420 /*VLAN_LOCK_ASSERT();*/
422 /* Find the parent. */
426 CURVNET_SET_QUIET(ifp_p->if_vnet);
428 bzero((char *)&sdl, sizeof(sdl));
429 sdl.sdl_len = sizeof(sdl);
430 sdl.sdl_family = AF_LINK;
431 sdl.sdl_index = ifp_p->if_index;
432 sdl.sdl_type = IFT_ETHER;
433 sdl.sdl_alen = ETHER_ADDR_LEN;
435 /* First, remove any existing filter entries. */
436 while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
437 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
438 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
441 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
445 /* Now program new ones. */
446 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
447 if (ifma->ifma_addr->sa_family != AF_LINK)
449 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
452 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
453 (char *)&mc->mc_addr, ETHER_ADDR_LEN);
454 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
455 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
456 LLADDR(&sdl), ETHER_ADDR_LEN);
457 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
467 * A handler for network interface departure events.
468 * Track departure of trunks here so that we don't access invalid
469 * pointers or whatever if a trunk is ripped from under us, e.g.,
470 * by ejecting its hot-plug card. However, if an ifnet is simply
471 * being renamed, then there's no need to tear down the state.
474 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
480 * Check if it's a trunk interface first of all
481 * to avoid needless locking.
483 if (ifp->if_vlantrunk == NULL)
486 /* If the ifnet is just being renamed, don't do anything. */
487 if (ifp->if_flags & IFF_RENAMING)
492 * OK, it's a trunk. Loop over and detach all vlan's on it.
493 * Check trunk pointer after each vlan_unconfig() as it will
494 * free it and set to NULL after the last vlan was detached.
497 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
498 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
499 vlan_unconfig_locked(ifv->ifv_ifp);
500 if (ifp->if_vlantrunk == NULL)
503 #else /* VLAN_ARRAY */
505 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
506 if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
507 vlan_unconfig_locked(ifv->ifv_ifp);
508 if (ifp->if_vlantrunk)
509 goto restart; /* trunk->hwidth can change */
513 #endif /* VLAN_ARRAY */
514 /* Trunk should have been destroyed in vlan_unconfig(). */
515 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
520 * VLAN support can be loaded as a module. The only place in the
521 * system that's intimately aware of this is ether_input. We hook
522 * into this code through vlan_input_p which is defined there and
523 * set here. Noone else in the system should be aware of this so
524 * we use an explicit reference here.
526 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
528 /* For if_link_state_change() eyes only... */
529 extern void (*vlan_link_state_p)(struct ifnet *, int);
532 vlan_modevent(module_t mod, int type, void *data)
537 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
538 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
539 if (ifdetach_tag == NULL)
542 vlan_input_p = vlan_input;
543 vlan_link_state_p = vlan_link_state;
544 vlan_trunk_cap_p = vlan_trunk_capabilities;
545 if_clone_attach(&vlan_cloner);
547 printf("vlan: initialized, using "
551 "hash tables with chaining"
557 if_clone_detach(&vlan_cloner);
558 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
560 vlan_link_state_p = NULL;
561 vlan_trunk_cap_p = NULL;
564 printf("vlan: unloaded\n");
572 static moduledata_t vlan_mod = {
578 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
579 MODULE_VERSION(if_vlan, 3);
581 static struct ifnet *
582 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
588 /* Check for <etherif>.<vlan> style interface names. */
589 IFNET_RLOCK_NOSLEEP();
590 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
591 if (ifp->if_type != IFT_ETHER)
593 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
595 cp = name + strlen(ifp->if_xname);
601 for(; *cp >= '0' && *cp <= '9'; cp++)
602 t = (t * 10) + (*cp - '0');
609 IFNET_RUNLOCK_NOSLEEP();
615 vlan_clone_match(struct if_clone *ifc, const char *name)
619 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
622 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
624 for (cp = name + 4; *cp != '\0'; cp++) {
625 if (*cp < '0' || *cp > '9')
633 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
645 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
648 * There are 3 (ugh) ways to specify the cloned device:
649 * o pass a parameter block with the clone request.
650 * o specify parameters in the text of the clone device name
651 * o specify no parameters and get an unattached device that
652 * must be configured separately.
653 * The first technique is preferred; the latter two are
654 * supported for backwards compatibilty.
657 error = copyin(params, &vlr, sizeof(vlr));
660 p = ifunit(vlr.vlr_parent);
664 * Don't let the caller set up a VLAN tag with
665 * anything except VLID bits.
667 if (vlr.vlr_tag & ~EVL_VLID_MASK)
669 error = ifc_name2unit(name, &unit);
675 wildcard = (unit < 0);
676 } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
682 * Don't let the caller set up a VLAN tag with
683 * anything except VLID bits.
685 if (tag & ~EVL_VLID_MASK)
690 error = ifc_name2unit(name, &unit);
694 wildcard = (unit < 0);
697 error = ifc_alloc_unit(ifc, &unit);
701 /* In the wildcard case, we need to update the name. */
703 for (dp = name; *dp != '\0'; dp++);
704 if (snprintf(dp, len - (dp-name), "%d", unit) >
705 len - (dp-name) - 1) {
706 panic("%s: interface name too long", __func__);
710 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
711 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
713 ifc_free_unit(ifc, unit);
717 SLIST_INIT(&ifv->vlan_mc_listhead);
721 * Set the name manually rather than using if_initname because
722 * we don't conform to the default naming convention for interfaces.
724 strlcpy(ifp->if_xname, name, IFNAMSIZ);
725 ifp->if_dname = ifc->ifc_name;
726 ifp->if_dunit = unit;
727 /* NB: flags are not set here */
728 ifp->if_linkmib = &ifv->ifv_mib;
729 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
730 /* NB: mtu is not set here */
732 ifp->if_init = vlan_init;
733 ifp->if_start = vlan_start;
734 ifp->if_ioctl = vlan_ioctl;
735 ifp->if_snd.ifq_maxlen = ifqmaxlen;
736 ifp->if_flags = VLAN_IFFLAGS;
737 ether_ifattach(ifp, eaddr);
738 /* Now undo some of the damage... */
739 ifp->if_baudrate = 0;
740 ifp->if_type = IFT_L2VLAN;
741 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
744 error = vlan_config(ifv, p, tag);
747 * Since we've partialy failed, we need to back
748 * out all the way, otherwise userland could get
749 * confused. Thus, we destroy the interface.
753 if_free_type(ifp, IFT_ETHER);
754 ifc_free_unit(ifc, unit);
760 /* Update flags on the parent, if necessary. */
761 vlan_setflags(ifp, 1);
768 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
770 struct ifvlan *ifv = ifp->if_softc;
771 int unit = ifp->if_dunit;
773 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
774 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
775 if_free_type(ifp, IFT_ETHER);
777 ifc_free_unit(ifc, unit);
783 * The ifp->if_init entry point for vlan(4) is a no-op.
786 vlan_init(void *foo __unused)
791 * The if_start method for vlan(4) interface. It doesn't
792 * raises the IFF_DRV_OACTIVE flag, since it is called
793 * only from IFQ_HANDOFF() macro in ether_output_frame().
794 * If the interface queue is full, and vlan_start() is
795 * not called, the queue would never get emptied and
796 * interface would stall forever.
799 vlan_start(struct ifnet *ifp)
810 IF_DEQUEUE(&ifp->if_snd, m);
816 * Do not run parent's if_start() if the parent is not up,
817 * or parent's driver will cause a system crash.
819 if (!UP_AND_RUNNING(p)) {
821 ifp->if_collisions++;
826 * Pad the frame to the minimum size allowed if told to.
827 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
828 * paragraph C.4.4.3.b. It can help to work around buggy
829 * bridges that violate paragraph C.4.4.3.a from the same
830 * document, i.e., fail to pad short frames after untagging.
831 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
832 * untagging it will produce a 62-byte frame, which is a runt
833 * and requires padding. There are VLAN-enabled network
834 * devices that just discard such runts instead or mishandle
838 static char pad[8]; /* just zeros */
841 for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
842 n > 0; n -= sizeof(pad))
843 if (!m_append(m, min(n, sizeof(pad)), pad))
847 if_printf(ifp, "cannot pad short frame\n");
855 * If underlying interface can do VLAN tag insertion itself,
856 * just pass the packet along. However, we need some way to
857 * tell the interface where the packet came from so that it
858 * knows how to find the VLAN tag to use, so we attach a
859 * packet tag that holds it.
861 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
862 m->m_pkthdr.ether_vtag = ifv->ifv_tag;
863 m->m_flags |= M_VLANTAG;
865 m = ether_vlanencap(m, ifv->ifv_tag);
868 "unable to prepend VLAN header\n");
875 * Send it, precisely as ether_output() would have.
876 * We are already running at splimp.
878 error = (p->if_transmit)(p, m);
887 vlan_input(struct ifnet *ifp, struct mbuf *m)
889 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
893 KASSERT(trunk != NULL, ("%s: no trunk", __func__));
895 if (m->m_flags & M_VLANTAG) {
897 * Packet is tagged, but m contains a normal
898 * Ethernet frame; the tag is stored out-of-band.
900 tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
901 m->m_flags &= ~M_VLANTAG;
903 struct ether_vlan_header *evl;
906 * Packet is tagged in-band as specified by 802.1q.
908 switch (ifp->if_type) {
910 if (m->m_len < sizeof(*evl) &&
911 (m = m_pullup(m, sizeof(*evl))) == NULL) {
912 if_printf(ifp, "cannot pullup VLAN header\n");
915 evl = mtod(m, struct ether_vlan_header *);
916 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
919 * Remove the 802.1q header by copying the Ethernet
920 * addresses over it and adjusting the beginning of
921 * the data in the mbuf. The encapsulated Ethernet
922 * type field is already in place.
924 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
925 ETHER_HDR_LEN - ETHER_TYPE_LEN);
926 m_adj(m, ETHER_VLAN_ENCAP_LEN);
931 panic("%s: %s has unsupported if_type %u",
932 __func__, ifp->if_xname, ifp->if_type);
942 ifv = trunk->vlans[tag];
944 ifv = vlan_gethash(trunk, tag);
946 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
947 TRUNK_RUNLOCK(trunk);
952 TRUNK_RUNLOCK(trunk);
954 m->m_pkthdr.rcvif = ifv->ifv_ifp;
955 ifv->ifv_ifp->if_ipackets++;
957 /* Pass it back through the parent's input routine. */
958 (*ifp->if_input)(ifv->ifv_ifp, m);
962 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
964 struct ifvlantrunk *trunk;
968 /* VID numbers 0x0 and 0xFFF are reserved */
969 if (tag == 0 || tag == 0xFFF)
971 if (p->if_type != IFT_ETHER)
972 return (EPROTONOSUPPORT);
973 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
974 return (EPROTONOSUPPORT);
978 if (p->if_vlantrunk == NULL) {
979 trunk = malloc(sizeof(struct ifvlantrunk),
980 M_VLAN, M_WAITOK | M_ZERO);
982 vlan_inithash(trunk);
985 if (p->if_vlantrunk != NULL) {
986 /* A race that that is very unlikely to be hit. */
988 vlan_freehash(trunk);
993 TRUNK_LOCK_INIT(trunk);
995 p->if_vlantrunk = trunk;
1000 trunk = p->if_vlantrunk;
1004 ifv->ifv_tag = tag; /* must set this before vlan_inshash() */
1006 if (trunk->vlans[tag] != NULL) {
1010 trunk->vlans[tag] = ifv;
1013 error = vlan_inshash(trunk, ifv);
1017 ifv->ifv_proto = ETHERTYPE_VLAN;
1018 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1019 ifv->ifv_mintu = ETHERMIN;
1020 ifv->ifv_pflags = 0;
1023 * If the parent supports the VLAN_MTU capability,
1024 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1027 if (p->if_capenable & IFCAP_VLAN_MTU) {
1029 * No need to fudge the MTU since the parent can
1030 * handle extended frames.
1032 ifv->ifv_mtufudge = 0;
1035 * Fudge the MTU by the encapsulation size. This
1036 * makes us incompatible with strictly compliant
1037 * 802.1Q implementations, but allows us to use
1038 * the feature with other NetBSD implementations,
1039 * which might still be useful.
1041 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1044 ifv->ifv_trunk = trunk;
1046 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1047 ifp->if_baudrate = p->if_baudrate;
1049 * Copy only a selected subset of flags from the parent.
1050 * Other flags are none of our business.
1052 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1053 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1054 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1055 #undef VLAN_COPY_FLAGS
1057 ifp->if_link_state = p->if_link_state;
1059 vlan_capabilities(ifv);
1062 * Set up our ``Ethernet address'' to reflect the underlying
1063 * physical interface's.
1065 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
1068 * Configure multicast addresses that may already be
1069 * joined on the vlan device.
1071 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1073 /* We are ready for operation now. */
1074 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1076 TRUNK_UNLOCK(trunk);
1078 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag);
1085 vlan_unconfig(struct ifnet *ifp)
1090 ret = vlan_unconfig_locked(ifp);
1096 vlan_unconfig_locked(struct ifnet *ifp)
1098 struct ifvlantrunk *trunk;
1099 struct vlan_mc_entry *mc;
1101 struct ifnet *parent;
1106 ifv = ifp->if_softc;
1107 trunk = ifv->ifv_trunk;
1110 if (trunk != NULL) {
1111 struct sockaddr_dl sdl;
1114 parent = trunk->parent;
1117 * Since the interface is being unconfigured, we need to
1118 * empty the list of multicast groups that we may have joined
1119 * while we were alive from the parent's list.
1121 bzero((char *)&sdl, sizeof(sdl));
1122 sdl.sdl_len = sizeof(sdl);
1123 sdl.sdl_family = AF_LINK;
1124 sdl.sdl_index = parent->if_index;
1125 sdl.sdl_type = IFT_ETHER;
1126 sdl.sdl_alen = ETHER_ADDR_LEN;
1128 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1129 bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
1131 error = if_delmulti(parent, (struct sockaddr *)&sdl);
1134 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1138 vlan_setflags(ifp, 0); /* clear special flags on parent */
1140 trunk->vlans[ifv->ifv_tag] = NULL;
1143 vlan_remhash(trunk, ifv);
1145 ifv->ifv_trunk = NULL;
1148 * Check if we were the last.
1150 if (trunk->refcnt == 0) {
1151 trunk->parent->if_vlantrunk = NULL;
1153 * XXXGL: If some ithread has already entered
1154 * vlan_input() and is now blocked on the trunk
1155 * lock, then it should preempt us right after
1156 * unlock and finish its work. Then we will acquire
1157 * lock again in trunk_destroy().
1159 TRUNK_UNLOCK(trunk);
1160 trunk_destroy(trunk);
1162 TRUNK_UNLOCK(trunk);
1165 /* Disconnect from parent. */
1166 if (ifv->ifv_pflags)
1167 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1168 ifp->if_mtu = ETHERMTU;
1169 ifp->if_link_state = LINK_STATE_UNKNOWN;
1170 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1173 * Only dispatch an event if vlan was
1174 * attached, otherwise there is nothing
1175 * to cleanup anyway.
1178 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag);
1183 /* Handle a reference counted flag that should be set on the parent as well */
1185 vlan_setflag(struct ifnet *ifp, int flag, int status,
1186 int (*func)(struct ifnet *, int))
1191 /* XXX VLAN_LOCK_ASSERT(); */
1193 ifv = ifp->if_softc;
1194 status = status ? (ifp->if_flags & flag) : 0;
1195 /* Now "status" contains the flag value or 0 */
1198 * See if recorded parent's status is different from what
1199 * we want it to be. If it is, flip it. We record parent's
1200 * status in ifv_pflags so that we won't clear parent's flag
1201 * we haven't set. In fact, we don't clear or set parent's
1202 * flags directly, but get or release references to them.
1203 * That's why we can be sure that recorded flags still are
1204 * in accord with actual parent's flags.
1206 if (status != (ifv->ifv_pflags & flag)) {
1207 error = (*func)(PARENT(ifv), status);
1210 ifv->ifv_pflags &= ~flag;
1211 ifv->ifv_pflags |= status;
1217 * Handle IFF_* flags that require certain changes on the parent:
1218 * if "status" is true, update parent's flags respective to our if_flags;
1219 * if "status" is false, forcedly clear the flags set on parent.
1222 vlan_setflags(struct ifnet *ifp, int status)
1226 for (i = 0; vlan_pflags[i].flag; i++) {
1227 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1228 status, vlan_pflags[i].func);
1235 /* Inform all vlans that their parent has changed link state */
1237 vlan_link_state(struct ifnet *ifp, int link)
1239 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1245 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1246 if (trunk->vlans[i] != NULL) {
1247 ifv = trunk->vlans[i];
1249 for (i = 0; i < (1 << trunk->hwidth); i++)
1250 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
1252 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1253 if_link_state_change(ifv->ifv_ifp,
1254 trunk->parent->if_link_state);
1256 TRUNK_UNLOCK(trunk);
1260 vlan_capabilities(struct ifvlan *ifv)
1262 struct ifnet *p = PARENT(ifv);
1263 struct ifnet *ifp = ifv->ifv_ifp;
1265 TRUNK_LOCK_ASSERT(TRUNK(ifv));
1268 * If the parent interface can do checksum offloading
1269 * on VLANs, then propagate its hardware-assisted
1270 * checksumming flags. Also assert that checksum
1271 * offloading requires hardware VLAN tagging.
1273 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1274 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1276 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1277 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1278 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1279 ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP |
1280 CSUM_UDP | CSUM_SCTP | CSUM_IP_FRAGS | CSUM_FRAGMENT);
1282 ifp->if_capenable = 0;
1283 ifp->if_hwassist = 0;
1286 * If the parent interface can do TSO on VLANs then
1287 * propagate the hardware-assisted flag. TSO on VLANs
1288 * does not necessarily require hardware VLAN tagging.
1290 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1291 ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO;
1292 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1293 ifp->if_capenable |= p->if_capenable & IFCAP_TSO;
1294 ifp->if_hwassist |= p->if_hwassist & CSUM_TSO;
1296 ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO);
1297 ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO);
1302 vlan_trunk_capabilities(struct ifnet *ifp)
1304 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1310 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1311 if (trunk->vlans[i] != NULL) {
1312 ifv = trunk->vlans[i];
1314 for (i = 0; i < (1 << trunk->hwidth); i++) {
1315 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1317 vlan_capabilities(ifv);
1319 TRUNK_UNLOCK(trunk);
1323 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1331 ifr = (struct ifreq *)data;
1332 ifv = ifp->if_softc;
1337 if (TRUNK(ifv) != NULL) {
1338 error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv),
1339 SIOCGIFMEDIA, data);
1341 /* Limit the result to the parent's current config. */
1343 struct ifmediareq *ifmr;
1345 ifmr = (struct ifmediareq *)data;
1346 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1347 ifmr->ifm_count = 1;
1348 error = copyout(&ifmr->ifm_current,
1365 * Set the interface MTU.
1368 if (TRUNK(ifv) != NULL) {
1370 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1372 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1375 ifp->if_mtu = ifr->ifr_mtu;
1382 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1385 if (vlr.vlr_parent[0] == '\0') {
1389 p = ifunit(vlr.vlr_parent);
1395 * Don't let the caller set up a VLAN tag with
1396 * anything except VLID bits.
1398 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1402 error = vlan_config(ifv, p, vlr.vlr_tag);
1406 /* Update flags on the parent, if necessary. */
1407 vlan_setflags(ifp, 1);
1411 bzero(&vlr, sizeof(vlr));
1413 if (TRUNK(ifv) != NULL) {
1414 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1415 sizeof(vlr.vlr_parent));
1416 vlr.vlr_tag = ifv->ifv_tag;
1419 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1424 * We should propagate selected flags to the parent,
1425 * e.g., promiscuous mode.
1427 if (TRUNK(ifv) != NULL)
1428 error = vlan_setflags(ifp, 1);
1434 * If we don't have a parent, just remember the membership for
1437 if (TRUNK(ifv) != NULL)
1438 error = vlan_setmulti(ifp);
1442 error = ether_ioctl(ifp, cmd, data);