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 *);
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)
502 struct ifvlan *ifv = ifp->if_softc;
503 struct ifnet *ifp_p = ifv->ifv_p;
506 ASSERT_IFNET_SERIALIZED_TX(ifp);
509 ifq_purge(&ifp->if_snd);
513 if ((ifp->if_flags & IFF_RUNNING) == 0)
517 struct netmsg_packet *nmp;
519 m = ifq_dequeue(&ifp->if_snd, NULL);
525 * Do not run parent's if_start() if the parent is not up,
526 * or parent's driver will cause a system crash.
528 if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) !=
529 (IFF_UP | IFF_RUNNING)) {
531 ifp->if_data.ifi_collisions++;
536 * We need some way to tell the interface where the packet
537 * came from so that it knows how to find the VLAN tag to
538 * use, so we set the ether_vlantag in the mbuf packet header
539 * to our vlan tag. We also set the M_VLANTAG flag in the
540 * mbuf to let the parent driver know that the ether_vlantag
543 m->m_pkthdr.ether_vlantag = ifv->ifv_tag;
544 m->m_flags |= M_VLANTAG;
546 nmp = &m->m_hdr.mh_netmsg;
548 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
549 0, vlan_start_dispatch);
551 nmp->base.lmsg.u.ms_resultp = ifp_p;
553 lwkt_sendmsg(netisr_portfn(ifp_p->if_start_cpuid(ifp_p)),
560 vlan_input(struct mbuf *m)
562 struct ifvlan *ifv = NULL;
564 struct vlan_trunk *vlantrunks;
565 struct vlan_entry *entry;
567 rcvif = m->m_pkthdr.rcvif;
568 KKASSERT(m->m_flags & M_VLANTAG);
570 vlantrunks = rcvif->if_vlantrunks;
571 if (vlantrunks == NULL) {
577 crit_enter(); /* XXX Necessary? */
578 LIST_FOREACH(entry, &vlantrunks[mycpuid].vlan_list, ifv_link) {
579 if (entry->ifv->ifv_tag ==
580 EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag)) {
588 * Packet is discarded if:
589 * - no corresponding vlan(4) interface
590 * - vlan(4) interface has not been completely set up yet,
591 * or is being destroyed (ifv->ifv_p != rcvif)
593 if (ifv == NULL || ifv->ifv_p != rcvif) {
600 * Clear M_VLANTAG, before the packet is handed to
603 m->m_flags &= ~M_VLANTAG;
605 ether_reinput_oncpu(&ifv->ifv_if, m, REINPUT_RUNBPF);
609 vlan_link_dispatch(netmsg_t msg)
611 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
612 struct ifvlan *ifv = vmsg->nv_ifv;
613 struct ifnet *ifp_p = vmsg->nv_ifp_p;
614 struct vlan_entry *entry;
615 struct vlan_trunk *vlantrunks, *trunk;
618 vlantrunks = ifp_p->if_vlantrunks;
619 KASSERT(vlantrunks != NULL,
620 ("vlan trunk has not been initialized yet"));
622 entry = &ifv->ifv_entries[cpu];
623 trunk = &vlantrunks[cpu];
626 LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link);
629 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
633 vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p)
635 struct netmsg_vlan vmsg;
637 /* Assert in netisr0 */
638 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
640 if (ifp_p->if_vlantrunks == NULL) {
641 struct vlan_trunk *vlantrunks;
644 vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN,
646 for (i = 0; i < ncpus; ++i)
647 LIST_INIT(&vlantrunks[i].vlan_list);
649 ifp_p->if_vlantrunks = vlantrunks;
652 bzero(&vmsg, sizeof(vmsg));
654 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
655 0, vlan_link_dispatch);
657 vmsg.nv_ifp_p = ifp_p;
659 ifnet_domsg(&vmsg.base.lmsg, 0);
663 vlan_config_dispatch(netmsg_t msg)
665 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
667 struct ifnet *ifp_p, *ifp;
668 struct sockaddr_dl *sdl1, *sdl2;
671 /* Assert in netisr0 */
673 ifp_p = ifunit(vmsg->nv_parent_name);
679 if (ifp_p->if_data.ifi_type != IFT_ETHER) {
680 error = EPROTONOSUPPORT;
692 /* Link vlan into parent's vlantrunk */
693 vlan_link(ifv, ifp_p);
695 ifnet_serialize_all(ifp);
697 ifv->ifv_tag = vmsg->nv_vlantag;
698 if (ifp_p->if_capenable & IFCAP_VLAN_MTU)
699 ifp->if_mtu = ifp_p->if_mtu;
701 ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN;
704 * Copy only a selected subset of flags from the parent.
705 * Other flags are none of our business.
707 #define VLAN_INHERIT_FLAGS (IFF_BROADCAST | IFF_MULTICAST | \
708 IFF_SIMPLEX | IFF_POINTOPOINT)
710 ifp->if_flags &= ~VLAN_INHERIT_FLAGS;
711 ifp->if_flags |= (ifp_p->if_flags & VLAN_INHERIT_FLAGS);
713 #undef VLAN_INHERIT_FLAGS
716 * Set up our ``Ethernet address'' to reflect the underlying
717 * physical interface's.
719 sdl1 = IF_LLSOCKADDR(ifp);
720 sdl2 = IF_LLSOCKADDR(ifp_p);
721 sdl1->sdl_type = IFT_ETHER;
722 sdl1->sdl_alen = ETHER_ADDR_LEN;
723 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
724 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
727 * Release vlan's serializer before reprogramming parent's
728 * multicast filter to avoid possible dead lock.
730 ifnet_deserialize_all(ifp);
733 * Configure multicast addresses that may already be
734 * joined on the vlan device.
736 vlan_setmulti(ifv, ifp_p);
739 * Set flags on the parent, if necessary.
741 vlan_setflags(ifv, ifp_p, 1);
744 * Connect to parent after everything have been set up,
745 * so input/output could know that vlan is ready to go
750 lwkt_replymsg(&vmsg->base.lmsg, error);
754 vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag)
756 struct netmsg_vlan vmsg;
758 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
760 bzero(&vmsg, sizeof(vmsg));
762 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
763 0, vlan_config_dispatch);
765 vmsg.nv_parent_name = parent_name;
766 vmsg.nv_vlantag = vlantag;
768 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0);
772 vlan_unlink_dispatch(netmsg_t msg)
774 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
775 struct ifvlan *ifv = vmsg->nv_ifv;
776 struct vlan_entry *entry;
779 KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL,
780 ("vlan trunk has not been initialized yet"));
781 entry = &ifv->ifv_entries[cpu];
784 LIST_REMOVE(entry, ifv_link);
787 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
791 vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p)
793 struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks;
794 struct netmsg_vlan vmsg;
796 /* Assert in netisr0 */
797 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
799 KASSERT(ifp_p->if_vlantrunks != NULL,
800 ("vlan trunk has not been initialized yet"));
802 bzero(&vmsg, sizeof(vmsg));
804 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
805 0, vlan_unlink_dispatch);
807 vmsg.nv_ifp_p = ifp_p;
809 ifnet_domsg(&vmsg.base.lmsg, 0);
812 if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) {
813 ifp_p->if_vlantrunks = NULL;
816 * Make sure that all protocol threads see if_vlantrunks change.
818 netmsg_service_sync();
819 kfree(vlantrunks, M_VLAN);
825 vlan_unconfig_dispatch(netmsg_t msg)
827 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
828 struct sockaddr_dl *sdl;
830 struct ifnet *ifp_p, *ifp;
833 /* Assert in netisr0 */
838 if (ifp->if_flags & IFF_UP)
841 ifnet_serialize_all(ifp);
843 ifp->if_flags &= ~IFF_RUNNING;
846 * Save parent ifnet pointer and disconnect from parent.
848 * This is done early in this function, so input/output could
849 * know that we are disconnecting.
855 * Release vlan's serializer before reprogramming parent's
856 * multicast filter to avoid possible dead lock.
858 ifnet_deserialize_all(ifp);
862 * Since the interface is being unconfigured, we need to
863 * empty the list of multicast groups that we may have joined
864 * while we were alive from the parent's list.
866 vlan_clrmulti(ifv, ifp_p);
868 /* Clear parent's flags which was set by us. */
869 vlan_setflags(ifv, ifp_p, 0);
872 ifnet_serialize_all(ifp);
874 ifp->if_mtu = ETHERMTU;
876 /* Clear our MAC address. */
877 sdl = IF_LLSOCKADDR(ifp);
878 sdl->sdl_type = IFT_ETHER;
879 sdl->sdl_alen = ETHER_ADDR_LEN;
880 bzero(LLADDR(sdl), ETHER_ADDR_LEN);
881 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
883 ifnet_deserialize_all(ifp);
885 /* Unlink vlan from parent's vlantrunk */
886 if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL)
887 vlan_unlink(ifv, ifp_p);
890 lwkt_replymsg(&vmsg->base.lmsg, error);
894 vlan_unconfig(struct ifvlan *ifv)
896 struct netmsg_vlan vmsg;
898 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
900 bzero(&vmsg, sizeof(vmsg));
902 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
903 0, vlan_unconfig_dispatch);
906 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0);
910 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
912 struct ifvlan *ifv = ifp->if_softc;
913 struct ifreq *ifr = (struct ifreq *)data;
918 ASSERT_IFNET_SERIALIZED_ALL(ifp);
925 * Release vlan interface's serializer to void
926 * possible dead lock.
928 ifnet_deserialize_all(ifp);
930 ifnet_serialize_all(ifp_p);
931 error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr);
932 ifnet_deserialize_all(ifp_p);
934 ifnet_serialize_all(ifp);
936 if (ifv->ifv_p == NULL || ifv->ifv_p != ifp_p) {
938 * We are disconnected from the original
939 * parent interface or the parent interface
940 * is changed, after vlan interface's
941 * serializer is released.
946 /* Limit the result to the parent's current config. */
948 struct ifmediareq *ifmr;
950 ifmr = (struct ifmediareq *) data;
951 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
953 error = copyout(&ifmr->ifm_current,
968 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
972 ifnet_deserialize_all(ifp);
973 if (vlr.vlr_parent[0] == '\0')
974 error = vlan_unconfig(ifv);
976 error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag);
977 ifnet_serialize_all(ifp);
981 bzero(&vlr, sizeof(vlr));
983 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
984 sizeof(vlr.vlr_parent));
985 vlr.vlr_tag = ifv->ifv_tag;
987 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
991 if (ifp->if_flags & IFF_UP)
994 ifp->if_flags &= ~IFF_RUNNING;
997 * We should propagate selected flags to the parent,
998 * e.g., promiscuous mode.
1000 ifnet_deserialize_all(ifp);
1001 error = vlan_config_flags(ifv);
1002 ifnet_serialize_all(ifp);
1007 ifnet_deserialize_all(ifp);
1008 error = vlan_config_multi(ifv);
1009 ifnet_serialize_all(ifp);
1013 error = ether_ioctl(ifp, cmd, data);
1020 vlan_multi_dispatch(netmsg_t msg)
1022 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
1023 struct ifvlan *ifv = vmsg->nv_ifv;
1027 * If we don't have a parent, just remember the membership for
1030 if (ifv->ifv_p != NULL)
1031 error = vlan_setmulti(ifv, ifv->ifv_p);
1032 lwkt_replymsg(&vmsg->base.lmsg, error);
1036 vlan_config_multi(struct ifvlan *ifv)
1038 struct netmsg_vlan vmsg;
1040 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
1042 bzero(&vmsg, sizeof(vmsg));
1044 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
1045 0, vlan_multi_dispatch);
1048 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0);
1052 vlan_flags_dispatch(netmsg_t msg)
1054 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
1055 struct ifvlan *ifv = vmsg->nv_ifv;
1059 * If we don't have a parent, just remember the flags for
1062 if (ifv->ifv_p != NULL)
1063 error = vlan_setflags(ifv, ifv->ifv_p, 1);
1064 lwkt_replymsg(&vmsg->base.lmsg, error);
1068 vlan_config_flags(struct ifvlan *ifv)
1070 struct netmsg_vlan vmsg;
1072 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
1074 bzero(&vmsg, sizeof(vmsg));
1076 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
1077 0, vlan_flags_dispatch);
1080 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0);