2 * Copyright 1998 Massachusetts Institute of Technology
3 * Copyright 2012 ADARA Networks, Inc.
4 * Copyright 2017 Dell EMC Isilon
6 * Portions of this software were developed by Robert N. M. Watson under
7 * contract to ADARA Networks, Inc.
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby
11 * granted, provided that both the above copyright notice and this
12 * permission notice appear in all copies, that both the above
13 * copyright notice and this permission notice appear in all
14 * supporting documentation, and that the name of M.I.T. not be used
15 * in advertising or publicity pertaining to distribution of the
16 * software without specific, written prior permission. M.I.T. makes
17 * no representations about the suitability of this software for any
18 * purpose. It is provided "as is" without express or implied
21 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
22 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
25 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
37 * This is sort of sneaky in the implementation, since
38 * we need to pretend to be enough of an Ethernet implementation
39 * to make arp work. The way we do this is by telling everyone
40 * that we are an Ethernet, and then catch the packets that
41 * ether_output() sends to us via if_transmit(), rewrite them for
42 * use by the real outgoing interface, and ask it to send them.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
49 #include "opt_inet6.h"
50 #include "opt_kern_tls.h"
51 #include "opt_netlink.h"
53 #include "opt_ratelimit.h"
55 #include <sys/param.h>
56 #include <sys/eventhandler.h>
57 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/module.h>
62 #include <sys/rmlock.h>
64 #include <sys/queue.h>
65 #include <sys/socket.h>
66 #include <sys/sockio.h>
67 #include <sys/sysctl.h>
68 #include <sys/systm.h>
70 #include <sys/taskqueue.h>
73 #include <net/ethernet.h>
75 #include <net/if_var.h>
76 #include <net/if_private.h>
77 #include <net/if_clone.h>
78 #include <net/if_dl.h>
79 #include <net/if_types.h>
80 #include <net/if_vlan_var.h>
81 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/if_ether.h>
89 #include <netlink/netlink.h>
90 #include <netlink/netlink_ctl.h>
91 #include <netlink/netlink_route.h>
92 #include <netlink/route/route_var.h>
94 #define VLAN_DEF_HWIDTH 4
95 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
97 #define UP_AND_RUNNING(ifp) \
98 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
100 CK_SLIST_HEAD(ifvlanhead, ifvlan);
103 struct ifnet *parent; /* parent interface of this trunk */
106 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
107 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
109 struct ifvlanhead *hash; /* dynamic hash-list table */
116 #if defined(KERN_TLS) || defined(RATELIMIT)
117 struct vlan_snd_tag {
118 struct m_snd_tag com;
119 struct m_snd_tag *tag;
122 static inline struct vlan_snd_tag *
123 mst_to_vst(struct m_snd_tag *mst)
126 return (__containerof(mst, struct vlan_snd_tag, com));
131 * This macro provides a facility to iterate over every vlan on a trunk with
132 * the assumption that none will be added/removed during iteration.
135 #define VLAN_FOREACH(_ifv, _trunk) \
137 for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
138 if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
139 #else /* VLAN_ARRAY */
140 #define VLAN_FOREACH(_ifv, _trunk) \
141 struct ifvlan *_next; \
143 for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
144 CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
145 #endif /* VLAN_ARRAY */
148 * This macro provides a facility to iterate over every vlan on a trunk while
149 * also modifying the number of vlans on the trunk. The iteration continues
150 * until some condition is met or there are no more vlans on the trunk.
153 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
154 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
156 for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
157 if (((_ifv) = (_trunk)->vlans[_i]))
158 #else /* VLAN_ARRAY */
160 * The hash table case is more complicated. We allow for the hash table to be
161 * modified (i.e. vlans removed) while we are iterating over it. To allow for
162 * this we must restart the iteration every time we "touch" something during
163 * the iteration, since removal will resize the hash table and invalidate our
164 * current position. If acting on the touched element causes the trunk to be
165 * emptied, then iteration also stops.
167 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
169 bool _touch = false; \
171 !(_cond) && _i < (1 << (_trunk)->hwidth); \
172 _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
173 if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
175 #endif /* VLAN_ARRAY */
177 struct vlan_mc_entry {
178 struct sockaddr_dl mc_addr;
179 CK_SLIST_ENTRY(vlan_mc_entry) mc_entries;
180 struct epoch_context mc_epoch_ctx;
184 struct ifvlantrunk *ifv_trunk;
185 struct ifnet *ifv_ifp;
186 #define TRUNK(ifv) ((ifv)->ifv_trunk)
187 #define PARENT(ifv) (TRUNK(ifv)->parent)
189 int ifv_pflags; /* special flags we have set on parent */
191 int ifv_encaplen; /* encapsulation length */
192 int ifv_mtufudge; /* MTU fudged by this much */
193 int ifv_mintu; /* min transmission unit */
194 struct ether_8021q_tag ifv_qtag;
195 #define ifv_proto ifv_qtag.proto
196 #define ifv_vid ifv_qtag.vid
197 #define ifv_pcp ifv_qtag.pcp
198 struct task lladdr_task;
199 CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
201 CK_SLIST_ENTRY(ifvlan) ifv_list;
205 /* Special flags we should propagate to parent. */
208 int (*func)(struct ifnet *, int);
210 {IFF_PROMISC, ifpromisc},
211 {IFF_ALLMULTI, if_allmulti},
215 VNET_DECLARE(int, vlan_mtag_pcp);
216 #define V_vlan_mtag_pcp VNET(vlan_mtag_pcp)
218 static const char vlanname[] = "vlan";
219 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
221 static eventhandler_tag ifdetach_tag;
222 static eventhandler_tag iflladdr_tag;
223 static eventhandler_tag ifevent_tag;
226 * if_vlan uses two module-level synchronizations primitives to allow concurrent
227 * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
228 * while they are being used for tx/rx. To accomplish this in a way that has
229 * acceptable performance and cooperation with other parts of the network stack
230 * there is a non-sleepable epoch(9) and an sx(9).
232 * The performance-sensitive paths that warrant using the epoch(9) are
233 * vlan_transmit and vlan_input. Both have to check for the vlan interface's
234 * existence using if_vlantrunk, and being in the network tx/rx paths the use
235 * of an epoch(9) gives a measureable improvement in performance.
237 * The reason for having an sx(9) is mostly because there are still areas that
238 * must be sleepable and also have safe concurrent access to a vlan interface.
239 * Since the sx(9) exists, it is used by default in most paths unless sleeping
240 * is not permitted, or if it is not clear whether sleeping is permitted.
243 #define _VLAN_SX_ID ifv_sx
245 static struct sx _VLAN_SX_ID;
247 #define VLAN_LOCKING_INIT() \
248 sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE)
250 #define VLAN_LOCKING_DESTROY() \
251 sx_destroy(&_VLAN_SX_ID)
253 #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID)
254 #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID)
255 #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID)
256 #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID)
257 #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
258 #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
259 #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED)
262 * We also have a per-trunk mutex that should be acquired when changing
265 #define TRUNK_LOCK_INIT(trunk) mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
266 #define TRUNK_LOCK_DESTROY(trunk) mtx_destroy(&(trunk)->lock)
267 #define TRUNK_WLOCK(trunk) mtx_lock(&(trunk)->lock)
268 #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock)
269 #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED);
272 * The VLAN_ARRAY substitutes the dynamic hash with a static array
273 * with 4096 entries. In theory this can give a boost in processing,
274 * however in practice it does not. Probably this is because the array
275 * is too big to fit into CPU cache.
278 static void vlan_inithash(struct ifvlantrunk *trunk);
279 static void vlan_freehash(struct ifvlantrunk *trunk);
280 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
281 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
282 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
283 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
286 static void trunk_destroy(struct ifvlantrunk *trunk);
288 static void vlan_init(void *foo);
289 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
290 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
291 #if defined(KERN_TLS) || defined(RATELIMIT)
292 static int vlan_snd_tag_alloc(struct ifnet *,
293 union if_snd_tag_alloc_params *, struct m_snd_tag **);
294 static int vlan_snd_tag_modify(struct m_snd_tag *,
295 union if_snd_tag_modify_params *);
296 static int vlan_snd_tag_query(struct m_snd_tag *,
297 union if_snd_tag_query_params *);
298 static void vlan_snd_tag_free(struct m_snd_tag *);
299 static struct m_snd_tag *vlan_next_snd_tag(struct m_snd_tag *);
300 static void vlan_ratelimit_query(struct ifnet *,
301 struct if_ratelimit_query_results *);
303 static void vlan_qflush(struct ifnet *ifp);
304 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
305 int (*func)(struct ifnet *, int));
306 static int vlan_setflags(struct ifnet *ifp, int status);
307 static int vlan_setmulti(struct ifnet *ifp);
308 static int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
310 static void vlan_altq_start(struct ifnet *ifp);
311 static int vlan_altq_transmit(struct ifnet *ifp, struct mbuf *m);
313 static int vlan_output(struct ifnet *ifp, struct mbuf *m,
314 const struct sockaddr *dst, struct route *ro);
315 static void vlan_unconfig(struct ifnet *ifp);
316 static void vlan_unconfig_locked(struct ifnet *ifp, int departing);
317 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag,
319 static void vlan_link_state(struct ifnet *ifp);
320 static void vlan_capabilities(struct ifvlan *ifv);
321 static void vlan_trunk_capabilities(struct ifnet *ifp);
323 static struct ifnet *vlan_clone_match_ethervid(const char *, int *);
324 static int vlan_clone_match(struct if_clone *, const char *);
325 static int vlan_clone_create(struct if_clone *, char *, size_t,
326 struct ifc_data *, struct ifnet **);
327 static int vlan_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
329 static int vlan_clone_create_nl(struct if_clone *ifc, char *name, size_t len,
330 struct ifc_data_nl *ifd);
331 static int vlan_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd);
332 static void vlan_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw);
334 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
335 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
336 static void vlan_ifevent(void *arg, struct ifnet *ifp, int event);
338 static void vlan_lladdr_fn(void *arg, int pending);
340 static struct if_clone *vlan_cloner;
343 VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
344 #define V_vlan_cloner VNET(vlan_cloner)
348 static const struct if_snd_tag_sw vlan_snd_tag_ul_sw = {
349 .snd_tag_modify = vlan_snd_tag_modify,
350 .snd_tag_query = vlan_snd_tag_query,
351 .snd_tag_free = vlan_snd_tag_free,
352 .next_snd_tag = vlan_next_snd_tag,
353 .type = IF_SND_TAG_TYPE_UNLIMITED
356 static const struct if_snd_tag_sw vlan_snd_tag_rl_sw = {
357 .snd_tag_modify = vlan_snd_tag_modify,
358 .snd_tag_query = vlan_snd_tag_query,
359 .snd_tag_free = vlan_snd_tag_free,
360 .next_snd_tag = vlan_next_snd_tag,
361 .type = IF_SND_TAG_TYPE_RATE_LIMIT
366 static const struct if_snd_tag_sw vlan_snd_tag_tls_sw = {
367 .snd_tag_modify = vlan_snd_tag_modify,
368 .snd_tag_query = vlan_snd_tag_query,
369 .snd_tag_free = vlan_snd_tag_free,
370 .next_snd_tag = vlan_next_snd_tag,
371 .type = IF_SND_TAG_TYPE_TLS
375 static const struct if_snd_tag_sw vlan_snd_tag_tls_rl_sw = {
376 .snd_tag_modify = vlan_snd_tag_modify,
377 .snd_tag_query = vlan_snd_tag_query,
378 .snd_tag_free = vlan_snd_tag_free,
379 .next_snd_tag = vlan_next_snd_tag,
380 .type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT
386 vlan_mc_free(struct epoch_context *ctx)
388 struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
393 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
396 vlan_inithash(struct ifvlantrunk *trunk)
401 * The trunk must not be locked here since we call malloc(M_WAITOK).
402 * It is OK in case this function is called before the trunk struct
403 * gets hooked up and becomes visible from other threads.
406 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
407 ("%s: hash already initialized", __func__));
409 trunk->hwidth = VLAN_DEF_HWIDTH;
410 n = 1 << trunk->hwidth;
411 trunk->hmask = n - 1;
412 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
413 for (i = 0; i < n; i++)
414 CK_SLIST_INIT(&trunk->hash[i]);
418 vlan_freehash(struct ifvlantrunk *trunk)
423 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
424 for (i = 0; i < (1 << trunk->hwidth); i++)
425 KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
426 ("%s: hash table not empty", __func__));
428 free(trunk->hash, M_VLAN);
430 trunk->hwidth = trunk->hmask = 0;
434 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
440 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
442 b = 1 << trunk->hwidth;
443 i = HASH(ifv->ifv_vid, trunk->hmask);
444 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
445 if (ifv->ifv_vid == ifv2->ifv_vid)
449 * Grow the hash when the number of vlans exceeds half of the number of
450 * hash buckets squared. This will make the average linked-list length
453 if (trunk->refcnt > (b * b) / 2) {
454 vlan_growhash(trunk, 1);
455 i = HASH(ifv->ifv_vid, trunk->hmask);
457 CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
464 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
470 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
472 b = 1 << (trunk->hwidth - 1);
473 i = HASH(ifv->ifv_vid, trunk->hmask);
474 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
477 CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
478 if (trunk->refcnt < (b * b) / 2)
479 vlan_growhash(trunk, -1);
483 panic("%s: vlan not found\n", __func__);
484 return (ENOENT); /*NOTREACHED*/
488 * Grow the hash larger or smaller if memory permits.
491 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
494 struct ifvlanhead *hash2;
495 int hwidth2, i, j, n, n2;
498 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
501 /* Harmless yet obvious coding error */
502 printf("%s: howmuch is 0\n", __func__);
506 hwidth2 = trunk->hwidth + howmuch;
507 n = 1 << trunk->hwidth;
509 /* Do not shrink the table below the default */
510 if (hwidth2 < VLAN_DEF_HWIDTH)
513 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
515 printf("%s: out of memory -- hash size not changed\n",
517 return; /* We can live with the old hash table */
519 for (j = 0; j < n2; j++)
520 CK_SLIST_INIT(&hash2[j]);
521 for (i = 0; i < n; i++)
522 while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
523 CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
524 j = HASH(ifv->ifv_vid, n2 - 1);
525 CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
528 free(trunk->hash, M_VLAN);
530 trunk->hwidth = hwidth2;
531 trunk->hmask = n2 - 1;
534 if_printf(trunk->parent,
535 "VLAN hash table resized from %d to %d buckets\n", n, n2);
538 static __inline struct ifvlan *
539 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
545 CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
546 if (ifv->ifv_vid == vid)
552 /* Debugging code to view the hashtables. */
554 vlan_dumphash(struct ifvlantrunk *trunk)
559 for (i = 0; i < (1 << trunk->hwidth); i++) {
561 CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
562 printf("%s ", ifv->ifv_ifp->if_xname);
569 static __inline struct ifvlan *
570 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
573 return trunk->vlans[vid];
577 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
580 if (trunk->vlans[ifv->ifv_vid] != NULL)
582 trunk->vlans[ifv->ifv_vid] = ifv;
589 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
592 trunk->vlans[ifv->ifv_vid] = NULL;
599 vlan_freehash(struct ifvlantrunk *trunk)
604 vlan_inithash(struct ifvlantrunk *trunk)
608 #endif /* !VLAN_ARRAY */
611 trunk_destroy(struct ifvlantrunk *trunk)
615 vlan_freehash(trunk);
616 trunk->parent->if_vlantrunk = NULL;
617 TRUNK_LOCK_DESTROY(trunk);
618 if_rele(trunk->parent);
623 * Program our multicast filter. What we're actually doing is
624 * programming the multicast filter of the parent. This has the
625 * side effect of causing the parent interface to receive multicast
626 * traffic that it doesn't really want, which ends up being discarded
627 * later by the upper protocol layers. Unfortunately, there's no way
628 * to avoid this: there really is only one physical interface.
631 vlan_setmulti(struct ifnet *ifp)
634 struct ifmultiaddr *ifma;
636 struct vlan_mc_entry *mc;
641 /* Find the parent. */
645 CURVNET_SET_QUIET(ifp_p->if_vnet);
647 /* First, remove any existing filter entries. */
648 while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
649 CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
650 (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
651 NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
654 /* Now program new ones. */
656 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
657 if (ifma->ifma_addr->sa_family != AF_LINK)
659 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
661 IF_ADDR_WUNLOCK(ifp);
665 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
666 mc->mc_addr.sdl_index = ifp_p->if_index;
667 CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
669 IF_ADDR_WUNLOCK(ifp);
670 CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
671 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
684 * A handler for interface ifnet events.
687 vlan_ifevent(void *arg __unused, struct ifnet *ifp, int event)
689 struct epoch_tracker et;
691 struct ifvlantrunk *trunk;
693 if (event != IFNET_EVENT_UPDATE_BAUDRATE)
697 trunk = ifp->if_vlantrunk;
704 VLAN_FOREACH(ifv, trunk) {
705 ifv->ifv_ifp->if_baudrate = ifp->if_baudrate;
707 TRUNK_WUNLOCK(trunk);
712 * A handler for parent interface link layer address changes.
713 * If the parent interface link layer address is changed we
714 * should also change it on all children vlans.
717 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
719 struct epoch_tracker et;
721 struct ifnet *ifv_ifp;
722 struct ifvlantrunk *trunk;
723 struct sockaddr_dl *sdl;
725 /* Need the epoch since this is run on taskqueue_swi. */
727 trunk = ifp->if_vlantrunk;
734 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
735 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
736 * ioctl calls on the parent garbling the lladdr of the child vlan.
739 VLAN_FOREACH(ifv, trunk) {
741 * Copy new new lladdr into the ifv_ifp, enqueue a task
742 * to actually call if_setlladdr. if_setlladdr needs to
743 * be deferred to a taskqueue because it will call into
744 * the if_vlan ioctl path and try to acquire the global
747 ifv_ifp = ifv->ifv_ifp;
748 bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
750 sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
751 sdl->sdl_alen = ifp->if_addrlen;
752 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
754 TRUNK_WUNLOCK(trunk);
759 * A handler for network interface departure events.
760 * Track departure of trunks here so that we don't access invalid
761 * pointers or whatever if a trunk is ripped from under us, e.g.,
762 * by ejecting its hot-plug card. However, if an ifnet is simply
763 * being renamed, then there's no need to tear down the state.
766 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
769 struct ifvlantrunk *trunk;
771 /* If the ifnet is just being renamed, don't do anything. */
772 if (ifp->if_flags & IFF_RENAMING)
775 trunk = ifp->if_vlantrunk;
782 * OK, it's a trunk. Loop over and detach all vlan's on it.
783 * Check trunk pointer after each vlan_unconfig() as it will
784 * free it and set to NULL after the last vlan was detached.
786 VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
787 ifp->if_vlantrunk == NULL)
788 vlan_unconfig_locked(ifv->ifv_ifp, 1);
790 /* Trunk should have been destroyed in vlan_unconfig(). */
791 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
796 * Return the trunk device for a virtual interface.
798 static struct ifnet *
799 vlan_trunkdev(struct ifnet *ifp)
805 if (ifp->if_type != IFT_L2VLAN)
816 * Return the 12-bit VLAN VID for this interface, for use by external
817 * components such as Infiniband.
819 * XXXRW: Note that the function name here is historical; it should be named
823 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
827 if (ifp->if_type != IFT_L2VLAN)
830 *vidp = ifv->ifv_vid;
835 vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
839 if (ifp->if_type != IFT_L2VLAN)
842 *pcpp = ifv->ifv_pcp;
847 * Return a driver specific cookie for this interface. Synchronization
848 * with setcookie must be provided by the driver.
851 vlan_cookie(struct ifnet *ifp)
855 if (ifp->if_type != IFT_L2VLAN)
858 return (ifv->ifv_cookie);
862 * Store a cookie in our softc that drivers can use to store driver
863 * private per-instance data in.
866 vlan_setcookie(struct ifnet *ifp, void *cookie)
870 if (ifp->if_type != IFT_L2VLAN)
873 ifv->ifv_cookie = cookie;
878 * Return the vlan device present at the specific VID.
880 static struct ifnet *
881 vlan_devat(struct ifnet *ifp, uint16_t vid)
883 struct ifvlantrunk *trunk;
888 trunk = ifp->if_vlantrunk;
892 ifv = vlan_gethash(trunk, vid);
899 * VLAN support can be loaded as a module. The only place in the
900 * system that's intimately aware of this is ether_input. We hook
901 * into this code through vlan_input_p which is defined there and
902 * set here. No one else in the system should be aware of this so
903 * we use an explicit reference here.
905 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
907 /* For if_link_state_change() eyes only... */
908 extern void (*vlan_link_state_p)(struct ifnet *);
910 static struct if_clone_addreq_v2 vlan_addreq = {
912 .match_f = vlan_clone_match,
913 .create_f = vlan_clone_create,
914 .destroy_f = vlan_clone_destroy,
915 .create_nl_f = vlan_clone_create_nl,
916 .modify_nl_f = vlan_clone_modify_nl,
917 .dump_nl_f = vlan_clone_dump_nl,
921 vlan_modevent(module_t mod, int type, void *data)
926 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
927 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
928 if (ifdetach_tag == NULL)
930 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
931 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
932 if (iflladdr_tag == NULL)
934 ifevent_tag = EVENTHANDLER_REGISTER(ifnet_event,
935 vlan_ifevent, NULL, EVENTHANDLER_PRI_ANY);
936 if (ifevent_tag == NULL)
939 vlan_input_p = vlan_input;
940 vlan_link_state_p = vlan_link_state;
941 vlan_trunk_cap_p = vlan_trunk_capabilities;
942 vlan_trunkdev_p = vlan_trunkdev;
943 vlan_cookie_p = vlan_cookie;
944 vlan_setcookie_p = vlan_setcookie;
945 vlan_tag_p = vlan_tag;
946 vlan_pcp_p = vlan_pcp;
947 vlan_devat_p = vlan_devat;
949 vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq);
952 printf("vlan: initialized, using "
956 "hash tables with chaining"
963 ifc_detach_cloner(vlan_cloner);
965 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
966 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
967 EVENTHANDLER_DEREGISTER(ifnet_event, ifevent_tag);
969 vlan_link_state_p = NULL;
970 vlan_trunk_cap_p = NULL;
971 vlan_trunkdev_p = NULL;
973 vlan_cookie_p = NULL;
974 vlan_setcookie_p = NULL;
976 VLAN_LOCKING_DESTROY();
978 printf("vlan: unloaded\n");
986 static moduledata_t vlan_mod = {
992 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
993 MODULE_VERSION(if_vlan, 3);
997 vnet_vlan_init(const void *unused __unused)
999 vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq);
1000 V_vlan_cloner = vlan_cloner;
1002 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
1003 vnet_vlan_init, NULL);
1006 vnet_vlan_uninit(const void *unused __unused)
1009 ifc_detach_cloner(V_vlan_cloner);
1011 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
1012 vnet_vlan_uninit, NULL);
1016 * Check for <etherif>.<vlan>[.<vlan> ...] style interface names.
1018 static struct ifnet *
1019 vlan_clone_match_ethervid(const char *name, int *vidp)
1021 char ifname[IFNAMSIZ];
1026 strlcpy(ifname, name, IFNAMSIZ);
1027 if ((cp = strrchr(ifname, '.')) == NULL)
1030 if ((ifp = ifunit_ref(ifname)) == NULL)
1033 if (*++cp == '\0') {
1038 for(; *cp >= '0' && *cp <= '9'; cp++)
1039 vid = (vid * 10) + (*cp - '0');
1051 vlan_clone_match(struct if_clone *ifc, const char *name)
1056 ifp = vlan_clone_match_ethervid(name, NULL);
1062 if (strncmp(vlanname, name, strlen(vlanname)) != 0)
1064 for (cp = name + 4; *cp != '\0'; cp++) {
1065 if (*cp < '0' || *cp > '9')
1073 vlan_clone_create(struct if_clone *ifc, char *name, size_t len,
1074 struct ifc_data *ifd, struct ifnet **ifpp)
1077 bool wildcard = false;
1078 bool subinterface = false;
1082 uint16_t proto = ETHERTYPE_VLAN;
1085 struct ifnet *p = NULL;
1087 struct sockaddr_dl *sdl;
1089 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
1093 * There are three ways to specify the cloned device:
1094 * o pass a parameter block with the clone request.
1095 * o specify parameters in the text of the clone device name
1096 * o specify no parameters and get an unattached device that
1097 * must be configured separately.
1098 * The first technique is preferred; the latter two are supported
1099 * for backwards compatibility.
1101 * XXXRW: Note historic use of the word "tag" here. New ioctls may be
1105 if (ifd->params != NULL) {
1106 error = ifc_copyin(ifd, &vlr, sizeof(vlr));
1110 proto = vlr.vlr_proto;
1112 #ifdef COMPAT_FREEBSD12
1114 proto = ETHERTYPE_VLAN;
1116 p = ifunit_ref(vlr.vlr_parent);
1121 if ((error = ifc_name2unit(name, &unit)) == 0) {
1124 * vlanX interface. Set wildcard to true if the unit number
1127 wildcard = (unit < 0);
1129 struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid);
1130 if (p_tmp != NULL) {
1132 subinterface = true;
1133 unit = IF_DUNIT_NONE;
1151 if (!subinterface) {
1152 /* vlanX interface, mark X as busy or allocate new unit # */
1153 error = ifc_alloc_unit(ifc, &unit);
1161 /* In the wildcard case, we need to update the name. */
1163 for (dp = name; *dp != '\0'; dp++);
1164 if (snprintf(dp, len - (dp-name), "%d", unit) >
1165 len - (dp-name) - 1) {
1166 panic("%s: interface name too long", __func__);
1170 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1171 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1174 ifc_free_unit(ifc, unit);
1180 CK_SLIST_INIT(&ifv->vlan_mc_listhead);
1181 ifp->if_softc = ifv;
1183 * Set the name manually rather than using if_initname because
1184 * we don't conform to the default naming convention for interfaces.
1186 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1187 ifp->if_dname = vlanname;
1188 ifp->if_dunit = unit;
1190 ifp->if_init = vlan_init;
1192 ifp->if_start = vlan_altq_start;
1193 ifp->if_transmit = vlan_altq_transmit;
1194 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1195 ifp->if_snd.ifq_drv_maxlen = 0;
1196 IFQ_SET_READY(&ifp->if_snd);
1198 ifp->if_transmit = vlan_transmit;
1200 ifp->if_qflush = vlan_qflush;
1201 ifp->if_ioctl = vlan_ioctl;
1202 #if defined(KERN_TLS) || defined(RATELIMIT)
1203 ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1204 ifp->if_ratelimit_query = vlan_ratelimit_query;
1206 ifp->if_flags = VLAN_IFFLAGS;
1207 ether_ifattach(ifp, eaddr);
1208 /* Now undo some of the damage... */
1209 ifp->if_baudrate = 0;
1210 ifp->if_type = IFT_L2VLAN;
1211 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1213 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1214 sdl->sdl_type = IFT_L2VLAN;
1217 error = vlan_config(ifv, p, vid, proto);
1221 * Since we've partially failed, we need to back
1222 * out all the way, otherwise userland could get
1223 * confused. Thus, we destroy the interface.
1225 ether_ifdetach(ifp);
1229 ifc_free_unit(ifc, unit);
1242 * Parsers of IFLA_INFO_DATA inside IFLA_LINKINFO of RTM_NEWLINK
1243 * {{nla_len=8, nla_type=IFLA_LINK}, 2},
1244 * {{nla_len=12, nla_type=IFLA_IFNAME}, "xvlan22"},
1245 * {{nla_len=24, nla_type=IFLA_LINKINFO},
1247 * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
1248 * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]}
1251 struct nl_parsed_vlan {
1253 uint16_t vlan_proto;
1254 struct ifla_vlan_flags vlan_flags;
1257 #define _OUT(_field) offsetof(struct nl_parsed_vlan, _field)
1258 static const struct nlattr_parser nla_p_vlan[] = {
1259 { .type = IFLA_VLAN_ID, .off = _OUT(vlan_id), .cb = nlattr_get_uint16 },
1260 { .type = IFLA_VLAN_FLAGS, .off = _OUT(vlan_flags), .cb = nlattr_get_nla },
1261 { .type = IFLA_VLAN_PROTOCOL, .off = _OUT(vlan_proto), .cb = nlattr_get_uint16 },
1264 NL_DECLARE_ATTR_PARSER(vlan_parser, nla_p_vlan);
1267 vlan_clone_create_nl(struct if_clone *ifc, char *name, size_t len,
1268 struct ifc_data_nl *ifd)
1270 struct epoch_tracker et;
1271 struct ifnet *ifp_parent;
1272 struct nl_pstate *npt = ifd->npt;
1273 struct nl_parsed_link *lattrs = ifd->lattrs;
1277 * lattrs.ifla_ifname is the new interface name
1278 * lattrs.ifi_index contains parent interface index
1279 * lattrs.ifla_idata contains un-parsed vlan data
1281 struct nl_parsed_vlan attrs = {
1283 .vlan_proto = ETHERTYPE_VLAN
1286 if (lattrs->ifla_idata == NULL) {
1287 nlmsg_report_err_msg(npt, "vlan id is required, guessing not supported");
1291 error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, npt, &attrs);
1294 if (attrs.vlan_id > 4095) {
1295 nlmsg_report_err_msg(npt, "Invalid VID: %d", attrs.vlan_id);
1298 if (attrs.vlan_proto != ETHERTYPE_VLAN && attrs.vlan_proto != ETHERTYPE_QINQ) {
1299 nlmsg_report_err_msg(npt, "Unsupported ethertype: 0x%04X", attrs.vlan_proto);
1303 struct vlanreq params = {
1304 .vlr_tag = attrs.vlan_id,
1305 .vlr_proto = attrs.vlan_proto,
1307 struct ifc_data ifd_new = { .flags = IFC_F_SYSSPACE, .unit = ifd->unit, .params = ¶ms };
1309 NET_EPOCH_ENTER(et);
1310 ifp_parent = ifnet_byindex(lattrs->ifi_index);
1311 if (ifp_parent != NULL)
1312 strlcpy(params.vlr_parent, if_name(ifp_parent), sizeof(params.vlr_parent));
1315 if (ifp_parent == NULL) {
1316 nlmsg_report_err_msg(npt, "unable to find parent interface %u", lattrs->ifi_index);
1320 error = vlan_clone_create(ifc, name, len, &ifd_new, &ifd->ifp);
1326 vlan_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd)
1328 struct nl_parsed_link *lattrs = ifd->lattrs;
1330 if ((lattrs->ifla_idata != NULL) && ((ifd->flags & IFC_F_CREATE) == 0)) {
1331 struct epoch_tracker et;
1332 struct nl_parsed_vlan attrs = {
1333 .vlan_proto = ETHERTYPE_VLAN,
1337 error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, ifd->npt, &attrs);
1341 NET_EPOCH_ENTER(et);
1342 struct ifnet *ifp_parent = ifnet_byindex_ref(lattrs->ifla_link);
1345 if (ifp_parent == NULL) {
1346 nlmsg_report_err_msg(ifd->npt, "unable to find parent interface %u",
1351 struct ifvlan *ifv = ifp->if_softc;
1352 error = vlan_config(ifv, ifp_parent, attrs.vlan_id, attrs.vlan_proto);
1354 if_rele(ifp_parent);
1359 return (nl_modify_ifp_generic(ifp, ifd->lattrs, ifd->bm, ifd->npt));
1363 * {{nla_len=24, nla_type=IFLA_LINKINFO},
1365 * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
1366 * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]}
1369 vlan_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw)
1371 uint32_t parent_index = 0;
1372 uint16_t vlan_id = 0;
1373 uint16_t vlan_proto = 0;
1376 struct ifvlan *ifv = ifp->if_softc;
1377 if (TRUNK(ifv) != NULL)
1378 parent_index = PARENT(ifv)->if_index;
1379 vlan_id = ifv->ifv_vid;
1380 vlan_proto = ifv->ifv_proto;
1383 if (parent_index != 0)
1384 nlattr_add_u32(nw, IFLA_LINK, parent_index);
1386 int off = nlattr_add_nested(nw, IFLA_LINKINFO);
1388 nlattr_add_string(nw, IFLA_INFO_KIND, "vlan");
1389 int off2 = nlattr_add_nested(nw, IFLA_INFO_DATA);
1391 nlattr_add_u16(nw, IFLA_VLAN_ID, vlan_id);
1392 nlattr_add_u16(nw, IFLA_VLAN_PROTOCOL, vlan_proto);
1393 nlattr_set_len(nw, off2);
1395 nlattr_set_len(nw, off);
1400 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
1402 struct ifvlan *ifv = ifp->if_softc;
1403 int unit = ifp->if_dunit;
1405 if (ifp->if_vlantrunk)
1409 IFQ_PURGE(&ifp->if_snd);
1411 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
1412 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
1414 * We should have the only reference to the ifv now, so we can now
1415 * drain any remaining lladdr task before freeing the ifnet and the
1418 taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1422 if (unit != IF_DUNIT_NONE)
1423 ifc_free_unit(ifc, unit);
1429 * The ifp->if_init entry point for vlan(4) is a no-op.
1432 vlan_init(void *foo __unused)
1437 * The if_transmit method for vlan(4) interface.
1440 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1444 int error, len, mcast;
1448 ifv = ifp->if_softc;
1449 if (TRUNK(ifv) == NULL) {
1450 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1455 len = m->m_pkthdr.len;
1456 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1460 #if defined(KERN_TLS) || defined(RATELIMIT)
1461 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1462 struct vlan_snd_tag *vst;
1463 struct m_snd_tag *mst;
1465 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1466 mst = m->m_pkthdr.snd_tag;
1467 vst = mst_to_vst(mst);
1468 if (vst->tag->ifp != p) {
1469 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1474 m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1475 m_snd_tag_rele(mst);
1480 * Do not run parent's if_transmit() if the parent is not up,
1481 * or parent's driver will cause a system crash.
1483 if (!UP_AND_RUNNING(p)) {
1484 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1489 if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) {
1490 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1495 * Send it, precisely as ether_output() would have.
1497 error = (p->if_transmit)(p, m);
1499 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1500 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1501 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1503 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1508 vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1517 * Find the first non-VLAN parent interface.
1519 ifv = ifp->if_softc;
1521 if (TRUNK(ifv) == NULL) {
1527 } while (p->if_type == IFT_L2VLAN);
1529 return p->if_output(ifp, m, dst, ro);
1534 vlan_altq_start(if_t ifp)
1536 struct ifaltq *ifq = &ifp->if_snd;
1540 IFQ_DEQUEUE_NOLOCK(ifq, m);
1542 vlan_transmit(ifp, m);
1543 IFQ_DEQUEUE_NOLOCK(ifq, m);
1549 vlan_altq_transmit(if_t ifp, struct mbuf *m)
1553 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
1554 IFQ_ENQUEUE(&ifp->if_snd, m, err);
1556 vlan_altq_start(ifp);
1558 err = vlan_transmit(ifp, m);
1565 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1568 vlan_qflush(struct ifnet *ifp __unused)
1573 vlan_input(struct ifnet *ifp, struct mbuf *m)
1575 struct ifvlantrunk *trunk;
1582 trunk = ifp->if_vlantrunk;
1583 if (trunk == NULL) {
1588 if (m->m_flags & M_VLANTAG) {
1590 * Packet is tagged, but m contains a normal
1591 * Ethernet frame; the tag is stored out-of-band.
1593 tag = m->m_pkthdr.ether_vtag;
1594 m->m_flags &= ~M_VLANTAG;
1596 struct ether_vlan_header *evl;
1599 * Packet is tagged in-band as specified by 802.1q.
1601 switch (ifp->if_type) {
1603 if (m->m_len < sizeof(*evl) &&
1604 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1605 if_printf(ifp, "cannot pullup VLAN header\n");
1608 evl = mtod(m, struct ether_vlan_header *);
1609 tag = ntohs(evl->evl_tag);
1612 * Remove the 802.1q header by copying the Ethernet
1613 * addresses over it and adjusting the beginning of
1614 * the data in the mbuf. The encapsulated Ethernet
1615 * type field is already in place.
1617 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1618 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1619 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1624 panic("%s: %s has unsupported if_type %u",
1625 __func__, ifp->if_xname, ifp->if_type);
1627 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1633 vid = EVL_VLANOFTAG(tag);
1635 ifv = vlan_gethash(trunk, vid);
1636 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1637 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1642 if (V_vlan_mtag_pcp) {
1644 * While uncommon, it is possible that we will find a 802.1q
1645 * packet encapsulated inside another packet that also had an
1646 * 802.1q header. For example, ethernet tunneled over IPSEC
1647 * arriving over ethernet. In that case, we replace the
1648 * existing 802.1q PCP m_tag value.
1650 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1652 mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1653 sizeof(uint8_t), M_NOWAIT);
1655 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1659 m_tag_prepend(m, mtag);
1661 *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1664 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1665 if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1667 /* Pass it back through the parent's input routine. */
1668 (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1672 vlan_lladdr_fn(void *arg, int pending __unused)
1677 ifv = (struct ifvlan *)arg;
1680 CURVNET_SET(ifp->if_vnet);
1682 /* The ifv_ifp already has the lladdr copied in. */
1683 if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1689 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid,
1692 struct epoch_tracker et;
1693 struct ifvlantrunk *trunk;
1698 * We can handle non-ethernet hardware types as long as
1699 * they handle the tagging and headers themselves.
1701 if (p->if_type != IFT_ETHER &&
1702 p->if_type != IFT_L2VLAN &&
1703 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1704 return (EPROTONOSUPPORT);
1705 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1706 return (EPROTONOSUPPORT);
1708 * Don't let the caller set up a VLAN VID with
1709 * anything except VLID bits.
1710 * VID numbers 0x0 and 0xFFF are reserved.
1712 if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1714 if (ifv->ifv_trunk) {
1715 trunk = ifv->ifv_trunk;
1716 if (trunk->parent != p)
1721 ifv->ifv_proto = proto;
1723 if (ifv->ifv_vid != vid) {
1725 vlan_remhash(trunk, ifv);
1727 error = vlan_inshash(trunk, ifv);
1734 if (p->if_vlantrunk == NULL) {
1735 trunk = malloc(sizeof(struct ifvlantrunk),
1736 M_VLAN, M_WAITOK | M_ZERO);
1737 vlan_inithash(trunk);
1738 TRUNK_LOCK_INIT(trunk);
1740 p->if_vlantrunk = trunk;
1742 if_ref(trunk->parent);
1743 TRUNK_WUNLOCK(trunk);
1745 trunk = p->if_vlantrunk;
1748 ifv->ifv_vid = vid; /* must set this before vlan_inshash() */
1749 ifv->ifv_pcp = 0; /* Default: best effort delivery. */
1750 error = vlan_inshash(trunk, ifv);
1753 ifv->ifv_proto = proto;
1754 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1755 ifv->ifv_mintu = ETHERMIN;
1756 ifv->ifv_pflags = 0;
1757 ifv->ifv_capenable = -1;
1760 * If the parent supports the VLAN_MTU capability,
1761 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1764 if (p->if_capenable & IFCAP_VLAN_MTU) {
1766 * No need to fudge the MTU since the parent can
1767 * handle extended frames.
1769 ifv->ifv_mtufudge = 0;
1772 * Fudge the MTU by the encapsulation size. This
1773 * makes us incompatible with strictly compliant
1774 * 802.1Q implementations, but allows us to use
1775 * the feature with other NetBSD implementations,
1776 * which might still be useful.
1778 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1781 ifv->ifv_trunk = trunk;
1784 * Initialize fields from our parent. This duplicates some
1785 * work with ether_ifattach() but allows for non-ethernet
1786 * interfaces to also work.
1788 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1789 ifp->if_baudrate = p->if_baudrate;
1790 ifp->if_input = p->if_input;
1791 ifp->if_resolvemulti = p->if_resolvemulti;
1792 ifp->if_addrlen = p->if_addrlen;
1793 ifp->if_broadcastaddr = p->if_broadcastaddr;
1794 ifp->if_pcp = ifv->ifv_pcp;
1797 * We wrap the parent's if_output using vlan_output to ensure that it
1798 * can't become stale.
1800 ifp->if_output = vlan_output;
1803 * Copy only a selected subset of flags from the parent.
1804 * Other flags are none of our business.
1806 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1807 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1808 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1809 #undef VLAN_COPY_FLAGS
1811 ifp->if_link_state = p->if_link_state;
1813 NET_EPOCH_ENTER(et);
1814 vlan_capabilities(ifv);
1818 * Set up our interface address to reflect the underlying
1819 * physical interface's.
1821 TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1822 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1826 * Do not schedule link address update if it was the same
1827 * as previous parent's. This helps avoid updating for each
1828 * associated llentry.
1830 if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) {
1831 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1832 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
1835 /* We are ready for operation now. */
1836 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1838 /* Update flags on the parent, if necessary. */
1839 vlan_setflags(ifp, 1);
1842 * Configure multicast addresses that may already be
1843 * joined on the vlan device.
1845 (void)vlan_setmulti(ifp);
1849 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1856 vlan_unconfig(struct ifnet *ifp)
1860 vlan_unconfig_locked(ifp, 0);
1865 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1867 struct ifvlantrunk *trunk;
1868 struct vlan_mc_entry *mc;
1870 struct ifnet *parent;
1873 VLAN_XLOCK_ASSERT();
1875 ifv = ifp->if_softc;
1876 trunk = ifv->ifv_trunk;
1879 if (trunk != NULL) {
1880 parent = trunk->parent;
1883 * Since the interface is being unconfigured, we need to
1884 * empty the list of multicast groups that we may have joined
1885 * while we were alive from the parent's list.
1887 while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1889 * If the parent interface is being detached,
1890 * all its multicast addresses have already
1891 * been removed. Warn about errors if
1892 * if_delmulti() does fail, but don't abort as
1893 * all callers expect vlan destruction to
1897 error = if_delmulti(parent,
1898 (struct sockaddr *)&mc->mc_addr);
1901 "Failed to delete multicast address from parent: %d\n",
1904 CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1905 NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
1908 vlan_setflags(ifp, 0); /* clear special flags on parent */
1910 vlan_remhash(trunk, ifv);
1911 ifv->ifv_trunk = NULL;
1914 * Check if we were the last.
1916 if (trunk->refcnt == 0) {
1917 parent->if_vlantrunk = NULL;
1919 trunk_destroy(trunk);
1923 /* Disconnect from parent. */
1924 if (ifv->ifv_pflags)
1925 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1926 ifp->if_mtu = ETHERMTU;
1927 ifp->if_link_state = LINK_STATE_UNKNOWN;
1928 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1931 * Only dispatch an event if vlan was
1932 * attached, otherwise there is nothing
1933 * to cleanup anyway.
1936 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1939 /* Handle a reference counted flag that should be set on the parent as well */
1941 vlan_setflag(struct ifnet *ifp, int flag, int status,
1942 int (*func)(struct ifnet *, int))
1947 VLAN_SXLOCK_ASSERT();
1949 ifv = ifp->if_softc;
1950 status = status ? (ifp->if_flags & flag) : 0;
1951 /* Now "status" contains the flag value or 0 */
1954 * See if recorded parent's status is different from what
1955 * we want it to be. If it is, flip it. We record parent's
1956 * status in ifv_pflags so that we won't clear parent's flag
1957 * we haven't set. In fact, we don't clear or set parent's
1958 * flags directly, but get or release references to them.
1959 * That's why we can be sure that recorded flags still are
1960 * in accord with actual parent's flags.
1962 if (status != (ifv->ifv_pflags & flag)) {
1963 error = (*func)(PARENT(ifv), status);
1966 ifv->ifv_pflags &= ~flag;
1967 ifv->ifv_pflags |= status;
1973 * Handle IFF_* flags that require certain changes on the parent:
1974 * if "status" is true, update parent's flags respective to our if_flags;
1975 * if "status" is false, forcedly clear the flags set on parent.
1978 vlan_setflags(struct ifnet *ifp, int status)
1982 for (i = 0; vlan_pflags[i].flag; i++) {
1983 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1984 status, vlan_pflags[i].func);
1991 /* Inform all vlans that their parent has changed link state */
1993 vlan_link_state(struct ifnet *ifp)
1995 struct epoch_tracker et;
1996 struct ifvlantrunk *trunk;
1999 NET_EPOCH_ENTER(et);
2000 trunk = ifp->if_vlantrunk;
2001 if (trunk == NULL) {
2007 VLAN_FOREACH(ifv, trunk) {
2008 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
2009 if_link_state_change(ifv->ifv_ifp,
2010 trunk->parent->if_link_state);
2012 TRUNK_WUNLOCK(trunk);
2017 vlan_capabilities(struct ifvlan *ifv)
2021 struct ifnet_hw_tsomax hw_tsomax;
2022 int cap = 0, ena = 0, mena;
2026 VLAN_SXLOCK_ASSERT();
2031 /* Mask parent interface enabled capabilities disabled by user. */
2032 mena = p->if_capenable & ifv->ifv_capenable;
2035 * If the parent interface can do checksum offloading
2036 * on VLANs, then propagate its hardware-assisted
2037 * checksumming flags. Also assert that checksum
2038 * offloading requires hardware VLAN tagging.
2040 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
2041 cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
2042 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
2043 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
2044 ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
2045 if (ena & IFCAP_TXCSUM)
2046 hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
2047 CSUM_UDP | CSUM_SCTP);
2048 if (ena & IFCAP_TXCSUM_IPV6)
2049 hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
2050 CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
2054 * If the parent interface can do TSO on VLANs then
2055 * propagate the hardware-assisted flag. TSO on VLANs
2056 * does not necessarily require hardware VLAN tagging.
2058 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
2059 if_hw_tsomax_common(p, &hw_tsomax);
2060 if_hw_tsomax_update(ifp, &hw_tsomax);
2061 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
2062 cap |= p->if_capabilities & IFCAP_TSO;
2063 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
2064 ena |= mena & IFCAP_TSO;
2065 if (ena & IFCAP_TSO)
2066 hwa |= p->if_hwassist & CSUM_TSO;
2070 * If the parent interface can do LRO and checksum offloading on
2071 * VLANs, then guess it may do LRO on VLANs. False positive here
2072 * cost nothing, while false negative may lead to some confusions.
2074 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
2075 cap |= p->if_capabilities & IFCAP_LRO;
2076 if (p->if_capenable & IFCAP_VLAN_HWCSUM)
2077 ena |= p->if_capenable & IFCAP_LRO;
2080 * If the parent interface can offload TCP connections over VLANs then
2081 * propagate its TOE capability to the VLAN interface.
2083 * All TOE drivers in the tree today can deal with VLANs. If this
2084 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
2087 #define IFCAP_VLAN_TOE IFCAP_TOE
2088 if (p->if_capabilities & IFCAP_VLAN_TOE)
2089 cap |= p->if_capabilities & IFCAP_TOE;
2090 if (p->if_capenable & IFCAP_VLAN_TOE) {
2091 SETTOEDEV(ifp, TOEDEV(p));
2092 ena |= mena & IFCAP_TOE;
2096 * If the parent interface supports dynamic link state, so does the
2099 cap |= (p->if_capabilities & IFCAP_LINKSTATE);
2100 ena |= (mena & IFCAP_LINKSTATE);
2104 * If the parent interface supports ratelimiting, so does the
2107 cap |= (p->if_capabilities & IFCAP_TXRTLMT);
2108 ena |= (mena & IFCAP_TXRTLMT);
2112 * If the parent interface supports unmapped mbufs, so does
2113 * the VLAN interface. Note that this should be fine even for
2114 * interfaces that don't support hardware tagging as headers
2115 * are prepended in normal mbufs to unmapped mbufs holding
2118 cap |= (p->if_capabilities & IFCAP_MEXTPG);
2119 ena |= (mena & IFCAP_MEXTPG);
2122 * If the parent interface can offload encryption and segmentation
2123 * of TLS records over TCP, propagate it's capability to the VLAN
2126 * All TLS drivers in the tree today can deal with VLANs. If
2127 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
2130 if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
2131 cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
2132 if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
2133 ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
2135 ifp->if_capabilities = cap;
2136 ifp->if_capenable = ena;
2137 ifp->if_hwassist = hwa;
2141 vlan_trunk_capabilities(struct ifnet *ifp)
2143 struct epoch_tracker et;
2144 struct ifvlantrunk *trunk;
2148 trunk = ifp->if_vlantrunk;
2149 if (trunk == NULL) {
2153 NET_EPOCH_ENTER(et);
2154 VLAN_FOREACH(ifv, trunk)
2155 vlan_capabilities(ifv);
2161 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2169 struct ifvlantrunk *trunk;
2171 int error = 0, oldmtu;
2173 ifr = (struct ifreq *)data;
2175 ifa = (struct ifaddr *) data;
2177 ifv = ifp->if_softc;
2181 ifp->if_flags |= IFF_UP;
2183 if (ifa->ifa_addr->sa_family == AF_INET)
2184 arp_ifinit(ifp, ifa);
2188 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
2193 if (TRUNK(ifv) != NULL) {
2196 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
2198 /* Limit the result to the parent's current config. */
2200 struct ifmediareq *ifmr;
2202 ifmr = (struct ifmediareq *)data;
2203 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
2204 ifmr->ifm_count = 1;
2205 error = copyout(&ifmr->ifm_current,
2222 * Set the interface MTU.
2226 if (trunk != NULL) {
2229 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
2231 (ifv->ifv_mintu - ifv->ifv_mtufudge))
2234 ifp->if_mtu = ifr->ifr_mtu;
2235 TRUNK_WUNLOCK(trunk);
2244 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
2245 * interface to be delegated to a jail without allowing the
2246 * jail to change what underlying interface/VID it is
2247 * associated with. We are not entirely convinced that this
2248 * is the right way to accomplish that policy goal.
2250 if (ifp->if_vnet != ifp->if_home_vnet) {
2255 error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
2258 if (vlr.vlr_parent[0] == '\0') {
2262 p = ifunit_ref(vlr.vlr_parent);
2267 #ifdef COMPAT_FREEBSD12
2268 if (vlr.vlr_proto == 0)
2269 vlr.vlr_proto = ETHERTYPE_VLAN;
2271 oldmtu = ifp->if_mtu;
2272 error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto);
2276 * VLAN MTU may change during addition of the vlandev.
2277 * If it did, do network layer specific procedure.
2279 if (ifp->if_mtu != oldmtu)
2285 if (ifp->if_vnet != ifp->if_home_vnet) {
2290 bzero(&vlr, sizeof(vlr));
2292 if (TRUNK(ifv) != NULL) {
2293 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
2294 sizeof(vlr.vlr_parent));
2295 vlr.vlr_tag = ifv->ifv_vid;
2296 vlr.vlr_proto = ifv->ifv_proto;
2299 error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
2304 * We should propagate selected flags to the parent,
2305 * e.g., promiscuous mode.
2308 if (TRUNK(ifv) != NULL)
2309 error = vlan_setflags(ifp, 1);
2316 * If we don't have a parent, just remember the membership for
2319 * XXX We need the rmlock here to avoid sleeping while
2320 * holding in6_multi_mtx.
2325 error = vlan_setmulti(ifp);
2331 if (ifp->if_vnet != ifp->if_home_vnet) {
2336 ifr->ifr_vlan_pcp = ifv->ifv_pcp;
2341 if (ifp->if_vnet != ifp->if_home_vnet) {
2346 error = priv_check(curthread, PRIV_NET_SETVLANPCP);
2349 if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) {
2353 ifv->ifv_pcp = ifr->ifr_vlan_pcp;
2354 ifp->if_pcp = ifv->ifv_pcp;
2355 /* broadcast event about PCP change */
2356 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
2361 ifv->ifv_capenable = ifr->ifr_reqcap;
2363 if (trunk != NULL) {
2364 struct epoch_tracker et;
2366 NET_EPOCH_ENTER(et);
2367 vlan_capabilities(ifv);
2381 #if defined(KERN_TLS) || defined(RATELIMIT)
2383 vlan_snd_tag_alloc(struct ifnet *ifp,
2384 union if_snd_tag_alloc_params *params,
2385 struct m_snd_tag **ppmt)
2387 struct epoch_tracker et;
2388 const struct if_snd_tag_sw *sw;
2389 struct vlan_snd_tag *vst;
2391 struct ifnet *parent;
2392 struct m_snd_tag *mst;
2395 NET_EPOCH_ENTER(et);
2396 ifv = ifp->if_softc;
2398 switch (params->hdr.type) {
2400 case IF_SND_TAG_TYPE_UNLIMITED:
2401 sw = &vlan_snd_tag_ul_sw;
2403 case IF_SND_TAG_TYPE_RATE_LIMIT:
2404 sw = &vlan_snd_tag_rl_sw;
2408 case IF_SND_TAG_TYPE_TLS:
2409 sw = &vlan_snd_tag_tls_sw;
2411 case IF_SND_TAG_TYPE_TLS_RX:
2413 if (params->tls_rx.vlan_id != 0)
2415 params->tls_rx.vlan_id = ifv->ifv_vid;
2418 case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
2419 sw = &vlan_snd_tag_tls_rl_sw;
2427 if (ifv->ifv_trunk != NULL)
2428 parent = PARENT(ifv);
2437 vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2445 error = m_snd_tag_alloc(parent, params, &mst);
2453 m_snd_tag_init(&vst->com, ifp, sw);
2463 return (EOPNOTSUPP);
2466 static struct m_snd_tag *
2467 vlan_next_snd_tag(struct m_snd_tag *mst)
2469 struct vlan_snd_tag *vst;
2471 vst = mst_to_vst(mst);
2476 vlan_snd_tag_modify(struct m_snd_tag *mst,
2477 union if_snd_tag_modify_params *params)
2479 struct vlan_snd_tag *vst;
2481 vst = mst_to_vst(mst);
2482 return (vst->tag->sw->snd_tag_modify(vst->tag, params));
2486 vlan_snd_tag_query(struct m_snd_tag *mst,
2487 union if_snd_tag_query_params *params)
2489 struct vlan_snd_tag *vst;
2491 vst = mst_to_vst(mst);
2492 return (vst->tag->sw->snd_tag_query(vst->tag, params));
2496 vlan_snd_tag_free(struct m_snd_tag *mst)
2498 struct vlan_snd_tag *vst;
2500 vst = mst_to_vst(mst);
2501 m_snd_tag_rele(vst->tag);
2506 vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
2509 * For vlan, we have an indirect
2510 * interface. The caller needs to
2511 * get a ratelimit tag on the actual
2512 * interface the flow will go on.
2514 q->rate_table = NULL;
2515 q->flags = RT_IS_INDIRECT;
2517 q->number_of_rates = 0;