Rework vlan configuration processing:
[dragonfly/netmp.git] / sys / net / vlan / if_vlan.c
blob83b36cf106945a6a130d6775e84795ed1ed83f7c
1 /*
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
14 * warranty.
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
27 * SUCH DAMAGE.
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.
58 #ifndef NVLAN
59 #include "use_vlan.h"
60 #endif
61 #include "opt_inet.h"
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #include <sys/mbuf.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>
73 #include <sys/bus.h>
74 #include <sys/thread2.h>
76 #include <net/bpf.h>
77 #include <net/ethernet.h>
78 #include <net/if.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>
86 #ifdef INET
87 #include <netinet/in.h>
88 #include <netinet/if_ether.h>
89 #endif
91 #include <net/vlan/if_vlan_var.h>
92 #include <net/vlan/if_vlan_ether.h>
94 struct ifvlan;
96 struct vlan_mc_entry {
97 struct ether_addr mc_addr;
98 SLIST_ENTRY(vlan_mc_entry) mc_entries;
101 struct vlan_entry {
102 struct ifvlan *ifv;
103 LIST_ENTRY(vlan_entry) ifv_link;
106 struct ifvlan {
107 struct arpcom ifv_ac; /* make this an interface */
108 struct ifnet *ifv_p; /* parent inteface of this vlan */
109 struct ifv_linkmib {
110 int ifvm_parent;
111 uint16_t ifvm_proto; /* encapsulation ethertype */
112 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
113 } ifv_mib;
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
121 struct vlan_trunk {
122 LIST_HEAD(, vlan_entry) vlan_list;
125 struct netmsg_vlan {
126 struct netmsg nv_nmsg;
127 struct ifvlan *nv_ifv;
128 struct ifnet *nv_ifp_p;
129 const char *nv_parent_name;
130 uint16_t nv_vlantag;
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,
171 NVLAN, IF_MAXUNIT);
173 static __inline void
174 vlan_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
176 if (next_cpu < ncpus)
177 lwkt_forwardmsg(ifa_portfn(next_cpu), lmsg);
178 else
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.
190 static int
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) {
216 int error;
218 if (ifma->ifma_addr->sa_family != AF_LINK)
219 continue;
221 /* Save a copy */
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);
231 if (error)
232 return error;
234 return 0;
237 static void
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);
257 kfree(mc, M_VLAN);
261 static int
262 vlan_modevent(module_t mod, int type, void *data)
264 switch (type) {
265 case MOD_LOAD:
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,
271 vlan_ifdetach, NULL,
272 EVENTHANDLER_PRI_ANY);
273 if_clone_attach(&vlan_cloner);
274 break;
276 case MOD_UNLOAD:
277 if_clone_detach(&vlan_cloner);
278 vlan_input_p = NULL;
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);
284 break;
286 return 0;
289 static moduledata_t vlan_mod = {
290 "if_vlan",
291 vlan_modevent,
295 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
297 static void
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)
307 goto reply;
308 trunk = &vlantrunks[mycpuid];
310 while (ifp_p->if_vlantrunks &&
311 (ifve = LIST_FIRST(&trunk->vlan_list)) != NULL)
312 vlan_unconfig(ifve->ifv);
313 reply:
314 lwkt_replymsg(&nmsg->nm_lmsg, 0);
317 static void
318 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
320 struct netmsg_vlan vmsg;
321 struct netmsg *nmsg;
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);
329 vmsg.nv_ifp_p = ifp;
331 lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
334 static int
335 vlan_clone_create(struct if_clone *ifc, int unit)
337 struct ifvlan *ifv;
338 struct ifnet *ifp;
339 int vlan_size, i;
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);
350 crit_exit();
352 ifp = &ifv->ifv_if;
353 ifp->if_softc = ifv;
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;
370 return (0);
373 static void
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);
380 crit_exit();
382 vlan_unconfig(ifv);
383 ether_ifdetach(ifp);
385 kfree(ifv, M_VLAN);
388 static void
389 vlan_init(void *xsc)
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;
400 static void
401 vlan_start(struct ifnet *ifp)
403 struct ifvlan *ifv = ifp->if_softc;
404 struct ifnet *ifp_p = ifv->ifv_p;
405 struct mbuf *m;
407 ASSERT_SERIALIZED(ifp->if_serializer);
409 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifp_p == NULL)
410 return;
412 ifp->if_flags |= IFF_OACTIVE;
413 for (;;) {
414 struct netmsg_packet *nmp;
415 struct netmsg *nmsg;
416 struct lwkt_port *port;
418 m = ifq_dequeue(&ifp->if_snd, NULL);
419 if (m == NULL)
420 break;
421 BPF_MTAP(ifp, m);
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)) {
429 m_freem(m);
430 ifp->if_data.ifi_collisions++;
431 continue;
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
440 * is really valid.
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);
449 nmp->nm_packet = m;
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);
454 ifp->if_opackets++;
456 ifp->if_flags &= ~IFF_OACTIVE;
459 static int
460 vlan_input_tag(struct mbuf *m, uint16_t t)
462 struct bpf_if *bif;
463 struct ifvlan *ifv;
464 struct ifnet *rcvif;
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
472 * bpf tap if active.
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)
480 break;
483 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
484 m_freem(m);
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);
501 return 0;
504 static int
505 vlan_input(const struct ether_header *eh, struct mbuf *m)
507 struct ifvlan *ifv;
508 struct ifnet *rcvif;
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 *)))
518 == ifv->ifv_tag))
519 break;
522 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
523 rcvif->if_noproto++;
524 m_freem(m);
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
534 * reentrant!).
536 eh_copy = *eh;
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);
549 return 0;
552 static void
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;
560 int cpu = mycpuid;
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];
569 crit_enter();
570 LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link);
571 crit_exit();
573 vlan_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
576 static void
577 vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p)
579 struct netmsg_vlan vmsg;
580 struct netmsg *nmsg;
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;
587 int i;
589 vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN,
590 M_WAITOK | M_ZERO);
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);
601 vmsg.nv_ifv = ifv;
602 vmsg.nv_ifp_p = ifp_p;
604 lwkt_domsg(ifa_portfn(0), &nmsg->nm_lmsg, 0);
607 static void
608 vlan_config_dispatch(struct netmsg *nmsg)
610 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
611 struct ifvlan *ifv;
612 struct ifnet *ifp_p, *ifp;
613 struct sockaddr_dl *sdl1, *sdl2;
614 int error;
616 /* Assert in netisr0 */
618 ifp_p = ifunit(vmsg->nv_parent_name);
619 if (ifp_p == NULL) {
620 error = ENOENT;
621 goto reply;
624 if (ifp_p->if_data.ifi_type != IFT_ETHER) {
625 error = EPROTONOSUPPORT;
626 goto reply;
629 ifv = vmsg->nv_ifv;
630 ifp = &ifv->ifv_if;
632 if (ifv->ifv_p) {
633 error = EBUSY;
634 goto reply;
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;
645 else
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
682 ifv->ifv_p = ifp_p;
683 error = 0;
684 reply:
685 lwkt_replymsg(&nmsg->nm_lmsg, error);
688 static int
689 vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag)
691 struct netmsg_vlan vmsg;
692 struct netmsg *nmsg;
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);
700 vmsg.nv_ifv = ifv;
701 vmsg.nv_parent_name = parent_name;
702 vmsg.nv_vlantag = vlantag;
704 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
707 static void
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;
714 int cpu = mycpuid;
716 KASSERT(ifp_p->if_vlantrunks != NULL,
717 ("vlan trunk has not been initialized yet\n"));
718 entry = &ifv->ifv_entries[cpu];
720 crit_enter();
721 LIST_REMOVE(entry, ifv_link);
722 crit_exit();
724 vlan_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
727 static void
728 vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p)
730 struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks;
731 struct netmsg_vlan vmsg;
732 struct netmsg *nmsg;
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);
744 vmsg.nv_ifv = ifv;
745 vmsg.nv_ifp_p = ifp_p;
747 lwkt_domsg(ifa_portfn(0), &nmsg->nm_lmsg, 0);
749 crit_enter();
750 if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) {
751 #ifdef notyet
752 ifp_p->if_vlantrunks = NULL;
753 netmsg_service_sync();
754 kfree(vlantrunks, M_VLAN);
755 #else
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);
760 #endif
762 crit_exit();
765 static void
766 vlan_unconfig_dispatch(struct netmsg *nmsg)
768 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
769 struct sockaddr_dl *sdl;
770 struct ifvlan *ifv;
771 struct ifnet *ifp_p, *ifp;
772 int error;
774 /* Assert in netisr0 */
776 ifv = vmsg->nv_ifv;
777 ifp = &ifv->ifv_if;
779 if (ifp->if_flags & IFF_UP)
780 if_down(ifp);
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.
792 ifp_p = ifv->ifv_p;
793 ifv->ifv_p = NULL;
796 * Release vlan's serializer before reprogramming parent's
797 * multicast filter to avoid possible dead lock.
799 lwkt_serialize_exit(ifp->if_serializer);
801 if (ifp_p) {
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);
827 error = 0;
828 lwkt_replymsg(&nmsg->nm_lmsg, error);
831 static int
832 vlan_unconfig(struct ifvlan *ifv)
834 struct netmsg_vlan vmsg;
835 struct netmsg *nmsg;
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);
843 vmsg.nv_ifv = ifv;
845 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
848 static int
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;
853 struct ifnet *ifp_p;
854 struct vlanreq vlr;
855 int error = 0;
857 ASSERT_SERIALIZED(ifp->if_serializer);
859 switch (cmd) {
860 case SIOCGIFMEDIA:
861 ifp_p = ifv->ifv_p;
862 if (ifp_p != NULL) {
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. */
871 if (error == 0) {
872 struct ifmediareq *ifmr;
874 ifmr = (struct ifmediareq *) data;
875 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
876 ifmr->ifm_count = 1;
877 error = copyout(&ifmr->ifm_current,
878 ifmr->ifm_ulist,
879 sizeof(int));
882 } else {
883 error = EINVAL;
885 break;
887 case SIOCSIFMEDIA:
888 error = EINVAL;
889 break;
891 case SIOCSETVLAN:
892 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
893 if (error)
894 break;
896 lwkt_serialize_exit(ifp->if_serializer);
897 if (vlr.vlr_parent[0] == '\0')
898 error = vlan_unconfig(ifv);
899 else
900 error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag);
901 lwkt_serialize_enter(ifp->if_serializer);
902 break;
904 case SIOCGETVLAN:
905 bzero(&vlr, sizeof(vlr));
906 if (ifv->ifv_p) {
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);
912 break;
914 case SIOCSIFFLAGS:
915 if (ifp->if_flags & IFF_UP)
916 ifp->if_init(ifp);
917 else
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;
927 error = EINVAL;
929 break;
931 case SIOCADDMULTI:
932 case SIOCDELMULTI:
933 lwkt_serialize_exit(ifp->if_serializer);
934 error = vlan_config_multi(ifv);
935 lwkt_serialize_enter(ifp->if_serializer);
936 break;
938 default:
939 error = ether_ioctl(ifp, cmd, data);
940 break;
942 return error;
945 static void
946 vlan_multi_dispatch(struct netmsg *nmsg)
948 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
949 struct ifvlan *ifv = vmsg->nv_ifv;
950 int error = 0;
953 * If we don't have a parent, just remember the membership for
954 * when we do.
956 if (ifv->ifv_p != NULL)
957 error = vlan_setmulti(ifv, ifv->ifv_p);
958 lwkt_replymsg(&nmsg->nm_lmsg, error);
961 static int
962 vlan_config_multi(struct ifvlan *ifv)
964 struct netmsg_vlan vmsg;
965 struct netmsg *nmsg;
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);
973 vmsg.nv_ifv = ifv;
975 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);