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>
122 #include <net/netisr2.h>
125 #include <netinet/in.h>
126 #include <netinet/if_ether.h>
129 #include <net/vlan/if_vlan_var.h>
130 #include <net/vlan/if_vlan_ether.h>
134 struct vlan_mc_entry {
135 struct ether_addr mc_addr;
136 SLIST_ENTRY(vlan_mc_entry) mc_entries;
141 LIST_ENTRY(vlan_entry) ifv_link;
145 struct arpcom ifv_ac; /* make this an interface */
146 struct ifnet *ifv_p; /* parent inteface of this vlan */
147 int ifv_pflags; /* special flags we have set on parent */
150 uint16_t ifvm_proto; /* encapsulation ethertype */
151 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
153 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
154 LIST_ENTRY(ifvlan) ifv_list;
155 struct vlan_entry ifv_entries[1];
157 #define ifv_if ifv_ac.ac_if
158 #define ifv_tag ifv_mib.ifvm_tag
161 LIST_HEAD(, vlan_entry) vlan_list;
165 struct netmsg_base base;
166 struct ifvlan *nv_ifv;
167 struct ifnet *nv_ifp_p;
168 const char *nv_parent_name;
172 #define VLANNAME "vlan"
174 SYSCTL_DECL(_net_link);
175 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
176 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
178 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
179 static LIST_HEAD(, ifvlan) ifv_list;
181 static int vlan_clone_create(struct if_clone *, int, caddr_t);
182 static int vlan_clone_destroy(struct ifnet *);
183 static void vlan_ifdetach(void *, struct ifnet *);
185 static void vlan_init(void *);
186 static void vlan_start(struct ifnet *, struct ifaltq_subque *);
187 static int vlan_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
188 static void vlan_input(struct mbuf *);
190 static int vlan_setflags(struct ifvlan *, struct ifnet *, int);
191 static int vlan_setflag(struct ifvlan *, struct ifnet *, int, int,
192 int (*)(struct ifnet *, int));
193 static int vlan_config_flags(struct ifvlan *ifv);
194 static void vlan_clrmulti(struct ifvlan *, struct ifnet *);
195 static int vlan_setmulti(struct ifvlan *, struct ifnet *);
196 static int vlan_config_multi(struct ifvlan *);
197 static int vlan_config(struct ifvlan *, const char *, uint16_t);
198 static int vlan_unconfig(struct ifvlan *);
199 static void vlan_link(struct ifvlan *, struct ifnet *);
200 static void vlan_unlink(struct ifvlan *, struct ifnet *);
202 static void vlan_config_dispatch(netmsg_t);
203 static void vlan_unconfig_dispatch(netmsg_t);
204 static void vlan_link_dispatch(netmsg_t);
205 static void vlan_unlink_dispatch(netmsg_t);
206 static void vlan_multi_dispatch(netmsg_t);
207 static void vlan_flags_dispatch(netmsg_t);
208 static void vlan_ifdetach_dispatch(netmsg_t);
210 /* Special flags we should propagate to parent */
213 int (*func)(struct ifnet *, int);
215 { IFF_PROMISC, ifpromisc },
216 { IFF_ALLMULTI, if_allmulti },
220 static eventhandler_tag vlan_ifdetach_cookie;
221 static struct if_clone vlan_cloner =
222 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy,
226 * Handle IFF_* flags that require certain changes on the parent:
227 * if "set" is true, update parent's flags respective to our if_flags;
228 * if "set" is false, forcedly clear the flags set on parent.
231 vlan_setflags(struct ifvlan *ifv, struct ifnet *ifp_p, int set)
235 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
237 for (i = 0; vlan_pflags[i].func != NULL; i++) {
238 error = vlan_setflag(ifv, ifp_p, vlan_pflags[i].flag,
239 set, vlan_pflags[i].func);
246 /* Handle a reference counted flag that should be set on the parent as well */
248 vlan_setflag(struct ifvlan *ifv, struct ifnet *ifp_p, int flag, int set,
249 int (*func)(struct ifnet *, int))
251 struct ifnet *ifp = &ifv->ifv_if;
254 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
256 ifv_flag = set ? (ifp->if_flags & flag) : 0;
259 * See if recorded parent's status is different from what
260 * we want it to be. If it is, flip it. We record parent's
261 * status in ifv_pflags so that we won't clear parent's flag
262 * we haven't set. In fact, we don't clear or set parent's
263 * flags directly, but get or release references to them.
264 * That's why we can be sure that recorded flags still are
265 * in accord with actual parent's flags.
267 if (ifv_flag != (ifv->ifv_pflags & flag)) {
268 error = func(ifp_p, ifv_flag);
271 ifv->ifv_pflags &= ~flag;
272 ifv->ifv_pflags |= ifv_flag;
278 * Program our multicast filter. What we're actually doing is
279 * programming the multicast filter of the parent. This has the
280 * side effect of causing the parent interface to receive multicast
281 * traffic that it doesn't really want, which ends up being discarded
282 * later by the upper protocol layers. Unfortunately, there's no way
283 * to avoid this: there really is only one physical interface.
286 vlan_setmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
288 struct ifmultiaddr *ifma, *rifma = NULL;
289 struct vlan_mc_entry *mc = NULL;
290 struct sockaddr_dl sdl;
291 struct ifnet *ifp = &ifv->ifv_if;
293 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
296 * First, remove any existing filter entries.
298 vlan_clrmulti(ifv, ifp_p);
301 * Now program new ones.
303 bzero(&sdl, sizeof(sdl));
304 sdl.sdl_len = sizeof(sdl);
305 sdl.sdl_family = AF_LINK;
306 sdl.sdl_index = ifp_p->if_index;
307 sdl.sdl_type = IFT_ETHER;
308 sdl.sdl_alen = ETHER_ADDR_LEN;
310 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
313 if (ifma->ifma_addr->sa_family != AF_LINK)
317 mc = kmalloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
318 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
319 &mc->mc_addr, ETHER_ADDR_LEN);
320 SLIST_INSERT_HEAD(&ifv->vlan_mc_listhead, mc, mc_entries);
322 /* Program the parent multicast filter */
323 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
324 LLADDR(&sdl), ETHER_ADDR_LEN);
325 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
333 vlan_clrmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
335 struct vlan_mc_entry *mc;
336 struct sockaddr_dl sdl;
338 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
340 bzero(&sdl, sizeof(sdl));
341 sdl.sdl_len = sizeof(sdl);
342 sdl.sdl_family = AF_LINK;
343 sdl.sdl_index = ifp_p->if_index;
344 sdl.sdl_type = IFT_ETHER;
345 sdl.sdl_alen = ETHER_ADDR_LEN;
347 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
348 bcopy(&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
349 if_delmulti(ifp_p, (struct sockaddr *)&sdl); /* ignore error */
351 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
357 vlan_modevent(module_t mod, int type, void *data)
361 LIST_INIT(&ifv_list);
362 vlan_input_p = vlan_input;
363 vlan_ifdetach_cookie =
364 EVENTHANDLER_REGISTER(ifnet_detach_event,
366 EVENTHANDLER_PRI_ANY);
367 if_clone_attach(&vlan_cloner);
371 if_clone_detach(&vlan_cloner);
375 * Make sure that all protocol threads see vlan_input_p change.
377 netmsg_service_sync();
379 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
380 vlan_ifdetach_cookie);
381 while (!LIST_EMPTY(&ifv_list))
382 vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
388 static moduledata_t vlan_mod = {
394 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
397 vlan_ifdetach_dispatch(netmsg_t msg)
399 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
400 struct ifnet *ifp_p = vmsg->nv_ifp_p;
401 struct vlan_trunk *vlantrunks, *trunk;
402 struct vlan_entry *ifve;
404 vlantrunks = ifp_p->if_vlantrunks;
405 if (vlantrunks == NULL)
407 trunk = &vlantrunks[mycpuid];
409 while (ifp_p->if_vlantrunks &&
410 (ifve = LIST_FIRST(&trunk->vlan_list)) != NULL)
411 vlan_unconfig(ifve->ifv);
413 lwkt_replymsg(&vmsg->base.lmsg, 0);
417 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
419 struct netmsg_vlan vmsg;
421 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
423 bzero(&vmsg, sizeof(vmsg));
425 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
426 0, vlan_ifdetach_dispatch);
429 lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
433 vlan_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
439 vlan_size = sizeof(struct ifvlan)
440 + ((ncpus - 1) * sizeof(struct vlan_entry));
441 ifv = kmalloc(vlan_size, M_VLAN, M_WAITOK | M_ZERO);
442 SLIST_INIT(&ifv->vlan_mc_listhead);
443 for (i = 0; i < ncpus; ++i)
444 ifv->ifv_entries[i].ifv = ifv;
446 crit_enter(); /* XXX not MP safe */
447 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
452 if_initname(ifp, "vlan", unit);
453 /* NB: flags are not set here */
454 ifp->if_linkmib = &ifv->ifv_mib;
455 ifp->if_linkmiblen = sizeof ifv->ifv_mib;
456 /* NB: mtu is not set here */
458 ifp->if_init = vlan_init;
459 ifp->if_start = vlan_start;
460 ifp->if_ioctl = vlan_ioctl;
461 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
462 ifq_set_ready(&ifp->if_snd);
463 ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr, NULL);
464 /* Now undo some of the damage... */
465 ifp->if_data.ifi_type = IFT_L2VLAN;
466 ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
472 vlan_clone_destroy(struct ifnet *ifp)
474 struct ifvlan *ifv = ifp->if_softc;
476 crit_enter(); /* XXX not MP safe */
477 LIST_REMOVE(ifv, ifv_list);
491 struct ifvlan *ifv = xsc;
492 struct ifnet *ifp = &ifv->ifv_if;
494 ASSERT_IFNET_SERIALIZED_ALL(ifp);
496 if (ifv->ifv_p != NULL)
497 ifp->if_flags |= IFF_RUNNING;
501 vlan_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
503 struct ifvlan *ifv = ifp->if_softc;
504 struct ifnet *ifp_p = ifv->ifv_p;
508 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
509 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
516 if ((ifp->if_flags & IFF_RUNNING) == 0)
519 p_port = netisr_cpuport(
520 ifsq_get_cpuid(ifq_get_subq_default(&ifp_p->if_snd)));
522 struct netmsg_packet *nmp;
524 m = ifsq_dequeue(ifsq, NULL);
530 * Do not run parent's if_start() if the parent is not up,
531 * or parent's driver will cause a system crash.
533 if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) !=
534 (IFF_UP | IFF_RUNNING)) {
536 IFNET_STAT_INC(ifp, collisions, 1);
541 * We need some way to tell the interface where the packet
542 * came from so that it knows how to find the VLAN tag to
543 * use, so we set the ether_vlantag in the mbuf packet header
544 * to our vlan tag. We also set the M_VLANTAG flag in the
545 * mbuf to let the parent driver know that the ether_vlantag
548 m->m_pkthdr.ether_vlantag = ifv->ifv_tag;
549 m->m_flags |= M_VLANTAG;
551 nmp = &m->m_hdr.mh_netmsg;
553 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
554 0, vlan_start_dispatch);
556 nmp->base.lmsg.u.ms_resultp = ifp_p;
558 lwkt_sendmsg(p_port, &nmp->base.lmsg);
559 IFNET_STAT_INC(ifp, opackets, 1);
564 vlan_input(struct mbuf *m)
566 struct ifvlan *ifv = NULL;
568 struct vlan_trunk *vlantrunks;
569 struct vlan_entry *entry;
571 rcvif = m->m_pkthdr.rcvif;
572 KKASSERT(m->m_flags & M_VLANTAG);
574 vlantrunks = rcvif->if_vlantrunks;
575 if (vlantrunks == NULL) {
576 IFNET_STAT_INC(rcvif, noproto, 1);
581 crit_enter(); /* XXX Necessary? */
582 LIST_FOREACH(entry, &vlantrunks[mycpuid].vlan_list, ifv_link) {
583 if (entry->ifv->ifv_tag ==
584 EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag)) {
592 * Packet is discarded if:
593 * - no corresponding vlan(4) interface
594 * - vlan(4) interface has not been completely set up yet,
595 * or is being destroyed (ifv->ifv_p != rcvif)
597 if (ifv == NULL || ifv->ifv_p != rcvif) {
598 IFNET_STAT_INC(rcvif, noproto, 1);
604 * Clear M_VLANTAG, before the packet is handed to
607 m->m_flags &= ~M_VLANTAG;
609 ether_reinput_oncpu(&ifv->ifv_if, m, REINPUT_RUNBPF);
613 vlan_link_dispatch(netmsg_t msg)
615 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
616 struct ifvlan *ifv = vmsg->nv_ifv;
617 struct ifnet *ifp_p = vmsg->nv_ifp_p;
618 struct vlan_entry *entry;
619 struct vlan_trunk *vlantrunks, *trunk;
622 vlantrunks = ifp_p->if_vlantrunks;
623 KASSERT(vlantrunks != NULL,
624 ("vlan trunk has not been initialized yet"));
626 entry = &ifv->ifv_entries[cpu];
627 trunk = &vlantrunks[cpu];
630 LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link);
633 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
637 vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p)
639 struct netmsg_vlan vmsg;
641 /* Assert in netisr0 */
642 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
644 if (ifp_p->if_vlantrunks == NULL) {
645 struct vlan_trunk *vlantrunks;
648 vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN,
650 for (i = 0; i < ncpus; ++i)
651 LIST_INIT(&vlantrunks[i].vlan_list);
653 ifp_p->if_vlantrunks = vlantrunks;
656 bzero(&vmsg, sizeof(vmsg));
658 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
659 0, vlan_link_dispatch);
661 vmsg.nv_ifp_p = ifp_p;
663 ifnet_domsg(&vmsg.base.lmsg, 0);
667 vlan_config_dispatch(netmsg_t msg)
669 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
671 struct ifnet *ifp_p, *ifp;
672 struct sockaddr_dl *sdl1, *sdl2;
675 /* Assert in netisr0 */
677 ifp_p = ifunit(vmsg->nv_parent_name);
683 if (ifp_p->if_data.ifi_type != IFT_ETHER) {
684 error = EPROTONOSUPPORT;
696 /* Link vlan into parent's vlantrunk */
697 vlan_link(ifv, ifp_p);
699 ifnet_serialize_all(ifp);
701 ifv->ifv_tag = vmsg->nv_vlantag;
702 if (ifp_p->if_capenable & IFCAP_VLAN_MTU)
703 ifp->if_mtu = ifp_p->if_mtu;
705 ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN;
708 * Copy only a selected subset of flags from the parent.
709 * Other flags are none of our business.
711 #define VLAN_INHERIT_FLAGS (IFF_BROADCAST | IFF_MULTICAST | \
712 IFF_SIMPLEX | IFF_POINTOPOINT)
714 ifp->if_flags &= ~VLAN_INHERIT_FLAGS;
715 ifp->if_flags |= (ifp_p->if_flags & VLAN_INHERIT_FLAGS);
717 #undef VLAN_INHERIT_FLAGS
720 * Set up our ``Ethernet address'' to reflect the underlying
721 * physical interface's.
723 sdl1 = IF_LLSOCKADDR(ifp);
724 sdl2 = IF_LLSOCKADDR(ifp_p);
725 sdl1->sdl_type = IFT_ETHER;
726 sdl1->sdl_alen = ETHER_ADDR_LEN;
727 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
728 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
731 * Release vlan's serializer before reprogramming parent's
732 * multicast filter to avoid possible dead lock.
734 ifnet_deserialize_all(ifp);
737 * Configure multicast addresses that may already be
738 * joined on the vlan device.
740 vlan_setmulti(ifv, ifp_p);
743 * Set flags on the parent, if necessary.
745 vlan_setflags(ifv, ifp_p, 1);
748 * Connect to parent after everything have been set up,
749 * so input/output could know that vlan is ready to go
754 lwkt_replymsg(&vmsg->base.lmsg, error);
758 vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag)
760 struct netmsg_vlan vmsg;
762 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
764 bzero(&vmsg, sizeof(vmsg));
766 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
767 0, vlan_config_dispatch);
769 vmsg.nv_parent_name = parent_name;
770 vmsg.nv_vlantag = vlantag;
772 return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
776 vlan_unlink_dispatch(netmsg_t msg)
778 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
779 struct ifvlan *ifv = vmsg->nv_ifv;
780 struct vlan_entry *entry;
783 KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL,
784 ("vlan trunk has not been initialized yet"));
785 entry = &ifv->ifv_entries[cpu];
788 LIST_REMOVE(entry, ifv_link);
791 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
795 vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p)
797 struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks;
798 struct netmsg_vlan vmsg;
800 /* Assert in netisr0 */
801 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
803 KASSERT(ifp_p->if_vlantrunks != NULL,
804 ("vlan trunk has not been initialized yet"));
806 bzero(&vmsg, sizeof(vmsg));
808 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
809 0, vlan_unlink_dispatch);
811 vmsg.nv_ifp_p = ifp_p;
813 ifnet_domsg(&vmsg.base.lmsg, 0);
816 if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) {
817 ifp_p->if_vlantrunks = NULL;
820 * Make sure that all protocol threads see if_vlantrunks change.
822 netmsg_service_sync();
823 kfree(vlantrunks, M_VLAN);
829 vlan_unconfig_dispatch(netmsg_t msg)
831 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
832 struct sockaddr_dl *sdl;
834 struct ifnet *ifp_p, *ifp;
837 /* Assert in netisr0 */
842 if (ifp->if_flags & IFF_UP)
845 ifnet_serialize_all(ifp);
847 ifp->if_flags &= ~IFF_RUNNING;
850 * Save parent ifnet pointer and disconnect from parent.
852 * This is done early in this function, so input/output could
853 * know that we are disconnecting.
859 * Release vlan's serializer before reprogramming parent's
860 * multicast filter to avoid possible dead lock.
862 ifnet_deserialize_all(ifp);
866 * Since the interface is being unconfigured, we need to
867 * empty the list of multicast groups that we may have joined
868 * while we were alive from the parent's list.
870 vlan_clrmulti(ifv, ifp_p);
872 /* Clear parent's flags which was set by us. */
873 vlan_setflags(ifv, ifp_p, 0);
876 ifnet_serialize_all(ifp);
878 ifp->if_mtu = ETHERMTU;
880 /* Clear our MAC address. */
881 sdl = IF_LLSOCKADDR(ifp);
882 sdl->sdl_type = IFT_ETHER;
883 sdl->sdl_alen = ETHER_ADDR_LEN;
884 bzero(LLADDR(sdl), ETHER_ADDR_LEN);
885 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
887 ifnet_deserialize_all(ifp);
889 /* Unlink vlan from parent's vlantrunk */
890 if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL)
891 vlan_unlink(ifv, ifp_p);
894 lwkt_replymsg(&vmsg->base.lmsg, error);
898 vlan_unconfig(struct ifvlan *ifv)
900 struct netmsg_vlan vmsg;
902 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
904 bzero(&vmsg, sizeof(vmsg));
906 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
907 0, vlan_unconfig_dispatch);
910 return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
914 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
916 struct ifvlan *ifv = ifp->if_softc;
917 struct ifreq *ifr = (struct ifreq *)data;
922 ASSERT_IFNET_SERIALIZED_ALL(ifp);
929 * Release vlan interface's serializer to void
930 * possible dead lock.
932 ifnet_deserialize_all(ifp);
934 ifnet_serialize_all(ifp_p);
935 error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr);
936 ifnet_deserialize_all(ifp_p);
938 ifnet_serialize_all(ifp);
940 if (ifv->ifv_p == NULL || ifv->ifv_p != ifp_p) {
942 * We are disconnected from the original
943 * parent interface or the parent interface
944 * is changed, after vlan interface's
945 * serializer is released.
950 /* Limit the result to the parent's current config. */
952 struct ifmediareq *ifmr;
954 ifmr = (struct ifmediareq *) data;
955 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
957 error = copyout(&ifmr->ifm_current,
972 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
976 ifnet_deserialize_all(ifp);
977 if (vlr.vlr_parent[0] == '\0')
978 error = vlan_unconfig(ifv);
980 error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag);
981 ifnet_serialize_all(ifp);
985 bzero(&vlr, sizeof(vlr));
987 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
988 sizeof(vlr.vlr_parent));
989 vlr.vlr_tag = ifv->ifv_tag;
991 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
995 if (ifp->if_flags & IFF_UP)
998 ifp->if_flags &= ~IFF_RUNNING;
1001 * We should propagate selected flags to the parent,
1002 * e.g., promiscuous mode.
1004 ifnet_deserialize_all(ifp);
1005 error = vlan_config_flags(ifv);
1006 ifnet_serialize_all(ifp);
1011 ifnet_deserialize_all(ifp);
1012 error = vlan_config_multi(ifv);
1013 ifnet_serialize_all(ifp);
1017 error = ether_ioctl(ifp, cmd, data);
1024 vlan_multi_dispatch(netmsg_t msg)
1026 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
1027 struct ifvlan *ifv = vmsg->nv_ifv;
1031 * If we don't have a parent, just remember the membership for
1034 if (ifv->ifv_p != NULL)
1035 error = vlan_setmulti(ifv, ifv->ifv_p);
1036 lwkt_replymsg(&vmsg->base.lmsg, error);
1040 vlan_config_multi(struct ifvlan *ifv)
1042 struct netmsg_vlan vmsg;
1044 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
1046 bzero(&vmsg, sizeof(vmsg));
1048 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
1049 0, vlan_multi_dispatch);
1052 return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
1056 vlan_flags_dispatch(netmsg_t msg)
1058 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
1059 struct ifvlan *ifv = vmsg->nv_ifv;
1063 * If we don't have a parent, just remember the flags for
1066 if (ifv->ifv_p != NULL)
1067 error = vlan_setflags(ifv, ifv->ifv_p, 1);
1068 lwkt_replymsg(&vmsg->base.lmsg, error);
1072 vlan_config_flags(struct ifvlan *ifv)
1074 struct netmsg_vlan vmsg;
1076 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
1078 bzero(&vmsg, sizeof(vmsg));
1080 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
1081 0, vlan_flags_dispatch);
1084 return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);