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