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.
45 * Note about vlan's MP safe approach:
47 * - All configuration operation, e.g. config, unconfig and change flags,
48 * is serialized by netisr0; not by vlan's serializer
50 * - Parent interface's trunk and vlans are linked in the following
53 * +--------------+--------+--------+--------+--------+
54 * | parent ifnet |trunk[0]|trunk[1]|trunk[2]|trunk[3]|
55 * +--------------+--------+--------+--------+--------+
58 * +--------------+--------+--------+--------+--------+
59 * | vlan ifnet |entry[0]|entry[1]|entry[2]|entry[3]|
60 * +--------------+--------+--------+--------+--------+
63 * +--------------+--------+--------+--------+--------+
64 * | vlan ifnet |entry[0]|entry[1]|entry[2]|entry[3]|
65 * +--------------+--------+--------+--------+--------+
67 * - Vlan is linked/unlinked onto parent interface's trunk using following
72 * netisr0 <---------------------------------------------+
77 * V fwdmsg fwdmsg fwdmsg |
78 * ifnet0 --------> ifnet1 --------> ifnet2 --------> ifnet3
79 * (link/unlink) (link/unlink) (link/unlink) (link/unlink)
81 * - Parent interface's trunk is destroyed in the following lockless way:
83 * old_trunk = ifp->if_vlantrunks;
84 * ifp->if_vlantrunks = NULL;
85 * netmsg_service_sync();
89 * Since all of the accessing of if_vlantrunks only happens in network
90 * threads (percpu netisr and ifnet threads), after netmsg_service_sync()
91 * the network threads are promised to see only NULL if_vlantrunks; we
92 * are safe to free the "to be destroyed" parent interface's trunk
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/kernel.h>
104 #include <sys/malloc.h>
105 #include <sys/mbuf.h>
106 #include <sys/module.h>
107 #include <sys/queue.h>
108 #include <sys/socket.h>
109 #include <sys/sockio.h>
110 #include <sys/sysctl.h>
112 #include <sys/thread2.h>
115 #include <net/ethernet.h>
117 #include <net/if_arp.h>
118 #include <net/if_dl.h>
119 #include <net/if_types.h>
120 #include <net/ifq_var.h>
121 #include <net/if_clone.h>
122 #include <net/netmsg2.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 *);
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 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(cpu_portfn(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)
503 struct ifvlan *ifv = ifp->if_softc;
504 struct ifnet *ifp_p = ifv->ifv_p;
507 ASSERT_IFNET_SERIALIZED_TX(ifp);
510 ifq_purge(&ifp->if_snd);
514 if ((ifp->if_flags & IFF_RUNNING) == 0)
518 struct netmsg_packet *nmp;
519 struct lwkt_port *port;
521 m = ifq_dequeue(&ifp->if_snd, NULL);
527 * Do not run parent's if_start() if the parent is not up,
528 * or parent's driver will cause a system crash.
530 if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) !=
531 (IFF_UP | IFF_RUNNING)) {
533 ifp->if_data.ifi_collisions++;
538 * We need some way to tell the interface where the packet
539 * came from so that it knows how to find the VLAN tag to
540 * use, so we set the ether_vlantag in the mbuf packet header
541 * to our vlan tag. We also set the M_VLANTAG flag in the
542 * mbuf to let the parent driver know that the ether_vlantag
545 m->m_pkthdr.ether_vlantag = ifv->ifv_tag;
546 m->m_flags |= M_VLANTAG;
548 nmp = &m->m_hdr.mh_netmsg;
550 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
551 0, vlan_start_dispatch);
553 nmp->base.lmsg.u.ms_resultp = ifp_p;
555 port = cpu_portfn(ifp_p->if_index % ncpus /* XXX */);
556 lwkt_sendmsg(port, &nmp->base.lmsg);
562 vlan_input(struct mbuf *m)
564 struct ifvlan *ifv = NULL;
566 struct vlan_trunk *vlantrunks;
567 struct vlan_entry *entry;
569 rcvif = m->m_pkthdr.rcvif;
570 KKASSERT(m->m_flags & M_VLANTAG);
572 vlantrunks = rcvif->if_vlantrunks;
573 if (vlantrunks == NULL) {
579 crit_enter(); /* XXX Necessary? */
580 LIST_FOREACH(entry, &vlantrunks[mycpuid].vlan_list, ifv_link) {
581 if (entry->ifv->ifv_tag ==
582 EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag)) {
590 * Packet is discarded if:
591 * - no corresponding vlan(4) interface
592 * - vlan(4) interface has not been completely set up yet,
593 * or is being destroyed (ifv->ifv_p != rcvif)
595 if (ifv == NULL || ifv->ifv_p != rcvif) {
602 * Clear M_VLANTAG, before the packet is handed to
605 m->m_flags &= ~M_VLANTAG;
607 ether_reinput_oncpu(&ifv->ifv_if, m, REINPUT_RUNBPF);
611 vlan_link_dispatch(netmsg_t msg)
613 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
614 struct ifvlan *ifv = vmsg->nv_ifv;
615 struct ifnet *ifp_p = vmsg->nv_ifp_p;
616 struct vlan_entry *entry;
617 struct vlan_trunk *vlantrunks, *trunk;
620 vlantrunks = ifp_p->if_vlantrunks;
621 KASSERT(vlantrunks != NULL,
622 ("vlan trunk has not been initialized yet\n"));
624 entry = &ifv->ifv_entries[cpu];
625 trunk = &vlantrunks[cpu];
628 LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link);
631 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
635 vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p)
637 struct netmsg_vlan vmsg;
639 /* Assert in netisr0 */
640 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
642 if (ifp_p->if_vlantrunks == NULL) {
643 struct vlan_trunk *vlantrunks;
646 vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN,
648 for (i = 0; i < ncpus; ++i)
649 LIST_INIT(&vlantrunks[i].vlan_list);
651 ifp_p->if_vlantrunks = vlantrunks;
654 bzero(&vmsg, sizeof(vmsg));
656 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
657 0, vlan_link_dispatch);
659 vmsg.nv_ifp_p = ifp_p;
661 ifnet_domsg(&vmsg.base.lmsg, 0);
665 vlan_config_dispatch(netmsg_t msg)
667 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
669 struct ifnet *ifp_p, *ifp;
670 struct sockaddr_dl *sdl1, *sdl2;
673 /* Assert in netisr0 */
675 ifp_p = ifunit(vmsg->nv_parent_name);
681 if (ifp_p->if_data.ifi_type != IFT_ETHER) {
682 error = EPROTONOSUPPORT;
694 /* Link vlan into parent's vlantrunk */
695 vlan_link(ifv, ifp_p);
697 ifnet_serialize_all(ifp);
699 ifv->ifv_tag = vmsg->nv_vlantag;
700 if (ifp_p->if_capenable & IFCAP_VLAN_MTU)
701 ifp->if_mtu = ifp_p->if_mtu;
703 ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN;
706 * Copy only a selected subset of flags from the parent.
707 * Other flags are none of our business.
709 #define VLAN_INHERIT_FLAGS (IFF_BROADCAST | IFF_MULTICAST | \
710 IFF_SIMPLEX | IFF_POINTOPOINT)
712 ifp->if_flags &= ~VLAN_INHERIT_FLAGS;
713 ifp->if_flags |= (ifp_p->if_flags & VLAN_INHERIT_FLAGS);
715 #undef VLAN_INHERIT_FLAGS
718 * Set up our ``Ethernet address'' to reflect the underlying
719 * physical interface's.
721 sdl1 = IF_LLSOCKADDR(ifp);
722 sdl2 = IF_LLSOCKADDR(ifp_p);
723 sdl1->sdl_type = IFT_ETHER;
724 sdl1->sdl_alen = ETHER_ADDR_LEN;
725 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
726 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
729 * Release vlan's serializer before reprogramming parent's
730 * multicast filter to avoid possible dead lock.
732 ifnet_deserialize_all(ifp);
735 * Configure multicast addresses that may already be
736 * joined on the vlan device.
738 vlan_setmulti(ifv, ifp_p);
741 * Set flags on the parent, if necessary.
743 vlan_setflags(ifv, ifp_p, 1);
746 * Connect to parent after everything have been set up,
747 * so input/output could know that vlan is ready to go
752 lwkt_replymsg(&vmsg->base.lmsg, error);
756 vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag)
758 struct netmsg_vlan vmsg;
760 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
762 bzero(&vmsg, sizeof(vmsg));
764 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
765 0, vlan_config_dispatch);
767 vmsg.nv_parent_name = parent_name;
768 vmsg.nv_vlantag = vlantag;
770 return lwkt_domsg(cpu_portfn(0), &vmsg.base.lmsg, 0);
774 vlan_unlink_dispatch(netmsg_t msg)
776 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
777 struct ifvlan *ifv = vmsg->nv_ifv;
778 struct vlan_entry *entry;
781 KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL,
782 ("vlan trunk has not been initialized yet\n"));
783 entry = &ifv->ifv_entries[cpu];
786 LIST_REMOVE(entry, ifv_link);
789 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
793 vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p)
795 struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks;
796 struct netmsg_vlan vmsg;
798 /* Assert in netisr0 */
799 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
801 KASSERT(ifp_p->if_vlantrunks != NULL,
802 ("vlan trunk has not been initialized yet\n"));
804 bzero(&vmsg, sizeof(vmsg));
806 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
807 0, vlan_unlink_dispatch);
809 vmsg.nv_ifp_p = ifp_p;
811 ifnet_domsg(&vmsg.base.lmsg, 0);
814 if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) {
815 ifp_p->if_vlantrunks = NULL;
818 * Make that all protocol threads see if_vlantrunks change.
820 netmsg_service_sync();
821 kfree(vlantrunks, M_VLAN);
827 vlan_unconfig_dispatch(netmsg_t msg)
829 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
830 struct sockaddr_dl *sdl;
832 struct ifnet *ifp_p, *ifp;
835 /* Assert in netisr0 */
840 if (ifp->if_flags & IFF_UP)
843 ifnet_serialize_all(ifp);
845 ifp->if_flags &= ~IFF_RUNNING;
848 * Save parent ifnet pointer and disconnect from parent.
850 * This is done early in this function, so input/output could
851 * know that we are disconnecting.
857 * Release vlan's serializer before reprogramming parent's
858 * multicast filter to avoid possible dead lock.
860 ifnet_deserialize_all(ifp);
864 * Since the interface is being unconfigured, we need to
865 * empty the list of multicast groups that we may have joined
866 * while we were alive from the parent's list.
868 vlan_clrmulti(ifv, ifp_p);
870 /* Clear parent's flags which was set by us. */
871 vlan_setflags(ifv, ifp_p, 0);
874 ifnet_serialize_all(ifp);
876 ifp->if_mtu = ETHERMTU;
878 /* Clear our MAC address. */
879 sdl = IF_LLSOCKADDR(ifp);
880 sdl->sdl_type = IFT_ETHER;
881 sdl->sdl_alen = ETHER_ADDR_LEN;
882 bzero(LLADDR(sdl), ETHER_ADDR_LEN);
883 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
885 ifnet_deserialize_all(ifp);
887 /* Unlink vlan from parent's vlantrunk */
888 if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL)
889 vlan_unlink(ifv, ifp_p);
892 lwkt_replymsg(&vmsg->base.lmsg, error);
896 vlan_unconfig(struct ifvlan *ifv)
898 struct netmsg_vlan vmsg;
900 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
902 bzero(&vmsg, sizeof(vmsg));
904 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
905 0, vlan_unconfig_dispatch);
908 return lwkt_domsg(cpu_portfn(0), &vmsg.base.lmsg, 0);
912 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
914 struct ifvlan *ifv = ifp->if_softc;
915 struct ifreq *ifr = (struct ifreq *)data;
920 ASSERT_IFNET_SERIALIZED_ALL(ifp);
927 * Release vlan interface's serializer to void
928 * possible dead lock.
930 ifnet_deserialize_all(ifp);
932 ifnet_serialize_all(ifp_p);
933 error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr);
934 ifnet_deserialize_all(ifp_p);
936 ifnet_serialize_all(ifp);
938 if (ifv->ifv_p == NULL || ifv->ifv_p != ifp_p) {
940 * We are disconnected from the original
941 * parent interface or the parent interface
942 * is changed, after vlan interface's
943 * serializer is released.
948 /* Limit the result to the parent's current config. */
950 struct ifmediareq *ifmr;
952 ifmr = (struct ifmediareq *) data;
953 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
955 error = copyout(&ifmr->ifm_current,
970 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
974 ifnet_deserialize_all(ifp);
975 if (vlr.vlr_parent[0] == '\0')
976 error = vlan_unconfig(ifv);
978 error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag);
979 ifnet_serialize_all(ifp);
983 bzero(&vlr, sizeof(vlr));
985 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
986 sizeof(vlr.vlr_parent));
987 vlr.vlr_tag = ifv->ifv_tag;
989 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
993 if (ifp->if_flags & IFF_UP)
996 ifp->if_flags &= ~IFF_RUNNING;
999 * We should propagate selected flags to the parent,
1000 * e.g., promiscuous mode.
1002 ifnet_deserialize_all(ifp);
1003 error = vlan_config_flags(ifv);
1004 ifnet_serialize_all(ifp);
1009 ifnet_deserialize_all(ifp);
1010 error = vlan_config_multi(ifv);
1011 ifnet_serialize_all(ifp);
1015 error = ether_ioctl(ifp, cmd, data);
1022 vlan_multi_dispatch(netmsg_t msg)
1024 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
1025 struct ifvlan *ifv = vmsg->nv_ifv;
1029 * If we don't have a parent, just remember the membership for
1032 if (ifv->ifv_p != NULL)
1033 error = vlan_setmulti(ifv, ifv->ifv_p);
1034 lwkt_replymsg(&vmsg->base.lmsg, error);
1038 vlan_config_multi(struct ifvlan *ifv)
1040 struct netmsg_vlan vmsg;
1042 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
1044 bzero(&vmsg, sizeof(vmsg));
1046 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
1047 0, vlan_multi_dispatch);
1050 return lwkt_domsg(cpu_portfn(0), &vmsg.base.lmsg, 0);
1054 vlan_flags_dispatch(netmsg_t msg)
1056 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
1057 struct ifvlan *ifv = vmsg->nv_ifv;
1061 * If we don't have a parent, just remember the flags for
1064 if (ifv->ifv_p != NULL)
1065 error = vlan_setflags(ifv, ifv->ifv_p, 1);
1066 lwkt_replymsg(&vmsg->base.lmsg, error);
1070 vlan_config_flags(struct ifvlan *ifv)
1072 struct netmsg_vlan vmsg;
1074 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
1076 bzero(&vmsg, sizeof(vmsg));
1078 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
1079 0, vlan_flags_dispatch);
1082 return lwkt_domsg(cpu_portfn(0), &vmsg.base.lmsg, 0);