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