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.
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_arp.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/if_vlan_var.h>
70 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
74 #define VLANNAME "vlan"
75 #define VLAN_DEF_HWIDTH 4
76 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
78 LIST_HEAD(ifvlanhead, ifvlan);
81 struct ifnet *parent; /* parent interface of this trunk */
84 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
85 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
87 struct ifvlanhead *hash; /* dynamic hash-list table */
92 LIST_ENTRY(ifvlantrunk) trunk_entry;
94 static LIST_HEAD(, ifvlantrunk) trunk_list;
96 struct vlan_mc_entry {
97 struct ether_addr mc_addr;
98 SLIST_ENTRY(vlan_mc_entry) mc_entries;
102 struct ifvlantrunk *ifv_trunk;
103 struct ifnet *ifv_ifp;
104 #define TRUNK(ifv) ((ifv)->ifv_trunk)
105 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
106 int ifv_pflags; /* special flags we have set on parent */
108 int ifvm_encaplen; /* encapsulation length */
109 int ifvm_mtufudge; /* MTU fudged by this much */
110 int ifvm_mintu; /* min transmission unit */
111 uint16_t ifvm_proto; /* encapsulation ethertype */
112 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
114 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
115 LIST_ENTRY(ifvlan) ifv_list;
117 #define ifv_proto ifv_mib.ifvm_proto
118 #define ifv_tag ifv_mib.ifvm_tag
119 #define ifv_encaplen ifv_mib.ifvm_encaplen
120 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
121 #define ifv_mintu ifv_mib.ifvm_mintu
123 /* Special flags we should propagate to parent. */
126 int (*func)(struct ifnet *, int);
128 {IFF_PROMISC, ifpromisc},
129 {IFF_ALLMULTI, if_allmulti},
133 SYSCTL_DECL(_net_link);
134 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
135 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
137 static int soft_pad = 0;
138 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
139 "pad short frames before tagging");
141 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
143 static eventhandler_tag ifdetach_tag;
146 * We have a global mutex, that is used to serialize configuration
147 * changes and isn't used in normal packet delivery.
149 * We also have a per-trunk rwlock, that is locked shared on packet
150 * processing and exclusive when configuration is changed.
152 * The VLAN_ARRAY substitutes the dynamic hash with a static array
153 * with 4096 entries. In theory this can give a boost in processing,
154 * however on practice it does not. Probably this is because array
155 * is too big to fit into CPU cache.
157 static struct mtx ifv_mtx;
158 #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
159 #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx)
160 #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED)
161 #define VLAN_LOCK() mtx_lock(&ifv_mtx)
162 #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx)
163 #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME)
164 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
165 #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw)
166 #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw)
167 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
168 #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw)
169 #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw)
170 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
173 static void vlan_inithash(struct ifvlantrunk *trunk);
174 static void vlan_freehash(struct ifvlantrunk *trunk);
175 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
176 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
177 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
178 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
181 static void trunk_destroy(struct ifvlantrunk *trunk);
183 static void vlan_start(struct ifnet *ifp);
184 static void vlan_init(void *foo);
185 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
186 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
187 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
188 int (*func)(struct ifnet *, int));
189 static int vlan_setflags(struct ifnet *ifp, int status);
190 static int vlan_setmulti(struct ifnet *ifp);
191 static int vlan_unconfig(struct ifnet *ifp);
192 static int vlan_unconfig_locked(struct ifnet *ifp);
193 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
194 static void vlan_link_state(struct ifnet *ifp, int link);
195 static void vlan_capabilities(struct ifvlan *ifv);
196 static void vlan_trunk_capabilities(struct ifnet *ifp);
198 static struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
199 const char *, int *);
200 static int vlan_clone_match(struct if_clone *, const char *);
201 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
202 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
204 static void vlan_ifdetach(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 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
213 vlan_inithash(struct ifvlantrunk *trunk)
218 * The trunk must not be locked here since we call malloc(M_WAITOK).
219 * It is OK in case this function is called before the trunk struct
220 * gets hooked up and becomes visible from other threads.
223 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
224 ("%s: hash already initialized", __func__));
226 trunk->hwidth = VLAN_DEF_HWIDTH;
227 n = 1 << trunk->hwidth;
228 trunk->hmask = n - 1;
229 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
230 for (i = 0; i < n; i++)
231 LIST_INIT(&trunk->hash[i]);
235 vlan_freehash(struct ifvlantrunk *trunk)
240 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
241 for (i = 0; i < (1 << trunk->hwidth); i++)
242 KASSERT(LIST_EMPTY(&trunk->hash[i]),
243 ("%s: hash table not empty", __func__));
245 free(trunk->hash, M_VLAN);
247 trunk->hwidth = trunk->hmask = 0;
251 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
256 TRUNK_LOCK_ASSERT(trunk);
257 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
259 b = 1 << trunk->hwidth;
260 i = HASH(ifv->ifv_tag, trunk->hmask);
261 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
262 if (ifv->ifv_tag == ifv2->ifv_tag)
266 * Grow the hash when the number of vlans exceeds half of the number of
267 * hash buckets squared. This will make the average linked-list length
270 if (trunk->refcnt > (b * b) / 2) {
271 vlan_growhash(trunk, 1);
272 i = HASH(ifv->ifv_tag, trunk->hmask);
274 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
281 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
286 TRUNK_LOCK_ASSERT(trunk);
287 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
289 b = 1 << trunk->hwidth;
290 i = HASH(ifv->ifv_tag, trunk->hmask);
291 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
294 LIST_REMOVE(ifv2, ifv_list);
295 if (trunk->refcnt < (b * b) / 2)
296 vlan_growhash(trunk, -1);
300 panic("%s: vlan not found\n", __func__);
301 return (ENOENT); /*NOTREACHED*/
305 * Grow the hash larger or smaller if memory permits.
308 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
312 struct ifvlanhead *hash2;
313 int hwidth2, i, j, n, n2;
315 TRUNK_LOCK_ASSERT(trunk);
316 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
319 /* Harmless yet obvious coding error */
320 printf("%s: howmuch is 0\n", __func__);
324 hwidth2 = trunk->hwidth + howmuch;
325 n = 1 << trunk->hwidth;
327 /* Do not shrink the table below the default */
328 if (hwidth2 < VLAN_DEF_HWIDTH)
331 /* M_NOWAIT because we're called with trunk mutex held */
332 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
334 printf("%s: out of memory -- hash size not changed\n",
336 return; /* We can live with the old hash table */
338 for (j = 0; j < n2; j++)
339 LIST_INIT(&hash2[j]);
340 for (i = 0; i < n; i++)
341 while (!LIST_EMPTY(&trunk->hash[i])) {
342 ifv = LIST_FIRST(&trunk->hash[i]);
343 LIST_REMOVE(ifv, ifv_list);
344 j = HASH(ifv->ifv_tag, n2 - 1);
345 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
347 free(trunk->hash, M_VLAN);
349 trunk->hwidth = hwidth2;
350 trunk->hmask = n2 - 1;
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;
394 LIST_REMOVE(trunk, trunk_entry);
396 TRUNK_LOCK_DESTROY(trunk);
401 * Program our multicast filter. What we're actually doing is
402 * programming the multicast filter of the parent. This has the
403 * side effect of causing the parent interface to receive multicast
404 * traffic that it doesn't really want, which ends up being discarded
405 * later by the upper protocol layers. Unfortunately, there's no way
406 * to avoid this: there really is only one physical interface.
408 * XXX: There is a possible race here if more than one thread is
409 * modifying the multicast state of the vlan interface at the same time.
412 vlan_setmulti(struct ifnet *ifp)
415 struct ifmultiaddr *ifma, *rifma = NULL;
417 struct vlan_mc_entry *mc = NULL;
418 struct sockaddr_dl sdl;
421 /*VLAN_LOCK_ASSERT();*/
423 /* Find the parent. */
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 (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
436 mc = SLIST_FIRST(&sc->vlan_mc_listhead);
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);
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)
535 LIST_INIT(&trunk_list);
537 vlan_input_p = vlan_input;
538 vlan_link_state_p = vlan_link_state;
539 vlan_trunk_cap_p = vlan_trunk_capabilities;
540 if_clone_attach(&vlan_cloner);
544 struct ifvlantrunk *trunk, *trunk1;
546 if_clone_detach(&vlan_cloner);
547 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
549 vlan_link_state_p = NULL;
550 vlan_trunk_cap_p = NULL;
552 LIST_FOREACH_SAFE(trunk, &trunk_list, trunk_entry, trunk1)
553 trunk_destroy(trunk);
564 static moduledata_t vlan_mod = {
570 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
571 MODULE_VERSION(if_vlan, 3);
572 MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
574 static struct ifnet *
575 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
581 /* Check for <etherif>.<vlan> style interface names. */
583 TAILQ_FOREACH(ifp, &ifnet, if_link) {
584 if (ifp->if_type != IFT_ETHER)
586 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
588 cp = name + strlen(ifp->if_xname);
591 for(; *cp != '\0'; cp++) {
592 if (*cp < '0' || *cp > '9')
594 t = (t * 10) + (*cp - '0');
606 vlan_clone_match(struct if_clone *ifc, const char *name)
610 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
613 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
615 for (cp = name + 4; *cp != '\0'; cp++) {
616 if (*cp < '0' || *cp > '9')
624 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
636 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
639 * There are 3 (ugh) ways to specify the cloned device:
640 * o pass a parameter block with the clone request.
641 * o specify parameters in the text of the clone device name
642 * o specify no parameters and get an unattached device that
643 * must be configured separately.
644 * The first technique is preferred; the latter two are
645 * supported for backwards compatibilty.
648 error = copyin(params, &vlr, sizeof(vlr));
651 p = ifunit(vlr.vlr_parent);
655 * Don't let the caller set up a VLAN tag with
656 * anything except VLID bits.
658 if (vlr.vlr_tag & ~EVL_VLID_MASK)
660 error = ifc_name2unit(name, &unit);
666 wildcard = (unit < 0);
667 } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
673 * Don't let the caller set up a VLAN tag with
674 * anything except VLID bits.
676 if (tag & ~EVL_VLID_MASK)
681 error = ifc_name2unit(name, &unit);
685 wildcard = (unit < 0);
688 error = ifc_alloc_unit(ifc, &unit);
692 /* In the wildcard case, we need to update the name. */
694 for (dp = name; *dp != '\0'; dp++);
695 if (snprintf(dp, len - (dp-name), "%d", unit) >
696 len - (dp-name) - 1) {
697 panic("%s: interface name too long", __func__);
701 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
702 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
704 ifc_free_unit(ifc, unit);
708 SLIST_INIT(&ifv->vlan_mc_listhead);
712 * Set the name manually rather than using if_initname because
713 * we don't conform to the default naming convention for interfaces.
715 strlcpy(ifp->if_xname, name, IFNAMSIZ);
716 ifp->if_dname = ifc->ifc_name;
717 ifp->if_dunit = unit;
718 /* NB: flags are not set here */
719 ifp->if_linkmib = &ifv->ifv_mib;
720 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
721 /* NB: mtu is not set here */
723 ifp->if_init = vlan_init;
724 ifp->if_start = vlan_start;
725 ifp->if_ioctl = vlan_ioctl;
726 ifp->if_snd.ifq_maxlen = ifqmaxlen;
727 ifp->if_flags = VLAN_IFFLAGS;
728 ether_ifattach(ifp, eaddr);
729 /* Now undo some of the damage... */
730 ifp->if_baudrate = 0;
731 ifp->if_type = IFT_L2VLAN;
732 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
735 error = vlan_config(ifv, p, tag);
738 * Since we've partialy failed, we need to back
739 * out all the way, otherwise userland could get
740 * confused. Thus, we destroy the interface.
744 if_free_type(ifp, IFT_ETHER);
750 /* Update flags on the parent, if necessary. */
751 vlan_setflags(ifp, 1);
758 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
760 struct ifvlan *ifv = ifp->if_softc;
761 int unit = ifp->if_dunit;
763 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
764 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
765 if_free_type(ifp, IFT_ETHER);
767 ifc_free_unit(ifc, unit);
773 * The ifp->if_init entry point for vlan(4) is a no-op.
776 vlan_init(void *foo __unused)
781 * The if_start method for vlan(4) interface. It doesn't
782 * raises the IFF_DRV_OACTIVE flag, since it is called
783 * only from IFQ_HANDOFF() macro in ether_output_frame().
784 * If the interface queue is full, and vlan_start() is
785 * not called, the queue would never get emptied and
786 * interface would stall forever.
789 vlan_start(struct ifnet *ifp)
800 IF_DEQUEUE(&ifp->if_snd, m);
806 * Do not run parent's if_start() if the parent is not up,
807 * or parent's driver will cause a system crash.
809 if (!((p->if_flags & IFF_UP) &&
810 (p->if_drv_flags & IFF_DRV_RUNNING))) {
812 ifp->if_collisions++;
817 * Pad the frame to the minimum size allowed if told to.
818 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
819 * paragraph C.4.4.3.b. It can help to work around buggy
820 * bridges that violate paragraph C.4.4.3.a from the same
821 * document, i.e., fail to pad short frames after untagging.
822 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
823 * untagging it will produce a 62-byte frame, which is a runt
824 * and requires padding. There are VLAN-enabled network
825 * devices that just discard such runts instead or mishandle
829 static char pad[8]; /* just zeros */
832 for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
833 n > 0; n -= sizeof(pad))
834 if (!m_append(m, min(n, sizeof(pad)), pad))
838 if_printf(ifp, "cannot pad short frame\n");
846 * If underlying interface can do VLAN tag insertion itself,
847 * just pass the packet along. However, we need some way to
848 * tell the interface where the packet came from so that it
849 * knows how to find the VLAN tag to use, so we attach a
850 * packet tag that holds it.
852 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
853 struct m_tag *mtag = (struct m_tag *)
854 uma_zalloc(zone_mtag_vlan, M_NOWAIT);
860 VLAN_TAG_VALUE(mtag) = ifv->ifv_tag;
861 m_tag_prepend(m, mtag);
862 m->m_flags |= M_VLANTAG;
864 struct ether_vlan_header *evl;
866 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
869 "unable to prepend VLAN header\n");
873 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
875 if (m->m_len < sizeof(*evl)) {
876 m = m_pullup(m, sizeof(*evl));
879 "cannot pullup VLAN header\n");
886 * Transform the Ethernet header into an Ethernet header
887 * with 802.1Q encapsulation.
889 bcopy(mtod(m, char *) + ifv->ifv_encaplen,
890 mtod(m, char *), ETHER_HDR_LEN);
891 evl = mtod(m, struct ether_vlan_header *);
892 evl->evl_proto = evl->evl_encap_proto;
893 evl->evl_encap_proto = htons(ifv->ifv_proto);
894 evl->evl_tag = htons(ifv->ifv_tag);
896 printf("%s: %*D\n", __func__, (int)sizeof(*evl),
897 (unsigned char *)evl, ":");
902 * Send it, precisely as ether_output() would have.
903 * We are already running at splimp.
905 IFQ_HANDOFF(p, m, error);
914 vlan_input(struct ifnet *ifp, struct mbuf *m)
916 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
921 KASSERT(trunk != NULL, ("%s: no trunk", __func__));
923 if (m->m_flags & M_VLANTAG) {
925 * Packet is tagged, but m contains a normal
926 * Ethernet frame; the tag is stored out-of-band.
928 mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
929 KASSERT(mtag != NULL,
930 ("%s: M_VLANTAG without m_tag", __func__));
931 tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag));
932 m_tag_delete(m, mtag);
933 m->m_flags &= ~M_VLANTAG;
935 struct ether_vlan_header *evl;
938 * Packet is tagged in-band as specified by 802.1q.
941 switch (ifp->if_type) {
943 if (m->m_len < sizeof(*evl) &&
944 (m = m_pullup(m, sizeof(*evl))) == NULL) {
945 if_printf(ifp, "cannot pullup VLAN header\n");
948 evl = mtod(m, struct ether_vlan_header *);
949 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
952 * Restore the original ethertype. We'll remove
953 * the encapsulation after we've found the vlan
954 * interface corresponding to the tag.
956 evl->evl_encap_proto = evl->evl_proto;
960 panic("%s: %s has unsupported if_type %u",
961 __func__, ifp->if_xname, ifp->if_type);
971 ifv = trunk->vlans[tag];
973 ifv = vlan_gethash(trunk, tag);
975 if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
976 TRUNK_RUNLOCK(trunk);
981 TRUNK_RUNLOCK(trunk);
985 * Packet had an in-line encapsulation header;
986 * remove it. The original header has already
987 * been fixed up above.
989 bcopy(mtod(m, caddr_t),
990 mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
992 m_adj(m, ETHER_VLAN_ENCAP_LEN);
995 m->m_pkthdr.rcvif = ifv->ifv_ifp;
996 ifv->ifv_ifp->if_ipackets++;
998 /* Pass it back through the parent's input routine. */
999 (*ifp->if_input)(ifv->ifv_ifp, m);
1003 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
1005 struct ifvlantrunk *trunk;
1009 /* VID numbers 0x0 and 0xFFF are reserved */
1010 if (tag == 0 || tag == 0xFFF)
1012 if (p->if_type != IFT_ETHER)
1013 return (EPROTONOSUPPORT);
1014 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1015 return (EPROTONOSUPPORT);
1019 if (p->if_vlantrunk == NULL) {
1020 trunk = malloc(sizeof(struct ifvlantrunk),
1021 M_VLAN, M_WAITOK | M_ZERO);
1023 vlan_inithash(trunk);
1026 if (p->if_vlantrunk != NULL) {
1027 /* A race that that is very unlikely to be hit. */
1029 vlan_freehash(trunk);
1031 free(trunk, M_VLAN);
1034 TRUNK_LOCK_INIT(trunk);
1035 LIST_INSERT_HEAD(&trunk_list, trunk, trunk_entry);
1037 p->if_vlantrunk = trunk;
1042 trunk = p->if_vlantrunk;
1046 ifv->ifv_tag = tag; /* must set this before vlan_inshash() */
1048 if (trunk->vlans[tag] != NULL) {
1052 trunk->vlans[tag] = ifv;
1055 error = vlan_inshash(trunk, ifv);
1059 ifv->ifv_proto = ETHERTYPE_VLAN;
1060 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1061 ifv->ifv_mintu = ETHERMIN;
1062 ifv->ifv_pflags = 0;
1065 * If the parent supports the VLAN_MTU capability,
1066 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1069 if (p->if_capenable & IFCAP_VLAN_MTU) {
1071 * No need to fudge the MTU since the parent can
1072 * handle extended frames.
1074 ifv->ifv_mtufudge = 0;
1077 * Fudge the MTU by the encapsulation size. This
1078 * makes us incompatible with strictly compliant
1079 * 802.1Q implementations, but allows us to use
1080 * the feature with other NetBSD implementations,
1081 * which might still be useful.
1083 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1086 ifv->ifv_trunk = trunk;
1088 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1089 ifp->if_baudrate = p->if_baudrate;
1091 * Copy only a selected subset of flags from the parent.
1092 * Other flags are none of our business.
1094 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1095 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1096 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1097 #undef VLAN_COPY_FLAGS
1099 ifp->if_link_state = p->if_link_state;
1101 vlan_capabilities(ifv);
1104 * Set up our ``Ethernet address'' to reflect the underlying
1105 * physical interface's.
1107 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
1110 * Configure multicast addresses that may already be
1111 * joined on the vlan device.
1113 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1115 /* We are ready for operation now. */
1116 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1118 TRUNK_UNLOCK(trunk);
1125 vlan_unconfig(struct ifnet *ifp)
1130 ret = vlan_unconfig_locked(ifp);
1136 vlan_unconfig_locked(struct ifnet *ifp)
1138 struct ifvlantrunk *trunk;
1139 struct vlan_mc_entry *mc;
1145 ifv = ifp->if_softc;
1146 trunk = ifv->ifv_trunk;
1149 struct sockaddr_dl sdl;
1150 struct ifnet *p = trunk->parent;
1155 * Since the interface is being unconfigured, we need to
1156 * empty the list of multicast groups that we may have joined
1157 * while we were alive from the parent's list.
1159 bzero((char *)&sdl, sizeof(sdl));
1160 sdl.sdl_len = sizeof(sdl);
1161 sdl.sdl_family = AF_LINK;
1162 sdl.sdl_index = p->if_index;
1163 sdl.sdl_type = IFT_ETHER;
1164 sdl.sdl_alen = ETHER_ADDR_LEN;
1166 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
1167 mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
1168 bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
1170 error = if_delmulti(p, (struct sockaddr *)&sdl);
1173 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1177 vlan_setflags(ifp, 0); /* clear special flags on parent */
1179 trunk->vlans[ifv->ifv_tag] = NULL;
1182 vlan_remhash(trunk, ifv);
1184 ifv->ifv_trunk = NULL;
1187 * Check if we were the last.
1189 if (trunk->refcnt == 0) {
1190 trunk->parent->if_vlantrunk = NULL;
1192 * XXXGL: If some ithread has already entered
1193 * vlan_input() and is now blocked on the trunk
1194 * lock, then it should preempt us right after
1195 * unlock and finish its work. Then we will acquire
1196 * lock again in trunk_destroy().
1198 TRUNK_UNLOCK(trunk);
1199 trunk_destroy(trunk);
1201 TRUNK_UNLOCK(trunk);
1204 /* Disconnect from parent. */
1205 if (ifv->ifv_pflags)
1206 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1207 ifp->if_mtu = ETHERMTU;
1208 ifp->if_link_state = LINK_STATE_UNKNOWN;
1209 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1214 /* Handle a reference counted flag that should be set on the parent as well */
1216 vlan_setflag(struct ifnet *ifp, int flag, int status,
1217 int (*func)(struct ifnet *, int))
1222 /* XXX VLAN_LOCK_ASSERT(); */
1224 ifv = ifp->if_softc;
1225 status = status ? (ifp->if_flags & flag) : 0;
1226 /* Now "status" contains the flag value or 0 */
1229 * See if recorded parent's status is different from what
1230 * we want it to be. If it is, flip it. We record parent's
1231 * status in ifv_pflags so that we won't clear parent's flag
1232 * we haven't set. In fact, we don't clear or set parent's
1233 * flags directly, but get or release references to them.
1234 * That's why we can be sure that recorded flags still are
1235 * in accord with actual parent's flags.
1237 if (status != (ifv->ifv_pflags & flag)) {
1238 error = (*func)(PARENT(ifv), status);
1241 ifv->ifv_pflags &= ~flag;
1242 ifv->ifv_pflags |= status;
1248 * Handle IFF_* flags that require certain changes on the parent:
1249 * if "status" is true, update parent's flags respective to our if_flags;
1250 * if "status" is false, forcedly clear the flags set on parent.
1253 vlan_setflags(struct ifnet *ifp, int status)
1257 for (i = 0; vlan_pflags[i].flag; i++) {
1258 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1259 status, vlan_pflags[i].func);
1266 /* Inform all vlans that their parent has changed link state */
1268 vlan_link_state(struct ifnet *ifp, int link)
1270 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1276 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1277 if (trunk->vlans[i] != NULL) {
1278 ifv = trunk->vlans[i];
1280 for (i = 0; i < (1 << trunk->hwidth); i++) {
1281 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1283 if_link_state_change(ifv->ifv_ifp,
1284 trunk->parent->if_link_state);
1286 TRUNK_UNLOCK(trunk);
1290 vlan_capabilities(struct ifvlan *ifv)
1292 struct ifnet *p = PARENT(ifv);
1293 struct ifnet *ifp = ifv->ifv_ifp;
1295 TRUNK_LOCK_ASSERT(TRUNK(ifv));
1298 * If the parent interface can do checksum offloading
1299 * on VLANs, then propagate its hardware-assisted
1300 * checksumming flags. Also assert that checksum
1301 * offloading requires hardware VLAN tagging.
1303 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1304 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1306 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1307 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1308 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1309 ifp->if_hwassist = p->if_hwassist;
1311 ifp->if_capenable = 0;
1312 ifp->if_hwassist = 0;
1317 vlan_trunk_capabilities(struct ifnet *ifp)
1319 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1325 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1326 if (trunk->vlans[i] != NULL) {
1327 ifv = trunk->vlans[i];
1329 for (i = 0; i < (1 << trunk->hwidth); i++) {
1330 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1332 vlan_capabilities(ifv);
1334 TRUNK_UNLOCK(trunk);
1338 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1347 ifr = (struct ifreq *)data;
1348 ifa = (struct ifaddr *)data;
1349 ifv = ifp->if_softc;
1353 ifp->if_flags |= IFF_UP;
1355 switch (ifa->ifa_addr->sa_family) {
1358 arp_ifinit(ifv->ifv_ifp, ifa);
1368 struct sockaddr *sa;
1370 sa = (struct sockaddr *) &ifr->ifr_data;
1371 bcopy(IF_LLADDR(ifp), (caddr_t)sa->sa_data,
1378 if (TRUNK(ifv) != NULL) {
1379 error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv),
1380 SIOCGIFMEDIA, data);
1382 /* Limit the result to the parent's current config. */
1384 struct ifmediareq *ifmr;
1386 ifmr = (struct ifmediareq *)data;
1387 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1388 ifmr->ifm_count = 1;
1389 error = copyout(&ifmr->ifm_current,
1406 * Set the interface MTU.
1409 if (TRUNK(ifv) != NULL) {
1411 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1413 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1416 ifp->if_mtu = ifr->ifr_mtu;
1423 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1426 if (vlr.vlr_parent[0] == '\0') {
1430 p = ifunit(vlr.vlr_parent);
1436 * Don't let the caller set up a VLAN tag with
1437 * anything except VLID bits.
1439 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1443 error = vlan_config(ifv, p, vlr.vlr_tag);
1447 /* Update flags on the parent, if necessary. */
1448 vlan_setflags(ifp, 1);
1452 bzero(&vlr, sizeof(vlr));
1454 if (TRUNK(ifv) != NULL) {
1455 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1456 sizeof(vlr.vlr_parent));
1457 vlr.vlr_tag = ifv->ifv_tag;
1460 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1465 * We should propagate selected flags to the parent,
1466 * e.g., promiscuous mode.
1468 if (TRUNK(ifv) != NULL)
1469 error = vlan_setflags(ifp, 1);
1475 * If we don't have a parent, just remember the membership for
1478 if (TRUNK(ifv) != NULL)
1479 error = vlan_setmulti(ifp);