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 CK_SLIST_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 struct vlan_snd_tag {
108 struct m_snd_tag com;
109 struct m_snd_tag *tag;
112 static inline struct vlan_snd_tag *
113 mst_to_vst(struct m_snd_tag *mst)
116 return (__containerof(mst, struct vlan_snd_tag, com));
121 * This macro provides a facility to iterate over every vlan on a trunk with
122 * the assumption that none will be added/removed during iteration.
125 #define VLAN_FOREACH(_ifv, _trunk) \
127 for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
128 if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
129 #else /* VLAN_ARRAY */
130 #define VLAN_FOREACH(_ifv, _trunk) \
131 struct ifvlan *_next; \
133 for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
134 CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
135 #endif /* VLAN_ARRAY */
138 * This macro provides a facility to iterate over every vlan on a trunk while
139 * also modifying the number of vlans on the trunk. The iteration continues
140 * until some condition is met or there are no more vlans on the trunk.
143 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
144 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
146 for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
147 if (((_ifv) = (_trunk)->vlans[_i]))
148 #else /* VLAN_ARRAY */
150 * The hash table case is more complicated. We allow for the hash table to be
151 * modified (i.e. vlans removed) while we are iterating over it. To allow for
152 * this we must restart the iteration every time we "touch" something during
153 * the iteration, since removal will resize the hash table and invalidate our
154 * current position. If acting on the touched element causes the trunk to be
155 * emptied, then iteration also stops.
157 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
159 bool _touch = false; \
161 !(_cond) && _i < (1 << (_trunk)->hwidth); \
162 _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
163 if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
165 #endif /* VLAN_ARRAY */
167 struct vlan_mc_entry {
168 struct sockaddr_dl mc_addr;
169 CK_SLIST_ENTRY(vlan_mc_entry) mc_entries;
170 struct epoch_context mc_epoch_ctx;
174 struct ifvlantrunk *ifv_trunk;
175 struct ifnet *ifv_ifp;
176 #define TRUNK(ifv) ((ifv)->ifv_trunk)
177 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
179 int ifv_pflags; /* special flags we have set on parent */
181 int ifv_encaplen; /* encapsulation length */
182 int ifv_mtufudge; /* MTU fudged by this much */
183 int ifv_mintu; /* min transmission unit */
184 uint16_t ifv_proto; /* encapsulation ethertype */
185 uint16_t ifv_tag; /* tag to apply on packets leaving if */
186 uint16_t ifv_vid; /* VLAN ID */
187 uint8_t ifv_pcp; /* Priority Code Point (PCP). */
188 struct task lladdr_task;
189 CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
191 CK_SLIST_ENTRY(ifvlan) ifv_list;
195 /* Special flags we should propagate to parent. */
198 int (*func)(struct ifnet *, int);
200 {IFF_PROMISC, ifpromisc},
201 {IFF_ALLMULTI, if_allmulti},
205 extern int vlan_mtag_pcp;
207 static const char vlanname[] = "vlan";
208 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
210 static eventhandler_tag ifdetach_tag;
211 static eventhandler_tag iflladdr_tag;
214 * if_vlan uses two module-level synchronizations primitives to allow concurrent
215 * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
216 * while they are being used for tx/rx. To accomplish this in a way that has
217 * acceptable performance and cooperation with other parts of the network stack
218 * there is a non-sleepable epoch(9) and an sx(9).
220 * The performance-sensitive paths that warrant using the epoch(9) are
221 * vlan_transmit and vlan_input. Both have to check for the vlan interface's
222 * existence using if_vlantrunk, and being in the network tx/rx paths the use
223 * of an epoch(9) gives a measureable improvement in performance.
225 * The reason for having an sx(9) is mostly because there are still areas that
226 * must be sleepable and also have safe concurrent access to a vlan interface.
227 * Since the sx(9) exists, it is used by default in most paths unless sleeping
228 * is not permitted, or if it is not clear whether sleeping is permitted.
231 #define _VLAN_SX_ID ifv_sx
233 static struct sx _VLAN_SX_ID;
235 #define VLAN_LOCKING_INIT() \
236 sx_init(&_VLAN_SX_ID, "vlan_sx")
238 #define VLAN_LOCKING_DESTROY() \
239 sx_destroy(&_VLAN_SX_ID)
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_WLOCK(trunk) mtx_lock(&(trunk)->lock)
257 #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock)
258 #define TRUNK_LOCK_ASSERT(trunk) MPASS(in_epoch(net_epoch_preempt) || mtx_owned(&(trunk)->lock))
259 #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED);
262 * The VLAN_ARRAY substitutes the dynamic hash with a static array
263 * with 4096 entries. In theory this can give a boost in processing,
264 * however in practice it does not. Probably this is because the array
265 * is too big to fit into CPU cache.
268 static void vlan_inithash(struct ifvlantrunk *trunk);
269 static void vlan_freehash(struct ifvlantrunk *trunk);
270 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
271 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
272 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
273 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
276 static void trunk_destroy(struct ifvlantrunk *trunk);
278 static void vlan_init(void *foo);
279 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
280 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
282 static int vlan_snd_tag_alloc(struct ifnet *,
283 union if_snd_tag_alloc_params *, struct m_snd_tag **);
284 static int vlan_snd_tag_modify(struct m_snd_tag *,
285 union if_snd_tag_modify_params *);
286 static int vlan_snd_tag_query(struct m_snd_tag *,
287 union if_snd_tag_query_params *);
288 static void vlan_snd_tag_free(struct m_snd_tag *);
290 static void vlan_qflush(struct ifnet *ifp);
291 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
292 int (*func)(struct ifnet *, int));
293 static int vlan_setflags(struct ifnet *ifp, int status);
294 static int vlan_setmulti(struct ifnet *ifp);
295 static int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
296 static void vlan_unconfig(struct ifnet *ifp);
297 static void vlan_unconfig_locked(struct ifnet *ifp, int departing);
298 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
299 static void vlan_link_state(struct ifnet *ifp);
300 static void vlan_capabilities(struct ifvlan *ifv);
301 static void vlan_trunk_capabilities(struct ifnet *ifp);
303 static struct ifnet *vlan_clone_match_ethervid(const char *, int *);
304 static int vlan_clone_match(struct if_clone *, const char *);
305 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
306 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
308 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
309 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
311 static void vlan_lladdr_fn(void *arg, int pending);
313 static struct if_clone *vlan_cloner;
316 VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
317 #define V_vlan_cloner VNET(vlan_cloner)
321 vlan_mc_free(struct epoch_context *ctx)
323 struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
328 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
331 vlan_inithash(struct ifvlantrunk *trunk)
336 * The trunk must not be locked here since we call malloc(M_WAITOK).
337 * It is OK in case this function is called before the trunk struct
338 * gets hooked up and becomes visible from other threads.
341 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
342 ("%s: hash already initialized", __func__));
344 trunk->hwidth = VLAN_DEF_HWIDTH;
345 n = 1 << trunk->hwidth;
346 trunk->hmask = n - 1;
347 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
348 for (i = 0; i < n; i++)
349 CK_SLIST_INIT(&trunk->hash[i]);
353 vlan_freehash(struct ifvlantrunk *trunk)
358 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
359 for (i = 0; i < (1 << trunk->hwidth); i++)
360 KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
361 ("%s: hash table not empty", __func__));
363 free(trunk->hash, M_VLAN);
365 trunk->hwidth = trunk->hmask = 0;
369 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
375 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
377 b = 1 << trunk->hwidth;
378 i = HASH(ifv->ifv_vid, trunk->hmask);
379 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
380 if (ifv->ifv_vid == ifv2->ifv_vid)
384 * Grow the hash when the number of vlans exceeds half of the number of
385 * hash buckets squared. This will make the average linked-list length
388 if (trunk->refcnt > (b * b) / 2) {
389 vlan_growhash(trunk, 1);
390 i = HASH(ifv->ifv_vid, trunk->hmask);
392 CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
399 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
405 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
407 b = 1 << trunk->hwidth;
408 i = HASH(ifv->ifv_vid, trunk->hmask);
409 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
412 CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
413 if (trunk->refcnt < (b * b) / 2)
414 vlan_growhash(trunk, -1);
418 panic("%s: vlan not found\n", __func__);
419 return (ENOENT); /*NOTREACHED*/
423 * Grow the hash larger or smaller if memory permits.
426 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
429 struct ifvlanhead *hash2;
430 int hwidth2, i, j, n, n2;
433 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
436 /* Harmless yet obvious coding error */
437 printf("%s: howmuch is 0\n", __func__);
441 hwidth2 = trunk->hwidth + howmuch;
442 n = 1 << trunk->hwidth;
444 /* Do not shrink the table below the default */
445 if (hwidth2 < VLAN_DEF_HWIDTH)
448 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
450 printf("%s: out of memory -- hash size not changed\n",
452 return; /* We can live with the old hash table */
454 for (j = 0; j < n2; j++)
455 CK_SLIST_INIT(&hash2[j]);
456 for (i = 0; i < n; i++)
457 while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
458 CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
459 j = HASH(ifv->ifv_vid, n2 - 1);
460 CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
463 free(trunk->hash, M_VLAN);
465 trunk->hwidth = hwidth2;
466 trunk->hmask = n2 - 1;
469 if_printf(trunk->parent,
470 "VLAN hash table resized from %d to %d buckets\n", n, n2);
473 static __inline struct ifvlan *
474 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
480 CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
481 if (ifv->ifv_vid == vid)
487 /* Debugging code to view the hashtables. */
489 vlan_dumphash(struct ifvlantrunk *trunk)
494 for (i = 0; i < (1 << trunk->hwidth); i++) {
496 CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
497 printf("%s ", ifv->ifv_ifp->if_xname);
504 static __inline struct ifvlan *
505 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
508 return trunk->vlans[vid];
512 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
515 if (trunk->vlans[ifv->ifv_vid] != NULL)
517 trunk->vlans[ifv->ifv_vid] = ifv;
524 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
527 trunk->vlans[ifv->ifv_vid] = NULL;
534 vlan_freehash(struct ifvlantrunk *trunk)
539 vlan_inithash(struct ifvlantrunk *trunk)
543 #endif /* !VLAN_ARRAY */
546 trunk_destroy(struct ifvlantrunk *trunk)
550 vlan_freehash(trunk);
551 trunk->parent->if_vlantrunk = NULL;
552 TRUNK_LOCK_DESTROY(trunk);
553 if_rele(trunk->parent);
558 * Program our multicast filter. What we're actually doing is
559 * programming the multicast filter of the parent. This has the
560 * side effect of causing the parent interface to receive multicast
561 * traffic that it doesn't really want, which ends up being discarded
562 * later by the upper protocol layers. Unfortunately, there's no way
563 * to avoid this: there really is only one physical interface.
566 vlan_setmulti(struct ifnet *ifp)
569 struct ifmultiaddr *ifma;
571 struct vlan_mc_entry *mc;
576 /* Find the parent. */
580 CURVNET_SET_QUIET(ifp_p->if_vnet);
582 /* First, remove any existing filter entries. */
583 while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
584 CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
585 (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
586 epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
589 /* Now program new ones. */
591 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
592 if (ifma->ifma_addr->sa_family != AF_LINK)
594 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
596 IF_ADDR_WUNLOCK(ifp);
599 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
600 mc->mc_addr.sdl_index = ifp_p->if_index;
601 CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
603 IF_ADDR_WUNLOCK(ifp);
604 CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
605 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
616 * A handler for parent interface link layer address changes.
617 * If the parent interface link layer address is changed we
618 * should also change it on all children vlans.
621 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
623 struct epoch_tracker et;
625 struct ifnet *ifv_ifp;
626 struct ifvlantrunk *trunk;
627 struct sockaddr_dl *sdl;
629 /* Need the epoch since this is run on taskqueue_swi. */
631 trunk = ifp->if_vlantrunk;
638 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
639 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
640 * ioctl calls on the parent garbling the lladdr of the child vlan.
643 VLAN_FOREACH(ifv, trunk) {
645 * Copy new new lladdr into the ifv_ifp, enqueue a task
646 * to actually call if_setlladdr. if_setlladdr needs to
647 * be deferred to a taskqueue because it will call into
648 * the if_vlan ioctl path and try to acquire the global
651 ifv_ifp = ifv->ifv_ifp;
652 bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
654 sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
655 sdl->sdl_alen = ifp->if_addrlen;
656 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
658 TRUNK_WUNLOCK(trunk);
663 * A handler for network interface departure events.
664 * Track departure of trunks here so that we don't access invalid
665 * pointers or whatever if a trunk is ripped from under us, e.g.,
666 * by ejecting its hot-plug card. However, if an ifnet is simply
667 * being renamed, then there's no need to tear down the state.
670 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
673 struct ifvlantrunk *trunk;
675 /* If the ifnet is just being renamed, don't do anything. */
676 if (ifp->if_flags & IFF_RENAMING)
679 trunk = ifp->if_vlantrunk;
686 * OK, it's a trunk. Loop over and detach all vlan's on it.
687 * Check trunk pointer after each vlan_unconfig() as it will
688 * free it and set to NULL after the last vlan was detached.
690 VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
691 ifp->if_vlantrunk == NULL)
692 vlan_unconfig_locked(ifv->ifv_ifp, 1);
694 /* Trunk should have been destroyed in vlan_unconfig(). */
695 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
700 * Return the trunk device for a virtual interface.
702 static struct ifnet *
703 vlan_trunkdev(struct ifnet *ifp)
705 struct epoch_tracker et;
708 if (ifp->if_type != IFT_L2VLAN)
721 * Return the 12-bit VLAN VID for this interface, for use by external
722 * components such as Infiniband.
724 * XXXRW: Note that the function name here is historical; it should be named
728 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
732 if (ifp->if_type != IFT_L2VLAN)
735 *vidp = ifv->ifv_vid;
740 vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
744 if (ifp->if_type != IFT_L2VLAN)
747 *pcpp = ifv->ifv_pcp;
752 * Return a driver specific cookie for this interface. Synchronization
753 * with setcookie must be provided by the driver.
756 vlan_cookie(struct ifnet *ifp)
760 if (ifp->if_type != IFT_L2VLAN)
763 return (ifv->ifv_cookie);
767 * Store a cookie in our softc that drivers can use to store driver
768 * private per-instance data in.
771 vlan_setcookie(struct ifnet *ifp, void *cookie)
775 if (ifp->if_type != IFT_L2VLAN)
778 ifv->ifv_cookie = cookie;
783 * Return the vlan device present at the specific VID.
785 static struct ifnet *
786 vlan_devat(struct ifnet *ifp, uint16_t vid)
788 struct epoch_tracker et;
789 struct ifvlantrunk *trunk;
793 trunk = ifp->if_vlantrunk;
799 ifv = vlan_gethash(trunk, vid);
807 * Recalculate the cached VLAN tag exposed via the MIB.
810 vlan_tag_recalculate(struct ifvlan *ifv)
813 ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
817 * VLAN support can be loaded as a module. The only place in the
818 * system that's intimately aware of this is ether_input. We hook
819 * into this code through vlan_input_p which is defined there and
820 * set here. No one else in the system should be aware of this so
821 * we use an explicit reference here.
823 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
825 /* For if_link_state_change() eyes only... */
826 extern void (*vlan_link_state_p)(struct ifnet *);
829 vlan_modevent(module_t mod, int type, void *data)
834 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
835 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
836 if (ifdetach_tag == NULL)
838 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
839 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
840 if (iflladdr_tag == NULL)
843 vlan_input_p = vlan_input;
844 vlan_link_state_p = vlan_link_state;
845 vlan_trunk_cap_p = vlan_trunk_capabilities;
846 vlan_trunkdev_p = vlan_trunkdev;
847 vlan_cookie_p = vlan_cookie;
848 vlan_setcookie_p = vlan_setcookie;
849 vlan_tag_p = vlan_tag;
850 vlan_pcp_p = vlan_pcp;
851 vlan_devat_p = vlan_devat;
853 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
854 vlan_clone_create, vlan_clone_destroy);
857 printf("vlan: initialized, using "
861 "hash tables with chaining"
868 if_clone_detach(vlan_cloner);
870 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
871 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
873 vlan_link_state_p = NULL;
874 vlan_trunk_cap_p = NULL;
875 vlan_trunkdev_p = NULL;
877 vlan_cookie_p = NULL;
878 vlan_setcookie_p = NULL;
880 VLAN_LOCKING_DESTROY();
882 printf("vlan: unloaded\n");
890 static moduledata_t vlan_mod = {
896 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
897 MODULE_VERSION(if_vlan, 3);
901 vnet_vlan_init(const void *unused __unused)
904 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
905 vlan_clone_create, vlan_clone_destroy);
906 V_vlan_cloner = vlan_cloner;
908 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
909 vnet_vlan_init, NULL);
912 vnet_vlan_uninit(const void *unused __unused)
915 if_clone_detach(V_vlan_cloner);
917 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
918 vnet_vlan_uninit, NULL);
922 * Check for <etherif>.<vlan> style interface names.
924 static struct ifnet *
925 vlan_clone_match_ethervid(const char *name, int *vidp)
927 char ifname[IFNAMSIZ];
932 strlcpy(ifname, name, IFNAMSIZ);
933 if ((cp = strchr(ifname, '.')) == NULL)
936 if ((ifp = ifunit_ref(ifname)) == NULL)
944 for(; *cp >= '0' && *cp <= '9'; cp++)
945 vid = (vid * 10) + (*cp - '0');
957 vlan_clone_match(struct if_clone *ifc, const char *name)
961 if (vlan_clone_match_ethervid(name, NULL) != NULL)
964 if (strncmp(vlanname, name, strlen(vlanname)) != 0)
966 for (cp = name + 4; *cp != '\0'; cp++) {
967 if (*cp < '0' || *cp > '9')
975 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
986 struct sockaddr_dl *sdl;
988 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
991 * There are 3 (ugh) ways to specify the cloned device:
992 * o pass a parameter block with the clone request.
993 * o specify parameters in the text of the clone device name
994 * o specify no parameters and get an unattached device that
995 * must be configured separately.
996 * The first technique is preferred; the latter two are
997 * supported for backwards compatibility.
999 * XXXRW: Note historic use of the word "tag" here. New ioctls may be
1003 error = copyin(params, &vlr, sizeof(vlr));
1006 p = ifunit_ref(vlr.vlr_parent);
1009 error = ifc_name2unit(name, &unit);
1015 wildcard = (unit < 0);
1016 } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1021 error = ifc_name2unit(name, &unit);
1025 wildcard = (unit < 0);
1028 error = ifc_alloc_unit(ifc, &unit);
1035 /* In the wildcard case, we need to update the name. */
1037 for (dp = name; *dp != '\0'; dp++);
1038 if (snprintf(dp, len - (dp-name), "%d", unit) >
1039 len - (dp-name) - 1) {
1040 panic("%s: interface name too long", __func__);
1044 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1045 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1047 ifc_free_unit(ifc, unit);
1053 CK_SLIST_INIT(&ifv->vlan_mc_listhead);
1054 ifp->if_softc = ifv;
1056 * Set the name manually rather than using if_initname because
1057 * we don't conform to the default naming convention for interfaces.
1059 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1060 ifp->if_dname = vlanname;
1061 ifp->if_dunit = unit;
1063 ifp->if_init = vlan_init;
1064 ifp->if_transmit = vlan_transmit;
1065 ifp->if_qflush = vlan_qflush;
1066 ifp->if_ioctl = vlan_ioctl;
1068 ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1069 ifp->if_snd_tag_modify = vlan_snd_tag_modify;
1070 ifp->if_snd_tag_query = vlan_snd_tag_query;
1071 ifp->if_snd_tag_free = vlan_snd_tag_free;
1073 ifp->if_flags = VLAN_IFFLAGS;
1074 ether_ifattach(ifp, eaddr);
1075 /* Now undo some of the damage... */
1076 ifp->if_baudrate = 0;
1077 ifp->if_type = IFT_L2VLAN;
1078 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1080 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1081 sdl->sdl_type = IFT_L2VLAN;
1084 error = vlan_config(ifv, p, vid);
1088 * Since we've partially failed, we need to back
1089 * out all the way, otherwise userland could get
1090 * confused. Thus, we destroy the interface.
1092 ether_ifdetach(ifp);
1095 ifc_free_unit(ifc, unit);
1106 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
1108 struct ifvlan *ifv = ifp->if_softc;
1109 int unit = ifp->if_dunit;
1111 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
1112 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
1114 * We should have the only reference to the ifv now, so we can now
1115 * drain any remaining lladdr task before freeing the ifnet and the
1118 taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1122 ifc_free_unit(ifc, unit);
1128 * The ifp->if_init entry point for vlan(4) is a no-op.
1131 vlan_init(void *foo __unused)
1136 * The if_transmit method for vlan(4) interface.
1139 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1141 struct epoch_tracker et;
1144 int error, len, mcast;
1146 NET_EPOCH_ENTER(et);
1147 ifv = ifp->if_softc;
1148 if (TRUNK(ifv) == NULL) {
1149 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1155 len = m->m_pkthdr.len;
1156 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1161 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1162 struct vlan_snd_tag *vst;
1163 struct m_snd_tag *mst;
1165 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1166 mst = m->m_pkthdr.snd_tag;
1167 vst = mst_to_vst(mst);
1168 if (vst->tag->ifp != p) {
1169 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1175 m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1176 m_snd_tag_rele(mst);
1181 * Do not run parent's if_transmit() if the parent is not up,
1182 * or parent's driver will cause a system crash.
1184 if (!UP_AND_RUNNING(p)) {
1185 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1191 if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1192 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1198 * Send it, precisely as ether_output() would have.
1200 error = (p->if_transmit)(p, m);
1202 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1203 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1204 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1206 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1212 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1215 vlan_qflush(struct ifnet *ifp __unused)
1220 vlan_input(struct ifnet *ifp, struct mbuf *m)
1222 struct epoch_tracker et;
1223 struct ifvlantrunk *trunk;
1228 NET_EPOCH_ENTER(et);
1229 trunk = ifp->if_vlantrunk;
1230 if (trunk == NULL) {
1236 if (m->m_flags & M_VLANTAG) {
1238 * Packet is tagged, but m contains a normal
1239 * Ethernet frame; the tag is stored out-of-band.
1241 tag = m->m_pkthdr.ether_vtag;
1242 m->m_flags &= ~M_VLANTAG;
1244 struct ether_vlan_header *evl;
1247 * Packet is tagged in-band as specified by 802.1q.
1249 switch (ifp->if_type) {
1251 if (m->m_len < sizeof(*evl) &&
1252 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1253 if_printf(ifp, "cannot pullup VLAN header\n");
1257 evl = mtod(m, struct ether_vlan_header *);
1258 tag = ntohs(evl->evl_tag);
1261 * Remove the 802.1q header by copying the Ethernet
1262 * addresses over it and adjusting the beginning of
1263 * the data in the mbuf. The encapsulated Ethernet
1264 * type field is already in place.
1266 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1267 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1268 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1273 panic("%s: %s has unsupported if_type %u",
1274 __func__, ifp->if_xname, ifp->if_type);
1276 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1283 vid = EVL_VLANOFTAG(tag);
1285 ifv = vlan_gethash(trunk, vid);
1286 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1288 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1293 if (vlan_mtag_pcp) {
1295 * While uncommon, it is possible that we will find a 802.1q
1296 * packet encapsulated inside another packet that also had an
1297 * 802.1q header. For example, ethernet tunneled over IPSEC
1298 * arriving over ethernet. In that case, we replace the
1299 * existing 802.1q PCP m_tag value.
1301 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1303 mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1304 sizeof(uint8_t), M_NOWAIT);
1306 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1311 m_tag_prepend(m, mtag);
1313 *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1316 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1317 if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1320 /* Pass it back through the parent's input routine. */
1321 (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1325 vlan_lladdr_fn(void *arg, int pending __unused)
1330 ifv = (struct ifvlan *)arg;
1333 CURVNET_SET(ifp->if_vnet);
1335 /* The ifv_ifp already has the lladdr copied in. */
1336 if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1342 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
1344 struct epoch_tracker et;
1345 struct ifvlantrunk *trunk;
1350 * We can handle non-ethernet hardware types as long as
1351 * they handle the tagging and headers themselves.
1353 if (p->if_type != IFT_ETHER &&
1354 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1355 return (EPROTONOSUPPORT);
1356 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1357 return (EPROTONOSUPPORT);
1359 * Don't let the caller set up a VLAN VID with
1360 * anything except VLID bits.
1361 * VID numbers 0x0 and 0xFFF are reserved.
1363 if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1369 if (p->if_vlantrunk == NULL) {
1370 trunk = malloc(sizeof(struct ifvlantrunk),
1371 M_VLAN, M_WAITOK | M_ZERO);
1372 vlan_inithash(trunk);
1373 TRUNK_LOCK_INIT(trunk);
1375 p->if_vlantrunk = trunk;
1377 if_ref(trunk->parent);
1378 TRUNK_WUNLOCK(trunk);
1380 trunk = p->if_vlantrunk;
1383 ifv->ifv_vid = vid; /* must set this before vlan_inshash() */
1384 ifv->ifv_pcp = 0; /* Default: best effort delivery. */
1385 vlan_tag_recalculate(ifv);
1386 error = vlan_inshash(trunk, ifv);
1389 ifv->ifv_proto = ETHERTYPE_VLAN;
1390 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1391 ifv->ifv_mintu = ETHERMIN;
1392 ifv->ifv_pflags = 0;
1393 ifv->ifv_capenable = -1;
1396 * If the parent supports the VLAN_MTU capability,
1397 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1400 if (p->if_capenable & IFCAP_VLAN_MTU) {
1402 * No need to fudge the MTU since the parent can
1403 * handle extended frames.
1405 ifv->ifv_mtufudge = 0;
1408 * Fudge the MTU by the encapsulation size. This
1409 * makes us incompatible with strictly compliant
1410 * 802.1Q implementations, but allows us to use
1411 * the feature with other NetBSD implementations,
1412 * which might still be useful.
1414 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1417 ifv->ifv_trunk = trunk;
1420 * Initialize fields from our parent. This duplicates some
1421 * work with ether_ifattach() but allows for non-ethernet
1422 * interfaces to also work.
1424 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1425 ifp->if_baudrate = p->if_baudrate;
1426 ifp->if_output = p->if_output;
1427 ifp->if_input = p->if_input;
1428 ifp->if_resolvemulti = p->if_resolvemulti;
1429 ifp->if_addrlen = p->if_addrlen;
1430 ifp->if_broadcastaddr = p->if_broadcastaddr;
1431 ifp->if_pcp = ifv->ifv_pcp;
1434 * Copy only a selected subset of flags from the parent.
1435 * Other flags are none of our business.
1437 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1438 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1439 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1440 #undef VLAN_COPY_FLAGS
1442 ifp->if_link_state = p->if_link_state;
1444 NET_EPOCH_ENTER(et);
1445 vlan_capabilities(ifv);
1449 * Set up our interface address to reflect the underlying
1450 * physical interface's.
1452 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1453 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1456 TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1458 /* We are ready for operation now. */
1459 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1461 /* Update flags on the parent, if necessary. */
1462 vlan_setflags(ifp, 1);
1465 * Configure multicast addresses that may already be
1466 * joined on the vlan device.
1468 (void)vlan_setmulti(ifp);
1472 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1479 vlan_unconfig(struct ifnet *ifp)
1483 vlan_unconfig_locked(ifp, 0);
1488 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1490 struct ifvlantrunk *trunk;
1491 struct vlan_mc_entry *mc;
1493 struct ifnet *parent;
1496 VLAN_XLOCK_ASSERT();
1498 ifv = ifp->if_softc;
1499 trunk = ifv->ifv_trunk;
1502 if (trunk != NULL) {
1503 parent = trunk->parent;
1506 * Since the interface is being unconfigured, we need to
1507 * empty the list of multicast groups that we may have joined
1508 * while we were alive from the parent's list.
1510 while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1512 * If the parent interface is being detached,
1513 * all its multicast addresses have already
1514 * been removed. Warn about errors if
1515 * if_delmulti() does fail, but don't abort as
1516 * all callers expect vlan destruction to
1520 error = if_delmulti(parent,
1521 (struct sockaddr *)&mc->mc_addr);
1524 "Failed to delete multicast address from parent: %d\n",
1527 CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1528 epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
1531 vlan_setflags(ifp, 0); /* clear special flags on parent */
1533 vlan_remhash(trunk, ifv);
1534 ifv->ifv_trunk = NULL;
1537 * Check if we were the last.
1539 if (trunk->refcnt == 0) {
1540 parent->if_vlantrunk = NULL;
1542 trunk_destroy(trunk);
1546 /* Disconnect from parent. */
1547 if (ifv->ifv_pflags)
1548 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1549 ifp->if_mtu = ETHERMTU;
1550 ifp->if_link_state = LINK_STATE_UNKNOWN;
1551 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1554 * Only dispatch an event if vlan was
1555 * attached, otherwise there is nothing
1556 * to cleanup anyway.
1559 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1562 /* Handle a reference counted flag that should be set on the parent as well */
1564 vlan_setflag(struct ifnet *ifp, int flag, int status,
1565 int (*func)(struct ifnet *, int))
1570 VLAN_SXLOCK_ASSERT();
1572 ifv = ifp->if_softc;
1573 status = status ? (ifp->if_flags & flag) : 0;
1574 /* Now "status" contains the flag value or 0 */
1577 * See if recorded parent's status is different from what
1578 * we want it to be. If it is, flip it. We record parent's
1579 * status in ifv_pflags so that we won't clear parent's flag
1580 * we haven't set. In fact, we don't clear or set parent's
1581 * flags directly, but get or release references to them.
1582 * That's why we can be sure that recorded flags still are
1583 * in accord with actual parent's flags.
1585 if (status != (ifv->ifv_pflags & flag)) {
1586 error = (*func)(PARENT(ifv), status);
1589 ifv->ifv_pflags &= ~flag;
1590 ifv->ifv_pflags |= status;
1596 * Handle IFF_* flags that require certain changes on the parent:
1597 * if "status" is true, update parent's flags respective to our if_flags;
1598 * if "status" is false, forcedly clear the flags set on parent.
1601 vlan_setflags(struct ifnet *ifp, int status)
1605 for (i = 0; vlan_pflags[i].flag; i++) {
1606 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1607 status, vlan_pflags[i].func);
1614 /* Inform all vlans that their parent has changed link state */
1616 vlan_link_state(struct ifnet *ifp)
1618 struct epoch_tracker et;
1619 struct ifvlantrunk *trunk;
1622 /* Called from a taskqueue_swi task, so we cannot sleep. */
1623 NET_EPOCH_ENTER(et);
1624 trunk = ifp->if_vlantrunk;
1625 if (trunk == NULL) {
1631 VLAN_FOREACH(ifv, trunk) {
1632 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1633 if_link_state_change(ifv->ifv_ifp,
1634 trunk->parent->if_link_state);
1636 TRUNK_WUNLOCK(trunk);
1641 vlan_capabilities(struct ifvlan *ifv)
1645 struct ifnet_hw_tsomax hw_tsomax;
1646 int cap = 0, ena = 0, mena;
1649 VLAN_SXLOCK_ASSERT();
1654 /* Mask parent interface enabled capabilities disabled by user. */
1655 mena = p->if_capenable & ifv->ifv_capenable;
1658 * If the parent interface can do checksum offloading
1659 * on VLANs, then propagate its hardware-assisted
1660 * checksumming flags. Also assert that checksum
1661 * offloading requires hardware VLAN tagging.
1663 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1664 cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1665 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1666 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1667 ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1668 if (ena & IFCAP_TXCSUM)
1669 hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1670 CSUM_UDP | CSUM_SCTP);
1671 if (ena & IFCAP_TXCSUM_IPV6)
1672 hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1673 CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
1677 * If the parent interface can do TSO on VLANs then
1678 * propagate the hardware-assisted flag. TSO on VLANs
1679 * does not necessarily require hardware VLAN tagging.
1681 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
1682 if_hw_tsomax_common(p, &hw_tsomax);
1683 if_hw_tsomax_update(ifp, &hw_tsomax);
1684 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1685 cap |= p->if_capabilities & IFCAP_TSO;
1686 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1687 ena |= mena & IFCAP_TSO;
1688 if (ena & IFCAP_TSO)
1689 hwa |= p->if_hwassist & CSUM_TSO;
1693 * If the parent interface can do LRO and checksum offloading on
1694 * VLANs, then guess it may do LRO on VLANs. False positive here
1695 * cost nothing, while false negative may lead to some confusions.
1697 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1698 cap |= p->if_capabilities & IFCAP_LRO;
1699 if (p->if_capenable & IFCAP_VLAN_HWCSUM)
1700 ena |= p->if_capenable & IFCAP_LRO;
1703 * If the parent interface can offload TCP connections over VLANs then
1704 * propagate its TOE capability to the VLAN interface.
1706 * All TOE drivers in the tree today can deal with VLANs. If this
1707 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1710 #define IFCAP_VLAN_TOE IFCAP_TOE
1711 if (p->if_capabilities & IFCAP_VLAN_TOE)
1712 cap |= p->if_capabilities & IFCAP_TOE;
1713 if (p->if_capenable & IFCAP_VLAN_TOE) {
1714 TOEDEV(ifp) = TOEDEV(p);
1715 ena |= mena & IFCAP_TOE;
1719 * If the parent interface supports dynamic link state, so does the
1722 cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1723 ena |= (mena & IFCAP_LINKSTATE);
1727 * If the parent interface supports ratelimiting, so does the
1730 cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1731 ena |= (mena & IFCAP_TXRTLMT);
1735 * If the parent interface supports unmapped mbufs, so does
1736 * the VLAN interface. Note that this should be fine even for
1737 * interfaces that don't support hardware tagging as headers
1738 * are prepended in normal mbufs to unmapped mbufs holding
1741 cap |= (p->if_capabilities & IFCAP_NOMAP);
1742 ena |= (mena & IFCAP_NOMAP);
1744 ifp->if_capabilities = cap;
1745 ifp->if_capenable = ena;
1746 ifp->if_hwassist = hwa;
1750 vlan_trunk_capabilities(struct ifnet *ifp)
1752 struct epoch_tracker et;
1753 struct ifvlantrunk *trunk;
1757 trunk = ifp->if_vlantrunk;
1758 if (trunk == NULL) {
1762 NET_EPOCH_ENTER(et);
1763 VLAN_FOREACH(ifv, trunk) {
1764 vlan_capabilities(ifv);
1771 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1777 struct ifvlantrunk *trunk;
1781 ifr = (struct ifreq *)data;
1782 ifa = (struct ifaddr *) data;
1783 ifv = ifp->if_softc;
1787 ifp->if_flags |= IFF_UP;
1789 if (ifa->ifa_addr->sa_family == AF_INET)
1790 arp_ifinit(ifp, ifa);
1794 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
1799 if (TRUNK(ifv) != NULL) {
1802 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1804 /* Limit the result to the parent's current config. */
1806 struct ifmediareq *ifmr;
1808 ifmr = (struct ifmediareq *)data;
1809 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1810 ifmr->ifm_count = 1;
1811 error = copyout(&ifmr->ifm_current,
1828 * Set the interface MTU.
1832 if (trunk != NULL) {
1835 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1837 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1840 ifp->if_mtu = ifr->ifr_mtu;
1841 TRUNK_WUNLOCK(trunk);
1850 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
1851 * interface to be delegated to a jail without allowing the
1852 * jail to change what underlying interface/VID it is
1853 * associated with. We are not entirely convinced that this
1854 * is the right way to accomplish that policy goal.
1856 if (ifp->if_vnet != ifp->if_home_vnet) {
1861 error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
1864 if (vlr.vlr_parent[0] == '\0') {
1868 p = ifunit_ref(vlr.vlr_parent);
1873 error = vlan_config(ifv, p, vlr.vlr_tag);
1879 if (ifp->if_vnet != ifp->if_home_vnet) {
1884 bzero(&vlr, sizeof(vlr));
1886 if (TRUNK(ifv) != NULL) {
1887 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1888 sizeof(vlr.vlr_parent));
1889 vlr.vlr_tag = ifv->ifv_vid;
1892 error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
1897 * We should propagate selected flags to the parent,
1898 * e.g., promiscuous mode.
1901 if (TRUNK(ifv) != NULL)
1902 error = vlan_setflags(ifp, 1);
1909 * If we don't have a parent, just remember the membership for
1912 * XXX We need the rmlock here to avoid sleeping while
1913 * holding in6_multi_mtx.
1918 error = vlan_setmulti(ifp);
1924 if (ifp->if_vnet != ifp->if_home_vnet) {
1929 ifr->ifr_vlan_pcp = ifv->ifv_pcp;
1934 if (ifp->if_vnet != ifp->if_home_vnet) {
1939 error = priv_check(curthread, PRIV_NET_SETVLANPCP);
1942 if (ifr->ifr_vlan_pcp > 7) {
1946 ifv->ifv_pcp = ifr->ifr_vlan_pcp;
1947 ifp->if_pcp = ifv->ifv_pcp;
1948 vlan_tag_recalculate(ifv);
1949 /* broadcast event about PCP change */
1950 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
1955 ifv->ifv_capenable = ifr->ifr_reqcap;
1957 if (trunk != NULL) {
1958 struct epoch_tracker et;
1960 NET_EPOCH_ENTER(et);
1961 vlan_capabilities(ifv);
1977 vlan_snd_tag_alloc(struct ifnet *ifp,
1978 union if_snd_tag_alloc_params *params,
1979 struct m_snd_tag **ppmt)
1981 struct epoch_tracker et;
1982 struct vlan_snd_tag *vst;
1984 struct ifnet *parent;
1987 NET_EPOCH_ENTER(et);
1988 ifv = ifp->if_softc;
1989 if (ifv->ifv_trunk != NULL)
1990 parent = PARENT(ifv);
1993 if (parent == NULL || parent->if_snd_tag_alloc == NULL) {
1995 return (EOPNOTSUPP);
2000 vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2006 error = parent->if_snd_tag_alloc(parent, params, &vst->tag);
2013 m_snd_tag_init(&vst->com, ifp);
2020 vlan_snd_tag_modify(struct m_snd_tag *mst,
2021 union if_snd_tag_modify_params *params)
2023 struct vlan_snd_tag *vst;
2025 vst = mst_to_vst(mst);
2026 return (vst->tag->ifp->if_snd_tag_modify(vst->tag, params));
2030 vlan_snd_tag_query(struct m_snd_tag *mst,
2031 union if_snd_tag_query_params *params)
2033 struct vlan_snd_tag *vst;
2035 vst = mst_to_vst(mst);
2036 return (vst->tag->ifp->if_snd_tag_query(vst->tag, params));
2040 vlan_snd_tag_free(struct m_snd_tag *mst)
2042 struct vlan_snd_tag *vst;
2044 vst = mst_to_vst(mst);
2045 m_snd_tag_rele(vst->tag);