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$");
51 #include <sys/param.h>
52 #include <sys/eventhandler.h>
53 #include <sys/kernel.h>
55 #include <sys/malloc.h>
57 #include <sys/module.h>
58 #include <sys/rmlock.h>
60 #include <sys/queue.h>
61 #include <sys/socket.h>
62 #include <sys/sockio.h>
63 #include <sys/sysctl.h>
64 #include <sys/systm.h>
66 #include <sys/taskqueue.h>
69 #include <net/ethernet.h>
71 #include <net/if_var.h>
72 #include <net/if_clone.h>
73 #include <net/if_dl.h>
74 #include <net/if_types.h>
75 #include <net/if_vlan_var.h>
79 #include <netinet/in.h>
80 #include <netinet/if_ether.h>
83 #define VLAN_DEF_HWIDTH 4
84 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
86 #define UP_AND_RUNNING(ifp) \
87 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
89 LIST_HEAD(ifvlanhead, ifvlan);
92 struct ifnet *parent; /* parent interface of this trunk */
95 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
96 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
98 struct ifvlanhead *hash; /* dynamic hash-list table */
106 * This macro provides a facility to iterate over every vlan on a trunk with
107 * the assumption that none will be added/removed during iteration.
110 #define VLAN_FOREACH(_ifv, _trunk) \
112 for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
113 if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
114 #else /* VLAN_ARRAY */
115 #define VLAN_FOREACH(_ifv, _trunk) \
116 struct ifvlan *_next; \
118 for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
119 LIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
120 #endif /* VLAN_ARRAY */
123 * This macro provides a facility to iterate over every vlan on a trunk while
124 * also modifying the number of vlans on the trunk. The iteration continues
125 * until some condition is met or there are no more vlans on the trunk.
128 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
129 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
131 for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
132 if (((_ifv) = (_trunk)->vlans[_i]))
133 #else /* VLAN_ARRAY */
135 * The hash table case is more complicated. We allow for the hash table to be
136 * modified (i.e. vlans removed) while we are iterating over it. To allow for
137 * this we must restart the iteration every time we "touch" something during
138 * the iteration, since removal will resize the hash table and invalidate our
139 * current position. If acting on the touched element causes the trunk to be
140 * emptied, then iteration also stops.
142 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
144 bool _touch = false; \
146 !(_cond) && _i < (1 << (_trunk)->hwidth); \
147 _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
148 if (((_ifv) = LIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
150 #endif /* VLAN_ARRAY */
152 struct vlan_mc_entry {
153 struct sockaddr_dl mc_addr;
154 SLIST_ENTRY(vlan_mc_entry) mc_entries;
158 struct ifvlantrunk *ifv_trunk;
159 struct ifnet *ifv_ifp;
160 #define TRUNK(ifv) ((ifv)->ifv_trunk)
161 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
163 int ifv_pflags; /* special flags we have set on parent */
166 int ifvm_encaplen; /* encapsulation length */
167 int ifvm_mtufudge; /* MTU fudged by this much */
168 int ifvm_mintu; /* min transmission unit */
169 uint16_t ifvm_proto; /* encapsulation ethertype */
170 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
171 uint16_t ifvm_vid; /* VLAN ID */
172 uint8_t ifvm_pcp; /* Priority Code Point (PCP). */
174 struct task lladdr_task;
175 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
177 LIST_ENTRY(ifvlan) ifv_list;
180 #define ifv_proto ifv_mib.ifvm_proto
181 #define ifv_tag ifv_mib.ifvm_tag
182 #define ifv_vid ifv_mib.ifvm_vid
183 #define ifv_pcp ifv_mib.ifvm_pcp
184 #define ifv_encaplen ifv_mib.ifvm_encaplen
185 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
186 #define ifv_mintu ifv_mib.ifvm_mintu
188 /* Special flags we should propagate to parent. */
191 int (*func)(struct ifnet *, int);
193 {IFF_PROMISC, ifpromisc},
194 {IFF_ALLMULTI, if_allmulti},
198 extern int vlan_mtag_pcp;
200 static const char vlanname[] = "vlan";
201 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
203 static eventhandler_tag ifdetach_tag;
204 static eventhandler_tag iflladdr_tag;
207 * if_vlan uses two module-level locks to allow concurrent modification of vlan
208 * interfaces and (mostly) allow for vlans to be destroyed while they are being
209 * used for tx/rx. To accomplish this in a way that has acceptable performance
210 * and cooperation with other parts of the network stack there is a
211 * non-sleepable rmlock(9) and an sx(9). Both locks are exclusively acquired
212 * when destroying a vlan interface, i.e. when the if_vlantrunk field of struct
213 * ifnet is de-allocated and NULL'd. Thus a reader holding either lock has a
214 * guarantee that the struct ifvlantrunk references a valid vlan trunk.
216 * The performance-sensitive paths that warrant using the rmlock(9) are
217 * vlan_transmit and vlan_input. Both have to check for the vlan interface's
218 * existence using if_vlantrunk, and being in the network tx/rx paths the use
219 * of an rmlock(9) gives a measureable improvement in performance.
221 * The reason for having an sx(9) is mostly because there are still areas that
222 * must be sleepable and also have safe concurrent access to a vlan interface.
223 * Since the sx(9) exists, it is used by default in most paths unless sleeping
224 * is not permitted, or if it is not clear whether sleeping is permitted.
226 * Note that despite these protections, there is still an inherent race in the
227 * destruction of vlans since there's no guarantee that the ifnet hasn't been
228 * freed/reused when the tx/rx functions are called by the stack. This can only
229 * be fixed by addressing ifnet's lifetime issues.
231 #define _VLAN_RM_ID ifv_rm_lock
232 #define _VLAN_SX_ID ifv_sx
234 static struct rmlock _VLAN_RM_ID;
235 static struct sx _VLAN_SX_ID;
237 #define VLAN_LOCKING_INIT() \
238 rm_init(&_VLAN_RM_ID, "vlan_rm"); \
239 sx_init(&_VLAN_SX_ID, "vlan_sx")
241 #define VLAN_LOCKING_DESTROY() \
242 rm_destroy(&_VLAN_RM_ID); \
243 sx_destroy(&_VLAN_SX_ID)
245 #define _VLAN_RM_TRACKER _vlan_rm_tracker
246 #define VLAN_RLOCK() rm_rlock(&_VLAN_RM_ID, \
248 #define VLAN_RUNLOCK() rm_runlock(&_VLAN_RM_ID, \
250 #define VLAN_WLOCK() rm_wlock(&_VLAN_RM_ID)
251 #define VLAN_WUNLOCK() rm_wunlock(&_VLAN_RM_ID)
252 #define VLAN_RLOCK_ASSERT() rm_assert(&_VLAN_RM_ID, RA_RLOCKED)
253 #define VLAN_WLOCK_ASSERT() rm_assert(&_VLAN_RM_ID, RA_WLOCKED)
254 #define VLAN_RWLOCK_ASSERT() rm_assert(&_VLAN_RM_ID, RA_LOCKED)
255 #define VLAN_LOCK_READER struct rm_priotracker _VLAN_RM_TRACKER
257 #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID)
258 #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID)
259 #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID)
260 #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID)
261 #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
262 #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
263 #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED)
267 * We also have a per-trunk rmlock(9), that is locked shared on packet
268 * processing and exclusive when configuration is changed. Note: This should
269 * only be acquired while there is a shared lock on either of the global locks
270 * via VLAN_SLOCK or VLAN_RLOCK. Thus, an exclusive lock on the global locks
271 * makes a call to TRUNK_RLOCK/TRUNK_WLOCK technically superfluous.
273 #define _TRUNK_RM_TRACKER _trunk_rm_tracker
274 #define TRUNK_LOCK_INIT(trunk) rm_init(&(trunk)->lock, vlanname)
275 #define TRUNK_LOCK_DESTROY(trunk) rm_destroy(&(trunk)->lock)
276 #define TRUNK_RLOCK(trunk) rm_rlock(&(trunk)->lock, \
278 #define TRUNK_WLOCK(trunk) rm_wlock(&(trunk)->lock)
279 #define TRUNK_RUNLOCK(trunk) rm_runlock(&(trunk)->lock, \
281 #define TRUNK_WUNLOCK(trunk) rm_wunlock(&(trunk)->lock)
282 #define TRUNK_RLOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_RLOCKED)
283 #define TRUNK_LOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_LOCKED)
284 #define TRUNK_WLOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_WLOCKED)
285 #define TRUNK_LOCK_READER struct rm_priotracker _TRUNK_RM_TRACKER
288 * The VLAN_ARRAY substitutes the dynamic hash with a static array
289 * with 4096 entries. In theory this can give a boost in processing,
290 * however in practice it does not. Probably this is because the array
291 * is too big to fit into CPU cache.
294 static void vlan_inithash(struct ifvlantrunk *trunk);
295 static void vlan_freehash(struct ifvlantrunk *trunk);
296 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
297 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
298 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
299 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
302 static void trunk_destroy(struct ifvlantrunk *trunk);
304 static void vlan_init(void *foo);
305 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
306 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
307 static void vlan_qflush(struct ifnet *ifp);
308 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
309 int (*func)(struct ifnet *, int));
310 static int vlan_setflags(struct ifnet *ifp, int status);
311 static int vlan_setmulti(struct ifnet *ifp);
312 static int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
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);
316 static void vlan_link_state(struct ifnet *ifp);
317 static void vlan_capabilities(struct ifvlan *ifv);
318 static void vlan_trunk_capabilities(struct ifnet *ifp);
320 static struct ifnet *vlan_clone_match_ethervid(const char *, int *);
321 static int vlan_clone_match(struct if_clone *, const char *);
322 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
323 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
325 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
326 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
328 static void vlan_lladdr_fn(void *arg, int pending);
330 static struct if_clone *vlan_cloner;
333 static VNET_DEFINE(struct if_clone *, vlan_cloner);
334 #define V_vlan_cloner VNET(vlan_cloner)
338 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
341 vlan_inithash(struct ifvlantrunk *trunk)
346 * The trunk must not be locked here since we call malloc(M_WAITOK).
347 * It is OK in case this function is called before the trunk struct
348 * gets hooked up and becomes visible from other threads.
351 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
352 ("%s: hash already initialized", __func__));
354 trunk->hwidth = VLAN_DEF_HWIDTH;
355 n = 1 << trunk->hwidth;
356 trunk->hmask = n - 1;
357 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
358 for (i = 0; i < n; i++)
359 LIST_INIT(&trunk->hash[i]);
363 vlan_freehash(struct ifvlantrunk *trunk)
368 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
369 for (i = 0; i < (1 << trunk->hwidth); i++)
370 KASSERT(LIST_EMPTY(&trunk->hash[i]),
371 ("%s: hash table not empty", __func__));
373 free(trunk->hash, M_VLAN);
375 trunk->hwidth = trunk->hmask = 0;
379 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
384 TRUNK_WLOCK_ASSERT(trunk);
385 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
387 b = 1 << trunk->hwidth;
388 i = HASH(ifv->ifv_vid, trunk->hmask);
389 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
390 if (ifv->ifv_vid == ifv2->ifv_vid)
394 * Grow the hash when the number of vlans exceeds half of the number of
395 * hash buckets squared. This will make the average linked-list length
398 if (trunk->refcnt > (b * b) / 2) {
399 vlan_growhash(trunk, 1);
400 i = HASH(ifv->ifv_vid, trunk->hmask);
402 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
409 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
414 TRUNK_WLOCK_ASSERT(trunk);
415 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
417 b = 1 << trunk->hwidth;
418 i = HASH(ifv->ifv_vid, trunk->hmask);
419 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
422 LIST_REMOVE(ifv2, ifv_list);
423 if (trunk->refcnt < (b * b) / 2)
424 vlan_growhash(trunk, -1);
428 panic("%s: vlan not found\n", __func__);
429 return (ENOENT); /*NOTREACHED*/
433 * Grow the hash larger or smaller if memory permits.
436 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
439 struct ifvlanhead *hash2;
440 int hwidth2, i, j, n, n2;
442 TRUNK_WLOCK_ASSERT(trunk);
443 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
446 /* Harmless yet obvious coding error */
447 printf("%s: howmuch is 0\n", __func__);
451 hwidth2 = trunk->hwidth + howmuch;
452 n = 1 << trunk->hwidth;
454 /* Do not shrink the table below the default */
455 if (hwidth2 < VLAN_DEF_HWIDTH)
458 /* M_NOWAIT because we're called with trunk mutex held */
459 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
461 printf("%s: out of memory -- hash size not changed\n",
463 return; /* We can live with the old hash table */
465 for (j = 0; j < n2; j++)
466 LIST_INIT(&hash2[j]);
467 for (i = 0; i < n; i++)
468 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
469 LIST_REMOVE(ifv, ifv_list);
470 j = HASH(ifv->ifv_vid, n2 - 1);
471 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
473 free(trunk->hash, M_VLAN);
475 trunk->hwidth = hwidth2;
476 trunk->hmask = n2 - 1;
479 if_printf(trunk->parent,
480 "VLAN hash table resized from %d to %d buckets\n", n, n2);
483 static __inline struct ifvlan *
484 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
488 TRUNK_RLOCK_ASSERT(trunk);
490 LIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
491 if (ifv->ifv_vid == vid)
497 /* Debugging code to view the hashtables. */
499 vlan_dumphash(struct ifvlantrunk *trunk)
504 for (i = 0; i < (1 << trunk->hwidth); i++) {
506 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
507 printf("%s ", ifv->ifv_ifp->if_xname);
514 static __inline struct ifvlan *
515 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
518 return trunk->vlans[vid];
522 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
525 if (trunk->vlans[ifv->ifv_vid] != NULL)
527 trunk->vlans[ifv->ifv_vid] = ifv;
534 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
537 trunk->vlans[ifv->ifv_vid] = NULL;
544 vlan_freehash(struct ifvlantrunk *trunk)
549 vlan_inithash(struct ifvlantrunk *trunk)
553 #endif /* !VLAN_ARRAY */
556 trunk_destroy(struct ifvlantrunk *trunk)
561 vlan_freehash(trunk);
562 trunk->parent->if_vlantrunk = NULL;
563 TRUNK_LOCK_DESTROY(trunk);
564 if_rele(trunk->parent);
569 * Program our multicast filter. What we're actually doing is
570 * programming the multicast filter of the parent. This has the
571 * side effect of causing the parent interface to receive multicast
572 * traffic that it doesn't really want, which ends up being discarded
573 * later by the upper protocol layers. Unfortunately, there's no way
574 * to avoid this: there really is only one physical interface.
577 vlan_setmulti(struct ifnet *ifp)
580 struct ifmultiaddr *ifma;
582 struct vlan_mc_entry *mc;
586 * XXX This stupidly needs the rmlock to avoid sleeping while holding
587 * the in6_multi_mtx (see in6_mc_join_locked).
589 VLAN_RWLOCK_ASSERT();
591 /* Find the parent. */
593 TRUNK_WLOCK_ASSERT(TRUNK(sc));
596 CURVNET_SET_QUIET(ifp_p->if_vnet);
598 /* First, remove any existing filter entries. */
599 while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
600 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
601 (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
605 /* Now program new ones. */
607 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
608 if (ifma->ifma_addr->sa_family != AF_LINK)
610 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
612 IF_ADDR_WUNLOCK(ifp);
615 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
616 mc->mc_addr.sdl_index = ifp_p->if_index;
617 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
619 IF_ADDR_WUNLOCK(ifp);
620 SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
621 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
632 * A handler for parent interface link layer address changes.
633 * If the parent interface link layer address is changed we
634 * should also change it on all children vlans.
637 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
640 struct ifnet *ifv_ifp;
641 struct ifvlantrunk *trunk;
642 struct sockaddr_dl *sdl;
645 /* Need the rmlock since this is run on taskqueue_swi. */
647 trunk = ifp->if_vlantrunk;
654 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
655 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
656 * ioctl calls on the parent garbling the lladdr of the child vlan.
659 VLAN_FOREACH(ifv, trunk) {
661 * Copy new new lladdr into the ifv_ifp, enqueue a task
662 * to actually call if_setlladdr. if_setlladdr needs to
663 * be deferred to a taskqueue because it will call into
664 * the if_vlan ioctl path and try to acquire the global
667 ifv_ifp = ifv->ifv_ifp;
668 bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
670 sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
671 sdl->sdl_alen = ifp->if_addrlen;
672 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
674 TRUNK_WUNLOCK(trunk);
679 * A handler for network interface departure events.
680 * Track departure of trunks here so that we don't access invalid
681 * pointers or whatever if a trunk is ripped from under us, e.g.,
682 * by ejecting its hot-plug card. However, if an ifnet is simply
683 * being renamed, then there's no need to tear down the state.
686 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
689 struct ifvlantrunk *trunk;
691 /* If the ifnet is just being renamed, don't do anything. */
692 if (ifp->if_flags & IFF_RENAMING)
695 trunk = ifp->if_vlantrunk;
702 * OK, it's a trunk. Loop over and detach all vlan's on it.
703 * Check trunk pointer after each vlan_unconfig() as it will
704 * free it and set to NULL after the last vlan was detached.
706 VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
707 ifp->if_vlantrunk == NULL)
708 vlan_unconfig_locked(ifv->ifv_ifp, 1);
710 /* Trunk should have been destroyed in vlan_unconfig(). */
711 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
716 * Return the trunk device for a virtual interface.
718 static struct ifnet *
719 vlan_trunkdev(struct ifnet *ifp)
724 if (ifp->if_type != IFT_L2VLAN)
727 /* Not clear if callers are sleepable, so acquire the rmlock. */
738 * Return the 12-bit VLAN VID for this interface, for use by external
739 * components such as Infiniband.
741 * XXXRW: Note that the function name here is historical; it should be named
745 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
749 if (ifp->if_type != IFT_L2VLAN)
752 *vidp = ifv->ifv_vid;
757 * Return a driver specific cookie for this interface. Synchronization
758 * with setcookie must be provided by the driver.
761 vlan_cookie(struct ifnet *ifp)
765 if (ifp->if_type != IFT_L2VLAN)
768 return (ifv->ifv_cookie);
772 * Store a cookie in our softc that drivers can use to store driver
773 * private per-instance data in.
776 vlan_setcookie(struct ifnet *ifp, void *cookie)
780 if (ifp->if_type != IFT_L2VLAN)
783 ifv->ifv_cookie = cookie;
788 * Return the vlan device present at the specific VID.
790 static struct ifnet *
791 vlan_devat(struct ifnet *ifp, uint16_t vid)
793 struct ifvlantrunk *trunk;
798 /* Not clear if callers are sleepable, so acquire the rmlock. */
800 trunk = ifp->if_vlantrunk;
807 ifv = vlan_gethash(trunk, vid);
810 TRUNK_RUNLOCK(trunk);
816 * Recalculate the cached VLAN tag exposed via the MIB.
819 vlan_tag_recalculate(struct ifvlan *ifv)
822 ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
826 * VLAN support can be loaded as a module. The only place in the
827 * system that's intimately aware of this is ether_input. We hook
828 * into this code through vlan_input_p which is defined there and
829 * set here. No one else in the system should be aware of this so
830 * we use an explicit reference here.
832 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
834 /* For if_link_state_change() eyes only... */
835 extern void (*vlan_link_state_p)(struct ifnet *);
838 vlan_modevent(module_t mod, int type, void *data)
843 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
844 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
845 if (ifdetach_tag == NULL)
847 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
848 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
849 if (iflladdr_tag == NULL)
852 vlan_input_p = vlan_input;
853 vlan_link_state_p = vlan_link_state;
854 vlan_trunk_cap_p = vlan_trunk_capabilities;
855 vlan_trunkdev_p = vlan_trunkdev;
856 vlan_cookie_p = vlan_cookie;
857 vlan_setcookie_p = vlan_setcookie;
858 vlan_tag_p = vlan_tag;
859 vlan_devat_p = vlan_devat;
861 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
862 vlan_clone_create, vlan_clone_destroy);
865 printf("vlan: initialized, using "
869 "hash tables with chaining"
876 if_clone_detach(vlan_cloner);
878 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
879 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
881 vlan_link_state_p = NULL;
882 vlan_trunk_cap_p = NULL;
883 vlan_trunkdev_p = NULL;
885 vlan_cookie_p = NULL;
886 vlan_setcookie_p = NULL;
888 VLAN_LOCKING_DESTROY();
890 printf("vlan: unloaded\n");
898 static moduledata_t vlan_mod = {
904 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
905 MODULE_VERSION(if_vlan, 3);
909 vnet_vlan_init(const void *unused __unused)
912 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
913 vlan_clone_create, vlan_clone_destroy);
914 V_vlan_cloner = vlan_cloner;
916 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
917 vnet_vlan_init, NULL);
920 vnet_vlan_uninit(const void *unused __unused)
923 if_clone_detach(V_vlan_cloner);
925 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
926 vnet_vlan_uninit, NULL);
930 * Check for <etherif>.<vlan> style interface names.
932 static struct ifnet *
933 vlan_clone_match_ethervid(const char *name, int *vidp)
935 char ifname[IFNAMSIZ];
940 strlcpy(ifname, name, IFNAMSIZ);
941 if ((cp = strchr(ifname, '.')) == NULL)
944 if ((ifp = ifunit_ref(ifname)) == NULL)
952 for(; *cp >= '0' && *cp <= '9'; cp++)
953 vid = (vid * 10) + (*cp - '0');
965 vlan_clone_match(struct if_clone *ifc, const char *name)
969 if (vlan_clone_match_ethervid(name, NULL) != NULL)
972 if (strncmp(vlanname, name, strlen(vlanname)) != 0)
974 for (cp = name + 4; *cp != '\0'; cp++) {
975 if (*cp < '0' || *cp > '9')
983 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
994 struct sockaddr_dl *sdl;
996 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
999 * There are 3 (ugh) ways to specify the cloned device:
1000 * o pass a parameter block with the clone request.
1001 * o specify parameters in the text of the clone device name
1002 * o specify no parameters and get an unattached device that
1003 * must be configured separately.
1004 * The first technique is preferred; the latter two are
1005 * supported for backwards compatibility.
1007 * XXXRW: Note historic use of the word "tag" here. New ioctls may be
1011 error = copyin(params, &vlr, sizeof(vlr));
1014 p = ifunit_ref(vlr.vlr_parent);
1017 error = ifc_name2unit(name, &unit);
1023 wildcard = (unit < 0);
1024 } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1029 error = ifc_name2unit(name, &unit);
1033 wildcard = (unit < 0);
1036 error = ifc_alloc_unit(ifc, &unit);
1043 /* In the wildcard case, we need to update the name. */
1045 for (dp = name; *dp != '\0'; dp++);
1046 if (snprintf(dp, len - (dp-name), "%d", unit) >
1047 len - (dp-name) - 1) {
1048 panic("%s: interface name too long", __func__);
1052 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1053 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1055 ifc_free_unit(ifc, unit);
1061 SLIST_INIT(&ifv->vlan_mc_listhead);
1062 ifp->if_softc = ifv;
1064 * Set the name manually rather than using if_initname because
1065 * we don't conform to the default naming convention for interfaces.
1067 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1068 ifp->if_dname = vlanname;
1069 ifp->if_dunit = unit;
1070 /* NB: flags are not set here */
1071 ifp->if_linkmib = &ifv->ifv_mib;
1072 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
1073 /* NB: mtu is not set here */
1075 ifp->if_init = vlan_init;
1076 ifp->if_transmit = vlan_transmit;
1077 ifp->if_qflush = vlan_qflush;
1078 ifp->if_ioctl = vlan_ioctl;
1079 ifp->if_flags = VLAN_IFFLAGS;
1080 ether_ifattach(ifp, eaddr);
1081 /* Now undo some of the damage... */
1082 ifp->if_baudrate = 0;
1083 ifp->if_type = IFT_L2VLAN;
1084 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1086 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1087 sdl->sdl_type = IFT_L2VLAN;
1090 error = vlan_config(ifv, p, vid);
1094 * Since we've partially failed, we need to back
1095 * out all the way, otherwise userland could get
1096 * confused. Thus, we destroy the interface.
1098 ether_ifdetach(ifp);
1101 ifc_free_unit(ifc, unit);
1112 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
1114 struct ifvlan *ifv = ifp->if_softc;
1115 int unit = ifp->if_dunit;
1117 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
1118 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
1120 * We should have the only reference to the ifv now, so we can now
1121 * drain any remaining lladdr task before freeing the ifnet and the
1124 taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1127 ifc_free_unit(ifc, unit);
1133 * The ifp->if_init entry point for vlan(4) is a no-op.
1136 vlan_init(void *foo __unused)
1141 * The if_transmit method for vlan(4) interface.
1144 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1148 int error, len, mcast;
1152 ifv = ifp->if_softc;
1153 if (TRUNK(ifv) == NULL) {
1154 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1160 len = m->m_pkthdr.len;
1161 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1166 * Do not run parent's if_transmit() if the parent is not up,
1167 * or parent's driver will cause a system crash.
1169 if (!UP_AND_RUNNING(p)) {
1170 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1176 if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1177 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1183 * Send it, precisely as ether_output() would have.
1185 error = (p->if_transmit)(p, m);
1187 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1188 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1189 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1191 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1197 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1200 vlan_qflush(struct ifnet *ifp __unused)
1205 vlan_input(struct ifnet *ifp, struct mbuf *m)
1207 struct ifvlantrunk *trunk;
1215 trunk = ifp->if_vlantrunk;
1216 if (trunk == NULL) {
1222 if (m->m_flags & M_VLANTAG) {
1224 * Packet is tagged, but m contains a normal
1225 * Ethernet frame; the tag is stored out-of-band.
1227 tag = m->m_pkthdr.ether_vtag;
1228 m->m_flags &= ~M_VLANTAG;
1230 struct ether_vlan_header *evl;
1233 * Packet is tagged in-band as specified by 802.1q.
1235 switch (ifp->if_type) {
1237 if (m->m_len < sizeof(*evl) &&
1238 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1239 if_printf(ifp, "cannot pullup VLAN header\n");
1243 evl = mtod(m, struct ether_vlan_header *);
1244 tag = ntohs(evl->evl_tag);
1247 * Remove the 802.1q header by copying the Ethernet
1248 * addresses over it and adjusting the beginning of
1249 * the data in the mbuf. The encapsulated Ethernet
1250 * type field is already in place.
1252 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1253 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1254 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1259 panic("%s: %s has unsupported if_type %u",
1260 __func__, ifp->if_xname, ifp->if_type);
1262 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1269 vid = EVL_VLANOFTAG(tag);
1272 ifv = vlan_gethash(trunk, vid);
1273 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1274 TRUNK_RUNLOCK(trunk);
1275 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1280 TRUNK_RUNLOCK(trunk);
1282 if (vlan_mtag_pcp) {
1284 * While uncommon, it is possible that we will find a 802.1q
1285 * packet encapsulated inside another packet that also had an
1286 * 802.1q header. For example, ethernet tunneled over IPSEC
1287 * arriving over ethernet. In that case, we replace the
1288 * existing 802.1q PCP m_tag value.
1290 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1292 mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1293 sizeof(uint8_t), M_NOWAIT);
1295 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1300 m_tag_prepend(m, mtag);
1302 *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1305 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1306 if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1309 /* Pass it back through the parent's input routine. */
1310 (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1314 vlan_lladdr_fn(void *arg, int pending __unused)
1319 ifv = (struct ifvlan *)arg;
1322 CURVNET_SET(ifp->if_vnet);
1324 /* The ifv_ifp already has the lladdr copied in. */
1325 if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1331 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
1333 struct ifvlantrunk *trunk;
1338 * We can handle non-ethernet hardware types as long as
1339 * they handle the tagging and headers themselves.
1341 if (p->if_type != IFT_ETHER &&
1342 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1343 return (EPROTONOSUPPORT);
1344 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1345 return (EPROTONOSUPPORT);
1347 * Don't let the caller set up a VLAN VID with
1348 * anything except VLID bits.
1349 * VID numbers 0x0 and 0xFFF are reserved.
1351 if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1356 /* Acquire rmlock after the branch so we can M_WAITOK. */
1358 if (p->if_vlantrunk == NULL) {
1359 trunk = malloc(sizeof(struct ifvlantrunk),
1360 M_VLAN, M_WAITOK | M_ZERO);
1361 vlan_inithash(trunk);
1362 TRUNK_LOCK_INIT(trunk);
1365 p->if_vlantrunk = trunk;
1367 if_ref(trunk->parent);
1370 trunk = p->if_vlantrunk;
1374 ifv->ifv_vid = vid; /* must set this before vlan_inshash() */
1375 ifv->ifv_pcp = 0; /* Default: best effort delivery. */
1376 vlan_tag_recalculate(ifv);
1377 error = vlan_inshash(trunk, ifv);
1380 ifv->ifv_proto = ETHERTYPE_VLAN;
1381 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1382 ifv->ifv_mintu = ETHERMIN;
1383 ifv->ifv_pflags = 0;
1384 ifv->ifv_capenable = -1;
1387 * If the parent supports the VLAN_MTU capability,
1388 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1391 if (p->if_capenable & IFCAP_VLAN_MTU) {
1393 * No need to fudge the MTU since the parent can
1394 * handle extended frames.
1396 ifv->ifv_mtufudge = 0;
1399 * Fudge the MTU by the encapsulation size. This
1400 * makes us incompatible with strictly compliant
1401 * 802.1Q implementations, but allows us to use
1402 * the feature with other NetBSD implementations,
1403 * which might still be useful.
1405 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1408 ifv->ifv_trunk = trunk;
1411 * Initialize fields from our parent. This duplicates some
1412 * work with ether_ifattach() but allows for non-ethernet
1413 * interfaces to also work.
1415 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1416 ifp->if_baudrate = p->if_baudrate;
1417 ifp->if_output = p->if_output;
1418 ifp->if_input = p->if_input;
1419 ifp->if_resolvemulti = p->if_resolvemulti;
1420 ifp->if_addrlen = p->if_addrlen;
1421 ifp->if_broadcastaddr = p->if_broadcastaddr;
1424 * Copy only a selected subset of flags from the parent.
1425 * Other flags are none of our business.
1427 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1428 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1429 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1430 #undef VLAN_COPY_FLAGS
1432 ifp->if_link_state = p->if_link_state;
1434 vlan_capabilities(ifv);
1437 * Set up our interface address to reflect the underlying
1438 * physical interface's.
1440 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1441 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1445 * Configure multicast addresses that may already be
1446 * joined on the vlan device.
1448 (void)vlan_setmulti(ifp);
1450 TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1452 /* We are ready for operation now. */
1453 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1455 /* Update flags on the parent, if necessary. */
1456 vlan_setflags(ifp, 1);
1459 * We need to drop the non-sleepable rmlock so that the underlying
1460 * devices can sleep in their vlan_config hooks.
1462 TRUNK_WUNLOCK(trunk);
1465 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1472 vlan_unconfig(struct ifnet *ifp)
1476 vlan_unconfig_locked(ifp, 0);
1481 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1483 struct ifvlantrunk *trunk;
1484 struct vlan_mc_entry *mc;
1486 struct ifnet *parent;
1489 VLAN_XLOCK_ASSERT();
1491 ifv = ifp->if_softc;
1492 trunk = ifv->ifv_trunk;
1495 if (trunk != NULL) {
1497 * Both vlan_transmit and vlan_input rely on the trunk fields
1498 * being NULL to determine whether to bail, so we need to get
1499 * an exclusive lock here to prevent them from using bad
1503 parent = trunk->parent;
1506 * Since the interface is being unconfigured, we need to
1507 * empty the list of multicast groups that we may have joined
1508 * while we were alive from the parent's list.
1510 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1512 * If the parent interface is being detached,
1513 * all its multicast addresses have already
1514 * been removed. Warn about errors if
1515 * if_delmulti() does fail, but don't abort as
1516 * all callers expect vlan destruction to
1520 error = if_delmulti(parent,
1521 (struct sockaddr *)&mc->mc_addr);
1524 "Failed to delete multicast address from parent: %d\n",
1527 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1531 vlan_setflags(ifp, 0); /* clear special flags on parent */
1534 * The trunk lock isn't actually required here, but
1535 * vlan_remhash expects it.
1538 vlan_remhash(trunk, ifv);
1539 TRUNK_WUNLOCK(trunk);
1540 ifv->ifv_trunk = NULL;
1543 * Check if we were the last.
1545 if (trunk->refcnt == 0) {
1546 parent->if_vlantrunk = NULL;
1547 trunk_destroy(trunk);
1552 /* Disconnect from parent. */
1553 if (ifv->ifv_pflags)
1554 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1555 ifp->if_mtu = ETHERMTU;
1556 ifp->if_link_state = LINK_STATE_UNKNOWN;
1557 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1560 * Only dispatch an event if vlan was
1561 * attached, otherwise there is nothing
1562 * to cleanup anyway.
1565 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1568 /* Handle a reference counted flag that should be set on the parent as well */
1570 vlan_setflag(struct ifnet *ifp, int flag, int status,
1571 int (*func)(struct ifnet *, int))
1576 VLAN_SXLOCK_ASSERT();
1578 ifv = ifp->if_softc;
1579 status = status ? (ifp->if_flags & flag) : 0;
1580 /* Now "status" contains the flag value or 0 */
1583 * See if recorded parent's status is different from what
1584 * we want it to be. If it is, flip it. We record parent's
1585 * status in ifv_pflags so that we won't clear parent's flag
1586 * we haven't set. In fact, we don't clear or set parent's
1587 * flags directly, but get or release references to them.
1588 * That's why we can be sure that recorded flags still are
1589 * in accord with actual parent's flags.
1591 if (status != (ifv->ifv_pflags & flag)) {
1592 error = (*func)(PARENT(ifv), status);
1595 ifv->ifv_pflags &= ~flag;
1596 ifv->ifv_pflags |= status;
1602 * Handle IFF_* flags that require certain changes on the parent:
1603 * if "status" is true, update parent's flags respective to our if_flags;
1604 * if "status" is false, forcedly clear the flags set on parent.
1607 vlan_setflags(struct ifnet *ifp, int status)
1611 for (i = 0; vlan_pflags[i].flag; i++) {
1612 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1613 status, vlan_pflags[i].func);
1620 /* Inform all vlans that their parent has changed link state */
1622 vlan_link_state(struct ifnet *ifp)
1624 struct ifvlantrunk *trunk;
1628 /* Called from a taskqueue_swi task, so we cannot sleep. */
1630 trunk = ifp->if_vlantrunk;
1631 if (trunk == NULL) {
1637 VLAN_FOREACH(ifv, trunk) {
1638 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1639 if_link_state_change(ifv->ifv_ifp,
1640 trunk->parent->if_link_state);
1642 TRUNK_WUNLOCK(trunk);
1647 vlan_capabilities(struct ifvlan *ifv)
1651 struct ifnet_hw_tsomax hw_tsomax;
1652 int cap = 0, ena = 0, mena;
1655 VLAN_SXLOCK_ASSERT();
1656 TRUNK_WLOCK_ASSERT(TRUNK(ifv));
1660 /* Mask parent interface enabled capabilities disabled by user. */
1661 mena = p->if_capenable & ifv->ifv_capenable;
1664 * If the parent interface can do checksum offloading
1665 * on VLANs, then propagate its hardware-assisted
1666 * checksumming flags. Also assert that checksum
1667 * offloading requires hardware VLAN tagging.
1669 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1670 cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1671 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1672 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1673 ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1674 if (ena & IFCAP_TXCSUM)
1675 hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1676 CSUM_UDP | CSUM_SCTP);
1677 if (ena & IFCAP_TXCSUM_IPV6)
1678 hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1679 CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
1683 * If the parent interface can do TSO on VLANs then
1684 * propagate the hardware-assisted flag. TSO on VLANs
1685 * does not necessarily require hardware VLAN tagging.
1687 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
1688 if_hw_tsomax_common(p, &hw_tsomax);
1689 if_hw_tsomax_update(ifp, &hw_tsomax);
1690 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1691 cap |= p->if_capabilities & IFCAP_TSO;
1692 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1693 ena |= mena & IFCAP_TSO;
1694 if (ena & IFCAP_TSO)
1695 hwa |= p->if_hwassist & CSUM_TSO;
1699 * If the parent interface can do LRO and checksum offloading on
1700 * VLANs, then guess it may do LRO on VLANs. False positive here
1701 * cost nothing, while false negative may lead to some confusions.
1703 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1704 cap |= p->if_capabilities & IFCAP_LRO;
1705 if (p->if_capenable & IFCAP_VLAN_HWCSUM)
1706 ena |= p->if_capenable & IFCAP_LRO;
1709 * If the parent interface can offload TCP connections over VLANs then
1710 * propagate its TOE capability to the VLAN interface.
1712 * All TOE drivers in the tree today can deal with VLANs. If this
1713 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1716 #define IFCAP_VLAN_TOE IFCAP_TOE
1717 if (p->if_capabilities & IFCAP_VLAN_TOE)
1718 cap |= p->if_capabilities & IFCAP_TOE;
1719 if (p->if_capenable & IFCAP_VLAN_TOE) {
1720 TOEDEV(ifp) = TOEDEV(p);
1721 ena |= mena & IFCAP_TOE;
1725 * If the parent interface supports dynamic link state, so does the
1728 cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1729 ena |= (mena & IFCAP_LINKSTATE);
1731 ifp->if_capabilities = cap;
1732 ifp->if_capenable = ena;
1733 ifp->if_hwassist = hwa;
1737 vlan_trunk_capabilities(struct ifnet *ifp)
1739 struct ifvlantrunk *trunk;
1743 trunk = ifp->if_vlantrunk;
1744 if (trunk == NULL) {
1749 VLAN_FOREACH(ifv, trunk) {
1750 vlan_capabilities(ifv);
1752 TRUNK_WUNLOCK(trunk);
1757 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1763 struct ifvlantrunk *trunk;
1768 ifr = (struct ifreq *)data;
1769 ifa = (struct ifaddr *) data;
1770 ifv = ifp->if_softc;
1774 ifp->if_flags |= IFF_UP;
1776 if (ifa->ifa_addr->sa_family == AF_INET)
1777 arp_ifinit(ifp, ifa);
1781 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
1786 if (TRUNK(ifv) != NULL) {
1789 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1791 /* Limit the result to the parent's current config. */
1793 struct ifmediareq *ifmr;
1795 ifmr = (struct ifmediareq *)data;
1796 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1797 ifmr->ifm_count = 1;
1798 error = copyout(&ifmr->ifm_current,
1815 * Set the interface MTU.
1819 if (trunk != NULL) {
1822 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1824 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1827 ifp->if_mtu = ifr->ifr_mtu;
1828 TRUNK_WUNLOCK(trunk);
1837 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
1838 * interface to be delegated to a jail without allowing the
1839 * jail to change what underlying interface/VID it is
1840 * associated with. We are not entirely convinced that this
1841 * is the right way to accomplish that policy goal.
1843 if (ifp->if_vnet != ifp->if_home_vnet) {
1848 error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
1851 if (vlr.vlr_parent[0] == '\0') {
1855 p = ifunit_ref(vlr.vlr_parent);
1860 error = vlan_config(ifv, p, vlr.vlr_tag);
1866 if (ifp->if_vnet != ifp->if_home_vnet) {
1871 bzero(&vlr, sizeof(vlr));
1873 if (TRUNK(ifv) != NULL) {
1874 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1875 sizeof(vlr.vlr_parent));
1876 vlr.vlr_tag = ifv->ifv_vid;
1879 error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
1884 * We should propagate selected flags to the parent,
1885 * e.g., promiscuous mode.
1888 if (TRUNK(ifv) != NULL)
1889 error = vlan_setflags(ifp, 1);
1896 * If we don't have a parent, just remember the membership for
1899 * XXX We need the rmlock here to avoid sleeping while
1900 * holding in6_multi_mtx.
1904 if (trunk != NULL) {
1906 error = vlan_setmulti(ifp);
1907 TRUNK_WUNLOCK(trunk);
1914 if (ifp->if_vnet != ifp->if_home_vnet) {
1919 ifr->ifr_vlan_pcp = ifv->ifv_pcp;
1924 if (ifp->if_vnet != ifp->if_home_vnet) {
1929 error = priv_check(curthread, PRIV_NET_SETVLANPCP);
1932 if (ifr->ifr_vlan_pcp > 7) {
1936 ifv->ifv_pcp = ifr->ifr_vlan_pcp;
1937 vlan_tag_recalculate(ifv);
1938 /* broadcast event about PCP change */
1939 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
1944 ifv->ifv_capenable = ifr->ifr_reqcap;
1946 if (trunk != NULL) {
1948 vlan_capabilities(ifv);
1949 TRUNK_WUNLOCK(trunk);