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