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.30 2008/03/16 15:30:20 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 * XXX It's incorrect to assume that we must always kludge up
46 * headers on the physical device's behalf: some devices support
47 * VLAN tag insertion and extraction in firmware. For these cases,
48 * one can change the behavior of the vlan interface by setting
49 * the LINK0 flag on it (that is setting the vlan interface's LINK0
50 * flag, _not_ the parent's LINK0 flag; we try to leave the parent
51 * alone). If the interface has the LINK0 flag set, then it will
52 * not modify the ethernet header on output, because the parent
53 * can do that for itself. On input, the parent can call vlan_input_tag()
54 * directly in order to supply us with an incoming mbuf and the vlan
55 * tag value that goes with it.
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
68 #include <sys/module.h>
69 #include <sys/queue.h>
70 #include <sys/socket.h>
71 #include <sys/sockio.h>
72 #include <sys/sysctl.h>
74 #include <sys/thread2.h>
77 #include <net/ethernet.h>
79 #include <net/if_arp.h>
80 #include <net/if_dl.h>
81 #include <net/if_types.h>
82 #include <net/ifq_var.h>
83 #include <net/if_clone.h>
84 #include <net/netmsg2.h>
87 #include <netinet/in.h>
88 #include <netinet/if_ether.h>
91 #include <net/vlan/if_vlan_var.h>
92 #include <net/vlan/if_vlan_ether.h>
96 struct vlan_mc_entry
{
97 struct ether_addr mc_addr
;
98 SLIST_ENTRY(vlan_mc_entry
) mc_entries
;
103 LIST_ENTRY(vlan_entry
) ifv_link
;
107 struct arpcom ifv_ac
; /* make this an interface */
108 struct ifnet
*ifv_p
; /* parent inteface of this vlan */
111 uint16_t ifvm_proto
; /* encapsulation ethertype */
112 uint16_t ifvm_tag
; /* tag to apply on packets leaving if */
114 SLIST_HEAD(, vlan_mc_entry
) vlan_mc_listhead
;
115 LIST_ENTRY(ifvlan
) ifv_list
;
116 struct vlan_entry ifv_entries
[1];
118 #define ifv_if ifv_ac.ac_if
119 #define ifv_tag ifv_mib.ifvm_tag
122 LIST_HEAD(, vlan_entry
) vlan_list
;
126 struct netmsg nv_nmsg
;
127 struct ifvlan
*nv_ifv
;
128 struct ifnet
*nv_ifp_p
;
129 const char *nv_parent_name
;
133 #define VLANNAME "vlan"
135 SYSCTL_DECL(_net_link
);
136 SYSCTL_NODE(_net_link
, IFT_L2VLAN
, vlan
, CTLFLAG_RW
, 0, "IEEE 802.1Q VLAN");
137 SYSCTL_NODE(_net_link_vlan
, PF_LINK
, link
, CTLFLAG_RW
, 0, "for consistency");
139 static MALLOC_DEFINE(M_VLAN
, "vlan", "802.1Q Virtual LAN Interface");
140 static LIST_HEAD(, ifvlan
) ifv_list
;
142 static int vlan_clone_create(struct if_clone
*, int);
143 static void vlan_clone_destroy(struct ifnet
*);
144 static void vlan_ifdetach(void *, struct ifnet
*);
146 static void vlan_init(void *);
147 static void vlan_start(struct ifnet
*);
148 static int vlan_ioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
150 static int vlan_input(const struct ether_header
*eh
, struct mbuf
*m
);
151 static int vlan_input_tag(struct mbuf
*m
, uint16_t t
);
153 static void vlan_clrmulti(struct ifvlan
*, struct ifnet
*);
154 static int vlan_setmulti(struct ifvlan
*, struct ifnet
*);
155 static int vlan_config_multi(struct ifvlan
*);
156 static int vlan_config(struct ifvlan
*, const char *, uint16_t);
157 static int vlan_unconfig(struct ifvlan
*);
158 static void vlan_link(struct ifvlan
*, struct ifnet
*);
159 static void vlan_unlink(struct ifvlan
*, struct ifnet
*);
161 static void vlan_config_dispatch(struct netmsg
*);
162 static void vlan_unconfig_dispatch(struct netmsg
*);
163 static void vlan_link_dispatch(struct netmsg
*);
164 static void vlan_unlink_dispatch(struct netmsg
*);
165 static void vlan_multi_dispatch(struct netmsg
*);
166 static void vlan_ifdetach_dispatch(struct netmsg
*);
168 static eventhandler_tag vlan_ifdetach_cookie
;
169 static struct if_clone vlan_cloner
=
170 IF_CLONE_INITIALIZER("vlan", vlan_clone_create
, vlan_clone_destroy
,
174 vlan_forwardmsg(struct lwkt_msg
*lmsg
, int next_cpu
)
176 if (next_cpu
< ncpus
)
177 lwkt_forwardmsg(ifa_portfn(next_cpu
), lmsg
);
179 lwkt_replymsg(lmsg
, 0);
183 * Program our multicast filter. What we're actually doing is
184 * programming the multicast filter of the parent. This has the
185 * side effect of causing the parent interface to receive multicast
186 * traffic that it doesn't really want, which ends up being discarded
187 * later by the upper protocol layers. Unfortunately, there's no way
188 * to avoid this: there really is only one physical interface.
191 vlan_setmulti(struct ifvlan
*ifv
, struct ifnet
*ifp_p
)
193 struct ifmultiaddr
*ifma
, *rifma
= NULL
;
194 struct vlan_mc_entry
*mc
= NULL
;
195 struct sockaddr_dl sdl
;
196 struct ifnet
*ifp
= &ifv
->ifv_if
;
198 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
201 * First, remove any existing filter entries.
203 vlan_clrmulti(ifv
, ifp_p
);
206 * Now program new ones.
208 bzero(&sdl
, sizeof(sdl
));
209 sdl
.sdl_len
= sizeof(sdl
);
210 sdl
.sdl_family
= AF_LINK
;
211 sdl
.sdl_index
= ifp_p
->if_index
;
212 sdl
.sdl_type
= IFT_ETHER
;
213 sdl
.sdl_alen
= ETHER_ADDR_LEN
;
215 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
218 if (ifma
->ifma_addr
->sa_family
!= AF_LINK
)
222 mc
= kmalloc(sizeof(struct vlan_mc_entry
), M_VLAN
, M_WAITOK
);
223 bcopy(LLADDR((struct sockaddr_dl
*)ifma
->ifma_addr
),
224 &mc
->mc_addr
, ETHER_ADDR_LEN
);
225 SLIST_INSERT_HEAD(&ifv
->vlan_mc_listhead
, mc
, mc_entries
);
227 /* Program the parent multicast filter */
228 bcopy(LLADDR((struct sockaddr_dl
*)ifma
->ifma_addr
),
229 LLADDR(&sdl
), ETHER_ADDR_LEN
);
230 error
= if_addmulti(ifp_p
, (struct sockaddr
*)&sdl
, &rifma
);
238 vlan_clrmulti(struct ifvlan
*ifv
, struct ifnet
*ifp_p
)
240 struct vlan_mc_entry
*mc
;
241 struct sockaddr_dl sdl
;
243 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
245 bzero(&sdl
, sizeof(sdl
));
246 sdl
.sdl_len
= sizeof(sdl
);
247 sdl
.sdl_family
= AF_LINK
;
248 sdl
.sdl_index
= ifp_p
->if_index
;
249 sdl
.sdl_type
= IFT_ETHER
;
250 sdl
.sdl_alen
= ETHER_ADDR_LEN
;
252 while ((mc
= SLIST_FIRST(&ifv
->vlan_mc_listhead
)) != NULL
) {
253 bcopy(&mc
->mc_addr
, LLADDR(&sdl
), ETHER_ADDR_LEN
);
254 if_delmulti(ifp_p
, (struct sockaddr
*)&sdl
); /* ignore error */
256 SLIST_REMOVE_HEAD(&ifv
->vlan_mc_listhead
, mc_entries
);
262 vlan_modevent(module_t mod
, int type
, void *data
)
266 LIST_INIT(&ifv_list
);
267 vlan_input_p
= vlan_input
;
268 vlan_input_tag_p
= vlan_input_tag
;
269 vlan_ifdetach_cookie
=
270 EVENTHANDLER_REGISTER(ifnet_detach_event
,
272 EVENTHANDLER_PRI_ANY
);
273 if_clone_attach(&vlan_cloner
);
277 if_clone_detach(&vlan_cloner
);
279 vlan_input_tag_p
= NULL
;
280 EVENTHANDLER_DEREGISTER(ifnet_detach_event
,
281 vlan_ifdetach_cookie
);
282 while (!LIST_EMPTY(&ifv_list
))
283 vlan_clone_destroy(&LIST_FIRST(&ifv_list
)->ifv_if
);
289 static moduledata_t vlan_mod
= {
295 DECLARE_MODULE(if_vlan
, vlan_mod
, SI_SUB_PSEUDO
, SI_ORDER_ANY
);
298 vlan_ifdetach_dispatch(struct netmsg
*nmsg
)
300 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
301 struct ifnet
*ifp_p
= vmsg
->nv_ifp_p
;
302 struct vlan_trunk
*vlantrunks
, *trunk
;
303 struct vlan_entry
*ifve
;
305 vlantrunks
= ifp_p
->if_vlantrunks
;
306 if (vlantrunks
== NULL
)
308 trunk
= &vlantrunks
[mycpuid
];
310 while (ifp_p
->if_vlantrunks
&&
311 (ifve
= LIST_FIRST(&trunk
->vlan_list
)) != NULL
)
312 vlan_unconfig(ifve
->ifv
);
314 lwkt_replymsg(&nmsg
->nm_lmsg
, 0);
318 vlan_ifdetach(void *arg __unused
, struct ifnet
*ifp
)
320 struct netmsg_vlan vmsg
;
323 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
325 bzero(&vmsg
, sizeof(vmsg
));
326 nmsg
= &vmsg
.nv_nmsg
;
328 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_ifdetach_dispatch
);
331 lwkt_domsg(cpu_portfn(0), &nmsg
->nm_lmsg
, 0);
335 vlan_clone_create(struct if_clone
*ifc
, int unit
)
341 vlan_size
= sizeof(struct ifvlan
)
342 + ((ncpus
- 1) * sizeof(struct vlan_entry
));
343 ifv
= kmalloc(vlan_size
, M_VLAN
, M_WAITOK
| M_ZERO
);
344 SLIST_INIT(&ifv
->vlan_mc_listhead
);
345 for (i
= 0; i
< ncpus
; ++i
)
346 ifv
->ifv_entries
[i
].ifv
= ifv
;
348 crit_enter(); /* XXX not MP safe */
349 LIST_INSERT_HEAD(&ifv_list
, ifv
, ifv_list
);
354 if_initname(ifp
, "vlan", unit
);
355 /* NB: flags are not set here */
356 ifp
->if_linkmib
= &ifv
->ifv_mib
;
357 ifp
->if_linkmiblen
= sizeof ifv
->ifv_mib
;
358 /* NB: mtu is not set here */
360 ifp
->if_init
= vlan_init
;
361 ifp
->if_start
= vlan_start
;
362 ifp
->if_ioctl
= vlan_ioctl
;
363 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
364 ifq_set_ready(&ifp
->if_snd
);
365 ether_ifattach(ifp
, ifv
->ifv_ac
.ac_enaddr
, NULL
);
366 /* Now undo some of the damage... */
367 ifp
->if_data
.ifi_type
= IFT_L2VLAN
;
368 ifp
->if_data
.ifi_hdrlen
= EVL_ENCAPLEN
;
374 vlan_clone_destroy(struct ifnet
*ifp
)
376 struct ifvlan
*ifv
= ifp
->if_softc
;
378 crit_enter(); /* XXX not MP safe */
379 LIST_REMOVE(ifv
, ifv_list
);
391 struct ifvlan
*ifv
= xsc
;
392 struct ifnet
*ifp
= &ifv
->ifv_if
;
394 ASSERT_SERIALIZED(ifp
->if_serializer
);
396 if (ifv
->ifv_p
!= NULL
)
397 ifp
->if_flags
|= IFF_RUNNING
;
401 vlan_start(struct ifnet
*ifp
)
403 struct ifvlan
*ifv
= ifp
->if_softc
;
404 struct ifnet
*ifp_p
= ifv
->ifv_p
;
407 ASSERT_SERIALIZED(ifp
->if_serializer
);
409 if ((ifp
->if_flags
& IFF_RUNNING
) == 0 || ifp_p
== NULL
)
412 ifp
->if_flags
|= IFF_OACTIVE
;
414 struct netmsg_packet
*nmp
;
416 struct lwkt_port
*port
;
418 m
= ifq_dequeue(&ifp
->if_snd
, NULL
);
424 * Do not run parent's if_start() if the parent is not up,
425 * or parent's driver will cause a system crash.
427 if ((ifp_p
->if_flags
& (IFF_UP
| IFF_RUNNING
)) !=
428 (IFF_UP
| IFF_RUNNING
)) {
430 ifp
->if_data
.ifi_collisions
++;
435 * We need some way to tell the interface where the packet
436 * came from so that it knows how to find the VLAN tag to
437 * use, so we set the ether_vlantag in the mbuf packet header
438 * to our vlan tag. We also set the M_VLANTAG flag in the
439 * mbuf to let the parent driver know that the ether_vlantag
442 m
->m_pkthdr
.ether_vlantag
= ifv
->ifv_tag
;
443 m
->m_flags
|= M_VLANTAG
;
445 nmp
= &m
->m_hdr
.mh_netmsg
;
446 nmsg
= &nmp
->nm_netmsg
;
448 netmsg_init(nmsg
, &netisr_apanic_rport
, 0, vlan_start_dispatch
);
450 nmsg
->nm_lmsg
.u
.ms_resultp
= ifp_p
;
452 port
= cpu_portfn(ifp_p
->if_index
% ncpus
/* XXX */);
453 lwkt_sendmsg(port
, &nmp
->nm_netmsg
.nm_lmsg
);
456 ifp
->if_flags
&= ~IFF_OACTIVE
;
460 vlan_input_tag(struct mbuf
*m
, uint16_t t
)
466 rcvif
= m
->m_pkthdr
.rcvif
;
468 ASSERT_SERIALIZED(rcvif
->if_serializer
);
471 * Fake up a header and send the packet to the physical interface's
474 if ((bif
= rcvif
->if_bpf
) != NULL
)
475 vlan_ether_ptap(bif
, m
, t
);
477 for (ifv
= LIST_FIRST(&ifv_list
); ifv
!= NULL
;
478 ifv
= LIST_NEXT(ifv
, ifv_list
)) {
479 if (rcvif
== ifv
->ifv_p
&& ifv
->ifv_tag
== t
)
483 if (ifv
== NULL
|| (ifv
->ifv_if
.if_flags
& IFF_UP
) == 0) {
485 return -1; /* So the parent can take note */
489 * Having found a valid vlan interface corresponding to
490 * the given source interface and vlan tag, run the
491 * the real packet through ether_input().
493 m
->m_pkthdr
.rcvif
= &ifv
->ifv_if
;
495 ifv
->ifv_if
.if_ipackets
++;
496 lwkt_serialize_exit(rcvif
->if_serializer
);
497 lwkt_serialize_enter(ifv
->ifv_if
.if_serializer
);
498 ether_input(&ifv
->ifv_if
, m
);
499 lwkt_serialize_exit(ifv
->ifv_if
.if_serializer
);
500 lwkt_serialize_enter(rcvif
->if_serializer
);
505 vlan_input(const struct ether_header
*eh
, struct mbuf
*m
)
509 struct ether_header eh_copy
;
511 rcvif
= m
->m_pkthdr
.rcvif
;
512 ASSERT_SERIALIZED(rcvif
->if_serializer
);
514 for (ifv
= LIST_FIRST(&ifv_list
); ifv
!= NULL
;
515 ifv
= LIST_NEXT(ifv
, ifv_list
)) {
516 if (rcvif
== ifv
->ifv_p
517 && (EVL_VLANOFTAG(ntohs(*mtod(m
, u_int16_t
*)))
522 if (ifv
== NULL
|| (ifv
->ifv_if
.if_flags
& IFF_UP
) == 0) {
525 return -1; /* so ether_input can take note */
529 * Having found a valid vlan interface corresponding to
530 * the given source interface and vlan tag, remove the
531 * remaining encapsulation (ether_vlan_header minus the ether_header
532 * that had already been removed) and run the real packet
533 * through ether_input() a second time (it had better be
537 eh_copy
.ether_type
= mtod(m
, u_int16_t
*)[1]; /* evl_proto */
538 m
->m_pkthdr
.rcvif
= &ifv
->ifv_if
;
539 m_adj(m
, EVL_ENCAPLEN
);
540 M_PREPEND(m
, ETHER_HDR_LEN
, MB_WAIT
);
541 *(struct ether_header
*)mtod(m
, void *) = eh_copy
;
543 ifv
->ifv_if
.if_ipackets
++;
544 lwkt_serialize_exit(rcvif
->if_serializer
);
545 lwkt_serialize_enter(ifv
->ifv_if
.if_serializer
);
546 ether_input(&ifv
->ifv_if
, m
);
547 lwkt_serialize_exit(ifv
->ifv_if
.if_serializer
);
548 lwkt_serialize_enter(rcvif
->if_serializer
);
553 vlan_link_dispatch(struct netmsg
*nmsg
)
555 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
556 struct ifvlan
*ifv
= vmsg
->nv_ifv
;
557 struct ifnet
*ifp_p
= vmsg
->nv_ifp_p
;
558 struct vlan_entry
*entry
;
559 struct vlan_trunk
*vlantrunks
, *trunk
;
562 vlantrunks
= ifp_p
->if_vlantrunks
;
563 KASSERT(vlantrunks
!= NULL
,
564 ("vlan trunk has not been initialized yet\n"));
566 entry
= &ifv
->ifv_entries
[cpu
];
567 trunk
= &vlantrunks
[cpu
];
570 LIST_INSERT_HEAD(&trunk
->vlan_list
, entry
, ifv_link
);
573 vlan_forwardmsg(&nmsg
->nm_lmsg
, cpu
+ 1);
577 vlan_link(struct ifvlan
*ifv
, struct ifnet
*ifp_p
)
579 struct netmsg_vlan vmsg
;
582 /* Assert in netisr0 */
583 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
585 if (ifp_p
->if_vlantrunks
== NULL
) {
586 struct vlan_trunk
*vlantrunks
;
589 vlantrunks
= kmalloc(sizeof(*vlantrunks
) * ncpus
, M_VLAN
,
591 for (i
= 0; i
< ncpus
; ++i
)
592 LIST_INIT(&vlantrunks
[i
].vlan_list
);
594 ifp_p
->if_vlantrunks
= vlantrunks
;
597 bzero(&vmsg
, sizeof(vmsg
));
598 nmsg
= &vmsg
.nv_nmsg
;
600 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_link_dispatch
);
602 vmsg
.nv_ifp_p
= ifp_p
;
604 lwkt_domsg(ifa_portfn(0), &nmsg
->nm_lmsg
, 0);
608 vlan_config_dispatch(struct netmsg
*nmsg
)
610 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
612 struct ifnet
*ifp_p
, *ifp
;
613 struct sockaddr_dl
*sdl1
, *sdl2
;
616 /* Assert in netisr0 */
618 ifp_p
= ifunit(vmsg
->nv_parent_name
);
624 if (ifp_p
->if_data
.ifi_type
!= IFT_ETHER
) {
625 error
= EPROTONOSUPPORT
;
637 /* Link vlan into parent's vlantrunk */
638 vlan_link(ifv
, ifp_p
);
640 lwkt_serialize_enter(ifp
->if_serializer
);
642 ifv
->ifv_tag
= vmsg
->nv_vlantag
;
643 if (ifp_p
->if_capenable
& IFCAP_VLAN_MTU
)
644 ifp
->if_mtu
= ifp_p
->if_mtu
;
646 ifp
->if_mtu
= ifp_p
->if_data
.ifi_mtu
- EVL_ENCAPLEN
;
649 * Copy only a selected subset of flags from the parent.
650 * Other flags are none of our business.
652 ifp
->if_flags
= (ifp_p
->if_flags
&
653 (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
| IFF_POINTOPOINT
));
656 * Set up our ``Ethernet address'' to reflect the underlying
657 * physical interface's.
659 sdl1
= IF_LLSOCKADDR(ifp
);
660 sdl2
= IF_LLSOCKADDR(ifp_p
);
661 sdl1
->sdl_type
= IFT_ETHER
;
662 sdl1
->sdl_alen
= ETHER_ADDR_LEN
;
663 bcopy(LLADDR(sdl2
), LLADDR(sdl1
), ETHER_ADDR_LEN
);
664 bcopy(LLADDR(sdl2
), ifv
->ifv_ac
.ac_enaddr
, ETHER_ADDR_LEN
);
667 * Release vlan's serializer before reprogramming parent's
668 * multicast filter to avoid possible dead lock.
670 lwkt_serialize_exit(ifp
->if_serializer
);
673 * Configure multicast addresses that may already be
674 * joined on the vlan device.
676 vlan_setmulti(ifv
, ifp_p
);
679 * Connect to parent after everything have been set up,
680 * so input/output could know that vlan is ready to go
685 lwkt_replymsg(&nmsg
->nm_lmsg
, error
);
689 vlan_config(struct ifvlan
*ifv
, const char *parent_name
, uint16_t vlantag
)
691 struct netmsg_vlan vmsg
;
694 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
696 bzero(&vmsg
, sizeof(vmsg
));
697 nmsg
= &vmsg
.nv_nmsg
;
699 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_config_dispatch
);
701 vmsg
.nv_parent_name
= parent_name
;
702 vmsg
.nv_vlantag
= vlantag
;
704 return lwkt_domsg(cpu_portfn(0), &nmsg
->nm_lmsg
, 0);
708 vlan_unlink_dispatch(struct netmsg
*nmsg
)
710 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
711 struct ifvlan
*ifv
= vmsg
->nv_ifv
;
712 struct ifnet
*ifp_p
= vmsg
->nv_ifp_p
;
713 struct vlan_entry
*entry
;
716 KASSERT(ifp_p
->if_vlantrunks
!= NULL
,
717 ("vlan trunk has not been initialized yet\n"));
718 entry
= &ifv
->ifv_entries
[cpu
];
721 LIST_REMOVE(entry
, ifv_link
);
724 vlan_forwardmsg(&nmsg
->nm_lmsg
, cpu
+ 1);
728 vlan_unlink(struct ifvlan
*ifv
, struct ifnet
*ifp_p
)
730 struct vlan_trunk
*vlantrunks
= ifp_p
->if_vlantrunks
;
731 struct netmsg_vlan vmsg
;
734 /* Assert in netisr0 */
735 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
737 KASSERT(ifp_p
->if_vlantrunks
!= NULL
,
738 ("vlan trunk has not been initialized yet\n"));
740 bzero(&vmsg
, sizeof(vmsg
));
741 nmsg
= &vmsg
.nv_nmsg
;
743 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_unlink_dispatch
);
745 vmsg
.nv_ifp_p
= ifp_p
;
747 lwkt_domsg(ifa_portfn(0), &nmsg
->nm_lmsg
, 0);
750 if (LIST_EMPTY(&vlantrunks
[mycpuid
].vlan_list
)) {
752 ifp_p
->if_vlantrunks
= NULL
;
753 netmsg_service_sync();
754 kfree(vlantrunks
, M_VLAN
);
756 lwkt_serialize_enter(ifp_p
->if_serializer
);
757 kfree(ifp_p
->if_vlantrunks
, M_VLAN
);
758 ifp_p
->if_vlantrunks
= NULL
;
759 lwkt_serialize_exit(ifp_p
->if_serializer
);
766 vlan_unconfig_dispatch(struct netmsg
*nmsg
)
768 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
769 struct sockaddr_dl
*sdl
;
771 struct ifnet
*ifp_p
, *ifp
;
774 /* Assert in netisr0 */
779 if (ifp
->if_flags
& IFF_UP
)
782 lwkt_serialize_enter(ifp
->if_serializer
);
784 ifp
->if_flags
&= ~IFF_RUNNING
;
787 * Save parent ifnet pointer and disconnect from parent.
789 * This is done early in this function, so input/output could
790 * know that we are disconnecting.
796 * Release vlan's serializer before reprogramming parent's
797 * multicast filter to avoid possible dead lock.
799 lwkt_serialize_exit(ifp
->if_serializer
);
803 * Since the interface is being unconfigured, we need to
804 * empty the list of multicast groups that we may have joined
805 * while we were alive from the parent's list.
807 vlan_clrmulti(ifv
, ifp_p
);
810 lwkt_serialize_enter(ifp
->if_serializer
);
812 ifp
->if_mtu
= ETHERMTU
;
814 /* Clear our MAC address. */
815 sdl
= IF_LLSOCKADDR(ifp
);
816 sdl
->sdl_type
= IFT_ETHER
;
817 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
818 bzero(LLADDR(sdl
), ETHER_ADDR_LEN
);
819 bzero(ifv
->ifv_ac
.ac_enaddr
, ETHER_ADDR_LEN
);
821 lwkt_serialize_exit(ifp
->if_serializer
);
823 /* Unlink vlan from parent's vlantrunk */
824 if (ifp_p
!= NULL
&& ifp_p
->if_vlantrunks
!= NULL
)
825 vlan_unlink(ifv
, ifp_p
);
828 lwkt_replymsg(&nmsg
->nm_lmsg
, error
);
832 vlan_unconfig(struct ifvlan
*ifv
)
834 struct netmsg_vlan vmsg
;
837 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
839 bzero(&vmsg
, sizeof(vmsg
));
840 nmsg
= &vmsg
.nv_nmsg
;
842 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_unconfig_dispatch
);
845 return lwkt_domsg(cpu_portfn(0), &nmsg
->nm_lmsg
, 0);
849 vlan_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
851 struct ifvlan
*ifv
= ifp
->if_softc
;
852 struct ifreq
*ifr
= (struct ifreq
*)data
;
857 ASSERT_SERIALIZED(ifp
->if_serializer
);
863 lwkt_serialize_exit(ifp
->if_serializer
);
865 lwkt_serialize_enter(ifp_p
->if_serializer
);
866 error
= ifp_p
->if_ioctl(ifp_p
, SIOCGIFMEDIA
, data
, cr
);
867 lwkt_serialize_exit(ifp_p
->if_serializer
);
869 lwkt_serialize_enter(ifp
->if_serializer
);
870 /* Limit the result to the parent's current config. */
872 struct ifmediareq
*ifmr
;
874 ifmr
= (struct ifmediareq
*) data
;
875 if (ifmr
->ifm_count
>= 1 && ifmr
->ifm_ulist
) {
877 error
= copyout(&ifmr
->ifm_current
,
892 error
= copyin(ifr
->ifr_data
, &vlr
, sizeof vlr
);
896 lwkt_serialize_exit(ifp
->if_serializer
);
897 if (vlr
.vlr_parent
[0] == '\0')
898 error
= vlan_unconfig(ifv
);
900 error
= vlan_config(ifv
, vlr
.vlr_parent
, vlr
.vlr_tag
);
901 lwkt_serialize_enter(ifp
->if_serializer
);
905 bzero(&vlr
, sizeof(vlr
));
907 strlcpy(vlr
.vlr_parent
, ifv
->ifv_p
->if_xname
,
908 sizeof(vlr
.vlr_parent
));
909 vlr
.vlr_tag
= ifv
->ifv_tag
;
911 error
= copyout(&vlr
, ifr
->ifr_data
, sizeof vlr
);
915 if (ifp
->if_flags
& IFF_UP
)
918 ifp
->if_flags
&= ~IFF_RUNNING
;
921 * We don't support promiscuous mode
922 * right now because it would require help from the
923 * underlying drivers, which hasn't been implemented.
925 if (ifr
->ifr_flags
& IFF_PROMISC
) {
926 ifp
->if_flags
&= ~IFF_PROMISC
;
933 lwkt_serialize_exit(ifp
->if_serializer
);
934 error
= vlan_config_multi(ifv
);
935 lwkt_serialize_enter(ifp
->if_serializer
);
939 error
= ether_ioctl(ifp
, cmd
, data
);
946 vlan_multi_dispatch(struct netmsg
*nmsg
)
948 struct netmsg_vlan
*vmsg
= (struct netmsg_vlan
*)nmsg
;
949 struct ifvlan
*ifv
= vmsg
->nv_ifv
;
953 * If we don't have a parent, just remember the membership for
956 if (ifv
->ifv_p
!= NULL
)
957 error
= vlan_setmulti(ifv
, ifv
->ifv_p
);
958 lwkt_replymsg(&nmsg
->nm_lmsg
, error
);
962 vlan_config_multi(struct ifvlan
*ifv
)
964 struct netmsg_vlan vmsg
;
967 ASSERT_NOT_SERIALIZED(ifv
->ifv_if
.if_serializer
);
969 bzero(&vmsg
, sizeof(vmsg
));
970 nmsg
= &vmsg
.nv_nmsg
;
972 netmsg_init(nmsg
, &curthread
->td_msgport
, 0, vlan_multi_dispatch
);
975 return lwkt_domsg(cpu_portfn(0), &nmsg
->nm_lmsg
, 0);