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