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