- Fix a possible race between getting vlan media and changing vlan parent
[dragonfly.git] / sys / net / vlan / if_vlan.c
blob814d3f9fcbf61fb1de355454813d61b5c39671ce
1 /*
2 * Copyright 1998 Massachusetts Institute of Technology
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.
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.
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.34 2008/06/15 11:41:40 sephe Exp $
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.
45 #ifndef NVLAN
46 #include "use_vlan.h"
47 #endif
48 #include "opt_inet.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/module.h>
56 #include <sys/queue.h>
57 #include <sys/socket.h>
58 #include <sys/sockio.h>
59 #include <sys/sysctl.h>
60 #include <sys/bus.h>
61 #include <sys/thread2.h>
63 #include <net/bpf.h>
64 #include <net/ethernet.h>
65 #include <net/if.h>
66 #include <net/if_arp.h>
67 #include <net/if_dl.h>
68 #include <net/if_types.h>
69 #include <net/ifq_var.h>
70 #include <net/if_clone.h>
71 #include <net/netmsg2.h>
73 #ifdef INET
74 #include <netinet/in.h>
75 #include <netinet/if_ether.h>
76 #endif
78 #include <net/vlan/if_vlan_var.h>
79 #include <net/vlan/if_vlan_ether.h>
81 struct ifvlan;
83 struct vlan_mc_entry {
84 struct ether_addr mc_addr;
85 SLIST_ENTRY(vlan_mc_entry) mc_entries;
88 struct vlan_entry {
89 struct ifvlan *ifv;
90 LIST_ENTRY(vlan_entry) ifv_link;
93 struct ifvlan {
94 struct arpcom ifv_ac; /* make this an interface */
95 struct ifnet *ifv_p; /* parent inteface of this vlan */
96 struct ifv_linkmib {
97 int ifvm_parent;
98 uint16_t ifvm_proto; /* encapsulation ethertype */
99 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
100 } ifv_mib;
101 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
102 LIST_ENTRY(ifvlan) ifv_list;
103 struct vlan_entry ifv_entries[1];
105 #define ifv_if ifv_ac.ac_if
106 #define ifv_tag ifv_mib.ifvm_tag
108 struct vlan_trunk {
109 LIST_HEAD(, vlan_entry) vlan_list;
112 struct netmsg_vlan {
113 struct netmsg nv_nmsg;
114 struct ifvlan *nv_ifv;
115 struct ifnet *nv_ifp_p;
116 const char *nv_parent_name;
117 uint16_t nv_vlantag;
120 #define VLANNAME "vlan"
122 SYSCTL_DECL(_net_link);
123 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
124 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
126 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
127 static LIST_HEAD(, ifvlan) ifv_list;
129 static int vlan_clone_create(struct if_clone *, int);
130 static void vlan_clone_destroy(struct ifnet *);
131 static void vlan_ifdetach(void *, struct ifnet *);
133 static void vlan_init(void *);
134 static void vlan_start(struct ifnet *);
135 static int vlan_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
137 static int vlan_input(struct mbuf *m, struct mbuf_chain *);
139 static void vlan_clrmulti(struct ifvlan *, struct ifnet *);
140 static int vlan_setmulti(struct ifvlan *, struct ifnet *);
141 static int vlan_config_multi(struct ifvlan *);
142 static int vlan_config(struct ifvlan *, const char *, uint16_t);
143 static int vlan_unconfig(struct ifvlan *);
144 static void vlan_link(struct ifvlan *, struct ifnet *);
145 static void vlan_unlink(struct ifvlan *, struct ifnet *);
147 static void vlan_config_dispatch(struct netmsg *);
148 static void vlan_unconfig_dispatch(struct netmsg *);
149 static void vlan_link_dispatch(struct netmsg *);
150 static void vlan_unlink_dispatch(struct netmsg *);
151 static void vlan_multi_dispatch(struct netmsg *);
152 static void vlan_ifdetach_dispatch(struct netmsg *);
154 static eventhandler_tag vlan_ifdetach_cookie;
155 static struct if_clone vlan_cloner =
156 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy,
157 NVLAN, IF_MAXUNIT);
159 static __inline void
160 vlan_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
162 if (next_cpu < ncpus)
163 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
164 else
165 lwkt_replymsg(lmsg, 0);
169 * Program our multicast filter. What we're actually doing is
170 * programming the multicast filter of the parent. This has the
171 * side effect of causing the parent interface to receive multicast
172 * traffic that it doesn't really want, which ends up being discarded
173 * later by the upper protocol layers. Unfortunately, there's no way
174 * to avoid this: there really is only one physical interface.
176 static int
177 vlan_setmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
179 struct ifmultiaddr *ifma, *rifma = NULL;
180 struct vlan_mc_entry *mc = NULL;
181 struct sockaddr_dl sdl;
182 struct ifnet *ifp = &ifv->ifv_if;
184 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
187 * First, remove any existing filter entries.
189 vlan_clrmulti(ifv, ifp_p);
192 * Now program new ones.
194 bzero(&sdl, sizeof(sdl));
195 sdl.sdl_len = sizeof(sdl);
196 sdl.sdl_family = AF_LINK;
197 sdl.sdl_index = ifp_p->if_index;
198 sdl.sdl_type = IFT_ETHER;
199 sdl.sdl_alen = ETHER_ADDR_LEN;
201 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
202 int error;
204 if (ifma->ifma_addr->sa_family != AF_LINK)
205 continue;
207 /* Save a copy */
208 mc = kmalloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
209 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
210 &mc->mc_addr, ETHER_ADDR_LEN);
211 SLIST_INSERT_HEAD(&ifv->vlan_mc_listhead, mc, mc_entries);
213 /* Program the parent multicast filter */
214 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
215 LLADDR(&sdl), ETHER_ADDR_LEN);
216 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
217 if (error)
218 return error;
220 return 0;
223 static void
224 vlan_clrmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
226 struct vlan_mc_entry *mc;
227 struct sockaddr_dl sdl;
229 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
231 bzero(&sdl, sizeof(sdl));
232 sdl.sdl_len = sizeof(sdl);
233 sdl.sdl_family = AF_LINK;
234 sdl.sdl_index = ifp_p->if_index;
235 sdl.sdl_type = IFT_ETHER;
236 sdl.sdl_alen = ETHER_ADDR_LEN;
238 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
239 bcopy(&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
240 if_delmulti(ifp_p, (struct sockaddr *)&sdl); /* ignore error */
242 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
243 kfree(mc, M_VLAN);
247 static int
248 vlan_modevent(module_t mod, int type, void *data)
250 switch (type) {
251 case MOD_LOAD:
252 LIST_INIT(&ifv_list);
253 vlan_input_p = vlan_input;
254 vlan_ifdetach_cookie =
255 EVENTHANDLER_REGISTER(ifnet_detach_event,
256 vlan_ifdetach, NULL,
257 EVENTHANDLER_PRI_ANY);
258 if_clone_attach(&vlan_cloner);
259 break;
261 case MOD_UNLOAD:
262 if_clone_detach(&vlan_cloner);
263 vlan_input_p = NULL;
264 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
265 vlan_ifdetach_cookie);
266 while (!LIST_EMPTY(&ifv_list))
267 vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
268 break;
270 return 0;
273 static moduledata_t vlan_mod = {
274 "if_vlan",
275 vlan_modevent,
279 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
281 static void
282 vlan_ifdetach_dispatch(struct netmsg *nmsg)
284 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
285 struct ifnet *ifp_p = vmsg->nv_ifp_p;
286 struct vlan_trunk *vlantrunks, *trunk;
287 struct vlan_entry *ifve;
289 vlantrunks = ifp_p->if_vlantrunks;
290 if (vlantrunks == NULL)
291 goto reply;
292 trunk = &vlantrunks[mycpuid];
294 while (ifp_p->if_vlantrunks &&
295 (ifve = LIST_FIRST(&trunk->vlan_list)) != NULL)
296 vlan_unconfig(ifve->ifv);
297 reply:
298 lwkt_replymsg(&nmsg->nm_lmsg, 0);
301 static void
302 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
304 struct netmsg_vlan vmsg;
305 struct netmsg *nmsg;
307 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
309 bzero(&vmsg, sizeof(vmsg));
310 nmsg = &vmsg.nv_nmsg;
312 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_ifdetach_dispatch);
313 vmsg.nv_ifp_p = ifp;
315 lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
318 static int
319 vlan_clone_create(struct if_clone *ifc, int unit)
321 struct ifvlan *ifv;
322 struct ifnet *ifp;
323 int vlan_size, i;
325 vlan_size = sizeof(struct ifvlan)
326 + ((ncpus - 1) * sizeof(struct vlan_entry));
327 ifv = kmalloc(vlan_size, M_VLAN, M_WAITOK | M_ZERO);
328 SLIST_INIT(&ifv->vlan_mc_listhead);
329 for (i = 0; i < ncpus; ++i)
330 ifv->ifv_entries[i].ifv = ifv;
332 crit_enter(); /* XXX not MP safe */
333 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
334 crit_exit();
336 ifp = &ifv->ifv_if;
337 ifp->if_softc = ifv;
338 if_initname(ifp, "vlan", unit);
339 /* NB: flags are not set here */
340 ifp->if_linkmib = &ifv->ifv_mib;
341 ifp->if_linkmiblen = sizeof ifv->ifv_mib;
342 /* NB: mtu is not set here */
344 ifp->if_init = vlan_init;
345 ifp->if_start = vlan_start;
346 ifp->if_ioctl = vlan_ioctl;
347 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
348 ifq_set_ready(&ifp->if_snd);
349 ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr, NULL);
350 /* Now undo some of the damage... */
351 ifp->if_data.ifi_type = IFT_L2VLAN;
352 ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
354 return (0);
357 static void
358 vlan_clone_destroy(struct ifnet *ifp)
360 struct ifvlan *ifv = ifp->if_softc;
362 crit_enter(); /* XXX not MP safe */
363 LIST_REMOVE(ifv, ifv_list);
364 crit_exit();
366 vlan_unconfig(ifv);
367 ether_ifdetach(ifp);
369 kfree(ifv, M_VLAN);
372 static void
373 vlan_init(void *xsc)
375 struct ifvlan *ifv = xsc;
376 struct ifnet *ifp = &ifv->ifv_if;
378 ASSERT_SERIALIZED(ifp->if_serializer);
380 if (ifv->ifv_p != NULL)
381 ifp->if_flags |= IFF_RUNNING;
384 static void
385 vlan_start(struct ifnet *ifp)
387 struct ifvlan *ifv = ifp->if_softc;
388 struct ifnet *ifp_p = ifv->ifv_p;
389 struct mbuf *m;
391 ASSERT_SERIALIZED(ifp->if_serializer);
393 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifp_p == NULL)
394 return;
396 ifp->if_flags |= IFF_OACTIVE;
397 for (;;) {
398 struct netmsg_packet *nmp;
399 struct netmsg *nmsg;
400 struct lwkt_port *port;
402 m = ifq_dequeue(&ifp->if_snd, NULL);
403 if (m == NULL)
404 break;
405 BPF_MTAP(ifp, m);
408 * Do not run parent's if_start() if the parent is not up,
409 * or parent's driver will cause a system crash.
411 if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) !=
412 (IFF_UP | IFF_RUNNING)) {
413 m_freem(m);
414 ifp->if_data.ifi_collisions++;
415 continue;
419 * We need some way to tell the interface where the packet
420 * came from so that it knows how to find the VLAN tag to
421 * use, so we set the ether_vlantag in the mbuf packet header
422 * to our vlan tag. We also set the M_VLANTAG flag in the
423 * mbuf to let the parent driver know that the ether_vlantag
424 * is really valid.
426 m->m_pkthdr.ether_vlantag = ifv->ifv_tag;
427 m->m_flags |= M_VLANTAG;
429 nmp = &m->m_hdr.mh_netmsg;
430 nmsg = &nmp->nm_netmsg;
432 netmsg_init(nmsg, &netisr_apanic_rport, 0, vlan_start_dispatch);
433 nmp->nm_packet = m;
434 nmsg->nm_lmsg.u.ms_resultp = ifp_p;
436 port = cpu_portfn(ifp_p->if_index % ncpus /* XXX */);
437 lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
438 ifp->if_opackets++;
440 ifp->if_flags &= ~IFF_OACTIVE;
443 static int
444 vlan_input(struct mbuf *m, struct mbuf_chain *chain)
446 struct ifvlan *ifv = NULL;
447 struct ifnet *rcvif;
448 struct vlan_trunk *vlantrunks;
449 struct vlan_entry *entry;
451 rcvif = m->m_pkthdr.rcvif;
452 ASSERT_SERIALIZED(rcvif->if_serializer);
453 KKASSERT(m->m_flags & M_VLANTAG);
455 vlantrunks = rcvif->if_vlantrunks;
456 if (vlantrunks == NULL) {
457 rcvif->if_noproto++;
458 m_freem(m);
459 return -1;
462 crit_enter();
463 LIST_FOREACH(entry, &vlantrunks[mycpuid].vlan_list, ifv_link) {
464 if (entry->ifv->ifv_tag ==
465 EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag)) {
466 ifv = entry->ifv;
467 break;
470 crit_exit();
473 * Packet is discarded if:
474 * - no corresponding vlan(4) interface
475 * - vlan(4) interface has not been completely set up yet,
476 * or is being destroyed (ifv->ifv_p != rcvif)
477 * - vlan(4) interface is not brought up
479 if (ifv == NULL || ifv->ifv_p != rcvif ||
480 (ifv->ifv_if.if_flags & IFF_UP) == 0) {
481 rcvif->if_noproto++;
482 m_freem(m);
483 return -1; /* so ether_input can take note */
487 * Clear M_VLANTAG, before the packet is handed to
488 * vlan(4) interface
490 m->m_flags &= ~M_VLANTAG;
492 ifv->ifv_if.if_ipackets++;
493 lwkt_serialize_exit(rcvif->if_serializer);
494 lwkt_serialize_enter(ifv->ifv_if.if_serializer);
495 ether_input_chain(&ifv->ifv_if, m, chain);
496 lwkt_serialize_exit(ifv->ifv_if.if_serializer);
497 lwkt_serialize_enter(rcvif->if_serializer);
498 return 0;
501 static void
502 vlan_link_dispatch(struct netmsg *nmsg)
504 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
505 struct ifvlan *ifv = vmsg->nv_ifv;
506 struct ifnet *ifp_p = vmsg->nv_ifp_p;
507 struct vlan_entry *entry;
508 struct vlan_trunk *vlantrunks, *trunk;
509 int cpu = mycpuid;
511 vlantrunks = ifp_p->if_vlantrunks;
512 KASSERT(vlantrunks != NULL,
513 ("vlan trunk has not been initialized yet\n"));
515 entry = &ifv->ifv_entries[cpu];
516 trunk = &vlantrunks[cpu];
518 crit_enter();
519 LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link);
520 crit_exit();
522 vlan_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
525 static void
526 vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p)
528 struct netmsg_vlan vmsg;
529 struct netmsg *nmsg;
531 /* Assert in netisr0 */
532 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
534 if (ifp_p->if_vlantrunks == NULL) {
535 struct vlan_trunk *vlantrunks;
536 int i;
538 vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN,
539 M_WAITOK | M_ZERO);
540 for (i = 0; i < ncpus; ++i)
541 LIST_INIT(&vlantrunks[i].vlan_list);
543 ifp_p->if_vlantrunks = vlantrunks;
546 bzero(&vmsg, sizeof(vmsg));
547 nmsg = &vmsg.nv_nmsg;
549 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_link_dispatch);
550 vmsg.nv_ifv = ifv;
551 vmsg.nv_ifp_p = ifp_p;
553 lwkt_domsg(ifnet_portfn(0), &nmsg->nm_lmsg, 0);
556 static void
557 vlan_config_dispatch(struct netmsg *nmsg)
559 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
560 struct ifvlan *ifv;
561 struct ifnet *ifp_p, *ifp;
562 struct sockaddr_dl *sdl1, *sdl2;
563 int error;
565 /* Assert in netisr0 */
567 ifp_p = ifunit(vmsg->nv_parent_name);
568 if (ifp_p == NULL) {
569 error = ENOENT;
570 goto reply;
573 if (ifp_p->if_data.ifi_type != IFT_ETHER) {
574 error = EPROTONOSUPPORT;
575 goto reply;
578 ifv = vmsg->nv_ifv;
579 ifp = &ifv->ifv_if;
581 if (ifv->ifv_p) {
582 error = EBUSY;
583 goto reply;
586 /* Link vlan into parent's vlantrunk */
587 vlan_link(ifv, ifp_p);
589 lwkt_serialize_enter(ifp->if_serializer);
591 ifv->ifv_tag = vmsg->nv_vlantag;
592 if (ifp_p->if_capenable & IFCAP_VLAN_MTU)
593 ifp->if_mtu = ifp_p->if_mtu;
594 else
595 ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN;
598 * Copy only a selected subset of flags from the parent.
599 * Other flags are none of our business.
601 ifp->if_flags = (ifp_p->if_flags &
602 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT));
605 * Set up our ``Ethernet address'' to reflect the underlying
606 * physical interface's.
608 sdl1 = IF_LLSOCKADDR(ifp);
609 sdl2 = IF_LLSOCKADDR(ifp_p);
610 sdl1->sdl_type = IFT_ETHER;
611 sdl1->sdl_alen = ETHER_ADDR_LEN;
612 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
613 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
616 * Release vlan's serializer before reprogramming parent's
617 * multicast filter to avoid possible dead lock.
619 lwkt_serialize_exit(ifp->if_serializer);
622 * Configure multicast addresses that may already be
623 * joined on the vlan device.
625 vlan_setmulti(ifv, ifp_p);
628 * Connect to parent after everything have been set up,
629 * so input/output could know that vlan is ready to go
631 ifv->ifv_p = ifp_p;
632 error = 0;
633 reply:
634 lwkt_replymsg(&nmsg->nm_lmsg, error);
637 static int
638 vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag)
640 struct netmsg_vlan vmsg;
641 struct netmsg *nmsg;
643 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
645 bzero(&vmsg, sizeof(vmsg));
646 nmsg = &vmsg.nv_nmsg;
648 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_config_dispatch);
649 vmsg.nv_ifv = ifv;
650 vmsg.nv_parent_name = parent_name;
651 vmsg.nv_vlantag = vlantag;
653 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
656 static void
657 vlan_unlink_dispatch(struct netmsg *nmsg)
659 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
660 struct ifvlan *ifv = vmsg->nv_ifv;
661 struct vlan_entry *entry;
662 int cpu = mycpuid;
664 KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL,
665 ("vlan trunk has not been initialized yet\n"));
666 entry = &ifv->ifv_entries[cpu];
668 crit_enter();
669 LIST_REMOVE(entry, ifv_link);
670 crit_exit();
672 vlan_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
675 static void
676 vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p)
678 struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks;
679 struct netmsg_vlan vmsg;
680 struct netmsg *nmsg;
682 /* Assert in netisr0 */
683 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
685 KASSERT(ifp_p->if_vlantrunks != NULL,
686 ("vlan trunk has not been initialized yet\n"));
688 bzero(&vmsg, sizeof(vmsg));
689 nmsg = &vmsg.nv_nmsg;
691 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_unlink_dispatch);
692 vmsg.nv_ifv = ifv;
693 vmsg.nv_ifp_p = ifp_p;
695 lwkt_domsg(ifnet_portfn(0), &nmsg->nm_lmsg, 0);
697 crit_enter();
698 if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) {
699 #ifdef notyet
700 ifp_p->if_vlantrunks = NULL;
701 netmsg_service_sync();
702 kfree(vlantrunks, M_VLAN);
703 #else
704 lwkt_serialize_enter(ifp_p->if_serializer);
705 kfree(ifp_p->if_vlantrunks, M_VLAN);
706 ifp_p->if_vlantrunks = NULL;
707 lwkt_serialize_exit(ifp_p->if_serializer);
708 #endif
710 crit_exit();
713 static void
714 vlan_unconfig_dispatch(struct netmsg *nmsg)
716 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
717 struct sockaddr_dl *sdl;
718 struct ifvlan *ifv;
719 struct ifnet *ifp_p, *ifp;
720 int error;
722 /* Assert in netisr0 */
724 ifv = vmsg->nv_ifv;
725 ifp = &ifv->ifv_if;
727 if (ifp->if_flags & IFF_UP)
728 if_down(ifp);
730 lwkt_serialize_enter(ifp->if_serializer);
732 ifp->if_flags &= ~IFF_RUNNING;
735 * Save parent ifnet pointer and disconnect from parent.
737 * This is done early in this function, so input/output could
738 * know that we are disconnecting.
740 ifp_p = ifv->ifv_p;
741 ifv->ifv_p = NULL;
744 * Release vlan's serializer before reprogramming parent's
745 * multicast filter to avoid possible dead lock.
747 lwkt_serialize_exit(ifp->if_serializer);
749 if (ifp_p) {
751 * Since the interface is being unconfigured, we need to
752 * empty the list of multicast groups that we may have joined
753 * while we were alive from the parent's list.
755 vlan_clrmulti(ifv, ifp_p);
758 lwkt_serialize_enter(ifp->if_serializer);
760 ifp->if_mtu = ETHERMTU;
762 /* Clear our MAC address. */
763 sdl = IF_LLSOCKADDR(ifp);
764 sdl->sdl_type = IFT_ETHER;
765 sdl->sdl_alen = ETHER_ADDR_LEN;
766 bzero(LLADDR(sdl), ETHER_ADDR_LEN);
767 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
769 lwkt_serialize_exit(ifp->if_serializer);
771 /* Unlink vlan from parent's vlantrunk */
772 if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL)
773 vlan_unlink(ifv, ifp_p);
775 error = 0;
776 lwkt_replymsg(&nmsg->nm_lmsg, error);
779 static int
780 vlan_unconfig(struct ifvlan *ifv)
782 struct netmsg_vlan vmsg;
783 struct netmsg *nmsg;
785 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
787 bzero(&vmsg, sizeof(vmsg));
788 nmsg = &vmsg.nv_nmsg;
790 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_unconfig_dispatch);
791 vmsg.nv_ifv = ifv;
793 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
796 static int
797 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
799 struct ifvlan *ifv = ifp->if_softc;
800 struct ifreq *ifr = (struct ifreq *)data;
801 struct ifnet *ifp_p;
802 struct vlanreq vlr;
803 int error = 0;
805 ASSERT_SERIALIZED(ifp->if_serializer);
807 switch (cmd) {
808 case SIOCGIFMEDIA:
809 ifp_p = ifv->ifv_p;
810 if (ifp_p != NULL) {
812 * Release vlan interface's serializer to void
813 * possible dead lock.
815 lwkt_serialize_exit(ifp->if_serializer);
817 lwkt_serialize_enter(ifp_p->if_serializer);
818 error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr);
819 lwkt_serialize_exit(ifp_p->if_serializer);
821 lwkt_serialize_enter(ifp->if_serializer);
823 if (ifv->ifv_p == NULL && ifv->ifv_p != ifp_p) {
825 * We are disconnected from the original
826 * parent interface or the parent interface
827 * is changed, after vlan interface's
828 * serializer is released.
830 error = EINVAL;
833 /* Limit the result to the parent's current config. */
834 if (error == 0) {
835 struct ifmediareq *ifmr;
837 ifmr = (struct ifmediareq *) data;
838 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
839 ifmr->ifm_count = 1;
840 error = copyout(&ifmr->ifm_current,
841 ifmr->ifm_ulist,
842 sizeof(int));
845 } else {
846 error = EINVAL;
848 break;
850 case SIOCSIFMEDIA:
851 error = EINVAL;
852 break;
854 case SIOCSETVLAN:
855 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
856 if (error)
857 break;
859 lwkt_serialize_exit(ifp->if_serializer);
860 if (vlr.vlr_parent[0] == '\0')
861 error = vlan_unconfig(ifv);
862 else
863 error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag);
864 lwkt_serialize_enter(ifp->if_serializer);
865 break;
867 case SIOCGETVLAN:
868 bzero(&vlr, sizeof(vlr));
869 if (ifv->ifv_p) {
870 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
871 sizeof(vlr.vlr_parent));
872 vlr.vlr_tag = ifv->ifv_tag;
874 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
875 break;
877 case SIOCSIFFLAGS:
878 if (ifp->if_flags & IFF_UP)
879 ifp->if_init(ifp);
880 else
881 ifp->if_flags &= ~IFF_RUNNING;
884 * We don't support promiscuous mode
885 * right now because it would require help from the
886 * underlying drivers, which hasn't been implemented.
888 if (ifr->ifr_flags & IFF_PROMISC) {
889 ifp->if_flags &= ~IFF_PROMISC;
890 error = EINVAL;
892 break;
894 case SIOCADDMULTI:
895 case SIOCDELMULTI:
896 lwkt_serialize_exit(ifp->if_serializer);
897 error = vlan_config_multi(ifv);
898 lwkt_serialize_enter(ifp->if_serializer);
899 break;
901 default:
902 error = ether_ioctl(ifp, cmd, data);
903 break;
905 return error;
908 static void
909 vlan_multi_dispatch(struct netmsg *nmsg)
911 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
912 struct ifvlan *ifv = vmsg->nv_ifv;
913 int error = 0;
916 * If we don't have a parent, just remember the membership for
917 * when we do.
919 if (ifv->ifv_p != NULL)
920 error = vlan_setmulti(ifv, ifv->ifv_p);
921 lwkt_replymsg(&nmsg->nm_lmsg, error);
924 static int
925 vlan_config_multi(struct ifvlan *ifv)
927 struct netmsg_vlan vmsg;
928 struct netmsg *nmsg;
930 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
932 bzero(&vmsg, sizeof(vmsg));
933 nmsg = &vmsg.nv_nmsg;
935 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_multi_dispatch);
936 vmsg.nv_ifv = ifv;
938 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);