xref: /dragonfly/sys/net/vlan/if_vlan.c (revision 2038fb68)
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.43 2008/11/22 04:00:53 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 #ifndef NVLAN
46 #include "use_vlan.h"
47 #endif
48 #include "opt_inet.h"
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/module.h>
56 #include <sys/queue.h>
57 #include <sys/socket.h>
58 #include <sys/sockio.h>
59 #include <sys/sysctl.h>
60 #include <sys/bus.h>
61 #include <sys/thread2.h>
62 
63 #include <net/bpf.h>
64 #include <net/ethernet.h>
65 #include <net/if.h>
66 #include <net/if_arp.h>
67 #include <net/if_dl.h>
68 #include <net/if_types.h>
69 #include <net/ifq_var.h>
70 #include <net/if_clone.h>
71 #include <net/netmsg2.h>
72 
73 #ifdef INET
74 #include <netinet/in.h>
75 #include <netinet/if_ether.h>
76 #endif
77 
78 #include <net/vlan/if_vlan_var.h>
79 #include <net/vlan/if_vlan_ether.h>
80 
81 struct ifvlan;
82 
83 struct vlan_mc_entry {
84 	struct ether_addr		mc_addr;
85 	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
86 };
87 
88 struct vlan_entry {
89 	struct ifvlan		*ifv;
90 	LIST_ENTRY(vlan_entry)	ifv_link;
91 };
92 
93 struct	ifvlan {
94 	struct	arpcom ifv_ac;	/* make this an interface */
95 	struct	ifnet *ifv_p;	/* parent inteface of this vlan */
96 	int ifv_pflags;		/* special flags we have set on parent */
97 	struct	ifv_linkmib {
98 		int	ifvm_parent;
99 		uint16_t ifvm_proto; /* encapsulation ethertype */
100 		uint16_t ifvm_tag; /* tag to apply on packets leaving if */
101 	}	ifv_mib;
102 	SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
103 	LIST_ENTRY(ifvlan) ifv_list;
104 	struct vlan_entry ifv_entries[1];
105 };
106 #define	ifv_if	ifv_ac.ac_if
107 #define	ifv_tag	ifv_mib.ifvm_tag
108 
109 struct vlan_trunk {
110 	LIST_HEAD(, vlan_entry) vlan_list;
111 };
112 
113 struct netmsg_vlan {
114 	struct netmsg	nv_nmsg;
115 	struct ifvlan	*nv_ifv;
116 	struct ifnet	*nv_ifp_p;
117 	const char	*nv_parent_name;
118 	uint16_t	nv_vlantag;
119 };
120 
121 #define VLANNAME	"vlan"
122 
123 SYSCTL_DECL(_net_link);
124 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
125 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
126 
127 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
128 static LIST_HEAD(, ifvlan) ifv_list;
129 
130 static int	vlan_clone_create(struct if_clone *, int);
131 static void	vlan_clone_destroy(struct ifnet *);
132 static void	vlan_ifdetach(void *, struct ifnet *);
133 
134 static void	vlan_init(void *);
135 static void	vlan_start(struct ifnet *);
136 static int	vlan_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
137 static void	vlan_input(struct mbuf *);
138 
139 static int	vlan_setflags(struct ifvlan *, struct ifnet *, int);
140 static int	vlan_setflag(struct ifvlan *, struct ifnet *, int, int,
141 			     int (*)(struct ifnet *, int));
142 static int	vlan_config_flags(struct ifvlan *ifv);
143 static void	vlan_clrmulti(struct ifvlan *, struct ifnet *);
144 static int	vlan_setmulti(struct ifvlan *, struct ifnet *);
145 static int	vlan_config_multi(struct ifvlan *);
146 static int	vlan_config(struct ifvlan *, const char *, uint16_t);
147 static int	vlan_unconfig(struct ifvlan *);
148 static void	vlan_link(struct ifvlan *, struct ifnet *);
149 static void	vlan_unlink(struct ifvlan *, struct ifnet *);
150 
151 static void	vlan_config_dispatch(struct netmsg *);
152 static void	vlan_unconfig_dispatch(struct netmsg *);
153 static void	vlan_link_dispatch(struct netmsg *);
154 static void	vlan_unlink_dispatch(struct netmsg *);
155 static void	vlan_multi_dispatch(struct netmsg *);
156 static void	vlan_flags_dispatch(struct netmsg *);
157 static void	vlan_ifdetach_dispatch(struct netmsg *);
158 
159 /* Special flags we should propagate to parent */
160 static struct {
161 	int flag;
162 	int (*func)(struct ifnet *, int);
163 } vlan_pflags[] = {
164 	{ IFF_PROMISC, ifpromisc },
165 	{ IFF_ALLMULTI, if_allmulti },
166 	{ 0, NULL }
167 };
168 
169 static eventhandler_tag vlan_ifdetach_cookie;
170 static struct if_clone vlan_cloner =
171 	IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy,
172 			     NVLAN, IF_MAXUNIT);
173 
174 /*
175  * Handle IFF_* flags that require certain changes on the parent:
176  * if "set" is true, update parent's flags respective to our if_flags;
177  * if "set" is false, forcedly clear the flags set on parent.
178  */
179 static int
180 vlan_setflags(struct ifvlan *ifv, struct ifnet *ifp_p, int set)
181 {
182 	int error, i;
183 
184 	ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
185 
186 	for (i = 0; vlan_pflags[i].func != NULL; i++) {
187 		error = vlan_setflag(ifv, ifp_p, vlan_pflags[i].flag,
188 				     set, vlan_pflags[i].func);
189 		if (error)
190 			return error;
191 	}
192 	return 0;
193 }
194 
195 /* Handle a reference counted flag that should be set on the parent as well */
196 static int
197 vlan_setflag(struct ifvlan *ifv, struct ifnet *ifp_p, int flag, int set,
198 	     int (*func)(struct ifnet *, int))
199 {
200 	struct ifnet *ifp = &ifv->ifv_if;
201 	int error, ifv_flag;
202 
203 	ASSERT_NOT_SERIALIZED(ifp->if_serializer);
204 
205 	ifv_flag = set ? (ifp->if_flags & flag) : 0;
206 
207 	/*
208 	 * See if recorded parent's status is different from what
209 	 * we want it to be.  If it is, flip it.  We record parent's
210 	 * status in ifv_pflags so that we won't clear parent's flag
211 	 * we haven't set.  In fact, we don't clear or set parent's
212 	 * flags directly, but get or release references to them.
213 	 * That's why we can be sure that recorded flags still are
214 	 * in accord with actual parent's flags.
215 	 */
216 	if (ifv_flag != (ifv->ifv_pflags & flag)) {
217 		error = func(ifp_p, ifv_flag);
218 		if (error)
219 			return error;
220 		ifv->ifv_pflags &= ~flag;
221 		ifv->ifv_pflags |= ifv_flag;
222 	}
223 	return 0;
224 }
225 
226 /*
227  * Program our multicast filter. What we're actually doing is
228  * programming the multicast filter of the parent. This has the
229  * side effect of causing the parent interface to receive multicast
230  * traffic that it doesn't really want, which ends up being discarded
231  * later by the upper protocol layers. Unfortunately, there's no way
232  * to avoid this: there really is only one physical interface.
233  */
234 static int
235 vlan_setmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
236 {
237 	struct ifmultiaddr *ifma, *rifma = NULL;
238 	struct vlan_mc_entry *mc = NULL;
239 	struct sockaddr_dl sdl;
240 	struct ifnet *ifp = &ifv->ifv_if;
241 
242 	ASSERT_NOT_SERIALIZED(ifp->if_serializer);
243 
244 	/*
245 	 * First, remove any existing filter entries.
246 	 */
247 	vlan_clrmulti(ifv, ifp_p);
248 
249 	/*
250 	 * Now program new ones.
251 	 */
252 	bzero(&sdl, sizeof(sdl));
253 	sdl.sdl_len = sizeof(sdl);
254 	sdl.sdl_family = AF_LINK;
255 	sdl.sdl_index = ifp_p->if_index;
256 	sdl.sdl_type = IFT_ETHER;
257 	sdl.sdl_alen = ETHER_ADDR_LEN;
258 
259 	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
260 		int error;
261 
262 		if (ifma->ifma_addr->sa_family != AF_LINK)
263 			continue;
264 
265 		/* Save a copy */
266 		mc = kmalloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
267 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
268 		      &mc->mc_addr, ETHER_ADDR_LEN);
269 		SLIST_INSERT_HEAD(&ifv->vlan_mc_listhead, mc, mc_entries);
270 
271 		/* Program the parent multicast filter */
272 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
273 		      LLADDR(&sdl), ETHER_ADDR_LEN);
274 		error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
275 		if (error)
276 			return error;
277 	}
278 	return 0;
279 }
280 
281 static void
282 vlan_clrmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
283 {
284 	struct vlan_mc_entry *mc;
285 	struct sockaddr_dl sdl;
286 
287 	ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
288 
289 	bzero(&sdl, sizeof(sdl));
290 	sdl.sdl_len = sizeof(sdl);
291 	sdl.sdl_family = AF_LINK;
292 	sdl.sdl_index = ifp_p->if_index;
293 	sdl.sdl_type = IFT_ETHER;
294 	sdl.sdl_alen = ETHER_ADDR_LEN;
295 
296 	while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
297 		bcopy(&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
298 		if_delmulti(ifp_p, (struct sockaddr *)&sdl); /* ignore error */
299 
300 		SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
301 		kfree(mc, M_VLAN);
302 	}
303 }
304 
305 static int
306 vlan_modevent(module_t mod, int type, void *data)
307 {
308 	switch (type) {
309 	case MOD_LOAD:
310 		LIST_INIT(&ifv_list);
311 		vlan_input_p = vlan_input;
312 		vlan_ifdetach_cookie =
313 		EVENTHANDLER_REGISTER(ifnet_detach_event,
314 				      vlan_ifdetach, NULL,
315 				      EVENTHANDLER_PRI_ANY);
316 		if_clone_attach(&vlan_cloner);
317 		break;
318 
319 	case MOD_UNLOAD:
320 		if_clone_detach(&vlan_cloner);
321 
322 		vlan_input_p = NULL;
323 		/*
324 		 * Make that all protocol threads see vlan_input_p change.
325 		 */
326 		netmsg_service_sync();
327 
328 		EVENTHANDLER_DEREGISTER(ifnet_detach_event,
329 					vlan_ifdetach_cookie);
330 		while (!LIST_EMPTY(&ifv_list))
331 			vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
332 		break;
333 	}
334 	return 0;
335 }
336 
337 static moduledata_t vlan_mod = {
338 	"if_vlan",
339 	vlan_modevent,
340 	0
341 };
342 
343 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
344 
345 static void
346 vlan_ifdetach_dispatch(struct netmsg *nmsg)
347 {
348 	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
349 	struct ifnet *ifp_p = vmsg->nv_ifp_p;
350 	struct vlan_trunk *vlantrunks, *trunk;
351 	struct vlan_entry *ifve;
352 
353 	vlantrunks = ifp_p->if_vlantrunks;
354 	if (vlantrunks == NULL)
355 		goto reply;
356 	trunk = &vlantrunks[mycpuid];
357 
358 	while (ifp_p->if_vlantrunks &&
359 	       (ifve = LIST_FIRST(&trunk->vlan_list)) != NULL)
360 		vlan_unconfig(ifve->ifv);
361 reply:
362 	lwkt_replymsg(&nmsg->nm_lmsg, 0);
363 }
364 
365 static void
366 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
367 {
368 	struct netmsg_vlan vmsg;
369 	struct netmsg *nmsg;
370 
371 	ASSERT_NOT_SERIALIZED(ifp->if_serializer);
372 
373 	bzero(&vmsg, sizeof(vmsg));
374 	nmsg = &vmsg.nv_nmsg;
375 
376 	netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_ifdetach_dispatch);
377 	vmsg.nv_ifp_p = ifp;
378 
379 	lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
380 }
381 
382 static int
383 vlan_clone_create(struct if_clone *ifc, int unit)
384 {
385 	struct ifvlan *ifv;
386 	struct ifnet *ifp;
387 	int vlan_size, i;
388 
389 	vlan_size = sizeof(struct ifvlan)
390 		  + ((ncpus - 1) * sizeof(struct vlan_entry));
391 	ifv = kmalloc(vlan_size, M_VLAN, M_WAITOK | M_ZERO);
392 	SLIST_INIT(&ifv->vlan_mc_listhead);
393 	for (i = 0; i < ncpus; ++i)
394 		ifv->ifv_entries[i].ifv = ifv;
395 
396 	crit_enter();	/* XXX not MP safe */
397 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
398 	crit_exit();
399 
400 	ifp = &ifv->ifv_if;
401 	ifp->if_softc = ifv;
402 	if_initname(ifp, "vlan", unit);
403 	/* NB: flags are not set here */
404 	ifp->if_linkmib = &ifv->ifv_mib;
405 	ifp->if_linkmiblen = sizeof ifv->ifv_mib;
406 	/* NB: mtu is not set here */
407 
408 	ifp->if_init = vlan_init;
409 	ifp->if_start = vlan_start;
410 	ifp->if_ioctl = vlan_ioctl;
411 	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
412 	ifq_set_ready(&ifp->if_snd);
413 	ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr, NULL);
414 	/* Now undo some of the damage... */
415 	ifp->if_data.ifi_type = IFT_L2VLAN;
416 	ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
417 
418 	return (0);
419 }
420 
421 static void
422 vlan_clone_destroy(struct ifnet *ifp)
423 {
424 	struct ifvlan *ifv = ifp->if_softc;
425 
426 	crit_enter();	/* XXX not MP safe */
427 	LIST_REMOVE(ifv, ifv_list);
428 	crit_exit();
429 
430 	vlan_unconfig(ifv);
431 	ether_ifdetach(ifp);
432 
433 	kfree(ifv, M_VLAN);
434 }
435 
436 static void
437 vlan_init(void *xsc)
438 {
439 	struct ifvlan *ifv = xsc;
440 	struct ifnet *ifp = &ifv->ifv_if;
441 
442 	ASSERT_SERIALIZED(ifp->if_serializer);
443 
444 	if (ifv->ifv_p != NULL)
445 		ifp->if_flags |= IFF_RUNNING;
446 }
447 
448 static void
449 vlan_start(struct ifnet *ifp)
450 {
451 	struct ifvlan *ifv = ifp->if_softc;
452 	struct ifnet *ifp_p = ifv->ifv_p;
453 	struct mbuf *m;
454 
455 	ASSERT_SERIALIZED(ifp->if_serializer);
456 
457 	if (ifp_p == NULL) {
458 		ifq_purge(&ifp->if_snd);
459 		return;
460 	}
461 
462 	if ((ifp->if_flags & IFF_RUNNING) == 0)
463 		return;
464 
465 	for (;;) {
466 		struct netmsg_packet *nmp;
467 		struct netmsg *nmsg;
468 		struct lwkt_port *port;
469 
470 		m = ifq_dequeue(&ifp->if_snd, NULL);
471 		if (m == NULL)
472 			break;
473 		BPF_MTAP(ifp, m);
474 
475 		/*
476 		 * Do not run parent's if_start() if the parent is not up,
477 		 * or parent's driver will cause a system crash.
478 		 */
479 		if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) !=
480 		    (IFF_UP | IFF_RUNNING)) {
481 			m_freem(m);
482 			ifp->if_data.ifi_collisions++;
483 			continue;
484 		}
485 
486 		/*
487 		 * We need some way to tell the interface where the packet
488 		 * came from so that it knows how to find the VLAN tag to
489 		 * use, so we set the ether_vlantag in the mbuf packet header
490 		 * to our vlan tag.  We also set the M_VLANTAG flag in the
491 		 * mbuf to let the parent driver know that the ether_vlantag
492 		 * is really valid.
493 		 */
494 		m->m_pkthdr.ether_vlantag = ifv->ifv_tag;
495 		m->m_flags |= M_VLANTAG;
496 
497 		nmp = &m->m_hdr.mh_netmsg;
498 		nmsg = &nmp->nm_netmsg;
499 
500 		netmsg_init(nmsg, &netisr_apanic_rport, 0, vlan_start_dispatch);
501 		nmp->nm_packet = m;
502 		nmsg->nm_lmsg.u.ms_resultp = ifp_p;
503 
504 		port = cpu_portfn(ifp_p->if_index % ncpus /* XXX */);
505 		lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
506 		ifp->if_opackets++;
507 	}
508 }
509 
510 static void
511 vlan_input(struct mbuf *m)
512 {
513 	struct ifvlan *ifv = NULL;
514 	struct ifnet *rcvif;
515 	struct vlan_trunk *vlantrunks;
516 	struct vlan_entry *entry;
517 
518 	rcvif = m->m_pkthdr.rcvif;
519 	KKASSERT(m->m_flags & M_VLANTAG);
520 
521 	vlantrunks = rcvif->if_vlantrunks;
522 	if (vlantrunks == NULL) {
523 		rcvif->if_noproto++;
524 		m_freem(m);
525 		return;
526 	}
527 
528 	crit_enter();	/* XXX Necessary? */
529 	LIST_FOREACH(entry, &vlantrunks[mycpuid].vlan_list, ifv_link) {
530 		if (entry->ifv->ifv_tag ==
531 		    EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag)) {
532 			ifv = entry->ifv;
533 			break;
534 		}
535 	}
536 	crit_exit();
537 
538 	/*
539 	 * Packet is discarded if:
540 	 * - no corresponding vlan(4) interface
541 	 * - vlan(4) interface has not been completely set up yet,
542 	 *   or is being destroyed (ifv->ifv_p != rcvif)
543 	 */
544 	if (ifv == NULL || ifv->ifv_p != rcvif) {
545 		rcvif->if_noproto++;
546 		m_freem(m);
547 		return;
548 	}
549 
550 	/*
551 	 * Clear M_VLANTAG, before the packet is handed to
552 	 * vlan(4) interface
553 	 */
554 	m->m_flags &= ~M_VLANTAG;
555 
556 	ether_reinput_oncpu(&ifv->ifv_if, m, 1);
557 }
558 
559 static void
560 vlan_link_dispatch(struct netmsg *nmsg)
561 {
562 	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
563 	struct ifvlan *ifv = vmsg->nv_ifv;
564 	struct ifnet *ifp_p = vmsg->nv_ifp_p;
565 	struct vlan_entry *entry;
566 	struct vlan_trunk *vlantrunks, *trunk;
567 	int cpu = mycpuid;
568 
569 	vlantrunks = ifp_p->if_vlantrunks;
570 	KASSERT(vlantrunks != NULL,
571 		("vlan trunk has not been initialized yet\n"));
572 
573 	entry = &ifv->ifv_entries[cpu];
574 	trunk = &vlantrunks[cpu];
575 
576 	crit_enter();
577 	LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link);
578 	crit_exit();
579 
580 	ifnet_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
581 }
582 
583 static void
584 vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p)
585 {
586 	struct netmsg_vlan vmsg;
587 	struct netmsg *nmsg;
588 
589 	/* Assert in netisr0 */
590 	ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
591 
592 	if (ifp_p->if_vlantrunks == NULL) {
593 		struct vlan_trunk *vlantrunks;
594 		int i;
595 
596 		vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN,
597 				     M_WAITOK | M_ZERO);
598 		for (i = 0; i < ncpus; ++i)
599 			LIST_INIT(&vlantrunks[i].vlan_list);
600 
601 		ifp_p->if_vlantrunks = vlantrunks;
602 	}
603 
604 	bzero(&vmsg, sizeof(vmsg));
605 	nmsg = &vmsg.nv_nmsg;
606 
607 	netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_link_dispatch);
608 	vmsg.nv_ifv = ifv;
609 	vmsg.nv_ifp_p = ifp_p;
610 
611 	ifnet_domsg(&nmsg->nm_lmsg, 0);
612 }
613 
614 static void
615 vlan_config_dispatch(struct netmsg *nmsg)
616 {
617 	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
618 	struct ifvlan *ifv;
619 	struct ifnet *ifp_p, *ifp;
620 	struct sockaddr_dl *sdl1, *sdl2;
621 	int error;
622 
623 	/* Assert in netisr0 */
624 
625 	ifp_p = ifunit(vmsg->nv_parent_name);
626 	if (ifp_p == NULL) {
627 		error = ENOENT;
628 		goto reply;
629 	}
630 
631 	if (ifp_p->if_data.ifi_type != IFT_ETHER) {
632 		error = EPROTONOSUPPORT;
633 		goto reply;
634 	}
635 
636 	ifv = vmsg->nv_ifv;
637 	ifp = &ifv->ifv_if;
638 
639 	if (ifv->ifv_p) {
640 		error = EBUSY;
641 		goto reply;
642 	}
643 
644 	/* Link vlan into parent's vlantrunk */
645 	vlan_link(ifv, ifp_p);
646 
647 	lwkt_serialize_enter(ifp->if_serializer);
648 
649 	ifv->ifv_tag = vmsg->nv_vlantag;
650 	if (ifp_p->if_capenable & IFCAP_VLAN_MTU)
651 		ifp->if_mtu = ifp_p->if_mtu;
652 	else
653 		ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN;
654 
655 	/*
656 	 * Copy only a selected subset of flags from the parent.
657 	 * Other flags are none of our business.
658 	 */
659 #define VLAN_INHERIT_FLAGS	(IFF_BROADCAST | IFF_MULTICAST | \
660 				 IFF_SIMPLEX | IFF_POINTOPOINT)
661 
662 	ifp->if_flags &= ~VLAN_INHERIT_FLAGS;
663 	ifp->if_flags |= (ifp_p->if_flags & VLAN_INHERIT_FLAGS);
664 
665 #undef VLAN_INHERIT_FLAGS
666 
667 	/*
668 	 * Set up our ``Ethernet address'' to reflect the underlying
669 	 * physical interface's.
670 	 */
671 	sdl1 = IF_LLSOCKADDR(ifp);
672 	sdl2 = IF_LLSOCKADDR(ifp_p);
673 	sdl1->sdl_type = IFT_ETHER;
674 	sdl1->sdl_alen = ETHER_ADDR_LEN;
675 	bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
676 	bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
677 
678 	/*
679 	 * Release vlan's serializer before reprogramming parent's
680 	 * multicast filter to avoid possible dead lock.
681 	 */
682 	lwkt_serialize_exit(ifp->if_serializer);
683 
684 	/*
685 	 * Configure multicast addresses that may already be
686 	 * joined on the vlan device.
687 	 */
688 	vlan_setmulti(ifv, ifp_p);
689 
690 	/*
691 	 * Set flags on the parent, if necessary.
692 	 */
693 	vlan_setflags(ifv, ifp_p, 1);
694 
695 	/*
696 	 * Connect to parent after everything have been set up,
697 	 * so input/output could know that vlan is ready to go
698 	 */
699 	ifv->ifv_p = ifp_p;
700 	error = 0;
701 reply:
702 	lwkt_replymsg(&nmsg->nm_lmsg, error);
703 }
704 
705 static int
706 vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag)
707 {
708 	struct netmsg_vlan vmsg;
709 	struct netmsg *nmsg;
710 
711 	ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
712 
713 	bzero(&vmsg, sizeof(vmsg));
714 	nmsg = &vmsg.nv_nmsg;
715 
716 	netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_config_dispatch);
717 	vmsg.nv_ifv = ifv;
718 	vmsg.nv_parent_name = parent_name;
719 	vmsg.nv_vlantag = vlantag;
720 
721 	return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
722 }
723 
724 static void
725 vlan_unlink_dispatch(struct netmsg *nmsg)
726 {
727 	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
728 	struct ifvlan *ifv = vmsg->nv_ifv;
729 	struct vlan_entry *entry;
730 	int cpu = mycpuid;
731 
732 	KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL,
733 		("vlan trunk has not been initialized yet\n"));
734 	entry = &ifv->ifv_entries[cpu];
735 
736 	crit_enter();
737 	LIST_REMOVE(entry, ifv_link);
738 	crit_exit();
739 
740 	ifnet_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
741 }
742 
743 static void
744 vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p)
745 {
746 	struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks;
747 	struct netmsg_vlan vmsg;
748 	struct netmsg *nmsg;
749 
750 	/* Assert in netisr0 */
751 	ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
752 
753 	KASSERT(ifp_p->if_vlantrunks != NULL,
754 		("vlan trunk has not been initialized yet\n"));
755 
756 	bzero(&vmsg, sizeof(vmsg));
757 	nmsg = &vmsg.nv_nmsg;
758 
759 	netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_unlink_dispatch);
760 	vmsg.nv_ifv = ifv;
761 	vmsg.nv_ifp_p = ifp_p;
762 
763 	ifnet_domsg(&nmsg->nm_lmsg, 0);
764 
765 	crit_enter();
766 	if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) {
767 		ifp_p->if_vlantrunks = NULL;
768 
769 		/*
770 		 * Make that all protocol threads see if_vlantrunks change.
771 		 */
772 		netmsg_service_sync();
773 		kfree(vlantrunks, M_VLAN);
774 	}
775 	crit_exit();
776 }
777 
778 static void
779 vlan_unconfig_dispatch(struct netmsg *nmsg)
780 {
781 	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
782 	struct sockaddr_dl *sdl;
783 	struct ifvlan *ifv;
784 	struct ifnet *ifp_p, *ifp;
785 	int error;
786 
787 	/* Assert in netisr0 */
788 
789 	ifv = vmsg->nv_ifv;
790 	ifp = &ifv->ifv_if;
791 
792 	if (ifp->if_flags & IFF_UP)
793 		if_down(ifp);
794 
795 	lwkt_serialize_enter(ifp->if_serializer);
796 
797 	ifp->if_flags &= ~IFF_RUNNING;
798 
799 	/*
800 	 * Save parent ifnet pointer and disconnect from parent.
801 	 *
802 	 * This is done early in this function, so input/output could
803 	 * know that we are disconnecting.
804 	 */
805 	ifp_p = ifv->ifv_p;
806 	ifv->ifv_p = NULL;
807 
808 	/*
809 	 * Release vlan's serializer before reprogramming parent's
810 	 * multicast filter to avoid possible dead lock.
811 	 */
812 	lwkt_serialize_exit(ifp->if_serializer);
813 
814 	if (ifp_p) {
815 		/*
816 		 * Since the interface is being unconfigured, we need to
817 		 * empty the list of multicast groups that we may have joined
818 		 * while we were alive from the parent's list.
819 		 */
820 		vlan_clrmulti(ifv, ifp_p);
821 
822 		/* Clear parent's flags which was set by us. */
823 		vlan_setflags(ifv, ifp_p, 0);
824 	}
825 
826 	lwkt_serialize_enter(ifp->if_serializer);
827 
828 	ifp->if_mtu = ETHERMTU;
829 
830 	/* Clear our MAC address. */
831 	sdl = IF_LLSOCKADDR(ifp);
832 	sdl->sdl_type = IFT_ETHER;
833 	sdl->sdl_alen = ETHER_ADDR_LEN;
834 	bzero(LLADDR(sdl), ETHER_ADDR_LEN);
835 	bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
836 
837 	lwkt_serialize_exit(ifp->if_serializer);
838 
839 	/* Unlink vlan from parent's vlantrunk */
840 	if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL)
841 		vlan_unlink(ifv, ifp_p);
842 
843 	error = 0;
844 	lwkt_replymsg(&nmsg->nm_lmsg, error);
845 }
846 
847 static int
848 vlan_unconfig(struct ifvlan *ifv)
849 {
850 	struct netmsg_vlan vmsg;
851 	struct netmsg *nmsg;
852 
853 	ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
854 
855 	bzero(&vmsg, sizeof(vmsg));
856 	nmsg = &vmsg.nv_nmsg;
857 
858 	netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_unconfig_dispatch);
859 	vmsg.nv_ifv = ifv;
860 
861 	return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
862 }
863 
864 static int
865 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
866 {
867 	struct ifvlan *ifv = ifp->if_softc;
868 	struct ifreq *ifr = (struct ifreq *)data;
869 	struct ifnet *ifp_p;
870 	struct vlanreq vlr;
871 	int error = 0;
872 
873 	ASSERT_SERIALIZED(ifp->if_serializer);
874 
875 	switch (cmd) {
876 	case SIOCGIFMEDIA:
877 		ifp_p = ifv->ifv_p;
878 		if (ifp_p != NULL) {
879 			/*
880 			 * Release vlan interface's serializer to void
881 			 * possible dead lock.
882 			 */
883 			lwkt_serialize_exit(ifp->if_serializer);
884 
885 			lwkt_serialize_enter(ifp_p->if_serializer);
886 			error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr);
887 			lwkt_serialize_exit(ifp_p->if_serializer);
888 
889 			lwkt_serialize_enter(ifp->if_serializer);
890 
891 			if (ifv->ifv_p == NULL && ifv->ifv_p != ifp_p) {
892 				/*
893 				 * We are disconnected from the original
894 				 * parent interface or the parent interface
895 				 * is changed, after vlan interface's
896 				 * serializer is released.
897 				 */
898 				error = EINVAL;
899 			}
900 
901 			/* Limit the result to the parent's current config. */
902 			if (error == 0) {
903 				struct ifmediareq *ifmr;
904 
905 				ifmr = (struct ifmediareq *) data;
906 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
907 					ifmr->ifm_count = 1;
908 					error = copyout(&ifmr->ifm_current,
909 						ifmr->ifm_ulist,
910 						sizeof(int));
911 				}
912 			}
913 		} else {
914 			error = EINVAL;
915 		}
916 		break;
917 
918 	case SIOCSIFMEDIA:
919 		error = EINVAL;
920 		break;
921 
922 	case SIOCSETVLAN:
923 		error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
924 		if (error)
925 			break;
926 
927 		lwkt_serialize_exit(ifp->if_serializer);
928 		if (vlr.vlr_parent[0] == '\0')
929 			error = vlan_unconfig(ifv);
930 		else
931 			error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag);
932 		lwkt_serialize_enter(ifp->if_serializer);
933 		break;
934 
935 	case SIOCGETVLAN:
936 		bzero(&vlr, sizeof(vlr));
937 		if (ifv->ifv_p) {
938 			strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
939 			    sizeof(vlr.vlr_parent));
940 			vlr.vlr_tag = ifv->ifv_tag;
941 		}
942 		error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
943 		break;
944 
945 	case SIOCSIFFLAGS:
946 		if (ifp->if_flags & IFF_UP)
947 			ifp->if_init(ifp);
948 		else
949 			ifp->if_flags &= ~IFF_RUNNING;
950 
951 		/*
952 		 * We should propagate selected flags to the parent,
953 		 * e.g., promiscuous mode.
954 		 */
955 		lwkt_serialize_exit(ifp->if_serializer);
956 		error = vlan_config_flags(ifv);
957 		lwkt_serialize_enter(ifp->if_serializer);
958 		break;
959 
960 	case SIOCADDMULTI:
961 	case SIOCDELMULTI:
962 		lwkt_serialize_exit(ifp->if_serializer);
963 		error = vlan_config_multi(ifv);
964 		lwkt_serialize_enter(ifp->if_serializer);
965 		break;
966 
967 	default:
968 		error = ether_ioctl(ifp, cmd, data);
969 		break;
970 	}
971 	return error;
972 }
973 
974 static void
975 vlan_multi_dispatch(struct netmsg *nmsg)
976 {
977 	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
978 	struct ifvlan *ifv = vmsg->nv_ifv;
979 	int error = 0;
980 
981 	/*
982 	 * If we don't have a parent, just remember the membership for
983 	 * when we do.
984 	 */
985 	if (ifv->ifv_p != NULL)
986 		error = vlan_setmulti(ifv, ifv->ifv_p);
987 	lwkt_replymsg(&nmsg->nm_lmsg, error);
988 }
989 
990 static int
991 vlan_config_multi(struct ifvlan *ifv)
992 {
993 	struct netmsg_vlan vmsg;
994 	struct netmsg *nmsg;
995 
996 	ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
997 
998 	bzero(&vmsg, sizeof(vmsg));
999 	nmsg = &vmsg.nv_nmsg;
1000 
1001 	netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_multi_dispatch);
1002 	vmsg.nv_ifv = ifv;
1003 
1004 	return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
1005 }
1006 
1007 static void
1008 vlan_flags_dispatch(struct netmsg *nmsg)
1009 {
1010 	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
1011 	struct ifvlan *ifv = vmsg->nv_ifv;
1012 	int error = 0;
1013 
1014 	/*
1015 	 * If we don't have a parent, just remember the flags for
1016 	 * when we do.
1017 	 */
1018 	if (ifv->ifv_p != NULL)
1019 		error = vlan_setflags(ifv, ifv->ifv_p, 1);
1020 	lwkt_replymsg(&nmsg->nm_lmsg, error);
1021 }
1022 
1023 static int
1024 vlan_config_flags(struct ifvlan *ifv)
1025 {
1026 	struct netmsg_vlan vmsg;
1027 	struct netmsg *nmsg;
1028 
1029 	ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
1030 
1031 	bzero(&vmsg, sizeof(vmsg));
1032 	nmsg = &vmsg.nv_nmsg;
1033 
1034 	netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_flags_dispatch);
1035 	vmsg.nv_ifv = ifv;
1036 
1037 	return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
1038 }
1039