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 $
30 * $DragonFly: src/sys/net/vlan/if_vlan.c,v 1.43 2008/11/22 04:00:53 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.
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.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>
61 #include <sys/thread2.h>
64 #include <net/ethernet.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>
74 #include <netinet/in.h>
75 #include <netinet/if_ether.h>
78 #include <net/vlan/if_vlan_var.h>
79 #include <net/vlan/if_vlan_ether.h>
83 struct vlan_mc_entry {
84 struct ether_addr mc_addr;
85 SLIST_ENTRY(vlan_mc_entry) mc_entries;
90 LIST_ENTRY(vlan_entry) ifv_link;
94 struct arpcom ifv_ac; /* make this an interface */
95 struct ifnet *ifv_p; /* parent inteface of this vlan */
96 int ifv_pflags; /* special flags we have set on parent */
99 uint16_t ifvm_proto; /* encapsulation ethertype */
100 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
102 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
103 LIST_ENTRY(ifvlan) ifv_list;
104 struct vlan_entry ifv_entries[1];
106 #define ifv_if ifv_ac.ac_if
107 #define ifv_tag ifv_mib.ifvm_tag
110 LIST_HEAD(, vlan_entry) vlan_list;
114 struct netmsg_base base;
115 struct ifvlan *nv_ifv;
116 struct ifnet *nv_ifp_p;
117 const char *nv_parent_name;
121 #define VLANNAME "vlan"
123 SYSCTL_DECL(_net_link);
124 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
125 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
127 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
128 static LIST_HEAD(, ifvlan) ifv_list;
130 static int vlan_clone_create(struct if_clone *, int, caddr_t);
131 static void vlan_clone_destroy(struct ifnet *);
132 static void vlan_ifdetach(void *, struct ifnet *);
134 static void vlan_init(void *);
135 static void vlan_start(struct ifnet *);
136 static int vlan_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
137 static void vlan_input(struct mbuf *);
139 static int vlan_setflags(struct ifvlan *, struct ifnet *, int);
140 static int vlan_setflag(struct ifvlan *, struct ifnet *, int, int,
141 int (*)(struct ifnet *, int));
142 static int vlan_config_flags(struct ifvlan *ifv);
143 static void vlan_clrmulti(struct ifvlan *, struct ifnet *);
144 static int vlan_setmulti(struct ifvlan *, struct ifnet *);
145 static int vlan_config_multi(struct ifvlan *);
146 static int vlan_config(struct ifvlan *, const char *, uint16_t);
147 static int vlan_unconfig(struct ifvlan *);
148 static void vlan_link(struct ifvlan *, struct ifnet *);
149 static void vlan_unlink(struct ifvlan *, struct ifnet *);
151 static void vlan_config_dispatch(netmsg_t);
152 static void vlan_unconfig_dispatch(netmsg_t);
153 static void vlan_link_dispatch(netmsg_t);
154 static void vlan_unlink_dispatch(netmsg_t);
155 static void vlan_multi_dispatch(netmsg_t);
156 static void vlan_flags_dispatch(netmsg_t);
157 static void vlan_ifdetach_dispatch(netmsg_t);
159 /* Special flags we should propagate to parent */
162 int (*func)(struct ifnet *, int);
164 { IFF_PROMISC, ifpromisc },
165 { IFF_ALLMULTI, if_allmulti },
169 static eventhandler_tag vlan_ifdetach_cookie;
170 static struct if_clone vlan_cloner =
171 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy,
175 * Handle IFF_* flags that require certain changes on the parent:
176 * if "set" is true, update parent's flags respective to our if_flags;
177 * if "set" is false, forcedly clear the flags set on parent.
180 vlan_setflags(struct ifvlan *ifv, struct ifnet *ifp_p, int set)
184 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
186 for (i = 0; vlan_pflags[i].func != NULL; i++) {
187 error = vlan_setflag(ifv, ifp_p, vlan_pflags[i].flag,
188 set, vlan_pflags[i].func);
195 /* Handle a reference counted flag that should be set on the parent as well */
197 vlan_setflag(struct ifvlan *ifv, struct ifnet *ifp_p, int flag, int set,
198 int (*func)(struct ifnet *, int))
200 struct ifnet *ifp = &ifv->ifv_if;
203 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
205 ifv_flag = set ? (ifp->if_flags & flag) : 0;
208 * See if recorded parent's status is different from what
209 * we want it to be. If it is, flip it. We record parent's
210 * status in ifv_pflags so that we won't clear parent's flag
211 * we haven't set. In fact, we don't clear or set parent's
212 * flags directly, but get or release references to them.
213 * That's why we can be sure that recorded flags still are
214 * in accord with actual parent's flags.
216 if (ifv_flag != (ifv->ifv_pflags & flag)) {
217 error = func(ifp_p, ifv_flag);
220 ifv->ifv_pflags &= ~flag;
221 ifv->ifv_pflags |= ifv_flag;
227 * Program our multicast filter. What we're actually doing is
228 * programming the multicast filter of the parent. This has the
229 * side effect of causing the parent interface to receive multicast
230 * traffic that it doesn't really want, which ends up being discarded
231 * later by the upper protocol layers. Unfortunately, there's no way
232 * to avoid this: there really is only one physical interface.
235 vlan_setmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
237 struct ifmultiaddr *ifma, *rifma = NULL;
238 struct vlan_mc_entry *mc = NULL;
239 struct sockaddr_dl sdl;
240 struct ifnet *ifp = &ifv->ifv_if;
242 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
245 * First, remove any existing filter entries.
247 vlan_clrmulti(ifv, ifp_p);
250 * Now program new ones.
252 bzero(&sdl, sizeof(sdl));
253 sdl.sdl_len = sizeof(sdl);
254 sdl.sdl_family = AF_LINK;
255 sdl.sdl_index = ifp_p->if_index;
256 sdl.sdl_type = IFT_ETHER;
257 sdl.sdl_alen = ETHER_ADDR_LEN;
259 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
262 if (ifma->ifma_addr->sa_family != AF_LINK)
266 mc = kmalloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
267 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
268 &mc->mc_addr, ETHER_ADDR_LEN);
269 SLIST_INSERT_HEAD(&ifv->vlan_mc_listhead, mc, mc_entries);
271 /* Program the parent multicast filter */
272 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
273 LLADDR(&sdl), ETHER_ADDR_LEN);
274 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
282 vlan_clrmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
284 struct vlan_mc_entry *mc;
285 struct sockaddr_dl sdl;
287 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
289 bzero(&sdl, sizeof(sdl));
290 sdl.sdl_len = sizeof(sdl);
291 sdl.sdl_family = AF_LINK;
292 sdl.sdl_index = ifp_p->if_index;
293 sdl.sdl_type = IFT_ETHER;
294 sdl.sdl_alen = ETHER_ADDR_LEN;
296 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
297 bcopy(&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
298 if_delmulti(ifp_p, (struct sockaddr *)&sdl); /* ignore error */
300 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
306 vlan_modevent(module_t mod, int type, void *data)
310 LIST_INIT(&ifv_list);
311 vlan_input_p = vlan_input;
312 vlan_ifdetach_cookie =
313 EVENTHANDLER_REGISTER(ifnet_detach_event,
315 EVENTHANDLER_PRI_ANY);
316 if_clone_attach(&vlan_cloner);
320 if_clone_detach(&vlan_cloner);
324 * Make that all protocol threads see vlan_input_p change.
326 netmsg_service_sync();
328 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
329 vlan_ifdetach_cookie);
330 while (!LIST_EMPTY(&ifv_list))
331 vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
337 static moduledata_t vlan_mod = {
343 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
346 vlan_ifdetach_dispatch(netmsg_t msg)
348 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
349 struct ifnet *ifp_p = vmsg->nv_ifp_p;
350 struct vlan_trunk *vlantrunks, *trunk;
351 struct vlan_entry *ifve;
353 vlantrunks = ifp_p->if_vlantrunks;
354 if (vlantrunks == NULL)
356 trunk = &vlantrunks[mycpuid];
358 while (ifp_p->if_vlantrunks &&
359 (ifve = LIST_FIRST(&trunk->vlan_list)) != NULL)
360 vlan_unconfig(ifve->ifv);
362 lwkt_replymsg(&vmsg->base.lmsg, 0);
366 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
368 struct netmsg_vlan vmsg;
370 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
372 bzero(&vmsg, sizeof(vmsg));
374 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
375 0, vlan_ifdetach_dispatch);
378 lwkt_domsg(cpu_portfn(0), &vmsg.base.lmsg, 0);
382 vlan_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
388 vlan_size = sizeof(struct ifvlan)
389 + ((ncpus - 1) * sizeof(struct vlan_entry));
390 ifv = kmalloc(vlan_size, M_VLAN, M_WAITOK | M_ZERO);
391 SLIST_INIT(&ifv->vlan_mc_listhead);
392 for (i = 0; i < ncpus; ++i)
393 ifv->ifv_entries[i].ifv = ifv;
395 crit_enter(); /* XXX not MP safe */
396 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
401 if_initname(ifp, "vlan", unit);
402 /* NB: flags are not set here */
403 ifp->if_linkmib = &ifv->ifv_mib;
404 ifp->if_linkmiblen = sizeof ifv->ifv_mib;
405 /* NB: mtu is not set here */
407 ifp->if_init = vlan_init;
408 ifp->if_start = vlan_start;
409 ifp->if_ioctl = vlan_ioctl;
410 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
411 ifq_set_ready(&ifp->if_snd);
412 ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr, NULL);
413 /* Now undo some of the damage... */
414 ifp->if_data.ifi_type = IFT_L2VLAN;
415 ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
421 vlan_clone_destroy(struct ifnet *ifp)
423 struct ifvlan *ifv = ifp->if_softc;
425 crit_enter(); /* XXX not MP safe */
426 LIST_REMOVE(ifv, ifv_list);
438 struct ifvlan *ifv = xsc;
439 struct ifnet *ifp = &ifv->ifv_if;
441 ASSERT_IFNET_SERIALIZED_ALL(ifp);
443 if (ifv->ifv_p != NULL)
444 ifp->if_flags |= IFF_RUNNING;
448 vlan_start(struct ifnet *ifp)
450 struct ifvlan *ifv = ifp->if_softc;
451 struct ifnet *ifp_p = ifv->ifv_p;
454 ASSERT_IFNET_SERIALIZED_TX(ifp);
457 ifq_purge(&ifp->if_snd);
461 if ((ifp->if_flags & IFF_RUNNING) == 0)
465 struct netmsg_packet *nmp;
466 struct lwkt_port *port;
468 m = ifq_dequeue(&ifp->if_snd, NULL);
474 * Do not run parent's if_start() if the parent is not up,
475 * or parent's driver will cause a system crash.
477 if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) !=
478 (IFF_UP | IFF_RUNNING)) {
480 ifp->if_data.ifi_collisions++;
485 * We need some way to tell the interface where the packet
486 * came from so that it knows how to find the VLAN tag to
487 * use, so we set the ether_vlantag in the mbuf packet header
488 * to our vlan tag. We also set the M_VLANTAG flag in the
489 * mbuf to let the parent driver know that the ether_vlantag
492 m->m_pkthdr.ether_vlantag = ifv->ifv_tag;
493 m->m_flags |= M_VLANTAG;
495 nmp = &m->m_hdr.mh_netmsg;
497 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
498 0, vlan_start_dispatch);
500 nmp->base.lmsg.u.ms_resultp = ifp_p;
502 port = cpu_portfn(ifp_p->if_index % ncpus /* XXX */);
503 lwkt_sendmsg(port, &nmp->base.lmsg);
509 vlan_input(struct mbuf *m)
511 struct ifvlan *ifv = NULL;
513 struct vlan_trunk *vlantrunks;
514 struct vlan_entry *entry;
516 rcvif = m->m_pkthdr.rcvif;
517 KKASSERT(m->m_flags & M_VLANTAG);
519 vlantrunks = rcvif->if_vlantrunks;
520 if (vlantrunks == NULL) {
526 crit_enter(); /* XXX Necessary? */
527 LIST_FOREACH(entry, &vlantrunks[mycpuid].vlan_list, ifv_link) {
528 if (entry->ifv->ifv_tag ==
529 EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag)) {
537 * Packet is discarded if:
538 * - no corresponding vlan(4) interface
539 * - vlan(4) interface has not been completely set up yet,
540 * or is being destroyed (ifv->ifv_p != rcvif)
542 if (ifv == NULL || ifv->ifv_p != rcvif) {
549 * Clear M_VLANTAG, before the packet is handed to
552 m->m_flags &= ~M_VLANTAG;
554 ether_reinput_oncpu(&ifv->ifv_if, m, 1);
558 vlan_link_dispatch(netmsg_t msg)
560 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
561 struct ifvlan *ifv = vmsg->nv_ifv;
562 struct ifnet *ifp_p = vmsg->nv_ifp_p;
563 struct vlan_entry *entry;
564 struct vlan_trunk *vlantrunks, *trunk;
567 vlantrunks = ifp_p->if_vlantrunks;
568 KASSERT(vlantrunks != NULL,
569 ("vlan trunk has not been initialized yet\n"));
571 entry = &ifv->ifv_entries[cpu];
572 trunk = &vlantrunks[cpu];
575 LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link);
578 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
582 vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p)
584 struct netmsg_vlan vmsg;
586 /* Assert in netisr0 */
587 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
589 if (ifp_p->if_vlantrunks == NULL) {
590 struct vlan_trunk *vlantrunks;
593 vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN,
595 for (i = 0; i < ncpus; ++i)
596 LIST_INIT(&vlantrunks[i].vlan_list);
598 ifp_p->if_vlantrunks = vlantrunks;
601 bzero(&vmsg, sizeof(vmsg));
603 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
604 0, vlan_link_dispatch);
606 vmsg.nv_ifp_p = ifp_p;
608 ifnet_domsg(&vmsg.base.lmsg, 0);
612 vlan_config_dispatch(netmsg_t msg)
614 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
616 struct ifnet *ifp_p, *ifp;
617 struct sockaddr_dl *sdl1, *sdl2;
620 /* Assert in netisr0 */
622 ifp_p = ifunit(vmsg->nv_parent_name);
628 if (ifp_p->if_data.ifi_type != IFT_ETHER) {
629 error = EPROTONOSUPPORT;
641 /* Link vlan into parent's vlantrunk */
642 vlan_link(ifv, ifp_p);
644 ifnet_serialize_all(ifp);
646 ifv->ifv_tag = vmsg->nv_vlantag;
647 if (ifp_p->if_capenable & IFCAP_VLAN_MTU)
648 ifp->if_mtu = ifp_p->if_mtu;
650 ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN;
653 * Copy only a selected subset of flags from the parent.
654 * Other flags are none of our business.
656 #define VLAN_INHERIT_FLAGS (IFF_BROADCAST | IFF_MULTICAST | \
657 IFF_SIMPLEX | IFF_POINTOPOINT)
659 ifp->if_flags &= ~VLAN_INHERIT_FLAGS;
660 ifp->if_flags |= (ifp_p->if_flags & VLAN_INHERIT_FLAGS);
662 #undef VLAN_INHERIT_FLAGS
665 * Set up our ``Ethernet address'' to reflect the underlying
666 * physical interface's.
668 sdl1 = IF_LLSOCKADDR(ifp);
669 sdl2 = IF_LLSOCKADDR(ifp_p);
670 sdl1->sdl_type = IFT_ETHER;
671 sdl1->sdl_alen = ETHER_ADDR_LEN;
672 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
673 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
676 * Release vlan's serializer before reprogramming parent's
677 * multicast filter to avoid possible dead lock.
679 ifnet_deserialize_all(ifp);
682 * Configure multicast addresses that may already be
683 * joined on the vlan device.
685 vlan_setmulti(ifv, ifp_p);
688 * Set flags on the parent, if necessary.
690 vlan_setflags(ifv, ifp_p, 1);
693 * Connect to parent after everything have been set up,
694 * so input/output could know that vlan is ready to go
699 lwkt_replymsg(&vmsg->base.lmsg, error);
703 vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag)
705 struct netmsg_vlan vmsg;
707 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
709 bzero(&vmsg, sizeof(vmsg));
711 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
712 0, vlan_config_dispatch);
714 vmsg.nv_parent_name = parent_name;
715 vmsg.nv_vlantag = vlantag;
717 return lwkt_domsg(cpu_portfn(0), &vmsg.base.lmsg, 0);
721 vlan_unlink_dispatch(netmsg_t msg)
723 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
724 struct ifvlan *ifv = vmsg->nv_ifv;
725 struct vlan_entry *entry;
728 KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL,
729 ("vlan trunk has not been initialized yet\n"));
730 entry = &ifv->ifv_entries[cpu];
733 LIST_REMOVE(entry, ifv_link);
736 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
740 vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p)
742 struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks;
743 struct netmsg_vlan vmsg;
745 /* Assert in netisr0 */
746 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
748 KASSERT(ifp_p->if_vlantrunks != NULL,
749 ("vlan trunk has not been initialized yet\n"));
751 bzero(&vmsg, sizeof(vmsg));
753 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
754 0, vlan_unlink_dispatch);
756 vmsg.nv_ifp_p = ifp_p;
758 ifnet_domsg(&vmsg.base.lmsg, 0);
761 if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) {
762 ifp_p->if_vlantrunks = NULL;
765 * Make that all protocol threads see if_vlantrunks change.
767 netmsg_service_sync();
768 kfree(vlantrunks, M_VLAN);
774 vlan_unconfig_dispatch(netmsg_t msg)
776 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
777 struct sockaddr_dl *sdl;
779 struct ifnet *ifp_p, *ifp;
782 /* Assert in netisr0 */
787 if (ifp->if_flags & IFF_UP)
790 ifnet_serialize_all(ifp);
792 ifp->if_flags &= ~IFF_RUNNING;
795 * Save parent ifnet pointer and disconnect from parent.
797 * This is done early in this function, so input/output could
798 * know that we are disconnecting.
804 * Release vlan's serializer before reprogramming parent's
805 * multicast filter to avoid possible dead lock.
807 ifnet_deserialize_all(ifp);
811 * Since the interface is being unconfigured, we need to
812 * empty the list of multicast groups that we may have joined
813 * while we were alive from the parent's list.
815 vlan_clrmulti(ifv, ifp_p);
817 /* Clear parent's flags which was set by us. */
818 vlan_setflags(ifv, ifp_p, 0);
821 ifnet_serialize_all(ifp);
823 ifp->if_mtu = ETHERMTU;
825 /* Clear our MAC address. */
826 sdl = IF_LLSOCKADDR(ifp);
827 sdl->sdl_type = IFT_ETHER;
828 sdl->sdl_alen = ETHER_ADDR_LEN;
829 bzero(LLADDR(sdl), ETHER_ADDR_LEN);
830 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
832 ifnet_deserialize_all(ifp);
834 /* Unlink vlan from parent's vlantrunk */
835 if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL)
836 vlan_unlink(ifv, ifp_p);
839 lwkt_replymsg(&vmsg->base.lmsg, error);
843 vlan_unconfig(struct ifvlan *ifv)
845 struct netmsg_vlan vmsg;
847 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
849 bzero(&vmsg, sizeof(vmsg));
851 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
852 0, vlan_unconfig_dispatch);
855 return lwkt_domsg(cpu_portfn(0), &vmsg.base.lmsg, 0);
859 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
861 struct ifvlan *ifv = ifp->if_softc;
862 struct ifreq *ifr = (struct ifreq *)data;
867 ASSERT_IFNET_SERIALIZED_ALL(ifp);
874 * Release vlan interface's serializer to void
875 * possible dead lock.
877 ifnet_deserialize_all(ifp);
879 ifnet_serialize_all(ifp_p);
880 error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr);
881 ifnet_deserialize_all(ifp_p);
883 ifnet_serialize_all(ifp);
885 if (ifv->ifv_p == NULL && ifv->ifv_p != ifp_p) {
887 * We are disconnected from the original
888 * parent interface or the parent interface
889 * is changed, after vlan interface's
890 * serializer is released.
895 /* Limit the result to the parent's current config. */
897 struct ifmediareq *ifmr;
899 ifmr = (struct ifmediareq *) data;
900 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
902 error = copyout(&ifmr->ifm_current,
917 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
921 ifnet_deserialize_all(ifp);
922 if (vlr.vlr_parent[0] == '\0')
923 error = vlan_unconfig(ifv);
925 error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag);
926 ifnet_serialize_all(ifp);
930 bzero(&vlr, sizeof(vlr));
932 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
933 sizeof(vlr.vlr_parent));
934 vlr.vlr_tag = ifv->ifv_tag;
936 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
940 if (ifp->if_flags & IFF_UP)
943 ifp->if_flags &= ~IFF_RUNNING;
946 * We should propagate selected flags to the parent,
947 * e.g., promiscuous mode.
949 ifnet_deserialize_all(ifp);
950 error = vlan_config_flags(ifv);
951 ifnet_serialize_all(ifp);
956 ifnet_deserialize_all(ifp);
957 error = vlan_config_multi(ifv);
958 ifnet_serialize_all(ifp);
962 error = ether_ioctl(ifp, cmd, data);
969 vlan_multi_dispatch(netmsg_t msg)
971 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
972 struct ifvlan *ifv = vmsg->nv_ifv;
976 * If we don't have a parent, just remember the membership for
979 if (ifv->ifv_p != NULL)
980 error = vlan_setmulti(ifv, ifv->ifv_p);
981 lwkt_replymsg(&vmsg->base.lmsg, error);
985 vlan_config_multi(struct ifvlan *ifv)
987 struct netmsg_vlan vmsg;
989 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
991 bzero(&vmsg, sizeof(vmsg));
993 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
994 0, vlan_multi_dispatch);
997 return lwkt_domsg(cpu_portfn(0), &vmsg.base.lmsg, 0);
1001 vlan_flags_dispatch(netmsg_t msg)
1003 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
1004 struct ifvlan *ifv = vmsg->nv_ifv;
1008 * If we don't have a parent, just remember the flags for
1011 if (ifv->ifv_p != NULL)
1012 error = vlan_setflags(ifv, ifv->ifv_p, 1);
1013 lwkt_replymsg(&vmsg->base.lmsg, error);
1017 vlan_config_flags(struct ifvlan *ifv)
1019 struct netmsg_vlan vmsg;
1021 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
1023 bzero(&vmsg, sizeof(vmsg));
1025 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
1026 0, vlan_flags_dispatch);
1029 return lwkt_domsg(cpu_portfn(0), &vmsg.base.lmsg, 0);