]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_vlan.c
This commit was generated by cvs2svn to compensate for changes in r149749,
[FreeBSD/FreeBSD.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  * $FreeBSD$
30  */
31
32 /*
33  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
34  * Might be extended some day to also handle IEEE 802.1p priority
35  * tagging.  This is sort of sneaky in the implementation, since
36  * we need to pretend to be enough of an Ethernet implementation
37  * to make arp work.  The way we do this is by telling everyone
38  * that we are an Ethernet, and then catch the packets that
39  * ether_output() left on our output queue when it calls
40  * if_start(), rewrite them for use by the real outgoing interface,
41  * and ask it to send them.
42  */
43
44 #include "opt_inet.h"
45
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/module.h>
51 #include <sys/queue.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
56
57 #include <net/bpf.h>
58 #include <net/ethernet.h>
59 #include <net/if.h>
60 #include <net/if_clone.h>
61 #include <net/if_arp.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 #include <net/if_vlan_var.h>
65
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
69 #endif
70
71 #define VLANNAME        "vlan"
72
73 struct vlan_mc_entry {
74         struct ether_addr               mc_addr;
75         SLIST_ENTRY(vlan_mc_entry)      mc_entries;
76 };
77
78 struct  ifvlan {
79         struct  ifnet *ifv_ifp;
80         struct  ifnet *ifv_p;   /* parent inteface of this vlan */
81         struct  ifv_linkmib {
82                 int     ifvm_parent;
83                 int     ifvm_encaplen;  /* encapsulation length */
84                 int     ifvm_mtufudge;  /* MTU fudged by this much */
85                 int     ifvm_mintu;     /* min transmission unit */
86                 u_int16_t ifvm_proto; /* encapsulation ethertype */
87                 u_int16_t ifvm_tag; /* tag to apply on packets leaving if */
88         }       ifv_mib;
89         SLIST_HEAD(__vlan_mchead, vlan_mc_entry)        vlan_mc_listhead;
90         LIST_ENTRY(ifvlan) ifv_list;
91         int     ifv_flags;
92 };
93 #define ifv_tag ifv_mib.ifvm_tag
94 #define ifv_encaplen    ifv_mib.ifvm_encaplen
95 #define ifv_mtufudge    ifv_mib.ifvm_mtufudge
96 #define ifv_mintu       ifv_mib.ifvm_mintu
97
98 #define IFVF_PROMISC    0x01            /* promiscuous mode enabled */
99
100 SYSCTL_DECL(_net_link);
101 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
102 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
103
104 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
105 static LIST_HEAD(, ifvlan) ifv_list;
106
107 /*
108  * Locking: one lock is used to guard both the ifv_list and modification
109  * to vlan data structures.  We are rather conservative here; probably
110  * more than necessary.
111  */
112 static struct mtx ifv_mtx;
113 #define VLAN_LOCK_INIT()        mtx_init(&ifv_mtx, VLANNAME, NULL, MTX_DEF)
114 #define VLAN_LOCK_DESTROY()     mtx_destroy(&ifv_mtx)
115 #define VLAN_LOCK_ASSERT()      mtx_assert(&ifv_mtx, MA_OWNED)
116 #define VLAN_LOCK()     mtx_lock(&ifv_mtx)
117 #define VLAN_UNLOCK()   mtx_unlock(&ifv_mtx)
118
119 static  void vlan_start(struct ifnet *ifp);
120 static  void vlan_ifinit(void *foo);
121 static  void vlan_input(struct ifnet *ifp, struct mbuf *m);
122 static  int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
123 static  int vlan_setmulti(struct ifnet *ifp);
124 static  int vlan_unconfig(struct ifnet *ifp);
125 static  int vlan_config(struct ifvlan *ifv, struct ifnet *p);
126 static  void vlan_link_state(struct ifnet *ifp, int link);
127 static  int vlan_set_promisc(struct ifnet *ifp);
128
129 static  struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
130     const char *, int *);
131 static  int vlan_clone_match(struct if_clone *, const char *);
132 static  int vlan_clone_create(struct if_clone *, char *, size_t);
133 static  int vlan_clone_destroy(struct if_clone *, struct ifnet *);
134
135 static  struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
136     IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
137
138 /*
139  * Program our multicast filter. What we're actually doing is
140  * programming the multicast filter of the parent. This has the
141  * side effect of causing the parent interface to receive multicast
142  * traffic that it doesn't really want, which ends up being discarded
143  * later by the upper protocol layers. Unfortunately, there's no way
144  * to avoid this: there really is only one physical interface.
145  *
146  * XXX: There is a possible race here if more than one thread is
147  *      modifying the multicast state of the vlan interface at the same time.
148  */
149 static int
150 vlan_setmulti(struct ifnet *ifp)
151 {
152         struct ifnet            *ifp_p;
153         struct ifmultiaddr      *ifma, *rifma = NULL;
154         struct ifvlan           *sc;
155         struct vlan_mc_entry    *mc = NULL;
156         struct sockaddr_dl      sdl;
157         int                     error;
158
159         /*VLAN_LOCK_ASSERT();*/
160
161         /* Find the parent. */
162         sc = ifp->if_softc;
163         ifp_p = sc->ifv_p;
164
165         /*
166          * If we don't have a parent, just remember the membership for
167          * when we do.
168          */
169         if (ifp_p == NULL)
170                 return (0);
171
172         bzero((char *)&sdl, sizeof(sdl));
173         sdl.sdl_len = sizeof(sdl);
174         sdl.sdl_family = AF_LINK;
175         sdl.sdl_index = ifp_p->if_index;
176         sdl.sdl_type = IFT_ETHER;
177         sdl.sdl_alen = ETHER_ADDR_LEN;
178
179         /* First, remove any existing filter entries. */
180         while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
181                 mc = SLIST_FIRST(&sc->vlan_mc_listhead);
182                 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
183                 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
184                 if (error)
185                         return (error);
186                 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
187                 free(mc, M_VLAN);
188         }
189
190         /* Now program new ones. */
191         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
192                 if (ifma->ifma_addr->sa_family != AF_LINK)
193                         continue;
194                 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
195                 if (mc == NULL)
196                         return (ENOMEM);
197                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
198                     (char *)&mc->mc_addr, ETHER_ADDR_LEN);
199                 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
200                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
201                     LLADDR(&sdl), ETHER_ADDR_LEN);
202                 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
203                 if (error)
204                         return (error);
205         }
206
207         return (0);
208 }
209
210 /*
211  * VLAN support can be loaded as a module.  The only place in the
212  * system that's intimately aware of this is ether_input.  We hook
213  * into this code through vlan_input_p which is defined there and
214  * set here.  Noone else in the system should be aware of this so
215  * we use an explicit reference here.
216  *
217  * NB: Noone should ever need to check if vlan_input_p is null or
218  *     not.  This is because interfaces have a count of the number
219  *     of active vlans (if_nvlans) and this should never be bumped
220  *     except by vlan_config--which is in this module so therefore
221  *     the module must be loaded and vlan_input_p must be non-NULL.
222  */
223 extern  void (*vlan_input_p)(struct ifnet *, struct mbuf *);
224
225 /* For if_link_state_change() eyes only... */
226 extern  void (*vlan_link_state_p)(struct ifnet *, int);
227
228 static int
229 vlan_modevent(module_t mod, int type, void *data)
230 {
231
232         switch (type) {
233         case MOD_LOAD:
234                 LIST_INIT(&ifv_list);
235                 VLAN_LOCK_INIT();
236                 vlan_input_p = vlan_input;
237                 vlan_link_state_p = vlan_link_state;
238                 if_clone_attach(&vlan_cloner);
239                 break;
240         case MOD_UNLOAD:
241                 if_clone_detach(&vlan_cloner);
242                 vlan_input_p = NULL;
243                 vlan_link_state_p = NULL;
244                 while (!LIST_EMPTY(&ifv_list))
245                         vlan_clone_destroy(&vlan_cloner,
246                             LIST_FIRST(&ifv_list)->ifv_ifp);
247                 VLAN_LOCK_DESTROY();
248                 break;
249         default:
250                 return (EOPNOTSUPP);
251         }
252         return (0);
253 }
254
255 static moduledata_t vlan_mod = {
256         "if_vlan",
257         vlan_modevent,
258         0
259 };
260
261 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
262 MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
263
264 static struct ifnet *
265 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
266 {
267         const char *cp;
268         struct ifnet *ifp;
269         int t = 0;
270
271         /* Check for <etherif>.<vlan> style interface names. */
272         IFNET_RLOCK();
273         TAILQ_FOREACH(ifp, &ifnet, if_link) {
274                 if (ifp->if_type != IFT_ETHER)
275                         continue;
276                 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
277                         continue;
278                 cp = name + strlen(ifp->if_xname);
279                 if (*cp != '.')
280                         continue;
281                 for(; *cp != '\0'; cp++) {
282                         if (*cp < '0' || *cp > '9')
283                                 continue;
284                         t = (t * 10) + (*cp - '0');
285                 }
286                 if (tag != NULL)
287                         *tag = t;
288                 break;
289         }
290         IFNET_RUNLOCK();
291
292         return (ifp);
293 }
294
295 static int
296 vlan_clone_match(struct if_clone *ifc, const char *name)
297 {
298         const char *cp;
299
300         if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
301                 return (1);
302
303         if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
304                 return (0);
305         for (cp = name + 4; *cp != '\0'; cp++) {
306                 if (*cp < '0' || *cp > '9')
307                         return (0);
308         }
309
310         return (1);
311 }
312
313 static int
314 vlan_clone_create(struct if_clone *ifc, char *name, size_t len)
315 {
316         char *dp;
317         int wildcard;
318         int unit;
319         int error;
320         int tag;
321         int ethertag;
322         struct ifvlan *ifv;
323         struct ifnet *ifp;
324         struct ifnet *p;
325         u_char eaddr[6] = {0,0,0,0,0,0};
326
327         if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
328                 ethertag = 1;
329                 unit = -1;
330                 wildcard = 0;
331
332                 /*
333                  * Don't let the caller set up a VLAN tag with
334                  * anything except VLID bits.
335                  */
336                 if (tag & ~EVL_VLID_MASK)
337                         return (EINVAL);
338         } else {
339                 ethertag = 0;
340
341                 error = ifc_name2unit(name, &unit);
342                 if (error != 0)
343                         return (error);
344
345                 wildcard = (unit < 0);
346         }
347
348         error = ifc_alloc_unit(ifc, &unit);
349         if (error != 0)
350                 return (error);
351
352         /* In the wildcard case, we need to update the name. */
353         if (wildcard) {
354                 for (dp = name; *dp != '\0'; dp++);
355                 if (snprintf(dp, len - (dp-name), "%d", unit) >
356                     len - (dp-name) - 1) {
357                         panic("%s: interface name too long", __func__);
358                 }
359         }
360
361         ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
362         ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
363         if (ifp == NULL) {
364                 ifc_free_unit(ifc, unit);
365                 free(ifv, M_VLAN);
366                 return (ENOSPC);
367         }
368         SLIST_INIT(&ifv->vlan_mc_listhead);
369
370         ifp->if_softc = ifv;
371         /*
372          * Set the name manually rather than using if_initname because
373          * we don't conform to the default naming convention for interfaces.
374          */
375         strlcpy(ifp->if_xname, name, IFNAMSIZ);
376         ifp->if_dname = ifc->ifc_name;
377         ifp->if_dunit = unit;
378         /* NB: flags are not set here */
379         ifp->if_linkmib = &ifv->ifv_mib;
380         ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
381         /* NB: mtu is not set here */
382
383         ifp->if_init = vlan_ifinit;
384         ifp->if_start = vlan_start;
385         ifp->if_ioctl = vlan_ioctl;
386         ifp->if_snd.ifq_maxlen = ifqmaxlen;
387         ether_ifattach(ifp, eaddr);
388         /* Now undo some of the damage... */
389         ifp->if_baudrate = 0;
390         ifp->if_type = IFT_L2VLAN;
391         ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
392
393         VLAN_LOCK();
394         LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
395         VLAN_UNLOCK();
396
397         if (ethertag) {
398                 VLAN_LOCK();
399                 error = vlan_config(ifv, p);
400                 if (error != 0) {
401                         /*
402                          * Since we've partialy failed, we need to back
403                          * out all the way, otherwise userland could get
404                          * confused.  Thus, we destroy the interface.
405                          */
406                         LIST_REMOVE(ifv, ifv_list);
407                         vlan_unconfig(ifp);
408                         VLAN_UNLOCK();
409                         ether_ifdetach(ifp);
410                         if_free_type(ifp, IFT_ETHER);
411                         free(ifv, M_VLAN);
412
413                         return (error);
414                 }
415                 ifv->ifv_tag = tag;
416                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
417                 VLAN_UNLOCK();
418
419                 /* Update promiscuous mode, if necessary. */
420                 vlan_set_promisc(ifp);
421         }
422
423         return (0);
424 }
425
426 static int
427 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
428 {
429         int unit;
430         struct ifvlan *ifv = ifp->if_softc;
431
432         unit = ifp->if_dunit;
433
434         VLAN_LOCK();
435         LIST_REMOVE(ifv, ifv_list);
436         vlan_unconfig(ifp);
437         VLAN_UNLOCK();
438
439         ether_ifdetach(ifp);
440         if_free_type(ifp, IFT_ETHER);
441
442         free(ifv, M_VLAN);
443
444         ifc_free_unit(ifc, unit);
445
446         return (0);
447 }
448
449 /*
450  * The ifp->if_init entry point for vlan(4) is a no-op.
451  */
452 static void
453 vlan_ifinit(void *foo)
454 {
455
456 }
457
458 static void
459 vlan_start(struct ifnet *ifp)
460 {
461         struct ifvlan *ifv;
462         struct ifnet *p;
463         struct ether_vlan_header *evl;
464         struct mbuf *m;
465         int error;
466
467         ifv = ifp->if_softc;
468         p = ifv->ifv_p;
469
470         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
471         for (;;) {
472                 IF_DEQUEUE(&ifp->if_snd, m);
473                 if (m == 0)
474                         break;
475                 BPF_MTAP(ifp, m);
476
477                 /*
478                  * Do not run parent's if_start() if the parent is not up,
479                  * or parent's driver will cause a system crash.
480                  */
481                 if (!((p->if_flags & IFF_UP) &&
482                     (p->if_drv_flags & IFF_DRV_RUNNING))) {
483                         m_freem(m);
484                         ifp->if_collisions++;
485                         continue;
486                 }
487
488                 /*
489                  * If underlying interface can do VLAN tag insertion itself,
490                  * just pass the packet along. However, we need some way to
491                  * tell the interface where the packet came from so that it
492                  * knows how to find the VLAN tag to use, so we attach a
493                  * packet tag that holds it.
494                  */
495                 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
496                         struct m_tag *mtag = m_tag_alloc(MTAG_VLAN,
497                                                          MTAG_VLAN_TAG,
498                                                          sizeof(u_int),
499                                                          M_NOWAIT);
500                         if (mtag == NULL) {
501                                 ifp->if_oerrors++;
502                                 m_freem(m);
503                                 continue;
504                         }
505                         VLAN_TAG_VALUE(mtag) = ifv->ifv_tag;
506                         m_tag_prepend(m, mtag);
507                         m->m_flags |= M_VLANTAG;
508                 } else {
509                         M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
510                         if (m == NULL) {
511                                 if_printf(ifp,
512                                     "unable to prepend VLAN header\n");
513                                 ifp->if_oerrors++;
514                                 continue;
515                         }
516                         /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
517
518                         if (m->m_len < sizeof(*evl)) {
519                                 m = m_pullup(m, sizeof(*evl));
520                                 if (m == NULL) {
521                                         if_printf(ifp,
522                                             "cannot pullup VLAN header\n");
523                                         ifp->if_oerrors++;
524                                         continue;
525                                 }
526                         }
527
528                         /*
529                          * Transform the Ethernet header into an Ethernet header
530                          * with 802.1Q encapsulation.
531                          */
532                         bcopy(mtod(m, char *) + ifv->ifv_encaplen,
533                               mtod(m, char *), ETHER_HDR_LEN);
534                         evl = mtod(m, struct ether_vlan_header *);
535                         evl->evl_proto = evl->evl_encap_proto;
536                         evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
537                         evl->evl_tag = htons(ifv->ifv_tag);
538 #ifdef DEBUG
539                         printf("vlan_start: %*D\n", (int)sizeof(*evl),
540                             (unsigned char *)evl, ":");
541 #endif
542                 }
543
544                 /*
545                  * Send it, precisely as ether_output() would have.
546                  * We are already running at splimp.
547                  */
548                 IFQ_HANDOFF(p, m, error);
549                 if (!error)
550                         ifp->if_opackets++;
551                 else
552                         ifp->if_oerrors++;
553         }
554         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
555 }
556
557 static void
558 vlan_input(struct ifnet *ifp, struct mbuf *m)
559 {
560         struct ether_vlan_header *evl;
561         struct ifvlan *ifv;
562         struct m_tag *mtag;
563         u_int tag;
564
565         mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
566         if (mtag != NULL) {
567                 /*
568                  * Packet is tagged, m contains a normal
569                  * Ethernet frame; the tag is stored out-of-band.
570                  */
571                 tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag));
572                 m_tag_delete(m, mtag);
573                 m->m_flags &= ~M_VLANTAG;
574         } else {
575                 switch (ifp->if_type) {
576                 case IFT_ETHER:
577                         if (m->m_len < sizeof(*evl) &&
578                             (m = m_pullup(m, sizeof(*evl))) == NULL) {
579                                 if_printf(ifp, "cannot pullup VLAN header\n");
580                                 return;
581                         }
582                         evl = mtod(m, struct ether_vlan_header *);
583                         KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN,
584                                 ("vlan_input: bad encapsulated protocols (%u)",
585                                  ntohs(evl->evl_encap_proto)));
586
587                         tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
588
589                         /*
590                          * Restore the original ethertype.  We'll remove
591                          * the encapsulation after we've found the vlan
592                          * interface corresponding to the tag.
593                          */
594                         evl->evl_encap_proto = evl->evl_proto;
595                         break;
596                 default:
597                         tag = (u_int) -1;
598 #ifdef DIAGNOSTIC
599                         panic("vlan_input: unsupported if type %u",
600                             ifp->if_type);
601 #endif
602                         break;
603                 }
604         }
605
606         VLAN_LOCK();
607         LIST_FOREACH(ifv, &ifv_list, ifv_list)
608                 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
609                         break;
610
611         if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
612                 VLAN_UNLOCK();
613                 m_freem(m);
614                 ifp->if_noproto++;
615 #ifdef DEBUG
616                 printf("vlan_input: tag %d, no interface\n", tag);
617 #endif
618                 return;
619         }
620         VLAN_UNLOCK();          /* XXX extend below? */
621 #ifdef DEBUG
622         printf("vlan_input: tag %d, parent %s\n", tag, ifv->ifv_p->if_xname);
623 #endif
624
625         if (mtag == NULL) {
626                 /*
627                  * Packet had an in-line encapsulation header;
628                  * remove it.  The original header has already
629                  * been fixed up above.
630                  */
631                 bcopy(mtod(m, caddr_t),
632                       mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
633                       ETHER_HDR_LEN);
634                 m_adj(m, ETHER_VLAN_ENCAP_LEN);
635         }
636
637         m->m_pkthdr.rcvif = ifv->ifv_ifp;
638         ifv->ifv_ifp->if_ipackets++;
639
640         /* Pass it back through the parent's input routine. */
641         (*ifp->if_input)(ifv->ifv_ifp, m);
642 }
643
644 static int
645 vlan_config(struct ifvlan *ifv, struct ifnet *p)
646 {
647         struct ifaddr *ifa1, *ifa2;
648         struct sockaddr_dl *sdl1, *sdl2;
649
650         VLAN_LOCK_ASSERT();
651
652         if (p->if_data.ifi_type != IFT_ETHER)
653                 return (EPROTONOSUPPORT);
654         if (ifv->ifv_p)
655                 return (EBUSY);
656
657         ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
658         ifv->ifv_mintu = ETHERMIN;
659         ifv->ifv_flags = 0;
660
661         /*
662          * The active VLAN counter on the parent is used
663          * at various places to see if there is a vlan(4)
664          * attached to this physical interface.
665          */
666         p->if_nvlans++;
667
668         /*
669          * If the parent supports the VLAN_MTU capability,
670          * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
671          * use it.
672          */
673         if (p->if_capenable & IFCAP_VLAN_MTU) {
674                 /*
675                  * No need to fudge the MTU since the parent can
676                  * handle extended frames.
677                  */
678                 ifv->ifv_mtufudge = 0;
679         } else {
680                 /*
681                  * Fudge the MTU by the encapsulation size.  This
682                  * makes us incompatible with strictly compliant
683                  * 802.1Q implementations, but allows us to use
684                  * the feature with other NetBSD implementations,
685                  * which might still be useful.
686                  */
687                 ifv->ifv_mtufudge = ifv->ifv_encaplen;
688         }
689
690         ifv->ifv_p = p;
691         ifv->ifv_ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
692         /*
693          * Copy only a selected subset of flags from the parent.
694          * Other flags are none of our business.
695          */
696         ifv->ifv_ifp->if_flags = (p->if_flags &
697             (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT));
698         ifv->ifv_ifp->if_link_state = p->if_link_state;
699
700 #if 0
701         /*
702          * Not ready yet.  We need notification from the parent
703          * when hw checksumming flags in its if_capenable change.
704          * Flags set in if_capabilities only are useless.
705          */
706         /*
707          * If the parent interface can do hardware-assisted
708          * VLAN encapsulation, then propagate its hardware-
709          * assisted checksumming flags.
710          */
711         if (p->if_capabilities & IFCAP_VLAN_HWTAGGING)
712                 ifv->ifv_ifpif_capabilities |= p->if_capabilities & IFCAP_HWCSUM;
713 #endif
714
715         /*
716          * Set up our ``Ethernet address'' to reflect the underlying
717          * physical interface's.
718          */
719         ifa1 = ifaddr_byindex(ifv->ifv_ifp->if_index);
720         ifa2 = ifaddr_byindex(p->if_index);
721         sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
722         sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
723         sdl1->sdl_type = IFT_ETHER;
724         sdl1->sdl_alen = ETHER_ADDR_LEN;
725         bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
726         bcopy(LLADDR(sdl2), IFP2ENADDR(ifv->ifv_ifp), ETHER_ADDR_LEN);
727
728         /*
729          * Configure multicast addresses that may already be
730          * joined on the vlan device.
731          */
732         (void)vlan_setmulti(ifv->ifv_ifp); /* XXX: VLAN lock held */
733
734         return (0);
735 }
736
737 static int
738 vlan_unconfig(struct ifnet *ifp)
739 {
740         struct ifaddr *ifa;
741         struct sockaddr_dl *sdl;
742         struct vlan_mc_entry *mc;
743         struct ifvlan *ifv;
744         struct ifnet *p;
745         int error;
746
747         VLAN_LOCK_ASSERT();
748
749         ifv = ifp->if_softc;
750         p = ifv->ifv_p;
751
752         if (p) {
753                 struct sockaddr_dl sdl;
754
755                 /*
756                  * Since the interface is being unconfigured, we need to
757                  * empty the list of multicast groups that we may have joined
758                  * while we were alive from the parent's list.
759                  */
760                 bzero((char *)&sdl, sizeof(sdl));
761                 sdl.sdl_len = sizeof(sdl);
762                 sdl.sdl_family = AF_LINK;
763                 sdl.sdl_index = p->if_index;
764                 sdl.sdl_type = IFT_ETHER;
765                 sdl.sdl_alen = ETHER_ADDR_LEN;
766
767                 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
768                         mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
769                         bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
770                             ETHER_ADDR_LEN);
771                         error = if_delmulti(p, (struct sockaddr *)&sdl);
772                         if (error)
773                                 return (error);
774                         SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
775                         free(mc, M_VLAN);
776                 }
777
778                 p->if_nvlans--;
779         }
780
781         /* Disconnect from parent. */
782         ifv->ifv_p = NULL;
783         ifv->ifv_ifp->if_mtu = ETHERMTU;                /* XXX why not 0? */
784         ifv->ifv_flags = 0;
785         ifv->ifv_ifp->if_link_state = LINK_STATE_UNKNOWN;
786
787         /* Clear our MAC address. */
788         ifa = ifaddr_byindex(ifv->ifv_ifp->if_index);
789         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
790         sdl->sdl_type = IFT_ETHER;
791         sdl->sdl_alen = ETHER_ADDR_LEN;
792         bzero(LLADDR(sdl), ETHER_ADDR_LEN);
793         bzero(IFP2ENADDR(ifv->ifv_ifp), ETHER_ADDR_LEN);
794
795         return (0);
796 }
797
798 static int
799 vlan_set_promisc(struct ifnet *ifp)
800 {
801         struct ifvlan *ifv = ifp->if_softc;
802         int error = 0;
803
804         if ((ifp->if_flags & IFF_PROMISC) != 0) {
805                 if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
806                         error = ifpromisc(ifv->ifv_p, 1);
807                         if (error == 0)
808                                 ifv->ifv_flags |= IFVF_PROMISC;
809                 }
810         } else {
811                 if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
812                         error = ifpromisc(ifv->ifv_p, 0);
813                         if (error == 0)
814                                 ifv->ifv_flags &= ~IFVF_PROMISC;
815                 }
816         }
817
818         return (error);
819 }
820
821 /* Inform all vlans that their parent has changed link state */
822 static void
823 vlan_link_state(struct ifnet *ifp, int link)
824 {
825         struct ifvlan *ifv;
826
827         VLAN_LOCK();
828         LIST_FOREACH(ifv, &ifv_list, ifv_list) {
829                 if (ifv->ifv_p == ifp)
830                         if_link_state_change(ifv->ifv_ifp,
831                             ifv->ifv_p->if_link_state);
832         }
833         VLAN_UNLOCK();
834 }
835
836 static int
837 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
838 {
839         struct ifaddr *ifa;
840         struct ifnet *p;
841         struct ifreq *ifr;
842         struct ifvlan *ifv;
843         struct vlanreq vlr;
844         int error = 0;
845
846         ifr = (struct ifreq *)data;
847         ifa = (struct ifaddr *)data;
848         ifv = ifp->if_softc;
849
850         switch (cmd) {
851         case SIOCSIFADDR:
852                 ifp->if_flags |= IFF_UP;
853
854                 switch (ifa->ifa_addr->sa_family) {
855 #ifdef INET
856                 case AF_INET:
857                         arp_ifinit(ifv->ifv_ifp, ifa);
858                         break;
859 #endif
860                 default:
861                         break;
862                 }
863                 break;
864
865         case SIOCGIFADDR:
866                 {
867                         struct sockaddr *sa;
868
869                         sa = (struct sockaddr *) &ifr->ifr_data;
870                         bcopy(IFP2ENADDR(ifp), (caddr_t)sa->sa_data,
871                             ETHER_ADDR_LEN);
872                 }
873                 break;
874
875         case SIOCGIFMEDIA:
876                 VLAN_LOCK();
877                 if (ifv->ifv_p != NULL) {
878                         error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
879                                         SIOCGIFMEDIA, data);
880                         VLAN_UNLOCK();
881                         /* Limit the result to the parent's current config. */
882                         if (error == 0) {
883                                 struct ifmediareq *ifmr;
884
885                                 ifmr = (struct ifmediareq *)data;
886                                 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
887                                         ifmr->ifm_count = 1;
888                                         error = copyout(&ifmr->ifm_current,
889                                                 ifmr->ifm_ulist,
890                                                 sizeof(int));
891                                 }
892                         }
893                 } else {
894                         VLAN_UNLOCK();
895                         error = EINVAL;
896                 }
897                 break;
898
899         case SIOCSIFMEDIA:
900                 error = EINVAL;
901                 break;
902
903         case SIOCSIFMTU:
904                 /*
905                  * Set the interface MTU.
906                  */
907                 VLAN_LOCK();
908                 if (ifv->ifv_p != NULL) {
909                         if (ifr->ifr_mtu >
910                              (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
911                             ifr->ifr_mtu <
912                              (ifv->ifv_mintu - ifv->ifv_mtufudge))
913                                 error = EINVAL;
914                         else
915                                 ifp->if_mtu = ifr->ifr_mtu;
916                 } else
917                         error = EINVAL;
918                 VLAN_UNLOCK();
919                 break;
920
921         case SIOCSETVLAN:
922                 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
923                 if (error)
924                         break;
925                 if (vlr.vlr_parent[0] == '\0') {
926                         VLAN_LOCK();
927                         vlan_unconfig(ifp);
928                         if (ifp->if_flags & IFF_UP)
929                                 if_down(ifp);
930                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
931                         VLAN_UNLOCK();
932                         break;
933                 }
934                 p = ifunit(vlr.vlr_parent);
935                 if (p == 0) {
936                         error = ENOENT;
937                         break;
938                 }
939                 /*
940                  * Don't let the caller set up a VLAN tag with
941                  * anything except VLID bits.
942                  */
943                 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
944                         error = EINVAL;
945                         break;
946                 }
947                 VLAN_LOCK();
948                 error = vlan_config(ifv, p);
949                 if (error) {
950                         VLAN_UNLOCK();
951                         break;
952                 }
953                 ifv->ifv_tag = vlr.vlr_tag;
954                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
955                 VLAN_UNLOCK();
956
957                 /* Update promiscuous mode, if necessary. */
958                 vlan_set_promisc(ifp);
959                 break;
960
961         case SIOCGETVLAN:
962                 bzero(&vlr, sizeof(vlr));
963                 VLAN_LOCK();
964                 if (ifv->ifv_p) {
965                         strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
966                             sizeof(vlr.vlr_parent));
967                         vlr.vlr_tag = ifv->ifv_tag;
968                 }
969                 VLAN_UNLOCK();
970                 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
971                 break;
972                 
973         case SIOCSIFFLAGS:
974                 /*
975                  * For promiscuous mode, we enable promiscuous mode on
976                  * the parent if we need promiscuous on the VLAN interface.
977                  */
978                 if (ifv->ifv_p != NULL)
979                         error = vlan_set_promisc(ifp);
980                 break;
981
982         case SIOCADDMULTI:
983         case SIOCDELMULTI:
984                 /*VLAN_LOCK();*/
985                 error = vlan_setmulti(ifp);
986                 /*VLAN_UNLOCK();*/
987                 break;
988         default:
989                 error = EINVAL;
990         }
991
992         return (error);
993 }