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