]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/net/if_vlan.c
MFC r218166:
[FreeBSD/releng/8.2.git] / sys / net / if_vlan.c
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /*
31  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
32  * Might be extended some day to also handle IEEE 802.1p priority
33  * tagging.  This is sort of sneaky in the implementation, since
34  * we need to pretend to be enough of an Ethernet implementation
35  * to make arp work.  The way we do this is by telling everyone
36  * that we are an Ethernet, and then catch the packets that
37  * ether_output() left on our output queue when it calls
38  * if_start(), rewrite them for use by the real outgoing interface,
39  * and ask it to send them.
40  */
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include "opt_vlan.h"
46
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/module.h>
53 #include <sys/rwlock.h>
54 #include <sys/queue.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/systm.h>
59
60 #include <net/bpf.h>
61 #include <net/ethernet.h>
62 #include <net/if.h>
63 #include <net/if_clone.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/if_vlan_var.h>
67 #include <net/vnet.h>
68
69 #define VLANNAME        "vlan"
70 #define VLAN_DEF_HWIDTH 4
71 #define VLAN_IFFLAGS    (IFF_BROADCAST | IFF_MULTICAST)
72
73 #define UP_AND_RUNNING(ifp) \
74     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
75
76 LIST_HEAD(ifvlanhead, ifvlan);
77
78 struct ifvlantrunk {
79         struct  ifnet   *parent;        /* parent interface of this trunk */
80         struct  rwlock  rw;
81 #ifdef VLAN_ARRAY
82 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
83         struct  ifvlan  *vlans[VLAN_ARRAY_SIZE]; /* static table */
84 #else
85         struct  ifvlanhead *hash;       /* dynamic hash-list table */
86         uint16_t        hmask;
87         uint16_t        hwidth;
88 #endif
89         int             refcnt;
90 };
91
92 struct vlan_mc_entry {
93         struct ether_addr               mc_addr;
94         SLIST_ENTRY(vlan_mc_entry)      mc_entries;
95 };
96
97 struct  ifvlan {
98         struct  ifvlantrunk *ifv_trunk;
99         struct  ifnet *ifv_ifp;
100 #define TRUNK(ifv)      ((ifv)->ifv_trunk)
101 #define PARENT(ifv)     ((ifv)->ifv_trunk->parent)
102         int     ifv_pflags;     /* special flags we have set on parent */
103         struct  ifv_linkmib {
104                 int     ifvm_encaplen;  /* encapsulation length */
105                 int     ifvm_mtufudge;  /* MTU fudged by this much */
106                 int     ifvm_mintu;     /* min transmission unit */
107                 uint16_t ifvm_proto;    /* encapsulation ethertype */
108                 uint16_t ifvm_tag;      /* tag to apply on packets leaving if */
109         }       ifv_mib;
110         SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
111 #ifndef VLAN_ARRAY
112         LIST_ENTRY(ifvlan) ifv_list;
113 #endif
114 };
115 #define ifv_proto       ifv_mib.ifvm_proto
116 #define ifv_tag         ifv_mib.ifvm_tag
117 #define ifv_encaplen    ifv_mib.ifvm_encaplen
118 #define ifv_mtufudge    ifv_mib.ifvm_mtufudge
119 #define ifv_mintu       ifv_mib.ifvm_mintu
120
121 /* Special flags we should propagate to parent. */
122 static struct {
123         int flag;
124         int (*func)(struct ifnet *, int);
125 } vlan_pflags[] = {
126         {IFF_PROMISC, ifpromisc},
127         {IFF_ALLMULTI, if_allmulti},
128         {0, NULL}
129 };
130
131 SYSCTL_DECL(_net_link);
132 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
133 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
134
135 static int soft_pad = 0;
136 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
137            "pad short frames before tagging");
138
139 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
140
141 static eventhandler_tag ifdetach_tag;
142 static eventhandler_tag iflladdr_tag;
143
144 /*
145  * We have a global mutex, that is used to serialize configuration
146  * changes and isn't used in normal packet delivery.
147  *
148  * We also have a per-trunk rwlock, that is locked shared on packet
149  * processing and exclusive when configuration is changed.
150  *
151  * The VLAN_ARRAY substitutes the dynamic hash with a static array
152  * with 4096 entries. In theory this can give a boost in processing,
153  * however on practice it does not. Probably this is because array
154  * is too big to fit into CPU cache.
155  */
156 static struct mtx ifv_mtx;
157 #define VLAN_LOCK_INIT()        mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
158 #define VLAN_LOCK_DESTROY()     mtx_destroy(&ifv_mtx)
159 #define VLAN_LOCK_ASSERT()      mtx_assert(&ifv_mtx, MA_OWNED)
160 #define VLAN_LOCK()             mtx_lock(&ifv_mtx)
161 #define VLAN_UNLOCK()           mtx_unlock(&ifv_mtx)
162 #define TRUNK_LOCK_INIT(trunk)  rw_init(&(trunk)->rw, VLANNAME)
163 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
164 #define TRUNK_LOCK(trunk)       rw_wlock(&(trunk)->rw)
165 #define TRUNK_UNLOCK(trunk)     rw_wunlock(&(trunk)->rw)
166 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
167 #define TRUNK_RLOCK(trunk)      rw_rlock(&(trunk)->rw)
168 #define TRUNK_RUNLOCK(trunk)    rw_runlock(&(trunk)->rw)
169 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
170
171 #ifndef VLAN_ARRAY
172 static  void vlan_inithash(struct ifvlantrunk *trunk);
173 static  void vlan_freehash(struct ifvlantrunk *trunk);
174 static  int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
175 static  int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
176 static  void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
177 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
178         uint16_t tag);
179 #endif
180 static  void trunk_destroy(struct ifvlantrunk *trunk);
181
182 static  void vlan_start(struct ifnet *ifp);
183 static  void vlan_init(void *foo);
184 static  void vlan_input(struct ifnet *ifp, struct mbuf *m);
185 static  int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
186 static  int vlan_setflag(struct ifnet *ifp, int flag, int status,
187     int (*func)(struct ifnet *, int));
188 static  int vlan_setflags(struct ifnet *ifp, int status);
189 static  int vlan_setmulti(struct ifnet *ifp);
190 static  void vlan_unconfig(struct ifnet *ifp);
191 static  void vlan_unconfig_locked(struct ifnet *ifp);
192 static  int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
193 static  void vlan_link_state(struct ifnet *ifp, int link);
194 static  void vlan_capabilities(struct ifvlan *ifv);
195 static  void vlan_trunk_capabilities(struct ifnet *ifp);
196
197 static  struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
198     const char *, int *);
199 static  int vlan_clone_match(struct if_clone *, const char *);
200 static  int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
201 static  int vlan_clone_destroy(struct if_clone *, struct ifnet *);
202
203 static  void vlan_ifdetach(void *arg, struct ifnet *ifp);
204 static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
205
206 static  struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
207     IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
208
209 #ifdef VIMAGE
210 static VNET_DEFINE(struct if_clone, vlan_cloner);
211 #define V_vlan_cloner   VNET(vlan_cloner)
212 #endif
213
214 #ifndef VLAN_ARRAY
215 #define HASH(n, m)      ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
216
217 static void
218 vlan_inithash(struct ifvlantrunk *trunk)
219 {
220         int i, n;
221         
222         /*
223          * The trunk must not be locked here since we call malloc(M_WAITOK).
224          * It is OK in case this function is called before the trunk struct
225          * gets hooked up and becomes visible from other threads.
226          */
227
228         KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
229             ("%s: hash already initialized", __func__));
230
231         trunk->hwidth = VLAN_DEF_HWIDTH;
232         n = 1 << trunk->hwidth;
233         trunk->hmask = n - 1;
234         trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
235         for (i = 0; i < n; i++)
236                 LIST_INIT(&trunk->hash[i]);
237 }
238
239 static void
240 vlan_freehash(struct ifvlantrunk *trunk)
241 {
242 #ifdef INVARIANTS
243         int i;
244
245         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
246         for (i = 0; i < (1 << trunk->hwidth); i++)
247                 KASSERT(LIST_EMPTY(&trunk->hash[i]),
248                     ("%s: hash table not empty", __func__));
249 #endif
250         free(trunk->hash, M_VLAN);
251         trunk->hash = NULL;
252         trunk->hwidth = trunk->hmask = 0;
253 }
254
255 static int
256 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
257 {
258         int i, b;
259         struct ifvlan *ifv2;
260
261         TRUNK_LOCK_ASSERT(trunk);
262         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
263
264         b = 1 << trunk->hwidth;
265         i = HASH(ifv->ifv_tag, trunk->hmask);
266         LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
267                 if (ifv->ifv_tag == ifv2->ifv_tag)
268                         return (EEXIST);
269
270         /*
271          * Grow the hash when the number of vlans exceeds half of the number of
272          * hash buckets squared. This will make the average linked-list length
273          * buckets/2.
274          */
275         if (trunk->refcnt > (b * b) / 2) {
276                 vlan_growhash(trunk, 1);
277                 i = HASH(ifv->ifv_tag, trunk->hmask);
278         }
279         LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
280         trunk->refcnt++;
281
282         return (0);
283 }
284
285 static int
286 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
287 {
288         int i, b;
289         struct ifvlan *ifv2;
290
291         TRUNK_LOCK_ASSERT(trunk);
292         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
293         
294         b = 1 << trunk->hwidth;
295         i = HASH(ifv->ifv_tag, trunk->hmask);
296         LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
297                 if (ifv2 == ifv) {
298                         trunk->refcnt--;
299                         LIST_REMOVE(ifv2, ifv_list);
300                         if (trunk->refcnt < (b * b) / 2)
301                                 vlan_growhash(trunk, -1);
302                         return (0);
303                 }
304
305         panic("%s: vlan not found\n", __func__);
306         return (ENOENT); /*NOTREACHED*/
307 }
308
309 /*
310  * Grow the hash larger or smaller if memory permits.
311  */
312 static void
313 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
314 {
315         struct ifvlan *ifv;
316         struct ifvlanhead *hash2;
317         int hwidth2, i, j, n, n2;
318
319         TRUNK_LOCK_ASSERT(trunk);
320         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
321
322         if (howmuch == 0) {
323                 /* Harmless yet obvious coding error */
324                 printf("%s: howmuch is 0\n", __func__);
325                 return;
326         }
327
328         hwidth2 = trunk->hwidth + howmuch;
329         n = 1 << trunk->hwidth;
330         n2 = 1 << hwidth2;
331         /* Do not shrink the table below the default */
332         if (hwidth2 < VLAN_DEF_HWIDTH)
333                 return;
334
335         /* M_NOWAIT because we're called with trunk mutex held */
336         hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
337         if (hash2 == NULL) {
338                 printf("%s: out of memory -- hash size not changed\n",
339                     __func__);
340                 return;         /* We can live with the old hash table */
341         }
342         for (j = 0; j < n2; j++)
343                 LIST_INIT(&hash2[j]);
344         for (i = 0; i < n; i++)
345                 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
346                         LIST_REMOVE(ifv, ifv_list);
347                         j = HASH(ifv->ifv_tag, n2 - 1);
348                         LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
349                 }
350         free(trunk->hash, M_VLAN);
351         trunk->hash = hash2;
352         trunk->hwidth = hwidth2;
353         trunk->hmask = n2 - 1;
354
355         if (bootverbose)
356                 if_printf(trunk->parent,
357                     "VLAN hash table resized from %d to %d buckets\n", n, n2);
358 }
359
360 static __inline struct ifvlan *
361 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
362 {
363         struct ifvlan *ifv;
364
365         TRUNK_LOCK_RASSERT(trunk);
366
367         LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
368                 if (ifv->ifv_tag == tag)
369                         return (ifv);
370         return (NULL);
371 }
372
373 #if 0
374 /* Debugging code to view the hashtables. */
375 static void
376 vlan_dumphash(struct ifvlantrunk *trunk)
377 {
378         int i;
379         struct ifvlan *ifv;
380
381         for (i = 0; i < (1 << trunk->hwidth); i++) {
382                 printf("%d: ", i);
383                 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
384                         printf("%s ", ifv->ifv_ifp->if_xname);
385                 printf("\n");
386         }
387 }
388 #endif /* 0 */
389 #endif /* !VLAN_ARRAY */
390
391 static void
392 trunk_destroy(struct ifvlantrunk *trunk)
393 {
394         VLAN_LOCK_ASSERT();
395
396         TRUNK_LOCK(trunk);
397 #ifndef VLAN_ARRAY
398         vlan_freehash(trunk);
399 #endif
400         trunk->parent->if_vlantrunk = NULL;
401         TRUNK_UNLOCK(trunk);
402         TRUNK_LOCK_DESTROY(trunk);
403         free(trunk, M_VLAN);
404 }
405
406 /*
407  * Program our multicast filter. What we're actually doing is
408  * programming the multicast filter of the parent. This has the
409  * side effect of causing the parent interface to receive multicast
410  * traffic that it doesn't really want, which ends up being discarded
411  * later by the upper protocol layers. Unfortunately, there's no way
412  * to avoid this: there really is only one physical interface.
413  *
414  * XXX: There is a possible race here if more than one thread is
415  *      modifying the multicast state of the vlan interface at the same time.
416  */
417 static int
418 vlan_setmulti(struct ifnet *ifp)
419 {
420         struct ifnet            *ifp_p;
421         struct ifmultiaddr      *ifma, *rifma = NULL;
422         struct ifvlan           *sc;
423         struct vlan_mc_entry    *mc;
424         struct sockaddr_dl      sdl;
425         int                     error;
426
427         /*VLAN_LOCK_ASSERT();*/
428
429         /* Find the parent. */
430         sc = ifp->if_softc;
431         ifp_p = PARENT(sc);
432
433         CURVNET_SET_QUIET(ifp_p->if_vnet);
434
435         bzero((char *)&sdl, sizeof(sdl));
436         sdl.sdl_len = sizeof(sdl);
437         sdl.sdl_family = AF_LINK;
438         sdl.sdl_index = ifp_p->if_index;
439         sdl.sdl_type = IFT_ETHER;
440         sdl.sdl_alen = ETHER_ADDR_LEN;
441
442         /* First, remove any existing filter entries. */
443         while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
444                 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
445                 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
446                 if (error)
447                         return (error);
448                 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
449                 free(mc, M_VLAN);
450         }
451
452         /* Now program new ones. */
453         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
454                 if (ifma->ifma_addr->sa_family != AF_LINK)
455                         continue;
456                 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
457                 if (mc == NULL)
458                         return (ENOMEM);
459                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
460                     (char *)&mc->mc_addr, ETHER_ADDR_LEN);
461                 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
462                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
463                     LLADDR(&sdl), ETHER_ADDR_LEN);
464                 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
465                 if (error)
466                         return (error);
467         }
468
469         CURVNET_RESTORE();
470         return (0);
471 }
472
473 /*
474  * A handler for parent interface link layer address changes.
475  * If the parent interface link layer address is changed we
476  * should also change it on all children vlans.
477  */
478 static void
479 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
480 {
481         struct ifvlan *ifv;
482 #ifndef VLAN_ARRAY
483         struct ifvlan *next;
484 #endif
485         int i;
486
487         /*
488          * Check if it's a trunk interface first of all
489          * to avoid needless locking.
490          */
491         if (ifp->if_vlantrunk == NULL)
492                 return;
493
494         VLAN_LOCK();
495         /*
496          * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
497          */
498 #ifdef VLAN_ARRAY
499         for (i = 0; i < VLAN_ARRAY_SIZE; i++)
500                 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
501 #else /* VLAN_ARRAY */
502         for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
503                 LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) {
504 #endif /* VLAN_ARRAY */
505                         VLAN_UNLOCK();
506                         if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
507                         VLAN_LOCK();
508                 }
509         VLAN_UNLOCK();
510
511 }
512
513 /*
514  * A handler for network interface departure events.
515  * Track departure of trunks here so that we don't access invalid
516  * pointers or whatever if a trunk is ripped from under us, e.g.,
517  * by ejecting its hot-plug card.  However, if an ifnet is simply
518  * being renamed, then there's no need to tear down the state.
519  */
520 static void
521 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
522 {
523         struct ifvlan *ifv;
524         int i;
525
526         /*
527          * Check if it's a trunk interface first of all
528          * to avoid needless locking.
529          */
530         if (ifp->if_vlantrunk == NULL)
531                 return;
532
533         /* If the ifnet is just being renamed, don't do anything. */
534         if (ifp->if_flags & IFF_RENAMING)
535                 return;
536
537         VLAN_LOCK();
538         /*
539          * OK, it's a trunk.  Loop over and detach all vlan's on it.
540          * Check trunk pointer after each vlan_unconfig() as it will
541          * free it and set to NULL after the last vlan was detached.
542          */
543 #ifdef VLAN_ARRAY
544         for (i = 0; i < VLAN_ARRAY_SIZE; i++)
545                 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
546                         vlan_unconfig_locked(ifv->ifv_ifp);
547                         if (ifp->if_vlantrunk == NULL)
548                                 break;
549                 }
550 #else /* VLAN_ARRAY */
551 restart:
552         for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
553                 if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
554                         vlan_unconfig_locked(ifv->ifv_ifp);
555                         if (ifp->if_vlantrunk)
556                                 goto restart;   /* trunk->hwidth can change */
557                         else
558                                 break;
559                 }
560 #endif /* VLAN_ARRAY */
561         /* Trunk should have been destroyed in vlan_unconfig(). */
562         KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
563         VLAN_UNLOCK();
564 }
565
566 /*
567  * VLAN support can be loaded as a module.  The only place in the
568  * system that's intimately aware of this is ether_input.  We hook
569  * into this code through vlan_input_p which is defined there and
570  * set here.  Noone else in the system should be aware of this so
571  * we use an explicit reference here.
572  */
573 extern  void (*vlan_input_p)(struct ifnet *, struct mbuf *);
574
575 /* For if_link_state_change() eyes only... */
576 extern  void (*vlan_link_state_p)(struct ifnet *, int);
577
578 static int
579 vlan_modevent(module_t mod, int type, void *data)
580 {
581
582         switch (type) {
583         case MOD_LOAD:
584                 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
585                     vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
586                 if (ifdetach_tag == NULL)
587                         return (ENOMEM);
588                 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
589                     vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
590                 if (iflladdr_tag == NULL)
591                         return (ENOMEM);
592                 VLAN_LOCK_INIT();
593                 vlan_input_p = vlan_input;
594                 vlan_link_state_p = vlan_link_state;
595                 vlan_trunk_cap_p = vlan_trunk_capabilities;
596 #ifndef VIMAGE
597                 if_clone_attach(&vlan_cloner);
598 #endif
599                 if (bootverbose)
600                         printf("vlan: initialized, using "
601 #ifdef VLAN_ARRAY
602                                "full-size arrays"
603 #else
604                                "hash tables with chaining"
605 #endif
606                         
607                                "\n");
608                 break;
609         case MOD_UNLOAD:
610 #ifndef VIMAGE
611                 if_clone_detach(&vlan_cloner);
612 #endif
613                 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
614                 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
615                 vlan_input_p = NULL;
616                 vlan_link_state_p = NULL;
617                 vlan_trunk_cap_p = NULL;
618                 VLAN_LOCK_DESTROY();
619                 if (bootverbose)
620                         printf("vlan: unloaded\n");
621                 break;
622         default:
623                 return (EOPNOTSUPP);
624         }
625         return (0);
626 }
627
628 static moduledata_t vlan_mod = {
629         "if_vlan",
630         vlan_modevent,
631         0
632 };
633
634 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
635 MODULE_VERSION(if_vlan, 3);
636
637 #ifdef VIMAGE
638 static void
639 vnet_vlan_init(const void *unused __unused)
640 {
641
642         V_vlan_cloner = vlan_cloner;
643         if_clone_attach(&V_vlan_cloner);
644 }
645 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
646     vnet_vlan_init, NULL);
647
648 static void
649 vnet_vlan_uninit(const void *unused __unused)
650 {
651
652         if_clone_detach(&V_vlan_cloner);
653 }
654 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
655     vnet_vlan_uninit, NULL);
656 #endif
657
658 static struct ifnet *
659 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
660 {
661         const char *cp;
662         struct ifnet *ifp;
663         int t;
664
665         /* Check for <etherif>.<vlan> style interface names. */
666         IFNET_RLOCK_NOSLEEP();
667         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
668                 if (ifp->if_type != IFT_ETHER)
669                         continue;
670                 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
671                         continue;
672                 cp = name + strlen(ifp->if_xname);
673                 if (*cp++ != '.')
674                         continue;
675                 if (*cp == '\0')
676                         continue;
677                 t = 0;
678                 for(; *cp >= '0' && *cp <= '9'; cp++)
679                         t = (t * 10) + (*cp - '0');
680                 if (*cp != '\0')
681                         continue;
682                 if (tag != NULL)
683                         *tag = t;
684                 break;
685         }
686         IFNET_RUNLOCK_NOSLEEP();
687
688         return (ifp);
689 }
690
691 static int
692 vlan_clone_match(struct if_clone *ifc, const char *name)
693 {
694         const char *cp;
695
696         if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
697                 return (1);
698
699         if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
700                 return (0);
701         for (cp = name + 4; *cp != '\0'; cp++) {
702                 if (*cp < '0' || *cp > '9')
703                         return (0);
704         }
705
706         return (1);
707 }
708
709 static int
710 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
711 {
712         char *dp;
713         int wildcard;
714         int unit;
715         int error;
716         int tag;
717         int ethertag;
718         struct ifvlan *ifv;
719         struct ifnet *ifp;
720         struct ifnet *p;
721         struct vlanreq vlr;
722         static const u_char eaddr[ETHER_ADDR_LEN];      /* 00:00:00:00:00:00 */
723
724         /*
725          * There are 3 (ugh) ways to specify the cloned device:
726          * o pass a parameter block with the clone request.
727          * o specify parameters in the text of the clone device name
728          * o specify no parameters and get an unattached device that
729          *   must be configured separately.
730          * The first technique is preferred; the latter two are
731          * supported for backwards compatibilty.
732          */
733         if (params) {
734                 error = copyin(params, &vlr, sizeof(vlr));
735                 if (error)
736                         return error;
737                 p = ifunit(vlr.vlr_parent);
738                 if (p == NULL)
739                         return ENXIO;
740                 /*
741                  * Don't let the caller set up a VLAN tag with
742                  * anything except VLID bits.
743                  */
744                 if (vlr.vlr_tag & ~EVL_VLID_MASK)
745                         return (EINVAL);
746                 error = ifc_name2unit(name, &unit);
747                 if (error != 0)
748                         return (error);
749
750                 ethertag = 1;
751                 tag = vlr.vlr_tag;
752                 wildcard = (unit < 0);
753         } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
754                 ethertag = 1;
755                 unit = -1;
756                 wildcard = 0;
757
758                 /*
759                  * Don't let the caller set up a VLAN tag with
760                  * anything except VLID bits.
761                  */
762                 if (tag & ~EVL_VLID_MASK)
763                         return (EINVAL);
764         } else {
765                 ethertag = 0;
766
767                 error = ifc_name2unit(name, &unit);
768                 if (error != 0)
769                         return (error);
770
771                 wildcard = (unit < 0);
772         }
773
774         error = ifc_alloc_unit(ifc, &unit);
775         if (error != 0)
776                 return (error);
777
778         /* In the wildcard case, we need to update the name. */
779         if (wildcard) {
780                 for (dp = name; *dp != '\0'; dp++);
781                 if (snprintf(dp, len - (dp-name), "%d", unit) >
782                     len - (dp-name) - 1) {
783                         panic("%s: interface name too long", __func__);
784                 }
785         }
786
787         ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
788         ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
789         if (ifp == NULL) {
790                 ifc_free_unit(ifc, unit);
791                 free(ifv, M_VLAN);
792                 return (ENOSPC);
793         }
794         SLIST_INIT(&ifv->vlan_mc_listhead);
795
796         ifp->if_softc = ifv;
797         /*
798          * Set the name manually rather than using if_initname because
799          * we don't conform to the default naming convention for interfaces.
800          */
801         strlcpy(ifp->if_xname, name, IFNAMSIZ);
802         ifp->if_dname = ifc->ifc_name;
803         ifp->if_dunit = unit;
804         /* NB: flags are not set here */
805         ifp->if_linkmib = &ifv->ifv_mib;
806         ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
807         /* NB: mtu is not set here */
808
809         ifp->if_init = vlan_init;
810         ifp->if_start = vlan_start;
811         ifp->if_ioctl = vlan_ioctl;
812         ifp->if_snd.ifq_maxlen = ifqmaxlen;
813         ifp->if_flags = VLAN_IFFLAGS;
814         ether_ifattach(ifp, eaddr);
815         /* Now undo some of the damage... */
816         ifp->if_baudrate = 0;
817         ifp->if_type = IFT_L2VLAN;
818         ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
819
820         if (ethertag) {
821                 error = vlan_config(ifv, p, tag);
822                 if (error != 0) {
823                         /*
824                          * Since we've partialy failed, we need to back
825                          * out all the way, otherwise userland could get
826                          * confused.  Thus, we destroy the interface.
827                          */
828                         ether_ifdetach(ifp);
829                         vlan_unconfig(ifp);
830                         if_free_type(ifp, IFT_ETHER);
831                         ifc_free_unit(ifc, unit);
832                         free(ifv, M_VLAN);
833
834                         return (error);
835                 }
836
837                 /* Update flags on the parent, if necessary. */
838                 vlan_setflags(ifp, 1);
839         }
840
841         return (0);
842 }
843
844 static int
845 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
846 {
847         struct ifvlan *ifv = ifp->if_softc;
848         int unit = ifp->if_dunit;
849
850         ether_ifdetach(ifp);    /* first, remove it from system-wide lists */
851         vlan_unconfig(ifp);     /* now it can be unconfigured and freed */
852         if_free_type(ifp, IFT_ETHER);
853         free(ifv, M_VLAN);
854         ifc_free_unit(ifc, unit);
855
856         return (0);
857 }
858
859 /*
860  * The ifp->if_init entry point for vlan(4) is a no-op.
861  */
862 static void
863 vlan_init(void *foo __unused)
864 {
865 }
866
867 /*
868  * The if_start method for vlan(4) interface. It doesn't
869  * raises the IFF_DRV_OACTIVE flag, since it is called
870  * only from IFQ_HANDOFF() macro in ether_output_frame().
871  * If the interface queue is full, and vlan_start() is
872  * not called, the queue would never get emptied and
873  * interface would stall forever.
874  */
875 static void
876 vlan_start(struct ifnet *ifp)
877 {
878         struct ifvlan *ifv;
879         struct ifnet *p;
880         struct mbuf *m;
881         int error;
882
883         ifv = ifp->if_softc;
884         p = PARENT(ifv);
885
886         for (;;) {
887                 IF_DEQUEUE(&ifp->if_snd, m);
888                 if (m == NULL)
889                         break;
890                 BPF_MTAP(ifp, m);
891
892                 /*
893                  * Do not run parent's if_start() if the parent is not up,
894                  * or parent's driver will cause a system crash.
895                  */
896                 if (!UP_AND_RUNNING(p)) {
897                         m_freem(m);
898                         ifp->if_collisions++;
899                         continue;
900                 }
901
902                 /*
903                  * Pad the frame to the minimum size allowed if told to.
904                  * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
905                  * paragraph C.4.4.3.b.  It can help to work around buggy
906                  * bridges that violate paragraph C.4.4.3.a from the same
907                  * document, i.e., fail to pad short frames after untagging.
908                  * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
909                  * untagging it will produce a 62-byte frame, which is a runt
910                  * and requires padding.  There are VLAN-enabled network
911                  * devices that just discard such runts instead or mishandle
912                  * them somehow.
913                  */
914                 if (soft_pad) {
915                         static char pad[8];     /* just zeros */
916                         int n;
917
918                         for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
919                              n > 0; n -= sizeof(pad))
920                                 if (!m_append(m, min(n, sizeof(pad)), pad))
921                                         break;
922
923                         if (n > 0) {
924                                 if_printf(ifp, "cannot pad short frame\n");
925                                 ifp->if_oerrors++;
926                                 m_freem(m);
927                                 continue;
928                         }
929                 }
930
931                 /*
932                  * If underlying interface can do VLAN tag insertion itself,
933                  * just pass the packet along. However, we need some way to
934                  * tell the interface where the packet came from so that it
935                  * knows how to find the VLAN tag to use, so we attach a
936                  * packet tag that holds it.
937                  */
938                 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
939                         m->m_pkthdr.ether_vtag = ifv->ifv_tag;
940                         m->m_flags |= M_VLANTAG;
941                 } else {
942                         m = ether_vlanencap(m, ifv->ifv_tag);
943                         if (m == NULL) {
944                                 if_printf(ifp,
945                                     "unable to prepend VLAN header\n");
946                                 ifp->if_oerrors++;
947                                 continue;
948                         }
949                 }
950
951                 /*
952                  * Send it, precisely as ether_output() would have.
953                  * We are already running at splimp.
954                  */
955                 error = (p->if_transmit)(p, m);
956                 if (!error)
957                         ifp->if_opackets++;
958                 else
959                         ifp->if_oerrors++;
960         }
961 }
962
963 static void
964 vlan_input(struct ifnet *ifp, struct mbuf *m)
965 {
966         struct ifvlantrunk *trunk = ifp->if_vlantrunk;
967         struct ifvlan *ifv;
968         uint16_t tag;
969
970         KASSERT(trunk != NULL, ("%s: no trunk", __func__));
971
972         if (m->m_flags & M_VLANTAG) {
973                 /*
974                  * Packet is tagged, but m contains a normal
975                  * Ethernet frame; the tag is stored out-of-band.
976                  */
977                 tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
978                 m->m_flags &= ~M_VLANTAG;
979         } else {
980                 struct ether_vlan_header *evl;
981
982                 /*
983                  * Packet is tagged in-band as specified by 802.1q.
984                  */
985                 switch (ifp->if_type) {
986                 case IFT_ETHER:
987                         if (m->m_len < sizeof(*evl) &&
988                             (m = m_pullup(m, sizeof(*evl))) == NULL) {
989                                 if_printf(ifp, "cannot pullup VLAN header\n");
990                                 return;
991                         }
992                         evl = mtod(m, struct ether_vlan_header *);
993                         tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
994
995                         /*
996                          * Remove the 802.1q header by copying the Ethernet
997                          * addresses over it and adjusting the beginning of
998                          * the data in the mbuf.  The encapsulated Ethernet
999                          * type field is already in place.
1000                          */
1001                         bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1002                               ETHER_HDR_LEN - ETHER_TYPE_LEN);
1003                         m_adj(m, ETHER_VLAN_ENCAP_LEN);
1004                         break;
1005
1006                 default:
1007 #ifdef INVARIANTS
1008                         panic("%s: %s has unsupported if_type %u",
1009                               __func__, ifp->if_xname, ifp->if_type);
1010 #endif
1011                         m_freem(m);
1012                         ifp->if_noproto++;
1013                         return;
1014                 }
1015         }
1016
1017         TRUNK_RLOCK(trunk);
1018 #ifdef VLAN_ARRAY
1019         ifv = trunk->vlans[tag];
1020 #else
1021         ifv = vlan_gethash(trunk, tag);
1022 #endif
1023         if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1024                 TRUNK_RUNLOCK(trunk);
1025                 m_freem(m);
1026                 ifp->if_noproto++;
1027                 return;
1028         }
1029         TRUNK_RUNLOCK(trunk);
1030
1031         m->m_pkthdr.rcvif = ifv->ifv_ifp;
1032         ifv->ifv_ifp->if_ipackets++;
1033
1034         /* Pass it back through the parent's input routine. */
1035         (*ifp->if_input)(ifv->ifv_ifp, m);
1036 }
1037
1038 static int
1039 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
1040 {
1041         struct ifvlantrunk *trunk;
1042         struct ifnet *ifp;
1043         int error = 0;
1044
1045         /* VID numbers 0x0 and 0xFFF are reserved */
1046         if (tag == 0 || tag == 0xFFF)
1047                 return (EINVAL);
1048         if (p->if_type != IFT_ETHER)
1049                 return (EPROTONOSUPPORT);
1050         if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1051                 return (EPROTONOSUPPORT);
1052         if (ifv->ifv_trunk)
1053                 return (EBUSY);
1054
1055         if (p->if_vlantrunk == NULL) {
1056                 trunk = malloc(sizeof(struct ifvlantrunk),
1057                     M_VLAN, M_WAITOK | M_ZERO);
1058 #ifndef VLAN_ARRAY
1059                 vlan_inithash(trunk);
1060 #endif
1061                 VLAN_LOCK();
1062                 if (p->if_vlantrunk != NULL) {
1063                         /* A race that that is very unlikely to be hit. */
1064 #ifndef VLAN_ARRAY
1065                         vlan_freehash(trunk);
1066 #endif
1067                         free(trunk, M_VLAN);
1068                         goto exists;
1069                 }
1070                 TRUNK_LOCK_INIT(trunk);
1071                 TRUNK_LOCK(trunk);
1072                 p->if_vlantrunk = trunk;
1073                 trunk->parent = p;
1074         } else {
1075                 VLAN_LOCK();
1076 exists:
1077                 trunk = p->if_vlantrunk;
1078                 TRUNK_LOCK(trunk);
1079         }
1080
1081         ifv->ifv_tag = tag;     /* must set this before vlan_inshash() */
1082 #ifdef VLAN_ARRAY
1083         if (trunk->vlans[tag] != NULL) {
1084                 error = EEXIST;
1085                 goto done;
1086         }
1087         trunk->vlans[tag] = ifv;
1088         trunk->refcnt++;
1089 #else
1090         error = vlan_inshash(trunk, ifv);
1091         if (error)
1092                 goto done;
1093 #endif
1094         ifv->ifv_proto = ETHERTYPE_VLAN;
1095         ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1096         ifv->ifv_mintu = ETHERMIN;
1097         ifv->ifv_pflags = 0;
1098
1099         /*
1100          * If the parent supports the VLAN_MTU capability,
1101          * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1102          * use it.
1103          */
1104         if (p->if_capenable & IFCAP_VLAN_MTU) {
1105                 /*
1106                  * No need to fudge the MTU since the parent can
1107                  * handle extended frames.
1108                  */
1109                 ifv->ifv_mtufudge = 0;
1110         } else {
1111                 /*
1112                  * Fudge the MTU by the encapsulation size.  This
1113                  * makes us incompatible with strictly compliant
1114                  * 802.1Q implementations, but allows us to use
1115                  * the feature with other NetBSD implementations,
1116                  * which might still be useful.
1117                  */
1118                 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1119         }
1120
1121         ifv->ifv_trunk = trunk;
1122         ifp = ifv->ifv_ifp;
1123         ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1124         ifp->if_baudrate = p->if_baudrate;
1125         /*
1126          * Copy only a selected subset of flags from the parent.
1127          * Other flags are none of our business.
1128          */
1129 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1130         ifp->if_flags &= ~VLAN_COPY_FLAGS;
1131         ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1132 #undef VLAN_COPY_FLAGS
1133
1134         ifp->if_link_state = p->if_link_state;
1135
1136         vlan_capabilities(ifv);
1137
1138         /*
1139          * Set up our ``Ethernet address'' to reflect the underlying
1140          * physical interface's.
1141          */
1142         bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
1143
1144         /*
1145          * Configure multicast addresses that may already be
1146          * joined on the vlan device.
1147          */
1148         (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1149
1150         /* We are ready for operation now. */
1151         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1152 done:
1153         TRUNK_UNLOCK(trunk);
1154         if (error == 0)
1155                 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag);
1156         VLAN_UNLOCK();
1157
1158         return (error);
1159 }
1160
1161 static void
1162 vlan_unconfig(struct ifnet *ifp)
1163 {
1164
1165         VLAN_LOCK();
1166         vlan_unconfig_locked(ifp);
1167         VLAN_UNLOCK();
1168 }
1169
1170 static void
1171 vlan_unconfig_locked(struct ifnet *ifp)
1172 {
1173         struct ifvlantrunk *trunk;
1174         struct vlan_mc_entry *mc;
1175         struct ifvlan *ifv;
1176         struct ifnet  *parent;
1177
1178         VLAN_LOCK_ASSERT();
1179
1180         ifv = ifp->if_softc;
1181         trunk = ifv->ifv_trunk;
1182         parent = NULL;
1183
1184         if (trunk != NULL) {
1185                 struct sockaddr_dl sdl;
1186
1187                 TRUNK_LOCK(trunk);
1188                 parent = trunk->parent;
1189
1190                 /*
1191                  * Since the interface is being unconfigured, we need to
1192                  * empty the list of multicast groups that we may have joined
1193                  * while we were alive from the parent's list.
1194                  */
1195                 bzero((char *)&sdl, sizeof(sdl));
1196                 sdl.sdl_len = sizeof(sdl);
1197                 sdl.sdl_family = AF_LINK;
1198                 sdl.sdl_index = parent->if_index;
1199                 sdl.sdl_type = IFT_ETHER;
1200                 sdl.sdl_alen = ETHER_ADDR_LEN;
1201
1202                 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1203                         bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
1204                             ETHER_ADDR_LEN);
1205
1206                         /*
1207                          * This may fail if the parent interface is
1208                          * being detached.  Regardless, we should do a
1209                          * best effort to free this interface as much
1210                          * as possible as all callers expect vlan
1211                          * destruction to succeed.
1212                          */
1213                         (void)if_delmulti(parent, (struct sockaddr *)&sdl);
1214                         SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1215                         free(mc, M_VLAN);
1216                 }
1217
1218                 vlan_setflags(ifp, 0); /* clear special flags on parent */
1219 #ifdef VLAN_ARRAY
1220                 trunk->vlans[ifv->ifv_tag] = NULL;
1221                 trunk->refcnt--;
1222 #else
1223                 vlan_remhash(trunk, ifv);
1224 #endif
1225                 ifv->ifv_trunk = NULL;
1226
1227                 /*
1228                  * Check if we were the last.
1229                  */
1230                 if (trunk->refcnt == 0) {
1231                         trunk->parent->if_vlantrunk = NULL;
1232                         /*
1233                          * XXXGL: If some ithread has already entered
1234                          * vlan_input() and is now blocked on the trunk
1235                          * lock, then it should preempt us right after
1236                          * unlock and finish its work. Then we will acquire
1237                          * lock again in trunk_destroy().
1238                          */
1239                         TRUNK_UNLOCK(trunk);
1240                         trunk_destroy(trunk);
1241                 } else
1242                         TRUNK_UNLOCK(trunk);
1243         }
1244
1245         /* Disconnect from parent. */
1246         if (ifv->ifv_pflags)
1247                 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1248         ifp->if_mtu = ETHERMTU;
1249         ifp->if_link_state = LINK_STATE_UNKNOWN;
1250         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1251
1252         /*
1253          * Only dispatch an event if vlan was
1254          * attached, otherwise there is nothing
1255          * to cleanup anyway.
1256          */
1257         if (parent != NULL)
1258                 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag);
1259 }
1260
1261 /* Handle a reference counted flag that should be set on the parent as well */
1262 static int
1263 vlan_setflag(struct ifnet *ifp, int flag, int status,
1264              int (*func)(struct ifnet *, int))
1265 {
1266         struct ifvlan *ifv;
1267         int error;
1268
1269         /* XXX VLAN_LOCK_ASSERT(); */
1270
1271         ifv = ifp->if_softc;
1272         status = status ? (ifp->if_flags & flag) : 0;
1273         /* Now "status" contains the flag value or 0 */
1274
1275         /*
1276          * See if recorded parent's status is different from what
1277          * we want it to be.  If it is, flip it.  We record parent's
1278          * status in ifv_pflags so that we won't clear parent's flag
1279          * we haven't set.  In fact, we don't clear or set parent's
1280          * flags directly, but get or release references to them.
1281          * That's why we can be sure that recorded flags still are
1282          * in accord with actual parent's flags.
1283          */
1284         if (status != (ifv->ifv_pflags & flag)) {
1285                 error = (*func)(PARENT(ifv), status);
1286                 if (error)
1287                         return (error);
1288                 ifv->ifv_pflags &= ~flag;
1289                 ifv->ifv_pflags |= status;
1290         }
1291         return (0);
1292 }
1293
1294 /*
1295  * Handle IFF_* flags that require certain changes on the parent:
1296  * if "status" is true, update parent's flags respective to our if_flags;
1297  * if "status" is false, forcedly clear the flags set on parent.
1298  */
1299 static int
1300 vlan_setflags(struct ifnet *ifp, int status)
1301 {
1302         int error, i;
1303         
1304         for (i = 0; vlan_pflags[i].flag; i++) {
1305                 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1306                                      status, vlan_pflags[i].func);
1307                 if (error)
1308                         return (error);
1309         }
1310         return (0);
1311 }
1312
1313 /* Inform all vlans that their parent has changed link state */
1314 static void
1315 vlan_link_state(struct ifnet *ifp, int link)
1316 {
1317         struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1318         struct ifvlan *ifv;
1319         int i;
1320
1321         TRUNK_LOCK(trunk);
1322 #ifdef VLAN_ARRAY
1323         for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1324                 if (trunk->vlans[i] != NULL) {
1325                         ifv = trunk->vlans[i];
1326 #else
1327         for (i = 0; i < (1 << trunk->hwidth); i++)
1328                 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
1329 #endif
1330                         ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1331                         if_link_state_change(ifv->ifv_ifp,
1332                             trunk->parent->if_link_state);
1333                 }
1334         TRUNK_UNLOCK(trunk);
1335 }
1336
1337 static void
1338 vlan_capabilities(struct ifvlan *ifv)
1339 {
1340         struct ifnet *p = PARENT(ifv);
1341         struct ifnet *ifp = ifv->ifv_ifp;
1342
1343         TRUNK_LOCK_ASSERT(TRUNK(ifv));
1344
1345         /*
1346          * If the parent interface can do checksum offloading
1347          * on VLANs, then propagate its hardware-assisted
1348          * checksumming flags. Also assert that checksum
1349          * offloading requires hardware VLAN tagging.
1350          */
1351         if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1352                 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1353
1354         if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1355             p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1356                 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1357                 ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP |
1358                     CSUM_UDP | CSUM_SCTP | CSUM_IP_FRAGS | CSUM_FRAGMENT);
1359         } else {
1360                 ifp->if_capenable = 0;
1361                 ifp->if_hwassist = 0;
1362         }
1363         /*
1364          * If the parent interface can do TSO on VLANs then
1365          * propagate the hardware-assisted flag. TSO on VLANs
1366          * does not necessarily require hardware VLAN tagging.
1367          */
1368         if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1369                 ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO;
1370         if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1371                 ifp->if_capenable |= p->if_capenable & IFCAP_TSO;
1372                 ifp->if_hwassist |= p->if_hwassist & CSUM_TSO;
1373         } else {
1374                 ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO);
1375                 ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO);
1376         }
1377 }
1378
1379 static void
1380 vlan_trunk_capabilities(struct ifnet *ifp)
1381 {
1382         struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1383         struct ifvlan *ifv;
1384         int i;
1385
1386         TRUNK_LOCK(trunk);
1387 #ifdef VLAN_ARRAY
1388         for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1389                 if (trunk->vlans[i] != NULL) {
1390                         ifv = trunk->vlans[i];
1391 #else
1392         for (i = 0; i < (1 << trunk->hwidth); i++) {
1393                 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1394 #endif
1395                         vlan_capabilities(ifv);
1396         }
1397         TRUNK_UNLOCK(trunk);
1398 }
1399
1400 static int
1401 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1402 {
1403         struct ifnet *p;
1404         struct ifreq *ifr;
1405         struct ifvlan *ifv;
1406         struct vlanreq vlr;
1407         int error = 0;
1408
1409         ifr = (struct ifreq *)data;
1410         ifv = ifp->if_softc;
1411
1412         switch (cmd) {
1413         case SIOCGIFMEDIA:
1414                 VLAN_LOCK();
1415                 if (TRUNK(ifv) != NULL) {
1416                         p = PARENT(ifv);
1417                         VLAN_UNLOCK();
1418                         error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1419                         /* Limit the result to the parent's current config. */
1420                         if (error == 0) {
1421                                 struct ifmediareq *ifmr;
1422
1423                                 ifmr = (struct ifmediareq *)data;
1424                                 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1425                                         ifmr->ifm_count = 1;
1426                                         error = copyout(&ifmr->ifm_current,
1427                                                 ifmr->ifm_ulist,
1428                                                 sizeof(int));
1429                                 }
1430                         }
1431                 } else {
1432                         VLAN_UNLOCK();
1433                         error = EINVAL;
1434                 }
1435                 break;
1436
1437         case SIOCSIFMEDIA:
1438                 error = EINVAL;
1439                 break;
1440
1441         case SIOCSIFMTU:
1442                 /*
1443                  * Set the interface MTU.
1444                  */
1445                 VLAN_LOCK();
1446                 if (TRUNK(ifv) != NULL) {
1447                         if (ifr->ifr_mtu >
1448                              (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1449                             ifr->ifr_mtu <
1450                              (ifv->ifv_mintu - ifv->ifv_mtufudge))
1451                                 error = EINVAL;
1452                         else
1453                                 ifp->if_mtu = ifr->ifr_mtu;
1454                 } else
1455                         error = EINVAL;
1456                 VLAN_UNLOCK();
1457                 break;
1458
1459         case SIOCSETVLAN:
1460 #ifdef VIMAGE
1461                 if (ifp->if_vnet != ifp->if_home_vnet) {
1462                         error = EPERM;
1463                         break;
1464                 }
1465 #endif
1466                 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1467                 if (error)
1468                         break;
1469                 if (vlr.vlr_parent[0] == '\0') {
1470                         vlan_unconfig(ifp);
1471                         break;
1472                 }
1473                 p = ifunit(vlr.vlr_parent);
1474                 if (p == 0) {
1475                         error = ENOENT;
1476                         break;
1477                 }
1478                 /*
1479                  * Don't let the caller set up a VLAN tag with
1480                  * anything except VLID bits.
1481                  */
1482                 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1483                         error = EINVAL;
1484                         break;
1485                 }
1486                 error = vlan_config(ifv, p, vlr.vlr_tag);
1487                 if (error)
1488                         break;
1489
1490                 /* Update flags on the parent, if necessary. */
1491                 vlan_setflags(ifp, 1);
1492                 break;
1493
1494         case SIOCGETVLAN:
1495 #ifdef VIMAGE
1496                 if (ifp->if_vnet != ifp->if_home_vnet) {
1497                         error = EPERM;
1498                         break;
1499                 }
1500 #endif
1501                 bzero(&vlr, sizeof(vlr));
1502                 VLAN_LOCK();
1503                 if (TRUNK(ifv) != NULL) {
1504                         strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1505                             sizeof(vlr.vlr_parent));
1506                         vlr.vlr_tag = ifv->ifv_tag;
1507                 }
1508                 VLAN_UNLOCK();
1509                 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1510                 break;
1511                 
1512         case SIOCSIFFLAGS:
1513                 /*
1514                  * We should propagate selected flags to the parent,
1515                  * e.g., promiscuous mode.
1516                  */
1517                 if (TRUNK(ifv) != NULL)
1518                         error = vlan_setflags(ifp, 1);
1519                 break;
1520
1521         case SIOCADDMULTI:
1522         case SIOCDELMULTI:
1523                 /*
1524                  * If we don't have a parent, just remember the membership for
1525                  * when we do.
1526                  */
1527                 if (TRUNK(ifv) != NULL)
1528                         error = vlan_setmulti(ifp);
1529                 break;
1530
1531         default:
1532                 error = ether_ioctl(ifp, cmd, data);
1533         }
1534
1535         return (error);
1536 }