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 vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
766 if (ifp->if_type != IFT_L2VLAN)
769 *pcpp = ifv->ifv_pcp;
774 * Return a driver specific cookie for this interface. Synchronization
775 * with setcookie must be provided by the driver.
778 vlan_cookie(struct ifnet *ifp)
782 if (ifp->if_type != IFT_L2VLAN)
785 return (ifv->ifv_cookie);
789 * Store a cookie in our softc that drivers can use to store driver
790 * private per-instance data in.
793 vlan_setcookie(struct ifnet *ifp, void *cookie)
797 if (ifp->if_type != IFT_L2VLAN)
800 ifv->ifv_cookie = cookie;
805 * Return the vlan device present at the specific VID.
807 static struct ifnet *
808 vlan_devat(struct ifnet *ifp, uint16_t vid)
810 struct ifvlantrunk *trunk;
815 /* Not clear if callers are sleepable, so acquire the rmlock. */
817 trunk = ifp->if_vlantrunk;
824 ifv = vlan_gethash(trunk, vid);
827 TRUNK_RUNLOCK(trunk);
833 * Recalculate the cached VLAN tag exposed via the MIB.
836 vlan_tag_recalculate(struct ifvlan *ifv)
839 ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
843 * VLAN support can be loaded as a module. The only place in the
844 * system that's intimately aware of this is ether_input. We hook
845 * into this code through vlan_input_p which is defined there and
846 * set here. No one else in the system should be aware of this so
847 * we use an explicit reference here.
849 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
851 /* For if_link_state_change() eyes only... */
852 extern void (*vlan_link_state_p)(struct ifnet *);
855 vlan_modevent(module_t mod, int type, void *data)
860 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
861 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
862 if (ifdetach_tag == NULL)
864 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
865 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
866 if (iflladdr_tag == NULL)
869 vlan_input_p = vlan_input;
870 vlan_link_state_p = vlan_link_state;
871 vlan_trunk_cap_p = vlan_trunk_capabilities;
872 vlan_trunkdev_p = vlan_trunkdev;
873 vlan_cookie_p = vlan_cookie;
874 vlan_setcookie_p = vlan_setcookie;
875 vlan_tag_p = vlan_tag;
876 vlan_pcp_p = vlan_pcp;
877 vlan_devat_p = vlan_devat;
879 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
880 vlan_clone_create, vlan_clone_destroy);
883 printf("vlan: initialized, using "
887 "hash tables with chaining"
894 if_clone_detach(vlan_cloner);
896 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
897 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
899 vlan_link_state_p = NULL;
900 vlan_trunk_cap_p = NULL;
901 vlan_trunkdev_p = NULL;
903 vlan_cookie_p = NULL;
904 vlan_setcookie_p = NULL;
906 VLAN_LOCKING_DESTROY();
908 printf("vlan: unloaded\n");
916 static moduledata_t vlan_mod = {
922 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
923 MODULE_VERSION(if_vlan, 3);
927 vnet_vlan_init(const void *unused __unused)
930 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
931 vlan_clone_create, vlan_clone_destroy);
932 V_vlan_cloner = vlan_cloner;
934 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
935 vnet_vlan_init, NULL);
938 vnet_vlan_uninit(const void *unused __unused)
941 if_clone_detach(V_vlan_cloner);
943 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
944 vnet_vlan_uninit, NULL);
948 * Check for <etherif>.<vlan> style interface names.
950 static struct ifnet *
951 vlan_clone_match_ethervid(const char *name, int *vidp)
953 char ifname[IFNAMSIZ];
958 strlcpy(ifname, name, IFNAMSIZ);
959 if ((cp = strchr(ifname, '.')) == NULL)
962 if ((ifp = ifunit_ref(ifname)) == NULL)
970 for(; *cp >= '0' && *cp <= '9'; cp++)
971 vid = (vid * 10) + (*cp - '0');
983 vlan_clone_match(struct if_clone *ifc, const char *name)
987 if (vlan_clone_match_ethervid(name, NULL) != NULL)
990 if (strncmp(vlanname, name, strlen(vlanname)) != 0)
992 for (cp = name + 4; *cp != '\0'; cp++) {
993 if (*cp < '0' || *cp > '9')
1001 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
1012 struct sockaddr_dl *sdl;
1014 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
1017 * There are 3 (ugh) ways to specify the cloned device:
1018 * o pass a parameter block with the clone request.
1019 * o specify parameters in the text of the clone device name
1020 * o specify no parameters and get an unattached device that
1021 * must be configured separately.
1022 * The first technique is preferred; the latter two are
1023 * supported for backwards compatibility.
1025 * XXXRW: Note historic use of the word "tag" here. New ioctls may be
1029 error = copyin(params, &vlr, sizeof(vlr));
1032 p = ifunit_ref(vlr.vlr_parent);
1035 error = ifc_name2unit(name, &unit);
1041 wildcard = (unit < 0);
1042 } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1047 error = ifc_name2unit(name, &unit);
1051 wildcard = (unit < 0);
1054 error = ifc_alloc_unit(ifc, &unit);
1061 /* In the wildcard case, we need to update the name. */
1063 for (dp = name; *dp != '\0'; dp++);
1064 if (snprintf(dp, len - (dp-name), "%d", unit) >
1065 len - (dp-name) - 1) {
1066 panic("%s: interface name too long", __func__);
1070 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1071 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1073 ifc_free_unit(ifc, unit);
1079 SLIST_INIT(&ifv->vlan_mc_listhead);
1080 ifp->if_softc = ifv;
1082 * Set the name manually rather than using if_initname because
1083 * we don't conform to the default naming convention for interfaces.
1085 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1086 ifp->if_dname = vlanname;
1087 ifp->if_dunit = unit;
1088 /* NB: flags are not set here */
1089 ifp->if_linkmib = &ifv->ifv_mib;
1090 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
1091 /* NB: mtu is not set here */
1093 ifp->if_init = vlan_init;
1094 ifp->if_transmit = vlan_transmit;
1095 ifp->if_qflush = vlan_qflush;
1096 ifp->if_ioctl = vlan_ioctl;
1098 ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1100 ifp->if_flags = VLAN_IFFLAGS;
1101 ether_ifattach(ifp, eaddr);
1102 /* Now undo some of the damage... */
1103 ifp->if_baudrate = 0;
1104 ifp->if_type = IFT_L2VLAN;
1105 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1107 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1108 sdl->sdl_type = IFT_L2VLAN;
1111 error = vlan_config(ifv, p, vid);
1115 * Since we've partially failed, we need to back
1116 * out all the way, otherwise userland could get
1117 * confused. Thus, we destroy the interface.
1119 ether_ifdetach(ifp);
1122 ifc_free_unit(ifc, unit);
1133 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
1135 struct ifvlan *ifv = ifp->if_softc;
1136 int unit = ifp->if_dunit;
1138 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
1139 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
1141 * We should have the only reference to the ifv now, so we can now
1142 * drain any remaining lladdr task before freeing the ifnet and the
1145 taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1148 ifc_free_unit(ifc, unit);
1154 * The ifp->if_init entry point for vlan(4) is a no-op.
1157 vlan_init(void *foo __unused)
1162 * The if_transmit method for vlan(4) interface.
1165 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1169 int error, len, mcast;
1173 ifv = ifp->if_softc;
1174 if (TRUNK(ifv) == NULL) {
1175 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1181 len = m->m_pkthdr.len;
1182 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1187 * Do not run parent's if_transmit() if the parent is not up,
1188 * or parent's driver will cause a system crash.
1190 if (!UP_AND_RUNNING(p)) {
1191 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1197 if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1198 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1204 * Send it, precisely as ether_output() would have.
1206 error = (p->if_transmit)(p, m);
1208 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1209 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1210 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1212 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1218 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1221 vlan_qflush(struct ifnet *ifp __unused)
1226 vlan_input(struct ifnet *ifp, struct mbuf *m)
1228 struct ifvlantrunk *trunk;
1236 trunk = ifp->if_vlantrunk;
1237 if (trunk == NULL) {
1243 if (m->m_flags & M_VLANTAG) {
1245 * Packet is tagged, but m contains a normal
1246 * Ethernet frame; the tag is stored out-of-band.
1248 tag = m->m_pkthdr.ether_vtag;
1249 m->m_flags &= ~M_VLANTAG;
1251 struct ether_vlan_header *evl;
1254 * Packet is tagged in-band as specified by 802.1q.
1256 switch (ifp->if_type) {
1258 if (m->m_len < sizeof(*evl) &&
1259 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1260 if_printf(ifp, "cannot pullup VLAN header\n");
1264 evl = mtod(m, struct ether_vlan_header *);
1265 tag = ntohs(evl->evl_tag);
1268 * Remove the 802.1q header by copying the Ethernet
1269 * addresses over it and adjusting the beginning of
1270 * the data in the mbuf. The encapsulated Ethernet
1271 * type field is already in place.
1273 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1274 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1275 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1280 panic("%s: %s has unsupported if_type %u",
1281 __func__, ifp->if_xname, ifp->if_type);
1283 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1290 vid = EVL_VLANOFTAG(tag);
1293 ifv = vlan_gethash(trunk, vid);
1294 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1295 TRUNK_RUNLOCK(trunk);
1296 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1301 TRUNK_RUNLOCK(trunk);
1303 if (vlan_mtag_pcp) {
1305 * While uncommon, it is possible that we will find a 802.1q
1306 * packet encapsulated inside another packet that also had an
1307 * 802.1q header. For example, ethernet tunneled over IPSEC
1308 * arriving over ethernet. In that case, we replace the
1309 * existing 802.1q PCP m_tag value.
1311 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1313 mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1314 sizeof(uint8_t), M_NOWAIT);
1316 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1321 m_tag_prepend(m, mtag);
1323 *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1326 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1327 if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1330 /* Pass it back through the parent's input routine. */
1331 (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1335 vlan_lladdr_fn(void *arg, int pending __unused)
1340 ifv = (struct ifvlan *)arg;
1342 /* The ifv_ifp already has the lladdr copied in. */
1343 if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1347 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
1349 struct ifvlantrunk *trunk;
1354 * We can handle non-ethernet hardware types as long as
1355 * they handle the tagging and headers themselves.
1357 if (p->if_type != IFT_ETHER &&
1358 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1359 return (EPROTONOSUPPORT);
1360 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1361 return (EPROTONOSUPPORT);
1363 * Don't let the caller set up a VLAN VID with
1364 * anything except VLID bits.
1365 * VID numbers 0x0 and 0xFFF are reserved.
1367 if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1372 /* Acquire rmlock after the branch so we can M_WAITOK. */
1374 if (p->if_vlantrunk == NULL) {
1375 trunk = malloc(sizeof(struct ifvlantrunk),
1376 M_VLAN, M_WAITOK | M_ZERO);
1377 vlan_inithash(trunk);
1378 TRUNK_LOCK_INIT(trunk);
1381 p->if_vlantrunk = trunk;
1383 if_ref(trunk->parent);
1386 trunk = p->if_vlantrunk;
1390 ifv->ifv_vid = vid; /* must set this before vlan_inshash() */
1391 ifv->ifv_pcp = 0; /* Default: best effort delivery. */
1392 vlan_tag_recalculate(ifv);
1393 error = vlan_inshash(trunk, ifv);
1396 ifv->ifv_proto = ETHERTYPE_VLAN;
1397 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1398 ifv->ifv_mintu = ETHERMIN;
1399 ifv->ifv_pflags = 0;
1400 ifv->ifv_capenable = -1;
1403 * If the parent supports the VLAN_MTU capability,
1404 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1407 if (p->if_capenable & IFCAP_VLAN_MTU) {
1409 * No need to fudge the MTU since the parent can
1410 * handle extended frames.
1412 ifv->ifv_mtufudge = 0;
1415 * Fudge the MTU by the encapsulation size. This
1416 * makes us incompatible with strictly compliant
1417 * 802.1Q implementations, but allows us to use
1418 * the feature with other NetBSD implementations,
1419 * which might still be useful.
1421 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1424 ifv->ifv_trunk = trunk;
1427 * Initialize fields from our parent. This duplicates some
1428 * work with ether_ifattach() but allows for non-ethernet
1429 * interfaces to also work.
1431 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1432 ifp->if_baudrate = p->if_baudrate;
1433 ifp->if_output = p->if_output;
1434 ifp->if_input = p->if_input;
1435 ifp->if_resolvemulti = p->if_resolvemulti;
1436 ifp->if_addrlen = p->if_addrlen;
1437 ifp->if_broadcastaddr = p->if_broadcastaddr;
1438 ifp->if_pcp = ifv->ifv_pcp;
1441 * Copy only a selected subset of flags from the parent.
1442 * Other flags are none of our business.
1444 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1445 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1446 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1447 #undef VLAN_COPY_FLAGS
1449 ifp->if_link_state = p->if_link_state;
1451 vlan_capabilities(ifv);
1454 * Set up our interface address to reflect the underlying
1455 * physical interface's.
1457 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1458 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1462 * Configure multicast addresses that may already be
1463 * joined on the vlan device.
1465 (void)vlan_setmulti(ifp);
1467 TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1469 /* We are ready for operation now. */
1470 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1472 /* Update flags on the parent, if necessary. */
1473 vlan_setflags(ifp, 1);
1476 * We need to drop the non-sleepable rmlock so that the underlying
1477 * devices can sleep in their vlan_config hooks.
1479 TRUNK_WUNLOCK(trunk);
1482 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1489 vlan_unconfig(struct ifnet *ifp)
1493 vlan_unconfig_locked(ifp, 0);
1498 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1500 struct ifvlantrunk *trunk;
1501 struct vlan_mc_entry *mc;
1503 struct ifnet *parent;
1506 VLAN_XLOCK_ASSERT();
1508 ifv = ifp->if_softc;
1509 trunk = ifv->ifv_trunk;
1512 if (trunk != NULL) {
1514 * Both vlan_transmit and vlan_input rely on the trunk fields
1515 * being NULL to determine whether to bail, so we need to get
1516 * an exclusive lock here to prevent them from using bad
1520 parent = trunk->parent;
1523 * Since the interface is being unconfigured, we need to
1524 * empty the list of multicast groups that we may have joined
1525 * while we were alive from the parent's list.
1527 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1529 * If the parent interface is being detached,
1530 * all its multicast addresses have already
1531 * been removed. Warn about errors if
1532 * if_delmulti() does fail, but don't abort as
1533 * all callers expect vlan destruction to
1537 error = if_delmulti(parent,
1538 (struct sockaddr *)&mc->mc_addr);
1541 "Failed to delete multicast address from parent: %d\n",
1544 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1548 vlan_setflags(ifp, 0); /* clear special flags on parent */
1551 * The trunk lock isn't actually required here, but
1552 * vlan_remhash expects it.
1555 vlan_remhash(trunk, ifv);
1556 TRUNK_WUNLOCK(trunk);
1557 ifv->ifv_trunk = NULL;
1560 * Check if we were the last.
1562 if (trunk->refcnt == 0) {
1563 parent->if_vlantrunk = NULL;
1564 trunk_destroy(trunk);
1569 /* Disconnect from parent. */
1570 if (ifv->ifv_pflags)
1571 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1572 ifp->if_mtu = ETHERMTU;
1573 ifp->if_link_state = LINK_STATE_UNKNOWN;
1574 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1577 * Only dispatch an event if vlan was
1578 * attached, otherwise there is nothing
1579 * to cleanup anyway.
1582 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1585 /* Handle a reference counted flag that should be set on the parent as well */
1587 vlan_setflag(struct ifnet *ifp, int flag, int status,
1588 int (*func)(struct ifnet *, int))
1593 VLAN_SXLOCK_ASSERT();
1595 ifv = ifp->if_softc;
1596 status = status ? (ifp->if_flags & flag) : 0;
1597 /* Now "status" contains the flag value or 0 */
1600 * See if recorded parent's status is different from what
1601 * we want it to be. If it is, flip it. We record parent's
1602 * status in ifv_pflags so that we won't clear parent's flag
1603 * we haven't set. In fact, we don't clear or set parent's
1604 * flags directly, but get or release references to them.
1605 * That's why we can be sure that recorded flags still are
1606 * in accord with actual parent's flags.
1608 if (status != (ifv->ifv_pflags & flag)) {
1609 error = (*func)(PARENT(ifv), status);
1612 ifv->ifv_pflags &= ~flag;
1613 ifv->ifv_pflags |= status;
1619 * Handle IFF_* flags that require certain changes on the parent:
1620 * if "status" is true, update parent's flags respective to our if_flags;
1621 * if "status" is false, forcedly clear the flags set on parent.
1624 vlan_setflags(struct ifnet *ifp, int status)
1628 for (i = 0; vlan_pflags[i].flag; i++) {
1629 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1630 status, vlan_pflags[i].func);
1637 /* Inform all vlans that their parent has changed link state */
1639 vlan_link_state(struct ifnet *ifp)
1641 struct ifvlantrunk *trunk;
1645 /* Called from a taskqueue_swi task, so we cannot sleep. */
1647 trunk = ifp->if_vlantrunk;
1648 if (trunk == NULL) {
1654 VLAN_FOREACH(ifv, trunk) {
1655 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1656 if_link_state_change(ifv->ifv_ifp,
1657 trunk->parent->if_link_state);
1659 TRUNK_WUNLOCK(trunk);
1664 vlan_capabilities(struct ifvlan *ifv)
1668 struct ifnet_hw_tsomax hw_tsomax;
1669 int cap = 0, ena = 0, mena;
1672 VLAN_SXLOCK_ASSERT();
1673 TRUNK_WLOCK_ASSERT(TRUNK(ifv));
1677 /* Mask parent interface enabled capabilities disabled by user. */
1678 mena = p->if_capenable & ifv->ifv_capenable;
1681 * If the parent interface can do checksum offloading
1682 * on VLANs, then propagate its hardware-assisted
1683 * checksumming flags. Also assert that checksum
1684 * offloading requires hardware VLAN tagging.
1686 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1687 cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1688 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1689 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1690 ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1691 if (ena & IFCAP_TXCSUM)
1692 hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1693 CSUM_UDP | CSUM_SCTP);
1694 if (ena & IFCAP_TXCSUM_IPV6)
1695 hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1696 CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
1700 * If the parent interface can do TSO on VLANs then
1701 * propagate the hardware-assisted flag. TSO on VLANs
1702 * does not necessarily require hardware VLAN tagging.
1704 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
1705 if_hw_tsomax_common(p, &hw_tsomax);
1706 if_hw_tsomax_update(ifp, &hw_tsomax);
1707 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1708 cap |= p->if_capabilities & IFCAP_TSO;
1709 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1710 ena |= mena & IFCAP_TSO;
1711 if (ena & IFCAP_TSO)
1712 hwa |= p->if_hwassist & CSUM_TSO;
1716 * If the parent interface can do LRO and checksum offloading on
1717 * VLANs, then guess it may do LRO on VLANs. False positive here
1718 * cost nothing, while false negative may lead to some confusions.
1720 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1721 cap |= p->if_capabilities & IFCAP_LRO;
1722 if (p->if_capenable & IFCAP_VLAN_HWCSUM)
1723 ena |= p->if_capenable & IFCAP_LRO;
1726 * If the parent interface can offload TCP connections over VLANs then
1727 * propagate its TOE capability to the VLAN interface.
1729 * All TOE drivers in the tree today can deal with VLANs. If this
1730 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1733 #define IFCAP_VLAN_TOE IFCAP_TOE
1734 if (p->if_capabilities & IFCAP_VLAN_TOE)
1735 cap |= p->if_capabilities & IFCAP_TOE;
1736 if (p->if_capenable & IFCAP_VLAN_TOE) {
1737 TOEDEV(ifp) = TOEDEV(p);
1738 ena |= mena & IFCAP_TOE;
1742 * If the parent interface supports dynamic link state, so does the
1745 cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1746 ena |= (mena & IFCAP_LINKSTATE);
1750 * If the parent interface supports ratelimiting, so does the
1753 cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1754 ena |= (mena & IFCAP_TXRTLMT);
1757 ifp->if_capabilities = cap;
1758 ifp->if_capenable = ena;
1759 ifp->if_hwassist = hwa;
1763 vlan_trunk_capabilities(struct ifnet *ifp)
1765 struct ifvlantrunk *trunk;
1769 trunk = ifp->if_vlantrunk;
1770 if (trunk == NULL) {
1775 VLAN_FOREACH(ifv, trunk) {
1776 vlan_capabilities(ifv);
1778 TRUNK_WUNLOCK(trunk);
1783 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1789 struct ifvlantrunk *trunk;
1794 ifr = (struct ifreq *)data;
1795 ifa = (struct ifaddr *) data;
1796 ifv = ifp->if_softc;
1800 ifp->if_flags |= IFF_UP;
1802 if (ifa->ifa_addr->sa_family == AF_INET)
1803 arp_ifinit(ifp, ifa);
1807 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
1812 if (TRUNK(ifv) != NULL) {
1815 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1817 /* Limit the result to the parent's current config. */
1819 struct ifmediareq *ifmr;
1821 ifmr = (struct ifmediareq *)data;
1822 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1823 ifmr->ifm_count = 1;
1824 error = copyout(&ifmr->ifm_current,
1841 * Set the interface MTU.
1845 if (trunk != NULL) {
1848 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1850 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1853 ifp->if_mtu = ifr->ifr_mtu;
1854 TRUNK_WUNLOCK(trunk);
1863 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
1864 * interface to be delegated to a jail without allowing the
1865 * jail to change what underlying interface/VID it is
1866 * associated with. We are not entirely convinced that this
1867 * is the right way to accomplish that policy goal.
1869 if (ifp->if_vnet != ifp->if_home_vnet) {
1874 error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
1877 if (vlr.vlr_parent[0] == '\0') {
1881 p = ifunit_ref(vlr.vlr_parent);
1886 error = vlan_config(ifv, p, vlr.vlr_tag);
1892 if (ifp->if_vnet != ifp->if_home_vnet) {
1897 bzero(&vlr, sizeof(vlr));
1899 if (TRUNK(ifv) != NULL) {
1900 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1901 sizeof(vlr.vlr_parent));
1902 vlr.vlr_tag = ifv->ifv_vid;
1905 error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
1910 * We should propagate selected flags to the parent,
1911 * e.g., promiscuous mode.
1914 if (TRUNK(ifv) != NULL)
1915 error = vlan_setflags(ifp, 1);
1922 * If we don't have a parent, just remember the membership for
1925 * XXX We need the rmlock here to avoid sleeping while
1926 * holding in6_multi_mtx.
1930 if (trunk != NULL) {
1932 error = vlan_setmulti(ifp);
1933 TRUNK_WUNLOCK(trunk);
1940 if (ifp->if_vnet != ifp->if_home_vnet) {
1945 ifr->ifr_vlan_pcp = ifv->ifv_pcp;
1950 if (ifp->if_vnet != ifp->if_home_vnet) {
1955 error = priv_check(curthread, PRIV_NET_SETVLANPCP);
1958 if (ifr->ifr_vlan_pcp > 7) {
1962 ifv->ifv_pcp = ifr->ifr_vlan_pcp;
1963 ifp->if_pcp = ifv->ifv_pcp;
1964 vlan_tag_recalculate(ifv);
1965 /* broadcast event about PCP change */
1966 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
1971 ifv->ifv_capenable = ifr->ifr_reqcap;
1973 if (trunk != NULL) {
1975 vlan_capabilities(ifv);
1976 TRUNK_WUNLOCK(trunk);
1991 vlan_snd_tag_alloc(struct ifnet *ifp,
1992 union if_snd_tag_alloc_params *params,
1993 struct m_snd_tag **ppmt)
1996 /* get trunk device */
1997 ifp = vlan_trunkdev(ifp);
1998 if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1999 return (EOPNOTSUPP);
2000 /* forward allocation request */
2001 return (ifp->if_snd_tag_alloc(ifp, params, ppmt));