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