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