]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_vlan.c
Make taskqgroup_attach{,_cpu}(9) work across architectures
[FreeBSD/FreeBSD.git] / sys / net / if_vlan.c
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  * Copyright 2012 ADARA Networks, Inc.
4  * Copyright 2017 Dell EMC Isilon
5  *
6  * Portions of this software were developed by Robert N. M. Watson under
7  * contract to ADARA Networks, Inc.
8  *
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
19  * warranty.
20  * 
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
32  * SUCH DAMAGE.
33  */
34
35 /*
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.
43  */
44
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47
48 #include "opt_inet.h"
49 #include "opt_vlan.h"
50 #include "opt_ratelimit.h"
51
52 #include <sys/param.h>
53 #include <sys/eventhandler.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/mbuf.h>
58 #include <sys/module.h>
59 #include <sys/rmlock.h>
60 #include <sys/priv.h>
61 #include <sys/queue.h>
62 #include <sys/socket.h>
63 #include <sys/sockio.h>
64 #include <sys/sysctl.h>
65 #include <sys/systm.h>
66 #include <sys/sx.h>
67 #include <sys/taskqueue.h>
68
69 #include <net/bpf.h>
70 #include <net/ethernet.h>
71 #include <net/if.h>
72 #include <net/if_var.h>
73 #include <net/if_clone.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <net/if_vlan_var.h>
77 #include <net/vnet.h>
78
79 #ifdef INET
80 #include <netinet/in.h>
81 #include <netinet/if_ether.h>
82 #endif
83
84 #define VLAN_DEF_HWIDTH 4
85 #define VLAN_IFFLAGS    (IFF_BROADCAST | IFF_MULTICAST)
86
87 #define UP_AND_RUNNING(ifp) \
88     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
89
90 CK_SLIST_HEAD(ifvlanhead, ifvlan);
91
92 struct ifvlantrunk {
93         struct  ifnet   *parent;        /* parent interface of this trunk */
94         struct  mtx     lock;
95 #ifdef VLAN_ARRAY
96 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
97         struct  ifvlan  *vlans[VLAN_ARRAY_SIZE]; /* static table */
98 #else
99         struct  ifvlanhead *hash;       /* dynamic hash-list table */
100         uint16_t        hmask;
101         uint16_t        hwidth;
102 #endif
103         int             refcnt;
104 };
105
106 /*
107  * This macro provides a facility to iterate over every vlan on a trunk with
108  * the assumption that none will be added/removed during iteration.
109  */
110 #ifdef VLAN_ARRAY
111 #define VLAN_FOREACH(_ifv, _trunk) \
112         size_t _i; \
113         for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
114                 if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
115 #else /* VLAN_ARRAY */
116 #define VLAN_FOREACH(_ifv, _trunk) \
117         struct ifvlan *_next; \
118         size_t _i; \
119         for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
120                 CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
121 #endif /* VLAN_ARRAY */
122
123 /*
124  * This macro provides a facility to iterate over every vlan on a trunk while
125  * also modifying the number of vlans on the trunk. The iteration continues
126  * until some condition is met or there are no more vlans on the trunk.
127  */
128 #ifdef VLAN_ARRAY
129 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
130 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
131         size_t _i; \
132         for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
133                 if (((_ifv) = (_trunk)->vlans[_i]))
134 #else /* VLAN_ARRAY */
135 /*
136  * The hash table case is more complicated. We allow for the hash table to be
137  * modified (i.e. vlans removed) while we are iterating over it. To allow for
138  * this we must restart the iteration every time we "touch" something during
139  * the iteration, since removal will resize the hash table and invalidate our
140  * current position. If acting on the touched element causes the trunk to be
141  * emptied, then iteration also stops.
142  */
143 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
144         size_t _i; \
145         bool _touch = false; \
146         for (_i = 0; \
147             !(_cond) && _i < (1 << (_trunk)->hwidth); \
148             _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
149                 if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
150                     (_touch = true))
151 #endif /* VLAN_ARRAY */
152
153 struct vlan_mc_entry {
154         struct sockaddr_dl              mc_addr;
155         CK_SLIST_ENTRY(vlan_mc_entry)   mc_entries;
156         struct epoch_context            mc_epoch_ctx;
157 };
158
159 struct ifvlan {
160         struct  ifvlantrunk *ifv_trunk;
161         struct  ifnet *ifv_ifp;
162 #define TRUNK(ifv)      ((ifv)->ifv_trunk)
163 #define PARENT(ifv)     ((ifv)->ifv_trunk->parent)
164         void    *ifv_cookie;
165         int     ifv_pflags;     /* special flags we have set on parent */
166         int     ifv_capenable;
167         int     ifv_encaplen;   /* encapsulation length */
168         int     ifv_mtufudge;   /* MTU fudged by this much */
169         int     ifv_mintu;      /* min transmission unit */
170         uint16_t ifv_proto;     /* encapsulation ethertype */
171         uint16_t ifv_tag;       /* tag to apply on packets leaving if */
172         uint16_t ifv_vid;       /* VLAN ID */
173         uint8_t ifv_pcp;        /* Priority Code Point (PCP). */
174         struct task lladdr_task;
175         CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
176 #ifndef VLAN_ARRAY
177         CK_SLIST_ENTRY(ifvlan) ifv_list;
178 #endif
179 };
180
181 /* Special flags we should propagate to parent. */
182 static struct {
183         int flag;
184         int (*func)(struct ifnet *, int);
185 } vlan_pflags[] = {
186         {IFF_PROMISC, ifpromisc},
187         {IFF_ALLMULTI, if_allmulti},
188         {0, NULL}
189 };
190
191 extern int vlan_mtag_pcp;
192
193 static const char vlanname[] = "vlan";
194 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
195
196 static eventhandler_tag ifdetach_tag;
197 static eventhandler_tag iflladdr_tag;
198
199 /*
200  * if_vlan uses two module-level synchronizations primitives to allow concurrent 
201  * modification of vlan interfaces and (mostly) allow for vlans to be destroyed 
202  * while they are being used for tx/rx. To accomplish this in a way that has 
203  * acceptable performance and cooperation with other parts of the network stack
204  * there is a non-sleepable epoch(9) and an sx(9).
205  *
206  * The performance-sensitive paths that warrant using the epoch(9) are
207  * vlan_transmit and vlan_input. Both have to check for the vlan interface's
208  * existence using if_vlantrunk, and being in the network tx/rx paths the use
209  * of an epoch(9) gives a measureable improvement in performance.
210  *
211  * The reason for having an sx(9) is mostly because there are still areas that
212  * must be sleepable and also have safe concurrent access to a vlan interface.
213  * Since the sx(9) exists, it is used by default in most paths unless sleeping
214  * is not permitted, or if it is not clear whether sleeping is permitted.
215  *
216  */
217 #define _VLAN_SX_ID ifv_sx
218
219 static struct sx _VLAN_SX_ID;
220
221 #define VLAN_LOCKING_INIT() \
222         sx_init(&_VLAN_SX_ID, "vlan_sx")
223
224 #define VLAN_LOCKING_DESTROY() \
225         sx_destroy(&_VLAN_SX_ID)
226
227 #define VLAN_SLOCK()                    sx_slock(&_VLAN_SX_ID)
228 #define VLAN_SUNLOCK()                  sx_sunlock(&_VLAN_SX_ID)
229 #define VLAN_XLOCK()                    sx_xlock(&_VLAN_SX_ID)
230 #define VLAN_XUNLOCK()                  sx_xunlock(&_VLAN_SX_ID)
231 #define VLAN_SLOCK_ASSERT()             sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
232 #define VLAN_XLOCK_ASSERT()             sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
233 #define VLAN_SXLOCK_ASSERT()            sx_assert(&_VLAN_SX_ID, SA_LOCKED)
234
235
236 /*
237  * We also have a per-trunk mutex that should be acquired when changing
238  * its state.
239  */
240 #define TRUNK_LOCK_INIT(trunk)          mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
241 #define TRUNK_LOCK_DESTROY(trunk)       mtx_destroy(&(trunk)->lock)
242 #define TRUNK_WLOCK(trunk)              mtx_lock(&(trunk)->lock)
243 #define TRUNK_WUNLOCK(trunk)            mtx_unlock(&(trunk)->lock)
244 #define TRUNK_LOCK_ASSERT(trunk)        MPASS(in_epoch(net_epoch_preempt) || mtx_owned(&(trunk)->lock))
245 #define TRUNK_WLOCK_ASSERT(trunk)       mtx_assert(&(trunk)->lock, MA_OWNED);
246
247 /*
248  * The VLAN_ARRAY substitutes the dynamic hash with a static array
249  * with 4096 entries. In theory this can give a boost in processing,
250  * however in practice it does not. Probably this is because the array
251  * is too big to fit into CPU cache.
252  */
253 #ifndef VLAN_ARRAY
254 static  void vlan_inithash(struct ifvlantrunk *trunk);
255 static  void vlan_freehash(struct ifvlantrunk *trunk);
256 static  int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
257 static  int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
258 static  void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
259 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
260         uint16_t vid);
261 #endif
262 static  void trunk_destroy(struct ifvlantrunk *trunk);
263
264 static  void vlan_init(void *foo);
265 static  void vlan_input(struct ifnet *ifp, struct mbuf *m);
266 static  int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
267 #ifdef RATELIMIT
268 static  int vlan_snd_tag_alloc(struct ifnet *,
269     union if_snd_tag_alloc_params *, struct m_snd_tag **);
270 #endif
271 static  void vlan_qflush(struct ifnet *ifp);
272 static  int vlan_setflag(struct ifnet *ifp, int flag, int status,
273     int (*func)(struct ifnet *, int));
274 static  int vlan_setflags(struct ifnet *ifp, int status);
275 static  int vlan_setmulti(struct ifnet *ifp);
276 static  int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
277 static  void vlan_unconfig(struct ifnet *ifp);
278 static  void vlan_unconfig_locked(struct ifnet *ifp, int departing);
279 static  int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
280 static  void vlan_link_state(struct ifnet *ifp);
281 static  void vlan_capabilities(struct ifvlan *ifv);
282 static  void vlan_trunk_capabilities(struct ifnet *ifp);
283
284 static  struct ifnet *vlan_clone_match_ethervid(const char *, int *);
285 static  int vlan_clone_match(struct if_clone *, const char *);
286 static  int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
287 static  int vlan_clone_destroy(struct if_clone *, struct ifnet *);
288
289 static  void vlan_ifdetach(void *arg, struct ifnet *ifp);
290 static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
291
292 static  void vlan_lladdr_fn(void *arg, int pending);
293
294 static struct if_clone *vlan_cloner;
295
296 #ifdef VIMAGE
297 VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
298 #define V_vlan_cloner   VNET(vlan_cloner)
299 #endif
300
301 static void
302 vlan_mc_free(struct epoch_context *ctx)
303 {
304         struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
305         free(mc, M_VLAN);
306 }
307
308 #ifndef VLAN_ARRAY
309 #define HASH(n, m)      ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
310
311 static void
312 vlan_inithash(struct ifvlantrunk *trunk)
313 {
314         int i, n;
315         
316         /*
317          * The trunk must not be locked here since we call malloc(M_WAITOK).
318          * It is OK in case this function is called before the trunk struct
319          * gets hooked up and becomes visible from other threads.
320          */
321
322         KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
323             ("%s: hash already initialized", __func__));
324
325         trunk->hwidth = VLAN_DEF_HWIDTH;
326         n = 1 << trunk->hwidth;
327         trunk->hmask = n - 1;
328         trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
329         for (i = 0; i < n; i++)
330                 CK_SLIST_INIT(&trunk->hash[i]);
331 }
332
333 static void
334 vlan_freehash(struct ifvlantrunk *trunk)
335 {
336 #ifdef INVARIANTS
337         int i;
338
339         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
340         for (i = 0; i < (1 << trunk->hwidth); i++)
341                 KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
342                     ("%s: hash table not empty", __func__));
343 #endif
344         free(trunk->hash, M_VLAN);
345         trunk->hash = NULL;
346         trunk->hwidth = trunk->hmask = 0;
347 }
348
349 static int
350 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
351 {
352         int i, b;
353         struct ifvlan *ifv2;
354
355         VLAN_XLOCK_ASSERT();
356         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
357
358         b = 1 << trunk->hwidth;
359         i = HASH(ifv->ifv_vid, trunk->hmask);
360         CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
361                 if (ifv->ifv_vid == ifv2->ifv_vid)
362                         return (EEXIST);
363
364         /*
365          * Grow the hash when the number of vlans exceeds half of the number of
366          * hash buckets squared. This will make the average linked-list length
367          * buckets/2.
368          */
369         if (trunk->refcnt > (b * b) / 2) {
370                 vlan_growhash(trunk, 1);
371                 i = HASH(ifv->ifv_vid, trunk->hmask);
372         }
373         CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
374         trunk->refcnt++;
375
376         return (0);
377 }
378
379 static int
380 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
381 {
382         int i, b;
383         struct ifvlan *ifv2;
384
385         VLAN_XLOCK_ASSERT();
386         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
387         
388         b = 1 << trunk->hwidth;
389         i = HASH(ifv->ifv_vid, trunk->hmask);
390         CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
391                 if (ifv2 == ifv) {
392                         trunk->refcnt--;
393                         CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
394                         if (trunk->refcnt < (b * b) / 2)
395                                 vlan_growhash(trunk, -1);
396                         return (0);
397                 }
398
399         panic("%s: vlan not found\n", __func__);
400         return (ENOENT); /*NOTREACHED*/
401 }
402
403 /*
404  * Grow the hash larger or smaller if memory permits.
405  */
406 static void
407 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
408 {
409         struct ifvlan *ifv;
410         struct ifvlanhead *hash2;
411         int hwidth2, i, j, n, n2;
412
413         VLAN_XLOCK_ASSERT();
414         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
415
416         if (howmuch == 0) {
417                 /* Harmless yet obvious coding error */
418                 printf("%s: howmuch is 0\n", __func__);
419                 return;
420         }
421
422         hwidth2 = trunk->hwidth + howmuch;
423         n = 1 << trunk->hwidth;
424         n2 = 1 << hwidth2;
425         /* Do not shrink the table below the default */
426         if (hwidth2 < VLAN_DEF_HWIDTH)
427                 return;
428
429         hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
430         if (hash2 == NULL) {
431                 printf("%s: out of memory -- hash size not changed\n",
432                     __func__);
433                 return;         /* We can live with the old hash table */
434         }
435         for (j = 0; j < n2; j++)
436                 CK_SLIST_INIT(&hash2[j]);
437         for (i = 0; i < n; i++)
438                 while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
439                         CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
440                         j = HASH(ifv->ifv_vid, n2 - 1);
441                         CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
442                 }
443         NET_EPOCH_WAIT();
444         free(trunk->hash, M_VLAN);
445         trunk->hash = hash2;
446         trunk->hwidth = hwidth2;
447         trunk->hmask = n2 - 1;
448
449         if (bootverbose)
450                 if_printf(trunk->parent,
451                     "VLAN hash table resized from %d to %d buckets\n", n, n2);
452 }
453
454 static __inline struct ifvlan *
455 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
456 {
457         struct ifvlan *ifv;
458
459         NET_EPOCH_ASSERT();
460
461         CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
462                 if (ifv->ifv_vid == vid)
463                         return (ifv);
464         return (NULL);
465 }
466
467 #if 0
468 /* Debugging code to view the hashtables. */
469 static void
470 vlan_dumphash(struct ifvlantrunk *trunk)
471 {
472         int i;
473         struct ifvlan *ifv;
474
475         for (i = 0; i < (1 << trunk->hwidth); i++) {
476                 printf("%d: ", i);
477                 CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
478                         printf("%s ", ifv->ifv_ifp->if_xname);
479                 printf("\n");
480         }
481 }
482 #endif /* 0 */
483 #else
484
485 static __inline struct ifvlan *
486 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
487 {
488
489         return trunk->vlans[vid];
490 }
491
492 static __inline int
493 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
494 {
495
496         if (trunk->vlans[ifv->ifv_vid] != NULL)
497                 return EEXIST;
498         trunk->vlans[ifv->ifv_vid] = ifv;
499         trunk->refcnt++;
500
501         return (0);
502 }
503
504 static __inline int
505 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
506 {
507
508         trunk->vlans[ifv->ifv_vid] = NULL;
509         trunk->refcnt--;
510
511         return (0);
512 }
513
514 static __inline void
515 vlan_freehash(struct ifvlantrunk *trunk)
516 {
517 }
518
519 static __inline void
520 vlan_inithash(struct ifvlantrunk *trunk)
521 {
522 }
523
524 #endif /* !VLAN_ARRAY */
525
526 static void
527 trunk_destroy(struct ifvlantrunk *trunk)
528 {
529         VLAN_XLOCK_ASSERT();
530
531         vlan_freehash(trunk);
532         trunk->parent->if_vlantrunk = NULL;
533         TRUNK_LOCK_DESTROY(trunk);
534         if_rele(trunk->parent);
535         free(trunk, M_VLAN);
536 }
537
538 /*
539  * Program our multicast filter. What we're actually doing is
540  * programming the multicast filter of the parent. This has the
541  * side effect of causing the parent interface to receive multicast
542  * traffic that it doesn't really want, which ends up being discarded
543  * later by the upper protocol layers. Unfortunately, there's no way
544  * to avoid this: there really is only one physical interface.
545  */
546 static int
547 vlan_setmulti(struct ifnet *ifp)
548 {
549         struct ifnet            *ifp_p;
550         struct ifmultiaddr      *ifma;
551         struct ifvlan           *sc;
552         struct vlan_mc_entry    *mc;
553         int                     error;
554
555         VLAN_XLOCK_ASSERT();
556
557         /* Find the parent. */
558         sc = ifp->if_softc;
559         ifp_p = PARENT(sc);
560
561         CURVNET_SET_QUIET(ifp_p->if_vnet);
562
563         /* First, remove any existing filter entries. */
564         while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
565                 CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
566                 (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
567                 epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
568         }
569
570         /* Now program new ones. */
571         IF_ADDR_WLOCK(ifp);
572         CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
573                 if (ifma->ifma_addr->sa_family != AF_LINK)
574                         continue;
575                 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
576                 if (mc == NULL) {
577                         IF_ADDR_WUNLOCK(ifp);
578                         return (ENOMEM);
579                 }
580                 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
581                 mc->mc_addr.sdl_index = ifp_p->if_index;
582                 CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
583         }
584         IF_ADDR_WUNLOCK(ifp);
585         CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
586                 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
587                     NULL);
588                 if (error)
589                         return (error);
590         }
591
592         CURVNET_RESTORE();
593         return (0);
594 }
595
596 /*
597  * A handler for parent interface link layer address changes.
598  * If the parent interface link layer address is changed we
599  * should also change it on all children vlans.
600  */
601 static void
602 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
603 {
604         struct epoch_tracker et;
605         struct ifvlan *ifv;
606         struct ifnet *ifv_ifp;
607         struct ifvlantrunk *trunk;
608         struct sockaddr_dl *sdl;
609
610         /* Need the epoch since this is run on taskqueue_swi. */
611         NET_EPOCH_ENTER(et);
612         trunk = ifp->if_vlantrunk;
613         if (trunk == NULL) {
614                 NET_EPOCH_EXIT(et);
615                 return;
616         }
617
618         /*
619          * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
620          * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
621          * ioctl calls on the parent garbling the lladdr of the child vlan.
622          */
623         TRUNK_WLOCK(trunk);
624         VLAN_FOREACH(ifv, trunk) {
625                 /*
626                  * Copy new new lladdr into the ifv_ifp, enqueue a task
627                  * to actually call if_setlladdr. if_setlladdr needs to
628                  * be deferred to a taskqueue because it will call into
629                  * the if_vlan ioctl path and try to acquire the global
630                  * lock.
631                  */
632                 ifv_ifp = ifv->ifv_ifp;
633                 bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
634                     ifp->if_addrlen);
635                 sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
636                 sdl->sdl_alen = ifp->if_addrlen;
637                 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
638         }
639         TRUNK_WUNLOCK(trunk);
640         NET_EPOCH_EXIT(et);
641 }
642
643 /*
644  * A handler for network interface departure events.
645  * Track departure of trunks here so that we don't access invalid
646  * pointers or whatever if a trunk is ripped from under us, e.g.,
647  * by ejecting its hot-plug card.  However, if an ifnet is simply
648  * being renamed, then there's no need to tear down the state.
649  */
650 static void
651 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
652 {
653         struct ifvlan *ifv;
654         struct ifvlantrunk *trunk;
655
656         /* If the ifnet is just being renamed, don't do anything. */
657         if (ifp->if_flags & IFF_RENAMING)
658                 return;
659         VLAN_XLOCK();
660         trunk = ifp->if_vlantrunk;
661         if (trunk == NULL) {
662                 VLAN_XUNLOCK();
663                 return;
664         }
665
666         /*
667          * OK, it's a trunk.  Loop over and detach all vlan's on it.
668          * Check trunk pointer after each vlan_unconfig() as it will
669          * free it and set to NULL after the last vlan was detached.
670          */
671         VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
672             ifp->if_vlantrunk == NULL)
673                 vlan_unconfig_locked(ifv->ifv_ifp, 1);
674
675         /* Trunk should have been destroyed in vlan_unconfig(). */
676         KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
677         VLAN_XUNLOCK();
678 }
679
680 /*
681  * Return the trunk device for a virtual interface.
682  */
683 static struct ifnet  *
684 vlan_trunkdev(struct ifnet *ifp)
685 {
686         struct epoch_tracker et;
687         struct ifvlan *ifv;
688
689         if (ifp->if_type != IFT_L2VLAN)
690                 return (NULL);
691
692         NET_EPOCH_ENTER(et);
693         ifv = ifp->if_softc;
694         ifp = NULL;
695         if (ifv->ifv_trunk)
696                 ifp = PARENT(ifv);
697         NET_EPOCH_EXIT(et);
698         return (ifp);
699 }
700
701 /*
702  * Return the 12-bit VLAN VID for this interface, for use by external
703  * components such as Infiniband.
704  *
705  * XXXRW: Note that the function name here is historical; it should be named
706  * vlan_vid().
707  */
708 static int
709 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
710 {
711         struct ifvlan *ifv;
712
713         if (ifp->if_type != IFT_L2VLAN)
714                 return (EINVAL);
715         ifv = ifp->if_softc;
716         *vidp = ifv->ifv_vid;
717         return (0);
718 }
719
720 static int
721 vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
722 {
723         struct ifvlan *ifv;
724
725         if (ifp->if_type != IFT_L2VLAN)
726                 return (EINVAL);
727         ifv = ifp->if_softc;
728         *pcpp = ifv->ifv_pcp;
729         return (0);
730 }
731
732 /*
733  * Return a driver specific cookie for this interface.  Synchronization
734  * with setcookie must be provided by the driver. 
735  */
736 static void *
737 vlan_cookie(struct ifnet *ifp)
738 {
739         struct ifvlan *ifv;
740
741         if (ifp->if_type != IFT_L2VLAN)
742                 return (NULL);
743         ifv = ifp->if_softc;
744         return (ifv->ifv_cookie);
745 }
746
747 /*
748  * Store a cookie in our softc that drivers can use to store driver
749  * private per-instance data in.
750  */
751 static int
752 vlan_setcookie(struct ifnet *ifp, void *cookie)
753 {
754         struct ifvlan *ifv;
755
756         if (ifp->if_type != IFT_L2VLAN)
757                 return (EINVAL);
758         ifv = ifp->if_softc;
759         ifv->ifv_cookie = cookie;
760         return (0);
761 }
762
763 /*
764  * Return the vlan device present at the specific VID.
765  */
766 static struct ifnet *
767 vlan_devat(struct ifnet *ifp, uint16_t vid)
768 {
769         struct epoch_tracker et;
770         struct ifvlantrunk *trunk;
771         struct ifvlan *ifv;
772
773         NET_EPOCH_ENTER(et);
774         trunk = ifp->if_vlantrunk;
775         if (trunk == NULL) {
776                 NET_EPOCH_EXIT(et);
777                 return (NULL);
778         }
779         ifp = NULL;
780         ifv = vlan_gethash(trunk, vid);
781         if (ifv)
782                 ifp = ifv->ifv_ifp;
783         NET_EPOCH_EXIT(et);
784         return (ifp);
785 }
786
787 /*
788  * Recalculate the cached VLAN tag exposed via the MIB.
789  */
790 static void
791 vlan_tag_recalculate(struct ifvlan *ifv)
792 {
793
794        ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
795 }
796
797 /*
798  * VLAN support can be loaded as a module.  The only place in the
799  * system that's intimately aware of this is ether_input.  We hook
800  * into this code through vlan_input_p which is defined there and
801  * set here.  No one else in the system should be aware of this so
802  * we use an explicit reference here.
803  */
804 extern  void (*vlan_input_p)(struct ifnet *, struct mbuf *);
805
806 /* For if_link_state_change() eyes only... */
807 extern  void (*vlan_link_state_p)(struct ifnet *);
808
809 static int
810 vlan_modevent(module_t mod, int type, void *data)
811 {
812
813         switch (type) {
814         case MOD_LOAD:
815                 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
816                     vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
817                 if (ifdetach_tag == NULL)
818                         return (ENOMEM);
819                 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
820                     vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
821                 if (iflladdr_tag == NULL)
822                         return (ENOMEM);
823                 VLAN_LOCKING_INIT();
824                 vlan_input_p = vlan_input;
825                 vlan_link_state_p = vlan_link_state;
826                 vlan_trunk_cap_p = vlan_trunk_capabilities;
827                 vlan_trunkdev_p = vlan_trunkdev;
828                 vlan_cookie_p = vlan_cookie;
829                 vlan_setcookie_p = vlan_setcookie;
830                 vlan_tag_p = vlan_tag;
831                 vlan_pcp_p = vlan_pcp;
832                 vlan_devat_p = vlan_devat;
833 #ifndef VIMAGE
834                 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
835                     vlan_clone_create, vlan_clone_destroy);
836 #endif
837                 if (bootverbose)
838                         printf("vlan: initialized, using "
839 #ifdef VLAN_ARRAY
840                                "full-size arrays"
841 #else
842                                "hash tables with chaining"
843 #endif
844                         
845                                "\n");
846                 break;
847         case MOD_UNLOAD:
848 #ifndef VIMAGE
849                 if_clone_detach(vlan_cloner);
850 #endif
851                 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
852                 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
853                 vlan_input_p = NULL;
854                 vlan_link_state_p = NULL;
855                 vlan_trunk_cap_p = NULL;
856                 vlan_trunkdev_p = NULL;
857                 vlan_tag_p = NULL;
858                 vlan_cookie_p = NULL;
859                 vlan_setcookie_p = NULL;
860                 vlan_devat_p = NULL;
861                 VLAN_LOCKING_DESTROY();
862                 if (bootverbose)
863                         printf("vlan: unloaded\n");
864                 break;
865         default:
866                 return (EOPNOTSUPP);
867         }
868         return (0);
869 }
870
871 static moduledata_t vlan_mod = {
872         "if_vlan",
873         vlan_modevent,
874         0
875 };
876
877 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
878 MODULE_VERSION(if_vlan, 3);
879
880 #ifdef VIMAGE
881 static void
882 vnet_vlan_init(const void *unused __unused)
883 {
884
885         vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
886                     vlan_clone_create, vlan_clone_destroy);
887         V_vlan_cloner = vlan_cloner;
888 }
889 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
890     vnet_vlan_init, NULL);
891
892 static void
893 vnet_vlan_uninit(const void *unused __unused)
894 {
895
896         if_clone_detach(V_vlan_cloner);
897 }
898 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
899     vnet_vlan_uninit, NULL);
900 #endif
901
902 /*
903  * Check for <etherif>.<vlan> style interface names.
904  */
905 static struct ifnet *
906 vlan_clone_match_ethervid(const char *name, int *vidp)
907 {
908         char ifname[IFNAMSIZ];
909         char *cp;
910         struct ifnet *ifp;
911         int vid;
912
913         strlcpy(ifname, name, IFNAMSIZ);
914         if ((cp = strchr(ifname, '.')) == NULL)
915                 return (NULL);
916         *cp = '\0';
917         if ((ifp = ifunit_ref(ifname)) == NULL)
918                 return (NULL);
919         /* Parse VID. */
920         if (*++cp == '\0') {
921                 if_rele(ifp);
922                 return (NULL);
923         }
924         vid = 0;
925         for(; *cp >= '0' && *cp <= '9'; cp++)
926                 vid = (vid * 10) + (*cp - '0');
927         if (*cp != '\0') {
928                 if_rele(ifp);
929                 return (NULL);
930         }
931         if (vidp != NULL)
932                 *vidp = vid;
933
934         return (ifp);
935 }
936
937 static int
938 vlan_clone_match(struct if_clone *ifc, const char *name)
939 {
940         const char *cp;
941
942         if (vlan_clone_match_ethervid(name, NULL) != NULL)
943                 return (1);
944
945         if (strncmp(vlanname, name, strlen(vlanname)) != 0)
946                 return (0);
947         for (cp = name + 4; *cp != '\0'; cp++) {
948                 if (*cp < '0' || *cp > '9')
949                         return (0);
950         }
951
952         return (1);
953 }
954
955 static int
956 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
957 {
958         char *dp;
959         int wildcard;
960         int unit;
961         int error;
962         int vid;
963         struct ifvlan *ifv;
964         struct ifnet *ifp;
965         struct ifnet *p;
966         struct ifaddr *ifa;
967         struct sockaddr_dl *sdl;
968         struct vlanreq vlr;
969         static const u_char eaddr[ETHER_ADDR_LEN];      /* 00:00:00:00:00:00 */
970
971         /*
972          * There are 3 (ugh) ways to specify the cloned device:
973          * o pass a parameter block with the clone request.
974          * o specify parameters in the text of the clone device name
975          * o specify no parameters and get an unattached device that
976          *   must be configured separately.
977          * The first technique is preferred; the latter two are
978          * supported for backwards compatibility.
979          *
980          * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
981          * called for.
982          */
983         if (params) {
984                 error = copyin(params, &vlr, sizeof(vlr));
985                 if (error)
986                         return error;
987                 p = ifunit_ref(vlr.vlr_parent);
988                 if (p == NULL)
989                         return (ENXIO);
990                 error = ifc_name2unit(name, &unit);
991                 if (error != 0) {
992                         if_rele(p);
993                         return (error);
994                 }
995                 vid = vlr.vlr_tag;
996                 wildcard = (unit < 0);
997         } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
998                 unit = -1;
999                 wildcard = 0;
1000         } else {
1001                 p = NULL;
1002                 error = ifc_name2unit(name, &unit);
1003                 if (error != 0)
1004                         return (error);
1005
1006                 wildcard = (unit < 0);
1007         }
1008
1009         error = ifc_alloc_unit(ifc, &unit);
1010         if (error != 0) {
1011                 if (p != NULL)
1012                         if_rele(p);
1013                 return (error);
1014         }
1015
1016         /* In the wildcard case, we need to update the name. */
1017         if (wildcard) {
1018                 for (dp = name; *dp != '\0'; dp++);
1019                 if (snprintf(dp, len - (dp-name), "%d", unit) >
1020                     len - (dp-name) - 1) {
1021                         panic("%s: interface name too long", __func__);
1022                 }
1023         }
1024
1025         ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1026         ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1027         if (ifp == NULL) {
1028                 ifc_free_unit(ifc, unit);
1029                 free(ifv, M_VLAN);
1030                 if (p != NULL)
1031                         if_rele(p);
1032                 return (ENOSPC);
1033         }
1034         CK_SLIST_INIT(&ifv->vlan_mc_listhead);
1035         ifp->if_softc = ifv;
1036         /*
1037          * Set the name manually rather than using if_initname because
1038          * we don't conform to the default naming convention for interfaces.
1039          */
1040         strlcpy(ifp->if_xname, name, IFNAMSIZ);
1041         ifp->if_dname = vlanname;
1042         ifp->if_dunit = unit;
1043
1044         ifp->if_init = vlan_init;
1045         ifp->if_transmit = vlan_transmit;
1046         ifp->if_qflush = vlan_qflush;
1047         ifp->if_ioctl = vlan_ioctl;
1048 #ifdef RATELIMIT
1049         ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1050 #endif
1051         ifp->if_flags = VLAN_IFFLAGS;
1052         ether_ifattach(ifp, eaddr);
1053         /* Now undo some of the damage... */
1054         ifp->if_baudrate = 0;
1055         ifp->if_type = IFT_L2VLAN;
1056         ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1057         ifa = ifp->if_addr;
1058         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1059         sdl->sdl_type = IFT_L2VLAN;
1060
1061         if (p != NULL) {
1062                 error = vlan_config(ifv, p, vid);
1063                 if_rele(p);
1064                 if (error != 0) {
1065                         /*
1066                          * Since we've partially failed, we need to back
1067                          * out all the way, otherwise userland could get
1068                          * confused.  Thus, we destroy the interface.
1069                          */
1070                         ether_ifdetach(ifp);
1071                         vlan_unconfig(ifp);
1072                         if_free(ifp);
1073                         ifc_free_unit(ifc, unit);
1074                         free(ifv, M_VLAN);
1075
1076                         return (error);
1077                 }
1078         }
1079
1080         return (0);
1081 }
1082
1083 static int
1084 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
1085 {
1086         struct ifvlan *ifv = ifp->if_softc;
1087         int unit = ifp->if_dunit;
1088
1089         ether_ifdetach(ifp);    /* first, remove it from system-wide lists */
1090         vlan_unconfig(ifp);     /* now it can be unconfigured and freed */
1091         /*
1092          * We should have the only reference to the ifv now, so we can now
1093          * drain any remaining lladdr task before freeing the ifnet and the
1094          * ifvlan.
1095          */
1096         taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1097         NET_EPOCH_WAIT();
1098         if_free(ifp);
1099         free(ifv, M_VLAN);
1100         ifc_free_unit(ifc, unit);
1101
1102         return (0);
1103 }
1104
1105 /*
1106  * The ifp->if_init entry point for vlan(4) is a no-op.
1107  */
1108 static void
1109 vlan_init(void *foo __unused)
1110 {
1111 }
1112
1113 /*
1114  * The if_transmit method for vlan(4) interface.
1115  */
1116 static int
1117 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1118 {
1119         struct epoch_tracker et;
1120         struct ifvlan *ifv;
1121         struct ifnet *p;
1122         int error, len, mcast;
1123
1124         NET_EPOCH_ENTER(et);
1125         ifv = ifp->if_softc;
1126         if (TRUNK(ifv) == NULL) {
1127                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1128                 NET_EPOCH_EXIT(et);
1129                 m_freem(m);
1130                 return (ENETDOWN);
1131         }
1132         p = PARENT(ifv);
1133         len = m->m_pkthdr.len;
1134         mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1135
1136         BPF_MTAP(ifp, m);
1137
1138         /*
1139          * Do not run parent's if_transmit() if the parent is not up,
1140          * or parent's driver will cause a system crash.
1141          */
1142         if (!UP_AND_RUNNING(p)) {
1143                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1144                 NET_EPOCH_EXIT(et);
1145                 m_freem(m);
1146                 return (ENETDOWN);
1147         }
1148
1149         if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1150                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1151                 NET_EPOCH_EXIT(et);
1152                 return (0);
1153         }
1154
1155         /*
1156          * Send it, precisely as ether_output() would have.
1157          */
1158         error = (p->if_transmit)(p, m);
1159         if (error == 0) {
1160                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1161                 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1162                 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1163         } else
1164                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1165         NET_EPOCH_EXIT(et);
1166         return (error);
1167 }
1168
1169 /*
1170  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1171  */
1172 static void
1173 vlan_qflush(struct ifnet *ifp __unused)
1174 {
1175 }
1176
1177 static void
1178 vlan_input(struct ifnet *ifp, struct mbuf *m)
1179 {
1180         struct epoch_tracker et;
1181         struct ifvlantrunk *trunk;
1182         struct ifvlan *ifv;
1183         struct m_tag *mtag;
1184         uint16_t vid, tag;
1185
1186         NET_EPOCH_ENTER(et);
1187         trunk = ifp->if_vlantrunk;
1188         if (trunk == NULL) {
1189                 NET_EPOCH_EXIT(et);
1190                 m_freem(m);
1191                 return;
1192         }
1193
1194         if (m->m_flags & M_VLANTAG) {
1195                 /*
1196                  * Packet is tagged, but m contains a normal
1197                  * Ethernet frame; the tag is stored out-of-band.
1198                  */
1199                 tag = m->m_pkthdr.ether_vtag;
1200                 m->m_flags &= ~M_VLANTAG;
1201         } else {
1202                 struct ether_vlan_header *evl;
1203
1204                 /*
1205                  * Packet is tagged in-band as specified by 802.1q.
1206                  */
1207                 switch (ifp->if_type) {
1208                 case IFT_ETHER:
1209                         if (m->m_len < sizeof(*evl) &&
1210                             (m = m_pullup(m, sizeof(*evl))) == NULL) {
1211                                 if_printf(ifp, "cannot pullup VLAN header\n");
1212                                 NET_EPOCH_EXIT(et);
1213                                 return;
1214                         }
1215                         evl = mtod(m, struct ether_vlan_header *);
1216                         tag = ntohs(evl->evl_tag);
1217
1218                         /*
1219                          * Remove the 802.1q header by copying the Ethernet
1220                          * addresses over it and adjusting the beginning of
1221                          * the data in the mbuf.  The encapsulated Ethernet
1222                          * type field is already in place.
1223                          */
1224                         bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1225                               ETHER_HDR_LEN - ETHER_TYPE_LEN);
1226                         m_adj(m, ETHER_VLAN_ENCAP_LEN);
1227                         break;
1228
1229                 default:
1230 #ifdef INVARIANTS
1231                         panic("%s: %s has unsupported if_type %u",
1232                               __func__, ifp->if_xname, ifp->if_type);
1233 #endif
1234                         if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1235                         NET_EPOCH_EXIT(et);
1236                         m_freem(m);
1237                         return;
1238                 }
1239         }
1240
1241         vid = EVL_VLANOFTAG(tag);
1242
1243         ifv = vlan_gethash(trunk, vid);
1244         if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1245                 NET_EPOCH_EXIT(et);
1246                 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1247                 m_freem(m);
1248                 return;
1249         }
1250
1251         if (vlan_mtag_pcp) {
1252                 /*
1253                  * While uncommon, it is possible that we will find a 802.1q
1254                  * packet encapsulated inside another packet that also had an
1255                  * 802.1q header.  For example, ethernet tunneled over IPSEC
1256                  * arriving over ethernet.  In that case, we replace the
1257                  * existing 802.1q PCP m_tag value.
1258                  */
1259                 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1260                 if (mtag == NULL) {
1261                         mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1262                             sizeof(uint8_t), M_NOWAIT);
1263                         if (mtag == NULL) {
1264                                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1265                                 NET_EPOCH_EXIT(et);
1266                                 m_freem(m);
1267                                 return;
1268                         }
1269                         m_tag_prepend(m, mtag);
1270                 }
1271                 *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1272         }
1273
1274         m->m_pkthdr.rcvif = ifv->ifv_ifp;
1275         if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1276         NET_EPOCH_EXIT(et);
1277
1278         /* Pass it back through the parent's input routine. */
1279         (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1280 }
1281
1282 static void
1283 vlan_lladdr_fn(void *arg, int pending __unused)
1284 {
1285         struct ifvlan *ifv;
1286         struct ifnet *ifp;
1287
1288         ifv = (struct ifvlan *)arg;
1289         ifp = ifv->ifv_ifp;
1290
1291         CURVNET_SET(ifp->if_vnet);
1292
1293         /* The ifv_ifp already has the lladdr copied in. */
1294         if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1295
1296         CURVNET_RESTORE();
1297 }
1298
1299 static int
1300 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
1301 {
1302         struct epoch_tracker et;
1303         struct ifvlantrunk *trunk;
1304         struct ifnet *ifp;
1305         int error = 0;
1306
1307         /*
1308          * We can handle non-ethernet hardware types as long as
1309          * they handle the tagging and headers themselves.
1310          */
1311         if (p->if_type != IFT_ETHER &&
1312             (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1313                 return (EPROTONOSUPPORT);
1314         if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1315                 return (EPROTONOSUPPORT);
1316         /*
1317          * Don't let the caller set up a VLAN VID with
1318          * anything except VLID bits.
1319          * VID numbers 0x0 and 0xFFF are reserved.
1320          */
1321         if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1322                 return (EINVAL);
1323         if (ifv->ifv_trunk)
1324                 return (EBUSY);
1325
1326         VLAN_XLOCK();
1327         if (p->if_vlantrunk == NULL) {
1328                 trunk = malloc(sizeof(struct ifvlantrunk),
1329                     M_VLAN, M_WAITOK | M_ZERO);
1330                 vlan_inithash(trunk);
1331                 TRUNK_LOCK_INIT(trunk);
1332                 TRUNK_WLOCK(trunk);
1333                 p->if_vlantrunk = trunk;
1334                 trunk->parent = p;
1335                 if_ref(trunk->parent);
1336                 TRUNK_WUNLOCK(trunk);
1337         } else {
1338                 trunk = p->if_vlantrunk;
1339         }
1340
1341         ifv->ifv_vid = vid;     /* must set this before vlan_inshash() */
1342         ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
1343         vlan_tag_recalculate(ifv);
1344         error = vlan_inshash(trunk, ifv);
1345         if (error)
1346                 goto done;
1347         ifv->ifv_proto = ETHERTYPE_VLAN;
1348         ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1349         ifv->ifv_mintu = ETHERMIN;
1350         ifv->ifv_pflags = 0;
1351         ifv->ifv_capenable = -1;
1352
1353         /*
1354          * If the parent supports the VLAN_MTU capability,
1355          * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1356          * use it.
1357          */
1358         if (p->if_capenable & IFCAP_VLAN_MTU) {
1359                 /*
1360                  * No need to fudge the MTU since the parent can
1361                  * handle extended frames.
1362                  */
1363                 ifv->ifv_mtufudge = 0;
1364         } else {
1365                 /*
1366                  * Fudge the MTU by the encapsulation size.  This
1367                  * makes us incompatible with strictly compliant
1368                  * 802.1Q implementations, but allows us to use
1369                  * the feature with other NetBSD implementations,
1370                  * which might still be useful.
1371                  */
1372                 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1373         }
1374
1375         ifv->ifv_trunk = trunk;
1376         ifp = ifv->ifv_ifp;
1377         /*
1378          * Initialize fields from our parent.  This duplicates some
1379          * work with ether_ifattach() but allows for non-ethernet
1380          * interfaces to also work.
1381          */
1382         ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1383         ifp->if_baudrate = p->if_baudrate;
1384         ifp->if_output = p->if_output;
1385         ifp->if_input = p->if_input;
1386         ifp->if_resolvemulti = p->if_resolvemulti;
1387         ifp->if_addrlen = p->if_addrlen;
1388         ifp->if_broadcastaddr = p->if_broadcastaddr;
1389         ifp->if_pcp = ifv->ifv_pcp;
1390
1391         /*
1392          * Copy only a selected subset of flags from the parent.
1393          * Other flags are none of our business.
1394          */
1395 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1396         ifp->if_flags &= ~VLAN_COPY_FLAGS;
1397         ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1398 #undef VLAN_COPY_FLAGS
1399
1400         ifp->if_link_state = p->if_link_state;
1401
1402         NET_EPOCH_ENTER(et);
1403         vlan_capabilities(ifv);
1404         NET_EPOCH_EXIT(et);
1405
1406         /*
1407          * Set up our interface address to reflect the underlying
1408          * physical interface's.
1409          */
1410         bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1411         ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1412             p->if_addrlen;
1413
1414         TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1415
1416         /* We are ready for operation now. */
1417         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1418
1419         /* Update flags on the parent, if necessary. */
1420         vlan_setflags(ifp, 1);
1421
1422         /*
1423          * Configure multicast addresses that may already be
1424          * joined on the vlan device.
1425          */
1426         (void)vlan_setmulti(ifp);
1427
1428 done:
1429         if (error == 0)
1430                 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1431         VLAN_XUNLOCK();
1432
1433         return (error);
1434 }
1435
1436 static void
1437 vlan_unconfig(struct ifnet *ifp)
1438 {
1439
1440         VLAN_XLOCK();
1441         vlan_unconfig_locked(ifp, 0);
1442         VLAN_XUNLOCK();
1443 }
1444
1445 static void
1446 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1447 {
1448         struct ifvlantrunk *trunk;
1449         struct vlan_mc_entry *mc;
1450         struct ifvlan *ifv;
1451         struct ifnet  *parent;
1452         int error;
1453
1454         VLAN_XLOCK_ASSERT();
1455
1456         ifv = ifp->if_softc;
1457         trunk = ifv->ifv_trunk;
1458         parent = NULL;
1459
1460         if (trunk != NULL) {
1461                 parent = trunk->parent;
1462
1463                 /*
1464                  * Since the interface is being unconfigured, we need to
1465                  * empty the list of multicast groups that we may have joined
1466                  * while we were alive from the parent's list.
1467                  */
1468                 while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1469                         /*
1470                          * If the parent interface is being detached,
1471                          * all its multicast addresses have already
1472                          * been removed.  Warn about errors if
1473                          * if_delmulti() does fail, but don't abort as
1474                          * all callers expect vlan destruction to
1475                          * succeed.
1476                          */
1477                         if (!departing) {
1478                                 error = if_delmulti(parent,
1479                                     (struct sockaddr *)&mc->mc_addr);
1480                                 if (error)
1481                                         if_printf(ifp,
1482                     "Failed to delete multicast address from parent: %d\n",
1483                                             error);
1484                         }
1485                         CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1486                         epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
1487                 }
1488
1489                 vlan_setflags(ifp, 0); /* clear special flags on parent */
1490
1491                 vlan_remhash(trunk, ifv);
1492                 ifv->ifv_trunk = NULL;
1493
1494                 /*
1495                  * Check if we were the last.
1496                  */
1497                 if (trunk->refcnt == 0) {
1498                         parent->if_vlantrunk = NULL;
1499                         NET_EPOCH_WAIT();
1500                         trunk_destroy(trunk);
1501                 }
1502         }
1503
1504         /* Disconnect from parent. */
1505         if (ifv->ifv_pflags)
1506                 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1507         ifp->if_mtu = ETHERMTU;
1508         ifp->if_link_state = LINK_STATE_UNKNOWN;
1509         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1510
1511         /*
1512          * Only dispatch an event if vlan was
1513          * attached, otherwise there is nothing
1514          * to cleanup anyway.
1515          */
1516         if (parent != NULL)
1517                 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1518 }
1519
1520 /* Handle a reference counted flag that should be set on the parent as well */
1521 static int
1522 vlan_setflag(struct ifnet *ifp, int flag, int status,
1523              int (*func)(struct ifnet *, int))
1524 {
1525         struct ifvlan *ifv;
1526         int error;
1527
1528         VLAN_SXLOCK_ASSERT();
1529
1530         ifv = ifp->if_softc;
1531         status = status ? (ifp->if_flags & flag) : 0;
1532         /* Now "status" contains the flag value or 0 */
1533
1534         /*
1535          * See if recorded parent's status is different from what
1536          * we want it to be.  If it is, flip it.  We record parent's
1537          * status in ifv_pflags so that we won't clear parent's flag
1538          * we haven't set.  In fact, we don't clear or set parent's
1539          * flags directly, but get or release references to them.
1540          * That's why we can be sure that recorded flags still are
1541          * in accord with actual parent's flags.
1542          */
1543         if (status != (ifv->ifv_pflags & flag)) {
1544                 error = (*func)(PARENT(ifv), status);
1545                 if (error)
1546                         return (error);
1547                 ifv->ifv_pflags &= ~flag;
1548                 ifv->ifv_pflags |= status;
1549         }
1550         return (0);
1551 }
1552
1553 /*
1554  * Handle IFF_* flags that require certain changes on the parent:
1555  * if "status" is true, update parent's flags respective to our if_flags;
1556  * if "status" is false, forcedly clear the flags set on parent.
1557  */
1558 static int
1559 vlan_setflags(struct ifnet *ifp, int status)
1560 {
1561         int error, i;
1562         
1563         for (i = 0; vlan_pflags[i].flag; i++) {
1564                 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1565                                      status, vlan_pflags[i].func);
1566                 if (error)
1567                         return (error);
1568         }
1569         return (0);
1570 }
1571
1572 /* Inform all vlans that their parent has changed link state */
1573 static void
1574 vlan_link_state(struct ifnet *ifp)
1575 {
1576         struct epoch_tracker et;
1577         struct ifvlantrunk *trunk;
1578         struct ifvlan *ifv;
1579
1580         /* Called from a taskqueue_swi task, so we cannot sleep. */
1581         NET_EPOCH_ENTER(et);
1582         trunk = ifp->if_vlantrunk;
1583         if (trunk == NULL) {
1584                 NET_EPOCH_EXIT(et);
1585                 return;
1586         }
1587
1588         TRUNK_WLOCK(trunk);
1589         VLAN_FOREACH(ifv, trunk) {
1590                 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1591                 if_link_state_change(ifv->ifv_ifp,
1592                     trunk->parent->if_link_state);
1593         }
1594         TRUNK_WUNLOCK(trunk);
1595         NET_EPOCH_EXIT(et);
1596 }
1597
1598 static void
1599 vlan_capabilities(struct ifvlan *ifv)
1600 {
1601         struct ifnet *p;
1602         struct ifnet *ifp;
1603         struct ifnet_hw_tsomax hw_tsomax;
1604         int cap = 0, ena = 0, mena;
1605         u_long hwa = 0;
1606
1607         VLAN_SXLOCK_ASSERT();
1608         NET_EPOCH_ASSERT();
1609         p = PARENT(ifv);
1610         ifp = ifv->ifv_ifp;
1611
1612         /* Mask parent interface enabled capabilities disabled by user. */
1613         mena = p->if_capenable & ifv->ifv_capenable;
1614
1615         /*
1616          * If the parent interface can do checksum offloading
1617          * on VLANs, then propagate its hardware-assisted
1618          * checksumming flags. Also assert that checksum
1619          * offloading requires hardware VLAN tagging.
1620          */
1621         if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1622                 cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1623         if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1624             p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1625                 ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1626                 if (ena & IFCAP_TXCSUM)
1627                         hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1628                             CSUM_UDP | CSUM_SCTP);
1629                 if (ena & IFCAP_TXCSUM_IPV6)
1630                         hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1631                             CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
1632         }
1633
1634         /*
1635          * If the parent interface can do TSO on VLANs then
1636          * propagate the hardware-assisted flag. TSO on VLANs
1637          * does not necessarily require hardware VLAN tagging.
1638          */
1639         memset(&hw_tsomax, 0, sizeof(hw_tsomax));
1640         if_hw_tsomax_common(p, &hw_tsomax);
1641         if_hw_tsomax_update(ifp, &hw_tsomax);
1642         if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1643                 cap |= p->if_capabilities & IFCAP_TSO;
1644         if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1645                 ena |= mena & IFCAP_TSO;
1646                 if (ena & IFCAP_TSO)
1647                         hwa |= p->if_hwassist & CSUM_TSO;
1648         }
1649
1650         /*
1651          * If the parent interface can do LRO and checksum offloading on
1652          * VLANs, then guess it may do LRO on VLANs.  False positive here
1653          * cost nothing, while false negative may lead to some confusions.
1654          */
1655         if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1656                 cap |= p->if_capabilities & IFCAP_LRO;
1657         if (p->if_capenable & IFCAP_VLAN_HWCSUM)
1658                 ena |= p->if_capenable & IFCAP_LRO;
1659
1660         /*
1661          * If the parent interface can offload TCP connections over VLANs then
1662          * propagate its TOE capability to the VLAN interface.
1663          *
1664          * All TOE drivers in the tree today can deal with VLANs.  If this
1665          * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1666          * with its own bit.
1667          */
1668 #define IFCAP_VLAN_TOE IFCAP_TOE
1669         if (p->if_capabilities & IFCAP_VLAN_TOE)
1670                 cap |= p->if_capabilities & IFCAP_TOE;
1671         if (p->if_capenable & IFCAP_VLAN_TOE) {
1672                 TOEDEV(ifp) = TOEDEV(p);
1673                 ena |= mena & IFCAP_TOE;
1674         }
1675
1676         /*
1677          * If the parent interface supports dynamic link state, so does the
1678          * VLAN interface.
1679          */
1680         cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1681         ena |= (mena & IFCAP_LINKSTATE);
1682
1683 #ifdef RATELIMIT
1684         /*
1685          * If the parent interface supports ratelimiting, so does the
1686          * VLAN interface.
1687          */
1688         cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1689         ena |= (mena & IFCAP_TXRTLMT);
1690 #endif
1691
1692         ifp->if_capabilities = cap;
1693         ifp->if_capenable = ena;
1694         ifp->if_hwassist = hwa;
1695 }
1696
1697 static void
1698 vlan_trunk_capabilities(struct ifnet *ifp)
1699 {
1700         struct epoch_tracker et;
1701         struct ifvlantrunk *trunk;
1702         struct ifvlan *ifv;
1703
1704         VLAN_SLOCK();
1705         trunk = ifp->if_vlantrunk;
1706         if (trunk == NULL) {
1707                 VLAN_SUNLOCK();
1708                 return;
1709         }
1710         NET_EPOCH_ENTER(et);
1711         VLAN_FOREACH(ifv, trunk) {
1712                 vlan_capabilities(ifv);
1713         }
1714         NET_EPOCH_EXIT(et);
1715         VLAN_SUNLOCK();
1716 }
1717
1718 static int
1719 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1720 {
1721         struct ifnet *p;
1722         struct ifreq *ifr;
1723         struct ifaddr *ifa;
1724         struct ifvlan *ifv;
1725         struct ifvlantrunk *trunk;
1726         struct vlanreq vlr;
1727         int error = 0;
1728
1729         ifr = (struct ifreq *)data;
1730         ifa = (struct ifaddr *) data;
1731         ifv = ifp->if_softc;
1732
1733         switch (cmd) {
1734         case SIOCSIFADDR:
1735                 ifp->if_flags |= IFF_UP;
1736 #ifdef INET
1737                 if (ifa->ifa_addr->sa_family == AF_INET)
1738                         arp_ifinit(ifp, ifa);
1739 #endif
1740                 break;
1741         case SIOCGIFADDR:
1742                 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
1743                     ifp->if_addrlen);
1744                 break;
1745         case SIOCGIFMEDIA:
1746                 VLAN_SLOCK();
1747                 if (TRUNK(ifv) != NULL) {
1748                         p = PARENT(ifv);
1749                         if_ref(p);
1750                         error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1751                         if_rele(p);
1752                         /* Limit the result to the parent's current config. */
1753                         if (error == 0) {
1754                                 struct ifmediareq *ifmr;
1755
1756                                 ifmr = (struct ifmediareq *)data;
1757                                 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1758                                         ifmr->ifm_count = 1;
1759                                         error = copyout(&ifmr->ifm_current,
1760                                                 ifmr->ifm_ulist,
1761                                                 sizeof(int));
1762                                 }
1763                         }
1764                 } else {
1765                         error = EINVAL;
1766                 }
1767                 VLAN_SUNLOCK();
1768                 break;
1769
1770         case SIOCSIFMEDIA:
1771                 error = EINVAL;
1772                 break;
1773
1774         case SIOCSIFMTU:
1775                 /*
1776                  * Set the interface MTU.
1777                  */
1778                 VLAN_SLOCK();
1779                 trunk = TRUNK(ifv);
1780                 if (trunk != NULL) {
1781                         TRUNK_WLOCK(trunk);
1782                         if (ifr->ifr_mtu >
1783                              (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1784                             ifr->ifr_mtu <
1785                              (ifv->ifv_mintu - ifv->ifv_mtufudge))
1786                                 error = EINVAL;
1787                         else
1788                                 ifp->if_mtu = ifr->ifr_mtu;
1789                         TRUNK_WUNLOCK(trunk);
1790                 } else
1791                         error = EINVAL;
1792                 VLAN_SUNLOCK();
1793                 break;
1794
1795         case SIOCSETVLAN:
1796 #ifdef VIMAGE
1797                 /*
1798                  * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
1799                  * interface to be delegated to a jail without allowing the
1800                  * jail to change what underlying interface/VID it is
1801                  * associated with.  We are not entirely convinced that this
1802                  * is the right way to accomplish that policy goal.
1803                  */
1804                 if (ifp->if_vnet != ifp->if_home_vnet) {
1805                         error = EPERM;
1806                         break;
1807                 }
1808 #endif
1809                 error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
1810                 if (error)
1811                         break;
1812                 if (vlr.vlr_parent[0] == '\0') {
1813                         vlan_unconfig(ifp);
1814                         break;
1815                 }
1816                 p = ifunit_ref(vlr.vlr_parent);
1817                 if (p == NULL) {
1818                         error = ENOENT;
1819                         break;
1820                 }
1821                 error = vlan_config(ifv, p, vlr.vlr_tag);
1822                 if_rele(p);
1823                 break;
1824
1825         case SIOCGETVLAN:
1826 #ifdef VIMAGE
1827                 if (ifp->if_vnet != ifp->if_home_vnet) {
1828                         error = EPERM;
1829                         break;
1830                 }
1831 #endif
1832                 bzero(&vlr, sizeof(vlr));
1833                 VLAN_SLOCK();
1834                 if (TRUNK(ifv) != NULL) {
1835                         strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1836                             sizeof(vlr.vlr_parent));
1837                         vlr.vlr_tag = ifv->ifv_vid;
1838                 }
1839                 VLAN_SUNLOCK();
1840                 error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
1841                 break;
1842                 
1843         case SIOCSIFFLAGS:
1844                 /*
1845                  * We should propagate selected flags to the parent,
1846                  * e.g., promiscuous mode.
1847                  */
1848                 VLAN_XLOCK();
1849                 if (TRUNK(ifv) != NULL)
1850                         error = vlan_setflags(ifp, 1);
1851                 VLAN_XUNLOCK();
1852                 break;
1853
1854         case SIOCADDMULTI:
1855         case SIOCDELMULTI:
1856                 /*
1857                  * If we don't have a parent, just remember the membership for
1858                  * when we do.
1859                  *
1860                  * XXX We need the rmlock here to avoid sleeping while
1861                  * holding in6_multi_mtx.
1862                  */
1863                 VLAN_XLOCK();
1864                 trunk = TRUNK(ifv);
1865                 if (trunk != NULL)
1866                         error = vlan_setmulti(ifp);
1867                 VLAN_XUNLOCK();
1868
1869                 break;
1870         case SIOCGVLANPCP:
1871 #ifdef VIMAGE
1872                 if (ifp->if_vnet != ifp->if_home_vnet) {
1873                         error = EPERM;
1874                         break;
1875                 }
1876 #endif
1877                 ifr->ifr_vlan_pcp = ifv->ifv_pcp;
1878                 break;
1879
1880         case SIOCSVLANPCP:
1881 #ifdef VIMAGE
1882                 if (ifp->if_vnet != ifp->if_home_vnet) {
1883                         error = EPERM;
1884                         break;
1885                 }
1886 #endif
1887                 error = priv_check(curthread, PRIV_NET_SETVLANPCP);
1888                 if (error)
1889                         break;
1890                 if (ifr->ifr_vlan_pcp > 7) {
1891                         error = EINVAL;
1892                         break;
1893                 }
1894                 ifv->ifv_pcp = ifr->ifr_vlan_pcp;
1895                 ifp->if_pcp = ifv->ifv_pcp;
1896                 vlan_tag_recalculate(ifv);
1897                 /* broadcast event about PCP change */
1898                 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
1899                 break;
1900
1901         case SIOCSIFCAP:
1902                 VLAN_SLOCK();
1903                 ifv->ifv_capenable = ifr->ifr_reqcap;
1904                 trunk = TRUNK(ifv);
1905                 if (trunk != NULL) {
1906                         struct epoch_tracker et;
1907
1908                         NET_EPOCH_ENTER(et);
1909                         vlan_capabilities(ifv);
1910                         NET_EPOCH_EXIT(et);
1911                 }
1912                 VLAN_SUNLOCK();
1913                 break;
1914
1915         default:
1916                 error = EINVAL;
1917                 break;
1918         }
1919
1920         return (error);
1921 }
1922
1923 #ifdef RATELIMIT
1924 static int
1925 vlan_snd_tag_alloc(struct ifnet *ifp,
1926     union if_snd_tag_alloc_params *params,
1927     struct m_snd_tag **ppmt)
1928 {
1929
1930         /* get trunk device */
1931         ifp = vlan_trunkdev(ifp);
1932         if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1933                 return (EOPNOTSUPP);
1934         /* forward allocation request */
1935         return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1936 }
1937 #endif