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.36 2008/06/24 11:40:56 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.
49 #include "opt_ethernet.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
56 #include <sys/module.h>
57 #include <sys/queue.h>
58 #include <sys/socket.h>
59 #include <sys/sockio.h>
60 #include <sys/sysctl.h>
62 #include <sys/thread2.h>
65 #include <net/ethernet.h>
67 #include <net/if_arp.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/ifq_var.h>
71 #include <net/if_clone.h>
72 #include <net/netmsg2.h>
75 #include <netinet/in.h>
76 #include <netinet/if_ether.h>
79 #include <net/vlan/if_vlan_var.h>
80 #include <net/vlan/if_vlan_ether.h>
84 struct vlan_mc_entry
{
85 struct ether_addr mc_addr
;
86 SLIST_ENTRY(vlan_mc_entry
) mc_entries
;
91 LIST_ENTRY(vlan_entry
) ifv_link
;
95 struct arpcom ifv_ac
; /* make this an interface */
96 struct ifnet
*ifv_p
; /* parent inteface of this vlan */
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 nv_nmsg
;
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);
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
*);
138 static int vlan_input(struct mbuf
*m
, struct mbuf_chain
*);
140 static void vlan_input2(struct mbuf
*);
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(struct netmsg
*);
152 static void vlan_unconfig_dispatch(struct netmsg
*);
153 static void vlan_link_dispatch(struct netmsg
*);
154 static void vlan_unlink_dispatch(struct netmsg
*);
155 static void vlan_multi_dispatch(struct netmsg
*);
156 static void vlan_ifdetach_dispatch(struct netmsg
*);
158 static eventhandler_tag vlan_ifdetach_cookie
;
159 static struct if_clone vlan_cloner
=
160 IF_CLONE_INITIALIZER("vlan", vlan_clone_create
, vlan_clone_destroy
,
164 vlan_forwardmsg(struct lwkt_msg
*lmsg
, int next_cpu
)
166 if (next_cpu
< ncpus
)
167 lwkt_forwardmsg(ifnet_portfn(next_cpu
), lmsg
);
169 lwkt_replymsg(lmsg
, 0);
173 * Program our multicast filter. What we're actually doing is
174 * programming the multicast filter of the parent. This has the
175 * side effect of causing the parent interface to receive multicast
176 * traffic that it doesn't really want, which ends up being discarded
177 * later by the upper protocol layers. Unfortunately, there's no way
178 * to avoid this: there really is only one physical interface.
181 vlan_setmulti(struct ifvlan
*ifv
, struct ifnet
*ifp_p
)
183 struct ifmultiaddr
*ifma
, *rifma
= NULL
;
184 struct vlan_mc_entry
*mc
= NULL
;
185 struct sockaddr_dl sdl
;
186 struct ifnet
*ifp
= &ifv
->ifv_if
;
188 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
191 * First, remove any existing filter entries.
193 vlan_clrmulti(ifv
, ifp_p
);
196 * Now program new ones.
198 bzero(&sdl
, sizeof(sdl
));
199 sdl
.sdl_len
= sizeof(sdl
);
200 sdl
.sdl_family
= AF_LINK
;
201 sdl
.sdl_index
= ifp_p
->if_index
;
202 sdl
.sdl_type
= IFT_ETHER
;
203 sdl
.sdl_alen
= ETHER_ADDR_LEN
;
205 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
208 if (ifma
->ifma_addr
->sa_family
!= AF_LINK
)
212 mc
= kmalloc(sizeof(struct vlan_mc_entry
), M_VLAN
, M_WAITOK
);
213 bcopy(LLADDR((struct sockaddr_dl
*)ifma
->ifma_addr
),
214 &mc
->mc_addr
, ETHER_ADDR_LEN
);
215 SLIST_INSERT_HEAD(&ifv
->vlan_mc_listhead
, mc
, mc_entries
);
217 /* Program the parent multicast filter */
218 bcopy(LLADDR((struct sockaddr_dl
*)ifma
->ifma_addr
),
219 LLADDR(&sdl
), ETHER_ADDR_LEN
);
220 error
= if_addmulti(ifp_p
, (struct sockaddr
*)&sdl
, &rifma
);
228 vlan_clrmulti(struct ifvlan
*ifv
, struct ifnet
*ifp_p
)
230 struct vlan_mc_entry
*mc
;
231 struct sockaddr_dl sdl
;
233 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
235 bzero(&sdl
, sizeof(sdl
));
236 sdl
.sdl_len
= sizeof(sdl
);
237 sdl
.sdl_family
= AF_LINK
;
238 sdl
.sdl_index
= ifp_p
->if_index
;
239 sdl
.sdl_type
= IFT_ETHER
;
240 sdl
.sdl_alen
= ETHER_ADDR_LEN
;
242 while ((mc
= SLIST_FIRST(&ifv
->vlan_mc_listhead
)) != NULL
) {
243 bcopy(&mc
->mc_addr
, LLADDR(&sdl
), ETHER_ADDR_LEN
);
244 if_delmulti(ifp_p
, (struct sockaddr
*)&sdl
); /* ignore error */
246 SLIST_REMOVE_HEAD(&ifv
->vlan_mc_listhead
, mc_entries
);
252 vlan_modevent(module_t mod
, int type
, void *data
)
256 LIST_INIT(&ifv_list
);
257 vlan_input_p
= vlan_input
;
259 vlan_input2_p
= vlan_input2
;
261 vlan_input2_p
= NULL
;
263 vlan_ifdetach_cookie
=
264 EVENTHANDLER_REGISTER(ifnet_detach_event
,
266 EVENTHANDLER_PRI_ANY
);
267 if_clone_attach(&vlan_cloner
);
271 if_clone_detach(&vlan_cloner
);
273 vlan_input2_p
= NULL
;
274 EVENTHANDLER_DEREGISTER(ifnet_detach_event
,
275 vlan_ifdetach_cookie
);
276 while (!LIST_EMPTY(&ifv_list
))
277 vlan_clone_destroy(&LIST_FIRST(&ifv_list
)->ifv_if
);
283 static moduledata_t vlan_mod
= {
289 DECLARE_MODULE(if_vlan
, vlan_mod
, SI_SUB_PSEUDO
, SI_ORDER_ANY
);
292 vlan_ifdetach_dispatch(struct netmsg
*nmsg
)
294 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
295 struct ifnet
*ifp_p
= vmsg
->nv_ifp_p
;
296 struct vlan_trunk
*vlantrunks
, *trunk
;
297 struct vlan_entry
*ifve
;
299 vlantrunks
= ifp_p
->if_vlantrunks
;
300 if (vlantrunks
== NULL
)
302 trunk
= &vlantrunks
[mycpuid
];
304 while (ifp_p
->if_vlantrunks
&&
305 (ifve
= LIST_FIRST(&trunk
->vlan_list
)) != NULL
)
306 vlan_unconfig(ifve
->ifv
);
308 lwkt_replymsg(&nmsg
->nm_lmsg
, 0);
312 vlan_ifdetach(void *arg __unused
, struct ifnet
*ifp
)
314 struct netmsg_vlan vmsg
;
317 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
319 bzero(&vmsg
, sizeof(vmsg
));
320 nmsg
= &vmsg
.nv_nmsg
;
322 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_ifdetach_dispatch
);
325 lwkt_domsg(cpu_portfn(0), &nmsg
->nm_lmsg
, 0);
329 vlan_clone_create(struct if_clone
*ifc
, int unit
)
335 vlan_size
= sizeof(struct ifvlan
)
336 + ((ncpus
- 1) * sizeof(struct vlan_entry
));
337 ifv
= kmalloc(vlan_size
, M_VLAN
, M_WAITOK
| M_ZERO
);
338 SLIST_INIT(&ifv
->vlan_mc_listhead
);
339 for (i
= 0; i
< ncpus
; ++i
)
340 ifv
->ifv_entries
[i
].ifv
= ifv
;
342 crit_enter(); /* XXX not MP safe */
343 LIST_INSERT_HEAD(&ifv_list
, ifv
, ifv_list
);
348 if_initname(ifp
, "vlan", unit
);
349 /* NB: flags are not set here */
350 ifp
->if_linkmib
= &ifv
->ifv_mib
;
351 ifp
->if_linkmiblen
= sizeof ifv
->ifv_mib
;
352 /* NB: mtu is not set here */
354 ifp
->if_init
= vlan_init
;
355 ifp
->if_start
= vlan_start
;
356 ifp
->if_ioctl
= vlan_ioctl
;
357 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
358 ifq_set_ready(&ifp
->if_snd
);
359 ether_ifattach(ifp
, ifv
->ifv_ac
.ac_enaddr
, NULL
);
360 /* Now undo some of the damage... */
361 ifp
->if_data
.ifi_type
= IFT_L2VLAN
;
362 ifp
->if_data
.ifi_hdrlen
= EVL_ENCAPLEN
;
368 vlan_clone_destroy(struct ifnet
*ifp
)
370 struct ifvlan
*ifv
= ifp
->if_softc
;
372 crit_enter(); /* XXX not MP safe */
373 LIST_REMOVE(ifv
, ifv_list
);
385 struct ifvlan
*ifv
= xsc
;
386 struct ifnet
*ifp
= &ifv
->ifv_if
;
388 ASSERT_SERIALIZED(ifp
->if_serializer
);
390 if (ifv
->ifv_p
!= NULL
)
391 ifp
->if_flags
|= IFF_RUNNING
;
395 vlan_start(struct ifnet
*ifp
)
397 struct ifvlan
*ifv
= ifp
->if_softc
;
398 struct ifnet
*ifp_p
= ifv
->ifv_p
;
401 ASSERT_SERIALIZED(ifp
->if_serializer
);
404 ifq_purge(&ifp
->if_snd
);
408 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
412 struct netmsg_packet
*nmp
;
414 struct lwkt_port
*port
;
416 m
= ifq_dequeue(&ifp
->if_snd
, NULL
);
422 * Do not run parent's if_start() if the parent is not up,
423 * or parent's driver will cause a system crash.
425 if ((ifp_p
->if_flags
& (IFF_UP
| IFF_RUNNING
)) !=
426 (IFF_UP
| IFF_RUNNING
)) {
428 ifp
->if_data
.ifi_collisions
++;
433 * We need some way to tell the interface where the packet
434 * came from so that it knows how to find the VLAN tag to
435 * use, so we set the ether_vlantag in the mbuf packet header
436 * to our vlan tag. We also set the M_VLANTAG flag in the
437 * mbuf to let the parent driver know that the ether_vlantag
440 m
->m_pkthdr
.ether_vlantag
= ifv
->ifv_tag
;
441 m
->m_flags
|= M_VLANTAG
;
443 nmp
= &m
->m_hdr
.mh_netmsg
;
444 nmsg
= &nmp
->nm_netmsg
;
446 netmsg_init(nmsg
, &netisr_apanic_rport
, 0, vlan_start_dispatch
);
448 nmsg
->nm_lmsg
.u
.ms_resultp
= ifp_p
;
450 port
= cpu_portfn(ifp_p
->if_index
% ncpus
/* XXX */);
451 lwkt_sendmsg(port
, &nmp
->nm_netmsg
.nm_lmsg
);
457 vlan_input(struct mbuf
*m
, struct mbuf_chain
*chain
)
459 struct ifvlan
*ifv
= NULL
;
461 struct vlan_trunk
*vlantrunks
;
462 struct vlan_entry
*entry
;
464 rcvif
= m
->m_pkthdr
.rcvif
;
465 ASSERT_SERIALIZED(rcvif
->if_serializer
);
466 KKASSERT(m
->m_flags
& M_VLANTAG
);
468 vlantrunks
= rcvif
->if_vlantrunks
;
469 if (vlantrunks
== NULL
) {
476 LIST_FOREACH(entry
, &vlantrunks
[mycpuid
].vlan_list
, ifv_link
) {
477 if (entry
->ifv
->ifv_tag
==
478 EVL_VLANOFTAG(m
->m_pkthdr
.ether_vlantag
)) {
486 * Packet is discarded if:
487 * - no corresponding vlan(4) interface
488 * - vlan(4) interface has not been completely set up yet,
489 * or is being destroyed (ifv->ifv_p != rcvif)
490 * - vlan(4) interface is not brought up
492 if (ifv
== NULL
|| ifv
->ifv_p
!= rcvif
||
493 (ifv
->ifv_if
.if_flags
& IFF_UP
) == 0) {
496 return -1; /* so ether_input can take note */
500 * Clear M_VLANTAG, before the packet is handed to
503 m
->m_flags
&= ~M_VLANTAG
;
505 ifv
->ifv_if
.if_ipackets
++;
506 lwkt_serialize_exit(rcvif
->if_serializer
);
507 lwkt_serialize_enter(ifv
->ifv_if
.if_serializer
);
508 ether_input_chain(&ifv
->ifv_if
, m
, chain
);
509 lwkt_serialize_exit(ifv
->ifv_if
.if_serializer
);
510 lwkt_serialize_enter(rcvif
->if_serializer
);
517 vlan_input2(struct mbuf
*m
)
519 struct ifvlan
*ifv
= NULL
;
520 struct ifnet
*rcvif
, *ifp
;
521 struct vlan_trunk
*vlantrunks
;
522 struct vlan_entry
*entry
;
524 rcvif
= m
->m_pkthdr
.rcvif
;
525 KKASSERT(m
->m_flags
& M_VLANTAG
);
527 vlantrunks
= rcvif
->if_vlantrunks
;
528 if (vlantrunks
== NULL
) {
534 crit_enter(); /* XXX Necessary? */
535 LIST_FOREACH(entry
, &vlantrunks
[mycpuid
].vlan_list
, ifv_link
) {
536 if (entry
->ifv
->ifv_tag
==
537 EVL_VLANOFTAG(m
->m_pkthdr
.ether_vlantag
)) {
545 * Packet is discarded if:
546 * - no corresponding vlan(4) interface
547 * - vlan(4) interface has not been completely set up yet,
548 * or is being destroyed (ifv->ifv_p != rcvif)
549 * - vlan(4) interface is not brought up
551 if (ifv
== NULL
|| ifv
->ifv_p
!= rcvif
||
552 (ifv
->ifv_if
.if_flags
& IFF_UP
) == 0) {
560 * Clear M_VLANTAG, before the packet is handed to
563 m
->m_flags
&= ~M_VLANTAG
;
565 /* Change receiving interface */
566 m
->m_pkthdr
.rcvif
= ifp
;
568 /* Update statistics */
570 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
571 if (m
->m_flags
& (M_MCAST
| M_BCAST
))
576 if (ifp
->if_flags
& IFF_MONITOR
) {
578 * Interface marked for monitoring; discard packet.
583 ether_input_oncpu(ifp
, m
);
586 #endif /* ETHER_INPUT2 */
589 vlan_link_dispatch(struct netmsg
*nmsg
)
591 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
592 struct ifvlan
*ifv
= vmsg
->nv_ifv
;
593 struct ifnet
*ifp_p
= vmsg
->nv_ifp_p
;
594 struct vlan_entry
*entry
;
595 struct vlan_trunk
*vlantrunks
, *trunk
;
598 vlantrunks
= ifp_p
->if_vlantrunks
;
599 KASSERT(vlantrunks
!= NULL
,
600 ("vlan trunk has not been initialized yet\n"));
602 entry
= &ifv
->ifv_entries
[cpu
];
603 trunk
= &vlantrunks
[cpu
];
606 LIST_INSERT_HEAD(&trunk
->vlan_list
, entry
, ifv_link
);
609 vlan_forwardmsg(&nmsg
->nm_lmsg
, cpu
+ 1);
613 vlan_link(struct ifvlan
*ifv
, struct ifnet
*ifp_p
)
615 struct netmsg_vlan vmsg
;
618 /* Assert in netisr0 */
619 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
621 if (ifp_p
->if_vlantrunks
== NULL
) {
622 struct vlan_trunk
*vlantrunks
;
625 vlantrunks
= kmalloc(sizeof(*vlantrunks
) * ncpus
, M_VLAN
,
627 for (i
= 0; i
< ncpus
; ++i
)
628 LIST_INIT(&vlantrunks
[i
].vlan_list
);
630 ifp_p
->if_vlantrunks
= vlantrunks
;
633 bzero(&vmsg
, sizeof(vmsg
));
634 nmsg
= &vmsg
.nv_nmsg
;
636 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_link_dispatch
);
638 vmsg
.nv_ifp_p
= ifp_p
;
640 lwkt_domsg(ifnet_portfn(0), &nmsg
->nm_lmsg
, 0);
644 vlan_config_dispatch(struct netmsg
*nmsg
)
646 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
648 struct ifnet
*ifp_p
, *ifp
;
649 struct sockaddr_dl
*sdl1
, *sdl2
;
652 /* Assert in netisr0 */
654 ifp_p
= ifunit(vmsg
->nv_parent_name
);
660 if (ifp_p
->if_data
.ifi_type
!= IFT_ETHER
) {
661 error
= EPROTONOSUPPORT
;
673 /* Link vlan into parent's vlantrunk */
674 vlan_link(ifv
, ifp_p
);
676 lwkt_serialize_enter(ifp
->if_serializer
);
678 ifv
->ifv_tag
= vmsg
->nv_vlantag
;
679 if (ifp_p
->if_capenable
& IFCAP_VLAN_MTU
)
680 ifp
->if_mtu
= ifp_p
->if_mtu
;
682 ifp
->if_mtu
= ifp_p
->if_data
.ifi_mtu
- EVL_ENCAPLEN
;
685 * Copy only a selected subset of flags from the parent.
686 * Other flags are none of our business.
688 ifp
->if_flags
= (ifp_p
->if_flags
&
689 (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
| IFF_POINTOPOINT
));
692 * Set up our ``Ethernet address'' to reflect the underlying
693 * physical interface's.
695 sdl1
= IF_LLSOCKADDR(ifp
);
696 sdl2
= IF_LLSOCKADDR(ifp_p
);
697 sdl1
->sdl_type
= IFT_ETHER
;
698 sdl1
->sdl_alen
= ETHER_ADDR_LEN
;
699 bcopy(LLADDR(sdl2
), LLADDR(sdl1
), ETHER_ADDR_LEN
);
700 bcopy(LLADDR(sdl2
), ifv
->ifv_ac
.ac_enaddr
, ETHER_ADDR_LEN
);
703 * Release vlan's serializer before reprogramming parent's
704 * multicast filter to avoid possible dead lock.
706 lwkt_serialize_exit(ifp
->if_serializer
);
709 * Configure multicast addresses that may already be
710 * joined on the vlan device.
712 vlan_setmulti(ifv
, ifp_p
);
715 * Connect to parent after everything have been set up,
716 * so input/output could know that vlan is ready to go
721 lwkt_replymsg(&nmsg
->nm_lmsg
, error
);
725 vlan_config(struct ifvlan
*ifv
, const char *parent_name
, uint16_t vlantag
)
727 struct netmsg_vlan vmsg
;
730 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
732 bzero(&vmsg
, sizeof(vmsg
));
733 nmsg
= &vmsg
.nv_nmsg
;
735 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_config_dispatch
);
737 vmsg
.nv_parent_name
= parent_name
;
738 vmsg
.nv_vlantag
= vlantag
;
740 return lwkt_domsg(cpu_portfn(0), &nmsg
->nm_lmsg
, 0);
744 vlan_unlink_dispatch(struct netmsg
*nmsg
)
746 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
747 struct ifvlan
*ifv
= vmsg
->nv_ifv
;
748 struct vlan_entry
*entry
;
751 KASSERT(vmsg
->nv_ifp_p
->if_vlantrunks
!= NULL
,
752 ("vlan trunk has not been initialized yet\n"));
753 entry
= &ifv
->ifv_entries
[cpu
];
756 LIST_REMOVE(entry
, ifv_link
);
759 vlan_forwardmsg(&nmsg
->nm_lmsg
, cpu
+ 1);
763 vlan_unlink(struct ifvlan
*ifv
, struct ifnet
*ifp_p
)
765 struct vlan_trunk
*vlantrunks
= ifp_p
->if_vlantrunks
;
766 struct netmsg_vlan vmsg
;
769 /* Assert in netisr0 */
770 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
772 KASSERT(ifp_p
->if_vlantrunks
!= NULL
,
773 ("vlan trunk has not been initialized yet\n"));
775 bzero(&vmsg
, sizeof(vmsg
));
776 nmsg
= &vmsg
.nv_nmsg
;
778 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_unlink_dispatch
);
780 vmsg
.nv_ifp_p
= ifp_p
;
782 lwkt_domsg(ifnet_portfn(0), &nmsg
->nm_lmsg
, 0);
785 if (LIST_EMPTY(&vlantrunks
[mycpuid
].vlan_list
)) {
787 ifp_p
->if_vlantrunks
= NULL
;
788 netmsg_service_sync();
789 kfree(vlantrunks
, M_VLAN
);
791 lwkt_serialize_enter(ifp_p
->if_serializer
);
792 kfree(ifp_p
->if_vlantrunks
, M_VLAN
);
793 ifp_p
->if_vlantrunks
= NULL
;
794 lwkt_serialize_exit(ifp_p
->if_serializer
);
801 vlan_unconfig_dispatch(struct netmsg
*nmsg
)
803 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
804 struct sockaddr_dl
*sdl
;
806 struct ifnet
*ifp_p
, *ifp
;
809 /* Assert in netisr0 */
814 if (ifp
->if_flags
& IFF_UP
)
817 lwkt_serialize_enter(ifp
->if_serializer
);
819 ifp
->if_flags
&= ~IFF_RUNNING
;
822 * Save parent ifnet pointer and disconnect from parent.
824 * This is done early in this function, so input/output could
825 * know that we are disconnecting.
831 * Release vlan's serializer before reprogramming parent's
832 * multicast filter to avoid possible dead lock.
834 lwkt_serialize_exit(ifp
->if_serializer
);
838 * Since the interface is being unconfigured, we need to
839 * empty the list of multicast groups that we may have joined
840 * while we were alive from the parent's list.
842 vlan_clrmulti(ifv
, ifp_p
);
845 lwkt_serialize_enter(ifp
->if_serializer
);
847 ifp
->if_mtu
= ETHERMTU
;
849 /* Clear our MAC address. */
850 sdl
= IF_LLSOCKADDR(ifp
);
851 sdl
->sdl_type
= IFT_ETHER
;
852 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
853 bzero(LLADDR(sdl
), ETHER_ADDR_LEN
);
854 bzero(ifv
->ifv_ac
.ac_enaddr
, ETHER_ADDR_LEN
);
856 lwkt_serialize_exit(ifp
->if_serializer
);
858 /* Unlink vlan from parent's vlantrunk */
859 if (ifp_p
!= NULL
&& ifp_p
->if_vlantrunks
!= NULL
)
860 vlan_unlink(ifv
, ifp_p
);
863 lwkt_replymsg(&nmsg
->nm_lmsg
, error
);
867 vlan_unconfig(struct ifvlan
*ifv
)
869 struct netmsg_vlan vmsg
;
872 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
874 bzero(&vmsg
, sizeof(vmsg
));
875 nmsg
= &vmsg
.nv_nmsg
;
877 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_unconfig_dispatch
);
880 return lwkt_domsg(cpu_portfn(0), &nmsg
->nm_lmsg
, 0);
884 vlan_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
886 struct ifvlan
*ifv
= ifp
->if_softc
;
887 struct ifreq
*ifr
= (struct ifreq
*)data
;
892 ASSERT_SERIALIZED(ifp
->if_serializer
);
899 * Release vlan interface's serializer to void
900 * possible dead lock.
902 lwkt_serialize_exit(ifp
->if_serializer
);
904 lwkt_serialize_enter(ifp_p
->if_serializer
);
905 error
= ifp_p
->if_ioctl(ifp_p
, SIOCGIFMEDIA
, data
, cr
);
906 lwkt_serialize_exit(ifp_p
->if_serializer
);
908 lwkt_serialize_enter(ifp
->if_serializer
);
910 if (ifv
->ifv_p
== NULL
&& ifv
->ifv_p
!= ifp_p
) {
912 * We are disconnected from the original
913 * parent interface or the parent interface
914 * is changed, after vlan interface's
915 * serializer is released.
920 /* Limit the result to the parent's current config. */
922 struct ifmediareq
*ifmr
;
924 ifmr
= (struct ifmediareq
*) data
;
925 if (ifmr
->ifm_count
>= 1 && ifmr
->ifm_ulist
) {
927 error
= copyout(&ifmr
->ifm_current
,
942 error
= copyin(ifr
->ifr_data
, &vlr
, sizeof vlr
);
946 lwkt_serialize_exit(ifp
->if_serializer
);
947 if (vlr
.vlr_parent
[0] == '\0')
948 error
= vlan_unconfig(ifv
);
950 error
= vlan_config(ifv
, vlr
.vlr_parent
, vlr
.vlr_tag
);
951 lwkt_serialize_enter(ifp
->if_serializer
);
955 bzero(&vlr
, sizeof(vlr
));
957 strlcpy(vlr
.vlr_parent
, ifv
->ifv_p
->if_xname
,
958 sizeof(vlr
.vlr_parent
));
959 vlr
.vlr_tag
= ifv
->ifv_tag
;
961 error
= copyout(&vlr
, ifr
->ifr_data
, sizeof vlr
);
965 if (ifp
->if_flags
& IFF_UP
)
968 ifp
->if_flags
&= ~IFF_RUNNING
;
971 * We don't support promiscuous mode
972 * right now because it would require help from the
973 * underlying drivers, which hasn't been implemented.
975 if (ifr
->ifr_flags
& IFF_PROMISC
) {
976 ifp
->if_flags
&= ~IFF_PROMISC
;
983 lwkt_serialize_exit(ifp
->if_serializer
);
984 error
= vlan_config_multi(ifv
);
985 lwkt_serialize_enter(ifp
->if_serializer
);
989 error
= ether_ioctl(ifp
, cmd
, data
);
996 vlan_multi_dispatch(struct netmsg
*nmsg
)
998 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
999 struct ifvlan
*ifv
= vmsg
->nv_ifv
;
1003 * If we don't have a parent, just remember the membership for
1006 if (ifv
->ifv_p
!= NULL
)
1007 error
= vlan_setmulti(ifv
, ifv
->ifv_p
);
1008 lwkt_replymsg(&nmsg
->nm_lmsg
, error
);
1012 vlan_config_multi(struct ifvlan
*ifv
)
1014 struct netmsg_vlan vmsg
;
1015 struct netmsg
*nmsg
;
1017 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
1019 bzero(&vmsg
, sizeof(vmsg
));
1020 nmsg
= &vmsg
.nv_nmsg
;
1022 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_multi_dispatch
);
1025 return lwkt_domsg(cpu_portfn(0), &nmsg
->nm_lmsg
, 0);