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
33 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
34 * Might be extended some day to also handle IEEE 802.1p priority
35 * tagging. This is sort of sneaky in the implementation, since
36 * we need to pretend to be enough of an Ethernet implementation
37 * to make arp work. The way we do this is by telling everyone
38 * that we are an Ethernet, and then catch the packets that
39 * ether_output() left on our output queue when it calls
40 * if_start(), rewrite them for use by the real outgoing interface,
41 * and ask it to send them.
46 #include <sys/param.h>
47 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/rwlock.h>
53 #include <sys/queue.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/systm.h>
60 #include <net/ethernet.h>
62 #include <net/if_clone.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/if_vlan_var.h>
68 #define VLANNAME "vlan"
69 #define VLAN_DEF_HWIDTH 4
70 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
72 #define UP_AND_RUNNING(ifp) \
73 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
75 LIST_HEAD(ifvlanhead, ifvlan);
78 struct ifnet *parent; /* parent interface of this trunk */
81 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
82 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
84 struct ifvlanhead *hash; /* dynamic hash-list table */
91 struct vlan_mc_entry {
92 struct ether_addr mc_addr;
93 SLIST_ENTRY(vlan_mc_entry) mc_entries;
97 struct ifvlantrunk *ifv_trunk;
98 struct ifnet *ifv_ifp;
99 #define TRUNK(ifv) ((ifv)->ifv_trunk)
100 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
101 int ifv_pflags; /* special flags we have set on parent */
103 int ifvm_encaplen; /* encapsulation length */
104 int ifvm_mtufudge; /* MTU fudged by this much */
105 int ifvm_mintu; /* min transmission unit */
106 uint16_t ifvm_proto; /* encapsulation ethertype */
107 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
109 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
111 LIST_ENTRY(ifvlan) ifv_list;
114 #define ifv_proto ifv_mib.ifvm_proto
115 #define ifv_tag ifv_mib.ifvm_tag
116 #define ifv_encaplen ifv_mib.ifvm_encaplen
117 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
118 #define ifv_mintu ifv_mib.ifvm_mintu
120 /* Special flags we should propagate to parent. */
123 int (*func)(struct ifnet *, int);
125 {IFF_PROMISC, ifpromisc},
126 {IFF_ALLMULTI, if_allmulti},
130 SYSCTL_DECL(_net_link);
131 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
132 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
134 static int soft_pad = 0;
135 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
136 "pad short frames before tagging");
138 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
140 static eventhandler_tag ifdetach_tag;
143 * We have a global mutex, that is used to serialize configuration
144 * changes and isn't used in normal packet delivery.
146 * We also have a per-trunk rwlock, that is locked shared on packet
147 * processing and exclusive when configuration is changed.
149 * The VLAN_ARRAY substitutes the dynamic hash with a static array
150 * with 4096 entries. In theory this can give a boost in processing,
151 * however on practice it does not. Probably this is because array
152 * is too big to fit into CPU cache.
154 static struct mtx ifv_mtx;
155 #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
156 #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx)
157 #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED)
158 #define VLAN_LOCK() mtx_lock(&ifv_mtx)
159 #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx)
160 #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME)
161 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
162 #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw)
163 #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw)
164 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
165 #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw)
166 #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw)
167 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
170 static void vlan_inithash(struct ifvlantrunk *trunk);
171 static void vlan_freehash(struct ifvlantrunk *trunk);
172 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
173 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
174 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
175 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
178 static void trunk_destroy(struct ifvlantrunk *trunk);
180 static void vlan_start(struct ifnet *ifp);
181 static void vlan_init(void *foo);
182 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
183 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
184 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
185 int (*func)(struct ifnet *, int));
186 static int vlan_setflags(struct ifnet *ifp, int status);
187 static int vlan_setmulti(struct ifnet *ifp);
188 static int vlan_unconfig(struct ifnet *ifp);
189 static int vlan_unconfig_locked(struct ifnet *ifp);
190 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
191 static void vlan_link_state(struct ifnet *ifp, int link);
192 static void vlan_capabilities(struct ifvlan *ifv);
193 static void vlan_trunk_capabilities(struct ifnet *ifp);
195 static struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
196 const char *, int *);
197 static int vlan_clone_match(struct if_clone *, const char *);
198 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
199 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
201 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
203 static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
204 IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
207 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
210 vlan_inithash(struct ifvlantrunk *trunk)
215 * The trunk must not be locked here since we call malloc(M_WAITOK).
216 * It is OK in case this function is called before the trunk struct
217 * gets hooked up and becomes visible from other threads.
220 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
221 ("%s: hash already initialized", __func__));
223 trunk->hwidth = VLAN_DEF_HWIDTH;
224 n = 1 << trunk->hwidth;
225 trunk->hmask = n - 1;
226 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
227 for (i = 0; i < n; i++)
228 LIST_INIT(&trunk->hash[i]);
232 vlan_freehash(struct ifvlantrunk *trunk)
237 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
238 for (i = 0; i < (1 << trunk->hwidth); i++)
239 KASSERT(LIST_EMPTY(&trunk->hash[i]),
240 ("%s: hash table not empty", __func__));
242 free(trunk->hash, M_VLAN);
244 trunk->hwidth = trunk->hmask = 0;
248 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
253 TRUNK_LOCK_ASSERT(trunk);
254 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
256 b = 1 << trunk->hwidth;
257 i = HASH(ifv->ifv_tag, trunk->hmask);
258 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
259 if (ifv->ifv_tag == ifv2->ifv_tag)
263 * Grow the hash when the number of vlans exceeds half of the number of
264 * hash buckets squared. This will make the average linked-list length
267 if (trunk->refcnt > (b * b) / 2) {
268 vlan_growhash(trunk, 1);
269 i = HASH(ifv->ifv_tag, trunk->hmask);
271 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
278 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
283 TRUNK_LOCK_ASSERT(trunk);
284 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
286 b = 1 << trunk->hwidth;
287 i = HASH(ifv->ifv_tag, trunk->hmask);
288 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
291 LIST_REMOVE(ifv2, ifv_list);
292 if (trunk->refcnt < (b * b) / 2)
293 vlan_growhash(trunk, -1);
297 panic("%s: vlan not found\n", __func__);
298 return (ENOENT); /*NOTREACHED*/
302 * Grow the hash larger or smaller if memory permits.
305 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
308 struct ifvlanhead *hash2;
309 int hwidth2, i, j, n, n2;
311 TRUNK_LOCK_ASSERT(trunk);
312 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
315 /* Harmless yet obvious coding error */
316 printf("%s: howmuch is 0\n", __func__);
320 hwidth2 = trunk->hwidth + howmuch;
321 n = 1 << trunk->hwidth;
323 /* Do not shrink the table below the default */
324 if (hwidth2 < VLAN_DEF_HWIDTH)
327 /* M_NOWAIT because we're called with trunk mutex held */
328 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
330 printf("%s: out of memory -- hash size not changed\n",
332 return; /* We can live with the old hash table */
334 for (j = 0; j < n2; j++)
335 LIST_INIT(&hash2[j]);
336 for (i = 0; i < n; i++)
337 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
338 LIST_REMOVE(ifv, ifv_list);
339 j = HASH(ifv->ifv_tag, n2 - 1);
340 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
342 free(trunk->hash, M_VLAN);
344 trunk->hwidth = hwidth2;
345 trunk->hmask = n2 - 1;
348 if_printf(trunk->parent,
349 "VLAN hash table resized from %d to %d buckets\n", n, n2);
352 static __inline struct ifvlan *
353 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
357 TRUNK_LOCK_RASSERT(trunk);
359 LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
360 if (ifv->ifv_tag == tag)
366 /* Debugging code to view the hashtables. */
368 vlan_dumphash(struct ifvlantrunk *trunk)
373 for (i = 0; i < (1 << trunk->hwidth); i++) {
375 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
376 printf("%s ", ifv->ifv_ifp->if_xname);
381 #endif /* !VLAN_ARRAY */
384 trunk_destroy(struct ifvlantrunk *trunk)
390 vlan_freehash(trunk);
392 trunk->parent->if_vlantrunk = NULL;
394 TRUNK_LOCK_DESTROY(trunk);
399 * Program our multicast filter. What we're actually doing is
400 * programming the multicast filter of the parent. This has the
401 * side effect of causing the parent interface to receive multicast
402 * traffic that it doesn't really want, which ends up being discarded
403 * later by the upper protocol layers. Unfortunately, there's no way
404 * to avoid this: there really is only one physical interface.
406 * XXX: There is a possible race here if more than one thread is
407 * modifying the multicast state of the vlan interface at the same time.
410 vlan_setmulti(struct ifnet *ifp)
413 struct ifmultiaddr *ifma, *rifma = NULL;
415 struct vlan_mc_entry *mc;
416 struct sockaddr_dl sdl;
419 /*VLAN_LOCK_ASSERT();*/
421 /* Find the parent. */
425 CURVNET_SET_QUIET(ifp_p->if_vnet);
427 bzero((char *)&sdl, sizeof(sdl));
428 sdl.sdl_len = sizeof(sdl);
429 sdl.sdl_family = AF_LINK;
430 sdl.sdl_index = ifp_p->if_index;
431 sdl.sdl_type = IFT_ETHER;
432 sdl.sdl_alen = ETHER_ADDR_LEN;
434 /* First, remove any existing filter entries. */
435 while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
436 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
437 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
440 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
444 /* Now program new ones. */
445 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
446 if (ifma->ifma_addr->sa_family != AF_LINK)
448 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
451 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
452 (char *)&mc->mc_addr, ETHER_ADDR_LEN);
453 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
454 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
455 LLADDR(&sdl), ETHER_ADDR_LEN);
456 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
466 * A handler for network interface departure events.
467 * Track departure of trunks here so that we don't access invalid
468 * pointers or whatever if a trunk is ripped from under us, e.g.,
469 * by ejecting its hot-plug card.
472 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
478 * Check if it's a trunk interface first of all
479 * to avoid needless locking.
481 if (ifp->if_vlantrunk == NULL)
486 * OK, it's a trunk. Loop over and detach all vlan's on it.
487 * Check trunk pointer after each vlan_unconfig() as it will
488 * free it and set to NULL after the last vlan was detached.
491 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
492 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
493 vlan_unconfig_locked(ifv->ifv_ifp);
494 if (ifp->if_vlantrunk == NULL)
497 #else /* VLAN_ARRAY */
499 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
500 if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
501 vlan_unconfig_locked(ifv->ifv_ifp);
502 if (ifp->if_vlantrunk)
503 goto restart; /* trunk->hwidth can change */
507 #endif /* VLAN_ARRAY */
508 /* Trunk should have been destroyed in vlan_unconfig(). */
509 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
514 * VLAN support can be loaded as a module. The only place in the
515 * system that's intimately aware of this is ether_input. We hook
516 * into this code through vlan_input_p which is defined there and
517 * set here. Noone else in the system should be aware of this so
518 * we use an explicit reference here.
520 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
522 /* For if_link_state_change() eyes only... */
523 extern void (*vlan_link_state_p)(struct ifnet *, int);
526 vlan_modevent(module_t mod, int type, void *data)
531 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
532 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
533 if (ifdetach_tag == NULL)
536 vlan_input_p = vlan_input;
537 vlan_link_state_p = vlan_link_state;
538 vlan_trunk_cap_p = vlan_trunk_capabilities;
539 if_clone_attach(&vlan_cloner);
541 printf("vlan: initialized, using "
545 "hash tables with chaining"
551 if_clone_detach(&vlan_cloner);
552 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
554 vlan_link_state_p = NULL;
555 vlan_trunk_cap_p = NULL;
558 printf("vlan: unloaded\n");
566 static moduledata_t vlan_mod = {
572 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
573 MODULE_VERSION(if_vlan, 3);
575 static struct ifnet *
576 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
582 /* Check for <etherif>.<vlan> style interface names. */
584 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
585 if (ifp->if_type != IFT_ETHER)
587 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
589 cp = name + strlen(ifp->if_xname);
592 for(; *cp != '\0'; cp++) {
593 if (*cp < '0' || *cp > '9')
595 t = (t * 10) + (*cp - '0');
607 vlan_clone_match(struct if_clone *ifc, const char *name)
611 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
614 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
616 for (cp = name + 4; *cp != '\0'; cp++) {
617 if (*cp < '0' || *cp > '9')
625 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
637 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
640 * There are 3 (ugh) ways to specify the cloned device:
641 * o pass a parameter block with the clone request.
642 * o specify parameters in the text of the clone device name
643 * o specify no parameters and get an unattached device that
644 * must be configured separately.
645 * The first technique is preferred; the latter two are
646 * supported for backwards compatibilty.
649 error = copyin(params, &vlr, sizeof(vlr));
652 p = ifunit(vlr.vlr_parent);
656 * Don't let the caller set up a VLAN tag with
657 * anything except VLID bits.
659 if (vlr.vlr_tag & ~EVL_VLID_MASK)
661 error = ifc_name2unit(name, &unit);
667 wildcard = (unit < 0);
668 } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
674 * Don't let the caller set up a VLAN tag with
675 * anything except VLID bits.
677 if (tag & ~EVL_VLID_MASK)
682 error = ifc_name2unit(name, &unit);
686 wildcard = (unit < 0);
689 error = ifc_alloc_unit(ifc, &unit);
693 /* In the wildcard case, we need to update the name. */
695 for (dp = name; *dp != '\0'; dp++);
696 if (snprintf(dp, len - (dp-name), "%d", unit) >
697 len - (dp-name) - 1) {
698 panic("%s: interface name too long", __func__);
702 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
703 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
705 ifc_free_unit(ifc, unit);
709 SLIST_INIT(&ifv->vlan_mc_listhead);
713 * Set the name manually rather than using if_initname because
714 * we don't conform to the default naming convention for interfaces.
716 strlcpy(ifp->if_xname, name, IFNAMSIZ);
717 ifp->if_dname = ifc->ifc_name;
718 ifp->if_dunit = unit;
719 /* NB: flags are not set here */
720 ifp->if_linkmib = &ifv->ifv_mib;
721 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
722 /* NB: mtu is not set here */
724 ifp->if_init = vlan_init;
725 ifp->if_start = vlan_start;
726 ifp->if_ioctl = vlan_ioctl;
727 ifp->if_snd.ifq_maxlen = ifqmaxlen;
728 ifp->if_flags = VLAN_IFFLAGS;
729 ether_ifattach(ifp, eaddr);
730 /* Now undo some of the damage... */
731 ifp->if_baudrate = 0;
732 ifp->if_type = IFT_L2VLAN;
733 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
736 error = vlan_config(ifv, p, tag);
739 * Since we've partialy failed, we need to back
740 * out all the way, otherwise userland could get
741 * confused. Thus, we destroy the interface.
745 if_free_type(ifp, IFT_ETHER);
746 ifc_free_unit(ifc, unit);
752 /* Update flags on the parent, if necessary. */
753 vlan_setflags(ifp, 1);
760 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
762 struct ifvlan *ifv = ifp->if_softc;
763 int unit = ifp->if_dunit;
765 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
766 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
767 if_free_type(ifp, IFT_ETHER);
769 ifc_free_unit(ifc, unit);
775 * The ifp->if_init entry point for vlan(4) is a no-op.
778 vlan_init(void *foo __unused)
783 * The if_start method for vlan(4) interface. It doesn't
784 * raises the IFF_DRV_OACTIVE flag, since it is called
785 * only from IFQ_HANDOFF() macro in ether_output_frame().
786 * If the interface queue is full, and vlan_start() is
787 * not called, the queue would never get emptied and
788 * interface would stall forever.
791 vlan_start(struct ifnet *ifp)
802 IF_DEQUEUE(&ifp->if_snd, m);
808 * Do not run parent's if_start() if the parent is not up,
809 * or parent's driver will cause a system crash.
811 if (!UP_AND_RUNNING(p)) {
813 ifp->if_collisions++;
818 * Pad the frame to the minimum size allowed if told to.
819 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
820 * paragraph C.4.4.3.b. It can help to work around buggy
821 * bridges that violate paragraph C.4.4.3.a from the same
822 * document, i.e., fail to pad short frames after untagging.
823 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
824 * untagging it will produce a 62-byte frame, which is a runt
825 * and requires padding. There are VLAN-enabled network
826 * devices that just discard such runts instead or mishandle
830 static char pad[8]; /* just zeros */
833 for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
834 n > 0; n -= sizeof(pad))
835 if (!m_append(m, min(n, sizeof(pad)), pad))
839 if_printf(ifp, "cannot pad short frame\n");
847 * If underlying interface can do VLAN tag insertion itself,
848 * just pass the packet along. However, we need some way to
849 * tell the interface where the packet came from so that it
850 * knows how to find the VLAN tag to use, so we attach a
851 * packet tag that holds it.
853 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
854 m->m_pkthdr.ether_vtag = ifv->ifv_tag;
855 m->m_flags |= M_VLANTAG;
857 m = ether_vlanencap(m, ifv->ifv_tag);
860 "unable to prepend VLAN header\n");
867 * Send it, precisely as ether_output() would have.
868 * We are already running at splimp.
870 error = (p->if_transmit)(p, m);
879 vlan_input(struct ifnet *ifp, struct mbuf *m)
881 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
885 KASSERT(trunk != NULL, ("%s: no trunk", __func__));
887 if (m->m_flags & M_VLANTAG) {
889 * Packet is tagged, but m contains a normal
890 * Ethernet frame; the tag is stored out-of-band.
892 tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
893 m->m_flags &= ~M_VLANTAG;
895 struct ether_vlan_header *evl;
898 * Packet is tagged in-band as specified by 802.1q.
900 switch (ifp->if_type) {
902 if (m->m_len < sizeof(*evl) &&
903 (m = m_pullup(m, sizeof(*evl))) == NULL) {
904 if_printf(ifp, "cannot pullup VLAN header\n");
907 evl = mtod(m, struct ether_vlan_header *);
908 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
911 * Remove the 802.1q header by copying the Ethernet
912 * addresses over it and adjusting the beginning of
913 * the data in the mbuf. The encapsulated Ethernet
914 * type field is already in place.
916 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
917 ETHER_HDR_LEN - ETHER_TYPE_LEN);
918 m_adj(m, ETHER_VLAN_ENCAP_LEN);
923 panic("%s: %s has unsupported if_type %u",
924 __func__, ifp->if_xname, ifp->if_type);
934 ifv = trunk->vlans[tag];
936 ifv = vlan_gethash(trunk, tag);
938 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
939 TRUNK_RUNLOCK(trunk);
944 TRUNK_RUNLOCK(trunk);
946 m->m_pkthdr.rcvif = ifv->ifv_ifp;
947 ifv->ifv_ifp->if_ipackets++;
949 /* Pass it back through the parent's input routine. */
950 (*ifp->if_input)(ifv->ifv_ifp, m);
954 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
956 struct ifvlantrunk *trunk;
960 /* VID numbers 0x0 and 0xFFF are reserved */
961 if (tag == 0 || tag == 0xFFF)
963 if (p->if_type != IFT_ETHER)
964 return (EPROTONOSUPPORT);
965 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
966 return (EPROTONOSUPPORT);
970 if (p->if_vlantrunk == NULL) {
971 trunk = malloc(sizeof(struct ifvlantrunk),
972 M_VLAN, M_WAITOK | M_ZERO);
974 vlan_inithash(trunk);
977 if (p->if_vlantrunk != NULL) {
978 /* A race that that is very unlikely to be hit. */
980 vlan_freehash(trunk);
985 TRUNK_LOCK_INIT(trunk);
987 p->if_vlantrunk = trunk;
992 trunk = p->if_vlantrunk;
996 ifv->ifv_tag = tag; /* must set this before vlan_inshash() */
998 if (trunk->vlans[tag] != NULL) {
1002 trunk->vlans[tag] = ifv;
1005 error = vlan_inshash(trunk, ifv);
1009 ifv->ifv_proto = ETHERTYPE_VLAN;
1010 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1011 ifv->ifv_mintu = ETHERMIN;
1012 ifv->ifv_pflags = 0;
1015 * If the parent supports the VLAN_MTU capability,
1016 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1019 if (p->if_capenable & IFCAP_VLAN_MTU) {
1021 * No need to fudge the MTU since the parent can
1022 * handle extended frames.
1024 ifv->ifv_mtufudge = 0;
1027 * Fudge the MTU by the encapsulation size. This
1028 * makes us incompatible with strictly compliant
1029 * 802.1Q implementations, but allows us to use
1030 * the feature with other NetBSD implementations,
1031 * which might still be useful.
1033 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1036 ifv->ifv_trunk = trunk;
1038 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1039 ifp->if_baudrate = p->if_baudrate;
1041 * Copy only a selected subset of flags from the parent.
1042 * Other flags are none of our business.
1044 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1045 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1046 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1047 #undef VLAN_COPY_FLAGS
1049 ifp->if_link_state = p->if_link_state;
1051 vlan_capabilities(ifv);
1054 * Set up our ``Ethernet address'' to reflect the underlying
1055 * physical interface's.
1057 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
1060 * Configure multicast addresses that may already be
1061 * joined on the vlan device.
1063 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1065 /* We are ready for operation now. */
1066 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1068 TRUNK_UNLOCK(trunk);
1070 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag);
1077 vlan_unconfig(struct ifnet *ifp)
1082 ret = vlan_unconfig_locked(ifp);
1088 vlan_unconfig_locked(struct ifnet *ifp)
1090 struct ifvlantrunk *trunk;
1091 struct vlan_mc_entry *mc;
1093 struct ifnet *parent;
1098 ifv = ifp->if_softc;
1099 trunk = ifv->ifv_trunk;
1102 if (trunk != NULL) {
1103 struct sockaddr_dl sdl;
1106 parent = trunk->parent;
1109 * Since the interface is being unconfigured, we need to
1110 * empty the list of multicast groups that we may have joined
1111 * while we were alive from the parent's list.
1113 bzero((char *)&sdl, sizeof(sdl));
1114 sdl.sdl_len = sizeof(sdl);
1115 sdl.sdl_family = AF_LINK;
1116 sdl.sdl_index = parent->if_index;
1117 sdl.sdl_type = IFT_ETHER;
1118 sdl.sdl_alen = ETHER_ADDR_LEN;
1120 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1121 bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
1123 error = if_delmulti(parent, (struct sockaddr *)&sdl);
1126 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1130 vlan_setflags(ifp, 0); /* clear special flags on parent */
1132 trunk->vlans[ifv->ifv_tag] = NULL;
1135 vlan_remhash(trunk, ifv);
1137 ifv->ifv_trunk = NULL;
1140 * Check if we were the last.
1142 if (trunk->refcnt == 0) {
1143 trunk->parent->if_vlantrunk = NULL;
1145 * XXXGL: If some ithread has already entered
1146 * vlan_input() and is now blocked on the trunk
1147 * lock, then it should preempt us right after
1148 * unlock and finish its work. Then we will acquire
1149 * lock again in trunk_destroy().
1151 TRUNK_UNLOCK(trunk);
1152 trunk_destroy(trunk);
1154 TRUNK_UNLOCK(trunk);
1157 /* Disconnect from parent. */
1158 if (ifv->ifv_pflags)
1159 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1160 ifp->if_mtu = ETHERMTU;
1161 ifp->if_link_state = LINK_STATE_UNKNOWN;
1162 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1165 * Only dispatch an event if vlan was
1166 * attached, otherwise there is nothing
1167 * to cleanup anyway.
1170 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag);
1175 /* Handle a reference counted flag that should be set on the parent as well */
1177 vlan_setflag(struct ifnet *ifp, int flag, int status,
1178 int (*func)(struct ifnet *, int))
1183 /* XXX VLAN_LOCK_ASSERT(); */
1185 ifv = ifp->if_softc;
1186 status = status ? (ifp->if_flags & flag) : 0;
1187 /* Now "status" contains the flag value or 0 */
1190 * See if recorded parent's status is different from what
1191 * we want it to be. If it is, flip it. We record parent's
1192 * status in ifv_pflags so that we won't clear parent's flag
1193 * we haven't set. In fact, we don't clear or set parent's
1194 * flags directly, but get or release references to them.
1195 * That's why we can be sure that recorded flags still are
1196 * in accord with actual parent's flags.
1198 if (status != (ifv->ifv_pflags & flag)) {
1199 error = (*func)(PARENT(ifv), status);
1202 ifv->ifv_pflags &= ~flag;
1203 ifv->ifv_pflags |= status;
1209 * Handle IFF_* flags that require certain changes on the parent:
1210 * if "status" is true, update parent's flags respective to our if_flags;
1211 * if "status" is false, forcedly clear the flags set on parent.
1214 vlan_setflags(struct ifnet *ifp, int status)
1218 for (i = 0; vlan_pflags[i].flag; i++) {
1219 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1220 status, vlan_pflags[i].func);
1227 /* Inform all vlans that their parent has changed link state */
1229 vlan_link_state(struct ifnet *ifp, int link)
1231 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1237 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1238 if (trunk->vlans[i] != NULL) {
1239 ifv = trunk->vlans[i];
1241 for (i = 0; i < (1 << trunk->hwidth); i++)
1242 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
1244 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1245 if_link_state_change(ifv->ifv_ifp,
1246 trunk->parent->if_link_state);
1248 TRUNK_UNLOCK(trunk);
1252 vlan_capabilities(struct ifvlan *ifv)
1254 struct ifnet *p = PARENT(ifv);
1255 struct ifnet *ifp = ifv->ifv_ifp;
1257 TRUNK_LOCK_ASSERT(TRUNK(ifv));
1260 * If the parent interface can do checksum offloading
1261 * on VLANs, then propagate its hardware-assisted
1262 * checksumming flags. Also assert that checksum
1263 * offloading requires hardware VLAN tagging.
1265 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1266 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1268 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1269 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1270 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1271 ifp->if_hwassist = p->if_hwassist;
1273 ifp->if_capenable = 0;
1274 ifp->if_hwassist = 0;
1279 vlan_trunk_capabilities(struct ifnet *ifp)
1281 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1287 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1288 if (trunk->vlans[i] != NULL) {
1289 ifv = trunk->vlans[i];
1291 for (i = 0; i < (1 << trunk->hwidth); i++) {
1292 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1294 vlan_capabilities(ifv);
1296 TRUNK_UNLOCK(trunk);
1300 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1308 ifr = (struct ifreq *)data;
1309 ifv = ifp->if_softc;
1314 if (TRUNK(ifv) != NULL) {
1315 error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv),
1316 SIOCGIFMEDIA, data);
1318 /* Limit the result to the parent's current config. */
1320 struct ifmediareq *ifmr;
1322 ifmr = (struct ifmediareq *)data;
1323 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1324 ifmr->ifm_count = 1;
1325 error = copyout(&ifmr->ifm_current,
1342 * Set the interface MTU.
1345 if (TRUNK(ifv) != NULL) {
1347 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1349 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1352 ifp->if_mtu = ifr->ifr_mtu;
1359 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1362 if (vlr.vlr_parent[0] == '\0') {
1366 p = ifunit(vlr.vlr_parent);
1372 * Don't let the caller set up a VLAN tag with
1373 * anything except VLID bits.
1375 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1379 error = vlan_config(ifv, p, vlr.vlr_tag);
1383 /* Update flags on the parent, if necessary. */
1384 vlan_setflags(ifp, 1);
1388 bzero(&vlr, sizeof(vlr));
1390 if (TRUNK(ifv) != NULL) {
1391 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1392 sizeof(vlr.vlr_parent));
1393 vlr.vlr_tag = ifv->ifv_tag;
1396 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1401 * We should propagate selected flags to the parent,
1402 * e.g., promiscuous mode.
1404 if (TRUNK(ifv) != NULL)
1405 error = vlan_setflags(ifp, 1);
1411 * If we don't have a parent, just remember the membership for
1414 if (TRUNK(ifv) != NULL)
1415 error = vlan_setmulti(ifp);
1419 error = ether_ioctl(ifp, cmd, data);