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