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 proto = ETHERTYPE_VLAN;
1059 p = ifunit_ref(vlr.vlr_parent);
1064 if ((error = ifc_name2unit(name, &unit)) == 0) {
1067 * vlanX interface. Set wildcard to true if the unit number
1070 wildcard = (unit < 0);
1072 struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid);
1073 if (p_tmp != NULL) {
1075 subinterface = true;
1076 unit = IF_DUNIT_NONE;
1094 if (!subinterface) {
1095 /* vlanX interface, mark X as busy or allocate new unit # */
1096 error = ifc_alloc_unit(ifc, &unit);
1104 /* In the wildcard case, we need to update the name. */
1106 for (dp = name; *dp != '\0'; dp++);
1107 if (snprintf(dp, len - (dp-name), "%d", unit) >
1108 len - (dp-name) - 1) {
1109 panic("%s: interface name too long", __func__);
1113 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1114 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1117 ifc_free_unit(ifc, unit);
1123 CK_SLIST_INIT(&ifv->vlan_mc_listhead);
1124 ifp->if_softc = ifv;
1126 * Set the name manually rather than using if_initname because
1127 * we don't conform to the default naming convention for interfaces.
1129 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1130 ifp->if_dname = vlanname;
1131 ifp->if_dunit = unit;
1133 ifp->if_init = vlan_init;
1135 ifp->if_start = vlan_altq_start;
1136 ifp->if_transmit = vlan_altq_transmit;
1137 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1138 ifp->if_snd.ifq_drv_maxlen = 0;
1139 IFQ_SET_READY(&ifp->if_snd);
1141 ifp->if_transmit = vlan_transmit;
1143 ifp->if_qflush = vlan_qflush;
1144 ifp->if_ioctl = vlan_ioctl;
1145 #if defined(KERN_TLS) || defined(RATELIMIT)
1146 ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1147 ifp->if_snd_tag_modify = vlan_snd_tag_modify;
1148 ifp->if_snd_tag_query = vlan_snd_tag_query;
1149 ifp->if_snd_tag_free = vlan_snd_tag_free;
1150 ifp->if_next_snd_tag = vlan_next_snd_tag;
1151 ifp->if_ratelimit_query = vlan_ratelimit_query;
1153 ifp->if_flags = VLAN_IFFLAGS;
1154 ether_ifattach(ifp, eaddr);
1155 /* Now undo some of the damage... */
1156 ifp->if_baudrate = 0;
1157 ifp->if_type = IFT_L2VLAN;
1158 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1160 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1161 sdl->sdl_type = IFT_L2VLAN;
1164 error = vlan_config(ifv, p, vid, proto);
1168 * Since we've partially failed, we need to back
1169 * out all the way, otherwise userland could get
1170 * confused. Thus, we destroy the interface.
1172 ether_ifdetach(ifp);
1176 ifc_free_unit(ifc, unit);
1187 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
1189 struct ifvlan *ifv = ifp->if_softc;
1190 int unit = ifp->if_dunit;
1192 if (ifp->if_vlantrunk)
1196 IFQ_PURGE(&ifp->if_snd);
1198 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
1199 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
1201 * We should have the only reference to the ifv now, so we can now
1202 * drain any remaining lladdr task before freeing the ifnet and the
1205 taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1209 if (unit != IF_DUNIT_NONE)
1210 ifc_free_unit(ifc, unit);
1216 * The ifp->if_init entry point for vlan(4) is a no-op.
1219 vlan_init(void *foo __unused)
1224 * The if_transmit method for vlan(4) interface.
1227 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1231 int error, len, mcast;
1235 ifv = ifp->if_softc;
1236 if (TRUNK(ifv) == NULL) {
1237 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1242 len = m->m_pkthdr.len;
1243 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1247 #if defined(KERN_TLS) || defined(RATELIMIT)
1248 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1249 struct vlan_snd_tag *vst;
1250 struct m_snd_tag *mst;
1252 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1253 mst = m->m_pkthdr.snd_tag;
1254 vst = mst_to_vst(mst);
1255 if (vst->tag->ifp != p) {
1256 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1261 m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1262 m_snd_tag_rele(mst);
1267 * Do not run parent's if_transmit() if the parent is not up,
1268 * or parent's driver will cause a system crash.
1270 if (!UP_AND_RUNNING(p)) {
1271 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1276 if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) {
1277 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1282 * Send it, precisely as ether_output() would have.
1284 error = (p->if_transmit)(p, m);
1286 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1287 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1288 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1290 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1295 vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1304 * Find the first non-VLAN parent interface.
1306 ifv = ifp->if_softc;
1308 if (TRUNK(ifv) == NULL) {
1314 } while (p->if_type == IFT_L2VLAN);
1316 return p->if_output(ifp, m, dst, ro);
1321 vlan_altq_start(if_t ifp)
1323 struct ifaltq *ifq = &ifp->if_snd;
1327 IFQ_DEQUEUE_NOLOCK(ifq, m);
1329 vlan_transmit(ifp, m);
1330 IFQ_DEQUEUE_NOLOCK(ifq, m);
1336 vlan_altq_transmit(if_t ifp, struct mbuf *m)
1340 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
1341 IFQ_ENQUEUE(&ifp->if_snd, m, err);
1343 vlan_altq_start(ifp);
1345 err = vlan_transmit(ifp, m);
1352 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1355 vlan_qflush(struct ifnet *ifp __unused)
1360 vlan_input(struct ifnet *ifp, struct mbuf *m)
1362 struct ifvlantrunk *trunk;
1369 trunk = ifp->if_vlantrunk;
1370 if (trunk == NULL) {
1375 if (m->m_flags & M_VLANTAG) {
1377 * Packet is tagged, but m contains a normal
1378 * Ethernet frame; the tag is stored out-of-band.
1380 tag = m->m_pkthdr.ether_vtag;
1381 m->m_flags &= ~M_VLANTAG;
1383 struct ether_vlan_header *evl;
1386 * Packet is tagged in-band as specified by 802.1q.
1388 switch (ifp->if_type) {
1390 if (m->m_len < sizeof(*evl) &&
1391 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1392 if_printf(ifp, "cannot pullup VLAN header\n");
1395 evl = mtod(m, struct ether_vlan_header *);
1396 tag = ntohs(evl->evl_tag);
1399 * Remove the 802.1q header by copying the Ethernet
1400 * addresses over it and adjusting the beginning of
1401 * the data in the mbuf. The encapsulated Ethernet
1402 * type field is already in place.
1404 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1405 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1406 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1411 panic("%s: %s has unsupported if_type %u",
1412 __func__, ifp->if_xname, ifp->if_type);
1414 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1420 vid = EVL_VLANOFTAG(tag);
1422 ifv = vlan_gethash(trunk, vid);
1423 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1424 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1429 if (vlan_mtag_pcp) {
1431 * While uncommon, it is possible that we will find a 802.1q
1432 * packet encapsulated inside another packet that also had an
1433 * 802.1q header. For example, ethernet tunneled over IPSEC
1434 * arriving over ethernet. In that case, we replace the
1435 * existing 802.1q PCP m_tag value.
1437 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1439 mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1440 sizeof(uint8_t), M_NOWAIT);
1442 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1446 m_tag_prepend(m, mtag);
1448 *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1451 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1452 if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1454 /* Pass it back through the parent's input routine. */
1455 (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1459 vlan_lladdr_fn(void *arg, int pending __unused)
1464 ifv = (struct ifvlan *)arg;
1467 CURVNET_SET(ifp->if_vnet);
1469 /* The ifv_ifp already has the lladdr copied in. */
1470 if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1476 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid,
1479 struct epoch_tracker et;
1480 struct ifvlantrunk *trunk;
1485 * We can handle non-ethernet hardware types as long as
1486 * they handle the tagging and headers themselves.
1488 if (p->if_type != IFT_ETHER &&
1489 p->if_type != IFT_L2VLAN &&
1490 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1491 return (EPROTONOSUPPORT);
1492 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1493 return (EPROTONOSUPPORT);
1495 * Don't let the caller set up a VLAN VID with
1496 * anything except VLID bits.
1497 * VID numbers 0x0 and 0xFFF are reserved.
1499 if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1505 if (p->if_vlantrunk == NULL) {
1506 trunk = malloc(sizeof(struct ifvlantrunk),
1507 M_VLAN, M_WAITOK | M_ZERO);
1508 vlan_inithash(trunk);
1509 TRUNK_LOCK_INIT(trunk);
1511 p->if_vlantrunk = trunk;
1513 if_ref(trunk->parent);
1514 TRUNK_WUNLOCK(trunk);
1516 trunk = p->if_vlantrunk;
1519 ifv->ifv_vid = vid; /* must set this before vlan_inshash() */
1520 ifv->ifv_pcp = 0; /* Default: best effort delivery. */
1521 error = vlan_inshash(trunk, ifv);
1524 ifv->ifv_proto = proto;
1525 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1526 ifv->ifv_mintu = ETHERMIN;
1527 ifv->ifv_pflags = 0;
1528 ifv->ifv_capenable = -1;
1531 * If the parent supports the VLAN_MTU capability,
1532 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1535 if (p->if_capenable & IFCAP_VLAN_MTU) {
1537 * No need to fudge the MTU since the parent can
1538 * handle extended frames.
1540 ifv->ifv_mtufudge = 0;
1543 * Fudge the MTU by the encapsulation size. This
1544 * makes us incompatible with strictly compliant
1545 * 802.1Q implementations, but allows us to use
1546 * the feature with other NetBSD implementations,
1547 * which might still be useful.
1549 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1552 ifv->ifv_trunk = trunk;
1555 * Initialize fields from our parent. This duplicates some
1556 * work with ether_ifattach() but allows for non-ethernet
1557 * interfaces to also work.
1559 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1560 ifp->if_baudrate = p->if_baudrate;
1561 ifp->if_input = p->if_input;
1562 ifp->if_resolvemulti = p->if_resolvemulti;
1563 ifp->if_addrlen = p->if_addrlen;
1564 ifp->if_broadcastaddr = p->if_broadcastaddr;
1565 ifp->if_pcp = ifv->ifv_pcp;
1568 * We wrap the parent's if_output using vlan_output to ensure that it
1569 * can't become stale.
1571 ifp->if_output = vlan_output;
1574 * Copy only a selected subset of flags from the parent.
1575 * Other flags are none of our business.
1577 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1578 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1579 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1580 #undef VLAN_COPY_FLAGS
1582 ifp->if_link_state = p->if_link_state;
1584 NET_EPOCH_ENTER(et);
1585 vlan_capabilities(ifv);
1589 * Set up our interface address to reflect the underlying
1590 * physical interface's.
1592 TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1593 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1597 * Do not schedule link address update if it was the same
1598 * as previous parent's. This helps avoid updating for each
1599 * associated llentry.
1601 if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) {
1602 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1603 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
1606 /* We are ready for operation now. */
1607 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1609 /* Update flags on the parent, if necessary. */
1610 vlan_setflags(ifp, 1);
1613 * Configure multicast addresses that may already be
1614 * joined on the vlan device.
1616 (void)vlan_setmulti(ifp);
1620 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1627 vlan_unconfig(struct ifnet *ifp)
1631 vlan_unconfig_locked(ifp, 0);
1636 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1638 struct ifvlantrunk *trunk;
1639 struct vlan_mc_entry *mc;
1641 struct ifnet *parent;
1644 VLAN_XLOCK_ASSERT();
1646 ifv = ifp->if_softc;
1647 trunk = ifv->ifv_trunk;
1650 if (trunk != NULL) {
1651 parent = trunk->parent;
1654 * Since the interface is being unconfigured, we need to
1655 * empty the list of multicast groups that we may have joined
1656 * while we were alive from the parent's list.
1658 while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1660 * If the parent interface is being detached,
1661 * all its multicast addresses have already
1662 * been removed. Warn about errors if
1663 * if_delmulti() does fail, but don't abort as
1664 * all callers expect vlan destruction to
1668 error = if_delmulti(parent,
1669 (struct sockaddr *)&mc->mc_addr);
1672 "Failed to delete multicast address from parent: %d\n",
1675 CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1676 NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
1679 vlan_setflags(ifp, 0); /* clear special flags on parent */
1681 vlan_remhash(trunk, ifv);
1682 ifv->ifv_trunk = NULL;
1685 * Check if we were the last.
1687 if (trunk->refcnt == 0) {
1688 parent->if_vlantrunk = NULL;
1690 trunk_destroy(trunk);
1694 /* Disconnect from parent. */
1695 if (ifv->ifv_pflags)
1696 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1697 ifp->if_mtu = ETHERMTU;
1698 ifp->if_link_state = LINK_STATE_UNKNOWN;
1699 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1702 * Only dispatch an event if vlan was
1703 * attached, otherwise there is nothing
1704 * to cleanup anyway.
1707 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1710 /* Handle a reference counted flag that should be set on the parent as well */
1712 vlan_setflag(struct ifnet *ifp, int flag, int status,
1713 int (*func)(struct ifnet *, int))
1718 VLAN_SXLOCK_ASSERT();
1720 ifv = ifp->if_softc;
1721 status = status ? (ifp->if_flags & flag) : 0;
1722 /* Now "status" contains the flag value or 0 */
1725 * See if recorded parent's status is different from what
1726 * we want it to be. If it is, flip it. We record parent's
1727 * status in ifv_pflags so that we won't clear parent's flag
1728 * we haven't set. In fact, we don't clear or set parent's
1729 * flags directly, but get or release references to them.
1730 * That's why we can be sure that recorded flags still are
1731 * in accord with actual parent's flags.
1733 if (status != (ifv->ifv_pflags & flag)) {
1734 error = (*func)(PARENT(ifv), status);
1737 ifv->ifv_pflags &= ~flag;
1738 ifv->ifv_pflags |= status;
1744 * Handle IFF_* flags that require certain changes on the parent:
1745 * if "status" is true, update parent's flags respective to our if_flags;
1746 * if "status" is false, forcedly clear the flags set on parent.
1749 vlan_setflags(struct ifnet *ifp, int status)
1753 for (i = 0; vlan_pflags[i].flag; i++) {
1754 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1755 status, vlan_pflags[i].func);
1762 /* Inform all vlans that their parent has changed link state */
1764 vlan_link_state(struct ifnet *ifp)
1766 struct epoch_tracker et;
1767 struct ifvlantrunk *trunk;
1770 NET_EPOCH_ENTER(et);
1771 trunk = ifp->if_vlantrunk;
1772 if (trunk == NULL) {
1778 VLAN_FOREACH(ifv, trunk) {
1779 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1780 if_link_state_change(ifv->ifv_ifp,
1781 trunk->parent->if_link_state);
1783 TRUNK_WUNLOCK(trunk);
1788 vlan_capabilities(struct ifvlan *ifv)
1792 struct ifnet_hw_tsomax hw_tsomax;
1793 int cap = 0, ena = 0, mena;
1797 VLAN_SXLOCK_ASSERT();
1802 /* Mask parent interface enabled capabilities disabled by user. */
1803 mena = p->if_capenable & ifv->ifv_capenable;
1806 * If the parent interface can do checksum offloading
1807 * on VLANs, then propagate its hardware-assisted
1808 * checksumming flags. Also assert that checksum
1809 * offloading requires hardware VLAN tagging.
1811 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1812 cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1813 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1814 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1815 ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1816 if (ena & IFCAP_TXCSUM)
1817 hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1818 CSUM_UDP | CSUM_SCTP);
1819 if (ena & IFCAP_TXCSUM_IPV6)
1820 hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1821 CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
1825 * If the parent interface can do TSO on VLANs then
1826 * propagate the hardware-assisted flag. TSO on VLANs
1827 * does not necessarily require hardware VLAN tagging.
1829 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
1830 if_hw_tsomax_common(p, &hw_tsomax);
1831 if_hw_tsomax_update(ifp, &hw_tsomax);
1832 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1833 cap |= p->if_capabilities & IFCAP_TSO;
1834 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1835 ena |= mena & IFCAP_TSO;
1836 if (ena & IFCAP_TSO)
1837 hwa |= p->if_hwassist & CSUM_TSO;
1841 * If the parent interface can do LRO and checksum offloading on
1842 * VLANs, then guess it may do LRO on VLANs. False positive here
1843 * cost nothing, while false negative may lead to some confusions.
1845 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1846 cap |= p->if_capabilities & IFCAP_LRO;
1847 if (p->if_capenable & IFCAP_VLAN_HWCSUM)
1848 ena |= mena & IFCAP_LRO;
1851 * If the parent interface can offload TCP connections over VLANs then
1852 * propagate its TOE capability to the VLAN interface.
1854 * All TOE drivers in the tree today can deal with VLANs. If this
1855 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1858 #define IFCAP_VLAN_TOE IFCAP_TOE
1859 if (p->if_capabilities & IFCAP_VLAN_TOE)
1860 cap |= p->if_capabilities & IFCAP_TOE;
1861 if (p->if_capenable & IFCAP_VLAN_TOE) {
1862 TOEDEV(ifp) = TOEDEV(p);
1863 ena |= mena & IFCAP_TOE;
1867 * If the parent interface supports dynamic link state, so does the
1870 cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1871 ena |= (mena & IFCAP_LINKSTATE);
1875 * If the parent interface supports ratelimiting, so does the
1878 cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1879 ena |= (mena & IFCAP_TXRTLMT);
1883 * If the parent interface supports unmapped mbufs, so does
1884 * the VLAN interface. Note that this should be fine even for
1885 * interfaces that don't support hardware tagging as headers
1886 * are prepended in normal mbufs to unmapped mbufs holding
1889 cap |= (p->if_capabilities & IFCAP_MEXTPG);
1890 ena |= (mena & IFCAP_MEXTPG);
1893 * If the parent interface can offload encryption and segmentation
1894 * of TLS records over TCP, propagate it's capability to the VLAN
1897 * All TLS drivers in the tree today can deal with VLANs. If
1898 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
1901 if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
1902 cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
1903 if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
1904 ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
1906 ifp->if_capabilities = cap;
1907 ifp->if_capenable = ena;
1908 ifp->if_hwassist = hwa;
1912 vlan_trunk_capabilities(struct ifnet *ifp)
1914 struct epoch_tracker et;
1915 struct ifvlantrunk *trunk;
1919 trunk = ifp->if_vlantrunk;
1920 if (trunk == NULL) {
1924 NET_EPOCH_ENTER(et);
1925 VLAN_FOREACH(ifv, trunk)
1926 vlan_capabilities(ifv);
1932 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1938 struct ifvlantrunk *trunk;
1940 int error = 0, oldmtu;
1942 ifr = (struct ifreq *)data;
1943 ifa = (struct ifaddr *) data;
1944 ifv = ifp->if_softc;
1948 ifp->if_flags |= IFF_UP;
1950 if (ifa->ifa_addr->sa_family == AF_INET)
1951 arp_ifinit(ifp, ifa);
1955 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
1960 if (TRUNK(ifv) != NULL) {
1963 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1965 /* Limit the result to the parent's current config. */
1967 struct ifmediareq *ifmr;
1969 ifmr = (struct ifmediareq *)data;
1970 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1971 ifmr->ifm_count = 1;
1972 error = copyout(&ifmr->ifm_current,
1989 * Set the interface MTU.
1993 if (trunk != NULL) {
1996 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1998 (ifv->ifv_mintu - ifv->ifv_mtufudge))
2001 ifp->if_mtu = ifr->ifr_mtu;
2002 TRUNK_WUNLOCK(trunk);
2011 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
2012 * interface to be delegated to a jail without allowing the
2013 * jail to change what underlying interface/VID it is
2014 * associated with. We are not entirely convinced that this
2015 * is the right way to accomplish that policy goal.
2017 if (ifp->if_vnet != ifp->if_home_vnet) {
2022 error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
2025 if (vlr.vlr_parent[0] == '\0') {
2029 p = ifunit_ref(vlr.vlr_parent);
2034 if (vlr.vlr_proto == 0)
2035 vlr.vlr_proto = ETHERTYPE_VLAN;
2036 oldmtu = ifp->if_mtu;
2037 error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto);
2041 * VLAN MTU may change during addition of the vlandev.
2042 * If it did, do network layer specific procedure.
2044 if (ifp->if_mtu != oldmtu) {
2054 if (ifp->if_vnet != ifp->if_home_vnet) {
2059 bzero(&vlr, sizeof(vlr));
2061 if (TRUNK(ifv) != NULL) {
2062 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
2063 sizeof(vlr.vlr_parent));
2064 vlr.vlr_tag = ifv->ifv_vid;
2065 vlr.vlr_proto = ifv->ifv_proto;
2068 error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
2073 * We should propagate selected flags to the parent,
2074 * e.g., promiscuous mode.
2077 if (TRUNK(ifv) != NULL)
2078 error = vlan_setflags(ifp, 1);
2085 * If we don't have a parent, just remember the membership for
2088 * XXX We need the rmlock here to avoid sleeping while
2089 * holding in6_multi_mtx.
2094 error = vlan_setmulti(ifp);
2100 if (ifp->if_vnet != ifp->if_home_vnet) {
2105 ifr->ifr_vlan_pcp = ifv->ifv_pcp;
2110 if (ifp->if_vnet != ifp->if_home_vnet) {
2115 error = priv_check(curthread, PRIV_NET_SETVLANPCP);
2118 if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) {
2122 ifv->ifv_pcp = ifr->ifr_vlan_pcp;
2123 ifp->if_pcp = ifv->ifv_pcp;
2124 /* broadcast event about PCP change */
2125 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
2130 ifv->ifv_capenable = ifr->ifr_reqcap;
2132 if (trunk != NULL) {
2133 struct epoch_tracker et;
2135 NET_EPOCH_ENTER(et);
2136 vlan_capabilities(ifv);
2150 #if defined(KERN_TLS) || defined(RATELIMIT)
2152 vlan_snd_tag_alloc(struct ifnet *ifp,
2153 union if_snd_tag_alloc_params *params,
2154 struct m_snd_tag **ppmt)
2156 struct epoch_tracker et;
2157 struct vlan_snd_tag *vst;
2159 struct ifnet *parent;
2162 NET_EPOCH_ENTER(et);
2163 ifv = ifp->if_softc;
2164 if (ifv->ifv_trunk != NULL)
2165 parent = PARENT(ifv);
2168 if (parent == NULL) {
2170 return (EOPNOTSUPP);
2175 vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2181 error = m_snd_tag_alloc(parent, params, &vst->tag);
2188 m_snd_tag_init(&vst->com, ifp, vst->tag->type);
2194 static struct m_snd_tag *
2195 vlan_next_snd_tag(struct m_snd_tag *mst)
2197 struct vlan_snd_tag *vst;
2199 vst = mst_to_vst(mst);
2204 vlan_snd_tag_modify(struct m_snd_tag *mst,
2205 union if_snd_tag_modify_params *params)
2207 struct vlan_snd_tag *vst;
2209 vst = mst_to_vst(mst);
2210 return (vst->tag->ifp->if_snd_tag_modify(vst->tag, params));
2214 vlan_snd_tag_query(struct m_snd_tag *mst,
2215 union if_snd_tag_query_params *params)
2217 struct vlan_snd_tag *vst;
2219 vst = mst_to_vst(mst);
2220 return (vst->tag->ifp->if_snd_tag_query(vst->tag, params));
2224 vlan_snd_tag_free(struct m_snd_tag *mst)
2226 struct vlan_snd_tag *vst;
2228 vst = mst_to_vst(mst);
2229 m_snd_tag_rele(vst->tag);
2234 vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
2237 * For vlan, we have an indirect
2238 * interface. The caller needs to
2239 * get a ratelimit tag on the actual
2240 * interface the flow will go on.
2242 q->rate_table = NULL;
2243 q->flags = RT_IS_INDIRECT;
2245 q->number_of_rates = 0;