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$");
49 #include "opt_inet6.h"
51 #include "opt_ratelimit.h"
53 #include <sys/param.h>
54 #include <sys/eventhandler.h>
55 #include <sys/kernel.h>
57 #include <sys/malloc.h>
59 #include <sys/module.h>
60 #include <sys/rmlock.h>
62 #include <sys/queue.h>
63 #include <sys/socket.h>
64 #include <sys/sockio.h>
65 #include <sys/sysctl.h>
66 #include <sys/systm.h>
68 #include <sys/taskqueue.h>
71 #include <net/ethernet.h>
73 #include <net/if_var.h>
74 #include <net/if_clone.h>
75 #include <net/if_dl.h>
76 #include <net/if_types.h>
77 #include <net/if_vlan_var.h>
78 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/if_ether.h>
88 * XXX: declare here to avoid to include many inet6 related files..
89 * should be more generalized?
91 extern void nd6_setmtu(struct ifnet *);
94 #define VLAN_DEF_HWIDTH 4
95 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
97 #define UP_AND_RUNNING(ifp) \
98 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
100 CK_SLIST_HEAD(ifvlanhead, ifvlan);
103 struct ifnet *parent; /* parent interface of this trunk */
106 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
107 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
109 struct ifvlanhead *hash; /* dynamic hash-list table */
117 * This macro provides a facility to iterate over every vlan on a trunk with
118 * the assumption that none will be added/removed during iteration.
121 #define VLAN_FOREACH(_ifv, _trunk) \
123 for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
124 if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
125 #else /* VLAN_ARRAY */
126 #define VLAN_FOREACH(_ifv, _trunk) \
127 struct ifvlan *_next; \
129 for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
130 CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
131 #endif /* VLAN_ARRAY */
134 * This macro provides a facility to iterate over every vlan on a trunk while
135 * also modifying the number of vlans on the trunk. The iteration continues
136 * until some condition is met or there are no more vlans on the trunk.
139 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
140 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
142 for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
143 if (((_ifv) = (_trunk)->vlans[_i]))
144 #else /* VLAN_ARRAY */
146 * The hash table case is more complicated. We allow for the hash table to be
147 * modified (i.e. vlans removed) while we are iterating over it. To allow for
148 * this we must restart the iteration every time we "touch" something during
149 * the iteration, since removal will resize the hash table and invalidate our
150 * current position. If acting on the touched element causes the trunk to be
151 * emptied, then iteration also stops.
153 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
155 bool _touch = false; \
157 !(_cond) && _i < (1 << (_trunk)->hwidth); \
158 _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
159 if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
161 #endif /* VLAN_ARRAY */
163 struct vlan_mc_entry {
164 struct sockaddr_dl mc_addr;
165 CK_SLIST_ENTRY(vlan_mc_entry) mc_entries;
166 struct epoch_context mc_epoch_ctx;
170 struct ifvlantrunk *ifv_trunk;
171 struct ifnet *ifv_ifp;
172 #define TRUNK(ifv) ((ifv)->ifv_trunk)
173 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
175 int ifv_pflags; /* special flags we have set on parent */
177 int ifv_encaplen; /* encapsulation length */
178 int ifv_mtufudge; /* MTU fudged by this much */
179 int ifv_mintu; /* min transmission unit */
180 uint16_t ifv_proto; /* encapsulation ethertype */
181 uint16_t ifv_tag; /* tag to apply on packets leaving if */
182 uint16_t ifv_vid; /* VLAN ID */
183 uint8_t ifv_pcp; /* Priority Code Point (PCP). */
184 struct task lladdr_task;
185 CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
187 CK_SLIST_ENTRY(ifvlan) ifv_list;
191 /* Special flags we should propagate to parent. */
194 int (*func)(struct ifnet *, int);
196 {IFF_PROMISC, ifpromisc},
197 {IFF_ALLMULTI, if_allmulti},
201 extern int vlan_mtag_pcp;
203 static const char vlanname[] = "vlan";
204 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
206 static eventhandler_tag ifdetach_tag;
207 static eventhandler_tag iflladdr_tag;
210 * if_vlan uses two module-level synchronizations primitives to allow concurrent
211 * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
212 * while they are being used for tx/rx. To accomplish this in a way that has
213 * acceptable performance and cooperation with other parts of the network stack
214 * there is a non-sleepable epoch(9) and an sx(9).
216 * The performance-sensitive paths that warrant using the epoch(9) are
217 * vlan_transmit and vlan_input. Both have to check for the vlan interface's
218 * existence using if_vlantrunk, and being in the network tx/rx paths the use
219 * of an epoch(9) gives a measureable improvement in performance.
221 * The reason for having an sx(9) is mostly because there are still areas that
222 * must be sleepable and also have safe concurrent access to a vlan interface.
223 * Since the sx(9) exists, it is used by default in most paths unless sleeping
224 * is not permitted, or if it is not clear whether sleeping is permitted.
227 #define _VLAN_SX_ID ifv_sx
229 static struct sx _VLAN_SX_ID;
231 #define VLAN_LOCKING_INIT() \
232 sx_init(&_VLAN_SX_ID, "vlan_sx")
234 #define VLAN_LOCKING_DESTROY() \
235 sx_destroy(&_VLAN_SX_ID)
237 #define VLAN_RLOCK() NET_EPOCH_ENTER();
238 #define VLAN_RUNLOCK() NET_EPOCH_EXIT();
239 #define VLAN_RLOCK_ASSERT() MPASS(in_epoch(net_epoch_preempt))
241 #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID)
242 #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID)
243 #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID)
244 #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID)
245 #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
246 #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
247 #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED)
251 * We also have a per-trunk mutex that should be acquired when changing
254 #define TRUNK_LOCK_INIT(trunk) mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
255 #define TRUNK_LOCK_DESTROY(trunk) mtx_destroy(&(trunk)->lock)
256 #define TRUNK_RLOCK(trunk) NET_EPOCH_ENTER()
257 #define TRUNK_WLOCK(trunk) mtx_lock(&(trunk)->lock)
258 #define TRUNK_RUNLOCK(trunk) NET_EPOCH_EXIT();
259 #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock)
260 #define TRUNK_RLOCK_ASSERT(trunk) MPASS(in_epoch(net_epoch_preempt))
261 #define TRUNK_LOCK_ASSERT(trunk) MPASS(in_epoch(net_epoch_preempt) || mtx_owned(&(trunk)->lock))
262 #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED);
265 * The VLAN_ARRAY substitutes the dynamic hash with a static array
266 * with 4096 entries. In theory this can give a boost in processing,
267 * however in practice it does not. Probably this is because the array
268 * is too big to fit into CPU cache.
271 static void vlan_inithash(struct ifvlantrunk *trunk);
272 static void vlan_freehash(struct ifvlantrunk *trunk);
273 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
274 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
275 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
276 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
279 static void trunk_destroy(struct ifvlantrunk *trunk);
281 static void vlan_init(void *foo);
282 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
283 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
285 static int vlan_snd_tag_alloc(struct ifnet *,
286 union if_snd_tag_alloc_params *, struct m_snd_tag **);
288 static void vlan_qflush(struct ifnet *ifp);
289 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
290 int (*func)(struct ifnet *, int));
291 static int vlan_setflags(struct ifnet *ifp, int status);
292 static int vlan_setmulti(struct ifnet *ifp);
293 static int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
295 static void vlan_altq_start(struct ifnet *ifp);
296 static int vlan_altq_transmit(struct ifnet *ifp, struct mbuf *m);
298 static int vlan_output(struct ifnet *ifp, struct mbuf *m,
299 const struct sockaddr *dst, struct route *ro);
300 static void vlan_unconfig(struct ifnet *ifp);
301 static void vlan_unconfig_locked(struct ifnet *ifp, int departing);
302 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
303 static void vlan_link_state(struct ifnet *ifp);
304 static void vlan_capabilities(struct ifvlan *ifv);
305 static void vlan_trunk_capabilities(struct ifnet *ifp);
307 static struct ifnet *vlan_clone_match_ethervid(const char *, int *);
308 static int vlan_clone_match(struct if_clone *, const char *);
309 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
310 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
312 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
313 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
315 static void vlan_lladdr_fn(void *arg, int pending);
317 static struct if_clone *vlan_cloner;
320 VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
321 #define V_vlan_cloner VNET(vlan_cloner)
325 vlan_mc_free(struct epoch_context *ctx)
327 struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
332 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
335 vlan_inithash(struct ifvlantrunk *trunk)
340 * The trunk must not be locked here since we call malloc(M_WAITOK).
341 * It is OK in case this function is called before the trunk struct
342 * gets hooked up and becomes visible from other threads.
345 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
346 ("%s: hash already initialized", __func__));
348 trunk->hwidth = VLAN_DEF_HWIDTH;
349 n = 1 << trunk->hwidth;
350 trunk->hmask = n - 1;
351 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
352 for (i = 0; i < n; i++)
353 CK_SLIST_INIT(&trunk->hash[i]);
357 vlan_freehash(struct ifvlantrunk *trunk)
362 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
363 for (i = 0; i < (1 << trunk->hwidth); i++)
364 KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
365 ("%s: hash table not empty", __func__));
367 free(trunk->hash, M_VLAN);
369 trunk->hwidth = trunk->hmask = 0;
373 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
379 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
381 b = 1 << trunk->hwidth;
382 i = HASH(ifv->ifv_vid, trunk->hmask);
383 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
384 if (ifv->ifv_vid == ifv2->ifv_vid)
388 * Grow the hash when the number of vlans exceeds half of the number of
389 * hash buckets squared. This will make the average linked-list length
392 if (trunk->refcnt > (b * b) / 2) {
393 vlan_growhash(trunk, 1);
394 i = HASH(ifv->ifv_vid, trunk->hmask);
396 CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
403 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
409 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
411 b = 1 << trunk->hwidth;
412 i = HASH(ifv->ifv_vid, trunk->hmask);
413 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
416 CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
417 if (trunk->refcnt < (b * b) / 2)
418 vlan_growhash(trunk, -1);
422 panic("%s: vlan not found\n", __func__);
423 return (ENOENT); /*NOTREACHED*/
427 * Grow the hash larger or smaller if memory permits.
430 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
433 struct ifvlanhead *hash2;
434 int hwidth2, i, j, n, n2;
437 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
440 /* Harmless yet obvious coding error */
441 printf("%s: howmuch is 0\n", __func__);
445 hwidth2 = trunk->hwidth + howmuch;
446 n = 1 << trunk->hwidth;
448 /* Do not shrink the table below the default */
449 if (hwidth2 < VLAN_DEF_HWIDTH)
452 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
454 printf("%s: out of memory -- hash size not changed\n",
456 return; /* We can live with the old hash table */
458 for (j = 0; j < n2; j++)
459 CK_SLIST_INIT(&hash2[j]);
460 for (i = 0; i < n; i++)
461 while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
462 CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
463 j = HASH(ifv->ifv_vid, n2 - 1);
464 CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
467 free(trunk->hash, M_VLAN);
469 trunk->hwidth = hwidth2;
470 trunk->hmask = n2 - 1;
473 if_printf(trunk->parent,
474 "VLAN hash table resized from %d to %d buckets\n", n, n2);
477 static __inline struct ifvlan *
478 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
482 TRUNK_RLOCK_ASSERT(trunk);
484 CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
485 if (ifv->ifv_vid == vid)
491 /* Debugging code to view the hashtables. */
493 vlan_dumphash(struct ifvlantrunk *trunk)
498 for (i = 0; i < (1 << trunk->hwidth); i++) {
500 CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
501 printf("%s ", ifv->ifv_ifp->if_xname);
508 static __inline struct ifvlan *
509 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
512 return trunk->vlans[vid];
516 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
519 if (trunk->vlans[ifv->ifv_vid] != NULL)
521 trunk->vlans[ifv->ifv_vid] = ifv;
528 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
531 trunk->vlans[ifv->ifv_vid] = NULL;
538 vlan_freehash(struct ifvlantrunk *trunk)
543 vlan_inithash(struct ifvlantrunk *trunk)
547 #endif /* !VLAN_ARRAY */
550 trunk_destroy(struct ifvlantrunk *trunk)
554 vlan_freehash(trunk);
555 trunk->parent->if_vlantrunk = NULL;
556 TRUNK_LOCK_DESTROY(trunk);
557 if_rele(trunk->parent);
562 * Program our multicast filter. What we're actually doing is
563 * programming the multicast filter of the parent. This has the
564 * side effect of causing the parent interface to receive multicast
565 * traffic that it doesn't really want, which ends up being discarded
566 * later by the upper protocol layers. Unfortunately, there's no way
567 * to avoid this: there really is only one physical interface.
570 vlan_setmulti(struct ifnet *ifp)
573 struct ifmultiaddr *ifma;
575 struct vlan_mc_entry *mc;
580 /* Find the parent. */
584 CURVNET_SET_QUIET(ifp_p->if_vnet);
586 /* First, remove any existing filter entries. */
587 while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
588 CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
589 (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
590 epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
593 /* Now program new ones. */
595 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
596 if (ifma->ifma_addr->sa_family != AF_LINK)
598 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
600 IF_ADDR_WUNLOCK(ifp);
603 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
604 mc->mc_addr.sdl_index = ifp_p->if_index;
605 CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
607 IF_ADDR_WUNLOCK(ifp);
608 CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
609 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
620 * A handler for parent interface link layer address changes.
621 * If the parent interface link layer address is changed we
622 * should also change it on all children vlans.
625 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
628 struct ifnet *ifv_ifp;
629 struct ifvlantrunk *trunk;
630 struct sockaddr_dl *sdl;
632 /* Need the rmlock since this is run on taskqueue_swi. */
634 trunk = ifp->if_vlantrunk;
641 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
642 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
643 * ioctl calls on the parent garbling the lladdr of the child vlan.
646 VLAN_FOREACH(ifv, trunk) {
648 * Copy new new lladdr into the ifv_ifp, enqueue a task
649 * to actually call if_setlladdr. if_setlladdr needs to
650 * be deferred to a taskqueue because it will call into
651 * the if_vlan ioctl path and try to acquire the global
654 ifv_ifp = ifv->ifv_ifp;
655 bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
657 sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
658 sdl->sdl_alen = ifp->if_addrlen;
659 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
661 TRUNK_WUNLOCK(trunk);
666 * A handler for network interface departure events.
667 * Track departure of trunks here so that we don't access invalid
668 * pointers or whatever if a trunk is ripped from under us, e.g.,
669 * by ejecting its hot-plug card. However, if an ifnet is simply
670 * being renamed, then there's no need to tear down the state.
673 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
676 struct ifvlantrunk *trunk;
678 /* If the ifnet is just being renamed, don't do anything. */
679 if (ifp->if_flags & IFF_RENAMING)
682 trunk = ifp->if_vlantrunk;
689 * OK, it's a trunk. Loop over and detach all vlan's on it.
690 * Check trunk pointer after each vlan_unconfig() as it will
691 * free it and set to NULL after the last vlan was detached.
693 VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
694 ifp->if_vlantrunk == NULL)
695 vlan_unconfig_locked(ifv->ifv_ifp, 1);
697 /* Trunk should have been destroyed in vlan_unconfig(). */
698 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
703 * Return the trunk device for a virtual interface.
705 static struct ifnet *
706 vlan_trunkdev(struct ifnet *ifp)
710 if (ifp->if_type != IFT_L2VLAN)
723 * Return the 12-bit VLAN VID for this interface, for use by external
724 * components such as Infiniband.
726 * XXXRW: Note that the function name here is historical; it should be named
730 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
734 if (ifp->if_type != IFT_L2VLAN)
737 *vidp = ifv->ifv_vid;
742 vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
746 if (ifp->if_type != IFT_L2VLAN)
749 *pcpp = ifv->ifv_pcp;
754 * Return a driver specific cookie for this interface. Synchronization
755 * with setcookie must be provided by the driver.
758 vlan_cookie(struct ifnet *ifp)
762 if (ifp->if_type != IFT_L2VLAN)
765 return (ifv->ifv_cookie);
769 * Store a cookie in our softc that drivers can use to store driver
770 * private per-instance data in.
773 vlan_setcookie(struct ifnet *ifp, void *cookie)
777 if (ifp->if_type != IFT_L2VLAN)
780 ifv->ifv_cookie = cookie;
785 * Return the vlan device present at the specific VID.
787 static struct ifnet *
788 vlan_devat(struct ifnet *ifp, uint16_t vid)
790 struct ifvlantrunk *trunk;
794 trunk = ifp->if_vlantrunk;
800 ifv = vlan_gethash(trunk, vid);
808 * Recalculate the cached VLAN tag exposed via the MIB.
811 vlan_tag_recalculate(struct ifvlan *ifv)
814 ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
818 * VLAN support can be loaded as a module. The only place in the
819 * system that's intimately aware of this is ether_input. We hook
820 * into this code through vlan_input_p which is defined there and
821 * set here. No one else in the system should be aware of this so
822 * we use an explicit reference here.
824 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
826 /* For if_link_state_change() eyes only... */
827 extern void (*vlan_link_state_p)(struct ifnet *);
830 vlan_modevent(module_t mod, int type, void *data)
835 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
836 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
837 if (ifdetach_tag == NULL)
839 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
840 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
841 if (iflladdr_tag == NULL)
844 vlan_input_p = vlan_input;
845 vlan_link_state_p = vlan_link_state;
846 vlan_trunk_cap_p = vlan_trunk_capabilities;
847 vlan_trunkdev_p = vlan_trunkdev;
848 vlan_cookie_p = vlan_cookie;
849 vlan_setcookie_p = vlan_setcookie;
850 vlan_tag_p = vlan_tag;
851 vlan_pcp_p = vlan_pcp;
852 vlan_devat_p = vlan_devat;
854 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
855 vlan_clone_create, vlan_clone_destroy);
858 printf("vlan: initialized, using "
862 "hash tables with chaining"
869 if_clone_detach(vlan_cloner);
871 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
872 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
874 vlan_link_state_p = NULL;
875 vlan_trunk_cap_p = NULL;
876 vlan_trunkdev_p = NULL;
878 vlan_cookie_p = NULL;
879 vlan_setcookie_p = NULL;
881 VLAN_LOCKING_DESTROY();
883 printf("vlan: unloaded\n");
891 static moduledata_t vlan_mod = {
897 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
898 MODULE_VERSION(if_vlan, 3);
902 vnet_vlan_init(const void *unused __unused)
905 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
906 vlan_clone_create, vlan_clone_destroy);
907 V_vlan_cloner = vlan_cloner;
909 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
910 vnet_vlan_init, NULL);
913 vnet_vlan_uninit(const void *unused __unused)
916 if_clone_detach(V_vlan_cloner);
918 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
919 vnet_vlan_uninit, NULL);
923 * Check for <etherif>.<vlan> style interface names.
925 static struct ifnet *
926 vlan_clone_match_ethervid(const char *name, int *vidp)
928 char ifname[IFNAMSIZ];
933 strlcpy(ifname, name, IFNAMSIZ);
934 if ((cp = strchr(ifname, '.')) == NULL)
937 if ((ifp = ifunit_ref(ifname)) == NULL)
945 for(; *cp >= '0' && *cp <= '9'; cp++)
946 vid = (vid * 10) + (*cp - '0');
958 vlan_clone_match(struct if_clone *ifc, const char *name)
962 if (vlan_clone_match_ethervid(name, NULL) != NULL)
965 if (strncmp(vlanname, name, strlen(vlanname)) != 0)
967 for (cp = name + 4; *cp != '\0'; cp++) {
968 if (*cp < '0' || *cp > '9')
976 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
987 struct sockaddr_dl *sdl;
989 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
992 * There are 3 (ugh) ways to specify the cloned device:
993 * o pass a parameter block with the clone request.
994 * o specify parameters in the text of the clone device name
995 * o specify no parameters and get an unattached device that
996 * must be configured separately.
997 * The first technique is preferred; the latter two are
998 * supported for backwards compatibility.
1000 * XXXRW: Note historic use of the word "tag" here. New ioctls may be
1004 error = copyin(params, &vlr, sizeof(vlr));
1007 p = ifunit_ref(vlr.vlr_parent);
1010 error = ifc_name2unit(name, &unit);
1016 wildcard = (unit < 0);
1017 } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1022 error = ifc_name2unit(name, &unit);
1026 wildcard = (unit < 0);
1029 error = ifc_alloc_unit(ifc, &unit);
1036 /* In the wildcard case, we need to update the name. */
1038 for (dp = name; *dp != '\0'; dp++);
1039 if (snprintf(dp, len - (dp-name), "%d", unit) >
1040 len - (dp-name) - 1) {
1041 panic("%s: interface name too long", __func__);
1045 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1046 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1048 ifc_free_unit(ifc, unit);
1054 CK_SLIST_INIT(&ifv->vlan_mc_listhead);
1055 ifp->if_softc = ifv;
1057 * Set the name manually rather than using if_initname because
1058 * we don't conform to the default naming convention for interfaces.
1060 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1061 ifp->if_dname = vlanname;
1062 ifp->if_dunit = unit;
1064 ifp->if_init = vlan_init;
1066 ifp->if_start = vlan_altq_start;
1067 ifp->if_transmit = vlan_altq_transmit;
1068 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1069 ifp->if_snd.ifq_drv_maxlen = 0;
1070 IFQ_SET_READY(&ifp->if_snd);
1072 ifp->if_transmit = vlan_transmit;
1074 ifp->if_qflush = vlan_qflush;
1075 ifp->if_ioctl = vlan_ioctl;
1077 ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1079 ifp->if_flags = VLAN_IFFLAGS;
1080 ether_ifattach(ifp, eaddr);
1081 /* Now undo some of the damage... */
1082 ifp->if_baudrate = 0;
1083 ifp->if_type = IFT_L2VLAN;
1084 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1086 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1087 sdl->sdl_type = IFT_L2VLAN;
1090 error = vlan_config(ifv, p, vid);
1094 * Since we've partially failed, we need to back
1095 * out all the way, otherwise userland could get
1096 * confused. Thus, we destroy the interface.
1098 ether_ifdetach(ifp);
1101 ifc_free_unit(ifc, unit);
1112 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
1114 struct ifvlan *ifv = ifp->if_softc;
1115 int unit = ifp->if_dunit;
1119 IFQ_PURGE(&ifp->if_snd);
1121 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
1122 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
1124 * We should have the only reference to the ifv now, so we can now
1125 * drain any remaining lladdr task before freeing the ifnet and the
1128 taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1132 ifc_free_unit(ifc, unit);
1138 * The ifp->if_init entry point for vlan(4) is a no-op.
1141 vlan_init(void *foo __unused)
1146 * The if_transmit method for vlan(4) interface.
1149 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1153 int error, len, mcast;
1156 ifv = ifp->if_softc;
1157 if (TRUNK(ifv) == NULL) {
1158 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1164 len = m->m_pkthdr.len;
1165 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1170 * Do not run parent's if_transmit() if the parent is not up,
1171 * or parent's driver will cause a system crash.
1173 if (!UP_AND_RUNNING(p)) {
1174 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1180 if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1181 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1187 * Send it, precisely as ether_output() would have.
1189 error = (p->if_transmit)(p, m);
1191 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1192 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1193 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1195 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1201 vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1208 ifv = ifp->if_softc;
1209 if (TRUNK(ifv) == NULL) {
1216 return p->if_output(ifp, m, dst, ro);
1221 vlan_altq_start(if_t ifp)
1223 struct ifaltq *ifq = &ifp->if_snd;
1227 IFQ_DEQUEUE_NOLOCK(ifq, m);
1229 vlan_transmit(ifp, m);
1230 IFQ_DEQUEUE_NOLOCK(ifq, m);
1236 vlan_altq_transmit(if_t ifp, struct mbuf *m)
1240 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
1241 IFQ_ENQUEUE(&ifp->if_snd, m, err);
1243 vlan_altq_start(ifp);
1245 err = vlan_transmit(ifp, m);
1252 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1255 vlan_qflush(struct ifnet *ifp __unused)
1260 vlan_input(struct ifnet *ifp, struct mbuf *m)
1262 struct ifvlantrunk *trunk;
1268 trunk = ifp->if_vlantrunk;
1269 if (trunk == NULL) {
1275 if (m->m_flags & M_VLANTAG) {
1277 * Packet is tagged, but m contains a normal
1278 * Ethernet frame; the tag is stored out-of-band.
1280 tag = m->m_pkthdr.ether_vtag;
1281 m->m_flags &= ~M_VLANTAG;
1283 struct ether_vlan_header *evl;
1286 * Packet is tagged in-band as specified by 802.1q.
1288 switch (ifp->if_type) {
1290 if (m->m_len < sizeof(*evl) &&
1291 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1292 if_printf(ifp, "cannot pullup VLAN header\n");
1296 evl = mtod(m, struct ether_vlan_header *);
1297 tag = ntohs(evl->evl_tag);
1300 * Remove the 802.1q header by copying the Ethernet
1301 * addresses over it and adjusting the beginning of
1302 * the data in the mbuf. The encapsulated Ethernet
1303 * type field is already in place.
1305 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1306 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1307 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1312 panic("%s: %s has unsupported if_type %u",
1313 __func__, ifp->if_xname, ifp->if_type);
1315 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1322 vid = EVL_VLANOFTAG(tag);
1324 ifv = vlan_gethash(trunk, vid);
1325 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1327 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1332 if (vlan_mtag_pcp) {
1334 * While uncommon, it is possible that we will find a 802.1q
1335 * packet encapsulated inside another packet that also had an
1336 * 802.1q header. For example, ethernet tunneled over IPSEC
1337 * arriving over ethernet. In that case, we replace the
1338 * existing 802.1q PCP m_tag value.
1340 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1342 mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1343 sizeof(uint8_t), M_NOWAIT);
1345 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1350 m_tag_prepend(m, mtag);
1352 *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1355 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1356 if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1359 /* Pass it back through the parent's input routine. */
1360 (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1364 vlan_lladdr_fn(void *arg, int pending __unused)
1369 ifv = (struct ifvlan *)arg;
1372 CURVNET_SET(ifp->if_vnet);
1374 /* The ifv_ifp already has the lladdr copied in. */
1375 if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1381 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
1383 struct ifvlantrunk *trunk;
1388 * We can handle non-ethernet hardware types as long as
1389 * they handle the tagging and headers themselves.
1391 if (p->if_type != IFT_ETHER &&
1392 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1393 return (EPROTONOSUPPORT);
1394 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1395 return (EPROTONOSUPPORT);
1397 * Don't let the caller set up a VLAN VID with
1398 * anything except VLID bits.
1399 * VID numbers 0x0 and 0xFFF are reserved.
1401 if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1407 if (p->if_vlantrunk == NULL) {
1408 trunk = malloc(sizeof(struct ifvlantrunk),
1409 M_VLAN, M_WAITOK | M_ZERO);
1410 vlan_inithash(trunk);
1411 TRUNK_LOCK_INIT(trunk);
1413 p->if_vlantrunk = trunk;
1415 if_ref(trunk->parent);
1416 TRUNK_WUNLOCK(trunk);
1418 trunk = p->if_vlantrunk;
1421 ifv->ifv_vid = vid; /* must set this before vlan_inshash() */
1422 ifv->ifv_pcp = 0; /* Default: best effort delivery. */
1423 vlan_tag_recalculate(ifv);
1424 error = vlan_inshash(trunk, ifv);
1427 ifv->ifv_proto = ETHERTYPE_VLAN;
1428 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1429 ifv->ifv_mintu = ETHERMIN;
1430 ifv->ifv_pflags = 0;
1431 ifv->ifv_capenable = -1;
1434 * If the parent supports the VLAN_MTU capability,
1435 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1438 if (p->if_capenable & IFCAP_VLAN_MTU) {
1440 * No need to fudge the MTU since the parent can
1441 * handle extended frames.
1443 ifv->ifv_mtufudge = 0;
1446 * Fudge the MTU by the encapsulation size. This
1447 * makes us incompatible with strictly compliant
1448 * 802.1Q implementations, but allows us to use
1449 * the feature with other NetBSD implementations,
1450 * which might still be useful.
1452 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1455 ifv->ifv_trunk = trunk;
1458 * Initialize fields from our parent. This duplicates some
1459 * work with ether_ifattach() but allows for non-ethernet
1460 * interfaces to also work.
1462 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1463 ifp->if_baudrate = p->if_baudrate;
1464 ifp->if_input = p->if_input;
1465 ifp->if_resolvemulti = p->if_resolvemulti;
1466 ifp->if_addrlen = p->if_addrlen;
1467 ifp->if_broadcastaddr = p->if_broadcastaddr;
1468 ifp->if_pcp = ifv->ifv_pcp;
1471 * We wrap the parent's if_output using vlan_output to ensure that it
1472 * can't become stale.
1474 ifp->if_output = vlan_output;
1477 * Copy only a selected subset of flags from the parent.
1478 * Other flags are none of our business.
1480 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1481 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1482 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1483 #undef VLAN_COPY_FLAGS
1485 ifp->if_link_state = p->if_link_state;
1487 TRUNK_RLOCK(TRUNK(ifv));
1488 vlan_capabilities(ifv);
1489 TRUNK_RUNLOCK(TRUNK(ifv));
1492 * Set up our interface address to reflect the underlying
1493 * physical interface's.
1495 TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1496 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1500 * Do not schedule link address update if it was the same
1501 * as previous parent's. This helps avoid updating for each
1502 * associated llentry.
1504 if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) {
1505 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1506 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
1509 /* We are ready for operation now. */
1510 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1512 /* Update flags on the parent, if necessary. */
1513 vlan_setflags(ifp, 1);
1516 * Configure multicast addresses that may already be
1517 * joined on the vlan device.
1519 (void)vlan_setmulti(ifp);
1523 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1530 vlan_unconfig(struct ifnet *ifp)
1534 vlan_unconfig_locked(ifp, 0);
1539 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1541 struct ifvlantrunk *trunk;
1542 struct vlan_mc_entry *mc;
1544 struct ifnet *parent;
1547 VLAN_XLOCK_ASSERT();
1549 ifv = ifp->if_softc;
1550 trunk = ifv->ifv_trunk;
1553 if (trunk != NULL) {
1554 parent = trunk->parent;
1557 * Since the interface is being unconfigured, we need to
1558 * empty the list of multicast groups that we may have joined
1559 * while we were alive from the parent's list.
1561 while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1563 * If the parent interface is being detached,
1564 * all its multicast addresses have already
1565 * been removed. Warn about errors if
1566 * if_delmulti() does fail, but don't abort as
1567 * all callers expect vlan destruction to
1571 error = if_delmulti(parent,
1572 (struct sockaddr *)&mc->mc_addr);
1575 "Failed to delete multicast address from parent: %d\n",
1578 CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1579 epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
1582 vlan_setflags(ifp, 0); /* clear special flags on parent */
1584 vlan_remhash(trunk, ifv);
1585 ifv->ifv_trunk = NULL;
1588 * Check if we were the last.
1590 if (trunk->refcnt == 0) {
1591 parent->if_vlantrunk = NULL;
1593 trunk_destroy(trunk);
1597 /* Disconnect from parent. */
1598 if (ifv->ifv_pflags)
1599 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1600 ifp->if_mtu = ETHERMTU;
1601 ifp->if_link_state = LINK_STATE_UNKNOWN;
1602 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1605 * Only dispatch an event if vlan was
1606 * attached, otherwise there is nothing
1607 * to cleanup anyway.
1610 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1613 /* Handle a reference counted flag that should be set on the parent as well */
1615 vlan_setflag(struct ifnet *ifp, int flag, int status,
1616 int (*func)(struct ifnet *, int))
1621 VLAN_SXLOCK_ASSERT();
1623 ifv = ifp->if_softc;
1624 status = status ? (ifp->if_flags & flag) : 0;
1625 /* Now "status" contains the flag value or 0 */
1628 * See if recorded parent's status is different from what
1629 * we want it to be. If it is, flip it. We record parent's
1630 * status in ifv_pflags so that we won't clear parent's flag
1631 * we haven't set. In fact, we don't clear or set parent's
1632 * flags directly, but get or release references to them.
1633 * That's why we can be sure that recorded flags still are
1634 * in accord with actual parent's flags.
1636 if (status != (ifv->ifv_pflags & flag)) {
1637 error = (*func)(PARENT(ifv), status);
1640 ifv->ifv_pflags &= ~flag;
1641 ifv->ifv_pflags |= status;
1647 * Handle IFF_* flags that require certain changes on the parent:
1648 * if "status" is true, update parent's flags respective to our if_flags;
1649 * if "status" is false, forcedly clear the flags set on parent.
1652 vlan_setflags(struct ifnet *ifp, int status)
1656 for (i = 0; vlan_pflags[i].flag; i++) {
1657 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1658 status, vlan_pflags[i].func);
1665 /* Inform all vlans that their parent has changed link state */
1667 vlan_link_state(struct ifnet *ifp)
1669 struct ifvlantrunk *trunk;
1672 /* Called from a taskqueue_swi task, so we cannot sleep. */
1674 trunk = ifp->if_vlantrunk;
1675 if (trunk == NULL) {
1681 VLAN_FOREACH(ifv, trunk) {
1682 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1683 if_link_state_change(ifv->ifv_ifp,
1684 trunk->parent->if_link_state);
1686 TRUNK_WUNLOCK(trunk);
1691 vlan_capabilities(struct ifvlan *ifv)
1695 struct ifnet_hw_tsomax hw_tsomax;
1696 int cap = 0, ena = 0, mena;
1699 VLAN_SXLOCK_ASSERT();
1700 TRUNK_RLOCK_ASSERT(TRUNK(ifv));
1704 /* Mask parent interface enabled capabilities disabled by user. */
1705 mena = p->if_capenable & ifv->ifv_capenable;
1708 * If the parent interface can do checksum offloading
1709 * on VLANs, then propagate its hardware-assisted
1710 * checksumming flags. Also assert that checksum
1711 * offloading requires hardware VLAN tagging.
1713 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1714 cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1715 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1716 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1717 ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1718 if (ena & IFCAP_TXCSUM)
1719 hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1720 CSUM_UDP | CSUM_SCTP);
1721 if (ena & IFCAP_TXCSUM_IPV6)
1722 hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1723 CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
1727 * If the parent interface can do TSO on VLANs then
1728 * propagate the hardware-assisted flag. TSO on VLANs
1729 * does not necessarily require hardware VLAN tagging.
1731 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
1732 if_hw_tsomax_common(p, &hw_tsomax);
1733 if_hw_tsomax_update(ifp, &hw_tsomax);
1734 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1735 cap |= p->if_capabilities & IFCAP_TSO;
1736 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1737 ena |= mena & IFCAP_TSO;
1738 if (ena & IFCAP_TSO)
1739 hwa |= p->if_hwassist & CSUM_TSO;
1743 * If the parent interface can do LRO and checksum offloading on
1744 * VLANs, then guess it may do LRO on VLANs. False positive here
1745 * cost nothing, while false negative may lead to some confusions.
1747 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1748 cap |= p->if_capabilities & IFCAP_LRO;
1749 if (p->if_capenable & IFCAP_VLAN_HWCSUM)
1750 ena |= p->if_capenable & IFCAP_LRO;
1753 * If the parent interface can offload TCP connections over VLANs then
1754 * propagate its TOE capability to the VLAN interface.
1756 * All TOE drivers in the tree today can deal with VLANs. If this
1757 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1760 #define IFCAP_VLAN_TOE IFCAP_TOE
1761 if (p->if_capabilities & IFCAP_VLAN_TOE)
1762 cap |= p->if_capabilities & IFCAP_TOE;
1763 if (p->if_capenable & IFCAP_VLAN_TOE) {
1764 TOEDEV(ifp) = TOEDEV(p);
1765 ena |= mena & IFCAP_TOE;
1769 * If the parent interface supports dynamic link state, so does the
1772 cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1773 ena |= (mena & IFCAP_LINKSTATE);
1777 * If the parent interface supports ratelimiting, so does the
1780 cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1781 ena |= (mena & IFCAP_TXRTLMT);
1784 ifp->if_capabilities = cap;
1785 ifp->if_capenable = ena;
1786 ifp->if_hwassist = hwa;
1790 vlan_trunk_capabilities(struct ifnet *ifp)
1792 struct ifvlantrunk *trunk;
1796 trunk = ifp->if_vlantrunk;
1797 if (trunk == NULL) {
1802 VLAN_FOREACH(ifv, trunk) {
1803 vlan_capabilities(ifv);
1805 TRUNK_RUNLOCK(trunk);
1810 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1816 struct ifvlantrunk *trunk;
1818 int error = 0, oldmtu;
1820 ifr = (struct ifreq *)data;
1821 ifa = (struct ifaddr *) data;
1822 ifv = ifp->if_softc;
1826 ifp->if_flags |= IFF_UP;
1828 if (ifa->ifa_addr->sa_family == AF_INET)
1829 arp_ifinit(ifp, ifa);
1833 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
1838 if (TRUNK(ifv) != NULL) {
1841 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1843 /* Limit the result to the parent's current config. */
1845 struct ifmediareq *ifmr;
1847 ifmr = (struct ifmediareq *)data;
1848 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1849 ifmr->ifm_count = 1;
1850 error = copyout(&ifmr->ifm_current,
1867 * Set the interface MTU.
1871 if (trunk != NULL) {
1874 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1876 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1879 ifp->if_mtu = ifr->ifr_mtu;
1880 TRUNK_WUNLOCK(trunk);
1889 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
1890 * interface to be delegated to a jail without allowing the
1891 * jail to change what underlying interface/VID it is
1892 * associated with. We are not entirely convinced that this
1893 * is the right way to accomplish that policy goal.
1895 if (ifp->if_vnet != ifp->if_home_vnet) {
1900 error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
1903 if (vlr.vlr_parent[0] == '\0') {
1907 p = ifunit_ref(vlr.vlr_parent);
1912 oldmtu = ifp->if_mtu;
1913 error = vlan_config(ifv, p, vlr.vlr_tag);
1917 * VLAN MTU may change during addition of the vlandev.
1918 * If it did, do network layer specific procedure.
1920 if (ifp->if_mtu != oldmtu) {
1930 if (ifp->if_vnet != ifp->if_home_vnet) {
1935 bzero(&vlr, sizeof(vlr));
1937 if (TRUNK(ifv) != NULL) {
1938 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1939 sizeof(vlr.vlr_parent));
1940 vlr.vlr_tag = ifv->ifv_vid;
1943 error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
1948 * We should propagate selected flags to the parent,
1949 * e.g., promiscuous mode.
1952 if (TRUNK(ifv) != NULL)
1953 error = vlan_setflags(ifp, 1);
1960 * If we don't have a parent, just remember the membership for
1963 * XXX We need the rmlock here to avoid sleeping while
1964 * holding in6_multi_mtx.
1969 error = vlan_setmulti(ifp);
1975 if (ifp->if_vnet != ifp->if_home_vnet) {
1980 ifr->ifr_vlan_pcp = ifv->ifv_pcp;
1985 if (ifp->if_vnet != ifp->if_home_vnet) {
1990 error = priv_check(curthread, PRIV_NET_SETVLANPCP);
1993 if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) {
1997 ifv->ifv_pcp = ifr->ifr_vlan_pcp;
1998 ifp->if_pcp = ifv->ifv_pcp;
1999 vlan_tag_recalculate(ifv);
2000 /* broadcast event about PCP change */
2001 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
2006 ifv->ifv_capenable = ifr->ifr_reqcap;
2008 if (trunk != NULL) {
2010 vlan_capabilities(ifv);
2011 TRUNK_RUNLOCK(trunk);
2026 vlan_snd_tag_alloc(struct ifnet *ifp,
2027 union if_snd_tag_alloc_params *params,
2028 struct m_snd_tag **ppmt)
2031 /* get trunk device */
2032 ifp = vlan_trunkdev(ifp);
2033 if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
2034 return (EOPNOTSUPP);
2035 /* forward allocation request */
2036 return (ifp->if_snd_tag_alloc(ifp, params, ppmt));