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