kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / net / vlan / 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: src/sys/net/if_vlan.c,v 1.15.2.13 2003/02/14 22:25:58 fenner Exp $
30  * $DragonFly: src/sys/net/vlan/if_vlan.c,v 1.4 2003/08/07 21:17:30 dillon Exp $
31  */
32
33 /*
34  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
35  * Might be extended some day to also handle IEEE 802.1p priority
36  * tagging.  This is sort of sneaky in the implementation, since
37  * we need to pretend to be enough of an Ethernet implementation
38  * to make arp work.  The way we do this is by telling everyone
39  * that we are an Ethernet, and then catch the packets that
40  * ether_output() left on our output queue queue when it calls
41  * if_start(), rewrite them for use by the real outgoing interface,
42  * and ask it to send them.
43  *
44  *
45  * XXX It's incorrect to assume that we must always kludge up
46  * headers on the physical device's behalf: some devices support
47  * VLAN tag insertion and extraction in firmware. For these cases,
48  * one can change the behavior of the vlan interface by setting
49  * the LINK0 flag on it (that is setting the vlan interface's LINK0
50  * flag, _not_ the parent's LINK0 flag; we try to leave the parent
51  * alone). If the interface has the LINK0 flag set, then it will
52  * not modify the ethernet header on output, because the parent
53  * can do that for itself. On input, the parent can call vlan_input_tag()
54  * directly in order to supply us with an incoming mbuf and the vlan
55  * tag value that goes with it.
56  */
57
58 #ifndef NVLAN
59 #include "use_vlan.h"
60 #endif
61 #include "opt_inet.h"
62
63 #include <sys/param.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
66 #include <sys/mbuf.h>
67 #include <sys/module.h>
68 #include <sys/queue.h>
69 #include <sys/socket.h>
70 #include <sys/sockio.h>
71 #include <sys/sysctl.h>
72 #include <sys/systm.h>
73 #include <machine/bus.h>        /* XXX: Shouldn't really be required! */
74 #include <sys/rman.h>
75
76 #include <net/bpf.h>
77 #include <net/ethernet.h>
78 #include <net/if.h>
79 #include <net/if_arp.h>
80 #include <net/if_dl.h>
81 #include <net/if_types.h>
82 #include "if_vlan_var.h"
83
84 #ifdef INET
85 #include <netinet/in.h>
86 #include <netinet/if_ether.h>
87 #endif
88
89 #define VLANNAME        "vlan"
90 #define VLAN_MAXUNIT    0x7fff  /* ifp->if_unit is only 15 bits */
91
92 SYSCTL_DECL(_net_link);
93 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
94 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
95
96 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
97 static struct rman vlanunits[1];
98 static LIST_HEAD(, ifvlan) ifv_list;
99
100 static  int vlan_clone_create(struct if_clone *, int *);
101 static  void vlan_clone_destroy(struct ifnet *);
102 static  void vlan_start(struct ifnet *ifp);
103 static  void vlan_ifinit(void *foo);
104 static  int vlan_input(struct ether_header *eh, struct mbuf *m);
105 static  int vlan_input_tag(struct ether_header *eh, struct mbuf *m,
106                 u_int16_t t);
107 static  int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
108 static  int vlan_setmulti(struct ifnet *ifp);
109 static  int vlan_unconfig(struct ifnet *ifp);
110 static  int vlan_config(struct ifvlan *ifv, struct ifnet *p);
111
112 struct if_clone vlan_cloner =
113     IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
114
115 /*
116  * Program our multicast filter. What we're actually doing is
117  * programming the multicast filter of the parent. This has the
118  * side effect of causing the parent interface to receive multicast
119  * traffic that it doesn't really want, which ends up being discarded
120  * later by the upper protocol layers. Unfortunately, there's no way
121  * to avoid this: there really is only one physical interface.
122  */
123 static int
124 vlan_setmulti(struct ifnet *ifp)
125 {
126         struct ifnet            *ifp_p;
127         struct ifmultiaddr      *ifma, *rifma = NULL;
128         struct ifvlan           *sc;
129         struct vlan_mc_entry    *mc = NULL;
130         struct sockaddr_dl      sdl;
131         int                     error;
132
133         /* Find the parent. */
134         sc = ifp->if_softc;
135         ifp_p = sc->ifv_p;
136
137         /*
138          * If we don't have a parent, just remember the membership for
139          * when we do.
140          */
141         if (ifp_p == NULL)
142                 return(0);
143
144         bzero((char *)&sdl, sizeof sdl);
145         sdl.sdl_len = sizeof sdl;
146         sdl.sdl_family = AF_LINK;
147         sdl.sdl_index = ifp_p->if_index;
148         sdl.sdl_type = IFT_ETHER;
149         sdl.sdl_alen = ETHER_ADDR_LEN;
150
151         /* First, remove any existing filter entries. */
152         while(SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
153                 mc = SLIST_FIRST(&sc->vlan_mc_listhead);
154                 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
155                 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
156                 if (error)
157                         return(error);
158                 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
159                 free(mc, M_VLAN);
160         }
161
162         /* Now program new ones. */
163         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
164                 if (ifma->ifma_addr->sa_family != AF_LINK)
165                         continue;
166                 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
167                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
168                     (char *)&mc->mc_addr, ETHER_ADDR_LEN);
169                 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
170                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
171                     LLADDR(&sdl), ETHER_ADDR_LEN);
172                 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
173                 if (error)
174                         return(error);
175         }
176
177         return(0);
178 }
179
180 static int
181 vlan_modevent(module_t mod, int type, void *data) 
182
183         int i;
184         int err;
185
186         switch (type) { 
187         case MOD_LOAD: 
188                 vlanunits->rm_type = RMAN_ARRAY;
189                 vlanunits->rm_descr = "configurable if_vlan units";
190                 err = rman_init(vlanunits);
191                 if (err != 0)
192                         return (err);
193                 err = rman_manage_region(vlanunits, 0, VLAN_MAXUNIT);
194                 if (err != 0) {
195                         printf("%s: vlanunits: rman_manage_region: Failed %d\n",
196                             VLANNAME, err);
197                         rman_fini(vlanunits);
198                         return (err);
199                 }
200                 LIST_INIT(&ifv_list);
201                 vlan_input_p = vlan_input;
202                 vlan_input_tag_p = vlan_input_tag;
203                 if_clone_attach(&vlan_cloner);
204                 for(i = 0; i < NVLAN; i ++) {
205                         err = vlan_clone_create(&vlan_cloner, &i);
206                         KASSERT(err == 0,
207                             ("Unexpected error creating initial VLANs"));
208                 }
209                 break; 
210         case MOD_UNLOAD: 
211                 if_clone_detach(&vlan_cloner);
212                 vlan_input_p = NULL;
213                 vlan_input_tag_p = NULL;
214                 while (!LIST_EMPTY(&ifv_list))
215                         vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
216                 err = rman_fini(vlanunits);
217                 if (err != 0)
218                          return (err);
219                 break;
220         } 
221         return 0; 
222
223
224 static moduledata_t vlan_mod = { 
225         "if_vlan", 
226         vlan_modevent, 
227         0
228 }; 
229
230 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
231
232 static int
233 vlan_clone_create(struct if_clone *ifc, int *unit)
234 {
235         struct resource *r;
236         struct ifvlan *ifv;
237         struct ifnet *ifp;
238         int s;
239
240         if (*unit > VLAN_MAXUNIT)
241                 return (ENXIO);
242
243         if (*unit < 0) {
244                 r  = rman_reserve_resource(vlanunits, 0, VLAN_MAXUNIT, 1,
245                     RF_ALLOCATED | RF_ACTIVE, NULL);
246                 if (r == NULL)
247                         return (ENOSPC);
248                 *unit = rman_get_start(r);
249         } else {
250                 r  = rman_reserve_resource(vlanunits, *unit, *unit, 1,
251                     RF_ALLOCATED | RF_ACTIVE, NULL);
252                 if (r == NULL)
253                         return (EEXIST);
254         }
255
256         ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK);
257         memset(ifv, 0, sizeof(struct ifvlan));
258         ifp = &ifv->ifv_if;
259         SLIST_INIT(&ifv->vlan_mc_listhead);
260
261         s = splnet();
262         LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
263         splx(s);
264
265         ifp->if_softc = ifv;
266         ifp->if_name = "vlan";
267         ifp->if_unit = *unit;
268         ifv->r_unit = r;
269         /* NB: flags are not set here */
270         ifp->if_linkmib = &ifv->ifv_mib;
271         ifp->if_linkmiblen = sizeof ifv->ifv_mib;
272         /* NB: mtu is not set here */
273
274         ifp->if_init = vlan_ifinit;
275         ifp->if_start = vlan_start;
276         ifp->if_ioctl = vlan_ioctl;
277         ifp->if_output = ether_output;
278         ifp->if_snd.ifq_maxlen = ifqmaxlen;
279         ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
280         /* Now undo some of the damage... */
281         ifp->if_data.ifi_type = IFT_L2VLAN;
282         ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
283
284         return (0);
285 }
286
287 static void
288 vlan_clone_destroy(struct ifnet *ifp)
289 {
290         struct ifvlan *ifv = ifp->if_softc;
291         int s;
292         int err;
293
294         s = splnet();
295         LIST_REMOVE(ifv, ifv_list);
296         vlan_unconfig(ifp);
297         splx(s);
298
299         ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
300
301         err = rman_release_resource(ifv->r_unit);
302         KASSERT(err == 0, ("Unexpected error freeing resource"));
303         free(ifv, M_VLAN);
304 }
305
306 static void
307 vlan_ifinit(void *foo)
308 {
309         return;
310 }
311
312 static void
313 vlan_start(struct ifnet *ifp)
314 {
315         struct ifvlan *ifv;
316         struct ifnet *p;
317         struct ether_vlan_header *evl;
318         struct mbuf *m;
319
320         ifv = ifp->if_softc;
321         p = ifv->ifv_p;
322
323         ifp->if_flags |= IFF_OACTIVE;
324         for (;;) {
325                 IF_DEQUEUE(&ifp->if_snd, m);
326                 if (m == 0)
327                         break;
328                 if (ifp->if_bpf)
329                         bpf_mtap(ifp, m);
330
331                 /*
332                  * Do not run parent's if_start() if the parent is not up,
333                  * or parent's driver will cause a system crash.
334                  */
335                 if ((p->if_flags & (IFF_UP | IFF_RUNNING)) !=
336                                         (IFF_UP | IFF_RUNNING)) {
337                         m_freem(m);
338                         ifp->if_data.ifi_collisions++;
339                         continue;
340                 }
341
342                 /*
343                  * If the LINK0 flag is set, it means the underlying interface
344                  * can do VLAN tag insertion itself and doesn't require us to
345                  * create a special header for it. In this case, we just pass
346                  * the packet along. However, we need some way to tell the
347                  * interface where the packet came from so that it knows how
348                  * to find the VLAN tag to use, so we set the rcvif in the
349                  * mbuf header to our ifnet.
350                  *
351                  * Note: we also set the M_PROTO1 flag in the mbuf to let
352                  * the parent driver know that the rcvif pointer is really
353                  * valid. We need to do this because sometimes mbufs will
354                  * be allocated by other parts of the system that contain
355                  * garbage in the rcvif pointer. Using the M_PROTO1 flag
356                  * lets the driver perform a proper sanity check and avoid
357                  * following potentially bogus rcvif pointers off into
358                  * never-never land.
359                  */
360                 if (ifp->if_flags & IFF_LINK0) {
361                         m->m_pkthdr.rcvif = ifp;
362                         m->m_flags |= M_PROTO1;
363                 } else {
364                         M_PREPEND(m, EVL_ENCAPLEN, M_DONTWAIT);
365                         if (m == NULL) {
366                                 printf("vlan%d: M_PREPEND failed", ifp->if_unit);
367                                 ifp->if_ierrors++;
368                                 continue;
369                         }
370                         /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
371
372                         m = m_pullup(m, ETHER_HDR_LEN + EVL_ENCAPLEN);
373                         if (m == NULL) {
374                                 printf("vlan%d: m_pullup failed", ifp->if_unit);
375                                 ifp->if_ierrors++;
376                                 continue;
377                         }
378
379                         /*
380                          * Transform the Ethernet header into an Ethernet header
381                          * with 802.1Q encapsulation.
382                          */
383                         bcopy(mtod(m, char *) + EVL_ENCAPLEN, mtod(m, char *),
384                               sizeof(struct ether_header));
385                         evl = mtod(m, struct ether_vlan_header *);
386                         evl->evl_proto = evl->evl_encap_proto;
387                         evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
388                         evl->evl_tag = htons(ifv->ifv_tag);
389 #ifdef DEBUG
390                         printf("vlan_start: %*D\n", sizeof *evl,
391                             (unsigned char *)evl, ":");
392 #endif
393                 }
394
395                 /*
396                  * Send it, precisely as ether_output() would have.
397                  * We are already running at splimp.
398                  */
399                 if (IF_QFULL(&p->if_snd)) {
400                         IF_DROP(&p->if_snd);
401                                 /* XXX stats */
402                         ifp->if_oerrors++;
403                         m_freem(m);
404                         continue;
405                 }
406                 IF_ENQUEUE(&p->if_snd, m);
407                 ifp->if_opackets++;
408                 p->if_obytes += m->m_pkthdr.len;
409                 if (m->m_flags & M_MCAST)
410                         p->if_omcasts++;
411                 if ((p->if_flags & IFF_OACTIVE) == 0)
412                         p->if_start(p);
413         }
414         ifp->if_flags &= ~IFF_OACTIVE;
415
416         return;
417 }
418
419 static int
420 vlan_input_tag(struct ether_header *eh, struct mbuf *m, u_int16_t t)
421 {
422         struct ifvlan *ifv;
423
424         /*
425          * Fake up a header and send the packet to the physical interface's
426          * bpf tap if active.
427          */
428         if (m->m_pkthdr.rcvif->if_bpf != NULL) {
429                 struct m_hdr mh;
430                 struct ether_vlan_header evh;
431
432                 bcopy(eh, &evh, 2*ETHER_ADDR_LEN);
433                 evh.evl_encap_proto = htons(ETHERTYPE_VLAN);
434                 evh.evl_tag = htons(t);
435                 evh.evl_proto = eh->ether_type;
436
437                 /* This kludge is OK; BPF treats the "mbuf" as read-only */
438                 mh.mh_next = m;
439                 mh.mh_data = (char *)&evh;
440                 mh.mh_len = ETHER_HDR_LEN + EVL_ENCAPLEN;
441                 bpf_mtap(m->m_pkthdr.rcvif, (struct mbuf *)&mh);
442         }
443
444         for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
445             ifv = LIST_NEXT(ifv, ifv_list)) {
446                 if (m->m_pkthdr.rcvif == ifv->ifv_p
447                     && ifv->ifv_tag == t)
448                         break;
449         }
450
451         if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
452                 m_freem(m);
453                 return -1;      /* So the parent can take note */
454         }
455
456         /*
457          * Having found a valid vlan interface corresponding to
458          * the given source interface and vlan tag, run the
459          * the real packet through ether_input().
460          */
461         m->m_pkthdr.rcvif = &ifv->ifv_if;
462
463         ifv->ifv_if.if_ipackets++;
464         ether_input(&ifv->ifv_if, eh, m);
465         return 0;
466 }
467
468 static int
469 vlan_input(struct ether_header *eh, struct mbuf *m)
470 {
471         struct ifvlan *ifv;
472
473         for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
474             ifv = LIST_NEXT(ifv, ifv_list)) {
475                 if (m->m_pkthdr.rcvif == ifv->ifv_p
476                     && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *)))
477                         == ifv->ifv_tag))
478                         break;
479         }
480
481         if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
482                 m->m_pkthdr.rcvif->if_noproto++;
483                 m_freem(m);
484                 return -1;      /* so ether_input can take note */
485         }
486
487         /*
488          * Having found a valid vlan interface corresponding to
489          * the given source interface and vlan tag, remove the
490          * encapsulation, and run the real packet through
491          * ether_input() a second time (it had better be
492          * reentrant!).
493          */
494         m->m_pkthdr.rcvif = &ifv->ifv_if;
495         eh->ether_type = mtod(m, u_int16_t *)[1];
496         m->m_data += EVL_ENCAPLEN;
497         m->m_len -= EVL_ENCAPLEN;
498         m->m_pkthdr.len -= EVL_ENCAPLEN;
499
500         ifv->ifv_if.if_ipackets++;
501         ether_input(&ifv->ifv_if, eh, m);
502         return 0;
503 }
504
505 static int
506 vlan_config(struct ifvlan *ifv, struct ifnet *p)
507 {
508         struct ifaddr *ifa1, *ifa2;
509         struct sockaddr_dl *sdl1, *sdl2;
510
511         if (p->if_data.ifi_type != IFT_ETHER)
512                 return EPROTONOSUPPORT;
513         if (ifv->ifv_p)
514                 return EBUSY;
515         ifv->ifv_p = p;
516         if (p->if_data.ifi_hdrlen == sizeof(struct ether_vlan_header))
517                 ifv->ifv_if.if_mtu = p->if_mtu;
518         else
519                 ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN;
520
521         /*
522          * Copy only a selected subset of flags from the parent.
523          * Other flags are none of our business.
524          */
525         ifv->ifv_if.if_flags = (p->if_flags &
526             (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT));
527
528         /*
529          * Set up our ``Ethernet address'' to reflect the underlying
530          * physical interface's.
531          */
532         ifa1 = ifnet_addrs[ifv->ifv_if.if_index - 1];
533         ifa2 = ifnet_addrs[p->if_index - 1];
534         sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
535         sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
536         sdl1->sdl_type = IFT_ETHER;
537         sdl1->sdl_alen = ETHER_ADDR_LEN;
538         bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
539         bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
540
541         /*
542          * Configure multicast addresses that may already be
543          * joined on the vlan device.
544          */
545         (void)vlan_setmulti(&ifv->ifv_if);
546
547         return 0;
548 }
549
550 static int
551 vlan_unconfig(struct ifnet *ifp)
552 {
553         struct ifaddr *ifa;
554         struct sockaddr_dl *sdl;
555         struct vlan_mc_entry *mc;
556         struct ifvlan *ifv;
557         struct ifnet *p;
558         int error;
559
560         ifv = ifp->if_softc;
561         p = ifv->ifv_p;
562
563         if (p) {
564                 struct sockaddr_dl sdl;
565
566                 /*
567                  * Since the interface is being unconfigured, we need to
568                  * empty the list of multicast groups that we may have joined
569                  * while we were alive from the parent's list.
570                  */
571                 bzero((char *)&sdl, sizeof sdl);
572                 sdl.sdl_len = sizeof sdl;
573                 sdl.sdl_family = AF_LINK;
574                 sdl.sdl_index = p->if_index;
575                 sdl.sdl_type = IFT_ETHER;
576                 sdl.sdl_alen = ETHER_ADDR_LEN;
577
578                 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
579                         mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
580                         bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
581                         error = if_delmulti(p, (struct sockaddr *)&sdl);
582                         if (error)
583                                 return(error);
584                         SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
585                         free(mc, M_VLAN);
586                 }
587         }
588
589         /* Disconnect from parent. */
590         ifv->ifv_p = NULL;
591         ifv->ifv_if.if_mtu = ETHERMTU;
592
593         /* Clear our MAC address. */
594         ifa = ifnet_addrs[ifv->ifv_if.if_index - 1];
595         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
596         sdl->sdl_type = IFT_ETHER;
597         sdl->sdl_alen = ETHER_ADDR_LEN;
598         bzero(LLADDR(sdl), ETHER_ADDR_LEN);
599         bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
600
601         return 0;
602 }
603
604 static int
605 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
606 {
607         struct ifaddr *ifa;
608         struct ifnet *p;
609         struct ifreq *ifr;
610         struct ifvlan *ifv;
611         struct vlanreq vlr;
612         int error = 0;
613
614         ifr = (struct ifreq *)data;
615         ifa = (struct ifaddr *)data;
616         ifv = ifp->if_softc;
617
618         switch (cmd) {
619         case SIOCSIFADDR:
620                 ifp->if_flags |= IFF_UP;
621
622                 switch (ifa->ifa_addr->sa_family) {
623 #ifdef INET
624                 case AF_INET:
625                         arp_ifinit(&ifv->ifv_if, ifa);
626                         break;
627 #endif
628                 default:
629                         break;
630                 }
631                 break;
632
633         case SIOCGIFADDR:
634                 {
635                         struct sockaddr *sa;
636
637                         sa = (struct sockaddr *) &ifr->ifr_data;
638                         bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
639                               (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
640                 }
641                 break;
642
643         case SIOCGIFMEDIA:
644                 if (ifv->ifv_p != NULL) {
645                         error = (ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCGIFMEDIA, data);
646                         /* Limit the result to the parent's current config. */
647                         if (error == 0) {
648                                 struct ifmediareq *ifmr;
649
650                                 ifmr = (struct ifmediareq *) data;
651                                 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
652                                         ifmr->ifm_count = 1;
653                                         error = copyout(&ifmr->ifm_current,
654                                                 ifmr->ifm_ulist, 
655                                                 sizeof(int));
656                                 }
657                         }
658                 } else
659                         error = EINVAL;
660                 break;
661
662         case SIOCSIFMEDIA:
663                 error = EINVAL;
664                 break;
665
666         case SIOCSIFMTU:
667                 /*
668                  * Set the interface MTU.
669                  * This is bogus. The underlying interface might support
670                  * jumbo frames.
671                  */
672                 if (ifr->ifr_mtu > ETHERMTU) {
673                         error = EINVAL;
674                 } else {
675                         ifp->if_mtu = ifr->ifr_mtu;
676                 }
677                 break;
678
679         case SIOCSETVLAN:
680                 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
681                 if (error)
682                         break;
683                 if (vlr.vlr_parent[0] == '\0') {
684                         vlan_unconfig(ifp);
685                         if (ifp->if_flags & IFF_UP) {
686                                 int s = splimp();
687                                 if_down(ifp);
688                                 splx(s);
689                         }               
690                         ifp->if_flags &= ~IFF_RUNNING;
691                         break;
692                 }
693                 p = ifunit(vlr.vlr_parent);
694                 if (p == 0) {
695                         error = ENOENT;
696                         break;
697                 }
698                 error = vlan_config(ifv, p);
699                 if (error)
700                         break;
701                 ifv->ifv_tag = vlr.vlr_tag;
702                 ifp->if_flags |= IFF_RUNNING;
703                 break;
704                 
705         case SIOCGETVLAN:
706                 bzero(&vlr, sizeof vlr);
707                 if (ifv->ifv_p) {
708                         snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent),
709                             "%s%d", ifv->ifv_p->if_name, ifv->ifv_p->if_unit);
710                         vlr.vlr_tag = ifv->ifv_tag;
711                 }
712                 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
713                 break;
714                 
715         case SIOCSIFFLAGS:
716                 /*
717                  * We don't support promiscuous mode
718                  * right now because it would require help from the
719                  * underlying drivers, which hasn't been implemented.
720                  */
721                 if (ifr->ifr_flags & (IFF_PROMISC)) {
722                         ifp->if_flags &= ~(IFF_PROMISC);
723                         error = EINVAL;
724                 }
725                 break;
726         case SIOCADDMULTI:
727         case SIOCDELMULTI:
728                 error = vlan_setmulti(ifp);
729                 break;
730         default:
731                 error = EINVAL;
732         }
733         return error;
734 }