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
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
29 * $FreeBSD: src/sys/net/if_vlan.c,v 1.15.2.13 2003/02/14 22:25:58 fenner Exp $
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.
44 * Note about vlan's MP safe approach:
46 * - All configuration operation, e.g. config, unconfig and change flags,
47 * is serialized by netisr0; not by vlan's serializer
49 * - Parent interface's trunk and vlans are linked in the following
52 * +--------------+--------+--------+--------+--------+
53 * | parent ifnet |trunk[0]|trunk[1]|trunk[2]|trunk[3]|
54 * +--------------+--------+--------+--------+--------+
57 * +--------------+--------+--------+--------+--------+
58 * | vlan ifnet |entry[0]|entry[1]|entry[2]|entry[3]|
59 * +--------------+--------+--------+--------+--------+
62 * +--------------+--------+--------+--------+--------+
63 * | vlan ifnet |entry[0]|entry[1]|entry[2]|entry[3]|
64 * +--------------+--------+--------+--------+--------+
66 * - Vlan is linked/unlinked onto parent interface's trunk using following
71 * netisr0 <---------------------------------------------+
76 * V fwdmsg fwdmsg fwdmsg |
77 * ifnet0 --------> ifnet1 --------> ifnet2 --------> ifnet3
78 * (link/unlink) (link/unlink) (link/unlink) (link/unlink)
80 * - Parent interface's trunk is destroyed in the following lockless way:
82 * old_trunk = ifp->if_vlantrunks;
83 * ifp->if_vlantrunks = NULL;
84 * netmsg_service_sync();
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
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>
111 #include <sys/thread2.h>
114 #include <net/ethernet.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>
124 #include <netinet/in.h>
125 #include <netinet/if_ether.h>
128 #include <net/vlan/if_vlan_var.h>
129 #include <net/vlan/if_vlan_ether.h>
133 struct vlan_mc_entry {
134 struct ether_addr mc_addr;
135 SLIST_ENTRY(vlan_mc_entry) mc_entries;
140 LIST_ENTRY(vlan_entry) ifv_link;
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 */
149 uint16_t ifvm_proto; /* encapsulation ethertype */
150 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
152 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
153 LIST_ENTRY(ifvlan) ifv_list;
154 struct vlan_entry ifv_entries[1];
156 #define ifv_if ifv_ac.ac_if
157 #define ifv_tag ifv_mib.ifvm_tag
160 LIST_HEAD(, vlan_entry) vlan_list;
164 struct netmsg_base base;
165 struct ifvlan *nv_ifv;
166 struct ifnet *nv_ifp_p;
167 const char *nv_parent_name;
171 #define VLANNAME "vlan"
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");
177 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
178 static LIST_HEAD(, ifvlan) ifv_list;
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 *);
184 static void vlan_init(void *);
185 static void vlan_start(struct ifnet *, struct ifaltq_subque *);
186 static int vlan_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
187 static void vlan_input(struct mbuf *);
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 *);
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);
209 /* Special flags we should propagate to parent */
212 int (*func)(struct ifnet *, int);
214 { IFF_PROMISC, ifpromisc },
215 { IFF_ALLMULTI, if_allmulti },
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,
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.
230 vlan_setflags(struct ifvlan *ifv, struct ifnet *ifp_p, int set)
234 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
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);
245 /* Handle a reference counted flag that should be set on the parent as well */
247 vlan_setflag(struct ifvlan *ifv, struct ifnet *ifp_p, int flag, int set,
248 int (*func)(struct ifnet *, int))
250 struct ifnet *ifp = &ifv->ifv_if;
253 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
255 ifv_flag = set ? (ifp->if_flags & flag) : 0;
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.
266 if (ifv_flag != (ifv->ifv_pflags & flag)) {
267 error = func(ifp_p, ifv_flag);
270 ifv->ifv_pflags &= ~flag;
271 ifv->ifv_pflags |= ifv_flag;
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.
285 vlan_setmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
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;
292 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
295 * First, remove any existing filter entries.
297 vlan_clrmulti(ifv, ifp_p);
300 * Now program new ones.
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;
309 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
312 if (ifma->ifma_addr->sa_family != AF_LINK)
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);
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);
332 vlan_clrmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
334 struct vlan_mc_entry *mc;
335 struct sockaddr_dl sdl;
337 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
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;
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 */
350 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
356 vlan_modevent(module_t mod, int type, void *data)
360 LIST_INIT(&ifv_list);
361 vlan_input_p = vlan_input;
362 vlan_ifdetach_cookie =
363 EVENTHANDLER_REGISTER(ifnet_detach_event,
365 EVENTHANDLER_PRI_ANY);
366 if_clone_attach(&vlan_cloner);
370 if_clone_detach(&vlan_cloner);
374 * Make sure that all protocol threads see vlan_input_p change.
376 netmsg_service_sync();
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);
387 static moduledata_t vlan_mod = {
393 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
396 vlan_ifdetach_dispatch(netmsg_t msg)
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;
403 vlantrunks = ifp_p->if_vlantrunks;
404 if (vlantrunks == NULL)
406 trunk = &vlantrunks[mycpuid];
408 while (ifp_p->if_vlantrunks &&
409 (ifve = LIST_FIRST(&trunk->vlan_list)) != NULL)
410 vlan_unconfig(ifve->ifv);
412 lwkt_replymsg(&vmsg->base.lmsg, 0);
416 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
418 struct netmsg_vlan vmsg;
420 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
422 bzero(&vmsg, sizeof(vmsg));
424 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
425 0, vlan_ifdetach_dispatch);
428 lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0);
432 vlan_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
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;
445 crit_enter(); /* XXX not MP safe */
446 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
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 */
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;
471 vlan_clone_destroy(struct ifnet *ifp)
473 struct ifvlan *ifv = ifp->if_softc;
475 crit_enter(); /* XXX not MP safe */
476 LIST_REMOVE(ifv, ifv_list);
490 struct ifvlan *ifv = xsc;
491 struct ifnet *ifp = &ifv->ifv_if;
493 ASSERT_IFNET_SERIALIZED_ALL(ifp);
495 if (ifv->ifv_p != NULL)
496 ifp->if_flags |= IFF_RUNNING;
500 vlan_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
502 struct ifvlan *ifv = ifp->if_softc;
503 struct ifnet *ifp_p = ifv->ifv_p;
507 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
508 ASSERT_IFNET_SERIALIZED_TX(ifp, ifsq);
515 if ((ifp->if_flags & IFF_RUNNING) == 0)
518 p_port = netisr_portfn(
519 ifsq_get_cpuid(ifq_get_subq_default(&ifp_p->if_snd)));
521 struct netmsg_packet *nmp;
523 m = ifsq_dequeue(ifsq, NULL);
529 * Do not run parent's if_start() if the parent is not up,
530 * or parent's driver will cause a system crash.
532 if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) !=
533 (IFF_UP | IFF_RUNNING)) {
535 IFNET_STAT_INC(ifp, collisions, 1);
540 * We need some way to tell the interface where the packet
541 * came from so that it knows how to find the VLAN tag to
542 * use, so we set the ether_vlantag in the mbuf packet header
543 * to our vlan tag. We also set the M_VLANTAG flag in the
544 * mbuf to let the parent driver know that the ether_vlantag
547 m->m_pkthdr.ether_vlantag = ifv->ifv_tag;
548 m->m_flags |= M_VLANTAG;
550 nmp = &m->m_hdr.mh_netmsg;
552 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
553 0, vlan_start_dispatch);
555 nmp->base.lmsg.u.ms_resultp = ifp_p;
557 lwkt_sendmsg(p_port, &nmp->base.lmsg);
558 IFNET_STAT_INC(ifp, opackets, 1);
563 vlan_input(struct mbuf *m)
565 struct ifvlan *ifv = NULL;
567 struct vlan_trunk *vlantrunks;
568 struct vlan_entry *entry;
570 rcvif = m->m_pkthdr.rcvif;
571 KKASSERT(m->m_flags & M_VLANTAG);
573 vlantrunks = rcvif->if_vlantrunks;
574 if (vlantrunks == NULL) {
575 IFNET_STAT_INC(rcvif, noproto, 1);
580 crit_enter(); /* XXX Necessary? */
581 LIST_FOREACH(entry, &vlantrunks[mycpuid].vlan_list, ifv_link) {
582 if (entry->ifv->ifv_tag ==
583 EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag)) {
591 * Packet is discarded if:
592 * - no corresponding vlan(4) interface
593 * - vlan(4) interface has not been completely set up yet,
594 * or is being destroyed (ifv->ifv_p != rcvif)
596 if (ifv == NULL || ifv->ifv_p != rcvif) {
597 IFNET_STAT_INC(rcvif, noproto, 1);
603 * Clear M_VLANTAG, before the packet is handed to
606 m->m_flags &= ~M_VLANTAG;
608 ether_reinput_oncpu(&ifv->ifv_if, m, REINPUT_RUNBPF);
612 vlan_link_dispatch(netmsg_t msg)
614 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
615 struct ifvlan *ifv = vmsg->nv_ifv;
616 struct ifnet *ifp_p = vmsg->nv_ifp_p;
617 struct vlan_entry *entry;
618 struct vlan_trunk *vlantrunks, *trunk;
621 vlantrunks = ifp_p->if_vlantrunks;
622 KASSERT(vlantrunks != NULL,
623 ("vlan trunk has not been initialized yet"));
625 entry = &ifv->ifv_entries[cpu];
626 trunk = &vlantrunks[cpu];
629 LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link);
632 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
636 vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p)
638 struct netmsg_vlan vmsg;
640 /* Assert in netisr0 */
641 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
643 if (ifp_p->if_vlantrunks == NULL) {
644 struct vlan_trunk *vlantrunks;
647 vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN,
649 for (i = 0; i < ncpus; ++i)
650 LIST_INIT(&vlantrunks[i].vlan_list);
652 ifp_p->if_vlantrunks = vlantrunks;
655 bzero(&vmsg, sizeof(vmsg));
657 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
658 0, vlan_link_dispatch);
660 vmsg.nv_ifp_p = ifp_p;
662 ifnet_domsg(&vmsg.base.lmsg, 0);
666 vlan_config_dispatch(netmsg_t msg)
668 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
670 struct ifnet *ifp_p, *ifp;
671 struct sockaddr_dl *sdl1, *sdl2;
674 /* Assert in netisr0 */
676 ifp_p = ifunit(vmsg->nv_parent_name);
682 if (ifp_p->if_data.ifi_type != IFT_ETHER) {
683 error = EPROTONOSUPPORT;
695 /* Link vlan into parent's vlantrunk */
696 vlan_link(ifv, ifp_p);
698 ifnet_serialize_all(ifp);
700 ifv->ifv_tag = vmsg->nv_vlantag;
701 if (ifp_p->if_capenable & IFCAP_VLAN_MTU)
702 ifp->if_mtu = ifp_p->if_mtu;
704 ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN;
707 * Copy only a selected subset of flags from the parent.
708 * Other flags are none of our business.
710 #define VLAN_INHERIT_FLAGS (IFF_BROADCAST | IFF_MULTICAST | \
711 IFF_SIMPLEX | IFF_POINTOPOINT)
713 ifp->if_flags &= ~VLAN_INHERIT_FLAGS;
714 ifp->if_flags |= (ifp_p->if_flags & VLAN_INHERIT_FLAGS);
716 #undef VLAN_INHERIT_FLAGS
719 * Set up our ``Ethernet address'' to reflect the underlying
720 * physical interface's.
722 sdl1 = IF_LLSOCKADDR(ifp);
723 sdl2 = IF_LLSOCKADDR(ifp_p);
724 sdl1->sdl_type = IFT_ETHER;
725 sdl1->sdl_alen = ETHER_ADDR_LEN;
726 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
727 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
730 * Release vlan's serializer before reprogramming parent's
731 * multicast filter to avoid possible dead lock.
733 ifnet_deserialize_all(ifp);
736 * Configure multicast addresses that may already be
737 * joined on the vlan device.
739 vlan_setmulti(ifv, ifp_p);
742 * Set flags on the parent, if necessary.
744 vlan_setflags(ifv, ifp_p, 1);
747 * Connect to parent after everything have been set up,
748 * so input/output could know that vlan is ready to go
753 lwkt_replymsg(&vmsg->base.lmsg, error);
757 vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag)
759 struct netmsg_vlan vmsg;
761 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
763 bzero(&vmsg, sizeof(vmsg));
765 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
766 0, vlan_config_dispatch);
768 vmsg.nv_parent_name = parent_name;
769 vmsg.nv_vlantag = vlantag;
771 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0);
775 vlan_unlink_dispatch(netmsg_t msg)
777 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
778 struct ifvlan *ifv = vmsg->nv_ifv;
779 struct vlan_entry *entry;
782 KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL,
783 ("vlan trunk has not been initialized yet"));
784 entry = &ifv->ifv_entries[cpu];
787 LIST_REMOVE(entry, ifv_link);
790 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
794 vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p)
796 struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks;
797 struct netmsg_vlan vmsg;
799 /* Assert in netisr0 */
800 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
802 KASSERT(ifp_p->if_vlantrunks != NULL,
803 ("vlan trunk has not been initialized yet"));
805 bzero(&vmsg, sizeof(vmsg));
807 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
808 0, vlan_unlink_dispatch);
810 vmsg.nv_ifp_p = ifp_p;
812 ifnet_domsg(&vmsg.base.lmsg, 0);
815 if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) {
816 ifp_p->if_vlantrunks = NULL;
819 * Make sure that all protocol threads see if_vlantrunks change.
821 netmsg_service_sync();
822 kfree(vlantrunks, M_VLAN);
828 vlan_unconfig_dispatch(netmsg_t msg)
830 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
831 struct sockaddr_dl *sdl;
833 struct ifnet *ifp_p, *ifp;
836 /* Assert in netisr0 */
841 if (ifp->if_flags & IFF_UP)
844 ifnet_serialize_all(ifp);
846 ifp->if_flags &= ~IFF_RUNNING;
849 * Save parent ifnet pointer and disconnect from parent.
851 * This is done early in this function, so input/output could
852 * know that we are disconnecting.
858 * Release vlan's serializer before reprogramming parent's
859 * multicast filter to avoid possible dead lock.
861 ifnet_deserialize_all(ifp);
865 * Since the interface is being unconfigured, we need to
866 * empty the list of multicast groups that we may have joined
867 * while we were alive from the parent's list.
869 vlan_clrmulti(ifv, ifp_p);
871 /* Clear parent's flags which was set by us. */
872 vlan_setflags(ifv, ifp_p, 0);
875 ifnet_serialize_all(ifp);
877 ifp->if_mtu = ETHERMTU;
879 /* Clear our MAC address. */
880 sdl = IF_LLSOCKADDR(ifp);
881 sdl->sdl_type = IFT_ETHER;
882 sdl->sdl_alen = ETHER_ADDR_LEN;
883 bzero(LLADDR(sdl), ETHER_ADDR_LEN);
884 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
886 ifnet_deserialize_all(ifp);
888 /* Unlink vlan from parent's vlantrunk */
889 if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL)
890 vlan_unlink(ifv, ifp_p);
893 lwkt_replymsg(&vmsg->base.lmsg, error);
897 vlan_unconfig(struct ifvlan *ifv)
899 struct netmsg_vlan vmsg;
901 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
903 bzero(&vmsg, sizeof(vmsg));
905 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
906 0, vlan_unconfig_dispatch);
909 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0);
913 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
915 struct ifvlan *ifv = ifp->if_softc;
916 struct ifreq *ifr = (struct ifreq *)data;
921 ASSERT_IFNET_SERIALIZED_ALL(ifp);
928 * Release vlan interface's serializer to void
929 * possible dead lock.
931 ifnet_deserialize_all(ifp);
933 ifnet_serialize_all(ifp_p);
934 error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr);
935 ifnet_deserialize_all(ifp_p);
937 ifnet_serialize_all(ifp);
939 if (ifv->ifv_p == NULL || ifv->ifv_p != ifp_p) {
941 * We are disconnected from the original
942 * parent interface or the parent interface
943 * is changed, after vlan interface's
944 * serializer is released.
949 /* Limit the result to the parent's current config. */
951 struct ifmediareq *ifmr;
953 ifmr = (struct ifmediareq *) data;
954 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
956 error = copyout(&ifmr->ifm_current,
971 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
975 ifnet_deserialize_all(ifp);
976 if (vlr.vlr_parent[0] == '\0')
977 error = vlan_unconfig(ifv);
979 error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag);
980 ifnet_serialize_all(ifp);
984 bzero(&vlr, sizeof(vlr));
986 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
987 sizeof(vlr.vlr_parent));
988 vlr.vlr_tag = ifv->ifv_tag;
990 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
994 if (ifp->if_flags & IFF_UP)
997 ifp->if_flags &= ~IFF_RUNNING;
1000 * We should propagate selected flags to the parent,
1001 * e.g., promiscuous mode.
1003 ifnet_deserialize_all(ifp);
1004 error = vlan_config_flags(ifv);
1005 ifnet_serialize_all(ifp);
1010 ifnet_deserialize_all(ifp);
1011 error = vlan_config_multi(ifv);
1012 ifnet_serialize_all(ifp);
1016 error = ether_ioctl(ifp, cmd, data);
1023 vlan_multi_dispatch(netmsg_t msg)
1025 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
1026 struct ifvlan *ifv = vmsg->nv_ifv;
1030 * If we don't have a parent, just remember the membership for
1033 if (ifv->ifv_p != NULL)
1034 error = vlan_setmulti(ifv, ifv->ifv_p);
1035 lwkt_replymsg(&vmsg->base.lmsg, error);
1039 vlan_config_multi(struct ifvlan *ifv)
1041 struct netmsg_vlan vmsg;
1043 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
1045 bzero(&vmsg, sizeof(vmsg));
1047 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
1048 0, vlan_multi_dispatch);
1051 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0);
1055 vlan_flags_dispatch(netmsg_t msg)
1057 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
1058 struct ifvlan *ifv = vmsg->nv_ifv;
1062 * If we don't have a parent, just remember the flags for
1065 if (ifv->ifv_p != NULL)
1066 error = vlan_setflags(ifv, ifv->ifv_p, 1);
1067 lwkt_replymsg(&vmsg->base.lmsg, error);
1071 vlan_config_flags(struct ifvlan *ifv)
1073 struct netmsg_vlan vmsg;
1075 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
1077 bzero(&vmsg, sizeof(vmsg));
1079 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
1080 0, vlan_flags_dispatch);
1083 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0);