2 * Copyright 1998 Massachusetts Institute of Technology
3 * Copyright 2012 ADARA Networks, Inc.
4 * Copyright 2017 Dell EMC Isilon
6 * Portions of this software were developed by Robert N. M. Watson under
7 * contract to ADARA Networks, Inc.
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby
11 * granted, provided that both the above copyright notice and this
12 * permission notice appear in all copies, that both the above
13 * copyright notice and this permission notice appear in all
14 * supporting documentation, and that the name of M.I.T. not be used
15 * in advertising or publicity pertaining to distribution of the
16 * software without specific, written prior permission. M.I.T. makes
17 * no representations about the suitability of this software for any
18 * purpose. It is provided "as is" without express or implied
21 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
22 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
25 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
37 * This is sort of sneaky in the implementation, since
38 * we need to pretend to be enough of an Ethernet implementation
39 * to make arp work. The way we do this is by telling everyone
40 * that we are an Ethernet, and then catch the packets that
41 * ether_output() sends to us via if_transmit(), rewrite them for
42 * use by the real outgoing interface, and ask it to send them.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
50 #include "opt_ratelimit.h"
52 #include <sys/param.h>
53 #include <sys/eventhandler.h>
54 #include <sys/kernel.h>
56 #include <sys/malloc.h>
58 #include <sys/module.h>
59 #include <sys/rmlock.h>
61 #include <sys/queue.h>
62 #include <sys/socket.h>
63 #include <sys/sockio.h>
64 #include <sys/sysctl.h>
65 #include <sys/systm.h>
67 #include <sys/taskqueue.h>
70 #include <net/ethernet.h>
72 #include <net/if_var.h>
73 #include <net/if_clone.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <net/if_vlan_var.h>
80 #include <netinet/in.h>
81 #include <netinet/if_ether.h>
84 #define VLAN_DEF_HWIDTH 4
85 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
87 #define UP_AND_RUNNING(ifp) \
88 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
90 LIST_HEAD(ifvlanhead, ifvlan);
93 struct ifnet *parent; /* parent interface of this trunk */
96 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
97 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
99 struct ifvlanhead *hash; /* dynamic hash-list table */
107 * This macro provides a facility to iterate over every vlan on a trunk with
108 * the assumption that none will be added/removed during iteration.
111 #define VLAN_FOREACH(_ifv, _trunk) \
113 for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
114 if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
115 #else /* VLAN_ARRAY */
116 #define VLAN_FOREACH(_ifv, _trunk) \
117 struct ifvlan *_next; \
119 for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
120 LIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
121 #endif /* VLAN_ARRAY */
124 * This macro provides a facility to iterate over every vlan on a trunk while
125 * also modifying the number of vlans on the trunk. The iteration continues
126 * until some condition is met or there are no more vlans on the trunk.
129 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
130 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
132 for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
133 if (((_ifv) = (_trunk)->vlans[_i]))
134 #else /* VLAN_ARRAY */
136 * The hash table case is more complicated. We allow for the hash table to be
137 * modified (i.e. vlans removed) while we are iterating over it. To allow for
138 * this we must restart the iteration every time we "touch" something during
139 * the iteration, since removal will resize the hash table and invalidate our
140 * current position. If acting on the touched element causes the trunk to be
141 * emptied, then iteration also stops.
143 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
145 bool _touch = false; \
147 !(_cond) && _i < (1 << (_trunk)->hwidth); \
148 _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
149 if (((_ifv) = LIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
151 #endif /* VLAN_ARRAY */
153 struct vlan_mc_entry {
154 struct sockaddr_dl mc_addr;
155 SLIST_ENTRY(vlan_mc_entry) mc_entries;
159 struct ifvlantrunk *ifv_trunk;
160 struct ifnet *ifv_ifp;
161 #define TRUNK(ifv) ((ifv)->ifv_trunk)
162 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
164 int ifv_pflags; /* special flags we have set on parent */
167 int ifvm_encaplen; /* encapsulation length */
168 int ifvm_mtufudge; /* MTU fudged by this much */
169 int ifvm_mintu; /* min transmission unit */
170 uint16_t ifvm_proto; /* encapsulation ethertype */
171 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
172 uint16_t ifvm_vid; /* VLAN ID */
173 uint8_t ifvm_pcp; /* Priority Code Point (PCP). */
175 struct task lladdr_task;
176 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
178 LIST_ENTRY(ifvlan) ifv_list;
181 #define ifv_proto ifv_mib.ifvm_proto
182 #define ifv_tag ifv_mib.ifvm_tag
183 #define ifv_vid ifv_mib.ifvm_vid
184 #define ifv_pcp ifv_mib.ifvm_pcp
185 #define ifv_encaplen ifv_mib.ifvm_encaplen
186 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
187 #define ifv_mintu ifv_mib.ifvm_mintu
189 /* Special flags we should propagate to parent. */
192 int (*func)(struct ifnet *, int);
194 {IFF_PROMISC, ifpromisc},
195 {IFF_ALLMULTI, if_allmulti},
199 extern int vlan_mtag_pcp;
201 static const char vlanname[] = "vlan";
202 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
204 static eventhandler_tag ifdetach_tag;
205 static eventhandler_tag iflladdr_tag;
208 * if_vlan uses two module-level locks to allow concurrent modification of vlan
209 * interfaces and (mostly) allow for vlans to be destroyed while they are being
210 * used for tx/rx. To accomplish this in a way that has acceptable performance
211 * and cooperation with other parts of the network stack there is a
212 * non-sleepable rmlock(9) and an sx(9). Both locks are exclusively acquired
213 * when destroying a vlan interface, i.e. when the if_vlantrunk field of struct
214 * ifnet is de-allocated and NULL'd. Thus a reader holding either lock has a
215 * guarantee that the struct ifvlantrunk references a valid vlan trunk.
217 * The performance-sensitive paths that warrant using the rmlock(9) are
218 * vlan_transmit and vlan_input. Both have to check for the vlan interface's
219 * existence using if_vlantrunk, and being in the network tx/rx paths the use
220 * of an rmlock(9) gives a measureable improvement in performance.
222 * The reason for having an sx(9) is mostly because there are still areas that
223 * must be sleepable and also have safe concurrent access to a vlan interface.
224 * Since the sx(9) exists, it is used by default in most paths unless sleeping
225 * is not permitted, or if it is not clear whether sleeping is permitted.
227 * Note that despite these protections, there is still an inherent race in the
228 * destruction of vlans since there's no guarantee that the ifnet hasn't been
229 * freed/reused when the tx/rx functions are called by the stack. This can only
230 * be fixed by addressing ifnet's lifetime issues.
232 #define _VLAN_RM_ID ifv_rm_lock
233 #define _VLAN_SX_ID ifv_sx
235 static struct rmlock _VLAN_RM_ID;
236 static struct sx _VLAN_SX_ID;
238 #define VLAN_LOCKING_INIT() \
239 rm_init(&_VLAN_RM_ID, "vlan_rm"); \
240 sx_init(&_VLAN_SX_ID, "vlan_sx")
242 #define VLAN_LOCKING_DESTROY() \
243 rm_destroy(&_VLAN_RM_ID); \
244 sx_destroy(&_VLAN_SX_ID)
246 #define _VLAN_RM_TRACKER _vlan_rm_tracker
247 #define VLAN_RLOCK() rm_rlock(&_VLAN_RM_ID, \
249 #define VLAN_RUNLOCK() rm_runlock(&_VLAN_RM_ID, \
251 #define VLAN_WLOCK() rm_wlock(&_VLAN_RM_ID)
252 #define VLAN_WUNLOCK() rm_wunlock(&_VLAN_RM_ID)
253 #define VLAN_RLOCK_ASSERT() rm_assert(&_VLAN_RM_ID, RA_RLOCKED)
254 #define VLAN_WLOCK_ASSERT() rm_assert(&_VLAN_RM_ID, RA_WLOCKED)
255 #define VLAN_RWLOCK_ASSERT() rm_assert(&_VLAN_RM_ID, RA_LOCKED)
256 #define VLAN_LOCK_READER struct rm_priotracker _VLAN_RM_TRACKER
258 #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID)
259 #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID)
260 #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID)
261 #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID)
262 #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
263 #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
264 #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED)
268 * We also have a per-trunk rmlock(9), that is locked shared on packet
269 * processing and exclusive when configuration is changed. Note: This should
270 * only be acquired while there is a shared lock on either of the global locks
271 * via VLAN_SLOCK or VLAN_RLOCK. Thus, an exclusive lock on the global locks
272 * makes a call to TRUNK_RLOCK/TRUNK_WLOCK technically superfluous.
274 #define _TRUNK_RM_TRACKER _trunk_rm_tracker
275 #define TRUNK_LOCK_INIT(trunk) rm_init(&(trunk)->lock, vlanname)
276 #define TRUNK_LOCK_DESTROY(trunk) rm_destroy(&(trunk)->lock)
277 #define TRUNK_RLOCK(trunk) rm_rlock(&(trunk)->lock, \
279 #define TRUNK_WLOCK(trunk) rm_wlock(&(trunk)->lock)
280 #define TRUNK_RUNLOCK(trunk) rm_runlock(&(trunk)->lock, \
282 #define TRUNK_WUNLOCK(trunk) rm_wunlock(&(trunk)->lock)
283 #define TRUNK_RLOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_RLOCKED)
284 #define TRUNK_LOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_LOCKED)
285 #define TRUNK_WLOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_WLOCKED)
286 #define TRUNK_LOCK_READER struct rm_priotracker _TRUNK_RM_TRACKER
289 * The VLAN_ARRAY substitutes the dynamic hash with a static array
290 * with 4096 entries. In theory this can give a boost in processing,
291 * however in practice it does not. Probably this is because the array
292 * is too big to fit into CPU cache.
295 static void vlan_inithash(struct ifvlantrunk *trunk);
296 static void vlan_freehash(struct ifvlantrunk *trunk);
297 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
298 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
299 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
300 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
303 static void trunk_destroy(struct ifvlantrunk *trunk);
305 static void vlan_init(void *foo);
306 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
307 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
309 static int vlan_snd_tag_alloc(struct ifnet *,
310 union if_snd_tag_alloc_params *, struct m_snd_tag **);
312 static void vlan_qflush(struct ifnet *ifp);
313 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
314 int (*func)(struct ifnet *, int));
315 static int vlan_setflags(struct ifnet *ifp, int status);
316 static int vlan_setmulti(struct ifnet *ifp);
317 static int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
318 static void vlan_unconfig(struct ifnet *ifp);
319 static void vlan_unconfig_locked(struct ifnet *ifp, int departing);
320 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
321 static void vlan_link_state(struct ifnet *ifp);
322 static void vlan_capabilities(struct ifvlan *ifv);
323 static void vlan_trunk_capabilities(struct ifnet *ifp);
325 static struct ifnet *vlan_clone_match_ethervid(const char *, int *);
326 static int vlan_clone_match(struct if_clone *, const char *);
327 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
328 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
330 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
331 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
333 static void vlan_lladdr_fn(void *arg, int pending);
335 static struct if_clone *vlan_cloner;
338 VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
339 #define V_vlan_cloner VNET(vlan_cloner)
343 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
346 vlan_inithash(struct ifvlantrunk *trunk)
351 * The trunk must not be locked here since we call malloc(M_WAITOK).
352 * It is OK in case this function is called before the trunk struct
353 * gets hooked up and becomes visible from other threads.
356 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
357 ("%s: hash already initialized", __func__));
359 trunk->hwidth = VLAN_DEF_HWIDTH;
360 n = 1 << trunk->hwidth;
361 trunk->hmask = n - 1;
362 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
363 for (i = 0; i < n; i++)
364 LIST_INIT(&trunk->hash[i]);
368 vlan_freehash(struct ifvlantrunk *trunk)
373 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
374 for (i = 0; i < (1 << trunk->hwidth); i++)
375 KASSERT(LIST_EMPTY(&trunk->hash[i]),
376 ("%s: hash table not empty", __func__));
378 free(trunk->hash, M_VLAN);
380 trunk->hwidth = trunk->hmask = 0;
384 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
389 TRUNK_WLOCK_ASSERT(trunk);
390 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
392 b = 1 << trunk->hwidth;
393 i = HASH(ifv->ifv_vid, trunk->hmask);
394 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
395 if (ifv->ifv_vid == ifv2->ifv_vid)
399 * Grow the hash when the number of vlans exceeds half of the number of
400 * hash buckets squared. This will make the average linked-list length
403 if (trunk->refcnt > (b * b) / 2) {
404 vlan_growhash(trunk, 1);
405 i = HASH(ifv->ifv_vid, trunk->hmask);
407 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
414 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
419 TRUNK_WLOCK_ASSERT(trunk);
420 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
422 b = 1 << trunk->hwidth;
423 i = HASH(ifv->ifv_vid, trunk->hmask);
424 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
427 LIST_REMOVE(ifv2, ifv_list);
428 if (trunk->refcnt < (b * b) / 2)
429 vlan_growhash(trunk, -1);
433 panic("%s: vlan not found\n", __func__);
434 return (ENOENT); /*NOTREACHED*/
438 * Grow the hash larger or smaller if memory permits.
441 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
444 struct ifvlanhead *hash2;
445 int hwidth2, i, j, n, n2;
447 TRUNK_WLOCK_ASSERT(trunk);
448 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
451 /* Harmless yet obvious coding error */
452 printf("%s: howmuch is 0\n", __func__);
456 hwidth2 = trunk->hwidth + howmuch;
457 n = 1 << trunk->hwidth;
459 /* Do not shrink the table below the default */
460 if (hwidth2 < VLAN_DEF_HWIDTH)
463 /* M_NOWAIT because we're called with trunk mutex held */
464 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
466 printf("%s: out of memory -- hash size not changed\n",
468 return; /* We can live with the old hash table */
470 for (j = 0; j < n2; j++)
471 LIST_INIT(&hash2[j]);
472 for (i = 0; i < n; i++)
473 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
474 LIST_REMOVE(ifv, ifv_list);
475 j = HASH(ifv->ifv_vid, n2 - 1);
476 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
478 free(trunk->hash, M_VLAN);
480 trunk->hwidth = hwidth2;
481 trunk->hmask = n2 - 1;
484 if_printf(trunk->parent,
485 "VLAN hash table resized from %d to %d buckets\n", n, n2);
488 static __inline struct ifvlan *
489 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
493 TRUNK_RLOCK_ASSERT(trunk);
495 LIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
496 if (ifv->ifv_vid == vid)
502 /* Debugging code to view the hashtables. */
504 vlan_dumphash(struct ifvlantrunk *trunk)
509 for (i = 0; i < (1 << trunk->hwidth); i++) {
511 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
512 printf("%s ", ifv->ifv_ifp->if_xname);
519 static __inline struct ifvlan *
520 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
523 return trunk->vlans[vid];
527 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
530 if (trunk->vlans[ifv->ifv_vid] != NULL)
532 trunk->vlans[ifv->ifv_vid] = ifv;
539 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
542 trunk->vlans[ifv->ifv_vid] = NULL;
549 vlan_freehash(struct ifvlantrunk *trunk)
554 vlan_inithash(struct ifvlantrunk *trunk)
558 #endif /* !VLAN_ARRAY */
561 trunk_destroy(struct ifvlantrunk *trunk)
566 vlan_freehash(trunk);
567 trunk->parent->if_vlantrunk = NULL;
568 TRUNK_LOCK_DESTROY(trunk);
569 if_rele(trunk->parent);
574 * Program our multicast filter. What we're actually doing is
575 * programming the multicast filter of the parent. This has the
576 * side effect of causing the parent interface to receive multicast
577 * traffic that it doesn't really want, which ends up being discarded
578 * later by the upper protocol layers. Unfortunately, there's no way
579 * to avoid this: there really is only one physical interface.
582 vlan_setmulti(struct ifnet *ifp)
585 struct ifmultiaddr *ifma;
587 struct vlan_mc_entry *mc;
591 * XXX This stupidly needs the rmlock to avoid sleeping while holding
592 * the in6_multi_mtx (see in6_mc_join_locked).
594 VLAN_RWLOCK_ASSERT();
596 /* Find the parent. */
598 TRUNK_WLOCK_ASSERT(TRUNK(sc));
601 CURVNET_SET_QUIET(ifp_p->if_vnet);
603 /* First, remove any existing filter entries. */
604 while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
605 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
606 (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
610 /* Now program new ones. */
612 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
613 if (ifma->ifma_addr->sa_family != AF_LINK)
615 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
617 IF_ADDR_WUNLOCK(ifp);
620 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
621 mc->mc_addr.sdl_index = ifp_p->if_index;
622 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
624 IF_ADDR_WUNLOCK(ifp);
625 SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
626 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
637 * A handler for parent interface link layer address changes.
638 * If the parent interface link layer address is changed we
639 * should also change it on all children vlans.
642 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
645 struct ifnet *ifv_ifp;
646 struct ifvlantrunk *trunk;
647 struct sockaddr_dl *sdl;
650 /* Need the rmlock since this is run on taskqueue_swi. */
652 trunk = ifp->if_vlantrunk;
659 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
660 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
661 * ioctl calls on the parent garbling the lladdr of the child vlan.
664 VLAN_FOREACH(ifv, trunk) {
666 * Copy new new lladdr into the ifv_ifp, enqueue a task
667 * to actually call if_setlladdr. if_setlladdr needs to
668 * be deferred to a taskqueue because it will call into
669 * the if_vlan ioctl path and try to acquire the global
672 ifv_ifp = ifv->ifv_ifp;
673 bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
675 sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
676 sdl->sdl_alen = ifp->if_addrlen;
677 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
679 TRUNK_WUNLOCK(trunk);
684 * A handler for network interface departure events.
685 * Track departure of trunks here so that we don't access invalid
686 * pointers or whatever if a trunk is ripped from under us, e.g.,
687 * by ejecting its hot-plug card. However, if an ifnet is simply
688 * being renamed, then there's no need to tear down the state.
691 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
694 struct ifvlantrunk *trunk;
696 /* If the ifnet is just being renamed, don't do anything. */
697 if (ifp->if_flags & IFF_RENAMING)
700 trunk = ifp->if_vlantrunk;
707 * OK, it's a trunk. Loop over and detach all vlan's on it.
708 * Check trunk pointer after each vlan_unconfig() as it will
709 * free it and set to NULL after the last vlan was detached.
711 VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
712 ifp->if_vlantrunk == NULL)
713 vlan_unconfig_locked(ifv->ifv_ifp, 1);
715 /* Trunk should have been destroyed in vlan_unconfig(). */
716 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
721 * Return the trunk device for a virtual interface.
723 static struct ifnet *
724 vlan_trunkdev(struct ifnet *ifp)
729 if (ifp->if_type != IFT_L2VLAN)
732 /* Not clear if callers are sleepable, so acquire the rmlock. */
743 * Return the 12-bit VLAN VID for this interface, for use by external
744 * components such as Infiniband.
746 * XXXRW: Note that the function name here is historical; it should be named
750 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
754 if (ifp->if_type != IFT_L2VLAN)
757 *vidp = ifv->ifv_vid;
762 * Return a driver specific cookie for this interface. Synchronization
763 * with setcookie must be provided by the driver.
766 vlan_cookie(struct ifnet *ifp)
770 if (ifp->if_type != IFT_L2VLAN)
773 return (ifv->ifv_cookie);
777 * Store a cookie in our softc that drivers can use to store driver
778 * private per-instance data in.
781 vlan_setcookie(struct ifnet *ifp, void *cookie)
785 if (ifp->if_type != IFT_L2VLAN)
788 ifv->ifv_cookie = cookie;
793 * Return the vlan device present at the specific VID.
795 static struct ifnet *
796 vlan_devat(struct ifnet *ifp, uint16_t vid)
798 struct ifvlantrunk *trunk;
803 /* Not clear if callers are sleepable, so acquire the rmlock. */
805 trunk = ifp->if_vlantrunk;
812 ifv = vlan_gethash(trunk, vid);
815 TRUNK_RUNLOCK(trunk);
821 * Recalculate the cached VLAN tag exposed via the MIB.
824 vlan_tag_recalculate(struct ifvlan *ifv)
827 ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
831 * VLAN support can be loaded as a module. The only place in the
832 * system that's intimately aware of this is ether_input. We hook
833 * into this code through vlan_input_p which is defined there and
834 * set here. No one else in the system should be aware of this so
835 * we use an explicit reference here.
837 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
839 /* For if_link_state_change() eyes only... */
840 extern void (*vlan_link_state_p)(struct ifnet *);
843 vlan_modevent(module_t mod, int type, void *data)
848 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
849 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
850 if (ifdetach_tag == NULL)
852 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
853 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
854 if (iflladdr_tag == NULL)
857 vlan_input_p = vlan_input;
858 vlan_link_state_p = vlan_link_state;
859 vlan_trunk_cap_p = vlan_trunk_capabilities;
860 vlan_trunkdev_p = vlan_trunkdev;
861 vlan_cookie_p = vlan_cookie;
862 vlan_setcookie_p = vlan_setcookie;
863 vlan_tag_p = vlan_tag;
864 vlan_devat_p = vlan_devat;
866 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
867 vlan_clone_create, vlan_clone_destroy);
870 printf("vlan: initialized, using "
874 "hash tables with chaining"
881 if_clone_detach(vlan_cloner);
883 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
884 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
886 vlan_link_state_p = NULL;
887 vlan_trunk_cap_p = NULL;
888 vlan_trunkdev_p = NULL;
890 vlan_cookie_p = NULL;
891 vlan_setcookie_p = NULL;
893 VLAN_LOCKING_DESTROY();
895 printf("vlan: unloaded\n");
903 static moduledata_t vlan_mod = {
909 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
910 MODULE_VERSION(if_vlan, 3);
914 vnet_vlan_init(const void *unused __unused)
917 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
918 vlan_clone_create, vlan_clone_destroy);
919 V_vlan_cloner = vlan_cloner;
921 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
922 vnet_vlan_init, NULL);
925 vnet_vlan_uninit(const void *unused __unused)
928 if_clone_detach(V_vlan_cloner);
930 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
931 vnet_vlan_uninit, NULL);
935 * Check for <etherif>.<vlan> style interface names.
937 static struct ifnet *
938 vlan_clone_match_ethervid(const char *name, int *vidp)
940 char ifname[IFNAMSIZ];
945 strlcpy(ifname, name, IFNAMSIZ);
946 if ((cp = strchr(ifname, '.')) == NULL)
949 if ((ifp = ifunit_ref(ifname)) == NULL)
957 for(; *cp >= '0' && *cp <= '9'; cp++)
958 vid = (vid * 10) + (*cp - '0');
970 vlan_clone_match(struct if_clone *ifc, const char *name)
974 if (vlan_clone_match_ethervid(name, NULL) != NULL)
977 if (strncmp(vlanname, name, strlen(vlanname)) != 0)
979 for (cp = name + 4; *cp != '\0'; cp++) {
980 if (*cp < '0' || *cp > '9')
988 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
999 struct sockaddr_dl *sdl;
1001 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
1004 * There are 3 (ugh) ways to specify the cloned device:
1005 * o pass a parameter block with the clone request.
1006 * o specify parameters in the text of the clone device name
1007 * o specify no parameters and get an unattached device that
1008 * must be configured separately.
1009 * The first technique is preferred; the latter two are
1010 * supported for backwards compatibility.
1012 * XXXRW: Note historic use of the word "tag" here. New ioctls may be
1016 error = copyin(params, &vlr, sizeof(vlr));
1019 p = ifunit_ref(vlr.vlr_parent);
1022 error = ifc_name2unit(name, &unit);
1028 wildcard = (unit < 0);
1029 } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1034 error = ifc_name2unit(name, &unit);
1038 wildcard = (unit < 0);
1041 error = ifc_alloc_unit(ifc, &unit);
1048 /* In the wildcard case, we need to update the name. */
1050 for (dp = name; *dp != '\0'; dp++);
1051 if (snprintf(dp, len - (dp-name), "%d", unit) >
1052 len - (dp-name) - 1) {
1053 panic("%s: interface name too long", __func__);
1057 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1058 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1060 ifc_free_unit(ifc, unit);
1066 SLIST_INIT(&ifv->vlan_mc_listhead);
1067 ifp->if_softc = ifv;
1069 * Set the name manually rather than using if_initname because
1070 * we don't conform to the default naming convention for interfaces.
1072 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1073 ifp->if_dname = vlanname;
1074 ifp->if_dunit = unit;
1075 /* NB: flags are not set here */
1076 ifp->if_linkmib = &ifv->ifv_mib;
1077 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
1078 /* NB: mtu is not set here */
1080 ifp->if_init = vlan_init;
1081 ifp->if_transmit = vlan_transmit;
1082 ifp->if_qflush = vlan_qflush;
1083 ifp->if_ioctl = vlan_ioctl;
1085 ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1087 ifp->if_flags = VLAN_IFFLAGS;
1088 ether_ifattach(ifp, eaddr);
1089 /* Now undo some of the damage... */
1090 ifp->if_baudrate = 0;
1091 ifp->if_type = IFT_L2VLAN;
1092 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1094 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1095 sdl->sdl_type = IFT_L2VLAN;
1098 error = vlan_config(ifv, p, vid);
1102 * Since we've partially failed, we need to back
1103 * out all the way, otherwise userland could get
1104 * confused. Thus, we destroy the interface.
1106 ether_ifdetach(ifp);
1109 ifc_free_unit(ifc, unit);
1120 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
1122 struct ifvlan *ifv = ifp->if_softc;
1123 int unit = ifp->if_dunit;
1125 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
1126 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
1128 * We should have the only reference to the ifv now, so we can now
1129 * drain any remaining lladdr task before freeing the ifnet and the
1132 taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1135 ifc_free_unit(ifc, unit);
1141 * The ifp->if_init entry point for vlan(4) is a no-op.
1144 vlan_init(void *foo __unused)
1149 * The if_transmit method for vlan(4) interface.
1152 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1156 int error, len, mcast;
1160 ifv = ifp->if_softc;
1161 if (TRUNK(ifv) == NULL) {
1162 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1168 len = m->m_pkthdr.len;
1169 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1174 * Do not run parent's if_transmit() if the parent is not up,
1175 * or parent's driver will cause a system crash.
1177 if (!UP_AND_RUNNING(p)) {
1178 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1184 if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1185 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1191 * Send it, precisely as ether_output() would have.
1193 error = (p->if_transmit)(p, m);
1195 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1196 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1197 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1199 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1205 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1208 vlan_qflush(struct ifnet *ifp __unused)
1213 vlan_input(struct ifnet *ifp, struct mbuf *m)
1215 struct ifvlantrunk *trunk;
1223 trunk = ifp->if_vlantrunk;
1224 if (trunk == NULL) {
1230 if (m->m_flags & M_VLANTAG) {
1232 * Packet is tagged, but m contains a normal
1233 * Ethernet frame; the tag is stored out-of-band.
1235 tag = m->m_pkthdr.ether_vtag;
1236 m->m_flags &= ~M_VLANTAG;
1238 struct ether_vlan_header *evl;
1241 * Packet is tagged in-band as specified by 802.1q.
1243 switch (ifp->if_type) {
1245 if (m->m_len < sizeof(*evl) &&
1246 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1247 if_printf(ifp, "cannot pullup VLAN header\n");
1251 evl = mtod(m, struct ether_vlan_header *);
1252 tag = ntohs(evl->evl_tag);
1255 * Remove the 802.1q header by copying the Ethernet
1256 * addresses over it and adjusting the beginning of
1257 * the data in the mbuf. The encapsulated Ethernet
1258 * type field is already in place.
1260 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1261 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1262 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1267 panic("%s: %s has unsupported if_type %u",
1268 __func__, ifp->if_xname, ifp->if_type);
1270 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1277 vid = EVL_VLANOFTAG(tag);
1280 ifv = vlan_gethash(trunk, vid);
1281 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1282 TRUNK_RUNLOCK(trunk);
1283 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1288 TRUNK_RUNLOCK(trunk);
1290 if (vlan_mtag_pcp) {
1292 * While uncommon, it is possible that we will find a 802.1q
1293 * packet encapsulated inside another packet that also had an
1294 * 802.1q header. For example, ethernet tunneled over IPSEC
1295 * arriving over ethernet. In that case, we replace the
1296 * existing 802.1q PCP m_tag value.
1298 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1300 mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1301 sizeof(uint8_t), M_NOWAIT);
1303 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1308 m_tag_prepend(m, mtag);
1310 *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1313 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1314 if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1317 /* Pass it back through the parent's input routine. */
1318 (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1322 vlan_lladdr_fn(void *arg, int pending __unused)
1327 ifv = (struct ifvlan *)arg;
1329 /* The ifv_ifp already has the lladdr copied in. */
1330 if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1334 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
1336 struct ifvlantrunk *trunk;
1341 * We can handle non-ethernet hardware types as long as
1342 * they handle the tagging and headers themselves.
1344 if (p->if_type != IFT_ETHER &&
1345 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1346 return (EPROTONOSUPPORT);
1347 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1348 return (EPROTONOSUPPORT);
1350 * Don't let the caller set up a VLAN VID with
1351 * anything except VLID bits.
1352 * VID numbers 0x0 and 0xFFF are reserved.
1354 if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1359 /* Acquire rmlock after the branch so we can M_WAITOK. */
1361 if (p->if_vlantrunk == NULL) {
1362 trunk = malloc(sizeof(struct ifvlantrunk),
1363 M_VLAN, M_WAITOK | M_ZERO);
1364 vlan_inithash(trunk);
1365 TRUNK_LOCK_INIT(trunk);
1368 p->if_vlantrunk = trunk;
1370 if_ref(trunk->parent);
1373 trunk = p->if_vlantrunk;
1377 ifv->ifv_vid = vid; /* must set this before vlan_inshash() */
1378 ifv->ifv_pcp = 0; /* Default: best effort delivery. */
1379 vlan_tag_recalculate(ifv);
1380 error = vlan_inshash(trunk, ifv);
1383 ifv->ifv_proto = ETHERTYPE_VLAN;
1384 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1385 ifv->ifv_mintu = ETHERMIN;
1386 ifv->ifv_pflags = 0;
1387 ifv->ifv_capenable = -1;
1390 * If the parent supports the VLAN_MTU capability,
1391 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1394 if (p->if_capenable & IFCAP_VLAN_MTU) {
1396 * No need to fudge the MTU since the parent can
1397 * handle extended frames.
1399 ifv->ifv_mtufudge = 0;
1402 * Fudge the MTU by the encapsulation size. This
1403 * makes us incompatible with strictly compliant
1404 * 802.1Q implementations, but allows us to use
1405 * the feature with other NetBSD implementations,
1406 * which might still be useful.
1408 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1411 ifv->ifv_trunk = trunk;
1414 * Initialize fields from our parent. This duplicates some
1415 * work with ether_ifattach() but allows for non-ethernet
1416 * interfaces to also work.
1418 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1419 ifp->if_baudrate = p->if_baudrate;
1420 ifp->if_output = p->if_output;
1421 ifp->if_input = p->if_input;
1422 ifp->if_resolvemulti = p->if_resolvemulti;
1423 ifp->if_addrlen = p->if_addrlen;
1424 ifp->if_broadcastaddr = p->if_broadcastaddr;
1427 * Copy only a selected subset of flags from the parent.
1428 * Other flags are none of our business.
1430 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1431 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1432 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1433 #undef VLAN_COPY_FLAGS
1435 ifp->if_link_state = p->if_link_state;
1437 vlan_capabilities(ifv);
1440 * Set up our interface address to reflect the underlying
1441 * physical interface's.
1443 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1444 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1448 * Configure multicast addresses that may already be
1449 * joined on the vlan device.
1451 (void)vlan_setmulti(ifp);
1453 TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1455 /* We are ready for operation now. */
1456 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1458 /* Update flags on the parent, if necessary. */
1459 vlan_setflags(ifp, 1);
1462 * We need to drop the non-sleepable rmlock so that the underlying
1463 * devices can sleep in their vlan_config hooks.
1465 TRUNK_WUNLOCK(trunk);
1468 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1475 vlan_unconfig(struct ifnet *ifp)
1479 vlan_unconfig_locked(ifp, 0);
1484 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1486 struct ifvlantrunk *trunk;
1487 struct vlan_mc_entry *mc;
1489 struct ifnet *parent;
1492 VLAN_XLOCK_ASSERT();
1494 ifv = ifp->if_softc;
1495 trunk = ifv->ifv_trunk;
1498 if (trunk != NULL) {
1500 * Both vlan_transmit and vlan_input rely on the trunk fields
1501 * being NULL to determine whether to bail, so we need to get
1502 * an exclusive lock here to prevent them from using bad
1506 parent = trunk->parent;
1509 * Since the interface is being unconfigured, we need to
1510 * empty the list of multicast groups that we may have joined
1511 * while we were alive from the parent's list.
1513 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1515 * If the parent interface is being detached,
1516 * all its multicast addresses have already
1517 * been removed. Warn about errors if
1518 * if_delmulti() does fail, but don't abort as
1519 * all callers expect vlan destruction to
1523 error = if_delmulti(parent,
1524 (struct sockaddr *)&mc->mc_addr);
1527 "Failed to delete multicast address from parent: %d\n",
1530 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1534 vlan_setflags(ifp, 0); /* clear special flags on parent */
1537 * The trunk lock isn't actually required here, but
1538 * vlan_remhash expects it.
1541 vlan_remhash(trunk, ifv);
1542 TRUNK_WUNLOCK(trunk);
1543 ifv->ifv_trunk = NULL;
1546 * Check if we were the last.
1548 if (trunk->refcnt == 0) {
1549 parent->if_vlantrunk = NULL;
1550 trunk_destroy(trunk);
1555 /* Disconnect from parent. */
1556 if (ifv->ifv_pflags)
1557 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1558 ifp->if_mtu = ETHERMTU;
1559 ifp->if_link_state = LINK_STATE_UNKNOWN;
1560 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1563 * Only dispatch an event if vlan was
1564 * attached, otherwise there is nothing
1565 * to cleanup anyway.
1568 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1571 /* Handle a reference counted flag that should be set on the parent as well */
1573 vlan_setflag(struct ifnet *ifp, int flag, int status,
1574 int (*func)(struct ifnet *, int))
1579 VLAN_SXLOCK_ASSERT();
1581 ifv = ifp->if_softc;
1582 status = status ? (ifp->if_flags & flag) : 0;
1583 /* Now "status" contains the flag value or 0 */
1586 * See if recorded parent's status is different from what
1587 * we want it to be. If it is, flip it. We record parent's
1588 * status in ifv_pflags so that we won't clear parent's flag
1589 * we haven't set. In fact, we don't clear or set parent's
1590 * flags directly, but get or release references to them.
1591 * That's why we can be sure that recorded flags still are
1592 * in accord with actual parent's flags.
1594 if (status != (ifv->ifv_pflags & flag)) {
1595 error = (*func)(PARENT(ifv), status);
1598 ifv->ifv_pflags &= ~flag;
1599 ifv->ifv_pflags |= status;
1605 * Handle IFF_* flags that require certain changes on the parent:
1606 * if "status" is true, update parent's flags respective to our if_flags;
1607 * if "status" is false, forcedly clear the flags set on parent.
1610 vlan_setflags(struct ifnet *ifp, int status)
1614 for (i = 0; vlan_pflags[i].flag; i++) {
1615 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1616 status, vlan_pflags[i].func);
1623 /* Inform all vlans that their parent has changed link state */
1625 vlan_link_state(struct ifnet *ifp)
1627 struct ifvlantrunk *trunk;
1631 /* Called from a taskqueue_swi task, so we cannot sleep. */
1633 trunk = ifp->if_vlantrunk;
1634 if (trunk == NULL) {
1640 VLAN_FOREACH(ifv, trunk) {
1641 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1642 if_link_state_change(ifv->ifv_ifp,
1643 trunk->parent->if_link_state);
1645 TRUNK_WUNLOCK(trunk);
1650 vlan_capabilities(struct ifvlan *ifv)
1654 struct ifnet_hw_tsomax hw_tsomax;
1655 int cap = 0, ena = 0, mena;
1658 VLAN_SXLOCK_ASSERT();
1659 TRUNK_WLOCK_ASSERT(TRUNK(ifv));
1663 /* Mask parent interface enabled capabilities disabled by user. */
1664 mena = p->if_capenable & ifv->ifv_capenable;
1667 * If the parent interface can do checksum offloading
1668 * on VLANs, then propagate its hardware-assisted
1669 * checksumming flags. Also assert that checksum
1670 * offloading requires hardware VLAN tagging.
1672 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1673 cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1674 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1675 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1676 ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1677 if (ena & IFCAP_TXCSUM)
1678 hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1679 CSUM_UDP | CSUM_SCTP);
1680 if (ena & IFCAP_TXCSUM_IPV6)
1681 hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1682 CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
1686 * If the parent interface can do TSO on VLANs then
1687 * propagate the hardware-assisted flag. TSO on VLANs
1688 * does not necessarily require hardware VLAN tagging.
1690 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
1691 if_hw_tsomax_common(p, &hw_tsomax);
1692 if_hw_tsomax_update(ifp, &hw_tsomax);
1693 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1694 cap |= p->if_capabilities & IFCAP_TSO;
1695 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1696 ena |= mena & IFCAP_TSO;
1697 if (ena & IFCAP_TSO)
1698 hwa |= p->if_hwassist & CSUM_TSO;
1702 * If the parent interface can do LRO and checksum offloading on
1703 * VLANs, then guess it may do LRO on VLANs. False positive here
1704 * cost nothing, while false negative may lead to some confusions.
1706 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1707 cap |= p->if_capabilities & IFCAP_LRO;
1708 if (p->if_capenable & IFCAP_VLAN_HWCSUM)
1709 ena |= p->if_capenable & IFCAP_LRO;
1712 * If the parent interface can offload TCP connections over VLANs then
1713 * propagate its TOE capability to the VLAN interface.
1715 * All TOE drivers in the tree today can deal with VLANs. If this
1716 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1719 #define IFCAP_VLAN_TOE IFCAP_TOE
1720 if (p->if_capabilities & IFCAP_VLAN_TOE)
1721 cap |= p->if_capabilities & IFCAP_TOE;
1722 if (p->if_capenable & IFCAP_VLAN_TOE) {
1723 TOEDEV(ifp) = TOEDEV(p);
1724 ena |= mena & IFCAP_TOE;
1728 * If the parent interface supports dynamic link state, so does the
1731 cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1732 ena |= (mena & IFCAP_LINKSTATE);
1736 * If the parent interface supports ratelimiting, so does the
1739 cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1740 ena |= (mena & IFCAP_TXRTLMT);
1743 ifp->if_capabilities = cap;
1744 ifp->if_capenable = ena;
1745 ifp->if_hwassist = hwa;
1749 vlan_trunk_capabilities(struct ifnet *ifp)
1751 struct ifvlantrunk *trunk;
1755 trunk = ifp->if_vlantrunk;
1756 if (trunk == NULL) {
1761 VLAN_FOREACH(ifv, trunk) {
1762 vlan_capabilities(ifv);
1764 TRUNK_WUNLOCK(trunk);
1769 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1775 struct ifvlantrunk *trunk;
1780 ifr = (struct ifreq *)data;
1781 ifa = (struct ifaddr *) data;
1782 ifv = ifp->if_softc;
1786 ifp->if_flags |= IFF_UP;
1788 if (ifa->ifa_addr->sa_family == AF_INET)
1789 arp_ifinit(ifp, ifa);
1793 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
1798 if (TRUNK(ifv) != NULL) {
1801 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1803 /* Limit the result to the parent's current config. */
1805 struct ifmediareq *ifmr;
1807 ifmr = (struct ifmediareq *)data;
1808 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1809 ifmr->ifm_count = 1;
1810 error = copyout(&ifmr->ifm_current,
1827 * Set the interface MTU.
1831 if (trunk != NULL) {
1834 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1836 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1839 ifp->if_mtu = ifr->ifr_mtu;
1840 TRUNK_WUNLOCK(trunk);
1849 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
1850 * interface to be delegated to a jail without allowing the
1851 * jail to change what underlying interface/VID it is
1852 * associated with. We are not entirely convinced that this
1853 * is the right way to accomplish that policy goal.
1855 if (ifp->if_vnet != ifp->if_home_vnet) {
1860 error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
1863 if (vlr.vlr_parent[0] == '\0') {
1867 p = ifunit_ref(vlr.vlr_parent);
1872 error = vlan_config(ifv, p, vlr.vlr_tag);
1878 if (ifp->if_vnet != ifp->if_home_vnet) {
1883 bzero(&vlr, sizeof(vlr));
1885 if (TRUNK(ifv) != NULL) {
1886 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1887 sizeof(vlr.vlr_parent));
1888 vlr.vlr_tag = ifv->ifv_vid;
1891 error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
1896 * We should propagate selected flags to the parent,
1897 * e.g., promiscuous mode.
1900 if (TRUNK(ifv) != NULL)
1901 error = vlan_setflags(ifp, 1);
1908 * If we don't have a parent, just remember the membership for
1911 * XXX We need the rmlock here to avoid sleeping while
1912 * holding in6_multi_mtx.
1916 if (trunk != NULL) {
1918 error = vlan_setmulti(ifp);
1919 TRUNK_WUNLOCK(trunk);
1926 if (ifp->if_vnet != ifp->if_home_vnet) {
1931 ifr->ifr_vlan_pcp = ifv->ifv_pcp;
1936 if (ifp->if_vnet != ifp->if_home_vnet) {
1941 error = priv_check(curthread, PRIV_NET_SETVLANPCP);
1944 if (ifr->ifr_vlan_pcp > 7) {
1948 ifv->ifv_pcp = ifr->ifr_vlan_pcp;
1949 vlan_tag_recalculate(ifv);
1950 /* broadcast event about PCP change */
1951 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
1956 ifv->ifv_capenable = ifr->ifr_reqcap;
1958 if (trunk != NULL) {
1960 vlan_capabilities(ifv);
1961 TRUNK_WUNLOCK(trunk);
1976 vlan_snd_tag_alloc(struct ifnet *ifp,
1977 union if_snd_tag_alloc_params *params,
1978 struct m_snd_tag **ppmt)
1981 /* get trunk device */
1982 ifp = vlan_trunkdev(ifp);
1983 if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1984 return (EOPNOTSUPP);
1985 /* forward allocation request */
1986 return (ifp->if_snd_tag_alloc(ifp, params, ppmt));