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_kern_tls.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>
81 #include <netinet/in.h>
82 #include <netinet/if_ether.h>
85 #define VLAN_DEF_HWIDTH 4
86 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
88 #define UP_AND_RUNNING(ifp) \
89 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
91 CK_SLIST_HEAD(ifvlanhead, ifvlan);
94 struct ifnet *parent; /* parent interface of this trunk */
97 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
98 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
100 struct ifvlanhead *hash; /* dynamic hash-list table */
107 #if defined(KERN_TLS) || defined(RATELIMIT)
108 struct vlan_snd_tag {
109 struct m_snd_tag com;
110 struct m_snd_tag *tag;
113 static inline struct vlan_snd_tag *
114 mst_to_vst(struct m_snd_tag *mst)
117 return (__containerof(mst, struct vlan_snd_tag, com));
122 * This macro provides a facility to iterate over every vlan on a trunk with
123 * the assumption that none will be added/removed during iteration.
126 #define VLAN_FOREACH(_ifv, _trunk) \
128 for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
129 if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
130 #else /* VLAN_ARRAY */
131 #define VLAN_FOREACH(_ifv, _trunk) \
132 struct ifvlan *_next; \
134 for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
135 CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
136 #endif /* VLAN_ARRAY */
139 * This macro provides a facility to iterate over every vlan on a trunk while
140 * also modifying the number of vlans on the trunk. The iteration continues
141 * until some condition is met or there are no more vlans on the trunk.
144 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
145 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
147 for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
148 if (((_ifv) = (_trunk)->vlans[_i]))
149 #else /* VLAN_ARRAY */
151 * The hash table case is more complicated. We allow for the hash table to be
152 * modified (i.e. vlans removed) while we are iterating over it. To allow for
153 * this we must restart the iteration every time we "touch" something during
154 * the iteration, since removal will resize the hash table and invalidate our
155 * current position. If acting on the touched element causes the trunk to be
156 * emptied, then iteration also stops.
158 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
160 bool _touch = false; \
162 !(_cond) && _i < (1 << (_trunk)->hwidth); \
163 _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
164 if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
166 #endif /* VLAN_ARRAY */
168 struct vlan_mc_entry {
169 struct sockaddr_dl mc_addr;
170 CK_SLIST_ENTRY(vlan_mc_entry) mc_entries;
171 struct epoch_context mc_epoch_ctx;
175 struct ifvlantrunk *ifv_trunk;
176 struct ifnet *ifv_ifp;
177 #define TRUNK(ifv) ((ifv)->ifv_trunk)
178 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
180 int ifv_pflags; /* special flags we have set on parent */
182 int ifv_encaplen; /* encapsulation length */
183 int ifv_mtufudge; /* MTU fudged by this much */
184 int ifv_mintu; /* min transmission unit */
185 uint16_t ifv_proto; /* encapsulation ethertype */
186 uint16_t ifv_tag; /* tag to apply on packets leaving if */
187 uint16_t ifv_vid; /* VLAN ID */
188 uint8_t ifv_pcp; /* Priority Code Point (PCP). */
189 struct task lladdr_task;
190 CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
192 CK_SLIST_ENTRY(ifvlan) ifv_list;
196 /* Special flags we should propagate to parent. */
199 int (*func)(struct ifnet *, int);
201 {IFF_PROMISC, ifpromisc},
202 {IFF_ALLMULTI, if_allmulti},
206 extern int vlan_mtag_pcp;
208 static const char vlanname[] = "vlan";
209 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
211 static eventhandler_tag ifdetach_tag;
212 static eventhandler_tag iflladdr_tag;
215 * if_vlan uses two module-level synchronizations primitives to allow concurrent
216 * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
217 * while they are being used for tx/rx. To accomplish this in a way that has
218 * acceptable performance and cooperation with other parts of the network stack
219 * there is a non-sleepable epoch(9) and an sx(9).
221 * The performance-sensitive paths that warrant using the epoch(9) are
222 * vlan_transmit and vlan_input. Both have to check for the vlan interface's
223 * existence using if_vlantrunk, and being in the network tx/rx paths the use
224 * of an epoch(9) gives a measureable improvement in performance.
226 * The reason for having an sx(9) is mostly because there are still areas that
227 * must be sleepable and also have safe concurrent access to a vlan interface.
228 * Since the sx(9) exists, it is used by default in most paths unless sleeping
229 * is not permitted, or if it is not clear whether sleeping is permitted.
232 #define _VLAN_SX_ID ifv_sx
234 static struct sx _VLAN_SX_ID;
236 #define VLAN_LOCKING_INIT() \
237 sx_init(&_VLAN_SX_ID, "vlan_sx")
239 #define VLAN_LOCKING_DESTROY() \
240 sx_destroy(&_VLAN_SX_ID)
242 #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID)
243 #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID)
244 #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID)
245 #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID)
246 #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
247 #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
248 #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_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED);
261 * The VLAN_ARRAY substitutes the dynamic hash with a static array
262 * with 4096 entries. In theory this can give a boost in processing,
263 * however in practice it does not. Probably this is because the array
264 * is too big to fit into CPU cache.
267 static void vlan_inithash(struct ifvlantrunk *trunk);
268 static void vlan_freehash(struct ifvlantrunk *trunk);
269 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
270 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
271 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
272 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
275 static void trunk_destroy(struct ifvlantrunk *trunk);
277 static void vlan_init(void *foo);
278 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
279 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
280 #if defined(KERN_TLS) || defined(RATELIMIT)
281 static int vlan_snd_tag_alloc(struct ifnet *,
282 union if_snd_tag_alloc_params *, struct m_snd_tag **);
283 static int vlan_snd_tag_modify(struct m_snd_tag *,
284 union if_snd_tag_modify_params *);
285 static int vlan_snd_tag_query(struct m_snd_tag *,
286 union if_snd_tag_query_params *);
287 static void vlan_snd_tag_free(struct m_snd_tag *);
289 static void vlan_qflush(struct ifnet *ifp);
290 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
291 int (*func)(struct ifnet *, int));
292 static int vlan_setflags(struct ifnet *ifp, int status);
293 static int vlan_setmulti(struct ifnet *ifp);
294 static int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
295 static int vlan_output(struct ifnet *ifp, struct mbuf *m,
296 const struct sockaddr *dst, struct route *ro);
297 static void vlan_unconfig(struct ifnet *ifp);
298 static void vlan_unconfig_locked(struct ifnet *ifp, int departing);
299 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
300 static void vlan_link_state(struct ifnet *ifp);
301 static void vlan_capabilities(struct ifvlan *ifv);
302 static void vlan_trunk_capabilities(struct ifnet *ifp);
304 static struct ifnet *vlan_clone_match_ethervid(const char *, int *);
305 static int vlan_clone_match(struct if_clone *, const char *);
306 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
307 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
309 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
310 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
312 static void vlan_lladdr_fn(void *arg, int pending);
314 static struct if_clone *vlan_cloner;
317 VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
318 #define V_vlan_cloner VNET(vlan_cloner)
322 vlan_mc_free(struct epoch_context *ctx)
324 struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
329 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
332 vlan_inithash(struct ifvlantrunk *trunk)
337 * The trunk must not be locked here since we call malloc(M_WAITOK).
338 * It is OK in case this function is called before the trunk struct
339 * gets hooked up and becomes visible from other threads.
342 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
343 ("%s: hash already initialized", __func__));
345 trunk->hwidth = VLAN_DEF_HWIDTH;
346 n = 1 << trunk->hwidth;
347 trunk->hmask = n - 1;
348 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
349 for (i = 0; i < n; i++)
350 CK_SLIST_INIT(&trunk->hash[i]);
354 vlan_freehash(struct ifvlantrunk *trunk)
359 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
360 for (i = 0; i < (1 << trunk->hwidth); i++)
361 KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
362 ("%s: hash table not empty", __func__));
364 free(trunk->hash, M_VLAN);
366 trunk->hwidth = trunk->hmask = 0;
370 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
376 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
378 b = 1 << trunk->hwidth;
379 i = HASH(ifv->ifv_vid, trunk->hmask);
380 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
381 if (ifv->ifv_vid == ifv2->ifv_vid)
385 * Grow the hash when the number of vlans exceeds half of the number of
386 * hash buckets squared. This will make the average linked-list length
389 if (trunk->refcnt > (b * b) / 2) {
390 vlan_growhash(trunk, 1);
391 i = HASH(ifv->ifv_vid, trunk->hmask);
393 CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
400 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
406 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
408 b = 1 << trunk->hwidth;
409 i = HASH(ifv->ifv_vid, trunk->hmask);
410 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
413 CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
414 if (trunk->refcnt < (b * b) / 2)
415 vlan_growhash(trunk, -1);
419 panic("%s: vlan not found\n", __func__);
420 return (ENOENT); /*NOTREACHED*/
424 * Grow the hash larger or smaller if memory permits.
427 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
430 struct ifvlanhead *hash2;
431 int hwidth2, i, j, n, n2;
434 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
437 /* Harmless yet obvious coding error */
438 printf("%s: howmuch is 0\n", __func__);
442 hwidth2 = trunk->hwidth + howmuch;
443 n = 1 << trunk->hwidth;
445 /* Do not shrink the table below the default */
446 if (hwidth2 < VLAN_DEF_HWIDTH)
449 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
451 printf("%s: out of memory -- hash size not changed\n",
453 return; /* We can live with the old hash table */
455 for (j = 0; j < n2; j++)
456 CK_SLIST_INIT(&hash2[j]);
457 for (i = 0; i < n; i++)
458 while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
459 CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
460 j = HASH(ifv->ifv_vid, n2 - 1);
461 CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
464 free(trunk->hash, M_VLAN);
466 trunk->hwidth = hwidth2;
467 trunk->hmask = n2 - 1;
470 if_printf(trunk->parent,
471 "VLAN hash table resized from %d to %d buckets\n", n, n2);
474 static __inline struct ifvlan *
475 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
481 CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
482 if (ifv->ifv_vid == vid)
488 /* Debugging code to view the hashtables. */
490 vlan_dumphash(struct ifvlantrunk *trunk)
495 for (i = 0; i < (1 << trunk->hwidth); i++) {
497 CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
498 printf("%s ", ifv->ifv_ifp->if_xname);
505 static __inline struct ifvlan *
506 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
509 return trunk->vlans[vid];
513 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
516 if (trunk->vlans[ifv->ifv_vid] != NULL)
518 trunk->vlans[ifv->ifv_vid] = ifv;
525 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
528 trunk->vlans[ifv->ifv_vid] = NULL;
535 vlan_freehash(struct ifvlantrunk *trunk)
540 vlan_inithash(struct ifvlantrunk *trunk)
544 #endif /* !VLAN_ARRAY */
547 trunk_destroy(struct ifvlantrunk *trunk)
551 vlan_freehash(trunk);
552 trunk->parent->if_vlantrunk = NULL;
553 TRUNK_LOCK_DESTROY(trunk);
554 if_rele(trunk->parent);
559 * Program our multicast filter. What we're actually doing is
560 * programming the multicast filter of the parent. This has the
561 * side effect of causing the parent interface to receive multicast
562 * traffic that it doesn't really want, which ends up being discarded
563 * later by the upper protocol layers. Unfortunately, there's no way
564 * to avoid this: there really is only one physical interface.
567 vlan_setmulti(struct ifnet *ifp)
570 struct ifmultiaddr *ifma;
572 struct vlan_mc_entry *mc;
577 /* Find the parent. */
581 CURVNET_SET_QUIET(ifp_p->if_vnet);
583 /* First, remove any existing filter entries. */
584 while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
585 CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
586 (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
587 epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
590 /* Now program new ones. */
592 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
593 if (ifma->ifma_addr->sa_family != AF_LINK)
595 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
597 IF_ADDR_WUNLOCK(ifp);
600 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
601 mc->mc_addr.sdl_index = ifp_p->if_index;
602 CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
604 IF_ADDR_WUNLOCK(ifp);
605 CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
606 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
617 * A handler for parent interface link layer address changes.
618 * If the parent interface link layer address is changed we
619 * should also change it on all children vlans.
622 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
624 struct epoch_tracker et;
626 struct ifnet *ifv_ifp;
627 struct ifvlantrunk *trunk;
628 struct sockaddr_dl *sdl;
630 /* Need the epoch since this is run on taskqueue_swi. */
632 trunk = ifp->if_vlantrunk;
639 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
640 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
641 * ioctl calls on the parent garbling the lladdr of the child vlan.
644 VLAN_FOREACH(ifv, trunk) {
646 * Copy new new lladdr into the ifv_ifp, enqueue a task
647 * to actually call if_setlladdr. if_setlladdr needs to
648 * be deferred to a taskqueue because it will call into
649 * the if_vlan ioctl path and try to acquire the global
652 ifv_ifp = ifv->ifv_ifp;
653 bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
655 sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
656 sdl->sdl_alen = ifp->if_addrlen;
657 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
659 TRUNK_WUNLOCK(trunk);
664 * A handler for network interface departure events.
665 * Track departure of trunks here so that we don't access invalid
666 * pointers or whatever if a trunk is ripped from under us, e.g.,
667 * by ejecting its hot-plug card. However, if an ifnet is simply
668 * being renamed, then there's no need to tear down the state.
671 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
674 struct ifvlantrunk *trunk;
676 /* If the ifnet is just being renamed, don't do anything. */
677 if (ifp->if_flags & IFF_RENAMING)
680 trunk = ifp->if_vlantrunk;
687 * OK, it's a trunk. Loop over and detach all vlan's on it.
688 * Check trunk pointer after each vlan_unconfig() as it will
689 * free it and set to NULL after the last vlan was detached.
691 VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
692 ifp->if_vlantrunk == NULL)
693 vlan_unconfig_locked(ifv->ifv_ifp, 1);
695 /* Trunk should have been destroyed in vlan_unconfig(). */
696 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
701 * Return the trunk device for a virtual interface.
703 static struct ifnet *
704 vlan_trunkdev(struct ifnet *ifp)
710 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 ifvlantrunk *trunk;
793 trunk = ifp->if_vlantrunk;
797 ifv = vlan_gethash(trunk, vid);
804 * Recalculate the cached VLAN tag exposed via the MIB.
807 vlan_tag_recalculate(struct ifvlan *ifv)
810 ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
814 * VLAN support can be loaded as a module. The only place in the
815 * system that's intimately aware of this is ether_input. We hook
816 * into this code through vlan_input_p which is defined there and
817 * set here. No one else in the system should be aware of this so
818 * we use an explicit reference here.
820 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
822 /* For if_link_state_change() eyes only... */
823 extern void (*vlan_link_state_p)(struct ifnet *);
826 vlan_modevent(module_t mod, int type, void *data)
831 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
832 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
833 if (ifdetach_tag == NULL)
835 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
836 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
837 if (iflladdr_tag == NULL)
840 vlan_input_p = vlan_input;
841 vlan_link_state_p = vlan_link_state;
842 vlan_trunk_cap_p = vlan_trunk_capabilities;
843 vlan_trunkdev_p = vlan_trunkdev;
844 vlan_cookie_p = vlan_cookie;
845 vlan_setcookie_p = vlan_setcookie;
846 vlan_tag_p = vlan_tag;
847 vlan_pcp_p = vlan_pcp;
848 vlan_devat_p = vlan_devat;
850 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
851 vlan_clone_create, vlan_clone_destroy);
854 printf("vlan: initialized, using "
858 "hash tables with chaining"
865 if_clone_detach(vlan_cloner);
867 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
868 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
870 vlan_link_state_p = NULL;
871 vlan_trunk_cap_p = NULL;
872 vlan_trunkdev_p = NULL;
874 vlan_cookie_p = NULL;
875 vlan_setcookie_p = NULL;
877 VLAN_LOCKING_DESTROY();
879 printf("vlan: unloaded\n");
887 static moduledata_t vlan_mod = {
893 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
894 MODULE_VERSION(if_vlan, 3);
898 vnet_vlan_init(const void *unused __unused)
901 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
902 vlan_clone_create, vlan_clone_destroy);
903 V_vlan_cloner = vlan_cloner;
905 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
906 vnet_vlan_init, NULL);
909 vnet_vlan_uninit(const void *unused __unused)
912 if_clone_detach(V_vlan_cloner);
914 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
915 vnet_vlan_uninit, NULL);
919 * Check for <etherif>.<vlan> style interface names.
921 static struct ifnet *
922 vlan_clone_match_ethervid(const char *name, int *vidp)
924 char ifname[IFNAMSIZ];
929 strlcpy(ifname, name, IFNAMSIZ);
930 if ((cp = strchr(ifname, '.')) == NULL)
933 if ((ifp = ifunit_ref(ifname)) == NULL)
941 for(; *cp >= '0' && *cp <= '9'; cp++)
942 vid = (vid * 10) + (*cp - '0');
954 vlan_clone_match(struct if_clone *ifc, const char *name)
958 if (vlan_clone_match_ethervid(name, NULL) != NULL)
961 if (strncmp(vlanname, name, strlen(vlanname)) != 0)
963 for (cp = name + 4; *cp != '\0'; cp++) {
964 if (*cp < '0' || *cp > '9')
972 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
983 struct sockaddr_dl *sdl;
985 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
988 * There are 3 (ugh) ways to specify the cloned device:
989 * o pass a parameter block with the clone request.
990 * o specify parameters in the text of the clone device name
991 * o specify no parameters and get an unattached device that
992 * must be configured separately.
993 * The first technique is preferred; the latter two are
994 * supported for backwards compatibility.
996 * XXXRW: Note historic use of the word "tag" here. New ioctls may be
1000 error = copyin(params, &vlr, sizeof(vlr));
1003 p = ifunit_ref(vlr.vlr_parent);
1006 error = ifc_name2unit(name, &unit);
1012 wildcard = (unit < 0);
1013 } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1018 error = ifc_name2unit(name, &unit);
1022 wildcard = (unit < 0);
1025 error = ifc_alloc_unit(ifc, &unit);
1032 /* In the wildcard case, we need to update the name. */
1034 for (dp = name; *dp != '\0'; dp++);
1035 if (snprintf(dp, len - (dp-name), "%d", unit) >
1036 len - (dp-name) - 1) {
1037 panic("%s: interface name too long", __func__);
1041 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1042 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1044 ifc_free_unit(ifc, unit);
1050 CK_SLIST_INIT(&ifv->vlan_mc_listhead);
1051 ifp->if_softc = ifv;
1053 * Set the name manually rather than using if_initname because
1054 * we don't conform to the default naming convention for interfaces.
1056 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1057 ifp->if_dname = vlanname;
1058 ifp->if_dunit = unit;
1060 ifp->if_init = vlan_init;
1061 ifp->if_transmit = vlan_transmit;
1062 ifp->if_qflush = vlan_qflush;
1063 ifp->if_ioctl = vlan_ioctl;
1064 #if defined(KERN_TLS) || defined(RATELIMIT)
1065 ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1066 ifp->if_snd_tag_modify = vlan_snd_tag_modify;
1067 ifp->if_snd_tag_query = vlan_snd_tag_query;
1068 ifp->if_snd_tag_free = vlan_snd_tag_free;
1070 ifp->if_flags = VLAN_IFFLAGS;
1071 ether_ifattach(ifp, eaddr);
1072 /* Now undo some of the damage... */
1073 ifp->if_baudrate = 0;
1074 ifp->if_type = IFT_L2VLAN;
1075 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1077 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1078 sdl->sdl_type = IFT_L2VLAN;
1081 error = vlan_config(ifv, p, vid);
1085 * Since we've partially failed, we need to back
1086 * out all the way, otherwise userland could get
1087 * confused. Thus, we destroy the interface.
1089 ether_ifdetach(ifp);
1092 ifc_free_unit(ifc, unit);
1103 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
1105 struct ifvlan *ifv = ifp->if_softc;
1106 int unit = ifp->if_dunit;
1108 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
1109 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
1111 * We should have the only reference to the ifv now, so we can now
1112 * drain any remaining lladdr task before freeing the ifnet and the
1115 taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1119 ifc_free_unit(ifc, unit);
1125 * The ifp->if_init entry point for vlan(4) is a no-op.
1128 vlan_init(void *foo __unused)
1133 * The if_transmit method for vlan(4) interface.
1136 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1140 int error, len, mcast;
1144 ifv = ifp->if_softc;
1145 if (TRUNK(ifv) == NULL) {
1146 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1151 len = m->m_pkthdr.len;
1152 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1156 #if defined(KERN_TLS) || defined(RATELIMIT)
1157 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1158 struct vlan_snd_tag *vst;
1159 struct m_snd_tag *mst;
1161 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1162 mst = m->m_pkthdr.snd_tag;
1163 vst = mst_to_vst(mst);
1164 if (vst->tag->ifp != p) {
1165 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1170 m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1171 m_snd_tag_rele(mst);
1176 * Do not run parent's if_transmit() if the parent is not up,
1177 * or parent's driver will cause a system crash.
1179 if (!UP_AND_RUNNING(p)) {
1180 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1185 if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1186 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1191 * Send it, precisely as ether_output() would have.
1193 error = (p->if_transmit)(p, m);
1195 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1196 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1197 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1199 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1204 vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1212 ifv = ifp->if_softc;
1213 if (TRUNK(ifv) == NULL) {
1218 return p->if_output(ifp, m, dst, ro);
1223 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1226 vlan_qflush(struct ifnet *ifp __unused)
1231 vlan_input(struct ifnet *ifp, struct mbuf *m)
1233 struct ifvlantrunk *trunk;
1240 trunk = ifp->if_vlantrunk;
1241 if (trunk == NULL) {
1246 if (m->m_flags & M_VLANTAG) {
1248 * Packet is tagged, but m contains a normal
1249 * Ethernet frame; the tag is stored out-of-band.
1251 tag = m->m_pkthdr.ether_vtag;
1252 m->m_flags &= ~M_VLANTAG;
1254 struct ether_vlan_header *evl;
1257 * Packet is tagged in-band as specified by 802.1q.
1259 switch (ifp->if_type) {
1261 if (m->m_len < sizeof(*evl) &&
1262 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1263 if_printf(ifp, "cannot pullup VLAN header\n");
1266 evl = mtod(m, struct ether_vlan_header *);
1267 tag = ntohs(evl->evl_tag);
1270 * Remove the 802.1q header by copying the Ethernet
1271 * addresses over it and adjusting the beginning of
1272 * the data in the mbuf. The encapsulated Ethernet
1273 * type field is already in place.
1275 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1276 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1277 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1282 panic("%s: %s has unsupported if_type %u",
1283 __func__, ifp->if_xname, ifp->if_type);
1285 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1291 vid = EVL_VLANOFTAG(tag);
1293 ifv = vlan_gethash(trunk, vid);
1294 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1295 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1300 if (vlan_mtag_pcp) {
1302 * While uncommon, it is possible that we will find a 802.1q
1303 * packet encapsulated inside another packet that also had an
1304 * 802.1q header. For example, ethernet tunneled over IPSEC
1305 * arriving over ethernet. In that case, we replace the
1306 * existing 802.1q PCP m_tag value.
1308 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1310 mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1311 sizeof(uint8_t), M_NOWAIT);
1313 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1317 m_tag_prepend(m, mtag);
1319 *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1322 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1323 if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1325 /* Pass it back through the parent's input routine. */
1326 (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1330 vlan_lladdr_fn(void *arg, int pending __unused)
1335 ifv = (struct ifvlan *)arg;
1338 CURVNET_SET(ifp->if_vnet);
1340 /* The ifv_ifp already has the lladdr copied in. */
1341 if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1347 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
1349 struct epoch_tracker et;
1350 struct ifvlantrunk *trunk;
1355 * We can handle non-ethernet hardware types as long as
1356 * they handle the tagging and headers themselves.
1358 if (p->if_type != IFT_ETHER &&
1359 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1360 return (EPROTONOSUPPORT);
1361 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1362 return (EPROTONOSUPPORT);
1364 * Don't let the caller set up a VLAN VID with
1365 * anything except VLID bits.
1366 * VID numbers 0x0 and 0xFFF are reserved.
1368 if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
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);
1380 p->if_vlantrunk = trunk;
1382 if_ref(trunk->parent);
1383 TRUNK_WUNLOCK(trunk);
1385 trunk = p->if_vlantrunk;
1388 ifv->ifv_vid = vid; /* must set this before vlan_inshash() */
1389 ifv->ifv_pcp = 0; /* Default: best effort delivery. */
1390 vlan_tag_recalculate(ifv);
1391 error = vlan_inshash(trunk, ifv);
1394 ifv->ifv_proto = ETHERTYPE_VLAN;
1395 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1396 ifv->ifv_mintu = ETHERMIN;
1397 ifv->ifv_pflags = 0;
1398 ifv->ifv_capenable = -1;
1401 * If the parent supports the VLAN_MTU capability,
1402 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1405 if (p->if_capenable & IFCAP_VLAN_MTU) {
1407 * No need to fudge the MTU since the parent can
1408 * handle extended frames.
1410 ifv->ifv_mtufudge = 0;
1413 * Fudge the MTU by the encapsulation size. This
1414 * makes us incompatible with strictly compliant
1415 * 802.1Q implementations, but allows us to use
1416 * the feature with other NetBSD implementations,
1417 * which might still be useful.
1419 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1422 ifv->ifv_trunk = trunk;
1425 * Initialize fields from our parent. This duplicates some
1426 * work with ether_ifattach() but allows for non-ethernet
1427 * interfaces to also work.
1429 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1430 ifp->if_baudrate = p->if_baudrate;
1431 ifp->if_input = p->if_input;
1432 ifp->if_resolvemulti = p->if_resolvemulti;
1433 ifp->if_addrlen = p->if_addrlen;
1434 ifp->if_broadcastaddr = p->if_broadcastaddr;
1435 ifp->if_pcp = ifv->ifv_pcp;
1438 * We wrap the parent's if_output using vlan_output to ensure that it
1439 * can't become stale.
1441 ifp->if_output = vlan_output;
1444 * Copy only a selected subset of flags from the parent.
1445 * Other flags are none of our business.
1447 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1448 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1449 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1450 #undef VLAN_COPY_FLAGS
1452 ifp->if_link_state = p->if_link_state;
1454 NET_EPOCH_ENTER(et);
1455 vlan_capabilities(ifv);
1459 * Set up our interface address to reflect the underlying
1460 * physical interface's.
1462 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1463 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1466 TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1468 /* We are ready for operation now. */
1469 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1471 /* Update flags on the parent, if necessary. */
1472 vlan_setflags(ifp, 1);
1475 * Configure multicast addresses that may already be
1476 * joined on the vlan device.
1478 (void)vlan_setmulti(ifp);
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) {
1513 parent = trunk->parent;
1516 * Since the interface is being unconfigured, we need to
1517 * empty the list of multicast groups that we may have joined
1518 * while we were alive from the parent's list.
1520 while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1522 * If the parent interface is being detached,
1523 * all its multicast addresses have already
1524 * been removed. Warn about errors if
1525 * if_delmulti() does fail, but don't abort as
1526 * all callers expect vlan destruction to
1530 error = if_delmulti(parent,
1531 (struct sockaddr *)&mc->mc_addr);
1534 "Failed to delete multicast address from parent: %d\n",
1537 CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1538 epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
1541 vlan_setflags(ifp, 0); /* clear special flags on parent */
1543 vlan_remhash(trunk, ifv);
1544 ifv->ifv_trunk = NULL;
1547 * Check if we were the last.
1549 if (trunk->refcnt == 0) {
1550 parent->if_vlantrunk = NULL;
1552 trunk_destroy(trunk);
1556 /* Disconnect from parent. */
1557 if (ifv->ifv_pflags)
1558 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1559 ifp->if_mtu = ETHERMTU;
1560 ifp->if_link_state = LINK_STATE_UNKNOWN;
1561 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1564 * Only dispatch an event if vlan was
1565 * attached, otherwise there is nothing
1566 * to cleanup anyway.
1569 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1572 /* Handle a reference counted flag that should be set on the parent as well */
1574 vlan_setflag(struct ifnet *ifp, int flag, int status,
1575 int (*func)(struct ifnet *, int))
1580 VLAN_SXLOCK_ASSERT();
1582 ifv = ifp->if_softc;
1583 status = status ? (ifp->if_flags & flag) : 0;
1584 /* Now "status" contains the flag value or 0 */
1587 * See if recorded parent's status is different from what
1588 * we want it to be. If it is, flip it. We record parent's
1589 * status in ifv_pflags so that we won't clear parent's flag
1590 * we haven't set. In fact, we don't clear or set parent's
1591 * flags directly, but get or release references to them.
1592 * That's why we can be sure that recorded flags still are
1593 * in accord with actual parent's flags.
1595 if (status != (ifv->ifv_pflags & flag)) {
1596 error = (*func)(PARENT(ifv), status);
1599 ifv->ifv_pflags &= ~flag;
1600 ifv->ifv_pflags |= status;
1606 * Handle IFF_* flags that require certain changes on the parent:
1607 * if "status" is true, update parent's flags respective to our if_flags;
1608 * if "status" is false, forcedly clear the flags set on parent.
1611 vlan_setflags(struct ifnet *ifp, int status)
1615 for (i = 0; vlan_pflags[i].flag; i++) {
1616 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1617 status, vlan_pflags[i].func);
1624 /* Inform all vlans that their parent has changed link state */
1626 vlan_link_state(struct ifnet *ifp)
1628 struct ifvlantrunk *trunk;
1633 trunk = ifp->if_vlantrunk;
1638 VLAN_FOREACH(ifv, trunk) {
1639 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1640 if_link_state_change(ifv->ifv_ifp,
1641 trunk->parent->if_link_state);
1643 TRUNK_WUNLOCK(trunk);
1647 vlan_capabilities(struct ifvlan *ifv)
1651 struct ifnet_hw_tsomax hw_tsomax;
1652 int cap = 0, ena = 0, mena;
1656 VLAN_SXLOCK_ASSERT();
1661 /* Mask parent interface enabled capabilities disabled by user. */
1662 mena = p->if_capenable & ifv->ifv_capenable;
1665 * If the parent interface can do checksum offloading
1666 * on VLANs, then propagate its hardware-assisted
1667 * checksumming flags. Also assert that checksum
1668 * offloading requires hardware VLAN tagging.
1670 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1671 cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1672 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1673 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1674 ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1675 if (ena & IFCAP_TXCSUM)
1676 hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1677 CSUM_UDP | CSUM_SCTP);
1678 if (ena & IFCAP_TXCSUM_IPV6)
1679 hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1680 CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
1684 * If the parent interface can do TSO on VLANs then
1685 * propagate the hardware-assisted flag. TSO on VLANs
1686 * does not necessarily require hardware VLAN tagging.
1688 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
1689 if_hw_tsomax_common(p, &hw_tsomax);
1690 if_hw_tsomax_update(ifp, &hw_tsomax);
1691 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1692 cap |= p->if_capabilities & IFCAP_TSO;
1693 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1694 ena |= mena & IFCAP_TSO;
1695 if (ena & IFCAP_TSO)
1696 hwa |= p->if_hwassist & CSUM_TSO;
1700 * If the parent interface can do LRO and checksum offloading on
1701 * VLANs, then guess it may do LRO on VLANs. False positive here
1702 * cost nothing, while false negative may lead to some confusions.
1704 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1705 cap |= p->if_capabilities & IFCAP_LRO;
1706 if (p->if_capenable & IFCAP_VLAN_HWCSUM)
1707 ena |= p->if_capenable & IFCAP_LRO;
1710 * If the parent interface can offload TCP connections over VLANs then
1711 * propagate its TOE capability to the VLAN interface.
1713 * All TOE drivers in the tree today can deal with VLANs. If this
1714 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1717 #define IFCAP_VLAN_TOE IFCAP_TOE
1718 if (p->if_capabilities & IFCAP_VLAN_TOE)
1719 cap |= p->if_capabilities & IFCAP_TOE;
1720 if (p->if_capenable & IFCAP_VLAN_TOE) {
1721 TOEDEV(ifp) = TOEDEV(p);
1722 ena |= mena & IFCAP_TOE;
1726 * If the parent interface supports dynamic link state, so does the
1729 cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1730 ena |= (mena & IFCAP_LINKSTATE);
1734 * If the parent interface supports ratelimiting, so does the
1737 cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1738 ena |= (mena & IFCAP_TXRTLMT);
1742 * If the parent interface supports unmapped mbufs, so does
1743 * the VLAN interface. Note that this should be fine even for
1744 * interfaces that don't support hardware tagging as headers
1745 * are prepended in normal mbufs to unmapped mbufs holding
1748 cap |= (p->if_capabilities & IFCAP_NOMAP);
1749 ena |= (mena & IFCAP_NOMAP);
1752 * If the parent interface can offload encryption and segmentation
1753 * of TLS records over TCP, propagate it's capability to the VLAN
1756 * All TLS drivers in the tree today can deal with VLANs. If
1757 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
1760 if (p->if_capabilities & IFCAP_TXTLS)
1761 cap |= p->if_capabilities & IFCAP_TXTLS;
1762 if (p->if_capenable & IFCAP_TXTLS)
1763 ena |= mena & IFCAP_TXTLS;
1765 ifp->if_capabilities = cap;
1766 ifp->if_capenable = ena;
1767 ifp->if_hwassist = hwa;
1771 vlan_trunk_capabilities(struct ifnet *ifp)
1773 struct ifvlantrunk *trunk;
1777 trunk = ifp->if_vlantrunk;
1778 if (trunk == NULL) {
1782 VLAN_FOREACH(ifv, trunk)
1783 vlan_capabilities(ifv);
1788 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1794 struct ifvlantrunk *trunk;
1798 ifr = (struct ifreq *)data;
1799 ifa = (struct ifaddr *) data;
1800 ifv = ifp->if_softc;
1804 ifp->if_flags |= IFF_UP;
1806 if (ifa->ifa_addr->sa_family == AF_INET)
1807 arp_ifinit(ifp, ifa);
1811 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
1816 if (TRUNK(ifv) != NULL) {
1819 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1821 /* Limit the result to the parent's current config. */
1823 struct ifmediareq *ifmr;
1825 ifmr = (struct ifmediareq *)data;
1826 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1827 ifmr->ifm_count = 1;
1828 error = copyout(&ifmr->ifm_current,
1845 * Set the interface MTU.
1849 if (trunk != NULL) {
1852 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1854 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1857 ifp->if_mtu = ifr->ifr_mtu;
1858 TRUNK_WUNLOCK(trunk);
1867 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
1868 * interface to be delegated to a jail without allowing the
1869 * jail to change what underlying interface/VID it is
1870 * associated with. We are not entirely convinced that this
1871 * is the right way to accomplish that policy goal.
1873 if (ifp->if_vnet != ifp->if_home_vnet) {
1878 error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
1881 if (vlr.vlr_parent[0] == '\0') {
1885 p = ifunit_ref(vlr.vlr_parent);
1890 error = vlan_config(ifv, p, vlr.vlr_tag);
1896 if (ifp->if_vnet != ifp->if_home_vnet) {
1901 bzero(&vlr, sizeof(vlr));
1903 if (TRUNK(ifv) != NULL) {
1904 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1905 sizeof(vlr.vlr_parent));
1906 vlr.vlr_tag = ifv->ifv_vid;
1909 error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
1914 * We should propagate selected flags to the parent,
1915 * e.g., promiscuous mode.
1918 if (TRUNK(ifv) != NULL)
1919 error = vlan_setflags(ifp, 1);
1926 * If we don't have a parent, just remember the membership for
1929 * XXX We need the rmlock here to avoid sleeping while
1930 * holding in6_multi_mtx.
1935 error = vlan_setmulti(ifp);
1941 if (ifp->if_vnet != ifp->if_home_vnet) {
1946 ifr->ifr_vlan_pcp = ifv->ifv_pcp;
1951 if (ifp->if_vnet != ifp->if_home_vnet) {
1956 error = priv_check(curthread, PRIV_NET_SETVLANPCP);
1959 if (ifr->ifr_vlan_pcp > 7) {
1963 ifv->ifv_pcp = ifr->ifr_vlan_pcp;
1964 ifp->if_pcp = ifv->ifv_pcp;
1965 vlan_tag_recalculate(ifv);
1966 /* broadcast event about PCP change */
1967 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
1972 ifv->ifv_capenable = ifr->ifr_reqcap;
1974 if (trunk != NULL) {
1975 struct epoch_tracker et;
1977 NET_EPOCH_ENTER(et);
1978 vlan_capabilities(ifv);
1992 #if defined(KERN_TLS) || defined(RATELIMIT)
1994 vlan_snd_tag_alloc(struct ifnet *ifp,
1995 union if_snd_tag_alloc_params *params,
1996 struct m_snd_tag **ppmt)
1998 struct epoch_tracker et;
1999 struct vlan_snd_tag *vst;
2001 struct ifnet *parent;
2004 NET_EPOCH_ENTER(et);
2005 ifv = ifp->if_softc;
2006 if (ifv->ifv_trunk != NULL)
2007 parent = PARENT(ifv);
2010 if (parent == NULL || parent->if_snd_tag_alloc == NULL) {
2012 return (EOPNOTSUPP);
2017 vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2023 error = parent->if_snd_tag_alloc(parent, params, &vst->tag);
2030 m_snd_tag_init(&vst->com, ifp);
2037 vlan_snd_tag_modify(struct m_snd_tag *mst,
2038 union if_snd_tag_modify_params *params)
2040 struct vlan_snd_tag *vst;
2042 vst = mst_to_vst(mst);
2043 return (vst->tag->ifp->if_snd_tag_modify(vst->tag, params));
2047 vlan_snd_tag_query(struct m_snd_tag *mst,
2048 union if_snd_tag_query_params *params)
2050 struct vlan_snd_tag *vst;
2052 vst = mst_to_vst(mst);
2053 return (vst->tag->ifp->if_snd_tag_query(vst->tag, params));
2057 vlan_snd_tag_free(struct m_snd_tag *mst)
2059 struct vlan_snd_tag *vst;
2061 vst = mst_to_vst(mst);
2062 m_snd_tag_rele(vst->tag);