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