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