Use netmsg_service_sync() to protect vlan_input_p
[dragonfly.git] / sys / net / vlan / if_vlan.c
CommitLineData
984263bc
MD
1/*
2 * Copyright 1998 Massachusetts Institute of Technology
3 *
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.
15 *
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.
28 *
29 * $FreeBSD: src/sys/net/if_vlan.c,v 1.15.2.13 2003/02/14 22:25:58 fenner Exp $
b327296f 30 * $DragonFly: src/sys/net/vlan/if_vlan.c,v 1.41 2008/09/23 11:50:11 sephe Exp $
984263bc
MD
31 */
32
33/*
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.
984263bc
MD
43 */
44
45#ifndef NVLAN
1f2de5d4 46#include "use_vlan.h"
984263bc
MD
47#endif
48#include "opt_inet.h"
49
50#include <sys/param.h>
1f7ab7c9 51#include <sys/systm.h>
984263bc
MD
52#include <sys/kernel.h>
53#include <sys/malloc.h>
54#include <sys/mbuf.h>
55#include <sys/module.h>
56#include <sys/queue.h>
57#include <sys/socket.h>
58#include <sys/sockio.h>
59#include <sys/sysctl.h>
1f7ab7c9 60#include <sys/bus.h>
126fc6c4 61#include <sys/thread2.h>
984263bc
MD
62
63#include <net/bpf.h>
64#include <net/ethernet.h>
65#include <net/if.h>
66#include <net/if_arp.h>
67#include <net/if_dl.h>
68#include <net/if_types.h>
4d723e5a 69#include <net/ifq_var.h>
65a24520 70#include <net/if_clone.h>
83790f85 71#include <net/netmsg2.h>
984263bc
MD
72
73#ifdef INET
74#include <netinet/in.h>
75#include <netinet/if_ether.h>
76#endif
77
4f7ea046
SZ
78#include <net/vlan/if_vlan_var.h>
79#include <net/vlan/if_vlan_ether.h>
80
9433ab01
SZ
81struct ifvlan;
82
83790f85
SZ
83struct vlan_mc_entry {
84 struct ether_addr mc_addr;
85 SLIST_ENTRY(vlan_mc_entry) mc_entries;
86};
87
9433ab01
SZ
88struct vlan_entry {
89 struct ifvlan *ifv;
90 LIST_ENTRY(vlan_entry) ifv_link;
91};
92
83790f85
SZ
93struct ifvlan {
94 struct arpcom ifv_ac; /* make this an interface */
95 struct ifnet *ifv_p; /* parent inteface of this vlan */
96 struct ifv_linkmib {
97 int ifvm_parent;
9433ab01
SZ
98 uint16_t ifvm_proto; /* encapsulation ethertype */
99 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
83790f85 100 } ifv_mib;
9433ab01 101 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
83790f85 102 LIST_ENTRY(ifvlan) ifv_list;
9433ab01 103 struct vlan_entry ifv_entries[1];
83790f85
SZ
104};
105#define ifv_if ifv_ac.ac_if
106#define ifv_tag ifv_mib.ifvm_tag
107
9433ab01
SZ
108struct vlan_trunk {
109 LIST_HEAD(, vlan_entry) vlan_list;
110};
111
112struct netmsg_vlan {
113 struct netmsg nv_nmsg;
114 struct ifvlan *nv_ifv;
115 struct ifnet *nv_ifp_p;
116 const char *nv_parent_name;
117 uint16_t nv_vlantag;
118};
119
984263bc 120#define VLANNAME "vlan"
984263bc
MD
121
122SYSCTL_DECL(_net_link);
123SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
124SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
125
126static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
984263bc
MD
127static LIST_HEAD(, ifvlan) ifv_list;
128
9433ab01
SZ
129static int vlan_clone_create(struct if_clone *, int);
130static void vlan_clone_destroy(struct ifnet *);
131static void vlan_ifdetach(void *, struct ifnet *);
132
133static void vlan_init(void *);
134static void vlan_start(struct ifnet *);
135static int vlan_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
50098e2e 136static void vlan_input(struct mbuf *);
9433ab01
SZ
137
138static void vlan_clrmulti(struct ifvlan *, struct ifnet *);
139static int vlan_setmulti(struct ifvlan *, struct ifnet *);
140static int vlan_config_multi(struct ifvlan *);
141static int vlan_config(struct ifvlan *, const char *, uint16_t);
142static int vlan_unconfig(struct ifvlan *);
143static void vlan_link(struct ifvlan *, struct ifnet *);
144static void vlan_unlink(struct ifvlan *, struct ifnet *);
145
146static void vlan_config_dispatch(struct netmsg *);
147static void vlan_unconfig_dispatch(struct netmsg *);
148static void vlan_link_dispatch(struct netmsg *);
149static void vlan_unlink_dispatch(struct netmsg *);
150static void vlan_multi_dispatch(struct netmsg *);
151static void vlan_ifdetach_dispatch(struct netmsg *);
152
153static eventhandler_tag vlan_ifdetach_cookie;
154static struct if_clone vlan_cloner =
155 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy,
156 NVLAN, IF_MAXUNIT);
157
984263bc
MD
158/*
159 * Program our multicast filter. What we're actually doing is
160 * programming the multicast filter of the parent. This has the
161 * side effect of causing the parent interface to receive multicast
162 * traffic that it doesn't really want, which ends up being discarded
163 * later by the upper protocol layers. Unfortunately, there's no way
164 * to avoid this: there really is only one physical interface.
165 */
166static int
9433ab01 167vlan_setmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
984263bc 168{
9433ab01
SZ
169 struct ifmultiaddr *ifma, *rifma = NULL;
170 struct vlan_mc_entry *mc = NULL;
171 struct sockaddr_dl sdl;
172 struct ifnet *ifp = &ifv->ifv_if;
984263bc 173
9433ab01 174 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
984263bc
MD
175
176 /*
9433ab01 177 * First, remove any existing filter entries.
984263bc 178 */
9433ab01 179 vlan_clrmulti(ifv, ifp_p);
984263bc 180
9433ab01
SZ
181 /*
182 * Now program new ones.
183 */
184 bzero(&sdl, sizeof(sdl));
185 sdl.sdl_len = sizeof(sdl);
984263bc
MD
186 sdl.sdl_family = AF_LINK;
187 sdl.sdl_index = ifp_p->if_index;
188 sdl.sdl_type = IFT_ETHER;
189 sdl.sdl_alen = ETHER_ADDR_LEN;
190
984263bc 191 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
9433ab01
SZ
192 int error;
193
984263bc
MD
194 if (ifma->ifma_addr->sa_family != AF_LINK)
195 continue;
9433ab01
SZ
196
197 /* Save a copy */
efda3bd0 198 mc = kmalloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
984263bc 199 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
9433ab01
SZ
200 &mc->mc_addr, ETHER_ADDR_LEN);
201 SLIST_INSERT_HEAD(&ifv->vlan_mc_listhead, mc, mc_entries);
202
203 /* Program the parent multicast filter */
984263bc 204 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
9433ab01 205 LLADDR(&sdl), ETHER_ADDR_LEN);
984263bc
MD
206 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
207 if (error)
9433ab01 208 return error;
984263bc 209 }
9433ab01
SZ
210 return 0;
211}
212
213static void
214vlan_clrmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
215{
216 struct vlan_mc_entry *mc;
217 struct sockaddr_dl sdl;
984263bc 218
9433ab01
SZ
219 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
220
221 bzero(&sdl, sizeof(sdl));
222 sdl.sdl_len = sizeof(sdl);
223 sdl.sdl_family = AF_LINK;
224 sdl.sdl_index = ifp_p->if_index;
225 sdl.sdl_type = IFT_ETHER;
226 sdl.sdl_alen = ETHER_ADDR_LEN;
227
228 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
229 bcopy(&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
230 if_delmulti(ifp_p, (struct sockaddr *)&sdl); /* ignore error */
231
232 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
233 kfree(mc, M_VLAN);
234 }
984263bc
MD
235}
236
237static int
9433ab01
SZ
238vlan_modevent(module_t mod, int type, void *data)
239{
240 switch (type) {
241 case MOD_LOAD:
984263bc 242 LIST_INIT(&ifv_list);
50098e2e 243 vlan_input_p = vlan_input;
9433ab01
SZ
244 vlan_ifdetach_cookie =
245 EVENTHANDLER_REGISTER(ifnet_detach_event,
246 vlan_ifdetach, NULL,
247 EVENTHANDLER_PRI_ANY);
984263bc 248 if_clone_attach(&vlan_cloner);
9433ab01
SZ
249 break;
250
251 case MOD_UNLOAD:
984263bc 252 if_clone_detach(&vlan_cloner);
b327296f 253
50098e2e 254 vlan_input_p = NULL;
b327296f
SZ
255 /*
256 * Make that all protocol threads see vlan_input_p change.
257 */
258 netmsg_service_sync();
259
9433ab01
SZ
260 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
261 vlan_ifdetach_cookie);
984263bc
MD
262 while (!LIST_EMPTY(&ifv_list))
263 vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
984263bc 264 break;
9433ab01
SZ
265 }
266 return 0;
267}
984263bc 268
9433ab01
SZ
269static moduledata_t vlan_mod = {
270 "if_vlan",
271 vlan_modevent,
984263bc 272 0
9433ab01 273};
984263bc
MD
274
275DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
276
9433ab01
SZ
277static void
278vlan_ifdetach_dispatch(struct netmsg *nmsg)
279{
280 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
281 struct ifnet *ifp_p = vmsg->nv_ifp_p;
282 struct vlan_trunk *vlantrunks, *trunk;
283 struct vlan_entry *ifve;
284
285 vlantrunks = ifp_p->if_vlantrunks;
286 if (vlantrunks == NULL)
287 goto reply;
288 trunk = &vlantrunks[mycpuid];
289
290 while (ifp_p->if_vlantrunks &&
291 (ifve = LIST_FIRST(&trunk->vlan_list)) != NULL)
292 vlan_unconfig(ifve->ifv);
293reply:
294 lwkt_replymsg(&nmsg->nm_lmsg, 0);
295}
296
297static void
298vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
299{
300 struct netmsg_vlan vmsg;
301 struct netmsg *nmsg;
302
303 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
304
305 bzero(&vmsg, sizeof(vmsg));
306 nmsg = &vmsg.nv_nmsg;
307
308 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_ifdetach_dispatch);
309 vmsg.nv_ifp_p = ifp;
310
311 lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
312}
313
984263bc 314static int
b7b982e2 315vlan_clone_create(struct if_clone *ifc, int unit)
984263bc 316{
984263bc
MD
317 struct ifvlan *ifv;
318 struct ifnet *ifp;
9433ab01 319 int vlan_size, i;
984263bc 320
9433ab01
SZ
321 vlan_size = sizeof(struct ifvlan)
322 + ((ncpus - 1) * sizeof(struct vlan_entry));
323 ifv = kmalloc(vlan_size, M_VLAN, M_WAITOK | M_ZERO);
984263bc 324 SLIST_INIT(&ifv->vlan_mc_listhead);
9433ab01
SZ
325 for (i = 0; i < ncpus; ++i)
326 ifv->ifv_entries[i].ifv = ifv;
984263bc 327
9433ab01 328 crit_enter(); /* XXX not MP safe */
984263bc 329 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
126fc6c4 330 crit_exit();
984263bc 331
9433ab01 332 ifp = &ifv->ifv_if;
984263bc 333 ifp->if_softc = ifv;
cdb7d804 334 if_initname(ifp, "vlan", unit);
984263bc
MD
335 /* NB: flags are not set here */
336 ifp->if_linkmib = &ifv->ifv_mib;
337 ifp->if_linkmiblen = sizeof ifv->ifv_mib;
338 /* NB: mtu is not set here */
339
9433ab01 340 ifp->if_init = vlan_init;
984263bc
MD
341 ifp->if_start = vlan_start;
342 ifp->if_ioctl = vlan_ioctl;
4d723e5a
JS
343 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
344 ifq_set_ready(&ifp->if_snd);
78195a76 345 ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr, NULL);
984263bc
MD
346 /* Now undo some of the damage... */
347 ifp->if_data.ifi_type = IFT_L2VLAN;
348 ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
349
350 return (0);
351}
352
353static void
354vlan_clone_destroy(struct ifnet *ifp)
355{
356 struct ifvlan *ifv = ifp->if_softc;
984263bc 357
9433ab01 358 crit_enter(); /* XXX not MP safe */
984263bc 359 LIST_REMOVE(ifv, ifv_list);
126fc6c4
JS
360 crit_exit();
361
9433ab01
SZ
362 vlan_unconfig(ifv);
363 ether_ifdetach(ifp);
364
efda3bd0 365 kfree(ifv, M_VLAN);
984263bc
MD
366}
367
368static void
9433ab01 369vlan_init(void *xsc)
984263bc 370{
9433ab01
SZ
371 struct ifvlan *ifv = xsc;
372 struct ifnet *ifp = &ifv->ifv_if;
373
374 ASSERT_SERIALIZED(ifp->if_serializer);
375
376 if (ifv->ifv_p != NULL)
377 ifp->if_flags |= IFF_RUNNING;
984263bc
MD
378}
379
380static void
381vlan_start(struct ifnet *ifp)
382{
9433ab01
SZ
383 struct ifvlan *ifv = ifp->if_softc;
384 struct ifnet *ifp_p = ifv->ifv_p;
984263bc
MD
385 struct mbuf *m;
386
9433ab01
SZ
387 ASSERT_SERIALIZED(ifp->if_serializer);
388
c7ecdcdf
SZ
389 if (ifp_p == NULL) {
390 ifq_purge(&ifp->if_snd);
391 return;
392 }
393
394 if ((ifp->if_flags & IFF_RUNNING) == 0)
9433ab01 395 return;
984263bc 396
984263bc 397 for (;;) {
83790f85
SZ
398 struct netmsg_packet *nmp;
399 struct netmsg *nmsg;
400 struct lwkt_port *port;
401
d2c71fa0
MD
402 m = ifq_dequeue(&ifp->if_snd, NULL);
403 if (m == NULL)
984263bc 404 break;
7600679e 405 BPF_MTAP(ifp, m);
984263bc
MD
406
407 /*
408 * Do not run parent's if_start() if the parent is not up,
409 * or parent's driver will cause a system crash.
410 */
83790f85
SZ
411 if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) !=
412 (IFF_UP | IFF_RUNNING)) {
984263bc
MD
413 m_freem(m);
414 ifp->if_data.ifi_collisions++;
415 continue;
416 }
417
4d723e5a 418 /*
83790f85
SZ
419 * We need some way to tell the interface where the packet
420 * came from so that it knows how to find the VLAN tag to
421 * use, so we set the ether_vlantag in the mbuf packet header
422 * to our vlan tag. We also set the M_VLANTAG flag in the
423 * mbuf to let the parent driver know that the ether_vlantag
424 * is really valid.
4d723e5a 425 */
83790f85
SZ
426 m->m_pkthdr.ether_vlantag = ifv->ifv_tag;
427 m->m_flags |= M_VLANTAG;
4d723e5a 428
83790f85
SZ
429 nmp = &m->m_hdr.mh_netmsg;
430 nmsg = &nmp->nm_netmsg;
984263bc 431
83790f85
SZ
432 netmsg_init(nmsg, &netisr_apanic_rport, 0, vlan_start_dispatch);
433 nmp->nm_packet = m;
434 nmsg->nm_lmsg.u.ms_resultp = ifp_p;
435
436 port = cpu_portfn(ifp_p->if_index % ncpus /* XXX */);
437 lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
438 ifp->if_opackets++;
984263bc 439 }
984263bc
MD
440}
441
7af50411 442static void
50098e2e 443vlan_input(struct mbuf *m)
7af50411
SZ
444{
445 struct ifvlan *ifv = NULL;
446 struct ifnet *rcvif, *ifp;
447 struct vlan_trunk *vlantrunks;
448 struct vlan_entry *entry;
449
450 rcvif = m->m_pkthdr.rcvif;
451 KKASSERT(m->m_flags & M_VLANTAG);
452
453 vlantrunks = rcvif->if_vlantrunks;
454 if (vlantrunks == NULL) {
455 rcvif->if_noproto++;
456 m_freem(m);
457 return;
458 }
459
460 crit_enter(); /* XXX Necessary? */
461 LIST_FOREACH(entry, &vlantrunks[mycpuid].vlan_list, ifv_link) {
462 if (entry->ifv->ifv_tag ==
463 EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag)) {
464 ifv = entry->ifv;
465 break;
466 }
467 }
468 crit_exit();
469
470 /*
471 * Packet is discarded if:
472 * - no corresponding vlan(4) interface
473 * - vlan(4) interface has not been completely set up yet,
474 * or is being destroyed (ifv->ifv_p != rcvif)
475 * - vlan(4) interface is not brought up
476 */
477 if (ifv == NULL || ifv->ifv_p != rcvif ||
478 (ifv->ifv_if.if_flags & IFF_UP) == 0) {
479 rcvif->if_noproto++;
480 m_freem(m);
481 return;
482 }
483 ifp = &ifv->ifv_if;
484
485 /*
486 * Clear M_VLANTAG, before the packet is handed to
487 * vlan(4) interface
488 */
489 m->m_flags &= ~M_VLANTAG;
490
491 /* Change receiving interface */
492 m->m_pkthdr.rcvif = ifp;
493
494 /* Update statistics */
495 ifp->if_ipackets++;
496 ifp->if_ibytes += m->m_pkthdr.len;
497 if (m->m_flags & (M_MCAST | M_BCAST))
498 ifp->if_imcasts++;
499
500 BPF_MTAP(ifp, m);
501
502 if (ifp->if_flags & IFF_MONITOR) {
503 /*
504 * Interface marked for monitoring; discard packet.
505 */
506 m_freem(m);
507 return;
508 }
509 ether_input_oncpu(ifp, m);
510}
511
9433ab01
SZ
512static void
513vlan_link_dispatch(struct netmsg *nmsg)
514{
515 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
516 struct ifvlan *ifv = vmsg->nv_ifv;
517 struct ifnet *ifp_p = vmsg->nv_ifp_p;
518 struct vlan_entry *entry;
519 struct vlan_trunk *vlantrunks, *trunk;
520 int cpu = mycpuid;
521
522 vlantrunks = ifp_p->if_vlantrunks;
523 KASSERT(vlantrunks != NULL,
524 ("vlan trunk has not been initialized yet\n"));
525
526 entry = &ifv->ifv_entries[cpu];
527 trunk = &vlantrunks[cpu];
528
529 crit_enter();
530 LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link);
531 crit_exit();
532
4e9c6c84 533 ifnet_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
9433ab01
SZ
534}
535
536static void
537vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p)
984263bc 538{
9433ab01
SZ
539 struct netmsg_vlan vmsg;
540 struct netmsg *nmsg;
541
542 /* Assert in netisr0 */
543 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
544
545 if (ifp_p->if_vlantrunks == NULL) {
546 struct vlan_trunk *vlantrunks;
547 int i;
548
549 vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN,
550 M_WAITOK | M_ZERO);
551 for (i = 0; i < ncpus; ++i)
552 LIST_INIT(&vlantrunks[i].vlan_list);
553
554 ifp_p->if_vlantrunks = vlantrunks;
555 }
556
557 bzero(&vmsg, sizeof(vmsg));
558 nmsg = &vmsg.nv_nmsg;
559
560 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_link_dispatch);
561 vmsg.nv_ifv = ifv;
562 vmsg.nv_ifp_p = ifp_p;
563
4e9c6c84 564 ifnet_domsg(&nmsg->nm_lmsg, 0);
9433ab01
SZ
565}
566
567static void
568vlan_config_dispatch(struct netmsg *nmsg)
569{
570 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
571 struct ifvlan *ifv;
572 struct ifnet *ifp_p, *ifp;
984263bc 573 struct sockaddr_dl *sdl1, *sdl2;
9433ab01
SZ
574 int error;
575
576 /* Assert in netisr0 */
577
578 ifp_p = ifunit(vmsg->nv_parent_name);
579 if (ifp_p == NULL) {
580 error = ENOENT;
581 goto reply;
582 }
583
584 if (ifp_p->if_data.ifi_type != IFT_ETHER) {
585 error = EPROTONOSUPPORT;
586 goto reply;
587 }
588
589 ifv = vmsg->nv_ifv;
590 ifp = &ifv->ifv_if;
984263bc 591
9433ab01
SZ
592 if (ifv->ifv_p) {
593 error = EBUSY;
594 goto reply;
595 }
596
597 /* Link vlan into parent's vlantrunk */
598 vlan_link(ifv, ifp_p);
599
600 lwkt_serialize_enter(ifp->if_serializer);
601
602 ifv->ifv_tag = vmsg->nv_vlantag;
603 if (ifp_p->if_capenable & IFCAP_VLAN_MTU)
604 ifp->if_mtu = ifp_p->if_mtu;
984263bc 605 else
9433ab01 606 ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN;
984263bc
MD
607
608 /*
609 * Copy only a selected subset of flags from the parent.
610 * Other flags are none of our business.
611 */
9433ab01 612 ifp->if_flags = (ifp_p->if_flags &
984263bc
MD
613 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT));
614
615 /*
616 * Set up our ``Ethernet address'' to reflect the underlying
617 * physical interface's.
618 */
9433ab01
SZ
619 sdl1 = IF_LLSOCKADDR(ifp);
620 sdl2 = IF_LLSOCKADDR(ifp_p);
984263bc
MD
621 sdl1->sdl_type = IFT_ETHER;
622 sdl1->sdl_alen = ETHER_ADDR_LEN;
623 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
624 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
625
9433ab01
SZ
626 /*
627 * Release vlan's serializer before reprogramming parent's
628 * multicast filter to avoid possible dead lock.
629 */
630 lwkt_serialize_exit(ifp->if_serializer);
631
984263bc
MD
632 /*
633 * Configure multicast addresses that may already be
634 * joined on the vlan device.
635 */
9433ab01 636 vlan_setmulti(ifv, ifp_p);
984263bc 637
9433ab01
SZ
638 /*
639 * Connect to parent after everything have been set up,
640 * so input/output could know that vlan is ready to go
641 */
642 ifv->ifv_p = ifp_p;
643 error = 0;
644reply:
645 lwkt_replymsg(&nmsg->nm_lmsg, error);
984263bc
MD
646}
647
648static int
9433ab01
SZ
649vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag)
650{
651 struct netmsg_vlan vmsg;
652 struct netmsg *nmsg;
653
654 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
655
656 bzero(&vmsg, sizeof(vmsg));
657 nmsg = &vmsg.nv_nmsg;
658
659 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_config_dispatch);
660 vmsg.nv_ifv = ifv;
661 vmsg.nv_parent_name = parent_name;
662 vmsg.nv_vlantag = vlantag;
663
664 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
665}
666
667static void
668vlan_unlink_dispatch(struct netmsg *nmsg)
669{
670 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
671 struct ifvlan *ifv = vmsg->nv_ifv;
9433ab01
SZ
672 struct vlan_entry *entry;
673 int cpu = mycpuid;
674
2700fbed 675 KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL,
9433ab01
SZ
676 ("vlan trunk has not been initialized yet\n"));
677 entry = &ifv->ifv_entries[cpu];
678
679 crit_enter();
680 LIST_REMOVE(entry, ifv_link);
681 crit_exit();
682
4e9c6c84 683 ifnet_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
9433ab01
SZ
684}
685
686static void
687vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p)
984263bc 688{
9433ab01
SZ
689 struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks;
690 struct netmsg_vlan vmsg;
691 struct netmsg *nmsg;
692
693 /* Assert in netisr0 */
694 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
695
696 KASSERT(ifp_p->if_vlantrunks != NULL,
697 ("vlan trunk has not been initialized yet\n"));
698
699 bzero(&vmsg, sizeof(vmsg));
700 nmsg = &vmsg.nv_nmsg;
701
702 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_unlink_dispatch);
703 vmsg.nv_ifv = ifv;
704 vmsg.nv_ifp_p = ifp_p;
705
4e9c6c84 706 ifnet_domsg(&nmsg->nm_lmsg, 0);
9433ab01
SZ
707
708 crit_enter();
709 if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) {
9433ab01 710 ifp_p->if_vlantrunks = NULL;
297c8124
SZ
711
712 /*
713 * Make that all protocol threads see if_vlantrunks change.
714 */
9433ab01
SZ
715 netmsg_service_sync();
716 kfree(vlantrunks, M_VLAN);
9433ab01
SZ
717 }
718 crit_exit();
719}
720
721static void
722vlan_unconfig_dispatch(struct netmsg *nmsg)
723{
724 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
984263bc 725 struct sockaddr_dl *sdl;
984263bc 726 struct ifvlan *ifv;
9433ab01 727 struct ifnet *ifp_p, *ifp;
984263bc
MD
728 int error;
729
9433ab01
SZ
730 /* Assert in netisr0 */
731
732 ifv = vmsg->nv_ifv;
733 ifp = &ifv->ifv_if;
734
735 if (ifp->if_flags & IFF_UP)
736 if_down(ifp);
984263bc 737
9433ab01
SZ
738 lwkt_serialize_enter(ifp->if_serializer);
739
740 ifp->if_flags &= ~IFF_RUNNING;
741
742 /*
743 * Save parent ifnet pointer and disconnect from parent.
744 *
745 * This is done early in this function, so input/output could
746 * know that we are disconnecting.
747 */
748 ifp_p = ifv->ifv_p;
749 ifv->ifv_p = NULL;
750
751 /*
752 * Release vlan's serializer before reprogramming parent's
753 * multicast filter to avoid possible dead lock.
754 */
755 lwkt_serialize_exit(ifp->if_serializer);
984263bc 756
9433ab01 757 if (ifp_p) {
984263bc
MD
758 /*
759 * Since the interface is being unconfigured, we need to
760 * empty the list of multicast groups that we may have joined
761 * while we were alive from the parent's list.
762 */
9433ab01 763 vlan_clrmulti(ifv, ifp_p);
984263bc
MD
764 }
765
9433ab01
SZ
766 lwkt_serialize_enter(ifp->if_serializer);
767
768 ifp->if_mtu = ETHERMTU;
984263bc
MD
769
770 /* Clear our MAC address. */
9433ab01 771 sdl = IF_LLSOCKADDR(ifp);
984263bc
MD
772 sdl->sdl_type = IFT_ETHER;
773 sdl->sdl_alen = ETHER_ADDR_LEN;
774 bzero(LLADDR(sdl), ETHER_ADDR_LEN);
775 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
776
9433ab01
SZ
777 lwkt_serialize_exit(ifp->if_serializer);
778
779 /* Unlink vlan from parent's vlantrunk */
780 if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL)
781 vlan_unlink(ifv, ifp_p);
782
783 error = 0;
784 lwkt_replymsg(&nmsg->nm_lmsg, error);
785}
786
787static int
788vlan_unconfig(struct ifvlan *ifv)
789{
790 struct netmsg_vlan vmsg;
791 struct netmsg *nmsg;
792
793 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
794
795 bzero(&vmsg, sizeof(vmsg));
796 nmsg = &vmsg.nv_nmsg;
797
798 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_unconfig_dispatch);
799 vmsg.nv_ifv = ifv;
800
801 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
984263bc
MD
802}
803
804static int
bd4539cc 805vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
984263bc 806{
9433ab01
SZ
807 struct ifvlan *ifv = ifp->if_softc;
808 struct ifreq *ifr = (struct ifreq *)data;
809 struct ifnet *ifp_p;
984263bc
MD
810 struct vlanreq vlr;
811 int error = 0;
812
78195a76 813 ASSERT_SERIALIZED(ifp->if_serializer);
126fc6c4 814
984263bc 815 switch (cmd) {
984263bc 816 case SIOCGIFMEDIA:
9433ab01
SZ
817 ifp_p = ifv->ifv_p;
818 if (ifp_p != NULL) {
7d50fe58
SZ
819 /*
820 * Release vlan interface's serializer to void
821 * possible dead lock.
822 */
78195a76 823 lwkt_serialize_exit(ifp->if_serializer);
9433ab01
SZ
824
825 lwkt_serialize_enter(ifp_p->if_serializer);
826 error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr);
827 lwkt_serialize_exit(ifp_p->if_serializer);
828
78195a76 829 lwkt_serialize_enter(ifp->if_serializer);
7d50fe58
SZ
830
831 if (ifv->ifv_p == NULL && ifv->ifv_p != ifp_p) {
832 /*
833 * We are disconnected from the original
834 * parent interface or the parent interface
835 * is changed, after vlan interface's
836 * serializer is released.
837 */
838 error = EINVAL;
839 }
840
984263bc
MD
841 /* Limit the result to the parent's current config. */
842 if (error == 0) {
843 struct ifmediareq *ifmr;
844
845 ifmr = (struct ifmediareq *) data;
846 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
847 ifmr->ifm_count = 1;
848 error = copyout(&ifmr->ifm_current,
849 ifmr->ifm_ulist,
850 sizeof(int));
851 }
852 }
9433ab01 853 } else {
984263bc 854 error = EINVAL;
9433ab01 855 }
984263bc
MD
856 break;
857
858 case SIOCSIFMEDIA:
859 error = EINVAL;
860 break;
861
984263bc
MD
862 case SIOCSETVLAN:
863 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
864 if (error)
865 break;
9433ab01
SZ
866
867 lwkt_serialize_exit(ifp->if_serializer);
868 if (vlr.vlr_parent[0] == '\0')
869 error = vlan_unconfig(ifv);
870 else
871 error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag);
872 lwkt_serialize_enter(ifp->if_serializer);
984263bc 873 break;
9433ab01 874
984263bc 875 case SIOCGETVLAN:
9433ab01 876 bzero(&vlr, sizeof(vlr));
984263bc 877 if (ifv->ifv_p) {
cdb7d804
MD
878 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
879 sizeof(vlr.vlr_parent));
984263bc
MD
880 vlr.vlr_tag = ifv->ifv_tag;
881 }
882 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
883 break;
7d50fe58 884
984263bc 885 case SIOCSIFFLAGS:
9433ab01
SZ
886 if (ifp->if_flags & IFF_UP)
887 ifp->if_init(ifp);
888 else
889 ifp->if_flags &= ~IFF_RUNNING;
890
984263bc
MD
891 /*
892 * We don't support promiscuous mode
893 * right now because it would require help from the
894 * underlying drivers, which hasn't been implemented.
895 */
9433ab01
SZ
896 if (ifr->ifr_flags & IFF_PROMISC) {
897 ifp->if_flags &= ~IFF_PROMISC;
984263bc
MD
898 error = EINVAL;
899 }
900 break;
212db56c 901
984263bc
MD
902 case SIOCADDMULTI:
903 case SIOCDELMULTI:
9433ab01
SZ
904 lwkt_serialize_exit(ifp->if_serializer);
905 error = vlan_config_multi(ifv);
906 lwkt_serialize_enter(ifp->if_serializer);
984263bc 907 break;
212db56c 908
984263bc 909 default:
212db56c
SZ
910 error = ether_ioctl(ifp, cmd, data);
911 break;
984263bc 912 }
9433ab01
SZ
913 return error;
914}
126fc6c4 915
9433ab01
SZ
916static void
917vlan_multi_dispatch(struct netmsg *nmsg)
918{
919 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg;
920 struct ifvlan *ifv = vmsg->nv_ifv;
921 int error = 0;
126fc6c4 922
9433ab01
SZ
923 /*
924 * If we don't have a parent, just remember the membership for
925 * when we do.
926 */
927 if (ifv->ifv_p != NULL)
928 error = vlan_setmulti(ifv, ifv->ifv_p);
929 lwkt_replymsg(&nmsg->nm_lmsg, error);
930}
931
932static int
933vlan_config_multi(struct ifvlan *ifv)
934{
935 struct netmsg_vlan vmsg;
936 struct netmsg *nmsg;
937
938 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer);
939
940 bzero(&vmsg, sizeof(vmsg));
941 nmsg = &vmsg.nv_nmsg;
942
943 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_multi_dispatch);
944 vmsg.nv_ifv = ifv;
945
946 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
984263bc 947}