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