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>
47 #include "opt_inet6.h"
48 #include "opt_kern_tls.h"
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>
77 #include <net/route.h>
81 #include <netinet/in.h>
82 #include <netinet/if_ether.h>
87 * XXX: declare here to avoid to include many inet6 related files..
88 * should be more generalized?
90 extern void nd6_setmtu(struct ifnet *);
93 #define VLAN_DEF_HWIDTH 4
94 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
96 #define UP_AND_RUNNING(ifp) \
97 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
99 CK_SLIST_HEAD(ifvlanhead, ifvlan);
102 struct ifnet *parent; /* parent interface of this trunk */
105 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
106 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
108 struct ifvlanhead *hash; /* dynamic hash-list table */
115 #if defined(KERN_TLS) || defined(RATELIMIT)
116 struct vlan_snd_tag {
117 struct m_snd_tag com;
118 struct m_snd_tag *tag;
121 static inline struct vlan_snd_tag *
122 mst_to_vst(struct m_snd_tag *mst)
125 return (__containerof(mst, struct vlan_snd_tag, com));
130 * This macro provides a facility to iterate over every vlan on a trunk with
131 * the assumption that none will be added/removed during iteration.
134 #define VLAN_FOREACH(_ifv, _trunk) \
136 for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
137 if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
138 #else /* VLAN_ARRAY */
139 #define VLAN_FOREACH(_ifv, _trunk) \
140 struct ifvlan *_next; \
142 for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
143 CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
144 #endif /* VLAN_ARRAY */
147 * This macro provides a facility to iterate over every vlan on a trunk while
148 * also modifying the number of vlans on the trunk. The iteration continues
149 * until some condition is met or there are no more vlans on the trunk.
152 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
153 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
155 for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
156 if (((_ifv) = (_trunk)->vlans[_i]))
157 #else /* VLAN_ARRAY */
159 * The hash table case is more complicated. We allow for the hash table to be
160 * modified (i.e. vlans removed) while we are iterating over it. To allow for
161 * this we must restart the iteration every time we "touch" something during
162 * the iteration, since removal will resize the hash table and invalidate our
163 * current position. If acting on the touched element causes the trunk to be
164 * emptied, then iteration also stops.
166 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
168 bool _touch = false; \
170 !(_cond) && _i < (1 << (_trunk)->hwidth); \
171 _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
172 if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
174 #endif /* VLAN_ARRAY */
176 struct vlan_mc_entry {
177 struct sockaddr_dl mc_addr;
178 CK_SLIST_ENTRY(vlan_mc_entry) mc_entries;
179 struct epoch_context mc_epoch_ctx;
183 struct ifvlantrunk *ifv_trunk;
184 struct ifnet *ifv_ifp;
185 #define TRUNK(ifv) ((ifv)->ifv_trunk)
186 #define PARENT(ifv) (TRUNK(ifv)->parent)
188 int ifv_pflags; /* special flags we have set on parent */
190 int ifv_encaplen; /* encapsulation length */
191 int ifv_mtufudge; /* MTU fudged by this much */
192 int ifv_mintu; /* min transmission unit */
193 struct ether_8021q_tag ifv_qtag;
194 #define ifv_proto ifv_qtag.proto
195 #define ifv_vid ifv_qtag.vid
196 #define ifv_pcp ifv_qtag.pcp
197 struct task lladdr_task;
198 CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
200 CK_SLIST_ENTRY(ifvlan) ifv_list;
204 /* Special flags we should propagate to parent. */
207 int (*func)(struct ifnet *, int);
209 {IFF_PROMISC, ifpromisc},
210 {IFF_ALLMULTI, if_allmulti},
214 extern int vlan_mtag_pcp;
216 static const char vlanname[] = "vlan";
217 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
219 static eventhandler_tag ifdetach_tag;
220 static eventhandler_tag iflladdr_tag;
221 static eventhandler_tag ifevent_tag;
224 * if_vlan uses two module-level synchronizations primitives to allow concurrent
225 * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
226 * while they are being used for tx/rx. To accomplish this in a way that has
227 * acceptable performance and cooperation with other parts of the network stack
228 * there is a non-sleepable epoch(9) and an sx(9).
230 * The performance-sensitive paths that warrant using the epoch(9) are
231 * vlan_transmit and vlan_input. Both have to check for the vlan interface's
232 * existence using if_vlantrunk, and being in the network tx/rx paths the use
233 * of an epoch(9) gives a measureable improvement in performance.
235 * The reason for having an sx(9) is mostly because there are still areas that
236 * must be sleepable and also have safe concurrent access to a vlan interface.
237 * Since the sx(9) exists, it is used by default in most paths unless sleeping
238 * is not permitted, or if it is not clear whether sleeping is permitted.
241 #define _VLAN_SX_ID ifv_sx
243 static struct sx _VLAN_SX_ID;
245 #define VLAN_LOCKING_INIT() \
246 sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE)
248 #define VLAN_LOCKING_DESTROY() \
249 sx_destroy(&_VLAN_SX_ID)
251 #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID)
252 #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID)
253 #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID)
254 #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID)
255 #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
256 #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
257 #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED)
260 * We also have a per-trunk mutex that should be acquired when changing
263 #define TRUNK_LOCK_INIT(trunk) mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
264 #define TRUNK_LOCK_DESTROY(trunk) mtx_destroy(&(trunk)->lock)
265 #define TRUNK_WLOCK(trunk) mtx_lock(&(trunk)->lock)
266 #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock)
267 #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED);
270 * The VLAN_ARRAY substitutes the dynamic hash with a static array
271 * with 4096 entries. In theory this can give a boost in processing,
272 * however in practice it does not. Probably this is because the array
273 * is too big to fit into CPU cache.
276 static void vlan_inithash(struct ifvlantrunk *trunk);
277 static void vlan_freehash(struct ifvlantrunk *trunk);
278 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
279 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
280 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
281 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
284 static void trunk_destroy(struct ifvlantrunk *trunk);
286 static void vlan_init(void *foo);
287 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
288 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
289 #if defined(KERN_TLS) || defined(RATELIMIT)
290 static int vlan_snd_tag_alloc(struct ifnet *,
291 union if_snd_tag_alloc_params *, struct m_snd_tag **);
292 static int vlan_snd_tag_modify(struct m_snd_tag *,
293 union if_snd_tag_modify_params *);
294 static int vlan_snd_tag_query(struct m_snd_tag *,
295 union if_snd_tag_query_params *);
296 static void vlan_snd_tag_free(struct m_snd_tag *);
297 static struct m_snd_tag *vlan_next_snd_tag(struct m_snd_tag *);
298 static void vlan_ratelimit_query(struct ifnet *,
299 struct if_ratelimit_query_results *);
301 static void vlan_qflush(struct ifnet *ifp);
302 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
303 int (*func)(struct ifnet *, int));
304 static int vlan_setflags(struct ifnet *ifp, int status);
305 static int vlan_setmulti(struct ifnet *ifp);
306 static int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
308 static void vlan_altq_start(struct ifnet *ifp);
309 static int vlan_altq_transmit(struct ifnet *ifp, struct mbuf *m);
311 static int vlan_output(struct ifnet *ifp, struct mbuf *m,
312 const struct sockaddr *dst, struct route *ro);
313 static void vlan_unconfig(struct ifnet *ifp);
314 static void vlan_unconfig_locked(struct ifnet *ifp, int departing);
315 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag,
317 static void vlan_link_state(struct ifnet *ifp);
318 static void vlan_capabilities(struct ifvlan *ifv);
319 static void vlan_trunk_capabilities(struct ifnet *ifp);
321 static struct ifnet *vlan_clone_match_ethervid(const char *, int *);
322 static int vlan_clone_match(struct if_clone *, const char *);
323 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
324 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
326 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
327 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
328 static void vlan_ifevent(void *arg, struct ifnet *ifp, int event);
330 static void vlan_lladdr_fn(void *arg, int pending);
332 static struct if_clone *vlan_cloner;
335 VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
336 #define V_vlan_cloner VNET(vlan_cloner)
340 vlan_mc_free(struct epoch_context *ctx)
342 struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
347 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
350 vlan_inithash(struct ifvlantrunk *trunk)
355 * The trunk must not be locked here since we call malloc(M_WAITOK).
356 * It is OK in case this function is called before the trunk struct
357 * gets hooked up and becomes visible from other threads.
360 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
361 ("%s: hash already initialized", __func__));
363 trunk->hwidth = VLAN_DEF_HWIDTH;
364 n = 1 << trunk->hwidth;
365 trunk->hmask = n - 1;
366 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
367 for (i = 0; i < n; i++)
368 CK_SLIST_INIT(&trunk->hash[i]);
372 vlan_freehash(struct ifvlantrunk *trunk)
377 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
378 for (i = 0; i < (1 << trunk->hwidth); i++)
379 KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
380 ("%s: hash table not empty", __func__));
382 free(trunk->hash, M_VLAN);
384 trunk->hwidth = trunk->hmask = 0;
388 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
394 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
396 b = 1 << trunk->hwidth;
397 i = HASH(ifv->ifv_vid, trunk->hmask);
398 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
399 if (ifv->ifv_vid == ifv2->ifv_vid)
403 * Grow the hash when the number of vlans exceeds half of the number of
404 * hash buckets squared. This will make the average linked-list length
407 if (trunk->refcnt > (b * b) / 2) {
408 vlan_growhash(trunk, 1);
409 i = HASH(ifv->ifv_vid, trunk->hmask);
411 CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
418 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
424 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
426 b = 1 << (trunk->hwidth - 1);
427 i = HASH(ifv->ifv_vid, trunk->hmask);
428 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
431 CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
432 if (trunk->refcnt < (b * b) / 2)
433 vlan_growhash(trunk, -1);
437 panic("%s: vlan not found\n", __func__);
438 return (ENOENT); /*NOTREACHED*/
442 * Grow the hash larger or smaller if memory permits.
445 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
448 struct ifvlanhead *hash2;
449 int hwidth2, i, j, n, n2;
452 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
455 /* Harmless yet obvious coding error */
456 printf("%s: howmuch is 0\n", __func__);
460 hwidth2 = trunk->hwidth + howmuch;
461 n = 1 << trunk->hwidth;
463 /* Do not shrink the table below the default */
464 if (hwidth2 < VLAN_DEF_HWIDTH)
467 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
469 printf("%s: out of memory -- hash size not changed\n",
471 return; /* We can live with the old hash table */
473 for (j = 0; j < n2; j++)
474 CK_SLIST_INIT(&hash2[j]);
475 for (i = 0; i < n; i++)
476 while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
477 CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
478 j = HASH(ifv->ifv_vid, n2 - 1);
479 CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
482 free(trunk->hash, M_VLAN);
484 trunk->hwidth = hwidth2;
485 trunk->hmask = n2 - 1;
488 if_printf(trunk->parent,
489 "VLAN hash table resized from %d to %d buckets\n", n, n2);
492 static __inline struct ifvlan *
493 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
499 CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
500 if (ifv->ifv_vid == vid)
506 /* Debugging code to view the hashtables. */
508 vlan_dumphash(struct ifvlantrunk *trunk)
513 for (i = 0; i < (1 << trunk->hwidth); i++) {
515 CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
516 printf("%s ", ifv->ifv_ifp->if_xname);
523 static __inline struct ifvlan *
524 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
527 return trunk->vlans[vid];
531 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
534 if (trunk->vlans[ifv->ifv_vid] != NULL)
536 trunk->vlans[ifv->ifv_vid] = ifv;
543 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
546 trunk->vlans[ifv->ifv_vid] = NULL;
553 vlan_freehash(struct ifvlantrunk *trunk)
558 vlan_inithash(struct ifvlantrunk *trunk)
562 #endif /* !VLAN_ARRAY */
565 trunk_destroy(struct ifvlantrunk *trunk)
569 vlan_freehash(trunk);
570 trunk->parent->if_vlantrunk = NULL;
571 TRUNK_LOCK_DESTROY(trunk);
572 if_rele(trunk->parent);
577 * Program our multicast filter. What we're actually doing is
578 * programming the multicast filter of the parent. This has the
579 * side effect of causing the parent interface to receive multicast
580 * traffic that it doesn't really want, which ends up being discarded
581 * later by the upper protocol layers. Unfortunately, there's no way
582 * to avoid this: there really is only one physical interface.
585 vlan_setmulti(struct ifnet *ifp)
588 struct ifmultiaddr *ifma;
590 struct vlan_mc_entry *mc;
595 /* Find the parent. */
599 CURVNET_SET_QUIET(ifp_p->if_vnet);
601 /* First, remove any existing filter entries. */
602 while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
603 CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
604 (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
605 NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
608 /* Now program new ones. */
610 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
611 if (ifma->ifma_addr->sa_family != AF_LINK)
613 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
615 IF_ADDR_WUNLOCK(ifp);
619 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
620 mc->mc_addr.sdl_index = ifp_p->if_index;
621 CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
623 IF_ADDR_WUNLOCK(ifp);
624 CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
625 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
638 * A handler for interface ifnet events.
641 vlan_ifevent(void *arg __unused, struct ifnet *ifp, int event)
643 struct epoch_tracker et;
645 struct ifvlantrunk *trunk;
647 if (event != IFNET_EVENT_UPDATE_BAUDRATE)
651 trunk = ifp->if_vlantrunk;
658 VLAN_FOREACH(ifv, trunk) {
659 ifv->ifv_ifp->if_baudrate = ifp->if_baudrate;
661 TRUNK_WUNLOCK(trunk);
666 * A handler for parent interface link layer address changes.
667 * If the parent interface link layer address is changed we
668 * should also change it on all children vlans.
671 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
673 struct epoch_tracker et;
675 struct ifnet *ifv_ifp;
676 struct ifvlantrunk *trunk;
677 struct sockaddr_dl *sdl;
679 /* Need the epoch since this is run on taskqueue_swi. */
681 trunk = ifp->if_vlantrunk;
688 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
689 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
690 * ioctl calls on the parent garbling the lladdr of the child vlan.
693 VLAN_FOREACH(ifv, trunk) {
695 * Copy new new lladdr into the ifv_ifp, enqueue a task
696 * to actually call if_setlladdr. if_setlladdr needs to
697 * be deferred to a taskqueue because it will call into
698 * the if_vlan ioctl path and try to acquire the global
701 ifv_ifp = ifv->ifv_ifp;
702 bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
704 sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
705 sdl->sdl_alen = ifp->if_addrlen;
706 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
708 TRUNK_WUNLOCK(trunk);
713 * A handler for network interface departure events.
714 * Track departure of trunks here so that we don't access invalid
715 * pointers or whatever if a trunk is ripped from under us, e.g.,
716 * by ejecting its hot-plug card. However, if an ifnet is simply
717 * being renamed, then there's no need to tear down the state.
720 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
723 struct ifvlantrunk *trunk;
725 /* If the ifnet is just being renamed, don't do anything. */
726 if (ifp->if_flags & IFF_RENAMING)
729 trunk = ifp->if_vlantrunk;
736 * OK, it's a trunk. Loop over and detach all vlan's on it.
737 * Check trunk pointer after each vlan_unconfig() as it will
738 * free it and set to NULL after the last vlan was detached.
740 VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
741 ifp->if_vlantrunk == NULL)
742 vlan_unconfig_locked(ifv->ifv_ifp, 1);
744 /* Trunk should have been destroyed in vlan_unconfig(). */
745 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
750 * Return the trunk device for a virtual interface.
752 static struct ifnet *
753 vlan_trunkdev(struct ifnet *ifp)
759 if (ifp->if_type != IFT_L2VLAN)
770 * Return the 12-bit VLAN VID for this interface, for use by external
771 * components such as Infiniband.
773 * XXXRW: Note that the function name here is historical; it should be named
777 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
781 if (ifp->if_type != IFT_L2VLAN)
784 *vidp = ifv->ifv_vid;
789 vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
793 if (ifp->if_type != IFT_L2VLAN)
796 *pcpp = ifv->ifv_pcp;
801 * Return a driver specific cookie for this interface. Synchronization
802 * with setcookie must be provided by the driver.
805 vlan_cookie(struct ifnet *ifp)
809 if (ifp->if_type != IFT_L2VLAN)
812 return (ifv->ifv_cookie);
816 * Store a cookie in our softc that drivers can use to store driver
817 * private per-instance data in.
820 vlan_setcookie(struct ifnet *ifp, void *cookie)
824 if (ifp->if_type != IFT_L2VLAN)
827 ifv->ifv_cookie = cookie;
832 * Return the vlan device present at the specific VID.
834 static struct ifnet *
835 vlan_devat(struct ifnet *ifp, uint16_t vid)
837 struct ifvlantrunk *trunk;
842 trunk = ifp->if_vlantrunk;
846 ifv = vlan_gethash(trunk, vid);
853 * VLAN support can be loaded as a module. The only place in the
854 * system that's intimately aware of this is ether_input. We hook
855 * into this code through vlan_input_p which is defined there and
856 * set here. No one else in the system should be aware of this so
857 * we use an explicit reference here.
859 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
861 /* For if_link_state_change() eyes only... */
862 extern void (*vlan_link_state_p)(struct ifnet *);
865 vlan_modevent(module_t mod, int type, void *data)
870 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
871 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
872 if (ifdetach_tag == NULL)
874 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
875 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
876 if (iflladdr_tag == NULL)
878 ifevent_tag = EVENTHANDLER_REGISTER(ifnet_event,
879 vlan_ifevent, NULL, EVENTHANDLER_PRI_ANY);
880 if (ifevent_tag == NULL)
883 vlan_input_p = vlan_input;
884 vlan_link_state_p = vlan_link_state;
885 vlan_trunk_cap_p = vlan_trunk_capabilities;
886 vlan_trunkdev_p = vlan_trunkdev;
887 vlan_cookie_p = vlan_cookie;
888 vlan_setcookie_p = vlan_setcookie;
889 vlan_tag_p = vlan_tag;
890 vlan_pcp_p = vlan_pcp;
891 vlan_devat_p = vlan_devat;
893 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
894 vlan_clone_create, vlan_clone_destroy);
897 printf("vlan: initialized, using "
901 "hash tables with chaining"
908 if_clone_detach(vlan_cloner);
910 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
911 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
912 EVENTHANDLER_DEREGISTER(ifnet_event, ifevent_tag);
914 vlan_link_state_p = NULL;
915 vlan_trunk_cap_p = NULL;
916 vlan_trunkdev_p = NULL;
918 vlan_cookie_p = NULL;
919 vlan_setcookie_p = NULL;
921 VLAN_LOCKING_DESTROY();
923 printf("vlan: unloaded\n");
931 static moduledata_t vlan_mod = {
937 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
938 MODULE_VERSION(if_vlan, 3);
942 vnet_vlan_init(const void *unused __unused)
945 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
946 vlan_clone_create, vlan_clone_destroy);
947 V_vlan_cloner = vlan_cloner;
949 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
950 vnet_vlan_init, NULL);
953 vnet_vlan_uninit(const void *unused __unused)
956 if_clone_detach(V_vlan_cloner);
958 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
959 vnet_vlan_uninit, NULL);
963 * Check for <etherif>.<vlan>[.<vlan> ...] style interface names.
965 static struct ifnet *
966 vlan_clone_match_ethervid(const char *name, int *vidp)
968 char ifname[IFNAMSIZ];
973 strlcpy(ifname, name, IFNAMSIZ);
974 if ((cp = strrchr(ifname, '.')) == NULL)
977 if ((ifp = ifunit_ref(ifname)) == NULL)
985 for(; *cp >= '0' && *cp <= '9'; cp++)
986 vid = (vid * 10) + (*cp - '0');
998 vlan_clone_match(struct if_clone *ifc, const char *name)
1003 ifp = vlan_clone_match_ethervid(name, NULL);
1009 if (strncmp(vlanname, name, strlen(vlanname)) != 0)
1011 for (cp = name + 4; *cp != '\0'; cp++) {
1012 if (*cp < '0' || *cp > '9')
1020 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
1023 bool wildcard = false;
1024 bool subinterface = false;
1028 uint16_t proto = ETHERTYPE_VLAN;
1031 struct ifnet *p = NULL;
1033 struct sockaddr_dl *sdl;
1035 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
1039 * There are three ways to specify the cloned device:
1040 * o pass a parameter block with the clone request.
1041 * o specify parameters in the text of the clone device name
1042 * o specify no parameters and get an unattached device that
1043 * must be configured separately.
1044 * The first technique is preferred; the latter two are supported
1045 * for backwards compatibility.
1047 * XXXRW: Note historic use of the word "tag" here. New ioctls may be
1052 error = copyin(params, &vlr, sizeof(vlr));
1056 proto = vlr.vlr_proto;
1058 #ifdef COMPAT_FREEBSD12
1060 proto = ETHERTYPE_VLAN;
1062 p = ifunit_ref(vlr.vlr_parent);
1067 if ((error = ifc_name2unit(name, &unit)) == 0) {
1070 * vlanX interface. Set wildcard to true if the unit number
1073 wildcard = (unit < 0);
1075 struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid);
1076 if (p_tmp != NULL) {
1078 subinterface = true;
1079 unit = IF_DUNIT_NONE;
1097 if (!subinterface) {
1098 /* vlanX interface, mark X as busy or allocate new unit # */
1099 error = ifc_alloc_unit(ifc, &unit);
1107 /* In the wildcard case, we need to update the name. */
1109 for (dp = name; *dp != '\0'; dp++);
1110 if (snprintf(dp, len - (dp-name), "%d", unit) >
1111 len - (dp-name) - 1) {
1112 panic("%s: interface name too long", __func__);
1116 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1117 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1120 ifc_free_unit(ifc, unit);
1126 CK_SLIST_INIT(&ifv->vlan_mc_listhead);
1127 ifp->if_softc = ifv;
1129 * Set the name manually rather than using if_initname because
1130 * we don't conform to the default naming convention for interfaces.
1132 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1133 ifp->if_dname = vlanname;
1134 ifp->if_dunit = unit;
1136 ifp->if_init = vlan_init;
1138 ifp->if_start = vlan_altq_start;
1139 ifp->if_transmit = vlan_altq_transmit;
1140 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1141 ifp->if_snd.ifq_drv_maxlen = 0;
1142 IFQ_SET_READY(&ifp->if_snd);
1144 ifp->if_transmit = vlan_transmit;
1146 ifp->if_qflush = vlan_qflush;
1147 ifp->if_ioctl = vlan_ioctl;
1148 #if defined(KERN_TLS) || defined(RATELIMIT)
1149 ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1150 ifp->if_snd_tag_modify = vlan_snd_tag_modify;
1151 ifp->if_snd_tag_query = vlan_snd_tag_query;
1152 ifp->if_snd_tag_free = vlan_snd_tag_free;
1153 ifp->if_next_snd_tag = vlan_next_snd_tag;
1154 ifp->if_ratelimit_query = vlan_ratelimit_query;
1156 ifp->if_flags = VLAN_IFFLAGS;
1157 ether_ifattach(ifp, eaddr);
1158 /* Now undo some of the damage... */
1159 ifp->if_baudrate = 0;
1160 ifp->if_type = IFT_L2VLAN;
1161 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1163 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1164 sdl->sdl_type = IFT_L2VLAN;
1167 error = vlan_config(ifv, p, vid, proto);
1171 * Since we've partially failed, we need to back
1172 * out all the way, otherwise userland could get
1173 * confused. Thus, we destroy the interface.
1175 ether_ifdetach(ifp);
1179 ifc_free_unit(ifc, unit);
1190 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
1192 struct ifvlan *ifv = ifp->if_softc;
1193 int unit = ifp->if_dunit;
1195 if (ifp->if_vlantrunk)
1199 IFQ_PURGE(&ifp->if_snd);
1201 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
1202 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
1204 * We should have the only reference to the ifv now, so we can now
1205 * drain any remaining lladdr task before freeing the ifnet and the
1208 taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1212 if (unit != IF_DUNIT_NONE)
1213 ifc_free_unit(ifc, unit);
1219 * The ifp->if_init entry point for vlan(4) is a no-op.
1222 vlan_init(void *foo __unused)
1227 * The if_transmit method for vlan(4) interface.
1230 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1234 int error, len, mcast;
1238 ifv = ifp->if_softc;
1239 if (TRUNK(ifv) == NULL) {
1240 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1245 len = m->m_pkthdr.len;
1246 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1250 #if defined(KERN_TLS) || defined(RATELIMIT)
1251 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1252 struct vlan_snd_tag *vst;
1253 struct m_snd_tag *mst;
1255 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1256 mst = m->m_pkthdr.snd_tag;
1257 vst = mst_to_vst(mst);
1258 if (vst->tag->ifp != p) {
1259 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1264 m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1265 m_snd_tag_rele(mst);
1270 * Do not run parent's if_transmit() if the parent is not up,
1271 * or parent's driver will cause a system crash.
1273 if (!UP_AND_RUNNING(p)) {
1274 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1279 if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) {
1280 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1285 * Send it, precisely as ether_output() would have.
1287 error = (p->if_transmit)(p, m);
1289 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1290 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1291 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1293 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1298 vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1307 * Find the first non-VLAN parent interface.
1309 ifv = ifp->if_softc;
1311 if (TRUNK(ifv) == NULL) {
1317 } while (p->if_type == IFT_L2VLAN);
1319 return p->if_output(ifp, m, dst, ro);
1324 vlan_altq_start(if_t ifp)
1326 struct ifaltq *ifq = &ifp->if_snd;
1330 IFQ_DEQUEUE_NOLOCK(ifq, m);
1332 vlan_transmit(ifp, m);
1333 IFQ_DEQUEUE_NOLOCK(ifq, m);
1339 vlan_altq_transmit(if_t ifp, struct mbuf *m)
1343 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
1344 IFQ_ENQUEUE(&ifp->if_snd, m, err);
1346 vlan_altq_start(ifp);
1348 err = vlan_transmit(ifp, m);
1355 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1358 vlan_qflush(struct ifnet *ifp __unused)
1363 vlan_input(struct ifnet *ifp, struct mbuf *m)
1365 struct ifvlantrunk *trunk;
1372 trunk = ifp->if_vlantrunk;
1373 if (trunk == NULL) {
1378 if (m->m_flags & M_VLANTAG) {
1380 * Packet is tagged, but m contains a normal
1381 * Ethernet frame; the tag is stored out-of-band.
1383 tag = m->m_pkthdr.ether_vtag;
1384 m->m_flags &= ~M_VLANTAG;
1386 struct ether_vlan_header *evl;
1389 * Packet is tagged in-band as specified by 802.1q.
1391 switch (ifp->if_type) {
1393 if (m->m_len < sizeof(*evl) &&
1394 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1395 if_printf(ifp, "cannot pullup VLAN header\n");
1398 evl = mtod(m, struct ether_vlan_header *);
1399 tag = ntohs(evl->evl_tag);
1402 * Remove the 802.1q header by copying the Ethernet
1403 * addresses over it and adjusting the beginning of
1404 * the data in the mbuf. The encapsulated Ethernet
1405 * type field is already in place.
1407 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1408 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1409 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1414 panic("%s: %s has unsupported if_type %u",
1415 __func__, ifp->if_xname, ifp->if_type);
1417 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1423 vid = EVL_VLANOFTAG(tag);
1425 ifv = vlan_gethash(trunk, vid);
1426 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1427 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1432 if (vlan_mtag_pcp) {
1434 * While uncommon, it is possible that we will find a 802.1q
1435 * packet encapsulated inside another packet that also had an
1436 * 802.1q header. For example, ethernet tunneled over IPSEC
1437 * arriving over ethernet. In that case, we replace the
1438 * existing 802.1q PCP m_tag value.
1440 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1442 mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1443 sizeof(uint8_t), M_NOWAIT);
1445 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1449 m_tag_prepend(m, mtag);
1451 *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1454 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1455 if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1457 /* Pass it back through the parent's input routine. */
1458 (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1462 vlan_lladdr_fn(void *arg, int pending __unused)
1467 ifv = (struct ifvlan *)arg;
1470 CURVNET_SET(ifp->if_vnet);
1472 /* The ifv_ifp already has the lladdr copied in. */
1473 if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1479 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid,
1482 struct epoch_tracker et;
1483 struct ifvlantrunk *trunk;
1488 * We can handle non-ethernet hardware types as long as
1489 * they handle the tagging and headers themselves.
1491 if (p->if_type != IFT_ETHER &&
1492 p->if_type != IFT_L2VLAN &&
1493 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1494 return (EPROTONOSUPPORT);
1495 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1496 return (EPROTONOSUPPORT);
1498 * Don't let the caller set up a VLAN VID with
1499 * anything except VLID bits.
1500 * VID numbers 0x0 and 0xFFF are reserved.
1502 if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1508 if (p->if_vlantrunk == NULL) {
1509 trunk = malloc(sizeof(struct ifvlantrunk),
1510 M_VLAN, M_WAITOK | M_ZERO);
1511 vlan_inithash(trunk);
1512 TRUNK_LOCK_INIT(trunk);
1514 p->if_vlantrunk = trunk;
1516 if_ref(trunk->parent);
1517 TRUNK_WUNLOCK(trunk);
1519 trunk = p->if_vlantrunk;
1522 ifv->ifv_vid = vid; /* must set this before vlan_inshash() */
1523 ifv->ifv_pcp = 0; /* Default: best effort delivery. */
1524 error = vlan_inshash(trunk, ifv);
1527 ifv->ifv_proto = proto;
1528 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1529 ifv->ifv_mintu = ETHERMIN;
1530 ifv->ifv_pflags = 0;
1531 ifv->ifv_capenable = -1;
1534 * If the parent supports the VLAN_MTU capability,
1535 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1538 if (p->if_capenable & IFCAP_VLAN_MTU) {
1540 * No need to fudge the MTU since the parent can
1541 * handle extended frames.
1543 ifv->ifv_mtufudge = 0;
1546 * Fudge the MTU by the encapsulation size. This
1547 * makes us incompatible with strictly compliant
1548 * 802.1Q implementations, but allows us to use
1549 * the feature with other NetBSD implementations,
1550 * which might still be useful.
1552 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1555 ifv->ifv_trunk = trunk;
1558 * Initialize fields from our parent. This duplicates some
1559 * work with ether_ifattach() but allows for non-ethernet
1560 * interfaces to also work.
1562 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1563 ifp->if_baudrate = p->if_baudrate;
1564 ifp->if_input = p->if_input;
1565 ifp->if_resolvemulti = p->if_resolvemulti;
1566 ifp->if_addrlen = p->if_addrlen;
1567 ifp->if_broadcastaddr = p->if_broadcastaddr;
1568 ifp->if_pcp = ifv->ifv_pcp;
1571 * We wrap the parent's if_output using vlan_output to ensure that it
1572 * can't become stale.
1574 ifp->if_output = vlan_output;
1577 * Copy only a selected subset of flags from the parent.
1578 * Other flags are none of our business.
1580 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1581 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1582 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1583 #undef VLAN_COPY_FLAGS
1585 ifp->if_link_state = p->if_link_state;
1587 NET_EPOCH_ENTER(et);
1588 vlan_capabilities(ifv);
1592 * Set up our interface address to reflect the underlying
1593 * physical interface's.
1595 TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1596 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1600 * Do not schedule link address update if it was the same
1601 * as previous parent's. This helps avoid updating for each
1602 * associated llentry.
1604 if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) {
1605 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1606 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
1609 /* We are ready for operation now. */
1610 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1612 /* Update flags on the parent, if necessary. */
1613 vlan_setflags(ifp, 1);
1616 * Configure multicast addresses that may already be
1617 * joined on the vlan device.
1619 (void)vlan_setmulti(ifp);
1623 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1630 vlan_unconfig(struct ifnet *ifp)
1634 vlan_unconfig_locked(ifp, 0);
1639 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1641 struct ifvlantrunk *trunk;
1642 struct vlan_mc_entry *mc;
1644 struct ifnet *parent;
1647 VLAN_XLOCK_ASSERT();
1649 ifv = ifp->if_softc;
1650 trunk = ifv->ifv_trunk;
1653 if (trunk != NULL) {
1654 parent = trunk->parent;
1657 * Since the interface is being unconfigured, we need to
1658 * empty the list of multicast groups that we may have joined
1659 * while we were alive from the parent's list.
1661 while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1663 * If the parent interface is being detached,
1664 * all its multicast addresses have already
1665 * been removed. Warn about errors if
1666 * if_delmulti() does fail, but don't abort as
1667 * all callers expect vlan destruction to
1671 error = if_delmulti(parent,
1672 (struct sockaddr *)&mc->mc_addr);
1675 "Failed to delete multicast address from parent: %d\n",
1678 CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1679 NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
1682 vlan_setflags(ifp, 0); /* clear special flags on parent */
1684 vlan_remhash(trunk, ifv);
1685 ifv->ifv_trunk = NULL;
1688 * Check if we were the last.
1690 if (trunk->refcnt == 0) {
1691 parent->if_vlantrunk = NULL;
1693 trunk_destroy(trunk);
1697 /* Disconnect from parent. */
1698 if (ifv->ifv_pflags)
1699 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1700 ifp->if_mtu = ETHERMTU;
1701 ifp->if_link_state = LINK_STATE_UNKNOWN;
1702 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1705 * Only dispatch an event if vlan was
1706 * attached, otherwise there is nothing
1707 * to cleanup anyway.
1710 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1713 /* Handle a reference counted flag that should be set on the parent as well */
1715 vlan_setflag(struct ifnet *ifp, int flag, int status,
1716 int (*func)(struct ifnet *, int))
1721 VLAN_SXLOCK_ASSERT();
1723 ifv = ifp->if_softc;
1724 status = status ? (ifp->if_flags & flag) : 0;
1725 /* Now "status" contains the flag value or 0 */
1728 * See if recorded parent's status is different from what
1729 * we want it to be. If it is, flip it. We record parent's
1730 * status in ifv_pflags so that we won't clear parent's flag
1731 * we haven't set. In fact, we don't clear or set parent's
1732 * flags directly, but get or release references to them.
1733 * That's why we can be sure that recorded flags still are
1734 * in accord with actual parent's flags.
1736 if (status != (ifv->ifv_pflags & flag)) {
1737 error = (*func)(PARENT(ifv), status);
1740 ifv->ifv_pflags &= ~flag;
1741 ifv->ifv_pflags |= status;
1747 * Handle IFF_* flags that require certain changes on the parent:
1748 * if "status" is true, update parent's flags respective to our if_flags;
1749 * if "status" is false, forcedly clear the flags set on parent.
1752 vlan_setflags(struct ifnet *ifp, int status)
1756 for (i = 0; vlan_pflags[i].flag; i++) {
1757 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1758 status, vlan_pflags[i].func);
1765 /* Inform all vlans that their parent has changed link state */
1767 vlan_link_state(struct ifnet *ifp)
1769 struct epoch_tracker et;
1770 struct ifvlantrunk *trunk;
1773 NET_EPOCH_ENTER(et);
1774 trunk = ifp->if_vlantrunk;
1775 if (trunk == NULL) {
1781 VLAN_FOREACH(ifv, trunk) {
1782 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1783 if_link_state_change(ifv->ifv_ifp,
1784 trunk->parent->if_link_state);
1786 TRUNK_WUNLOCK(trunk);
1791 vlan_capabilities(struct ifvlan *ifv)
1795 struct ifnet_hw_tsomax hw_tsomax;
1796 int cap = 0, ena = 0, mena;
1800 VLAN_SXLOCK_ASSERT();
1805 /* Mask parent interface enabled capabilities disabled by user. */
1806 mena = p->if_capenable & ifv->ifv_capenable;
1809 * If the parent interface can do checksum offloading
1810 * on VLANs, then propagate its hardware-assisted
1811 * checksumming flags. Also assert that checksum
1812 * offloading requires hardware VLAN tagging.
1814 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1815 cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1816 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1817 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1818 ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1819 if (ena & IFCAP_TXCSUM)
1820 hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1821 CSUM_UDP | CSUM_SCTP);
1822 if (ena & IFCAP_TXCSUM_IPV6)
1823 hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1824 CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
1828 * If the parent interface can do TSO on VLANs then
1829 * propagate the hardware-assisted flag. TSO on VLANs
1830 * does not necessarily require hardware VLAN tagging.
1832 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
1833 if_hw_tsomax_common(p, &hw_tsomax);
1834 if_hw_tsomax_update(ifp, &hw_tsomax);
1835 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1836 cap |= p->if_capabilities & IFCAP_TSO;
1837 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1838 ena |= mena & IFCAP_TSO;
1839 if (ena & IFCAP_TSO)
1840 hwa |= p->if_hwassist & CSUM_TSO;
1844 * If the parent interface can do LRO and checksum offloading on
1845 * VLANs, then guess it may do LRO on VLANs. False positive here
1846 * cost nothing, while false negative may lead to some confusions.
1848 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1849 cap |= p->if_capabilities & IFCAP_LRO;
1850 if (p->if_capenable & IFCAP_VLAN_HWCSUM)
1851 ena |= mena & IFCAP_LRO;
1854 * If the parent interface can offload TCP connections over VLANs then
1855 * propagate its TOE capability to the VLAN interface.
1857 * All TOE drivers in the tree today can deal with VLANs. If this
1858 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1861 #define IFCAP_VLAN_TOE IFCAP_TOE
1862 if (p->if_capabilities & IFCAP_VLAN_TOE)
1863 cap |= p->if_capabilities & IFCAP_TOE;
1864 if (p->if_capenable & IFCAP_VLAN_TOE) {
1865 TOEDEV(ifp) = TOEDEV(p);
1866 ena |= mena & IFCAP_TOE;
1870 * If the parent interface supports dynamic link state, so does the
1873 cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1874 ena |= (mena & IFCAP_LINKSTATE);
1878 * If the parent interface supports ratelimiting, so does the
1881 cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1882 ena |= (mena & IFCAP_TXRTLMT);
1886 * If the parent interface supports unmapped mbufs, so does
1887 * the VLAN interface. Note that this should be fine even for
1888 * interfaces that don't support hardware tagging as headers
1889 * are prepended in normal mbufs to unmapped mbufs holding
1892 cap |= (p->if_capabilities & IFCAP_MEXTPG);
1893 ena |= (mena & IFCAP_MEXTPG);
1896 * If the parent interface can offload encryption and segmentation
1897 * of TLS records over TCP, propagate it's capability to the VLAN
1900 * All TLS drivers in the tree today can deal with VLANs. If
1901 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
1904 if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
1905 cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
1906 if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
1907 ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
1909 ifp->if_capabilities = cap;
1910 ifp->if_capenable = ena;
1911 ifp->if_hwassist = hwa;
1915 vlan_trunk_capabilities(struct ifnet *ifp)
1917 struct epoch_tracker et;
1918 struct ifvlantrunk *trunk;
1922 trunk = ifp->if_vlantrunk;
1923 if (trunk == NULL) {
1927 NET_EPOCH_ENTER(et);
1928 VLAN_FOREACH(ifv, trunk)
1929 vlan_capabilities(ifv);
1935 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1941 struct ifvlantrunk *trunk;
1943 int error = 0, oldmtu;
1945 ifr = (struct ifreq *)data;
1946 ifa = (struct ifaddr *) data;
1947 ifv = ifp->if_softc;
1951 ifp->if_flags |= IFF_UP;
1953 if (ifa->ifa_addr->sa_family == AF_INET)
1954 arp_ifinit(ifp, ifa);
1958 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
1963 if (TRUNK(ifv) != NULL) {
1966 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1968 /* Limit the result to the parent's current config. */
1970 struct ifmediareq *ifmr;
1972 ifmr = (struct ifmediareq *)data;
1973 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1974 ifmr->ifm_count = 1;
1975 error = copyout(&ifmr->ifm_current,
1992 * Set the interface MTU.
1996 if (trunk != NULL) {
1999 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
2001 (ifv->ifv_mintu - ifv->ifv_mtufudge))
2004 ifp->if_mtu = ifr->ifr_mtu;
2005 TRUNK_WUNLOCK(trunk);
2014 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
2015 * interface to be delegated to a jail without allowing the
2016 * jail to change what underlying interface/VID it is
2017 * associated with. We are not entirely convinced that this
2018 * is the right way to accomplish that policy goal.
2020 if (ifp->if_vnet != ifp->if_home_vnet) {
2025 error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
2028 if (vlr.vlr_parent[0] == '\0') {
2032 p = ifunit_ref(vlr.vlr_parent);
2037 #ifdef COMPAT_FREEBSD12
2038 if (vlr.vlr_proto == 0)
2039 vlr.vlr_proto = ETHERTYPE_VLAN;
2041 oldmtu = ifp->if_mtu;
2042 error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto);
2046 * VLAN MTU may change during addition of the vlandev.
2047 * If it did, do network layer specific procedure.
2049 if (ifp->if_mtu != oldmtu) {
2059 if (ifp->if_vnet != ifp->if_home_vnet) {
2064 bzero(&vlr, sizeof(vlr));
2066 if (TRUNK(ifv) != NULL) {
2067 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
2068 sizeof(vlr.vlr_parent));
2069 vlr.vlr_tag = ifv->ifv_vid;
2070 vlr.vlr_proto = ifv->ifv_proto;
2073 error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
2078 * We should propagate selected flags to the parent,
2079 * e.g., promiscuous mode.
2082 if (TRUNK(ifv) != NULL)
2083 error = vlan_setflags(ifp, 1);
2090 * If we don't have a parent, just remember the membership for
2093 * XXX We need the rmlock here to avoid sleeping while
2094 * holding in6_multi_mtx.
2099 error = vlan_setmulti(ifp);
2105 if (ifp->if_vnet != ifp->if_home_vnet) {
2110 ifr->ifr_vlan_pcp = ifv->ifv_pcp;
2115 if (ifp->if_vnet != ifp->if_home_vnet) {
2120 error = priv_check(curthread, PRIV_NET_SETVLANPCP);
2123 if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) {
2127 ifv->ifv_pcp = ifr->ifr_vlan_pcp;
2128 ifp->if_pcp = ifv->ifv_pcp;
2129 /* broadcast event about PCP change */
2130 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
2135 ifv->ifv_capenable = ifr->ifr_reqcap;
2137 if (trunk != NULL) {
2138 struct epoch_tracker et;
2140 NET_EPOCH_ENTER(et);
2141 vlan_capabilities(ifv);
2155 #if defined(KERN_TLS) || defined(RATELIMIT)
2157 vlan_snd_tag_alloc(struct ifnet *ifp,
2158 union if_snd_tag_alloc_params *params,
2159 struct m_snd_tag **ppmt)
2161 struct epoch_tracker et;
2162 struct vlan_snd_tag *vst;
2164 struct ifnet *parent;
2167 NET_EPOCH_ENTER(et);
2168 ifv = ifp->if_softc;
2169 if (ifv->ifv_trunk != NULL)
2170 parent = PARENT(ifv);
2173 if (parent == NULL) {
2175 return (EOPNOTSUPP);
2180 vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2186 error = m_snd_tag_alloc(parent, params, &vst->tag);
2193 m_snd_tag_init(&vst->com, ifp, vst->tag->type);
2199 static struct m_snd_tag *
2200 vlan_next_snd_tag(struct m_snd_tag *mst)
2202 struct vlan_snd_tag *vst;
2204 vst = mst_to_vst(mst);
2209 vlan_snd_tag_modify(struct m_snd_tag *mst,
2210 union if_snd_tag_modify_params *params)
2212 struct vlan_snd_tag *vst;
2214 vst = mst_to_vst(mst);
2215 return (vst->tag->ifp->if_snd_tag_modify(vst->tag, params));
2219 vlan_snd_tag_query(struct m_snd_tag *mst,
2220 union if_snd_tag_query_params *params)
2222 struct vlan_snd_tag *vst;
2224 vst = mst_to_vst(mst);
2225 return (vst->tag->ifp->if_snd_tag_query(vst->tag, params));
2229 vlan_snd_tag_free(struct m_snd_tag *mst)
2231 struct vlan_snd_tag *vst;
2233 vst = mst_to_vst(mst);
2234 m_snd_tag_rele(vst->tag);
2239 vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
2242 * For vlan, we have an indirect
2243 * interface. The caller needs to
2244 * get a ratelimit tag on the actual
2245 * interface the flow will go on.
2247 q->rate_table = NULL;
2248 q->flags = RT_IS_INDIRECT;
2250 q->number_of_rates = 0;