xref: /dragonfly/sys/net/vlan/if_vlan.c (revision 6b5c5d0d)
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.26 2008/01/11 11:59:41 sephe 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/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #include <sys/mbuf.h>
68 #include <sys/module.h>
69 #include <sys/queue.h>
70 #include <sys/socket.h>
71 #include <sys/sockio.h>
72 #include <sys/sysctl.h>
73 #include <sys/bus.h>
74 #include <sys/thread2.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 <net/ifq_var.h>
83 #include <net/if_clone.h>
84 #include "if_vlan_var.h"
85 
86 #ifdef INET
87 #include <netinet/in.h>
88 #include <netinet/if_ether.h>
89 #endif
90 
91 #define VLANNAME	"vlan"
92 
93 SYSCTL_DECL(_net_link);
94 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
95 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
96 
97 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
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(const struct ether_header *eh, struct mbuf *m);
105 static	int vlan_input_tag(struct mbuf *m, uint16_t t);
106 static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr,
107 		struct ucred *cr);
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 = IF_CLONE_INITIALIZER("vlan", vlan_clone_create,
113     vlan_clone_destroy, NVLAN, IF_MAXUNIT);
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 		kfree(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 = kmalloc(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 
184 	switch (type) {
185 	case MOD_LOAD:
186 		LIST_INIT(&ifv_list);
187 		vlan_input_p = vlan_input;
188 		vlan_input_tag_p = vlan_input_tag;
189 		if_clone_attach(&vlan_cloner);
190 		break;
191 	case MOD_UNLOAD:
192 		if_clone_detach(&vlan_cloner);
193 		vlan_input_p = NULL;
194 		vlan_input_tag_p = NULL;
195 		while (!LIST_EMPTY(&ifv_list))
196 			vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
197 		break;
198 	}
199 	return 0;
200 }
201 
202 static moduledata_t vlan_mod = {
203 	"if_vlan",
204 	vlan_modevent,
205 	0
206 };
207 
208 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
209 
210 static int
211 vlan_clone_create(struct if_clone *ifc, int unit)
212 {
213 	struct ifvlan *ifv;
214 	struct ifnet *ifp;
215 
216 	ifv = kmalloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
217 	ifp = &ifv->ifv_if;
218 	SLIST_INIT(&ifv->vlan_mc_listhead);
219 
220 	crit_enter();
221 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
222 	crit_exit();
223 
224 	ifp->if_softc = ifv;
225 	if_initname(ifp, "vlan", unit);
226 	/* NB: flags are not set here */
227 	ifp->if_linkmib = &ifv->ifv_mib;
228 	ifp->if_linkmiblen = sizeof ifv->ifv_mib;
229 	/* NB: mtu is not set here */
230 
231 	ifp->if_init = vlan_ifinit;
232 	ifp->if_start = vlan_start;
233 	ifp->if_ioctl = vlan_ioctl;
234 	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
235 	ifq_set_ready(&ifp->if_snd);
236 	ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr, NULL);
237 	/* Now undo some of the damage... */
238 	ifp->if_data.ifi_type = IFT_L2VLAN;
239 	ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
240 
241 	return (0);
242 }
243 
244 static void
245 vlan_clone_destroy(struct ifnet *ifp)
246 {
247 	struct ifvlan *ifv = ifp->if_softc;
248 
249 	crit_enter();
250 
251 	LIST_REMOVE(ifv, ifv_list);
252 	vlan_unconfig(ifp);
253 	ether_ifdetach(ifp);
254 
255 	crit_exit();
256 
257 	kfree(ifv, M_VLAN);
258 }
259 
260 static void
261 vlan_ifinit(void *foo)
262 {
263 	return;
264 }
265 
266 static void
267 vlan_start(struct ifnet *ifp)
268 {
269 	struct ifvlan *ifv;
270 	struct ifnet *p;
271 	struct ether_vlan_header *evl;
272 	struct mbuf *m;
273 	int error;
274 	struct altq_pktattr pktattr;
275 
276 	ifv = ifp->if_softc;
277 	p = ifv->ifv_p;
278 
279 	ifp->if_flags |= IFF_OACTIVE;
280 	for (;;) {
281 		m = ifq_dequeue(&ifp->if_snd, NULL);
282 		if (m == NULL)
283 			break;
284 		BPF_MTAP(ifp, m);
285 
286 		/*
287 		 * Do not run parent's if_start() if the parent is not up,
288 		 * or parent's driver will cause a system crash.
289 		 */
290 		if ((p->if_flags & (IFF_UP | IFF_RUNNING)) !=
291 					(IFF_UP | IFF_RUNNING)) {
292 			m_freem(m);
293 			ifp->if_data.ifi_collisions++;
294 			continue;
295 		}
296 
297 		/*
298 		 * If ALTQ is enabled on the parent interface, do
299 		 * classification; the queueing discipline might
300 		 * not require classification, but might require
301 		 * the address family/header pointer in the pktattr.
302 		 */
303 		if (ifq_is_enabled(&p->if_snd))
304 			altq_etherclassify(&p->if_snd, m, &pktattr);
305 
306 		/*
307 		 * If underlying interface can do VLAN tag insertion itself,
308 		 * just pass the packet along. However, we need some way to
309 		 * tell the interface where the packet came from so that it
310 		 * knows how to find the VLAN tag to use, so we set the rcvif
311 		 * in the mbuf header to our ifnet.
312 		 *
313 		 * Note: we also set the M_PROTO1 flag in the mbuf to let
314 		 * the parent driver know that the rcvif pointer is really
315 		 * valid. We need to do this because sometimes mbufs will
316 		 * be allocated by other parts of the system that contain
317 		 * garbage in the rcvif pointer. Using the M_PROTO1 flag
318 		 * lets the driver perform a proper sanity check and avoid
319 		 * following potentially bogus rcvif pointers off into
320 		 * never-never land.
321 		 */
322 		if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
323 			m->m_pkthdr.rcvif = ifp;
324 			m->m_flags |= M_PROTO1;
325 		} else {
326 			M_PREPEND(m, EVL_ENCAPLEN, MB_DONTWAIT);
327 			if (m == NULL) {
328 				kprintf("%s: M_PREPEND failed", ifp->if_xname);
329 				ifp->if_ierrors++;
330 				continue;
331 			}
332 			/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
333 
334 			m = m_pullup(m, ETHER_HDR_LEN + EVL_ENCAPLEN);
335 			if (m == NULL) {
336 				kprintf("%s: m_pullup failed", ifp->if_xname);
337 				ifp->if_ierrors++;
338 				continue;
339 			}
340 
341 			/*
342 			 * Transform the Ethernet header into an Ethernet header
343 			 * with 802.1Q encapsulation.
344 			 */
345 			bcopy(mtod(m, char *) + EVL_ENCAPLEN, mtod(m, char *),
346 			      sizeof(struct ether_header));
347 			evl = mtod(m, struct ether_vlan_header *);
348 			evl->evl_proto = evl->evl_encap_proto;
349 			evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
350 			evl->evl_tag = htons(ifv->ifv_tag);
351 #ifdef DEBUG
352 			kprintf("vlan_start: %*D\n", sizeof *evl,
353 			    (unsigned char *)evl, ":");
354 #endif
355 		}
356 
357 		/*
358 		 * Send it, precisely as ether_output() would have.
359 		 * We are already running at splimp.
360 		 */
361 		lwkt_serialize_exit(ifp->if_serializer);
362 		lwkt_serialize_enter(p->if_serializer);
363 		error = ifq_handoff(p, m, &pktattr);
364 		lwkt_serialize_exit(p->if_serializer);
365 		lwkt_serialize_enter(ifp->if_serializer);
366 		if (error)
367 			ifp->if_oerrors++;
368 		else
369 			ifp->if_opackets++;
370 	}
371 	ifp->if_flags &= ~IFF_OACTIVE;
372 
373 	return;
374 }
375 
376 static int
377 vlan_input_tag( struct mbuf *m, uint16_t t)
378 {
379 	struct bpf_if *bif;
380 	struct ifvlan *ifv;
381 	struct ifnet *rcvif;
382 
383 	rcvif = m->m_pkthdr.rcvif;
384 
385 	ASSERT_SERIALIZED(rcvif->if_serializer);
386 
387 	/*
388 	 * Fake up a header and send the packet to the physical interface's
389 	 * bpf tap if active.
390 	 */
391 	if ((bif = rcvif->if_bpf) != NULL) {
392 		struct ether_header *eh;
393 		struct ether_vlan_header evh;
394 
395 		eh = mtod(m, struct ether_header *);
396 		m_adj(m, ETHER_HDR_LEN);
397 		bcopy(eh, &evh, 2*ETHER_ADDR_LEN);
398 		evh.evl_encap_proto = htons(ETHERTYPE_VLAN);
399 		evh.evl_tag = htons(t);
400 		evh.evl_proto = eh->ether_type;
401 		bpf_ptap(bif, m, &evh, ETHER_HDR_LEN + EVL_ENCAPLEN);
402 		/* XXX assumes data was left intact */
403 		M_PREPEND(m, ETHER_HDR_LEN, MB_WAIT);
404 	}
405 
406 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
407 	    ifv = LIST_NEXT(ifv, ifv_list)) {
408 		if (rcvif == ifv->ifv_p && ifv->ifv_tag == t)
409 			break;
410 	}
411 
412 	if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
413 		m_freem(m);
414 		return -1;	/* So the parent can take note */
415 	}
416 
417 	/*
418 	 * Having found a valid vlan interface corresponding to
419 	 * the given source interface and vlan tag, run the
420 	 * the real packet through ether_input().
421 	 */
422 	m->m_pkthdr.rcvif = &ifv->ifv_if;
423 
424 	ifv->ifv_if.if_ipackets++;
425 	lwkt_serialize_exit(rcvif->if_serializer);
426 	lwkt_serialize_enter(ifv->ifv_if.if_serializer);
427 	ether_input(&ifv->ifv_if, m);
428 	lwkt_serialize_exit(ifv->ifv_if.if_serializer);
429 	lwkt_serialize_enter(rcvif->if_serializer);
430 	return 0;
431 }
432 
433 static int
434 vlan_input(const struct ether_header *eh, struct mbuf *m)
435 {
436 	struct ifvlan *ifv;
437 	struct ifnet *rcvif;
438 	struct ether_header eh_copy;
439 
440 	rcvif = m->m_pkthdr.rcvif;
441 	ASSERT_SERIALIZED(rcvif->if_serializer);
442 
443 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
444 	    ifv = LIST_NEXT(ifv, ifv_list)) {
445 		if (rcvif == ifv->ifv_p
446 		    && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *)))
447 			== ifv->ifv_tag))
448 			break;
449 	}
450 
451 	if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
452 		rcvif->if_noproto++;
453 		m_freem(m);
454 		return -1;	/* so ether_input can take note */
455 	}
456 
457 	/*
458 	 * Having found a valid vlan interface corresponding to
459 	 * the given source interface and vlan tag, remove the
460 	 * remaining encapsulation (ether_vlan_header minus the ether_header
461 	 * that had already been removed) and run the real packet
462 	 * through ether_input() a second time (it had better be
463 	 * reentrant!).
464 	 */
465 	eh_copy = *eh;
466 	eh_copy.ether_type = mtod(m, u_int16_t *)[1];	/* evl_proto */
467 	m->m_pkthdr.rcvif = &ifv->ifv_if;
468 	m_adj(m, EVL_ENCAPLEN);
469 	M_PREPEND(m, ETHER_HDR_LEN, MB_WAIT);
470 	*(struct ether_header *)mtod(m, void *) = eh_copy;
471 
472 	ifv->ifv_if.if_ipackets++;
473 	lwkt_serialize_exit(rcvif->if_serializer);
474 	lwkt_serialize_enter(ifv->ifv_if.if_serializer);
475 	ether_input(&ifv->ifv_if, m);
476 	lwkt_serialize_exit(ifv->ifv_if.if_serializer);
477 	lwkt_serialize_enter(rcvif->if_serializer);
478 	return 0;
479 }
480 
481 static int
482 vlan_config(struct ifvlan *ifv, struct ifnet *p)
483 {
484 	struct sockaddr_dl *sdl1, *sdl2;
485 
486 	if (p->if_data.ifi_type != IFT_ETHER)
487 		return EPROTONOSUPPORT;
488 	if (ifv->ifv_p)
489 		return EBUSY;
490 	ifv->ifv_p = p;
491 	if (p->if_capenable & IFCAP_VLAN_MTU)
492 		ifv->ifv_if.if_mtu = p->if_mtu;
493 	else
494 		ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN;
495 
496 	/*
497 	 * Copy only a selected subset of flags from the parent.
498 	 * Other flags are none of our business.
499 	 */
500 	ifv->ifv_if.if_flags = (p->if_flags &
501 	    (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT));
502 
503 	/*
504 	 * Set up our ``Ethernet address'' to reflect the underlying
505 	 * physical interface's.
506 	 */
507 	sdl1 = IF_LLSOCKADDR(&ifv->ifv_if);
508 	sdl2 = IF_LLSOCKADDR(p);
509 	sdl1->sdl_type = IFT_ETHER;
510 	sdl1->sdl_alen = ETHER_ADDR_LEN;
511 	bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
512 	bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
513 
514 	/*
515 	 * Configure multicast addresses that may already be
516 	 * joined on the vlan device.
517 	 */
518 	vlan_setmulti(&ifv->ifv_if);
519 
520 	return 0;
521 }
522 
523 static int
524 vlan_unconfig(struct ifnet *ifp)
525 {
526 	struct sockaddr_dl *sdl;
527 	struct vlan_mc_entry *mc;
528 	struct ifvlan *ifv;
529 	struct ifnet *p;
530 	int error;
531 
532 	ifv = ifp->if_softc;
533 	p = ifv->ifv_p;
534 
535 	if (p) {
536 		struct sockaddr_dl sdl;
537 
538 		/*
539 		 * Since the interface is being unconfigured, we need to
540 		 * empty the list of multicast groups that we may have joined
541 		 * while we were alive from the parent's list.
542 		 */
543 		bzero((char *)&sdl, sizeof sdl);
544 		sdl.sdl_len = sizeof sdl;
545 		sdl.sdl_family = AF_LINK;
546 		sdl.sdl_index = p->if_index;
547 		sdl.sdl_type = IFT_ETHER;
548 		sdl.sdl_alen = ETHER_ADDR_LEN;
549 
550 		while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
551 			mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
552 			bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
553 			error = if_delmulti(p, (struct sockaddr *)&sdl);
554 			if (error)
555 				return(error);
556 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
557 			kfree(mc, M_VLAN);
558 		}
559 	}
560 
561 	/* Disconnect from parent. */
562 	ifv->ifv_p = NULL;
563 	ifv->ifv_if.if_mtu = ETHERMTU;
564 
565 	/* Clear our MAC address. */
566 	sdl = IF_LLSOCKADDR(&ifv->ifv_if);
567 	sdl->sdl_type = IFT_ETHER;
568 	sdl->sdl_alen = ETHER_ADDR_LEN;
569 	bzero(LLADDR(sdl), ETHER_ADDR_LEN);
570 	bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
571 
572 	return 0;
573 }
574 
575 static int
576 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
577 {
578 	struct ifaddr *ifa;
579 	struct ifnet *p;
580 	struct ifreq *ifr;
581 	struct ifvlan *ifv;
582 	struct vlanreq vlr;
583 	int error = 0;
584 
585 	ifr = (struct ifreq *)data;
586 	ifa = (struct ifaddr *)data;
587 	ifv = ifp->if_softc;
588 
589 	ASSERT_SERIALIZED(ifp->if_serializer);
590 	crit_enter();
591 
592 	switch (cmd) {
593 	case SIOCSIFADDR:
594 		ifp->if_flags |= IFF_UP;
595 
596 		switch (ifa->ifa_addr->sa_family) {
597 #ifdef INET
598 		case AF_INET:
599 			arp_ifinit(&ifv->ifv_if, ifa);
600 			break;
601 #endif
602 		default:
603 			break;
604 		}
605 		break;
606 
607 	case SIOCGIFADDR:
608 		{
609 			struct sockaddr *sa;
610 
611 			sa = (struct sockaddr *) &ifr->ifr_data;
612 			bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
613 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
614 		}
615 		break;
616 
617 	case SIOCGIFMEDIA:
618 		if (ifv->ifv_p != NULL) {
619 			lwkt_serialize_exit(ifp->if_serializer);
620 			lwkt_serialize_enter(ifv->ifv_p->if_serializer);
621 			error = ifv->ifv_p->if_ioctl(ifv->ifv_p,
622 						     SIOCGIFMEDIA, data, cr);
623 			lwkt_serialize_exit(ifv->ifv_p->if_serializer);
624 			lwkt_serialize_enter(ifp->if_serializer);
625 			/* Limit the result to the parent's current config. */
626 			if (error == 0) {
627 				struct ifmediareq *ifmr;
628 
629 				ifmr = (struct ifmediareq *) data;
630 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
631 					ifmr->ifm_count = 1;
632 					error = copyout(&ifmr->ifm_current,
633 						ifmr->ifm_ulist,
634 						sizeof(int));
635 				}
636 			}
637 		} else
638 			error = EINVAL;
639 		break;
640 
641 	case SIOCSIFMEDIA:
642 		error = EINVAL;
643 		break;
644 
645 	case SIOCSIFMTU:
646 		/*
647 		 * Set the interface MTU.
648 		 * This is bogus. The underlying interface might support
649 	 	 * jumbo frames.
650 		 */
651 		if (ifr->ifr_mtu > ETHERMTU) {
652 			error = EINVAL;
653 		} else {
654 			ifp->if_mtu = ifr->ifr_mtu;
655 		}
656 		break;
657 
658 	case SIOCSETVLAN:
659 		error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
660 		if (error)
661 			break;
662 		if (vlr.vlr_parent[0] == '\0') {
663 			vlan_unconfig(ifp);
664 			if (ifp->if_flags & IFF_UP)
665 				if_down(ifp);
666 			ifp->if_flags &= ~IFF_RUNNING;
667 			break;
668 		}
669 		p = ifunit(vlr.vlr_parent);
670 		if (p == 0) {
671 			error = ENOENT;
672 			break;
673 		}
674 		error = vlan_config(ifv, p);
675 		if (error)
676 			break;
677 		ifv->ifv_tag = vlr.vlr_tag;
678 		ifp->if_flags |= IFF_RUNNING;
679 		break;
680 
681 	case SIOCGETVLAN:
682 		bzero(&vlr, sizeof vlr);
683 		if (ifv->ifv_p) {
684 			strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
685 			    sizeof(vlr.vlr_parent));
686 			vlr.vlr_tag = ifv->ifv_tag;
687 		}
688 		error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
689 		break;
690 
691 	case SIOCSIFFLAGS:
692 		/*
693 		 * We don't support promiscuous mode
694 		 * right now because it would require help from the
695 		 * underlying drivers, which hasn't been implemented.
696 		 */
697 		if (ifr->ifr_flags & (IFF_PROMISC)) {
698 			ifp->if_flags &= ~(IFF_PROMISC);
699 			error = EINVAL;
700 		}
701 		break;
702 	case SIOCADDMULTI:
703 	case SIOCDELMULTI:
704 		error = vlan_setmulti(ifp);
705 		break;
706 	default:
707 		error = EINVAL;
708 	}
709 
710 	crit_exit();
711 
712 	return error;
713 }
714